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 "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>
74 #include <linux/mman.h>
75 #include <linux/workqueue.h>
76 #include <xen/balloon.h>
80 /* Enable/disable with sysfs. */
81 static int xen_selfballooning_enabled __read_mostly
;
84 * Controls rate at which memory target (this iteration) approaches
85 * ultimate goal when memory need is increasing (up-hysteresis) or
86 * decreasing (down-hysteresis). Higher values of hysteresis cause
87 * slower increases/decreases. The default values for the various
88 * parameters were deemed reasonable by experimentation, may be
89 * workload-dependent, and can all be adjusted via sysfs.
91 static unsigned int selfballoon_downhysteresis __read_mostly
= 8;
92 static unsigned int selfballoon_uphysteresis __read_mostly
= 1;
94 /* In HZ, controls frequency of worker invocation. */
95 static unsigned int selfballoon_interval __read_mostly
= 5;
97 static void selfballoon_process(struct work_struct
*work
);
98 static DECLARE_DELAYED_WORK(selfballoon_worker
, selfballoon_process
);
100 #ifdef CONFIG_FRONTSWAP
101 #include <linux/frontswap.h>
103 /* Enable/disable with sysfs. */
104 static bool frontswap_selfshrinking __read_mostly
;
106 /* Enable/disable with kernel boot option. */
107 static bool use_frontswap_selfshrink __initdata
= true;
110 * The default values for the following parameters were deemed reasonable
111 * by experimentation, may be workload-dependent, and can all be
112 * adjusted via sysfs.
115 /* Control rate for frontswap shrinking. Higher hysteresis is slower. */
116 static unsigned int frontswap_hysteresis __read_mostly
= 20;
119 * Number of selfballoon worker invocations to wait before observing that
120 * frontswap selfshrinking should commence. Note that selfshrinking does
121 * not use a separate worker thread.
123 static unsigned int frontswap_inertia __read_mostly
= 3;
125 /* Countdown to next invocation of frontswap_shrink() */
126 static unsigned long frontswap_inertia_counter
;
129 * Invoked by the selfballoon worker thread, uses current number of pages
130 * in frontswap (frontswap_curr_pages()), previous status, and control
131 * values (hysteresis and inertia) to determine if frontswap should be
132 * shrunk and what the new frontswap size should be. Note that
133 * frontswap_shrink is essentially a partial swapoff that immediately
134 * transfers pages from the "swap device" (frontswap) back into kernel
135 * RAM; despite the name, frontswap "shrinking" is very different from
136 * the "shrinker" interface used by the kernel MM subsystem to reclaim
139 static void frontswap_selfshrink(void)
141 static unsigned long cur_frontswap_pages
;
142 static unsigned long last_frontswap_pages
;
143 static unsigned long tgt_frontswap_pages
;
145 last_frontswap_pages
= cur_frontswap_pages
;
146 cur_frontswap_pages
= frontswap_curr_pages();
147 if (!cur_frontswap_pages
||
148 (cur_frontswap_pages
> last_frontswap_pages
)) {
149 frontswap_inertia_counter
= frontswap_inertia
;
152 if (frontswap_inertia_counter
&& --frontswap_inertia_counter
)
154 if (cur_frontswap_pages
<= frontswap_hysteresis
)
155 tgt_frontswap_pages
= 0;
157 tgt_frontswap_pages
= cur_frontswap_pages
-
158 (cur_frontswap_pages
/ frontswap_hysteresis
);
159 frontswap_shrink(tgt_frontswap_pages
);
162 static int __init
xen_nofrontswap_selfshrink_setup(char *s
)
164 use_frontswap_selfshrink
= false;
168 __setup("noselfshrink", xen_nofrontswap_selfshrink_setup
);
170 /* Disable with kernel boot option. */
171 static bool use_selfballooning __initdata
= true;
173 static int __init
xen_noselfballooning_setup(char *s
)
175 use_selfballooning
= false;
179 __setup("noselfballooning", xen_noselfballooning_setup
);
180 #else /* !CONFIG_FRONTSWAP */
181 /* Enable with kernel boot option. */
182 static bool use_selfballooning __initdata
= false;
184 static int __init
xen_selfballooning_setup(char *s
)
186 use_selfballooning
= true;
190 __setup("selfballooning", xen_selfballooning_setup
);
191 #endif /* CONFIG_FRONTSWAP */
194 * Use current balloon size, the goal (vm_committed_as), and hysteresis
195 * parameters to set a new target balloon size
197 static void selfballoon_process(struct work_struct
*work
)
199 unsigned long cur_pages
, goal_pages
, tgt_pages
;
200 bool reset_timer
= false;
202 if (xen_selfballooning_enabled
) {
203 cur_pages
= balloon_stats
.current_pages
;
204 tgt_pages
= cur_pages
; /* default is no change */
205 goal_pages
= percpu_counter_read_positive(&vm_committed_as
) +
206 balloon_stats
.current_pages
- totalram_pages
;
207 #ifdef CONFIG_FRONTSWAP
208 /* allow space for frontswap pages to be repatriated */
209 if (frontswap_selfshrinking
&& frontswap_enabled
)
210 goal_pages
+= frontswap_curr_pages();
212 if (cur_pages
> goal_pages
)
213 tgt_pages
= cur_pages
-
214 ((cur_pages
- goal_pages
) /
215 selfballoon_downhysteresis
);
216 else if (cur_pages
< goal_pages
)
217 tgt_pages
= cur_pages
+
218 ((goal_pages
- cur_pages
) /
219 selfballoon_uphysteresis
);
220 /* else if cur_pages == goal_pages, no change */
221 balloon_set_new_target(tgt_pages
);
224 #ifdef CONFIG_FRONTSWAP
225 if (frontswap_selfshrinking
&& frontswap_enabled
) {
226 frontswap_selfshrink();
231 schedule_delayed_work(&selfballoon_worker
,
232 selfballoon_interval
* HZ
);
237 #include <linux/sysdev.h>
238 #include <linux/capability.h>
240 #define SELFBALLOON_SHOW(name, format, args...) \
241 static ssize_t show_##name(struct sys_device *dev, \
242 struct sysdev_attribute *attr, \
245 return sprintf(buf, format, ##args); \
248 SELFBALLOON_SHOW(selfballooning
, "%d\n", xen_selfballooning_enabled
);
250 static ssize_t
store_selfballooning(struct sys_device
*dev
,
251 struct sysdev_attribute
*attr
,
255 bool was_enabled
= xen_selfballooning_enabled
;
259 if (!capable(CAP_SYS_ADMIN
))
262 err
= strict_strtoul(buf
, 10, &tmp
);
263 if (err
|| ((tmp
!= 0) && (tmp
!= 1)))
266 xen_selfballooning_enabled
= !!tmp
;
267 if (!was_enabled
&& xen_selfballooning_enabled
)
268 schedule_delayed_work(&selfballoon_worker
,
269 selfballoon_interval
* HZ
);
274 static SYSDEV_ATTR(selfballooning
, S_IRUGO
| S_IWUSR
,
275 show_selfballooning
, store_selfballooning
);
277 SELFBALLOON_SHOW(selfballoon_interval
, "%d\n", selfballoon_interval
);
279 static ssize_t
store_selfballoon_interval(struct sys_device
*dev
,
280 struct sysdev_attribute
*attr
,
287 if (!capable(CAP_SYS_ADMIN
))
289 err
= strict_strtoul(buf
, 10, &val
);
292 selfballoon_interval
= val
;
296 static SYSDEV_ATTR(selfballoon_interval
, S_IRUGO
| S_IWUSR
,
297 show_selfballoon_interval
, store_selfballoon_interval
);
299 SELFBALLOON_SHOW(selfballoon_downhys
, "%d\n", selfballoon_downhysteresis
);
301 static ssize_t
store_selfballoon_downhys(struct sys_device
*dev
,
302 struct sysdev_attribute
*attr
,
309 if (!capable(CAP_SYS_ADMIN
))
311 err
= strict_strtoul(buf
, 10, &val
);
314 selfballoon_downhysteresis
= val
;
318 static SYSDEV_ATTR(selfballoon_downhysteresis
, S_IRUGO
| S_IWUSR
,
319 show_selfballoon_downhys
, store_selfballoon_downhys
);
322 SELFBALLOON_SHOW(selfballoon_uphys
, "%d\n", selfballoon_uphysteresis
);
324 static ssize_t
store_selfballoon_uphys(struct sys_device
*dev
,
325 struct sysdev_attribute
*attr
,
332 if (!capable(CAP_SYS_ADMIN
))
334 err
= strict_strtoul(buf
, 10, &val
);
337 selfballoon_uphysteresis
= val
;
341 static SYSDEV_ATTR(selfballoon_uphysteresis
, S_IRUGO
| S_IWUSR
,
342 show_selfballoon_uphys
, store_selfballoon_uphys
);
344 #ifdef CONFIG_FRONTSWAP
345 SELFBALLOON_SHOW(frontswap_selfshrinking
, "%d\n", frontswap_selfshrinking
);
347 static ssize_t
store_frontswap_selfshrinking(struct sys_device
*dev
,
348 struct sysdev_attribute
*attr
,
352 bool was_enabled
= frontswap_selfshrinking
;
356 if (!capable(CAP_SYS_ADMIN
))
358 err
= strict_strtoul(buf
, 10, &tmp
);
359 if (err
|| ((tmp
!= 0) && (tmp
!= 1)))
361 frontswap_selfshrinking
= !!tmp
;
362 if (!was_enabled
&& !xen_selfballooning_enabled
&&
363 frontswap_selfshrinking
)
364 schedule_delayed_work(&selfballoon_worker
,
365 selfballoon_interval
* HZ
);
370 static SYSDEV_ATTR(frontswap_selfshrinking
, S_IRUGO
| S_IWUSR
,
371 show_frontswap_selfshrinking
, store_frontswap_selfshrinking
);
373 SELFBALLOON_SHOW(frontswap_inertia
, "%d\n", frontswap_inertia
);
375 static ssize_t
store_frontswap_inertia(struct sys_device
*dev
,
376 struct sysdev_attribute
*attr
,
383 if (!capable(CAP_SYS_ADMIN
))
385 err
= strict_strtoul(buf
, 10, &val
);
388 frontswap_inertia
= val
;
389 frontswap_inertia_counter
= val
;
393 static SYSDEV_ATTR(frontswap_inertia
, S_IRUGO
| S_IWUSR
,
394 show_frontswap_inertia
, store_frontswap_inertia
);
396 SELFBALLOON_SHOW(frontswap_hysteresis
, "%d\n", frontswap_hysteresis
);
398 static ssize_t
store_frontswap_hysteresis(struct sys_device
*dev
,
399 struct sysdev_attribute
*attr
,
406 if (!capable(CAP_SYS_ADMIN
))
408 err
= strict_strtoul(buf
, 10, &val
);
411 frontswap_hysteresis
= val
;
415 static SYSDEV_ATTR(frontswap_hysteresis
, S_IRUGO
| S_IWUSR
,
416 show_frontswap_hysteresis
, store_frontswap_hysteresis
);
418 #endif /* CONFIG_FRONTSWAP */
420 static struct attribute
*selfballoon_attrs
[] = {
421 &attr_selfballooning
.attr
,
422 &attr_selfballoon_interval
.attr
,
423 &attr_selfballoon_downhysteresis
.attr
,
424 &attr_selfballoon_uphysteresis
.attr
,
425 #ifdef CONFIG_FRONTSWAP
426 &attr_frontswap_selfshrinking
.attr
,
427 &attr_frontswap_hysteresis
.attr
,
428 &attr_frontswap_inertia
.attr
,
433 static struct attribute_group selfballoon_group
= {
434 .name
= "selfballoon",
435 .attrs
= selfballoon_attrs
439 int register_xen_selfballooning(struct sys_device
*sysdev
)
444 error
= sysfs_create_group(&sysdev
->kobj
, &selfballoon_group
);
448 EXPORT_SYMBOL(register_xen_selfballooning
);
450 static int __init
xen_selfballoon_init(void)
457 if (xen_initial_domain()) {
458 pr_info("xen/balloon: Xen selfballooning driver "
459 "disabled for domain0.\n");
463 xen_selfballooning_enabled
= tmem_enabled
&& use_selfballooning
;
464 if (xen_selfballooning_enabled
) {
465 pr_info("xen/balloon: Initializing Xen "
466 "selfballooning driver.\n");
469 #ifdef CONFIG_FRONTSWAP
470 frontswap_selfshrinking
= tmem_enabled
&& use_frontswap_selfshrink
;
471 if (frontswap_selfshrinking
) {
472 pr_info("xen/balloon: Initializing frontswap "
473 "selfshrinking driver.\n");
480 schedule_delayed_work(&selfballoon_worker
, selfballoon_interval
* HZ
);
485 subsys_initcall(xen_selfballoon_init
);
487 MODULE_LICENSE("GPL");