xref: /linux/mm/vmpressure.c (revision cf79f291f985662150363b4a93d16f88f12643bc)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
270ddf637SAnton Vorontsov /*
370ddf637SAnton Vorontsov  * Linux VM pressure
470ddf637SAnton Vorontsov  *
570ddf637SAnton Vorontsov  * Copyright 2012 Linaro Ltd.
670ddf637SAnton Vorontsov  *		  Anton Vorontsov <anton.vorontsov@linaro.org>
770ddf637SAnton Vorontsov  *
870ddf637SAnton Vorontsov  * Based on ideas from Andrew Morton, David Rientjes, KOSAKI Motohiro,
970ddf637SAnton Vorontsov  * Leonid Moiseichuk, Mel Gorman, Minchan Kim and Pekka Enberg.
1070ddf637SAnton Vorontsov  */
1170ddf637SAnton Vorontsov 
1270ddf637SAnton Vorontsov #include <linux/cgroup.h>
1370ddf637SAnton Vorontsov #include <linux/fs.h>
1470ddf637SAnton Vorontsov #include <linux/log2.h>
1570ddf637SAnton Vorontsov #include <linux/sched.h>
1670ddf637SAnton Vorontsov #include <linux/mm.h>
1770ddf637SAnton Vorontsov #include <linux/vmstat.h>
1870ddf637SAnton Vorontsov #include <linux/eventfd.h>
191ff6bbfdSTejun Heo #include <linux/slab.h>
2070ddf637SAnton Vorontsov #include <linux/swap.h>
2170ddf637SAnton Vorontsov #include <linux/printk.h>
2270ddf637SAnton Vorontsov #include <linux/vmpressure.h>
2370ddf637SAnton Vorontsov 
2470ddf637SAnton Vorontsov /*
2570ddf637SAnton Vorontsov  * The window size (vmpressure_win) is the number of scanned pages before
2670ddf637SAnton Vorontsov  * we try to analyze scanned/reclaimed ratio. So the window is used as a
2770ddf637SAnton Vorontsov  * rate-limit tunable for the "low" level notification, and also for
2870ddf637SAnton Vorontsov  * averaging the ratio for medium/critical levels. Using small window
2970ddf637SAnton Vorontsov  * sizes can cause lot of false positives, but too big window size will
3070ddf637SAnton Vorontsov  * delay the notifications.
3170ddf637SAnton Vorontsov  *
3270ddf637SAnton Vorontsov  * As the vmscan reclaimer logic works with chunks which are multiple of
3370ddf637SAnton Vorontsov  * SWAP_CLUSTER_MAX, it makes sense to use it for the window size as well.
3470ddf637SAnton Vorontsov  *
3570ddf637SAnton Vorontsov  * TODO: Make the window size depend on machine size, as we do for vmstat
3670ddf637SAnton Vorontsov  * thresholds. Currently we set it to 512 pages (2MB for 4KB pages).
3770ddf637SAnton Vorontsov  */
3870ddf637SAnton Vorontsov static const unsigned long vmpressure_win = SWAP_CLUSTER_MAX * 16;
3970ddf637SAnton Vorontsov 
4070ddf637SAnton Vorontsov /*
4170ddf637SAnton Vorontsov  * These thresholds are used when we account memory pressure through
4270ddf637SAnton Vorontsov  * scanned/reclaimed ratio. The current values were chosen empirically. In
4370ddf637SAnton Vorontsov  * essence, they are percents: the higher the value, the more number
4470ddf637SAnton Vorontsov  * unsuccessful reclaims there were.
4570ddf637SAnton Vorontsov  */
4670ddf637SAnton Vorontsov static const unsigned int vmpressure_level_med = 60;
4770ddf637SAnton Vorontsov static const unsigned int vmpressure_level_critical = 95;
4870ddf637SAnton Vorontsov 
4970ddf637SAnton Vorontsov /*
5070ddf637SAnton Vorontsov  * When there are too little pages left to scan, vmpressure() may miss the
5170ddf637SAnton Vorontsov  * critical pressure as number of pages will be less than "window size".
5270ddf637SAnton Vorontsov  * However, in that case the vmscan priority will raise fast as the
5370ddf637SAnton Vorontsov  * reclaimer will try to scan LRUs more deeply.
5470ddf637SAnton Vorontsov  *
5570ddf637SAnton Vorontsov  * The vmscan logic considers these special priorities:
5670ddf637SAnton Vorontsov  *
5770ddf637SAnton Vorontsov  * prio == DEF_PRIORITY (12): reclaimer starts with that value
5870ddf637SAnton Vorontsov  * prio <= DEF_PRIORITY - 2 : kswapd becomes somewhat overwhelmed
5970ddf637SAnton Vorontsov  * prio == 0                : close to OOM, kernel scans every page in an lru
6070ddf637SAnton Vorontsov  *
6170ddf637SAnton Vorontsov  * Any value in this range is acceptable for this tunable (i.e. from 12 to
6270ddf637SAnton Vorontsov  * 0). Current value for the vmpressure_level_critical_prio is chosen
6370ddf637SAnton Vorontsov  * empirically, but the number, in essence, means that we consider
6470ddf637SAnton Vorontsov  * critical level when scanning depth is ~10% of the lru size (vmscan
6570ddf637SAnton Vorontsov  * scans 'lru_size >> prio' pages, so it is actually 12.5%, or one
6670ddf637SAnton Vorontsov  * eights).
6770ddf637SAnton Vorontsov  */
6870ddf637SAnton Vorontsov static const unsigned int vmpressure_level_critical_prio = ilog2(100 / 10);
6970ddf637SAnton Vorontsov 
work_to_vmpressure(struct work_struct * work)7070ddf637SAnton Vorontsov static struct vmpressure *work_to_vmpressure(struct work_struct *work)
7170ddf637SAnton Vorontsov {
7270ddf637SAnton Vorontsov 	return container_of(work, struct vmpressure, work);
7370ddf637SAnton Vorontsov }
7470ddf637SAnton Vorontsov 
vmpressure_parent(struct vmpressure * vmpr)7570ddf637SAnton Vorontsov static struct vmpressure *vmpressure_parent(struct vmpressure *vmpr)
7670ddf637SAnton Vorontsov {
779647875bSHui Su 	struct mem_cgroup *memcg = vmpressure_to_memcg(vmpr);
7870ddf637SAnton Vorontsov 
7970ddf637SAnton Vorontsov 	memcg = parent_mem_cgroup(memcg);
8070ddf637SAnton Vorontsov 	if (!memcg)
8170ddf637SAnton Vorontsov 		return NULL;
8270ddf637SAnton Vorontsov 	return memcg_to_vmpressure(memcg);
8370ddf637SAnton Vorontsov }
8470ddf637SAnton Vorontsov 
8570ddf637SAnton Vorontsov enum vmpressure_levels {
8670ddf637SAnton Vorontsov 	VMPRESSURE_LOW = 0,
8770ddf637SAnton Vorontsov 	VMPRESSURE_MEDIUM,
8870ddf637SAnton Vorontsov 	VMPRESSURE_CRITICAL,
8970ddf637SAnton Vorontsov 	VMPRESSURE_NUM_LEVELS,
9070ddf637SAnton Vorontsov };
9170ddf637SAnton Vorontsov 
92b6bb9811SDavid Rientjes enum vmpressure_modes {
93b6bb9811SDavid Rientjes 	VMPRESSURE_NO_PASSTHROUGH = 0,
94b6bb9811SDavid Rientjes 	VMPRESSURE_HIERARCHY,
95b6bb9811SDavid Rientjes 	VMPRESSURE_LOCAL,
96b6bb9811SDavid Rientjes 	VMPRESSURE_NUM_MODES,
97b6bb9811SDavid Rientjes };
98b6bb9811SDavid Rientjes 
9970ddf637SAnton Vorontsov static const char * const vmpressure_str_levels[] = {
10070ddf637SAnton Vorontsov 	[VMPRESSURE_LOW] = "low",
10170ddf637SAnton Vorontsov 	[VMPRESSURE_MEDIUM] = "medium",
10270ddf637SAnton Vorontsov 	[VMPRESSURE_CRITICAL] = "critical",
10370ddf637SAnton Vorontsov };
10470ddf637SAnton Vorontsov 
105b6bb9811SDavid Rientjes static const char * const vmpressure_str_modes[] = {
106b6bb9811SDavid Rientjes 	[VMPRESSURE_NO_PASSTHROUGH] = "default",
107b6bb9811SDavid Rientjes 	[VMPRESSURE_HIERARCHY] = "hierarchy",
108b6bb9811SDavid Rientjes 	[VMPRESSURE_LOCAL] = "local",
109b6bb9811SDavid Rientjes };
110b6bb9811SDavid Rientjes 
vmpressure_level(unsigned long pressure)11170ddf637SAnton Vorontsov static enum vmpressure_levels vmpressure_level(unsigned long pressure)
11270ddf637SAnton Vorontsov {
11370ddf637SAnton Vorontsov 	if (pressure >= vmpressure_level_critical)
11470ddf637SAnton Vorontsov 		return VMPRESSURE_CRITICAL;
11570ddf637SAnton Vorontsov 	else if (pressure >= vmpressure_level_med)
11670ddf637SAnton Vorontsov 		return VMPRESSURE_MEDIUM;
11770ddf637SAnton Vorontsov 	return VMPRESSURE_LOW;
11870ddf637SAnton Vorontsov }
11970ddf637SAnton Vorontsov 
vmpressure_calc_level(unsigned long scanned,unsigned long reclaimed)12070ddf637SAnton Vorontsov static enum vmpressure_levels vmpressure_calc_level(unsigned long scanned,
12170ddf637SAnton Vorontsov 						    unsigned long reclaimed)
12270ddf637SAnton Vorontsov {
12370ddf637SAnton Vorontsov 	unsigned long scale = scanned + reclaimed;
124e1587a49SVinayak Menon 	unsigned long pressure = 0;
12570ddf637SAnton Vorontsov 
12670ddf637SAnton Vorontsov 	/*
127d7143e31Szhongjiang 	 * reclaimed can be greater than scanned for things such as reclaimed
128d7143e31Szhongjiang 	 * slab pages. shrink_node() just adds reclaimed pages without a
129d7143e31Szhongjiang 	 * related increment to scanned pages.
130e1587a49SVinayak Menon 	 */
131e1587a49SVinayak Menon 	if (reclaimed >= scanned)
132e1587a49SVinayak Menon 		goto out;
133e1587a49SVinayak Menon 	/*
13470ddf637SAnton Vorontsov 	 * We calculate the ratio (in percents) of how many pages were
13570ddf637SAnton Vorontsov 	 * scanned vs. reclaimed in a given time frame (window). Note that
13670ddf637SAnton Vorontsov 	 * time is in VM reclaimer's "ticks", i.e. number of pages
13770ddf637SAnton Vorontsov 	 * scanned. This makes it possible to set desired reaction time
13870ddf637SAnton Vorontsov 	 * and serves as a ratelimit.
13970ddf637SAnton Vorontsov 	 */
14070ddf637SAnton Vorontsov 	pressure = scale - (reclaimed * scale / scanned);
14170ddf637SAnton Vorontsov 	pressure = pressure * 100 / scale;
14270ddf637SAnton Vorontsov 
143e1587a49SVinayak Menon out:
14470ddf637SAnton Vorontsov 	pr_debug("%s: %3lu  (s: %lu  r: %lu)\n", __func__, pressure,
14570ddf637SAnton Vorontsov 		 scanned, reclaimed);
14670ddf637SAnton Vorontsov 
14770ddf637SAnton Vorontsov 	return vmpressure_level(pressure);
14870ddf637SAnton Vorontsov }
14970ddf637SAnton Vorontsov 
15070ddf637SAnton Vorontsov struct vmpressure_event {
15170ddf637SAnton Vorontsov 	struct eventfd_ctx *efd;
15270ddf637SAnton Vorontsov 	enum vmpressure_levels level;
153b6bb9811SDavid Rientjes 	enum vmpressure_modes mode;
15470ddf637SAnton Vorontsov 	struct list_head node;
15570ddf637SAnton Vorontsov };
15670ddf637SAnton Vorontsov 
vmpressure_event(struct vmpressure * vmpr,const enum vmpressure_levels level,bool ancestor,bool signalled)15770ddf637SAnton Vorontsov static bool vmpressure_event(struct vmpressure *vmpr,
158b6bb9811SDavid Rientjes 			     const enum vmpressure_levels level,
159b6bb9811SDavid Rientjes 			     bool ancestor, bool signalled)
16070ddf637SAnton Vorontsov {
16170ddf637SAnton Vorontsov 	struct vmpressure_event *ev;
162b6bb9811SDavid Rientjes 	bool ret = false;
16370ddf637SAnton Vorontsov 
16470ddf637SAnton Vorontsov 	mutex_lock(&vmpr->events_lock);
16570ddf637SAnton Vorontsov 	list_for_each_entry(ev, &vmpr->events, node) {
166b6bb9811SDavid Rientjes 		if (ancestor && ev->mode == VMPRESSURE_LOCAL)
167b6bb9811SDavid Rientjes 			continue;
168b6bb9811SDavid Rientjes 		if (signalled && ev->mode == VMPRESSURE_NO_PASSTHROUGH)
169b6bb9811SDavid Rientjes 			continue;
170b6bb9811SDavid Rientjes 		if (level < ev->level)
171b6bb9811SDavid Rientjes 			continue;
172*3652117fSChristian Brauner 		eventfd_signal(ev->efd);
173b6bb9811SDavid Rientjes 		ret = true;
17470ddf637SAnton Vorontsov 	}
17570ddf637SAnton Vorontsov 	mutex_unlock(&vmpr->events_lock);
17670ddf637SAnton Vorontsov 
177b6bb9811SDavid Rientjes 	return ret;
17870ddf637SAnton Vorontsov }
17970ddf637SAnton Vorontsov 
vmpressure_work_fn(struct work_struct * work)18070ddf637SAnton Vorontsov static void vmpressure_work_fn(struct work_struct *work)
18170ddf637SAnton Vorontsov {
18270ddf637SAnton Vorontsov 	struct vmpressure *vmpr = work_to_vmpressure(work);
18370ddf637SAnton Vorontsov 	unsigned long scanned;
18470ddf637SAnton Vorontsov 	unsigned long reclaimed;
1858e8ae645SJohannes Weiner 	enum vmpressure_levels level;
186b6bb9811SDavid Rientjes 	bool ancestor = false;
187b6bb9811SDavid Rientjes 	bool signalled = false;
18870ddf637SAnton Vorontsov 
18991b57191SAndrew Morton 	spin_lock(&vmpr->sr_lock);
19070ddf637SAnton Vorontsov 	/*
19170ddf637SAnton Vorontsov 	 * Several contexts might be calling vmpressure(), so it is
19270ddf637SAnton Vorontsov 	 * possible that the work was rescheduled again before the old
19370ddf637SAnton Vorontsov 	 * work context cleared the counters. In that case we will run
19470ddf637SAnton Vorontsov 	 * just after the old work returns, but then scanned might be zero
19570ddf637SAnton Vorontsov 	 * here. No need for any locks here since we don't care if
19670ddf637SAnton Vorontsov 	 * vmpr->reclaimed is in sync.
19770ddf637SAnton Vorontsov 	 */
1988e8ae645SJohannes Weiner 	scanned = vmpr->tree_scanned;
19991b57191SAndrew Morton 	if (!scanned) {
20091b57191SAndrew Morton 		spin_unlock(&vmpr->sr_lock);
20191b57191SAndrew Morton 		return;
20291b57191SAndrew Morton 	}
20391b57191SAndrew Morton 
2048e8ae645SJohannes Weiner 	reclaimed = vmpr->tree_reclaimed;
2058e8ae645SJohannes Weiner 	vmpr->tree_scanned = 0;
2068e8ae645SJohannes Weiner 	vmpr->tree_reclaimed = 0;
20722f2020fSMichal Hocko 	spin_unlock(&vmpr->sr_lock);
20870ddf637SAnton Vorontsov 
2098e8ae645SJohannes Weiner 	level = vmpressure_calc_level(scanned, reclaimed);
2108e8ae645SJohannes Weiner 
21170ddf637SAnton Vorontsov 	do {
212b6bb9811SDavid Rientjes 		if (vmpressure_event(vmpr, level, ancestor, signalled))
213b6bb9811SDavid Rientjes 			signalled = true;
214b6bb9811SDavid Rientjes 		ancestor = true;
21570ddf637SAnton Vorontsov 	} while ((vmpr = vmpressure_parent(vmpr)));
21670ddf637SAnton Vorontsov }
21770ddf637SAnton Vorontsov 
21870ddf637SAnton Vorontsov /**
21970ddf637SAnton Vorontsov  * vmpressure() - Account memory pressure through scanned/reclaimed ratio
22070ddf637SAnton Vorontsov  * @gfp:	reclaimer's gfp mask
22170ddf637SAnton Vorontsov  * @memcg:	cgroup memory controller handle
2228e8ae645SJohannes Weiner  * @tree:	legacy subtree mode
22370ddf637SAnton Vorontsov  * @scanned:	number of pages scanned
22470ddf637SAnton Vorontsov  * @reclaimed:	number of pages reclaimed
22570ddf637SAnton Vorontsov  *
22670ddf637SAnton Vorontsov  * This function should be called from the vmscan reclaim path to account
22770ddf637SAnton Vorontsov  * "instantaneous" memory pressure (scanned/reclaimed ratio). The raw
22870ddf637SAnton Vorontsov  * pressure index is then further refined and averaged over time.
22970ddf637SAnton Vorontsov  *
2308e8ae645SJohannes Weiner  * If @tree is set, vmpressure is in traditional userspace reporting
2318e8ae645SJohannes Weiner  * mode: @memcg is considered the pressure root and userspace is
2328e8ae645SJohannes Weiner  * notified of the entire subtree's reclaim efficiency.
2338e8ae645SJohannes Weiner  *
2348e8ae645SJohannes Weiner  * If @tree is not set, reclaim efficiency is recorded for @memcg, and
2358e8ae645SJohannes Weiner  * only in-kernel users are notified.
2368e8ae645SJohannes Weiner  *
23770ddf637SAnton Vorontsov  * This function does not return any value.
23870ddf637SAnton Vorontsov  */
vmpressure(gfp_t gfp,struct mem_cgroup * memcg,bool tree,unsigned long scanned,unsigned long reclaimed)2398e8ae645SJohannes Weiner void vmpressure(gfp_t gfp, struct mem_cgroup *memcg, bool tree,
24070ddf637SAnton Vorontsov 		unsigned long scanned, unsigned long reclaimed)
24170ddf637SAnton Vorontsov {
24256cab285SSuren Baghdasaryan 	struct vmpressure *vmpr;
24356cab285SSuren Baghdasaryan 
24456cab285SSuren Baghdasaryan 	if (mem_cgroup_disabled())
24556cab285SSuren Baghdasaryan 		return;
24656cab285SSuren Baghdasaryan 
247ac8a5296SAbel Wu 	/*
248ac8a5296SAbel Wu 	 * The in-kernel users only care about the reclaim efficiency
249ac8a5296SAbel Wu 	 * for this @memcg rather than the whole subtree, and there
250ac8a5296SAbel Wu 	 * isn't and won't be any in-kernel user in a legacy cgroup.
251ac8a5296SAbel Wu 	 */
252ac8a5296SAbel Wu 	if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && !tree)
253ac8a5296SAbel Wu 		return;
254ac8a5296SAbel Wu 
25556cab285SSuren Baghdasaryan 	vmpr = memcg_to_vmpressure(memcg);
25670ddf637SAnton Vorontsov 
25770ddf637SAnton Vorontsov 	/*
25870ddf637SAnton Vorontsov 	 * Here we only want to account pressure that userland is able to
25970ddf637SAnton Vorontsov 	 * help us with. For example, suppose that DMA zone is under
26070ddf637SAnton Vorontsov 	 * pressure; if we notify userland about that kind of pressure,
26170ddf637SAnton Vorontsov 	 * then it will be mostly a waste as it will trigger unnecessary
26270ddf637SAnton Vorontsov 	 * freeing of memory by userland (since userland is more likely to
26370ddf637SAnton Vorontsov 	 * have HIGHMEM/MOVABLE pages instead of the DMA fallback). That
26470ddf637SAnton Vorontsov 	 * is why we include only movable, highmem and FS/IO pages.
26570ddf637SAnton Vorontsov 	 * Indirect reclaim (kswapd) sets sc->gfp_mask to GFP_KERNEL, so
26670ddf637SAnton Vorontsov 	 * we account it too.
26770ddf637SAnton Vorontsov 	 */
26870ddf637SAnton Vorontsov 	if (!(gfp & (__GFP_HIGHMEM | __GFP_MOVABLE | __GFP_IO | __GFP_FS)))
26970ddf637SAnton Vorontsov 		return;
27070ddf637SAnton Vorontsov 
27170ddf637SAnton Vorontsov 	/*
27270ddf637SAnton Vorontsov 	 * If we got here with no pages scanned, then that is an indicator
27370ddf637SAnton Vorontsov 	 * that reclaimer was unable to find any shrinkable LRUs at the
27470ddf637SAnton Vorontsov 	 * current scanning depth. But it does not mean that we should
27570ddf637SAnton Vorontsov 	 * report the critical pressure, yet. If the scanning priority
27670ddf637SAnton Vorontsov 	 * (scanning depth) goes too high (deep), we will be notified
27770ddf637SAnton Vorontsov 	 * through vmpressure_prio(). But so far, keep calm.
27870ddf637SAnton Vorontsov 	 */
27970ddf637SAnton Vorontsov 	if (!scanned)
28070ddf637SAnton Vorontsov 		return;
28170ddf637SAnton Vorontsov 
2828e8ae645SJohannes Weiner 	if (tree) {
28322f2020fSMichal Hocko 		spin_lock(&vmpr->sr_lock);
2843c1da7beSVladimir Davydov 		scanned = vmpr->tree_scanned += scanned;
2858e8ae645SJohannes Weiner 		vmpr->tree_reclaimed += reclaimed;
28622f2020fSMichal Hocko 		spin_unlock(&vmpr->sr_lock);
28770ddf637SAnton Vorontsov 
2888e0ed445SMichal Hocko 		if (scanned < vmpressure_win)
28970ddf637SAnton Vorontsov 			return;
29070ddf637SAnton Vorontsov 		schedule_work(&vmpr->work);
2918e8ae645SJohannes Weiner 	} else {
2928e8ae645SJohannes Weiner 		enum vmpressure_levels level;
2938e8ae645SJohannes Weiner 
2948e8ae645SJohannes Weiner 		/* For now, no users for root-level efficiency */
295d8a1c03fSYang Shi 		if (!memcg || mem_cgroup_is_root(memcg))
2968e8ae645SJohannes Weiner 			return;
2978e8ae645SJohannes Weiner 
2988e8ae645SJohannes Weiner 		spin_lock(&vmpr->sr_lock);
2998e8ae645SJohannes Weiner 		scanned = vmpr->scanned += scanned;
3008e8ae645SJohannes Weiner 		reclaimed = vmpr->reclaimed += reclaimed;
3018e8ae645SJohannes Weiner 		if (scanned < vmpressure_win) {
3028e8ae645SJohannes Weiner 			spin_unlock(&vmpr->sr_lock);
3038e8ae645SJohannes Weiner 			return;
3048e8ae645SJohannes Weiner 		}
3058e8ae645SJohannes Weiner 		vmpr->scanned = vmpr->reclaimed = 0;
3068e8ae645SJohannes Weiner 		spin_unlock(&vmpr->sr_lock);
3078e8ae645SJohannes Weiner 
3088e8ae645SJohannes Weiner 		level = vmpressure_calc_level(scanned, reclaimed);
3098e8ae645SJohannes Weiner 
3108e8ae645SJohannes Weiner 		if (level > VMPRESSURE_LOW) {
3118e8ae645SJohannes Weiner 			/*
3128e8ae645SJohannes Weiner 			 * Let the socket buffer allocator know that
3138e8ae645SJohannes Weiner 			 * we are having trouble reclaiming LRU pages.
3148e8ae645SJohannes Weiner 			 *
3158e8ae645SJohannes Weiner 			 * For hysteresis keep the pressure state
3168e8ae645SJohannes Weiner 			 * asserted for a second in which subsequent
3178e8ae645SJohannes Weiner 			 * pressure events can occur.
3188e8ae645SJohannes Weiner 			 */
3197e6ec49cSYuanzheng Song 			WRITE_ONCE(memcg->socket_pressure, jiffies + HZ);
3208e8ae645SJohannes Weiner 		}
3218e8ae645SJohannes Weiner 	}
32270ddf637SAnton Vorontsov }
32370ddf637SAnton Vorontsov 
32470ddf637SAnton Vorontsov /**
32570ddf637SAnton Vorontsov  * vmpressure_prio() - Account memory pressure through reclaimer priority level
32670ddf637SAnton Vorontsov  * @gfp:	reclaimer's gfp mask
32770ddf637SAnton Vorontsov  * @memcg:	cgroup memory controller handle
32870ddf637SAnton Vorontsov  * @prio:	reclaimer's priority
32970ddf637SAnton Vorontsov  *
33070ddf637SAnton Vorontsov  * This function should be called from the reclaim path every time when
33170ddf637SAnton Vorontsov  * the vmscan's reclaiming priority (scanning depth) changes.
33270ddf637SAnton Vorontsov  *
33370ddf637SAnton Vorontsov  * This function does not return any value.
33470ddf637SAnton Vorontsov  */
vmpressure_prio(gfp_t gfp,struct mem_cgroup * memcg,int prio)33570ddf637SAnton Vorontsov void vmpressure_prio(gfp_t gfp, struct mem_cgroup *memcg, int prio)
33670ddf637SAnton Vorontsov {
33770ddf637SAnton Vorontsov 	/*
33870ddf637SAnton Vorontsov 	 * We only use prio for accounting critical level. For more info
33970ddf637SAnton Vorontsov 	 * see comment for vmpressure_level_critical_prio variable above.
34070ddf637SAnton Vorontsov 	 */
34170ddf637SAnton Vorontsov 	if (prio > vmpressure_level_critical_prio)
34270ddf637SAnton Vorontsov 		return;
34370ddf637SAnton Vorontsov 
34470ddf637SAnton Vorontsov 	/*
34570ddf637SAnton Vorontsov 	 * OK, the prio is below the threshold, updating vmpressure
34670ddf637SAnton Vorontsov 	 * information before shrinker dives into long shrinking of long
34770ddf637SAnton Vorontsov 	 * range vmscan. Passing scanned = vmpressure_win, reclaimed = 0
34870ddf637SAnton Vorontsov 	 * to the vmpressure() basically means that we signal 'critical'
34970ddf637SAnton Vorontsov 	 * level.
35070ddf637SAnton Vorontsov 	 */
3518e8ae645SJohannes Weiner 	vmpressure(gfp, memcg, true, vmpressure_win, 0);
35270ddf637SAnton Vorontsov }
35370ddf637SAnton Vorontsov 
354b6bb9811SDavid Rientjes #define MAX_VMPRESSURE_ARGS_LEN	(strlen("critical") + strlen("hierarchy") + 2)
355b6bb9811SDavid Rientjes 
35670ddf637SAnton Vorontsov /**
35770ddf637SAnton Vorontsov  * vmpressure_register_event() - Bind vmpressure notifications to an eventfd
35859b6f873STejun Heo  * @memcg:	memcg that is interested in vmpressure notifications
35970ddf637SAnton Vorontsov  * @eventfd:	eventfd context to link notifications with
360b6bb9811SDavid Rientjes  * @args:	event arguments (pressure level threshold, optional mode)
36170ddf637SAnton Vorontsov  *
36270ddf637SAnton Vorontsov  * This function associates eventfd context with the vmpressure
36370ddf637SAnton Vorontsov  * infrastructure, so that the notifications will be delivered to the
364b6bb9811SDavid Rientjes  * @eventfd. The @args parameter is a comma-delimited string that denotes a
365b6bb9811SDavid Rientjes  * pressure level threshold (one of vmpressure_str_levels, i.e. "low", "medium",
366b6bb9811SDavid Rientjes  * or "critical") and an optional mode (one of vmpressure_str_modes, i.e.
367b6bb9811SDavid Rientjes  * "hierarchy" or "local").
36870ddf637SAnton Vorontsov  *
369347c4a87STejun Heo  * To be used as memcg event method.
370518a8671SDan Carpenter  *
371518a8671SDan Carpenter  * Return: 0 on success, -ENOMEM on memory failure or -EINVAL if @args could
372518a8671SDan Carpenter  * not be parsed.
37370ddf637SAnton Vorontsov  */
vmpressure_register_event(struct mem_cgroup * memcg,struct eventfd_ctx * eventfd,const char * args)37459b6f873STejun Heo int vmpressure_register_event(struct mem_cgroup *memcg,
375347c4a87STejun Heo 			      struct eventfd_ctx *eventfd, const char *args)
37670ddf637SAnton Vorontsov {
37759b6f873STejun Heo 	struct vmpressure *vmpr = memcg_to_vmpressure(memcg);
37870ddf637SAnton Vorontsov 	struct vmpressure_event *ev;
379b6bb9811SDavid Rientjes 	enum vmpressure_modes mode = VMPRESSURE_NO_PASSTHROUGH;
380518a8671SDan Carpenter 	enum vmpressure_levels level;
381b6bb9811SDavid Rientjes 	char *spec, *spec_orig;
382b6bb9811SDavid Rientjes 	char *token;
383b6bb9811SDavid Rientjes 	int ret = 0;
38470ddf637SAnton Vorontsov 
385d62ff365SAndy Shevchenko 	spec_orig = spec = kstrndup(args, MAX_VMPRESSURE_ARGS_LEN, GFP_KERNEL);
386565dc842SYang Shi 	if (!spec)
387565dc842SYang Shi 		return -ENOMEM;
388b6bb9811SDavid Rientjes 
389b6bb9811SDavid Rientjes 	/* Find required level */
390b6bb9811SDavid Rientjes 	token = strsep(&spec, ",");
391518a8671SDan Carpenter 	ret = match_string(vmpressure_str_levels, VMPRESSURE_NUM_LEVELS, token);
392518a8671SDan Carpenter 	if (ret < 0)
393b6bb9811SDavid Rientjes 		goto out;
394518a8671SDan Carpenter 	level = ret;
39570ddf637SAnton Vorontsov 
396b6bb9811SDavid Rientjes 	/* Find optional mode */
397b6bb9811SDavid Rientjes 	token = strsep(&spec, ",");
398b6bb9811SDavid Rientjes 	if (token) {
399518a8671SDan Carpenter 		ret = match_string(vmpressure_str_modes, VMPRESSURE_NUM_MODES, token);
400518a8671SDan Carpenter 		if (ret < 0)
401b6bb9811SDavid Rientjes 			goto out;
402518a8671SDan Carpenter 		mode = ret;
403b6bb9811SDavid Rientjes 	}
40470ddf637SAnton Vorontsov 
40570ddf637SAnton Vorontsov 	ev = kzalloc(sizeof(*ev), GFP_KERNEL);
406b6bb9811SDavid Rientjes 	if (!ev) {
407b6bb9811SDavid Rientjes 		ret = -ENOMEM;
408b6bb9811SDavid Rientjes 		goto out;
409b6bb9811SDavid Rientjes 	}
41070ddf637SAnton Vorontsov 
41170ddf637SAnton Vorontsov 	ev->efd = eventfd;
41270ddf637SAnton Vorontsov 	ev->level = level;
413b6bb9811SDavid Rientjes 	ev->mode = mode;
41470ddf637SAnton Vorontsov 
41570ddf637SAnton Vorontsov 	mutex_lock(&vmpr->events_lock);
41670ddf637SAnton Vorontsov 	list_add(&ev->node, &vmpr->events);
41770ddf637SAnton Vorontsov 	mutex_unlock(&vmpr->events_lock);
418518a8671SDan Carpenter 	ret = 0;
419b6bb9811SDavid Rientjes out:
420b6bb9811SDavid Rientjes 	kfree(spec_orig);
421b6bb9811SDavid Rientjes 	return ret;
42270ddf637SAnton Vorontsov }
42370ddf637SAnton Vorontsov 
42470ddf637SAnton Vorontsov /**
42570ddf637SAnton Vorontsov  * vmpressure_unregister_event() - Unbind eventfd from vmpressure
42659b6f873STejun Heo  * @memcg:	memcg handle
42770ddf637SAnton Vorontsov  * @eventfd:	eventfd context that was used to link vmpressure with the @cg
42870ddf637SAnton Vorontsov  *
42970ddf637SAnton Vorontsov  * This function does internal manipulations to detach the @eventfd from
43070ddf637SAnton Vorontsov  * the vmpressure notifications, and then frees internal resources
43170ddf637SAnton Vorontsov  * associated with the @eventfd (but the @eventfd itself is not freed).
43270ddf637SAnton Vorontsov  *
433347c4a87STejun Heo  * To be used as memcg event method.
43470ddf637SAnton Vorontsov  */
vmpressure_unregister_event(struct mem_cgroup * memcg,struct eventfd_ctx * eventfd)43559b6f873STejun Heo void vmpressure_unregister_event(struct mem_cgroup *memcg,
43670ddf637SAnton Vorontsov 				 struct eventfd_ctx *eventfd)
43770ddf637SAnton Vorontsov {
43859b6f873STejun Heo 	struct vmpressure *vmpr = memcg_to_vmpressure(memcg);
43970ddf637SAnton Vorontsov 	struct vmpressure_event *ev;
44070ddf637SAnton Vorontsov 
44170ddf637SAnton Vorontsov 	mutex_lock(&vmpr->events_lock);
44270ddf637SAnton Vorontsov 	list_for_each_entry(ev, &vmpr->events, node) {
44370ddf637SAnton Vorontsov 		if (ev->efd != eventfd)
44470ddf637SAnton Vorontsov 			continue;
44570ddf637SAnton Vorontsov 		list_del(&ev->node);
44670ddf637SAnton Vorontsov 		kfree(ev);
44770ddf637SAnton Vorontsov 		break;
44870ddf637SAnton Vorontsov 	}
44970ddf637SAnton Vorontsov 	mutex_unlock(&vmpr->events_lock);
45070ddf637SAnton Vorontsov }
45170ddf637SAnton Vorontsov 
45270ddf637SAnton Vorontsov /**
45370ddf637SAnton Vorontsov  * vmpressure_init() - Initialize vmpressure control structure
45470ddf637SAnton Vorontsov  * @vmpr:	Structure to be initialized
45570ddf637SAnton Vorontsov  *
45670ddf637SAnton Vorontsov  * This function should be called on every allocated vmpressure structure
45770ddf637SAnton Vorontsov  * before any usage.
45870ddf637SAnton Vorontsov  */
vmpressure_init(struct vmpressure * vmpr)45970ddf637SAnton Vorontsov void vmpressure_init(struct vmpressure *vmpr)
46070ddf637SAnton Vorontsov {
46122f2020fSMichal Hocko 	spin_lock_init(&vmpr->sr_lock);
46270ddf637SAnton Vorontsov 	mutex_init(&vmpr->events_lock);
46370ddf637SAnton Vorontsov 	INIT_LIST_HEAD(&vmpr->events);
46470ddf637SAnton Vorontsov 	INIT_WORK(&vmpr->work, vmpressure_work_fn);
46570ddf637SAnton Vorontsov }
46633cb876eSMichal Hocko 
46733cb876eSMichal Hocko /**
46833cb876eSMichal Hocko  * vmpressure_cleanup() - shuts down vmpressure control structure
46933cb876eSMichal Hocko  * @vmpr:	Structure to be cleaned up
47033cb876eSMichal Hocko  *
47133cb876eSMichal Hocko  * This function should be called before the structure in which it is
47233cb876eSMichal Hocko  * embedded is cleaned up.
47333cb876eSMichal Hocko  */
vmpressure_cleanup(struct vmpressure * vmpr)47433cb876eSMichal Hocko void vmpressure_cleanup(struct vmpressure *vmpr)
47533cb876eSMichal Hocko {
47633cb876eSMichal Hocko 	/*
47733cb876eSMichal Hocko 	 * Make sure there is no pending work before eventfd infrastructure
47833cb876eSMichal Hocko 	 * goes away.
47933cb876eSMichal Hocko 	 */
48033cb876eSMichal Hocko 	flush_work(&vmpr->work);
48133cb876eSMichal Hocko }
482