blob: 02817a85f877c777c603079a08cb27b236ee8ee2 [file] [log] [blame]
Dan Magenheimera50777c2011-07-08 12:26:21 -06001/******************************************************************************
2 * Xen selfballoon driver (and optional frontswap self-shrinking driver)
3 *
4 * Copyright (c) 2009-2011, Dan Magenheimer, Oracle Corp.
5 *
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.
10 *
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.
20 *
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.
29 *
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.
44 *
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.
49 *
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).
52 *
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
Konrad Rzeszutek Wilk37d46e12013-05-14 13:56:42 -040056 * of the guest memory. Note, that selfballooning should be disabled by default
57 * if frontswap is not configured. Similarly selfballooning should be enabled
58 * by default if frontswap is configured and can be disabled with the
59 * "tmem.selfballooning=0" kernel boot option. Finally, when frontswap is
60 * configured, frontswap-selfshrinking can be disabled with the
61 * "tmem.selfshrink=0" kernel boot option.
Dan Magenheimera50777c2011-07-08 12:26:21 -060062 *
63 * Selfballooning is disallowed in domain0 and force-disabled.
64 *
65 */
66
Joe Perches283c0972013-06-28 03:21:41 -070067#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
68
Dan Magenheimera50777c2011-07-08 12:26:21 -060069#include <linux/kernel.h>
Dan Magenheimer38a1ed42011-09-27 08:47:58 -060070#include <linux/bootmem.h>
71#include <linux/swap.h>
Dan Magenheimera50777c2011-07-08 12:26:21 -060072#include <linux/mm.h>
73#include <linux/mman.h>
Randy Dunlap4fec0e02011-08-15 21:41:43 -070074#include <linux/module.h>
Konrad Rzeszutek Wilk0642d2e2011-07-22 14:01:16 -040075#include <linux/workqueue.h>
Greg Kroah-Hartmancb0c05c2011-12-15 11:07:02 -080076#include <linux/device.h>
Dan Magenheimera50777c2011-07-08 12:26:21 -060077#include <xen/balloon.h>
Dan Magenheimera50777c2011-07-08 12:26:21 -060078#include <xen/tmem.h>
Konrad Rzeszutek Wilk0642d2e2011-07-22 14:01:16 -040079#include <xen/xen.h>
Dan Magenheimera50777c2011-07-08 12:26:21 -060080
81/* Enable/disable with sysfs. */
82static int xen_selfballooning_enabled __read_mostly;
83
84/*
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.
91 */
92static unsigned int selfballoon_downhysteresis __read_mostly = 8;
93static unsigned int selfballoon_uphysteresis __read_mostly = 1;
94
95/* In HZ, controls frequency of worker invocation. */
96static unsigned int selfballoon_interval __read_mostly = 5;
97
Dan Magenheimer38a1ed42011-09-27 08:47:58 -060098/*
99 * Minimum usable RAM in MB for selfballooning target for balloon.
100 * If non-zero, it is added to totalreserve_pages and self-ballooning
101 * will not balloon below the sum. If zero, a piecewise linear function
102 * is calculated as a minimum and added to totalreserve_pages. Note that
103 * setting this value indiscriminately may cause OOMs and crashes.
104 */
105static unsigned int selfballoon_min_usable_mb;
106
Jana Saoutd79d5952012-05-15 12:34:46 +0200107/*
108 * Amount of RAM in MB to add to the target number of pages.
109 * Can be used to reserve some more room for caches and the like.
110 */
111static unsigned int selfballoon_reserved_mb;
112
Dan Magenheimera50777c2011-07-08 12:26:21 -0600113static void selfballoon_process(struct work_struct *work);
114static DECLARE_DELAYED_WORK(selfballoon_worker, selfballoon_process);
115
116#ifdef CONFIG_FRONTSWAP
117#include <linux/frontswap.h>
118
119/* Enable/disable with sysfs. */
120static bool frontswap_selfshrinking __read_mostly;
121
Dan Magenheimera50777c2011-07-08 12:26:21 -0600122/*
123 * The default values for the following parameters were deemed reasonable
124 * by experimentation, may be workload-dependent, and can all be
125 * adjusted via sysfs.
126 */
127
128/* Control rate for frontswap shrinking. Higher hysteresis is slower. */
129static unsigned int frontswap_hysteresis __read_mostly = 20;
130
131/*
132 * Number of selfballoon worker invocations to wait before observing that
133 * frontswap selfshrinking should commence. Note that selfshrinking does
134 * not use a separate worker thread.
135 */
136static unsigned int frontswap_inertia __read_mostly = 3;
137
138/* Countdown to next invocation of frontswap_shrink() */
139static unsigned long frontswap_inertia_counter;
140
141/*
142 * Invoked by the selfballoon worker thread, uses current number of pages
143 * in frontswap (frontswap_curr_pages()), previous status, and control
144 * values (hysteresis and inertia) to determine if frontswap should be
145 * shrunk and what the new frontswap size should be. Note that
146 * frontswap_shrink is essentially a partial swapoff that immediately
147 * transfers pages from the "swap device" (frontswap) back into kernel
148 * RAM; despite the name, frontswap "shrinking" is very different from
149 * the "shrinker" interface used by the kernel MM subsystem to reclaim
150 * memory.
151 */
152static void frontswap_selfshrink(void)
153{
154 static unsigned long cur_frontswap_pages;
155 static unsigned long last_frontswap_pages;
156 static unsigned long tgt_frontswap_pages;
157
158 last_frontswap_pages = cur_frontswap_pages;
159 cur_frontswap_pages = frontswap_curr_pages();
160 if (!cur_frontswap_pages ||
161 (cur_frontswap_pages > last_frontswap_pages)) {
162 frontswap_inertia_counter = frontswap_inertia;
163 return;
164 }
165 if (frontswap_inertia_counter && --frontswap_inertia_counter)
166 return;
167 if (cur_frontswap_pages <= frontswap_hysteresis)
168 tgt_frontswap_pages = 0;
169 else
170 tgt_frontswap_pages = cur_frontswap_pages -
171 (cur_frontswap_pages / frontswap_hysteresis);
172 frontswap_shrink(tgt_frontswap_pages);
173}
174
Dan Magenheimera50777c2011-07-08 12:26:21 -0600175#endif /* CONFIG_FRONTSWAP */
176
Dan Magenheimer38a1ed42011-09-27 08:47:58 -0600177#define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
178
Dan Magenheimera50777c2011-07-08 12:26:21 -0600179/*
180 * Use current balloon size, the goal (vm_committed_as), and hysteresis
181 * parameters to set a new target balloon size
182 */
183static void selfballoon_process(struct work_struct *work)
184{
Dan Magenheimer38a1ed42011-09-27 08:47:58 -0600185 unsigned long cur_pages, goal_pages, tgt_pages, floor_pages;
186 unsigned long useful_pages;
Dan Magenheimera50777c2011-07-08 12:26:21 -0600187 bool reset_timer = false;
188
189 if (xen_selfballooning_enabled) {
Dan Magenheimer38a1ed42011-09-27 08:47:58 -0600190 cur_pages = totalram_pages;
Dan Magenheimera50777c2011-07-08 12:26:21 -0600191 tgt_pages = cur_pages; /* default is no change */
K. Y. Srinivasan997071b2012-11-15 14:34:42 -0800192 goal_pages = vm_memory_committed() +
Jana Saoutd79d5952012-05-15 12:34:46 +0200193 totalreserve_pages +
194 MB2PAGES(selfballoon_reserved_mb);
Dan Magenheimera50777c2011-07-08 12:26:21 -0600195#ifdef CONFIG_FRONTSWAP
196 /* allow space for frontswap pages to be repatriated */
197 if (frontswap_selfshrinking && frontswap_enabled)
198 goal_pages += frontswap_curr_pages();
199#endif
200 if (cur_pages > goal_pages)
201 tgt_pages = cur_pages -
202 ((cur_pages - goal_pages) /
203 selfballoon_downhysteresis);
204 else if (cur_pages < goal_pages)
205 tgt_pages = cur_pages +
206 ((goal_pages - cur_pages) /
207 selfballoon_uphysteresis);
208 /* else if cur_pages == goal_pages, no change */
Dan Magenheimer38a1ed42011-09-27 08:47:58 -0600209 useful_pages = max_pfn - totalreserve_pages;
210 if (selfballoon_min_usable_mb != 0)
211 floor_pages = totalreserve_pages +
212 MB2PAGES(selfballoon_min_usable_mb);
213 /* piecewise linear function ending in ~3% slope */
214 else if (useful_pages < MB2PAGES(16))
215 floor_pages = max_pfn; /* not worth ballooning */
216 else if (useful_pages < MB2PAGES(64))
217 floor_pages = totalreserve_pages + MB2PAGES(16) +
218 ((useful_pages - MB2PAGES(16)) >> 1);
219 else if (useful_pages < MB2PAGES(512))
220 floor_pages = totalreserve_pages + MB2PAGES(40) +
221 ((useful_pages - MB2PAGES(40)) >> 3);
222 else /* useful_pages >= MB2PAGES(512) */
223 floor_pages = totalreserve_pages + MB2PAGES(99) +
224 ((useful_pages - MB2PAGES(99)) >> 5);
225 if (tgt_pages < floor_pages)
226 tgt_pages = floor_pages;
227 balloon_set_new_target(tgt_pages +
228 balloon_stats.current_pages - totalram_pages);
Dan Magenheimera50777c2011-07-08 12:26:21 -0600229 reset_timer = true;
230 }
231#ifdef CONFIG_FRONTSWAP
232 if (frontswap_selfshrinking && frontswap_enabled) {
233 frontswap_selfshrink();
234 reset_timer = true;
235 }
236#endif
237 if (reset_timer)
238 schedule_delayed_work(&selfballoon_worker,
239 selfballoon_interval * HZ);
240}
241
242#ifdef CONFIG_SYSFS
243
Dan Magenheimera50777c2011-07-08 12:26:21 -0600244#include <linux/capability.h>
245
246#define SELFBALLOON_SHOW(name, format, args...) \
Kay Sievers07068022011-12-14 15:32:50 -0800247 static ssize_t show_##name(struct device *dev, \
248 struct device_attribute *attr, \
249 char *buf) \
Dan Magenheimera50777c2011-07-08 12:26:21 -0600250 { \
251 return sprintf(buf, format, ##args); \
252 }
253
254SELFBALLOON_SHOW(selfballooning, "%d\n", xen_selfballooning_enabled);
255
Kay Sievers07068022011-12-14 15:32:50 -0800256static ssize_t store_selfballooning(struct device *dev,
257 struct device_attribute *attr,
Dan Magenheimera50777c2011-07-08 12:26:21 -0600258 const char *buf,
259 size_t count)
260{
261 bool was_enabled = xen_selfballooning_enabled;
262 unsigned long tmp;
263 int err;
264
265 if (!capable(CAP_SYS_ADMIN))
266 return -EPERM;
267
268 err = strict_strtoul(buf, 10, &tmp);
269 if (err || ((tmp != 0) && (tmp != 1)))
270 return -EINVAL;
271
272 xen_selfballooning_enabled = !!tmp;
273 if (!was_enabled && xen_selfballooning_enabled)
274 schedule_delayed_work(&selfballoon_worker,
275 selfballoon_interval * HZ);
276
277 return count;
278}
279
Kay Sievers07068022011-12-14 15:32:50 -0800280static DEVICE_ATTR(selfballooning, S_IRUGO | S_IWUSR,
Dan Magenheimera50777c2011-07-08 12:26:21 -0600281 show_selfballooning, store_selfballooning);
282
283SELFBALLOON_SHOW(selfballoon_interval, "%d\n", selfballoon_interval);
284
Kay Sievers07068022011-12-14 15:32:50 -0800285static ssize_t store_selfballoon_interval(struct device *dev,
286 struct device_attribute *attr,
Dan Magenheimera50777c2011-07-08 12:26:21 -0600287 const char *buf,
288 size_t count)
289{
290 unsigned long val;
291 int err;
292
293 if (!capable(CAP_SYS_ADMIN))
294 return -EPERM;
295 err = strict_strtoul(buf, 10, &val);
296 if (err || val == 0)
297 return -EINVAL;
298 selfballoon_interval = val;
299 return count;
300}
301
Kay Sievers07068022011-12-14 15:32:50 -0800302static DEVICE_ATTR(selfballoon_interval, S_IRUGO | S_IWUSR,
Dan Magenheimera50777c2011-07-08 12:26:21 -0600303 show_selfballoon_interval, store_selfballoon_interval);
304
305SELFBALLOON_SHOW(selfballoon_downhys, "%d\n", selfballoon_downhysteresis);
306
Kay Sievers07068022011-12-14 15:32:50 -0800307static ssize_t store_selfballoon_downhys(struct device *dev,
308 struct device_attribute *attr,
Dan Magenheimera50777c2011-07-08 12:26:21 -0600309 const char *buf,
310 size_t count)
311{
312 unsigned long val;
313 int err;
314
315 if (!capable(CAP_SYS_ADMIN))
316 return -EPERM;
317 err = strict_strtoul(buf, 10, &val);
318 if (err || val == 0)
319 return -EINVAL;
320 selfballoon_downhysteresis = val;
321 return count;
322}
323
Kay Sievers07068022011-12-14 15:32:50 -0800324static DEVICE_ATTR(selfballoon_downhysteresis, S_IRUGO | S_IWUSR,
Dan Magenheimera50777c2011-07-08 12:26:21 -0600325 show_selfballoon_downhys, store_selfballoon_downhys);
326
327
328SELFBALLOON_SHOW(selfballoon_uphys, "%d\n", selfballoon_uphysteresis);
329
Kay Sievers07068022011-12-14 15:32:50 -0800330static ssize_t store_selfballoon_uphys(struct device *dev,
331 struct device_attribute *attr,
Dan Magenheimera50777c2011-07-08 12:26:21 -0600332 const char *buf,
333 size_t count)
334{
335 unsigned long val;
336 int err;
337
338 if (!capable(CAP_SYS_ADMIN))
339 return -EPERM;
340 err = strict_strtoul(buf, 10, &val);
341 if (err || val == 0)
342 return -EINVAL;
343 selfballoon_uphysteresis = val;
344 return count;
345}
346
Kay Sievers07068022011-12-14 15:32:50 -0800347static DEVICE_ATTR(selfballoon_uphysteresis, S_IRUGO | S_IWUSR,
Dan Magenheimera50777c2011-07-08 12:26:21 -0600348 show_selfballoon_uphys, store_selfballoon_uphys);
349
Dan Magenheimer38a1ed42011-09-27 08:47:58 -0600350SELFBALLOON_SHOW(selfballoon_min_usable_mb, "%d\n",
351 selfballoon_min_usable_mb);
352
Kay Sievers07068022011-12-14 15:32:50 -0800353static ssize_t store_selfballoon_min_usable_mb(struct device *dev,
354 struct device_attribute *attr,
Dan Magenheimer38a1ed42011-09-27 08:47:58 -0600355 const char *buf,
356 size_t count)
357{
358 unsigned long val;
359 int err;
360
361 if (!capable(CAP_SYS_ADMIN))
362 return -EPERM;
363 err = strict_strtoul(buf, 10, &val);
364 if (err || val == 0)
365 return -EINVAL;
366 selfballoon_min_usable_mb = val;
367 return count;
368}
369
Kay Sievers07068022011-12-14 15:32:50 -0800370static DEVICE_ATTR(selfballoon_min_usable_mb, S_IRUGO | S_IWUSR,
Dan Magenheimer38a1ed42011-09-27 08:47:58 -0600371 show_selfballoon_min_usable_mb,
372 store_selfballoon_min_usable_mb);
373
Jana Saoutd79d5952012-05-15 12:34:46 +0200374SELFBALLOON_SHOW(selfballoon_reserved_mb, "%d\n",
375 selfballoon_reserved_mb);
376
377static ssize_t store_selfballoon_reserved_mb(struct device *dev,
378 struct device_attribute *attr,
379 const char *buf,
380 size_t count)
381{
382 unsigned long val;
383 int err;
384
385 if (!capable(CAP_SYS_ADMIN))
386 return -EPERM;
387 err = strict_strtoul(buf, 10, &val);
388 if (err || val == 0)
389 return -EINVAL;
390 selfballoon_reserved_mb = val;
391 return count;
392}
393
394static DEVICE_ATTR(selfballoon_reserved_mb, S_IRUGO | S_IWUSR,
395 show_selfballoon_reserved_mb,
396 store_selfballoon_reserved_mb);
397
Dan Magenheimer38a1ed42011-09-27 08:47:58 -0600398
Dan Magenheimera50777c2011-07-08 12:26:21 -0600399#ifdef CONFIG_FRONTSWAP
400SELFBALLOON_SHOW(frontswap_selfshrinking, "%d\n", frontswap_selfshrinking);
401
Kay Sievers07068022011-12-14 15:32:50 -0800402static ssize_t store_frontswap_selfshrinking(struct device *dev,
403 struct device_attribute *attr,
Dan Magenheimera50777c2011-07-08 12:26:21 -0600404 const char *buf,
405 size_t count)
406{
407 bool was_enabled = frontswap_selfshrinking;
408 unsigned long tmp;
409 int err;
410
411 if (!capable(CAP_SYS_ADMIN))
412 return -EPERM;
413 err = strict_strtoul(buf, 10, &tmp);
414 if (err || ((tmp != 0) && (tmp != 1)))
415 return -EINVAL;
416 frontswap_selfshrinking = !!tmp;
417 if (!was_enabled && !xen_selfballooning_enabled &&
418 frontswap_selfshrinking)
419 schedule_delayed_work(&selfballoon_worker,
420 selfballoon_interval * HZ);
421
422 return count;
423}
424
Kay Sievers07068022011-12-14 15:32:50 -0800425static DEVICE_ATTR(frontswap_selfshrinking, S_IRUGO | S_IWUSR,
Dan Magenheimera50777c2011-07-08 12:26:21 -0600426 show_frontswap_selfshrinking, store_frontswap_selfshrinking);
427
428SELFBALLOON_SHOW(frontswap_inertia, "%d\n", frontswap_inertia);
429
Kay Sievers07068022011-12-14 15:32:50 -0800430static ssize_t store_frontswap_inertia(struct device *dev,
431 struct device_attribute *attr,
Dan Magenheimera50777c2011-07-08 12:26:21 -0600432 const char *buf,
433 size_t count)
434{
435 unsigned long val;
436 int err;
437
438 if (!capable(CAP_SYS_ADMIN))
439 return -EPERM;
440 err = strict_strtoul(buf, 10, &val);
441 if (err || val == 0)
442 return -EINVAL;
443 frontswap_inertia = val;
444 frontswap_inertia_counter = val;
445 return count;
446}
447
Kay Sievers07068022011-12-14 15:32:50 -0800448static DEVICE_ATTR(frontswap_inertia, S_IRUGO | S_IWUSR,
Dan Magenheimera50777c2011-07-08 12:26:21 -0600449 show_frontswap_inertia, store_frontswap_inertia);
450
451SELFBALLOON_SHOW(frontswap_hysteresis, "%d\n", frontswap_hysteresis);
452
Kay Sievers07068022011-12-14 15:32:50 -0800453static ssize_t store_frontswap_hysteresis(struct device *dev,
454 struct device_attribute *attr,
Dan Magenheimera50777c2011-07-08 12:26:21 -0600455 const char *buf,
456 size_t count)
457{
458 unsigned long val;
459 int err;
460
461 if (!capable(CAP_SYS_ADMIN))
462 return -EPERM;
463 err = strict_strtoul(buf, 10, &val);
464 if (err || val == 0)
465 return -EINVAL;
466 frontswap_hysteresis = val;
467 return count;
468}
469
Kay Sievers07068022011-12-14 15:32:50 -0800470static DEVICE_ATTR(frontswap_hysteresis, S_IRUGO | S_IWUSR,
Dan Magenheimera50777c2011-07-08 12:26:21 -0600471 show_frontswap_hysteresis, store_frontswap_hysteresis);
472
473#endif /* CONFIG_FRONTSWAP */
474
475static struct attribute *selfballoon_attrs[] = {
Kay Sievers07068022011-12-14 15:32:50 -0800476 &dev_attr_selfballooning.attr,
477 &dev_attr_selfballoon_interval.attr,
478 &dev_attr_selfballoon_downhysteresis.attr,
479 &dev_attr_selfballoon_uphysteresis.attr,
480 &dev_attr_selfballoon_min_usable_mb.attr,
Jana Saoutd79d5952012-05-15 12:34:46 +0200481 &dev_attr_selfballoon_reserved_mb.attr,
Dan Magenheimera50777c2011-07-08 12:26:21 -0600482#ifdef CONFIG_FRONTSWAP
Kay Sievers07068022011-12-14 15:32:50 -0800483 &dev_attr_frontswap_selfshrinking.attr,
484 &dev_attr_frontswap_hysteresis.attr,
485 &dev_attr_frontswap_inertia.attr,
Dan Magenheimera50777c2011-07-08 12:26:21 -0600486#endif
487 NULL
488};
489
Jan Beulichead1d012012-03-14 12:34:19 -0400490static const struct attribute_group selfballoon_group = {
Dan Magenheimera50777c2011-07-08 12:26:21 -0600491 .name = "selfballoon",
492 .attrs = selfballoon_attrs
493};
494#endif
495
Kay Sievers07068022011-12-14 15:32:50 -0800496int register_xen_selfballooning(struct device *dev)
Dan Magenheimera50777c2011-07-08 12:26:21 -0600497{
498 int error = -1;
499
500#ifdef CONFIG_SYSFS
Kay Sievers07068022011-12-14 15:32:50 -0800501 error = sysfs_create_group(&dev->kobj, &selfballoon_group);
Dan Magenheimera50777c2011-07-08 12:26:21 -0600502#endif
503 return error;
504}
505EXPORT_SYMBOL(register_xen_selfballooning);
506
Dan Magenheimer10a7a0772013-04-30 15:27:00 -0700507int xen_selfballoon_init(bool use_selfballooning, bool use_frontswap_selfshrink)
Dan Magenheimera50777c2011-07-08 12:26:21 -0600508{
509 bool enable = false;
510
511 if (!xen_domain())
512 return -ENODEV;
513
514 if (xen_initial_domain()) {
Joe Perches283c0972013-06-28 03:21:41 -0700515 pr_info("Xen selfballooning driver disabled for domain0\n");
Dan Magenheimera50777c2011-07-08 12:26:21 -0600516 return -ENODEV;
517 }
518
519 xen_selfballooning_enabled = tmem_enabled && use_selfballooning;
520 if (xen_selfballooning_enabled) {
Joe Perches283c0972013-06-28 03:21:41 -0700521 pr_info("Initializing Xen selfballooning driver\n");
Dan Magenheimera50777c2011-07-08 12:26:21 -0600522 enable = true;
523 }
524#ifdef CONFIG_FRONTSWAP
525 frontswap_selfshrinking = tmem_enabled && use_frontswap_selfshrink;
526 if (frontswap_selfshrinking) {
Joe Perches283c0972013-06-28 03:21:41 -0700527 pr_info("Initializing frontswap selfshrinking driver\n");
Dan Magenheimera50777c2011-07-08 12:26:21 -0600528 enable = true;
529 }
530#endif
531 if (!enable)
532 return -ENODEV;
533
534 schedule_delayed_work(&selfballoon_worker, selfballoon_interval * HZ);
535
536 return 0;
537}
Dan Magenheimer10a7a0772013-04-30 15:27:00 -0700538EXPORT_SYMBOL(xen_selfballoon_init);