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