1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
83static int xen_selfballooning_enabled __read_mostly;
84
85
86
87
88
89
90
91
92
93static unsigned int selfballoon_downhysteresis __read_mostly = 8;
94static unsigned int selfballoon_uphysteresis __read_mostly = 1;
95
96
97static unsigned int selfballoon_interval __read_mostly = 5;
98
99
100
101
102
103
104
105
106static unsigned int selfballoon_min_usable_mb;
107
108
109
110
111
112static unsigned int selfballoon_reserved_mb;
113
114static void selfballoon_process(struct work_struct *work);
115static DECLARE_DELAYED_WORK(selfballoon_worker, selfballoon_process);
116
117#ifdef CONFIG_FRONTSWAP
118#include <linux/frontswap.h>
119
120
121static bool frontswap_selfshrinking __read_mostly;
122
123
124static bool use_frontswap_selfshrink __initdata = true;
125
126
127
128
129
130
131
132
133static unsigned int frontswap_hysteresis __read_mostly = 20;
134
135
136
137
138
139
140static unsigned int frontswap_inertia __read_mostly = 3;
141
142
143static unsigned long frontswap_inertia_counter;
144
145
146
147
148
149
150
151
152
153
154
155
156static void frontswap_selfshrink(void)
157{
158 static unsigned long cur_frontswap_pages;
159 static unsigned long last_frontswap_pages;
160 static unsigned long tgt_frontswap_pages;
161
162 last_frontswap_pages = cur_frontswap_pages;
163 cur_frontswap_pages = frontswap_curr_pages();
164 if (!cur_frontswap_pages ||
165 (cur_frontswap_pages > last_frontswap_pages)) {
166 frontswap_inertia_counter = frontswap_inertia;
167 return;
168 }
169 if (frontswap_inertia_counter && --frontswap_inertia_counter)
170 return;
171 if (cur_frontswap_pages <= frontswap_hysteresis)
172 tgt_frontswap_pages = 0;
173 else
174 tgt_frontswap_pages = cur_frontswap_pages -
175 (cur_frontswap_pages / frontswap_hysteresis);
176 frontswap_shrink(tgt_frontswap_pages);
177}
178
179static int __init xen_nofrontswap_selfshrink_setup(char *s)
180{
181 use_frontswap_selfshrink = false;
182 return 1;
183}
184
185__setup("noselfshrink", xen_nofrontswap_selfshrink_setup);
186
187
188static bool use_selfballooning __initdata = true;
189
190static int __init xen_noselfballooning_setup(char *s)
191{
192 use_selfballooning = false;
193 return 1;
194}
195
196__setup("noselfballooning", xen_noselfballooning_setup);
197#else
198
199static bool use_selfballooning __initdata = false;
200
201static int __init xen_selfballooning_setup(char *s)
202{
203 use_selfballooning = true;
204 return 1;
205}
206
207__setup("selfballooning", xen_selfballooning_setup);
208#endif
209
210#define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
211
212
213
214
215
216static void selfballoon_process(struct work_struct *work)
217{
218 unsigned long cur_pages, goal_pages, tgt_pages, floor_pages;
219 unsigned long useful_pages;
220 bool reset_timer = false;
221
222 if (xen_selfballooning_enabled) {
223 cur_pages = totalram_pages;
224 tgt_pages = cur_pages;
225 goal_pages = percpu_counter_read_positive(&vm_committed_as) +
226 totalreserve_pages +
227 MB2PAGES(selfballoon_reserved_mb);
228#ifdef CONFIG_FRONTSWAP
229
230 if (frontswap_selfshrinking && frontswap_enabled)
231 goal_pages += frontswap_curr_pages();
232#endif
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);
241
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);
246
247 else if (useful_pages < MB2PAGES(16))
248 floor_pages = max_pfn;
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);
255 else
256 floor_pages = totalreserve_pages + MB2PAGES(99) +
257 ((useful_pages - MB2PAGES(99)) >> 5);
258 if (tgt_pages < floor_pages)
259 tgt_pages = floor_pages;
260 balloon_set_new_target(tgt_pages +
261 balloon_stats.current_pages - totalram_pages);
262 reset_timer = true;
263 }
264#ifdef CONFIG_FRONTSWAP
265 if (frontswap_selfshrinking && frontswap_enabled) {
266 frontswap_selfshrink();
267 reset_timer = true;
268 }
269#endif
270 if (reset_timer)
271 schedule_delayed_work(&selfballoon_worker,
272 selfballoon_interval * HZ);
273}
274
275#ifdef CONFIG_SYSFS
276
277#include <linux/capability.h>
278
279#define SELFBALLOON_SHOW(name, format, args...) \
280 static ssize_t show_##name(struct device *dev, \
281 struct device_attribute *attr, \
282 char *buf) \
283 { \
284 return sprintf(buf, format, ##args); \
285 }
286
287SELFBALLOON_SHOW(selfballooning, "%d\n", xen_selfballooning_enabled);
288
289static ssize_t store_selfballooning(struct device *dev,
290 struct device_attribute *attr,
291 const char *buf,
292 size_t count)
293{
294 bool was_enabled = xen_selfballooning_enabled;
295 unsigned long tmp;
296 int err;
297
298 if (!capable(CAP_SYS_ADMIN))
299 return -EPERM;
300
301 err = strict_strtoul(buf, 10, &tmp);
302 if (err || ((tmp != 0) && (tmp != 1)))
303 return -EINVAL;
304
305 xen_selfballooning_enabled = !!tmp;
306 if (!was_enabled && xen_selfballooning_enabled)
307 schedule_delayed_work(&selfballoon_worker,
308 selfballoon_interval * HZ);
309
310 return count;
311}
312
313static DEVICE_ATTR(selfballooning, S_IRUGO | S_IWUSR,
314 show_selfballooning, store_selfballooning);
315
316SELFBALLOON_SHOW(selfballoon_interval, "%d\n", selfballoon_interval);
317
318static ssize_t store_selfballoon_interval(struct device *dev,
319 struct device_attribute *attr,
320 const char *buf,
321 size_t count)
322{
323 unsigned long val;
324 int err;
325
326 if (!capable(CAP_SYS_ADMIN))
327 return -EPERM;
328 err = strict_strtoul(buf, 10, &val);
329 if (err || val == 0)
330 return -EINVAL;
331 selfballoon_interval = val;
332 return count;
333}
334
335static DEVICE_ATTR(selfballoon_interval, S_IRUGO | S_IWUSR,
336 show_selfballoon_interval, store_selfballoon_interval);
337
338SELFBALLOON_SHOW(selfballoon_downhys, "%d\n", selfballoon_downhysteresis);
339
340static ssize_t store_selfballoon_downhys(struct device *dev,
341 struct device_attribute *attr,
342 const char *buf,
343 size_t count)
344{
345 unsigned long val;
346 int err;
347
348 if (!capable(CAP_SYS_ADMIN))
349 return -EPERM;
350 err = strict_strtoul(buf, 10, &val);
351 if (err || val == 0)
352 return -EINVAL;
353 selfballoon_downhysteresis = val;
354 return count;
355}
356
357static DEVICE_ATTR(selfballoon_downhysteresis, S_IRUGO | S_IWUSR,
358 show_selfballoon_downhys, store_selfballoon_downhys);
359
360
361SELFBALLOON_SHOW(selfballoon_uphys, "%d\n", selfballoon_uphysteresis);
362
363static ssize_t store_selfballoon_uphys(struct device *dev,
364 struct device_attribute *attr,
365 const char *buf,
366 size_t count)
367{
368 unsigned long val;
369 int err;
370
371 if (!capable(CAP_SYS_ADMIN))
372 return -EPERM;
373 err = strict_strtoul(buf, 10, &val);
374 if (err || val == 0)
375 return -EINVAL;
376 selfballoon_uphysteresis = val;
377 return count;
378}
379
380static DEVICE_ATTR(selfballoon_uphysteresis, S_IRUGO | S_IWUSR,
381 show_selfballoon_uphys, store_selfballoon_uphys);
382
383SELFBALLOON_SHOW(selfballoon_min_usable_mb, "%d\n",
384 selfballoon_min_usable_mb);
385
386static ssize_t store_selfballoon_min_usable_mb(struct device *dev,
387 struct device_attribute *attr,
388 const char *buf,
389 size_t count)
390{
391 unsigned long val;
392 int err;
393
394 if (!capable(CAP_SYS_ADMIN))
395 return -EPERM;
396 err = strict_strtoul(buf, 10, &val);
397 if (err || val == 0)
398 return -EINVAL;
399 selfballoon_min_usable_mb = val;
400 return count;
401}
402
403static DEVICE_ATTR(selfballoon_min_usable_mb, S_IRUGO | S_IWUSR,
404 show_selfballoon_min_usable_mb,
405 store_selfballoon_min_usable_mb);
406
407SELFBALLOON_SHOW(selfballoon_reserved_mb, "%d\n",
408 selfballoon_reserved_mb);
409
410static ssize_t store_selfballoon_reserved_mb(struct device *dev,
411 struct device_attribute *attr,
412 const char *buf,
413 size_t count)
414{
415 unsigned long val;
416 int err;
417
418 if (!capable(CAP_SYS_ADMIN))
419 return -EPERM;
420 err = strict_strtoul(buf, 10, &val);
421 if (err || val == 0)
422 return -EINVAL;
423 selfballoon_reserved_mb = val;
424 return count;
425}
426
427static DEVICE_ATTR(selfballoon_reserved_mb, S_IRUGO | S_IWUSR,
428 show_selfballoon_reserved_mb,
429 store_selfballoon_reserved_mb);
430
431
432#ifdef CONFIG_FRONTSWAP
433SELFBALLOON_SHOW(frontswap_selfshrinking, "%d\n", frontswap_selfshrinking);
434
435static ssize_t store_frontswap_selfshrinking(struct device *dev,
436 struct device_attribute *attr,
437 const char *buf,
438 size_t count)
439{
440 bool was_enabled = frontswap_selfshrinking;
441 unsigned long tmp;
442 int err;
443
444 if (!capable(CAP_SYS_ADMIN))
445 return -EPERM;
446 err = strict_strtoul(buf, 10, &tmp);
447 if (err || ((tmp != 0) && (tmp != 1)))
448 return -EINVAL;
449 frontswap_selfshrinking = !!tmp;
450 if (!was_enabled && !xen_selfballooning_enabled &&
451 frontswap_selfshrinking)
452 schedule_delayed_work(&selfballoon_worker,
453 selfballoon_interval * HZ);
454
455 return count;
456}
457
458static DEVICE_ATTR(frontswap_selfshrinking, S_IRUGO | S_IWUSR,
459 show_frontswap_selfshrinking, store_frontswap_selfshrinking);
460
461SELFBALLOON_SHOW(frontswap_inertia, "%d\n", frontswap_inertia);
462
463static ssize_t store_frontswap_inertia(struct device *dev,
464 struct device_attribute *attr,
465 const char *buf,
466 size_t count)
467{
468 unsigned long val;
469 int err;
470
471 if (!capable(CAP_SYS_ADMIN))
472 return -EPERM;
473 err = strict_strtoul(buf, 10, &val);
474 if (err || val == 0)
475 return -EINVAL;
476 frontswap_inertia = val;
477 frontswap_inertia_counter = val;
478 return count;
479}
480
481static DEVICE_ATTR(frontswap_inertia, S_IRUGO | S_IWUSR,
482 show_frontswap_inertia, store_frontswap_inertia);
483
484SELFBALLOON_SHOW(frontswap_hysteresis, "%d\n", frontswap_hysteresis);
485
486static ssize_t store_frontswap_hysteresis(struct device *dev,
487 struct device_attribute *attr,
488 const char *buf,
489 size_t count)
490{
491 unsigned long val;
492 int err;
493
494 if (!capable(CAP_SYS_ADMIN))
495 return -EPERM;
496 err = strict_strtoul(buf, 10, &val);
497 if (err || val == 0)
498 return -EINVAL;
499 frontswap_hysteresis = val;
500 return count;
501}
502
503static DEVICE_ATTR(frontswap_hysteresis, S_IRUGO | S_IWUSR,
504 show_frontswap_hysteresis, store_frontswap_hysteresis);
505
506#endif
507
508static 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,
519#endif
520 NULL
521};
522
523static const struct attribute_group selfballoon_group = {
524 .name = "selfballoon",
525 .attrs = selfballoon_attrs
526};
527#endif
528
529int register_xen_selfballooning(struct device *dev)
530{
531 int error = -1;
532
533#ifdef CONFIG_SYSFS
534 error = sysfs_create_group(&dev->kobj, &selfballoon_group);
535#endif
536 return error;
537}
538EXPORT_SYMBOL(register_xen_selfballooning);
539
540static int __init xen_selfballoon_init(void)
541{
542 bool enable = false;
543
544 if (!xen_domain())
545 return -ENODEV;
546
547 if (xen_initial_domain()) {
548 pr_info("xen/balloon: Xen selfballooning driver "
549 "disabled for domain0.\n");
550 return -ENODEV;
551 }
552
553 xen_selfballooning_enabled = tmem_enabled && use_selfballooning;
554 if (xen_selfballooning_enabled) {
555 pr_info("xen/balloon: Initializing Xen "
556 "selfballooning driver.\n");
557 enable = true;
558 }
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");
564 enable = true;
565 }
566#endif
567 if (!enable)
568 return -ENODEV;
569
570 schedule_delayed_work(&selfballoon_worker, selfballoon_interval * HZ);
571
572 return 0;
573}
574
575subsys_initcall(xen_selfballoon_init);
576
577MODULE_LICENSE("GPL");
578