70 #include <linux/kernel.h>
74 #include <linux/mman.h>
75 #include <linux/module.h>
77 #include <linux/device.h>
93 static unsigned int selfballoon_downhysteresis
__read_mostly = 8;
94 static unsigned int selfballoon_uphysteresis
__read_mostly = 1;
106 static unsigned int selfballoon_min_usable_mb;
112 static unsigned int selfballoon_reserved_mb;
117 #ifdef CONFIG_FRONTSWAP
124 static bool use_frontswap_selfshrink
__initdata =
true;
143 static unsigned long frontswap_inertia_counter;
156 static void frontswap_selfshrink(
void)
158 static unsigned long cur_frontswap_pages;
159 static unsigned long last_frontswap_pages;
160 static unsigned long tgt_frontswap_pages;
162 last_frontswap_pages = cur_frontswap_pages;
164 if (!cur_frontswap_pages ||
165 (cur_frontswap_pages > last_frontswap_pages)) {
166 frontswap_inertia_counter = frontswap_inertia;
169 if (frontswap_inertia_counter && --frontswap_inertia_counter)
171 if (cur_frontswap_pages <= frontswap_hysteresis)
172 tgt_frontswap_pages = 0;
174 tgt_frontswap_pages = cur_frontswap_pages -
175 (cur_frontswap_pages / frontswap_hysteresis);
179 static int __init xen_nofrontswap_selfshrink_setup(
char *
s)
181 use_frontswap_selfshrink =
false;
185 __setup(
"noselfshrink", xen_nofrontswap_selfshrink_setup);
188 static bool use_selfballooning
__initdata =
true;
190 static int __init xen_noselfballooning_setup(
char *
s)
192 use_selfballooning =
false;
196 __setup(
"noselfballooning", xen_noselfballooning_setup);
199 static bool use_selfballooning __initdata =
false;
201 static int __init xen_selfballooning_setup(
char *
s)
203 use_selfballooning =
true;
207 __setup(
"selfballooning", xen_selfballooning_setup);
210 #define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
218 unsigned long cur_pages, goal_pages, tgt_pages, floor_pages;
219 unsigned long useful_pages;
222 if (xen_selfballooning_enabled) {
223 cur_pages = totalram_pages;
224 tgt_pages = cur_pages;
228 #ifdef CONFIG_FRONTSWAP
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);
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);
247 else if (useful_pages <
MB2PAGES(16))
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);
256 floor_pages = totalreserve_pages +
MB2PAGES(99) +
257 ((useful_pages -
MB2PAGES(99)) >> 5);
258 if (tgt_pages < floor_pages)
259 tgt_pages = floor_pages;
264 #ifdef CONFIG_FRONTSWAP
266 frontswap_selfshrink();
272 selfballoon_interval *
HZ);
277 #include <linux/capability.h>
279 #define SELFBALLOON_SHOW(name, format, args...) \
280 static ssize_t show_##name(struct device *dev, \
281 struct device_attribute *attr, \
284 return sprintf(buf, format, ##args); \
287 SELFBALLOON_SHOW(selfballooning,
"%d\n", xen_selfballooning_enabled);
294 bool was_enabled = xen_selfballooning_enabled;
302 if (err || ((tmp != 0) && (tmp != 1)))
305 xen_selfballooning_enabled = !!
tmp;
306 if (!was_enabled && xen_selfballooning_enabled)
308 selfballoon_interval *
HZ);
314 show_selfballooning, store_selfballooning);
316 SELFBALLOON_SHOW(selfballoon_interval,
"%d\n", selfballoon_interval);
318 static ssize_t store_selfballoon_interval(
struct device *dev,
331 selfballoon_interval =
val;
336 show_selfballoon_interval, store_selfballoon_interval);
338 SELFBALLOON_SHOW(selfballoon_downhys,
"%d\n", selfballoon_downhysteresis);
353 selfballoon_downhysteresis =
val;
358 show_selfballoon_downhys, store_selfballoon_downhys);
361 SELFBALLOON_SHOW(selfballoon_uphys,
"%d\n", selfballoon_uphysteresis);
376 selfballoon_uphysteresis =
val;
381 show_selfballoon_uphys, store_selfballoon_uphys);
383 SELFBALLOON_SHOW(selfballoon_min_usable_mb,
"%d\n",
384 selfballoon_min_usable_mb);
386 static ssize_t store_selfballoon_min_usable_mb(
struct device *dev,
399 selfballoon_min_usable_mb =
val;
404 show_selfballoon_min_usable_mb,
405 store_selfballoon_min_usable_mb);
407 SELFBALLOON_SHOW(selfballoon_reserved_mb,
"%d\n",
408 selfballoon_reserved_mb);
410 static ssize_t store_selfballoon_reserved_mb(
struct device *dev,
423 selfballoon_reserved_mb =
val;
428 show_selfballoon_reserved_mb,
429 store_selfballoon_reserved_mb);
432 #ifdef CONFIG_FRONTSWAP
433 SELFBALLOON_SHOW(frontswap_selfshrinking,
"%d\n", frontswap_selfshrinking);
435 static ssize_t store_frontswap_selfshrinking(
struct device *dev,
440 bool was_enabled = frontswap_selfshrinking;
447 if (err || ((tmp != 0) && (tmp != 1)))
449 frontswap_selfshrinking = !!
tmp;
450 if (!was_enabled && !xen_selfballooning_enabled &&
451 frontswap_selfshrinking)
453 selfballoon_interval *
HZ);
459 show_frontswap_selfshrinking, store_frontswap_selfshrinking);
461 SELFBALLOON_SHOW(frontswap_inertia,
"%d\n", frontswap_inertia);
476 frontswap_inertia =
val;
477 frontswap_inertia_counter =
val;
482 show_frontswap_inertia, store_frontswap_inertia);
484 SELFBALLOON_SHOW(frontswap_hysteresis,
"%d\n", frontswap_hysteresis);
486 static ssize_t store_frontswap_hysteresis(
struct device *dev,
499 frontswap_hysteresis =
val;
504 show_frontswap_hysteresis, store_frontswap_hysteresis);
508 static 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,
524 .
name =
"selfballoon",
525 .attrs = selfballoon_attrs
540 static int __init xen_selfballoon_init(
void)
548 pr_info(
"xen/balloon: Xen selfballooning driver "
549 "disabled for domain0.\n");
553 xen_selfballooning_enabled =
tmem_enabled && use_selfballooning;
554 if (xen_selfballooning_enabled) {
555 pr_info(
"xen/balloon: Initializing Xen "
556 "selfballooning driver.\n");
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");