xref: /openbmc/linux/mm/vmscan.c (revision 7cc30fcfd2a894589d832a192cac3dc5cd302bb8)
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>
49f9fe48beSRoss Zwisler #include <linux/dax.h>
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #include <asm/tlbflush.h>
521da177e4SLinus Torvalds #include <asm/div64.h>
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds #include <linux/swapops.h>
55117aad1eSRafael Aquini #include <linux/balloon_compaction.h>
561da177e4SLinus Torvalds 
570f8053a5SNick Piggin #include "internal.h"
580f8053a5SNick Piggin 
5933906bc5SMel Gorman #define CREATE_TRACE_POINTS
6033906bc5SMel Gorman #include <trace/events/vmscan.h>
6133906bc5SMel Gorman 
621da177e4SLinus Torvalds struct scan_control {
6322fba335SKOSAKI Motohiro 	/* How many pages shrink_list() should reclaim */
6422fba335SKOSAKI Motohiro 	unsigned long nr_to_reclaim;
6522fba335SKOSAKI Motohiro 
661da177e4SLinus Torvalds 	/* This context's GFP mask */
676daa0e28SAl Viro 	gfp_t gfp_mask;
681da177e4SLinus Torvalds 
69ee814fe2SJohannes Weiner 	/* Allocation order */
705ad333ebSAndy Whitcroft 	int order;
7166e1707bSBalbir Singh 
72ee814fe2SJohannes Weiner 	/*
73ee814fe2SJohannes Weiner 	 * Nodemask of nodes allowed by the caller. If NULL, all nodes
74ee814fe2SJohannes Weiner 	 * are scanned.
75ee814fe2SJohannes Weiner 	 */
76ee814fe2SJohannes Weiner 	nodemask_t	*nodemask;
779e3b2f8cSKonstantin Khlebnikov 
785f53e762SKOSAKI Motohiro 	/*
79f16015fbSJohannes Weiner 	 * The memory cgroup that hit its limit and as a result is the
80f16015fbSJohannes Weiner 	 * primary target of this reclaim invocation.
81f16015fbSJohannes Weiner 	 */
82f16015fbSJohannes Weiner 	struct mem_cgroup *target_mem_cgroup;
8366e1707bSBalbir Singh 
84ee814fe2SJohannes Weiner 	/* Scan (total_size >> priority) pages at once */
85ee814fe2SJohannes Weiner 	int priority;
86ee814fe2SJohannes Weiner 
87b2e18757SMel Gorman 	/* The highest zone to isolate pages for reclaim from */
88b2e18757SMel Gorman 	enum zone_type reclaim_idx;
89b2e18757SMel Gorman 
90ee814fe2SJohannes Weiner 	unsigned int may_writepage:1;
91ee814fe2SJohannes Weiner 
92ee814fe2SJohannes Weiner 	/* Can mapped pages be reclaimed? */
93ee814fe2SJohannes Weiner 	unsigned int may_unmap:1;
94ee814fe2SJohannes Weiner 
95ee814fe2SJohannes Weiner 	/* Can pages be swapped as part of reclaim? */
96ee814fe2SJohannes Weiner 	unsigned int may_swap:1;
97ee814fe2SJohannes Weiner 
98241994edSJohannes Weiner 	/* Can cgroups be reclaimed below their normal consumption range? */
99241994edSJohannes Weiner 	unsigned int may_thrash:1;
100241994edSJohannes Weiner 
101ee814fe2SJohannes Weiner 	unsigned int hibernation_mode:1;
102ee814fe2SJohannes Weiner 
103ee814fe2SJohannes Weiner 	/* One of the zones is ready for compaction */
104ee814fe2SJohannes Weiner 	unsigned int compaction_ready:1;
105ee814fe2SJohannes Weiner 
106ee814fe2SJohannes Weiner 	/* Incremented by the number of inactive pages that were scanned */
107ee814fe2SJohannes Weiner 	unsigned long nr_scanned;
108ee814fe2SJohannes Weiner 
109ee814fe2SJohannes Weiner 	/* Number of pages freed so far during a call to shrink_zones() */
110ee814fe2SJohannes Weiner 	unsigned long nr_reclaimed;
1111da177e4SLinus Torvalds };
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCH
1141da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field)			\
1151da177e4SLinus Torvalds 	do {								\
1161da177e4SLinus Torvalds 		if ((_page)->lru.prev != _base) {			\
1171da177e4SLinus Torvalds 			struct page *prev;				\
1181da177e4SLinus Torvalds 									\
1191da177e4SLinus Torvalds 			prev = lru_to_page(&(_page->lru));		\
1201da177e4SLinus Torvalds 			prefetch(&prev->_field);			\
1211da177e4SLinus Torvalds 		}							\
1221da177e4SLinus Torvalds 	} while (0)
1231da177e4SLinus Torvalds #else
1241da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
1251da177e4SLinus Torvalds #endif
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW
1281da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field)			\
1291da177e4SLinus Torvalds 	do {								\
1301da177e4SLinus Torvalds 		if ((_page)->lru.prev != _base) {			\
1311da177e4SLinus Torvalds 			struct page *prev;				\
1321da177e4SLinus Torvalds 									\
1331da177e4SLinus Torvalds 			prev = lru_to_page(&(_page->lru));		\
1341da177e4SLinus Torvalds 			prefetchw(&prev->_field);			\
1351da177e4SLinus Torvalds 		}							\
1361da177e4SLinus Torvalds 	} while (0)
1371da177e4SLinus Torvalds #else
1381da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
1391da177e4SLinus Torvalds #endif
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds /*
1421da177e4SLinus Torvalds  * From 0 .. 100.  Higher means more swappy.
1431da177e4SLinus Torvalds  */
1441da177e4SLinus Torvalds int vm_swappiness = 60;
145d0480be4SWang Sheng-Hui /*
146d0480be4SWang Sheng-Hui  * The total number of pages which are beyond the high watermark within all
147d0480be4SWang Sheng-Hui  * zones.
148d0480be4SWang Sheng-Hui  */
149d0480be4SWang Sheng-Hui unsigned long vm_total_pages;
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds static LIST_HEAD(shrinker_list);
1521da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem);
1531da177e4SLinus Torvalds 
154c255a458SAndrew Morton #ifdef CONFIG_MEMCG
15589b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc)
15689b5fae5SJohannes Weiner {
157f16015fbSJohannes Weiner 	return !sc->target_mem_cgroup;
15889b5fae5SJohannes Weiner }
15997c9341fSTejun Heo 
16097c9341fSTejun Heo /**
16197c9341fSTejun Heo  * sane_reclaim - is the usual dirty throttling mechanism operational?
16297c9341fSTejun Heo  * @sc: scan_control in question
16397c9341fSTejun Heo  *
16497c9341fSTejun Heo  * The normal page dirty throttling mechanism in balance_dirty_pages() is
16597c9341fSTejun Heo  * completely broken with the legacy memcg and direct stalling in
16697c9341fSTejun Heo  * shrink_page_list() is used for throttling instead, which lacks all the
16797c9341fSTejun Heo  * niceties such as fairness, adaptive pausing, bandwidth proportional
16897c9341fSTejun Heo  * allocation and configurability.
16997c9341fSTejun Heo  *
17097c9341fSTejun Heo  * This function tests whether the vmscan currently in progress can assume
17197c9341fSTejun Heo  * that the normal dirty throttling mechanism is operational.
17297c9341fSTejun Heo  */
17397c9341fSTejun Heo static bool sane_reclaim(struct scan_control *sc)
17497c9341fSTejun Heo {
17597c9341fSTejun Heo 	struct mem_cgroup *memcg = sc->target_mem_cgroup;
17697c9341fSTejun Heo 
17797c9341fSTejun Heo 	if (!memcg)
17897c9341fSTejun Heo 		return true;
17997c9341fSTejun Heo #ifdef CONFIG_CGROUP_WRITEBACK
18069234aceSLinus Torvalds 	if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
18197c9341fSTejun Heo 		return true;
18297c9341fSTejun Heo #endif
18397c9341fSTejun Heo 	return false;
18497c9341fSTejun Heo }
18591a45470SKAMEZAWA Hiroyuki #else
18689b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc)
18789b5fae5SJohannes Weiner {
18889b5fae5SJohannes Weiner 	return true;
18989b5fae5SJohannes Weiner }
19097c9341fSTejun Heo 
19197c9341fSTejun Heo static bool sane_reclaim(struct scan_control *sc)
19297c9341fSTejun Heo {
19397c9341fSTejun Heo 	return true;
19497c9341fSTejun Heo }
19591a45470SKAMEZAWA Hiroyuki #endif
19691a45470SKAMEZAWA Hiroyuki 
197599d0c95SMel Gorman /*
198599d0c95SMel Gorman  * This misses isolated pages which are not accounted for to save counters.
199599d0c95SMel Gorman  * As the data only determines if reclaim or compaction continues, it is
200599d0c95SMel Gorman  * not expected that isolated pages will be a dominating factor.
201599d0c95SMel Gorman  */
2020a0337e0SMichal Hocko unsigned long zone_reclaimable_pages(struct zone *zone)
2036e543d57SLisa Du {
204d031a157SAlexandru Moise 	unsigned long nr;
2056e543d57SLisa Du 
206599d0c95SMel Gorman 	nr = zone_page_state_snapshot(zone, NR_ZONE_LRU_FILE);
2076e543d57SLisa Du 	if (get_nr_swap_pages() > 0)
208599d0c95SMel Gorman 		nr += zone_page_state_snapshot(zone, NR_ZONE_LRU_ANON);
2096e543d57SLisa Du 
2106e543d57SLisa Du 	return nr;
2116e543d57SLisa Du }
2126e543d57SLisa Du 
213599d0c95SMel Gorman unsigned long pgdat_reclaimable_pages(struct pglist_data *pgdat)
2146e543d57SLisa Du {
215599d0c95SMel Gorman 	unsigned long nr;
216599d0c95SMel Gorman 
217599d0c95SMel Gorman 	nr = node_page_state_snapshot(pgdat, NR_ACTIVE_FILE) +
218599d0c95SMel Gorman 	     node_page_state_snapshot(pgdat, NR_INACTIVE_FILE) +
219599d0c95SMel Gorman 	     node_page_state_snapshot(pgdat, NR_ISOLATED_FILE);
220599d0c95SMel Gorman 
221599d0c95SMel Gorman 	if (get_nr_swap_pages() > 0)
222599d0c95SMel Gorman 		nr += node_page_state_snapshot(pgdat, NR_ACTIVE_ANON) +
223599d0c95SMel Gorman 		      node_page_state_snapshot(pgdat, NR_INACTIVE_ANON) +
224599d0c95SMel Gorman 		      node_page_state_snapshot(pgdat, NR_ISOLATED_ANON);
225599d0c95SMel Gorman 
226599d0c95SMel Gorman 	return nr;
227599d0c95SMel Gorman }
228599d0c95SMel Gorman 
229599d0c95SMel Gorman bool pgdat_reclaimable(struct pglist_data *pgdat)
230599d0c95SMel Gorman {
231599d0c95SMel Gorman 	return node_page_state_snapshot(pgdat, NR_PAGES_SCANNED) <
232599d0c95SMel Gorman 		pgdat_reclaimable_pages(pgdat) * 6;
2336e543d57SLisa Du }
2346e543d57SLisa Du 
23523047a96SJohannes Weiner unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru)
236c9f299d9SKOSAKI Motohiro {
237c3c787e8SHugh Dickins 	if (!mem_cgroup_disabled())
2384d7dcca2SHugh Dickins 		return mem_cgroup_get_lru_size(lruvec, lru);
239a3d8e054SKOSAKI Motohiro 
240599d0c95SMel Gorman 	return node_page_state(lruvec_pgdat(lruvec), NR_LRU_BASE + lru);
241c9f299d9SKOSAKI Motohiro }
242c9f299d9SKOSAKI Motohiro 
2431da177e4SLinus Torvalds /*
2441d3d4437SGlauber Costa  * Add a shrinker callback to be called from the vm.
2451da177e4SLinus Torvalds  */
2461d3d4437SGlauber Costa int register_shrinker(struct shrinker *shrinker)
2471da177e4SLinus Torvalds {
2481d3d4437SGlauber Costa 	size_t size = sizeof(*shrinker->nr_deferred);
2491d3d4437SGlauber Costa 
2501d3d4437SGlauber Costa 	if (shrinker->flags & SHRINKER_NUMA_AWARE)
2511d3d4437SGlauber Costa 		size *= nr_node_ids;
2521d3d4437SGlauber Costa 
2531d3d4437SGlauber Costa 	shrinker->nr_deferred = kzalloc(size, GFP_KERNEL);
2541d3d4437SGlauber Costa 	if (!shrinker->nr_deferred)
2551d3d4437SGlauber Costa 		return -ENOMEM;
2561d3d4437SGlauber Costa 
2571da177e4SLinus Torvalds 	down_write(&shrinker_rwsem);
2581da177e4SLinus Torvalds 	list_add_tail(&shrinker->list, &shrinker_list);
2591da177e4SLinus Torvalds 	up_write(&shrinker_rwsem);
2601d3d4437SGlauber Costa 	return 0;
2611da177e4SLinus Torvalds }
2628e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker);
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds /*
2651da177e4SLinus Torvalds  * Remove one
2661da177e4SLinus Torvalds  */
2678e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker)
2681da177e4SLinus Torvalds {
2691da177e4SLinus Torvalds 	down_write(&shrinker_rwsem);
2701da177e4SLinus Torvalds 	list_del(&shrinker->list);
2711da177e4SLinus Torvalds 	up_write(&shrinker_rwsem);
272ae393321SAndrew Vagin 	kfree(shrinker->nr_deferred);
2731da177e4SLinus Torvalds }
2748e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker);
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds #define SHRINK_BATCH 128
2771d3d4437SGlauber Costa 
278cb731d6cSVladimir Davydov static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,
2796b4f7799SJohannes Weiner 				    struct shrinker *shrinker,
2806b4f7799SJohannes Weiner 				    unsigned long nr_scanned,
2816b4f7799SJohannes Weiner 				    unsigned long nr_eligible)
2821da177e4SLinus Torvalds {
28324f7c6b9SDave Chinner 	unsigned long freed = 0;
2841da177e4SLinus Torvalds 	unsigned long long delta;
285635697c6SKonstantin Khlebnikov 	long total_scan;
286d5bc5fd3SVladimir Davydov 	long freeable;
287acf92b48SDave Chinner 	long nr;
288acf92b48SDave Chinner 	long new_nr;
2891d3d4437SGlauber Costa 	int nid = shrinkctl->nid;
290e9299f50SDave Chinner 	long batch_size = shrinker->batch ? shrinker->batch
291e9299f50SDave Chinner 					  : SHRINK_BATCH;
2921da177e4SLinus Torvalds 
293d5bc5fd3SVladimir Davydov 	freeable = shrinker->count_objects(shrinker, shrinkctl);
294d5bc5fd3SVladimir Davydov 	if (freeable == 0)
2951d3d4437SGlauber Costa 		return 0;
296635697c6SKonstantin Khlebnikov 
297acf92b48SDave Chinner 	/*
298acf92b48SDave Chinner 	 * copy the current shrinker scan count into a local variable
299acf92b48SDave Chinner 	 * and zero it so that other concurrent shrinker invocations
300acf92b48SDave Chinner 	 * don't also do this scanning work.
301acf92b48SDave Chinner 	 */
3021d3d4437SGlauber Costa 	nr = atomic_long_xchg(&shrinker->nr_deferred[nid], 0);
303acf92b48SDave Chinner 
304acf92b48SDave Chinner 	total_scan = nr;
3056b4f7799SJohannes Weiner 	delta = (4 * nr_scanned) / shrinker->seeks;
306d5bc5fd3SVladimir Davydov 	delta *= freeable;
3076b4f7799SJohannes Weiner 	do_div(delta, nr_eligible + 1);
308acf92b48SDave Chinner 	total_scan += delta;
309acf92b48SDave Chinner 	if (total_scan < 0) {
3108612c663SPintu Kumar 		pr_err("shrink_slab: %pF negative objects to delete nr=%ld\n",
311a0b02131SDave Chinner 		       shrinker->scan_objects, total_scan);
312d5bc5fd3SVladimir Davydov 		total_scan = freeable;
313ea164d73SAndrea Arcangeli 	}
314ea164d73SAndrea Arcangeli 
315ea164d73SAndrea Arcangeli 	/*
3163567b59aSDave Chinner 	 * We need to avoid excessive windup on filesystem shrinkers
3173567b59aSDave Chinner 	 * due to large numbers of GFP_NOFS allocations causing the
3183567b59aSDave Chinner 	 * shrinkers to return -1 all the time. This results in a large
3193567b59aSDave Chinner 	 * nr being built up so when a shrink that can do some work
3203567b59aSDave Chinner 	 * comes along it empties the entire cache due to nr >>>
321d5bc5fd3SVladimir Davydov 	 * freeable. This is bad for sustaining a working set in
3223567b59aSDave Chinner 	 * memory.
3233567b59aSDave Chinner 	 *
3243567b59aSDave Chinner 	 * Hence only allow the shrinker to scan the entire cache when
3253567b59aSDave Chinner 	 * a large delta change is calculated directly.
3263567b59aSDave Chinner 	 */
327d5bc5fd3SVladimir Davydov 	if (delta < freeable / 4)
328d5bc5fd3SVladimir Davydov 		total_scan = min(total_scan, freeable / 2);
3293567b59aSDave Chinner 
3303567b59aSDave Chinner 	/*
331ea164d73SAndrea Arcangeli 	 * Avoid risking looping forever due to too large nr value:
332ea164d73SAndrea Arcangeli 	 * never try to free more than twice the estimate number of
333ea164d73SAndrea Arcangeli 	 * freeable entries.
334ea164d73SAndrea Arcangeli 	 */
335d5bc5fd3SVladimir Davydov 	if (total_scan > freeable * 2)
336d5bc5fd3SVladimir Davydov 		total_scan = freeable * 2;
3371da177e4SLinus Torvalds 
33824f7c6b9SDave Chinner 	trace_mm_shrink_slab_start(shrinker, shrinkctl, nr,
3396b4f7799SJohannes Weiner 				   nr_scanned, nr_eligible,
340d5bc5fd3SVladimir Davydov 				   freeable, delta, total_scan);
34109576073SDave Chinner 
3420b1fb40aSVladimir Davydov 	/*
3430b1fb40aSVladimir Davydov 	 * Normally, we should not scan less than batch_size objects in one
3440b1fb40aSVladimir Davydov 	 * pass to avoid too frequent shrinker calls, but if the slab has less
3450b1fb40aSVladimir Davydov 	 * than batch_size objects in total and we are really tight on memory,
3460b1fb40aSVladimir Davydov 	 * we will try to reclaim all available objects, otherwise we can end
3470b1fb40aSVladimir Davydov 	 * up failing allocations although there are plenty of reclaimable
3480b1fb40aSVladimir Davydov 	 * objects spread over several slabs with usage less than the
3490b1fb40aSVladimir Davydov 	 * batch_size.
3500b1fb40aSVladimir Davydov 	 *
3510b1fb40aSVladimir Davydov 	 * We detect the "tight on memory" situations by looking at the total
3520b1fb40aSVladimir Davydov 	 * number of objects we want to scan (total_scan). If it is greater
353d5bc5fd3SVladimir Davydov 	 * than the total number of objects on slab (freeable), we must be
3540b1fb40aSVladimir Davydov 	 * scanning at high prio and therefore should try to reclaim as much as
3550b1fb40aSVladimir Davydov 	 * possible.
3560b1fb40aSVladimir Davydov 	 */
3570b1fb40aSVladimir Davydov 	while (total_scan >= batch_size ||
358d5bc5fd3SVladimir Davydov 	       total_scan >= freeable) {
35924f7c6b9SDave Chinner 		unsigned long ret;
3600b1fb40aSVladimir Davydov 		unsigned long nr_to_scan = min(batch_size, total_scan);
3611da177e4SLinus Torvalds 
3620b1fb40aSVladimir Davydov 		shrinkctl->nr_to_scan = nr_to_scan;
36324f7c6b9SDave Chinner 		ret = shrinker->scan_objects(shrinker, shrinkctl);
36424f7c6b9SDave Chinner 		if (ret == SHRINK_STOP)
3651da177e4SLinus Torvalds 			break;
36624f7c6b9SDave Chinner 		freed += ret;
36724f7c6b9SDave Chinner 
3680b1fb40aSVladimir Davydov 		count_vm_events(SLABS_SCANNED, nr_to_scan);
3690b1fb40aSVladimir Davydov 		total_scan -= nr_to_scan;
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 		cond_resched();
3721da177e4SLinus Torvalds 	}
3731da177e4SLinus Torvalds 
374acf92b48SDave Chinner 	/*
375acf92b48SDave Chinner 	 * move the unused scan count back into the shrinker in a
376acf92b48SDave Chinner 	 * manner that handles concurrent updates. If we exhausted the
377acf92b48SDave Chinner 	 * scan, there is no need to do an update.
378acf92b48SDave Chinner 	 */
37983aeeadaSKonstantin Khlebnikov 	if (total_scan > 0)
38083aeeadaSKonstantin Khlebnikov 		new_nr = atomic_long_add_return(total_scan,
3811d3d4437SGlauber Costa 						&shrinker->nr_deferred[nid]);
38283aeeadaSKonstantin Khlebnikov 	else
3831d3d4437SGlauber Costa 		new_nr = atomic_long_read(&shrinker->nr_deferred[nid]);
384acf92b48SDave Chinner 
385df9024a8SDave Hansen 	trace_mm_shrink_slab_end(shrinker, nid, freed, nr, new_nr, total_scan);
3861d3d4437SGlauber Costa 	return freed;
3871d3d4437SGlauber Costa }
3881d3d4437SGlauber Costa 
3896b4f7799SJohannes Weiner /**
390cb731d6cSVladimir Davydov  * shrink_slab - shrink slab caches
3916b4f7799SJohannes Weiner  * @gfp_mask: allocation context
3926b4f7799SJohannes Weiner  * @nid: node whose slab caches to target
393cb731d6cSVladimir Davydov  * @memcg: memory cgroup whose slab caches to target
3946b4f7799SJohannes Weiner  * @nr_scanned: pressure numerator
3956b4f7799SJohannes Weiner  * @nr_eligible: pressure denominator
3961d3d4437SGlauber Costa  *
3976b4f7799SJohannes Weiner  * Call the shrink functions to age shrinkable caches.
3981d3d4437SGlauber Costa  *
3996b4f7799SJohannes Weiner  * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set,
4006b4f7799SJohannes Weiner  * unaware shrinkers will receive a node id of 0 instead.
4011d3d4437SGlauber Costa  *
402cb731d6cSVladimir Davydov  * @memcg specifies the memory cgroup to target. If it is not NULL,
403cb731d6cSVladimir Davydov  * only shrinkers with SHRINKER_MEMCG_AWARE set will be called to scan
4040fc9f58aSVladimir Davydov  * objects from the memory cgroup specified. Otherwise, only unaware
4050fc9f58aSVladimir Davydov  * shrinkers are called.
406cb731d6cSVladimir Davydov  *
4076b4f7799SJohannes Weiner  * @nr_scanned and @nr_eligible form a ratio that indicate how much of
4086b4f7799SJohannes Weiner  * the available objects should be scanned.  Page reclaim for example
4096b4f7799SJohannes Weiner  * passes the number of pages scanned and the number of pages on the
4106b4f7799SJohannes Weiner  * LRU lists that it considered on @nid, plus a bias in @nr_scanned
4116b4f7799SJohannes Weiner  * when it encountered mapped pages.  The ratio is further biased by
4126b4f7799SJohannes Weiner  * the ->seeks setting of the shrink function, which indicates the
4136b4f7799SJohannes Weiner  * cost to recreate an object relative to that of an LRU page.
4141d3d4437SGlauber Costa  *
4156b4f7799SJohannes Weiner  * Returns the number of reclaimed slab objects.
4161d3d4437SGlauber Costa  */
417cb731d6cSVladimir Davydov static unsigned long shrink_slab(gfp_t gfp_mask, int nid,
418cb731d6cSVladimir Davydov 				 struct mem_cgroup *memcg,
4196b4f7799SJohannes Weiner 				 unsigned long nr_scanned,
4206b4f7799SJohannes Weiner 				 unsigned long nr_eligible)
4211d3d4437SGlauber Costa {
4221d3d4437SGlauber Costa 	struct shrinker *shrinker;
4231d3d4437SGlauber Costa 	unsigned long freed = 0;
4241d3d4437SGlauber Costa 
4250fc9f58aSVladimir Davydov 	if (memcg && (!memcg_kmem_enabled() || !mem_cgroup_online(memcg)))
426cb731d6cSVladimir Davydov 		return 0;
427cb731d6cSVladimir Davydov 
4286b4f7799SJohannes Weiner 	if (nr_scanned == 0)
4296b4f7799SJohannes Weiner 		nr_scanned = SWAP_CLUSTER_MAX;
4301d3d4437SGlauber Costa 
4311d3d4437SGlauber Costa 	if (!down_read_trylock(&shrinker_rwsem)) {
4321d3d4437SGlauber Costa 		/*
4331d3d4437SGlauber Costa 		 * If we would return 0, our callers would understand that we
4341d3d4437SGlauber Costa 		 * have nothing else to shrink and give up trying. By returning
4351d3d4437SGlauber Costa 		 * 1 we keep it going and assume we'll be able to shrink next
4361d3d4437SGlauber Costa 		 * time.
4371d3d4437SGlauber Costa 		 */
4381d3d4437SGlauber Costa 		freed = 1;
4391d3d4437SGlauber Costa 		goto out;
4401d3d4437SGlauber Costa 	}
4411d3d4437SGlauber Costa 
4421d3d4437SGlauber Costa 	list_for_each_entry(shrinker, &shrinker_list, list) {
4436b4f7799SJohannes Weiner 		struct shrink_control sc = {
4446b4f7799SJohannes Weiner 			.gfp_mask = gfp_mask,
4456b4f7799SJohannes Weiner 			.nid = nid,
446cb731d6cSVladimir Davydov 			.memcg = memcg,
4476b4f7799SJohannes Weiner 		};
4486b4f7799SJohannes Weiner 
4490fc9f58aSVladimir Davydov 		/*
4500fc9f58aSVladimir Davydov 		 * If kernel memory accounting is disabled, we ignore
4510fc9f58aSVladimir Davydov 		 * SHRINKER_MEMCG_AWARE flag and call all shrinkers
4520fc9f58aSVladimir Davydov 		 * passing NULL for memcg.
4530fc9f58aSVladimir Davydov 		 */
4540fc9f58aSVladimir Davydov 		if (memcg_kmem_enabled() &&
4550fc9f58aSVladimir Davydov 		    !!memcg != !!(shrinker->flags & SHRINKER_MEMCG_AWARE))
456cb731d6cSVladimir Davydov 			continue;
457cb731d6cSVladimir Davydov 
4586b4f7799SJohannes Weiner 		if (!(shrinker->flags & SHRINKER_NUMA_AWARE))
4596b4f7799SJohannes Weiner 			sc.nid = 0;
4606b4f7799SJohannes Weiner 
461cb731d6cSVladimir Davydov 		freed += do_shrink_slab(&sc, shrinker, nr_scanned, nr_eligible);
462ec97097bSVladimir Davydov 	}
4631d3d4437SGlauber Costa 
4641da177e4SLinus Torvalds 	up_read(&shrinker_rwsem);
465f06590bdSMinchan Kim out:
466f06590bdSMinchan Kim 	cond_resched();
46724f7c6b9SDave Chinner 	return freed;
4681da177e4SLinus Torvalds }
4691da177e4SLinus Torvalds 
470cb731d6cSVladimir Davydov void drop_slab_node(int nid)
471cb731d6cSVladimir Davydov {
472cb731d6cSVladimir Davydov 	unsigned long freed;
473cb731d6cSVladimir Davydov 
474cb731d6cSVladimir Davydov 	do {
475cb731d6cSVladimir Davydov 		struct mem_cgroup *memcg = NULL;
476cb731d6cSVladimir Davydov 
477cb731d6cSVladimir Davydov 		freed = 0;
478cb731d6cSVladimir Davydov 		do {
479cb731d6cSVladimir Davydov 			freed += shrink_slab(GFP_KERNEL, nid, memcg,
480cb731d6cSVladimir Davydov 					     1000, 1000);
481cb731d6cSVladimir Davydov 		} while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL);
482cb731d6cSVladimir Davydov 	} while (freed > 10);
483cb731d6cSVladimir Davydov }
484cb731d6cSVladimir Davydov 
485cb731d6cSVladimir Davydov void drop_slab(void)
486cb731d6cSVladimir Davydov {
487cb731d6cSVladimir Davydov 	int nid;
488cb731d6cSVladimir Davydov 
489cb731d6cSVladimir Davydov 	for_each_online_node(nid)
490cb731d6cSVladimir Davydov 		drop_slab_node(nid);
491cb731d6cSVladimir Davydov }
492cb731d6cSVladimir Davydov 
4931da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page)
4941da177e4SLinus Torvalds {
495ceddc3a5SJohannes Weiner 	/*
496ceddc3a5SJohannes Weiner 	 * A freeable page cache page is referenced only by the caller
497ceddc3a5SJohannes Weiner 	 * that isolated the page, the page cache radix tree and
498ceddc3a5SJohannes Weiner 	 * optional buffer heads at page->private.
499ceddc3a5SJohannes Weiner 	 */
500edcf4748SJohannes Weiner 	return page_count(page) - page_has_private(page) == 2;
5011da177e4SLinus Torvalds }
5021da177e4SLinus Torvalds 
503703c2708STejun Heo static int may_write_to_inode(struct inode *inode, struct scan_control *sc)
5041da177e4SLinus Torvalds {
505930d9152SChristoph Lameter 	if (current->flags & PF_SWAPWRITE)
5061da177e4SLinus Torvalds 		return 1;
507703c2708STejun Heo 	if (!inode_write_congested(inode))
5081da177e4SLinus Torvalds 		return 1;
509703c2708STejun Heo 	if (inode_to_bdi(inode) == current->backing_dev_info)
5101da177e4SLinus Torvalds 		return 1;
5111da177e4SLinus Torvalds 	return 0;
5121da177e4SLinus Torvalds }
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds /*
5151da177e4SLinus Torvalds  * We detected a synchronous write error writing a page out.  Probably
5161da177e4SLinus Torvalds  * -ENOSPC.  We need to propagate that into the address_space for a subsequent
5171da177e4SLinus Torvalds  * fsync(), msync() or close().
5181da177e4SLinus Torvalds  *
5191da177e4SLinus Torvalds  * The tricky part is that after writepage we cannot touch the mapping: nothing
5201da177e4SLinus Torvalds  * prevents it from being freed up.  But we have a ref on the page and once
5211da177e4SLinus Torvalds  * that page is locked, the mapping is pinned.
5221da177e4SLinus Torvalds  *
5231da177e4SLinus Torvalds  * We're allowed to run sleeping lock_page() here because we know the caller has
5241da177e4SLinus Torvalds  * __GFP_FS.
5251da177e4SLinus Torvalds  */
5261da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping,
5271da177e4SLinus Torvalds 				struct page *page, int error)
5281da177e4SLinus Torvalds {
5297eaceaccSJens Axboe 	lock_page(page);
5303e9f45bdSGuillaume Chazarain 	if (page_mapping(page) == mapping)
5313e9f45bdSGuillaume Chazarain 		mapping_set_error(mapping, error);
5321da177e4SLinus Torvalds 	unlock_page(page);
5331da177e4SLinus Torvalds }
5341da177e4SLinus Torvalds 
53504e62a29SChristoph Lameter /* possible outcome of pageout() */
53604e62a29SChristoph Lameter typedef enum {
53704e62a29SChristoph Lameter 	/* failed to write page out, page is locked */
53804e62a29SChristoph Lameter 	PAGE_KEEP,
53904e62a29SChristoph Lameter 	/* move page to the active list, page is locked */
54004e62a29SChristoph Lameter 	PAGE_ACTIVATE,
54104e62a29SChristoph Lameter 	/* page has been sent to the disk successfully, page is unlocked */
54204e62a29SChristoph Lameter 	PAGE_SUCCESS,
54304e62a29SChristoph Lameter 	/* page is clean and locked */
54404e62a29SChristoph Lameter 	PAGE_CLEAN,
54504e62a29SChristoph Lameter } pageout_t;
54604e62a29SChristoph Lameter 
5471da177e4SLinus Torvalds /*
5481742f19fSAndrew Morton  * pageout is called by shrink_page_list() for each dirty page.
5491742f19fSAndrew Morton  * Calls ->writepage().
5501da177e4SLinus Torvalds  */
551c661b078SAndy Whitcroft static pageout_t pageout(struct page *page, struct address_space *mapping,
5527d3579e8SKOSAKI Motohiro 			 struct scan_control *sc)
5531da177e4SLinus Torvalds {
5541da177e4SLinus Torvalds 	/*
5551da177e4SLinus Torvalds 	 * If the page is dirty, only perform writeback if that write
5561da177e4SLinus Torvalds 	 * will be non-blocking.  To prevent this allocation from being
5571da177e4SLinus Torvalds 	 * stalled by pagecache activity.  But note that there may be
5581da177e4SLinus Torvalds 	 * stalls if we need to run get_block().  We could test
5591da177e4SLinus Torvalds 	 * PagePrivate for that.
5601da177e4SLinus Torvalds 	 *
5618174202bSAl Viro 	 * If this process is currently in __generic_file_write_iter() against
5621da177e4SLinus Torvalds 	 * this page's queue, we can perform writeback even if that
5631da177e4SLinus Torvalds 	 * will block.
5641da177e4SLinus Torvalds 	 *
5651da177e4SLinus Torvalds 	 * If the page is swapcache, write it back even if that would
5661da177e4SLinus Torvalds 	 * block, for some throttling. This happens by accident, because
5671da177e4SLinus Torvalds 	 * swap_backing_dev_info is bust: it doesn't reflect the
5681da177e4SLinus Torvalds 	 * congestion state of the swapdevs.  Easy to fix, if needed.
5691da177e4SLinus Torvalds 	 */
5701da177e4SLinus Torvalds 	if (!is_page_cache_freeable(page))
5711da177e4SLinus Torvalds 		return PAGE_KEEP;
5721da177e4SLinus Torvalds 	if (!mapping) {
5731da177e4SLinus Torvalds 		/*
5741da177e4SLinus Torvalds 		 * Some data journaling orphaned pages can have
5751da177e4SLinus Torvalds 		 * page->mapping == NULL while being dirty with clean buffers.
5761da177e4SLinus Torvalds 		 */
577266cf658SDavid Howells 		if (page_has_private(page)) {
5781da177e4SLinus Torvalds 			if (try_to_free_buffers(page)) {
5791da177e4SLinus Torvalds 				ClearPageDirty(page);
580b1de0d13SMitchel Humpherys 				pr_info("%s: orphaned page\n", __func__);
5811da177e4SLinus Torvalds 				return PAGE_CLEAN;
5821da177e4SLinus Torvalds 			}
5831da177e4SLinus Torvalds 		}
5841da177e4SLinus Torvalds 		return PAGE_KEEP;
5851da177e4SLinus Torvalds 	}
5861da177e4SLinus Torvalds 	if (mapping->a_ops->writepage == NULL)
5871da177e4SLinus Torvalds 		return PAGE_ACTIVATE;
588703c2708STejun Heo 	if (!may_write_to_inode(mapping->host, sc))
5891da177e4SLinus Torvalds 		return PAGE_KEEP;
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	if (clear_page_dirty_for_io(page)) {
5921da177e4SLinus Torvalds 		int res;
5931da177e4SLinus Torvalds 		struct writeback_control wbc = {
5941da177e4SLinus Torvalds 			.sync_mode = WB_SYNC_NONE,
5951da177e4SLinus Torvalds 			.nr_to_write = SWAP_CLUSTER_MAX,
596111ebb6eSOGAWA Hirofumi 			.range_start = 0,
597111ebb6eSOGAWA Hirofumi 			.range_end = LLONG_MAX,
5981da177e4SLinus Torvalds 			.for_reclaim = 1,
5991da177e4SLinus Torvalds 		};
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 		SetPageReclaim(page);
6021da177e4SLinus Torvalds 		res = mapping->a_ops->writepage(page, &wbc);
6031da177e4SLinus Torvalds 		if (res < 0)
6041da177e4SLinus Torvalds 			handle_write_error(mapping, page, res);
605994fc28cSZach Brown 		if (res == AOP_WRITEPAGE_ACTIVATE) {
6061da177e4SLinus Torvalds 			ClearPageReclaim(page);
6071da177e4SLinus Torvalds 			return PAGE_ACTIVATE;
6081da177e4SLinus Torvalds 		}
609c661b078SAndy Whitcroft 
6101da177e4SLinus Torvalds 		if (!PageWriteback(page)) {
6111da177e4SLinus Torvalds 			/* synchronous write or broken a_ops? */
6121da177e4SLinus Torvalds 			ClearPageReclaim(page);
6131da177e4SLinus Torvalds 		}
6143aa23851Syalin wang 		trace_mm_vmscan_writepage(page);
615c4a25635SMel Gorman 		inc_node_page_state(page, NR_VMSCAN_WRITE);
6161da177e4SLinus Torvalds 		return PAGE_SUCCESS;
6171da177e4SLinus Torvalds 	}
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	return PAGE_CLEAN;
6201da177e4SLinus Torvalds }
6211da177e4SLinus Torvalds 
622a649fd92SAndrew Morton /*
623e286781dSNick Piggin  * Same as remove_mapping, but if the page is removed from the mapping, it
624e286781dSNick Piggin  * gets returned with a refcount of 0.
625a649fd92SAndrew Morton  */
626a528910eSJohannes Weiner static int __remove_mapping(struct address_space *mapping, struct page *page,
627a528910eSJohannes Weiner 			    bool reclaimed)
62849d2e9ccSChristoph Lameter {
629c4843a75SGreg Thelen 	unsigned long flags;
630c4843a75SGreg Thelen 
63128e4d965SNick Piggin 	BUG_ON(!PageLocked(page));
63228e4d965SNick Piggin 	BUG_ON(mapping != page_mapping(page));
63349d2e9ccSChristoph Lameter 
634c4843a75SGreg Thelen 	spin_lock_irqsave(&mapping->tree_lock, flags);
63549d2e9ccSChristoph Lameter 	/*
6360fd0e6b0SNick Piggin 	 * The non racy check for a busy page.
6370fd0e6b0SNick Piggin 	 *
6380fd0e6b0SNick Piggin 	 * Must be careful with the order of the tests. When someone has
6390fd0e6b0SNick Piggin 	 * a ref to the page, it may be possible that they dirty it then
6400fd0e6b0SNick Piggin 	 * drop the reference. So if PageDirty is tested before page_count
6410fd0e6b0SNick Piggin 	 * here, then the following race may occur:
6420fd0e6b0SNick Piggin 	 *
6430fd0e6b0SNick Piggin 	 * get_user_pages(&page);
6440fd0e6b0SNick Piggin 	 * [user mapping goes away]
6450fd0e6b0SNick Piggin 	 * write_to(page);
6460fd0e6b0SNick Piggin 	 *				!PageDirty(page)    [good]
6470fd0e6b0SNick Piggin 	 * SetPageDirty(page);
6480fd0e6b0SNick Piggin 	 * put_page(page);
6490fd0e6b0SNick Piggin 	 *				!page_count(page)   [good, discard it]
6500fd0e6b0SNick Piggin 	 *
6510fd0e6b0SNick Piggin 	 * [oops, our write_to data is lost]
6520fd0e6b0SNick Piggin 	 *
6530fd0e6b0SNick Piggin 	 * Reversing the order of the tests ensures such a situation cannot
6540fd0e6b0SNick Piggin 	 * escape unnoticed. The smp_rmb is needed to ensure the page->flags
6550139aa7bSJoonsoo Kim 	 * load is not satisfied before that of page->_refcount.
6560fd0e6b0SNick Piggin 	 *
6570fd0e6b0SNick Piggin 	 * Note that if SetPageDirty is always performed via set_page_dirty,
6580fd0e6b0SNick Piggin 	 * and thus under tree_lock, then this ordering is not required.
65949d2e9ccSChristoph Lameter 	 */
660fe896d18SJoonsoo Kim 	if (!page_ref_freeze(page, 2))
66149d2e9ccSChristoph Lameter 		goto cannot_free;
662e286781dSNick Piggin 	/* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */
663e286781dSNick Piggin 	if (unlikely(PageDirty(page))) {
664fe896d18SJoonsoo Kim 		page_ref_unfreeze(page, 2);
66549d2e9ccSChristoph Lameter 		goto cannot_free;
666e286781dSNick Piggin 	}
66749d2e9ccSChristoph Lameter 
66849d2e9ccSChristoph Lameter 	if (PageSwapCache(page)) {
66949d2e9ccSChristoph Lameter 		swp_entry_t swap = { .val = page_private(page) };
6700a31bc97SJohannes Weiner 		mem_cgroup_swapout(page, swap);
67149d2e9ccSChristoph Lameter 		__delete_from_swap_cache(page);
672c4843a75SGreg Thelen 		spin_unlock_irqrestore(&mapping->tree_lock, flags);
6730a31bc97SJohannes Weiner 		swapcache_free(swap);
674e286781dSNick Piggin 	} else {
6756072d13cSLinus Torvalds 		void (*freepage)(struct page *);
676a528910eSJohannes Weiner 		void *shadow = NULL;
6776072d13cSLinus Torvalds 
6786072d13cSLinus Torvalds 		freepage = mapping->a_ops->freepage;
679a528910eSJohannes Weiner 		/*
680a528910eSJohannes Weiner 		 * Remember a shadow entry for reclaimed file cache in
681a528910eSJohannes Weiner 		 * order to detect refaults, thus thrashing, later on.
682a528910eSJohannes Weiner 		 *
683a528910eSJohannes Weiner 		 * But don't store shadows in an address space that is
684a528910eSJohannes Weiner 		 * already exiting.  This is not just an optizimation,
685a528910eSJohannes Weiner 		 * inode reclaim needs to empty out the radix tree or
686a528910eSJohannes Weiner 		 * the nodes are lost.  Don't plant shadows behind its
687a528910eSJohannes Weiner 		 * back.
688f9fe48beSRoss Zwisler 		 *
689f9fe48beSRoss Zwisler 		 * We also don't store shadows for DAX mappings because the
690f9fe48beSRoss Zwisler 		 * only page cache pages found in these are zero pages
691f9fe48beSRoss Zwisler 		 * covering holes, and because we don't want to mix DAX
692f9fe48beSRoss Zwisler 		 * exceptional entries and shadow exceptional entries in the
693f9fe48beSRoss Zwisler 		 * same page_tree.
694a528910eSJohannes Weiner 		 */
695a528910eSJohannes Weiner 		if (reclaimed && page_is_file_cache(page) &&
696f9fe48beSRoss Zwisler 		    !mapping_exiting(mapping) && !dax_mapping(mapping))
697a528910eSJohannes Weiner 			shadow = workingset_eviction(mapping, page);
69862cccb8cSJohannes Weiner 		__delete_from_page_cache(page, shadow);
699c4843a75SGreg Thelen 		spin_unlock_irqrestore(&mapping->tree_lock, flags);
7006072d13cSLinus Torvalds 
7016072d13cSLinus Torvalds 		if (freepage != NULL)
7026072d13cSLinus Torvalds 			freepage(page);
703e286781dSNick Piggin 	}
704e286781dSNick Piggin 
70549d2e9ccSChristoph Lameter 	return 1;
70649d2e9ccSChristoph Lameter 
70749d2e9ccSChristoph Lameter cannot_free:
708c4843a75SGreg Thelen 	spin_unlock_irqrestore(&mapping->tree_lock, flags);
70949d2e9ccSChristoph Lameter 	return 0;
71049d2e9ccSChristoph Lameter }
71149d2e9ccSChristoph Lameter 
7121da177e4SLinus Torvalds /*
713e286781dSNick Piggin  * Attempt to detach a locked page from its ->mapping.  If it is dirty or if
714e286781dSNick Piggin  * someone else has a ref on the page, abort and return 0.  If it was
715e286781dSNick Piggin  * successfully detached, return 1.  Assumes the caller has a single ref on
716e286781dSNick Piggin  * this page.
717e286781dSNick Piggin  */
718e286781dSNick Piggin int remove_mapping(struct address_space *mapping, struct page *page)
719e286781dSNick Piggin {
720a528910eSJohannes Weiner 	if (__remove_mapping(mapping, page, false)) {
721e286781dSNick Piggin 		/*
722e286781dSNick Piggin 		 * Unfreezing the refcount with 1 rather than 2 effectively
723e286781dSNick Piggin 		 * drops the pagecache ref for us without requiring another
724e286781dSNick Piggin 		 * atomic operation.
725e286781dSNick Piggin 		 */
726fe896d18SJoonsoo Kim 		page_ref_unfreeze(page, 1);
727e286781dSNick Piggin 		return 1;
728e286781dSNick Piggin 	}
729e286781dSNick Piggin 	return 0;
730e286781dSNick Piggin }
731e286781dSNick Piggin 
732894bc310SLee Schermerhorn /**
733894bc310SLee Schermerhorn  * putback_lru_page - put previously isolated page onto appropriate LRU list
734894bc310SLee Schermerhorn  * @page: page to be put back to appropriate lru list
735894bc310SLee Schermerhorn  *
736894bc310SLee Schermerhorn  * Add previously isolated @page to appropriate LRU list.
737894bc310SLee Schermerhorn  * Page may still be unevictable for other reasons.
738894bc310SLee Schermerhorn  *
739894bc310SLee Schermerhorn  * lru_lock must not be held, interrupts must be enabled.
740894bc310SLee Schermerhorn  */
741894bc310SLee Schermerhorn void putback_lru_page(struct page *page)
742894bc310SLee Schermerhorn {
7430ec3b74cSVlastimil Babka 	bool is_unevictable;
744bbfd28eeSLee Schermerhorn 	int was_unevictable = PageUnevictable(page);
745894bc310SLee Schermerhorn 
746309381feSSasha Levin 	VM_BUG_ON_PAGE(PageLRU(page), page);
747894bc310SLee Schermerhorn 
748894bc310SLee Schermerhorn redo:
749894bc310SLee Schermerhorn 	ClearPageUnevictable(page);
750894bc310SLee Schermerhorn 
75139b5f29aSHugh Dickins 	if (page_evictable(page)) {
752894bc310SLee Schermerhorn 		/*
753894bc310SLee Schermerhorn 		 * For evictable pages, we can use the cache.
754894bc310SLee Schermerhorn 		 * In event of a race, worst case is we end up with an
755894bc310SLee Schermerhorn 		 * unevictable page on [in]active list.
756894bc310SLee Schermerhorn 		 * We know how to handle that.
757894bc310SLee Schermerhorn 		 */
7580ec3b74cSVlastimil Babka 		is_unevictable = false;
759c53954a0SMel Gorman 		lru_cache_add(page);
760894bc310SLee Schermerhorn 	} else {
761894bc310SLee Schermerhorn 		/*
762894bc310SLee Schermerhorn 		 * Put unevictable pages directly on zone's unevictable
763894bc310SLee Schermerhorn 		 * list.
764894bc310SLee Schermerhorn 		 */
7650ec3b74cSVlastimil Babka 		is_unevictable = true;
766894bc310SLee Schermerhorn 		add_page_to_unevictable_list(page);
7676a7b9548SJohannes Weiner 		/*
76821ee9f39SMinchan Kim 		 * When racing with an mlock or AS_UNEVICTABLE clearing
76921ee9f39SMinchan Kim 		 * (page is unlocked) make sure that if the other thread
77021ee9f39SMinchan Kim 		 * does not observe our setting of PG_lru and fails
77124513264SHugh Dickins 		 * isolation/check_move_unevictable_pages,
77221ee9f39SMinchan Kim 		 * we see PG_mlocked/AS_UNEVICTABLE cleared below and move
7736a7b9548SJohannes Weiner 		 * the page back to the evictable list.
7746a7b9548SJohannes Weiner 		 *
77521ee9f39SMinchan Kim 		 * The other side is TestClearPageMlocked() or shmem_lock().
7766a7b9548SJohannes Weiner 		 */
7776a7b9548SJohannes Weiner 		smp_mb();
778894bc310SLee Schermerhorn 	}
779894bc310SLee Schermerhorn 
780894bc310SLee Schermerhorn 	/*
781894bc310SLee Schermerhorn 	 * page's status can change while we move it among lru. If an evictable
782894bc310SLee Schermerhorn 	 * page is on unevictable list, it never be freed. To avoid that,
783894bc310SLee Schermerhorn 	 * check after we added it to the list, again.
784894bc310SLee Schermerhorn 	 */
7850ec3b74cSVlastimil Babka 	if (is_unevictable && page_evictable(page)) {
786894bc310SLee Schermerhorn 		if (!isolate_lru_page(page)) {
787894bc310SLee Schermerhorn 			put_page(page);
788894bc310SLee Schermerhorn 			goto redo;
789894bc310SLee Schermerhorn 		}
790894bc310SLee Schermerhorn 		/* This means someone else dropped this page from LRU
791894bc310SLee Schermerhorn 		 * So, it will be freed or putback to LRU again. There is
792894bc310SLee Schermerhorn 		 * nothing to do here.
793894bc310SLee Schermerhorn 		 */
794894bc310SLee Schermerhorn 	}
795894bc310SLee Schermerhorn 
7960ec3b74cSVlastimil Babka 	if (was_unevictable && !is_unevictable)
797bbfd28eeSLee Schermerhorn 		count_vm_event(UNEVICTABLE_PGRESCUED);
7980ec3b74cSVlastimil Babka 	else if (!was_unevictable && is_unevictable)
799bbfd28eeSLee Schermerhorn 		count_vm_event(UNEVICTABLE_PGCULLED);
800bbfd28eeSLee Schermerhorn 
801894bc310SLee Schermerhorn 	put_page(page);		/* drop ref from isolate */
802894bc310SLee Schermerhorn }
803894bc310SLee Schermerhorn 
804dfc8d636SJohannes Weiner enum page_references {
805dfc8d636SJohannes Weiner 	PAGEREF_RECLAIM,
806dfc8d636SJohannes Weiner 	PAGEREF_RECLAIM_CLEAN,
80764574746SJohannes Weiner 	PAGEREF_KEEP,
808dfc8d636SJohannes Weiner 	PAGEREF_ACTIVATE,
809dfc8d636SJohannes Weiner };
810dfc8d636SJohannes Weiner 
811dfc8d636SJohannes Weiner static enum page_references page_check_references(struct page *page,
812dfc8d636SJohannes Weiner 						  struct scan_control *sc)
813dfc8d636SJohannes Weiner {
81464574746SJohannes Weiner 	int referenced_ptes, referenced_page;
815dfc8d636SJohannes Weiner 	unsigned long vm_flags;
816dfc8d636SJohannes Weiner 
817c3ac9a8aSJohannes Weiner 	referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup,
818c3ac9a8aSJohannes Weiner 					  &vm_flags);
81964574746SJohannes Weiner 	referenced_page = TestClearPageReferenced(page);
820dfc8d636SJohannes Weiner 
821dfc8d636SJohannes Weiner 	/*
822dfc8d636SJohannes Weiner 	 * Mlock lost the isolation race with us.  Let try_to_unmap()
823dfc8d636SJohannes Weiner 	 * move the page to the unevictable list.
824dfc8d636SJohannes Weiner 	 */
825dfc8d636SJohannes Weiner 	if (vm_flags & VM_LOCKED)
826dfc8d636SJohannes Weiner 		return PAGEREF_RECLAIM;
827dfc8d636SJohannes Weiner 
82864574746SJohannes Weiner 	if (referenced_ptes) {
829e4898273SMichal Hocko 		if (PageSwapBacked(page))
83064574746SJohannes Weiner 			return PAGEREF_ACTIVATE;
83164574746SJohannes Weiner 		/*
83264574746SJohannes Weiner 		 * All mapped pages start out with page table
83364574746SJohannes Weiner 		 * references from the instantiating fault, so we need
83464574746SJohannes Weiner 		 * to look twice if a mapped file page is used more
83564574746SJohannes Weiner 		 * than once.
83664574746SJohannes Weiner 		 *
83764574746SJohannes Weiner 		 * Mark it and spare it for another trip around the
83864574746SJohannes Weiner 		 * inactive list.  Another page table reference will
83964574746SJohannes Weiner 		 * lead to its activation.
84064574746SJohannes Weiner 		 *
84164574746SJohannes Weiner 		 * Note: the mark is set for activated pages as well
84264574746SJohannes Weiner 		 * so that recently deactivated but used pages are
84364574746SJohannes Weiner 		 * quickly recovered.
84464574746SJohannes Weiner 		 */
84564574746SJohannes Weiner 		SetPageReferenced(page);
84664574746SJohannes Weiner 
84734dbc67aSKonstantin Khlebnikov 		if (referenced_page || referenced_ptes > 1)
848dfc8d636SJohannes Weiner 			return PAGEREF_ACTIVATE;
849dfc8d636SJohannes Weiner 
850c909e993SKonstantin Khlebnikov 		/*
851c909e993SKonstantin Khlebnikov 		 * Activate file-backed executable pages after first usage.
852c909e993SKonstantin Khlebnikov 		 */
853c909e993SKonstantin Khlebnikov 		if (vm_flags & VM_EXEC)
854c909e993SKonstantin Khlebnikov 			return PAGEREF_ACTIVATE;
855c909e993SKonstantin Khlebnikov 
85664574746SJohannes Weiner 		return PAGEREF_KEEP;
85764574746SJohannes Weiner 	}
85864574746SJohannes Weiner 
859dfc8d636SJohannes Weiner 	/* Reclaim if clean, defer dirty pages to writeback */
8602e30244aSKOSAKI Motohiro 	if (referenced_page && !PageSwapBacked(page))
861dfc8d636SJohannes Weiner 		return PAGEREF_RECLAIM_CLEAN;
86264574746SJohannes Weiner 
86364574746SJohannes Weiner 	return PAGEREF_RECLAIM;
864dfc8d636SJohannes Weiner }
865dfc8d636SJohannes Weiner 
866e2be15f6SMel Gorman /* Check if a page is dirty or under writeback */
867e2be15f6SMel Gorman static void page_check_dirty_writeback(struct page *page,
868e2be15f6SMel Gorman 				       bool *dirty, bool *writeback)
869e2be15f6SMel Gorman {
870b4597226SMel Gorman 	struct address_space *mapping;
871b4597226SMel Gorman 
872e2be15f6SMel Gorman 	/*
873e2be15f6SMel Gorman 	 * Anonymous pages are not handled by flushers and must be written
874e2be15f6SMel Gorman 	 * from reclaim context. Do not stall reclaim based on them
875e2be15f6SMel Gorman 	 */
876e2be15f6SMel Gorman 	if (!page_is_file_cache(page)) {
877e2be15f6SMel Gorman 		*dirty = false;
878e2be15f6SMel Gorman 		*writeback = false;
879e2be15f6SMel Gorman 		return;
880e2be15f6SMel Gorman 	}
881e2be15f6SMel Gorman 
882e2be15f6SMel Gorman 	/* By default assume that the page flags are accurate */
883e2be15f6SMel Gorman 	*dirty = PageDirty(page);
884e2be15f6SMel Gorman 	*writeback = PageWriteback(page);
885b4597226SMel Gorman 
886b4597226SMel Gorman 	/* Verify dirty/writeback state if the filesystem supports it */
887b4597226SMel Gorman 	if (!page_has_private(page))
888b4597226SMel Gorman 		return;
889b4597226SMel Gorman 
890b4597226SMel Gorman 	mapping = page_mapping(page);
891b4597226SMel Gorman 	if (mapping && mapping->a_ops->is_dirty_writeback)
892b4597226SMel Gorman 		mapping->a_ops->is_dirty_writeback(page, dirty, writeback);
893e2be15f6SMel Gorman }
894e2be15f6SMel Gorman 
895e286781dSNick Piggin /*
8961742f19fSAndrew Morton  * shrink_page_list() returns the number of reclaimed pages
8971da177e4SLinus Torvalds  */
8981742f19fSAndrew Morton static unsigned long shrink_page_list(struct list_head *page_list,
899599d0c95SMel Gorman 				      struct pglist_data *pgdat,
900f84f6e2bSMel Gorman 				      struct scan_control *sc,
90102c6de8dSMinchan Kim 				      enum ttu_flags ttu_flags,
9028e950282SMel Gorman 				      unsigned long *ret_nr_dirty,
903d43006d5SMel Gorman 				      unsigned long *ret_nr_unqueued_dirty,
9048e950282SMel Gorman 				      unsigned long *ret_nr_congested,
90502c6de8dSMinchan Kim 				      unsigned long *ret_nr_writeback,
906b1a6f21eSMel Gorman 				      unsigned long *ret_nr_immediate,
90702c6de8dSMinchan Kim 				      bool force_reclaim)
9081da177e4SLinus Torvalds {
9091da177e4SLinus Torvalds 	LIST_HEAD(ret_pages);
910abe4c3b5SMel Gorman 	LIST_HEAD(free_pages);
9111da177e4SLinus Torvalds 	int pgactivate = 0;
912d43006d5SMel Gorman 	unsigned long nr_unqueued_dirty = 0;
9130e093d99SMel Gorman 	unsigned long nr_dirty = 0;
9140e093d99SMel Gorman 	unsigned long nr_congested = 0;
91505ff5137SAndrew Morton 	unsigned long nr_reclaimed = 0;
91692df3a72SMel Gorman 	unsigned long nr_writeback = 0;
917b1a6f21eSMel Gorman 	unsigned long nr_immediate = 0;
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds 	cond_resched();
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds 	while (!list_empty(page_list)) {
9221da177e4SLinus Torvalds 		struct address_space *mapping;
9231da177e4SLinus Torvalds 		struct page *page;
9241da177e4SLinus Torvalds 		int may_enter_fs;
92502c6de8dSMinchan Kim 		enum page_references references = PAGEREF_RECLAIM_CLEAN;
926e2be15f6SMel Gorman 		bool dirty, writeback;
927854e9ed0SMinchan Kim 		bool lazyfree = false;
928854e9ed0SMinchan Kim 		int ret = SWAP_SUCCESS;
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 		cond_resched();
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 		page = lru_to_page(page_list);
9331da177e4SLinus Torvalds 		list_del(&page->lru);
9341da177e4SLinus Torvalds 
935529ae9aaSNick Piggin 		if (!trylock_page(page))
9361da177e4SLinus Torvalds 			goto keep;
9371da177e4SLinus Torvalds 
938309381feSSasha Levin 		VM_BUG_ON_PAGE(PageActive(page), page);
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds 		sc->nr_scanned++;
94180e43426SChristoph Lameter 
94239b5f29aSHugh Dickins 		if (unlikely(!page_evictable(page)))
943b291f000SNick Piggin 			goto cull_mlocked;
944894bc310SLee Schermerhorn 
945a6dc60f8SJohannes Weiner 		if (!sc->may_unmap && page_mapped(page))
94680e43426SChristoph Lameter 			goto keep_locked;
94780e43426SChristoph Lameter 
9481da177e4SLinus Torvalds 		/* Double the slab pressure for mapped and swapcache pages */
9491da177e4SLinus Torvalds 		if (page_mapped(page) || PageSwapCache(page))
9501da177e4SLinus Torvalds 			sc->nr_scanned++;
9511da177e4SLinus Torvalds 
952c661b078SAndy Whitcroft 		may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
953c661b078SAndy Whitcroft 			(PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
954c661b078SAndy Whitcroft 
955e62e384eSMichal Hocko 		/*
956e2be15f6SMel Gorman 		 * The number of dirty pages determines if a zone is marked
957e2be15f6SMel Gorman 		 * reclaim_congested which affects wait_iff_congested. kswapd
958e2be15f6SMel Gorman 		 * will stall and start writing pages if the tail of the LRU
959e2be15f6SMel Gorman 		 * is all dirty unqueued pages.
960e2be15f6SMel Gorman 		 */
961e2be15f6SMel Gorman 		page_check_dirty_writeback(page, &dirty, &writeback);
962e2be15f6SMel Gorman 		if (dirty || writeback)
963e2be15f6SMel Gorman 			nr_dirty++;
964e2be15f6SMel Gorman 
965e2be15f6SMel Gorman 		if (dirty && !writeback)
966e2be15f6SMel Gorman 			nr_unqueued_dirty++;
967e2be15f6SMel Gorman 
968d04e8acdSMel Gorman 		/*
969d04e8acdSMel Gorman 		 * Treat this page as congested if the underlying BDI is or if
970d04e8acdSMel Gorman 		 * pages are cycling through the LRU so quickly that the
971d04e8acdSMel Gorman 		 * pages marked for immediate reclaim are making it to the
972d04e8acdSMel Gorman 		 * end of the LRU a second time.
973d04e8acdSMel Gorman 		 */
974e2be15f6SMel Gorman 		mapping = page_mapping(page);
9751da58ee2SJamie Liu 		if (((dirty || writeback) && mapping &&
976703c2708STejun Heo 		     inode_write_congested(mapping->host)) ||
977d04e8acdSMel Gorman 		    (writeback && PageReclaim(page)))
978e2be15f6SMel Gorman 			nr_congested++;
979e2be15f6SMel Gorman 
980e2be15f6SMel Gorman 		/*
981283aba9fSMel Gorman 		 * If a page at the tail of the LRU is under writeback, there
982283aba9fSMel Gorman 		 * are three cases to consider.
983e62e384eSMichal Hocko 		 *
984283aba9fSMel Gorman 		 * 1) If reclaim is encountering an excessive number of pages
985283aba9fSMel Gorman 		 *    under writeback and this page is both under writeback and
986283aba9fSMel Gorman 		 *    PageReclaim then it indicates that pages are being queued
987283aba9fSMel Gorman 		 *    for IO but are being recycled through the LRU before the
988283aba9fSMel Gorman 		 *    IO can complete. Waiting on the page itself risks an
989283aba9fSMel Gorman 		 *    indefinite stall if it is impossible to writeback the
990283aba9fSMel Gorman 		 *    page due to IO error or disconnected storage so instead
991b1a6f21eSMel Gorman 		 *    note that the LRU is being scanned too quickly and the
992b1a6f21eSMel Gorman 		 *    caller can stall after page list has been processed.
993c3b94f44SHugh Dickins 		 *
99497c9341fSTejun Heo 		 * 2) Global or new memcg reclaim encounters a page that is
995ecf5fc6eSMichal Hocko 		 *    not marked for immediate reclaim, or the caller does not
996ecf5fc6eSMichal Hocko 		 *    have __GFP_FS (or __GFP_IO if it's simply going to swap,
997ecf5fc6eSMichal Hocko 		 *    not to fs). In this case mark the page for immediate
99897c9341fSTejun Heo 		 *    reclaim and continue scanning.
999283aba9fSMel Gorman 		 *
1000ecf5fc6eSMichal Hocko 		 *    Require may_enter_fs because we would wait on fs, which
1001ecf5fc6eSMichal Hocko 		 *    may not have submitted IO yet. And the loop driver might
1002283aba9fSMel Gorman 		 *    enter reclaim, and deadlock if it waits on a page for
1003283aba9fSMel Gorman 		 *    which it is needed to do the write (loop masks off
1004283aba9fSMel Gorman 		 *    __GFP_IO|__GFP_FS for this reason); but more thought
1005283aba9fSMel Gorman 		 *    would probably show more reasons.
1006283aba9fSMel Gorman 		 *
10077fadc820SHugh Dickins 		 * 3) Legacy memcg encounters a page that is already marked
1008283aba9fSMel Gorman 		 *    PageReclaim. memcg does not have any dirty pages
1009283aba9fSMel Gorman 		 *    throttling so we could easily OOM just because too many
1010283aba9fSMel Gorman 		 *    pages are in writeback and there is nothing else to
1011283aba9fSMel Gorman 		 *    reclaim. Wait for the writeback to complete.
1012e62e384eSMichal Hocko 		 */
1013283aba9fSMel Gorman 		if (PageWriteback(page)) {
1014283aba9fSMel Gorman 			/* Case 1 above */
1015283aba9fSMel Gorman 			if (current_is_kswapd() &&
1016283aba9fSMel Gorman 			    PageReclaim(page) &&
1017599d0c95SMel Gorman 			    test_bit(PGDAT_WRITEBACK, &pgdat->flags)) {
1018b1a6f21eSMel Gorman 				nr_immediate++;
1019b1a6f21eSMel Gorman 				goto keep_locked;
1020283aba9fSMel Gorman 
1021283aba9fSMel Gorman 			/* Case 2 above */
102297c9341fSTejun Heo 			} else if (sane_reclaim(sc) ||
1023ecf5fc6eSMichal Hocko 			    !PageReclaim(page) || !may_enter_fs) {
1024c3b94f44SHugh Dickins 				/*
1025c3b94f44SHugh Dickins 				 * This is slightly racy - end_page_writeback()
1026c3b94f44SHugh Dickins 				 * might have just cleared PageReclaim, then
1027c3b94f44SHugh Dickins 				 * setting PageReclaim here end up interpreted
1028c3b94f44SHugh Dickins 				 * as PageReadahead - but that does not matter
1029c3b94f44SHugh Dickins 				 * enough to care.  What we do want is for this
1030c3b94f44SHugh Dickins 				 * page to have PageReclaim set next time memcg
1031c3b94f44SHugh Dickins 				 * reclaim reaches the tests above, so it will
1032c3b94f44SHugh Dickins 				 * then wait_on_page_writeback() to avoid OOM;
1033c3b94f44SHugh Dickins 				 * and it's also appropriate in global reclaim.
1034c3b94f44SHugh Dickins 				 */
1035c3b94f44SHugh Dickins 				SetPageReclaim(page);
103692df3a72SMel Gorman 				nr_writeback++;
1037c3b94f44SHugh Dickins 				goto keep_locked;
1038283aba9fSMel Gorman 
1039283aba9fSMel Gorman 			/* Case 3 above */
1040283aba9fSMel Gorman 			} else {
10417fadc820SHugh Dickins 				unlock_page(page);
1042c3b94f44SHugh Dickins 				wait_on_page_writeback(page);
10437fadc820SHugh Dickins 				/* then go back and try same page again */
10447fadc820SHugh Dickins 				list_add_tail(&page->lru, page_list);
10457fadc820SHugh Dickins 				continue;
1046e62e384eSMichal Hocko 			}
1047283aba9fSMel Gorman 		}
10481da177e4SLinus Torvalds 
104902c6de8dSMinchan Kim 		if (!force_reclaim)
10506a18adb3SKonstantin Khlebnikov 			references = page_check_references(page, sc);
105102c6de8dSMinchan Kim 
1052dfc8d636SJohannes Weiner 		switch (references) {
1053dfc8d636SJohannes Weiner 		case PAGEREF_ACTIVATE:
10541da177e4SLinus Torvalds 			goto activate_locked;
105564574746SJohannes Weiner 		case PAGEREF_KEEP:
105664574746SJohannes Weiner 			goto keep_locked;
1057dfc8d636SJohannes Weiner 		case PAGEREF_RECLAIM:
1058dfc8d636SJohannes Weiner 		case PAGEREF_RECLAIM_CLEAN:
1059dfc8d636SJohannes Weiner 			; /* try to reclaim the page below */
1060dfc8d636SJohannes Weiner 		}
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds 		/*
10631da177e4SLinus Torvalds 		 * Anonymous process memory has backing store?
10641da177e4SLinus Torvalds 		 * Try to allocate it some swap space here.
10651da177e4SLinus Torvalds 		 */
1066b291f000SNick Piggin 		if (PageAnon(page) && !PageSwapCache(page)) {
106763eb6b93SHugh Dickins 			if (!(sc->gfp_mask & __GFP_IO))
106863eb6b93SHugh Dickins 				goto keep_locked;
10695bc7b8acSShaohua Li 			if (!add_to_swap(page, page_list))
10701da177e4SLinus Torvalds 				goto activate_locked;
1071854e9ed0SMinchan Kim 			lazyfree = true;
107263eb6b93SHugh Dickins 			may_enter_fs = 1;
10731da177e4SLinus Torvalds 
1074e2be15f6SMel Gorman 			/* Adding to swap updated mapping */
10751da177e4SLinus Torvalds 			mapping = page_mapping(page);
10767751b2daSKirill A. Shutemov 		} else if (unlikely(PageTransHuge(page))) {
10777751b2daSKirill A. Shutemov 			/* Split file THP */
10787751b2daSKirill A. Shutemov 			if (split_huge_page_to_list(page, page_list))
10797751b2daSKirill A. Shutemov 				goto keep_locked;
1080e2be15f6SMel Gorman 		}
10811da177e4SLinus Torvalds 
10827751b2daSKirill A. Shutemov 		VM_BUG_ON_PAGE(PageTransHuge(page), page);
10837751b2daSKirill A. Shutemov 
10841da177e4SLinus Torvalds 		/*
10851da177e4SLinus Torvalds 		 * The page is mapped into the page tables of one or more
10861da177e4SLinus Torvalds 		 * processes. Try to unmap it here.
10871da177e4SLinus Torvalds 		 */
10881da177e4SLinus Torvalds 		if (page_mapped(page) && mapping) {
1089854e9ed0SMinchan Kim 			switch (ret = try_to_unmap(page, lazyfree ?
1090854e9ed0SMinchan Kim 				(ttu_flags | TTU_BATCH_FLUSH | TTU_LZFREE) :
1091854e9ed0SMinchan Kim 				(ttu_flags | TTU_BATCH_FLUSH))) {
10921da177e4SLinus Torvalds 			case SWAP_FAIL:
10931da177e4SLinus Torvalds 				goto activate_locked;
10941da177e4SLinus Torvalds 			case SWAP_AGAIN:
10951da177e4SLinus Torvalds 				goto keep_locked;
1096b291f000SNick Piggin 			case SWAP_MLOCK:
1097b291f000SNick Piggin 				goto cull_mlocked;
1098854e9ed0SMinchan Kim 			case SWAP_LZFREE:
1099854e9ed0SMinchan Kim 				goto lazyfree;
11001da177e4SLinus Torvalds 			case SWAP_SUCCESS:
11011da177e4SLinus Torvalds 				; /* try to free the page below */
11021da177e4SLinus Torvalds 			}
11031da177e4SLinus Torvalds 		}
11041da177e4SLinus Torvalds 
11051da177e4SLinus Torvalds 		if (PageDirty(page)) {
1106ee72886dSMel Gorman 			/*
1107ee72886dSMel Gorman 			 * Only kswapd can writeback filesystem pages to
1108d43006d5SMel Gorman 			 * avoid risk of stack overflow but only writeback
1109d43006d5SMel Gorman 			 * if many dirty pages have been encountered.
1110ee72886dSMel Gorman 			 */
1111f84f6e2bSMel Gorman 			if (page_is_file_cache(page) &&
11129e3b2f8cSKonstantin Khlebnikov 					(!current_is_kswapd() ||
1113599d0c95SMel Gorman 					 !test_bit(PGDAT_DIRTY, &pgdat->flags))) {
111449ea7eb6SMel Gorman 				/*
111549ea7eb6SMel Gorman 				 * Immediately reclaim when written back.
111649ea7eb6SMel Gorman 				 * Similar in principal to deactivate_page()
111749ea7eb6SMel Gorman 				 * except we already have the page isolated
111849ea7eb6SMel Gorman 				 * and know it's dirty
111949ea7eb6SMel Gorman 				 */
1120c4a25635SMel Gorman 				inc_node_page_state(page, NR_VMSCAN_IMMEDIATE);
112149ea7eb6SMel Gorman 				SetPageReclaim(page);
112249ea7eb6SMel Gorman 
1123ee72886dSMel Gorman 				goto keep_locked;
1124ee72886dSMel Gorman 			}
1125ee72886dSMel Gorman 
1126dfc8d636SJohannes Weiner 			if (references == PAGEREF_RECLAIM_CLEAN)
11271da177e4SLinus Torvalds 				goto keep_locked;
11284dd4b920SAndrew Morton 			if (!may_enter_fs)
11291da177e4SLinus Torvalds 				goto keep_locked;
113052a8363eSChristoph Lameter 			if (!sc->may_writepage)
11311da177e4SLinus Torvalds 				goto keep_locked;
11321da177e4SLinus Torvalds 
1133d950c947SMel Gorman 			/*
1134d950c947SMel Gorman 			 * Page is dirty. Flush the TLB if a writable entry
1135d950c947SMel Gorman 			 * potentially exists to avoid CPU writes after IO
1136d950c947SMel Gorman 			 * starts and then write it out here.
1137d950c947SMel Gorman 			 */
1138d950c947SMel Gorman 			try_to_unmap_flush_dirty();
11397d3579e8SKOSAKI Motohiro 			switch (pageout(page, mapping, sc)) {
11401da177e4SLinus Torvalds 			case PAGE_KEEP:
11411da177e4SLinus Torvalds 				goto keep_locked;
11421da177e4SLinus Torvalds 			case PAGE_ACTIVATE:
11431da177e4SLinus Torvalds 				goto activate_locked;
11441da177e4SLinus Torvalds 			case PAGE_SUCCESS:
11457d3579e8SKOSAKI Motohiro 				if (PageWriteback(page))
114641ac1999SMel Gorman 					goto keep;
11477d3579e8SKOSAKI Motohiro 				if (PageDirty(page))
11481da177e4SLinus Torvalds 					goto keep;
11497d3579e8SKOSAKI Motohiro 
11501da177e4SLinus Torvalds 				/*
11511da177e4SLinus Torvalds 				 * A synchronous write - probably a ramdisk.  Go
11521da177e4SLinus Torvalds 				 * ahead and try to reclaim the page.
11531da177e4SLinus Torvalds 				 */
1154529ae9aaSNick Piggin 				if (!trylock_page(page))
11551da177e4SLinus Torvalds 					goto keep;
11561da177e4SLinus Torvalds 				if (PageDirty(page) || PageWriteback(page))
11571da177e4SLinus Torvalds 					goto keep_locked;
11581da177e4SLinus Torvalds 				mapping = page_mapping(page);
11591da177e4SLinus Torvalds 			case PAGE_CLEAN:
11601da177e4SLinus Torvalds 				; /* try to free the page below */
11611da177e4SLinus Torvalds 			}
11621da177e4SLinus Torvalds 		}
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 		/*
11651da177e4SLinus Torvalds 		 * If the page has buffers, try to free the buffer mappings
11661da177e4SLinus Torvalds 		 * associated with this page. If we succeed we try to free
11671da177e4SLinus Torvalds 		 * the page as well.
11681da177e4SLinus Torvalds 		 *
11691da177e4SLinus Torvalds 		 * We do this even if the page is PageDirty().
11701da177e4SLinus Torvalds 		 * try_to_release_page() does not perform I/O, but it is
11711da177e4SLinus Torvalds 		 * possible for a page to have PageDirty set, but it is actually
11721da177e4SLinus Torvalds 		 * clean (all its buffers are clean).  This happens if the
11731da177e4SLinus Torvalds 		 * buffers were written out directly, with submit_bh(). ext3
11741da177e4SLinus Torvalds 		 * will do this, as well as the blockdev mapping.
11751da177e4SLinus Torvalds 		 * try_to_release_page() will discover that cleanness and will
11761da177e4SLinus Torvalds 		 * drop the buffers and mark the page clean - it can be freed.
11771da177e4SLinus Torvalds 		 *
11781da177e4SLinus Torvalds 		 * Rarely, pages can have buffers and no ->mapping.  These are
11791da177e4SLinus Torvalds 		 * the pages which were not successfully invalidated in
11801da177e4SLinus Torvalds 		 * truncate_complete_page().  We try to drop those buffers here
11811da177e4SLinus Torvalds 		 * and if that worked, and the page is no longer mapped into
11821da177e4SLinus Torvalds 		 * process address space (page_count == 1) it can be freed.
11831da177e4SLinus Torvalds 		 * Otherwise, leave the page on the LRU so it is swappable.
11841da177e4SLinus Torvalds 		 */
1185266cf658SDavid Howells 		if (page_has_private(page)) {
11861da177e4SLinus Torvalds 			if (!try_to_release_page(page, sc->gfp_mask))
11871da177e4SLinus Torvalds 				goto activate_locked;
1188e286781dSNick Piggin 			if (!mapping && page_count(page) == 1) {
1189e286781dSNick Piggin 				unlock_page(page);
1190e286781dSNick Piggin 				if (put_page_testzero(page))
11911da177e4SLinus Torvalds 					goto free_it;
1192e286781dSNick Piggin 				else {
1193e286781dSNick Piggin 					/*
1194e286781dSNick Piggin 					 * rare race with speculative reference.
1195e286781dSNick Piggin 					 * the speculative reference will free
1196e286781dSNick Piggin 					 * this page shortly, so we may
1197e286781dSNick Piggin 					 * increment nr_reclaimed here (and
1198e286781dSNick Piggin 					 * leave it off the LRU).
1199e286781dSNick Piggin 					 */
1200e286781dSNick Piggin 					nr_reclaimed++;
1201e286781dSNick Piggin 					continue;
1202e286781dSNick Piggin 				}
1203e286781dSNick Piggin 			}
12041da177e4SLinus Torvalds 		}
12051da177e4SLinus Torvalds 
1206854e9ed0SMinchan Kim lazyfree:
1207a528910eSJohannes Weiner 		if (!mapping || !__remove_mapping(mapping, page, true))
120849d2e9ccSChristoph Lameter 			goto keep_locked;
12091da177e4SLinus Torvalds 
1210a978d6f5SNick Piggin 		/*
1211a978d6f5SNick Piggin 		 * At this point, we have no other references and there is
1212a978d6f5SNick Piggin 		 * no way to pick any more up (removed from LRU, removed
1213a978d6f5SNick Piggin 		 * from pagecache). Can use non-atomic bitops now (and
1214a978d6f5SNick Piggin 		 * we obviously don't have to worry about waking up a process
1215a978d6f5SNick Piggin 		 * waiting on the page lock, because there are no references.
1216a978d6f5SNick Piggin 		 */
121748c935adSKirill A. Shutemov 		__ClearPageLocked(page);
1218e286781dSNick Piggin free_it:
1219854e9ed0SMinchan Kim 		if (ret == SWAP_LZFREE)
1220854e9ed0SMinchan Kim 			count_vm_event(PGLAZYFREED);
1221854e9ed0SMinchan Kim 
122205ff5137SAndrew Morton 		nr_reclaimed++;
1223abe4c3b5SMel Gorman 
1224abe4c3b5SMel Gorman 		/*
1225abe4c3b5SMel Gorman 		 * Is there need to periodically free_page_list? It would
1226abe4c3b5SMel Gorman 		 * appear not as the counts should be low
1227abe4c3b5SMel Gorman 		 */
1228abe4c3b5SMel Gorman 		list_add(&page->lru, &free_pages);
12291da177e4SLinus Torvalds 		continue;
12301da177e4SLinus Torvalds 
1231b291f000SNick Piggin cull_mlocked:
123263d6c5adSHugh Dickins 		if (PageSwapCache(page))
123363d6c5adSHugh Dickins 			try_to_free_swap(page);
1234b291f000SNick Piggin 		unlock_page(page);
1235c54839a7SJaewon Kim 		list_add(&page->lru, &ret_pages);
1236b291f000SNick Piggin 		continue;
1237b291f000SNick Piggin 
12381da177e4SLinus Torvalds activate_locked:
123968a22394SRik van Riel 		/* Not a candidate for swapping, so reclaim swap space. */
12405ccc5abaSVladimir Davydov 		if (PageSwapCache(page) && mem_cgroup_swap_full(page))
1241a2c43eedSHugh Dickins 			try_to_free_swap(page);
1242309381feSSasha Levin 		VM_BUG_ON_PAGE(PageActive(page), page);
12431da177e4SLinus Torvalds 		SetPageActive(page);
12441da177e4SLinus Torvalds 		pgactivate++;
12451da177e4SLinus Torvalds keep_locked:
12461da177e4SLinus Torvalds 		unlock_page(page);
12471da177e4SLinus Torvalds keep:
12481da177e4SLinus Torvalds 		list_add(&page->lru, &ret_pages);
1249309381feSSasha Levin 		VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page);
12501da177e4SLinus Torvalds 	}
1251abe4c3b5SMel Gorman 
1252747db954SJohannes Weiner 	mem_cgroup_uncharge_list(&free_pages);
125372b252aeSMel Gorman 	try_to_unmap_flush();
1254b745bc85SMel Gorman 	free_hot_cold_page_list(&free_pages, true);
1255abe4c3b5SMel Gorman 
12561da177e4SLinus Torvalds 	list_splice(&ret_pages, page_list);
1257f8891e5eSChristoph Lameter 	count_vm_events(PGACTIVATE, pgactivate);
12580a31bc97SJohannes Weiner 
12598e950282SMel Gorman 	*ret_nr_dirty += nr_dirty;
12608e950282SMel Gorman 	*ret_nr_congested += nr_congested;
1261d43006d5SMel Gorman 	*ret_nr_unqueued_dirty += nr_unqueued_dirty;
126292df3a72SMel Gorman 	*ret_nr_writeback += nr_writeback;
1263b1a6f21eSMel Gorman 	*ret_nr_immediate += nr_immediate;
126405ff5137SAndrew Morton 	return nr_reclaimed;
12651da177e4SLinus Torvalds }
12661da177e4SLinus Torvalds 
126702c6de8dSMinchan Kim unsigned long reclaim_clean_pages_from_list(struct zone *zone,
126802c6de8dSMinchan Kim 					    struct list_head *page_list)
126902c6de8dSMinchan Kim {
127002c6de8dSMinchan Kim 	struct scan_control sc = {
127102c6de8dSMinchan Kim 		.gfp_mask = GFP_KERNEL,
127202c6de8dSMinchan Kim 		.priority = DEF_PRIORITY,
127302c6de8dSMinchan Kim 		.may_unmap = 1,
127402c6de8dSMinchan Kim 	};
12758e950282SMel Gorman 	unsigned long ret, dummy1, dummy2, dummy3, dummy4, dummy5;
127602c6de8dSMinchan Kim 	struct page *page, *next;
127702c6de8dSMinchan Kim 	LIST_HEAD(clean_pages);
127802c6de8dSMinchan Kim 
127902c6de8dSMinchan Kim 	list_for_each_entry_safe(page, next, page_list, lru) {
1280117aad1eSRafael Aquini 		if (page_is_file_cache(page) && !PageDirty(page) &&
1281b1123ea6SMinchan Kim 		    !__PageMovable(page)) {
128202c6de8dSMinchan Kim 			ClearPageActive(page);
128302c6de8dSMinchan Kim 			list_move(&page->lru, &clean_pages);
128402c6de8dSMinchan Kim 		}
128502c6de8dSMinchan Kim 	}
128602c6de8dSMinchan Kim 
1287599d0c95SMel Gorman 	ret = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc,
128802c6de8dSMinchan Kim 			TTU_UNMAP|TTU_IGNORE_ACCESS,
12898e950282SMel Gorman 			&dummy1, &dummy2, &dummy3, &dummy4, &dummy5, true);
129002c6de8dSMinchan Kim 	list_splice(&clean_pages, page_list);
1291599d0c95SMel Gorman 	mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, -ret);
129202c6de8dSMinchan Kim 	return ret;
129302c6de8dSMinchan Kim }
129402c6de8dSMinchan Kim 
12955ad333ebSAndy Whitcroft /*
12965ad333ebSAndy Whitcroft  * Attempt to remove the specified page from its LRU.  Only take this page
12975ad333ebSAndy Whitcroft  * if it is of the appropriate PageActive status.  Pages which are being
12985ad333ebSAndy Whitcroft  * freed elsewhere are also ignored.
12995ad333ebSAndy Whitcroft  *
13005ad333ebSAndy Whitcroft  * page:	page to consider
13015ad333ebSAndy Whitcroft  * mode:	one of the LRU isolation modes defined above
13025ad333ebSAndy Whitcroft  *
13035ad333ebSAndy Whitcroft  * returns 0 on success, -ve errno on failure.
13045ad333ebSAndy Whitcroft  */
1305f3fd4a61SKonstantin Khlebnikov int __isolate_lru_page(struct page *page, isolate_mode_t mode)
13065ad333ebSAndy Whitcroft {
13075ad333ebSAndy Whitcroft 	int ret = -EINVAL;
13085ad333ebSAndy Whitcroft 
13095ad333ebSAndy Whitcroft 	/* Only take pages on the LRU. */
13105ad333ebSAndy Whitcroft 	if (!PageLRU(page))
13115ad333ebSAndy Whitcroft 		return ret;
13125ad333ebSAndy Whitcroft 
1313e46a2879SMinchan Kim 	/* Compaction should not handle unevictable pages but CMA can do so */
1314e46a2879SMinchan Kim 	if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE))
1315894bc310SLee Schermerhorn 		return ret;
1316894bc310SLee Schermerhorn 
13175ad333ebSAndy Whitcroft 	ret = -EBUSY;
131808e552c6SKAMEZAWA Hiroyuki 
1319c8244935SMel Gorman 	/*
1320c8244935SMel Gorman 	 * To minimise LRU disruption, the caller can indicate that it only
1321c8244935SMel Gorman 	 * wants to isolate pages it will be able to operate on without
1322c8244935SMel Gorman 	 * blocking - clean pages for the most part.
1323c8244935SMel Gorman 	 *
1324c8244935SMel Gorman 	 * ISOLATE_CLEAN means that only clean pages should be isolated. This
1325c8244935SMel Gorman 	 * is used by reclaim when it is cannot write to backing storage
1326c8244935SMel Gorman 	 *
1327c8244935SMel Gorman 	 * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages
1328c8244935SMel Gorman 	 * that it is possible to migrate without blocking
1329c8244935SMel Gorman 	 */
1330c8244935SMel Gorman 	if (mode & (ISOLATE_CLEAN|ISOLATE_ASYNC_MIGRATE)) {
1331c8244935SMel Gorman 		/* All the caller can do on PageWriteback is block */
1332c8244935SMel Gorman 		if (PageWriteback(page))
133339deaf85SMinchan Kim 			return ret;
133439deaf85SMinchan Kim 
1335c8244935SMel Gorman 		if (PageDirty(page)) {
1336c8244935SMel Gorman 			struct address_space *mapping;
1337c8244935SMel Gorman 
1338c8244935SMel Gorman 			/* ISOLATE_CLEAN means only clean pages */
1339c8244935SMel Gorman 			if (mode & ISOLATE_CLEAN)
1340c8244935SMel Gorman 				return ret;
1341c8244935SMel Gorman 
1342c8244935SMel Gorman 			/*
1343c8244935SMel Gorman 			 * Only pages without mappings or that have a
1344c8244935SMel Gorman 			 * ->migratepage callback are possible to migrate
1345c8244935SMel Gorman 			 * without blocking
1346c8244935SMel Gorman 			 */
1347c8244935SMel Gorman 			mapping = page_mapping(page);
1348c8244935SMel Gorman 			if (mapping && !mapping->a_ops->migratepage)
1349c8244935SMel Gorman 				return ret;
1350c8244935SMel Gorman 		}
1351c8244935SMel Gorman 	}
1352c8244935SMel Gorman 
1353f80c0673SMinchan Kim 	if ((mode & ISOLATE_UNMAPPED) && page_mapped(page))
1354f80c0673SMinchan Kim 		return ret;
1355f80c0673SMinchan Kim 
13565ad333ebSAndy Whitcroft 	if (likely(get_page_unless_zero(page))) {
13575ad333ebSAndy Whitcroft 		/*
13585ad333ebSAndy Whitcroft 		 * Be careful not to clear PageLRU until after we're
13595ad333ebSAndy Whitcroft 		 * sure the page is not being freed elsewhere -- the
13605ad333ebSAndy Whitcroft 		 * page release code relies on it.
13615ad333ebSAndy Whitcroft 		 */
13625ad333ebSAndy Whitcroft 		ClearPageLRU(page);
13635ad333ebSAndy Whitcroft 		ret = 0;
13645ad333ebSAndy Whitcroft 	}
13655ad333ebSAndy Whitcroft 
13665ad333ebSAndy Whitcroft 	return ret;
13675ad333ebSAndy Whitcroft }
13685ad333ebSAndy Whitcroft 
136949d2e9ccSChristoph Lameter /*
1370a52633d8SMel Gorman  * zone_lru_lock is heavily contended.  Some of the functions that
13711da177e4SLinus Torvalds  * shrink the lists perform better by taking out a batch of pages
13721da177e4SLinus Torvalds  * and working on them outside the LRU lock.
13731da177e4SLinus Torvalds  *
13741da177e4SLinus Torvalds  * For pagecache intensive workloads, this function is the hottest
13751da177e4SLinus Torvalds  * spot in the kernel (apart from copy_*_user functions).
13761da177e4SLinus Torvalds  *
13771da177e4SLinus Torvalds  * Appropriate locks must be held before calling this function.
13781da177e4SLinus Torvalds  *
13791da177e4SLinus Torvalds  * @nr_to_scan:	The number of pages to look through on the list.
13805dc35979SKonstantin Khlebnikov  * @lruvec:	The LRU vector to pull pages from.
13811da177e4SLinus Torvalds  * @dst:	The temp list to put pages on to.
1382f626012dSHugh Dickins  * @nr_scanned:	The number of pages that were scanned.
1383fe2c2a10SRik van Riel  * @sc:		The scan_control struct for this reclaim session
13845ad333ebSAndy Whitcroft  * @mode:	One of the LRU isolation modes
13853cb99451SKonstantin Khlebnikov  * @lru:	LRU list id for isolating
13861da177e4SLinus Torvalds  *
13871da177e4SLinus Torvalds  * returns how many pages were moved onto *@dst.
13881da177e4SLinus Torvalds  */
138969e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
13905dc35979SKonstantin Khlebnikov 		struct lruvec *lruvec, struct list_head *dst,
1391fe2c2a10SRik van Riel 		unsigned long *nr_scanned, struct scan_control *sc,
13923cb99451SKonstantin Khlebnikov 		isolate_mode_t mode, enum lru_list lru)
13931da177e4SLinus Torvalds {
139475b00af7SHugh Dickins 	struct list_head *src = &lruvec->lists[lru];
139569e05944SAndrew Morton 	unsigned long nr_taken = 0;
1396599d0c95SMel Gorman 	unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 };
1397*7cc30fcfSMel Gorman 	unsigned long nr_skipped[MAX_NR_ZONES] = { 0, };
1398599d0c95SMel Gorman 	unsigned long scan, nr_pages;
1399b2e18757SMel Gorman 	LIST_HEAD(pages_skipped);
14001da177e4SLinus Torvalds 
14010b802f10SVladimir Davydov 	for (scan = 0; scan < nr_to_scan && nr_taken < nr_to_scan &&
14020b802f10SVladimir Davydov 					!list_empty(src); scan++) {
14035ad333ebSAndy Whitcroft 		struct page *page;
14045ad333ebSAndy Whitcroft 
14051da177e4SLinus Torvalds 		page = lru_to_page(src);
14061da177e4SLinus Torvalds 		prefetchw_prev_lru_page(page, src, flags);
14071da177e4SLinus Torvalds 
1408309381feSSasha Levin 		VM_BUG_ON_PAGE(!PageLRU(page), page);
14098d438f96SNick Piggin 
1410b2e18757SMel Gorman 		if (page_zonenum(page) > sc->reclaim_idx) {
1411b2e18757SMel Gorman 			list_move(&page->lru, &pages_skipped);
1412*7cc30fcfSMel Gorman 			nr_skipped[page_zonenum(page)]++;
1413b2e18757SMel Gorman 			continue;
1414b2e18757SMel Gorman 		}
1415b2e18757SMel Gorman 
1416f3fd4a61SKonstantin Khlebnikov 		switch (__isolate_lru_page(page, mode)) {
14175ad333ebSAndy Whitcroft 		case 0:
1418599d0c95SMel Gorman 			nr_pages = hpage_nr_pages(page);
1419599d0c95SMel Gorman 			nr_taken += nr_pages;
1420599d0c95SMel Gorman 			nr_zone_taken[page_zonenum(page)] += nr_pages;
14215ad333ebSAndy Whitcroft 			list_move(&page->lru, dst);
14225ad333ebSAndy Whitcroft 			break;
14237c8ee9a8SNick Piggin 
14245ad333ebSAndy Whitcroft 		case -EBUSY:
14255ad333ebSAndy Whitcroft 			/* else it is being freed elsewhere */
14265ad333ebSAndy Whitcroft 			list_move(&page->lru, src);
14275ad333ebSAndy Whitcroft 			continue;
14285ad333ebSAndy Whitcroft 
14295ad333ebSAndy Whitcroft 		default:
14305ad333ebSAndy Whitcroft 			BUG();
14315ad333ebSAndy Whitcroft 		}
14325ad333ebSAndy Whitcroft 	}
14331da177e4SLinus Torvalds 
1434b2e18757SMel Gorman 	/*
1435b2e18757SMel Gorman 	 * Splice any skipped pages to the start of the LRU list. Note that
1436b2e18757SMel Gorman 	 * this disrupts the LRU order when reclaiming for lower zones but
1437b2e18757SMel Gorman 	 * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX
1438b2e18757SMel Gorman 	 * scanning would soon rescan the same pages to skip and put the
1439b2e18757SMel Gorman 	 * system at risk of premature OOM.
1440b2e18757SMel Gorman 	 */
1441*7cc30fcfSMel Gorman 	if (!list_empty(&pages_skipped)) {
1442*7cc30fcfSMel Gorman 		int zid;
1443*7cc30fcfSMel Gorman 
1444b2e18757SMel Gorman 		list_splice(&pages_skipped, src);
1445*7cc30fcfSMel Gorman 		for (zid = 0; zid < MAX_NR_ZONES; zid++) {
1446*7cc30fcfSMel Gorman 			if (!nr_skipped[zid])
1447*7cc30fcfSMel Gorman 				continue;
1448*7cc30fcfSMel Gorman 
1449*7cc30fcfSMel Gorman 			__count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]);
1450*7cc30fcfSMel Gorman 		}
1451*7cc30fcfSMel Gorman 	}
1452f626012dSHugh Dickins 	*nr_scanned = scan;
1453e5146b12SMel Gorman 	trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan, scan,
145475b00af7SHugh Dickins 				    nr_taken, mode, is_file_lru(lru));
1455599d0c95SMel Gorman 	for (scan = 0; scan < MAX_NR_ZONES; scan++) {
1456599d0c95SMel Gorman 		nr_pages = nr_zone_taken[scan];
1457599d0c95SMel Gorman 		if (!nr_pages)
1458599d0c95SMel Gorman 			continue;
1459599d0c95SMel Gorman 
1460599d0c95SMel Gorman 		update_lru_size(lruvec, lru, scan, -nr_pages);
1461599d0c95SMel Gorman 	}
14621da177e4SLinus Torvalds 	return nr_taken;
14631da177e4SLinus Torvalds }
14641da177e4SLinus Torvalds 
146562695a84SNick Piggin /**
146662695a84SNick Piggin  * isolate_lru_page - tries to isolate a page from its LRU list
146762695a84SNick Piggin  * @page: page to isolate from its LRU list
146862695a84SNick Piggin  *
146962695a84SNick Piggin  * Isolates a @page from an LRU list, clears PageLRU and adjusts the
147062695a84SNick Piggin  * vmstat statistic corresponding to whatever LRU list the page was on.
147162695a84SNick Piggin  *
147262695a84SNick Piggin  * Returns 0 if the page was removed from an LRU list.
147362695a84SNick Piggin  * Returns -EBUSY if the page was not on an LRU list.
147462695a84SNick Piggin  *
147562695a84SNick Piggin  * The returned page will have PageLRU() cleared.  If it was found on
1476894bc310SLee Schermerhorn  * the active list, it will have PageActive set.  If it was found on
1477894bc310SLee Schermerhorn  * the unevictable list, it will have the PageUnevictable bit set. That flag
1478894bc310SLee Schermerhorn  * may need to be cleared by the caller before letting the page go.
147962695a84SNick Piggin  *
148062695a84SNick Piggin  * The vmstat statistic corresponding to the list on which the page was
148162695a84SNick Piggin  * found will be decremented.
148262695a84SNick Piggin  *
148362695a84SNick Piggin  * Restrictions:
148462695a84SNick Piggin  * (1) Must be called with an elevated refcount on the page. This is a
148562695a84SNick Piggin  *     fundamentnal difference from isolate_lru_pages (which is called
148662695a84SNick Piggin  *     without a stable reference).
148762695a84SNick Piggin  * (2) the lru_lock must not be held.
148862695a84SNick Piggin  * (3) interrupts must be enabled.
148962695a84SNick Piggin  */
149062695a84SNick Piggin int isolate_lru_page(struct page *page)
149162695a84SNick Piggin {
149262695a84SNick Piggin 	int ret = -EBUSY;
149362695a84SNick Piggin 
1494309381feSSasha Levin 	VM_BUG_ON_PAGE(!page_count(page), page);
1495cf2a82eeSKirill A. Shutemov 	WARN_RATELIMIT(PageTail(page), "trying to isolate tail page");
14960c917313SKonstantin Khlebnikov 
149762695a84SNick Piggin 	if (PageLRU(page)) {
149862695a84SNick Piggin 		struct zone *zone = page_zone(page);
1499fa9add64SHugh Dickins 		struct lruvec *lruvec;
150062695a84SNick Piggin 
1501a52633d8SMel Gorman 		spin_lock_irq(zone_lru_lock(zone));
1502599d0c95SMel Gorman 		lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat);
15030c917313SKonstantin Khlebnikov 		if (PageLRU(page)) {
1504894bc310SLee Schermerhorn 			int lru = page_lru(page);
15050c917313SKonstantin Khlebnikov 			get_page(page);
150662695a84SNick Piggin 			ClearPageLRU(page);
1507fa9add64SHugh Dickins 			del_page_from_lru_list(page, lruvec, lru);
1508fa9add64SHugh Dickins 			ret = 0;
150962695a84SNick Piggin 		}
1510a52633d8SMel Gorman 		spin_unlock_irq(zone_lru_lock(zone));
151162695a84SNick Piggin 	}
151262695a84SNick Piggin 	return ret;
151362695a84SNick Piggin }
151462695a84SNick Piggin 
15155ad333ebSAndy Whitcroft /*
1516d37dd5dcSFengguang Wu  * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and
1517d37dd5dcSFengguang Wu  * then get resheduled. When there are massive number of tasks doing page
1518d37dd5dcSFengguang Wu  * allocation, such sleeping direct reclaimers may keep piling up on each CPU,
1519d37dd5dcSFengguang Wu  * the LRU list will go small and be scanned faster than necessary, leading to
1520d37dd5dcSFengguang Wu  * unnecessary swapping, thrashing and OOM.
152135cd7815SRik van Riel  */
1522599d0c95SMel Gorman static int too_many_isolated(struct pglist_data *pgdat, int file,
152335cd7815SRik van Riel 		struct scan_control *sc)
152435cd7815SRik van Riel {
152535cd7815SRik van Riel 	unsigned long inactive, isolated;
152635cd7815SRik van Riel 
152735cd7815SRik van Riel 	if (current_is_kswapd())
152835cd7815SRik van Riel 		return 0;
152935cd7815SRik van Riel 
153097c9341fSTejun Heo 	if (!sane_reclaim(sc))
153135cd7815SRik van Riel 		return 0;
153235cd7815SRik van Riel 
153335cd7815SRik van Riel 	if (file) {
1534599d0c95SMel Gorman 		inactive = node_page_state(pgdat, NR_INACTIVE_FILE);
1535599d0c95SMel Gorman 		isolated = node_page_state(pgdat, NR_ISOLATED_FILE);
153635cd7815SRik van Riel 	} else {
1537599d0c95SMel Gorman 		inactive = node_page_state(pgdat, NR_INACTIVE_ANON);
1538599d0c95SMel Gorman 		isolated = node_page_state(pgdat, NR_ISOLATED_ANON);
153935cd7815SRik van Riel 	}
154035cd7815SRik van Riel 
15413cf23841SFengguang Wu 	/*
15423cf23841SFengguang Wu 	 * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they
15433cf23841SFengguang Wu 	 * won't get blocked by normal direct-reclaimers, forming a circular
15443cf23841SFengguang Wu 	 * deadlock.
15453cf23841SFengguang Wu 	 */
1546d0164adcSMel Gorman 	if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS))
15473cf23841SFengguang Wu 		inactive >>= 3;
15483cf23841SFengguang Wu 
154935cd7815SRik van Riel 	return isolated > inactive;
155035cd7815SRik van Riel }
155135cd7815SRik van Riel 
155266635629SMel Gorman static noinline_for_stack void
155375b00af7SHugh Dickins putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list)
155466635629SMel Gorman {
155527ac81d8SKonstantin Khlebnikov 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
1556599d0c95SMel Gorman 	struct pglist_data *pgdat = lruvec_pgdat(lruvec);
15573f79768fSHugh Dickins 	LIST_HEAD(pages_to_free);
155866635629SMel Gorman 
155966635629SMel Gorman 	/*
156066635629SMel Gorman 	 * Put back any unfreeable pages.
156166635629SMel Gorman 	 */
156266635629SMel Gorman 	while (!list_empty(page_list)) {
15633f79768fSHugh Dickins 		struct page *page = lru_to_page(page_list);
156466635629SMel Gorman 		int lru;
15653f79768fSHugh Dickins 
1566309381feSSasha Levin 		VM_BUG_ON_PAGE(PageLRU(page), page);
156766635629SMel Gorman 		list_del(&page->lru);
156839b5f29aSHugh Dickins 		if (unlikely(!page_evictable(page))) {
1569599d0c95SMel Gorman 			spin_unlock_irq(&pgdat->lru_lock);
157066635629SMel Gorman 			putback_lru_page(page);
1571599d0c95SMel Gorman 			spin_lock_irq(&pgdat->lru_lock);
157266635629SMel Gorman 			continue;
157366635629SMel Gorman 		}
1574fa9add64SHugh Dickins 
1575599d0c95SMel Gorman 		lruvec = mem_cgroup_page_lruvec(page, pgdat);
1576fa9add64SHugh Dickins 
15777a608572SLinus Torvalds 		SetPageLRU(page);
157866635629SMel Gorman 		lru = page_lru(page);
1579fa9add64SHugh Dickins 		add_page_to_lru_list(page, lruvec, lru);
1580fa9add64SHugh Dickins 
158166635629SMel Gorman 		if (is_active_lru(lru)) {
158266635629SMel Gorman 			int file = is_file_lru(lru);
15839992af10SRik van Riel 			int numpages = hpage_nr_pages(page);
15849992af10SRik van Riel 			reclaim_stat->recent_rotated[file] += numpages;
158566635629SMel Gorman 		}
15862bcf8879SHugh Dickins 		if (put_page_testzero(page)) {
15872bcf8879SHugh Dickins 			__ClearPageLRU(page);
15882bcf8879SHugh Dickins 			__ClearPageActive(page);
1589fa9add64SHugh Dickins 			del_page_from_lru_list(page, lruvec, lru);
15902bcf8879SHugh Dickins 
15912bcf8879SHugh Dickins 			if (unlikely(PageCompound(page))) {
1592599d0c95SMel Gorman 				spin_unlock_irq(&pgdat->lru_lock);
1593747db954SJohannes Weiner 				mem_cgroup_uncharge(page);
15942bcf8879SHugh Dickins 				(*get_compound_page_dtor(page))(page);
1595599d0c95SMel Gorman 				spin_lock_irq(&pgdat->lru_lock);
15962bcf8879SHugh Dickins 			} else
15972bcf8879SHugh Dickins 				list_add(&page->lru, &pages_to_free);
159866635629SMel Gorman 		}
159966635629SMel Gorman 	}
160066635629SMel Gorman 
16013f79768fSHugh Dickins 	/*
16023f79768fSHugh Dickins 	 * To save our caller's stack, now use input list for pages to free.
16033f79768fSHugh Dickins 	 */
16043f79768fSHugh Dickins 	list_splice(&pages_to_free, page_list);
160566635629SMel Gorman }
160666635629SMel Gorman 
160766635629SMel Gorman /*
1608399ba0b9SNeilBrown  * If a kernel thread (such as nfsd for loop-back mounts) services
1609399ba0b9SNeilBrown  * a backing device by writing to the page cache it sets PF_LESS_THROTTLE.
1610399ba0b9SNeilBrown  * In that case we should only throttle if the backing device it is
1611399ba0b9SNeilBrown  * writing to is congested.  In other cases it is safe to throttle.
1612399ba0b9SNeilBrown  */
1613399ba0b9SNeilBrown static int current_may_throttle(void)
1614399ba0b9SNeilBrown {
1615399ba0b9SNeilBrown 	return !(current->flags & PF_LESS_THROTTLE) ||
1616399ba0b9SNeilBrown 		current->backing_dev_info == NULL ||
1617399ba0b9SNeilBrown 		bdi_write_congested(current->backing_dev_info);
1618399ba0b9SNeilBrown }
1619399ba0b9SNeilBrown 
1620399ba0b9SNeilBrown /*
1621b2e18757SMel Gorman  * shrink_inactive_list() is a helper for shrink_node().  It returns the number
16221742f19fSAndrew Morton  * of reclaimed pages
16231da177e4SLinus Torvalds  */
162466635629SMel Gorman static noinline_for_stack unsigned long
16251a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec,
16269e3b2f8cSKonstantin Khlebnikov 		     struct scan_control *sc, enum lru_list lru)
16271da177e4SLinus Torvalds {
16281da177e4SLinus Torvalds 	LIST_HEAD(page_list);
1629e247dbceSKOSAKI Motohiro 	unsigned long nr_scanned;
163005ff5137SAndrew Morton 	unsigned long nr_reclaimed = 0;
1631e247dbceSKOSAKI Motohiro 	unsigned long nr_taken;
16328e950282SMel Gorman 	unsigned long nr_dirty = 0;
16338e950282SMel Gorman 	unsigned long nr_congested = 0;
1634e2be15f6SMel Gorman 	unsigned long nr_unqueued_dirty = 0;
163592df3a72SMel Gorman 	unsigned long nr_writeback = 0;
1636b1a6f21eSMel Gorman 	unsigned long nr_immediate = 0;
1637f3fd4a61SKonstantin Khlebnikov 	isolate_mode_t isolate_mode = 0;
16383cb99451SKonstantin Khlebnikov 	int file = is_file_lru(lru);
1639599d0c95SMel Gorman 	struct pglist_data *pgdat = lruvec_pgdat(lruvec);
16401a93be0eSKonstantin Khlebnikov 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
164178dc583dSKOSAKI Motohiro 
1642599d0c95SMel Gorman 	while (unlikely(too_many_isolated(pgdat, file, sc))) {
164358355c78SKOSAKI Motohiro 		congestion_wait(BLK_RW_ASYNC, HZ/10);
164435cd7815SRik van Riel 
164535cd7815SRik van Riel 		/* We are about to die and free our memory. Return now. */
164635cd7815SRik van Riel 		if (fatal_signal_pending(current))
164735cd7815SRik van Riel 			return SWAP_CLUSTER_MAX;
164835cd7815SRik van Riel 	}
164935cd7815SRik van Riel 
16501da177e4SLinus Torvalds 	lru_add_drain();
1651f80c0673SMinchan Kim 
1652f80c0673SMinchan Kim 	if (!sc->may_unmap)
165361317289SHillf Danton 		isolate_mode |= ISOLATE_UNMAPPED;
1654f80c0673SMinchan Kim 	if (!sc->may_writepage)
165561317289SHillf Danton 		isolate_mode |= ISOLATE_CLEAN;
1656f80c0673SMinchan Kim 
1657599d0c95SMel Gorman 	spin_lock_irq(&pgdat->lru_lock);
16581da177e4SLinus Torvalds 
16595dc35979SKonstantin Khlebnikov 	nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list,
16605dc35979SKonstantin Khlebnikov 				     &nr_scanned, sc, isolate_mode, lru);
166195d918fcSKonstantin Khlebnikov 
1662599d0c95SMel Gorman 	__mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
16639d5e6a9fSHugh Dickins 	reclaim_stat->recent_scanned[file] += nr_taken;
166495d918fcSKonstantin Khlebnikov 
166589b5fae5SJohannes Weiner 	if (global_reclaim(sc)) {
1666599d0c95SMel Gorman 		__mod_node_page_state(pgdat, NR_PAGES_SCANNED, nr_scanned);
1667b35ea17bSKOSAKI Motohiro 		if (current_is_kswapd())
1668599d0c95SMel Gorman 			__count_vm_events(PGSCAN_KSWAPD, nr_scanned);
1669b35ea17bSKOSAKI Motohiro 		else
1670599d0c95SMel Gorman 			__count_vm_events(PGSCAN_DIRECT, nr_scanned);
1671b35ea17bSKOSAKI Motohiro 	}
1672599d0c95SMel Gorman 	spin_unlock_irq(&pgdat->lru_lock);
1673d563c050SHillf Danton 
1674d563c050SHillf Danton 	if (nr_taken == 0)
167566635629SMel Gorman 		return 0;
1676b35ea17bSKOSAKI Motohiro 
1677599d0c95SMel Gorman 	nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, TTU_UNMAP,
16788e950282SMel Gorman 				&nr_dirty, &nr_unqueued_dirty, &nr_congested,
16798e950282SMel Gorman 				&nr_writeback, &nr_immediate,
1680b1a6f21eSMel Gorman 				false);
1681c661b078SAndy Whitcroft 
1682599d0c95SMel Gorman 	spin_lock_irq(&pgdat->lru_lock);
16833f79768fSHugh Dickins 
1684904249aaSYing Han 	if (global_reclaim(sc)) {
1685b35ea17bSKOSAKI Motohiro 		if (current_is_kswapd())
1686599d0c95SMel Gorman 			__count_vm_events(PGSTEAL_KSWAPD, nr_reclaimed);
1687904249aaSYing Han 		else
1688599d0c95SMel Gorman 			__count_vm_events(PGSTEAL_DIRECT, nr_reclaimed);
1689904249aaSYing Han 	}
1690a74609faSNick Piggin 
169127ac81d8SKonstantin Khlebnikov 	putback_inactive_pages(lruvec, &page_list);
16923f79768fSHugh Dickins 
1693599d0c95SMel Gorman 	__mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken);
16943f79768fSHugh Dickins 
1695599d0c95SMel Gorman 	spin_unlock_irq(&pgdat->lru_lock);
16963f79768fSHugh Dickins 
1697747db954SJohannes Weiner 	mem_cgroup_uncharge_list(&page_list);
1698b745bc85SMel Gorman 	free_hot_cold_page_list(&page_list, true);
1699e11da5b4SMel Gorman 
170092df3a72SMel Gorman 	/*
170192df3a72SMel Gorman 	 * If reclaim is isolating dirty pages under writeback, it implies
170292df3a72SMel Gorman 	 * that the long-lived page allocation rate is exceeding the page
170392df3a72SMel Gorman 	 * laundering rate. Either the global limits are not being effective
170492df3a72SMel Gorman 	 * at throttling processes due to the page distribution throughout
170592df3a72SMel Gorman 	 * zones or there is heavy usage of a slow backing device. The
170692df3a72SMel Gorman 	 * only option is to throttle from reclaim context which is not ideal
170792df3a72SMel Gorman 	 * as there is no guarantee the dirtying process is throttled in the
170892df3a72SMel Gorman 	 * same way balance_dirty_pages() manages.
170992df3a72SMel Gorman 	 *
17108e950282SMel Gorman 	 * Once a zone is flagged ZONE_WRITEBACK, kswapd will count the number
17118e950282SMel Gorman 	 * of pages under pages flagged for immediate reclaim and stall if any
17128e950282SMel Gorman 	 * are encountered in the nr_immediate check below.
171392df3a72SMel Gorman 	 */
1714918fc718SMel Gorman 	if (nr_writeback && nr_writeback == nr_taken)
1715599d0c95SMel Gorman 		set_bit(PGDAT_WRITEBACK, &pgdat->flags);
171692df3a72SMel Gorman 
1717d43006d5SMel Gorman 	/*
171897c9341fSTejun Heo 	 * Legacy memcg will stall in page writeback so avoid forcibly
171997c9341fSTejun Heo 	 * stalling here.
1720d43006d5SMel Gorman 	 */
172197c9341fSTejun Heo 	if (sane_reclaim(sc)) {
1722b1a6f21eSMel Gorman 		/*
17238e950282SMel Gorman 		 * Tag a zone as congested if all the dirty pages scanned were
17248e950282SMel Gorman 		 * backed by a congested BDI and wait_iff_congested will stall.
17258e950282SMel Gorman 		 */
17268e950282SMel Gorman 		if (nr_dirty && nr_dirty == nr_congested)
1727599d0c95SMel Gorman 			set_bit(PGDAT_CONGESTED, &pgdat->flags);
17288e950282SMel Gorman 
17298e950282SMel Gorman 		/*
1730b1a6f21eSMel Gorman 		 * If dirty pages are scanned that are not queued for IO, it
1731b1a6f21eSMel Gorman 		 * implies that flushers are not keeping up. In this case, flag
1732599d0c95SMel Gorman 		 * the pgdat PGDAT_DIRTY and kswapd will start writing pages from
173357054651SJohannes Weiner 		 * reclaim context.
1734b1a6f21eSMel Gorman 		 */
1735b1a6f21eSMel Gorman 		if (nr_unqueued_dirty == nr_taken)
1736599d0c95SMel Gorman 			set_bit(PGDAT_DIRTY, &pgdat->flags);
1737b1a6f21eSMel Gorman 
1738b1a6f21eSMel Gorman 		/*
1739b738d764SLinus Torvalds 		 * If kswapd scans pages marked marked for immediate
1740b738d764SLinus Torvalds 		 * reclaim and under writeback (nr_immediate), it implies
1741b738d764SLinus Torvalds 		 * that pages are cycling through the LRU faster than
1742b1a6f21eSMel Gorman 		 * they are written so also forcibly stall.
1743b1a6f21eSMel Gorman 		 */
1744b738d764SLinus Torvalds 		if (nr_immediate && current_may_throttle())
1745b1a6f21eSMel Gorman 			congestion_wait(BLK_RW_ASYNC, HZ/10);
1746e2be15f6SMel Gorman 	}
1747d43006d5SMel Gorman 
17488e950282SMel Gorman 	/*
17498e950282SMel Gorman 	 * Stall direct reclaim for IO completions if underlying BDIs or zone
17508e950282SMel Gorman 	 * is congested. Allow kswapd to continue until it starts encountering
17518e950282SMel Gorman 	 * unqueued dirty pages or cycling through the LRU too quickly.
17528e950282SMel Gorman 	 */
1753399ba0b9SNeilBrown 	if (!sc->hibernation_mode && !current_is_kswapd() &&
1754399ba0b9SNeilBrown 	    current_may_throttle())
1755599d0c95SMel Gorman 		wait_iff_congested(pgdat, BLK_RW_ASYNC, HZ/10);
17568e950282SMel Gorman 
1757599d0c95SMel Gorman 	trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id,
1758599d0c95SMel Gorman 			nr_scanned, nr_reclaimed,
1759ba5e9579Syalin wang 			sc->priority, file);
176005ff5137SAndrew Morton 	return nr_reclaimed;
17611da177e4SLinus Torvalds }
17621da177e4SLinus Torvalds 
17633bb1a852SMartin Bligh /*
17641cfb419bSKAMEZAWA Hiroyuki  * This moves pages from the active list to the inactive list.
17651cfb419bSKAMEZAWA Hiroyuki  *
17661cfb419bSKAMEZAWA Hiroyuki  * We move them the other way if the page is referenced by one or more
17671cfb419bSKAMEZAWA Hiroyuki  * processes, from rmap.
17681cfb419bSKAMEZAWA Hiroyuki  *
17691cfb419bSKAMEZAWA Hiroyuki  * If the pages are mostly unmapped, the processing is fast and it is
1770a52633d8SMel Gorman  * appropriate to hold zone_lru_lock across the whole operation.  But if
17711cfb419bSKAMEZAWA Hiroyuki  * the pages are mapped, the processing is slow (page_referenced()) so we
1772a52633d8SMel Gorman  * should drop zone_lru_lock around each page.  It's impossible to balance
17731cfb419bSKAMEZAWA Hiroyuki  * this, so instead we remove the pages from the LRU while processing them.
17741cfb419bSKAMEZAWA Hiroyuki  * It is safe to rely on PG_active against the non-LRU pages in here because
17751cfb419bSKAMEZAWA Hiroyuki  * nobody will play with that bit on a non-LRU page.
17761cfb419bSKAMEZAWA Hiroyuki  *
17770139aa7bSJoonsoo Kim  * The downside is that we have to touch page->_refcount against each page.
17781cfb419bSKAMEZAWA Hiroyuki  * But we had to alter page->flags anyway.
17791cfb419bSKAMEZAWA Hiroyuki  */
17801cfb419bSKAMEZAWA Hiroyuki 
1781fa9add64SHugh Dickins static void move_active_pages_to_lru(struct lruvec *lruvec,
17823eb4140fSWu Fengguang 				     struct list_head *list,
17832bcf8879SHugh Dickins 				     struct list_head *pages_to_free,
17843eb4140fSWu Fengguang 				     enum lru_list lru)
17853eb4140fSWu Fengguang {
1786599d0c95SMel Gorman 	struct pglist_data *pgdat = lruvec_pgdat(lruvec);
17873eb4140fSWu Fengguang 	unsigned long pgmoved = 0;
17883eb4140fSWu Fengguang 	struct page *page;
1789fa9add64SHugh Dickins 	int nr_pages;
17903eb4140fSWu Fengguang 
17913eb4140fSWu Fengguang 	while (!list_empty(list)) {
17923eb4140fSWu Fengguang 		page = lru_to_page(list);
1793599d0c95SMel Gorman 		lruvec = mem_cgroup_page_lruvec(page, pgdat);
17943eb4140fSWu Fengguang 
1795309381feSSasha Levin 		VM_BUG_ON_PAGE(PageLRU(page), page);
17963eb4140fSWu Fengguang 		SetPageLRU(page);
17973eb4140fSWu Fengguang 
1798fa9add64SHugh Dickins 		nr_pages = hpage_nr_pages(page);
1799599d0c95SMel Gorman 		update_lru_size(lruvec, lru, page_zonenum(page), nr_pages);
1800925b7673SJohannes Weiner 		list_move(&page->lru, &lruvec->lists[lru]);
1801fa9add64SHugh Dickins 		pgmoved += nr_pages;
18023eb4140fSWu Fengguang 
18032bcf8879SHugh Dickins 		if (put_page_testzero(page)) {
18042bcf8879SHugh Dickins 			__ClearPageLRU(page);
18052bcf8879SHugh Dickins 			__ClearPageActive(page);
1806fa9add64SHugh Dickins 			del_page_from_lru_list(page, lruvec, lru);
18072bcf8879SHugh Dickins 
18082bcf8879SHugh Dickins 			if (unlikely(PageCompound(page))) {
1809599d0c95SMel Gorman 				spin_unlock_irq(&pgdat->lru_lock);
1810747db954SJohannes Weiner 				mem_cgroup_uncharge(page);
18112bcf8879SHugh Dickins 				(*get_compound_page_dtor(page))(page);
1812599d0c95SMel Gorman 				spin_lock_irq(&pgdat->lru_lock);
18132bcf8879SHugh Dickins 			} else
18142bcf8879SHugh Dickins 				list_add(&page->lru, pages_to_free);
18153eb4140fSWu Fengguang 		}
18163eb4140fSWu Fengguang 	}
18179d5e6a9fSHugh Dickins 
18183eb4140fSWu Fengguang 	if (!is_active_lru(lru))
18193eb4140fSWu Fengguang 		__count_vm_events(PGDEACTIVATE, pgmoved);
18203eb4140fSWu Fengguang }
18211cfb419bSKAMEZAWA Hiroyuki 
1822f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan,
18231a93be0eSKonstantin Khlebnikov 			       struct lruvec *lruvec,
1824f16015fbSJohannes Weiner 			       struct scan_control *sc,
18259e3b2f8cSKonstantin Khlebnikov 			       enum lru_list lru)
18261cfb419bSKAMEZAWA Hiroyuki {
182744c241f1SKOSAKI Motohiro 	unsigned long nr_taken;
1828f626012dSHugh Dickins 	unsigned long nr_scanned;
18296fe6b7e3SWu Fengguang 	unsigned long vm_flags;
18301cfb419bSKAMEZAWA Hiroyuki 	LIST_HEAD(l_hold);	/* The pages which were snipped off */
18318cab4754SWu Fengguang 	LIST_HEAD(l_active);
1832b69408e8SChristoph Lameter 	LIST_HEAD(l_inactive);
18331cfb419bSKAMEZAWA Hiroyuki 	struct page *page;
18341a93be0eSKonstantin Khlebnikov 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
183544c241f1SKOSAKI Motohiro 	unsigned long nr_rotated = 0;
1836f3fd4a61SKonstantin Khlebnikov 	isolate_mode_t isolate_mode = 0;
18373cb99451SKonstantin Khlebnikov 	int file = is_file_lru(lru);
1838599d0c95SMel Gorman 	struct pglist_data *pgdat = lruvec_pgdat(lruvec);
18391cfb419bSKAMEZAWA Hiroyuki 
18401da177e4SLinus Torvalds 	lru_add_drain();
1841f80c0673SMinchan Kim 
1842f80c0673SMinchan Kim 	if (!sc->may_unmap)
184361317289SHillf Danton 		isolate_mode |= ISOLATE_UNMAPPED;
1844f80c0673SMinchan Kim 	if (!sc->may_writepage)
184561317289SHillf Danton 		isolate_mode |= ISOLATE_CLEAN;
1846f80c0673SMinchan Kim 
1847599d0c95SMel Gorman 	spin_lock_irq(&pgdat->lru_lock);
1848925b7673SJohannes Weiner 
18495dc35979SKonstantin Khlebnikov 	nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold,
18505dc35979SKonstantin Khlebnikov 				     &nr_scanned, sc, isolate_mode, lru);
185189b5fae5SJohannes Weiner 
1852599d0c95SMel Gorman 	__mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
1853b7c46d15SJohannes Weiner 	reclaim_stat->recent_scanned[file] += nr_taken;
18541cfb419bSKAMEZAWA Hiroyuki 
18559d5e6a9fSHugh Dickins 	if (global_reclaim(sc))
1856599d0c95SMel Gorman 		__mod_node_page_state(pgdat, NR_PAGES_SCANNED, nr_scanned);
1857599d0c95SMel Gorman 	__count_vm_events(PGREFILL, nr_scanned);
18589d5e6a9fSHugh Dickins 
1859599d0c95SMel Gorman 	spin_unlock_irq(&pgdat->lru_lock);
18601da177e4SLinus Torvalds 
18611da177e4SLinus Torvalds 	while (!list_empty(&l_hold)) {
18621da177e4SLinus Torvalds 		cond_resched();
18631da177e4SLinus Torvalds 		page = lru_to_page(&l_hold);
18641da177e4SLinus Torvalds 		list_del(&page->lru);
18657e9cd484SRik van Riel 
186639b5f29aSHugh Dickins 		if (unlikely(!page_evictable(page))) {
1867894bc310SLee Schermerhorn 			putback_lru_page(page);
1868894bc310SLee Schermerhorn 			continue;
1869894bc310SLee Schermerhorn 		}
1870894bc310SLee Schermerhorn 
1871cc715d99SMel Gorman 		if (unlikely(buffer_heads_over_limit)) {
1872cc715d99SMel Gorman 			if (page_has_private(page) && trylock_page(page)) {
1873cc715d99SMel Gorman 				if (page_has_private(page))
1874cc715d99SMel Gorman 					try_to_release_page(page, 0);
1875cc715d99SMel Gorman 				unlock_page(page);
1876cc715d99SMel Gorman 			}
1877cc715d99SMel Gorman 		}
1878cc715d99SMel Gorman 
1879c3ac9a8aSJohannes Weiner 		if (page_referenced(page, 0, sc->target_mem_cgroup,
1880c3ac9a8aSJohannes Weiner 				    &vm_flags)) {
18819992af10SRik van Riel 			nr_rotated += hpage_nr_pages(page);
18828cab4754SWu Fengguang 			/*
18838cab4754SWu Fengguang 			 * Identify referenced, file-backed active pages and
18848cab4754SWu Fengguang 			 * give them one more trip around the active list. So
18858cab4754SWu Fengguang 			 * that executable code get better chances to stay in
18868cab4754SWu Fengguang 			 * memory under moderate memory pressure.  Anon pages
18878cab4754SWu Fengguang 			 * are not likely to be evicted by use-once streaming
18888cab4754SWu Fengguang 			 * IO, plus JVM can create lots of anon VM_EXEC pages,
18898cab4754SWu Fengguang 			 * so we ignore them here.
18908cab4754SWu Fengguang 			 */
189141e20983SWu Fengguang 			if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) {
18928cab4754SWu Fengguang 				list_add(&page->lru, &l_active);
18938cab4754SWu Fengguang 				continue;
18948cab4754SWu Fengguang 			}
18958cab4754SWu Fengguang 		}
18967e9cd484SRik van Riel 
18975205e56eSKOSAKI Motohiro 		ClearPageActive(page);	/* we are de-activating */
18981da177e4SLinus Torvalds 		list_add(&page->lru, &l_inactive);
18991da177e4SLinus Torvalds 	}
19001da177e4SLinus Torvalds 
1901b555749aSAndrew Morton 	/*
19028cab4754SWu Fengguang 	 * Move pages back to the lru list.
1903b555749aSAndrew Morton 	 */
1904599d0c95SMel Gorman 	spin_lock_irq(&pgdat->lru_lock);
19054f98a2feSRik van Riel 	/*
19068cab4754SWu Fengguang 	 * Count referenced pages from currently used mappings as rotated,
19078cab4754SWu Fengguang 	 * even though only some of them are actually re-activated.  This
19088cab4754SWu Fengguang 	 * helps balance scan pressure between file and anonymous pages in
19097c0db9e9SJerome Marchand 	 * get_scan_count.
1910556adecbSRik van Riel 	 */
1911b7c46d15SJohannes Weiner 	reclaim_stat->recent_rotated[file] += nr_rotated;
1912556adecbSRik van Riel 
1913fa9add64SHugh Dickins 	move_active_pages_to_lru(lruvec, &l_active, &l_hold, lru);
1914fa9add64SHugh Dickins 	move_active_pages_to_lru(lruvec, &l_inactive, &l_hold, lru - LRU_ACTIVE);
1915599d0c95SMel Gorman 	__mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken);
1916599d0c95SMel Gorman 	spin_unlock_irq(&pgdat->lru_lock);
19172bcf8879SHugh Dickins 
1918747db954SJohannes Weiner 	mem_cgroup_uncharge_list(&l_hold);
1919b745bc85SMel Gorman 	free_hot_cold_page_list(&l_hold, true);
19201da177e4SLinus Torvalds }
19211da177e4SLinus Torvalds 
192259dc76b0SRik van Riel /*
192359dc76b0SRik van Riel  * The inactive anon list should be small enough that the VM never has
192459dc76b0SRik van Riel  * to do too much work.
192514797e23SKOSAKI Motohiro  *
192659dc76b0SRik van Riel  * The inactive file list should be small enough to leave most memory
192759dc76b0SRik van Riel  * to the established workingset on the scan-resistant active list,
192859dc76b0SRik van Riel  * but large enough to avoid thrashing the aggregate readahead window.
192959dc76b0SRik van Riel  *
193059dc76b0SRik van Riel  * Both inactive lists should also be large enough that each inactive
193159dc76b0SRik van Riel  * page has a chance to be referenced again before it is reclaimed.
193259dc76b0SRik van Riel  *
193359dc76b0SRik van Riel  * The inactive_ratio is the target ratio of ACTIVE to INACTIVE pages
193459dc76b0SRik van Riel  * on this LRU, maintained by the pageout code. A zone->inactive_ratio
193559dc76b0SRik van Riel  * of 3 means 3:1 or 25% of the pages are kept on the inactive list.
193659dc76b0SRik van Riel  *
193759dc76b0SRik van Riel  * total     target    max
193859dc76b0SRik van Riel  * memory    ratio     inactive
193959dc76b0SRik van Riel  * -------------------------------------
194059dc76b0SRik van Riel  *   10MB       1         5MB
194159dc76b0SRik van Riel  *  100MB       1        50MB
194259dc76b0SRik van Riel  *    1GB       3       250MB
194359dc76b0SRik van Riel  *   10GB      10       0.9GB
194459dc76b0SRik van Riel  *  100GB      31         3GB
194559dc76b0SRik van Riel  *    1TB     101        10GB
194659dc76b0SRik van Riel  *   10TB     320        32GB
194714797e23SKOSAKI Motohiro  */
194859dc76b0SRik van Riel static bool inactive_list_is_low(struct lruvec *lruvec, bool file)
194914797e23SKOSAKI Motohiro {
195059dc76b0SRik van Riel 	unsigned long inactive_ratio;
195159dc76b0SRik van Riel 	unsigned long inactive;
195259dc76b0SRik van Riel 	unsigned long active;
195359dc76b0SRik van Riel 	unsigned long gb;
195459dc76b0SRik van Riel 
195574e3f3c3SMinchan Kim 	/*
195674e3f3c3SMinchan Kim 	 * If we don't have swap space, anonymous page deactivation
195774e3f3c3SMinchan Kim 	 * is pointless.
195874e3f3c3SMinchan Kim 	 */
195959dc76b0SRik van Riel 	if (!file && !total_swap_pages)
196042e2e457SYaowei Bai 		return false;
196174e3f3c3SMinchan Kim 
196259dc76b0SRik van Riel 	inactive = lruvec_lru_size(lruvec, file * LRU_FILE);
196359dc76b0SRik van Riel 	active = lruvec_lru_size(lruvec, file * LRU_FILE + LRU_ACTIVE);
1964f16015fbSJohannes Weiner 
196559dc76b0SRik van Riel 	gb = (inactive + active) >> (30 - PAGE_SHIFT);
196659dc76b0SRik van Riel 	if (gb)
196759dc76b0SRik van Riel 		inactive_ratio = int_sqrt(10 * gb);
1968b39415b2SRik van Riel 	else
196959dc76b0SRik van Riel 		inactive_ratio = 1;
197059dc76b0SRik van Riel 
197159dc76b0SRik van Riel 	return inactive * inactive_ratio < active;
1972b39415b2SRik van Riel }
1973b39415b2SRik van Riel 
19744f98a2feSRik van Riel static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
19751a93be0eSKonstantin Khlebnikov 				 struct lruvec *lruvec, struct scan_control *sc)
1976b69408e8SChristoph Lameter {
1977b39415b2SRik van Riel 	if (is_active_lru(lru)) {
197859dc76b0SRik van Riel 		if (inactive_list_is_low(lruvec, is_file_lru(lru)))
19791a93be0eSKonstantin Khlebnikov 			shrink_active_list(nr_to_scan, lruvec, sc, lru);
1980556adecbSRik van Riel 		return 0;
1981556adecbSRik van Riel 	}
1982556adecbSRik van Riel 
19831a93be0eSKonstantin Khlebnikov 	return shrink_inactive_list(nr_to_scan, lruvec, sc, lru);
1984b69408e8SChristoph Lameter }
1985b69408e8SChristoph Lameter 
19869a265114SJohannes Weiner enum scan_balance {
19879a265114SJohannes Weiner 	SCAN_EQUAL,
19889a265114SJohannes Weiner 	SCAN_FRACT,
19899a265114SJohannes Weiner 	SCAN_ANON,
19909a265114SJohannes Weiner 	SCAN_FILE,
19919a265114SJohannes Weiner };
19929a265114SJohannes Weiner 
19931da177e4SLinus Torvalds /*
19944f98a2feSRik van Riel  * Determine how aggressively the anon and file LRU lists should be
19954f98a2feSRik van Riel  * scanned.  The relative value of each set of LRU lists is determined
19964f98a2feSRik van Riel  * by looking at the fraction of the pages scanned we did rotate back
19974f98a2feSRik van Riel  * onto the active list instead of evict.
19984f98a2feSRik van Riel  *
1999be7bd59dSWanpeng Li  * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan
2000be7bd59dSWanpeng Li  * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan
20014f98a2feSRik van Riel  */
200233377678SVladimir Davydov static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg,
20036b4f7799SJohannes Weiner 			   struct scan_control *sc, unsigned long *nr,
20046b4f7799SJohannes Weiner 			   unsigned long *lru_pages)
20054f98a2feSRik van Riel {
200633377678SVladimir Davydov 	int swappiness = mem_cgroup_swappiness(memcg);
200790126375SKonstantin Khlebnikov 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
20089a265114SJohannes Weiner 	u64 fraction[2];
20099a265114SJohannes Weiner 	u64 denominator = 0;	/* gcc */
2010599d0c95SMel Gorman 	struct pglist_data *pgdat = lruvec_pgdat(lruvec);
20119a265114SJohannes Weiner 	unsigned long anon_prio, file_prio;
20129a265114SJohannes Weiner 	enum scan_balance scan_balance;
20130bf1457fSJohannes Weiner 	unsigned long anon, file;
20149a265114SJohannes Weiner 	bool force_scan = false;
20159a265114SJohannes Weiner 	unsigned long ap, fp;
20169a265114SJohannes Weiner 	enum lru_list lru;
20176f04f48dSSuleiman Souhlal 	bool some_scanned;
20186f04f48dSSuleiman Souhlal 	int pass;
2019246e87a9SKAMEZAWA Hiroyuki 
2020f11c0ca5SJohannes Weiner 	/*
2021f11c0ca5SJohannes Weiner 	 * If the zone or memcg is small, nr[l] can be 0.  This
2022f11c0ca5SJohannes Weiner 	 * results in no scanning on this priority and a potential
2023f11c0ca5SJohannes Weiner 	 * priority drop.  Global direct reclaim can go to the next
2024f11c0ca5SJohannes Weiner 	 * zone and tends to have no problems. Global kswapd is for
2025f11c0ca5SJohannes Weiner 	 * zone balancing and it needs to scan a minimum amount. When
2026f11c0ca5SJohannes Weiner 	 * reclaiming for a memcg, a priority drop can cause high
2027f11c0ca5SJohannes Weiner 	 * latencies, so it's better to scan a minimum amount there as
2028f11c0ca5SJohannes Weiner 	 * well.
2029f11c0ca5SJohannes Weiner 	 */
203090cbc250SVladimir Davydov 	if (current_is_kswapd()) {
2031599d0c95SMel Gorman 		if (!pgdat_reclaimable(pgdat))
2032a4d3e9e7SJohannes Weiner 			force_scan = true;
2033eb01aaabSVladimir Davydov 		if (!mem_cgroup_online(memcg))
203490cbc250SVladimir Davydov 			force_scan = true;
203590cbc250SVladimir Davydov 	}
203689b5fae5SJohannes Weiner 	if (!global_reclaim(sc))
2037a4d3e9e7SJohannes Weiner 		force_scan = true;
203876a33fc3SShaohua Li 
203976a33fc3SShaohua Li 	/* If we have no swap space, do not bother scanning anon pages. */
2040d8b38438SVladimir Davydov 	if (!sc->may_swap || mem_cgroup_get_nr_swap_pages(memcg) <= 0) {
20419a265114SJohannes Weiner 		scan_balance = SCAN_FILE;
204276a33fc3SShaohua Li 		goto out;
204376a33fc3SShaohua Li 	}
20444f98a2feSRik van Riel 
204510316b31SJohannes Weiner 	/*
204610316b31SJohannes Weiner 	 * Global reclaim will swap to prevent OOM even with no
204710316b31SJohannes Weiner 	 * swappiness, but memcg users want to use this knob to
204810316b31SJohannes Weiner 	 * disable swapping for individual groups completely when
204910316b31SJohannes Weiner 	 * using the memory controller's swap limit feature would be
205010316b31SJohannes Weiner 	 * too expensive.
205110316b31SJohannes Weiner 	 */
205202695175SJohannes Weiner 	if (!global_reclaim(sc) && !swappiness) {
20539a265114SJohannes Weiner 		scan_balance = SCAN_FILE;
205410316b31SJohannes Weiner 		goto out;
205510316b31SJohannes Weiner 	}
205610316b31SJohannes Weiner 
205710316b31SJohannes Weiner 	/*
205810316b31SJohannes Weiner 	 * Do not apply any pressure balancing cleverness when the
205910316b31SJohannes Weiner 	 * system is close to OOM, scan both anon and file equally
206010316b31SJohannes Weiner 	 * (unless the swappiness setting disagrees with swapping).
206110316b31SJohannes Weiner 	 */
206202695175SJohannes Weiner 	if (!sc->priority && swappiness) {
20639a265114SJohannes Weiner 		scan_balance = SCAN_EQUAL;
206410316b31SJohannes Weiner 		goto out;
206510316b31SJohannes Weiner 	}
206610316b31SJohannes Weiner 
206711d16c25SJohannes Weiner 	/*
206862376251SJohannes Weiner 	 * Prevent the reclaimer from falling into the cache trap: as
206962376251SJohannes Weiner 	 * cache pages start out inactive, every cache fault will tip
207062376251SJohannes Weiner 	 * the scan balance towards the file LRU.  And as the file LRU
207162376251SJohannes Weiner 	 * shrinks, so does the window for rotation from references.
207262376251SJohannes Weiner 	 * This means we have a runaway feedback loop where a tiny
207362376251SJohannes Weiner 	 * thrashing file LRU becomes infinitely more attractive than
207462376251SJohannes Weiner 	 * anon pages.  Try to detect this based on file LRU size.
207562376251SJohannes Weiner 	 */
207662376251SJohannes Weiner 	if (global_reclaim(sc)) {
2077599d0c95SMel Gorman 		unsigned long pgdatfile;
2078599d0c95SMel Gorman 		unsigned long pgdatfree;
2079599d0c95SMel Gorman 		int z;
2080599d0c95SMel Gorman 		unsigned long total_high_wmark = 0;
208162376251SJohannes Weiner 
2082599d0c95SMel Gorman 		pgdatfree = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES);
2083599d0c95SMel Gorman 		pgdatfile = node_page_state(pgdat, NR_ACTIVE_FILE) +
2084599d0c95SMel Gorman 			   node_page_state(pgdat, NR_INACTIVE_FILE);
20852ab051e1SJerome Marchand 
2086599d0c95SMel Gorman 		for (z = 0; z < MAX_NR_ZONES; z++) {
2087599d0c95SMel Gorman 			struct zone *zone = &pgdat->node_zones[z];
2088599d0c95SMel Gorman 			if (!populated_zone(zone))
2089599d0c95SMel Gorman 				continue;
2090599d0c95SMel Gorman 
2091599d0c95SMel Gorman 			total_high_wmark += high_wmark_pages(zone);
2092599d0c95SMel Gorman 		}
2093599d0c95SMel Gorman 
2094599d0c95SMel Gorman 		if (unlikely(pgdatfile + pgdatfree <= total_high_wmark)) {
209562376251SJohannes Weiner 			scan_balance = SCAN_ANON;
209662376251SJohannes Weiner 			goto out;
209762376251SJohannes Weiner 		}
209862376251SJohannes Weiner 	}
209962376251SJohannes Weiner 
210062376251SJohannes Weiner 	/*
2101316bda0eSVladimir Davydov 	 * If there is enough inactive page cache, i.e. if the size of the
2102316bda0eSVladimir Davydov 	 * inactive list is greater than that of the active list *and* the
2103316bda0eSVladimir Davydov 	 * inactive list actually has some pages to scan on this priority, we
2104316bda0eSVladimir Davydov 	 * do not reclaim anything from the anonymous working set right now.
2105316bda0eSVladimir Davydov 	 * Without the second condition we could end up never scanning an
2106316bda0eSVladimir Davydov 	 * lruvec even if it has plenty of old anonymous pages unless the
2107316bda0eSVladimir Davydov 	 * system is under heavy pressure.
2108e9868505SRik van Riel 	 */
210959dc76b0SRik van Riel 	if (!inactive_list_is_low(lruvec, true) &&
211023047a96SJohannes Weiner 	    lruvec_lru_size(lruvec, LRU_INACTIVE_FILE) >> sc->priority) {
21119a265114SJohannes Weiner 		scan_balance = SCAN_FILE;
2112e9868505SRik van Riel 		goto out;
21134f98a2feSRik van Riel 	}
21144f98a2feSRik van Riel 
21159a265114SJohannes Weiner 	scan_balance = SCAN_FRACT;
21169a265114SJohannes Weiner 
21174f98a2feSRik van Riel 	/*
211858c37f6eSKOSAKI Motohiro 	 * With swappiness at 100, anonymous and file have the same priority.
211958c37f6eSKOSAKI Motohiro 	 * This scanning priority is essentially the inverse of IO cost.
212058c37f6eSKOSAKI Motohiro 	 */
212102695175SJohannes Weiner 	anon_prio = swappiness;
212275b00af7SHugh Dickins 	file_prio = 200 - anon_prio;
212358c37f6eSKOSAKI Motohiro 
212458c37f6eSKOSAKI Motohiro 	/*
21254f98a2feSRik van Riel 	 * OK, so we have swap space and a fair amount of page cache
21264f98a2feSRik van Riel 	 * pages.  We use the recently rotated / recently scanned
21274f98a2feSRik van Riel 	 * ratios to determine how valuable each cache is.
21284f98a2feSRik van Riel 	 *
21294f98a2feSRik van Riel 	 * Because workloads change over time (and to avoid overflow)
21304f98a2feSRik van Riel 	 * we keep these statistics as a floating average, which ends
21314f98a2feSRik van Riel 	 * up weighing recent references more than old ones.
21324f98a2feSRik van Riel 	 *
21334f98a2feSRik van Riel 	 * anon in [0], file in [1]
21344f98a2feSRik van Riel 	 */
21352ab051e1SJerome Marchand 
213623047a96SJohannes Weiner 	anon  = lruvec_lru_size(lruvec, LRU_ACTIVE_ANON) +
213723047a96SJohannes Weiner 		lruvec_lru_size(lruvec, LRU_INACTIVE_ANON);
213823047a96SJohannes Weiner 	file  = lruvec_lru_size(lruvec, LRU_ACTIVE_FILE) +
213923047a96SJohannes Weiner 		lruvec_lru_size(lruvec, LRU_INACTIVE_FILE);
21402ab051e1SJerome Marchand 
2141599d0c95SMel Gorman 	spin_lock_irq(&pgdat->lru_lock);
214258c37f6eSKOSAKI Motohiro 	if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) {
21436e901571SKOSAKI Motohiro 		reclaim_stat->recent_scanned[0] /= 2;
21446e901571SKOSAKI Motohiro 		reclaim_stat->recent_rotated[0] /= 2;
21454f98a2feSRik van Riel 	}
21464f98a2feSRik van Riel 
21476e901571SKOSAKI Motohiro 	if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) {
21486e901571SKOSAKI Motohiro 		reclaim_stat->recent_scanned[1] /= 2;
21496e901571SKOSAKI Motohiro 		reclaim_stat->recent_rotated[1] /= 2;
21504f98a2feSRik van Riel 	}
21514f98a2feSRik van Riel 
21524f98a2feSRik van Riel 	/*
215300d8089cSRik van Riel 	 * The amount of pressure on anon vs file pages is inversely
215400d8089cSRik van Riel 	 * proportional to the fraction of recently scanned pages on
215500d8089cSRik van Riel 	 * each list that were recently referenced and in active use.
21564f98a2feSRik van Riel 	 */
2157fe35004fSSatoru Moriya 	ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1);
21586e901571SKOSAKI Motohiro 	ap /= reclaim_stat->recent_rotated[0] + 1;
21594f98a2feSRik van Riel 
2160fe35004fSSatoru Moriya 	fp = file_prio * (reclaim_stat->recent_scanned[1] + 1);
21616e901571SKOSAKI Motohiro 	fp /= reclaim_stat->recent_rotated[1] + 1;
2162599d0c95SMel Gorman 	spin_unlock_irq(&pgdat->lru_lock);
21634f98a2feSRik van Riel 
216476a33fc3SShaohua Li 	fraction[0] = ap;
216576a33fc3SShaohua Li 	fraction[1] = fp;
216676a33fc3SShaohua Li 	denominator = ap + fp + 1;
216776a33fc3SShaohua Li out:
21686f04f48dSSuleiman Souhlal 	some_scanned = false;
21696f04f48dSSuleiman Souhlal 	/* Only use force_scan on second pass. */
21706f04f48dSSuleiman Souhlal 	for (pass = 0; !some_scanned && pass < 2; pass++) {
21716b4f7799SJohannes Weiner 		*lru_pages = 0;
21724111304dSHugh Dickins 		for_each_evictable_lru(lru) {
21734111304dSHugh Dickins 			int file = is_file_lru(lru);
2174d778df51SJohannes Weiner 			unsigned long size;
217576a33fc3SShaohua Li 			unsigned long scan;
217676a33fc3SShaohua Li 
217723047a96SJohannes Weiner 			size = lruvec_lru_size(lruvec, lru);
2178d778df51SJohannes Weiner 			scan = size >> sc->priority;
21799a265114SJohannes Weiner 
21806f04f48dSSuleiman Souhlal 			if (!scan && pass && force_scan)
2181d778df51SJohannes Weiner 				scan = min(size, SWAP_CLUSTER_MAX);
21829a265114SJohannes Weiner 
21839a265114SJohannes Weiner 			switch (scan_balance) {
21849a265114SJohannes Weiner 			case SCAN_EQUAL:
21859a265114SJohannes Weiner 				/* Scan lists relative to size */
21869a265114SJohannes Weiner 				break;
21879a265114SJohannes Weiner 			case SCAN_FRACT:
21889a265114SJohannes Weiner 				/*
21899a265114SJohannes Weiner 				 * Scan types proportional to swappiness and
21909a265114SJohannes Weiner 				 * their relative recent reclaim efficiency.
21919a265114SJohannes Weiner 				 */
21926f04f48dSSuleiman Souhlal 				scan = div64_u64(scan * fraction[file],
21936f04f48dSSuleiman Souhlal 							denominator);
21949a265114SJohannes Weiner 				break;
21959a265114SJohannes Weiner 			case SCAN_FILE:
21969a265114SJohannes Weiner 			case SCAN_ANON:
21979a265114SJohannes Weiner 				/* Scan one type exclusively */
21986b4f7799SJohannes Weiner 				if ((scan_balance == SCAN_FILE) != file) {
21996b4f7799SJohannes Weiner 					size = 0;
22009a265114SJohannes Weiner 					scan = 0;
22016b4f7799SJohannes Weiner 				}
22029a265114SJohannes Weiner 				break;
22039a265114SJohannes Weiner 			default:
22049a265114SJohannes Weiner 				/* Look ma, no brain */
22059a265114SJohannes Weiner 				BUG();
22069a265114SJohannes Weiner 			}
22076b4f7799SJohannes Weiner 
22086b4f7799SJohannes Weiner 			*lru_pages += size;
22094111304dSHugh Dickins 			nr[lru] = scan;
22106b4f7799SJohannes Weiner 
22116f04f48dSSuleiman Souhlal 			/*
22126f04f48dSSuleiman Souhlal 			 * Skip the second pass and don't force_scan,
22136f04f48dSSuleiman Souhlal 			 * if we found something to scan.
22146f04f48dSSuleiman Souhlal 			 */
22156f04f48dSSuleiman Souhlal 			some_scanned |= !!scan;
22166f04f48dSSuleiman Souhlal 		}
221776a33fc3SShaohua Li 	}
22186e08a369SWu Fengguang }
22194f98a2feSRik van Riel 
222072b252aeSMel Gorman #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
222172b252aeSMel Gorman static void init_tlb_ubc(void)
222272b252aeSMel Gorman {
222372b252aeSMel Gorman 	/*
222472b252aeSMel Gorman 	 * This deliberately does not clear the cpumask as it's expensive
222572b252aeSMel Gorman 	 * and unnecessary. If there happens to be data in there then the
222672b252aeSMel Gorman 	 * first SWAP_CLUSTER_MAX pages will send an unnecessary IPI and
222772b252aeSMel Gorman 	 * then will be cleared.
222872b252aeSMel Gorman 	 */
222972b252aeSMel Gorman 	current->tlb_ubc.flush_required = false;
223072b252aeSMel Gorman }
223172b252aeSMel Gorman #else
223272b252aeSMel Gorman static inline void init_tlb_ubc(void)
223372b252aeSMel Gorman {
223472b252aeSMel Gorman }
223572b252aeSMel Gorman #endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */
223672b252aeSMel Gorman 
22379b4f98cdSJohannes Weiner /*
2238a9dd0a83SMel Gorman  * This is a basic per-node page freer.  Used by both kswapd and direct reclaim.
22399b4f98cdSJohannes Weiner  */
2240a9dd0a83SMel Gorman static void shrink_node_memcg(struct pglist_data *pgdat, struct mem_cgroup *memcg,
22416b4f7799SJohannes Weiner 			      struct scan_control *sc, unsigned long *lru_pages)
22429b4f98cdSJohannes Weiner {
2243ef8f2327SMel Gorman 	struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg);
22449b4f98cdSJohannes Weiner 	unsigned long nr[NR_LRU_LISTS];
2245e82e0561SMel Gorman 	unsigned long targets[NR_LRU_LISTS];
22469b4f98cdSJohannes Weiner 	unsigned long nr_to_scan;
22479b4f98cdSJohannes Weiner 	enum lru_list lru;
22489b4f98cdSJohannes Weiner 	unsigned long nr_reclaimed = 0;
22499b4f98cdSJohannes Weiner 	unsigned long nr_to_reclaim = sc->nr_to_reclaim;
22509b4f98cdSJohannes Weiner 	struct blk_plug plug;
22511a501907SMel Gorman 	bool scan_adjusted;
22529b4f98cdSJohannes Weiner 
225333377678SVladimir Davydov 	get_scan_count(lruvec, memcg, sc, nr, lru_pages);
22549b4f98cdSJohannes Weiner 
2255e82e0561SMel Gorman 	/* Record the original scan target for proportional adjustments later */
2256e82e0561SMel Gorman 	memcpy(targets, nr, sizeof(nr));
2257e82e0561SMel Gorman 
22581a501907SMel Gorman 	/*
22591a501907SMel Gorman 	 * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal
22601a501907SMel Gorman 	 * event that can occur when there is little memory pressure e.g.
22611a501907SMel Gorman 	 * multiple streaming readers/writers. Hence, we do not abort scanning
22621a501907SMel Gorman 	 * when the requested number of pages are reclaimed when scanning at
22631a501907SMel Gorman 	 * DEF_PRIORITY on the assumption that the fact we are direct
22641a501907SMel Gorman 	 * reclaiming implies that kswapd is not keeping up and it is best to
22651a501907SMel Gorman 	 * do a batch of work at once. For memcg reclaim one check is made to
22661a501907SMel Gorman 	 * abort proportional reclaim if either the file or anon lru has already
22671a501907SMel Gorman 	 * dropped to zero at the first pass.
22681a501907SMel Gorman 	 */
22691a501907SMel Gorman 	scan_adjusted = (global_reclaim(sc) && !current_is_kswapd() &&
22701a501907SMel Gorman 			 sc->priority == DEF_PRIORITY);
22711a501907SMel Gorman 
227272b252aeSMel Gorman 	init_tlb_ubc();
227372b252aeSMel Gorman 
22749b4f98cdSJohannes Weiner 	blk_start_plug(&plug);
22759b4f98cdSJohannes Weiner 	while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
22769b4f98cdSJohannes Weiner 					nr[LRU_INACTIVE_FILE]) {
2277e82e0561SMel Gorman 		unsigned long nr_anon, nr_file, percentage;
2278e82e0561SMel Gorman 		unsigned long nr_scanned;
2279e82e0561SMel Gorman 
22809b4f98cdSJohannes Weiner 		for_each_evictable_lru(lru) {
22819b4f98cdSJohannes Weiner 			if (nr[lru]) {
22829b4f98cdSJohannes Weiner 				nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX);
22839b4f98cdSJohannes Weiner 				nr[lru] -= nr_to_scan;
22849b4f98cdSJohannes Weiner 
22859b4f98cdSJohannes Weiner 				nr_reclaimed += shrink_list(lru, nr_to_scan,
22869b4f98cdSJohannes Weiner 							    lruvec, sc);
22879b4f98cdSJohannes Weiner 			}
22889b4f98cdSJohannes Weiner 		}
2289e82e0561SMel Gorman 
2290e82e0561SMel Gorman 		if (nr_reclaimed < nr_to_reclaim || scan_adjusted)
2291e82e0561SMel Gorman 			continue;
2292e82e0561SMel Gorman 
22939b4f98cdSJohannes Weiner 		/*
2294e82e0561SMel Gorman 		 * For kswapd and memcg, reclaim at least the number of pages
22951a501907SMel Gorman 		 * requested. Ensure that the anon and file LRUs are scanned
2296e82e0561SMel Gorman 		 * proportionally what was requested by get_scan_count(). We
2297e82e0561SMel Gorman 		 * stop reclaiming one LRU and reduce the amount scanning
2298e82e0561SMel Gorman 		 * proportional to the original scan target.
2299e82e0561SMel Gorman 		 */
2300e82e0561SMel Gorman 		nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE];
2301e82e0561SMel Gorman 		nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON];
2302e82e0561SMel Gorman 
23031a501907SMel Gorman 		/*
23041a501907SMel Gorman 		 * It's just vindictive to attack the larger once the smaller
23051a501907SMel Gorman 		 * has gone to zero.  And given the way we stop scanning the
23061a501907SMel Gorman 		 * smaller below, this makes sure that we only make one nudge
23071a501907SMel Gorman 		 * towards proportionality once we've got nr_to_reclaim.
23081a501907SMel Gorman 		 */
23091a501907SMel Gorman 		if (!nr_file || !nr_anon)
23101a501907SMel Gorman 			break;
23111a501907SMel Gorman 
2312e82e0561SMel Gorman 		if (nr_file > nr_anon) {
2313e82e0561SMel Gorman 			unsigned long scan_target = targets[LRU_INACTIVE_ANON] +
2314e82e0561SMel Gorman 						targets[LRU_ACTIVE_ANON] + 1;
2315e82e0561SMel Gorman 			lru = LRU_BASE;
2316e82e0561SMel Gorman 			percentage = nr_anon * 100 / scan_target;
2317e82e0561SMel Gorman 		} else {
2318e82e0561SMel Gorman 			unsigned long scan_target = targets[LRU_INACTIVE_FILE] +
2319e82e0561SMel Gorman 						targets[LRU_ACTIVE_FILE] + 1;
2320e82e0561SMel Gorman 			lru = LRU_FILE;
2321e82e0561SMel Gorman 			percentage = nr_file * 100 / scan_target;
2322e82e0561SMel Gorman 		}
2323e82e0561SMel Gorman 
2324e82e0561SMel Gorman 		/* Stop scanning the smaller of the LRU */
2325e82e0561SMel Gorman 		nr[lru] = 0;
2326e82e0561SMel Gorman 		nr[lru + LRU_ACTIVE] = 0;
2327e82e0561SMel Gorman 
2328e82e0561SMel Gorman 		/*
2329e82e0561SMel Gorman 		 * Recalculate the other LRU scan count based on its original
2330e82e0561SMel Gorman 		 * scan target and the percentage scanning already complete
2331e82e0561SMel Gorman 		 */
2332e82e0561SMel Gorman 		lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE;
2333e82e0561SMel Gorman 		nr_scanned = targets[lru] - nr[lru];
2334e82e0561SMel Gorman 		nr[lru] = targets[lru] * (100 - percentage) / 100;
2335e82e0561SMel Gorman 		nr[lru] -= min(nr[lru], nr_scanned);
2336e82e0561SMel Gorman 
2337e82e0561SMel Gorman 		lru += LRU_ACTIVE;
2338e82e0561SMel Gorman 		nr_scanned = targets[lru] - nr[lru];
2339e82e0561SMel Gorman 		nr[lru] = targets[lru] * (100 - percentage) / 100;
2340e82e0561SMel Gorman 		nr[lru] -= min(nr[lru], nr_scanned);
2341e82e0561SMel Gorman 
2342e82e0561SMel Gorman 		scan_adjusted = true;
23439b4f98cdSJohannes Weiner 	}
23449b4f98cdSJohannes Weiner 	blk_finish_plug(&plug);
23459b4f98cdSJohannes Weiner 	sc->nr_reclaimed += nr_reclaimed;
23469b4f98cdSJohannes Weiner 
23479b4f98cdSJohannes Weiner 	/*
23489b4f98cdSJohannes Weiner 	 * Even if we did not try to evict anon pages at all, we want to
23499b4f98cdSJohannes Weiner 	 * rebalance the anon lru active/inactive ratio.
23509b4f98cdSJohannes Weiner 	 */
235159dc76b0SRik van Riel 	if (inactive_list_is_low(lruvec, false))
23529b4f98cdSJohannes Weiner 		shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
23539b4f98cdSJohannes Weiner 				   sc, LRU_ACTIVE_ANON);
23549b4f98cdSJohannes Weiner 
23559b4f98cdSJohannes Weiner 	throttle_vm_writeout(sc->gfp_mask);
23569b4f98cdSJohannes Weiner }
23579b4f98cdSJohannes Weiner 
235823b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */
23599e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc)
236023b9da55SMel Gorman {
2361d84da3f9SKirill A. Shutemov 	if (IS_ENABLED(CONFIG_COMPACTION) && sc->order &&
236223b9da55SMel Gorman 			(sc->order > PAGE_ALLOC_COSTLY_ORDER ||
23639e3b2f8cSKonstantin Khlebnikov 			 sc->priority < DEF_PRIORITY - 2))
236423b9da55SMel Gorman 		return true;
236523b9da55SMel Gorman 
236623b9da55SMel Gorman 	return false;
236723b9da55SMel Gorman }
236823b9da55SMel Gorman 
23694f98a2feSRik van Riel /*
237023b9da55SMel Gorman  * Reclaim/compaction is used for high-order allocation requests. It reclaims
237123b9da55SMel Gorman  * order-0 pages before compacting the zone. should_continue_reclaim() returns
237223b9da55SMel Gorman  * true if more pages should be reclaimed such that when the page allocator
237323b9da55SMel Gorman  * calls try_to_compact_zone() that it will have enough free pages to succeed.
237423b9da55SMel Gorman  * It will give up earlier than that if there is difficulty reclaiming pages.
23753e7d3449SMel Gorman  */
2376a9dd0a83SMel Gorman static inline bool should_continue_reclaim(struct pglist_data *pgdat,
23773e7d3449SMel Gorman 					unsigned long nr_reclaimed,
23783e7d3449SMel Gorman 					unsigned long nr_scanned,
23793e7d3449SMel Gorman 					struct scan_control *sc)
23803e7d3449SMel Gorman {
23813e7d3449SMel Gorman 	unsigned long pages_for_compaction;
23823e7d3449SMel Gorman 	unsigned long inactive_lru_pages;
2383a9dd0a83SMel Gorman 	int z;
23843e7d3449SMel Gorman 
23853e7d3449SMel Gorman 	/* If not in reclaim/compaction mode, stop */
23869e3b2f8cSKonstantin Khlebnikov 	if (!in_reclaim_compaction(sc))
23873e7d3449SMel Gorman 		return false;
23883e7d3449SMel Gorman 
23892876592fSMel Gorman 	/* Consider stopping depending on scan and reclaim activity */
23902876592fSMel Gorman 	if (sc->gfp_mask & __GFP_REPEAT) {
23913e7d3449SMel Gorman 		/*
23922876592fSMel Gorman 		 * For __GFP_REPEAT allocations, stop reclaiming if the
23932876592fSMel Gorman 		 * full LRU list has been scanned and we are still failing
23942876592fSMel Gorman 		 * to reclaim pages. This full LRU scan is potentially
23952876592fSMel Gorman 		 * expensive but a __GFP_REPEAT caller really wants to succeed
23963e7d3449SMel Gorman 		 */
23973e7d3449SMel Gorman 		if (!nr_reclaimed && !nr_scanned)
23983e7d3449SMel Gorman 			return false;
23992876592fSMel Gorman 	} else {
24002876592fSMel Gorman 		/*
24012876592fSMel Gorman 		 * For non-__GFP_REPEAT allocations which can presumably
24022876592fSMel Gorman 		 * fail without consequence, stop if we failed to reclaim
24032876592fSMel Gorman 		 * any pages from the last SWAP_CLUSTER_MAX number of
24042876592fSMel Gorman 		 * pages that were scanned. This will return to the
24052876592fSMel Gorman 		 * caller faster at the risk reclaim/compaction and
24062876592fSMel Gorman 		 * the resulting allocation attempt fails
24072876592fSMel Gorman 		 */
24082876592fSMel Gorman 		if (!nr_reclaimed)
24092876592fSMel Gorman 			return false;
24102876592fSMel Gorman 	}
24113e7d3449SMel Gorman 
24123e7d3449SMel Gorman 	/*
24133e7d3449SMel Gorman 	 * If we have not reclaimed enough pages for compaction and the
24143e7d3449SMel Gorman 	 * inactive lists are large enough, continue reclaiming
24153e7d3449SMel Gorman 	 */
24163e7d3449SMel Gorman 	pages_for_compaction = (2UL << sc->order);
2417a9dd0a83SMel Gorman 	inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE);
2418ec8acf20SShaohua Li 	if (get_nr_swap_pages() > 0)
2419a9dd0a83SMel Gorman 		inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON);
24203e7d3449SMel Gorman 	if (sc->nr_reclaimed < pages_for_compaction &&
24213e7d3449SMel Gorman 			inactive_lru_pages > pages_for_compaction)
24223e7d3449SMel Gorman 		return true;
24233e7d3449SMel Gorman 
24243e7d3449SMel Gorman 	/* If compaction would go ahead or the allocation would succeed, stop */
2425a9dd0a83SMel Gorman 	for (z = 0; z <= sc->reclaim_idx; z++) {
2426a9dd0a83SMel Gorman 		struct zone *zone = &pgdat->node_zones[z];
2427a9dd0a83SMel Gorman 		if (!populated_zone(zone))
2428a9dd0a83SMel Gorman 			continue;
2429a9dd0a83SMel Gorman 
2430a9dd0a83SMel Gorman 		switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) {
24313e7d3449SMel Gorman 		case COMPACT_PARTIAL:
24323e7d3449SMel Gorman 		case COMPACT_CONTINUE:
24333e7d3449SMel Gorman 			return false;
24343e7d3449SMel Gorman 		default:
2435a9dd0a83SMel Gorman 			/* check next zone */
2436a9dd0a83SMel Gorman 			;
24373e7d3449SMel Gorman 		}
24383e7d3449SMel Gorman 	}
2439a9dd0a83SMel Gorman 	return true;
2440a9dd0a83SMel Gorman }
24413e7d3449SMel Gorman 
2442970a39a3SMel Gorman static bool shrink_node(pg_data_t *pgdat, struct scan_control *sc)
2443f16015fbSJohannes Weiner {
2444cb731d6cSVladimir Davydov 	struct reclaim_state *reclaim_state = current->reclaim_state;
24459b4f98cdSJohannes Weiner 	unsigned long nr_reclaimed, nr_scanned;
24462344d7e4SJohannes Weiner 	bool reclaimable = false;
24479b4f98cdSJohannes Weiner 
24489b4f98cdSJohannes Weiner 	do {
24495660048cSJohannes Weiner 		struct mem_cgroup *root = sc->target_mem_cgroup;
24505660048cSJohannes Weiner 		struct mem_cgroup_reclaim_cookie reclaim = {
2451ef8f2327SMel Gorman 			.pgdat = pgdat,
24529e3b2f8cSKonstantin Khlebnikov 			.priority = sc->priority,
24535660048cSJohannes Weiner 		};
2454a9dd0a83SMel Gorman 		unsigned long node_lru_pages = 0;
2455694fbc0fSAndrew Morton 		struct mem_cgroup *memcg;
24565660048cSJohannes Weiner 
24579b4f98cdSJohannes Weiner 		nr_reclaimed = sc->nr_reclaimed;
24589b4f98cdSJohannes Weiner 		nr_scanned = sc->nr_scanned;
24599b4f98cdSJohannes Weiner 
2460694fbc0fSAndrew Morton 		memcg = mem_cgroup_iter(root, NULL, &reclaim);
2461694fbc0fSAndrew Morton 		do {
24626b4f7799SJohannes Weiner 			unsigned long lru_pages;
24638e8ae645SJohannes Weiner 			unsigned long reclaimed;
2464cb731d6cSVladimir Davydov 			unsigned long scanned;
24659b4f98cdSJohannes Weiner 
2466241994edSJohannes Weiner 			if (mem_cgroup_low(root, memcg)) {
2467241994edSJohannes Weiner 				if (!sc->may_thrash)
2468241994edSJohannes Weiner 					continue;
2469241994edSJohannes Weiner 				mem_cgroup_events(memcg, MEMCG_LOW, 1);
2470241994edSJohannes Weiner 			}
2471241994edSJohannes Weiner 
24728e8ae645SJohannes Weiner 			reclaimed = sc->nr_reclaimed;
2473cb731d6cSVladimir Davydov 			scanned = sc->nr_scanned;
24745660048cSJohannes Weiner 
2475a9dd0a83SMel Gorman 			shrink_node_memcg(pgdat, memcg, sc, &lru_pages);
2476a9dd0a83SMel Gorman 			node_lru_pages += lru_pages;
2477f9be23d6SKonstantin Khlebnikov 
2478b2e18757SMel Gorman 			if (!global_reclaim(sc))
2479a9dd0a83SMel Gorman 				shrink_slab(sc->gfp_mask, pgdat->node_id,
2480cb731d6cSVladimir Davydov 					    memcg, sc->nr_scanned - scanned,
2481cb731d6cSVladimir Davydov 					    lru_pages);
2482cb731d6cSVladimir Davydov 
24838e8ae645SJohannes Weiner 			/* Record the group's reclaim efficiency */
24848e8ae645SJohannes Weiner 			vmpressure(sc->gfp_mask, memcg, false,
24858e8ae645SJohannes Weiner 				   sc->nr_scanned - scanned,
24868e8ae645SJohannes Weiner 				   sc->nr_reclaimed - reclaimed);
24878e8ae645SJohannes Weiner 
24885660048cSJohannes Weiner 			/*
2489a394cb8eSMichal Hocko 			 * Direct reclaim and kswapd have to scan all memory
2490a394cb8eSMichal Hocko 			 * cgroups to fulfill the overall scan target for the
2491a9dd0a83SMel Gorman 			 * node.
2492a394cb8eSMichal Hocko 			 *
2493a394cb8eSMichal Hocko 			 * Limit reclaim, on the other hand, only cares about
2494a394cb8eSMichal Hocko 			 * nr_to_reclaim pages to be reclaimed and it will
2495a394cb8eSMichal Hocko 			 * retry with decreasing priority if one round over the
2496a394cb8eSMichal Hocko 			 * whole hierarchy is not sufficient.
24975660048cSJohannes Weiner 			 */
2498a394cb8eSMichal Hocko 			if (!global_reclaim(sc) &&
2499a394cb8eSMichal Hocko 					sc->nr_reclaimed >= sc->nr_to_reclaim) {
25005660048cSJohannes Weiner 				mem_cgroup_iter_break(root, memcg);
25015660048cSJohannes Weiner 				break;
25025660048cSJohannes Weiner 			}
2503241994edSJohannes Weiner 		} while ((memcg = mem_cgroup_iter(root, memcg, &reclaim)));
250470ddf637SAnton Vorontsov 
25056b4f7799SJohannes Weiner 		/*
25066b4f7799SJohannes Weiner 		 * Shrink the slab caches in the same proportion that
25076b4f7799SJohannes Weiner 		 * the eligible LRU pages were scanned.
25086b4f7799SJohannes Weiner 		 */
2509b2e18757SMel Gorman 		if (global_reclaim(sc))
2510a9dd0a83SMel Gorman 			shrink_slab(sc->gfp_mask, pgdat->node_id, NULL,
25116b4f7799SJohannes Weiner 				    sc->nr_scanned - nr_scanned,
2512a9dd0a83SMel Gorman 				    node_lru_pages);
25136b4f7799SJohannes Weiner 
25146b4f7799SJohannes Weiner 		if (reclaim_state) {
2515cb731d6cSVladimir Davydov 			sc->nr_reclaimed += reclaim_state->reclaimed_slab;
25166b4f7799SJohannes Weiner 			reclaim_state->reclaimed_slab = 0;
25176b4f7799SJohannes Weiner 		}
25186b4f7799SJohannes Weiner 
25198e8ae645SJohannes Weiner 		/* Record the subtree's reclaim efficiency */
25208e8ae645SJohannes Weiner 		vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true,
252170ddf637SAnton Vorontsov 			   sc->nr_scanned - nr_scanned,
252270ddf637SAnton Vorontsov 			   sc->nr_reclaimed - nr_reclaimed);
252370ddf637SAnton Vorontsov 
25242344d7e4SJohannes Weiner 		if (sc->nr_reclaimed - nr_reclaimed)
25252344d7e4SJohannes Weiner 			reclaimable = true;
25262344d7e4SJohannes Weiner 
2527a9dd0a83SMel Gorman 	} while (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed,
25289b4f98cdSJohannes Weiner 					 sc->nr_scanned - nr_scanned, sc));
25292344d7e4SJohannes Weiner 
25302344d7e4SJohannes Weiner 	return reclaimable;
2531f16015fbSJohannes Weiner }
2532f16015fbSJohannes Weiner 
253353853e2dSVlastimil Babka /*
253453853e2dSVlastimil Babka  * Returns true if compaction should go ahead for a high-order request, or
253553853e2dSVlastimil Babka  * the high-order allocation would succeed without compaction.
253653853e2dSVlastimil Babka  */
25374f588331SMel Gorman static inline bool compaction_ready(struct zone *zone, struct scan_control *sc)
2538fe4b1b24SMel Gorman {
253931483b6aSMel Gorman 	unsigned long watermark;
2540fe4b1b24SMel Gorman 	bool watermark_ok;
2541fe4b1b24SMel Gorman 
2542fe4b1b24SMel Gorman 	/*
2543fe4b1b24SMel Gorman 	 * Compaction takes time to run and there are potentially other
2544fe4b1b24SMel Gorman 	 * callers using the pages just freed. Continue reclaiming until
2545fe4b1b24SMel Gorman 	 * there is a buffer of free pages available to give compaction
2546fe4b1b24SMel Gorman 	 * a reasonable chance of completing and allocating the page
2547fe4b1b24SMel Gorman 	 */
25484f588331SMel Gorman 	watermark = high_wmark_pages(zone) + (2UL << sc->order);
25494f588331SMel Gorman 	watermark_ok = zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx);
2550fe4b1b24SMel Gorman 
2551fe4b1b24SMel Gorman 	/*
2552fe4b1b24SMel Gorman 	 * If compaction is deferred, reclaim up to a point where
2553fe4b1b24SMel Gorman 	 * compaction will have a chance of success when re-enabled
2554fe4b1b24SMel Gorman 	 */
25554f588331SMel Gorman 	if (compaction_deferred(zone, sc->order))
2556fe4b1b24SMel Gorman 		return watermark_ok;
2557fe4b1b24SMel Gorman 
255853853e2dSVlastimil Babka 	/*
255953853e2dSVlastimil Babka 	 * If compaction is not ready to start and allocation is not likely
256053853e2dSVlastimil Babka 	 * to succeed without it, then keep reclaiming.
256153853e2dSVlastimil Babka 	 */
25624f588331SMel Gorman 	if (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx) == COMPACT_SKIPPED)
2563fe4b1b24SMel Gorman 		return false;
2564fe4b1b24SMel Gorman 
2565fe4b1b24SMel Gorman 	return watermark_ok;
2566fe4b1b24SMel Gorman }
2567fe4b1b24SMel Gorman 
25681da177e4SLinus Torvalds /*
25691da177e4SLinus Torvalds  * This is the direct reclaim path, for page-allocating processes.  We only
25701da177e4SLinus Torvalds  * try to reclaim pages from zones which will satisfy the caller's allocation
25711da177e4SLinus Torvalds  * request.
25721da177e4SLinus Torvalds  *
25731da177e4SLinus Torvalds  * If a zone is deemed to be full of pinned pages then just give it a light
25741da177e4SLinus Torvalds  * scan then give up on it.
25751da177e4SLinus Torvalds  */
25760a0337e0SMichal Hocko static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc)
25771da177e4SLinus Torvalds {
2578dd1a239fSMel Gorman 	struct zoneref *z;
257954a6eb5cSMel Gorman 	struct zone *zone;
25800608f43dSAndrew Morton 	unsigned long nr_soft_reclaimed;
25810608f43dSAndrew Morton 	unsigned long nr_soft_scanned;
2582619d0d76SWeijie Yang 	gfp_t orig_mask;
258379dafcdcSMel Gorman 	pg_data_t *last_pgdat = NULL;
25841cfb419bSKAMEZAWA Hiroyuki 
2585cc715d99SMel Gorman 	/*
2586cc715d99SMel Gorman 	 * If the number of buffer_heads in the machine exceeds the maximum
2587cc715d99SMel Gorman 	 * allowed level, force direct reclaim to scan the highmem zone as
2588cc715d99SMel Gorman 	 * highmem pages could be pinning lowmem pages storing buffer_heads
2589cc715d99SMel Gorman 	 */
2590619d0d76SWeijie Yang 	orig_mask = sc->gfp_mask;
2591b2e18757SMel Gorman 	if (buffer_heads_over_limit) {
2592cc715d99SMel Gorman 		sc->gfp_mask |= __GFP_HIGHMEM;
25934f588331SMel Gorman 		sc->reclaim_idx = gfp_zone(sc->gfp_mask);
2594b2e18757SMel Gorman 	}
2595cc715d99SMel Gorman 
2596d4debc66SMel Gorman 	for_each_zone_zonelist_nodemask(zone, z, zonelist,
2597b2e18757SMel Gorman 					sc->reclaim_idx, sc->nodemask) {
2598f3fe6512SCon Kolivas 		if (!populated_zone(zone))
25991da177e4SLinus Torvalds 			continue;
26006b4f7799SJohannes Weiner 
2601b2e18757SMel Gorman 		/*
26021cfb419bSKAMEZAWA Hiroyuki 		 * Take care memory controller reclaiming has small influence
26031cfb419bSKAMEZAWA Hiroyuki 		 * to global LRU.
26041cfb419bSKAMEZAWA Hiroyuki 		 */
260589b5fae5SJohannes Weiner 		if (global_reclaim(sc)) {
2606344736f2SVladimir Davydov 			if (!cpuset_zone_allowed(zone,
2607344736f2SVladimir Davydov 						 GFP_KERNEL | __GFP_HARDWALL))
26081da177e4SLinus Torvalds 				continue;
260965ec02cbSVladimir Davydov 
26106e543d57SLisa Du 			if (sc->priority != DEF_PRIORITY &&
2611599d0c95SMel Gorman 			    !pgdat_reclaimable(zone->zone_pgdat))
26121da177e4SLinus Torvalds 				continue;	/* Let kswapd poll it */
26130b06496aSJohannes Weiner 
2614e0887c19SRik van Riel 			/*
2615e0c23279SMel Gorman 			 * If we already have plenty of memory free for
2616e0c23279SMel Gorman 			 * compaction in this zone, don't free any more.
2617e0c23279SMel Gorman 			 * Even though compaction is invoked for any
2618e0c23279SMel Gorman 			 * non-zero order, only frequent costly order
2619e0c23279SMel Gorman 			 * reclamation is disruptive enough to become a
2620c7cfa37bSCopot Alexandru 			 * noticeable problem, like transparent huge
2621c7cfa37bSCopot Alexandru 			 * page allocations.
2622e0887c19SRik van Riel 			 */
26230b06496aSJohannes Weiner 			if (IS_ENABLED(CONFIG_COMPACTION) &&
26240b06496aSJohannes Weiner 			    sc->order > PAGE_ALLOC_COSTLY_ORDER &&
26254f588331SMel Gorman 			    compaction_ready(zone, sc)) {
26260b06496aSJohannes Weiner 				sc->compaction_ready = true;
2627e0887c19SRik van Riel 				continue;
2628e0887c19SRik van Riel 			}
26290b06496aSJohannes Weiner 
26300608f43dSAndrew Morton 			/*
263179dafcdcSMel Gorman 			 * Shrink each node in the zonelist once. If the
263279dafcdcSMel Gorman 			 * zonelist is ordered by zone (not the default) then a
263379dafcdcSMel Gorman 			 * node may be shrunk multiple times but in that case
263479dafcdcSMel Gorman 			 * the user prefers lower zones being preserved.
263579dafcdcSMel Gorman 			 */
263679dafcdcSMel Gorman 			if (zone->zone_pgdat == last_pgdat)
263779dafcdcSMel Gorman 				continue;
263879dafcdcSMel Gorman 
263979dafcdcSMel Gorman 			/*
26400608f43dSAndrew Morton 			 * This steals pages from memory cgroups over softlimit
26410608f43dSAndrew Morton 			 * and returns the number of reclaimed pages and
26420608f43dSAndrew Morton 			 * scanned pages. This works for global memory pressure
26430608f43dSAndrew Morton 			 * and balancing, not for a memcg's limit.
26440608f43dSAndrew Morton 			 */
26450608f43dSAndrew Morton 			nr_soft_scanned = 0;
2646ef8f2327SMel Gorman 			nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat,
26470608f43dSAndrew Morton 						sc->order, sc->gfp_mask,
26480608f43dSAndrew Morton 						&nr_soft_scanned);
26490608f43dSAndrew Morton 			sc->nr_reclaimed += nr_soft_reclaimed;
26500608f43dSAndrew Morton 			sc->nr_scanned += nr_soft_scanned;
2651ac34a1a3SKAMEZAWA Hiroyuki 			/* need some check for avoid more shrink_zone() */
2652ac34a1a3SKAMEZAWA Hiroyuki 		}
2653d149e3b2SYing Han 
265479dafcdcSMel Gorman 		/* See comment about same check for global reclaim above */
265579dafcdcSMel Gorman 		if (zone->zone_pgdat == last_pgdat)
265679dafcdcSMel Gorman 			continue;
265779dafcdcSMel Gorman 		last_pgdat = zone->zone_pgdat;
2658970a39a3SMel Gorman 		shrink_node(zone->zone_pgdat, sc);
26591da177e4SLinus Torvalds 	}
2660e0c23279SMel Gorman 
266165ec02cbSVladimir Davydov 	/*
2662619d0d76SWeijie Yang 	 * Restore to original mask to avoid the impact on the caller if we
2663619d0d76SWeijie Yang 	 * promoted it to __GFP_HIGHMEM.
2664619d0d76SWeijie Yang 	 */
2665619d0d76SWeijie Yang 	sc->gfp_mask = orig_mask;
26661da177e4SLinus Torvalds }
26671da177e4SLinus Torvalds 
26681da177e4SLinus Torvalds /*
26691da177e4SLinus Torvalds  * This is the main entry point to direct page reclaim.
26701da177e4SLinus Torvalds  *
26711da177e4SLinus Torvalds  * If a full scan of the inactive list fails to free enough memory then we
26721da177e4SLinus Torvalds  * are "out of memory" and something needs to be killed.
26731da177e4SLinus Torvalds  *
26741da177e4SLinus Torvalds  * If the caller is !__GFP_FS then the probability of a failure is reasonably
26751da177e4SLinus Torvalds  * high - the zone may be full of dirty or under-writeback pages, which this
26765b0830cbSJens Axboe  * caller can't do much about.  We kick the writeback threads and take explicit
26775b0830cbSJens Axboe  * naps in the hope that some of these pages can be written.  But if the
26785b0830cbSJens Axboe  * allocating task holds filesystem locks which prevent writeout this might not
26795b0830cbSJens Axboe  * work, and the allocation attempt will fail.
2680a41f24eaSNishanth Aravamudan  *
2681a41f24eaSNishanth Aravamudan  * returns:	0, if no pages reclaimed
2682a41f24eaSNishanth Aravamudan  * 		else, the number of pages reclaimed
26831da177e4SLinus Torvalds  */
2684dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
26853115cd91SVladimir Davydov 					  struct scan_control *sc)
26861da177e4SLinus Torvalds {
2687241994edSJohannes Weiner 	int initial_priority = sc->priority;
268869e05944SAndrew Morton 	unsigned long total_scanned = 0;
268922fba335SKOSAKI Motohiro 	unsigned long writeback_threshold;
2690241994edSJohannes Weiner retry:
2691873b4771SKeika Kobayashi 	delayacct_freepages_start();
2692873b4771SKeika Kobayashi 
269389b5fae5SJohannes Weiner 	if (global_reclaim(sc))
2694*7cc30fcfSMel Gorman 		__count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1);
26951da177e4SLinus Torvalds 
26969e3b2f8cSKonstantin Khlebnikov 	do {
269770ddf637SAnton Vorontsov 		vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup,
269870ddf637SAnton Vorontsov 				sc->priority);
269966e1707bSBalbir Singh 		sc->nr_scanned = 0;
27000a0337e0SMichal Hocko 		shrink_zones(zonelist, sc);
2701e0c23279SMel Gorman 
270266e1707bSBalbir Singh 		total_scanned += sc->nr_scanned;
2703bb21c7ceSKOSAKI Motohiro 		if (sc->nr_reclaimed >= sc->nr_to_reclaim)
27040b06496aSJohannes Weiner 			break;
27050b06496aSJohannes Weiner 
27060b06496aSJohannes Weiner 		if (sc->compaction_ready)
27070b06496aSJohannes Weiner 			break;
27081da177e4SLinus Torvalds 
27091da177e4SLinus Torvalds 		/*
27100e50ce3bSMinchan Kim 		 * If we're getting trouble reclaiming, start doing
27110e50ce3bSMinchan Kim 		 * writepage even in laptop mode.
27120e50ce3bSMinchan Kim 		 */
27130e50ce3bSMinchan Kim 		if (sc->priority < DEF_PRIORITY - 2)
27140e50ce3bSMinchan Kim 			sc->may_writepage = 1;
27150e50ce3bSMinchan Kim 
27160e50ce3bSMinchan Kim 		/*
27171da177e4SLinus Torvalds 		 * Try to write back as many pages as we just scanned.  This
27181da177e4SLinus Torvalds 		 * tends to cause slow streaming writers to write data to the
27191da177e4SLinus Torvalds 		 * disk smoothly, at the dirtying rate, which is nice.   But
27201da177e4SLinus Torvalds 		 * that's undesirable in laptop mode, where we *want* lumpy
27211da177e4SLinus Torvalds 		 * writeout.  So in laptop mode, write out the whole world.
27221da177e4SLinus Torvalds 		 */
272322fba335SKOSAKI Motohiro 		writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2;
272422fba335SKOSAKI Motohiro 		if (total_scanned > writeback_threshold) {
27250e175a18SCurt Wohlgemuth 			wakeup_flusher_threads(laptop_mode ? 0 : total_scanned,
27260e175a18SCurt Wohlgemuth 						WB_REASON_TRY_TO_FREE_PAGES);
272766e1707bSBalbir Singh 			sc->may_writepage = 1;
27281da177e4SLinus Torvalds 		}
27290b06496aSJohannes Weiner 	} while (--sc->priority >= 0);
2730bb21c7ceSKOSAKI Motohiro 
2731873b4771SKeika Kobayashi 	delayacct_freepages_end();
2732873b4771SKeika Kobayashi 
2733bb21c7ceSKOSAKI Motohiro 	if (sc->nr_reclaimed)
2734bb21c7ceSKOSAKI Motohiro 		return sc->nr_reclaimed;
2735bb21c7ceSKOSAKI Motohiro 
27360cee34fdSMel Gorman 	/* Aborted reclaim to try compaction? don't OOM, then */
27370b06496aSJohannes Weiner 	if (sc->compaction_ready)
27387335084dSMel Gorman 		return 1;
27397335084dSMel Gorman 
2740241994edSJohannes Weiner 	/* Untapped cgroup reserves?  Don't OOM, retry. */
2741241994edSJohannes Weiner 	if (!sc->may_thrash) {
2742241994edSJohannes Weiner 		sc->priority = initial_priority;
2743241994edSJohannes Weiner 		sc->may_thrash = 1;
2744241994edSJohannes Weiner 		goto retry;
2745241994edSJohannes Weiner 	}
2746241994edSJohannes Weiner 
2747bb21c7ceSKOSAKI Motohiro 	return 0;
27481da177e4SLinus Torvalds }
27491da177e4SLinus Torvalds 
27505515061dSMel Gorman static bool pfmemalloc_watermark_ok(pg_data_t *pgdat)
27515515061dSMel Gorman {
27525515061dSMel Gorman 	struct zone *zone;
27535515061dSMel Gorman 	unsigned long pfmemalloc_reserve = 0;
27545515061dSMel Gorman 	unsigned long free_pages = 0;
27555515061dSMel Gorman 	int i;
27565515061dSMel Gorman 	bool wmark_ok;
27575515061dSMel Gorman 
27585515061dSMel Gorman 	for (i = 0; i <= ZONE_NORMAL; i++) {
27595515061dSMel Gorman 		zone = &pgdat->node_zones[i];
2760f012a84aSNishanth Aravamudan 		if (!populated_zone(zone) ||
2761599d0c95SMel Gorman 		    pgdat_reclaimable_pages(pgdat) == 0)
2762675becceSMel Gorman 			continue;
2763675becceSMel Gorman 
27645515061dSMel Gorman 		pfmemalloc_reserve += min_wmark_pages(zone);
27655515061dSMel Gorman 		free_pages += zone_page_state(zone, NR_FREE_PAGES);
27665515061dSMel Gorman 	}
27675515061dSMel Gorman 
2768675becceSMel Gorman 	/* If there are no reserves (unexpected config) then do not throttle */
2769675becceSMel Gorman 	if (!pfmemalloc_reserve)
2770675becceSMel Gorman 		return true;
2771675becceSMel Gorman 
27725515061dSMel Gorman 	wmark_ok = free_pages > pfmemalloc_reserve / 2;
27735515061dSMel Gorman 
27745515061dSMel Gorman 	/* kswapd must be awake if processes are being throttled */
27755515061dSMel Gorman 	if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) {
277638087d9bSMel Gorman 		pgdat->kswapd_classzone_idx = min(pgdat->kswapd_classzone_idx,
27775515061dSMel Gorman 						(enum zone_type)ZONE_NORMAL);
27785515061dSMel Gorman 		wake_up_interruptible(&pgdat->kswapd_wait);
27795515061dSMel Gorman 	}
27805515061dSMel Gorman 
27815515061dSMel Gorman 	return wmark_ok;
27825515061dSMel Gorman }
27835515061dSMel Gorman 
27845515061dSMel Gorman /*
27855515061dSMel Gorman  * Throttle direct reclaimers if backing storage is backed by the network
27865515061dSMel Gorman  * and the PFMEMALLOC reserve for the preferred node is getting dangerously
27875515061dSMel Gorman  * depleted. kswapd will continue to make progress and wake the processes
278850694c28SMel Gorman  * when the low watermark is reached.
278950694c28SMel Gorman  *
279050694c28SMel Gorman  * Returns true if a fatal signal was delivered during throttling. If this
279150694c28SMel Gorman  * happens, the page allocator should not consider triggering the OOM killer.
27925515061dSMel Gorman  */
279350694c28SMel Gorman static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist,
27945515061dSMel Gorman 					nodemask_t *nodemask)
27955515061dSMel Gorman {
2796675becceSMel Gorman 	struct zoneref *z;
27975515061dSMel Gorman 	struct zone *zone;
2798675becceSMel Gorman 	pg_data_t *pgdat = NULL;
27995515061dSMel Gorman 
28005515061dSMel Gorman 	/*
28015515061dSMel Gorman 	 * Kernel threads should not be throttled as they may be indirectly
28025515061dSMel Gorman 	 * responsible for cleaning pages necessary for reclaim to make forward
28035515061dSMel Gorman 	 * progress. kjournald for example may enter direct reclaim while
28045515061dSMel Gorman 	 * committing a transaction where throttling it could forcing other
28055515061dSMel Gorman 	 * processes to block on log_wait_commit().
28065515061dSMel Gorman 	 */
28075515061dSMel Gorman 	if (current->flags & PF_KTHREAD)
280850694c28SMel Gorman 		goto out;
280950694c28SMel Gorman 
281050694c28SMel Gorman 	/*
281150694c28SMel Gorman 	 * If a fatal signal is pending, this process should not throttle.
281250694c28SMel Gorman 	 * It should return quickly so it can exit and free its memory
281350694c28SMel Gorman 	 */
281450694c28SMel Gorman 	if (fatal_signal_pending(current))
281550694c28SMel Gorman 		goto out;
28165515061dSMel Gorman 
2817675becceSMel Gorman 	/*
2818675becceSMel Gorman 	 * Check if the pfmemalloc reserves are ok by finding the first node
2819675becceSMel Gorman 	 * with a usable ZONE_NORMAL or lower zone. The expectation is that
2820675becceSMel Gorman 	 * GFP_KERNEL will be required for allocating network buffers when
2821675becceSMel Gorman 	 * swapping over the network so ZONE_HIGHMEM is unusable.
2822675becceSMel Gorman 	 *
2823675becceSMel Gorman 	 * Throttling is based on the first usable node and throttled processes
2824675becceSMel Gorman 	 * wait on a queue until kswapd makes progress and wakes them. There
2825675becceSMel Gorman 	 * is an affinity then between processes waking up and where reclaim
2826675becceSMel Gorman 	 * progress has been made assuming the process wakes on the same node.
2827675becceSMel Gorman 	 * More importantly, processes running on remote nodes will not compete
2828675becceSMel Gorman 	 * for remote pfmemalloc reserves and processes on different nodes
2829675becceSMel Gorman 	 * should make reasonable progress.
2830675becceSMel Gorman 	 */
2831675becceSMel Gorman 	for_each_zone_zonelist_nodemask(zone, z, zonelist,
283217636faaSMichael S. Tsirkin 					gfp_zone(gfp_mask), nodemask) {
2833675becceSMel Gorman 		if (zone_idx(zone) > ZONE_NORMAL)
2834675becceSMel Gorman 			continue;
2835675becceSMel Gorman 
2836675becceSMel Gorman 		/* Throttle based on the first usable node */
28375515061dSMel Gorman 		pgdat = zone->zone_pgdat;
28385515061dSMel Gorman 		if (pfmemalloc_watermark_ok(pgdat))
283950694c28SMel Gorman 			goto out;
2840675becceSMel Gorman 		break;
2841675becceSMel Gorman 	}
2842675becceSMel Gorman 
2843675becceSMel Gorman 	/* If no zone was usable by the allocation flags then do not throttle */
2844675becceSMel Gorman 	if (!pgdat)
2845675becceSMel Gorman 		goto out;
28465515061dSMel Gorman 
284768243e76SMel Gorman 	/* Account for the throttling */
284868243e76SMel Gorman 	count_vm_event(PGSCAN_DIRECT_THROTTLE);
284968243e76SMel Gorman 
28505515061dSMel Gorman 	/*
28515515061dSMel Gorman 	 * If the caller cannot enter the filesystem, it's possible that it
28525515061dSMel Gorman 	 * is due to the caller holding an FS lock or performing a journal
28535515061dSMel Gorman 	 * transaction in the case of a filesystem like ext[3|4]. In this case,
28545515061dSMel Gorman 	 * it is not safe to block on pfmemalloc_wait as kswapd could be
28555515061dSMel Gorman 	 * blocked waiting on the same lock. Instead, throttle for up to a
28565515061dSMel Gorman 	 * second before continuing.
28575515061dSMel Gorman 	 */
28585515061dSMel Gorman 	if (!(gfp_mask & __GFP_FS)) {
28595515061dSMel Gorman 		wait_event_interruptible_timeout(pgdat->pfmemalloc_wait,
28605515061dSMel Gorman 			pfmemalloc_watermark_ok(pgdat), HZ);
286150694c28SMel Gorman 
286250694c28SMel Gorman 		goto check_pending;
28635515061dSMel Gorman 	}
28645515061dSMel Gorman 
28655515061dSMel Gorman 	/* Throttle until kswapd wakes the process */
28665515061dSMel Gorman 	wait_event_killable(zone->zone_pgdat->pfmemalloc_wait,
28675515061dSMel Gorman 		pfmemalloc_watermark_ok(pgdat));
286850694c28SMel Gorman 
286950694c28SMel Gorman check_pending:
287050694c28SMel Gorman 	if (fatal_signal_pending(current))
287150694c28SMel Gorman 		return true;
287250694c28SMel Gorman 
287350694c28SMel Gorman out:
287450694c28SMel Gorman 	return false;
28755515061dSMel Gorman }
28765515061dSMel Gorman 
2877dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
2878327c0e96SKAMEZAWA Hiroyuki 				gfp_t gfp_mask, nodemask_t *nodemask)
287966e1707bSBalbir Singh {
288033906bc5SMel Gorman 	unsigned long nr_reclaimed;
288166e1707bSBalbir Singh 	struct scan_control sc = {
288222fba335SKOSAKI Motohiro 		.nr_to_reclaim = SWAP_CLUSTER_MAX,
2883ee814fe2SJohannes Weiner 		.gfp_mask = (gfp_mask = memalloc_noio_flags(gfp_mask)),
2884b2e18757SMel Gorman 		.reclaim_idx = gfp_zone(gfp_mask),
2885ee814fe2SJohannes Weiner 		.order = order,
2886ee814fe2SJohannes Weiner 		.nodemask = nodemask,
2887ee814fe2SJohannes Weiner 		.priority = DEF_PRIORITY,
2888ee814fe2SJohannes Weiner 		.may_writepage = !laptop_mode,
2889a6dc60f8SJohannes Weiner 		.may_unmap = 1,
28902e2e4259SKOSAKI Motohiro 		.may_swap = 1,
289166e1707bSBalbir Singh 	};
289266e1707bSBalbir Singh 
28935515061dSMel Gorman 	/*
289450694c28SMel Gorman 	 * Do not enter reclaim if fatal signal was delivered while throttled.
289550694c28SMel Gorman 	 * 1 is returned so that the page allocator does not OOM kill at this
289650694c28SMel Gorman 	 * point.
28975515061dSMel Gorman 	 */
289850694c28SMel Gorman 	if (throttle_direct_reclaim(gfp_mask, zonelist, nodemask))
28995515061dSMel Gorman 		return 1;
29005515061dSMel Gorman 
290133906bc5SMel Gorman 	trace_mm_vmscan_direct_reclaim_begin(order,
290233906bc5SMel Gorman 				sc.may_writepage,
2903e5146b12SMel Gorman 				gfp_mask,
2904e5146b12SMel Gorman 				sc.reclaim_idx);
290533906bc5SMel Gorman 
29063115cd91SVladimir Davydov 	nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
290733906bc5SMel Gorman 
290833906bc5SMel Gorman 	trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
290933906bc5SMel Gorman 
291033906bc5SMel Gorman 	return nr_reclaimed;
291166e1707bSBalbir Singh }
291266e1707bSBalbir Singh 
2913c255a458SAndrew Morton #ifdef CONFIG_MEMCG
291466e1707bSBalbir Singh 
2915a9dd0a83SMel Gorman unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg,
29164e416953SBalbir Singh 						gfp_t gfp_mask, bool noswap,
2917ef8f2327SMel Gorman 						pg_data_t *pgdat,
29180ae5e89cSYing Han 						unsigned long *nr_scanned)
29194e416953SBalbir Singh {
29204e416953SBalbir Singh 	struct scan_control sc = {
2921b8f5c566SKOSAKI Motohiro 		.nr_to_reclaim = SWAP_CLUSTER_MAX,
2922ee814fe2SJohannes Weiner 		.target_mem_cgroup = memcg,
29234e416953SBalbir Singh 		.may_writepage = !laptop_mode,
29244e416953SBalbir Singh 		.may_unmap = 1,
2925b2e18757SMel Gorman 		.reclaim_idx = MAX_NR_ZONES - 1,
29264e416953SBalbir Singh 		.may_swap = !noswap,
29274e416953SBalbir Singh 	};
29286b4f7799SJohannes Weiner 	unsigned long lru_pages;
29290ae5e89cSYing Han 
29304e416953SBalbir Singh 	sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
29314e416953SBalbir Singh 			(GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
2932bdce6d9eSKOSAKI Motohiro 
29339e3b2f8cSKonstantin Khlebnikov 	trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order,
2934bdce6d9eSKOSAKI Motohiro 						      sc.may_writepage,
2935e5146b12SMel Gorman 						      sc.gfp_mask,
2936e5146b12SMel Gorman 						      sc.reclaim_idx);
2937bdce6d9eSKOSAKI Motohiro 
29384e416953SBalbir Singh 	/*
29394e416953SBalbir Singh 	 * NOTE: Although we can get the priority field, using it
29404e416953SBalbir Singh 	 * here is not a good idea, since it limits the pages we can scan.
2941a9dd0a83SMel Gorman 	 * if we don't reclaim here, the shrink_node from balance_pgdat
29424e416953SBalbir Singh 	 * will pick up pages from other mem cgroup's as well. We hack
29434e416953SBalbir Singh 	 * the priority and make it zero.
29444e416953SBalbir Singh 	 */
2945ef8f2327SMel Gorman 	shrink_node_memcg(pgdat, memcg, &sc, &lru_pages);
2946bdce6d9eSKOSAKI Motohiro 
2947bdce6d9eSKOSAKI Motohiro 	trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed);
2948bdce6d9eSKOSAKI Motohiro 
29490ae5e89cSYing Han 	*nr_scanned = sc.nr_scanned;
29504e416953SBalbir Singh 	return sc.nr_reclaimed;
29514e416953SBalbir Singh }
29524e416953SBalbir Singh 
295372835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
2954b70a2a21SJohannes Weiner 					   unsigned long nr_pages,
29558c7c6e34SKAMEZAWA Hiroyuki 					   gfp_t gfp_mask,
2956b70a2a21SJohannes Weiner 					   bool may_swap)
295766e1707bSBalbir Singh {
29584e416953SBalbir Singh 	struct zonelist *zonelist;
2959bdce6d9eSKOSAKI Motohiro 	unsigned long nr_reclaimed;
2960889976dbSYing Han 	int nid;
296166e1707bSBalbir Singh 	struct scan_control sc = {
2962b70a2a21SJohannes Weiner 		.nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
2963ee814fe2SJohannes Weiner 		.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
2964ee814fe2SJohannes Weiner 				(GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK),
2965b2e18757SMel Gorman 		.reclaim_idx = MAX_NR_ZONES - 1,
2966ee814fe2SJohannes Weiner 		.target_mem_cgroup = memcg,
2967ee814fe2SJohannes Weiner 		.priority = DEF_PRIORITY,
296866e1707bSBalbir Singh 		.may_writepage = !laptop_mode,
2969a6dc60f8SJohannes Weiner 		.may_unmap = 1,
2970b70a2a21SJohannes Weiner 		.may_swap = may_swap,
2971a09ed5e0SYing Han 	};
297266e1707bSBalbir Singh 
2973889976dbSYing Han 	/*
2974889976dbSYing Han 	 * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't
2975889976dbSYing Han 	 * take care of from where we get pages. So the node where we start the
2976889976dbSYing Han 	 * scan does not need to be the current node.
2977889976dbSYing Han 	 */
297872835c86SJohannes Weiner 	nid = mem_cgroup_select_victim_node(memcg);
2979889976dbSYing Han 
2980889976dbSYing Han 	zonelist = NODE_DATA(nid)->node_zonelists;
2981bdce6d9eSKOSAKI Motohiro 
2982bdce6d9eSKOSAKI Motohiro 	trace_mm_vmscan_memcg_reclaim_begin(0,
2983bdce6d9eSKOSAKI Motohiro 					    sc.may_writepage,
2984e5146b12SMel Gorman 					    sc.gfp_mask,
2985e5146b12SMel Gorman 					    sc.reclaim_idx);
2986bdce6d9eSKOSAKI Motohiro 
29873115cd91SVladimir Davydov 	nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
2988bdce6d9eSKOSAKI Motohiro 
2989bdce6d9eSKOSAKI Motohiro 	trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
2990bdce6d9eSKOSAKI Motohiro 
2991bdce6d9eSKOSAKI Motohiro 	return nr_reclaimed;
299266e1707bSBalbir Singh }
299366e1707bSBalbir Singh #endif
299466e1707bSBalbir Singh 
29951d82de61SMel Gorman static void age_active_anon(struct pglist_data *pgdat,
2996ef8f2327SMel Gorman 				struct scan_control *sc)
2997f16015fbSJohannes Weiner {
2998b95a2f2dSJohannes Weiner 	struct mem_cgroup *memcg;
2999b95a2f2dSJohannes Weiner 
3000b95a2f2dSJohannes Weiner 	if (!total_swap_pages)
3001b95a2f2dSJohannes Weiner 		return;
3002b95a2f2dSJohannes Weiner 
3003b95a2f2dSJohannes Weiner 	memcg = mem_cgroup_iter(NULL, NULL, NULL);
3004b95a2f2dSJohannes Weiner 	do {
3005ef8f2327SMel Gorman 		struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg);
3006f16015fbSJohannes Weiner 
300759dc76b0SRik van Riel 		if (inactive_list_is_low(lruvec, false))
30081a93be0eSKonstantin Khlebnikov 			shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
30099e3b2f8cSKonstantin Khlebnikov 					   sc, LRU_ACTIVE_ANON);
3010b95a2f2dSJohannes Weiner 
3011b95a2f2dSJohannes Weiner 		memcg = mem_cgroup_iter(NULL, memcg, NULL);
3012b95a2f2dSJohannes Weiner 	} while (memcg);
3013f16015fbSJohannes Weiner }
3014f16015fbSJohannes Weiner 
301531483b6aSMel Gorman static bool zone_balanced(struct zone *zone, int order, int classzone_idx)
301660cefed4SJohannes Weiner {
301731483b6aSMel Gorman 	unsigned long mark = high_wmark_pages(zone);
301860cefed4SJohannes Weiner 
30196256c6b4SMel Gorman 	if (!zone_watermark_ok_safe(zone, order, mark, classzone_idx))
30206256c6b4SMel Gorman 		return false;
30216256c6b4SMel Gorman 
30226256c6b4SMel Gorman 	/*
30236256c6b4SMel Gorman 	 * If any eligible zone is balanced then the node is not considered
30246256c6b4SMel Gorman 	 * to be congested or dirty
30256256c6b4SMel Gorman 	 */
30266256c6b4SMel Gorman 	clear_bit(PGDAT_CONGESTED, &zone->zone_pgdat->flags);
30276256c6b4SMel Gorman 	clear_bit(PGDAT_DIRTY, &zone->zone_pgdat->flags);
30286256c6b4SMel Gorman 
30296256c6b4SMel Gorman 	return true;
303060cefed4SJohannes Weiner }
303160cefed4SJohannes Weiner 
30321741c877SMel Gorman /*
30335515061dSMel Gorman  * Prepare kswapd for sleeping. This verifies that there are no processes
30345515061dSMel Gorman  * waiting in throttle_direct_reclaim() and that watermarks have been met.
30355515061dSMel Gorman  *
30365515061dSMel Gorman  * Returns true if kswapd is ready to sleep
30375515061dSMel Gorman  */
3038d9f21d42SMel Gorman static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, int classzone_idx)
3039f50de2d3SMel Gorman {
30401d82de61SMel Gorman 	int i;
30411d82de61SMel Gorman 
30425515061dSMel Gorman 	/*
30439e5e3661SVlastimil Babka 	 * The throttled processes are normally woken up in balance_pgdat() as
30449e5e3661SVlastimil Babka 	 * soon as pfmemalloc_watermark_ok() is true. But there is a potential
30459e5e3661SVlastimil Babka 	 * race between when kswapd checks the watermarks and a process gets
30469e5e3661SVlastimil Babka 	 * throttled. There is also a potential race if processes get
30479e5e3661SVlastimil Babka 	 * throttled, kswapd wakes, a large process exits thereby balancing the
30489e5e3661SVlastimil Babka 	 * zones, which causes kswapd to exit balance_pgdat() before reaching
30499e5e3661SVlastimil Babka 	 * the wake up checks. If kswapd is going to sleep, no process should
30509e5e3661SVlastimil Babka 	 * be sleeping on pfmemalloc_wait, so wake them now if necessary. If
30519e5e3661SVlastimil Babka 	 * the wake up is premature, processes will wake kswapd and get
30529e5e3661SVlastimil Babka 	 * throttled again. The difference from wake ups in balance_pgdat() is
30539e5e3661SVlastimil Babka 	 * that here we are under prepare_to_wait().
30545515061dSMel Gorman 	 */
30559e5e3661SVlastimil Babka 	if (waitqueue_active(&pgdat->pfmemalloc_wait))
30569e5e3661SVlastimil Babka 		wake_up_all(&pgdat->pfmemalloc_wait);
3057f50de2d3SMel Gorman 
30581d82de61SMel Gorman 	for (i = 0; i <= classzone_idx; i++) {
30591d82de61SMel Gorman 		struct zone *zone = pgdat->node_zones + i;
30601d82de61SMel Gorman 
30611d82de61SMel Gorman 		if (!populated_zone(zone))
30621d82de61SMel Gorman 			continue;
30631d82de61SMel Gorman 
306438087d9bSMel Gorman 		if (!zone_balanced(zone, order, classzone_idx))
306538087d9bSMel Gorman 			return false;
30661d82de61SMel Gorman 	}
30671d82de61SMel Gorman 
306838087d9bSMel Gorman 	return true;
3069f50de2d3SMel Gorman }
3070f50de2d3SMel Gorman 
30711da177e4SLinus Torvalds /*
30721d82de61SMel Gorman  * kswapd shrinks a node of pages that are at or below the highest usable
30731d82de61SMel Gorman  * zone that is currently unbalanced.
3074b8e83b94SMel Gorman  *
3075b8e83b94SMel Gorman  * Returns true if kswapd scanned at least the requested number of pages to
3076283aba9fSMel Gorman  * reclaim or if the lack of progress was due to pages under writeback.
3077283aba9fSMel Gorman  * This is used to determine if the scanning priority needs to be raised.
307875485363SMel Gorman  */
30791d82de61SMel Gorman static bool kswapd_shrink_node(pg_data_t *pgdat,
3080accf6242SVlastimil Babka 			       struct scan_control *sc)
308175485363SMel Gorman {
30821d82de61SMel Gorman 	struct zone *zone;
30831d82de61SMel Gorman 	int z;
308475485363SMel Gorman 
30851d82de61SMel Gorman 	/* Reclaim a number of pages proportional to the number of zones */
30861d82de61SMel Gorman 	sc->nr_to_reclaim = 0;
3087970a39a3SMel Gorman 	for (z = 0; z <= sc->reclaim_idx; z++) {
30881d82de61SMel Gorman 		zone = pgdat->node_zones + z;
30891d82de61SMel Gorman 		if (!populated_zone(zone))
30901d82de61SMel Gorman 			continue;
30917c954f6dSMel Gorman 
30921d82de61SMel Gorman 		sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX);
30937c954f6dSMel Gorman 	}
30947c954f6dSMel Gorman 
30951d82de61SMel Gorman 	/*
30961d82de61SMel Gorman 	 * Historically care was taken to put equal pressure on all zones but
30971d82de61SMel Gorman 	 * now pressure is applied based on node LRU order.
30981d82de61SMel Gorman 	 */
3099970a39a3SMel Gorman 	shrink_node(pgdat, sc);
31001d82de61SMel Gorman 
31011d82de61SMel Gorman 	/*
31021d82de61SMel Gorman 	 * Fragmentation may mean that the system cannot be rebalanced for
31031d82de61SMel Gorman 	 * high-order allocations. If twice the allocation size has been
31041d82de61SMel Gorman 	 * reclaimed then recheck watermarks only at order-0 to prevent
31051d82de61SMel Gorman 	 * excessive reclaim. Assume that a process requested a high-order
31061d82de61SMel Gorman 	 * can direct reclaim/compact.
31071d82de61SMel Gorman 	 */
31081d82de61SMel Gorman 	if (sc->order && sc->nr_reclaimed >= 2UL << sc->order)
31091d82de61SMel Gorman 		sc->order = 0;
31101d82de61SMel Gorman 
3111b8e83b94SMel Gorman 	return sc->nr_scanned >= sc->nr_to_reclaim;
311275485363SMel Gorman }
311375485363SMel Gorman 
311475485363SMel Gorman /*
31151d82de61SMel Gorman  * For kswapd, balance_pgdat() will reclaim pages across a node from zones
31161d82de61SMel Gorman  * that are eligible for use by the caller until at least one zone is
31171d82de61SMel Gorman  * balanced.
31181da177e4SLinus Torvalds  *
31191d82de61SMel Gorman  * Returns the order kswapd finished reclaiming at.
31201da177e4SLinus Torvalds  *
31211da177e4SLinus Torvalds  * kswapd scans the zones in the highmem->normal->dma direction.  It skips
312241858966SMel Gorman  * zones which have free_pages > high_wmark_pages(zone), but once a zone is
31231d82de61SMel Gorman  * found to have free_pages <= high_wmark_pages(zone), any page is that zone
31241d82de61SMel Gorman  * or lower is eligible for reclaim until at least one usable zone is
31251d82de61SMel Gorman  * balanced.
31261da177e4SLinus Torvalds  */
3127accf6242SVlastimil Babka static int balance_pgdat(pg_data_t *pgdat, int order, int classzone_idx)
31281da177e4SLinus Torvalds {
31291da177e4SLinus Torvalds 	int i;
31300608f43dSAndrew Morton 	unsigned long nr_soft_reclaimed;
31310608f43dSAndrew Morton 	unsigned long nr_soft_scanned;
31321d82de61SMel Gorman 	struct zone *zone;
3133179e9639SAndrew Morton 	struct scan_control sc = {
3134179e9639SAndrew Morton 		.gfp_mask = GFP_KERNEL,
3135ee814fe2SJohannes Weiner 		.order = order,
3136b8e83b94SMel Gorman 		.priority = DEF_PRIORITY,
3137ee814fe2SJohannes Weiner 		.may_writepage = !laptop_mode,
3138a6dc60f8SJohannes Weiner 		.may_unmap = 1,
31392e2e4259SKOSAKI Motohiro 		.may_swap = 1,
3140179e9639SAndrew Morton 	};
3141f8891e5eSChristoph Lameter 	count_vm_event(PAGEOUTRUN);
31421da177e4SLinus Torvalds 
31439e3b2f8cSKonstantin Khlebnikov 	do {
3144b8e83b94SMel Gorman 		bool raise_priority = true;
3145b8e83b94SMel Gorman 
3146b8e83b94SMel Gorman 		sc.nr_reclaimed = 0;
314784c7a777SMel Gorman 		sc.reclaim_idx = classzone_idx;
31481da177e4SLinus Torvalds 
314986c79f6bSMel Gorman 		/*
315084c7a777SMel Gorman 		 * If the number of buffer_heads exceeds the maximum allowed
315184c7a777SMel Gorman 		 * then consider reclaiming from all zones. This has a dual
315284c7a777SMel Gorman 		 * purpose -- on 64-bit systems it is expected that
315384c7a777SMel Gorman 		 * buffer_heads are stripped during active rotation. On 32-bit
315484c7a777SMel Gorman 		 * systems, highmem pages can pin lowmem memory and shrinking
315584c7a777SMel Gorman 		 * buffers can relieve lowmem pressure. Reclaim may still not
315684c7a777SMel Gorman 		 * go ahead if all eligible zones for the original allocation
315784c7a777SMel Gorman 		 * request are balanced to avoid excessive reclaim from kswapd.
315886c79f6bSMel Gorman 		 */
315986c79f6bSMel Gorman 		if (buffer_heads_over_limit) {
316086c79f6bSMel Gorman 			for (i = MAX_NR_ZONES - 1; i >= 0; i--) {
316186c79f6bSMel Gorman 				zone = pgdat->node_zones + i;
316286c79f6bSMel Gorman 				if (!populated_zone(zone))
316386c79f6bSMel Gorman 					continue;
316486c79f6bSMel Gorman 
3165970a39a3SMel Gorman 				sc.reclaim_idx = i;
316686c79f6bSMel Gorman 				break;
316786c79f6bSMel Gorman 			}
316886c79f6bSMel Gorman 		}
316986c79f6bSMel Gorman 
317086c79f6bSMel Gorman 		/*
317186c79f6bSMel Gorman 		 * Only reclaim if there are no eligible zones. Check from
317286c79f6bSMel Gorman 		 * high to low zone as allocations prefer higher zones.
317386c79f6bSMel Gorman 		 * Scanning from low to high zone would allow congestion to be
317486c79f6bSMel Gorman 		 * cleared during a very small window when a small low
317586c79f6bSMel Gorman 		 * zone was balanced even under extreme pressure when the
317684c7a777SMel Gorman 		 * overall node may be congested. Note that sc.reclaim_idx
317784c7a777SMel Gorman 		 * is not used as buffer_heads_over_limit may have adjusted
317884c7a777SMel Gorman 		 * it.
317986c79f6bSMel Gorman 		 */
318084c7a777SMel Gorman 		for (i = classzone_idx; i >= 0; i--) {
31811d82de61SMel Gorman 			zone = pgdat->node_zones + i;
3182f3fe6512SCon Kolivas 			if (!populated_zone(zone))
31831da177e4SLinus Torvalds 				continue;
31841da177e4SLinus Torvalds 
318584c7a777SMel Gorman 			if (zone_balanced(zone, sc.order, classzone_idx))
31861da177e4SLinus Torvalds 				goto out;
318786c79f6bSMel Gorman 		}
3188e1dbeda6SAndrew Morton 
31891da177e4SLinus Torvalds 		/*
31901d82de61SMel Gorman 		 * Do some background aging of the anon list, to give
31911d82de61SMel Gorman 		 * pages a chance to be referenced before reclaiming. All
31921d82de61SMel Gorman 		 * pages are rotated regardless of classzone as this is
31931d82de61SMel Gorman 		 * about consistent aging.
31941d82de61SMel Gorman 		 */
3195ef8f2327SMel Gorman 		age_active_anon(pgdat, &sc);
31961d82de61SMel Gorman 
31971d82de61SMel Gorman 		/*
3198b7ea3c41SMel Gorman 		 * If we're getting trouble reclaiming, start doing writepage
3199b7ea3c41SMel Gorman 		 * even in laptop mode.
3200b7ea3c41SMel Gorman 		 */
32011d82de61SMel Gorman 		if (sc.priority < DEF_PRIORITY - 2 || !pgdat_reclaimable(pgdat))
3202b7ea3c41SMel Gorman 			sc.may_writepage = 1;
3203b7ea3c41SMel Gorman 
32041d82de61SMel Gorman 		/* Call soft limit reclaim before calling shrink_node. */
32051da177e4SLinus Torvalds 		sc.nr_scanned = 0;
32060608f43dSAndrew Morton 		nr_soft_scanned = 0;
3207ef8f2327SMel Gorman 		nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order,
32081d82de61SMel Gorman 						sc.gfp_mask, &nr_soft_scanned);
32090608f43dSAndrew Morton 		sc.nr_reclaimed += nr_soft_reclaimed;
32100608f43dSAndrew Morton 
321132a4330dSRik van Riel 		/*
32121d82de61SMel Gorman 		 * There should be no need to raise the scanning priority if
32131d82de61SMel Gorman 		 * enough pages are already being scanned that that high
32141d82de61SMel Gorman 		 * watermark would be met at 100% efficiency.
321532a4330dSRik van Riel 		 */
3216970a39a3SMel Gorman 		if (kswapd_shrink_node(pgdat, &sc))
3217b8e83b94SMel Gorman 			raise_priority = false;
3218d7868daeSMel Gorman 
32195515061dSMel Gorman 		/*
32205515061dSMel Gorman 		 * If the low watermark is met there is no need for processes
32215515061dSMel Gorman 		 * to be throttled on pfmemalloc_wait as they should not be
32225515061dSMel Gorman 		 * able to safely make forward progress. Wake them
32235515061dSMel Gorman 		 */
32245515061dSMel Gorman 		if (waitqueue_active(&pgdat->pfmemalloc_wait) &&
32255515061dSMel Gorman 				pfmemalloc_watermark_ok(pgdat))
3226cfc51155SVlastimil Babka 			wake_up_all(&pgdat->pfmemalloc_wait);
32275515061dSMel Gorman 
3228b8e83b94SMel Gorman 		/* Check if kswapd should be suspending */
3229b8e83b94SMel Gorman 		if (try_to_freeze() || kthread_should_stop())
3230b8e83b94SMel Gorman 			break;
3231b8e83b94SMel Gorman 
3232b8e83b94SMel Gorman 		/*
3233b8e83b94SMel Gorman 		 * Raise priority if scanning rate is too low or there was no
3234b8e83b94SMel Gorman 		 * progress in reclaiming pages
3235b8e83b94SMel Gorman 		 */
3236b8e83b94SMel Gorman 		if (raise_priority || !sc.nr_reclaimed)
3237b8e83b94SMel Gorman 			sc.priority--;
32381d82de61SMel Gorman 	} while (sc.priority >= 1);
32391da177e4SLinus Torvalds 
3240b8e83b94SMel Gorman out:
32410abdee2bSMel Gorman 	/*
32421d82de61SMel Gorman 	 * Return the order kswapd stopped reclaiming at as
32431d82de61SMel Gorman 	 * prepare_kswapd_sleep() takes it into account. If another caller
32441d82de61SMel Gorman 	 * entered the allocator slow path while kswapd was awake, order will
32451d82de61SMel Gorman 	 * remain at the higher level.
32460abdee2bSMel Gorman 	 */
32471d82de61SMel Gorman 	return sc.order;
32481da177e4SLinus Torvalds }
32491da177e4SLinus Torvalds 
325038087d9bSMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order,
325138087d9bSMel Gorman 				unsigned int classzone_idx)
3252f0bc0a60SKOSAKI Motohiro {
3253f0bc0a60SKOSAKI Motohiro 	long remaining = 0;
3254f0bc0a60SKOSAKI Motohiro 	DEFINE_WAIT(wait);
3255f0bc0a60SKOSAKI Motohiro 
3256f0bc0a60SKOSAKI Motohiro 	if (freezing(current) || kthread_should_stop())
3257f0bc0a60SKOSAKI Motohiro 		return;
3258f0bc0a60SKOSAKI Motohiro 
3259f0bc0a60SKOSAKI Motohiro 	prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
3260f0bc0a60SKOSAKI Motohiro 
3261f0bc0a60SKOSAKI Motohiro 	/* Try to sleep for a short interval */
3262d9f21d42SMel Gorman 	if (prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) {
3263fd901c95SVlastimil Babka 		/*
3264fd901c95SVlastimil Babka 		 * Compaction records what page blocks it recently failed to
3265fd901c95SVlastimil Babka 		 * isolate pages from and skips them in the future scanning.
3266fd901c95SVlastimil Babka 		 * When kswapd is going to sleep, it is reasonable to assume
3267fd901c95SVlastimil Babka 		 * that pages and compaction may succeed so reset the cache.
3268fd901c95SVlastimil Babka 		 */
3269fd901c95SVlastimil Babka 		reset_isolation_suitable(pgdat);
3270fd901c95SVlastimil Babka 
3271fd901c95SVlastimil Babka 		/*
3272fd901c95SVlastimil Babka 		 * We have freed the memory, now we should compact it to make
3273fd901c95SVlastimil Babka 		 * allocation of the requested order possible.
3274fd901c95SVlastimil Babka 		 */
327538087d9bSMel Gorman 		wakeup_kcompactd(pgdat, alloc_order, classzone_idx);
3276fd901c95SVlastimil Babka 
3277f0bc0a60SKOSAKI Motohiro 		remaining = schedule_timeout(HZ/10);
327838087d9bSMel Gorman 
327938087d9bSMel Gorman 		/*
328038087d9bSMel Gorman 		 * If woken prematurely then reset kswapd_classzone_idx and
328138087d9bSMel Gorman 		 * order. The values will either be from a wakeup request or
328238087d9bSMel Gorman 		 * the previous request that slept prematurely.
328338087d9bSMel Gorman 		 */
328438087d9bSMel Gorman 		if (remaining) {
328538087d9bSMel Gorman 			pgdat->kswapd_classzone_idx = max(pgdat->kswapd_classzone_idx, classzone_idx);
328638087d9bSMel Gorman 			pgdat->kswapd_order = max(pgdat->kswapd_order, reclaim_order);
328738087d9bSMel Gorman 		}
328838087d9bSMel Gorman 
3289f0bc0a60SKOSAKI Motohiro 		finish_wait(&pgdat->kswapd_wait, &wait);
3290f0bc0a60SKOSAKI Motohiro 		prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
3291f0bc0a60SKOSAKI Motohiro 	}
3292f0bc0a60SKOSAKI Motohiro 
3293f0bc0a60SKOSAKI Motohiro 	/*
3294f0bc0a60SKOSAKI Motohiro 	 * After a short sleep, check if it was a premature sleep. If not, then
3295f0bc0a60SKOSAKI Motohiro 	 * go fully to sleep until explicitly woken up.
3296f0bc0a60SKOSAKI Motohiro 	 */
3297d9f21d42SMel Gorman 	if (!remaining &&
3298d9f21d42SMel Gorman 	    prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) {
3299f0bc0a60SKOSAKI Motohiro 		trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
3300f0bc0a60SKOSAKI Motohiro 
3301f0bc0a60SKOSAKI Motohiro 		/*
3302f0bc0a60SKOSAKI Motohiro 		 * vmstat counters are not perfectly accurate and the estimated
3303f0bc0a60SKOSAKI Motohiro 		 * value for counters such as NR_FREE_PAGES can deviate from the
3304f0bc0a60SKOSAKI Motohiro 		 * true value by nr_online_cpus * threshold. To avoid the zone
3305f0bc0a60SKOSAKI Motohiro 		 * watermarks being breached while under pressure, we reduce the
3306f0bc0a60SKOSAKI Motohiro 		 * per-cpu vmstat threshold while kswapd is awake and restore
3307f0bc0a60SKOSAKI Motohiro 		 * them before going back to sleep.
3308f0bc0a60SKOSAKI Motohiro 		 */
3309f0bc0a60SKOSAKI Motohiro 		set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold);
33101c7e7f6cSAaditya Kumar 
33111c7e7f6cSAaditya Kumar 		if (!kthread_should_stop())
3312f0bc0a60SKOSAKI Motohiro 			schedule();
33131c7e7f6cSAaditya Kumar 
3314f0bc0a60SKOSAKI Motohiro 		set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold);
3315f0bc0a60SKOSAKI Motohiro 	} else {
3316f0bc0a60SKOSAKI Motohiro 		if (remaining)
3317f0bc0a60SKOSAKI Motohiro 			count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY);
3318f0bc0a60SKOSAKI Motohiro 		else
3319f0bc0a60SKOSAKI Motohiro 			count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY);
3320f0bc0a60SKOSAKI Motohiro 	}
3321f0bc0a60SKOSAKI Motohiro 	finish_wait(&pgdat->kswapd_wait, &wait);
3322f0bc0a60SKOSAKI Motohiro }
3323f0bc0a60SKOSAKI Motohiro 
33241da177e4SLinus Torvalds /*
33251da177e4SLinus Torvalds  * The background pageout daemon, started as a kernel thread
33261da177e4SLinus Torvalds  * from the init process.
33271da177e4SLinus Torvalds  *
33281da177e4SLinus Torvalds  * This basically trickles out pages so that we have _some_
33291da177e4SLinus Torvalds  * free memory available even if there is no other activity
33301da177e4SLinus Torvalds  * that frees anything up. This is needed for things like routing
33311da177e4SLinus Torvalds  * etc, where we otherwise might have all activity going on in
33321da177e4SLinus Torvalds  * asynchronous contexts that cannot page things out.
33331da177e4SLinus Torvalds  *
33341da177e4SLinus Torvalds  * If there are applications that are active memory-allocators
33351da177e4SLinus Torvalds  * (most normal use), this basically shouldn't matter.
33361da177e4SLinus Torvalds  */
33371da177e4SLinus Torvalds static int kswapd(void *p)
33381da177e4SLinus Torvalds {
333938087d9bSMel Gorman 	unsigned int alloc_order, reclaim_order, classzone_idx;
33401da177e4SLinus Torvalds 	pg_data_t *pgdat = (pg_data_t*)p;
33411da177e4SLinus Torvalds 	struct task_struct *tsk = current;
3342f0bc0a60SKOSAKI Motohiro 
33431da177e4SLinus Torvalds 	struct reclaim_state reclaim_state = {
33441da177e4SLinus Torvalds 		.reclaimed_slab = 0,
33451da177e4SLinus Torvalds 	};
3346a70f7302SRusty Russell 	const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
33471da177e4SLinus Torvalds 
3348cf40bd16SNick Piggin 	lockdep_set_current_reclaim_state(GFP_KERNEL);
3349cf40bd16SNick Piggin 
3350174596a0SRusty Russell 	if (!cpumask_empty(cpumask))
3351c5f59f08SMike Travis 		set_cpus_allowed_ptr(tsk, cpumask);
33521da177e4SLinus Torvalds 	current->reclaim_state = &reclaim_state;
33531da177e4SLinus Torvalds 
33541da177e4SLinus Torvalds 	/*
33551da177e4SLinus Torvalds 	 * Tell the memory management that we're a "memory allocator",
33561da177e4SLinus Torvalds 	 * and that if we need more memory we should get access to it
33571da177e4SLinus Torvalds 	 * regardless (see "__alloc_pages()"). "kswapd" should
33581da177e4SLinus Torvalds 	 * never get caught in the normal page freeing logic.
33591da177e4SLinus Torvalds 	 *
33601da177e4SLinus Torvalds 	 * (Kswapd normally doesn't need memory anyway, but sometimes
33611da177e4SLinus Torvalds 	 * you need a small amount of memory in order to be able to
33621da177e4SLinus Torvalds 	 * page out something else, and this flag essentially protects
33631da177e4SLinus Torvalds 	 * us from recursively trying to free more memory as we're
33641da177e4SLinus Torvalds 	 * trying to free the first piece of memory in the first place).
33651da177e4SLinus Torvalds 	 */
3366930d9152SChristoph Lameter 	tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
336783144186SRafael J. Wysocki 	set_freezable();
33681da177e4SLinus Torvalds 
336938087d9bSMel Gorman 	pgdat->kswapd_order = alloc_order = reclaim_order = 0;
337038087d9bSMel Gorman 	pgdat->kswapd_classzone_idx = classzone_idx = 0;
33711da177e4SLinus Torvalds 	for ( ; ; ) {
33726f6313d4SJeff Liu 		bool ret;
33733e1d1d28SChristoph Lameter 
337438087d9bSMel Gorman kswapd_try_sleep:
337538087d9bSMel Gorman 		kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order,
337638087d9bSMel Gorman 					classzone_idx);
3377215ddd66SMel Gorman 
337838087d9bSMel Gorman 		/* Read the new order and classzone_idx */
337938087d9bSMel Gorman 		alloc_order = reclaim_order = pgdat->kswapd_order;
338038087d9bSMel Gorman 		classzone_idx = pgdat->kswapd_classzone_idx;
338138087d9bSMel Gorman 		pgdat->kswapd_order = 0;
338238087d9bSMel Gorman 		pgdat->kswapd_classzone_idx = 0;
33831da177e4SLinus Torvalds 
33848fe23e05SDavid Rientjes 		ret = try_to_freeze();
33858fe23e05SDavid Rientjes 		if (kthread_should_stop())
33868fe23e05SDavid Rientjes 			break;
33878fe23e05SDavid Rientjes 
33888fe23e05SDavid Rientjes 		/*
33898fe23e05SDavid Rientjes 		 * We can speed up thawing tasks if we don't call balance_pgdat
33908fe23e05SDavid Rientjes 		 * after returning from the refrigerator
3391b1296cc4SRafael J. Wysocki 		 */
339238087d9bSMel Gorman 		if (ret)
339338087d9bSMel Gorman 			continue;
33941d82de61SMel Gorman 
339538087d9bSMel Gorman 		/*
339638087d9bSMel Gorman 		 * Reclaim begins at the requested order but if a high-order
339738087d9bSMel Gorman 		 * reclaim fails then kswapd falls back to reclaiming for
339838087d9bSMel Gorman 		 * order-0. If that happens, kswapd will consider sleeping
339938087d9bSMel Gorman 		 * for the order it finished reclaiming at (reclaim_order)
340038087d9bSMel Gorman 		 * but kcompactd is woken to compact for the original
340138087d9bSMel Gorman 		 * request (alloc_order).
340238087d9bSMel Gorman 		 */
3403e5146b12SMel Gorman 		trace_mm_vmscan_kswapd_wake(pgdat->node_id, classzone_idx,
3404e5146b12SMel Gorman 						alloc_order);
340538087d9bSMel Gorman 		reclaim_order = balance_pgdat(pgdat, alloc_order, classzone_idx);
340638087d9bSMel Gorman 		if (reclaim_order < alloc_order)
340738087d9bSMel Gorman 			goto kswapd_try_sleep;
340838087d9bSMel Gorman 
340938087d9bSMel Gorman 		alloc_order = reclaim_order = pgdat->kswapd_order;
341038087d9bSMel Gorman 		classzone_idx = pgdat->kswapd_classzone_idx;
341133906bc5SMel Gorman 	}
3412b0a8cc58STakamori Yamaguchi 
341371abdc15SJohannes Weiner 	tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD);
3414b0a8cc58STakamori Yamaguchi 	current->reclaim_state = NULL;
341571abdc15SJohannes Weiner 	lockdep_clear_current_reclaim_state();
341671abdc15SJohannes Weiner 
34171da177e4SLinus Torvalds 	return 0;
34181da177e4SLinus Torvalds }
34191da177e4SLinus Torvalds 
34201da177e4SLinus Torvalds /*
34211da177e4SLinus Torvalds  * A zone is low on free memory, so wake its kswapd task to service it.
34221da177e4SLinus Torvalds  */
342399504748SMel Gorman void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx)
34241da177e4SLinus Torvalds {
34251da177e4SLinus Torvalds 	pg_data_t *pgdat;
3426e1a55637SMel Gorman 	int z;
34271da177e4SLinus Torvalds 
3428f3fe6512SCon Kolivas 	if (!populated_zone(zone))
34291da177e4SLinus Torvalds 		return;
34301da177e4SLinus Torvalds 
3431344736f2SVladimir Davydov 	if (!cpuset_zone_allowed(zone, GFP_KERNEL | __GFP_HARDWALL))
34321da177e4SLinus Torvalds 		return;
343388f5acf8SMel Gorman 	pgdat = zone->zone_pgdat;
343438087d9bSMel Gorman 	pgdat->kswapd_classzone_idx = max(pgdat->kswapd_classzone_idx, classzone_idx);
343538087d9bSMel Gorman 	pgdat->kswapd_order = max(pgdat->kswapd_order, order);
34368d0986e2SCon Kolivas 	if (!waitqueue_active(&pgdat->kswapd_wait))
34371da177e4SLinus Torvalds 		return;
3438e1a55637SMel Gorman 
3439e1a55637SMel Gorman 	/* Only wake kswapd if all zones are unbalanced */
3440e1a55637SMel Gorman 	for (z = 0; z <= classzone_idx; z++) {
3441e1a55637SMel Gorman 		zone = pgdat->node_zones + z;
3442e1a55637SMel Gorman 		if (!populated_zone(zone))
3443e1a55637SMel Gorman 			continue;
3444e1a55637SMel Gorman 
3445e1a55637SMel Gorman 		if (zone_balanced(zone, order, classzone_idx))
344688f5acf8SMel Gorman 			return;
3447e1a55637SMel Gorman 	}
344888f5acf8SMel Gorman 
344988f5acf8SMel Gorman 	trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order);
34508d0986e2SCon Kolivas 	wake_up_interruptible(&pgdat->kswapd_wait);
34511da177e4SLinus Torvalds }
34521da177e4SLinus Torvalds 
3453c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION
34541da177e4SLinus Torvalds /*
34557b51755cSKOSAKI Motohiro  * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of
3456d6277db4SRafael J. Wysocki  * freed pages.
3457d6277db4SRafael J. Wysocki  *
3458d6277db4SRafael J. Wysocki  * Rather than trying to age LRUs the aim is to preserve the overall
3459d6277db4SRafael J. Wysocki  * LRU order by reclaiming preferentially
3460d6277db4SRafael J. Wysocki  * inactive > active > active referenced > active mapped
34611da177e4SLinus Torvalds  */
34627b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
34631da177e4SLinus Torvalds {
3464d6277db4SRafael J. Wysocki 	struct reclaim_state reclaim_state;
3465d6277db4SRafael J. Wysocki 	struct scan_control sc = {
34667b51755cSKOSAKI Motohiro 		.nr_to_reclaim = nr_to_reclaim,
3467ee814fe2SJohannes Weiner 		.gfp_mask = GFP_HIGHUSER_MOVABLE,
3468b2e18757SMel Gorman 		.reclaim_idx = MAX_NR_ZONES - 1,
34699e3b2f8cSKonstantin Khlebnikov 		.priority = DEF_PRIORITY,
3470ee814fe2SJohannes Weiner 		.may_writepage = 1,
3471ee814fe2SJohannes Weiner 		.may_unmap = 1,
3472ee814fe2SJohannes Weiner 		.may_swap = 1,
3473ee814fe2SJohannes Weiner 		.hibernation_mode = 1,
34741da177e4SLinus Torvalds 	};
34757b51755cSKOSAKI Motohiro 	struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
34767b51755cSKOSAKI Motohiro 	struct task_struct *p = current;
34777b51755cSKOSAKI Motohiro 	unsigned long nr_reclaimed;
34781da177e4SLinus Torvalds 
34797b51755cSKOSAKI Motohiro 	p->flags |= PF_MEMALLOC;
34807b51755cSKOSAKI Motohiro 	lockdep_set_current_reclaim_state(sc.gfp_mask);
3481d6277db4SRafael J. Wysocki 	reclaim_state.reclaimed_slab = 0;
34827b51755cSKOSAKI Motohiro 	p->reclaim_state = &reclaim_state;
3483d6277db4SRafael J. Wysocki 
34843115cd91SVladimir Davydov 	nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
3485d6277db4SRafael J. Wysocki 
34867b51755cSKOSAKI Motohiro 	p->reclaim_state = NULL;
34877b51755cSKOSAKI Motohiro 	lockdep_clear_current_reclaim_state();
34887b51755cSKOSAKI Motohiro 	p->flags &= ~PF_MEMALLOC;
3489d6277db4SRafael J. Wysocki 
34907b51755cSKOSAKI Motohiro 	return nr_reclaimed;
34911da177e4SLinus Torvalds }
3492c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */
34931da177e4SLinus Torvalds 
34941da177e4SLinus Torvalds /* It's optimal to keep kswapds on the same CPUs as their memory, but
34951da177e4SLinus Torvalds    not required for correctness.  So if the last cpu in a node goes
34961da177e4SLinus Torvalds    away, we get changed to run anywhere: as the first one comes back,
34971da177e4SLinus Torvalds    restore their cpu bindings. */
3498fcb35a9bSGreg Kroah-Hartman static int cpu_callback(struct notifier_block *nfb, unsigned long action,
3499fcb35a9bSGreg Kroah-Hartman 			void *hcpu)
35001da177e4SLinus Torvalds {
350158c0a4a7SYasunori Goto 	int nid;
35021da177e4SLinus Torvalds 
35038bb78442SRafael J. Wysocki 	if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
350448fb2e24SLai Jiangshan 		for_each_node_state(nid, N_MEMORY) {
3505c5f59f08SMike Travis 			pg_data_t *pgdat = NODE_DATA(nid);
3506a70f7302SRusty Russell 			const struct cpumask *mask;
3507a70f7302SRusty Russell 
3508a70f7302SRusty Russell 			mask = cpumask_of_node(pgdat->node_id);
3509c5f59f08SMike Travis 
35103e597945SRusty Russell 			if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
35111da177e4SLinus Torvalds 				/* One of our CPUs online: restore mask */
3512c5f59f08SMike Travis 				set_cpus_allowed_ptr(pgdat->kswapd, mask);
35131da177e4SLinus Torvalds 		}
35141da177e4SLinus Torvalds 	}
35151da177e4SLinus Torvalds 	return NOTIFY_OK;
35161da177e4SLinus Torvalds }
35171da177e4SLinus Torvalds 
35183218ae14SYasunori Goto /*
35193218ae14SYasunori Goto  * This kswapd start function will be called by init and node-hot-add.
35203218ae14SYasunori Goto  * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
35213218ae14SYasunori Goto  */
35223218ae14SYasunori Goto int kswapd_run(int nid)
35233218ae14SYasunori Goto {
35243218ae14SYasunori Goto 	pg_data_t *pgdat = NODE_DATA(nid);
35253218ae14SYasunori Goto 	int ret = 0;
35263218ae14SYasunori Goto 
35273218ae14SYasunori Goto 	if (pgdat->kswapd)
35283218ae14SYasunori Goto 		return 0;
35293218ae14SYasunori Goto 
35303218ae14SYasunori Goto 	pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
35313218ae14SYasunori Goto 	if (IS_ERR(pgdat->kswapd)) {
35323218ae14SYasunori Goto 		/* failure at boot is fatal */
35333218ae14SYasunori Goto 		BUG_ON(system_state == SYSTEM_BOOTING);
3534d5dc0ad9SGavin Shan 		pr_err("Failed to start kswapd on node %d\n", nid);
3535d5dc0ad9SGavin Shan 		ret = PTR_ERR(pgdat->kswapd);
3536d72515b8SXishi Qiu 		pgdat->kswapd = NULL;
35373218ae14SYasunori Goto 	}
35383218ae14SYasunori Goto 	return ret;
35393218ae14SYasunori Goto }
35403218ae14SYasunori Goto 
35418fe23e05SDavid Rientjes /*
3542d8adde17SJiang Liu  * Called by memory hotplug when all memory in a node is offlined.  Caller must
3543bfc8c901SVladimir Davydov  * hold mem_hotplug_begin/end().
35448fe23e05SDavid Rientjes  */
35458fe23e05SDavid Rientjes void kswapd_stop(int nid)
35468fe23e05SDavid Rientjes {
35478fe23e05SDavid Rientjes 	struct task_struct *kswapd = NODE_DATA(nid)->kswapd;
35488fe23e05SDavid Rientjes 
3549d8adde17SJiang Liu 	if (kswapd) {
35508fe23e05SDavid Rientjes 		kthread_stop(kswapd);
3551d8adde17SJiang Liu 		NODE_DATA(nid)->kswapd = NULL;
3552d8adde17SJiang Liu 	}
35538fe23e05SDavid Rientjes }
35548fe23e05SDavid Rientjes 
35551da177e4SLinus Torvalds static int __init kswapd_init(void)
35561da177e4SLinus Torvalds {
35573218ae14SYasunori Goto 	int nid;
355869e05944SAndrew Morton 
35591da177e4SLinus Torvalds 	swap_setup();
356048fb2e24SLai Jiangshan 	for_each_node_state(nid, N_MEMORY)
35613218ae14SYasunori Goto  		kswapd_run(nid);
35621da177e4SLinus Torvalds 	hotcpu_notifier(cpu_callback, 0);
35631da177e4SLinus Torvalds 	return 0;
35641da177e4SLinus Torvalds }
35651da177e4SLinus Torvalds 
35661da177e4SLinus Torvalds module_init(kswapd_init)
35679eeff239SChristoph Lameter 
35689eeff239SChristoph Lameter #ifdef CONFIG_NUMA
35699eeff239SChristoph Lameter /*
3570a5f5f91dSMel Gorman  * Node reclaim mode
35719eeff239SChristoph Lameter  *
3572a5f5f91dSMel Gorman  * If non-zero call node_reclaim when the number of free pages falls below
35739eeff239SChristoph Lameter  * the watermarks.
35749eeff239SChristoph Lameter  */
3575a5f5f91dSMel Gorman int node_reclaim_mode __read_mostly;
35769eeff239SChristoph Lameter 
35771b2ffb78SChristoph Lameter #define RECLAIM_OFF 0
35787d03431cSFernando Luis Vazquez Cao #define RECLAIM_ZONE (1<<0)	/* Run shrink_inactive_list on the zone */
35791b2ffb78SChristoph Lameter #define RECLAIM_WRITE (1<<1)	/* Writeout pages during reclaim */
358095bbc0c7SZhihui Zhang #define RECLAIM_UNMAP (1<<2)	/* Unmap pages during reclaim */
35811b2ffb78SChristoph Lameter 
35829eeff239SChristoph Lameter /*
3583a5f5f91dSMel Gorman  * Priority for NODE_RECLAIM. This determines the fraction of pages
3584a92f7126SChristoph Lameter  * of a node considered for each zone_reclaim. 4 scans 1/16th of
3585a92f7126SChristoph Lameter  * a zone.
3586a92f7126SChristoph Lameter  */
3587a5f5f91dSMel Gorman #define NODE_RECLAIM_PRIORITY 4
3588a92f7126SChristoph Lameter 
35899eeff239SChristoph Lameter /*
3590a5f5f91dSMel Gorman  * Percentage of pages in a zone that must be unmapped for node_reclaim to
35919614634fSChristoph Lameter  * occur.
35929614634fSChristoph Lameter  */
35939614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1;
35949614634fSChristoph Lameter 
35959614634fSChristoph Lameter /*
35960ff38490SChristoph Lameter  * If the number of slab pages in a zone grows beyond this percentage then
35970ff38490SChristoph Lameter  * slab reclaim needs to occur.
35980ff38490SChristoph Lameter  */
35990ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5;
36000ff38490SChristoph Lameter 
360111fb9989SMel Gorman static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat)
360290afa5deSMel Gorman {
360311fb9989SMel Gorman 	unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED);
360411fb9989SMel Gorman 	unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) +
360511fb9989SMel Gorman 		node_page_state(pgdat, NR_ACTIVE_FILE);
360690afa5deSMel Gorman 
360790afa5deSMel Gorman 	/*
360890afa5deSMel Gorman 	 * It's possible for there to be more file mapped pages than
360990afa5deSMel Gorman 	 * accounted for by the pages on the file LRU lists because
361090afa5deSMel Gorman 	 * tmpfs pages accounted for as ANON can also be FILE_MAPPED
361190afa5deSMel Gorman 	 */
361290afa5deSMel Gorman 	return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0;
361390afa5deSMel Gorman }
361490afa5deSMel Gorman 
361590afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */
3616a5f5f91dSMel Gorman static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat)
361790afa5deSMel Gorman {
3618d031a157SAlexandru Moise 	unsigned long nr_pagecache_reclaimable;
3619d031a157SAlexandru Moise 	unsigned long delta = 0;
362090afa5deSMel Gorman 
362190afa5deSMel Gorman 	/*
362295bbc0c7SZhihui Zhang 	 * If RECLAIM_UNMAP is set, then all file pages are considered
362390afa5deSMel Gorman 	 * potentially reclaimable. Otherwise, we have to worry about
362411fb9989SMel Gorman 	 * pages like swapcache and node_unmapped_file_pages() provides
362590afa5deSMel Gorman 	 * a better estimate
362690afa5deSMel Gorman 	 */
3627a5f5f91dSMel Gorman 	if (node_reclaim_mode & RECLAIM_UNMAP)
3628a5f5f91dSMel Gorman 		nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES);
362990afa5deSMel Gorman 	else
3630a5f5f91dSMel Gorman 		nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat);
363190afa5deSMel Gorman 
363290afa5deSMel Gorman 	/* If we can't clean pages, remove dirty pages from consideration */
3633a5f5f91dSMel Gorman 	if (!(node_reclaim_mode & RECLAIM_WRITE))
3634a5f5f91dSMel Gorman 		delta += node_page_state(pgdat, NR_FILE_DIRTY);
363590afa5deSMel Gorman 
363690afa5deSMel Gorman 	/* Watch for any possible underflows due to delta */
363790afa5deSMel Gorman 	if (unlikely(delta > nr_pagecache_reclaimable))
363890afa5deSMel Gorman 		delta = nr_pagecache_reclaimable;
363990afa5deSMel Gorman 
364090afa5deSMel Gorman 	return nr_pagecache_reclaimable - delta;
364190afa5deSMel Gorman }
364290afa5deSMel Gorman 
36430ff38490SChristoph Lameter /*
3644a5f5f91dSMel Gorman  * Try to free up some pages from this node through reclaim.
36459eeff239SChristoph Lameter  */
3646a5f5f91dSMel Gorman static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
36479eeff239SChristoph Lameter {
36487fb2d46dSChristoph Lameter 	/* Minimum pages needed in order to stay on node */
364969e05944SAndrew Morton 	const unsigned long nr_pages = 1 << order;
36509eeff239SChristoph Lameter 	struct task_struct *p = current;
36519eeff239SChristoph Lameter 	struct reclaim_state reclaim_state;
3652a5f5f91dSMel Gorman 	int classzone_idx = gfp_zone(gfp_mask);
3653179e9639SAndrew Morton 	struct scan_control sc = {
365462b726c1SAndrew Morton 		.nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
365521caf2fcSMing Lei 		.gfp_mask = (gfp_mask = memalloc_noio_flags(gfp_mask)),
3656bd2f6199SJohannes Weiner 		.order = order,
3657a5f5f91dSMel Gorman 		.priority = NODE_RECLAIM_PRIORITY,
3658a5f5f91dSMel Gorman 		.may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE),
3659a5f5f91dSMel Gorman 		.may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP),
3660ee814fe2SJohannes Weiner 		.may_swap = 1,
3661a5f5f91dSMel Gorman 		.reclaim_idx = classzone_idx,
3662179e9639SAndrew Morton 	};
36639eeff239SChristoph Lameter 
36649eeff239SChristoph Lameter 	cond_resched();
3665d4f7796eSChristoph Lameter 	/*
366695bbc0c7SZhihui Zhang 	 * We need to be able to allocate from the reserves for RECLAIM_UNMAP
3667d4f7796eSChristoph Lameter 	 * and we also need to be able to write out pages for RECLAIM_WRITE
366895bbc0c7SZhihui Zhang 	 * and RECLAIM_UNMAP.
3669d4f7796eSChristoph Lameter 	 */
3670d4f7796eSChristoph Lameter 	p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
367176ca542dSKOSAKI Motohiro 	lockdep_set_current_reclaim_state(gfp_mask);
36729eeff239SChristoph Lameter 	reclaim_state.reclaimed_slab = 0;
36739eeff239SChristoph Lameter 	p->reclaim_state = &reclaim_state;
3674c84db23cSChristoph Lameter 
3675a5f5f91dSMel Gorman 	if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages) {
3676a92f7126SChristoph Lameter 		/*
36770ff38490SChristoph Lameter 		 * Free memory by calling shrink zone with increasing
36780ff38490SChristoph Lameter 		 * priorities until we have enough memory freed.
3679a92f7126SChristoph Lameter 		 */
3680a92f7126SChristoph Lameter 		do {
3681970a39a3SMel Gorman 			shrink_node(pgdat, &sc);
36829e3b2f8cSKonstantin Khlebnikov 		} while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0);
36830ff38490SChristoph Lameter 	}
3684a92f7126SChristoph Lameter 
36859eeff239SChristoph Lameter 	p->reclaim_state = NULL;
3686d4f7796eSChristoph Lameter 	current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
368776ca542dSKOSAKI Motohiro 	lockdep_clear_current_reclaim_state();
3688a79311c1SRik van Riel 	return sc.nr_reclaimed >= nr_pages;
36899eeff239SChristoph Lameter }
3690179e9639SAndrew Morton 
3691a5f5f91dSMel Gorman int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
3692179e9639SAndrew Morton {
3693d773ed6bSDavid Rientjes 	int ret;
3694179e9639SAndrew Morton 
3695179e9639SAndrew Morton 	/*
3696a5f5f91dSMel Gorman 	 * Node reclaim reclaims unmapped file backed pages and
36970ff38490SChristoph Lameter 	 * slab pages if we are over the defined limits.
369834aa1330SChristoph Lameter 	 *
36999614634fSChristoph Lameter 	 * A small portion of unmapped file backed pages is needed for
37009614634fSChristoph Lameter 	 * file I/O otherwise pages read by file I/O will be immediately
3701a5f5f91dSMel Gorman 	 * thrown out if the node is overallocated. So we do not reclaim
3702a5f5f91dSMel Gorman 	 * if less than a specified percentage of the node is used by
37039614634fSChristoph Lameter 	 * unmapped file backed pages.
3704179e9639SAndrew Morton 	 */
3705a5f5f91dSMel Gorman 	if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages &&
3706a5f5f91dSMel Gorman 	    sum_zone_node_page_state(pgdat->node_id, NR_SLAB_RECLAIMABLE) <= pgdat->min_slab_pages)
3707a5f5f91dSMel Gorman 		return NODE_RECLAIM_FULL;
3708179e9639SAndrew Morton 
3709a5f5f91dSMel Gorman 	if (!pgdat_reclaimable(pgdat))
3710a5f5f91dSMel Gorman 		return NODE_RECLAIM_FULL;
3711d773ed6bSDavid Rientjes 
3712179e9639SAndrew Morton 	/*
3713d773ed6bSDavid Rientjes 	 * Do not scan if the allocation should not be delayed.
3714179e9639SAndrew Morton 	 */
3715d0164adcSMel Gorman 	if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC))
3716a5f5f91dSMel Gorman 		return NODE_RECLAIM_NOSCAN;
3717179e9639SAndrew Morton 
3718179e9639SAndrew Morton 	/*
3719a5f5f91dSMel Gorman 	 * Only run node reclaim on the local node or on nodes that do not
3720179e9639SAndrew Morton 	 * have associated processors. This will favor the local processor
3721179e9639SAndrew Morton 	 * over remote processors and spread off node memory allocations
3722179e9639SAndrew Morton 	 * as wide as possible.
3723179e9639SAndrew Morton 	 */
3724a5f5f91dSMel Gorman 	if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id())
3725a5f5f91dSMel Gorman 		return NODE_RECLAIM_NOSCAN;
3726d773ed6bSDavid Rientjes 
3727a5f5f91dSMel Gorman 	if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags))
3728a5f5f91dSMel Gorman 		return NODE_RECLAIM_NOSCAN;
3729fa5e084eSMel Gorman 
3730a5f5f91dSMel Gorman 	ret = __node_reclaim(pgdat, gfp_mask, order);
3731a5f5f91dSMel Gorman 	clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags);
3732d773ed6bSDavid Rientjes 
373324cf7251SMel Gorman 	if (!ret)
373424cf7251SMel Gorman 		count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED);
373524cf7251SMel Gorman 
3736d773ed6bSDavid Rientjes 	return ret;
3737179e9639SAndrew Morton }
37389eeff239SChristoph Lameter #endif
3739894bc310SLee Schermerhorn 
3740894bc310SLee Schermerhorn /*
3741894bc310SLee Schermerhorn  * page_evictable - test whether a page is evictable
3742894bc310SLee Schermerhorn  * @page: the page to test
3743894bc310SLee Schermerhorn  *
3744894bc310SLee Schermerhorn  * Test whether page is evictable--i.e., should be placed on active/inactive
374539b5f29aSHugh Dickins  * lists vs unevictable list.
3746894bc310SLee Schermerhorn  *
3747894bc310SLee Schermerhorn  * Reasons page might not be evictable:
3748ba9ddf49SLee Schermerhorn  * (1) page's mapping marked unevictable
3749b291f000SNick Piggin  * (2) page is part of an mlocked VMA
3750ba9ddf49SLee Schermerhorn  *
3751894bc310SLee Schermerhorn  */
375239b5f29aSHugh Dickins int page_evictable(struct page *page)
3753894bc310SLee Schermerhorn {
375439b5f29aSHugh Dickins 	return !mapping_unevictable(page_mapping(page)) && !PageMlocked(page);
3755894bc310SLee Schermerhorn }
375689e004eaSLee Schermerhorn 
375785046579SHugh Dickins #ifdef CONFIG_SHMEM
375889e004eaSLee Schermerhorn /**
375924513264SHugh Dickins  * check_move_unevictable_pages - check pages for evictability and move to appropriate zone lru list
376024513264SHugh Dickins  * @pages:	array of pages to check
376124513264SHugh Dickins  * @nr_pages:	number of pages to check
376289e004eaSLee Schermerhorn  *
376324513264SHugh Dickins  * Checks pages for evictability and moves them to the appropriate lru list.
376485046579SHugh Dickins  *
376585046579SHugh Dickins  * This function is only used for SysV IPC SHM_UNLOCK.
376689e004eaSLee Schermerhorn  */
376724513264SHugh Dickins void check_move_unevictable_pages(struct page **pages, int nr_pages)
376889e004eaSLee Schermerhorn {
3769925b7673SJohannes Weiner 	struct lruvec *lruvec;
377024513264SHugh Dickins 	struct zone *zone = NULL;
377124513264SHugh Dickins 	int pgscanned = 0;
377224513264SHugh Dickins 	int pgrescued = 0;
377389e004eaSLee Schermerhorn 	int i;
377489e004eaSLee Schermerhorn 
377524513264SHugh Dickins 	for (i = 0; i < nr_pages; i++) {
377624513264SHugh Dickins 		struct page *page = pages[i];
377724513264SHugh Dickins 		struct zone *pagezone;
377889e004eaSLee Schermerhorn 
377924513264SHugh Dickins 		pgscanned++;
378024513264SHugh Dickins 		pagezone = page_zone(page);
378189e004eaSLee Schermerhorn 		if (pagezone != zone) {
378289e004eaSLee Schermerhorn 			if (zone)
3783a52633d8SMel Gorman 				spin_unlock_irq(zone_lru_lock(zone));
378489e004eaSLee Schermerhorn 			zone = pagezone;
3785a52633d8SMel Gorman 			spin_lock_irq(zone_lru_lock(zone));
378689e004eaSLee Schermerhorn 		}
3787599d0c95SMel Gorman 		lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat);
378889e004eaSLee Schermerhorn 
378924513264SHugh Dickins 		if (!PageLRU(page) || !PageUnevictable(page))
379024513264SHugh Dickins 			continue;
379189e004eaSLee Schermerhorn 
379239b5f29aSHugh Dickins 		if (page_evictable(page)) {
379324513264SHugh Dickins 			enum lru_list lru = page_lru_base_type(page);
379424513264SHugh Dickins 
3795309381feSSasha Levin 			VM_BUG_ON_PAGE(PageActive(page), page);
379624513264SHugh Dickins 			ClearPageUnevictable(page);
3797fa9add64SHugh Dickins 			del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE);
3798fa9add64SHugh Dickins 			add_page_to_lru_list(page, lruvec, lru);
379924513264SHugh Dickins 			pgrescued++;
380089e004eaSLee Schermerhorn 		}
380189e004eaSLee Schermerhorn 	}
380224513264SHugh Dickins 
380324513264SHugh Dickins 	if (zone) {
380424513264SHugh Dickins 		__count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued);
380524513264SHugh Dickins 		__count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned);
3806a52633d8SMel Gorman 		spin_unlock_irq(zone_lru_lock(zone));
380724513264SHugh Dickins 	}
380885046579SHugh Dickins }
380985046579SHugh Dickins #endif /* CONFIG_SHMEM */
3810