aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/misc/vmw_balloon.c
diff options
context:
space:
mode:
authorDmitry Torokhov <dtor@vmware.com>2010-09-22 16:04:57 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2010-09-22 20:22:38 -0400
commitd544b7a40ad3423676b8876aad64fc5f87296b2d (patch)
treef08302b1ce132a71aa8702841846a94ef8e999f3 /drivers/misc/vmw_balloon.c
parent710224fa2750cf449c02dd115548acebfdd2c86a (diff)
vmware balloon: rename module
In an effort to minimize customer confusion we want to unify naming convention for VMware-provided kernel modules. This change renames the balloon driver from vmware_ballon to vmw_balloon. We expect to follow this naming convention (vmw_<module_name>) for all modules that are part of mainline kernel and/or being distributed by VMware, with the sole exception of vmxnet3 driver (since the name of mainline driver happens to match with the name used in VMware Tools). Signed-off-by: Dmitry Torokhov <dtor@vmware.com> Acked-by: Bhavesh Davda <bhavesh@vmware.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers/misc/vmw_balloon.c')
-rw-r--r--drivers/misc/vmw_balloon.c844
1 files changed, 844 insertions, 0 deletions
diff --git a/drivers/misc/vmw_balloon.c b/drivers/misc/vmw_balloon.c
new file mode 100644
index 000000000000..2a1e804a71aa
--- /dev/null
+++ b/drivers/misc/vmw_balloon.c
@@ -0,0 +1,844 @@
1/*
2 * VMware Balloon driver.
3 *
4 * Copyright (C) 2000-2010, VMware, Inc. All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; version 2 of the License and no later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more
14 * details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Maintained by: Dmitry Torokhov <dtor@vmware.com>
21 */
22
23/*
24 * This is VMware physical memory management driver for Linux. The driver
25 * acts like a "balloon" that can be inflated to reclaim physical pages by
26 * reserving them in the guest and invalidating them in the monitor,
27 * freeing up the underlying machine pages so they can be allocated to
28 * other guests. The balloon can also be deflated to allow the guest to
29 * use more physical memory. Higher level policies can control the sizes
30 * of balloons in VMs in order to manage physical memory resources.
31 */
32
33//#define DEBUG
34#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35
36#include <linux/types.h>
37#include <linux/kernel.h>
38#include <linux/mm.h>
39#include <linux/sched.h>
40#include <linux/module.h>
41#include <linux/workqueue.h>
42#include <linux/debugfs.h>
43#include <linux/seq_file.h>
44#include <asm/hypervisor.h>
45
46MODULE_AUTHOR("VMware, Inc.");
47MODULE_DESCRIPTION("VMware Memory Control (Balloon) Driver");
48MODULE_VERSION("1.2.1.1-k");
49MODULE_ALIAS("dmi:*:svnVMware*:*");
50MODULE_ALIAS("vmware_vmmemctl");
51MODULE_LICENSE("GPL");
52
53/*
54 * Various constants controlling rate of inflaint/deflating balloon,
55 * measured in pages.
56 */
57
58/*
59 * Rate of allocating memory when there is no memory pressure
60 * (driver performs non-sleeping allocations).
61 */
62#define VMW_BALLOON_NOSLEEP_ALLOC_MAX 16384U
63
64/*
65 * Rates of memory allocaton when guest experiences memory pressure
66 * (driver performs sleeping allocations).
67 */
68#define VMW_BALLOON_RATE_ALLOC_MIN 512U
69#define VMW_BALLOON_RATE_ALLOC_MAX 2048U
70#define VMW_BALLOON_RATE_ALLOC_INC 16U
71
72/*
73 * Rates for releasing pages while deflating balloon.
74 */
75#define VMW_BALLOON_RATE_FREE_MIN 512U
76#define VMW_BALLOON_RATE_FREE_MAX 16384U
77#define VMW_BALLOON_RATE_FREE_INC 16U
78
79/*
80 * When guest is under memory pressure, use a reduced page allocation
81 * rate for next several cycles.
82 */
83#define VMW_BALLOON_SLOW_CYCLES 4
84
85/*
86 * Use __GFP_HIGHMEM to allow pages from HIGHMEM zone. We don't
87 * allow wait (__GFP_WAIT) for NOSLEEP page allocations. Use
88 * __GFP_NOWARN, to suppress page allocation failure warnings.
89 */
90#define VMW_PAGE_ALLOC_NOSLEEP (__GFP_HIGHMEM|__GFP_NOWARN)
91
92/*
93 * Use GFP_HIGHUSER when executing in a separate kernel thread
94 * context and allocation can sleep. This is less stressful to
95 * the guest memory system, since it allows the thread to block
96 * while memory is reclaimed, and won't take pages from emergency
97 * low-memory pools.
98 */
99#define VMW_PAGE_ALLOC_CANSLEEP (GFP_HIGHUSER)
100
101/* Maximum number of page allocations without yielding processor */
102#define VMW_BALLOON_YIELD_THRESHOLD 1024
103
104/* Maximum number of refused pages we accumulate during inflation cycle */
105#define VMW_BALLOON_MAX_REFUSED 16
106
107/*
108 * Hypervisor communication port definitions.
109 */
110#define VMW_BALLOON_HV_PORT 0x5670
111#define VMW_BALLOON_HV_MAGIC 0x456c6d6f
112#define VMW_BALLOON_PROTOCOL_VERSION 2
113#define VMW_BALLOON_GUEST_ID 1 /* Linux */
114
115#define VMW_BALLOON_CMD_START 0
116#define VMW_BALLOON_CMD_GET_TARGET 1
117#define VMW_BALLOON_CMD_LOCK 2
118#define VMW_BALLOON_CMD_UNLOCK 3
119#define VMW_BALLOON_CMD_GUEST_ID 4
120
121/* error codes */
122#define VMW_BALLOON_SUCCESS 0
123#define VMW_BALLOON_FAILURE -1
124#define VMW_BALLOON_ERROR_CMD_INVALID 1
125#define VMW_BALLOON_ERROR_PPN_INVALID 2
126#define VMW_BALLOON_ERROR_PPN_LOCKED 3
127#define VMW_BALLOON_ERROR_PPN_UNLOCKED 4
128#define VMW_BALLOON_ERROR_PPN_PINNED 5
129#define VMW_BALLOON_ERROR_PPN_NOTNEEDED 6
130#define VMW_BALLOON_ERROR_RESET 7
131#define VMW_BALLOON_ERROR_BUSY 8
132
133#define VMWARE_BALLOON_CMD(cmd, data, result) \
134({ \
135 unsigned long __stat, __dummy1, __dummy2; \
136 __asm__ __volatile__ ("inl (%%dx)" : \
137 "=a"(__stat), \
138 "=c"(__dummy1), \
139 "=d"(__dummy2), \
140 "=b"(result) : \
141 "0"(VMW_BALLOON_HV_MAGIC), \
142 "1"(VMW_BALLOON_CMD_##cmd), \
143 "2"(VMW_BALLOON_HV_PORT), \
144 "3"(data) : \
145 "memory"); \
146 result &= -1UL; \
147 __stat & -1UL; \
148})
149
150#ifdef CONFIG_DEBUG_FS
151struct vmballoon_stats {
152 unsigned int timer;
153
154 /* allocation statustics */
155 unsigned int alloc;
156 unsigned int alloc_fail;
157 unsigned int sleep_alloc;
158 unsigned int sleep_alloc_fail;
159 unsigned int refused_alloc;
160 unsigned int refused_free;
161 unsigned int free;
162
163 /* monitor operations */
164 unsigned int lock;
165 unsigned int lock_fail;
166 unsigned int unlock;
167 unsigned int unlock_fail;
168 unsigned int target;
169 unsigned int target_fail;
170 unsigned int start;
171 unsigned int start_fail;
172 unsigned int guest_type;
173 unsigned int guest_type_fail;
174};
175
176#define STATS_INC(stat) (stat)++
177#else
178#define STATS_INC(stat)
179#endif
180
181struct vmballoon {
182
183 /* list of reserved physical pages */
184 struct list_head pages;
185
186 /* transient list of non-balloonable pages */
187 struct list_head refused_pages;
188 unsigned int n_refused_pages;
189
190 /* balloon size in pages */
191 unsigned int size;
192 unsigned int target;
193
194 /* reset flag */
195 bool reset_required;
196
197 /* adjustment rates (pages per second) */
198 unsigned int rate_alloc;
199 unsigned int rate_free;
200
201 /* slowdown page allocations for next few cycles */
202 unsigned int slow_allocation_cycles;
203
204#ifdef CONFIG_DEBUG_FS
205 /* statistics */
206 struct vmballoon_stats stats;
207
208 /* debugfs file exporting statistics */
209 struct dentry *dbg_entry;
210#endif
211
212 struct sysinfo sysinfo;
213
214 struct delayed_work dwork;
215};
216
217static struct vmballoon balloon;
218static struct workqueue_struct *vmballoon_wq;
219
220/*
221 * Send "start" command to the host, communicating supported version
222 * of the protocol.
223 */
224static bool vmballoon_send_start(struct vmballoon *b)
225{
226 unsigned long status, dummy;
227
228 STATS_INC(b->stats.start);
229
230 status = VMWARE_BALLOON_CMD(START, VMW_BALLOON_PROTOCOL_VERSION, dummy);
231 if (status == VMW_BALLOON_SUCCESS)
232 return true;
233
234 pr_debug("%s - failed, hv returns %ld\n", __func__, status);
235 STATS_INC(b->stats.start_fail);
236 return false;
237}
238
239static bool vmballoon_check_status(struct vmballoon *b, unsigned long status)
240{
241 switch (status) {
242 case VMW_BALLOON_SUCCESS:
243 return true;
244
245 case VMW_BALLOON_ERROR_RESET:
246 b->reset_required = true;
247 /* fall through */
248
249 default:
250 return false;
251 }
252}
253
254/*
255 * Communicate guest type to the host so that it can adjust ballooning
256 * algorithm to the one most appropriate for the guest. This command
257 * is normally issued after sending "start" command and is part of
258 * standard reset sequence.
259 */
260static bool vmballoon_send_guest_id(struct vmballoon *b)
261{
262 unsigned long status, dummy;
263
264 status = VMWARE_BALLOON_CMD(GUEST_ID, VMW_BALLOON_GUEST_ID, dummy);
265
266 STATS_INC(b->stats.guest_type);
267
268 if (vmballoon_check_status(b, status))
269 return true;
270
271 pr_debug("%s - failed, hv returns %ld\n", __func__, status);
272 STATS_INC(b->stats.guest_type_fail);
273 return false;
274}
275
276/*
277 * Retrieve desired balloon size from the host.
278 */
279static bool vmballoon_send_get_target(struct vmballoon *b, u32 *new_target)
280{
281 unsigned long status;
282 unsigned long target;
283 unsigned long limit;
284 u32 limit32;
285
286 /*
287 * si_meminfo() is cheap. Moreover, we want to provide dynamic
288 * max balloon size later. So let us call si_meminfo() every
289 * iteration.
290 */
291 si_meminfo(&b->sysinfo);
292 limit = b->sysinfo.totalram;
293
294 /* Ensure limit fits in 32-bits */
295 limit32 = (u32)limit;
296 if (limit != limit32)
297 return false;
298
299 /* update stats */
300 STATS_INC(b->stats.target);
301
302 status = VMWARE_BALLOON_CMD(GET_TARGET, limit, target);
303 if (vmballoon_check_status(b, status)) {
304 *new_target = target;
305 return true;
306 }
307
308 pr_debug("%s - failed, hv returns %ld\n", __func__, status);
309 STATS_INC(b->stats.target_fail);
310 return false;
311}
312
313/*
314 * Notify the host about allocated page so that host can use it without
315 * fear that guest will need it. Host may reject some pages, we need to
316 * check the return value and maybe submit a different page.
317 */
318static bool vmballoon_send_lock_page(struct vmballoon *b, unsigned long pfn)
319{
320 unsigned long status, dummy;
321 u32 pfn32;
322
323 pfn32 = (u32)pfn;
324 if (pfn32 != pfn)
325 return false;
326
327 STATS_INC(b->stats.lock);
328
329 status = VMWARE_BALLOON_CMD(LOCK, pfn, dummy);
330 if (vmballoon_check_status(b, status))
331 return true;
332
333 pr_debug("%s - ppn %lx, hv returns %ld\n", __func__, pfn, status);
334 STATS_INC(b->stats.lock_fail);
335 return false;
336}
337
338/*
339 * Notify the host that guest intends to release given page back into
340 * the pool of available (to the guest) pages.
341 */
342static bool vmballoon_send_unlock_page(struct vmballoon *b, unsigned long pfn)
343{
344 unsigned long status, dummy;
345 u32 pfn32;
346
347 pfn32 = (u32)pfn;
348 if (pfn32 != pfn)
349 return false;
350
351 STATS_INC(b->stats.unlock);
352
353 status = VMWARE_BALLOON_CMD(UNLOCK, pfn, dummy);
354 if (vmballoon_check_status(b, status))
355 return true;
356
357 pr_debug("%s - ppn %lx, hv returns %ld\n", __func__, pfn, status);
358 STATS_INC(b->stats.unlock_fail);
359 return false;
360}
361
362/*
363 * Quickly release all pages allocated for the balloon. This function is
364 * called when host decides to "reset" balloon for one reason or another.
365 * Unlike normal "deflate" we do not (shall not) notify host of the pages
366 * being released.
367 */
368static void vmballoon_pop(struct vmballoon *b)
369{
370 struct page *page, *next;
371 unsigned int count = 0;
372
373 list_for_each_entry_safe(page, next, &b->pages, lru) {
374 list_del(&page->lru);
375 __free_page(page);
376 STATS_INC(b->stats.free);
377 b->size--;
378
379 if (++count >= b->rate_free) {
380 count = 0;
381 cond_resched();
382 }
383 }
384}
385
386/*
387 * Perform standard reset sequence by popping the balloon (in case it
388 * is not empty) and then restarting protocol. This operation normally
389 * happens when host responds with VMW_BALLOON_ERROR_RESET to a command.
390 */
391static void vmballoon_reset(struct vmballoon *b)
392{
393 /* free all pages, skipping monitor unlock */
394 vmballoon_pop(b);
395
396 if (vmballoon_send_start(b)) {
397 b->reset_required = false;
398 if (!vmballoon_send_guest_id(b))
399 pr_err("failed to send guest ID to the host\n");
400 }
401}
402
403/*
404 * Allocate (or reserve) a page for the balloon and notify the host. If host
405 * refuses the page put it on "refuse" list and allocate another one until host
406 * is satisfied. "Refused" pages are released at the end of inflation cycle
407 * (when we allocate b->rate_alloc pages).
408 */
409static int vmballoon_reserve_page(struct vmballoon *b, bool can_sleep)
410{
411 struct page *page;
412 gfp_t flags;
413 bool locked = false;
414
415 do {
416 if (!can_sleep)
417 STATS_INC(b->stats.alloc);
418 else
419 STATS_INC(b->stats.sleep_alloc);
420
421 flags = can_sleep ? VMW_PAGE_ALLOC_CANSLEEP : VMW_PAGE_ALLOC_NOSLEEP;
422 page = alloc_page(flags);
423 if (!page) {
424 if (!can_sleep)
425 STATS_INC(b->stats.alloc_fail);
426 else
427 STATS_INC(b->stats.sleep_alloc_fail);
428 return -ENOMEM;
429 }
430
431 /* inform monitor */
432 locked = vmballoon_send_lock_page(b, page_to_pfn(page));
433 if (!locked) {
434 STATS_INC(b->stats.refused_alloc);
435
436 if (b->reset_required) {
437 __free_page(page);
438 return -EIO;
439 }
440
441 /*
442 * Place page on the list of non-balloonable pages
443 * and retry allocation, unless we already accumulated
444 * too many of them, in which case take a breather.
445 */
446 list_add(&page->lru, &b->refused_pages);
447 if (++b->n_refused_pages >= VMW_BALLOON_MAX_REFUSED)
448 return -EIO;
449 }
450 } while (!locked);
451
452 /* track allocated page */
453 list_add(&page->lru, &b->pages);
454
455 /* update balloon size */
456 b->size++;
457
458 return 0;
459}
460
461/*
462 * Release the page allocated for the balloon. Note that we first notify
463 * the host so it can make sure the page will be available for the guest
464 * to use, if needed.
465 */
466static int vmballoon_release_page(struct vmballoon *b, struct page *page)
467{
468 if (!vmballoon_send_unlock_page(b, page_to_pfn(page)))
469 return -EIO;
470
471 list_del(&page->lru);
472
473 /* deallocate page */
474 __free_page(page);
475 STATS_INC(b->stats.free);
476
477 /* update balloon size */
478 b->size--;
479
480 return 0;
481}
482
483/*
484 * Release pages that were allocated while attempting to inflate the
485 * balloon but were refused by the host for one reason or another.
486 */
487static void vmballoon_release_refused_pages(struct vmballoon *b)
488{
489 struct page *page, *next;
490
491 list_for_each_entry_safe(page, next, &b->refused_pages, lru) {
492 list_del(&page->lru);
493 __free_page(page);
494 STATS_INC(b->stats.refused_free);
495 }
496
497 b->n_refused_pages = 0;
498}
499
500/*
501 * Inflate the balloon towards its target size. Note that we try to limit
502 * the rate of allocation to make sure we are not choking the rest of the
503 * system.
504 */
505static void vmballoon_inflate(struct vmballoon *b)
506{
507 unsigned int goal;
508 unsigned int rate;
509 unsigned int i;
510 unsigned int allocations = 0;
511 int error = 0;
512 bool alloc_can_sleep = false;
513
514 pr_debug("%s - size: %d, target %d\n", __func__, b->size, b->target);
515
516 /*
517 * First try NOSLEEP page allocations to inflate balloon.
518 *
519 * If we do not throttle nosleep allocations, we can drain all
520 * free pages in the guest quickly (if the balloon target is high).
521 * As a side-effect, draining free pages helps to inform (force)
522 * the guest to start swapping if balloon target is not met yet,
523 * which is a desired behavior. However, balloon driver can consume
524 * all available CPU cycles if too many pages are allocated in a
525 * second. Therefore, we throttle nosleep allocations even when
526 * the guest is not under memory pressure. OTOH, if we have already
527 * predicted that the guest is under memory pressure, then we
528 * slowdown page allocations considerably.
529 */
530
531 goal = b->target - b->size;
532 /*
533 * Start with no sleep allocation rate which may be higher
534 * than sleeping allocation rate.
535 */
536 rate = b->slow_allocation_cycles ?
537 b->rate_alloc : VMW_BALLOON_NOSLEEP_ALLOC_MAX;
538
539 pr_debug("%s - goal: %d, no-sleep rate: %d, sleep rate: %d\n",
540 __func__, goal, rate, b->rate_alloc);
541
542 for (i = 0; i < goal; i++) {
543
544 error = vmballoon_reserve_page(b, alloc_can_sleep);
545 if (error) {
546 if (error != -ENOMEM) {
547 /*
548 * Not a page allocation failure, stop this
549 * cycle. Maybe we'll get new target from
550 * the host soon.
551 */
552 break;
553 }
554
555 if (alloc_can_sleep) {
556 /*
557 * CANSLEEP page allocation failed, so guest
558 * is under severe memory pressure. Quickly
559 * decrease allocation rate.
560 */
561 b->rate_alloc = max(b->rate_alloc / 2,
562 VMW_BALLOON_RATE_ALLOC_MIN);
563 break;
564 }
565
566 /*
567 * NOSLEEP page allocation failed, so the guest is
568 * under memory pressure. Let us slow down page
569 * allocations for next few cycles so that the guest
570 * gets out of memory pressure. Also, if we already
571 * allocated b->rate_alloc pages, let's pause,
572 * otherwise switch to sleeping allocations.
573 */
574 b->slow_allocation_cycles = VMW_BALLOON_SLOW_CYCLES;
575
576 if (i >= b->rate_alloc)
577 break;
578
579 alloc_can_sleep = true;
580 /* Lower rate for sleeping allocations. */
581 rate = b->rate_alloc;
582 }
583
584 if (++allocations > VMW_BALLOON_YIELD_THRESHOLD) {
585 cond_resched();
586 allocations = 0;
587 }
588
589 if (i >= rate) {
590 /* We allocated enough pages, let's take a break. */
591 break;
592 }
593 }
594
595 /*
596 * We reached our goal without failures so try increasing
597 * allocation rate.
598 */
599 if (error == 0 && i >= b->rate_alloc) {
600 unsigned int mult = i / b->rate_alloc;
601
602 b->rate_alloc =
603 min(b->rate_alloc + mult * VMW_BALLOON_RATE_ALLOC_INC,
604 VMW_BALLOON_RATE_ALLOC_MAX);
605 }
606
607 vmballoon_release_refused_pages(b);
608}
609
610/*
611 * Decrease the size of the balloon allowing guest to use more memory.
612 */
613static void vmballoon_deflate(struct vmballoon *b)
614{
615 struct page *page, *next;
616 unsigned int i = 0;
617 unsigned int goal;
618 int error;
619
620 pr_debug("%s - size: %d, target %d\n", __func__, b->size, b->target);
621
622 /* limit deallocation rate */
623 goal = min(b->size - b->target, b->rate_free);
624
625 pr_debug("%s - goal: %d, rate: %d\n", __func__, goal, b->rate_free);
626
627 /* free pages to reach target */
628 list_for_each_entry_safe(page, next, &b->pages, lru) {
629 error = vmballoon_release_page(b, page);
630 if (error) {
631 /* quickly decrease rate in case of error */
632 b->rate_free = max(b->rate_free / 2,
633 VMW_BALLOON_RATE_FREE_MIN);
634 return;
635 }
636
637 if (++i >= goal)
638 break;
639 }
640
641 /* slowly increase rate if there were no errors */
642 b->rate_free = min(b->rate_free + VMW_BALLOON_RATE_FREE_INC,
643 VMW_BALLOON_RATE_FREE_MAX);
644}
645
646/*
647 * Balloon work function: reset protocol, if needed, get the new size and
648 * adjust balloon as needed. Repeat in 1 sec.
649 */
650static void vmballoon_work(struct work_struct *work)
651{
652 struct delayed_work *dwork = to_delayed_work(work);
653 struct vmballoon *b = container_of(dwork, struct vmballoon, dwork);
654 unsigned int target;
655
656 STATS_INC(b->stats.timer);
657
658 if (b->reset_required)
659 vmballoon_reset(b);
660
661 if (b->slow_allocation_cycles > 0)
662 b->slow_allocation_cycles--;
663
664 if (vmballoon_send_get_target(b, &target)) {
665 /* update target, adjust size */
666 b->target = target;
667
668 if (b->size < target)
669 vmballoon_inflate(b);
670 else if (b->size > target)
671 vmballoon_deflate(b);
672 }
673
674 queue_delayed_work(vmballoon_wq, dwork, round_jiffies_relative(HZ));
675}
676
677/*
678 * DEBUGFS Interface
679 */
680#ifdef CONFIG_DEBUG_FS
681
682static int vmballoon_debug_show(struct seq_file *f, void *offset)
683{
684 struct vmballoon *b = f->private;
685 struct vmballoon_stats *stats = &b->stats;
686
687 /* format size info */
688 seq_printf(f,
689 "target: %8d pages\n"
690 "current: %8d pages\n",
691 b->target, b->size);
692
693 /* format rate info */
694 seq_printf(f,
695 "rateNoSleepAlloc: %8d pages/sec\n"
696 "rateSleepAlloc: %8d pages/sec\n"
697 "rateFree: %8d pages/sec\n",
698 VMW_BALLOON_NOSLEEP_ALLOC_MAX,
699 b->rate_alloc, b->rate_free);
700
701 seq_printf(f,
702 "\n"
703 "timer: %8u\n"
704 "start: %8u (%4u failed)\n"
705 "guestType: %8u (%4u failed)\n"
706 "lock: %8u (%4u failed)\n"
707 "unlock: %8u (%4u failed)\n"
708 "target: %8u (%4u failed)\n"
709 "primNoSleepAlloc: %8u (%4u failed)\n"
710 "primCanSleepAlloc: %8u (%4u failed)\n"
711 "primFree: %8u\n"
712 "errAlloc: %8u\n"
713 "errFree: %8u\n",
714 stats->timer,
715 stats->start, stats->start_fail,
716 stats->guest_type, stats->guest_type_fail,
717 stats->lock, stats->lock_fail,
718 stats->unlock, stats->unlock_fail,
719 stats->target, stats->target_fail,
720 stats->alloc, stats->alloc_fail,
721 stats->sleep_alloc, stats->sleep_alloc_fail,
722 stats->free,
723 stats->refused_alloc, stats->refused_free);
724
725 return 0;
726}
727
728static int vmballoon_debug_open(struct inode *inode, struct file *file)
729{
730 return single_open(file, vmballoon_debug_show, inode->i_private);
731}
732
733static const struct file_operations vmballoon_debug_fops = {
734 .owner = THIS_MODULE,
735 .open = vmballoon_debug_open,
736 .read = seq_read,
737 .llseek = seq_lseek,
738 .release = single_release,
739};
740
741static int __init vmballoon_debugfs_init(struct vmballoon *b)
742{
743 int error;
744
745 b->dbg_entry = debugfs_create_file("vmmemctl", S_IRUGO, NULL, b,
746 &vmballoon_debug_fops);
747 if (IS_ERR(b->dbg_entry)) {
748 error = PTR_ERR(b->dbg_entry);
749 pr_err("failed to create debugfs entry, error: %d\n", error);
750 return error;
751 }
752
753 return 0;
754}
755
756static void __exit vmballoon_debugfs_exit(struct vmballoon *b)
757{
758 debugfs_remove(b->dbg_entry);
759}
760
761#else
762
763static inline int vmballoon_debugfs_init(struct vmballoon *b)
764{
765 return 0;
766}
767
768static inline void vmballoon_debugfs_exit(struct vmballoon *b)
769{
770}
771
772#endif /* CONFIG_DEBUG_FS */
773
774static int __init vmballoon_init(void)
775{
776 int error;
777
778 /*
779 * Check if we are running on VMware's hypervisor and bail out
780 * if we are not.
781 */
782 if (x86_hyper != &x86_hyper_vmware)
783 return -ENODEV;
784
785 vmballoon_wq = create_freezeable_workqueue("vmmemctl");
786 if (!vmballoon_wq) {
787 pr_err("failed to create workqueue\n");
788 return -ENOMEM;
789 }
790
791 INIT_LIST_HEAD(&balloon.pages);
792 INIT_LIST_HEAD(&balloon.refused_pages);
793
794 /* initialize rates */
795 balloon.rate_alloc = VMW_BALLOON_RATE_ALLOC_MAX;
796 balloon.rate_free = VMW_BALLOON_RATE_FREE_MAX;
797
798 INIT_DELAYED_WORK(&balloon.dwork, vmballoon_work);
799
800 /*
801 * Start balloon.
802 */
803 if (!vmballoon_send_start(&balloon)) {
804 pr_err("failed to send start command to the host\n");
805 error = -EIO;
806 goto fail;
807 }
808
809 if (!vmballoon_send_guest_id(&balloon)) {
810 pr_err("failed to send guest ID to the host\n");
811 error = -EIO;
812 goto fail;
813 }
814
815 error = vmballoon_debugfs_init(&balloon);
816 if (error)
817 goto fail;
818
819 queue_delayed_work(vmballoon_wq, &balloon.dwork, 0);
820
821 return 0;
822
823fail:
824 destroy_workqueue(vmballoon_wq);
825 return error;
826}
827module_init(vmballoon_init);
828
829static void __exit vmballoon_exit(void)
830{
831 cancel_delayed_work_sync(&balloon.dwork);
832 destroy_workqueue(vmballoon_wq);
833
834 vmballoon_debugfs_exit(&balloon);
835
836 /*
837 * Deallocate all reserved memory, and reset connection with monitor.
838 * Reset connection before deallocating memory to avoid potential for
839 * additional spurious resets from guest touching deallocated pages.
840 */
841 vmballoon_send_start(&balloon);
842 vmballoon_pop(&balloon);
843}
844module_exit(vmballoon_exit);