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
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 "tmem.selfballooning=1" kernel boot option; similarly
61 * selfballooning is enabled by default if frontswap is configured and
62 * can be disabled with the "tmem.selfballooning=0" kernel boot option. Finally,
63 * when frontswap is configured,frontswap-selfshrinking can be disabled
64 * with the "tmem.selfshrink=0" kernel boot option.
66 * Selfballooning is disallowed in domain0 and force-disabled.
70 #include <linux/kernel.h>
71 #include <linux/bootmem.h>
72 #include <linux/swap.h>
74 #include <linux/mman.h>
75 #include <linux/module.h>
76 #include <linux/workqueue.h>
77 #include <linux/device.h>
78 #include <xen/balloon.h>
82 /* Enable/disable with sysfs. */
83 static int xen_selfballooning_enabled __read_mostly;
86 * Controls rate at which memory target (this iteration) approaches
87 * ultimate goal when memory need is increasing (up-hysteresis) or
88 * decreasing (down-hysteresis). Higher values of hysteresis cause
89 * slower increases/decreases. The default values for the various
90 * parameters were deemed reasonable by experimentation, may be
91 * workload-dependent, and can all be adjusted via sysfs.
93 static unsigned int selfballoon_downhysteresis __read_mostly = 8;
94 static unsigned int selfballoon_uphysteresis __read_mostly = 1;
96 /* In HZ, controls frequency of worker invocation. */
97 static unsigned int selfballoon_interval __read_mostly = 5;
100 * Minimum usable RAM in MB for selfballooning target for balloon.
101 * If non-zero, it is added to totalreserve_pages and self-ballooning
102 * will not balloon below the sum. If zero, a piecewise linear function
103 * is calculated as a minimum and added to totalreserve_pages. Note that
104 * setting this value indiscriminately may cause OOMs and crashes.
106 static unsigned int selfballoon_min_usable_mb;
109 * Amount of RAM in MB to add to the target number of pages.
110 * Can be used to reserve some more room for caches and the like.
112 static unsigned int selfballoon_reserved_mb;
114 static void selfballoon_process(struct work_struct *work);
115 static DECLARE_DELAYED_WORK(selfballoon_worker, selfballoon_process);
117 #ifdef CONFIG_FRONTSWAP
118 #include <linux/frontswap.h>
120 /* Enable/disable with sysfs. */
121 static bool frontswap_selfshrinking __read_mostly;
124 * The default values for the following parameters were deemed reasonable
125 * by experimentation, may be workload-dependent, and can all be
126 * adjusted via sysfs.
129 /* Control rate for frontswap shrinking. Higher hysteresis is slower. */
130 static unsigned int frontswap_hysteresis __read_mostly = 20;
133 * Number of selfballoon worker invocations to wait before observing that
134 * frontswap selfshrinking should commence. Note that selfshrinking does
135 * not use a separate worker thread.
137 static unsigned int frontswap_inertia __read_mostly = 3;
139 /* Countdown to next invocation of frontswap_shrink() */
140 static unsigned long frontswap_inertia_counter;
143 * Invoked by the selfballoon worker thread, uses current number of pages
144 * in frontswap (frontswap_curr_pages()), previous status, and control
145 * values (hysteresis and inertia) to determine if frontswap should be
146 * shrunk and what the new frontswap size should be. Note that
147 * frontswap_shrink is essentially a partial swapoff that immediately
148 * transfers pages from the "swap device" (frontswap) back into kernel
149 * RAM; despite the name, frontswap "shrinking" is very different from
150 * the "shrinker" interface used by the kernel MM subsystem to reclaim
153 static void frontswap_selfshrink(void)
155 static unsigned long cur_frontswap_pages;
156 static unsigned long last_frontswap_pages;
157 static unsigned long tgt_frontswap_pages;
159 last_frontswap_pages = cur_frontswap_pages;
160 cur_frontswap_pages = frontswap_curr_pages();
161 if (!cur_frontswap_pages ||
162 (cur_frontswap_pages > last_frontswap_pages)) {
163 frontswap_inertia_counter = frontswap_inertia;
166 if (frontswap_inertia_counter && --frontswap_inertia_counter)
168 if (cur_frontswap_pages <= frontswap_hysteresis)
169 tgt_frontswap_pages = 0;
171 tgt_frontswap_pages = cur_frontswap_pages -
172 (cur_frontswap_pages / frontswap_hysteresis);
173 frontswap_shrink(tgt_frontswap_pages);
176 #endif /* CONFIG_FRONTSWAP */
178 #define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
181 * Use current balloon size, the goal (vm_committed_as), and hysteresis
182 * parameters to set a new target balloon size
184 static void selfballoon_process(struct work_struct *work)
186 unsigned long cur_pages, goal_pages, tgt_pages, floor_pages;
187 unsigned long useful_pages;
188 bool reset_timer = false;
190 if (xen_selfballooning_enabled) {
191 cur_pages = totalram_pages;
192 tgt_pages = cur_pages; /* default is no change */
193 goal_pages = vm_memory_committed() +
195 MB2PAGES(selfballoon_reserved_mb);
196 #ifdef CONFIG_FRONTSWAP
197 /* allow space for frontswap pages to be repatriated */
198 if (frontswap_selfshrinking && frontswap_enabled)
199 goal_pages += frontswap_curr_pages();
201 if (cur_pages > goal_pages)
202 tgt_pages = cur_pages -
203 ((cur_pages - goal_pages) /
204 selfballoon_downhysteresis);
205 else if (cur_pages < goal_pages)
206 tgt_pages = cur_pages +
207 ((goal_pages - cur_pages) /
208 selfballoon_uphysteresis);
209 /* else if cur_pages == goal_pages, no change */
210 useful_pages = max_pfn - totalreserve_pages;
211 if (selfballoon_min_usable_mb != 0)
212 floor_pages = totalreserve_pages +
213 MB2PAGES(selfballoon_min_usable_mb);
214 /* piecewise linear function ending in ~3% slope */
215 else if (useful_pages < MB2PAGES(16))
216 floor_pages = max_pfn; /* not worth ballooning */
217 else if (useful_pages < MB2PAGES(64))
218 floor_pages = totalreserve_pages + MB2PAGES(16) +
219 ((useful_pages - MB2PAGES(16)) >> 1);
220 else if (useful_pages < MB2PAGES(512))
221 floor_pages = totalreserve_pages + MB2PAGES(40) +
222 ((useful_pages - MB2PAGES(40)) >> 3);
223 else /* useful_pages >= MB2PAGES(512) */
224 floor_pages = totalreserve_pages + MB2PAGES(99) +
225 ((useful_pages - MB2PAGES(99)) >> 5);
226 if (tgt_pages < floor_pages)
227 tgt_pages = floor_pages;
228 balloon_set_new_target(tgt_pages +
229 balloon_stats.current_pages - totalram_pages);
232 #ifdef CONFIG_FRONTSWAP
233 if (frontswap_selfshrinking && frontswap_enabled) {
234 frontswap_selfshrink();
239 schedule_delayed_work(&selfballoon_worker,
240 selfballoon_interval * HZ);
245 #include <linux/capability.h>
247 #define SELFBALLOON_SHOW(name, format, args...) \
248 static ssize_t show_##name(struct device *dev, \
249 struct device_attribute *attr, \
252 return sprintf(buf, format, ##args); \
255 SELFBALLOON_SHOW(selfballooning, "%d\n", xen_selfballooning_enabled);
257 static ssize_t store_selfballooning(struct device *dev,
258 struct device_attribute *attr,
262 bool was_enabled = xen_selfballooning_enabled;
266 if (!capable(CAP_SYS_ADMIN))
269 err = strict_strtoul(buf, 10, &tmp);
270 if (err || ((tmp != 0) && (tmp != 1)))
273 xen_selfballooning_enabled = !!tmp;
274 if (!was_enabled && xen_selfballooning_enabled)
275 schedule_delayed_work(&selfballoon_worker,
276 selfballoon_interval * HZ);
281 static DEVICE_ATTR(selfballooning, S_IRUGO | S_IWUSR,
282 show_selfballooning, store_selfballooning);
284 SELFBALLOON_SHOW(selfballoon_interval, "%d\n", selfballoon_interval);
286 static ssize_t store_selfballoon_interval(struct device *dev,
287 struct device_attribute *attr,
294 if (!capable(CAP_SYS_ADMIN))
296 err = strict_strtoul(buf, 10, &val);
299 selfballoon_interval = val;
303 static DEVICE_ATTR(selfballoon_interval, S_IRUGO | S_IWUSR,
304 show_selfballoon_interval, store_selfballoon_interval);
306 SELFBALLOON_SHOW(selfballoon_downhys, "%d\n", selfballoon_downhysteresis);
308 static ssize_t store_selfballoon_downhys(struct device *dev,
309 struct device_attribute *attr,
316 if (!capable(CAP_SYS_ADMIN))
318 err = strict_strtoul(buf, 10, &val);
321 selfballoon_downhysteresis = val;
325 static DEVICE_ATTR(selfballoon_downhysteresis, S_IRUGO | S_IWUSR,
326 show_selfballoon_downhys, store_selfballoon_downhys);
329 SELFBALLOON_SHOW(selfballoon_uphys, "%d\n", selfballoon_uphysteresis);
331 static ssize_t store_selfballoon_uphys(struct device *dev,
332 struct device_attribute *attr,
339 if (!capable(CAP_SYS_ADMIN))
341 err = strict_strtoul(buf, 10, &val);
344 selfballoon_uphysteresis = val;
348 static DEVICE_ATTR(selfballoon_uphysteresis, S_IRUGO | S_IWUSR,
349 show_selfballoon_uphys, store_selfballoon_uphys);
351 SELFBALLOON_SHOW(selfballoon_min_usable_mb, "%d\n",
352 selfballoon_min_usable_mb);
354 static ssize_t store_selfballoon_min_usable_mb(struct device *dev,
355 struct device_attribute *attr,
362 if (!capable(CAP_SYS_ADMIN))
364 err = strict_strtoul(buf, 10, &val);
367 selfballoon_min_usable_mb = val;
371 static DEVICE_ATTR(selfballoon_min_usable_mb, S_IRUGO | S_IWUSR,
372 show_selfballoon_min_usable_mb,
373 store_selfballoon_min_usable_mb);
375 SELFBALLOON_SHOW(selfballoon_reserved_mb, "%d\n",
376 selfballoon_reserved_mb);
378 static ssize_t store_selfballoon_reserved_mb(struct device *dev,
379 struct device_attribute *attr,
386 if (!capable(CAP_SYS_ADMIN))
388 err = strict_strtoul(buf, 10, &val);
391 selfballoon_reserved_mb = val;
395 static DEVICE_ATTR(selfballoon_reserved_mb, S_IRUGO | S_IWUSR,
396 show_selfballoon_reserved_mb,
397 store_selfballoon_reserved_mb);
400 #ifdef CONFIG_FRONTSWAP
401 SELFBALLOON_SHOW(frontswap_selfshrinking, "%d\n", frontswap_selfshrinking);
403 static ssize_t store_frontswap_selfshrinking(struct device *dev,
404 struct device_attribute *attr,
408 bool was_enabled = frontswap_selfshrinking;
412 if (!capable(CAP_SYS_ADMIN))
414 err = strict_strtoul(buf, 10, &tmp);
415 if (err || ((tmp != 0) && (tmp != 1)))
417 frontswap_selfshrinking = !!tmp;
418 if (!was_enabled && !xen_selfballooning_enabled &&
419 frontswap_selfshrinking)
420 schedule_delayed_work(&selfballoon_worker,
421 selfballoon_interval * HZ);
426 static DEVICE_ATTR(frontswap_selfshrinking, S_IRUGO | S_IWUSR,
427 show_frontswap_selfshrinking, store_frontswap_selfshrinking);
429 SELFBALLOON_SHOW(frontswap_inertia, "%d\n", frontswap_inertia);
431 static ssize_t store_frontswap_inertia(struct device *dev,
432 struct device_attribute *attr,
439 if (!capable(CAP_SYS_ADMIN))
441 err = strict_strtoul(buf, 10, &val);
444 frontswap_inertia = val;
445 frontswap_inertia_counter = val;
449 static DEVICE_ATTR(frontswap_inertia, S_IRUGO | S_IWUSR,
450 show_frontswap_inertia, store_frontswap_inertia);
452 SELFBALLOON_SHOW(frontswap_hysteresis, "%d\n", frontswap_hysteresis);
454 static ssize_t store_frontswap_hysteresis(struct device *dev,
455 struct device_attribute *attr,
462 if (!capable(CAP_SYS_ADMIN))
464 err = strict_strtoul(buf, 10, &val);
467 frontswap_hysteresis = val;
471 static DEVICE_ATTR(frontswap_hysteresis, S_IRUGO | S_IWUSR,
472 show_frontswap_hysteresis, store_frontswap_hysteresis);
474 #endif /* CONFIG_FRONTSWAP */
476 static struct attribute *selfballoon_attrs[] = {
477 &dev_attr_selfballooning.attr,
478 &dev_attr_selfballoon_interval.attr,
479 &dev_attr_selfballoon_downhysteresis.attr,
480 &dev_attr_selfballoon_uphysteresis.attr,
481 &dev_attr_selfballoon_min_usable_mb.attr,
482 &dev_attr_selfballoon_reserved_mb.attr,
483 #ifdef CONFIG_FRONTSWAP
484 &dev_attr_frontswap_selfshrinking.attr,
485 &dev_attr_frontswap_hysteresis.attr,
486 &dev_attr_frontswap_inertia.attr,
491 static const struct attribute_group selfballoon_group = {
492 .name = "selfballoon",
493 .attrs = selfballoon_attrs
497 int register_xen_selfballooning(struct device *dev)
502 error = sysfs_create_group(&dev->kobj, &selfballoon_group);
506 EXPORT_SYMBOL(register_xen_selfballooning);
508 int xen_selfballoon_init(bool use_selfballooning, bool use_frontswap_selfshrink)
515 if (xen_initial_domain()) {
516 pr_info("xen/balloon: Xen selfballooning driver "
517 "disabled for domain0.\n");
521 xen_selfballooning_enabled = tmem_enabled && use_selfballooning;
522 if (xen_selfballooning_enabled) {
523 pr_info("xen/balloon: Initializing Xen "
524 "selfballooning driver.\n");
527 #ifdef CONFIG_FRONTSWAP
528 frontswap_selfshrinking = tmem_enabled && use_frontswap_selfshrink;
529 if (frontswap_selfshrinking) {
530 pr_info("xen/balloon: Initializing frontswap "
531 "selfshrinking driver.\n");
538 schedule_delayed_work(&selfballoon_worker, selfballoon_interval * HZ);
542 EXPORT_SYMBOL(xen_selfballoon_init);