// SPDX-License-Identifier: GPL-2.0
/*
* VMware Balloon driver.
*
* Copyright (C) 2000-2018, VMware, Inc. All Rights Reserved.
*
* This is VMware physical memory management driver for Linux. The driver
* acts like a "balloon" that can be inflated to reclaim physical pages by
* reserving them in the guest and invalidating them in the monitor,
* freeing up the underlying machine pages so they can be allocated to
* other guests. The balloon can also be deflated to allow the guest to
* use more physical memory. Higher level policies can control the sizes
* of balloons in VMs in order to manage physical memory resources.
*/
//#define DEBUG
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/vmalloc.h>
#include <linux/sched.h>
#include <linux/module.h>
#include <linux/workqueue.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <linux/rwsem.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/vmw_vmci_defs.h>
#include <linux/vmw_vmci_api.h>
#include <asm/hypervisor.h>
MODULE_AUTHOR("VMware, Inc.");
MODULE_DESCRIPTION("VMware Memory Control (Balloon) Driver");
MODULE_VERSION("1.5.0.0-k");
MODULE_ALIAS("dmi:*:svnVMware*:*");
MODULE_ALIAS("vmware_vmmemctl");
MODULE_LICENSE("GPL");
/*
* Use __GFP_HIGHMEM to allow pages from HIGHMEM zone. We don't allow wait
* (__GFP_RECLAIM) for huge page allocations. Use __GFP_NOWARN, to suppress page
* allocation failure warnings. Disallow access to emergency low-memory pools.
*/
#define VMW_HUGE_PAGE_ALLOC_FLAGS (__GFP_HIGHMEM|__GFP_NOWARN| \
__GFP_NOMEMALLOC)
/*
* Use __GFP_HIGHMEM to allow pages from HIGHMEM zone. We allow lightweight
* reclamation (__GFP_NORETRY). Use __GFP_NOWARN, to suppress page allocation
* failure warnings. Disallow access to emergency low-memory pools.
*/
#define VMW_PAGE_ALLOC_FLAGS (__GFP_HIGHMEM|__GFP_NOWARN| \
__GFP_NOMEMALLOC|__GFP_NORETRY)
/* Maximum number of refused pages we accumulate during inflation cycle */
#define VMW_BALLOON_MAX_REFUSED 16
/*
* Hypervisor communication port definitions.
*/
#define VMW_BALLOON_HV_PORT 0x5670
#define VMW_BALLOON_HV_MAGIC 0x456c6d6f
#define VMW_BALLOON_GUEST_ID 1 /* Linux */
enum vmwballoon_capabilities {
/*
* Bit 0 is reserved and not associated to any capability.
*/
VMW_BALLOON_BASIC_CMDS = (1 << 1),
VMW_BALLOON_BATCHED_CMDS = (1 << 2),
VMW_BALLOON_BATCHED_2M_CMDS = (1 << 3),
VMW_BALLOON_SIGNALLED_WAKEUP_CMD = (1 << 4),
};
#define VMW_BALLOON_CAPABILITIES (VMW_BALLOON_BASIC_CMDS \
| VMW_BALLOON_BATCHED_CMDS \
| VMW_BALLOON_BATCHED_2M_CMDS \
| VMW_BALLOON_SIGNALLED_WAKEUP_CMD)
#define VMW_BALLOON_2M_ORDER (PMD_SHIFT - PAGE_SHIFT)
enum vmballoon_page_size_type {
VMW_BALLOON_4K_PAGE,
VMW_BALLOON_2M_PAGE,
VMW_BALLOON_LAST_SIZE = VMW_BALLOON_2M_PAGE
};
#define VMW_BALLOON_NUM_PAGE_SIZES (VMW_BALLOON_LAST_SIZE + 1)
static const char * const vmballoon_page_size_names[] = {
[VMW_BALLOON_4K_PAGE] = "4k",
[VMW_BALLOON_2M_PAGE] = "2M"
};
enum vmballoon_op {
VMW_BALLOON_INFLATE,
VMW_BALLOON_DEFLATE
};
enum vmballoon_op_stat_type {
VMW_BALLOON_OP_STAT,
VMW_BALLOON_OP_FAIL_STAT
};
#define VMW_BALLOON_OP_STAT_TYPES (VMW_BALLOON_OP_FAIL_STAT + 1)
/**
* enum vmballoon_cmd_type - backdoor commands.
*
* Availability of the commands is as followed:
*
* %VMW_BALLOON_CMD_START, %VMW_BALLOON_CMD_GET_TARGET and
* %VMW_BALLOON_CMD_GUEST_ID are always available.
*
* If the host reports %VMW_BALLOON_BASIC_CMDS are supported then
* %VMW_BALLOON_CMD_LOCK and %VMW_BALLOON_CMD_UNLOCK commands are available.
*
* If the host reports %VMW_BALLOON_BATCHED_CMDS are supported then
* %VMW_BALLOON_CMD_BATCHED_LOCK and VMW_BALLOON_CMD_BATCHED_UNLOCK commands
* are available.
*
* If the host reports %VMW_BALLOON_BATCHED_2M_CMDS are supported then
* %VMW_BALLOON_CMD_BATCHED_2M_LOCK and %VMW_BALLOON_CMD_BATCHED_2M_UNLOCK
* are supported.
*
* If the host reports VMW_BALLOON_SIGNALLED_WAKEUP_CMD is supported then
* VMW_BALLOON_CMD_VMCI_DOORBELL_SET command is supported.
*
* @VMW_BALLOON_CMD_START: Communicating supported version with the hypervisor.
* @VMW_BALLOON_CMD_GET_TARGET: Gets the balloon target size.
* @VMW_BALLOON_CMD_LOCK: Informs the hypervisor about a ballooned page.
* @VMW_BALLOON_CMD_UNLOCK: Informs the hypervisor about a page that is about
* to be deflated from the balloon.
* @VMW_BALLOON_CMD_GUEST_ID: Informs the hypervisor about the type of OS that
* runs in the VM.
* @VMW_BALLOON_CMD_BATCHED_LOCK: Inform the hypervisor about a batch of
* ballooned pages (up to 512).
* @VMW_BALLOON_CMD_BATCHED_UNLOCK: Inform the hypervisor about a batch of
* pages that are about to be deflated from the
* balloon (up to 512).
* @VMW_BALLOON_CMD_BATCHED_2M_LOCK: Similar to @VMW_BALLOON_CMD_BATCHED_LOCK
* for 2MB pages.
* @VMW_BALLOON_CMD_BATCHED_2M_UNLOCK: Similar to
* @VMW_BALLOON_CMD_BATCHED_UNLOCK for 2MB
* pages.
* @VMW_BALLOON_CMD_VMCI_DOORBELL_SET: A command to set doorbell notification
* that would be invoked when the balloon
* size changes.
* @VMW_BALLOON_CMD_LAST: Value of the last command.
*/
enum vmballoon_cmd_type {
VMW_BALLOON_CMD_START,
VMW_BALLOON_CMD_GET_TARGET,
VMW_BALLOON_CMD_LOCK,
VMW_BALLOON_CMD_UNLOCK,
VMW_BALLOON_CMD_GUEST_ID,
/* No command 5 */
VMW_BALLOON_CMD_BATCHED_LOCK = 6,
VMW_BALLOON_CMD_BATCHED_UNLOCK,
VMW_BALLOON_CMD_BATCHED_2M_LOCK,
VMW_BALLOON_CMD_BATCHED_2M_UNLOCK,
VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
VMW_BALLOON_CMD_LAST = VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
};
#define VMW_BALLOON_CMD_NUM (VMW_BALLOON_CMD_LAST + 1)
enum vmballoon_error_codes {
VMW_BALLOON_SUCCESS,
VMW_BALLOON_ERROR_CMD_INVALID,
VMW_BALLOON_ERROR_PPN_INVALID,
VMW_BALLOON_ERROR_PPN_LOCKED,
VMW_BALLOON_ERROR_PPN_UNLOCKED,
VMW_BALLOON_ERROR_PPN_PINNED,
VMW_BALLOON_ERROR_PPN_NOTNEEDED,
VMW_BALLOON_ERROR_RESET,
VMW_BALLOON_ERROR_BUSY
};
#define VMW_BALLOON_SUCCESS_WITH_CAPABILITIES (0x03000000)
#define VMW_BALLOON_CMD_WITH_TARGET_MASK \
((1UL << VMW_BALLOON_CMD_GET_TARGET) | \
(1UL << VMW_BALLOON_CMD_LOCK) | \
(1UL << VMW_BALLOON_CMD_UNLOCK) | \
(1UL << VMW_BALLOON_CMD_BATCHED_LOCK) | \
(1UL << VMW_BALLOON_CMD_BATCHED_UNLOCK) | \
(1UL << VMW_BALLOON_CMD_BATCHED_2M_LOCK) | \
(1UL << VMW_BALLOON_CMD_BATCHED_2M_UNLOCK))
static const char * const vmballo
|