blob: d3fba55a0028c638613190c76a57e4e1b8e71c6e [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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
4 *
5 * Swap reorganised 29.12.95, Stephen Tweedie.
6 * kswapd added: 7.1.96 sct
7 * Removed kswapd_ctl limits, and swap out as many pages as needed
8 * to bring the system back to freepages.high: 2.4.97, Rik van Riel.
9 * Zone aware kswapd started 02/00, Kanoj Sarcar (kanoj@sgi.com).
10 * Multiqueue VM started 5.8.00, Rik van Riel.
11 */
12
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -070013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/mm.h>
Ingo Molnar5b3cc152017-02-02 20:43:54 +010016#include <linux/sched/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kernel_stat.h>
20#include <linux/swap.h>
21#include <linux/pagemap.h>
22#include <linux/init.h>
23#include <linux/highmem.h>
Anton Vorontsov70ddf632013-04-29 15:08:31 -070024#include <linux/vmpressure.h>
Andrew Mortone129b5c2006-09-27 01:50:00 -070025#include <linux/vmstat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/file.h>
27#include <linux/writeback.h>
28#include <linux/blkdev.h>
29#include <linux/buffer_head.h> /* for try_to_release_page(),
30 buffer_heads_over_limit */
31#include <linux/mm_inline.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/backing-dev.h>
33#include <linux/rmap.h>
34#include <linux/topology.h>
35#include <linux/cpu.h>
36#include <linux/cpuset.h>
Mel Gorman3e7d3442011-01-13 15:45:56 -080037#include <linux/compaction.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/notifier.h>
39#include <linux/rwsem.h>
Rafael J. Wysocki248a0302006-03-22 00:09:04 -080040#include <linux/delay.h>
Yasunori Goto3218ae12006-06-27 02:53:33 -070041#include <linux/kthread.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080042#include <linux/freezer.h>
Balbir Singh66e17072008-02-07 00:13:56 -080043#include <linux/memcontrol.h>
Keika Kobayashi873b4772008-07-25 01:48:52 -070044#include <linux/delayacct.h>
Lee Schermerhornaf936a12008-10-18 20:26:53 -070045#include <linux/sysctl.h>
KOSAKI Motohiro929bea72011-04-14 15:22:12 -070046#include <linux/oom.h>
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +000047#include <linux/pagevec.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070048#include <linux/prefetch.h>
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -070049#include <linux/printk.h>
Ross Zwislerf9fe48b2016-01-22 15:10:40 -080050#include <linux/dax.h>
Johannes Weinereb414682018-10-26 15:06:27 -070051#include <linux/psi.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
Johannes Weineree814fe2014-08-06 16:06:19 -070068 /*
69 * Nodemask of nodes allowed by the caller. If NULL, all nodes
70 * are scanned.
71 */
72 nodemask_t *nodemask;
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -070073
KOSAKI Motohiro5f53e762010-05-24 14:32:37 -070074 /*
Johannes Weinerf16015f2012-01-12 17:17:52 -080075 * The memory cgroup that hit its limit and as a result is the
76 * primary target of this reclaim invocation.
77 */
78 struct mem_cgroup *target_mem_cgroup;
Balbir Singh66e17072008-02-07 00:13:56 -080079
Johannes Weiner7cf111b2020-06-03 16:03:06 -070080 /*
81 * Scan pressure balancing between anon and file LRUs
82 */
83 unsigned long anon_cost;
84 unsigned long file_cost;
85
Johannes Weinerb91ac372019-11-30 17:56:02 -080086 /* Can active pages be deactivated as part of reclaim? */
87#define DEACTIVATE_ANON 1
88#define DEACTIVATE_FILE 2
89 unsigned int may_deactivate:2;
90 unsigned int force_deactivate:1;
91 unsigned int skipped_deactivate:1;
92
Johannes Weiner1276ad62017-02-24 14:56:11 -080093 /* Writepage batching in laptop mode; RECLAIM_WRITE */
Johannes Weineree814fe2014-08-06 16:06:19 -070094 unsigned int may_writepage:1;
95
96 /* Can mapped pages be reclaimed? */
97 unsigned int may_unmap:1;
98
99 /* Can pages be swapped as part of reclaim? */
100 unsigned int may_swap:1;
101
Yisheng Xied6622f62017-05-03 14:53:57 -0700102 /*
103 * Cgroups are not reclaimed below their configured memory.low,
104 * unless we threaten to OOM. If any cgroups are skipped due to
105 * memory.low and nothing was reclaimed, go back for memory.low.
106 */
107 unsigned int memcg_low_reclaim:1;
108 unsigned int memcg_low_skipped:1;
Johannes Weiner241994ed2015-02-11 15:26:06 -0800109
Johannes Weineree814fe2014-08-06 16:06:19 -0700110 unsigned int hibernation_mode:1;
111
112 /* One of the zones is ready for compaction */
113 unsigned int compaction_ready:1;
114
Johannes Weinerb91ac372019-11-30 17:56:02 -0800115 /* There is easily reclaimable cold cache in the current node */
116 unsigned int cache_trim_mode:1;
117
Johannes Weiner53138ce2019-11-30 17:55:56 -0800118 /* The file pages on the current node are dangerously low */
119 unsigned int file_is_tiny:1;
120
Greg Thelenbb451fd2018-08-17 15:45:19 -0700121 /* Allocation order */
122 s8 order;
123
124 /* Scan (total_size >> priority) pages at once */
125 s8 priority;
126
127 /* The highest zone to isolate pages for reclaim from */
128 s8 reclaim_idx;
129
130 /* This context's GFP mask */
131 gfp_t gfp_mask;
132
Johannes Weineree814fe2014-08-06 16:06:19 -0700133 /* Incremented by the number of inactive pages that were scanned */
134 unsigned long nr_scanned;
135
136 /* Number of pages freed so far during a call to shrink_zones() */
137 unsigned long nr_reclaimed;
Andrey Ryabinind108c772018-04-10 16:27:59 -0700138
139 struct {
140 unsigned int dirty;
141 unsigned int unqueued_dirty;
142 unsigned int congested;
143 unsigned int writeback;
144 unsigned int immediate;
145 unsigned int file_taken;
146 unsigned int taken;
147 } nr;
Yafang Shaoe5ca8072019-07-16 16:26:09 -0700148
149 /* for recording the reclaimed slab by now */
150 struct reclaim_state reclaim_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#ifdef ARCH_HAS_PREFETCHW
154#define prefetchw_prev_lru_page(_page, _base, _field) \
155 do { \
156 if ((_page)->lru.prev != _base) { \
157 struct page *prev; \
158 \
159 prev = lru_to_page(&(_page->lru)); \
160 prefetchw(&prev->_field); \
161 } \
162 } while (0)
163#else
164#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
165#endif
166
167/*
Johannes Weinerc8439662020-06-03 16:02:37 -0700168 * From 0 .. 200. Higher means more swappy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 */
170int vm_swappiness = 60;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Yang Shi0a432dc2019-09-23 15:38:12 -0700172static void set_task_reclaim_state(struct task_struct *task,
173 struct reclaim_state *rs)
174{
175 /* Check for an overwrite */
176 WARN_ON_ONCE(rs && task->reclaim_state);
177
178 /* Check for the nulling of an already-nulled member */
179 WARN_ON_ONCE(!rs && !task->reclaim_state);
180
181 task->reclaim_state = rs;
182}
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184static LIST_HEAD(shrinker_list);
185static DECLARE_RWSEM(shrinker_rwsem);
186
Yang Shi0a432dc2019-09-23 15:38:12 -0700187#ifdef CONFIG_MEMCG
Yang Shi2bfd3632021-05-04 18:36:11 -0700188
189static int memcg_shrinker_map_size;
Yang Shi2bfd3632021-05-04 18:36:11 -0700190
191static void free_shrinker_map_rcu(struct rcu_head *head)
192{
193 kvfree(container_of(head, struct memcg_shrinker_map, rcu));
194}
195
196static int expand_one_shrinker_map(struct mem_cgroup *memcg,
197 int size, int old_size)
198{
199 struct memcg_shrinker_map *new, *old;
200 struct mem_cgroup_per_node *pn;
201 int nid;
202
Yang Shi2bfd3632021-05-04 18:36:11 -0700203 for_each_node(nid) {
204 pn = memcg->nodeinfo[nid];
205 old = rcu_dereference_protected(pn->shrinker_map, true);
206 /* Not yet online memcg */
207 if (!old)
208 return 0;
209
210 new = kvmalloc_node(sizeof(*new) + size, GFP_KERNEL, nid);
211 if (!new)
212 return -ENOMEM;
213
214 /* Set all old bits, clear all new bits */
215 memset(new->map, (int)0xff, old_size);
216 memset((void *)new->map + old_size, 0, size - old_size);
217
218 rcu_assign_pointer(pn->shrinker_map, new);
219 call_rcu(&old->rcu, free_shrinker_map_rcu);
220 }
221
222 return 0;
223}
224
225void free_shrinker_maps(struct mem_cgroup *memcg)
226{
227 struct mem_cgroup_per_node *pn;
228 struct memcg_shrinker_map *map;
229 int nid;
230
231 if (mem_cgroup_is_root(memcg))
232 return;
233
234 for_each_node(nid) {
235 pn = memcg->nodeinfo[nid];
236 map = rcu_dereference_protected(pn->shrinker_map, true);
237 kvfree(map);
238 rcu_assign_pointer(pn->shrinker_map, NULL);
239 }
240}
241
242int alloc_shrinker_maps(struct mem_cgroup *memcg)
243{
244 struct memcg_shrinker_map *map;
245 int nid, size, ret = 0;
246
247 if (mem_cgroup_is_root(memcg))
248 return 0;
249
Yang Shid27cf2a2021-05-04 18:36:14 -0700250 down_write(&shrinker_rwsem);
Yang Shi2bfd3632021-05-04 18:36:11 -0700251 size = memcg_shrinker_map_size;
252 for_each_node(nid) {
253 map = kvzalloc_node(sizeof(*map) + size, GFP_KERNEL, nid);
254 if (!map) {
255 free_shrinker_maps(memcg);
256 ret = -ENOMEM;
257 break;
258 }
259 rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_map, map);
260 }
Yang Shid27cf2a2021-05-04 18:36:14 -0700261 up_write(&shrinker_rwsem);
Yang Shi2bfd3632021-05-04 18:36:11 -0700262
263 return ret;
264}
265
266static int expand_shrinker_maps(int new_id)
267{
268 int size, old_size, ret = 0;
269 struct mem_cgroup *memcg;
270
271 size = DIV_ROUND_UP(new_id + 1, BITS_PER_LONG) * sizeof(unsigned long);
272 old_size = memcg_shrinker_map_size;
273 if (size <= old_size)
274 return 0;
275
Yang Shi2bfd3632021-05-04 18:36:11 -0700276 if (!root_mem_cgroup)
Yang Shid27cf2a2021-05-04 18:36:14 -0700277 goto out;
278
279 lockdep_assert_held(&shrinker_rwsem);
Yang Shi2bfd3632021-05-04 18:36:11 -0700280
281 memcg = mem_cgroup_iter(NULL, NULL, NULL);
282 do {
283 if (mem_cgroup_is_root(memcg))
284 continue;
285 ret = expand_one_shrinker_map(memcg, size, old_size);
286 if (ret) {
287 mem_cgroup_iter_break(NULL, memcg);
Yang Shid27cf2a2021-05-04 18:36:14 -0700288 goto out;
Yang Shi2bfd3632021-05-04 18:36:11 -0700289 }
290 } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL);
Yang Shid27cf2a2021-05-04 18:36:14 -0700291out:
Yang Shi2bfd3632021-05-04 18:36:11 -0700292 if (!ret)
293 memcg_shrinker_map_size = size;
Yang Shid27cf2a2021-05-04 18:36:14 -0700294
Yang Shi2bfd3632021-05-04 18:36:11 -0700295 return ret;
296}
297
298void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id)
299{
300 if (shrinker_id >= 0 && memcg && !mem_cgroup_is_root(memcg)) {
301 struct memcg_shrinker_map *map;
302
303 rcu_read_lock();
304 map = rcu_dereference(memcg->nodeinfo[nid]->shrinker_map);
305 /* Pairs with smp mb in shrink_slab() */
306 smp_mb__before_atomic();
307 set_bit(shrinker_id, map->map);
308 rcu_read_unlock();
309 }
310}
311
Kirill Tkhai7e010df2018-08-17 15:48:34 -0700312/*
313 * We allow subsystems to populate their shrinker-related
314 * LRU lists before register_shrinker_prepared() is called
315 * for the shrinker, since we don't want to impose
316 * restrictions on their internal registration order.
317 * In this case shrink_slab_memcg() may find corresponding
318 * bit is set in the shrinkers map.
319 *
320 * This value is used by the function to detect registering
321 * shrinkers and to skip do_shrink_slab() calls for them.
322 */
323#define SHRINKER_REGISTERING ((struct shrinker *)~0UL)
324
Kirill Tkhaib4c2b232018-08-17 15:47:29 -0700325static DEFINE_IDR(shrinker_idr);
326static int shrinker_nr_max;
327
328static int prealloc_memcg_shrinker(struct shrinker *shrinker)
329{
330 int id, ret = -ENOMEM;
331
332 down_write(&shrinker_rwsem);
333 /* This may call shrinker, so it must use down_read_trylock() */
Kirill Tkhai7e010df2018-08-17 15:48:34 -0700334 id = idr_alloc(&shrinker_idr, SHRINKER_REGISTERING, 0, 0, GFP_KERNEL);
Kirill Tkhaib4c2b232018-08-17 15:47:29 -0700335 if (id < 0)
336 goto unlock;
337
Kirill Tkhai0a4465d2018-08-17 15:47:37 -0700338 if (id >= shrinker_nr_max) {
Yang Shi2bfd3632021-05-04 18:36:11 -0700339 if (expand_shrinker_maps(id)) {
Kirill Tkhai0a4465d2018-08-17 15:47:37 -0700340 idr_remove(&shrinker_idr, id);
341 goto unlock;
342 }
343
Kirill Tkhaib4c2b232018-08-17 15:47:29 -0700344 shrinker_nr_max = id + 1;
Kirill Tkhai0a4465d2018-08-17 15:47:37 -0700345 }
Kirill Tkhaib4c2b232018-08-17 15:47:29 -0700346 shrinker->id = id;
347 ret = 0;
348unlock:
349 up_write(&shrinker_rwsem);
350 return ret;
351}
352
353static void unregister_memcg_shrinker(struct shrinker *shrinker)
354{
355 int id = shrinker->id;
356
357 BUG_ON(id < 0);
358
359 down_write(&shrinker_rwsem);
360 idr_remove(&shrinker_idr, id);
361 up_write(&shrinker_rwsem);
362}
Kirill Tkhaib4c2b232018-08-17 15:47:29 -0700363
Johannes Weinerb5ead352019-11-30 17:55:40 -0800364static bool cgroup_reclaim(struct scan_control *sc)
Johannes Weiner89b5fae2012-01-12 17:17:50 -0800365{
Johannes Weinerb5ead352019-11-30 17:55:40 -0800366 return sc->target_mem_cgroup;
Johannes Weiner89b5fae2012-01-12 17:17:50 -0800367}
Tejun Heo97c93412015-05-22 18:23:36 -0400368
369/**
Johannes Weinerb5ead352019-11-30 17:55:40 -0800370 * writeback_throttling_sane - is the usual dirty throttling mechanism available?
Tejun Heo97c93412015-05-22 18:23:36 -0400371 * @sc: scan_control in question
372 *
373 * The normal page dirty throttling mechanism in balance_dirty_pages() is
374 * completely broken with the legacy memcg and direct stalling in
375 * shrink_page_list() is used for throttling instead, which lacks all the
376 * niceties such as fairness, adaptive pausing, bandwidth proportional
377 * allocation and configurability.
378 *
379 * This function tests whether the vmscan currently in progress can assume
380 * that the normal dirty throttling mechanism is operational.
381 */
Johannes Weinerb5ead352019-11-30 17:55:40 -0800382static bool writeback_throttling_sane(struct scan_control *sc)
Tejun Heo97c93412015-05-22 18:23:36 -0400383{
Johannes Weinerb5ead352019-11-30 17:55:40 -0800384 if (!cgroup_reclaim(sc))
Tejun Heo97c93412015-05-22 18:23:36 -0400385 return true;
386#ifdef CONFIG_CGROUP_WRITEBACK
Linus Torvalds69234ac2015-11-05 14:51:32 -0800387 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
Tejun Heo97c93412015-05-22 18:23:36 -0400388 return true;
389#endif
390 return false;
391}
KAMEZAWA Hiroyuki91a45472008-02-07 00:14:29 -0800392#else
Yang Shi0a432dc2019-09-23 15:38:12 -0700393static int prealloc_memcg_shrinker(struct shrinker *shrinker)
394{
395 return 0;
396}
397
398static void unregister_memcg_shrinker(struct shrinker *shrinker)
399{
400}
401
Johannes Weinerb5ead352019-11-30 17:55:40 -0800402static bool cgroup_reclaim(struct scan_control *sc)
Johannes Weiner89b5fae2012-01-12 17:17:50 -0800403{
Johannes Weinerb5ead352019-11-30 17:55:40 -0800404 return false;
Johannes Weiner89b5fae2012-01-12 17:17:50 -0800405}
Tejun Heo97c93412015-05-22 18:23:36 -0400406
Johannes Weinerb5ead352019-11-30 17:55:40 -0800407static bool writeback_throttling_sane(struct scan_control *sc)
Tejun Heo97c93412015-05-22 18:23:36 -0400408{
409 return true;
410}
KAMEZAWA Hiroyuki91a45472008-02-07 00:14:29 -0800411#endif
412
Mel Gorman5a1c84b2016-07-28 15:47:31 -0700413/*
414 * This misses isolated pages which are not accounted for to save counters.
415 * As the data only determines if reclaim or compaction continues, it is
416 * not expected that isolated pages will be a dominating factor.
417 */
418unsigned long zone_reclaimable_pages(struct zone *zone)
419{
420 unsigned long nr;
421
422 nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) +
423 zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE);
424 if (get_nr_swap_pages() > 0)
425 nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) +
426 zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON);
427
428 return nr;
429}
430
Michal Hockofd538802017-02-22 15:45:58 -0800431/**
432 * lruvec_lru_size - Returns the number of pages on the given LRU list.
433 * @lruvec: lru vector
434 * @lru: lru to use
435 * @zone_idx: zones to consider (use MAX_NR_ZONES for the whole LRU list)
436 */
Yu Zhao20913392021-02-24 12:08:44 -0800437static unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru,
438 int zone_idx)
KOSAKI Motohiroc9f299d2009-01-07 18:08:16 -0800439{
Johannes Weinerde3b0152019-11-30 17:55:31 -0800440 unsigned long size = 0;
Michal Hockofd538802017-02-22 15:45:58 -0800441 int zid;
442
Johannes Weinerde3b0152019-11-30 17:55:31 -0800443 for (zid = 0; zid <= zone_idx && zid < MAX_NR_ZONES; zid++) {
Michal Hockofd538802017-02-22 15:45:58 -0800444 struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid];
KOSAKI Motohiroc9f299d2009-01-07 18:08:16 -0800445
Michal Hockofd538802017-02-22 15:45:58 -0800446 if (!managed_zone(zone))
447 continue;
Michal Hockob4536f0c82017-01-10 16:58:04 -0800448
Michal Hockofd538802017-02-22 15:45:58 -0800449 if (!mem_cgroup_disabled())
Johannes Weinerde3b0152019-11-30 17:55:31 -0800450 size += mem_cgroup_get_zone_lru_size(lruvec, lru, zid);
Michal Hockofd538802017-02-22 15:45:58 -0800451 else
Johannes Weinerde3b0152019-11-30 17:55:31 -0800452 size += zone_page_state(zone, NR_ZONE_LRU_BASE + lru);
Michal Hockofd538802017-02-22 15:45:58 -0800453 }
Johannes Weinerde3b0152019-11-30 17:55:31 -0800454 return size;
Michal Hockob4536f0c82017-01-10 16:58:04 -0800455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/*
Glauber Costa1d3d4432013-08-28 10:18:04 +1000458 * Add a shrinker callback to be called from the vm.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 */
Tetsuo Handa8e049442018-04-04 19:53:07 +0900460int prealloc_shrinker(struct shrinker *shrinker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Alexey Dobriyanb9726c22019-03-05 15:48:26 -0800462 unsigned int size = sizeof(*shrinker->nr_deferred);
Glauber Costa1d3d4432013-08-28 10:18:04 +1000463
Glauber Costa1d3d4432013-08-28 10:18:04 +1000464 if (shrinker->flags & SHRINKER_NUMA_AWARE)
465 size *= nr_node_ids;
466
467 shrinker->nr_deferred = kzalloc(size, GFP_KERNEL);
468 if (!shrinker->nr_deferred)
469 return -ENOMEM;
Kirill Tkhaib4c2b232018-08-17 15:47:29 -0700470
471 if (shrinker->flags & SHRINKER_MEMCG_AWARE) {
472 if (prealloc_memcg_shrinker(shrinker))
473 goto free_deferred;
474 }
475
Tetsuo Handa8e049442018-04-04 19:53:07 +0900476 return 0;
Kirill Tkhaib4c2b232018-08-17 15:47:29 -0700477
478free_deferred:
479 kfree(shrinker->nr_deferred);
480 shrinker->nr_deferred = NULL;
481 return -ENOMEM;
Tetsuo Handa8e049442018-04-04 19:53:07 +0900482}
Glauber Costa1d3d4432013-08-28 10:18:04 +1000483
Tetsuo Handa8e049442018-04-04 19:53:07 +0900484void free_prealloced_shrinker(struct shrinker *shrinker)
485{
Kirill Tkhaib4c2b232018-08-17 15:47:29 -0700486 if (!shrinker->nr_deferred)
487 return;
488
489 if (shrinker->flags & SHRINKER_MEMCG_AWARE)
490 unregister_memcg_shrinker(shrinker);
491
Tetsuo Handa8e049442018-04-04 19:53:07 +0900492 kfree(shrinker->nr_deferred);
493 shrinker->nr_deferred = NULL;
494}
495
496void register_shrinker_prepared(struct shrinker *shrinker)
497{
Rusty Russell8e1f9362007-07-17 04:03:17 -0700498 down_write(&shrinker_rwsem);
499 list_add_tail(&shrinker->list, &shrinker_list);
Yang Shi42a9a532019-12-17 20:51:52 -0800500#ifdef CONFIG_MEMCG
Kirill Tkhai8df4a442018-08-21 21:51:49 -0700501 if (shrinker->flags & SHRINKER_MEMCG_AWARE)
502 idr_replace(&shrinker_idr, shrinker, shrinker->id);
Kirill Tkhai7e010df2018-08-17 15:48:34 -0700503#endif
Rusty Russell8e1f9362007-07-17 04:03:17 -0700504 up_write(&shrinker_rwsem);
Tetsuo Handa8e049442018-04-04 19:53:07 +0900505}
506
507int register_shrinker(struct shrinker *shrinker)
508{
509 int err = prealloc_shrinker(shrinker);
510
511 if (err)
512 return err;
513 register_shrinker_prepared(shrinker);
Glauber Costa1d3d4432013-08-28 10:18:04 +1000514 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
Rusty Russell8e1f9362007-07-17 04:03:17 -0700516EXPORT_SYMBOL(register_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518/*
519 * Remove one
520 */
Rusty Russell8e1f9362007-07-17 04:03:17 -0700521void unregister_shrinker(struct shrinker *shrinker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Tetsuo Handabb422a72017-12-18 20:31:41 +0900523 if (!shrinker->nr_deferred)
524 return;
Kirill Tkhaib4c2b232018-08-17 15:47:29 -0700525 if (shrinker->flags & SHRINKER_MEMCG_AWARE)
526 unregister_memcg_shrinker(shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 down_write(&shrinker_rwsem);
528 list_del(&shrinker->list);
529 up_write(&shrinker_rwsem);
Andrew Vaginae393322013-10-16 13:46:46 -0700530 kfree(shrinker->nr_deferred);
Tetsuo Handabb422a72017-12-18 20:31:41 +0900531 shrinker->nr_deferred = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
Rusty Russell8e1f9362007-07-17 04:03:17 -0700533EXPORT_SYMBOL(unregister_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535#define SHRINK_BATCH 128
Glauber Costa1d3d4432013-08-28 10:18:04 +1000536
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800537static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,
Josef Bacik9092c712018-01-31 16:16:26 -0800538 struct shrinker *shrinker, int priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Glauber Costa1d3d4432013-08-28 10:18:04 +1000540 unsigned long freed = 0;
541 unsigned long long delta;
542 long total_scan;
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700543 long freeable;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000544 long nr;
545 long new_nr;
546 int nid = shrinkctl->nid;
547 long batch_size = shrinker->batch ? shrinker->batch
548 : SHRINK_BATCH;
Shaohua Li5f33a082016-12-12 16:41:50 -0800549 long scanned = 0, next_deferred;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000550
Kirill Tkhaiac7fb3a2018-08-17 15:48:30 -0700551 if (!(shrinker->flags & SHRINKER_NUMA_AWARE))
552 nid = 0;
553
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700554 freeable = shrinker->count_objects(shrinker, shrinkctl);
Kirill Tkhai9b996462018-08-17 15:48:21 -0700555 if (freeable == 0 || freeable == SHRINK_EMPTY)
556 return freeable;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000557
558 /*
559 * copy the current shrinker scan count into a local variable
560 * and zero it so that other concurrent shrinker invocations
561 * don't also do this scanning work.
562 */
563 nr = atomic_long_xchg(&shrinker->nr_deferred[nid], 0);
564
565 total_scan = nr;
Johannes Weiner4b85afb2018-10-26 15:06:42 -0700566 if (shrinker->seeks) {
567 delta = freeable >> priority;
568 delta *= 4;
569 do_div(delta, shrinker->seeks);
570 } else {
571 /*
572 * These objects don't require any IO to create. Trim
573 * them aggressively under memory pressure to keep
574 * them from causing refetches in the IO caches.
575 */
576 delta = freeable / 2;
577 }
Roman Gushchin172b06c32018-09-20 12:22:46 -0700578
Glauber Costa1d3d4432013-08-28 10:18:04 +1000579 total_scan += delta;
580 if (total_scan < 0) {
Sakari Ailusd75f7732019-03-25 21:32:28 +0200581 pr_err("shrink_slab: %pS negative objects to delete nr=%ld\n",
Dave Chinnera0b02132013-08-28 10:18:16 +1000582 shrinker->scan_objects, total_scan);
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700583 total_scan = freeable;
Shaohua Li5f33a082016-12-12 16:41:50 -0800584 next_deferred = nr;
585 } else
586 next_deferred = total_scan;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000587
588 /*
589 * We need to avoid excessive windup on filesystem shrinkers
590 * due to large numbers of GFP_NOFS allocations causing the
591 * shrinkers to return -1 all the time. This results in a large
592 * nr being built up so when a shrink that can do some work
593 * comes along it empties the entire cache due to nr >>>
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700594 * freeable. This is bad for sustaining a working set in
Glauber Costa1d3d4432013-08-28 10:18:04 +1000595 * memory.
596 *
597 * Hence only allow the shrinker to scan the entire cache when
598 * a large delta change is calculated directly.
599 */
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700600 if (delta < freeable / 4)
601 total_scan = min(total_scan, freeable / 2);
Glauber Costa1d3d4432013-08-28 10:18:04 +1000602
603 /*
604 * Avoid risking looping forever due to too large nr value:
605 * never try to free more than twice the estimate number of
606 * freeable entries.
607 */
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700608 if (total_scan > freeable * 2)
609 total_scan = freeable * 2;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000610
611 trace_mm_shrink_slab_start(shrinker, shrinkctl, nr,
Josef Bacik9092c712018-01-31 16:16:26 -0800612 freeable, delta, total_scan, priority);
Glauber Costa1d3d4432013-08-28 10:18:04 +1000613
Vladimir Davydov0b1fb402014-01-23 15:53:22 -0800614 /*
615 * Normally, we should not scan less than batch_size objects in one
616 * pass to avoid too frequent shrinker calls, but if the slab has less
617 * than batch_size objects in total and we are really tight on memory,
618 * we will try to reclaim all available objects, otherwise we can end
619 * up failing allocations although there are plenty of reclaimable
620 * objects spread over several slabs with usage less than the
621 * batch_size.
622 *
623 * We detect the "tight on memory" situations by looking at the total
624 * number of objects we want to scan (total_scan). If it is greater
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700625 * than the total number of objects on slab (freeable), we must be
Vladimir Davydov0b1fb402014-01-23 15:53:22 -0800626 * scanning at high prio and therefore should try to reclaim as much as
627 * possible.
628 */
629 while (total_scan >= batch_size ||
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700630 total_scan >= freeable) {
Dave Chinnera0b02132013-08-28 10:18:16 +1000631 unsigned long ret;
Vladimir Davydov0b1fb402014-01-23 15:53:22 -0800632 unsigned long nr_to_scan = min(batch_size, total_scan);
Glauber Costa1d3d4432013-08-28 10:18:04 +1000633
Vladimir Davydov0b1fb402014-01-23 15:53:22 -0800634 shrinkctl->nr_to_scan = nr_to_scan;
Chris Wilsond460acb2017-09-06 16:19:26 -0700635 shrinkctl->nr_scanned = nr_to_scan;
Dave Chinnera0b02132013-08-28 10:18:16 +1000636 ret = shrinker->scan_objects(shrinker, shrinkctl);
637 if (ret == SHRINK_STOP)
638 break;
639 freed += ret;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000640
Chris Wilsond460acb2017-09-06 16:19:26 -0700641 count_vm_events(SLABS_SCANNED, shrinkctl->nr_scanned);
642 total_scan -= shrinkctl->nr_scanned;
643 scanned += shrinkctl->nr_scanned;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000644
645 cond_resched();
646 }
647
Shaohua Li5f33a082016-12-12 16:41:50 -0800648 if (next_deferred >= scanned)
649 next_deferred -= scanned;
650 else
651 next_deferred = 0;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000652 /*
653 * move the unused scan count back into the shrinker in a
654 * manner that handles concurrent updates. If we exhausted the
655 * scan, there is no need to do an update.
656 */
Shaohua Li5f33a082016-12-12 16:41:50 -0800657 if (next_deferred > 0)
658 new_nr = atomic_long_add_return(next_deferred,
Glauber Costa1d3d4432013-08-28 10:18:04 +1000659 &shrinker->nr_deferred[nid]);
660 else
661 new_nr = atomic_long_read(&shrinker->nr_deferred[nid]);
662
Yang Shi8efb4b52021-05-04 18:36:08 -0700663 trace_mm_shrink_slab_end(shrinker, shrinkctl->nid, freed, nr, new_nr, total_scan);
Glauber Costa1d3d4432013-08-28 10:18:04 +1000664 return freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
Yang Shi0a432dc2019-09-23 15:38:12 -0700667#ifdef CONFIG_MEMCG
Kirill Tkhaib0dedc42018-08-17 15:48:14 -0700668static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,
669 struct mem_cgroup *memcg, int priority)
670{
671 struct memcg_shrinker_map *map;
Kirill Tkhaib8e57ef2018-10-05 15:52:10 -0700672 unsigned long ret, freed = 0;
673 int i;
Kirill Tkhaib0dedc42018-08-17 15:48:14 -0700674
Yang Shi0a432dc2019-09-23 15:38:12 -0700675 if (!mem_cgroup_online(memcg))
Kirill Tkhaib0dedc42018-08-17 15:48:14 -0700676 return 0;
677
678 if (!down_read_trylock(&shrinker_rwsem))
679 return 0;
680
681 map = rcu_dereference_protected(memcg->nodeinfo[nid]->shrinker_map,
682 true);
683 if (unlikely(!map))
684 goto unlock;
685
686 for_each_set_bit(i, map->map, shrinker_nr_max) {
687 struct shrink_control sc = {
688 .gfp_mask = gfp_mask,
689 .nid = nid,
690 .memcg = memcg,
691 };
692 struct shrinker *shrinker;
693
694 shrinker = idr_find(&shrinker_idr, i);
Kirill Tkhai7e010df2018-08-17 15:48:34 -0700695 if (unlikely(!shrinker || shrinker == SHRINKER_REGISTERING)) {
696 if (!shrinker)
697 clear_bit(i, map->map);
Kirill Tkhaib0dedc42018-08-17 15:48:14 -0700698 continue;
699 }
700
Yang Shi0a432dc2019-09-23 15:38:12 -0700701 /* Call non-slab shrinkers even though kmem is disabled */
702 if (!memcg_kmem_enabled() &&
703 !(shrinker->flags & SHRINKER_NONSLAB))
704 continue;
705
Kirill Tkhaib0dedc42018-08-17 15:48:14 -0700706 ret = do_shrink_slab(&sc, shrinker, priority);
Kirill Tkhaif90280d2018-08-17 15:48:25 -0700707 if (ret == SHRINK_EMPTY) {
708 clear_bit(i, map->map);
709 /*
710 * After the shrinker reported that it had no objects to
711 * free, but before we cleared the corresponding bit in
712 * the memcg shrinker map, a new object might have been
713 * added. To make sure, we have the bit set in this
714 * case, we invoke the shrinker one more time and reset
715 * the bit if it reports that it is not empty anymore.
716 * The memory barrier here pairs with the barrier in
Yang Shi2bfd3632021-05-04 18:36:11 -0700717 * set_shrinker_bit():
Kirill Tkhaif90280d2018-08-17 15:48:25 -0700718 *
719 * list_lru_add() shrink_slab_memcg()
720 * list_add_tail() clear_bit()
721 * <MB> <MB>
722 * set_bit() do_shrink_slab()
723 */
724 smp_mb__after_atomic();
725 ret = do_shrink_slab(&sc, shrinker, priority);
726 if (ret == SHRINK_EMPTY)
727 ret = 0;
728 else
Yang Shi2bfd3632021-05-04 18:36:11 -0700729 set_shrinker_bit(memcg, nid, i);
Kirill Tkhaif90280d2018-08-17 15:48:25 -0700730 }
Kirill Tkhaib0dedc42018-08-17 15:48:14 -0700731 freed += ret;
732
733 if (rwsem_is_contended(&shrinker_rwsem)) {
734 freed = freed ? : 1;
735 break;
736 }
737 }
738unlock:
739 up_read(&shrinker_rwsem);
740 return freed;
741}
Yang Shi0a432dc2019-09-23 15:38:12 -0700742#else /* CONFIG_MEMCG */
Kirill Tkhaib0dedc42018-08-17 15:48:14 -0700743static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,
744 struct mem_cgroup *memcg, int priority)
745{
746 return 0;
747}
Yang Shi0a432dc2019-09-23 15:38:12 -0700748#endif /* CONFIG_MEMCG */
Kirill Tkhaib0dedc42018-08-17 15:48:14 -0700749
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800750/**
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800751 * shrink_slab - shrink slab caches
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800752 * @gfp_mask: allocation context
753 * @nid: node whose slab caches to target
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800754 * @memcg: memory cgroup whose slab caches to target
Josef Bacik9092c712018-01-31 16:16:26 -0800755 * @priority: the reclaim priority
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 *
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800757 * Call the shrink functions to age shrinkable caches.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 *
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800759 * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set,
760 * unaware shrinkers will receive a node id of 0 instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 *
Vladimir Davydovaeed1d322018-08-17 15:48:17 -0700762 * @memcg specifies the memory cgroup to target. Unaware shrinkers
763 * are called only if it is the root cgroup.
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800764 *
Josef Bacik9092c712018-01-31 16:16:26 -0800765 * @priority is sc->priority, we take the number of objects and >> by priority
766 * in order to get the scan target.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 *
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800768 * Returns the number of reclaimed slab objects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 */
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800770static unsigned long shrink_slab(gfp_t gfp_mask, int nid,
771 struct mem_cgroup *memcg,
Josef Bacik9092c712018-01-31 16:16:26 -0800772 int priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Kirill Tkhaib8e57ef2018-10-05 15:52:10 -0700774 unsigned long ret, freed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 struct shrinker *shrinker;
776
Yang Shifa1e5122019-08-02 21:48:44 -0700777 /*
778 * The root memcg might be allocated even though memcg is disabled
779 * via "cgroup_disable=memory" boot parameter. This could make
780 * mem_cgroup_is_root() return false, then just run memcg slab
781 * shrink, but skip global shrink. This may result in premature
782 * oom.
783 */
784 if (!mem_cgroup_disabled() && !mem_cgroup_is_root(memcg))
Kirill Tkhaib0dedc42018-08-17 15:48:14 -0700785 return shrink_slab_memcg(gfp_mask, nid, memcg, priority);
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800786
Tetsuo Handae830c632018-04-05 16:23:35 -0700787 if (!down_read_trylock(&shrinker_rwsem))
Minchan Kimf06590b2011-05-24 17:11:11 -0700788 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 list_for_each_entry(shrinker, &shrinker_list, list) {
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800791 struct shrink_control sc = {
792 .gfp_mask = gfp_mask,
793 .nid = nid,
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800794 .memcg = memcg,
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800795 };
Vladimir Davydovec970972014-01-23 15:53:23 -0800796
Kirill Tkhai9b996462018-08-17 15:48:21 -0700797 ret = do_shrink_slab(&sc, shrinker, priority);
798 if (ret == SHRINK_EMPTY)
799 ret = 0;
800 freed += ret;
Minchan Kime4966122018-01-31 16:16:55 -0800801 /*
802 * Bail out if someone want to register a new shrinker to
Ethon Paul55b65a52020-06-04 16:49:10 -0700803 * prevent the registration from being stalled for long periods
Minchan Kime4966122018-01-31 16:16:55 -0800804 * by parallel ongoing shrinking.
805 */
806 if (rwsem_is_contended(&shrinker_rwsem)) {
807 freed = freed ? : 1;
808 break;
809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 up_read(&shrinker_rwsem);
Minchan Kimf06590b2011-05-24 17:11:11 -0700813out:
814 cond_resched();
Dave Chinner24f7c6b2013-08-28 10:17:56 +1000815 return freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800818void drop_slab_node(int nid)
819{
820 unsigned long freed;
821
822 do {
823 struct mem_cgroup *memcg = NULL;
824
Chunxin Zang069c4112020-10-13 16:56:46 -0700825 if (fatal_signal_pending(current))
826 return;
827
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800828 freed = 0;
Vladimir Davydovaeed1d322018-08-17 15:48:17 -0700829 memcg = mem_cgroup_iter(NULL, NULL, NULL);
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800830 do {
Josef Bacik9092c712018-01-31 16:16:26 -0800831 freed += shrink_slab(GFP_KERNEL, nid, memcg, 0);
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800832 } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL);
833 } while (freed > 10);
834}
835
836void drop_slab(void)
837{
838 int nid;
839
840 for_each_online_node(nid)
841 drop_slab_node(nid);
842}
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844static inline int is_page_cache_freeable(struct page *page)
845{
Johannes Weinerceddc3a2009-09-21 17:03:00 -0700846 /*
847 * A freeable page cache page is referenced only by the caller
Matthew Wilcox67891ff2018-06-10 07:34:39 -0400848 * that isolated the page, the page cache and optional buffer
849 * heads at page->private.
Johannes Weinerceddc3a2009-09-21 17:03:00 -0700850 */
Matthew Wilcox (Oracle)3efe62e2020-10-15 20:05:56 -0700851 int page_cache_pins = thp_nr_pages(page);
Matthew Wilcox67891ff2018-06-10 07:34:39 -0400852 return page_count(page) - page_has_private(page) == 1 + page_cache_pins;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
Yang Shicb165562019-11-30 17:55:28 -0800855static int may_write_to_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Christoph Lameter930d9152006-01-08 01:00:47 -0800857 if (current->flags & PF_SWAPWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return 1;
Tejun Heo703c2702015-05-22 17:13:44 -0400859 if (!inode_write_congested(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return 1;
Tejun Heo703c2702015-05-22 17:13:44 -0400861 if (inode_to_bdi(inode) == current->backing_dev_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return 1;
863 return 0;
864}
865
866/*
867 * We detected a synchronous write error writing a page out. Probably
868 * -ENOSPC. We need to propagate that into the address_space for a subsequent
869 * fsync(), msync() or close().
870 *
871 * The tricky part is that after writepage we cannot touch the mapping: nothing
872 * prevents it from being freed up. But we have a ref on the page and once
873 * that page is locked, the mapping is pinned.
874 *
875 * We're allowed to run sleeping lock_page() here because we know the caller has
876 * __GFP_FS.
877 */
878static void handle_write_error(struct address_space *mapping,
879 struct page *page, int error)
880{
Jens Axboe7eaceac2011-03-10 08:52:07 +0100881 lock_page(page);
Guillaume Chazarain3e9f45b2007-05-08 00:23:25 -0700882 if (page_mapping(page) == mapping)
883 mapping_set_error(mapping, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 unlock_page(page);
885}
886
Christoph Lameter04e62a22006-06-23 02:03:38 -0700887/* possible outcome of pageout() */
888typedef enum {
889 /* failed to write page out, page is locked */
890 PAGE_KEEP,
891 /* move page to the active list, page is locked */
892 PAGE_ACTIVATE,
893 /* page has been sent to the disk successfully, page is unlocked */
894 PAGE_SUCCESS,
895 /* page is clean and locked */
896 PAGE_CLEAN,
897} pageout_t;
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899/*
Andrew Morton1742f192006-03-22 00:08:21 -0800900 * pageout is called by shrink_page_list() for each dirty page.
901 * Calls ->writepage().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 */
Yang Shicb165562019-11-30 17:55:28 -0800903static pageout_t pageout(struct page *page, struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
905 /*
906 * If the page is dirty, only perform writeback if that write
907 * will be non-blocking. To prevent this allocation from being
908 * stalled by pagecache activity. But note that there may be
909 * stalls if we need to run get_block(). We could test
910 * PagePrivate for that.
911 *
Al Viro81742022014-04-03 03:17:43 -0400912 * If this process is currently in __generic_file_write_iter() against
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 * this page's queue, we can perform writeback even if that
914 * will block.
915 *
916 * If the page is swapcache, write it back even if that would
917 * block, for some throttling. This happens by accident, because
918 * swap_backing_dev_info is bust: it doesn't reflect the
919 * congestion state of the swapdevs. Easy to fix, if needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 */
921 if (!is_page_cache_freeable(page))
922 return PAGE_KEEP;
923 if (!mapping) {
924 /*
925 * Some data journaling orphaned pages can have
926 * page->mapping == NULL while being dirty with clean buffers.
927 */
David Howells266cf652009-04-03 16:42:36 +0100928 if (page_has_private(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (try_to_free_buffers(page)) {
930 ClearPageDirty(page);
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -0700931 pr_info("%s: orphaned page\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return PAGE_CLEAN;
933 }
934 }
935 return PAGE_KEEP;
936 }
937 if (mapping->a_ops->writepage == NULL)
938 return PAGE_ACTIVATE;
Yang Shicb165562019-11-30 17:55:28 -0800939 if (!may_write_to_inode(mapping->host))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return PAGE_KEEP;
941
942 if (clear_page_dirty_for_io(page)) {
943 int res;
944 struct writeback_control wbc = {
945 .sync_mode = WB_SYNC_NONE,
946 .nr_to_write = SWAP_CLUSTER_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700947 .range_start = 0,
948 .range_end = LLONG_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 .for_reclaim = 1,
950 };
951
952 SetPageReclaim(page);
953 res = mapping->a_ops->writepage(page, &wbc);
954 if (res < 0)
955 handle_write_error(mapping, page, res);
Zach Brown994fc28c2005-12-15 14:28:17 -0800956 if (res == AOP_WRITEPAGE_ACTIVATE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 ClearPageReclaim(page);
958 return PAGE_ACTIVATE;
959 }
Andy Whitcroftc661b072007-08-22 14:01:26 -0700960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (!PageWriteback(page)) {
962 /* synchronous write or broken a_ops? */
963 ClearPageReclaim(page);
964 }
yalin wang3aa23852016-01-14 15:18:30 -0800965 trace_mm_vmscan_writepage(page);
Mel Gormanc4a25632016-07-28 15:46:23 -0700966 inc_node_page_state(page, NR_VMSCAN_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return PAGE_SUCCESS;
968 }
969
970 return PAGE_CLEAN;
971}
972
Andrew Mortona649fd92006-10-17 00:09:36 -0700973/*
Nick Piggine2867812008-07-25 19:45:30 -0700974 * Same as remove_mapping, but if the page is removed from the mapping, it
975 * gets returned with a refcount of 0.
Andrew Mortona649fd92006-10-17 00:09:36 -0700976 */
Johannes Weinera5289102014-04-03 14:47:51 -0700977static int __remove_mapping(struct address_space *mapping, struct page *page,
Johannes Weinerb9107182019-11-30 17:55:59 -0800978 bool reclaimed, struct mem_cgroup *target_memcg)
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800979{
Greg Thelenc4843a72015-05-22 17:13:16 -0400980 unsigned long flags;
Huang Yingbd4c82c22017-09-06 16:22:49 -0700981 int refcount;
Joonsoo Kimaae466b2020-08-11 18:30:50 -0700982 void *shadow = NULL;
Greg Thelenc4843a72015-05-22 17:13:16 -0400983
Nick Piggin28e4d962006-09-25 23:31:23 -0700984 BUG_ON(!PageLocked(page));
985 BUG_ON(mapping != page_mapping(page));
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800986
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700987 xa_lock_irqsave(&mapping->i_pages, flags);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800988 /*
Nick Piggin0fd0e6b2006-09-27 01:50:02 -0700989 * The non racy check for a busy page.
990 *
991 * Must be careful with the order of the tests. When someone has
992 * a ref to the page, it may be possible that they dirty it then
993 * drop the reference. So if PageDirty is tested before page_count
994 * here, then the following race may occur:
995 *
996 * get_user_pages(&page);
997 * [user mapping goes away]
998 * write_to(page);
999 * !PageDirty(page) [good]
1000 * SetPageDirty(page);
1001 * put_page(page);
1002 * !page_count(page) [good, discard it]
1003 *
1004 * [oops, our write_to data is lost]
1005 *
1006 * Reversing the order of the tests ensures such a situation cannot
1007 * escape unnoticed. The smp_rmb is needed to ensure the page->flags
Joonsoo Kim0139aa72016-05-19 17:10:49 -07001008 * load is not satisfied before that of page->_refcount.
Nick Piggin0fd0e6b2006-09-27 01:50:02 -07001009 *
1010 * Note that if SetPageDirty is always performed via set_page_dirty,
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001011 * and thus under the i_pages lock, then this ordering is not required.
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001012 */
William Kucharski906d2782019-10-18 20:20:33 -07001013 refcount = 1 + compound_nr(page);
Huang Yingbd4c82c22017-09-06 16:22:49 -07001014 if (!page_ref_freeze(page, refcount))
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001015 goto cannot_free;
Jiang Biao1c4c3b92018-08-21 21:53:13 -07001016 /* note: atomic_cmpxchg in page_ref_freeze provides the smp_rmb */
Nick Piggine2867812008-07-25 19:45:30 -07001017 if (unlikely(PageDirty(page))) {
Huang Yingbd4c82c22017-09-06 16:22:49 -07001018 page_ref_unfreeze(page, refcount);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001019 goto cannot_free;
Nick Piggine2867812008-07-25 19:45:30 -07001020 }
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001021
1022 if (PageSwapCache(page)) {
1023 swp_entry_t swap = { .val = page_private(page) };
Johannes Weiner0a31bc92014-08-08 14:19:22 -07001024 mem_cgroup_swapout(page, swap);
Joonsoo Kimaae466b2020-08-11 18:30:50 -07001025 if (reclaimed && !mapping_exiting(mapping))
1026 shadow = workingset_eviction(page, target_memcg);
1027 __delete_from_swap_cache(page, swap, shadow);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001028 xa_unlock_irqrestore(&mapping->i_pages, flags);
Minchan Kim75f6d6d2017-07-06 15:37:21 -07001029 put_swap_page(page, swap);
Nick Piggine2867812008-07-25 19:45:30 -07001030 } else {
Linus Torvalds6072d132010-12-01 13:35:19 -05001031 void (*freepage)(struct page *);
1032
1033 freepage = mapping->a_ops->freepage;
Johannes Weinera5289102014-04-03 14:47:51 -07001034 /*
1035 * Remember a shadow entry for reclaimed file cache in
1036 * order to detect refaults, thus thrashing, later on.
1037 *
1038 * But don't store shadows in an address space that is
dylan-meiners238c3042020-08-06 23:26:29 -07001039 * already exiting. This is not just an optimization,
Johannes Weinera5289102014-04-03 14:47:51 -07001040 * inode reclaim needs to empty out the radix tree or
1041 * the nodes are lost. Don't plant shadows behind its
1042 * back.
Ross Zwislerf9fe48b2016-01-22 15:10:40 -08001043 *
1044 * We also don't store shadows for DAX mappings because the
1045 * only page cache pages found in these are zero pages
1046 * covering holes, and because we don't want to mix DAX
1047 * exceptional entries and shadow exceptional entries in the
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001048 * same address_space.
Johannes Weinera5289102014-04-03 14:47:51 -07001049 */
Huang Ying9de4f222020-04-06 20:04:41 -07001050 if (reclaimed && page_is_file_lru(page) &&
Ross Zwislerf9fe48b2016-01-22 15:10:40 -08001051 !mapping_exiting(mapping) && !dax_mapping(mapping))
Johannes Weinerb9107182019-11-30 17:55:59 -08001052 shadow = workingset_eviction(page, target_memcg);
Johannes Weiner62cccb82016-03-15 14:57:22 -07001053 __delete_from_page_cache(page, shadow);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001054 xa_unlock_irqrestore(&mapping->i_pages, flags);
Linus Torvalds6072d132010-12-01 13:35:19 -05001055
1056 if (freepage != NULL)
1057 freepage(page);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001058 }
1059
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001060 return 1;
1061
1062cannot_free:
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001063 xa_unlock_irqrestore(&mapping->i_pages, flags);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001064 return 0;
1065}
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067/*
Nick Piggine2867812008-07-25 19:45:30 -07001068 * Attempt to detach a locked page from its ->mapping. If it is dirty or if
1069 * someone else has a ref on the page, abort and return 0. If it was
1070 * successfully detached, return 1. Assumes the caller has a single ref on
1071 * this page.
1072 */
1073int remove_mapping(struct address_space *mapping, struct page *page)
1074{
Johannes Weinerb9107182019-11-30 17:55:59 -08001075 if (__remove_mapping(mapping, page, false, NULL)) {
Nick Piggine2867812008-07-25 19:45:30 -07001076 /*
1077 * Unfreezing the refcount with 1 rather than 2 effectively
1078 * drops the pagecache ref for us without requiring another
1079 * atomic operation.
1080 */
Joonsoo Kimfe896d12016-03-17 14:19:26 -07001081 page_ref_unfreeze(page, 1);
Nick Piggine2867812008-07-25 19:45:30 -07001082 return 1;
1083 }
1084 return 0;
1085}
1086
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001087/**
1088 * putback_lru_page - put previously isolated page onto appropriate LRU list
1089 * @page: page to be put back to appropriate lru list
1090 *
1091 * Add previously isolated @page to appropriate LRU list.
1092 * Page may still be unevictable for other reasons.
1093 *
1094 * lru_lock must not be held, interrupts must be enabled.
1095 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001096void putback_lru_page(struct page *page)
1097{
Shakeel Butt9c4e6b12018-02-21 14:45:28 -08001098 lru_cache_add(page);
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001099 put_page(page); /* drop ref from isolate */
1100}
1101
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001102enum page_references {
1103 PAGEREF_RECLAIM,
1104 PAGEREF_RECLAIM_CLEAN,
Johannes Weiner645747462010-03-05 13:42:22 -08001105 PAGEREF_KEEP,
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001106 PAGEREF_ACTIVATE,
1107};
1108
1109static enum page_references page_check_references(struct page *page,
1110 struct scan_control *sc)
1111{
Johannes Weiner645747462010-03-05 13:42:22 -08001112 int referenced_ptes, referenced_page;
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001113 unsigned long vm_flags;
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001114
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001115 referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup,
1116 &vm_flags);
Johannes Weiner645747462010-03-05 13:42:22 -08001117 referenced_page = TestClearPageReferenced(page);
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001118
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001119 /*
1120 * Mlock lost the isolation race with us. Let try_to_unmap()
1121 * move the page to the unevictable list.
1122 */
1123 if (vm_flags & VM_LOCKED)
1124 return PAGEREF_RECLAIM;
1125
Johannes Weiner645747462010-03-05 13:42:22 -08001126 if (referenced_ptes) {
Johannes Weiner645747462010-03-05 13:42:22 -08001127 /*
1128 * All mapped pages start out with page table
1129 * references from the instantiating fault, so we need
1130 * to look twice if a mapped file page is used more
1131 * than once.
1132 *
1133 * Mark it and spare it for another trip around the
1134 * inactive list. Another page table reference will
1135 * lead to its activation.
1136 *
1137 * Note: the mark is set for activated pages as well
1138 * so that recently deactivated but used pages are
1139 * quickly recovered.
1140 */
1141 SetPageReferenced(page);
1142
Konstantin Khlebnikov34dbc672012-01-10 15:06:59 -08001143 if (referenced_page || referenced_ptes > 1)
Johannes Weiner645747462010-03-05 13:42:22 -08001144 return PAGEREF_ACTIVATE;
1145
Konstantin Khlebnikovc909e992012-01-10 15:07:03 -08001146 /*
1147 * Activate file-backed executable pages after first usage.
1148 */
Joonsoo Kimb5181542020-08-11 18:30:40 -07001149 if ((vm_flags & VM_EXEC) && !PageSwapBacked(page))
Konstantin Khlebnikovc909e992012-01-10 15:07:03 -08001150 return PAGEREF_ACTIVATE;
1151
Johannes Weiner645747462010-03-05 13:42:22 -08001152 return PAGEREF_KEEP;
1153 }
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001154
1155 /* Reclaim if clean, defer dirty pages to writeback */
KOSAKI Motohiro2e302442010-10-26 14:21:46 -07001156 if (referenced_page && !PageSwapBacked(page))
Johannes Weiner645747462010-03-05 13:42:22 -08001157 return PAGEREF_RECLAIM_CLEAN;
1158
1159 return PAGEREF_RECLAIM;
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001160}
1161
Mel Gormane2be15f2013-07-03 15:01:57 -07001162/* Check if a page is dirty or under writeback */
1163static void page_check_dirty_writeback(struct page *page,
1164 bool *dirty, bool *writeback)
1165{
Mel Gormanb4597222013-07-03 15:02:05 -07001166 struct address_space *mapping;
1167
Mel Gormane2be15f2013-07-03 15:01:57 -07001168 /*
1169 * Anonymous pages are not handled by flushers and must be written
1170 * from reclaim context. Do not stall reclaim based on them
1171 */
Huang Ying9de4f222020-04-06 20:04:41 -07001172 if (!page_is_file_lru(page) ||
Shaohua Li802a3a92017-05-03 14:52:32 -07001173 (PageAnon(page) && !PageSwapBacked(page))) {
Mel Gormane2be15f2013-07-03 15:01:57 -07001174 *dirty = false;
1175 *writeback = false;
1176 return;
1177 }
1178
1179 /* By default assume that the page flags are accurate */
1180 *dirty = PageDirty(page);
1181 *writeback = PageWriteback(page);
Mel Gormanb4597222013-07-03 15:02:05 -07001182
1183 /* Verify dirty/writeback state if the filesystem supports it */
1184 if (!page_has_private(page))
1185 return;
1186
1187 mapping = page_mapping(page);
1188 if (mapping && mapping->a_ops->is_dirty_writeback)
1189 mapping->a_ops->is_dirty_writeback(page, dirty, writeback);
Mel Gormane2be15f2013-07-03 15:01:57 -07001190}
1191
Nick Piggine2867812008-07-25 19:45:30 -07001192/*
Andrew Morton1742f192006-03-22 00:08:21 -08001193 * shrink_page_list() returns the number of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 */
Maninder Singh730ec8c2020-06-03 16:01:18 -07001195static unsigned int shrink_page_list(struct list_head *page_list,
1196 struct pglist_data *pgdat,
1197 struct scan_control *sc,
Maninder Singh730ec8c2020-06-03 16:01:18 -07001198 struct reclaim_stat *stat,
1199 bool ignore_references)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
1201 LIST_HEAD(ret_pages);
Mel Gormanabe4c3b2010-08-09 17:19:31 -07001202 LIST_HEAD(free_pages);
Maninder Singh730ec8c2020-06-03 16:01:18 -07001203 unsigned int nr_reclaimed = 0;
1204 unsigned int pgactivate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Kirill Tkhai060f0052019-03-05 15:48:15 -08001206 memset(stat, 0, sizeof(*stat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 cond_resched();
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 while (!list_empty(page_list)) {
1210 struct address_space *mapping;
1211 struct page *page;
Minchan Kim8940b342019-09-25 16:49:11 -07001212 enum page_references references = PAGEREF_RECLAIM;
Kirill Tkhai4b793062020-04-01 21:10:18 -07001213 bool dirty, writeback, may_enter_fs;
Yang Shi98879b32019-07-11 20:59:30 -07001214 unsigned int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 cond_resched();
1217
1218 page = lru_to_page(page_list);
1219 list_del(&page->lru);
1220
Nick Piggin529ae9a2008-08-02 12:01:03 +02001221 if (!trylock_page(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 goto keep;
1223
Sasha Levin309381fea2014-01-23 15:52:54 -08001224 VM_BUG_ON_PAGE(PageActive(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -07001226 nr_pages = compound_nr(page);
Yang Shi98879b32019-07-11 20:59:30 -07001227
1228 /* Account the number of base pages even though THP */
1229 sc->nr_scanned += nr_pages;
Christoph Lameter80e43422006-02-11 17:55:53 -08001230
Hugh Dickins39b5f292012-10-08 16:33:18 -07001231 if (unlikely(!page_evictable(page)))
Minchan Kimad6b6702017-05-03 14:54:13 -07001232 goto activate_locked;
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001233
Johannes Weinera6dc60f82009-03-31 15:19:30 -07001234 if (!sc->may_unmap && page_mapped(page))
Christoph Lameter80e43422006-02-11 17:55:53 -08001235 goto keep_locked;
1236
Andy Whitcroftc661b072007-08-22 14:01:26 -07001237 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
1238 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
1239
Mel Gorman283aba92013-07-03 15:01:51 -07001240 /*
Andrey Ryabinin894befe2018-04-10 16:27:51 -07001241 * The number of dirty pages determines if a node is marked
Mel Gormane2be15f2013-07-03 15:01:57 -07001242 * reclaim_congested which affects wait_iff_congested. kswapd
1243 * will stall and start writing pages if the tail of the LRU
1244 * is all dirty unqueued pages.
1245 */
1246 page_check_dirty_writeback(page, &dirty, &writeback);
1247 if (dirty || writeback)
Kirill Tkhai060f0052019-03-05 15:48:15 -08001248 stat->nr_dirty++;
Mel Gormane2be15f2013-07-03 15:01:57 -07001249
1250 if (dirty && !writeback)
Kirill Tkhai060f0052019-03-05 15:48:15 -08001251 stat->nr_unqueued_dirty++;
Mel Gormane2be15f2013-07-03 15:01:57 -07001252
Mel Gormand04e8ac2013-07-03 15:02:03 -07001253 /*
1254 * Treat this page as congested if the underlying BDI is or if
1255 * pages are cycling through the LRU so quickly that the
1256 * pages marked for immediate reclaim are making it to the
1257 * end of the LRU a second time.
1258 */
Mel Gormane2be15f2013-07-03 15:01:57 -07001259 mapping = page_mapping(page);
Jamie Liu1da58ee2014-12-10 15:43:20 -08001260 if (((dirty || writeback) && mapping &&
Tejun Heo703c2702015-05-22 17:13:44 -04001261 inode_write_congested(mapping->host)) ||
Mel Gormand04e8ac2013-07-03 15:02:03 -07001262 (writeback && PageReclaim(page)))
Kirill Tkhai060f0052019-03-05 15:48:15 -08001263 stat->nr_congested++;
Mel Gormane2be15f2013-07-03 15:01:57 -07001264
1265 /*
Mel Gorman283aba92013-07-03 15:01:51 -07001266 * If a page at the tail of the LRU is under writeback, there
1267 * are three cases to consider.
1268 *
1269 * 1) If reclaim is encountering an excessive number of pages
1270 * under writeback and this page is both under writeback and
1271 * PageReclaim then it indicates that pages are being queued
1272 * for IO but are being recycled through the LRU before the
1273 * IO can complete. Waiting on the page itself risks an
1274 * indefinite stall if it is impossible to writeback the
1275 * page due to IO error or disconnected storage so instead
Mel Gormanb1a6f212013-07-03 15:01:58 -07001276 * note that the LRU is being scanned too quickly and the
1277 * caller can stall after page list has been processed.
Mel Gorman283aba92013-07-03 15:01:51 -07001278 *
Tejun Heo97c93412015-05-22 18:23:36 -04001279 * 2) Global or new memcg reclaim encounters a page that is
Michal Hockoecf5fc62015-08-04 14:36:58 -07001280 * not marked for immediate reclaim, or the caller does not
1281 * have __GFP_FS (or __GFP_IO if it's simply going to swap,
1282 * not to fs). In this case mark the page for immediate
Tejun Heo97c93412015-05-22 18:23:36 -04001283 * reclaim and continue scanning.
Mel Gorman283aba92013-07-03 15:01:51 -07001284 *
Michal Hockoecf5fc62015-08-04 14:36:58 -07001285 * Require may_enter_fs because we would wait on fs, which
1286 * may not have submitted IO yet. And the loop driver might
Mel Gorman283aba92013-07-03 15:01:51 -07001287 * enter reclaim, and deadlock if it waits on a page for
1288 * which it is needed to do the write (loop masks off
1289 * __GFP_IO|__GFP_FS for this reason); but more thought
1290 * would probably show more reasons.
1291 *
Hugh Dickins7fadc822015-09-08 15:03:46 -07001292 * 3) Legacy memcg encounters a page that is already marked
Mel Gorman283aba92013-07-03 15:01:51 -07001293 * PageReclaim. memcg does not have any dirty pages
1294 * throttling so we could easily OOM just because too many
1295 * pages are in writeback and there is nothing else to
1296 * reclaim. Wait for the writeback to complete.
Johannes Weinerc55e8d02017-02-24 14:56:23 -08001297 *
1298 * In cases 1) and 2) we activate the pages to get them out of
1299 * the way while we continue scanning for clean pages on the
1300 * inactive list and refilling from the active list. The
1301 * observation here is that waiting for disk writes is more
1302 * expensive than potentially causing reloads down the line.
1303 * Since they're marked for immediate reclaim, they won't put
1304 * memory pressure on the cache working set any longer than it
1305 * takes to write them to disk.
Mel Gorman283aba92013-07-03 15:01:51 -07001306 */
Andy Whitcroftc661b072007-08-22 14:01:26 -07001307 if (PageWriteback(page)) {
Mel Gorman283aba92013-07-03 15:01:51 -07001308 /* Case 1 above */
1309 if (current_is_kswapd() &&
1310 PageReclaim(page) &&
Mel Gorman599d0c92016-07-28 15:45:31 -07001311 test_bit(PGDAT_WRITEBACK, &pgdat->flags)) {
Kirill Tkhai060f0052019-03-05 15:48:15 -08001312 stat->nr_immediate++;
Johannes Weinerc55e8d02017-02-24 14:56:23 -08001313 goto activate_locked;
Mel Gorman283aba92013-07-03 15:01:51 -07001314
1315 /* Case 2 above */
Johannes Weinerb5ead352019-11-30 17:55:40 -08001316 } else if (writeback_throttling_sane(sc) ||
Michal Hockoecf5fc62015-08-04 14:36:58 -07001317 !PageReclaim(page) || !may_enter_fs) {
Hugh Dickinsc3b94f42012-07-31 16:45:59 -07001318 /*
1319 * This is slightly racy - end_page_writeback()
1320 * might have just cleared PageReclaim, then
1321 * setting PageReclaim here end up interpreted
1322 * as PageReadahead - but that does not matter
1323 * enough to care. What we do want is for this
1324 * page to have PageReclaim set next time memcg
1325 * reclaim reaches the tests above, so it will
1326 * then wait_on_page_writeback() to avoid OOM;
1327 * and it's also appropriate in global reclaim.
1328 */
1329 SetPageReclaim(page);
Kirill Tkhai060f0052019-03-05 15:48:15 -08001330 stat->nr_writeback++;
Johannes Weinerc55e8d02017-02-24 14:56:23 -08001331 goto activate_locked;
Mel Gorman283aba92013-07-03 15:01:51 -07001332
1333 /* Case 3 above */
1334 } else {
Hugh Dickins7fadc822015-09-08 15:03:46 -07001335 unlock_page(page);
Mel Gorman283aba92013-07-03 15:01:51 -07001336 wait_on_page_writeback(page);
Hugh Dickins7fadc822015-09-08 15:03:46 -07001337 /* then go back and try same page again */
1338 list_add_tail(&page->lru, page_list);
1339 continue;
Michal Hockoe62e3842012-07-31 16:45:55 -07001340 }
Andy Whitcroftc661b072007-08-22 14:01:26 -07001341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Minchan Kim8940b342019-09-25 16:49:11 -07001343 if (!ignore_references)
Minchan Kim02c6de82012-10-08 16:31:55 -07001344 references = page_check_references(page, sc);
1345
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001346 switch (references) {
1347 case PAGEREF_ACTIVATE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 goto activate_locked;
Johannes Weiner645747462010-03-05 13:42:22 -08001349 case PAGEREF_KEEP:
Yang Shi98879b32019-07-11 20:59:30 -07001350 stat->nr_ref_keep += nr_pages;
Johannes Weiner645747462010-03-05 13:42:22 -08001351 goto keep_locked;
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001352 case PAGEREF_RECLAIM:
1353 case PAGEREF_RECLAIM_CLEAN:
1354 ; /* try to reclaim the page below */
1355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 /*
1358 * Anonymous process memory has backing store?
1359 * Try to allocate it some swap space here.
Shaohua Li802a3a92017-05-03 14:52:32 -07001360 * Lazyfree page could be freed directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 */
Huang Yingbd4c82c22017-09-06 16:22:49 -07001362 if (PageAnon(page) && PageSwapBacked(page)) {
1363 if (!PageSwapCache(page)) {
1364 if (!(sc->gfp_mask & __GFP_IO))
1365 goto keep_locked;
Linus Torvaldsfeb889f2021-01-16 15:34:57 -08001366 if (page_maybe_dma_pinned(page))
1367 goto keep_locked;
Huang Yingbd4c82c22017-09-06 16:22:49 -07001368 if (PageTransHuge(page)) {
1369 /* cannot split THP, skip it */
1370 if (!can_split_huge_page(page, NULL))
1371 goto activate_locked;
1372 /*
1373 * Split pages without a PMD map right
1374 * away. Chances are some or all of the
1375 * tail pages can be freed without IO.
1376 */
1377 if (!compound_mapcount(page) &&
1378 split_huge_page_to_list(page,
1379 page_list))
1380 goto activate_locked;
1381 }
1382 if (!add_to_swap(page)) {
1383 if (!PageTransHuge(page))
Yang Shi98879b32019-07-11 20:59:30 -07001384 goto activate_locked_split;
Huang Yingbd4c82c22017-09-06 16:22:49 -07001385 /* Fallback to swap normal pages */
1386 if (split_huge_page_to_list(page,
1387 page_list))
1388 goto activate_locked;
Huang Yingfe490cc2017-09-06 16:22:52 -07001389#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1390 count_vm_event(THP_SWPOUT_FALLBACK);
1391#endif
Huang Yingbd4c82c22017-09-06 16:22:49 -07001392 if (!add_to_swap(page))
Yang Shi98879b32019-07-11 20:59:30 -07001393 goto activate_locked_split;
Huang Yingbd4c82c22017-09-06 16:22:49 -07001394 }
Minchan Kim0f074652017-07-06 15:37:24 -07001395
Kirill Tkhai4b793062020-04-01 21:10:18 -07001396 may_enter_fs = true;
Huang Yingbd4c82c22017-09-06 16:22:49 -07001397
1398 /* Adding to swap updated mapping */
1399 mapping = page_mapping(page);
Minchan Kim0f074652017-07-06 15:37:24 -07001400 }
Kirill A. Shutemov7751b2d2016-07-26 15:25:56 -07001401 } else if (unlikely(PageTransHuge(page))) {
1402 /* Split file THP */
1403 if (split_huge_page_to_list(page, page_list))
1404 goto keep_locked;
Mel Gormane2be15f2013-07-03 15:01:57 -07001405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 /*
Yang Shi98879b32019-07-11 20:59:30 -07001408 * THP may get split above, need minus tail pages and update
1409 * nr_pages to avoid accounting tail pages twice.
1410 *
1411 * The tail pages that are added into swap cache successfully
1412 * reach here.
1413 */
1414 if ((nr_pages > 1) && !PageTransHuge(page)) {
1415 sc->nr_scanned -= (nr_pages - 1);
1416 nr_pages = 1;
1417 }
1418
1419 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 * The page is mapped into the page tables of one or more
1421 * processes. Try to unmap it here.
1422 */
Shaohua Li802a3a92017-05-03 14:52:32 -07001423 if (page_mapped(page)) {
Shakeel Butt013339d2020-12-14 19:06:39 -08001424 enum ttu_flags flags = TTU_BATCH_FLUSH;
Jaewon Kim1f318a92020-06-03 16:01:15 -07001425 bool was_swapbacked = PageSwapBacked(page);
Huang Yingbd4c82c22017-09-06 16:22:49 -07001426
1427 if (unlikely(PageTransHuge(page)))
1428 flags |= TTU_SPLIT_HUGE_PMD;
Jaewon Kim1f318a92020-06-03 16:01:15 -07001429
Huang Yingbd4c82c22017-09-06 16:22:49 -07001430 if (!try_to_unmap(page, flags)) {
Yang Shi98879b32019-07-11 20:59:30 -07001431 stat->nr_unmap_fail += nr_pages;
Jaewon Kim1f318a92020-06-03 16:01:15 -07001432 if (!was_swapbacked && PageSwapBacked(page))
1433 stat->nr_lazyfree_fail += nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 goto activate_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
1436 }
1437
1438 if (PageDirty(page)) {
Mel Gormanee728862011-10-31 17:07:38 -07001439 /*
Johannes Weiner4eda4822017-02-24 14:56:20 -08001440 * Only kswapd can writeback filesystem pages
1441 * to avoid risk of stack overflow. But avoid
1442 * injecting inefficient single-page IO into
1443 * flusher writeback as much as possible: only
1444 * write pages when we've encountered many
1445 * dirty pages, and when we've already scanned
1446 * the rest of the LRU for clean pages and see
1447 * the same dirty pages again (PageReclaim).
Mel Gormanee728862011-10-31 17:07:38 -07001448 */
Huang Ying9de4f222020-04-06 20:04:41 -07001449 if (page_is_file_lru(page) &&
Johannes Weiner4eda4822017-02-24 14:56:20 -08001450 (!current_is_kswapd() || !PageReclaim(page) ||
1451 !test_bit(PGDAT_DIRTY, &pgdat->flags))) {
Mel Gorman49ea7eb2011-10-31 17:07:59 -07001452 /*
1453 * Immediately reclaim when written back.
1454 * Similar in principal to deactivate_page()
1455 * except we already have the page isolated
1456 * and know it's dirty
1457 */
Mel Gormanc4a25632016-07-28 15:46:23 -07001458 inc_node_page_state(page, NR_VMSCAN_IMMEDIATE);
Mel Gorman49ea7eb2011-10-31 17:07:59 -07001459 SetPageReclaim(page);
1460
Johannes Weinerc55e8d02017-02-24 14:56:23 -08001461 goto activate_locked;
Mel Gormanee728862011-10-31 17:07:38 -07001462 }
1463
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001464 if (references == PAGEREF_RECLAIM_CLEAN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 goto keep_locked;
Andrew Morton4dd4b922008-03-24 12:29:52 -07001466 if (!may_enter_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 goto keep_locked;
Christoph Lameter52a83632006-02-01 03:05:28 -08001468 if (!sc->may_writepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 goto keep_locked;
1470
Mel Gormand950c942015-09-04 15:47:35 -07001471 /*
1472 * Page is dirty. Flush the TLB if a writable entry
1473 * potentially exists to avoid CPU writes after IO
1474 * starts and then write it out here.
1475 */
1476 try_to_unmap_flush_dirty();
Yang Shicb165562019-11-30 17:55:28 -08001477 switch (pageout(page, mapping)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 case PAGE_KEEP:
1479 goto keep_locked;
1480 case PAGE_ACTIVATE:
1481 goto activate_locked;
1482 case PAGE_SUCCESS:
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -07001483 stat->nr_pageout += thp_nr_pages(page);
Johannes Weiner96f8bf42020-06-03 16:03:09 -07001484
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -07001485 if (PageWriteback(page))
Mel Gorman41ac1992012-05-29 15:06:19 -07001486 goto keep;
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -07001487 if (PageDirty(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 goto keep;
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -07001489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 /*
1491 * A synchronous write - probably a ramdisk. Go
1492 * ahead and try to reclaim the page.
1493 */
Nick Piggin529ae9a2008-08-02 12:01:03 +02001494 if (!trylock_page(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 goto keep;
1496 if (PageDirty(page) || PageWriteback(page))
1497 goto keep_locked;
1498 mapping = page_mapping(page);
Gustavo A. R. Silva01359eb2020-12-14 19:15:00 -08001499 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 case PAGE_CLEAN:
1501 ; /* try to free the page below */
1502 }
1503 }
1504
1505 /*
1506 * If the page has buffers, try to free the buffer mappings
1507 * associated with this page. If we succeed we try to free
1508 * the page as well.
1509 *
1510 * We do this even if the page is PageDirty().
1511 * try_to_release_page() does not perform I/O, but it is
1512 * possible for a page to have PageDirty set, but it is actually
1513 * clean (all its buffers are clean). This happens if the
1514 * buffers were written out directly, with submit_bh(). ext3
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001515 * will do this, as well as the blockdev mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 * try_to_release_page() will discover that cleanness and will
1517 * drop the buffers and mark the page clean - it can be freed.
1518 *
1519 * Rarely, pages can have buffers and no ->mapping. These are
1520 * the pages which were not successfully invalidated in
Yang Shid12b8952020-12-14 19:13:02 -08001521 * truncate_cleanup_page(). We try to drop those buffers here
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 * and if that worked, and the page is no longer mapped into
1523 * process address space (page_count == 1) it can be freed.
1524 * Otherwise, leave the page on the LRU so it is swappable.
1525 */
David Howells266cf652009-04-03 16:42:36 +01001526 if (page_has_private(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (!try_to_release_page(page, sc->gfp_mask))
1528 goto activate_locked;
Nick Piggine2867812008-07-25 19:45:30 -07001529 if (!mapping && page_count(page) == 1) {
1530 unlock_page(page);
1531 if (put_page_testzero(page))
1532 goto free_it;
1533 else {
1534 /*
1535 * rare race with speculative reference.
1536 * the speculative reference will free
1537 * this page shortly, so we may
1538 * increment nr_reclaimed here (and
1539 * leave it off the LRU).
1540 */
1541 nr_reclaimed++;
1542 continue;
1543 }
1544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
1546
Shaohua Li802a3a92017-05-03 14:52:32 -07001547 if (PageAnon(page) && !PageSwapBacked(page)) {
1548 /* follow __remove_mapping for reference */
1549 if (!page_ref_freeze(page, 1))
1550 goto keep_locked;
1551 if (PageDirty(page)) {
1552 page_ref_unfreeze(page, 1);
1553 goto keep_locked;
1554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Shaohua Li802a3a92017-05-03 14:52:32 -07001556 count_vm_event(PGLAZYFREED);
Roman Gushchin22621852017-07-06 15:40:25 -07001557 count_memcg_page_event(page, PGLAZYFREED);
Johannes Weinerb9107182019-11-30 17:55:59 -08001558 } else if (!mapping || !__remove_mapping(mapping, page, true,
1559 sc->target_mem_cgroup))
Shaohua Li802a3a92017-05-03 14:52:32 -07001560 goto keep_locked;
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001561
1562 unlock_page(page);
Nick Piggine2867812008-07-25 19:45:30 -07001563free_it:
Yang Shi98879b32019-07-11 20:59:30 -07001564 /*
1565 * THP may get swapped out in a whole, need account
1566 * all base pages.
1567 */
1568 nr_reclaimed += nr_pages;
Mel Gormanabe4c3b2010-08-09 17:19:31 -07001569
1570 /*
1571 * Is there need to periodically free_page_list? It would
1572 * appear not as the counts should be low
1573 */
Yang Shi7ae88532019-09-23 15:38:09 -07001574 if (unlikely(PageTransHuge(page)))
Matthew Wilcox (Oracle)ff45fc32020-06-03 16:01:09 -07001575 destroy_compound_page(page);
Yang Shi7ae88532019-09-23 15:38:09 -07001576 else
Huang Yingbd4c82c22017-09-06 16:22:49 -07001577 list_add(&page->lru, &free_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 continue;
1579
Yang Shi98879b32019-07-11 20:59:30 -07001580activate_locked_split:
1581 /*
1582 * The tail pages that are failed to add into swap cache
1583 * reach here. Fixup nr_scanned and nr_pages.
1584 */
1585 if (nr_pages > 1) {
1586 sc->nr_scanned -= (nr_pages - 1);
1587 nr_pages = 1;
1588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589activate_locked:
Rik van Riel68a223942008-10-18 20:26:23 -07001590 /* Not a candidate for swapping, so reclaim swap space. */
Minchan Kimad6b6702017-05-03 14:54:13 -07001591 if (PageSwapCache(page) && (mem_cgroup_swap_full(page) ||
1592 PageMlocked(page)))
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -08001593 try_to_free_swap(page);
Sasha Levin309381fea2014-01-23 15:52:54 -08001594 VM_BUG_ON_PAGE(PageActive(page), page);
Minchan Kimad6b6702017-05-03 14:54:13 -07001595 if (!PageMlocked(page)) {
Huang Ying9de4f222020-04-06 20:04:41 -07001596 int type = page_is_file_lru(page);
Minchan Kimad6b6702017-05-03 14:54:13 -07001597 SetPageActive(page);
Yang Shi98879b32019-07-11 20:59:30 -07001598 stat->nr_activate[type] += nr_pages;
Roman Gushchin22621852017-07-06 15:40:25 -07001599 count_memcg_page_event(page, PGACTIVATE);
Minchan Kimad6b6702017-05-03 14:54:13 -07001600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601keep_locked:
1602 unlock_page(page);
1603keep:
1604 list_add(&page->lru, &ret_pages);
Sasha Levin309381fea2014-01-23 15:52:54 -08001605 VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 }
Mel Gormanabe4c3b2010-08-09 17:19:31 -07001607
Yang Shi98879b32019-07-11 20:59:30 -07001608 pgactivate = stat->nr_activate[0] + stat->nr_activate[1];
1609
Johannes Weiner747db952014-08-08 14:19:24 -07001610 mem_cgroup_uncharge_list(&free_pages);
Mel Gorman72b252a2015-09-04 15:47:32 -07001611 try_to_unmap_flush();
Mel Gorman2d4894b2017-11-15 17:37:59 -08001612 free_unref_page_list(&free_pages);
Mel Gormanabe4c3b2010-08-09 17:19:31 -07001613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 list_splice(&ret_pages, page_list);
Kirill Tkhai886cf192019-05-13 17:16:51 -07001615 count_vm_events(PGACTIVATE, pgactivate);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07001616
Andrew Morton05ff5132006-03-22 00:08:20 -08001617 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
Maninder Singh730ec8c2020-06-03 16:01:18 -07001620unsigned int reclaim_clean_pages_from_list(struct zone *zone,
Minchan Kim02c6de82012-10-08 16:31:55 -07001621 struct list_head *page_list)
1622{
1623 struct scan_control sc = {
1624 .gfp_mask = GFP_KERNEL,
1625 .priority = DEF_PRIORITY,
1626 .may_unmap = 1,
1627 };
Jaewon Kim1f318a92020-06-03 16:01:15 -07001628 struct reclaim_stat stat;
Maninder Singh730ec8c2020-06-03 16:01:18 -07001629 unsigned int nr_reclaimed;
Minchan Kim02c6de82012-10-08 16:31:55 -07001630 struct page *page, *next;
1631 LIST_HEAD(clean_pages);
1632
1633 list_for_each_entry_safe(page, next, page_list, lru) {
Oscar Salvadorae37c7f2021-05-04 18:35:29 -07001634 if (!PageHuge(page) && page_is_file_lru(page) &&
1635 !PageDirty(page) && !__PageMovable(page) &&
1636 !PageUnevictable(page)) {
Minchan Kim02c6de82012-10-08 16:31:55 -07001637 ClearPageActive(page);
1638 list_move(&page->lru, &clean_pages);
1639 }
1640 }
1641
Jaewon Kim1f318a92020-06-03 16:01:15 -07001642 nr_reclaimed = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc,
Shakeel Butt013339d2020-12-14 19:06:39 -08001643 &stat, true);
Minchan Kim02c6de82012-10-08 16:31:55 -07001644 list_splice(&clean_pages, page_list);
Nicholas Piggin2da9f632020-11-13 22:51:46 -08001645 mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE,
1646 -(long)nr_reclaimed);
Jaewon Kim1f318a92020-06-03 16:01:15 -07001647 /*
1648 * Since lazyfree pages are isolated from file LRU from the beginning,
1649 * they will rotate back to anonymous LRU in the end if it failed to
1650 * discard so isolated count will be mismatched.
1651 * Compensate the isolated count for both LRU lists.
1652 */
1653 mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON,
1654 stat.nr_lazyfree_fail);
1655 mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE,
Nicholas Piggin2da9f632020-11-13 22:51:46 -08001656 -(long)stat.nr_lazyfree_fail);
Jaewon Kim1f318a92020-06-03 16:01:15 -07001657 return nr_reclaimed;
Minchan Kim02c6de82012-10-08 16:31:55 -07001658}
1659
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001660/*
1661 * Attempt to remove the specified page from its LRU. Only take this page
1662 * if it is of the appropriate PageActive status. Pages which are being
1663 * freed elsewhere are also ignored.
1664 *
1665 * page: page to consider
1666 * mode: one of the LRU isolation modes defined above
1667 *
Alex Shic2135f72021-02-24 12:08:01 -08001668 * returns true on success, false on failure.
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001669 */
Alex Shic2135f72021-02-24 12:08:01 -08001670bool __isolate_lru_page_prepare(struct page *page, isolate_mode_t mode)
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001671{
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001672 /* Only take pages on the LRU. */
1673 if (!PageLRU(page))
Alex Shic2135f72021-02-24 12:08:01 -08001674 return false;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001675
Minchan Kime46a2872012-10-08 16:33:48 -07001676 /* Compaction should not handle unevictable pages but CMA can do so */
1677 if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE))
Alex Shic2135f72021-02-24 12:08:01 -08001678 return false;
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001679
Mel Gormanc8244932012-01-12 17:19:38 -08001680 /*
1681 * To minimise LRU disruption, the caller can indicate that it only
1682 * wants to isolate pages it will be able to operate on without
1683 * blocking - clean pages for the most part.
1684 *
Mel Gormanc8244932012-01-12 17:19:38 -08001685 * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages
1686 * that it is possible to migrate without blocking
1687 */
Johannes Weiner1276ad62017-02-24 14:56:11 -08001688 if (mode & ISOLATE_ASYNC_MIGRATE) {
Mel Gormanc8244932012-01-12 17:19:38 -08001689 /* All the caller can do on PageWriteback is block */
1690 if (PageWriteback(page))
Alex Shic2135f72021-02-24 12:08:01 -08001691 return false;
Mel Gormanc8244932012-01-12 17:19:38 -08001692
1693 if (PageDirty(page)) {
1694 struct address_space *mapping;
Mel Gorman69d763f2018-01-31 16:19:52 -08001695 bool migrate_dirty;
Mel Gormanc8244932012-01-12 17:19:38 -08001696
Mel Gormanc8244932012-01-12 17:19:38 -08001697 /*
1698 * Only pages without mappings or that have a
1699 * ->migratepage callback are possible to migrate
Mel Gorman69d763f2018-01-31 16:19:52 -08001700 * without blocking. However, we can be racing with
1701 * truncation so it's necessary to lock the page
1702 * to stabilise the mapping as truncation holds
1703 * the page lock until after the page is removed
1704 * from the page cache.
Mel Gormanc8244932012-01-12 17:19:38 -08001705 */
Mel Gorman69d763f2018-01-31 16:19:52 -08001706 if (!trylock_page(page))
Alex Shic2135f72021-02-24 12:08:01 -08001707 return false;
Mel Gorman69d763f2018-01-31 16:19:52 -08001708
Mel Gormanc8244932012-01-12 17:19:38 -08001709 mapping = page_mapping(page);
Hugh Dickins145e1a72018-06-01 16:50:50 -07001710 migrate_dirty = !mapping || mapping->a_ops->migratepage;
Mel Gorman69d763f2018-01-31 16:19:52 -08001711 unlock_page(page);
1712 if (!migrate_dirty)
Alex Shic2135f72021-02-24 12:08:01 -08001713 return false;
Mel Gormanc8244932012-01-12 17:19:38 -08001714 }
1715 }
Minchan Kim39deaf82011-10-31 17:06:51 -07001716
Minchan Kimf80c0672011-10-31 17:06:55 -07001717 if ((mode & ISOLATE_UNMAPPED) && page_mapped(page))
Alex Shic2135f72021-02-24 12:08:01 -08001718 return false;
Minchan Kimf80c0672011-10-31 17:06:55 -07001719
Alex Shic2135f72021-02-24 12:08:01 -08001720 return true;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001721}
1722
Mel Gorman7ee36a12016-07-28 15:47:17 -07001723/*
1724 * Update LRU sizes after isolating pages. The LRU size updates must
Ethon Paul55b65a52020-06-04 16:49:10 -07001725 * be complete before mem_cgroup_update_lru_size due to a sanity check.
Mel Gorman7ee36a12016-07-28 15:47:17 -07001726 */
1727static __always_inline void update_lru_sizes(struct lruvec *lruvec,
Michal Hockob4536f0c82017-01-10 16:58:04 -08001728 enum lru_list lru, unsigned long *nr_zone_taken)
Mel Gorman7ee36a12016-07-28 15:47:17 -07001729{
Mel Gorman7ee36a12016-07-28 15:47:17 -07001730 int zid;
1731
Mel Gorman7ee36a12016-07-28 15:47:17 -07001732 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
1733 if (!nr_zone_taken[zid])
1734 continue;
1735
Wei Yanga892cb62020-06-03 16:01:12 -07001736 update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]);
Mel Gorman7ee36a12016-07-28 15:47:17 -07001737 }
Mel Gorman7ee36a12016-07-28 15:47:17 -07001738
Mel Gorman7ee36a12016-07-28 15:47:17 -07001739}
1740
Andrey Ryabininf4b7e272019-03-05 15:49:39 -08001741/**
Hugh Dickins15b44732020-12-15 14:21:31 -08001742 * Isolating page from the lruvec to fill in @dst list by nr_to_scan times.
1743 *
1744 * lruvec->lru_lock is heavily contended. Some of the functions that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 * shrink the lists perform better by taking out a batch of pages
1746 * and working on them outside the LRU lock.
1747 *
1748 * For pagecache intensive workloads, this function is the hottest
1749 * spot in the kernel (apart from copy_*_user functions).
1750 *
Hugh Dickins15b44732020-12-15 14:21:31 -08001751 * Lru_lock must be held before calling this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 *
Minchan Kim791b48b2017-05-12 15:47:06 -07001753 * @nr_to_scan: The number of eligible pages to look through on the list.
Konstantin Khlebnikov5dc35972012-05-29 15:06:58 -07001754 * @lruvec: The LRU vector to pull pages from.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 * @dst: The temp list to put pages on to.
Hugh Dickinsf6260122012-01-12 17:20:06 -08001756 * @nr_scanned: The number of pages that were scanned.
Rik van Rielfe2c2a12012-03-21 16:33:51 -07001757 * @sc: The scan_control struct for this reclaim session
Konstantin Khlebnikov3cb99452012-05-29 15:06:53 -07001758 * @lru: LRU list id for isolating
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 *
1760 * returns how many pages were moved onto *@dst.
1761 */
Andrew Morton69e05942006-03-22 00:08:19 -08001762static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
Konstantin Khlebnikov5dc35972012-05-29 15:06:58 -07001763 struct lruvec *lruvec, struct list_head *dst,
Rik van Rielfe2c2a12012-03-21 16:33:51 -07001764 unsigned long *nr_scanned, struct scan_control *sc,
Kirill Tkhaia9e7c392019-03-05 15:46:55 -08001765 enum lru_list lru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
Hugh Dickins75b00af2012-05-29 15:07:09 -07001767 struct list_head *src = &lruvec->lists[lru];
Andrew Morton69e05942006-03-22 00:08:19 -08001768 unsigned long nr_taken = 0;
Mel Gorman599d0c92016-07-28 15:45:31 -07001769 unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 };
Mel Gorman7cc30fc2016-07-28 15:46:59 -07001770 unsigned long nr_skipped[MAX_NR_ZONES] = { 0, };
Johannes Weiner3db65812017-05-03 14:52:13 -07001771 unsigned long skipped = 0;
Minchan Kim791b48b2017-05-12 15:47:06 -07001772 unsigned long scan, total_scan, nr_pages;
Mel Gormanb2e18752016-07-28 15:45:37 -07001773 LIST_HEAD(pages_skipped);
Kirill Tkhaia9e7c392019-03-05 15:46:55 -08001774 isolate_mode_t mode = (sc->may_unmap ? 0 : ISOLATE_UNMAPPED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Yang Shi98879b32019-07-11 20:59:30 -07001776 total_scan = 0;
Minchan Kim791b48b2017-05-12 15:47:06 -07001777 scan = 0;
Yang Shi98879b32019-07-11 20:59:30 -07001778 while (scan < nr_to_scan && !list_empty(src)) {
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001779 struct page *page;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 page = lru_to_page(src);
1782 prefetchw_prev_lru_page(page, src, flags);
1783
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -07001784 nr_pages = compound_nr(page);
Yang Shi98879b32019-07-11 20:59:30 -07001785 total_scan += nr_pages;
1786
Mel Gormanb2e18752016-07-28 15:45:37 -07001787 if (page_zonenum(page) > sc->reclaim_idx) {
1788 list_move(&page->lru, &pages_skipped);
Yang Shi98879b32019-07-11 20:59:30 -07001789 nr_skipped[page_zonenum(page)] += nr_pages;
Mel Gormanb2e18752016-07-28 15:45:37 -07001790 continue;
1791 }
1792
Minchan Kim791b48b2017-05-12 15:47:06 -07001793 /*
1794 * Do not count skipped pages because that makes the function
1795 * return with no isolated pages if the LRU mostly contains
1796 * ineligible pages. This causes the VM to not reclaim any
1797 * pages, triggering a premature OOM.
Yang Shi98879b32019-07-11 20:59:30 -07001798 *
1799 * Account all tail pages of THP. This would not cause
1800 * premature OOM since __isolate_lru_page() returns -EBUSY
1801 * only when the page is being freed somewhere else.
Minchan Kim791b48b2017-05-12 15:47:06 -07001802 */
Yang Shi98879b32019-07-11 20:59:30 -07001803 scan += nr_pages;
Alex Shic2135f72021-02-24 12:08:01 -08001804 if (!__isolate_lru_page_prepare(page, mode)) {
1805 /* It is being freed elsewhere */
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001806 list_move(&page->lru, src);
Alex Shic2135f72021-02-24 12:08:01 -08001807 continue;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001808 }
Alex Shic2135f72021-02-24 12:08:01 -08001809 /*
1810 * Be careful not to clear PageLRU until after we're
1811 * sure the page is not being freed elsewhere -- the
1812 * page release code relies on it.
1813 */
1814 if (unlikely(!get_page_unless_zero(page))) {
1815 list_move(&page->lru, src);
1816 continue;
1817 }
1818
1819 if (!TestClearPageLRU(page)) {
1820 /* Another thread is already isolating this page */
1821 put_page(page);
1822 list_move(&page->lru, src);
1823 continue;
1824 }
1825
1826 nr_taken += nr_pages;
1827 nr_zone_taken[page_zonenum(page)] += nr_pages;
1828 list_move(&page->lru, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 }
1830
Mel Gormanb2e18752016-07-28 15:45:37 -07001831 /*
1832 * Splice any skipped pages to the start of the LRU list. Note that
1833 * this disrupts the LRU order when reclaiming for lower zones but
1834 * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX
1835 * scanning would soon rescan the same pages to skip and put the
1836 * system at risk of premature OOM.
1837 */
Mel Gorman7cc30fc2016-07-28 15:46:59 -07001838 if (!list_empty(&pages_skipped)) {
1839 int zid;
1840
Johannes Weiner3db65812017-05-03 14:52:13 -07001841 list_splice(&pages_skipped, src);
Mel Gorman7cc30fc2016-07-28 15:46:59 -07001842 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
1843 if (!nr_skipped[zid])
1844 continue;
1845
1846 __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]);
Michal Hocko1265e3a2017-02-22 15:44:21 -08001847 skipped += nr_skipped[zid];
Mel Gorman7cc30fc2016-07-28 15:46:59 -07001848 }
1849 }
Minchan Kim791b48b2017-05-12 15:47:06 -07001850 *nr_scanned = total_scan;
Michal Hocko1265e3a2017-02-22 15:44:21 -08001851 trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan,
Minchan Kim791b48b2017-05-12 15:47:06 -07001852 total_scan, skipped, nr_taken, mode, lru);
Michal Hockob4536f0c82017-01-10 16:58:04 -08001853 update_lru_sizes(lruvec, lru, nr_zone_taken);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 return nr_taken;
1855}
1856
Nick Piggin62695a82008-10-18 20:26:09 -07001857/**
1858 * isolate_lru_page - tries to isolate a page from its LRU list
1859 * @page: page to isolate from its LRU list
1860 *
1861 * Isolates a @page from an LRU list, clears PageLRU and adjusts the
1862 * vmstat statistic corresponding to whatever LRU list the page was on.
1863 *
1864 * Returns 0 if the page was removed from an LRU list.
1865 * Returns -EBUSY if the page was not on an LRU list.
1866 *
1867 * The returned page will have PageLRU() cleared. If it was found on
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001868 * the active list, it will have PageActive set. If it was found on
1869 * the unevictable list, it will have the PageUnevictable bit set. That flag
1870 * may need to be cleared by the caller before letting the page go.
Nick Piggin62695a82008-10-18 20:26:09 -07001871 *
1872 * The vmstat statistic corresponding to the list on which the page was
1873 * found will be decremented.
1874 *
1875 * Restrictions:
Mike Rapoporta5d09be2018-02-06 15:42:19 -08001876 *
Nick Piggin62695a82008-10-18 20:26:09 -07001877 * (1) Must be called with an elevated refcount on the page. This is a
Hui Su01c47762020-10-13 16:56:49 -07001878 * fundamental difference from isolate_lru_pages (which is called
Nick Piggin62695a82008-10-18 20:26:09 -07001879 * without a stable reference).
1880 * (2) the lru_lock must not be held.
1881 * (3) interrupts must be enabled.
1882 */
1883int isolate_lru_page(struct page *page)
1884{
1885 int ret = -EBUSY;
1886
Sasha Levin309381fea2014-01-23 15:52:54 -08001887 VM_BUG_ON_PAGE(!page_count(page), page);
Kirill A. Shutemovcf2a82e2016-02-05 15:36:36 -08001888 WARN_RATELIMIT(PageTail(page), "trying to isolate tail page");
Konstantin Khlebnikov0c917312011-05-24 17:12:21 -07001889
Alex Shid25b5bd2020-12-15 12:34:16 -08001890 if (TestClearPageLRU(page)) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001891 struct lruvec *lruvec;
Nick Piggin62695a82008-10-18 20:26:09 -07001892
Alex Shid25b5bd2020-12-15 12:34:16 -08001893 get_page(page);
Alex Shi6168d0d2020-12-15 12:34:29 -08001894 lruvec = lock_page_lruvec_irq(page);
Yu Zhao46ae6b22021-02-24 12:08:25 -08001895 del_page_from_lru_list(page, lruvec);
Alex Shi6168d0d2020-12-15 12:34:29 -08001896 unlock_page_lruvec_irq(lruvec);
Alex Shid25b5bd2020-12-15 12:34:16 -08001897 ret = 0;
Nick Piggin62695a82008-10-18 20:26:09 -07001898 }
Alex Shid25b5bd2020-12-15 12:34:16 -08001899
Nick Piggin62695a82008-10-18 20:26:09 -07001900 return ret;
1901}
1902
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001903/*
Fengguang Wud37dd5d2012-12-18 14:23:28 -08001904 * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and
Xianting Tian178821b2019-11-30 17:56:05 -08001905 * then get rescheduled. When there are massive number of tasks doing page
Fengguang Wud37dd5d2012-12-18 14:23:28 -08001906 * allocation, such sleeping direct reclaimers may keep piling up on each CPU,
1907 * the LRU list will go small and be scanned faster than necessary, leading to
1908 * unnecessary swapping, thrashing and OOM.
Rik van Riel35cd7812009-09-21 17:01:38 -07001909 */
Mel Gorman599d0c92016-07-28 15:45:31 -07001910static int too_many_isolated(struct pglist_data *pgdat, int file,
Rik van Riel35cd7812009-09-21 17:01:38 -07001911 struct scan_control *sc)
1912{
1913 unsigned long inactive, isolated;
1914
1915 if (current_is_kswapd())
1916 return 0;
1917
Johannes Weinerb5ead352019-11-30 17:55:40 -08001918 if (!writeback_throttling_sane(sc))
Rik van Riel35cd7812009-09-21 17:01:38 -07001919 return 0;
1920
1921 if (file) {
Mel Gorman599d0c92016-07-28 15:45:31 -07001922 inactive = node_page_state(pgdat, NR_INACTIVE_FILE);
1923 isolated = node_page_state(pgdat, NR_ISOLATED_FILE);
Rik van Riel35cd7812009-09-21 17:01:38 -07001924 } else {
Mel Gorman599d0c92016-07-28 15:45:31 -07001925 inactive = node_page_state(pgdat, NR_INACTIVE_ANON);
1926 isolated = node_page_state(pgdat, NR_ISOLATED_ANON);
Rik van Riel35cd7812009-09-21 17:01:38 -07001927 }
1928
Fengguang Wu3cf23842012-12-18 14:23:31 -08001929 /*
1930 * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they
1931 * won't get blocked by normal direct-reclaimers, forming a circular
1932 * deadlock.
1933 */
Mel Gormand0164ad2015-11-06 16:28:21 -08001934 if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS))
Fengguang Wu3cf23842012-12-18 14:23:31 -08001935 inactive >>= 3;
1936
Rik van Riel35cd7812009-09-21 17:01:38 -07001937 return isolated > inactive;
1938}
1939
Kirill Tkhaia222f342019-05-13 17:17:00 -07001940/*
Hugh Dickins15b44732020-12-15 14:21:31 -08001941 * move_pages_to_lru() moves pages from private @list to appropriate LRU list.
1942 * On return, @list is reused as a list of pages to be freed by the caller.
Kirill Tkhaia222f342019-05-13 17:17:00 -07001943 *
1944 * Returns the number of pages moved to the given lruvec.
1945 */
Kirill Tkhaia222f342019-05-13 17:17:00 -07001946static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec,
1947 struct list_head *list)
Mel Gorman66635622010-08-09 17:19:30 -07001948{
Kirill Tkhaia222f342019-05-13 17:17:00 -07001949 int nr_pages, nr_moved = 0;
Hugh Dickins3f797682012-01-12 17:20:07 -08001950 LIST_HEAD(pages_to_free);
Kirill Tkhaia222f342019-05-13 17:17:00 -07001951 struct page *page;
Mel Gorman66635622010-08-09 17:19:30 -07001952
Kirill Tkhaia222f342019-05-13 17:17:00 -07001953 while (!list_empty(list)) {
1954 page = lru_to_page(list);
Sasha Levin309381fea2014-01-23 15:52:54 -08001955 VM_BUG_ON_PAGE(PageLRU(page), page);
Alex Shi3d06afa2020-12-15 12:33:37 -08001956 list_del(&page->lru);
Hugh Dickins39b5f292012-10-08 16:33:18 -07001957 if (unlikely(!page_evictable(page))) {
Alex Shi6168d0d2020-12-15 12:34:29 -08001958 spin_unlock_irq(&lruvec->lru_lock);
Mel Gorman66635622010-08-09 17:19:30 -07001959 putback_lru_page(page);
Alex Shi6168d0d2020-12-15 12:34:29 -08001960 spin_lock_irq(&lruvec->lru_lock);
Mel Gorman66635622010-08-09 17:19:30 -07001961 continue;
1962 }
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001963
Alex Shi3d06afa2020-12-15 12:33:37 -08001964 /*
1965 * The SetPageLRU needs to be kept here for list integrity.
1966 * Otherwise:
1967 * #0 move_pages_to_lru #1 release_pages
1968 * if !put_page_testzero
1969 * if (put_page_testzero())
1970 * !PageLRU //skip lru_lock
1971 * SetPageLRU()
1972 * list_add(&page->lru,)
1973 * list_add(&page->lru,)
1974 */
Linus Torvalds7a608572011-01-17 14:42:19 -08001975 SetPageLRU(page);
Kirill Tkhaia222f342019-05-13 17:17:00 -07001976
Alex Shi3d06afa2020-12-15 12:33:37 -08001977 if (unlikely(put_page_testzero(page))) {
Yu Zhao87560172021-02-24 12:08:28 -08001978 __clear_page_lru_flags(page);
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001979
1980 if (unlikely(PageCompound(page))) {
Alex Shi6168d0d2020-12-15 12:34:29 -08001981 spin_unlock_irq(&lruvec->lru_lock);
Matthew Wilcox (Oracle)ff45fc32020-06-03 16:01:09 -07001982 destroy_compound_page(page);
Alex Shi6168d0d2020-12-15 12:34:29 -08001983 spin_lock_irq(&lruvec->lru_lock);
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001984 } else
1985 list_add(&page->lru, &pages_to_free);
Alex Shi3d06afa2020-12-15 12:33:37 -08001986
1987 continue;
Mel Gorman66635622010-08-09 17:19:30 -07001988 }
Alex Shi3d06afa2020-12-15 12:33:37 -08001989
Alex Shiafca9152020-12-15 12:34:02 -08001990 /*
1991 * All pages were isolated from the same lruvec (and isolation
1992 * inhibits memcg migration).
1993 */
Alexander Duyck2a5e4e32020-12-15 12:34:33 -08001994 VM_BUG_ON_PAGE(!lruvec_holds_page_lru_lock(page, lruvec), page);
Yu Zhao3a9c9782021-02-24 12:08:17 -08001995 add_page_to_lru_list(page, lruvec);
Alex Shi3d06afa2020-12-15 12:33:37 -08001996 nr_pages = thp_nr_pages(page);
Alex Shi3d06afa2020-12-15 12:33:37 -08001997 nr_moved += nr_pages;
1998 if (PageActive(page))
1999 workingset_age_nonresident(lruvec, nr_pages);
Mel Gorman66635622010-08-09 17:19:30 -07002000 }
Mel Gorman66635622010-08-09 17:19:30 -07002001
Hugh Dickins3f797682012-01-12 17:20:07 -08002002 /*
2003 * To save our caller's stack, now use input list for pages to free.
2004 */
Kirill Tkhaia222f342019-05-13 17:17:00 -07002005 list_splice(&pages_to_free, list);
2006
2007 return nr_moved;
Mel Gorman66635622010-08-09 17:19:30 -07002008}
2009
2010/*
NeilBrown399ba0b2014-06-04 16:07:42 -07002011 * If a kernel thread (such as nfsd for loop-back mounts) services
NeilBrowna37b0712020-06-01 21:48:18 -07002012 * a backing device by writing to the page cache it sets PF_LOCAL_THROTTLE.
NeilBrown399ba0b2014-06-04 16:07:42 -07002013 * In that case we should only throttle if the backing device it is
2014 * writing to is congested. In other cases it is safe to throttle.
2015 */
2016static int current_may_throttle(void)
2017{
NeilBrowna37b0712020-06-01 21:48:18 -07002018 return !(current->flags & PF_LOCAL_THROTTLE) ||
NeilBrown399ba0b2014-06-04 16:07:42 -07002019 current->backing_dev_info == NULL ||
2020 bdi_write_congested(current->backing_dev_info);
2021}
2022
2023/*
Mel Gormanb2e18752016-07-28 15:45:37 -07002024 * shrink_inactive_list() is a helper for shrink_node(). It returns the number
Andrew Morton1742f192006-03-22 00:08:21 -08002025 * of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 */
Mel Gorman66635622010-08-09 17:19:30 -07002027static noinline_for_stack unsigned long
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07002028shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec,
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002029 struct scan_control *sc, enum lru_list lru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
2031 LIST_HEAD(page_list);
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07002032 unsigned long nr_scanned;
Maninder Singh730ec8c2020-06-03 16:01:18 -07002033 unsigned int nr_reclaimed = 0;
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07002034 unsigned long nr_taken;
Kirill Tkhai060f0052019-03-05 15:48:15 -08002035 struct reclaim_stat stat;
Johannes Weiner497a6c12020-06-03 16:02:34 -07002036 bool file = is_file_lru(lru);
Kirill Tkhaif46b7912019-05-13 17:22:33 -07002037 enum vm_event_item item;
Mel Gorman599d0c92016-07-28 15:45:31 -07002038 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
Michal Hockodb73ee02017-09-06 16:21:11 -07002039 bool stalled = false;
KOSAKI Motohiro78dc5832009-06-16 15:31:40 -07002040
Mel Gorman599d0c92016-07-28 15:45:31 -07002041 while (unlikely(too_many_isolated(pgdat, file, sc))) {
Michal Hockodb73ee02017-09-06 16:21:11 -07002042 if (stalled)
2043 return 0;
2044
2045 /* wait a bit for the reclaimer. */
2046 msleep(100);
2047 stalled = true;
Rik van Riel35cd7812009-09-21 17:01:38 -07002048
2049 /* We are about to die and free our memory. Return now. */
2050 if (fatal_signal_pending(current))
2051 return SWAP_CLUSTER_MAX;
2052 }
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 lru_add_drain();
Minchan Kimf80c0672011-10-31 17:06:55 -07002055
Alex Shi6168d0d2020-12-15 12:34:29 -08002056 spin_lock_irq(&lruvec->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Konstantin Khlebnikov5dc35972012-05-29 15:06:58 -07002058 nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list,
Kirill Tkhaia9e7c392019-03-05 15:46:55 -08002059 &nr_scanned, sc, lru);
Konstantin Khlebnikov95d918f2012-05-29 15:06:59 -07002060
Mel Gorman599d0c92016-07-28 15:45:31 -07002061 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
Kirill Tkhaif46b7912019-05-13 17:22:33 -07002062 item = current_is_kswapd() ? PGSCAN_KSWAPD : PGSCAN_DIRECT;
Johannes Weinerb5ead352019-11-30 17:55:40 -08002063 if (!cgroup_reclaim(sc))
Kirill Tkhaif46b7912019-05-13 17:22:33 -07002064 __count_vm_events(item, nr_scanned);
2065 __count_memcg_events(lruvec_memcg(lruvec), item, nr_scanned);
Johannes Weiner497a6c12020-06-03 16:02:34 -07002066 __count_vm_events(PGSCAN_ANON + file, nr_scanned);
2067
Alex Shi6168d0d2020-12-15 12:34:29 -08002068 spin_unlock_irq(&lruvec->lru_lock);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07002069
Hillf Dantond563c052012-03-21 16:34:02 -07002070 if (nr_taken == 0)
Mel Gorman66635622010-08-09 17:19:30 -07002071 return 0;
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07002072
Shakeel Butt013339d2020-12-14 19:06:39 -08002073 nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, &stat, false);
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07002074
Alex Shi6168d0d2020-12-15 12:34:29 -08002075 spin_lock_irq(&lruvec->lru_lock);
Johannes Weiner497a6c12020-06-03 16:02:34 -07002076 move_pages_to_lru(lruvec, &page_list);
2077
2078 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken);
Kirill Tkhaif46b7912019-05-13 17:22:33 -07002079 item = current_is_kswapd() ? PGSTEAL_KSWAPD : PGSTEAL_DIRECT;
Johannes Weinerb5ead352019-11-30 17:55:40 -08002080 if (!cgroup_reclaim(sc))
Kirill Tkhaif46b7912019-05-13 17:22:33 -07002081 __count_vm_events(item, nr_reclaimed);
2082 __count_memcg_events(lruvec_memcg(lruvec), item, nr_reclaimed);
Johannes Weiner497a6c12020-06-03 16:02:34 -07002083 __count_vm_events(PGSTEAL_ANON + file, nr_reclaimed);
Alex Shi6168d0d2020-12-15 12:34:29 -08002084 spin_unlock_irq(&lruvec->lru_lock);
Hugh Dickins3f797682012-01-12 17:20:07 -08002085
Alex Shi75cc3c92020-12-15 14:20:50 -08002086 lru_note_cost(lruvec, file, stat.nr_pageout);
Johannes Weiner747db952014-08-08 14:19:24 -07002087 mem_cgroup_uncharge_list(&page_list);
Mel Gorman2d4894b2017-11-15 17:37:59 -08002088 free_unref_page_list(&page_list);
Mel Gormane11da5b2010-10-26 14:21:40 -07002089
Mel Gorman92df3a72011-10-31 17:07:56 -07002090 /*
Andrey Ryabinin1c610d52018-03-22 16:17:42 -07002091 * If dirty pages are scanned that are not queued for IO, it
2092 * implies that flushers are not doing their job. This can
2093 * happen when memory pressure pushes dirty pages to the end of
2094 * the LRU before the dirty limits are breached and the dirty
2095 * data has expired. It can also happen when the proportion of
2096 * dirty pages grows not through writes but through memory
2097 * pressure reclaiming all the clean cache. And in some cases,
2098 * the flushers simply cannot keep up with the allocation
2099 * rate. Nudge the flusher threads in case they are asleep.
2100 */
2101 if (stat.nr_unqueued_dirty == nr_taken)
2102 wakeup_flusher_threads(WB_REASON_VMSCAN);
2103
Andrey Ryabinind108c772018-04-10 16:27:59 -07002104 sc->nr.dirty += stat.nr_dirty;
2105 sc->nr.congested += stat.nr_congested;
2106 sc->nr.unqueued_dirty += stat.nr_unqueued_dirty;
2107 sc->nr.writeback += stat.nr_writeback;
2108 sc->nr.immediate += stat.nr_immediate;
2109 sc->nr.taken += nr_taken;
2110 if (file)
2111 sc->nr.file_taken += nr_taken;
Mel Gorman8e950282013-07-03 15:02:02 -07002112
Mel Gorman599d0c92016-07-28 15:45:31 -07002113 trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id,
Steven Rostedtd51d1e62018-04-10 16:28:07 -07002114 nr_scanned, nr_reclaimed, &stat, sc->priority, file);
Andrew Morton05ff5132006-03-22 00:08:20 -08002115 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116}
2117
Hugh Dickins15b44732020-12-15 14:21:31 -08002118/*
2119 * shrink_active_list() moves pages from the active LRU to the inactive LRU.
2120 *
2121 * We move them the other way if the page is referenced by one or more
2122 * processes.
2123 *
2124 * If the pages are mostly unmapped, the processing is fast and it is
2125 * appropriate to hold lru_lock across the whole operation. But if
2126 * the pages are mapped, the processing is slow (page_referenced()), so
2127 * we should drop lru_lock around each page. It's impossible to balance
2128 * this, so instead we remove the pages from the LRU while processing them.
2129 * It is safe to rely on PG_active against the non-LRU pages in here because
2130 * nobody will play with that bit on a non-LRU page.
2131 *
2132 * The downside is that we have to touch page->_refcount against each page.
2133 * But we had to alter page->flags anyway.
2134 */
Hugh Dickinsf6260122012-01-12 17:20:06 -08002135static void shrink_active_list(unsigned long nr_to_scan,
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07002136 struct lruvec *lruvec,
Johannes Weinerf16015f2012-01-12 17:17:52 -08002137 struct scan_control *sc,
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002138 enum lru_list lru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
KOSAKI Motohiro44c241f2009-09-21 17:01:35 -07002140 unsigned long nr_taken;
Hugh Dickinsf6260122012-01-12 17:20:06 -08002141 unsigned long nr_scanned;
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -07002142 unsigned long vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 LIST_HEAD(l_hold); /* The pages which were snipped off */
Wu Fengguang8cab4752009-06-16 15:33:12 -07002144 LIST_HEAD(l_active);
Christoph Lameterb69408e2008-10-18 20:26:14 -07002145 LIST_HEAD(l_inactive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 struct page *page;
Michal Hocko9d998b42017-02-22 15:44:18 -08002147 unsigned nr_deactivate, nr_activate;
2148 unsigned nr_rotated = 0;
Konstantin Khlebnikov3cb99452012-05-29 15:06:53 -07002149 int file = is_file_lru(lru);
Mel Gorman599d0c92016-07-28 15:45:31 -07002150 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152 lru_add_drain();
Minchan Kimf80c0672011-10-31 17:06:55 -07002153
Alex Shi6168d0d2020-12-15 12:34:29 -08002154 spin_lock_irq(&lruvec->lru_lock);
Johannes Weiner925b7672012-01-12 17:18:15 -08002155
Konstantin Khlebnikov5dc35972012-05-29 15:06:58 -07002156 nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold,
Kirill Tkhaia9e7c392019-03-05 15:46:55 -08002157 &nr_scanned, sc, lru);
Johannes Weiner89b5fae2012-01-12 17:17:50 -08002158
Mel Gorman599d0c92016-07-28 15:45:31 -07002159 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08002160
Shakeel Butt912c0572020-08-06 23:26:32 -07002161 if (!cgroup_reclaim(sc))
2162 __count_vm_events(PGREFILL, nr_scanned);
Yafang Shao2fa26902019-05-13 17:23:02 -07002163 __count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned);
Hugh Dickins9d5e6a92016-05-19 17:12:38 -07002164
Alex Shi6168d0d2020-12-15 12:34:29 -08002165 spin_unlock_irq(&lruvec->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 while (!list_empty(&l_hold)) {
2168 cond_resched();
2169 page = lru_to_page(&l_hold);
2170 list_del(&page->lru);
Rik van Riel7e9cd482008-10-18 20:26:35 -07002171
Hugh Dickins39b5f292012-10-08 16:33:18 -07002172 if (unlikely(!page_evictable(page))) {
Lee Schermerhorn894bc312008-10-18 20:26:39 -07002173 putback_lru_page(page);
2174 continue;
2175 }
2176
Mel Gormancc715d92012-03-21 16:34:00 -07002177 if (unlikely(buffer_heads_over_limit)) {
2178 if (page_has_private(page) && trylock_page(page)) {
2179 if (page_has_private(page))
2180 try_to_release_page(page, 0);
2181 unlock_page(page);
2182 }
2183 }
2184
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07002185 if (page_referenced(page, 0, sc->target_mem_cgroup,
2186 &vm_flags)) {
Wu Fengguang8cab4752009-06-16 15:33:12 -07002187 /*
2188 * Identify referenced, file-backed active pages and
2189 * give them one more trip around the active list. So
2190 * that executable code get better chances to stay in
2191 * memory under moderate memory pressure. Anon pages
2192 * are not likely to be evicted by use-once streaming
2193 * IO, plus JVM can create lots of anon VM_EXEC pages,
2194 * so we ignore them here.
2195 */
Huang Ying9de4f222020-04-06 20:04:41 -07002196 if ((vm_flags & VM_EXEC) && page_is_file_lru(page)) {
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -07002197 nr_rotated += thp_nr_pages(page);
Wu Fengguang8cab4752009-06-16 15:33:12 -07002198 list_add(&page->lru, &l_active);
2199 continue;
2200 }
2201 }
Rik van Riel7e9cd482008-10-18 20:26:35 -07002202
KOSAKI Motohiro5205e562009-09-21 17:01:44 -07002203 ClearPageActive(page); /* we are de-activating */
Johannes Weiner1899ad12018-10-26 15:06:04 -07002204 SetPageWorkingset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 list_add(&page->lru, &l_inactive);
2206 }
2207
Andrew Mortonb5557492009-01-06 14:40:13 -08002208 /*
Wu Fengguang8cab4752009-06-16 15:33:12 -07002209 * Move pages back to the lru list.
Andrew Mortonb5557492009-01-06 14:40:13 -08002210 */
Alex Shi6168d0d2020-12-15 12:34:29 -08002211 spin_lock_irq(&lruvec->lru_lock);
Rik van Riel556adec2008-10-18 20:26:34 -07002212
Kirill Tkhaia222f342019-05-13 17:17:00 -07002213 nr_activate = move_pages_to_lru(lruvec, &l_active);
2214 nr_deactivate = move_pages_to_lru(lruvec, &l_inactive);
Kirill Tkhaif372d892019-05-13 17:16:57 -07002215 /* Keep all free pages in l_active list */
2216 list_splice(&l_inactive, &l_active);
Kirill Tkhai9851ac12019-05-13 17:16:54 -07002217
2218 __count_vm_events(PGDEACTIVATE, nr_deactivate);
2219 __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, nr_deactivate);
2220
Mel Gorman599d0c92016-07-28 15:45:31 -07002221 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken);
Alex Shi6168d0d2020-12-15 12:34:29 -08002222 spin_unlock_irq(&lruvec->lru_lock);
Hugh Dickins2bcf8872012-01-12 17:19:56 -08002223
Kirill Tkhaif372d892019-05-13 17:16:57 -07002224 mem_cgroup_uncharge_list(&l_active);
2225 free_unref_page_list(&l_active);
Michal Hocko9d998b42017-02-22 15:44:18 -08002226 trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate,
2227 nr_deactivate, nr_rotated, sc->priority, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228}
2229
Minchan Kim1a4e58c2019-09-25 16:49:15 -07002230unsigned long reclaim_pages(struct list_head *page_list)
2231{
Yang Shif661d002020-04-01 21:10:05 -07002232 int nid = NUMA_NO_NODE;
Maninder Singh730ec8c2020-06-03 16:01:18 -07002233 unsigned int nr_reclaimed = 0;
Minchan Kim1a4e58c2019-09-25 16:49:15 -07002234 LIST_HEAD(node_page_list);
2235 struct reclaim_stat dummy_stat;
2236 struct page *page;
2237 struct scan_control sc = {
2238 .gfp_mask = GFP_KERNEL,
2239 .priority = DEF_PRIORITY,
2240 .may_writepage = 1,
2241 .may_unmap = 1,
2242 .may_swap = 1,
2243 };
2244
2245 while (!list_empty(page_list)) {
2246 page = lru_to_page(page_list);
Yang Shif661d002020-04-01 21:10:05 -07002247 if (nid == NUMA_NO_NODE) {
Minchan Kim1a4e58c2019-09-25 16:49:15 -07002248 nid = page_to_nid(page);
2249 INIT_LIST_HEAD(&node_page_list);
2250 }
2251
2252 if (nid == page_to_nid(page)) {
2253 ClearPageActive(page);
2254 list_move(&page->lru, &node_page_list);
2255 continue;
2256 }
2257
2258 nr_reclaimed += shrink_page_list(&node_page_list,
2259 NODE_DATA(nid),
Shakeel Butt013339d2020-12-14 19:06:39 -08002260 &sc, &dummy_stat, false);
Minchan Kim1a4e58c2019-09-25 16:49:15 -07002261 while (!list_empty(&node_page_list)) {
2262 page = lru_to_page(&node_page_list);
2263 list_del(&page->lru);
2264 putback_lru_page(page);
2265 }
2266
Yang Shif661d002020-04-01 21:10:05 -07002267 nid = NUMA_NO_NODE;
Minchan Kim1a4e58c2019-09-25 16:49:15 -07002268 }
2269
2270 if (!list_empty(&node_page_list)) {
2271 nr_reclaimed += shrink_page_list(&node_page_list,
2272 NODE_DATA(nid),
Shakeel Butt013339d2020-12-14 19:06:39 -08002273 &sc, &dummy_stat, false);
Minchan Kim1a4e58c2019-09-25 16:49:15 -07002274 while (!list_empty(&node_page_list)) {
2275 page = lru_to_page(&node_page_list);
2276 list_del(&page->lru);
2277 putback_lru_page(page);
2278 }
2279 }
2280
2281 return nr_reclaimed;
2282}
2283
Johannes Weinerb91ac372019-11-30 17:56:02 -08002284static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
2285 struct lruvec *lruvec, struct scan_control *sc)
2286{
2287 if (is_active_lru(lru)) {
2288 if (sc->may_deactivate & (1 << is_file_lru(lru)))
2289 shrink_active_list(nr_to_scan, lruvec, sc, lru);
2290 else
2291 sc->skipped_deactivate = 1;
2292 return 0;
2293 }
2294
2295 return shrink_inactive_list(nr_to_scan, lruvec, sc, lru);
2296}
2297
Rik van Riel59dc76b2016-05-20 16:56:31 -07002298/*
2299 * The inactive anon list should be small enough that the VM never has
2300 * to do too much work.
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002301 *
Rik van Riel59dc76b2016-05-20 16:56:31 -07002302 * The inactive file list should be small enough to leave most memory
2303 * to the established workingset on the scan-resistant active list,
2304 * but large enough to avoid thrashing the aggregate readahead window.
2305 *
2306 * Both inactive lists should also be large enough that each inactive
2307 * page has a chance to be referenced again before it is reclaimed.
2308 *
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002309 * If that fails and refaulting is observed, the inactive list grows.
2310 *
Rik van Riel59dc76b2016-05-20 16:56:31 -07002311 * The inactive_ratio is the target ratio of ACTIVE to INACTIVE pages
Andrey Ryabinin3a50d142017-11-15 17:34:15 -08002312 * on this LRU, maintained by the pageout code. An inactive_ratio
Rik van Riel59dc76b2016-05-20 16:56:31 -07002313 * of 3 means 3:1 or 25% of the pages are kept on the inactive list.
2314 *
2315 * total target max
2316 * memory ratio inactive
2317 * -------------------------------------
2318 * 10MB 1 5MB
2319 * 100MB 1 50MB
2320 * 1GB 3 250MB
2321 * 10GB 10 0.9GB
2322 * 100GB 31 3GB
2323 * 1TB 101 10GB
2324 * 10TB 320 32GB
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002325 */
Johannes Weinerb91ac372019-11-30 17:56:02 -08002326static bool inactive_is_low(struct lruvec *lruvec, enum lru_list inactive_lru)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002327{
Johannes Weinerb91ac372019-11-30 17:56:02 -08002328 enum lru_list active_lru = inactive_lru + LRU_ACTIVE;
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002329 unsigned long inactive, active;
2330 unsigned long inactive_ratio;
Rik van Riel59dc76b2016-05-20 16:56:31 -07002331 unsigned long gb;
2332
Johannes Weinerb91ac372019-11-30 17:56:02 -08002333 inactive = lruvec_page_state(lruvec, NR_LRU_BASE + inactive_lru);
2334 active = lruvec_page_state(lruvec, NR_LRU_BASE + active_lru);
Mel Gormanf8d1a312016-07-28 15:47:34 -07002335
Johannes Weinerb91ac372019-11-30 17:56:02 -08002336 gb = (inactive + active) >> (30 - PAGE_SHIFT);
Joonsoo Kim40025702020-08-11 18:30:54 -07002337 if (gb)
Johannes Weinerb91ac372019-11-30 17:56:02 -08002338 inactive_ratio = int_sqrt(10 * gb);
2339 else
2340 inactive_ratio = 1;
Michal Hockofd538802017-02-22 15:45:58 -08002341
Rik van Riel59dc76b2016-05-20 16:56:31 -07002342 return inactive * inactive_ratio < active;
Rik van Rielb39415b2009-12-14 17:59:48 -08002343}
2344
Johannes Weiner9a265112013-02-22 16:32:17 -08002345enum scan_balance {
2346 SCAN_EQUAL,
2347 SCAN_FRACT,
2348 SCAN_ANON,
2349 SCAN_FILE,
2350};
2351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352/*
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002353 * Determine how aggressively the anon and file LRU lists should be
2354 * scanned. The relative value of each set of LRU lists is determined
2355 * by looking at the fraction of the pages scanned we did rotate back
2356 * onto the active list instead of evict.
2357 *
Wanpeng Libe7bd592012-06-14 20:41:02 +08002358 * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan
2359 * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002360 */
Johannes Weinerafaf07a2019-11-30 17:55:46 -08002361static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
2362 unsigned long *nr)
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002363{
Johannes Weinerafaf07a2019-11-30 17:55:46 -08002364 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
Johannes Weinerd483a5d2020-06-03 16:03:13 -07002365 unsigned long anon_cost, file_cost, total_cost;
Vladimir Davydov33377672016-01-20 15:02:59 -08002366 int swappiness = mem_cgroup_swappiness(memcg);
Yu Zhaoed017372020-10-15 20:09:55 -07002367 u64 fraction[ANON_AND_FILE];
Johannes Weiner9a265112013-02-22 16:32:17 -08002368 u64 denominator = 0; /* gcc */
Johannes Weiner9a265112013-02-22 16:32:17 -08002369 enum scan_balance scan_balance;
Johannes Weiner9a265112013-02-22 16:32:17 -08002370 unsigned long ap, fp;
2371 enum lru_list lru;
Shaohua Li76a33fc2010-05-24 14:32:36 -07002372
2373 /* If we have no swap space, do not bother scanning anon pages. */
Vladimir Davydovd8b38432016-01-20 15:03:07 -08002374 if (!sc->may_swap || mem_cgroup_get_nr_swap_pages(memcg) <= 0) {
Johannes Weiner9a265112013-02-22 16:32:17 -08002375 scan_balance = SCAN_FILE;
Shaohua Li76a33fc2010-05-24 14:32:36 -07002376 goto out;
2377 }
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002378
Johannes Weiner10316b32013-02-22 16:32:14 -08002379 /*
2380 * Global reclaim will swap to prevent OOM even with no
2381 * swappiness, but memcg users want to use this knob to
2382 * disable swapping for individual groups completely when
2383 * using the memory controller's swap limit feature would be
2384 * too expensive.
2385 */
Johannes Weinerb5ead352019-11-30 17:55:40 -08002386 if (cgroup_reclaim(sc) && !swappiness) {
Johannes Weiner9a265112013-02-22 16:32:17 -08002387 scan_balance = SCAN_FILE;
Johannes Weiner10316b32013-02-22 16:32:14 -08002388 goto out;
2389 }
2390
2391 /*
2392 * Do not apply any pressure balancing cleverness when the
2393 * system is close to OOM, scan both anon and file equally
2394 * (unless the swappiness setting disagrees with swapping).
2395 */
Johannes Weiner02695172014-08-06 16:06:17 -07002396 if (!sc->priority && swappiness) {
Johannes Weiner9a265112013-02-22 16:32:17 -08002397 scan_balance = SCAN_EQUAL;
Johannes Weiner10316b32013-02-22 16:32:14 -08002398 goto out;
2399 }
2400
Johannes Weiner11d16c22013-02-22 16:32:15 -08002401 /*
Johannes Weiner53138ce2019-11-30 17:55:56 -08002402 * If the system is almost out of file pages, force-scan anon.
Johannes Weiner62376252014-05-06 12:50:07 -07002403 */
Johannes Weinerb91ac372019-11-30 17:56:02 -08002404 if (sc->file_is_tiny) {
Johannes Weiner53138ce2019-11-30 17:55:56 -08002405 scan_balance = SCAN_ANON;
2406 goto out;
Johannes Weiner62376252014-05-06 12:50:07 -07002407 }
2408
2409 /*
Johannes Weinerb91ac372019-11-30 17:56:02 -08002410 * If there is enough inactive page cache, we do not reclaim
2411 * anything from the anonymous working right now.
Johannes Weiner7c5bd702013-02-22 16:32:10 -08002412 */
Johannes Weinerb91ac372019-11-30 17:56:02 -08002413 if (sc->cache_trim_mode) {
Johannes Weiner9a265112013-02-22 16:32:17 -08002414 scan_balance = SCAN_FILE;
Johannes Weiner7c5bd702013-02-22 16:32:10 -08002415 goto out;
2416 }
2417
Johannes Weiner9a265112013-02-22 16:32:17 -08002418 scan_balance = SCAN_FRACT;
Johannes Weiner7c5bd702013-02-22 16:32:10 -08002419 /*
Johannes Weiner314b57f2020-06-03 16:03:03 -07002420 * Calculate the pressure balance between anon and file pages.
2421 *
2422 * The amount of pressure we put on each LRU is inversely
2423 * proportional to the cost of reclaiming each list, as
2424 * determined by the share of pages that are refaulting, times
2425 * the relative IO cost of bringing back a swapped out
2426 * anonymous page vs reloading a filesystem page (swappiness).
2427 *
Johannes Weinerd483a5d2020-06-03 16:03:13 -07002428 * Although we limit that influence to ensure no list gets
2429 * left behind completely: at least a third of the pressure is
2430 * applied, before swappiness.
2431 *
Johannes Weiner314b57f2020-06-03 16:03:03 -07002432 * With swappiness at 100, anon and file have equal IO cost.
KOSAKI Motohiro58c37f62010-08-09 17:19:51 -07002433 */
Johannes Weinerd483a5d2020-06-03 16:03:13 -07002434 total_cost = sc->anon_cost + sc->file_cost;
2435 anon_cost = total_cost + sc->anon_cost;
2436 file_cost = total_cost + sc->file_cost;
2437 total_cost = anon_cost + file_cost;
KOSAKI Motohiro58c37f62010-08-09 17:19:51 -07002438
Johannes Weinerd483a5d2020-06-03 16:03:13 -07002439 ap = swappiness * (total_cost + 1);
2440 ap /= anon_cost + 1;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002441
Johannes Weinerd483a5d2020-06-03 16:03:13 -07002442 fp = (200 - swappiness) * (total_cost + 1);
2443 fp /= file_cost + 1;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002444
Shaohua Li76a33fc2010-05-24 14:32:36 -07002445 fraction[0] = ap;
2446 fraction[1] = fp;
Johannes Weinera4fe1632020-06-03 16:02:50 -07002447 denominator = ap + fp;
Shaohua Li76a33fc2010-05-24 14:32:36 -07002448out:
Johannes Weiner688035f2017-05-03 14:52:07 -07002449 for_each_evictable_lru(lru) {
2450 int file = is_file_lru(lru);
Chris Down9783aa92019-10-06 17:58:32 -07002451 unsigned long lruvec_size;
Johannes Weiner688035f2017-05-03 14:52:07 -07002452 unsigned long scan;
Chris Down1bc63fb2019-10-06 17:58:38 -07002453 unsigned long protection;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002454
Chris Down9783aa92019-10-06 17:58:32 -07002455 lruvec_size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx);
Yafang Shao22f74962020-08-06 23:22:01 -07002456 protection = mem_cgroup_protection(sc->target_mem_cgroup,
2457 memcg,
Chris Down1bc63fb2019-10-06 17:58:38 -07002458 sc->memcg_low_reclaim);
Chris Down9783aa92019-10-06 17:58:32 -07002459
Chris Down1bc63fb2019-10-06 17:58:38 -07002460 if (protection) {
Chris Down9783aa92019-10-06 17:58:32 -07002461 /*
2462 * Scale a cgroup's reclaim pressure by proportioning
2463 * its current usage to its memory.low or memory.min
2464 * setting.
2465 *
2466 * This is important, as otherwise scanning aggression
2467 * becomes extremely binary -- from nothing as we
2468 * approach the memory protection threshold, to totally
2469 * nominal as we exceed it. This results in requiring
2470 * setting extremely liberal protection thresholds. It
2471 * also means we simply get no protection at all if we
2472 * set it too low, which is not ideal.
Chris Down1bc63fb2019-10-06 17:58:38 -07002473 *
2474 * If there is any protection in place, we reduce scan
2475 * pressure by how much of the total memory used is
2476 * within protection thresholds.
Chris Down9783aa92019-10-06 17:58:32 -07002477 *
Chris Down9de7ca42019-10-06 17:58:35 -07002478 * There is one special case: in the first reclaim pass,
2479 * we skip over all groups that are within their low
2480 * protection. If that fails to reclaim enough pages to
2481 * satisfy the reclaim goal, we come back and override
2482 * the best-effort low protection. However, we still
2483 * ideally want to honor how well-behaved groups are in
2484 * that case instead of simply punishing them all
2485 * equally. As such, we reclaim them based on how much
Chris Down1bc63fb2019-10-06 17:58:38 -07002486 * memory they are using, reducing the scan pressure
2487 * again by how much of the total memory used is under
2488 * hard protection.
Chris Down9783aa92019-10-06 17:58:32 -07002489 */
Chris Down1bc63fb2019-10-06 17:58:38 -07002490 unsigned long cgroup_size = mem_cgroup_size(memcg);
2491
2492 /* Avoid TOCTOU with earlier protection check */
2493 cgroup_size = max(cgroup_size, protection);
2494
2495 scan = lruvec_size - lruvec_size * protection /
2496 cgroup_size;
Chris Down9783aa92019-10-06 17:58:32 -07002497
2498 /*
Chris Down1bc63fb2019-10-06 17:58:38 -07002499 * Minimally target SWAP_CLUSTER_MAX pages to keep
Ethon Paul55b65a52020-06-04 16:49:10 -07002500 * reclaim moving forwards, avoiding decrementing
Chris Down9de7ca42019-10-06 17:58:35 -07002501 * sc->priority further than desirable.
Chris Down9783aa92019-10-06 17:58:32 -07002502 */
Chris Down1bc63fb2019-10-06 17:58:38 -07002503 scan = max(scan, SWAP_CLUSTER_MAX);
Chris Down9783aa92019-10-06 17:58:32 -07002504 } else {
2505 scan = lruvec_size;
2506 }
2507
2508 scan >>= sc->priority;
2509
Johannes Weiner688035f2017-05-03 14:52:07 -07002510 /*
2511 * If the cgroup's already been deleted, make sure to
2512 * scrape out the remaining cache.
2513 */
2514 if (!scan && !mem_cgroup_online(memcg))
Chris Down9783aa92019-10-06 17:58:32 -07002515 scan = min(lruvec_size, SWAP_CLUSTER_MAX);
Johannes Weiner9a265112013-02-22 16:32:17 -08002516
Johannes Weiner688035f2017-05-03 14:52:07 -07002517 switch (scan_balance) {
2518 case SCAN_EQUAL:
2519 /* Scan lists relative to size */
2520 break;
2521 case SCAN_FRACT:
Johannes Weiner9a265112013-02-22 16:32:17 -08002522 /*
Johannes Weiner688035f2017-05-03 14:52:07 -07002523 * Scan types proportional to swappiness and
2524 * their relative recent reclaim efficiency.
Gavin Shan76073c62020-02-20 20:04:24 -08002525 * Make sure we don't miss the last page on
2526 * the offlined memory cgroups because of a
2527 * round-off error.
Johannes Weiner9a265112013-02-22 16:32:17 -08002528 */
Gavin Shan76073c62020-02-20 20:04:24 -08002529 scan = mem_cgroup_online(memcg) ?
2530 div64_u64(scan * fraction[file], denominator) :
2531 DIV64_U64_ROUND_UP(scan * fraction[file],
Roman Gushchin68600f62018-10-26 15:03:27 -07002532 denominator);
Johannes Weiner688035f2017-05-03 14:52:07 -07002533 break;
2534 case SCAN_FILE:
2535 case SCAN_ANON:
2536 /* Scan one type exclusively */
Mateusz Noseke072bff2020-04-01 21:10:15 -07002537 if ((scan_balance == SCAN_FILE) != file)
Johannes Weiner688035f2017-05-03 14:52:07 -07002538 scan = 0;
Johannes Weiner688035f2017-05-03 14:52:07 -07002539 break;
2540 default:
2541 /* Look ma, no brain */
2542 BUG();
Johannes Weiner9a265112013-02-22 16:32:17 -08002543 }
Johannes Weiner688035f2017-05-03 14:52:07 -07002544
Johannes Weiner688035f2017-05-03 14:52:07 -07002545 nr[lru] = scan;
Shaohua Li76a33fc2010-05-24 14:32:36 -07002546 }
Wu Fengguang6e08a362009-06-16 15:32:29 -07002547}
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002548
Johannes Weinerafaf07a2019-11-30 17:55:46 -08002549static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002550{
2551 unsigned long nr[NR_LRU_LISTS];
Mel Gormane82e0562013-07-03 15:01:44 -07002552 unsigned long targets[NR_LRU_LISTS];
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002553 unsigned long nr_to_scan;
2554 enum lru_list lru;
2555 unsigned long nr_reclaimed = 0;
2556 unsigned long nr_to_reclaim = sc->nr_to_reclaim;
2557 struct blk_plug plug;
Mel Gorman1a501902014-06-04 16:10:49 -07002558 bool scan_adjusted;
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002559
Johannes Weinerafaf07a2019-11-30 17:55:46 -08002560 get_scan_count(lruvec, sc, nr);
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002561
Mel Gormane82e0562013-07-03 15:01:44 -07002562 /* Record the original scan target for proportional adjustments later */
2563 memcpy(targets, nr, sizeof(nr));
2564
Mel Gorman1a501902014-06-04 16:10:49 -07002565 /*
2566 * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal
2567 * event that can occur when there is little memory pressure e.g.
2568 * multiple streaming readers/writers. Hence, we do not abort scanning
2569 * when the requested number of pages are reclaimed when scanning at
2570 * DEF_PRIORITY on the assumption that the fact we are direct
2571 * reclaiming implies that kswapd is not keeping up and it is best to
2572 * do a batch of work at once. For memcg reclaim one check is made to
2573 * abort proportional reclaim if either the file or anon lru has already
2574 * dropped to zero at the first pass.
2575 */
Johannes Weinerb5ead352019-11-30 17:55:40 -08002576 scan_adjusted = (!cgroup_reclaim(sc) && !current_is_kswapd() &&
Mel Gorman1a501902014-06-04 16:10:49 -07002577 sc->priority == DEF_PRIORITY);
2578
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002579 blk_start_plug(&plug);
2580 while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
2581 nr[LRU_INACTIVE_FILE]) {
Mel Gormane82e0562013-07-03 15:01:44 -07002582 unsigned long nr_anon, nr_file, percentage;
2583 unsigned long nr_scanned;
2584
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002585 for_each_evictable_lru(lru) {
2586 if (nr[lru]) {
2587 nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX);
2588 nr[lru] -= nr_to_scan;
2589
2590 nr_reclaimed += shrink_list(lru, nr_to_scan,
Johannes Weiner3b991202019-04-18 17:50:34 -07002591 lruvec, sc);
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002592 }
2593 }
Mel Gormane82e0562013-07-03 15:01:44 -07002594
Michal Hockobd041732016-12-02 17:26:48 -08002595 cond_resched();
2596
Mel Gormane82e0562013-07-03 15:01:44 -07002597 if (nr_reclaimed < nr_to_reclaim || scan_adjusted)
2598 continue;
2599
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002600 /*
Mel Gormane82e0562013-07-03 15:01:44 -07002601 * For kswapd and memcg, reclaim at least the number of pages
Mel Gorman1a501902014-06-04 16:10:49 -07002602 * requested. Ensure that the anon and file LRUs are scanned
Mel Gormane82e0562013-07-03 15:01:44 -07002603 * proportionally what was requested by get_scan_count(). We
2604 * stop reclaiming one LRU and reduce the amount scanning
2605 * proportional to the original scan target.
2606 */
2607 nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE];
2608 nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON];
2609
Mel Gorman1a501902014-06-04 16:10:49 -07002610 /*
2611 * It's just vindictive to attack the larger once the smaller
2612 * has gone to zero. And given the way we stop scanning the
2613 * smaller below, this makes sure that we only make one nudge
2614 * towards proportionality once we've got nr_to_reclaim.
2615 */
2616 if (!nr_file || !nr_anon)
2617 break;
2618
Mel Gormane82e0562013-07-03 15:01:44 -07002619 if (nr_file > nr_anon) {
2620 unsigned long scan_target = targets[LRU_INACTIVE_ANON] +
2621 targets[LRU_ACTIVE_ANON] + 1;
2622 lru = LRU_BASE;
2623 percentage = nr_anon * 100 / scan_target;
2624 } else {
2625 unsigned long scan_target = targets[LRU_INACTIVE_FILE] +
2626 targets[LRU_ACTIVE_FILE] + 1;
2627 lru = LRU_FILE;
2628 percentage = nr_file * 100 / scan_target;
2629 }
2630
2631 /* Stop scanning the smaller of the LRU */
2632 nr[lru] = 0;
2633 nr[lru + LRU_ACTIVE] = 0;
2634
2635 /*
2636 * Recalculate the other LRU scan count based on its original
2637 * scan target and the percentage scanning already complete
2638 */
2639 lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE;
2640 nr_scanned = targets[lru] - nr[lru];
2641 nr[lru] = targets[lru] * (100 - percentage) / 100;
2642 nr[lru] -= min(nr[lru], nr_scanned);
2643
2644 lru += LRU_ACTIVE;
2645 nr_scanned = targets[lru] - nr[lru];
2646 nr[lru] = targets[lru] * (100 - percentage) / 100;
2647 nr[lru] -= min(nr[lru], nr_scanned);
2648
2649 scan_adjusted = true;
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002650 }
2651 blk_finish_plug(&plug);
2652 sc->nr_reclaimed += nr_reclaimed;
2653
2654 /*
2655 * Even if we did not try to evict anon pages at all, we want to
2656 * rebalance the anon lru active/inactive ratio.
2657 */
Johannes Weinerb91ac372019-11-30 17:56:02 -08002658 if (total_swap_pages && inactive_is_low(lruvec, LRU_INACTIVE_ANON))
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002659 shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
2660 sc, LRU_ACTIVE_ANON);
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002661}
2662
Mel Gorman23b9da52012-05-29 15:06:20 -07002663/* Use reclaim/compaction for costly allocs or under memory pressure */
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002664static bool in_reclaim_compaction(struct scan_control *sc)
Mel Gorman23b9da52012-05-29 15:06:20 -07002665{
Kirill A. Shutemovd84da3f2012-12-11 16:00:31 -08002666 if (IS_ENABLED(CONFIG_COMPACTION) && sc->order &&
Mel Gorman23b9da52012-05-29 15:06:20 -07002667 (sc->order > PAGE_ALLOC_COSTLY_ORDER ||
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002668 sc->priority < DEF_PRIORITY - 2))
Mel Gorman23b9da52012-05-29 15:06:20 -07002669 return true;
2670
2671 return false;
2672}
2673
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002674/*
Mel Gorman23b9da52012-05-29 15:06:20 -07002675 * Reclaim/compaction is used for high-order allocation requests. It reclaims
2676 * order-0 pages before compacting the zone. should_continue_reclaim() returns
2677 * true if more pages should be reclaimed such that when the page allocator
Qiwu Chendf3a45f2020-06-03 16:01:21 -07002678 * calls try_to_compact_pages() that it will have enough free pages to succeed.
Mel Gorman23b9da52012-05-29 15:06:20 -07002679 * It will give up earlier than that if there is difficulty reclaiming pages.
Mel Gorman3e7d3442011-01-13 15:45:56 -08002680 */
Mel Gormana9dd0a82016-07-28 15:46:02 -07002681static inline bool should_continue_reclaim(struct pglist_data *pgdat,
Mel Gorman3e7d3442011-01-13 15:45:56 -08002682 unsigned long nr_reclaimed,
Mel Gorman3e7d3442011-01-13 15:45:56 -08002683 struct scan_control *sc)
2684{
2685 unsigned long pages_for_compaction;
2686 unsigned long inactive_lru_pages;
Mel Gormana9dd0a82016-07-28 15:46:02 -07002687 int z;
Mel Gorman3e7d3442011-01-13 15:45:56 -08002688
2689 /* If not in reclaim/compaction mode, stop */
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002690 if (!in_reclaim_compaction(sc))
Mel Gorman3e7d3442011-01-13 15:45:56 -08002691 return false;
2692
Vlastimil Babka5ee04712019-09-23 15:37:29 -07002693 /*
2694 * Stop if we failed to reclaim any pages from the last SWAP_CLUSTER_MAX
2695 * number of pages that were scanned. This will return to the caller
2696 * with the risk reclaim/compaction and the resulting allocation attempt
2697 * fails. In the past we have tried harder for __GFP_RETRY_MAYFAIL
2698 * allocations through requiring that the full LRU list has been scanned
2699 * first, by assuming that zero delta of sc->nr_scanned means full LRU
2700 * scan, but that approximation was wrong, and there were corner cases
2701 * where always a non-zero amount of pages were scanned.
2702 */
2703 if (!nr_reclaimed)
2704 return false;
Mel Gorman3e7d3442011-01-13 15:45:56 -08002705
Mel Gorman3e7d3442011-01-13 15:45:56 -08002706 /* If compaction would go ahead or the allocation would succeed, stop */
Mel Gormana9dd0a82016-07-28 15:46:02 -07002707 for (z = 0; z <= sc->reclaim_idx; z++) {
2708 struct zone *zone = &pgdat->node_zones[z];
Mel Gorman6aa303d2016-09-01 16:14:55 -07002709 if (!managed_zone(zone))
Mel Gormana9dd0a82016-07-28 15:46:02 -07002710 continue;
2711
2712 switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) {
Vlastimil Babkacf378312016-10-07 16:57:41 -07002713 case COMPACT_SUCCESS:
Mel Gormana9dd0a82016-07-28 15:46:02 -07002714 case COMPACT_CONTINUE:
2715 return false;
2716 default:
2717 /* check next zone */
2718 ;
2719 }
Mel Gorman3e7d3442011-01-13 15:45:56 -08002720 }
Hillf Danton1c6c1592019-09-23 15:37:26 -07002721
2722 /*
2723 * If we have not reclaimed enough pages for compaction and the
2724 * inactive lists are large enough, continue reclaiming
2725 */
2726 pages_for_compaction = compact_gap(sc->order);
2727 inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE);
2728 if (get_nr_swap_pages() > 0)
2729 inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON);
2730
Vlastimil Babka5ee04712019-09-23 15:37:29 -07002731 return inactive_lru_pages > pages_for_compaction;
Mel Gorman3e7d3442011-01-13 15:45:56 -08002732}
2733
Johannes Weiner0f6a5cf2019-11-30 17:55:49 -08002734static void shrink_node_memcgs(pg_data_t *pgdat, struct scan_control *sc)
Johannes Weinerf16015f2012-01-12 17:17:52 -08002735{
Johannes Weiner0f6a5cf2019-11-30 17:55:49 -08002736 struct mem_cgroup *target_memcg = sc->target_mem_cgroup;
Johannes Weinerd2af3392019-11-30 17:55:43 -08002737 struct mem_cgroup *memcg;
Johannes Weinerf16015f2012-01-12 17:17:52 -08002738
Johannes Weiner0f6a5cf2019-11-30 17:55:49 -08002739 memcg = mem_cgroup_iter(target_memcg, NULL, NULL);
Johannes Weiner56600482012-01-12 17:17:59 -08002740 do {
Johannes Weinerafaf07a2019-11-30 17:55:46 -08002741 struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat);
Johannes Weinerd2af3392019-11-30 17:55:43 -08002742 unsigned long reclaimed;
2743 unsigned long scanned;
Johannes Weiner56600482012-01-12 17:17:59 -08002744
Xunlei Pange3336ca2020-09-04 16:35:27 -07002745 /*
2746 * This loop can become CPU-bound when target memcgs
2747 * aren't eligible for reclaim - either because they
2748 * don't have any reclaimable pages, or because their
2749 * memory is explicitly protected. Avoid soft lockups.
2750 */
2751 cond_resched();
2752
Chris Down45c7f7e2020-08-06 23:22:05 -07002753 mem_cgroup_calculate_protection(target_memcg, memcg);
2754
2755 if (mem_cgroup_below_min(memcg)) {
Johannes Weinerd2af3392019-11-30 17:55:43 -08002756 /*
2757 * Hard protection.
2758 * If there is no reclaimable memory, OOM.
2759 */
2760 continue;
Chris Down45c7f7e2020-08-06 23:22:05 -07002761 } else if (mem_cgroup_below_low(memcg)) {
Johannes Weinerd2af3392019-11-30 17:55:43 -08002762 /*
2763 * Soft protection.
2764 * Respect the protection only as long as
2765 * there is an unprotected supply
2766 * of reclaimable memory from other cgroups.
2767 */
2768 if (!sc->memcg_low_reclaim) {
2769 sc->memcg_low_skipped = 1;
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07002770 continue;
Johannes Weiner241994ed2015-02-11 15:26:06 -08002771 }
Johannes Weinerd2af3392019-11-30 17:55:43 -08002772 memcg_memory_event(memcg, MEMCG_LOW);
Johannes Weiner6b4f7792014-12-12 16:56:13 -08002773 }
2774
Johannes Weinerd2af3392019-11-30 17:55:43 -08002775 reclaimed = sc->nr_reclaimed;
2776 scanned = sc->nr_scanned;
Johannes Weinerafaf07a2019-11-30 17:55:46 -08002777
2778 shrink_lruvec(lruvec, sc);
Anton Vorontsov70ddf632013-04-29 15:08:31 -07002779
Johannes Weinerd2af3392019-11-30 17:55:43 -08002780 shrink_slab(sc->gfp_mask, pgdat->node_id, memcg,
2781 sc->priority);
Johannes Weiner2344d7e2014-08-06 16:06:15 -07002782
Johannes Weinerd2af3392019-11-30 17:55:43 -08002783 /* Record the group's reclaim efficiency */
2784 vmpressure(sc->gfp_mask, memcg, false,
2785 sc->nr_scanned - scanned,
2786 sc->nr_reclaimed - reclaimed);
Andrey Ryabinind108c772018-04-10 16:27:59 -07002787
Johannes Weiner0f6a5cf2019-11-30 17:55:49 -08002788 } while ((memcg = mem_cgroup_iter(target_memcg, memcg, NULL)));
2789}
2790
Liu Song6c9e09072020-01-30 22:14:08 -08002791static void shrink_node(pg_data_t *pgdat, struct scan_control *sc)
Johannes Weiner0f6a5cf2019-11-30 17:55:49 -08002792{
2793 struct reclaim_state *reclaim_state = current->reclaim_state;
Johannes Weiner0f6a5cf2019-11-30 17:55:49 -08002794 unsigned long nr_reclaimed, nr_scanned;
Johannes Weiner1b051172019-11-30 17:55:52 -08002795 struct lruvec *target_lruvec;
Johannes Weiner0f6a5cf2019-11-30 17:55:49 -08002796 bool reclaimable = false;
Johannes Weinerb91ac372019-11-30 17:56:02 -08002797 unsigned long file;
Johannes Weiner0f6a5cf2019-11-30 17:55:49 -08002798
Johannes Weiner1b051172019-11-30 17:55:52 -08002799 target_lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, pgdat);
2800
Johannes Weiner0f6a5cf2019-11-30 17:55:49 -08002801again:
2802 memset(&sc->nr, 0, sizeof(sc->nr));
2803
2804 nr_reclaimed = sc->nr_reclaimed;
2805 nr_scanned = sc->nr_scanned;
2806
Johannes Weiner53138ce2019-11-30 17:55:56 -08002807 /*
Johannes Weiner7cf111b2020-06-03 16:03:06 -07002808 * Determine the scan balance between anon and file LRUs.
2809 */
Alex Shi6168d0d2020-12-15 12:34:29 -08002810 spin_lock_irq(&target_lruvec->lru_lock);
Johannes Weiner7cf111b2020-06-03 16:03:06 -07002811 sc->anon_cost = target_lruvec->anon_cost;
2812 sc->file_cost = target_lruvec->file_cost;
Alex Shi6168d0d2020-12-15 12:34:29 -08002813 spin_unlock_irq(&target_lruvec->lru_lock);
Johannes Weiner7cf111b2020-06-03 16:03:06 -07002814
2815 /*
Johannes Weinerb91ac372019-11-30 17:56:02 -08002816 * Target desirable inactive:active list ratios for the anon
2817 * and file LRU lists.
2818 */
2819 if (!sc->force_deactivate) {
2820 unsigned long refaults;
2821
Joonsoo Kim170b04b72020-08-11 18:30:43 -07002822 refaults = lruvec_page_state(target_lruvec,
2823 WORKINGSET_ACTIVATE_ANON);
2824 if (refaults != target_lruvec->refaults[0] ||
2825 inactive_is_low(target_lruvec, LRU_INACTIVE_ANON))
Johannes Weinerb91ac372019-11-30 17:56:02 -08002826 sc->may_deactivate |= DEACTIVATE_ANON;
2827 else
2828 sc->may_deactivate &= ~DEACTIVATE_ANON;
2829
2830 /*
2831 * When refaults are being observed, it means a new
2832 * workingset is being established. Deactivate to get
2833 * rid of any stale active pages quickly.
2834 */
2835 refaults = lruvec_page_state(target_lruvec,
Joonsoo Kim170b04b72020-08-11 18:30:43 -07002836 WORKINGSET_ACTIVATE_FILE);
2837 if (refaults != target_lruvec->refaults[1] ||
Johannes Weinerb91ac372019-11-30 17:56:02 -08002838 inactive_is_low(target_lruvec, LRU_INACTIVE_FILE))
2839 sc->may_deactivate |= DEACTIVATE_FILE;
2840 else
2841 sc->may_deactivate &= ~DEACTIVATE_FILE;
2842 } else
2843 sc->may_deactivate = DEACTIVATE_ANON | DEACTIVATE_FILE;
2844
2845 /*
2846 * If we have plenty of inactive file pages that aren't
2847 * thrashing, try to reclaim those first before touching
2848 * anonymous pages.
2849 */
2850 file = lruvec_page_state(target_lruvec, NR_INACTIVE_FILE);
2851 if (file >> sc->priority && !(sc->may_deactivate & DEACTIVATE_FILE))
2852 sc->cache_trim_mode = 1;
2853 else
2854 sc->cache_trim_mode = 0;
2855
2856 /*
Johannes Weiner53138ce2019-11-30 17:55:56 -08002857 * Prevent the reclaimer from falling into the cache trap: as
2858 * cache pages start out inactive, every cache fault will tip
2859 * the scan balance towards the file LRU. And as the file LRU
2860 * shrinks, so does the window for rotation from references.
2861 * This means we have a runaway feedback loop where a tiny
2862 * thrashing file LRU becomes infinitely more attractive than
2863 * anon pages. Try to detect this based on file LRU size.
2864 */
2865 if (!cgroup_reclaim(sc)) {
Johannes Weiner53138ce2019-11-30 17:55:56 -08002866 unsigned long total_high_wmark = 0;
Johannes Weinerb91ac372019-11-30 17:56:02 -08002867 unsigned long free, anon;
2868 int z;
Johannes Weiner53138ce2019-11-30 17:55:56 -08002869
2870 free = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES);
2871 file = node_page_state(pgdat, NR_ACTIVE_FILE) +
2872 node_page_state(pgdat, NR_INACTIVE_FILE);
2873
2874 for (z = 0; z < MAX_NR_ZONES; z++) {
2875 struct zone *zone = &pgdat->node_zones[z];
2876 if (!managed_zone(zone))
2877 continue;
2878
2879 total_high_wmark += high_wmark_pages(zone);
2880 }
2881
Johannes Weinerb91ac372019-11-30 17:56:02 -08002882 /*
2883 * Consider anon: if that's low too, this isn't a
2884 * runaway file reclaim problem, but rather just
2885 * extreme pressure. Reclaim as per usual then.
2886 */
2887 anon = node_page_state(pgdat, NR_INACTIVE_ANON);
2888
2889 sc->file_is_tiny =
2890 file + free <= total_high_wmark &&
2891 !(sc->may_deactivate & DEACTIVATE_ANON) &&
2892 anon >> sc->priority;
Johannes Weiner53138ce2019-11-30 17:55:56 -08002893 }
2894
Johannes Weiner0f6a5cf2019-11-30 17:55:49 -08002895 shrink_node_memcgs(pgdat, sc);
Andrey Ryabinind108c772018-04-10 16:27:59 -07002896
Johannes Weinerd2af3392019-11-30 17:55:43 -08002897 if (reclaim_state) {
2898 sc->nr_reclaimed += reclaim_state->reclaimed_slab;
2899 reclaim_state->reclaimed_slab = 0;
2900 }
Andrey Ryabinind108c772018-04-10 16:27:59 -07002901
Johannes Weinerd2af3392019-11-30 17:55:43 -08002902 /* Record the subtree's reclaim efficiency */
Johannes Weiner1b051172019-11-30 17:55:52 -08002903 vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true,
Johannes Weinerd2af3392019-11-30 17:55:43 -08002904 sc->nr_scanned - nr_scanned,
2905 sc->nr_reclaimed - nr_reclaimed);
2906
2907 if (sc->nr_reclaimed - nr_reclaimed)
2908 reclaimable = true;
2909
2910 if (current_is_kswapd()) {
2911 /*
2912 * If reclaim is isolating dirty pages under writeback,
2913 * it implies that the long-lived page allocation rate
2914 * is exceeding the page laundering rate. Either the
2915 * global limits are not being effective at throttling
2916 * processes due to the page distribution throughout
2917 * zones or there is heavy usage of a slow backing
2918 * device. The only option is to throttle from reclaim
2919 * context which is not ideal as there is no guarantee
2920 * the dirtying process is throttled in the same way
2921 * balance_dirty_pages() manages.
2922 *
2923 * Once a node is flagged PGDAT_WRITEBACK, kswapd will
2924 * count the number of pages under pages flagged for
2925 * immediate reclaim and stall if any are encountered
2926 * in the nr_immediate check below.
2927 */
2928 if (sc->nr.writeback && sc->nr.writeback == sc->nr.taken)
2929 set_bit(PGDAT_WRITEBACK, &pgdat->flags);
Andrey Ryabinind108c772018-04-10 16:27:59 -07002930
Johannes Weinerd2af3392019-11-30 17:55:43 -08002931 /* Allow kswapd to start writing pages during reclaim.*/
2932 if (sc->nr.unqueued_dirty == sc->nr.file_taken)
2933 set_bit(PGDAT_DIRTY, &pgdat->flags);
Andrey Ryabinine3c1ac52018-04-10 16:28:03 -07002934
2935 /*
Randy Dunlap1eba09c2020-08-11 18:33:26 -07002936 * If kswapd scans pages marked for immediate
Johannes Weinerd2af3392019-11-30 17:55:43 -08002937 * reclaim and under writeback (nr_immediate), it
2938 * implies that pages are cycling through the LRU
2939 * faster than they are written so also forcibly stall.
Andrey Ryabinind108c772018-04-10 16:27:59 -07002940 */
Johannes Weinerd2af3392019-11-30 17:55:43 -08002941 if (sc->nr.immediate)
2942 congestion_wait(BLK_RW_ASYNC, HZ/10);
2943 }
Andrey Ryabinind108c772018-04-10 16:27:59 -07002944
Johannes Weinerd2af3392019-11-30 17:55:43 -08002945 /*
Johannes Weiner1b051172019-11-30 17:55:52 -08002946 * Tag a node/memcg as congested if all the dirty pages
2947 * scanned were backed by a congested BDI and
2948 * wait_iff_congested will stall.
2949 *
Johannes Weinerd2af3392019-11-30 17:55:43 -08002950 * Legacy memcg will stall in page writeback so avoid forcibly
2951 * stalling in wait_iff_congested().
2952 */
Johannes Weiner1b051172019-11-30 17:55:52 -08002953 if ((current_is_kswapd() ||
2954 (cgroup_reclaim(sc) && writeback_throttling_sane(sc))) &&
Johannes Weinerd2af3392019-11-30 17:55:43 -08002955 sc->nr.dirty && sc->nr.dirty == sc->nr.congested)
Johannes Weiner1b051172019-11-30 17:55:52 -08002956 set_bit(LRUVEC_CONGESTED, &target_lruvec->flags);
Johannes Weinerd2af3392019-11-30 17:55:43 -08002957
2958 /*
2959 * Stall direct reclaim for IO completions if underlying BDIs
2960 * and node is congested. Allow kswapd to continue until it
2961 * starts encountering unqueued dirty pages or cycling through
2962 * the LRU too quickly.
2963 */
Johannes Weiner1b051172019-11-30 17:55:52 -08002964 if (!current_is_kswapd() && current_may_throttle() &&
2965 !sc->hibernation_mode &&
2966 test_bit(LRUVEC_CONGESTED, &target_lruvec->flags))
Johannes Weinerd2af3392019-11-30 17:55:43 -08002967 wait_iff_congested(BLK_RW_ASYNC, HZ/10);
2968
2969 if (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed,
2970 sc))
2971 goto again;
Johannes Weiner2344d7e2014-08-06 16:06:15 -07002972
Johannes Weinerc73322d2017-05-03 14:51:51 -07002973 /*
2974 * Kswapd gives up on balancing particular nodes after too
2975 * many failures to reclaim anything from them and goes to
2976 * sleep. On reclaim progress, reset the failure counter. A
2977 * successful direct reclaim run will revive a dormant kswapd.
2978 */
2979 if (reclaimable)
2980 pgdat->kswapd_failures = 0;
Johannes Weinerf16015f2012-01-12 17:17:52 -08002981}
2982
Vlastimil Babka53853e22014-10-09 15:27:02 -07002983/*
Vlastimil Babkafdd4c6142016-10-07 16:58:03 -07002984 * Returns true if compaction should go ahead for a costly-order request, or
2985 * the allocation would already succeed without compaction. Return false if we
2986 * should reclaim first.
Vlastimil Babka53853e22014-10-09 15:27:02 -07002987 */
Mel Gorman4f588332016-07-28 15:46:38 -07002988static inline bool compaction_ready(struct zone *zone, struct scan_control *sc)
Mel Gormanfe4b1b22012-01-12 17:19:45 -08002989{
Mel Gorman31483b62016-07-28 15:45:46 -07002990 unsigned long watermark;
Vlastimil Babkafdd4c6142016-10-07 16:58:03 -07002991 enum compact_result suitable;
Mel Gormanfe4b1b22012-01-12 17:19:45 -08002992
Vlastimil Babkafdd4c6142016-10-07 16:58:03 -07002993 suitable = compaction_suitable(zone, sc->order, 0, sc->reclaim_idx);
2994 if (suitable == COMPACT_SUCCESS)
2995 /* Allocation should succeed already. Don't reclaim. */
2996 return true;
2997 if (suitable == COMPACT_SKIPPED)
2998 /* Compaction cannot yet proceed. Do reclaim. */
Mel Gormanfe4b1b22012-01-12 17:19:45 -08002999 return false;
3000
Vlastimil Babkafdd4c6142016-10-07 16:58:03 -07003001 /*
3002 * Compaction is already possible, but it takes time to run and there
3003 * are potentially other callers using the pages just freed. So proceed
3004 * with reclaim to make a buffer of free pages available to give
3005 * compaction a reasonable chance of completing and allocating the page.
3006 * Note that we won't actually reclaim the whole buffer in one attempt
3007 * as the target watermark in should_continue_reclaim() is lower. But if
3008 * we are already above the high+gap watermark, don't reclaim at all.
3009 */
3010 watermark = high_wmark_pages(zone) + compact_gap(sc->order);
3011
3012 return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx);
Mel Gormanfe4b1b22012-01-12 17:19:45 -08003013}
3014
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015/*
3016 * This is the direct reclaim path, for page-allocating processes. We only
3017 * try to reclaim pages from zones which will satisfy the caller's allocation
3018 * request.
3019 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 * If a zone is deemed to be full of pinned pages then just give it a light
3021 * scan then give up on it.
3022 */
Michal Hocko0a0337e2016-05-20 16:57:00 -07003023static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024{
Mel Gormandd1a2392008-04-28 02:12:17 -07003025 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07003026 struct zone *zone;
Andrew Morton0608f432013-09-24 15:27:41 -07003027 unsigned long nr_soft_reclaimed;
3028 unsigned long nr_soft_scanned;
Weijie Yang619d0d762014-04-07 15:36:59 -07003029 gfp_t orig_mask;
Mel Gorman79dafcd2016-07-28 15:45:53 -07003030 pg_data_t *last_pgdat = NULL;
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08003031
Mel Gormancc715d92012-03-21 16:34:00 -07003032 /*
3033 * If the number of buffer_heads in the machine exceeds the maximum
3034 * allowed level, force direct reclaim to scan the highmem zone as
3035 * highmem pages could be pinning lowmem pages storing buffer_heads
3036 */
Weijie Yang619d0d762014-04-07 15:36:59 -07003037 orig_mask = sc->gfp_mask;
Mel Gormanb2e18752016-07-28 15:45:37 -07003038 if (buffer_heads_over_limit) {
Mel Gormancc715d92012-03-21 16:34:00 -07003039 sc->gfp_mask |= __GFP_HIGHMEM;
Mel Gorman4f588332016-07-28 15:46:38 -07003040 sc->reclaim_idx = gfp_zone(sc->gfp_mask);
Mel Gormanb2e18752016-07-28 15:45:37 -07003041 }
Mel Gormancc715d92012-03-21 16:34:00 -07003042
Mel Gormand4debc62010-08-09 17:19:29 -07003043 for_each_zone_zonelist_nodemask(zone, z, zonelist,
Mel Gormanb2e18752016-07-28 15:45:37 -07003044 sc->reclaim_idx, sc->nodemask) {
Mel Gormanb2e18752016-07-28 15:45:37 -07003045 /*
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08003046 * Take care memory controller reclaiming has small influence
3047 * to global LRU.
3048 */
Johannes Weinerb5ead352019-11-30 17:55:40 -08003049 if (!cgroup_reclaim(sc)) {
Vladimir Davydov344736f2014-10-20 15:50:30 +04003050 if (!cpuset_zone_allowed(zone,
3051 GFP_KERNEL | __GFP_HARDWALL))
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08003052 continue;
Vladimir Davydov65ec02c2014-04-03 14:47:20 -07003053
Johannes Weiner0b064962014-08-06 16:06:12 -07003054 /*
3055 * If we already have plenty of memory free for
3056 * compaction in this zone, don't free any more.
3057 * Even though compaction is invoked for any
3058 * non-zero order, only frequent costly order
3059 * reclamation is disruptive enough to become a
3060 * noticeable problem, like transparent huge
3061 * page allocations.
3062 */
3063 if (IS_ENABLED(CONFIG_COMPACTION) &&
3064 sc->order > PAGE_ALLOC_COSTLY_ORDER &&
Mel Gorman4f588332016-07-28 15:46:38 -07003065 compaction_ready(zone, sc)) {
Johannes Weiner0b064962014-08-06 16:06:12 -07003066 sc->compaction_ready = true;
3067 continue;
Rik van Riele0887c12011-10-31 17:09:31 -07003068 }
Johannes Weiner0b064962014-08-06 16:06:12 -07003069
Andrew Morton0608f432013-09-24 15:27:41 -07003070 /*
Mel Gorman79dafcd2016-07-28 15:45:53 -07003071 * Shrink each node in the zonelist once. If the
3072 * zonelist is ordered by zone (not the default) then a
3073 * node may be shrunk multiple times but in that case
3074 * the user prefers lower zones being preserved.
3075 */
3076 if (zone->zone_pgdat == last_pgdat)
3077 continue;
3078
3079 /*
Andrew Morton0608f432013-09-24 15:27:41 -07003080 * This steals pages from memory cgroups over softlimit
3081 * and returns the number of reclaimed pages and
3082 * scanned pages. This works for global memory pressure
3083 * and balancing, not for a memcg's limit.
3084 */
3085 nr_soft_scanned = 0;
Mel Gormanef8f2322016-07-28 15:46:05 -07003086 nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat,
Andrew Morton0608f432013-09-24 15:27:41 -07003087 sc->order, sc->gfp_mask,
3088 &nr_soft_scanned);
3089 sc->nr_reclaimed += nr_soft_reclaimed;
3090 sc->nr_scanned += nr_soft_scanned;
KAMEZAWA Hiroyukiac34a1a2011-06-27 16:18:12 -07003091 /* need some check for avoid more shrink_zone() */
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08003092 }
Nick Piggin408d8542006-09-25 23:31:27 -07003093
Mel Gorman79dafcd2016-07-28 15:45:53 -07003094 /* See comment about same check for global reclaim above */
3095 if (zone->zone_pgdat == last_pgdat)
3096 continue;
3097 last_pgdat = zone->zone_pgdat;
Mel Gorman970a39a2016-07-28 15:46:35 -07003098 shrink_node(zone->zone_pgdat, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 }
Mel Gormane0c23272011-10-31 17:09:33 -07003100
Vladimir Davydov65ec02c2014-04-03 14:47:20 -07003101 /*
Weijie Yang619d0d762014-04-07 15:36:59 -07003102 * Restore to original mask to avoid the impact on the caller if we
3103 * promoted it to __GFP_HIGHMEM.
3104 */
3105 sc->gfp_mask = orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106}
Rik van Riel4f98a2f2008-10-18 20:26:32 -07003107
Johannes Weinerb9107182019-11-30 17:55:59 -08003108static void snapshot_refaults(struct mem_cgroup *target_memcg, pg_data_t *pgdat)
Johannes Weiner2a2e4882017-05-03 14:55:03 -07003109{
Johannes Weinerb9107182019-11-30 17:55:59 -08003110 struct lruvec *target_lruvec;
3111 unsigned long refaults;
Johannes Weiner2a2e4882017-05-03 14:55:03 -07003112
Johannes Weinerb9107182019-11-30 17:55:59 -08003113 target_lruvec = mem_cgroup_lruvec(target_memcg, pgdat);
Joonsoo Kim170b04b72020-08-11 18:30:43 -07003114 refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_ANON);
3115 target_lruvec->refaults[0] = refaults;
3116 refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_FILE);
3117 target_lruvec->refaults[1] = refaults;
Johannes Weiner2a2e4882017-05-03 14:55:03 -07003118}
3119
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120/*
3121 * This is the main entry point to direct page reclaim.
3122 *
3123 * If a full scan of the inactive list fails to free enough memory then we
3124 * are "out of memory" and something needs to be killed.
3125 *
3126 * If the caller is !__GFP_FS then the probability of a failure is reasonably
3127 * high - the zone may be full of dirty or under-writeback pages, which this
Jens Axboe5b0830c2009-09-23 19:37:09 +02003128 * caller can't do much about. We kick the writeback threads and take explicit
3129 * naps in the hope that some of these pages can be written. But if the
3130 * allocating task holds filesystem locks which prevent writeout this might not
3131 * work, and the allocation attempt will fail.
Nishanth Aravamudana41f24e2008-04-29 00:58:25 -07003132 *
3133 * returns: 0, if no pages reclaimed
3134 * else, the number of pages reclaimed
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 */
Mel Gormandac1d272008-04-28 02:12:12 -07003136static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
Vladimir Davydov3115cd92014-04-03 14:47:22 -07003137 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138{
Johannes Weiner241994ed2015-02-11 15:26:06 -08003139 int initial_priority = sc->priority;
Johannes Weiner2a2e4882017-05-03 14:55:03 -07003140 pg_data_t *last_pgdat;
3141 struct zoneref *z;
3142 struct zone *zone;
Johannes Weiner241994ed2015-02-11 15:26:06 -08003143retry:
Keika Kobayashi873b4772008-07-25 01:48:52 -07003144 delayacct_freepages_start();
3145
Johannes Weinerb5ead352019-11-30 17:55:40 -08003146 if (!cgroup_reclaim(sc))
Mel Gorman7cc30fc2016-07-28 15:46:59 -07003147 __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07003149 do {
Anton Vorontsov70ddf632013-04-29 15:08:31 -07003150 vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup,
3151 sc->priority);
Balbir Singh66e17072008-02-07 00:13:56 -08003152 sc->nr_scanned = 0;
Michal Hocko0a0337e2016-05-20 16:57:00 -07003153 shrink_zones(zonelist, sc);
Mel Gormane0c23272011-10-31 17:09:33 -07003154
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07003155 if (sc->nr_reclaimed >= sc->nr_to_reclaim)
Johannes Weiner0b064962014-08-06 16:06:12 -07003156 break;
3157
3158 if (sc->compaction_ready)
3159 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
3161 /*
Minchan Kim0e50ce32013-02-22 16:35:37 -08003162 * If we're getting trouble reclaiming, start doing
3163 * writepage even in laptop mode.
3164 */
3165 if (sc->priority < DEF_PRIORITY - 2)
3166 sc->may_writepage = 1;
Johannes Weiner0b064962014-08-06 16:06:12 -07003167 } while (--sc->priority >= 0);
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07003168
Johannes Weiner2a2e4882017-05-03 14:55:03 -07003169 last_pgdat = NULL;
3170 for_each_zone_zonelist_nodemask(zone, z, zonelist, sc->reclaim_idx,
3171 sc->nodemask) {
3172 if (zone->zone_pgdat == last_pgdat)
3173 continue;
3174 last_pgdat = zone->zone_pgdat;
Johannes Weiner1b051172019-11-30 17:55:52 -08003175
Johannes Weiner2a2e4882017-05-03 14:55:03 -07003176 snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat);
Johannes Weiner1b051172019-11-30 17:55:52 -08003177
3178 if (cgroup_reclaim(sc)) {
3179 struct lruvec *lruvec;
3180
3181 lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup,
3182 zone->zone_pgdat);
3183 clear_bit(LRUVEC_CONGESTED, &lruvec->flags);
3184 }
Johannes Weiner2a2e4882017-05-03 14:55:03 -07003185 }
3186
Keika Kobayashi873b4772008-07-25 01:48:52 -07003187 delayacct_freepages_end();
3188
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07003189 if (sc->nr_reclaimed)
3190 return sc->nr_reclaimed;
3191
Mel Gorman0cee34f2012-01-12 17:19:49 -08003192 /* Aborted reclaim to try compaction? don't OOM, then */
Johannes Weiner0b064962014-08-06 16:06:12 -07003193 if (sc->compaction_ready)
Mel Gorman73350842012-01-12 17:19:33 -08003194 return 1;
3195
Johannes Weinerb91ac372019-11-30 17:56:02 -08003196 /*
3197 * We make inactive:active ratio decisions based on the node's
3198 * composition of memory, but a restrictive reclaim_idx or a
3199 * memory.low cgroup setting can exempt large amounts of
3200 * memory from reclaim. Neither of which are very common, so
3201 * instead of doing costly eligibility calculations of the
3202 * entire cgroup subtree up front, we assume the estimates are
3203 * good, and retry with forcible deactivation if that fails.
3204 */
3205 if (sc->skipped_deactivate) {
3206 sc->priority = initial_priority;
3207 sc->force_deactivate = 1;
3208 sc->skipped_deactivate = 0;
3209 goto retry;
3210 }
3211
Johannes Weiner241994ed2015-02-11 15:26:06 -08003212 /* Untapped cgroup reserves? Don't OOM, retry. */
Yisheng Xied6622f62017-05-03 14:53:57 -07003213 if (sc->memcg_low_skipped) {
Johannes Weiner241994ed2015-02-11 15:26:06 -08003214 sc->priority = initial_priority;
Johannes Weinerb91ac372019-11-30 17:56:02 -08003215 sc->force_deactivate = 0;
Yisheng Xied6622f62017-05-03 14:53:57 -07003216 sc->memcg_low_reclaim = 1;
3217 sc->memcg_low_skipped = 0;
Johannes Weiner241994ed2015-02-11 15:26:06 -08003218 goto retry;
3219 }
3220
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07003221 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222}
3223
Johannes Weinerc73322d2017-05-03 14:51:51 -07003224static bool allow_direct_reclaim(pg_data_t *pgdat)
Mel Gorman55150612012-07-31 16:44:35 -07003225{
3226 struct zone *zone;
3227 unsigned long pfmemalloc_reserve = 0;
3228 unsigned long free_pages = 0;
3229 int i;
3230 bool wmark_ok;
3231
Johannes Weinerc73322d2017-05-03 14:51:51 -07003232 if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
3233 return true;
3234
Mel Gorman55150612012-07-31 16:44:35 -07003235 for (i = 0; i <= ZONE_NORMAL; i++) {
3236 zone = &pgdat->node_zones[i];
Johannes Weinerd450abd82017-05-03 14:51:54 -07003237 if (!managed_zone(zone))
3238 continue;
3239
3240 if (!zone_reclaimable_pages(zone))
Mel Gorman675becc2014-06-04 16:07:35 -07003241 continue;
3242
Mel Gorman55150612012-07-31 16:44:35 -07003243 pfmemalloc_reserve += min_wmark_pages(zone);
3244 free_pages += zone_page_state(zone, NR_FREE_PAGES);
3245 }
3246
Mel Gorman675becc2014-06-04 16:07:35 -07003247 /* If there are no reserves (unexpected config) then do not throttle */
3248 if (!pfmemalloc_reserve)
3249 return true;
3250
Mel Gorman55150612012-07-31 16:44:35 -07003251 wmark_ok = free_pages > pfmemalloc_reserve / 2;
3252
3253 /* kswapd must be awake if processes are being throttled */
3254 if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) {
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003255 if (READ_ONCE(pgdat->kswapd_highest_zoneidx) > ZONE_NORMAL)
3256 WRITE_ONCE(pgdat->kswapd_highest_zoneidx, ZONE_NORMAL);
Qian Cai5644e1fb2020-04-01 21:10:12 -07003257
Mel Gorman55150612012-07-31 16:44:35 -07003258 wake_up_interruptible(&pgdat->kswapd_wait);
3259 }
3260
3261 return wmark_ok;
3262}
3263
3264/*
3265 * Throttle direct reclaimers if backing storage is backed by the network
3266 * and the PFMEMALLOC reserve for the preferred node is getting dangerously
3267 * depleted. kswapd will continue to make progress and wake the processes
Mel Gorman50694c22012-11-26 16:29:48 -08003268 * when the low watermark is reached.
3269 *
3270 * Returns true if a fatal signal was delivered during throttling. If this
3271 * happens, the page allocator should not consider triggering the OOM killer.
Mel Gorman55150612012-07-31 16:44:35 -07003272 */
Mel Gorman50694c22012-11-26 16:29:48 -08003273static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist,
Mel Gorman55150612012-07-31 16:44:35 -07003274 nodemask_t *nodemask)
3275{
Mel Gorman675becc2014-06-04 16:07:35 -07003276 struct zoneref *z;
Mel Gorman55150612012-07-31 16:44:35 -07003277 struct zone *zone;
Mel Gorman675becc2014-06-04 16:07:35 -07003278 pg_data_t *pgdat = NULL;
Mel Gorman55150612012-07-31 16:44:35 -07003279
3280 /*
3281 * Kernel threads should not be throttled as they may be indirectly
3282 * responsible for cleaning pages necessary for reclaim to make forward
3283 * progress. kjournald for example may enter direct reclaim while
3284 * committing a transaction where throttling it could forcing other
3285 * processes to block on log_wait_commit().
3286 */
3287 if (current->flags & PF_KTHREAD)
Mel Gorman50694c22012-11-26 16:29:48 -08003288 goto out;
3289
3290 /*
3291 * If a fatal signal is pending, this process should not throttle.
3292 * It should return quickly so it can exit and free its memory
3293 */
3294 if (fatal_signal_pending(current))
3295 goto out;
Mel Gorman55150612012-07-31 16:44:35 -07003296
Mel Gorman675becc2014-06-04 16:07:35 -07003297 /*
3298 * Check if the pfmemalloc reserves are ok by finding the first node
3299 * with a usable ZONE_NORMAL or lower zone. The expectation is that
3300 * GFP_KERNEL will be required for allocating network buffers when
3301 * swapping over the network so ZONE_HIGHMEM is unusable.
3302 *
3303 * Throttling is based on the first usable node and throttled processes
3304 * wait on a queue until kswapd makes progress and wakes them. There
3305 * is an affinity then between processes waking up and where reclaim
3306 * progress has been made assuming the process wakes on the same node.
3307 * More importantly, processes running on remote nodes will not compete
3308 * for remote pfmemalloc reserves and processes on different nodes
3309 * should make reasonable progress.
3310 */
3311 for_each_zone_zonelist_nodemask(zone, z, zonelist,
Michael S. Tsirkin17636fa2015-01-26 12:58:41 -08003312 gfp_zone(gfp_mask), nodemask) {
Mel Gorman675becc2014-06-04 16:07:35 -07003313 if (zone_idx(zone) > ZONE_NORMAL)
3314 continue;
3315
3316 /* Throttle based on the first usable node */
3317 pgdat = zone->zone_pgdat;
Johannes Weinerc73322d2017-05-03 14:51:51 -07003318 if (allow_direct_reclaim(pgdat))
Mel Gorman675becc2014-06-04 16:07:35 -07003319 goto out;
3320 break;
3321 }
3322
3323 /* If no zone was usable by the allocation flags then do not throttle */
3324 if (!pgdat)
Mel Gorman50694c22012-11-26 16:29:48 -08003325 goto out;
Mel Gorman55150612012-07-31 16:44:35 -07003326
Mel Gorman68243e72012-07-31 16:44:39 -07003327 /* Account for the throttling */
3328 count_vm_event(PGSCAN_DIRECT_THROTTLE);
3329
Mel Gorman55150612012-07-31 16:44:35 -07003330 /*
3331 * If the caller cannot enter the filesystem, it's possible that it
3332 * is due to the caller holding an FS lock or performing a journal
3333 * transaction in the case of a filesystem like ext[3|4]. In this case,
3334 * it is not safe to block on pfmemalloc_wait as kswapd could be
3335 * blocked waiting on the same lock. Instead, throttle for up to a
3336 * second before continuing.
3337 */
3338 if (!(gfp_mask & __GFP_FS)) {
3339 wait_event_interruptible_timeout(pgdat->pfmemalloc_wait,
Johannes Weinerc73322d2017-05-03 14:51:51 -07003340 allow_direct_reclaim(pgdat), HZ);
Mel Gorman50694c22012-11-26 16:29:48 -08003341
3342 goto check_pending;
Mel Gorman55150612012-07-31 16:44:35 -07003343 }
3344
3345 /* Throttle until kswapd wakes the process */
3346 wait_event_killable(zone->zone_pgdat->pfmemalloc_wait,
Johannes Weinerc73322d2017-05-03 14:51:51 -07003347 allow_direct_reclaim(pgdat));
Mel Gorman50694c22012-11-26 16:29:48 -08003348
3349check_pending:
3350 if (fatal_signal_pending(current))
3351 return true;
3352
3353out:
3354 return false;
Mel Gorman55150612012-07-31 16:44:35 -07003355}
3356
Mel Gormandac1d272008-04-28 02:12:12 -07003357unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
KAMEZAWA Hiroyuki327c0e92009-03-31 15:23:31 -07003358 gfp_t gfp_mask, nodemask_t *nodemask)
Balbir Singh66e17072008-02-07 00:13:56 -08003359{
Mel Gorman33906bc2010-08-09 17:19:16 -07003360 unsigned long nr_reclaimed;
Balbir Singh66e17072008-02-07 00:13:56 -08003361 struct scan_control sc = {
KOSAKI Motohiro22fba332009-12-14 17:59:10 -08003362 .nr_to_reclaim = SWAP_CLUSTER_MAX,
Nick Desaulniersf2f43e52017-07-06 15:36:50 -07003363 .gfp_mask = current_gfp_context(gfp_mask),
Mel Gormanb2e18752016-07-28 15:45:37 -07003364 .reclaim_idx = gfp_zone(gfp_mask),
Johannes Weineree814fe2014-08-06 16:06:19 -07003365 .order = order,
3366 .nodemask = nodemask,
3367 .priority = DEF_PRIORITY,
3368 .may_writepage = !laptop_mode,
Johannes Weinera6dc60f82009-03-31 15:19:30 -07003369 .may_unmap = 1,
KOSAKI Motohiro2e2e4252009-04-21 12:24:57 -07003370 .may_swap = 1,
Balbir Singh66e17072008-02-07 00:13:56 -08003371 };
3372
Mel Gorman55150612012-07-31 16:44:35 -07003373 /*
Greg Thelenbb451fd2018-08-17 15:45:19 -07003374 * scan_control uses s8 fields for order, priority, and reclaim_idx.
3375 * Confirm they are large enough for max values.
3376 */
3377 BUILD_BUG_ON(MAX_ORDER > S8_MAX);
3378 BUILD_BUG_ON(DEF_PRIORITY > S8_MAX);
3379 BUILD_BUG_ON(MAX_NR_ZONES > S8_MAX);
3380
3381 /*
Mel Gorman50694c22012-11-26 16:29:48 -08003382 * Do not enter reclaim if fatal signal was delivered while throttled.
3383 * 1 is returned so that the page allocator does not OOM kill at this
3384 * point.
Mel Gorman55150612012-07-31 16:44:35 -07003385 */
Nick Desaulniersf2f43e52017-07-06 15:36:50 -07003386 if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask))
Mel Gorman55150612012-07-31 16:44:35 -07003387 return 1;
3388
Andrew Morton1732d2b012019-07-16 16:26:15 -07003389 set_task_reclaim_state(current, &sc.reclaim_state);
Yafang Shao3481c372019-05-13 17:19:14 -07003390 trace_mm_vmscan_direct_reclaim_begin(order, sc.gfp_mask);
Mel Gorman33906bc2010-08-09 17:19:16 -07003391
Vladimir Davydov3115cd92014-04-03 14:47:22 -07003392 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
Mel Gorman33906bc2010-08-09 17:19:16 -07003393
3394 trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
Andrew Morton1732d2b012019-07-16 16:26:15 -07003395 set_task_reclaim_state(current, NULL);
Mel Gorman33906bc2010-08-09 17:19:16 -07003396
3397 return nr_reclaimed;
Balbir Singh66e17072008-02-07 00:13:56 -08003398}
3399
Andrew Mortonc255a452012-07-31 16:43:02 -07003400#ifdef CONFIG_MEMCG
Balbir Singh66e17072008-02-07 00:13:56 -08003401
Michal Hockod2e5fb92019-08-30 16:04:50 -07003402/* Only used by soft limit reclaim. Do not reuse for anything else. */
Mel Gormana9dd0a82016-07-28 15:46:02 -07003403unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg,
Balbir Singh4e416952009-09-23 15:56:39 -07003404 gfp_t gfp_mask, bool noswap,
Mel Gormanef8f2322016-07-28 15:46:05 -07003405 pg_data_t *pgdat,
Ying Han0ae5e892011-05-26 16:25:25 -07003406 unsigned long *nr_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07003407{
Johannes Weinerafaf07a2019-11-30 17:55:46 -08003408 struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat);
Balbir Singh4e416952009-09-23 15:56:39 -07003409 struct scan_control sc = {
KOSAKI Motohirob8f5c562010-08-10 18:03:02 -07003410 .nr_to_reclaim = SWAP_CLUSTER_MAX,
Johannes Weineree814fe2014-08-06 16:06:19 -07003411 .target_mem_cgroup = memcg,
Balbir Singh4e416952009-09-23 15:56:39 -07003412 .may_writepage = !laptop_mode,
3413 .may_unmap = 1,
Mel Gormanb2e18752016-07-28 15:45:37 -07003414 .reclaim_idx = MAX_NR_ZONES - 1,
Balbir Singh4e416952009-09-23 15:56:39 -07003415 .may_swap = !noswap,
Balbir Singh4e416952009-09-23 15:56:39 -07003416 };
Ying Han0ae5e892011-05-26 16:25:25 -07003417
Michal Hockod2e5fb92019-08-30 16:04:50 -07003418 WARN_ON_ONCE(!current->reclaim_state);
3419
Balbir Singh4e416952009-09-23 15:56:39 -07003420 sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
3421 (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003422
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07003423 trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order,
Yafang Shao3481c372019-05-13 17:19:14 -07003424 sc.gfp_mask);
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003425
Balbir Singh4e416952009-09-23 15:56:39 -07003426 /*
3427 * NOTE: Although we can get the priority field, using it
3428 * here is not a good idea, since it limits the pages we can scan.
Mel Gormana9dd0a82016-07-28 15:46:02 -07003429 * if we don't reclaim here, the shrink_node from balance_pgdat
Balbir Singh4e416952009-09-23 15:56:39 -07003430 * will pick up pages from other mem cgroup's as well. We hack
3431 * the priority and make it zero.
3432 */
Johannes Weinerafaf07a2019-11-30 17:55:46 -08003433 shrink_lruvec(lruvec, &sc);
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003434
3435 trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed);
3436
Ying Han0ae5e892011-05-26 16:25:25 -07003437 *nr_scanned = sc.nr_scanned;
Yafang Shao0308f7c2019-07-16 16:26:12 -07003438
Balbir Singh4e416952009-09-23 15:56:39 -07003439 return sc.nr_reclaimed;
3440}
3441
Johannes Weiner72835c82012-01-12 17:18:32 -08003442unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003443 unsigned long nr_pages,
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003444 gfp_t gfp_mask,
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003445 bool may_swap)
Balbir Singh66e17072008-02-07 00:13:56 -08003446{
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003447 unsigned long nr_reclaimed;
Vlastimil Babka499118e2017-05-08 15:59:50 -07003448 unsigned int noreclaim_flag;
Balbir Singh66e17072008-02-07 00:13:56 -08003449 struct scan_control sc = {
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003450 .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
Michal Hocko7dea19f2017-05-03 14:53:15 -07003451 .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) |
Johannes Weineree814fe2014-08-06 16:06:19 -07003452 (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK),
Mel Gormanb2e18752016-07-28 15:45:37 -07003453 .reclaim_idx = MAX_NR_ZONES - 1,
Johannes Weineree814fe2014-08-06 16:06:19 -07003454 .target_mem_cgroup = memcg,
3455 .priority = DEF_PRIORITY,
Balbir Singh66e17072008-02-07 00:13:56 -08003456 .may_writepage = !laptop_mode,
Johannes Weinera6dc60f82009-03-31 15:19:30 -07003457 .may_unmap = 1,
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003458 .may_swap = may_swap,
Ying Hana09ed5e2011-05-24 17:12:26 -07003459 };
Shakeel Buttfa40d1e2019-11-30 17:50:16 -08003460 /*
3461 * Traverse the ZONELIST_FALLBACK zonelist of the current node to put
3462 * equal pressure on all the nodes. This is based on the assumption that
3463 * the reclaim does not bail out early.
3464 */
3465 struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
Balbir Singh66e17072008-02-07 00:13:56 -08003466
Andrew Morton1732d2b012019-07-16 16:26:15 -07003467 set_task_reclaim_state(current, &sc.reclaim_state);
Yafang Shao3481c372019-05-13 17:19:14 -07003468 trace_mm_vmscan_memcg_reclaim_begin(0, sc.gfp_mask);
Vlastimil Babka499118e2017-05-08 15:59:50 -07003469 noreclaim_flag = memalloc_noreclaim_save();
Johannes Weinereb414682018-10-26 15:06:27 -07003470
Vladimir Davydov3115cd92014-04-03 14:47:22 -07003471 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
Johannes Weinereb414682018-10-26 15:06:27 -07003472
Vlastimil Babka499118e2017-05-08 15:59:50 -07003473 memalloc_noreclaim_restore(noreclaim_flag);
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003474 trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
Andrew Morton1732d2b012019-07-16 16:26:15 -07003475 set_task_reclaim_state(current, NULL);
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003476
3477 return nr_reclaimed;
Balbir Singh66e17072008-02-07 00:13:56 -08003478}
3479#endif
3480
Mel Gorman1d82de62016-07-28 15:45:43 -07003481static void age_active_anon(struct pglist_data *pgdat,
Mel Gormanef8f2322016-07-28 15:46:05 -07003482 struct scan_control *sc)
Johannes Weinerf16015f2012-01-12 17:17:52 -08003483{
Johannes Weinerb95a2f22012-01-12 17:18:06 -08003484 struct mem_cgroup *memcg;
Johannes Weinerb91ac372019-11-30 17:56:02 -08003485 struct lruvec *lruvec;
Johannes Weinerf16015f2012-01-12 17:17:52 -08003486
Johannes Weinerb95a2f22012-01-12 17:18:06 -08003487 if (!total_swap_pages)
3488 return;
3489
Johannes Weinerb91ac372019-11-30 17:56:02 -08003490 lruvec = mem_cgroup_lruvec(NULL, pgdat);
3491 if (!inactive_is_low(lruvec, LRU_INACTIVE_ANON))
3492 return;
3493
Johannes Weinerb95a2f22012-01-12 17:18:06 -08003494 memcg = mem_cgroup_iter(NULL, NULL, NULL);
3495 do {
Johannes Weinerb91ac372019-11-30 17:56:02 -08003496 lruvec = mem_cgroup_lruvec(memcg, pgdat);
3497 shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
3498 sc, LRU_ACTIVE_ANON);
Johannes Weinerb95a2f22012-01-12 17:18:06 -08003499 memcg = mem_cgroup_iter(NULL, memcg, NULL);
3500 } while (memcg);
Johannes Weinerf16015f2012-01-12 17:17:52 -08003501}
3502
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003503static bool pgdat_watermark_boosted(pg_data_t *pgdat, int highest_zoneidx)
Mel Gorman1c308442018-12-28 00:35:52 -08003504{
3505 int i;
3506 struct zone *zone;
3507
3508 /*
3509 * Check for watermark boosts top-down as the higher zones
3510 * are more likely to be boosted. Both watermarks and boosts
Randy Dunlap1eba09c2020-08-11 18:33:26 -07003511 * should not be checked at the same time as reclaim would
Mel Gorman1c308442018-12-28 00:35:52 -08003512 * start prematurely when there is no boosting and a lower
3513 * zone is balanced.
3514 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003515 for (i = highest_zoneidx; i >= 0; i--) {
Mel Gorman1c308442018-12-28 00:35:52 -08003516 zone = pgdat->node_zones + i;
3517 if (!managed_zone(zone))
3518 continue;
3519
3520 if (zone->watermark_boost)
3521 return true;
3522 }
3523
3524 return false;
3525}
3526
Mel Gormane716f2e2017-05-03 14:53:45 -07003527/*
3528 * Returns true if there is an eligible zone balanced for the request order
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003529 * and highest_zoneidx
Mel Gormane716f2e2017-05-03 14:53:45 -07003530 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003531static bool pgdat_balanced(pg_data_t *pgdat, int order, int highest_zoneidx)
Johannes Weiner60cefed2012-11-29 13:54:23 -08003532{
Mel Gormane716f2e2017-05-03 14:53:45 -07003533 int i;
3534 unsigned long mark = -1;
3535 struct zone *zone;
Johannes Weiner60cefed2012-11-29 13:54:23 -08003536
Mel Gorman1c308442018-12-28 00:35:52 -08003537 /*
3538 * Check watermarks bottom-up as lower zones are more likely to
3539 * meet watermarks.
3540 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003541 for (i = 0; i <= highest_zoneidx; i++) {
Mel Gormane716f2e2017-05-03 14:53:45 -07003542 zone = pgdat->node_zones + i;
Mel Gorman6256c6b2016-07-28 15:45:56 -07003543
Mel Gormane716f2e2017-05-03 14:53:45 -07003544 if (!managed_zone(zone))
3545 continue;
3546
3547 mark = high_wmark_pages(zone);
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003548 if (zone_watermark_ok_safe(zone, order, mark, highest_zoneidx))
Mel Gormane716f2e2017-05-03 14:53:45 -07003549 return true;
3550 }
3551
3552 /*
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003553 * If a node has no populated zone within highest_zoneidx, it does not
Mel Gormane716f2e2017-05-03 14:53:45 -07003554 * need balancing by definition. This can happen if a zone-restricted
3555 * allocation tries to wake a remote kswapd.
3556 */
3557 if (mark == -1)
3558 return true;
3559
3560 return false;
Johannes Weiner60cefed2012-11-29 13:54:23 -08003561}
3562
Mel Gorman631b6e02017-05-03 14:53:41 -07003563/* Clear pgdat state for congested, dirty or under writeback. */
3564static void clear_pgdat_congested(pg_data_t *pgdat)
3565{
Johannes Weiner1b051172019-11-30 17:55:52 -08003566 struct lruvec *lruvec = mem_cgroup_lruvec(NULL, pgdat);
3567
3568 clear_bit(LRUVEC_CONGESTED, &lruvec->flags);
Mel Gorman631b6e02017-05-03 14:53:41 -07003569 clear_bit(PGDAT_DIRTY, &pgdat->flags);
3570 clear_bit(PGDAT_WRITEBACK, &pgdat->flags);
3571}
3572
Mel Gorman1741c872011-01-13 15:46:21 -08003573/*
Mel Gorman55150612012-07-31 16:44:35 -07003574 * Prepare kswapd for sleeping. This verifies that there are no processes
3575 * waiting in throttle_direct_reclaim() and that watermarks have been met.
3576 *
3577 * Returns true if kswapd is ready to sleep
3578 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003579static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order,
3580 int highest_zoneidx)
Mel Gormanf50de2d2009-12-14 17:58:53 -08003581{
Mel Gorman55150612012-07-31 16:44:35 -07003582 /*
Vlastimil Babka9e5e3662015-01-08 14:32:40 -08003583 * The throttled processes are normally woken up in balance_pgdat() as
Johannes Weinerc73322d2017-05-03 14:51:51 -07003584 * soon as allow_direct_reclaim() is true. But there is a potential
Vlastimil Babka9e5e3662015-01-08 14:32:40 -08003585 * race between when kswapd checks the watermarks and a process gets
3586 * throttled. There is also a potential race if processes get
3587 * throttled, kswapd wakes, a large process exits thereby balancing the
3588 * zones, which causes kswapd to exit balance_pgdat() before reaching
3589 * the wake up checks. If kswapd is going to sleep, no process should
3590 * be sleeping on pfmemalloc_wait, so wake them now if necessary. If
3591 * the wake up is premature, processes will wake kswapd and get
3592 * throttled again. The difference from wake ups in balance_pgdat() is
3593 * that here we are under prepare_to_wait().
Mel Gorman55150612012-07-31 16:44:35 -07003594 */
Vlastimil Babka9e5e3662015-01-08 14:32:40 -08003595 if (waitqueue_active(&pgdat->pfmemalloc_wait))
3596 wake_up_all(&pgdat->pfmemalloc_wait);
Mel Gormanf50de2d2009-12-14 17:58:53 -08003597
Johannes Weinerc73322d2017-05-03 14:51:51 -07003598 /* Hopeless node, leave it to direct reclaim */
3599 if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
3600 return true;
3601
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003602 if (pgdat_balanced(pgdat, order, highest_zoneidx)) {
Mel Gormane716f2e2017-05-03 14:53:45 -07003603 clear_pgdat_congested(pgdat);
3604 return true;
Mel Gorman1d82de62016-07-28 15:45:43 -07003605 }
3606
Shantanu Goel333b0a42017-05-03 14:53:38 -07003607 return false;
Mel Gormanf50de2d2009-12-14 17:58:53 -08003608}
3609
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610/*
Mel Gorman1d82de62016-07-28 15:45:43 -07003611 * kswapd shrinks a node of pages that are at or below the highest usable
3612 * zone that is currently unbalanced.
Mel Gormanb8e83b92013-07-03 15:01:45 -07003613 *
3614 * Returns true if kswapd scanned at least the requested number of pages to
Mel Gorman283aba92013-07-03 15:01:51 -07003615 * reclaim or if the lack of progress was due to pages under writeback.
3616 * This is used to determine if the scanning priority needs to be raised.
Mel Gorman75485362013-07-03 15:01:42 -07003617 */
Mel Gorman1d82de62016-07-28 15:45:43 -07003618static bool kswapd_shrink_node(pg_data_t *pgdat,
Vlastimil Babkaaccf6242016-03-17 14:18:15 -07003619 struct scan_control *sc)
Mel Gorman75485362013-07-03 15:01:42 -07003620{
Mel Gorman1d82de62016-07-28 15:45:43 -07003621 struct zone *zone;
3622 int z;
Mel Gorman75485362013-07-03 15:01:42 -07003623
Mel Gorman1d82de62016-07-28 15:45:43 -07003624 /* Reclaim a number of pages proportional to the number of zones */
3625 sc->nr_to_reclaim = 0;
Mel Gorman970a39a2016-07-28 15:46:35 -07003626 for (z = 0; z <= sc->reclaim_idx; z++) {
Mel Gorman1d82de62016-07-28 15:45:43 -07003627 zone = pgdat->node_zones + z;
Mel Gorman6aa303d2016-09-01 16:14:55 -07003628 if (!managed_zone(zone))
Mel Gorman1d82de62016-07-28 15:45:43 -07003629 continue;
Mel Gorman7c954f62013-07-03 15:01:54 -07003630
Mel Gorman1d82de62016-07-28 15:45:43 -07003631 sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX);
Mel Gorman7c954f62013-07-03 15:01:54 -07003632 }
3633
Mel Gorman1d82de62016-07-28 15:45:43 -07003634 /*
3635 * Historically care was taken to put equal pressure on all zones but
3636 * now pressure is applied based on node LRU order.
3637 */
Mel Gorman970a39a2016-07-28 15:46:35 -07003638 shrink_node(pgdat, sc);
Mel Gorman1d82de62016-07-28 15:45:43 -07003639
3640 /*
3641 * Fragmentation may mean that the system cannot be rebalanced for
3642 * high-order allocations. If twice the allocation size has been
3643 * reclaimed then recheck watermarks only at order-0 to prevent
3644 * excessive reclaim. Assume that a process requested a high-order
3645 * can direct reclaim/compact.
3646 */
Vlastimil Babka9861a622016-10-07 16:57:53 -07003647 if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order))
Mel Gorman1d82de62016-07-28 15:45:43 -07003648 sc->order = 0;
3649
Mel Gormanb8e83b92013-07-03 15:01:45 -07003650 return sc->nr_scanned >= sc->nr_to_reclaim;
Mel Gorman75485362013-07-03 15:01:42 -07003651}
3652
3653/*
Mel Gorman1d82de62016-07-28 15:45:43 -07003654 * For kswapd, balance_pgdat() will reclaim pages across a node from zones
3655 * that are eligible for use by the caller until at least one zone is
3656 * balanced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 *
Mel Gorman1d82de62016-07-28 15:45:43 -07003658 * Returns the order kswapd finished reclaiming at.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 *
3660 * kswapd scans the zones in the highmem->normal->dma direction. It skips
Mel Gorman41858962009-06-16 15:32:12 -07003661 * zones which have free_pages > high_wmark_pages(zone), but once a zone is
Wei Yang8bb4e7a2019-03-05 15:46:22 -08003662 * found to have free_pages <= high_wmark_pages(zone), any page in that zone
Mel Gorman1d82de62016-07-28 15:45:43 -07003663 * or lower is eligible for reclaim until at least one usable zone is
3664 * balanced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003666static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 int i;
Andrew Morton0608f432013-09-24 15:27:41 -07003669 unsigned long nr_soft_reclaimed;
3670 unsigned long nr_soft_scanned;
Johannes Weinereb414682018-10-26 15:06:27 -07003671 unsigned long pflags;
Mel Gorman1c308442018-12-28 00:35:52 -08003672 unsigned long nr_boost_reclaim;
3673 unsigned long zone_boosts[MAX_NR_ZONES] = { 0, };
3674 bool boosted;
Mel Gorman1d82de62016-07-28 15:45:43 -07003675 struct zone *zone;
Andrew Morton179e9632006-03-22 00:08:18 -08003676 struct scan_control sc = {
3677 .gfp_mask = GFP_KERNEL,
Johannes Weineree814fe2014-08-06 16:06:19 -07003678 .order = order,
Johannes Weinera6dc60f82009-03-31 15:19:30 -07003679 .may_unmap = 1,
Andrew Morton179e9632006-03-22 00:08:18 -08003680 };
Omar Sandoval93781322018-06-07 17:07:02 -07003681
Andrew Morton1732d2b012019-07-16 16:26:15 -07003682 set_task_reclaim_state(current, &sc.reclaim_state);
Johannes Weinereb414682018-10-26 15:06:27 -07003683 psi_memstall_enter(&pflags);
Omar Sandoval93781322018-06-07 17:07:02 -07003684 __fs_reclaim_acquire();
3685
Christoph Lameterf8891e52006-06-30 01:55:45 -07003686 count_vm_event(PAGEOUTRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687
Mel Gorman1c308442018-12-28 00:35:52 -08003688 /*
3689 * Account for the reclaim boost. Note that the zone boost is left in
3690 * place so that parallel allocations that are near the watermark will
3691 * stall or direct reclaim until kswapd is finished.
3692 */
3693 nr_boost_reclaim = 0;
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003694 for (i = 0; i <= highest_zoneidx; i++) {
Mel Gorman1c308442018-12-28 00:35:52 -08003695 zone = pgdat->node_zones + i;
3696 if (!managed_zone(zone))
3697 continue;
3698
3699 nr_boost_reclaim += zone->watermark_boost;
3700 zone_boosts[i] = zone->watermark_boost;
3701 }
3702 boosted = nr_boost_reclaim;
3703
3704restart:
3705 sc.priority = DEF_PRIORITY;
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07003706 do {
Johannes Weinerc73322d2017-05-03 14:51:51 -07003707 unsigned long nr_reclaimed = sc.nr_reclaimed;
Mel Gormanb8e83b92013-07-03 15:01:45 -07003708 bool raise_priority = true;
Mel Gorman1c308442018-12-28 00:35:52 -08003709 bool balanced;
Omar Sandoval93781322018-06-07 17:07:02 -07003710 bool ret;
Mel Gormanb8e83b92013-07-03 15:01:45 -07003711
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003712 sc.reclaim_idx = highest_zoneidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713
Mel Gorman86c79f62016-07-28 15:45:59 -07003714 /*
Mel Gorman84c7a772016-07-28 15:46:44 -07003715 * If the number of buffer_heads exceeds the maximum allowed
3716 * then consider reclaiming from all zones. This has a dual
3717 * purpose -- on 64-bit systems it is expected that
3718 * buffer_heads are stripped during active rotation. On 32-bit
3719 * systems, highmem pages can pin lowmem memory and shrinking
3720 * buffers can relieve lowmem pressure. Reclaim may still not
3721 * go ahead if all eligible zones for the original allocation
3722 * request are balanced to avoid excessive reclaim from kswapd.
Mel Gorman86c79f62016-07-28 15:45:59 -07003723 */
3724 if (buffer_heads_over_limit) {
3725 for (i = MAX_NR_ZONES - 1; i >= 0; i--) {
3726 zone = pgdat->node_zones + i;
Mel Gorman6aa303d2016-09-01 16:14:55 -07003727 if (!managed_zone(zone))
Mel Gorman86c79f62016-07-28 15:45:59 -07003728 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
Mel Gorman970a39a2016-07-28 15:46:35 -07003730 sc.reclaim_idx = i;
Andrew Mortone1dbeda2006-12-06 20:32:01 -08003731 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 }
Zlatko Calusicdafcb732013-02-22 16:32:34 -08003734
Mel Gorman86c79f62016-07-28 15:45:59 -07003735 /*
Mel Gorman1c308442018-12-28 00:35:52 -08003736 * If the pgdat is imbalanced then ignore boosting and preserve
3737 * the watermarks for a later time and restart. Note that the
3738 * zone watermarks will be still reset at the end of balancing
3739 * on the grounds that the normal reclaim should be enough to
3740 * re-evaluate if boosting is required when kswapd next wakes.
Mel Gorman86c79f62016-07-28 15:45:59 -07003741 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003742 balanced = pgdat_balanced(pgdat, sc.order, highest_zoneidx);
Mel Gorman1c308442018-12-28 00:35:52 -08003743 if (!balanced && nr_boost_reclaim) {
3744 nr_boost_reclaim = 0;
3745 goto restart;
3746 }
3747
3748 /*
3749 * If boosting is not active then only reclaim if there are no
3750 * eligible zones. Note that sc.reclaim_idx is not used as
3751 * buffer_heads_over_limit may have adjusted it.
3752 */
3753 if (!nr_boost_reclaim && balanced)
Mel Gormane716f2e2017-05-03 14:53:45 -07003754 goto out;
Andrew Mortone1dbeda2006-12-06 20:32:01 -08003755
Mel Gorman1c308442018-12-28 00:35:52 -08003756 /* Limit the priority of boosting to avoid reclaim writeback */
3757 if (nr_boost_reclaim && sc.priority == DEF_PRIORITY - 2)
3758 raise_priority = false;
3759
3760 /*
3761 * Do not writeback or swap pages for boosted reclaim. The
3762 * intent is to relieve pressure not issue sub-optimal IO
3763 * from reclaim context. If no pages are reclaimed, the
3764 * reclaim will be aborted.
3765 */
3766 sc.may_writepage = !laptop_mode && !nr_boost_reclaim;
3767 sc.may_swap = !nr_boost_reclaim;
Mel Gorman1c308442018-12-28 00:35:52 -08003768
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 /*
Mel Gorman1d82de62016-07-28 15:45:43 -07003770 * Do some background aging of the anon list, to give
3771 * pages a chance to be referenced before reclaiming. All
3772 * pages are rotated regardless of classzone as this is
3773 * about consistent aging.
3774 */
Mel Gormanef8f2322016-07-28 15:46:05 -07003775 age_active_anon(pgdat, &sc);
Mel Gorman1d82de62016-07-28 15:45:43 -07003776
3777 /*
Mel Gormanb7ea3c42013-07-03 15:01:53 -07003778 * If we're getting trouble reclaiming, start doing writepage
3779 * even in laptop mode.
3780 */
Johannes Weiner047d72c2017-05-03 14:51:57 -07003781 if (sc.priority < DEF_PRIORITY - 2)
Mel Gormanb7ea3c42013-07-03 15:01:53 -07003782 sc.may_writepage = 1;
3783
Mel Gorman1d82de62016-07-28 15:45:43 -07003784 /* Call soft limit reclaim before calling shrink_node. */
3785 sc.nr_scanned = 0;
3786 nr_soft_scanned = 0;
Mel Gormanef8f2322016-07-28 15:46:05 -07003787 nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order,
Mel Gorman1d82de62016-07-28 15:45:43 -07003788 sc.gfp_mask, &nr_soft_scanned);
3789 sc.nr_reclaimed += nr_soft_reclaimed;
3790
Mel Gormanb7ea3c42013-07-03 15:01:53 -07003791 /*
Mel Gorman1d82de62016-07-28 15:45:43 -07003792 * There should be no need to raise the scanning priority if
3793 * enough pages are already being scanned that that high
3794 * watermark would be met at 100% efficiency.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 */
Mel Gorman970a39a2016-07-28 15:46:35 -07003796 if (kswapd_shrink_node(pgdat, &sc))
Mel Gorman1d82de62016-07-28 15:45:43 -07003797 raise_priority = false;
Mel Gorman55150612012-07-31 16:44:35 -07003798
3799 /*
3800 * If the low watermark is met there is no need for processes
3801 * to be throttled on pfmemalloc_wait as they should not be
3802 * able to safely make forward progress. Wake them
3803 */
3804 if (waitqueue_active(&pgdat->pfmemalloc_wait) &&
Johannes Weinerc73322d2017-05-03 14:51:51 -07003805 allow_direct_reclaim(pgdat))
Vlastimil Babkacfc51152015-02-11 15:25:12 -08003806 wake_up_all(&pgdat->pfmemalloc_wait);
Mel Gorman55150612012-07-31 16:44:35 -07003807
Mel Gormanb8e83b92013-07-03 15:01:45 -07003808 /* Check if kswapd should be suspending */
Omar Sandoval93781322018-06-07 17:07:02 -07003809 __fs_reclaim_release();
3810 ret = try_to_freeze();
3811 __fs_reclaim_acquire();
3812 if (ret || kthread_should_stop())
Mel Gormanb8e83b92013-07-03 15:01:45 -07003813 break;
3814
3815 /*
3816 * Raise priority if scanning rate is too low or there was no
3817 * progress in reclaiming pages
3818 */
Johannes Weinerc73322d2017-05-03 14:51:51 -07003819 nr_reclaimed = sc.nr_reclaimed - nr_reclaimed;
Mel Gorman1c308442018-12-28 00:35:52 -08003820 nr_boost_reclaim -= min(nr_boost_reclaim, nr_reclaimed);
3821
3822 /*
3823 * If reclaim made no progress for a boost, stop reclaim as
3824 * IO cannot be queued and it could be an infinite loop in
3825 * extreme circumstances.
3826 */
3827 if (nr_boost_reclaim && !nr_reclaimed)
3828 break;
3829
Johannes Weinerc73322d2017-05-03 14:51:51 -07003830 if (raise_priority || !nr_reclaimed)
Mel Gormanb8e83b92013-07-03 15:01:45 -07003831 sc.priority--;
Mel Gorman1d82de62016-07-28 15:45:43 -07003832 } while (sc.priority >= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833
Johannes Weinerc73322d2017-05-03 14:51:51 -07003834 if (!sc.nr_reclaimed)
3835 pgdat->kswapd_failures++;
3836
Mel Gormanb8e83b92013-07-03 15:01:45 -07003837out:
Mel Gorman1c308442018-12-28 00:35:52 -08003838 /* If reclaim was boosted, account for the reclaim done in this pass */
3839 if (boosted) {
3840 unsigned long flags;
3841
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003842 for (i = 0; i <= highest_zoneidx; i++) {
Mel Gorman1c308442018-12-28 00:35:52 -08003843 if (!zone_boosts[i])
3844 continue;
3845
3846 /* Increments are under the zone lock */
3847 zone = pgdat->node_zones + i;
3848 spin_lock_irqsave(&zone->lock, flags);
3849 zone->watermark_boost -= min(zone->watermark_boost, zone_boosts[i]);
3850 spin_unlock_irqrestore(&zone->lock, flags);
3851 }
3852
3853 /*
3854 * As there is now likely space, wakeup kcompact to defragment
3855 * pageblocks.
3856 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003857 wakeup_kcompactd(pgdat, pageblock_order, highest_zoneidx);
Mel Gorman1c308442018-12-28 00:35:52 -08003858 }
3859
Johannes Weiner2a2e4882017-05-03 14:55:03 -07003860 snapshot_refaults(NULL, pgdat);
Omar Sandoval93781322018-06-07 17:07:02 -07003861 __fs_reclaim_release();
Johannes Weinereb414682018-10-26 15:06:27 -07003862 psi_memstall_leave(&pflags);
Andrew Morton1732d2b012019-07-16 16:26:15 -07003863 set_task_reclaim_state(current, NULL);
Yafang Shaoe5ca8072019-07-16 16:26:09 -07003864
Mel Gorman0abdee22011-01-13 15:46:22 -08003865 /*
Mel Gorman1d82de62016-07-28 15:45:43 -07003866 * Return the order kswapd stopped reclaiming at as
3867 * prepare_kswapd_sleep() takes it into account. If another caller
3868 * entered the allocator slow path while kswapd was awake, order will
3869 * remain at the higher level.
Mel Gorman0abdee22011-01-13 15:46:22 -08003870 */
Mel Gorman1d82de62016-07-28 15:45:43 -07003871 return sc.order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872}
3873
Mel Gormane716f2e2017-05-03 14:53:45 -07003874/*
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003875 * The pgdat->kswapd_highest_zoneidx is used to pass the highest zone index to
3876 * be reclaimed by kswapd from the waker. If the value is MAX_NR_ZONES which is
3877 * not a valid index then either kswapd runs for first time or kswapd couldn't
3878 * sleep after previous reclaim attempt (node is still unbalanced). In that
3879 * case return the zone index of the previous kswapd reclaim cycle.
Mel Gormane716f2e2017-05-03 14:53:45 -07003880 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003881static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat,
3882 enum zone_type prev_highest_zoneidx)
Mel Gormane716f2e2017-05-03 14:53:45 -07003883{
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003884 enum zone_type curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx);
Qian Cai5644e1fb2020-04-01 21:10:12 -07003885
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003886 return curr_idx == MAX_NR_ZONES ? prev_highest_zoneidx : curr_idx;
Mel Gormane716f2e2017-05-03 14:53:45 -07003887}
3888
Mel Gorman38087d92016-07-28 15:45:49 -07003889static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003890 unsigned int highest_zoneidx)
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08003891{
3892 long remaining = 0;
3893 DEFINE_WAIT(wait);
3894
3895 if (freezing(current) || kthread_should_stop())
3896 return;
3897
3898 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
3899
Shantanu Goel333b0a42017-05-03 14:53:38 -07003900 /*
3901 * Try to sleep for a short interval. Note that kcompactd will only be
3902 * woken if it is possible to sleep for a short interval. This is
3903 * deliberate on the assumption that if reclaim cannot keep an
3904 * eligible zone balanced that it's also unlikely that compaction will
3905 * succeed.
3906 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003907 if (prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) {
Vlastimil Babkafd901c92016-04-28 16:18:49 -07003908 /*
3909 * Compaction records what page blocks it recently failed to
3910 * isolate pages from and skips them in the future scanning.
3911 * When kswapd is going to sleep, it is reasonable to assume
3912 * that pages and compaction may succeed so reset the cache.
3913 */
3914 reset_isolation_suitable(pgdat);
3915
3916 /*
3917 * We have freed the memory, now we should compact it to make
3918 * allocation of the requested order possible.
3919 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003920 wakeup_kcompactd(pgdat, alloc_order, highest_zoneidx);
Vlastimil Babkafd901c92016-04-28 16:18:49 -07003921
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08003922 remaining = schedule_timeout(HZ/10);
Mel Gorman38087d92016-07-28 15:45:49 -07003923
3924 /*
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003925 * If woken prematurely then reset kswapd_highest_zoneidx and
Mel Gorman38087d92016-07-28 15:45:49 -07003926 * order. The values will either be from a wakeup request or
3927 * the previous request that slept prematurely.
3928 */
3929 if (remaining) {
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003930 WRITE_ONCE(pgdat->kswapd_highest_zoneidx,
3931 kswapd_highest_zoneidx(pgdat,
3932 highest_zoneidx));
Qian Cai5644e1fb2020-04-01 21:10:12 -07003933
3934 if (READ_ONCE(pgdat->kswapd_order) < reclaim_order)
3935 WRITE_ONCE(pgdat->kswapd_order, reclaim_order);
Mel Gorman38087d92016-07-28 15:45:49 -07003936 }
3937
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08003938 finish_wait(&pgdat->kswapd_wait, &wait);
3939 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
3940 }
3941
3942 /*
3943 * After a short sleep, check if it was a premature sleep. If not, then
3944 * go fully to sleep until explicitly woken up.
3945 */
Mel Gormand9f21d42016-07-28 15:46:41 -07003946 if (!remaining &&
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003947 prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) {
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08003948 trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
3949
3950 /*
3951 * vmstat counters are not perfectly accurate and the estimated
3952 * value for counters such as NR_FREE_PAGES can deviate from the
3953 * true value by nr_online_cpus * threshold. To avoid the zone
3954 * watermarks being breached while under pressure, we reduce the
3955 * per-cpu vmstat threshold while kswapd is awake and restore
3956 * them before going back to sleep.
3957 */
3958 set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold);
Aaditya Kumar1c7e7f62012-07-17 15:48:07 -07003959
3960 if (!kthread_should_stop())
3961 schedule();
3962
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08003963 set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold);
3964 } else {
3965 if (remaining)
3966 count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY);
3967 else
3968 count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY);
3969 }
3970 finish_wait(&pgdat->kswapd_wait, &wait);
3971}
3972
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973/*
3974 * The background pageout daemon, started as a kernel thread
Rik van Riel4f98a2f2008-10-18 20:26:32 -07003975 * from the init process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 *
3977 * This basically trickles out pages so that we have _some_
3978 * free memory available even if there is no other activity
3979 * that frees anything up. This is needed for things like routing
3980 * etc, where we otherwise might have all activity going on in
3981 * asynchronous contexts that cannot page things out.
3982 *
3983 * If there are applications that are active memory-allocators
3984 * (most normal use), this basically shouldn't matter.
3985 */
3986static int kswapd(void *p)
3987{
Mel Gormane716f2e2017-05-03 14:53:45 -07003988 unsigned int alloc_order, reclaim_order;
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003989 unsigned int highest_zoneidx = MAX_NR_ZONES - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 pg_data_t *pgdat = (pg_data_t*)p;
3991 struct task_struct *tsk = current;
Rusty Russella70f7302009-03-13 14:49:46 +10303992 const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
Rusty Russell174596a2009-01-01 10:12:29 +10303994 if (!cpumask_empty(cpumask))
Mike Travisc5f59f02008-04-04 18:11:10 -07003995 set_cpus_allowed_ptr(tsk, cpumask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
3997 /*
3998 * Tell the memory management that we're a "memory allocator",
3999 * and that if we need more memory we should get access to it
4000 * regardless (see "__alloc_pages()"). "kswapd" should
4001 * never get caught in the normal page freeing logic.
4002 *
4003 * (Kswapd normally doesn't need memory anyway, but sometimes
4004 * you need a small amount of memory in order to be able to
4005 * page out something else, and this flag essentially protects
4006 * us from recursively trying to free more memory as we're
4007 * trying to free the first piece of memory in the first place).
4008 */
Christoph Lameter930d9152006-01-08 01:00:47 -08004009 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
Rafael J. Wysocki83144182007-07-17 04:03:35 -07004010 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011
Qian Cai5644e1fb2020-04-01 21:10:12 -07004012 WRITE_ONCE(pgdat->kswapd_order, 0);
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004013 WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 for ( ; ; ) {
Jeff Liu6f6313d2012-12-11 16:02:48 -08004015 bool ret;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07004016
Qian Cai5644e1fb2020-04-01 21:10:12 -07004017 alloc_order = reclaim_order = READ_ONCE(pgdat->kswapd_order);
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004018 highest_zoneidx = kswapd_highest_zoneidx(pgdat,
4019 highest_zoneidx);
Mel Gormane716f2e2017-05-03 14:53:45 -07004020
Mel Gorman38087d92016-07-28 15:45:49 -07004021kswapd_try_sleep:
4022 kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004023 highest_zoneidx);
Mel Gorman215ddd62011-07-08 15:39:40 -07004024
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004025 /* Read the new order and highest_zoneidx */
Lukas Bulwahn2b47a242020-12-14 19:12:18 -08004026 alloc_order = READ_ONCE(pgdat->kswapd_order);
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004027 highest_zoneidx = kswapd_highest_zoneidx(pgdat,
4028 highest_zoneidx);
Qian Cai5644e1fb2020-04-01 21:10:12 -07004029 WRITE_ONCE(pgdat->kswapd_order, 0);
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004030 WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
David Rientjes8fe23e02009-12-14 17:58:33 -08004032 ret = try_to_freeze();
4033 if (kthread_should_stop())
4034 break;
4035
4036 /*
4037 * We can speed up thawing tasks if we don't call balance_pgdat
4038 * after returning from the refrigerator
4039 */
Mel Gorman38087d92016-07-28 15:45:49 -07004040 if (ret)
4041 continue;
Mel Gorman1d82de62016-07-28 15:45:43 -07004042
Mel Gorman38087d92016-07-28 15:45:49 -07004043 /*
4044 * Reclaim begins at the requested order but if a high-order
4045 * reclaim fails then kswapd falls back to reclaiming for
4046 * order-0. If that happens, kswapd will consider sleeping
4047 * for the order it finished reclaiming at (reclaim_order)
4048 * but kcompactd is woken to compact for the original
4049 * request (alloc_order).
4050 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004051 trace_mm_vmscan_kswapd_wake(pgdat->node_id, highest_zoneidx,
Mel Gormane5146b12016-07-28 15:46:47 -07004052 alloc_order);
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004053 reclaim_order = balance_pgdat(pgdat, alloc_order,
4054 highest_zoneidx);
Mel Gorman38087d92016-07-28 15:45:49 -07004055 if (reclaim_order < alloc_order)
4056 goto kswapd_try_sleep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 }
Takamori Yamaguchib0a8cc52012-11-08 15:53:39 -08004058
Johannes Weiner71abdc12014-06-06 14:35:35 -07004059 tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD);
Johannes Weiner71abdc12014-06-06 14:35:35 -07004060
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 return 0;
4062}
4063
4064/*
David Rientjes5ecd9d42018-04-05 16:25:16 -07004065 * A zone is low on free memory or too fragmented for high-order memory. If
4066 * kswapd should reclaim (direct reclaim is deferred), wake it up for the zone's
4067 * pgdat. It will wake up kcompactd after reclaiming memory. If kswapd reclaim
4068 * has failed or is not needed, still wake up kcompactd if only compaction is
4069 * needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 */
David Rientjes5ecd9d42018-04-05 16:25:16 -07004071void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004072 enum zone_type highest_zoneidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073{
4074 pg_data_t *pgdat;
Qian Cai5644e1fb2020-04-01 21:10:12 -07004075 enum zone_type curr_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076
Mel Gorman6aa303d2016-09-01 16:14:55 -07004077 if (!managed_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 return;
4079
David Rientjes5ecd9d42018-04-05 16:25:16 -07004080 if (!cpuset_zone_allowed(zone, gfp_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 return;
Shakeel Buttdffcac2c2019-07-04 15:14:42 -07004082
Qian Cai5644e1fb2020-04-01 21:10:12 -07004083 pgdat = zone->zone_pgdat;
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004084 curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx);
Qian Cai5644e1fb2020-04-01 21:10:12 -07004085
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004086 if (curr_idx == MAX_NR_ZONES || curr_idx < highest_zoneidx)
4087 WRITE_ONCE(pgdat->kswapd_highest_zoneidx, highest_zoneidx);
Qian Cai5644e1fb2020-04-01 21:10:12 -07004088
4089 if (READ_ONCE(pgdat->kswapd_order) < order)
4090 WRITE_ONCE(pgdat->kswapd_order, order);
4091
Con Kolivas8d0986e2005-09-13 01:25:07 -07004092 if (!waitqueue_active(&pgdat->kswapd_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 return;
Mel Gormane1a55632016-07-28 15:46:26 -07004094
David Rientjes5ecd9d42018-04-05 16:25:16 -07004095 /* Hopeless node, leave it to direct reclaim if possible */
4096 if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES ||
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004097 (pgdat_balanced(pgdat, order, highest_zoneidx) &&
4098 !pgdat_watermark_boosted(pgdat, highest_zoneidx))) {
David Rientjes5ecd9d42018-04-05 16:25:16 -07004099 /*
4100 * There may be plenty of free memory available, but it's too
4101 * fragmented for high-order allocations. Wake up kcompactd
4102 * and rely on compaction_suitable() to determine if it's
4103 * needed. If it fails, it will defer subsequent attempts to
4104 * ratelimit its work.
4105 */
4106 if (!(gfp_flags & __GFP_DIRECT_RECLAIM))
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004107 wakeup_kcompactd(pgdat, order, highest_zoneidx);
Johannes Weinerc73322d2017-05-03 14:51:51 -07004108 return;
David Rientjes5ecd9d42018-04-05 16:25:16 -07004109 }
Johannes Weinerc73322d2017-05-03 14:51:51 -07004110
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004111 trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, highest_zoneidx, order,
David Rientjes5ecd9d42018-04-05 16:25:16 -07004112 gfp_flags);
Con Kolivas8d0986e2005-09-13 01:25:07 -07004113 wake_up_interruptible(&pgdat->kswapd_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114}
4115
Rafael J. Wysockic6f37f12009-05-24 22:16:31 +02004116#ifdef CONFIG_HIBERNATION
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117/*
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08004118 * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07004119 * freed pages.
4120 *
4121 * Rather than trying to age LRUs the aim is to preserve the overall
4122 * LRU order by reclaiming preferentially
4123 * inactive > active > active referenced > active mapped
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 */
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08004125unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126{
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07004127 struct scan_control sc = {
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08004128 .nr_to_reclaim = nr_to_reclaim,
Johannes Weineree814fe2014-08-06 16:06:19 -07004129 .gfp_mask = GFP_HIGHUSER_MOVABLE,
Mel Gormanb2e18752016-07-28 15:45:37 -07004130 .reclaim_idx = MAX_NR_ZONES - 1,
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07004131 .priority = DEF_PRIORITY,
Johannes Weineree814fe2014-08-06 16:06:19 -07004132 .may_writepage = 1,
4133 .may_unmap = 1,
4134 .may_swap = 1,
4135 .hibernation_mode = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 };
Ying Hana09ed5e2011-05-24 17:12:26 -07004137 struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08004138 unsigned long nr_reclaimed;
Vlastimil Babka499118e2017-05-08 15:59:50 -07004139 unsigned int noreclaim_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01004141 fs_reclaim_acquire(sc.gfp_mask);
Omar Sandoval93781322018-06-07 17:07:02 -07004142 noreclaim_flag = memalloc_noreclaim_save();
Andrew Morton1732d2b012019-07-16 16:26:15 -07004143 set_task_reclaim_state(current, &sc.reclaim_state);
Andrew Morton69e05942006-03-22 00:08:19 -08004144
Vladimir Davydov3115cd92014-04-03 14:47:22 -07004145 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07004146
Andrew Morton1732d2b012019-07-16 16:26:15 -07004147 set_task_reclaim_state(current, NULL);
Vlastimil Babka499118e2017-05-08 15:59:50 -07004148 memalloc_noreclaim_restore(noreclaim_flag);
Omar Sandoval93781322018-06-07 17:07:02 -07004149 fs_reclaim_release(sc.gfp_mask);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07004150
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08004151 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152}
Rafael J. Wysockic6f37f12009-05-24 22:16:31 +02004153#endif /* CONFIG_HIBERNATION */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154
Yasunori Goto3218ae12006-06-27 02:53:33 -07004155/*
4156 * This kswapd start function will be called by init and node-hot-add.
4157 * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
4158 */
4159int kswapd_run(int nid)
4160{
4161 pg_data_t *pgdat = NODE_DATA(nid);
4162 int ret = 0;
4163
4164 if (pgdat->kswapd)
4165 return 0;
4166
4167 pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
4168 if (IS_ERR(pgdat->kswapd)) {
4169 /* failure at boot is fatal */
Thomas Gleixnerc6202ad2017-05-16 20:42:46 +02004170 BUG_ON(system_state < SYSTEM_RUNNING);
Gavin Shand5dc0ad2012-10-08 16:29:27 -07004171 pr_err("Failed to start kswapd on node %d\n", nid);
4172 ret = PTR_ERR(pgdat->kswapd);
Xishi Qiud72515b2013-04-17 15:58:34 -07004173 pgdat->kswapd = NULL;
Yasunori Goto3218ae12006-06-27 02:53:33 -07004174 }
4175 return ret;
4176}
4177
David Rientjes8fe23e02009-12-14 17:58:33 -08004178/*
Jiang Liud8adde12012-07-11 14:01:52 -07004179 * Called by memory hotplug when all memory in a node is offlined. Caller must
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07004180 * hold mem_hotplug_begin/end().
David Rientjes8fe23e02009-12-14 17:58:33 -08004181 */
4182void kswapd_stop(int nid)
4183{
4184 struct task_struct *kswapd = NODE_DATA(nid)->kswapd;
4185
Jiang Liud8adde12012-07-11 14:01:52 -07004186 if (kswapd) {
David Rientjes8fe23e02009-12-14 17:58:33 -08004187 kthread_stop(kswapd);
Jiang Liud8adde12012-07-11 14:01:52 -07004188 NODE_DATA(nid)->kswapd = NULL;
4189 }
David Rientjes8fe23e02009-12-14 17:58:33 -08004190}
4191
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192static int __init kswapd_init(void)
4193{
Wei Yang6b700b52020-04-01 21:10:09 -07004194 int nid;
Andrew Morton69e05942006-03-22 00:08:19 -08004195
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 swap_setup();
Lai Jiangshan48fb2e22012-12-12 13:51:43 -08004197 for_each_node_state(nid, N_MEMORY)
Yasunori Goto3218ae12006-06-27 02:53:33 -07004198 kswapd_run(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 return 0;
4200}
4201
4202module_init(kswapd_init)
Christoph Lameter9eeff232006-01-18 17:42:31 -08004203
4204#ifdef CONFIG_NUMA
4205/*
Mel Gormana5f5f912016-07-28 15:46:32 -07004206 * Node reclaim mode
Christoph Lameter9eeff232006-01-18 17:42:31 -08004207 *
Mel Gormana5f5f912016-07-28 15:46:32 -07004208 * If non-zero call node_reclaim when the number of free pages falls below
Christoph Lameter9eeff232006-01-18 17:42:31 -08004209 * the watermarks.
Christoph Lameter9eeff232006-01-18 17:42:31 -08004210 */
Mel Gormana5f5f912016-07-28 15:46:32 -07004211int node_reclaim_mode __read_mostly;
Christoph Lameter9eeff232006-01-18 17:42:31 -08004212
Dave Hansen51998362021-02-24 12:09:15 -08004213/*
Mel Gormana5f5f912016-07-28 15:46:32 -07004214 * Priority for NODE_RECLAIM. This determines the fraction of pages
Christoph Lametera92f7122006-02-01 03:05:32 -08004215 * of a node considered for each zone_reclaim. 4 scans 1/16th of
4216 * a zone.
4217 */
Mel Gormana5f5f912016-07-28 15:46:32 -07004218#define NODE_RECLAIM_PRIORITY 4
Christoph Lametera92f7122006-02-01 03:05:32 -08004219
Christoph Lameter9eeff232006-01-18 17:42:31 -08004220/*
Mel Gormana5f5f912016-07-28 15:46:32 -07004221 * Percentage of pages in a zone that must be unmapped for node_reclaim to
Christoph Lameter96146342006-07-03 00:24:13 -07004222 * occur.
4223 */
4224int sysctl_min_unmapped_ratio = 1;
4225
4226/*
Christoph Lameter0ff38492006-09-25 23:31:52 -07004227 * If the number of slab pages in a zone grows beyond this percentage then
4228 * slab reclaim needs to occur.
4229 */
4230int sysctl_min_slab_ratio = 5;
4231
Mel Gorman11fb9982016-07-28 15:46:20 -07004232static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat)
Mel Gorman90afa5d2009-06-16 15:33:20 -07004233{
Mel Gorman11fb9982016-07-28 15:46:20 -07004234 unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED);
4235 unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) +
4236 node_page_state(pgdat, NR_ACTIVE_FILE);
Mel Gorman90afa5d2009-06-16 15:33:20 -07004237
4238 /*
4239 * It's possible for there to be more file mapped pages than
4240 * accounted for by the pages on the file LRU lists because
4241 * tmpfs pages accounted for as ANON can also be FILE_MAPPED
4242 */
4243 return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0;
4244}
4245
4246/* Work out how many page cache pages we can reclaim in this reclaim_mode */
Mel Gormana5f5f912016-07-28 15:46:32 -07004247static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat)
Mel Gorman90afa5d2009-06-16 15:33:20 -07004248{
Alexandru Moised031a152015-11-05 18:48:08 -08004249 unsigned long nr_pagecache_reclaimable;
4250 unsigned long delta = 0;
Mel Gorman90afa5d2009-06-16 15:33:20 -07004251
4252 /*
Zhihui Zhang95bbc0c2015-06-24 16:56:42 -07004253 * If RECLAIM_UNMAP is set, then all file pages are considered
Mel Gorman90afa5d2009-06-16 15:33:20 -07004254 * potentially reclaimable. Otherwise, we have to worry about
Mel Gorman11fb9982016-07-28 15:46:20 -07004255 * pages like swapcache and node_unmapped_file_pages() provides
Mel Gorman90afa5d2009-06-16 15:33:20 -07004256 * a better estimate
4257 */
Mel Gormana5f5f912016-07-28 15:46:32 -07004258 if (node_reclaim_mode & RECLAIM_UNMAP)
4259 nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES);
Mel Gorman90afa5d2009-06-16 15:33:20 -07004260 else
Mel Gormana5f5f912016-07-28 15:46:32 -07004261 nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat);
Mel Gorman90afa5d2009-06-16 15:33:20 -07004262
4263 /* If we can't clean pages, remove dirty pages from consideration */
Mel Gormana5f5f912016-07-28 15:46:32 -07004264 if (!(node_reclaim_mode & RECLAIM_WRITE))
4265 delta += node_page_state(pgdat, NR_FILE_DIRTY);
Mel Gorman90afa5d2009-06-16 15:33:20 -07004266
4267 /* Watch for any possible underflows due to delta */
4268 if (unlikely(delta > nr_pagecache_reclaimable))
4269 delta = nr_pagecache_reclaimable;
4270
4271 return nr_pagecache_reclaimable - delta;
4272}
4273
Christoph Lameter0ff38492006-09-25 23:31:52 -07004274/*
Mel Gormana5f5f912016-07-28 15:46:32 -07004275 * Try to free up some pages from this node through reclaim.
Christoph Lameter9eeff232006-01-18 17:42:31 -08004276 */
Mel Gormana5f5f912016-07-28 15:46:32 -07004277static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
Christoph Lameter9eeff232006-01-18 17:42:31 -08004278{
Christoph Lameter7fb2d462006-03-22 00:08:22 -08004279 /* Minimum pages needed in order to stay on node */
Andrew Morton69e05942006-03-22 00:08:19 -08004280 const unsigned long nr_pages = 1 << order;
Christoph Lameter9eeff232006-01-18 17:42:31 -08004281 struct task_struct *p = current;
Vlastimil Babka499118e2017-05-08 15:59:50 -07004282 unsigned int noreclaim_flag;
Andrew Morton179e9632006-03-22 00:08:18 -08004283 struct scan_control sc = {
Andrew Morton62b726c2013-02-22 16:32:24 -08004284 .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
Nick Desaulniersf2f43e52017-07-06 15:36:50 -07004285 .gfp_mask = current_gfp_context(gfp_mask),
Johannes Weinerbd2f6192009-03-31 15:19:38 -07004286 .order = order,
Mel Gormana5f5f912016-07-28 15:46:32 -07004287 .priority = NODE_RECLAIM_PRIORITY,
4288 .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE),
4289 .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP),
Johannes Weineree814fe2014-08-06 16:06:19 -07004290 .may_swap = 1,
Nick Desaulniersf2f43e52017-07-06 15:36:50 -07004291 .reclaim_idx = gfp_zone(gfp_mask),
Andrew Morton179e9632006-03-22 00:08:18 -08004292 };
Christoph Lameter9eeff232006-01-18 17:42:31 -08004293
Yafang Shao132bb8c2019-05-13 17:17:53 -07004294 trace_mm_vmscan_node_reclaim_begin(pgdat->node_id, order,
4295 sc.gfp_mask);
4296
Christoph Lameter9eeff232006-01-18 17:42:31 -08004297 cond_resched();
Omar Sandoval93781322018-06-07 17:07:02 -07004298 fs_reclaim_acquire(sc.gfp_mask);
Christoph Lameterd4f77962006-02-24 13:04:22 -08004299 /*
Zhihui Zhang95bbc0c2015-06-24 16:56:42 -07004300 * We need to be able to allocate from the reserves for RECLAIM_UNMAP
Christoph Lameterd4f77962006-02-24 13:04:22 -08004301 * and we also need to be able to write out pages for RECLAIM_WRITE
Zhihui Zhang95bbc0c2015-06-24 16:56:42 -07004302 * and RECLAIM_UNMAP.
Christoph Lameterd4f77962006-02-24 13:04:22 -08004303 */
Vlastimil Babka499118e2017-05-08 15:59:50 -07004304 noreclaim_flag = memalloc_noreclaim_save();
4305 p->flags |= PF_SWAPWRITE;
Andrew Morton1732d2b012019-07-16 16:26:15 -07004306 set_task_reclaim_state(p, &sc.reclaim_state);
Christoph Lameterc84db232006-02-01 03:05:29 -08004307
Mel Gormana5f5f912016-07-28 15:46:32 -07004308 if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages) {
Christoph Lameter0ff38492006-09-25 23:31:52 -07004309 /*
Andrey Ryabinin894befe2018-04-10 16:27:51 -07004310 * Free memory by calling shrink node with increasing
Christoph Lameter0ff38492006-09-25 23:31:52 -07004311 * priorities until we have enough memory freed.
4312 */
Christoph Lameter0ff38492006-09-25 23:31:52 -07004313 do {
Mel Gorman970a39a2016-07-28 15:46:35 -07004314 shrink_node(pgdat, &sc);
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07004315 } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0);
Christoph Lameter0ff38492006-09-25 23:31:52 -07004316 }
Christoph Lameterc84db232006-02-01 03:05:29 -08004317
Andrew Morton1732d2b012019-07-16 16:26:15 -07004318 set_task_reclaim_state(p, NULL);
Vlastimil Babka499118e2017-05-08 15:59:50 -07004319 current->flags &= ~PF_SWAPWRITE;
4320 memalloc_noreclaim_restore(noreclaim_flag);
Omar Sandoval93781322018-06-07 17:07:02 -07004321 fs_reclaim_release(sc.gfp_mask);
Yafang Shao132bb8c2019-05-13 17:17:53 -07004322
4323 trace_mm_vmscan_node_reclaim_end(sc.nr_reclaimed);
4324
Rik van Riela79311c2009-01-06 14:40:01 -08004325 return sc.nr_reclaimed >= nr_pages;
Christoph Lameter9eeff232006-01-18 17:42:31 -08004326}
Andrew Morton179e9632006-03-22 00:08:18 -08004327
Mel Gormana5f5f912016-07-28 15:46:32 -07004328int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
Andrew Morton179e9632006-03-22 00:08:18 -08004329{
David Rientjesd773ed62007-10-16 23:26:01 -07004330 int ret;
Andrew Morton179e9632006-03-22 00:08:18 -08004331
4332 /*
Mel Gormana5f5f912016-07-28 15:46:32 -07004333 * Node reclaim reclaims unmapped file backed pages and
Christoph Lameter0ff38492006-09-25 23:31:52 -07004334 * slab pages if we are over the defined limits.
Christoph Lameter34aa1332006-06-30 01:55:37 -07004335 *
Christoph Lameter96146342006-07-03 00:24:13 -07004336 * A small portion of unmapped file backed pages is needed for
4337 * file I/O otherwise pages read by file I/O will be immediately
Mel Gormana5f5f912016-07-28 15:46:32 -07004338 * thrown out if the node is overallocated. So we do not reclaim
4339 * if less than a specified percentage of the node is used by
Christoph Lameter96146342006-07-03 00:24:13 -07004340 * unmapped file backed pages.
Andrew Morton179e9632006-03-22 00:08:18 -08004341 */
Mel Gormana5f5f912016-07-28 15:46:32 -07004342 if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages &&
Roman Gushchind42f3242020-08-06 23:20:39 -07004343 node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B) <=
4344 pgdat->min_slab_pages)
Mel Gormana5f5f912016-07-28 15:46:32 -07004345 return NODE_RECLAIM_FULL;
Andrew Morton179e9632006-03-22 00:08:18 -08004346
4347 /*
David Rientjesd773ed62007-10-16 23:26:01 -07004348 * Do not scan if the allocation should not be delayed.
Andrew Morton179e9632006-03-22 00:08:18 -08004349 */
Mel Gormand0164ad2015-11-06 16:28:21 -08004350 if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC))
Mel Gormana5f5f912016-07-28 15:46:32 -07004351 return NODE_RECLAIM_NOSCAN;
Andrew Morton179e9632006-03-22 00:08:18 -08004352
4353 /*
Mel Gormana5f5f912016-07-28 15:46:32 -07004354 * Only run node reclaim on the local node or on nodes that do not
Andrew Morton179e9632006-03-22 00:08:18 -08004355 * have associated processors. This will favor the local processor
4356 * over remote processors and spread off node memory allocations
4357 * as wide as possible.
4358 */
Mel Gormana5f5f912016-07-28 15:46:32 -07004359 if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id())
4360 return NODE_RECLAIM_NOSCAN;
David Rientjesd773ed62007-10-16 23:26:01 -07004361
Mel Gormana5f5f912016-07-28 15:46:32 -07004362 if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags))
4363 return NODE_RECLAIM_NOSCAN;
Mel Gormanfa5e0842009-06-16 15:33:22 -07004364
Mel Gormana5f5f912016-07-28 15:46:32 -07004365 ret = __node_reclaim(pgdat, gfp_mask, order);
4366 clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags);
David Rientjesd773ed62007-10-16 23:26:01 -07004367
Mel Gorman24cf725182009-06-16 15:33:23 -07004368 if (!ret)
4369 count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED);
4370
David Rientjesd773ed62007-10-16 23:26:01 -07004371 return ret;
Andrew Morton179e9632006-03-22 00:08:18 -08004372}
Christoph Lameter9eeff232006-01-18 17:42:31 -08004373#endif
Lee Schermerhorn894bc312008-10-18 20:26:39 -07004374
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07004375/**
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00004376 * check_move_unevictable_pages - check pages for evictability and move to
4377 * appropriate zone lru list
4378 * @pvec: pagevec with lru pages to check
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07004379 *
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00004380 * Checks pages for evictability, if an evictable page is in the unevictable
4381 * lru list, moves it to the appropriate evictable lru list. This function
4382 * should be only used for lru pages.
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07004383 */
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00004384void check_move_unevictable_pages(struct pagevec *pvec)
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07004385{
Alex Shi6168d0d2020-12-15 12:34:29 -08004386 struct lruvec *lruvec = NULL;
Hugh Dickins24513262012-01-20 14:34:21 -08004387 int pgscanned = 0;
4388 int pgrescued = 0;
4389 int i;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07004390
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00004391 for (i = 0; i < pvec->nr; i++) {
4392 struct page *page = pvec->pages[i];
Hugh Dickins8d8869c2020-09-18 21:20:12 -07004393 int nr_pages;
Lee Schermerhornaf936a12008-10-18 20:26:53 -07004394
Hugh Dickins8d8869c2020-09-18 21:20:12 -07004395 if (PageTransTail(page))
4396 continue;
4397
4398 nr_pages = thp_nr_pages(page);
4399 pgscanned += nr_pages;
4400
Alex Shid25b5bd2020-12-15 12:34:16 -08004401 /* block memcg migration during page moving between lru */
4402 if (!TestClearPageLRU(page))
4403 continue;
4404
Alexander Duyck2a5e4e32020-12-15 12:34:33 -08004405 lruvec = relock_page_lruvec_irq(page, lruvec);
Alex Shid25b5bd2020-12-15 12:34:16 -08004406 if (page_evictable(page) && PageUnevictable(page)) {
Yu Zhao46ae6b22021-02-24 12:08:25 -08004407 del_page_from_lru_list(page, lruvec);
Hugh Dickins24513262012-01-20 14:34:21 -08004408 ClearPageUnevictable(page);
Yu Zhao3a9c9782021-02-24 12:08:17 -08004409 add_page_to_lru_list(page, lruvec);
Hugh Dickins8d8869c2020-09-18 21:20:12 -07004410 pgrescued += nr_pages;
Hugh Dickins24513262012-01-20 14:34:21 -08004411 }
Alex Shid25b5bd2020-12-15 12:34:16 -08004412 SetPageLRU(page);
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07004413 }
Hugh Dickins24513262012-01-20 14:34:21 -08004414
Alex Shi6168d0d2020-12-15 12:34:29 -08004415 if (lruvec) {
Hugh Dickins24513262012-01-20 14:34:21 -08004416 __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued);
4417 __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned);
Alex Shi6168d0d2020-12-15 12:34:29 -08004418 unlock_page_lruvec_irq(lruvec);
Alex Shid25b5bd2020-12-15 12:34:16 -08004419 } else if (pgscanned) {
4420 count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned);
Hugh Dickins24513262012-01-20 14:34:21 -08004421 }
Hugh Dickins850465792012-01-20 14:34:19 -08004422}
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00004423EXPORT_SYMBOL_GPL(check_move_unevictable_pages);