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