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