Blame view

drivers/misc/vmw_balloon.c 54.7 KB
8b4770ec7   Nadav Amit   vmw_balloon: upda...
1
  // SPDX-License-Identifier: GPL-2.0
453dc6593   Dmitry Torokhov   VMware Balloon dr...
2
3
4
  /*
   * VMware Balloon driver.
   *
8b4770ec7   Nadav Amit   vmw_balloon: upda...
5
   * Copyright (C) 2000-2018, VMware, Inc. All Rights Reserved.
453dc6593   Dmitry Torokhov   VMware Balloon dr...
6
   *
453dc6593   Dmitry Torokhov   VMware Balloon dr...
7
8
9
10
11
12
13
14
15
16
17
18
19
   * This is VMware physical memory management driver for Linux. The driver
   * acts like a "balloon" that can be inflated to reclaim physical pages by
   * reserving them in the guest and invalidating them in the monitor,
   * freeing up the underlying machine pages so they can be allocated to
   * other guests.  The balloon can also be deflated to allow the guest to
   * use more physical memory. Higher level policies can control the sizes
   * of balloons in VMs in order to manage physical memory resources.
   */
  
  //#define DEBUG
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  
  #include <linux/types.h>
97976e01d   Sean Christopherson   vmw_balloon: Expl...
20
  #include <linux/io.h>
453dc6593   Dmitry Torokhov   VMware Balloon dr...
21
22
  #include <linux/kernel.h>
  #include <linux/mm.h>
f220a80f0   Xavier Deguillard   VMware balloon: a...
23
  #include <linux/vmalloc.h>
453dc6593   Dmitry Torokhov   VMware Balloon dr...
24
25
26
27
28
  #include <linux/sched.h>
  #include <linux/module.h>
  #include <linux/workqueue.h>
  #include <linux/debugfs.h>
  #include <linux/seq_file.h>
c7b3690fb   Nadav Amit   vmw_balloon: stat...
29
30
  #include <linux/rwsem.h>
  #include <linux/slab.h>
6e4453b32   Nadav Amit   vmw_balloon: rewo...
31
  #include <linux/spinlock.h>
83a8afa72   Nadav Amit   vmw_balloon: Comp...
32
  #include <linux/mount.h>
933a90bf4   Linus Torvalds   Merge branch 'wor...
33
  #include <linux/pseudo_fs.h>
83a8afa72   Nadav Amit   vmw_balloon: Comp...
34
  #include <linux/balloon_compaction.h>
48e3d668b   Philip P. Moltmann   VMware balloon: E...
35
36
  #include <linux/vmw_vmci_defs.h>
  #include <linux/vmw_vmci_api.h>
a10a56980   H. Peter Anvin   Modify the VMware...
37
  #include <asm/hypervisor.h>
453dc6593   Dmitry Torokhov   VMware Balloon dr...
38
39
40
  
  MODULE_AUTHOR("VMware, Inc.");
  MODULE_DESCRIPTION("VMware Memory Control (Balloon) Driver");
453dc6593   Dmitry Torokhov   VMware Balloon dr...
41
42
43
  MODULE_ALIAS("dmi:*:svnVMware*:*");
  MODULE_ALIAS("vmware_vmmemctl");
  MODULE_LICENSE("GPL");
5d1a86ecf   Nadav Amit   vmw_balloon: Add ...
44
45
46
47
48
49
50
51
52
  static bool __read_mostly vmwballoon_shrinker_enable;
  module_param(vmwballoon_shrinker_enable, bool, 0444);
  MODULE_PARM_DESC(vmwballoon_shrinker_enable,
  	"Enable non-cooperative out-of-memory protection. Disabled by default as it may degrade performance.");
  
  /* Delay in seconds after shrink before inflation. */
  #define VMBALLOON_SHRINK_DELAY		(5)
  
  /* Maximum number of refused pages we accumulate during inflation cycle */
55adaa495   Dmitry Torokhov   vmware balloon: c...
53
  #define VMW_BALLOON_MAX_REFUSED		16
453dc6593   Dmitry Torokhov   VMware Balloon dr...
54

83a8afa72   Nadav Amit   vmw_balloon: Comp...
55
56
  /* Magic number for the balloon mount-point */
  #define BALLOON_VMW_MAGIC		0x0ba11007
453dc6593   Dmitry Torokhov   VMware Balloon dr...
57
58
59
60
61
  /*
   * Hypervisor communication port definitions.
   */
  #define VMW_BALLOON_HV_PORT		0x5670
  #define VMW_BALLOON_HV_MAGIC		0x456c6d6f
453dc6593   Dmitry Torokhov   VMware Balloon dr...
62
  #define VMW_BALLOON_GUEST_ID		1	/* Linux */
eb79100fe   Xavier Deguillard   VMware balloon: A...
63
64
65
66
  enum vmwballoon_capabilities {
  	/*
  	 * Bit 0 is reserved and not associated to any capability.
  	 */
48e3d668b   Philip P. Moltmann   VMware balloon: E...
67
68
69
70
  	VMW_BALLOON_BASIC_CMDS			= (1 << 1),
  	VMW_BALLOON_BATCHED_CMDS		= (1 << 2),
  	VMW_BALLOON_BATCHED_2M_CMDS		= (1 << 3),
  	VMW_BALLOON_SIGNALLED_WAKEUP_CMD	= (1 << 4),
553983027   Xavier Deguillard   vmw_balloon: supp...
71
  	VMW_BALLOON_64_BIT_TARGET		= (1 << 5)
eb79100fe   Xavier Deguillard   VMware balloon: A...
72
  };
553983027   Xavier Deguillard   vmw_balloon: supp...
73
  #define VMW_BALLOON_CAPABILITIES_COMMON	(VMW_BALLOON_BASIC_CMDS \
365bd7ef7   Philip P. Moltmann   VMware balloon: S...
74
  					| VMW_BALLOON_BATCHED_CMDS \
48e3d668b   Philip P. Moltmann   VMware balloon: E...
75
76
  					| VMW_BALLOON_BATCHED_2M_CMDS \
  					| VMW_BALLOON_SIGNALLED_WAKEUP_CMD)
365bd7ef7   Philip P. Moltmann   VMware balloon: S...
77

25acbdd7e   Nadav Amit   vmw_balloon: rena...
78
  #define VMW_BALLOON_2M_ORDER		(PMD_SHIFT - PAGE_SHIFT)
eb79100fe   Xavier Deguillard   VMware balloon: A...
79

553983027   Xavier Deguillard   vmw_balloon: supp...
80
81
82
83
84
85
86
87
88
  /*
   * 64-bit targets are only supported in 64-bit
   */
  #ifdef CONFIG_64BIT
  #define VMW_BALLOON_CAPABILITIES	(VMW_BALLOON_CAPABILITIES_COMMON \
  					| VMW_BALLOON_64_BIT_TARGET)
  #else
  #define VMW_BALLOON_CAPABILITIES	VMW_BALLOON_CAPABILITIES_COMMON
  #endif
c7b3690fb   Nadav Amit   vmw_balloon: stat...
89
90
91
92
93
94
95
  enum vmballoon_page_size_type {
  	VMW_BALLOON_4K_PAGE,
  	VMW_BALLOON_2M_PAGE,
  	VMW_BALLOON_LAST_SIZE = VMW_BALLOON_2M_PAGE
  };
  
  #define VMW_BALLOON_NUM_PAGE_SIZES	(VMW_BALLOON_LAST_SIZE + 1)
6e4453b32   Nadav Amit   vmw_balloon: rewo...
96
97
98
99
100
101
102
103
104
  static const char * const vmballoon_page_size_names[] = {
  	[VMW_BALLOON_4K_PAGE]			= "4k",
  	[VMW_BALLOON_2M_PAGE]			= "2M"
  };
  
  enum vmballoon_op {
  	VMW_BALLOON_INFLATE,
  	VMW_BALLOON_DEFLATE
  };
c7b3690fb   Nadav Amit   vmw_balloon: stat...
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  enum vmballoon_op_stat_type {
  	VMW_BALLOON_OP_STAT,
  	VMW_BALLOON_OP_FAIL_STAT
  };
  
  #define VMW_BALLOON_OP_STAT_TYPES	(VMW_BALLOON_OP_FAIL_STAT + 1)
  
  /**
   * enum vmballoon_cmd_type - backdoor commands.
   *
   * Availability of the commands is as followed:
   *
   * %VMW_BALLOON_CMD_START, %VMW_BALLOON_CMD_GET_TARGET and
   * %VMW_BALLOON_CMD_GUEST_ID are always available.
   *
   * If the host reports %VMW_BALLOON_BASIC_CMDS are supported then
   * %VMW_BALLOON_CMD_LOCK and %VMW_BALLOON_CMD_UNLOCK commands are available.
f220a80f0   Xavier Deguillard   VMware balloon: a...
122
   *
c7b3690fb   Nadav Amit   vmw_balloon: stat...
123
124
125
   * If the host reports %VMW_BALLOON_BATCHED_CMDS are supported then
   * %VMW_BALLOON_CMD_BATCHED_LOCK and VMW_BALLOON_CMD_BATCHED_UNLOCK commands
   * are available.
f220a80f0   Xavier Deguillard   VMware balloon: a...
126
   *
c7b3690fb   Nadav Amit   vmw_balloon: stat...
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
   * If the host reports %VMW_BALLOON_BATCHED_2M_CMDS are supported then
   * %VMW_BALLOON_CMD_BATCHED_2M_LOCK and %VMW_BALLOON_CMD_BATCHED_2M_UNLOCK
   * are supported.
   *
   * If the host reports  VMW_BALLOON_SIGNALLED_WAKEUP_CMD is supported then
   * VMW_BALLOON_CMD_VMCI_DOORBELL_SET command is supported.
   *
   * @VMW_BALLOON_CMD_START: Communicating supported version with the hypervisor.
   * @VMW_BALLOON_CMD_GET_TARGET: Gets the balloon target size.
   * @VMW_BALLOON_CMD_LOCK: Informs the hypervisor about a ballooned page.
   * @VMW_BALLOON_CMD_UNLOCK: Informs the hypervisor about a page that is about
   *			    to be deflated from the balloon.
   * @VMW_BALLOON_CMD_GUEST_ID: Informs the hypervisor about the type of OS that
   *			      runs in the VM.
   * @VMW_BALLOON_CMD_BATCHED_LOCK: Inform the hypervisor about a batch of
   *				  ballooned pages (up to 512).
   * @VMW_BALLOON_CMD_BATCHED_UNLOCK: Inform the hypervisor about a batch of
   *				  pages that are about to be deflated from the
   *				  balloon (up to 512).
   * @VMW_BALLOON_CMD_BATCHED_2M_LOCK: Similar to @VMW_BALLOON_CMD_BATCHED_LOCK
   *				     for 2MB pages.
   * @VMW_BALLOON_CMD_BATCHED_2M_UNLOCK: Similar to
   *				       @VMW_BALLOON_CMD_BATCHED_UNLOCK for 2MB
   *				       pages.
   * @VMW_BALLOON_CMD_VMCI_DOORBELL_SET: A command to set doorbell notification
   *				       that would be invoked when the balloon
   *				       size changes.
   * @VMW_BALLOON_CMD_LAST: Value of the last command.
f220a80f0   Xavier Deguillard   VMware balloon: a...
155
   */
c7b3690fb   Nadav Amit   vmw_balloon: stat...
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
  enum vmballoon_cmd_type {
  	VMW_BALLOON_CMD_START,
  	VMW_BALLOON_CMD_GET_TARGET,
  	VMW_BALLOON_CMD_LOCK,
  	VMW_BALLOON_CMD_UNLOCK,
  	VMW_BALLOON_CMD_GUEST_ID,
  	/* No command 5 */
  	VMW_BALLOON_CMD_BATCHED_LOCK = 6,
  	VMW_BALLOON_CMD_BATCHED_UNLOCK,
  	VMW_BALLOON_CMD_BATCHED_2M_LOCK,
  	VMW_BALLOON_CMD_BATCHED_2M_UNLOCK,
  	VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
  	VMW_BALLOON_CMD_LAST = VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
  };
  
  #define VMW_BALLOON_CMD_NUM	(VMW_BALLOON_CMD_LAST + 1)
  
  enum vmballoon_error_codes {
  	VMW_BALLOON_SUCCESS,
  	VMW_BALLOON_ERROR_CMD_INVALID,
  	VMW_BALLOON_ERROR_PPN_INVALID,
  	VMW_BALLOON_ERROR_PPN_LOCKED,
  	VMW_BALLOON_ERROR_PPN_UNLOCKED,
  	VMW_BALLOON_ERROR_PPN_PINNED,
  	VMW_BALLOON_ERROR_PPN_NOTNEEDED,
  	VMW_BALLOON_ERROR_RESET,
  	VMW_BALLOON_ERROR_BUSY
  };
eb79100fe   Xavier Deguillard   VMware balloon: A...
184
185
  
  #define VMW_BALLOON_SUCCESS_WITH_CAPABILITIES	(0x03000000)
10a95d5d8   Nadav Amit   vmw_balloon: hand...
186
187
188
189
190
191
192
193
  #define VMW_BALLOON_CMD_WITH_TARGET_MASK			\
  	((1UL << VMW_BALLOON_CMD_GET_TARGET)		|	\
  	 (1UL << VMW_BALLOON_CMD_LOCK)			|	\
  	 (1UL << VMW_BALLOON_CMD_UNLOCK)		|	\
  	 (1UL << VMW_BALLOON_CMD_BATCHED_LOCK)		|	\
  	 (1UL << VMW_BALLOON_CMD_BATCHED_UNLOCK)	|	\
  	 (1UL << VMW_BALLOON_CMD_BATCHED_2M_LOCK)	|	\
  	 (1UL << VMW_BALLOON_CMD_BATCHED_2M_UNLOCK))
681311848   Nadav Amit   vmw_balloon: unif...
194
195
196
197
198
199
200
201
202
203
204
205
  static const char * const vmballoon_cmd_names[] = {
  	[VMW_BALLOON_CMD_START]			= "start",
  	[VMW_BALLOON_CMD_GET_TARGET]		= "target",
  	[VMW_BALLOON_CMD_LOCK]			= "lock",
  	[VMW_BALLOON_CMD_UNLOCK]		= "unlock",
  	[VMW_BALLOON_CMD_GUEST_ID]		= "guestType",
  	[VMW_BALLOON_CMD_BATCHED_LOCK]		= "batchLock",
  	[VMW_BALLOON_CMD_BATCHED_UNLOCK]	= "batchUnlock",
  	[VMW_BALLOON_CMD_BATCHED_2M_LOCK]	= "2m-lock",
  	[VMW_BALLOON_CMD_BATCHED_2M_UNLOCK]	= "2m-unlock",
  	[VMW_BALLOON_CMD_VMCI_DOORBELL_SET]	= "doorbellSet"
  };
c7b3690fb   Nadav Amit   vmw_balloon: stat...
206
207
208
209
210
211
212
  enum vmballoon_stat_page {
  	VMW_BALLOON_PAGE_STAT_ALLOC,
  	VMW_BALLOON_PAGE_STAT_ALLOC_FAIL,
  	VMW_BALLOON_PAGE_STAT_REFUSED_ALLOC,
  	VMW_BALLOON_PAGE_STAT_REFUSED_FREE,
  	VMW_BALLOON_PAGE_STAT_FREE,
  	VMW_BALLOON_PAGE_STAT_LAST = VMW_BALLOON_PAGE_STAT_FREE
453dc6593   Dmitry Torokhov   VMware Balloon dr...
213
  };
c7b3690fb   Nadav Amit   vmw_balloon: stat...
214
215
216
217
218
  #define VMW_BALLOON_PAGE_STAT_NUM	(VMW_BALLOON_PAGE_STAT_LAST + 1)
  
  enum vmballoon_stat_general {
  	VMW_BALLOON_STAT_TIMER,
  	VMW_BALLOON_STAT_DOORBELL,
8840a6f4a   Nadav Amit   vmw_balloon: add ...
219
  	VMW_BALLOON_STAT_RESET,
5d1a86ecf   Nadav Amit   vmw_balloon: Add ...
220
221
222
  	VMW_BALLOON_STAT_SHRINK,
  	VMW_BALLOON_STAT_SHRINK_FREE,
  	VMW_BALLOON_STAT_LAST = VMW_BALLOON_STAT_SHRINK_FREE
c7b3690fb   Nadav Amit   vmw_balloon: stat...
223
224
225
  };
  
  #define VMW_BALLOON_STAT_NUM		(VMW_BALLOON_STAT_LAST + 1)
df8d0d42a   Nadav Amit   vmw_balloon: chan...
226
  static DEFINE_STATIC_KEY_TRUE(vmw_balloon_batching);
c7b3690fb   Nadav Amit   vmw_balloon: stat...
227
  static DEFINE_STATIC_KEY_FALSE(balloon_stat_enabled);
f220a80f0   Xavier Deguillard   VMware balloon: a...
228

6e4453b32   Nadav Amit   vmw_balloon: rewo...
229
  struct vmballoon_ctl {
453dc6593   Dmitry Torokhov   VMware Balloon dr...
230
  	struct list_head pages;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
231
  	struct list_head refused_pages;
ae29783d3   Nadav Amit   vmw_balloon: Spli...
232
  	struct list_head prealloc_pages;
55adaa495   Dmitry Torokhov   vmware balloon: c...
233
  	unsigned int n_refused_pages;
6e4453b32   Nadav Amit   vmw_balloon: rewo...
234
235
236
237
  	unsigned int n_pages;
  	enum vmballoon_page_size_type page_size;
  	enum vmballoon_op op;
  };
6c9487579   Nadav Amit   vmw_balloon: simp...
238
239
240
241
242
243
244
245
246
247
248
249
  /**
   * struct vmballoon_batch_entry - a batch entry for lock or unlock.
   *
   * @status: the status of the operation, which is written by the hypervisor.
   * @reserved: reserved for future use. Must be set to zero.
   * @pfn: the physical frame number of the page to be locked or unlocked.
   */
  struct vmballoon_batch_entry {
  	u64 status : 5;
  	u64 reserved : PAGE_SHIFT - 5;
  	u64 pfn : 52;
  } __packed;
365bd7ef7   Philip P. Moltmann   VMware balloon: S...
250
  struct vmballoon {
6e4453b32   Nadav Amit   vmw_balloon: rewo...
251
252
253
254
255
256
257
258
259
260
261
262
263
264
  	/**
  	 * @max_page_size: maximum supported page size for ballooning.
  	 *
  	 * Protected by @conf_sem
  	 */
  	enum vmballoon_page_size_type max_page_size;
  
  	/**
  	 * @size: balloon actual size in basic page size (frames).
  	 *
  	 * While we currently do not support size which is bigger than 32-bit,
  	 * in preparation for future support, use 64-bits.
  	 */
  	atomic64_t size;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
265

6e4453b32   Nadav Amit   vmw_balloon: rewo...
266
267
268
269
270
271
272
273
274
275
  	/**
  	 * @target: balloon target size in basic page size (frames).
  	 *
  	 * We do not protect the target under the assumption that setting the
  	 * value is always done through a single write. If this assumption ever
  	 * breaks, we would have to use X_ONCE for accesses, and suffer the less
  	 * optimized code. Although we may read stale target value if multiple
  	 * accesses happen at once, the performance impact should be minor.
  	 */
  	unsigned long target;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
276

6e4453b32   Nadav Amit   vmw_balloon: rewo...
277
278
279
280
281
282
283
284
  	/**
  	 * @reset_required: reset flag
  	 *
  	 * Setting this flag may introduce races, but the code is expected to
  	 * handle them gracefully. In the worst case, another operation will
  	 * fail as reset did not take place. Clearing the flag is done while
  	 * holding @conf_sem for write.
  	 */
453dc6593   Dmitry Torokhov   VMware Balloon dr...
285
  	bool reset_required;
6e4453b32   Nadav Amit   vmw_balloon: rewo...
286
287
288
289
290
  	/**
  	 * @capabilities: hypervisor balloon capabilities.
  	 *
  	 * Protected by @conf_sem.
  	 */
f220a80f0   Xavier Deguillard   VMware balloon: a...
291
  	unsigned long capabilities;
6c9487579   Nadav Amit   vmw_balloon: simp...
292
293
294
295
296
297
298
  	/**
  	 * @batch_page: pointer to communication batch page.
  	 *
  	 * When batching is used, batch_page points to a page, which holds up to
  	 * %VMW_BALLOON_BATCH_MAX_PAGES entries for locking or unlocking.
  	 */
  	struct vmballoon_batch_entry *batch_page;
6e4453b32   Nadav Amit   vmw_balloon: rewo...
299
300
301
302
303
304
305
306
307
  	/**
  	 * @batch_max_pages: maximum pages that can be locked/unlocked.
  	 *
  	 * Indicates the number of pages that the hypervisor can lock or unlock
  	 * at once, according to whether batching is enabled. If batching is
  	 * disabled, only a single page can be locked/unlock on each operation.
  	 *
  	 * Protected by @conf_sem.
  	 */
f220a80f0   Xavier Deguillard   VMware balloon: a...
308
  	unsigned int batch_max_pages;
6e4453b32   Nadav Amit   vmw_balloon: rewo...
309
310
311
312
313
314
315
316
317
  
  	/**
  	 * @page: page to be locked/unlocked by the hypervisor
  	 *
  	 * @page is only used when batching is disabled and a single page is
  	 * reclaimed on each iteration.
  	 *
  	 * Protected by @comm_lock.
  	 */
f220a80f0   Xavier Deguillard   VMware balloon: a...
318
  	struct page *page;
5d1a86ecf   Nadav Amit   vmw_balloon: Add ...
319
320
321
322
323
324
325
326
  	/**
  	 * @shrink_timeout: timeout until the next inflation.
  	 *
  	 * After an shrink event, indicates the time in jiffies after which
  	 * inflation is allowed again. Can be written concurrently with reads,
  	 * so must use READ_ONCE/WRITE_ONCE when accessing.
  	 */
  	unsigned long shrink_timeout;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
327
  	/* statistics */
c7b3690fb   Nadav Amit   vmw_balloon: stat...
328
  	struct vmballoon_stats *stats;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
329

c7b3690fb   Nadav Amit   vmw_balloon: stat...
330
  #ifdef CONFIG_DEBUG_FS
453dc6593   Dmitry Torokhov   VMware Balloon dr...
331
332
333
  	/* debugfs file exporting statistics */
  	struct dentry *dbg_entry;
  #endif
83a8afa72   Nadav Amit   vmw_balloon: Comp...
334
335
336
337
  	/**
  	 * @b_dev_info: balloon device information descriptor.
  	 */
  	struct balloon_dev_info b_dev_info;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
338
  	struct delayed_work dwork;
48e3d668b   Philip P. Moltmann   VMware balloon: E...
339

6e4453b32   Nadav Amit   vmw_balloon: rewo...
340
  	/**
83a8afa72   Nadav Amit   vmw_balloon: Comp...
341
342
343
344
345
346
347
  	 * @huge_pages - list of the inflated 2MB pages.
  	 *
  	 * Protected by @b_dev_info.pages_lock .
  	 */
  	struct list_head huge_pages;
  
  	/**
6e4453b32   Nadav Amit   vmw_balloon: rewo...
348
349
350
351
  	 * @vmci_doorbell.
  	 *
  	 * Protected by @conf_sem.
  	 */
48e3d668b   Philip P. Moltmann   VMware balloon: E...
352
  	struct vmci_handle vmci_doorbell;
c7b3690fb   Nadav Amit   vmw_balloon: stat...
353
354
355
356
357
  
  	/**
  	 * @conf_sem: semaphore to protect the configuration and the statistics.
  	 */
  	struct rw_semaphore conf_sem;
6e4453b32   Nadav Amit   vmw_balloon: rewo...
358
359
360
361
362
363
364
  
  	/**
  	 * @comm_lock: lock to protect the communication with the host.
  	 *
  	 * Lock ordering: @conf_sem -> @comm_lock .
  	 */
  	spinlock_t comm_lock;
5d1a86ecf   Nadav Amit   vmw_balloon: Add ...
365
366
367
368
369
370
371
372
373
374
375
376
377
378
  
  	/**
  	 * @shrinker: shrinker interface that is used to avoid over-inflation.
  	 */
  	struct shrinker shrinker;
  
  	/**
  	 * @shrinker_registered: whether the shrinker was registered.
  	 *
  	 * The shrinker interface does not handle gracefully the removal of
  	 * shrinker that was not registered before. This indication allows to
  	 * simplify the unregistration process.
  	 */
  	bool shrinker_registered;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
379
380
381
  };
  
  static struct vmballoon balloon;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
382

c7b3690fb   Nadav Amit   vmw_balloon: stat...
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
  struct vmballoon_stats {
  	/* timer / doorbell operations */
  	atomic64_t general_stat[VMW_BALLOON_STAT_NUM];
  
  	/* allocation statistics for huge and small pages */
  	atomic64_t
  	       page_stat[VMW_BALLOON_PAGE_STAT_NUM][VMW_BALLOON_NUM_PAGE_SIZES];
  
  	/* Monitor operations: total operations, and failures */
  	atomic64_t ops[VMW_BALLOON_CMD_NUM][VMW_BALLOON_OP_STAT_TYPES];
  };
  
  static inline bool is_vmballoon_stats_on(void)
  {
  	return IS_ENABLED(CONFIG_DEBUG_FS) &&
  		static_branch_unlikely(&balloon_stat_enabled);
  }
  
  static inline void vmballoon_stats_op_inc(struct vmballoon *b, unsigned int op,
  					  enum vmballoon_op_stat_type type)
  {
  	if (is_vmballoon_stats_on())
  		atomic64_inc(&b->stats->ops[op][type]);
  }
  
  static inline void vmballoon_stats_gen_inc(struct vmballoon *b,
  					   enum vmballoon_stat_general stat)
  {
  	if (is_vmballoon_stats_on())
  		atomic64_inc(&b->stats->general_stat[stat]);
  }
  
  static inline void vmballoon_stats_gen_add(struct vmballoon *b,
  					   enum vmballoon_stat_general stat,
  					   unsigned int val)
  {
  	if (is_vmballoon_stats_on())
  		atomic64_add(val, &b->stats->general_stat[stat]);
  }
  
  static inline void vmballoon_stats_page_inc(struct vmballoon *b,
  					    enum vmballoon_stat_page stat,
6e4453b32   Nadav Amit   vmw_balloon: rewo...
425
  					    enum vmballoon_page_size_type size)
c7b3690fb   Nadav Amit   vmw_balloon: stat...
426
427
  {
  	if (is_vmballoon_stats_on())
6e4453b32   Nadav Amit   vmw_balloon: rewo...
428
429
430
431
432
433
434
435
436
437
  		atomic64_inc(&b->stats->page_stat[stat][size]);
  }
  
  static inline void vmballoon_stats_page_add(struct vmballoon *b,
  					    enum vmballoon_stat_page stat,
  					    enum vmballoon_page_size_type size,
  					    unsigned int val)
  {
  	if (is_vmballoon_stats_on())
  		atomic64_add(val, &b->stats->page_stat[stat][size]);
c7b3690fb   Nadav Amit   vmw_balloon: stat...
438
  }
10a95d5d8   Nadav Amit   vmw_balloon: hand...
439
440
441
442
443
  static inline unsigned long
  __vmballoon_cmd(struct vmballoon *b, unsigned long cmd, unsigned long arg1,
  		unsigned long arg2, unsigned long *result)
  {
  	unsigned long status, dummy1, dummy2, dummy3, local_result;
c7b3690fb   Nadav Amit   vmw_balloon: stat...
444
  	vmballoon_stats_op_inc(b, cmd, VMW_BALLOON_OP_STAT);
681311848   Nadav Amit   vmw_balloon: unif...
445

10a95d5d8   Nadav Amit   vmw_balloon: hand...
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
  	asm volatile ("inl %%dx" :
  		"=a"(status),
  		"=c"(dummy1),
  		"=d"(dummy2),
  		"=b"(local_result),
  		"=S"(dummy3) :
  		"0"(VMW_BALLOON_HV_MAGIC),
  		"1"(cmd),
  		"2"(VMW_BALLOON_HV_PORT),
  		"3"(arg1),
  		"4"(arg2) :
  		"memory");
  
  	/* update the result if needed */
  	if (result)
  		*result = (cmd == VMW_BALLOON_CMD_START) ? dummy1 :
  							   local_result;
  
  	/* update target when applicable */
  	if (status == VMW_BALLOON_SUCCESS &&
  	    ((1ul << cmd) & VMW_BALLOON_CMD_WITH_TARGET_MASK))
6e4453b32   Nadav Amit   vmw_balloon: rewo...
467
  		WRITE_ONCE(b->target, local_result);
10a95d5d8   Nadav Amit   vmw_balloon: hand...
468

681311848   Nadav Amit   vmw_balloon: unif...
469
470
  	if (status != VMW_BALLOON_SUCCESS &&
  	    status != VMW_BALLOON_SUCCESS_WITH_CAPABILITIES) {
c7b3690fb   Nadav Amit   vmw_balloon: stat...
471
  		vmballoon_stats_op_inc(b, cmd, VMW_BALLOON_OP_FAIL_STAT);
681311848   Nadav Amit   vmw_balloon: unif...
472
473
474
475
476
  		pr_debug("%s: %s [0x%lx,0x%lx) failed, returned %ld
  ",
  			 __func__, vmballoon_cmd_names[cmd], arg1, arg2,
  			 status);
  	}
10a95d5d8   Nadav Amit   vmw_balloon: hand...
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
  	/* mark reset required accordingly */
  	if (status == VMW_BALLOON_ERROR_RESET)
  		b->reset_required = true;
  
  	return status;
  }
  
  static __always_inline unsigned long
  vmballoon_cmd(struct vmballoon *b, unsigned long cmd, unsigned long arg1,
  	      unsigned long arg2)
  {
  	unsigned long dummy;
  
  	return __vmballoon_cmd(b, cmd, arg1, arg2, &dummy);
  }
453dc6593   Dmitry Torokhov   VMware Balloon dr...
492
493
494
495
  /*
   * Send "start" command to the host, communicating supported version
   * of the protocol.
   */
22d293ee8   Nadav Amit   vmw_balloon: gene...
496
  static int vmballoon_send_start(struct vmballoon *b, unsigned long req_caps)
453dc6593   Dmitry Torokhov   VMware Balloon dr...
497
  {
10a95d5d8   Nadav Amit   vmw_balloon: hand...
498
  	unsigned long status, capabilities;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
499

10a95d5d8   Nadav Amit   vmw_balloon: hand...
500
501
  	status = __vmballoon_cmd(b, VMW_BALLOON_CMD_START, req_caps, 0,
  				 &capabilities);
f220a80f0   Xavier Deguillard   VMware balloon: a...
502
503
504
505
  
  	switch (status) {
  	case VMW_BALLOON_SUCCESS_WITH_CAPABILITIES:
  		b->capabilities = capabilities;
365bd7ef7   Philip P. Moltmann   VMware balloon: S...
506
  		break;
f220a80f0   Xavier Deguillard   VMware balloon: a...
507
508
  	case VMW_BALLOON_SUCCESS:
  		b->capabilities = VMW_BALLOON_BASIC_CMDS;
365bd7ef7   Philip P. Moltmann   VMware balloon: S...
509
510
  		break;
  	default:
22d293ee8   Nadav Amit   vmw_balloon: gene...
511
  		return -EIO;
f220a80f0   Xavier Deguillard   VMware balloon: a...
512
  	}
453dc6593   Dmitry Torokhov   VMware Balloon dr...
513

5081efd11   Nadav Amit   vmw_balloon: do n...
514
515
516
517
518
  	/*
  	 * 2MB pages are only supported with batching. If batching is for some
  	 * reason disabled, do not use 2MB pages, since otherwise the legacy
  	 * mechanism is used with 2MB pages, causing a failure.
  	 */
6e4453b32   Nadav Amit   vmw_balloon: rewo...
519
  	b->max_page_size = VMW_BALLOON_4K_PAGE;
5081efd11   Nadav Amit   vmw_balloon: do n...
520
521
  	if ((b->capabilities & VMW_BALLOON_BATCHED_2M_CMDS) &&
  	    (b->capabilities & VMW_BALLOON_BATCHED_CMDS))
6e4453b32   Nadav Amit   vmw_balloon: rewo...
522
  		b->max_page_size = VMW_BALLOON_2M_PAGE;
365bd7ef7   Philip P. Moltmann   VMware balloon: S...
523

22d293ee8   Nadav Amit   vmw_balloon: gene...
524
  	return 0;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
525
  }
22d293ee8   Nadav Amit   vmw_balloon: gene...
526
527
528
529
530
  /**
   * vmballoon_send_guest_id - communicate guest type to the host.
   *
   * @b: pointer to the balloon.
   *
453dc6593   Dmitry Torokhov   VMware Balloon dr...
531
532
533
534
   * Communicate guest type to the host so that it can adjust ballooning
   * algorithm to the one most appropriate for the guest. This command
   * is normally issued after sending "start" command and is part of
   * standard reset sequence.
22d293ee8   Nadav Amit   vmw_balloon: gene...
535
536
   *
   * Return: zero on success or appropriate error code.
453dc6593   Dmitry Torokhov   VMware Balloon dr...
537
   */
22d293ee8   Nadav Amit   vmw_balloon: gene...
538
  static int vmballoon_send_guest_id(struct vmballoon *b)
453dc6593   Dmitry Torokhov   VMware Balloon dr...
539
  {
10a95d5d8   Nadav Amit   vmw_balloon: hand...
540
  	unsigned long status;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
541

10a95d5d8   Nadav Amit   vmw_balloon: hand...
542
543
  	status = vmballoon_cmd(b, VMW_BALLOON_CMD_GUEST_ID,
  			       VMW_BALLOON_GUEST_ID, 0);
453dc6593   Dmitry Torokhov   VMware Balloon dr...
544

22d293ee8   Nadav Amit   vmw_balloon: gene...
545
  	return status == VMW_BALLOON_SUCCESS ? 0 : -EIO;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
546
  }
6e4453b32   Nadav Amit   vmw_balloon: rewo...
547
548
549
550
551
552
553
554
  /**
   * vmballoon_page_order() - return the order of the page
   * @page_size: the size of the page.
   *
   * Return: the allocation order.
   */
  static inline
  unsigned int vmballoon_page_order(enum vmballoon_page_size_type page_size)
365bd7ef7   Philip P. Moltmann   VMware balloon: S...
555
  {
6e4453b32   Nadav Amit   vmw_balloon: rewo...
556
557
  	return page_size == VMW_BALLOON_2M_PAGE ? VMW_BALLOON_2M_ORDER : 0;
  }
365bd7ef7   Philip P. Moltmann   VMware balloon: S...
558

6e4453b32   Nadav Amit   vmw_balloon: rewo...
559
560
561
562
563
564
565
566
567
568
  /**
   * vmballoon_page_in_frames() - returns the number of frames in a page.
   * @page_size: the size of the page.
   *
   * Return: the number of 4k frames.
   */
  static inline unsigned int
  vmballoon_page_in_frames(enum vmballoon_page_size_type page_size)
  {
  	return 1 << vmballoon_page_order(page_size);
365bd7ef7   Philip P. Moltmann   VMware balloon: S...
569
  }
0395be3ec   Nadav Amit   vmw_balloon: simp...
570
  /**
8165540c7   David Hildenbrand   vmw_balloon: mark...
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
   * vmballoon_mark_page_offline() - mark a page as offline
   * @page: pointer for the page.
   * @page_size: the size of the page.
   */
  static void
  vmballoon_mark_page_offline(struct page *page,
  			    enum vmballoon_page_size_type page_size)
  {
  	int i;
  
  	for (i = 0; i < vmballoon_page_in_frames(page_size); i++)
  		__SetPageOffline(page + i);
  }
  
  /**
   * vmballoon_mark_page_online() - mark a page as online
   * @page: pointer for the page.
   * @page_size: the size of the page.
   */
  static void
  vmballoon_mark_page_online(struct page *page,
  			   enum vmballoon_page_size_type page_size)
  {
  	int i;
  
  	for (i = 0; i < vmballoon_page_in_frames(page_size); i++)
  		__ClearPageOffline(page + i);
  }
  
  /**
0395be3ec   Nadav Amit   vmw_balloon: simp...
601
602
603
604
605
606
607
   * vmballoon_send_get_target() - Retrieve desired balloon size from the host.
   *
   * @b: pointer to the balloon.
   *
   * Return: zero on success, EINVAL if limit does not fit in 32-bit, as required
   * by the host-guest protocol and EIO if an error occurred in communicating with
   * the host.
453dc6593   Dmitry Torokhov   VMware Balloon dr...
608
   */
0395be3ec   Nadav Amit   vmw_balloon: simp...
609
  static int vmballoon_send_get_target(struct vmballoon *b)
453dc6593   Dmitry Torokhov   VMware Balloon dr...
610
611
  {
  	unsigned long status;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
612
  	unsigned long limit;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
613

ca79b0c21   Arun KS   mm: convert total...
614
  	limit = totalram_pages();
453dc6593   Dmitry Torokhov   VMware Balloon dr...
615

553983027   Xavier Deguillard   vmw_balloon: supp...
616
617
618
  	/* Ensure limit fits in 32-bits if 64-bit targets are not supported */
  	if (!(b->capabilities & VMW_BALLOON_64_BIT_TARGET) &&
  	    limit != (u32)limit)
0395be3ec   Nadav Amit   vmw_balloon: simp...
619
  		return -EINVAL;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
620

10a95d5d8   Nadav Amit   vmw_balloon: hand...
621
  	status = vmballoon_cmd(b, VMW_BALLOON_CMD_GET_TARGET, limit, 0);
0395be3ec   Nadav Amit   vmw_balloon: simp...
622
  	return status == VMW_BALLOON_SUCCESS ? 0 : -EIO;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
623
  }
6e4453b32   Nadav Amit   vmw_balloon: rewo...
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
  /**
   * vmballoon_alloc_page_list - allocates a list of pages.
   *
   * @b: pointer to the balloon.
   * @ctl: pointer for the %struct vmballoon_ctl, which defines the operation.
   * @req_n_pages: the number of requested pages.
   *
   * Tries to allocate @req_n_pages. Add them to the list of balloon pages in
   * @ctl.pages and updates @ctl.n_pages to reflect the number of pages.
   *
   * Return: zero on success or error code otherwise.
   */
  static int vmballoon_alloc_page_list(struct vmballoon *b,
  				     struct vmballoon_ctl *ctl,
  				     unsigned int req_n_pages)
365bd7ef7   Philip P. Moltmann   VMware balloon: S...
639
  {
6e4453b32   Nadav Amit   vmw_balloon: rewo...
640
641
  	struct page *page;
  	unsigned int i;
365bd7ef7   Philip P. Moltmann   VMware balloon: S...
642

6e4453b32   Nadav Amit   vmw_balloon: rewo...
643
  	for (i = 0; i < req_n_pages; i++) {
ae29783d3   Nadav Amit   vmw_balloon: Spli...
644
645
646
647
648
649
650
651
652
653
654
655
  		/*
  		 * First check if we happen to have pages that were allocated
  		 * before. This happens when 2MB page rejected during inflation
  		 * by the hypervisor, and then split into 4KB pages.
  		 */
  		if (!list_empty(&ctl->prealloc_pages)) {
  			page = list_first_entry(&ctl->prealloc_pages,
  						struct page, lru);
  			list_del(&page->lru);
  		} else {
  			if (ctl->page_size == VMW_BALLOON_2M_PAGE)
  				page = alloc_pages(__GFP_HIGHMEM|__GFP_NOWARN|
83a8afa72   Nadav Amit   vmw_balloon: Comp...
656
  					__GFP_NOMEMALLOC, VMW_BALLOON_2M_ORDER);
ae29783d3   Nadav Amit   vmw_balloon: Spli...
657
658
  			else
  				page = balloon_page_alloc();
365bd7ef7   Philip P. Moltmann   VMware balloon: S...
659

ae29783d3   Nadav Amit   vmw_balloon: Spli...
660
661
662
  			vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_ALLOC,
  						 ctl->page_size);
  		}
6e4453b32   Nadav Amit   vmw_balloon: rewo...
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
  
  		if (page) {
  			/* Success. Add the page to the list and continue. */
  			list_add(&page->lru, &ctl->pages);
  			continue;
  		}
  
  		/* Allocation failed. Update statistics and stop. */
  		vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_ALLOC_FAIL,
  					 ctl->page_size);
  		break;
  	}
  
  	ctl->n_pages = i;
  
  	return req_n_pages == ctl->n_pages ? 0 : -ENOMEM;
365bd7ef7   Philip P. Moltmann   VMware balloon: S...
679
  }
6e4453b32   Nadav Amit   vmw_balloon: rewo...
680
681
682
683
684
685
686
  /**
   * vmballoon_handle_one_result - Handle lock/unlock result for a single page.
   *
   * @b: pointer for %struct vmballoon.
   * @page: pointer for the page whose result should be handled.
   * @page_size: size of the page.
   * @status: status of the operation as provided by the hypervisor.
453dc6593   Dmitry Torokhov   VMware Balloon dr...
687
   */
6e4453b32   Nadav Amit   vmw_balloon: rewo...
688
689
690
  static int vmballoon_handle_one_result(struct vmballoon *b, struct page *page,
  				       enum vmballoon_page_size_type page_size,
  				       unsigned long status)
453dc6593   Dmitry Torokhov   VMware Balloon dr...
691
  {
6e4453b32   Nadav Amit   vmw_balloon: rewo...
692
693
694
  	/* On success do nothing. The page is already on the balloon list. */
  	if (likely(status == VMW_BALLOON_SUCCESS))
  		return 0;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
695

6e4453b32   Nadav Amit   vmw_balloon: rewo...
696
697
698
699
700
701
702
703
704
705
  	pr_debug("%s: failed comm pfn %lx status %lu page_size %s
  ", __func__,
  		 page_to_pfn(page), status,
  		 vmballoon_page_size_names[page_size]);
  
  	/* Error occurred */
  	vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_REFUSED_ALLOC,
  				 page_size);
  
  	return -EIO;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
706
  }
df8d0d42a   Nadav Amit   vmw_balloon: chan...
707
708
709
710
711
712
713
714
715
716
717
718
  /**
   * vmballoon_status_page - returns the status of (un)lock operation
   *
   * @b: pointer to the balloon.
   * @idx: index for the page for which the operation is performed.
   * @p: pointer to where the page struct is returned.
   *
   * Following a lock or unlock operation, returns the status of the operation for
   * an individual page. Provides the page that the operation was performed on on
   * the @page argument.
   *
   * Returns: The status of a lock or unlock operation for an individual page.
453dc6593   Dmitry Torokhov   VMware Balloon dr...
719
   */
df8d0d42a   Nadav Amit   vmw_balloon: chan...
720
721
  static unsigned long vmballoon_status_page(struct vmballoon *b, int idx,
  					   struct page **p)
453dc6593   Dmitry Torokhov   VMware Balloon dr...
722
  {
df8d0d42a   Nadav Amit   vmw_balloon: chan...
723
724
725
726
727
  	if (static_branch_likely(&vmw_balloon_batching)) {
  		/* batching mode */
  		*p = pfn_to_page(b->batch_page[idx].pfn);
  		return b->batch_page[idx].status;
  	}
10a95d5d8   Nadav Amit   vmw_balloon: hand...
728

df8d0d42a   Nadav Amit   vmw_balloon: chan...
729
730
  	/* non-batching mode */
  	*p = b->page;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
731

df8d0d42a   Nadav Amit   vmw_balloon: chan...
732
733
734
735
736
737
738
739
  	/*
  	 * If a failure occurs, the indication will be provided in the status
  	 * of the entire operation, which is considered before the individual
  	 * page status. So for non-batching mode, the indication is always of
  	 * success.
  	 */
  	return VMW_BALLOON_SUCCESS;
  }
453dc6593   Dmitry Torokhov   VMware Balloon dr...
740

df8d0d42a   Nadav Amit   vmw_balloon: chan...
741
742
743
744
  /**
   * vmballoon_lock_op - notifies the host about inflated/deflated pages.
   * @b: pointer to the balloon.
   * @num_pages: number of inflated/deflated pages.
6e4453b32   Nadav Amit   vmw_balloon: rewo...
745
746
   * @page_size: size of the page.
   * @op: the type of operation (lock or unlock).
df8d0d42a   Nadav Amit   vmw_balloon: chan...
747
748
749
750
751
752
753
754
755
756
757
   *
   * Notify the host about page(s) that were ballooned (or removed from the
   * balloon) so that host can use it without fear that guest will need it (or
   * stop using them since the VM does). Host may reject some pages, we need to
   * check the return value and maybe submit a different page. The pages that are
   * inflated/deflated are pointed by @b->page.
   *
   * Return: result as provided by the hypervisor.
   */
  static unsigned long vmballoon_lock_op(struct vmballoon *b,
  				       unsigned int num_pages,
6e4453b32   Nadav Amit   vmw_balloon: rewo...
758
759
  				       enum vmballoon_page_size_type page_size,
  				       enum vmballoon_op op)
df8d0d42a   Nadav Amit   vmw_balloon: chan...
760
761
  {
  	unsigned long cmd, pfn;
6e4453b32   Nadav Amit   vmw_balloon: rewo...
762
  	lockdep_assert_held(&b->comm_lock);
df8d0d42a   Nadav Amit   vmw_balloon: chan...
763
  	if (static_branch_likely(&vmw_balloon_batching)) {
6e4453b32   Nadav Amit   vmw_balloon: rewo...
764
765
766
767
  		if (op == VMW_BALLOON_INFLATE)
  			cmd = page_size == VMW_BALLOON_2M_PAGE ?
  				VMW_BALLOON_CMD_BATCHED_2M_LOCK :
  				VMW_BALLOON_CMD_BATCHED_LOCK;
df8d0d42a   Nadav Amit   vmw_balloon: chan...
768
  		else
6e4453b32   Nadav Amit   vmw_balloon: rewo...
769
770
771
  			cmd = page_size == VMW_BALLOON_2M_PAGE ?
  				VMW_BALLOON_CMD_BATCHED_2M_UNLOCK :
  				VMW_BALLOON_CMD_BATCHED_UNLOCK;
df8d0d42a   Nadav Amit   vmw_balloon: chan...
772
773
774
  
  		pfn = PHYS_PFN(virt_to_phys(b->batch_page));
  	} else {
6e4453b32   Nadav Amit   vmw_balloon: rewo...
775
776
  		cmd = op == VMW_BALLOON_INFLATE ? VMW_BALLOON_CMD_LOCK :
  						  VMW_BALLOON_CMD_UNLOCK;
df8d0d42a   Nadav Amit   vmw_balloon: chan...
777
778
779
780
781
  		pfn = page_to_pfn(b->page);
  
  		/* In non-batching mode, PFNs must fit in 32-bit */
  		if (unlikely(pfn != (u32)pfn))
  			return VMW_BALLOON_ERROR_PPN_INVALID;
ef0f8f112   Xavier Deguillard   VMware balloon: p...
782
  	}
453dc6593   Dmitry Torokhov   VMware Balloon dr...
783

df8d0d42a   Nadav Amit   vmw_balloon: chan...
784
  	return vmballoon_cmd(b, cmd, pfn, num_pages);
453dc6593   Dmitry Torokhov   VMware Balloon dr...
785
  }
6e4453b32   Nadav Amit   vmw_balloon: rewo...
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
  /**
   * vmballoon_add_page - adds a page towards lock/unlock operation.
   *
   * @b: pointer to the balloon.
   * @idx: index of the page to be ballooned in this batch.
   * @p: pointer to the page that is about to be ballooned.
   *
   * Adds the page to be ballooned. Must be called while holding @comm_lock.
   */
  static void vmballoon_add_page(struct vmballoon *b, unsigned int idx,
  			       struct page *p)
  {
  	lockdep_assert_held(&b->comm_lock);
  
  	if (static_branch_likely(&vmw_balloon_batching))
  		b->batch_page[idx] = (struct vmballoon_batch_entry)
  					{ .pfn = page_to_pfn(p) };
  	else
  		b->page = p;
  }
  
  /**
   * vmballoon_lock - lock or unlock a batch of pages.
   *
   * @b: pointer to the balloon.
   * @ctl: pointer for the %struct vmballoon_ctl, which defines the operation.
   *
   * Notifies the host of about ballooned pages (after inflation or deflation,
   * according to @ctl). If the host rejects the page put it on the
   * @ctl refuse list. These refused page are then released when moving to the
   * next size of pages.
   *
   * Note that we neither free any @page here nor put them back on the ballooned
   * pages list. Instead we queue it for later processing. We do that for several
   * reasons. First, we do not want to free the page under the lock. Second, it
   * allows us to unify the handling of lock and unlock. In the inflate case, the
   * caller will check if there are too many refused pages and release them.
   * Although it is not identical to the past behavior, it should not affect
   * performance.
   */
  static int vmballoon_lock(struct vmballoon *b, struct vmballoon_ctl *ctl)
f220a80f0   Xavier Deguillard   VMware balloon: a...
827
  {
df8d0d42a   Nadav Amit   vmw_balloon: chan...
828
  	unsigned long batch_status;
6e4453b32   Nadav Amit   vmw_balloon: rewo...
829
830
831
832
833
834
  	struct page *page;
  	unsigned int i, num_pages;
  
  	num_pages = ctl->n_pages;
  	if (num_pages == 0)
  		return 0;
f220a80f0   Xavier Deguillard   VMware balloon: a...
835

6e4453b32   Nadav Amit   vmw_balloon: rewo...
836
837
  	/* communication with the host is done under the communication lock */
  	spin_lock(&b->comm_lock);
f220a80f0   Xavier Deguillard   VMware balloon: a...
838

6e4453b32   Nadav Amit   vmw_balloon: rewo...
839
840
841
842
843
844
845
846
847
848
849
850
  	i = 0;
  	list_for_each_entry(page, &ctl->pages, lru)
  		vmballoon_add_page(b, i++, page);
  
  	batch_status = vmballoon_lock_op(b, ctl->n_pages, ctl->page_size,
  					 ctl->op);
  
  	/*
  	 * Iterate over the pages in the provided list. Since we are changing
  	 * @ctl->n_pages we are saving the original value in @num_pages and
  	 * use this value to bound the loop.
  	 */
f220a80f0   Xavier Deguillard   VMware balloon: a...
851
  	for (i = 0; i < num_pages; i++) {
df8d0d42a   Nadav Amit   vmw_balloon: chan...
852
  		unsigned long status;
f220a80f0   Xavier Deguillard   VMware balloon: a...
853

6e4453b32   Nadav Amit   vmw_balloon: rewo...
854
  		status = vmballoon_status_page(b, i, &page);
df8d0d42a   Nadav Amit   vmw_balloon: chan...
855
856
857
858
859
860
861
  
  		/*
  		 * Failure of the whole batch overrides a single operation
  		 * results.
  		 */
  		if (batch_status != VMW_BALLOON_SUCCESS)
  			status = batch_status;
f220a80f0   Xavier Deguillard   VMware balloon: a...
862

6e4453b32   Nadav Amit   vmw_balloon: rewo...
863
864
865
  		/* Continue if no error happened */
  		if (!vmballoon_handle_one_result(b, page, ctl->page_size,
  						 status))
df8d0d42a   Nadav Amit   vmw_balloon: chan...
866
  			continue;
df8d0d42a   Nadav Amit   vmw_balloon: chan...
867

8fa3c61a7   Nadav Amit   vmw_balloon: trea...
868
  		/*
6e4453b32   Nadav Amit   vmw_balloon: rewo...
869
870
  		 * Error happened. Move the pages to the refused list and update
  		 * the pages number.
8fa3c61a7   Nadav Amit   vmw_balloon: trea...
871
  		 */
6e4453b32   Nadav Amit   vmw_balloon: rewo...
872
873
874
  		list_move(&page->lru, &ctl->refused_pages);
  		ctl->n_pages--;
  		ctl->n_refused_pages++;
f220a80f0   Xavier Deguillard   VMware balloon: a...
875
  	}
6e4453b32   Nadav Amit   vmw_balloon: rewo...
876
  	spin_unlock(&b->comm_lock);
df8d0d42a   Nadav Amit   vmw_balloon: chan...
877
  	return batch_status == VMW_BALLOON_SUCCESS ? 0 : -EIO;
f220a80f0   Xavier Deguillard   VMware balloon: a...
878
  }
6e4453b32   Nadav Amit   vmw_balloon: rewo...
879
880
881
882
883
884
885
886
  /**
   * vmballoon_release_page_list() - Releases a page list
   *
   * @page_list: list of pages to release.
   * @n_pages: pointer to the number of pages.
   * @page_size: whether the pages in the list are 2MB (or else 4KB).
   *
   * Releases the list of pages and zeros the number of pages.
453dc6593   Dmitry Torokhov   VMware Balloon dr...
887
   */
6e4453b32   Nadav Amit   vmw_balloon: rewo...
888
889
890
  static void vmballoon_release_page_list(struct list_head *page_list,
  				       int *n_pages,
  				       enum vmballoon_page_size_type page_size)
453dc6593   Dmitry Torokhov   VMware Balloon dr...
891
  {
6e4453b32   Nadav Amit   vmw_balloon: rewo...
892
  	struct page *page, *tmp;
df8d0d42a   Nadav Amit   vmw_balloon: chan...
893

6e4453b32   Nadav Amit   vmw_balloon: rewo...
894
895
896
  	list_for_each_entry_safe(page, tmp, page_list, lru) {
  		list_del(&page->lru);
  		__free_pages(page, vmballoon_page_order(page_size));
f220a80f0   Xavier Deguillard   VMware balloon: a...
897
  	}
ae29783d3   Nadav Amit   vmw_balloon: Spli...
898
899
  	if (n_pages)
  		*n_pages = 0;
f220a80f0   Xavier Deguillard   VMware balloon: a...
900
  }
6e4453b32   Nadav Amit   vmw_balloon: rewo...
901

453dc6593   Dmitry Torokhov   VMware Balloon dr...
902
903
904
905
  /*
   * Release pages that were allocated while attempting to inflate the
   * balloon but were refused by the host for one reason or another.
   */
365bd7ef7   Philip P. Moltmann   VMware balloon: S...
906
  static void vmballoon_release_refused_pages(struct vmballoon *b,
6e4453b32   Nadav Amit   vmw_balloon: rewo...
907
  					    struct vmballoon_ctl *ctl)
453dc6593   Dmitry Torokhov   VMware Balloon dr...
908
  {
6e4453b32   Nadav Amit   vmw_balloon: rewo...
909
910
  	vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_REFUSED_FREE,
  				 ctl->page_size);
453dc6593   Dmitry Torokhov   VMware Balloon dr...
911

6e4453b32   Nadav Amit   vmw_balloon: rewo...
912
913
  	vmballoon_release_page_list(&ctl->refused_pages, &ctl->n_refused_pages,
  				    ctl->page_size);
f220a80f0   Xavier Deguillard   VMware balloon: a...
914
  }
8b079cd00   Nadav Amit   vmw_balloon: refa...
915
916
917
918
919
920
921
922
923
924
925
  /**
   * vmballoon_change - retrieve the required balloon change
   *
   * @b: pointer for the balloon.
   *
   * Return: the required change for the balloon size. A positive number
   * indicates inflation, a negative number indicates a deflation.
   */
  static int64_t vmballoon_change(struct vmballoon *b)
  {
  	int64_t size, target;
6e4453b32   Nadav Amit   vmw_balloon: rewo...
926
927
  	size = atomic64_read(&b->size);
  	target = READ_ONCE(b->target);
8b079cd00   Nadav Amit   vmw_balloon: refa...
928
929
930
931
932
933
934
935
936
937
  
  	/*
  	 * We must cast first because of int sizes
  	 * Otherwise we might get huge positives instead of negatives
  	 */
  
  	if (b->reset_required)
  		return 0;
  
  	/* consider a 2MB slack on deflate, unless the balloon is emptied */
6e4453b32   Nadav Amit   vmw_balloon: rewo...
938
939
  	if (target < size && target != 0 &&
  	    size - target < vmballoon_page_in_frames(VMW_BALLOON_2M_PAGE))
8b079cd00   Nadav Amit   vmw_balloon: refa...
940
  		return 0;
5d1a86ecf   Nadav Amit   vmw_balloon: Add ...
941
942
943
  	/* If an out-of-memory recently occurred, inflation is disallowed. */
  	if (target > size && time_before(jiffies, READ_ONCE(b->shrink_timeout)))
  		return 0;
8b079cd00   Nadav Amit   vmw_balloon: refa...
944
945
  	return target - size;
  }
6e4453b32   Nadav Amit   vmw_balloon: rewo...
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
  /**
   * vmballoon_enqueue_page_list() - Enqueues list of pages after inflation.
   *
   * @b: pointer to balloon.
   * @pages: list of pages to enqueue.
   * @n_pages: pointer to number of pages in list. The value is zeroed.
   * @page_size: whether the pages are 2MB or 4KB pages.
   *
   * Enqueues the provides list of pages in the ballooned page list, clears the
   * list and zeroes the number of pages that was provided.
   */
  static void vmballoon_enqueue_page_list(struct vmballoon *b,
  					struct list_head *pages,
  					unsigned int *n_pages,
  					enum vmballoon_page_size_type page_size)
  {
83a8afa72   Nadav Amit   vmw_balloon: Comp...
962
  	unsigned long flags;
468e0ffac   Nadav Amit   vmw_balloon: Fix ...
963
  	struct page *page;
83a8afa72   Nadav Amit   vmw_balloon: Comp...
964
965
966
967
968
969
970
971
972
  
  	if (page_size == VMW_BALLOON_4K_PAGE) {
  		balloon_page_list_enqueue(&b->b_dev_info, pages);
  	} else {
  		/*
  		 * Keep the huge pages in a local list which is not available
  		 * for the balloon compaction mechanism.
  		 */
  		spin_lock_irqsave(&b->b_dev_info.pages_lock, flags);
468e0ffac   Nadav Amit   vmw_balloon: Fix ...
973
974
975
976
  
  		list_for_each_entry(page, pages, lru) {
  			vmballoon_mark_page_offline(page, VMW_BALLOON_2M_PAGE);
  		}
83a8afa72   Nadav Amit   vmw_balloon: Comp...
977
978
979
980
981
  		list_splice_init(pages, &b->huge_pages);
  		__count_vm_events(BALLOON_INFLATE, *n_pages *
  				  vmballoon_page_in_frames(VMW_BALLOON_2M_PAGE));
  		spin_unlock_irqrestore(&b->b_dev_info.pages_lock, flags);
  	}
6e4453b32   Nadav Amit   vmw_balloon: rewo...
982

6e4453b32   Nadav Amit   vmw_balloon: rewo...
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
  	*n_pages = 0;
  }
  
  /**
   * vmballoon_dequeue_page_list() - Dequeues page lists for deflation.
   *
   * @b: pointer to balloon.
   * @pages: list of pages to enqueue.
   * @n_pages: pointer to number of pages in list. The value is zeroed.
   * @page_size: whether the pages are 2MB or 4KB pages.
   * @n_req_pages: the number of requested pages.
   *
   * Dequeues the number of requested pages from the balloon for deflation. The
   * number of dequeued pages may be lower, if not enough pages in the requested
   * size are available.
   */
  static void vmballoon_dequeue_page_list(struct vmballoon *b,
  					struct list_head *pages,
  					unsigned int *n_pages,
  					enum vmballoon_page_size_type page_size,
  					unsigned int n_req_pages)
  {
6e4453b32   Nadav Amit   vmw_balloon: rewo...
1005
1006
  	struct page *page, *tmp;
  	unsigned int i = 0;
83a8afa72   Nadav Amit   vmw_balloon: Comp...
1007
  	unsigned long flags;
6e4453b32   Nadav Amit   vmw_balloon: rewo...
1008

83a8afa72   Nadav Amit   vmw_balloon: Comp...
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
  	/* In the case of 4k pages, use the compaction infrastructure */
  	if (page_size == VMW_BALLOON_4K_PAGE) {
  		*n_pages = balloon_page_list_dequeue(&b->b_dev_info, pages,
  						     n_req_pages);
  		return;
  	}
  
  	/* 2MB pages */
  	spin_lock_irqsave(&b->b_dev_info.pages_lock, flags);
  	list_for_each_entry_safe(page, tmp, &b->huge_pages, lru) {
468e0ffac   Nadav Amit   vmw_balloon: Fix ...
1019
  		vmballoon_mark_page_online(page, VMW_BALLOON_2M_PAGE);
6e4453b32   Nadav Amit   vmw_balloon: rewo...
1020
1021
1022
1023
  		list_move(&page->lru, pages);
  		if (++i == n_req_pages)
  			break;
  	}
83a8afa72   Nadav Amit   vmw_balloon: Comp...
1024
1025
1026
1027
  
  	__count_vm_events(BALLOON_DEFLATE,
  			  i * vmballoon_page_in_frames(VMW_BALLOON_2M_PAGE));
  	spin_unlock_irqrestore(&b->b_dev_info.pages_lock, flags);
6e4453b32   Nadav Amit   vmw_balloon: rewo...
1028
1029
1030
1031
  	*n_pages = i;
  }
  
  /**
ae29783d3   Nadav Amit   vmw_balloon: Spli...
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
   * vmballoon_split_refused_pages() - Split the 2MB refused pages to 4k.
   *
   * If inflation of 2MB pages was denied by the hypervisor, it is likely to be
   * due to one or few 4KB pages. These 2MB pages may keep being allocated and
   * then being refused. To prevent this case, this function splits the refused
   * pages into 4KB pages and adds them into @prealloc_pages list.
   *
   * @ctl: pointer for the %struct vmballoon_ctl, which defines the operation.
   */
  static void vmballoon_split_refused_pages(struct vmballoon_ctl *ctl)
  {
  	struct page *page, *tmp;
  	unsigned int i, order;
  
  	order = vmballoon_page_order(ctl->page_size);
  
  	list_for_each_entry_safe(page, tmp, &ctl->refused_pages, lru) {
  		list_del(&page->lru);
  		split_page(page, order);
  		for (i = 0; i < (1 << order); i++)
  			list_add(&page[i].lru, &ctl->prealloc_pages);
  	}
  	ctl->n_refused_pages = 0;
  }
  
  /**
6e4453b32   Nadav Amit   vmw_balloon: rewo...
1058
1059
1060
   * vmballoon_inflate() - Inflate the balloon towards its target size.
   *
   * @b: pointer to the balloon.
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1061
1062
1063
   */
  static void vmballoon_inflate(struct vmballoon *b)
  {
6e4453b32   Nadav Amit   vmw_balloon: rewo...
1064
1065
1066
1067
  	int64_t to_inflate_frames;
  	struct vmballoon_ctl ctl = {
  		.pages = LIST_HEAD_INIT(ctl.pages),
  		.refused_pages = LIST_HEAD_INIT(ctl.refused_pages),
ae29783d3   Nadav Amit   vmw_balloon: Spli...
1068
  		.prealloc_pages = LIST_HEAD_INIT(ctl.prealloc_pages),
6e4453b32   Nadav Amit   vmw_balloon: rewo...
1069
1070
1071
  		.page_size = b->max_page_size,
  		.op = VMW_BALLOON_INFLATE
  	};
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1072

6e4453b32   Nadav Amit   vmw_balloon: rewo...
1073
1074
1075
  	while ((to_inflate_frames = vmballoon_change(b)) > 0) {
  		unsigned int to_inflate_pages, page_in_frames;
  		int alloc_error, lock_error = 0;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1076

6e4453b32   Nadav Amit   vmw_balloon: rewo...
1077
1078
  		VM_BUG_ON(!list_empty(&ctl.pages));
  		VM_BUG_ON(ctl.n_pages != 0);
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1079

6e4453b32   Nadav Amit   vmw_balloon: rewo...
1080
  		page_in_frames = vmballoon_page_in_frames(ctl.page_size);
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1081

6e4453b32   Nadav Amit   vmw_balloon: rewo...
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
  		to_inflate_pages = min_t(unsigned long, b->batch_max_pages,
  					 DIV_ROUND_UP_ULL(to_inflate_frames,
  							  page_in_frames));
  
  		/* Start by allocating */
  		alloc_error = vmballoon_alloc_page_list(b, &ctl,
  							to_inflate_pages);
  
  		/* Actually lock the pages by telling the hypervisor */
  		lock_error = vmballoon_lock(b, &ctl);
  
  		/*
  		 * If an error indicates that something serious went wrong,
  		 * stop the inflation.
  		 */
  		if (lock_error)
622074a9f   Nadav Amit   vmw_balloon: remo...
1098
  			break;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1099

6e4453b32   Nadav Amit   vmw_balloon: rewo...
1100
1101
  		/* Update the balloon size */
  		atomic64_add(ctl.n_pages * page_in_frames, &b->size);
8fa3c61a7   Nadav Amit   vmw_balloon: trea...
1102

6e4453b32   Nadav Amit   vmw_balloon: rewo...
1103
1104
  		vmballoon_enqueue_page_list(b, &ctl.pages, &ctl.n_pages,
  					    ctl.page_size);
10a95d5d8   Nadav Amit   vmw_balloon: hand...
1105

6e4453b32   Nadav Amit   vmw_balloon: rewo...
1106
1107
1108
1109
1110
1111
1112
1113
  		/*
  		 * If allocation failed or the number of refused pages exceeds
  		 * the maximum allowed, move to the next page size.
  		 */
  		if (alloc_error ||
  		    ctl.n_refused_pages >= VMW_BALLOON_MAX_REFUSED) {
  			if (ctl.page_size == VMW_BALLOON_4K_PAGE)
  				break;
8fa3c61a7   Nadav Amit   vmw_balloon: trea...
1114
1115
  
  			/*
ae29783d3   Nadav Amit   vmw_balloon: Spli...
1116
1117
  			 * Split the refused pages to 4k. This will also empty
  			 * the refused pages list.
8fa3c61a7   Nadav Amit   vmw_balloon: trea...
1118
  			 */
ae29783d3   Nadav Amit   vmw_balloon: Spli...
1119
  			vmballoon_split_refused_pages(&ctl);
6e4453b32   Nadav Amit   vmw_balloon: rewo...
1120
  			ctl.page_size--;
f220a80f0   Xavier Deguillard   VMware balloon: a...
1121
  		}
ef0f8f112   Xavier Deguillard   VMware balloon: p...
1122

33d268ed0   Philip P. Moltmann   VMware balloon: D...
1123
  		cond_resched();
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1124
  	}
6e4453b32   Nadav Amit   vmw_balloon: rewo...
1125
1126
1127
1128
1129
1130
1131
  	/*
  	 * Release pages that were allocated while attempting to inflate the
  	 * balloon but were refused by the host for one reason or another,
  	 * and update the statistics.
  	 */
  	if (ctl.n_refused_pages != 0)
  		vmballoon_release_refused_pages(b, &ctl);
ae29783d3   Nadav Amit   vmw_balloon: Spli...
1132
1133
  
  	vmballoon_release_page_list(&ctl.prealloc_pages, NULL, ctl.page_size);
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1134
  }
6e4453b32   Nadav Amit   vmw_balloon: rewo...
1135
1136
1137
1138
1139
1140
1141
1142
  /**
   * vmballoon_deflate() - Decrease the size of the balloon.
   *
   * @b: pointer to the balloon
   * @n_frames: the number of frames to deflate. If zero, automatically
   * calculated according to the target size.
   * @coordinated: whether to coordinate with the host
   *
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1143
   * Decrease the size of the balloon allowing guest to use more memory.
6e4453b32   Nadav Amit   vmw_balloon: rewo...
1144
1145
   *
   * Return: The number of deflated frames (i.e., basic page size units)
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1146
   */
6e4453b32   Nadav Amit   vmw_balloon: rewo...
1147
1148
  static unsigned long vmballoon_deflate(struct vmballoon *b, uint64_t n_frames,
  				       bool coordinated)
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1149
  {
6e4453b32   Nadav Amit   vmw_balloon: rewo...
1150
1151
1152
1153
1154
1155
1156
1157
  	unsigned long deflated_frames = 0;
  	unsigned long tried_frames = 0;
  	struct vmballoon_ctl ctl = {
  		.pages = LIST_HEAD_INIT(ctl.pages),
  		.refused_pages = LIST_HEAD_INIT(ctl.refused_pages),
  		.page_size = VMW_BALLOON_4K_PAGE,
  		.op = VMW_BALLOON_DEFLATE
  	};
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1158

453dc6593   Dmitry Torokhov   VMware Balloon dr...
1159
  	/* free pages to reach target */
6e4453b32   Nadav Amit   vmw_balloon: rewo...
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
  	while (true) {
  		unsigned int to_deflate_pages, n_unlocked_frames;
  		unsigned int page_in_frames;
  		int64_t to_deflate_frames;
  		bool deflated_all;
  
  		page_in_frames = vmballoon_page_in_frames(ctl.page_size);
  
  		VM_BUG_ON(!list_empty(&ctl.pages));
  		VM_BUG_ON(ctl.n_pages);
  		VM_BUG_ON(!list_empty(&ctl.refused_pages));
  		VM_BUG_ON(ctl.n_refused_pages);
  
  		/*
  		 * If we were requested a specific number of frames, we try to
  		 * deflate this number of frames. Otherwise, deflation is
  		 * performed according to the target and balloon size.
  		 */
  		to_deflate_frames = n_frames ? n_frames - tried_frames :
  					       -vmballoon_change(b);
  
  		/* break if no work to do */
  		if (to_deflate_frames <= 0)
  			break;
  
  		/*
  		 * Calculate the number of frames based on current page size,
  		 * but limit the deflated frames to a single chunk
  		 */
  		to_deflate_pages = min_t(unsigned long, b->batch_max_pages,
  					 DIV_ROUND_UP_ULL(to_deflate_frames,
  							  page_in_frames));
  
  		/* First take the pages from the balloon pages. */
  		vmballoon_dequeue_page_list(b, &ctl.pages, &ctl.n_pages,
  					    ctl.page_size, to_deflate_pages);
f220a80f0   Xavier Deguillard   VMware balloon: a...
1196

6e4453b32   Nadav Amit   vmw_balloon: rewo...
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
  		/*
  		 * Before pages are moving to the refused list, count their
  		 * frames as frames that we tried to deflate.
  		 */
  		tried_frames += ctl.n_pages * page_in_frames;
  
  		/*
  		 * Unlock the pages by communicating with the hypervisor if the
  		 * communication is coordinated (i.e., not pop). We ignore the
  		 * return code. Instead we check if all the pages we manage to
  		 * unlock all the pages. If we failed, we will move to the next
  		 * page size, and would eventually try again later.
  		 */
  		if (coordinated)
  			vmballoon_lock(b, &ctl);
  
  		/*
  		 * Check if we deflated enough. We will move to the next page
  		 * size if we did not manage to do so. This calculation takes
  		 * place now, as once the pages are released, the number of
  		 * pages is zeroed.
  		 */
  		deflated_all = (ctl.n_pages == to_deflate_pages);
33d268ed0   Philip P. Moltmann   VMware balloon: D...
1220

6e4453b32   Nadav Amit   vmw_balloon: rewo...
1221
1222
1223
1224
  		/* Update local and global counters */
  		n_unlocked_frames = ctl.n_pages * page_in_frames;
  		atomic64_sub(n_unlocked_frames, &b->size);
  		deflated_frames += n_unlocked_frames;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1225

6e4453b32   Nadav Amit   vmw_balloon: rewo...
1226
1227
  		vmballoon_stats_page_add(b, VMW_BALLOON_PAGE_STAT_FREE,
  					 ctl.page_size, ctl.n_pages);
33d268ed0   Philip P. Moltmann   VMware balloon: D...
1228

6e4453b32   Nadav Amit   vmw_balloon: rewo...
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
  		/* free the ballooned pages */
  		vmballoon_release_page_list(&ctl.pages, &ctl.n_pages,
  					    ctl.page_size);
  
  		/* Return the refused pages to the ballooned list. */
  		vmballoon_enqueue_page_list(b, &ctl.refused_pages,
  					    &ctl.n_refused_pages,
  					    ctl.page_size);
  
  		/* If we failed to unlock all the pages, move to next size. */
  		if (!deflated_all) {
  			if (ctl.page_size == b->max_page_size)
  				break;
  			ctl.page_size++;
365bd7ef7   Philip P. Moltmann   VMware balloon: S...
1243
  		}
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1244

6e4453b32   Nadav Amit   vmw_balloon: rewo...
1245
  		cond_resched();
365bd7ef7   Philip P. Moltmann   VMware balloon: S...
1246
  	}
6e4453b32   Nadav Amit   vmw_balloon: rewo...
1247
1248
  
  	return deflated_frames;
f220a80f0   Xavier Deguillard   VMware balloon: a...
1249
  }
df8d0d42a   Nadav Amit   vmw_balloon: chan...
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
  /**
   * vmballoon_deinit_batching - disables batching mode.
   *
   * @b: pointer to &struct vmballoon.
   *
   * Disables batching, by deallocating the page for communication with the
   * hypervisor and disabling the static key to indicate that batching is off.
   */
  static void vmballoon_deinit_batching(struct vmballoon *b)
  {
  	free_page((unsigned long)b->batch_page);
  	b->batch_page = NULL;
  	static_branch_disable(&vmw_balloon_batching);
  	b->batch_max_pages = 1;
  }
f220a80f0   Xavier Deguillard   VMware balloon: a...
1265

df8d0d42a   Nadav Amit   vmw_balloon: chan...
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
  /**
   * vmballoon_init_batching - enable batching mode.
   *
   * @b: pointer to &struct vmballoon.
   *
   * Enables batching, by allocating a page for communication with the hypervisor
   * and enabling the static_key to use batching.
   *
   * Return: zero on success or an appropriate error-code.
   */
  static int vmballoon_init_batching(struct vmballoon *b)
f220a80f0   Xavier Deguillard   VMware balloon: a...
1277
  {
b23220fe0   Gil Kupfer   vmw_balloon: fixi...
1278
  	struct page *page;
f220a80f0   Xavier Deguillard   VMware balloon: a...
1279

b23220fe0   Gil Kupfer   vmw_balloon: fixi...
1280
1281
  	page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  	if (!page)
df8d0d42a   Nadav Amit   vmw_balloon: chan...
1282
  		return -ENOMEM;
f220a80f0   Xavier Deguillard   VMware balloon: a...
1283

b23220fe0   Gil Kupfer   vmw_balloon: fixi...
1284
  	b->batch_page = page_address(page);
df8d0d42a   Nadav Amit   vmw_balloon: chan...
1285
1286
1287
1288
1289
  	b->batch_max_pages = PAGE_SIZE / sizeof(struct vmballoon_batch_entry);
  
  	static_branch_enable(&vmw_balloon_batching);
  
  	return 0;
f220a80f0   Xavier Deguillard   VMware balloon: a...
1290
1291
1292
  }
  
  /*
48e3d668b   Philip P. Moltmann   VMware balloon: E...
1293
1294
1295
1296
1297
   * Receive notification and resize balloon
   */
  static void vmballoon_doorbell(void *client_data)
  {
  	struct vmballoon *b = client_data;
c7b3690fb   Nadav Amit   vmw_balloon: stat...
1298
  	vmballoon_stats_gen_inc(b, VMW_BALLOON_STAT_DOORBELL);
48e3d668b   Philip P. Moltmann   VMware balloon: E...
1299
1300
1301
1302
1303
1304
1305
1306
1307
  
  	mod_delayed_work(system_freezable_wq, &b->dwork, 0);
  }
  
  /*
   * Clean up vmci doorbell
   */
  static void vmballoon_vmci_cleanup(struct vmballoon *b)
  {
10a95d5d8   Nadav Amit   vmw_balloon: hand...
1308
1309
  	vmballoon_cmd(b, VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
  		      VMCI_INVALID_ID, VMCI_INVALID_ID);
48e3d668b   Philip P. Moltmann   VMware balloon: E...
1310

48e3d668b   Philip P. Moltmann   VMware balloon: E...
1311
1312
1313
1314
1315
  	if (!vmci_handle_is_invalid(b->vmci_doorbell)) {
  		vmci_doorbell_destroy(b->vmci_doorbell);
  		b->vmci_doorbell = VMCI_INVALID_HANDLE;
  	}
  }
22d293ee8   Nadav Amit   vmw_balloon: gene...
1316
1317
1318
1319
1320
1321
1322
1323
1324
  /**
   * vmballoon_vmci_init - Initialize vmci doorbell.
   *
   * @b: pointer to the balloon.
   *
   * Return: zero on success or when wakeup command not supported. Error-code
   * otherwise.
   *
   * Initialize vmci doorbell, to get notified as soon as balloon changes.
48e3d668b   Philip P. Moltmann   VMware balloon: E...
1325
1326
1327
   */
  static int vmballoon_vmci_init(struct vmballoon *b)
  {
10a95d5d8   Nadav Amit   vmw_balloon: hand...
1328
  	unsigned long error;
48e3d668b   Philip P. Moltmann   VMware balloon: E...
1329

ce664331b   Nadav Amit   vmw_balloon: VMCI...
1330
1331
  	if ((b->capabilities & VMW_BALLOON_SIGNALLED_WAKEUP_CMD) == 0)
  		return 0;
48e3d668b   Philip P. Moltmann   VMware balloon: E...
1332

ce664331b   Nadav Amit   vmw_balloon: VMCI...
1333
1334
1335
  	error = vmci_doorbell_create(&b->vmci_doorbell, VMCI_FLAG_DELAYED_CB,
  				     VMCI_PRIVILEGE_FLAG_RESTRICTED,
  				     vmballoon_doorbell, b);
48e3d668b   Philip P. Moltmann   VMware balloon: E...
1336

ce664331b   Nadav Amit   vmw_balloon: VMCI...
1337
1338
  	if (error != VMCI_SUCCESS)
  		goto fail;
10a95d5d8   Nadav Amit   vmw_balloon: hand...
1339
1340
1341
  	error =	__vmballoon_cmd(b, VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
  				b->vmci_doorbell.context,
  				b->vmci_doorbell.resource, NULL);
ce664331b   Nadav Amit   vmw_balloon: VMCI...
1342

ce664331b   Nadav Amit   vmw_balloon: VMCI...
1343
1344
  	if (error != VMW_BALLOON_SUCCESS)
  		goto fail;
48e3d668b   Philip P. Moltmann   VMware balloon: E...
1345
1346
  
  	return 0;
ce664331b   Nadav Amit   vmw_balloon: VMCI...
1347
1348
1349
  fail:
  	vmballoon_vmci_cleanup(b);
  	return -EIO;
48e3d668b   Philip P. Moltmann   VMware balloon: E...
1350
  }
6e4453b32   Nadav Amit   vmw_balloon: rewo...
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
  /**
   * vmballoon_pop - Quickly release all pages allocate for the balloon.
   *
   * @b: pointer to the balloon.
   *
   * This function is called when host decides to "reset" balloon for one reason
   * or another. Unlike normal "deflate" we do not (shall not) notify host of the
   * pages being released.
   */
  static void vmballoon_pop(struct vmballoon *b)
  {
  	unsigned long size;
  
  	while ((size = atomic64_read(&b->size)))
  		vmballoon_deflate(b, size, false);
  }
48e3d668b   Philip P. Moltmann   VMware balloon: E...
1367
  /*
f220a80f0   Xavier Deguillard   VMware balloon: a...
1368
1369
1370
1371
1372
1373
   * Perform standard reset sequence by popping the balloon (in case it
   * is not  empty) and then restarting protocol. This operation normally
   * happens when host responds with VMW_BALLOON_ERROR_RESET to a command.
   */
  static void vmballoon_reset(struct vmballoon *b)
  {
48e3d668b   Philip P. Moltmann   VMware balloon: E...
1374
  	int error;
c7b3690fb   Nadav Amit   vmw_balloon: stat...
1375
  	down_write(&b->conf_sem);
48e3d668b   Philip P. Moltmann   VMware balloon: E...
1376
  	vmballoon_vmci_cleanup(b);
f220a80f0   Xavier Deguillard   VMware balloon: a...
1377
1378
  	/* free all pages, skipping monitor unlock */
  	vmballoon_pop(b);
22d293ee8   Nadav Amit   vmw_balloon: gene...
1379
  	if (vmballoon_send_start(b, VMW_BALLOON_CAPABILITIES))
d04071a5d   Dan Carpenter   vmw_balloon: rele...
1380
  		goto unlock;
f220a80f0   Xavier Deguillard   VMware balloon: a...
1381
1382
  
  	if ((b->capabilities & VMW_BALLOON_BATCHED_CMDS) != 0) {
df8d0d42a   Nadav Amit   vmw_balloon: chan...
1383
  		if (vmballoon_init_batching(b)) {
f220a80f0   Xavier Deguillard   VMware balloon: a...
1384
1385
1386
1387
1388
1389
1390
  			/*
  			 * We failed to initialize batching, inform the monitor
  			 * about it by sending a null capability.
  			 *
  			 * The guest will retry in one second.
  			 */
  			vmballoon_send_start(b, 0);
d04071a5d   Dan Carpenter   vmw_balloon: rele...
1391
  			goto unlock;
f220a80f0   Xavier Deguillard   VMware balloon: a...
1392
1393
  		}
  	} else if ((b->capabilities & VMW_BALLOON_BASIC_CMDS) != 0) {
df8d0d42a   Nadav Amit   vmw_balloon: chan...
1394
  		vmballoon_deinit_batching(b);
f220a80f0   Xavier Deguillard   VMware balloon: a...
1395
  	}
8840a6f4a   Nadav Amit   vmw_balloon: add ...
1396
  	vmballoon_stats_gen_inc(b, VMW_BALLOON_STAT_RESET);
f220a80f0   Xavier Deguillard   VMware balloon: a...
1397
  	b->reset_required = false;
48e3d668b   Philip P. Moltmann   VMware balloon: E...
1398
1399
1400
1401
1402
  
  	error = vmballoon_vmci_init(b);
  	if (error)
  		pr_err("failed to initialize vmci doorbell
  ");
22d293ee8   Nadav Amit   vmw_balloon: gene...
1403
  	if (vmballoon_send_guest_id(b))
f220a80f0   Xavier Deguillard   VMware balloon: a...
1404
1405
  		pr_err("failed to send guest ID to the host
  ");
c7b3690fb   Nadav Amit   vmw_balloon: stat...
1406

d04071a5d   Dan Carpenter   vmw_balloon: rele...
1407
  unlock:
c7b3690fb   Nadav Amit   vmw_balloon: stat...
1408
  	up_write(&b->conf_sem);
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1409
  }
8b079cd00   Nadav Amit   vmw_balloon: refa...
1410
1411
1412
1413
1414
1415
1416
  /**
   * vmballoon_work - periodic balloon worker for reset, inflation and deflation.
   *
   * @work: pointer to the &work_struct which is provided by the workqueue.
   *
   * Resets the protocol if needed, gets the new size and adjusts balloon as
   * needed. Repeat in 1 sec.
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1417
1418
1419
1420
1421
   */
  static void vmballoon_work(struct work_struct *work)
  {
  	struct delayed_work *dwork = to_delayed_work(work);
  	struct vmballoon *b = container_of(dwork, struct vmballoon, dwork);
8b079cd00   Nadav Amit   vmw_balloon: refa...
1422
  	int64_t change = 0;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1423

453dc6593   Dmitry Torokhov   VMware Balloon dr...
1424
1425
  	if (b->reset_required)
  		vmballoon_reset(b);
c7b3690fb   Nadav Amit   vmw_balloon: stat...
1426
1427
1428
1429
1430
1431
1432
1433
  	down_read(&b->conf_sem);
  
  	/*
  	 * Update the stats while holding the semaphore to ensure that
  	 * @stats_enabled is consistent with whether the stats are actually
  	 * enabled
  	 */
  	vmballoon_stats_gen_inc(b, VMW_BALLOON_STAT_TIMER);
0395be3ec   Nadav Amit   vmw_balloon: simp...
1434
  	if (!vmballoon_send_get_target(b))
8b079cd00   Nadav Amit   vmw_balloon: refa...
1435
1436
1437
  		change = vmballoon_change(b);
  
  	if (change != 0) {
6e4453b32   Nadav Amit   vmw_balloon: rewo...
1438
1439
1440
  		pr_debug("%s - size: %llu, target %lu
  ", __func__,
  			 atomic64_read(&b->size), READ_ONCE(b->target));
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1441

8b079cd00   Nadav Amit   vmw_balloon: refa...
1442
  		if (change > 0)
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1443
  			vmballoon_inflate(b);
8b079cd00   Nadav Amit   vmw_balloon: refa...
1444
  		else  /* (change < 0) */
6e4453b32   Nadav Amit   vmw_balloon: rewo...
1445
  			vmballoon_deflate(b, 0, true);
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1446
  	}
c7b3690fb   Nadav Amit   vmw_balloon: stat...
1447
  	up_read(&b->conf_sem);
beda94da3   Dmitry Torokhov   Vmware balloon: s...
1448
1449
1450
1451
1452
1453
  	/*
  	 * We are using a freezable workqueue so that balloon operations are
  	 * stopped while the system transitions to/from sleep/hibernation.
  	 */
  	queue_delayed_work(system_freezable_wq,
  			   dwork, round_jiffies_relative(HZ));
c7b3690fb   Nadav Amit   vmw_balloon: stat...
1454

453dc6593   Dmitry Torokhov   VMware Balloon dr...
1455
  }
5d1a86ecf   Nadav Amit   vmw_balloon: Add ...
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
  /**
   * vmballoon_shrinker_scan() - deflate the balloon due to memory pressure.
   * @shrinker: pointer to the balloon shrinker.
   * @sc: page reclaim information.
   *
   * Returns: number of pages that were freed during deflation.
   */
  static unsigned long vmballoon_shrinker_scan(struct shrinker *shrinker,
  					     struct shrink_control *sc)
  {
  	struct vmballoon *b = &balloon;
  	unsigned long deflated_frames;
  
  	pr_debug("%s - size: %llu", __func__, atomic64_read(&b->size));
  
  	vmballoon_stats_gen_inc(b, VMW_BALLOON_STAT_SHRINK);
  
  	/*
  	 * If the lock is also contended for read, we cannot easily reclaim and
  	 * we bail out.
  	 */
  	if (!down_read_trylock(&b->conf_sem))
  		return 0;
  
  	deflated_frames = vmballoon_deflate(b, sc->nr_to_scan, true);
  
  	vmballoon_stats_gen_add(b, VMW_BALLOON_STAT_SHRINK_FREE,
  				deflated_frames);
  
  	/*
  	 * Delay future inflation for some time to mitigate the situations in
  	 * which balloon continuously grows and shrinks. Use WRITE_ONCE() since
  	 * the access is asynchronous.
  	 */
  	WRITE_ONCE(b->shrink_timeout, jiffies + HZ * VMBALLOON_SHRINK_DELAY);
  
  	up_read(&b->conf_sem);
  
  	return deflated_frames;
  }
  
  /**
   * vmballoon_shrinker_count() - return the number of ballooned pages.
   * @shrinker: pointer to the balloon shrinker.
   * @sc: page reclaim information.
   *
   * Returns: number of 4k pages that are allocated for the balloon and can
   *	    therefore be reclaimed under pressure.
   */
  static unsigned long vmballoon_shrinker_count(struct shrinker *shrinker,
  					      struct shrink_control *sc)
  {
  	struct vmballoon *b = &balloon;
  
  	return atomic64_read(&b->size);
  }
  
  static void vmballoon_unregister_shrinker(struct vmballoon *b)
  {
  	if (b->shrinker_registered)
  		unregister_shrinker(&b->shrinker);
  	b->shrinker_registered = false;
  }
  
  static int vmballoon_register_shrinker(struct vmballoon *b)
  {
  	int r;
  
  	/* Do nothing if the shrinker is not enabled */
  	if (!vmwballoon_shrinker_enable)
  		return 0;
  
  	b->shrinker.scan_objects = vmballoon_shrinker_scan;
  	b->shrinker.count_objects = vmballoon_shrinker_count;
  	b->shrinker.seeks = DEFAULT_SEEKS;
  
  	r = register_shrinker(&b->shrinker);
  
  	if (r == 0)
  		b->shrinker_registered = true;
  
  	return r;
  }
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1539
1540
1541
1542
  /*
   * DEBUGFS Interface
   */
  #ifdef CONFIG_DEBUG_FS
c7b3690fb   Nadav Amit   vmw_balloon: stat...
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
  static const char * const vmballoon_stat_page_names[] = {
  	[VMW_BALLOON_PAGE_STAT_ALLOC]		= "alloc",
  	[VMW_BALLOON_PAGE_STAT_ALLOC_FAIL]	= "allocFail",
  	[VMW_BALLOON_PAGE_STAT_REFUSED_ALLOC]	= "errAlloc",
  	[VMW_BALLOON_PAGE_STAT_REFUSED_FREE]	= "errFree",
  	[VMW_BALLOON_PAGE_STAT_FREE]		= "free"
  };
  
  static const char * const vmballoon_stat_names[] = {
  	[VMW_BALLOON_STAT_TIMER]		= "timer",
8840a6f4a   Nadav Amit   vmw_balloon: add ...
1553
1554
  	[VMW_BALLOON_STAT_DOORBELL]		= "doorbell",
  	[VMW_BALLOON_STAT_RESET]		= "reset",
5d1a86ecf   Nadav Amit   vmw_balloon: Add ...
1555
1556
  	[VMW_BALLOON_STAT_SHRINK]		= "shrink",
  	[VMW_BALLOON_STAT_SHRINK_FREE]		= "shrinkFree"
c7b3690fb   Nadav Amit   vmw_balloon: stat...
1557
  };
c7b3690fb   Nadav Amit   vmw_balloon: stat...
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
  static int vmballoon_enable_stats(struct vmballoon *b)
  {
  	int r = 0;
  
  	down_write(&b->conf_sem);
  
  	/* did we somehow race with another reader which enabled stats? */
  	if (b->stats)
  		goto out;
  
  	b->stats = kzalloc(sizeof(*b->stats), GFP_KERNEL);
  
  	if (!b->stats) {
  		/* allocation failed */
  		r = -ENOMEM;
  		goto out;
  	}
  	static_key_enable(&balloon_stat_enabled.key);
  out:
  	up_write(&b->conf_sem);
  	return r;
  }
  
  /**
   * vmballoon_debug_show - shows statistics of balloon operations.
   * @f: pointer to the &struct seq_file.
   * @offset: ignored.
   *
   * Provides the statistics that can be accessed in vmmemctl in the debugfs.
   * To avoid the overhead - mainly that of memory - of collecting the statistics,
   * we only collect statistics after the first time the counters are read.
   *
   * Return: zero on success or an error code.
   */
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1592
1593
1594
  static int vmballoon_debug_show(struct seq_file *f, void *offset)
  {
  	struct vmballoon *b = f->private;
c7b3690fb   Nadav Amit   vmw_balloon: stat...
1595
1596
1597
1598
1599
1600
1601
1602
1603
  	int i, j;
  
  	/* enables stats if they are disabled */
  	if (!b->stats) {
  		int r = vmballoon_enable_stats(b);
  
  		if (r)
  			return r;
  	}
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1604

b36e89da8   Philip P. Moltmann   VMware balloon: S...
1605
  	/* format capabilities info */
6e4453b32   Nadav Amit   vmw_balloon: rewo...
1606
1607
  	seq_printf(f, "%-22s: %#16x
  ", "balloon capabilities",
c7b3690fb   Nadav Amit   vmw_balloon: stat...
1608
  		   VMW_BALLOON_CAPABILITIES);
6e4453b32   Nadav Amit   vmw_balloon: rewo...
1609
1610
  	seq_printf(f, "%-22s: %#16lx
  ", "used capabilities", b->capabilities);
c7b3690fb   Nadav Amit   vmw_balloon: stat...
1611
1612
1613
  	seq_printf(f, "%-22s: %16s
  ", "is resetting",
  		   b->reset_required ? "y" : "n");
b36e89da8   Philip P. Moltmann   VMware balloon: S...
1614

453dc6593   Dmitry Torokhov   VMware Balloon dr...
1615
  	/* format size info */
6e4453b32   Nadav Amit   vmw_balloon: rewo...
1616
1617
1618
1619
  	seq_printf(f, "%-22s: %16lu
  ", "target", READ_ONCE(b->target));
  	seq_printf(f, "%-22s: %16llu
  ", "current", atomic64_read(&b->size));
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1620

681311848   Nadav Amit   vmw_balloon: unif...
1621
1622
1623
  	for (i = 0; i < VMW_BALLOON_CMD_NUM; i++) {
  		if (vmballoon_cmd_names[i] == NULL)
  			continue;
c7b3690fb   Nadav Amit   vmw_balloon: stat...
1624
1625
1626
1627
1628
  		seq_printf(f, "%-22s: %16llu (%llu failed)
  ",
  			   vmballoon_cmd_names[i],
  			   atomic64_read(&b->stats->ops[i][VMW_BALLOON_OP_STAT]),
  			   atomic64_read(&b->stats->ops[i][VMW_BALLOON_OP_FAIL_STAT]));
681311848   Nadav Amit   vmw_balloon: unif...
1629
  	}
c7b3690fb   Nadav Amit   vmw_balloon: stat...
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
  	for (i = 0; i < VMW_BALLOON_STAT_NUM; i++)
  		seq_printf(f, "%-22s: %16llu
  ",
  			   vmballoon_stat_names[i],
  			   atomic64_read(&b->stats->general_stat[i]));
  
  	for (i = 0; i < VMW_BALLOON_PAGE_STAT_NUM; i++) {
  		for (j = 0; j < VMW_BALLOON_NUM_PAGE_SIZES; j++)
  			seq_printf(f, "%-18s(%s): %16llu
  ",
  				   vmballoon_stat_page_names[i],
  				   vmballoon_page_size_names[j],
  				   atomic64_read(&b->stats->page_stat[i][j]));
  	}
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1644
1645
1646
  
  	return 0;
  }
2796b43fe   Yangtao Li   misc: remove GENW...
1647
  DEFINE_SHOW_ATTRIBUTE(vmballoon_debug);
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1648

225afca60   Greg Kroah-Hartman   vmw_balloon: no n...
1649
  static void __init vmballoon_debugfs_init(struct vmballoon *b)
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1650
  {
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1651
1652
  	b->dbg_entry = debugfs_create_file("vmmemctl", S_IRUGO, NULL, b,
  					   &vmballoon_debug_fops);
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1653
1654
1655
1656
  }
  
  static void __exit vmballoon_debugfs_exit(struct vmballoon *b)
  {
c7b3690fb   Nadav Amit   vmw_balloon: stat...
1657
  	static_key_disable(&balloon_stat_enabled.key);
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1658
  	debugfs_remove(b->dbg_entry);
c7b3690fb   Nadav Amit   vmw_balloon: stat...
1659
1660
  	kfree(b->stats);
  	b->stats = NULL;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1661
1662
1663
  }
  
  #else
225afca60   Greg Kroah-Hartman   vmw_balloon: no n...
1664
  static inline void vmballoon_debugfs_init(struct vmballoon *b)
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1665
  {
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1666
1667
1668
1669
1670
1671
1672
  }
  
  static inline void vmballoon_debugfs_exit(struct vmballoon *b)
  {
  }
  
  #endif	/* CONFIG_DEBUG_FS */
83a8afa72   Nadav Amit   vmw_balloon: Comp...
1673
1674
  
  #ifdef CONFIG_BALLOON_COMPACTION
933a90bf4   Linus Torvalds   Merge branch 'wor...
1675
  static int vmballoon_init_fs_context(struct fs_context *fc)
83a8afa72   Nadav Amit   vmw_balloon: Comp...
1676
  {
933a90bf4   Linus Torvalds   Merge branch 'wor...
1677
  	return init_pseudo(fc, BALLOON_VMW_MAGIC) ? 0 : -ENOMEM;
83a8afa72   Nadav Amit   vmw_balloon: Comp...
1678
1679
1680
  }
  
  static struct file_system_type vmballoon_fs = {
933a90bf4   Linus Torvalds   Merge branch 'wor...
1681
1682
1683
  	.name           	= "balloon-vmware",
  	.init_fs_context	= vmballoon_init_fs_context,
  	.kill_sb        	= kill_anon_super,
83a8afa72   Nadav Amit   vmw_balloon: Comp...
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
  };
  
  static struct vfsmount *vmballoon_mnt;
  
  /**
   * vmballoon_migratepage() - migrates a balloon page.
   * @b_dev_info: balloon device information descriptor.
   * @newpage: the page to which @page should be migrated.
   * @page: a ballooned page that should be migrated.
   * @mode: migration mode, ignored.
   *
   * This function is really open-coded, but that is according to the interface
   * that balloon_compaction provides.
   *
   * Return: zero on success, -EAGAIN when migration cannot be performed
   *	   momentarily, and -EBUSY if migration failed and should be retried
   *	   with that specific page.
   */
  static int vmballoon_migratepage(struct balloon_dev_info *b_dev_info,
  				 struct page *newpage, struct page *page,
  				 enum migrate_mode mode)
  {
  	unsigned long status, flags;
  	struct vmballoon *b;
  	int ret;
  
  	b = container_of(b_dev_info, struct vmballoon, b_dev_info);
  
  	/*
  	 * If the semaphore is taken, there is ongoing configuration change
  	 * (i.e., balloon reset), so try again.
  	 */
  	if (!down_read_trylock(&b->conf_sem))
  		return -EAGAIN;
  
  	spin_lock(&b->comm_lock);
  	/*
  	 * We must start by deflating and not inflating, as otherwise the
  	 * hypervisor may tell us that it has enough memory and the new page is
  	 * not needed. Since the old page is isolated, we cannot use the list
  	 * interface to unlock it, as the LRU field is used for isolation.
  	 * Instead, we use the native interface directly.
  	 */
  	vmballoon_add_page(b, 0, page);
  	status = vmballoon_lock_op(b, 1, VMW_BALLOON_4K_PAGE,
  				   VMW_BALLOON_DEFLATE);
  
  	if (status == VMW_BALLOON_SUCCESS)
  		status = vmballoon_status_page(b, 0, &page);
  
  	/*
  	 * If a failure happened, let the migration mechanism know that it
  	 * should not retry.
  	 */
  	if (status != VMW_BALLOON_SUCCESS) {
  		spin_unlock(&b->comm_lock);
  		ret = -EBUSY;
  		goto out_unlock;
  	}
  
  	/*
  	 * The page is isolated, so it is safe to delete it without holding
  	 * @pages_lock . We keep holding @comm_lock since we will need it in a
  	 * second.
  	 */
  	balloon_page_delete(page);
  
  	put_page(page);
  
  	/* Inflate */
  	vmballoon_add_page(b, 0, newpage);
  	status = vmballoon_lock_op(b, 1, VMW_BALLOON_4K_PAGE,
  				   VMW_BALLOON_INFLATE);
  
  	if (status == VMW_BALLOON_SUCCESS)
  		status = vmballoon_status_page(b, 0, &newpage);
  
  	spin_unlock(&b->comm_lock);
  
  	if (status != VMW_BALLOON_SUCCESS) {
  		/*
  		 * A failure happened. While we can deflate the page we just
  		 * inflated, this deflation can also encounter an error. Instead
  		 * we will decrease the size of the balloon to reflect the
  		 * change and report failure.
  		 */
  		atomic64_dec(&b->size);
  		ret = -EBUSY;
  	} else {
  		/*
  		 * Success. Take a reference for the page, and we will add it to
  		 * the list after acquiring the lock.
  		 */
  		get_page(newpage);
  		ret = MIGRATEPAGE_SUCCESS;
  	}
  
  	/* Update the balloon list under the @pages_lock */
  	spin_lock_irqsave(&b->b_dev_info.pages_lock, flags);
  
  	/*
  	 * On inflation success, we already took a reference for the @newpage.
  	 * If we succeed just insert it to the list and update the statistics
  	 * under the lock.
  	 */
  	if (ret == MIGRATEPAGE_SUCCESS) {
  		balloon_page_insert(&b->b_dev_info, newpage);
  		__count_vm_event(BALLOON_MIGRATE);
  	}
  
  	/*
  	 * We deflated successfully, so regardless to the inflation success, we
  	 * need to reduce the number of isolated_pages.
  	 */
  	b->b_dev_info.isolated_pages--;
  	spin_unlock_irqrestore(&b->b_dev_info.pages_lock, flags);
  
  out_unlock:
  	up_read(&b->conf_sem);
  	return ret;
  }
  
  /**
   * vmballoon_compaction_deinit() - removes compaction related data.
   *
   * @b: pointer to the balloon.
   */
  static void vmballoon_compaction_deinit(struct vmballoon *b)
  {
  	if (!IS_ERR(b->b_dev_info.inode))
  		iput(b->b_dev_info.inode);
  
  	b->b_dev_info.inode = NULL;
  	kern_unmount(vmballoon_mnt);
  	vmballoon_mnt = NULL;
  }
  
  /**
   * vmballoon_compaction_init() - initialized compaction for the balloon.
   *
   * @b: pointer to the balloon.
   *
   * If during the initialization a failure occurred, this function does not
   * perform cleanup. The caller must call vmballoon_compaction_deinit() in this
   * case.
   *
   * Return: zero on success or error code on failure.
   */
  static __init int vmballoon_compaction_init(struct vmballoon *b)
  {
  	vmballoon_mnt = kern_mount(&vmballoon_fs);
  	if (IS_ERR(vmballoon_mnt))
  		return PTR_ERR(vmballoon_mnt);
  
  	b->b_dev_info.migratepage = vmballoon_migratepage;
  	b->b_dev_info.inode = alloc_anon_inode(vmballoon_mnt->mnt_sb);
  
  	if (IS_ERR(b->b_dev_info.inode))
  		return PTR_ERR(b->b_dev_info.inode);
  
  	b->b_dev_info.inode->i_mapping->a_ops = &balloon_aops;
  	return 0;
  }
  
  #else /* CONFIG_BALLOON_COMPACTION */
  
  static void vmballoon_compaction_deinit(struct vmballoon *b)
  {
  }
  
  static int vmballoon_compaction_init(struct vmballoon *b)
  {
  	return 0;
  }
  
  #endif /* CONFIG_BALLOON_COMPACTION */
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1860
1861
1862
  static int __init vmballoon_init(void)
  {
  	int error;
6e4453b32   Nadav Amit   vmw_balloon: rewo...
1863

453dc6593   Dmitry Torokhov   VMware Balloon dr...
1864
1865
1866
1867
  	/*
  	 * Check if we are running on VMware's hypervisor and bail out
  	 * if we are not.
  	 */
03b2a320b   Juergen Gross   x86/virt: Add enu...
1868
  	if (x86_hyper_type != X86_HYPER_VMWARE)
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1869
  		return -ENODEV;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1870
  	INIT_DELAYED_WORK(&balloon.dwork, vmballoon_work);
5d1a86ecf   Nadav Amit   vmw_balloon: Add ...
1871
1872
1873
  	error = vmballoon_register_shrinker(&balloon);
  	if (error)
  		goto fail;
83a8afa72   Nadav Amit   vmw_balloon: Comp...
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
  	/*
  	 * Initialization of compaction must be done after the call to
  	 * balloon_devinfo_init() .
  	 */
  	balloon_devinfo_init(&balloon.b_dev_info);
  	error = vmballoon_compaction_init(&balloon);
  	if (error)
  		goto fail;
  
  	INIT_LIST_HEAD(&balloon.huge_pages);
6e4453b32   Nadav Amit   vmw_balloon: rewo...
1884
  	spin_lock_init(&balloon.comm_lock);
c7b3690fb   Nadav Amit   vmw_balloon: stat...
1885
  	init_rwsem(&balloon.conf_sem);
48e3d668b   Philip P. Moltmann   VMware balloon: E...
1886
  	balloon.vmci_doorbell = VMCI_INVALID_HANDLE;
d7568c130   Philip P. Moltmann   VMware balloon: T...
1887
1888
1889
  	balloon.batch_page = NULL;
  	balloon.page = NULL;
  	balloon.reset_required = true;
beda94da3   Dmitry Torokhov   Vmware balloon: s...
1890
  	queue_delayed_work(system_freezable_wq, &balloon.dwork, 0);
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1891

f632a8170   Linus Torvalds   Merge tag 'driver...
1892
  	vmballoon_debugfs_init(&balloon);
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1893
  	return 0;
83a8afa72   Nadav Amit   vmw_balloon: Comp...
1894
  fail:
5d1a86ecf   Nadav Amit   vmw_balloon: Add ...
1895
  	vmballoon_unregister_shrinker(&balloon);
83a8afa72   Nadav Amit   vmw_balloon: Comp...
1896
1897
  	vmballoon_compaction_deinit(&balloon);
  	return error;
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1898
  }
c3cc1b0fc   Nadav Amit   vmw_balloon: fix ...
1899
1900
1901
1902
1903
1904
1905
1906
  
  /*
   * Using late_initcall() instead of module_init() allows the balloon to use the
   * VMCI doorbell even when the balloon is built into the kernel. Otherwise the
   * VMCI is probed only after the balloon is initialized. If the balloon is used
   * as a module, late_initcall() is equivalent to module_init().
   */
  late_initcall(vmballoon_init);
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1907
1908
1909
  
  static void __exit vmballoon_exit(void)
  {
5d1a86ecf   Nadav Amit   vmw_balloon: Add ...
1910
  	vmballoon_unregister_shrinker(&balloon);
48e3d668b   Philip P. Moltmann   VMware balloon: E...
1911
  	vmballoon_vmci_cleanup(&balloon);
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1912
  	cancel_delayed_work_sync(&balloon.dwork);
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1913
1914
1915
1916
1917
1918
1919
1920
  
  	vmballoon_debugfs_exit(&balloon);
  
  	/*
  	 * Deallocate all reserved memory, and reset connection with monitor.
  	 * Reset connection before deallocating memory to avoid potential for
  	 * additional spurious resets from guest touching deallocated pages.
  	 */
d7568c130   Philip P. Moltmann   VMware balloon: T...
1921
  	vmballoon_send_start(&balloon, 0);
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1922
  	vmballoon_pop(&balloon);
83a8afa72   Nadav Amit   vmw_balloon: Comp...
1923
1924
1925
  
  	/* Only once we popped the balloon, compaction can be deinit */
  	vmballoon_compaction_deinit(&balloon);
453dc6593   Dmitry Torokhov   VMware Balloon dr...
1926
1927
  }
  module_exit(vmballoon_exit);