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