blob: c8f48297aa7f72a1fd4e16c050f3f2f4ff0b7fc6 [file] [log] [blame]
Anton Vorontsov77a60932013-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>
22#include <linux/swap.h>
23#include <linux/printk.h>
24#include <linux/slab.h>
25#include <linux/vmpressure.h>
26
27/*
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.
34 *
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.
37 *
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).
40 */
41static const unsigned long vmpressure_win = SWAP_CLUSTER_MAX * 16;
42
43/*
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.
48 */
49static const unsigned int vmpressure_level_med = 60;
50static const unsigned int vmpressure_level_critical = 95;
51
52/*
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.
57 *
58 * The vmscan logic considers these special priorities:
59 *
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
63 *
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).
70 */
71static const unsigned int vmpressure_level_critical_prio = ilog2(100 / 10);
72
73static struct vmpressure *work_to_vmpressure(struct work_struct *work)
74{
75 return container_of(work, struct vmpressure, work);
76}
77
78static struct vmpressure *cg_to_vmpressure(struct cgroup *cg)
79{
80 return css_to_vmpressure(cgroup_subsys_state(cg, mem_cgroup_subsys_id));
81}
82
83static struct vmpressure *vmpressure_parent(struct vmpressure *vmpr)
84{
85 struct cgroup *cg = vmpressure_to_css(vmpr)->cgroup;
86 struct mem_cgroup *memcg = mem_cgroup_from_cont(cg);
87
88 memcg = parent_mem_cgroup(memcg);
89 if (!memcg)
90 return NULL;
91 return memcg_to_vmpressure(memcg);
92}
93
94enum vmpressure_levels {
95 VMPRESSURE_LOW = 0,
96 VMPRESSURE_MEDIUM,
97 VMPRESSURE_CRITICAL,
98 VMPRESSURE_NUM_LEVELS,
99};
100
101static const char * const vmpressure_str_levels[] = {
102 [VMPRESSURE_LOW] = "low",
103 [VMPRESSURE_MEDIUM] = "medium",
104 [VMPRESSURE_CRITICAL] = "critical",
105};
106
107static enum vmpressure_levels vmpressure_level(unsigned long pressure)
108{
109 if (pressure >= vmpressure_level_critical)
110 return VMPRESSURE_CRITICAL;
111 else if (pressure >= vmpressure_level_med)
112 return VMPRESSURE_MEDIUM;
113 return VMPRESSURE_LOW;
114}
115
116static enum vmpressure_levels vmpressure_calc_level(unsigned long scanned,
117 unsigned long reclaimed)
118{
119 unsigned long scale = scanned + reclaimed;
120 unsigned long pressure;
121
122 /*
123 * We calculate the ratio (in percents) of how many pages were
124 * scanned vs. reclaimed in a given time frame (window). Note that
125 * time is in VM reclaimer's "ticks", i.e. number of pages
126 * scanned. This makes it possible to set desired reaction time
127 * and serves as a ratelimit.
128 */
129 pressure = scale - (reclaimed * scale / scanned);
130 pressure = pressure * 100 / scale;
131
132 pr_debug("%s: %3lu (s: %lu r: %lu)\n", __func__, pressure,
133 scanned, reclaimed);
134
135 return vmpressure_level(pressure);
136}
137
138struct vmpressure_event {
139 struct eventfd_ctx *efd;
140 enum vmpressure_levels level;
141 struct list_head node;
142};
143
144static bool vmpressure_event(struct vmpressure *vmpr,
145 unsigned long scanned, unsigned long reclaimed)
146{
147 struct vmpressure_event *ev;
148 enum vmpressure_levels level;
149 bool signalled = false;
150
151 level = vmpressure_calc_level(scanned, reclaimed);
152
153 mutex_lock(&vmpr->events_lock);
154
155 list_for_each_entry(ev, &vmpr->events, node) {
156 if (level >= ev->level) {
157 eventfd_signal(ev->efd, 1);
158 signalled = true;
159 }
160 }
161
162 mutex_unlock(&vmpr->events_lock);
163
164 return signalled;
165}
166
167static void vmpressure_work_fn(struct work_struct *work)
168{
169 struct vmpressure *vmpr = work_to_vmpressure(work);
170 unsigned long scanned;
171 unsigned long reclaimed;
172
173 /*
174 * Several contexts might be calling vmpressure(), so it is
175 * possible that the work was rescheduled again before the old
176 * work context cleared the counters. In that case we will run
177 * just after the old work returns, but then scanned might be zero
178 * here. No need for any locks here since we don't care if
179 * vmpr->reclaimed is in sync.
180 */
181 if (!vmpr->scanned)
182 return;
183
184 mutex_lock(&vmpr->sr_lock);
185 scanned = vmpr->scanned;
186 reclaimed = vmpr->reclaimed;
187 vmpr->scanned = 0;
188 vmpr->reclaimed = 0;
189 mutex_unlock(&vmpr->sr_lock);
190
191 do {
192 if (vmpressure_event(vmpr, scanned, reclaimed))
193 break;
194 /*
195 * If not handled, propagate the event upward into the
196 * hierarchy.
197 */
198 } while ((vmpr = vmpressure_parent(vmpr)));
199}
200
201/**
202 * vmpressure() - Account memory pressure through scanned/reclaimed ratio
203 * @gfp: reclaimer's gfp mask
204 * @memcg: cgroup memory controller handle
205 * @scanned: number of pages scanned
206 * @reclaimed: number of pages reclaimed
207 *
208 * This function should be called from the vmscan reclaim path to account
209 * "instantaneous" memory pressure (scanned/reclaimed ratio). The raw
210 * pressure index is then further refined and averaged over time.
211 *
212 * This function does not return any value.
213 */
214void vmpressure(gfp_t gfp, struct mem_cgroup *memcg,
215 unsigned long scanned, unsigned long reclaimed)
216{
217 struct vmpressure *vmpr = memcg_to_vmpressure(memcg);
218
219 /*
220 * Here we only want to account pressure that userland is able to
221 * help us with. For example, suppose that DMA zone is under
222 * pressure; if we notify userland about that kind of pressure,
223 * then it will be mostly a waste as it will trigger unnecessary
224 * freeing of memory by userland (since userland is more likely to
225 * have HIGHMEM/MOVABLE pages instead of the DMA fallback). That
226 * is why we include only movable, highmem and FS/IO pages.
227 * Indirect reclaim (kswapd) sets sc->gfp_mask to GFP_KERNEL, so
228 * we account it too.
229 */
230 if (!(gfp & (__GFP_HIGHMEM | __GFP_MOVABLE | __GFP_IO | __GFP_FS)))
231 return;
232
233 /*
234 * If we got here with no pages scanned, then that is an indicator
235 * that reclaimer was unable to find any shrinkable LRUs at the
236 * current scanning depth. But it does not mean that we should
237 * report the critical pressure, yet. If the scanning priority
238 * (scanning depth) goes too high (deep), we will be notified
239 * through vmpressure_prio(). But so far, keep calm.
240 */
241 if (!scanned)
242 return;
243
244 mutex_lock(&vmpr->sr_lock);
245 vmpr->scanned += scanned;
246 vmpr->reclaimed += reclaimed;
247 scanned = vmpr->scanned;
248 mutex_unlock(&vmpr->sr_lock);
249
250 if (scanned < vmpressure_win || work_pending(&vmpr->work))
251 return;
252 schedule_work(&vmpr->work);
253}
254
255/**
256 * vmpressure_prio() - Account memory pressure through reclaimer priority level
257 * @gfp: reclaimer's gfp mask
258 * @memcg: cgroup memory controller handle
259 * @prio: reclaimer's priority
260 *
261 * This function should be called from the reclaim path every time when
262 * the vmscan's reclaiming priority (scanning depth) changes.
263 *
264 * This function does not return any value.
265 */
266void vmpressure_prio(gfp_t gfp, struct mem_cgroup *memcg, int prio)
267{
268 /*
269 * We only use prio for accounting critical level. For more info
270 * see comment for vmpressure_level_critical_prio variable above.
271 */
272 if (prio > vmpressure_level_critical_prio)
273 return;
274
275 /*
276 * OK, the prio is below the threshold, updating vmpressure
277 * information before shrinker dives into long shrinking of long
278 * range vmscan. Passing scanned = vmpressure_win, reclaimed = 0
279 * to the vmpressure() basically means that we signal 'critical'
280 * level.
281 */
282 vmpressure(gfp, memcg, vmpressure_win, 0);
283}
284
285/**
286 * vmpressure_register_event() - Bind vmpressure notifications to an eventfd
287 * @cg: cgroup that is interested in vmpressure notifications
288 * @cft: cgroup control files handle
289 * @eventfd: eventfd context to link notifications with
290 * @args: event arguments (used to set up a pressure level threshold)
291 *
292 * This function associates eventfd context with the vmpressure
293 * infrastructure, so that the notifications will be delivered to the
294 * @eventfd. The @args parameter is a string that denotes pressure level
295 * threshold (one of vmpressure_str_levels, i.e. "low", "medium", or
296 * "critical").
297 *
298 * This function should not be used directly, just pass it to (struct
299 * cftype).register_event, and then cgroup core will handle everything by
300 * itself.
301 */
302int vmpressure_register_event(struct cgroup *cg, struct cftype *cft,
303 struct eventfd_ctx *eventfd, const char *args)
304{
305 struct vmpressure *vmpr = cg_to_vmpressure(cg);
306 struct vmpressure_event *ev;
307 int level;
308
309 for (level = 0; level < VMPRESSURE_NUM_LEVELS; level++) {
310 if (!strcmp(vmpressure_str_levels[level], args))
311 break;
312 }
313
314 if (level >= VMPRESSURE_NUM_LEVELS)
315 return -EINVAL;
316
317 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
318 if (!ev)
319 return -ENOMEM;
320
321 ev->efd = eventfd;
322 ev->level = level;
323
324 mutex_lock(&vmpr->events_lock);
325 list_add(&ev->node, &vmpr->events);
326 mutex_unlock(&vmpr->events_lock);
327
328 return 0;
329}
330
331/**
332 * vmpressure_unregister_event() - Unbind eventfd from vmpressure
333 * @cg: cgroup handle
334 * @cft: cgroup control files handle
335 * @eventfd: eventfd context that was used to link vmpressure with the @cg
336 *
337 * This function does internal manipulations to detach the @eventfd from
338 * the vmpressure notifications, and then frees internal resources
339 * associated with the @eventfd (but the @eventfd itself is not freed).
340 *
341 * This function should not be used directly, just pass it to (struct
342 * cftype).unregister_event, and then cgroup core will handle everything
343 * by itself.
344 */
345void vmpressure_unregister_event(struct cgroup *cg, struct cftype *cft,
346 struct eventfd_ctx *eventfd)
347{
348 struct vmpressure *vmpr = cg_to_vmpressure(cg);
349 struct vmpressure_event *ev;
350
351 mutex_lock(&vmpr->events_lock);
352 list_for_each_entry(ev, &vmpr->events, node) {
353 if (ev->efd != eventfd)
354 continue;
355 list_del(&ev->node);
356 kfree(ev);
357 break;
358 }
359 mutex_unlock(&vmpr->events_lock);
360}
361
362/**
363 * vmpressure_init() - Initialize vmpressure control structure
364 * @vmpr: Structure to be initialized
365 *
366 * This function should be called on every allocated vmpressure structure
367 * before any usage.
368 */
369void vmpressure_init(struct vmpressure *vmpr)
370{
371 mutex_init(&vmpr->sr_lock);
372 mutex_init(&vmpr->events_lock);
373 INIT_LIST_HEAD(&vmpr->events);
374 INIT_WORK(&vmpr->work, vmpressure_work_fn);
375}