blob: d1601163d895da9cfff3386c66ac4f51ea9ed30e [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 Shia2fb1262021-05-04 18:36:17 -0700188static int shrinker_nr_max;
Yang Shi2bfd3632021-05-04 18:36:11 -0700189
Yang Shia2fb1262021-05-04 18:36:17 -0700190static inline int shrinker_map_size(int nr_items)
191{
192 return (DIV_ROUND_UP(nr_items, BITS_PER_LONG) * sizeof(unsigned long));
193}
Yang Shi2bfd3632021-05-04 18:36:11 -0700194
Yang Shi468ab842021-05-04 18:36:26 -0700195static struct shrinker_info *shrinker_info_protected(struct mem_cgroup *memcg,
196 int nid)
197{
198 return rcu_dereference_protected(memcg->nodeinfo[nid]->shrinker_info,
199 lockdep_is_held(&shrinker_rwsem));
200}
201
Yang Shie4262c42021-05-04 18:36:23 -0700202static int expand_one_shrinker_info(struct mem_cgroup *memcg,
203 int size, int old_size)
Yang Shi2bfd3632021-05-04 18:36:11 -0700204{
Yang Shie4262c42021-05-04 18:36:23 -0700205 struct shrinker_info *new, *old;
Yang Shi2bfd3632021-05-04 18:36:11 -0700206 struct mem_cgroup_per_node *pn;
207 int nid;
208
Yang Shi2bfd3632021-05-04 18:36:11 -0700209 for_each_node(nid) {
210 pn = memcg->nodeinfo[nid];
Yang Shi468ab842021-05-04 18:36:26 -0700211 old = shrinker_info_protected(memcg, nid);
Yang Shi2bfd3632021-05-04 18:36:11 -0700212 /* Not yet online memcg */
213 if (!old)
214 return 0;
215
216 new = kvmalloc_node(sizeof(*new) + size, GFP_KERNEL, nid);
217 if (!new)
218 return -ENOMEM;
219
220 /* Set all old bits, clear all new bits */
221 memset(new->map, (int)0xff, old_size);
222 memset((void *)new->map + old_size, 0, size - old_size);
223
Yang Shie4262c42021-05-04 18:36:23 -0700224 rcu_assign_pointer(pn->shrinker_info, new);
Yang Shi72673e82021-05-04 18:36:20 -0700225 kvfree_rcu(old, rcu);
Yang Shi2bfd3632021-05-04 18:36:11 -0700226 }
227
228 return 0;
229}
230
Yang Shie4262c42021-05-04 18:36:23 -0700231void free_shrinker_info(struct mem_cgroup *memcg)
Yang Shi2bfd3632021-05-04 18:36:11 -0700232{
233 struct mem_cgroup_per_node *pn;
Yang Shie4262c42021-05-04 18:36:23 -0700234 struct shrinker_info *info;
Yang Shi2bfd3632021-05-04 18:36:11 -0700235 int nid;
236
237 if (mem_cgroup_is_root(memcg))
238 return;
239
240 for_each_node(nid) {
241 pn = memcg->nodeinfo[nid];
Yang Shie4262c42021-05-04 18:36:23 -0700242 info = rcu_dereference_protected(pn->shrinker_info, true);
243 kvfree(info);
244 rcu_assign_pointer(pn->shrinker_info, NULL);
Yang Shi2bfd3632021-05-04 18:36:11 -0700245 }
246}
247
Yang Shie4262c42021-05-04 18:36:23 -0700248int alloc_shrinker_info(struct mem_cgroup *memcg)
Yang Shi2bfd3632021-05-04 18:36:11 -0700249{
Yang Shie4262c42021-05-04 18:36:23 -0700250 struct shrinker_info *info;
Yang Shi2bfd3632021-05-04 18:36:11 -0700251 int nid, size, ret = 0;
252
253 if (mem_cgroup_is_root(memcg))
254 return 0;
255
Yang Shid27cf2a2021-05-04 18:36:14 -0700256 down_write(&shrinker_rwsem);
Yang Shia2fb1262021-05-04 18:36:17 -0700257 size = shrinker_map_size(shrinker_nr_max);
Yang Shi2bfd3632021-05-04 18:36:11 -0700258 for_each_node(nid) {
Yang Shie4262c42021-05-04 18:36:23 -0700259 info = kvzalloc_node(sizeof(*info) + size, GFP_KERNEL, nid);
260 if (!info) {
261 free_shrinker_info(memcg);
Yang Shi2bfd3632021-05-04 18:36:11 -0700262 ret = -ENOMEM;
263 break;
264 }
Yang Shie4262c42021-05-04 18:36:23 -0700265 rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_info, info);
Yang Shi2bfd3632021-05-04 18:36:11 -0700266 }
Yang Shid27cf2a2021-05-04 18:36:14 -0700267 up_write(&shrinker_rwsem);
Yang Shi2bfd3632021-05-04 18:36:11 -0700268
269 return ret;
270}
271
Yang Shie4262c42021-05-04 18:36:23 -0700272static int expand_shrinker_info(int new_id)
Yang Shi2bfd3632021-05-04 18:36:11 -0700273{
274 int size, old_size, ret = 0;
Yang Shia2fb1262021-05-04 18:36:17 -0700275 int new_nr_max = new_id + 1;
Yang Shi2bfd3632021-05-04 18:36:11 -0700276 struct mem_cgroup *memcg;
277
Yang Shia2fb1262021-05-04 18:36:17 -0700278 size = shrinker_map_size(new_nr_max);
279 old_size = shrinker_map_size(shrinker_nr_max);
Yang Shi2bfd3632021-05-04 18:36:11 -0700280 if (size <= old_size)
Yang Shia2fb1262021-05-04 18:36:17 -0700281 goto out;
Yang Shi2bfd3632021-05-04 18:36:11 -0700282
Yang Shi2bfd3632021-05-04 18:36:11 -0700283 if (!root_mem_cgroup)
Yang Shid27cf2a2021-05-04 18:36:14 -0700284 goto out;
285
286 lockdep_assert_held(&shrinker_rwsem);
Yang Shi2bfd3632021-05-04 18:36:11 -0700287
288 memcg = mem_cgroup_iter(NULL, NULL, NULL);
289 do {
290 if (mem_cgroup_is_root(memcg))
291 continue;
Yang Shie4262c42021-05-04 18:36:23 -0700292 ret = expand_one_shrinker_info(memcg, size, old_size);
Yang Shi2bfd3632021-05-04 18:36:11 -0700293 if (ret) {
294 mem_cgroup_iter_break(NULL, memcg);
Yang Shid27cf2a2021-05-04 18:36:14 -0700295 goto out;
Yang Shi2bfd3632021-05-04 18:36:11 -0700296 }
297 } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL);
Yang Shid27cf2a2021-05-04 18:36:14 -0700298out:
Yang Shi2bfd3632021-05-04 18:36:11 -0700299 if (!ret)
Yang Shia2fb1262021-05-04 18:36:17 -0700300 shrinker_nr_max = new_nr_max;
Yang Shid27cf2a2021-05-04 18:36:14 -0700301
Yang Shi2bfd3632021-05-04 18:36:11 -0700302 return ret;
303}
304
305void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id)
306{
307 if (shrinker_id >= 0 && memcg && !mem_cgroup_is_root(memcg)) {
Yang Shie4262c42021-05-04 18:36:23 -0700308 struct shrinker_info *info;
Yang Shi2bfd3632021-05-04 18:36:11 -0700309
310 rcu_read_lock();
Yang Shie4262c42021-05-04 18:36:23 -0700311 info = rcu_dereference(memcg->nodeinfo[nid]->shrinker_info);
Yang Shi2bfd3632021-05-04 18:36:11 -0700312 /* Pairs with smp mb in shrink_slab() */
313 smp_mb__before_atomic();
Yang Shie4262c42021-05-04 18:36:23 -0700314 set_bit(shrinker_id, info->map);
Yang Shi2bfd3632021-05-04 18:36:11 -0700315 rcu_read_unlock();
316 }
317}
318
Kirill Tkhaib4c2b232018-08-17 15:47:29 -0700319static DEFINE_IDR(shrinker_idr);
Kirill Tkhaib4c2b232018-08-17 15:47:29 -0700320
321static int prealloc_memcg_shrinker(struct shrinker *shrinker)
322{
323 int id, ret = -ENOMEM;
324
325 down_write(&shrinker_rwsem);
326 /* This may call shrinker, so it must use down_read_trylock() */
Yang Shi41ca6682021-05-04 18:36:29 -0700327 id = idr_alloc(&shrinker_idr, shrinker, 0, 0, GFP_KERNEL);
Kirill Tkhaib4c2b232018-08-17 15:47:29 -0700328 if (id < 0)
329 goto unlock;
330
Kirill Tkhai0a4465d2018-08-17 15:47:37 -0700331 if (id >= shrinker_nr_max) {
Yang Shie4262c42021-05-04 18:36:23 -0700332 if (expand_shrinker_info(id)) {
Kirill Tkhai0a4465d2018-08-17 15:47:37 -0700333 idr_remove(&shrinker_idr, id);
334 goto unlock;
335 }
Kirill Tkhai0a4465d2018-08-17 15:47:37 -0700336 }
Kirill Tkhaib4c2b232018-08-17 15:47:29 -0700337 shrinker->id = id;
338 ret = 0;
339unlock:
340 up_write(&shrinker_rwsem);
341 return ret;
342}
343
344static void unregister_memcg_shrinker(struct shrinker *shrinker)
345{
346 int id = shrinker->id;
347
348 BUG_ON(id < 0);
349
Yang Shi41ca6682021-05-04 18:36:29 -0700350 lockdep_assert_held(&shrinker_rwsem);
351
Kirill Tkhaib4c2b232018-08-17 15:47:29 -0700352 idr_remove(&shrinker_idr, id);
Kirill Tkhaib4c2b232018-08-17 15:47:29 -0700353}
Kirill Tkhaib4c2b232018-08-17 15:47:29 -0700354
Johannes Weinerb5ead352019-11-30 17:55:40 -0800355static bool cgroup_reclaim(struct scan_control *sc)
Johannes Weiner89b5fae2012-01-12 17:17:50 -0800356{
Johannes Weinerb5ead352019-11-30 17:55:40 -0800357 return sc->target_mem_cgroup;
Johannes Weiner89b5fae2012-01-12 17:17:50 -0800358}
Tejun Heo97c93412015-05-22 18:23:36 -0400359
360/**
Johannes Weinerb5ead352019-11-30 17:55:40 -0800361 * writeback_throttling_sane - is the usual dirty throttling mechanism available?
Tejun Heo97c93412015-05-22 18:23:36 -0400362 * @sc: scan_control in question
363 *
364 * The normal page dirty throttling mechanism in balance_dirty_pages() is
365 * completely broken with the legacy memcg and direct stalling in
366 * shrink_page_list() is used for throttling instead, which lacks all the
367 * niceties such as fairness, adaptive pausing, bandwidth proportional
368 * allocation and configurability.
369 *
370 * This function tests whether the vmscan currently in progress can assume
371 * that the normal dirty throttling mechanism is operational.
372 */
Johannes Weinerb5ead352019-11-30 17:55:40 -0800373static bool writeback_throttling_sane(struct scan_control *sc)
Tejun Heo97c93412015-05-22 18:23:36 -0400374{
Johannes Weinerb5ead352019-11-30 17:55:40 -0800375 if (!cgroup_reclaim(sc))
Tejun Heo97c93412015-05-22 18:23:36 -0400376 return true;
377#ifdef CONFIG_CGROUP_WRITEBACK
Linus Torvalds69234ac2015-11-05 14:51:32 -0800378 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
Tejun Heo97c93412015-05-22 18:23:36 -0400379 return true;
380#endif
381 return false;
382}
KAMEZAWA Hiroyuki91a45472008-02-07 00:14:29 -0800383#else
Yang Shi0a432dc2019-09-23 15:38:12 -0700384static int prealloc_memcg_shrinker(struct shrinker *shrinker)
385{
386 return 0;
387}
388
389static void unregister_memcg_shrinker(struct shrinker *shrinker)
390{
391}
392
Johannes Weinerb5ead352019-11-30 17:55:40 -0800393static bool cgroup_reclaim(struct scan_control *sc)
Johannes Weiner89b5fae2012-01-12 17:17:50 -0800394{
Johannes Weinerb5ead352019-11-30 17:55:40 -0800395 return false;
Johannes Weiner89b5fae2012-01-12 17:17:50 -0800396}
Tejun Heo97c93412015-05-22 18:23:36 -0400397
Johannes Weinerb5ead352019-11-30 17:55:40 -0800398static bool writeback_throttling_sane(struct scan_control *sc)
Tejun Heo97c93412015-05-22 18:23:36 -0400399{
400 return true;
401}
KAMEZAWA Hiroyuki91a45472008-02-07 00:14:29 -0800402#endif
403
Mel Gorman5a1c84b2016-07-28 15:47:31 -0700404/*
405 * This misses isolated pages which are not accounted for to save counters.
406 * As the data only determines if reclaim or compaction continues, it is
407 * not expected that isolated pages will be a dominating factor.
408 */
409unsigned long zone_reclaimable_pages(struct zone *zone)
410{
411 unsigned long nr;
412
413 nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) +
414 zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE);
415 if (get_nr_swap_pages() > 0)
416 nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) +
417 zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON);
418
419 return nr;
420}
421
Michal Hockofd538802017-02-22 15:45:58 -0800422/**
423 * lruvec_lru_size - Returns the number of pages on the given LRU list.
424 * @lruvec: lru vector
425 * @lru: lru to use
426 * @zone_idx: zones to consider (use MAX_NR_ZONES for the whole LRU list)
427 */
Yu Zhao20913392021-02-24 12:08:44 -0800428static unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru,
429 int zone_idx)
KOSAKI Motohiroc9f299d2009-01-07 18:08:16 -0800430{
Johannes Weinerde3b0152019-11-30 17:55:31 -0800431 unsigned long size = 0;
Michal Hockofd538802017-02-22 15:45:58 -0800432 int zid;
433
Johannes Weinerde3b0152019-11-30 17:55:31 -0800434 for (zid = 0; zid <= zone_idx && zid < MAX_NR_ZONES; zid++) {
Michal Hockofd538802017-02-22 15:45:58 -0800435 struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid];
KOSAKI Motohiroc9f299d2009-01-07 18:08:16 -0800436
Michal Hockofd538802017-02-22 15:45:58 -0800437 if (!managed_zone(zone))
438 continue;
Michal Hockob4536f0c82017-01-10 16:58:04 -0800439
Michal Hockofd538802017-02-22 15:45:58 -0800440 if (!mem_cgroup_disabled())
Johannes Weinerde3b0152019-11-30 17:55:31 -0800441 size += mem_cgroup_get_zone_lru_size(lruvec, lru, zid);
Michal Hockofd538802017-02-22 15:45:58 -0800442 else
Johannes Weinerde3b0152019-11-30 17:55:31 -0800443 size += zone_page_state(zone, NR_ZONE_LRU_BASE + lru);
Michal Hockofd538802017-02-22 15:45:58 -0800444 }
Johannes Weinerde3b0152019-11-30 17:55:31 -0800445 return size;
Michal Hockob4536f0c82017-01-10 16:58:04 -0800446}
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448/*
Glauber Costa1d3d4432013-08-28 10:18:04 +1000449 * Add a shrinker callback to be called from the vm.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 */
Tetsuo Handa8e049442018-04-04 19:53:07 +0900451int prealloc_shrinker(struct shrinker *shrinker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Alexey Dobriyanb9726c22019-03-05 15:48:26 -0800453 unsigned int size = sizeof(*shrinker->nr_deferred);
Glauber Costa1d3d4432013-08-28 10:18:04 +1000454
Glauber Costa1d3d4432013-08-28 10:18:04 +1000455 if (shrinker->flags & SHRINKER_NUMA_AWARE)
456 size *= nr_node_ids;
457
458 shrinker->nr_deferred = kzalloc(size, GFP_KERNEL);
459 if (!shrinker->nr_deferred)
460 return -ENOMEM;
Kirill Tkhaib4c2b232018-08-17 15:47:29 -0700461
462 if (shrinker->flags & SHRINKER_MEMCG_AWARE) {
463 if (prealloc_memcg_shrinker(shrinker))
464 goto free_deferred;
465 }
466
Tetsuo Handa8e049442018-04-04 19:53:07 +0900467 return 0;
Kirill Tkhaib4c2b232018-08-17 15:47:29 -0700468
469free_deferred:
470 kfree(shrinker->nr_deferred);
471 shrinker->nr_deferred = NULL;
472 return -ENOMEM;
Tetsuo Handa8e049442018-04-04 19:53:07 +0900473}
Glauber Costa1d3d4432013-08-28 10:18:04 +1000474
Tetsuo Handa8e049442018-04-04 19:53:07 +0900475void free_prealloced_shrinker(struct shrinker *shrinker)
476{
Kirill Tkhaib4c2b232018-08-17 15:47:29 -0700477 if (!shrinker->nr_deferred)
478 return;
479
Yang Shi41ca6682021-05-04 18:36:29 -0700480 if (shrinker->flags & SHRINKER_MEMCG_AWARE) {
481 down_write(&shrinker_rwsem);
Kirill Tkhaib4c2b232018-08-17 15:47:29 -0700482 unregister_memcg_shrinker(shrinker);
Yang Shi41ca6682021-05-04 18:36:29 -0700483 up_write(&shrinker_rwsem);
484 }
Kirill Tkhaib4c2b232018-08-17 15:47:29 -0700485
Tetsuo Handa8e049442018-04-04 19:53:07 +0900486 kfree(shrinker->nr_deferred);
487 shrinker->nr_deferred = NULL;
488}
489
490void register_shrinker_prepared(struct shrinker *shrinker)
491{
Rusty Russell8e1f9362007-07-17 04:03:17 -0700492 down_write(&shrinker_rwsem);
493 list_add_tail(&shrinker->list, &shrinker_list);
Yang Shi41ca6682021-05-04 18:36:29 -0700494 shrinker->flags |= SHRINKER_REGISTERED;
Rusty Russell8e1f9362007-07-17 04:03:17 -0700495 up_write(&shrinker_rwsem);
Tetsuo Handa8e049442018-04-04 19:53:07 +0900496}
497
498int register_shrinker(struct shrinker *shrinker)
499{
500 int err = prealloc_shrinker(shrinker);
501
502 if (err)
503 return err;
504 register_shrinker_prepared(shrinker);
Glauber Costa1d3d4432013-08-28 10:18:04 +1000505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
Rusty Russell8e1f9362007-07-17 04:03:17 -0700507EXPORT_SYMBOL(register_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509/*
510 * Remove one
511 */
Rusty Russell8e1f9362007-07-17 04:03:17 -0700512void unregister_shrinker(struct shrinker *shrinker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Yang Shi41ca6682021-05-04 18:36:29 -0700514 if (!(shrinker->flags & SHRINKER_REGISTERED))
Tetsuo Handabb422a72017-12-18 20:31:41 +0900515 return;
Yang Shi41ca6682021-05-04 18:36:29 -0700516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 down_write(&shrinker_rwsem);
518 list_del(&shrinker->list);
Yang Shi41ca6682021-05-04 18:36:29 -0700519 shrinker->flags &= ~SHRINKER_REGISTERED;
520 if (shrinker->flags & SHRINKER_MEMCG_AWARE)
521 unregister_memcg_shrinker(shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 up_write(&shrinker_rwsem);
Yang Shi41ca6682021-05-04 18:36:29 -0700523
Andrew Vaginae393322013-10-16 13:46:46 -0700524 kfree(shrinker->nr_deferred);
Tetsuo Handabb422a72017-12-18 20:31:41 +0900525 shrinker->nr_deferred = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
Rusty Russell8e1f9362007-07-17 04:03:17 -0700527EXPORT_SYMBOL(unregister_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529#define SHRINK_BATCH 128
Glauber Costa1d3d4432013-08-28 10:18:04 +1000530
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800531static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,
Josef Bacik9092c712018-01-31 16:16:26 -0800532 struct shrinker *shrinker, int priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Glauber Costa1d3d4432013-08-28 10:18:04 +1000534 unsigned long freed = 0;
535 unsigned long long delta;
536 long total_scan;
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700537 long freeable;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000538 long nr;
539 long new_nr;
540 int nid = shrinkctl->nid;
541 long batch_size = shrinker->batch ? shrinker->batch
542 : SHRINK_BATCH;
Shaohua Li5f33a082016-12-12 16:41:50 -0800543 long scanned = 0, next_deferred;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000544
Kirill Tkhaiac7fb3a2018-08-17 15:48:30 -0700545 if (!(shrinker->flags & SHRINKER_NUMA_AWARE))
546 nid = 0;
547
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700548 freeable = shrinker->count_objects(shrinker, shrinkctl);
Kirill Tkhai9b996462018-08-17 15:48:21 -0700549 if (freeable == 0 || freeable == SHRINK_EMPTY)
550 return freeable;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000551
552 /*
553 * copy the current shrinker scan count into a local variable
554 * and zero it so that other concurrent shrinker invocations
555 * don't also do this scanning work.
556 */
557 nr = atomic_long_xchg(&shrinker->nr_deferred[nid], 0);
558
559 total_scan = nr;
Johannes Weiner4b85afb2018-10-26 15:06:42 -0700560 if (shrinker->seeks) {
561 delta = freeable >> priority;
562 delta *= 4;
563 do_div(delta, shrinker->seeks);
564 } else {
565 /*
566 * These objects don't require any IO to create. Trim
567 * them aggressively under memory pressure to keep
568 * them from causing refetches in the IO caches.
569 */
570 delta = freeable / 2;
571 }
Roman Gushchin172b06c32018-09-20 12:22:46 -0700572
Glauber Costa1d3d4432013-08-28 10:18:04 +1000573 total_scan += delta;
574 if (total_scan < 0) {
Sakari Ailusd75f7732019-03-25 21:32:28 +0200575 pr_err("shrink_slab: %pS negative objects to delete nr=%ld\n",
Dave Chinnera0b02132013-08-28 10:18:16 +1000576 shrinker->scan_objects, total_scan);
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700577 total_scan = freeable;
Shaohua Li5f33a082016-12-12 16:41:50 -0800578 next_deferred = nr;
579 } else
580 next_deferred = total_scan;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000581
582 /*
583 * We need to avoid excessive windup on filesystem shrinkers
584 * due to large numbers of GFP_NOFS allocations causing the
585 * shrinkers to return -1 all the time. This results in a large
586 * nr being built up so when a shrink that can do some work
587 * comes along it empties the entire cache due to nr >>>
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700588 * freeable. This is bad for sustaining a working set in
Glauber Costa1d3d4432013-08-28 10:18:04 +1000589 * memory.
590 *
591 * Hence only allow the shrinker to scan the entire cache when
592 * a large delta change is calculated directly.
593 */
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700594 if (delta < freeable / 4)
595 total_scan = min(total_scan, freeable / 2);
Glauber Costa1d3d4432013-08-28 10:18:04 +1000596
597 /*
598 * Avoid risking looping forever due to too large nr value:
599 * never try to free more than twice the estimate number of
600 * freeable entries.
601 */
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700602 if (total_scan > freeable * 2)
603 total_scan = freeable * 2;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000604
605 trace_mm_shrink_slab_start(shrinker, shrinkctl, nr,
Josef Bacik9092c712018-01-31 16:16:26 -0800606 freeable, delta, total_scan, priority);
Glauber Costa1d3d4432013-08-28 10:18:04 +1000607
Vladimir Davydov0b1fb402014-01-23 15:53:22 -0800608 /*
609 * Normally, we should not scan less than batch_size objects in one
610 * pass to avoid too frequent shrinker calls, but if the slab has less
611 * than batch_size objects in total and we are really tight on memory,
612 * we will try to reclaim all available objects, otherwise we can end
613 * up failing allocations although there are plenty of reclaimable
614 * objects spread over several slabs with usage less than the
615 * batch_size.
616 *
617 * We detect the "tight on memory" situations by looking at the total
618 * number of objects we want to scan (total_scan). If it is greater
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700619 * than the total number of objects on slab (freeable), we must be
Vladimir Davydov0b1fb402014-01-23 15:53:22 -0800620 * scanning at high prio and therefore should try to reclaim as much as
621 * possible.
622 */
623 while (total_scan >= batch_size ||
Vladimir Davydovd5bc5fd2014-04-03 14:47:32 -0700624 total_scan >= freeable) {
Dave Chinnera0b02132013-08-28 10:18:16 +1000625 unsigned long ret;
Vladimir Davydov0b1fb402014-01-23 15:53:22 -0800626 unsigned long nr_to_scan = min(batch_size, total_scan);
Glauber Costa1d3d4432013-08-28 10:18:04 +1000627
Vladimir Davydov0b1fb402014-01-23 15:53:22 -0800628 shrinkctl->nr_to_scan = nr_to_scan;
Chris Wilsond460acb2017-09-06 16:19:26 -0700629 shrinkctl->nr_scanned = nr_to_scan;
Dave Chinnera0b02132013-08-28 10:18:16 +1000630 ret = shrinker->scan_objects(shrinker, shrinkctl);
631 if (ret == SHRINK_STOP)
632 break;
633 freed += ret;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000634
Chris Wilsond460acb2017-09-06 16:19:26 -0700635 count_vm_events(SLABS_SCANNED, shrinkctl->nr_scanned);
636 total_scan -= shrinkctl->nr_scanned;
637 scanned += shrinkctl->nr_scanned;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000638
639 cond_resched();
640 }
641
Shaohua Li5f33a082016-12-12 16:41:50 -0800642 if (next_deferred >= scanned)
643 next_deferred -= scanned;
644 else
645 next_deferred = 0;
Glauber Costa1d3d4432013-08-28 10:18:04 +1000646 /*
647 * move the unused scan count back into the shrinker in a
648 * manner that handles concurrent updates. If we exhausted the
649 * scan, there is no need to do an update.
650 */
Shaohua Li5f33a082016-12-12 16:41:50 -0800651 if (next_deferred > 0)
652 new_nr = atomic_long_add_return(next_deferred,
Glauber Costa1d3d4432013-08-28 10:18:04 +1000653 &shrinker->nr_deferred[nid]);
654 else
655 new_nr = atomic_long_read(&shrinker->nr_deferred[nid]);
656
Yang Shi8efb4b52021-05-04 18:36:08 -0700657 trace_mm_shrink_slab_end(shrinker, shrinkctl->nid, freed, nr, new_nr, total_scan);
Glauber Costa1d3d4432013-08-28 10:18:04 +1000658 return freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
Yang Shi0a432dc2019-09-23 15:38:12 -0700661#ifdef CONFIG_MEMCG
Kirill Tkhaib0dedc42018-08-17 15:48:14 -0700662static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,
663 struct mem_cgroup *memcg, int priority)
664{
Yang Shie4262c42021-05-04 18:36:23 -0700665 struct shrinker_info *info;
Kirill Tkhaib8e57ef2018-10-05 15:52:10 -0700666 unsigned long ret, freed = 0;
667 int i;
Kirill Tkhaib0dedc42018-08-17 15:48:14 -0700668
Yang Shi0a432dc2019-09-23 15:38:12 -0700669 if (!mem_cgroup_online(memcg))
Kirill Tkhaib0dedc42018-08-17 15:48:14 -0700670 return 0;
671
672 if (!down_read_trylock(&shrinker_rwsem))
673 return 0;
674
Yang Shi468ab842021-05-04 18:36:26 -0700675 info = shrinker_info_protected(memcg, nid);
Yang Shie4262c42021-05-04 18:36:23 -0700676 if (unlikely(!info))
Kirill Tkhaib0dedc42018-08-17 15:48:14 -0700677 goto unlock;
678
Yang Shie4262c42021-05-04 18:36:23 -0700679 for_each_set_bit(i, info->map, shrinker_nr_max) {
Kirill Tkhaib0dedc42018-08-17 15:48:14 -0700680 struct shrink_control sc = {
681 .gfp_mask = gfp_mask,
682 .nid = nid,
683 .memcg = memcg,
684 };
685 struct shrinker *shrinker;
686
687 shrinker = idr_find(&shrinker_idr, i);
Yang Shi41ca6682021-05-04 18:36:29 -0700688 if (unlikely(!shrinker || !(shrinker->flags & SHRINKER_REGISTERED))) {
Kirill Tkhai7e010df2018-08-17 15:48:34 -0700689 if (!shrinker)
Yang Shie4262c42021-05-04 18:36:23 -0700690 clear_bit(i, info->map);
Kirill Tkhaib0dedc42018-08-17 15:48:14 -0700691 continue;
692 }
693
Yang Shi0a432dc2019-09-23 15:38:12 -0700694 /* Call non-slab shrinkers even though kmem is disabled */
695 if (!memcg_kmem_enabled() &&
696 !(shrinker->flags & SHRINKER_NONSLAB))
697 continue;
698
Kirill Tkhaib0dedc42018-08-17 15:48:14 -0700699 ret = do_shrink_slab(&sc, shrinker, priority);
Kirill Tkhaif90280d2018-08-17 15:48:25 -0700700 if (ret == SHRINK_EMPTY) {
Yang Shie4262c42021-05-04 18:36:23 -0700701 clear_bit(i, info->map);
Kirill Tkhaif90280d2018-08-17 15:48:25 -0700702 /*
703 * After the shrinker reported that it had no objects to
704 * free, but before we cleared the corresponding bit in
705 * the memcg shrinker map, a new object might have been
706 * added. To make sure, we have the bit set in this
707 * case, we invoke the shrinker one more time and reset
708 * the bit if it reports that it is not empty anymore.
709 * The memory barrier here pairs with the barrier in
Yang Shi2bfd3632021-05-04 18:36:11 -0700710 * set_shrinker_bit():
Kirill Tkhaif90280d2018-08-17 15:48:25 -0700711 *
712 * list_lru_add() shrink_slab_memcg()
713 * list_add_tail() clear_bit()
714 * <MB> <MB>
715 * set_bit() do_shrink_slab()
716 */
717 smp_mb__after_atomic();
718 ret = do_shrink_slab(&sc, shrinker, priority);
719 if (ret == SHRINK_EMPTY)
720 ret = 0;
721 else
Yang Shi2bfd3632021-05-04 18:36:11 -0700722 set_shrinker_bit(memcg, nid, i);
Kirill Tkhaif90280d2018-08-17 15:48:25 -0700723 }
Kirill Tkhaib0dedc42018-08-17 15:48:14 -0700724 freed += ret;
725
726 if (rwsem_is_contended(&shrinker_rwsem)) {
727 freed = freed ? : 1;
728 break;
729 }
730 }
731unlock:
732 up_read(&shrinker_rwsem);
733 return freed;
734}
Yang Shi0a432dc2019-09-23 15:38:12 -0700735#else /* CONFIG_MEMCG */
Kirill Tkhaib0dedc42018-08-17 15:48:14 -0700736static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,
737 struct mem_cgroup *memcg, int priority)
738{
739 return 0;
740}
Yang Shi0a432dc2019-09-23 15:38:12 -0700741#endif /* CONFIG_MEMCG */
Kirill Tkhaib0dedc42018-08-17 15:48:14 -0700742
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800743/**
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800744 * shrink_slab - shrink slab caches
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800745 * @gfp_mask: allocation context
746 * @nid: node whose slab caches to target
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800747 * @memcg: memory cgroup whose slab caches to target
Josef Bacik9092c712018-01-31 16:16:26 -0800748 * @priority: the reclaim priority
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 *
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800750 * Call the shrink functions to age shrinkable caches.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 *
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800752 * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set,
753 * unaware shrinkers will receive a node id of 0 instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 *
Vladimir Davydovaeed1d322018-08-17 15:48:17 -0700755 * @memcg specifies the memory cgroup to target. Unaware shrinkers
756 * are called only if it is the root cgroup.
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800757 *
Josef Bacik9092c712018-01-31 16:16:26 -0800758 * @priority is sc->priority, we take the number of objects and >> by priority
759 * in order to get the scan target.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 *
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800761 * Returns the number of reclaimed slab objects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 */
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800763static unsigned long shrink_slab(gfp_t gfp_mask, int nid,
764 struct mem_cgroup *memcg,
Josef Bacik9092c712018-01-31 16:16:26 -0800765 int priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Kirill Tkhaib8e57ef2018-10-05 15:52:10 -0700767 unsigned long ret, freed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 struct shrinker *shrinker;
769
Yang Shifa1e5122019-08-02 21:48:44 -0700770 /*
771 * The root memcg might be allocated even though memcg is disabled
772 * via "cgroup_disable=memory" boot parameter. This could make
773 * mem_cgroup_is_root() return false, then just run memcg slab
774 * shrink, but skip global shrink. This may result in premature
775 * oom.
776 */
777 if (!mem_cgroup_disabled() && !mem_cgroup_is_root(memcg))
Kirill Tkhaib0dedc42018-08-17 15:48:14 -0700778 return shrink_slab_memcg(gfp_mask, nid, memcg, priority);
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800779
Tetsuo Handae830c632018-04-05 16:23:35 -0700780 if (!down_read_trylock(&shrinker_rwsem))
Minchan Kimf06590b2011-05-24 17:11:11 -0700781 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 list_for_each_entry(shrinker, &shrinker_list, list) {
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800784 struct shrink_control sc = {
785 .gfp_mask = gfp_mask,
786 .nid = nid,
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800787 .memcg = memcg,
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800788 };
Vladimir Davydovec970972014-01-23 15:53:23 -0800789
Kirill Tkhai9b996462018-08-17 15:48:21 -0700790 ret = do_shrink_slab(&sc, shrinker, priority);
791 if (ret == SHRINK_EMPTY)
792 ret = 0;
793 freed += ret;
Minchan Kime4966122018-01-31 16:16:55 -0800794 /*
795 * Bail out if someone want to register a new shrinker to
Ethon Paul55b65a52020-06-04 16:49:10 -0700796 * prevent the registration from being stalled for long periods
Minchan Kime4966122018-01-31 16:16:55 -0800797 * by parallel ongoing shrinking.
798 */
799 if (rwsem_is_contended(&shrinker_rwsem)) {
800 freed = freed ? : 1;
801 break;
802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 up_read(&shrinker_rwsem);
Minchan Kimf06590b2011-05-24 17:11:11 -0700806out:
807 cond_resched();
Dave Chinner24f7c6b2013-08-28 10:17:56 +1000808 return freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800811void drop_slab_node(int nid)
812{
813 unsigned long freed;
814
815 do {
816 struct mem_cgroup *memcg = NULL;
817
Chunxin Zang069c4112020-10-13 16:56:46 -0700818 if (fatal_signal_pending(current))
819 return;
820
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800821 freed = 0;
Vladimir Davydovaeed1d322018-08-17 15:48:17 -0700822 memcg = mem_cgroup_iter(NULL, NULL, NULL);
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800823 do {
Josef Bacik9092c712018-01-31 16:16:26 -0800824 freed += shrink_slab(GFP_KERNEL, nid, memcg, 0);
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800825 } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL);
826 } while (freed > 10);
827}
828
829void drop_slab(void)
830{
831 int nid;
832
833 for_each_online_node(nid)
834 drop_slab_node(nid);
835}
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837static inline int is_page_cache_freeable(struct page *page)
838{
Johannes Weinerceddc3a2009-09-21 17:03:00 -0700839 /*
840 * A freeable page cache page is referenced only by the caller
Matthew Wilcox67891ff2018-06-10 07:34:39 -0400841 * that isolated the page, the page cache and optional buffer
842 * heads at page->private.
Johannes Weinerceddc3a2009-09-21 17:03:00 -0700843 */
Matthew Wilcox (Oracle)3efe62e2020-10-15 20:05:56 -0700844 int page_cache_pins = thp_nr_pages(page);
Matthew Wilcox67891ff2018-06-10 07:34:39 -0400845 return page_count(page) - page_has_private(page) == 1 + page_cache_pins;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
Yang Shicb165562019-11-30 17:55:28 -0800848static int may_write_to_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Christoph Lameter930d9152006-01-08 01:00:47 -0800850 if (current->flags & PF_SWAPWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return 1;
Tejun Heo703c2702015-05-22 17:13:44 -0400852 if (!inode_write_congested(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return 1;
Tejun Heo703c2702015-05-22 17:13:44 -0400854 if (inode_to_bdi(inode) == current->backing_dev_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return 1;
856 return 0;
857}
858
859/*
860 * We detected a synchronous write error writing a page out. Probably
861 * -ENOSPC. We need to propagate that into the address_space for a subsequent
862 * fsync(), msync() or close().
863 *
864 * The tricky part is that after writepage we cannot touch the mapping: nothing
865 * prevents it from being freed up. But we have a ref on the page and once
866 * that page is locked, the mapping is pinned.
867 *
868 * We're allowed to run sleeping lock_page() here because we know the caller has
869 * __GFP_FS.
870 */
871static void handle_write_error(struct address_space *mapping,
872 struct page *page, int error)
873{
Jens Axboe7eaceac2011-03-10 08:52:07 +0100874 lock_page(page);
Guillaume Chazarain3e9f45b2007-05-08 00:23:25 -0700875 if (page_mapping(page) == mapping)
876 mapping_set_error(mapping, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 unlock_page(page);
878}
879
Christoph Lameter04e62a22006-06-23 02:03:38 -0700880/* possible outcome of pageout() */
881typedef enum {
882 /* failed to write page out, page is locked */
883 PAGE_KEEP,
884 /* move page to the active list, page is locked */
885 PAGE_ACTIVATE,
886 /* page has been sent to the disk successfully, page is unlocked */
887 PAGE_SUCCESS,
888 /* page is clean and locked */
889 PAGE_CLEAN,
890} pageout_t;
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892/*
Andrew Morton1742f192006-03-22 00:08:21 -0800893 * pageout is called by shrink_page_list() for each dirty page.
894 * Calls ->writepage().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 */
Yang Shicb165562019-11-30 17:55:28 -0800896static pageout_t pageout(struct page *page, struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
898 /*
899 * If the page is dirty, only perform writeback if that write
900 * will be non-blocking. To prevent this allocation from being
901 * stalled by pagecache activity. But note that there may be
902 * stalls if we need to run get_block(). We could test
903 * PagePrivate for that.
904 *
Al Viro81742022014-04-03 03:17:43 -0400905 * If this process is currently in __generic_file_write_iter() against
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 * this page's queue, we can perform writeback even if that
907 * will block.
908 *
909 * If the page is swapcache, write it back even if that would
910 * block, for some throttling. This happens by accident, because
911 * swap_backing_dev_info is bust: it doesn't reflect the
912 * congestion state of the swapdevs. Easy to fix, if needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 */
914 if (!is_page_cache_freeable(page))
915 return PAGE_KEEP;
916 if (!mapping) {
917 /*
918 * Some data journaling orphaned pages can have
919 * page->mapping == NULL while being dirty with clean buffers.
920 */
David Howells266cf652009-04-03 16:42:36 +0100921 if (page_has_private(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (try_to_free_buffers(page)) {
923 ClearPageDirty(page);
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -0700924 pr_info("%s: orphaned page\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return PAGE_CLEAN;
926 }
927 }
928 return PAGE_KEEP;
929 }
930 if (mapping->a_ops->writepage == NULL)
931 return PAGE_ACTIVATE;
Yang Shicb165562019-11-30 17:55:28 -0800932 if (!may_write_to_inode(mapping->host))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return PAGE_KEEP;
934
935 if (clear_page_dirty_for_io(page)) {
936 int res;
937 struct writeback_control wbc = {
938 .sync_mode = WB_SYNC_NONE,
939 .nr_to_write = SWAP_CLUSTER_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700940 .range_start = 0,
941 .range_end = LLONG_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 .for_reclaim = 1,
943 };
944
945 SetPageReclaim(page);
946 res = mapping->a_ops->writepage(page, &wbc);
947 if (res < 0)
948 handle_write_error(mapping, page, res);
Zach Brown994fc28c2005-12-15 14:28:17 -0800949 if (res == AOP_WRITEPAGE_ACTIVATE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 ClearPageReclaim(page);
951 return PAGE_ACTIVATE;
952 }
Andy Whitcroftc661b072007-08-22 14:01:26 -0700953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (!PageWriteback(page)) {
955 /* synchronous write or broken a_ops? */
956 ClearPageReclaim(page);
957 }
yalin wang3aa23852016-01-14 15:18:30 -0800958 trace_mm_vmscan_writepage(page);
Mel Gormanc4a25632016-07-28 15:46:23 -0700959 inc_node_page_state(page, NR_VMSCAN_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return PAGE_SUCCESS;
961 }
962
963 return PAGE_CLEAN;
964}
965
Andrew Mortona649fd92006-10-17 00:09:36 -0700966/*
Nick Piggine2867812008-07-25 19:45:30 -0700967 * Same as remove_mapping, but if the page is removed from the mapping, it
968 * gets returned with a refcount of 0.
Andrew Mortona649fd92006-10-17 00:09:36 -0700969 */
Johannes Weinera5289102014-04-03 14:47:51 -0700970static int __remove_mapping(struct address_space *mapping, struct page *page,
Johannes Weinerb9107182019-11-30 17:55:59 -0800971 bool reclaimed, struct mem_cgroup *target_memcg)
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800972{
Greg Thelenc4843a72015-05-22 17:13:16 -0400973 unsigned long flags;
Huang Yingbd4c82c22017-09-06 16:22:49 -0700974 int refcount;
Joonsoo Kimaae466b2020-08-11 18:30:50 -0700975 void *shadow = NULL;
Greg Thelenc4843a72015-05-22 17:13:16 -0400976
Nick Piggin28e4d962006-09-25 23:31:23 -0700977 BUG_ON(!PageLocked(page));
978 BUG_ON(mapping != page_mapping(page));
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800979
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700980 xa_lock_irqsave(&mapping->i_pages, flags);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800981 /*
Nick Piggin0fd0e6b2006-09-27 01:50:02 -0700982 * The non racy check for a busy page.
983 *
984 * Must be careful with the order of the tests. When someone has
985 * a ref to the page, it may be possible that they dirty it then
986 * drop the reference. So if PageDirty is tested before page_count
987 * here, then the following race may occur:
988 *
989 * get_user_pages(&page);
990 * [user mapping goes away]
991 * write_to(page);
992 * !PageDirty(page) [good]
993 * SetPageDirty(page);
994 * put_page(page);
995 * !page_count(page) [good, discard it]
996 *
997 * [oops, our write_to data is lost]
998 *
999 * Reversing the order of the tests ensures such a situation cannot
1000 * escape unnoticed. The smp_rmb is needed to ensure the page->flags
Joonsoo Kim0139aa72016-05-19 17:10:49 -07001001 * load is not satisfied before that of page->_refcount.
Nick Piggin0fd0e6b2006-09-27 01:50:02 -07001002 *
1003 * Note that if SetPageDirty is always performed via set_page_dirty,
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001004 * and thus under the i_pages lock, then this ordering is not required.
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001005 */
William Kucharski906d2782019-10-18 20:20:33 -07001006 refcount = 1 + compound_nr(page);
Huang Yingbd4c82c22017-09-06 16:22:49 -07001007 if (!page_ref_freeze(page, refcount))
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001008 goto cannot_free;
Jiang Biao1c4c3b92018-08-21 21:53:13 -07001009 /* note: atomic_cmpxchg in page_ref_freeze provides the smp_rmb */
Nick Piggine2867812008-07-25 19:45:30 -07001010 if (unlikely(PageDirty(page))) {
Huang Yingbd4c82c22017-09-06 16:22:49 -07001011 page_ref_unfreeze(page, refcount);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001012 goto cannot_free;
Nick Piggine2867812008-07-25 19:45:30 -07001013 }
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001014
1015 if (PageSwapCache(page)) {
1016 swp_entry_t swap = { .val = page_private(page) };
Johannes Weiner0a31bc92014-08-08 14:19:22 -07001017 mem_cgroup_swapout(page, swap);
Joonsoo Kimaae466b2020-08-11 18:30:50 -07001018 if (reclaimed && !mapping_exiting(mapping))
1019 shadow = workingset_eviction(page, target_memcg);
1020 __delete_from_swap_cache(page, swap, shadow);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001021 xa_unlock_irqrestore(&mapping->i_pages, flags);
Minchan Kim75f6d6d2017-07-06 15:37:21 -07001022 put_swap_page(page, swap);
Nick Piggine2867812008-07-25 19:45:30 -07001023 } else {
Linus Torvalds6072d132010-12-01 13:35:19 -05001024 void (*freepage)(struct page *);
1025
1026 freepage = mapping->a_ops->freepage;
Johannes Weinera5289102014-04-03 14:47:51 -07001027 /*
1028 * Remember a shadow entry for reclaimed file cache in
1029 * order to detect refaults, thus thrashing, later on.
1030 *
1031 * But don't store shadows in an address space that is
dylan-meiners238c3042020-08-06 23:26:29 -07001032 * already exiting. This is not just an optimization,
Johannes Weinera5289102014-04-03 14:47:51 -07001033 * inode reclaim needs to empty out the radix tree or
1034 * the nodes are lost. Don't plant shadows behind its
1035 * back.
Ross Zwislerf9fe48b2016-01-22 15:10:40 -08001036 *
1037 * We also don't store shadows for DAX mappings because the
1038 * only page cache pages found in these are zero pages
1039 * covering holes, and because we don't want to mix DAX
1040 * exceptional entries and shadow exceptional entries in the
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001041 * same address_space.
Johannes Weinera5289102014-04-03 14:47:51 -07001042 */
Huang Ying9de4f222020-04-06 20:04:41 -07001043 if (reclaimed && page_is_file_lru(page) &&
Ross Zwislerf9fe48b2016-01-22 15:10:40 -08001044 !mapping_exiting(mapping) && !dax_mapping(mapping))
Johannes Weinerb9107182019-11-30 17:55:59 -08001045 shadow = workingset_eviction(page, target_memcg);
Johannes Weiner62cccb82016-03-15 14:57:22 -07001046 __delete_from_page_cache(page, shadow);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001047 xa_unlock_irqrestore(&mapping->i_pages, flags);
Linus Torvalds6072d132010-12-01 13:35:19 -05001048
1049 if (freepage != NULL)
1050 freepage(page);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001051 }
1052
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001053 return 1;
1054
1055cannot_free:
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001056 xa_unlock_irqrestore(&mapping->i_pages, flags);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001057 return 0;
1058}
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060/*
Nick Piggine2867812008-07-25 19:45:30 -07001061 * Attempt to detach a locked page from its ->mapping. If it is dirty or if
1062 * someone else has a ref on the page, abort and return 0. If it was
1063 * successfully detached, return 1. Assumes the caller has a single ref on
1064 * this page.
1065 */
1066int remove_mapping(struct address_space *mapping, struct page *page)
1067{
Johannes Weinerb9107182019-11-30 17:55:59 -08001068 if (__remove_mapping(mapping, page, false, NULL)) {
Nick Piggine2867812008-07-25 19:45:30 -07001069 /*
1070 * Unfreezing the refcount with 1 rather than 2 effectively
1071 * drops the pagecache ref for us without requiring another
1072 * atomic operation.
1073 */
Joonsoo Kimfe896d12016-03-17 14:19:26 -07001074 page_ref_unfreeze(page, 1);
Nick Piggine2867812008-07-25 19:45:30 -07001075 return 1;
1076 }
1077 return 0;
1078}
1079
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001080/**
1081 * putback_lru_page - put previously isolated page onto appropriate LRU list
1082 * @page: page to be put back to appropriate lru list
1083 *
1084 * Add previously isolated @page to appropriate LRU list.
1085 * Page may still be unevictable for other reasons.
1086 *
1087 * lru_lock must not be held, interrupts must be enabled.
1088 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001089void putback_lru_page(struct page *page)
1090{
Shakeel Butt9c4e6b12018-02-21 14:45:28 -08001091 lru_cache_add(page);
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001092 put_page(page); /* drop ref from isolate */
1093}
1094
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001095enum page_references {
1096 PAGEREF_RECLAIM,
1097 PAGEREF_RECLAIM_CLEAN,
Johannes Weiner645747462010-03-05 13:42:22 -08001098 PAGEREF_KEEP,
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001099 PAGEREF_ACTIVATE,
1100};
1101
1102static enum page_references page_check_references(struct page *page,
1103 struct scan_control *sc)
1104{
Johannes Weiner645747462010-03-05 13:42:22 -08001105 int referenced_ptes, referenced_page;
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001106 unsigned long vm_flags;
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001107
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001108 referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup,
1109 &vm_flags);
Johannes Weiner645747462010-03-05 13:42:22 -08001110 referenced_page = TestClearPageReferenced(page);
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001111
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001112 /*
1113 * Mlock lost the isolation race with us. Let try_to_unmap()
1114 * move the page to the unevictable list.
1115 */
1116 if (vm_flags & VM_LOCKED)
1117 return PAGEREF_RECLAIM;
1118
Johannes Weiner645747462010-03-05 13:42:22 -08001119 if (referenced_ptes) {
Johannes Weiner645747462010-03-05 13:42:22 -08001120 /*
1121 * All mapped pages start out with page table
1122 * references from the instantiating fault, so we need
1123 * to look twice if a mapped file page is used more
1124 * than once.
1125 *
1126 * Mark it and spare it for another trip around the
1127 * inactive list. Another page table reference will
1128 * lead to its activation.
1129 *
1130 * Note: the mark is set for activated pages as well
1131 * so that recently deactivated but used pages are
1132 * quickly recovered.
1133 */
1134 SetPageReferenced(page);
1135
Konstantin Khlebnikov34dbc672012-01-10 15:06:59 -08001136 if (referenced_page || referenced_ptes > 1)
Johannes Weiner645747462010-03-05 13:42:22 -08001137 return PAGEREF_ACTIVATE;
1138
Konstantin Khlebnikovc909e992012-01-10 15:07:03 -08001139 /*
1140 * Activate file-backed executable pages after first usage.
1141 */
Joonsoo Kimb5181542020-08-11 18:30:40 -07001142 if ((vm_flags & VM_EXEC) && !PageSwapBacked(page))
Konstantin Khlebnikovc909e992012-01-10 15:07:03 -08001143 return PAGEREF_ACTIVATE;
1144
Johannes Weiner645747462010-03-05 13:42:22 -08001145 return PAGEREF_KEEP;
1146 }
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001147
1148 /* Reclaim if clean, defer dirty pages to writeback */
KOSAKI Motohiro2e302442010-10-26 14:21:46 -07001149 if (referenced_page && !PageSwapBacked(page))
Johannes Weiner645747462010-03-05 13:42:22 -08001150 return PAGEREF_RECLAIM_CLEAN;
1151
1152 return PAGEREF_RECLAIM;
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001153}
1154
Mel Gormane2be15f2013-07-03 15:01:57 -07001155/* Check if a page is dirty or under writeback */
1156static void page_check_dirty_writeback(struct page *page,
1157 bool *dirty, bool *writeback)
1158{
Mel Gormanb4597222013-07-03 15:02:05 -07001159 struct address_space *mapping;
1160
Mel Gormane2be15f2013-07-03 15:01:57 -07001161 /*
1162 * Anonymous pages are not handled by flushers and must be written
1163 * from reclaim context. Do not stall reclaim based on them
1164 */
Huang Ying9de4f222020-04-06 20:04:41 -07001165 if (!page_is_file_lru(page) ||
Shaohua Li802a3a92017-05-03 14:52:32 -07001166 (PageAnon(page) && !PageSwapBacked(page))) {
Mel Gormane2be15f2013-07-03 15:01:57 -07001167 *dirty = false;
1168 *writeback = false;
1169 return;
1170 }
1171
1172 /* By default assume that the page flags are accurate */
1173 *dirty = PageDirty(page);
1174 *writeback = PageWriteback(page);
Mel Gormanb4597222013-07-03 15:02:05 -07001175
1176 /* Verify dirty/writeback state if the filesystem supports it */
1177 if (!page_has_private(page))
1178 return;
1179
1180 mapping = page_mapping(page);
1181 if (mapping && mapping->a_ops->is_dirty_writeback)
1182 mapping->a_ops->is_dirty_writeback(page, dirty, writeback);
Mel Gormane2be15f2013-07-03 15:01:57 -07001183}
1184
Nick Piggine2867812008-07-25 19:45:30 -07001185/*
Andrew Morton1742f192006-03-22 00:08:21 -08001186 * shrink_page_list() returns the number of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 */
Maninder Singh730ec8c2020-06-03 16:01:18 -07001188static unsigned int shrink_page_list(struct list_head *page_list,
1189 struct pglist_data *pgdat,
1190 struct scan_control *sc,
Maninder Singh730ec8c2020-06-03 16:01:18 -07001191 struct reclaim_stat *stat,
1192 bool ignore_references)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
1194 LIST_HEAD(ret_pages);
Mel Gormanabe4c3b2010-08-09 17:19:31 -07001195 LIST_HEAD(free_pages);
Maninder Singh730ec8c2020-06-03 16:01:18 -07001196 unsigned int nr_reclaimed = 0;
1197 unsigned int pgactivate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Kirill Tkhai060f0052019-03-05 15:48:15 -08001199 memset(stat, 0, sizeof(*stat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 cond_resched();
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 while (!list_empty(page_list)) {
1203 struct address_space *mapping;
1204 struct page *page;
Minchan Kim8940b342019-09-25 16:49:11 -07001205 enum page_references references = PAGEREF_RECLAIM;
Kirill Tkhai4b793062020-04-01 21:10:18 -07001206 bool dirty, writeback, may_enter_fs;
Yang Shi98879b32019-07-11 20:59:30 -07001207 unsigned int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 cond_resched();
1210
1211 page = lru_to_page(page_list);
1212 list_del(&page->lru);
1213
Nick Piggin529ae9a2008-08-02 12:01:03 +02001214 if (!trylock_page(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 goto keep;
1216
Sasha Levin309381fea2014-01-23 15:52:54 -08001217 VM_BUG_ON_PAGE(PageActive(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -07001219 nr_pages = compound_nr(page);
Yang Shi98879b32019-07-11 20:59:30 -07001220
1221 /* Account the number of base pages even though THP */
1222 sc->nr_scanned += nr_pages;
Christoph Lameter80e43422006-02-11 17:55:53 -08001223
Hugh Dickins39b5f292012-10-08 16:33:18 -07001224 if (unlikely(!page_evictable(page)))
Minchan Kimad6b6702017-05-03 14:54:13 -07001225 goto activate_locked;
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001226
Johannes Weinera6dc60f82009-03-31 15:19:30 -07001227 if (!sc->may_unmap && page_mapped(page))
Christoph Lameter80e43422006-02-11 17:55:53 -08001228 goto keep_locked;
1229
Andy Whitcroftc661b072007-08-22 14:01:26 -07001230 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
1231 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
1232
Mel Gorman283aba92013-07-03 15:01:51 -07001233 /*
Andrey Ryabinin894befe2018-04-10 16:27:51 -07001234 * The number of dirty pages determines if a node is marked
Mel Gormane2be15f2013-07-03 15:01:57 -07001235 * reclaim_congested which affects wait_iff_congested. kswapd
1236 * will stall and start writing pages if the tail of the LRU
1237 * is all dirty unqueued pages.
1238 */
1239 page_check_dirty_writeback(page, &dirty, &writeback);
1240 if (dirty || writeback)
Kirill Tkhai060f0052019-03-05 15:48:15 -08001241 stat->nr_dirty++;
Mel Gormane2be15f2013-07-03 15:01:57 -07001242
1243 if (dirty && !writeback)
Kirill Tkhai060f0052019-03-05 15:48:15 -08001244 stat->nr_unqueued_dirty++;
Mel Gormane2be15f2013-07-03 15:01:57 -07001245
Mel Gormand04e8ac2013-07-03 15:02:03 -07001246 /*
1247 * Treat this page as congested if the underlying BDI is or if
1248 * pages are cycling through the LRU so quickly that the
1249 * pages marked for immediate reclaim are making it to the
1250 * end of the LRU a second time.
1251 */
Mel Gormane2be15f2013-07-03 15:01:57 -07001252 mapping = page_mapping(page);
Jamie Liu1da58ee2014-12-10 15:43:20 -08001253 if (((dirty || writeback) && mapping &&
Tejun Heo703c2702015-05-22 17:13:44 -04001254 inode_write_congested(mapping->host)) ||
Mel Gormand04e8ac2013-07-03 15:02:03 -07001255 (writeback && PageReclaim(page)))
Kirill Tkhai060f0052019-03-05 15:48:15 -08001256 stat->nr_congested++;
Mel Gormane2be15f2013-07-03 15:01:57 -07001257
1258 /*
Mel Gorman283aba92013-07-03 15:01:51 -07001259 * If a page at the tail of the LRU is under writeback, there
1260 * are three cases to consider.
1261 *
1262 * 1) If reclaim is encountering an excessive number of pages
1263 * under writeback and this page is both under writeback and
1264 * PageReclaim then it indicates that pages are being queued
1265 * for IO but are being recycled through the LRU before the
1266 * IO can complete. Waiting on the page itself risks an
1267 * indefinite stall if it is impossible to writeback the
1268 * page due to IO error or disconnected storage so instead
Mel Gormanb1a6f212013-07-03 15:01:58 -07001269 * note that the LRU is being scanned too quickly and the
1270 * caller can stall after page list has been processed.
Mel Gorman283aba92013-07-03 15:01:51 -07001271 *
Tejun Heo97c93412015-05-22 18:23:36 -04001272 * 2) Global or new memcg reclaim encounters a page that is
Michal Hockoecf5fc62015-08-04 14:36:58 -07001273 * not marked for immediate reclaim, or the caller does not
1274 * have __GFP_FS (or __GFP_IO if it's simply going to swap,
1275 * not to fs). In this case mark the page for immediate
Tejun Heo97c93412015-05-22 18:23:36 -04001276 * reclaim and continue scanning.
Mel Gorman283aba92013-07-03 15:01:51 -07001277 *
Michal Hockoecf5fc62015-08-04 14:36:58 -07001278 * Require may_enter_fs because we would wait on fs, which
1279 * may not have submitted IO yet. And the loop driver might
Mel Gorman283aba92013-07-03 15:01:51 -07001280 * enter reclaim, and deadlock if it waits on a page for
1281 * which it is needed to do the write (loop masks off
1282 * __GFP_IO|__GFP_FS for this reason); but more thought
1283 * would probably show more reasons.
1284 *
Hugh Dickins7fadc822015-09-08 15:03:46 -07001285 * 3) Legacy memcg encounters a page that is already marked
Mel Gorman283aba92013-07-03 15:01:51 -07001286 * PageReclaim. memcg does not have any dirty pages
1287 * throttling so we could easily OOM just because too many
1288 * pages are in writeback and there is nothing else to
1289 * reclaim. Wait for the writeback to complete.
Johannes Weinerc55e8d02017-02-24 14:56:23 -08001290 *
1291 * In cases 1) and 2) we activate the pages to get them out of
1292 * the way while we continue scanning for clean pages on the
1293 * inactive list and refilling from the active list. The
1294 * observation here is that waiting for disk writes is more
1295 * expensive than potentially causing reloads down the line.
1296 * Since they're marked for immediate reclaim, they won't put
1297 * memory pressure on the cache working set any longer than it
1298 * takes to write them to disk.
Mel Gorman283aba92013-07-03 15:01:51 -07001299 */
Andy Whitcroftc661b072007-08-22 14:01:26 -07001300 if (PageWriteback(page)) {
Mel Gorman283aba92013-07-03 15:01:51 -07001301 /* Case 1 above */
1302 if (current_is_kswapd() &&
1303 PageReclaim(page) &&
Mel Gorman599d0c92016-07-28 15:45:31 -07001304 test_bit(PGDAT_WRITEBACK, &pgdat->flags)) {
Kirill Tkhai060f0052019-03-05 15:48:15 -08001305 stat->nr_immediate++;
Johannes Weinerc55e8d02017-02-24 14:56:23 -08001306 goto activate_locked;
Mel Gorman283aba92013-07-03 15:01:51 -07001307
1308 /* Case 2 above */
Johannes Weinerb5ead352019-11-30 17:55:40 -08001309 } else if (writeback_throttling_sane(sc) ||
Michal Hockoecf5fc62015-08-04 14:36:58 -07001310 !PageReclaim(page) || !may_enter_fs) {
Hugh Dickinsc3b94f42012-07-31 16:45:59 -07001311 /*
1312 * This is slightly racy - end_page_writeback()
1313 * might have just cleared PageReclaim, then
1314 * setting PageReclaim here end up interpreted
1315 * as PageReadahead - but that does not matter
1316 * enough to care. What we do want is for this
1317 * page to have PageReclaim set next time memcg
1318 * reclaim reaches the tests above, so it will
1319 * then wait_on_page_writeback() to avoid OOM;
1320 * and it's also appropriate in global reclaim.
1321 */
1322 SetPageReclaim(page);
Kirill Tkhai060f0052019-03-05 15:48:15 -08001323 stat->nr_writeback++;
Johannes Weinerc55e8d02017-02-24 14:56:23 -08001324 goto activate_locked;
Mel Gorman283aba92013-07-03 15:01:51 -07001325
1326 /* Case 3 above */
1327 } else {
Hugh Dickins7fadc822015-09-08 15:03:46 -07001328 unlock_page(page);
Mel Gorman283aba92013-07-03 15:01:51 -07001329 wait_on_page_writeback(page);
Hugh Dickins7fadc822015-09-08 15:03:46 -07001330 /* then go back and try same page again */
1331 list_add_tail(&page->lru, page_list);
1332 continue;
Michal Hockoe62e3842012-07-31 16:45:55 -07001333 }
Andy Whitcroftc661b072007-08-22 14:01:26 -07001334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Minchan Kim8940b342019-09-25 16:49:11 -07001336 if (!ignore_references)
Minchan Kim02c6de82012-10-08 16:31:55 -07001337 references = page_check_references(page, sc);
1338
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001339 switch (references) {
1340 case PAGEREF_ACTIVATE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 goto activate_locked;
Johannes Weiner645747462010-03-05 13:42:22 -08001342 case PAGEREF_KEEP:
Yang Shi98879b32019-07-11 20:59:30 -07001343 stat->nr_ref_keep += nr_pages;
Johannes Weiner645747462010-03-05 13:42:22 -08001344 goto keep_locked;
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001345 case PAGEREF_RECLAIM:
1346 case PAGEREF_RECLAIM_CLEAN:
1347 ; /* try to reclaim the page below */
1348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 /*
1351 * Anonymous process memory has backing store?
1352 * Try to allocate it some swap space here.
Shaohua Li802a3a92017-05-03 14:52:32 -07001353 * Lazyfree page could be freed directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 */
Huang Yingbd4c82c22017-09-06 16:22:49 -07001355 if (PageAnon(page) && PageSwapBacked(page)) {
1356 if (!PageSwapCache(page)) {
1357 if (!(sc->gfp_mask & __GFP_IO))
1358 goto keep_locked;
Linus Torvaldsfeb889f2021-01-16 15:34:57 -08001359 if (page_maybe_dma_pinned(page))
1360 goto keep_locked;
Huang Yingbd4c82c22017-09-06 16:22:49 -07001361 if (PageTransHuge(page)) {
1362 /* cannot split THP, skip it */
1363 if (!can_split_huge_page(page, NULL))
1364 goto activate_locked;
1365 /*
1366 * Split pages without a PMD map right
1367 * away. Chances are some or all of the
1368 * tail pages can be freed without IO.
1369 */
1370 if (!compound_mapcount(page) &&
1371 split_huge_page_to_list(page,
1372 page_list))
1373 goto activate_locked;
1374 }
1375 if (!add_to_swap(page)) {
1376 if (!PageTransHuge(page))
Yang Shi98879b32019-07-11 20:59:30 -07001377 goto activate_locked_split;
Huang Yingbd4c82c22017-09-06 16:22:49 -07001378 /* Fallback to swap normal pages */
1379 if (split_huge_page_to_list(page,
1380 page_list))
1381 goto activate_locked;
Huang Yingfe490cc2017-09-06 16:22:52 -07001382#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1383 count_vm_event(THP_SWPOUT_FALLBACK);
1384#endif
Huang Yingbd4c82c22017-09-06 16:22:49 -07001385 if (!add_to_swap(page))
Yang Shi98879b32019-07-11 20:59:30 -07001386 goto activate_locked_split;
Huang Yingbd4c82c22017-09-06 16:22:49 -07001387 }
Minchan Kim0f074652017-07-06 15:37:24 -07001388
Kirill Tkhai4b793062020-04-01 21:10:18 -07001389 may_enter_fs = true;
Huang Yingbd4c82c22017-09-06 16:22:49 -07001390
1391 /* Adding to swap updated mapping */
1392 mapping = page_mapping(page);
Minchan Kim0f074652017-07-06 15:37:24 -07001393 }
Kirill A. Shutemov7751b2d2016-07-26 15:25:56 -07001394 } else if (unlikely(PageTransHuge(page))) {
1395 /* Split file THP */
1396 if (split_huge_page_to_list(page, page_list))
1397 goto keep_locked;
Mel Gormane2be15f2013-07-03 15:01:57 -07001398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 /*
Yang Shi98879b32019-07-11 20:59:30 -07001401 * THP may get split above, need minus tail pages and update
1402 * nr_pages to avoid accounting tail pages twice.
1403 *
1404 * The tail pages that are added into swap cache successfully
1405 * reach here.
1406 */
1407 if ((nr_pages > 1) && !PageTransHuge(page)) {
1408 sc->nr_scanned -= (nr_pages - 1);
1409 nr_pages = 1;
1410 }
1411
1412 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 * The page is mapped into the page tables of one or more
1414 * processes. Try to unmap it here.
1415 */
Shaohua Li802a3a92017-05-03 14:52:32 -07001416 if (page_mapped(page)) {
Shakeel Butt013339d2020-12-14 19:06:39 -08001417 enum ttu_flags flags = TTU_BATCH_FLUSH;
Jaewon Kim1f318a92020-06-03 16:01:15 -07001418 bool was_swapbacked = PageSwapBacked(page);
Huang Yingbd4c82c22017-09-06 16:22:49 -07001419
1420 if (unlikely(PageTransHuge(page)))
1421 flags |= TTU_SPLIT_HUGE_PMD;
Jaewon Kim1f318a92020-06-03 16:01:15 -07001422
Huang Yingbd4c82c22017-09-06 16:22:49 -07001423 if (!try_to_unmap(page, flags)) {
Yang Shi98879b32019-07-11 20:59:30 -07001424 stat->nr_unmap_fail += nr_pages;
Jaewon Kim1f318a92020-06-03 16:01:15 -07001425 if (!was_swapbacked && PageSwapBacked(page))
1426 stat->nr_lazyfree_fail += nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 goto activate_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
1429 }
1430
1431 if (PageDirty(page)) {
Mel Gormanee728862011-10-31 17:07:38 -07001432 /*
Johannes Weiner4eda4822017-02-24 14:56:20 -08001433 * Only kswapd can writeback filesystem pages
1434 * to avoid risk of stack overflow. But avoid
1435 * injecting inefficient single-page IO into
1436 * flusher writeback as much as possible: only
1437 * write pages when we've encountered many
1438 * dirty pages, and when we've already scanned
1439 * the rest of the LRU for clean pages and see
1440 * the same dirty pages again (PageReclaim).
Mel Gormanee728862011-10-31 17:07:38 -07001441 */
Huang Ying9de4f222020-04-06 20:04:41 -07001442 if (page_is_file_lru(page) &&
Johannes Weiner4eda4822017-02-24 14:56:20 -08001443 (!current_is_kswapd() || !PageReclaim(page) ||
1444 !test_bit(PGDAT_DIRTY, &pgdat->flags))) {
Mel Gorman49ea7eb2011-10-31 17:07:59 -07001445 /*
1446 * Immediately reclaim when written back.
1447 * Similar in principal to deactivate_page()
1448 * except we already have the page isolated
1449 * and know it's dirty
1450 */
Mel Gormanc4a25632016-07-28 15:46:23 -07001451 inc_node_page_state(page, NR_VMSCAN_IMMEDIATE);
Mel Gorman49ea7eb2011-10-31 17:07:59 -07001452 SetPageReclaim(page);
1453
Johannes Weinerc55e8d02017-02-24 14:56:23 -08001454 goto activate_locked;
Mel Gormanee728862011-10-31 17:07:38 -07001455 }
1456
Johannes Weinerdfc8d632010-03-05 13:42:19 -08001457 if (references == PAGEREF_RECLAIM_CLEAN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 goto keep_locked;
Andrew Morton4dd4b922008-03-24 12:29:52 -07001459 if (!may_enter_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 goto keep_locked;
Christoph Lameter52a83632006-02-01 03:05:28 -08001461 if (!sc->may_writepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 goto keep_locked;
1463
Mel Gormand950c942015-09-04 15:47:35 -07001464 /*
1465 * Page is dirty. Flush the TLB if a writable entry
1466 * potentially exists to avoid CPU writes after IO
1467 * starts and then write it out here.
1468 */
1469 try_to_unmap_flush_dirty();
Yang Shicb165562019-11-30 17:55:28 -08001470 switch (pageout(page, mapping)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 case PAGE_KEEP:
1472 goto keep_locked;
1473 case PAGE_ACTIVATE:
1474 goto activate_locked;
1475 case PAGE_SUCCESS:
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -07001476 stat->nr_pageout += thp_nr_pages(page);
Johannes Weiner96f8bf42020-06-03 16:03:09 -07001477
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -07001478 if (PageWriteback(page))
Mel Gorman41ac1992012-05-29 15:06:19 -07001479 goto keep;
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -07001480 if (PageDirty(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 goto keep;
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -07001482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 /*
1484 * A synchronous write - probably a ramdisk. Go
1485 * ahead and try to reclaim the page.
1486 */
Nick Piggin529ae9a2008-08-02 12:01:03 +02001487 if (!trylock_page(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 goto keep;
1489 if (PageDirty(page) || PageWriteback(page))
1490 goto keep_locked;
1491 mapping = page_mapping(page);
Gustavo A. R. Silva01359eb2020-12-14 19:15:00 -08001492 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 case PAGE_CLEAN:
1494 ; /* try to free the page below */
1495 }
1496 }
1497
1498 /*
1499 * If the page has buffers, try to free the buffer mappings
1500 * associated with this page. If we succeed we try to free
1501 * the page as well.
1502 *
1503 * We do this even if the page is PageDirty().
1504 * try_to_release_page() does not perform I/O, but it is
1505 * possible for a page to have PageDirty set, but it is actually
1506 * clean (all its buffers are clean). This happens if the
1507 * buffers were written out directly, with submit_bh(). ext3
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001508 * will do this, as well as the blockdev mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 * try_to_release_page() will discover that cleanness and will
1510 * drop the buffers and mark the page clean - it can be freed.
1511 *
1512 * Rarely, pages can have buffers and no ->mapping. These are
1513 * the pages which were not successfully invalidated in
Yang Shid12b8952020-12-14 19:13:02 -08001514 * truncate_cleanup_page(). We try to drop those buffers here
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 * and if that worked, and the page is no longer mapped into
1516 * process address space (page_count == 1) it can be freed.
1517 * Otherwise, leave the page on the LRU so it is swappable.
1518 */
David Howells266cf652009-04-03 16:42:36 +01001519 if (page_has_private(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (!try_to_release_page(page, sc->gfp_mask))
1521 goto activate_locked;
Nick Piggine2867812008-07-25 19:45:30 -07001522 if (!mapping && page_count(page) == 1) {
1523 unlock_page(page);
1524 if (put_page_testzero(page))
1525 goto free_it;
1526 else {
1527 /*
1528 * rare race with speculative reference.
1529 * the speculative reference will free
1530 * this page shortly, so we may
1531 * increment nr_reclaimed here (and
1532 * leave it off the LRU).
1533 */
1534 nr_reclaimed++;
1535 continue;
1536 }
1537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
1539
Shaohua Li802a3a92017-05-03 14:52:32 -07001540 if (PageAnon(page) && !PageSwapBacked(page)) {
1541 /* follow __remove_mapping for reference */
1542 if (!page_ref_freeze(page, 1))
1543 goto keep_locked;
1544 if (PageDirty(page)) {
1545 page_ref_unfreeze(page, 1);
1546 goto keep_locked;
1547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Shaohua Li802a3a92017-05-03 14:52:32 -07001549 count_vm_event(PGLAZYFREED);
Roman Gushchin22621852017-07-06 15:40:25 -07001550 count_memcg_page_event(page, PGLAZYFREED);
Johannes Weinerb9107182019-11-30 17:55:59 -08001551 } else if (!mapping || !__remove_mapping(mapping, page, true,
1552 sc->target_mem_cgroup))
Shaohua Li802a3a92017-05-03 14:52:32 -07001553 goto keep_locked;
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001554
1555 unlock_page(page);
Nick Piggine2867812008-07-25 19:45:30 -07001556free_it:
Yang Shi98879b32019-07-11 20:59:30 -07001557 /*
1558 * THP may get swapped out in a whole, need account
1559 * all base pages.
1560 */
1561 nr_reclaimed += nr_pages;
Mel Gormanabe4c3b2010-08-09 17:19:31 -07001562
1563 /*
1564 * Is there need to periodically free_page_list? It would
1565 * appear not as the counts should be low
1566 */
Yang Shi7ae88532019-09-23 15:38:09 -07001567 if (unlikely(PageTransHuge(page)))
Matthew Wilcox (Oracle)ff45fc32020-06-03 16:01:09 -07001568 destroy_compound_page(page);
Yang Shi7ae88532019-09-23 15:38:09 -07001569 else
Huang Yingbd4c82c22017-09-06 16:22:49 -07001570 list_add(&page->lru, &free_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 continue;
1572
Yang Shi98879b32019-07-11 20:59:30 -07001573activate_locked_split:
1574 /*
1575 * The tail pages that are failed to add into swap cache
1576 * reach here. Fixup nr_scanned and nr_pages.
1577 */
1578 if (nr_pages > 1) {
1579 sc->nr_scanned -= (nr_pages - 1);
1580 nr_pages = 1;
1581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582activate_locked:
Rik van Riel68a223942008-10-18 20:26:23 -07001583 /* Not a candidate for swapping, so reclaim swap space. */
Minchan Kimad6b6702017-05-03 14:54:13 -07001584 if (PageSwapCache(page) && (mem_cgroup_swap_full(page) ||
1585 PageMlocked(page)))
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -08001586 try_to_free_swap(page);
Sasha Levin309381fea2014-01-23 15:52:54 -08001587 VM_BUG_ON_PAGE(PageActive(page), page);
Minchan Kimad6b6702017-05-03 14:54:13 -07001588 if (!PageMlocked(page)) {
Huang Ying9de4f222020-04-06 20:04:41 -07001589 int type = page_is_file_lru(page);
Minchan Kimad6b6702017-05-03 14:54:13 -07001590 SetPageActive(page);
Yang Shi98879b32019-07-11 20:59:30 -07001591 stat->nr_activate[type] += nr_pages;
Roman Gushchin22621852017-07-06 15:40:25 -07001592 count_memcg_page_event(page, PGACTIVATE);
Minchan Kimad6b6702017-05-03 14:54:13 -07001593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594keep_locked:
1595 unlock_page(page);
1596keep:
1597 list_add(&page->lru, &ret_pages);
Sasha Levin309381fea2014-01-23 15:52:54 -08001598 VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 }
Mel Gormanabe4c3b2010-08-09 17:19:31 -07001600
Yang Shi98879b32019-07-11 20:59:30 -07001601 pgactivate = stat->nr_activate[0] + stat->nr_activate[1];
1602
Johannes Weiner747db952014-08-08 14:19:24 -07001603 mem_cgroup_uncharge_list(&free_pages);
Mel Gorman72b252a2015-09-04 15:47:32 -07001604 try_to_unmap_flush();
Mel Gorman2d4894b2017-11-15 17:37:59 -08001605 free_unref_page_list(&free_pages);
Mel Gormanabe4c3b2010-08-09 17:19:31 -07001606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 list_splice(&ret_pages, page_list);
Kirill Tkhai886cf192019-05-13 17:16:51 -07001608 count_vm_events(PGACTIVATE, pgactivate);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07001609
Andrew Morton05ff5132006-03-22 00:08:20 -08001610 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
1612
Maninder Singh730ec8c2020-06-03 16:01:18 -07001613unsigned int reclaim_clean_pages_from_list(struct zone *zone,
Minchan Kim02c6de82012-10-08 16:31:55 -07001614 struct list_head *page_list)
1615{
1616 struct scan_control sc = {
1617 .gfp_mask = GFP_KERNEL,
1618 .priority = DEF_PRIORITY,
1619 .may_unmap = 1,
1620 };
Jaewon Kim1f318a92020-06-03 16:01:15 -07001621 struct reclaim_stat stat;
Maninder Singh730ec8c2020-06-03 16:01:18 -07001622 unsigned int nr_reclaimed;
Minchan Kim02c6de82012-10-08 16:31:55 -07001623 struct page *page, *next;
1624 LIST_HEAD(clean_pages);
1625
1626 list_for_each_entry_safe(page, next, page_list, lru) {
Oscar Salvadorae37c7f2021-05-04 18:35:29 -07001627 if (!PageHuge(page) && page_is_file_lru(page) &&
1628 !PageDirty(page) && !__PageMovable(page) &&
1629 !PageUnevictable(page)) {
Minchan Kim02c6de82012-10-08 16:31:55 -07001630 ClearPageActive(page);
1631 list_move(&page->lru, &clean_pages);
1632 }
1633 }
1634
Jaewon Kim1f318a92020-06-03 16:01:15 -07001635 nr_reclaimed = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc,
Shakeel Butt013339d2020-12-14 19:06:39 -08001636 &stat, true);
Minchan Kim02c6de82012-10-08 16:31:55 -07001637 list_splice(&clean_pages, page_list);
Nicholas Piggin2da9f632020-11-13 22:51:46 -08001638 mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE,
1639 -(long)nr_reclaimed);
Jaewon Kim1f318a92020-06-03 16:01:15 -07001640 /*
1641 * Since lazyfree pages are isolated from file LRU from the beginning,
1642 * they will rotate back to anonymous LRU in the end if it failed to
1643 * discard so isolated count will be mismatched.
1644 * Compensate the isolated count for both LRU lists.
1645 */
1646 mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON,
1647 stat.nr_lazyfree_fail);
1648 mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE,
Nicholas Piggin2da9f632020-11-13 22:51:46 -08001649 -(long)stat.nr_lazyfree_fail);
Jaewon Kim1f318a92020-06-03 16:01:15 -07001650 return nr_reclaimed;
Minchan Kim02c6de82012-10-08 16:31:55 -07001651}
1652
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001653/*
1654 * Attempt to remove the specified page from its LRU. Only take this page
1655 * if it is of the appropriate PageActive status. Pages which are being
1656 * freed elsewhere are also ignored.
1657 *
1658 * page: page to consider
1659 * mode: one of the LRU isolation modes defined above
1660 *
Alex Shic2135f72021-02-24 12:08:01 -08001661 * returns true on success, false on failure.
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001662 */
Alex Shic2135f72021-02-24 12:08:01 -08001663bool __isolate_lru_page_prepare(struct page *page, isolate_mode_t mode)
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001664{
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001665 /* Only take pages on the LRU. */
1666 if (!PageLRU(page))
Alex Shic2135f72021-02-24 12:08:01 -08001667 return false;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001668
Minchan Kime46a2872012-10-08 16:33:48 -07001669 /* Compaction should not handle unevictable pages but CMA can do so */
1670 if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE))
Alex Shic2135f72021-02-24 12:08:01 -08001671 return false;
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001672
Mel Gormanc8244932012-01-12 17:19:38 -08001673 /*
1674 * To minimise LRU disruption, the caller can indicate that it only
1675 * wants to isolate pages it will be able to operate on without
1676 * blocking - clean pages for the most part.
1677 *
Mel Gormanc8244932012-01-12 17:19:38 -08001678 * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages
1679 * that it is possible to migrate without blocking
1680 */
Johannes Weiner1276ad62017-02-24 14:56:11 -08001681 if (mode & ISOLATE_ASYNC_MIGRATE) {
Mel Gormanc8244932012-01-12 17:19:38 -08001682 /* All the caller can do on PageWriteback is block */
1683 if (PageWriteback(page))
Alex Shic2135f72021-02-24 12:08:01 -08001684 return false;
Mel Gormanc8244932012-01-12 17:19:38 -08001685
1686 if (PageDirty(page)) {
1687 struct address_space *mapping;
Mel Gorman69d763f2018-01-31 16:19:52 -08001688 bool migrate_dirty;
Mel Gormanc8244932012-01-12 17:19:38 -08001689
Mel Gormanc8244932012-01-12 17:19:38 -08001690 /*
1691 * Only pages without mappings or that have a
1692 * ->migratepage callback are possible to migrate
Mel Gorman69d763f2018-01-31 16:19:52 -08001693 * without blocking. However, we can be racing with
1694 * truncation so it's necessary to lock the page
1695 * to stabilise the mapping as truncation holds
1696 * the page lock until after the page is removed
1697 * from the page cache.
Mel Gormanc8244932012-01-12 17:19:38 -08001698 */
Mel Gorman69d763f2018-01-31 16:19:52 -08001699 if (!trylock_page(page))
Alex Shic2135f72021-02-24 12:08:01 -08001700 return false;
Mel Gorman69d763f2018-01-31 16:19:52 -08001701
Mel Gormanc8244932012-01-12 17:19:38 -08001702 mapping = page_mapping(page);
Hugh Dickins145e1a72018-06-01 16:50:50 -07001703 migrate_dirty = !mapping || mapping->a_ops->migratepage;
Mel Gorman69d763f2018-01-31 16:19:52 -08001704 unlock_page(page);
1705 if (!migrate_dirty)
Alex Shic2135f72021-02-24 12:08:01 -08001706 return false;
Mel Gormanc8244932012-01-12 17:19:38 -08001707 }
1708 }
Minchan Kim39deaf82011-10-31 17:06:51 -07001709
Minchan Kimf80c0672011-10-31 17:06:55 -07001710 if ((mode & ISOLATE_UNMAPPED) && page_mapped(page))
Alex Shic2135f72021-02-24 12:08:01 -08001711 return false;
Minchan Kimf80c0672011-10-31 17:06:55 -07001712
Alex Shic2135f72021-02-24 12:08:01 -08001713 return true;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001714}
1715
Mel Gorman7ee36a12016-07-28 15:47:17 -07001716/*
1717 * Update LRU sizes after isolating pages. The LRU size updates must
Ethon Paul55b65a52020-06-04 16:49:10 -07001718 * be complete before mem_cgroup_update_lru_size due to a sanity check.
Mel Gorman7ee36a12016-07-28 15:47:17 -07001719 */
1720static __always_inline void update_lru_sizes(struct lruvec *lruvec,
Michal Hockob4536f0c82017-01-10 16:58:04 -08001721 enum lru_list lru, unsigned long *nr_zone_taken)
Mel Gorman7ee36a12016-07-28 15:47:17 -07001722{
Mel Gorman7ee36a12016-07-28 15:47:17 -07001723 int zid;
1724
Mel Gorman7ee36a12016-07-28 15:47:17 -07001725 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
1726 if (!nr_zone_taken[zid])
1727 continue;
1728
Wei Yanga892cb62020-06-03 16:01:12 -07001729 update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]);
Mel Gorman7ee36a12016-07-28 15:47:17 -07001730 }
Mel Gorman7ee36a12016-07-28 15:47:17 -07001731
Mel Gorman7ee36a12016-07-28 15:47:17 -07001732}
1733
Andrey Ryabininf4b7e272019-03-05 15:49:39 -08001734/**
Hugh Dickins15b44732020-12-15 14:21:31 -08001735 * Isolating page from the lruvec to fill in @dst list by nr_to_scan times.
1736 *
1737 * lruvec->lru_lock is heavily contended. Some of the functions that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 * shrink the lists perform better by taking out a batch of pages
1739 * and working on them outside the LRU lock.
1740 *
1741 * For pagecache intensive workloads, this function is the hottest
1742 * spot in the kernel (apart from copy_*_user functions).
1743 *
Hugh Dickins15b44732020-12-15 14:21:31 -08001744 * Lru_lock must be held before calling this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 *
Minchan Kim791b48b2017-05-12 15:47:06 -07001746 * @nr_to_scan: The number of eligible pages to look through on the list.
Konstantin Khlebnikov5dc35972012-05-29 15:06:58 -07001747 * @lruvec: The LRU vector to pull pages from.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 * @dst: The temp list to put pages on to.
Hugh Dickinsf6260122012-01-12 17:20:06 -08001749 * @nr_scanned: The number of pages that were scanned.
Rik van Rielfe2c2a12012-03-21 16:33:51 -07001750 * @sc: The scan_control struct for this reclaim session
Konstantin Khlebnikov3cb99452012-05-29 15:06:53 -07001751 * @lru: LRU list id for isolating
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 *
1753 * returns how many pages were moved onto *@dst.
1754 */
Andrew Morton69e05942006-03-22 00:08:19 -08001755static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
Konstantin Khlebnikov5dc35972012-05-29 15:06:58 -07001756 struct lruvec *lruvec, struct list_head *dst,
Rik van Rielfe2c2a12012-03-21 16:33:51 -07001757 unsigned long *nr_scanned, struct scan_control *sc,
Kirill Tkhaia9e7c392019-03-05 15:46:55 -08001758 enum lru_list lru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Hugh Dickins75b00af2012-05-29 15:07:09 -07001760 struct list_head *src = &lruvec->lists[lru];
Andrew Morton69e05942006-03-22 00:08:19 -08001761 unsigned long nr_taken = 0;
Mel Gorman599d0c92016-07-28 15:45:31 -07001762 unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 };
Mel Gorman7cc30fc2016-07-28 15:46:59 -07001763 unsigned long nr_skipped[MAX_NR_ZONES] = { 0, };
Johannes Weiner3db65812017-05-03 14:52:13 -07001764 unsigned long skipped = 0;
Minchan Kim791b48b2017-05-12 15:47:06 -07001765 unsigned long scan, total_scan, nr_pages;
Mel Gormanb2e18752016-07-28 15:45:37 -07001766 LIST_HEAD(pages_skipped);
Kirill Tkhaia9e7c392019-03-05 15:46:55 -08001767 isolate_mode_t mode = (sc->may_unmap ? 0 : ISOLATE_UNMAPPED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Yang Shi98879b32019-07-11 20:59:30 -07001769 total_scan = 0;
Minchan Kim791b48b2017-05-12 15:47:06 -07001770 scan = 0;
Yang Shi98879b32019-07-11 20:59:30 -07001771 while (scan < nr_to_scan && !list_empty(src)) {
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001772 struct page *page;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 page = lru_to_page(src);
1775 prefetchw_prev_lru_page(page, src, flags);
1776
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -07001777 nr_pages = compound_nr(page);
Yang Shi98879b32019-07-11 20:59:30 -07001778 total_scan += nr_pages;
1779
Mel Gormanb2e18752016-07-28 15:45:37 -07001780 if (page_zonenum(page) > sc->reclaim_idx) {
1781 list_move(&page->lru, &pages_skipped);
Yang Shi98879b32019-07-11 20:59:30 -07001782 nr_skipped[page_zonenum(page)] += nr_pages;
Mel Gormanb2e18752016-07-28 15:45:37 -07001783 continue;
1784 }
1785
Minchan Kim791b48b2017-05-12 15:47:06 -07001786 /*
1787 * Do not count skipped pages because that makes the function
1788 * return with no isolated pages if the LRU mostly contains
1789 * ineligible pages. This causes the VM to not reclaim any
1790 * pages, triggering a premature OOM.
Yang Shi98879b32019-07-11 20:59:30 -07001791 *
1792 * Account all tail pages of THP. This would not cause
1793 * premature OOM since __isolate_lru_page() returns -EBUSY
1794 * only when the page is being freed somewhere else.
Minchan Kim791b48b2017-05-12 15:47:06 -07001795 */
Yang Shi98879b32019-07-11 20:59:30 -07001796 scan += nr_pages;
Alex Shic2135f72021-02-24 12:08:01 -08001797 if (!__isolate_lru_page_prepare(page, mode)) {
1798 /* It is being freed elsewhere */
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001799 list_move(&page->lru, src);
Alex Shic2135f72021-02-24 12:08:01 -08001800 continue;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001801 }
Alex Shic2135f72021-02-24 12:08:01 -08001802 /*
1803 * Be careful not to clear PageLRU until after we're
1804 * sure the page is not being freed elsewhere -- the
1805 * page release code relies on it.
1806 */
1807 if (unlikely(!get_page_unless_zero(page))) {
1808 list_move(&page->lru, src);
1809 continue;
1810 }
1811
1812 if (!TestClearPageLRU(page)) {
1813 /* Another thread is already isolating this page */
1814 put_page(page);
1815 list_move(&page->lru, src);
1816 continue;
1817 }
1818
1819 nr_taken += nr_pages;
1820 nr_zone_taken[page_zonenum(page)] += nr_pages;
1821 list_move(&page->lru, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 }
1823
Mel Gormanb2e18752016-07-28 15:45:37 -07001824 /*
1825 * Splice any skipped pages to the start of the LRU list. Note that
1826 * this disrupts the LRU order when reclaiming for lower zones but
1827 * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX
1828 * scanning would soon rescan the same pages to skip and put the
1829 * system at risk of premature OOM.
1830 */
Mel Gorman7cc30fc2016-07-28 15:46:59 -07001831 if (!list_empty(&pages_skipped)) {
1832 int zid;
1833
Johannes Weiner3db65812017-05-03 14:52:13 -07001834 list_splice(&pages_skipped, src);
Mel Gorman7cc30fc2016-07-28 15:46:59 -07001835 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
1836 if (!nr_skipped[zid])
1837 continue;
1838
1839 __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]);
Michal Hocko1265e3a2017-02-22 15:44:21 -08001840 skipped += nr_skipped[zid];
Mel Gorman7cc30fc2016-07-28 15:46:59 -07001841 }
1842 }
Minchan Kim791b48b2017-05-12 15:47:06 -07001843 *nr_scanned = total_scan;
Michal Hocko1265e3a2017-02-22 15:44:21 -08001844 trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan,
Minchan Kim791b48b2017-05-12 15:47:06 -07001845 total_scan, skipped, nr_taken, mode, lru);
Michal Hockob4536f0c82017-01-10 16:58:04 -08001846 update_lru_sizes(lruvec, lru, nr_zone_taken);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 return nr_taken;
1848}
1849
Nick Piggin62695a82008-10-18 20:26:09 -07001850/**
1851 * isolate_lru_page - tries to isolate a page from its LRU list
1852 * @page: page to isolate from its LRU list
1853 *
1854 * Isolates a @page from an LRU list, clears PageLRU and adjusts the
1855 * vmstat statistic corresponding to whatever LRU list the page was on.
1856 *
1857 * Returns 0 if the page was removed from an LRU list.
1858 * Returns -EBUSY if the page was not on an LRU list.
1859 *
1860 * The returned page will have PageLRU() cleared. If it was found on
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001861 * the active list, it will have PageActive set. If it was found on
1862 * the unevictable list, it will have the PageUnevictable bit set. That flag
1863 * may need to be cleared by the caller before letting the page go.
Nick Piggin62695a82008-10-18 20:26:09 -07001864 *
1865 * The vmstat statistic corresponding to the list on which the page was
1866 * found will be decremented.
1867 *
1868 * Restrictions:
Mike Rapoporta5d09be2018-02-06 15:42:19 -08001869 *
Nick Piggin62695a82008-10-18 20:26:09 -07001870 * (1) Must be called with an elevated refcount on the page. This is a
Hui Su01c47762020-10-13 16:56:49 -07001871 * fundamental difference from isolate_lru_pages (which is called
Nick Piggin62695a82008-10-18 20:26:09 -07001872 * without a stable reference).
1873 * (2) the lru_lock must not be held.
1874 * (3) interrupts must be enabled.
1875 */
1876int isolate_lru_page(struct page *page)
1877{
1878 int ret = -EBUSY;
1879
Sasha Levin309381fea2014-01-23 15:52:54 -08001880 VM_BUG_ON_PAGE(!page_count(page), page);
Kirill A. Shutemovcf2a82e2016-02-05 15:36:36 -08001881 WARN_RATELIMIT(PageTail(page), "trying to isolate tail page");
Konstantin Khlebnikov0c917312011-05-24 17:12:21 -07001882
Alex Shid25b5bd2020-12-15 12:34:16 -08001883 if (TestClearPageLRU(page)) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001884 struct lruvec *lruvec;
Nick Piggin62695a82008-10-18 20:26:09 -07001885
Alex Shid25b5bd2020-12-15 12:34:16 -08001886 get_page(page);
Alex Shi6168d0d2020-12-15 12:34:29 -08001887 lruvec = lock_page_lruvec_irq(page);
Yu Zhao46ae6b22021-02-24 12:08:25 -08001888 del_page_from_lru_list(page, lruvec);
Alex Shi6168d0d2020-12-15 12:34:29 -08001889 unlock_page_lruvec_irq(lruvec);
Alex Shid25b5bd2020-12-15 12:34:16 -08001890 ret = 0;
Nick Piggin62695a82008-10-18 20:26:09 -07001891 }
Alex Shid25b5bd2020-12-15 12:34:16 -08001892
Nick Piggin62695a82008-10-18 20:26:09 -07001893 return ret;
1894}
1895
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001896/*
Fengguang Wud37dd5d2012-12-18 14:23:28 -08001897 * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and
Xianting Tian178821b2019-11-30 17:56:05 -08001898 * then get rescheduled. When there are massive number of tasks doing page
Fengguang Wud37dd5d2012-12-18 14:23:28 -08001899 * allocation, such sleeping direct reclaimers may keep piling up on each CPU,
1900 * the LRU list will go small and be scanned faster than necessary, leading to
1901 * unnecessary swapping, thrashing and OOM.
Rik van Riel35cd7812009-09-21 17:01:38 -07001902 */
Mel Gorman599d0c92016-07-28 15:45:31 -07001903static int too_many_isolated(struct pglist_data *pgdat, int file,
Rik van Riel35cd7812009-09-21 17:01:38 -07001904 struct scan_control *sc)
1905{
1906 unsigned long inactive, isolated;
1907
1908 if (current_is_kswapd())
1909 return 0;
1910
Johannes Weinerb5ead352019-11-30 17:55:40 -08001911 if (!writeback_throttling_sane(sc))
Rik van Riel35cd7812009-09-21 17:01:38 -07001912 return 0;
1913
1914 if (file) {
Mel Gorman599d0c92016-07-28 15:45:31 -07001915 inactive = node_page_state(pgdat, NR_INACTIVE_FILE);
1916 isolated = node_page_state(pgdat, NR_ISOLATED_FILE);
Rik van Riel35cd7812009-09-21 17:01:38 -07001917 } else {
Mel Gorman599d0c92016-07-28 15:45:31 -07001918 inactive = node_page_state(pgdat, NR_INACTIVE_ANON);
1919 isolated = node_page_state(pgdat, NR_ISOLATED_ANON);
Rik van Riel35cd7812009-09-21 17:01:38 -07001920 }
1921
Fengguang Wu3cf23842012-12-18 14:23:31 -08001922 /*
1923 * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they
1924 * won't get blocked by normal direct-reclaimers, forming a circular
1925 * deadlock.
1926 */
Mel Gormand0164ad2015-11-06 16:28:21 -08001927 if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS))
Fengguang Wu3cf23842012-12-18 14:23:31 -08001928 inactive >>= 3;
1929
Rik van Riel35cd7812009-09-21 17:01:38 -07001930 return isolated > inactive;
1931}
1932
Kirill Tkhaia222f342019-05-13 17:17:00 -07001933/*
Hugh Dickins15b44732020-12-15 14:21:31 -08001934 * move_pages_to_lru() moves pages from private @list to appropriate LRU list.
1935 * On return, @list is reused as a list of pages to be freed by the caller.
Kirill Tkhaia222f342019-05-13 17:17:00 -07001936 *
1937 * Returns the number of pages moved to the given lruvec.
1938 */
Kirill Tkhaia222f342019-05-13 17:17:00 -07001939static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec,
1940 struct list_head *list)
Mel Gorman66635622010-08-09 17:19:30 -07001941{
Kirill Tkhaia222f342019-05-13 17:17:00 -07001942 int nr_pages, nr_moved = 0;
Hugh Dickins3f797682012-01-12 17:20:07 -08001943 LIST_HEAD(pages_to_free);
Kirill Tkhaia222f342019-05-13 17:17:00 -07001944 struct page *page;
Mel Gorman66635622010-08-09 17:19:30 -07001945
Kirill Tkhaia222f342019-05-13 17:17:00 -07001946 while (!list_empty(list)) {
1947 page = lru_to_page(list);
Sasha Levin309381fea2014-01-23 15:52:54 -08001948 VM_BUG_ON_PAGE(PageLRU(page), page);
Alex Shi3d06afa2020-12-15 12:33:37 -08001949 list_del(&page->lru);
Hugh Dickins39b5f292012-10-08 16:33:18 -07001950 if (unlikely(!page_evictable(page))) {
Alex Shi6168d0d2020-12-15 12:34:29 -08001951 spin_unlock_irq(&lruvec->lru_lock);
Mel Gorman66635622010-08-09 17:19:30 -07001952 putback_lru_page(page);
Alex Shi6168d0d2020-12-15 12:34:29 -08001953 spin_lock_irq(&lruvec->lru_lock);
Mel Gorman66635622010-08-09 17:19:30 -07001954 continue;
1955 }
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001956
Alex Shi3d06afa2020-12-15 12:33:37 -08001957 /*
1958 * The SetPageLRU needs to be kept here for list integrity.
1959 * Otherwise:
1960 * #0 move_pages_to_lru #1 release_pages
1961 * if !put_page_testzero
1962 * if (put_page_testzero())
1963 * !PageLRU //skip lru_lock
1964 * SetPageLRU()
1965 * list_add(&page->lru,)
1966 * list_add(&page->lru,)
1967 */
Linus Torvalds7a608572011-01-17 14:42:19 -08001968 SetPageLRU(page);
Kirill Tkhaia222f342019-05-13 17:17:00 -07001969
Alex Shi3d06afa2020-12-15 12:33:37 -08001970 if (unlikely(put_page_testzero(page))) {
Yu Zhao87560172021-02-24 12:08:28 -08001971 __clear_page_lru_flags(page);
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001972
1973 if (unlikely(PageCompound(page))) {
Alex Shi6168d0d2020-12-15 12:34:29 -08001974 spin_unlock_irq(&lruvec->lru_lock);
Matthew Wilcox (Oracle)ff45fc32020-06-03 16:01:09 -07001975 destroy_compound_page(page);
Alex Shi6168d0d2020-12-15 12:34:29 -08001976 spin_lock_irq(&lruvec->lru_lock);
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001977 } else
1978 list_add(&page->lru, &pages_to_free);
Alex Shi3d06afa2020-12-15 12:33:37 -08001979
1980 continue;
Mel Gorman66635622010-08-09 17:19:30 -07001981 }
Alex Shi3d06afa2020-12-15 12:33:37 -08001982
Alex Shiafca9152020-12-15 12:34:02 -08001983 /*
1984 * All pages were isolated from the same lruvec (and isolation
1985 * inhibits memcg migration).
1986 */
Alexander Duyck2a5e4e32020-12-15 12:34:33 -08001987 VM_BUG_ON_PAGE(!lruvec_holds_page_lru_lock(page, lruvec), page);
Yu Zhao3a9c9782021-02-24 12:08:17 -08001988 add_page_to_lru_list(page, lruvec);
Alex Shi3d06afa2020-12-15 12:33:37 -08001989 nr_pages = thp_nr_pages(page);
Alex Shi3d06afa2020-12-15 12:33:37 -08001990 nr_moved += nr_pages;
1991 if (PageActive(page))
1992 workingset_age_nonresident(lruvec, nr_pages);
Mel Gorman66635622010-08-09 17:19:30 -07001993 }
Mel Gorman66635622010-08-09 17:19:30 -07001994
Hugh Dickins3f797682012-01-12 17:20:07 -08001995 /*
1996 * To save our caller's stack, now use input list for pages to free.
1997 */
Kirill Tkhaia222f342019-05-13 17:17:00 -07001998 list_splice(&pages_to_free, list);
1999
2000 return nr_moved;
Mel Gorman66635622010-08-09 17:19:30 -07002001}
2002
2003/*
NeilBrown399ba0b2014-06-04 16:07:42 -07002004 * If a kernel thread (such as nfsd for loop-back mounts) services
NeilBrowna37b0712020-06-01 21:48:18 -07002005 * a backing device by writing to the page cache it sets PF_LOCAL_THROTTLE.
NeilBrown399ba0b2014-06-04 16:07:42 -07002006 * In that case we should only throttle if the backing device it is
2007 * writing to is congested. In other cases it is safe to throttle.
2008 */
2009static int current_may_throttle(void)
2010{
NeilBrowna37b0712020-06-01 21:48:18 -07002011 return !(current->flags & PF_LOCAL_THROTTLE) ||
NeilBrown399ba0b2014-06-04 16:07:42 -07002012 current->backing_dev_info == NULL ||
2013 bdi_write_congested(current->backing_dev_info);
2014}
2015
2016/*
Mel Gormanb2e18752016-07-28 15:45:37 -07002017 * shrink_inactive_list() is a helper for shrink_node(). It returns the number
Andrew Morton1742f192006-03-22 00:08:21 -08002018 * of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 */
Mel Gorman66635622010-08-09 17:19:30 -07002020static noinline_for_stack unsigned long
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07002021shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec,
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002022 struct scan_control *sc, enum lru_list lru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
2024 LIST_HEAD(page_list);
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07002025 unsigned long nr_scanned;
Maninder Singh730ec8c2020-06-03 16:01:18 -07002026 unsigned int nr_reclaimed = 0;
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07002027 unsigned long nr_taken;
Kirill Tkhai060f0052019-03-05 15:48:15 -08002028 struct reclaim_stat stat;
Johannes Weiner497a6c12020-06-03 16:02:34 -07002029 bool file = is_file_lru(lru);
Kirill Tkhaif46b7912019-05-13 17:22:33 -07002030 enum vm_event_item item;
Mel Gorman599d0c92016-07-28 15:45:31 -07002031 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
Michal Hockodb73ee02017-09-06 16:21:11 -07002032 bool stalled = false;
KOSAKI Motohiro78dc5832009-06-16 15:31:40 -07002033
Mel Gorman599d0c92016-07-28 15:45:31 -07002034 while (unlikely(too_many_isolated(pgdat, file, sc))) {
Michal Hockodb73ee02017-09-06 16:21:11 -07002035 if (stalled)
2036 return 0;
2037
2038 /* wait a bit for the reclaimer. */
2039 msleep(100);
2040 stalled = true;
Rik van Riel35cd7812009-09-21 17:01:38 -07002041
2042 /* We are about to die and free our memory. Return now. */
2043 if (fatal_signal_pending(current))
2044 return SWAP_CLUSTER_MAX;
2045 }
2046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 lru_add_drain();
Minchan Kimf80c0672011-10-31 17:06:55 -07002048
Alex Shi6168d0d2020-12-15 12:34:29 -08002049 spin_lock_irq(&lruvec->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Konstantin Khlebnikov5dc35972012-05-29 15:06:58 -07002051 nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list,
Kirill Tkhaia9e7c392019-03-05 15:46:55 -08002052 &nr_scanned, sc, lru);
Konstantin Khlebnikov95d918f2012-05-29 15:06:59 -07002053
Mel Gorman599d0c92016-07-28 15:45:31 -07002054 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
Kirill Tkhaif46b7912019-05-13 17:22:33 -07002055 item = current_is_kswapd() ? PGSCAN_KSWAPD : PGSCAN_DIRECT;
Johannes Weinerb5ead352019-11-30 17:55:40 -08002056 if (!cgroup_reclaim(sc))
Kirill Tkhaif46b7912019-05-13 17:22:33 -07002057 __count_vm_events(item, nr_scanned);
2058 __count_memcg_events(lruvec_memcg(lruvec), item, nr_scanned);
Johannes Weiner497a6c12020-06-03 16:02:34 -07002059 __count_vm_events(PGSCAN_ANON + file, nr_scanned);
2060
Alex Shi6168d0d2020-12-15 12:34:29 -08002061 spin_unlock_irq(&lruvec->lru_lock);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07002062
Hillf Dantond563c052012-03-21 16:34:02 -07002063 if (nr_taken == 0)
Mel Gorman66635622010-08-09 17:19:30 -07002064 return 0;
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07002065
Shakeel Butt013339d2020-12-14 19:06:39 -08002066 nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, &stat, false);
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07002067
Alex Shi6168d0d2020-12-15 12:34:29 -08002068 spin_lock_irq(&lruvec->lru_lock);
Johannes Weiner497a6c12020-06-03 16:02:34 -07002069 move_pages_to_lru(lruvec, &page_list);
2070
2071 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken);
Kirill Tkhaif46b7912019-05-13 17:22:33 -07002072 item = current_is_kswapd() ? PGSTEAL_KSWAPD : PGSTEAL_DIRECT;
Johannes Weinerb5ead352019-11-30 17:55:40 -08002073 if (!cgroup_reclaim(sc))
Kirill Tkhaif46b7912019-05-13 17:22:33 -07002074 __count_vm_events(item, nr_reclaimed);
2075 __count_memcg_events(lruvec_memcg(lruvec), item, nr_reclaimed);
Johannes Weiner497a6c12020-06-03 16:02:34 -07002076 __count_vm_events(PGSTEAL_ANON + file, nr_reclaimed);
Alex Shi6168d0d2020-12-15 12:34:29 -08002077 spin_unlock_irq(&lruvec->lru_lock);
Hugh Dickins3f797682012-01-12 17:20:07 -08002078
Alex Shi75cc3c92020-12-15 14:20:50 -08002079 lru_note_cost(lruvec, file, stat.nr_pageout);
Johannes Weiner747db952014-08-08 14:19:24 -07002080 mem_cgroup_uncharge_list(&page_list);
Mel Gorman2d4894b2017-11-15 17:37:59 -08002081 free_unref_page_list(&page_list);
Mel Gormane11da5b2010-10-26 14:21:40 -07002082
Mel Gorman92df3a72011-10-31 17:07:56 -07002083 /*
Andrey Ryabinin1c610d52018-03-22 16:17:42 -07002084 * If dirty pages are scanned that are not queued for IO, it
2085 * implies that flushers are not doing their job. This can
2086 * happen when memory pressure pushes dirty pages to the end of
2087 * the LRU before the dirty limits are breached and the dirty
2088 * data has expired. It can also happen when the proportion of
2089 * dirty pages grows not through writes but through memory
2090 * pressure reclaiming all the clean cache. And in some cases,
2091 * the flushers simply cannot keep up with the allocation
2092 * rate. Nudge the flusher threads in case they are asleep.
2093 */
2094 if (stat.nr_unqueued_dirty == nr_taken)
2095 wakeup_flusher_threads(WB_REASON_VMSCAN);
2096
Andrey Ryabinind108c772018-04-10 16:27:59 -07002097 sc->nr.dirty += stat.nr_dirty;
2098 sc->nr.congested += stat.nr_congested;
2099 sc->nr.unqueued_dirty += stat.nr_unqueued_dirty;
2100 sc->nr.writeback += stat.nr_writeback;
2101 sc->nr.immediate += stat.nr_immediate;
2102 sc->nr.taken += nr_taken;
2103 if (file)
2104 sc->nr.file_taken += nr_taken;
Mel Gorman8e950282013-07-03 15:02:02 -07002105
Mel Gorman599d0c92016-07-28 15:45:31 -07002106 trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id,
Steven Rostedtd51d1e62018-04-10 16:28:07 -07002107 nr_scanned, nr_reclaimed, &stat, sc->priority, file);
Andrew Morton05ff5132006-03-22 00:08:20 -08002108 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109}
2110
Hugh Dickins15b44732020-12-15 14:21:31 -08002111/*
2112 * shrink_active_list() moves pages from the active LRU to the inactive LRU.
2113 *
2114 * We move them the other way if the page is referenced by one or more
2115 * processes.
2116 *
2117 * If the pages are mostly unmapped, the processing is fast and it is
2118 * appropriate to hold lru_lock across the whole operation. But if
2119 * the pages are mapped, the processing is slow (page_referenced()), so
2120 * we should drop lru_lock around each page. It's impossible to balance
2121 * this, so instead we remove the pages from the LRU while processing them.
2122 * It is safe to rely on PG_active against the non-LRU pages in here because
2123 * nobody will play with that bit on a non-LRU page.
2124 *
2125 * The downside is that we have to touch page->_refcount against each page.
2126 * But we had to alter page->flags anyway.
2127 */
Hugh Dickinsf6260122012-01-12 17:20:06 -08002128static void shrink_active_list(unsigned long nr_to_scan,
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07002129 struct lruvec *lruvec,
Johannes Weinerf16015f2012-01-12 17:17:52 -08002130 struct scan_control *sc,
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002131 enum lru_list lru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
KOSAKI Motohiro44c241f2009-09-21 17:01:35 -07002133 unsigned long nr_taken;
Hugh Dickinsf6260122012-01-12 17:20:06 -08002134 unsigned long nr_scanned;
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -07002135 unsigned long vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 LIST_HEAD(l_hold); /* The pages which were snipped off */
Wu Fengguang8cab4752009-06-16 15:33:12 -07002137 LIST_HEAD(l_active);
Christoph Lameterb69408e2008-10-18 20:26:14 -07002138 LIST_HEAD(l_inactive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 struct page *page;
Michal Hocko9d998b42017-02-22 15:44:18 -08002140 unsigned nr_deactivate, nr_activate;
2141 unsigned nr_rotated = 0;
Konstantin Khlebnikov3cb99452012-05-29 15:06:53 -07002142 int file = is_file_lru(lru);
Mel Gorman599d0c92016-07-28 15:45:31 -07002143 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 lru_add_drain();
Minchan Kimf80c0672011-10-31 17:06:55 -07002146
Alex Shi6168d0d2020-12-15 12:34:29 -08002147 spin_lock_irq(&lruvec->lru_lock);
Johannes Weiner925b7672012-01-12 17:18:15 -08002148
Konstantin Khlebnikov5dc35972012-05-29 15:06:58 -07002149 nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold,
Kirill Tkhaia9e7c392019-03-05 15:46:55 -08002150 &nr_scanned, sc, lru);
Johannes Weiner89b5fae2012-01-12 17:17:50 -08002151
Mel Gorman599d0c92016-07-28 15:45:31 -07002152 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08002153
Shakeel Butt912c0572020-08-06 23:26:32 -07002154 if (!cgroup_reclaim(sc))
2155 __count_vm_events(PGREFILL, nr_scanned);
Yafang Shao2fa26902019-05-13 17:23:02 -07002156 __count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned);
Hugh Dickins9d5e6a92016-05-19 17:12:38 -07002157
Alex Shi6168d0d2020-12-15 12:34:29 -08002158 spin_unlock_irq(&lruvec->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 while (!list_empty(&l_hold)) {
2161 cond_resched();
2162 page = lru_to_page(&l_hold);
2163 list_del(&page->lru);
Rik van Riel7e9cd482008-10-18 20:26:35 -07002164
Hugh Dickins39b5f292012-10-08 16:33:18 -07002165 if (unlikely(!page_evictable(page))) {
Lee Schermerhorn894bc312008-10-18 20:26:39 -07002166 putback_lru_page(page);
2167 continue;
2168 }
2169
Mel Gormancc715d92012-03-21 16:34:00 -07002170 if (unlikely(buffer_heads_over_limit)) {
2171 if (page_has_private(page) && trylock_page(page)) {
2172 if (page_has_private(page))
2173 try_to_release_page(page, 0);
2174 unlock_page(page);
2175 }
2176 }
2177
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07002178 if (page_referenced(page, 0, sc->target_mem_cgroup,
2179 &vm_flags)) {
Wu Fengguang8cab4752009-06-16 15:33:12 -07002180 /*
2181 * Identify referenced, file-backed active pages and
2182 * give them one more trip around the active list. So
2183 * that executable code get better chances to stay in
2184 * memory under moderate memory pressure. Anon pages
2185 * are not likely to be evicted by use-once streaming
2186 * IO, plus JVM can create lots of anon VM_EXEC pages,
2187 * so we ignore them here.
2188 */
Huang Ying9de4f222020-04-06 20:04:41 -07002189 if ((vm_flags & VM_EXEC) && page_is_file_lru(page)) {
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -07002190 nr_rotated += thp_nr_pages(page);
Wu Fengguang8cab4752009-06-16 15:33:12 -07002191 list_add(&page->lru, &l_active);
2192 continue;
2193 }
2194 }
Rik van Riel7e9cd482008-10-18 20:26:35 -07002195
KOSAKI Motohiro5205e562009-09-21 17:01:44 -07002196 ClearPageActive(page); /* we are de-activating */
Johannes Weiner1899ad12018-10-26 15:06:04 -07002197 SetPageWorkingset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 list_add(&page->lru, &l_inactive);
2199 }
2200
Andrew Mortonb5557492009-01-06 14:40:13 -08002201 /*
Wu Fengguang8cab4752009-06-16 15:33:12 -07002202 * Move pages back to the lru list.
Andrew Mortonb5557492009-01-06 14:40:13 -08002203 */
Alex Shi6168d0d2020-12-15 12:34:29 -08002204 spin_lock_irq(&lruvec->lru_lock);
Rik van Riel556adec2008-10-18 20:26:34 -07002205
Kirill Tkhaia222f342019-05-13 17:17:00 -07002206 nr_activate = move_pages_to_lru(lruvec, &l_active);
2207 nr_deactivate = move_pages_to_lru(lruvec, &l_inactive);
Kirill Tkhaif372d892019-05-13 17:16:57 -07002208 /* Keep all free pages in l_active list */
2209 list_splice(&l_inactive, &l_active);
Kirill Tkhai9851ac12019-05-13 17:16:54 -07002210
2211 __count_vm_events(PGDEACTIVATE, nr_deactivate);
2212 __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, nr_deactivate);
2213
Mel Gorman599d0c92016-07-28 15:45:31 -07002214 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken);
Alex Shi6168d0d2020-12-15 12:34:29 -08002215 spin_unlock_irq(&lruvec->lru_lock);
Hugh Dickins2bcf8872012-01-12 17:19:56 -08002216
Kirill Tkhaif372d892019-05-13 17:16:57 -07002217 mem_cgroup_uncharge_list(&l_active);
2218 free_unref_page_list(&l_active);
Michal Hocko9d998b42017-02-22 15:44:18 -08002219 trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate,
2220 nr_deactivate, nr_rotated, sc->priority, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221}
2222
Minchan Kim1a4e58c2019-09-25 16:49:15 -07002223unsigned long reclaim_pages(struct list_head *page_list)
2224{
Yang Shif661d002020-04-01 21:10:05 -07002225 int nid = NUMA_NO_NODE;
Maninder Singh730ec8c2020-06-03 16:01:18 -07002226 unsigned int nr_reclaimed = 0;
Minchan Kim1a4e58c2019-09-25 16:49:15 -07002227 LIST_HEAD(node_page_list);
2228 struct reclaim_stat dummy_stat;
2229 struct page *page;
2230 struct scan_control sc = {
2231 .gfp_mask = GFP_KERNEL,
2232 .priority = DEF_PRIORITY,
2233 .may_writepage = 1,
2234 .may_unmap = 1,
2235 .may_swap = 1,
2236 };
2237
2238 while (!list_empty(page_list)) {
2239 page = lru_to_page(page_list);
Yang Shif661d002020-04-01 21:10:05 -07002240 if (nid == NUMA_NO_NODE) {
Minchan Kim1a4e58c2019-09-25 16:49:15 -07002241 nid = page_to_nid(page);
2242 INIT_LIST_HEAD(&node_page_list);
2243 }
2244
2245 if (nid == page_to_nid(page)) {
2246 ClearPageActive(page);
2247 list_move(&page->lru, &node_page_list);
2248 continue;
2249 }
2250
2251 nr_reclaimed += shrink_page_list(&node_page_list,
2252 NODE_DATA(nid),
Shakeel Butt013339d2020-12-14 19:06:39 -08002253 &sc, &dummy_stat, false);
Minchan Kim1a4e58c2019-09-25 16:49:15 -07002254 while (!list_empty(&node_page_list)) {
2255 page = lru_to_page(&node_page_list);
2256 list_del(&page->lru);
2257 putback_lru_page(page);
2258 }
2259
Yang Shif661d002020-04-01 21:10:05 -07002260 nid = NUMA_NO_NODE;
Minchan Kim1a4e58c2019-09-25 16:49:15 -07002261 }
2262
2263 if (!list_empty(&node_page_list)) {
2264 nr_reclaimed += shrink_page_list(&node_page_list,
2265 NODE_DATA(nid),
Shakeel Butt013339d2020-12-14 19:06:39 -08002266 &sc, &dummy_stat, false);
Minchan Kim1a4e58c2019-09-25 16:49:15 -07002267 while (!list_empty(&node_page_list)) {
2268 page = lru_to_page(&node_page_list);
2269 list_del(&page->lru);
2270 putback_lru_page(page);
2271 }
2272 }
2273
2274 return nr_reclaimed;
2275}
2276
Johannes Weinerb91ac372019-11-30 17:56:02 -08002277static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
2278 struct lruvec *lruvec, struct scan_control *sc)
2279{
2280 if (is_active_lru(lru)) {
2281 if (sc->may_deactivate & (1 << is_file_lru(lru)))
2282 shrink_active_list(nr_to_scan, lruvec, sc, lru);
2283 else
2284 sc->skipped_deactivate = 1;
2285 return 0;
2286 }
2287
2288 return shrink_inactive_list(nr_to_scan, lruvec, sc, lru);
2289}
2290
Rik van Riel59dc76b2016-05-20 16:56:31 -07002291/*
2292 * The inactive anon list should be small enough that the VM never has
2293 * to do too much work.
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002294 *
Rik van Riel59dc76b2016-05-20 16:56:31 -07002295 * The inactive file list should be small enough to leave most memory
2296 * to the established workingset on the scan-resistant active list,
2297 * but large enough to avoid thrashing the aggregate readahead window.
2298 *
2299 * Both inactive lists should also be large enough that each inactive
2300 * page has a chance to be referenced again before it is reclaimed.
2301 *
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002302 * If that fails and refaulting is observed, the inactive list grows.
2303 *
Rik van Riel59dc76b2016-05-20 16:56:31 -07002304 * The inactive_ratio is the target ratio of ACTIVE to INACTIVE pages
Andrey Ryabinin3a50d142017-11-15 17:34:15 -08002305 * on this LRU, maintained by the pageout code. An inactive_ratio
Rik van Riel59dc76b2016-05-20 16:56:31 -07002306 * of 3 means 3:1 or 25% of the pages are kept on the inactive list.
2307 *
2308 * total target max
2309 * memory ratio inactive
2310 * -------------------------------------
2311 * 10MB 1 5MB
2312 * 100MB 1 50MB
2313 * 1GB 3 250MB
2314 * 10GB 10 0.9GB
2315 * 100GB 31 3GB
2316 * 1TB 101 10GB
2317 * 10TB 320 32GB
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002318 */
Johannes Weinerb91ac372019-11-30 17:56:02 -08002319static bool inactive_is_low(struct lruvec *lruvec, enum lru_list inactive_lru)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002320{
Johannes Weinerb91ac372019-11-30 17:56:02 -08002321 enum lru_list active_lru = inactive_lru + LRU_ACTIVE;
Johannes Weiner2a2e4882017-05-03 14:55:03 -07002322 unsigned long inactive, active;
2323 unsigned long inactive_ratio;
Rik van Riel59dc76b2016-05-20 16:56:31 -07002324 unsigned long gb;
2325
Johannes Weinerb91ac372019-11-30 17:56:02 -08002326 inactive = lruvec_page_state(lruvec, NR_LRU_BASE + inactive_lru);
2327 active = lruvec_page_state(lruvec, NR_LRU_BASE + active_lru);
Mel Gormanf8d1a312016-07-28 15:47:34 -07002328
Johannes Weinerb91ac372019-11-30 17:56:02 -08002329 gb = (inactive + active) >> (30 - PAGE_SHIFT);
Joonsoo Kim40025702020-08-11 18:30:54 -07002330 if (gb)
Johannes Weinerb91ac372019-11-30 17:56:02 -08002331 inactive_ratio = int_sqrt(10 * gb);
2332 else
2333 inactive_ratio = 1;
Michal Hockofd538802017-02-22 15:45:58 -08002334
Rik van Riel59dc76b2016-05-20 16:56:31 -07002335 return inactive * inactive_ratio < active;
Rik van Rielb39415b2009-12-14 17:59:48 -08002336}
2337
Johannes Weiner9a265112013-02-22 16:32:17 -08002338enum scan_balance {
2339 SCAN_EQUAL,
2340 SCAN_FRACT,
2341 SCAN_ANON,
2342 SCAN_FILE,
2343};
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345/*
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002346 * Determine how aggressively the anon and file LRU lists should be
2347 * scanned. The relative value of each set of LRU lists is determined
2348 * by looking at the fraction of the pages scanned we did rotate back
2349 * onto the active list instead of evict.
2350 *
Wanpeng Libe7bd592012-06-14 20:41:02 +08002351 * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan
2352 * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002353 */
Johannes Weinerafaf07a2019-11-30 17:55:46 -08002354static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
2355 unsigned long *nr)
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002356{
Johannes Weinerafaf07a2019-11-30 17:55:46 -08002357 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
Johannes Weinerd483a5d2020-06-03 16:03:13 -07002358 unsigned long anon_cost, file_cost, total_cost;
Vladimir Davydov33377672016-01-20 15:02:59 -08002359 int swappiness = mem_cgroup_swappiness(memcg);
Yu Zhaoed017372020-10-15 20:09:55 -07002360 u64 fraction[ANON_AND_FILE];
Johannes Weiner9a265112013-02-22 16:32:17 -08002361 u64 denominator = 0; /* gcc */
Johannes Weiner9a265112013-02-22 16:32:17 -08002362 enum scan_balance scan_balance;
Johannes Weiner9a265112013-02-22 16:32:17 -08002363 unsigned long ap, fp;
2364 enum lru_list lru;
Shaohua Li76a33fc2010-05-24 14:32:36 -07002365
2366 /* If we have no swap space, do not bother scanning anon pages. */
Vladimir Davydovd8b38432016-01-20 15:03:07 -08002367 if (!sc->may_swap || mem_cgroup_get_nr_swap_pages(memcg) <= 0) {
Johannes Weiner9a265112013-02-22 16:32:17 -08002368 scan_balance = SCAN_FILE;
Shaohua Li76a33fc2010-05-24 14:32:36 -07002369 goto out;
2370 }
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002371
Johannes Weiner10316b32013-02-22 16:32:14 -08002372 /*
2373 * Global reclaim will swap to prevent OOM even with no
2374 * swappiness, but memcg users want to use this knob to
2375 * disable swapping for individual groups completely when
2376 * using the memory controller's swap limit feature would be
2377 * too expensive.
2378 */
Johannes Weinerb5ead352019-11-30 17:55:40 -08002379 if (cgroup_reclaim(sc) && !swappiness) {
Johannes Weiner9a265112013-02-22 16:32:17 -08002380 scan_balance = SCAN_FILE;
Johannes Weiner10316b32013-02-22 16:32:14 -08002381 goto out;
2382 }
2383
2384 /*
2385 * Do not apply any pressure balancing cleverness when the
2386 * system is close to OOM, scan both anon and file equally
2387 * (unless the swappiness setting disagrees with swapping).
2388 */
Johannes Weiner02695172014-08-06 16:06:17 -07002389 if (!sc->priority && swappiness) {
Johannes Weiner9a265112013-02-22 16:32:17 -08002390 scan_balance = SCAN_EQUAL;
Johannes Weiner10316b32013-02-22 16:32:14 -08002391 goto out;
2392 }
2393
Johannes Weiner11d16c22013-02-22 16:32:15 -08002394 /*
Johannes Weiner53138ce2019-11-30 17:55:56 -08002395 * If the system is almost out of file pages, force-scan anon.
Johannes Weiner62376252014-05-06 12:50:07 -07002396 */
Johannes Weinerb91ac372019-11-30 17:56:02 -08002397 if (sc->file_is_tiny) {
Johannes Weiner53138ce2019-11-30 17:55:56 -08002398 scan_balance = SCAN_ANON;
2399 goto out;
Johannes Weiner62376252014-05-06 12:50:07 -07002400 }
2401
2402 /*
Johannes Weinerb91ac372019-11-30 17:56:02 -08002403 * If there is enough inactive page cache, we do not reclaim
2404 * anything from the anonymous working right now.
Johannes Weiner7c5bd702013-02-22 16:32:10 -08002405 */
Johannes Weinerb91ac372019-11-30 17:56:02 -08002406 if (sc->cache_trim_mode) {
Johannes Weiner9a265112013-02-22 16:32:17 -08002407 scan_balance = SCAN_FILE;
Johannes Weiner7c5bd702013-02-22 16:32:10 -08002408 goto out;
2409 }
2410
Johannes Weiner9a265112013-02-22 16:32:17 -08002411 scan_balance = SCAN_FRACT;
Johannes Weiner7c5bd702013-02-22 16:32:10 -08002412 /*
Johannes Weiner314b57f2020-06-03 16:03:03 -07002413 * Calculate the pressure balance between anon and file pages.
2414 *
2415 * The amount of pressure we put on each LRU is inversely
2416 * proportional to the cost of reclaiming each list, as
2417 * determined by the share of pages that are refaulting, times
2418 * the relative IO cost of bringing back a swapped out
2419 * anonymous page vs reloading a filesystem page (swappiness).
2420 *
Johannes Weinerd483a5d2020-06-03 16:03:13 -07002421 * Although we limit that influence to ensure no list gets
2422 * left behind completely: at least a third of the pressure is
2423 * applied, before swappiness.
2424 *
Johannes Weiner314b57f2020-06-03 16:03:03 -07002425 * With swappiness at 100, anon and file have equal IO cost.
KOSAKI Motohiro58c37f62010-08-09 17:19:51 -07002426 */
Johannes Weinerd483a5d2020-06-03 16:03:13 -07002427 total_cost = sc->anon_cost + sc->file_cost;
2428 anon_cost = total_cost + sc->anon_cost;
2429 file_cost = total_cost + sc->file_cost;
2430 total_cost = anon_cost + file_cost;
KOSAKI Motohiro58c37f62010-08-09 17:19:51 -07002431
Johannes Weinerd483a5d2020-06-03 16:03:13 -07002432 ap = swappiness * (total_cost + 1);
2433 ap /= anon_cost + 1;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002434
Johannes Weinerd483a5d2020-06-03 16:03:13 -07002435 fp = (200 - swappiness) * (total_cost + 1);
2436 fp /= file_cost + 1;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002437
Shaohua Li76a33fc2010-05-24 14:32:36 -07002438 fraction[0] = ap;
2439 fraction[1] = fp;
Johannes Weinera4fe1632020-06-03 16:02:50 -07002440 denominator = ap + fp;
Shaohua Li76a33fc2010-05-24 14:32:36 -07002441out:
Johannes Weiner688035f2017-05-03 14:52:07 -07002442 for_each_evictable_lru(lru) {
2443 int file = is_file_lru(lru);
Chris Down9783aa92019-10-06 17:58:32 -07002444 unsigned long lruvec_size;
Johannes Weiner688035f2017-05-03 14:52:07 -07002445 unsigned long scan;
Chris Down1bc63fb2019-10-06 17:58:38 -07002446 unsigned long protection;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002447
Chris Down9783aa92019-10-06 17:58:32 -07002448 lruvec_size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx);
Yafang Shao22f74962020-08-06 23:22:01 -07002449 protection = mem_cgroup_protection(sc->target_mem_cgroup,
2450 memcg,
Chris Down1bc63fb2019-10-06 17:58:38 -07002451 sc->memcg_low_reclaim);
Chris Down9783aa92019-10-06 17:58:32 -07002452
Chris Down1bc63fb2019-10-06 17:58:38 -07002453 if (protection) {
Chris Down9783aa92019-10-06 17:58:32 -07002454 /*
2455 * Scale a cgroup's reclaim pressure by proportioning
2456 * its current usage to its memory.low or memory.min
2457 * setting.
2458 *
2459 * This is important, as otherwise scanning aggression
2460 * becomes extremely binary -- from nothing as we
2461 * approach the memory protection threshold, to totally
2462 * nominal as we exceed it. This results in requiring
2463 * setting extremely liberal protection thresholds. It
2464 * also means we simply get no protection at all if we
2465 * set it too low, which is not ideal.
Chris Down1bc63fb2019-10-06 17:58:38 -07002466 *
2467 * If there is any protection in place, we reduce scan
2468 * pressure by how much of the total memory used is
2469 * within protection thresholds.
Chris Down9783aa92019-10-06 17:58:32 -07002470 *
Chris Down9de7ca42019-10-06 17:58:35 -07002471 * There is one special case: in the first reclaim pass,
2472 * we skip over all groups that are within their low
2473 * protection. If that fails to reclaim enough pages to
2474 * satisfy the reclaim goal, we come back and override
2475 * the best-effort low protection. However, we still
2476 * ideally want to honor how well-behaved groups are in
2477 * that case instead of simply punishing them all
2478 * equally. As such, we reclaim them based on how much
Chris Down1bc63fb2019-10-06 17:58:38 -07002479 * memory they are using, reducing the scan pressure
2480 * again by how much of the total memory used is under
2481 * hard protection.
Chris Down9783aa92019-10-06 17:58:32 -07002482 */
Chris Down1bc63fb2019-10-06 17:58:38 -07002483 unsigned long cgroup_size = mem_cgroup_size(memcg);
2484
2485 /* Avoid TOCTOU with earlier protection check */
2486 cgroup_size = max(cgroup_size, protection);
2487
2488 scan = lruvec_size - lruvec_size * protection /
2489 cgroup_size;
Chris Down9783aa92019-10-06 17:58:32 -07002490
2491 /*
Chris Down1bc63fb2019-10-06 17:58:38 -07002492 * Minimally target SWAP_CLUSTER_MAX pages to keep
Ethon Paul55b65a52020-06-04 16:49:10 -07002493 * reclaim moving forwards, avoiding decrementing
Chris Down9de7ca42019-10-06 17:58:35 -07002494 * sc->priority further than desirable.
Chris Down9783aa92019-10-06 17:58:32 -07002495 */
Chris Down1bc63fb2019-10-06 17:58:38 -07002496 scan = max(scan, SWAP_CLUSTER_MAX);
Chris Down9783aa92019-10-06 17:58:32 -07002497 } else {
2498 scan = lruvec_size;
2499 }
2500
2501 scan >>= sc->priority;
2502
Johannes Weiner688035f2017-05-03 14:52:07 -07002503 /*
2504 * If the cgroup's already been deleted, make sure to
2505 * scrape out the remaining cache.
2506 */
2507 if (!scan && !mem_cgroup_online(memcg))
Chris Down9783aa92019-10-06 17:58:32 -07002508 scan = min(lruvec_size, SWAP_CLUSTER_MAX);
Johannes Weiner9a265112013-02-22 16:32:17 -08002509
Johannes Weiner688035f2017-05-03 14:52:07 -07002510 switch (scan_balance) {
2511 case SCAN_EQUAL:
2512 /* Scan lists relative to size */
2513 break;
2514 case SCAN_FRACT:
Johannes Weiner9a265112013-02-22 16:32:17 -08002515 /*
Johannes Weiner688035f2017-05-03 14:52:07 -07002516 * Scan types proportional to swappiness and
2517 * their relative recent reclaim efficiency.
Gavin Shan76073c62020-02-20 20:04:24 -08002518 * Make sure we don't miss the last page on
2519 * the offlined memory cgroups because of a
2520 * round-off error.
Johannes Weiner9a265112013-02-22 16:32:17 -08002521 */
Gavin Shan76073c62020-02-20 20:04:24 -08002522 scan = mem_cgroup_online(memcg) ?
2523 div64_u64(scan * fraction[file], denominator) :
2524 DIV64_U64_ROUND_UP(scan * fraction[file],
Roman Gushchin68600f62018-10-26 15:03:27 -07002525 denominator);
Johannes Weiner688035f2017-05-03 14:52:07 -07002526 break;
2527 case SCAN_FILE:
2528 case SCAN_ANON:
2529 /* Scan one type exclusively */
Mateusz Noseke072bff2020-04-01 21:10:15 -07002530 if ((scan_balance == SCAN_FILE) != file)
Johannes Weiner688035f2017-05-03 14:52:07 -07002531 scan = 0;
Johannes Weiner688035f2017-05-03 14:52:07 -07002532 break;
2533 default:
2534 /* Look ma, no brain */
2535 BUG();
Johannes Weiner9a265112013-02-22 16:32:17 -08002536 }
Johannes Weiner688035f2017-05-03 14:52:07 -07002537
Johannes Weiner688035f2017-05-03 14:52:07 -07002538 nr[lru] = scan;
Shaohua Li76a33fc2010-05-24 14:32:36 -07002539 }
Wu Fengguang6e08a362009-06-16 15:32:29 -07002540}
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002541
Johannes Weinerafaf07a2019-11-30 17:55:46 -08002542static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002543{
2544 unsigned long nr[NR_LRU_LISTS];
Mel Gormane82e0562013-07-03 15:01:44 -07002545 unsigned long targets[NR_LRU_LISTS];
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002546 unsigned long nr_to_scan;
2547 enum lru_list lru;
2548 unsigned long nr_reclaimed = 0;
2549 unsigned long nr_to_reclaim = sc->nr_to_reclaim;
2550 struct blk_plug plug;
Mel Gorman1a501902014-06-04 16:10:49 -07002551 bool scan_adjusted;
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002552
Johannes Weinerafaf07a2019-11-30 17:55:46 -08002553 get_scan_count(lruvec, sc, nr);
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002554
Mel Gormane82e0562013-07-03 15:01:44 -07002555 /* Record the original scan target for proportional adjustments later */
2556 memcpy(targets, nr, sizeof(nr));
2557
Mel Gorman1a501902014-06-04 16:10:49 -07002558 /*
2559 * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal
2560 * event that can occur when there is little memory pressure e.g.
2561 * multiple streaming readers/writers. Hence, we do not abort scanning
2562 * when the requested number of pages are reclaimed when scanning at
2563 * DEF_PRIORITY on the assumption that the fact we are direct
2564 * reclaiming implies that kswapd is not keeping up and it is best to
2565 * do a batch of work at once. For memcg reclaim one check is made to
2566 * abort proportional reclaim if either the file or anon lru has already
2567 * dropped to zero at the first pass.
2568 */
Johannes Weinerb5ead352019-11-30 17:55:40 -08002569 scan_adjusted = (!cgroup_reclaim(sc) && !current_is_kswapd() &&
Mel Gorman1a501902014-06-04 16:10:49 -07002570 sc->priority == DEF_PRIORITY);
2571
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002572 blk_start_plug(&plug);
2573 while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
2574 nr[LRU_INACTIVE_FILE]) {
Mel Gormane82e0562013-07-03 15:01:44 -07002575 unsigned long nr_anon, nr_file, percentage;
2576 unsigned long nr_scanned;
2577
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002578 for_each_evictable_lru(lru) {
2579 if (nr[lru]) {
2580 nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX);
2581 nr[lru] -= nr_to_scan;
2582
2583 nr_reclaimed += shrink_list(lru, nr_to_scan,
Johannes Weiner3b991202019-04-18 17:50:34 -07002584 lruvec, sc);
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002585 }
2586 }
Mel Gormane82e0562013-07-03 15:01:44 -07002587
Michal Hockobd041732016-12-02 17:26:48 -08002588 cond_resched();
2589
Mel Gormane82e0562013-07-03 15:01:44 -07002590 if (nr_reclaimed < nr_to_reclaim || scan_adjusted)
2591 continue;
2592
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002593 /*
Mel Gormane82e0562013-07-03 15:01:44 -07002594 * For kswapd and memcg, reclaim at least the number of pages
Mel Gorman1a501902014-06-04 16:10:49 -07002595 * requested. Ensure that the anon and file LRUs are scanned
Mel Gormane82e0562013-07-03 15:01:44 -07002596 * proportionally what was requested by get_scan_count(). We
2597 * stop reclaiming one LRU and reduce the amount scanning
2598 * proportional to the original scan target.
2599 */
2600 nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE];
2601 nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON];
2602
Mel Gorman1a501902014-06-04 16:10:49 -07002603 /*
2604 * It's just vindictive to attack the larger once the smaller
2605 * has gone to zero. And given the way we stop scanning the
2606 * smaller below, this makes sure that we only make one nudge
2607 * towards proportionality once we've got nr_to_reclaim.
2608 */
2609 if (!nr_file || !nr_anon)
2610 break;
2611
Mel Gormane82e0562013-07-03 15:01:44 -07002612 if (nr_file > nr_anon) {
2613 unsigned long scan_target = targets[LRU_INACTIVE_ANON] +
2614 targets[LRU_ACTIVE_ANON] + 1;
2615 lru = LRU_BASE;
2616 percentage = nr_anon * 100 / scan_target;
2617 } else {
2618 unsigned long scan_target = targets[LRU_INACTIVE_FILE] +
2619 targets[LRU_ACTIVE_FILE] + 1;
2620 lru = LRU_FILE;
2621 percentage = nr_file * 100 / scan_target;
2622 }
2623
2624 /* Stop scanning the smaller of the LRU */
2625 nr[lru] = 0;
2626 nr[lru + LRU_ACTIVE] = 0;
2627
2628 /*
2629 * Recalculate the other LRU scan count based on its original
2630 * scan target and the percentage scanning already complete
2631 */
2632 lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE;
2633 nr_scanned = targets[lru] - nr[lru];
2634 nr[lru] = targets[lru] * (100 - percentage) / 100;
2635 nr[lru] -= min(nr[lru], nr_scanned);
2636
2637 lru += LRU_ACTIVE;
2638 nr_scanned = targets[lru] - nr[lru];
2639 nr[lru] = targets[lru] * (100 - percentage) / 100;
2640 nr[lru] -= min(nr[lru], nr_scanned);
2641
2642 scan_adjusted = true;
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002643 }
2644 blk_finish_plug(&plug);
2645 sc->nr_reclaimed += nr_reclaimed;
2646
2647 /*
2648 * Even if we did not try to evict anon pages at all, we want to
2649 * rebalance the anon lru active/inactive ratio.
2650 */
Johannes Weinerb91ac372019-11-30 17:56:02 -08002651 if (total_swap_pages && inactive_is_low(lruvec, LRU_INACTIVE_ANON))
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002652 shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
2653 sc, LRU_ACTIVE_ANON);
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002654}
2655
Mel Gorman23b9da52012-05-29 15:06:20 -07002656/* Use reclaim/compaction for costly allocs or under memory pressure */
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002657static bool in_reclaim_compaction(struct scan_control *sc)
Mel Gorman23b9da52012-05-29 15:06:20 -07002658{
Kirill A. Shutemovd84da3f2012-12-11 16:00:31 -08002659 if (IS_ENABLED(CONFIG_COMPACTION) && sc->order &&
Mel Gorman23b9da52012-05-29 15:06:20 -07002660 (sc->order > PAGE_ALLOC_COSTLY_ORDER ||
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002661 sc->priority < DEF_PRIORITY - 2))
Mel Gorman23b9da52012-05-29 15:06:20 -07002662 return true;
2663
2664 return false;
2665}
2666
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002667/*
Mel Gorman23b9da52012-05-29 15:06:20 -07002668 * Reclaim/compaction is used for high-order allocation requests. It reclaims
2669 * order-0 pages before compacting the zone. should_continue_reclaim() returns
2670 * true if more pages should be reclaimed such that when the page allocator
Qiwu Chendf3a45f2020-06-03 16:01:21 -07002671 * calls try_to_compact_pages() that it will have enough free pages to succeed.
Mel Gorman23b9da52012-05-29 15:06:20 -07002672 * It will give up earlier than that if there is difficulty reclaiming pages.
Mel Gorman3e7d3442011-01-13 15:45:56 -08002673 */
Mel Gormana9dd0a82016-07-28 15:46:02 -07002674static inline bool should_continue_reclaim(struct pglist_data *pgdat,
Mel Gorman3e7d3442011-01-13 15:45:56 -08002675 unsigned long nr_reclaimed,
Mel Gorman3e7d3442011-01-13 15:45:56 -08002676 struct scan_control *sc)
2677{
2678 unsigned long pages_for_compaction;
2679 unsigned long inactive_lru_pages;
Mel Gormana9dd0a82016-07-28 15:46:02 -07002680 int z;
Mel Gorman3e7d3442011-01-13 15:45:56 -08002681
2682 /* If not in reclaim/compaction mode, stop */
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002683 if (!in_reclaim_compaction(sc))
Mel Gorman3e7d3442011-01-13 15:45:56 -08002684 return false;
2685
Vlastimil Babka5ee04712019-09-23 15:37:29 -07002686 /*
2687 * Stop if we failed to reclaim any pages from the last SWAP_CLUSTER_MAX
2688 * number of pages that were scanned. This will return to the caller
2689 * with the risk reclaim/compaction and the resulting allocation attempt
2690 * fails. In the past we have tried harder for __GFP_RETRY_MAYFAIL
2691 * allocations through requiring that the full LRU list has been scanned
2692 * first, by assuming that zero delta of sc->nr_scanned means full LRU
2693 * scan, but that approximation was wrong, and there were corner cases
2694 * where always a non-zero amount of pages were scanned.
2695 */
2696 if (!nr_reclaimed)
2697 return false;
Mel Gorman3e7d3442011-01-13 15:45:56 -08002698
Mel Gorman3e7d3442011-01-13 15:45:56 -08002699 /* If compaction would go ahead or the allocation would succeed, stop */
Mel Gormana9dd0a82016-07-28 15:46:02 -07002700 for (z = 0; z <= sc->reclaim_idx; z++) {
2701 struct zone *zone = &pgdat->node_zones[z];
Mel Gorman6aa303d2016-09-01 16:14:55 -07002702 if (!managed_zone(zone))
Mel Gormana9dd0a82016-07-28 15:46:02 -07002703 continue;
2704
2705 switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) {
Vlastimil Babkacf378312016-10-07 16:57:41 -07002706 case COMPACT_SUCCESS:
Mel Gormana9dd0a82016-07-28 15:46:02 -07002707 case COMPACT_CONTINUE:
2708 return false;
2709 default:
2710 /* check next zone */
2711 ;
2712 }
Mel Gorman3e7d3442011-01-13 15:45:56 -08002713 }
Hillf Danton1c6c1592019-09-23 15:37:26 -07002714
2715 /*
2716 * If we have not reclaimed enough pages for compaction and the
2717 * inactive lists are large enough, continue reclaiming
2718 */
2719 pages_for_compaction = compact_gap(sc->order);
2720 inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE);
2721 if (get_nr_swap_pages() > 0)
2722 inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON);
2723
Vlastimil Babka5ee04712019-09-23 15:37:29 -07002724 return inactive_lru_pages > pages_for_compaction;
Mel Gorman3e7d3442011-01-13 15:45:56 -08002725}
2726
Johannes Weiner0f6a5cf2019-11-30 17:55:49 -08002727static void shrink_node_memcgs(pg_data_t *pgdat, struct scan_control *sc)
Johannes Weinerf16015f2012-01-12 17:17:52 -08002728{
Johannes Weiner0f6a5cf2019-11-30 17:55:49 -08002729 struct mem_cgroup *target_memcg = sc->target_mem_cgroup;
Johannes Weinerd2af3392019-11-30 17:55:43 -08002730 struct mem_cgroup *memcg;
Johannes Weinerf16015f2012-01-12 17:17:52 -08002731
Johannes Weiner0f6a5cf2019-11-30 17:55:49 -08002732 memcg = mem_cgroup_iter(target_memcg, NULL, NULL);
Johannes Weiner56600482012-01-12 17:17:59 -08002733 do {
Johannes Weinerafaf07a2019-11-30 17:55:46 -08002734 struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat);
Johannes Weinerd2af3392019-11-30 17:55:43 -08002735 unsigned long reclaimed;
2736 unsigned long scanned;
Johannes Weiner56600482012-01-12 17:17:59 -08002737
Xunlei Pange3336ca2020-09-04 16:35:27 -07002738 /*
2739 * This loop can become CPU-bound when target memcgs
2740 * aren't eligible for reclaim - either because they
2741 * don't have any reclaimable pages, or because their
2742 * memory is explicitly protected. Avoid soft lockups.
2743 */
2744 cond_resched();
2745
Chris Down45c7f7e2020-08-06 23:22:05 -07002746 mem_cgroup_calculate_protection(target_memcg, memcg);
2747
2748 if (mem_cgroup_below_min(memcg)) {
Johannes Weinerd2af3392019-11-30 17:55:43 -08002749 /*
2750 * Hard protection.
2751 * If there is no reclaimable memory, OOM.
2752 */
2753 continue;
Chris Down45c7f7e2020-08-06 23:22:05 -07002754 } else if (mem_cgroup_below_low(memcg)) {
Johannes Weinerd2af3392019-11-30 17:55:43 -08002755 /*
2756 * Soft protection.
2757 * Respect the protection only as long as
2758 * there is an unprotected supply
2759 * of reclaimable memory from other cgroups.
2760 */
2761 if (!sc->memcg_low_reclaim) {
2762 sc->memcg_low_skipped = 1;
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07002763 continue;
Johannes Weiner241994ed2015-02-11 15:26:06 -08002764 }
Johannes Weinerd2af3392019-11-30 17:55:43 -08002765 memcg_memory_event(memcg, MEMCG_LOW);
Johannes Weiner6b4f7792014-12-12 16:56:13 -08002766 }
2767
Johannes Weinerd2af3392019-11-30 17:55:43 -08002768 reclaimed = sc->nr_reclaimed;
2769 scanned = sc->nr_scanned;
Johannes Weinerafaf07a2019-11-30 17:55:46 -08002770
2771 shrink_lruvec(lruvec, sc);
Anton Vorontsov70ddf632013-04-29 15:08:31 -07002772
Johannes Weinerd2af3392019-11-30 17:55:43 -08002773 shrink_slab(sc->gfp_mask, pgdat->node_id, memcg,
2774 sc->priority);
Johannes Weiner2344d7e2014-08-06 16:06:15 -07002775
Johannes Weinerd2af3392019-11-30 17:55:43 -08002776 /* Record the group's reclaim efficiency */
2777 vmpressure(sc->gfp_mask, memcg, false,
2778 sc->nr_scanned - scanned,
2779 sc->nr_reclaimed - reclaimed);
Andrey Ryabinind108c772018-04-10 16:27:59 -07002780
Johannes Weiner0f6a5cf2019-11-30 17:55:49 -08002781 } while ((memcg = mem_cgroup_iter(target_memcg, memcg, NULL)));
2782}
2783
Liu Song6c9e09072020-01-30 22:14:08 -08002784static void shrink_node(pg_data_t *pgdat, struct scan_control *sc)
Johannes Weiner0f6a5cf2019-11-30 17:55:49 -08002785{
2786 struct reclaim_state *reclaim_state = current->reclaim_state;
Johannes Weiner0f6a5cf2019-11-30 17:55:49 -08002787 unsigned long nr_reclaimed, nr_scanned;
Johannes Weiner1b051172019-11-30 17:55:52 -08002788 struct lruvec *target_lruvec;
Johannes Weiner0f6a5cf2019-11-30 17:55:49 -08002789 bool reclaimable = false;
Johannes Weinerb91ac372019-11-30 17:56:02 -08002790 unsigned long file;
Johannes Weiner0f6a5cf2019-11-30 17:55:49 -08002791
Johannes Weiner1b051172019-11-30 17:55:52 -08002792 target_lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, pgdat);
2793
Johannes Weiner0f6a5cf2019-11-30 17:55:49 -08002794again:
2795 memset(&sc->nr, 0, sizeof(sc->nr));
2796
2797 nr_reclaimed = sc->nr_reclaimed;
2798 nr_scanned = sc->nr_scanned;
2799
Johannes Weiner53138ce2019-11-30 17:55:56 -08002800 /*
Johannes Weiner7cf111b2020-06-03 16:03:06 -07002801 * Determine the scan balance between anon and file LRUs.
2802 */
Alex Shi6168d0d2020-12-15 12:34:29 -08002803 spin_lock_irq(&target_lruvec->lru_lock);
Johannes Weiner7cf111b2020-06-03 16:03:06 -07002804 sc->anon_cost = target_lruvec->anon_cost;
2805 sc->file_cost = target_lruvec->file_cost;
Alex Shi6168d0d2020-12-15 12:34:29 -08002806 spin_unlock_irq(&target_lruvec->lru_lock);
Johannes Weiner7cf111b2020-06-03 16:03:06 -07002807
2808 /*
Johannes Weinerb91ac372019-11-30 17:56:02 -08002809 * Target desirable inactive:active list ratios for the anon
2810 * and file LRU lists.
2811 */
2812 if (!sc->force_deactivate) {
2813 unsigned long refaults;
2814
Joonsoo Kim170b04b72020-08-11 18:30:43 -07002815 refaults = lruvec_page_state(target_lruvec,
2816 WORKINGSET_ACTIVATE_ANON);
2817 if (refaults != target_lruvec->refaults[0] ||
2818 inactive_is_low(target_lruvec, LRU_INACTIVE_ANON))
Johannes Weinerb91ac372019-11-30 17:56:02 -08002819 sc->may_deactivate |= DEACTIVATE_ANON;
2820 else
2821 sc->may_deactivate &= ~DEACTIVATE_ANON;
2822
2823 /*
2824 * When refaults are being observed, it means a new
2825 * workingset is being established. Deactivate to get
2826 * rid of any stale active pages quickly.
2827 */
2828 refaults = lruvec_page_state(target_lruvec,
Joonsoo Kim170b04b72020-08-11 18:30:43 -07002829 WORKINGSET_ACTIVATE_FILE);
2830 if (refaults != target_lruvec->refaults[1] ||
Johannes Weinerb91ac372019-11-30 17:56:02 -08002831 inactive_is_low(target_lruvec, LRU_INACTIVE_FILE))
2832 sc->may_deactivate |= DEACTIVATE_FILE;
2833 else
2834 sc->may_deactivate &= ~DEACTIVATE_FILE;
2835 } else
2836 sc->may_deactivate = DEACTIVATE_ANON | DEACTIVATE_FILE;
2837
2838 /*
2839 * If we have plenty of inactive file pages that aren't
2840 * thrashing, try to reclaim those first before touching
2841 * anonymous pages.
2842 */
2843 file = lruvec_page_state(target_lruvec, NR_INACTIVE_FILE);
2844 if (file >> sc->priority && !(sc->may_deactivate & DEACTIVATE_FILE))
2845 sc->cache_trim_mode = 1;
2846 else
2847 sc->cache_trim_mode = 0;
2848
2849 /*
Johannes Weiner53138ce2019-11-30 17:55:56 -08002850 * Prevent the reclaimer from falling into the cache trap: as
2851 * cache pages start out inactive, every cache fault will tip
2852 * the scan balance towards the file LRU. And as the file LRU
2853 * shrinks, so does the window for rotation from references.
2854 * This means we have a runaway feedback loop where a tiny
2855 * thrashing file LRU becomes infinitely more attractive than
2856 * anon pages. Try to detect this based on file LRU size.
2857 */
2858 if (!cgroup_reclaim(sc)) {
Johannes Weiner53138ce2019-11-30 17:55:56 -08002859 unsigned long total_high_wmark = 0;
Johannes Weinerb91ac372019-11-30 17:56:02 -08002860 unsigned long free, anon;
2861 int z;
Johannes Weiner53138ce2019-11-30 17:55:56 -08002862
2863 free = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES);
2864 file = node_page_state(pgdat, NR_ACTIVE_FILE) +
2865 node_page_state(pgdat, NR_INACTIVE_FILE);
2866
2867 for (z = 0; z < MAX_NR_ZONES; z++) {
2868 struct zone *zone = &pgdat->node_zones[z];
2869 if (!managed_zone(zone))
2870 continue;
2871
2872 total_high_wmark += high_wmark_pages(zone);
2873 }
2874
Johannes Weinerb91ac372019-11-30 17:56:02 -08002875 /*
2876 * Consider anon: if that's low too, this isn't a
2877 * runaway file reclaim problem, but rather just
2878 * extreme pressure. Reclaim as per usual then.
2879 */
2880 anon = node_page_state(pgdat, NR_INACTIVE_ANON);
2881
2882 sc->file_is_tiny =
2883 file + free <= total_high_wmark &&
2884 !(sc->may_deactivate & DEACTIVATE_ANON) &&
2885 anon >> sc->priority;
Johannes Weiner53138ce2019-11-30 17:55:56 -08002886 }
2887
Johannes Weiner0f6a5cf2019-11-30 17:55:49 -08002888 shrink_node_memcgs(pgdat, sc);
Andrey Ryabinind108c772018-04-10 16:27:59 -07002889
Johannes Weinerd2af3392019-11-30 17:55:43 -08002890 if (reclaim_state) {
2891 sc->nr_reclaimed += reclaim_state->reclaimed_slab;
2892 reclaim_state->reclaimed_slab = 0;
2893 }
Andrey Ryabinind108c772018-04-10 16:27:59 -07002894
Johannes Weinerd2af3392019-11-30 17:55:43 -08002895 /* Record the subtree's reclaim efficiency */
Johannes Weiner1b051172019-11-30 17:55:52 -08002896 vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true,
Johannes Weinerd2af3392019-11-30 17:55:43 -08002897 sc->nr_scanned - nr_scanned,
2898 sc->nr_reclaimed - nr_reclaimed);
2899
2900 if (sc->nr_reclaimed - nr_reclaimed)
2901 reclaimable = true;
2902
2903 if (current_is_kswapd()) {
2904 /*
2905 * If reclaim is isolating dirty pages under writeback,
2906 * it implies that the long-lived page allocation rate
2907 * is exceeding the page laundering rate. Either the
2908 * global limits are not being effective at throttling
2909 * processes due to the page distribution throughout
2910 * zones or there is heavy usage of a slow backing
2911 * device. The only option is to throttle from reclaim
2912 * context which is not ideal as there is no guarantee
2913 * the dirtying process is throttled in the same way
2914 * balance_dirty_pages() manages.
2915 *
2916 * Once a node is flagged PGDAT_WRITEBACK, kswapd will
2917 * count the number of pages under pages flagged for
2918 * immediate reclaim and stall if any are encountered
2919 * in the nr_immediate check below.
2920 */
2921 if (sc->nr.writeback && sc->nr.writeback == sc->nr.taken)
2922 set_bit(PGDAT_WRITEBACK, &pgdat->flags);
Andrey Ryabinind108c772018-04-10 16:27:59 -07002923
Johannes Weinerd2af3392019-11-30 17:55:43 -08002924 /* Allow kswapd to start writing pages during reclaim.*/
2925 if (sc->nr.unqueued_dirty == sc->nr.file_taken)
2926 set_bit(PGDAT_DIRTY, &pgdat->flags);
Andrey Ryabinine3c1ac52018-04-10 16:28:03 -07002927
2928 /*
Randy Dunlap1eba09c2020-08-11 18:33:26 -07002929 * If kswapd scans pages marked for immediate
Johannes Weinerd2af3392019-11-30 17:55:43 -08002930 * reclaim and under writeback (nr_immediate), it
2931 * implies that pages are cycling through the LRU
2932 * faster than they are written so also forcibly stall.
Andrey Ryabinind108c772018-04-10 16:27:59 -07002933 */
Johannes Weinerd2af3392019-11-30 17:55:43 -08002934 if (sc->nr.immediate)
2935 congestion_wait(BLK_RW_ASYNC, HZ/10);
2936 }
Andrey Ryabinind108c772018-04-10 16:27:59 -07002937
Johannes Weinerd2af3392019-11-30 17:55:43 -08002938 /*
Johannes Weiner1b051172019-11-30 17:55:52 -08002939 * Tag a node/memcg as congested if all the dirty pages
2940 * scanned were backed by a congested BDI and
2941 * wait_iff_congested will stall.
2942 *
Johannes Weinerd2af3392019-11-30 17:55:43 -08002943 * Legacy memcg will stall in page writeback so avoid forcibly
2944 * stalling in wait_iff_congested().
2945 */
Johannes Weiner1b051172019-11-30 17:55:52 -08002946 if ((current_is_kswapd() ||
2947 (cgroup_reclaim(sc) && writeback_throttling_sane(sc))) &&
Johannes Weinerd2af3392019-11-30 17:55:43 -08002948 sc->nr.dirty && sc->nr.dirty == sc->nr.congested)
Johannes Weiner1b051172019-11-30 17:55:52 -08002949 set_bit(LRUVEC_CONGESTED, &target_lruvec->flags);
Johannes Weinerd2af3392019-11-30 17:55:43 -08002950
2951 /*
2952 * Stall direct reclaim for IO completions if underlying BDIs
2953 * and node is congested. Allow kswapd to continue until it
2954 * starts encountering unqueued dirty pages or cycling through
2955 * the LRU too quickly.
2956 */
Johannes Weiner1b051172019-11-30 17:55:52 -08002957 if (!current_is_kswapd() && current_may_throttle() &&
2958 !sc->hibernation_mode &&
2959 test_bit(LRUVEC_CONGESTED, &target_lruvec->flags))
Johannes Weinerd2af3392019-11-30 17:55:43 -08002960 wait_iff_congested(BLK_RW_ASYNC, HZ/10);
2961
2962 if (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed,
2963 sc))
2964 goto again;
Johannes Weiner2344d7e2014-08-06 16:06:15 -07002965
Johannes Weinerc73322d2017-05-03 14:51:51 -07002966 /*
2967 * Kswapd gives up on balancing particular nodes after too
2968 * many failures to reclaim anything from them and goes to
2969 * sleep. On reclaim progress, reset the failure counter. A
2970 * successful direct reclaim run will revive a dormant kswapd.
2971 */
2972 if (reclaimable)
2973 pgdat->kswapd_failures = 0;
Johannes Weinerf16015f2012-01-12 17:17:52 -08002974}
2975
Vlastimil Babka53853e22014-10-09 15:27:02 -07002976/*
Vlastimil Babkafdd4c6142016-10-07 16:58:03 -07002977 * Returns true if compaction should go ahead for a costly-order request, or
2978 * the allocation would already succeed without compaction. Return false if we
2979 * should reclaim first.
Vlastimil Babka53853e22014-10-09 15:27:02 -07002980 */
Mel Gorman4f588332016-07-28 15:46:38 -07002981static inline bool compaction_ready(struct zone *zone, struct scan_control *sc)
Mel Gormanfe4b1b22012-01-12 17:19:45 -08002982{
Mel Gorman31483b62016-07-28 15:45:46 -07002983 unsigned long watermark;
Vlastimil Babkafdd4c6142016-10-07 16:58:03 -07002984 enum compact_result suitable;
Mel Gormanfe4b1b22012-01-12 17:19:45 -08002985
Vlastimil Babkafdd4c6142016-10-07 16:58:03 -07002986 suitable = compaction_suitable(zone, sc->order, 0, sc->reclaim_idx);
2987 if (suitable == COMPACT_SUCCESS)
2988 /* Allocation should succeed already. Don't reclaim. */
2989 return true;
2990 if (suitable == COMPACT_SKIPPED)
2991 /* Compaction cannot yet proceed. Do reclaim. */
Mel Gormanfe4b1b22012-01-12 17:19:45 -08002992 return false;
2993
Vlastimil Babkafdd4c6142016-10-07 16:58:03 -07002994 /*
2995 * Compaction is already possible, but it takes time to run and there
2996 * are potentially other callers using the pages just freed. So proceed
2997 * with reclaim to make a buffer of free pages available to give
2998 * compaction a reasonable chance of completing and allocating the page.
2999 * Note that we won't actually reclaim the whole buffer in one attempt
3000 * as the target watermark in should_continue_reclaim() is lower. But if
3001 * we are already above the high+gap watermark, don't reclaim at all.
3002 */
3003 watermark = high_wmark_pages(zone) + compact_gap(sc->order);
3004
3005 return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx);
Mel Gormanfe4b1b22012-01-12 17:19:45 -08003006}
3007
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008/*
3009 * This is the direct reclaim path, for page-allocating processes. We only
3010 * try to reclaim pages from zones which will satisfy the caller's allocation
3011 * request.
3012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 * If a zone is deemed to be full of pinned pages then just give it a light
3014 * scan then give up on it.
3015 */
Michal Hocko0a0337e2016-05-20 16:57:00 -07003016static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017{
Mel Gormandd1a2392008-04-28 02:12:17 -07003018 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07003019 struct zone *zone;
Andrew Morton0608f432013-09-24 15:27:41 -07003020 unsigned long nr_soft_reclaimed;
3021 unsigned long nr_soft_scanned;
Weijie Yang619d0d762014-04-07 15:36:59 -07003022 gfp_t orig_mask;
Mel Gorman79dafcd2016-07-28 15:45:53 -07003023 pg_data_t *last_pgdat = NULL;
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08003024
Mel Gormancc715d92012-03-21 16:34:00 -07003025 /*
3026 * If the number of buffer_heads in the machine exceeds the maximum
3027 * allowed level, force direct reclaim to scan the highmem zone as
3028 * highmem pages could be pinning lowmem pages storing buffer_heads
3029 */
Weijie Yang619d0d762014-04-07 15:36:59 -07003030 orig_mask = sc->gfp_mask;
Mel Gormanb2e18752016-07-28 15:45:37 -07003031 if (buffer_heads_over_limit) {
Mel Gormancc715d92012-03-21 16:34:00 -07003032 sc->gfp_mask |= __GFP_HIGHMEM;
Mel Gorman4f588332016-07-28 15:46:38 -07003033 sc->reclaim_idx = gfp_zone(sc->gfp_mask);
Mel Gormanb2e18752016-07-28 15:45:37 -07003034 }
Mel Gormancc715d92012-03-21 16:34:00 -07003035
Mel Gormand4debc62010-08-09 17:19:29 -07003036 for_each_zone_zonelist_nodemask(zone, z, zonelist,
Mel Gormanb2e18752016-07-28 15:45:37 -07003037 sc->reclaim_idx, sc->nodemask) {
Mel Gormanb2e18752016-07-28 15:45:37 -07003038 /*
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08003039 * Take care memory controller reclaiming has small influence
3040 * to global LRU.
3041 */
Johannes Weinerb5ead352019-11-30 17:55:40 -08003042 if (!cgroup_reclaim(sc)) {
Vladimir Davydov344736f2014-10-20 15:50:30 +04003043 if (!cpuset_zone_allowed(zone,
3044 GFP_KERNEL | __GFP_HARDWALL))
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08003045 continue;
Vladimir Davydov65ec02c2014-04-03 14:47:20 -07003046
Johannes Weiner0b064962014-08-06 16:06:12 -07003047 /*
3048 * If we already have plenty of memory free for
3049 * compaction in this zone, don't free any more.
3050 * Even though compaction is invoked for any
3051 * non-zero order, only frequent costly order
3052 * reclamation is disruptive enough to become a
3053 * noticeable problem, like transparent huge
3054 * page allocations.
3055 */
3056 if (IS_ENABLED(CONFIG_COMPACTION) &&
3057 sc->order > PAGE_ALLOC_COSTLY_ORDER &&
Mel Gorman4f588332016-07-28 15:46:38 -07003058 compaction_ready(zone, sc)) {
Johannes Weiner0b064962014-08-06 16:06:12 -07003059 sc->compaction_ready = true;
3060 continue;
Rik van Riele0887c12011-10-31 17:09:31 -07003061 }
Johannes Weiner0b064962014-08-06 16:06:12 -07003062
Andrew Morton0608f432013-09-24 15:27:41 -07003063 /*
Mel Gorman79dafcd2016-07-28 15:45:53 -07003064 * Shrink each node in the zonelist once. If the
3065 * zonelist is ordered by zone (not the default) then a
3066 * node may be shrunk multiple times but in that case
3067 * the user prefers lower zones being preserved.
3068 */
3069 if (zone->zone_pgdat == last_pgdat)
3070 continue;
3071
3072 /*
Andrew Morton0608f432013-09-24 15:27:41 -07003073 * This steals pages from memory cgroups over softlimit
3074 * and returns the number of reclaimed pages and
3075 * scanned pages. This works for global memory pressure
3076 * and balancing, not for a memcg's limit.
3077 */
3078 nr_soft_scanned = 0;
Mel Gormanef8f2322016-07-28 15:46:05 -07003079 nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat,
Andrew Morton0608f432013-09-24 15:27:41 -07003080 sc->order, sc->gfp_mask,
3081 &nr_soft_scanned);
3082 sc->nr_reclaimed += nr_soft_reclaimed;
3083 sc->nr_scanned += nr_soft_scanned;
KAMEZAWA Hiroyukiac34a1a2011-06-27 16:18:12 -07003084 /* need some check for avoid more shrink_zone() */
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08003085 }
Nick Piggin408d8542006-09-25 23:31:27 -07003086
Mel Gorman79dafcd2016-07-28 15:45:53 -07003087 /* See comment about same check for global reclaim above */
3088 if (zone->zone_pgdat == last_pgdat)
3089 continue;
3090 last_pgdat = zone->zone_pgdat;
Mel Gorman970a39a2016-07-28 15:46:35 -07003091 shrink_node(zone->zone_pgdat, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 }
Mel Gormane0c23272011-10-31 17:09:33 -07003093
Vladimir Davydov65ec02c2014-04-03 14:47:20 -07003094 /*
Weijie Yang619d0d762014-04-07 15:36:59 -07003095 * Restore to original mask to avoid the impact on the caller if we
3096 * promoted it to __GFP_HIGHMEM.
3097 */
3098 sc->gfp_mask = orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099}
Rik van Riel4f98a2f2008-10-18 20:26:32 -07003100
Johannes Weinerb9107182019-11-30 17:55:59 -08003101static void snapshot_refaults(struct mem_cgroup *target_memcg, pg_data_t *pgdat)
Johannes Weiner2a2e4882017-05-03 14:55:03 -07003102{
Johannes Weinerb9107182019-11-30 17:55:59 -08003103 struct lruvec *target_lruvec;
3104 unsigned long refaults;
Johannes Weiner2a2e4882017-05-03 14:55:03 -07003105
Johannes Weinerb9107182019-11-30 17:55:59 -08003106 target_lruvec = mem_cgroup_lruvec(target_memcg, pgdat);
Joonsoo Kim170b04b72020-08-11 18:30:43 -07003107 refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_ANON);
3108 target_lruvec->refaults[0] = refaults;
3109 refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_FILE);
3110 target_lruvec->refaults[1] = refaults;
Johannes Weiner2a2e4882017-05-03 14:55:03 -07003111}
3112
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113/*
3114 * This is the main entry point to direct page reclaim.
3115 *
3116 * If a full scan of the inactive list fails to free enough memory then we
3117 * are "out of memory" and something needs to be killed.
3118 *
3119 * If the caller is !__GFP_FS then the probability of a failure is reasonably
3120 * high - the zone may be full of dirty or under-writeback pages, which this
Jens Axboe5b0830c2009-09-23 19:37:09 +02003121 * caller can't do much about. We kick the writeback threads and take explicit
3122 * naps in the hope that some of these pages can be written. But if the
3123 * allocating task holds filesystem locks which prevent writeout this might not
3124 * work, and the allocation attempt will fail.
Nishanth Aravamudana41f24e2008-04-29 00:58:25 -07003125 *
3126 * returns: 0, if no pages reclaimed
3127 * else, the number of pages reclaimed
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 */
Mel Gormandac1d272008-04-28 02:12:12 -07003129static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
Vladimir Davydov3115cd92014-04-03 14:47:22 -07003130 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131{
Johannes Weiner241994ed2015-02-11 15:26:06 -08003132 int initial_priority = sc->priority;
Johannes Weiner2a2e4882017-05-03 14:55:03 -07003133 pg_data_t *last_pgdat;
3134 struct zoneref *z;
3135 struct zone *zone;
Johannes Weiner241994ed2015-02-11 15:26:06 -08003136retry:
Keika Kobayashi873b4772008-07-25 01:48:52 -07003137 delayacct_freepages_start();
3138
Johannes Weinerb5ead352019-11-30 17:55:40 -08003139 if (!cgroup_reclaim(sc))
Mel Gorman7cc30fc2016-07-28 15:46:59 -07003140 __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07003142 do {
Anton Vorontsov70ddf632013-04-29 15:08:31 -07003143 vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup,
3144 sc->priority);
Balbir Singh66e17072008-02-07 00:13:56 -08003145 sc->nr_scanned = 0;
Michal Hocko0a0337e2016-05-20 16:57:00 -07003146 shrink_zones(zonelist, sc);
Mel Gormane0c23272011-10-31 17:09:33 -07003147
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07003148 if (sc->nr_reclaimed >= sc->nr_to_reclaim)
Johannes Weiner0b064962014-08-06 16:06:12 -07003149 break;
3150
3151 if (sc->compaction_ready)
3152 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
3154 /*
Minchan Kim0e50ce32013-02-22 16:35:37 -08003155 * If we're getting trouble reclaiming, start doing
3156 * writepage even in laptop mode.
3157 */
3158 if (sc->priority < DEF_PRIORITY - 2)
3159 sc->may_writepage = 1;
Johannes Weiner0b064962014-08-06 16:06:12 -07003160 } while (--sc->priority >= 0);
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07003161
Johannes Weiner2a2e4882017-05-03 14:55:03 -07003162 last_pgdat = NULL;
3163 for_each_zone_zonelist_nodemask(zone, z, zonelist, sc->reclaim_idx,
3164 sc->nodemask) {
3165 if (zone->zone_pgdat == last_pgdat)
3166 continue;
3167 last_pgdat = zone->zone_pgdat;
Johannes Weiner1b051172019-11-30 17:55:52 -08003168
Johannes Weiner2a2e4882017-05-03 14:55:03 -07003169 snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat);
Johannes Weiner1b051172019-11-30 17:55:52 -08003170
3171 if (cgroup_reclaim(sc)) {
3172 struct lruvec *lruvec;
3173
3174 lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup,
3175 zone->zone_pgdat);
3176 clear_bit(LRUVEC_CONGESTED, &lruvec->flags);
3177 }
Johannes Weiner2a2e4882017-05-03 14:55:03 -07003178 }
3179
Keika Kobayashi873b4772008-07-25 01:48:52 -07003180 delayacct_freepages_end();
3181
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07003182 if (sc->nr_reclaimed)
3183 return sc->nr_reclaimed;
3184
Mel Gorman0cee34f2012-01-12 17:19:49 -08003185 /* Aborted reclaim to try compaction? don't OOM, then */
Johannes Weiner0b064962014-08-06 16:06:12 -07003186 if (sc->compaction_ready)
Mel Gorman73350842012-01-12 17:19:33 -08003187 return 1;
3188
Johannes Weinerb91ac372019-11-30 17:56:02 -08003189 /*
3190 * We make inactive:active ratio decisions based on the node's
3191 * composition of memory, but a restrictive reclaim_idx or a
3192 * memory.low cgroup setting can exempt large amounts of
3193 * memory from reclaim. Neither of which are very common, so
3194 * instead of doing costly eligibility calculations of the
3195 * entire cgroup subtree up front, we assume the estimates are
3196 * good, and retry with forcible deactivation if that fails.
3197 */
3198 if (sc->skipped_deactivate) {
3199 sc->priority = initial_priority;
3200 sc->force_deactivate = 1;
3201 sc->skipped_deactivate = 0;
3202 goto retry;
3203 }
3204
Johannes Weiner241994ed2015-02-11 15:26:06 -08003205 /* Untapped cgroup reserves? Don't OOM, retry. */
Yisheng Xied6622f62017-05-03 14:53:57 -07003206 if (sc->memcg_low_skipped) {
Johannes Weiner241994ed2015-02-11 15:26:06 -08003207 sc->priority = initial_priority;
Johannes Weinerb91ac372019-11-30 17:56:02 -08003208 sc->force_deactivate = 0;
Yisheng Xied6622f62017-05-03 14:53:57 -07003209 sc->memcg_low_reclaim = 1;
3210 sc->memcg_low_skipped = 0;
Johannes Weiner241994ed2015-02-11 15:26:06 -08003211 goto retry;
3212 }
3213
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07003214 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215}
3216
Johannes Weinerc73322d2017-05-03 14:51:51 -07003217static bool allow_direct_reclaim(pg_data_t *pgdat)
Mel Gorman55150612012-07-31 16:44:35 -07003218{
3219 struct zone *zone;
3220 unsigned long pfmemalloc_reserve = 0;
3221 unsigned long free_pages = 0;
3222 int i;
3223 bool wmark_ok;
3224
Johannes Weinerc73322d2017-05-03 14:51:51 -07003225 if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
3226 return true;
3227
Mel Gorman55150612012-07-31 16:44:35 -07003228 for (i = 0; i <= ZONE_NORMAL; i++) {
3229 zone = &pgdat->node_zones[i];
Johannes Weinerd450abd82017-05-03 14:51:54 -07003230 if (!managed_zone(zone))
3231 continue;
3232
3233 if (!zone_reclaimable_pages(zone))
Mel Gorman675becc2014-06-04 16:07:35 -07003234 continue;
3235
Mel Gorman55150612012-07-31 16:44:35 -07003236 pfmemalloc_reserve += min_wmark_pages(zone);
3237 free_pages += zone_page_state(zone, NR_FREE_PAGES);
3238 }
3239
Mel Gorman675becc2014-06-04 16:07:35 -07003240 /* If there are no reserves (unexpected config) then do not throttle */
3241 if (!pfmemalloc_reserve)
3242 return true;
3243
Mel Gorman55150612012-07-31 16:44:35 -07003244 wmark_ok = free_pages > pfmemalloc_reserve / 2;
3245
3246 /* kswapd must be awake if processes are being throttled */
3247 if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) {
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003248 if (READ_ONCE(pgdat->kswapd_highest_zoneidx) > ZONE_NORMAL)
3249 WRITE_ONCE(pgdat->kswapd_highest_zoneidx, ZONE_NORMAL);
Qian Cai5644e1fb2020-04-01 21:10:12 -07003250
Mel Gorman55150612012-07-31 16:44:35 -07003251 wake_up_interruptible(&pgdat->kswapd_wait);
3252 }
3253
3254 return wmark_ok;
3255}
3256
3257/*
3258 * Throttle direct reclaimers if backing storage is backed by the network
3259 * and the PFMEMALLOC reserve for the preferred node is getting dangerously
3260 * depleted. kswapd will continue to make progress and wake the processes
Mel Gorman50694c22012-11-26 16:29:48 -08003261 * when the low watermark is reached.
3262 *
3263 * Returns true if a fatal signal was delivered during throttling. If this
3264 * happens, the page allocator should not consider triggering the OOM killer.
Mel Gorman55150612012-07-31 16:44:35 -07003265 */
Mel Gorman50694c22012-11-26 16:29:48 -08003266static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist,
Mel Gorman55150612012-07-31 16:44:35 -07003267 nodemask_t *nodemask)
3268{
Mel Gorman675becc2014-06-04 16:07:35 -07003269 struct zoneref *z;
Mel Gorman55150612012-07-31 16:44:35 -07003270 struct zone *zone;
Mel Gorman675becc2014-06-04 16:07:35 -07003271 pg_data_t *pgdat = NULL;
Mel Gorman55150612012-07-31 16:44:35 -07003272
3273 /*
3274 * Kernel threads should not be throttled as they may be indirectly
3275 * responsible for cleaning pages necessary for reclaim to make forward
3276 * progress. kjournald for example may enter direct reclaim while
3277 * committing a transaction where throttling it could forcing other
3278 * processes to block on log_wait_commit().
3279 */
3280 if (current->flags & PF_KTHREAD)
Mel Gorman50694c22012-11-26 16:29:48 -08003281 goto out;
3282
3283 /*
3284 * If a fatal signal is pending, this process should not throttle.
3285 * It should return quickly so it can exit and free its memory
3286 */
3287 if (fatal_signal_pending(current))
3288 goto out;
Mel Gorman55150612012-07-31 16:44:35 -07003289
Mel Gorman675becc2014-06-04 16:07:35 -07003290 /*
3291 * Check if the pfmemalloc reserves are ok by finding the first node
3292 * with a usable ZONE_NORMAL or lower zone. The expectation is that
3293 * GFP_KERNEL will be required for allocating network buffers when
3294 * swapping over the network so ZONE_HIGHMEM is unusable.
3295 *
3296 * Throttling is based on the first usable node and throttled processes
3297 * wait on a queue until kswapd makes progress and wakes them. There
3298 * is an affinity then between processes waking up and where reclaim
3299 * progress has been made assuming the process wakes on the same node.
3300 * More importantly, processes running on remote nodes will not compete
3301 * for remote pfmemalloc reserves and processes on different nodes
3302 * should make reasonable progress.
3303 */
3304 for_each_zone_zonelist_nodemask(zone, z, zonelist,
Michael S. Tsirkin17636fa2015-01-26 12:58:41 -08003305 gfp_zone(gfp_mask), nodemask) {
Mel Gorman675becc2014-06-04 16:07:35 -07003306 if (zone_idx(zone) > ZONE_NORMAL)
3307 continue;
3308
3309 /* Throttle based on the first usable node */
3310 pgdat = zone->zone_pgdat;
Johannes Weinerc73322d2017-05-03 14:51:51 -07003311 if (allow_direct_reclaim(pgdat))
Mel Gorman675becc2014-06-04 16:07:35 -07003312 goto out;
3313 break;
3314 }
3315
3316 /* If no zone was usable by the allocation flags then do not throttle */
3317 if (!pgdat)
Mel Gorman50694c22012-11-26 16:29:48 -08003318 goto out;
Mel Gorman55150612012-07-31 16:44:35 -07003319
Mel Gorman68243e72012-07-31 16:44:39 -07003320 /* Account for the throttling */
3321 count_vm_event(PGSCAN_DIRECT_THROTTLE);
3322
Mel Gorman55150612012-07-31 16:44:35 -07003323 /*
3324 * If the caller cannot enter the filesystem, it's possible that it
3325 * is due to the caller holding an FS lock or performing a journal
3326 * transaction in the case of a filesystem like ext[3|4]. In this case,
3327 * it is not safe to block on pfmemalloc_wait as kswapd could be
3328 * blocked waiting on the same lock. Instead, throttle for up to a
3329 * second before continuing.
3330 */
3331 if (!(gfp_mask & __GFP_FS)) {
3332 wait_event_interruptible_timeout(pgdat->pfmemalloc_wait,
Johannes Weinerc73322d2017-05-03 14:51:51 -07003333 allow_direct_reclaim(pgdat), HZ);
Mel Gorman50694c22012-11-26 16:29:48 -08003334
3335 goto check_pending;
Mel Gorman55150612012-07-31 16:44:35 -07003336 }
3337
3338 /* Throttle until kswapd wakes the process */
3339 wait_event_killable(zone->zone_pgdat->pfmemalloc_wait,
Johannes Weinerc73322d2017-05-03 14:51:51 -07003340 allow_direct_reclaim(pgdat));
Mel Gorman50694c22012-11-26 16:29:48 -08003341
3342check_pending:
3343 if (fatal_signal_pending(current))
3344 return true;
3345
3346out:
3347 return false;
Mel Gorman55150612012-07-31 16:44:35 -07003348}
3349
Mel Gormandac1d272008-04-28 02:12:12 -07003350unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
KAMEZAWA Hiroyuki327c0e92009-03-31 15:23:31 -07003351 gfp_t gfp_mask, nodemask_t *nodemask)
Balbir Singh66e17072008-02-07 00:13:56 -08003352{
Mel Gorman33906bc2010-08-09 17:19:16 -07003353 unsigned long nr_reclaimed;
Balbir Singh66e17072008-02-07 00:13:56 -08003354 struct scan_control sc = {
KOSAKI Motohiro22fba332009-12-14 17:59:10 -08003355 .nr_to_reclaim = SWAP_CLUSTER_MAX,
Nick Desaulniersf2f43e52017-07-06 15:36:50 -07003356 .gfp_mask = current_gfp_context(gfp_mask),
Mel Gormanb2e18752016-07-28 15:45:37 -07003357 .reclaim_idx = gfp_zone(gfp_mask),
Johannes Weineree814fe2014-08-06 16:06:19 -07003358 .order = order,
3359 .nodemask = nodemask,
3360 .priority = DEF_PRIORITY,
3361 .may_writepage = !laptop_mode,
Johannes Weinera6dc60f82009-03-31 15:19:30 -07003362 .may_unmap = 1,
KOSAKI Motohiro2e2e4252009-04-21 12:24:57 -07003363 .may_swap = 1,
Balbir Singh66e17072008-02-07 00:13:56 -08003364 };
3365
Mel Gorman55150612012-07-31 16:44:35 -07003366 /*
Greg Thelenbb451fd2018-08-17 15:45:19 -07003367 * scan_control uses s8 fields for order, priority, and reclaim_idx.
3368 * Confirm they are large enough for max values.
3369 */
3370 BUILD_BUG_ON(MAX_ORDER > S8_MAX);
3371 BUILD_BUG_ON(DEF_PRIORITY > S8_MAX);
3372 BUILD_BUG_ON(MAX_NR_ZONES > S8_MAX);
3373
3374 /*
Mel Gorman50694c22012-11-26 16:29:48 -08003375 * Do not enter reclaim if fatal signal was delivered while throttled.
3376 * 1 is returned so that the page allocator does not OOM kill at this
3377 * point.
Mel Gorman55150612012-07-31 16:44:35 -07003378 */
Nick Desaulniersf2f43e52017-07-06 15:36:50 -07003379 if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask))
Mel Gorman55150612012-07-31 16:44:35 -07003380 return 1;
3381
Andrew Morton1732d2b012019-07-16 16:26:15 -07003382 set_task_reclaim_state(current, &sc.reclaim_state);
Yafang Shao3481c372019-05-13 17:19:14 -07003383 trace_mm_vmscan_direct_reclaim_begin(order, sc.gfp_mask);
Mel Gorman33906bc2010-08-09 17:19:16 -07003384
Vladimir Davydov3115cd92014-04-03 14:47:22 -07003385 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
Mel Gorman33906bc2010-08-09 17:19:16 -07003386
3387 trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
Andrew Morton1732d2b012019-07-16 16:26:15 -07003388 set_task_reclaim_state(current, NULL);
Mel Gorman33906bc2010-08-09 17:19:16 -07003389
3390 return nr_reclaimed;
Balbir Singh66e17072008-02-07 00:13:56 -08003391}
3392
Andrew Mortonc255a452012-07-31 16:43:02 -07003393#ifdef CONFIG_MEMCG
Balbir Singh66e17072008-02-07 00:13:56 -08003394
Michal Hockod2e5fb92019-08-30 16:04:50 -07003395/* Only used by soft limit reclaim. Do not reuse for anything else. */
Mel Gormana9dd0a82016-07-28 15:46:02 -07003396unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg,
Balbir Singh4e416952009-09-23 15:56:39 -07003397 gfp_t gfp_mask, bool noswap,
Mel Gormanef8f2322016-07-28 15:46:05 -07003398 pg_data_t *pgdat,
Ying Han0ae5e892011-05-26 16:25:25 -07003399 unsigned long *nr_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07003400{
Johannes Weinerafaf07a2019-11-30 17:55:46 -08003401 struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat);
Balbir Singh4e416952009-09-23 15:56:39 -07003402 struct scan_control sc = {
KOSAKI Motohirob8f5c562010-08-10 18:03:02 -07003403 .nr_to_reclaim = SWAP_CLUSTER_MAX,
Johannes Weineree814fe2014-08-06 16:06:19 -07003404 .target_mem_cgroup = memcg,
Balbir Singh4e416952009-09-23 15:56:39 -07003405 .may_writepage = !laptop_mode,
3406 .may_unmap = 1,
Mel Gormanb2e18752016-07-28 15:45:37 -07003407 .reclaim_idx = MAX_NR_ZONES - 1,
Balbir Singh4e416952009-09-23 15:56:39 -07003408 .may_swap = !noswap,
Balbir Singh4e416952009-09-23 15:56:39 -07003409 };
Ying Han0ae5e892011-05-26 16:25:25 -07003410
Michal Hockod2e5fb92019-08-30 16:04:50 -07003411 WARN_ON_ONCE(!current->reclaim_state);
3412
Balbir Singh4e416952009-09-23 15:56:39 -07003413 sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
3414 (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003415
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07003416 trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order,
Yafang Shao3481c372019-05-13 17:19:14 -07003417 sc.gfp_mask);
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003418
Balbir Singh4e416952009-09-23 15:56:39 -07003419 /*
3420 * NOTE: Although we can get the priority field, using it
3421 * here is not a good idea, since it limits the pages we can scan.
Mel Gormana9dd0a82016-07-28 15:46:02 -07003422 * if we don't reclaim here, the shrink_node from balance_pgdat
Balbir Singh4e416952009-09-23 15:56:39 -07003423 * will pick up pages from other mem cgroup's as well. We hack
3424 * the priority and make it zero.
3425 */
Johannes Weinerafaf07a2019-11-30 17:55:46 -08003426 shrink_lruvec(lruvec, &sc);
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003427
3428 trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed);
3429
Ying Han0ae5e892011-05-26 16:25:25 -07003430 *nr_scanned = sc.nr_scanned;
Yafang Shao0308f7c2019-07-16 16:26:12 -07003431
Balbir Singh4e416952009-09-23 15:56:39 -07003432 return sc.nr_reclaimed;
3433}
3434
Johannes Weiner72835c82012-01-12 17:18:32 -08003435unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003436 unsigned long nr_pages,
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003437 gfp_t gfp_mask,
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003438 bool may_swap)
Balbir Singh66e17072008-02-07 00:13:56 -08003439{
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003440 unsigned long nr_reclaimed;
Vlastimil Babka499118e2017-05-08 15:59:50 -07003441 unsigned int noreclaim_flag;
Balbir Singh66e17072008-02-07 00:13:56 -08003442 struct scan_control sc = {
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003443 .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
Michal Hocko7dea19f2017-05-03 14:53:15 -07003444 .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) |
Johannes Weineree814fe2014-08-06 16:06:19 -07003445 (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK),
Mel Gormanb2e18752016-07-28 15:45:37 -07003446 .reclaim_idx = MAX_NR_ZONES - 1,
Johannes Weineree814fe2014-08-06 16:06:19 -07003447 .target_mem_cgroup = memcg,
3448 .priority = DEF_PRIORITY,
Balbir Singh66e17072008-02-07 00:13:56 -08003449 .may_writepage = !laptop_mode,
Johannes Weinera6dc60f82009-03-31 15:19:30 -07003450 .may_unmap = 1,
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003451 .may_swap = may_swap,
Ying Hana09ed5e2011-05-24 17:12:26 -07003452 };
Shakeel Buttfa40d1e2019-11-30 17:50:16 -08003453 /*
3454 * Traverse the ZONELIST_FALLBACK zonelist of the current node to put
3455 * equal pressure on all the nodes. This is based on the assumption that
3456 * the reclaim does not bail out early.
3457 */
3458 struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
Balbir Singh66e17072008-02-07 00:13:56 -08003459
Andrew Morton1732d2b012019-07-16 16:26:15 -07003460 set_task_reclaim_state(current, &sc.reclaim_state);
Yafang Shao3481c372019-05-13 17:19:14 -07003461 trace_mm_vmscan_memcg_reclaim_begin(0, sc.gfp_mask);
Vlastimil Babka499118e2017-05-08 15:59:50 -07003462 noreclaim_flag = memalloc_noreclaim_save();
Johannes Weinereb414682018-10-26 15:06:27 -07003463
Vladimir Davydov3115cd92014-04-03 14:47:22 -07003464 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
Johannes Weinereb414682018-10-26 15:06:27 -07003465
Vlastimil Babka499118e2017-05-08 15:59:50 -07003466 memalloc_noreclaim_restore(noreclaim_flag);
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003467 trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
Andrew Morton1732d2b012019-07-16 16:26:15 -07003468 set_task_reclaim_state(current, NULL);
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07003469
3470 return nr_reclaimed;
Balbir Singh66e17072008-02-07 00:13:56 -08003471}
3472#endif
3473
Mel Gorman1d82de62016-07-28 15:45:43 -07003474static void age_active_anon(struct pglist_data *pgdat,
Mel Gormanef8f2322016-07-28 15:46:05 -07003475 struct scan_control *sc)
Johannes Weinerf16015f2012-01-12 17:17:52 -08003476{
Johannes Weinerb95a2f22012-01-12 17:18:06 -08003477 struct mem_cgroup *memcg;
Johannes Weinerb91ac372019-11-30 17:56:02 -08003478 struct lruvec *lruvec;
Johannes Weinerf16015f2012-01-12 17:17:52 -08003479
Johannes Weinerb95a2f22012-01-12 17:18:06 -08003480 if (!total_swap_pages)
3481 return;
3482
Johannes Weinerb91ac372019-11-30 17:56:02 -08003483 lruvec = mem_cgroup_lruvec(NULL, pgdat);
3484 if (!inactive_is_low(lruvec, LRU_INACTIVE_ANON))
3485 return;
3486
Johannes Weinerb95a2f22012-01-12 17:18:06 -08003487 memcg = mem_cgroup_iter(NULL, NULL, NULL);
3488 do {
Johannes Weinerb91ac372019-11-30 17:56:02 -08003489 lruvec = mem_cgroup_lruvec(memcg, pgdat);
3490 shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
3491 sc, LRU_ACTIVE_ANON);
Johannes Weinerb95a2f22012-01-12 17:18:06 -08003492 memcg = mem_cgroup_iter(NULL, memcg, NULL);
3493 } while (memcg);
Johannes Weinerf16015f2012-01-12 17:17:52 -08003494}
3495
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003496static bool pgdat_watermark_boosted(pg_data_t *pgdat, int highest_zoneidx)
Mel Gorman1c308442018-12-28 00:35:52 -08003497{
3498 int i;
3499 struct zone *zone;
3500
3501 /*
3502 * Check for watermark boosts top-down as the higher zones
3503 * are more likely to be boosted. Both watermarks and boosts
Randy Dunlap1eba09c2020-08-11 18:33:26 -07003504 * should not be checked at the same time as reclaim would
Mel Gorman1c308442018-12-28 00:35:52 -08003505 * start prematurely when there is no boosting and a lower
3506 * zone is balanced.
3507 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003508 for (i = highest_zoneidx; i >= 0; i--) {
Mel Gorman1c308442018-12-28 00:35:52 -08003509 zone = pgdat->node_zones + i;
3510 if (!managed_zone(zone))
3511 continue;
3512
3513 if (zone->watermark_boost)
3514 return true;
3515 }
3516
3517 return false;
3518}
3519
Mel Gormane716f2e2017-05-03 14:53:45 -07003520/*
3521 * Returns true if there is an eligible zone balanced for the request order
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003522 * and highest_zoneidx
Mel Gormane716f2e2017-05-03 14:53:45 -07003523 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003524static bool pgdat_balanced(pg_data_t *pgdat, int order, int highest_zoneidx)
Johannes Weiner60cefed2012-11-29 13:54:23 -08003525{
Mel Gormane716f2e2017-05-03 14:53:45 -07003526 int i;
3527 unsigned long mark = -1;
3528 struct zone *zone;
Johannes Weiner60cefed2012-11-29 13:54:23 -08003529
Mel Gorman1c308442018-12-28 00:35:52 -08003530 /*
3531 * Check watermarks bottom-up as lower zones are more likely to
3532 * meet watermarks.
3533 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003534 for (i = 0; i <= highest_zoneidx; i++) {
Mel Gormane716f2e2017-05-03 14:53:45 -07003535 zone = pgdat->node_zones + i;
Mel Gorman6256c6b2016-07-28 15:45:56 -07003536
Mel Gormane716f2e2017-05-03 14:53:45 -07003537 if (!managed_zone(zone))
3538 continue;
3539
3540 mark = high_wmark_pages(zone);
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003541 if (zone_watermark_ok_safe(zone, order, mark, highest_zoneidx))
Mel Gormane716f2e2017-05-03 14:53:45 -07003542 return true;
3543 }
3544
3545 /*
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003546 * If a node has no populated zone within highest_zoneidx, it does not
Mel Gormane716f2e2017-05-03 14:53:45 -07003547 * need balancing by definition. This can happen if a zone-restricted
3548 * allocation tries to wake a remote kswapd.
3549 */
3550 if (mark == -1)
3551 return true;
3552
3553 return false;
Johannes Weiner60cefed2012-11-29 13:54:23 -08003554}
3555
Mel Gorman631b6e02017-05-03 14:53:41 -07003556/* Clear pgdat state for congested, dirty or under writeback. */
3557static void clear_pgdat_congested(pg_data_t *pgdat)
3558{
Johannes Weiner1b051172019-11-30 17:55:52 -08003559 struct lruvec *lruvec = mem_cgroup_lruvec(NULL, pgdat);
3560
3561 clear_bit(LRUVEC_CONGESTED, &lruvec->flags);
Mel Gorman631b6e02017-05-03 14:53:41 -07003562 clear_bit(PGDAT_DIRTY, &pgdat->flags);
3563 clear_bit(PGDAT_WRITEBACK, &pgdat->flags);
3564}
3565
Mel Gorman1741c872011-01-13 15:46:21 -08003566/*
Mel Gorman55150612012-07-31 16:44:35 -07003567 * Prepare kswapd for sleeping. This verifies that there are no processes
3568 * waiting in throttle_direct_reclaim() and that watermarks have been met.
3569 *
3570 * Returns true if kswapd is ready to sleep
3571 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003572static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order,
3573 int highest_zoneidx)
Mel Gormanf50de2d2009-12-14 17:58:53 -08003574{
Mel Gorman55150612012-07-31 16:44:35 -07003575 /*
Vlastimil Babka9e5e3662015-01-08 14:32:40 -08003576 * The throttled processes are normally woken up in balance_pgdat() as
Johannes Weinerc73322d2017-05-03 14:51:51 -07003577 * soon as allow_direct_reclaim() is true. But there is a potential
Vlastimil Babka9e5e3662015-01-08 14:32:40 -08003578 * race between when kswapd checks the watermarks and a process gets
3579 * throttled. There is also a potential race if processes get
3580 * throttled, kswapd wakes, a large process exits thereby balancing the
3581 * zones, which causes kswapd to exit balance_pgdat() before reaching
3582 * the wake up checks. If kswapd is going to sleep, no process should
3583 * be sleeping on pfmemalloc_wait, so wake them now if necessary. If
3584 * the wake up is premature, processes will wake kswapd and get
3585 * throttled again. The difference from wake ups in balance_pgdat() is
3586 * that here we are under prepare_to_wait().
Mel Gorman55150612012-07-31 16:44:35 -07003587 */
Vlastimil Babka9e5e3662015-01-08 14:32:40 -08003588 if (waitqueue_active(&pgdat->pfmemalloc_wait))
3589 wake_up_all(&pgdat->pfmemalloc_wait);
Mel Gormanf50de2d2009-12-14 17:58:53 -08003590
Johannes Weinerc73322d2017-05-03 14:51:51 -07003591 /* Hopeless node, leave it to direct reclaim */
3592 if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
3593 return true;
3594
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003595 if (pgdat_balanced(pgdat, order, highest_zoneidx)) {
Mel Gormane716f2e2017-05-03 14:53:45 -07003596 clear_pgdat_congested(pgdat);
3597 return true;
Mel Gorman1d82de62016-07-28 15:45:43 -07003598 }
3599
Shantanu Goel333b0a42017-05-03 14:53:38 -07003600 return false;
Mel Gormanf50de2d2009-12-14 17:58:53 -08003601}
3602
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603/*
Mel Gorman1d82de62016-07-28 15:45:43 -07003604 * kswapd shrinks a node of pages that are at or below the highest usable
3605 * zone that is currently unbalanced.
Mel Gormanb8e83b92013-07-03 15:01:45 -07003606 *
3607 * Returns true if kswapd scanned at least the requested number of pages to
Mel Gorman283aba92013-07-03 15:01:51 -07003608 * reclaim or if the lack of progress was due to pages under writeback.
3609 * This is used to determine if the scanning priority needs to be raised.
Mel Gorman75485362013-07-03 15:01:42 -07003610 */
Mel Gorman1d82de62016-07-28 15:45:43 -07003611static bool kswapd_shrink_node(pg_data_t *pgdat,
Vlastimil Babkaaccf6242016-03-17 14:18:15 -07003612 struct scan_control *sc)
Mel Gorman75485362013-07-03 15:01:42 -07003613{
Mel Gorman1d82de62016-07-28 15:45:43 -07003614 struct zone *zone;
3615 int z;
Mel Gorman75485362013-07-03 15:01:42 -07003616
Mel Gorman1d82de62016-07-28 15:45:43 -07003617 /* Reclaim a number of pages proportional to the number of zones */
3618 sc->nr_to_reclaim = 0;
Mel Gorman970a39a2016-07-28 15:46:35 -07003619 for (z = 0; z <= sc->reclaim_idx; z++) {
Mel Gorman1d82de62016-07-28 15:45:43 -07003620 zone = pgdat->node_zones + z;
Mel Gorman6aa303d2016-09-01 16:14:55 -07003621 if (!managed_zone(zone))
Mel Gorman1d82de62016-07-28 15:45:43 -07003622 continue;
Mel Gorman7c954f62013-07-03 15:01:54 -07003623
Mel Gorman1d82de62016-07-28 15:45:43 -07003624 sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX);
Mel Gorman7c954f62013-07-03 15:01:54 -07003625 }
3626
Mel Gorman1d82de62016-07-28 15:45:43 -07003627 /*
3628 * Historically care was taken to put equal pressure on all zones but
3629 * now pressure is applied based on node LRU order.
3630 */
Mel Gorman970a39a2016-07-28 15:46:35 -07003631 shrink_node(pgdat, sc);
Mel Gorman1d82de62016-07-28 15:45:43 -07003632
3633 /*
3634 * Fragmentation may mean that the system cannot be rebalanced for
3635 * high-order allocations. If twice the allocation size has been
3636 * reclaimed then recheck watermarks only at order-0 to prevent
3637 * excessive reclaim. Assume that a process requested a high-order
3638 * can direct reclaim/compact.
3639 */
Vlastimil Babka9861a622016-10-07 16:57:53 -07003640 if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order))
Mel Gorman1d82de62016-07-28 15:45:43 -07003641 sc->order = 0;
3642
Mel Gormanb8e83b92013-07-03 15:01:45 -07003643 return sc->nr_scanned >= sc->nr_to_reclaim;
Mel Gorman75485362013-07-03 15:01:42 -07003644}
3645
3646/*
Mel Gorman1d82de62016-07-28 15:45:43 -07003647 * For kswapd, balance_pgdat() will reclaim pages across a node from zones
3648 * that are eligible for use by the caller until at least one zone is
3649 * balanced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 *
Mel Gorman1d82de62016-07-28 15:45:43 -07003651 * Returns the order kswapd finished reclaiming at.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 *
3653 * kswapd scans the zones in the highmem->normal->dma direction. It skips
Mel Gorman41858962009-06-16 15:32:12 -07003654 * zones which have free_pages > high_wmark_pages(zone), but once a zone is
Wei Yang8bb4e7a2019-03-05 15:46:22 -08003655 * found to have free_pages <= high_wmark_pages(zone), any page in that zone
Mel Gorman1d82de62016-07-28 15:45:43 -07003656 * or lower is eligible for reclaim until at least one usable zone is
3657 * balanced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003659static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 int i;
Andrew Morton0608f432013-09-24 15:27:41 -07003662 unsigned long nr_soft_reclaimed;
3663 unsigned long nr_soft_scanned;
Johannes Weinereb414682018-10-26 15:06:27 -07003664 unsigned long pflags;
Mel Gorman1c308442018-12-28 00:35:52 -08003665 unsigned long nr_boost_reclaim;
3666 unsigned long zone_boosts[MAX_NR_ZONES] = { 0, };
3667 bool boosted;
Mel Gorman1d82de62016-07-28 15:45:43 -07003668 struct zone *zone;
Andrew Morton179e9632006-03-22 00:08:18 -08003669 struct scan_control sc = {
3670 .gfp_mask = GFP_KERNEL,
Johannes Weineree814fe2014-08-06 16:06:19 -07003671 .order = order,
Johannes Weinera6dc60f82009-03-31 15:19:30 -07003672 .may_unmap = 1,
Andrew Morton179e9632006-03-22 00:08:18 -08003673 };
Omar Sandoval93781322018-06-07 17:07:02 -07003674
Andrew Morton1732d2b012019-07-16 16:26:15 -07003675 set_task_reclaim_state(current, &sc.reclaim_state);
Johannes Weinereb414682018-10-26 15:06:27 -07003676 psi_memstall_enter(&pflags);
Omar Sandoval93781322018-06-07 17:07:02 -07003677 __fs_reclaim_acquire();
3678
Christoph Lameterf8891e52006-06-30 01:55:45 -07003679 count_vm_event(PAGEOUTRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
Mel Gorman1c308442018-12-28 00:35:52 -08003681 /*
3682 * Account for the reclaim boost. Note that the zone boost is left in
3683 * place so that parallel allocations that are near the watermark will
3684 * stall or direct reclaim until kswapd is finished.
3685 */
3686 nr_boost_reclaim = 0;
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003687 for (i = 0; i <= highest_zoneidx; i++) {
Mel Gorman1c308442018-12-28 00:35:52 -08003688 zone = pgdat->node_zones + i;
3689 if (!managed_zone(zone))
3690 continue;
3691
3692 nr_boost_reclaim += zone->watermark_boost;
3693 zone_boosts[i] = zone->watermark_boost;
3694 }
3695 boosted = nr_boost_reclaim;
3696
3697restart:
3698 sc.priority = DEF_PRIORITY;
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07003699 do {
Johannes Weinerc73322d2017-05-03 14:51:51 -07003700 unsigned long nr_reclaimed = sc.nr_reclaimed;
Mel Gormanb8e83b92013-07-03 15:01:45 -07003701 bool raise_priority = true;
Mel Gorman1c308442018-12-28 00:35:52 -08003702 bool balanced;
Omar Sandoval93781322018-06-07 17:07:02 -07003703 bool ret;
Mel Gormanb8e83b92013-07-03 15:01:45 -07003704
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003705 sc.reclaim_idx = highest_zoneidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706
Mel Gorman86c79f62016-07-28 15:45:59 -07003707 /*
Mel Gorman84c7a772016-07-28 15:46:44 -07003708 * If the number of buffer_heads exceeds the maximum allowed
3709 * then consider reclaiming from all zones. This has a dual
3710 * purpose -- on 64-bit systems it is expected that
3711 * buffer_heads are stripped during active rotation. On 32-bit
3712 * systems, highmem pages can pin lowmem memory and shrinking
3713 * buffers can relieve lowmem pressure. Reclaim may still not
3714 * go ahead if all eligible zones for the original allocation
3715 * request are balanced to avoid excessive reclaim from kswapd.
Mel Gorman86c79f62016-07-28 15:45:59 -07003716 */
3717 if (buffer_heads_over_limit) {
3718 for (i = MAX_NR_ZONES - 1; i >= 0; i--) {
3719 zone = pgdat->node_zones + i;
Mel Gorman6aa303d2016-09-01 16:14:55 -07003720 if (!managed_zone(zone))
Mel Gorman86c79f62016-07-28 15:45:59 -07003721 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722
Mel Gorman970a39a2016-07-28 15:46:35 -07003723 sc.reclaim_idx = i;
Andrew Mortone1dbeda2006-12-06 20:32:01 -08003724 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 }
Zlatko Calusicdafcb732013-02-22 16:32:34 -08003727
Mel Gorman86c79f62016-07-28 15:45:59 -07003728 /*
Mel Gorman1c308442018-12-28 00:35:52 -08003729 * If the pgdat is imbalanced then ignore boosting and preserve
3730 * the watermarks for a later time and restart. Note that the
3731 * zone watermarks will be still reset at the end of balancing
3732 * on the grounds that the normal reclaim should be enough to
3733 * re-evaluate if boosting is required when kswapd next wakes.
Mel Gorman86c79f62016-07-28 15:45:59 -07003734 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003735 balanced = pgdat_balanced(pgdat, sc.order, highest_zoneidx);
Mel Gorman1c308442018-12-28 00:35:52 -08003736 if (!balanced && nr_boost_reclaim) {
3737 nr_boost_reclaim = 0;
3738 goto restart;
3739 }
3740
3741 /*
3742 * If boosting is not active then only reclaim if there are no
3743 * eligible zones. Note that sc.reclaim_idx is not used as
3744 * buffer_heads_over_limit may have adjusted it.
3745 */
3746 if (!nr_boost_reclaim && balanced)
Mel Gormane716f2e2017-05-03 14:53:45 -07003747 goto out;
Andrew Mortone1dbeda2006-12-06 20:32:01 -08003748
Mel Gorman1c308442018-12-28 00:35:52 -08003749 /* Limit the priority of boosting to avoid reclaim writeback */
3750 if (nr_boost_reclaim && sc.priority == DEF_PRIORITY - 2)
3751 raise_priority = false;
3752
3753 /*
3754 * Do not writeback or swap pages for boosted reclaim. The
3755 * intent is to relieve pressure not issue sub-optimal IO
3756 * from reclaim context. If no pages are reclaimed, the
3757 * reclaim will be aborted.
3758 */
3759 sc.may_writepage = !laptop_mode && !nr_boost_reclaim;
3760 sc.may_swap = !nr_boost_reclaim;
Mel Gorman1c308442018-12-28 00:35:52 -08003761
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 /*
Mel Gorman1d82de62016-07-28 15:45:43 -07003763 * Do some background aging of the anon list, to give
3764 * pages a chance to be referenced before reclaiming. All
3765 * pages are rotated regardless of classzone as this is
3766 * about consistent aging.
3767 */
Mel Gormanef8f2322016-07-28 15:46:05 -07003768 age_active_anon(pgdat, &sc);
Mel Gorman1d82de62016-07-28 15:45:43 -07003769
3770 /*
Mel Gormanb7ea3c42013-07-03 15:01:53 -07003771 * If we're getting trouble reclaiming, start doing writepage
3772 * even in laptop mode.
3773 */
Johannes Weiner047d72c2017-05-03 14:51:57 -07003774 if (sc.priority < DEF_PRIORITY - 2)
Mel Gormanb7ea3c42013-07-03 15:01:53 -07003775 sc.may_writepage = 1;
3776
Mel Gorman1d82de62016-07-28 15:45:43 -07003777 /* Call soft limit reclaim before calling shrink_node. */
3778 sc.nr_scanned = 0;
3779 nr_soft_scanned = 0;
Mel Gormanef8f2322016-07-28 15:46:05 -07003780 nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order,
Mel Gorman1d82de62016-07-28 15:45:43 -07003781 sc.gfp_mask, &nr_soft_scanned);
3782 sc.nr_reclaimed += nr_soft_reclaimed;
3783
Mel Gormanb7ea3c42013-07-03 15:01:53 -07003784 /*
Mel Gorman1d82de62016-07-28 15:45:43 -07003785 * There should be no need to raise the scanning priority if
3786 * enough pages are already being scanned that that high
3787 * watermark would be met at 100% efficiency.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 */
Mel Gorman970a39a2016-07-28 15:46:35 -07003789 if (kswapd_shrink_node(pgdat, &sc))
Mel Gorman1d82de62016-07-28 15:45:43 -07003790 raise_priority = false;
Mel Gorman55150612012-07-31 16:44:35 -07003791
3792 /*
3793 * If the low watermark is met there is no need for processes
3794 * to be throttled on pfmemalloc_wait as they should not be
3795 * able to safely make forward progress. Wake them
3796 */
3797 if (waitqueue_active(&pgdat->pfmemalloc_wait) &&
Johannes Weinerc73322d2017-05-03 14:51:51 -07003798 allow_direct_reclaim(pgdat))
Vlastimil Babkacfc51152015-02-11 15:25:12 -08003799 wake_up_all(&pgdat->pfmemalloc_wait);
Mel Gorman55150612012-07-31 16:44:35 -07003800
Mel Gormanb8e83b92013-07-03 15:01:45 -07003801 /* Check if kswapd should be suspending */
Omar Sandoval93781322018-06-07 17:07:02 -07003802 __fs_reclaim_release();
3803 ret = try_to_freeze();
3804 __fs_reclaim_acquire();
3805 if (ret || kthread_should_stop())
Mel Gormanb8e83b92013-07-03 15:01:45 -07003806 break;
3807
3808 /*
3809 * Raise priority if scanning rate is too low or there was no
3810 * progress in reclaiming pages
3811 */
Johannes Weinerc73322d2017-05-03 14:51:51 -07003812 nr_reclaimed = sc.nr_reclaimed - nr_reclaimed;
Mel Gorman1c308442018-12-28 00:35:52 -08003813 nr_boost_reclaim -= min(nr_boost_reclaim, nr_reclaimed);
3814
3815 /*
3816 * If reclaim made no progress for a boost, stop reclaim as
3817 * IO cannot be queued and it could be an infinite loop in
3818 * extreme circumstances.
3819 */
3820 if (nr_boost_reclaim && !nr_reclaimed)
3821 break;
3822
Johannes Weinerc73322d2017-05-03 14:51:51 -07003823 if (raise_priority || !nr_reclaimed)
Mel Gormanb8e83b92013-07-03 15:01:45 -07003824 sc.priority--;
Mel Gorman1d82de62016-07-28 15:45:43 -07003825 } while (sc.priority >= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
Johannes Weinerc73322d2017-05-03 14:51:51 -07003827 if (!sc.nr_reclaimed)
3828 pgdat->kswapd_failures++;
3829
Mel Gormanb8e83b92013-07-03 15:01:45 -07003830out:
Mel Gorman1c308442018-12-28 00:35:52 -08003831 /* If reclaim was boosted, account for the reclaim done in this pass */
3832 if (boosted) {
3833 unsigned long flags;
3834
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003835 for (i = 0; i <= highest_zoneidx; i++) {
Mel Gorman1c308442018-12-28 00:35:52 -08003836 if (!zone_boosts[i])
3837 continue;
3838
3839 /* Increments are under the zone lock */
3840 zone = pgdat->node_zones + i;
3841 spin_lock_irqsave(&zone->lock, flags);
3842 zone->watermark_boost -= min(zone->watermark_boost, zone_boosts[i]);
3843 spin_unlock_irqrestore(&zone->lock, flags);
3844 }
3845
3846 /*
3847 * As there is now likely space, wakeup kcompact to defragment
3848 * pageblocks.
3849 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003850 wakeup_kcompactd(pgdat, pageblock_order, highest_zoneidx);
Mel Gorman1c308442018-12-28 00:35:52 -08003851 }
3852
Johannes Weiner2a2e4882017-05-03 14:55:03 -07003853 snapshot_refaults(NULL, pgdat);
Omar Sandoval93781322018-06-07 17:07:02 -07003854 __fs_reclaim_release();
Johannes Weinereb414682018-10-26 15:06:27 -07003855 psi_memstall_leave(&pflags);
Andrew Morton1732d2b012019-07-16 16:26:15 -07003856 set_task_reclaim_state(current, NULL);
Yafang Shaoe5ca8072019-07-16 16:26:09 -07003857
Mel Gorman0abdee22011-01-13 15:46:22 -08003858 /*
Mel Gorman1d82de62016-07-28 15:45:43 -07003859 * Return the order kswapd stopped reclaiming at as
3860 * prepare_kswapd_sleep() takes it into account. If another caller
3861 * entered the allocator slow path while kswapd was awake, order will
3862 * remain at the higher level.
Mel Gorman0abdee22011-01-13 15:46:22 -08003863 */
Mel Gorman1d82de62016-07-28 15:45:43 -07003864 return sc.order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865}
3866
Mel Gormane716f2e2017-05-03 14:53:45 -07003867/*
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003868 * The pgdat->kswapd_highest_zoneidx is used to pass the highest zone index to
3869 * be reclaimed by kswapd from the waker. If the value is MAX_NR_ZONES which is
3870 * not a valid index then either kswapd runs for first time or kswapd couldn't
3871 * sleep after previous reclaim attempt (node is still unbalanced). In that
3872 * case return the zone index of the previous kswapd reclaim cycle.
Mel Gormane716f2e2017-05-03 14:53:45 -07003873 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003874static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat,
3875 enum zone_type prev_highest_zoneidx)
Mel Gormane716f2e2017-05-03 14:53:45 -07003876{
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003877 enum zone_type curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx);
Qian Cai5644e1fb2020-04-01 21:10:12 -07003878
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003879 return curr_idx == MAX_NR_ZONES ? prev_highest_zoneidx : curr_idx;
Mel Gormane716f2e2017-05-03 14:53:45 -07003880}
3881
Mel Gorman38087d92016-07-28 15:45:49 -07003882static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003883 unsigned int highest_zoneidx)
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08003884{
3885 long remaining = 0;
3886 DEFINE_WAIT(wait);
3887
3888 if (freezing(current) || kthread_should_stop())
3889 return;
3890
3891 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
3892
Shantanu Goel333b0a42017-05-03 14:53:38 -07003893 /*
3894 * Try to sleep for a short interval. Note that kcompactd will only be
3895 * woken if it is possible to sleep for a short interval. This is
3896 * deliberate on the assumption that if reclaim cannot keep an
3897 * eligible zone balanced that it's also unlikely that compaction will
3898 * succeed.
3899 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003900 if (prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) {
Vlastimil Babkafd901c92016-04-28 16:18:49 -07003901 /*
3902 * Compaction records what page blocks it recently failed to
3903 * isolate pages from and skips them in the future scanning.
3904 * When kswapd is going to sleep, it is reasonable to assume
3905 * that pages and compaction may succeed so reset the cache.
3906 */
3907 reset_isolation_suitable(pgdat);
3908
3909 /*
3910 * We have freed the memory, now we should compact it to make
3911 * allocation of the requested order possible.
3912 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003913 wakeup_kcompactd(pgdat, alloc_order, highest_zoneidx);
Vlastimil Babkafd901c92016-04-28 16:18:49 -07003914
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08003915 remaining = schedule_timeout(HZ/10);
Mel Gorman38087d92016-07-28 15:45:49 -07003916
3917 /*
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003918 * If woken prematurely then reset kswapd_highest_zoneidx and
Mel Gorman38087d92016-07-28 15:45:49 -07003919 * order. The values will either be from a wakeup request or
3920 * the previous request that slept prematurely.
3921 */
3922 if (remaining) {
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003923 WRITE_ONCE(pgdat->kswapd_highest_zoneidx,
3924 kswapd_highest_zoneidx(pgdat,
3925 highest_zoneidx));
Qian Cai5644e1fb2020-04-01 21:10:12 -07003926
3927 if (READ_ONCE(pgdat->kswapd_order) < reclaim_order)
3928 WRITE_ONCE(pgdat->kswapd_order, reclaim_order);
Mel Gorman38087d92016-07-28 15:45:49 -07003929 }
3930
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08003931 finish_wait(&pgdat->kswapd_wait, &wait);
3932 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
3933 }
3934
3935 /*
3936 * After a short sleep, check if it was a premature sleep. If not, then
3937 * go fully to sleep until explicitly woken up.
3938 */
Mel Gormand9f21d42016-07-28 15:46:41 -07003939 if (!remaining &&
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003940 prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) {
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08003941 trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
3942
3943 /*
3944 * vmstat counters are not perfectly accurate and the estimated
3945 * value for counters such as NR_FREE_PAGES can deviate from the
3946 * true value by nr_online_cpus * threshold. To avoid the zone
3947 * watermarks being breached while under pressure, we reduce the
3948 * per-cpu vmstat threshold while kswapd is awake and restore
3949 * them before going back to sleep.
3950 */
3951 set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold);
Aaditya Kumar1c7e7f62012-07-17 15:48:07 -07003952
3953 if (!kthread_should_stop())
3954 schedule();
3955
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08003956 set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold);
3957 } else {
3958 if (remaining)
3959 count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY);
3960 else
3961 count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY);
3962 }
3963 finish_wait(&pgdat->kswapd_wait, &wait);
3964}
3965
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966/*
3967 * The background pageout daemon, started as a kernel thread
Rik van Riel4f98a2f2008-10-18 20:26:32 -07003968 * from the init process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 *
3970 * This basically trickles out pages so that we have _some_
3971 * free memory available even if there is no other activity
3972 * that frees anything up. This is needed for things like routing
3973 * etc, where we otherwise might have all activity going on in
3974 * asynchronous contexts that cannot page things out.
3975 *
3976 * If there are applications that are active memory-allocators
3977 * (most normal use), this basically shouldn't matter.
3978 */
3979static int kswapd(void *p)
3980{
Mel Gormane716f2e2017-05-03 14:53:45 -07003981 unsigned int alloc_order, reclaim_order;
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003982 unsigned int highest_zoneidx = MAX_NR_ZONES - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 pg_data_t *pgdat = (pg_data_t*)p;
3984 struct task_struct *tsk = current;
Rusty Russella70f7302009-03-13 14:49:46 +10303985 const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986
Rusty Russell174596a2009-01-01 10:12:29 +10303987 if (!cpumask_empty(cpumask))
Mike Travisc5f59f02008-04-04 18:11:10 -07003988 set_cpus_allowed_ptr(tsk, cpumask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
3990 /*
3991 * Tell the memory management that we're a "memory allocator",
3992 * and that if we need more memory we should get access to it
3993 * regardless (see "__alloc_pages()"). "kswapd" should
3994 * never get caught in the normal page freeing logic.
3995 *
3996 * (Kswapd normally doesn't need memory anyway, but sometimes
3997 * you need a small amount of memory in order to be able to
3998 * page out something else, and this flag essentially protects
3999 * us from recursively trying to free more memory as we're
4000 * trying to free the first piece of memory in the first place).
4001 */
Christoph Lameter930d9152006-01-08 01:00:47 -08004002 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
Rafael J. Wysocki83144182007-07-17 04:03:35 -07004003 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004
Qian Cai5644e1fb2020-04-01 21:10:12 -07004005 WRITE_ONCE(pgdat->kswapd_order, 0);
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004006 WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 for ( ; ; ) {
Jeff Liu6f6313d2012-12-11 16:02:48 -08004008 bool ret;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07004009
Qian Cai5644e1fb2020-04-01 21:10:12 -07004010 alloc_order = reclaim_order = READ_ONCE(pgdat->kswapd_order);
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004011 highest_zoneidx = kswapd_highest_zoneidx(pgdat,
4012 highest_zoneidx);
Mel Gormane716f2e2017-05-03 14:53:45 -07004013
Mel Gorman38087d92016-07-28 15:45:49 -07004014kswapd_try_sleep:
4015 kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004016 highest_zoneidx);
Mel Gorman215ddd62011-07-08 15:39:40 -07004017
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004018 /* Read the new order and highest_zoneidx */
Lukas Bulwahn2b47a242020-12-14 19:12:18 -08004019 alloc_order = READ_ONCE(pgdat->kswapd_order);
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004020 highest_zoneidx = kswapd_highest_zoneidx(pgdat,
4021 highest_zoneidx);
Qian Cai5644e1fb2020-04-01 21:10:12 -07004022 WRITE_ONCE(pgdat->kswapd_order, 0);
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004023 WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024
David Rientjes8fe23e02009-12-14 17:58:33 -08004025 ret = try_to_freeze();
4026 if (kthread_should_stop())
4027 break;
4028
4029 /*
4030 * We can speed up thawing tasks if we don't call balance_pgdat
4031 * after returning from the refrigerator
4032 */
Mel Gorman38087d92016-07-28 15:45:49 -07004033 if (ret)
4034 continue;
Mel Gorman1d82de62016-07-28 15:45:43 -07004035
Mel Gorman38087d92016-07-28 15:45:49 -07004036 /*
4037 * Reclaim begins at the requested order but if a high-order
4038 * reclaim fails then kswapd falls back to reclaiming for
4039 * order-0. If that happens, kswapd will consider sleeping
4040 * for the order it finished reclaiming at (reclaim_order)
4041 * but kcompactd is woken to compact for the original
4042 * request (alloc_order).
4043 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004044 trace_mm_vmscan_kswapd_wake(pgdat->node_id, highest_zoneidx,
Mel Gormane5146b12016-07-28 15:46:47 -07004045 alloc_order);
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004046 reclaim_order = balance_pgdat(pgdat, alloc_order,
4047 highest_zoneidx);
Mel Gorman38087d92016-07-28 15:45:49 -07004048 if (reclaim_order < alloc_order)
4049 goto kswapd_try_sleep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 }
Takamori Yamaguchib0a8cc52012-11-08 15:53:39 -08004051
Johannes Weiner71abdc12014-06-06 14:35:35 -07004052 tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD);
Johannes Weiner71abdc12014-06-06 14:35:35 -07004053
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 return 0;
4055}
4056
4057/*
David Rientjes5ecd9d42018-04-05 16:25:16 -07004058 * A zone is low on free memory or too fragmented for high-order memory. If
4059 * kswapd should reclaim (direct reclaim is deferred), wake it up for the zone's
4060 * pgdat. It will wake up kcompactd after reclaiming memory. If kswapd reclaim
4061 * has failed or is not needed, still wake up kcompactd if only compaction is
4062 * needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 */
David Rientjes5ecd9d42018-04-05 16:25:16 -07004064void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004065 enum zone_type highest_zoneidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066{
4067 pg_data_t *pgdat;
Qian Cai5644e1fb2020-04-01 21:10:12 -07004068 enum zone_type curr_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069
Mel Gorman6aa303d2016-09-01 16:14:55 -07004070 if (!managed_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 return;
4072
David Rientjes5ecd9d42018-04-05 16:25:16 -07004073 if (!cpuset_zone_allowed(zone, gfp_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 return;
Shakeel Buttdffcac2c2019-07-04 15:14:42 -07004075
Qian Cai5644e1fb2020-04-01 21:10:12 -07004076 pgdat = zone->zone_pgdat;
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004077 curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx);
Qian Cai5644e1fb2020-04-01 21:10:12 -07004078
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004079 if (curr_idx == MAX_NR_ZONES || curr_idx < highest_zoneidx)
4080 WRITE_ONCE(pgdat->kswapd_highest_zoneidx, highest_zoneidx);
Qian Cai5644e1fb2020-04-01 21:10:12 -07004081
4082 if (READ_ONCE(pgdat->kswapd_order) < order)
4083 WRITE_ONCE(pgdat->kswapd_order, order);
4084
Con Kolivas8d0986e2005-09-13 01:25:07 -07004085 if (!waitqueue_active(&pgdat->kswapd_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 return;
Mel Gormane1a55632016-07-28 15:46:26 -07004087
David Rientjes5ecd9d42018-04-05 16:25:16 -07004088 /* Hopeless node, leave it to direct reclaim if possible */
4089 if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES ||
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004090 (pgdat_balanced(pgdat, order, highest_zoneidx) &&
4091 !pgdat_watermark_boosted(pgdat, highest_zoneidx))) {
David Rientjes5ecd9d42018-04-05 16:25:16 -07004092 /*
4093 * There may be plenty of free memory available, but it's too
4094 * fragmented for high-order allocations. Wake up kcompactd
4095 * and rely on compaction_suitable() to determine if it's
4096 * needed. If it fails, it will defer subsequent attempts to
4097 * ratelimit its work.
4098 */
4099 if (!(gfp_flags & __GFP_DIRECT_RECLAIM))
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004100 wakeup_kcompactd(pgdat, order, highest_zoneidx);
Johannes Weinerc73322d2017-05-03 14:51:51 -07004101 return;
David Rientjes5ecd9d42018-04-05 16:25:16 -07004102 }
Johannes Weinerc73322d2017-05-03 14:51:51 -07004103
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004104 trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, highest_zoneidx, order,
David Rientjes5ecd9d42018-04-05 16:25:16 -07004105 gfp_flags);
Con Kolivas8d0986e2005-09-13 01:25:07 -07004106 wake_up_interruptible(&pgdat->kswapd_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107}
4108
Rafael J. Wysockic6f37f12009-05-24 22:16:31 +02004109#ifdef CONFIG_HIBERNATION
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110/*
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08004111 * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07004112 * freed pages.
4113 *
4114 * Rather than trying to age LRUs the aim is to preserve the overall
4115 * LRU order by reclaiming preferentially
4116 * inactive > active > active referenced > active mapped
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 */
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08004118unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119{
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07004120 struct scan_control sc = {
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08004121 .nr_to_reclaim = nr_to_reclaim,
Johannes Weineree814fe2014-08-06 16:06:19 -07004122 .gfp_mask = GFP_HIGHUSER_MOVABLE,
Mel Gormanb2e18752016-07-28 15:45:37 -07004123 .reclaim_idx = MAX_NR_ZONES - 1,
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07004124 .priority = DEF_PRIORITY,
Johannes Weineree814fe2014-08-06 16:06:19 -07004125 .may_writepage = 1,
4126 .may_unmap = 1,
4127 .may_swap = 1,
4128 .hibernation_mode = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 };
Ying Hana09ed5e2011-05-24 17:12:26 -07004130 struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08004131 unsigned long nr_reclaimed;
Vlastimil Babka499118e2017-05-08 15:59:50 -07004132 unsigned int noreclaim_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01004134 fs_reclaim_acquire(sc.gfp_mask);
Omar Sandoval93781322018-06-07 17:07:02 -07004135 noreclaim_flag = memalloc_noreclaim_save();
Andrew Morton1732d2b012019-07-16 16:26:15 -07004136 set_task_reclaim_state(current, &sc.reclaim_state);
Andrew Morton69e05942006-03-22 00:08:19 -08004137
Vladimir Davydov3115cd92014-04-03 14:47:22 -07004138 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07004139
Andrew Morton1732d2b012019-07-16 16:26:15 -07004140 set_task_reclaim_state(current, NULL);
Vlastimil Babka499118e2017-05-08 15:59:50 -07004141 memalloc_noreclaim_restore(noreclaim_flag);
Omar Sandoval93781322018-06-07 17:07:02 -07004142 fs_reclaim_release(sc.gfp_mask);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07004143
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08004144 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145}
Rafael J. Wysockic6f37f12009-05-24 22:16:31 +02004146#endif /* CONFIG_HIBERNATION */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147
Yasunori Goto3218ae12006-06-27 02:53:33 -07004148/*
4149 * This kswapd start function will be called by init and node-hot-add.
4150 * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
4151 */
4152int kswapd_run(int nid)
4153{
4154 pg_data_t *pgdat = NODE_DATA(nid);
4155 int ret = 0;
4156
4157 if (pgdat->kswapd)
4158 return 0;
4159
4160 pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
4161 if (IS_ERR(pgdat->kswapd)) {
4162 /* failure at boot is fatal */
Thomas Gleixnerc6202ad2017-05-16 20:42:46 +02004163 BUG_ON(system_state < SYSTEM_RUNNING);
Gavin Shand5dc0ad2012-10-08 16:29:27 -07004164 pr_err("Failed to start kswapd on node %d\n", nid);
4165 ret = PTR_ERR(pgdat->kswapd);
Xishi Qiud72515b2013-04-17 15:58:34 -07004166 pgdat->kswapd = NULL;
Yasunori Goto3218ae12006-06-27 02:53:33 -07004167 }
4168 return ret;
4169}
4170
David Rientjes8fe23e02009-12-14 17:58:33 -08004171/*
Jiang Liud8adde12012-07-11 14:01:52 -07004172 * Called by memory hotplug when all memory in a node is offlined. Caller must
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07004173 * hold mem_hotplug_begin/end().
David Rientjes8fe23e02009-12-14 17:58:33 -08004174 */
4175void kswapd_stop(int nid)
4176{
4177 struct task_struct *kswapd = NODE_DATA(nid)->kswapd;
4178
Jiang Liud8adde12012-07-11 14:01:52 -07004179 if (kswapd) {
David Rientjes8fe23e02009-12-14 17:58:33 -08004180 kthread_stop(kswapd);
Jiang Liud8adde12012-07-11 14:01:52 -07004181 NODE_DATA(nid)->kswapd = NULL;
4182 }
David Rientjes8fe23e02009-12-14 17:58:33 -08004183}
4184
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185static int __init kswapd_init(void)
4186{
Wei Yang6b700b52020-04-01 21:10:09 -07004187 int nid;
Andrew Morton69e05942006-03-22 00:08:19 -08004188
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 swap_setup();
Lai Jiangshan48fb2e22012-12-12 13:51:43 -08004190 for_each_node_state(nid, N_MEMORY)
Yasunori Goto3218ae12006-06-27 02:53:33 -07004191 kswapd_run(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 return 0;
4193}
4194
4195module_init(kswapd_init)
Christoph Lameter9eeff232006-01-18 17:42:31 -08004196
4197#ifdef CONFIG_NUMA
4198/*
Mel Gormana5f5f912016-07-28 15:46:32 -07004199 * Node reclaim mode
Christoph Lameter9eeff232006-01-18 17:42:31 -08004200 *
Mel Gormana5f5f912016-07-28 15:46:32 -07004201 * If non-zero call node_reclaim when the number of free pages falls below
Christoph Lameter9eeff232006-01-18 17:42:31 -08004202 * the watermarks.
Christoph Lameter9eeff232006-01-18 17:42:31 -08004203 */
Mel Gormana5f5f912016-07-28 15:46:32 -07004204int node_reclaim_mode __read_mostly;
Christoph Lameter9eeff232006-01-18 17:42:31 -08004205
Dave Hansen51998362021-02-24 12:09:15 -08004206/*
Mel Gormana5f5f912016-07-28 15:46:32 -07004207 * Priority for NODE_RECLAIM. This determines the fraction of pages
Christoph Lametera92f7122006-02-01 03:05:32 -08004208 * of a node considered for each zone_reclaim. 4 scans 1/16th of
4209 * a zone.
4210 */
Mel Gormana5f5f912016-07-28 15:46:32 -07004211#define NODE_RECLAIM_PRIORITY 4
Christoph Lametera92f7122006-02-01 03:05:32 -08004212
Christoph Lameter9eeff232006-01-18 17:42:31 -08004213/*
Mel Gormana5f5f912016-07-28 15:46:32 -07004214 * Percentage of pages in a zone that must be unmapped for node_reclaim to
Christoph Lameter96146342006-07-03 00:24:13 -07004215 * occur.
4216 */
4217int sysctl_min_unmapped_ratio = 1;
4218
4219/*
Christoph Lameter0ff38492006-09-25 23:31:52 -07004220 * If the number of slab pages in a zone grows beyond this percentage then
4221 * slab reclaim needs to occur.
4222 */
4223int sysctl_min_slab_ratio = 5;
4224
Mel Gorman11fb9982016-07-28 15:46:20 -07004225static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat)
Mel Gorman90afa5d2009-06-16 15:33:20 -07004226{
Mel Gorman11fb9982016-07-28 15:46:20 -07004227 unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED);
4228 unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) +
4229 node_page_state(pgdat, NR_ACTIVE_FILE);
Mel Gorman90afa5d2009-06-16 15:33:20 -07004230
4231 /*
4232 * It's possible for there to be more file mapped pages than
4233 * accounted for by the pages on the file LRU lists because
4234 * tmpfs pages accounted for as ANON can also be FILE_MAPPED
4235 */
4236 return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0;
4237}
4238
4239/* Work out how many page cache pages we can reclaim in this reclaim_mode */
Mel Gormana5f5f912016-07-28 15:46:32 -07004240static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat)
Mel Gorman90afa5d2009-06-16 15:33:20 -07004241{
Alexandru Moised031a152015-11-05 18:48:08 -08004242 unsigned long nr_pagecache_reclaimable;
4243 unsigned long delta = 0;
Mel Gorman90afa5d2009-06-16 15:33:20 -07004244
4245 /*
Zhihui Zhang95bbc0c2015-06-24 16:56:42 -07004246 * If RECLAIM_UNMAP is set, then all file pages are considered
Mel Gorman90afa5d2009-06-16 15:33:20 -07004247 * potentially reclaimable. Otherwise, we have to worry about
Mel Gorman11fb9982016-07-28 15:46:20 -07004248 * pages like swapcache and node_unmapped_file_pages() provides
Mel Gorman90afa5d2009-06-16 15:33:20 -07004249 * a better estimate
4250 */
Mel Gormana5f5f912016-07-28 15:46:32 -07004251 if (node_reclaim_mode & RECLAIM_UNMAP)
4252 nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES);
Mel Gorman90afa5d2009-06-16 15:33:20 -07004253 else
Mel Gormana5f5f912016-07-28 15:46:32 -07004254 nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat);
Mel Gorman90afa5d2009-06-16 15:33:20 -07004255
4256 /* If we can't clean pages, remove dirty pages from consideration */
Mel Gormana5f5f912016-07-28 15:46:32 -07004257 if (!(node_reclaim_mode & RECLAIM_WRITE))
4258 delta += node_page_state(pgdat, NR_FILE_DIRTY);
Mel Gorman90afa5d2009-06-16 15:33:20 -07004259
4260 /* Watch for any possible underflows due to delta */
4261 if (unlikely(delta > nr_pagecache_reclaimable))
4262 delta = nr_pagecache_reclaimable;
4263
4264 return nr_pagecache_reclaimable - delta;
4265}
4266
Christoph Lameter0ff38492006-09-25 23:31:52 -07004267/*
Mel Gormana5f5f912016-07-28 15:46:32 -07004268 * Try to free up some pages from this node through reclaim.
Christoph Lameter9eeff232006-01-18 17:42:31 -08004269 */
Mel Gormana5f5f912016-07-28 15:46:32 -07004270static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
Christoph Lameter9eeff232006-01-18 17:42:31 -08004271{
Christoph Lameter7fb2d462006-03-22 00:08:22 -08004272 /* Minimum pages needed in order to stay on node */
Andrew Morton69e05942006-03-22 00:08:19 -08004273 const unsigned long nr_pages = 1 << order;
Christoph Lameter9eeff232006-01-18 17:42:31 -08004274 struct task_struct *p = current;
Vlastimil Babka499118e2017-05-08 15:59:50 -07004275 unsigned int noreclaim_flag;
Andrew Morton179e9632006-03-22 00:08:18 -08004276 struct scan_control sc = {
Andrew Morton62b726c2013-02-22 16:32:24 -08004277 .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
Nick Desaulniersf2f43e52017-07-06 15:36:50 -07004278 .gfp_mask = current_gfp_context(gfp_mask),
Johannes Weinerbd2f6192009-03-31 15:19:38 -07004279 .order = order,
Mel Gormana5f5f912016-07-28 15:46:32 -07004280 .priority = NODE_RECLAIM_PRIORITY,
4281 .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE),
4282 .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP),
Johannes Weineree814fe2014-08-06 16:06:19 -07004283 .may_swap = 1,
Nick Desaulniersf2f43e52017-07-06 15:36:50 -07004284 .reclaim_idx = gfp_zone(gfp_mask),
Andrew Morton179e9632006-03-22 00:08:18 -08004285 };
Christoph Lameter9eeff232006-01-18 17:42:31 -08004286
Yafang Shao132bb8c2019-05-13 17:17:53 -07004287 trace_mm_vmscan_node_reclaim_begin(pgdat->node_id, order,
4288 sc.gfp_mask);
4289
Christoph Lameter9eeff232006-01-18 17:42:31 -08004290 cond_resched();
Omar Sandoval93781322018-06-07 17:07:02 -07004291 fs_reclaim_acquire(sc.gfp_mask);
Christoph Lameterd4f77962006-02-24 13:04:22 -08004292 /*
Zhihui Zhang95bbc0c2015-06-24 16:56:42 -07004293 * We need to be able to allocate from the reserves for RECLAIM_UNMAP
Christoph Lameterd4f77962006-02-24 13:04:22 -08004294 * and we also need to be able to write out pages for RECLAIM_WRITE
Zhihui Zhang95bbc0c2015-06-24 16:56:42 -07004295 * and RECLAIM_UNMAP.
Christoph Lameterd4f77962006-02-24 13:04:22 -08004296 */
Vlastimil Babka499118e2017-05-08 15:59:50 -07004297 noreclaim_flag = memalloc_noreclaim_save();
4298 p->flags |= PF_SWAPWRITE;
Andrew Morton1732d2b012019-07-16 16:26:15 -07004299 set_task_reclaim_state(p, &sc.reclaim_state);
Christoph Lameterc84db232006-02-01 03:05:29 -08004300
Mel Gormana5f5f912016-07-28 15:46:32 -07004301 if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages) {
Christoph Lameter0ff38492006-09-25 23:31:52 -07004302 /*
Andrey Ryabinin894befe2018-04-10 16:27:51 -07004303 * Free memory by calling shrink node with increasing
Christoph Lameter0ff38492006-09-25 23:31:52 -07004304 * priorities until we have enough memory freed.
4305 */
Christoph Lameter0ff38492006-09-25 23:31:52 -07004306 do {
Mel Gorman970a39a2016-07-28 15:46:35 -07004307 shrink_node(pgdat, &sc);
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07004308 } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0);
Christoph Lameter0ff38492006-09-25 23:31:52 -07004309 }
Christoph Lameterc84db232006-02-01 03:05:29 -08004310
Andrew Morton1732d2b012019-07-16 16:26:15 -07004311 set_task_reclaim_state(p, NULL);
Vlastimil Babka499118e2017-05-08 15:59:50 -07004312 current->flags &= ~PF_SWAPWRITE;
4313 memalloc_noreclaim_restore(noreclaim_flag);
Omar Sandoval93781322018-06-07 17:07:02 -07004314 fs_reclaim_release(sc.gfp_mask);
Yafang Shao132bb8c2019-05-13 17:17:53 -07004315
4316 trace_mm_vmscan_node_reclaim_end(sc.nr_reclaimed);
4317
Rik van Riela79311c2009-01-06 14:40:01 -08004318 return sc.nr_reclaimed >= nr_pages;
Christoph Lameter9eeff232006-01-18 17:42:31 -08004319}
Andrew Morton179e9632006-03-22 00:08:18 -08004320
Mel Gormana5f5f912016-07-28 15:46:32 -07004321int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
Andrew Morton179e9632006-03-22 00:08:18 -08004322{
David Rientjesd773ed62007-10-16 23:26:01 -07004323 int ret;
Andrew Morton179e9632006-03-22 00:08:18 -08004324
4325 /*
Mel Gormana5f5f912016-07-28 15:46:32 -07004326 * Node reclaim reclaims unmapped file backed pages and
Christoph Lameter0ff38492006-09-25 23:31:52 -07004327 * slab pages if we are over the defined limits.
Christoph Lameter34aa1332006-06-30 01:55:37 -07004328 *
Christoph Lameter96146342006-07-03 00:24:13 -07004329 * A small portion of unmapped file backed pages is needed for
4330 * file I/O otherwise pages read by file I/O will be immediately
Mel Gormana5f5f912016-07-28 15:46:32 -07004331 * thrown out if the node is overallocated. So we do not reclaim
4332 * if less than a specified percentage of the node is used by
Christoph Lameter96146342006-07-03 00:24:13 -07004333 * unmapped file backed pages.
Andrew Morton179e9632006-03-22 00:08:18 -08004334 */
Mel Gormana5f5f912016-07-28 15:46:32 -07004335 if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages &&
Roman Gushchind42f3242020-08-06 23:20:39 -07004336 node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B) <=
4337 pgdat->min_slab_pages)
Mel Gormana5f5f912016-07-28 15:46:32 -07004338 return NODE_RECLAIM_FULL;
Andrew Morton179e9632006-03-22 00:08:18 -08004339
4340 /*
David Rientjesd773ed62007-10-16 23:26:01 -07004341 * Do not scan if the allocation should not be delayed.
Andrew Morton179e9632006-03-22 00:08:18 -08004342 */
Mel Gormand0164ad2015-11-06 16:28:21 -08004343 if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC))
Mel Gormana5f5f912016-07-28 15:46:32 -07004344 return NODE_RECLAIM_NOSCAN;
Andrew Morton179e9632006-03-22 00:08:18 -08004345
4346 /*
Mel Gormana5f5f912016-07-28 15:46:32 -07004347 * Only run node reclaim on the local node or on nodes that do not
Andrew Morton179e9632006-03-22 00:08:18 -08004348 * have associated processors. This will favor the local processor
4349 * over remote processors and spread off node memory allocations
4350 * as wide as possible.
4351 */
Mel Gormana5f5f912016-07-28 15:46:32 -07004352 if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id())
4353 return NODE_RECLAIM_NOSCAN;
David Rientjesd773ed62007-10-16 23:26:01 -07004354
Mel Gormana5f5f912016-07-28 15:46:32 -07004355 if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags))
4356 return NODE_RECLAIM_NOSCAN;
Mel Gormanfa5e0842009-06-16 15:33:22 -07004357
Mel Gormana5f5f912016-07-28 15:46:32 -07004358 ret = __node_reclaim(pgdat, gfp_mask, order);
4359 clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags);
David Rientjesd773ed62007-10-16 23:26:01 -07004360
Mel Gorman24cf725182009-06-16 15:33:23 -07004361 if (!ret)
4362 count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED);
4363
David Rientjesd773ed62007-10-16 23:26:01 -07004364 return ret;
Andrew Morton179e9632006-03-22 00:08:18 -08004365}
Christoph Lameter9eeff232006-01-18 17:42:31 -08004366#endif
Lee Schermerhorn894bc312008-10-18 20:26:39 -07004367
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07004368/**
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00004369 * check_move_unevictable_pages - check pages for evictability and move to
4370 * appropriate zone lru list
4371 * @pvec: pagevec with lru pages to check
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07004372 *
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00004373 * Checks pages for evictability, if an evictable page is in the unevictable
4374 * lru list, moves it to the appropriate evictable lru list. This function
4375 * should be only used for lru pages.
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07004376 */
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00004377void check_move_unevictable_pages(struct pagevec *pvec)
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07004378{
Alex Shi6168d0d2020-12-15 12:34:29 -08004379 struct lruvec *lruvec = NULL;
Hugh Dickins24513262012-01-20 14:34:21 -08004380 int pgscanned = 0;
4381 int pgrescued = 0;
4382 int i;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07004383
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00004384 for (i = 0; i < pvec->nr; i++) {
4385 struct page *page = pvec->pages[i];
Hugh Dickins8d8869c2020-09-18 21:20:12 -07004386 int nr_pages;
Lee Schermerhornaf936a12008-10-18 20:26:53 -07004387
Hugh Dickins8d8869c2020-09-18 21:20:12 -07004388 if (PageTransTail(page))
4389 continue;
4390
4391 nr_pages = thp_nr_pages(page);
4392 pgscanned += nr_pages;
4393
Alex Shid25b5bd2020-12-15 12:34:16 -08004394 /* block memcg migration during page moving between lru */
4395 if (!TestClearPageLRU(page))
4396 continue;
4397
Alexander Duyck2a5e4e32020-12-15 12:34:33 -08004398 lruvec = relock_page_lruvec_irq(page, lruvec);
Alex Shid25b5bd2020-12-15 12:34:16 -08004399 if (page_evictable(page) && PageUnevictable(page)) {
Yu Zhao46ae6b22021-02-24 12:08:25 -08004400 del_page_from_lru_list(page, lruvec);
Hugh Dickins24513262012-01-20 14:34:21 -08004401 ClearPageUnevictable(page);
Yu Zhao3a9c9782021-02-24 12:08:17 -08004402 add_page_to_lru_list(page, lruvec);
Hugh Dickins8d8869c2020-09-18 21:20:12 -07004403 pgrescued += nr_pages;
Hugh Dickins24513262012-01-20 14:34:21 -08004404 }
Alex Shid25b5bd2020-12-15 12:34:16 -08004405 SetPageLRU(page);
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07004406 }
Hugh Dickins24513262012-01-20 14:34:21 -08004407
Alex Shi6168d0d2020-12-15 12:34:29 -08004408 if (lruvec) {
Hugh Dickins24513262012-01-20 14:34:21 -08004409 __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued);
4410 __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned);
Alex Shi6168d0d2020-12-15 12:34:29 -08004411 unlock_page_lruvec_irq(lruvec);
Alex Shid25b5bd2020-12-15 12:34:16 -08004412 } else if (pgscanned) {
4413 count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned);
Hugh Dickins24513262012-01-20 14:34:21 -08004414 }
Hugh Dickins850465792012-01-20 14:34:19 -08004415}
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00004416EXPORT_SYMBOL_GPL(check_move_unevictable_pages);