blob: 8840299420e0b287f3cd81733a1c13a0c7957fb0 [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>
Nadav Amit83a8afa2019-04-25 04:54:43 -070031#include <linux/mount.h>
Linus Torvalds933a90b2019-07-19 10:42:02 -070032#include <linux/pseudo_fs.h>
Nadav Amit83a8afa2019-04-25 04:54:43 -070033#include <linux/balloon_compaction.h>
Philip P. Moltmann48e3d662015-08-06 15:18:01 -070034#include <linux/vmw_vmci_defs.h>
35#include <linux/vmw_vmci_api.h>
H. Peter Anvina10a5692010-05-09 01:13:42 -070036#include <asm/hypervisor.h>
Dmitry Torokhov453dc652010-04-23 13:18:08 -040037
38MODULE_AUTHOR("VMware, Inc.");
39MODULE_DESCRIPTION("VMware Memory Control (Balloon) Driver");
Dmitry Torokhov453dc652010-04-23 13:18:08 -040040MODULE_ALIAS("dmi:*:svnVMware*:*");
41MODULE_ALIAS("vmware_vmmemctl");
42MODULE_LICENSE("GPL");
43
Nadav Amit5d1a86e2019-04-25 04:54:44 -070044static bool __read_mostly vmwballoon_shrinker_enable;
45module_param(vmwballoon_shrinker_enable, bool, 0444);
46MODULE_PARM_DESC(vmwballoon_shrinker_enable,
47 "Enable non-cooperative out-of-memory protection. Disabled by default as it may degrade performance.");
48
49/* Delay in seconds after shrink before inflation. */
50#define VMBALLOON_SHRINK_DELAY (5)
51
52/* Maximum number of refused pages we accumulate during inflation cycle */
Dmitry Torokhov55adaa42010-06-04 14:14:52 -070053#define VMW_BALLOON_MAX_REFUSED 16
Dmitry Torokhov453dc652010-04-23 13:18:08 -040054
Nadav Amit83a8afa2019-04-25 04:54:43 -070055/* Magic number for the balloon mount-point */
56#define BALLOON_VMW_MAGIC 0x0ba11007
57
Dmitry Torokhov453dc652010-04-23 13:18:08 -040058/*
59 * Hypervisor communication port definitions.
60 */
61#define VMW_BALLOON_HV_PORT 0x5670
62#define VMW_BALLOON_HV_MAGIC 0x456c6d6f
Dmitry Torokhov453dc652010-04-23 13:18:08 -040063#define VMW_BALLOON_GUEST_ID 1 /* Linux */
64
Xavier Deguillardeb791002015-06-12 11:43:23 -070065enum vmwballoon_capabilities {
66 /*
67 * Bit 0 is reserved and not associated to any capability.
68 */
Philip P. Moltmann48e3d662015-08-06 15:18:01 -070069 VMW_BALLOON_BASIC_CMDS = (1 << 1),
70 VMW_BALLOON_BATCHED_CMDS = (1 << 2),
71 VMW_BALLOON_BATCHED_2M_CMDS = (1 << 3),
72 VMW_BALLOON_SIGNALLED_WAKEUP_CMD = (1 << 4),
Xavier Deguillard55398302019-02-06 15:57:02 -080073 VMW_BALLOON_64_BIT_TARGET = (1 << 5)
Xavier Deguillardeb791002015-06-12 11:43:23 -070074};
75
Xavier Deguillard55398302019-02-06 15:57:02 -080076#define VMW_BALLOON_CAPABILITIES_COMMON (VMW_BALLOON_BASIC_CMDS \
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -070077 | VMW_BALLOON_BATCHED_CMDS \
Philip P. Moltmann48e3d662015-08-06 15:18:01 -070078 | VMW_BALLOON_BATCHED_2M_CMDS \
79 | VMW_BALLOON_SIGNALLED_WAKEUP_CMD)
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -070080
Nadav Amit25acbdd2018-09-20 10:30:14 -070081#define VMW_BALLOON_2M_ORDER (PMD_SHIFT - PAGE_SHIFT)
Xavier Deguillardeb791002015-06-12 11:43:23 -070082
Xavier Deguillard55398302019-02-06 15:57:02 -080083/*
84 * 64-bit targets are only supported in 64-bit
85 */
86#ifdef CONFIG_64BIT
87#define VMW_BALLOON_CAPABILITIES (VMW_BALLOON_CAPABILITIES_COMMON \
88 | VMW_BALLOON_64_BIT_TARGET)
89#else
90#define VMW_BALLOON_CAPABILITIES VMW_BALLOON_CAPABILITIES_COMMON
91#endif
92
Nadav Amitc7b36902018-09-20 10:30:17 -070093enum vmballoon_page_size_type {
94 VMW_BALLOON_4K_PAGE,
95 VMW_BALLOON_2M_PAGE,
96 VMW_BALLOON_LAST_SIZE = VMW_BALLOON_2M_PAGE
97};
98
99#define VMW_BALLOON_NUM_PAGE_SIZES (VMW_BALLOON_LAST_SIZE + 1)
100
Nadav Amit6e4453b2018-09-20 10:30:18 -0700101static const char * const vmballoon_page_size_names[] = {
102 [VMW_BALLOON_4K_PAGE] = "4k",
103 [VMW_BALLOON_2M_PAGE] = "2M"
104};
105
106enum vmballoon_op {
107 VMW_BALLOON_INFLATE,
108 VMW_BALLOON_DEFLATE
109};
110
Nadav Amitc7b36902018-09-20 10:30:17 -0700111enum vmballoon_op_stat_type {
112 VMW_BALLOON_OP_STAT,
113 VMW_BALLOON_OP_FAIL_STAT
114};
115
116#define VMW_BALLOON_OP_STAT_TYPES (VMW_BALLOON_OP_FAIL_STAT + 1)
117
118/**
119 * enum vmballoon_cmd_type - backdoor commands.
Xavier Deguillardf220a802015-08-06 15:17:58 -0700120 *
Nadav Amitc7b36902018-09-20 10:30:17 -0700121 * Availability of the commands is as followed:
Xavier Deguillardf220a802015-08-06 15:17:58 -0700122 *
Nadav Amitc7b36902018-09-20 10:30:17 -0700123 * %VMW_BALLOON_CMD_START, %VMW_BALLOON_CMD_GET_TARGET and
124 * %VMW_BALLOON_CMD_GUEST_ID are always available.
125 *
126 * If the host reports %VMW_BALLOON_BASIC_CMDS are supported then
127 * %VMW_BALLOON_CMD_LOCK and %VMW_BALLOON_CMD_UNLOCK commands are available.
128 *
129 * If the host reports %VMW_BALLOON_BATCHED_CMDS are supported then
130 * %VMW_BALLOON_CMD_BATCHED_LOCK and VMW_BALLOON_CMD_BATCHED_UNLOCK commands
131 * are available.
132 *
133 * If the host reports %VMW_BALLOON_BATCHED_2M_CMDS are supported then
134 * %VMW_BALLOON_CMD_BATCHED_2M_LOCK and %VMW_BALLOON_CMD_BATCHED_2M_UNLOCK
135 * are supported.
136 *
137 * If the host reports VMW_BALLOON_SIGNALLED_WAKEUP_CMD is supported then
138 * VMW_BALLOON_CMD_VMCI_DOORBELL_SET command is supported.
139 *
140 * @VMW_BALLOON_CMD_START: Communicating supported version with the hypervisor.
141 * @VMW_BALLOON_CMD_GET_TARGET: Gets the balloon target size.
142 * @VMW_BALLOON_CMD_LOCK: Informs the hypervisor about a ballooned page.
143 * @VMW_BALLOON_CMD_UNLOCK: Informs the hypervisor about a page that is about
144 * to be deflated from the balloon.
145 * @VMW_BALLOON_CMD_GUEST_ID: Informs the hypervisor about the type of OS that
146 * runs in the VM.
147 * @VMW_BALLOON_CMD_BATCHED_LOCK: Inform the hypervisor about a batch of
148 * ballooned pages (up to 512).
149 * @VMW_BALLOON_CMD_BATCHED_UNLOCK: Inform the hypervisor about a batch of
150 * pages that are about to be deflated from the
151 * balloon (up to 512).
152 * @VMW_BALLOON_CMD_BATCHED_2M_LOCK: Similar to @VMW_BALLOON_CMD_BATCHED_LOCK
153 * for 2MB pages.
154 * @VMW_BALLOON_CMD_BATCHED_2M_UNLOCK: Similar to
155 * @VMW_BALLOON_CMD_BATCHED_UNLOCK for 2MB
156 * pages.
157 * @VMW_BALLOON_CMD_VMCI_DOORBELL_SET: A command to set doorbell notification
158 * that would be invoked when the balloon
159 * size changes.
160 * @VMW_BALLOON_CMD_LAST: Value of the last command.
Xavier Deguillardf220a802015-08-06 15:17:58 -0700161 */
Nadav Amitc7b36902018-09-20 10:30:17 -0700162enum vmballoon_cmd_type {
163 VMW_BALLOON_CMD_START,
164 VMW_BALLOON_CMD_GET_TARGET,
165 VMW_BALLOON_CMD_LOCK,
166 VMW_BALLOON_CMD_UNLOCK,
167 VMW_BALLOON_CMD_GUEST_ID,
168 /* No command 5 */
169 VMW_BALLOON_CMD_BATCHED_LOCK = 6,
170 VMW_BALLOON_CMD_BATCHED_UNLOCK,
171 VMW_BALLOON_CMD_BATCHED_2M_LOCK,
172 VMW_BALLOON_CMD_BATCHED_2M_UNLOCK,
173 VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
174 VMW_BALLOON_CMD_LAST = VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
175};
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700176
Nadav Amitc7b36902018-09-20 10:30:17 -0700177#define VMW_BALLOON_CMD_NUM (VMW_BALLOON_CMD_LAST + 1)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400178
Nadav Amitc7b36902018-09-20 10:30:17 -0700179enum vmballoon_error_codes {
180 VMW_BALLOON_SUCCESS,
181 VMW_BALLOON_ERROR_CMD_INVALID,
182 VMW_BALLOON_ERROR_PPN_INVALID,
183 VMW_BALLOON_ERROR_PPN_LOCKED,
184 VMW_BALLOON_ERROR_PPN_UNLOCKED,
185 VMW_BALLOON_ERROR_PPN_PINNED,
186 VMW_BALLOON_ERROR_PPN_NOTNEEDED,
187 VMW_BALLOON_ERROR_RESET,
188 VMW_BALLOON_ERROR_BUSY
189};
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400190
Xavier Deguillardeb791002015-06-12 11:43:23 -0700191#define VMW_BALLOON_SUCCESS_WITH_CAPABILITIES (0x03000000)
192
Nadav Amit10a95d52018-09-20 10:30:07 -0700193#define VMW_BALLOON_CMD_WITH_TARGET_MASK \
194 ((1UL << VMW_BALLOON_CMD_GET_TARGET) | \
195 (1UL << VMW_BALLOON_CMD_LOCK) | \
196 (1UL << VMW_BALLOON_CMD_UNLOCK) | \
197 (1UL << VMW_BALLOON_CMD_BATCHED_LOCK) | \
198 (1UL << VMW_BALLOON_CMD_BATCHED_UNLOCK) | \
199 (1UL << VMW_BALLOON_CMD_BATCHED_2M_LOCK) | \
200 (1UL << VMW_BALLOON_CMD_BATCHED_2M_UNLOCK))
201
Nadav Amit68131182018-09-20 10:30:08 -0700202static const char * const vmballoon_cmd_names[] = {
203 [VMW_BALLOON_CMD_START] = "start",
204 [VMW_BALLOON_CMD_GET_TARGET] = "target",
205 [VMW_BALLOON_CMD_LOCK] = "lock",
206 [VMW_BALLOON_CMD_UNLOCK] = "unlock",
207 [VMW_BALLOON_CMD_GUEST_ID] = "guestType",
208 [VMW_BALLOON_CMD_BATCHED_LOCK] = "batchLock",
209 [VMW_BALLOON_CMD_BATCHED_UNLOCK] = "batchUnlock",
210 [VMW_BALLOON_CMD_BATCHED_2M_LOCK] = "2m-lock",
211 [VMW_BALLOON_CMD_BATCHED_2M_UNLOCK] = "2m-unlock",
212 [VMW_BALLOON_CMD_VMCI_DOORBELL_SET] = "doorbellSet"
213};
214
Nadav Amitc7b36902018-09-20 10:30:17 -0700215enum vmballoon_stat_page {
216 VMW_BALLOON_PAGE_STAT_ALLOC,
217 VMW_BALLOON_PAGE_STAT_ALLOC_FAIL,
218 VMW_BALLOON_PAGE_STAT_REFUSED_ALLOC,
219 VMW_BALLOON_PAGE_STAT_REFUSED_FREE,
220 VMW_BALLOON_PAGE_STAT_FREE,
221 VMW_BALLOON_PAGE_STAT_LAST = VMW_BALLOON_PAGE_STAT_FREE
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400222};
223
Nadav Amitc7b36902018-09-20 10:30:17 -0700224#define VMW_BALLOON_PAGE_STAT_NUM (VMW_BALLOON_PAGE_STAT_LAST + 1)
225
226enum vmballoon_stat_general {
227 VMW_BALLOON_STAT_TIMER,
228 VMW_BALLOON_STAT_DOORBELL,
Nadav Amit8840a6f2018-09-20 10:30:20 -0700229 VMW_BALLOON_STAT_RESET,
Nadav Amit5d1a86e2019-04-25 04:54:44 -0700230 VMW_BALLOON_STAT_SHRINK,
231 VMW_BALLOON_STAT_SHRINK_FREE,
232 VMW_BALLOON_STAT_LAST = VMW_BALLOON_STAT_SHRINK_FREE
Nadav Amitc7b36902018-09-20 10:30:17 -0700233};
234
235#define VMW_BALLOON_STAT_NUM (VMW_BALLOON_STAT_LAST + 1)
236
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700237static DEFINE_STATIC_KEY_TRUE(vmw_balloon_batching);
Nadav Amitc7b36902018-09-20 10:30:17 -0700238static DEFINE_STATIC_KEY_FALSE(balloon_stat_enabled);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700239
Nadav Amit6e4453b2018-09-20 10:30:18 -0700240struct vmballoon_ctl {
241 struct list_head pages;
242 struct list_head refused_pages;
Nadav Amitae297832019-04-25 04:54:45 -0700243 struct list_head prealloc_pages;
Nadav Amit6e4453b2018-09-20 10:30:18 -0700244 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
Nadav Amit6c948752018-09-20 10:30:10 -0700250/**
251 * struct vmballoon_batch_entry - a batch entry for lock or unlock.
252 *
253 * @status: the status of the operation, which is written by the hypervisor.
254 * @reserved: reserved for future use. Must be set to zero.
255 * @pfn: the physical frame number of the page to be locked or unlocked.
256 */
257struct vmballoon_batch_entry {
258 u64 status : 5;
259 u64 reserved : PAGE_SHIFT - 5;
260 u64 pfn : 52;
261} __packed;
262
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700263struct vmballoon {
Nadav Amit6e4453b2018-09-20 10:30:18 -0700264 /**
265 * @max_page_size: maximum supported page size for ballooning.
266 *
267 * Protected by @conf_sem
268 */
269 enum vmballoon_page_size_type max_page_size;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400270
Nadav Amit6e4453b2018-09-20 10:30:18 -0700271 /**
272 * @size: balloon actual size in basic page size (frames).
273 *
274 * While we currently do not support size which is bigger than 32-bit,
275 * in preparation for future support, use 64-bits.
276 */
277 atomic64_t size;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400278
Nadav Amit6e4453b2018-09-20 10:30:18 -0700279 /**
280 * @target: balloon target size in basic page size (frames).
281 *
282 * We do not protect the target under the assumption that setting the
283 * value is always done through a single write. If this assumption ever
284 * breaks, we would have to use X_ONCE for accesses, and suffer the less
285 * optimized code. Although we may read stale target value if multiple
286 * accesses happen at once, the performance impact should be minor.
287 */
288 unsigned long target;
289
290 /**
291 * @reset_required: reset flag
292 *
293 * Setting this flag may introduce races, but the code is expected to
294 * handle them gracefully. In the worst case, another operation will
295 * fail as reset did not take place. Clearing the flag is done while
296 * holding @conf_sem for write.
297 */
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400298 bool reset_required;
299
Nadav Amit6e4453b2018-09-20 10:30:18 -0700300 /**
301 * @capabilities: hypervisor balloon capabilities.
302 *
303 * Protected by @conf_sem.
304 */
Xavier Deguillardf220a802015-08-06 15:17:58 -0700305 unsigned long capabilities;
306
Nadav Amit6c948752018-09-20 10:30:10 -0700307 /**
308 * @batch_page: pointer to communication batch page.
309 *
310 * When batching is used, batch_page points to a page, which holds up to
311 * %VMW_BALLOON_BATCH_MAX_PAGES entries for locking or unlocking.
312 */
313 struct vmballoon_batch_entry *batch_page;
314
Nadav Amit6e4453b2018-09-20 10:30:18 -0700315 /**
316 * @batch_max_pages: maximum pages that can be locked/unlocked.
317 *
318 * Indicates the number of pages that the hypervisor can lock or unlock
319 * at once, according to whether batching is enabled. If batching is
320 * disabled, only a single page can be locked/unlock on each operation.
321 *
322 * Protected by @conf_sem.
323 */
Xavier Deguillardf220a802015-08-06 15:17:58 -0700324 unsigned int batch_max_pages;
Nadav Amit6e4453b2018-09-20 10:30:18 -0700325
326 /**
327 * @page: page to be locked/unlocked by the hypervisor
328 *
329 * @page is only used when batching is disabled and a single page is
330 * reclaimed on each iteration.
331 *
332 * Protected by @comm_lock.
333 */
Xavier Deguillardf220a802015-08-06 15:17:58 -0700334 struct page *page;
335
Nadav Amit5d1a86e2019-04-25 04:54:44 -0700336 /**
337 * @shrink_timeout: timeout until the next inflation.
338 *
339 * After an shrink event, indicates the time in jiffies after which
340 * inflation is allowed again. Can be written concurrently with reads,
341 * so must use READ_ONCE/WRITE_ONCE when accessing.
342 */
343 unsigned long shrink_timeout;
344
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400345 /* statistics */
Nadav Amitc7b36902018-09-20 10:30:17 -0700346 struct vmballoon_stats *stats;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400347
Nadav Amitc7b36902018-09-20 10:30:17 -0700348#ifdef CONFIG_DEBUG_FS
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400349 /* debugfs file exporting statistics */
350 struct dentry *dbg_entry;
351#endif
352
Nadav Amit83a8afa2019-04-25 04:54:43 -0700353 /**
354 * @b_dev_info: balloon device information descriptor.
355 */
356 struct balloon_dev_info b_dev_info;
357
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400358 struct delayed_work dwork;
Philip P. Moltmann48e3d662015-08-06 15:18:01 -0700359
Nadav Amit6e4453b2018-09-20 10:30:18 -0700360 /**
Nadav Amit83a8afa2019-04-25 04:54:43 -0700361 * @huge_pages - list of the inflated 2MB pages.
362 *
363 * Protected by @b_dev_info.pages_lock .
364 */
365 struct list_head huge_pages;
366
367 /**
Nadav Amit6e4453b2018-09-20 10:30:18 -0700368 * @vmci_doorbell.
369 *
370 * Protected by @conf_sem.
371 */
Philip P. Moltmann48e3d662015-08-06 15:18:01 -0700372 struct vmci_handle vmci_doorbell;
Nadav Amitc7b36902018-09-20 10:30:17 -0700373
374 /**
375 * @conf_sem: semaphore to protect the configuration and the statistics.
376 */
377 struct rw_semaphore conf_sem;
Nadav Amit6e4453b2018-09-20 10:30:18 -0700378
379 /**
380 * @comm_lock: lock to protect the communication with the host.
381 *
382 * Lock ordering: @conf_sem -> @comm_lock .
383 */
384 spinlock_t comm_lock;
Nadav Amit5d1a86e2019-04-25 04:54:44 -0700385
386 /**
387 * @shrinker: shrinker interface that is used to avoid over-inflation.
388 */
389 struct shrinker shrinker;
390
391 /**
392 * @shrinker_registered: whether the shrinker was registered.
393 *
394 * The shrinker interface does not handle gracefully the removal of
395 * shrinker that was not registered before. This indication allows to
396 * simplify the unregistration process.
397 */
398 bool shrinker_registered;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400399};
400
401static struct vmballoon balloon;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400402
Nadav Amitc7b36902018-09-20 10:30:17 -0700403struct vmballoon_stats {
404 /* timer / doorbell operations */
405 atomic64_t general_stat[VMW_BALLOON_STAT_NUM];
406
407 /* allocation statistics for huge and small pages */
408 atomic64_t
409 page_stat[VMW_BALLOON_PAGE_STAT_NUM][VMW_BALLOON_NUM_PAGE_SIZES];
410
411 /* Monitor operations: total operations, and failures */
412 atomic64_t ops[VMW_BALLOON_CMD_NUM][VMW_BALLOON_OP_STAT_TYPES];
413};
414
415static inline bool is_vmballoon_stats_on(void)
416{
417 return IS_ENABLED(CONFIG_DEBUG_FS) &&
418 static_branch_unlikely(&balloon_stat_enabled);
419}
420
421static inline void vmballoon_stats_op_inc(struct vmballoon *b, unsigned int op,
422 enum vmballoon_op_stat_type type)
423{
424 if (is_vmballoon_stats_on())
425 atomic64_inc(&b->stats->ops[op][type]);
426}
427
428static inline void vmballoon_stats_gen_inc(struct vmballoon *b,
429 enum vmballoon_stat_general stat)
430{
431 if (is_vmballoon_stats_on())
432 atomic64_inc(&b->stats->general_stat[stat]);
433}
434
435static inline void vmballoon_stats_gen_add(struct vmballoon *b,
436 enum vmballoon_stat_general stat,
437 unsigned int val)
438{
439 if (is_vmballoon_stats_on())
440 atomic64_add(val, &b->stats->general_stat[stat]);
441}
442
443static inline void vmballoon_stats_page_inc(struct vmballoon *b,
444 enum vmballoon_stat_page stat,
Nadav Amit6e4453b2018-09-20 10:30:18 -0700445 enum vmballoon_page_size_type size)
Nadav Amitc7b36902018-09-20 10:30:17 -0700446{
447 if (is_vmballoon_stats_on())
Nadav Amit6e4453b2018-09-20 10:30:18 -0700448 atomic64_inc(&b->stats->page_stat[stat][size]);
449}
450
451static inline void vmballoon_stats_page_add(struct vmballoon *b,
452 enum vmballoon_stat_page stat,
453 enum vmballoon_page_size_type size,
454 unsigned int val)
455{
456 if (is_vmballoon_stats_on())
457 atomic64_add(val, &b->stats->page_stat[stat][size]);
Nadav Amitc7b36902018-09-20 10:30:17 -0700458}
459
Nadav Amit10a95d52018-09-20 10:30:07 -0700460static inline unsigned long
461__vmballoon_cmd(struct vmballoon *b, unsigned long cmd, unsigned long arg1,
462 unsigned long arg2, unsigned long *result)
463{
464 unsigned long status, dummy1, dummy2, dummy3, local_result;
465
Nadav Amitc7b36902018-09-20 10:30:17 -0700466 vmballoon_stats_op_inc(b, cmd, VMW_BALLOON_OP_STAT);
Nadav Amit68131182018-09-20 10:30:08 -0700467
Nadav Amit10a95d52018-09-20 10:30:07 -0700468 asm volatile ("inl %%dx" :
469 "=a"(status),
470 "=c"(dummy1),
471 "=d"(dummy2),
472 "=b"(local_result),
473 "=S"(dummy3) :
474 "0"(VMW_BALLOON_HV_MAGIC),
475 "1"(cmd),
476 "2"(VMW_BALLOON_HV_PORT),
477 "3"(arg1),
478 "4"(arg2) :
479 "memory");
480
481 /* update the result if needed */
482 if (result)
483 *result = (cmd == VMW_BALLOON_CMD_START) ? dummy1 :
484 local_result;
485
486 /* update target when applicable */
487 if (status == VMW_BALLOON_SUCCESS &&
488 ((1ul << cmd) & VMW_BALLOON_CMD_WITH_TARGET_MASK))
Nadav Amit6e4453b2018-09-20 10:30:18 -0700489 WRITE_ONCE(b->target, local_result);
Nadav Amit10a95d52018-09-20 10:30:07 -0700490
Nadav Amit68131182018-09-20 10:30:08 -0700491 if (status != VMW_BALLOON_SUCCESS &&
492 status != VMW_BALLOON_SUCCESS_WITH_CAPABILITIES) {
Nadav Amitc7b36902018-09-20 10:30:17 -0700493 vmballoon_stats_op_inc(b, cmd, VMW_BALLOON_OP_FAIL_STAT);
Nadav Amit68131182018-09-20 10:30:08 -0700494 pr_debug("%s: %s [0x%lx,0x%lx) failed, returned %ld\n",
495 __func__, vmballoon_cmd_names[cmd], arg1, arg2,
496 status);
497 }
498
Nadav Amit10a95d52018-09-20 10:30:07 -0700499 /* mark reset required accordingly */
500 if (status == VMW_BALLOON_ERROR_RESET)
501 b->reset_required = true;
502
503 return status;
504}
505
506static __always_inline unsigned long
507vmballoon_cmd(struct vmballoon *b, unsigned long cmd, unsigned long arg1,
508 unsigned long arg2)
509{
510 unsigned long dummy;
511
512 return __vmballoon_cmd(b, cmd, arg1, arg2, &dummy);
513}
514
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400515/*
516 * Send "start" command to the host, communicating supported version
517 * of the protocol.
518 */
Nadav Amit22d293e2018-09-20 10:30:19 -0700519static int vmballoon_send_start(struct vmballoon *b, unsigned long req_caps)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400520{
Nadav Amit10a95d52018-09-20 10:30:07 -0700521 unsigned long status, capabilities;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400522
Nadav Amit10a95d52018-09-20 10:30:07 -0700523 status = __vmballoon_cmd(b, VMW_BALLOON_CMD_START, req_caps, 0,
524 &capabilities);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700525
526 switch (status) {
527 case VMW_BALLOON_SUCCESS_WITH_CAPABILITIES:
528 b->capabilities = capabilities;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700529 break;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700530 case VMW_BALLOON_SUCCESS:
531 b->capabilities = VMW_BALLOON_BASIC_CMDS;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700532 break;
533 default:
Nadav Amit22d293e2018-09-20 10:30:19 -0700534 return -EIO;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700535 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400536
Nadav Amit5081efd2018-06-19 16:00:25 -0700537 /*
538 * 2MB pages are only supported with batching. If batching is for some
539 * reason disabled, do not use 2MB pages, since otherwise the legacy
540 * mechanism is used with 2MB pages, causing a failure.
541 */
Nadav Amit6e4453b2018-09-20 10:30:18 -0700542 b->max_page_size = VMW_BALLOON_4K_PAGE;
Nadav Amit5081efd2018-06-19 16:00:25 -0700543 if ((b->capabilities & VMW_BALLOON_BATCHED_2M_CMDS) &&
544 (b->capabilities & VMW_BALLOON_BATCHED_CMDS))
Nadav Amit6e4453b2018-09-20 10:30:18 -0700545 b->max_page_size = VMW_BALLOON_2M_PAGE;
546
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700547
Nadav Amit22d293e2018-09-20 10:30:19 -0700548 return 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400549}
550
Nadav Amit22d293e2018-09-20 10:30:19 -0700551/**
552 * vmballoon_send_guest_id - communicate guest type to the host.
553 *
554 * @b: pointer to the balloon.
555 *
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400556 * Communicate guest type to the host so that it can adjust ballooning
557 * algorithm to the one most appropriate for the guest. This command
558 * is normally issued after sending "start" command and is part of
559 * standard reset sequence.
Nadav Amit22d293e2018-09-20 10:30:19 -0700560 *
561 * Return: zero on success or appropriate error code.
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400562 */
Nadav Amit22d293e2018-09-20 10:30:19 -0700563static int vmballoon_send_guest_id(struct vmballoon *b)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400564{
Nadav Amit10a95d52018-09-20 10:30:07 -0700565 unsigned long status;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400566
Nadav Amit10a95d52018-09-20 10:30:07 -0700567 status = vmballoon_cmd(b, VMW_BALLOON_CMD_GUEST_ID,
568 VMW_BALLOON_GUEST_ID, 0);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400569
Nadav Amit22d293e2018-09-20 10:30:19 -0700570 return status == VMW_BALLOON_SUCCESS ? 0 : -EIO;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400571}
572
Nadav Amit6e4453b2018-09-20 10:30:18 -0700573/**
574 * vmballoon_page_order() - return the order of the page
575 * @page_size: the size of the page.
576 *
577 * Return: the allocation order.
578 */
579static inline
580unsigned int vmballoon_page_order(enum vmballoon_page_size_type page_size)
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700581{
Nadav Amit6e4453b2018-09-20 10:30:18 -0700582 return page_size == VMW_BALLOON_2M_PAGE ? VMW_BALLOON_2M_ORDER : 0;
583}
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700584
Nadav Amit6e4453b2018-09-20 10:30:18 -0700585/**
586 * vmballoon_page_in_frames() - returns the number of frames in a page.
587 * @page_size: the size of the page.
588 *
589 * Return: the number of 4k frames.
590 */
591static inline unsigned int
592vmballoon_page_in_frames(enum vmballoon_page_size_type page_size)
593{
594 return 1 << vmballoon_page_order(page_size);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700595}
596
Nadav Amit0395be32018-09-20 10:30:16 -0700597/**
David Hildenbrand81655402019-03-05 15:42:41 -0800598 * vmballoon_mark_page_offline() - mark a page as offline
599 * @page: pointer for the page.
600 * @page_size: the size of the page.
601 */
602static void
603vmballoon_mark_page_offline(struct page *page,
604 enum vmballoon_page_size_type page_size)
605{
606 int i;
607
608 for (i = 0; i < vmballoon_page_in_frames(page_size); i++)
609 __SetPageOffline(page + i);
610}
611
612/**
613 * vmballoon_mark_page_online() - mark a page as online
614 * @page: pointer for the page.
615 * @page_size: the size of the page.
616 */
617static void
618vmballoon_mark_page_online(struct page *page,
619 enum vmballoon_page_size_type page_size)
620{
621 int i;
622
623 for (i = 0; i < vmballoon_page_in_frames(page_size); i++)
624 __ClearPageOffline(page + i);
625}
626
627/**
Nadav Amit0395be32018-09-20 10:30:16 -0700628 * vmballoon_send_get_target() - Retrieve desired balloon size from the host.
629 *
630 * @b: pointer to the balloon.
631 *
632 * Return: zero on success, EINVAL if limit does not fit in 32-bit, as required
633 * by the host-guest protocol and EIO if an error occurred in communicating with
634 * the host.
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400635 */
Nadav Amit0395be32018-09-20 10:30:16 -0700636static int vmballoon_send_get_target(struct vmballoon *b)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400637{
638 unsigned long status;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400639 unsigned long limit;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400640
Arun KSca79b0c2018-12-28 00:34:29 -0800641 limit = totalram_pages();
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400642
Xavier Deguillard55398302019-02-06 15:57:02 -0800643 /* Ensure limit fits in 32-bits if 64-bit targets are not supported */
644 if (!(b->capabilities & VMW_BALLOON_64_BIT_TARGET) &&
645 limit != (u32)limit)
Nadav Amit0395be32018-09-20 10:30:16 -0700646 return -EINVAL;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400647
Nadav Amit10a95d52018-09-20 10:30:07 -0700648 status = vmballoon_cmd(b, VMW_BALLOON_CMD_GET_TARGET, limit, 0);
649
Nadav Amit0395be32018-09-20 10:30:16 -0700650 return status == VMW_BALLOON_SUCCESS ? 0 : -EIO;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400651}
652
Nadav Amit6e4453b2018-09-20 10:30:18 -0700653/**
654 * vmballoon_alloc_page_list - allocates a list of pages.
655 *
656 * @b: pointer to the balloon.
657 * @ctl: pointer for the %struct vmballoon_ctl, which defines the operation.
658 * @req_n_pages: the number of requested pages.
659 *
660 * Tries to allocate @req_n_pages. Add them to the list of balloon pages in
661 * @ctl.pages and updates @ctl.n_pages to reflect the number of pages.
662 *
663 * Return: zero on success or error code otherwise.
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400664 */
Nadav Amit6e4453b2018-09-20 10:30:18 -0700665static int vmballoon_alloc_page_list(struct vmballoon *b,
666 struct vmballoon_ctl *ctl,
667 unsigned int req_n_pages)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400668{
Nadav Amit6e4453b2018-09-20 10:30:18 -0700669 struct page *page;
670 unsigned int i;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400671
Nadav Amit6e4453b2018-09-20 10:30:18 -0700672 for (i = 0; i < req_n_pages; i++) {
Nadav Amitae297832019-04-25 04:54:45 -0700673 /*
674 * First check if we happen to have pages that were allocated
675 * before. This happens when 2MB page rejected during inflation
676 * by the hypervisor, and then split into 4KB pages.
677 */
678 if (!list_empty(&ctl->prealloc_pages)) {
679 page = list_first_entry(&ctl->prealloc_pages,
680 struct page, lru);
681 list_del(&page->lru);
682 } else {
683 if (ctl->page_size == VMW_BALLOON_2M_PAGE)
684 page = alloc_pages(__GFP_HIGHMEM|__GFP_NOWARN|
Nadav Amit83a8afa2019-04-25 04:54:43 -0700685 __GFP_NOMEMALLOC, VMW_BALLOON_2M_ORDER);
Nadav Amitae297832019-04-25 04:54:45 -0700686 else
687 page = balloon_page_alloc();
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700688
Nadav Amitae297832019-04-25 04:54:45 -0700689 vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_ALLOC,
690 ctl->page_size);
691 }
Nadav Amit6e4453b2018-09-20 10:30:18 -0700692
693 if (page) {
David Hildenbrand81655402019-03-05 15:42:41 -0800694 vmballoon_mark_page_offline(page, ctl->page_size);
Nadav Amit6e4453b2018-09-20 10:30:18 -0700695 /* Success. Add the page to the list and continue. */
696 list_add(&page->lru, &ctl->pages);
697 continue;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700698 }
Nadav Amit6e4453b2018-09-20 10:30:18 -0700699
700 /* Allocation failed. Update statistics and stop. */
701 vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_ALLOC_FAIL,
702 ctl->page_size);
703 break;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400704 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400705
Nadav Amit6e4453b2018-09-20 10:30:18 -0700706 ctl->n_pages = i;
707
708 return req_n_pages == ctl->n_pages ? 0 : -ENOMEM;
709}
710
711/**
712 * vmballoon_handle_one_result - Handle lock/unlock result for a single page.
713 *
714 * @b: pointer for %struct vmballoon.
715 * @page: pointer for the page whose result should be handled.
716 * @page_size: size of the page.
717 * @status: status of the operation as provided by the hypervisor.
718 */
719static int vmballoon_handle_one_result(struct vmballoon *b, struct page *page,
720 enum vmballoon_page_size_type page_size,
721 unsigned long status)
722{
723 /* On success do nothing. The page is already on the balloon list. */
724 if (likely(status == VMW_BALLOON_SUCCESS))
725 return 0;
726
727 pr_debug("%s: failed comm pfn %lx status %lu page_size %s\n", __func__,
728 page_to_pfn(page), status,
729 vmballoon_page_size_names[page_size]);
730
731 /* Error occurred */
732 vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_REFUSED_ALLOC,
733 page_size);
734
735 return -EIO;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400736}
737
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700738/**
739 * vmballoon_status_page - returns the status of (un)lock operation
740 *
741 * @b: pointer to the balloon.
742 * @idx: index for the page for which the operation is performed.
743 * @p: pointer to where the page struct is returned.
744 *
745 * Following a lock or unlock operation, returns the status of the operation for
746 * an individual page. Provides the page that the operation was performed on on
747 * the @page argument.
748 *
749 * Returns: The status of a lock or unlock operation for an individual page.
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400750 */
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700751static unsigned long vmballoon_status_page(struct vmballoon *b, int idx,
752 struct page **p)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400753{
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700754 if (static_branch_likely(&vmw_balloon_batching)) {
755 /* batching mode */
756 *p = pfn_to_page(b->batch_page[idx].pfn);
757 return b->batch_page[idx].status;
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700758 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400759
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700760 /* non-batching mode */
761 *p = b->page;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400762
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700763 /*
764 * If a failure occurs, the indication will be provided in the status
765 * of the entire operation, which is considered before the individual
766 * page status. So for non-batching mode, the indication is always of
767 * success.
768 */
769 return VMW_BALLOON_SUCCESS;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400770}
771
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700772/**
773 * vmballoon_lock_op - notifies the host about inflated/deflated pages.
774 * @b: pointer to the balloon.
775 * @num_pages: number of inflated/deflated pages.
Nadav Amit6e4453b2018-09-20 10:30:18 -0700776 * @page_size: size of the page.
777 * @op: the type of operation (lock or unlock).
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700778 *
779 * Notify the host about page(s) that were ballooned (or removed from the
780 * balloon) so that host can use it without fear that guest will need it (or
781 * stop using them since the VM does). Host may reject some pages, we need to
782 * check the return value and maybe submit a different page. The pages that are
783 * inflated/deflated are pointed by @b->page.
784 *
785 * Return: result as provided by the hypervisor.
786 */
787static unsigned long vmballoon_lock_op(struct vmballoon *b,
788 unsigned int num_pages,
Nadav Amit6e4453b2018-09-20 10:30:18 -0700789 enum vmballoon_page_size_type page_size,
790 enum vmballoon_op op)
Xavier Deguillardf220a802015-08-06 15:17:58 -0700791{
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700792 unsigned long cmd, pfn;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700793
Nadav Amit6e4453b2018-09-20 10:30:18 -0700794 lockdep_assert_held(&b->comm_lock);
795
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700796 if (static_branch_likely(&vmw_balloon_batching)) {
Nadav Amit6e4453b2018-09-20 10:30:18 -0700797 if (op == VMW_BALLOON_INFLATE)
798 cmd = page_size == VMW_BALLOON_2M_PAGE ?
799 VMW_BALLOON_CMD_BATCHED_2M_LOCK :
800 VMW_BALLOON_CMD_BATCHED_LOCK;
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700801 else
Nadav Amit6e4453b2018-09-20 10:30:18 -0700802 cmd = page_size == VMW_BALLOON_2M_PAGE ?
803 VMW_BALLOON_CMD_BATCHED_2M_UNLOCK :
804 VMW_BALLOON_CMD_BATCHED_UNLOCK;
Nadav Amit10a95d52018-09-20 10:30:07 -0700805
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700806 pfn = PHYS_PFN(virt_to_phys(b->batch_page));
807 } else {
Nadav Amit6e4453b2018-09-20 10:30:18 -0700808 cmd = op == VMW_BALLOON_INFLATE ? VMW_BALLOON_CMD_LOCK :
809 VMW_BALLOON_CMD_UNLOCK;
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700810 pfn = page_to_pfn(b->page);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700811
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700812 /* In non-batching mode, PFNs must fit in 32-bit */
813 if (unlikely(pfn != (u32)pfn))
814 return VMW_BALLOON_ERROR_PPN_INVALID;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700815 }
816
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700817 return vmballoon_cmd(b, cmd, pfn, num_pages);
818}
819
Nadav Amit6e4453b2018-09-20 10:30:18 -0700820/**
821 * vmballoon_add_page - adds a page towards lock/unlock operation.
822 *
823 * @b: pointer to the balloon.
824 * @idx: index of the page to be ballooned in this batch.
825 * @p: pointer to the page that is about to be ballooned.
826 *
827 * Adds the page to be ballooned. Must be called while holding @comm_lock.
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400828 */
Nadav Amit6e4453b2018-09-20 10:30:18 -0700829static void vmballoon_add_page(struct vmballoon *b, unsigned int idx,
830 struct page *p)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400831{
Nadav Amit6e4453b2018-09-20 10:30:18 -0700832 lockdep_assert_held(&b->comm_lock);
833
834 if (static_branch_likely(&vmw_balloon_batching))
835 b->batch_page[idx] = (struct vmballoon_batch_entry)
836 { .pfn = page_to_pfn(p) };
837 else
838 b->page = p;
839}
840
841/**
842 * vmballoon_lock - lock or unlock a batch of pages.
843 *
844 * @b: pointer to the balloon.
845 * @ctl: pointer for the %struct vmballoon_ctl, which defines the operation.
846 *
847 * Notifies the host of about ballooned pages (after inflation or deflation,
848 * according to @ctl). If the host rejects the page put it on the
849 * @ctl refuse list. These refused page are then released when moving to the
850 * next size of pages.
851 *
852 * Note that we neither free any @page here nor put them back on the ballooned
853 * pages list. Instead we queue it for later processing. We do that for several
854 * reasons. First, we do not want to free the page under the lock. Second, it
855 * allows us to unify the handling of lock and unlock. In the inflate case, the
856 * caller will check if there are too many refused pages and release them.
857 * Although it is not identical to the past behavior, it should not affect
858 * performance.
859 */
860static int vmballoon_lock(struct vmballoon *b, struct vmballoon_ctl *ctl)
861{
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700862 unsigned long batch_status;
Nadav Amit6e4453b2018-09-20 10:30:18 -0700863 struct page *page;
864 unsigned int i, num_pages;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700865
Nadav Amit6e4453b2018-09-20 10:30:18 -0700866 num_pages = ctl->n_pages;
867 if (num_pages == 0)
868 return 0;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700869
Nadav Amit6e4453b2018-09-20 10:30:18 -0700870 /* communication with the host is done under the communication lock */
871 spin_lock(&b->comm_lock);
872
873 i = 0;
874 list_for_each_entry(page, &ctl->pages, lru)
875 vmballoon_add_page(b, i++, page);
876
877 batch_status = vmballoon_lock_op(b, ctl->n_pages, ctl->page_size,
878 ctl->op);
879
880 /*
881 * Iterate over the pages in the provided list. Since we are changing
882 * @ctl->n_pages we are saving the original value in @num_pages and
883 * use this value to bound the loop.
884 */
Xavier Deguillardf220a802015-08-06 15:17:58 -0700885 for (i = 0; i < num_pages; i++) {
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700886 unsigned long status;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700887
Nadav Amit6e4453b2018-09-20 10:30:18 -0700888 status = vmballoon_status_page(b, i, &page);
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700889
890 /*
891 * Failure of the whole batch overrides a single operation
892 * results.
893 */
894 if (batch_status != VMW_BALLOON_SUCCESS)
895 status = batch_status;
896
Nadav Amit6e4453b2018-09-20 10:30:18 -0700897 /* Continue if no error happened */
898 if (!vmballoon_handle_one_result(b, page, ctl->page_size,
899 status))
900 continue;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700901
Nadav Amit6e4453b2018-09-20 10:30:18 -0700902 /*
903 * Error happened. Move the pages to the refused list and update
904 * the pages number.
905 */
906 list_move(&page->lru, &ctl->refused_pages);
907 ctl->n_pages--;
908 ctl->n_refused_pages++;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700909 }
910
Nadav Amit6e4453b2018-09-20 10:30:18 -0700911 spin_unlock(&b->comm_lock);
912
Nadav Amitdf8d0d42018-09-20 10:30:12 -0700913 return batch_status == VMW_BALLOON_SUCCESS ? 0 : -EIO;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700914}
915
Nadav Amit6e4453b2018-09-20 10:30:18 -0700916/**
917 * vmballoon_release_page_list() - Releases a page list
918 *
919 * @page_list: list of pages to release.
920 * @n_pages: pointer to the number of pages.
921 * @page_size: whether the pages in the list are 2MB (or else 4KB).
922 *
923 * Releases the list of pages and zeros the number of pages.
924 */
925static void vmballoon_release_page_list(struct list_head *page_list,
926 int *n_pages,
927 enum vmballoon_page_size_type page_size)
928{
929 struct page *page, *tmp;
930
931 list_for_each_entry_safe(page, tmp, page_list, lru) {
932 list_del(&page->lru);
David Hildenbrand81655402019-03-05 15:42:41 -0800933 vmballoon_mark_page_online(page, page_size);
Nadav Amit6e4453b2018-09-20 10:30:18 -0700934 __free_pages(page, vmballoon_page_order(page_size));
935 }
936
Nadav Amitae297832019-04-25 04:54:45 -0700937 if (n_pages)
938 *n_pages = 0;
Nadav Amit6e4453b2018-09-20 10:30:18 -0700939}
940
941
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400942/*
943 * Release pages that were allocated while attempting to inflate the
944 * balloon but were refused by the host for one reason or another.
945 */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700946static void vmballoon_release_refused_pages(struct vmballoon *b,
Nadav Amit6e4453b2018-09-20 10:30:18 -0700947 struct vmballoon_ctl *ctl)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400948{
Nadav Amit6e4453b2018-09-20 10:30:18 -0700949 vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_REFUSED_FREE,
950 ctl->page_size);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400951
Nadav Amit6e4453b2018-09-20 10:30:18 -0700952 vmballoon_release_page_list(&ctl->refused_pages, &ctl->n_refused_pages,
953 ctl->page_size);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700954}
955
Nadav Amit8b079cd2018-09-20 10:30:15 -0700956/**
957 * vmballoon_change - retrieve the required balloon change
958 *
959 * @b: pointer for the balloon.
960 *
961 * Return: the required change for the balloon size. A positive number
962 * indicates inflation, a negative number indicates a deflation.
963 */
964static int64_t vmballoon_change(struct vmballoon *b)
965{
966 int64_t size, target;
967
Nadav Amit6e4453b2018-09-20 10:30:18 -0700968 size = atomic64_read(&b->size);
969 target = READ_ONCE(b->target);
Nadav Amit8b079cd2018-09-20 10:30:15 -0700970
971 /*
972 * We must cast first because of int sizes
973 * Otherwise we might get huge positives instead of negatives
974 */
975
976 if (b->reset_required)
977 return 0;
978
979 /* consider a 2MB slack on deflate, unless the balloon is emptied */
Nadav Amit6e4453b2018-09-20 10:30:18 -0700980 if (target < size && target != 0 &&
981 size - target < vmballoon_page_in_frames(VMW_BALLOON_2M_PAGE))
Nadav Amit8b079cd2018-09-20 10:30:15 -0700982 return 0;
983
Nadav Amit5d1a86e2019-04-25 04:54:44 -0700984 /* If an out-of-memory recently occurred, inflation is disallowed. */
985 if (target > size && time_before(jiffies, READ_ONCE(b->shrink_timeout)))
986 return 0;
987
Nadav Amit8b079cd2018-09-20 10:30:15 -0700988 return target - size;
989}
990
Nadav Amit6e4453b2018-09-20 10:30:18 -0700991/**
992 * vmballoon_enqueue_page_list() - Enqueues list of pages after inflation.
993 *
994 * @b: pointer to balloon.
995 * @pages: list of pages to enqueue.
996 * @n_pages: pointer to number of pages in list. The value is zeroed.
997 * @page_size: whether the pages are 2MB or 4KB pages.
998 *
999 * Enqueues the provides list of pages in the ballooned page list, clears the
1000 * list and zeroes the number of pages that was provided.
1001 */
1002static void vmballoon_enqueue_page_list(struct vmballoon *b,
1003 struct list_head *pages,
1004 unsigned int *n_pages,
1005 enum vmballoon_page_size_type page_size)
1006{
Nadav Amit83a8afa2019-04-25 04:54:43 -07001007 unsigned long flags;
Nadav Amit6e4453b2018-09-20 10:30:18 -07001008
Nadav Amit83a8afa2019-04-25 04:54:43 -07001009 if (page_size == VMW_BALLOON_4K_PAGE) {
1010 balloon_page_list_enqueue(&b->b_dev_info, pages);
1011 } else {
1012 /*
1013 * Keep the huge pages in a local list which is not available
1014 * for the balloon compaction mechanism.
1015 */
1016 spin_lock_irqsave(&b->b_dev_info.pages_lock, flags);
1017 list_splice_init(pages, &b->huge_pages);
1018 __count_vm_events(BALLOON_INFLATE, *n_pages *
1019 vmballoon_page_in_frames(VMW_BALLOON_2M_PAGE));
1020 spin_unlock_irqrestore(&b->b_dev_info.pages_lock, flags);
1021 }
1022
Nadav Amit6e4453b2018-09-20 10:30:18 -07001023 *n_pages = 0;
1024}
1025
1026/**
1027 * vmballoon_dequeue_page_list() - Dequeues page lists for deflation.
1028 *
1029 * @b: pointer to balloon.
1030 * @pages: list of pages to enqueue.
1031 * @n_pages: pointer to number of pages in list. The value is zeroed.
1032 * @page_size: whether the pages are 2MB or 4KB pages.
1033 * @n_req_pages: the number of requested pages.
1034 *
1035 * Dequeues the number of requested pages from the balloon for deflation. The
1036 * number of dequeued pages may be lower, if not enough pages in the requested
1037 * size are available.
1038 */
1039static void vmballoon_dequeue_page_list(struct vmballoon *b,
1040 struct list_head *pages,
1041 unsigned int *n_pages,
1042 enum vmballoon_page_size_type page_size,
1043 unsigned int n_req_pages)
1044{
Nadav Amit6e4453b2018-09-20 10:30:18 -07001045 struct page *page, *tmp;
1046 unsigned int i = 0;
Nadav Amit83a8afa2019-04-25 04:54:43 -07001047 unsigned long flags;
Nadav Amit6e4453b2018-09-20 10:30:18 -07001048
Nadav Amit83a8afa2019-04-25 04:54:43 -07001049 /* In the case of 4k pages, use the compaction infrastructure */
1050 if (page_size == VMW_BALLOON_4K_PAGE) {
1051 *n_pages = balloon_page_list_dequeue(&b->b_dev_info, pages,
1052 n_req_pages);
1053 return;
1054 }
1055
1056 /* 2MB pages */
1057 spin_lock_irqsave(&b->b_dev_info.pages_lock, flags);
1058 list_for_each_entry_safe(page, tmp, &b->huge_pages, lru) {
Nadav Amit6e4453b2018-09-20 10:30:18 -07001059 list_move(&page->lru, pages);
1060 if (++i == n_req_pages)
1061 break;
1062 }
Nadav Amit83a8afa2019-04-25 04:54:43 -07001063
1064 __count_vm_events(BALLOON_DEFLATE,
1065 i * vmballoon_page_in_frames(VMW_BALLOON_2M_PAGE));
1066 spin_unlock_irqrestore(&b->b_dev_info.pages_lock, flags);
Nadav Amit6e4453b2018-09-20 10:30:18 -07001067 *n_pages = i;
1068}
1069
1070/**
Nadav Amitae297832019-04-25 04:54:45 -07001071 * vmballoon_split_refused_pages() - Split the 2MB refused pages to 4k.
1072 *
1073 * If inflation of 2MB pages was denied by the hypervisor, it is likely to be
1074 * due to one or few 4KB pages. These 2MB pages may keep being allocated and
1075 * then being refused. To prevent this case, this function splits the refused
1076 * pages into 4KB pages and adds them into @prealloc_pages list.
1077 *
1078 * @ctl: pointer for the %struct vmballoon_ctl, which defines the operation.
1079 */
1080static void vmballoon_split_refused_pages(struct vmballoon_ctl *ctl)
1081{
1082 struct page *page, *tmp;
1083 unsigned int i, order;
1084
1085 order = vmballoon_page_order(ctl->page_size);
1086
1087 list_for_each_entry_safe(page, tmp, &ctl->refused_pages, lru) {
1088 list_del(&page->lru);
1089 split_page(page, order);
1090 for (i = 0; i < (1 << order); i++)
1091 list_add(&page[i].lru, &ctl->prealloc_pages);
1092 }
1093 ctl->n_refused_pages = 0;
1094}
1095
1096/**
Nadav Amit6e4453b2018-09-20 10:30:18 -07001097 * vmballoon_inflate() - Inflate the balloon towards its target size.
1098 *
1099 * @b: pointer to the balloon.
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001100 */
1101static void vmballoon_inflate(struct vmballoon *b)
1102{
Nadav Amit6e4453b2018-09-20 10:30:18 -07001103 int64_t to_inflate_frames;
1104 struct vmballoon_ctl ctl = {
1105 .pages = LIST_HEAD_INIT(ctl.pages),
1106 .refused_pages = LIST_HEAD_INIT(ctl.refused_pages),
Nadav Amitae297832019-04-25 04:54:45 -07001107 .prealloc_pages = LIST_HEAD_INIT(ctl.prealloc_pages),
Nadav Amit6e4453b2018-09-20 10:30:18 -07001108 .page_size = b->max_page_size,
1109 .op = VMW_BALLOON_INFLATE
1110 };
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001111
Nadav Amit6e4453b2018-09-20 10:30:18 -07001112 while ((to_inflate_frames = vmballoon_change(b)) > 0) {
1113 unsigned int to_inflate_pages, page_in_frames;
1114 int alloc_error, lock_error = 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001115
Nadav Amit6e4453b2018-09-20 10:30:18 -07001116 VM_BUG_ON(!list_empty(&ctl.pages));
1117 VM_BUG_ON(ctl.n_pages != 0);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001118
Nadav Amit6e4453b2018-09-20 10:30:18 -07001119 page_in_frames = vmballoon_page_in_frames(ctl.page_size);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001120
Nadav Amit6e4453b2018-09-20 10:30:18 -07001121 to_inflate_pages = min_t(unsigned long, b->batch_max_pages,
1122 DIV_ROUND_UP_ULL(to_inflate_frames,
1123 page_in_frames));
Nadav Amitc7b36902018-09-20 10:30:17 -07001124
Nadav Amit6e4453b2018-09-20 10:30:18 -07001125 /* Start by allocating */
1126 alloc_error = vmballoon_alloc_page_list(b, &ctl,
1127 to_inflate_pages);
Nadav Amitc7b36902018-09-20 10:30:17 -07001128
Nadav Amit6e4453b2018-09-20 10:30:18 -07001129 /* Actually lock the pages by telling the hypervisor */
1130 lock_error = vmballoon_lock(b, &ctl);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001131
Nadav Amit6e4453b2018-09-20 10:30:18 -07001132 /*
1133 * If an error indicates that something serious went wrong,
1134 * stop the inflation.
1135 */
1136 if (lock_error)
Nadav Amit622074a2018-09-20 10:30:11 -07001137 break;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001138
Nadav Amit6e4453b2018-09-20 10:30:18 -07001139 /* Update the balloon size */
1140 atomic64_add(ctl.n_pages * page_in_frames, &b->size);
Nadav Amit8fa3c612018-09-20 10:30:13 -07001141
Nadav Amit6e4453b2018-09-20 10:30:18 -07001142 vmballoon_enqueue_page_list(b, &ctl.pages, &ctl.n_pages,
1143 ctl.page_size);
Nadav Amit10a95d52018-09-20 10:30:07 -07001144
Nadav Amit6e4453b2018-09-20 10:30:18 -07001145 /*
1146 * If allocation failed or the number of refused pages exceeds
1147 * the maximum allowed, move to the next page size.
1148 */
1149 if (alloc_error ||
1150 ctl.n_refused_pages >= VMW_BALLOON_MAX_REFUSED) {
1151 if (ctl.page_size == VMW_BALLOON_4K_PAGE)
1152 break;
Nadav Amit8fa3c612018-09-20 10:30:13 -07001153
1154 /*
Nadav Amitae297832019-04-25 04:54:45 -07001155 * Split the refused pages to 4k. This will also empty
1156 * the refused pages list.
Nadav Amit8fa3c612018-09-20 10:30:13 -07001157 */
Nadav Amitae297832019-04-25 04:54:45 -07001158 vmballoon_split_refused_pages(&ctl);
Nadav Amit6e4453b2018-09-20 10:30:18 -07001159 ctl.page_size--;
Xavier Deguillardf220a802015-08-06 15:17:58 -07001160 }
Xavier Deguillardef0f8f12015-06-12 11:43:22 -07001161
Philip P. Moltmann33d268e2015-08-06 15:18:01 -07001162 cond_resched();
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001163 }
1164
Nadav Amit6e4453b2018-09-20 10:30:18 -07001165 /*
1166 * Release pages that were allocated while attempting to inflate the
1167 * balloon but were refused by the host for one reason or another,
1168 * and update the statistics.
1169 */
1170 if (ctl.n_refused_pages != 0)
1171 vmballoon_release_refused_pages(b, &ctl);
Nadav Amitae297832019-04-25 04:54:45 -07001172
1173 vmballoon_release_page_list(&ctl.prealloc_pages, NULL, ctl.page_size);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001174}
1175
Nadav Amit6e4453b2018-09-20 10:30:18 -07001176/**
1177 * vmballoon_deflate() - Decrease the size of the balloon.
1178 *
1179 * @b: pointer to the balloon
1180 * @n_frames: the number of frames to deflate. If zero, automatically
1181 * calculated according to the target size.
1182 * @coordinated: whether to coordinate with the host
1183 *
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001184 * Decrease the size of the balloon allowing guest to use more memory.
Nadav Amit6e4453b2018-09-20 10:30:18 -07001185 *
1186 * Return: The number of deflated frames (i.e., basic page size units)
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001187 */
Nadav Amit6e4453b2018-09-20 10:30:18 -07001188static unsigned long vmballoon_deflate(struct vmballoon *b, uint64_t n_frames,
1189 bool coordinated)
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001190{
Nadav Amit6e4453b2018-09-20 10:30:18 -07001191 unsigned long deflated_frames = 0;
1192 unsigned long tried_frames = 0;
1193 struct vmballoon_ctl ctl = {
1194 .pages = LIST_HEAD_INIT(ctl.pages),
1195 .refused_pages = LIST_HEAD_INIT(ctl.refused_pages),
1196 .page_size = VMW_BALLOON_4K_PAGE,
1197 .op = VMW_BALLOON_DEFLATE
1198 };
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001199
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001200 /* free pages to reach target */
Nadav Amit6e4453b2018-09-20 10:30:18 -07001201 while (true) {
1202 unsigned int to_deflate_pages, n_unlocked_frames;
1203 unsigned int page_in_frames;
1204 int64_t to_deflate_frames;
1205 bool deflated_all;
Xavier Deguillardf220a802015-08-06 15:17:58 -07001206
Nadav Amit6e4453b2018-09-20 10:30:18 -07001207 page_in_frames = vmballoon_page_in_frames(ctl.page_size);
1208
1209 VM_BUG_ON(!list_empty(&ctl.pages));
1210 VM_BUG_ON(ctl.n_pages);
1211 VM_BUG_ON(!list_empty(&ctl.refused_pages));
1212 VM_BUG_ON(ctl.n_refused_pages);
1213
1214 /*
1215 * If we were requested a specific number of frames, we try to
1216 * deflate this number of frames. Otherwise, deflation is
1217 * performed according to the target and balloon size.
1218 */
1219 to_deflate_frames = n_frames ? n_frames - tried_frames :
1220 -vmballoon_change(b);
1221
1222 /* break if no work to do */
1223 if (to_deflate_frames <= 0)
1224 break;
1225
1226 /*
1227 * Calculate the number of frames based on current page size,
1228 * but limit the deflated frames to a single chunk
1229 */
1230 to_deflate_pages = min_t(unsigned long, b->batch_max_pages,
1231 DIV_ROUND_UP_ULL(to_deflate_frames,
1232 page_in_frames));
1233
1234 /* First take the pages from the balloon pages. */
1235 vmballoon_dequeue_page_list(b, &ctl.pages, &ctl.n_pages,
1236 ctl.page_size, to_deflate_pages);
1237
1238 /*
1239 * Before pages are moving to the refused list, count their
1240 * frames as frames that we tried to deflate.
1241 */
1242 tried_frames += ctl.n_pages * page_in_frames;
1243
1244 /*
1245 * Unlock the pages by communicating with the hypervisor if the
1246 * communication is coordinated (i.e., not pop). We ignore the
1247 * return code. Instead we check if all the pages we manage to
1248 * unlock all the pages. If we failed, we will move to the next
1249 * page size, and would eventually try again later.
1250 */
1251 if (coordinated)
1252 vmballoon_lock(b, &ctl);
1253
1254 /*
1255 * Check if we deflated enough. We will move to the next page
1256 * size if we did not manage to do so. This calculation takes
1257 * place now, as once the pages are released, the number of
1258 * pages is zeroed.
1259 */
1260 deflated_all = (ctl.n_pages == to_deflate_pages);
1261
1262 /* Update local and global counters */
1263 n_unlocked_frames = ctl.n_pages * page_in_frames;
1264 atomic64_sub(n_unlocked_frames, &b->size);
1265 deflated_frames += n_unlocked_frames;
1266
1267 vmballoon_stats_page_add(b, VMW_BALLOON_PAGE_STAT_FREE,
1268 ctl.page_size, ctl.n_pages);
1269
1270 /* free the ballooned pages */
1271 vmballoon_release_page_list(&ctl.pages, &ctl.n_pages,
1272 ctl.page_size);
1273
1274 /* Return the refused pages to the ballooned list. */
1275 vmballoon_enqueue_page_list(b, &ctl.refused_pages,
1276 &ctl.n_refused_pages,
1277 ctl.page_size);
1278
1279 /* If we failed to unlock all the pages, move to next size. */
1280 if (!deflated_all) {
1281 if (ctl.page_size == b->max_page_size)
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001282 break;
Nadav Amit6e4453b2018-09-20 10:30:18 -07001283 ctl.page_size++;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001284 }
1285
Nadav Amit6e4453b2018-09-20 10:30:18 -07001286 cond_resched();
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001287 }
Nadav Amit6e4453b2018-09-20 10:30:18 -07001288
1289 return deflated_frames;
Xavier Deguillardf220a802015-08-06 15:17:58 -07001290}
1291
Nadav Amitdf8d0d42018-09-20 10:30:12 -07001292/**
1293 * vmballoon_deinit_batching - disables batching mode.
1294 *
1295 * @b: pointer to &struct vmballoon.
1296 *
1297 * Disables batching, by deallocating the page for communication with the
1298 * hypervisor and disabling the static key to indicate that batching is off.
1299 */
1300static void vmballoon_deinit_batching(struct vmballoon *b)
1301{
1302 free_page((unsigned long)b->batch_page);
1303 b->batch_page = NULL;
1304 static_branch_disable(&vmw_balloon_batching);
1305 b->batch_max_pages = 1;
1306}
Xavier Deguillardf220a802015-08-06 15:17:58 -07001307
Nadav Amitdf8d0d42018-09-20 10:30:12 -07001308/**
1309 * vmballoon_init_batching - enable batching mode.
1310 *
1311 * @b: pointer to &struct vmballoon.
1312 *
1313 * Enables batching, by allocating a page for communication with the hypervisor
1314 * and enabling the static_key to use batching.
1315 *
1316 * Return: zero on success or an appropriate error-code.
1317 */
1318static int vmballoon_init_batching(struct vmballoon *b)
Xavier Deguillardf220a802015-08-06 15:17:58 -07001319{
Gil Kupferb23220f2018-06-01 00:47:47 -07001320 struct page *page;
1321
1322 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1323 if (!page)
Nadav Amitdf8d0d42018-09-20 10:30:12 -07001324 return -ENOMEM;
Xavier Deguillardf220a802015-08-06 15:17:58 -07001325
Gil Kupferb23220f2018-06-01 00:47:47 -07001326 b->batch_page = page_address(page);
Nadav Amitdf8d0d42018-09-20 10:30:12 -07001327 b->batch_max_pages = PAGE_SIZE / sizeof(struct vmballoon_batch_entry);
1328
1329 static_branch_enable(&vmw_balloon_batching);
1330
1331 return 0;
Xavier Deguillardf220a802015-08-06 15:17:58 -07001332}
1333
1334/*
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001335 * Receive notification and resize balloon
1336 */
1337static void vmballoon_doorbell(void *client_data)
1338{
1339 struct vmballoon *b = client_data;
1340
Nadav Amitc7b36902018-09-20 10:30:17 -07001341 vmballoon_stats_gen_inc(b, VMW_BALLOON_STAT_DOORBELL);
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001342
1343 mod_delayed_work(system_freezable_wq, &b->dwork, 0);
1344}
1345
1346/*
1347 * Clean up vmci doorbell
1348 */
1349static void vmballoon_vmci_cleanup(struct vmballoon *b)
1350{
Nadav Amit10a95d52018-09-20 10:30:07 -07001351 vmballoon_cmd(b, VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
1352 VMCI_INVALID_ID, VMCI_INVALID_ID);
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001353
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001354 if (!vmci_handle_is_invalid(b->vmci_doorbell)) {
1355 vmci_doorbell_destroy(b->vmci_doorbell);
1356 b->vmci_doorbell = VMCI_INVALID_HANDLE;
1357 }
1358}
1359
Nadav Amit22d293e2018-09-20 10:30:19 -07001360/**
1361 * vmballoon_vmci_init - Initialize vmci doorbell.
1362 *
1363 * @b: pointer to the balloon.
1364 *
1365 * Return: zero on success or when wakeup command not supported. Error-code
1366 * otherwise.
1367 *
1368 * Initialize vmci doorbell, to get notified as soon as balloon changes.
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001369 */
1370static int vmballoon_vmci_init(struct vmballoon *b)
1371{
Nadav Amit10a95d52018-09-20 10:30:07 -07001372 unsigned long error;
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001373
Nadav Amitce664332018-06-19 16:00:26 -07001374 if ((b->capabilities & VMW_BALLOON_SIGNALLED_WAKEUP_CMD) == 0)
1375 return 0;
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001376
Nadav Amitce664332018-06-19 16:00:26 -07001377 error = vmci_doorbell_create(&b->vmci_doorbell, VMCI_FLAG_DELAYED_CB,
1378 VMCI_PRIVILEGE_FLAG_RESTRICTED,
1379 vmballoon_doorbell, b);
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001380
Nadav Amitce664332018-06-19 16:00:26 -07001381 if (error != VMCI_SUCCESS)
1382 goto fail;
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001383
Nadav Amit10a95d52018-09-20 10:30:07 -07001384 error = __vmballoon_cmd(b, VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
1385 b->vmci_doorbell.context,
1386 b->vmci_doorbell.resource, NULL);
Nadav Amitce664332018-06-19 16:00:26 -07001387
Nadav Amitce664332018-06-19 16:00:26 -07001388 if (error != VMW_BALLOON_SUCCESS)
1389 goto fail;
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001390
1391 return 0;
Nadav Amitce664332018-06-19 16:00:26 -07001392fail:
1393 vmballoon_vmci_cleanup(b);
1394 return -EIO;
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001395}
1396
Nadav Amit6e4453b2018-09-20 10:30:18 -07001397/**
1398 * vmballoon_pop - Quickly release all pages allocate for the balloon.
1399 *
1400 * @b: pointer to the balloon.
1401 *
1402 * This function is called when host decides to "reset" balloon for one reason
1403 * or another. Unlike normal "deflate" we do not (shall not) notify host of the
1404 * pages being released.
1405 */
1406static void vmballoon_pop(struct vmballoon *b)
1407{
1408 unsigned long size;
1409
1410 while ((size = atomic64_read(&b->size)))
1411 vmballoon_deflate(b, size, false);
1412}
1413
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001414/*
Xavier Deguillardf220a802015-08-06 15:17:58 -07001415 * Perform standard reset sequence by popping the balloon (in case it
1416 * is not empty) and then restarting protocol. This operation normally
1417 * happens when host responds with VMW_BALLOON_ERROR_RESET to a command.
1418 */
1419static void vmballoon_reset(struct vmballoon *b)
1420{
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001421 int error;
1422
Nadav Amitc7b36902018-09-20 10:30:17 -07001423 down_write(&b->conf_sem);
1424
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001425 vmballoon_vmci_cleanup(b);
1426
Xavier Deguillardf220a802015-08-06 15:17:58 -07001427 /* free all pages, skipping monitor unlock */
1428 vmballoon_pop(b);
1429
Nadav Amit22d293e2018-09-20 10:30:19 -07001430 if (vmballoon_send_start(b, VMW_BALLOON_CAPABILITIES))
Dan Carpenterd04071a2019-02-11 21:45:45 +03001431 goto unlock;
Xavier Deguillardf220a802015-08-06 15:17:58 -07001432
1433 if ((b->capabilities & VMW_BALLOON_BATCHED_CMDS) != 0) {
Nadav Amitdf8d0d42018-09-20 10:30:12 -07001434 if (vmballoon_init_batching(b)) {
Xavier Deguillardf220a802015-08-06 15:17:58 -07001435 /*
1436 * We failed to initialize batching, inform the monitor
1437 * about it by sending a null capability.
1438 *
1439 * The guest will retry in one second.
1440 */
1441 vmballoon_send_start(b, 0);
Dan Carpenterd04071a2019-02-11 21:45:45 +03001442 goto unlock;
Xavier Deguillardf220a802015-08-06 15:17:58 -07001443 }
1444 } else if ((b->capabilities & VMW_BALLOON_BASIC_CMDS) != 0) {
Nadav Amitdf8d0d42018-09-20 10:30:12 -07001445 vmballoon_deinit_batching(b);
Xavier Deguillardf220a802015-08-06 15:17:58 -07001446 }
1447
Nadav Amit8840a6f2018-09-20 10:30:20 -07001448 vmballoon_stats_gen_inc(b, VMW_BALLOON_STAT_RESET);
Xavier Deguillardf220a802015-08-06 15:17:58 -07001449 b->reset_required = false;
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001450
1451 error = vmballoon_vmci_init(b);
1452 if (error)
1453 pr_err("failed to initialize vmci doorbell\n");
1454
Nadav Amit22d293e2018-09-20 10:30:19 -07001455 if (vmballoon_send_guest_id(b))
Xavier Deguillardf220a802015-08-06 15:17:58 -07001456 pr_err("failed to send guest ID to the host\n");
Nadav Amitc7b36902018-09-20 10:30:17 -07001457
Dan Carpenterd04071a2019-02-11 21:45:45 +03001458unlock:
Nadav Amitc7b36902018-09-20 10:30:17 -07001459 up_write(&b->conf_sem);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001460}
1461
Nadav Amit8b079cd2018-09-20 10:30:15 -07001462/**
1463 * vmballoon_work - periodic balloon worker for reset, inflation and deflation.
1464 *
1465 * @work: pointer to the &work_struct which is provided by the workqueue.
1466 *
1467 * Resets the protocol if needed, gets the new size and adjusts balloon as
1468 * needed. Repeat in 1 sec.
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001469 */
1470static void vmballoon_work(struct work_struct *work)
1471{
1472 struct delayed_work *dwork = to_delayed_work(work);
1473 struct vmballoon *b = container_of(dwork, struct vmballoon, dwork);
Nadav Amit8b079cd2018-09-20 10:30:15 -07001474 int64_t change = 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001475
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001476 if (b->reset_required)
1477 vmballoon_reset(b);
1478
Nadav Amitc7b36902018-09-20 10:30:17 -07001479 down_read(&b->conf_sem);
1480
1481 /*
1482 * Update the stats while holding the semaphore to ensure that
1483 * @stats_enabled is consistent with whether the stats are actually
1484 * enabled
1485 */
1486 vmballoon_stats_gen_inc(b, VMW_BALLOON_STAT_TIMER);
1487
Nadav Amit0395be32018-09-20 10:30:16 -07001488 if (!vmballoon_send_get_target(b))
Nadav Amit8b079cd2018-09-20 10:30:15 -07001489 change = vmballoon_change(b);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001490
Nadav Amit8b079cd2018-09-20 10:30:15 -07001491 if (change != 0) {
Nadav Amit6e4453b2018-09-20 10:30:18 -07001492 pr_debug("%s - size: %llu, target %lu\n", __func__,
1493 atomic64_read(&b->size), READ_ONCE(b->target));
Nadav Amit8b079cd2018-09-20 10:30:15 -07001494
1495 if (change > 0)
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001496 vmballoon_inflate(b);
Nadav Amit8b079cd2018-09-20 10:30:15 -07001497 else /* (change < 0) */
Nadav Amit6e4453b2018-09-20 10:30:18 -07001498 vmballoon_deflate(b, 0, true);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001499 }
1500
Nadav Amitc7b36902018-09-20 10:30:17 -07001501 up_read(&b->conf_sem);
1502
Dmitry Torokhovbeda94d2011-07-26 16:08:56 -07001503 /*
1504 * We are using a freezable workqueue so that balloon operations are
1505 * stopped while the system transitions to/from sleep/hibernation.
1506 */
1507 queue_delayed_work(system_freezable_wq,
1508 dwork, round_jiffies_relative(HZ));
Nadav Amitc7b36902018-09-20 10:30:17 -07001509
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001510}
1511
Nadav Amit5d1a86e2019-04-25 04:54:44 -07001512/**
1513 * vmballoon_shrinker_scan() - deflate the balloon due to memory pressure.
1514 * @shrinker: pointer to the balloon shrinker.
1515 * @sc: page reclaim information.
1516 *
1517 * Returns: number of pages that were freed during deflation.
1518 */
1519static unsigned long vmballoon_shrinker_scan(struct shrinker *shrinker,
1520 struct shrink_control *sc)
1521{
1522 struct vmballoon *b = &balloon;
1523 unsigned long deflated_frames;
1524
1525 pr_debug("%s - size: %llu", __func__, atomic64_read(&b->size));
1526
1527 vmballoon_stats_gen_inc(b, VMW_BALLOON_STAT_SHRINK);
1528
1529 /*
1530 * If the lock is also contended for read, we cannot easily reclaim and
1531 * we bail out.
1532 */
1533 if (!down_read_trylock(&b->conf_sem))
1534 return 0;
1535
1536 deflated_frames = vmballoon_deflate(b, sc->nr_to_scan, true);
1537
1538 vmballoon_stats_gen_add(b, VMW_BALLOON_STAT_SHRINK_FREE,
1539 deflated_frames);
1540
1541 /*
1542 * Delay future inflation for some time to mitigate the situations in
1543 * which balloon continuously grows and shrinks. Use WRITE_ONCE() since
1544 * the access is asynchronous.
1545 */
1546 WRITE_ONCE(b->shrink_timeout, jiffies + HZ * VMBALLOON_SHRINK_DELAY);
1547
1548 up_read(&b->conf_sem);
1549
1550 return deflated_frames;
1551}
1552
1553/**
1554 * vmballoon_shrinker_count() - return the number of ballooned pages.
1555 * @shrinker: pointer to the balloon shrinker.
1556 * @sc: page reclaim information.
1557 *
1558 * Returns: number of 4k pages that are allocated for the balloon and can
1559 * therefore be reclaimed under pressure.
1560 */
1561static unsigned long vmballoon_shrinker_count(struct shrinker *shrinker,
1562 struct shrink_control *sc)
1563{
1564 struct vmballoon *b = &balloon;
1565
1566 return atomic64_read(&b->size);
1567}
1568
1569static void vmballoon_unregister_shrinker(struct vmballoon *b)
1570{
1571 if (b->shrinker_registered)
1572 unregister_shrinker(&b->shrinker);
1573 b->shrinker_registered = false;
1574}
1575
1576static int vmballoon_register_shrinker(struct vmballoon *b)
1577{
1578 int r;
1579
1580 /* Do nothing if the shrinker is not enabled */
1581 if (!vmwballoon_shrinker_enable)
1582 return 0;
1583
1584 b->shrinker.scan_objects = vmballoon_shrinker_scan;
1585 b->shrinker.count_objects = vmballoon_shrinker_count;
1586 b->shrinker.seeks = DEFAULT_SEEKS;
1587
1588 r = register_shrinker(&b->shrinker);
1589
1590 if (r == 0)
1591 b->shrinker_registered = true;
1592
1593 return r;
1594}
1595
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001596/*
1597 * DEBUGFS Interface
1598 */
1599#ifdef CONFIG_DEBUG_FS
1600
Nadav Amitc7b36902018-09-20 10:30:17 -07001601static const char * const vmballoon_stat_page_names[] = {
1602 [VMW_BALLOON_PAGE_STAT_ALLOC] = "alloc",
1603 [VMW_BALLOON_PAGE_STAT_ALLOC_FAIL] = "allocFail",
1604 [VMW_BALLOON_PAGE_STAT_REFUSED_ALLOC] = "errAlloc",
1605 [VMW_BALLOON_PAGE_STAT_REFUSED_FREE] = "errFree",
1606 [VMW_BALLOON_PAGE_STAT_FREE] = "free"
1607};
1608
1609static const char * const vmballoon_stat_names[] = {
1610 [VMW_BALLOON_STAT_TIMER] = "timer",
Nadav Amit8840a6f2018-09-20 10:30:20 -07001611 [VMW_BALLOON_STAT_DOORBELL] = "doorbell",
1612 [VMW_BALLOON_STAT_RESET] = "reset",
Nadav Amit5d1a86e2019-04-25 04:54:44 -07001613 [VMW_BALLOON_STAT_SHRINK] = "shrink",
1614 [VMW_BALLOON_STAT_SHRINK_FREE] = "shrinkFree"
Nadav Amitc7b36902018-09-20 10:30:17 -07001615};
1616
Nadav Amitc7b36902018-09-20 10:30:17 -07001617static int vmballoon_enable_stats(struct vmballoon *b)
1618{
1619 int r = 0;
1620
1621 down_write(&b->conf_sem);
1622
1623 /* did we somehow race with another reader which enabled stats? */
1624 if (b->stats)
1625 goto out;
1626
1627 b->stats = kzalloc(sizeof(*b->stats), GFP_KERNEL);
1628
1629 if (!b->stats) {
1630 /* allocation failed */
1631 r = -ENOMEM;
1632 goto out;
1633 }
1634 static_key_enable(&balloon_stat_enabled.key);
1635out:
1636 up_write(&b->conf_sem);
1637 return r;
1638}
1639
1640/**
1641 * vmballoon_debug_show - shows statistics of balloon operations.
1642 * @f: pointer to the &struct seq_file.
1643 * @offset: ignored.
1644 *
1645 * Provides the statistics that can be accessed in vmmemctl in the debugfs.
1646 * To avoid the overhead - mainly that of memory - of collecting the statistics,
1647 * we only collect statistics after the first time the counters are read.
1648 *
1649 * Return: zero on success or an error code.
1650 */
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001651static int vmballoon_debug_show(struct seq_file *f, void *offset)
1652{
1653 struct vmballoon *b = f->private;
Nadav Amitc7b36902018-09-20 10:30:17 -07001654 int i, j;
1655
1656 /* enables stats if they are disabled */
1657 if (!b->stats) {
1658 int r = vmballoon_enable_stats(b);
1659
1660 if (r)
1661 return r;
1662 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001663
Philip P. Moltmannb36e89d2015-08-06 15:18:00 -07001664 /* format capabilities info */
Nadav Amit6e4453b2018-09-20 10:30:18 -07001665 seq_printf(f, "%-22s: %#16x\n", "balloon capabilities",
Nadav Amitc7b36902018-09-20 10:30:17 -07001666 VMW_BALLOON_CAPABILITIES);
Nadav Amit6e4453b2018-09-20 10:30:18 -07001667 seq_printf(f, "%-22s: %#16lx\n", "used capabilities", b->capabilities);
Nadav Amitc7b36902018-09-20 10:30:17 -07001668 seq_printf(f, "%-22s: %16s\n", "is resetting",
1669 b->reset_required ? "y" : "n");
Philip P. Moltmannb36e89d2015-08-06 15:18:00 -07001670
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001671 /* format size info */
Nadav Amit6e4453b2018-09-20 10:30:18 -07001672 seq_printf(f, "%-22s: %16lu\n", "target", READ_ONCE(b->target));
1673 seq_printf(f, "%-22s: %16llu\n", "current", atomic64_read(&b->size));
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001674
Nadav Amit68131182018-09-20 10:30:08 -07001675 for (i = 0; i < VMW_BALLOON_CMD_NUM; i++) {
1676 if (vmballoon_cmd_names[i] == NULL)
1677 continue;
1678
Nadav Amitc7b36902018-09-20 10:30:17 -07001679 seq_printf(f, "%-22s: %16llu (%llu failed)\n",
1680 vmballoon_cmd_names[i],
1681 atomic64_read(&b->stats->ops[i][VMW_BALLOON_OP_STAT]),
1682 atomic64_read(&b->stats->ops[i][VMW_BALLOON_OP_FAIL_STAT]));
Nadav Amit68131182018-09-20 10:30:08 -07001683 }
1684
Nadav Amitc7b36902018-09-20 10:30:17 -07001685 for (i = 0; i < VMW_BALLOON_STAT_NUM; i++)
1686 seq_printf(f, "%-22s: %16llu\n",
1687 vmballoon_stat_names[i],
1688 atomic64_read(&b->stats->general_stat[i]));
1689
1690 for (i = 0; i < VMW_BALLOON_PAGE_STAT_NUM; i++) {
1691 for (j = 0; j < VMW_BALLOON_NUM_PAGE_SIZES; j++)
1692 seq_printf(f, "%-18s(%s): %16llu\n",
1693 vmballoon_stat_page_names[i],
1694 vmballoon_page_size_names[j],
1695 atomic64_read(&b->stats->page_stat[i][j]));
1696 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001697
1698 return 0;
1699}
1700
Yangtao Li2796b432018-12-01 12:05:30 -05001701DEFINE_SHOW_ATTRIBUTE(vmballoon_debug);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001702
Greg Kroah-Hartman225afca2019-06-11 20:55:28 +02001703static void __init vmballoon_debugfs_init(struct vmballoon *b)
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001704{
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001705 b->dbg_entry = debugfs_create_file("vmmemctl", S_IRUGO, NULL, b,
1706 &vmballoon_debug_fops);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001707}
1708
1709static void __exit vmballoon_debugfs_exit(struct vmballoon *b)
1710{
Nadav Amitc7b36902018-09-20 10:30:17 -07001711 static_key_disable(&balloon_stat_enabled.key);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001712 debugfs_remove(b->dbg_entry);
Nadav Amitc7b36902018-09-20 10:30:17 -07001713 kfree(b->stats);
1714 b->stats = NULL;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001715}
1716
1717#else
1718
Greg Kroah-Hartman225afca2019-06-11 20:55:28 +02001719static inline void vmballoon_debugfs_init(struct vmballoon *b)
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001720{
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001721}
1722
1723static inline void vmballoon_debugfs_exit(struct vmballoon *b)
1724{
1725}
1726
1727#endif /* CONFIG_DEBUG_FS */
1728
Nadav Amit83a8afa2019-04-25 04:54:43 -07001729
1730#ifdef CONFIG_BALLOON_COMPACTION
1731
Linus Torvalds933a90b2019-07-19 10:42:02 -07001732static int vmballoon_init_fs_context(struct fs_context *fc)
Nadav Amit83a8afa2019-04-25 04:54:43 -07001733{
Linus Torvalds933a90b2019-07-19 10:42:02 -07001734 return init_pseudo(fc, BALLOON_VMW_MAGIC) ? 0 : -ENOMEM;
Nadav Amit83a8afa2019-04-25 04:54:43 -07001735}
1736
1737static struct file_system_type vmballoon_fs = {
Linus Torvalds933a90b2019-07-19 10:42:02 -07001738 .name = "balloon-vmware",
1739 .init_fs_context = vmballoon_init_fs_context,
1740 .kill_sb = kill_anon_super,
Nadav Amit83a8afa2019-04-25 04:54:43 -07001741};
1742
1743static struct vfsmount *vmballoon_mnt;
1744
1745/**
1746 * vmballoon_migratepage() - migrates a balloon page.
1747 * @b_dev_info: balloon device information descriptor.
1748 * @newpage: the page to which @page should be migrated.
1749 * @page: a ballooned page that should be migrated.
1750 * @mode: migration mode, ignored.
1751 *
1752 * This function is really open-coded, but that is according to the interface
1753 * that balloon_compaction provides.
1754 *
1755 * Return: zero on success, -EAGAIN when migration cannot be performed
1756 * momentarily, and -EBUSY if migration failed and should be retried
1757 * with that specific page.
1758 */
1759static int vmballoon_migratepage(struct balloon_dev_info *b_dev_info,
1760 struct page *newpage, struct page *page,
1761 enum migrate_mode mode)
1762{
1763 unsigned long status, flags;
1764 struct vmballoon *b;
1765 int ret;
1766
1767 b = container_of(b_dev_info, struct vmballoon, b_dev_info);
1768
1769 /*
1770 * If the semaphore is taken, there is ongoing configuration change
1771 * (i.e., balloon reset), so try again.
1772 */
1773 if (!down_read_trylock(&b->conf_sem))
1774 return -EAGAIN;
1775
1776 spin_lock(&b->comm_lock);
1777 /*
1778 * We must start by deflating and not inflating, as otherwise the
1779 * hypervisor may tell us that it has enough memory and the new page is
1780 * not needed. Since the old page is isolated, we cannot use the list
1781 * interface to unlock it, as the LRU field is used for isolation.
1782 * Instead, we use the native interface directly.
1783 */
1784 vmballoon_add_page(b, 0, page);
1785 status = vmballoon_lock_op(b, 1, VMW_BALLOON_4K_PAGE,
1786 VMW_BALLOON_DEFLATE);
1787
1788 if (status == VMW_BALLOON_SUCCESS)
1789 status = vmballoon_status_page(b, 0, &page);
1790
1791 /*
1792 * If a failure happened, let the migration mechanism know that it
1793 * should not retry.
1794 */
1795 if (status != VMW_BALLOON_SUCCESS) {
1796 spin_unlock(&b->comm_lock);
1797 ret = -EBUSY;
1798 goto out_unlock;
1799 }
1800
1801 /*
1802 * The page is isolated, so it is safe to delete it without holding
1803 * @pages_lock . We keep holding @comm_lock since we will need it in a
1804 * second.
1805 */
1806 balloon_page_delete(page);
1807
1808 put_page(page);
1809
1810 /* Inflate */
1811 vmballoon_add_page(b, 0, newpage);
1812 status = vmballoon_lock_op(b, 1, VMW_BALLOON_4K_PAGE,
1813 VMW_BALLOON_INFLATE);
1814
1815 if (status == VMW_BALLOON_SUCCESS)
1816 status = vmballoon_status_page(b, 0, &newpage);
1817
1818 spin_unlock(&b->comm_lock);
1819
1820 if (status != VMW_BALLOON_SUCCESS) {
1821 /*
1822 * A failure happened. While we can deflate the page we just
1823 * inflated, this deflation can also encounter an error. Instead
1824 * we will decrease the size of the balloon to reflect the
1825 * change and report failure.
1826 */
1827 atomic64_dec(&b->size);
1828 ret = -EBUSY;
1829 } else {
1830 /*
1831 * Success. Take a reference for the page, and we will add it to
1832 * the list after acquiring the lock.
1833 */
1834 get_page(newpage);
1835 ret = MIGRATEPAGE_SUCCESS;
1836 }
1837
1838 /* Update the balloon list under the @pages_lock */
1839 spin_lock_irqsave(&b->b_dev_info.pages_lock, flags);
1840
1841 /*
1842 * On inflation success, we already took a reference for the @newpage.
1843 * If we succeed just insert it to the list and update the statistics
1844 * under the lock.
1845 */
1846 if (ret == MIGRATEPAGE_SUCCESS) {
1847 balloon_page_insert(&b->b_dev_info, newpage);
1848 __count_vm_event(BALLOON_MIGRATE);
1849 }
1850
1851 /*
1852 * We deflated successfully, so regardless to the inflation success, we
1853 * need to reduce the number of isolated_pages.
1854 */
1855 b->b_dev_info.isolated_pages--;
1856 spin_unlock_irqrestore(&b->b_dev_info.pages_lock, flags);
1857
1858out_unlock:
1859 up_read(&b->conf_sem);
1860 return ret;
1861}
1862
1863/**
1864 * vmballoon_compaction_deinit() - removes compaction related data.
1865 *
1866 * @b: pointer to the balloon.
1867 */
1868static void vmballoon_compaction_deinit(struct vmballoon *b)
1869{
1870 if (!IS_ERR(b->b_dev_info.inode))
1871 iput(b->b_dev_info.inode);
1872
1873 b->b_dev_info.inode = NULL;
1874 kern_unmount(vmballoon_mnt);
1875 vmballoon_mnt = NULL;
1876}
1877
1878/**
1879 * vmballoon_compaction_init() - initialized compaction for the balloon.
1880 *
1881 * @b: pointer to the balloon.
1882 *
1883 * If during the initialization a failure occurred, this function does not
1884 * perform cleanup. The caller must call vmballoon_compaction_deinit() in this
1885 * case.
1886 *
1887 * Return: zero on success or error code on failure.
1888 */
1889static __init int vmballoon_compaction_init(struct vmballoon *b)
1890{
1891 vmballoon_mnt = kern_mount(&vmballoon_fs);
1892 if (IS_ERR(vmballoon_mnt))
1893 return PTR_ERR(vmballoon_mnt);
1894
1895 b->b_dev_info.migratepage = vmballoon_migratepage;
1896 b->b_dev_info.inode = alloc_anon_inode(vmballoon_mnt->mnt_sb);
1897
1898 if (IS_ERR(b->b_dev_info.inode))
1899 return PTR_ERR(b->b_dev_info.inode);
1900
1901 b->b_dev_info.inode->i_mapping->a_ops = &balloon_aops;
1902 return 0;
1903}
1904
1905#else /* CONFIG_BALLOON_COMPACTION */
1906
1907static void vmballoon_compaction_deinit(struct vmballoon *b)
1908{
1909}
1910
1911static int vmballoon_compaction_init(struct vmballoon *b)
1912{
1913 return 0;
1914}
1915
1916#endif /* CONFIG_BALLOON_COMPACTION */
1917
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001918static int __init vmballoon_init(void)
1919{
1920 int error;
Nadav Amit6e4453b2018-09-20 10:30:18 -07001921
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001922 /*
1923 * Check if we are running on VMware's hypervisor and bail out
1924 * if we are not.
1925 */
Juergen Gross03b2a322017-11-09 14:27:36 +01001926 if (x86_hyper_type != X86_HYPER_VMWARE)
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001927 return -ENODEV;
1928
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001929 INIT_DELAYED_WORK(&balloon.dwork, vmballoon_work);
1930
Nadav Amit5d1a86e2019-04-25 04:54:44 -07001931 error = vmballoon_register_shrinker(&balloon);
1932 if (error)
1933 goto fail;
1934
Nadav Amit83a8afa2019-04-25 04:54:43 -07001935 /*
1936 * Initialization of compaction must be done after the call to
1937 * balloon_devinfo_init() .
1938 */
1939 balloon_devinfo_init(&balloon.b_dev_info);
1940 error = vmballoon_compaction_init(&balloon);
1941 if (error)
1942 goto fail;
1943
1944 INIT_LIST_HEAD(&balloon.huge_pages);
Nadav Amit6e4453b2018-09-20 10:30:18 -07001945 spin_lock_init(&balloon.comm_lock);
Nadav Amitc7b36902018-09-20 10:30:17 -07001946 init_rwsem(&balloon.conf_sem);
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001947 balloon.vmci_doorbell = VMCI_INVALID_HANDLE;
Philip P. Moltmannd7568c12015-08-06 15:18:01 -07001948 balloon.batch_page = NULL;
1949 balloon.page = NULL;
1950 balloon.reset_required = true;
1951
Dmitry Torokhovbeda94d2011-07-26 16:08:56 -07001952 queue_delayed_work(system_freezable_wq, &balloon.dwork, 0);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001953
Linus Torvaldsf632a812019-07-12 12:24:03 -07001954 vmballoon_debugfs_init(&balloon);
1955
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001956 return 0;
Nadav Amit83a8afa2019-04-25 04:54:43 -07001957fail:
Nadav Amit5d1a86e2019-04-25 04:54:44 -07001958 vmballoon_unregister_shrinker(&balloon);
Nadav Amit83a8afa2019-04-25 04:54:43 -07001959 vmballoon_compaction_deinit(&balloon);
1960 return error;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001961}
Nadav Amitc3cc1b02018-06-19 16:00:27 -07001962
1963/*
1964 * Using late_initcall() instead of module_init() allows the balloon to use the
1965 * VMCI doorbell even when the balloon is built into the kernel. Otherwise the
1966 * VMCI is probed only after the balloon is initialized. If the balloon is used
1967 * as a module, late_initcall() is equivalent to module_init().
1968 */
1969late_initcall(vmballoon_init);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001970
1971static void __exit vmballoon_exit(void)
1972{
Nadav Amit5d1a86e2019-04-25 04:54:44 -07001973 vmballoon_unregister_shrinker(&balloon);
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001974 vmballoon_vmci_cleanup(&balloon);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001975 cancel_delayed_work_sync(&balloon.dwork);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001976
1977 vmballoon_debugfs_exit(&balloon);
1978
1979 /*
1980 * Deallocate all reserved memory, and reset connection with monitor.
1981 * Reset connection before deallocating memory to avoid potential for
1982 * additional spurious resets from guest touching deallocated pages.
1983 */
Philip P. Moltmannd7568c12015-08-06 15:18:01 -07001984 vmballoon_send_start(&balloon, 0);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001985 vmballoon_pop(&balloon);
Nadav Amit83a8afa2019-04-25 04:54:43 -07001986
1987 /* Only once we popped the balloon, compaction can be deinit */
1988 vmballoon_compaction_deinit(&balloon);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001989}
1990module_exit(vmballoon_exit);