blob: b0b6f99c8f0fc99d5e34705ba8bebb0a331568fc [file] [log] [blame]
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001/*
2 * VMware Balloon driver.
3 *
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07004 * Copyright (C) 2000-2014, VMware, Inc. All Rights Reserved.
Dmitry Torokhov453dc652010-04-23 13:18:08 -04005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; version 2 of the License and no later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more
14 * details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
Dmitry Torokhov73b35d02014-06-20 10:14:58 -070020 * Maintained by: Xavier Deguillard <xdeguillard@vmware.com>
21 * Philip Moltmann <moltmann@vmware.com>
Dmitry Torokhov453dc652010-04-23 13:18:08 -040022 */
23
24/*
25 * This is VMware physical memory management driver for Linux. The driver
26 * acts like a "balloon" that can be inflated to reclaim physical pages by
27 * reserving them in the guest and invalidating them in the monitor,
28 * freeing up the underlying machine pages so they can be allocated to
29 * other guests. The balloon can also be deflated to allow the guest to
30 * use more physical memory. Higher level policies can control the sizes
31 * of balloons in VMs in order to manage physical memory resources.
32 */
33
34//#define DEBUG
35#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36
37#include <linux/types.h>
38#include <linux/kernel.h>
39#include <linux/mm.h>
Xavier Deguillardf220a802015-08-06 15:17:58 -070040#include <linux/vmalloc.h>
Dmitry Torokhov453dc652010-04-23 13:18:08 -040041#include <linux/sched.h>
42#include <linux/module.h>
43#include <linux/workqueue.h>
44#include <linux/debugfs.h>
45#include <linux/seq_file.h>
Philip P. Moltmann48e3d662015-08-06 15:18:01 -070046#include <linux/vmw_vmci_defs.h>
47#include <linux/vmw_vmci_api.h>
H. Peter Anvina10a5692010-05-09 01:13:42 -070048#include <asm/hypervisor.h>
Dmitry Torokhov453dc652010-04-23 13:18:08 -040049
50MODULE_AUTHOR("VMware, Inc.");
51MODULE_DESCRIPTION("VMware Memory Control (Balloon) Driver");
Philip P. Moltmann48e3d662015-08-06 15:18:01 -070052MODULE_VERSION("1.5.0.0-k");
Dmitry Torokhov453dc652010-04-23 13:18:08 -040053MODULE_ALIAS("dmi:*:svnVMware*:*");
54MODULE_ALIAS("vmware_vmmemctl");
55MODULE_LICENSE("GPL");
56
57/*
58 * Various constants controlling rate of inflaint/deflating balloon,
59 * measured in pages.
60 */
61
62/*
Dmitry Torokhov453dc652010-04-23 13:18:08 -040063 * Rates of memory allocaton when guest experiences memory pressure
64 * (driver performs sleeping allocations).
65 */
66#define VMW_BALLOON_RATE_ALLOC_MIN 512U
67#define VMW_BALLOON_RATE_ALLOC_MAX 2048U
68#define VMW_BALLOON_RATE_ALLOC_INC 16U
69
70/*
Dmitry Torokhov453dc652010-04-23 13:18:08 -040071 * When guest is under memory pressure, use a reduced page allocation
72 * rate for next several cycles.
73 */
74#define VMW_BALLOON_SLOW_CYCLES 4
75
76/*
77 * Use __GFP_HIGHMEM to allow pages from HIGHMEM zone. We don't
Mel Gorman71baba42015-11-06 16:28:28 -080078 * allow wait (__GFP_RECLAIM) for NOSLEEP page allocations. Use
Dmitry Torokhov453dc652010-04-23 13:18:08 -040079 * __GFP_NOWARN, to suppress page allocation failure warnings.
80 */
81#define VMW_PAGE_ALLOC_NOSLEEP (__GFP_HIGHMEM|__GFP_NOWARN)
82
83/*
84 * Use GFP_HIGHUSER when executing in a separate kernel thread
85 * context and allocation can sleep. This is less stressful to
86 * the guest memory system, since it allows the thread to block
87 * while memory is reclaimed, and won't take pages from emergency
88 * low-memory pools.
89 */
90#define VMW_PAGE_ALLOC_CANSLEEP (GFP_HIGHUSER)
91
Dmitry Torokhov55adaa42010-06-04 14:14:52 -070092/* Maximum number of refused pages we accumulate during inflation cycle */
93#define VMW_BALLOON_MAX_REFUSED 16
Dmitry Torokhov453dc652010-04-23 13:18:08 -040094
95/*
96 * Hypervisor communication port definitions.
97 */
98#define VMW_BALLOON_HV_PORT 0x5670
99#define VMW_BALLOON_HV_MAGIC 0x456c6d6f
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400100#define VMW_BALLOON_GUEST_ID 1 /* Linux */
101
Xavier Deguillardeb79100f2015-06-12 11:43:23 -0700102enum vmwballoon_capabilities {
103 /*
104 * Bit 0 is reserved and not associated to any capability.
105 */
Philip P. Moltmann48e3d662015-08-06 15:18:01 -0700106 VMW_BALLOON_BASIC_CMDS = (1 << 1),
107 VMW_BALLOON_BATCHED_CMDS = (1 << 2),
108 VMW_BALLOON_BATCHED_2M_CMDS = (1 << 3),
109 VMW_BALLOON_SIGNALLED_WAKEUP_CMD = (1 << 4),
Xavier Deguillardeb79100f2015-06-12 11:43:23 -0700110};
111
Xavier Deguillardf220a802015-08-06 15:17:58 -0700112#define VMW_BALLOON_CAPABILITIES (VMW_BALLOON_BASIC_CMDS \
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700113 | VMW_BALLOON_BATCHED_CMDS \
Philip P. Moltmann48e3d662015-08-06 15:18:01 -0700114 | VMW_BALLOON_BATCHED_2M_CMDS \
115 | VMW_BALLOON_SIGNALLED_WAKEUP_CMD)
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700116
117#define VMW_BALLOON_2M_SHIFT (9)
118#define VMW_BALLOON_NUM_PAGE_SIZES (2)
Xavier Deguillardeb79100f2015-06-12 11:43:23 -0700119
Xavier Deguillardf220a802015-08-06 15:17:58 -0700120/*
121 * Backdoor commands availability:
122 *
123 * START, GET_TARGET and GUEST_ID are always available,
124 *
125 * VMW_BALLOON_BASIC_CMDS:
126 * LOCK and UNLOCK commands,
127 * VMW_BALLOON_BATCHED_CMDS:
128 * BATCHED_LOCK and BATCHED_UNLOCK commands.
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700129 * VMW BALLOON_BATCHED_2M_CMDS:
Philip P. Moltmann48e3d662015-08-06 15:18:01 -0700130 * BATCHED_2M_LOCK and BATCHED_2M_UNLOCK commands,
131 * VMW VMW_BALLOON_SIGNALLED_WAKEUP_CMD:
132 * VMW_BALLOON_CMD_VMCI_DOORBELL_SET command.
Xavier Deguillardf220a802015-08-06 15:17:58 -0700133 */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700134#define VMW_BALLOON_CMD_START 0
135#define VMW_BALLOON_CMD_GET_TARGET 1
136#define VMW_BALLOON_CMD_LOCK 2
137#define VMW_BALLOON_CMD_UNLOCK 3
138#define VMW_BALLOON_CMD_GUEST_ID 4
139#define VMW_BALLOON_CMD_BATCHED_LOCK 6
140#define VMW_BALLOON_CMD_BATCHED_UNLOCK 7
141#define VMW_BALLOON_CMD_BATCHED_2M_LOCK 8
142#define VMW_BALLOON_CMD_BATCHED_2M_UNLOCK 9
Philip P. Moltmann48e3d662015-08-06 15:18:01 -0700143#define VMW_BALLOON_CMD_VMCI_DOORBELL_SET 10
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700144
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400145
146/* error codes */
Xavier Deguillardeb79100f2015-06-12 11:43:23 -0700147#define VMW_BALLOON_SUCCESS 0
148#define VMW_BALLOON_FAILURE -1
149#define VMW_BALLOON_ERROR_CMD_INVALID 1
150#define VMW_BALLOON_ERROR_PPN_INVALID 2
151#define VMW_BALLOON_ERROR_PPN_LOCKED 3
152#define VMW_BALLOON_ERROR_PPN_UNLOCKED 4
153#define VMW_BALLOON_ERROR_PPN_PINNED 5
154#define VMW_BALLOON_ERROR_PPN_NOTNEEDED 6
155#define VMW_BALLOON_ERROR_RESET 7
156#define VMW_BALLOON_ERROR_BUSY 8
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400157
Xavier Deguillardeb79100f2015-06-12 11:43:23 -0700158#define VMW_BALLOON_SUCCESS_WITH_CAPABILITIES (0x03000000)
159
Xavier Deguillardf220a802015-08-06 15:17:58 -0700160/* Batch page description */
161
162/*
163 * Layout of a page in the batch page:
164 *
165 * +-------------+----------+--------+
166 * | | | |
167 * | Page number | Reserved | Status |
168 * | | | |
169 * +-------------+----------+--------+
170 * 64 PAGE_SHIFT 6 0
171 *
Xavier Deguillardf220a802015-08-06 15:17:58 -0700172 * The reserved field should be set to 0.
173 */
174#define VMW_BALLOON_BATCH_MAX_PAGES (PAGE_SIZE / sizeof(u64))
175#define VMW_BALLOON_BATCH_STATUS_MASK ((1UL << 5) - 1)
176#define VMW_BALLOON_BATCH_PAGE_MASK (~((1UL << PAGE_SHIFT) - 1))
177
178struct vmballoon_batch_page {
179 u64 pages[VMW_BALLOON_BATCH_MAX_PAGES];
180};
181
182static u64 vmballoon_batch_get_pa(struct vmballoon_batch_page *batch, int idx)
183{
184 return batch->pages[idx] & VMW_BALLOON_BATCH_PAGE_MASK;
185}
186
187static int vmballoon_batch_get_status(struct vmballoon_batch_page *batch,
188 int idx)
189{
190 return (int)(batch->pages[idx] & VMW_BALLOON_BATCH_STATUS_MASK);
191}
192
193static void vmballoon_batch_set_pa(struct vmballoon_batch_page *batch, int idx,
194 u64 pa)
195{
196 batch->pages[idx] = pa;
197}
198
199
200#define VMWARE_BALLOON_CMD(cmd, arg1, arg2, result) \
Xavier Deguillardeb79100f2015-06-12 11:43:23 -0700201({ \
Xavier Deguillardf220a802015-08-06 15:17:58 -0700202 unsigned long __status, __dummy1, __dummy2, __dummy3; \
Xavier Deguillardeb79100f2015-06-12 11:43:23 -0700203 __asm__ __volatile__ ("inl %%dx" : \
204 "=a"(__status), \
205 "=c"(__dummy1), \
206 "=d"(__dummy2), \
Xavier Deguillardf220a802015-08-06 15:17:58 -0700207 "=b"(result), \
208 "=S" (__dummy3) : \
Xavier Deguillardeb79100f2015-06-12 11:43:23 -0700209 "0"(VMW_BALLOON_HV_MAGIC), \
210 "1"(VMW_BALLOON_CMD_##cmd), \
211 "2"(VMW_BALLOON_HV_PORT), \
Xavier Deguillardf220a802015-08-06 15:17:58 -0700212 "3"(arg1), \
213 "4" (arg2) : \
Xavier Deguillardeb79100f2015-06-12 11:43:23 -0700214 "memory"); \
215 if (VMW_BALLOON_CMD_##cmd == VMW_BALLOON_CMD_START) \
216 result = __dummy1; \
217 result &= -1UL; \
218 __status & -1UL; \
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400219})
220
221#ifdef CONFIG_DEBUG_FS
222struct vmballoon_stats {
223 unsigned int timer;
Philip P. Moltmann48e3d662015-08-06 15:18:01 -0700224 unsigned int doorbell;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400225
Rakib Mullick2ca02df2011-11-02 13:40:07 -0700226 /* allocation statistics */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700227 unsigned int alloc[VMW_BALLOON_NUM_PAGE_SIZES];
228 unsigned int alloc_fail[VMW_BALLOON_NUM_PAGE_SIZES];
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400229 unsigned int sleep_alloc;
230 unsigned int sleep_alloc_fail;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700231 unsigned int refused_alloc[VMW_BALLOON_NUM_PAGE_SIZES];
232 unsigned int refused_free[VMW_BALLOON_NUM_PAGE_SIZES];
233 unsigned int free[VMW_BALLOON_NUM_PAGE_SIZES];
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400234
235 /* monitor operations */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700236 unsigned int lock[VMW_BALLOON_NUM_PAGE_SIZES];
237 unsigned int lock_fail[VMW_BALLOON_NUM_PAGE_SIZES];
238 unsigned int unlock[VMW_BALLOON_NUM_PAGE_SIZES];
239 unsigned int unlock_fail[VMW_BALLOON_NUM_PAGE_SIZES];
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400240 unsigned int target;
241 unsigned int target_fail;
242 unsigned int start;
243 unsigned int start_fail;
244 unsigned int guest_type;
245 unsigned int guest_type_fail;
Philip P. Moltmann48e3d662015-08-06 15:18:01 -0700246 unsigned int doorbell_set;
247 unsigned int doorbell_unset;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400248};
249
250#define STATS_INC(stat) (stat)++
251#else
252#define STATS_INC(stat)
253#endif
254
Xavier Deguillardf220a802015-08-06 15:17:58 -0700255struct vmballoon;
256
257struct vmballoon_ops {
258 void (*add_page)(struct vmballoon *b, int idx, struct page *p);
Xavier Deguillard4670de42015-08-06 15:17:59 -0700259 int (*lock)(struct vmballoon *b, unsigned int num_pages,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700260 bool is_2m_pages, unsigned int *target);
Xavier Deguillard4670de42015-08-06 15:17:59 -0700261 int (*unlock)(struct vmballoon *b, unsigned int num_pages,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700262 bool is_2m_pages, unsigned int *target);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700263};
264
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700265struct vmballoon_page_size {
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400266 /* list of reserved physical pages */
267 struct list_head pages;
268
269 /* transient list of non-balloonable pages */
270 struct list_head refused_pages;
Dmitry Torokhov55adaa42010-06-04 14:14:52 -0700271 unsigned int n_refused_pages;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700272};
273
274struct vmballoon {
275 struct vmballoon_page_size page_sizes[VMW_BALLOON_NUM_PAGE_SIZES];
276
277 /* supported page sizes. 1 == 4k pages only, 2 == 4k and 2m pages */
278 unsigned supported_page_sizes;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400279
280 /* balloon size in pages */
281 unsigned int size;
282 unsigned int target;
283
284 /* reset flag */
285 bool reset_required;
286
287 /* adjustment rates (pages per second) */
288 unsigned int rate_alloc;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400289
290 /* slowdown page allocations for next few cycles */
291 unsigned int slow_allocation_cycles;
292
Xavier Deguillardf220a802015-08-06 15:17:58 -0700293 unsigned long capabilities;
294
295 struct vmballoon_batch_page *batch_page;
296 unsigned int batch_max_pages;
297 struct page *page;
298
299 const struct vmballoon_ops *ops;
300
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400301#ifdef CONFIG_DEBUG_FS
302 /* statistics */
303 struct vmballoon_stats stats;
304
305 /* debugfs file exporting statistics */
306 struct dentry *dbg_entry;
307#endif
308
309 struct sysinfo sysinfo;
310
311 struct delayed_work dwork;
Philip P. Moltmann48e3d662015-08-06 15:18:01 -0700312
313 struct vmci_handle vmci_doorbell;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400314};
315
316static struct vmballoon balloon;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400317
318/*
319 * Send "start" command to the host, communicating supported version
320 * of the protocol.
321 */
Xavier Deguillardf220a802015-08-06 15:17:58 -0700322static bool vmballoon_send_start(struct vmballoon *b, unsigned long req_caps)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400323{
Xavier Deguillardf220a802015-08-06 15:17:58 -0700324 unsigned long status, capabilities, dummy = 0;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700325 bool success;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400326
327 STATS_INC(b->stats.start);
328
Xavier Deguillardf220a802015-08-06 15:17:58 -0700329 status = VMWARE_BALLOON_CMD(START, req_caps, dummy, capabilities);
330
331 switch (status) {
332 case VMW_BALLOON_SUCCESS_WITH_CAPABILITIES:
333 b->capabilities = capabilities;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700334 success = true;
335 break;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700336 case VMW_BALLOON_SUCCESS:
337 b->capabilities = VMW_BALLOON_BASIC_CMDS;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700338 success = true;
339 break;
340 default:
341 success = false;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700342 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400343
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700344 if (b->capabilities & VMW_BALLOON_BATCHED_2M_CMDS)
345 b->supported_page_sizes = 2;
346 else
347 b->supported_page_sizes = 1;
348
349 if (!success) {
350 pr_debug("%s - failed, hv returns %ld\n", __func__, status);
351 STATS_INC(b->stats.start_fail);
352 }
353 return success;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400354}
355
356static bool vmballoon_check_status(struct vmballoon *b, unsigned long status)
357{
358 switch (status) {
359 case VMW_BALLOON_SUCCESS:
360 return true;
361
362 case VMW_BALLOON_ERROR_RESET:
363 b->reset_required = true;
364 /* fall through */
365
366 default:
367 return false;
368 }
369}
370
371/*
372 * Communicate guest type to the host so that it can adjust ballooning
373 * algorithm to the one most appropriate for the guest. This command
374 * is normally issued after sending "start" command and is part of
375 * standard reset sequence.
376 */
377static bool vmballoon_send_guest_id(struct vmballoon *b)
378{
Xavier Deguillardf220a802015-08-06 15:17:58 -0700379 unsigned long status, dummy = 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400380
Xavier Deguillardf220a802015-08-06 15:17:58 -0700381 status = VMWARE_BALLOON_CMD(GUEST_ID, VMW_BALLOON_GUEST_ID, dummy,
382 dummy);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400383
384 STATS_INC(b->stats.guest_type);
385
386 if (vmballoon_check_status(b, status))
387 return true;
388
389 pr_debug("%s - failed, hv returns %ld\n", __func__, status);
390 STATS_INC(b->stats.guest_type_fail);
391 return false;
392}
393
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700394static u16 vmballoon_page_size(bool is_2m_page)
395{
396 if (is_2m_page)
397 return 1 << VMW_BALLOON_2M_SHIFT;
398
399 return 1;
400}
401
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400402/*
403 * Retrieve desired balloon size from the host.
404 */
405static bool vmballoon_send_get_target(struct vmballoon *b, u32 *new_target)
406{
407 unsigned long status;
408 unsigned long target;
409 unsigned long limit;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700410 unsigned long dummy = 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400411 u32 limit32;
412
413 /*
414 * si_meminfo() is cheap. Moreover, we want to provide dynamic
415 * max balloon size later. So let us call si_meminfo() every
416 * iteration.
417 */
418 si_meminfo(&b->sysinfo);
419 limit = b->sysinfo.totalram;
420
421 /* Ensure limit fits in 32-bits */
422 limit32 = (u32)limit;
423 if (limit != limit32)
424 return false;
425
426 /* update stats */
427 STATS_INC(b->stats.target);
428
Xavier Deguillardf220a802015-08-06 15:17:58 -0700429 status = VMWARE_BALLOON_CMD(GET_TARGET, limit, dummy, target);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400430 if (vmballoon_check_status(b, status)) {
431 *new_target = target;
432 return true;
433 }
434
435 pr_debug("%s - failed, hv returns %ld\n", __func__, status);
436 STATS_INC(b->stats.target_fail);
437 return false;
438}
439
440/*
441 * Notify the host about allocated page so that host can use it without
442 * fear that guest will need it. Host may reject some pages, we need to
443 * check the return value and maybe submit a different page.
444 */
Danny Kukawka3e5ba462012-01-30 23:00:08 +0100445static int vmballoon_send_lock_page(struct vmballoon *b, unsigned long pfn,
Xavier Deguillard4670de42015-08-06 15:17:59 -0700446 unsigned int *hv_status, unsigned int *target)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400447{
Xavier Deguillardf220a802015-08-06 15:17:58 -0700448 unsigned long status, dummy = 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400449 u32 pfn32;
450
451 pfn32 = (u32)pfn;
452 if (pfn32 != pfn)
Nadav Amit0e0dd1a2018-06-19 16:00:24 -0700453 return -EINVAL;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400454
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700455 STATS_INC(b->stats.lock[false]);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400456
Xavier Deguillard4670de42015-08-06 15:17:59 -0700457 *hv_status = status = VMWARE_BALLOON_CMD(LOCK, pfn, dummy, *target);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400458 if (vmballoon_check_status(b, status))
Danny Kukawka3e5ba462012-01-30 23:00:08 +0100459 return 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400460
461 pr_debug("%s - ppn %lx, hv returns %ld\n", __func__, pfn, status);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700462 STATS_INC(b->stats.lock_fail[false]);
Nadav Amit0e0dd1a2018-06-19 16:00:24 -0700463 return -EIO;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400464}
465
Xavier Deguillardf220a802015-08-06 15:17:58 -0700466static int vmballoon_send_batched_lock(struct vmballoon *b,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700467 unsigned int num_pages, bool is_2m_pages, unsigned int *target)
Xavier Deguillardf220a802015-08-06 15:17:58 -0700468{
Xavier Deguillard4670de42015-08-06 15:17:59 -0700469 unsigned long status;
Nadav Amit63c003e2018-07-02 19:27:13 -0700470 unsigned long pfn = PHYS_PFN(virt_to_phys(b->batch_page));
Xavier Deguillardf220a802015-08-06 15:17:58 -0700471
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700472 STATS_INC(b->stats.lock[is_2m_pages]);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700473
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700474 if (is_2m_pages)
475 status = VMWARE_BALLOON_CMD(BATCHED_2M_LOCK, pfn, num_pages,
476 *target);
477 else
478 status = VMWARE_BALLOON_CMD(BATCHED_LOCK, pfn, num_pages,
479 *target);
480
Xavier Deguillardf220a802015-08-06 15:17:58 -0700481 if (vmballoon_check_status(b, status))
482 return 0;
483
484 pr_debug("%s - batch ppn %lx, hv returns %ld\n", __func__, pfn, status);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700485 STATS_INC(b->stats.lock_fail[is_2m_pages]);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700486 return 1;
487}
488
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400489/*
490 * Notify the host that guest intends to release given page back into
491 * the pool of available (to the guest) pages.
492 */
Xavier Deguillard4670de42015-08-06 15:17:59 -0700493static bool vmballoon_send_unlock_page(struct vmballoon *b, unsigned long pfn,
494 unsigned int *target)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400495{
Xavier Deguillardf220a802015-08-06 15:17:58 -0700496 unsigned long status, dummy = 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400497 u32 pfn32;
498
499 pfn32 = (u32)pfn;
500 if (pfn32 != pfn)
501 return false;
502
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700503 STATS_INC(b->stats.unlock[false]);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400504
Xavier Deguillard4670de42015-08-06 15:17:59 -0700505 status = VMWARE_BALLOON_CMD(UNLOCK, pfn, dummy, *target);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400506 if (vmballoon_check_status(b, status))
507 return true;
508
509 pr_debug("%s - ppn %lx, hv returns %ld\n", __func__, pfn, status);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700510 STATS_INC(b->stats.unlock_fail[false]);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400511 return false;
512}
513
Xavier Deguillardf220a802015-08-06 15:17:58 -0700514static bool vmballoon_send_batched_unlock(struct vmballoon *b,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700515 unsigned int num_pages, bool is_2m_pages, unsigned int *target)
Xavier Deguillardf220a802015-08-06 15:17:58 -0700516{
Xavier Deguillard4670de42015-08-06 15:17:59 -0700517 unsigned long status;
Nadav Amit63c003e2018-07-02 19:27:13 -0700518 unsigned long pfn = PHYS_PFN(virt_to_phys(b->batch_page));
Xavier Deguillardf220a802015-08-06 15:17:58 -0700519
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700520 STATS_INC(b->stats.unlock[is_2m_pages]);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700521
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700522 if (is_2m_pages)
523 status = VMWARE_BALLOON_CMD(BATCHED_2M_UNLOCK, pfn, num_pages,
524 *target);
525 else
526 status = VMWARE_BALLOON_CMD(BATCHED_UNLOCK, pfn, num_pages,
527 *target);
528
Xavier Deguillardf220a802015-08-06 15:17:58 -0700529 if (vmballoon_check_status(b, status))
530 return true;
531
532 pr_debug("%s - batch ppn %lx, hv returns %ld\n", __func__, pfn, status);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700533 STATS_INC(b->stats.unlock_fail[is_2m_pages]);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700534 return false;
535}
536
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700537static struct page *vmballoon_alloc_page(gfp_t flags, bool is_2m_page)
538{
539 if (is_2m_page)
540 return alloc_pages(flags, VMW_BALLOON_2M_SHIFT);
541
542 return alloc_page(flags);
543}
544
545static void vmballoon_free_page(struct page *page, bool is_2m_page)
546{
547 if (is_2m_page)
548 __free_pages(page, VMW_BALLOON_2M_SHIFT);
549 else
550 __free_page(page);
551}
552
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400553/*
554 * Quickly release all pages allocated for the balloon. This function is
555 * called when host decides to "reset" balloon for one reason or another.
556 * Unlike normal "deflate" we do not (shall not) notify host of the pages
557 * being released.
558 */
559static void vmballoon_pop(struct vmballoon *b)
560{
561 struct page *page, *next;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700562 unsigned is_2m_pages;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400563
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700564 for (is_2m_pages = 0; is_2m_pages < VMW_BALLOON_NUM_PAGE_SIZES;
565 is_2m_pages++) {
566 struct vmballoon_page_size *page_size =
567 &b->page_sizes[is_2m_pages];
568 u16 size_per_page = vmballoon_page_size(is_2m_pages);
569
570 list_for_each_entry_safe(page, next, &page_size->pages, lru) {
571 list_del(&page->lru);
572 vmballoon_free_page(page, is_2m_pages);
573 STATS_INC(b->stats.free[is_2m_pages]);
574 b->size -= size_per_page;
575 cond_resched();
576 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400577 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400578
Gil Kupferd9bc59c2018-06-01 00:47:47 -0700579 /* Clearing the batch_page unconditionally has no adverse effect */
580 free_page((unsigned long)b->batch_page);
581 b->batch_page = NULL;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400582}
583
584/*
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700585 * Notify the host of a ballooned page. If host rejects the page put it on the
586 * refuse list, those refused page are then released at the end of the
587 * inflation cycle.
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400588 */
Xavier Deguillard4670de42015-08-06 15:17:59 -0700589static int vmballoon_lock_page(struct vmballoon *b, unsigned int num_pages,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700590 bool is_2m_pages, unsigned int *target)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400591{
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700592 int locked, hv_status;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700593 struct page *page = b->page;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700594 struct vmballoon_page_size *page_size = &b->page_sizes[false];
595
596 /* is_2m_pages can never happen as 2m pages support implies batching */
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400597
Xavier Deguillard4670de42015-08-06 15:17:59 -0700598 locked = vmballoon_send_lock_page(b, page_to_pfn(page), &hv_status,
599 target);
Nadav Amit0e0dd1a2018-06-19 16:00:24 -0700600 if (locked) {
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700601 STATS_INC(b->stats.refused_alloc[false]);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400602
Nadav Amit0e0dd1a2018-06-19 16:00:24 -0700603 if (locked == -EIO &&
604 (hv_status == VMW_BALLOON_ERROR_RESET ||
605 hv_status == VMW_BALLOON_ERROR_PPN_NOTNEEDED)) {
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700606 vmballoon_free_page(page, false);
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700607 return -EIO;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400608 }
609
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700610 /*
611 * Place page on the list of non-balloonable pages
612 * and retry allocation, unless we already accumulated
613 * too many of them, in which case take a breather.
614 */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700615 if (page_size->n_refused_pages < VMW_BALLOON_MAX_REFUSED) {
616 page_size->n_refused_pages++;
617 list_add(&page->lru, &page_size->refused_pages);
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700618 } else {
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700619 vmballoon_free_page(page, false);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400620 }
Nadav Amit0e0dd1a2018-06-19 16:00:24 -0700621 return locked;
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700622 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400623
624 /* track allocated page */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700625 list_add(&page->lru, &page_size->pages);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400626
627 /* update balloon size */
628 b->size++;
629
630 return 0;
631}
632
Xavier Deguillardf220a802015-08-06 15:17:58 -0700633static int vmballoon_lock_batched_page(struct vmballoon *b,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700634 unsigned int num_pages, bool is_2m_pages, unsigned int *target)
Xavier Deguillardf220a802015-08-06 15:17:58 -0700635{
636 int locked, i;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700637 u16 size_per_page = vmballoon_page_size(is_2m_pages);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700638
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700639 locked = vmballoon_send_batched_lock(b, num_pages, is_2m_pages,
640 target);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700641 if (locked > 0) {
642 for (i = 0; i < num_pages; i++) {
643 u64 pa = vmballoon_batch_get_pa(b->batch_page, i);
644 struct page *p = pfn_to_page(pa >> PAGE_SHIFT);
645
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700646 vmballoon_free_page(p, is_2m_pages);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700647 }
648
649 return -EIO;
650 }
651
652 for (i = 0; i < num_pages; i++) {
653 u64 pa = vmballoon_batch_get_pa(b->batch_page, i);
654 struct page *p = pfn_to_page(pa >> PAGE_SHIFT);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700655 struct vmballoon_page_size *page_size =
656 &b->page_sizes[is_2m_pages];
Xavier Deguillardf220a802015-08-06 15:17:58 -0700657
658 locked = vmballoon_batch_get_status(b->batch_page, i);
659
660 switch (locked) {
661 case VMW_BALLOON_SUCCESS:
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700662 list_add(&p->lru, &page_size->pages);
663 b->size += size_per_page;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700664 break;
665 case VMW_BALLOON_ERROR_PPN_PINNED:
666 case VMW_BALLOON_ERROR_PPN_INVALID:
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700667 if (page_size->n_refused_pages
668 < VMW_BALLOON_MAX_REFUSED) {
669 list_add(&p->lru, &page_size->refused_pages);
670 page_size->n_refused_pages++;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700671 break;
672 }
673 /* Fallthrough */
674 case VMW_BALLOON_ERROR_RESET:
675 case VMW_BALLOON_ERROR_PPN_NOTNEEDED:
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700676 vmballoon_free_page(p, is_2m_pages);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700677 break;
678 default:
679 /* This should never happen */
680 WARN_ON_ONCE(true);
681 }
682 }
683
684 return 0;
685}
686
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400687/*
688 * Release the page allocated for the balloon. Note that we first notify
689 * the host so it can make sure the page will be available for the guest
690 * to use, if needed.
691 */
Xavier Deguillard4670de42015-08-06 15:17:59 -0700692static int vmballoon_unlock_page(struct vmballoon *b, unsigned int num_pages,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700693 bool is_2m_pages, unsigned int *target)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400694{
Xavier Deguillardf220a802015-08-06 15:17:58 -0700695 struct page *page = b->page;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700696 struct vmballoon_page_size *page_size = &b->page_sizes[false];
697
698 /* is_2m_pages can never happen as 2m pages support implies batching */
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400699
Xavier Deguillard4670de42015-08-06 15:17:59 -0700700 if (!vmballoon_send_unlock_page(b, page_to_pfn(page), target)) {
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700701 list_add(&page->lru, &page_size->pages);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700702 return -EIO;
703 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400704
705 /* deallocate page */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700706 vmballoon_free_page(page, false);
707 STATS_INC(b->stats.free[false]);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400708
709 /* update balloon size */
710 b->size--;
711
712 return 0;
713}
714
Xavier Deguillardf220a802015-08-06 15:17:58 -0700715static int vmballoon_unlock_batched_page(struct vmballoon *b,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700716 unsigned int num_pages, bool is_2m_pages,
717 unsigned int *target)
Xavier Deguillardf220a802015-08-06 15:17:58 -0700718{
719 int locked, i, ret = 0;
720 bool hv_success;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700721 u16 size_per_page = vmballoon_page_size(is_2m_pages);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700722
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700723 hv_success = vmballoon_send_batched_unlock(b, num_pages, is_2m_pages,
724 target);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700725 if (!hv_success)
726 ret = -EIO;
727
728 for (i = 0; i < num_pages; i++) {
729 u64 pa = vmballoon_batch_get_pa(b->batch_page, i);
730 struct page *p = pfn_to_page(pa >> PAGE_SHIFT);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700731 struct vmballoon_page_size *page_size =
732 &b->page_sizes[is_2m_pages];
Xavier Deguillardf220a802015-08-06 15:17:58 -0700733
734 locked = vmballoon_batch_get_status(b->batch_page, i);
735 if (!hv_success || locked != VMW_BALLOON_SUCCESS) {
736 /*
737 * That page wasn't successfully unlocked by the
738 * hypervisor, re-add it to the list of pages owned by
739 * the balloon driver.
740 */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700741 list_add(&p->lru, &page_size->pages);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700742 } else {
743 /* deallocate page */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700744 vmballoon_free_page(p, is_2m_pages);
745 STATS_INC(b->stats.free[is_2m_pages]);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700746
747 /* update balloon size */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700748 b->size -= size_per_page;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700749 }
750 }
751
752 return ret;
753}
754
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400755/*
756 * Release pages that were allocated while attempting to inflate the
757 * balloon but were refused by the host for one reason or another.
758 */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700759static void vmballoon_release_refused_pages(struct vmballoon *b,
760 bool is_2m_pages)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400761{
762 struct page *page, *next;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700763 struct vmballoon_page_size *page_size =
764 &b->page_sizes[is_2m_pages];
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400765
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700766 list_for_each_entry_safe(page, next, &page_size->refused_pages, lru) {
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400767 list_del(&page->lru);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700768 vmballoon_free_page(page, is_2m_pages);
769 STATS_INC(b->stats.refused_free[is_2m_pages]);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400770 }
Dmitry Torokhov55adaa42010-06-04 14:14:52 -0700771
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700772 page_size->n_refused_pages = 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400773}
774
Xavier Deguillardf220a802015-08-06 15:17:58 -0700775static void vmballoon_add_page(struct vmballoon *b, int idx, struct page *p)
776{
777 b->page = p;
778}
779
780static void vmballoon_add_batched_page(struct vmballoon *b, int idx,
781 struct page *p)
782{
783 vmballoon_batch_set_pa(b->batch_page, idx,
784 (u64)page_to_pfn(p) << PAGE_SHIFT);
785}
786
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400787/*
788 * Inflate the balloon towards its target size. Note that we try to limit
789 * the rate of allocation to make sure we are not choking the rest of the
790 * system.
791 */
792static void vmballoon_inflate(struct vmballoon *b)
793{
Philip P. Moltmann33d268e2015-08-06 15:18:01 -0700794 unsigned rate;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400795 unsigned int allocations = 0;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700796 unsigned int num_pages = 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400797 int error = 0;
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700798 gfp_t flags = VMW_PAGE_ALLOC_NOSLEEP;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700799 bool is_2m_pages;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400800
801 pr_debug("%s - size: %d, target %d\n", __func__, b->size, b->target);
802
803 /*
804 * First try NOSLEEP page allocations to inflate balloon.
805 *
806 * If we do not throttle nosleep allocations, we can drain all
807 * free pages in the guest quickly (if the balloon target is high).
808 * As a side-effect, draining free pages helps to inform (force)
809 * the guest to start swapping if balloon target is not met yet,
810 * which is a desired behavior. However, balloon driver can consume
811 * all available CPU cycles if too many pages are allocated in a
812 * second. Therefore, we throttle nosleep allocations even when
813 * the guest is not under memory pressure. OTOH, if we have already
814 * predicted that the guest is under memory pressure, then we
815 * slowdown page allocations considerably.
816 */
817
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400818 /*
819 * Start with no sleep allocation rate which may be higher
820 * than sleeping allocation rate.
821 */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700822 if (b->slow_allocation_cycles) {
823 rate = b->rate_alloc;
824 is_2m_pages = false;
825 } else {
826 rate = UINT_MAX;
827 is_2m_pages =
828 b->supported_page_sizes == VMW_BALLOON_NUM_PAGE_SIZES;
829 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400830
Philip P. Moltmann33d268e2015-08-06 15:18:01 -0700831 pr_debug("%s - goal: %d, no-sleep rate: %u, sleep rate: %d\n",
Xavier Deguillard4670de42015-08-06 15:17:59 -0700832 __func__, b->target - b->size, rate, b->rate_alloc);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400833
Philip P. Moltmann33d268e2015-08-06 15:18:01 -0700834 while (!b->reset_required &&
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700835 b->size + num_pages * vmballoon_page_size(is_2m_pages)
836 < b->target) {
Xavier Deguillard4670de42015-08-06 15:17:59 -0700837 struct page *page;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400838
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700839 if (flags == VMW_PAGE_ALLOC_NOSLEEP)
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700840 STATS_INC(b->stats.alloc[is_2m_pages]);
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700841 else
842 STATS_INC(b->stats.sleep_alloc);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400843
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700844 page = vmballoon_alloc_page(flags, is_2m_pages);
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700845 if (!page) {
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700846 STATS_INC(b->stats.alloc_fail[is_2m_pages]);
847
848 if (is_2m_pages) {
849 b->ops->lock(b, num_pages, true, &b->target);
850
851 /*
852 * ignore errors from locking as we now switch
853 * to 4k pages and we might get different
854 * errors.
855 */
856
857 num_pages = 0;
858 is_2m_pages = false;
859 continue;
860 }
861
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700862 if (flags == VMW_PAGE_ALLOC_CANSLEEP) {
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400863 /*
864 * CANSLEEP page allocation failed, so guest
865 * is under severe memory pressure. Quickly
866 * decrease allocation rate.
867 */
868 b->rate_alloc = max(b->rate_alloc / 2,
869 VMW_BALLOON_RATE_ALLOC_MIN);
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700870 STATS_INC(b->stats.sleep_alloc_fail);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400871 break;
872 }
873
874 /*
875 * NOSLEEP page allocation failed, so the guest is
876 * under memory pressure. Let us slow down page
877 * allocations for next few cycles so that the guest
878 * gets out of memory pressure. Also, if we already
879 * allocated b->rate_alloc pages, let's pause,
880 * otherwise switch to sleeping allocations.
881 */
882 b->slow_allocation_cycles = VMW_BALLOON_SLOW_CYCLES;
883
Xavier Deguillard4670de42015-08-06 15:17:59 -0700884 if (allocations >= b->rate_alloc)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400885 break;
886
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700887 flags = VMW_PAGE_ALLOC_CANSLEEP;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400888 /* Lower rate for sleeping allocations. */
889 rate = b->rate_alloc;
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700890 continue;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400891 }
892
Xavier Deguillardf220a802015-08-06 15:17:58 -0700893 b->ops->add_page(b, num_pages++, page);
894 if (num_pages == b->batch_max_pages) {
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700895 error = b->ops->lock(b, num_pages, is_2m_pages,
896 &b->target);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700897 num_pages = 0;
898 if (error)
899 break;
900 }
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700901
Philip P. Moltmann33d268e2015-08-06 15:18:01 -0700902 cond_resched();
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400903
Xavier Deguillard4670de42015-08-06 15:17:59 -0700904 if (allocations >= rate) {
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400905 /* We allocated enough pages, let's take a break. */
906 break;
907 }
908 }
909
Xavier Deguillardf220a802015-08-06 15:17:58 -0700910 if (num_pages > 0)
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700911 b->ops->lock(b, num_pages, is_2m_pages, &b->target);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700912
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400913 /*
914 * We reached our goal without failures so try increasing
915 * allocation rate.
916 */
Xavier Deguillard4670de42015-08-06 15:17:59 -0700917 if (error == 0 && allocations >= b->rate_alloc) {
918 unsigned int mult = allocations / b->rate_alloc;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400919
920 b->rate_alloc =
921 min(b->rate_alloc + mult * VMW_BALLOON_RATE_ALLOC_INC,
922 VMW_BALLOON_RATE_ALLOC_MAX);
923 }
924
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700925 vmballoon_release_refused_pages(b, true);
926 vmballoon_release_refused_pages(b, false);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400927}
928
929/*
930 * Decrease the size of the balloon allowing guest to use more memory.
931 */
932static void vmballoon_deflate(struct vmballoon *b)
933{
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700934 unsigned is_2m_pages;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400935
Philip P. Moltmann33d268e2015-08-06 15:18:01 -0700936 pr_debug("%s - size: %d, target %d\n", __func__, b->size, b->target);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400937
938 /* free pages to reach target */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700939 for (is_2m_pages = 0; is_2m_pages < b->supported_page_sizes;
940 is_2m_pages++) {
941 struct page *page, *next;
942 unsigned int num_pages = 0;
943 struct vmballoon_page_size *page_size =
944 &b->page_sizes[is_2m_pages];
Xavier Deguillardf220a802015-08-06 15:17:58 -0700945
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700946 list_for_each_entry_safe(page, next, &page_size->pages, lru) {
947 if (b->reset_required ||
948 (b->target > 0 &&
949 b->size - num_pages
950 * vmballoon_page_size(is_2m_pages)
951 < b->target + vmballoon_page_size(true)))
952 break;
Philip P. Moltmann33d268e2015-08-06 15:18:01 -0700953
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700954 list_del(&page->lru);
955 b->ops->add_page(b, num_pages++, page);
956
957 if (num_pages == b->batch_max_pages) {
958 int error;
959
960 error = b->ops->unlock(b, num_pages,
961 is_2m_pages, &b->target);
962 num_pages = 0;
963 if (error)
964 return;
965 }
966
967 cond_resched();
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400968 }
969
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700970 if (num_pages > 0)
971 b->ops->unlock(b, num_pages, is_2m_pages, &b->target);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400972 }
Xavier Deguillardf220a802015-08-06 15:17:58 -0700973}
974
975static const struct vmballoon_ops vmballoon_basic_ops = {
976 .add_page = vmballoon_add_page,
977 .lock = vmballoon_lock_page,
978 .unlock = vmballoon_unlock_page
979};
980
981static const struct vmballoon_ops vmballoon_batched_ops = {
982 .add_page = vmballoon_add_batched_page,
983 .lock = vmballoon_lock_batched_page,
984 .unlock = vmballoon_unlock_batched_page
985};
986
987static bool vmballoon_init_batching(struct vmballoon *b)
988{
Gil Kupferd9bc59c2018-06-01 00:47:47 -0700989 struct page *page;
990
991 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
992 if (!page)
Xavier Deguillardf220a802015-08-06 15:17:58 -0700993 return false;
994
Gil Kupferd9bc59c2018-06-01 00:47:47 -0700995 b->batch_page = page_address(page);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700996 return true;
997}
998
999/*
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001000 * Receive notification and resize balloon
1001 */
1002static void vmballoon_doorbell(void *client_data)
1003{
1004 struct vmballoon *b = client_data;
1005
1006 STATS_INC(b->stats.doorbell);
1007
1008 mod_delayed_work(system_freezable_wq, &b->dwork, 0);
1009}
1010
1011/*
1012 * Clean up vmci doorbell
1013 */
1014static void vmballoon_vmci_cleanup(struct vmballoon *b)
1015{
1016 int error;
1017
1018 VMWARE_BALLOON_CMD(VMCI_DOORBELL_SET, VMCI_INVALID_ID,
1019 VMCI_INVALID_ID, error);
1020 STATS_INC(b->stats.doorbell_unset);
1021
1022 if (!vmci_handle_is_invalid(b->vmci_doorbell)) {
1023 vmci_doorbell_destroy(b->vmci_doorbell);
1024 b->vmci_doorbell = VMCI_INVALID_HANDLE;
1025 }
1026}
1027
1028/*
1029 * Initialize vmci doorbell, to get notified as soon as balloon changes
1030 */
1031static int vmballoon_vmci_init(struct vmballoon *b)
1032{
1033 int error = 0;
1034
1035 if ((b->capabilities & VMW_BALLOON_SIGNALLED_WAKEUP_CMD) != 0) {
1036 error = vmci_doorbell_create(&b->vmci_doorbell,
1037 VMCI_FLAG_DELAYED_CB,
1038 VMCI_PRIVILEGE_FLAG_RESTRICTED,
1039 vmballoon_doorbell, b);
1040
1041 if (error == VMCI_SUCCESS) {
1042 VMWARE_BALLOON_CMD(VMCI_DOORBELL_SET,
1043 b->vmci_doorbell.context,
1044 b->vmci_doorbell.resource, error);
1045 STATS_INC(b->stats.doorbell_set);
1046 }
1047 }
1048
1049 if (error != 0) {
1050 vmballoon_vmci_cleanup(b);
1051
1052 return -EIO;
1053 }
1054
1055 return 0;
1056}
1057
1058/*
Xavier Deguillardf220a802015-08-06 15:17:58 -07001059 * Perform standard reset sequence by popping the balloon (in case it
1060 * is not empty) and then restarting protocol. This operation normally
1061 * happens when host responds with VMW_BALLOON_ERROR_RESET to a command.
1062 */
1063static void vmballoon_reset(struct vmballoon *b)
1064{
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001065 int error;
1066
1067 vmballoon_vmci_cleanup(b);
1068
Xavier Deguillardf220a802015-08-06 15:17:58 -07001069 /* free all pages, skipping monitor unlock */
1070 vmballoon_pop(b);
1071
1072 if (!vmballoon_send_start(b, VMW_BALLOON_CAPABILITIES))
1073 return;
1074
1075 if ((b->capabilities & VMW_BALLOON_BATCHED_CMDS) != 0) {
1076 b->ops = &vmballoon_batched_ops;
1077 b->batch_max_pages = VMW_BALLOON_BATCH_MAX_PAGES;
1078 if (!vmballoon_init_batching(b)) {
1079 /*
1080 * We failed to initialize batching, inform the monitor
1081 * about it by sending a null capability.
1082 *
1083 * The guest will retry in one second.
1084 */
1085 vmballoon_send_start(b, 0);
1086 return;
1087 }
1088 } else if ((b->capabilities & VMW_BALLOON_BASIC_CMDS) != 0) {
1089 b->ops = &vmballoon_basic_ops;
1090 b->batch_max_pages = 1;
1091 }
1092
1093 b->reset_required = false;
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001094
1095 error = vmballoon_vmci_init(b);
1096 if (error)
1097 pr_err("failed to initialize vmci doorbell\n");
1098
Xavier Deguillardf220a802015-08-06 15:17:58 -07001099 if (!vmballoon_send_guest_id(b))
1100 pr_err("failed to send guest ID to the host\n");
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001101}
1102
1103/*
1104 * Balloon work function: reset protocol, if needed, get the new size and
1105 * adjust balloon as needed. Repeat in 1 sec.
1106 */
1107static void vmballoon_work(struct work_struct *work)
1108{
1109 struct delayed_work *dwork = to_delayed_work(work);
1110 struct vmballoon *b = container_of(dwork, struct vmballoon, dwork);
1111 unsigned int target;
1112
1113 STATS_INC(b->stats.timer);
1114
1115 if (b->reset_required)
1116 vmballoon_reset(b);
1117
1118 if (b->slow_allocation_cycles > 0)
1119 b->slow_allocation_cycles--;
1120
Philip P. Moltmannd7568c12015-08-06 15:18:01 -07001121 if (!b->reset_required && vmballoon_send_get_target(b, &target)) {
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001122 /* update target, adjust size */
1123 b->target = target;
1124
1125 if (b->size < target)
1126 vmballoon_inflate(b);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001127 else if (target == 0 ||
1128 b->size > target + vmballoon_page_size(true))
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001129 vmballoon_deflate(b);
1130 }
1131
Dmitry Torokhovbeda94d2011-07-26 16:08:56 -07001132 /*
1133 * We are using a freezable workqueue so that balloon operations are
1134 * stopped while the system transitions to/from sleep/hibernation.
1135 */
1136 queue_delayed_work(system_freezable_wq,
1137 dwork, round_jiffies_relative(HZ));
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001138}
1139
1140/*
1141 * DEBUGFS Interface
1142 */
1143#ifdef CONFIG_DEBUG_FS
1144
1145static int vmballoon_debug_show(struct seq_file *f, void *offset)
1146{
1147 struct vmballoon *b = f->private;
1148 struct vmballoon_stats *stats = &b->stats;
1149
Philip P. Moltmannb36e89d2015-08-06 15:18:00 -07001150 /* format capabilities info */
1151 seq_printf(f,
1152 "balloon capabilities: %#4x\n"
Philip P. Moltmannd7568c12015-08-06 15:18:01 -07001153 "used capabilities: %#4lx\n"
1154 "is resetting: %c\n",
1155 VMW_BALLOON_CAPABILITIES, b->capabilities,
1156 b->reset_required ? 'y' : 'n');
Philip P. Moltmannb36e89d2015-08-06 15:18:00 -07001157
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001158 /* format size info */
1159 seq_printf(f,
1160 "target: %8d pages\n"
1161 "current: %8d pages\n",
1162 b->target, b->size);
1163
1164 /* format rate info */
1165 seq_printf(f,
Philip P. Moltmann33d268e2015-08-06 15:18:01 -07001166 "rateSleepAlloc: %8d pages/sec\n",
1167 b->rate_alloc);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001168
1169 seq_printf(f,
1170 "\n"
1171 "timer: %8u\n"
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001172 "doorbell: %8u\n"
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001173 "start: %8u (%4u failed)\n"
1174 "guestType: %8u (%4u failed)\n"
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001175 "2m-lock: %8u (%4u failed)\n"
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001176 "lock: %8u (%4u failed)\n"
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001177 "2m-unlock: %8u (%4u failed)\n"
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001178 "unlock: %8u (%4u failed)\n"
1179 "target: %8u (%4u failed)\n"
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001180 "prim2mAlloc: %8u (%4u failed)\n"
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001181 "primNoSleepAlloc: %8u (%4u failed)\n"
1182 "primCanSleepAlloc: %8u (%4u failed)\n"
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001183 "prim2mFree: %8u\n"
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001184 "primFree: %8u\n"
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001185 "err2mAlloc: %8u\n"
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001186 "errAlloc: %8u\n"
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001187 "err2mFree: %8u\n"
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001188 "errFree: %8u\n"
1189 "doorbellSet: %8u\n"
1190 "doorbellUnset: %8u\n",
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001191 stats->timer,
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001192 stats->doorbell,
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001193 stats->start, stats->start_fail,
1194 stats->guest_type, stats->guest_type_fail,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001195 stats->lock[true], stats->lock_fail[true],
1196 stats->lock[false], stats->lock_fail[false],
1197 stats->unlock[true], stats->unlock_fail[true],
1198 stats->unlock[false], stats->unlock_fail[false],
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001199 stats->target, stats->target_fail,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001200 stats->alloc[true], stats->alloc_fail[true],
1201 stats->alloc[false], stats->alloc_fail[false],
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001202 stats->sleep_alloc, stats->sleep_alloc_fail,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001203 stats->free[true],
1204 stats->free[false],
1205 stats->refused_alloc[true], stats->refused_alloc[false],
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001206 stats->refused_free[true], stats->refused_free[false],
1207 stats->doorbell_set, stats->doorbell_unset);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001208
1209 return 0;
1210}
1211
1212static int vmballoon_debug_open(struct inode *inode, struct file *file)
1213{
1214 return single_open(file, vmballoon_debug_show, inode->i_private);
1215}
1216
1217static const struct file_operations vmballoon_debug_fops = {
1218 .owner = THIS_MODULE,
1219 .open = vmballoon_debug_open,
1220 .read = seq_read,
1221 .llseek = seq_lseek,
1222 .release = single_release,
1223};
1224
1225static int __init vmballoon_debugfs_init(struct vmballoon *b)
1226{
1227 int error;
1228
1229 b->dbg_entry = debugfs_create_file("vmmemctl", S_IRUGO, NULL, b,
1230 &vmballoon_debug_fops);
1231 if (IS_ERR(b->dbg_entry)) {
1232 error = PTR_ERR(b->dbg_entry);
1233 pr_err("failed to create debugfs entry, error: %d\n", error);
1234 return error;
1235 }
1236
1237 return 0;
1238}
1239
1240static void __exit vmballoon_debugfs_exit(struct vmballoon *b)
1241{
1242 debugfs_remove(b->dbg_entry);
1243}
1244
1245#else
1246
1247static inline int vmballoon_debugfs_init(struct vmballoon *b)
1248{
1249 return 0;
1250}
1251
1252static inline void vmballoon_debugfs_exit(struct vmballoon *b)
1253{
1254}
1255
1256#endif /* CONFIG_DEBUG_FS */
1257
1258static int __init vmballoon_init(void)
1259{
1260 int error;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001261 unsigned is_2m_pages;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001262 /*
1263 * Check if we are running on VMware's hypervisor and bail out
1264 * if we are not.
1265 */
H. Peter Anvina10a5692010-05-09 01:13:42 -07001266 if (x86_hyper != &x86_hyper_vmware)
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001267 return -ENODEV;
1268
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001269 for (is_2m_pages = 0; is_2m_pages < VMW_BALLOON_NUM_PAGE_SIZES;
1270 is_2m_pages++) {
1271 INIT_LIST_HEAD(&balloon.page_sizes[is_2m_pages].pages);
1272 INIT_LIST_HEAD(&balloon.page_sizes[is_2m_pages].refused_pages);
1273 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001274
1275 /* initialize rates */
1276 balloon.rate_alloc = VMW_BALLOON_RATE_ALLOC_MAX;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001277
1278 INIT_DELAYED_WORK(&balloon.dwork, vmballoon_work);
1279
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001280 error = vmballoon_debugfs_init(&balloon);
1281 if (error)
Dmitry Torokhovbeda94d2011-07-26 16:08:56 -07001282 return error;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001283
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001284 balloon.vmci_doorbell = VMCI_INVALID_HANDLE;
Philip P. Moltmannd7568c12015-08-06 15:18:01 -07001285 balloon.batch_page = NULL;
1286 balloon.page = NULL;
1287 balloon.reset_required = true;
1288
Dmitry Torokhovbeda94d2011-07-26 16:08:56 -07001289 queue_delayed_work(system_freezable_wq, &balloon.dwork, 0);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001290
1291 return 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001292}
1293module_init(vmballoon_init);
1294
1295static void __exit vmballoon_exit(void)
1296{
Philip P. Moltmann48e3d662015-08-06 15:18:01 -07001297 vmballoon_vmci_cleanup(&balloon);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001298 cancel_delayed_work_sync(&balloon.dwork);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001299
1300 vmballoon_debugfs_exit(&balloon);
1301
1302 /*
1303 * Deallocate all reserved memory, and reset connection with monitor.
1304 * Reset connection before deallocating memory to avoid potential for
1305 * additional spurious resets from guest touching deallocated pages.
1306 */
Philip P. Moltmannd7568c12015-08-06 15:18:01 -07001307 vmballoon_send_start(&balloon, 0);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001308 vmballoon_pop(&balloon);
1309}
1310module_exit(vmballoon_exit);