xref: /OK3568_Linux_fs/kernel/drivers/misc/vmw_balloon.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * VMware Balloon driver.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2000-2018, VMware, Inc. All Rights Reserved.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * This is VMware physical memory management driver for Linux. The driver
8*4882a593Smuzhiyun  * acts like a "balloon" that can be inflated to reclaim physical pages by
9*4882a593Smuzhiyun  * reserving them in the guest and invalidating them in the monitor,
10*4882a593Smuzhiyun  * freeing up the underlying machine pages so they can be allocated to
11*4882a593Smuzhiyun  * other guests.  The balloon can also be deflated to allow the guest to
12*4882a593Smuzhiyun  * use more physical memory. Higher level policies can control the sizes
13*4882a593Smuzhiyun  * of balloons in VMs in order to manage physical memory resources.
14*4882a593Smuzhiyun  */
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun //#define DEBUG
17*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #include <linux/types.h>
20*4882a593Smuzhiyun #include <linux/io.h>
21*4882a593Smuzhiyun #include <linux/kernel.h>
22*4882a593Smuzhiyun #include <linux/mm.h>
23*4882a593Smuzhiyun #include <linux/vmalloc.h>
24*4882a593Smuzhiyun #include <linux/sched.h>
25*4882a593Smuzhiyun #include <linux/module.h>
26*4882a593Smuzhiyun #include <linux/workqueue.h>
27*4882a593Smuzhiyun #include <linux/debugfs.h>
28*4882a593Smuzhiyun #include <linux/seq_file.h>
29*4882a593Smuzhiyun #include <linux/rwsem.h>
30*4882a593Smuzhiyun #include <linux/slab.h>
31*4882a593Smuzhiyun #include <linux/spinlock.h>
32*4882a593Smuzhiyun #include <linux/mount.h>
33*4882a593Smuzhiyun #include <linux/pseudo_fs.h>
34*4882a593Smuzhiyun #include <linux/balloon_compaction.h>
35*4882a593Smuzhiyun #include <linux/vmw_vmci_defs.h>
36*4882a593Smuzhiyun #include <linux/vmw_vmci_api.h>
37*4882a593Smuzhiyun #include <asm/hypervisor.h>
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun MODULE_AUTHOR("VMware, Inc.");
40*4882a593Smuzhiyun MODULE_DESCRIPTION("VMware Memory Control (Balloon) Driver");
41*4882a593Smuzhiyun MODULE_ALIAS("dmi:*:svnVMware*:*");
42*4882a593Smuzhiyun MODULE_ALIAS("vmware_vmmemctl");
43*4882a593Smuzhiyun MODULE_LICENSE("GPL");
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun static bool __read_mostly vmwballoon_shrinker_enable;
46*4882a593Smuzhiyun module_param(vmwballoon_shrinker_enable, bool, 0444);
47*4882a593Smuzhiyun MODULE_PARM_DESC(vmwballoon_shrinker_enable,
48*4882a593Smuzhiyun 	"Enable non-cooperative out-of-memory protection. Disabled by default as it may degrade performance.");
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun /* Delay in seconds after shrink before inflation. */
51*4882a593Smuzhiyun #define VMBALLOON_SHRINK_DELAY		(5)
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun /* Maximum number of refused pages we accumulate during inflation cycle */
54*4882a593Smuzhiyun #define VMW_BALLOON_MAX_REFUSED		16
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun /* Magic number for the balloon mount-point */
57*4882a593Smuzhiyun #define BALLOON_VMW_MAGIC		0x0ba11007
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun /*
60*4882a593Smuzhiyun  * Hypervisor communication port definitions.
61*4882a593Smuzhiyun  */
62*4882a593Smuzhiyun #define VMW_BALLOON_HV_PORT		0x5670
63*4882a593Smuzhiyun #define VMW_BALLOON_HV_MAGIC		0x456c6d6f
64*4882a593Smuzhiyun #define VMW_BALLOON_GUEST_ID		1	/* Linux */
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun enum vmwballoon_capabilities {
67*4882a593Smuzhiyun 	/*
68*4882a593Smuzhiyun 	 * Bit 0 is reserved and not associated to any capability.
69*4882a593Smuzhiyun 	 */
70*4882a593Smuzhiyun 	VMW_BALLOON_BASIC_CMDS			= (1 << 1),
71*4882a593Smuzhiyun 	VMW_BALLOON_BATCHED_CMDS		= (1 << 2),
72*4882a593Smuzhiyun 	VMW_BALLOON_BATCHED_2M_CMDS		= (1 << 3),
73*4882a593Smuzhiyun 	VMW_BALLOON_SIGNALLED_WAKEUP_CMD	= (1 << 4),
74*4882a593Smuzhiyun 	VMW_BALLOON_64_BIT_TARGET		= (1 << 5)
75*4882a593Smuzhiyun };
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun #define VMW_BALLOON_CAPABILITIES_COMMON	(VMW_BALLOON_BASIC_CMDS \
78*4882a593Smuzhiyun 					| VMW_BALLOON_BATCHED_CMDS \
79*4882a593Smuzhiyun 					| VMW_BALLOON_BATCHED_2M_CMDS \
80*4882a593Smuzhiyun 					| VMW_BALLOON_SIGNALLED_WAKEUP_CMD)
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun #define VMW_BALLOON_2M_ORDER		(PMD_SHIFT - PAGE_SHIFT)
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun /*
85*4882a593Smuzhiyun  * 64-bit targets are only supported in 64-bit
86*4882a593Smuzhiyun  */
87*4882a593Smuzhiyun #ifdef CONFIG_64BIT
88*4882a593Smuzhiyun #define VMW_BALLOON_CAPABILITIES	(VMW_BALLOON_CAPABILITIES_COMMON \
89*4882a593Smuzhiyun 					| VMW_BALLOON_64_BIT_TARGET)
90*4882a593Smuzhiyun #else
91*4882a593Smuzhiyun #define VMW_BALLOON_CAPABILITIES	VMW_BALLOON_CAPABILITIES_COMMON
92*4882a593Smuzhiyun #endif
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun enum vmballoon_page_size_type {
95*4882a593Smuzhiyun 	VMW_BALLOON_4K_PAGE,
96*4882a593Smuzhiyun 	VMW_BALLOON_2M_PAGE,
97*4882a593Smuzhiyun 	VMW_BALLOON_LAST_SIZE = VMW_BALLOON_2M_PAGE
98*4882a593Smuzhiyun };
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun #define VMW_BALLOON_NUM_PAGE_SIZES	(VMW_BALLOON_LAST_SIZE + 1)
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun static const char * const vmballoon_page_size_names[] = {
103*4882a593Smuzhiyun 	[VMW_BALLOON_4K_PAGE]			= "4k",
104*4882a593Smuzhiyun 	[VMW_BALLOON_2M_PAGE]			= "2M"
105*4882a593Smuzhiyun };
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun enum vmballoon_op {
108*4882a593Smuzhiyun 	VMW_BALLOON_INFLATE,
109*4882a593Smuzhiyun 	VMW_BALLOON_DEFLATE
110*4882a593Smuzhiyun };
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun enum vmballoon_op_stat_type {
113*4882a593Smuzhiyun 	VMW_BALLOON_OP_STAT,
114*4882a593Smuzhiyun 	VMW_BALLOON_OP_FAIL_STAT
115*4882a593Smuzhiyun };
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun #define VMW_BALLOON_OP_STAT_TYPES	(VMW_BALLOON_OP_FAIL_STAT + 1)
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun /**
120*4882a593Smuzhiyun  * enum vmballoon_cmd_type - backdoor commands.
121*4882a593Smuzhiyun  *
122*4882a593Smuzhiyun  * Availability of the commands is as followed:
123*4882a593Smuzhiyun  *
124*4882a593Smuzhiyun  * %VMW_BALLOON_CMD_START, %VMW_BALLOON_CMD_GET_TARGET and
125*4882a593Smuzhiyun  * %VMW_BALLOON_CMD_GUEST_ID are always available.
126*4882a593Smuzhiyun  *
127*4882a593Smuzhiyun  * If the host reports %VMW_BALLOON_BASIC_CMDS are supported then
128*4882a593Smuzhiyun  * %VMW_BALLOON_CMD_LOCK and %VMW_BALLOON_CMD_UNLOCK commands are available.
129*4882a593Smuzhiyun  *
130*4882a593Smuzhiyun  * If the host reports %VMW_BALLOON_BATCHED_CMDS are supported then
131*4882a593Smuzhiyun  * %VMW_BALLOON_CMD_BATCHED_LOCK and VMW_BALLOON_CMD_BATCHED_UNLOCK commands
132*4882a593Smuzhiyun  * are available.
133*4882a593Smuzhiyun  *
134*4882a593Smuzhiyun  * If the host reports %VMW_BALLOON_BATCHED_2M_CMDS are supported then
135*4882a593Smuzhiyun  * %VMW_BALLOON_CMD_BATCHED_2M_LOCK and %VMW_BALLOON_CMD_BATCHED_2M_UNLOCK
136*4882a593Smuzhiyun  * are supported.
137*4882a593Smuzhiyun  *
138*4882a593Smuzhiyun  * If the host reports  VMW_BALLOON_SIGNALLED_WAKEUP_CMD is supported then
139*4882a593Smuzhiyun  * VMW_BALLOON_CMD_VMCI_DOORBELL_SET command is supported.
140*4882a593Smuzhiyun  *
141*4882a593Smuzhiyun  * @VMW_BALLOON_CMD_START: Communicating supported version with the hypervisor.
142*4882a593Smuzhiyun  * @VMW_BALLOON_CMD_GET_TARGET: Gets the balloon target size.
143*4882a593Smuzhiyun  * @VMW_BALLOON_CMD_LOCK: Informs the hypervisor about a ballooned page.
144*4882a593Smuzhiyun  * @VMW_BALLOON_CMD_UNLOCK: Informs the hypervisor about a page that is about
145*4882a593Smuzhiyun  *			    to be deflated from the balloon.
146*4882a593Smuzhiyun  * @VMW_BALLOON_CMD_GUEST_ID: Informs the hypervisor about the type of OS that
147*4882a593Smuzhiyun  *			      runs in the VM.
148*4882a593Smuzhiyun  * @VMW_BALLOON_CMD_BATCHED_LOCK: Inform the hypervisor about a batch of
149*4882a593Smuzhiyun  *				  ballooned pages (up to 512).
150*4882a593Smuzhiyun  * @VMW_BALLOON_CMD_BATCHED_UNLOCK: Inform the hypervisor about a batch of
151*4882a593Smuzhiyun  *				  pages that are about to be deflated from the
152*4882a593Smuzhiyun  *				  balloon (up to 512).
153*4882a593Smuzhiyun  * @VMW_BALLOON_CMD_BATCHED_2M_LOCK: Similar to @VMW_BALLOON_CMD_BATCHED_LOCK
154*4882a593Smuzhiyun  *				     for 2MB pages.
155*4882a593Smuzhiyun  * @VMW_BALLOON_CMD_BATCHED_2M_UNLOCK: Similar to
156*4882a593Smuzhiyun  *				       @VMW_BALLOON_CMD_BATCHED_UNLOCK for 2MB
157*4882a593Smuzhiyun  *				       pages.
158*4882a593Smuzhiyun  * @VMW_BALLOON_CMD_VMCI_DOORBELL_SET: A command to set doorbell notification
159*4882a593Smuzhiyun  *				       that would be invoked when the balloon
160*4882a593Smuzhiyun  *				       size changes.
161*4882a593Smuzhiyun  * @VMW_BALLOON_CMD_LAST: Value of the last command.
162*4882a593Smuzhiyun  */
163*4882a593Smuzhiyun enum vmballoon_cmd_type {
164*4882a593Smuzhiyun 	VMW_BALLOON_CMD_START,
165*4882a593Smuzhiyun 	VMW_BALLOON_CMD_GET_TARGET,
166*4882a593Smuzhiyun 	VMW_BALLOON_CMD_LOCK,
167*4882a593Smuzhiyun 	VMW_BALLOON_CMD_UNLOCK,
168*4882a593Smuzhiyun 	VMW_BALLOON_CMD_GUEST_ID,
169*4882a593Smuzhiyun 	/* No command 5 */
170*4882a593Smuzhiyun 	VMW_BALLOON_CMD_BATCHED_LOCK = 6,
171*4882a593Smuzhiyun 	VMW_BALLOON_CMD_BATCHED_UNLOCK,
172*4882a593Smuzhiyun 	VMW_BALLOON_CMD_BATCHED_2M_LOCK,
173*4882a593Smuzhiyun 	VMW_BALLOON_CMD_BATCHED_2M_UNLOCK,
174*4882a593Smuzhiyun 	VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
175*4882a593Smuzhiyun 	VMW_BALLOON_CMD_LAST = VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
176*4882a593Smuzhiyun };
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun #define VMW_BALLOON_CMD_NUM	(VMW_BALLOON_CMD_LAST + 1)
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun enum vmballoon_error_codes {
181*4882a593Smuzhiyun 	VMW_BALLOON_SUCCESS,
182*4882a593Smuzhiyun 	VMW_BALLOON_ERROR_CMD_INVALID,
183*4882a593Smuzhiyun 	VMW_BALLOON_ERROR_PPN_INVALID,
184*4882a593Smuzhiyun 	VMW_BALLOON_ERROR_PPN_LOCKED,
185*4882a593Smuzhiyun 	VMW_BALLOON_ERROR_PPN_UNLOCKED,
186*4882a593Smuzhiyun 	VMW_BALLOON_ERROR_PPN_PINNED,
187*4882a593Smuzhiyun 	VMW_BALLOON_ERROR_PPN_NOTNEEDED,
188*4882a593Smuzhiyun 	VMW_BALLOON_ERROR_RESET,
189*4882a593Smuzhiyun 	VMW_BALLOON_ERROR_BUSY
190*4882a593Smuzhiyun };
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun #define VMW_BALLOON_SUCCESS_WITH_CAPABILITIES	(0x03000000)
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun #define VMW_BALLOON_CMD_WITH_TARGET_MASK			\
195*4882a593Smuzhiyun 	((1UL << VMW_BALLOON_CMD_GET_TARGET)		|	\
196*4882a593Smuzhiyun 	 (1UL << VMW_BALLOON_CMD_LOCK)			|	\
197*4882a593Smuzhiyun 	 (1UL << VMW_BALLOON_CMD_UNLOCK)		|	\
198*4882a593Smuzhiyun 	 (1UL << VMW_BALLOON_CMD_BATCHED_LOCK)		|	\
199*4882a593Smuzhiyun 	 (1UL << VMW_BALLOON_CMD_BATCHED_UNLOCK)	|	\
200*4882a593Smuzhiyun 	 (1UL << VMW_BALLOON_CMD_BATCHED_2M_LOCK)	|	\
201*4882a593Smuzhiyun 	 (1UL << VMW_BALLOON_CMD_BATCHED_2M_UNLOCK))
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun static const char * const vmballoon_cmd_names[] = {
204*4882a593Smuzhiyun 	[VMW_BALLOON_CMD_START]			= "start",
205*4882a593Smuzhiyun 	[VMW_BALLOON_CMD_GET_TARGET]		= "target",
206*4882a593Smuzhiyun 	[VMW_BALLOON_CMD_LOCK]			= "lock",
207*4882a593Smuzhiyun 	[VMW_BALLOON_CMD_UNLOCK]		= "unlock",
208*4882a593Smuzhiyun 	[VMW_BALLOON_CMD_GUEST_ID]		= "guestType",
209*4882a593Smuzhiyun 	[VMW_BALLOON_CMD_BATCHED_LOCK]		= "batchLock",
210*4882a593Smuzhiyun 	[VMW_BALLOON_CMD_BATCHED_UNLOCK]	= "batchUnlock",
211*4882a593Smuzhiyun 	[VMW_BALLOON_CMD_BATCHED_2M_LOCK]	= "2m-lock",
212*4882a593Smuzhiyun 	[VMW_BALLOON_CMD_BATCHED_2M_UNLOCK]	= "2m-unlock",
213*4882a593Smuzhiyun 	[VMW_BALLOON_CMD_VMCI_DOORBELL_SET]	= "doorbellSet"
214*4882a593Smuzhiyun };
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun enum vmballoon_stat_page {
217*4882a593Smuzhiyun 	VMW_BALLOON_PAGE_STAT_ALLOC,
218*4882a593Smuzhiyun 	VMW_BALLOON_PAGE_STAT_ALLOC_FAIL,
219*4882a593Smuzhiyun 	VMW_BALLOON_PAGE_STAT_REFUSED_ALLOC,
220*4882a593Smuzhiyun 	VMW_BALLOON_PAGE_STAT_REFUSED_FREE,
221*4882a593Smuzhiyun 	VMW_BALLOON_PAGE_STAT_FREE,
222*4882a593Smuzhiyun 	VMW_BALLOON_PAGE_STAT_LAST = VMW_BALLOON_PAGE_STAT_FREE
223*4882a593Smuzhiyun };
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun #define VMW_BALLOON_PAGE_STAT_NUM	(VMW_BALLOON_PAGE_STAT_LAST + 1)
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun enum vmballoon_stat_general {
228*4882a593Smuzhiyun 	VMW_BALLOON_STAT_TIMER,
229*4882a593Smuzhiyun 	VMW_BALLOON_STAT_DOORBELL,
230*4882a593Smuzhiyun 	VMW_BALLOON_STAT_RESET,
231*4882a593Smuzhiyun 	VMW_BALLOON_STAT_SHRINK,
232*4882a593Smuzhiyun 	VMW_BALLOON_STAT_SHRINK_FREE,
233*4882a593Smuzhiyun 	VMW_BALLOON_STAT_LAST = VMW_BALLOON_STAT_SHRINK_FREE
234*4882a593Smuzhiyun };
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun #define VMW_BALLOON_STAT_NUM		(VMW_BALLOON_STAT_LAST + 1)
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun static DEFINE_STATIC_KEY_TRUE(vmw_balloon_batching);
239*4882a593Smuzhiyun static DEFINE_STATIC_KEY_FALSE(balloon_stat_enabled);
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun struct vmballoon_ctl {
242*4882a593Smuzhiyun 	struct list_head pages;
243*4882a593Smuzhiyun 	struct list_head refused_pages;
244*4882a593Smuzhiyun 	struct list_head prealloc_pages;
245*4882a593Smuzhiyun 	unsigned int n_refused_pages;
246*4882a593Smuzhiyun 	unsigned int n_pages;
247*4882a593Smuzhiyun 	enum vmballoon_page_size_type page_size;
248*4882a593Smuzhiyun 	enum vmballoon_op op;
249*4882a593Smuzhiyun };
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun /**
252*4882a593Smuzhiyun  * struct vmballoon_batch_entry - a batch entry for lock or unlock.
253*4882a593Smuzhiyun  *
254*4882a593Smuzhiyun  * @status: the status of the operation, which is written by the hypervisor.
255*4882a593Smuzhiyun  * @reserved: reserved for future use. Must be set to zero.
256*4882a593Smuzhiyun  * @pfn: the physical frame number of the page to be locked or unlocked.
257*4882a593Smuzhiyun  */
258*4882a593Smuzhiyun struct vmballoon_batch_entry {
259*4882a593Smuzhiyun 	u64 status : 5;
260*4882a593Smuzhiyun 	u64 reserved : PAGE_SHIFT - 5;
261*4882a593Smuzhiyun 	u64 pfn : 52;
262*4882a593Smuzhiyun } __packed;
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun struct vmballoon {
265*4882a593Smuzhiyun 	/**
266*4882a593Smuzhiyun 	 * @max_page_size: maximum supported page size for ballooning.
267*4882a593Smuzhiyun 	 *
268*4882a593Smuzhiyun 	 * Protected by @conf_sem
269*4882a593Smuzhiyun 	 */
270*4882a593Smuzhiyun 	enum vmballoon_page_size_type max_page_size;
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	/**
273*4882a593Smuzhiyun 	 * @size: balloon actual size in basic page size (frames).
274*4882a593Smuzhiyun 	 *
275*4882a593Smuzhiyun 	 * While we currently do not support size which is bigger than 32-bit,
276*4882a593Smuzhiyun 	 * in preparation for future support, use 64-bits.
277*4882a593Smuzhiyun 	 */
278*4882a593Smuzhiyun 	atomic64_t size;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	/**
281*4882a593Smuzhiyun 	 * @target: balloon target size in basic page size (frames).
282*4882a593Smuzhiyun 	 *
283*4882a593Smuzhiyun 	 * We do not protect the target under the assumption that setting the
284*4882a593Smuzhiyun 	 * value is always done through a single write. If this assumption ever
285*4882a593Smuzhiyun 	 * breaks, we would have to use X_ONCE for accesses, and suffer the less
286*4882a593Smuzhiyun 	 * optimized code. Although we may read stale target value if multiple
287*4882a593Smuzhiyun 	 * accesses happen at once, the performance impact should be minor.
288*4882a593Smuzhiyun 	 */
289*4882a593Smuzhiyun 	unsigned long target;
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 	/**
292*4882a593Smuzhiyun 	 * @reset_required: reset flag
293*4882a593Smuzhiyun 	 *
294*4882a593Smuzhiyun 	 * Setting this flag may introduce races, but the code is expected to
295*4882a593Smuzhiyun 	 * handle them gracefully. In the worst case, another operation will
296*4882a593Smuzhiyun 	 * fail as reset did not take place. Clearing the flag is done while
297*4882a593Smuzhiyun 	 * holding @conf_sem for write.
298*4882a593Smuzhiyun 	 */
299*4882a593Smuzhiyun 	bool reset_required;
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	/**
302*4882a593Smuzhiyun 	 * @capabilities: hypervisor balloon capabilities.
303*4882a593Smuzhiyun 	 *
304*4882a593Smuzhiyun 	 * Protected by @conf_sem.
305*4882a593Smuzhiyun 	 */
306*4882a593Smuzhiyun 	unsigned long capabilities;
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	/**
309*4882a593Smuzhiyun 	 * @batch_page: pointer to communication batch page.
310*4882a593Smuzhiyun 	 *
311*4882a593Smuzhiyun 	 * When batching is used, batch_page points to a page, which holds up to
312*4882a593Smuzhiyun 	 * %VMW_BALLOON_BATCH_MAX_PAGES entries for locking or unlocking.
313*4882a593Smuzhiyun 	 */
314*4882a593Smuzhiyun 	struct vmballoon_batch_entry *batch_page;
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	/**
317*4882a593Smuzhiyun 	 * @batch_max_pages: maximum pages that can be locked/unlocked.
318*4882a593Smuzhiyun 	 *
319*4882a593Smuzhiyun 	 * Indicates the number of pages that the hypervisor can lock or unlock
320*4882a593Smuzhiyun 	 * at once, according to whether batching is enabled. If batching is
321*4882a593Smuzhiyun 	 * disabled, only a single page can be locked/unlock on each operation.
322*4882a593Smuzhiyun 	 *
323*4882a593Smuzhiyun 	 * Protected by @conf_sem.
324*4882a593Smuzhiyun 	 */
325*4882a593Smuzhiyun 	unsigned int batch_max_pages;
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	/**
328*4882a593Smuzhiyun 	 * @page: page to be locked/unlocked by the hypervisor
329*4882a593Smuzhiyun 	 *
330*4882a593Smuzhiyun 	 * @page is only used when batching is disabled and a single page is
331*4882a593Smuzhiyun 	 * reclaimed on each iteration.
332*4882a593Smuzhiyun 	 *
333*4882a593Smuzhiyun 	 * Protected by @comm_lock.
334*4882a593Smuzhiyun 	 */
335*4882a593Smuzhiyun 	struct page *page;
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	/**
338*4882a593Smuzhiyun 	 * @shrink_timeout: timeout until the next inflation.
339*4882a593Smuzhiyun 	 *
340*4882a593Smuzhiyun 	 * After an shrink event, indicates the time in jiffies after which
341*4882a593Smuzhiyun 	 * inflation is allowed again. Can be written concurrently with reads,
342*4882a593Smuzhiyun 	 * so must use READ_ONCE/WRITE_ONCE when accessing.
343*4882a593Smuzhiyun 	 */
344*4882a593Smuzhiyun 	unsigned long shrink_timeout;
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 	/* statistics */
347*4882a593Smuzhiyun 	struct vmballoon_stats *stats;
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_FS
350*4882a593Smuzhiyun 	/* debugfs file exporting statistics */
351*4882a593Smuzhiyun 	struct dentry *dbg_entry;
352*4882a593Smuzhiyun #endif
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	/**
355*4882a593Smuzhiyun 	 * @b_dev_info: balloon device information descriptor.
356*4882a593Smuzhiyun 	 */
357*4882a593Smuzhiyun 	struct balloon_dev_info b_dev_info;
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	struct delayed_work dwork;
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	/**
362*4882a593Smuzhiyun 	 * @huge_pages - list of the inflated 2MB pages.
363*4882a593Smuzhiyun 	 *
364*4882a593Smuzhiyun 	 * Protected by @b_dev_info.pages_lock .
365*4882a593Smuzhiyun 	 */
366*4882a593Smuzhiyun 	struct list_head huge_pages;
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 	/**
369*4882a593Smuzhiyun 	 * @vmci_doorbell.
370*4882a593Smuzhiyun 	 *
371*4882a593Smuzhiyun 	 * Protected by @conf_sem.
372*4882a593Smuzhiyun 	 */
373*4882a593Smuzhiyun 	struct vmci_handle vmci_doorbell;
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	/**
376*4882a593Smuzhiyun 	 * @conf_sem: semaphore to protect the configuration and the statistics.
377*4882a593Smuzhiyun 	 */
378*4882a593Smuzhiyun 	struct rw_semaphore conf_sem;
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 	/**
381*4882a593Smuzhiyun 	 * @comm_lock: lock to protect the communication with the host.
382*4882a593Smuzhiyun 	 *
383*4882a593Smuzhiyun 	 * Lock ordering: @conf_sem -> @comm_lock .
384*4882a593Smuzhiyun 	 */
385*4882a593Smuzhiyun 	spinlock_t comm_lock;
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	/**
388*4882a593Smuzhiyun 	 * @shrinker: shrinker interface that is used to avoid over-inflation.
389*4882a593Smuzhiyun 	 */
390*4882a593Smuzhiyun 	struct shrinker shrinker;
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	/**
393*4882a593Smuzhiyun 	 * @shrinker_registered: whether the shrinker was registered.
394*4882a593Smuzhiyun 	 *
395*4882a593Smuzhiyun 	 * The shrinker interface does not handle gracefully the removal of
396*4882a593Smuzhiyun 	 * shrinker that was not registered before. This indication allows to
397*4882a593Smuzhiyun 	 * simplify the unregistration process.
398*4882a593Smuzhiyun 	 */
399*4882a593Smuzhiyun 	bool shrinker_registered;
400*4882a593Smuzhiyun };
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun static struct vmballoon balloon;
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun struct vmballoon_stats {
405*4882a593Smuzhiyun 	/* timer / doorbell operations */
406*4882a593Smuzhiyun 	atomic64_t general_stat[VMW_BALLOON_STAT_NUM];
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	/* allocation statistics for huge and small pages */
409*4882a593Smuzhiyun 	atomic64_t
410*4882a593Smuzhiyun 	       page_stat[VMW_BALLOON_PAGE_STAT_NUM][VMW_BALLOON_NUM_PAGE_SIZES];
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	/* Monitor operations: total operations, and failures */
413*4882a593Smuzhiyun 	atomic64_t ops[VMW_BALLOON_CMD_NUM][VMW_BALLOON_OP_STAT_TYPES];
414*4882a593Smuzhiyun };
415*4882a593Smuzhiyun 
is_vmballoon_stats_on(void)416*4882a593Smuzhiyun static inline bool is_vmballoon_stats_on(void)
417*4882a593Smuzhiyun {
418*4882a593Smuzhiyun 	return IS_ENABLED(CONFIG_DEBUG_FS) &&
419*4882a593Smuzhiyun 		static_branch_unlikely(&balloon_stat_enabled);
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun 
vmballoon_stats_op_inc(struct vmballoon * b,unsigned int op,enum vmballoon_op_stat_type type)422*4882a593Smuzhiyun static inline void vmballoon_stats_op_inc(struct vmballoon *b, unsigned int op,
423*4882a593Smuzhiyun 					  enum vmballoon_op_stat_type type)
424*4882a593Smuzhiyun {
425*4882a593Smuzhiyun 	if (is_vmballoon_stats_on())
426*4882a593Smuzhiyun 		atomic64_inc(&b->stats->ops[op][type]);
427*4882a593Smuzhiyun }
428*4882a593Smuzhiyun 
vmballoon_stats_gen_inc(struct vmballoon * b,enum vmballoon_stat_general stat)429*4882a593Smuzhiyun static inline void vmballoon_stats_gen_inc(struct vmballoon *b,
430*4882a593Smuzhiyun 					   enum vmballoon_stat_general stat)
431*4882a593Smuzhiyun {
432*4882a593Smuzhiyun 	if (is_vmballoon_stats_on())
433*4882a593Smuzhiyun 		atomic64_inc(&b->stats->general_stat[stat]);
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun 
vmballoon_stats_gen_add(struct vmballoon * b,enum vmballoon_stat_general stat,unsigned int val)436*4882a593Smuzhiyun static inline void vmballoon_stats_gen_add(struct vmballoon *b,
437*4882a593Smuzhiyun 					   enum vmballoon_stat_general stat,
438*4882a593Smuzhiyun 					   unsigned int val)
439*4882a593Smuzhiyun {
440*4882a593Smuzhiyun 	if (is_vmballoon_stats_on())
441*4882a593Smuzhiyun 		atomic64_add(val, &b->stats->general_stat[stat]);
442*4882a593Smuzhiyun }
443*4882a593Smuzhiyun 
vmballoon_stats_page_inc(struct vmballoon * b,enum vmballoon_stat_page stat,enum vmballoon_page_size_type size)444*4882a593Smuzhiyun static inline void vmballoon_stats_page_inc(struct vmballoon *b,
445*4882a593Smuzhiyun 					    enum vmballoon_stat_page stat,
446*4882a593Smuzhiyun 					    enum vmballoon_page_size_type size)
447*4882a593Smuzhiyun {
448*4882a593Smuzhiyun 	if (is_vmballoon_stats_on())
449*4882a593Smuzhiyun 		atomic64_inc(&b->stats->page_stat[stat][size]);
450*4882a593Smuzhiyun }
451*4882a593Smuzhiyun 
vmballoon_stats_page_add(struct vmballoon * b,enum vmballoon_stat_page stat,enum vmballoon_page_size_type size,unsigned int val)452*4882a593Smuzhiyun static inline void vmballoon_stats_page_add(struct vmballoon *b,
453*4882a593Smuzhiyun 					    enum vmballoon_stat_page stat,
454*4882a593Smuzhiyun 					    enum vmballoon_page_size_type size,
455*4882a593Smuzhiyun 					    unsigned int val)
456*4882a593Smuzhiyun {
457*4882a593Smuzhiyun 	if (is_vmballoon_stats_on())
458*4882a593Smuzhiyun 		atomic64_add(val, &b->stats->page_stat[stat][size]);
459*4882a593Smuzhiyun }
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun static inline unsigned long
__vmballoon_cmd(struct vmballoon * b,unsigned long cmd,unsigned long arg1,unsigned long arg2,unsigned long * result)462*4882a593Smuzhiyun __vmballoon_cmd(struct vmballoon *b, unsigned long cmd, unsigned long arg1,
463*4882a593Smuzhiyun 		unsigned long arg2, unsigned long *result)
464*4882a593Smuzhiyun {
465*4882a593Smuzhiyun 	unsigned long status, dummy1, dummy2, dummy3, local_result;
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun 	vmballoon_stats_op_inc(b, cmd, VMW_BALLOON_OP_STAT);
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 	asm volatile ("inl %%dx" :
470*4882a593Smuzhiyun 		"=a"(status),
471*4882a593Smuzhiyun 		"=c"(dummy1),
472*4882a593Smuzhiyun 		"=d"(dummy2),
473*4882a593Smuzhiyun 		"=b"(local_result),
474*4882a593Smuzhiyun 		"=S"(dummy3) :
475*4882a593Smuzhiyun 		"0"(VMW_BALLOON_HV_MAGIC),
476*4882a593Smuzhiyun 		"1"(cmd),
477*4882a593Smuzhiyun 		"2"(VMW_BALLOON_HV_PORT),
478*4882a593Smuzhiyun 		"3"(arg1),
479*4882a593Smuzhiyun 		"4"(arg2) :
480*4882a593Smuzhiyun 		"memory");
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	/* update the result if needed */
483*4882a593Smuzhiyun 	if (result)
484*4882a593Smuzhiyun 		*result = (cmd == VMW_BALLOON_CMD_START) ? dummy1 :
485*4882a593Smuzhiyun 							   local_result;
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun 	/* update target when applicable */
488*4882a593Smuzhiyun 	if (status == VMW_BALLOON_SUCCESS &&
489*4882a593Smuzhiyun 	    ((1ul << cmd) & VMW_BALLOON_CMD_WITH_TARGET_MASK))
490*4882a593Smuzhiyun 		WRITE_ONCE(b->target, local_result);
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 	if (status != VMW_BALLOON_SUCCESS &&
493*4882a593Smuzhiyun 	    status != VMW_BALLOON_SUCCESS_WITH_CAPABILITIES) {
494*4882a593Smuzhiyun 		vmballoon_stats_op_inc(b, cmd, VMW_BALLOON_OP_FAIL_STAT);
495*4882a593Smuzhiyun 		pr_debug("%s: %s [0x%lx,0x%lx) failed, returned %ld\n",
496*4882a593Smuzhiyun 			 __func__, vmballoon_cmd_names[cmd], arg1, arg2,
497*4882a593Smuzhiyun 			 status);
498*4882a593Smuzhiyun 	}
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 	/* mark reset required accordingly */
501*4882a593Smuzhiyun 	if (status == VMW_BALLOON_ERROR_RESET)
502*4882a593Smuzhiyun 		b->reset_required = true;
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun 	return status;
505*4882a593Smuzhiyun }
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun static __always_inline unsigned long
vmballoon_cmd(struct vmballoon * b,unsigned long cmd,unsigned long arg1,unsigned long arg2)508*4882a593Smuzhiyun vmballoon_cmd(struct vmballoon *b, unsigned long cmd, unsigned long arg1,
509*4882a593Smuzhiyun 	      unsigned long arg2)
510*4882a593Smuzhiyun {
511*4882a593Smuzhiyun 	unsigned long dummy;
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	return __vmballoon_cmd(b, cmd, arg1, arg2, &dummy);
514*4882a593Smuzhiyun }
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun /*
517*4882a593Smuzhiyun  * Send "start" command to the host, communicating supported version
518*4882a593Smuzhiyun  * of the protocol.
519*4882a593Smuzhiyun  */
vmballoon_send_start(struct vmballoon * b,unsigned long req_caps)520*4882a593Smuzhiyun static int vmballoon_send_start(struct vmballoon *b, unsigned long req_caps)
521*4882a593Smuzhiyun {
522*4882a593Smuzhiyun 	unsigned long status, capabilities;
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 	status = __vmballoon_cmd(b, VMW_BALLOON_CMD_START, req_caps, 0,
525*4882a593Smuzhiyun 				 &capabilities);
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	switch (status) {
528*4882a593Smuzhiyun 	case VMW_BALLOON_SUCCESS_WITH_CAPABILITIES:
529*4882a593Smuzhiyun 		b->capabilities = capabilities;
530*4882a593Smuzhiyun 		break;
531*4882a593Smuzhiyun 	case VMW_BALLOON_SUCCESS:
532*4882a593Smuzhiyun 		b->capabilities = VMW_BALLOON_BASIC_CMDS;
533*4882a593Smuzhiyun 		break;
534*4882a593Smuzhiyun 	default:
535*4882a593Smuzhiyun 		return -EIO;
536*4882a593Smuzhiyun 	}
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	/*
539*4882a593Smuzhiyun 	 * 2MB pages are only supported with batching. If batching is for some
540*4882a593Smuzhiyun 	 * reason disabled, do not use 2MB pages, since otherwise the legacy
541*4882a593Smuzhiyun 	 * mechanism is used with 2MB pages, causing a failure.
542*4882a593Smuzhiyun 	 */
543*4882a593Smuzhiyun 	b->max_page_size = VMW_BALLOON_4K_PAGE;
544*4882a593Smuzhiyun 	if ((b->capabilities & VMW_BALLOON_BATCHED_2M_CMDS) &&
545*4882a593Smuzhiyun 	    (b->capabilities & VMW_BALLOON_BATCHED_CMDS))
546*4882a593Smuzhiyun 		b->max_page_size = VMW_BALLOON_2M_PAGE;
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	return 0;
550*4882a593Smuzhiyun }
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun /**
553*4882a593Smuzhiyun  * vmballoon_send_guest_id - communicate guest type to the host.
554*4882a593Smuzhiyun  *
555*4882a593Smuzhiyun  * @b: pointer to the balloon.
556*4882a593Smuzhiyun  *
557*4882a593Smuzhiyun  * Communicate guest type to the host so that it can adjust ballooning
558*4882a593Smuzhiyun  * algorithm to the one most appropriate for the guest. This command
559*4882a593Smuzhiyun  * is normally issued after sending "start" command and is part of
560*4882a593Smuzhiyun  * standard reset sequence.
561*4882a593Smuzhiyun  *
562*4882a593Smuzhiyun  * Return: zero on success or appropriate error code.
563*4882a593Smuzhiyun  */
vmballoon_send_guest_id(struct vmballoon * b)564*4882a593Smuzhiyun static int vmballoon_send_guest_id(struct vmballoon *b)
565*4882a593Smuzhiyun {
566*4882a593Smuzhiyun 	unsigned long status;
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 	status = vmballoon_cmd(b, VMW_BALLOON_CMD_GUEST_ID,
569*4882a593Smuzhiyun 			       VMW_BALLOON_GUEST_ID, 0);
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun 	return status == VMW_BALLOON_SUCCESS ? 0 : -EIO;
572*4882a593Smuzhiyun }
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun /**
575*4882a593Smuzhiyun  * vmballoon_page_order() - return the order of the page
576*4882a593Smuzhiyun  * @page_size: the size of the page.
577*4882a593Smuzhiyun  *
578*4882a593Smuzhiyun  * Return: the allocation order.
579*4882a593Smuzhiyun  */
580*4882a593Smuzhiyun static inline
vmballoon_page_order(enum vmballoon_page_size_type page_size)581*4882a593Smuzhiyun unsigned int vmballoon_page_order(enum vmballoon_page_size_type page_size)
582*4882a593Smuzhiyun {
583*4882a593Smuzhiyun 	return page_size == VMW_BALLOON_2M_PAGE ? VMW_BALLOON_2M_ORDER : 0;
584*4882a593Smuzhiyun }
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun /**
587*4882a593Smuzhiyun  * vmballoon_page_in_frames() - returns the number of frames in a page.
588*4882a593Smuzhiyun  * @page_size: the size of the page.
589*4882a593Smuzhiyun  *
590*4882a593Smuzhiyun  * Return: the number of 4k frames.
591*4882a593Smuzhiyun  */
592*4882a593Smuzhiyun static inline unsigned int
vmballoon_page_in_frames(enum vmballoon_page_size_type page_size)593*4882a593Smuzhiyun vmballoon_page_in_frames(enum vmballoon_page_size_type page_size)
594*4882a593Smuzhiyun {
595*4882a593Smuzhiyun 	return 1 << vmballoon_page_order(page_size);
596*4882a593Smuzhiyun }
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun /**
599*4882a593Smuzhiyun  * vmballoon_mark_page_offline() - mark a page as offline
600*4882a593Smuzhiyun  * @page: pointer for the page.
601*4882a593Smuzhiyun  * @page_size: the size of the page.
602*4882a593Smuzhiyun  */
603*4882a593Smuzhiyun static void
vmballoon_mark_page_offline(struct page * page,enum vmballoon_page_size_type page_size)604*4882a593Smuzhiyun vmballoon_mark_page_offline(struct page *page,
605*4882a593Smuzhiyun 			    enum vmballoon_page_size_type page_size)
606*4882a593Smuzhiyun {
607*4882a593Smuzhiyun 	int i;
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun 	for (i = 0; i < vmballoon_page_in_frames(page_size); i++)
610*4882a593Smuzhiyun 		__SetPageOffline(page + i);
611*4882a593Smuzhiyun }
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun /**
614*4882a593Smuzhiyun  * vmballoon_mark_page_online() - mark a page as online
615*4882a593Smuzhiyun  * @page: pointer for the page.
616*4882a593Smuzhiyun  * @page_size: the size of the page.
617*4882a593Smuzhiyun  */
618*4882a593Smuzhiyun static void
vmballoon_mark_page_online(struct page * page,enum vmballoon_page_size_type page_size)619*4882a593Smuzhiyun vmballoon_mark_page_online(struct page *page,
620*4882a593Smuzhiyun 			   enum vmballoon_page_size_type page_size)
621*4882a593Smuzhiyun {
622*4882a593Smuzhiyun 	int i;
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	for (i = 0; i < vmballoon_page_in_frames(page_size); i++)
625*4882a593Smuzhiyun 		__ClearPageOffline(page + i);
626*4882a593Smuzhiyun }
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun /**
629*4882a593Smuzhiyun  * vmballoon_send_get_target() - Retrieve desired balloon size from the host.
630*4882a593Smuzhiyun  *
631*4882a593Smuzhiyun  * @b: pointer to the balloon.
632*4882a593Smuzhiyun  *
633*4882a593Smuzhiyun  * Return: zero on success, EINVAL if limit does not fit in 32-bit, as required
634*4882a593Smuzhiyun  * by the host-guest protocol and EIO if an error occurred in communicating with
635*4882a593Smuzhiyun  * the host.
636*4882a593Smuzhiyun  */
vmballoon_send_get_target(struct vmballoon * b)637*4882a593Smuzhiyun static int vmballoon_send_get_target(struct vmballoon *b)
638*4882a593Smuzhiyun {
639*4882a593Smuzhiyun 	unsigned long status;
640*4882a593Smuzhiyun 	unsigned long limit;
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun 	limit = totalram_pages();
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 	/* Ensure limit fits in 32-bits if 64-bit targets are not supported */
645*4882a593Smuzhiyun 	if (!(b->capabilities & VMW_BALLOON_64_BIT_TARGET) &&
646*4882a593Smuzhiyun 	    limit != (u32)limit)
647*4882a593Smuzhiyun 		return -EINVAL;
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun 	status = vmballoon_cmd(b, VMW_BALLOON_CMD_GET_TARGET, limit, 0);
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	return status == VMW_BALLOON_SUCCESS ? 0 : -EIO;
652*4882a593Smuzhiyun }
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun /**
655*4882a593Smuzhiyun  * vmballoon_alloc_page_list - allocates a list of pages.
656*4882a593Smuzhiyun  *
657*4882a593Smuzhiyun  * @b: pointer to the balloon.
658*4882a593Smuzhiyun  * @ctl: pointer for the %struct vmballoon_ctl, which defines the operation.
659*4882a593Smuzhiyun  * @req_n_pages: the number of requested pages.
660*4882a593Smuzhiyun  *
661*4882a593Smuzhiyun  * Tries to allocate @req_n_pages. Add them to the list of balloon pages in
662*4882a593Smuzhiyun  * @ctl.pages and updates @ctl.n_pages to reflect the number of pages.
663*4882a593Smuzhiyun  *
664*4882a593Smuzhiyun  * Return: zero on success or error code otherwise.
665*4882a593Smuzhiyun  */
vmballoon_alloc_page_list(struct vmballoon * b,struct vmballoon_ctl * ctl,unsigned int req_n_pages)666*4882a593Smuzhiyun static int vmballoon_alloc_page_list(struct vmballoon *b,
667*4882a593Smuzhiyun 				     struct vmballoon_ctl *ctl,
668*4882a593Smuzhiyun 				     unsigned int req_n_pages)
669*4882a593Smuzhiyun {
670*4882a593Smuzhiyun 	struct page *page;
671*4882a593Smuzhiyun 	unsigned int i;
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 	for (i = 0; i < req_n_pages; i++) {
674*4882a593Smuzhiyun 		/*
675*4882a593Smuzhiyun 		 * First check if we happen to have pages that were allocated
676*4882a593Smuzhiyun 		 * before. This happens when 2MB page rejected during inflation
677*4882a593Smuzhiyun 		 * by the hypervisor, and then split into 4KB pages.
678*4882a593Smuzhiyun 		 */
679*4882a593Smuzhiyun 		if (!list_empty(&ctl->prealloc_pages)) {
680*4882a593Smuzhiyun 			page = list_first_entry(&ctl->prealloc_pages,
681*4882a593Smuzhiyun 						struct page, lru);
682*4882a593Smuzhiyun 			list_del(&page->lru);
683*4882a593Smuzhiyun 		} else {
684*4882a593Smuzhiyun 			if (ctl->page_size == VMW_BALLOON_2M_PAGE)
685*4882a593Smuzhiyun 				page = alloc_pages(__GFP_HIGHMEM|__GFP_NOWARN|
686*4882a593Smuzhiyun 					__GFP_NOMEMALLOC, VMW_BALLOON_2M_ORDER);
687*4882a593Smuzhiyun 			else
688*4882a593Smuzhiyun 				page = balloon_page_alloc();
689*4882a593Smuzhiyun 
690*4882a593Smuzhiyun 			vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_ALLOC,
691*4882a593Smuzhiyun 						 ctl->page_size);
692*4882a593Smuzhiyun 		}
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun 		if (page) {
695*4882a593Smuzhiyun 			/* Success. Add the page to the list and continue. */
696*4882a593Smuzhiyun 			list_add(&page->lru, &ctl->pages);
697*4882a593Smuzhiyun 			continue;
698*4882a593Smuzhiyun 		}
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun 		/* Allocation failed. Update statistics and stop. */
701*4882a593Smuzhiyun 		vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_ALLOC_FAIL,
702*4882a593Smuzhiyun 					 ctl->page_size);
703*4882a593Smuzhiyun 		break;
704*4882a593Smuzhiyun 	}
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun 	ctl->n_pages = i;
707*4882a593Smuzhiyun 
708*4882a593Smuzhiyun 	return req_n_pages == ctl->n_pages ? 0 : -ENOMEM;
709*4882a593Smuzhiyun }
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun /**
712*4882a593Smuzhiyun  * vmballoon_handle_one_result - Handle lock/unlock result for a single page.
713*4882a593Smuzhiyun  *
714*4882a593Smuzhiyun  * @b: pointer for %struct vmballoon.
715*4882a593Smuzhiyun  * @page: pointer for the page whose result should be handled.
716*4882a593Smuzhiyun  * @page_size: size of the page.
717*4882a593Smuzhiyun  * @status: status of the operation as provided by the hypervisor.
718*4882a593Smuzhiyun  */
vmballoon_handle_one_result(struct vmballoon * b,struct page * page,enum vmballoon_page_size_type page_size,unsigned long status)719*4882a593Smuzhiyun static int vmballoon_handle_one_result(struct vmballoon *b, struct page *page,
720*4882a593Smuzhiyun 				       enum vmballoon_page_size_type page_size,
721*4882a593Smuzhiyun 				       unsigned long status)
722*4882a593Smuzhiyun {
723*4882a593Smuzhiyun 	/* On success do nothing. The page is already on the balloon list. */
724*4882a593Smuzhiyun 	if (likely(status == VMW_BALLOON_SUCCESS))
725*4882a593Smuzhiyun 		return 0;
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	pr_debug("%s: failed comm pfn %lx status %lu page_size %s\n", __func__,
728*4882a593Smuzhiyun 		 page_to_pfn(page), status,
729*4882a593Smuzhiyun 		 vmballoon_page_size_names[page_size]);
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	/* Error occurred */
732*4882a593Smuzhiyun 	vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_REFUSED_ALLOC,
733*4882a593Smuzhiyun 				 page_size);
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	return -EIO;
736*4882a593Smuzhiyun }
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun /**
739*4882a593Smuzhiyun  * vmballoon_status_page - returns the status of (un)lock operation
740*4882a593Smuzhiyun  *
741*4882a593Smuzhiyun  * @b: pointer to the balloon.
742*4882a593Smuzhiyun  * @idx: index for the page for which the operation is performed.
743*4882a593Smuzhiyun  * @p: pointer to where the page struct is returned.
744*4882a593Smuzhiyun  *
745*4882a593Smuzhiyun  * Following a lock or unlock operation, returns the status of the operation for
746*4882a593Smuzhiyun  * an individual page. Provides the page that the operation was performed on on
747*4882a593Smuzhiyun  * the @page argument.
748*4882a593Smuzhiyun  *
749*4882a593Smuzhiyun  * Returns: The status of a lock or unlock operation for an individual page.
750*4882a593Smuzhiyun  */
vmballoon_status_page(struct vmballoon * b,int idx,struct page ** p)751*4882a593Smuzhiyun static unsigned long vmballoon_status_page(struct vmballoon *b, int idx,
752*4882a593Smuzhiyun 					   struct page **p)
753*4882a593Smuzhiyun {
754*4882a593Smuzhiyun 	if (static_branch_likely(&vmw_balloon_batching)) {
755*4882a593Smuzhiyun 		/* batching mode */
756*4882a593Smuzhiyun 		*p = pfn_to_page(b->batch_page[idx].pfn);
757*4882a593Smuzhiyun 		return b->batch_page[idx].status;
758*4882a593Smuzhiyun 	}
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun 	/* non-batching mode */
761*4882a593Smuzhiyun 	*p = b->page;
762*4882a593Smuzhiyun 
763*4882a593Smuzhiyun 	/*
764*4882a593Smuzhiyun 	 * If a failure occurs, the indication will be provided in the status
765*4882a593Smuzhiyun 	 * of the entire operation, which is considered before the individual
766*4882a593Smuzhiyun 	 * page status. So for non-batching mode, the indication is always of
767*4882a593Smuzhiyun 	 * success.
768*4882a593Smuzhiyun 	 */
769*4882a593Smuzhiyun 	return VMW_BALLOON_SUCCESS;
770*4882a593Smuzhiyun }
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun /**
773*4882a593Smuzhiyun  * vmballoon_lock_op - notifies the host about inflated/deflated pages.
774*4882a593Smuzhiyun  * @b: pointer to the balloon.
775*4882a593Smuzhiyun  * @num_pages: number of inflated/deflated pages.
776*4882a593Smuzhiyun  * @page_size: size of the page.
777*4882a593Smuzhiyun  * @op: the type of operation (lock or unlock).
778*4882a593Smuzhiyun  *
779*4882a593Smuzhiyun  * Notify the host about page(s) that were ballooned (or removed from the
780*4882a593Smuzhiyun  * balloon) so that host can use it without fear that guest will need it (or
781*4882a593Smuzhiyun  * stop using them since the VM does). Host may reject some pages, we need to
782*4882a593Smuzhiyun  * check the return value and maybe submit a different page. The pages that are
783*4882a593Smuzhiyun  * inflated/deflated are pointed by @b->page.
784*4882a593Smuzhiyun  *
785*4882a593Smuzhiyun  * Return: result as provided by the hypervisor.
786*4882a593Smuzhiyun  */
vmballoon_lock_op(struct vmballoon * b,unsigned int num_pages,enum vmballoon_page_size_type page_size,enum vmballoon_op op)787*4882a593Smuzhiyun static unsigned long vmballoon_lock_op(struct vmballoon *b,
788*4882a593Smuzhiyun 				       unsigned int num_pages,
789*4882a593Smuzhiyun 				       enum vmballoon_page_size_type page_size,
790*4882a593Smuzhiyun 				       enum vmballoon_op op)
791*4882a593Smuzhiyun {
792*4882a593Smuzhiyun 	unsigned long cmd, pfn;
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 	lockdep_assert_held(&b->comm_lock);
795*4882a593Smuzhiyun 
796*4882a593Smuzhiyun 	if (static_branch_likely(&vmw_balloon_batching)) {
797*4882a593Smuzhiyun 		if (op == VMW_BALLOON_INFLATE)
798*4882a593Smuzhiyun 			cmd = page_size == VMW_BALLOON_2M_PAGE ?
799*4882a593Smuzhiyun 				VMW_BALLOON_CMD_BATCHED_2M_LOCK :
800*4882a593Smuzhiyun 				VMW_BALLOON_CMD_BATCHED_LOCK;
801*4882a593Smuzhiyun 		else
802*4882a593Smuzhiyun 			cmd = page_size == VMW_BALLOON_2M_PAGE ?
803*4882a593Smuzhiyun 				VMW_BALLOON_CMD_BATCHED_2M_UNLOCK :
804*4882a593Smuzhiyun 				VMW_BALLOON_CMD_BATCHED_UNLOCK;
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun 		pfn = PHYS_PFN(virt_to_phys(b->batch_page));
807*4882a593Smuzhiyun 	} else {
808*4882a593Smuzhiyun 		cmd = op == VMW_BALLOON_INFLATE ? VMW_BALLOON_CMD_LOCK :
809*4882a593Smuzhiyun 						  VMW_BALLOON_CMD_UNLOCK;
810*4882a593Smuzhiyun 		pfn = page_to_pfn(b->page);
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 		/* In non-batching mode, PFNs must fit in 32-bit */
813*4882a593Smuzhiyun 		if (unlikely(pfn != (u32)pfn))
814*4882a593Smuzhiyun 			return VMW_BALLOON_ERROR_PPN_INVALID;
815*4882a593Smuzhiyun 	}
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 	return vmballoon_cmd(b, cmd, pfn, num_pages);
818*4882a593Smuzhiyun }
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun /**
821*4882a593Smuzhiyun  * vmballoon_add_page - adds a page towards lock/unlock operation.
822*4882a593Smuzhiyun  *
823*4882a593Smuzhiyun  * @b: pointer to the balloon.
824*4882a593Smuzhiyun  * @idx: index of the page to be ballooned in this batch.
825*4882a593Smuzhiyun  * @p: pointer to the page that is about to be ballooned.
826*4882a593Smuzhiyun  *
827*4882a593Smuzhiyun  * Adds the page to be ballooned. Must be called while holding @comm_lock.
828*4882a593Smuzhiyun  */
vmballoon_add_page(struct vmballoon * b,unsigned int idx,struct page * p)829*4882a593Smuzhiyun static void vmballoon_add_page(struct vmballoon *b, unsigned int idx,
830*4882a593Smuzhiyun 			       struct page *p)
831*4882a593Smuzhiyun {
832*4882a593Smuzhiyun 	lockdep_assert_held(&b->comm_lock);
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 	if (static_branch_likely(&vmw_balloon_batching))
835*4882a593Smuzhiyun 		b->batch_page[idx] = (struct vmballoon_batch_entry)
836*4882a593Smuzhiyun 					{ .pfn = page_to_pfn(p) };
837*4882a593Smuzhiyun 	else
838*4882a593Smuzhiyun 		b->page = p;
839*4882a593Smuzhiyun }
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun /**
842*4882a593Smuzhiyun  * vmballoon_lock - lock or unlock a batch of pages.
843*4882a593Smuzhiyun  *
844*4882a593Smuzhiyun  * @b: pointer to the balloon.
845*4882a593Smuzhiyun  * @ctl: pointer for the %struct vmballoon_ctl, which defines the operation.
846*4882a593Smuzhiyun  *
847*4882a593Smuzhiyun  * Notifies the host of about ballooned pages (after inflation or deflation,
848*4882a593Smuzhiyun  * according to @ctl). If the host rejects the page put it on the
849*4882a593Smuzhiyun  * @ctl refuse list. These refused page are then released when moving to the
850*4882a593Smuzhiyun  * next size of pages.
851*4882a593Smuzhiyun  *
852*4882a593Smuzhiyun  * Note that we neither free any @page here nor put them back on the ballooned
853*4882a593Smuzhiyun  * pages list. Instead we queue it for later processing. We do that for several
854*4882a593Smuzhiyun  * reasons. First, we do not want to free the page under the lock. Second, it
855*4882a593Smuzhiyun  * allows us to unify the handling of lock and unlock. In the inflate case, the
856*4882a593Smuzhiyun  * caller will check if there are too many refused pages and release them.
857*4882a593Smuzhiyun  * Although it is not identical to the past behavior, it should not affect
858*4882a593Smuzhiyun  * performance.
859*4882a593Smuzhiyun  */
vmballoon_lock(struct vmballoon * b,struct vmballoon_ctl * ctl)860*4882a593Smuzhiyun static int vmballoon_lock(struct vmballoon *b, struct vmballoon_ctl *ctl)
861*4882a593Smuzhiyun {
862*4882a593Smuzhiyun 	unsigned long batch_status;
863*4882a593Smuzhiyun 	struct page *page;
864*4882a593Smuzhiyun 	unsigned int i, num_pages;
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 	num_pages = ctl->n_pages;
867*4882a593Smuzhiyun 	if (num_pages == 0)
868*4882a593Smuzhiyun 		return 0;
869*4882a593Smuzhiyun 
870*4882a593Smuzhiyun 	/* communication with the host is done under the communication lock */
871*4882a593Smuzhiyun 	spin_lock(&b->comm_lock);
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun 	i = 0;
874*4882a593Smuzhiyun 	list_for_each_entry(page, &ctl->pages, lru)
875*4882a593Smuzhiyun 		vmballoon_add_page(b, i++, page);
876*4882a593Smuzhiyun 
877*4882a593Smuzhiyun 	batch_status = vmballoon_lock_op(b, ctl->n_pages, ctl->page_size,
878*4882a593Smuzhiyun 					 ctl->op);
879*4882a593Smuzhiyun 
880*4882a593Smuzhiyun 	/*
881*4882a593Smuzhiyun 	 * Iterate over the pages in the provided list. Since we are changing
882*4882a593Smuzhiyun 	 * @ctl->n_pages we are saving the original value in @num_pages and
883*4882a593Smuzhiyun 	 * use this value to bound the loop.
884*4882a593Smuzhiyun 	 */
885*4882a593Smuzhiyun 	for (i = 0; i < num_pages; i++) {
886*4882a593Smuzhiyun 		unsigned long status;
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun 		status = vmballoon_status_page(b, i, &page);
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun 		/*
891*4882a593Smuzhiyun 		 * Failure of the whole batch overrides a single operation
892*4882a593Smuzhiyun 		 * results.
893*4882a593Smuzhiyun 		 */
894*4882a593Smuzhiyun 		if (batch_status != VMW_BALLOON_SUCCESS)
895*4882a593Smuzhiyun 			status = batch_status;
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 		/* Continue if no error happened */
898*4882a593Smuzhiyun 		if (!vmballoon_handle_one_result(b, page, ctl->page_size,
899*4882a593Smuzhiyun 						 status))
900*4882a593Smuzhiyun 			continue;
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun 		/*
903*4882a593Smuzhiyun 		 * Error happened. Move the pages to the refused list and update
904*4882a593Smuzhiyun 		 * the pages number.
905*4882a593Smuzhiyun 		 */
906*4882a593Smuzhiyun 		list_move(&page->lru, &ctl->refused_pages);
907*4882a593Smuzhiyun 		ctl->n_pages--;
908*4882a593Smuzhiyun 		ctl->n_refused_pages++;
909*4882a593Smuzhiyun 	}
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun 	spin_unlock(&b->comm_lock);
912*4882a593Smuzhiyun 
913*4882a593Smuzhiyun 	return batch_status == VMW_BALLOON_SUCCESS ? 0 : -EIO;
914*4882a593Smuzhiyun }
915*4882a593Smuzhiyun 
916*4882a593Smuzhiyun /**
917*4882a593Smuzhiyun  * vmballoon_release_page_list() - Releases a page list
918*4882a593Smuzhiyun  *
919*4882a593Smuzhiyun  * @page_list: list of pages to release.
920*4882a593Smuzhiyun  * @n_pages: pointer to the number of pages.
921*4882a593Smuzhiyun  * @page_size: whether the pages in the list are 2MB (or else 4KB).
922*4882a593Smuzhiyun  *
923*4882a593Smuzhiyun  * Releases the list of pages and zeros the number of pages.
924*4882a593Smuzhiyun  */
vmballoon_release_page_list(struct list_head * page_list,int * n_pages,enum vmballoon_page_size_type page_size)925*4882a593Smuzhiyun static void vmballoon_release_page_list(struct list_head *page_list,
926*4882a593Smuzhiyun 				       int *n_pages,
927*4882a593Smuzhiyun 				       enum vmballoon_page_size_type page_size)
928*4882a593Smuzhiyun {
929*4882a593Smuzhiyun 	struct page *page, *tmp;
930*4882a593Smuzhiyun 
931*4882a593Smuzhiyun 	list_for_each_entry_safe(page, tmp, page_list, lru) {
932*4882a593Smuzhiyun 		list_del(&page->lru);
933*4882a593Smuzhiyun 		__free_pages(page, vmballoon_page_order(page_size));
934*4882a593Smuzhiyun 	}
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 	if (n_pages)
937*4882a593Smuzhiyun 		*n_pages = 0;
938*4882a593Smuzhiyun }
939*4882a593Smuzhiyun 
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun /*
942*4882a593Smuzhiyun  * Release pages that were allocated while attempting to inflate the
943*4882a593Smuzhiyun  * balloon but were refused by the host for one reason or another.
944*4882a593Smuzhiyun  */
vmballoon_release_refused_pages(struct vmballoon * b,struct vmballoon_ctl * ctl)945*4882a593Smuzhiyun static void vmballoon_release_refused_pages(struct vmballoon *b,
946*4882a593Smuzhiyun 					    struct vmballoon_ctl *ctl)
947*4882a593Smuzhiyun {
948*4882a593Smuzhiyun 	vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_REFUSED_FREE,
949*4882a593Smuzhiyun 				 ctl->page_size);
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun 	vmballoon_release_page_list(&ctl->refused_pages, &ctl->n_refused_pages,
952*4882a593Smuzhiyun 				    ctl->page_size);
953*4882a593Smuzhiyun }
954*4882a593Smuzhiyun 
955*4882a593Smuzhiyun /**
956*4882a593Smuzhiyun  * vmballoon_change - retrieve the required balloon change
957*4882a593Smuzhiyun  *
958*4882a593Smuzhiyun  * @b: pointer for the balloon.
959*4882a593Smuzhiyun  *
960*4882a593Smuzhiyun  * Return: the required change for the balloon size. A positive number
961*4882a593Smuzhiyun  * indicates inflation, a negative number indicates a deflation.
962*4882a593Smuzhiyun  */
vmballoon_change(struct vmballoon * b)963*4882a593Smuzhiyun static int64_t vmballoon_change(struct vmballoon *b)
964*4882a593Smuzhiyun {
965*4882a593Smuzhiyun 	int64_t size, target;
966*4882a593Smuzhiyun 
967*4882a593Smuzhiyun 	size = atomic64_read(&b->size);
968*4882a593Smuzhiyun 	target = READ_ONCE(b->target);
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun 	/*
971*4882a593Smuzhiyun 	 * We must cast first because of int sizes
972*4882a593Smuzhiyun 	 * Otherwise we might get huge positives instead of negatives
973*4882a593Smuzhiyun 	 */
974*4882a593Smuzhiyun 
975*4882a593Smuzhiyun 	if (b->reset_required)
976*4882a593Smuzhiyun 		return 0;
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun 	/* consider a 2MB slack on deflate, unless the balloon is emptied */
979*4882a593Smuzhiyun 	if (target < size && target != 0 &&
980*4882a593Smuzhiyun 	    size - target < vmballoon_page_in_frames(VMW_BALLOON_2M_PAGE))
981*4882a593Smuzhiyun 		return 0;
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun 	/* If an out-of-memory recently occurred, inflation is disallowed. */
984*4882a593Smuzhiyun 	if (target > size && time_before(jiffies, READ_ONCE(b->shrink_timeout)))
985*4882a593Smuzhiyun 		return 0;
986*4882a593Smuzhiyun 
987*4882a593Smuzhiyun 	return target - size;
988*4882a593Smuzhiyun }
989*4882a593Smuzhiyun 
990*4882a593Smuzhiyun /**
991*4882a593Smuzhiyun  * vmballoon_enqueue_page_list() - Enqueues list of pages after inflation.
992*4882a593Smuzhiyun  *
993*4882a593Smuzhiyun  * @b: pointer to balloon.
994*4882a593Smuzhiyun  * @pages: list of pages to enqueue.
995*4882a593Smuzhiyun  * @n_pages: pointer to number of pages in list. The value is zeroed.
996*4882a593Smuzhiyun  * @page_size: whether the pages are 2MB or 4KB pages.
997*4882a593Smuzhiyun  *
998*4882a593Smuzhiyun  * Enqueues the provides list of pages in the ballooned page list, clears the
999*4882a593Smuzhiyun  * list and zeroes the number of pages that was provided.
1000*4882a593Smuzhiyun  */
vmballoon_enqueue_page_list(struct vmballoon * b,struct list_head * pages,unsigned int * n_pages,enum vmballoon_page_size_type page_size)1001*4882a593Smuzhiyun static void vmballoon_enqueue_page_list(struct vmballoon *b,
1002*4882a593Smuzhiyun 					struct list_head *pages,
1003*4882a593Smuzhiyun 					unsigned int *n_pages,
1004*4882a593Smuzhiyun 					enum vmballoon_page_size_type page_size)
1005*4882a593Smuzhiyun {
1006*4882a593Smuzhiyun 	unsigned long flags;
1007*4882a593Smuzhiyun 	struct page *page;
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun 	if (page_size == VMW_BALLOON_4K_PAGE) {
1010*4882a593Smuzhiyun 		balloon_page_list_enqueue(&b->b_dev_info, pages);
1011*4882a593Smuzhiyun 	} else {
1012*4882a593Smuzhiyun 		/*
1013*4882a593Smuzhiyun 		 * Keep the huge pages in a local list which is not available
1014*4882a593Smuzhiyun 		 * for the balloon compaction mechanism.
1015*4882a593Smuzhiyun 		 */
1016*4882a593Smuzhiyun 		spin_lock_irqsave(&b->b_dev_info.pages_lock, flags);
1017*4882a593Smuzhiyun 
1018*4882a593Smuzhiyun 		list_for_each_entry(page, pages, lru) {
1019*4882a593Smuzhiyun 			vmballoon_mark_page_offline(page, VMW_BALLOON_2M_PAGE);
1020*4882a593Smuzhiyun 		}
1021*4882a593Smuzhiyun 
1022*4882a593Smuzhiyun 		list_splice_init(pages, &b->huge_pages);
1023*4882a593Smuzhiyun 		__count_vm_events(BALLOON_INFLATE, *n_pages *
1024*4882a593Smuzhiyun 				  vmballoon_page_in_frames(VMW_BALLOON_2M_PAGE));
1025*4882a593Smuzhiyun 		spin_unlock_irqrestore(&b->b_dev_info.pages_lock, flags);
1026*4882a593Smuzhiyun 	}
1027*4882a593Smuzhiyun 
1028*4882a593Smuzhiyun 	*n_pages = 0;
1029*4882a593Smuzhiyun }
1030*4882a593Smuzhiyun 
1031*4882a593Smuzhiyun /**
1032*4882a593Smuzhiyun  * vmballoon_dequeue_page_list() - Dequeues page lists for deflation.
1033*4882a593Smuzhiyun  *
1034*4882a593Smuzhiyun  * @b: pointer to balloon.
1035*4882a593Smuzhiyun  * @pages: list of pages to enqueue.
1036*4882a593Smuzhiyun  * @n_pages: pointer to number of pages in list. The value is zeroed.
1037*4882a593Smuzhiyun  * @page_size: whether the pages are 2MB or 4KB pages.
1038*4882a593Smuzhiyun  * @n_req_pages: the number of requested pages.
1039*4882a593Smuzhiyun  *
1040*4882a593Smuzhiyun  * Dequeues the number of requested pages from the balloon for deflation. The
1041*4882a593Smuzhiyun  * number of dequeued pages may be lower, if not enough pages in the requested
1042*4882a593Smuzhiyun  * size are available.
1043*4882a593Smuzhiyun  */
vmballoon_dequeue_page_list(struct vmballoon * b,struct list_head * pages,unsigned int * n_pages,enum vmballoon_page_size_type page_size,unsigned int n_req_pages)1044*4882a593Smuzhiyun static void vmballoon_dequeue_page_list(struct vmballoon *b,
1045*4882a593Smuzhiyun 					struct list_head *pages,
1046*4882a593Smuzhiyun 					unsigned int *n_pages,
1047*4882a593Smuzhiyun 					enum vmballoon_page_size_type page_size,
1048*4882a593Smuzhiyun 					unsigned int n_req_pages)
1049*4882a593Smuzhiyun {
1050*4882a593Smuzhiyun 	struct page *page, *tmp;
1051*4882a593Smuzhiyun 	unsigned int i = 0;
1052*4882a593Smuzhiyun 	unsigned long flags;
1053*4882a593Smuzhiyun 
1054*4882a593Smuzhiyun 	/* In the case of 4k pages, use the compaction infrastructure */
1055*4882a593Smuzhiyun 	if (page_size == VMW_BALLOON_4K_PAGE) {
1056*4882a593Smuzhiyun 		*n_pages = balloon_page_list_dequeue(&b->b_dev_info, pages,
1057*4882a593Smuzhiyun 						     n_req_pages);
1058*4882a593Smuzhiyun 		return;
1059*4882a593Smuzhiyun 	}
1060*4882a593Smuzhiyun 
1061*4882a593Smuzhiyun 	/* 2MB pages */
1062*4882a593Smuzhiyun 	spin_lock_irqsave(&b->b_dev_info.pages_lock, flags);
1063*4882a593Smuzhiyun 	list_for_each_entry_safe(page, tmp, &b->huge_pages, lru) {
1064*4882a593Smuzhiyun 		vmballoon_mark_page_online(page, VMW_BALLOON_2M_PAGE);
1065*4882a593Smuzhiyun 
1066*4882a593Smuzhiyun 		list_move(&page->lru, pages);
1067*4882a593Smuzhiyun 		if (++i == n_req_pages)
1068*4882a593Smuzhiyun 			break;
1069*4882a593Smuzhiyun 	}
1070*4882a593Smuzhiyun 
1071*4882a593Smuzhiyun 	__count_vm_events(BALLOON_DEFLATE,
1072*4882a593Smuzhiyun 			  i * vmballoon_page_in_frames(VMW_BALLOON_2M_PAGE));
1073*4882a593Smuzhiyun 	spin_unlock_irqrestore(&b->b_dev_info.pages_lock, flags);
1074*4882a593Smuzhiyun 	*n_pages = i;
1075*4882a593Smuzhiyun }
1076*4882a593Smuzhiyun 
1077*4882a593Smuzhiyun /**
1078*4882a593Smuzhiyun  * vmballoon_split_refused_pages() - Split the 2MB refused pages to 4k.
1079*4882a593Smuzhiyun  *
1080*4882a593Smuzhiyun  * If inflation of 2MB pages was denied by the hypervisor, it is likely to be
1081*4882a593Smuzhiyun  * due to one or few 4KB pages. These 2MB pages may keep being allocated and
1082*4882a593Smuzhiyun  * then being refused. To prevent this case, this function splits the refused
1083*4882a593Smuzhiyun  * pages into 4KB pages and adds them into @prealloc_pages list.
1084*4882a593Smuzhiyun  *
1085*4882a593Smuzhiyun  * @ctl: pointer for the %struct vmballoon_ctl, which defines the operation.
1086*4882a593Smuzhiyun  */
vmballoon_split_refused_pages(struct vmballoon_ctl * ctl)1087*4882a593Smuzhiyun static void vmballoon_split_refused_pages(struct vmballoon_ctl *ctl)
1088*4882a593Smuzhiyun {
1089*4882a593Smuzhiyun 	struct page *page, *tmp;
1090*4882a593Smuzhiyun 	unsigned int i, order;
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun 	order = vmballoon_page_order(ctl->page_size);
1093*4882a593Smuzhiyun 
1094*4882a593Smuzhiyun 	list_for_each_entry_safe(page, tmp, &ctl->refused_pages, lru) {
1095*4882a593Smuzhiyun 		list_del(&page->lru);
1096*4882a593Smuzhiyun 		split_page(page, order);
1097*4882a593Smuzhiyun 		for (i = 0; i < (1 << order); i++)
1098*4882a593Smuzhiyun 			list_add(&page[i].lru, &ctl->prealloc_pages);
1099*4882a593Smuzhiyun 	}
1100*4882a593Smuzhiyun 	ctl->n_refused_pages = 0;
1101*4882a593Smuzhiyun }
1102*4882a593Smuzhiyun 
1103*4882a593Smuzhiyun /**
1104*4882a593Smuzhiyun  * vmballoon_inflate() - Inflate the balloon towards its target size.
1105*4882a593Smuzhiyun  *
1106*4882a593Smuzhiyun  * @b: pointer to the balloon.
1107*4882a593Smuzhiyun  */
vmballoon_inflate(struct vmballoon * b)1108*4882a593Smuzhiyun static void vmballoon_inflate(struct vmballoon *b)
1109*4882a593Smuzhiyun {
1110*4882a593Smuzhiyun 	int64_t to_inflate_frames;
1111*4882a593Smuzhiyun 	struct vmballoon_ctl ctl = {
1112*4882a593Smuzhiyun 		.pages = LIST_HEAD_INIT(ctl.pages),
1113*4882a593Smuzhiyun 		.refused_pages = LIST_HEAD_INIT(ctl.refused_pages),
1114*4882a593Smuzhiyun 		.prealloc_pages = LIST_HEAD_INIT(ctl.prealloc_pages),
1115*4882a593Smuzhiyun 		.page_size = b->max_page_size,
1116*4882a593Smuzhiyun 		.op = VMW_BALLOON_INFLATE
1117*4882a593Smuzhiyun 	};
1118*4882a593Smuzhiyun 
1119*4882a593Smuzhiyun 	while ((to_inflate_frames = vmballoon_change(b)) > 0) {
1120*4882a593Smuzhiyun 		unsigned int to_inflate_pages, page_in_frames;
1121*4882a593Smuzhiyun 		int alloc_error, lock_error = 0;
1122*4882a593Smuzhiyun 
1123*4882a593Smuzhiyun 		VM_BUG_ON(!list_empty(&ctl.pages));
1124*4882a593Smuzhiyun 		VM_BUG_ON(ctl.n_pages != 0);
1125*4882a593Smuzhiyun 
1126*4882a593Smuzhiyun 		page_in_frames = vmballoon_page_in_frames(ctl.page_size);
1127*4882a593Smuzhiyun 
1128*4882a593Smuzhiyun 		to_inflate_pages = min_t(unsigned long, b->batch_max_pages,
1129*4882a593Smuzhiyun 					 DIV_ROUND_UP_ULL(to_inflate_frames,
1130*4882a593Smuzhiyun 							  page_in_frames));
1131*4882a593Smuzhiyun 
1132*4882a593Smuzhiyun 		/* Start by allocating */
1133*4882a593Smuzhiyun 		alloc_error = vmballoon_alloc_page_list(b, &ctl,
1134*4882a593Smuzhiyun 							to_inflate_pages);
1135*4882a593Smuzhiyun 
1136*4882a593Smuzhiyun 		/* Actually lock the pages by telling the hypervisor */
1137*4882a593Smuzhiyun 		lock_error = vmballoon_lock(b, &ctl);
1138*4882a593Smuzhiyun 
1139*4882a593Smuzhiyun 		/*
1140*4882a593Smuzhiyun 		 * If an error indicates that something serious went wrong,
1141*4882a593Smuzhiyun 		 * stop the inflation.
1142*4882a593Smuzhiyun 		 */
1143*4882a593Smuzhiyun 		if (lock_error)
1144*4882a593Smuzhiyun 			break;
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun 		/* Update the balloon size */
1147*4882a593Smuzhiyun 		atomic64_add(ctl.n_pages * page_in_frames, &b->size);
1148*4882a593Smuzhiyun 
1149*4882a593Smuzhiyun 		vmballoon_enqueue_page_list(b, &ctl.pages, &ctl.n_pages,
1150*4882a593Smuzhiyun 					    ctl.page_size);
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun 		/*
1153*4882a593Smuzhiyun 		 * If allocation failed or the number of refused pages exceeds
1154*4882a593Smuzhiyun 		 * the maximum allowed, move to the next page size.
1155*4882a593Smuzhiyun 		 */
1156*4882a593Smuzhiyun 		if (alloc_error ||
1157*4882a593Smuzhiyun 		    ctl.n_refused_pages >= VMW_BALLOON_MAX_REFUSED) {
1158*4882a593Smuzhiyun 			if (ctl.page_size == VMW_BALLOON_4K_PAGE)
1159*4882a593Smuzhiyun 				break;
1160*4882a593Smuzhiyun 
1161*4882a593Smuzhiyun 			/*
1162*4882a593Smuzhiyun 			 * Split the refused pages to 4k. This will also empty
1163*4882a593Smuzhiyun 			 * the refused pages list.
1164*4882a593Smuzhiyun 			 */
1165*4882a593Smuzhiyun 			vmballoon_split_refused_pages(&ctl);
1166*4882a593Smuzhiyun 			ctl.page_size--;
1167*4882a593Smuzhiyun 		}
1168*4882a593Smuzhiyun 
1169*4882a593Smuzhiyun 		cond_resched();
1170*4882a593Smuzhiyun 	}
1171*4882a593Smuzhiyun 
1172*4882a593Smuzhiyun 	/*
1173*4882a593Smuzhiyun 	 * Release pages that were allocated while attempting to inflate the
1174*4882a593Smuzhiyun 	 * balloon but were refused by the host for one reason or another,
1175*4882a593Smuzhiyun 	 * and update the statistics.
1176*4882a593Smuzhiyun 	 */
1177*4882a593Smuzhiyun 	if (ctl.n_refused_pages != 0)
1178*4882a593Smuzhiyun 		vmballoon_release_refused_pages(b, &ctl);
1179*4882a593Smuzhiyun 
1180*4882a593Smuzhiyun 	vmballoon_release_page_list(&ctl.prealloc_pages, NULL, ctl.page_size);
1181*4882a593Smuzhiyun }
1182*4882a593Smuzhiyun 
1183*4882a593Smuzhiyun /**
1184*4882a593Smuzhiyun  * vmballoon_deflate() - Decrease the size of the balloon.
1185*4882a593Smuzhiyun  *
1186*4882a593Smuzhiyun  * @b: pointer to the balloon
1187*4882a593Smuzhiyun  * @n_frames: the number of frames to deflate. If zero, automatically
1188*4882a593Smuzhiyun  * calculated according to the target size.
1189*4882a593Smuzhiyun  * @coordinated: whether to coordinate with the host
1190*4882a593Smuzhiyun  *
1191*4882a593Smuzhiyun  * Decrease the size of the balloon allowing guest to use more memory.
1192*4882a593Smuzhiyun  *
1193*4882a593Smuzhiyun  * Return: The number of deflated frames (i.e., basic page size units)
1194*4882a593Smuzhiyun  */
vmballoon_deflate(struct vmballoon * b,uint64_t n_frames,bool coordinated)1195*4882a593Smuzhiyun static unsigned long vmballoon_deflate(struct vmballoon *b, uint64_t n_frames,
1196*4882a593Smuzhiyun 				       bool coordinated)
1197*4882a593Smuzhiyun {
1198*4882a593Smuzhiyun 	unsigned long deflated_frames = 0;
1199*4882a593Smuzhiyun 	unsigned long tried_frames = 0;
1200*4882a593Smuzhiyun 	struct vmballoon_ctl ctl = {
1201*4882a593Smuzhiyun 		.pages = LIST_HEAD_INIT(ctl.pages),
1202*4882a593Smuzhiyun 		.refused_pages = LIST_HEAD_INIT(ctl.refused_pages),
1203*4882a593Smuzhiyun 		.page_size = VMW_BALLOON_4K_PAGE,
1204*4882a593Smuzhiyun 		.op = VMW_BALLOON_DEFLATE
1205*4882a593Smuzhiyun 	};
1206*4882a593Smuzhiyun 
1207*4882a593Smuzhiyun 	/* free pages to reach target */
1208*4882a593Smuzhiyun 	while (true) {
1209*4882a593Smuzhiyun 		unsigned int to_deflate_pages, n_unlocked_frames;
1210*4882a593Smuzhiyun 		unsigned int page_in_frames;
1211*4882a593Smuzhiyun 		int64_t to_deflate_frames;
1212*4882a593Smuzhiyun 		bool deflated_all;
1213*4882a593Smuzhiyun 
1214*4882a593Smuzhiyun 		page_in_frames = vmballoon_page_in_frames(ctl.page_size);
1215*4882a593Smuzhiyun 
1216*4882a593Smuzhiyun 		VM_BUG_ON(!list_empty(&ctl.pages));
1217*4882a593Smuzhiyun 		VM_BUG_ON(ctl.n_pages);
1218*4882a593Smuzhiyun 		VM_BUG_ON(!list_empty(&ctl.refused_pages));
1219*4882a593Smuzhiyun 		VM_BUG_ON(ctl.n_refused_pages);
1220*4882a593Smuzhiyun 
1221*4882a593Smuzhiyun 		/*
1222*4882a593Smuzhiyun 		 * If we were requested a specific number of frames, we try to
1223*4882a593Smuzhiyun 		 * deflate this number of frames. Otherwise, deflation is
1224*4882a593Smuzhiyun 		 * performed according to the target and balloon size.
1225*4882a593Smuzhiyun 		 */
1226*4882a593Smuzhiyun 		to_deflate_frames = n_frames ? n_frames - tried_frames :
1227*4882a593Smuzhiyun 					       -vmballoon_change(b);
1228*4882a593Smuzhiyun 
1229*4882a593Smuzhiyun 		/* break if no work to do */
1230*4882a593Smuzhiyun 		if (to_deflate_frames <= 0)
1231*4882a593Smuzhiyun 			break;
1232*4882a593Smuzhiyun 
1233*4882a593Smuzhiyun 		/*
1234*4882a593Smuzhiyun 		 * Calculate the number of frames based on current page size,
1235*4882a593Smuzhiyun 		 * but limit the deflated frames to a single chunk
1236*4882a593Smuzhiyun 		 */
1237*4882a593Smuzhiyun 		to_deflate_pages = min_t(unsigned long, b->batch_max_pages,
1238*4882a593Smuzhiyun 					 DIV_ROUND_UP_ULL(to_deflate_frames,
1239*4882a593Smuzhiyun 							  page_in_frames));
1240*4882a593Smuzhiyun 
1241*4882a593Smuzhiyun 		/* First take the pages from the balloon pages. */
1242*4882a593Smuzhiyun 		vmballoon_dequeue_page_list(b, &ctl.pages, &ctl.n_pages,
1243*4882a593Smuzhiyun 					    ctl.page_size, to_deflate_pages);
1244*4882a593Smuzhiyun 
1245*4882a593Smuzhiyun 		/*
1246*4882a593Smuzhiyun 		 * Before pages are moving to the refused list, count their
1247*4882a593Smuzhiyun 		 * frames as frames that we tried to deflate.
1248*4882a593Smuzhiyun 		 */
1249*4882a593Smuzhiyun 		tried_frames += ctl.n_pages * page_in_frames;
1250*4882a593Smuzhiyun 
1251*4882a593Smuzhiyun 		/*
1252*4882a593Smuzhiyun 		 * Unlock the pages by communicating with the hypervisor if the
1253*4882a593Smuzhiyun 		 * communication is coordinated (i.e., not pop). We ignore the
1254*4882a593Smuzhiyun 		 * return code. Instead we check if all the pages we manage to
1255*4882a593Smuzhiyun 		 * unlock all the pages. If we failed, we will move to the next
1256*4882a593Smuzhiyun 		 * page size, and would eventually try again later.
1257*4882a593Smuzhiyun 		 */
1258*4882a593Smuzhiyun 		if (coordinated)
1259*4882a593Smuzhiyun 			vmballoon_lock(b, &ctl);
1260*4882a593Smuzhiyun 
1261*4882a593Smuzhiyun 		/*
1262*4882a593Smuzhiyun 		 * Check if we deflated enough. We will move to the next page
1263*4882a593Smuzhiyun 		 * size if we did not manage to do so. This calculation takes
1264*4882a593Smuzhiyun 		 * place now, as once the pages are released, the number of
1265*4882a593Smuzhiyun 		 * pages is zeroed.
1266*4882a593Smuzhiyun 		 */
1267*4882a593Smuzhiyun 		deflated_all = (ctl.n_pages == to_deflate_pages);
1268*4882a593Smuzhiyun 
1269*4882a593Smuzhiyun 		/* Update local and global counters */
1270*4882a593Smuzhiyun 		n_unlocked_frames = ctl.n_pages * page_in_frames;
1271*4882a593Smuzhiyun 		atomic64_sub(n_unlocked_frames, &b->size);
1272*4882a593Smuzhiyun 		deflated_frames += n_unlocked_frames;
1273*4882a593Smuzhiyun 
1274*4882a593Smuzhiyun 		vmballoon_stats_page_add(b, VMW_BALLOON_PAGE_STAT_FREE,
1275*4882a593Smuzhiyun 					 ctl.page_size, ctl.n_pages);
1276*4882a593Smuzhiyun 
1277*4882a593Smuzhiyun 		/* free the ballooned pages */
1278*4882a593Smuzhiyun 		vmballoon_release_page_list(&ctl.pages, &ctl.n_pages,
1279*4882a593Smuzhiyun 					    ctl.page_size);
1280*4882a593Smuzhiyun 
1281*4882a593Smuzhiyun 		/* Return the refused pages to the ballooned list. */
1282*4882a593Smuzhiyun 		vmballoon_enqueue_page_list(b, &ctl.refused_pages,
1283*4882a593Smuzhiyun 					    &ctl.n_refused_pages,
1284*4882a593Smuzhiyun 					    ctl.page_size);
1285*4882a593Smuzhiyun 
1286*4882a593Smuzhiyun 		/* If we failed to unlock all the pages, move to next size. */
1287*4882a593Smuzhiyun 		if (!deflated_all) {
1288*4882a593Smuzhiyun 			if (ctl.page_size == b->max_page_size)
1289*4882a593Smuzhiyun 				break;
1290*4882a593Smuzhiyun 			ctl.page_size++;
1291*4882a593Smuzhiyun 		}
1292*4882a593Smuzhiyun 
1293*4882a593Smuzhiyun 		cond_resched();
1294*4882a593Smuzhiyun 	}
1295*4882a593Smuzhiyun 
1296*4882a593Smuzhiyun 	return deflated_frames;
1297*4882a593Smuzhiyun }
1298*4882a593Smuzhiyun 
1299*4882a593Smuzhiyun /**
1300*4882a593Smuzhiyun  * vmballoon_deinit_batching - disables batching mode.
1301*4882a593Smuzhiyun  *
1302*4882a593Smuzhiyun  * @b: pointer to &struct vmballoon.
1303*4882a593Smuzhiyun  *
1304*4882a593Smuzhiyun  * Disables batching, by deallocating the page for communication with the
1305*4882a593Smuzhiyun  * hypervisor and disabling the static key to indicate that batching is off.
1306*4882a593Smuzhiyun  */
vmballoon_deinit_batching(struct vmballoon * b)1307*4882a593Smuzhiyun static void vmballoon_deinit_batching(struct vmballoon *b)
1308*4882a593Smuzhiyun {
1309*4882a593Smuzhiyun 	free_page((unsigned long)b->batch_page);
1310*4882a593Smuzhiyun 	b->batch_page = NULL;
1311*4882a593Smuzhiyun 	static_branch_disable(&vmw_balloon_batching);
1312*4882a593Smuzhiyun 	b->batch_max_pages = 1;
1313*4882a593Smuzhiyun }
1314*4882a593Smuzhiyun 
1315*4882a593Smuzhiyun /**
1316*4882a593Smuzhiyun  * vmballoon_init_batching - enable batching mode.
1317*4882a593Smuzhiyun  *
1318*4882a593Smuzhiyun  * @b: pointer to &struct vmballoon.
1319*4882a593Smuzhiyun  *
1320*4882a593Smuzhiyun  * Enables batching, by allocating a page for communication with the hypervisor
1321*4882a593Smuzhiyun  * and enabling the static_key to use batching.
1322*4882a593Smuzhiyun  *
1323*4882a593Smuzhiyun  * Return: zero on success or an appropriate error-code.
1324*4882a593Smuzhiyun  */
vmballoon_init_batching(struct vmballoon * b)1325*4882a593Smuzhiyun static int vmballoon_init_batching(struct vmballoon *b)
1326*4882a593Smuzhiyun {
1327*4882a593Smuzhiyun 	struct page *page;
1328*4882a593Smuzhiyun 
1329*4882a593Smuzhiyun 	page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1330*4882a593Smuzhiyun 	if (!page)
1331*4882a593Smuzhiyun 		return -ENOMEM;
1332*4882a593Smuzhiyun 
1333*4882a593Smuzhiyun 	b->batch_page = page_address(page);
1334*4882a593Smuzhiyun 	b->batch_max_pages = PAGE_SIZE / sizeof(struct vmballoon_batch_entry);
1335*4882a593Smuzhiyun 
1336*4882a593Smuzhiyun 	static_branch_enable(&vmw_balloon_batching);
1337*4882a593Smuzhiyun 
1338*4882a593Smuzhiyun 	return 0;
1339*4882a593Smuzhiyun }
1340*4882a593Smuzhiyun 
1341*4882a593Smuzhiyun /*
1342*4882a593Smuzhiyun  * Receive notification and resize balloon
1343*4882a593Smuzhiyun  */
vmballoon_doorbell(void * client_data)1344*4882a593Smuzhiyun static void vmballoon_doorbell(void *client_data)
1345*4882a593Smuzhiyun {
1346*4882a593Smuzhiyun 	struct vmballoon *b = client_data;
1347*4882a593Smuzhiyun 
1348*4882a593Smuzhiyun 	vmballoon_stats_gen_inc(b, VMW_BALLOON_STAT_DOORBELL);
1349*4882a593Smuzhiyun 
1350*4882a593Smuzhiyun 	mod_delayed_work(system_freezable_wq, &b->dwork, 0);
1351*4882a593Smuzhiyun }
1352*4882a593Smuzhiyun 
1353*4882a593Smuzhiyun /*
1354*4882a593Smuzhiyun  * Clean up vmci doorbell
1355*4882a593Smuzhiyun  */
vmballoon_vmci_cleanup(struct vmballoon * b)1356*4882a593Smuzhiyun static void vmballoon_vmci_cleanup(struct vmballoon *b)
1357*4882a593Smuzhiyun {
1358*4882a593Smuzhiyun 	vmballoon_cmd(b, VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
1359*4882a593Smuzhiyun 		      VMCI_INVALID_ID, VMCI_INVALID_ID);
1360*4882a593Smuzhiyun 
1361*4882a593Smuzhiyun 	if (!vmci_handle_is_invalid(b->vmci_doorbell)) {
1362*4882a593Smuzhiyun 		vmci_doorbell_destroy(b->vmci_doorbell);
1363*4882a593Smuzhiyun 		b->vmci_doorbell = VMCI_INVALID_HANDLE;
1364*4882a593Smuzhiyun 	}
1365*4882a593Smuzhiyun }
1366*4882a593Smuzhiyun 
1367*4882a593Smuzhiyun /**
1368*4882a593Smuzhiyun  * vmballoon_vmci_init - Initialize vmci doorbell.
1369*4882a593Smuzhiyun  *
1370*4882a593Smuzhiyun  * @b: pointer to the balloon.
1371*4882a593Smuzhiyun  *
1372*4882a593Smuzhiyun  * Return: zero on success or when wakeup command not supported. Error-code
1373*4882a593Smuzhiyun  * otherwise.
1374*4882a593Smuzhiyun  *
1375*4882a593Smuzhiyun  * Initialize vmci doorbell, to get notified as soon as balloon changes.
1376*4882a593Smuzhiyun  */
vmballoon_vmci_init(struct vmballoon * b)1377*4882a593Smuzhiyun static int vmballoon_vmci_init(struct vmballoon *b)
1378*4882a593Smuzhiyun {
1379*4882a593Smuzhiyun 	unsigned long error;
1380*4882a593Smuzhiyun 
1381*4882a593Smuzhiyun 	if ((b->capabilities & VMW_BALLOON_SIGNALLED_WAKEUP_CMD) == 0)
1382*4882a593Smuzhiyun 		return 0;
1383*4882a593Smuzhiyun 
1384*4882a593Smuzhiyun 	error = vmci_doorbell_create(&b->vmci_doorbell, VMCI_FLAG_DELAYED_CB,
1385*4882a593Smuzhiyun 				     VMCI_PRIVILEGE_FLAG_RESTRICTED,
1386*4882a593Smuzhiyun 				     vmballoon_doorbell, b);
1387*4882a593Smuzhiyun 
1388*4882a593Smuzhiyun 	if (error != VMCI_SUCCESS)
1389*4882a593Smuzhiyun 		goto fail;
1390*4882a593Smuzhiyun 
1391*4882a593Smuzhiyun 	error =	__vmballoon_cmd(b, VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
1392*4882a593Smuzhiyun 				b->vmci_doorbell.context,
1393*4882a593Smuzhiyun 				b->vmci_doorbell.resource, NULL);
1394*4882a593Smuzhiyun 
1395*4882a593Smuzhiyun 	if (error != VMW_BALLOON_SUCCESS)
1396*4882a593Smuzhiyun 		goto fail;
1397*4882a593Smuzhiyun 
1398*4882a593Smuzhiyun 	return 0;
1399*4882a593Smuzhiyun fail:
1400*4882a593Smuzhiyun 	vmballoon_vmci_cleanup(b);
1401*4882a593Smuzhiyun 	return -EIO;
1402*4882a593Smuzhiyun }
1403*4882a593Smuzhiyun 
1404*4882a593Smuzhiyun /**
1405*4882a593Smuzhiyun  * vmballoon_pop - Quickly release all pages allocate for the balloon.
1406*4882a593Smuzhiyun  *
1407*4882a593Smuzhiyun  * @b: pointer to the balloon.
1408*4882a593Smuzhiyun  *
1409*4882a593Smuzhiyun  * This function is called when host decides to "reset" balloon for one reason
1410*4882a593Smuzhiyun  * or another. Unlike normal "deflate" we do not (shall not) notify host of the
1411*4882a593Smuzhiyun  * pages being released.
1412*4882a593Smuzhiyun  */
vmballoon_pop(struct vmballoon * b)1413*4882a593Smuzhiyun static void vmballoon_pop(struct vmballoon *b)
1414*4882a593Smuzhiyun {
1415*4882a593Smuzhiyun 	unsigned long size;
1416*4882a593Smuzhiyun 
1417*4882a593Smuzhiyun 	while ((size = atomic64_read(&b->size)))
1418*4882a593Smuzhiyun 		vmballoon_deflate(b, size, false);
1419*4882a593Smuzhiyun }
1420*4882a593Smuzhiyun 
1421*4882a593Smuzhiyun /*
1422*4882a593Smuzhiyun  * Perform standard reset sequence by popping the balloon (in case it
1423*4882a593Smuzhiyun  * is not  empty) and then restarting protocol. This operation normally
1424*4882a593Smuzhiyun  * happens when host responds with VMW_BALLOON_ERROR_RESET to a command.
1425*4882a593Smuzhiyun  */
vmballoon_reset(struct vmballoon * b)1426*4882a593Smuzhiyun static void vmballoon_reset(struct vmballoon *b)
1427*4882a593Smuzhiyun {
1428*4882a593Smuzhiyun 	int error;
1429*4882a593Smuzhiyun 
1430*4882a593Smuzhiyun 	down_write(&b->conf_sem);
1431*4882a593Smuzhiyun 
1432*4882a593Smuzhiyun 	vmballoon_vmci_cleanup(b);
1433*4882a593Smuzhiyun 
1434*4882a593Smuzhiyun 	/* free all pages, skipping monitor unlock */
1435*4882a593Smuzhiyun 	vmballoon_pop(b);
1436*4882a593Smuzhiyun 
1437*4882a593Smuzhiyun 	if (vmballoon_send_start(b, VMW_BALLOON_CAPABILITIES))
1438*4882a593Smuzhiyun 		goto unlock;
1439*4882a593Smuzhiyun 
1440*4882a593Smuzhiyun 	if ((b->capabilities & VMW_BALLOON_BATCHED_CMDS) != 0) {
1441*4882a593Smuzhiyun 		if (vmballoon_init_batching(b)) {
1442*4882a593Smuzhiyun 			/*
1443*4882a593Smuzhiyun 			 * We failed to initialize batching, inform the monitor
1444*4882a593Smuzhiyun 			 * about it by sending a null capability.
1445*4882a593Smuzhiyun 			 *
1446*4882a593Smuzhiyun 			 * The guest will retry in one second.
1447*4882a593Smuzhiyun 			 */
1448*4882a593Smuzhiyun 			vmballoon_send_start(b, 0);
1449*4882a593Smuzhiyun 			goto unlock;
1450*4882a593Smuzhiyun 		}
1451*4882a593Smuzhiyun 	} else if ((b->capabilities & VMW_BALLOON_BASIC_CMDS) != 0) {
1452*4882a593Smuzhiyun 		vmballoon_deinit_batching(b);
1453*4882a593Smuzhiyun 	}
1454*4882a593Smuzhiyun 
1455*4882a593Smuzhiyun 	vmballoon_stats_gen_inc(b, VMW_BALLOON_STAT_RESET);
1456*4882a593Smuzhiyun 	b->reset_required = false;
1457*4882a593Smuzhiyun 
1458*4882a593Smuzhiyun 	error = vmballoon_vmci_init(b);
1459*4882a593Smuzhiyun 	if (error)
1460*4882a593Smuzhiyun 		pr_err("failed to initialize vmci doorbell\n");
1461*4882a593Smuzhiyun 
1462*4882a593Smuzhiyun 	if (vmballoon_send_guest_id(b))
1463*4882a593Smuzhiyun 		pr_err("failed to send guest ID to the host\n");
1464*4882a593Smuzhiyun 
1465*4882a593Smuzhiyun unlock:
1466*4882a593Smuzhiyun 	up_write(&b->conf_sem);
1467*4882a593Smuzhiyun }
1468*4882a593Smuzhiyun 
1469*4882a593Smuzhiyun /**
1470*4882a593Smuzhiyun  * vmballoon_work - periodic balloon worker for reset, inflation and deflation.
1471*4882a593Smuzhiyun  *
1472*4882a593Smuzhiyun  * @work: pointer to the &work_struct which is provided by the workqueue.
1473*4882a593Smuzhiyun  *
1474*4882a593Smuzhiyun  * Resets the protocol if needed, gets the new size and adjusts balloon as
1475*4882a593Smuzhiyun  * needed. Repeat in 1 sec.
1476*4882a593Smuzhiyun  */
vmballoon_work(struct work_struct * work)1477*4882a593Smuzhiyun static void vmballoon_work(struct work_struct *work)
1478*4882a593Smuzhiyun {
1479*4882a593Smuzhiyun 	struct delayed_work *dwork = to_delayed_work(work);
1480*4882a593Smuzhiyun 	struct vmballoon *b = container_of(dwork, struct vmballoon, dwork);
1481*4882a593Smuzhiyun 	int64_t change = 0;
1482*4882a593Smuzhiyun 
1483*4882a593Smuzhiyun 	if (b->reset_required)
1484*4882a593Smuzhiyun 		vmballoon_reset(b);
1485*4882a593Smuzhiyun 
1486*4882a593Smuzhiyun 	down_read(&b->conf_sem);
1487*4882a593Smuzhiyun 
1488*4882a593Smuzhiyun 	/*
1489*4882a593Smuzhiyun 	 * Update the stats while holding the semaphore to ensure that
1490*4882a593Smuzhiyun 	 * @stats_enabled is consistent with whether the stats are actually
1491*4882a593Smuzhiyun 	 * enabled
1492*4882a593Smuzhiyun 	 */
1493*4882a593Smuzhiyun 	vmballoon_stats_gen_inc(b, VMW_BALLOON_STAT_TIMER);
1494*4882a593Smuzhiyun 
1495*4882a593Smuzhiyun 	if (!vmballoon_send_get_target(b))
1496*4882a593Smuzhiyun 		change = vmballoon_change(b);
1497*4882a593Smuzhiyun 
1498*4882a593Smuzhiyun 	if (change != 0) {
1499*4882a593Smuzhiyun 		pr_debug("%s - size: %llu, target %lu\n", __func__,
1500*4882a593Smuzhiyun 			 atomic64_read(&b->size), READ_ONCE(b->target));
1501*4882a593Smuzhiyun 
1502*4882a593Smuzhiyun 		if (change > 0)
1503*4882a593Smuzhiyun 			vmballoon_inflate(b);
1504*4882a593Smuzhiyun 		else  /* (change < 0) */
1505*4882a593Smuzhiyun 			vmballoon_deflate(b, 0, true);
1506*4882a593Smuzhiyun 	}
1507*4882a593Smuzhiyun 
1508*4882a593Smuzhiyun 	up_read(&b->conf_sem);
1509*4882a593Smuzhiyun 
1510*4882a593Smuzhiyun 	/*
1511*4882a593Smuzhiyun 	 * We are using a freezable workqueue so that balloon operations are
1512*4882a593Smuzhiyun 	 * stopped while the system transitions to/from sleep/hibernation.
1513*4882a593Smuzhiyun 	 */
1514*4882a593Smuzhiyun 	queue_delayed_work(system_freezable_wq,
1515*4882a593Smuzhiyun 			   dwork, round_jiffies_relative(HZ));
1516*4882a593Smuzhiyun 
1517*4882a593Smuzhiyun }
1518*4882a593Smuzhiyun 
1519*4882a593Smuzhiyun /**
1520*4882a593Smuzhiyun  * vmballoon_shrinker_scan() - deflate the balloon due to memory pressure.
1521*4882a593Smuzhiyun  * @shrinker: pointer to the balloon shrinker.
1522*4882a593Smuzhiyun  * @sc: page reclaim information.
1523*4882a593Smuzhiyun  *
1524*4882a593Smuzhiyun  * Returns: number of pages that were freed during deflation.
1525*4882a593Smuzhiyun  */
vmballoon_shrinker_scan(struct shrinker * shrinker,struct shrink_control * sc)1526*4882a593Smuzhiyun static unsigned long vmballoon_shrinker_scan(struct shrinker *shrinker,
1527*4882a593Smuzhiyun 					     struct shrink_control *sc)
1528*4882a593Smuzhiyun {
1529*4882a593Smuzhiyun 	struct vmballoon *b = &balloon;
1530*4882a593Smuzhiyun 	unsigned long deflated_frames;
1531*4882a593Smuzhiyun 
1532*4882a593Smuzhiyun 	pr_debug("%s - size: %llu", __func__, atomic64_read(&b->size));
1533*4882a593Smuzhiyun 
1534*4882a593Smuzhiyun 	vmballoon_stats_gen_inc(b, VMW_BALLOON_STAT_SHRINK);
1535*4882a593Smuzhiyun 
1536*4882a593Smuzhiyun 	/*
1537*4882a593Smuzhiyun 	 * If the lock is also contended for read, we cannot easily reclaim and
1538*4882a593Smuzhiyun 	 * we bail out.
1539*4882a593Smuzhiyun 	 */
1540*4882a593Smuzhiyun 	if (!down_read_trylock(&b->conf_sem))
1541*4882a593Smuzhiyun 		return 0;
1542*4882a593Smuzhiyun 
1543*4882a593Smuzhiyun 	deflated_frames = vmballoon_deflate(b, sc->nr_to_scan, true);
1544*4882a593Smuzhiyun 
1545*4882a593Smuzhiyun 	vmballoon_stats_gen_add(b, VMW_BALLOON_STAT_SHRINK_FREE,
1546*4882a593Smuzhiyun 				deflated_frames);
1547*4882a593Smuzhiyun 
1548*4882a593Smuzhiyun 	/*
1549*4882a593Smuzhiyun 	 * Delay future inflation for some time to mitigate the situations in
1550*4882a593Smuzhiyun 	 * which balloon continuously grows and shrinks. Use WRITE_ONCE() since
1551*4882a593Smuzhiyun 	 * the access is asynchronous.
1552*4882a593Smuzhiyun 	 */
1553*4882a593Smuzhiyun 	WRITE_ONCE(b->shrink_timeout, jiffies + HZ * VMBALLOON_SHRINK_DELAY);
1554*4882a593Smuzhiyun 
1555*4882a593Smuzhiyun 	up_read(&b->conf_sem);
1556*4882a593Smuzhiyun 
1557*4882a593Smuzhiyun 	return deflated_frames;
1558*4882a593Smuzhiyun }
1559*4882a593Smuzhiyun 
1560*4882a593Smuzhiyun /**
1561*4882a593Smuzhiyun  * vmballoon_shrinker_count() - return the number of ballooned pages.
1562*4882a593Smuzhiyun  * @shrinker: pointer to the balloon shrinker.
1563*4882a593Smuzhiyun  * @sc: page reclaim information.
1564*4882a593Smuzhiyun  *
1565*4882a593Smuzhiyun  * Returns: number of 4k pages that are allocated for the balloon and can
1566*4882a593Smuzhiyun  *	    therefore be reclaimed under pressure.
1567*4882a593Smuzhiyun  */
vmballoon_shrinker_count(struct shrinker * shrinker,struct shrink_control * sc)1568*4882a593Smuzhiyun static unsigned long vmballoon_shrinker_count(struct shrinker *shrinker,
1569*4882a593Smuzhiyun 					      struct shrink_control *sc)
1570*4882a593Smuzhiyun {
1571*4882a593Smuzhiyun 	struct vmballoon *b = &balloon;
1572*4882a593Smuzhiyun 
1573*4882a593Smuzhiyun 	return atomic64_read(&b->size);
1574*4882a593Smuzhiyun }
1575*4882a593Smuzhiyun 
vmballoon_unregister_shrinker(struct vmballoon * b)1576*4882a593Smuzhiyun static void vmballoon_unregister_shrinker(struct vmballoon *b)
1577*4882a593Smuzhiyun {
1578*4882a593Smuzhiyun 	if (b->shrinker_registered)
1579*4882a593Smuzhiyun 		unregister_shrinker(&b->shrinker);
1580*4882a593Smuzhiyun 	b->shrinker_registered = false;
1581*4882a593Smuzhiyun }
1582*4882a593Smuzhiyun 
vmballoon_register_shrinker(struct vmballoon * b)1583*4882a593Smuzhiyun static int vmballoon_register_shrinker(struct vmballoon *b)
1584*4882a593Smuzhiyun {
1585*4882a593Smuzhiyun 	int r;
1586*4882a593Smuzhiyun 
1587*4882a593Smuzhiyun 	/* Do nothing if the shrinker is not enabled */
1588*4882a593Smuzhiyun 	if (!vmwballoon_shrinker_enable)
1589*4882a593Smuzhiyun 		return 0;
1590*4882a593Smuzhiyun 
1591*4882a593Smuzhiyun 	b->shrinker.scan_objects = vmballoon_shrinker_scan;
1592*4882a593Smuzhiyun 	b->shrinker.count_objects = vmballoon_shrinker_count;
1593*4882a593Smuzhiyun 	b->shrinker.seeks = DEFAULT_SEEKS;
1594*4882a593Smuzhiyun 
1595*4882a593Smuzhiyun 	r = register_shrinker(&b->shrinker);
1596*4882a593Smuzhiyun 
1597*4882a593Smuzhiyun 	if (r == 0)
1598*4882a593Smuzhiyun 		b->shrinker_registered = true;
1599*4882a593Smuzhiyun 
1600*4882a593Smuzhiyun 	return r;
1601*4882a593Smuzhiyun }
1602*4882a593Smuzhiyun 
1603*4882a593Smuzhiyun /*
1604*4882a593Smuzhiyun  * DEBUGFS Interface
1605*4882a593Smuzhiyun  */
1606*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_FS
1607*4882a593Smuzhiyun 
1608*4882a593Smuzhiyun static const char * const vmballoon_stat_page_names[] = {
1609*4882a593Smuzhiyun 	[VMW_BALLOON_PAGE_STAT_ALLOC]		= "alloc",
1610*4882a593Smuzhiyun 	[VMW_BALLOON_PAGE_STAT_ALLOC_FAIL]	= "allocFail",
1611*4882a593Smuzhiyun 	[VMW_BALLOON_PAGE_STAT_REFUSED_ALLOC]	= "errAlloc",
1612*4882a593Smuzhiyun 	[VMW_BALLOON_PAGE_STAT_REFUSED_FREE]	= "errFree",
1613*4882a593Smuzhiyun 	[VMW_BALLOON_PAGE_STAT_FREE]		= "free"
1614*4882a593Smuzhiyun };
1615*4882a593Smuzhiyun 
1616*4882a593Smuzhiyun static const char * const vmballoon_stat_names[] = {
1617*4882a593Smuzhiyun 	[VMW_BALLOON_STAT_TIMER]		= "timer",
1618*4882a593Smuzhiyun 	[VMW_BALLOON_STAT_DOORBELL]		= "doorbell",
1619*4882a593Smuzhiyun 	[VMW_BALLOON_STAT_RESET]		= "reset",
1620*4882a593Smuzhiyun 	[VMW_BALLOON_STAT_SHRINK]		= "shrink",
1621*4882a593Smuzhiyun 	[VMW_BALLOON_STAT_SHRINK_FREE]		= "shrinkFree"
1622*4882a593Smuzhiyun };
1623*4882a593Smuzhiyun 
vmballoon_enable_stats(struct vmballoon * b)1624*4882a593Smuzhiyun static int vmballoon_enable_stats(struct vmballoon *b)
1625*4882a593Smuzhiyun {
1626*4882a593Smuzhiyun 	int r = 0;
1627*4882a593Smuzhiyun 
1628*4882a593Smuzhiyun 	down_write(&b->conf_sem);
1629*4882a593Smuzhiyun 
1630*4882a593Smuzhiyun 	/* did we somehow race with another reader which enabled stats? */
1631*4882a593Smuzhiyun 	if (b->stats)
1632*4882a593Smuzhiyun 		goto out;
1633*4882a593Smuzhiyun 
1634*4882a593Smuzhiyun 	b->stats = kzalloc(sizeof(*b->stats), GFP_KERNEL);
1635*4882a593Smuzhiyun 
1636*4882a593Smuzhiyun 	if (!b->stats) {
1637*4882a593Smuzhiyun 		/* allocation failed */
1638*4882a593Smuzhiyun 		r = -ENOMEM;
1639*4882a593Smuzhiyun 		goto out;
1640*4882a593Smuzhiyun 	}
1641*4882a593Smuzhiyun 	static_key_enable(&balloon_stat_enabled.key);
1642*4882a593Smuzhiyun out:
1643*4882a593Smuzhiyun 	up_write(&b->conf_sem);
1644*4882a593Smuzhiyun 	return r;
1645*4882a593Smuzhiyun }
1646*4882a593Smuzhiyun 
1647*4882a593Smuzhiyun /**
1648*4882a593Smuzhiyun  * vmballoon_debug_show - shows statistics of balloon operations.
1649*4882a593Smuzhiyun  * @f: pointer to the &struct seq_file.
1650*4882a593Smuzhiyun  * @offset: ignored.
1651*4882a593Smuzhiyun  *
1652*4882a593Smuzhiyun  * Provides the statistics that can be accessed in vmmemctl in the debugfs.
1653*4882a593Smuzhiyun  * To avoid the overhead - mainly that of memory - of collecting the statistics,
1654*4882a593Smuzhiyun  * we only collect statistics after the first time the counters are read.
1655*4882a593Smuzhiyun  *
1656*4882a593Smuzhiyun  * Return: zero on success or an error code.
1657*4882a593Smuzhiyun  */
vmballoon_debug_show(struct seq_file * f,void * offset)1658*4882a593Smuzhiyun static int vmballoon_debug_show(struct seq_file *f, void *offset)
1659*4882a593Smuzhiyun {
1660*4882a593Smuzhiyun 	struct vmballoon *b = f->private;
1661*4882a593Smuzhiyun 	int i, j;
1662*4882a593Smuzhiyun 
1663*4882a593Smuzhiyun 	/* enables stats if they are disabled */
1664*4882a593Smuzhiyun 	if (!b->stats) {
1665*4882a593Smuzhiyun 		int r = vmballoon_enable_stats(b);
1666*4882a593Smuzhiyun 
1667*4882a593Smuzhiyun 		if (r)
1668*4882a593Smuzhiyun 			return r;
1669*4882a593Smuzhiyun 	}
1670*4882a593Smuzhiyun 
1671*4882a593Smuzhiyun 	/* format capabilities info */
1672*4882a593Smuzhiyun 	seq_printf(f, "%-22s: %#16x\n", "balloon capabilities",
1673*4882a593Smuzhiyun 		   VMW_BALLOON_CAPABILITIES);
1674*4882a593Smuzhiyun 	seq_printf(f, "%-22s: %#16lx\n", "used capabilities", b->capabilities);
1675*4882a593Smuzhiyun 	seq_printf(f, "%-22s: %16s\n", "is resetting",
1676*4882a593Smuzhiyun 		   b->reset_required ? "y" : "n");
1677*4882a593Smuzhiyun 
1678*4882a593Smuzhiyun 	/* format size info */
1679*4882a593Smuzhiyun 	seq_printf(f, "%-22s: %16lu\n", "target", READ_ONCE(b->target));
1680*4882a593Smuzhiyun 	seq_printf(f, "%-22s: %16llu\n", "current", atomic64_read(&b->size));
1681*4882a593Smuzhiyun 
1682*4882a593Smuzhiyun 	for (i = 0; i < VMW_BALLOON_CMD_NUM; i++) {
1683*4882a593Smuzhiyun 		if (vmballoon_cmd_names[i] == NULL)
1684*4882a593Smuzhiyun 			continue;
1685*4882a593Smuzhiyun 
1686*4882a593Smuzhiyun 		seq_printf(f, "%-22s: %16llu (%llu failed)\n",
1687*4882a593Smuzhiyun 			   vmballoon_cmd_names[i],
1688*4882a593Smuzhiyun 			   atomic64_read(&b->stats->ops[i][VMW_BALLOON_OP_STAT]),
1689*4882a593Smuzhiyun 			   atomic64_read(&b->stats->ops[i][VMW_BALLOON_OP_FAIL_STAT]));
1690*4882a593Smuzhiyun 	}
1691*4882a593Smuzhiyun 
1692*4882a593Smuzhiyun 	for (i = 0; i < VMW_BALLOON_STAT_NUM; i++)
1693*4882a593Smuzhiyun 		seq_printf(f, "%-22s: %16llu\n",
1694*4882a593Smuzhiyun 			   vmballoon_stat_names[i],
1695*4882a593Smuzhiyun 			   atomic64_read(&b->stats->general_stat[i]));
1696*4882a593Smuzhiyun 
1697*4882a593Smuzhiyun 	for (i = 0; i < VMW_BALLOON_PAGE_STAT_NUM; i++) {
1698*4882a593Smuzhiyun 		for (j = 0; j < VMW_BALLOON_NUM_PAGE_SIZES; j++)
1699*4882a593Smuzhiyun 			seq_printf(f, "%-18s(%s): %16llu\n",
1700*4882a593Smuzhiyun 				   vmballoon_stat_page_names[i],
1701*4882a593Smuzhiyun 				   vmballoon_page_size_names[j],
1702*4882a593Smuzhiyun 				   atomic64_read(&b->stats->page_stat[i][j]));
1703*4882a593Smuzhiyun 	}
1704*4882a593Smuzhiyun 
1705*4882a593Smuzhiyun 	return 0;
1706*4882a593Smuzhiyun }
1707*4882a593Smuzhiyun 
1708*4882a593Smuzhiyun DEFINE_SHOW_ATTRIBUTE(vmballoon_debug);
1709*4882a593Smuzhiyun 
vmballoon_debugfs_init(struct vmballoon * b)1710*4882a593Smuzhiyun static void __init vmballoon_debugfs_init(struct vmballoon *b)
1711*4882a593Smuzhiyun {
1712*4882a593Smuzhiyun 	b->dbg_entry = debugfs_create_file("vmmemctl", S_IRUGO, NULL, b,
1713*4882a593Smuzhiyun 					   &vmballoon_debug_fops);
1714*4882a593Smuzhiyun }
1715*4882a593Smuzhiyun 
vmballoon_debugfs_exit(struct vmballoon * b)1716*4882a593Smuzhiyun static void __exit vmballoon_debugfs_exit(struct vmballoon *b)
1717*4882a593Smuzhiyun {
1718*4882a593Smuzhiyun 	static_key_disable(&balloon_stat_enabled.key);
1719*4882a593Smuzhiyun 	debugfs_remove(b->dbg_entry);
1720*4882a593Smuzhiyun 	kfree(b->stats);
1721*4882a593Smuzhiyun 	b->stats = NULL;
1722*4882a593Smuzhiyun }
1723*4882a593Smuzhiyun 
1724*4882a593Smuzhiyun #else
1725*4882a593Smuzhiyun 
vmballoon_debugfs_init(struct vmballoon * b)1726*4882a593Smuzhiyun static inline void vmballoon_debugfs_init(struct vmballoon *b)
1727*4882a593Smuzhiyun {
1728*4882a593Smuzhiyun }
1729*4882a593Smuzhiyun 
vmballoon_debugfs_exit(struct vmballoon * b)1730*4882a593Smuzhiyun static inline void vmballoon_debugfs_exit(struct vmballoon *b)
1731*4882a593Smuzhiyun {
1732*4882a593Smuzhiyun }
1733*4882a593Smuzhiyun 
1734*4882a593Smuzhiyun #endif	/* CONFIG_DEBUG_FS */
1735*4882a593Smuzhiyun 
1736*4882a593Smuzhiyun 
1737*4882a593Smuzhiyun #ifdef CONFIG_BALLOON_COMPACTION
1738*4882a593Smuzhiyun 
vmballoon_init_fs_context(struct fs_context * fc)1739*4882a593Smuzhiyun static int vmballoon_init_fs_context(struct fs_context *fc)
1740*4882a593Smuzhiyun {
1741*4882a593Smuzhiyun 	return init_pseudo(fc, BALLOON_VMW_MAGIC) ? 0 : -ENOMEM;
1742*4882a593Smuzhiyun }
1743*4882a593Smuzhiyun 
1744*4882a593Smuzhiyun static struct file_system_type vmballoon_fs = {
1745*4882a593Smuzhiyun 	.name           	= "balloon-vmware",
1746*4882a593Smuzhiyun 	.init_fs_context	= vmballoon_init_fs_context,
1747*4882a593Smuzhiyun 	.kill_sb        	= kill_anon_super,
1748*4882a593Smuzhiyun };
1749*4882a593Smuzhiyun 
1750*4882a593Smuzhiyun static struct vfsmount *vmballoon_mnt;
1751*4882a593Smuzhiyun 
1752*4882a593Smuzhiyun /**
1753*4882a593Smuzhiyun  * vmballoon_migratepage() - migrates a balloon page.
1754*4882a593Smuzhiyun  * @b_dev_info: balloon device information descriptor.
1755*4882a593Smuzhiyun  * @newpage: the page to which @page should be migrated.
1756*4882a593Smuzhiyun  * @page: a ballooned page that should be migrated.
1757*4882a593Smuzhiyun  * @mode: migration mode, ignored.
1758*4882a593Smuzhiyun  *
1759*4882a593Smuzhiyun  * This function is really open-coded, but that is according to the interface
1760*4882a593Smuzhiyun  * that balloon_compaction provides.
1761*4882a593Smuzhiyun  *
1762*4882a593Smuzhiyun  * Return: zero on success, -EAGAIN when migration cannot be performed
1763*4882a593Smuzhiyun  *	   momentarily, and -EBUSY if migration failed and should be retried
1764*4882a593Smuzhiyun  *	   with that specific page.
1765*4882a593Smuzhiyun  */
vmballoon_migratepage(struct balloon_dev_info * b_dev_info,struct page * newpage,struct page * page,enum migrate_mode mode)1766*4882a593Smuzhiyun static int vmballoon_migratepage(struct balloon_dev_info *b_dev_info,
1767*4882a593Smuzhiyun 				 struct page *newpage, struct page *page,
1768*4882a593Smuzhiyun 				 enum migrate_mode mode)
1769*4882a593Smuzhiyun {
1770*4882a593Smuzhiyun 	unsigned long status, flags;
1771*4882a593Smuzhiyun 	struct vmballoon *b;
1772*4882a593Smuzhiyun 	int ret;
1773*4882a593Smuzhiyun 
1774*4882a593Smuzhiyun 	b = container_of(b_dev_info, struct vmballoon, b_dev_info);
1775*4882a593Smuzhiyun 
1776*4882a593Smuzhiyun 	/*
1777*4882a593Smuzhiyun 	 * If the semaphore is taken, there is ongoing configuration change
1778*4882a593Smuzhiyun 	 * (i.e., balloon reset), so try again.
1779*4882a593Smuzhiyun 	 */
1780*4882a593Smuzhiyun 	if (!down_read_trylock(&b->conf_sem))
1781*4882a593Smuzhiyun 		return -EAGAIN;
1782*4882a593Smuzhiyun 
1783*4882a593Smuzhiyun 	spin_lock(&b->comm_lock);
1784*4882a593Smuzhiyun 	/*
1785*4882a593Smuzhiyun 	 * We must start by deflating and not inflating, as otherwise the
1786*4882a593Smuzhiyun 	 * hypervisor may tell us that it has enough memory and the new page is
1787*4882a593Smuzhiyun 	 * not needed. Since the old page is isolated, we cannot use the list
1788*4882a593Smuzhiyun 	 * interface to unlock it, as the LRU field is used for isolation.
1789*4882a593Smuzhiyun 	 * Instead, we use the native interface directly.
1790*4882a593Smuzhiyun 	 */
1791*4882a593Smuzhiyun 	vmballoon_add_page(b, 0, page);
1792*4882a593Smuzhiyun 	status = vmballoon_lock_op(b, 1, VMW_BALLOON_4K_PAGE,
1793*4882a593Smuzhiyun 				   VMW_BALLOON_DEFLATE);
1794*4882a593Smuzhiyun 
1795*4882a593Smuzhiyun 	if (status == VMW_BALLOON_SUCCESS)
1796*4882a593Smuzhiyun 		status = vmballoon_status_page(b, 0, &page);
1797*4882a593Smuzhiyun 
1798*4882a593Smuzhiyun 	/*
1799*4882a593Smuzhiyun 	 * If a failure happened, let the migration mechanism know that it
1800*4882a593Smuzhiyun 	 * should not retry.
1801*4882a593Smuzhiyun 	 */
1802*4882a593Smuzhiyun 	if (status != VMW_BALLOON_SUCCESS) {
1803*4882a593Smuzhiyun 		spin_unlock(&b->comm_lock);
1804*4882a593Smuzhiyun 		ret = -EBUSY;
1805*4882a593Smuzhiyun 		goto out_unlock;
1806*4882a593Smuzhiyun 	}
1807*4882a593Smuzhiyun 
1808*4882a593Smuzhiyun 	/*
1809*4882a593Smuzhiyun 	 * The page is isolated, so it is safe to delete it without holding
1810*4882a593Smuzhiyun 	 * @pages_lock . We keep holding @comm_lock since we will need it in a
1811*4882a593Smuzhiyun 	 * second.
1812*4882a593Smuzhiyun 	 */
1813*4882a593Smuzhiyun 	balloon_page_delete(page);
1814*4882a593Smuzhiyun 
1815*4882a593Smuzhiyun 	put_page(page);
1816*4882a593Smuzhiyun 
1817*4882a593Smuzhiyun 	/* Inflate */
1818*4882a593Smuzhiyun 	vmballoon_add_page(b, 0, newpage);
1819*4882a593Smuzhiyun 	status = vmballoon_lock_op(b, 1, VMW_BALLOON_4K_PAGE,
1820*4882a593Smuzhiyun 				   VMW_BALLOON_INFLATE);
1821*4882a593Smuzhiyun 
1822*4882a593Smuzhiyun 	if (status == VMW_BALLOON_SUCCESS)
1823*4882a593Smuzhiyun 		status = vmballoon_status_page(b, 0, &newpage);
1824*4882a593Smuzhiyun 
1825*4882a593Smuzhiyun 	spin_unlock(&b->comm_lock);
1826*4882a593Smuzhiyun 
1827*4882a593Smuzhiyun 	if (status != VMW_BALLOON_SUCCESS) {
1828*4882a593Smuzhiyun 		/*
1829*4882a593Smuzhiyun 		 * A failure happened. While we can deflate the page we just
1830*4882a593Smuzhiyun 		 * inflated, this deflation can also encounter an error. Instead
1831*4882a593Smuzhiyun 		 * we will decrease the size of the balloon to reflect the
1832*4882a593Smuzhiyun 		 * change and report failure.
1833*4882a593Smuzhiyun 		 */
1834*4882a593Smuzhiyun 		atomic64_dec(&b->size);
1835*4882a593Smuzhiyun 		ret = -EBUSY;
1836*4882a593Smuzhiyun 	} else {
1837*4882a593Smuzhiyun 		/*
1838*4882a593Smuzhiyun 		 * Success. Take a reference for the page, and we will add it to
1839*4882a593Smuzhiyun 		 * the list after acquiring the lock.
1840*4882a593Smuzhiyun 		 */
1841*4882a593Smuzhiyun 		get_page(newpage);
1842*4882a593Smuzhiyun 		ret = MIGRATEPAGE_SUCCESS;
1843*4882a593Smuzhiyun 	}
1844*4882a593Smuzhiyun 
1845*4882a593Smuzhiyun 	/* Update the balloon list under the @pages_lock */
1846*4882a593Smuzhiyun 	spin_lock_irqsave(&b->b_dev_info.pages_lock, flags);
1847*4882a593Smuzhiyun 
1848*4882a593Smuzhiyun 	/*
1849*4882a593Smuzhiyun 	 * On inflation success, we already took a reference for the @newpage.
1850*4882a593Smuzhiyun 	 * If we succeed just insert it to the list and update the statistics
1851*4882a593Smuzhiyun 	 * under the lock.
1852*4882a593Smuzhiyun 	 */
1853*4882a593Smuzhiyun 	if (ret == MIGRATEPAGE_SUCCESS) {
1854*4882a593Smuzhiyun 		balloon_page_insert(&b->b_dev_info, newpage);
1855*4882a593Smuzhiyun 		__count_vm_event(BALLOON_MIGRATE);
1856*4882a593Smuzhiyun 	}
1857*4882a593Smuzhiyun 
1858*4882a593Smuzhiyun 	/*
1859*4882a593Smuzhiyun 	 * We deflated successfully, so regardless to the inflation success, we
1860*4882a593Smuzhiyun 	 * need to reduce the number of isolated_pages.
1861*4882a593Smuzhiyun 	 */
1862*4882a593Smuzhiyun 	b->b_dev_info.isolated_pages--;
1863*4882a593Smuzhiyun 	spin_unlock_irqrestore(&b->b_dev_info.pages_lock, flags);
1864*4882a593Smuzhiyun 
1865*4882a593Smuzhiyun out_unlock:
1866*4882a593Smuzhiyun 	up_read(&b->conf_sem);
1867*4882a593Smuzhiyun 	return ret;
1868*4882a593Smuzhiyun }
1869*4882a593Smuzhiyun 
1870*4882a593Smuzhiyun /**
1871*4882a593Smuzhiyun  * vmballoon_compaction_deinit() - removes compaction related data.
1872*4882a593Smuzhiyun  *
1873*4882a593Smuzhiyun  * @b: pointer to the balloon.
1874*4882a593Smuzhiyun  */
vmballoon_compaction_deinit(struct vmballoon * b)1875*4882a593Smuzhiyun static void vmballoon_compaction_deinit(struct vmballoon *b)
1876*4882a593Smuzhiyun {
1877*4882a593Smuzhiyun 	if (!IS_ERR(b->b_dev_info.inode))
1878*4882a593Smuzhiyun 		iput(b->b_dev_info.inode);
1879*4882a593Smuzhiyun 
1880*4882a593Smuzhiyun 	b->b_dev_info.inode = NULL;
1881*4882a593Smuzhiyun 	kern_unmount(vmballoon_mnt);
1882*4882a593Smuzhiyun 	vmballoon_mnt = NULL;
1883*4882a593Smuzhiyun }
1884*4882a593Smuzhiyun 
1885*4882a593Smuzhiyun /**
1886*4882a593Smuzhiyun  * vmballoon_compaction_init() - initialized compaction for the balloon.
1887*4882a593Smuzhiyun  *
1888*4882a593Smuzhiyun  * @b: pointer to the balloon.
1889*4882a593Smuzhiyun  *
1890*4882a593Smuzhiyun  * If during the initialization a failure occurred, this function does not
1891*4882a593Smuzhiyun  * perform cleanup. The caller must call vmballoon_compaction_deinit() in this
1892*4882a593Smuzhiyun  * case.
1893*4882a593Smuzhiyun  *
1894*4882a593Smuzhiyun  * Return: zero on success or error code on failure.
1895*4882a593Smuzhiyun  */
vmballoon_compaction_init(struct vmballoon * b)1896*4882a593Smuzhiyun static __init int vmballoon_compaction_init(struct vmballoon *b)
1897*4882a593Smuzhiyun {
1898*4882a593Smuzhiyun 	vmballoon_mnt = kern_mount(&vmballoon_fs);
1899*4882a593Smuzhiyun 	if (IS_ERR(vmballoon_mnt))
1900*4882a593Smuzhiyun 		return PTR_ERR(vmballoon_mnt);
1901*4882a593Smuzhiyun 
1902*4882a593Smuzhiyun 	b->b_dev_info.migratepage = vmballoon_migratepage;
1903*4882a593Smuzhiyun 	b->b_dev_info.inode = alloc_anon_inode(vmballoon_mnt->mnt_sb);
1904*4882a593Smuzhiyun 
1905*4882a593Smuzhiyun 	if (IS_ERR(b->b_dev_info.inode))
1906*4882a593Smuzhiyun 		return PTR_ERR(b->b_dev_info.inode);
1907*4882a593Smuzhiyun 
1908*4882a593Smuzhiyun 	b->b_dev_info.inode->i_mapping->a_ops = &balloon_aops;
1909*4882a593Smuzhiyun 	return 0;
1910*4882a593Smuzhiyun }
1911*4882a593Smuzhiyun 
1912*4882a593Smuzhiyun #else /* CONFIG_BALLOON_COMPACTION */
1913*4882a593Smuzhiyun 
vmballoon_compaction_deinit(struct vmballoon * b)1914*4882a593Smuzhiyun static void vmballoon_compaction_deinit(struct vmballoon *b)
1915*4882a593Smuzhiyun {
1916*4882a593Smuzhiyun }
1917*4882a593Smuzhiyun 
vmballoon_compaction_init(struct vmballoon * b)1918*4882a593Smuzhiyun static int vmballoon_compaction_init(struct vmballoon *b)
1919*4882a593Smuzhiyun {
1920*4882a593Smuzhiyun 	return 0;
1921*4882a593Smuzhiyun }
1922*4882a593Smuzhiyun 
1923*4882a593Smuzhiyun #endif /* CONFIG_BALLOON_COMPACTION */
1924*4882a593Smuzhiyun 
vmballoon_init(void)1925*4882a593Smuzhiyun static int __init vmballoon_init(void)
1926*4882a593Smuzhiyun {
1927*4882a593Smuzhiyun 	int error;
1928*4882a593Smuzhiyun 
1929*4882a593Smuzhiyun 	/*
1930*4882a593Smuzhiyun 	 * Check if we are running on VMware's hypervisor and bail out
1931*4882a593Smuzhiyun 	 * if we are not.
1932*4882a593Smuzhiyun 	 */
1933*4882a593Smuzhiyun 	if (x86_hyper_type != X86_HYPER_VMWARE)
1934*4882a593Smuzhiyun 		return -ENODEV;
1935*4882a593Smuzhiyun 
1936*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&balloon.dwork, vmballoon_work);
1937*4882a593Smuzhiyun 
1938*4882a593Smuzhiyun 	error = vmballoon_register_shrinker(&balloon);
1939*4882a593Smuzhiyun 	if (error)
1940*4882a593Smuzhiyun 		goto fail;
1941*4882a593Smuzhiyun 
1942*4882a593Smuzhiyun 	/*
1943*4882a593Smuzhiyun 	 * Initialization of compaction must be done after the call to
1944*4882a593Smuzhiyun 	 * balloon_devinfo_init() .
1945*4882a593Smuzhiyun 	 */
1946*4882a593Smuzhiyun 	balloon_devinfo_init(&balloon.b_dev_info);
1947*4882a593Smuzhiyun 	error = vmballoon_compaction_init(&balloon);
1948*4882a593Smuzhiyun 	if (error)
1949*4882a593Smuzhiyun 		goto fail;
1950*4882a593Smuzhiyun 
1951*4882a593Smuzhiyun 	INIT_LIST_HEAD(&balloon.huge_pages);
1952*4882a593Smuzhiyun 	spin_lock_init(&balloon.comm_lock);
1953*4882a593Smuzhiyun 	init_rwsem(&balloon.conf_sem);
1954*4882a593Smuzhiyun 	balloon.vmci_doorbell = VMCI_INVALID_HANDLE;
1955*4882a593Smuzhiyun 	balloon.batch_page = NULL;
1956*4882a593Smuzhiyun 	balloon.page = NULL;
1957*4882a593Smuzhiyun 	balloon.reset_required = true;
1958*4882a593Smuzhiyun 
1959*4882a593Smuzhiyun 	queue_delayed_work(system_freezable_wq, &balloon.dwork, 0);
1960*4882a593Smuzhiyun 
1961*4882a593Smuzhiyun 	vmballoon_debugfs_init(&balloon);
1962*4882a593Smuzhiyun 
1963*4882a593Smuzhiyun 	return 0;
1964*4882a593Smuzhiyun fail:
1965*4882a593Smuzhiyun 	vmballoon_unregister_shrinker(&balloon);
1966*4882a593Smuzhiyun 	vmballoon_compaction_deinit(&balloon);
1967*4882a593Smuzhiyun 	return error;
1968*4882a593Smuzhiyun }
1969*4882a593Smuzhiyun 
1970*4882a593Smuzhiyun /*
1971*4882a593Smuzhiyun  * Using late_initcall() instead of module_init() allows the balloon to use the
1972*4882a593Smuzhiyun  * VMCI doorbell even when the balloon is built into the kernel. Otherwise the
1973*4882a593Smuzhiyun  * VMCI is probed only after the balloon is initialized. If the balloon is used
1974*4882a593Smuzhiyun  * as a module, late_initcall() is equivalent to module_init().
1975*4882a593Smuzhiyun  */
1976*4882a593Smuzhiyun late_initcall(vmballoon_init);
1977*4882a593Smuzhiyun 
vmballoon_exit(void)1978*4882a593Smuzhiyun static void __exit vmballoon_exit(void)
1979*4882a593Smuzhiyun {
1980*4882a593Smuzhiyun 	vmballoon_unregister_shrinker(&balloon);
1981*4882a593Smuzhiyun 	vmballoon_vmci_cleanup(&balloon);
1982*4882a593Smuzhiyun 	cancel_delayed_work_sync(&balloon.dwork);
1983*4882a593Smuzhiyun 
1984*4882a593Smuzhiyun 	vmballoon_debugfs_exit(&balloon);
1985*4882a593Smuzhiyun 
1986*4882a593Smuzhiyun 	/*
1987*4882a593Smuzhiyun 	 * Deallocate all reserved memory, and reset connection with monitor.
1988*4882a593Smuzhiyun 	 * Reset connection before deallocating memory to avoid potential for
1989*4882a593Smuzhiyun 	 * additional spurious resets from guest touching deallocated pages.
1990*4882a593Smuzhiyun 	 */
1991*4882a593Smuzhiyun 	vmballoon_send_start(&balloon, 0);
1992*4882a593Smuzhiyun 	vmballoon_pop(&balloon);
1993*4882a593Smuzhiyun 
1994*4882a593Smuzhiyun 	/* Only once we popped the balloon, compaction can be deinit */
1995*4882a593Smuzhiyun 	vmballoon_compaction_deinit(&balloon);
1996*4882a593Smuzhiyun }
1997*4882a593Smuzhiyun module_exit(vmballoon_exit);
1998