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