blob: c02c850ea3490af95fde44f94bd199fbdc500684 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/mm/vmscan.c
4 *
5 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
6 *
7 * Swap reorganised 29.12.95, Stephen Tweedie.
8 * kswapd added: 7.1.96 sct
9 * Removed kswapd_ctl limits, and swap out as many pages as needed
10 * to bring the system back to freepages.high: 2.4.97, Rik van Riel.
11 * Zone aware kswapd started 02/00, Kanoj Sarcar (kanoj@sgi.com).
12 * Multiqueue VM started 5.8.00, Rik van Riel.
13 */
14
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -070015#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mm.h>
Ingo Molnar5b3cc152017-02-02 20:43:54 +010018#include <linux/sched/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/kernel_stat.h>
22#include <linux/swap.h>
23#include <linux/pagemap.h>
24#include <linux/init.h>
25#include <linux/highmem.h>
Anton Vorontsov70ddf632013-04-29 15:08:31 -070026#include <linux/vmpressure.h>
Andrew Mortone129b5c2006-09-27 01:50:00 -070027#include <linux/vmstat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/file.h>
29#include <linux/writeback.h>
30#include <linux/blkdev.h>
31#include <linux/buffer_head.h> /* for try_to_release_page(),
32 buffer_heads_over_limit */
33#include <linux/mm_inline.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/backing-dev.h>
35#include <linux/rmap.h>
36#include <linux/topology.h>
37#include <linux/cpu.h>
38#include <linux/cpuset.h>
Mel Gorman3e7d3442011-01-13 15:45:56 -080039#include <linux/compaction.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/notifier.h>
41#include <linux/rwsem.h>
Rafael J. Wysocki248a0302006-03-22 00:09:04 -080042#include <linux/delay.h>
Yasunori Goto3218ae12006-06-27 02:53:33 -070043#include <linux/kthread.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080044#include <linux/freezer.h>
Balbir Singh66e17072008-02-07 00:13:56 -080045#include <linux/memcontrol.h>
Keika Kobayashi873b4772008-07-25 01:48:52 -070046#include <linux/delayacct.h>
Lee Schermerhornaf936a12008-10-18 20:26:53 -070047#include <linux/sysctl.h>
KOSAKI Motohiro929bea72011-04-14 15:22:12 -070048#include <linux/oom.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070049#include <linux/prefetch.h>
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -070050#include <linux/printk.h>
Ross Zwislerf9fe48b2016-01-22 15:10:40 -080051#include <linux/dax.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#include <asm/tlbflush.h>
54#include <asm/div64.h>
55
56#include <linux/swapops.h>
Rafael Aquini117aad12013-09-30 13:45:16 -070057#include <linux/balloon_compaction.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Nick Piggin0f8053a2006-03-22 00:08:33 -080059#include "internal.h"
60
Mel Gorman33906bc2010-08-09 17:19:16 -070061#define CREATE_TRACE_POINTS
62#include <trace/events/vmscan.h>
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064struct scan_control {
KOSAKI Motohiro22fba332009-12-14 17:59:10 -080065 /* How many pages shrink_list() should reclaim */
66 unsigned long nr_to_reclaim;
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 /* This context's GFP mask */
Al Viro6daa0e22005-10-21 03:18:50 -040069 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Johannes Weineree814fe2014-08-06 16:06:19 -070071 /* Allocation order */
Andy Whitcroft5ad333e2007-07-17 04:03:16 -070072 int order;
Balbir Singh66e17072008-02-07 00:13:56 -080073
Johannes Weineree814fe2014-08-06 16:06:19 -070074 /*
75 * Nodemask of nodes allowed by the caller. If NULL, all nodes
76 * are scanned.
77 */
78 nodemask_t *nodemask;
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -070079
KOSAKI Motohiro5f53e762010-05-24 14:32:37 -070080 /*
Johannes Weinerf16015f2012-01-12 17:17:52 -080081 * The memory cgroup that hit its limit and as a result is the
82 * primary target of this reclaim invocation.
83 */
84 struct mem_cgroup *target_mem_cgroup;
Balbir Singh66e17072008-02-07 00:13:56 -080085
Johannes Weineree814fe2014-08-06 16:06:19 -070086 /* Scan (total_size >> priority) pages at once */
87 int priority;
88
Mel Gormanb2e18752016-07-28 15:45:37 -070089 /* The highest zone to isolate pages for reclaim from */
90 enum zone_type reclaim_idx;
91
Johannes Weiner1276ad62017-02-24 14:56:11 -080092 /* Writepage batching in laptop mode; RECLAIM_WRITE */
Johannes Weineree814fe2014-08-06 16:06:19 -070093 unsigned int may_writepage:1;
94
95 /* Can mapped pages be reclaimed? */
96 unsigned int may_unmap:1;
97
98 /* Can pages be swapped as part of reclaim? */
99 unsigned int may_swap:1;
100
Yisheng Xied6622f62017-05-03 14:53:57 -0700101 /*
102 * Cgroups are not reclaimed below their configured memory.low,
103 * unless we threaten to OOM. If any cgroups are skipped due to
104 * memory.low and nothing was reclaimed, go back for memory.low.
105 */
106 unsigned int memcg_low_reclaim:1;
107 unsigned int memcg_low_skipped:1;
Johannes Weiner241994ed2015-02-11 15:26:06 -0800108
Johannes Weineree814fe2014-08-06 16:06:19 -0700109 unsigned int hibernation_mode:1;
110
111 /* One of the zones is ready for compaction */
112 unsigned int compaction_ready:1;
113
114 /* Incremented by the number of inactive pages that were scanned */
115 unsigned long nr_scanned;
116
117 /* Number of pages freed so far during a call to shrink_zones() */
118 unsigned long nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119};
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#ifdef ARCH_HAS_PREFETCH
122#define prefetch_prev_lru_page(_page, _base, _field) \
123 do { \
124 if ((_page)->lru.prev != _base) { \
125 struct page *prev; \
126 \
127 prev = lru_to_page(&(_page->lru)); \
128 prefetch(&prev->_field); \
129 } \
130 } while (0)
131#else
132#define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
133#endif
134
135#ifdef ARCH_HAS_PREFETCHW
136#define prefetchw_prev_lru_page(_page, _base, _field) \
137 do { \
138 if ((_page)->lru.prev != _base) { \
139 struct page *prev; \
140 \
141 prev = lru_to_page(&(_page->lru)); \
142 prefetchw(&prev->_field); \
143 } \
144 } while (0)
145#else
146#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
147#endif
148
149/*
150 * From 0 .. 100. Higher means more swappy.
151 */
152int vm_swappiness = 60;
Wang Sheng-Huid0480be2014-08-06 16:07:07 -0700153/*
154 * The total number of pages which are beyond the high watermark within all
155 * zones.
156 */
157unsigned long vm_total_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159static LIST_HEAD(shrinker_list);
160static DECLARE_RWSEM(shrinker_rwsem);
161
Andrew Mortonc255a452012-07-31 16:43:02 -0700162#ifdef CONFIG_MEMCG
Johannes Weiner89b5fae2012-01-12 17:17:50 -0800163static bool global_reclaim(struct scan_control *sc)
164{
Johannes Weinerf16015f2012-01-12 17:17:52 -0800165 return !sc->target_mem_cgroup;
Johannes Weiner89b5fae2012-01-12 17:17:50 -0800166}
Tejun Heo97c93412015-05-22 18:23:36 -0400167
168/**
169 * sane_reclaim - is the usual dirty throttling mechanism operational?
170 * @sc: scan_control in question
171 *
172 * The normal page dirty throttling mechanism in balance_dirty_pages() is
173 * completely broken with the legacy memcg and direct stalling in
174 * shrink_page_list() is used for throttling instead, which lacks all the
175 * niceties such as fairness, adaptive pausing, bandwidth proportional
176 * allocation and configurability.
177 *
178 * This function tests whether the vmscan currently in progress can assume
179 * that the normal dirty throttling mechanism is operational.
180 */
181static bool sane_reclaim(struct scan_control *sc)
182{
183 struct mem_cgroup *memcg = sc->target_mem_cgroup;
184
185 if (!memcg)
186 return true;
187#ifdef CONFIG_CGROUP_WRITEBACK
Linus Torvalds69234ac2015-11-05 14:51:32 -0800188 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
Tejun Heo97c93412015-05-22 18:23:36 -0400189 return true;
190#endif
191 return false;
192}
KAMEZAWA Hiroyuki91a45472008-02-07 00:14:29 -0800193#else
Johannes Weiner89b5fae2012-01-12 17:17:50 -0800194static bool global_reclaim(struct scan_control *sc)
195{
196 return true;
197}
Tejun Heo97c93412015-05-22 18:23:36 -0400198
199static bool sane_reclaim(struct scan_control *sc)
200{
201 return true;
202}
KAMEZAWA Hiroyuki91a45472008-02-07 00:14:29 -0800203#endif
204
Mel Gorman5a1c84b2016-07-28 15:47:31 -0700205/*
206 * This misses isolated pages which are not accounted for to save counters.
207 * As the data only determines if reclaim or compaction continues, it is
208 * not expected that isolated pages will be a dominating factor.
209 */
210unsigned long zone_reclaimable_pages(struct zone *zone)
211{
212 unsigned long nr;
213
214 nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) +
215 zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE);
216 if (get_nr_swap_pages() > 0)
217 nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) +
218 zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON);
219
220 return nr;
221}
222
Mel Gorman599d0c92016-07-28 15:45:31 -0700223unsigned long pgdat_reclaimable_pages(struct pglist_data *pgdat)
Lisa Du6e543d52013-09-11 14:22:36 -0700224{
Mel Gorman599d0c92016-07-28 15:45:31 -0700225 unsigned long nr;
226
227 nr = node_page_state_snapshot(pgdat, NR_ACTIVE_FILE) +
228 node_page_state_snapshot(pgdat, NR_INACTIVE_FILE) +
229 node_page_state_snapshot(pgdat, NR_ISOLATED_FILE);
230
231 if (get_nr_swap_pages() > 0)
232 nr += node_page_state_snapshot(pgdat, NR_ACTIVE_ANON) +
233 node_page_state_snapshot(pgdat, NR_INACTIVE_ANON) +
234 node_page_state_snapshot(pgdat, NR_ISOLATED_ANON);
235
236 return nr;
237}
238
Michal Hockofd538802017-02-22 15:45:58 -0800239/**
240 * lruvec_lru_size - Returns the number of pages on the given LRU list.
241 * @lruvec: lru vector
242 * @lru: lru to use
243 * @zone_idx: zones to consider (use MAX_NR_ZONES for the whole LRU list)
244 */
245unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, int zone_idx)
KOSAKI Motohiroc9f299d2009-01-07 18:08:16 -0800246{
Michal Hockofd538802017-02-22 15:45:58 -0800247 unsigned long lru_size;
248 int zid;
249
Hugh Dickinsc3c787e2012-05-29 15:06:52 -0700250 if (!mem_cgroup_disabled())
Michal Hockofd538802017-02-22 15:45:58 -0800251 lru_size = mem_cgroup_get_lru_size(lruvec, lru);
252 else
253 lru_size = node_page_state(lruvec_pgdat(lruvec), NR_LRU_BASE + lru);
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800254
Michal Hockofd538802017-02-22 15:45:58 -0800255 for (zid = zone_idx + 1; zid < MAX_NR_ZONES; zid++) {
256 struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid];
257 unsigned long size;
KOSAKI Motohiroc9f299d2009-01-07 18:08:16 -0800258
Michal Hockofd538802017-02-22 15:45:58 -0800259 if (!managed_zone(zone))
260 continue;
Michal Hockob4536f0c82017-01-10 16:58:04 -0800261
Michal Hockofd538802017-02-22 15:45:58 -0800262 if (!mem_cgroup_disabled())
263 size = mem_cgroup_get_zone_lru_size(lruvec, lru, zid);
264 else
265 size = zone_page_state(&lruvec_pgdat(lruvec)->node_zones[zid],
266 NR_ZONE_LRU_BASE + lru);
267 lru_size -= min(size, lru_size);
268 }
269
270 return lru_size;
271
Michal Hockob4536f0c82017-01-10 16:58:04 -0800272}
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274/*
Glauber Costa1d3d4432013-08-28 10:18:04 +1000275 * Add a shrinker callback to be called from the vm.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 */
Glauber Costa1d3d4432013-08-28 10:18:04 +1000277int register_shrinker(struct shrinker *shrinker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Glauber Costa1d3d4432013-08-28 10:18:04 +1000279 size_t size = sizeof(*shrinker->nr_deferred);
280
Glauber Costa1d3d4432013-08-28 10:18:04 +1000281 if (shrinker->flags & SHRINKER_NUMA_AWARE)
282 size *= nr_node_ids;
283
284 shrinker->nr_deferred = kzalloc(size, GFP_KERNEL);
285 if (!shrinker->nr_deferred)
286 return -ENOMEM;
287
Rusty Russell8e1f9362007-07-17 04:03:17 -0700288 down_write(&shrinker_rwsem);
289 list_add_tail(&shrinker->list, &shrinker_list);
290 up_write(&shrinker_rwsem);
Glauber Costa1d3d4432013-08-28 10:18:04 +1000291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
Rusty Russell8e1f9362007-07-17 04:03:17 -0700293EXPORT_SYMBOL(register_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295/*
296 * Remove one
297 */
Rusty Russell8e1f9362007-07-17 04:03:17 -0700298void unregister_shrinker(struct shrinker *shrinker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 down_write(&shrinker_rwsem);
301 list_del(&shrinker->list);
302 up_write(&shrinker_rwsem);
Andrew Vaginae393322013-10-16 13:46:46 -0700303 kfree(shrinker->nr_deferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
Rusty Russell8e1f9362007-07-17 04:03:17 -0700305EXPORT_SYMBOL(unregister_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307#define SHRINK_BATCH 128
Glauber Costa1d3d4432013-08-28 10:18:04 +1000308
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800309static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,
310 struct shrinker *shrinker,
311 unsigned long nr_scanned,
312 unsigned long nr_eligible)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Glauber Costa1d3d4432013-08-28 10:18:04 +1000314 unsigned long freed = 0;
315 unsigned long long delta;
316 long total_scan;
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700317 long freeable;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000318 long nr;
319 long new_nr;
320 int nid = shrinkctl->nid;
321 long batch_size = shrinker->batch ? shrinker->batch
322 : SHRINK_BATCH;
Shaohua Li5f33a082016-12-12 16:41:50 -0800323 long scanned = 0, next_deferred;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000324
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700325 freeable = shrinker->count_objects(shrinker, shrinkctl);
326 if (freeable == 0)
Glauber Costa1d3d4432013-08-28 10:18:04 +1000327 return 0;
328
329 /*
330 * copy the current shrinker scan count into a local variable
331 * and zero it so that other concurrent shrinker invocations
332 * don't also do this scanning work.
333 */
334 nr = atomic_long_xchg(&shrinker->nr_deferred[nid], 0);
335
336 total_scan = nr;
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800337 delta = (4 * nr_scanned) / shrinker->seeks;
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700338 delta *= freeable;
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800339 do_div(delta, nr_eligible + 1);
Glauber Costa1d3d4432013-08-28 10:18:04 +1000340 total_scan += delta;
341 if (total_scan < 0) {
Pintu Kumar8612c662014-12-10 15:42:58 -0800342 pr_err("shrink_slab: %pF negative objects to delete nr=%ld\n",
Dave Chinnera0b02132013-08-28 10:18:16 +1000343 shrinker->scan_objects, total_scan);
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700344 total_scan = freeable;
Shaohua Li5f33a082016-12-12 16:41:50 -0800345 next_deferred = nr;
346 } else
347 next_deferred = total_scan;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000348
349 /*
350 * We need to avoid excessive windup on filesystem shrinkers
351 * due to large numbers of GFP_NOFS allocations causing the
352 * shrinkers to return -1 all the time. This results in a large
353 * nr being built up so when a shrink that can do some work
354 * comes along it empties the entire cache due to nr >>>
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700355 * freeable. This is bad for sustaining a working set in
Glauber Costa1d3d4432013-08-28 10:18:04 +1000356 * memory.
357 *
358 * Hence only allow the shrinker to scan the entire cache when
359 * a large delta change is calculated directly.
360 */
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700361 if (delta < freeable / 4)
362 total_scan = min(total_scan, freeable / 2);
Glauber Costa1d3d4432013-08-28 10:18:04 +1000363
364 /*
365 * Avoid risking looping forever due to too large nr value:
366 * never try to free more than twice the estimate number of
367 * freeable entries.
368 */
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700369 if (total_scan > freeable * 2)
370 total_scan = freeable * 2;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000371
372 trace_mm_shrink_slab_start(shrinker, shrinkctl, nr,
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800373 nr_scanned, nr_eligible,
374 freeable, delta, total_scan);
Glauber Costa1d3d4432013-08-28 10:18:04 +1000375
Vladimir Davydov0b1fb402014-01-23 15:53:22 -0800376 /*
377 * Normally, we should not scan less than batch_size objects in one
378 * pass to avoid too frequent shrinker calls, but if the slab has less
379 * than batch_size objects in total and we are really tight on memory,
380 * we will try to reclaim all available objects, otherwise we can end
381 * up failing allocations although there are plenty of reclaimable
382 * objects spread over several slabs with usage less than the
383 * batch_size.
384 *
385 * We detect the "tight on memory" situations by looking at the total
386 * number of objects we want to scan (total_scan). If it is greater
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700387 * than the total number of objects on slab (freeable), we must be
Vladimir Davydov0b1fb402014-01-23 15:53:22 -0800388 * scanning at high prio and therefore should try to reclaim as much as
389 * possible.
390 */
391 while (total_scan >= batch_size ||
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700392 total_scan >= freeable) {
Dave Chinnera0b02132013-08-28 10:18:16 +1000393 unsigned long ret;
Vladimir Davydov0b1fb402014-01-23 15:53:22 -0800394 unsigned long nr_to_scan = min(batch_size, total_scan);
Glauber Costa1d3d4432013-08-28 10:18:04 +1000395
Vladimir Davydov0b1fb402014-01-23 15:53:22 -0800396 shrinkctl->nr_to_scan = nr_to_scan;
Chris Wilsond460acb2017-09-06 16:19:26 -0700397 shrinkctl->nr_scanned = nr_to_scan;
Dave Chinnera0b02132013-08-28 10:18:16 +1000398 ret = shrinker->scan_objects(shrinker, shrinkctl);
399 if (ret == SHRINK_STOP)
400 break;
401 freed += ret;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000402
Chris Wilsond460acb2017-09-06 16:19:26 -0700403 count_vm_events(SLABS_SCANNED, shrinkctl->nr_scanned);
404 total_scan -= shrinkctl->nr_scanned;
405 scanned += shrinkctl->nr_scanned;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000406
407 cond_resched();
408 }
409
Shaohua Li5f33a082016-12-12 16:41:50 -0800410 if (next_deferred >= scanned)
411 next_deferred -= scanned;
412 else
413 next_deferred = 0;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000414 /*
415 * move the unused scan count back into the shrinker in a
416 * manner that handles concurrent updates. If we exhausted the
417 * scan, there is no need to do an update.
418 */
Shaohua Li5f33a082016-12-12 16:41:50 -0800419 if (next_deferred > 0)
420 new_nr = atomic_long_add_return(next_deferred,
Glauber Costa1d3d4432013-08-28 10:18:04 +1000421 &shrinker->nr_deferred[nid]);
422 else
423 new_nr = atomic_long_read(&shrinker->nr_deferred[nid]);
424
Dave Hansendf9024a2014-06-04 16:08:07 -0700425 trace_mm_shrink_slab_end(shrinker, nid, freed, nr, new_nr, total_scan);
Glauber Costa1d3d4432013-08-28 10:18:04 +1000426 return freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800429/**
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800430 * shrink_slab - shrink slab caches
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800431 * @gfp_mask: allocation context
432 * @nid: node whose slab caches to target
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800433 * @memcg: memory cgroup whose slab caches to target
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800434 * @nr_scanned: pressure numerator
435 * @nr_eligible: pressure denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 *
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800437 * Call the shrink functions to age shrinkable caches.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 *
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800439 * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set,
440 * unaware shrinkers will receive a node id of 0 instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 *
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800442 * @memcg specifies the memory cgroup to target. If it is not NULL,
443 * only shrinkers with SHRINKER_MEMCG_AWARE set will be called to scan
Vladimir Davydov0fc9f582016-03-17 14:18:30 -0700444 * objects from the memory cgroup specified. Otherwise, only unaware
445 * shrinkers are called.
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800446 *
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800447 * @nr_scanned and @nr_eligible form a ratio that indicate how much of
448 * the available objects should be scanned. Page reclaim for example
449 * passes the number of pages scanned and the number of pages on the
450 * LRU lists that it considered on @nid, plus a bias in @nr_scanned
451 * when it encountered mapped pages. The ratio is further biased by
452 * the ->seeks setting of the shrink function, which indicates the
453 * cost to recreate an object relative to that of an LRU page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 *
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800455 * Returns the number of reclaimed slab objects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 */
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800457static unsigned long shrink_slab(gfp_t gfp_mask, int nid,
458 struct mem_cgroup *memcg,
459 unsigned long nr_scanned,
460 unsigned long nr_eligible)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 struct shrinker *shrinker;
Dave Chinner24f7c6b2013-08-28 10:17:56 +1000463 unsigned long freed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Vladimir Davydov0fc9f582016-03-17 14:18:30 -0700465 if (memcg && (!memcg_kmem_enabled() || !mem_cgroup_online(memcg)))
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800466 return 0;
467
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800468 if (nr_scanned == 0)
469 nr_scanned = SWAP_CLUSTER_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Minchan Kimf06590b2011-05-24 17:11:11 -0700471 if (!down_read_trylock(&shrinker_rwsem)) {
Dave Chinner24f7c6b2013-08-28 10:17:56 +1000472 /*
473 * If we would return 0, our callers would understand that we
474 * have nothing else to shrink and give up trying. By returning
475 * 1 we keep it going and assume we'll be able to shrink next
476 * time.
477 */
478 freed = 1;
Minchan Kimf06590b2011-05-24 17:11:11 -0700479 goto out;
480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 list_for_each_entry(shrinker, &shrinker_list, list) {
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800483 struct shrink_control sc = {
484 .gfp_mask = gfp_mask,
485 .nid = nid,
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800486 .memcg = memcg,
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800487 };
Vladimir Davydovec970972014-01-23 15:53:23 -0800488
Vladimir Davydov0fc9f582016-03-17 14:18:30 -0700489 /*
490 * If kernel memory accounting is disabled, we ignore
491 * SHRINKER_MEMCG_AWARE flag and call all shrinkers
492 * passing NULL for memcg.
493 */
494 if (memcg_kmem_enabled() &&
495 !!memcg != !!(shrinker->flags & SHRINKER_MEMCG_AWARE))
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800496 continue;
497
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800498 if (!(shrinker->flags & SHRINKER_NUMA_AWARE))
499 sc.nid = 0;
Dave Chinneracf92b42011-07-08 14:14:35 +1000500
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800501 freed += do_shrink_slab(&sc, shrinker, nr_scanned, nr_eligible);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 up_read(&shrinker_rwsem);
Minchan Kimf06590b2011-05-24 17:11:11 -0700505out:
506 cond_resched();
Dave Chinner24f7c6b2013-08-28 10:17:56 +1000507 return freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800510void drop_slab_node(int nid)
511{
512 unsigned long freed;
513
514 do {
515 struct mem_cgroup *memcg = NULL;
516
517 freed = 0;
518 do {
519 freed += shrink_slab(GFP_KERNEL, nid, memcg,
520 1000, 1000);
521 } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL);
522 } while (freed > 10);
523}
524
525void drop_slab(void)
526{
527 int nid;
528
529 for_each_online_node(nid)
530 drop_slab_node(nid);
531}
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533static inline int is_page_cache_freeable(struct page *page)
534{
Johannes Weinerceddc3a2009-09-21 17:03:00 -0700535 /*
536 * A freeable page cache page is referenced only by the caller
537 * that isolated the page, the page cache radix tree and
538 * optional buffer heads at page->private.
539 */
Huang Yingbd4c82c22017-09-06 16:22:49 -0700540 int radix_pins = PageTransHuge(page) && PageSwapCache(page) ?
541 HPAGE_PMD_NR : 1;
542 return page_count(page) - page_has_private(page) == 1 + radix_pins;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Tejun Heo703c2702015-05-22 17:13:44 -0400545static int may_write_to_inode(struct inode *inode, struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Christoph Lameter930d9152006-01-08 01:00:47 -0800547 if (current->flags & PF_SWAPWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return 1;
Tejun Heo703c2702015-05-22 17:13:44 -0400549 if (!inode_write_congested(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return 1;
Tejun Heo703c2702015-05-22 17:13:44 -0400551 if (inode_to_bdi(inode) == current->backing_dev_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return 1;
553 return 0;
554}
555
556/*
557 * We detected a synchronous write error writing a page out. Probably
558 * -ENOSPC. We need to propagate that into the address_space for a subsequent
559 * fsync(), msync() or close().
560 *
561 * The tricky part is that after writepage we cannot touch the mapping: nothing
562 * prevents it from being freed up. But we have a ref on the page and once
563 * that page is locked, the mapping is pinned.
564 *
565 * We're allowed to run sleeping lock_page() here because we know the caller has
566 * __GFP_FS.
567 */
568static void handle_write_error(struct address_space *mapping,
569 struct page *page, int error)
570{
Jens Axboe7eaceac2011-03-10 08:52:07 +0100571 lock_page(page);
Guillaume Chazarain3e9f45b2007-05-08 00:23:25 -0700572 if (page_mapping(page) == mapping)
573 mapping_set_error(mapping, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 unlock_page(page);
575}
576
Christoph Lameter04e62a22006-06-23 02:03:38 -0700577/* possible outcome of pageout() */
578typedef enum {
579 /* failed to write page out, page is locked */
580 PAGE_KEEP,
581 /* move page to the active list, page is locked */
582 PAGE_ACTIVATE,
583 /* page has been sent to the disk successfully, page is unlocked */
584 PAGE_SUCCESS,
585 /* page is clean and locked */
586 PAGE_CLEAN,
587} pageout_t;
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589/*
Andrew Morton1742f192006-03-22 00:08:21 -0800590 * pageout is called by shrink_page_list() for each dirty page.
591 * Calls ->writepage().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 */
Andy Whitcroftc661b072007-08-22 14:01:26 -0700593static pageout_t pageout(struct page *page, struct address_space *mapping,
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -0700594 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 /*
597 * If the page is dirty, only perform writeback if that write
598 * will be non-blocking. To prevent this allocation from being
599 * stalled by pagecache activity. But note that there may be
600 * stalls if we need to run get_block(). We could test
601 * PagePrivate for that.
602 *
Al Viro81742022014-04-03 03:17:43 -0400603 * If this process is currently in __generic_file_write_iter() against
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 * this page's queue, we can perform writeback even if that
605 * will block.
606 *
607 * If the page is swapcache, write it back even if that would
608 * block, for some throttling. This happens by accident, because
609 * swap_backing_dev_info is bust: it doesn't reflect the
610 * congestion state of the swapdevs. Easy to fix, if needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 */
612 if (!is_page_cache_freeable(page))
613 return PAGE_KEEP;
614 if (!mapping) {
615 /*
616 * Some data journaling orphaned pages can have
617 * page->mapping == NULL while being dirty with clean buffers.
618 */
David Howells266cf652009-04-03 16:42:36 +0100619 if (page_has_private(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (try_to_free_buffers(page)) {
621 ClearPageDirty(page);
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -0700622 pr_info("%s: orphaned page\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return PAGE_CLEAN;
624 }
625 }
626 return PAGE_KEEP;
627 }
628 if (mapping->a_ops->writepage == NULL)
629 return PAGE_ACTIVATE;
Tejun Heo703c2702015-05-22 17:13:44 -0400630 if (!may_write_to_inode(mapping->host, sc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return PAGE_KEEP;
632
633 if (clear_page_dirty_for_io(page)) {
634 int res;
635 struct writeback_control wbc = {
636 .sync_mode = WB_SYNC_NONE,
637 .nr_to_write = SWAP_CLUSTER_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700638 .range_start = 0,
639 .range_end = LLONG_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 .for_reclaim = 1,
641 };
642
643 SetPageReclaim(page);
644 res = mapping->a_ops->writepage(page, &wbc);
645 if (res < 0)
646 handle_write_error(mapping, page, res);
Zach Brown994fc28c2005-12-15 14:28:17 -0800647 if (res == AOP_WRITEPAGE_ACTIVATE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 ClearPageReclaim(page);
649 return PAGE_ACTIVATE;
650 }
Andy Whitcroftc661b072007-08-22 14:01:26 -0700651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (!PageWriteback(page)) {
653 /* synchronous write or broken a_ops? */
654 ClearPageReclaim(page);
655 }
yalin wang3aa23852016-01-14 15:18:30 -0800656 trace_mm_vmscan_writepage(page);
Mel Gormanc4a25632016-07-28 15:46:23 -0700657 inc_node_page_state(page, NR_VMSCAN_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return PAGE_SUCCESS;
659 }
660
661 return PAGE_CLEAN;
662}
663
Andrew Mortona649fd92006-10-17 00:09:36 -0700664/*
Nick Piggine2867812008-07-25 19:45:30 -0700665 * Same as remove_mapping, but if the page is removed from the mapping, it
666 * gets returned with a refcount of 0.
Andrew Mortona649fd92006-10-17 00:09:36 -0700667 */
Johannes Weinera5289102014-04-03 14:47:51 -0700668static int __remove_mapping(struct address_space *mapping, struct page *page,
669 bool reclaimed)
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800670{
Greg Thelenc4843a72015-05-22 17:13:16 -0400671 unsigned long flags;
Huang Yingbd4c82c22017-09-06 16:22:49 -0700672 int refcount;
Greg Thelenc4843a72015-05-22 17:13:16 -0400673
Nick Piggin28e4d962006-09-25 23:31:23 -0700674 BUG_ON(!PageLocked(page));
675 BUG_ON(mapping != page_mapping(page));
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800676
Greg Thelenc4843a72015-05-22 17:13:16 -0400677 spin_lock_irqsave(&mapping->tree_lock, flags);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800678 /*
Nick Piggin0fd0e6b2006-09-27 01:50:02 -0700679 * The non racy check for a busy page.
680 *
681 * Must be careful with the order of the tests. When someone has
682 * a ref to the page, it may be possible that they dirty it then
683 * drop the reference. So if PageDirty is tested before page_count
684 * here, then the following race may occur:
685 *
686 * get_user_pages(&page);
687 * [user mapping goes away]
688 * write_to(page);
689 * !PageDirty(page) [good]
690 * SetPageDirty(page);
691 * put_page(page);
692 * !page_count(page) [good, discard it]
693 *
694 * [oops, our write_to data is lost]
695 *
696 * Reversing the order of the tests ensures such a situation cannot
697 * escape unnoticed. The smp_rmb is needed to ensure the page->flags
Joonsoo Kim0139aa72016-05-19 17:10:49 -0700698 * load is not satisfied before that of page->_refcount.
Nick Piggin0fd0e6b2006-09-27 01:50:02 -0700699 *
700 * Note that if SetPageDirty is always performed via set_page_dirty,
701 * and thus under tree_lock, then this ordering is not required.
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800702 */
Huang Yingbd4c82c22017-09-06 16:22:49 -0700703 if (unlikely(PageTransHuge(page)) && PageSwapCache(page))
704 refcount = 1 + HPAGE_PMD_NR;
705 else
706 refcount = 2;
707 if (!page_ref_freeze(page, refcount))
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800708 goto cannot_free;
Nick Piggine2867812008-07-25 19:45:30 -0700709 /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */
710 if (unlikely(PageDirty(page))) {
Huang Yingbd4c82c22017-09-06 16:22:49 -0700711 page_ref_unfreeze(page, refcount);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800712 goto cannot_free;
Nick Piggine2867812008-07-25 19:45:30 -0700713 }
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800714
715 if (PageSwapCache(page)) {
716 swp_entry_t swap = { .val = page_private(page) };
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700717 mem_cgroup_swapout(page, swap);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800718 __delete_from_swap_cache(page);
Greg Thelenc4843a72015-05-22 17:13:16 -0400719 spin_unlock_irqrestore(&mapping->tree_lock, flags);
Minchan Kim75f6d6d2017-07-06 15:37:21 -0700720 put_swap_page(page, swap);
Nick Piggine2867812008-07-25 19:45:30 -0700721 } else {
Linus Torvalds6072d132010-12-01 13:35:19 -0500722 void (*freepage)(struct page *);
Johannes Weinera5289102014-04-03 14:47:51 -0700723 void *shadow = NULL;
Linus Torvalds6072d132010-12-01 13:35:19 -0500724
725 freepage = mapping->a_ops->freepage;
Johannes Weinera5289102014-04-03 14:47:51 -0700726 /*
727 * Remember a shadow entry for reclaimed file cache in
728 * order to detect refaults, thus thrashing, later on.
729 *
730 * But don't store shadows in an address space that is
731 * already exiting. This is not just an optizimation,
732 * inode reclaim needs to empty out the radix tree or
733 * the nodes are lost. Don't plant shadows behind its
734 * back.
Ross Zwislerf9fe48b2016-01-22 15:10:40 -0800735 *
736 * We also don't store shadows for DAX mappings because the
737 * only page cache pages found in these are zero pages
738 * covering holes, and because we don't want to mix DAX
739 * exceptional entries and shadow exceptional entries in the
740 * same page_tree.
Johannes Weinera5289102014-04-03 14:47:51 -0700741 */
742 if (reclaimed && page_is_file_cache(page) &&
Ross Zwislerf9fe48b2016-01-22 15:10:40 -0800743 !mapping_exiting(mapping) && !dax_mapping(mapping))
Johannes Weinera5289102014-04-03 14:47:51 -0700744 shadow = workingset_eviction(mapping, page);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700745 __delete_from_page_cache(page, shadow);
Greg Thelenc4843a72015-05-22 17:13:16 -0400746 spin_unlock_irqrestore(&mapping->tree_lock, flags);
Linus Torvalds6072d132010-12-01 13:35:19 -0500747
748 if (freepage != NULL)
749 freepage(page);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800750 }
751
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800752 return 1;
753
754cannot_free:
Greg Thelenc4843a72015-05-22 17:13:16 -0400755 spin_unlock_irqrestore(&mapping->tree_lock, flags);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800756 return 0;
757}
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759/*
Nick Piggine2867812008-07-25 19:45:30 -0700760 * Attempt to detach a locked page from its ->mapping. If it is dirty or if
761 * someone else has a ref on the page, abort and return 0. If it was
762 * successfully detached, return 1. Assumes the caller has a single ref on
763 * this page.
764 */
765int remove_mapping(struct address_space *mapping, struct page *page)
766{
Johannes Weinera5289102014-04-03 14:47:51 -0700767 if (__remove_mapping(mapping, page, false)) {
Nick Piggine2867812008-07-25 19:45:30 -0700768 /*
769 * Unfreezing the refcount with 1 rather than 2 effectively
770 * drops the pagecache ref for us without requiring another
771 * atomic operation.
772 */
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700773 page_ref_unfreeze(page, 1);
Nick Piggine2867812008-07-25 19:45:30 -0700774 return 1;
775 }
776 return 0;
777}
778
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700779/**
780 * putback_lru_page - put previously isolated page onto appropriate LRU list
781 * @page: page to be put back to appropriate lru list
782 *
783 * Add previously isolated @page to appropriate LRU list.
784 * Page may still be unevictable for other reasons.
785 *
786 * lru_lock must not be held, interrupts must be enabled.
787 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700788void putback_lru_page(struct page *page)
789{
Vlastimil Babka0ec3b742013-09-11 14:22:26 -0700790 bool is_unevictable;
Lee Schermerhornbbfd28e2008-10-18 20:26:40 -0700791 int was_unevictable = PageUnevictable(page);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700792
Sasha Levin309381fea2014-01-23 15:52:54 -0800793 VM_BUG_ON_PAGE(PageLRU(page), page);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700794
795redo:
796 ClearPageUnevictable(page);
797
Hugh Dickins39b5f292012-10-08 16:33:18 -0700798 if (page_evictable(page)) {
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700799 /*
800 * For evictable pages, we can use the cache.
801 * In event of a race, worst case is we end up with an
802 * unevictable page on [in]active list.
803 * We know how to handle that.
804 */
Vlastimil Babka0ec3b742013-09-11 14:22:26 -0700805 is_unevictable = false;
Mel Gormanc53954a2013-07-03 15:02:34 -0700806 lru_cache_add(page);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700807 } else {
808 /*
809 * Put unevictable pages directly on zone's unevictable
810 * list.
811 */
Vlastimil Babka0ec3b742013-09-11 14:22:26 -0700812 is_unevictable = true;
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700813 add_page_to_unevictable_list(page);
Johannes Weiner6a7b9542009-10-26 16:50:00 -0700814 /*
Minchan Kim21ee9f32011-10-31 17:09:28 -0700815 * When racing with an mlock or AS_UNEVICTABLE clearing
816 * (page is unlocked) make sure that if the other thread
817 * does not observe our setting of PG_lru and fails
Hugh Dickins24513262012-01-20 14:34:21 -0800818 * isolation/check_move_unevictable_pages,
Minchan Kim21ee9f32011-10-31 17:09:28 -0700819 * we see PG_mlocked/AS_UNEVICTABLE cleared below and move
Johannes Weiner6a7b9542009-10-26 16:50:00 -0700820 * the page back to the evictable list.
821 *
Minchan Kim21ee9f32011-10-31 17:09:28 -0700822 * The other side is TestClearPageMlocked() or shmem_lock().
Johannes Weiner6a7b9542009-10-26 16:50:00 -0700823 */
824 smp_mb();
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700825 }
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700826
827 /*
828 * page's status can change while we move it among lru. If an evictable
829 * page is on unevictable list, it never be freed. To avoid that,
830 * check after we added it to the list, again.
831 */
Vlastimil Babka0ec3b742013-09-11 14:22:26 -0700832 if (is_unevictable && page_evictable(page)) {
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700833 if (!isolate_lru_page(page)) {
834 put_page(page);
835 goto redo;
836 }
837 /* This means someone else dropped this page from LRU
838 * So, it will be freed or putback to LRU again. There is
839 * nothing to do here.
840 */
841 }
842
Vlastimil Babka0ec3b742013-09-11 14:22:26 -0700843 if (was_unevictable && !is_unevictable)
Lee Schermerhornbbfd28e2008-10-18 20:26:40 -0700844 count_vm_event(UNEVICTABLE_PGRESCUED);
Vlastimil Babka0ec3b742013-09-11 14:22:26 -0700845 else if (!was_unevictable && is_unevictable)
Lee Schermerhornbbfd28e2008-10-18 20:26:40 -0700846 count_vm_event(UNEVICTABLE_PGCULLED);
847
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700848 put_page(page); /* drop ref from isolate */
849}
850
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800851enum page_references {
852 PAGEREF_RECLAIM,
853 PAGEREF_RECLAIM_CLEAN,
Johannes Weiner645747462010-03-05 13:42:22 -0800854 PAGEREF_KEEP,
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800855 PAGEREF_ACTIVATE,
856};
857
858static enum page_references page_check_references(struct page *page,
859 struct scan_control *sc)
860{
Johannes Weiner645747462010-03-05 13:42:22 -0800861 int referenced_ptes, referenced_page;
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800862 unsigned long vm_flags;
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800863
Johannes Weinerc3ac9a82012-05-29 15:06:25 -0700864 referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup,
865 &vm_flags);
Johannes Weiner645747462010-03-05 13:42:22 -0800866 referenced_page = TestClearPageReferenced(page);
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800867
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800868 /*
869 * Mlock lost the isolation race with us. Let try_to_unmap()
870 * move the page to the unevictable list.
871 */
872 if (vm_flags & VM_LOCKED)
873 return PAGEREF_RECLAIM;
874
Johannes Weiner645747462010-03-05 13:42:22 -0800875 if (referenced_ptes) {
Michal Hockoe48982732012-05-29 15:06:45 -0700876 if (PageSwapBacked(page))
Johannes Weiner645747462010-03-05 13:42:22 -0800877 return PAGEREF_ACTIVATE;
878 /*
879 * All mapped pages start out with page table
880 * references from the instantiating fault, so we need
881 * to look twice if a mapped file page is used more
882 * than once.
883 *
884 * Mark it and spare it for another trip around the
885 * inactive list. Another page table reference will
886 * lead to its activation.
887 *
888 * Note: the mark is set for activated pages as well
889 * so that recently deactivated but used pages are
890 * quickly recovered.
891 */
892 SetPageReferenced(page);
893
Konstantin Khlebnikov34dbc672012-01-10 15:06:59 -0800894 if (referenced_page || referenced_ptes > 1)
Johannes Weiner645747462010-03-05 13:42:22 -0800895 return PAGEREF_ACTIVATE;
896
Konstantin Khlebnikovc909e992012-01-10 15:07:03 -0800897 /*
898 * Activate file-backed executable pages after first usage.
899 */
900 if (vm_flags & VM_EXEC)
901 return PAGEREF_ACTIVATE;
902
Johannes Weiner645747462010-03-05 13:42:22 -0800903 return PAGEREF_KEEP;
904 }
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800905
906 /* Reclaim if clean, defer dirty pages to writeback */
KOSAKI Motohiro2e302442010-10-26 14:21:46 -0700907 if (referenced_page && !PageSwapBacked(page))
Johannes Weiner645747462010-03-05 13:42:22 -0800908 return PAGEREF_RECLAIM_CLEAN;
909
910 return PAGEREF_RECLAIM;
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800911}
912
Mel Gormane2be15f2013-07-03 15:01:57 -0700913/* Check if a page is dirty or under writeback */
914static void page_check_dirty_writeback(struct page *page,
915 bool *dirty, bool *writeback)
916{
Mel Gormanb4597222013-07-03 15:02:05 -0700917 struct address_space *mapping;
918
Mel Gormane2be15f2013-07-03 15:01:57 -0700919 /*
920 * Anonymous pages are not handled by flushers and must be written
921 * from reclaim context. Do not stall reclaim based on them
922 */
Shaohua Li802a3a92017-05-03 14:52:32 -0700923 if (!page_is_file_cache(page) ||
924 (PageAnon(page) && !PageSwapBacked(page))) {
Mel Gormane2be15f2013-07-03 15:01:57 -0700925 *dirty = false;
926 *writeback = false;
927 return;
928 }
929
930 /* By default assume that the page flags are accurate */
931 *dirty = PageDirty(page);
932 *writeback = PageWriteback(page);
Mel Gormanb4597222013-07-03 15:02:05 -0700933
934 /* Verify dirty/writeback state if the filesystem supports it */
935 if (!page_has_private(page))
936 return;
937
938 mapping = page_mapping(page);
939 if (mapping && mapping->a_ops->is_dirty_writeback)
940 mapping->a_ops->is_dirty_writeback(page, dirty, writeback);
Mel Gormane2be15f2013-07-03 15:01:57 -0700941}
942
Michal Hocko3c710c12017-02-22 15:44:27 -0800943struct reclaim_stat {
944 unsigned nr_dirty;
945 unsigned nr_unqueued_dirty;
946 unsigned nr_congested;
947 unsigned nr_writeback;
948 unsigned nr_immediate;
Michal Hocko5bccd162017-02-22 15:44:30 -0800949 unsigned nr_activate;
950 unsigned nr_ref_keep;
951 unsigned nr_unmap_fail;
Michal Hocko3c710c12017-02-22 15:44:27 -0800952};
953
Nick Piggine2867812008-07-25 19:45:30 -0700954/*
Andrew Morton1742f192006-03-22 00:08:21 -0800955 * shrink_page_list() returns the number of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 */
Andrew Morton1742f192006-03-22 00:08:21 -0800957static unsigned long shrink_page_list(struct list_head *page_list,
Mel Gorman599d0c92016-07-28 15:45:31 -0700958 struct pglist_data *pgdat,
Mel Gormanf84f6e22011-10-31 17:07:51 -0700959 struct scan_control *sc,
Minchan Kim02c6de82012-10-08 16:31:55 -0700960 enum ttu_flags ttu_flags,
Michal Hocko3c710c12017-02-22 15:44:27 -0800961 struct reclaim_stat *stat,
Minchan Kim02c6de82012-10-08 16:31:55 -0700962 bool force_reclaim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
964 LIST_HEAD(ret_pages);
Mel Gormanabe4c3b2010-08-09 17:19:31 -0700965 LIST_HEAD(free_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 int pgactivate = 0;
Michal Hocko3c710c12017-02-22 15:44:27 -0800967 unsigned nr_unqueued_dirty = 0;
968 unsigned nr_dirty = 0;
969 unsigned nr_congested = 0;
970 unsigned nr_reclaimed = 0;
971 unsigned nr_writeback = 0;
972 unsigned nr_immediate = 0;
Michal Hocko5bccd162017-02-22 15:44:30 -0800973 unsigned nr_ref_keep = 0;
974 unsigned nr_unmap_fail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 cond_resched();
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 while (!list_empty(page_list)) {
979 struct address_space *mapping;
980 struct page *page;
981 int may_enter_fs;
Minchan Kim02c6de82012-10-08 16:31:55 -0700982 enum page_references references = PAGEREF_RECLAIM_CLEAN;
Mel Gormane2be15f2013-07-03 15:01:57 -0700983 bool dirty, writeback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 cond_resched();
986
987 page = lru_to_page(page_list);
988 list_del(&page->lru);
989
Nick Piggin529ae9a2008-08-02 12:01:03 +0200990 if (!trylock_page(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 goto keep;
992
Sasha Levin309381fea2014-01-23 15:52:54 -0800993 VM_BUG_ON_PAGE(PageActive(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 sc->nr_scanned++;
Christoph Lameter80e43422006-02-11 17:55:53 -0800996
Hugh Dickins39b5f292012-10-08 16:33:18 -0700997 if (unlikely(!page_evictable(page)))
Minchan Kimad6b6702017-05-03 14:54:13 -0700998 goto activate_locked;
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700999
Johannes Weinera6dc60f82009-03-31 15:19:30 -07001000 if (!sc->may_unmap && page_mapped(page))
Christoph Lameter80e43422006-02-11 17:55:53 -08001001 goto keep_locked;
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 /* Double the slab pressure for mapped and swapcache pages */
Shaohua Li802a3a92017-05-03 14:52:32 -07001004 if ((page_mapped(page) || PageSwapCache(page)) &&
1005 !(PageAnon(page) && !PageSwapBacked(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 sc->nr_scanned++;
1007
Andy Whitcroftc661b072007-08-22 14:01:26 -07001008 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
1009 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
1010
Mel Gorman283aba92013-07-03 15:01:51 -07001011 /*
Mel Gormane2be15f2013-07-03 15:01:57 -07001012 * The number of dirty pages determines if a zone is marked
1013 * reclaim_congested which affects wait_iff_congested. kswapd
1014 * will stall and start writing pages if the tail of the LRU
1015 * is all dirty unqueued pages.
1016 */
1017 page_check_dirty_writeback(page, &dirty, &writeback);
1018 if (dirty || writeback)
1019 nr_dirty++;
1020
1021 if (dirty && !writeback)
1022 nr_unqueued_dirty++;
1023
Mel Gormand04e8ac2013-07-03 15:02:03 -07001024 /*
1025 * Treat this page as congested if the underlying BDI is or if
1026 * pages are cycling through the LRU so quickly that the
1027 * pages marked for immediate reclaim are making it to the
1028 * end of the LRU a second time.
1029 */
Mel Gormane2be15f2013-07-03 15:01:57 -07001030 mapping = page_mapping(page);
Jamie Liu1da58ee2014-12-10 15:43:20 -08001031 if (((dirty || writeback) && mapping &&
Tejun Heo703c2702015-05-22 17:13:44 -04001032 inode_write_congested(mapping->host)) ||
Mel Gormand04e8ac2013-07-03 15:02:03 -07001033 (writeback && PageReclaim(page)))
Mel Gormane2be15f2013-07-03 15:01:57 -07001034 nr_congested++;
1035
1036 /*
Mel Gorman283aba92013-07-03 15:01:51 -07001037 * If a page at the tail of the LRU is under writeback, there
1038 * are three cases to consider.
1039 *
1040 * 1) If reclaim is encountering an excessive number of pages
1041 * under writeback and this page is both under writeback and
1042 * PageReclaim then it indicates that pages are being queued
1043 * for IO but are being recycled through the LRU before the
1044 * IO can complete. Waiting on the page itself risks an
1045 * indefinite stall if it is impossible to writeback the
1046 * page due to IO error or disconnected storage so instead
Mel Gormanb1a6f212013-07-03 15:01:58 -07001047 * note that the LRU is being scanned too quickly and the
1048 * caller can stall after page list has been processed.
Mel Gorman283aba92013-07-03 15:01:51 -07001049 *
Tejun Heo97c93412015-05-22 18:23:36 -04001050 * 2) Global or new memcg reclaim encounters a page that is
Michal Hockoecf5fc62015-08-04 14:36:58 -07001051 * not marked for immediate reclaim, or the caller does not
1052 * have __GFP_FS (or __GFP_IO if it's simply going to swap,
1053 * not to fs). In this case mark the page for immediate
Tejun Heo97c93412015-05-22 18:23:36 -04001054 * reclaim and continue scanning.
Mel Gorman283aba92013-07-03 15:01:51 -07001055 *
Michal Hockoecf5fc62015-08-04 14:36:58 -07001056 * Require may_enter_fs because we would wait on fs, which
1057 * may not have submitted IO yet. And the loop driver might
Mel Gorman283aba92013-07-03 15:01:51 -07001058 * enter reclaim, and deadlock if it waits on a page for
1059 * which it is needed to do the write (loop masks off
1060 * __GFP_IO|__GFP_FS for this reason); but more thought
1061 * would probably show more reasons.
1062 *
Hugh Dickins7fadc822015-09-08 15:03:46 -07001063 * 3) Legacy memcg encounters a page that is already marked
Mel Gorman283aba92013-07-03 15:01:51 -07001064 * PageReclaim. memcg does not have any dirty pages
1065 * throttling so we could easily OOM just because too many
1066 * pages are in writeback and there is nothing else to
1067 * reclaim. Wait for the writeback to complete.
Johannes Weinerc55e8d02017-02-24 14:56:23 -08001068 *
1069 * In cases 1) and 2) we activate the pages to get them out of
1070 * the way while we continue scanning for clean pages on the
1071 * inactive list and refilling from the active list. The
1072 * observation here is that waiting for disk writes is more
1073 * expensive than potentially causing reloads down the line.
1074 * Since they're marked for immediate reclaim, they won't put
1075 * memory pressure on the cache working set any longer than it
1076 * takes to write them to disk.
Mel Gorman283aba92013-07-03 15:01:51 -07001077 */
Andy Whitcroftc661b072007-08-22 14:01:26 -07001078 if (PageWriteback(page)) {
Mel Gorman283aba92013-07-03 15:01:51 -07001079 /* Case 1 above */
1080 if (current_is_kswapd() &&
1081 PageReclaim(page) &&
Mel Gorman599d0c92016-07-28 15:45:31 -07001082 test_bit(PGDAT_WRITEBACK, &pgdat->flags)) {
Mel Gormanb1a6f212013-07-03 15:01:58 -07001083 nr_immediate++;
Johannes Weinerc55e8d02017-02-24 14:56:23 -08001084 goto activate_locked;
Mel Gorman283aba92013-07-03 15:01:51 -07001085
1086 /* Case 2 above */
Tejun Heo97c93412015-05-22 18:23:36 -04001087 } else if (sane_reclaim(sc) ||
Michal Hockoecf5fc62015-08-04 14:36:58 -07001088 !PageReclaim(page) || !may_enter_fs) {
Hugh Dickinsc3b94f42012-07-31 16:45:59 -07001089 /*
1090 * This is slightly racy - end_page_writeback()
1091 * might have just cleared PageReclaim, then
1092 * setting PageReclaim here end up interpreted
1093 * as PageReadahead - but that does not matter
1094 * enough to care. What we do want is for this
1095 * page to have PageReclaim set next time memcg
1096 * reclaim reaches the tests above, so it will
1097 * then wait_on_page_writeback() to avoid OOM;
1098 * and it's also appropriate in global reclaim.
1099 */
1100 SetPageReclaim(page);
Michal Hockoe62e3842012-07-31 16:45:55 -07001101 nr_writeback++;
Johannes Weinerc55e8d02017-02-24 14:56:23 -08001102 goto activate_locked;
Mel Gorman283aba92013-07-03 15:01:51 -07001103
1104 /* Case 3 above */
1105 } else {
Hugh Dickins7fadc822015-09-08 15:03:46 -07001106 unlock_page(page);
Mel Gorman283aba92013-07-03 15:01:51 -07001107 wait_on_page_writeback(page);
Hugh Dickins7fadc822015-09-08 15:03:46 -07001108 /* then go back and try same page again */
1109 list_add_tail(&page->lru, page_list);
1110 continue;
Michal Hockoe62e3842012-07-31 16:45:55 -07001111 }
Andy Whitcroftc661b072007-08-22 14:01:26 -07001112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Minchan Kim02c6de82012-10-08 16:31:55 -07001114 if (!force_reclaim)
1115 references = page_check_references(page, sc);
1116
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001117 switch (references) {
1118 case PAGEREF_ACTIVATE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 goto activate_locked;
Johannes Weiner645747462010-03-05 13:42:22 -08001120 case PAGEREF_KEEP:
Michal Hocko5bccd162017-02-22 15:44:30 -08001121 nr_ref_keep++;
Johannes Weiner645747462010-03-05 13:42:22 -08001122 goto keep_locked;
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001123 case PAGEREF_RECLAIM:
1124 case PAGEREF_RECLAIM_CLEAN:
1125 ; /* try to reclaim the page below */
1126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 /*
1129 * Anonymous process memory has backing store?
1130 * Try to allocate it some swap space here.
Shaohua Li802a3a92017-05-03 14:52:32 -07001131 * Lazyfree page could be freed directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 */
Huang Yingbd4c82c22017-09-06 16:22:49 -07001133 if (PageAnon(page) && PageSwapBacked(page)) {
1134 if (!PageSwapCache(page)) {
1135 if (!(sc->gfp_mask & __GFP_IO))
1136 goto keep_locked;
1137 if (PageTransHuge(page)) {
1138 /* cannot split THP, skip it */
1139 if (!can_split_huge_page(page, NULL))
1140 goto activate_locked;
1141 /*
1142 * Split pages without a PMD map right
1143 * away. Chances are some or all of the
1144 * tail pages can be freed without IO.
1145 */
1146 if (!compound_mapcount(page) &&
1147 split_huge_page_to_list(page,
1148 page_list))
1149 goto activate_locked;
1150 }
1151 if (!add_to_swap(page)) {
1152 if (!PageTransHuge(page))
1153 goto activate_locked;
1154 /* Fallback to swap normal pages */
1155 if (split_huge_page_to_list(page,
1156 page_list))
1157 goto activate_locked;
Huang Yingfe490cc2017-09-06 16:22:52 -07001158#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1159 count_vm_event(THP_SWPOUT_FALLBACK);
1160#endif
Huang Yingbd4c82c22017-09-06 16:22:49 -07001161 if (!add_to_swap(page))
1162 goto activate_locked;
1163 }
Minchan Kim0f074652017-07-06 15:37:24 -07001164
Huang Yingbd4c82c22017-09-06 16:22:49 -07001165 may_enter_fs = 1;
1166
1167 /* Adding to swap updated mapping */
1168 mapping = page_mapping(page);
Minchan Kim0f074652017-07-06 15:37:24 -07001169 }
Kirill A. Shutemov7751b2d2016-07-26 15:25:56 -07001170 } else if (unlikely(PageTransHuge(page))) {
1171 /* Split file THP */
1172 if (split_huge_page_to_list(page, page_list))
1173 goto keep_locked;
Mel Gormane2be15f2013-07-03 15:01:57 -07001174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 /*
1177 * The page is mapped into the page tables of one or more
1178 * processes. Try to unmap it here.
1179 */
Shaohua Li802a3a92017-05-03 14:52:32 -07001180 if (page_mapped(page)) {
Huang Yingbd4c82c22017-09-06 16:22:49 -07001181 enum ttu_flags flags = ttu_flags | TTU_BATCH_FLUSH;
1182
1183 if (unlikely(PageTransHuge(page)))
1184 flags |= TTU_SPLIT_HUGE_PMD;
1185 if (!try_to_unmap(page, flags)) {
Michal Hocko5bccd162017-02-22 15:44:30 -08001186 nr_unmap_fail++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 goto activate_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189 }
1190
1191 if (PageDirty(page)) {
Mel Gormanee728862011-10-31 17:07:38 -07001192 /*
Johannes Weiner4eda4822017-02-24 14:56:20 -08001193 * Only kswapd can writeback filesystem pages
1194 * to avoid risk of stack overflow. But avoid
1195 * injecting inefficient single-page IO into
1196 * flusher writeback as much as possible: only
1197 * write pages when we've encountered many
1198 * dirty pages, and when we've already scanned
1199 * the rest of the LRU for clean pages and see
1200 * the same dirty pages again (PageReclaim).
Mel Gormanee728862011-10-31 17:07:38 -07001201 */
Mel Gormanf84f6e22011-10-31 17:07:51 -07001202 if (page_is_file_cache(page) &&
Johannes Weiner4eda4822017-02-24 14:56:20 -08001203 (!current_is_kswapd() || !PageReclaim(page) ||
1204 !test_bit(PGDAT_DIRTY, &pgdat->flags))) {
Mel Gorman49ea7eb2011-10-31 17:07:59 -07001205 /*
1206 * Immediately reclaim when written back.
1207 * Similar in principal to deactivate_page()
1208 * except we already have the page isolated
1209 * and know it's dirty
1210 */
Mel Gormanc4a25632016-07-28 15:46:23 -07001211 inc_node_page_state(page, NR_VMSCAN_IMMEDIATE);
Mel Gorman49ea7eb2011-10-31 17:07:59 -07001212 SetPageReclaim(page);
1213
Johannes Weinerc55e8d02017-02-24 14:56:23 -08001214 goto activate_locked;
Mel Gormanee728862011-10-31 17:07:38 -07001215 }
1216
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001217 if (references == PAGEREF_RECLAIM_CLEAN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 goto keep_locked;
Andrew Morton4dd4b922008-03-24 12:29:52 -07001219 if (!may_enter_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 goto keep_locked;
Christoph Lameter52a83632006-02-01 03:05:28 -08001221 if (!sc->may_writepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 goto keep_locked;
1223
Mel Gormand950c942015-09-04 15:47:35 -07001224 /*
1225 * Page is dirty. Flush the TLB if a writable entry
1226 * potentially exists to avoid CPU writes after IO
1227 * starts and then write it out here.
1228 */
1229 try_to_unmap_flush_dirty();
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -07001230 switch (pageout(page, mapping, sc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 case PAGE_KEEP:
1232 goto keep_locked;
1233 case PAGE_ACTIVATE:
1234 goto activate_locked;
1235 case PAGE_SUCCESS:
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -07001236 if (PageWriteback(page))
Mel Gorman41ac1992012-05-29 15:06:19 -07001237 goto keep;
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -07001238 if (PageDirty(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 goto keep;
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -07001240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 /*
1242 * A synchronous write - probably a ramdisk. Go
1243 * ahead and try to reclaim the page.
1244 */
Nick Piggin529ae9a2008-08-02 12:01:03 +02001245 if (!trylock_page(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 goto keep;
1247 if (PageDirty(page) || PageWriteback(page))
1248 goto keep_locked;
1249 mapping = page_mapping(page);
1250 case PAGE_CLEAN:
1251 ; /* try to free the page below */
1252 }
1253 }
1254
1255 /*
1256 * If the page has buffers, try to free the buffer mappings
1257 * associated with this page. If we succeed we try to free
1258 * the page as well.
1259 *
1260 * We do this even if the page is PageDirty().
1261 * try_to_release_page() does not perform I/O, but it is
1262 * possible for a page to have PageDirty set, but it is actually
1263 * clean (all its buffers are clean). This happens if the
1264 * buffers were written out directly, with submit_bh(). ext3
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001265 * will do this, as well as the blockdev mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 * try_to_release_page() will discover that cleanness and will
1267 * drop the buffers and mark the page clean - it can be freed.
1268 *
1269 * Rarely, pages can have buffers and no ->mapping. These are
1270 * the pages which were not successfully invalidated in
1271 * truncate_complete_page(). We try to drop those buffers here
1272 * and if that worked, and the page is no longer mapped into
1273 * process address space (page_count == 1) it can be freed.
1274 * Otherwise, leave the page on the LRU so it is swappable.
1275 */
David Howells266cf652009-04-03 16:42:36 +01001276 if (page_has_private(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (!try_to_release_page(page, sc->gfp_mask))
1278 goto activate_locked;
Nick Piggine2867812008-07-25 19:45:30 -07001279 if (!mapping && page_count(page) == 1) {
1280 unlock_page(page);
1281 if (put_page_testzero(page))
1282 goto free_it;
1283 else {
1284 /*
1285 * rare race with speculative reference.
1286 * the speculative reference will free
1287 * this page shortly, so we may
1288 * increment nr_reclaimed here (and
1289 * leave it off the LRU).
1290 */
1291 nr_reclaimed++;
1292 continue;
1293 }
1294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296
Shaohua Li802a3a92017-05-03 14:52:32 -07001297 if (PageAnon(page) && !PageSwapBacked(page)) {
1298 /* follow __remove_mapping for reference */
1299 if (!page_ref_freeze(page, 1))
1300 goto keep_locked;
1301 if (PageDirty(page)) {
1302 page_ref_unfreeze(page, 1);
1303 goto keep_locked;
1304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Shaohua Li802a3a92017-05-03 14:52:32 -07001306 count_vm_event(PGLAZYFREED);
Roman Gushchin22621852017-07-06 15:40:25 -07001307 count_memcg_page_event(page, PGLAZYFREED);
Shaohua Li802a3a92017-05-03 14:52:32 -07001308 } else if (!mapping || !__remove_mapping(mapping, page, true))
1309 goto keep_locked;
Nick Piggina978d6f2008-10-18 20:26:58 -07001310 /*
1311 * At this point, we have no other references and there is
1312 * no way to pick any more up (removed from LRU, removed
1313 * from pagecache). Can use non-atomic bitops now (and
1314 * we obviously don't have to worry about waking up a process
1315 * waiting on the page lock, because there are no references.
1316 */
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -08001317 __ClearPageLocked(page);
Nick Piggine2867812008-07-25 19:45:30 -07001318free_it:
Andrew Morton05ff5132006-03-22 00:08:20 -08001319 nr_reclaimed++;
Mel Gormanabe4c3b2010-08-09 17:19:31 -07001320
1321 /*
1322 * Is there need to periodically free_page_list? It would
1323 * appear not as the counts should be low
1324 */
Huang Yingbd4c82c22017-09-06 16:22:49 -07001325 if (unlikely(PageTransHuge(page))) {
1326 mem_cgroup_uncharge(page);
1327 (*get_compound_page_dtor(page))(page);
1328 } else
1329 list_add(&page->lru, &free_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 continue;
1331
1332activate_locked:
Rik van Riel68a223942008-10-18 20:26:23 -07001333 /* Not a candidate for swapping, so reclaim swap space. */
Minchan Kimad6b6702017-05-03 14:54:13 -07001334 if (PageSwapCache(page) && (mem_cgroup_swap_full(page) ||
1335 PageMlocked(page)))
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -08001336 try_to_free_swap(page);
Sasha Levin309381fea2014-01-23 15:52:54 -08001337 VM_BUG_ON_PAGE(PageActive(page), page);
Minchan Kimad6b6702017-05-03 14:54:13 -07001338 if (!PageMlocked(page)) {
1339 SetPageActive(page);
1340 pgactivate++;
Roman Gushchin22621852017-07-06 15:40:25 -07001341 count_memcg_page_event(page, PGACTIVATE);
Minchan Kimad6b6702017-05-03 14:54:13 -07001342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343keep_locked:
1344 unlock_page(page);
1345keep:
1346 list_add(&page->lru, &ret_pages);
Sasha Levin309381fea2014-01-23 15:52:54 -08001347 VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
Mel Gormanabe4c3b2010-08-09 17:19:31 -07001349
Johannes Weiner747db952014-08-08 14:19:24 -07001350 mem_cgroup_uncharge_list(&free_pages);
Mel Gorman72b252a2015-09-04 15:47:32 -07001351 try_to_unmap_flush();
Mel Gorman2d4894b2017-11-15 17:37:59 -08001352 free_unref_page_list(&free_pages);
Mel Gormanabe4c3b2010-08-09 17:19:31 -07001353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 list_splice(&ret_pages, page_list);
Christoph Lameterf8891e52006-06-30 01:55:45 -07001355 count_vm_events(PGACTIVATE, pgactivate);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07001356
Michal Hocko3c710c12017-02-22 15:44:27 -08001357 if (stat) {
1358 stat->nr_dirty = nr_dirty;
1359 stat->nr_congested = nr_congested;
1360 stat->nr_unqueued_dirty = nr_unqueued_dirty;
1361 stat->nr_writeback = nr_writeback;
1362 stat->nr_immediate = nr_immediate;
Michal Hocko5bccd162017-02-22 15:44:30 -08001363 stat->nr_activate = pgactivate;
1364 stat->nr_ref_keep = nr_ref_keep;
1365 stat->nr_unmap_fail = nr_unmap_fail;
Michal Hocko3c710c12017-02-22 15:44:27 -08001366 }
Andrew Morton05ff5132006-03-22 00:08:20 -08001367 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
Minchan Kim02c6de82012-10-08 16:31:55 -07001370unsigned long reclaim_clean_pages_from_list(struct zone *zone,
1371 struct list_head *page_list)
1372{
1373 struct scan_control sc = {
1374 .gfp_mask = GFP_KERNEL,
1375 .priority = DEF_PRIORITY,
1376 .may_unmap = 1,
1377 };
Michal Hocko3c710c12017-02-22 15:44:27 -08001378 unsigned long ret;
Minchan Kim02c6de82012-10-08 16:31:55 -07001379 struct page *page, *next;
1380 LIST_HEAD(clean_pages);
1381
1382 list_for_each_entry_safe(page, next, page_list, lru) {
Rafael Aquini117aad12013-09-30 13:45:16 -07001383 if (page_is_file_cache(page) && !PageDirty(page) &&
Minchan Kimb1123ea62016-07-26 15:23:09 -07001384 !__PageMovable(page)) {
Minchan Kim02c6de82012-10-08 16:31:55 -07001385 ClearPageActive(page);
1386 list_move(&page->lru, &clean_pages);
1387 }
1388 }
1389
Mel Gorman599d0c92016-07-28 15:45:31 -07001390 ret = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc,
Shaohua Lia128ca72017-05-03 14:52:22 -07001391 TTU_IGNORE_ACCESS, NULL, true);
Minchan Kim02c6de82012-10-08 16:31:55 -07001392 list_splice(&clean_pages, page_list);
Mel Gorman599d0c92016-07-28 15:45:31 -07001393 mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, -ret);
Minchan Kim02c6de82012-10-08 16:31:55 -07001394 return ret;
1395}
1396
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001397/*
1398 * Attempt to remove the specified page from its LRU. Only take this page
1399 * if it is of the appropriate PageActive status. Pages which are being
1400 * freed elsewhere are also ignored.
1401 *
1402 * page: page to consider
1403 * mode: one of the LRU isolation modes defined above
1404 *
1405 * returns 0 on success, -ve errno on failure.
1406 */
Konstantin Khlebnikovf3fd4a62012-05-29 15:06:54 -07001407int __isolate_lru_page(struct page *page, isolate_mode_t mode)
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001408{
1409 int ret = -EINVAL;
1410
1411 /* Only take pages on the LRU. */
1412 if (!PageLRU(page))
1413 return ret;
1414
Minchan Kime46a2872012-10-08 16:33:48 -07001415 /* Compaction should not handle unevictable pages but CMA can do so */
1416 if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE))
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001417 return ret;
1418
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001419 ret = -EBUSY;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001420
Mel Gormanc8244932012-01-12 17:19:38 -08001421 /*
1422 * To minimise LRU disruption, the caller can indicate that it only
1423 * wants to isolate pages it will be able to operate on without
1424 * blocking - clean pages for the most part.
1425 *
Mel Gormanc8244932012-01-12 17:19:38 -08001426 * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages
1427 * that it is possible to migrate without blocking
1428 */
Johannes Weiner1276ad62017-02-24 14:56:11 -08001429 if (mode & ISOLATE_ASYNC_MIGRATE) {
Mel Gormanc8244932012-01-12 17:19:38 -08001430 /* All the caller can do on PageWriteback is block */
1431 if (PageWriteback(page))
1432 return ret;
1433
1434 if (PageDirty(page)) {
1435 struct address_space *mapping;
1436
Mel Gormanc8244932012-01-12 17:19:38 -08001437 /*
1438 * Only pages without mappings or that have a
1439 * ->migratepage callback are possible to migrate
1440 * without blocking
1441 */
1442 mapping = page_mapping(page);
1443 if (mapping && !mapping->a_ops->migratepage)
1444 return ret;
1445 }
1446 }
Minchan Kim39deaf82011-10-31 17:06:51 -07001447
Minchan Kimf80c0672011-10-31 17:06:55 -07001448 if ((mode & ISOLATE_UNMAPPED) && page_mapped(page))
1449 return ret;
1450
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001451 if (likely(get_page_unless_zero(page))) {
1452 /*
1453 * Be careful not to clear PageLRU until after we're
1454 * sure the page is not being freed elsewhere -- the
1455 * page release code relies on it.
1456 */
1457 ClearPageLRU(page);
1458 ret = 0;
1459 }
1460
1461 return ret;
1462}
1463
Mel Gorman7ee36a12016-07-28 15:47:17 -07001464
1465/*
1466 * Update LRU sizes after isolating pages. The LRU size updates must
1467 * be complete before mem_cgroup_update_lru_size due to a santity check.
1468 */
1469static __always_inline void update_lru_sizes(struct lruvec *lruvec,
Michal Hockob4536f0c82017-01-10 16:58:04 -08001470 enum lru_list lru, unsigned long *nr_zone_taken)
Mel Gorman7ee36a12016-07-28 15:47:17 -07001471{
Mel Gorman7ee36a12016-07-28 15:47:17 -07001472 int zid;
1473
Mel Gorman7ee36a12016-07-28 15:47:17 -07001474 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
1475 if (!nr_zone_taken[zid])
1476 continue;
1477
1478 __update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]);
Michal Hockob4536f0c82017-01-10 16:58:04 -08001479#ifdef CONFIG_MEMCG
1480 mem_cgroup_update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]);
1481#endif
Mel Gorman7ee36a12016-07-28 15:47:17 -07001482 }
Mel Gorman7ee36a12016-07-28 15:47:17 -07001483
Mel Gorman7ee36a12016-07-28 15:47:17 -07001484}
1485
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001486/*
Mel Gormana52633d2016-07-28 15:45:28 -07001487 * zone_lru_lock is heavily contended. Some of the functions that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 * shrink the lists perform better by taking out a batch of pages
1489 * and working on them outside the LRU lock.
1490 *
1491 * For pagecache intensive workloads, this function is the hottest
1492 * spot in the kernel (apart from copy_*_user functions).
1493 *
1494 * Appropriate locks must be held before calling this function.
1495 *
Minchan Kim791b48b2017-05-12 15:47:06 -07001496 * @nr_to_scan: The number of eligible pages to look through on the list.
Konstantin Khlebnikov5dc35972012-05-29 15:06:58 -07001497 * @lruvec: The LRU vector to pull pages from.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 * @dst: The temp list to put pages on to.
Hugh Dickinsf6260122012-01-12 17:20:06 -08001499 * @nr_scanned: The number of pages that were scanned.
Rik van Rielfe2c2a12012-03-21 16:33:51 -07001500 * @sc: The scan_control struct for this reclaim session
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001501 * @mode: One of the LRU isolation modes
Konstantin Khlebnikov3cb99452012-05-29 15:06:53 -07001502 * @lru: LRU list id for isolating
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 *
1504 * returns how many pages were moved onto *@dst.
1505 */
Andrew Morton69e05942006-03-22 00:08:19 -08001506static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
Konstantin Khlebnikov5dc35972012-05-29 15:06:58 -07001507 struct lruvec *lruvec, struct list_head *dst,
Rik van Rielfe2c2a12012-03-21 16:33:51 -07001508 unsigned long *nr_scanned, struct scan_control *sc,
Konstantin Khlebnikov3cb99452012-05-29 15:06:53 -07001509 isolate_mode_t mode, enum lru_list lru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Hugh Dickins75b00af2012-05-29 15:07:09 -07001511 struct list_head *src = &lruvec->lists[lru];
Andrew Morton69e05942006-03-22 00:08:19 -08001512 unsigned long nr_taken = 0;
Mel Gorman599d0c92016-07-28 15:45:31 -07001513 unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 };
Mel Gorman7cc30fc2016-07-28 15:46:59 -07001514 unsigned long nr_skipped[MAX_NR_ZONES] = { 0, };
Johannes Weiner3db65812017-05-03 14:52:13 -07001515 unsigned long skipped = 0;
Minchan Kim791b48b2017-05-12 15:47:06 -07001516 unsigned long scan, total_scan, nr_pages;
Mel Gormanb2e18752016-07-28 15:45:37 -07001517 LIST_HEAD(pages_skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Minchan Kim791b48b2017-05-12 15:47:06 -07001519 scan = 0;
1520 for (total_scan = 0;
1521 scan < nr_to_scan && nr_taken < nr_to_scan && !list_empty(src);
1522 total_scan++) {
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001523 struct page *page;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 page = lru_to_page(src);
1526 prefetchw_prev_lru_page(page, src, flags);
1527
Sasha Levin309381fea2014-01-23 15:52:54 -08001528 VM_BUG_ON_PAGE(!PageLRU(page), page);
Nick Piggin8d438f92006-03-22 00:07:59 -08001529
Mel Gormanb2e18752016-07-28 15:45:37 -07001530 if (page_zonenum(page) > sc->reclaim_idx) {
1531 list_move(&page->lru, &pages_skipped);
Mel Gorman7cc30fc2016-07-28 15:46:59 -07001532 nr_skipped[page_zonenum(page)]++;
Mel Gormanb2e18752016-07-28 15:45:37 -07001533 continue;
1534 }
1535
Minchan Kim791b48b2017-05-12 15:47:06 -07001536 /*
1537 * Do not count skipped pages because that makes the function
1538 * return with no isolated pages if the LRU mostly contains
1539 * ineligible pages. This causes the VM to not reclaim any
1540 * pages, triggering a premature OOM.
1541 */
1542 scan++;
Konstantin Khlebnikovf3fd4a62012-05-29 15:06:54 -07001543 switch (__isolate_lru_page(page, mode)) {
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001544 case 0:
Mel Gorman599d0c92016-07-28 15:45:31 -07001545 nr_pages = hpage_nr_pages(page);
1546 nr_taken += nr_pages;
1547 nr_zone_taken[page_zonenum(page)] += nr_pages;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001548 list_move(&page->lru, dst);
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001549 break;
Nick Piggin46453a62006-03-22 00:07:58 -08001550
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001551 case -EBUSY:
1552 /* else it is being freed elsewhere */
1553 list_move(&page->lru, src);
1554 continue;
1555
1556 default:
1557 BUG();
1558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
1560
Mel Gormanb2e18752016-07-28 15:45:37 -07001561 /*
1562 * Splice any skipped pages to the start of the LRU list. Note that
1563 * this disrupts the LRU order when reclaiming for lower zones but
1564 * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX
1565 * scanning would soon rescan the same pages to skip and put the
1566 * system at risk of premature OOM.
1567 */
Mel Gorman7cc30fc2016-07-28 15:46:59 -07001568 if (!list_empty(&pages_skipped)) {
1569 int zid;
1570
Johannes Weiner3db65812017-05-03 14:52:13 -07001571 list_splice(&pages_skipped, src);
Mel Gorman7cc30fc2016-07-28 15:46:59 -07001572 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
1573 if (!nr_skipped[zid])
1574 continue;
1575
1576 __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]);
Michal Hocko1265e3a2017-02-22 15:44:21 -08001577 skipped += nr_skipped[zid];
Mel Gorman7cc30fc2016-07-28 15:46:59 -07001578 }
1579 }
Minchan Kim791b48b2017-05-12 15:47:06 -07001580 *nr_scanned = total_scan;
Michal Hocko1265e3a2017-02-22 15:44:21 -08001581 trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan,
Minchan Kim791b48b2017-05-12 15:47:06 -07001582 total_scan, skipped, nr_taken, mode, lru);
Michal Hockob4536f0c82017-01-10 16:58:04 -08001583 update_lru_sizes(lruvec, lru, nr_zone_taken);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 return nr_taken;
1585}
1586
Nick Piggin62695a82008-10-18 20:26:09 -07001587/**
1588 * isolate_lru_page - tries to isolate a page from its LRU list
1589 * @page: page to isolate from its LRU list
1590 *
1591 * Isolates a @page from an LRU list, clears PageLRU and adjusts the
1592 * vmstat statistic corresponding to whatever LRU list the page was on.
1593 *
1594 * Returns 0 if the page was removed from an LRU list.
1595 * Returns -EBUSY if the page was not on an LRU list.
1596 *
1597 * The returned page will have PageLRU() cleared. If it was found on
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001598 * the active list, it will have PageActive set. If it was found on
1599 * the unevictable list, it will have the PageUnevictable bit set. That flag
1600 * may need to be cleared by the caller before letting the page go.
Nick Piggin62695a82008-10-18 20:26:09 -07001601 *
1602 * The vmstat statistic corresponding to the list on which the page was
1603 * found will be decremented.
1604 *
1605 * Restrictions:
1606 * (1) Must be called with an elevated refcount on the page. This is a
1607 * fundamentnal difference from isolate_lru_pages (which is called
1608 * without a stable reference).
1609 * (2) the lru_lock must not be held.
1610 * (3) interrupts must be enabled.
1611 */
1612int isolate_lru_page(struct page *page)
1613{
1614 int ret = -EBUSY;
1615
Sasha Levin309381fea2014-01-23 15:52:54 -08001616 VM_BUG_ON_PAGE(!page_count(page), page);
Kirill A. Shutemovcf2a82e2016-02-05 15:36:36 -08001617 WARN_RATELIMIT(PageTail(page), "trying to isolate tail page");
Konstantin Khlebnikov0c917312011-05-24 17:12:21 -07001618
Nick Piggin62695a82008-10-18 20:26:09 -07001619 if (PageLRU(page)) {
1620 struct zone *zone = page_zone(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001621 struct lruvec *lruvec;
Nick Piggin62695a82008-10-18 20:26:09 -07001622
Mel Gormana52633d2016-07-28 15:45:28 -07001623 spin_lock_irq(zone_lru_lock(zone));
Mel Gorman599d0c92016-07-28 15:45:31 -07001624 lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat);
Konstantin Khlebnikov0c917312011-05-24 17:12:21 -07001625 if (PageLRU(page)) {
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001626 int lru = page_lru(page);
Konstantin Khlebnikov0c917312011-05-24 17:12:21 -07001627 get_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -07001628 ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001629 del_page_from_lru_list(page, lruvec, lru);
1630 ret = 0;
Nick Piggin62695a82008-10-18 20:26:09 -07001631 }
Mel Gormana52633d2016-07-28 15:45:28 -07001632 spin_unlock_irq(zone_lru_lock(zone));
Nick Piggin62695a82008-10-18 20:26:09 -07001633 }
1634 return ret;
1635}
1636
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001637/*
Fengguang Wud37dd5d2012-12-18 14:23:28 -08001638 * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and
1639 * then get resheduled. When there are massive number of tasks doing page
1640 * allocation, such sleeping direct reclaimers may keep piling up on each CPU,
1641 * the LRU list will go small and be scanned faster than necessary, leading to
1642 * unnecessary swapping, thrashing and OOM.
Rik van Riel35cd7812009-09-21 17:01:38 -07001643 */
Mel Gorman599d0c92016-07-28 15:45:31 -07001644static int too_many_isolated(struct pglist_data *pgdat, int file,
Rik van Riel35cd7812009-09-21 17:01:38 -07001645 struct scan_control *sc)
1646{
1647 unsigned long inactive, isolated;
1648
1649 if (current_is_kswapd())
1650 return 0;
1651
Tejun Heo97c93412015-05-22 18:23:36 -04001652 if (!sane_reclaim(sc))
Rik van Riel35cd7812009-09-21 17:01:38 -07001653 return 0;
1654
1655 if (file) {
Mel Gorman599d0c92016-07-28 15:45:31 -07001656 inactive = node_page_state(pgdat, NR_INACTIVE_FILE);
1657 isolated = node_page_state(pgdat, NR_ISOLATED_FILE);
Rik van Riel35cd7812009-09-21 17:01:38 -07001658 } else {
Mel Gorman599d0c92016-07-28 15:45:31 -07001659 inactive = node_page_state(pgdat, NR_INACTIVE_ANON);
1660 isolated = node_page_state(pgdat, NR_ISOLATED_ANON);
Rik van Riel35cd7812009-09-21 17:01:38 -07001661 }
1662
Fengguang Wu3cf23842012-12-18 14:23:31 -08001663 /*
1664 * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they
1665 * won't get blocked by normal direct-reclaimers, forming a circular
1666 * deadlock.
1667 */
Mel Gormand0164ad2015-11-06 16:28:21 -08001668 if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS))
Fengguang Wu3cf23842012-12-18 14:23:31 -08001669 inactive >>= 3;
1670
Rik van Riel35cd7812009-09-21 17:01:38 -07001671 return isolated > inactive;
1672}
1673
Mel Gorman66635622010-08-09 17:19:30 -07001674static noinline_for_stack void
Hugh Dickins75b00af2012-05-29 15:07:09 -07001675putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list)
Mel Gorman66635622010-08-09 17:19:30 -07001676{
Konstantin Khlebnikov27ac81d2012-05-29 15:07:00 -07001677 struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
Mel Gorman599d0c92016-07-28 15:45:31 -07001678 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
Hugh Dickins3f797682012-01-12 17:20:07 -08001679 LIST_HEAD(pages_to_free);
Mel Gorman66635622010-08-09 17:19:30 -07001680
Mel Gorman66635622010-08-09 17:19:30 -07001681 /*
1682 * Put back any unfreeable pages.
1683 */
Mel Gorman66635622010-08-09 17:19:30 -07001684 while (!list_empty(page_list)) {
Hugh Dickins3f797682012-01-12 17:20:07 -08001685 struct page *page = lru_to_page(page_list);
Mel Gorman66635622010-08-09 17:19:30 -07001686 int lru;
Hugh Dickins3f797682012-01-12 17:20:07 -08001687
Sasha Levin309381fea2014-01-23 15:52:54 -08001688 VM_BUG_ON_PAGE(PageLRU(page), page);
Mel Gorman66635622010-08-09 17:19:30 -07001689 list_del(&page->lru);
Hugh Dickins39b5f292012-10-08 16:33:18 -07001690 if (unlikely(!page_evictable(page))) {
Mel Gorman599d0c92016-07-28 15:45:31 -07001691 spin_unlock_irq(&pgdat->lru_lock);
Mel Gorman66635622010-08-09 17:19:30 -07001692 putback_lru_page(page);
Mel Gorman599d0c92016-07-28 15:45:31 -07001693 spin_lock_irq(&pgdat->lru_lock);
Mel Gorman66635622010-08-09 17:19:30 -07001694 continue;
1695 }
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001696
Mel Gorman599d0c92016-07-28 15:45:31 -07001697 lruvec = mem_cgroup_page_lruvec(page, pgdat);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001698
Linus Torvalds7a608572011-01-17 14:42:19 -08001699 SetPageLRU(page);
Mel Gorman66635622010-08-09 17:19:30 -07001700 lru = page_lru(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001701 add_page_to_lru_list(page, lruvec, lru);
1702
Mel Gorman66635622010-08-09 17:19:30 -07001703 if (is_active_lru(lru)) {
1704 int file = is_file_lru(lru);
Rik van Riel9992af12011-01-13 15:47:13 -08001705 int numpages = hpage_nr_pages(page);
1706 reclaim_stat->recent_rotated[file] += numpages;
Mel Gorman66635622010-08-09 17:19:30 -07001707 }
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001708 if (put_page_testzero(page)) {
1709 __ClearPageLRU(page);
1710 __ClearPageActive(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001711 del_page_from_lru_list(page, lruvec, lru);
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001712
1713 if (unlikely(PageCompound(page))) {
Mel Gorman599d0c92016-07-28 15:45:31 -07001714 spin_unlock_irq(&pgdat->lru_lock);
Johannes Weiner747db952014-08-08 14:19:24 -07001715 mem_cgroup_uncharge(page);
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001716 (*get_compound_page_dtor(page))(page);
Mel Gorman599d0c92016-07-28 15:45:31 -07001717 spin_lock_irq(&pgdat->lru_lock);
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001718 } else
1719 list_add(&page->lru, &pages_to_free);
Mel Gorman66635622010-08-09 17:19:30 -07001720 }
1721 }
Mel Gorman66635622010-08-09 17:19:30 -07001722
Hugh Dickins3f797682012-01-12 17:20:07 -08001723 /*
1724 * To save our caller's stack, now use input list for pages to free.
1725 */
1726 list_splice(&pages_to_free, page_list);
Mel Gorman66635622010-08-09 17:19:30 -07001727}
1728
1729/*
NeilBrown399ba0b2014-06-04 16:07:42 -07001730 * If a kernel thread (such as nfsd for loop-back mounts) services
1731 * a backing device by writing to the page cache it sets PF_LESS_THROTTLE.
1732 * In that case we should only throttle if the backing device it is
1733 * writing to is congested. In other cases it is safe to throttle.
1734 */
1735static int current_may_throttle(void)
1736{
1737 return !(current->flags & PF_LESS_THROTTLE) ||
1738 current->backing_dev_info == NULL ||
1739 bdi_write_congested(current->backing_dev_info);
1740}
1741
1742/*
Mel Gormanb2e18752016-07-28 15:45:37 -07001743 * shrink_inactive_list() is a helper for shrink_node(). It returns the number
Andrew Morton1742f192006-03-22 00:08:21 -08001744 * of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 */
Mel Gorman66635622010-08-09 17:19:30 -07001746static noinline_for_stack unsigned long
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07001747shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec,
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07001748 struct scan_control *sc, enum lru_list lru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
1750 LIST_HEAD(page_list);
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001751 unsigned long nr_scanned;
Andrew Morton05ff5132006-03-22 00:08:20 -08001752 unsigned long nr_reclaimed = 0;
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001753 unsigned long nr_taken;
Michal Hocko3c710c12017-02-22 15:44:27 -08001754 struct reclaim_stat stat = {};
Konstantin Khlebnikovf3fd4a62012-05-29 15:06:54 -07001755 isolate_mode_t isolate_mode = 0;
Konstantin Khlebnikov3cb99452012-05-29 15:06:53 -07001756 int file = is_file_lru(lru);
Mel Gorman599d0c92016-07-28 15:45:31 -07001757 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07001758 struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
Michal Hockodb73ee02017-09-06 16:21:11 -07001759 bool stalled = false;
KOSAKI Motohiro78dc5832009-06-16 15:31:40 -07001760
Mel Gorman599d0c92016-07-28 15:45:31 -07001761 while (unlikely(too_many_isolated(pgdat, file, sc))) {
Michal Hockodb73ee02017-09-06 16:21:11 -07001762 if (stalled)
1763 return 0;
1764
1765 /* wait a bit for the reclaimer. */
1766 msleep(100);
1767 stalled = true;
Rik van Riel35cd7812009-09-21 17:01:38 -07001768
1769 /* We are about to die and free our memory. Return now. */
1770 if (fatal_signal_pending(current))
1771 return SWAP_CLUSTER_MAX;
1772 }
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 lru_add_drain();
Minchan Kimf80c0672011-10-31 17:06:55 -07001775
1776 if (!sc->may_unmap)
Hillf Danton61317282012-03-21 16:33:48 -07001777 isolate_mode |= ISOLATE_UNMAPPED;
Minchan Kimf80c0672011-10-31 17:06:55 -07001778
Mel Gorman599d0c92016-07-28 15:45:31 -07001779 spin_lock_irq(&pgdat->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Konstantin Khlebnikov5dc35972012-05-29 15:06:58 -07001781 nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list,
1782 &nr_scanned, sc, isolate_mode, lru);
Konstantin Khlebnikov95d918f2012-05-29 15:06:59 -07001783
Mel Gorman599d0c92016-07-28 15:45:31 -07001784 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
Hugh Dickins9d5e6a92016-05-19 17:12:38 -07001785 reclaim_stat->recent_scanned[file] += nr_taken;
Konstantin Khlebnikov95d918f2012-05-29 15:06:59 -07001786
Roman Gushchin22621852017-07-06 15:40:25 -07001787 if (current_is_kswapd()) {
1788 if (global_reclaim(sc))
Mel Gorman599d0c92016-07-28 15:45:31 -07001789 __count_vm_events(PGSCAN_KSWAPD, nr_scanned);
Roman Gushchin22621852017-07-06 15:40:25 -07001790 count_memcg_events(lruvec_memcg(lruvec), PGSCAN_KSWAPD,
1791 nr_scanned);
1792 } else {
1793 if (global_reclaim(sc))
Mel Gorman599d0c92016-07-28 15:45:31 -07001794 __count_vm_events(PGSCAN_DIRECT, nr_scanned);
Roman Gushchin22621852017-07-06 15:40:25 -07001795 count_memcg_events(lruvec_memcg(lruvec), PGSCAN_DIRECT,
1796 nr_scanned);
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001797 }
Mel Gorman599d0c92016-07-28 15:45:31 -07001798 spin_unlock_irq(&pgdat->lru_lock);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07001799
Hillf Dantond563c052012-03-21 16:34:02 -07001800 if (nr_taken == 0)
Mel Gorman66635622010-08-09 17:19:30 -07001801 return 0;
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001802
Shaohua Lia128ca72017-05-03 14:52:22 -07001803 nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, 0,
Michal Hocko3c710c12017-02-22 15:44:27 -08001804 &stat, false);
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001805
Mel Gorman599d0c92016-07-28 15:45:31 -07001806 spin_lock_irq(&pgdat->lru_lock);
Hugh Dickins3f797682012-01-12 17:20:07 -08001807
Roman Gushchin22621852017-07-06 15:40:25 -07001808 if (current_is_kswapd()) {
1809 if (global_reclaim(sc))
Mel Gorman599d0c92016-07-28 15:45:31 -07001810 __count_vm_events(PGSTEAL_KSWAPD, nr_reclaimed);
Roman Gushchin22621852017-07-06 15:40:25 -07001811 count_memcg_events(lruvec_memcg(lruvec), PGSTEAL_KSWAPD,
1812 nr_reclaimed);
1813 } else {
1814 if (global_reclaim(sc))
Mel Gorman599d0c92016-07-28 15:45:31 -07001815 __count_vm_events(PGSTEAL_DIRECT, nr_reclaimed);
Roman Gushchin22621852017-07-06 15:40:25 -07001816 count_memcg_events(lruvec_memcg(lruvec), PGSTEAL_DIRECT,
1817 nr_reclaimed);
Ying Han904249a2012-04-25 16:01:48 -07001818 }
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001819
Konstantin Khlebnikov27ac81d2012-05-29 15:07:00 -07001820 putback_inactive_pages(lruvec, &page_list);
Hugh Dickins3f797682012-01-12 17:20:07 -08001821
Mel Gorman599d0c92016-07-28 15:45:31 -07001822 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken);
Hugh Dickins3f797682012-01-12 17:20:07 -08001823
Mel Gorman599d0c92016-07-28 15:45:31 -07001824 spin_unlock_irq(&pgdat->lru_lock);
Hugh Dickins3f797682012-01-12 17:20:07 -08001825
Johannes Weiner747db952014-08-08 14:19:24 -07001826 mem_cgroup_uncharge_list(&page_list);
Mel Gorman2d4894b2017-11-15 17:37:59 -08001827 free_unref_page_list(&page_list);
Mel Gormane11da5b2010-10-26 14:21:40 -07001828
Mel Gorman92df3a72011-10-31 17:07:56 -07001829 /*
1830 * If reclaim is isolating dirty pages under writeback, it implies
1831 * that the long-lived page allocation rate is exceeding the page
1832 * laundering rate. Either the global limits are not being effective
1833 * at throttling processes due to the page distribution throughout
1834 * zones or there is heavy usage of a slow backing device. The
1835 * only option is to throttle from reclaim context which is not ideal
1836 * as there is no guarantee the dirtying process is throttled in the
1837 * same way balance_dirty_pages() manages.
1838 *
Mel Gorman8e950282013-07-03 15:02:02 -07001839 * Once a zone is flagged ZONE_WRITEBACK, kswapd will count the number
1840 * of pages under pages flagged for immediate reclaim and stall if any
1841 * are encountered in the nr_immediate check below.
Mel Gorman92df3a72011-10-31 17:07:56 -07001842 */
Michal Hocko3c710c12017-02-22 15:44:27 -08001843 if (stat.nr_writeback && stat.nr_writeback == nr_taken)
Mel Gorman599d0c92016-07-28 15:45:31 -07001844 set_bit(PGDAT_WRITEBACK, &pgdat->flags);
Mel Gorman92df3a72011-10-31 17:07:56 -07001845
Mel Gormand43006d2013-07-03 15:01:50 -07001846 /*
Tejun Heo97c93412015-05-22 18:23:36 -04001847 * Legacy memcg will stall in page writeback so avoid forcibly
1848 * stalling here.
Mel Gormand43006d2013-07-03 15:01:50 -07001849 */
Tejun Heo97c93412015-05-22 18:23:36 -04001850 if (sane_reclaim(sc)) {
Mel Gormanb1a6f212013-07-03 15:01:58 -07001851 /*
Mel Gorman8e950282013-07-03 15:02:02 -07001852 * Tag a zone as congested if all the dirty pages scanned were
1853 * backed by a congested BDI and wait_iff_congested will stall.
1854 */
Michal Hocko3c710c12017-02-22 15:44:27 -08001855 if (stat.nr_dirty && stat.nr_dirty == stat.nr_congested)
Mel Gorman599d0c92016-07-28 15:45:31 -07001856 set_bit(PGDAT_CONGESTED, &pgdat->flags);
Mel Gorman8e950282013-07-03 15:02:02 -07001857
1858 /*
Mel Gormanb1a6f212013-07-03 15:01:58 -07001859 * If dirty pages are scanned that are not queued for IO, it
Johannes Weiner726d0612017-02-24 14:56:14 -08001860 * implies that flushers are not doing their job. This can
1861 * happen when memory pressure pushes dirty pages to the end of
1862 * the LRU before the dirty limits are breached and the dirty
1863 * data has expired. It can also happen when the proportion of
1864 * dirty pages grows not through writes but through memory
1865 * pressure reclaiming all the clean cache. And in some cases,
1866 * the flushers simply cannot keep up with the allocation
1867 * rate. Nudge the flusher threads in case they are asleep, but
1868 * also allow kswapd to start writing pages during reclaim.
Mel Gormanb1a6f212013-07-03 15:01:58 -07001869 */
Johannes Weiner726d0612017-02-24 14:56:14 -08001870 if (stat.nr_unqueued_dirty == nr_taken) {
Jens Axboe9ba4b2d2017-09-20 08:58:25 -06001871 wakeup_flusher_threads(WB_REASON_VMSCAN);
Mel Gorman599d0c92016-07-28 15:45:31 -07001872 set_bit(PGDAT_DIRTY, &pgdat->flags);
Johannes Weiner726d0612017-02-24 14:56:14 -08001873 }
Mel Gormanb1a6f212013-07-03 15:01:58 -07001874
1875 /*
Linus Torvaldsb738d762014-06-08 14:17:00 -07001876 * If kswapd scans pages marked marked for immediate
1877 * reclaim and under writeback (nr_immediate), it implies
1878 * that pages are cycling through the LRU faster than
Mel Gormanb1a6f212013-07-03 15:01:58 -07001879 * they are written so also forcibly stall.
1880 */
Michal Hocko3c710c12017-02-22 15:44:27 -08001881 if (stat.nr_immediate && current_may_throttle())
Mel Gormanb1a6f212013-07-03 15:01:58 -07001882 congestion_wait(BLK_RW_ASYNC, HZ/10);
Mel Gormane2be15f2013-07-03 15:01:57 -07001883 }
Mel Gormand43006d2013-07-03 15:01:50 -07001884
Mel Gorman8e950282013-07-03 15:02:02 -07001885 /*
1886 * Stall direct reclaim for IO completions if underlying BDIs or zone
1887 * is congested. Allow kswapd to continue until it starts encountering
1888 * unqueued dirty pages or cycling through the LRU too quickly.
1889 */
NeilBrown399ba0b2014-06-04 16:07:42 -07001890 if (!sc->hibernation_mode && !current_is_kswapd() &&
1891 current_may_throttle())
Mel Gorman599d0c92016-07-28 15:45:31 -07001892 wait_iff_congested(pgdat, BLK_RW_ASYNC, HZ/10);
Mel Gorman8e950282013-07-03 15:02:02 -07001893
Mel Gorman599d0c92016-07-28 15:45:31 -07001894 trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id,
1895 nr_scanned, nr_reclaimed,
Michal Hocko5bccd162017-02-22 15:44:30 -08001896 stat.nr_dirty, stat.nr_writeback,
1897 stat.nr_congested, stat.nr_immediate,
1898 stat.nr_activate, stat.nr_ref_keep,
1899 stat.nr_unmap_fail,
yalin wangba5e9572016-01-14 15:18:48 -08001900 sc->priority, file);
Andrew Morton05ff5132006-03-22 00:08:20 -08001901 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902}
1903
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001904/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 * This moves pages from the active list to the inactive list.
1906 *
1907 * We move them the other way if the page is referenced by one or more
1908 * processes, from rmap.
1909 *
1910 * If the pages are mostly unmapped, the processing is fast and it is
Mel Gormana52633d2016-07-28 15:45:28 -07001911 * appropriate to hold zone_lru_lock across the whole operation. But if
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 * the pages are mapped, the processing is slow (page_referenced()) so we
Mel Gormana52633d2016-07-28 15:45:28 -07001913 * should drop zone_lru_lock around each page. It's impossible to balance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 * this, so instead we remove the pages from the LRU while processing them.
1915 * It is safe to rely on PG_active against the non-LRU pages in here because
1916 * nobody will play with that bit on a non-LRU page.
1917 *
Joonsoo Kim0139aa72016-05-19 17:10:49 -07001918 * The downside is that we have to touch page->_refcount against each page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 * But we had to alter page->flags anyway.
Michal Hocko9d998b42017-02-22 15:44:18 -08001920 *
1921 * Returns the number of pages moved to the given lru.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 */
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001923
Michal Hocko9d998b42017-02-22 15:44:18 -08001924static unsigned move_active_pages_to_lru(struct lruvec *lruvec,
Wu Fengguang3eb41402009-06-16 15:33:13 -07001925 struct list_head *list,
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001926 struct list_head *pages_to_free,
Wu Fengguang3eb41402009-06-16 15:33:13 -07001927 enum lru_list lru)
1928{
Mel Gorman599d0c92016-07-28 15:45:31 -07001929 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
Wu Fengguang3eb41402009-06-16 15:33:13 -07001930 struct page *page;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001931 int nr_pages;
Michal Hocko9d998b42017-02-22 15:44:18 -08001932 int nr_moved = 0;
Wu Fengguang3eb41402009-06-16 15:33:13 -07001933
Wu Fengguang3eb41402009-06-16 15:33:13 -07001934 while (!list_empty(list)) {
1935 page = lru_to_page(list);
Mel Gorman599d0c92016-07-28 15:45:31 -07001936 lruvec = mem_cgroup_page_lruvec(page, pgdat);
Wu Fengguang3eb41402009-06-16 15:33:13 -07001937
Sasha Levin309381fea2014-01-23 15:52:54 -08001938 VM_BUG_ON_PAGE(PageLRU(page), page);
Wu Fengguang3eb41402009-06-16 15:33:13 -07001939 SetPageLRU(page);
1940
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001941 nr_pages = hpage_nr_pages(page);
Mel Gorman599d0c92016-07-28 15:45:31 -07001942 update_lru_size(lruvec, lru, page_zonenum(page), nr_pages);
Johannes Weiner925b7672012-01-12 17:18:15 -08001943 list_move(&page->lru, &lruvec->lists[lru]);
Wu Fengguang3eb41402009-06-16 15:33:13 -07001944
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001945 if (put_page_testzero(page)) {
1946 __ClearPageLRU(page);
1947 __ClearPageActive(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001948 del_page_from_lru_list(page, lruvec, lru);
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001949
1950 if (unlikely(PageCompound(page))) {
Mel Gorman599d0c92016-07-28 15:45:31 -07001951 spin_unlock_irq(&pgdat->lru_lock);
Johannes Weiner747db952014-08-08 14:19:24 -07001952 mem_cgroup_uncharge(page);
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001953 (*get_compound_page_dtor(page))(page);
Mel Gorman599d0c92016-07-28 15:45:31 -07001954 spin_lock_irq(&pgdat->lru_lock);
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001955 } else
1956 list_add(&page->lru, pages_to_free);
Michal Hocko9d998b42017-02-22 15:44:18 -08001957 } else {
1958 nr_moved += nr_pages;
Wu Fengguang3eb41402009-06-16 15:33:13 -07001959 }
1960 }
Hugh Dickins9d5e6a92016-05-19 17:12:38 -07001961
Roman Gushchin22621852017-07-06 15:40:25 -07001962 if (!is_active_lru(lru)) {
Michal Hockof0958902017-02-22 15:45:55 -08001963 __count_vm_events(PGDEACTIVATE, nr_moved);
Roman Gushchin22621852017-07-06 15:40:25 -07001964 count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE,
1965 nr_moved);
1966 }
Michal Hocko9d998b42017-02-22 15:44:18 -08001967
1968 return nr_moved;
Wu Fengguang3eb41402009-06-16 15:33:13 -07001969}
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001970
Hugh Dickinsf6260122012-01-12 17:20:06 -08001971static void shrink_active_list(unsigned long nr_to_scan,
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07001972 struct lruvec *lruvec,
Johannes Weinerf16015f2012-01-12 17:17:52 -08001973 struct scan_control *sc,
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07001974 enum lru_list lru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
KOSAKI Motohiro44c241f2009-09-21 17:01:35 -07001976 unsigned long nr_taken;
Hugh Dickinsf6260122012-01-12 17:20:06 -08001977 unsigned long nr_scanned;
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -07001978 unsigned long vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 LIST_HEAD(l_hold); /* The pages which were snipped off */
Wu Fengguang8cab4752009-06-16 15:33:12 -07001980 LIST_HEAD(l_active);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001981 LIST_HEAD(l_inactive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 struct page *page;
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07001983 struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
Michal Hocko9d998b42017-02-22 15:44:18 -08001984 unsigned nr_deactivate, nr_activate;
1985 unsigned nr_rotated = 0;
Konstantin Khlebnikovf3fd4a62012-05-29 15:06:54 -07001986 isolate_mode_t isolate_mode = 0;
Konstantin Khlebnikov3cb99452012-05-29 15:06:53 -07001987 int file = is_file_lru(lru);
Mel Gorman599d0c92016-07-28 15:45:31 -07001988 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 lru_add_drain();
Minchan Kimf80c0672011-10-31 17:06:55 -07001991
1992 if (!sc->may_unmap)
Hillf Danton61317282012-03-21 16:33:48 -07001993 isolate_mode |= ISOLATE_UNMAPPED;
Minchan Kimf80c0672011-10-31 17:06:55 -07001994
Mel Gorman599d0c92016-07-28 15:45:31 -07001995 spin_lock_irq(&pgdat->lru_lock);
Johannes Weiner925b7672012-01-12 17:18:15 -08001996
Konstantin Khlebnikov5dc35972012-05-29 15:06:58 -07001997 nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold,
1998 &nr_scanned, sc, isolate_mode, lru);
Johannes Weiner89b5fae2012-01-12 17:17:50 -08001999
Mel Gorman599d0c92016-07-28 15:45:31 -07002000 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
Johannes Weinerb7c46d12009-09-21 17:02:56 -07002001 reclaim_stat->recent_scanned[file] += nr_taken;
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08002002
Mel Gorman599d0c92016-07-28 15:45:31 -07002003 __count_vm_events(PGREFILL, nr_scanned);
Roman Gushchin22621852017-07-06 15:40:25 -07002004 count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned);
Hugh Dickins9d5e6a92016-05-19 17:12:38 -07002005
Mel Gorman599d0c92016-07-28 15:45:31 -07002006 spin_unlock_irq(&pgdat->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 while (!list_empty(&l_hold)) {
2009 cond_resched();
2010 page = lru_to_page(&l_hold);
2011 list_del(&page->lru);
Rik van Riel7e9cd482008-10-18 20:26:35 -07002012
Hugh Dickins39b5f292012-10-08 16:33:18 -07002013 if (unlikely(!page_evictable(page))) {
Lee Schermerhorn894bc312008-10-18 20:26:39 -07002014 putback_lru_page(page);
2015 continue;
2016 }
2017
Mel Gormancc715d92012-03-21 16:34:00 -07002018 if (unlikely(buffer_heads_over_limit)) {
2019 if (page_has_private(page) && trylock_page(page)) {
2020 if (page_has_private(page))
2021 try_to_release_page(page, 0);
2022 unlock_page(page);
2023 }
2024 }
2025
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07002026 if (page_referenced(page, 0, sc->target_mem_cgroup,
2027 &vm_flags)) {
Rik van Riel9992af12011-01-13 15:47:13 -08002028 nr_rotated += hpage_nr_pages(page);
Wu Fengguang8cab4752009-06-16 15:33:12 -07002029 /*
2030 * Identify referenced, file-backed active pages and
2031 * give them one more trip around the active list. So
2032 * that executable code get better chances to stay in
2033 * memory under moderate memory pressure. Anon pages
2034 * are not likely to be evicted by use-once streaming
2035 * IO, plus JVM can create lots of anon VM_EXEC pages,
2036 * so we ignore them here.
2037 */
Wu Fengguang41e20982009-10-26 16:49:53 -07002038 if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) {
Wu Fengguang8cab4752009-06-16 15:33:12 -07002039 list_add(&page->lru, &l_active);
2040 continue;
2041 }
2042 }
Rik van Riel7e9cd482008-10-18 20:26:35 -07002043
KOSAKI Motohiro5205e562009-09-21 17:01:44 -07002044 ClearPageActive(page); /* we are de-activating */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 list_add(&page->lru, &l_inactive);
2046 }
2047
Andrew Mortonb5557492009-01-06 14:40:13 -08002048 /*
Wu Fengguang8cab4752009-06-16 15:33:12 -07002049 * Move pages back to the lru list.
Andrew Mortonb5557492009-01-06 14:40:13 -08002050 */
Mel Gorman599d0c92016-07-28 15:45:31 -07002051 spin_lock_irq(&pgdat->lru_lock);
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002052 /*
Wu Fengguang8cab4752009-06-16 15:33:12 -07002053 * Count referenced pages from currently used mappings as rotated,
2054 * even though only some of them are actually re-activated. This
2055 * helps balance scan pressure between file and anonymous pages in
Jerome Marchand7c0db9e2014-08-06 16:08:01 -07002056 * get_scan_count.
Rik van Riel7e9cd482008-10-18 20:26:35 -07002057 */
Johannes Weinerb7c46d12009-09-21 17:02:56 -07002058 reclaim_stat->recent_rotated[file] += nr_rotated;
Rik van Riel556adec2008-10-18 20:26:34 -07002059
Michal Hocko9d998b42017-02-22 15:44:18 -08002060 nr_activate = move_active_pages_to_lru(lruvec, &l_active, &l_hold, lru);
2061 nr_deactivate = move_active_pages_to_lru(lruvec, &l_inactive, &l_hold, lru - LRU_ACTIVE);
Mel Gorman599d0c92016-07-28 15:45:31 -07002062 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken);
2063 spin_unlock_irq(&pgdat->lru_lock);
Hugh Dickins2bcf8872012-01-12 17:19:56 -08002064
Johannes Weiner747db952014-08-08 14:19:24 -07002065 mem_cgroup_uncharge_list(&l_hold);
Mel Gorman2d4894b2017-11-15 17:37:59 -08002066 free_unref_page_list(&l_hold);
Michal Hocko9d998b42017-02-22 15:44:18 -08002067 trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate,
2068 nr_deactivate, nr_rotated, sc->priority, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
Rik van Riel59dc76b2016-05-20 16:56:31 -07002071/*
2072 * The inactive anon list should be small enough that the VM never has
2073 * to do too much work.
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002074 *
Rik van Riel59dc76b2016-05-20 16:56:31 -07002075 * The inactive file list should be small enough to leave most memory
2076 * to the established workingset on the scan-resistant active list,
2077 * but large enough to avoid thrashing the aggregate readahead window.
2078 *
2079 * Both inactive lists should also be large enough that each inactive
2080 * page has a chance to be referenced again before it is reclaimed.
2081 *
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002082 * If that fails and refaulting is observed, the inactive list grows.
2083 *
Rik van Riel59dc76b2016-05-20 16:56:31 -07002084 * The inactive_ratio is the target ratio of ACTIVE to INACTIVE pages
Andrey Ryabinin3a50d142017-11-15 17:34:15 -08002085 * on this LRU, maintained by the pageout code. An inactive_ratio
Rik van Riel59dc76b2016-05-20 16:56:31 -07002086 * of 3 means 3:1 or 25% of the pages are kept on the inactive list.
2087 *
2088 * total target max
2089 * memory ratio inactive
2090 * -------------------------------------
2091 * 10MB 1 5MB
2092 * 100MB 1 50MB
2093 * 1GB 3 250MB
2094 * 10GB 10 0.9GB
2095 * 100GB 31 3GB
2096 * 1TB 101 10GB
2097 * 10TB 320 32GB
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002098 */
Mel Gormanf8d1a312016-07-28 15:47:34 -07002099static bool inactive_list_is_low(struct lruvec *lruvec, bool file,
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002100 struct mem_cgroup *memcg,
2101 struct scan_control *sc, bool actual_reclaim)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002102{
Michal Hockofd538802017-02-22 15:45:58 -08002103 enum lru_list active_lru = file * LRU_FILE + LRU_ACTIVE;
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002104 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
2105 enum lru_list inactive_lru = file * LRU_FILE;
2106 unsigned long inactive, active;
2107 unsigned long inactive_ratio;
2108 unsigned long refaults;
Rik van Riel59dc76b2016-05-20 16:56:31 -07002109 unsigned long gb;
2110
Minchan Kim74e3f3c2010-10-26 14:21:31 -07002111 /*
2112 * If we don't have swap space, anonymous page deactivation
2113 * is pointless.
2114 */
Rik van Riel59dc76b2016-05-20 16:56:31 -07002115 if (!file && !total_swap_pages)
Yaowei Bai42e2e452015-11-05 18:47:36 -08002116 return false;
Minchan Kim74e3f3c2010-10-26 14:21:31 -07002117
Michal Hockofd538802017-02-22 15:45:58 -08002118 inactive = lruvec_lru_size(lruvec, inactive_lru, sc->reclaim_idx);
2119 active = lruvec_lru_size(lruvec, active_lru, sc->reclaim_idx);
Mel Gormanf8d1a312016-07-28 15:47:34 -07002120
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002121 if (memcg)
Johannes Weinerccda7f42017-05-03 14:55:16 -07002122 refaults = memcg_page_state(memcg, WORKINGSET_ACTIVATE);
Rik van Rielb39415b2009-12-14 17:59:48 -08002123 else
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002124 refaults = node_page_state(pgdat, WORKINGSET_ACTIVATE);
Rik van Riel59dc76b2016-05-20 16:56:31 -07002125
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002126 /*
2127 * When refaults are being observed, it means a new workingset
2128 * is being established. Disable active list protection to get
2129 * rid of the stale workingset quickly.
2130 */
2131 if (file && actual_reclaim && lruvec->refaults != refaults) {
2132 inactive_ratio = 0;
2133 } else {
2134 gb = (inactive + active) >> (30 - PAGE_SHIFT);
2135 if (gb)
2136 inactive_ratio = int_sqrt(10 * gb);
2137 else
2138 inactive_ratio = 1;
2139 }
2140
2141 if (actual_reclaim)
2142 trace_mm_vmscan_inactive_list_is_low(pgdat->node_id, sc->reclaim_idx,
2143 lruvec_lru_size(lruvec, inactive_lru, MAX_NR_ZONES), inactive,
2144 lruvec_lru_size(lruvec, active_lru, MAX_NR_ZONES), active,
2145 inactive_ratio, file);
Michal Hockofd538802017-02-22 15:45:58 -08002146
Rik van Riel59dc76b2016-05-20 16:56:31 -07002147 return inactive * inactive_ratio < active;
Rik van Rielb39415b2009-12-14 17:59:48 -08002148}
2149
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002150static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002151 struct lruvec *lruvec, struct mem_cgroup *memcg,
2152 struct scan_control *sc)
Christoph Lameterb69408e2008-10-18 20:26:14 -07002153{
Rik van Rielb39415b2009-12-14 17:59:48 -08002154 if (is_active_lru(lru)) {
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002155 if (inactive_list_is_low(lruvec, is_file_lru(lru),
2156 memcg, sc, true))
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07002157 shrink_active_list(nr_to_scan, lruvec, sc, lru);
Rik van Riel556adec2008-10-18 20:26:34 -07002158 return 0;
2159 }
2160
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07002161 return shrink_inactive_list(nr_to_scan, lruvec, sc, lru);
Christoph Lameterb69408e2008-10-18 20:26:14 -07002162}
2163
Johannes Weiner9a265112013-02-22 16:32:17 -08002164enum scan_balance {
2165 SCAN_EQUAL,
2166 SCAN_FRACT,
2167 SCAN_ANON,
2168 SCAN_FILE,
2169};
2170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171/*
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002172 * Determine how aggressively the anon and file LRU lists should be
2173 * scanned. The relative value of each set of LRU lists is determined
2174 * by looking at the fraction of the pages scanned we did rotate back
2175 * onto the active list instead of evict.
2176 *
Wanpeng Libe7bd592012-06-14 20:41:02 +08002177 * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan
2178 * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002179 */
Vladimir Davydov33377672016-01-20 15:02:59 -08002180static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg,
Johannes Weiner6b4f7792014-12-12 16:56:13 -08002181 struct scan_control *sc, unsigned long *nr,
2182 unsigned long *lru_pages)
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002183{
Vladimir Davydov33377672016-01-20 15:02:59 -08002184 int swappiness = mem_cgroup_swappiness(memcg);
Konstantin Khlebnikov90126372012-05-29 15:07:01 -07002185 struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
Johannes Weiner9a265112013-02-22 16:32:17 -08002186 u64 fraction[2];
2187 u64 denominator = 0; /* gcc */
Mel Gorman599d0c92016-07-28 15:45:31 -07002188 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
Johannes Weiner9a265112013-02-22 16:32:17 -08002189 unsigned long anon_prio, file_prio;
2190 enum scan_balance scan_balance;
Johannes Weiner0bf14572014-04-08 16:04:10 -07002191 unsigned long anon, file;
Johannes Weiner9a265112013-02-22 16:32:17 -08002192 unsigned long ap, fp;
2193 enum lru_list lru;
Shaohua Li76a33fc2010-05-24 14:32:36 -07002194
2195 /* If we have no swap space, do not bother scanning anon pages. */
Vladimir Davydovd8b38432016-01-20 15:03:07 -08002196 if (!sc->may_swap || mem_cgroup_get_nr_swap_pages(memcg) <= 0) {
Johannes Weiner9a265112013-02-22 16:32:17 -08002197 scan_balance = SCAN_FILE;
Shaohua Li76a33fc2010-05-24 14:32:36 -07002198 goto out;
2199 }
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002200
Johannes Weiner10316b32013-02-22 16:32:14 -08002201 /*
2202 * Global reclaim will swap to prevent OOM even with no
2203 * swappiness, but memcg users want to use this knob to
2204 * disable swapping for individual groups completely when
2205 * using the memory controller's swap limit feature would be
2206 * too expensive.
2207 */
Johannes Weiner02695172014-08-06 16:06:17 -07002208 if (!global_reclaim(sc) && !swappiness) {
Johannes Weiner9a265112013-02-22 16:32:17 -08002209 scan_balance = SCAN_FILE;
Johannes Weiner10316b32013-02-22 16:32:14 -08002210 goto out;
2211 }
2212
2213 /*
2214 * Do not apply any pressure balancing cleverness when the
2215 * system is close to OOM, scan both anon and file equally
2216 * (unless the swappiness setting disagrees with swapping).
2217 */
Johannes Weiner02695172014-08-06 16:06:17 -07002218 if (!sc->priority && swappiness) {
Johannes Weiner9a265112013-02-22 16:32:17 -08002219 scan_balance = SCAN_EQUAL;
Johannes Weiner10316b32013-02-22 16:32:14 -08002220 goto out;
2221 }
2222
Johannes Weiner11d16c22013-02-22 16:32:15 -08002223 /*
Johannes Weiner62376252014-05-06 12:50:07 -07002224 * Prevent the reclaimer from falling into the cache trap: as
2225 * cache pages start out inactive, every cache fault will tip
2226 * the scan balance towards the file LRU. And as the file LRU
2227 * shrinks, so does the window for rotation from references.
2228 * This means we have a runaway feedback loop where a tiny
2229 * thrashing file LRU becomes infinitely more attractive than
2230 * anon pages. Try to detect this based on file LRU size.
2231 */
2232 if (global_reclaim(sc)) {
Mel Gorman599d0c92016-07-28 15:45:31 -07002233 unsigned long pgdatfile;
2234 unsigned long pgdatfree;
2235 int z;
2236 unsigned long total_high_wmark = 0;
Johannes Weiner62376252014-05-06 12:50:07 -07002237
Mel Gorman599d0c92016-07-28 15:45:31 -07002238 pgdatfree = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES);
2239 pgdatfile = node_page_state(pgdat, NR_ACTIVE_FILE) +
2240 node_page_state(pgdat, NR_INACTIVE_FILE);
Jerome Marchand2ab051e2014-08-06 16:08:03 -07002241
Mel Gorman599d0c92016-07-28 15:45:31 -07002242 for (z = 0; z < MAX_NR_ZONES; z++) {
2243 struct zone *zone = &pgdat->node_zones[z];
Mel Gorman6aa303d2016-09-01 16:14:55 -07002244 if (!managed_zone(zone))
Mel Gorman599d0c92016-07-28 15:45:31 -07002245 continue;
2246
2247 total_high_wmark += high_wmark_pages(zone);
2248 }
2249
2250 if (unlikely(pgdatfile + pgdatfree <= total_high_wmark)) {
David Rientjes06226222017-07-10 15:47:20 -07002251 /*
2252 * Force SCAN_ANON if there are enough inactive
2253 * anonymous pages on the LRU in eligible zones.
2254 * Otherwise, the small LRU gets thrashed.
2255 */
2256 if (!inactive_list_is_low(lruvec, false, memcg, sc, false) &&
2257 lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, sc->reclaim_idx)
2258 >> sc->priority) {
2259 scan_balance = SCAN_ANON;
2260 goto out;
2261 }
Johannes Weiner62376252014-05-06 12:50:07 -07002262 }
2263 }
2264
2265 /*
Vladimir Davydov316bda02016-01-14 15:19:38 -08002266 * If there is enough inactive page cache, i.e. if the size of the
2267 * inactive list is greater than that of the active list *and* the
2268 * inactive list actually has some pages to scan on this priority, we
2269 * do not reclaim anything from the anonymous working set right now.
2270 * Without the second condition we could end up never scanning an
2271 * lruvec even if it has plenty of old anonymous pages unless the
2272 * system is under heavy pressure.
Johannes Weiner7c5bd702013-02-22 16:32:10 -08002273 */
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002274 if (!inactive_list_is_low(lruvec, true, memcg, sc, false) &&
Michal Hocko71ab6cf2017-02-22 15:46:01 -08002275 lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, sc->reclaim_idx) >> sc->priority) {
Johannes Weiner9a265112013-02-22 16:32:17 -08002276 scan_balance = SCAN_FILE;
Johannes Weiner7c5bd702013-02-22 16:32:10 -08002277 goto out;
2278 }
2279
Johannes Weiner9a265112013-02-22 16:32:17 -08002280 scan_balance = SCAN_FRACT;
2281
Johannes Weiner7c5bd702013-02-22 16:32:10 -08002282 /*
KOSAKI Motohiro58c37f62010-08-09 17:19:51 -07002283 * With swappiness at 100, anonymous and file have the same priority.
2284 * This scanning priority is essentially the inverse of IO cost.
2285 */
Johannes Weiner02695172014-08-06 16:06:17 -07002286 anon_prio = swappiness;
Hugh Dickins75b00af2012-05-29 15:07:09 -07002287 file_prio = 200 - anon_prio;
KOSAKI Motohiro58c37f62010-08-09 17:19:51 -07002288
2289 /*
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002290 * OK, so we have swap space and a fair amount of page cache
2291 * pages. We use the recently rotated / recently scanned
2292 * ratios to determine how valuable each cache is.
2293 *
2294 * Because workloads change over time (and to avoid overflow)
2295 * we keep these statistics as a floating average, which ends
2296 * up weighing recent references more than old ones.
2297 *
2298 * anon in [0], file in [1]
2299 */
Jerome Marchand2ab051e2014-08-06 16:08:03 -07002300
Michal Hockofd538802017-02-22 15:45:58 -08002301 anon = lruvec_lru_size(lruvec, LRU_ACTIVE_ANON, MAX_NR_ZONES) +
2302 lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, MAX_NR_ZONES);
2303 file = lruvec_lru_size(lruvec, LRU_ACTIVE_FILE, MAX_NR_ZONES) +
2304 lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, MAX_NR_ZONES);
Jerome Marchand2ab051e2014-08-06 16:08:03 -07002305
Mel Gorman599d0c92016-07-28 15:45:31 -07002306 spin_lock_irq(&pgdat->lru_lock);
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08002307 if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) {
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08002308 reclaim_stat->recent_scanned[0] /= 2;
2309 reclaim_stat->recent_rotated[0] /= 2;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002310 }
2311
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08002312 if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) {
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08002313 reclaim_stat->recent_scanned[1] /= 2;
2314 reclaim_stat->recent_rotated[1] /= 2;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002315 }
2316
2317 /*
Rik van Riel00d80892008-11-19 15:36:44 -08002318 * The amount of pressure on anon vs file pages is inversely
2319 * proportional to the fraction of recently scanned pages on
2320 * each list that were recently referenced and in active use.
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002321 */
Satoru Moriyafe350042012-05-29 15:06:47 -07002322 ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1);
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08002323 ap /= reclaim_stat->recent_rotated[0] + 1;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002324
Satoru Moriyafe350042012-05-29 15:06:47 -07002325 fp = file_prio * (reclaim_stat->recent_scanned[1] + 1);
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08002326 fp /= reclaim_stat->recent_rotated[1] + 1;
Mel Gorman599d0c92016-07-28 15:45:31 -07002327 spin_unlock_irq(&pgdat->lru_lock);
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002328
Shaohua Li76a33fc2010-05-24 14:32:36 -07002329 fraction[0] = ap;
2330 fraction[1] = fp;
2331 denominator = ap + fp + 1;
2332out:
Johannes Weiner688035f2017-05-03 14:52:07 -07002333 *lru_pages = 0;
2334 for_each_evictable_lru(lru) {
2335 int file = is_file_lru(lru);
2336 unsigned long size;
2337 unsigned long scan;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002338
Johannes Weiner688035f2017-05-03 14:52:07 -07002339 size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx);
2340 scan = size >> sc->priority;
2341 /*
2342 * If the cgroup's already been deleted, make sure to
2343 * scrape out the remaining cache.
2344 */
2345 if (!scan && !mem_cgroup_online(memcg))
2346 scan = min(size, SWAP_CLUSTER_MAX);
Johannes Weiner9a265112013-02-22 16:32:17 -08002347
Johannes Weiner688035f2017-05-03 14:52:07 -07002348 switch (scan_balance) {
2349 case SCAN_EQUAL:
2350 /* Scan lists relative to size */
2351 break;
2352 case SCAN_FRACT:
Johannes Weiner9a265112013-02-22 16:32:17 -08002353 /*
Johannes Weiner688035f2017-05-03 14:52:07 -07002354 * Scan types proportional to swappiness and
2355 * their relative recent reclaim efficiency.
Johannes Weiner9a265112013-02-22 16:32:17 -08002356 */
Johannes Weiner688035f2017-05-03 14:52:07 -07002357 scan = div64_u64(scan * fraction[file],
2358 denominator);
2359 break;
2360 case SCAN_FILE:
2361 case SCAN_ANON:
2362 /* Scan one type exclusively */
2363 if ((scan_balance == SCAN_FILE) != file) {
2364 size = 0;
2365 scan = 0;
2366 }
2367 break;
2368 default:
2369 /* Look ma, no brain */
2370 BUG();
Johannes Weiner9a265112013-02-22 16:32:17 -08002371 }
Johannes Weiner688035f2017-05-03 14:52:07 -07002372
2373 *lru_pages += size;
2374 nr[lru] = scan;
Shaohua Li76a33fc2010-05-24 14:32:36 -07002375 }
Wu Fengguang6e08a362009-06-16 15:32:29 -07002376}
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002377
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002378/*
Mel Gormana9dd0a82016-07-28 15:46:02 -07002379 * This is a basic per-node page freer. Used by both kswapd and direct reclaim.
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002380 */
Mel Gormana9dd0a82016-07-28 15:46:02 -07002381static void shrink_node_memcg(struct pglist_data *pgdat, struct mem_cgroup *memcg,
Vladimir Davydov33377672016-01-20 15:02:59 -08002382 struct scan_control *sc, unsigned long *lru_pages)
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002383{
Mel Gormanef8f2322016-07-28 15:46:05 -07002384 struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg);
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002385 unsigned long nr[NR_LRU_LISTS];
Mel Gormane82e0562013-07-03 15:01:44 -07002386 unsigned long targets[NR_LRU_LISTS];
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002387 unsigned long nr_to_scan;
2388 enum lru_list lru;
2389 unsigned long nr_reclaimed = 0;
2390 unsigned long nr_to_reclaim = sc->nr_to_reclaim;
2391 struct blk_plug plug;
Mel Gorman1a501902014-06-04 16:10:49 -07002392 bool scan_adjusted;
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002393
Vladimir Davydov33377672016-01-20 15:02:59 -08002394 get_scan_count(lruvec, memcg, sc, nr, lru_pages);
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002395
Mel Gormane82e0562013-07-03 15:01:44 -07002396 /* Record the original scan target for proportional adjustments later */
2397 memcpy(targets, nr, sizeof(nr));
2398
Mel Gorman1a501902014-06-04 16:10:49 -07002399 /*
2400 * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal
2401 * event that can occur when there is little memory pressure e.g.
2402 * multiple streaming readers/writers. Hence, we do not abort scanning
2403 * when the requested number of pages are reclaimed when scanning at
2404 * DEF_PRIORITY on the assumption that the fact we are direct
2405 * reclaiming implies that kswapd is not keeping up and it is best to
2406 * do a batch of work at once. For memcg reclaim one check is made to
2407 * abort proportional reclaim if either the file or anon lru has already
2408 * dropped to zero at the first pass.
2409 */
2410 scan_adjusted = (global_reclaim(sc) && !current_is_kswapd() &&
2411 sc->priority == DEF_PRIORITY);
2412
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002413 blk_start_plug(&plug);
2414 while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
2415 nr[LRU_INACTIVE_FILE]) {
Mel Gormane82e0562013-07-03 15:01:44 -07002416 unsigned long nr_anon, nr_file, percentage;
2417 unsigned long nr_scanned;
2418
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002419 for_each_evictable_lru(lru) {
2420 if (nr[lru]) {
2421 nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX);
2422 nr[lru] -= nr_to_scan;
2423
2424 nr_reclaimed += shrink_list(lru, nr_to_scan,
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002425 lruvec, memcg, sc);
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002426 }
2427 }
Mel Gormane82e0562013-07-03 15:01:44 -07002428
Michal Hockobd041732016-12-02 17:26:48 -08002429 cond_resched();
2430
Mel Gormane82e0562013-07-03 15:01:44 -07002431 if (nr_reclaimed < nr_to_reclaim || scan_adjusted)
2432 continue;
2433
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002434 /*
Mel Gormane82e0562013-07-03 15:01:44 -07002435 * For kswapd and memcg, reclaim at least the number of pages
Mel Gorman1a501902014-06-04 16:10:49 -07002436 * requested. Ensure that the anon and file LRUs are scanned
Mel Gormane82e0562013-07-03 15:01:44 -07002437 * proportionally what was requested by get_scan_count(). We
2438 * stop reclaiming one LRU and reduce the amount scanning
2439 * proportional to the original scan target.
2440 */
2441 nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE];
2442 nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON];
2443
Mel Gorman1a501902014-06-04 16:10:49 -07002444 /*
2445 * It's just vindictive to attack the larger once the smaller
2446 * has gone to zero. And given the way we stop scanning the
2447 * smaller below, this makes sure that we only make one nudge
2448 * towards proportionality once we've got nr_to_reclaim.
2449 */
2450 if (!nr_file || !nr_anon)
2451 break;
2452
Mel Gormane82e0562013-07-03 15:01:44 -07002453 if (nr_file > nr_anon) {
2454 unsigned long scan_target = targets[LRU_INACTIVE_ANON] +
2455 targets[LRU_ACTIVE_ANON] + 1;
2456 lru = LRU_BASE;
2457 percentage = nr_anon * 100 / scan_target;
2458 } else {
2459 unsigned long scan_target = targets[LRU_INACTIVE_FILE] +
2460 targets[LRU_ACTIVE_FILE] + 1;
2461 lru = LRU_FILE;
2462 percentage = nr_file * 100 / scan_target;
2463 }
2464
2465 /* Stop scanning the smaller of the LRU */
2466 nr[lru] = 0;
2467 nr[lru + LRU_ACTIVE] = 0;
2468
2469 /*
2470 * Recalculate the other LRU scan count based on its original
2471 * scan target and the percentage scanning already complete
2472 */
2473 lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE;
2474 nr_scanned = targets[lru] - nr[lru];
2475 nr[lru] = targets[lru] * (100 - percentage) / 100;
2476 nr[lru] -= min(nr[lru], nr_scanned);
2477
2478 lru += LRU_ACTIVE;
2479 nr_scanned = targets[lru] - nr[lru];
2480 nr[lru] = targets[lru] * (100 - percentage) / 100;
2481 nr[lru] -= min(nr[lru], nr_scanned);
2482
2483 scan_adjusted = true;
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002484 }
2485 blk_finish_plug(&plug);
2486 sc->nr_reclaimed += nr_reclaimed;
2487
2488 /*
2489 * Even if we did not try to evict anon pages at all, we want to
2490 * rebalance the anon lru active/inactive ratio.
2491 */
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002492 if (inactive_list_is_low(lruvec, false, memcg, sc, true))
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002493 shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
2494 sc, LRU_ACTIVE_ANON);
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002495}
2496
Mel Gorman23b9da52012-05-29 15:06:20 -07002497/* Use reclaim/compaction for costly allocs or under memory pressure */
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002498static bool in_reclaim_compaction(struct scan_control *sc)
Mel Gorman23b9da52012-05-29 15:06:20 -07002499{
Kirill A. Shutemovd84da3f2012-12-11 16:00:31 -08002500 if (IS_ENABLED(CONFIG_COMPACTION) && sc->order &&
Mel Gorman23b9da52012-05-29 15:06:20 -07002501 (sc->order > PAGE_ALLOC_COSTLY_ORDER ||
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002502 sc->priority < DEF_PRIORITY - 2))
Mel Gorman23b9da52012-05-29 15:06:20 -07002503 return true;
2504
2505 return false;
2506}
2507
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002508/*
Mel Gorman23b9da52012-05-29 15:06:20 -07002509 * Reclaim/compaction is used for high-order allocation requests. It reclaims
2510 * order-0 pages before compacting the zone. should_continue_reclaim() returns
2511 * true if more pages should be reclaimed such that when the page allocator
2512 * calls try_to_compact_zone() that it will have enough free pages to succeed.
2513 * It will give up earlier than that if there is difficulty reclaiming pages.
Mel Gorman3e7d3442011-01-13 15:45:56 -08002514 */
Mel Gormana9dd0a82016-07-28 15:46:02 -07002515static inline bool should_continue_reclaim(struct pglist_data *pgdat,
Mel Gorman3e7d3442011-01-13 15:45:56 -08002516 unsigned long nr_reclaimed,
2517 unsigned long nr_scanned,
2518 struct scan_control *sc)
2519{
2520 unsigned long pages_for_compaction;
2521 unsigned long inactive_lru_pages;
Mel Gormana9dd0a82016-07-28 15:46:02 -07002522 int z;
Mel Gorman3e7d3442011-01-13 15:45:56 -08002523
2524 /* If not in reclaim/compaction mode, stop */
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002525 if (!in_reclaim_compaction(sc))
Mel Gorman3e7d3442011-01-13 15:45:56 -08002526 return false;
2527
Mel Gorman28765922011-02-25 14:44:20 -08002528 /* Consider stopping depending on scan and reclaim activity */
Michal Hockodcda9b02017-07-12 14:36:45 -07002529 if (sc->gfp_mask & __GFP_RETRY_MAYFAIL) {
Mel Gorman28765922011-02-25 14:44:20 -08002530 /*
Michal Hockodcda9b02017-07-12 14:36:45 -07002531 * For __GFP_RETRY_MAYFAIL allocations, stop reclaiming if the
Mel Gorman28765922011-02-25 14:44:20 -08002532 * full LRU list has been scanned and we are still failing
2533 * to reclaim pages. This full LRU scan is potentially
Michal Hockodcda9b02017-07-12 14:36:45 -07002534 * expensive but a __GFP_RETRY_MAYFAIL caller really wants to succeed
Mel Gorman28765922011-02-25 14:44:20 -08002535 */
2536 if (!nr_reclaimed && !nr_scanned)
2537 return false;
2538 } else {
2539 /*
Michal Hockodcda9b02017-07-12 14:36:45 -07002540 * For non-__GFP_RETRY_MAYFAIL allocations which can presumably
Mel Gorman28765922011-02-25 14:44:20 -08002541 * fail without consequence, stop if we failed to reclaim
2542 * any pages from the last SWAP_CLUSTER_MAX number of
2543 * pages that were scanned. This will return to the
2544 * caller faster at the risk reclaim/compaction and
2545 * the resulting allocation attempt fails
2546 */
2547 if (!nr_reclaimed)
2548 return false;
2549 }
Mel Gorman3e7d3442011-01-13 15:45:56 -08002550
2551 /*
2552 * If we have not reclaimed enough pages for compaction and the
2553 * inactive lists are large enough, continue reclaiming
2554 */
Vlastimil Babka9861a622016-10-07 16:57:53 -07002555 pages_for_compaction = compact_gap(sc->order);
Mel Gormana9dd0a82016-07-28 15:46:02 -07002556 inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE);
Shaohua Liec8acf22013-02-22 16:34:38 -08002557 if (get_nr_swap_pages() > 0)
Mel Gormana9dd0a82016-07-28 15:46:02 -07002558 inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON);
Mel Gorman3e7d3442011-01-13 15:45:56 -08002559 if (sc->nr_reclaimed < pages_for_compaction &&
2560 inactive_lru_pages > pages_for_compaction)
2561 return true;
2562
2563 /* If compaction would go ahead or the allocation would succeed, stop */
Mel Gormana9dd0a82016-07-28 15:46:02 -07002564 for (z = 0; z <= sc->reclaim_idx; z++) {
2565 struct zone *zone = &pgdat->node_zones[z];
Mel Gorman6aa303d2016-09-01 16:14:55 -07002566 if (!managed_zone(zone))
Mel Gormana9dd0a82016-07-28 15:46:02 -07002567 continue;
2568
2569 switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) {
Vlastimil Babkacf378312016-10-07 16:57:41 -07002570 case COMPACT_SUCCESS:
Mel Gormana9dd0a82016-07-28 15:46:02 -07002571 case COMPACT_CONTINUE:
2572 return false;
2573 default:
2574 /* check next zone */
2575 ;
2576 }
Mel Gorman3e7d3442011-01-13 15:45:56 -08002577 }
Mel Gormana9dd0a82016-07-28 15:46:02 -07002578 return true;
Mel Gorman3e7d3442011-01-13 15:45:56 -08002579}
2580
Mel Gorman970a39a2016-07-28 15:46:35 -07002581static bool shrink_node(pg_data_t *pgdat, struct scan_control *sc)
Johannes Weinerf16015f2012-01-12 17:17:52 -08002582{
Vladimir Davydovcb731d62015-02-12 14:58:54 -08002583 struct reclaim_state *reclaim_state = current->reclaim_state;
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002584 unsigned long nr_reclaimed, nr_scanned;
Johannes Weiner2344d7e2014-08-06 16:06:15 -07002585 bool reclaimable = false;
Johannes Weinerf16015f2012-01-12 17:17:52 -08002586
Johannes Weiner56600482012-01-12 17:17:59 -08002587 do {
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002588 struct mem_cgroup *root = sc->target_mem_cgroup;
2589 struct mem_cgroup_reclaim_cookie reclaim = {
Mel Gormanef8f2322016-07-28 15:46:05 -07002590 .pgdat = pgdat,
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002591 .priority = sc->priority,
2592 };
Mel Gormana9dd0a82016-07-28 15:46:02 -07002593 unsigned long node_lru_pages = 0;
Andrew Morton694fbc02013-09-24 15:27:37 -07002594 struct mem_cgroup *memcg;
Johannes Weiner56600482012-01-12 17:17:59 -08002595
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002596 nr_reclaimed = sc->nr_reclaimed;
2597 nr_scanned = sc->nr_scanned;
Konstantin Khlebnikovf9be23d2012-05-29 15:07:02 -07002598
Andrew Morton694fbc02013-09-24 15:27:37 -07002599 memcg = mem_cgroup_iter(root, NULL, &reclaim);
2600 do {
Johannes Weiner6b4f7792014-12-12 16:56:13 -08002601 unsigned long lru_pages;
Johannes Weiner8e8ae642016-01-14 15:21:32 -08002602 unsigned long reclaimed;
Vladimir Davydovcb731d62015-02-12 14:58:54 -08002603 unsigned long scanned;
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002604
Johannes Weiner241994ed2015-02-11 15:26:06 -08002605 if (mem_cgroup_low(root, memcg)) {
Yisheng Xied6622f62017-05-03 14:53:57 -07002606 if (!sc->memcg_low_reclaim) {
2607 sc->memcg_low_skipped = 1;
Johannes Weiner241994ed2015-02-11 15:26:06 -08002608 continue;
Yisheng Xied6622f62017-05-03 14:53:57 -07002609 }
Johannes Weiner31176c72017-05-03 14:55:07 -07002610 mem_cgroup_event(memcg, MEMCG_LOW);
Johannes Weiner241994ed2015-02-11 15:26:06 -08002611 }
2612
Johannes Weiner8e8ae642016-01-14 15:21:32 -08002613 reclaimed = sc->nr_reclaimed;
Vladimir Davydovcb731d62015-02-12 14:58:54 -08002614 scanned = sc->nr_scanned;
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002615
Mel Gormana9dd0a82016-07-28 15:46:02 -07002616 shrink_node_memcg(pgdat, memcg, sc, &lru_pages);
2617 node_lru_pages += lru_pages;
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002618
Vladimir Davydovb5afba22016-08-02 14:03:04 -07002619 if (memcg)
Mel Gormana9dd0a82016-07-28 15:46:02 -07002620 shrink_slab(sc->gfp_mask, pgdat->node_id,
Vladimir Davydovcb731d62015-02-12 14:58:54 -08002621 memcg, sc->nr_scanned - scanned,
2622 lru_pages);
2623
Johannes Weiner8e8ae642016-01-14 15:21:32 -08002624 /* Record the group's reclaim efficiency */
2625 vmpressure(sc->gfp_mask, memcg, false,
2626 sc->nr_scanned - scanned,
2627 sc->nr_reclaimed - reclaimed);
2628
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002629 /*
Michal Hockoa394cb82013-02-22 16:32:30 -08002630 * Direct reclaim and kswapd have to scan all memory
2631 * cgroups to fulfill the overall scan target for the
Mel Gormana9dd0a82016-07-28 15:46:02 -07002632 * node.
Michal Hockoa394cb82013-02-22 16:32:30 -08002633 *
2634 * Limit reclaim, on the other hand, only cares about
2635 * nr_to_reclaim pages to be reclaimed and it will
2636 * retry with decreasing priority if one round over the
2637 * whole hierarchy is not sufficient.
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002638 */
Michal Hockoa394cb82013-02-22 16:32:30 -08002639 if (!global_reclaim(sc) &&
2640 sc->nr_reclaimed >= sc->nr_to_reclaim) {
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002641 mem_cgroup_iter_break(root, memcg);
2642 break;
2643 }
Johannes Weiner241994ed2015-02-11 15:26:06 -08002644 } while ((memcg = mem_cgroup_iter(root, memcg, &reclaim)));
Anton Vorontsov70ddf632013-04-29 15:08:31 -07002645
Johannes Weiner6b4f7792014-12-12 16:56:13 -08002646 /*
2647 * Shrink the slab caches in the same proportion that
2648 * the eligible LRU pages were scanned.
2649 */
Mel Gormanb2e18752016-07-28 15:45:37 -07002650 if (global_reclaim(sc))
Mel Gormana9dd0a82016-07-28 15:46:02 -07002651 shrink_slab(sc->gfp_mask, pgdat->node_id, NULL,
Vladimir Davydovcb731d62015-02-12 14:58:54 -08002652 sc->nr_scanned - nr_scanned,
Mel Gormana9dd0a82016-07-28 15:46:02 -07002653 node_lru_pages);
Johannes Weiner6b4f7792014-12-12 16:56:13 -08002654
Vladimir Davydovcb731d62015-02-12 14:58:54 -08002655 if (reclaim_state) {
2656 sc->nr_reclaimed += reclaim_state->reclaimed_slab;
2657 reclaim_state->reclaimed_slab = 0;
Johannes Weiner6b4f7792014-12-12 16:56:13 -08002658 }
2659
Johannes Weiner8e8ae642016-01-14 15:21:32 -08002660 /* Record the subtree's reclaim efficiency */
2661 vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true,
Anton Vorontsov70ddf632013-04-29 15:08:31 -07002662 sc->nr_scanned - nr_scanned,
2663 sc->nr_reclaimed - nr_reclaimed);
2664
Johannes Weiner2344d7e2014-08-06 16:06:15 -07002665 if (sc->nr_reclaimed - nr_reclaimed)
2666 reclaimable = true;
2667
Mel Gormana9dd0a82016-07-28 15:46:02 -07002668 } while (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed,
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002669 sc->nr_scanned - nr_scanned, sc));
Johannes Weiner2344d7e2014-08-06 16:06:15 -07002670
Johannes Weinerc73322d2017-05-03 14:51:51 -07002671 /*
2672 * Kswapd gives up on balancing particular nodes after too
2673 * many failures to reclaim anything from them and goes to
2674 * sleep. On reclaim progress, reset the failure counter. A
2675 * successful direct reclaim run will revive a dormant kswapd.
2676 */
2677 if (reclaimable)
2678 pgdat->kswapd_failures = 0;
2679
Johannes Weiner2344d7e2014-08-06 16:06:15 -07002680 return reclaimable;
Johannes Weinerf16015f2012-01-12 17:17:52 -08002681}
2682
Vlastimil Babka53853e22014-10-09 15:27:02 -07002683/*
Vlastimil Babkafdd4c6142016-10-07 16:58:03 -07002684 * Returns true if compaction should go ahead for a costly-order request, or
2685 * the allocation would already succeed without compaction. Return false if we
2686 * should reclaim first.
Vlastimil Babka53853e22014-10-09 15:27:02 -07002687 */
Mel Gorman4f588332016-07-28 15:46:38 -07002688static inline bool compaction_ready(struct zone *zone, struct scan_control *sc)
Mel Gormanfe4b1b22012-01-12 17:19:45 -08002689{
Mel Gorman31483b62016-07-28 15:45:46 -07002690 unsigned long watermark;
Vlastimil Babkafdd4c6142016-10-07 16:58:03 -07002691 enum compact_result suitable;
Mel Gormanfe4b1b22012-01-12 17:19:45 -08002692
Vlastimil Babkafdd4c6142016-10-07 16:58:03 -07002693 suitable = compaction_suitable(zone, sc->order, 0, sc->reclaim_idx);
2694 if (suitable == COMPACT_SUCCESS)
2695 /* Allocation should succeed already. Don't reclaim. */
2696 return true;
2697 if (suitable == COMPACT_SKIPPED)
2698 /* Compaction cannot yet proceed. Do reclaim. */
Mel Gormanfe4b1b22012-01-12 17:19:45 -08002699 return false;
2700
Vlastimil Babkafdd4c6142016-10-07 16:58:03 -07002701 /*
2702 * Compaction is already possible, but it takes time to run and there
2703 * are potentially other callers using the pages just freed. So proceed
2704 * with reclaim to make a buffer of free pages available to give
2705 * compaction a reasonable chance of completing and allocating the page.
2706 * Note that we won't actually reclaim the whole buffer in one attempt
2707 * as the target watermark in should_continue_reclaim() is lower. But if
2708 * we are already above the high+gap watermark, don't reclaim at all.
2709 */
2710 watermark = high_wmark_pages(zone) + compact_gap(sc->order);
2711
2712 return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx);
Mel Gormanfe4b1b22012-01-12 17:19:45 -08002713}
2714
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715/*
2716 * This is the direct reclaim path, for page-allocating processes. We only
2717 * try to reclaim pages from zones which will satisfy the caller's allocation
2718 * request.
2719 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 * If a zone is deemed to be full of pinned pages then just give it a light
2721 * scan then give up on it.
2722 */
Michal Hocko0a0337e2016-05-20 16:57:00 -07002723static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724{
Mel Gormandd1a2392008-04-28 02:12:17 -07002725 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07002726 struct zone *zone;
Andrew Morton0608f432013-09-24 15:27:41 -07002727 unsigned long nr_soft_reclaimed;
2728 unsigned long nr_soft_scanned;
Weijie Yang619d0d762014-04-07 15:36:59 -07002729 gfp_t orig_mask;
Mel Gorman79dafcd2016-07-28 15:45:53 -07002730 pg_data_t *last_pgdat = NULL;
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08002731
Mel Gormancc715d92012-03-21 16:34:00 -07002732 /*
2733 * If the number of buffer_heads in the machine exceeds the maximum
2734 * allowed level, force direct reclaim to scan the highmem zone as
2735 * highmem pages could be pinning lowmem pages storing buffer_heads
2736 */
Weijie Yang619d0d762014-04-07 15:36:59 -07002737 orig_mask = sc->gfp_mask;
Mel Gormanb2e18752016-07-28 15:45:37 -07002738 if (buffer_heads_over_limit) {
Mel Gormancc715d92012-03-21 16:34:00 -07002739 sc->gfp_mask |= __GFP_HIGHMEM;
Mel Gorman4f588332016-07-28 15:46:38 -07002740 sc->reclaim_idx = gfp_zone(sc->gfp_mask);
Mel Gormanb2e18752016-07-28 15:45:37 -07002741 }
Mel Gormancc715d92012-03-21 16:34:00 -07002742
Mel Gormand4debc62010-08-09 17:19:29 -07002743 for_each_zone_zonelist_nodemask(zone, z, zonelist,
Mel Gormanb2e18752016-07-28 15:45:37 -07002744 sc->reclaim_idx, sc->nodemask) {
Mel Gormanb2e18752016-07-28 15:45:37 -07002745 /*
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08002746 * Take care memory controller reclaiming has small influence
2747 * to global LRU.
2748 */
Johannes Weiner89b5fae2012-01-12 17:17:50 -08002749 if (global_reclaim(sc)) {
Vladimir Davydov344736f2014-10-20 15:50:30 +04002750 if (!cpuset_zone_allowed(zone,
2751 GFP_KERNEL | __GFP_HARDWALL))
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08002752 continue;
Vladimir Davydov65ec02c2014-04-03 14:47:20 -07002753
Johannes Weiner0b064962014-08-06 16:06:12 -07002754 /*
2755 * If we already have plenty of memory free for
2756 * compaction in this zone, don't free any more.
2757 * Even though compaction is invoked for any
2758 * non-zero order, only frequent costly order
2759 * reclamation is disruptive enough to become a
2760 * noticeable problem, like transparent huge
2761 * page allocations.
2762 */
2763 if (IS_ENABLED(CONFIG_COMPACTION) &&
2764 sc->order > PAGE_ALLOC_COSTLY_ORDER &&
Mel Gorman4f588332016-07-28 15:46:38 -07002765 compaction_ready(zone, sc)) {
Johannes Weiner0b064962014-08-06 16:06:12 -07002766 sc->compaction_ready = true;
2767 continue;
Rik van Riele0887c12011-10-31 17:09:31 -07002768 }
Johannes Weiner0b064962014-08-06 16:06:12 -07002769
Andrew Morton0608f432013-09-24 15:27:41 -07002770 /*
Mel Gorman79dafcd2016-07-28 15:45:53 -07002771 * Shrink each node in the zonelist once. If the
2772 * zonelist is ordered by zone (not the default) then a
2773 * node may be shrunk multiple times but in that case
2774 * the user prefers lower zones being preserved.
2775 */
2776 if (zone->zone_pgdat == last_pgdat)
2777 continue;
2778
2779 /*
Andrew Morton0608f432013-09-24 15:27:41 -07002780 * This steals pages from memory cgroups over softlimit
2781 * and returns the number of reclaimed pages and
2782 * scanned pages. This works for global memory pressure
2783 * and balancing, not for a memcg's limit.
2784 */
2785 nr_soft_scanned = 0;
Mel Gormanef8f2322016-07-28 15:46:05 -07002786 nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat,
Andrew Morton0608f432013-09-24 15:27:41 -07002787 sc->order, sc->gfp_mask,
2788 &nr_soft_scanned);
2789 sc->nr_reclaimed += nr_soft_reclaimed;
2790 sc->nr_scanned += nr_soft_scanned;
KAMEZAWA Hiroyukiac34a1a2011-06-27 16:18:12 -07002791 /* need some check for avoid more shrink_zone() */
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08002792 }
Nick Piggin408d8542006-09-25 23:31:27 -07002793
Mel Gorman79dafcd2016-07-28 15:45:53 -07002794 /* See comment about same check for global reclaim above */
2795 if (zone->zone_pgdat == last_pgdat)
2796 continue;
2797 last_pgdat = zone->zone_pgdat;
Mel Gorman970a39a2016-07-28 15:46:35 -07002798 shrink_node(zone->zone_pgdat, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 }
Mel Gormane0c23272011-10-31 17:09:33 -07002800
Vladimir Davydov65ec02c2014-04-03 14:47:20 -07002801 /*
Weijie Yang619d0d762014-04-07 15:36:59 -07002802 * Restore to original mask to avoid the impact on the caller if we
2803 * promoted it to __GFP_HIGHMEM.
2804 */
2805 sc->gfp_mask = orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806}
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002807
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002808static void snapshot_refaults(struct mem_cgroup *root_memcg, pg_data_t *pgdat)
2809{
2810 struct mem_cgroup *memcg;
2811
2812 memcg = mem_cgroup_iter(root_memcg, NULL, NULL);
2813 do {
2814 unsigned long refaults;
2815 struct lruvec *lruvec;
2816
2817 if (memcg)
Johannes Weinerccda7f42017-05-03 14:55:16 -07002818 refaults = memcg_page_state(memcg, WORKINGSET_ACTIVATE);
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002819 else
2820 refaults = node_page_state(pgdat, WORKINGSET_ACTIVATE);
2821
2822 lruvec = mem_cgroup_lruvec(pgdat, memcg);
2823 lruvec->refaults = refaults;
2824 } while ((memcg = mem_cgroup_iter(root_memcg, memcg, NULL)));
2825}
2826
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827/*
2828 * This is the main entry point to direct page reclaim.
2829 *
2830 * If a full scan of the inactive list fails to free enough memory then we
2831 * are "out of memory" and something needs to be killed.
2832 *
2833 * If the caller is !__GFP_FS then the probability of a failure is reasonably
2834 * high - the zone may be full of dirty or under-writeback pages, which this
Jens Axboe5b0830c2009-09-23 19:37:09 +02002835 * caller can't do much about. We kick the writeback threads and take explicit
2836 * naps in the hope that some of these pages can be written. But if the
2837 * allocating task holds filesystem locks which prevent writeout this might not
2838 * work, and the allocation attempt will fail.
Nishanth Aravamudana41f24e2008-04-29 00:58:25 -07002839 *
2840 * returns: 0, if no pages reclaimed
2841 * else, the number of pages reclaimed
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 */
Mel Gormandac1d272008-04-28 02:12:12 -07002843static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
Vladimir Davydov3115cd92014-04-03 14:47:22 -07002844 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845{
Johannes Weiner241994ed2015-02-11 15:26:06 -08002846 int initial_priority = sc->priority;
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002847 pg_data_t *last_pgdat;
2848 struct zoneref *z;
2849 struct zone *zone;
Johannes Weiner241994ed2015-02-11 15:26:06 -08002850retry:
Keika Kobayashi873b4772008-07-25 01:48:52 -07002851 delayacct_freepages_start();
2852
Johannes Weiner89b5fae2012-01-12 17:17:50 -08002853 if (global_reclaim(sc))
Mel Gorman7cc30fc2016-07-28 15:46:59 -07002854 __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002856 do {
Anton Vorontsov70ddf632013-04-29 15:08:31 -07002857 vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup,
2858 sc->priority);
Balbir Singh66e17072008-02-07 00:13:56 -08002859 sc->nr_scanned = 0;
Michal Hocko0a0337e2016-05-20 16:57:00 -07002860 shrink_zones(zonelist, sc);
Mel Gormane0c23272011-10-31 17:09:33 -07002861
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07002862 if (sc->nr_reclaimed >= sc->nr_to_reclaim)
Johannes Weiner0b064962014-08-06 16:06:12 -07002863 break;
2864
2865 if (sc->compaction_ready)
2866 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
2868 /*
Minchan Kim0e50ce32013-02-22 16:35:37 -08002869 * If we're getting trouble reclaiming, start doing
2870 * writepage even in laptop mode.
2871 */
2872 if (sc->priority < DEF_PRIORITY - 2)
2873 sc->may_writepage = 1;
Johannes Weiner0b064962014-08-06 16:06:12 -07002874 } while (--sc->priority >= 0);
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07002875
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002876 last_pgdat = NULL;
2877 for_each_zone_zonelist_nodemask(zone, z, zonelist, sc->reclaim_idx,
2878 sc->nodemask) {
2879 if (zone->zone_pgdat == last_pgdat)
2880 continue;
2881 last_pgdat = zone->zone_pgdat;
2882 snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat);
2883 }
2884
Keika Kobayashi873b4772008-07-25 01:48:52 -07002885 delayacct_freepages_end();
2886
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07002887 if (sc->nr_reclaimed)
2888 return sc->nr_reclaimed;
2889
Mel Gorman0cee34f2012-01-12 17:19:49 -08002890 /* Aborted reclaim to try compaction? don't OOM, then */
Johannes Weiner0b064962014-08-06 16:06:12 -07002891 if (sc->compaction_ready)
Mel Gorman73350842012-01-12 17:19:33 -08002892 return 1;
2893
Johannes Weiner241994ed2015-02-11 15:26:06 -08002894 /* Untapped cgroup reserves? Don't OOM, retry. */
Yisheng Xied6622f62017-05-03 14:53:57 -07002895 if (sc->memcg_low_skipped) {
Johannes Weiner241994ed2015-02-11 15:26:06 -08002896 sc->priority = initial_priority;
Yisheng Xied6622f62017-05-03 14:53:57 -07002897 sc->memcg_low_reclaim = 1;
2898 sc->memcg_low_skipped = 0;
Johannes Weiner241994ed2015-02-11 15:26:06 -08002899 goto retry;
2900 }
2901
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07002902 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903}
2904
Johannes Weinerc73322d2017-05-03 14:51:51 -07002905static bool allow_direct_reclaim(pg_data_t *pgdat)
Mel Gorman55150612012-07-31 16:44:35 -07002906{
2907 struct zone *zone;
2908 unsigned long pfmemalloc_reserve = 0;
2909 unsigned long free_pages = 0;
2910 int i;
2911 bool wmark_ok;
2912
Johannes Weinerc73322d2017-05-03 14:51:51 -07002913 if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
2914 return true;
2915
Mel Gorman55150612012-07-31 16:44:35 -07002916 for (i = 0; i <= ZONE_NORMAL; i++) {
2917 zone = &pgdat->node_zones[i];
Johannes Weinerd450abd2017-05-03 14:51:54 -07002918 if (!managed_zone(zone))
2919 continue;
2920
2921 if (!zone_reclaimable_pages(zone))
Mel Gorman675becc2014-06-04 16:07:35 -07002922 continue;
2923
Mel Gorman55150612012-07-31 16:44:35 -07002924 pfmemalloc_reserve += min_wmark_pages(zone);
2925 free_pages += zone_page_state(zone, NR_FREE_PAGES);
2926 }
2927
Mel Gorman675becc2014-06-04 16:07:35 -07002928 /* If there are no reserves (unexpected config) then do not throttle */
2929 if (!pfmemalloc_reserve)
2930 return true;
2931
Mel Gorman55150612012-07-31 16:44:35 -07002932 wmark_ok = free_pages > pfmemalloc_reserve / 2;
2933
2934 /* kswapd must be awake if processes are being throttled */
2935 if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) {
Mel Gorman38087d92016-07-28 15:45:49 -07002936 pgdat->kswapd_classzone_idx = min(pgdat->kswapd_classzone_idx,
Mel Gorman55150612012-07-31 16:44:35 -07002937 (enum zone_type)ZONE_NORMAL);
2938 wake_up_interruptible(&pgdat->kswapd_wait);
2939 }
2940
2941 return wmark_ok;
2942}
2943
2944/*
2945 * Throttle direct reclaimers if backing storage is backed by the network
2946 * and the PFMEMALLOC reserve for the preferred node is getting dangerously
2947 * depleted. kswapd will continue to make progress and wake the processes
Mel Gorman50694c22012-11-26 16:29:48 -08002948 * when the low watermark is reached.
2949 *
2950 * Returns true if a fatal signal was delivered during throttling. If this
2951 * happens, the page allocator should not consider triggering the OOM killer.
Mel Gorman55150612012-07-31 16:44:35 -07002952 */
Mel Gorman50694c22012-11-26 16:29:48 -08002953static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist,
Mel Gorman55150612012-07-31 16:44:35 -07002954 nodemask_t *nodemask)
2955{
Mel Gorman675becc2014-06-04 16:07:35 -07002956 struct zoneref *z;
Mel Gorman55150612012-07-31 16:44:35 -07002957 struct zone *zone;
Mel Gorman675becc2014-06-04 16:07:35 -07002958 pg_data_t *pgdat = NULL;
Mel Gorman55150612012-07-31 16:44:35 -07002959
2960 /*
2961 * Kernel threads should not be throttled as they may be indirectly
2962 * responsible for cleaning pages necessary for reclaim to make forward
2963 * progress. kjournald for example may enter direct reclaim while
2964 * committing a transaction where throttling it could forcing other
2965 * processes to block on log_wait_commit().
2966 */
2967 if (current->flags & PF_KTHREAD)
Mel Gorman50694c22012-11-26 16:29:48 -08002968 goto out;
2969
2970 /*
2971 * If a fatal signal is pending, this process should not throttle.
2972 * It should return quickly so it can exit and free its memory
2973 */
2974 if (fatal_signal_pending(current))
2975 goto out;
Mel Gorman55150612012-07-31 16:44:35 -07002976
Mel Gorman675becc2014-06-04 16:07:35 -07002977 /*
2978 * Check if the pfmemalloc reserves are ok by finding the first node
2979 * with a usable ZONE_NORMAL or lower zone. The expectation is that
2980 * GFP_KERNEL will be required for allocating network buffers when
2981 * swapping over the network so ZONE_HIGHMEM is unusable.
2982 *
2983 * Throttling is based on the first usable node and throttled processes
2984 * wait on a queue until kswapd makes progress and wakes them. There
2985 * is an affinity then between processes waking up and where reclaim
2986 * progress has been made assuming the process wakes on the same node.
2987 * More importantly, processes running on remote nodes will not compete
2988 * for remote pfmemalloc reserves and processes on different nodes
2989 * should make reasonable progress.
2990 */
2991 for_each_zone_zonelist_nodemask(zone, z, zonelist,
Michael S. Tsirkin17636fa2015-01-26 12:58:41 -08002992 gfp_zone(gfp_mask), nodemask) {
Mel Gorman675becc2014-06-04 16:07:35 -07002993 if (zone_idx(zone) > ZONE_NORMAL)
2994 continue;
2995
2996 /* Throttle based on the first usable node */
2997 pgdat = zone->zone_pgdat;
Johannes Weinerc73322d2017-05-03 14:51:51 -07002998 if (allow_direct_reclaim(pgdat))
Mel Gorman675becc2014-06-04 16:07:35 -07002999 goto out;
3000 break;
3001 }
3002
3003 /* If no zone was usable by the allocation flags then do not throttle */
3004 if (!pgdat)
Mel Gorman50694c22012-11-26 16:29:48 -08003005 goto out;
Mel Gorman55150612012-07-31 16:44:35 -07003006
Mel Gorman68243e72012-07-31 16:44:39 -07003007 /* Account for the throttling */
3008 count_vm_event(PGSCAN_DIRECT_THROTTLE);
3009
Mel Gorman55150612012-07-31 16:44:35 -07003010 /*
3011 * If the caller cannot enter the filesystem, it's possible that it
3012 * is due to the caller holding an FS lock or performing a journal
3013 * transaction in the case of a filesystem like ext[3|4]. In this case,
3014 * it is not safe to block on pfmemalloc_wait as kswapd could be
3015 * blocked waiting on the same lock. Instead, throttle for up to a
3016 * second before continuing.
3017 */
3018 if (!(gfp_mask & __GFP_FS)) {
3019 wait_event_interruptible_timeout(pgdat->pfmemalloc_wait,
Johannes Weinerc73322d2017-05-03 14:51:51 -07003020 allow_direct_reclaim(pgdat), HZ);
Mel Gorman50694c22012-11-26 16:29:48 -08003021
3022 goto check_pending;
Mel Gorman55150612012-07-31 16:44:35 -07003023 }
3024
3025 /* Throttle until kswapd wakes the process */
3026 wait_event_killable(zone->zone_pgdat->pfmemalloc_wait,
Johannes Weinerc73322d2017-05-03 14:51:51 -07003027 allow_direct_reclaim(pgdat));
Mel Gorman50694c22012-11-26 16:29:48 -08003028
3029check_pending:
3030 if (fatal_signal_pending(current))
3031 return true;
3032
3033out:
3034 return false;
Mel Gorman55150612012-07-31 16:44:35 -07003035}
3036
Mel Gormandac1d272008-04-28 02:12:12 -07003037unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
KAMEZAWA Hiroyuki327c0e92009-03-31 15:23:31 -07003038 gfp_t gfp_mask, nodemask_t *nodemask)
Balbir Singh66e17072008-02-07 00:13:56 -08003039{
Mel Gorman33906bc2010-08-09 17:19:16 -07003040 unsigned long nr_reclaimed;
Balbir Singh66e17072008-02-07 00:13:56 -08003041 struct scan_control sc = {
KOSAKI Motohiro22fba332009-12-14 17:59:10 -08003042 .nr_to_reclaim = SWAP_CLUSTER_MAX,
Nick Desaulniersf2f43e52017-07-06 15:36:50 -07003043 .gfp_mask = current_gfp_context(gfp_mask),
Mel Gormanb2e18752016-07-28 15:45:37 -07003044 .reclaim_idx = gfp_zone(gfp_mask),
Johannes Weineree814fe2014-08-06 16:06:19 -07003045 .order = order,
3046 .nodemask = nodemask,
3047 .priority = DEF_PRIORITY,
3048 .may_writepage = !laptop_mode,
Johannes Weinera6dc60f82009-03-31 15:19:30 -07003049 .may_unmap = 1,
KOSAKI Motohiro2e2e4252009-04-21 12:24:57 -07003050 .may_swap = 1,
Balbir Singh66e17072008-02-07 00:13:56 -08003051 };
3052
Mel Gorman55150612012-07-31 16:44:35 -07003053 /*
Mel Gorman50694c22012-11-26 16:29:48 -08003054 * Do not enter reclaim if fatal signal was delivered while throttled.
3055 * 1 is returned so that the page allocator does not OOM kill at this
3056 * point.
Mel Gorman55150612012-07-31 16:44:35 -07003057 */
Nick Desaulniersf2f43e52017-07-06 15:36:50 -07003058 if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask))
Mel Gorman55150612012-07-31 16:44:35 -07003059 return 1;
3060
Mel Gorman33906bc2010-08-09 17:19:16 -07003061 trace_mm_vmscan_direct_reclaim_begin(order,
3062 sc.may_writepage,
Nick Desaulniersf2f43e52017-07-06 15:36:50 -07003063 sc.gfp_mask,
Mel Gormane5146b12016-07-28 15:46:47 -07003064 sc.reclaim_idx);
Mel Gorman33906bc2010-08-09 17:19:16 -07003065
Vladimir Davydov3115cd92014-04-03 14:47:22 -07003066 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
Mel Gorman33906bc2010-08-09 17:19:16 -07003067
3068 trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
3069
3070 return nr_reclaimed;
Balbir Singh66e17072008-02-07 00:13:56 -08003071}
3072
Andrew Mortonc255a452012-07-31 16:43:02 -07003073#ifdef CONFIG_MEMCG
Balbir Singh66e17072008-02-07 00:13:56 -08003074
Mel Gormana9dd0a82016-07-28 15:46:02 -07003075unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg,
Balbir Singh4e416952009-09-23 15:56:39 -07003076 gfp_t gfp_mask, bool noswap,
Mel Gormanef8f2322016-07-28 15:46:05 -07003077 pg_data_t *pgdat,
Ying Han0ae5e892011-05-26 16:25:25 -07003078 unsigned long *nr_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07003079{
3080 struct scan_control sc = {
KOSAKI Motohirob8f5c562010-08-10 18:03:02 -07003081 .nr_to_reclaim = SWAP_CLUSTER_MAX,
Johannes Weineree814fe2014-08-06 16:06:19 -07003082 .target_mem_cgroup = memcg,
Balbir Singh4e416952009-09-23 15:56:39 -07003083 .may_writepage = !laptop_mode,
3084 .may_unmap = 1,
Mel Gormanb2e18752016-07-28 15:45:37 -07003085 .reclaim_idx = MAX_NR_ZONES - 1,
Balbir Singh4e416952009-09-23 15:56:39 -07003086 .may_swap = !noswap,
Balbir Singh4e416952009-09-23 15:56:39 -07003087 };
Johannes Weiner6b4f7792014-12-12 16:56:13 -08003088 unsigned long lru_pages;
Ying Han0ae5e892011-05-26 16:25:25 -07003089
Balbir Singh4e416952009-09-23 15:56:39 -07003090 sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
3091 (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003092
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07003093 trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order,
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003094 sc.may_writepage,
Mel Gormane5146b12016-07-28 15:46:47 -07003095 sc.gfp_mask,
3096 sc.reclaim_idx);
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003097
Balbir Singh4e416952009-09-23 15:56:39 -07003098 /*
3099 * NOTE: Although we can get the priority field, using it
3100 * here is not a good idea, since it limits the pages we can scan.
Mel Gormana9dd0a82016-07-28 15:46:02 -07003101 * if we don't reclaim here, the shrink_node from balance_pgdat
Balbir Singh4e416952009-09-23 15:56:39 -07003102 * will pick up pages from other mem cgroup's as well. We hack
3103 * the priority and make it zero.
3104 */
Mel Gormanef8f2322016-07-28 15:46:05 -07003105 shrink_node_memcg(pgdat, memcg, &sc, &lru_pages);
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003106
3107 trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed);
3108
Ying Han0ae5e892011-05-26 16:25:25 -07003109 *nr_scanned = sc.nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003110 return sc.nr_reclaimed;
3111}
3112
Johannes Weiner72835c82012-01-12 17:18:32 -08003113unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003114 unsigned long nr_pages,
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003115 gfp_t gfp_mask,
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003116 bool may_swap)
Balbir Singh66e17072008-02-07 00:13:56 -08003117{
Balbir Singh4e416952009-09-23 15:56:39 -07003118 struct zonelist *zonelist;
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003119 unsigned long nr_reclaimed;
Ying Han889976d2011-05-26 16:25:33 -07003120 int nid;
Vlastimil Babka499118e2017-05-08 15:59:50 -07003121 unsigned int noreclaim_flag;
Balbir Singh66e17072008-02-07 00:13:56 -08003122 struct scan_control sc = {
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003123 .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
Michal Hocko7dea19f2017-05-03 14:53:15 -07003124 .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) |
Johannes Weineree814fe2014-08-06 16:06:19 -07003125 (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK),
Mel Gormanb2e18752016-07-28 15:45:37 -07003126 .reclaim_idx = MAX_NR_ZONES - 1,
Johannes Weineree814fe2014-08-06 16:06:19 -07003127 .target_mem_cgroup = memcg,
3128 .priority = DEF_PRIORITY,
Balbir Singh66e17072008-02-07 00:13:56 -08003129 .may_writepage = !laptop_mode,
Johannes Weinera6dc60f82009-03-31 15:19:30 -07003130 .may_unmap = 1,
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003131 .may_swap = may_swap,
Ying Hana09ed5e2011-05-24 17:12:26 -07003132 };
Balbir Singh66e17072008-02-07 00:13:56 -08003133
Ying Han889976d2011-05-26 16:25:33 -07003134 /*
3135 * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't
3136 * take care of from where we get pages. So the node where we start the
3137 * scan does not need to be the current node.
3138 */
Johannes Weiner72835c82012-01-12 17:18:32 -08003139 nid = mem_cgroup_select_victim_node(memcg);
Ying Han889976d2011-05-26 16:25:33 -07003140
Aneesh Kumar K.Vc9634cf2016-10-07 16:59:12 -07003141 zonelist = &NODE_DATA(nid)->node_zonelists[ZONELIST_FALLBACK];
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003142
3143 trace_mm_vmscan_memcg_reclaim_begin(0,
3144 sc.may_writepage,
Mel Gormane5146b12016-07-28 15:46:47 -07003145 sc.gfp_mask,
3146 sc.reclaim_idx);
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003147
Vlastimil Babka499118e2017-05-08 15:59:50 -07003148 noreclaim_flag = memalloc_noreclaim_save();
Vladimir Davydov3115cd92014-04-03 14:47:22 -07003149 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
Vlastimil Babka499118e2017-05-08 15:59:50 -07003150 memalloc_noreclaim_restore(noreclaim_flag);
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003151
3152 trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
3153
3154 return nr_reclaimed;
Balbir Singh66e17072008-02-07 00:13:56 -08003155}
3156#endif
3157
Mel Gorman1d82de62016-07-28 15:45:43 -07003158static void age_active_anon(struct pglist_data *pgdat,
Mel Gormanef8f2322016-07-28 15:46:05 -07003159 struct scan_control *sc)
Johannes Weinerf16015f2012-01-12 17:17:52 -08003160{
Johannes Weinerb95a2f22012-01-12 17:18:06 -08003161 struct mem_cgroup *memcg;
Johannes Weinerf16015f2012-01-12 17:17:52 -08003162
Johannes Weinerb95a2f22012-01-12 17:18:06 -08003163 if (!total_swap_pages)
3164 return;
3165
3166 memcg = mem_cgroup_iter(NULL, NULL, NULL);
3167 do {
Mel Gormanef8f2322016-07-28 15:46:05 -07003168 struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg);
Johannes Weinerb95a2f22012-01-12 17:18:06 -08003169
Johannes Weiner2a2e4882017-05-03 14:55:03 -07003170 if (inactive_list_is_low(lruvec, false, memcg, sc, true))
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07003171 shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07003172 sc, LRU_ACTIVE_ANON);
Johannes Weinerb95a2f22012-01-12 17:18:06 -08003173
3174 memcg = mem_cgroup_iter(NULL, memcg, NULL);
3175 } while (memcg);
Johannes Weinerf16015f2012-01-12 17:17:52 -08003176}
3177
Mel Gormane716f2e2017-05-03 14:53:45 -07003178/*
3179 * Returns true if there is an eligible zone balanced for the request order
3180 * and classzone_idx
3181 */
3182static bool pgdat_balanced(pg_data_t *pgdat, int order, int classzone_idx)
Johannes Weiner60cefed2012-11-29 13:54:23 -08003183{
Mel Gormane716f2e2017-05-03 14:53:45 -07003184 int i;
3185 unsigned long mark = -1;
3186 struct zone *zone;
Johannes Weiner60cefed2012-11-29 13:54:23 -08003187
Mel Gormane716f2e2017-05-03 14:53:45 -07003188 for (i = 0; i <= classzone_idx; i++) {
3189 zone = pgdat->node_zones + i;
Mel Gorman6256c6b2016-07-28 15:45:56 -07003190
Mel Gormane716f2e2017-05-03 14:53:45 -07003191 if (!managed_zone(zone))
3192 continue;
3193
3194 mark = high_wmark_pages(zone);
3195 if (zone_watermark_ok_safe(zone, order, mark, classzone_idx))
3196 return true;
3197 }
3198
3199 /*
3200 * If a node has no populated zone within classzone_idx, it does not
3201 * need balancing by definition. This can happen if a zone-restricted
3202 * allocation tries to wake a remote kswapd.
3203 */
3204 if (mark == -1)
3205 return true;
3206
3207 return false;
Johannes Weiner60cefed2012-11-29 13:54:23 -08003208}
3209
Mel Gorman631b6e02017-05-03 14:53:41 -07003210/* Clear pgdat state for congested, dirty or under writeback. */
3211static void clear_pgdat_congested(pg_data_t *pgdat)
3212{
3213 clear_bit(PGDAT_CONGESTED, &pgdat->flags);
3214 clear_bit(PGDAT_DIRTY, &pgdat->flags);
3215 clear_bit(PGDAT_WRITEBACK, &pgdat->flags);
3216}
3217
Mel Gorman1741c872011-01-13 15:46:21 -08003218/*
Mel Gorman55150612012-07-31 16:44:35 -07003219 * Prepare kswapd for sleeping. This verifies that there are no processes
3220 * waiting in throttle_direct_reclaim() and that watermarks have been met.
3221 *
3222 * Returns true if kswapd is ready to sleep
3223 */
Mel Gormand9f21d42016-07-28 15:46:41 -07003224static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, int classzone_idx)
Mel Gormanf50de2d2009-12-14 17:58:53 -08003225{
Mel Gorman55150612012-07-31 16:44:35 -07003226 /*
Vlastimil Babka9e5e3662015-01-08 14:32:40 -08003227 * The throttled processes are normally woken up in balance_pgdat() as
Johannes Weinerc73322d2017-05-03 14:51:51 -07003228 * soon as allow_direct_reclaim() is true. But there is a potential
Vlastimil Babka9e5e3662015-01-08 14:32:40 -08003229 * race between when kswapd checks the watermarks and a process gets
3230 * throttled. There is also a potential race if processes get
3231 * throttled, kswapd wakes, a large process exits thereby balancing the
3232 * zones, which causes kswapd to exit balance_pgdat() before reaching
3233 * the wake up checks. If kswapd is going to sleep, no process should
3234 * be sleeping on pfmemalloc_wait, so wake them now if necessary. If
3235 * the wake up is premature, processes will wake kswapd and get
3236 * throttled again. The difference from wake ups in balance_pgdat() is
3237 * that here we are under prepare_to_wait().
Mel Gorman55150612012-07-31 16:44:35 -07003238 */
Vlastimil Babka9e5e3662015-01-08 14:32:40 -08003239 if (waitqueue_active(&pgdat->pfmemalloc_wait))
3240 wake_up_all(&pgdat->pfmemalloc_wait);
Mel Gormanf50de2d2009-12-14 17:58:53 -08003241
Johannes Weinerc73322d2017-05-03 14:51:51 -07003242 /* Hopeless node, leave it to direct reclaim */
3243 if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
3244 return true;
3245
Mel Gormane716f2e2017-05-03 14:53:45 -07003246 if (pgdat_balanced(pgdat, order, classzone_idx)) {
3247 clear_pgdat_congested(pgdat);
3248 return true;
Mel Gorman1d82de62016-07-28 15:45:43 -07003249 }
3250
Shantanu Goel333b0a42017-05-03 14:53:38 -07003251 return false;
Mel Gormanf50de2d2009-12-14 17:58:53 -08003252}
3253
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254/*
Mel Gorman1d82de62016-07-28 15:45:43 -07003255 * kswapd shrinks a node of pages that are at or below the highest usable
3256 * zone that is currently unbalanced.
Mel Gormanb8e83b92013-07-03 15:01:45 -07003257 *
3258 * Returns true if kswapd scanned at least the requested number of pages to
Mel Gorman283aba92013-07-03 15:01:51 -07003259 * reclaim or if the lack of progress was due to pages under writeback.
3260 * This is used to determine if the scanning priority needs to be raised.
Mel Gorman75485362013-07-03 15:01:42 -07003261 */
Mel Gorman1d82de62016-07-28 15:45:43 -07003262static bool kswapd_shrink_node(pg_data_t *pgdat,
Vlastimil Babkaaccf6242016-03-17 14:18:15 -07003263 struct scan_control *sc)
Mel Gorman75485362013-07-03 15:01:42 -07003264{
Mel Gorman1d82de62016-07-28 15:45:43 -07003265 struct zone *zone;
3266 int z;
Mel Gorman75485362013-07-03 15:01:42 -07003267
Mel Gorman1d82de62016-07-28 15:45:43 -07003268 /* Reclaim a number of pages proportional to the number of zones */
3269 sc->nr_to_reclaim = 0;
Mel Gorman970a39a2016-07-28 15:46:35 -07003270 for (z = 0; z <= sc->reclaim_idx; z++) {
Mel Gorman1d82de62016-07-28 15:45:43 -07003271 zone = pgdat->node_zones + z;
Mel Gorman6aa303d2016-09-01 16:14:55 -07003272 if (!managed_zone(zone))
Mel Gorman1d82de62016-07-28 15:45:43 -07003273 continue;
Mel Gorman7c954f62013-07-03 15:01:54 -07003274
Mel Gorman1d82de62016-07-28 15:45:43 -07003275 sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX);
Mel Gorman7c954f62013-07-03 15:01:54 -07003276 }
3277
Mel Gorman1d82de62016-07-28 15:45:43 -07003278 /*
3279 * Historically care was taken to put equal pressure on all zones but
3280 * now pressure is applied based on node LRU order.
3281 */
Mel Gorman970a39a2016-07-28 15:46:35 -07003282 shrink_node(pgdat, sc);
Mel Gorman1d82de62016-07-28 15:45:43 -07003283
3284 /*
3285 * Fragmentation may mean that the system cannot be rebalanced for
3286 * high-order allocations. If twice the allocation size has been
3287 * reclaimed then recheck watermarks only at order-0 to prevent
3288 * excessive reclaim. Assume that a process requested a high-order
3289 * can direct reclaim/compact.
3290 */
Vlastimil Babka9861a622016-10-07 16:57:53 -07003291 if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order))
Mel Gorman1d82de62016-07-28 15:45:43 -07003292 sc->order = 0;
3293
Mel Gormanb8e83b92013-07-03 15:01:45 -07003294 return sc->nr_scanned >= sc->nr_to_reclaim;
Mel Gorman75485362013-07-03 15:01:42 -07003295}
3296
3297/*
Mel Gorman1d82de62016-07-28 15:45:43 -07003298 * For kswapd, balance_pgdat() will reclaim pages across a node from zones
3299 * that are eligible for use by the caller until at least one zone is
3300 * balanced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 *
Mel Gorman1d82de62016-07-28 15:45:43 -07003302 * Returns the order kswapd finished reclaiming at.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 *
3304 * kswapd scans the zones in the highmem->normal->dma direction. It skips
Mel Gorman41858962009-06-16 15:32:12 -07003305 * zones which have free_pages > high_wmark_pages(zone), but once a zone is
Mel Gorman1d82de62016-07-28 15:45:43 -07003306 * found to have free_pages <= high_wmark_pages(zone), any page is that zone
3307 * or lower is eligible for reclaim until at least one usable zone is
3308 * balanced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 */
Vlastimil Babkaaccf6242016-03-17 14:18:15 -07003310static int balance_pgdat(pg_data_t *pgdat, int order, int classzone_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 int i;
Andrew Morton0608f432013-09-24 15:27:41 -07003313 unsigned long nr_soft_reclaimed;
3314 unsigned long nr_soft_scanned;
Mel Gorman1d82de62016-07-28 15:45:43 -07003315 struct zone *zone;
Andrew Morton179e9632006-03-22 00:08:18 -08003316 struct scan_control sc = {
3317 .gfp_mask = GFP_KERNEL,
Johannes Weineree814fe2014-08-06 16:06:19 -07003318 .order = order,
Mel Gormanb8e83b92013-07-03 15:01:45 -07003319 .priority = DEF_PRIORITY,
Johannes Weineree814fe2014-08-06 16:06:19 -07003320 .may_writepage = !laptop_mode,
Johannes Weinera6dc60f82009-03-31 15:19:30 -07003321 .may_unmap = 1,
KOSAKI Motohiro2e2e4252009-04-21 12:24:57 -07003322 .may_swap = 1,
Andrew Morton179e9632006-03-22 00:08:18 -08003323 };
Christoph Lameterf8891e52006-06-30 01:55:45 -07003324 count_vm_event(PAGEOUTRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07003326 do {
Johannes Weinerc73322d2017-05-03 14:51:51 -07003327 unsigned long nr_reclaimed = sc.nr_reclaimed;
Mel Gormanb8e83b92013-07-03 15:01:45 -07003328 bool raise_priority = true;
3329
Mel Gorman84c7a772016-07-28 15:46:44 -07003330 sc.reclaim_idx = classzone_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331
Mel Gorman86c79f62016-07-28 15:45:59 -07003332 /*
Mel Gorman84c7a772016-07-28 15:46:44 -07003333 * If the number of buffer_heads exceeds the maximum allowed
3334 * then consider reclaiming from all zones. This has a dual
3335 * purpose -- on 64-bit systems it is expected that
3336 * buffer_heads are stripped during active rotation. On 32-bit
3337 * systems, highmem pages can pin lowmem memory and shrinking
3338 * buffers can relieve lowmem pressure. Reclaim may still not
3339 * go ahead if all eligible zones for the original allocation
3340 * request are balanced to avoid excessive reclaim from kswapd.
Mel Gorman86c79f62016-07-28 15:45:59 -07003341 */
3342 if (buffer_heads_over_limit) {
3343 for (i = MAX_NR_ZONES - 1; i >= 0; i--) {
3344 zone = pgdat->node_zones + i;
Mel Gorman6aa303d2016-09-01 16:14:55 -07003345 if (!managed_zone(zone))
Mel Gorman86c79f62016-07-28 15:45:59 -07003346 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
Mel Gorman970a39a2016-07-28 15:46:35 -07003348 sc.reclaim_idx = i;
Andrew Mortone1dbeda2006-12-06 20:32:01 -08003349 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 }
Zlatko Calusicdafcb732013-02-22 16:32:34 -08003352
Mel Gorman86c79f62016-07-28 15:45:59 -07003353 /*
Mel Gormane716f2e2017-05-03 14:53:45 -07003354 * Only reclaim if there are no eligible zones. Note that
3355 * sc.reclaim_idx is not used as buffer_heads_over_limit may
3356 * have adjusted it.
Mel Gorman86c79f62016-07-28 15:45:59 -07003357 */
Mel Gormane716f2e2017-05-03 14:53:45 -07003358 if (pgdat_balanced(pgdat, sc.order, classzone_idx))
3359 goto out;
Andrew Mortone1dbeda2006-12-06 20:32:01 -08003360
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 /*
Mel Gorman1d82de62016-07-28 15:45:43 -07003362 * Do some background aging of the anon list, to give
3363 * pages a chance to be referenced before reclaiming. All
3364 * pages are rotated regardless of classzone as this is
3365 * about consistent aging.
3366 */
Mel Gormanef8f2322016-07-28 15:46:05 -07003367 age_active_anon(pgdat, &sc);
Mel Gorman1d82de62016-07-28 15:45:43 -07003368
3369 /*
Mel Gormanb7ea3c42013-07-03 15:01:53 -07003370 * If we're getting trouble reclaiming, start doing writepage
3371 * even in laptop mode.
3372 */
Johannes Weiner047d72c2017-05-03 14:51:57 -07003373 if (sc.priority < DEF_PRIORITY - 2)
Mel Gormanb7ea3c42013-07-03 15:01:53 -07003374 sc.may_writepage = 1;
3375
Mel Gorman1d82de62016-07-28 15:45:43 -07003376 /* Call soft limit reclaim before calling shrink_node. */
3377 sc.nr_scanned = 0;
3378 nr_soft_scanned = 0;
Mel Gormanef8f2322016-07-28 15:46:05 -07003379 nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order,
Mel Gorman1d82de62016-07-28 15:45:43 -07003380 sc.gfp_mask, &nr_soft_scanned);
3381 sc.nr_reclaimed += nr_soft_reclaimed;
3382
Mel Gormanb7ea3c42013-07-03 15:01:53 -07003383 /*
Mel Gorman1d82de62016-07-28 15:45:43 -07003384 * There should be no need to raise the scanning priority if
3385 * enough pages are already being scanned that that high
3386 * watermark would be met at 100% efficiency.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 */
Mel Gorman970a39a2016-07-28 15:46:35 -07003388 if (kswapd_shrink_node(pgdat, &sc))
Mel Gorman1d82de62016-07-28 15:45:43 -07003389 raise_priority = false;
Mel Gorman55150612012-07-31 16:44:35 -07003390
3391 /*
3392 * If the low watermark is met there is no need for processes
3393 * to be throttled on pfmemalloc_wait as they should not be
3394 * able to safely make forward progress. Wake them
3395 */
3396 if (waitqueue_active(&pgdat->pfmemalloc_wait) &&
Johannes Weinerc73322d2017-05-03 14:51:51 -07003397 allow_direct_reclaim(pgdat))
Vlastimil Babkacfc51152015-02-11 15:25:12 -08003398 wake_up_all(&pgdat->pfmemalloc_wait);
Mel Gorman55150612012-07-31 16:44:35 -07003399
Mel Gormanb8e83b92013-07-03 15:01:45 -07003400 /* Check if kswapd should be suspending */
3401 if (try_to_freeze() || kthread_should_stop())
3402 break;
3403
3404 /*
3405 * Raise priority if scanning rate is too low or there was no
3406 * progress in reclaiming pages
3407 */
Johannes Weinerc73322d2017-05-03 14:51:51 -07003408 nr_reclaimed = sc.nr_reclaimed - nr_reclaimed;
3409 if (raise_priority || !nr_reclaimed)
Mel Gormanb8e83b92013-07-03 15:01:45 -07003410 sc.priority--;
Mel Gorman1d82de62016-07-28 15:45:43 -07003411 } while (sc.priority >= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412
Johannes Weinerc73322d2017-05-03 14:51:51 -07003413 if (!sc.nr_reclaimed)
3414 pgdat->kswapd_failures++;
3415
Mel Gormanb8e83b92013-07-03 15:01:45 -07003416out:
Johannes Weiner2a2e4882017-05-03 14:55:03 -07003417 snapshot_refaults(NULL, pgdat);
Mel Gorman0abdee22011-01-13 15:46:22 -08003418 /*
Mel Gorman1d82de62016-07-28 15:45:43 -07003419 * Return the order kswapd stopped reclaiming at as
3420 * prepare_kswapd_sleep() takes it into account. If another caller
3421 * entered the allocator slow path while kswapd was awake, order will
3422 * remain at the higher level.
Mel Gorman0abdee22011-01-13 15:46:22 -08003423 */
Mel Gorman1d82de62016-07-28 15:45:43 -07003424 return sc.order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425}
3426
Mel Gormane716f2e2017-05-03 14:53:45 -07003427/*
3428 * pgdat->kswapd_classzone_idx is the highest zone index that a recent
3429 * allocation request woke kswapd for. When kswapd has not woken recently,
3430 * the value is MAX_NR_ZONES which is not a valid index. This compares a
3431 * given classzone and returns it or the highest classzone index kswapd
3432 * was recently woke for.
3433 */
3434static enum zone_type kswapd_classzone_idx(pg_data_t *pgdat,
3435 enum zone_type classzone_idx)
3436{
3437 if (pgdat->kswapd_classzone_idx == MAX_NR_ZONES)
3438 return classzone_idx;
3439
3440 return max(pgdat->kswapd_classzone_idx, classzone_idx);
3441}
3442
Mel Gorman38087d92016-07-28 15:45:49 -07003443static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order,
3444 unsigned int classzone_idx)
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08003445{
3446 long remaining = 0;
3447 DEFINE_WAIT(wait);
3448
3449 if (freezing(current) || kthread_should_stop())
3450 return;
3451
3452 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
3453
Shantanu Goel333b0a42017-05-03 14:53:38 -07003454 /*
3455 * Try to sleep for a short interval. Note that kcompactd will only be
3456 * woken if it is possible to sleep for a short interval. This is
3457 * deliberate on the assumption that if reclaim cannot keep an
3458 * eligible zone balanced that it's also unlikely that compaction will
3459 * succeed.
3460 */
Mel Gormand9f21d42016-07-28 15:46:41 -07003461 if (prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) {
Vlastimil Babkafd901c92016-04-28 16:18:49 -07003462 /*
3463 * Compaction records what page blocks it recently failed to
3464 * isolate pages from and skips them in the future scanning.
3465 * When kswapd is going to sleep, it is reasonable to assume
3466 * that pages and compaction may succeed so reset the cache.
3467 */
3468 reset_isolation_suitable(pgdat);
3469
3470 /*
3471 * We have freed the memory, now we should compact it to make
3472 * allocation of the requested order possible.
3473 */
Mel Gorman38087d92016-07-28 15:45:49 -07003474 wakeup_kcompactd(pgdat, alloc_order, classzone_idx);
Vlastimil Babkafd901c92016-04-28 16:18:49 -07003475
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08003476 remaining = schedule_timeout(HZ/10);
Mel Gorman38087d92016-07-28 15:45:49 -07003477
3478 /*
3479 * If woken prematurely then reset kswapd_classzone_idx and
3480 * order. The values will either be from a wakeup request or
3481 * the previous request that slept prematurely.
3482 */
3483 if (remaining) {
Mel Gormane716f2e2017-05-03 14:53:45 -07003484 pgdat->kswapd_classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx);
Mel Gorman38087d92016-07-28 15:45:49 -07003485 pgdat->kswapd_order = max(pgdat->kswapd_order, reclaim_order);
3486 }
3487
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08003488 finish_wait(&pgdat->kswapd_wait, &wait);
3489 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
3490 }
3491
3492 /*
3493 * After a short sleep, check if it was a premature sleep. If not, then
3494 * go fully to sleep until explicitly woken up.
3495 */
Mel Gormand9f21d42016-07-28 15:46:41 -07003496 if (!remaining &&
3497 prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) {
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08003498 trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
3499
3500 /*
3501 * vmstat counters are not perfectly accurate and the estimated
3502 * value for counters such as NR_FREE_PAGES can deviate from the
3503 * true value by nr_online_cpus * threshold. To avoid the zone
3504 * watermarks being breached while under pressure, we reduce the
3505 * per-cpu vmstat threshold while kswapd is awake and restore
3506 * them before going back to sleep.
3507 */
3508 set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold);
Aaditya Kumar1c7e7f62012-07-17 15:48:07 -07003509
3510 if (!kthread_should_stop())
3511 schedule();
3512
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08003513 set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold);
3514 } else {
3515 if (remaining)
3516 count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY);
3517 else
3518 count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY);
3519 }
3520 finish_wait(&pgdat->kswapd_wait, &wait);
3521}
3522
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523/*
3524 * The background pageout daemon, started as a kernel thread
Rik van Riel4f98a2f2008-10-18 20:26:32 -07003525 * from the init process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 *
3527 * This basically trickles out pages so that we have _some_
3528 * free memory available even if there is no other activity
3529 * that frees anything up. This is needed for things like routing
3530 * etc, where we otherwise might have all activity going on in
3531 * asynchronous contexts that cannot page things out.
3532 *
3533 * If there are applications that are active memory-allocators
3534 * (most normal use), this basically shouldn't matter.
3535 */
3536static int kswapd(void *p)
3537{
Mel Gormane716f2e2017-05-03 14:53:45 -07003538 unsigned int alloc_order, reclaim_order;
3539 unsigned int classzone_idx = MAX_NR_ZONES - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 pg_data_t *pgdat = (pg_data_t*)p;
3541 struct task_struct *tsk = current;
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08003542
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 struct reclaim_state reclaim_state = {
3544 .reclaimed_slab = 0,
3545 };
Rusty Russella70f7302009-03-13 14:49:46 +10303546 const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547
Rusty Russell174596a2009-01-01 10:12:29 +10303548 if (!cpumask_empty(cpumask))
Mike Travisc5f59f02008-04-04 18:11:10 -07003549 set_cpus_allowed_ptr(tsk, cpumask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 current->reclaim_state = &reclaim_state;
3551
3552 /*
3553 * Tell the memory management that we're a "memory allocator",
3554 * and that if we need more memory we should get access to it
3555 * regardless (see "__alloc_pages()"). "kswapd" should
3556 * never get caught in the normal page freeing logic.
3557 *
3558 * (Kswapd normally doesn't need memory anyway, but sometimes
3559 * you need a small amount of memory in order to be able to
3560 * page out something else, and this flag essentially protects
3561 * us from recursively trying to free more memory as we're
3562 * trying to free the first piece of memory in the first place).
3563 */
Christoph Lameter930d9152006-01-08 01:00:47 -08003564 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003565 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Mel Gormane716f2e2017-05-03 14:53:45 -07003567 pgdat->kswapd_order = 0;
3568 pgdat->kswapd_classzone_idx = MAX_NR_ZONES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 for ( ; ; ) {
Jeff Liu6f6313d2012-12-11 16:02:48 -08003570 bool ret;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07003571
Mel Gormane716f2e2017-05-03 14:53:45 -07003572 alloc_order = reclaim_order = pgdat->kswapd_order;
3573 classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx);
3574
Mel Gorman38087d92016-07-28 15:45:49 -07003575kswapd_try_sleep:
3576 kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order,
3577 classzone_idx);
Mel Gorman215ddd62011-07-08 15:39:40 -07003578
Mel Gorman38087d92016-07-28 15:45:49 -07003579 /* Read the new order and classzone_idx */
3580 alloc_order = reclaim_order = pgdat->kswapd_order;
Mel Gormane716f2e2017-05-03 14:53:45 -07003581 classzone_idx = kswapd_classzone_idx(pgdat, 0);
Mel Gorman38087d92016-07-28 15:45:49 -07003582 pgdat->kswapd_order = 0;
Mel Gormane716f2e2017-05-03 14:53:45 -07003583 pgdat->kswapd_classzone_idx = MAX_NR_ZONES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
David Rientjes8fe23e02009-12-14 17:58:33 -08003585 ret = try_to_freeze();
3586 if (kthread_should_stop())
3587 break;
3588
3589 /*
3590 * We can speed up thawing tasks if we don't call balance_pgdat
3591 * after returning from the refrigerator
3592 */
Mel Gorman38087d92016-07-28 15:45:49 -07003593 if (ret)
3594 continue;
Mel Gorman1d82de62016-07-28 15:45:43 -07003595
Mel Gorman38087d92016-07-28 15:45:49 -07003596 /*
3597 * Reclaim begins at the requested order but if a high-order
3598 * reclaim fails then kswapd falls back to reclaiming for
3599 * order-0. If that happens, kswapd will consider sleeping
3600 * for the order it finished reclaiming at (reclaim_order)
3601 * but kcompactd is woken to compact for the original
3602 * request (alloc_order).
3603 */
Mel Gormane5146b12016-07-28 15:46:47 -07003604 trace_mm_vmscan_kswapd_wake(pgdat->node_id, classzone_idx,
3605 alloc_order);
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01003606 fs_reclaim_acquire(GFP_KERNEL);
Mel Gorman38087d92016-07-28 15:45:49 -07003607 reclaim_order = balance_pgdat(pgdat, alloc_order, classzone_idx);
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01003608 fs_reclaim_release(GFP_KERNEL);
Mel Gorman38087d92016-07-28 15:45:49 -07003609 if (reclaim_order < alloc_order)
3610 goto kswapd_try_sleep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 }
Takamori Yamaguchib0a8cc52012-11-08 15:53:39 -08003612
Johannes Weiner71abdc12014-06-06 14:35:35 -07003613 tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD);
Takamori Yamaguchib0a8cc52012-11-08 15:53:39 -08003614 current->reclaim_state = NULL;
Johannes Weiner71abdc12014-06-06 14:35:35 -07003615
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 return 0;
3617}
3618
3619/*
3620 * A zone is low on free memory, so wake its kswapd task to service it.
3621 */
Mel Gorman99504742011-01-13 15:46:20 -08003622void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623{
3624 pg_data_t *pgdat;
3625
Mel Gorman6aa303d2016-09-01 16:14:55 -07003626 if (!managed_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 return;
3628
Vladimir Davydov344736f2014-10-20 15:50:30 +04003629 if (!cpuset_zone_allowed(zone, GFP_KERNEL | __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 return;
Mel Gorman88f5acf2011-01-13 15:45:41 -08003631 pgdat = zone->zone_pgdat;
Mel Gormane716f2e2017-05-03 14:53:45 -07003632 pgdat->kswapd_classzone_idx = kswapd_classzone_idx(pgdat,
3633 classzone_idx);
Mel Gorman38087d92016-07-28 15:45:49 -07003634 pgdat->kswapd_order = max(pgdat->kswapd_order, order);
Con Kolivas8d0986e2005-09-13 01:25:07 -07003635 if (!waitqueue_active(&pgdat->kswapd_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 return;
Mel Gormane1a55632016-07-28 15:46:26 -07003637
Johannes Weinerc73322d2017-05-03 14:51:51 -07003638 /* Hopeless node, leave it to direct reclaim */
3639 if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
3640 return;
3641
Mel Gormane716f2e2017-05-03 14:53:45 -07003642 if (pgdat_balanced(pgdat, order, classzone_idx))
3643 return;
Mel Gormane1a55632016-07-28 15:46:26 -07003644
Mel Gormane716f2e2017-05-03 14:53:45 -07003645 trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, classzone_idx, order);
Con Kolivas8d0986e2005-09-13 01:25:07 -07003646 wake_up_interruptible(&pgdat->kswapd_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647}
3648
Rafael J. Wysockic6f37f12009-05-24 22:16:31 +02003649#ifdef CONFIG_HIBERNATION
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650/*
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08003651 * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07003652 * freed pages.
3653 *
3654 * Rather than trying to age LRUs the aim is to preserve the overall
3655 * LRU order by reclaiming preferentially
3656 * inactive > active > active referenced > active mapped
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 */
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08003658unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659{
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07003660 struct reclaim_state reclaim_state;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07003661 struct scan_control sc = {
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08003662 .nr_to_reclaim = nr_to_reclaim,
Johannes Weineree814fe2014-08-06 16:06:19 -07003663 .gfp_mask = GFP_HIGHUSER_MOVABLE,
Mel Gormanb2e18752016-07-28 15:45:37 -07003664 .reclaim_idx = MAX_NR_ZONES - 1,
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07003665 .priority = DEF_PRIORITY,
Johannes Weineree814fe2014-08-06 16:06:19 -07003666 .may_writepage = 1,
3667 .may_unmap = 1,
3668 .may_swap = 1,
3669 .hibernation_mode = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 };
Ying Hana09ed5e2011-05-24 17:12:26 -07003671 struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08003672 struct task_struct *p = current;
3673 unsigned long nr_reclaimed;
Vlastimil Babka499118e2017-05-08 15:59:50 -07003674 unsigned int noreclaim_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675
Vlastimil Babka499118e2017-05-08 15:59:50 -07003676 noreclaim_flag = memalloc_noreclaim_save();
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01003677 fs_reclaim_acquire(sc.gfp_mask);
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08003678 reclaim_state.reclaimed_slab = 0;
3679 p->reclaim_state = &reclaim_state;
Andrew Morton69e05942006-03-22 00:08:19 -08003680
Vladimir Davydov3115cd92014-04-03 14:47:22 -07003681 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07003682
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08003683 p->reclaim_state = NULL;
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01003684 fs_reclaim_release(sc.gfp_mask);
Vlastimil Babka499118e2017-05-08 15:59:50 -07003685 memalloc_noreclaim_restore(noreclaim_flag);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07003686
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08003687 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688}
Rafael J. Wysockic6f37f12009-05-24 22:16:31 +02003689#endif /* CONFIG_HIBERNATION */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691/* It's optimal to keep kswapds on the same CPUs as their memory, but
3692 not required for correctness. So if the last cpu in a node goes
3693 away, we get changed to run anywhere: as the first one comes back,
3694 restore their cpu bindings. */
Sebastian Andrzej Siewior517bbed2016-11-03 15:50:03 +01003695static int kswapd_cpu_online(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696{
Yasunori Goto58c0a4a2007-10-16 01:25:40 -07003697 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698
Sebastian Andrzej Siewior517bbed2016-11-03 15:50:03 +01003699 for_each_node_state(nid, N_MEMORY) {
3700 pg_data_t *pgdat = NODE_DATA(nid);
3701 const struct cpumask *mask;
Rusty Russella70f7302009-03-13 14:49:46 +10303702
Sebastian Andrzej Siewior517bbed2016-11-03 15:50:03 +01003703 mask = cpumask_of_node(pgdat->node_id);
Mike Travisc5f59f02008-04-04 18:11:10 -07003704
Sebastian Andrzej Siewior517bbed2016-11-03 15:50:03 +01003705 if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
3706 /* One of our CPUs online: restore mask */
3707 set_cpus_allowed_ptr(pgdat->kswapd, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 }
Sebastian Andrzej Siewior517bbed2016-11-03 15:50:03 +01003709 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711
Yasunori Goto3218ae12006-06-27 02:53:33 -07003712/*
3713 * This kswapd start function will be called by init and node-hot-add.
3714 * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
3715 */
3716int kswapd_run(int nid)
3717{
3718 pg_data_t *pgdat = NODE_DATA(nid);
3719 int ret = 0;
3720
3721 if (pgdat->kswapd)
3722 return 0;
3723
3724 pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
3725 if (IS_ERR(pgdat->kswapd)) {
3726 /* failure at boot is fatal */
Thomas Gleixnerc6202ad2017-05-16 20:42:46 +02003727 BUG_ON(system_state < SYSTEM_RUNNING);
Gavin Shand5dc0ad2012-10-08 16:29:27 -07003728 pr_err("Failed to start kswapd on node %d\n", nid);
3729 ret = PTR_ERR(pgdat->kswapd);
Xishi Qiud72515b2013-04-17 15:58:34 -07003730 pgdat->kswapd = NULL;
Yasunori Goto3218ae12006-06-27 02:53:33 -07003731 }
3732 return ret;
3733}
3734
David Rientjes8fe23e02009-12-14 17:58:33 -08003735/*
Jiang Liud8adde12012-07-11 14:01:52 -07003736 * Called by memory hotplug when all memory in a node is offlined. Caller must
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07003737 * hold mem_hotplug_begin/end().
David Rientjes8fe23e02009-12-14 17:58:33 -08003738 */
3739void kswapd_stop(int nid)
3740{
3741 struct task_struct *kswapd = NODE_DATA(nid)->kswapd;
3742
Jiang Liud8adde12012-07-11 14:01:52 -07003743 if (kswapd) {
David Rientjes8fe23e02009-12-14 17:58:33 -08003744 kthread_stop(kswapd);
Jiang Liud8adde12012-07-11 14:01:52 -07003745 NODE_DATA(nid)->kswapd = NULL;
3746 }
David Rientjes8fe23e02009-12-14 17:58:33 -08003747}
3748
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749static int __init kswapd_init(void)
3750{
Sebastian Andrzej Siewior517bbed2016-11-03 15:50:03 +01003751 int nid, ret;
Andrew Morton69e05942006-03-22 00:08:19 -08003752
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 swap_setup();
Lai Jiangshan48fb2e22012-12-12 13:51:43 -08003754 for_each_node_state(nid, N_MEMORY)
Yasunori Goto3218ae12006-06-27 02:53:33 -07003755 kswapd_run(nid);
Sebastian Andrzej Siewior517bbed2016-11-03 15:50:03 +01003756 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
3757 "mm/vmscan:online", kswapd_cpu_online,
3758 NULL);
3759 WARN_ON(ret < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 return 0;
3761}
3762
3763module_init(kswapd_init)
Christoph Lameter9eeff232006-01-18 17:42:31 -08003764
3765#ifdef CONFIG_NUMA
3766/*
Mel Gormana5f5f912016-07-28 15:46:32 -07003767 * Node reclaim mode
Christoph Lameter9eeff232006-01-18 17:42:31 -08003768 *
Mel Gormana5f5f912016-07-28 15:46:32 -07003769 * If non-zero call node_reclaim when the number of free pages falls below
Christoph Lameter9eeff232006-01-18 17:42:31 -08003770 * the watermarks.
Christoph Lameter9eeff232006-01-18 17:42:31 -08003771 */
Mel Gormana5f5f912016-07-28 15:46:32 -07003772int node_reclaim_mode __read_mostly;
Christoph Lameter9eeff232006-01-18 17:42:31 -08003773
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08003774#define RECLAIM_OFF 0
Fernando Luis Vazquez Cao7d034312008-07-29 22:33:41 -07003775#define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08003776#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
Zhihui Zhang95bbc0c2015-06-24 16:56:42 -07003777#define RECLAIM_UNMAP (1<<2) /* Unmap pages during reclaim */
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08003778
Christoph Lameter9eeff232006-01-18 17:42:31 -08003779/*
Mel Gormana5f5f912016-07-28 15:46:32 -07003780 * Priority for NODE_RECLAIM. This determines the fraction of pages
Christoph Lametera92f7122006-02-01 03:05:32 -08003781 * of a node considered for each zone_reclaim. 4 scans 1/16th of
3782 * a zone.
3783 */
Mel Gormana5f5f912016-07-28 15:46:32 -07003784#define NODE_RECLAIM_PRIORITY 4
Christoph Lametera92f7122006-02-01 03:05:32 -08003785
Christoph Lameter9eeff232006-01-18 17:42:31 -08003786/*
Mel Gormana5f5f912016-07-28 15:46:32 -07003787 * Percentage of pages in a zone that must be unmapped for node_reclaim to
Christoph Lameter96146342006-07-03 00:24:13 -07003788 * occur.
3789 */
3790int sysctl_min_unmapped_ratio = 1;
3791
3792/*
Christoph Lameter0ff38492006-09-25 23:31:52 -07003793 * If the number of slab pages in a zone grows beyond this percentage then
3794 * slab reclaim needs to occur.
3795 */
3796int sysctl_min_slab_ratio = 5;
3797
Mel Gorman11fb9982016-07-28 15:46:20 -07003798static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat)
Mel Gorman90afa5d2009-06-16 15:33:20 -07003799{
Mel Gorman11fb9982016-07-28 15:46:20 -07003800 unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED);
3801 unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) +
3802 node_page_state(pgdat, NR_ACTIVE_FILE);
Mel Gorman90afa5d2009-06-16 15:33:20 -07003803
3804 /*
3805 * It's possible for there to be more file mapped pages than
3806 * accounted for by the pages on the file LRU lists because
3807 * tmpfs pages accounted for as ANON can also be FILE_MAPPED
3808 */
3809 return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0;
3810}
3811
3812/* Work out how many page cache pages we can reclaim in this reclaim_mode */
Mel Gormana5f5f912016-07-28 15:46:32 -07003813static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat)
Mel Gorman90afa5d2009-06-16 15:33:20 -07003814{
Alexandru Moised031a152015-11-05 18:48:08 -08003815 unsigned long nr_pagecache_reclaimable;
3816 unsigned long delta = 0;
Mel Gorman90afa5d2009-06-16 15:33:20 -07003817
3818 /*
Zhihui Zhang95bbc0c2015-06-24 16:56:42 -07003819 * If RECLAIM_UNMAP is set, then all file pages are considered
Mel Gorman90afa5d2009-06-16 15:33:20 -07003820 * potentially reclaimable. Otherwise, we have to worry about
Mel Gorman11fb9982016-07-28 15:46:20 -07003821 * pages like swapcache and node_unmapped_file_pages() provides
Mel Gorman90afa5d2009-06-16 15:33:20 -07003822 * a better estimate
3823 */
Mel Gormana5f5f912016-07-28 15:46:32 -07003824 if (node_reclaim_mode & RECLAIM_UNMAP)
3825 nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES);
Mel Gorman90afa5d2009-06-16 15:33:20 -07003826 else
Mel Gormana5f5f912016-07-28 15:46:32 -07003827 nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat);
Mel Gorman90afa5d2009-06-16 15:33:20 -07003828
3829 /* If we can't clean pages, remove dirty pages from consideration */
Mel Gormana5f5f912016-07-28 15:46:32 -07003830 if (!(node_reclaim_mode & RECLAIM_WRITE))
3831 delta += node_page_state(pgdat, NR_FILE_DIRTY);
Mel Gorman90afa5d2009-06-16 15:33:20 -07003832
3833 /* Watch for any possible underflows due to delta */
3834 if (unlikely(delta > nr_pagecache_reclaimable))
3835 delta = nr_pagecache_reclaimable;
3836
3837 return nr_pagecache_reclaimable - delta;
3838}
3839
Christoph Lameter0ff38492006-09-25 23:31:52 -07003840/*
Mel Gormana5f5f912016-07-28 15:46:32 -07003841 * Try to free up some pages from this node through reclaim.
Christoph Lameter9eeff232006-01-18 17:42:31 -08003842 */
Mel Gormana5f5f912016-07-28 15:46:32 -07003843static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
Christoph Lameter9eeff232006-01-18 17:42:31 -08003844{
Christoph Lameter7fb2d462006-03-22 00:08:22 -08003845 /* Minimum pages needed in order to stay on node */
Andrew Morton69e05942006-03-22 00:08:19 -08003846 const unsigned long nr_pages = 1 << order;
Christoph Lameter9eeff232006-01-18 17:42:31 -08003847 struct task_struct *p = current;
3848 struct reclaim_state reclaim_state;
Vlastimil Babka499118e2017-05-08 15:59:50 -07003849 unsigned int noreclaim_flag;
Andrew Morton179e9632006-03-22 00:08:18 -08003850 struct scan_control sc = {
Andrew Morton62b726c2013-02-22 16:32:24 -08003851 .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
Nick Desaulniersf2f43e52017-07-06 15:36:50 -07003852 .gfp_mask = current_gfp_context(gfp_mask),
Johannes Weinerbd2f6192009-03-31 15:19:38 -07003853 .order = order,
Mel Gormana5f5f912016-07-28 15:46:32 -07003854 .priority = NODE_RECLAIM_PRIORITY,
3855 .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE),
3856 .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP),
Johannes Weineree814fe2014-08-06 16:06:19 -07003857 .may_swap = 1,
Nick Desaulniersf2f43e52017-07-06 15:36:50 -07003858 .reclaim_idx = gfp_zone(gfp_mask),
Andrew Morton179e9632006-03-22 00:08:18 -08003859 };
Christoph Lameter9eeff232006-01-18 17:42:31 -08003860
Christoph Lameter9eeff232006-01-18 17:42:31 -08003861 cond_resched();
Christoph Lameterd4f77962006-02-24 13:04:22 -08003862 /*
Zhihui Zhang95bbc0c2015-06-24 16:56:42 -07003863 * We need to be able to allocate from the reserves for RECLAIM_UNMAP
Christoph Lameterd4f77962006-02-24 13:04:22 -08003864 * and we also need to be able to write out pages for RECLAIM_WRITE
Zhihui Zhang95bbc0c2015-06-24 16:56:42 -07003865 * and RECLAIM_UNMAP.
Christoph Lameterd4f77962006-02-24 13:04:22 -08003866 */
Vlastimil Babka499118e2017-05-08 15:59:50 -07003867 noreclaim_flag = memalloc_noreclaim_save();
3868 p->flags |= PF_SWAPWRITE;
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01003869 fs_reclaim_acquire(sc.gfp_mask);
Christoph Lameter9eeff232006-01-18 17:42:31 -08003870 reclaim_state.reclaimed_slab = 0;
3871 p->reclaim_state = &reclaim_state;
Christoph Lameterc84db232006-02-01 03:05:29 -08003872
Mel Gormana5f5f912016-07-28 15:46:32 -07003873 if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages) {
Christoph Lameter0ff38492006-09-25 23:31:52 -07003874 /*
3875 * Free memory by calling shrink zone with increasing
3876 * priorities until we have enough memory freed.
3877 */
Christoph Lameter0ff38492006-09-25 23:31:52 -07003878 do {
Mel Gorman970a39a2016-07-28 15:46:35 -07003879 shrink_node(pgdat, &sc);
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07003880 } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0);
Christoph Lameter0ff38492006-09-25 23:31:52 -07003881 }
Christoph Lameterc84db232006-02-01 03:05:29 -08003882
Christoph Lameter9eeff232006-01-18 17:42:31 -08003883 p->reclaim_state = NULL;
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01003884 fs_reclaim_release(gfp_mask);
Vlastimil Babka499118e2017-05-08 15:59:50 -07003885 current->flags &= ~PF_SWAPWRITE;
3886 memalloc_noreclaim_restore(noreclaim_flag);
Rik van Riela79311c2009-01-06 14:40:01 -08003887 return sc.nr_reclaimed >= nr_pages;
Christoph Lameter9eeff232006-01-18 17:42:31 -08003888}
Andrew Morton179e9632006-03-22 00:08:18 -08003889
Mel Gormana5f5f912016-07-28 15:46:32 -07003890int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
Andrew Morton179e9632006-03-22 00:08:18 -08003891{
David Rientjesd773ed62007-10-16 23:26:01 -07003892 int ret;
Andrew Morton179e9632006-03-22 00:08:18 -08003893
3894 /*
Mel Gormana5f5f912016-07-28 15:46:32 -07003895 * Node reclaim reclaims unmapped file backed pages and
Christoph Lameter0ff38492006-09-25 23:31:52 -07003896 * slab pages if we are over the defined limits.
Christoph Lameter34aa1332006-06-30 01:55:37 -07003897 *
Christoph Lameter96146342006-07-03 00:24:13 -07003898 * A small portion of unmapped file backed pages is needed for
3899 * file I/O otherwise pages read by file I/O will be immediately
Mel Gormana5f5f912016-07-28 15:46:32 -07003900 * thrown out if the node is overallocated. So we do not reclaim
3901 * if less than a specified percentage of the node is used by
Christoph Lameter96146342006-07-03 00:24:13 -07003902 * unmapped file backed pages.
Andrew Morton179e9632006-03-22 00:08:18 -08003903 */
Mel Gormana5f5f912016-07-28 15:46:32 -07003904 if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages &&
Johannes Weiner385386c2017-07-06 15:40:43 -07003905 node_page_state(pgdat, NR_SLAB_RECLAIMABLE) <= pgdat->min_slab_pages)
Mel Gormana5f5f912016-07-28 15:46:32 -07003906 return NODE_RECLAIM_FULL;
Andrew Morton179e9632006-03-22 00:08:18 -08003907
3908 /*
David Rientjesd773ed62007-10-16 23:26:01 -07003909 * Do not scan if the allocation should not be delayed.
Andrew Morton179e9632006-03-22 00:08:18 -08003910 */
Mel Gormand0164ad2015-11-06 16:28:21 -08003911 if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC))
Mel Gormana5f5f912016-07-28 15:46:32 -07003912 return NODE_RECLAIM_NOSCAN;
Andrew Morton179e9632006-03-22 00:08:18 -08003913
3914 /*
Mel Gormana5f5f912016-07-28 15:46:32 -07003915 * Only run node reclaim on the local node or on nodes that do not
Andrew Morton179e9632006-03-22 00:08:18 -08003916 * have associated processors. This will favor the local processor
3917 * over remote processors and spread off node memory allocations
3918 * as wide as possible.
3919 */
Mel Gormana5f5f912016-07-28 15:46:32 -07003920 if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id())
3921 return NODE_RECLAIM_NOSCAN;
David Rientjesd773ed62007-10-16 23:26:01 -07003922
Mel Gormana5f5f912016-07-28 15:46:32 -07003923 if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags))
3924 return NODE_RECLAIM_NOSCAN;
Mel Gormanfa5e0842009-06-16 15:33:22 -07003925
Mel Gormana5f5f912016-07-28 15:46:32 -07003926 ret = __node_reclaim(pgdat, gfp_mask, order);
3927 clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags);
David Rientjesd773ed62007-10-16 23:26:01 -07003928
Mel Gorman24cf725182009-06-16 15:33:23 -07003929 if (!ret)
3930 count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED);
3931
David Rientjesd773ed62007-10-16 23:26:01 -07003932 return ret;
Andrew Morton179e9632006-03-22 00:08:18 -08003933}
Christoph Lameter9eeff232006-01-18 17:42:31 -08003934#endif
Lee Schermerhorn894bc312008-10-18 20:26:39 -07003935
Lee Schermerhorn894bc312008-10-18 20:26:39 -07003936/*
3937 * page_evictable - test whether a page is evictable
3938 * @page: the page to test
Lee Schermerhorn894bc312008-10-18 20:26:39 -07003939 *
3940 * Test whether page is evictable--i.e., should be placed on active/inactive
Hugh Dickins39b5f292012-10-08 16:33:18 -07003941 * lists vs unevictable list.
Lee Schermerhorn894bc312008-10-18 20:26:39 -07003942 *
3943 * Reasons page might not be evictable:
Lee Schermerhornba9ddf42008-10-18 20:26:42 -07003944 * (1) page's mapping marked unevictable
Nick Pigginb291f002008-10-18 20:26:44 -07003945 * (2) page is part of an mlocked VMA
Lee Schermerhornba9ddf42008-10-18 20:26:42 -07003946 *
Lee Schermerhorn894bc312008-10-18 20:26:39 -07003947 */
Hugh Dickins39b5f292012-10-08 16:33:18 -07003948int page_evictable(struct page *page)
Lee Schermerhorn894bc312008-10-18 20:26:39 -07003949{
Hugh Dickins39b5f292012-10-08 16:33:18 -07003950 return !mapping_unevictable(page_mapping(page)) && !PageMlocked(page);
Lee Schermerhorn894bc312008-10-18 20:26:39 -07003951}
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003952
Hugh Dickins850465792012-01-20 14:34:19 -08003953#ifdef CONFIG_SHMEM
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003954/**
Hugh Dickins24513262012-01-20 14:34:21 -08003955 * check_move_unevictable_pages - check pages for evictability and move to appropriate zone lru list
3956 * @pages: array of pages to check
3957 * @nr_pages: number of pages to check
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003958 *
Hugh Dickins24513262012-01-20 14:34:21 -08003959 * Checks pages for evictability and moves them to the appropriate lru list.
Hugh Dickins850465792012-01-20 14:34:19 -08003960 *
3961 * This function is only used for SysV IPC SHM_UNLOCK.
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003962 */
Hugh Dickins24513262012-01-20 14:34:21 -08003963void check_move_unevictable_pages(struct page **pages, int nr_pages)
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003964{
Johannes Weiner925b7672012-01-12 17:18:15 -08003965 struct lruvec *lruvec;
Mel Gorman785b99f2016-07-28 15:47:23 -07003966 struct pglist_data *pgdat = NULL;
Hugh Dickins24513262012-01-20 14:34:21 -08003967 int pgscanned = 0;
3968 int pgrescued = 0;
3969 int i;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003970
Hugh Dickins24513262012-01-20 14:34:21 -08003971 for (i = 0; i < nr_pages; i++) {
3972 struct page *page = pages[i];
Mel Gorman785b99f2016-07-28 15:47:23 -07003973 struct pglist_data *pagepgdat = page_pgdat(page);
Lee Schermerhornaf936a12008-10-18 20:26:53 -07003974
Hugh Dickins24513262012-01-20 14:34:21 -08003975 pgscanned++;
Mel Gorman785b99f2016-07-28 15:47:23 -07003976 if (pagepgdat != pgdat) {
3977 if (pgdat)
3978 spin_unlock_irq(&pgdat->lru_lock);
3979 pgdat = pagepgdat;
3980 spin_lock_irq(&pgdat->lru_lock);
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003981 }
Mel Gorman785b99f2016-07-28 15:47:23 -07003982 lruvec = mem_cgroup_page_lruvec(page, pgdat);
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003983
Hugh Dickins24513262012-01-20 14:34:21 -08003984 if (!PageLRU(page) || !PageUnevictable(page))
3985 continue;
3986
Hugh Dickins39b5f292012-10-08 16:33:18 -07003987 if (page_evictable(page)) {
Hugh Dickins24513262012-01-20 14:34:21 -08003988 enum lru_list lru = page_lru_base_type(page);
3989
Sasha Levin309381fea2014-01-23 15:52:54 -08003990 VM_BUG_ON_PAGE(PageActive(page), page);
Hugh Dickins24513262012-01-20 14:34:21 -08003991 ClearPageUnevictable(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07003992 del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE);
3993 add_page_to_lru_list(page, lruvec, lru);
Hugh Dickins24513262012-01-20 14:34:21 -08003994 pgrescued++;
3995 }
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003996 }
Hugh Dickins24513262012-01-20 14:34:21 -08003997
Mel Gorman785b99f2016-07-28 15:47:23 -07003998 if (pgdat) {
Hugh Dickins24513262012-01-20 14:34:21 -08003999 __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued);
4000 __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned);
Mel Gorman785b99f2016-07-28 15:47:23 -07004001 spin_unlock_irq(&pgdat->lru_lock);
Hugh Dickins24513262012-01-20 14:34:21 -08004002 }
Hugh Dickins850465792012-01-20 14:34:19 -08004003}
4004#endif /* CONFIG_SHMEM */