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: Xavier Deguillard <xdeguillard@vmware.com>
21 * Philip Moltmann <moltmann@vmware.com>
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.
35 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37 #include <linux/types.h>
38 #include <linux/kernel.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>
45 #include <asm/hypervisor.h>
47 MODULE_AUTHOR("VMware, Inc.");
48 MODULE_DESCRIPTION("VMware Memory Control (Balloon) Driver");
49 MODULE_VERSION("1.2.1.3-k");
50 MODULE_ALIAS("dmi:*:svnVMware*:*");
51 MODULE_ALIAS("vmware_vmmemctl");
52 MODULE_LICENSE("GPL");
55 * Various constants controlling rate of inflaint/deflating balloon,
60 * Rate of allocating memory when there is no memory pressure
61 * (driver performs non-sleeping allocations).
63 #define VMW_BALLOON_NOSLEEP_ALLOC_MAX 16384U
66 * Rates of memory allocaton when guest experiences memory pressure
67 * (driver performs sleeping allocations).
69 #define VMW_BALLOON_RATE_ALLOC_MIN 512U
70 #define VMW_BALLOON_RATE_ALLOC_MAX 2048U
71 #define VMW_BALLOON_RATE_ALLOC_INC 16U
74 * Rates for releasing pages while deflating balloon.
76 #define VMW_BALLOON_RATE_FREE_MIN 512U
77 #define VMW_BALLOON_RATE_FREE_MAX 16384U
78 #define VMW_BALLOON_RATE_FREE_INC 16U
81 * When guest is under memory pressure, use a reduced page allocation
82 * rate for next several cycles.
84 #define VMW_BALLOON_SLOW_CYCLES 4
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.
91 #define VMW_PAGE_ALLOC_NOSLEEP (__GFP_HIGHMEM|__GFP_NOWARN)
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
100 #define VMW_PAGE_ALLOC_CANSLEEP (GFP_HIGHUSER)
102 /* Maximum number of page allocations without yielding processor */
103 #define VMW_BALLOON_YIELD_THRESHOLD 1024
105 /* Maximum number of refused pages we accumulate during inflation cycle */
106 #define VMW_BALLOON_MAX_REFUSED 16
109 * Hypervisor communication port definitions.
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 */
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
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
134 #define VMWARE_BALLOON_CMD(cmd, data, result) \
136 unsigned long __stat, __dummy1, __dummy2; \
137 __asm__ __volatile__ ("inl %%dx" : \
142 "0"(VMW_BALLOON_HV_MAGIC), \
143 "1"(VMW_BALLOON_CMD_##cmd), \
144 "2"(VMW_BALLOON_HV_PORT), \
151 #ifdef CONFIG_DEBUG_FS
152 struct vmballoon_stats
{
155 /* allocation statistics */
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
;
164 /* monitor operations */
166 unsigned int lock_fail
;
168 unsigned int unlock_fail
;
170 unsigned int target_fail
;
172 unsigned int start_fail
;
173 unsigned int guest_type
;
174 unsigned int guest_type_fail
;
177 #define STATS_INC(stat) (stat)++
179 #define STATS_INC(stat)
184 /* list of reserved physical pages */
185 struct list_head pages
;
187 /* transient list of non-balloonable pages */
188 struct list_head refused_pages
;
189 unsigned int n_refused_pages
;
191 /* balloon size in pages */
198 /* adjustment rates (pages per second) */
199 unsigned int rate_alloc
;
200 unsigned int rate_free
;
202 /* slowdown page allocations for next few cycles */
203 unsigned int slow_allocation_cycles
;
205 #ifdef CONFIG_DEBUG_FS
207 struct vmballoon_stats stats
;
209 /* debugfs file exporting statistics */
210 struct dentry
*dbg_entry
;
213 struct sysinfo sysinfo
;
215 struct delayed_work dwork
;
218 static struct vmballoon balloon
;
221 * Send "start" command to the host, communicating supported version
224 static bool vmballoon_send_start(struct vmballoon
*b
)
226 unsigned long status
, dummy
;
228 STATS_INC(b
->stats
.start
);
230 status
= VMWARE_BALLOON_CMD(START
, VMW_BALLOON_PROTOCOL_VERSION
, dummy
);
231 if (status
== VMW_BALLOON_SUCCESS
)
234 pr_debug("%s - failed, hv returns %ld\n", __func__
, status
);
235 STATS_INC(b
->stats
.start_fail
);
239 static bool vmballoon_check_status(struct vmballoon
*b
, unsigned long status
)
242 case VMW_BALLOON_SUCCESS
:
245 case VMW_BALLOON_ERROR_RESET
:
246 b
->reset_required
= true;
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.
260 static bool vmballoon_send_guest_id(struct vmballoon
*b
)
262 unsigned long status
, dummy
;
264 status
= VMWARE_BALLOON_CMD(GUEST_ID
, VMW_BALLOON_GUEST_ID
, dummy
);
266 STATS_INC(b
->stats
.guest_type
);
268 if (vmballoon_check_status(b
, status
))
271 pr_debug("%s - failed, hv returns %ld\n", __func__
, status
);
272 STATS_INC(b
->stats
.guest_type_fail
);
277 * Retrieve desired balloon size from the host.
279 static bool vmballoon_send_get_target(struct vmballoon
*b
, u32
*new_target
)
281 unsigned long status
;
282 unsigned long target
;
287 * si_meminfo() is cheap. Moreover, we want to provide dynamic
288 * max balloon size later. So let us call si_meminfo() every
291 si_meminfo(&b
->sysinfo
);
292 limit
= b
->sysinfo
.totalram
;
294 /* Ensure limit fits in 32-bits */
295 limit32
= (u32
)limit
;
296 if (limit
!= limit32
)
300 STATS_INC(b
->stats
.target
);
302 status
= VMWARE_BALLOON_CMD(GET_TARGET
, limit
, target
);
303 if (vmballoon_check_status(b
, status
)) {
304 *new_target
= target
;
308 pr_debug("%s - failed, hv returns %ld\n", __func__
, status
);
309 STATS_INC(b
->stats
.target_fail
);
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.
318 static int vmballoon_send_lock_page(struct vmballoon
*b
, unsigned long pfn
,
319 unsigned int *hv_status
)
321 unsigned long status
, dummy
;
328 STATS_INC(b
->stats
.lock
);
330 *hv_status
= status
= VMWARE_BALLOON_CMD(LOCK
, pfn
, dummy
);
331 if (vmballoon_check_status(b
, status
))
334 pr_debug("%s - ppn %lx, hv returns %ld\n", __func__
, pfn
, status
);
335 STATS_INC(b
->stats
.lock_fail
);
340 * Notify the host that guest intends to release given page back into
341 * the pool of available (to the guest) pages.
343 static bool vmballoon_send_unlock_page(struct vmballoon
*b
, unsigned long pfn
)
345 unsigned long status
, dummy
;
352 STATS_INC(b
->stats
.unlock
);
354 status
= VMWARE_BALLOON_CMD(UNLOCK
, pfn
, dummy
);
355 if (vmballoon_check_status(b
, status
))
358 pr_debug("%s - ppn %lx, hv returns %ld\n", __func__
, pfn
, status
);
359 STATS_INC(b
->stats
.unlock_fail
);
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
369 static void vmballoon_pop(struct vmballoon
*b
)
371 struct page
*page
, *next
;
372 unsigned int count
= 0;
374 list_for_each_entry_safe(page
, next
, &b
->pages
, lru
) {
375 list_del(&page
->lru
);
377 STATS_INC(b
->stats
.free
);
380 if (++count
>= b
->rate_free
) {
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.
392 static void vmballoon_reset(struct vmballoon
*b
)
394 /* free all pages, skipping monitor unlock */
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");
405 * Allocate (or reserve) a page for the balloon and notify the host. If host
406 * refuses the page put it on "refuse" list and allocate another one until host
407 * is satisfied. "Refused" pages are released at the end of inflation cycle
408 * (when we allocate b->rate_alloc pages).
410 static int vmballoon_reserve_page(struct vmballoon
*b
, bool can_sleep
)
414 unsigned int hv_status
;
416 flags
= can_sleep
? VMW_PAGE_ALLOC_CANSLEEP
: VMW_PAGE_ALLOC_NOSLEEP
;
420 STATS_INC(b
->stats
.alloc
);
422 STATS_INC(b
->stats
.sleep_alloc
);
424 page
= alloc_page(flags
);
427 STATS_INC(b
->stats
.alloc_fail
);
429 STATS_INC(b
->stats
.sleep_alloc_fail
);
434 locked
= vmballoon_send_lock_page(b
, page_to_pfn(page
), &hv_status
);
436 STATS_INC(b
->stats
.refused_alloc
);
438 if (hv_status
== VMW_BALLOON_ERROR_RESET
||
439 hv_status
== VMW_BALLOON_ERROR_PPN_NOTNEEDED
) {
445 * Place page on the list of non-balloonable pages
446 * and retry allocation, unless we already accumulated
447 * too many of them, in which case take a breather.
449 list_add(&page
->lru
, &b
->refused_pages
);
450 if (++b
->n_refused_pages
>= VMW_BALLOON_MAX_REFUSED
)
453 } while (locked
!= 0);
455 /* track allocated page */
456 list_add(&page
->lru
, &b
->pages
);
458 /* update balloon size */
465 * Release the page allocated for the balloon. Note that we first notify
466 * the host so it can make sure the page will be available for the guest
469 static int vmballoon_release_page(struct vmballoon
*b
, struct page
*page
)
471 if (!vmballoon_send_unlock_page(b
, page_to_pfn(page
)))
474 list_del(&page
->lru
);
476 /* deallocate page */
478 STATS_INC(b
->stats
.free
);
480 /* update balloon size */
487 * Release pages that were allocated while attempting to inflate the
488 * balloon but were refused by the host for one reason or another.
490 static void vmballoon_release_refused_pages(struct vmballoon
*b
)
492 struct page
*page
, *next
;
494 list_for_each_entry_safe(page
, next
, &b
->refused_pages
, lru
) {
495 list_del(&page
->lru
);
497 STATS_INC(b
->stats
.refused_free
);
500 b
->n_refused_pages
= 0;
504 * Inflate the balloon towards its target size. Note that we try to limit
505 * the rate of allocation to make sure we are not choking the rest of the
508 static void vmballoon_inflate(struct vmballoon
*b
)
513 unsigned int allocations
= 0;
515 bool alloc_can_sleep
= false;
517 pr_debug("%s - size: %d, target %d\n", __func__
, b
->size
, b
->target
);
520 * First try NOSLEEP page allocations to inflate balloon.
522 * If we do not throttle nosleep allocations, we can drain all
523 * free pages in the guest quickly (if the balloon target is high).
524 * As a side-effect, draining free pages helps to inform (force)
525 * the guest to start swapping if balloon target is not met yet,
526 * which is a desired behavior. However, balloon driver can consume
527 * all available CPU cycles if too many pages are allocated in a
528 * second. Therefore, we throttle nosleep allocations even when
529 * the guest is not under memory pressure. OTOH, if we have already
530 * predicted that the guest is under memory pressure, then we
531 * slowdown page allocations considerably.
534 goal
= b
->target
- b
->size
;
536 * Start with no sleep allocation rate which may be higher
537 * than sleeping allocation rate.
539 rate
= b
->slow_allocation_cycles
?
540 b
->rate_alloc
: VMW_BALLOON_NOSLEEP_ALLOC_MAX
;
542 pr_debug("%s - goal: %d, no-sleep rate: %d, sleep rate: %d\n",
543 __func__
, goal
, rate
, b
->rate_alloc
);
545 for (i
= 0; i
< goal
; i
++) {
547 error
= vmballoon_reserve_page(b
, alloc_can_sleep
);
549 if (error
!= -ENOMEM
) {
551 * Not a page allocation failure, stop this
552 * cycle. Maybe we'll get new target from
558 if (alloc_can_sleep
) {
560 * CANSLEEP page allocation failed, so guest
561 * is under severe memory pressure. Quickly
562 * decrease allocation rate.
564 b
->rate_alloc
= max(b
->rate_alloc
/ 2,
565 VMW_BALLOON_RATE_ALLOC_MIN
);
570 * NOSLEEP page allocation failed, so the guest is
571 * under memory pressure. Let us slow down page
572 * allocations for next few cycles so that the guest
573 * gets out of memory pressure. Also, if we already
574 * allocated b->rate_alloc pages, let's pause,
575 * otherwise switch to sleeping allocations.
577 b
->slow_allocation_cycles
= VMW_BALLOON_SLOW_CYCLES
;
579 if (i
>= b
->rate_alloc
)
582 alloc_can_sleep
= true;
583 /* Lower rate for sleeping allocations. */
584 rate
= b
->rate_alloc
;
587 if (++allocations
> VMW_BALLOON_YIELD_THRESHOLD
) {
593 /* We allocated enough pages, let's take a break. */
599 * We reached our goal without failures so try increasing
602 if (error
== 0 && i
>= b
->rate_alloc
) {
603 unsigned int mult
= i
/ b
->rate_alloc
;
606 min(b
->rate_alloc
+ mult
* VMW_BALLOON_RATE_ALLOC_INC
,
607 VMW_BALLOON_RATE_ALLOC_MAX
);
610 vmballoon_release_refused_pages(b
);
614 * Decrease the size of the balloon allowing guest to use more memory.
616 static void vmballoon_deflate(struct vmballoon
*b
)
618 struct page
*page
, *next
;
623 pr_debug("%s - size: %d, target %d\n", __func__
, b
->size
, b
->target
);
625 /* limit deallocation rate */
626 goal
= min(b
->size
- b
->target
, b
->rate_free
);
628 pr_debug("%s - goal: %d, rate: %d\n", __func__
, goal
, b
->rate_free
);
630 /* free pages to reach target */
631 list_for_each_entry_safe(page
, next
, &b
->pages
, lru
) {
632 error
= vmballoon_release_page(b
, page
);
634 /* quickly decrease rate in case of error */
635 b
->rate_free
= max(b
->rate_free
/ 2,
636 VMW_BALLOON_RATE_FREE_MIN
);
644 /* slowly increase rate if there were no errors */
645 b
->rate_free
= min(b
->rate_free
+ VMW_BALLOON_RATE_FREE_INC
,
646 VMW_BALLOON_RATE_FREE_MAX
);
650 * Balloon work function: reset protocol, if needed, get the new size and
651 * adjust balloon as needed. Repeat in 1 sec.
653 static void vmballoon_work(struct work_struct
*work
)
655 struct delayed_work
*dwork
= to_delayed_work(work
);
656 struct vmballoon
*b
= container_of(dwork
, struct vmballoon
, dwork
);
659 STATS_INC(b
->stats
.timer
);
661 if (b
->reset_required
)
664 if (b
->slow_allocation_cycles
> 0)
665 b
->slow_allocation_cycles
--;
667 if (vmballoon_send_get_target(b
, &target
)) {
668 /* update target, adjust size */
671 if (b
->size
< target
)
672 vmballoon_inflate(b
);
673 else if (b
->size
> target
)
674 vmballoon_deflate(b
);
678 * We are using a freezable workqueue so that balloon operations are
679 * stopped while the system transitions to/from sleep/hibernation.
681 queue_delayed_work(system_freezable_wq
,
682 dwork
, round_jiffies_relative(HZ
));
688 #ifdef CONFIG_DEBUG_FS
690 static int vmballoon_debug_show(struct seq_file
*f
, void *offset
)
692 struct vmballoon
*b
= f
->private;
693 struct vmballoon_stats
*stats
= &b
->stats
;
695 /* format size info */
697 "target: %8d pages\n"
698 "current: %8d pages\n",
701 /* format rate info */
703 "rateNoSleepAlloc: %8d pages/sec\n"
704 "rateSleepAlloc: %8d pages/sec\n"
705 "rateFree: %8d pages/sec\n",
706 VMW_BALLOON_NOSLEEP_ALLOC_MAX
,
707 b
->rate_alloc
, b
->rate_free
);
712 "start: %8u (%4u failed)\n"
713 "guestType: %8u (%4u failed)\n"
714 "lock: %8u (%4u failed)\n"
715 "unlock: %8u (%4u failed)\n"
716 "target: %8u (%4u failed)\n"
717 "primNoSleepAlloc: %8u (%4u failed)\n"
718 "primCanSleepAlloc: %8u (%4u failed)\n"
723 stats
->start
, stats
->start_fail
,
724 stats
->guest_type
, stats
->guest_type_fail
,
725 stats
->lock
, stats
->lock_fail
,
726 stats
->unlock
, stats
->unlock_fail
,
727 stats
->target
, stats
->target_fail
,
728 stats
->alloc
, stats
->alloc_fail
,
729 stats
->sleep_alloc
, stats
->sleep_alloc_fail
,
731 stats
->refused_alloc
, stats
->refused_free
);
736 static int vmballoon_debug_open(struct inode
*inode
, struct file
*file
)
738 return single_open(file
, vmballoon_debug_show
, inode
->i_private
);
741 static const struct file_operations vmballoon_debug_fops
= {
742 .owner
= THIS_MODULE
,
743 .open
= vmballoon_debug_open
,
746 .release
= single_release
,
749 static int __init
vmballoon_debugfs_init(struct vmballoon
*b
)
753 b
->dbg_entry
= debugfs_create_file("vmmemctl", S_IRUGO
, NULL
, b
,
754 &vmballoon_debug_fops
);
755 if (IS_ERR(b
->dbg_entry
)) {
756 error
= PTR_ERR(b
->dbg_entry
);
757 pr_err("failed to create debugfs entry, error: %d\n", error
);
764 static void __exit
vmballoon_debugfs_exit(struct vmballoon
*b
)
766 debugfs_remove(b
->dbg_entry
);
771 static inline int vmballoon_debugfs_init(struct vmballoon
*b
)
776 static inline void vmballoon_debugfs_exit(struct vmballoon
*b
)
780 #endif /* CONFIG_DEBUG_FS */
782 static int __init
vmballoon_init(void)
787 * Check if we are running on VMware's hypervisor and bail out
790 if (x86_hyper
!= &x86_hyper_vmware
)
793 INIT_LIST_HEAD(&balloon
.pages
);
794 INIT_LIST_HEAD(&balloon
.refused_pages
);
796 /* initialize rates */
797 balloon
.rate_alloc
= VMW_BALLOON_RATE_ALLOC_MAX
;
798 balloon
.rate_free
= VMW_BALLOON_RATE_FREE_MAX
;
800 INIT_DELAYED_WORK(&balloon
.dwork
, vmballoon_work
);
805 if (!vmballoon_send_start(&balloon
)) {
806 pr_err("failed to send start command to the host\n");
810 if (!vmballoon_send_guest_id(&balloon
)) {
811 pr_err("failed to send guest ID to the host\n");
815 error
= vmballoon_debugfs_init(&balloon
);
819 queue_delayed_work(system_freezable_wq
, &balloon
.dwork
, 0);
823 module_init(vmballoon_init
);
825 static void __exit
vmballoon_exit(void)
827 cancel_delayed_work_sync(&balloon
.dwork
);
829 vmballoon_debugfs_exit(&balloon
);
832 * Deallocate all reserved memory, and reset connection with monitor.
833 * Reset connection before deallocating memory to avoid potential for
834 * additional spurious resets from guest touching deallocated pages.
836 vmballoon_send_start(&balloon
);
837 vmballoon_pop(&balloon
);
839 module_exit(vmballoon_exit
);