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
108static void selfballoon_process(struct work_struct *work);
109static DECLARE_DELAYED_WORK(selfballoon_worker, selfballoon_process);
110
111#ifdef CONFIG_FRONTSWAP
112#include <linux/frontswap.h>
113
114
115static bool frontswap_selfshrinking __read_mostly;
116
117
118static bool use_frontswap_selfshrink __initdata = true;
119
120
121
122
123
124
125
126
127static unsigned int frontswap_hysteresis __read_mostly = 20;
128
129
130
131
132
133
134static unsigned int frontswap_inertia __read_mostly = 3;
135
136
137static unsigned long frontswap_inertia_counter;
138
139
140
141
142
143
144
145
146
147
148
149
150static void frontswap_selfshrink(void)
151{
152 static unsigned long cur_frontswap_pages;
153 static unsigned long last_frontswap_pages;
154 static unsigned long tgt_frontswap_pages;
155
156 last_frontswap_pages = cur_frontswap_pages;
157 cur_frontswap_pages = frontswap_curr_pages();
158 if (!cur_frontswap_pages ||
159 (cur_frontswap_pages > last_frontswap_pages)) {
160 frontswap_inertia_counter = frontswap_inertia;
161 return;
162 }
163 if (frontswap_inertia_counter && --frontswap_inertia_counter)
164 return;
165 if (cur_frontswap_pages <= frontswap_hysteresis)
166 tgt_frontswap_pages = 0;
167 else
168 tgt_frontswap_pages = cur_frontswap_pages -
169 (cur_frontswap_pages / frontswap_hysteresis);
170 frontswap_shrink(tgt_frontswap_pages);
171}
172
173static int __init xen_nofrontswap_selfshrink_setup(char *s)
174{
175 use_frontswap_selfshrink = false;
176 return 1;
177}
178
179__setup("noselfshrink", xen_nofrontswap_selfshrink_setup);
180
181
182static bool use_selfballooning __initdata = true;
183
184static int __init xen_noselfballooning_setup(char *s)
185{
186 use_selfballooning = false;
187 return 1;
188}
189
190__setup("noselfballooning", xen_noselfballooning_setup);
191#else
192
193static bool use_selfballooning __initdata = false;
194
195static int __init xen_selfballooning_setup(char *s)
196{
197 use_selfballooning = true;
198 return 1;
199}
200
201__setup("selfballooning", xen_selfballooning_setup);
202#endif
203
204#define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
205
206
207
208
209
210static void selfballoon_process(struct work_struct *work)
211{
212 unsigned long cur_pages, goal_pages, tgt_pages, floor_pages;
213 unsigned long useful_pages;
214 bool reset_timer = false;
215
216 if (xen_selfballooning_enabled) {
217 cur_pages = totalram_pages;
218 tgt_pages = cur_pages;
219 goal_pages = percpu_counter_read_positive(&vm_committed_as) +
220 totalreserve_pages;
221#ifdef CONFIG_FRONTSWAP
222
223 if (frontswap_selfshrinking && frontswap_enabled)
224 goal_pages += frontswap_curr_pages();
225#endif
226 if (cur_pages > goal_pages)
227 tgt_pages = cur_pages -
228 ((cur_pages - goal_pages) /
229 selfballoon_downhysteresis);
230 else if (cur_pages < goal_pages)
231 tgt_pages = cur_pages +
232 ((goal_pages - cur_pages) /
233 selfballoon_uphysteresis);
234
235 useful_pages = max_pfn - totalreserve_pages;
236 if (selfballoon_min_usable_mb != 0)
237 floor_pages = totalreserve_pages +
238 MB2PAGES(selfballoon_min_usable_mb);
239
240 else if (useful_pages < MB2PAGES(16))
241 floor_pages = max_pfn;
242 else if (useful_pages < MB2PAGES(64))
243 floor_pages = totalreserve_pages + MB2PAGES(16) +
244 ((useful_pages - MB2PAGES(16)) >> 1);
245 else if (useful_pages < MB2PAGES(512))
246 floor_pages = totalreserve_pages + MB2PAGES(40) +
247 ((useful_pages - MB2PAGES(40)) >> 3);
248 else
249 floor_pages = totalreserve_pages + MB2PAGES(99) +
250 ((useful_pages - MB2PAGES(99)) >> 5);
251 if (tgt_pages < floor_pages)
252 tgt_pages = floor_pages;
253 balloon_set_new_target(tgt_pages +
254 balloon_stats.current_pages - totalram_pages);
255 reset_timer = true;
256 }
257#ifdef CONFIG_FRONTSWAP
258 if (frontswap_selfshrinking && frontswap_enabled) {
259 frontswap_selfshrink();
260 reset_timer = true;
261 }
262#endif
263 if (reset_timer)
264 schedule_delayed_work(&selfballoon_worker,
265 selfballoon_interval * HZ);
266}
267
268#ifdef CONFIG_SYSFS
269
270#include <linux/capability.h>
271
272#define SELFBALLOON_SHOW(name, format, args...) \
273 static ssize_t show_##name(struct device *dev, \
274 struct device_attribute *attr, \
275 char *buf) \
276 { \
277 return sprintf(buf, format, ##args); \
278 }
279
280SELFBALLOON_SHOW(selfballooning, "%d\n", xen_selfballooning_enabled);
281
282static ssize_t store_selfballooning(struct device *dev,
283 struct device_attribute *attr,
284 const char *buf,
285 size_t count)
286{
287 bool was_enabled = xen_selfballooning_enabled;
288 unsigned long tmp;
289 int err;
290
291 if (!capable(CAP_SYS_ADMIN))
292 return -EPERM;
293
294 err = strict_strtoul(buf, 10, &tmp);
295 if (err || ((tmp != 0) && (tmp != 1)))
296 return -EINVAL;
297
298 xen_selfballooning_enabled = !!tmp;
299 if (!was_enabled && xen_selfballooning_enabled)
300 schedule_delayed_work(&selfballoon_worker,
301 selfballoon_interval * HZ);
302
303 return count;
304}
305
306static DEVICE_ATTR(selfballooning, S_IRUGO | S_IWUSR,
307 show_selfballooning, store_selfballooning);
308
309SELFBALLOON_SHOW(selfballoon_interval, "%d\n", selfballoon_interval);
310
311static ssize_t store_selfballoon_interval(struct device *dev,
312 struct device_attribute *attr,
313 const char *buf,
314 size_t count)
315{
316 unsigned long val;
317 int err;
318
319 if (!capable(CAP_SYS_ADMIN))
320 return -EPERM;
321 err = strict_strtoul(buf, 10, &val);
322 if (err || val == 0)
323 return -EINVAL;
324 selfballoon_interval = val;
325 return count;
326}
327
328static DEVICE_ATTR(selfballoon_interval, S_IRUGO | S_IWUSR,
329 show_selfballoon_interval, store_selfballoon_interval);
330
331SELFBALLOON_SHOW(selfballoon_downhys, "%d\n", selfballoon_downhysteresis);
332
333static ssize_t store_selfballoon_downhys(struct device *dev,
334 struct device_attribute *attr,
335 const char *buf,
336 size_t count)
337{
338 unsigned long val;
339 int err;
340
341 if (!capable(CAP_SYS_ADMIN))
342 return -EPERM;
343 err = strict_strtoul(buf, 10, &val);
344 if (err || val == 0)
345 return -EINVAL;
346 selfballoon_downhysteresis = val;
347 return count;
348}
349
350static DEVICE_ATTR(selfballoon_downhysteresis, S_IRUGO | S_IWUSR,
351 show_selfballoon_downhys, store_selfballoon_downhys);
352
353
354SELFBALLOON_SHOW(selfballoon_uphys, "%d\n", selfballoon_uphysteresis);
355
356static ssize_t store_selfballoon_uphys(struct device *dev,
357 struct device_attribute *attr,
358 const char *buf,
359 size_t count)
360{
361 unsigned long val;
362 int err;
363
364 if (!capable(CAP_SYS_ADMIN))
365 return -EPERM;
366 err = strict_strtoul(buf, 10, &val);
367 if (err || val == 0)
368 return -EINVAL;
369 selfballoon_uphysteresis = val;
370 return count;
371}
372
373static DEVICE_ATTR(selfballoon_uphysteresis, S_IRUGO | S_IWUSR,
374 show_selfballoon_uphys, store_selfballoon_uphys);
375
376SELFBALLOON_SHOW(selfballoon_min_usable_mb, "%d\n",
377 selfballoon_min_usable_mb);
378
379static ssize_t store_selfballoon_min_usable_mb(struct device *dev,
380 struct device_attribute *attr,
381 const char *buf,
382 size_t count)
383{
384 unsigned long val;
385 int err;
386
387 if (!capable(CAP_SYS_ADMIN))
388 return -EPERM;
389 err = strict_strtoul(buf, 10, &val);
390 if (err || val == 0)
391 return -EINVAL;
392 selfballoon_min_usable_mb = val;
393 return count;
394}
395
396static DEVICE_ATTR(selfballoon_min_usable_mb, S_IRUGO | S_IWUSR,
397 show_selfballoon_min_usable_mb,
398 store_selfballoon_min_usable_mb);
399
400
401#ifdef CONFIG_FRONTSWAP
402SELFBALLOON_SHOW(frontswap_selfshrinking, "%d\n", frontswap_selfshrinking);
403
404static ssize_t store_frontswap_selfshrinking(struct device *dev,
405 struct device_attribute *attr,
406 const char *buf,
407 size_t count)
408{
409 bool was_enabled = frontswap_selfshrinking;
410 unsigned long tmp;
411 int err;
412
413 if (!capable(CAP_SYS_ADMIN))
414 return -EPERM;
415 err = strict_strtoul(buf, 10, &tmp);
416 if (err || ((tmp != 0) && (tmp != 1)))
417 return -EINVAL;
418 frontswap_selfshrinking = !!tmp;
419 if (!was_enabled && !xen_selfballooning_enabled &&
420 frontswap_selfshrinking)
421 schedule_delayed_work(&selfballoon_worker,
422 selfballoon_interval * HZ);
423
424 return count;
425}
426
427static DEVICE_ATTR(frontswap_selfshrinking, S_IRUGO | S_IWUSR,
428 show_frontswap_selfshrinking, store_frontswap_selfshrinking);
429
430SELFBALLOON_SHOW(frontswap_inertia, "%d\n", frontswap_inertia);
431
432static ssize_t store_frontswap_inertia(struct device *dev,
433 struct device_attribute *attr,
434 const char *buf,
435 size_t count)
436{
437 unsigned long val;
438 int err;
439
440 if (!capable(CAP_SYS_ADMIN))
441 return -EPERM;
442 err = strict_strtoul(buf, 10, &val);
443 if (err || val == 0)
444 return -EINVAL;
445 frontswap_inertia = val;
446 frontswap_inertia_counter = val;
447 return count;
448}
449
450static DEVICE_ATTR(frontswap_inertia, S_IRUGO | S_IWUSR,
451 show_frontswap_inertia, store_frontswap_inertia);
452
453SELFBALLOON_SHOW(frontswap_hysteresis, "%d\n", frontswap_hysteresis);
454
455static ssize_t store_frontswap_hysteresis(struct device *dev,
456 struct device_attribute *attr,
457 const char *buf,
458 size_t count)
459{
460 unsigned long val;
461 int err;
462
463 if (!capable(CAP_SYS_ADMIN))
464 return -EPERM;
465 err = strict_strtoul(buf, 10, &val);
466 if (err || val == 0)
467 return -EINVAL;
468 frontswap_hysteresis = val;
469 return count;
470}
471
472static DEVICE_ATTR(frontswap_hysteresis, S_IRUGO | S_IWUSR,
473 show_frontswap_hysteresis, store_frontswap_hysteresis);
474
475#endif
476
477static struct attribute *selfballoon_attrs[] = {
478 &dev_attr_selfballooning.attr,
479 &dev_attr_selfballoon_interval.attr,
480 &dev_attr_selfballoon_downhysteresis.attr,
481 &dev_attr_selfballoon_uphysteresis.attr,
482 &dev_attr_selfballoon_min_usable_mb.attr,
483#ifdef CONFIG_FRONTSWAP
484 &dev_attr_frontswap_selfshrinking.attr,
485 &dev_attr_frontswap_hysteresis.attr,
486 &dev_attr_frontswap_inertia.attr,
487#endif
488 NULL
489};
490
491static struct attribute_group selfballoon_group = {
492 .name = "selfballoon",
493 .attrs = selfballoon_attrs
494};
495#endif
496
497int register_xen_selfballooning(struct device *dev)
498{
499 int error = -1;
500
501#ifdef CONFIG_SYSFS
502 error = sysfs_create_group(&dev->kobj, &selfballoon_group);
503#endif
504 return error;
505}
506EXPORT_SYMBOL(register_xen_selfballooning);
507
508static int __init xen_selfballoon_init(void)
509{
510 bool enable = false;
511
512 if (!xen_domain())
513 return -ENODEV;
514
515 if (xen_initial_domain()) {
516 pr_info("xen/balloon: Xen selfballooning driver "
517 "disabled for domain0.\n");
518 return -ENODEV;
519 }
520
521 xen_selfballooning_enabled = tmem_enabled && use_selfballooning;
522 if (xen_selfballooning_enabled) {
523 pr_info("xen/balloon: Initializing Xen "
524 "selfballooning driver.\n");
525 enable = true;
526 }
527#ifdef CONFIG_FRONTSWAP
528 frontswap_selfshrinking = tmem_enabled && use_frontswap_selfshrink;
529 if (frontswap_selfshrinking) {
530 pr_info("xen/balloon: Initializing frontswap "
531 "selfshrinking driver.\n");
532 enable = true;
533 }
534#endif
535 if (!enable)
536 return -ENODEV;
537
538 schedule_delayed_work(&selfballoon_worker, selfballoon_interval * HZ);
539
540 return 0;
541}
542
543subsys_initcall(xen_selfballoon_init);
544
545MODULE_LICENSE("GPL");
546