blob: 28fe9e5a14ca064ebbd20df1c162dbe7bd119cd4 [file] [log] [blame]
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001/*
2 * VMware Balloon driver.
3 *
Xavier Deguillardf220a802015-08-06 15:17:58 -07004 * Copyright (C) 2000-2013, 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>
H. Peter Anvina10a5692010-05-09 01:13:42 -070046#include <asm/hypervisor.h>
Dmitry Torokhov453dc652010-04-23 13:18:08 -040047
48MODULE_AUTHOR("VMware, Inc.");
49MODULE_DESCRIPTION("VMware Memory Control (Balloon) Driver");
Philip P. Moltmannd7568c12015-08-06 15:18:01 -070050MODULE_VERSION("1.4.1.0-k");
Dmitry Torokhov453dc652010-04-23 13:18:08 -040051MODULE_ALIAS("dmi:*:svnVMware*:*");
52MODULE_ALIAS("vmware_vmmemctl");
53MODULE_LICENSE("GPL");
54
55/*
56 * Various constants controlling rate of inflaint/deflating balloon,
57 * measured in pages.
58 */
59
60/*
Dmitry Torokhov453dc652010-04-23 13:18:08 -040061 * Rates of memory allocaton when guest experiences memory pressure
62 * (driver performs sleeping allocations).
63 */
64#define VMW_BALLOON_RATE_ALLOC_MIN 512U
65#define VMW_BALLOON_RATE_ALLOC_MAX 2048U
66#define VMW_BALLOON_RATE_ALLOC_INC 16U
67
68/*
Dmitry Torokhov453dc652010-04-23 13:18:08 -040069 * When guest is under memory pressure, use a reduced page allocation
70 * rate for next several cycles.
71 */
72#define VMW_BALLOON_SLOW_CYCLES 4
73
74/*
75 * Use __GFP_HIGHMEM to allow pages from HIGHMEM zone. We don't
76 * allow wait (__GFP_WAIT) for NOSLEEP page allocations. Use
77 * __GFP_NOWARN, to suppress page allocation failure warnings.
78 */
79#define VMW_PAGE_ALLOC_NOSLEEP (__GFP_HIGHMEM|__GFP_NOWARN)
80
81/*
82 * Use GFP_HIGHUSER when executing in a separate kernel thread
83 * context and allocation can sleep. This is less stressful to
84 * the guest memory system, since it allows the thread to block
85 * while memory is reclaimed, and won't take pages from emergency
86 * low-memory pools.
87 */
88#define VMW_PAGE_ALLOC_CANSLEEP (GFP_HIGHUSER)
89
Dmitry Torokhov55adaa42010-06-04 14:14:52 -070090/* Maximum number of refused pages we accumulate during inflation cycle */
91#define VMW_BALLOON_MAX_REFUSED 16
Dmitry Torokhov453dc652010-04-23 13:18:08 -040092
93/*
94 * Hypervisor communication port definitions.
95 */
96#define VMW_BALLOON_HV_PORT 0x5670
97#define VMW_BALLOON_HV_MAGIC 0x456c6d6f
Dmitry Torokhov453dc652010-04-23 13:18:08 -040098#define VMW_BALLOON_GUEST_ID 1 /* Linux */
99
Xavier Deguillardeb79100f2015-06-12 11:43:23 -0700100enum vmwballoon_capabilities {
101 /*
102 * Bit 0 is reserved and not associated to any capability.
103 */
104 VMW_BALLOON_BASIC_CMDS = (1 << 1),
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700105 VMW_BALLOON_BATCHED_CMDS = (1 << 2),
106 VMW_BALLOON_BATCHED_2M_CMDS = (1 << 3),
Xavier Deguillardeb79100f2015-06-12 11:43:23 -0700107};
108
Xavier Deguillardf220a802015-08-06 15:17:58 -0700109#define VMW_BALLOON_CAPABILITIES (VMW_BALLOON_BASIC_CMDS \
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700110 | VMW_BALLOON_BATCHED_CMDS \
111 | VMW_BALLOON_BATCHED_2M_CMDS)
112
113#define VMW_BALLOON_2M_SHIFT (9)
114#define VMW_BALLOON_NUM_PAGE_SIZES (2)
Xavier Deguillardeb79100f2015-06-12 11:43:23 -0700115
Xavier Deguillardf220a802015-08-06 15:17:58 -0700116/*
117 * Backdoor commands availability:
118 *
119 * START, GET_TARGET and GUEST_ID are always available,
120 *
121 * VMW_BALLOON_BASIC_CMDS:
122 * LOCK and UNLOCK commands,
123 * VMW_BALLOON_BATCHED_CMDS:
124 * BATCHED_LOCK and BATCHED_UNLOCK commands.
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700125 * VMW BALLOON_BATCHED_2M_CMDS:
126 * BATCHED_2M_LOCK and BATCHED_2M_UNLOCK commands.
Xavier Deguillardf220a802015-08-06 15:17:58 -0700127 */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700128#define VMW_BALLOON_CMD_START 0
129#define VMW_BALLOON_CMD_GET_TARGET 1
130#define VMW_BALLOON_CMD_LOCK 2
131#define VMW_BALLOON_CMD_UNLOCK 3
132#define VMW_BALLOON_CMD_GUEST_ID 4
133#define VMW_BALLOON_CMD_BATCHED_LOCK 6
134#define VMW_BALLOON_CMD_BATCHED_UNLOCK 7
135#define VMW_BALLOON_CMD_BATCHED_2M_LOCK 8
136#define VMW_BALLOON_CMD_BATCHED_2M_UNLOCK 9
137
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400138
139/* error codes */
Xavier Deguillardeb79100f2015-06-12 11:43:23 -0700140#define VMW_BALLOON_SUCCESS 0
141#define VMW_BALLOON_FAILURE -1
142#define VMW_BALLOON_ERROR_CMD_INVALID 1
143#define VMW_BALLOON_ERROR_PPN_INVALID 2
144#define VMW_BALLOON_ERROR_PPN_LOCKED 3
145#define VMW_BALLOON_ERROR_PPN_UNLOCKED 4
146#define VMW_BALLOON_ERROR_PPN_PINNED 5
147#define VMW_BALLOON_ERROR_PPN_NOTNEEDED 6
148#define VMW_BALLOON_ERROR_RESET 7
149#define VMW_BALLOON_ERROR_BUSY 8
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400150
Xavier Deguillardeb79100f2015-06-12 11:43:23 -0700151#define VMW_BALLOON_SUCCESS_WITH_CAPABILITIES (0x03000000)
152
Xavier Deguillardf220a802015-08-06 15:17:58 -0700153/* Batch page description */
154
155/*
156 * Layout of a page in the batch page:
157 *
158 * +-------------+----------+--------+
159 * | | | |
160 * | Page number | Reserved | Status |
161 * | | | |
162 * +-------------+----------+--------+
163 * 64 PAGE_SHIFT 6 0
164 *
Xavier Deguillardf220a802015-08-06 15:17:58 -0700165 * The reserved field should be set to 0.
166 */
167#define VMW_BALLOON_BATCH_MAX_PAGES (PAGE_SIZE / sizeof(u64))
168#define VMW_BALLOON_BATCH_STATUS_MASK ((1UL << 5) - 1)
169#define VMW_BALLOON_BATCH_PAGE_MASK (~((1UL << PAGE_SHIFT) - 1))
170
171struct vmballoon_batch_page {
172 u64 pages[VMW_BALLOON_BATCH_MAX_PAGES];
173};
174
175static u64 vmballoon_batch_get_pa(struct vmballoon_batch_page *batch, int idx)
176{
177 return batch->pages[idx] & VMW_BALLOON_BATCH_PAGE_MASK;
178}
179
180static int vmballoon_batch_get_status(struct vmballoon_batch_page *batch,
181 int idx)
182{
183 return (int)(batch->pages[idx] & VMW_BALLOON_BATCH_STATUS_MASK);
184}
185
186static void vmballoon_batch_set_pa(struct vmballoon_batch_page *batch, int idx,
187 u64 pa)
188{
189 batch->pages[idx] = pa;
190}
191
192
193#define VMWARE_BALLOON_CMD(cmd, arg1, arg2, result) \
Xavier Deguillardeb79100f2015-06-12 11:43:23 -0700194({ \
Xavier Deguillardf220a802015-08-06 15:17:58 -0700195 unsigned long __status, __dummy1, __dummy2, __dummy3; \
Xavier Deguillardeb79100f2015-06-12 11:43:23 -0700196 __asm__ __volatile__ ("inl %%dx" : \
197 "=a"(__status), \
198 "=c"(__dummy1), \
199 "=d"(__dummy2), \
Xavier Deguillardf220a802015-08-06 15:17:58 -0700200 "=b"(result), \
201 "=S" (__dummy3) : \
Xavier Deguillardeb79100f2015-06-12 11:43:23 -0700202 "0"(VMW_BALLOON_HV_MAGIC), \
203 "1"(VMW_BALLOON_CMD_##cmd), \
204 "2"(VMW_BALLOON_HV_PORT), \
Xavier Deguillardf220a802015-08-06 15:17:58 -0700205 "3"(arg1), \
206 "4" (arg2) : \
Xavier Deguillardeb79100f2015-06-12 11:43:23 -0700207 "memory"); \
208 if (VMW_BALLOON_CMD_##cmd == VMW_BALLOON_CMD_START) \
209 result = __dummy1; \
210 result &= -1UL; \
211 __status & -1UL; \
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400212})
213
214#ifdef CONFIG_DEBUG_FS
215struct vmballoon_stats {
216 unsigned int timer;
217
Rakib Mullick2ca02df2011-11-02 13:40:07 -0700218 /* allocation statistics */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700219 unsigned int alloc[VMW_BALLOON_NUM_PAGE_SIZES];
220 unsigned int alloc_fail[VMW_BALLOON_NUM_PAGE_SIZES];
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400221 unsigned int sleep_alloc;
222 unsigned int sleep_alloc_fail;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700223 unsigned int refused_alloc[VMW_BALLOON_NUM_PAGE_SIZES];
224 unsigned int refused_free[VMW_BALLOON_NUM_PAGE_SIZES];
225 unsigned int free[VMW_BALLOON_NUM_PAGE_SIZES];
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400226
227 /* monitor operations */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700228 unsigned int lock[VMW_BALLOON_NUM_PAGE_SIZES];
229 unsigned int lock_fail[VMW_BALLOON_NUM_PAGE_SIZES];
230 unsigned int unlock[VMW_BALLOON_NUM_PAGE_SIZES];
231 unsigned int unlock_fail[VMW_BALLOON_NUM_PAGE_SIZES];
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400232 unsigned int target;
233 unsigned int target_fail;
234 unsigned int start;
235 unsigned int start_fail;
236 unsigned int guest_type;
237 unsigned int guest_type_fail;
238};
239
240#define STATS_INC(stat) (stat)++
241#else
242#define STATS_INC(stat)
243#endif
244
Xavier Deguillardf220a802015-08-06 15:17:58 -0700245struct vmballoon;
246
247struct vmballoon_ops {
248 void (*add_page)(struct vmballoon *b, int idx, struct page *p);
Xavier Deguillard4670de42015-08-06 15:17:59 -0700249 int (*lock)(struct vmballoon *b, unsigned int num_pages,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700250 bool is_2m_pages, unsigned int *target);
Xavier Deguillard4670de42015-08-06 15:17:59 -0700251 int (*unlock)(struct vmballoon *b, unsigned int num_pages,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700252 bool is_2m_pages, unsigned int *target);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700253};
254
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700255struct vmballoon_page_size {
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400256 /* list of reserved physical pages */
257 struct list_head pages;
258
259 /* transient list of non-balloonable pages */
260 struct list_head refused_pages;
Dmitry Torokhov55adaa42010-06-04 14:14:52 -0700261 unsigned int n_refused_pages;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700262};
263
264struct vmballoon {
265 struct vmballoon_page_size page_sizes[VMW_BALLOON_NUM_PAGE_SIZES];
266
267 /* supported page sizes. 1 == 4k pages only, 2 == 4k and 2m pages */
268 unsigned supported_page_sizes;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400269
270 /* balloon size in pages */
271 unsigned int size;
272 unsigned int target;
273
274 /* reset flag */
275 bool reset_required;
276
277 /* adjustment rates (pages per second) */
278 unsigned int rate_alloc;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400279
280 /* slowdown page allocations for next few cycles */
281 unsigned int slow_allocation_cycles;
282
Xavier Deguillardf220a802015-08-06 15:17:58 -0700283 unsigned long capabilities;
284
285 struct vmballoon_batch_page *batch_page;
286 unsigned int batch_max_pages;
287 struct page *page;
288
289 const struct vmballoon_ops *ops;
290
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400291#ifdef CONFIG_DEBUG_FS
292 /* statistics */
293 struct vmballoon_stats stats;
294
295 /* debugfs file exporting statistics */
296 struct dentry *dbg_entry;
297#endif
298
299 struct sysinfo sysinfo;
300
301 struct delayed_work dwork;
302};
303
304static struct vmballoon balloon;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400305
306/*
307 * Send "start" command to the host, communicating supported version
308 * of the protocol.
309 */
Xavier Deguillardf220a802015-08-06 15:17:58 -0700310static bool vmballoon_send_start(struct vmballoon *b, unsigned long req_caps)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400311{
Xavier Deguillardf220a802015-08-06 15:17:58 -0700312 unsigned long status, capabilities, dummy = 0;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700313 bool success;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400314
315 STATS_INC(b->stats.start);
316
Xavier Deguillardf220a802015-08-06 15:17:58 -0700317 status = VMWARE_BALLOON_CMD(START, req_caps, dummy, capabilities);
318
319 switch (status) {
320 case VMW_BALLOON_SUCCESS_WITH_CAPABILITIES:
321 b->capabilities = capabilities;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700322 success = true;
323 break;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700324 case VMW_BALLOON_SUCCESS:
325 b->capabilities = VMW_BALLOON_BASIC_CMDS;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700326 success = true;
327 break;
328 default:
329 success = false;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700330 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400331
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700332 if (b->capabilities & VMW_BALLOON_BATCHED_2M_CMDS)
333 b->supported_page_sizes = 2;
334 else
335 b->supported_page_sizes = 1;
336
337 if (!success) {
338 pr_debug("%s - failed, hv returns %ld\n", __func__, status);
339 STATS_INC(b->stats.start_fail);
340 }
341 return success;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400342}
343
344static bool vmballoon_check_status(struct vmballoon *b, unsigned long status)
345{
346 switch (status) {
347 case VMW_BALLOON_SUCCESS:
348 return true;
349
350 case VMW_BALLOON_ERROR_RESET:
351 b->reset_required = true;
352 /* fall through */
353
354 default:
355 return false;
356 }
357}
358
359/*
360 * Communicate guest type to the host so that it can adjust ballooning
361 * algorithm to the one most appropriate for the guest. This command
362 * is normally issued after sending "start" command and is part of
363 * standard reset sequence.
364 */
365static bool vmballoon_send_guest_id(struct vmballoon *b)
366{
Xavier Deguillardf220a802015-08-06 15:17:58 -0700367 unsigned long status, dummy = 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400368
Xavier Deguillardf220a802015-08-06 15:17:58 -0700369 status = VMWARE_BALLOON_CMD(GUEST_ID, VMW_BALLOON_GUEST_ID, dummy,
370 dummy);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400371
372 STATS_INC(b->stats.guest_type);
373
374 if (vmballoon_check_status(b, status))
375 return true;
376
377 pr_debug("%s - failed, hv returns %ld\n", __func__, status);
378 STATS_INC(b->stats.guest_type_fail);
379 return false;
380}
381
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700382static u16 vmballoon_page_size(bool is_2m_page)
383{
384 if (is_2m_page)
385 return 1 << VMW_BALLOON_2M_SHIFT;
386
387 return 1;
388}
389
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400390/*
391 * Retrieve desired balloon size from the host.
392 */
393static bool vmballoon_send_get_target(struct vmballoon *b, u32 *new_target)
394{
395 unsigned long status;
396 unsigned long target;
397 unsigned long limit;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700398 unsigned long dummy = 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400399 u32 limit32;
400
401 /*
402 * si_meminfo() is cheap. Moreover, we want to provide dynamic
403 * max balloon size later. So let us call si_meminfo() every
404 * iteration.
405 */
406 si_meminfo(&b->sysinfo);
407 limit = b->sysinfo.totalram;
408
409 /* Ensure limit fits in 32-bits */
410 limit32 = (u32)limit;
411 if (limit != limit32)
412 return false;
413
414 /* update stats */
415 STATS_INC(b->stats.target);
416
Xavier Deguillardf220a802015-08-06 15:17:58 -0700417 status = VMWARE_BALLOON_CMD(GET_TARGET, limit, dummy, target);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400418 if (vmballoon_check_status(b, status)) {
419 *new_target = target;
420 return true;
421 }
422
423 pr_debug("%s - failed, hv returns %ld\n", __func__, status);
424 STATS_INC(b->stats.target_fail);
425 return false;
426}
427
428/*
429 * Notify the host about allocated page so that host can use it without
430 * fear that guest will need it. Host may reject some pages, we need to
431 * check the return value and maybe submit a different page.
432 */
Danny Kukawka3e5ba462012-01-30 23:00:08 +0100433static int vmballoon_send_lock_page(struct vmballoon *b, unsigned long pfn,
Xavier Deguillard4670de42015-08-06 15:17:59 -0700434 unsigned int *hv_status, unsigned int *target)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400435{
Xavier Deguillardf220a802015-08-06 15:17:58 -0700436 unsigned long status, dummy = 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400437 u32 pfn32;
438
439 pfn32 = (u32)pfn;
440 if (pfn32 != pfn)
Danny Kukawka3e5ba462012-01-30 23:00:08 +0100441 return -1;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400442
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700443 STATS_INC(b->stats.lock[false]);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400444
Xavier Deguillard4670de42015-08-06 15:17:59 -0700445 *hv_status = status = VMWARE_BALLOON_CMD(LOCK, pfn, dummy, *target);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400446 if (vmballoon_check_status(b, status))
Danny Kukawka3e5ba462012-01-30 23:00:08 +0100447 return 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400448
449 pr_debug("%s - ppn %lx, hv returns %ld\n", __func__, pfn, status);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700450 STATS_INC(b->stats.lock_fail[false]);
Danny Kukawka3e5ba462012-01-30 23:00:08 +0100451 return 1;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400452}
453
Xavier Deguillardf220a802015-08-06 15:17:58 -0700454static int vmballoon_send_batched_lock(struct vmballoon *b,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700455 unsigned int num_pages, bool is_2m_pages, unsigned int *target)
Xavier Deguillardf220a802015-08-06 15:17:58 -0700456{
Xavier Deguillard4670de42015-08-06 15:17:59 -0700457 unsigned long status;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700458 unsigned long pfn = page_to_pfn(b->page);
459
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700460 STATS_INC(b->stats.lock[is_2m_pages]);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700461
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700462 if (is_2m_pages)
463 status = VMWARE_BALLOON_CMD(BATCHED_2M_LOCK, pfn, num_pages,
464 *target);
465 else
466 status = VMWARE_BALLOON_CMD(BATCHED_LOCK, pfn, num_pages,
467 *target);
468
Xavier Deguillardf220a802015-08-06 15:17:58 -0700469 if (vmballoon_check_status(b, status))
470 return 0;
471
472 pr_debug("%s - batch ppn %lx, hv returns %ld\n", __func__, pfn, status);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700473 STATS_INC(b->stats.lock_fail[is_2m_pages]);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700474 return 1;
475}
476
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400477/*
478 * Notify the host that guest intends to release given page back into
479 * the pool of available (to the guest) pages.
480 */
Xavier Deguillard4670de42015-08-06 15:17:59 -0700481static bool vmballoon_send_unlock_page(struct vmballoon *b, unsigned long pfn,
482 unsigned int *target)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400483{
Xavier Deguillardf220a802015-08-06 15:17:58 -0700484 unsigned long status, dummy = 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400485 u32 pfn32;
486
487 pfn32 = (u32)pfn;
488 if (pfn32 != pfn)
489 return false;
490
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700491 STATS_INC(b->stats.unlock[false]);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400492
Xavier Deguillard4670de42015-08-06 15:17:59 -0700493 status = VMWARE_BALLOON_CMD(UNLOCK, pfn, dummy, *target);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400494 if (vmballoon_check_status(b, status))
495 return true;
496
497 pr_debug("%s - ppn %lx, hv returns %ld\n", __func__, pfn, status);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700498 STATS_INC(b->stats.unlock_fail[false]);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400499 return false;
500}
501
Xavier Deguillardf220a802015-08-06 15:17:58 -0700502static bool vmballoon_send_batched_unlock(struct vmballoon *b,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700503 unsigned int num_pages, bool is_2m_pages, unsigned int *target)
Xavier Deguillardf220a802015-08-06 15:17:58 -0700504{
Xavier Deguillard4670de42015-08-06 15:17:59 -0700505 unsigned long status;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700506 unsigned long pfn = page_to_pfn(b->page);
507
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700508 STATS_INC(b->stats.unlock[is_2m_pages]);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700509
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700510 if (is_2m_pages)
511 status = VMWARE_BALLOON_CMD(BATCHED_2M_UNLOCK, pfn, num_pages,
512 *target);
513 else
514 status = VMWARE_BALLOON_CMD(BATCHED_UNLOCK, pfn, num_pages,
515 *target);
516
Xavier Deguillardf220a802015-08-06 15:17:58 -0700517 if (vmballoon_check_status(b, status))
518 return true;
519
520 pr_debug("%s - batch ppn %lx, hv returns %ld\n", __func__, pfn, status);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700521 STATS_INC(b->stats.unlock_fail[is_2m_pages]);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700522 return false;
523}
524
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700525static struct page *vmballoon_alloc_page(gfp_t flags, bool is_2m_page)
526{
527 if (is_2m_page)
528 return alloc_pages(flags, VMW_BALLOON_2M_SHIFT);
529
530 return alloc_page(flags);
531}
532
533static void vmballoon_free_page(struct page *page, bool is_2m_page)
534{
535 if (is_2m_page)
536 __free_pages(page, VMW_BALLOON_2M_SHIFT);
537 else
538 __free_page(page);
539}
540
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400541/*
542 * Quickly release all pages allocated for the balloon. This function is
543 * called when host decides to "reset" balloon for one reason or another.
544 * Unlike normal "deflate" we do not (shall not) notify host of the pages
545 * being released.
546 */
547static void vmballoon_pop(struct vmballoon *b)
548{
549 struct page *page, *next;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700550 unsigned is_2m_pages;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400551
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700552 for (is_2m_pages = 0; is_2m_pages < VMW_BALLOON_NUM_PAGE_SIZES;
553 is_2m_pages++) {
554 struct vmballoon_page_size *page_size =
555 &b->page_sizes[is_2m_pages];
556 u16 size_per_page = vmballoon_page_size(is_2m_pages);
557
558 list_for_each_entry_safe(page, next, &page_size->pages, lru) {
559 list_del(&page->lru);
560 vmballoon_free_page(page, is_2m_pages);
561 STATS_INC(b->stats.free[is_2m_pages]);
562 b->size -= size_per_page;
563 cond_resched();
564 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400565 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400566
Philip P. Moltmannd7568c12015-08-06 15:18:01 -0700567 if (b->batch_page) {
568 vunmap(b->batch_page);
569 b->batch_page = NULL;
570 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400571
Philip P. Moltmannd7568c12015-08-06 15:18:01 -0700572 if (b->page) {
573 __free_page(b->page);
574 b->page = NULL;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400575 }
576}
577
578/*
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700579 * Notify the host of a ballooned page. If host rejects the page put it on the
580 * refuse list, those refused page are then released at the end of the
581 * inflation cycle.
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400582 */
Xavier Deguillard4670de42015-08-06 15:17:59 -0700583static int vmballoon_lock_page(struct vmballoon *b, unsigned int num_pages,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700584 bool is_2m_pages, unsigned int *target)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400585{
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700586 int locked, hv_status;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700587 struct page *page = b->page;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700588 struct vmballoon_page_size *page_size = &b->page_sizes[false];
589
590 /* is_2m_pages can never happen as 2m pages support implies batching */
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400591
Xavier Deguillard4670de42015-08-06 15:17:59 -0700592 locked = vmballoon_send_lock_page(b, page_to_pfn(page), &hv_status,
593 target);
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700594 if (locked > 0) {
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700595 STATS_INC(b->stats.refused_alloc[false]);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400596
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700597 if (hv_status == VMW_BALLOON_ERROR_RESET ||
598 hv_status == VMW_BALLOON_ERROR_PPN_NOTNEEDED) {
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700599 vmballoon_free_page(page, false);
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700600 return -EIO;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400601 }
602
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700603 /*
604 * Place page on the list of non-balloonable pages
605 * and retry allocation, unless we already accumulated
606 * too many of them, in which case take a breather.
607 */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700608 if (page_size->n_refused_pages < VMW_BALLOON_MAX_REFUSED) {
609 page_size->n_refused_pages++;
610 list_add(&page->lru, &page_size->refused_pages);
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700611 } else {
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700612 vmballoon_free_page(page, false);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400613 }
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700614 return -EIO;
615 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400616
617 /* track allocated page */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700618 list_add(&page->lru, &page_size->pages);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400619
620 /* update balloon size */
621 b->size++;
622
623 return 0;
624}
625
Xavier Deguillardf220a802015-08-06 15:17:58 -0700626static int vmballoon_lock_batched_page(struct vmballoon *b,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700627 unsigned int num_pages, bool is_2m_pages, unsigned int *target)
Xavier Deguillardf220a802015-08-06 15:17:58 -0700628{
629 int locked, i;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700630 u16 size_per_page = vmballoon_page_size(is_2m_pages);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700631
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700632 locked = vmballoon_send_batched_lock(b, num_pages, is_2m_pages,
633 target);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700634 if (locked > 0) {
635 for (i = 0; i < num_pages; i++) {
636 u64 pa = vmballoon_batch_get_pa(b->batch_page, i);
637 struct page *p = pfn_to_page(pa >> PAGE_SHIFT);
638
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700639 vmballoon_free_page(p, is_2m_pages);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700640 }
641
642 return -EIO;
643 }
644
645 for (i = 0; i < num_pages; i++) {
646 u64 pa = vmballoon_batch_get_pa(b->batch_page, i);
647 struct page *p = pfn_to_page(pa >> PAGE_SHIFT);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700648 struct vmballoon_page_size *page_size =
649 &b->page_sizes[is_2m_pages];
Xavier Deguillardf220a802015-08-06 15:17:58 -0700650
651 locked = vmballoon_batch_get_status(b->batch_page, i);
652
653 switch (locked) {
654 case VMW_BALLOON_SUCCESS:
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700655 list_add(&p->lru, &page_size->pages);
656 b->size += size_per_page;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700657 break;
658 case VMW_BALLOON_ERROR_PPN_PINNED:
659 case VMW_BALLOON_ERROR_PPN_INVALID:
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700660 if (page_size->n_refused_pages
661 < VMW_BALLOON_MAX_REFUSED) {
662 list_add(&p->lru, &page_size->refused_pages);
663 page_size->n_refused_pages++;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700664 break;
665 }
666 /* Fallthrough */
667 case VMW_BALLOON_ERROR_RESET:
668 case VMW_BALLOON_ERROR_PPN_NOTNEEDED:
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700669 vmballoon_free_page(p, is_2m_pages);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700670 break;
671 default:
672 /* This should never happen */
673 WARN_ON_ONCE(true);
674 }
675 }
676
677 return 0;
678}
679
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400680/*
681 * Release the page allocated for the balloon. Note that we first notify
682 * the host so it can make sure the page will be available for the guest
683 * to use, if needed.
684 */
Xavier Deguillard4670de42015-08-06 15:17:59 -0700685static int vmballoon_unlock_page(struct vmballoon *b, unsigned int num_pages,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700686 bool is_2m_pages, unsigned int *target)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400687{
Xavier Deguillardf220a802015-08-06 15:17:58 -0700688 struct page *page = b->page;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700689 struct vmballoon_page_size *page_size = &b->page_sizes[false];
690
691 /* is_2m_pages can never happen as 2m pages support implies batching */
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400692
Xavier Deguillard4670de42015-08-06 15:17:59 -0700693 if (!vmballoon_send_unlock_page(b, page_to_pfn(page), target)) {
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700694 list_add(&page->lru, &page_size->pages);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700695 return -EIO;
696 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400697
698 /* deallocate page */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700699 vmballoon_free_page(page, false);
700 STATS_INC(b->stats.free[false]);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400701
702 /* update balloon size */
703 b->size--;
704
705 return 0;
706}
707
Xavier Deguillardf220a802015-08-06 15:17:58 -0700708static int vmballoon_unlock_batched_page(struct vmballoon *b,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700709 unsigned int num_pages, bool is_2m_pages,
710 unsigned int *target)
Xavier Deguillardf220a802015-08-06 15:17:58 -0700711{
712 int locked, i, ret = 0;
713 bool hv_success;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700714 u16 size_per_page = vmballoon_page_size(is_2m_pages);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700715
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700716 hv_success = vmballoon_send_batched_unlock(b, num_pages, is_2m_pages,
717 target);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700718 if (!hv_success)
719 ret = -EIO;
720
721 for (i = 0; i < num_pages; i++) {
722 u64 pa = vmballoon_batch_get_pa(b->batch_page, i);
723 struct page *p = pfn_to_page(pa >> PAGE_SHIFT);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700724 struct vmballoon_page_size *page_size =
725 &b->page_sizes[is_2m_pages];
Xavier Deguillardf220a802015-08-06 15:17:58 -0700726
727 locked = vmballoon_batch_get_status(b->batch_page, i);
728 if (!hv_success || locked != VMW_BALLOON_SUCCESS) {
729 /*
730 * That page wasn't successfully unlocked by the
731 * hypervisor, re-add it to the list of pages owned by
732 * the balloon driver.
733 */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700734 list_add(&p->lru, &page_size->pages);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700735 } else {
736 /* deallocate page */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700737 vmballoon_free_page(p, is_2m_pages);
738 STATS_INC(b->stats.free[is_2m_pages]);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700739
740 /* update balloon size */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700741 b->size -= size_per_page;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700742 }
743 }
744
745 return ret;
746}
747
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400748/*
749 * Release pages that were allocated while attempting to inflate the
750 * balloon but were refused by the host for one reason or another.
751 */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700752static void vmballoon_release_refused_pages(struct vmballoon *b,
753 bool is_2m_pages)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400754{
755 struct page *page, *next;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700756 struct vmballoon_page_size *page_size =
757 &b->page_sizes[is_2m_pages];
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400758
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700759 list_for_each_entry_safe(page, next, &page_size->refused_pages, lru) {
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400760 list_del(&page->lru);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700761 vmballoon_free_page(page, is_2m_pages);
762 STATS_INC(b->stats.refused_free[is_2m_pages]);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400763 }
Dmitry Torokhov55adaa42010-06-04 14:14:52 -0700764
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700765 page_size->n_refused_pages = 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400766}
767
Xavier Deguillardf220a802015-08-06 15:17:58 -0700768static void vmballoon_add_page(struct vmballoon *b, int idx, struct page *p)
769{
770 b->page = p;
771}
772
773static void vmballoon_add_batched_page(struct vmballoon *b, int idx,
774 struct page *p)
775{
776 vmballoon_batch_set_pa(b->batch_page, idx,
777 (u64)page_to_pfn(p) << PAGE_SHIFT);
778}
779
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400780/*
781 * Inflate the balloon towards its target size. Note that we try to limit
782 * the rate of allocation to make sure we are not choking the rest of the
783 * system.
784 */
785static void vmballoon_inflate(struct vmballoon *b)
786{
Philip P. Moltmann33d268e2015-08-06 15:18:01 -0700787 unsigned rate;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400788 unsigned int allocations = 0;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700789 unsigned int num_pages = 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400790 int error = 0;
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700791 gfp_t flags = VMW_PAGE_ALLOC_NOSLEEP;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700792 bool is_2m_pages;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400793
794 pr_debug("%s - size: %d, target %d\n", __func__, b->size, b->target);
795
796 /*
797 * First try NOSLEEP page allocations to inflate balloon.
798 *
799 * If we do not throttle nosleep allocations, we can drain all
800 * free pages in the guest quickly (if the balloon target is high).
801 * As a side-effect, draining free pages helps to inform (force)
802 * the guest to start swapping if balloon target is not met yet,
803 * which is a desired behavior. However, balloon driver can consume
804 * all available CPU cycles if too many pages are allocated in a
805 * second. Therefore, we throttle nosleep allocations even when
806 * the guest is not under memory pressure. OTOH, if we have already
807 * predicted that the guest is under memory pressure, then we
808 * slowdown page allocations considerably.
809 */
810
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400811 /*
812 * Start with no sleep allocation rate which may be higher
813 * than sleeping allocation rate.
814 */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700815 if (b->slow_allocation_cycles) {
816 rate = b->rate_alloc;
817 is_2m_pages = false;
818 } else {
819 rate = UINT_MAX;
820 is_2m_pages =
821 b->supported_page_sizes == VMW_BALLOON_NUM_PAGE_SIZES;
822 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400823
Philip P. Moltmann33d268e2015-08-06 15:18:01 -0700824 pr_debug("%s - goal: %d, no-sleep rate: %u, sleep rate: %d\n",
Xavier Deguillard4670de42015-08-06 15:17:59 -0700825 __func__, b->target - b->size, rate, b->rate_alloc);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400826
Philip P. Moltmann33d268e2015-08-06 15:18:01 -0700827 while (!b->reset_required &&
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700828 b->size + num_pages * vmballoon_page_size(is_2m_pages)
829 < b->target) {
Xavier Deguillard4670de42015-08-06 15:17:59 -0700830 struct page *page;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400831
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700832 if (flags == VMW_PAGE_ALLOC_NOSLEEP)
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700833 STATS_INC(b->stats.alloc[is_2m_pages]);
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700834 else
835 STATS_INC(b->stats.sleep_alloc);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400836
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700837 page = vmballoon_alloc_page(flags, is_2m_pages);
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700838 if (!page) {
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700839 STATS_INC(b->stats.alloc_fail[is_2m_pages]);
840
841 if (is_2m_pages) {
842 b->ops->lock(b, num_pages, true, &b->target);
843
844 /*
845 * ignore errors from locking as we now switch
846 * to 4k pages and we might get different
847 * errors.
848 */
849
850 num_pages = 0;
851 is_2m_pages = false;
852 continue;
853 }
854
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700855 if (flags == VMW_PAGE_ALLOC_CANSLEEP) {
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400856 /*
857 * CANSLEEP page allocation failed, so guest
858 * is under severe memory pressure. Quickly
859 * decrease allocation rate.
860 */
861 b->rate_alloc = max(b->rate_alloc / 2,
862 VMW_BALLOON_RATE_ALLOC_MIN);
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700863 STATS_INC(b->stats.sleep_alloc_fail);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400864 break;
865 }
866
867 /*
868 * NOSLEEP page allocation failed, so the guest is
869 * under memory pressure. Let us slow down page
870 * allocations for next few cycles so that the guest
871 * gets out of memory pressure. Also, if we already
872 * allocated b->rate_alloc pages, let's pause,
873 * otherwise switch to sleeping allocations.
874 */
875 b->slow_allocation_cycles = VMW_BALLOON_SLOW_CYCLES;
876
Xavier Deguillard4670de42015-08-06 15:17:59 -0700877 if (allocations >= b->rate_alloc)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400878 break;
879
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700880 flags = VMW_PAGE_ALLOC_CANSLEEP;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400881 /* Lower rate for sleeping allocations. */
882 rate = b->rate_alloc;
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700883 continue;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400884 }
885
Xavier Deguillardf220a802015-08-06 15:17:58 -0700886 b->ops->add_page(b, num_pages++, page);
887 if (num_pages == b->batch_max_pages) {
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700888 error = b->ops->lock(b, num_pages, is_2m_pages,
889 &b->target);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700890 num_pages = 0;
891 if (error)
892 break;
893 }
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700894
Philip P. Moltmann33d268e2015-08-06 15:18:01 -0700895 cond_resched();
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400896
Xavier Deguillard4670de42015-08-06 15:17:59 -0700897 if (allocations >= rate) {
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400898 /* We allocated enough pages, let's take a break. */
899 break;
900 }
901 }
902
Xavier Deguillardf220a802015-08-06 15:17:58 -0700903 if (num_pages > 0)
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700904 b->ops->lock(b, num_pages, is_2m_pages, &b->target);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700905
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400906 /*
907 * We reached our goal without failures so try increasing
908 * allocation rate.
909 */
Xavier Deguillard4670de42015-08-06 15:17:59 -0700910 if (error == 0 && allocations >= b->rate_alloc) {
911 unsigned int mult = allocations / b->rate_alloc;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400912
913 b->rate_alloc =
914 min(b->rate_alloc + mult * VMW_BALLOON_RATE_ALLOC_INC,
915 VMW_BALLOON_RATE_ALLOC_MAX);
916 }
917
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700918 vmballoon_release_refused_pages(b, true);
919 vmballoon_release_refused_pages(b, false);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400920}
921
922/*
923 * Decrease the size of the balloon allowing guest to use more memory.
924 */
925static void vmballoon_deflate(struct vmballoon *b)
926{
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700927 unsigned is_2m_pages;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400928
Philip P. Moltmann33d268e2015-08-06 15:18:01 -0700929 pr_debug("%s - size: %d, target %d\n", __func__, b->size, b->target);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400930
931 /* free pages to reach target */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700932 for (is_2m_pages = 0; is_2m_pages < b->supported_page_sizes;
933 is_2m_pages++) {
934 struct page *page, *next;
935 unsigned int num_pages = 0;
936 struct vmballoon_page_size *page_size =
937 &b->page_sizes[is_2m_pages];
Xavier Deguillardf220a802015-08-06 15:17:58 -0700938
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700939 list_for_each_entry_safe(page, next, &page_size->pages, lru) {
940 if (b->reset_required ||
941 (b->target > 0 &&
942 b->size - num_pages
943 * vmballoon_page_size(is_2m_pages)
944 < b->target + vmballoon_page_size(true)))
945 break;
Philip P. Moltmann33d268e2015-08-06 15:18:01 -0700946
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700947 list_del(&page->lru);
948 b->ops->add_page(b, num_pages++, page);
949
950 if (num_pages == b->batch_max_pages) {
951 int error;
952
953 error = b->ops->unlock(b, num_pages,
954 is_2m_pages, &b->target);
955 num_pages = 0;
956 if (error)
957 return;
958 }
959
960 cond_resched();
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400961 }
962
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700963 if (num_pages > 0)
964 b->ops->unlock(b, num_pages, is_2m_pages, &b->target);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400965 }
Xavier Deguillardf220a802015-08-06 15:17:58 -0700966}
967
968static const struct vmballoon_ops vmballoon_basic_ops = {
969 .add_page = vmballoon_add_page,
970 .lock = vmballoon_lock_page,
971 .unlock = vmballoon_unlock_page
972};
973
974static const struct vmballoon_ops vmballoon_batched_ops = {
975 .add_page = vmballoon_add_batched_page,
976 .lock = vmballoon_lock_batched_page,
977 .unlock = vmballoon_unlock_batched_page
978};
979
980static bool vmballoon_init_batching(struct vmballoon *b)
981{
982 b->page = alloc_page(VMW_PAGE_ALLOC_NOSLEEP);
983 if (!b->page)
984 return false;
985
986 b->batch_page = vmap(&b->page, 1, VM_MAP, PAGE_KERNEL);
987 if (!b->batch_page) {
988 __free_page(b->page);
989 return false;
990 }
991
992 return true;
993}
994
995/*
996 * Perform standard reset sequence by popping the balloon (in case it
997 * is not empty) and then restarting protocol. This operation normally
998 * happens when host responds with VMW_BALLOON_ERROR_RESET to a command.
999 */
1000static void vmballoon_reset(struct vmballoon *b)
1001{
1002 /* free all pages, skipping monitor unlock */
1003 vmballoon_pop(b);
1004
1005 if (!vmballoon_send_start(b, VMW_BALLOON_CAPABILITIES))
1006 return;
1007
1008 if ((b->capabilities & VMW_BALLOON_BATCHED_CMDS) != 0) {
1009 b->ops = &vmballoon_batched_ops;
1010 b->batch_max_pages = VMW_BALLOON_BATCH_MAX_PAGES;
1011 if (!vmballoon_init_batching(b)) {
1012 /*
1013 * We failed to initialize batching, inform the monitor
1014 * about it by sending a null capability.
1015 *
1016 * The guest will retry in one second.
1017 */
1018 vmballoon_send_start(b, 0);
1019 return;
1020 }
1021 } else if ((b->capabilities & VMW_BALLOON_BASIC_CMDS) != 0) {
1022 b->ops = &vmballoon_basic_ops;
1023 b->batch_max_pages = 1;
1024 }
1025
1026 b->reset_required = false;
1027 if (!vmballoon_send_guest_id(b))
1028 pr_err("failed to send guest ID to the host\n");
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001029}
1030
1031/*
1032 * Balloon work function: reset protocol, if needed, get the new size and
1033 * adjust balloon as needed. Repeat in 1 sec.
1034 */
1035static void vmballoon_work(struct work_struct *work)
1036{
1037 struct delayed_work *dwork = to_delayed_work(work);
1038 struct vmballoon *b = container_of(dwork, struct vmballoon, dwork);
1039 unsigned int target;
1040
1041 STATS_INC(b->stats.timer);
1042
1043 if (b->reset_required)
1044 vmballoon_reset(b);
1045
1046 if (b->slow_allocation_cycles > 0)
1047 b->slow_allocation_cycles--;
1048
Philip P. Moltmannd7568c12015-08-06 15:18:01 -07001049 if (!b->reset_required && vmballoon_send_get_target(b, &target)) {
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001050 /* update target, adjust size */
1051 b->target = target;
1052
1053 if (b->size < target)
1054 vmballoon_inflate(b);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001055 else if (target == 0 ||
1056 b->size > target + vmballoon_page_size(true))
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001057 vmballoon_deflate(b);
1058 }
1059
Dmitry Torokhovbeda94d2011-07-26 16:08:56 -07001060 /*
1061 * We are using a freezable workqueue so that balloon operations are
1062 * stopped while the system transitions to/from sleep/hibernation.
1063 */
1064 queue_delayed_work(system_freezable_wq,
1065 dwork, round_jiffies_relative(HZ));
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001066}
1067
1068/*
1069 * DEBUGFS Interface
1070 */
1071#ifdef CONFIG_DEBUG_FS
1072
1073static int vmballoon_debug_show(struct seq_file *f, void *offset)
1074{
1075 struct vmballoon *b = f->private;
1076 struct vmballoon_stats *stats = &b->stats;
1077
Philip P. Moltmannb36e89d2015-08-06 15:18:00 -07001078 /* format capabilities info */
1079 seq_printf(f,
1080 "balloon capabilities: %#4x\n"
Philip P. Moltmannd7568c12015-08-06 15:18:01 -07001081 "used capabilities: %#4lx\n"
1082 "is resetting: %c\n",
1083 VMW_BALLOON_CAPABILITIES, b->capabilities,
1084 b->reset_required ? 'y' : 'n');
Philip P. Moltmannb36e89d2015-08-06 15:18:00 -07001085
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001086 /* format size info */
1087 seq_printf(f,
1088 "target: %8d pages\n"
1089 "current: %8d pages\n",
1090 b->target, b->size);
1091
1092 /* format rate info */
1093 seq_printf(f,
Philip P. Moltmann33d268e2015-08-06 15:18:01 -07001094 "rateSleepAlloc: %8d pages/sec\n",
1095 b->rate_alloc);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001096
1097 seq_printf(f,
1098 "\n"
1099 "timer: %8u\n"
1100 "start: %8u (%4u failed)\n"
1101 "guestType: %8u (%4u failed)\n"
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001102 "2m-lock: %8u (%4u failed)\n"
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001103 "lock: %8u (%4u failed)\n"
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001104 "2m-unlock: %8u (%4u failed)\n"
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001105 "unlock: %8u (%4u failed)\n"
1106 "target: %8u (%4u failed)\n"
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001107 "prim2mAlloc: %8u (%4u failed)\n"
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001108 "primNoSleepAlloc: %8u (%4u failed)\n"
1109 "primCanSleepAlloc: %8u (%4u failed)\n"
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001110 "prim2mFree: %8u\n"
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001111 "primFree: %8u\n"
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001112 "err2mAlloc: %8u\n"
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001113 "errAlloc: %8u\n"
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001114 "err2mFree: %8u\n"
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001115 "errFree: %8u\n",
1116 stats->timer,
1117 stats->start, stats->start_fail,
1118 stats->guest_type, stats->guest_type_fail,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001119 stats->lock[true], stats->lock_fail[true],
1120 stats->lock[false], stats->lock_fail[false],
1121 stats->unlock[true], stats->unlock_fail[true],
1122 stats->unlock[false], stats->unlock_fail[false],
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001123 stats->target, stats->target_fail,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001124 stats->alloc[true], stats->alloc_fail[true],
1125 stats->alloc[false], stats->alloc_fail[false],
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001126 stats->sleep_alloc, stats->sleep_alloc_fail,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001127 stats->free[true],
1128 stats->free[false],
1129 stats->refused_alloc[true], stats->refused_alloc[false],
1130 stats->refused_free[true], stats->refused_free[false]);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001131
1132 return 0;
1133}
1134
1135static int vmballoon_debug_open(struct inode *inode, struct file *file)
1136{
1137 return single_open(file, vmballoon_debug_show, inode->i_private);
1138}
1139
1140static const struct file_operations vmballoon_debug_fops = {
1141 .owner = THIS_MODULE,
1142 .open = vmballoon_debug_open,
1143 .read = seq_read,
1144 .llseek = seq_lseek,
1145 .release = single_release,
1146};
1147
1148static int __init vmballoon_debugfs_init(struct vmballoon *b)
1149{
1150 int error;
1151
1152 b->dbg_entry = debugfs_create_file("vmmemctl", S_IRUGO, NULL, b,
1153 &vmballoon_debug_fops);
1154 if (IS_ERR(b->dbg_entry)) {
1155 error = PTR_ERR(b->dbg_entry);
1156 pr_err("failed to create debugfs entry, error: %d\n", error);
1157 return error;
1158 }
1159
1160 return 0;
1161}
1162
1163static void __exit vmballoon_debugfs_exit(struct vmballoon *b)
1164{
1165 debugfs_remove(b->dbg_entry);
1166}
1167
1168#else
1169
1170static inline int vmballoon_debugfs_init(struct vmballoon *b)
1171{
1172 return 0;
1173}
1174
1175static inline void vmballoon_debugfs_exit(struct vmballoon *b)
1176{
1177}
1178
1179#endif /* CONFIG_DEBUG_FS */
1180
1181static int __init vmballoon_init(void)
1182{
1183 int error;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001184 unsigned is_2m_pages;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001185 /*
1186 * Check if we are running on VMware's hypervisor and bail out
1187 * if we are not.
1188 */
H. Peter Anvina10a5692010-05-09 01:13:42 -07001189 if (x86_hyper != &x86_hyper_vmware)
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001190 return -ENODEV;
1191
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001192 for (is_2m_pages = 0; is_2m_pages < VMW_BALLOON_NUM_PAGE_SIZES;
1193 is_2m_pages++) {
1194 INIT_LIST_HEAD(&balloon.page_sizes[is_2m_pages].pages);
1195 INIT_LIST_HEAD(&balloon.page_sizes[is_2m_pages].refused_pages);
1196 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001197
1198 /* initialize rates */
1199 balloon.rate_alloc = VMW_BALLOON_RATE_ALLOC_MAX;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001200
1201 INIT_DELAYED_WORK(&balloon.dwork, vmballoon_work);
1202
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001203 error = vmballoon_debugfs_init(&balloon);
1204 if (error)
Dmitry Torokhovbeda94d2011-07-26 16:08:56 -07001205 return error;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001206
Philip P. Moltmannd7568c12015-08-06 15:18:01 -07001207 balloon.batch_page = NULL;
1208 balloon.page = NULL;
1209 balloon.reset_required = true;
1210
Dmitry Torokhovbeda94d2011-07-26 16:08:56 -07001211 queue_delayed_work(system_freezable_wq, &balloon.dwork, 0);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001212
1213 return 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001214}
1215module_init(vmballoon_init);
1216
1217static void __exit vmballoon_exit(void)
1218{
1219 cancel_delayed_work_sync(&balloon.dwork);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001220
1221 vmballoon_debugfs_exit(&balloon);
1222
1223 /*
1224 * Deallocate all reserved memory, and reset connection with monitor.
1225 * Reset connection before deallocating memory to avoid potential for
1226 * additional spurious resets from guest touching deallocated pages.
1227 */
Philip P. Moltmannd7568c12015-08-06 15:18:01 -07001228 vmballoon_send_start(&balloon, 0);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001229 vmballoon_pop(&balloon);
1230}
1231module_exit(vmballoon_exit);