userfaultfd: add new syscall to provide memory externalization
[linux-2.6/btrfs-unstable.git] / mm / vmpressure.c
blobc5afd573d7da79afc814225043319cc66120addf
1 /*
2 * Linux VM pressure
4 * Copyright 2012 Linaro Ltd.
5 * Anton Vorontsov <anton.vorontsov@linaro.org>
7 * Based on ideas from Andrew Morton, David Rientjes, KOSAKI Motohiro,
8 * Leonid Moiseichuk, Mel Gorman, Minchan Kim and Pekka Enberg.
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.
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>
22 #include <linux/slab.h>
23 #include <linux/swap.h>
24 #include <linux/printk.h>
25 #include <linux/vmpressure.h>
28 * The window size (vmpressure_win) is the number of scanned pages before
29 * we try to analyze scanned/reclaimed ratio. So the window is used as a
30 * rate-limit tunable for the "low" level notification, and also for
31 * averaging the ratio for medium/critical levels. Using small window
32 * sizes can cause lot of false positives, but too big window size will
33 * delay the notifications.
35 * As the vmscan reclaimer logic works with chunks which are multiple of
36 * SWAP_CLUSTER_MAX, it makes sense to use it for the window size as well.
38 * TODO: Make the window size depend on machine size, as we do for vmstat
39 * thresholds. Currently we set it to 512 pages (2MB for 4KB pages).
41 static const unsigned long vmpressure_win = SWAP_CLUSTER_MAX * 16;
44 * These thresholds are used when we account memory pressure through
45 * scanned/reclaimed ratio. The current values were chosen empirically. In
46 * essence, they are percents: the higher the value, the more number
47 * unsuccessful reclaims there were.
49 static const unsigned int vmpressure_level_med = 60;
50 static const unsigned int vmpressure_level_critical = 95;
53 * When there are too little pages left to scan, vmpressure() may miss the
54 * critical pressure as number of pages will be less than "window size".
55 * However, in that case the vmscan priority will raise fast as the
56 * reclaimer will try to scan LRUs more deeply.
58 * The vmscan logic considers these special priorities:
60 * prio == DEF_PRIORITY (12): reclaimer starts with that value
61 * prio <= DEF_PRIORITY - 2 : kswapd becomes somewhat overwhelmed
62 * prio == 0 : close to OOM, kernel scans every page in an lru
64 * Any value in this range is acceptable for this tunable (i.e. from 12 to
65 * 0). Current value for the vmpressure_level_critical_prio is chosen
66 * empirically, but the number, in essence, means that we consider
67 * critical level when scanning depth is ~10% of the lru size (vmscan
68 * scans 'lru_size >> prio' pages, so it is actually 12.5%, or one
69 * eights).
71 static const unsigned int vmpressure_level_critical_prio = ilog2(100 / 10);
73 static struct vmpressure *work_to_vmpressure(struct work_struct *work)
75 return container_of(work, struct vmpressure, work);
78 static struct vmpressure *vmpressure_parent(struct vmpressure *vmpr)
80 struct cgroup_subsys_state *css = vmpressure_to_css(vmpr);
81 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
83 memcg = parent_mem_cgroup(memcg);
84 if (!memcg)
85 return NULL;
86 return memcg_to_vmpressure(memcg);
89 enum vmpressure_levels {
90 VMPRESSURE_LOW = 0,
91 VMPRESSURE_MEDIUM,
92 VMPRESSURE_CRITICAL,
93 VMPRESSURE_NUM_LEVELS,
96 static const char * const vmpressure_str_levels[] = {
97 [VMPRESSURE_LOW] = "low",
98 [VMPRESSURE_MEDIUM] = "medium",
99 [VMPRESSURE_CRITICAL] = "critical",
102 static enum vmpressure_levels vmpressure_level(unsigned long pressure)
104 if (pressure >= vmpressure_level_critical)
105 return VMPRESSURE_CRITICAL;
106 else if (pressure >= vmpressure_level_med)
107 return VMPRESSURE_MEDIUM;
108 return VMPRESSURE_LOW;
111 static enum vmpressure_levels vmpressure_calc_level(unsigned long scanned,
112 unsigned long reclaimed)
114 unsigned long scale = scanned + reclaimed;
115 unsigned long pressure;
118 * We calculate the ratio (in percents) of how many pages were
119 * scanned vs. reclaimed in a given time frame (window). Note that
120 * time is in VM reclaimer's "ticks", i.e. number of pages
121 * scanned. This makes it possible to set desired reaction time
122 * and serves as a ratelimit.
124 pressure = scale - (reclaimed * scale / scanned);
125 pressure = pressure * 100 / scale;
127 pr_debug("%s: %3lu (s: %lu r: %lu)\n", __func__, pressure,
128 scanned, reclaimed);
130 return vmpressure_level(pressure);
133 struct vmpressure_event {
134 struct eventfd_ctx *efd;
135 enum vmpressure_levels level;
136 struct list_head node;
139 static bool vmpressure_event(struct vmpressure *vmpr,
140 unsigned long scanned, unsigned long reclaimed)
142 struct vmpressure_event *ev;
143 enum vmpressure_levels level;
144 bool signalled = false;
146 level = vmpressure_calc_level(scanned, reclaimed);
148 mutex_lock(&vmpr->events_lock);
150 list_for_each_entry(ev, &vmpr->events, node) {
151 if (level >= ev->level) {
152 eventfd_signal(ev->efd, 1);
153 signalled = true;
157 mutex_unlock(&vmpr->events_lock);
159 return signalled;
162 static void vmpressure_work_fn(struct work_struct *work)
164 struct vmpressure *vmpr = work_to_vmpressure(work);
165 unsigned long scanned;
166 unsigned long reclaimed;
168 spin_lock(&vmpr->sr_lock);
170 * Several contexts might be calling vmpressure(), so it is
171 * possible that the work was rescheduled again before the old
172 * work context cleared the counters. In that case we will run
173 * just after the old work returns, but then scanned might be zero
174 * here. No need for any locks here since we don't care if
175 * vmpr->reclaimed is in sync.
177 scanned = vmpr->scanned;
178 if (!scanned) {
179 spin_unlock(&vmpr->sr_lock);
180 return;
183 reclaimed = vmpr->reclaimed;
184 vmpr->scanned = 0;
185 vmpr->reclaimed = 0;
186 spin_unlock(&vmpr->sr_lock);
188 do {
189 if (vmpressure_event(vmpr, scanned, reclaimed))
190 break;
192 * If not handled, propagate the event upward into the
193 * hierarchy.
195 } while ((vmpr = vmpressure_parent(vmpr)));
199 * vmpressure() - Account memory pressure through scanned/reclaimed ratio
200 * @gfp: reclaimer's gfp mask
201 * @memcg: cgroup memory controller handle
202 * @scanned: number of pages scanned
203 * @reclaimed: number of pages reclaimed
205 * This function should be called from the vmscan reclaim path to account
206 * "instantaneous" memory pressure (scanned/reclaimed ratio). The raw
207 * pressure index is then further refined and averaged over time.
209 * This function does not return any value.
211 void vmpressure(gfp_t gfp, struct mem_cgroup *memcg,
212 unsigned long scanned, unsigned long reclaimed)
214 struct vmpressure *vmpr = memcg_to_vmpressure(memcg);
217 * Here we only want to account pressure that userland is able to
218 * help us with. For example, suppose that DMA zone is under
219 * pressure; if we notify userland about that kind of pressure,
220 * then it will be mostly a waste as it will trigger unnecessary
221 * freeing of memory by userland (since userland is more likely to
222 * have HIGHMEM/MOVABLE pages instead of the DMA fallback). That
223 * is why we include only movable, highmem and FS/IO pages.
224 * Indirect reclaim (kswapd) sets sc->gfp_mask to GFP_KERNEL, so
225 * we account it too.
227 if (!(gfp & (__GFP_HIGHMEM | __GFP_MOVABLE | __GFP_IO | __GFP_FS)))
228 return;
231 * If we got here with no pages scanned, then that is an indicator
232 * that reclaimer was unable to find any shrinkable LRUs at the
233 * current scanning depth. But it does not mean that we should
234 * report the critical pressure, yet. If the scanning priority
235 * (scanning depth) goes too high (deep), we will be notified
236 * through vmpressure_prio(). But so far, keep calm.
238 if (!scanned)
239 return;
241 spin_lock(&vmpr->sr_lock);
242 vmpr->scanned += scanned;
243 vmpr->reclaimed += reclaimed;
244 scanned = vmpr->scanned;
245 spin_unlock(&vmpr->sr_lock);
247 if (scanned < vmpressure_win)
248 return;
249 schedule_work(&vmpr->work);
253 * vmpressure_prio() - Account memory pressure through reclaimer priority level
254 * @gfp: reclaimer's gfp mask
255 * @memcg: cgroup memory controller handle
256 * @prio: reclaimer's priority
258 * This function should be called from the reclaim path every time when
259 * the vmscan's reclaiming priority (scanning depth) changes.
261 * This function does not return any value.
263 void vmpressure_prio(gfp_t gfp, struct mem_cgroup *memcg, int prio)
266 * We only use prio for accounting critical level. For more info
267 * see comment for vmpressure_level_critical_prio variable above.
269 if (prio > vmpressure_level_critical_prio)
270 return;
273 * OK, the prio is below the threshold, updating vmpressure
274 * information before shrinker dives into long shrinking of long
275 * range vmscan. Passing scanned = vmpressure_win, reclaimed = 0
276 * to the vmpressure() basically means that we signal 'critical'
277 * level.
279 vmpressure(gfp, memcg, vmpressure_win, 0);
283 * vmpressure_register_event() - Bind vmpressure notifications to an eventfd
284 * @memcg: memcg that is interested in vmpressure notifications
285 * @eventfd: eventfd context to link notifications with
286 * @args: event arguments (used to set up a pressure level threshold)
288 * This function associates eventfd context with the vmpressure
289 * infrastructure, so that the notifications will be delivered to the
290 * @eventfd. The @args parameter is a string that denotes pressure level
291 * threshold (one of vmpressure_str_levels, i.e. "low", "medium", or
292 * "critical").
294 * To be used as memcg event method.
296 int vmpressure_register_event(struct mem_cgroup *memcg,
297 struct eventfd_ctx *eventfd, const char *args)
299 struct vmpressure *vmpr = memcg_to_vmpressure(memcg);
300 struct vmpressure_event *ev;
301 int level;
303 for (level = 0; level < VMPRESSURE_NUM_LEVELS; level++) {
304 if (!strcmp(vmpressure_str_levels[level], args))
305 break;
308 if (level >= VMPRESSURE_NUM_LEVELS)
309 return -EINVAL;
311 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
312 if (!ev)
313 return -ENOMEM;
315 ev->efd = eventfd;
316 ev->level = level;
318 mutex_lock(&vmpr->events_lock);
319 list_add(&ev->node, &vmpr->events);
320 mutex_unlock(&vmpr->events_lock);
322 return 0;
326 * vmpressure_unregister_event() - Unbind eventfd from vmpressure
327 * @memcg: memcg handle
328 * @eventfd: eventfd context that was used to link vmpressure with the @cg
330 * This function does internal manipulations to detach the @eventfd from
331 * the vmpressure notifications, and then frees internal resources
332 * associated with the @eventfd (but the @eventfd itself is not freed).
334 * To be used as memcg event method.
336 void vmpressure_unregister_event(struct mem_cgroup *memcg,
337 struct eventfd_ctx *eventfd)
339 struct vmpressure *vmpr = memcg_to_vmpressure(memcg);
340 struct vmpressure_event *ev;
342 mutex_lock(&vmpr->events_lock);
343 list_for_each_entry(ev, &vmpr->events, node) {
344 if (ev->efd != eventfd)
345 continue;
346 list_del(&ev->node);
347 kfree(ev);
348 break;
350 mutex_unlock(&vmpr->events_lock);
354 * vmpressure_init() - Initialize vmpressure control structure
355 * @vmpr: Structure to be initialized
357 * This function should be called on every allocated vmpressure structure
358 * before any usage.
360 void vmpressure_init(struct vmpressure *vmpr)
362 spin_lock_init(&vmpr->sr_lock);
363 mutex_init(&vmpr->events_lock);
364 INIT_LIST_HEAD(&vmpr->events);
365 INIT_WORK(&vmpr->work, vmpressure_work_fn);
369 * vmpressure_cleanup() - shuts down vmpressure control structure
370 * @vmpr: Structure to be cleaned up
372 * This function should be called before the structure in which it is
373 * embedded is cleaned up.
375 void vmpressure_cleanup(struct vmpressure *vmpr)
378 * Make sure there is no pending work before eventfd infrastructure
379 * goes away.
381 flush_work(&vmpr->work);