blob: 1306f326620df09b0778ef67a6440fa7741c24a6 [file] [log] [blame]
Anton Vorontsov70ddf632013-04-29 15:08:31 -07001/*
2 * Linux VM pressure
3 *
4 * Copyright 2012 Linaro Ltd.
5 * Anton Vorontsov <anton.vorontsov@linaro.org>
6 *
7 * Based on ideas from Andrew Morton, David Rientjes, KOSAKI Motohiro,
8 * Leonid Moiseichuk, Mel Gorman, Minchan Kim and Pekka Enberg.
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published
12 * by the Free Software Foundation.
13 */
14
15#include <linux/cgroup.h>
16#include <linux/fs.h>
17#include <linux/log2.h>
18#include <linux/sched.h>
19#include <linux/mm.h>
20#include <linux/vmstat.h>
21#include <linux/eventfd.h>
Tejun Heo1ff6bbf2014-01-28 18:10:37 -050022#include <linux/slab.h>
Anton Vorontsov70ddf632013-04-29 15:08:31 -070023#include <linux/swap.h>
24#include <linux/printk.h>
Vinayak Menon13088ad2015-03-04 16:38:28 +053025#include <linux/notifier.h>
26#include <linux/init.h>
Vinayak Menon58091092015-03-31 11:06:29 +053027#include <linux/module.h>
Anton Vorontsov70ddf632013-04-29 15:08:31 -070028#include <linux/vmpressure.h>
29
30/*
31 * The window size (vmpressure_win) is the number of scanned pages before
32 * we try to analyze scanned/reclaimed ratio. So the window is used as a
33 * rate-limit tunable for the "low" level notification, and also for
34 * averaging the ratio for medium/critical levels. Using small window
35 * sizes can cause lot of false positives, but too big window size will
36 * delay the notifications.
37 *
38 * As the vmscan reclaimer logic works with chunks which are multiple of
39 * SWAP_CLUSTER_MAX, it makes sense to use it for the window size as well.
40 *
41 * TODO: Make the window size depend on machine size, as we do for vmstat
42 * thresholds. Currently we set it to 512 pages (2MB for 4KB pages).
43 */
Vinayak Menon9cd33022016-09-19 12:44:15 +053044static unsigned long vmpressure_win = SWAP_CLUSTER_MAX * 16;
Anton Vorontsov70ddf632013-04-29 15:08:31 -070045
46/*
47 * These thresholds are used when we account memory pressure through
48 * scanned/reclaimed ratio. The current values were chosen empirically. In
49 * essence, they are percents: the higher the value, the more number
50 * unsuccessful reclaims there were.
51 */
52static const unsigned int vmpressure_level_med = 60;
53static const unsigned int vmpressure_level_critical = 95;
54
Vinayak Menon58091092015-03-31 11:06:29 +053055static unsigned long vmpressure_scale_max = 100;
56module_param_named(vmpressure_scale_max, vmpressure_scale_max,
57 ulong, 0644);
58
Vinayak Menon44778e52015-08-19 16:16:39 +053059/* vmpressure values >= this will be scaled based on allocstalls */
60static unsigned long allocstall_threshold = 70;
61module_param_named(allocstall_threshold, allocstall_threshold,
62 ulong, 0644);
63
Vinayak Menon13088ad2015-03-04 16:38:28 +053064static struct vmpressure global_vmpressure;
65static BLOCKING_NOTIFIER_HEAD(vmpressure_notifier);
66
67int vmpressure_notifier_register(struct notifier_block *nb)
68{
69 return blocking_notifier_chain_register(&vmpressure_notifier, nb);
70}
71
72int vmpressure_notifier_unregister(struct notifier_block *nb)
73{
74 return blocking_notifier_chain_unregister(&vmpressure_notifier, nb);
75}
76
77static void vmpressure_notify(unsigned long pressure)
78{
79 blocking_notifier_call_chain(&vmpressure_notifier, pressure, NULL);
80}
81
Anton Vorontsov70ddf632013-04-29 15:08:31 -070082/*
83 * When there are too little pages left to scan, vmpressure() may miss the
84 * critical pressure as number of pages will be less than "window size".
85 * However, in that case the vmscan priority will raise fast as the
86 * reclaimer will try to scan LRUs more deeply.
87 *
88 * The vmscan logic considers these special priorities:
89 *
90 * prio == DEF_PRIORITY (12): reclaimer starts with that value
91 * prio <= DEF_PRIORITY - 2 : kswapd becomes somewhat overwhelmed
92 * prio == 0 : close to OOM, kernel scans every page in an lru
93 *
94 * Any value in this range is acceptable for this tunable (i.e. from 12 to
95 * 0). Current value for the vmpressure_level_critical_prio is chosen
96 * empirically, but the number, in essence, means that we consider
97 * critical level when scanning depth is ~10% of the lru size (vmscan
98 * scans 'lru_size >> prio' pages, so it is actually 12.5%, or one
99 * eights).
100 */
101static const unsigned int vmpressure_level_critical_prio = ilog2(100 / 10);
102
103static struct vmpressure *work_to_vmpressure(struct work_struct *work)
104{
105 return container_of(work, struct vmpressure, work);
106}
107
Vinayak Menon13088ad2015-03-04 16:38:28 +0530108#ifdef CONFIG_MEMCG
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700109static struct vmpressure *vmpressure_parent(struct vmpressure *vmpr)
110{
Tejun Heo182446d2013-08-08 20:11:24 -0400111 struct cgroup_subsys_state *css = vmpressure_to_css(vmpr);
112 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700113
114 memcg = parent_mem_cgroup(memcg);
115 if (!memcg)
116 return NULL;
117 return memcg_to_vmpressure(memcg);
118}
Vinayak Menon13088ad2015-03-04 16:38:28 +0530119#else
120static struct vmpressure *vmpressure_parent(struct vmpressure *vmpr)
121{
122 return NULL;
123}
124#endif
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700125
126enum vmpressure_levels {
127 VMPRESSURE_LOW = 0,
128 VMPRESSURE_MEDIUM,
129 VMPRESSURE_CRITICAL,
130 VMPRESSURE_NUM_LEVELS,
131};
132
133static const char * const vmpressure_str_levels[] = {
134 [VMPRESSURE_LOW] = "low",
135 [VMPRESSURE_MEDIUM] = "medium",
136 [VMPRESSURE_CRITICAL] = "critical",
137};
138
139static enum vmpressure_levels vmpressure_level(unsigned long pressure)
140{
141 if (pressure >= vmpressure_level_critical)
142 return VMPRESSURE_CRITICAL;
143 else if (pressure >= vmpressure_level_med)
144 return VMPRESSURE_MEDIUM;
145 return VMPRESSURE_LOW;
146}
147
Vinayak Menon13088ad2015-03-04 16:38:28 +0530148static unsigned long vmpressure_calc_pressure(unsigned long scanned,
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700149 unsigned long reclaimed)
150{
151 unsigned long scale = scanned + reclaimed;
Vinayak Menon58d1dbb2017-02-24 14:59:39 -0800152 unsigned long pressure = 0;
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700153
154 /*
Vinayak Menon58d1dbb2017-02-24 14:59:39 -0800155 * reclaimed can be greater than scanned in cases
156 * like THP, where the scanned is 1 and reclaimed
157 * could be 512
158 */
159 if (reclaimed >= scanned)
160 goto out;
161 /*
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700162 * We calculate the ratio (in percents) of how many pages were
163 * scanned vs. reclaimed in a given time frame (window). Note that
164 * time is in VM reclaimer's "ticks", i.e. number of pages
165 * scanned. This makes it possible to set desired reaction time
166 * and serves as a ratelimit.
167 */
168 pressure = scale - (reclaimed * scale / scanned);
169 pressure = pressure * 100 / scale;
170
Vinayak Menon58d1dbb2017-02-24 14:59:39 -0800171out:
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700172 pr_debug("%s: %3lu (s: %lu r: %lu)\n", __func__, pressure,
173 scanned, reclaimed);
174
Vinayak Menon13088ad2015-03-04 16:38:28 +0530175 return pressure;
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700176}
177
Vinayak Menon58091092015-03-31 11:06:29 +0530178static unsigned long vmpressure_account_stall(unsigned long pressure,
179 unsigned long stall, unsigned long scanned)
180{
Vinayak Menon44778e52015-08-19 16:16:39 +0530181 unsigned long scale;
182
183 if (pressure < allocstall_threshold)
184 return pressure;
185
186 scale = ((vmpressure_scale_max - pressure) * stall) / scanned;
Vinayak Menon58091092015-03-31 11:06:29 +0530187
188 return pressure + scale;
189}
190
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700191struct vmpressure_event {
192 struct eventfd_ctx *efd;
193 enum vmpressure_levels level;
194 struct list_head node;
195};
196
197static bool vmpressure_event(struct vmpressure *vmpr,
Johannes Weiner8e8ae642016-01-14 15:21:32 -0800198 enum vmpressure_levels level)
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700199{
200 struct vmpressure_event *ev;
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700201 bool signalled = false;
202
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700203 mutex_lock(&vmpr->events_lock);
204
205 list_for_each_entry(ev, &vmpr->events, node) {
206 if (level >= ev->level) {
207 eventfd_signal(ev->efd, 1);
208 signalled = true;
209 }
210 }
211
212 mutex_unlock(&vmpr->events_lock);
213
214 return signalled;
215}
216
217static void vmpressure_work_fn(struct work_struct *work)
218{
219 struct vmpressure *vmpr = work_to_vmpressure(work);
220 unsigned long scanned;
221 unsigned long reclaimed;
Vinayak Menon13088ad2015-03-04 16:38:28 +0530222 unsigned long pressure;
Johannes Weiner8e8ae642016-01-14 15:21:32 -0800223 enum vmpressure_levels level;
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700224
Andrew Morton91b57192014-12-02 15:59:28 -0800225 spin_lock(&vmpr->sr_lock);
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700226 /*
227 * Several contexts might be calling vmpressure(), so it is
228 * possible that the work was rescheduled again before the old
229 * work context cleared the counters. In that case we will run
230 * just after the old work returns, but then scanned might be zero
231 * here. No need for any locks here since we don't care if
232 * vmpr->reclaimed is in sync.
233 */
Johannes Weiner8e8ae642016-01-14 15:21:32 -0800234 scanned = vmpr->tree_scanned;
Andrew Morton91b57192014-12-02 15:59:28 -0800235 if (!scanned) {
236 spin_unlock(&vmpr->sr_lock);
237 return;
238 }
239
Johannes Weiner8e8ae642016-01-14 15:21:32 -0800240 reclaimed = vmpr->tree_reclaimed;
241 vmpr->tree_scanned = 0;
242 vmpr->tree_reclaimed = 0;
Michal Hocko22f20202013-07-31 13:53:48 -0700243 spin_unlock(&vmpr->sr_lock);
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700244
Vinayak Menon13088ad2015-03-04 16:38:28 +0530245 pressure = vmpressure_calc_pressure(scanned, reclaimed);
246 level = vmpressure_level(pressure);
Johannes Weiner8e8ae642016-01-14 15:21:32 -0800247
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700248 do {
Johannes Weiner8e8ae642016-01-14 15:21:32 -0800249 if (vmpressure_event(vmpr, level))
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700250 break;
251 /*
252 * If not handled, propagate the event upward into the
253 * hierarchy.
254 */
255 } while ((vmpr = vmpressure_parent(vmpr)));
256}
257
Vinayak Menon13088ad2015-03-04 16:38:28 +0530258#ifdef CONFIG_MEMCG
259static void vmpressure_memcg(gfp_t gfp, struct mem_cgroup *memcg, bool tree,
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700260 unsigned long scanned, unsigned long reclaimed)
261{
262 struct vmpressure *vmpr = memcg_to_vmpressure(memcg);
263
264 /*
265 * Here we only want to account pressure that userland is able to
266 * help us with. For example, suppose that DMA zone is under
267 * pressure; if we notify userland about that kind of pressure,
268 * then it will be mostly a waste as it will trigger unnecessary
269 * freeing of memory by userland (since userland is more likely to
270 * have HIGHMEM/MOVABLE pages instead of the DMA fallback). That
271 * is why we include only movable, highmem and FS/IO pages.
272 * Indirect reclaim (kswapd) sets sc->gfp_mask to GFP_KERNEL, so
273 * we account it too.
274 */
275 if (!(gfp & (__GFP_HIGHMEM | __GFP_MOVABLE | __GFP_IO | __GFP_FS)))
276 return;
277
278 /*
279 * If we got here with no pages scanned, then that is an indicator
280 * that reclaimer was unable to find any shrinkable LRUs at the
281 * current scanning depth. But it does not mean that we should
282 * report the critical pressure, yet. If the scanning priority
283 * (scanning depth) goes too high (deep), we will be notified
284 * through vmpressure_prio(). But so far, keep calm.
285 */
286 if (!scanned)
287 return;
288
Johannes Weiner8e8ae642016-01-14 15:21:32 -0800289 if (tree) {
290 spin_lock(&vmpr->sr_lock);
Vladimir Davydov3c1da7b2016-02-02 16:57:49 -0800291 scanned = vmpr->tree_scanned += scanned;
Johannes Weiner8e8ae642016-01-14 15:21:32 -0800292 vmpr->tree_reclaimed += reclaimed;
Johannes Weiner8e8ae642016-01-14 15:21:32 -0800293 spin_unlock(&vmpr->sr_lock);
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700294
Johannes Weiner8e8ae642016-01-14 15:21:32 -0800295 if (scanned < vmpressure_win)
296 return;
297 schedule_work(&vmpr->work);
298 } else {
299 enum vmpressure_levels level;
Vinayak Menon13088ad2015-03-04 16:38:28 +0530300 unsigned long pressure;
Johannes Weiner8e8ae642016-01-14 15:21:32 -0800301
302 /* For now, no users for root-level efficiency */
Hugh Dickins686739f2016-01-14 15:21:37 -0800303 if (!memcg || memcg == root_mem_cgroup)
Johannes Weiner8e8ae642016-01-14 15:21:32 -0800304 return;
305
306 spin_lock(&vmpr->sr_lock);
307 scanned = vmpr->scanned += scanned;
308 reclaimed = vmpr->reclaimed += reclaimed;
309 if (scanned < vmpressure_win) {
310 spin_unlock(&vmpr->sr_lock);
311 return;
312 }
313 vmpr->scanned = vmpr->reclaimed = 0;
314 spin_unlock(&vmpr->sr_lock);
315
Vinayak Menon13088ad2015-03-04 16:38:28 +0530316 pressure = vmpressure_calc_pressure(scanned, reclaimed);
317 level = vmpressure_level(pressure);
Johannes Weiner8e8ae642016-01-14 15:21:32 -0800318
319 if (level > VMPRESSURE_LOW) {
320 /*
321 * Let the socket buffer allocator know that
322 * we are having trouble reclaiming LRU pages.
323 *
324 * For hysteresis keep the pressure state
325 * asserted for a second in which subsequent
326 * pressure events can occur.
327 */
328 memcg->socket_pressure = jiffies + HZ;
329 }
330 }
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700331}
Vinayak Menon13088ad2015-03-04 16:38:28 +0530332#else
333static void vmpressure_memcg(gfp_t gfp, struct mem_cgroup *memcg, bool tree,
334 unsigned long scanned, unsigned long reclaimed) { }
335#endif
336
Vinayak Menon9cd33022016-09-19 12:44:15 +0530337static void calculate_vmpressure_win(void)
338{
339 long x;
340
341 x = global_node_page_state(NR_FILE_PAGES) -
342 global_node_page_state(NR_SHMEM) -
343 total_swapcache_pages() +
344 global_page_state(NR_FREE_PAGES);
345 if (x < 1)
346 x = 1;
347 /*
348 * For low (free + cached), vmpressure window should be
349 * small, and high for higher values of (free + cached).
350 * But it should not be linear as well. This ensures
351 * timely vmpressure notifications when system is under
352 * memory pressure, and optimal number of events when
353 * cached is high. The sqaure root function is empirically
354 * found to serve the purpose.
355 */
356 x = int_sqrt(x);
357 vmpressure_win = x;
358}
359
Vinayak Menon13088ad2015-03-04 16:38:28 +0530360static void vmpressure_global(gfp_t gfp, unsigned long scanned,
361 unsigned long reclaimed)
362{
363 struct vmpressure *vmpr = &global_vmpressure;
364 unsigned long pressure;
Vinayak Menon58091092015-03-31 11:06:29 +0530365 unsigned long stall;
Vinayak Menon13088ad2015-03-04 16:38:28 +0530366
367 if (!(gfp & (__GFP_HIGHMEM | __GFP_MOVABLE | __GFP_IO | __GFP_FS)))
368 return;
369
370 if (!scanned)
371 return;
372
373 spin_lock(&vmpr->sr_lock);
Vinayak Menon9cd33022016-09-19 12:44:15 +0530374 if (!vmpr->scanned)
375 calculate_vmpressure_win();
376
Vinayak Menon13088ad2015-03-04 16:38:28 +0530377 vmpr->scanned += scanned;
378 vmpr->reclaimed += reclaimed;
Vinayak Menon58091092015-03-31 11:06:29 +0530379
380 if (!current_is_kswapd())
381 vmpr->stall += scanned;
382
383 stall = vmpr->stall;
Vinayak Menon13088ad2015-03-04 16:38:28 +0530384 scanned = vmpr->scanned;
385 reclaimed = vmpr->reclaimed;
386 spin_unlock(&vmpr->sr_lock);
387
388 if (scanned < vmpressure_win)
389 return;
390
391 spin_lock(&vmpr->sr_lock);
392 vmpr->scanned = 0;
393 vmpr->reclaimed = 0;
Vinayak Menon58091092015-03-31 11:06:29 +0530394 vmpr->stall = 0;
Vinayak Menon13088ad2015-03-04 16:38:28 +0530395 spin_unlock(&vmpr->sr_lock);
396
397 pressure = vmpressure_calc_pressure(scanned, reclaimed);
Vinayak Menon58091092015-03-31 11:06:29 +0530398 pressure = vmpressure_account_stall(pressure, stall, scanned);
Vinayak Menon13088ad2015-03-04 16:38:28 +0530399 vmpressure_notify(pressure);
400}
401
402/**
403 * vmpressure() - Account memory pressure through scanned/reclaimed ratio
404 * @gfp: reclaimer's gfp mask
405 * @memcg: cgroup memory controller handle
406 * @tree: legacy subtree mode
407 * @scanned: number of pages scanned
408 * @reclaimed: number of pages reclaimed
409 *
410 * This function should be called from the vmscan reclaim path to account
411 * "instantaneous" memory pressure (scanned/reclaimed ratio). The raw
412 * pressure index is then further refined and averaged over time.
413 *
414 * If @tree is set, vmpressure is in traditional userspace reporting
415 * mode: @memcg is considered the pressure root and userspace is
416 * notified of the entire subtree's reclaim efficiency.
417 *
418 * If @tree is not set, reclaim efficiency is recorded for @memcg, and
419 * only in-kernel users are notified.
420 *
421 * This function does not return any value.
422 */
423void vmpressure(gfp_t gfp, struct mem_cgroup *memcg, bool tree,
424 unsigned long scanned, unsigned long reclaimed)
425{
426 if (!memcg)
427 vmpressure_global(gfp, scanned, reclaimed);
428
429 if (IS_ENABLED(CONFIG_MEMCG))
430 vmpressure_memcg(gfp, memcg, tree, scanned, reclaimed);
431}
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700432
433/**
434 * vmpressure_prio() - Account memory pressure through reclaimer priority level
435 * @gfp: reclaimer's gfp mask
436 * @memcg: cgroup memory controller handle
437 * @prio: reclaimer's priority
438 *
439 * This function should be called from the reclaim path every time when
440 * the vmscan's reclaiming priority (scanning depth) changes.
441 *
442 * This function does not return any value.
443 */
444void vmpressure_prio(gfp_t gfp, struct mem_cgroup *memcg, int prio)
445{
446 /*
447 * We only use prio for accounting critical level. For more info
448 * see comment for vmpressure_level_critical_prio variable above.
449 */
450 if (prio > vmpressure_level_critical_prio)
451 return;
452
453 /*
454 * OK, the prio is below the threshold, updating vmpressure
455 * information before shrinker dives into long shrinking of long
456 * range vmscan. Passing scanned = vmpressure_win, reclaimed = 0
457 * to the vmpressure() basically means that we signal 'critical'
458 * level.
459 */
Johannes Weiner8e8ae642016-01-14 15:21:32 -0800460 vmpressure(gfp, memcg, true, vmpressure_win, 0);
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700461}
462
463/**
464 * vmpressure_register_event() - Bind vmpressure notifications to an eventfd
Tejun Heo59b6f872013-11-22 18:20:43 -0500465 * @memcg: memcg that is interested in vmpressure notifications
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700466 * @eventfd: eventfd context to link notifications with
467 * @args: event arguments (used to set up a pressure level threshold)
468 *
469 * This function associates eventfd context with the vmpressure
470 * infrastructure, so that the notifications will be delivered to the
471 * @eventfd. The @args parameter is a string that denotes pressure level
472 * threshold (one of vmpressure_str_levels, i.e. "low", "medium", or
473 * "critical").
474 *
Tejun Heo347c4a82013-11-22 18:20:43 -0500475 * To be used as memcg event method.
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700476 */
Tejun Heo59b6f872013-11-22 18:20:43 -0500477int vmpressure_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -0500478 struct eventfd_ctx *eventfd, const char *args)
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700479{
Tejun Heo59b6f872013-11-22 18:20:43 -0500480 struct vmpressure *vmpr = memcg_to_vmpressure(memcg);
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700481 struct vmpressure_event *ev;
482 int level;
483
484 for (level = 0; level < VMPRESSURE_NUM_LEVELS; level++) {
485 if (!strcmp(vmpressure_str_levels[level], args))
486 break;
487 }
488
489 if (level >= VMPRESSURE_NUM_LEVELS)
490 return -EINVAL;
491
492 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
493 if (!ev)
494 return -ENOMEM;
495
496 ev->efd = eventfd;
497 ev->level = level;
498
499 mutex_lock(&vmpr->events_lock);
500 list_add(&ev->node, &vmpr->events);
501 mutex_unlock(&vmpr->events_lock);
502
503 return 0;
504}
505
506/**
507 * vmpressure_unregister_event() - Unbind eventfd from vmpressure
Tejun Heo59b6f872013-11-22 18:20:43 -0500508 * @memcg: memcg handle
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700509 * @eventfd: eventfd context that was used to link vmpressure with the @cg
510 *
511 * This function does internal manipulations to detach the @eventfd from
512 * the vmpressure notifications, and then frees internal resources
513 * associated with the @eventfd (but the @eventfd itself is not freed).
514 *
Tejun Heo347c4a82013-11-22 18:20:43 -0500515 * To be used as memcg event method.
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700516 */
Tejun Heo59b6f872013-11-22 18:20:43 -0500517void vmpressure_unregister_event(struct mem_cgroup *memcg,
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700518 struct eventfd_ctx *eventfd)
519{
Tejun Heo59b6f872013-11-22 18:20:43 -0500520 struct vmpressure *vmpr = memcg_to_vmpressure(memcg);
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700521 struct vmpressure_event *ev;
522
523 mutex_lock(&vmpr->events_lock);
524 list_for_each_entry(ev, &vmpr->events, node) {
525 if (ev->efd != eventfd)
526 continue;
527 list_del(&ev->node);
528 kfree(ev);
529 break;
530 }
531 mutex_unlock(&vmpr->events_lock);
532}
533
534/**
535 * vmpressure_init() - Initialize vmpressure control structure
536 * @vmpr: Structure to be initialized
537 *
538 * This function should be called on every allocated vmpressure structure
539 * before any usage.
540 */
541void vmpressure_init(struct vmpressure *vmpr)
542{
Michal Hocko22f20202013-07-31 13:53:48 -0700543 spin_lock_init(&vmpr->sr_lock);
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700544 mutex_init(&vmpr->events_lock);
545 INIT_LIST_HEAD(&vmpr->events);
546 INIT_WORK(&vmpr->work, vmpressure_work_fn);
547}
Michal Hocko33cb8762013-07-31 13:53:51 -0700548
549/**
550 * vmpressure_cleanup() - shuts down vmpressure control structure
551 * @vmpr: Structure to be cleaned up
552 *
553 * This function should be called before the structure in which it is
554 * embedded is cleaned up.
555 */
556void vmpressure_cleanup(struct vmpressure *vmpr)
557{
558 /*
559 * Make sure there is no pending work before eventfd infrastructure
560 * goes away.
561 */
562 flush_work(&vmpr->work);
563}
Vinayak Menon13088ad2015-03-04 16:38:28 +0530564
565static int vmpressure_global_init(void)
566{
567 vmpressure_init(&global_vmpressure);
568 return 0;
569}
570late_initcall(vmpressure_global_init);