Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / xen / xen-selfballoon.c
blobe21e3cee68faa884b52b3081a9562a9ea163a634
1 /******************************************************************************
2 * Xen selfballoon driver (and optional frontswap self-shrinking driver)
4 * Copyright (c) 2009-2011, Dan Magenheimer, Oracle Corp.
6 * This code complements the cleancache and frontswap patchsets to optimize
7 * support for Xen Transcendent Memory ("tmem"). The policy it implements
8 * is rudimentary and will likely improve over time, but it does work well
9 * enough today.
11 * Two functionalities are implemented here which both use "control theory"
12 * (feedback) to optimize memory utilization. In a virtualized environment
13 * such as Xen, RAM is often a scarce resource and we would like to ensure
14 * that each of a possibly large number of virtual machines is using RAM
15 * efficiently, i.e. using as little as possible when under light load
16 * and obtaining as much as possible when memory demands are high.
17 * Since RAM needs vary highly dynamically and sometimes dramatically,
18 * "hysteresis" is used, that is, memory target is determined not just
19 * on current data but also on past data stored in the system.
21 * "Selfballooning" creates memory pressure by managing the Xen balloon
22 * driver to decrease and increase available kernel memory, driven
23 * largely by the target value of "Committed_AS" (see /proc/meminfo).
24 * Since Committed_AS does not account for clean mapped pages (i.e. pages
25 * in RAM that are identical to pages on disk), selfballooning has the
26 * affect of pushing less frequently used clean pagecache pages out of
27 * kernel RAM and, presumably using cleancache, into Xen tmem where
28 * Xen can more efficiently optimize RAM utilization for such pages.
30 * When kernel memory demand unexpectedly increases faster than Xen, via
31 * the selfballoon driver, is able to (or chooses to) provide usable RAM,
32 * the kernel may invoke swapping. In most cases, frontswap is able
33 * to absorb this swapping into Xen tmem. However, due to the fact
34 * that the kernel swap subsystem assumes swapping occurs to a disk,
35 * swapped pages may sit on the disk for a very long time; even if
36 * the kernel knows the page will never be used again. This is because
37 * the disk space costs very little and can be overwritten when
38 * necessary. When such stale pages are in frontswap, however, they
39 * are taking up valuable real estate. "Frontswap selfshrinking" works
40 * to resolve this: When frontswap activity is otherwise stable
41 * and the guest kernel is not under memory pressure, the "frontswap
42 * selfshrinking" accounts for this by providing pressure to remove some
43 * pages from frontswap and return them to kernel memory.
45 * For both "selfballooning" and "frontswap-selfshrinking", a worker
46 * thread is used and sysfs tunables are provided to adjust the frequency
47 * and rate of adjustments to achieve the goal, as well as to disable one
48 * or both functions independently.
50 * While some argue that this functionality can and should be implemented
51 * in userspace, it has been observed that bad things happen (e.g. OOMs).
53 * System configuration note: Selfballooning should not be enabled on
54 * systems without a sufficiently large swap device configured; for best
55 * results, it is recommended that total swap be increased by the size
56 * of the guest memory. Also, while technically not required to be
57 * configured, it is highly recommended that frontswap also be configured
58 * and enabled when selfballooning is running. So, selfballooning
59 * is disabled by default if frontswap is not configured and can only
60 * be enabled with the "selfballooning" kernel boot option; similarly
61 * selfballooning is enabled by default if frontswap is configured and
62 * can be disabled with the "noselfballooning" kernel boot option. Finally,
63 * when frontswap is configured, frontswap-selfshrinking can be disabled
64 * with the "noselfshrink" kernel boot option.
66 * Selfballooning is disallowed in domain0 and force-disabled.
70 #include <linux/kernel.h>
71 #include <linux/stat.h>
72 #include <linux/module.h>
73 #include <linux/mm.h>
74 #include <linux/mman.h>
75 #include <linux/module.h>
76 #include <linux/workqueue.h>
77 #include <xen/balloon.h>
78 #include <xen/tmem.h>
79 #include <xen/xen.h>
81 /* Enable/disable with sysfs. */
82 static int xen_selfballooning_enabled __read_mostly;
85 * Controls rate at which memory target (this iteration) approaches
86 * ultimate goal when memory need is increasing (up-hysteresis) or
87 * decreasing (down-hysteresis). Higher values of hysteresis cause
88 * slower increases/decreases. The default values for the various
89 * parameters were deemed reasonable by experimentation, may be
90 * workload-dependent, and can all be adjusted via sysfs.
92 static unsigned int selfballoon_downhysteresis __read_mostly = 8;
93 static unsigned int selfballoon_uphysteresis __read_mostly = 1;
95 /* In HZ, controls frequency of worker invocation. */
96 static unsigned int selfballoon_interval __read_mostly = 5;
98 static void selfballoon_process(struct work_struct *work);
99 static DECLARE_DELAYED_WORK(selfballoon_worker, selfballoon_process);
101 #ifdef CONFIG_FRONTSWAP
102 #include <linux/frontswap.h>
104 /* Enable/disable with sysfs. */
105 static bool frontswap_selfshrinking __read_mostly;
107 /* Enable/disable with kernel boot option. */
108 static bool use_frontswap_selfshrink __initdata = true;
111 * The default values for the following parameters were deemed reasonable
112 * by experimentation, may be workload-dependent, and can all be
113 * adjusted via sysfs.
116 /* Control rate for frontswap shrinking. Higher hysteresis is slower. */
117 static unsigned int frontswap_hysteresis __read_mostly = 20;
120 * Number of selfballoon worker invocations to wait before observing that
121 * frontswap selfshrinking should commence. Note that selfshrinking does
122 * not use a separate worker thread.
124 static unsigned int frontswap_inertia __read_mostly = 3;
126 /* Countdown to next invocation of frontswap_shrink() */
127 static unsigned long frontswap_inertia_counter;
130 * Invoked by the selfballoon worker thread, uses current number of pages
131 * in frontswap (frontswap_curr_pages()), previous status, and control
132 * values (hysteresis and inertia) to determine if frontswap should be
133 * shrunk and what the new frontswap size should be. Note that
134 * frontswap_shrink is essentially a partial swapoff that immediately
135 * transfers pages from the "swap device" (frontswap) back into kernel
136 * RAM; despite the name, frontswap "shrinking" is very different from
137 * the "shrinker" interface used by the kernel MM subsystem to reclaim
138 * memory.
140 static void frontswap_selfshrink(void)
142 static unsigned long cur_frontswap_pages;
143 static unsigned long last_frontswap_pages;
144 static unsigned long tgt_frontswap_pages;
146 last_frontswap_pages = cur_frontswap_pages;
147 cur_frontswap_pages = frontswap_curr_pages();
148 if (!cur_frontswap_pages ||
149 (cur_frontswap_pages > last_frontswap_pages)) {
150 frontswap_inertia_counter = frontswap_inertia;
151 return;
153 if (frontswap_inertia_counter && --frontswap_inertia_counter)
154 return;
155 if (cur_frontswap_pages <= frontswap_hysteresis)
156 tgt_frontswap_pages = 0;
157 else
158 tgt_frontswap_pages = cur_frontswap_pages -
159 (cur_frontswap_pages / frontswap_hysteresis);
160 frontswap_shrink(tgt_frontswap_pages);
163 static int __init xen_nofrontswap_selfshrink_setup(char *s)
165 use_frontswap_selfshrink = false;
166 return 1;
169 __setup("noselfshrink", xen_nofrontswap_selfshrink_setup);
171 /* Disable with kernel boot option. */
172 static bool use_selfballooning __initdata = true;
174 static int __init xen_noselfballooning_setup(char *s)
176 use_selfballooning = false;
177 return 1;
180 __setup("noselfballooning", xen_noselfballooning_setup);
181 #else /* !CONFIG_FRONTSWAP */
182 /* Enable with kernel boot option. */
183 static bool use_selfballooning __initdata = false;
185 static int __init xen_selfballooning_setup(char *s)
187 use_selfballooning = true;
188 return 1;
191 __setup("selfballooning", xen_selfballooning_setup);
192 #endif /* CONFIG_FRONTSWAP */
195 * Use current balloon size, the goal (vm_committed_as), and hysteresis
196 * parameters to set a new target balloon size
198 static void selfballoon_process(struct work_struct *work)
200 unsigned long cur_pages, goal_pages, tgt_pages;
201 bool reset_timer = false;
203 if (xen_selfballooning_enabled) {
204 cur_pages = balloon_stats.current_pages;
205 tgt_pages = cur_pages; /* default is no change */
206 goal_pages = percpu_counter_read_positive(&vm_committed_as) +
207 balloon_stats.current_pages - totalram_pages;
208 #ifdef CONFIG_FRONTSWAP
209 /* allow space for frontswap pages to be repatriated */
210 if (frontswap_selfshrinking && frontswap_enabled)
211 goal_pages += frontswap_curr_pages();
212 #endif
213 if (cur_pages > goal_pages)
214 tgt_pages = cur_pages -
215 ((cur_pages - goal_pages) /
216 selfballoon_downhysteresis);
217 else if (cur_pages < goal_pages)
218 tgt_pages = cur_pages +
219 ((goal_pages - cur_pages) /
220 selfballoon_uphysteresis);
221 /* else if cur_pages == goal_pages, no change */
222 balloon_set_new_target(tgt_pages);
223 reset_timer = true;
225 #ifdef CONFIG_FRONTSWAP
226 if (frontswap_selfshrinking && frontswap_enabled) {
227 frontswap_selfshrink();
228 reset_timer = true;
230 #endif
231 if (reset_timer)
232 schedule_delayed_work(&selfballoon_worker,
233 selfballoon_interval * HZ);
236 #ifdef CONFIG_SYSFS
238 #include <linux/sysdev.h>
239 #include <linux/capability.h>
241 #define SELFBALLOON_SHOW(name, format, args...) \
242 static ssize_t show_##name(struct sys_device *dev, \
243 struct sysdev_attribute *attr, \
244 char *buf) \
246 return sprintf(buf, format, ##args); \
249 SELFBALLOON_SHOW(selfballooning, "%d\n", xen_selfballooning_enabled);
251 static ssize_t store_selfballooning(struct sys_device *dev,
252 struct sysdev_attribute *attr,
253 const char *buf,
254 size_t count)
256 bool was_enabled = xen_selfballooning_enabled;
257 unsigned long tmp;
258 int err;
260 if (!capable(CAP_SYS_ADMIN))
261 return -EPERM;
263 err = strict_strtoul(buf, 10, &tmp);
264 if (err || ((tmp != 0) && (tmp != 1)))
265 return -EINVAL;
267 xen_selfballooning_enabled = !!tmp;
268 if (!was_enabled && xen_selfballooning_enabled)
269 schedule_delayed_work(&selfballoon_worker,
270 selfballoon_interval * HZ);
272 return count;
275 static SYSDEV_ATTR(selfballooning, S_IRUGO | S_IWUSR,
276 show_selfballooning, store_selfballooning);
278 SELFBALLOON_SHOW(selfballoon_interval, "%d\n", selfballoon_interval);
280 static ssize_t store_selfballoon_interval(struct sys_device *dev,
281 struct sysdev_attribute *attr,
282 const char *buf,
283 size_t count)
285 unsigned long val;
286 int err;
288 if (!capable(CAP_SYS_ADMIN))
289 return -EPERM;
290 err = strict_strtoul(buf, 10, &val);
291 if (err || val == 0)
292 return -EINVAL;
293 selfballoon_interval = val;
294 return count;
297 static SYSDEV_ATTR(selfballoon_interval, S_IRUGO | S_IWUSR,
298 show_selfballoon_interval, store_selfballoon_interval);
300 SELFBALLOON_SHOW(selfballoon_downhys, "%d\n", selfballoon_downhysteresis);
302 static ssize_t store_selfballoon_downhys(struct sys_device *dev,
303 struct sysdev_attribute *attr,
304 const char *buf,
305 size_t count)
307 unsigned long val;
308 int err;
310 if (!capable(CAP_SYS_ADMIN))
311 return -EPERM;
312 err = strict_strtoul(buf, 10, &val);
313 if (err || val == 0)
314 return -EINVAL;
315 selfballoon_downhysteresis = val;
316 return count;
319 static SYSDEV_ATTR(selfballoon_downhysteresis, S_IRUGO | S_IWUSR,
320 show_selfballoon_downhys, store_selfballoon_downhys);
323 SELFBALLOON_SHOW(selfballoon_uphys, "%d\n", selfballoon_uphysteresis);
325 static ssize_t store_selfballoon_uphys(struct sys_device *dev,
326 struct sysdev_attribute *attr,
327 const char *buf,
328 size_t count)
330 unsigned long val;
331 int err;
333 if (!capable(CAP_SYS_ADMIN))
334 return -EPERM;
335 err = strict_strtoul(buf, 10, &val);
336 if (err || val == 0)
337 return -EINVAL;
338 selfballoon_uphysteresis = val;
339 return count;
342 static SYSDEV_ATTR(selfballoon_uphysteresis, S_IRUGO | S_IWUSR,
343 show_selfballoon_uphys, store_selfballoon_uphys);
345 #ifdef CONFIG_FRONTSWAP
346 SELFBALLOON_SHOW(frontswap_selfshrinking, "%d\n", frontswap_selfshrinking);
348 static ssize_t store_frontswap_selfshrinking(struct sys_device *dev,
349 struct sysdev_attribute *attr,
350 const char *buf,
351 size_t count)
353 bool was_enabled = frontswap_selfshrinking;
354 unsigned long tmp;
355 int err;
357 if (!capable(CAP_SYS_ADMIN))
358 return -EPERM;
359 err = strict_strtoul(buf, 10, &tmp);
360 if (err || ((tmp != 0) && (tmp != 1)))
361 return -EINVAL;
362 frontswap_selfshrinking = !!tmp;
363 if (!was_enabled && !xen_selfballooning_enabled &&
364 frontswap_selfshrinking)
365 schedule_delayed_work(&selfballoon_worker,
366 selfballoon_interval * HZ);
368 return count;
371 static SYSDEV_ATTR(frontswap_selfshrinking, S_IRUGO | S_IWUSR,
372 show_frontswap_selfshrinking, store_frontswap_selfshrinking);
374 SELFBALLOON_SHOW(frontswap_inertia, "%d\n", frontswap_inertia);
376 static ssize_t store_frontswap_inertia(struct sys_device *dev,
377 struct sysdev_attribute *attr,
378 const char *buf,
379 size_t count)
381 unsigned long val;
382 int err;
384 if (!capable(CAP_SYS_ADMIN))
385 return -EPERM;
386 err = strict_strtoul(buf, 10, &val);
387 if (err || val == 0)
388 return -EINVAL;
389 frontswap_inertia = val;
390 frontswap_inertia_counter = val;
391 return count;
394 static SYSDEV_ATTR(frontswap_inertia, S_IRUGO | S_IWUSR,
395 show_frontswap_inertia, store_frontswap_inertia);
397 SELFBALLOON_SHOW(frontswap_hysteresis, "%d\n", frontswap_hysteresis);
399 static ssize_t store_frontswap_hysteresis(struct sys_device *dev,
400 struct sysdev_attribute *attr,
401 const char *buf,
402 size_t count)
404 unsigned long val;
405 int err;
407 if (!capable(CAP_SYS_ADMIN))
408 return -EPERM;
409 err = strict_strtoul(buf, 10, &val);
410 if (err || val == 0)
411 return -EINVAL;
412 frontswap_hysteresis = val;
413 return count;
416 static SYSDEV_ATTR(frontswap_hysteresis, S_IRUGO | S_IWUSR,
417 show_frontswap_hysteresis, store_frontswap_hysteresis);
419 #endif /* CONFIG_FRONTSWAP */
421 static struct attribute *selfballoon_attrs[] = {
422 &attr_selfballooning.attr,
423 &attr_selfballoon_interval.attr,
424 &attr_selfballoon_downhysteresis.attr,
425 &attr_selfballoon_uphysteresis.attr,
426 #ifdef CONFIG_FRONTSWAP
427 &attr_frontswap_selfshrinking.attr,
428 &attr_frontswap_hysteresis.attr,
429 &attr_frontswap_inertia.attr,
430 #endif
431 NULL
434 static struct attribute_group selfballoon_group = {
435 .name = "selfballoon",
436 .attrs = selfballoon_attrs
438 #endif
440 int register_xen_selfballooning(struct sys_device *sysdev)
442 int error = -1;
444 #ifdef CONFIG_SYSFS
445 error = sysfs_create_group(&sysdev->kobj, &selfballoon_group);
446 #endif
447 return error;
449 EXPORT_SYMBOL(register_xen_selfballooning);
451 static int __init xen_selfballoon_init(void)
453 bool enable = false;
455 if (!xen_domain())
456 return -ENODEV;
458 if (xen_initial_domain()) {
459 pr_info("xen/balloon: Xen selfballooning driver "
460 "disabled for domain0.\n");
461 return -ENODEV;
464 xen_selfballooning_enabled = tmem_enabled && use_selfballooning;
465 if (xen_selfballooning_enabled) {
466 pr_info("xen/balloon: Initializing Xen "
467 "selfballooning driver.\n");
468 enable = true;
470 #ifdef CONFIG_FRONTSWAP
471 frontswap_selfshrinking = tmem_enabled && use_frontswap_selfshrink;
472 if (frontswap_selfshrinking) {
473 pr_info("xen/balloon: Initializing frontswap "
474 "selfshrinking driver.\n");
475 enable = true;
477 #endif
478 if (!enable)
479 return -ENODEV;
481 schedule_delayed_work(&selfballoon_worker, selfballoon_interval * HZ);
483 return 0;
486 subsys_initcall(xen_selfballoon_init);
488 MODULE_LICENSE("GPL");