blob: 153e0795f4f006d21bfba500b916c72ca4164a25 [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{
Tetsuo Handabb422a72017-12-18 20:31:41 +0900300 if (!shrinker->nr_deferred)
301 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 down_write(&shrinker_rwsem);
303 list_del(&shrinker->list);
304 up_write(&shrinker_rwsem);
Andrew Vaginae393322013-10-16 13:46:46 -0700305 kfree(shrinker->nr_deferred);
Tetsuo Handabb422a72017-12-18 20:31:41 +0900306 shrinker->nr_deferred = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
Rusty Russell8e1f9362007-07-17 04:03:17 -0700308EXPORT_SYMBOL(unregister_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310#define SHRINK_BATCH 128
Glauber Costa1d3d4432013-08-28 10:18:04 +1000311
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800312static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,
Josef Bacik9092c712018-01-31 16:16:26 -0800313 struct shrinker *shrinker, int priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Glauber Costa1d3d4432013-08-28 10:18:04 +1000315 unsigned long freed = 0;
316 unsigned long long delta;
317 long total_scan;
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700318 long freeable;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000319 long nr;
320 long new_nr;
321 int nid = shrinkctl->nid;
322 long batch_size = shrinker->batch ? shrinker->batch
323 : SHRINK_BATCH;
Shaohua Li5f33a082016-12-12 16:41:50 -0800324 long scanned = 0, next_deferred;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000325
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700326 freeable = shrinker->count_objects(shrinker, shrinkctl);
327 if (freeable == 0)
Glauber Costa1d3d4432013-08-28 10:18:04 +1000328 return 0;
329
330 /*
331 * copy the current shrinker scan count into a local variable
332 * and zero it so that other concurrent shrinker invocations
333 * don't also do this scanning work.
334 */
335 nr = atomic_long_xchg(&shrinker->nr_deferred[nid], 0);
336
337 total_scan = nr;
Josef Bacik9092c712018-01-31 16:16:26 -0800338 delta = freeable >> priority;
339 delta *= 4;
340 do_div(delta, shrinker->seeks);
Glauber Costa1d3d4432013-08-28 10:18:04 +1000341 total_scan += delta;
342 if (total_scan < 0) {
Pintu Kumar8612c662014-12-10 15:42:58 -0800343 pr_err("shrink_slab: %pF negative objects to delete nr=%ld\n",
Dave Chinnera0b02132013-08-28 10:18:16 +1000344 shrinker->scan_objects, total_scan);
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700345 total_scan = freeable;
Shaohua Li5f33a082016-12-12 16:41:50 -0800346 next_deferred = nr;
347 } else
348 next_deferred = total_scan;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000349
350 /*
351 * We need to avoid excessive windup on filesystem shrinkers
352 * due to large numbers of GFP_NOFS allocations causing the
353 * shrinkers to return -1 all the time. This results in a large
354 * nr being built up so when a shrink that can do some work
355 * comes along it empties the entire cache due to nr >>>
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700356 * freeable. This is bad for sustaining a working set in
Glauber Costa1d3d4432013-08-28 10:18:04 +1000357 * memory.
358 *
359 * Hence only allow the shrinker to scan the entire cache when
360 * a large delta change is calculated directly.
361 */
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700362 if (delta < freeable / 4)
363 total_scan = min(total_scan, freeable / 2);
Glauber Costa1d3d4432013-08-28 10:18:04 +1000364
365 /*
366 * Avoid risking looping forever due to too large nr value:
367 * never try to free more than twice the estimate number of
368 * freeable entries.
369 */
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700370 if (total_scan > freeable * 2)
371 total_scan = freeable * 2;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000372
373 trace_mm_shrink_slab_start(shrinker, shrinkctl, nr,
Josef Bacik9092c712018-01-31 16:16:26 -0800374 freeable, delta, total_scan, priority);
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
Josef Bacik9092c712018-01-31 16:16:26 -0800434 * @priority: the reclaim priority
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 *
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800436 * Call the shrink functions to age shrinkable caches.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 *
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800438 * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set,
439 * unaware shrinkers will receive a node id of 0 instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 *
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800441 * @memcg specifies the memory cgroup to target. If it is not NULL,
442 * only shrinkers with SHRINKER_MEMCG_AWARE set will be called to scan
Vladimir Davydov0fc9f582016-03-17 14:18:30 -0700443 * objects from the memory cgroup specified. Otherwise, only unaware
444 * shrinkers are called.
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800445 *
Josef Bacik9092c712018-01-31 16:16:26 -0800446 * @priority is sc->priority, we take the number of objects and >> by priority
447 * in order to get the scan target.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 *
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800449 * Returns the number of reclaimed slab objects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 */
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800451static unsigned long shrink_slab(gfp_t gfp_mask, int nid,
452 struct mem_cgroup *memcg,
Josef Bacik9092c712018-01-31 16:16:26 -0800453 int priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 struct shrinker *shrinker;
Dave Chinner24f7c6b2013-08-28 10:17:56 +1000456 unsigned long freed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Vladimir Davydov0fc9f582016-03-17 14:18:30 -0700458 if (memcg && (!memcg_kmem_enabled() || !mem_cgroup_online(memcg)))
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800459 return 0;
460
Minchan Kimf06590b2011-05-24 17:11:11 -0700461 if (!down_read_trylock(&shrinker_rwsem)) {
Dave Chinner24f7c6b2013-08-28 10:17:56 +1000462 /*
463 * If we would return 0, our callers would understand that we
464 * have nothing else to shrink and give up trying. By returning
465 * 1 we keep it going and assume we'll be able to shrink next
466 * time.
467 */
468 freed = 1;
Minchan Kimf06590b2011-05-24 17:11:11 -0700469 goto out;
470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 list_for_each_entry(shrinker, &shrinker_list, list) {
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800473 struct shrink_control sc = {
474 .gfp_mask = gfp_mask,
475 .nid = nid,
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800476 .memcg = memcg,
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800477 };
Vladimir Davydovec970972014-01-23 15:53:23 -0800478
Vladimir Davydov0fc9f582016-03-17 14:18:30 -0700479 /*
480 * If kernel memory accounting is disabled, we ignore
481 * SHRINKER_MEMCG_AWARE flag and call all shrinkers
482 * passing NULL for memcg.
483 */
484 if (memcg_kmem_enabled() &&
485 !!memcg != !!(shrinker->flags & SHRINKER_MEMCG_AWARE))
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800486 continue;
487
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800488 if (!(shrinker->flags & SHRINKER_NUMA_AWARE))
489 sc.nid = 0;
Dave Chinneracf92b42011-07-08 14:14:35 +1000490
Josef Bacik9092c712018-01-31 16:16:26 -0800491 freed += do_shrink_slab(&sc, shrinker, priority);
Minchan Kime4966122018-01-31 16:16:55 -0800492 /*
493 * Bail out if someone want to register a new shrinker to
494 * prevent the regsitration from being stalled for long periods
495 * by parallel ongoing shrinking.
496 */
497 if (rwsem_is_contended(&shrinker_rwsem)) {
498 freed = freed ? : 1;
499 break;
500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 up_read(&shrinker_rwsem);
Minchan Kimf06590b2011-05-24 17:11:11 -0700504out:
505 cond_resched();
Dave Chinner24f7c6b2013-08-28 10:17:56 +1000506 return freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800509void drop_slab_node(int nid)
510{
511 unsigned long freed;
512
513 do {
514 struct mem_cgroup *memcg = NULL;
515
516 freed = 0;
517 do {
Josef Bacik9092c712018-01-31 16:16:26 -0800518 freed += shrink_slab(GFP_KERNEL, nid, memcg, 0);
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800519 } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL);
520 } while (freed > 10);
521}
522
523void drop_slab(void)
524{
525 int nid;
526
527 for_each_online_node(nid)
528 drop_slab_node(nid);
529}
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531static inline int is_page_cache_freeable(struct page *page)
532{
Johannes Weinerceddc3a2009-09-21 17:03:00 -0700533 /*
534 * A freeable page cache page is referenced only by the caller
535 * that isolated the page, the page cache radix tree and
536 * optional buffer heads at page->private.
537 */
Huang Yingbd4c82c22017-09-06 16:22:49 -0700538 int radix_pins = PageTransHuge(page) && PageSwapCache(page) ?
539 HPAGE_PMD_NR : 1;
540 return page_count(page) - page_has_private(page) == 1 + radix_pins;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
Tejun Heo703c2702015-05-22 17:13:44 -0400543static int may_write_to_inode(struct inode *inode, struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Christoph Lameter930d9152006-01-08 01:00:47 -0800545 if (current->flags & PF_SWAPWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return 1;
Tejun Heo703c2702015-05-22 17:13:44 -0400547 if (!inode_write_congested(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return 1;
Tejun Heo703c2702015-05-22 17:13:44 -0400549 if (inode_to_bdi(inode) == current->backing_dev_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return 1;
551 return 0;
552}
553
554/*
555 * We detected a synchronous write error writing a page out. Probably
556 * -ENOSPC. We need to propagate that into the address_space for a subsequent
557 * fsync(), msync() or close().
558 *
559 * The tricky part is that after writepage we cannot touch the mapping: nothing
560 * prevents it from being freed up. But we have a ref on the page and once
561 * that page is locked, the mapping is pinned.
562 *
563 * We're allowed to run sleeping lock_page() here because we know the caller has
564 * __GFP_FS.
565 */
566static void handle_write_error(struct address_space *mapping,
567 struct page *page, int error)
568{
Jens Axboe7eaceac2011-03-10 08:52:07 +0100569 lock_page(page);
Guillaume Chazarain3e9f45b2007-05-08 00:23:25 -0700570 if (page_mapping(page) == mapping)
571 mapping_set_error(mapping, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 unlock_page(page);
573}
574
Christoph Lameter04e62a22006-06-23 02:03:38 -0700575/* possible outcome of pageout() */
576typedef enum {
577 /* failed to write page out, page is locked */
578 PAGE_KEEP,
579 /* move page to the active list, page is locked */
580 PAGE_ACTIVATE,
581 /* page has been sent to the disk successfully, page is unlocked */
582 PAGE_SUCCESS,
583 /* page is clean and locked */
584 PAGE_CLEAN,
585} pageout_t;
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587/*
Andrew Morton1742f192006-03-22 00:08:21 -0800588 * pageout is called by shrink_page_list() for each dirty page.
589 * Calls ->writepage().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 */
Andy Whitcroftc661b072007-08-22 14:01:26 -0700591static pageout_t pageout(struct page *page, struct address_space *mapping,
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -0700592 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 /*
595 * If the page is dirty, only perform writeback if that write
596 * will be non-blocking. To prevent this allocation from being
597 * stalled by pagecache activity. But note that there may be
598 * stalls if we need to run get_block(). We could test
599 * PagePrivate for that.
600 *
Al Viro81742022014-04-03 03:17:43 -0400601 * If this process is currently in __generic_file_write_iter() against
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 * this page's queue, we can perform writeback even if that
603 * will block.
604 *
605 * If the page is swapcache, write it back even if that would
606 * block, for some throttling. This happens by accident, because
607 * swap_backing_dev_info is bust: it doesn't reflect the
608 * congestion state of the swapdevs. Easy to fix, if needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 */
610 if (!is_page_cache_freeable(page))
611 return PAGE_KEEP;
612 if (!mapping) {
613 /*
614 * Some data journaling orphaned pages can have
615 * page->mapping == NULL while being dirty with clean buffers.
616 */
David Howells266cf652009-04-03 16:42:36 +0100617 if (page_has_private(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (try_to_free_buffers(page)) {
619 ClearPageDirty(page);
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -0700620 pr_info("%s: orphaned page\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return PAGE_CLEAN;
622 }
623 }
624 return PAGE_KEEP;
625 }
626 if (mapping->a_ops->writepage == NULL)
627 return PAGE_ACTIVATE;
Tejun Heo703c2702015-05-22 17:13:44 -0400628 if (!may_write_to_inode(mapping->host, sc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return PAGE_KEEP;
630
631 if (clear_page_dirty_for_io(page)) {
632 int res;
633 struct writeback_control wbc = {
634 .sync_mode = WB_SYNC_NONE,
635 .nr_to_write = SWAP_CLUSTER_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700636 .range_start = 0,
637 .range_end = LLONG_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 .for_reclaim = 1,
639 };
640
641 SetPageReclaim(page);
642 res = mapping->a_ops->writepage(page, &wbc);
643 if (res < 0)
644 handle_write_error(mapping, page, res);
Zach Brown994fc28c2005-12-15 14:28:17 -0800645 if (res == AOP_WRITEPAGE_ACTIVATE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 ClearPageReclaim(page);
647 return PAGE_ACTIVATE;
648 }
Andy Whitcroftc661b072007-08-22 14:01:26 -0700649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (!PageWriteback(page)) {
651 /* synchronous write or broken a_ops? */
652 ClearPageReclaim(page);
653 }
yalin wang3aa23852016-01-14 15:18:30 -0800654 trace_mm_vmscan_writepage(page);
Mel Gormanc4a25632016-07-28 15:46:23 -0700655 inc_node_page_state(page, NR_VMSCAN_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return PAGE_SUCCESS;
657 }
658
659 return PAGE_CLEAN;
660}
661
Andrew Mortona649fd92006-10-17 00:09:36 -0700662/*
Nick Piggine2867812008-07-25 19:45:30 -0700663 * Same as remove_mapping, but if the page is removed from the mapping, it
664 * gets returned with a refcount of 0.
Andrew Mortona649fd92006-10-17 00:09:36 -0700665 */
Johannes Weinera5289102014-04-03 14:47:51 -0700666static int __remove_mapping(struct address_space *mapping, struct page *page,
667 bool reclaimed)
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800668{
Greg Thelenc4843a72015-05-22 17:13:16 -0400669 unsigned long flags;
Huang Yingbd4c82c22017-09-06 16:22:49 -0700670 int refcount;
Greg Thelenc4843a72015-05-22 17:13:16 -0400671
Nick Piggin28e4d962006-09-25 23:31:23 -0700672 BUG_ON(!PageLocked(page));
673 BUG_ON(mapping != page_mapping(page));
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800674
Greg Thelenc4843a72015-05-22 17:13:16 -0400675 spin_lock_irqsave(&mapping->tree_lock, flags);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800676 /*
Nick Piggin0fd0e6b2006-09-27 01:50:02 -0700677 * The non racy check for a busy page.
678 *
679 * Must be careful with the order of the tests. When someone has
680 * a ref to the page, it may be possible that they dirty it then
681 * drop the reference. So if PageDirty is tested before page_count
682 * here, then the following race may occur:
683 *
684 * get_user_pages(&page);
685 * [user mapping goes away]
686 * write_to(page);
687 * !PageDirty(page) [good]
688 * SetPageDirty(page);
689 * put_page(page);
690 * !page_count(page) [good, discard it]
691 *
692 * [oops, our write_to data is lost]
693 *
694 * Reversing the order of the tests ensures such a situation cannot
695 * escape unnoticed. The smp_rmb is needed to ensure the page->flags
Joonsoo Kim0139aa72016-05-19 17:10:49 -0700696 * load is not satisfied before that of page->_refcount.
Nick Piggin0fd0e6b2006-09-27 01:50:02 -0700697 *
698 * Note that if SetPageDirty is always performed via set_page_dirty,
699 * and thus under tree_lock, then this ordering is not required.
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800700 */
Huang Yingbd4c82c22017-09-06 16:22:49 -0700701 if (unlikely(PageTransHuge(page)) && PageSwapCache(page))
702 refcount = 1 + HPAGE_PMD_NR;
703 else
704 refcount = 2;
705 if (!page_ref_freeze(page, refcount))
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800706 goto cannot_free;
Nick Piggine2867812008-07-25 19:45:30 -0700707 /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */
708 if (unlikely(PageDirty(page))) {
Huang Yingbd4c82c22017-09-06 16:22:49 -0700709 page_ref_unfreeze(page, refcount);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800710 goto cannot_free;
Nick Piggine2867812008-07-25 19:45:30 -0700711 }
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800712
713 if (PageSwapCache(page)) {
714 swp_entry_t swap = { .val = page_private(page) };
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700715 mem_cgroup_swapout(page, swap);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800716 __delete_from_swap_cache(page);
Greg Thelenc4843a72015-05-22 17:13:16 -0400717 spin_unlock_irqrestore(&mapping->tree_lock, flags);
Minchan Kim75f6d6d2017-07-06 15:37:21 -0700718 put_swap_page(page, swap);
Nick Piggine2867812008-07-25 19:45:30 -0700719 } else {
Linus Torvalds6072d132010-12-01 13:35:19 -0500720 void (*freepage)(struct page *);
Johannes Weinera5289102014-04-03 14:47:51 -0700721 void *shadow = NULL;
Linus Torvalds6072d132010-12-01 13:35:19 -0500722
723 freepage = mapping->a_ops->freepage;
Johannes Weinera5289102014-04-03 14:47:51 -0700724 /*
725 * Remember a shadow entry for reclaimed file cache in
726 * order to detect refaults, thus thrashing, later on.
727 *
728 * But don't store shadows in an address space that is
729 * already exiting. This is not just an optizimation,
730 * inode reclaim needs to empty out the radix tree or
731 * the nodes are lost. Don't plant shadows behind its
732 * back.
Ross Zwislerf9fe48b2016-01-22 15:10:40 -0800733 *
734 * We also don't store shadows for DAX mappings because the
735 * only page cache pages found in these are zero pages
736 * covering holes, and because we don't want to mix DAX
737 * exceptional entries and shadow exceptional entries in the
738 * same page_tree.
Johannes Weinera5289102014-04-03 14:47:51 -0700739 */
740 if (reclaimed && page_is_file_cache(page) &&
Ross Zwislerf9fe48b2016-01-22 15:10:40 -0800741 !mapping_exiting(mapping) && !dax_mapping(mapping))
Johannes Weinera5289102014-04-03 14:47:51 -0700742 shadow = workingset_eviction(mapping, page);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700743 __delete_from_page_cache(page, shadow);
Greg Thelenc4843a72015-05-22 17:13:16 -0400744 spin_unlock_irqrestore(&mapping->tree_lock, flags);
Linus Torvalds6072d132010-12-01 13:35:19 -0500745
746 if (freepage != NULL)
747 freepage(page);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800748 }
749
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800750 return 1;
751
752cannot_free:
Greg Thelenc4843a72015-05-22 17:13:16 -0400753 spin_unlock_irqrestore(&mapping->tree_lock, flags);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800754 return 0;
755}
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757/*
Nick Piggine2867812008-07-25 19:45:30 -0700758 * Attempt to detach a locked page from its ->mapping. If it is dirty or if
759 * someone else has a ref on the page, abort and return 0. If it was
760 * successfully detached, return 1. Assumes the caller has a single ref on
761 * this page.
762 */
763int remove_mapping(struct address_space *mapping, struct page *page)
764{
Johannes Weinera5289102014-04-03 14:47:51 -0700765 if (__remove_mapping(mapping, page, false)) {
Nick Piggine2867812008-07-25 19:45:30 -0700766 /*
767 * Unfreezing the refcount with 1 rather than 2 effectively
768 * drops the pagecache ref for us without requiring another
769 * atomic operation.
770 */
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700771 page_ref_unfreeze(page, 1);
Nick Piggine2867812008-07-25 19:45:30 -0700772 return 1;
773 }
774 return 0;
775}
776
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700777/**
778 * putback_lru_page - put previously isolated page onto appropriate LRU list
779 * @page: page to be put back to appropriate lru list
780 *
781 * Add previously isolated @page to appropriate LRU list.
782 * Page may still be unevictable for other reasons.
783 *
784 * lru_lock must not be held, interrupts must be enabled.
785 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700786void putback_lru_page(struct page *page)
787{
Vlastimil Babka0ec3b742013-09-11 14:22:26 -0700788 bool is_unevictable;
Lee Schermerhornbbfd28e2008-10-18 20:26:40 -0700789 int was_unevictable = PageUnevictable(page);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700790
Sasha Levin309381fea2014-01-23 15:52:54 -0800791 VM_BUG_ON_PAGE(PageLRU(page), page);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700792
793redo:
794 ClearPageUnevictable(page);
795
Hugh Dickins39b5f292012-10-08 16:33:18 -0700796 if (page_evictable(page)) {
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700797 /*
798 * For evictable pages, we can use the cache.
799 * In event of a race, worst case is we end up with an
800 * unevictable page on [in]active list.
801 * We know how to handle that.
802 */
Vlastimil Babka0ec3b742013-09-11 14:22:26 -0700803 is_unevictable = false;
Mel Gormanc53954a2013-07-03 15:02:34 -0700804 lru_cache_add(page);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700805 } else {
806 /*
807 * Put unevictable pages directly on zone's unevictable
808 * list.
809 */
Vlastimil Babka0ec3b742013-09-11 14:22:26 -0700810 is_unevictable = true;
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700811 add_page_to_unevictable_list(page);
Johannes Weiner6a7b9542009-10-26 16:50:00 -0700812 /*
Minchan Kim21ee9f32011-10-31 17:09:28 -0700813 * When racing with an mlock or AS_UNEVICTABLE clearing
814 * (page is unlocked) make sure that if the other thread
815 * does not observe our setting of PG_lru and fails
Hugh Dickins24513262012-01-20 14:34:21 -0800816 * isolation/check_move_unevictable_pages,
Minchan Kim21ee9f32011-10-31 17:09:28 -0700817 * we see PG_mlocked/AS_UNEVICTABLE cleared below and move
Johannes Weiner6a7b9542009-10-26 16:50:00 -0700818 * the page back to the evictable list.
819 *
Minchan Kim21ee9f32011-10-31 17:09:28 -0700820 * The other side is TestClearPageMlocked() or shmem_lock().
Johannes Weiner6a7b9542009-10-26 16:50:00 -0700821 */
822 smp_mb();
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700823 }
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700824
825 /*
826 * page's status can change while we move it among lru. If an evictable
827 * page is on unevictable list, it never be freed. To avoid that,
828 * check after we added it to the list, again.
829 */
Vlastimil Babka0ec3b742013-09-11 14:22:26 -0700830 if (is_unevictable && page_evictable(page)) {
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700831 if (!isolate_lru_page(page)) {
832 put_page(page);
833 goto redo;
834 }
835 /* This means someone else dropped this page from LRU
836 * So, it will be freed or putback to LRU again. There is
837 * nothing to do here.
838 */
839 }
840
Vlastimil Babka0ec3b742013-09-11 14:22:26 -0700841 if (was_unevictable && !is_unevictable)
Lee Schermerhornbbfd28e2008-10-18 20:26:40 -0700842 count_vm_event(UNEVICTABLE_PGRESCUED);
Vlastimil Babka0ec3b742013-09-11 14:22:26 -0700843 else if (!was_unevictable && is_unevictable)
Lee Schermerhornbbfd28e2008-10-18 20:26:40 -0700844 count_vm_event(UNEVICTABLE_PGCULLED);
845
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700846 put_page(page); /* drop ref from isolate */
847}
848
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800849enum page_references {
850 PAGEREF_RECLAIM,
851 PAGEREF_RECLAIM_CLEAN,
Johannes Weiner645747462010-03-05 13:42:22 -0800852 PAGEREF_KEEP,
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800853 PAGEREF_ACTIVATE,
854};
855
856static enum page_references page_check_references(struct page *page,
857 struct scan_control *sc)
858{
Johannes Weiner645747462010-03-05 13:42:22 -0800859 int referenced_ptes, referenced_page;
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800860 unsigned long vm_flags;
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800861
Johannes Weinerc3ac9a82012-05-29 15:06:25 -0700862 referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup,
863 &vm_flags);
Johannes Weiner645747462010-03-05 13:42:22 -0800864 referenced_page = TestClearPageReferenced(page);
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800865
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800866 /*
867 * Mlock lost the isolation race with us. Let try_to_unmap()
868 * move the page to the unevictable list.
869 */
870 if (vm_flags & VM_LOCKED)
871 return PAGEREF_RECLAIM;
872
Johannes Weiner645747462010-03-05 13:42:22 -0800873 if (referenced_ptes) {
Michal Hockoe48982732012-05-29 15:06:45 -0700874 if (PageSwapBacked(page))
Johannes Weiner645747462010-03-05 13:42:22 -0800875 return PAGEREF_ACTIVATE;
876 /*
877 * All mapped pages start out with page table
878 * references from the instantiating fault, so we need
879 * to look twice if a mapped file page is used more
880 * than once.
881 *
882 * Mark it and spare it for another trip around the
883 * inactive list. Another page table reference will
884 * lead to its activation.
885 *
886 * Note: the mark is set for activated pages as well
887 * so that recently deactivated but used pages are
888 * quickly recovered.
889 */
890 SetPageReferenced(page);
891
Konstantin Khlebnikov34dbc672012-01-10 15:06:59 -0800892 if (referenced_page || referenced_ptes > 1)
Johannes Weiner645747462010-03-05 13:42:22 -0800893 return PAGEREF_ACTIVATE;
894
Konstantin Khlebnikovc909e992012-01-10 15:07:03 -0800895 /*
896 * Activate file-backed executable pages after first usage.
897 */
898 if (vm_flags & VM_EXEC)
899 return PAGEREF_ACTIVATE;
900
Johannes Weiner645747462010-03-05 13:42:22 -0800901 return PAGEREF_KEEP;
902 }
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800903
904 /* Reclaim if clean, defer dirty pages to writeback */
KOSAKI Motohiro2e302442010-10-26 14:21:46 -0700905 if (referenced_page && !PageSwapBacked(page))
Johannes Weiner645747462010-03-05 13:42:22 -0800906 return PAGEREF_RECLAIM_CLEAN;
907
908 return PAGEREF_RECLAIM;
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800909}
910
Mel Gormane2be15f2013-07-03 15:01:57 -0700911/* Check if a page is dirty or under writeback */
912static void page_check_dirty_writeback(struct page *page,
913 bool *dirty, bool *writeback)
914{
Mel Gormanb4597222013-07-03 15:02:05 -0700915 struct address_space *mapping;
916
Mel Gormane2be15f2013-07-03 15:01:57 -0700917 /*
918 * Anonymous pages are not handled by flushers and must be written
919 * from reclaim context. Do not stall reclaim based on them
920 */
Shaohua Li802a3a92017-05-03 14:52:32 -0700921 if (!page_is_file_cache(page) ||
922 (PageAnon(page) && !PageSwapBacked(page))) {
Mel Gormane2be15f2013-07-03 15:01:57 -0700923 *dirty = false;
924 *writeback = false;
925 return;
926 }
927
928 /* By default assume that the page flags are accurate */
929 *dirty = PageDirty(page);
930 *writeback = PageWriteback(page);
Mel Gormanb4597222013-07-03 15:02:05 -0700931
932 /* Verify dirty/writeback state if the filesystem supports it */
933 if (!page_has_private(page))
934 return;
935
936 mapping = page_mapping(page);
937 if (mapping && mapping->a_ops->is_dirty_writeback)
938 mapping->a_ops->is_dirty_writeback(page, dirty, writeback);
Mel Gormane2be15f2013-07-03 15:01:57 -0700939}
940
Michal Hocko3c710c12017-02-22 15:44:27 -0800941struct reclaim_stat {
942 unsigned nr_dirty;
943 unsigned nr_unqueued_dirty;
944 unsigned nr_congested;
945 unsigned nr_writeback;
946 unsigned nr_immediate;
Michal Hocko5bccd162017-02-22 15:44:30 -0800947 unsigned nr_activate;
948 unsigned nr_ref_keep;
949 unsigned nr_unmap_fail;
Michal Hocko3c710c12017-02-22 15:44:27 -0800950};
951
Nick Piggine2867812008-07-25 19:45:30 -0700952/*
Andrew Morton1742f192006-03-22 00:08:21 -0800953 * shrink_page_list() returns the number of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 */
Andrew Morton1742f192006-03-22 00:08:21 -0800955static unsigned long shrink_page_list(struct list_head *page_list,
Mel Gorman599d0c92016-07-28 15:45:31 -0700956 struct pglist_data *pgdat,
Mel Gormanf84f6e22011-10-31 17:07:51 -0700957 struct scan_control *sc,
Minchan Kim02c6de82012-10-08 16:31:55 -0700958 enum ttu_flags ttu_flags,
Michal Hocko3c710c12017-02-22 15:44:27 -0800959 struct reclaim_stat *stat,
Minchan Kim02c6de82012-10-08 16:31:55 -0700960 bool force_reclaim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
962 LIST_HEAD(ret_pages);
Mel Gormanabe4c3b2010-08-09 17:19:31 -0700963 LIST_HEAD(free_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 int pgactivate = 0;
Michal Hocko3c710c12017-02-22 15:44:27 -0800965 unsigned nr_unqueued_dirty = 0;
966 unsigned nr_dirty = 0;
967 unsigned nr_congested = 0;
968 unsigned nr_reclaimed = 0;
969 unsigned nr_writeback = 0;
970 unsigned nr_immediate = 0;
Michal Hocko5bccd162017-02-22 15:44:30 -0800971 unsigned nr_ref_keep = 0;
972 unsigned nr_unmap_fail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 cond_resched();
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 while (!list_empty(page_list)) {
977 struct address_space *mapping;
978 struct page *page;
979 int may_enter_fs;
Minchan Kim02c6de82012-10-08 16:31:55 -0700980 enum page_references references = PAGEREF_RECLAIM_CLEAN;
Mel Gormane2be15f2013-07-03 15:01:57 -0700981 bool dirty, writeback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 cond_resched();
984
985 page = lru_to_page(page_list);
986 list_del(&page->lru);
987
Nick Piggin529ae9a2008-08-02 12:01:03 +0200988 if (!trylock_page(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 goto keep;
990
Sasha Levin309381fea2014-01-23 15:52:54 -0800991 VM_BUG_ON_PAGE(PageActive(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 sc->nr_scanned++;
Christoph Lameter80e43422006-02-11 17:55:53 -0800994
Hugh Dickins39b5f292012-10-08 16:33:18 -0700995 if (unlikely(!page_evictable(page)))
Minchan Kimad6b6702017-05-03 14:54:13 -0700996 goto activate_locked;
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700997
Johannes Weinera6dc60f82009-03-31 15:19:30 -0700998 if (!sc->may_unmap && page_mapped(page))
Christoph Lameter80e43422006-02-11 17:55:53 -0800999 goto keep_locked;
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 /* Double the slab pressure for mapped and swapcache pages */
Shaohua Li802a3a92017-05-03 14:52:32 -07001002 if ((page_mapped(page) || PageSwapCache(page)) &&
1003 !(PageAnon(page) && !PageSwapBacked(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 sc->nr_scanned++;
1005
Andy Whitcroftc661b072007-08-22 14:01:26 -07001006 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
1007 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
1008
Mel Gorman283aba92013-07-03 15:01:51 -07001009 /*
Mel Gormane2be15f2013-07-03 15:01:57 -07001010 * The number of dirty pages determines if a zone is marked
1011 * reclaim_congested which affects wait_iff_congested. kswapd
1012 * will stall and start writing pages if the tail of the LRU
1013 * is all dirty unqueued pages.
1014 */
1015 page_check_dirty_writeback(page, &dirty, &writeback);
1016 if (dirty || writeback)
1017 nr_dirty++;
1018
1019 if (dirty && !writeback)
1020 nr_unqueued_dirty++;
1021
Mel Gormand04e8ac2013-07-03 15:02:03 -07001022 /*
1023 * Treat this page as congested if the underlying BDI is or if
1024 * pages are cycling through the LRU so quickly that the
1025 * pages marked for immediate reclaim are making it to the
1026 * end of the LRU a second time.
1027 */
Mel Gormane2be15f2013-07-03 15:01:57 -07001028 mapping = page_mapping(page);
Jamie Liu1da58ee2014-12-10 15:43:20 -08001029 if (((dirty || writeback) && mapping &&
Tejun Heo703c2702015-05-22 17:13:44 -04001030 inode_write_congested(mapping->host)) ||
Mel Gormand04e8ac2013-07-03 15:02:03 -07001031 (writeback && PageReclaim(page)))
Mel Gormane2be15f2013-07-03 15:01:57 -07001032 nr_congested++;
1033
1034 /*
Mel Gorman283aba92013-07-03 15:01:51 -07001035 * If a page at the tail of the LRU is under writeback, there
1036 * are three cases to consider.
1037 *
1038 * 1) If reclaim is encountering an excessive number of pages
1039 * under writeback and this page is both under writeback and
1040 * PageReclaim then it indicates that pages are being queued
1041 * for IO but are being recycled through the LRU before the
1042 * IO can complete. Waiting on the page itself risks an
1043 * indefinite stall if it is impossible to writeback the
1044 * page due to IO error or disconnected storage so instead
Mel Gormanb1a6f212013-07-03 15:01:58 -07001045 * note that the LRU is being scanned too quickly and the
1046 * caller can stall after page list has been processed.
Mel Gorman283aba92013-07-03 15:01:51 -07001047 *
Tejun Heo97c93412015-05-22 18:23:36 -04001048 * 2) Global or new memcg reclaim encounters a page that is
Michal Hockoecf5fc62015-08-04 14:36:58 -07001049 * not marked for immediate reclaim, or the caller does not
1050 * have __GFP_FS (or __GFP_IO if it's simply going to swap,
1051 * not to fs). In this case mark the page for immediate
Tejun Heo97c93412015-05-22 18:23:36 -04001052 * reclaim and continue scanning.
Mel Gorman283aba92013-07-03 15:01:51 -07001053 *
Michal Hockoecf5fc62015-08-04 14:36:58 -07001054 * Require may_enter_fs because we would wait on fs, which
1055 * may not have submitted IO yet. And the loop driver might
Mel Gorman283aba92013-07-03 15:01:51 -07001056 * enter reclaim, and deadlock if it waits on a page for
1057 * which it is needed to do the write (loop masks off
1058 * __GFP_IO|__GFP_FS for this reason); but more thought
1059 * would probably show more reasons.
1060 *
Hugh Dickins7fadc822015-09-08 15:03:46 -07001061 * 3) Legacy memcg encounters a page that is already marked
Mel Gorman283aba92013-07-03 15:01:51 -07001062 * PageReclaim. memcg does not have any dirty pages
1063 * throttling so we could easily OOM just because too many
1064 * pages are in writeback and there is nothing else to
1065 * reclaim. Wait for the writeback to complete.
Johannes Weinerc55e8d02017-02-24 14:56:23 -08001066 *
1067 * In cases 1) and 2) we activate the pages to get them out of
1068 * the way while we continue scanning for clean pages on the
1069 * inactive list and refilling from the active list. The
1070 * observation here is that waiting for disk writes is more
1071 * expensive than potentially causing reloads down the line.
1072 * Since they're marked for immediate reclaim, they won't put
1073 * memory pressure on the cache working set any longer than it
1074 * takes to write them to disk.
Mel Gorman283aba92013-07-03 15:01:51 -07001075 */
Andy Whitcroftc661b072007-08-22 14:01:26 -07001076 if (PageWriteback(page)) {
Mel Gorman283aba92013-07-03 15:01:51 -07001077 /* Case 1 above */
1078 if (current_is_kswapd() &&
1079 PageReclaim(page) &&
Mel Gorman599d0c92016-07-28 15:45:31 -07001080 test_bit(PGDAT_WRITEBACK, &pgdat->flags)) {
Mel Gormanb1a6f212013-07-03 15:01:58 -07001081 nr_immediate++;
Johannes Weinerc55e8d02017-02-24 14:56:23 -08001082 goto activate_locked;
Mel Gorman283aba92013-07-03 15:01:51 -07001083
1084 /* Case 2 above */
Tejun Heo97c93412015-05-22 18:23:36 -04001085 } else if (sane_reclaim(sc) ||
Michal Hockoecf5fc62015-08-04 14:36:58 -07001086 !PageReclaim(page) || !may_enter_fs) {
Hugh Dickinsc3b94f42012-07-31 16:45:59 -07001087 /*
1088 * This is slightly racy - end_page_writeback()
1089 * might have just cleared PageReclaim, then
1090 * setting PageReclaim here end up interpreted
1091 * as PageReadahead - but that does not matter
1092 * enough to care. What we do want is for this
1093 * page to have PageReclaim set next time memcg
1094 * reclaim reaches the tests above, so it will
1095 * then wait_on_page_writeback() to avoid OOM;
1096 * and it's also appropriate in global reclaim.
1097 */
1098 SetPageReclaim(page);
Michal Hockoe62e3842012-07-31 16:45:55 -07001099 nr_writeback++;
Johannes Weinerc55e8d02017-02-24 14:56:23 -08001100 goto activate_locked;
Mel Gorman283aba92013-07-03 15:01:51 -07001101
1102 /* Case 3 above */
1103 } else {
Hugh Dickins7fadc822015-09-08 15:03:46 -07001104 unlock_page(page);
Mel Gorman283aba92013-07-03 15:01:51 -07001105 wait_on_page_writeback(page);
Hugh Dickins7fadc822015-09-08 15:03:46 -07001106 /* then go back and try same page again */
1107 list_add_tail(&page->lru, page_list);
1108 continue;
Michal Hockoe62e3842012-07-31 16:45:55 -07001109 }
Andy Whitcroftc661b072007-08-22 14:01:26 -07001110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Minchan Kim02c6de82012-10-08 16:31:55 -07001112 if (!force_reclaim)
1113 references = page_check_references(page, sc);
1114
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001115 switch (references) {
1116 case PAGEREF_ACTIVATE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 goto activate_locked;
Johannes Weiner645747462010-03-05 13:42:22 -08001118 case PAGEREF_KEEP:
Michal Hocko5bccd162017-02-22 15:44:30 -08001119 nr_ref_keep++;
Johannes Weiner645747462010-03-05 13:42:22 -08001120 goto keep_locked;
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001121 case PAGEREF_RECLAIM:
1122 case PAGEREF_RECLAIM_CLEAN:
1123 ; /* try to reclaim the page below */
1124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 /*
1127 * Anonymous process memory has backing store?
1128 * Try to allocate it some swap space here.
Shaohua Li802a3a92017-05-03 14:52:32 -07001129 * Lazyfree page could be freed directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 */
Huang Yingbd4c82c22017-09-06 16:22:49 -07001131 if (PageAnon(page) && PageSwapBacked(page)) {
1132 if (!PageSwapCache(page)) {
1133 if (!(sc->gfp_mask & __GFP_IO))
1134 goto keep_locked;
1135 if (PageTransHuge(page)) {
1136 /* cannot split THP, skip it */
1137 if (!can_split_huge_page(page, NULL))
1138 goto activate_locked;
1139 /*
1140 * Split pages without a PMD map right
1141 * away. Chances are some or all of the
1142 * tail pages can be freed without IO.
1143 */
1144 if (!compound_mapcount(page) &&
1145 split_huge_page_to_list(page,
1146 page_list))
1147 goto activate_locked;
1148 }
1149 if (!add_to_swap(page)) {
1150 if (!PageTransHuge(page))
1151 goto activate_locked;
1152 /* Fallback to swap normal pages */
1153 if (split_huge_page_to_list(page,
1154 page_list))
1155 goto activate_locked;
Huang Yingfe490cc2017-09-06 16:22:52 -07001156#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1157 count_vm_event(THP_SWPOUT_FALLBACK);
1158#endif
Huang Yingbd4c82c22017-09-06 16:22:49 -07001159 if (!add_to_swap(page))
1160 goto activate_locked;
1161 }
Minchan Kim0f074652017-07-06 15:37:24 -07001162
Huang Yingbd4c82c22017-09-06 16:22:49 -07001163 may_enter_fs = 1;
1164
1165 /* Adding to swap updated mapping */
1166 mapping = page_mapping(page);
Minchan Kim0f074652017-07-06 15:37:24 -07001167 }
Kirill A. Shutemov7751b2d2016-07-26 15:25:56 -07001168 } else if (unlikely(PageTransHuge(page))) {
1169 /* Split file THP */
1170 if (split_huge_page_to_list(page, page_list))
1171 goto keep_locked;
Mel Gormane2be15f2013-07-03 15:01:57 -07001172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 /*
1175 * The page is mapped into the page tables of one or more
1176 * processes. Try to unmap it here.
1177 */
Shaohua Li802a3a92017-05-03 14:52:32 -07001178 if (page_mapped(page)) {
Huang Yingbd4c82c22017-09-06 16:22:49 -07001179 enum ttu_flags flags = ttu_flags | TTU_BATCH_FLUSH;
1180
1181 if (unlikely(PageTransHuge(page)))
1182 flags |= TTU_SPLIT_HUGE_PMD;
1183 if (!try_to_unmap(page, flags)) {
Michal Hocko5bccd162017-02-22 15:44:30 -08001184 nr_unmap_fail++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 goto activate_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
1187 }
1188
1189 if (PageDirty(page)) {
Mel Gormanee728862011-10-31 17:07:38 -07001190 /*
Johannes Weiner4eda4822017-02-24 14:56:20 -08001191 * Only kswapd can writeback filesystem pages
1192 * to avoid risk of stack overflow. But avoid
1193 * injecting inefficient single-page IO into
1194 * flusher writeback as much as possible: only
1195 * write pages when we've encountered many
1196 * dirty pages, and when we've already scanned
1197 * the rest of the LRU for clean pages and see
1198 * the same dirty pages again (PageReclaim).
Mel Gormanee728862011-10-31 17:07:38 -07001199 */
Mel Gormanf84f6e22011-10-31 17:07:51 -07001200 if (page_is_file_cache(page) &&
Johannes Weiner4eda4822017-02-24 14:56:20 -08001201 (!current_is_kswapd() || !PageReclaim(page) ||
1202 !test_bit(PGDAT_DIRTY, &pgdat->flags))) {
Mel Gorman49ea7eb2011-10-31 17:07:59 -07001203 /*
1204 * Immediately reclaim when written back.
1205 * Similar in principal to deactivate_page()
1206 * except we already have the page isolated
1207 * and know it's dirty
1208 */
Mel Gormanc4a25632016-07-28 15:46:23 -07001209 inc_node_page_state(page, NR_VMSCAN_IMMEDIATE);
Mel Gorman49ea7eb2011-10-31 17:07:59 -07001210 SetPageReclaim(page);
1211
Johannes Weinerc55e8d02017-02-24 14:56:23 -08001212 goto activate_locked;
Mel Gormanee728862011-10-31 17:07:38 -07001213 }
1214
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001215 if (references == PAGEREF_RECLAIM_CLEAN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 goto keep_locked;
Andrew Morton4dd4b922008-03-24 12:29:52 -07001217 if (!may_enter_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 goto keep_locked;
Christoph Lameter52a83632006-02-01 03:05:28 -08001219 if (!sc->may_writepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 goto keep_locked;
1221
Mel Gormand950c942015-09-04 15:47:35 -07001222 /*
1223 * Page is dirty. Flush the TLB if a writable entry
1224 * potentially exists to avoid CPU writes after IO
1225 * starts and then write it out here.
1226 */
1227 try_to_unmap_flush_dirty();
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -07001228 switch (pageout(page, mapping, sc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 case PAGE_KEEP:
1230 goto keep_locked;
1231 case PAGE_ACTIVATE:
1232 goto activate_locked;
1233 case PAGE_SUCCESS:
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -07001234 if (PageWriteback(page))
Mel Gorman41ac1992012-05-29 15:06:19 -07001235 goto keep;
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -07001236 if (PageDirty(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 goto keep;
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -07001238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 /*
1240 * A synchronous write - probably a ramdisk. Go
1241 * ahead and try to reclaim the page.
1242 */
Nick Piggin529ae9a2008-08-02 12:01:03 +02001243 if (!trylock_page(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 goto keep;
1245 if (PageDirty(page) || PageWriteback(page))
1246 goto keep_locked;
1247 mapping = page_mapping(page);
1248 case PAGE_CLEAN:
1249 ; /* try to free the page below */
1250 }
1251 }
1252
1253 /*
1254 * If the page has buffers, try to free the buffer mappings
1255 * associated with this page. If we succeed we try to free
1256 * the page as well.
1257 *
1258 * We do this even if the page is PageDirty().
1259 * try_to_release_page() does not perform I/O, but it is
1260 * possible for a page to have PageDirty set, but it is actually
1261 * clean (all its buffers are clean). This happens if the
1262 * buffers were written out directly, with submit_bh(). ext3
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001263 * will do this, as well as the blockdev mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 * try_to_release_page() will discover that cleanness and will
1265 * drop the buffers and mark the page clean - it can be freed.
1266 *
1267 * Rarely, pages can have buffers and no ->mapping. These are
1268 * the pages which were not successfully invalidated in
1269 * truncate_complete_page(). We try to drop those buffers here
1270 * and if that worked, and the page is no longer mapped into
1271 * process address space (page_count == 1) it can be freed.
1272 * Otherwise, leave the page on the LRU so it is swappable.
1273 */
David Howells266cf652009-04-03 16:42:36 +01001274 if (page_has_private(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (!try_to_release_page(page, sc->gfp_mask))
1276 goto activate_locked;
Nick Piggine2867812008-07-25 19:45:30 -07001277 if (!mapping && page_count(page) == 1) {
1278 unlock_page(page);
1279 if (put_page_testzero(page))
1280 goto free_it;
1281 else {
1282 /*
1283 * rare race with speculative reference.
1284 * the speculative reference will free
1285 * this page shortly, so we may
1286 * increment nr_reclaimed here (and
1287 * leave it off the LRU).
1288 */
1289 nr_reclaimed++;
1290 continue;
1291 }
1292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294
Shaohua Li802a3a92017-05-03 14:52:32 -07001295 if (PageAnon(page) && !PageSwapBacked(page)) {
1296 /* follow __remove_mapping for reference */
1297 if (!page_ref_freeze(page, 1))
1298 goto keep_locked;
1299 if (PageDirty(page)) {
1300 page_ref_unfreeze(page, 1);
1301 goto keep_locked;
1302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Shaohua Li802a3a92017-05-03 14:52:32 -07001304 count_vm_event(PGLAZYFREED);
Roman Gushchin22621852017-07-06 15:40:25 -07001305 count_memcg_page_event(page, PGLAZYFREED);
Shaohua Li802a3a92017-05-03 14:52:32 -07001306 } else if (!mapping || !__remove_mapping(mapping, page, true))
1307 goto keep_locked;
Nick Piggina978d6f2008-10-18 20:26:58 -07001308 /*
1309 * At this point, we have no other references and there is
1310 * no way to pick any more up (removed from LRU, removed
1311 * from pagecache). Can use non-atomic bitops now (and
1312 * we obviously don't have to worry about waking up a process
1313 * waiting on the page lock, because there are no references.
1314 */
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -08001315 __ClearPageLocked(page);
Nick Piggine2867812008-07-25 19:45:30 -07001316free_it:
Andrew Morton05ff5132006-03-22 00:08:20 -08001317 nr_reclaimed++;
Mel Gormanabe4c3b2010-08-09 17:19:31 -07001318
1319 /*
1320 * Is there need to periodically free_page_list? It would
1321 * appear not as the counts should be low
1322 */
Huang Yingbd4c82c22017-09-06 16:22:49 -07001323 if (unlikely(PageTransHuge(page))) {
1324 mem_cgroup_uncharge(page);
1325 (*get_compound_page_dtor(page))(page);
1326 } else
1327 list_add(&page->lru, &free_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 continue;
1329
1330activate_locked:
Rik van Riel68a223942008-10-18 20:26:23 -07001331 /* Not a candidate for swapping, so reclaim swap space. */
Minchan Kimad6b6702017-05-03 14:54:13 -07001332 if (PageSwapCache(page) && (mem_cgroup_swap_full(page) ||
1333 PageMlocked(page)))
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -08001334 try_to_free_swap(page);
Sasha Levin309381fea2014-01-23 15:52:54 -08001335 VM_BUG_ON_PAGE(PageActive(page), page);
Minchan Kimad6b6702017-05-03 14:54:13 -07001336 if (!PageMlocked(page)) {
1337 SetPageActive(page);
1338 pgactivate++;
Roman Gushchin22621852017-07-06 15:40:25 -07001339 count_memcg_page_event(page, PGACTIVATE);
Minchan Kimad6b6702017-05-03 14:54:13 -07001340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341keep_locked:
1342 unlock_page(page);
1343keep:
1344 list_add(&page->lru, &ret_pages);
Sasha Levin309381fea2014-01-23 15:52:54 -08001345 VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
Mel Gormanabe4c3b2010-08-09 17:19:31 -07001347
Johannes Weiner747db952014-08-08 14:19:24 -07001348 mem_cgroup_uncharge_list(&free_pages);
Mel Gorman72b252a2015-09-04 15:47:32 -07001349 try_to_unmap_flush();
Mel Gorman2d4894b2017-11-15 17:37:59 -08001350 free_unref_page_list(&free_pages);
Mel Gormanabe4c3b2010-08-09 17:19:31 -07001351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 list_splice(&ret_pages, page_list);
Christoph Lameterf8891e52006-06-30 01:55:45 -07001353 count_vm_events(PGACTIVATE, pgactivate);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07001354
Michal Hocko3c710c12017-02-22 15:44:27 -08001355 if (stat) {
1356 stat->nr_dirty = nr_dirty;
1357 stat->nr_congested = nr_congested;
1358 stat->nr_unqueued_dirty = nr_unqueued_dirty;
1359 stat->nr_writeback = nr_writeback;
1360 stat->nr_immediate = nr_immediate;
Michal Hocko5bccd162017-02-22 15:44:30 -08001361 stat->nr_activate = pgactivate;
1362 stat->nr_ref_keep = nr_ref_keep;
1363 stat->nr_unmap_fail = nr_unmap_fail;
Michal Hocko3c710c12017-02-22 15:44:27 -08001364 }
Andrew Morton05ff5132006-03-22 00:08:20 -08001365 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366}
1367
Minchan Kim02c6de82012-10-08 16:31:55 -07001368unsigned long reclaim_clean_pages_from_list(struct zone *zone,
1369 struct list_head *page_list)
1370{
1371 struct scan_control sc = {
1372 .gfp_mask = GFP_KERNEL,
1373 .priority = DEF_PRIORITY,
1374 .may_unmap = 1,
1375 };
Michal Hocko3c710c12017-02-22 15:44:27 -08001376 unsigned long ret;
Minchan Kim02c6de82012-10-08 16:31:55 -07001377 struct page *page, *next;
1378 LIST_HEAD(clean_pages);
1379
1380 list_for_each_entry_safe(page, next, page_list, lru) {
Rafael Aquini117aad12013-09-30 13:45:16 -07001381 if (page_is_file_cache(page) && !PageDirty(page) &&
Minchan Kimb1123ea62016-07-26 15:23:09 -07001382 !__PageMovable(page)) {
Minchan Kim02c6de82012-10-08 16:31:55 -07001383 ClearPageActive(page);
1384 list_move(&page->lru, &clean_pages);
1385 }
1386 }
1387
Mel Gorman599d0c92016-07-28 15:45:31 -07001388 ret = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc,
Shaohua Lia128ca72017-05-03 14:52:22 -07001389 TTU_IGNORE_ACCESS, NULL, true);
Minchan Kim02c6de82012-10-08 16:31:55 -07001390 list_splice(&clean_pages, page_list);
Mel Gorman599d0c92016-07-28 15:45:31 -07001391 mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, -ret);
Minchan Kim02c6de82012-10-08 16:31:55 -07001392 return ret;
1393}
1394
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001395/*
1396 * Attempt to remove the specified page from its LRU. Only take this page
1397 * if it is of the appropriate PageActive status. Pages which are being
1398 * freed elsewhere are also ignored.
1399 *
1400 * page: page to consider
1401 * mode: one of the LRU isolation modes defined above
1402 *
1403 * returns 0 on success, -ve errno on failure.
1404 */
Konstantin Khlebnikovf3fd4a62012-05-29 15:06:54 -07001405int __isolate_lru_page(struct page *page, isolate_mode_t mode)
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001406{
1407 int ret = -EINVAL;
1408
1409 /* Only take pages on the LRU. */
1410 if (!PageLRU(page))
1411 return ret;
1412
Minchan Kime46a2872012-10-08 16:33:48 -07001413 /* Compaction should not handle unevictable pages but CMA can do so */
1414 if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE))
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001415 return ret;
1416
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001417 ret = -EBUSY;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001418
Mel Gormanc8244932012-01-12 17:19:38 -08001419 /*
1420 * To minimise LRU disruption, the caller can indicate that it only
1421 * wants to isolate pages it will be able to operate on without
1422 * blocking - clean pages for the most part.
1423 *
Mel Gormanc8244932012-01-12 17:19:38 -08001424 * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages
1425 * that it is possible to migrate without blocking
1426 */
Johannes Weiner1276ad62017-02-24 14:56:11 -08001427 if (mode & ISOLATE_ASYNC_MIGRATE) {
Mel Gormanc8244932012-01-12 17:19:38 -08001428 /* All the caller can do on PageWriteback is block */
1429 if (PageWriteback(page))
1430 return ret;
1431
1432 if (PageDirty(page)) {
1433 struct address_space *mapping;
1434
Mel Gormanc8244932012-01-12 17:19:38 -08001435 /*
1436 * Only pages without mappings or that have a
1437 * ->migratepage callback are possible to migrate
1438 * without blocking
1439 */
1440 mapping = page_mapping(page);
1441 if (mapping && !mapping->a_ops->migratepage)
1442 return ret;
1443 }
1444 }
Minchan Kim39deaf82011-10-31 17:06:51 -07001445
Minchan Kimf80c0672011-10-31 17:06:55 -07001446 if ((mode & ISOLATE_UNMAPPED) && page_mapped(page))
1447 return ret;
1448
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001449 if (likely(get_page_unless_zero(page))) {
1450 /*
1451 * Be careful not to clear PageLRU until after we're
1452 * sure the page is not being freed elsewhere -- the
1453 * page release code relies on it.
1454 */
1455 ClearPageLRU(page);
1456 ret = 0;
1457 }
1458
1459 return ret;
1460}
1461
Mel Gorman7ee36a12016-07-28 15:47:17 -07001462
1463/*
1464 * Update LRU sizes after isolating pages. The LRU size updates must
1465 * be complete before mem_cgroup_update_lru_size due to a santity check.
1466 */
1467static __always_inline void update_lru_sizes(struct lruvec *lruvec,
Michal Hockob4536f0c82017-01-10 16:58:04 -08001468 enum lru_list lru, unsigned long *nr_zone_taken)
Mel Gorman7ee36a12016-07-28 15:47:17 -07001469{
Mel Gorman7ee36a12016-07-28 15:47:17 -07001470 int zid;
1471
Mel Gorman7ee36a12016-07-28 15:47:17 -07001472 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
1473 if (!nr_zone_taken[zid])
1474 continue;
1475
1476 __update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]);
Michal Hockob4536f0c82017-01-10 16:58:04 -08001477#ifdef CONFIG_MEMCG
1478 mem_cgroup_update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]);
1479#endif
Mel Gorman7ee36a12016-07-28 15:47:17 -07001480 }
Mel Gorman7ee36a12016-07-28 15:47:17 -07001481
Mel Gorman7ee36a12016-07-28 15:47:17 -07001482}
1483
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001484/*
Mel Gormana52633d2016-07-28 15:45:28 -07001485 * zone_lru_lock is heavily contended. Some of the functions that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 * shrink the lists perform better by taking out a batch of pages
1487 * and working on them outside the LRU lock.
1488 *
1489 * For pagecache intensive workloads, this function is the hottest
1490 * spot in the kernel (apart from copy_*_user functions).
1491 *
1492 * Appropriate locks must be held before calling this function.
1493 *
Minchan Kim791b48b2017-05-12 15:47:06 -07001494 * @nr_to_scan: The number of eligible pages to look through on the list.
Konstantin Khlebnikov5dc35972012-05-29 15:06:58 -07001495 * @lruvec: The LRU vector to pull pages from.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 * @dst: The temp list to put pages on to.
Hugh Dickinsf6260122012-01-12 17:20:06 -08001497 * @nr_scanned: The number of pages that were scanned.
Rik van Rielfe2c2a12012-03-21 16:33:51 -07001498 * @sc: The scan_control struct for this reclaim session
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001499 * @mode: One of the LRU isolation modes
Konstantin Khlebnikov3cb99452012-05-29 15:06:53 -07001500 * @lru: LRU list id for isolating
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 *
1502 * returns how many pages were moved onto *@dst.
1503 */
Andrew Morton69e05942006-03-22 00:08:19 -08001504static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
Konstantin Khlebnikov5dc35972012-05-29 15:06:58 -07001505 struct lruvec *lruvec, struct list_head *dst,
Rik van Rielfe2c2a12012-03-21 16:33:51 -07001506 unsigned long *nr_scanned, struct scan_control *sc,
Konstantin Khlebnikov3cb99452012-05-29 15:06:53 -07001507 isolate_mode_t mode, enum lru_list lru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Hugh Dickins75b00af2012-05-29 15:07:09 -07001509 struct list_head *src = &lruvec->lists[lru];
Andrew Morton69e05942006-03-22 00:08:19 -08001510 unsigned long nr_taken = 0;
Mel Gorman599d0c92016-07-28 15:45:31 -07001511 unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 };
Mel Gorman7cc30fc2016-07-28 15:46:59 -07001512 unsigned long nr_skipped[MAX_NR_ZONES] = { 0, };
Johannes Weiner3db65812017-05-03 14:52:13 -07001513 unsigned long skipped = 0;
Minchan Kim791b48b2017-05-12 15:47:06 -07001514 unsigned long scan, total_scan, nr_pages;
Mel Gormanb2e18752016-07-28 15:45:37 -07001515 LIST_HEAD(pages_skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Minchan Kim791b48b2017-05-12 15:47:06 -07001517 scan = 0;
1518 for (total_scan = 0;
1519 scan < nr_to_scan && nr_taken < nr_to_scan && !list_empty(src);
1520 total_scan++) {
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001521 struct page *page;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 page = lru_to_page(src);
1524 prefetchw_prev_lru_page(page, src, flags);
1525
Sasha Levin309381fea2014-01-23 15:52:54 -08001526 VM_BUG_ON_PAGE(!PageLRU(page), page);
Nick Piggin8d438f92006-03-22 00:07:59 -08001527
Mel Gormanb2e18752016-07-28 15:45:37 -07001528 if (page_zonenum(page) > sc->reclaim_idx) {
1529 list_move(&page->lru, &pages_skipped);
Mel Gorman7cc30fc2016-07-28 15:46:59 -07001530 nr_skipped[page_zonenum(page)]++;
Mel Gormanb2e18752016-07-28 15:45:37 -07001531 continue;
1532 }
1533
Minchan Kim791b48b2017-05-12 15:47:06 -07001534 /*
1535 * Do not count skipped pages because that makes the function
1536 * return with no isolated pages if the LRU mostly contains
1537 * ineligible pages. This causes the VM to not reclaim any
1538 * pages, triggering a premature OOM.
1539 */
1540 scan++;
Konstantin Khlebnikovf3fd4a62012-05-29 15:06:54 -07001541 switch (__isolate_lru_page(page, mode)) {
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001542 case 0:
Mel Gorman599d0c92016-07-28 15:45:31 -07001543 nr_pages = hpage_nr_pages(page);
1544 nr_taken += nr_pages;
1545 nr_zone_taken[page_zonenum(page)] += nr_pages;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001546 list_move(&page->lru, dst);
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001547 break;
Nick Piggin46453a62006-03-22 00:07:58 -08001548
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001549 case -EBUSY:
1550 /* else it is being freed elsewhere */
1551 list_move(&page->lru, src);
1552 continue;
1553
1554 default:
1555 BUG();
1556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
1558
Mel Gormanb2e18752016-07-28 15:45:37 -07001559 /*
1560 * Splice any skipped pages to the start of the LRU list. Note that
1561 * this disrupts the LRU order when reclaiming for lower zones but
1562 * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX
1563 * scanning would soon rescan the same pages to skip and put the
1564 * system at risk of premature OOM.
1565 */
Mel Gorman7cc30fc2016-07-28 15:46:59 -07001566 if (!list_empty(&pages_skipped)) {
1567 int zid;
1568
Johannes Weiner3db65812017-05-03 14:52:13 -07001569 list_splice(&pages_skipped, src);
Mel Gorman7cc30fc2016-07-28 15:46:59 -07001570 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
1571 if (!nr_skipped[zid])
1572 continue;
1573
1574 __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]);
Michal Hocko1265e3a2017-02-22 15:44:21 -08001575 skipped += nr_skipped[zid];
Mel Gorman7cc30fc2016-07-28 15:46:59 -07001576 }
1577 }
Minchan Kim791b48b2017-05-12 15:47:06 -07001578 *nr_scanned = total_scan;
Michal Hocko1265e3a2017-02-22 15:44:21 -08001579 trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan,
Minchan Kim791b48b2017-05-12 15:47:06 -07001580 total_scan, skipped, nr_taken, mode, lru);
Michal Hockob4536f0c82017-01-10 16:58:04 -08001581 update_lru_sizes(lruvec, lru, nr_zone_taken);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 return nr_taken;
1583}
1584
Nick Piggin62695a82008-10-18 20:26:09 -07001585/**
1586 * isolate_lru_page - tries to isolate a page from its LRU list
1587 * @page: page to isolate from its LRU list
1588 *
1589 * Isolates a @page from an LRU list, clears PageLRU and adjusts the
1590 * vmstat statistic corresponding to whatever LRU list the page was on.
1591 *
1592 * Returns 0 if the page was removed from an LRU list.
1593 * Returns -EBUSY if the page was not on an LRU list.
1594 *
1595 * The returned page will have PageLRU() cleared. If it was found on
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001596 * the active list, it will have PageActive set. If it was found on
1597 * the unevictable list, it will have the PageUnevictable bit set. That flag
1598 * may need to be cleared by the caller before letting the page go.
Nick Piggin62695a82008-10-18 20:26:09 -07001599 *
1600 * The vmstat statistic corresponding to the list on which the page was
1601 * found will be decremented.
1602 *
1603 * Restrictions:
1604 * (1) Must be called with an elevated refcount on the page. This is a
1605 * fundamentnal difference from isolate_lru_pages (which is called
1606 * without a stable reference).
1607 * (2) the lru_lock must not be held.
1608 * (3) interrupts must be enabled.
1609 */
1610int isolate_lru_page(struct page *page)
1611{
1612 int ret = -EBUSY;
1613
Sasha Levin309381fea2014-01-23 15:52:54 -08001614 VM_BUG_ON_PAGE(!page_count(page), page);
Kirill A. Shutemovcf2a82e2016-02-05 15:36:36 -08001615 WARN_RATELIMIT(PageTail(page), "trying to isolate tail page");
Konstantin Khlebnikov0c917312011-05-24 17:12:21 -07001616
Nick Piggin62695a82008-10-18 20:26:09 -07001617 if (PageLRU(page)) {
1618 struct zone *zone = page_zone(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001619 struct lruvec *lruvec;
Nick Piggin62695a82008-10-18 20:26:09 -07001620
Mel Gormana52633d2016-07-28 15:45:28 -07001621 spin_lock_irq(zone_lru_lock(zone));
Mel Gorman599d0c92016-07-28 15:45:31 -07001622 lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat);
Konstantin Khlebnikov0c917312011-05-24 17:12:21 -07001623 if (PageLRU(page)) {
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001624 int lru = page_lru(page);
Konstantin Khlebnikov0c917312011-05-24 17:12:21 -07001625 get_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -07001626 ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001627 del_page_from_lru_list(page, lruvec, lru);
1628 ret = 0;
Nick Piggin62695a82008-10-18 20:26:09 -07001629 }
Mel Gormana52633d2016-07-28 15:45:28 -07001630 spin_unlock_irq(zone_lru_lock(zone));
Nick Piggin62695a82008-10-18 20:26:09 -07001631 }
1632 return ret;
1633}
1634
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001635/*
Fengguang Wud37dd5d2012-12-18 14:23:28 -08001636 * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and
1637 * then get resheduled. When there are massive number of tasks doing page
1638 * allocation, such sleeping direct reclaimers may keep piling up on each CPU,
1639 * the LRU list will go small and be scanned faster than necessary, leading to
1640 * unnecessary swapping, thrashing and OOM.
Rik van Riel35cd7812009-09-21 17:01:38 -07001641 */
Mel Gorman599d0c92016-07-28 15:45:31 -07001642static int too_many_isolated(struct pglist_data *pgdat, int file,
Rik van Riel35cd7812009-09-21 17:01:38 -07001643 struct scan_control *sc)
1644{
1645 unsigned long inactive, isolated;
1646
1647 if (current_is_kswapd())
1648 return 0;
1649
Tejun Heo97c93412015-05-22 18:23:36 -04001650 if (!sane_reclaim(sc))
Rik van Riel35cd7812009-09-21 17:01:38 -07001651 return 0;
1652
1653 if (file) {
Mel Gorman599d0c92016-07-28 15:45:31 -07001654 inactive = node_page_state(pgdat, NR_INACTIVE_FILE);
1655 isolated = node_page_state(pgdat, NR_ISOLATED_FILE);
Rik van Riel35cd7812009-09-21 17:01:38 -07001656 } else {
Mel Gorman599d0c92016-07-28 15:45:31 -07001657 inactive = node_page_state(pgdat, NR_INACTIVE_ANON);
1658 isolated = node_page_state(pgdat, NR_ISOLATED_ANON);
Rik van Riel35cd7812009-09-21 17:01:38 -07001659 }
1660
Fengguang Wu3cf23842012-12-18 14:23:31 -08001661 /*
1662 * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they
1663 * won't get blocked by normal direct-reclaimers, forming a circular
1664 * deadlock.
1665 */
Mel Gormand0164ad2015-11-06 16:28:21 -08001666 if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS))
Fengguang Wu3cf23842012-12-18 14:23:31 -08001667 inactive >>= 3;
1668
Rik van Riel35cd7812009-09-21 17:01:38 -07001669 return isolated > inactive;
1670}
1671
Mel Gorman66635622010-08-09 17:19:30 -07001672static noinline_for_stack void
Hugh Dickins75b00af2012-05-29 15:07:09 -07001673putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list)
Mel Gorman66635622010-08-09 17:19:30 -07001674{
Konstantin Khlebnikov27ac81d2012-05-29 15:07:00 -07001675 struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
Mel Gorman599d0c92016-07-28 15:45:31 -07001676 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
Hugh Dickins3f797682012-01-12 17:20:07 -08001677 LIST_HEAD(pages_to_free);
Mel Gorman66635622010-08-09 17:19:30 -07001678
Mel Gorman66635622010-08-09 17:19:30 -07001679 /*
1680 * Put back any unfreeable pages.
1681 */
Mel Gorman66635622010-08-09 17:19:30 -07001682 while (!list_empty(page_list)) {
Hugh Dickins3f797682012-01-12 17:20:07 -08001683 struct page *page = lru_to_page(page_list);
Mel Gorman66635622010-08-09 17:19:30 -07001684 int lru;
Hugh Dickins3f797682012-01-12 17:20:07 -08001685
Sasha Levin309381fea2014-01-23 15:52:54 -08001686 VM_BUG_ON_PAGE(PageLRU(page), page);
Mel Gorman66635622010-08-09 17:19:30 -07001687 list_del(&page->lru);
Hugh Dickins39b5f292012-10-08 16:33:18 -07001688 if (unlikely(!page_evictable(page))) {
Mel Gorman599d0c92016-07-28 15:45:31 -07001689 spin_unlock_irq(&pgdat->lru_lock);
Mel Gorman66635622010-08-09 17:19:30 -07001690 putback_lru_page(page);
Mel Gorman599d0c92016-07-28 15:45:31 -07001691 spin_lock_irq(&pgdat->lru_lock);
Mel Gorman66635622010-08-09 17:19:30 -07001692 continue;
1693 }
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001694
Mel Gorman599d0c92016-07-28 15:45:31 -07001695 lruvec = mem_cgroup_page_lruvec(page, pgdat);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001696
Linus Torvalds7a608572011-01-17 14:42:19 -08001697 SetPageLRU(page);
Mel Gorman66635622010-08-09 17:19:30 -07001698 lru = page_lru(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001699 add_page_to_lru_list(page, lruvec, lru);
1700
Mel Gorman66635622010-08-09 17:19:30 -07001701 if (is_active_lru(lru)) {
1702 int file = is_file_lru(lru);
Rik van Riel9992af12011-01-13 15:47:13 -08001703 int numpages = hpage_nr_pages(page);
1704 reclaim_stat->recent_rotated[file] += numpages;
Mel Gorman66635622010-08-09 17:19:30 -07001705 }
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001706 if (put_page_testzero(page)) {
1707 __ClearPageLRU(page);
1708 __ClearPageActive(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001709 del_page_from_lru_list(page, lruvec, lru);
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001710
1711 if (unlikely(PageCompound(page))) {
Mel Gorman599d0c92016-07-28 15:45:31 -07001712 spin_unlock_irq(&pgdat->lru_lock);
Johannes Weiner747db952014-08-08 14:19:24 -07001713 mem_cgroup_uncharge(page);
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001714 (*get_compound_page_dtor(page))(page);
Mel Gorman599d0c92016-07-28 15:45:31 -07001715 spin_lock_irq(&pgdat->lru_lock);
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001716 } else
1717 list_add(&page->lru, &pages_to_free);
Mel Gorman66635622010-08-09 17:19:30 -07001718 }
1719 }
Mel Gorman66635622010-08-09 17:19:30 -07001720
Hugh Dickins3f797682012-01-12 17:20:07 -08001721 /*
1722 * To save our caller's stack, now use input list for pages to free.
1723 */
1724 list_splice(&pages_to_free, page_list);
Mel Gorman66635622010-08-09 17:19:30 -07001725}
1726
1727/*
NeilBrown399ba0b2014-06-04 16:07:42 -07001728 * If a kernel thread (such as nfsd for loop-back mounts) services
1729 * a backing device by writing to the page cache it sets PF_LESS_THROTTLE.
1730 * In that case we should only throttle if the backing device it is
1731 * writing to is congested. In other cases it is safe to throttle.
1732 */
1733static int current_may_throttle(void)
1734{
1735 return !(current->flags & PF_LESS_THROTTLE) ||
1736 current->backing_dev_info == NULL ||
1737 bdi_write_congested(current->backing_dev_info);
1738}
1739
1740/*
Mel Gormanb2e18752016-07-28 15:45:37 -07001741 * shrink_inactive_list() is a helper for shrink_node(). It returns the number
Andrew Morton1742f192006-03-22 00:08:21 -08001742 * of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 */
Mel Gorman66635622010-08-09 17:19:30 -07001744static noinline_for_stack unsigned long
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07001745shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec,
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07001746 struct scan_control *sc, enum lru_list lru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
1748 LIST_HEAD(page_list);
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001749 unsigned long nr_scanned;
Andrew Morton05ff5132006-03-22 00:08:20 -08001750 unsigned long nr_reclaimed = 0;
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001751 unsigned long nr_taken;
Michal Hocko3c710c12017-02-22 15:44:27 -08001752 struct reclaim_stat stat = {};
Konstantin Khlebnikovf3fd4a62012-05-29 15:06:54 -07001753 isolate_mode_t isolate_mode = 0;
Konstantin Khlebnikov3cb99452012-05-29 15:06:53 -07001754 int file = is_file_lru(lru);
Mel Gorman599d0c92016-07-28 15:45:31 -07001755 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07001756 struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
Michal Hockodb73ee02017-09-06 16:21:11 -07001757 bool stalled = false;
KOSAKI Motohiro78dc5832009-06-16 15:31:40 -07001758
Mel Gorman599d0c92016-07-28 15:45:31 -07001759 while (unlikely(too_many_isolated(pgdat, file, sc))) {
Michal Hockodb73ee02017-09-06 16:21:11 -07001760 if (stalled)
1761 return 0;
1762
1763 /* wait a bit for the reclaimer. */
1764 msleep(100);
1765 stalled = true;
Rik van Riel35cd7812009-09-21 17:01:38 -07001766
1767 /* We are about to die and free our memory. Return now. */
1768 if (fatal_signal_pending(current))
1769 return SWAP_CLUSTER_MAX;
1770 }
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 lru_add_drain();
Minchan Kimf80c0672011-10-31 17:06:55 -07001773
1774 if (!sc->may_unmap)
Hillf Danton61317282012-03-21 16:33:48 -07001775 isolate_mode |= ISOLATE_UNMAPPED;
Minchan Kimf80c0672011-10-31 17:06:55 -07001776
Mel Gorman599d0c92016-07-28 15:45:31 -07001777 spin_lock_irq(&pgdat->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Konstantin Khlebnikov5dc35972012-05-29 15:06:58 -07001779 nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list,
1780 &nr_scanned, sc, isolate_mode, lru);
Konstantin Khlebnikov95d918f2012-05-29 15:06:59 -07001781
Mel Gorman599d0c92016-07-28 15:45:31 -07001782 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
Hugh Dickins9d5e6a92016-05-19 17:12:38 -07001783 reclaim_stat->recent_scanned[file] += nr_taken;
Konstantin Khlebnikov95d918f2012-05-29 15:06:59 -07001784
Roman Gushchin22621852017-07-06 15:40:25 -07001785 if (current_is_kswapd()) {
1786 if (global_reclaim(sc))
Mel Gorman599d0c92016-07-28 15:45:31 -07001787 __count_vm_events(PGSCAN_KSWAPD, nr_scanned);
Roman Gushchin22621852017-07-06 15:40:25 -07001788 count_memcg_events(lruvec_memcg(lruvec), PGSCAN_KSWAPD,
1789 nr_scanned);
1790 } else {
1791 if (global_reclaim(sc))
Mel Gorman599d0c92016-07-28 15:45:31 -07001792 __count_vm_events(PGSCAN_DIRECT, nr_scanned);
Roman Gushchin22621852017-07-06 15:40:25 -07001793 count_memcg_events(lruvec_memcg(lruvec), PGSCAN_DIRECT,
1794 nr_scanned);
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001795 }
Mel Gorman599d0c92016-07-28 15:45:31 -07001796 spin_unlock_irq(&pgdat->lru_lock);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07001797
Hillf Dantond563c052012-03-21 16:34:02 -07001798 if (nr_taken == 0)
Mel Gorman66635622010-08-09 17:19:30 -07001799 return 0;
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001800
Shaohua Lia128ca72017-05-03 14:52:22 -07001801 nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, 0,
Michal Hocko3c710c12017-02-22 15:44:27 -08001802 &stat, false);
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001803
Mel Gorman599d0c92016-07-28 15:45:31 -07001804 spin_lock_irq(&pgdat->lru_lock);
Hugh Dickins3f797682012-01-12 17:20:07 -08001805
Roman Gushchin22621852017-07-06 15:40:25 -07001806 if (current_is_kswapd()) {
1807 if (global_reclaim(sc))
Mel Gorman599d0c92016-07-28 15:45:31 -07001808 __count_vm_events(PGSTEAL_KSWAPD, nr_reclaimed);
Roman Gushchin22621852017-07-06 15:40:25 -07001809 count_memcg_events(lruvec_memcg(lruvec), PGSTEAL_KSWAPD,
1810 nr_reclaimed);
1811 } else {
1812 if (global_reclaim(sc))
Mel Gorman599d0c92016-07-28 15:45:31 -07001813 __count_vm_events(PGSTEAL_DIRECT, nr_reclaimed);
Roman Gushchin22621852017-07-06 15:40:25 -07001814 count_memcg_events(lruvec_memcg(lruvec), PGSTEAL_DIRECT,
1815 nr_reclaimed);
Ying Han904249a2012-04-25 16:01:48 -07001816 }
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001817
Konstantin Khlebnikov27ac81d2012-05-29 15:07:00 -07001818 putback_inactive_pages(lruvec, &page_list);
Hugh Dickins3f797682012-01-12 17:20:07 -08001819
Mel Gorman599d0c92016-07-28 15:45:31 -07001820 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken);
Hugh Dickins3f797682012-01-12 17:20:07 -08001821
Mel Gorman599d0c92016-07-28 15:45:31 -07001822 spin_unlock_irq(&pgdat->lru_lock);
Hugh Dickins3f797682012-01-12 17:20:07 -08001823
Johannes Weiner747db952014-08-08 14:19:24 -07001824 mem_cgroup_uncharge_list(&page_list);
Mel Gorman2d4894b2017-11-15 17:37:59 -08001825 free_unref_page_list(&page_list);
Mel Gormane11da5b2010-10-26 14:21:40 -07001826
Mel Gorman92df3a72011-10-31 17:07:56 -07001827 /*
1828 * If reclaim is isolating dirty pages under writeback, it implies
1829 * that the long-lived page allocation rate is exceeding the page
1830 * laundering rate. Either the global limits are not being effective
1831 * at throttling processes due to the page distribution throughout
1832 * zones or there is heavy usage of a slow backing device. The
1833 * only option is to throttle from reclaim context which is not ideal
1834 * as there is no guarantee the dirtying process is throttled in the
1835 * same way balance_dirty_pages() manages.
1836 *
Mel Gorman8e950282013-07-03 15:02:02 -07001837 * Once a zone is flagged ZONE_WRITEBACK, kswapd will count the number
1838 * of pages under pages flagged for immediate reclaim and stall if any
1839 * are encountered in the nr_immediate check below.
Mel Gorman92df3a72011-10-31 17:07:56 -07001840 */
Michal Hocko3c710c12017-02-22 15:44:27 -08001841 if (stat.nr_writeback && stat.nr_writeback == nr_taken)
Mel Gorman599d0c92016-07-28 15:45:31 -07001842 set_bit(PGDAT_WRITEBACK, &pgdat->flags);
Mel Gorman92df3a72011-10-31 17:07:56 -07001843
Mel Gormand43006d2013-07-03 15:01:50 -07001844 /*
Tejun Heo97c93412015-05-22 18:23:36 -04001845 * Legacy memcg will stall in page writeback so avoid forcibly
1846 * stalling here.
Mel Gormand43006d2013-07-03 15:01:50 -07001847 */
Tejun Heo97c93412015-05-22 18:23:36 -04001848 if (sane_reclaim(sc)) {
Mel Gormanb1a6f212013-07-03 15:01:58 -07001849 /*
Mel Gorman8e950282013-07-03 15:02:02 -07001850 * Tag a zone as congested if all the dirty pages scanned were
1851 * backed by a congested BDI and wait_iff_congested will stall.
1852 */
Michal Hocko3c710c12017-02-22 15:44:27 -08001853 if (stat.nr_dirty && stat.nr_dirty == stat.nr_congested)
Mel Gorman599d0c92016-07-28 15:45:31 -07001854 set_bit(PGDAT_CONGESTED, &pgdat->flags);
Mel Gorman8e950282013-07-03 15:02:02 -07001855
1856 /*
Mel Gormanb1a6f212013-07-03 15:01:58 -07001857 * If dirty pages are scanned that are not queued for IO, it
Johannes Weiner726d061f2017-02-24 14:56:14 -08001858 * implies that flushers are not doing their job. This can
1859 * happen when memory pressure pushes dirty pages to the end of
1860 * the LRU before the dirty limits are breached and the dirty
1861 * data has expired. It can also happen when the proportion of
1862 * dirty pages grows not through writes but through memory
1863 * pressure reclaiming all the clean cache. And in some cases,
1864 * the flushers simply cannot keep up with the allocation
1865 * rate. Nudge the flusher threads in case they are asleep, but
1866 * also allow kswapd to start writing pages during reclaim.
Mel Gormanb1a6f212013-07-03 15:01:58 -07001867 */
Johannes Weiner726d061f2017-02-24 14:56:14 -08001868 if (stat.nr_unqueued_dirty == nr_taken) {
Jens Axboe9ba4b2d2017-09-20 08:58:25 -06001869 wakeup_flusher_threads(WB_REASON_VMSCAN);
Mel Gorman599d0c92016-07-28 15:45:31 -07001870 set_bit(PGDAT_DIRTY, &pgdat->flags);
Johannes Weiner726d061f2017-02-24 14:56:14 -08001871 }
Mel Gormanb1a6f212013-07-03 15:01:58 -07001872
1873 /*
Linus Torvaldsb738d762014-06-08 14:17:00 -07001874 * If kswapd scans pages marked marked for immediate
1875 * reclaim and under writeback (nr_immediate), it implies
1876 * that pages are cycling through the LRU faster than
Mel Gormanb1a6f212013-07-03 15:01:58 -07001877 * they are written so also forcibly stall.
1878 */
Michal Hocko3c710c12017-02-22 15:44:27 -08001879 if (stat.nr_immediate && current_may_throttle())
Mel Gormanb1a6f212013-07-03 15:01:58 -07001880 congestion_wait(BLK_RW_ASYNC, HZ/10);
Mel Gormane2be15f2013-07-03 15:01:57 -07001881 }
Mel Gormand43006d2013-07-03 15:01:50 -07001882
Mel Gorman8e950282013-07-03 15:02:02 -07001883 /*
1884 * Stall direct reclaim for IO completions if underlying BDIs or zone
1885 * is congested. Allow kswapd to continue until it starts encountering
1886 * unqueued dirty pages or cycling through the LRU too quickly.
1887 */
NeilBrown399ba0b2014-06-04 16:07:42 -07001888 if (!sc->hibernation_mode && !current_is_kswapd() &&
1889 current_may_throttle())
Mel Gorman599d0c92016-07-28 15:45:31 -07001890 wait_iff_congested(pgdat, BLK_RW_ASYNC, HZ/10);
Mel Gorman8e950282013-07-03 15:02:02 -07001891
Mel Gorman599d0c92016-07-28 15:45:31 -07001892 trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id,
1893 nr_scanned, nr_reclaimed,
Michal Hocko5bccd162017-02-22 15:44:30 -08001894 stat.nr_dirty, stat.nr_writeback,
1895 stat.nr_congested, stat.nr_immediate,
1896 stat.nr_activate, stat.nr_ref_keep,
1897 stat.nr_unmap_fail,
yalin wangba5e9572016-01-14 15:18:48 -08001898 sc->priority, file);
Andrew Morton05ff5132006-03-22 00:08:20 -08001899 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900}
1901
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001902/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 * This moves pages from the active list to the inactive list.
1904 *
1905 * We move them the other way if the page is referenced by one or more
1906 * processes, from rmap.
1907 *
1908 * If the pages are mostly unmapped, the processing is fast and it is
Mel Gormana52633d2016-07-28 15:45:28 -07001909 * appropriate to hold zone_lru_lock across the whole operation. But if
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 * the pages are mapped, the processing is slow (page_referenced()) so we
Mel Gormana52633d2016-07-28 15:45:28 -07001911 * should drop zone_lru_lock around each page. It's impossible to balance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 * this, so instead we remove the pages from the LRU while processing them.
1913 * It is safe to rely on PG_active against the non-LRU pages in here because
1914 * nobody will play with that bit on a non-LRU page.
1915 *
Joonsoo Kim0139aa72016-05-19 17:10:49 -07001916 * The downside is that we have to touch page->_refcount against each page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 * But we had to alter page->flags anyway.
Michal Hocko9d998b42017-02-22 15:44:18 -08001918 *
1919 * Returns the number of pages moved to the given lru.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 */
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001921
Michal Hocko9d998b42017-02-22 15:44:18 -08001922static unsigned move_active_pages_to_lru(struct lruvec *lruvec,
Wu Fengguang3eb41402009-06-16 15:33:13 -07001923 struct list_head *list,
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001924 struct list_head *pages_to_free,
Wu Fengguang3eb41402009-06-16 15:33:13 -07001925 enum lru_list lru)
1926{
Mel Gorman599d0c92016-07-28 15:45:31 -07001927 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
Wu Fengguang3eb41402009-06-16 15:33:13 -07001928 struct page *page;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001929 int nr_pages;
Michal Hocko9d998b42017-02-22 15:44:18 -08001930 int nr_moved = 0;
Wu Fengguang3eb41402009-06-16 15:33:13 -07001931
Wu Fengguang3eb41402009-06-16 15:33:13 -07001932 while (!list_empty(list)) {
1933 page = lru_to_page(list);
Mel Gorman599d0c92016-07-28 15:45:31 -07001934 lruvec = mem_cgroup_page_lruvec(page, pgdat);
Wu Fengguang3eb41402009-06-16 15:33:13 -07001935
Sasha Levin309381fea2014-01-23 15:52:54 -08001936 VM_BUG_ON_PAGE(PageLRU(page), page);
Wu Fengguang3eb41402009-06-16 15:33:13 -07001937 SetPageLRU(page);
1938
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001939 nr_pages = hpage_nr_pages(page);
Mel Gorman599d0c92016-07-28 15:45:31 -07001940 update_lru_size(lruvec, lru, page_zonenum(page), nr_pages);
Johannes Weiner925b7672012-01-12 17:18:15 -08001941 list_move(&page->lru, &lruvec->lists[lru]);
Wu Fengguang3eb41402009-06-16 15:33:13 -07001942
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001943 if (put_page_testzero(page)) {
1944 __ClearPageLRU(page);
1945 __ClearPageActive(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001946 del_page_from_lru_list(page, lruvec, lru);
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001947
1948 if (unlikely(PageCompound(page))) {
Mel Gorman599d0c92016-07-28 15:45:31 -07001949 spin_unlock_irq(&pgdat->lru_lock);
Johannes Weiner747db952014-08-08 14:19:24 -07001950 mem_cgroup_uncharge(page);
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001951 (*get_compound_page_dtor(page))(page);
Mel Gorman599d0c92016-07-28 15:45:31 -07001952 spin_lock_irq(&pgdat->lru_lock);
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001953 } else
1954 list_add(&page->lru, pages_to_free);
Michal Hocko9d998b42017-02-22 15:44:18 -08001955 } else {
1956 nr_moved += nr_pages;
Wu Fengguang3eb41402009-06-16 15:33:13 -07001957 }
1958 }
Hugh Dickins9d5e6a92016-05-19 17:12:38 -07001959
Roman Gushchin22621852017-07-06 15:40:25 -07001960 if (!is_active_lru(lru)) {
Michal Hockof0958902017-02-22 15:45:55 -08001961 __count_vm_events(PGDEACTIVATE, nr_moved);
Roman Gushchin22621852017-07-06 15:40:25 -07001962 count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE,
1963 nr_moved);
1964 }
Michal Hocko9d998b42017-02-22 15:44:18 -08001965
1966 return nr_moved;
Wu Fengguang3eb41402009-06-16 15:33:13 -07001967}
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001968
Hugh Dickinsf6260122012-01-12 17:20:06 -08001969static void shrink_active_list(unsigned long nr_to_scan,
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07001970 struct lruvec *lruvec,
Johannes Weinerf16015f2012-01-12 17:17:52 -08001971 struct scan_control *sc,
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07001972 enum lru_list lru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
KOSAKI Motohiro44c241f2009-09-21 17:01:35 -07001974 unsigned long nr_taken;
Hugh Dickinsf6260122012-01-12 17:20:06 -08001975 unsigned long nr_scanned;
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -07001976 unsigned long vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 LIST_HEAD(l_hold); /* The pages which were snipped off */
Wu Fengguang8cab4752009-06-16 15:33:12 -07001978 LIST_HEAD(l_active);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001979 LIST_HEAD(l_inactive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 struct page *page;
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07001981 struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
Michal Hocko9d998b42017-02-22 15:44:18 -08001982 unsigned nr_deactivate, nr_activate;
1983 unsigned nr_rotated = 0;
Konstantin Khlebnikovf3fd4a62012-05-29 15:06:54 -07001984 isolate_mode_t isolate_mode = 0;
Konstantin Khlebnikov3cb99452012-05-29 15:06:53 -07001985 int file = is_file_lru(lru);
Mel Gorman599d0c92016-07-28 15:45:31 -07001986 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
1988 lru_add_drain();
Minchan Kimf80c0672011-10-31 17:06:55 -07001989
1990 if (!sc->may_unmap)
Hillf Danton61317282012-03-21 16:33:48 -07001991 isolate_mode |= ISOLATE_UNMAPPED;
Minchan Kimf80c0672011-10-31 17:06:55 -07001992
Mel Gorman599d0c92016-07-28 15:45:31 -07001993 spin_lock_irq(&pgdat->lru_lock);
Johannes Weiner925b7672012-01-12 17:18:15 -08001994
Konstantin Khlebnikov5dc35972012-05-29 15:06:58 -07001995 nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold,
1996 &nr_scanned, sc, isolate_mode, lru);
Johannes Weiner89b5fae2012-01-12 17:17:50 -08001997
Mel Gorman599d0c92016-07-28 15:45:31 -07001998 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
Johannes Weinerb7c46d12009-09-21 17:02:56 -07001999 reclaim_stat->recent_scanned[file] += nr_taken;
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08002000
Mel Gorman599d0c92016-07-28 15:45:31 -07002001 __count_vm_events(PGREFILL, nr_scanned);
Roman Gushchin22621852017-07-06 15:40:25 -07002002 count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned);
Hugh Dickins9d5e6a92016-05-19 17:12:38 -07002003
Mel Gorman599d0c92016-07-28 15:45:31 -07002004 spin_unlock_irq(&pgdat->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 while (!list_empty(&l_hold)) {
2007 cond_resched();
2008 page = lru_to_page(&l_hold);
2009 list_del(&page->lru);
Rik van Riel7e9cd482008-10-18 20:26:35 -07002010
Hugh Dickins39b5f292012-10-08 16:33:18 -07002011 if (unlikely(!page_evictable(page))) {
Lee Schermerhorn894bc312008-10-18 20:26:39 -07002012 putback_lru_page(page);
2013 continue;
2014 }
2015
Mel Gormancc715d92012-03-21 16:34:00 -07002016 if (unlikely(buffer_heads_over_limit)) {
2017 if (page_has_private(page) && trylock_page(page)) {
2018 if (page_has_private(page))
2019 try_to_release_page(page, 0);
2020 unlock_page(page);
2021 }
2022 }
2023
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07002024 if (page_referenced(page, 0, sc->target_mem_cgroup,
2025 &vm_flags)) {
Rik van Riel9992af12011-01-13 15:47:13 -08002026 nr_rotated += hpage_nr_pages(page);
Wu Fengguang8cab4752009-06-16 15:33:12 -07002027 /*
2028 * Identify referenced, file-backed active pages and
2029 * give them one more trip around the active list. So
2030 * that executable code get better chances to stay in
2031 * memory under moderate memory pressure. Anon pages
2032 * are not likely to be evicted by use-once streaming
2033 * IO, plus JVM can create lots of anon VM_EXEC pages,
2034 * so we ignore them here.
2035 */
Wu Fengguang41e20982009-10-26 16:49:53 -07002036 if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) {
Wu Fengguang8cab4752009-06-16 15:33:12 -07002037 list_add(&page->lru, &l_active);
2038 continue;
2039 }
2040 }
Rik van Riel7e9cd482008-10-18 20:26:35 -07002041
KOSAKI Motohiro5205e562009-09-21 17:01:44 -07002042 ClearPageActive(page); /* we are de-activating */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 list_add(&page->lru, &l_inactive);
2044 }
2045
Andrew Mortonb5557492009-01-06 14:40:13 -08002046 /*
Wu Fengguang8cab4752009-06-16 15:33:12 -07002047 * Move pages back to the lru list.
Andrew Mortonb5557492009-01-06 14:40:13 -08002048 */
Mel Gorman599d0c92016-07-28 15:45:31 -07002049 spin_lock_irq(&pgdat->lru_lock);
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002050 /*
Wu Fengguang8cab4752009-06-16 15:33:12 -07002051 * Count referenced pages from currently used mappings as rotated,
2052 * even though only some of them are actually re-activated. This
2053 * helps balance scan pressure between file and anonymous pages in
Jerome Marchand7c0db9e2014-08-06 16:08:01 -07002054 * get_scan_count.
Rik van Riel7e9cd482008-10-18 20:26:35 -07002055 */
Johannes Weinerb7c46d12009-09-21 17:02:56 -07002056 reclaim_stat->recent_rotated[file] += nr_rotated;
Rik van Riel556adec2008-10-18 20:26:34 -07002057
Michal Hocko9d998b42017-02-22 15:44:18 -08002058 nr_activate = move_active_pages_to_lru(lruvec, &l_active, &l_hold, lru);
2059 nr_deactivate = move_active_pages_to_lru(lruvec, &l_inactive, &l_hold, lru - LRU_ACTIVE);
Mel Gorman599d0c92016-07-28 15:45:31 -07002060 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken);
2061 spin_unlock_irq(&pgdat->lru_lock);
Hugh Dickins2bcf8872012-01-12 17:19:56 -08002062
Johannes Weiner747db952014-08-08 14:19:24 -07002063 mem_cgroup_uncharge_list(&l_hold);
Mel Gorman2d4894b2017-11-15 17:37:59 -08002064 free_unref_page_list(&l_hold);
Michal Hocko9d998b42017-02-22 15:44:18 -08002065 trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate,
2066 nr_deactivate, nr_rotated, sc->priority, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
2068
Rik van Riel59dc76b2016-05-20 16:56:31 -07002069/*
2070 * The inactive anon list should be small enough that the VM never has
2071 * to do too much work.
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002072 *
Rik van Riel59dc76b2016-05-20 16:56:31 -07002073 * The inactive file list should be small enough to leave most memory
2074 * to the established workingset on the scan-resistant active list,
2075 * but large enough to avoid thrashing the aggregate readahead window.
2076 *
2077 * Both inactive lists should also be large enough that each inactive
2078 * page has a chance to be referenced again before it is reclaimed.
2079 *
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002080 * If that fails and refaulting is observed, the inactive list grows.
2081 *
Rik van Riel59dc76b2016-05-20 16:56:31 -07002082 * The inactive_ratio is the target ratio of ACTIVE to INACTIVE pages
Andrey Ryabinin3a50d142017-11-15 17:34:15 -08002083 * on this LRU, maintained by the pageout code. An inactive_ratio
Rik van Riel59dc76b2016-05-20 16:56:31 -07002084 * of 3 means 3:1 or 25% of the pages are kept on the inactive list.
2085 *
2086 * total target max
2087 * memory ratio inactive
2088 * -------------------------------------
2089 * 10MB 1 5MB
2090 * 100MB 1 50MB
2091 * 1GB 3 250MB
2092 * 10GB 10 0.9GB
2093 * 100GB 31 3GB
2094 * 1TB 101 10GB
2095 * 10TB 320 32GB
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002096 */
Mel Gormanf8d1a312016-07-28 15:47:34 -07002097static bool inactive_list_is_low(struct lruvec *lruvec, bool file,
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002098 struct mem_cgroup *memcg,
2099 struct scan_control *sc, bool actual_reclaim)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002100{
Michal Hockofd538802017-02-22 15:45:58 -08002101 enum lru_list active_lru = file * LRU_FILE + LRU_ACTIVE;
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002102 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
2103 enum lru_list inactive_lru = file * LRU_FILE;
2104 unsigned long inactive, active;
2105 unsigned long inactive_ratio;
2106 unsigned long refaults;
Rik van Riel59dc76b2016-05-20 16:56:31 -07002107 unsigned long gb;
2108
Minchan Kim74e3f3c2010-10-26 14:21:31 -07002109 /*
2110 * If we don't have swap space, anonymous page deactivation
2111 * is pointless.
2112 */
Rik van Riel59dc76b2016-05-20 16:56:31 -07002113 if (!file && !total_swap_pages)
Yaowei Bai42e2e452015-11-05 18:47:36 -08002114 return false;
Minchan Kim74e3f3c2010-10-26 14:21:31 -07002115
Michal Hockofd538802017-02-22 15:45:58 -08002116 inactive = lruvec_lru_size(lruvec, inactive_lru, sc->reclaim_idx);
2117 active = lruvec_lru_size(lruvec, active_lru, sc->reclaim_idx);
Mel Gormanf8d1a312016-07-28 15:47:34 -07002118
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002119 if (memcg)
Johannes Weinerccda7f42017-05-03 14:55:16 -07002120 refaults = memcg_page_state(memcg, WORKINGSET_ACTIVATE);
Rik van Rielb39415b2009-12-14 17:59:48 -08002121 else
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002122 refaults = node_page_state(pgdat, WORKINGSET_ACTIVATE);
Rik van Riel59dc76b2016-05-20 16:56:31 -07002123
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002124 /*
2125 * When refaults are being observed, it means a new workingset
2126 * is being established. Disable active list protection to get
2127 * rid of the stale workingset quickly.
2128 */
2129 if (file && actual_reclaim && lruvec->refaults != refaults) {
2130 inactive_ratio = 0;
2131 } else {
2132 gb = (inactive + active) >> (30 - PAGE_SHIFT);
2133 if (gb)
2134 inactive_ratio = int_sqrt(10 * gb);
2135 else
2136 inactive_ratio = 1;
2137 }
2138
2139 if (actual_reclaim)
2140 trace_mm_vmscan_inactive_list_is_low(pgdat->node_id, sc->reclaim_idx,
2141 lruvec_lru_size(lruvec, inactive_lru, MAX_NR_ZONES), inactive,
2142 lruvec_lru_size(lruvec, active_lru, MAX_NR_ZONES), active,
2143 inactive_ratio, file);
Michal Hockofd538802017-02-22 15:45:58 -08002144
Rik van Riel59dc76b2016-05-20 16:56:31 -07002145 return inactive * inactive_ratio < active;
Rik van Rielb39415b2009-12-14 17:59:48 -08002146}
2147
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002148static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002149 struct lruvec *lruvec, struct mem_cgroup *memcg,
2150 struct scan_control *sc)
Christoph Lameterb69408e2008-10-18 20:26:14 -07002151{
Rik van Rielb39415b2009-12-14 17:59:48 -08002152 if (is_active_lru(lru)) {
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002153 if (inactive_list_is_low(lruvec, is_file_lru(lru),
2154 memcg, sc, true))
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07002155 shrink_active_list(nr_to_scan, lruvec, sc, lru);
Rik van Riel556adec2008-10-18 20:26:34 -07002156 return 0;
2157 }
2158
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07002159 return shrink_inactive_list(nr_to_scan, lruvec, sc, lru);
Christoph Lameterb69408e2008-10-18 20:26:14 -07002160}
2161
Johannes Weiner9a265112013-02-22 16:32:17 -08002162enum scan_balance {
2163 SCAN_EQUAL,
2164 SCAN_FRACT,
2165 SCAN_ANON,
2166 SCAN_FILE,
2167};
2168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169/*
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002170 * Determine how aggressively the anon and file LRU lists should be
2171 * scanned. The relative value of each set of LRU lists is determined
2172 * by looking at the fraction of the pages scanned we did rotate back
2173 * onto the active list instead of evict.
2174 *
Wanpeng Libe7bd592012-06-14 20:41:02 +08002175 * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan
2176 * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002177 */
Vladimir Davydov33377672016-01-20 15:02:59 -08002178static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg,
Johannes Weiner6b4f7792014-12-12 16:56:13 -08002179 struct scan_control *sc, unsigned long *nr,
2180 unsigned long *lru_pages)
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002181{
Vladimir Davydov33377672016-01-20 15:02:59 -08002182 int swappiness = mem_cgroup_swappiness(memcg);
Konstantin Khlebnikov90126372012-05-29 15:07:01 -07002183 struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
Johannes Weiner9a265112013-02-22 16:32:17 -08002184 u64 fraction[2];
2185 u64 denominator = 0; /* gcc */
Mel Gorman599d0c92016-07-28 15:45:31 -07002186 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
Johannes Weiner9a265112013-02-22 16:32:17 -08002187 unsigned long anon_prio, file_prio;
2188 enum scan_balance scan_balance;
Johannes Weiner0bf14572014-04-08 16:04:10 -07002189 unsigned long anon, file;
Johannes Weiner9a265112013-02-22 16:32:17 -08002190 unsigned long ap, fp;
2191 enum lru_list lru;
Shaohua Li76a33fc2010-05-24 14:32:36 -07002192
2193 /* If we have no swap space, do not bother scanning anon pages. */
Vladimir Davydovd8b38432016-01-20 15:03:07 -08002194 if (!sc->may_swap || mem_cgroup_get_nr_swap_pages(memcg) <= 0) {
Johannes Weiner9a265112013-02-22 16:32:17 -08002195 scan_balance = SCAN_FILE;
Shaohua Li76a33fc2010-05-24 14:32:36 -07002196 goto out;
2197 }
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002198
Johannes Weiner10316b32013-02-22 16:32:14 -08002199 /*
2200 * Global reclaim will swap to prevent OOM even with no
2201 * swappiness, but memcg users want to use this knob to
2202 * disable swapping for individual groups completely when
2203 * using the memory controller's swap limit feature would be
2204 * too expensive.
2205 */
Johannes Weiner02695172014-08-06 16:06:17 -07002206 if (!global_reclaim(sc) && !swappiness) {
Johannes Weiner9a265112013-02-22 16:32:17 -08002207 scan_balance = SCAN_FILE;
Johannes Weiner10316b32013-02-22 16:32:14 -08002208 goto out;
2209 }
2210
2211 /*
2212 * Do not apply any pressure balancing cleverness when the
2213 * system is close to OOM, scan both anon and file equally
2214 * (unless the swappiness setting disagrees with swapping).
2215 */
Johannes Weiner02695172014-08-06 16:06:17 -07002216 if (!sc->priority && swappiness) {
Johannes Weiner9a265112013-02-22 16:32:17 -08002217 scan_balance = SCAN_EQUAL;
Johannes Weiner10316b32013-02-22 16:32:14 -08002218 goto out;
2219 }
2220
Johannes Weiner11d16c22013-02-22 16:32:15 -08002221 /*
Johannes Weiner62376252014-05-06 12:50:07 -07002222 * Prevent the reclaimer from falling into the cache trap: as
2223 * cache pages start out inactive, every cache fault will tip
2224 * the scan balance towards the file LRU. And as the file LRU
2225 * shrinks, so does the window for rotation from references.
2226 * This means we have a runaway feedback loop where a tiny
2227 * thrashing file LRU becomes infinitely more attractive than
2228 * anon pages. Try to detect this based on file LRU size.
2229 */
2230 if (global_reclaim(sc)) {
Mel Gorman599d0c92016-07-28 15:45:31 -07002231 unsigned long pgdatfile;
2232 unsigned long pgdatfree;
2233 int z;
2234 unsigned long total_high_wmark = 0;
Johannes Weiner62376252014-05-06 12:50:07 -07002235
Mel Gorman599d0c92016-07-28 15:45:31 -07002236 pgdatfree = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES);
2237 pgdatfile = node_page_state(pgdat, NR_ACTIVE_FILE) +
2238 node_page_state(pgdat, NR_INACTIVE_FILE);
Jerome Marchand2ab051e2014-08-06 16:08:03 -07002239
Mel Gorman599d0c92016-07-28 15:45:31 -07002240 for (z = 0; z < MAX_NR_ZONES; z++) {
2241 struct zone *zone = &pgdat->node_zones[z];
Mel Gorman6aa303d2016-09-01 16:14:55 -07002242 if (!managed_zone(zone))
Mel Gorman599d0c92016-07-28 15:45:31 -07002243 continue;
2244
2245 total_high_wmark += high_wmark_pages(zone);
2246 }
2247
2248 if (unlikely(pgdatfile + pgdatfree <= total_high_wmark)) {
David Rientjes06226222017-07-10 15:47:20 -07002249 /*
2250 * Force SCAN_ANON if there are enough inactive
2251 * anonymous pages on the LRU in eligible zones.
2252 * Otherwise, the small LRU gets thrashed.
2253 */
2254 if (!inactive_list_is_low(lruvec, false, memcg, sc, false) &&
2255 lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, sc->reclaim_idx)
2256 >> sc->priority) {
2257 scan_balance = SCAN_ANON;
2258 goto out;
2259 }
Johannes Weiner62376252014-05-06 12:50:07 -07002260 }
2261 }
2262
2263 /*
Vladimir Davydov316bda02016-01-14 15:19:38 -08002264 * If there is enough inactive page cache, i.e. if the size of the
2265 * inactive list is greater than that of the active list *and* the
2266 * inactive list actually has some pages to scan on this priority, we
2267 * do not reclaim anything from the anonymous working set right now.
2268 * Without the second condition we could end up never scanning an
2269 * lruvec even if it has plenty of old anonymous pages unless the
2270 * system is under heavy pressure.
Johannes Weiner7c5bd702013-02-22 16:32:10 -08002271 */
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002272 if (!inactive_list_is_low(lruvec, true, memcg, sc, false) &&
Michal Hocko71ab6cf2017-02-22 15:46:01 -08002273 lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, sc->reclaim_idx) >> sc->priority) {
Johannes Weiner9a265112013-02-22 16:32:17 -08002274 scan_balance = SCAN_FILE;
Johannes Weiner7c5bd702013-02-22 16:32:10 -08002275 goto out;
2276 }
2277
Johannes Weiner9a265112013-02-22 16:32:17 -08002278 scan_balance = SCAN_FRACT;
2279
Johannes Weiner7c5bd702013-02-22 16:32:10 -08002280 /*
KOSAKI Motohiro58c37f62010-08-09 17:19:51 -07002281 * With swappiness at 100, anonymous and file have the same priority.
2282 * This scanning priority is essentially the inverse of IO cost.
2283 */
Johannes Weiner02695172014-08-06 16:06:17 -07002284 anon_prio = swappiness;
Hugh Dickins75b00af2012-05-29 15:07:09 -07002285 file_prio = 200 - anon_prio;
KOSAKI Motohiro58c37f62010-08-09 17:19:51 -07002286
2287 /*
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002288 * OK, so we have swap space and a fair amount of page cache
2289 * pages. We use the recently rotated / recently scanned
2290 * ratios to determine how valuable each cache is.
2291 *
2292 * Because workloads change over time (and to avoid overflow)
2293 * we keep these statistics as a floating average, which ends
2294 * up weighing recent references more than old ones.
2295 *
2296 * anon in [0], file in [1]
2297 */
Jerome Marchand2ab051e2014-08-06 16:08:03 -07002298
Michal Hockofd538802017-02-22 15:45:58 -08002299 anon = lruvec_lru_size(lruvec, LRU_ACTIVE_ANON, MAX_NR_ZONES) +
2300 lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, MAX_NR_ZONES);
2301 file = lruvec_lru_size(lruvec, LRU_ACTIVE_FILE, MAX_NR_ZONES) +
2302 lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, MAX_NR_ZONES);
Jerome Marchand2ab051e2014-08-06 16:08:03 -07002303
Mel Gorman599d0c92016-07-28 15:45:31 -07002304 spin_lock_irq(&pgdat->lru_lock);
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08002305 if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) {
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08002306 reclaim_stat->recent_scanned[0] /= 2;
2307 reclaim_stat->recent_rotated[0] /= 2;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002308 }
2309
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08002310 if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) {
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08002311 reclaim_stat->recent_scanned[1] /= 2;
2312 reclaim_stat->recent_rotated[1] /= 2;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002313 }
2314
2315 /*
Rik van Riel00d80892008-11-19 15:36:44 -08002316 * The amount of pressure on anon vs file pages is inversely
2317 * proportional to the fraction of recently scanned pages on
2318 * each list that were recently referenced and in active use.
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002319 */
Satoru Moriyafe350042012-05-29 15:06:47 -07002320 ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1);
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08002321 ap /= reclaim_stat->recent_rotated[0] + 1;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002322
Satoru Moriyafe350042012-05-29 15:06:47 -07002323 fp = file_prio * (reclaim_stat->recent_scanned[1] + 1);
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08002324 fp /= reclaim_stat->recent_rotated[1] + 1;
Mel Gorman599d0c92016-07-28 15:45:31 -07002325 spin_unlock_irq(&pgdat->lru_lock);
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002326
Shaohua Li76a33fc2010-05-24 14:32:36 -07002327 fraction[0] = ap;
2328 fraction[1] = fp;
2329 denominator = ap + fp + 1;
2330out:
Johannes Weiner688035f2017-05-03 14:52:07 -07002331 *lru_pages = 0;
2332 for_each_evictable_lru(lru) {
2333 int file = is_file_lru(lru);
2334 unsigned long size;
2335 unsigned long scan;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002336
Johannes Weiner688035f2017-05-03 14:52:07 -07002337 size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx);
2338 scan = size >> sc->priority;
2339 /*
2340 * If the cgroup's already been deleted, make sure to
2341 * scrape out the remaining cache.
2342 */
2343 if (!scan && !mem_cgroup_online(memcg))
2344 scan = min(size, SWAP_CLUSTER_MAX);
Johannes Weiner9a265112013-02-22 16:32:17 -08002345
Johannes Weiner688035f2017-05-03 14:52:07 -07002346 switch (scan_balance) {
2347 case SCAN_EQUAL:
2348 /* Scan lists relative to size */
2349 break;
2350 case SCAN_FRACT:
Johannes Weiner9a265112013-02-22 16:32:17 -08002351 /*
Johannes Weiner688035f2017-05-03 14:52:07 -07002352 * Scan types proportional to swappiness and
2353 * their relative recent reclaim efficiency.
Johannes Weiner9a265112013-02-22 16:32:17 -08002354 */
Johannes Weiner688035f2017-05-03 14:52:07 -07002355 scan = div64_u64(scan * fraction[file],
2356 denominator);
2357 break;
2358 case SCAN_FILE:
2359 case SCAN_ANON:
2360 /* Scan one type exclusively */
2361 if ((scan_balance == SCAN_FILE) != file) {
2362 size = 0;
2363 scan = 0;
2364 }
2365 break;
2366 default:
2367 /* Look ma, no brain */
2368 BUG();
Johannes Weiner9a265112013-02-22 16:32:17 -08002369 }
Johannes Weiner688035f2017-05-03 14:52:07 -07002370
2371 *lru_pages += size;
2372 nr[lru] = scan;
Shaohua Li76a33fc2010-05-24 14:32:36 -07002373 }
Wu Fengguang6e08a362009-06-16 15:32:29 -07002374}
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002375
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002376/*
Mel Gormana9dd0a82016-07-28 15:46:02 -07002377 * This is a basic per-node page freer. Used by both kswapd and direct reclaim.
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002378 */
Mel Gormana9dd0a82016-07-28 15:46:02 -07002379static void shrink_node_memcg(struct pglist_data *pgdat, struct mem_cgroup *memcg,
Vladimir Davydov33377672016-01-20 15:02:59 -08002380 struct scan_control *sc, unsigned long *lru_pages)
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002381{
Mel Gormanef8f2322016-07-28 15:46:05 -07002382 struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg);
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002383 unsigned long nr[NR_LRU_LISTS];
Mel Gormane82e0562013-07-03 15:01:44 -07002384 unsigned long targets[NR_LRU_LISTS];
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002385 unsigned long nr_to_scan;
2386 enum lru_list lru;
2387 unsigned long nr_reclaimed = 0;
2388 unsigned long nr_to_reclaim = sc->nr_to_reclaim;
2389 struct blk_plug plug;
Mel Gorman1a501902014-06-04 16:10:49 -07002390 bool scan_adjusted;
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002391
Vladimir Davydov33377672016-01-20 15:02:59 -08002392 get_scan_count(lruvec, memcg, sc, nr, lru_pages);
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002393
Mel Gormane82e0562013-07-03 15:01:44 -07002394 /* Record the original scan target for proportional adjustments later */
2395 memcpy(targets, nr, sizeof(nr));
2396
Mel Gorman1a501902014-06-04 16:10:49 -07002397 /*
2398 * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal
2399 * event that can occur when there is little memory pressure e.g.
2400 * multiple streaming readers/writers. Hence, we do not abort scanning
2401 * when the requested number of pages are reclaimed when scanning at
2402 * DEF_PRIORITY on the assumption that the fact we are direct
2403 * reclaiming implies that kswapd is not keeping up and it is best to
2404 * do a batch of work at once. For memcg reclaim one check is made to
2405 * abort proportional reclaim if either the file or anon lru has already
2406 * dropped to zero at the first pass.
2407 */
2408 scan_adjusted = (global_reclaim(sc) && !current_is_kswapd() &&
2409 sc->priority == DEF_PRIORITY);
2410
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002411 blk_start_plug(&plug);
2412 while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
2413 nr[LRU_INACTIVE_FILE]) {
Mel Gormane82e0562013-07-03 15:01:44 -07002414 unsigned long nr_anon, nr_file, percentage;
2415 unsigned long nr_scanned;
2416
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002417 for_each_evictable_lru(lru) {
2418 if (nr[lru]) {
2419 nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX);
2420 nr[lru] -= nr_to_scan;
2421
2422 nr_reclaimed += shrink_list(lru, nr_to_scan,
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002423 lruvec, memcg, sc);
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002424 }
2425 }
Mel Gormane82e0562013-07-03 15:01:44 -07002426
Michal Hockobd041732016-12-02 17:26:48 -08002427 cond_resched();
2428
Mel Gormane82e0562013-07-03 15:01:44 -07002429 if (nr_reclaimed < nr_to_reclaim || scan_adjusted)
2430 continue;
2431
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002432 /*
Mel Gormane82e0562013-07-03 15:01:44 -07002433 * For kswapd and memcg, reclaim at least the number of pages
Mel Gorman1a501902014-06-04 16:10:49 -07002434 * requested. Ensure that the anon and file LRUs are scanned
Mel Gormane82e0562013-07-03 15:01:44 -07002435 * proportionally what was requested by get_scan_count(). We
2436 * stop reclaiming one LRU and reduce the amount scanning
2437 * proportional to the original scan target.
2438 */
2439 nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE];
2440 nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON];
2441
Mel Gorman1a501902014-06-04 16:10:49 -07002442 /*
2443 * It's just vindictive to attack the larger once the smaller
2444 * has gone to zero. And given the way we stop scanning the
2445 * smaller below, this makes sure that we only make one nudge
2446 * towards proportionality once we've got nr_to_reclaim.
2447 */
2448 if (!nr_file || !nr_anon)
2449 break;
2450
Mel Gormane82e0562013-07-03 15:01:44 -07002451 if (nr_file > nr_anon) {
2452 unsigned long scan_target = targets[LRU_INACTIVE_ANON] +
2453 targets[LRU_ACTIVE_ANON] + 1;
2454 lru = LRU_BASE;
2455 percentage = nr_anon * 100 / scan_target;
2456 } else {
2457 unsigned long scan_target = targets[LRU_INACTIVE_FILE] +
2458 targets[LRU_ACTIVE_FILE] + 1;
2459 lru = LRU_FILE;
2460 percentage = nr_file * 100 / scan_target;
2461 }
2462
2463 /* Stop scanning the smaller of the LRU */
2464 nr[lru] = 0;
2465 nr[lru + LRU_ACTIVE] = 0;
2466
2467 /*
2468 * Recalculate the other LRU scan count based on its original
2469 * scan target and the percentage scanning already complete
2470 */
2471 lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE;
2472 nr_scanned = targets[lru] - nr[lru];
2473 nr[lru] = targets[lru] * (100 - percentage) / 100;
2474 nr[lru] -= min(nr[lru], nr_scanned);
2475
2476 lru += LRU_ACTIVE;
2477 nr_scanned = targets[lru] - nr[lru];
2478 nr[lru] = targets[lru] * (100 - percentage) / 100;
2479 nr[lru] -= min(nr[lru], nr_scanned);
2480
2481 scan_adjusted = true;
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002482 }
2483 blk_finish_plug(&plug);
2484 sc->nr_reclaimed += nr_reclaimed;
2485
2486 /*
2487 * Even if we did not try to evict anon pages at all, we want to
2488 * rebalance the anon lru active/inactive ratio.
2489 */
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002490 if (inactive_list_is_low(lruvec, false, memcg, sc, true))
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002491 shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
2492 sc, LRU_ACTIVE_ANON);
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002493}
2494
Mel Gorman23b9da52012-05-29 15:06:20 -07002495/* Use reclaim/compaction for costly allocs or under memory pressure */
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002496static bool in_reclaim_compaction(struct scan_control *sc)
Mel Gorman23b9da52012-05-29 15:06:20 -07002497{
Kirill A. Shutemovd84da3f2012-12-11 16:00:31 -08002498 if (IS_ENABLED(CONFIG_COMPACTION) && sc->order &&
Mel Gorman23b9da52012-05-29 15:06:20 -07002499 (sc->order > PAGE_ALLOC_COSTLY_ORDER ||
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002500 sc->priority < DEF_PRIORITY - 2))
Mel Gorman23b9da52012-05-29 15:06:20 -07002501 return true;
2502
2503 return false;
2504}
2505
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002506/*
Mel Gorman23b9da52012-05-29 15:06:20 -07002507 * Reclaim/compaction is used for high-order allocation requests. It reclaims
2508 * order-0 pages before compacting the zone. should_continue_reclaim() returns
2509 * true if more pages should be reclaimed such that when the page allocator
2510 * calls try_to_compact_zone() that it will have enough free pages to succeed.
2511 * It will give up earlier than that if there is difficulty reclaiming pages.
Mel Gorman3e7d3442011-01-13 15:45:56 -08002512 */
Mel Gormana9dd0a82016-07-28 15:46:02 -07002513static inline bool should_continue_reclaim(struct pglist_data *pgdat,
Mel Gorman3e7d3442011-01-13 15:45:56 -08002514 unsigned long nr_reclaimed,
2515 unsigned long nr_scanned,
2516 struct scan_control *sc)
2517{
2518 unsigned long pages_for_compaction;
2519 unsigned long inactive_lru_pages;
Mel Gormana9dd0a82016-07-28 15:46:02 -07002520 int z;
Mel Gorman3e7d3442011-01-13 15:45:56 -08002521
2522 /* If not in reclaim/compaction mode, stop */
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002523 if (!in_reclaim_compaction(sc))
Mel Gorman3e7d3442011-01-13 15:45:56 -08002524 return false;
2525
Mel Gorman28765922011-02-25 14:44:20 -08002526 /* Consider stopping depending on scan and reclaim activity */
Michal Hockodcda9b02017-07-12 14:36:45 -07002527 if (sc->gfp_mask & __GFP_RETRY_MAYFAIL) {
Mel Gorman28765922011-02-25 14:44:20 -08002528 /*
Michal Hockodcda9b02017-07-12 14:36:45 -07002529 * For __GFP_RETRY_MAYFAIL allocations, stop reclaiming if the
Mel Gorman28765922011-02-25 14:44:20 -08002530 * full LRU list has been scanned and we are still failing
2531 * to reclaim pages. This full LRU scan is potentially
Michal Hockodcda9b02017-07-12 14:36:45 -07002532 * expensive but a __GFP_RETRY_MAYFAIL caller really wants to succeed
Mel Gorman28765922011-02-25 14:44:20 -08002533 */
2534 if (!nr_reclaimed && !nr_scanned)
2535 return false;
2536 } else {
2537 /*
Michal Hockodcda9b02017-07-12 14:36:45 -07002538 * For non-__GFP_RETRY_MAYFAIL allocations which can presumably
Mel Gorman28765922011-02-25 14:44:20 -08002539 * fail without consequence, stop if we failed to reclaim
2540 * any pages from the last SWAP_CLUSTER_MAX number of
2541 * pages that were scanned. This will return to the
2542 * caller faster at the risk reclaim/compaction and
2543 * the resulting allocation attempt fails
2544 */
2545 if (!nr_reclaimed)
2546 return false;
2547 }
Mel Gorman3e7d3442011-01-13 15:45:56 -08002548
2549 /*
2550 * If we have not reclaimed enough pages for compaction and the
2551 * inactive lists are large enough, continue reclaiming
2552 */
Vlastimil Babka9861a622016-10-07 16:57:53 -07002553 pages_for_compaction = compact_gap(sc->order);
Mel Gormana9dd0a82016-07-28 15:46:02 -07002554 inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE);
Shaohua Liec8acf22013-02-22 16:34:38 -08002555 if (get_nr_swap_pages() > 0)
Mel Gormana9dd0a82016-07-28 15:46:02 -07002556 inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON);
Mel Gorman3e7d3442011-01-13 15:45:56 -08002557 if (sc->nr_reclaimed < pages_for_compaction &&
2558 inactive_lru_pages > pages_for_compaction)
2559 return true;
2560
2561 /* If compaction would go ahead or the allocation would succeed, stop */
Mel Gormana9dd0a82016-07-28 15:46:02 -07002562 for (z = 0; z <= sc->reclaim_idx; z++) {
2563 struct zone *zone = &pgdat->node_zones[z];
Mel Gorman6aa303d2016-09-01 16:14:55 -07002564 if (!managed_zone(zone))
Mel Gormana9dd0a82016-07-28 15:46:02 -07002565 continue;
2566
2567 switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) {
Vlastimil Babkacf378312016-10-07 16:57:41 -07002568 case COMPACT_SUCCESS:
Mel Gormana9dd0a82016-07-28 15:46:02 -07002569 case COMPACT_CONTINUE:
2570 return false;
2571 default:
2572 /* check next zone */
2573 ;
2574 }
Mel Gorman3e7d3442011-01-13 15:45:56 -08002575 }
Mel Gormana9dd0a82016-07-28 15:46:02 -07002576 return true;
Mel Gorman3e7d3442011-01-13 15:45:56 -08002577}
2578
Mel Gorman970a39a2016-07-28 15:46:35 -07002579static bool shrink_node(pg_data_t *pgdat, struct scan_control *sc)
Johannes Weinerf16015f2012-01-12 17:17:52 -08002580{
Vladimir Davydovcb731d62015-02-12 14:58:54 -08002581 struct reclaim_state *reclaim_state = current->reclaim_state;
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002582 unsigned long nr_reclaimed, nr_scanned;
Johannes Weiner2344d7e2014-08-06 16:06:15 -07002583 bool reclaimable = false;
Johannes Weinerf16015f2012-01-12 17:17:52 -08002584
Johannes Weiner56600482012-01-12 17:17:59 -08002585 do {
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002586 struct mem_cgroup *root = sc->target_mem_cgroup;
2587 struct mem_cgroup_reclaim_cookie reclaim = {
Mel Gormanef8f2322016-07-28 15:46:05 -07002588 .pgdat = pgdat,
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002589 .priority = sc->priority,
2590 };
Mel Gormana9dd0a82016-07-28 15:46:02 -07002591 unsigned long node_lru_pages = 0;
Andrew Morton694fbc02013-09-24 15:27:37 -07002592 struct mem_cgroup *memcg;
Johannes Weiner56600482012-01-12 17:17:59 -08002593
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002594 nr_reclaimed = sc->nr_reclaimed;
2595 nr_scanned = sc->nr_scanned;
Konstantin Khlebnikovf9be23d2012-05-29 15:07:02 -07002596
Andrew Morton694fbc02013-09-24 15:27:37 -07002597 memcg = mem_cgroup_iter(root, NULL, &reclaim);
2598 do {
Johannes Weiner6b4f7792014-12-12 16:56:13 -08002599 unsigned long lru_pages;
Johannes Weiner8e8ae642016-01-14 15:21:32 -08002600 unsigned long reclaimed;
Vladimir Davydovcb731d62015-02-12 14:58:54 -08002601 unsigned long scanned;
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002602
Johannes Weiner241994ed2015-02-11 15:26:06 -08002603 if (mem_cgroup_low(root, memcg)) {
Yisheng Xied6622f62017-05-03 14:53:57 -07002604 if (!sc->memcg_low_reclaim) {
2605 sc->memcg_low_skipped = 1;
Johannes Weiner241994ed2015-02-11 15:26:06 -08002606 continue;
Yisheng Xied6622f62017-05-03 14:53:57 -07002607 }
Johannes Weiner31176c72017-05-03 14:55:07 -07002608 mem_cgroup_event(memcg, MEMCG_LOW);
Johannes Weiner241994ed2015-02-11 15:26:06 -08002609 }
2610
Johannes Weiner8e8ae642016-01-14 15:21:32 -08002611 reclaimed = sc->nr_reclaimed;
Vladimir Davydovcb731d62015-02-12 14:58:54 -08002612 scanned = sc->nr_scanned;
Mel Gormana9dd0a82016-07-28 15:46:02 -07002613 shrink_node_memcg(pgdat, memcg, sc, &lru_pages);
2614 node_lru_pages += lru_pages;
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002615
Vladimir Davydovb5afba22016-08-02 14:03:04 -07002616 if (memcg)
Mel Gormana9dd0a82016-07-28 15:46:02 -07002617 shrink_slab(sc->gfp_mask, pgdat->node_id,
Josef Bacik9092c712018-01-31 16:16:26 -08002618 memcg, sc->priority);
Vladimir Davydovcb731d62015-02-12 14:58:54 -08002619
Johannes Weiner8e8ae642016-01-14 15:21:32 -08002620 /* Record the group's reclaim efficiency */
2621 vmpressure(sc->gfp_mask, memcg, false,
2622 sc->nr_scanned - scanned,
2623 sc->nr_reclaimed - reclaimed);
2624
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002625 /*
Michal Hockoa394cb82013-02-22 16:32:30 -08002626 * Direct reclaim and kswapd have to scan all memory
2627 * cgroups to fulfill the overall scan target for the
Mel Gormana9dd0a82016-07-28 15:46:02 -07002628 * node.
Michal Hockoa394cb82013-02-22 16:32:30 -08002629 *
2630 * Limit reclaim, on the other hand, only cares about
2631 * nr_to_reclaim pages to be reclaimed and it will
2632 * retry with decreasing priority if one round over the
2633 * whole hierarchy is not sufficient.
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002634 */
Michal Hockoa394cb82013-02-22 16:32:30 -08002635 if (!global_reclaim(sc) &&
2636 sc->nr_reclaimed >= sc->nr_to_reclaim) {
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002637 mem_cgroup_iter_break(root, memcg);
2638 break;
2639 }
Johannes Weiner241994ed2015-02-11 15:26:06 -08002640 } while ((memcg = mem_cgroup_iter(root, memcg, &reclaim)));
Anton Vorontsov70ddf632013-04-29 15:08:31 -07002641
Mel Gormanb2e18752016-07-28 15:45:37 -07002642 if (global_reclaim(sc))
Mel Gormana9dd0a82016-07-28 15:46:02 -07002643 shrink_slab(sc->gfp_mask, pgdat->node_id, NULL,
Josef Bacik9092c712018-01-31 16:16:26 -08002644 sc->priority);
Johannes Weiner6b4f7792014-12-12 16:56:13 -08002645
Vladimir Davydovcb731d62015-02-12 14:58:54 -08002646 if (reclaim_state) {
2647 sc->nr_reclaimed += reclaim_state->reclaimed_slab;
2648 reclaim_state->reclaimed_slab = 0;
Johannes Weiner6b4f7792014-12-12 16:56:13 -08002649 }
2650
Johannes Weiner8e8ae642016-01-14 15:21:32 -08002651 /* Record the subtree's reclaim efficiency */
2652 vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true,
Anton Vorontsov70ddf632013-04-29 15:08:31 -07002653 sc->nr_scanned - nr_scanned,
2654 sc->nr_reclaimed - nr_reclaimed);
2655
Johannes Weiner2344d7e2014-08-06 16:06:15 -07002656 if (sc->nr_reclaimed - nr_reclaimed)
2657 reclaimable = true;
2658
Mel Gormana9dd0a82016-07-28 15:46:02 -07002659 } while (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed,
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002660 sc->nr_scanned - nr_scanned, sc));
Johannes Weiner2344d7e2014-08-06 16:06:15 -07002661
Johannes Weinerc73322d2017-05-03 14:51:51 -07002662 /*
2663 * Kswapd gives up on balancing particular nodes after too
2664 * many failures to reclaim anything from them and goes to
2665 * sleep. On reclaim progress, reset the failure counter. A
2666 * successful direct reclaim run will revive a dormant kswapd.
2667 */
2668 if (reclaimable)
2669 pgdat->kswapd_failures = 0;
2670
Johannes Weiner2344d7e2014-08-06 16:06:15 -07002671 return reclaimable;
Johannes Weinerf16015f2012-01-12 17:17:52 -08002672}
2673
Vlastimil Babka53853e22014-10-09 15:27:02 -07002674/*
Vlastimil Babkafdd4c6142016-10-07 16:58:03 -07002675 * Returns true if compaction should go ahead for a costly-order request, or
2676 * the allocation would already succeed without compaction. Return false if we
2677 * should reclaim first.
Vlastimil Babka53853e22014-10-09 15:27:02 -07002678 */
Mel Gorman4f588332016-07-28 15:46:38 -07002679static inline bool compaction_ready(struct zone *zone, struct scan_control *sc)
Mel Gormanfe4b1b22012-01-12 17:19:45 -08002680{
Mel Gorman31483b62016-07-28 15:45:46 -07002681 unsigned long watermark;
Vlastimil Babkafdd4c6142016-10-07 16:58:03 -07002682 enum compact_result suitable;
Mel Gormanfe4b1b22012-01-12 17:19:45 -08002683
Vlastimil Babkafdd4c6142016-10-07 16:58:03 -07002684 suitable = compaction_suitable(zone, sc->order, 0, sc->reclaim_idx);
2685 if (suitable == COMPACT_SUCCESS)
2686 /* Allocation should succeed already. Don't reclaim. */
2687 return true;
2688 if (suitable == COMPACT_SKIPPED)
2689 /* Compaction cannot yet proceed. Do reclaim. */
Mel Gormanfe4b1b22012-01-12 17:19:45 -08002690 return false;
2691
Vlastimil Babkafdd4c6142016-10-07 16:58:03 -07002692 /*
2693 * Compaction is already possible, but it takes time to run and there
2694 * are potentially other callers using the pages just freed. So proceed
2695 * with reclaim to make a buffer of free pages available to give
2696 * compaction a reasonable chance of completing and allocating the page.
2697 * Note that we won't actually reclaim the whole buffer in one attempt
2698 * as the target watermark in should_continue_reclaim() is lower. But if
2699 * we are already above the high+gap watermark, don't reclaim at all.
2700 */
2701 watermark = high_wmark_pages(zone) + compact_gap(sc->order);
2702
2703 return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx);
Mel Gormanfe4b1b22012-01-12 17:19:45 -08002704}
2705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706/*
2707 * This is the direct reclaim path, for page-allocating processes. We only
2708 * try to reclaim pages from zones which will satisfy the caller's allocation
2709 * request.
2710 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 * If a zone is deemed to be full of pinned pages then just give it a light
2712 * scan then give up on it.
2713 */
Michal Hocko0a0337e2016-05-20 16:57:00 -07002714static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
Mel Gormandd1a2392008-04-28 02:12:17 -07002716 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07002717 struct zone *zone;
Andrew Morton0608f432013-09-24 15:27:41 -07002718 unsigned long nr_soft_reclaimed;
2719 unsigned long nr_soft_scanned;
Weijie Yang619d0d762014-04-07 15:36:59 -07002720 gfp_t orig_mask;
Mel Gorman79dafcd2016-07-28 15:45:53 -07002721 pg_data_t *last_pgdat = NULL;
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08002722
Mel Gormancc715d92012-03-21 16:34:00 -07002723 /*
2724 * If the number of buffer_heads in the machine exceeds the maximum
2725 * allowed level, force direct reclaim to scan the highmem zone as
2726 * highmem pages could be pinning lowmem pages storing buffer_heads
2727 */
Weijie Yang619d0d762014-04-07 15:36:59 -07002728 orig_mask = sc->gfp_mask;
Mel Gormanb2e18752016-07-28 15:45:37 -07002729 if (buffer_heads_over_limit) {
Mel Gormancc715d92012-03-21 16:34:00 -07002730 sc->gfp_mask |= __GFP_HIGHMEM;
Mel Gorman4f588332016-07-28 15:46:38 -07002731 sc->reclaim_idx = gfp_zone(sc->gfp_mask);
Mel Gormanb2e18752016-07-28 15:45:37 -07002732 }
Mel Gormancc715d92012-03-21 16:34:00 -07002733
Mel Gormand4debc62010-08-09 17:19:29 -07002734 for_each_zone_zonelist_nodemask(zone, z, zonelist,
Mel Gormanb2e18752016-07-28 15:45:37 -07002735 sc->reclaim_idx, sc->nodemask) {
Mel Gormanb2e18752016-07-28 15:45:37 -07002736 /*
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08002737 * Take care memory controller reclaiming has small influence
2738 * to global LRU.
2739 */
Johannes Weiner89b5fae2012-01-12 17:17:50 -08002740 if (global_reclaim(sc)) {
Vladimir Davydov344736f2014-10-20 15:50:30 +04002741 if (!cpuset_zone_allowed(zone,
2742 GFP_KERNEL | __GFP_HARDWALL))
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08002743 continue;
Vladimir Davydov65ec02c2014-04-03 14:47:20 -07002744
Johannes Weiner0b064962014-08-06 16:06:12 -07002745 /*
2746 * If we already have plenty of memory free for
2747 * compaction in this zone, don't free any more.
2748 * Even though compaction is invoked for any
2749 * non-zero order, only frequent costly order
2750 * reclamation is disruptive enough to become a
2751 * noticeable problem, like transparent huge
2752 * page allocations.
2753 */
2754 if (IS_ENABLED(CONFIG_COMPACTION) &&
2755 sc->order > PAGE_ALLOC_COSTLY_ORDER &&
Mel Gorman4f588332016-07-28 15:46:38 -07002756 compaction_ready(zone, sc)) {
Johannes Weiner0b064962014-08-06 16:06:12 -07002757 sc->compaction_ready = true;
2758 continue;
Rik van Riele0887c12011-10-31 17:09:31 -07002759 }
Johannes Weiner0b064962014-08-06 16:06:12 -07002760
Andrew Morton0608f432013-09-24 15:27:41 -07002761 /*
Mel Gorman79dafcd2016-07-28 15:45:53 -07002762 * Shrink each node in the zonelist once. If the
2763 * zonelist is ordered by zone (not the default) then a
2764 * node may be shrunk multiple times but in that case
2765 * the user prefers lower zones being preserved.
2766 */
2767 if (zone->zone_pgdat == last_pgdat)
2768 continue;
2769
2770 /*
Andrew Morton0608f432013-09-24 15:27:41 -07002771 * This steals pages from memory cgroups over softlimit
2772 * and returns the number of reclaimed pages and
2773 * scanned pages. This works for global memory pressure
2774 * and balancing, not for a memcg's limit.
2775 */
2776 nr_soft_scanned = 0;
Mel Gormanef8f2322016-07-28 15:46:05 -07002777 nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat,
Andrew Morton0608f432013-09-24 15:27:41 -07002778 sc->order, sc->gfp_mask,
2779 &nr_soft_scanned);
2780 sc->nr_reclaimed += nr_soft_reclaimed;
2781 sc->nr_scanned += nr_soft_scanned;
KAMEZAWA Hiroyukiac34a1a2011-06-27 16:18:12 -07002782 /* need some check for avoid more shrink_zone() */
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08002783 }
Nick Piggin408d8542006-09-25 23:31:27 -07002784
Mel Gorman79dafcd2016-07-28 15:45:53 -07002785 /* See comment about same check for global reclaim above */
2786 if (zone->zone_pgdat == last_pgdat)
2787 continue;
2788 last_pgdat = zone->zone_pgdat;
Mel Gorman970a39a2016-07-28 15:46:35 -07002789 shrink_node(zone->zone_pgdat, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 }
Mel Gormane0c23272011-10-31 17:09:33 -07002791
Vladimir Davydov65ec02c2014-04-03 14:47:20 -07002792 /*
Weijie Yang619d0d762014-04-07 15:36:59 -07002793 * Restore to original mask to avoid the impact on the caller if we
2794 * promoted it to __GFP_HIGHMEM.
2795 */
2796 sc->gfp_mask = orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797}
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002798
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002799static void snapshot_refaults(struct mem_cgroup *root_memcg, pg_data_t *pgdat)
2800{
2801 struct mem_cgroup *memcg;
2802
2803 memcg = mem_cgroup_iter(root_memcg, NULL, NULL);
2804 do {
2805 unsigned long refaults;
2806 struct lruvec *lruvec;
2807
2808 if (memcg)
Johannes Weinerccda7f42017-05-03 14:55:16 -07002809 refaults = memcg_page_state(memcg, WORKINGSET_ACTIVATE);
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002810 else
2811 refaults = node_page_state(pgdat, WORKINGSET_ACTIVATE);
2812
2813 lruvec = mem_cgroup_lruvec(pgdat, memcg);
2814 lruvec->refaults = refaults;
2815 } while ((memcg = mem_cgroup_iter(root_memcg, memcg, NULL)));
2816}
2817
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818/*
2819 * This is the main entry point to direct page reclaim.
2820 *
2821 * If a full scan of the inactive list fails to free enough memory then we
2822 * are "out of memory" and something needs to be killed.
2823 *
2824 * If the caller is !__GFP_FS then the probability of a failure is reasonably
2825 * high - the zone may be full of dirty or under-writeback pages, which this
Jens Axboe5b0830c2009-09-23 19:37:09 +02002826 * caller can't do much about. We kick the writeback threads and take explicit
2827 * naps in the hope that some of these pages can be written. But if the
2828 * allocating task holds filesystem locks which prevent writeout this might not
2829 * work, and the allocation attempt will fail.
Nishanth Aravamudana41f24e2008-04-29 00:58:25 -07002830 *
2831 * returns: 0, if no pages reclaimed
2832 * else, the number of pages reclaimed
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 */
Mel Gormandac1d272008-04-28 02:12:12 -07002834static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
Vladimir Davydov3115cd92014-04-03 14:47:22 -07002835 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
Johannes Weiner241994ed2015-02-11 15:26:06 -08002837 int initial_priority = sc->priority;
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002838 pg_data_t *last_pgdat;
2839 struct zoneref *z;
2840 struct zone *zone;
Johannes Weiner241994ed2015-02-11 15:26:06 -08002841retry:
Keika Kobayashi873b4772008-07-25 01:48:52 -07002842 delayacct_freepages_start();
2843
Johannes Weiner89b5fae2012-01-12 17:17:50 -08002844 if (global_reclaim(sc))
Mel Gorman7cc30fc2016-07-28 15:46:59 -07002845 __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002847 do {
Anton Vorontsov70ddf632013-04-29 15:08:31 -07002848 vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup,
2849 sc->priority);
Balbir Singh66e17072008-02-07 00:13:56 -08002850 sc->nr_scanned = 0;
Michal Hocko0a0337e2016-05-20 16:57:00 -07002851 shrink_zones(zonelist, sc);
Mel Gormane0c23272011-10-31 17:09:33 -07002852
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07002853 if (sc->nr_reclaimed >= sc->nr_to_reclaim)
Johannes Weiner0b064962014-08-06 16:06:12 -07002854 break;
2855
2856 if (sc->compaction_ready)
2857 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
2859 /*
Minchan Kim0e50ce32013-02-22 16:35:37 -08002860 * If we're getting trouble reclaiming, start doing
2861 * writepage even in laptop mode.
2862 */
2863 if (sc->priority < DEF_PRIORITY - 2)
2864 sc->may_writepage = 1;
Johannes Weiner0b064962014-08-06 16:06:12 -07002865 } while (--sc->priority >= 0);
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07002866
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002867 last_pgdat = NULL;
2868 for_each_zone_zonelist_nodemask(zone, z, zonelist, sc->reclaim_idx,
2869 sc->nodemask) {
2870 if (zone->zone_pgdat == last_pgdat)
2871 continue;
2872 last_pgdat = zone->zone_pgdat;
2873 snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat);
2874 }
2875
Keika Kobayashi873b4772008-07-25 01:48:52 -07002876 delayacct_freepages_end();
2877
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07002878 if (sc->nr_reclaimed)
2879 return sc->nr_reclaimed;
2880
Mel Gorman0cee34f2012-01-12 17:19:49 -08002881 /* Aborted reclaim to try compaction? don't OOM, then */
Johannes Weiner0b064962014-08-06 16:06:12 -07002882 if (sc->compaction_ready)
Mel Gorman73350842012-01-12 17:19:33 -08002883 return 1;
2884
Johannes Weiner241994ed2015-02-11 15:26:06 -08002885 /* Untapped cgroup reserves? Don't OOM, retry. */
Yisheng Xied6622f62017-05-03 14:53:57 -07002886 if (sc->memcg_low_skipped) {
Johannes Weiner241994ed2015-02-11 15:26:06 -08002887 sc->priority = initial_priority;
Yisheng Xied6622f62017-05-03 14:53:57 -07002888 sc->memcg_low_reclaim = 1;
2889 sc->memcg_low_skipped = 0;
Johannes Weiner241994ed2015-02-11 15:26:06 -08002890 goto retry;
2891 }
2892
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07002893 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894}
2895
Johannes Weinerc73322d2017-05-03 14:51:51 -07002896static bool allow_direct_reclaim(pg_data_t *pgdat)
Mel Gorman55150612012-07-31 16:44:35 -07002897{
2898 struct zone *zone;
2899 unsigned long pfmemalloc_reserve = 0;
2900 unsigned long free_pages = 0;
2901 int i;
2902 bool wmark_ok;
2903
Johannes Weinerc73322d2017-05-03 14:51:51 -07002904 if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
2905 return true;
2906
Mel Gorman55150612012-07-31 16:44:35 -07002907 for (i = 0; i <= ZONE_NORMAL; i++) {
2908 zone = &pgdat->node_zones[i];
Johannes Weinerd450abd2017-05-03 14:51:54 -07002909 if (!managed_zone(zone))
2910 continue;
2911
2912 if (!zone_reclaimable_pages(zone))
Mel Gorman675becc2014-06-04 16:07:35 -07002913 continue;
2914
Mel Gorman55150612012-07-31 16:44:35 -07002915 pfmemalloc_reserve += min_wmark_pages(zone);
2916 free_pages += zone_page_state(zone, NR_FREE_PAGES);
2917 }
2918
Mel Gorman675becc2014-06-04 16:07:35 -07002919 /* If there are no reserves (unexpected config) then do not throttle */
2920 if (!pfmemalloc_reserve)
2921 return true;
2922
Mel Gorman55150612012-07-31 16:44:35 -07002923 wmark_ok = free_pages > pfmemalloc_reserve / 2;
2924
2925 /* kswapd must be awake if processes are being throttled */
2926 if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) {
Mel Gorman38087d92016-07-28 15:45:49 -07002927 pgdat->kswapd_classzone_idx = min(pgdat->kswapd_classzone_idx,
Mel Gorman55150612012-07-31 16:44:35 -07002928 (enum zone_type)ZONE_NORMAL);
2929 wake_up_interruptible(&pgdat->kswapd_wait);
2930 }
2931
2932 return wmark_ok;
2933}
2934
2935/*
2936 * Throttle direct reclaimers if backing storage is backed by the network
2937 * and the PFMEMALLOC reserve for the preferred node is getting dangerously
2938 * depleted. kswapd will continue to make progress and wake the processes
Mel Gorman50694c22012-11-26 16:29:48 -08002939 * when the low watermark is reached.
2940 *
2941 * Returns true if a fatal signal was delivered during throttling. If this
2942 * happens, the page allocator should not consider triggering the OOM killer.
Mel Gorman55150612012-07-31 16:44:35 -07002943 */
Mel Gorman50694c22012-11-26 16:29:48 -08002944static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist,
Mel Gorman55150612012-07-31 16:44:35 -07002945 nodemask_t *nodemask)
2946{
Mel Gorman675becc2014-06-04 16:07:35 -07002947 struct zoneref *z;
Mel Gorman55150612012-07-31 16:44:35 -07002948 struct zone *zone;
Mel Gorman675becc2014-06-04 16:07:35 -07002949 pg_data_t *pgdat = NULL;
Mel Gorman55150612012-07-31 16:44:35 -07002950
2951 /*
2952 * Kernel threads should not be throttled as they may be indirectly
2953 * responsible for cleaning pages necessary for reclaim to make forward
2954 * progress. kjournald for example may enter direct reclaim while
2955 * committing a transaction where throttling it could forcing other
2956 * processes to block on log_wait_commit().
2957 */
2958 if (current->flags & PF_KTHREAD)
Mel Gorman50694c22012-11-26 16:29:48 -08002959 goto out;
2960
2961 /*
2962 * If a fatal signal is pending, this process should not throttle.
2963 * It should return quickly so it can exit and free its memory
2964 */
2965 if (fatal_signal_pending(current))
2966 goto out;
Mel Gorman55150612012-07-31 16:44:35 -07002967
Mel Gorman675becc2014-06-04 16:07:35 -07002968 /*
2969 * Check if the pfmemalloc reserves are ok by finding the first node
2970 * with a usable ZONE_NORMAL or lower zone. The expectation is that
2971 * GFP_KERNEL will be required for allocating network buffers when
2972 * swapping over the network so ZONE_HIGHMEM is unusable.
2973 *
2974 * Throttling is based on the first usable node and throttled processes
2975 * wait on a queue until kswapd makes progress and wakes them. There
2976 * is an affinity then between processes waking up and where reclaim
2977 * progress has been made assuming the process wakes on the same node.
2978 * More importantly, processes running on remote nodes will not compete
2979 * for remote pfmemalloc reserves and processes on different nodes
2980 * should make reasonable progress.
2981 */
2982 for_each_zone_zonelist_nodemask(zone, z, zonelist,
Michael S. Tsirkin17636fa2015-01-26 12:58:41 -08002983 gfp_zone(gfp_mask), nodemask) {
Mel Gorman675becc2014-06-04 16:07:35 -07002984 if (zone_idx(zone) > ZONE_NORMAL)
2985 continue;
2986
2987 /* Throttle based on the first usable node */
2988 pgdat = zone->zone_pgdat;
Johannes Weinerc73322d2017-05-03 14:51:51 -07002989 if (allow_direct_reclaim(pgdat))
Mel Gorman675becc2014-06-04 16:07:35 -07002990 goto out;
2991 break;
2992 }
2993
2994 /* If no zone was usable by the allocation flags then do not throttle */
2995 if (!pgdat)
Mel Gorman50694c22012-11-26 16:29:48 -08002996 goto out;
Mel Gorman55150612012-07-31 16:44:35 -07002997
Mel Gorman68243e72012-07-31 16:44:39 -07002998 /* Account for the throttling */
2999 count_vm_event(PGSCAN_DIRECT_THROTTLE);
3000
Mel Gorman55150612012-07-31 16:44:35 -07003001 /*
3002 * If the caller cannot enter the filesystem, it's possible that it
3003 * is due to the caller holding an FS lock or performing a journal
3004 * transaction in the case of a filesystem like ext[3|4]. In this case,
3005 * it is not safe to block on pfmemalloc_wait as kswapd could be
3006 * blocked waiting on the same lock. Instead, throttle for up to a
3007 * second before continuing.
3008 */
3009 if (!(gfp_mask & __GFP_FS)) {
3010 wait_event_interruptible_timeout(pgdat->pfmemalloc_wait,
Johannes Weinerc73322d2017-05-03 14:51:51 -07003011 allow_direct_reclaim(pgdat), HZ);
Mel Gorman50694c22012-11-26 16:29:48 -08003012
3013 goto check_pending;
Mel Gorman55150612012-07-31 16:44:35 -07003014 }
3015
3016 /* Throttle until kswapd wakes the process */
3017 wait_event_killable(zone->zone_pgdat->pfmemalloc_wait,
Johannes Weinerc73322d2017-05-03 14:51:51 -07003018 allow_direct_reclaim(pgdat));
Mel Gorman50694c22012-11-26 16:29:48 -08003019
3020check_pending:
3021 if (fatal_signal_pending(current))
3022 return true;
3023
3024out:
3025 return false;
Mel Gorman55150612012-07-31 16:44:35 -07003026}
3027
Mel Gormandac1d272008-04-28 02:12:12 -07003028unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
KAMEZAWA Hiroyuki327c0e92009-03-31 15:23:31 -07003029 gfp_t gfp_mask, nodemask_t *nodemask)
Balbir Singh66e17072008-02-07 00:13:56 -08003030{
Mel Gorman33906bc2010-08-09 17:19:16 -07003031 unsigned long nr_reclaimed;
Balbir Singh66e17072008-02-07 00:13:56 -08003032 struct scan_control sc = {
KOSAKI Motohiro22fba332009-12-14 17:59:10 -08003033 .nr_to_reclaim = SWAP_CLUSTER_MAX,
Nick Desaulniersf2f43e52017-07-06 15:36:50 -07003034 .gfp_mask = current_gfp_context(gfp_mask),
Mel Gormanb2e18752016-07-28 15:45:37 -07003035 .reclaim_idx = gfp_zone(gfp_mask),
Johannes Weineree814fe2014-08-06 16:06:19 -07003036 .order = order,
3037 .nodemask = nodemask,
3038 .priority = DEF_PRIORITY,
3039 .may_writepage = !laptop_mode,
Johannes Weinera6dc60f82009-03-31 15:19:30 -07003040 .may_unmap = 1,
KOSAKI Motohiro2e2e4252009-04-21 12:24:57 -07003041 .may_swap = 1,
Balbir Singh66e17072008-02-07 00:13:56 -08003042 };
3043
Mel Gorman55150612012-07-31 16:44:35 -07003044 /*
Mel Gorman50694c22012-11-26 16:29:48 -08003045 * Do not enter reclaim if fatal signal was delivered while throttled.
3046 * 1 is returned so that the page allocator does not OOM kill at this
3047 * point.
Mel Gorman55150612012-07-31 16:44:35 -07003048 */
Nick Desaulniersf2f43e52017-07-06 15:36:50 -07003049 if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask))
Mel Gorman55150612012-07-31 16:44:35 -07003050 return 1;
3051
Mel Gorman33906bc2010-08-09 17:19:16 -07003052 trace_mm_vmscan_direct_reclaim_begin(order,
3053 sc.may_writepage,
Nick Desaulniersf2f43e52017-07-06 15:36:50 -07003054 sc.gfp_mask,
Mel Gormane5146b12016-07-28 15:46:47 -07003055 sc.reclaim_idx);
Mel Gorman33906bc2010-08-09 17:19:16 -07003056
Vladimir Davydov3115cd92014-04-03 14:47:22 -07003057 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
Mel Gorman33906bc2010-08-09 17:19:16 -07003058
3059 trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
3060
3061 return nr_reclaimed;
Balbir Singh66e17072008-02-07 00:13:56 -08003062}
3063
Andrew Mortonc255a452012-07-31 16:43:02 -07003064#ifdef CONFIG_MEMCG
Balbir Singh66e17072008-02-07 00:13:56 -08003065
Mel Gormana9dd0a82016-07-28 15:46:02 -07003066unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg,
Balbir Singh4e416952009-09-23 15:56:39 -07003067 gfp_t gfp_mask, bool noswap,
Mel Gormanef8f2322016-07-28 15:46:05 -07003068 pg_data_t *pgdat,
Ying Han0ae5e892011-05-26 16:25:25 -07003069 unsigned long *nr_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07003070{
3071 struct scan_control sc = {
KOSAKI Motohirob8f5c562010-08-10 18:03:02 -07003072 .nr_to_reclaim = SWAP_CLUSTER_MAX,
Johannes Weineree814fe2014-08-06 16:06:19 -07003073 .target_mem_cgroup = memcg,
Balbir Singh4e416952009-09-23 15:56:39 -07003074 .may_writepage = !laptop_mode,
3075 .may_unmap = 1,
Mel Gormanb2e18752016-07-28 15:45:37 -07003076 .reclaim_idx = MAX_NR_ZONES - 1,
Balbir Singh4e416952009-09-23 15:56:39 -07003077 .may_swap = !noswap,
Balbir Singh4e416952009-09-23 15:56:39 -07003078 };
Johannes Weiner6b4f7792014-12-12 16:56:13 -08003079 unsigned long lru_pages;
Ying Han0ae5e892011-05-26 16:25:25 -07003080
Balbir Singh4e416952009-09-23 15:56:39 -07003081 sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
3082 (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003083
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07003084 trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order,
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003085 sc.may_writepage,
Mel Gormane5146b12016-07-28 15:46:47 -07003086 sc.gfp_mask,
3087 sc.reclaim_idx);
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003088
Balbir Singh4e416952009-09-23 15:56:39 -07003089 /*
3090 * NOTE: Although we can get the priority field, using it
3091 * here is not a good idea, since it limits the pages we can scan.
Mel Gormana9dd0a82016-07-28 15:46:02 -07003092 * if we don't reclaim here, the shrink_node from balance_pgdat
Balbir Singh4e416952009-09-23 15:56:39 -07003093 * will pick up pages from other mem cgroup's as well. We hack
3094 * the priority and make it zero.
3095 */
Mel Gormanef8f2322016-07-28 15:46:05 -07003096 shrink_node_memcg(pgdat, memcg, &sc, &lru_pages);
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003097
3098 trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed);
3099
Ying Han0ae5e892011-05-26 16:25:25 -07003100 *nr_scanned = sc.nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003101 return sc.nr_reclaimed;
3102}
3103
Johannes Weiner72835c82012-01-12 17:18:32 -08003104unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003105 unsigned long nr_pages,
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003106 gfp_t gfp_mask,
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003107 bool may_swap)
Balbir Singh66e17072008-02-07 00:13:56 -08003108{
Balbir Singh4e416952009-09-23 15:56:39 -07003109 struct zonelist *zonelist;
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003110 unsigned long nr_reclaimed;
Ying Han889976d2011-05-26 16:25:33 -07003111 int nid;
Vlastimil Babka499118e2017-05-08 15:59:50 -07003112 unsigned int noreclaim_flag;
Balbir Singh66e17072008-02-07 00:13:56 -08003113 struct scan_control sc = {
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003114 .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
Michal Hocko7dea19f2017-05-03 14:53:15 -07003115 .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) |
Johannes Weineree814fe2014-08-06 16:06:19 -07003116 (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK),
Mel Gormanb2e18752016-07-28 15:45:37 -07003117 .reclaim_idx = MAX_NR_ZONES - 1,
Johannes Weineree814fe2014-08-06 16:06:19 -07003118 .target_mem_cgroup = memcg,
3119 .priority = DEF_PRIORITY,
Balbir Singh66e17072008-02-07 00:13:56 -08003120 .may_writepage = !laptop_mode,
Johannes Weinera6dc60f82009-03-31 15:19:30 -07003121 .may_unmap = 1,
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003122 .may_swap = may_swap,
Ying Hana09ed5e2011-05-24 17:12:26 -07003123 };
Balbir Singh66e17072008-02-07 00:13:56 -08003124
Ying Han889976d2011-05-26 16:25:33 -07003125 /*
3126 * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't
3127 * take care of from where we get pages. So the node where we start the
3128 * scan does not need to be the current node.
3129 */
Johannes Weiner72835c82012-01-12 17:18:32 -08003130 nid = mem_cgroup_select_victim_node(memcg);
Ying Han889976d2011-05-26 16:25:33 -07003131
Aneesh Kumar K.Vc9634cf2016-10-07 16:59:12 -07003132 zonelist = &NODE_DATA(nid)->node_zonelists[ZONELIST_FALLBACK];
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003133
3134 trace_mm_vmscan_memcg_reclaim_begin(0,
3135 sc.may_writepage,
Mel Gormane5146b12016-07-28 15:46:47 -07003136 sc.gfp_mask,
3137 sc.reclaim_idx);
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003138
Vlastimil Babka499118e2017-05-08 15:59:50 -07003139 noreclaim_flag = memalloc_noreclaim_save();
Vladimir Davydov3115cd92014-04-03 14:47:22 -07003140 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
Vlastimil Babka499118e2017-05-08 15:59:50 -07003141 memalloc_noreclaim_restore(noreclaim_flag);
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003142
3143 trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
3144
3145 return nr_reclaimed;
Balbir Singh66e17072008-02-07 00:13:56 -08003146}
3147#endif
3148
Mel Gorman1d82de62016-07-28 15:45:43 -07003149static void age_active_anon(struct pglist_data *pgdat,
Mel Gormanef8f2322016-07-28 15:46:05 -07003150 struct scan_control *sc)
Johannes Weinerf16015f2012-01-12 17:17:52 -08003151{
Johannes Weinerb95a2f22012-01-12 17:18:06 -08003152 struct mem_cgroup *memcg;
Johannes Weinerf16015f2012-01-12 17:17:52 -08003153
Johannes Weinerb95a2f22012-01-12 17:18:06 -08003154 if (!total_swap_pages)
3155 return;
3156
3157 memcg = mem_cgroup_iter(NULL, NULL, NULL);
3158 do {
Mel Gormanef8f2322016-07-28 15:46:05 -07003159 struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg);
Johannes Weinerb95a2f22012-01-12 17:18:06 -08003160
Johannes Weiner2a2e4882017-05-03 14:55:03 -07003161 if (inactive_list_is_low(lruvec, false, memcg, sc, true))
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07003162 shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07003163 sc, LRU_ACTIVE_ANON);
Johannes Weinerb95a2f22012-01-12 17:18:06 -08003164
3165 memcg = mem_cgroup_iter(NULL, memcg, NULL);
3166 } while (memcg);
Johannes Weinerf16015f2012-01-12 17:17:52 -08003167}
3168
Mel Gormane716f2e2017-05-03 14:53:45 -07003169/*
3170 * Returns true if there is an eligible zone balanced for the request order
3171 * and classzone_idx
3172 */
3173static bool pgdat_balanced(pg_data_t *pgdat, int order, int classzone_idx)
Johannes Weiner60cefed2012-11-29 13:54:23 -08003174{
Mel Gormane716f2e2017-05-03 14:53:45 -07003175 int i;
3176 unsigned long mark = -1;
3177 struct zone *zone;
Johannes Weiner60cefed2012-11-29 13:54:23 -08003178
Mel Gormane716f2e2017-05-03 14:53:45 -07003179 for (i = 0; i <= classzone_idx; i++) {
3180 zone = pgdat->node_zones + i;
Mel Gorman6256c6b2016-07-28 15:45:56 -07003181
Mel Gormane716f2e2017-05-03 14:53:45 -07003182 if (!managed_zone(zone))
3183 continue;
3184
3185 mark = high_wmark_pages(zone);
3186 if (zone_watermark_ok_safe(zone, order, mark, classzone_idx))
3187 return true;
3188 }
3189
3190 /*
3191 * If a node has no populated zone within classzone_idx, it does not
3192 * need balancing by definition. This can happen if a zone-restricted
3193 * allocation tries to wake a remote kswapd.
3194 */
3195 if (mark == -1)
3196 return true;
3197
3198 return false;
Johannes Weiner60cefed2012-11-29 13:54:23 -08003199}
3200
Mel Gorman631b6e02017-05-03 14:53:41 -07003201/* Clear pgdat state for congested, dirty or under writeback. */
3202static void clear_pgdat_congested(pg_data_t *pgdat)
3203{
3204 clear_bit(PGDAT_CONGESTED, &pgdat->flags);
3205 clear_bit(PGDAT_DIRTY, &pgdat->flags);
3206 clear_bit(PGDAT_WRITEBACK, &pgdat->flags);
3207}
3208
Mel Gorman1741c872011-01-13 15:46:21 -08003209/*
Mel Gorman55150612012-07-31 16:44:35 -07003210 * Prepare kswapd for sleeping. This verifies that there are no processes
3211 * waiting in throttle_direct_reclaim() and that watermarks have been met.
3212 *
3213 * Returns true if kswapd is ready to sleep
3214 */
Mel Gormand9f21d42016-07-28 15:46:41 -07003215static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, int classzone_idx)
Mel Gormanf50de2d2009-12-14 17:58:53 -08003216{
Mel Gorman55150612012-07-31 16:44:35 -07003217 /*
Vlastimil Babka9e5e3662015-01-08 14:32:40 -08003218 * The throttled processes are normally woken up in balance_pgdat() as
Johannes Weinerc73322d2017-05-03 14:51:51 -07003219 * soon as allow_direct_reclaim() is true. But there is a potential
Vlastimil Babka9e5e3662015-01-08 14:32:40 -08003220 * race between when kswapd checks the watermarks and a process gets
3221 * throttled. There is also a potential race if processes get
3222 * throttled, kswapd wakes, a large process exits thereby balancing the
3223 * zones, which causes kswapd to exit balance_pgdat() before reaching
3224 * the wake up checks. If kswapd is going to sleep, no process should
3225 * be sleeping on pfmemalloc_wait, so wake them now if necessary. If
3226 * the wake up is premature, processes will wake kswapd and get
3227 * throttled again. The difference from wake ups in balance_pgdat() is
3228 * that here we are under prepare_to_wait().
Mel Gorman55150612012-07-31 16:44:35 -07003229 */
Vlastimil Babka9e5e3662015-01-08 14:32:40 -08003230 if (waitqueue_active(&pgdat->pfmemalloc_wait))
3231 wake_up_all(&pgdat->pfmemalloc_wait);
Mel Gormanf50de2d2009-12-14 17:58:53 -08003232
Johannes Weinerc73322d2017-05-03 14:51:51 -07003233 /* Hopeless node, leave it to direct reclaim */
3234 if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
3235 return true;
3236
Mel Gormane716f2e2017-05-03 14:53:45 -07003237 if (pgdat_balanced(pgdat, order, classzone_idx)) {
3238 clear_pgdat_congested(pgdat);
3239 return true;
Mel Gorman1d82de62016-07-28 15:45:43 -07003240 }
3241
Shantanu Goel333b0a42017-05-03 14:53:38 -07003242 return false;
Mel Gormanf50de2d2009-12-14 17:58:53 -08003243}
3244
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245/*
Mel Gorman1d82de62016-07-28 15:45:43 -07003246 * kswapd shrinks a node of pages that are at or below the highest usable
3247 * zone that is currently unbalanced.
Mel Gormanb8e83b92013-07-03 15:01:45 -07003248 *
3249 * Returns true if kswapd scanned at least the requested number of pages to
Mel Gorman283aba92013-07-03 15:01:51 -07003250 * reclaim or if the lack of progress was due to pages under writeback.
3251 * This is used to determine if the scanning priority needs to be raised.
Mel Gorman75485362013-07-03 15:01:42 -07003252 */
Mel Gorman1d82de62016-07-28 15:45:43 -07003253static bool kswapd_shrink_node(pg_data_t *pgdat,
Vlastimil Babkaaccf6242016-03-17 14:18:15 -07003254 struct scan_control *sc)
Mel Gorman75485362013-07-03 15:01:42 -07003255{
Mel Gorman1d82de62016-07-28 15:45:43 -07003256 struct zone *zone;
3257 int z;
Mel Gorman75485362013-07-03 15:01:42 -07003258
Mel Gorman1d82de62016-07-28 15:45:43 -07003259 /* Reclaim a number of pages proportional to the number of zones */
3260 sc->nr_to_reclaim = 0;
Mel Gorman970a39a2016-07-28 15:46:35 -07003261 for (z = 0; z <= sc->reclaim_idx; z++) {
Mel Gorman1d82de62016-07-28 15:45:43 -07003262 zone = pgdat->node_zones + z;
Mel Gorman6aa303d2016-09-01 16:14:55 -07003263 if (!managed_zone(zone))
Mel Gorman1d82de62016-07-28 15:45:43 -07003264 continue;
Mel Gorman7c954f62013-07-03 15:01:54 -07003265
Mel Gorman1d82de62016-07-28 15:45:43 -07003266 sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX);
Mel Gorman7c954f62013-07-03 15:01:54 -07003267 }
3268
Mel Gorman1d82de62016-07-28 15:45:43 -07003269 /*
3270 * Historically care was taken to put equal pressure on all zones but
3271 * now pressure is applied based on node LRU order.
3272 */
Mel Gorman970a39a2016-07-28 15:46:35 -07003273 shrink_node(pgdat, sc);
Mel Gorman1d82de62016-07-28 15:45:43 -07003274
3275 /*
3276 * Fragmentation may mean that the system cannot be rebalanced for
3277 * high-order allocations. If twice the allocation size has been
3278 * reclaimed then recheck watermarks only at order-0 to prevent
3279 * excessive reclaim. Assume that a process requested a high-order
3280 * can direct reclaim/compact.
3281 */
Vlastimil Babka9861a622016-10-07 16:57:53 -07003282 if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order))
Mel Gorman1d82de62016-07-28 15:45:43 -07003283 sc->order = 0;
3284
Mel Gormanb8e83b92013-07-03 15:01:45 -07003285 return sc->nr_scanned >= sc->nr_to_reclaim;
Mel Gorman75485362013-07-03 15:01:42 -07003286}
3287
3288/*
Mel Gorman1d82de62016-07-28 15:45:43 -07003289 * For kswapd, balance_pgdat() will reclaim pages across a node from zones
3290 * that are eligible for use by the caller until at least one zone is
3291 * balanced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 *
Mel Gorman1d82de62016-07-28 15:45:43 -07003293 * Returns the order kswapd finished reclaiming at.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 *
3295 * kswapd scans the zones in the highmem->normal->dma direction. It skips
Mel Gorman41858962009-06-16 15:32:12 -07003296 * zones which have free_pages > high_wmark_pages(zone), but once a zone is
Mel Gorman1d82de62016-07-28 15:45:43 -07003297 * found to have free_pages <= high_wmark_pages(zone), any page is that zone
3298 * or lower is eligible for reclaim until at least one usable zone is
3299 * balanced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 */
Vlastimil Babkaaccf6242016-03-17 14:18:15 -07003301static int balance_pgdat(pg_data_t *pgdat, int order, int classzone_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 int i;
Andrew Morton0608f432013-09-24 15:27:41 -07003304 unsigned long nr_soft_reclaimed;
3305 unsigned long nr_soft_scanned;
Mel Gorman1d82de62016-07-28 15:45:43 -07003306 struct zone *zone;
Andrew Morton179e9632006-03-22 00:08:18 -08003307 struct scan_control sc = {
3308 .gfp_mask = GFP_KERNEL,
Johannes Weineree814fe2014-08-06 16:06:19 -07003309 .order = order,
Mel Gormanb8e83b92013-07-03 15:01:45 -07003310 .priority = DEF_PRIORITY,
Johannes Weineree814fe2014-08-06 16:06:19 -07003311 .may_writepage = !laptop_mode,
Johannes Weinera6dc60f82009-03-31 15:19:30 -07003312 .may_unmap = 1,
KOSAKI Motohiro2e2e4252009-04-21 12:24:57 -07003313 .may_swap = 1,
Andrew Morton179e9632006-03-22 00:08:18 -08003314 };
Christoph Lameterf8891e52006-06-30 01:55:45 -07003315 count_vm_event(PAGEOUTRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07003317 do {
Johannes Weinerc73322d2017-05-03 14:51:51 -07003318 unsigned long nr_reclaimed = sc.nr_reclaimed;
Mel Gormanb8e83b92013-07-03 15:01:45 -07003319 bool raise_priority = true;
3320
Mel Gorman84c7a772016-07-28 15:46:44 -07003321 sc.reclaim_idx = classzone_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
Mel Gorman86c79f62016-07-28 15:45:59 -07003323 /*
Mel Gorman84c7a772016-07-28 15:46:44 -07003324 * If the number of buffer_heads exceeds the maximum allowed
3325 * then consider reclaiming from all zones. This has a dual
3326 * purpose -- on 64-bit systems it is expected that
3327 * buffer_heads are stripped during active rotation. On 32-bit
3328 * systems, highmem pages can pin lowmem memory and shrinking
3329 * buffers can relieve lowmem pressure. Reclaim may still not
3330 * go ahead if all eligible zones for the original allocation
3331 * request are balanced to avoid excessive reclaim from kswapd.
Mel Gorman86c79f62016-07-28 15:45:59 -07003332 */
3333 if (buffer_heads_over_limit) {
3334 for (i = MAX_NR_ZONES - 1; i >= 0; i--) {
3335 zone = pgdat->node_zones + i;
Mel Gorman6aa303d2016-09-01 16:14:55 -07003336 if (!managed_zone(zone))
Mel Gorman86c79f62016-07-28 15:45:59 -07003337 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
Mel Gorman970a39a2016-07-28 15:46:35 -07003339 sc.reclaim_idx = i;
Andrew Mortone1dbeda2006-12-06 20:32:01 -08003340 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 }
Zlatko Calusicdafcb732013-02-22 16:32:34 -08003343
Mel Gorman86c79f62016-07-28 15:45:59 -07003344 /*
Mel Gormane716f2e2017-05-03 14:53:45 -07003345 * Only reclaim if there are no eligible zones. Note that
3346 * sc.reclaim_idx is not used as buffer_heads_over_limit may
3347 * have adjusted it.
Mel Gorman86c79f62016-07-28 15:45:59 -07003348 */
Mel Gormane716f2e2017-05-03 14:53:45 -07003349 if (pgdat_balanced(pgdat, sc.order, classzone_idx))
3350 goto out;
Andrew Mortone1dbeda2006-12-06 20:32:01 -08003351
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 /*
Mel Gorman1d82de62016-07-28 15:45:43 -07003353 * Do some background aging of the anon list, to give
3354 * pages a chance to be referenced before reclaiming. All
3355 * pages are rotated regardless of classzone as this is
3356 * about consistent aging.
3357 */
Mel Gormanef8f2322016-07-28 15:46:05 -07003358 age_active_anon(pgdat, &sc);
Mel Gorman1d82de62016-07-28 15:45:43 -07003359
3360 /*
Mel Gormanb7ea3c42013-07-03 15:01:53 -07003361 * If we're getting trouble reclaiming, start doing writepage
3362 * even in laptop mode.
3363 */
Johannes Weiner047d72c2017-05-03 14:51:57 -07003364 if (sc.priority < DEF_PRIORITY - 2)
Mel Gormanb7ea3c42013-07-03 15:01:53 -07003365 sc.may_writepage = 1;
3366
Mel Gorman1d82de62016-07-28 15:45:43 -07003367 /* Call soft limit reclaim before calling shrink_node. */
3368 sc.nr_scanned = 0;
3369 nr_soft_scanned = 0;
Mel Gormanef8f2322016-07-28 15:46:05 -07003370 nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order,
Mel Gorman1d82de62016-07-28 15:45:43 -07003371 sc.gfp_mask, &nr_soft_scanned);
3372 sc.nr_reclaimed += nr_soft_reclaimed;
3373
Mel Gormanb7ea3c42013-07-03 15:01:53 -07003374 /*
Mel Gorman1d82de62016-07-28 15:45:43 -07003375 * There should be no need to raise the scanning priority if
3376 * enough pages are already being scanned that that high
3377 * watermark would be met at 100% efficiency.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 */
Mel Gorman970a39a2016-07-28 15:46:35 -07003379 if (kswapd_shrink_node(pgdat, &sc))
Mel Gorman1d82de62016-07-28 15:45:43 -07003380 raise_priority = false;
Mel Gorman55150612012-07-31 16:44:35 -07003381
3382 /*
3383 * If the low watermark is met there is no need for processes
3384 * to be throttled on pfmemalloc_wait as they should not be
3385 * able to safely make forward progress. Wake them
3386 */
3387 if (waitqueue_active(&pgdat->pfmemalloc_wait) &&
Johannes Weinerc73322d2017-05-03 14:51:51 -07003388 allow_direct_reclaim(pgdat))
Vlastimil Babkacfc51152015-02-11 15:25:12 -08003389 wake_up_all(&pgdat->pfmemalloc_wait);
Mel Gorman55150612012-07-31 16:44:35 -07003390
Mel Gormanb8e83b92013-07-03 15:01:45 -07003391 /* Check if kswapd should be suspending */
3392 if (try_to_freeze() || kthread_should_stop())
3393 break;
3394
3395 /*
3396 * Raise priority if scanning rate is too low or there was no
3397 * progress in reclaiming pages
3398 */
Johannes Weinerc73322d2017-05-03 14:51:51 -07003399 nr_reclaimed = sc.nr_reclaimed - nr_reclaimed;
3400 if (raise_priority || !nr_reclaimed)
Mel Gormanb8e83b92013-07-03 15:01:45 -07003401 sc.priority--;
Mel Gorman1d82de62016-07-28 15:45:43 -07003402 } while (sc.priority >= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403
Johannes Weinerc73322d2017-05-03 14:51:51 -07003404 if (!sc.nr_reclaimed)
3405 pgdat->kswapd_failures++;
3406
Mel Gormanb8e83b92013-07-03 15:01:45 -07003407out:
Johannes Weiner2a2e4882017-05-03 14:55:03 -07003408 snapshot_refaults(NULL, pgdat);
Mel Gorman0abdee22011-01-13 15:46:22 -08003409 /*
Mel Gorman1d82de62016-07-28 15:45:43 -07003410 * Return the order kswapd stopped reclaiming at as
3411 * prepare_kswapd_sleep() takes it into account. If another caller
3412 * entered the allocator slow path while kswapd was awake, order will
3413 * remain at the higher level.
Mel Gorman0abdee22011-01-13 15:46:22 -08003414 */
Mel Gorman1d82de62016-07-28 15:45:43 -07003415 return sc.order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416}
3417
Mel Gormane716f2e2017-05-03 14:53:45 -07003418/*
3419 * pgdat->kswapd_classzone_idx is the highest zone index that a recent
3420 * allocation request woke kswapd for. When kswapd has not woken recently,
3421 * the value is MAX_NR_ZONES which is not a valid index. This compares a
3422 * given classzone and returns it or the highest classzone index kswapd
3423 * was recently woke for.
3424 */
3425static enum zone_type kswapd_classzone_idx(pg_data_t *pgdat,
3426 enum zone_type classzone_idx)
3427{
3428 if (pgdat->kswapd_classzone_idx == MAX_NR_ZONES)
3429 return classzone_idx;
3430
3431 return max(pgdat->kswapd_classzone_idx, classzone_idx);
3432}
3433
Mel Gorman38087d92016-07-28 15:45:49 -07003434static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order,
3435 unsigned int classzone_idx)
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08003436{
3437 long remaining = 0;
3438 DEFINE_WAIT(wait);
3439
3440 if (freezing(current) || kthread_should_stop())
3441 return;
3442
3443 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
3444
Shantanu Goel333b0a42017-05-03 14:53:38 -07003445 /*
3446 * Try to sleep for a short interval. Note that kcompactd will only be
3447 * woken if it is possible to sleep for a short interval. This is
3448 * deliberate on the assumption that if reclaim cannot keep an
3449 * eligible zone balanced that it's also unlikely that compaction will
3450 * succeed.
3451 */
Mel Gormand9f21d42016-07-28 15:46:41 -07003452 if (prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) {
Vlastimil Babkafd901c92016-04-28 16:18:49 -07003453 /*
3454 * Compaction records what page blocks it recently failed to
3455 * isolate pages from and skips them in the future scanning.
3456 * When kswapd is going to sleep, it is reasonable to assume
3457 * that pages and compaction may succeed so reset the cache.
3458 */
3459 reset_isolation_suitable(pgdat);
3460
3461 /*
3462 * We have freed the memory, now we should compact it to make
3463 * allocation of the requested order possible.
3464 */
Mel Gorman38087d92016-07-28 15:45:49 -07003465 wakeup_kcompactd(pgdat, alloc_order, classzone_idx);
Vlastimil Babkafd901c92016-04-28 16:18:49 -07003466
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08003467 remaining = schedule_timeout(HZ/10);
Mel Gorman38087d92016-07-28 15:45:49 -07003468
3469 /*
3470 * If woken prematurely then reset kswapd_classzone_idx and
3471 * order. The values will either be from a wakeup request or
3472 * the previous request that slept prematurely.
3473 */
3474 if (remaining) {
Mel Gormane716f2e2017-05-03 14:53:45 -07003475 pgdat->kswapd_classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx);
Mel Gorman38087d92016-07-28 15:45:49 -07003476 pgdat->kswapd_order = max(pgdat->kswapd_order, reclaim_order);
3477 }
3478
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08003479 finish_wait(&pgdat->kswapd_wait, &wait);
3480 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
3481 }
3482
3483 /*
3484 * After a short sleep, check if it was a premature sleep. If not, then
3485 * go fully to sleep until explicitly woken up.
3486 */
Mel Gormand9f21d42016-07-28 15:46:41 -07003487 if (!remaining &&
3488 prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) {
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08003489 trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
3490
3491 /*
3492 * vmstat counters are not perfectly accurate and the estimated
3493 * value for counters such as NR_FREE_PAGES can deviate from the
3494 * true value by nr_online_cpus * threshold. To avoid the zone
3495 * watermarks being breached while under pressure, we reduce the
3496 * per-cpu vmstat threshold while kswapd is awake and restore
3497 * them before going back to sleep.
3498 */
3499 set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold);
Aaditya Kumar1c7e7f62012-07-17 15:48:07 -07003500
3501 if (!kthread_should_stop())
3502 schedule();
3503
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08003504 set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold);
3505 } else {
3506 if (remaining)
3507 count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY);
3508 else
3509 count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY);
3510 }
3511 finish_wait(&pgdat->kswapd_wait, &wait);
3512}
3513
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514/*
3515 * The background pageout daemon, started as a kernel thread
Rik van Riel4f98a2f2008-10-18 20:26:32 -07003516 * from the init process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 *
3518 * This basically trickles out pages so that we have _some_
3519 * free memory available even if there is no other activity
3520 * that frees anything up. This is needed for things like routing
3521 * etc, where we otherwise might have all activity going on in
3522 * asynchronous contexts that cannot page things out.
3523 *
3524 * If there are applications that are active memory-allocators
3525 * (most normal use), this basically shouldn't matter.
3526 */
3527static int kswapd(void *p)
3528{
Mel Gormane716f2e2017-05-03 14:53:45 -07003529 unsigned int alloc_order, reclaim_order;
3530 unsigned int classzone_idx = MAX_NR_ZONES - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 pg_data_t *pgdat = (pg_data_t*)p;
3532 struct task_struct *tsk = current;
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08003533
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 struct reclaim_state reclaim_state = {
3535 .reclaimed_slab = 0,
3536 };
Rusty Russella70f7302009-03-13 14:49:46 +10303537 const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
Rusty Russell174596a2009-01-01 10:12:29 +10303539 if (!cpumask_empty(cpumask))
Mike Travisc5f59f02008-04-04 18:11:10 -07003540 set_cpus_allowed_ptr(tsk, cpumask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 current->reclaim_state = &reclaim_state;
3542
3543 /*
3544 * Tell the memory management that we're a "memory allocator",
3545 * and that if we need more memory we should get access to it
3546 * regardless (see "__alloc_pages()"). "kswapd" should
3547 * never get caught in the normal page freeing logic.
3548 *
3549 * (Kswapd normally doesn't need memory anyway, but sometimes
3550 * you need a small amount of memory in order to be able to
3551 * page out something else, and this flag essentially protects
3552 * us from recursively trying to free more memory as we're
3553 * trying to free the first piece of memory in the first place).
3554 */
Christoph Lameter930d9152006-01-08 01:00:47 -08003555 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003556 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557
Mel Gormane716f2e2017-05-03 14:53:45 -07003558 pgdat->kswapd_order = 0;
3559 pgdat->kswapd_classzone_idx = MAX_NR_ZONES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 for ( ; ; ) {
Jeff Liu6f6313d2012-12-11 16:02:48 -08003561 bool ret;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07003562
Mel Gormane716f2e2017-05-03 14:53:45 -07003563 alloc_order = reclaim_order = pgdat->kswapd_order;
3564 classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx);
3565
Mel Gorman38087d92016-07-28 15:45:49 -07003566kswapd_try_sleep:
3567 kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order,
3568 classzone_idx);
Mel Gorman215ddd62011-07-08 15:39:40 -07003569
Mel Gorman38087d92016-07-28 15:45:49 -07003570 /* Read the new order and classzone_idx */
3571 alloc_order = reclaim_order = pgdat->kswapd_order;
Mel Gormane716f2e2017-05-03 14:53:45 -07003572 classzone_idx = kswapd_classzone_idx(pgdat, 0);
Mel Gorman38087d92016-07-28 15:45:49 -07003573 pgdat->kswapd_order = 0;
Mel Gormane716f2e2017-05-03 14:53:45 -07003574 pgdat->kswapd_classzone_idx = MAX_NR_ZONES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575
David Rientjes8fe23e02009-12-14 17:58:33 -08003576 ret = try_to_freeze();
3577 if (kthread_should_stop())
3578 break;
3579
3580 /*
3581 * We can speed up thawing tasks if we don't call balance_pgdat
3582 * after returning from the refrigerator
3583 */
Mel Gorman38087d92016-07-28 15:45:49 -07003584 if (ret)
3585 continue;
Mel Gorman1d82de62016-07-28 15:45:43 -07003586
Mel Gorman38087d92016-07-28 15:45:49 -07003587 /*
3588 * Reclaim begins at the requested order but if a high-order
3589 * reclaim fails then kswapd falls back to reclaiming for
3590 * order-0. If that happens, kswapd will consider sleeping
3591 * for the order it finished reclaiming at (reclaim_order)
3592 * but kcompactd is woken to compact for the original
3593 * request (alloc_order).
3594 */
Mel Gormane5146b12016-07-28 15:46:47 -07003595 trace_mm_vmscan_kswapd_wake(pgdat->node_id, classzone_idx,
3596 alloc_order);
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01003597 fs_reclaim_acquire(GFP_KERNEL);
Mel Gorman38087d92016-07-28 15:45:49 -07003598 reclaim_order = balance_pgdat(pgdat, alloc_order, classzone_idx);
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01003599 fs_reclaim_release(GFP_KERNEL);
Mel Gorman38087d92016-07-28 15:45:49 -07003600 if (reclaim_order < alloc_order)
3601 goto kswapd_try_sleep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 }
Takamori Yamaguchib0a8cc52012-11-08 15:53:39 -08003603
Johannes Weiner71abdc12014-06-06 14:35:35 -07003604 tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD);
Takamori Yamaguchib0a8cc52012-11-08 15:53:39 -08003605 current->reclaim_state = NULL;
Johannes Weiner71abdc12014-06-06 14:35:35 -07003606
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 return 0;
3608}
3609
3610/*
3611 * A zone is low on free memory, so wake its kswapd task to service it.
3612 */
Mel Gorman99504742011-01-13 15:46:20 -08003613void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614{
3615 pg_data_t *pgdat;
3616
Mel Gorman6aa303d2016-09-01 16:14:55 -07003617 if (!managed_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 return;
3619
Vladimir Davydov344736f2014-10-20 15:50:30 +04003620 if (!cpuset_zone_allowed(zone, GFP_KERNEL | __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 return;
Mel Gorman88f5acf2011-01-13 15:45:41 -08003622 pgdat = zone->zone_pgdat;
Mel Gormane716f2e2017-05-03 14:53:45 -07003623 pgdat->kswapd_classzone_idx = kswapd_classzone_idx(pgdat,
3624 classzone_idx);
Mel Gorman38087d92016-07-28 15:45:49 -07003625 pgdat->kswapd_order = max(pgdat->kswapd_order, order);
Con Kolivas8d0986e2005-09-13 01:25:07 -07003626 if (!waitqueue_active(&pgdat->kswapd_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 return;
Mel Gormane1a55632016-07-28 15:46:26 -07003628
Johannes Weinerc73322d2017-05-03 14:51:51 -07003629 /* Hopeless node, leave it to direct reclaim */
3630 if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
3631 return;
3632
Mel Gormane716f2e2017-05-03 14:53:45 -07003633 if (pgdat_balanced(pgdat, order, classzone_idx))
3634 return;
Mel Gormane1a55632016-07-28 15:46:26 -07003635
Mel Gormane716f2e2017-05-03 14:53:45 -07003636 trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, classzone_idx, order);
Con Kolivas8d0986e2005-09-13 01:25:07 -07003637 wake_up_interruptible(&pgdat->kswapd_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638}
3639
Rafael J. Wysockic6f37f12009-05-24 22:16:31 +02003640#ifdef CONFIG_HIBERNATION
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641/*
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08003642 * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07003643 * freed pages.
3644 *
3645 * Rather than trying to age LRUs the aim is to preserve the overall
3646 * LRU order by reclaiming preferentially
3647 * inactive > active > active referenced > active mapped
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 */
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08003649unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650{
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07003651 struct reclaim_state reclaim_state;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07003652 struct scan_control sc = {
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08003653 .nr_to_reclaim = nr_to_reclaim,
Johannes Weineree814fe2014-08-06 16:06:19 -07003654 .gfp_mask = GFP_HIGHUSER_MOVABLE,
Mel Gormanb2e18752016-07-28 15:45:37 -07003655 .reclaim_idx = MAX_NR_ZONES - 1,
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07003656 .priority = DEF_PRIORITY,
Johannes Weineree814fe2014-08-06 16:06:19 -07003657 .may_writepage = 1,
3658 .may_unmap = 1,
3659 .may_swap = 1,
3660 .hibernation_mode = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 };
Ying Hana09ed5e2011-05-24 17:12:26 -07003662 struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08003663 struct task_struct *p = current;
3664 unsigned long nr_reclaimed;
Vlastimil Babka499118e2017-05-08 15:59:50 -07003665 unsigned int noreclaim_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666
Vlastimil Babka499118e2017-05-08 15:59:50 -07003667 noreclaim_flag = memalloc_noreclaim_save();
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01003668 fs_reclaim_acquire(sc.gfp_mask);
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08003669 reclaim_state.reclaimed_slab = 0;
3670 p->reclaim_state = &reclaim_state;
Andrew Morton69e05942006-03-22 00:08:19 -08003671
Vladimir Davydov3115cd92014-04-03 14:47:22 -07003672 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07003673
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08003674 p->reclaim_state = NULL;
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01003675 fs_reclaim_release(sc.gfp_mask);
Vlastimil Babka499118e2017-05-08 15:59:50 -07003676 memalloc_noreclaim_restore(noreclaim_flag);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07003677
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08003678 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679}
Rafael J. Wysockic6f37f12009-05-24 22:16:31 +02003680#endif /* CONFIG_HIBERNATION */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682/* It's optimal to keep kswapds on the same CPUs as their memory, but
3683 not required for correctness. So if the last cpu in a node goes
3684 away, we get changed to run anywhere: as the first one comes back,
3685 restore their cpu bindings. */
Sebastian Andrzej Siewior517bbed2016-11-03 15:50:03 +01003686static int kswapd_cpu_online(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687{
Yasunori Goto58c0a4a2007-10-16 01:25:40 -07003688 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689
Sebastian Andrzej Siewior517bbed2016-11-03 15:50:03 +01003690 for_each_node_state(nid, N_MEMORY) {
3691 pg_data_t *pgdat = NODE_DATA(nid);
3692 const struct cpumask *mask;
Rusty Russella70f7302009-03-13 14:49:46 +10303693
Sebastian Andrzej Siewior517bbed2016-11-03 15:50:03 +01003694 mask = cpumask_of_node(pgdat->node_id);
Mike Travisc5f59f02008-04-04 18:11:10 -07003695
Sebastian Andrzej Siewior517bbed2016-11-03 15:50:03 +01003696 if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
3697 /* One of our CPUs online: restore mask */
3698 set_cpus_allowed_ptr(pgdat->kswapd, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 }
Sebastian Andrzej Siewior517bbed2016-11-03 15:50:03 +01003700 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
Yasunori Goto3218ae12006-06-27 02:53:33 -07003703/*
3704 * This kswapd start function will be called by init and node-hot-add.
3705 * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
3706 */
3707int kswapd_run(int nid)
3708{
3709 pg_data_t *pgdat = NODE_DATA(nid);
3710 int ret = 0;
3711
3712 if (pgdat->kswapd)
3713 return 0;
3714
3715 pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
3716 if (IS_ERR(pgdat->kswapd)) {
3717 /* failure at boot is fatal */
Thomas Gleixnerc6202ad2017-05-16 20:42:46 +02003718 BUG_ON(system_state < SYSTEM_RUNNING);
Gavin Shand5dc0ad2012-10-08 16:29:27 -07003719 pr_err("Failed to start kswapd on node %d\n", nid);
3720 ret = PTR_ERR(pgdat->kswapd);
Xishi Qiud72515b2013-04-17 15:58:34 -07003721 pgdat->kswapd = NULL;
Yasunori Goto3218ae12006-06-27 02:53:33 -07003722 }
3723 return ret;
3724}
3725
David Rientjes8fe23e02009-12-14 17:58:33 -08003726/*
Jiang Liud8adde12012-07-11 14:01:52 -07003727 * Called by memory hotplug when all memory in a node is offlined. Caller must
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07003728 * hold mem_hotplug_begin/end().
David Rientjes8fe23e02009-12-14 17:58:33 -08003729 */
3730void kswapd_stop(int nid)
3731{
3732 struct task_struct *kswapd = NODE_DATA(nid)->kswapd;
3733
Jiang Liud8adde12012-07-11 14:01:52 -07003734 if (kswapd) {
David Rientjes8fe23e02009-12-14 17:58:33 -08003735 kthread_stop(kswapd);
Jiang Liud8adde12012-07-11 14:01:52 -07003736 NODE_DATA(nid)->kswapd = NULL;
3737 }
David Rientjes8fe23e02009-12-14 17:58:33 -08003738}
3739
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740static int __init kswapd_init(void)
3741{
Sebastian Andrzej Siewior517bbed2016-11-03 15:50:03 +01003742 int nid, ret;
Andrew Morton69e05942006-03-22 00:08:19 -08003743
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 swap_setup();
Lai Jiangshan48fb2e22012-12-12 13:51:43 -08003745 for_each_node_state(nid, N_MEMORY)
Yasunori Goto3218ae12006-06-27 02:53:33 -07003746 kswapd_run(nid);
Sebastian Andrzej Siewior517bbed2016-11-03 15:50:03 +01003747 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
3748 "mm/vmscan:online", kswapd_cpu_online,
3749 NULL);
3750 WARN_ON(ret < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 return 0;
3752}
3753
3754module_init(kswapd_init)
Christoph Lameter9eeff232006-01-18 17:42:31 -08003755
3756#ifdef CONFIG_NUMA
3757/*
Mel Gormana5f5f912016-07-28 15:46:32 -07003758 * Node reclaim mode
Christoph Lameter9eeff232006-01-18 17:42:31 -08003759 *
Mel Gormana5f5f912016-07-28 15:46:32 -07003760 * If non-zero call node_reclaim when the number of free pages falls below
Christoph Lameter9eeff232006-01-18 17:42:31 -08003761 * the watermarks.
Christoph Lameter9eeff232006-01-18 17:42:31 -08003762 */
Mel Gormana5f5f912016-07-28 15:46:32 -07003763int node_reclaim_mode __read_mostly;
Christoph Lameter9eeff232006-01-18 17:42:31 -08003764
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08003765#define RECLAIM_OFF 0
Fernando Luis Vazquez Cao7d034312008-07-29 22:33:41 -07003766#define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08003767#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
Zhihui Zhang95bbc0c2015-06-24 16:56:42 -07003768#define RECLAIM_UNMAP (1<<2) /* Unmap pages during reclaim */
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08003769
Christoph Lameter9eeff232006-01-18 17:42:31 -08003770/*
Mel Gormana5f5f912016-07-28 15:46:32 -07003771 * Priority for NODE_RECLAIM. This determines the fraction of pages
Christoph Lametera92f7122006-02-01 03:05:32 -08003772 * of a node considered for each zone_reclaim. 4 scans 1/16th of
3773 * a zone.
3774 */
Mel Gormana5f5f912016-07-28 15:46:32 -07003775#define NODE_RECLAIM_PRIORITY 4
Christoph Lametera92f7122006-02-01 03:05:32 -08003776
Christoph Lameter9eeff232006-01-18 17:42:31 -08003777/*
Mel Gormana5f5f912016-07-28 15:46:32 -07003778 * Percentage of pages in a zone that must be unmapped for node_reclaim to
Christoph Lameter96146342006-07-03 00:24:13 -07003779 * occur.
3780 */
3781int sysctl_min_unmapped_ratio = 1;
3782
3783/*
Christoph Lameter0ff38492006-09-25 23:31:52 -07003784 * If the number of slab pages in a zone grows beyond this percentage then
3785 * slab reclaim needs to occur.
3786 */
3787int sysctl_min_slab_ratio = 5;
3788
Mel Gorman11fb9982016-07-28 15:46:20 -07003789static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat)
Mel Gorman90afa5d2009-06-16 15:33:20 -07003790{
Mel Gorman11fb9982016-07-28 15:46:20 -07003791 unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED);
3792 unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) +
3793 node_page_state(pgdat, NR_ACTIVE_FILE);
Mel Gorman90afa5d2009-06-16 15:33:20 -07003794
3795 /*
3796 * It's possible for there to be more file mapped pages than
3797 * accounted for by the pages on the file LRU lists because
3798 * tmpfs pages accounted for as ANON can also be FILE_MAPPED
3799 */
3800 return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0;
3801}
3802
3803/* Work out how many page cache pages we can reclaim in this reclaim_mode */
Mel Gormana5f5f912016-07-28 15:46:32 -07003804static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat)
Mel Gorman90afa5d2009-06-16 15:33:20 -07003805{
Alexandru Moised031a152015-11-05 18:48:08 -08003806 unsigned long nr_pagecache_reclaimable;
3807 unsigned long delta = 0;
Mel Gorman90afa5d2009-06-16 15:33:20 -07003808
3809 /*
Zhihui Zhang95bbc0c2015-06-24 16:56:42 -07003810 * If RECLAIM_UNMAP is set, then all file pages are considered
Mel Gorman90afa5d2009-06-16 15:33:20 -07003811 * potentially reclaimable. Otherwise, we have to worry about
Mel Gorman11fb9982016-07-28 15:46:20 -07003812 * pages like swapcache and node_unmapped_file_pages() provides
Mel Gorman90afa5d2009-06-16 15:33:20 -07003813 * a better estimate
3814 */
Mel Gormana5f5f912016-07-28 15:46:32 -07003815 if (node_reclaim_mode & RECLAIM_UNMAP)
3816 nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES);
Mel Gorman90afa5d2009-06-16 15:33:20 -07003817 else
Mel Gormana5f5f912016-07-28 15:46:32 -07003818 nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat);
Mel Gorman90afa5d2009-06-16 15:33:20 -07003819
3820 /* If we can't clean pages, remove dirty pages from consideration */
Mel Gormana5f5f912016-07-28 15:46:32 -07003821 if (!(node_reclaim_mode & RECLAIM_WRITE))
3822 delta += node_page_state(pgdat, NR_FILE_DIRTY);
Mel Gorman90afa5d2009-06-16 15:33:20 -07003823
3824 /* Watch for any possible underflows due to delta */
3825 if (unlikely(delta > nr_pagecache_reclaimable))
3826 delta = nr_pagecache_reclaimable;
3827
3828 return nr_pagecache_reclaimable - delta;
3829}
3830
Christoph Lameter0ff38492006-09-25 23:31:52 -07003831/*
Mel Gormana5f5f912016-07-28 15:46:32 -07003832 * Try to free up some pages from this node through reclaim.
Christoph Lameter9eeff232006-01-18 17:42:31 -08003833 */
Mel Gormana5f5f912016-07-28 15:46:32 -07003834static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
Christoph Lameter9eeff232006-01-18 17:42:31 -08003835{
Christoph Lameter7fb2d462006-03-22 00:08:22 -08003836 /* Minimum pages needed in order to stay on node */
Andrew Morton69e05942006-03-22 00:08:19 -08003837 const unsigned long nr_pages = 1 << order;
Christoph Lameter9eeff232006-01-18 17:42:31 -08003838 struct task_struct *p = current;
3839 struct reclaim_state reclaim_state;
Vlastimil Babka499118e2017-05-08 15:59:50 -07003840 unsigned int noreclaim_flag;
Andrew Morton179e9632006-03-22 00:08:18 -08003841 struct scan_control sc = {
Andrew Morton62b726c2013-02-22 16:32:24 -08003842 .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
Nick Desaulniersf2f43e52017-07-06 15:36:50 -07003843 .gfp_mask = current_gfp_context(gfp_mask),
Johannes Weinerbd2f6192009-03-31 15:19:38 -07003844 .order = order,
Mel Gormana5f5f912016-07-28 15:46:32 -07003845 .priority = NODE_RECLAIM_PRIORITY,
3846 .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE),
3847 .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP),
Johannes Weineree814fe2014-08-06 16:06:19 -07003848 .may_swap = 1,
Nick Desaulniersf2f43e52017-07-06 15:36:50 -07003849 .reclaim_idx = gfp_zone(gfp_mask),
Andrew Morton179e9632006-03-22 00:08:18 -08003850 };
Christoph Lameter9eeff232006-01-18 17:42:31 -08003851
Christoph Lameter9eeff232006-01-18 17:42:31 -08003852 cond_resched();
Christoph Lameterd4f77962006-02-24 13:04:22 -08003853 /*
Zhihui Zhang95bbc0c2015-06-24 16:56:42 -07003854 * We need to be able to allocate from the reserves for RECLAIM_UNMAP
Christoph Lameterd4f77962006-02-24 13:04:22 -08003855 * and we also need to be able to write out pages for RECLAIM_WRITE
Zhihui Zhang95bbc0c2015-06-24 16:56:42 -07003856 * and RECLAIM_UNMAP.
Christoph Lameterd4f77962006-02-24 13:04:22 -08003857 */
Vlastimil Babka499118e2017-05-08 15:59:50 -07003858 noreclaim_flag = memalloc_noreclaim_save();
3859 p->flags |= PF_SWAPWRITE;
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01003860 fs_reclaim_acquire(sc.gfp_mask);
Christoph Lameter9eeff232006-01-18 17:42:31 -08003861 reclaim_state.reclaimed_slab = 0;
3862 p->reclaim_state = &reclaim_state;
Christoph Lameterc84db23c2006-02-01 03:05:29 -08003863
Mel Gormana5f5f912016-07-28 15:46:32 -07003864 if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages) {
Christoph Lameter0ff38492006-09-25 23:31:52 -07003865 /*
3866 * Free memory by calling shrink zone with increasing
3867 * priorities until we have enough memory freed.
3868 */
Christoph Lameter0ff38492006-09-25 23:31:52 -07003869 do {
Mel Gorman970a39a2016-07-28 15:46:35 -07003870 shrink_node(pgdat, &sc);
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07003871 } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0);
Christoph Lameter0ff38492006-09-25 23:31:52 -07003872 }
Christoph Lameterc84db23c2006-02-01 03:05:29 -08003873
Christoph Lameter9eeff232006-01-18 17:42:31 -08003874 p->reclaim_state = NULL;
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01003875 fs_reclaim_release(gfp_mask);
Vlastimil Babka499118e2017-05-08 15:59:50 -07003876 current->flags &= ~PF_SWAPWRITE;
3877 memalloc_noreclaim_restore(noreclaim_flag);
Rik van Riela79311c2009-01-06 14:40:01 -08003878 return sc.nr_reclaimed >= nr_pages;
Christoph Lameter9eeff232006-01-18 17:42:31 -08003879}
Andrew Morton179e9632006-03-22 00:08:18 -08003880
Mel Gormana5f5f912016-07-28 15:46:32 -07003881int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
Andrew Morton179e9632006-03-22 00:08:18 -08003882{
David Rientjesd773ed62007-10-16 23:26:01 -07003883 int ret;
Andrew Morton179e9632006-03-22 00:08:18 -08003884
3885 /*
Mel Gormana5f5f912016-07-28 15:46:32 -07003886 * Node reclaim reclaims unmapped file backed pages and
Christoph Lameter0ff38492006-09-25 23:31:52 -07003887 * slab pages if we are over the defined limits.
Christoph Lameter34aa1332006-06-30 01:55:37 -07003888 *
Christoph Lameter96146342006-07-03 00:24:13 -07003889 * A small portion of unmapped file backed pages is needed for
3890 * file I/O otherwise pages read by file I/O will be immediately
Mel Gormana5f5f912016-07-28 15:46:32 -07003891 * thrown out if the node is overallocated. So we do not reclaim
3892 * if less than a specified percentage of the node is used by
Christoph Lameter96146342006-07-03 00:24:13 -07003893 * unmapped file backed pages.
Andrew Morton179e9632006-03-22 00:08:18 -08003894 */
Mel Gormana5f5f912016-07-28 15:46:32 -07003895 if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages &&
Johannes Weiner385386c2017-07-06 15:40:43 -07003896 node_page_state(pgdat, NR_SLAB_RECLAIMABLE) <= pgdat->min_slab_pages)
Mel Gormana5f5f912016-07-28 15:46:32 -07003897 return NODE_RECLAIM_FULL;
Andrew Morton179e9632006-03-22 00:08:18 -08003898
3899 /*
David Rientjesd773ed62007-10-16 23:26:01 -07003900 * Do not scan if the allocation should not be delayed.
Andrew Morton179e9632006-03-22 00:08:18 -08003901 */
Mel Gormand0164ad2015-11-06 16:28:21 -08003902 if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC))
Mel Gormana5f5f912016-07-28 15:46:32 -07003903 return NODE_RECLAIM_NOSCAN;
Andrew Morton179e9632006-03-22 00:08:18 -08003904
3905 /*
Mel Gormana5f5f912016-07-28 15:46:32 -07003906 * Only run node reclaim on the local node or on nodes that do not
Andrew Morton179e9632006-03-22 00:08:18 -08003907 * have associated processors. This will favor the local processor
3908 * over remote processors and spread off node memory allocations
3909 * as wide as possible.
3910 */
Mel Gormana5f5f912016-07-28 15:46:32 -07003911 if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id())
3912 return NODE_RECLAIM_NOSCAN;
David Rientjesd773ed62007-10-16 23:26:01 -07003913
Mel Gormana5f5f912016-07-28 15:46:32 -07003914 if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags))
3915 return NODE_RECLAIM_NOSCAN;
Mel Gormanfa5e0842009-06-16 15:33:22 -07003916
Mel Gormana5f5f912016-07-28 15:46:32 -07003917 ret = __node_reclaim(pgdat, gfp_mask, order);
3918 clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags);
David Rientjesd773ed62007-10-16 23:26:01 -07003919
Mel Gorman24cf725182009-06-16 15:33:23 -07003920 if (!ret)
3921 count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED);
3922
David Rientjesd773ed62007-10-16 23:26:01 -07003923 return ret;
Andrew Morton179e9632006-03-22 00:08:18 -08003924}
Christoph Lameter9eeff232006-01-18 17:42:31 -08003925#endif
Lee Schermerhorn894bc312008-10-18 20:26:39 -07003926
Lee Schermerhorn894bc312008-10-18 20:26:39 -07003927/*
3928 * page_evictable - test whether a page is evictable
3929 * @page: the page to test
Lee Schermerhorn894bc312008-10-18 20:26:39 -07003930 *
3931 * Test whether page is evictable--i.e., should be placed on active/inactive
Hugh Dickins39b5f292012-10-08 16:33:18 -07003932 * lists vs unevictable list.
Lee Schermerhorn894bc312008-10-18 20:26:39 -07003933 *
3934 * Reasons page might not be evictable:
Lee Schermerhornba9ddf42008-10-18 20:26:42 -07003935 * (1) page's mapping marked unevictable
Nick Pigginb291f002008-10-18 20:26:44 -07003936 * (2) page is part of an mlocked VMA
Lee Schermerhornba9ddf42008-10-18 20:26:42 -07003937 *
Lee Schermerhorn894bc312008-10-18 20:26:39 -07003938 */
Hugh Dickins39b5f292012-10-08 16:33:18 -07003939int page_evictable(struct page *page)
Lee Schermerhorn894bc312008-10-18 20:26:39 -07003940{
Hugh Dickins39b5f292012-10-08 16:33:18 -07003941 return !mapping_unevictable(page_mapping(page)) && !PageMlocked(page);
Lee Schermerhorn894bc312008-10-18 20:26:39 -07003942}
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003943
Hugh Dickins850465792012-01-20 14:34:19 -08003944#ifdef CONFIG_SHMEM
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003945/**
Hugh Dickins24513262012-01-20 14:34:21 -08003946 * check_move_unevictable_pages - check pages for evictability and move to appropriate zone lru list
3947 * @pages: array of pages to check
3948 * @nr_pages: number of pages to check
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003949 *
Hugh Dickins24513262012-01-20 14:34:21 -08003950 * Checks pages for evictability and moves them to the appropriate lru list.
Hugh Dickins850465792012-01-20 14:34:19 -08003951 *
3952 * This function is only used for SysV IPC SHM_UNLOCK.
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003953 */
Hugh Dickins24513262012-01-20 14:34:21 -08003954void check_move_unevictable_pages(struct page **pages, int nr_pages)
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003955{
Johannes Weiner925b7672012-01-12 17:18:15 -08003956 struct lruvec *lruvec;
Mel Gorman785b99f2016-07-28 15:47:23 -07003957 struct pglist_data *pgdat = NULL;
Hugh Dickins24513262012-01-20 14:34:21 -08003958 int pgscanned = 0;
3959 int pgrescued = 0;
3960 int i;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003961
Hugh Dickins24513262012-01-20 14:34:21 -08003962 for (i = 0; i < nr_pages; i++) {
3963 struct page *page = pages[i];
Mel Gorman785b99f2016-07-28 15:47:23 -07003964 struct pglist_data *pagepgdat = page_pgdat(page);
Lee Schermerhornaf936a12008-10-18 20:26:53 -07003965
Hugh Dickins24513262012-01-20 14:34:21 -08003966 pgscanned++;
Mel Gorman785b99f2016-07-28 15:47:23 -07003967 if (pagepgdat != pgdat) {
3968 if (pgdat)
3969 spin_unlock_irq(&pgdat->lru_lock);
3970 pgdat = pagepgdat;
3971 spin_lock_irq(&pgdat->lru_lock);
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003972 }
Mel Gorman785b99f2016-07-28 15:47:23 -07003973 lruvec = mem_cgroup_page_lruvec(page, pgdat);
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003974
Hugh Dickins24513262012-01-20 14:34:21 -08003975 if (!PageLRU(page) || !PageUnevictable(page))
3976 continue;
3977
Hugh Dickins39b5f292012-10-08 16:33:18 -07003978 if (page_evictable(page)) {
Hugh Dickins24513262012-01-20 14:34:21 -08003979 enum lru_list lru = page_lru_base_type(page);
3980
Sasha Levin309381fea2014-01-23 15:52:54 -08003981 VM_BUG_ON_PAGE(PageActive(page), page);
Hugh Dickins24513262012-01-20 14:34:21 -08003982 ClearPageUnevictable(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07003983 del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE);
3984 add_page_to_lru_list(page, lruvec, lru);
Hugh Dickins24513262012-01-20 14:34:21 -08003985 pgrescued++;
3986 }
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003987 }
Hugh Dickins24513262012-01-20 14:34:21 -08003988
Mel Gorman785b99f2016-07-28 15:47:23 -07003989 if (pgdat) {
Hugh Dickins24513262012-01-20 14:34:21 -08003990 __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued);
3991 __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned);
Mel Gorman785b99f2016-07-28 15:47:23 -07003992 spin_unlock_irq(&pgdat->lru_lock);
Hugh Dickins24513262012-01-20 14:34:21 -08003993 }
Hugh Dickins850465792012-01-20 14:34:19 -08003994}
3995#endif /* CONFIG_SHMEM */