blob: 7842999a0d2466e62a0a1c6c6446d14977058821 [file] [log] [blame]
Nadav Amit8b4770e2018-06-19 16:00:29 -07001// SPDX-License-Identifier: GPL-2.0
Dmitry Torokhov453dc652010-04-23 13:18:08 -04002/*
3 * VMware Balloon driver.
4 *
Nadav Amit8b4770e2018-06-19 16:00:29 -07005 * Copyright (C) 2000-2018, VMware, Inc. All Rights Reserved.
Dmitry Torokhov453dc652010-04-23 13:18:08 -04006 *
Dmitry Torokhov453dc652010-04-23 13:18:08 -04007 * This is VMware physical memory management driver for Linux. The driver
8 * acts like a "balloon" that can be inflated to reclaim physical pages by
9 * reserving them in the guest and invalidating them in the monitor,
10 * freeing up the underlying machine pages so they can be allocated to
11 * other guests. The balloon can also be deflated to allow the guest to
12 * use more physical memory. Higher level policies can control the sizes
13 * of balloons in VMs in order to manage physical memory resources.
14 */
15
16//#define DEBUG
17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19#include <linux/types.h>
20#include <linux/kernel.h>
21#include <linux/mm.h>
Xavier Deguillardf220a802015-08-06 15:17:58 -070022#include <linux/vmalloc.h>
Dmitry Torokhov453dc652010-04-23 13:18:08 -040023#include <linux/sched.h>
24#include <linux/module.h>
25#include <linux/workqueue.h>
26#include <linux/debugfs.h>
27#include <linux/seq_file.h>
Nadav Amitc7b36902018-09-20 10:30:17 -070028#include <linux/rwsem.h>
29#include <linux/slab.h>
Nadav Amit6e4453b2018-09-20 10:30:18 -070030#include <linux/spinlock.h>
Philip P. Moltmann48e3d662015-08-06 15:18:01 -070031#include <linux/vmw_vmci_defs.h>
32#include <linux/vmw_vmci_api.h>
H. Peter Anvina10a5692010-05-09 01:13:42 -070033#include <asm/hypervisor.h>
Dmitry Torokhov453dc652010-04-23 13:18:08 -040034
35MODULE_AUTHOR("VMware, Inc.");
36MODULE_DESCRIPTION("VMware Memory Control (Balloon) Driver");
Dmitry Torokhov453dc652010-04-23 13:18:08 -040037MODULE_ALIAS("dmi:*:svnVMware*:*");
38MODULE_ALIAS("vmware_vmmemctl");
39MODULE_LICENSE("GPL");
40
41/*
Nadav Amit622074a2018-09-20 10:30:11 -070042 * Use __GFP_HIGHMEM to allow pages from HIGHMEM zone. We don't allow wait
43 * (__GFP_RECLAIM) for huge page allocations. Use __GFP_NOWARN, to suppress page
44 * allocation failure warnings. Disallow access to emergency low-memory pools.
Dmitry Torokhov453dc652010-04-23 13:18:08 -040045 */
Nadav Amit622074a2018-09-20 10:30:11 -070046#define VMW_HUGE_PAGE_ALLOC_FLAGS (__GFP_HIGHMEM|__GFP_NOWARN| \
47 __GFP_NOMEMALLOC)
Dmitry Torokhov453dc652010-04-23 13:18:08 -040048
49/*
Nadav Amit622074a2018-09-20 10:30:11 -070050 * Use __GFP_HIGHMEM to allow pages from HIGHMEM zone. We allow lightweight
51 * reclamation (__GFP_NORETRY). Use __GFP_NOWARN, to suppress page allocation
52 * failure warnings. Disallow access to emergency low-memory pools.
Dmitry Torokhov453dc652010-04-23 13:18:08 -040053 */
Nadav Amit622074a2018-09-20 10:30:11 -070054#define VMW_PAGE_ALLOC_FLAGS (__GFP_HIGHMEM|__GFP_NOWARN| \
55 __GFP_NOMEMALLOC|__GFP_NORETRY)
Dmitry Torokhov453dc652010-04-23 13:18:08 -040056
Dmitry Torokhov55adaa42010-06-04 14:14:52 -070057/* Maximum number of refused pages we accumulate during inflation cycle */
58#define VMW_BALLOON_MAX_REFUSED 16
Dmitry Torokhov453dc652010-04-23 13:18:08 -040059
60/*
61 * Hypervisor communication port definitions.
62 */
63#define VMW_BALLOON_HV_PORT 0x5670
64#define VMW_BALLOON_HV_MAGIC 0x456c6d6f
Dmitry Torokhov453dc652010-04-23 13:18:08 -040065#define VMW_BALLOON_GUEST_ID 1 /* Linux */
66
Xavier Deguillardeb791002015-06-12 11:43:23 -070067enum vmwballoon_capabilities {
68 /*
69 * Bit 0 is reserved and not associated to any capability.
70 */
Philip P. Moltmann48e3d662015-08-06 15:18:01 -070071 VMW_BALLOON_BASIC_CMDS = (1 << 1),
72 VMW_BALLOON_BATCHED_CMDS = (1 << 2),
73 VMW_BALLOON_BATCHED_2M_CMDS = (1 << 3),
74 VMW_BALLOON_SIGNALLED_WAKEUP_CMD = (1 << 4),
Xavier Deguillard55398302019-02-06 15:57:02 -080075 VMW_BALLOON_64_BIT_TARGET = (1 << 5)
Xavier Deguillardeb791002015-06-12 11:43:23 -070076};
77
Xavier Deguillard55398302019-02-06 15:57:02 -080078#define VMW_BALLOON_CAPABILITIES_COMMON (VMW_BALLOON_BASIC_CMDS \
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -070079 | VMW_BALLOON_BATCHED_CMDS \
Philip P. Moltmann48e3d662015-08-06 15:18:01 -070080 | VMW_BALLOON_BATCHED_2M_CMDS \
81 | VMW_BALLOON_SIGNALLED_WAKEUP_CMD)
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -070082
Nadav Amit25acbdd2018-09-20 10:30:14 -070083#define VMW_BALLOON_2M_ORDER (PMD_SHIFT - PAGE_SHIFT)
Xavier Deguillardeb791002015-06-12 11:43:23 -070084
Xavier Deguillard55398302019-02-06 15:57:02 -080085/*
86 * 64-bit targets are only supported in 64-bit
87 */
88#ifdef CONFIG_64BIT
89#define VMW_BALLOON_CAPABILITIES (VMW_BALLOON_CAPABILITIES_COMMON \
90 | VMW_BALLOON_64_BIT_TARGET)
91#else
92#define VMW_BALLOON_CAPABILITIES VMW_BALLOON_CAPABILITIES_COMMON
93#endif
94
Nadav Amitc7b36902018-09-20 10:30:17 -070095enum vmballoon_page_size_type {
96 VMW_BALLOON_4K_PAGE,
97 VMW_BALLOON_2M_PAGE,
98 VMW_BALLOON_LAST_SIZE = VMW_BALLOON_2M_PAGE
99};
100
101#define VMW_BALLOON_NUM_PAGE_SIZES (VMW_BALLOON_LAST_SIZE + 1)
102
Nadav Amit6e4453b2018-09-20 10:30:18 -0700103static const char * const vmballoon_page_size_names[] = {
104 [VMW_BALLOON_4K_PAGE] = "4k",
105 [VMW_BALLOON_2M_PAGE] = "2M"
106};
107
108enum vmballoon_op {
109 VMW_BALLOON_INFLATE,
110 VMW_BALLOON_DEFLATE
111};
112
Nadav Amitc7b36902018-09-20 10:30:17 -0700113enum vmballoon_op_stat_type {
114 VMW_BALLOON_OP_STAT,
115 VMW_BALLOON_OP_FAIL_STAT
116};
117
118#define VMW_BALLOON_OP_STAT_TYPES (VMW_BALLOON_OP_FAIL_STAT + 1)
119
120/**
121 * enum vmballoon_cmd_type - backdoor commands.
Xavier Deguillardf220a802015-08-06 15:17:58 -0700122 *
Nadav Amitc7b36902018-09-20 10:30:17 -0700123 * Availability of the commands is as followed:
Xavier Deguillardf220a802015-08-06 15:17:58 -0700124 *
Nadav Amitc7b36902018-09-20 10:30:17 -0700125 * %VMW_BALLOON_CMD_START, %VMW_BALLOON_CMD_GET_TARGET and
126 * %VMW_BALLOON_CMD_GUEST_ID are always available.
127 *
128 * If the host reports %VMW_BALLOON_BASIC_CMDS are supported then
129 * %VMW_BALLOON_CMD_LOCK and %VMW_BALLOON_CMD_UNLOCK commands are available.
130 *
131 * If the host reports %VMW_BALLOON_BATCHED_CMDS are supported then
132 * %VMW_BALLOON_CMD_BATCHED_LOCK and VMW_BALLOON_CMD_BATCHED_UNLOCK commands
133 * are available.
134 *
135 * If the host reports %VMW_BALLOON_BATCHED_2M_CMDS are supported then
136 * %VMW_BALLOON_CMD_BATCHED_2M_LOCK and %VMW_BALLOON_CMD_BATCHED_2M_UNLOCK
137 * are supported.
138 *
139 * If the host reports VMW_BALLOON_SIGNALLED_WAKEUP_CMD is supported then
140 * VMW_BALLOON_CMD_VMCI_DOORBELL_SET command is supported.
141 *
142 * @VMW_BALLOON_CMD_START: Communicating supported version with the hypervisor.
143 * @VMW_BALLOON_CMD_GET_TARGET: Gets the balloon target size.
144 * @VMW_BALLOON_CMD_LOCK: Informs the hypervisor about a ballooned page.
145 * @VMW_BALLOON_CMD_UNLOCK: Informs the hypervisor about a page that is about
146 * to be deflated from the balloon.
147 * @VMW_BALLOON_CMD_GUEST_ID: Informs the hypervisor about the type of OS that
148 * runs in the VM.
149 * @VMW_BALLOON_CMD_BATCHED_LOCK: Inform the hypervisor about a batch of
150 * ballooned pages (up to 512).
151 * @VMW_BALLOON_CMD_BATCHED_UNLOCK: Inform the hypervisor about a batch of
152 * pages that are about to be deflated from the
153 * balloon (up to 512).
154 * @VMW_BALLOON_CMD_BATCHED_2M_LOCK: Similar to @VMW_BALLOON_CMD_BATCHED_LOCK
155 * for 2MB pages.
156 * @VMW_BALLOON_CMD_BATCHED_2M_UNLOCK: Similar to
157 * @VMW_BALLOON_CMD_BATCHED_UNLOCK for 2MB
158 * pages.
159 * @VMW_BALLOON_CMD_VMCI_DOORBELL_SET: A command to set doorbell notification
160 * that would be invoked when the balloon
161 * size changes.
162 * @VMW_BALLOON_CMD_LAST: Value of the last command.
Xavier Deguillardf220a802015-08-06 15:17:58 -0700163 */
Nadav Amitc7b36902018-09-20 10:30:17 -0700164enum vmballoon_cmd_type {
165 VMW_BALLOON_CMD_START,
166 VMW_BALLOON_CMD_GET_TARGET,
167 VMW_BALLOON_CMD_LOCK,
168 VMW_BALLOON_CMD_UNLOCK,
169 VMW_BALLOON_CMD_GUEST_ID,
170 /* No command 5 */
171 VMW_BALLOON_CMD_BATCHED_LOCK = 6,
172 VMW_BALLOON_CMD_BATCHED_UNLOCK,
173 VMW_BALLOON_CMD_BATCHED_2M_LOCK,
174 VMW_BALLOON_CMD_BATCHED_2M_UNLOCK,
175 VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
176 VMW_BALLOON_CMD_LAST = VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
177};
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700178
Nadav Amitc7b36902018-09-20 10:30:17 -0700179#define VMW_BALLOON_CMD_NUM (VMW_BALLOON_CMD_LAST + 1)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400180
Nadav Amitc7b36902018-09-20 10:30:17 -0700181enum vmballoon_error_codes {
182 VMW_BALLOON_SUCCESS,
183 VMW_BALLOON_ERROR_CMD_INVALID,
184 VMW_BALLOON_ERROR_PPN_INVALID,
185 VMW_BALLOON_ERROR_PPN_LOCKED,
186 VMW_BALLOON_ERROR_PPN_UNLOCKED,
187 VMW_BALLOON_ERROR_PPN_PINNED,
188 VMW_BALLOON_ERROR_PPN_NOTNEEDED,
189 VMW_BALLOON_ERROR_RESET,
190 VMW_BALLOON_ERROR_BUSY
191};
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400192
Xavier Deguillardeb791002015-06-12 11:43:23 -0700193#define VMW_BALLOON_SUCCESS_WITH_CAPABILITIES (0x03000000)
194
Nadav Amit10a95d52018-09-20 10:30:07 -0700195#define VMW_BALLOON_CMD_WITH_TARGET_MASK \
196 ((1UL << VMW_BALLOON_CMD_GET_TARGET) | \
197 (1UL << VMW_BALLOON_CMD_LOCK) | \
198 (1UL << VMW_BALLOON_CMD_UNLOCK) | \
199 (1UL << VMW_BALLOON_CMD_BATCHED_LOCK) | \
200 (1UL << VMW_BALLOON_CMD_BATCHED_UNLOCK) | \
201 (1UL << VMW_BALLOON_CMD_BATCHED_2M_LOCK) | \
202 (1UL << VMW_BALLOON_CMD_BATCHED_2M_UNLOCK))
203
Nadav Amit68131182018-09-20 10:30:08 -0700204static const char * const vmballoon_cmd_names[] = {
205 [VMW_BALLOON_CMD_START] = "start",
206 [VMW_BALLOON_CMD_GET_TARGET] = "target",
207 [VMW_BALLOON_CMD_LOCK] = "lock",
208 [VMW_BALLOON_CMD_UNLOCK] = "unlock",
209 [VMW_BALLOON_CMD_GUEST_ID] = "guestType",
210 [VMW_BALLOON_CMD_BATCHED_LOCK] = "batchLock",
211 [VMW_BALLOON_CMD_BATCHED_UNLOCK] = "batchUnlock",
212 [VMW_BALLOON_CMD_BATCHED_2M_LOCK] = "2m-lock",
213 [VMW_BALLOON_CMD_BATCHED_2M_UNLOCK] = "2m-unlock",
214 [VMW_BALLOON_CMD_VMCI_DOORBELL_SET] = "doorbellSet"
215};
216
Nadav Amitc7b36902018-09-20 10:30:17 -0700217enum vmballoon_stat_page {
218 VMW_BALLOON_PAGE_STAT_ALLOC,
219 VMW_BALLOON_PAGE_STAT_ALLOC_FAIL,
220 VMW_BALLOON_PAGE_STAT_REFUSED_ALLOC,
221 VMW_BALLOON_PAGE_STAT_REFUSED_FREE,
222 VMW_BALLOON_PAGE_STAT_FREE,
223 VMW_BALLOON_PAGE_STAT_LAST = VMW_BALLOON_PAGE_STAT_FREE
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400224};
225
Nadav Amitc7b36902018-09-20 10:30:17 -0700226#define VMW_BALLOON_PAGE_STAT_NUM (VMW_BALLOON_PAGE_STAT_LAST + 1)
227
228enum vmballoon_stat_general {
229 VMW_BALLOON_STAT_TIMER,
230 VMW_BALLOON_STAT_DOORBELL,
Nadav Amit8840a6f2018-09-20 10:30:20 -0700231 VMW_BALLOON_STAT_RESET,
232 VMW_BALLOON_STAT_LAST = VMW_BALLOON_STAT_RESET
Nadav Amitc7b36902018-09-20 10:30:17 -0700233};
234
235#define VMW_BALLOON_STAT_NUM (VMW_BALLOON_STAT_LAST + 1)
236
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400237
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700238static DEFINE_STATIC_KEY_TRUE(vmw_balloon_batching);
Nadav Amitc7b36902018-09-20 10:30:17 -0700239static DEFINE_STATIC_KEY_FALSE(balloon_stat_enabled);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700240
Nadav Amit6e4453b2018-09-20 10:30:18 -0700241struct vmballoon_ctl {
242 struct list_head pages;
243 struct list_head refused_pages;
244 unsigned int n_refused_pages;
245 unsigned int n_pages;
246 enum vmballoon_page_size_type page_size;
247 enum vmballoon_op op;
248};
249
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700250struct vmballoon_page_size {
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400251 /* list of reserved physical pages */
252 struct list_head pages;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700253};
254
Nadav Amit6c948752018-09-20 10:30:10 -0700255/**
256 * struct vmballoon_batch_entry - a batch entry for lock or unlock.
257 *
258 * @status: the status of the operation, which is written by the hypervisor.
259 * @reserved: reserved for future use. Must be set to zero.
260 * @pfn: the physical frame number of the page to be locked or unlocked.
261 */
262struct vmballoon_batch_entry {
263 u64 status : 5;
264 u64 reserved : PAGE_SHIFT - 5;
265 u64 pfn : 52;
266} __packed;
267
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700268struct vmballoon {
269 struct vmballoon_page_size page_sizes[VMW_BALLOON_NUM_PAGE_SIZES];
270
Nadav Amit6e4453b2018-09-20 10:30:18 -0700271 /**
272 * @max_page_size: maximum supported page size for ballooning.
273 *
274 * Protected by @conf_sem
275 */
276 enum vmballoon_page_size_type max_page_size;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400277
Nadav Amit6e4453b2018-09-20 10:30:18 -0700278 /**
279 * @size: balloon actual size in basic page size (frames).
280 *
281 * While we currently do not support size which is bigger than 32-bit,
282 * in preparation for future support, use 64-bits.
283 */
284 atomic64_t size;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400285
Nadav Amit6e4453b2018-09-20 10:30:18 -0700286 /**
287 * @target: balloon target size in basic page size (frames).
288 *
289 * We do not protect the target under the assumption that setting the
290 * value is always done through a single write. If this assumption ever
291 * breaks, we would have to use X_ONCE for accesses, and suffer the less
292 * optimized code. Although we may read stale target value if multiple
293 * accesses happen at once, the performance impact should be minor.
294 */
295 unsigned long target;
296
297 /**
298 * @reset_required: reset flag
299 *
300 * Setting this flag may introduce races, but the code is expected to
301 * handle them gracefully. In the worst case, another operation will
302 * fail as reset did not take place. Clearing the flag is done while
303 * holding @conf_sem for write.
304 */
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400305 bool reset_required;
306
Nadav Amit6e4453b2018-09-20 10:30:18 -0700307 /**
308 * @capabilities: hypervisor balloon capabilities.
309 *
310 * Protected by @conf_sem.
311 */
Xavier Deguillardf220a802015-08-06 15:17:58 -0700312 unsigned long capabilities;
313
Nadav Amit6c948752018-09-20 10:30:10 -0700314 /**
315 * @batch_page: pointer to communication batch page.
316 *
317 * When batching is used, batch_page points to a page, which holds up to
318 * %VMW_BALLOON_BATCH_MAX_PAGES entries for locking or unlocking.
319 */
320 struct vmballoon_batch_entry *batch_page;
321
Nadav Amit6e4453b2018-09-20 10:30:18 -0700322 /**
323 * @batch_max_pages: maximum pages that can be locked/unlocked.
324 *
325 * Indicates the number of pages that the hypervisor can lock or unlock
326 * at once, according to whether batching is enabled. If batching is
327 * disabled, only a single page can be locked/unlock on each operation.
328 *
329 * Protected by @conf_sem.
330 */
Xavier Deguillardf220a802015-08-06 15:17:58 -0700331 unsigned int batch_max_pages;
Nadav Amit6e4453b2018-09-20 10:30:18 -0700332
333 /**
334 * @page: page to be locked/unlocked by the hypervisor
335 *
336 * @page is only used when batching is disabled and a single page is
337 * reclaimed on each iteration.
338 *
339 * Protected by @comm_lock.
340 */
Xavier Deguillardf220a802015-08-06 15:17:58 -0700341 struct page *page;
342
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400343 /* statistics */
Nadav Amitc7b36902018-09-20 10:30:17 -0700344 struct vmballoon_stats *stats;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400345
Nadav Amitc7b36902018-09-20 10:30:17 -0700346#ifdef CONFIG_DEBUG_FS
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400347 /* debugfs file exporting statistics */
348 struct dentry *dbg_entry;
349#endif
350
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400351 struct delayed_work dwork;
Philip P. Moltmann48e3d662015-08-06 15:18:01 -0700352
Nadav Amit6e4453b2018-09-20 10:30:18 -0700353 /**
354 * @vmci_doorbell.
355 *
356 * Protected by @conf_sem.
357 */
Philip P. Moltmann48e3d662015-08-06 15:18:01 -0700358 struct vmci_handle vmci_doorbell;
Nadav Amitc7b36902018-09-20 10:30:17 -0700359
360 /**
361 * @conf_sem: semaphore to protect the configuration and the statistics.
362 */
363 struct rw_semaphore conf_sem;
Nadav Amit6e4453b2018-09-20 10:30:18 -0700364
365 /**
366 * @comm_lock: lock to protect the communication with the host.
367 *
368 * Lock ordering: @conf_sem -> @comm_lock .
369 */
370 spinlock_t comm_lock;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400371};
372
373static struct vmballoon balloon;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400374
Nadav Amitc7b36902018-09-20 10:30:17 -0700375struct vmballoon_stats {
376 /* timer / doorbell operations */
377 atomic64_t general_stat[VMW_BALLOON_STAT_NUM];
378
379 /* allocation statistics for huge and small pages */
380 atomic64_t
381 page_stat[VMW_BALLOON_PAGE_STAT_NUM][VMW_BALLOON_NUM_PAGE_SIZES];
382
383 /* Monitor operations: total operations, and failures */
384 atomic64_t ops[VMW_BALLOON_CMD_NUM][VMW_BALLOON_OP_STAT_TYPES];
385};
386
387static inline bool is_vmballoon_stats_on(void)
388{
389 return IS_ENABLED(CONFIG_DEBUG_FS) &&
390 static_branch_unlikely(&balloon_stat_enabled);
391}
392
393static inline void vmballoon_stats_op_inc(struct vmballoon *b, unsigned int op,
394 enum vmballoon_op_stat_type type)
395{
396 if (is_vmballoon_stats_on())
397 atomic64_inc(&b->stats->ops[op][type]);
398}
399
400static inline void vmballoon_stats_gen_inc(struct vmballoon *b,
401 enum vmballoon_stat_general stat)
402{
403 if (is_vmballoon_stats_on())
404 atomic64_inc(&b->stats->general_stat[stat]);
405}
406
407static inline void vmballoon_stats_gen_add(struct vmballoon *b,
408 enum vmballoon_stat_general stat,
409 unsigned int val)
410{
411 if (is_vmballoon_stats_on())
412 atomic64_add(val, &b->stats->general_stat[stat]);
413}
414
415static inline void vmballoon_stats_page_inc(struct vmballoon *b,
416 enum vmballoon_stat_page stat,
Nadav Amit6e4453b2018-09-20 10:30:18 -0700417 enum vmballoon_page_size_type size)
Nadav Amitc7b36902018-09-20 10:30:17 -0700418{
419 if (is_vmballoon_stats_on())
Nadav Amit6e4453b2018-09-20 10:30:18 -0700420 atomic64_inc(&b->stats->page_stat[stat][size]);
421}
422
423static inline void vmballoon_stats_page_add(struct vmballoon *b,
424 enum vmballoon_stat_page stat,
425 enum vmballoon_page_size_type size,
426 unsigned int val)
427{
428 if (is_vmballoon_stats_on())
429 atomic64_add(val, &b->stats->page_stat[stat][size]);
Nadav Amitc7b36902018-09-20 10:30:17 -0700430}
431
Nadav Amit10a95d52018-09-20 10:30:07 -0700432static inline unsigned long
433__vmballoon_cmd(struct vmballoon *b, unsigned long cmd, unsigned long arg1,
434 unsigned long arg2, unsigned long *result)
435{
436 unsigned long status, dummy1, dummy2, dummy3, local_result;
437
Nadav Amitc7b36902018-09-20 10:30:17 -0700438 vmballoon_stats_op_inc(b, cmd, VMW_BALLOON_OP_STAT);
Nadav Amit68131182018-09-20 10:30:08 -0700439
Nadav Amit10a95d52018-09-20 10:30:07 -0700440 asm volatile ("inl %%dx" :
441 "=a"(status),
442 "=c"(dummy1),
443 "=d"(dummy2),
444 "=b"(local_result),
445 "=S"(dummy3) :
446 "0"(VMW_BALLOON_HV_MAGIC),
447 "1"(cmd),
448 "2"(VMW_BALLOON_HV_PORT),
449 "3"(arg1),
450 "4"(arg2) :
451 "memory");
452
453 /* update the result if needed */
454 if (result)
455 *result = (cmd == VMW_BALLOON_CMD_START) ? dummy1 :
456 local_result;
457
458 /* update target when applicable */
459 if (status == VMW_BALLOON_SUCCESS &&
460 ((1ul << cmd) & VMW_BALLOON_CMD_WITH_TARGET_MASK))
Nadav Amit6e4453b2018-09-20 10:30:18 -0700461 WRITE_ONCE(b->target, local_result);
Nadav Amit10a95d52018-09-20 10:30:07 -0700462
Nadav Amit68131182018-09-20 10:30:08 -0700463 if (status != VMW_BALLOON_SUCCESS &&
464 status != VMW_BALLOON_SUCCESS_WITH_CAPABILITIES) {
Nadav Amitc7b36902018-09-20 10:30:17 -0700465 vmballoon_stats_op_inc(b, cmd, VMW_BALLOON_OP_FAIL_STAT);
Nadav Amit68131182018-09-20 10:30:08 -0700466 pr_debug("%s: %s [0x%lx,0x%lx) failed, returned %ld\n",
467 __func__, vmballoon_cmd_names[cmd], arg1, arg2,
468 status);
469 }
470
Nadav Amit10a95d52018-09-20 10:30:07 -0700471 /* mark reset required accordingly */
472 if (status == VMW_BALLOON_ERROR_RESET)
473 b->reset_required = true;
474
475 return status;
476}
477
478static __always_inline unsigned long
479vmballoon_cmd(struct vmballoon *b, unsigned long cmd, unsigned long arg1,
480 unsigned long arg2)
481{
482 unsigned long dummy;
483
484 return __vmballoon_cmd(b, cmd, arg1, arg2, &dummy);
485}
486
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400487/*
488 * Send "start" command to the host, communicating supported version
489 * of the protocol.
490 */
Nadav Amit22d293e2018-09-20 10:30:19 -0700491static int vmballoon_send_start(struct vmballoon *b, unsigned long req_caps)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400492{
Nadav Amit10a95d52018-09-20 10:30:07 -0700493 unsigned long status, capabilities;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400494
Nadav Amit10a95d52018-09-20 10:30:07 -0700495 status = __vmballoon_cmd(b, VMW_BALLOON_CMD_START, req_caps, 0,
496 &capabilities);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700497
498 switch (status) {
499 case VMW_BALLOON_SUCCESS_WITH_CAPABILITIES:
500 b->capabilities = capabilities;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700501 break;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700502 case VMW_BALLOON_SUCCESS:
503 b->capabilities = VMW_BALLOON_BASIC_CMDS;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700504 break;
505 default:
Nadav Amit22d293e2018-09-20 10:30:19 -0700506 return -EIO;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700507 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400508
Nadav Amit5081efd2018-06-19 16:00:25 -0700509 /*
510 * 2MB pages are only supported with batching. If batching is for some
511 * reason disabled, do not use 2MB pages, since otherwise the legacy
512 * mechanism is used with 2MB pages, causing a failure.
513 */
Nadav Amit6e4453b2018-09-20 10:30:18 -0700514 b->max_page_size = VMW_BALLOON_4K_PAGE;
Nadav Amit5081efd2018-06-19 16:00:25 -0700515 if ((b->capabilities & VMW_BALLOON_BATCHED_2M_CMDS) &&
516 (b->capabilities & VMW_BALLOON_BATCHED_CMDS))
Nadav Amit6e4453b2018-09-20 10:30:18 -0700517 b->max_page_size = VMW_BALLOON_2M_PAGE;
518
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700519
Nadav Amit22d293e2018-09-20 10:30:19 -0700520 return 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400521}
522
Nadav Amit22d293e2018-09-20 10:30:19 -0700523/**
524 * vmballoon_send_guest_id - communicate guest type to the host.
525 *
526 * @b: pointer to the balloon.
527 *
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400528 * Communicate guest type to the host so that it can adjust ballooning
529 * algorithm to the one most appropriate for the guest. This command
530 * is normally issued after sending "start" command and is part of
531 * standard reset sequence.
Nadav Amit22d293e2018-09-20 10:30:19 -0700532 *
533 * Return: zero on success or appropriate error code.
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400534 */
Nadav Amit22d293e2018-09-20 10:30:19 -0700535static int vmballoon_send_guest_id(struct vmballoon *b)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400536{
Nadav Amit10a95d52018-09-20 10:30:07 -0700537 unsigned long status;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400538
Nadav Amit10a95d52018-09-20 10:30:07 -0700539 status = vmballoon_cmd(b, VMW_BALLOON_CMD_GUEST_ID,
540 VMW_BALLOON_GUEST_ID, 0);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400541
Nadav Amit22d293e2018-09-20 10:30:19 -0700542 return status == VMW_BALLOON_SUCCESS ? 0 : -EIO;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400543}
544
Nadav Amit6e4453b2018-09-20 10:30:18 -0700545/**
546 * vmballoon_page_order() - return the order of the page
547 * @page_size: the size of the page.
548 *
549 * Return: the allocation order.
550 */
551static inline
552unsigned int vmballoon_page_order(enum vmballoon_page_size_type page_size)
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700553{
Nadav Amit6e4453b2018-09-20 10:30:18 -0700554 return page_size == VMW_BALLOON_2M_PAGE ? VMW_BALLOON_2M_ORDER : 0;
555}
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700556
Nadav Amit6e4453b2018-09-20 10:30:18 -0700557/**
558 * vmballoon_page_in_frames() - returns the number of frames in a page.
559 * @page_size: the size of the page.
560 *
561 * Return: the number of 4k frames.
562 */
563static inline unsigned int
564vmballoon_page_in_frames(enum vmballoon_page_size_type page_size)
565{
566 return 1 << vmballoon_page_order(page_size);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700567}
568
Nadav Amit0395be32018-09-20 10:30:16 -0700569/**
570 * vmballoon_send_get_target() - Retrieve desired balloon size from the host.
571 *
572 * @b: pointer to the balloon.
573 *
574 * Return: zero on success, EINVAL if limit does not fit in 32-bit, as required
575 * by the host-guest protocol and EIO if an error occurred in communicating with
576 * the host.
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400577 */
Nadav Amit0395be32018-09-20 10:30:16 -0700578static int vmballoon_send_get_target(struct vmballoon *b)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400579{
580 unsigned long status;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400581 unsigned long limit;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400582
Arun KSca79b0c2018-12-28 00:34:29 -0800583 limit = totalram_pages();
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400584
Xavier Deguillard55398302019-02-06 15:57:02 -0800585 /* Ensure limit fits in 32-bits if 64-bit targets are not supported */
586 if (!(b->capabilities & VMW_BALLOON_64_BIT_TARGET) &&
587 limit != (u32)limit)
Nadav Amit0395be32018-09-20 10:30:16 -0700588 return -EINVAL;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400589
Nadav Amit10a95d52018-09-20 10:30:07 -0700590 status = vmballoon_cmd(b, VMW_BALLOON_CMD_GET_TARGET, limit, 0);
591
Nadav Amit0395be32018-09-20 10:30:16 -0700592 return status == VMW_BALLOON_SUCCESS ? 0 : -EIO;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400593}
594
Nadav Amit6e4453b2018-09-20 10:30:18 -0700595/**
596 * vmballoon_alloc_page_list - allocates a list of pages.
597 *
598 * @b: pointer to the balloon.
599 * @ctl: pointer for the %struct vmballoon_ctl, which defines the operation.
600 * @req_n_pages: the number of requested pages.
601 *
602 * Tries to allocate @req_n_pages. Add them to the list of balloon pages in
603 * @ctl.pages and updates @ctl.n_pages to reflect the number of pages.
604 *
605 * Return: zero on success or error code otherwise.
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400606 */
Nadav Amit6e4453b2018-09-20 10:30:18 -0700607static int vmballoon_alloc_page_list(struct vmballoon *b,
608 struct vmballoon_ctl *ctl,
609 unsigned int req_n_pages)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400610{
Nadav Amit6e4453b2018-09-20 10:30:18 -0700611 struct page *page;
612 unsigned int i;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400613
Nadav Amit6e4453b2018-09-20 10:30:18 -0700614 for (i = 0; i < req_n_pages; i++) {
615 if (ctl->page_size == VMW_BALLOON_2M_PAGE)
616 page = alloc_pages(VMW_HUGE_PAGE_ALLOC_FLAGS,
617 VMW_BALLOON_2M_ORDER);
618 else
619 page = alloc_page(VMW_PAGE_ALLOC_FLAGS);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700620
Nadav Amit6e4453b2018-09-20 10:30:18 -0700621 /* Update statistics */
622 vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_ALLOC,
623 ctl->page_size);
624
625 if (page) {
626 /* Success. Add the page to the list and continue. */
627 list_add(&page->lru, &ctl->pages);
628 continue;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700629 }
Nadav Amit6e4453b2018-09-20 10:30:18 -0700630
631 /* Allocation failed. Update statistics and stop. */
632 vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_ALLOC_FAIL,
633 ctl->page_size);
634 break;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400635 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400636
Nadav Amit6e4453b2018-09-20 10:30:18 -0700637 ctl->n_pages = i;
638
639 return req_n_pages == ctl->n_pages ? 0 : -ENOMEM;
640}
641
642/**
643 * vmballoon_handle_one_result - Handle lock/unlock result for a single page.
644 *
645 * @b: pointer for %struct vmballoon.
646 * @page: pointer for the page whose result should be handled.
647 * @page_size: size of the page.
648 * @status: status of the operation as provided by the hypervisor.
649 */
650static int vmballoon_handle_one_result(struct vmballoon *b, struct page *page,
651 enum vmballoon_page_size_type page_size,
652 unsigned long status)
653{
654 /* On success do nothing. The page is already on the balloon list. */
655 if (likely(status == VMW_BALLOON_SUCCESS))
656 return 0;
657
658 pr_debug("%s: failed comm pfn %lx status %lu page_size %s\n", __func__,
659 page_to_pfn(page), status,
660 vmballoon_page_size_names[page_size]);
661
662 /* Error occurred */
663 vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_REFUSED_ALLOC,
664 page_size);
665
666 return -EIO;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400667}
668
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700669/**
670 * vmballoon_status_page - returns the status of (un)lock operation
671 *
672 * @b: pointer to the balloon.
673 * @idx: index for the page for which the operation is performed.
674 * @p: pointer to where the page struct is returned.
675 *
676 * Following a lock or unlock operation, returns the status of the operation for
677 * an individual page. Provides the page that the operation was performed on on
678 * the @page argument.
679 *
680 * Returns: The status of a lock or unlock operation for an individual page.
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400681 */
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700682static unsigned long vmballoon_status_page(struct vmballoon *b, int idx,
683 struct page **p)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400684{
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700685 if (static_branch_likely(&vmw_balloon_batching)) {
686 /* batching mode */
687 *p = pfn_to_page(b->batch_page[idx].pfn);
688 return b->batch_page[idx].status;
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700689 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400690
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700691 /* non-batching mode */
692 *p = b->page;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400693
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700694 /*
695 * If a failure occurs, the indication will be provided in the status
696 * of the entire operation, which is considered before the individual
697 * page status. So for non-batching mode, the indication is always of
698 * success.
699 */
700 return VMW_BALLOON_SUCCESS;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400701}
702
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700703/**
704 * vmballoon_lock_op - notifies the host about inflated/deflated pages.
705 * @b: pointer to the balloon.
706 * @num_pages: number of inflated/deflated pages.
Nadav Amit6e4453b2018-09-20 10:30:18 -0700707 * @page_size: size of the page.
708 * @op: the type of operation (lock or unlock).
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700709 *
710 * Notify the host about page(s) that were ballooned (or removed from the
711 * balloon) so that host can use it without fear that guest will need it (or
712 * stop using them since the VM does). Host may reject some pages, we need to
713 * check the return value and maybe submit a different page. The pages that are
714 * inflated/deflated are pointed by @b->page.
715 *
716 * Return: result as provided by the hypervisor.
717 */
718static unsigned long vmballoon_lock_op(struct vmballoon *b,
719 unsigned int num_pages,
Nadav Amit6e4453b2018-09-20 10:30:18 -0700720 enum vmballoon_page_size_type page_size,
721 enum vmballoon_op op)
Xavier Deguillardf220a802015-08-06 15:17:58 -0700722{
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700723 unsigned long cmd, pfn;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700724
Nadav Amit6e4453b2018-09-20 10:30:18 -0700725 lockdep_assert_held(&b->comm_lock);
726
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700727 if (static_branch_likely(&vmw_balloon_batching)) {
Nadav Amit6e4453b2018-09-20 10:30:18 -0700728 if (op == VMW_BALLOON_INFLATE)
729 cmd = page_size == VMW_BALLOON_2M_PAGE ?
730 VMW_BALLOON_CMD_BATCHED_2M_LOCK :
731 VMW_BALLOON_CMD_BATCHED_LOCK;
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700732 else
Nadav Amit6e4453b2018-09-20 10:30:18 -0700733 cmd = page_size == VMW_BALLOON_2M_PAGE ?
734 VMW_BALLOON_CMD_BATCHED_2M_UNLOCK :
735 VMW_BALLOON_CMD_BATCHED_UNLOCK;
Nadav Amit10a95d52018-09-20 10:30:07 -0700736
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700737 pfn = PHYS_PFN(virt_to_phys(b->batch_page));
738 } else {
Nadav Amit6e4453b2018-09-20 10:30:18 -0700739 cmd = op == VMW_BALLOON_INFLATE ? VMW_BALLOON_CMD_LOCK :
740 VMW_BALLOON_CMD_UNLOCK;
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700741 pfn = page_to_pfn(b->page);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700742
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700743 /* In non-batching mode, PFNs must fit in 32-bit */
744 if (unlikely(pfn != (u32)pfn))
745 return VMW_BALLOON_ERROR_PPN_INVALID;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700746 }
747
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700748 return vmballoon_cmd(b, cmd, pfn, num_pages);
749}
750
Nadav Amit6e4453b2018-09-20 10:30:18 -0700751/**
752 * vmballoon_add_page - adds a page towards lock/unlock operation.
753 *
754 * @b: pointer to the balloon.
755 * @idx: index of the page to be ballooned in this batch.
756 * @p: pointer to the page that is about to be ballooned.
757 *
758 * Adds the page to be ballooned. Must be called while holding @comm_lock.
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400759 */
Nadav Amit6e4453b2018-09-20 10:30:18 -0700760static void vmballoon_add_page(struct vmballoon *b, unsigned int idx,
761 struct page *p)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400762{
Nadav Amit6e4453b2018-09-20 10:30:18 -0700763 lockdep_assert_held(&b->comm_lock);
764
765 if (static_branch_likely(&vmw_balloon_batching))
766 b->batch_page[idx] = (struct vmballoon_batch_entry)
767 { .pfn = page_to_pfn(p) };
768 else
769 b->page = p;
770}
771
772/**
773 * vmballoon_lock - lock or unlock a batch of pages.
774 *
775 * @b: pointer to the balloon.
776 * @ctl: pointer for the %struct vmballoon_ctl, which defines the operation.
777 *
778 * Notifies the host of about ballooned pages (after inflation or deflation,
779 * according to @ctl). If the host rejects the page put it on the
780 * @ctl refuse list. These refused page are then released when moving to the
781 * next size of pages.
782 *
783 * Note that we neither free any @page here nor put them back on the ballooned
784 * pages list. Instead we queue it for later processing. We do that for several
785 * reasons. First, we do not want to free the page under the lock. Second, it
786 * allows us to unify the handling of lock and unlock. In the inflate case, the
787 * caller will check if there are too many refused pages and release them.
788 * Although it is not identical to the past behavior, it should not affect
789 * performance.
790 */
791static int vmballoon_lock(struct vmballoon *b, struct vmballoon_ctl *ctl)
792{
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700793 unsigned long batch_status;
Nadav Amit6e4453b2018-09-20 10:30:18 -0700794 struct page *page;
795 unsigned int i, num_pages;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700796
Nadav Amit6e4453b2018-09-20 10:30:18 -0700797 num_pages = ctl->n_pages;
798 if (num_pages == 0)
799 return 0;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700800
Nadav Amit6e4453b2018-09-20 10:30:18 -0700801 /* communication with the host is done under the communication lock */
802 spin_lock(&b->comm_lock);
803
804 i = 0;
805 list_for_each_entry(page, &ctl->pages, lru)
806 vmballoon_add_page(b, i++, page);
807
808 batch_status = vmballoon_lock_op(b, ctl->n_pages, ctl->page_size,
809 ctl->op);
810
811 /*
812 * Iterate over the pages in the provided list. Since we are changing
813 * @ctl->n_pages we are saving the original value in @num_pages and
814 * use this value to bound the loop.
815 */
Xavier Deguillardf220a802015-08-06 15:17:58 -0700816 for (i = 0; i < num_pages; i++) {
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700817 unsigned long status;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700818
Nadav Amit6e4453b2018-09-20 10:30:18 -0700819 status = vmballoon_status_page(b, i, &page);
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700820
821 /*
822 * Failure of the whole batch overrides a single operation
823 * results.
824 */
825 if (batch_status != VMW_BALLOON_SUCCESS)
826 status = batch_status;
827
Nadav Amit6e4453b2018-09-20 10:30:18 -0700828 /* Continue if no error happened */
829 if (!vmballoon_handle_one_result(b, page, ctl->page_size,
830 status))
831 continue;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700832
Nadav Amit6e4453b2018-09-20 10:30:18 -0700833 /*
834 * Error happened. Move the pages to the refused list and update
835 * the pages number.
836 */
837 list_move(&page->lru, &ctl->refused_pages);
838 ctl->n_pages--;
839 ctl->n_refused_pages++;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700840 }
841
Nadav Amit6e4453b2018-09-20 10:30:18 -0700842 spin_unlock(&b->comm_lock);
843
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700844 return batch_status == VMW_BALLOON_SUCCESS ? 0 : -EIO;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700845}
846
Nadav Amit6e4453b2018-09-20 10:30:18 -0700847/**
848 * vmballoon_release_page_list() - Releases a page list
849 *
850 * @page_list: list of pages to release.
851 * @n_pages: pointer to the number of pages.
852 * @page_size: whether the pages in the list are 2MB (or else 4KB).
853 *
854 * Releases the list of pages and zeros the number of pages.
855 */
856static void vmballoon_release_page_list(struct list_head *page_list,
857 int *n_pages,
858 enum vmballoon_page_size_type page_size)
859{
860 struct page *page, *tmp;
861
862 list_for_each_entry_safe(page, tmp, page_list, lru) {
863 list_del(&page->lru);
864 __free_pages(page, vmballoon_page_order(page_size));
865 }
866
867 *n_pages = 0;
868}
869
870
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400871/*
872 * Release pages that were allocated while attempting to inflate the
873 * balloon but were refused by the host for one reason or another.
874 */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700875static void vmballoon_release_refused_pages(struct vmballoon *b,
Nadav Amit6e4453b2018-09-20 10:30:18 -0700876 struct vmballoon_ctl *ctl)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400877{
Nadav Amit6e4453b2018-09-20 10:30:18 -0700878 vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_REFUSED_FREE,
879 ctl->page_size);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400880
Nadav Amit6e4453b2018-09-20 10:30:18 -0700881 vmballoon_release_page_list(&ctl->refused_pages, &ctl->n_refused_pages,
882 ctl->page_size);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700883}
884
Nadav Amit8b079cd2018-09-20 10:30:15 -0700885/**
886 * vmballoon_change - retrieve the required balloon change
887 *
888 * @b: pointer for the balloon.
889 *
890 * Return: the required change for the balloon size. A positive number
891 * indicates inflation, a negative number indicates a deflation.
892 */
893static int64_t vmballoon_change(struct vmballoon *b)
894{
895 int64_t size, target;
896
Nadav Amit6e4453b2018-09-20 10:30:18 -0700897 size = atomic64_read(&b->size);
898 target = READ_ONCE(b->target);
Nadav Amit8b079cd2018-09-20 10:30:15 -0700899
900 /*
901 * We must cast first because of int sizes
902 * Otherwise we might get huge positives instead of negatives
903 */
904
905 if (b->reset_required)
906 return 0;
907
908 /* consider a 2MB slack on deflate, unless the balloon is emptied */
Nadav Amit6e4453b2018-09-20 10:30:18 -0700909 if (target < size && target != 0 &&
910 size - target < vmballoon_page_in_frames(VMW_BALLOON_2M_PAGE))
Nadav Amit8b079cd2018-09-20 10:30:15 -0700911 return 0;
912
913 return target - size;
914}
915
Nadav Amit6e4453b2018-09-20 10:30:18 -0700916/**
917 * vmballoon_enqueue_page_list() - Enqueues list of pages after inflation.
918 *
919 * @b: pointer to balloon.
920 * @pages: list of pages to enqueue.
921 * @n_pages: pointer to number of pages in list. The value is zeroed.
922 * @page_size: whether the pages are 2MB or 4KB pages.
923 *
924 * Enqueues the provides list of pages in the ballooned page list, clears the
925 * list and zeroes the number of pages that was provided.
926 */
927static void vmballoon_enqueue_page_list(struct vmballoon *b,
928 struct list_head *pages,
929 unsigned int *n_pages,
930 enum vmballoon_page_size_type page_size)
931{
932 struct vmballoon_page_size *page_size_info = &b->page_sizes[page_size];
933
934 list_splice_init(pages, &page_size_info->pages);
935 *n_pages = 0;
936}
937
938/**
939 * vmballoon_dequeue_page_list() - Dequeues page lists for deflation.
940 *
941 * @b: pointer to balloon.
942 * @pages: list of pages to enqueue.
943 * @n_pages: pointer to number of pages in list. The value is zeroed.
944 * @page_size: whether the pages are 2MB or 4KB pages.
945 * @n_req_pages: the number of requested pages.
946 *
947 * Dequeues the number of requested pages from the balloon for deflation. The
948 * number of dequeued pages may be lower, if not enough pages in the requested
949 * size are available.
950 */
951static void vmballoon_dequeue_page_list(struct vmballoon *b,
952 struct list_head *pages,
953 unsigned int *n_pages,
954 enum vmballoon_page_size_type page_size,
955 unsigned int n_req_pages)
956{
957 struct vmballoon_page_size *page_size_info = &b->page_sizes[page_size];
958 struct page *page, *tmp;
959 unsigned int i = 0;
960
961 list_for_each_entry_safe(page, tmp, &page_size_info->pages, lru) {
962 list_move(&page->lru, pages);
963 if (++i == n_req_pages)
964 break;
965 }
966 *n_pages = i;
967}
968
969/**
970 * vmballoon_inflate() - Inflate the balloon towards its target size.
971 *
972 * @b: pointer to the balloon.
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400973 */
974static void vmballoon_inflate(struct vmballoon *b)
975{
Nadav Amit6e4453b2018-09-20 10:30:18 -0700976 int64_t to_inflate_frames;
977 struct vmballoon_ctl ctl = {
978 .pages = LIST_HEAD_INIT(ctl.pages),
979 .refused_pages = LIST_HEAD_INIT(ctl.refused_pages),
980 .page_size = b->max_page_size,
981 .op = VMW_BALLOON_INFLATE
982 };
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400983
Nadav Amit6e4453b2018-09-20 10:30:18 -0700984 while ((to_inflate_frames = vmballoon_change(b)) > 0) {
985 unsigned int to_inflate_pages, page_in_frames;
986 int alloc_error, lock_error = 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400987
Nadav Amit6e4453b2018-09-20 10:30:18 -0700988 VM_BUG_ON(!list_empty(&ctl.pages));
989 VM_BUG_ON(ctl.n_pages != 0);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400990
Nadav Amit6e4453b2018-09-20 10:30:18 -0700991 page_in_frames = vmballoon_page_in_frames(ctl.page_size);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400992
Nadav Amit6e4453b2018-09-20 10:30:18 -0700993 to_inflate_pages = min_t(unsigned long, b->batch_max_pages,
994 DIV_ROUND_UP_ULL(to_inflate_frames,
995 page_in_frames));
Nadav Amitc7b36902018-09-20 10:30:17 -0700996
Nadav Amit6e4453b2018-09-20 10:30:18 -0700997 /* Start by allocating */
998 alloc_error = vmballoon_alloc_page_list(b, &ctl,
999 to_inflate_pages);
Nadav Amitc7b36902018-09-20 10:30:17 -07001000
Nadav Amit6e4453b2018-09-20 10:30:18 -07001001 /* Actually lock the pages by telling the hypervisor */
1002 lock_error = vmballoon_lock(b, &ctl);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001003
Nadav Amit6e4453b2018-09-20 10:30:18 -07001004 /*
1005 * If an error indicates that something serious went wrong,
1006 * stop the inflation.
1007 */
1008 if (lock_error)
Nadav Amit622074a2018-09-20 10:30:11 -07001009 break;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001010
Nadav Amit6e4453b2018-09-20 10:30:18 -07001011 /* Update the balloon size */
1012 atomic64_add(ctl.n_pages * page_in_frames, &b->size);
Nadav Amit8fa3c612018-09-20 10:30:13 -07001013
Nadav Amit6e4453b2018-09-20 10:30:18 -07001014 vmballoon_enqueue_page_list(b, &ctl.pages, &ctl.n_pages,
1015 ctl.page_size);
Nadav Amit10a95d52018-09-20 10:30:07 -07001016
Nadav Amit6e4453b2018-09-20 10:30:18 -07001017 /*
1018 * If allocation failed or the number of refused pages exceeds
1019 * the maximum allowed, move to the next page size.
1020 */
1021 if (alloc_error ||
1022 ctl.n_refused_pages >= VMW_BALLOON_MAX_REFUSED) {
1023 if (ctl.page_size == VMW_BALLOON_4K_PAGE)
1024 break;
Nadav Amit8fa3c612018-09-20 10:30:13 -07001025
1026 /*
Nadav Amit6e4453b2018-09-20 10:30:18 -07001027 * Ignore errors from locking as we now switch to 4k
1028 * pages and we might get different errors.
Nadav Amit8fa3c612018-09-20 10:30:13 -07001029 */
Nadav Amit6e4453b2018-09-20 10:30:18 -07001030 vmballoon_release_refused_pages(b, &ctl);
1031 ctl.page_size--;
Xavier Deguillardf220a802015-08-06 15:17:58 -07001032 }
Xavier Deguillardef0f8f12015-06-12 11:43:22 -07001033
Philip P. Moltmann33d268e2015-08-06 15:18:01 -07001034 cond_resched();
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001035 }
1036
Nadav Amit6e4453b2018-09-20 10:30:18 -07001037 /*
1038 * Release pages that were allocated while attempting to inflate the
1039 * balloon but were refused by the host for one reason or another,
1040 * and update the statistics.
1041 */
1042 if (ctl.n_refused_pages != 0)
1043 vmballoon_release_refused_pages(b, &ctl);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001044}
1045
Nadav Amit6e4453b2018-09-20 10:30:18 -07001046/**
1047 * vmballoon_deflate() - Decrease the size of the balloon.
1048 *
1049 * @b: pointer to the balloon
1050 * @n_frames: the number of frames to deflate. If zero, automatically
1051 * calculated according to the target size.
1052 * @coordinated: whether to coordinate with the host
1053 *
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001054 * Decrease the size of the balloon allowing guest to use more memory.
Nadav Amit6e4453b2018-09-20 10:30:18 -07001055 *
1056 * Return: The number of deflated frames (i.e., basic page size units)
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001057 */
Nadav Amit6e4453b2018-09-20 10:30:18 -07001058static unsigned long vmballoon_deflate(struct vmballoon *b, uint64_t n_frames,
1059 bool coordinated)
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001060{
Nadav Amit6e4453b2018-09-20 10:30:18 -07001061 unsigned long deflated_frames = 0;
1062 unsigned long tried_frames = 0;
1063 struct vmballoon_ctl ctl = {
1064 .pages = LIST_HEAD_INIT(ctl.pages),
1065 .refused_pages = LIST_HEAD_INIT(ctl.refused_pages),
1066 .page_size = VMW_BALLOON_4K_PAGE,
1067 .op = VMW_BALLOON_DEFLATE
1068 };
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001069
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001070 /* free pages to reach target */
Nadav Amit6e4453b2018-09-20 10:30:18 -07001071 while (true) {
1072 unsigned int to_deflate_pages, n_unlocked_frames;
1073 unsigned int page_in_frames;
1074 int64_t to_deflate_frames;
1075 bool deflated_all;
Xavier Deguillardf220a802015-08-06 15:17:58 -07001076
Nadav Amit6e4453b2018-09-20 10:30:18 -07001077 page_in_frames = vmballoon_page_in_frames(ctl.page_size);
1078
1079 VM_BUG_ON(!list_empty(&ctl.pages));
1080 VM_BUG_ON(ctl.n_pages);
1081 VM_BUG_ON(!list_empty(&ctl.refused_pages));
1082 VM_BUG_ON(ctl.n_refused_pages);
1083
1084 /*
1085 * If we were requested a specific number of frames, we try to
1086 * deflate this number of frames. Otherwise, deflation is
1087 * performed according to the target and balloon size.
1088 */
1089 to_deflate_frames = n_frames ? n_frames - tried_frames :
1090 -vmballoon_change(b);
1091
1092 /* break if no work to do */
1093 if (to_deflate_frames <= 0)
1094 break;
1095
1096 /*
1097 * Calculate the number of frames based on current page size,
1098 * but limit the deflated frames to a single chunk
1099 */
1100 to_deflate_pages = min_t(unsigned long, b->batch_max_pages,
1101 DIV_ROUND_UP_ULL(to_deflate_frames,
1102 page_in_frames));
1103
1104 /* First take the pages from the balloon pages. */
1105 vmballoon_dequeue_page_list(b, &ctl.pages, &ctl.n_pages,
1106 ctl.page_size, to_deflate_pages);
1107
1108 /*
1109 * Before pages are moving to the refused list, count their
1110 * frames as frames that we tried to deflate.
1111 */
1112 tried_frames += ctl.n_pages * page_in_frames;
1113
1114 /*
1115 * Unlock the pages by communicating with the hypervisor if the
1116 * communication is coordinated (i.e., not pop). We ignore the
1117 * return code. Instead we check if all the pages we manage to
1118 * unlock all the pages. If we failed, we will move to the next
1119 * page size, and would eventually try again later.
1120 */
1121 if (coordinated)
1122 vmballoon_lock(b, &ctl);
1123
1124 /*
1125 * Check if we deflated enough. We will move to the next page
1126 * size if we did not manage to do so. This calculation takes
1127 * place now, as once the pages are released, the number of
1128 * pages is zeroed.
1129 */
1130 deflated_all = (ctl.n_pages == to_deflate_pages);
1131
1132 /* Update local and global counters */
1133 n_unlocked_frames = ctl.n_pages * page_in_frames;
1134 atomic64_sub(n_unlocked_frames, &b->size);
1135 deflated_frames += n_unlocked_frames;
1136
1137 vmballoon_stats_page_add(b, VMW_BALLOON_PAGE_STAT_FREE,
1138 ctl.page_size, ctl.n_pages);
1139
1140 /* free the ballooned pages */
1141 vmballoon_release_page_list(&ctl.pages, &ctl.n_pages,
1142 ctl.page_size);
1143
1144 /* Return the refused pages to the ballooned list. */
1145 vmballoon_enqueue_page_list(b, &ctl.refused_pages,
1146 &ctl.n_refused_pages,
1147 ctl.page_size);
1148
1149 /* If we failed to unlock all the pages, move to next size. */
1150 if (!deflated_all) {
1151 if (ctl.page_size == b->max_page_size)
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001152 break;
Nadav Amit6e4453b2018-09-20 10:30:18 -07001153 ctl.page_size++;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001154 }
1155
Nadav Amit6e4453b2018-09-20 10:30:18 -07001156 cond_resched();
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001157 }
Nadav Amit6e4453b2018-09-20 10:30:18 -07001158
1159 return deflated_frames;
Xavier Deguillardf220a802015-08-06 15:17:58 -07001160}
1161
Nadav Amitdf8d0d42018-09-20 10:30:12 -07001162/**
1163 * vmballoon_deinit_batching - disables batching mode.
1164 *
1165 * @b: pointer to &struct vmballoon.
1166 *
1167 * Disables batching, by deallocating the page for communication with the
1168 * hypervisor and disabling the static key to indicate that batching is off.
1169 */
1170static void vmballoon_deinit_batching(struct vmballoon *b)
1171{
1172 free_page((unsigned long)b->batch_page);
1173 b->batch_page = NULL;
1174 static_branch_disable(&vmw_balloon_batching);
1175 b->batch_max_pages = 1;
1176}
Xavier Deguillardf220a802015-08-06 15:17:58 -07001177
Nadav Amitdf8d0d42018-09-20 10:30:12 -07001178/**
1179 * vmballoon_init_batching - enable batching mode.
1180 *
1181 * @b: pointer to &struct vmballoon.
1182 *
1183 * Enables batching, by allocating a page for communication with the hypervisor
1184 * and enabling the static_key to use batching.
1185 *
1186 * Return: zero on success or an appropriate error-code.
1187 */
1188static int vmballoon_init_batching(struct vmballoon *b)
Xavier Deguillardf220a802015-08-06 15:17:58 -07001189{
Gil Kupferb23220f2018-06-01 00:47:47 -07001190 struct page *page;
1191
1192 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1193 if (!page)
Nadav Amitdf8d0d42018-09-20 10:30:12 -07001194 return -ENOMEM;
Xavier Deguillardf220a802015-08-06 15:17:58 -07001195
Gil Kupferb23220f2018-06-01 00:47:47 -07001196 b->batch_page = page_address(page);
Nadav Amitdf8d0d42018-09-20 10:30:12 -07001197 b->batch_max_pages = PAGE_SIZE / sizeof(struct vmballoon_batch_entry);
1198
1199 static_branch_enable(&vmw_balloon_batching);
1200
1201 return 0;
Xavier Deguillardf220a802015-08-06 15:17:58 -07001202}
1203
1204/*
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001205 * Receive notification and resize balloon
1206 */
1207static void vmballoon_doorbell(void *client_data)
1208{
1209 struct vmballoon *b = client_data;
1210
Nadav Amitc7b36902018-09-20 10:30:17 -07001211 vmballoon_stats_gen_inc(b, VMW_BALLOON_STAT_DOORBELL);
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001212
1213 mod_delayed_work(system_freezable_wq, &b->dwork, 0);
1214}
1215
1216/*
1217 * Clean up vmci doorbell
1218 */
1219static void vmballoon_vmci_cleanup(struct vmballoon *b)
1220{
Nadav Amit10a95d52018-09-20 10:30:07 -07001221 vmballoon_cmd(b, VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
1222 VMCI_INVALID_ID, VMCI_INVALID_ID);
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001223
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001224 if (!vmci_handle_is_invalid(b->vmci_doorbell)) {
1225 vmci_doorbell_destroy(b->vmci_doorbell);
1226 b->vmci_doorbell = VMCI_INVALID_HANDLE;
1227 }
1228}
1229
Nadav Amit22d293e2018-09-20 10:30:19 -07001230/**
1231 * vmballoon_vmci_init - Initialize vmci doorbell.
1232 *
1233 * @b: pointer to the balloon.
1234 *
1235 * Return: zero on success or when wakeup command not supported. Error-code
1236 * otherwise.
1237 *
1238 * Initialize vmci doorbell, to get notified as soon as balloon changes.
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001239 */
1240static int vmballoon_vmci_init(struct vmballoon *b)
1241{
Nadav Amit10a95d52018-09-20 10:30:07 -07001242 unsigned long error;
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001243
Nadav Amitce664332018-06-19 16:00:26 -07001244 if ((b->capabilities & VMW_BALLOON_SIGNALLED_WAKEUP_CMD) == 0)
1245 return 0;
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001246
Nadav Amitce664332018-06-19 16:00:26 -07001247 error = vmci_doorbell_create(&b->vmci_doorbell, VMCI_FLAG_DELAYED_CB,
1248 VMCI_PRIVILEGE_FLAG_RESTRICTED,
1249 vmballoon_doorbell, b);
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001250
Nadav Amitce664332018-06-19 16:00:26 -07001251 if (error != VMCI_SUCCESS)
1252 goto fail;
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001253
Nadav Amit10a95d52018-09-20 10:30:07 -07001254 error = __vmballoon_cmd(b, VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
1255 b->vmci_doorbell.context,
1256 b->vmci_doorbell.resource, NULL);
Nadav Amitce664332018-06-19 16:00:26 -07001257
Nadav Amitce664332018-06-19 16:00:26 -07001258 if (error != VMW_BALLOON_SUCCESS)
1259 goto fail;
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001260
1261 return 0;
Nadav Amitce664332018-06-19 16:00:26 -07001262fail:
1263 vmballoon_vmci_cleanup(b);
1264 return -EIO;
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001265}
1266
Nadav Amit6e4453b2018-09-20 10:30:18 -07001267/**
1268 * vmballoon_pop - Quickly release all pages allocate for the balloon.
1269 *
1270 * @b: pointer to the balloon.
1271 *
1272 * This function is called when host decides to "reset" balloon for one reason
1273 * or another. Unlike normal "deflate" we do not (shall not) notify host of the
1274 * pages being released.
1275 */
1276static void vmballoon_pop(struct vmballoon *b)
1277{
1278 unsigned long size;
1279
1280 while ((size = atomic64_read(&b->size)))
1281 vmballoon_deflate(b, size, false);
1282}
1283
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001284/*
Xavier Deguillardf220a802015-08-06 15:17:58 -07001285 * Perform standard reset sequence by popping the balloon (in case it
1286 * is not empty) and then restarting protocol. This operation normally
1287 * happens when host responds with VMW_BALLOON_ERROR_RESET to a command.
1288 */
1289static void vmballoon_reset(struct vmballoon *b)
1290{
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001291 int error;
1292
Nadav Amitc7b36902018-09-20 10:30:17 -07001293 down_write(&b->conf_sem);
1294
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001295 vmballoon_vmci_cleanup(b);
1296
Xavier Deguillardf220a802015-08-06 15:17:58 -07001297 /* free all pages, skipping monitor unlock */
1298 vmballoon_pop(b);
1299
Nadav Amit22d293e2018-09-20 10:30:19 -07001300 if (vmballoon_send_start(b, VMW_BALLOON_CAPABILITIES))
Dan Carpenterd04071a2019-02-11 21:45:45 +03001301 goto unlock;
Xavier Deguillardf220a802015-08-06 15:17:58 -07001302
1303 if ((b->capabilities & VMW_BALLOON_BATCHED_CMDS) != 0) {
Nadav Amitdf8d0d42018-09-20 10:30:12 -07001304 if (vmballoon_init_batching(b)) {
Xavier Deguillardf220a802015-08-06 15:17:58 -07001305 /*
1306 * We failed to initialize batching, inform the monitor
1307 * about it by sending a null capability.
1308 *
1309 * The guest will retry in one second.
1310 */
1311 vmballoon_send_start(b, 0);
Dan Carpenterd04071a2019-02-11 21:45:45 +03001312 goto unlock;
Xavier Deguillardf220a802015-08-06 15:17:58 -07001313 }
1314 } else if ((b->capabilities & VMW_BALLOON_BASIC_CMDS) != 0) {
Nadav Amitdf8d0d42018-09-20 10:30:12 -07001315 vmballoon_deinit_batching(b);
Xavier Deguillardf220a802015-08-06 15:17:58 -07001316 }
1317
Nadav Amit8840a6f2018-09-20 10:30:20 -07001318 vmballoon_stats_gen_inc(b, VMW_BALLOON_STAT_RESET);
Xavier Deguillardf220a802015-08-06 15:17:58 -07001319 b->reset_required = false;
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001320
1321 error = vmballoon_vmci_init(b);
1322 if (error)
1323 pr_err("failed to initialize vmci doorbell\n");
1324
Nadav Amit22d293e2018-09-20 10:30:19 -07001325 if (vmballoon_send_guest_id(b))
Xavier Deguillardf220a802015-08-06 15:17:58 -07001326 pr_err("failed to send guest ID to the host\n");
Nadav Amitc7b36902018-09-20 10:30:17 -07001327
Dan Carpenterd04071a2019-02-11 21:45:45 +03001328unlock:
Nadav Amitc7b36902018-09-20 10:30:17 -07001329 up_write(&b->conf_sem);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001330}
1331
Nadav Amit8b079cd2018-09-20 10:30:15 -07001332/**
1333 * vmballoon_work - periodic balloon worker for reset, inflation and deflation.
1334 *
1335 * @work: pointer to the &work_struct which is provided by the workqueue.
1336 *
1337 * Resets the protocol if needed, gets the new size and adjusts balloon as
1338 * needed. Repeat in 1 sec.
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001339 */
1340static void vmballoon_work(struct work_struct *work)
1341{
1342 struct delayed_work *dwork = to_delayed_work(work);
1343 struct vmballoon *b = container_of(dwork, struct vmballoon, dwork);
Nadav Amit8b079cd2018-09-20 10:30:15 -07001344 int64_t change = 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001345
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001346 if (b->reset_required)
1347 vmballoon_reset(b);
1348
Nadav Amitc7b36902018-09-20 10:30:17 -07001349 down_read(&b->conf_sem);
1350
1351 /*
1352 * Update the stats while holding the semaphore to ensure that
1353 * @stats_enabled is consistent with whether the stats are actually
1354 * enabled
1355 */
1356 vmballoon_stats_gen_inc(b, VMW_BALLOON_STAT_TIMER);
1357
Nadav Amit0395be32018-09-20 10:30:16 -07001358 if (!vmballoon_send_get_target(b))
Nadav Amit8b079cd2018-09-20 10:30:15 -07001359 change = vmballoon_change(b);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001360
Nadav Amit8b079cd2018-09-20 10:30:15 -07001361 if (change != 0) {
Nadav Amit6e4453b2018-09-20 10:30:18 -07001362 pr_debug("%s - size: %llu, target %lu\n", __func__,
1363 atomic64_read(&b->size), READ_ONCE(b->target));
Nadav Amit8b079cd2018-09-20 10:30:15 -07001364
1365 if (change > 0)
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001366 vmballoon_inflate(b);
Nadav Amit8b079cd2018-09-20 10:30:15 -07001367 else /* (change < 0) */
Nadav Amit6e4453b2018-09-20 10:30:18 -07001368 vmballoon_deflate(b, 0, true);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001369 }
1370
Nadav Amitc7b36902018-09-20 10:30:17 -07001371 up_read(&b->conf_sem);
1372
Dmitry Torokhovbeda94d2011-07-26 16:08:56 -07001373 /*
1374 * We are using a freezable workqueue so that balloon operations are
1375 * stopped while the system transitions to/from sleep/hibernation.
1376 */
1377 queue_delayed_work(system_freezable_wq,
1378 dwork, round_jiffies_relative(HZ));
Nadav Amitc7b36902018-09-20 10:30:17 -07001379
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001380}
1381
1382/*
1383 * DEBUGFS Interface
1384 */
1385#ifdef CONFIG_DEBUG_FS
1386
Nadav Amitc7b36902018-09-20 10:30:17 -07001387static const char * const vmballoon_stat_page_names[] = {
1388 [VMW_BALLOON_PAGE_STAT_ALLOC] = "alloc",
1389 [VMW_BALLOON_PAGE_STAT_ALLOC_FAIL] = "allocFail",
1390 [VMW_BALLOON_PAGE_STAT_REFUSED_ALLOC] = "errAlloc",
1391 [VMW_BALLOON_PAGE_STAT_REFUSED_FREE] = "errFree",
1392 [VMW_BALLOON_PAGE_STAT_FREE] = "free"
1393};
1394
1395static const char * const vmballoon_stat_names[] = {
1396 [VMW_BALLOON_STAT_TIMER] = "timer",
Nadav Amit8840a6f2018-09-20 10:30:20 -07001397 [VMW_BALLOON_STAT_DOORBELL] = "doorbell",
1398 [VMW_BALLOON_STAT_RESET] = "reset",
Nadav Amitc7b36902018-09-20 10:30:17 -07001399};
1400
Nadav Amitc7b36902018-09-20 10:30:17 -07001401static int vmballoon_enable_stats(struct vmballoon *b)
1402{
1403 int r = 0;
1404
1405 down_write(&b->conf_sem);
1406
1407 /* did we somehow race with another reader which enabled stats? */
1408 if (b->stats)
1409 goto out;
1410
1411 b->stats = kzalloc(sizeof(*b->stats), GFP_KERNEL);
1412
1413 if (!b->stats) {
1414 /* allocation failed */
1415 r = -ENOMEM;
1416 goto out;
1417 }
1418 static_key_enable(&balloon_stat_enabled.key);
1419out:
1420 up_write(&b->conf_sem);
1421 return r;
1422}
1423
1424/**
1425 * vmballoon_debug_show - shows statistics of balloon operations.
1426 * @f: pointer to the &struct seq_file.
1427 * @offset: ignored.
1428 *
1429 * Provides the statistics that can be accessed in vmmemctl in the debugfs.
1430 * To avoid the overhead - mainly that of memory - of collecting the statistics,
1431 * we only collect statistics after the first time the counters are read.
1432 *
1433 * Return: zero on success or an error code.
1434 */
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001435static int vmballoon_debug_show(struct seq_file *f, void *offset)
1436{
1437 struct vmballoon *b = f->private;
Nadav Amitc7b36902018-09-20 10:30:17 -07001438 int i, j;
1439
1440 /* enables stats if they are disabled */
1441 if (!b->stats) {
1442 int r = vmballoon_enable_stats(b);
1443
1444 if (r)
1445 return r;
1446 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001447
Philip P. Moltmannb36e89d2015-08-06 15:18:00 -07001448 /* format capabilities info */
Nadav Amit6e4453b2018-09-20 10:30:18 -07001449 seq_printf(f, "%-22s: %#16x\n", "balloon capabilities",
Nadav Amitc7b36902018-09-20 10:30:17 -07001450 VMW_BALLOON_CAPABILITIES);
Nadav Amit6e4453b2018-09-20 10:30:18 -07001451 seq_printf(f, "%-22s: %#16lx\n", "used capabilities", b->capabilities);
Nadav Amitc7b36902018-09-20 10:30:17 -07001452 seq_printf(f, "%-22s: %16s\n", "is resetting",
1453 b->reset_required ? "y" : "n");
Philip P. Moltmannb36e89d2015-08-06 15:18:00 -07001454
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001455 /* format size info */
Nadav Amit6e4453b2018-09-20 10:30:18 -07001456 seq_printf(f, "%-22s: %16lu\n", "target", READ_ONCE(b->target));
1457 seq_printf(f, "%-22s: %16llu\n", "current", atomic64_read(&b->size));
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001458
Nadav Amit68131182018-09-20 10:30:08 -07001459 for (i = 0; i < VMW_BALLOON_CMD_NUM; i++) {
1460 if (vmballoon_cmd_names[i] == NULL)
1461 continue;
1462
Nadav Amitc7b36902018-09-20 10:30:17 -07001463 seq_printf(f, "%-22s: %16llu (%llu failed)\n",
1464 vmballoon_cmd_names[i],
1465 atomic64_read(&b->stats->ops[i][VMW_BALLOON_OP_STAT]),
1466 atomic64_read(&b->stats->ops[i][VMW_BALLOON_OP_FAIL_STAT]));
Nadav Amit68131182018-09-20 10:30:08 -07001467 }
1468
Nadav Amitc7b36902018-09-20 10:30:17 -07001469 for (i = 0; i < VMW_BALLOON_STAT_NUM; i++)
1470 seq_printf(f, "%-22s: %16llu\n",
1471 vmballoon_stat_names[i],
1472 atomic64_read(&b->stats->general_stat[i]));
1473
1474 for (i = 0; i < VMW_BALLOON_PAGE_STAT_NUM; i++) {
1475 for (j = 0; j < VMW_BALLOON_NUM_PAGE_SIZES; j++)
1476 seq_printf(f, "%-18s(%s): %16llu\n",
1477 vmballoon_stat_page_names[i],
1478 vmballoon_page_size_names[j],
1479 atomic64_read(&b->stats->page_stat[i][j]));
1480 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001481
1482 return 0;
1483}
1484
Yangtao Li2796b432018-12-01 12:05:30 -05001485DEFINE_SHOW_ATTRIBUTE(vmballoon_debug);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001486
1487static int __init vmballoon_debugfs_init(struct vmballoon *b)
1488{
1489 int error;
1490
1491 b->dbg_entry = debugfs_create_file("vmmemctl", S_IRUGO, NULL, b,
1492 &vmballoon_debug_fops);
1493 if (IS_ERR(b->dbg_entry)) {
1494 error = PTR_ERR(b->dbg_entry);
1495 pr_err("failed to create debugfs entry, error: %d\n", error);
1496 return error;
1497 }
1498
1499 return 0;
1500}
1501
1502static void __exit vmballoon_debugfs_exit(struct vmballoon *b)
1503{
Nadav Amitc7b36902018-09-20 10:30:17 -07001504 static_key_disable(&balloon_stat_enabled.key);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001505 debugfs_remove(b->dbg_entry);
Nadav Amitc7b36902018-09-20 10:30:17 -07001506 kfree(b->stats);
1507 b->stats = NULL;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001508}
1509
1510#else
1511
1512static inline int vmballoon_debugfs_init(struct vmballoon *b)
1513{
1514 return 0;
1515}
1516
1517static inline void vmballoon_debugfs_exit(struct vmballoon *b)
1518{
1519}
1520
1521#endif /* CONFIG_DEBUG_FS */
1522
1523static int __init vmballoon_init(void)
1524{
Nadav Amit6e4453b2018-09-20 10:30:18 -07001525 enum vmballoon_page_size_type page_size;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001526 int error;
Nadav Amit6e4453b2018-09-20 10:30:18 -07001527
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001528 /*
1529 * Check if we are running on VMware's hypervisor and bail out
1530 * if we are not.
1531 */
Juergen Gross03b2a322017-11-09 14:27:36 +01001532 if (x86_hyper_type != X86_HYPER_VMWARE)
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001533 return -ENODEV;
1534
Nadav Amit6e4453b2018-09-20 10:30:18 -07001535 for (page_size = VMW_BALLOON_4K_PAGE;
1536 page_size <= VMW_BALLOON_LAST_SIZE; page_size++)
1537 INIT_LIST_HEAD(&balloon.page_sizes[page_size].pages);
1538
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001539
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001540 INIT_DELAYED_WORK(&balloon.dwork, vmballoon_work);
1541
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001542 error = vmballoon_debugfs_init(&balloon);
1543 if (error)
Dmitry Torokhovbeda94d2011-07-26 16:08:56 -07001544 return error;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001545
Nadav Amit6e4453b2018-09-20 10:30:18 -07001546 spin_lock_init(&balloon.comm_lock);
Nadav Amitc7b36902018-09-20 10:30:17 -07001547 init_rwsem(&balloon.conf_sem);
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001548 balloon.vmci_doorbell = VMCI_INVALID_HANDLE;
Philip P. Moltmannd7568c12015-08-06 15:18:01 -07001549 balloon.batch_page = NULL;
1550 balloon.page = NULL;
1551 balloon.reset_required = true;
1552
Dmitry Torokhovbeda94d2011-07-26 16:08:56 -07001553 queue_delayed_work(system_freezable_wq, &balloon.dwork, 0);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001554
1555 return 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001556}
Nadav Amitc3cc1b02018-06-19 16:00:27 -07001557
1558/*
1559 * Using late_initcall() instead of module_init() allows the balloon to use the
1560 * VMCI doorbell even when the balloon is built into the kernel. Otherwise the
1561 * VMCI is probed only after the balloon is initialized. If the balloon is used
1562 * as a module, late_initcall() is equivalent to module_init().
1563 */
1564late_initcall(vmballoon_init);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001565
1566static void __exit vmballoon_exit(void)
1567{
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001568 vmballoon_vmci_cleanup(&balloon);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001569 cancel_delayed_work_sync(&balloon.dwork);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001570
1571 vmballoon_debugfs_exit(&balloon);
1572
1573 /*
1574 * Deallocate all reserved memory, and reset connection with monitor.
1575 * Reset connection before deallocating memory to avoid potential for
1576 * additional spurious resets from guest touching deallocated pages.
1577 */
Philip P. Moltmannd7568c12015-08-06 15:18:01 -07001578 vmballoon_send_start(&balloon, 0);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001579 vmballoon_pop(&balloon);
1580}
1581module_exit(vmballoon_exit);