Linux Audio

Check our new training course

Loading...
v3.1
  1/******************************************************************************
  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
 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.
 65 *
 66 * Selfballooning is disallowed in domain0 and force-disabled.
 67 *
 68 */
 69
 70#include <linux/kernel.h>
 
 
 71#include <linux/mm.h>
 72#include <linux/mman.h>
 73#include <linux/module.h>
 74#include <linux/workqueue.h>
 
 75#include <xen/balloon.h>
 76#include <xen/tmem.h>
 77#include <xen/xen.h>
 78
 79/* Enable/disable with sysfs. */
 80static int xen_selfballooning_enabled __read_mostly;
 81
 82/*
 83 * Controls rate at which memory target (this iteration) approaches
 84 * ultimate goal when memory need is increasing (up-hysteresis) or
 85 * decreasing (down-hysteresis). Higher values of hysteresis cause
 86 * slower increases/decreases. The default values for the various
 87 * parameters were deemed reasonable by experimentation, may be
 88 * workload-dependent, and can all be adjusted via sysfs.
 89 */
 90static unsigned int selfballoon_downhysteresis __read_mostly = 8;
 91static unsigned int selfballoon_uphysteresis __read_mostly = 1;
 92
 93/* In HZ, controls frequency of worker invocation. */
 94static unsigned int selfballoon_interval __read_mostly = 5;
 95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 96static void selfballoon_process(struct work_struct *work);
 97static DECLARE_DELAYED_WORK(selfballoon_worker, selfballoon_process);
 98
 99#ifdef CONFIG_FRONTSWAP
100#include <linux/frontswap.h>
101
102/* Enable/disable with sysfs. */
103static bool frontswap_selfshrinking __read_mostly;
104
105/* Enable/disable with kernel boot option. */
106static bool use_frontswap_selfshrink __initdata = true;
107
108/*
109 * The default values for the following parameters were deemed reasonable
110 * by experimentation, may be workload-dependent, and can all be
111 * adjusted via sysfs.
112 */
113
114/* Control rate for frontswap shrinking. Higher hysteresis is slower. */
115static unsigned int frontswap_hysteresis __read_mostly = 20;
116
117/*
118 * Number of selfballoon worker invocations to wait before observing that
119 * frontswap selfshrinking should commence. Note that selfshrinking does
120 * not use a separate worker thread.
121 */
122static unsigned int frontswap_inertia __read_mostly = 3;
123
124/* Countdown to next invocation of frontswap_shrink() */
125static unsigned long frontswap_inertia_counter;
126
127/*
128 * Invoked by the selfballoon worker thread, uses current number of pages
129 * in frontswap (frontswap_curr_pages()), previous status, and control
130 * values (hysteresis and inertia) to determine if frontswap should be
131 * shrunk and what the new frontswap size should be.  Note that
132 * frontswap_shrink is essentially a partial swapoff that immediately
133 * transfers pages from the "swap device" (frontswap) back into kernel
134 * RAM; despite the name, frontswap "shrinking" is very different from
135 * the "shrinker" interface used by the kernel MM subsystem to reclaim
136 * memory.
137 */
138static void frontswap_selfshrink(void)
139{
140	static unsigned long cur_frontswap_pages;
141	static unsigned long last_frontswap_pages;
142	static unsigned long tgt_frontswap_pages;
143
144	last_frontswap_pages = cur_frontswap_pages;
145	cur_frontswap_pages = frontswap_curr_pages();
146	if (!cur_frontswap_pages ||
147			(cur_frontswap_pages > last_frontswap_pages)) {
148		frontswap_inertia_counter = frontswap_inertia;
149		return;
150	}
151	if (frontswap_inertia_counter && --frontswap_inertia_counter)
152		return;
153	if (cur_frontswap_pages <= frontswap_hysteresis)
154		tgt_frontswap_pages = 0;
155	else
156		tgt_frontswap_pages = cur_frontswap_pages -
157			(cur_frontswap_pages / frontswap_hysteresis);
158	frontswap_shrink(tgt_frontswap_pages);
159}
160
161static int __init xen_nofrontswap_selfshrink_setup(char *s)
162{
163	use_frontswap_selfshrink = false;
164	return 1;
165}
166
167__setup("noselfshrink", xen_nofrontswap_selfshrink_setup);
168
169/* Disable with kernel boot option. */
170static bool use_selfballooning __initdata = true;
171
172static int __init xen_noselfballooning_setup(char *s)
173{
174	use_selfballooning = false;
175	return 1;
176}
177
178__setup("noselfballooning", xen_noselfballooning_setup);
179#else /* !CONFIG_FRONTSWAP */
180/* Enable with kernel boot option. */
181static bool use_selfballooning __initdata = false;
182
183static int __init xen_selfballooning_setup(char *s)
184{
185	use_selfballooning = true;
186	return 1;
187}
188
189__setup("selfballooning", xen_selfballooning_setup);
190#endif /* CONFIG_FRONTSWAP */
191
 
 
192/*
193 * Use current balloon size, the goal (vm_committed_as), and hysteresis
194 * parameters to set a new target balloon size
195 */
196static void selfballoon_process(struct work_struct *work)
197{
198	unsigned long cur_pages, goal_pages, tgt_pages;
 
199	bool reset_timer = false;
200
201	if (xen_selfballooning_enabled) {
202		cur_pages = balloon_stats.current_pages;
203		tgt_pages = cur_pages; /* default is no change */
204		goal_pages = percpu_counter_read_positive(&vm_committed_as) +
205			balloon_stats.current_pages - totalram_pages;
 
206#ifdef CONFIG_FRONTSWAP
207		/* allow space for frontswap pages to be repatriated */
208		if (frontswap_selfshrinking && frontswap_enabled)
209			goal_pages += frontswap_curr_pages();
210#endif
211		if (cur_pages > goal_pages)
212			tgt_pages = cur_pages -
213				((cur_pages - goal_pages) /
214				  selfballoon_downhysteresis);
215		else if (cur_pages < goal_pages)
216			tgt_pages = cur_pages +
217				((goal_pages - cur_pages) /
218				  selfballoon_uphysteresis);
219		/* else if cur_pages == goal_pages, no change */
220		balloon_set_new_target(tgt_pages);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221		reset_timer = true;
222	}
223#ifdef CONFIG_FRONTSWAP
224	if (frontswap_selfshrinking && frontswap_enabled) {
225		frontswap_selfshrink();
226		reset_timer = true;
227	}
228#endif
229	if (reset_timer)
230		schedule_delayed_work(&selfballoon_worker,
231			selfballoon_interval * HZ);
232}
233
234#ifdef CONFIG_SYSFS
235
236#include <linux/sysdev.h>
237#include <linux/capability.h>
238
239#define SELFBALLOON_SHOW(name, format, args...)				\
240	static ssize_t show_##name(struct sys_device *dev,	\
241					   struct sysdev_attribute *attr, \
242					   char *buf) \
243	{ \
244		return sprintf(buf, format, ##args); \
245	}
246
247SELFBALLOON_SHOW(selfballooning, "%d\n", xen_selfballooning_enabled);
248
249static ssize_t store_selfballooning(struct sys_device *dev,
250			    struct sysdev_attribute *attr,
251			    const char *buf,
252			    size_t count)
253{
254	bool was_enabled = xen_selfballooning_enabled;
255	unsigned long tmp;
256	int err;
257
258	if (!capable(CAP_SYS_ADMIN))
259		return -EPERM;
260
261	err = strict_strtoul(buf, 10, &tmp);
262	if (err || ((tmp != 0) && (tmp != 1)))
263		return -EINVAL;
264
265	xen_selfballooning_enabled = !!tmp;
266	if (!was_enabled && xen_selfballooning_enabled)
267		schedule_delayed_work(&selfballoon_worker,
268			selfballoon_interval * HZ);
269
270	return count;
271}
272
273static SYSDEV_ATTR(selfballooning, S_IRUGO | S_IWUSR,
274		   show_selfballooning, store_selfballooning);
275
276SELFBALLOON_SHOW(selfballoon_interval, "%d\n", selfballoon_interval);
277
278static ssize_t store_selfballoon_interval(struct sys_device *dev,
279					  struct sysdev_attribute *attr,
280					  const char *buf,
281					  size_t count)
282{
283	unsigned long val;
284	int err;
285
286	if (!capable(CAP_SYS_ADMIN))
287		return -EPERM;
288	err = strict_strtoul(buf, 10, &val);
289	if (err || val == 0)
290		return -EINVAL;
291	selfballoon_interval = val;
292	return count;
293}
294
295static SYSDEV_ATTR(selfballoon_interval, S_IRUGO | S_IWUSR,
296		   show_selfballoon_interval, store_selfballoon_interval);
297
298SELFBALLOON_SHOW(selfballoon_downhys, "%d\n", selfballoon_downhysteresis);
299
300static ssize_t store_selfballoon_downhys(struct sys_device *dev,
301					 struct sysdev_attribute *attr,
302					 const char *buf,
303					 size_t count)
304{
305	unsigned long val;
306	int err;
307
308	if (!capable(CAP_SYS_ADMIN))
309		return -EPERM;
310	err = strict_strtoul(buf, 10, &val);
311	if (err || val == 0)
312		return -EINVAL;
313	selfballoon_downhysteresis = val;
314	return count;
315}
316
317static SYSDEV_ATTR(selfballoon_downhysteresis, S_IRUGO | S_IWUSR,
318		   show_selfballoon_downhys, store_selfballoon_downhys);
319
320
321SELFBALLOON_SHOW(selfballoon_uphys, "%d\n", selfballoon_uphysteresis);
322
323static ssize_t store_selfballoon_uphys(struct sys_device *dev,
324				       struct sysdev_attribute *attr,
325				       const char *buf,
326				       size_t count)
327{
328	unsigned long val;
329	int err;
330
331	if (!capable(CAP_SYS_ADMIN))
332		return -EPERM;
333	err = strict_strtoul(buf, 10, &val);
334	if (err || val == 0)
335		return -EINVAL;
336	selfballoon_uphysteresis = val;
337	return count;
338}
339
340static SYSDEV_ATTR(selfballoon_uphysteresis, S_IRUGO | S_IWUSR,
341		   show_selfballoon_uphys, store_selfballoon_uphys);
342
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
343#ifdef CONFIG_FRONTSWAP
344SELFBALLOON_SHOW(frontswap_selfshrinking, "%d\n", frontswap_selfshrinking);
345
346static ssize_t store_frontswap_selfshrinking(struct sys_device *dev,
347					     struct sysdev_attribute *attr,
348					     const char *buf,
349					     size_t count)
350{
351	bool was_enabled = frontswap_selfshrinking;
352	unsigned long tmp;
353	int err;
354
355	if (!capable(CAP_SYS_ADMIN))
356		return -EPERM;
357	err = strict_strtoul(buf, 10, &tmp);
358	if (err || ((tmp != 0) && (tmp != 1)))
359		return -EINVAL;
360	frontswap_selfshrinking = !!tmp;
361	if (!was_enabled && !xen_selfballooning_enabled &&
362	     frontswap_selfshrinking)
363		schedule_delayed_work(&selfballoon_worker,
364			selfballoon_interval * HZ);
365
366	return count;
367}
368
369static SYSDEV_ATTR(frontswap_selfshrinking, S_IRUGO | S_IWUSR,
370		   show_frontswap_selfshrinking, store_frontswap_selfshrinking);
371
372SELFBALLOON_SHOW(frontswap_inertia, "%d\n", frontswap_inertia);
373
374static ssize_t store_frontswap_inertia(struct sys_device *dev,
375				       struct sysdev_attribute *attr,
376				       const char *buf,
377				       size_t count)
378{
379	unsigned long val;
380	int err;
381
382	if (!capable(CAP_SYS_ADMIN))
383		return -EPERM;
384	err = strict_strtoul(buf, 10, &val);
385	if (err || val == 0)
386		return -EINVAL;
387	frontswap_inertia = val;
388	frontswap_inertia_counter = val;
389	return count;
390}
391
392static SYSDEV_ATTR(frontswap_inertia, S_IRUGO | S_IWUSR,
393		   show_frontswap_inertia, store_frontswap_inertia);
394
395SELFBALLOON_SHOW(frontswap_hysteresis, "%d\n", frontswap_hysteresis);
396
397static ssize_t store_frontswap_hysteresis(struct sys_device *dev,
398					  struct sysdev_attribute *attr,
399					  const char *buf,
400					  size_t count)
401{
402	unsigned long val;
403	int err;
404
405	if (!capable(CAP_SYS_ADMIN))
406		return -EPERM;
407	err = strict_strtoul(buf, 10, &val);
408	if (err || val == 0)
409		return -EINVAL;
410	frontswap_hysteresis = val;
411	return count;
412}
413
414static SYSDEV_ATTR(frontswap_hysteresis, S_IRUGO | S_IWUSR,
415		   show_frontswap_hysteresis, store_frontswap_hysteresis);
416
417#endif /* CONFIG_FRONTSWAP */
418
419static struct attribute *selfballoon_attrs[] = {
420	&attr_selfballooning.attr,
421	&attr_selfballoon_interval.attr,
422	&attr_selfballoon_downhysteresis.attr,
423	&attr_selfballoon_uphysteresis.attr,
 
 
424#ifdef CONFIG_FRONTSWAP
425	&attr_frontswap_selfshrinking.attr,
426	&attr_frontswap_hysteresis.attr,
427	&attr_frontswap_inertia.attr,
428#endif
429	NULL
430};
431
432static struct attribute_group selfballoon_group = {
433	.name = "selfballoon",
434	.attrs = selfballoon_attrs
435};
436#endif
437
438int register_xen_selfballooning(struct sys_device *sysdev)
439{
440	int error = -1;
441
442#ifdef CONFIG_SYSFS
443	error = sysfs_create_group(&sysdev->kobj, &selfballoon_group);
444#endif
445	return error;
446}
447EXPORT_SYMBOL(register_xen_selfballooning);
448
449static int __init xen_selfballoon_init(void)
450{
451	bool enable = false;
452
453	if (!xen_domain())
454		return -ENODEV;
455
456	if (xen_initial_domain()) {
457		pr_info("xen/balloon: Xen selfballooning driver "
458				"disabled for domain0.\n");
459		return -ENODEV;
460	}
461
462	xen_selfballooning_enabled = tmem_enabled && use_selfballooning;
463	if (xen_selfballooning_enabled) {
464		pr_info("xen/balloon: Initializing Xen "
465					"selfballooning driver.\n");
466		enable = true;
467	}
468#ifdef CONFIG_FRONTSWAP
469	frontswap_selfshrinking = tmem_enabled && use_frontswap_selfshrink;
470	if (frontswap_selfshrinking) {
471		pr_info("xen/balloon: Initializing frontswap "
472					"selfshrinking driver.\n");
473		enable = true;
474	}
475#endif
476	if (!enable)
477		return -ENODEV;
478
479	schedule_delayed_work(&selfballoon_worker, selfballoon_interval * HZ);
480
481	return 0;
482}
483
484subsys_initcall(xen_selfballoon_init);
485
486MODULE_LICENSE("GPL");
v3.5.6
  1/******************************************************************************
  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
 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.
 65 *
 66 * Selfballooning is disallowed in domain0 and force-disabled.
 67 *
 68 */
 69
 70#include <linux/kernel.h>
 71#include <linux/bootmem.h>
 72#include <linux/swap.h>
 73#include <linux/mm.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>
 79#include <xen/tmem.h>
 80#include <xen/xen.h>
 81
 82/* Enable/disable with sysfs. */
 83static int xen_selfballooning_enabled __read_mostly;
 84
 85/*
 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.
 92 */
 93static unsigned int selfballoon_downhysteresis __read_mostly = 8;
 94static unsigned int selfballoon_uphysteresis __read_mostly = 1;
 95
 96/* In HZ, controls frequency of worker invocation. */
 97static unsigned int selfballoon_interval __read_mostly = 5;
 98
 99/*
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.
105 */
106static unsigned int selfballoon_min_usable_mb;
107
108/*
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.
111 */
112static unsigned int selfballoon_reserved_mb;
113
114static void selfballoon_process(struct work_struct *work);
115static DECLARE_DELAYED_WORK(selfballoon_worker, selfballoon_process);
116
117#ifdef CONFIG_FRONTSWAP
118#include <linux/frontswap.h>
119
120/* Enable/disable with sysfs. */
121static bool frontswap_selfshrinking __read_mostly;
122
123/* Enable/disable with kernel boot option. */
124static bool use_frontswap_selfshrink __initdata = true;
125
126/*
127 * The default values for the following parameters were deemed reasonable
128 * by experimentation, may be workload-dependent, and can all be
129 * adjusted via sysfs.
130 */
131
132/* Control rate for frontswap shrinking. Higher hysteresis is slower. */
133static unsigned int frontswap_hysteresis __read_mostly = 20;
134
135/*
136 * Number of selfballoon worker invocations to wait before observing that
137 * frontswap selfshrinking should commence. Note that selfshrinking does
138 * not use a separate worker thread.
139 */
140static unsigned int frontswap_inertia __read_mostly = 3;
141
142/* Countdown to next invocation of frontswap_shrink() */
143static unsigned long frontswap_inertia_counter;
144
145/*
146 * Invoked by the selfballoon worker thread, uses current number of pages
147 * in frontswap (frontswap_curr_pages()), previous status, and control
148 * values (hysteresis and inertia) to determine if frontswap should be
149 * shrunk and what the new frontswap size should be.  Note that
150 * frontswap_shrink is essentially a partial swapoff that immediately
151 * transfers pages from the "swap device" (frontswap) back into kernel
152 * RAM; despite the name, frontswap "shrinking" is very different from
153 * the "shrinker" interface used by the kernel MM subsystem to reclaim
154 * memory.
155 */
156static void frontswap_selfshrink(void)
157{
158	static unsigned long cur_frontswap_pages;
159	static unsigned long last_frontswap_pages;
160	static unsigned long tgt_frontswap_pages;
161
162	last_frontswap_pages = cur_frontswap_pages;
163	cur_frontswap_pages = frontswap_curr_pages();
164	if (!cur_frontswap_pages ||
165			(cur_frontswap_pages > last_frontswap_pages)) {
166		frontswap_inertia_counter = frontswap_inertia;
167		return;
168	}
169	if (frontswap_inertia_counter && --frontswap_inertia_counter)
170		return;
171	if (cur_frontswap_pages <= frontswap_hysteresis)
172		tgt_frontswap_pages = 0;
173	else
174		tgt_frontswap_pages = cur_frontswap_pages -
175			(cur_frontswap_pages / frontswap_hysteresis);
176	frontswap_shrink(tgt_frontswap_pages);
177}
178
179static int __init xen_nofrontswap_selfshrink_setup(char *s)
180{
181	use_frontswap_selfshrink = false;
182	return 1;
183}
184
185__setup("noselfshrink", xen_nofrontswap_selfshrink_setup);
186
187/* Disable with kernel boot option. */
188static bool use_selfballooning __initdata = true;
189
190static int __init xen_noselfballooning_setup(char *s)
191{
192	use_selfballooning = false;
193	return 1;
194}
195
196__setup("noselfballooning", xen_noselfballooning_setup);
197#else /* !CONFIG_FRONTSWAP */
198/* Enable with kernel boot option. */
199static bool use_selfballooning __initdata = false;
200
201static int __init xen_selfballooning_setup(char *s)
202{
203	use_selfballooning = true;
204	return 1;
205}
206
207__setup("selfballooning", xen_selfballooning_setup);
208#endif /* CONFIG_FRONTSWAP */
209
210#define MB2PAGES(mb)	((mb) << (20 - PAGE_SHIFT))
211
212/*
213 * Use current balloon size, the goal (vm_committed_as), and hysteresis
214 * parameters to set a new target balloon size
215 */
216static void selfballoon_process(struct work_struct *work)
217{
218	unsigned long cur_pages, goal_pages, tgt_pages, floor_pages;
219	unsigned long useful_pages;
220	bool reset_timer = false;
221
222	if (xen_selfballooning_enabled) {
223		cur_pages = totalram_pages;
224		tgt_pages = cur_pages; /* default is no change */
225		goal_pages = percpu_counter_read_positive(&vm_committed_as) +
226				totalreserve_pages +
227				MB2PAGES(selfballoon_reserved_mb);
228#ifdef CONFIG_FRONTSWAP
229		/* allow space for frontswap pages to be repatriated */
230		if (frontswap_selfshrinking && frontswap_enabled)
231			goal_pages += frontswap_curr_pages();
232#endif
233		if (cur_pages > goal_pages)
234			tgt_pages = cur_pages -
235				((cur_pages - goal_pages) /
236				  selfballoon_downhysteresis);
237		else if (cur_pages < goal_pages)
238			tgt_pages = cur_pages +
239				((goal_pages - cur_pages) /
240				  selfballoon_uphysteresis);
241		/* else if cur_pages == goal_pages, no change */
242		useful_pages = max_pfn - totalreserve_pages;
243		if (selfballoon_min_usable_mb != 0)
244			floor_pages = totalreserve_pages +
245					MB2PAGES(selfballoon_min_usable_mb);
246		/* piecewise linear function ending in ~3% slope */
247		else if (useful_pages < MB2PAGES(16))
248			floor_pages = max_pfn; /* not worth ballooning */
249		else if (useful_pages < MB2PAGES(64))
250			floor_pages = totalreserve_pages + MB2PAGES(16) +
251					((useful_pages - MB2PAGES(16)) >> 1);
252		else if (useful_pages < MB2PAGES(512))
253			floor_pages = totalreserve_pages + MB2PAGES(40) +
254					((useful_pages - MB2PAGES(40)) >> 3);
255		else /* useful_pages >= MB2PAGES(512) */
256			floor_pages = totalreserve_pages + MB2PAGES(99) +
257					((useful_pages - MB2PAGES(99)) >> 5);
258		if (tgt_pages < floor_pages)
259			tgt_pages = floor_pages;
260		balloon_set_new_target(tgt_pages +
261			balloon_stats.current_pages - totalram_pages);
262		reset_timer = true;
263	}
264#ifdef CONFIG_FRONTSWAP
265	if (frontswap_selfshrinking && frontswap_enabled) {
266		frontswap_selfshrink();
267		reset_timer = true;
268	}
269#endif
270	if (reset_timer)
271		schedule_delayed_work(&selfballoon_worker,
272			selfballoon_interval * HZ);
273}
274
275#ifdef CONFIG_SYSFS
276
 
277#include <linux/capability.h>
278
279#define SELFBALLOON_SHOW(name, format, args...)				\
280	static ssize_t show_##name(struct device *dev,	\
281					  struct device_attribute *attr, \
282					  char *buf) \
283	{ \
284		return sprintf(buf, format, ##args); \
285	}
286
287SELFBALLOON_SHOW(selfballooning, "%d\n", xen_selfballooning_enabled);
288
289static ssize_t store_selfballooning(struct device *dev,
290			    struct device_attribute *attr,
291			    const char *buf,
292			    size_t count)
293{
294	bool was_enabled = xen_selfballooning_enabled;
295	unsigned long tmp;
296	int err;
297
298	if (!capable(CAP_SYS_ADMIN))
299		return -EPERM;
300
301	err = strict_strtoul(buf, 10, &tmp);
302	if (err || ((tmp != 0) && (tmp != 1)))
303		return -EINVAL;
304
305	xen_selfballooning_enabled = !!tmp;
306	if (!was_enabled && xen_selfballooning_enabled)
307		schedule_delayed_work(&selfballoon_worker,
308			selfballoon_interval * HZ);
309
310	return count;
311}
312
313static DEVICE_ATTR(selfballooning, S_IRUGO | S_IWUSR,
314		   show_selfballooning, store_selfballooning);
315
316SELFBALLOON_SHOW(selfballoon_interval, "%d\n", selfballoon_interval);
317
318static ssize_t store_selfballoon_interval(struct device *dev,
319					  struct device_attribute *attr,
320					  const char *buf,
321					  size_t count)
322{
323	unsigned long val;
324	int err;
325
326	if (!capable(CAP_SYS_ADMIN))
327		return -EPERM;
328	err = strict_strtoul(buf, 10, &val);
329	if (err || val == 0)
330		return -EINVAL;
331	selfballoon_interval = val;
332	return count;
333}
334
335static DEVICE_ATTR(selfballoon_interval, S_IRUGO | S_IWUSR,
336		   show_selfballoon_interval, store_selfballoon_interval);
337
338SELFBALLOON_SHOW(selfballoon_downhys, "%d\n", selfballoon_downhysteresis);
339
340static ssize_t store_selfballoon_downhys(struct device *dev,
341					 struct device_attribute *attr,
342					 const char *buf,
343					 size_t count)
344{
345	unsigned long val;
346	int err;
347
348	if (!capable(CAP_SYS_ADMIN))
349		return -EPERM;
350	err = strict_strtoul(buf, 10, &val);
351	if (err || val == 0)
352		return -EINVAL;
353	selfballoon_downhysteresis = val;
354	return count;
355}
356
357static DEVICE_ATTR(selfballoon_downhysteresis, S_IRUGO | S_IWUSR,
358		   show_selfballoon_downhys, store_selfballoon_downhys);
359
360
361SELFBALLOON_SHOW(selfballoon_uphys, "%d\n", selfballoon_uphysteresis);
362
363static ssize_t store_selfballoon_uphys(struct device *dev,
364				       struct device_attribute *attr,
365				       const char *buf,
366				       size_t count)
367{
368	unsigned long val;
369	int err;
370
371	if (!capable(CAP_SYS_ADMIN))
372		return -EPERM;
373	err = strict_strtoul(buf, 10, &val);
374	if (err || val == 0)
375		return -EINVAL;
376	selfballoon_uphysteresis = val;
377	return count;
378}
379
380static DEVICE_ATTR(selfballoon_uphysteresis, S_IRUGO | S_IWUSR,
381		   show_selfballoon_uphys, store_selfballoon_uphys);
382
383SELFBALLOON_SHOW(selfballoon_min_usable_mb, "%d\n",
384				selfballoon_min_usable_mb);
385
386static ssize_t store_selfballoon_min_usable_mb(struct device *dev,
387					       struct device_attribute *attr,
388					       const char *buf,
389					       size_t count)
390{
391	unsigned long val;
392	int err;
393
394	if (!capable(CAP_SYS_ADMIN))
395		return -EPERM;
396	err = strict_strtoul(buf, 10, &val);
397	if (err || val == 0)
398		return -EINVAL;
399	selfballoon_min_usable_mb = val;
400	return count;
401}
402
403static DEVICE_ATTR(selfballoon_min_usable_mb, S_IRUGO | S_IWUSR,
404		   show_selfballoon_min_usable_mb,
405		   store_selfballoon_min_usable_mb);
406
407SELFBALLOON_SHOW(selfballoon_reserved_mb, "%d\n",
408				selfballoon_reserved_mb);
409
410static ssize_t store_selfballoon_reserved_mb(struct device *dev,
411					     struct device_attribute *attr,
412					     const char *buf,
413					     size_t count)
414{
415	unsigned long val;
416	int err;
417
418	if (!capable(CAP_SYS_ADMIN))
419		return -EPERM;
420	err = strict_strtoul(buf, 10, &val);
421	if (err || val == 0)
422		return -EINVAL;
423	selfballoon_reserved_mb = val;
424	return count;
425}
426
427static DEVICE_ATTR(selfballoon_reserved_mb, S_IRUGO | S_IWUSR,
428		   show_selfballoon_reserved_mb,
429		   store_selfballoon_reserved_mb);
430
431
432#ifdef CONFIG_FRONTSWAP
433SELFBALLOON_SHOW(frontswap_selfshrinking, "%d\n", frontswap_selfshrinking);
434
435static ssize_t store_frontswap_selfshrinking(struct device *dev,
436					     struct device_attribute *attr,
437					     const char *buf,
438					     size_t count)
439{
440	bool was_enabled = frontswap_selfshrinking;
441	unsigned long tmp;
442	int err;
443
444	if (!capable(CAP_SYS_ADMIN))
445		return -EPERM;
446	err = strict_strtoul(buf, 10, &tmp);
447	if (err || ((tmp != 0) && (tmp != 1)))
448		return -EINVAL;
449	frontswap_selfshrinking = !!tmp;
450	if (!was_enabled && !xen_selfballooning_enabled &&
451	     frontswap_selfshrinking)
452		schedule_delayed_work(&selfballoon_worker,
453			selfballoon_interval * HZ);
454
455	return count;
456}
457
458static DEVICE_ATTR(frontswap_selfshrinking, S_IRUGO | S_IWUSR,
459		   show_frontswap_selfshrinking, store_frontswap_selfshrinking);
460
461SELFBALLOON_SHOW(frontswap_inertia, "%d\n", frontswap_inertia);
462
463static ssize_t store_frontswap_inertia(struct device *dev,
464				       struct device_attribute *attr,
465				       const char *buf,
466				       size_t count)
467{
468	unsigned long val;
469	int err;
470
471	if (!capable(CAP_SYS_ADMIN))
472		return -EPERM;
473	err = strict_strtoul(buf, 10, &val);
474	if (err || val == 0)
475		return -EINVAL;
476	frontswap_inertia = val;
477	frontswap_inertia_counter = val;
478	return count;
479}
480
481static DEVICE_ATTR(frontswap_inertia, S_IRUGO | S_IWUSR,
482		   show_frontswap_inertia, store_frontswap_inertia);
483
484SELFBALLOON_SHOW(frontswap_hysteresis, "%d\n", frontswap_hysteresis);
485
486static ssize_t store_frontswap_hysteresis(struct device *dev,
487					  struct device_attribute *attr,
488					  const char *buf,
489					  size_t count)
490{
491	unsigned long val;
492	int err;
493
494	if (!capable(CAP_SYS_ADMIN))
495		return -EPERM;
496	err = strict_strtoul(buf, 10, &val);
497	if (err || val == 0)
498		return -EINVAL;
499	frontswap_hysteresis = val;
500	return count;
501}
502
503static DEVICE_ATTR(frontswap_hysteresis, S_IRUGO | S_IWUSR,
504		   show_frontswap_hysteresis, store_frontswap_hysteresis);
505
506#endif /* CONFIG_FRONTSWAP */
507
508static struct attribute *selfballoon_attrs[] = {
509	&dev_attr_selfballooning.attr,
510	&dev_attr_selfballoon_interval.attr,
511	&dev_attr_selfballoon_downhysteresis.attr,
512	&dev_attr_selfballoon_uphysteresis.attr,
513	&dev_attr_selfballoon_min_usable_mb.attr,
514	&dev_attr_selfballoon_reserved_mb.attr,
515#ifdef CONFIG_FRONTSWAP
516	&dev_attr_frontswap_selfshrinking.attr,
517	&dev_attr_frontswap_hysteresis.attr,
518	&dev_attr_frontswap_inertia.attr,
519#endif
520	NULL
521};
522
523static const struct attribute_group selfballoon_group = {
524	.name = "selfballoon",
525	.attrs = selfballoon_attrs
526};
527#endif
528
529int register_xen_selfballooning(struct device *dev)
530{
531	int error = -1;
532
533#ifdef CONFIG_SYSFS
534	error = sysfs_create_group(&dev->kobj, &selfballoon_group);
535#endif
536	return error;
537}
538EXPORT_SYMBOL(register_xen_selfballooning);
539
540static int __init xen_selfballoon_init(void)
541{
542	bool enable = false;
543
544	if (!xen_domain())
545		return -ENODEV;
546
547	if (xen_initial_domain()) {
548		pr_info("xen/balloon: Xen selfballooning driver "
549				"disabled for domain0.\n");
550		return -ENODEV;
551	}
552
553	xen_selfballooning_enabled = tmem_enabled && use_selfballooning;
554	if (xen_selfballooning_enabled) {
555		pr_info("xen/balloon: Initializing Xen "
556					"selfballooning driver.\n");
557		enable = true;
558	}
559#ifdef CONFIG_FRONTSWAP
560	frontswap_selfshrinking = tmem_enabled && use_frontswap_selfshrink;
561	if (frontswap_selfshrinking) {
562		pr_info("xen/balloon: Initializing frontswap "
563					"selfshrinking driver.\n");
564		enable = true;
565	}
566#endif
567	if (!enable)
568		return -ENODEV;
569
570	schedule_delayed_work(&selfballoon_worker, selfballoon_interval * HZ);
571
572	return 0;
573}
574
575subsys_initcall(xen_selfballoon_init);
576
577MODULE_LICENSE("GPL");