blob: 99a831ea82c0e9fdf91c7483172ce446673be3c4 [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");
Philip P. Moltmann48e3d662015-08-06 15:18:01 -070037MODULE_VERSION("1.5.0.0-k");
Dmitry Torokhov453dc652010-04-23 13:18:08 -040038MODULE_ALIAS("dmi:*:svnVMware*:*");
39MODULE_ALIAS("vmware_vmmemctl");
40MODULE_LICENSE("GPL");
41
42/*
Nadav Amit622074a2018-09-20 10:30:11 -070043 * Use __GFP_HIGHMEM to allow pages from HIGHMEM zone. We don't allow wait
44 * (__GFP_RECLAIM) for huge page allocations. Use __GFP_NOWARN, to suppress page
45 * allocation failure warnings. Disallow access to emergency low-memory pools.
Dmitry Torokhov453dc652010-04-23 13:18:08 -040046 */
Nadav Amit622074a2018-09-20 10:30:11 -070047#define VMW_HUGE_PAGE_ALLOC_FLAGS (__GFP_HIGHMEM|__GFP_NOWARN| \
48 __GFP_NOMEMALLOC)
Dmitry Torokhov453dc652010-04-23 13:18:08 -040049
50/*
Nadav Amit622074a2018-09-20 10:30:11 -070051 * Use __GFP_HIGHMEM to allow pages from HIGHMEM zone. We allow lightweight
52 * reclamation (__GFP_NORETRY). Use __GFP_NOWARN, to suppress page allocation
53 * failure warnings. Disallow access to emergency low-memory pools.
Dmitry Torokhov453dc652010-04-23 13:18:08 -040054 */
Nadav Amit622074a2018-09-20 10:30:11 -070055#define VMW_PAGE_ALLOC_FLAGS (__GFP_HIGHMEM|__GFP_NOWARN| \
56 __GFP_NOMEMALLOC|__GFP_NORETRY)
Dmitry Torokhov453dc652010-04-23 13:18:08 -040057
Dmitry Torokhov55adaa42010-06-04 14:14:52 -070058/* Maximum number of refused pages we accumulate during inflation cycle */
59#define VMW_BALLOON_MAX_REFUSED 16
Dmitry Torokhov453dc652010-04-23 13:18:08 -040060
61/*
62 * Hypervisor communication port definitions.
63 */
64#define VMW_BALLOON_HV_PORT 0x5670
65#define VMW_BALLOON_HV_MAGIC 0x456c6d6f
Dmitry Torokhov453dc652010-04-23 13:18:08 -040066#define VMW_BALLOON_GUEST_ID 1 /* Linux */
67
Xavier Deguillardeb791002015-06-12 11:43:23 -070068enum vmwballoon_capabilities {
69 /*
70 * Bit 0 is reserved and not associated to any capability.
71 */
Philip P. Moltmann48e3d662015-08-06 15:18:01 -070072 VMW_BALLOON_BASIC_CMDS = (1 << 1),
73 VMW_BALLOON_BATCHED_CMDS = (1 << 2),
74 VMW_BALLOON_BATCHED_2M_CMDS = (1 << 3),
75 VMW_BALLOON_SIGNALLED_WAKEUP_CMD = (1 << 4),
Xavier Deguillardeb791002015-06-12 11:43:23 -070076};
77
Xavier Deguillardf220a802015-08-06 15:17:58 -070078#define VMW_BALLOON_CAPABILITIES (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
Nadav Amitc7b36902018-09-20 10:30:17 -070085enum vmballoon_page_size_type {
86 VMW_BALLOON_4K_PAGE,
87 VMW_BALLOON_2M_PAGE,
88 VMW_BALLOON_LAST_SIZE = VMW_BALLOON_2M_PAGE
89};
90
91#define VMW_BALLOON_NUM_PAGE_SIZES (VMW_BALLOON_LAST_SIZE + 1)
92
Nadav Amit6e4453b2018-09-20 10:30:18 -070093static const char * const vmballoon_page_size_names[] = {
94 [VMW_BALLOON_4K_PAGE] = "4k",
95 [VMW_BALLOON_2M_PAGE] = "2M"
96};
97
98enum vmballoon_op {
99 VMW_BALLOON_INFLATE,
100 VMW_BALLOON_DEFLATE
101};
102
Nadav Amitc7b36902018-09-20 10:30:17 -0700103enum vmballoon_op_stat_type {
104 VMW_BALLOON_OP_STAT,
105 VMW_BALLOON_OP_FAIL_STAT
106};
107
108#define VMW_BALLOON_OP_STAT_TYPES (VMW_BALLOON_OP_FAIL_STAT + 1)
109
110/**
111 * enum vmballoon_cmd_type - backdoor commands.
Xavier Deguillardf220a802015-08-06 15:17:58 -0700112 *
Nadav Amitc7b36902018-09-20 10:30:17 -0700113 * Availability of the commands is as followed:
Xavier Deguillardf220a802015-08-06 15:17:58 -0700114 *
Nadav Amitc7b36902018-09-20 10:30:17 -0700115 * %VMW_BALLOON_CMD_START, %VMW_BALLOON_CMD_GET_TARGET and
116 * %VMW_BALLOON_CMD_GUEST_ID are always available.
117 *
118 * If the host reports %VMW_BALLOON_BASIC_CMDS are supported then
119 * %VMW_BALLOON_CMD_LOCK and %VMW_BALLOON_CMD_UNLOCK commands are available.
120 *
121 * If the host reports %VMW_BALLOON_BATCHED_CMDS are supported then
122 * %VMW_BALLOON_CMD_BATCHED_LOCK and VMW_BALLOON_CMD_BATCHED_UNLOCK commands
123 * are available.
124 *
125 * If the host reports %VMW_BALLOON_BATCHED_2M_CMDS are supported then
126 * %VMW_BALLOON_CMD_BATCHED_2M_LOCK and %VMW_BALLOON_CMD_BATCHED_2M_UNLOCK
127 * are supported.
128 *
129 * If the host reports VMW_BALLOON_SIGNALLED_WAKEUP_CMD is supported then
130 * VMW_BALLOON_CMD_VMCI_DOORBELL_SET command is supported.
131 *
132 * @VMW_BALLOON_CMD_START: Communicating supported version with the hypervisor.
133 * @VMW_BALLOON_CMD_GET_TARGET: Gets the balloon target size.
134 * @VMW_BALLOON_CMD_LOCK: Informs the hypervisor about a ballooned page.
135 * @VMW_BALLOON_CMD_UNLOCK: Informs the hypervisor about a page that is about
136 * to be deflated from the balloon.
137 * @VMW_BALLOON_CMD_GUEST_ID: Informs the hypervisor about the type of OS that
138 * runs in the VM.
139 * @VMW_BALLOON_CMD_BATCHED_LOCK: Inform the hypervisor about a batch of
140 * ballooned pages (up to 512).
141 * @VMW_BALLOON_CMD_BATCHED_UNLOCK: Inform the hypervisor about a batch of
142 * pages that are about to be deflated from the
143 * balloon (up to 512).
144 * @VMW_BALLOON_CMD_BATCHED_2M_LOCK: Similar to @VMW_BALLOON_CMD_BATCHED_LOCK
145 * for 2MB pages.
146 * @VMW_BALLOON_CMD_BATCHED_2M_UNLOCK: Similar to
147 * @VMW_BALLOON_CMD_BATCHED_UNLOCK for 2MB
148 * pages.
149 * @VMW_BALLOON_CMD_VMCI_DOORBELL_SET: A command to set doorbell notification
150 * that would be invoked when the balloon
151 * size changes.
152 * @VMW_BALLOON_CMD_LAST: Value of the last command.
Xavier Deguillardf220a802015-08-06 15:17:58 -0700153 */
Nadav Amitc7b36902018-09-20 10:30:17 -0700154enum vmballoon_cmd_type {
155 VMW_BALLOON_CMD_START,
156 VMW_BALLOON_CMD_GET_TARGET,
157 VMW_BALLOON_CMD_LOCK,
158 VMW_BALLOON_CMD_UNLOCK,
159 VMW_BALLOON_CMD_GUEST_ID,
160 /* No command 5 */
161 VMW_BALLOON_CMD_BATCHED_LOCK = 6,
162 VMW_BALLOON_CMD_BATCHED_UNLOCK,
163 VMW_BALLOON_CMD_BATCHED_2M_LOCK,
164 VMW_BALLOON_CMD_BATCHED_2M_UNLOCK,
165 VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
166 VMW_BALLOON_CMD_LAST = VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
167};
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700168
Nadav Amitc7b36902018-09-20 10:30:17 -0700169#define VMW_BALLOON_CMD_NUM (VMW_BALLOON_CMD_LAST + 1)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400170
Nadav Amitc7b36902018-09-20 10:30:17 -0700171enum vmballoon_error_codes {
172 VMW_BALLOON_SUCCESS,
173 VMW_BALLOON_ERROR_CMD_INVALID,
174 VMW_BALLOON_ERROR_PPN_INVALID,
175 VMW_BALLOON_ERROR_PPN_LOCKED,
176 VMW_BALLOON_ERROR_PPN_UNLOCKED,
177 VMW_BALLOON_ERROR_PPN_PINNED,
178 VMW_BALLOON_ERROR_PPN_NOTNEEDED,
179 VMW_BALLOON_ERROR_RESET,
180 VMW_BALLOON_ERROR_BUSY
181};
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400182
Xavier Deguillardeb791002015-06-12 11:43:23 -0700183#define VMW_BALLOON_SUCCESS_WITH_CAPABILITIES (0x03000000)
184
Nadav Amit10a95d52018-09-20 10:30:07 -0700185#define VMW_BALLOON_CMD_WITH_TARGET_MASK \
186 ((1UL << VMW_BALLOON_CMD_GET_TARGET) | \
187 (1UL << VMW_BALLOON_CMD_LOCK) | \
188 (1UL << VMW_BALLOON_CMD_UNLOCK) | \
189 (1UL << VMW_BALLOON_CMD_BATCHED_LOCK) | \
190 (1UL << VMW_BALLOON_CMD_BATCHED_UNLOCK) | \
191 (1UL << VMW_BALLOON_CMD_BATCHED_2M_LOCK) | \
192 (1UL << VMW_BALLOON_CMD_BATCHED_2M_UNLOCK))
193
Nadav Amit68131182018-09-20 10:30:08 -0700194static const char * const vmballoon_cmd_names[] = {
195 [VMW_BALLOON_CMD_START] = "start",
196 [VMW_BALLOON_CMD_GET_TARGET] = "target",
197 [VMW_BALLOON_CMD_LOCK] = "lock",
198 [VMW_BALLOON_CMD_UNLOCK] = "unlock",
199 [VMW_BALLOON_CMD_GUEST_ID] = "guestType",
200 [VMW_BALLOON_CMD_BATCHED_LOCK] = "batchLock",
201 [VMW_BALLOON_CMD_BATCHED_UNLOCK] = "batchUnlock",
202 [VMW_BALLOON_CMD_BATCHED_2M_LOCK] = "2m-lock",
203 [VMW_BALLOON_CMD_BATCHED_2M_UNLOCK] = "2m-unlock",
204 [VMW_BALLOON_CMD_VMCI_DOORBELL_SET] = "doorbellSet"
205};
206
Nadav Amitc7b36902018-09-20 10:30:17 -0700207enum vmballoon_stat_page {
208 VMW_BALLOON_PAGE_STAT_ALLOC,
209 VMW_BALLOON_PAGE_STAT_ALLOC_FAIL,
210 VMW_BALLOON_PAGE_STAT_REFUSED_ALLOC,
211 VMW_BALLOON_PAGE_STAT_REFUSED_FREE,
212 VMW_BALLOON_PAGE_STAT_FREE,
213 VMW_BALLOON_PAGE_STAT_LAST = VMW_BALLOON_PAGE_STAT_FREE
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400214};
215
Nadav Amitc7b36902018-09-20 10:30:17 -0700216#define VMW_BALLOON_PAGE_STAT_NUM (VMW_BALLOON_PAGE_STAT_LAST + 1)
217
218enum vmballoon_stat_general {
219 VMW_BALLOON_STAT_TIMER,
220 VMW_BALLOON_STAT_DOORBELL,
221 VMW_BALLOON_STAT_LAST = VMW_BALLOON_STAT_DOORBELL
222};
223
224#define VMW_BALLOON_STAT_NUM (VMW_BALLOON_STAT_LAST + 1)
225
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400226
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700227static DEFINE_STATIC_KEY_TRUE(vmw_balloon_batching);
Nadav Amitc7b36902018-09-20 10:30:17 -0700228static DEFINE_STATIC_KEY_FALSE(balloon_stat_enabled);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700229
Nadav Amit6e4453b2018-09-20 10:30:18 -0700230struct vmballoon_ctl {
231 struct list_head pages;
232 struct list_head refused_pages;
233 unsigned int n_refused_pages;
234 unsigned int n_pages;
235 enum vmballoon_page_size_type page_size;
236 enum vmballoon_op op;
237};
238
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700239struct vmballoon_page_size {
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400240 /* list of reserved physical pages */
241 struct list_head pages;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700242};
243
Nadav Amit6c948752018-09-20 10:30:10 -0700244/**
245 * struct vmballoon_batch_entry - a batch entry for lock or unlock.
246 *
247 * @status: the status of the operation, which is written by the hypervisor.
248 * @reserved: reserved for future use. Must be set to zero.
249 * @pfn: the physical frame number of the page to be locked or unlocked.
250 */
251struct vmballoon_batch_entry {
252 u64 status : 5;
253 u64 reserved : PAGE_SHIFT - 5;
254 u64 pfn : 52;
255} __packed;
256
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700257struct vmballoon {
258 struct vmballoon_page_size page_sizes[VMW_BALLOON_NUM_PAGE_SIZES];
259
Nadav Amit6e4453b2018-09-20 10:30:18 -0700260 /**
261 * @max_page_size: maximum supported page size for ballooning.
262 *
263 * Protected by @conf_sem
264 */
265 enum vmballoon_page_size_type max_page_size;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400266
Nadav Amit6e4453b2018-09-20 10:30:18 -0700267 /**
268 * @size: balloon actual size in basic page size (frames).
269 *
270 * While we currently do not support size which is bigger than 32-bit,
271 * in preparation for future support, use 64-bits.
272 */
273 atomic64_t size;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400274
Nadav Amit6e4453b2018-09-20 10:30:18 -0700275 /**
276 * @target: balloon target size in basic page size (frames).
277 *
278 * We do not protect the target under the assumption that setting the
279 * value is always done through a single write. If this assumption ever
280 * breaks, we would have to use X_ONCE for accesses, and suffer the less
281 * optimized code. Although we may read stale target value if multiple
282 * accesses happen at once, the performance impact should be minor.
283 */
284 unsigned long target;
285
286 /**
287 * @reset_required: reset flag
288 *
289 * Setting this flag may introduce races, but the code is expected to
290 * handle them gracefully. In the worst case, another operation will
291 * fail as reset did not take place. Clearing the flag is done while
292 * holding @conf_sem for write.
293 */
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400294 bool reset_required;
295
Nadav Amit6e4453b2018-09-20 10:30:18 -0700296 /**
297 * @capabilities: hypervisor balloon capabilities.
298 *
299 * Protected by @conf_sem.
300 */
Xavier Deguillardf220a802015-08-06 15:17:58 -0700301 unsigned long capabilities;
302
Nadav Amit6c948752018-09-20 10:30:10 -0700303 /**
304 * @batch_page: pointer to communication batch page.
305 *
306 * When batching is used, batch_page points to a page, which holds up to
307 * %VMW_BALLOON_BATCH_MAX_PAGES entries for locking or unlocking.
308 */
309 struct vmballoon_batch_entry *batch_page;
310
Nadav Amit6e4453b2018-09-20 10:30:18 -0700311 /**
312 * @batch_max_pages: maximum pages that can be locked/unlocked.
313 *
314 * Indicates the number of pages that the hypervisor can lock or unlock
315 * at once, according to whether batching is enabled. If batching is
316 * disabled, only a single page can be locked/unlock on each operation.
317 *
318 * Protected by @conf_sem.
319 */
Xavier Deguillardf220a802015-08-06 15:17:58 -0700320 unsigned int batch_max_pages;
Nadav Amit6e4453b2018-09-20 10:30:18 -0700321
322 /**
323 * @page: page to be locked/unlocked by the hypervisor
324 *
325 * @page is only used when batching is disabled and a single page is
326 * reclaimed on each iteration.
327 *
328 * Protected by @comm_lock.
329 */
Xavier Deguillardf220a802015-08-06 15:17:58 -0700330 struct page *page;
331
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400332 /* statistics */
Nadav Amitc7b36902018-09-20 10:30:17 -0700333 struct vmballoon_stats *stats;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400334
Nadav Amitc7b36902018-09-20 10:30:17 -0700335#ifdef CONFIG_DEBUG_FS
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400336 /* debugfs file exporting statistics */
337 struct dentry *dbg_entry;
338#endif
339
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400340 struct delayed_work dwork;
Philip P. Moltmann48e3d662015-08-06 15:18:01 -0700341
Nadav Amit6e4453b2018-09-20 10:30:18 -0700342 /**
343 * @vmci_doorbell.
344 *
345 * Protected by @conf_sem.
346 */
Philip P. Moltmann48e3d662015-08-06 15:18:01 -0700347 struct vmci_handle vmci_doorbell;
Nadav Amitc7b36902018-09-20 10:30:17 -0700348
349 /**
350 * @conf_sem: semaphore to protect the configuration and the statistics.
351 */
352 struct rw_semaphore conf_sem;
Nadav Amit6e4453b2018-09-20 10:30:18 -0700353
354 /**
355 * @comm_lock: lock to protect the communication with the host.
356 *
357 * Lock ordering: @conf_sem -> @comm_lock .
358 */
359 spinlock_t comm_lock;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400360};
361
362static struct vmballoon balloon;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400363
Nadav Amitc7b36902018-09-20 10:30:17 -0700364struct vmballoon_stats {
365 /* timer / doorbell operations */
366 atomic64_t general_stat[VMW_BALLOON_STAT_NUM];
367
368 /* allocation statistics for huge and small pages */
369 atomic64_t
370 page_stat[VMW_BALLOON_PAGE_STAT_NUM][VMW_BALLOON_NUM_PAGE_SIZES];
371
372 /* Monitor operations: total operations, and failures */
373 atomic64_t ops[VMW_BALLOON_CMD_NUM][VMW_BALLOON_OP_STAT_TYPES];
374};
375
376static inline bool is_vmballoon_stats_on(void)
377{
378 return IS_ENABLED(CONFIG_DEBUG_FS) &&
379 static_branch_unlikely(&balloon_stat_enabled);
380}
381
382static inline void vmballoon_stats_op_inc(struct vmballoon *b, unsigned int op,
383 enum vmballoon_op_stat_type type)
384{
385 if (is_vmballoon_stats_on())
386 atomic64_inc(&b->stats->ops[op][type]);
387}
388
389static inline void vmballoon_stats_gen_inc(struct vmballoon *b,
390 enum vmballoon_stat_general stat)
391{
392 if (is_vmballoon_stats_on())
393 atomic64_inc(&b->stats->general_stat[stat]);
394}
395
396static inline void vmballoon_stats_gen_add(struct vmballoon *b,
397 enum vmballoon_stat_general stat,
398 unsigned int val)
399{
400 if (is_vmballoon_stats_on())
401 atomic64_add(val, &b->stats->general_stat[stat]);
402}
403
404static inline void vmballoon_stats_page_inc(struct vmballoon *b,
405 enum vmballoon_stat_page stat,
Nadav Amit6e4453b2018-09-20 10:30:18 -0700406 enum vmballoon_page_size_type size)
Nadav Amitc7b36902018-09-20 10:30:17 -0700407{
408 if (is_vmballoon_stats_on())
Nadav Amit6e4453b2018-09-20 10:30:18 -0700409 atomic64_inc(&b->stats->page_stat[stat][size]);
410}
411
412static inline void vmballoon_stats_page_add(struct vmballoon *b,
413 enum vmballoon_stat_page stat,
414 enum vmballoon_page_size_type size,
415 unsigned int val)
416{
417 if (is_vmballoon_stats_on())
418 atomic64_add(val, &b->stats->page_stat[stat][size]);
Nadav Amitc7b36902018-09-20 10:30:17 -0700419}
420
Nadav Amit10a95d52018-09-20 10:30:07 -0700421static inline unsigned long
422__vmballoon_cmd(struct vmballoon *b, unsigned long cmd, unsigned long arg1,
423 unsigned long arg2, unsigned long *result)
424{
425 unsigned long status, dummy1, dummy2, dummy3, local_result;
426
Nadav Amitc7b36902018-09-20 10:30:17 -0700427 vmballoon_stats_op_inc(b, cmd, VMW_BALLOON_OP_STAT);
Nadav Amit68131182018-09-20 10:30:08 -0700428
Nadav Amit10a95d52018-09-20 10:30:07 -0700429 asm volatile ("inl %%dx" :
430 "=a"(status),
431 "=c"(dummy1),
432 "=d"(dummy2),
433 "=b"(local_result),
434 "=S"(dummy3) :
435 "0"(VMW_BALLOON_HV_MAGIC),
436 "1"(cmd),
437 "2"(VMW_BALLOON_HV_PORT),
438 "3"(arg1),
439 "4"(arg2) :
440 "memory");
441
442 /* update the result if needed */
443 if (result)
444 *result = (cmd == VMW_BALLOON_CMD_START) ? dummy1 :
445 local_result;
446
447 /* update target when applicable */
448 if (status == VMW_BALLOON_SUCCESS &&
449 ((1ul << cmd) & VMW_BALLOON_CMD_WITH_TARGET_MASK))
Nadav Amit6e4453b2018-09-20 10:30:18 -0700450 WRITE_ONCE(b->target, local_result);
Nadav Amit10a95d52018-09-20 10:30:07 -0700451
Nadav Amit68131182018-09-20 10:30:08 -0700452 if (status != VMW_BALLOON_SUCCESS &&
453 status != VMW_BALLOON_SUCCESS_WITH_CAPABILITIES) {
Nadav Amitc7b36902018-09-20 10:30:17 -0700454 vmballoon_stats_op_inc(b, cmd, VMW_BALLOON_OP_FAIL_STAT);
Nadav Amit68131182018-09-20 10:30:08 -0700455 pr_debug("%s: %s [0x%lx,0x%lx) failed, returned %ld\n",
456 __func__, vmballoon_cmd_names[cmd], arg1, arg2,
457 status);
458 }
459
Nadav Amit10a95d52018-09-20 10:30:07 -0700460 /* mark reset required accordingly */
461 if (status == VMW_BALLOON_ERROR_RESET)
462 b->reset_required = true;
463
464 return status;
465}
466
467static __always_inline unsigned long
468vmballoon_cmd(struct vmballoon *b, unsigned long cmd, unsigned long arg1,
469 unsigned long arg2)
470{
471 unsigned long dummy;
472
473 return __vmballoon_cmd(b, cmd, arg1, arg2, &dummy);
474}
475
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400476/*
477 * Send "start" command to the host, communicating supported version
478 * of the protocol.
479 */
Nadav Amit22d293e2018-09-20 10:30:19 -0700480static int vmballoon_send_start(struct vmballoon *b, unsigned long req_caps)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400481{
Nadav Amit10a95d52018-09-20 10:30:07 -0700482 unsigned long status, capabilities;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400483
Nadav Amit10a95d52018-09-20 10:30:07 -0700484 status = __vmballoon_cmd(b, VMW_BALLOON_CMD_START, req_caps, 0,
485 &capabilities);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700486
487 switch (status) {
488 case VMW_BALLOON_SUCCESS_WITH_CAPABILITIES:
489 b->capabilities = capabilities;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700490 break;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700491 case VMW_BALLOON_SUCCESS:
492 b->capabilities = VMW_BALLOON_BASIC_CMDS;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700493 break;
494 default:
Nadav Amit22d293e2018-09-20 10:30:19 -0700495 return -EIO;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700496 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400497
Nadav Amit5081efd2018-06-19 16:00:25 -0700498 /*
499 * 2MB pages are only supported with batching. If batching is for some
500 * reason disabled, do not use 2MB pages, since otherwise the legacy
501 * mechanism is used with 2MB pages, causing a failure.
502 */
Nadav Amit6e4453b2018-09-20 10:30:18 -0700503 b->max_page_size = VMW_BALLOON_4K_PAGE;
Nadav Amit5081efd2018-06-19 16:00:25 -0700504 if ((b->capabilities & VMW_BALLOON_BATCHED_2M_CMDS) &&
505 (b->capabilities & VMW_BALLOON_BATCHED_CMDS))
Nadav Amit6e4453b2018-09-20 10:30:18 -0700506 b->max_page_size = VMW_BALLOON_2M_PAGE;
507
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700508
Nadav Amit22d293e2018-09-20 10:30:19 -0700509 return 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400510}
511
Nadav Amit22d293e2018-09-20 10:30:19 -0700512/**
513 * vmballoon_send_guest_id - communicate guest type to the host.
514 *
515 * @b: pointer to the balloon.
516 *
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400517 * Communicate guest type to the host so that it can adjust ballooning
518 * algorithm to the one most appropriate for the guest. This command
519 * is normally issued after sending "start" command and is part of
520 * standard reset sequence.
Nadav Amit22d293e2018-09-20 10:30:19 -0700521 *
522 * Return: zero on success or appropriate error code.
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400523 */
Nadav Amit22d293e2018-09-20 10:30:19 -0700524static int vmballoon_send_guest_id(struct vmballoon *b)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400525{
Nadav Amit10a95d52018-09-20 10:30:07 -0700526 unsigned long status;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400527
Nadav Amit10a95d52018-09-20 10:30:07 -0700528 status = vmballoon_cmd(b, VMW_BALLOON_CMD_GUEST_ID,
529 VMW_BALLOON_GUEST_ID, 0);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400530
Nadav Amit22d293e2018-09-20 10:30:19 -0700531 return status == VMW_BALLOON_SUCCESS ? 0 : -EIO;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400532}
533
Nadav Amit6e4453b2018-09-20 10:30:18 -0700534/**
535 * vmballoon_page_order() - return the order of the page
536 * @page_size: the size of the page.
537 *
538 * Return: the allocation order.
539 */
540static inline
541unsigned int vmballoon_page_order(enum vmballoon_page_size_type page_size)
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700542{
Nadav Amit6e4453b2018-09-20 10:30:18 -0700543 return page_size == VMW_BALLOON_2M_PAGE ? VMW_BALLOON_2M_ORDER : 0;
544}
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700545
Nadav Amit6e4453b2018-09-20 10:30:18 -0700546/**
547 * vmballoon_page_in_frames() - returns the number of frames in a page.
548 * @page_size: the size of the page.
549 *
550 * Return: the number of 4k frames.
551 */
552static inline unsigned int
553vmballoon_page_in_frames(enum vmballoon_page_size_type page_size)
554{
555 return 1 << vmballoon_page_order(page_size);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700556}
557
Nadav Amit0395be32018-09-20 10:30:16 -0700558/**
559 * vmballoon_send_get_target() - Retrieve desired balloon size from the host.
560 *
561 * @b: pointer to the balloon.
562 *
563 * Return: zero on success, EINVAL if limit does not fit in 32-bit, as required
564 * by the host-guest protocol and EIO if an error occurred in communicating with
565 * the host.
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400566 */
Nadav Amit0395be32018-09-20 10:30:16 -0700567static int vmballoon_send_get_target(struct vmballoon *b)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400568{
569 unsigned long status;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400570 unsigned long limit;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400571
Nadav Amit0395be32018-09-20 10:30:16 -0700572 limit = totalram_pages;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400573
574 /* Ensure limit fits in 32-bits */
Nadav Amit0395be32018-09-20 10:30:16 -0700575 if (limit != (u32)limit)
576 return -EINVAL;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400577
Nadav Amit10a95d52018-09-20 10:30:07 -0700578 status = vmballoon_cmd(b, VMW_BALLOON_CMD_GET_TARGET, limit, 0);
579
Nadav Amit0395be32018-09-20 10:30:16 -0700580 return status == VMW_BALLOON_SUCCESS ? 0 : -EIO;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400581}
582
Nadav Amit6e4453b2018-09-20 10:30:18 -0700583/**
584 * vmballoon_alloc_page_list - allocates a list of pages.
585 *
586 * @b: pointer to the balloon.
587 * @ctl: pointer for the %struct vmballoon_ctl, which defines the operation.
588 * @req_n_pages: the number of requested pages.
589 *
590 * Tries to allocate @req_n_pages. Add them to the list of balloon pages in
591 * @ctl.pages and updates @ctl.n_pages to reflect the number of pages.
592 *
593 * Return: zero on success or error code otherwise.
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400594 */
Nadav Amit6e4453b2018-09-20 10:30:18 -0700595static int vmballoon_alloc_page_list(struct vmballoon *b,
596 struct vmballoon_ctl *ctl,
597 unsigned int req_n_pages)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400598{
Nadav Amit6e4453b2018-09-20 10:30:18 -0700599 struct page *page;
600 unsigned int i;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400601
Nadav Amit6e4453b2018-09-20 10:30:18 -0700602 for (i = 0; i < req_n_pages; i++) {
603 if (ctl->page_size == VMW_BALLOON_2M_PAGE)
604 page = alloc_pages(VMW_HUGE_PAGE_ALLOC_FLAGS,
605 VMW_BALLOON_2M_ORDER);
606 else
607 page = alloc_page(VMW_PAGE_ALLOC_FLAGS);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700608
Nadav Amit6e4453b2018-09-20 10:30:18 -0700609 /* Update statistics */
610 vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_ALLOC,
611 ctl->page_size);
612
613 if (page) {
614 /* Success. Add the page to the list and continue. */
615 list_add(&page->lru, &ctl->pages);
616 continue;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700617 }
Nadav Amit6e4453b2018-09-20 10:30:18 -0700618
619 /* Allocation failed. Update statistics and stop. */
620 vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_ALLOC_FAIL,
621 ctl->page_size);
622 break;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400623 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400624
Nadav Amit6e4453b2018-09-20 10:30:18 -0700625 ctl->n_pages = i;
626
627 return req_n_pages == ctl->n_pages ? 0 : -ENOMEM;
628}
629
630/**
631 * vmballoon_handle_one_result - Handle lock/unlock result for a single page.
632 *
633 * @b: pointer for %struct vmballoon.
634 * @page: pointer for the page whose result should be handled.
635 * @page_size: size of the page.
636 * @status: status of the operation as provided by the hypervisor.
637 */
638static int vmballoon_handle_one_result(struct vmballoon *b, struct page *page,
639 enum vmballoon_page_size_type page_size,
640 unsigned long status)
641{
642 /* On success do nothing. The page is already on the balloon list. */
643 if (likely(status == VMW_BALLOON_SUCCESS))
644 return 0;
645
646 pr_debug("%s: failed comm pfn %lx status %lu page_size %s\n", __func__,
647 page_to_pfn(page), status,
648 vmballoon_page_size_names[page_size]);
649
650 /* Error occurred */
651 vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_REFUSED_ALLOC,
652 page_size);
653
654 return -EIO;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400655}
656
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700657/**
658 * vmballoon_status_page - returns the status of (un)lock operation
659 *
660 * @b: pointer to the balloon.
661 * @idx: index for the page for which the operation is performed.
662 * @p: pointer to where the page struct is returned.
663 *
664 * Following a lock or unlock operation, returns the status of the operation for
665 * an individual page. Provides the page that the operation was performed on on
666 * the @page argument.
667 *
668 * Returns: The status of a lock or unlock operation for an individual page.
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400669 */
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700670static unsigned long vmballoon_status_page(struct vmballoon *b, int idx,
671 struct page **p)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400672{
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700673 if (static_branch_likely(&vmw_balloon_batching)) {
674 /* batching mode */
675 *p = pfn_to_page(b->batch_page[idx].pfn);
676 return b->batch_page[idx].status;
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700677 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400678
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700679 /* non-batching mode */
680 *p = b->page;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400681
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700682 /*
683 * If a failure occurs, the indication will be provided in the status
684 * of the entire operation, which is considered before the individual
685 * page status. So for non-batching mode, the indication is always of
686 * success.
687 */
688 return VMW_BALLOON_SUCCESS;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400689}
690
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700691/**
692 * vmballoon_lock_op - notifies the host about inflated/deflated pages.
693 * @b: pointer to the balloon.
694 * @num_pages: number of inflated/deflated pages.
Nadav Amit6e4453b2018-09-20 10:30:18 -0700695 * @page_size: size of the page.
696 * @op: the type of operation (lock or unlock).
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700697 *
698 * Notify the host about page(s) that were ballooned (or removed from the
699 * balloon) so that host can use it without fear that guest will need it (or
700 * stop using them since the VM does). Host may reject some pages, we need to
701 * check the return value and maybe submit a different page. The pages that are
702 * inflated/deflated are pointed by @b->page.
703 *
704 * Return: result as provided by the hypervisor.
705 */
706static unsigned long vmballoon_lock_op(struct vmballoon *b,
707 unsigned int num_pages,
Nadav Amit6e4453b2018-09-20 10:30:18 -0700708 enum vmballoon_page_size_type page_size,
709 enum vmballoon_op op)
Xavier Deguillardf220a802015-08-06 15:17:58 -0700710{
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700711 unsigned long cmd, pfn;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700712
Nadav Amit6e4453b2018-09-20 10:30:18 -0700713 lockdep_assert_held(&b->comm_lock);
714
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700715 if (static_branch_likely(&vmw_balloon_batching)) {
Nadav Amit6e4453b2018-09-20 10:30:18 -0700716 if (op == VMW_BALLOON_INFLATE)
717 cmd = page_size == VMW_BALLOON_2M_PAGE ?
718 VMW_BALLOON_CMD_BATCHED_2M_LOCK :
719 VMW_BALLOON_CMD_BATCHED_LOCK;
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700720 else
Nadav Amit6e4453b2018-09-20 10:30:18 -0700721 cmd = page_size == VMW_BALLOON_2M_PAGE ?
722 VMW_BALLOON_CMD_BATCHED_2M_UNLOCK :
723 VMW_BALLOON_CMD_BATCHED_UNLOCK;
Nadav Amit10a95d52018-09-20 10:30:07 -0700724
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700725 pfn = PHYS_PFN(virt_to_phys(b->batch_page));
726 } else {
Nadav Amit6e4453b2018-09-20 10:30:18 -0700727 cmd = op == VMW_BALLOON_INFLATE ? VMW_BALLOON_CMD_LOCK :
728 VMW_BALLOON_CMD_UNLOCK;
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700729 pfn = page_to_pfn(b->page);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700730
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700731 /* In non-batching mode, PFNs must fit in 32-bit */
732 if (unlikely(pfn != (u32)pfn))
733 return VMW_BALLOON_ERROR_PPN_INVALID;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700734 }
735
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700736 return vmballoon_cmd(b, cmd, pfn, num_pages);
737}
738
Nadav Amit6e4453b2018-09-20 10:30:18 -0700739/**
740 * vmballoon_add_page - adds a page towards lock/unlock operation.
741 *
742 * @b: pointer to the balloon.
743 * @idx: index of the page to be ballooned in this batch.
744 * @p: pointer to the page that is about to be ballooned.
745 *
746 * Adds the page to be ballooned. Must be called while holding @comm_lock.
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400747 */
Nadav Amit6e4453b2018-09-20 10:30:18 -0700748static void vmballoon_add_page(struct vmballoon *b, unsigned int idx,
749 struct page *p)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400750{
Nadav Amit6e4453b2018-09-20 10:30:18 -0700751 lockdep_assert_held(&b->comm_lock);
752
753 if (static_branch_likely(&vmw_balloon_batching))
754 b->batch_page[idx] = (struct vmballoon_batch_entry)
755 { .pfn = page_to_pfn(p) };
756 else
757 b->page = p;
758}
759
760/**
761 * vmballoon_lock - lock or unlock a batch of pages.
762 *
763 * @b: pointer to the balloon.
764 * @ctl: pointer for the %struct vmballoon_ctl, which defines the operation.
765 *
766 * Notifies the host of about ballooned pages (after inflation or deflation,
767 * according to @ctl). If the host rejects the page put it on the
768 * @ctl refuse list. These refused page are then released when moving to the
769 * next size of pages.
770 *
771 * Note that we neither free any @page here nor put them back on the ballooned
772 * pages list. Instead we queue it for later processing. We do that for several
773 * reasons. First, we do not want to free the page under the lock. Second, it
774 * allows us to unify the handling of lock and unlock. In the inflate case, the
775 * caller will check if there are too many refused pages and release them.
776 * Although it is not identical to the past behavior, it should not affect
777 * performance.
778 */
779static int vmballoon_lock(struct vmballoon *b, struct vmballoon_ctl *ctl)
780{
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700781 unsigned long batch_status;
Nadav Amit6e4453b2018-09-20 10:30:18 -0700782 struct page *page;
783 unsigned int i, num_pages;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700784
Nadav Amit6e4453b2018-09-20 10:30:18 -0700785 num_pages = ctl->n_pages;
786 if (num_pages == 0)
787 return 0;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700788
Nadav Amit6e4453b2018-09-20 10:30:18 -0700789 /* communication with the host is done under the communication lock */
790 spin_lock(&b->comm_lock);
791
792 i = 0;
793 list_for_each_entry(page, &ctl->pages, lru)
794 vmballoon_add_page(b, i++, page);
795
796 batch_status = vmballoon_lock_op(b, ctl->n_pages, ctl->page_size,
797 ctl->op);
798
799 /*
800 * Iterate over the pages in the provided list. Since we are changing
801 * @ctl->n_pages we are saving the original value in @num_pages and
802 * use this value to bound the loop.
803 */
Xavier Deguillardf220a802015-08-06 15:17:58 -0700804 for (i = 0; i < num_pages; i++) {
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700805 unsigned long status;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700806
Nadav Amit6e4453b2018-09-20 10:30:18 -0700807 status = vmballoon_status_page(b, i, &page);
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700808
809 /*
810 * Failure of the whole batch overrides a single operation
811 * results.
812 */
813 if (batch_status != VMW_BALLOON_SUCCESS)
814 status = batch_status;
815
Nadav Amit6e4453b2018-09-20 10:30:18 -0700816 /* Continue if no error happened */
817 if (!vmballoon_handle_one_result(b, page, ctl->page_size,
818 status))
819 continue;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700820
Nadav Amit6e4453b2018-09-20 10:30:18 -0700821 /*
822 * Error happened. Move the pages to the refused list and update
823 * the pages number.
824 */
825 list_move(&page->lru, &ctl->refused_pages);
826 ctl->n_pages--;
827 ctl->n_refused_pages++;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700828 }
829
Nadav Amit6e4453b2018-09-20 10:30:18 -0700830 spin_unlock(&b->comm_lock);
831
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700832 return batch_status == VMW_BALLOON_SUCCESS ? 0 : -EIO;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700833}
834
Nadav Amit6e4453b2018-09-20 10:30:18 -0700835/**
836 * vmballoon_release_page_list() - Releases a page list
837 *
838 * @page_list: list of pages to release.
839 * @n_pages: pointer to the number of pages.
840 * @page_size: whether the pages in the list are 2MB (or else 4KB).
841 *
842 * Releases the list of pages and zeros the number of pages.
843 */
844static void vmballoon_release_page_list(struct list_head *page_list,
845 int *n_pages,
846 enum vmballoon_page_size_type page_size)
847{
848 struct page *page, *tmp;
849
850 list_for_each_entry_safe(page, tmp, page_list, lru) {
851 list_del(&page->lru);
852 __free_pages(page, vmballoon_page_order(page_size));
853 }
854
855 *n_pages = 0;
856}
857
858
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400859/*
860 * Release pages that were allocated while attempting to inflate the
861 * balloon but were refused by the host for one reason or another.
862 */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700863static void vmballoon_release_refused_pages(struct vmballoon *b,
Nadav Amit6e4453b2018-09-20 10:30:18 -0700864 struct vmballoon_ctl *ctl)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400865{
Nadav Amit6e4453b2018-09-20 10:30:18 -0700866 vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_REFUSED_FREE,
867 ctl->page_size);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400868
Nadav Amit6e4453b2018-09-20 10:30:18 -0700869 vmballoon_release_page_list(&ctl->refused_pages, &ctl->n_refused_pages,
870 ctl->page_size);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700871}
872
Nadav Amit8b079cd2018-09-20 10:30:15 -0700873/**
874 * vmballoon_change - retrieve the required balloon change
875 *
876 * @b: pointer for the balloon.
877 *
878 * Return: the required change for the balloon size. A positive number
879 * indicates inflation, a negative number indicates a deflation.
880 */
881static int64_t vmballoon_change(struct vmballoon *b)
882{
883 int64_t size, target;
884
Nadav Amit6e4453b2018-09-20 10:30:18 -0700885 size = atomic64_read(&b->size);
886 target = READ_ONCE(b->target);
Nadav Amit8b079cd2018-09-20 10:30:15 -0700887
888 /*
889 * We must cast first because of int sizes
890 * Otherwise we might get huge positives instead of negatives
891 */
892
893 if (b->reset_required)
894 return 0;
895
896 /* consider a 2MB slack on deflate, unless the balloon is emptied */
Nadav Amit6e4453b2018-09-20 10:30:18 -0700897 if (target < size && target != 0 &&
898 size - target < vmballoon_page_in_frames(VMW_BALLOON_2M_PAGE))
Nadav Amit8b079cd2018-09-20 10:30:15 -0700899 return 0;
900
901 return target - size;
902}
903
Nadav Amit6e4453b2018-09-20 10:30:18 -0700904/**
905 * vmballoon_enqueue_page_list() - Enqueues list of pages after inflation.
906 *
907 * @b: pointer to balloon.
908 * @pages: list of pages to enqueue.
909 * @n_pages: pointer to number of pages in list. The value is zeroed.
910 * @page_size: whether the pages are 2MB or 4KB pages.
911 *
912 * Enqueues the provides list of pages in the ballooned page list, clears the
913 * list and zeroes the number of pages that was provided.
914 */
915static void vmballoon_enqueue_page_list(struct vmballoon *b,
916 struct list_head *pages,
917 unsigned int *n_pages,
918 enum vmballoon_page_size_type page_size)
919{
920 struct vmballoon_page_size *page_size_info = &b->page_sizes[page_size];
921
922 list_splice_init(pages, &page_size_info->pages);
923 *n_pages = 0;
924}
925
926/**
927 * vmballoon_dequeue_page_list() - Dequeues page lists for deflation.
928 *
929 * @b: pointer to balloon.
930 * @pages: list of pages to enqueue.
931 * @n_pages: pointer to number of pages in list. The value is zeroed.
932 * @page_size: whether the pages are 2MB or 4KB pages.
933 * @n_req_pages: the number of requested pages.
934 *
935 * Dequeues the number of requested pages from the balloon for deflation. The
936 * number of dequeued pages may be lower, if not enough pages in the requested
937 * size are available.
938 */
939static void vmballoon_dequeue_page_list(struct vmballoon *b,
940 struct list_head *pages,
941 unsigned int *n_pages,
942 enum vmballoon_page_size_type page_size,
943 unsigned int n_req_pages)
944{
945 struct vmballoon_page_size *page_size_info = &b->page_sizes[page_size];
946 struct page *page, *tmp;
947 unsigned int i = 0;
948
949 list_for_each_entry_safe(page, tmp, &page_size_info->pages, lru) {
950 list_move(&page->lru, pages);
951 if (++i == n_req_pages)
952 break;
953 }
954 *n_pages = i;
955}
956
957/**
958 * vmballoon_inflate() - Inflate the balloon towards its target size.
959 *
960 * @b: pointer to the balloon.
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400961 */
962static void vmballoon_inflate(struct vmballoon *b)
963{
Nadav Amit6e4453b2018-09-20 10:30:18 -0700964 int64_t to_inflate_frames;
965 struct vmballoon_ctl ctl = {
966 .pages = LIST_HEAD_INIT(ctl.pages),
967 .refused_pages = LIST_HEAD_INIT(ctl.refused_pages),
968 .page_size = b->max_page_size,
969 .op = VMW_BALLOON_INFLATE
970 };
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400971
Nadav Amit6e4453b2018-09-20 10:30:18 -0700972 while ((to_inflate_frames = vmballoon_change(b)) > 0) {
973 unsigned int to_inflate_pages, page_in_frames;
974 int alloc_error, lock_error = 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400975
Nadav Amit6e4453b2018-09-20 10:30:18 -0700976 VM_BUG_ON(!list_empty(&ctl.pages));
977 VM_BUG_ON(ctl.n_pages != 0);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400978
Nadav Amit6e4453b2018-09-20 10:30:18 -0700979 page_in_frames = vmballoon_page_in_frames(ctl.page_size);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400980
Nadav Amit6e4453b2018-09-20 10:30:18 -0700981 to_inflate_pages = min_t(unsigned long, b->batch_max_pages,
982 DIV_ROUND_UP_ULL(to_inflate_frames,
983 page_in_frames));
Nadav Amitc7b36902018-09-20 10:30:17 -0700984
Nadav Amit6e4453b2018-09-20 10:30:18 -0700985 /* Start by allocating */
986 alloc_error = vmballoon_alloc_page_list(b, &ctl,
987 to_inflate_pages);
Nadav Amitc7b36902018-09-20 10:30:17 -0700988
Nadav Amit6e4453b2018-09-20 10:30:18 -0700989 /* Actually lock the pages by telling the hypervisor */
990 lock_error = vmballoon_lock(b, &ctl);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700991
Nadav Amit6e4453b2018-09-20 10:30:18 -0700992 /*
993 * If an error indicates that something serious went wrong,
994 * stop the inflation.
995 */
996 if (lock_error)
Nadav Amit622074a2018-09-20 10:30:11 -0700997 break;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400998
Nadav Amit6e4453b2018-09-20 10:30:18 -0700999 /* Update the balloon size */
1000 atomic64_add(ctl.n_pages * page_in_frames, &b->size);
Nadav Amit8fa3c612018-09-20 10:30:13 -07001001
Nadav Amit6e4453b2018-09-20 10:30:18 -07001002 vmballoon_enqueue_page_list(b, &ctl.pages, &ctl.n_pages,
1003 ctl.page_size);
Nadav Amit10a95d52018-09-20 10:30:07 -07001004
Nadav Amit6e4453b2018-09-20 10:30:18 -07001005 /*
1006 * If allocation failed or the number of refused pages exceeds
1007 * the maximum allowed, move to the next page size.
1008 */
1009 if (alloc_error ||
1010 ctl.n_refused_pages >= VMW_BALLOON_MAX_REFUSED) {
1011 if (ctl.page_size == VMW_BALLOON_4K_PAGE)
1012 break;
Nadav Amit8fa3c612018-09-20 10:30:13 -07001013
1014 /*
Nadav Amit6e4453b2018-09-20 10:30:18 -07001015 * Ignore errors from locking as we now switch to 4k
1016 * pages and we might get different errors.
Nadav Amit8fa3c612018-09-20 10:30:13 -07001017 */
Nadav Amit6e4453b2018-09-20 10:30:18 -07001018 vmballoon_release_refused_pages(b, &ctl);
1019 ctl.page_size--;
Xavier Deguillardf220a802015-08-06 15:17:58 -07001020 }
Xavier Deguillardef0f8f12015-06-12 11:43:22 -07001021
Philip P. Moltmann33d268e2015-08-06 15:18:01 -07001022 cond_resched();
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001023 }
1024
Nadav Amit6e4453b2018-09-20 10:30:18 -07001025 /*
1026 * Release pages that were allocated while attempting to inflate the
1027 * balloon but were refused by the host for one reason or another,
1028 * and update the statistics.
1029 */
1030 if (ctl.n_refused_pages != 0)
1031 vmballoon_release_refused_pages(b, &ctl);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001032}
1033
Nadav Amit6e4453b2018-09-20 10:30:18 -07001034/**
1035 * vmballoon_deflate() - Decrease the size of the balloon.
1036 *
1037 * @b: pointer to the balloon
1038 * @n_frames: the number of frames to deflate. If zero, automatically
1039 * calculated according to the target size.
1040 * @coordinated: whether to coordinate with the host
1041 *
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001042 * Decrease the size of the balloon allowing guest to use more memory.
Nadav Amit6e4453b2018-09-20 10:30:18 -07001043 *
1044 * Return: The number of deflated frames (i.e., basic page size units)
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001045 */
Nadav Amit6e4453b2018-09-20 10:30:18 -07001046static unsigned long vmballoon_deflate(struct vmballoon *b, uint64_t n_frames,
1047 bool coordinated)
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001048{
Nadav Amit6e4453b2018-09-20 10:30:18 -07001049 unsigned long deflated_frames = 0;
1050 unsigned long tried_frames = 0;
1051 struct vmballoon_ctl ctl = {
1052 .pages = LIST_HEAD_INIT(ctl.pages),
1053 .refused_pages = LIST_HEAD_INIT(ctl.refused_pages),
1054 .page_size = VMW_BALLOON_4K_PAGE,
1055 .op = VMW_BALLOON_DEFLATE
1056 };
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001057
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001058 /* free pages to reach target */
Nadav Amit6e4453b2018-09-20 10:30:18 -07001059 while (true) {
1060 unsigned int to_deflate_pages, n_unlocked_frames;
1061 unsigned int page_in_frames;
1062 int64_t to_deflate_frames;
1063 bool deflated_all;
Xavier Deguillardf220a802015-08-06 15:17:58 -07001064
Nadav Amit6e4453b2018-09-20 10:30:18 -07001065 page_in_frames = vmballoon_page_in_frames(ctl.page_size);
1066
1067 VM_BUG_ON(!list_empty(&ctl.pages));
1068 VM_BUG_ON(ctl.n_pages);
1069 VM_BUG_ON(!list_empty(&ctl.refused_pages));
1070 VM_BUG_ON(ctl.n_refused_pages);
1071
1072 /*
1073 * If we were requested a specific number of frames, we try to
1074 * deflate this number of frames. Otherwise, deflation is
1075 * performed according to the target and balloon size.
1076 */
1077 to_deflate_frames = n_frames ? n_frames - tried_frames :
1078 -vmballoon_change(b);
1079
1080 /* break if no work to do */
1081 if (to_deflate_frames <= 0)
1082 break;
1083
1084 /*
1085 * Calculate the number of frames based on current page size,
1086 * but limit the deflated frames to a single chunk
1087 */
1088 to_deflate_pages = min_t(unsigned long, b->batch_max_pages,
1089 DIV_ROUND_UP_ULL(to_deflate_frames,
1090 page_in_frames));
1091
1092 /* First take the pages from the balloon pages. */
1093 vmballoon_dequeue_page_list(b, &ctl.pages, &ctl.n_pages,
1094 ctl.page_size, to_deflate_pages);
1095
1096 /*
1097 * Before pages are moving to the refused list, count their
1098 * frames as frames that we tried to deflate.
1099 */
1100 tried_frames += ctl.n_pages * page_in_frames;
1101
1102 /*
1103 * Unlock the pages by communicating with the hypervisor if the
1104 * communication is coordinated (i.e., not pop). We ignore the
1105 * return code. Instead we check if all the pages we manage to
1106 * unlock all the pages. If we failed, we will move to the next
1107 * page size, and would eventually try again later.
1108 */
1109 if (coordinated)
1110 vmballoon_lock(b, &ctl);
1111
1112 /*
1113 * Check if we deflated enough. We will move to the next page
1114 * size if we did not manage to do so. This calculation takes
1115 * place now, as once the pages are released, the number of
1116 * pages is zeroed.
1117 */
1118 deflated_all = (ctl.n_pages == to_deflate_pages);
1119
1120 /* Update local and global counters */
1121 n_unlocked_frames = ctl.n_pages * page_in_frames;
1122 atomic64_sub(n_unlocked_frames, &b->size);
1123 deflated_frames += n_unlocked_frames;
1124
1125 vmballoon_stats_page_add(b, VMW_BALLOON_PAGE_STAT_FREE,
1126 ctl.page_size, ctl.n_pages);
1127
1128 /* free the ballooned pages */
1129 vmballoon_release_page_list(&ctl.pages, &ctl.n_pages,
1130 ctl.page_size);
1131
1132 /* Return the refused pages to the ballooned list. */
1133 vmballoon_enqueue_page_list(b, &ctl.refused_pages,
1134 &ctl.n_refused_pages,
1135 ctl.page_size);
1136
1137 /* If we failed to unlock all the pages, move to next size. */
1138 if (!deflated_all) {
1139 if (ctl.page_size == b->max_page_size)
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001140 break;
Nadav Amit6e4453b2018-09-20 10:30:18 -07001141 ctl.page_size++;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001142 }
1143
Nadav Amit6e4453b2018-09-20 10:30:18 -07001144 cond_resched();
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001145 }
Nadav Amit6e4453b2018-09-20 10:30:18 -07001146
1147 return deflated_frames;
Xavier Deguillardf220a802015-08-06 15:17:58 -07001148}
1149
Nadav Amitdf8d0d42018-09-20 10:30:12 -07001150/**
1151 * vmballoon_deinit_batching - disables batching mode.
1152 *
1153 * @b: pointer to &struct vmballoon.
1154 *
1155 * Disables batching, by deallocating the page for communication with the
1156 * hypervisor and disabling the static key to indicate that batching is off.
1157 */
1158static void vmballoon_deinit_batching(struct vmballoon *b)
1159{
1160 free_page((unsigned long)b->batch_page);
1161 b->batch_page = NULL;
1162 static_branch_disable(&vmw_balloon_batching);
1163 b->batch_max_pages = 1;
1164}
Xavier Deguillardf220a802015-08-06 15:17:58 -07001165
Nadav Amitdf8d0d42018-09-20 10:30:12 -07001166/**
1167 * vmballoon_init_batching - enable batching mode.
1168 *
1169 * @b: pointer to &struct vmballoon.
1170 *
1171 * Enables batching, by allocating a page for communication with the hypervisor
1172 * and enabling the static_key to use batching.
1173 *
1174 * Return: zero on success or an appropriate error-code.
1175 */
1176static int vmballoon_init_batching(struct vmballoon *b)
Xavier Deguillardf220a802015-08-06 15:17:58 -07001177{
Gil Kupferb23220f2018-06-01 00:47:47 -07001178 struct page *page;
1179
1180 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1181 if (!page)
Nadav Amitdf8d0d42018-09-20 10:30:12 -07001182 return -ENOMEM;
Xavier Deguillardf220a802015-08-06 15:17:58 -07001183
Gil Kupferb23220f2018-06-01 00:47:47 -07001184 b->batch_page = page_address(page);
Nadav Amitdf8d0d42018-09-20 10:30:12 -07001185 b->batch_max_pages = PAGE_SIZE / sizeof(struct vmballoon_batch_entry);
1186
1187 static_branch_enable(&vmw_balloon_batching);
1188
1189 return 0;
Xavier Deguillardf220a802015-08-06 15:17:58 -07001190}
1191
1192/*
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001193 * Receive notification and resize balloon
1194 */
1195static void vmballoon_doorbell(void *client_data)
1196{
1197 struct vmballoon *b = client_data;
1198
Nadav Amitc7b36902018-09-20 10:30:17 -07001199 vmballoon_stats_gen_inc(b, VMW_BALLOON_STAT_DOORBELL);
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001200
1201 mod_delayed_work(system_freezable_wq, &b->dwork, 0);
1202}
1203
1204/*
1205 * Clean up vmci doorbell
1206 */
1207static void vmballoon_vmci_cleanup(struct vmballoon *b)
1208{
Nadav Amit10a95d52018-09-20 10:30:07 -07001209 vmballoon_cmd(b, VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
1210 VMCI_INVALID_ID, VMCI_INVALID_ID);
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001211
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001212 if (!vmci_handle_is_invalid(b->vmci_doorbell)) {
1213 vmci_doorbell_destroy(b->vmci_doorbell);
1214 b->vmci_doorbell = VMCI_INVALID_HANDLE;
1215 }
1216}
1217
Nadav Amit22d293e2018-09-20 10:30:19 -07001218/**
1219 * vmballoon_vmci_init - Initialize vmci doorbell.
1220 *
1221 * @b: pointer to the balloon.
1222 *
1223 * Return: zero on success or when wakeup command not supported. Error-code
1224 * otherwise.
1225 *
1226 * Initialize vmci doorbell, to get notified as soon as balloon changes.
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001227 */
1228static int vmballoon_vmci_init(struct vmballoon *b)
1229{
Nadav Amit10a95d52018-09-20 10:30:07 -07001230 unsigned long error;
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001231
Nadav Amitce664332018-06-19 16:00:26 -07001232 if ((b->capabilities & VMW_BALLOON_SIGNALLED_WAKEUP_CMD) == 0)
1233 return 0;
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001234
Nadav Amitce664332018-06-19 16:00:26 -07001235 error = vmci_doorbell_create(&b->vmci_doorbell, VMCI_FLAG_DELAYED_CB,
1236 VMCI_PRIVILEGE_FLAG_RESTRICTED,
1237 vmballoon_doorbell, b);
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001238
Nadav Amitce664332018-06-19 16:00:26 -07001239 if (error != VMCI_SUCCESS)
1240 goto fail;
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001241
Nadav Amit10a95d52018-09-20 10:30:07 -07001242 error = __vmballoon_cmd(b, VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
1243 b->vmci_doorbell.context,
1244 b->vmci_doorbell.resource, NULL);
Nadav Amitce664332018-06-19 16:00:26 -07001245
Nadav Amitce664332018-06-19 16:00:26 -07001246 if (error != VMW_BALLOON_SUCCESS)
1247 goto fail;
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001248
1249 return 0;
Nadav Amitce664332018-06-19 16:00:26 -07001250fail:
1251 vmballoon_vmci_cleanup(b);
1252 return -EIO;
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001253}
1254
Nadav Amit6e4453b2018-09-20 10:30:18 -07001255/**
1256 * vmballoon_pop - Quickly release all pages allocate for the balloon.
1257 *
1258 * @b: pointer to the balloon.
1259 *
1260 * This function is called when host decides to "reset" balloon for one reason
1261 * or another. Unlike normal "deflate" we do not (shall not) notify host of the
1262 * pages being released.
1263 */
1264static void vmballoon_pop(struct vmballoon *b)
1265{
1266 unsigned long size;
1267
1268 while ((size = atomic64_read(&b->size)))
1269 vmballoon_deflate(b, size, false);
1270}
1271
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001272/*
Xavier Deguillardf220a802015-08-06 15:17:58 -07001273 * Perform standard reset sequence by popping the balloon (in case it
1274 * is not empty) and then restarting protocol. This operation normally
1275 * happens when host responds with VMW_BALLOON_ERROR_RESET to a command.
1276 */
1277static void vmballoon_reset(struct vmballoon *b)
1278{
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001279 int error;
1280
Nadav Amitc7b36902018-09-20 10:30:17 -07001281 down_write(&b->conf_sem);
1282
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001283 vmballoon_vmci_cleanup(b);
1284
Xavier Deguillardf220a802015-08-06 15:17:58 -07001285 /* free all pages, skipping monitor unlock */
1286 vmballoon_pop(b);
1287
Nadav Amit22d293e2018-09-20 10:30:19 -07001288 if (vmballoon_send_start(b, VMW_BALLOON_CAPABILITIES))
Xavier Deguillardf220a802015-08-06 15:17:58 -07001289 return;
1290
1291 if ((b->capabilities & VMW_BALLOON_BATCHED_CMDS) != 0) {
Nadav Amitdf8d0d42018-09-20 10:30:12 -07001292 if (vmballoon_init_batching(b)) {
Xavier Deguillardf220a802015-08-06 15:17:58 -07001293 /*
1294 * We failed to initialize batching, inform the monitor
1295 * about it by sending a null capability.
1296 *
1297 * The guest will retry in one second.
1298 */
1299 vmballoon_send_start(b, 0);
1300 return;
1301 }
1302 } else if ((b->capabilities & VMW_BALLOON_BASIC_CMDS) != 0) {
Nadav Amitdf8d0d42018-09-20 10:30:12 -07001303 vmballoon_deinit_batching(b);
Xavier Deguillardf220a802015-08-06 15:17:58 -07001304 }
1305
1306 b->reset_required = false;
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001307
1308 error = vmballoon_vmci_init(b);
1309 if (error)
1310 pr_err("failed to initialize vmci doorbell\n");
1311
Nadav Amit22d293e2018-09-20 10:30:19 -07001312 if (vmballoon_send_guest_id(b))
Xavier Deguillardf220a802015-08-06 15:17:58 -07001313 pr_err("failed to send guest ID to the host\n");
Nadav Amitc7b36902018-09-20 10:30:17 -07001314
1315 up_write(&b->conf_sem);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001316}
1317
Nadav Amit8b079cd2018-09-20 10:30:15 -07001318/**
1319 * vmballoon_work - periodic balloon worker for reset, inflation and deflation.
1320 *
1321 * @work: pointer to the &work_struct which is provided by the workqueue.
1322 *
1323 * Resets the protocol if needed, gets the new size and adjusts balloon as
1324 * needed. Repeat in 1 sec.
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001325 */
1326static void vmballoon_work(struct work_struct *work)
1327{
1328 struct delayed_work *dwork = to_delayed_work(work);
1329 struct vmballoon *b = container_of(dwork, struct vmballoon, dwork);
Nadav Amit8b079cd2018-09-20 10:30:15 -07001330 int64_t change = 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001331
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001332 if (b->reset_required)
1333 vmballoon_reset(b);
1334
Nadav Amitc7b36902018-09-20 10:30:17 -07001335 down_read(&b->conf_sem);
1336
1337 /*
1338 * Update the stats while holding the semaphore to ensure that
1339 * @stats_enabled is consistent with whether the stats are actually
1340 * enabled
1341 */
1342 vmballoon_stats_gen_inc(b, VMW_BALLOON_STAT_TIMER);
1343
Nadav Amit0395be32018-09-20 10:30:16 -07001344 if (!vmballoon_send_get_target(b))
Nadav Amit8b079cd2018-09-20 10:30:15 -07001345 change = vmballoon_change(b);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001346
Nadav Amit8b079cd2018-09-20 10:30:15 -07001347 if (change != 0) {
Nadav Amit6e4453b2018-09-20 10:30:18 -07001348 pr_debug("%s - size: %llu, target %lu\n", __func__,
1349 atomic64_read(&b->size), READ_ONCE(b->target));
Nadav Amit8b079cd2018-09-20 10:30:15 -07001350
1351 if (change > 0)
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001352 vmballoon_inflate(b);
Nadav Amit8b079cd2018-09-20 10:30:15 -07001353 else /* (change < 0) */
Nadav Amit6e4453b2018-09-20 10:30:18 -07001354 vmballoon_deflate(b, 0, true);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001355 }
1356
Nadav Amitc7b36902018-09-20 10:30:17 -07001357 up_read(&b->conf_sem);
1358
Dmitry Torokhovbeda94d2011-07-26 16:08:56 -07001359 /*
1360 * We are using a freezable workqueue so that balloon operations are
1361 * stopped while the system transitions to/from sleep/hibernation.
1362 */
1363 queue_delayed_work(system_freezable_wq,
1364 dwork, round_jiffies_relative(HZ));
Nadav Amitc7b36902018-09-20 10:30:17 -07001365
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001366}
1367
1368/*
1369 * DEBUGFS Interface
1370 */
1371#ifdef CONFIG_DEBUG_FS
1372
Nadav Amitc7b36902018-09-20 10:30:17 -07001373static const char * const vmballoon_stat_page_names[] = {
1374 [VMW_BALLOON_PAGE_STAT_ALLOC] = "alloc",
1375 [VMW_BALLOON_PAGE_STAT_ALLOC_FAIL] = "allocFail",
1376 [VMW_BALLOON_PAGE_STAT_REFUSED_ALLOC] = "errAlloc",
1377 [VMW_BALLOON_PAGE_STAT_REFUSED_FREE] = "errFree",
1378 [VMW_BALLOON_PAGE_STAT_FREE] = "free"
1379};
1380
1381static const char * const vmballoon_stat_names[] = {
1382 [VMW_BALLOON_STAT_TIMER] = "timer",
1383 [VMW_BALLOON_STAT_DOORBELL] = "doorbell"
1384};
1385
Nadav Amitc7b36902018-09-20 10:30:17 -07001386static int vmballoon_enable_stats(struct vmballoon *b)
1387{
1388 int r = 0;
1389
1390 down_write(&b->conf_sem);
1391
1392 /* did we somehow race with another reader which enabled stats? */
1393 if (b->stats)
1394 goto out;
1395
1396 b->stats = kzalloc(sizeof(*b->stats), GFP_KERNEL);
1397
1398 if (!b->stats) {
1399 /* allocation failed */
1400 r = -ENOMEM;
1401 goto out;
1402 }
1403 static_key_enable(&balloon_stat_enabled.key);
1404out:
1405 up_write(&b->conf_sem);
1406 return r;
1407}
1408
1409/**
1410 * vmballoon_debug_show - shows statistics of balloon operations.
1411 * @f: pointer to the &struct seq_file.
1412 * @offset: ignored.
1413 *
1414 * Provides the statistics that can be accessed in vmmemctl in the debugfs.
1415 * To avoid the overhead - mainly that of memory - of collecting the statistics,
1416 * we only collect statistics after the first time the counters are read.
1417 *
1418 * Return: zero on success or an error code.
1419 */
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001420static int vmballoon_debug_show(struct seq_file *f, void *offset)
1421{
1422 struct vmballoon *b = f->private;
Nadav Amitc7b36902018-09-20 10:30:17 -07001423 int i, j;
1424
1425 /* enables stats if they are disabled */
1426 if (!b->stats) {
1427 int r = vmballoon_enable_stats(b);
1428
1429 if (r)
1430 return r;
1431 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001432
Philip P. Moltmannb36e89d2015-08-06 15:18:00 -07001433 /* format capabilities info */
Nadav Amit6e4453b2018-09-20 10:30:18 -07001434 seq_printf(f, "%-22s: %#16x\n", "balloon capabilities",
Nadav Amitc7b36902018-09-20 10:30:17 -07001435 VMW_BALLOON_CAPABILITIES);
Nadav Amit6e4453b2018-09-20 10:30:18 -07001436 seq_printf(f, "%-22s: %#16lx\n", "used capabilities", b->capabilities);
Nadav Amitc7b36902018-09-20 10:30:17 -07001437 seq_printf(f, "%-22s: %16s\n", "is resetting",
1438 b->reset_required ? "y" : "n");
Philip P. Moltmannb36e89d2015-08-06 15:18:00 -07001439
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001440 /* format size info */
Nadav Amit6e4453b2018-09-20 10:30:18 -07001441 seq_printf(f, "%-22s: %16lu\n", "target", READ_ONCE(b->target));
1442 seq_printf(f, "%-22s: %16llu\n", "current", atomic64_read(&b->size));
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001443
Nadav Amit68131182018-09-20 10:30:08 -07001444 for (i = 0; i < VMW_BALLOON_CMD_NUM; i++) {
1445 if (vmballoon_cmd_names[i] == NULL)
1446 continue;
1447
Nadav Amitc7b36902018-09-20 10:30:17 -07001448 seq_printf(f, "%-22s: %16llu (%llu failed)\n",
1449 vmballoon_cmd_names[i],
1450 atomic64_read(&b->stats->ops[i][VMW_BALLOON_OP_STAT]),
1451 atomic64_read(&b->stats->ops[i][VMW_BALLOON_OP_FAIL_STAT]));
Nadav Amit68131182018-09-20 10:30:08 -07001452 }
1453
Nadav Amitc7b36902018-09-20 10:30:17 -07001454 for (i = 0; i < VMW_BALLOON_STAT_NUM; i++)
1455 seq_printf(f, "%-22s: %16llu\n",
1456 vmballoon_stat_names[i],
1457 atomic64_read(&b->stats->general_stat[i]));
1458
1459 for (i = 0; i < VMW_BALLOON_PAGE_STAT_NUM; i++) {
1460 for (j = 0; j < VMW_BALLOON_NUM_PAGE_SIZES; j++)
1461 seq_printf(f, "%-18s(%s): %16llu\n",
1462 vmballoon_stat_page_names[i],
1463 vmballoon_page_size_names[j],
1464 atomic64_read(&b->stats->page_stat[i][j]));
1465 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001466
1467 return 0;
1468}
1469
1470static int vmballoon_debug_open(struct inode *inode, struct file *file)
1471{
1472 return single_open(file, vmballoon_debug_show, inode->i_private);
1473}
1474
1475static const struct file_operations vmballoon_debug_fops = {
1476 .owner = THIS_MODULE,
1477 .open = vmballoon_debug_open,
1478 .read = seq_read,
1479 .llseek = seq_lseek,
1480 .release = single_release,
1481};
1482
1483static int __init vmballoon_debugfs_init(struct vmballoon *b)
1484{
1485 int error;
1486
1487 b->dbg_entry = debugfs_create_file("vmmemctl", S_IRUGO, NULL, b,
1488 &vmballoon_debug_fops);
1489 if (IS_ERR(b->dbg_entry)) {
1490 error = PTR_ERR(b->dbg_entry);
1491 pr_err("failed to create debugfs entry, error: %d\n", error);
1492 return error;
1493 }
1494
1495 return 0;
1496}
1497
1498static void __exit vmballoon_debugfs_exit(struct vmballoon *b)
1499{
Nadav Amitc7b36902018-09-20 10:30:17 -07001500 static_key_disable(&balloon_stat_enabled.key);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001501 debugfs_remove(b->dbg_entry);
Nadav Amitc7b36902018-09-20 10:30:17 -07001502 kfree(b->stats);
1503 b->stats = NULL;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001504}
1505
1506#else
1507
1508static inline int vmballoon_debugfs_init(struct vmballoon *b)
1509{
1510 return 0;
1511}
1512
1513static inline void vmballoon_debugfs_exit(struct vmballoon *b)
1514{
1515}
1516
1517#endif /* CONFIG_DEBUG_FS */
1518
1519static int __init vmballoon_init(void)
1520{
Nadav Amit6e4453b2018-09-20 10:30:18 -07001521 enum vmballoon_page_size_type page_size;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001522 int error;
Nadav Amit6e4453b2018-09-20 10:30:18 -07001523
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001524 /*
1525 * Check if we are running on VMware's hypervisor and bail out
1526 * if we are not.
1527 */
Juergen Gross03b2a322017-11-09 14:27:36 +01001528 if (x86_hyper_type != X86_HYPER_VMWARE)
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001529 return -ENODEV;
1530
Nadav Amit6e4453b2018-09-20 10:30:18 -07001531 for (page_size = VMW_BALLOON_4K_PAGE;
1532 page_size <= VMW_BALLOON_LAST_SIZE; page_size++)
1533 INIT_LIST_HEAD(&balloon.page_sizes[page_size].pages);
1534
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001535
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001536 INIT_DELAYED_WORK(&balloon.dwork, vmballoon_work);
1537
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001538 error = vmballoon_debugfs_init(&balloon);
1539 if (error)
Dmitry Torokhovbeda94d2011-07-26 16:08:56 -07001540 return error;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001541
Nadav Amit6e4453b2018-09-20 10:30:18 -07001542 spin_lock_init(&balloon.comm_lock);
Nadav Amitc7b36902018-09-20 10:30:17 -07001543 init_rwsem(&balloon.conf_sem);
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001544 balloon.vmci_doorbell = VMCI_INVALID_HANDLE;
Philip P. Moltmannd7568c12015-08-06 15:18:01 -07001545 balloon.batch_page = NULL;
1546 balloon.page = NULL;
1547 balloon.reset_required = true;
1548
Dmitry Torokhovbeda94d2011-07-26 16:08:56 -07001549 queue_delayed_work(system_freezable_wq, &balloon.dwork, 0);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001550
1551 return 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001552}
Nadav Amitc3cc1b02018-06-19 16:00:27 -07001553
1554/*
1555 * Using late_initcall() instead of module_init() allows the balloon to use the
1556 * VMCI doorbell even when the balloon is built into the kernel. Otherwise the
1557 * VMCI is probed only after the balloon is initialized. If the balloon is used
1558 * as a module, late_initcall() is equivalent to module_init().
1559 */
1560late_initcall(vmballoon_init);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001561
1562static void __exit vmballoon_exit(void)
1563{
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001564 vmballoon_vmci_cleanup(&balloon);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001565 cancel_delayed_work_sync(&balloon.dwork);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001566
1567 vmballoon_debugfs_exit(&balloon);
1568
1569 /*
1570 * Deallocate all reserved memory, and reset connection with monitor.
1571 * Reset connection before deallocating memory to avoid potential for
1572 * additional spurious resets from guest touching deallocated pages.
1573 */
Philip P. Moltmannd7568c12015-08-06 15:18:01 -07001574 vmballoon_send_start(&balloon, 0);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001575 vmballoon_pop(&balloon);
1576}
1577module_exit(vmballoon_exit);