2 * VMware Balloon driver.
4 * Copyright (C) 2000-2010, VMware, Inc. All Rights Reserved.
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.
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
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.
20 * Maintained by: Dmitry Torokhov <dtor@vmware.com>
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.
34 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36 #include <linux/types.h>
37 #include <linux/kernel.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/vmware.h>
46 MODULE_AUTHOR("VMware, Inc.");
47 MODULE_DESCRIPTION("VMware Memory Control (Balloon) Driver");
48 MODULE_VERSION("1.2.1.0-K");
49 MODULE_ALIAS("dmi:*:svnVMware*:*");
50 MODULE_ALIAS("vmware_vmmemctl");
51 MODULE_LICENSE("GPL");
54 * Various constants controlling rate of inflaint/deflating balloon,
59 * Rate of allocating memory when there is no memory pressure
60 * (driver performs non-sleeping allocations).
62 #define VMW_BALLOON_NOSLEEP_ALLOC_MAX 16384U
65 * Rates of memory allocaton when guest experiences memory pressure
66 * (driver performs sleeping allocations).
68 #define VMW_BALLOON_RATE_ALLOC_MIN 512U
69 #define VMW_BALLOON_RATE_ALLOC_MAX 2048U
70 #define VMW_BALLOON_RATE_ALLOC_INC 16U
73 * Rates for releasing pages while deflating balloon.
75 #define VMW_BALLOON_RATE_FREE_MIN 512U
76 #define VMW_BALLOON_RATE_FREE_MAX 16384U
77 #define VMW_BALLOON_RATE_FREE_INC 16U
80 * When guest is under memory pressure, use a reduced page allocation
81 * rate for next several cycles.
83 #define VMW_BALLOON_SLOW_CYCLES 4
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.
90 #define VMW_PAGE_ALLOC_NOSLEEP (__GFP_HIGHMEM|__GFP_NOWARN)
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
99 #define VMW_PAGE_ALLOC_CANSLEEP (GFP_HIGHUSER)
101 /* Maximum number of page allocations without yielding processor */
102 #define VMW_BALLOON_YIELD_THRESHOLD 1024
106 * Hypervisor communication port definitions.
108 #define VMW_BALLOON_HV_PORT 0x5670
109 #define VMW_BALLOON_HV_MAGIC 0x456c6d6f
110 #define VMW_BALLOON_PROTOCOL_VERSION 2
111 #define VMW_BALLOON_GUEST_ID 1 /* Linux */
113 #define VMW_BALLOON_CMD_START 0
114 #define VMW_BALLOON_CMD_GET_TARGET 1
115 #define VMW_BALLOON_CMD_LOCK 2
116 #define VMW_BALLOON_CMD_UNLOCK 3
117 #define VMW_BALLOON_CMD_GUEST_ID 4
120 #define VMW_BALLOON_SUCCESS 0
121 #define VMW_BALLOON_FAILURE -1
122 #define VMW_BALLOON_ERROR_CMD_INVALID 1
123 #define VMW_BALLOON_ERROR_PPN_INVALID 2
124 #define VMW_BALLOON_ERROR_PPN_LOCKED 3
125 #define VMW_BALLOON_ERROR_PPN_UNLOCKED 4
126 #define VMW_BALLOON_ERROR_PPN_PINNED 5
127 #define VMW_BALLOON_ERROR_PPN_NOTNEEDED 6
128 #define VMW_BALLOON_ERROR_RESET 7
129 #define VMW_BALLOON_ERROR_BUSY 8
131 #define VMWARE_BALLOON_CMD(cmd, data, result) \
133 unsigned long __stat, __dummy1, __dummy2; \
134 __asm__ __volatile__ ("inl (%%dx)" : \
139 "0"(VMW_BALLOON_HV_MAGIC), \
140 "1"(VMW_BALLOON_CMD_##cmd), \
141 "2"(VMW_BALLOON_HV_PORT), \
148 #ifdef CONFIG_DEBUG_FS
149 struct vmballoon_stats
{
152 /* allocation statustics */
154 unsigned int alloc_fail
;
155 unsigned int sleep_alloc
;
156 unsigned int sleep_alloc_fail
;
157 unsigned int refused_alloc
;
158 unsigned int refused_free
;
161 /* monitor operations */
163 unsigned int lock_fail
;
165 unsigned int unlock_fail
;
167 unsigned int target_fail
;
169 unsigned int start_fail
;
170 unsigned int guest_type
;
171 unsigned int guest_type_fail
;
174 #define STATS_INC(stat) (stat)++
176 #define STATS_INC(stat)
181 /* list of reserved physical pages */
182 struct list_head pages
;
184 /* transient list of non-balloonable pages */
185 struct list_head refused_pages
;
187 /* balloon size in pages */
194 /* adjustment rates (pages per second) */
195 unsigned int rate_alloc
;
196 unsigned int rate_free
;
198 /* slowdown page allocations for next few cycles */
199 unsigned int slow_allocation_cycles
;
201 #ifdef CONFIG_DEBUG_FS
203 struct vmballoon_stats stats
;
205 /* debugfs file exporting statistics */
206 struct dentry
*dbg_entry
;
209 struct sysinfo sysinfo
;
211 struct delayed_work dwork
;
214 static struct vmballoon balloon
;
215 static struct workqueue_struct
*vmballoon_wq
;
218 * Send "start" command to the host, communicating supported version
221 static bool vmballoon_send_start(struct vmballoon
*b
)
223 unsigned long status
, dummy
;
225 STATS_INC(b
->stats
.start
);
227 status
= VMWARE_BALLOON_CMD(START
, VMW_BALLOON_PROTOCOL_VERSION
, dummy
);
228 if (status
== VMW_BALLOON_SUCCESS
)
231 pr_debug("%s - failed, hv returns %ld\n", __func__
, status
);
232 STATS_INC(b
->stats
.start_fail
);
236 static bool vmballoon_check_status(struct vmballoon
*b
, unsigned long status
)
239 case VMW_BALLOON_SUCCESS
:
242 case VMW_BALLOON_ERROR_RESET
:
243 b
->reset_required
= true;
252 * Communicate guest type to the host so that it can adjust ballooning
253 * algorithm to the one most appropriate for the guest. This command
254 * is normally issued after sending "start" command and is part of
255 * standard reset sequence.
257 static bool vmballoon_send_guest_id(struct vmballoon
*b
)
259 unsigned long status
, dummy
;
261 status
= VMWARE_BALLOON_CMD(GUEST_ID
, VMW_BALLOON_GUEST_ID
, dummy
);
263 STATS_INC(b
->stats
.guest_type
);
265 if (vmballoon_check_status(b
, status
))
268 pr_debug("%s - failed, hv returns %ld\n", __func__
, status
);
269 STATS_INC(b
->stats
.guest_type_fail
);
274 * Retrieve desired balloon size from the host.
276 static bool vmballoon_send_get_target(struct vmballoon
*b
, u32
*new_target
)
278 unsigned long status
;
279 unsigned long target
;
284 * si_meminfo() is cheap. Moreover, we want to provide dynamic
285 * max balloon size later. So let us call si_meminfo() every
288 si_meminfo(&b
->sysinfo
);
289 limit
= b
->sysinfo
.totalram
;
291 /* Ensure limit fits in 32-bits */
292 limit32
= (u32
)limit
;
293 if (limit
!= limit32
)
297 STATS_INC(b
->stats
.target
);
299 status
= VMWARE_BALLOON_CMD(GET_TARGET
, limit
, target
);
300 if (vmballoon_check_status(b
, status
)) {
301 *new_target
= target
;
305 pr_debug("%s - failed, hv returns %ld\n", __func__
, status
);
306 STATS_INC(b
->stats
.target_fail
);
311 * Notify the host about allocated page so that host can use it without
312 * fear that guest will need it. Host may reject some pages, we need to
313 * check the return value and maybe submit a different page.
315 static bool vmballoon_send_lock_page(struct vmballoon
*b
, unsigned long pfn
)
317 unsigned long status
, dummy
;
324 STATS_INC(b
->stats
.lock
);
326 status
= VMWARE_BALLOON_CMD(LOCK
, pfn
, dummy
);
327 if (vmballoon_check_status(b
, status
))
330 pr_debug("%s - ppn %lx, hv returns %ld\n", __func__
, pfn
, status
);
331 STATS_INC(b
->stats
.lock_fail
);
336 * Notify the host that guest intends to release given page back into
337 * the pool of available (to the guest) pages.
339 static bool vmballoon_send_unlock_page(struct vmballoon
*b
, unsigned long pfn
)
341 unsigned long status
, dummy
;
348 STATS_INC(b
->stats
.unlock
);
350 status
= VMWARE_BALLOON_CMD(UNLOCK
, pfn
, dummy
);
351 if (vmballoon_check_status(b
, status
))
354 pr_debug("%s - ppn %lx, hv returns %ld\n", __func__
, pfn
, status
);
355 STATS_INC(b
->stats
.unlock_fail
);
360 * Quickly release all pages allocated for the balloon. This function is
361 * called when host decides to "reset" balloon for one reason or another.
362 * Unlike normal "deflate" we do not (shall not) notify host of the pages
365 static void vmballoon_pop(struct vmballoon
*b
)
367 struct page
*page
, *next
;
368 unsigned int count
= 0;
370 list_for_each_entry_safe(page
, next
, &b
->pages
, lru
) {
371 list_del(&page
->lru
);
373 STATS_INC(b
->stats
.free
);
376 if (++count
>= b
->rate_free
) {
384 * Perform standard reset sequence by popping the balloon (in case it
385 * is not empty) and then restarting protocol. This operation normally
386 * happens when host responds with VMW_BALLOON_ERROR_RESET to a command.
388 static void vmballoon_reset(struct vmballoon
*b
)
390 /* free all pages, skipping monitor unlock */
393 if (vmballoon_send_start(b
)) {
394 b
->reset_required
= false;
395 if (!vmballoon_send_guest_id(b
))
396 pr_err("failed to send guest ID to the host\n");
401 * Allocate (or reserve) a page for the balloon and notify the host. If host
402 * refuses the page put it on "refuse" list and allocate another one until host
403 * is satisfied. "Refused" pages are released at the end of inflation cycle
404 * (when we allocate b->rate_alloc pages).
406 static int vmballoon_reserve_page(struct vmballoon
*b
, bool can_sleep
)
414 STATS_INC(b
->stats
.alloc
);
416 STATS_INC(b
->stats
.sleep_alloc
);
418 flags
= can_sleep
? VMW_PAGE_ALLOC_CANSLEEP
: VMW_PAGE_ALLOC_NOSLEEP
;
419 page
= alloc_page(flags
);
422 STATS_INC(b
->stats
.alloc_fail
);
424 STATS_INC(b
->stats
.sleep_alloc_fail
);
429 locked
= vmballoon_send_lock_page(b
, page_to_pfn(page
));
431 if (b
->reset_required
) {
436 /* place on list of non-balloonable pages, retry allocation */
437 list_add(&page
->lru
, &b
->refused_pages
);
438 STATS_INC(b
->stats
.refused_alloc
);
442 /* track allocated page */
443 list_add(&page
->lru
, &b
->pages
);
445 /* update balloon size */
452 * Release the page allocated for the balloon. Note that we first notify
453 * the host so it can make sure the page will be available for the guest
456 static int vmballoon_release_page(struct vmballoon
*b
, struct page
*page
)
458 if (!vmballoon_send_unlock_page(b
, page_to_pfn(page
)))
461 list_del(&page
->lru
);
463 /* deallocate page */
465 STATS_INC(b
->stats
.free
);
467 /* update balloon size */
474 * Release pages that were allocated while attempting to inflate the
475 * balloon but were refused by the host for one reason or another.
477 static void vmballoon_release_refused_pages(struct vmballoon
*b
)
479 struct page
*page
, *next
;
481 list_for_each_entry_safe(page
, next
, &b
->refused_pages
, lru
) {
482 list_del(&page
->lru
);
484 STATS_INC(b
->stats
.refused_free
);
489 * Inflate the balloon towards its target size. Note that we try to limit
490 * the rate of allocation to make sure we are not choking the rest of the
493 static void vmballoon_inflate(struct vmballoon
*b
)
498 unsigned int allocations
= 0;
500 bool alloc_can_sleep
= false;
502 pr_debug("%s - size: %d, target %d\n", __func__
, b
->size
, b
->target
);
505 * First try NOSLEEP page allocations to inflate balloon.
507 * If we do not throttle nosleep allocations, we can drain all
508 * free pages in the guest quickly (if the balloon target is high).
509 * As a side-effect, draining free pages helps to inform (force)
510 * the guest to start swapping if balloon target is not met yet,
511 * which is a desired behavior. However, balloon driver can consume
512 * all available CPU cycles if too many pages are allocated in a
513 * second. Therefore, we throttle nosleep allocations even when
514 * the guest is not under memory pressure. OTOH, if we have already
515 * predicted that the guest is under memory pressure, then we
516 * slowdown page allocations considerably.
519 goal
= b
->target
- b
->size
;
521 * Start with no sleep allocation rate which may be higher
522 * than sleeping allocation rate.
524 rate
= b
->slow_allocation_cycles
?
525 b
->rate_alloc
: VMW_BALLOON_NOSLEEP_ALLOC_MAX
;
527 pr_debug("%s - goal: %d, no-sleep rate: %d, sleep rate: %d\n",
528 __func__
, goal
, rate
, b
->rate_alloc
);
530 for (i
= 0; i
< goal
; i
++) {
532 error
= vmballoon_reserve_page(b
, alloc_can_sleep
);
534 if (error
!= -ENOMEM
) {
536 * Not a page allocation failure, stop this
537 * cycle. Maybe we'll get new target from
543 if (alloc_can_sleep
) {
545 * CANSLEEP page allocation failed, so guest
546 * is under severe memory pressure. Quickly
547 * decrease allocation rate.
549 b
->rate_alloc
= max(b
->rate_alloc
/ 2,
550 VMW_BALLOON_RATE_ALLOC_MIN
);
555 * NOSLEEP page allocation failed, so the guest is
556 * under memory pressure. Let us slow down page
557 * allocations for next few cycles so that the guest
558 * gets out of memory pressure. Also, if we already
559 * allocated b->rate_alloc pages, let's pause,
560 * otherwise switch to sleeping allocations.
562 b
->slow_allocation_cycles
= VMW_BALLOON_SLOW_CYCLES
;
564 if (i
>= b
->rate_alloc
)
567 alloc_can_sleep
= true;
568 /* Lower rate for sleeping allocations. */
569 rate
= b
->rate_alloc
;
572 if (++allocations
> VMW_BALLOON_YIELD_THRESHOLD
) {
578 /* We allocated enough pages, let's take a break. */
584 * We reached our goal without failures so try increasing
587 if (error
== 0 && i
>= b
->rate_alloc
) {
588 unsigned int mult
= i
/ b
->rate_alloc
;
591 min(b
->rate_alloc
+ mult
* VMW_BALLOON_RATE_ALLOC_INC
,
592 VMW_BALLOON_RATE_ALLOC_MAX
);
595 vmballoon_release_refused_pages(b
);
599 * Decrease the size of the balloon allowing guest to use more memory.
601 static void vmballoon_deflate(struct vmballoon
*b
)
603 struct page
*page
, *next
;
608 pr_debug("%s - size: %d, target %d\n", __func__
, b
->size
, b
->target
);
610 /* limit deallocation rate */
611 goal
= min(b
->size
- b
->target
, b
->rate_free
);
613 pr_debug("%s - goal: %d, rate: %d\n", __func__
, goal
, b
->rate_free
);
615 /* free pages to reach target */
616 list_for_each_entry_safe(page
, next
, &b
->pages
, lru
) {
617 error
= vmballoon_release_page(b
, page
);
619 /* quickly decrease rate in case of error */
620 b
->rate_free
= max(b
->rate_free
/ 2,
621 VMW_BALLOON_RATE_FREE_MIN
);
629 /* slowly increase rate if there were no errors */
630 b
->rate_free
= min(b
->rate_free
+ VMW_BALLOON_RATE_FREE_INC
,
631 VMW_BALLOON_RATE_FREE_MAX
);
635 * Balloon work function: reset protocol, if needed, get the new size and
636 * adjust balloon as needed. Repeat in 1 sec.
638 static void vmballoon_work(struct work_struct
*work
)
640 struct delayed_work
*dwork
= to_delayed_work(work
);
641 struct vmballoon
*b
= container_of(dwork
, struct vmballoon
, dwork
);
644 STATS_INC(b
->stats
.timer
);
646 if (b
->reset_required
)
649 if (b
->slow_allocation_cycles
> 0)
650 b
->slow_allocation_cycles
--;
652 if (vmballoon_send_get_target(b
, &target
)) {
653 /* update target, adjust size */
656 if (b
->size
< target
)
657 vmballoon_inflate(b
);
658 else if (b
->size
> target
)
659 vmballoon_deflate(b
);
662 queue_delayed_work(vmballoon_wq
, dwork
, round_jiffies_relative(HZ
));
668 #ifdef CONFIG_DEBUG_FS
670 static int vmballoon_debug_show(struct seq_file
*f
, void *offset
)
672 struct vmballoon
*b
= f
->private;
673 struct vmballoon_stats
*stats
= &b
->stats
;
675 /* format size info */
677 "target: %8d pages\n"
678 "current: %8d pages\n",
681 /* format rate info */
683 "rateNoSleepAlloc: %8d pages/sec\n"
684 "rateSleepAlloc: %8d pages/sec\n"
685 "rateFree: %8d pages/sec\n",
686 VMW_BALLOON_NOSLEEP_ALLOC_MAX
,
687 b
->rate_alloc
, b
->rate_free
);
692 "start: %8u (%4u failed)\n"
693 "guestType: %8u (%4u failed)\n"
694 "lock: %8u (%4u failed)\n"
695 "unlock: %8u (%4u failed)\n"
696 "target: %8u (%4u failed)\n"
697 "primNoSleepAlloc: %8u (%4u failed)\n"
698 "primCanSleepAlloc: %8u (%4u failed)\n"
703 stats
->start
, stats
->start_fail
,
704 stats
->guest_type
, stats
->guest_type_fail
,
705 stats
->lock
, stats
->lock_fail
,
706 stats
->unlock
, stats
->unlock_fail
,
707 stats
->target
, stats
->target_fail
,
708 stats
->alloc
, stats
->alloc_fail
,
709 stats
->sleep_alloc
, stats
->sleep_alloc_fail
,
711 stats
->refused_alloc
, stats
->refused_free
);
716 static int vmballoon_debug_open(struct inode
*inode
, struct file
*file
)
718 return single_open(file
, vmballoon_debug_show
, inode
->i_private
);
721 static const struct file_operations vmballoon_debug_fops
= {
722 .owner
= THIS_MODULE
,
723 .open
= vmballoon_debug_open
,
726 .release
= single_release
,
729 static int __init
vmballoon_debugfs_init(struct vmballoon
*b
)
733 b
->dbg_entry
= debugfs_create_file("vmmemctl", S_IRUGO
, NULL
, b
,
734 &vmballoon_debug_fops
);
735 if (IS_ERR(b
->dbg_entry
)) {
736 error
= PTR_ERR(b
->dbg_entry
);
737 pr_err("failed to create debugfs entry, error: %d\n", error
);
744 static void __exit
vmballoon_debugfs_exit(struct vmballoon
*b
)
746 debugfs_remove(b
->dbg_entry
);
751 static inline int vmballoon_debugfs_init(struct vmballoon
*b
)
756 static inline void vmballoon_debugfs_exit(struct vmballoon
*b
)
760 #endif /* CONFIG_DEBUG_FS */
762 static int __init
vmballoon_init(void)
767 * Check if we are running on VMware's hypervisor and bail out
770 if (!vmware_platform())
773 vmballoon_wq
= create_freezeable_workqueue("vmmemctl");
775 pr_err("failed to create workqueue\n");
779 INIT_LIST_HEAD(&balloon
.pages
);
780 INIT_LIST_HEAD(&balloon
.refused_pages
);
782 /* initialize rates */
783 balloon
.rate_alloc
= VMW_BALLOON_RATE_ALLOC_MAX
;
784 balloon
.rate_free
= VMW_BALLOON_RATE_FREE_MAX
;
786 INIT_DELAYED_WORK(&balloon
.dwork
, vmballoon_work
);
791 if (!vmballoon_send_start(&balloon
)) {
792 pr_err("failed to send start command to the host\n");
797 if (!vmballoon_send_guest_id(&balloon
)) {
798 pr_err("failed to send guest ID to the host\n");
803 error
= vmballoon_debugfs_init(&balloon
);
807 queue_delayed_work(vmballoon_wq
, &balloon
.dwork
, 0);
812 destroy_workqueue(vmballoon_wq
);
815 module_init(vmballoon_init
);
817 static void __exit
vmballoon_exit(void)
819 cancel_delayed_work_sync(&balloon
.dwork
);
820 destroy_workqueue(vmballoon_wq
);
822 vmballoon_debugfs_exit(&balloon
);
825 * Deallocate all reserved memory, and reset connection with monitor.
826 * Reset connection before deallocating memory to avoid potential for
827 * additional spurious resets from guest touching deallocated pages.
829 vmballoon_send_start(&balloon
);
830 vmballoon_pop(&balloon
);
832 module_exit(vmballoon_exit
);