]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/misc/vmw_balloon.c
VMware balloon: partially inline vmballoon_reserve_page.
[mirror_ubuntu-artful-kernel.git] / drivers / misc / vmw_balloon.c
CommitLineData
453dc659
DT
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 *
73b35d07
DT
20 * Maintained by: Xavier Deguillard <xdeguillard@vmware.com>
21 * Philip Moltmann <moltmann@vmware.com>
453dc659
DT
22 */
23
24/*
25 * This is VMware physical memory management driver for Linux. The driver
26 * acts like a "balloon" that can be inflated to reclaim physical pages by
27 * reserving them in the guest and invalidating them in the monitor,
28 * freeing up the underlying machine pages so they can be allocated to
29 * other guests. The balloon can also be deflated to allow the guest to
30 * use more physical memory. Higher level policies can control the sizes
31 * of balloons in VMs in order to manage physical memory resources.
32 */
33
34//#define DEBUG
35#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36
37#include <linux/types.h>
38#include <linux/kernel.h>
39#include <linux/mm.h>
40#include <linux/sched.h>
41#include <linux/module.h>
42#include <linux/workqueue.h>
43#include <linux/debugfs.h>
44#include <linux/seq_file.h>
a10a5698 45#include <asm/hypervisor.h>
453dc659
DT
46
47MODULE_AUTHOR("VMware, Inc.");
48MODULE_DESCRIPTION("VMware Memory Control (Balloon) Driver");
ef0f8f11 49MODULE_VERSION("1.2.2.0-k");
453dc659
DT
50MODULE_ALIAS("dmi:*:svnVMware*:*");
51MODULE_ALIAS("vmware_vmmemctl");
52MODULE_LICENSE("GPL");
53
54/*
55 * Various constants controlling rate of inflaint/deflating balloon,
56 * measured in pages.
57 */
58
59/*
60 * Rate of allocating memory when there is no memory pressure
61 * (driver performs non-sleeping allocations).
62 */
63#define VMW_BALLOON_NOSLEEP_ALLOC_MAX 16384U
64
65/*
66 * Rates of memory allocaton when guest experiences memory pressure
67 * (driver performs sleeping allocations).
68 */
69#define VMW_BALLOON_RATE_ALLOC_MIN 512U
70#define VMW_BALLOON_RATE_ALLOC_MAX 2048U
71#define VMW_BALLOON_RATE_ALLOC_INC 16U
72
73/*
74 * Rates for releasing pages while deflating balloon.
75 */
76#define VMW_BALLOON_RATE_FREE_MIN 512U
77#define VMW_BALLOON_RATE_FREE_MAX 16384U
78#define VMW_BALLOON_RATE_FREE_INC 16U
79
80/*
81 * When guest is under memory pressure, use a reduced page allocation
82 * rate for next several cycles.
83 */
84#define VMW_BALLOON_SLOW_CYCLES 4
85
86/*
87 * Use __GFP_HIGHMEM to allow pages from HIGHMEM zone. We don't
88 * allow wait (__GFP_WAIT) for NOSLEEP page allocations. Use
89 * __GFP_NOWARN, to suppress page allocation failure warnings.
90 */
91#define VMW_PAGE_ALLOC_NOSLEEP (__GFP_HIGHMEM|__GFP_NOWARN)
92
93/*
94 * Use GFP_HIGHUSER when executing in a separate kernel thread
95 * context and allocation can sleep. This is less stressful to
96 * the guest memory system, since it allows the thread to block
97 * while memory is reclaimed, and won't take pages from emergency
98 * low-memory pools.
99 */
100#define VMW_PAGE_ALLOC_CANSLEEP (GFP_HIGHUSER)
101
102/* Maximum number of page allocations without yielding processor */
103#define VMW_BALLOON_YIELD_THRESHOLD 1024
104
55adaa49
DT
105/* Maximum number of refused pages we accumulate during inflation cycle */
106#define VMW_BALLOON_MAX_REFUSED 16
453dc659
DT
107
108/*
109 * Hypervisor communication port definitions.
110 */
111#define VMW_BALLOON_HV_PORT 0x5670
112#define VMW_BALLOON_HV_MAGIC 0x456c6d6f
113#define VMW_BALLOON_PROTOCOL_VERSION 2
114#define VMW_BALLOON_GUEST_ID 1 /* Linux */
115
116#define VMW_BALLOON_CMD_START 0
117#define VMW_BALLOON_CMD_GET_TARGET 1
118#define VMW_BALLOON_CMD_LOCK 2
119#define VMW_BALLOON_CMD_UNLOCK 3
120#define VMW_BALLOON_CMD_GUEST_ID 4
121
122/* error codes */
123#define VMW_BALLOON_SUCCESS 0
124#define VMW_BALLOON_FAILURE -1
125#define VMW_BALLOON_ERROR_CMD_INVALID 1
126#define VMW_BALLOON_ERROR_PPN_INVALID 2
127#define VMW_BALLOON_ERROR_PPN_LOCKED 3
128#define VMW_BALLOON_ERROR_PPN_UNLOCKED 4
129#define VMW_BALLOON_ERROR_PPN_PINNED 5
130#define VMW_BALLOON_ERROR_PPN_NOTNEEDED 6
131#define VMW_BALLOON_ERROR_RESET 7
132#define VMW_BALLOON_ERROR_BUSY 8
133
134#define VMWARE_BALLOON_CMD(cmd, data, result) \
135({ \
136 unsigned long __stat, __dummy1, __dummy2; \
e83736c8 137 __asm__ __volatile__ ("inl %%dx" : \
453dc659
DT
138 "=a"(__stat), \
139 "=c"(__dummy1), \
140 "=d"(__dummy2), \
141 "=b"(result) : \
142 "0"(VMW_BALLOON_HV_MAGIC), \
143 "1"(VMW_BALLOON_CMD_##cmd), \
144 "2"(VMW_BALLOON_HV_PORT), \
145 "3"(data) : \
146 "memory"); \
147 result &= -1UL; \
148 __stat & -1UL; \
149})
150
151#ifdef CONFIG_DEBUG_FS
152struct vmballoon_stats {
153 unsigned int timer;
154
2ca02df6 155 /* allocation statistics */
453dc659
DT
156 unsigned int alloc;
157 unsigned int alloc_fail;
158 unsigned int sleep_alloc;
159 unsigned int sleep_alloc_fail;
160 unsigned int refused_alloc;
161 unsigned int refused_free;
162 unsigned int free;
163
164 /* monitor operations */
165 unsigned int lock;
166 unsigned int lock_fail;
167 unsigned int unlock;
168 unsigned int unlock_fail;
169 unsigned int target;
170 unsigned int target_fail;
171 unsigned int start;
172 unsigned int start_fail;
173 unsigned int guest_type;
174 unsigned int guest_type_fail;
175};
176
177#define STATS_INC(stat) (stat)++
178#else
179#define STATS_INC(stat)
180#endif
181
182struct vmballoon {
183
184 /* list of reserved physical pages */
185 struct list_head pages;
186
187 /* transient list of non-balloonable pages */
188 struct list_head refused_pages;
55adaa49 189 unsigned int n_refused_pages;
453dc659
DT
190
191 /* balloon size in pages */
192 unsigned int size;
193 unsigned int target;
194
195 /* reset flag */
196 bool reset_required;
197
198 /* adjustment rates (pages per second) */
199 unsigned int rate_alloc;
200 unsigned int rate_free;
201
202 /* slowdown page allocations for next few cycles */
203 unsigned int slow_allocation_cycles;
204
205#ifdef CONFIG_DEBUG_FS
206 /* statistics */
207 struct vmballoon_stats stats;
208
209 /* debugfs file exporting statistics */
210 struct dentry *dbg_entry;
211#endif
212
213 struct sysinfo sysinfo;
214
215 struct delayed_work dwork;
216};
217
218static struct vmballoon balloon;
453dc659
DT
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 */
3e5ba466 318static int vmballoon_send_lock_page(struct vmballoon *b, unsigned long pfn,
d27a0c06 319 unsigned int *hv_status)
453dc659
DT
320{
321 unsigned long status, dummy;
322 u32 pfn32;
323
324 pfn32 = (u32)pfn;
325 if (pfn32 != pfn)
3e5ba466 326 return -1;
453dc659
DT
327
328 STATS_INC(b->stats.lock);
329
d27a0c06 330 *hv_status = status = VMWARE_BALLOON_CMD(LOCK, pfn, dummy);
453dc659 331 if (vmballoon_check_status(b, status))
3e5ba466 332 return 0;
453dc659
DT
333
334 pr_debug("%s - ppn %lx, hv returns %ld\n", __func__, pfn, status);
335 STATS_INC(b->stats.lock_fail);
3e5ba466 336 return 1;
453dc659
DT
337}
338
339/*
340 * Notify the host that guest intends to release given page back into
341 * the pool of available (to the guest) pages.
342 */
343static bool vmballoon_send_unlock_page(struct vmballoon *b, unsigned long pfn)
344{
345 unsigned long status, dummy;
346 u32 pfn32;
347
348 pfn32 = (u32)pfn;
349 if (pfn32 != pfn)
350 return false;
351
352 STATS_INC(b->stats.unlock);
353
354 status = VMWARE_BALLOON_CMD(UNLOCK, pfn, dummy);
355 if (vmballoon_check_status(b, status))
356 return true;
357
358 pr_debug("%s - ppn %lx, hv returns %ld\n", __func__, pfn, status);
359 STATS_INC(b->stats.unlock_fail);
360 return false;
361}
362
363/*
364 * Quickly release all pages allocated for the balloon. This function is
365 * called when host decides to "reset" balloon for one reason or another.
366 * Unlike normal "deflate" we do not (shall not) notify host of the pages
367 * being released.
368 */
369static void vmballoon_pop(struct vmballoon *b)
370{
371 struct page *page, *next;
372 unsigned int count = 0;
373
374 list_for_each_entry_safe(page, next, &b->pages, lru) {
375 list_del(&page->lru);
376 __free_page(page);
377 STATS_INC(b->stats.free);
378 b->size--;
379
380 if (++count >= b->rate_free) {
381 count = 0;
382 cond_resched();
383 }
384 }
385}
386
387/*
388 * Perform standard reset sequence by popping the balloon (in case it
389 * is not empty) and then restarting protocol. This operation normally
390 * happens when host responds with VMW_BALLOON_ERROR_RESET to a command.
391 */
392static void vmballoon_reset(struct vmballoon *b)
393{
394 /* free all pages, skipping monitor unlock */
395 vmballoon_pop(b);
396
397 if (vmballoon_send_start(b)) {
398 b->reset_required = false;
399 if (!vmballoon_send_guest_id(b))
400 pr_err("failed to send guest ID to the host\n");
401 }
402}
403
404/*
ef0f8f11
XD
405 * Notify the host of a ballooned page. If host rejects the page put it on the
406 * refuse list, those refused page are then released at the end of the
407 * inflation cycle.
453dc659 408 */
ef0f8f11 409static int vmballoon_lock_page(struct vmballoon *b, struct page *page)
453dc659 410{
ef0f8f11 411 int locked, hv_status;
453dc659 412
ef0f8f11
XD
413 locked = vmballoon_send_lock_page(b, page_to_pfn(page), &hv_status);
414 if (locked > 0) {
415 STATS_INC(b->stats.refused_alloc);
453dc659 416
ef0f8f11
XD
417 if (hv_status == VMW_BALLOON_ERROR_RESET ||
418 hv_status == VMW_BALLOON_ERROR_PPN_NOTNEEDED) {
419 __free_page(page);
420 return -EIO;
421 }
453dc659 422
ef0f8f11
XD
423 /*
424 * Place page on the list of non-balloonable pages
425 * and retry allocation, unless we already accumulated
426 * too many of them, in which case take a breather.
427 */
428 if (b->n_refused_pages < VMW_BALLOON_MAX_REFUSED) {
429 b->n_refused_pages++;
453dc659 430 list_add(&page->lru, &b->refused_pages);
ef0f8f11
XD
431 } else {
432 __free_page(page);
453dc659 433 }
ef0f8f11
XD
434 return -EIO;
435 }
453dc659
DT
436
437 /* track allocated page */
438 list_add(&page->lru, &b->pages);
439
440 /* update balloon size */
441 b->size++;
442
443 return 0;
444}
445
446/*
447 * Release the page allocated for the balloon. Note that we first notify
448 * the host so it can make sure the page will be available for the guest
449 * to use, if needed.
450 */
451static int vmballoon_release_page(struct vmballoon *b, struct page *page)
452{
453 if (!vmballoon_send_unlock_page(b, page_to_pfn(page)))
454 return -EIO;
455
456 list_del(&page->lru);
457
458 /* deallocate page */
459 __free_page(page);
460 STATS_INC(b->stats.free);
461
462 /* update balloon size */
463 b->size--;
464
465 return 0;
466}
467
468/*
469 * Release pages that were allocated while attempting to inflate the
470 * balloon but were refused by the host for one reason or another.
471 */
472static void vmballoon_release_refused_pages(struct vmballoon *b)
473{
474 struct page *page, *next;
475
476 list_for_each_entry_safe(page, next, &b->refused_pages, lru) {
477 list_del(&page->lru);
478 __free_page(page);
479 STATS_INC(b->stats.refused_free);
480 }
55adaa49
DT
481
482 b->n_refused_pages = 0;
453dc659
DT
483}
484
485/*
486 * Inflate the balloon towards its target size. Note that we try to limit
487 * the rate of allocation to make sure we are not choking the rest of the
488 * system.
489 */
490static void vmballoon_inflate(struct vmballoon *b)
491{
492 unsigned int goal;
493 unsigned int rate;
494 unsigned int i;
495 unsigned int allocations = 0;
496 int error = 0;
ef0f8f11 497 gfp_t flags = VMW_PAGE_ALLOC_NOSLEEP;
453dc659
DT
498
499 pr_debug("%s - size: %d, target %d\n", __func__, b->size, b->target);
500
501 /*
502 * First try NOSLEEP page allocations to inflate balloon.
503 *
504 * If we do not throttle nosleep allocations, we can drain all
505 * free pages in the guest quickly (if the balloon target is high).
506 * As a side-effect, draining free pages helps to inform (force)
507 * the guest to start swapping if balloon target is not met yet,
508 * which is a desired behavior. However, balloon driver can consume
509 * all available CPU cycles if too many pages are allocated in a
510 * second. Therefore, we throttle nosleep allocations even when
511 * the guest is not under memory pressure. OTOH, if we have already
512 * predicted that the guest is under memory pressure, then we
513 * slowdown page allocations considerably.
514 */
515
516 goal = b->target - b->size;
517 /*
518 * Start with no sleep allocation rate which may be higher
519 * than sleeping allocation rate.
520 */
521 rate = b->slow_allocation_cycles ?
522 b->rate_alloc : VMW_BALLOON_NOSLEEP_ALLOC_MAX;
523
524 pr_debug("%s - goal: %d, no-sleep rate: %d, sleep rate: %d\n",
525 __func__, goal, rate, b->rate_alloc);
526
527 for (i = 0; i < goal; i++) {
ef0f8f11 528 struct page *page;
453dc659 529
ef0f8f11
XD
530 if (flags == VMW_PAGE_ALLOC_NOSLEEP)
531 STATS_INC(b->stats.alloc);
532 else
533 STATS_INC(b->stats.sleep_alloc);
453dc659 534
ef0f8f11
XD
535 page = alloc_page(flags);
536 if (!page) {
537 if (flags == VMW_PAGE_ALLOC_CANSLEEP) {
453dc659
DT
538 /*
539 * CANSLEEP page allocation failed, so guest
540 * is under severe memory pressure. Quickly
541 * decrease allocation rate.
542 */
543 b->rate_alloc = max(b->rate_alloc / 2,
544 VMW_BALLOON_RATE_ALLOC_MIN);
ef0f8f11 545 STATS_INC(b->stats.sleep_alloc_fail);
453dc659
DT
546 break;
547 }
ef0f8f11 548 STATS_INC(b->stats.alloc_fail);
453dc659
DT
549
550 /*
551 * NOSLEEP page allocation failed, so the guest is
552 * under memory pressure. Let us slow down page
553 * allocations for next few cycles so that the guest
554 * gets out of memory pressure. Also, if we already
555 * allocated b->rate_alloc pages, let's pause,
556 * otherwise switch to sleeping allocations.
557 */
558 b->slow_allocation_cycles = VMW_BALLOON_SLOW_CYCLES;
559
560 if (i >= b->rate_alloc)
561 break;
562
ef0f8f11 563 flags = VMW_PAGE_ALLOC_CANSLEEP;
453dc659
DT
564 /* Lower rate for sleeping allocations. */
565 rate = b->rate_alloc;
ef0f8f11 566 continue;
453dc659
DT
567 }
568
ef0f8f11
XD
569 error = vmballoon_lock_page(b, page);
570 if (error)
571 break;
572
453dc659
DT
573 if (++allocations > VMW_BALLOON_YIELD_THRESHOLD) {
574 cond_resched();
575 allocations = 0;
576 }
577
578 if (i >= rate) {
579 /* We allocated enough pages, let's take a break. */
580 break;
581 }
582 }
583
584 /*
585 * We reached our goal without failures so try increasing
586 * allocation rate.
587 */
588 if (error == 0 && i >= b->rate_alloc) {
589 unsigned int mult = i / b->rate_alloc;
590
591 b->rate_alloc =
592 min(b->rate_alloc + mult * VMW_BALLOON_RATE_ALLOC_INC,
593 VMW_BALLOON_RATE_ALLOC_MAX);
594 }
595
596 vmballoon_release_refused_pages(b);
597}
598
599/*
600 * Decrease the size of the balloon allowing guest to use more memory.
601 */
602static void vmballoon_deflate(struct vmballoon *b)
603{
604 struct page *page, *next;
605 unsigned int i = 0;
606 unsigned int goal;
607 int error;
608
609 pr_debug("%s - size: %d, target %d\n", __func__, b->size, b->target);
610
611 /* limit deallocation rate */
612 goal = min(b->size - b->target, b->rate_free);
613
614 pr_debug("%s - goal: %d, rate: %d\n", __func__, goal, b->rate_free);
615
616 /* free pages to reach target */
617 list_for_each_entry_safe(page, next, &b->pages, lru) {
618 error = vmballoon_release_page(b, page);
619 if (error) {
620 /* quickly decrease rate in case of error */
621 b->rate_free = max(b->rate_free / 2,
622 VMW_BALLOON_RATE_FREE_MIN);
623 return;
624 }
625
626 if (++i >= goal)
627 break;
628 }
629
630 /* slowly increase rate if there were no errors */
631 b->rate_free = min(b->rate_free + VMW_BALLOON_RATE_FREE_INC,
632 VMW_BALLOON_RATE_FREE_MAX);
633}
634
635/*
636 * Balloon work function: reset protocol, if needed, get the new size and
637 * adjust balloon as needed. Repeat in 1 sec.
638 */
639static void vmballoon_work(struct work_struct *work)
640{
641 struct delayed_work *dwork = to_delayed_work(work);
642 struct vmballoon *b = container_of(dwork, struct vmballoon, dwork);
643 unsigned int target;
644
645 STATS_INC(b->stats.timer);
646
647 if (b->reset_required)
648 vmballoon_reset(b);
649
650 if (b->slow_allocation_cycles > 0)
651 b->slow_allocation_cycles--;
652
653 if (vmballoon_send_get_target(b, &target)) {
654 /* update target, adjust size */
655 b->target = target;
656
657 if (b->size < target)
658 vmballoon_inflate(b);
659 else if (b->size > target)
660 vmballoon_deflate(b);
661 }
662
beda94da
DT
663 /*
664 * We are using a freezable workqueue so that balloon operations are
665 * stopped while the system transitions to/from sleep/hibernation.
666 */
667 queue_delayed_work(system_freezable_wq,
668 dwork, round_jiffies_relative(HZ));
453dc659
DT
669}
670
671/*
672 * DEBUGFS Interface
673 */
674#ifdef CONFIG_DEBUG_FS
675
676static int vmballoon_debug_show(struct seq_file *f, void *offset)
677{
678 struct vmballoon *b = f->private;
679 struct vmballoon_stats *stats = &b->stats;
680
681 /* format size info */
682 seq_printf(f,
683 "target: %8d pages\n"
684 "current: %8d pages\n",
685 b->target, b->size);
686
687 /* format rate info */
688 seq_printf(f,
689 "rateNoSleepAlloc: %8d pages/sec\n"
690 "rateSleepAlloc: %8d pages/sec\n"
691 "rateFree: %8d pages/sec\n",
692 VMW_BALLOON_NOSLEEP_ALLOC_MAX,
693 b->rate_alloc, b->rate_free);
694
695 seq_printf(f,
696 "\n"
697 "timer: %8u\n"
698 "start: %8u (%4u failed)\n"
699 "guestType: %8u (%4u failed)\n"
700 "lock: %8u (%4u failed)\n"
701 "unlock: %8u (%4u failed)\n"
702 "target: %8u (%4u failed)\n"
703 "primNoSleepAlloc: %8u (%4u failed)\n"
704 "primCanSleepAlloc: %8u (%4u failed)\n"
705 "primFree: %8u\n"
706 "errAlloc: %8u\n"
707 "errFree: %8u\n",
708 stats->timer,
709 stats->start, stats->start_fail,
710 stats->guest_type, stats->guest_type_fail,
711 stats->lock, stats->lock_fail,
712 stats->unlock, stats->unlock_fail,
713 stats->target, stats->target_fail,
714 stats->alloc, stats->alloc_fail,
715 stats->sleep_alloc, stats->sleep_alloc_fail,
716 stats->free,
717 stats->refused_alloc, stats->refused_free);
718
719 return 0;
720}
721
722static int vmballoon_debug_open(struct inode *inode, struct file *file)
723{
724 return single_open(file, vmballoon_debug_show, inode->i_private);
725}
726
727static const struct file_operations vmballoon_debug_fops = {
728 .owner = THIS_MODULE,
729 .open = vmballoon_debug_open,
730 .read = seq_read,
731 .llseek = seq_lseek,
732 .release = single_release,
733};
734
735static int __init vmballoon_debugfs_init(struct vmballoon *b)
736{
737 int error;
738
739 b->dbg_entry = debugfs_create_file("vmmemctl", S_IRUGO, NULL, b,
740 &vmballoon_debug_fops);
741 if (IS_ERR(b->dbg_entry)) {
742 error = PTR_ERR(b->dbg_entry);
743 pr_err("failed to create debugfs entry, error: %d\n", error);
744 return error;
745 }
746
747 return 0;
748}
749
750static void __exit vmballoon_debugfs_exit(struct vmballoon *b)
751{
752 debugfs_remove(b->dbg_entry);
753}
754
755#else
756
757static inline int vmballoon_debugfs_init(struct vmballoon *b)
758{
759 return 0;
760}
761
762static inline void vmballoon_debugfs_exit(struct vmballoon *b)
763{
764}
765
766#endif /* CONFIG_DEBUG_FS */
767
768static int __init vmballoon_init(void)
769{
770 int error;
771
772 /*
773 * Check if we are running on VMware's hypervisor and bail out
774 * if we are not.
775 */
a10a5698 776 if (x86_hyper != &x86_hyper_vmware)
453dc659
DT
777 return -ENODEV;
778
453dc659
DT
779 INIT_LIST_HEAD(&balloon.pages);
780 INIT_LIST_HEAD(&balloon.refused_pages);
781
782 /* initialize rates */
783 balloon.rate_alloc = VMW_BALLOON_RATE_ALLOC_MAX;
784 balloon.rate_free = VMW_BALLOON_RATE_FREE_MAX;
785
786 INIT_DELAYED_WORK(&balloon.dwork, vmballoon_work);
787
788 /*
789 * Start balloon.
790 */
791 if (!vmballoon_send_start(&balloon)) {
792 pr_err("failed to send start command to the host\n");
beda94da 793 return -EIO;
453dc659
DT
794 }
795
796 if (!vmballoon_send_guest_id(&balloon)) {
797 pr_err("failed to send guest ID to the host\n");
beda94da 798 return -EIO;
453dc659
DT
799 }
800
801 error = vmballoon_debugfs_init(&balloon);
802 if (error)
beda94da 803 return error;
453dc659 804
beda94da 805 queue_delayed_work(system_freezable_wq, &balloon.dwork, 0);
453dc659
DT
806
807 return 0;
453dc659
DT
808}
809module_init(vmballoon_init);
810
811static void __exit vmballoon_exit(void)
812{
813 cancel_delayed_work_sync(&balloon.dwork);
453dc659
DT
814
815 vmballoon_debugfs_exit(&balloon);
816
817 /*
818 * Deallocate all reserved memory, and reset connection with monitor.
819 * Reset connection before deallocating memory to avoid potential for
820 * additional spurious resets from guest touching deallocated pages.
821 */
822 vmballoon_send_start(&balloon);
823 vmballoon_pop(&balloon);
824}
825module_exit(vmballoon_exit);