blob: 01519ff443ddd2a48fe12554eb69088d1a2b0919 [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. Moltmann365bd7e2015-08-06 15:18:01 -070050MODULE_VERSION("1.4.0.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 Deguillardeb791002015-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 Deguillardeb791002015-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 Deguillardeb791002015-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 Deguillardeb791002015-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 Deguillardeb791002015-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 Deguillardeb791002015-06-12 11:43:23 -0700194({ \
Xavier Deguillardf220a802015-08-06 15:17:58 -0700195 unsigned long __status, __dummy1, __dummy2, __dummy3; \
Xavier Deguillardeb791002015-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 Deguillardeb791002015-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 Deguillardeb791002015-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 Deguillard4670de4d2015-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 Deguillard4670de4d2015-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 Deguillard4670de4d2015-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 Deguillard4670de4d2015-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 Deguillard4670de4d2015-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 Deguillard4670de4d2015-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 Deguillard4670de4d2015-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 Deguillard4670de4d2015-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
Xavier Deguillardf220a802015-08-06 15:17:58 -0700567 if ((b->capabilities & VMW_BALLOON_BATCHED_CMDS) != 0) {
568 if (b->batch_page)
569 vunmap(b->batch_page);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400570
Xavier Deguillardf220a802015-08-06 15:17:58 -0700571 if (b->page)
572 __free_page(b->page);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400573 }
574}
575
576/*
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700577 * Notify the host of a ballooned page. If host rejects the page put it on the
578 * refuse list, those refused page are then released at the end of the
579 * inflation cycle.
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400580 */
Xavier Deguillard4670de4d2015-08-06 15:17:59 -0700581static int vmballoon_lock_page(struct vmballoon *b, unsigned int num_pages,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700582 bool is_2m_pages, unsigned int *target)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400583{
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700584 int locked, hv_status;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700585 struct page *page = b->page;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700586 struct vmballoon_page_size *page_size = &b->page_sizes[false];
587
588 /* is_2m_pages can never happen as 2m pages support implies batching */
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400589
Xavier Deguillard4670de4d2015-08-06 15:17:59 -0700590 locked = vmballoon_send_lock_page(b, page_to_pfn(page), &hv_status,
591 target);
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700592 if (locked > 0) {
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700593 STATS_INC(b->stats.refused_alloc[false]);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400594
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700595 if (hv_status == VMW_BALLOON_ERROR_RESET ||
596 hv_status == VMW_BALLOON_ERROR_PPN_NOTNEEDED) {
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700597 vmballoon_free_page(page, false);
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700598 return -EIO;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400599 }
600
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700601 /*
602 * Place page on the list of non-balloonable pages
603 * and retry allocation, unless we already accumulated
604 * too many of them, in which case take a breather.
605 */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700606 if (page_size->n_refused_pages < VMW_BALLOON_MAX_REFUSED) {
607 page_size->n_refused_pages++;
608 list_add(&page->lru, &page_size->refused_pages);
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700609 } else {
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700610 vmballoon_free_page(page, false);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400611 }
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700612 return -EIO;
613 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400614
615 /* track allocated page */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700616 list_add(&page->lru, &page_size->pages);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400617
618 /* update balloon size */
619 b->size++;
620
621 return 0;
622}
623
Xavier Deguillardf220a802015-08-06 15:17:58 -0700624static int vmballoon_lock_batched_page(struct vmballoon *b,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700625 unsigned int num_pages, bool is_2m_pages, unsigned int *target)
Xavier Deguillardf220a802015-08-06 15:17:58 -0700626{
627 int locked, i;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700628 u16 size_per_page = vmballoon_page_size(is_2m_pages);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700629
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700630 locked = vmballoon_send_batched_lock(b, num_pages, is_2m_pages,
631 target);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700632 if (locked > 0) {
633 for (i = 0; i < num_pages; i++) {
634 u64 pa = vmballoon_batch_get_pa(b->batch_page, i);
635 struct page *p = pfn_to_page(pa >> PAGE_SHIFT);
636
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700637 vmballoon_free_page(p, is_2m_pages);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700638 }
639
640 return -EIO;
641 }
642
643 for (i = 0; i < num_pages; i++) {
644 u64 pa = vmballoon_batch_get_pa(b->batch_page, i);
645 struct page *p = pfn_to_page(pa >> PAGE_SHIFT);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700646 struct vmballoon_page_size *page_size =
647 &b->page_sizes[is_2m_pages];
Xavier Deguillardf220a802015-08-06 15:17:58 -0700648
649 locked = vmballoon_batch_get_status(b->batch_page, i);
650
651 switch (locked) {
652 case VMW_BALLOON_SUCCESS:
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700653 list_add(&p->lru, &page_size->pages);
654 b->size += size_per_page;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700655 break;
656 case VMW_BALLOON_ERROR_PPN_PINNED:
657 case VMW_BALLOON_ERROR_PPN_INVALID:
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700658 if (page_size->n_refused_pages
659 < VMW_BALLOON_MAX_REFUSED) {
660 list_add(&p->lru, &page_size->refused_pages);
661 page_size->n_refused_pages++;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700662 break;
663 }
664 /* Fallthrough */
665 case VMW_BALLOON_ERROR_RESET:
666 case VMW_BALLOON_ERROR_PPN_NOTNEEDED:
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700667 vmballoon_free_page(p, is_2m_pages);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700668 break;
669 default:
670 /* This should never happen */
671 WARN_ON_ONCE(true);
672 }
673 }
674
675 return 0;
676}
677
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400678/*
679 * Release the page allocated for the balloon. Note that we first notify
680 * the host so it can make sure the page will be available for the guest
681 * to use, if needed.
682 */
Xavier Deguillard4670de4d2015-08-06 15:17:59 -0700683static int vmballoon_unlock_page(struct vmballoon *b, unsigned int num_pages,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700684 bool is_2m_pages, unsigned int *target)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400685{
Xavier Deguillardf220a802015-08-06 15:17:58 -0700686 struct page *page = b->page;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700687 struct vmballoon_page_size *page_size = &b->page_sizes[false];
688
689 /* is_2m_pages can never happen as 2m pages support implies batching */
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400690
Xavier Deguillard4670de4d2015-08-06 15:17:59 -0700691 if (!vmballoon_send_unlock_page(b, page_to_pfn(page), target)) {
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700692 list_add(&page->lru, &page_size->pages);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700693 return -EIO;
694 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400695
696 /* deallocate page */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700697 vmballoon_free_page(page, false);
698 STATS_INC(b->stats.free[false]);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400699
700 /* update balloon size */
701 b->size--;
702
703 return 0;
704}
705
Xavier Deguillardf220a802015-08-06 15:17:58 -0700706static int vmballoon_unlock_batched_page(struct vmballoon *b,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700707 unsigned int num_pages, bool is_2m_pages,
708 unsigned int *target)
Xavier Deguillardf220a802015-08-06 15:17:58 -0700709{
710 int locked, i, ret = 0;
711 bool hv_success;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700712 u16 size_per_page = vmballoon_page_size(is_2m_pages);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700713
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700714 hv_success = vmballoon_send_batched_unlock(b, num_pages, is_2m_pages,
715 target);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700716 if (!hv_success)
717 ret = -EIO;
718
719 for (i = 0; i < num_pages; i++) {
720 u64 pa = vmballoon_batch_get_pa(b->batch_page, i);
721 struct page *p = pfn_to_page(pa >> PAGE_SHIFT);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700722 struct vmballoon_page_size *page_size =
723 &b->page_sizes[is_2m_pages];
Xavier Deguillardf220a802015-08-06 15:17:58 -0700724
725 locked = vmballoon_batch_get_status(b->batch_page, i);
726 if (!hv_success || locked != VMW_BALLOON_SUCCESS) {
727 /*
728 * That page wasn't successfully unlocked by the
729 * hypervisor, re-add it to the list of pages owned by
730 * the balloon driver.
731 */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700732 list_add(&p->lru, &page_size->pages);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700733 } else {
734 /* deallocate page */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700735 vmballoon_free_page(p, is_2m_pages);
736 STATS_INC(b->stats.free[is_2m_pages]);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700737
738 /* update balloon size */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700739 b->size -= size_per_page;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700740 }
741 }
742
743 return ret;
744}
745
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400746/*
747 * Release pages that were allocated while attempting to inflate the
748 * balloon but were refused by the host for one reason or another.
749 */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700750static void vmballoon_release_refused_pages(struct vmballoon *b,
751 bool is_2m_pages)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400752{
753 struct page *page, *next;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700754 struct vmballoon_page_size *page_size =
755 &b->page_sizes[is_2m_pages];
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400756
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700757 list_for_each_entry_safe(page, next, &page_size->refused_pages, lru) {
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400758 list_del(&page->lru);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700759 vmballoon_free_page(page, is_2m_pages);
760 STATS_INC(b->stats.refused_free[is_2m_pages]);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400761 }
Dmitry Torokhov55adaa42010-06-04 14:14:52 -0700762
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700763 page_size->n_refused_pages = 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400764}
765
Xavier Deguillardf220a802015-08-06 15:17:58 -0700766static void vmballoon_add_page(struct vmballoon *b, int idx, struct page *p)
767{
768 b->page = p;
769}
770
771static void vmballoon_add_batched_page(struct vmballoon *b, int idx,
772 struct page *p)
773{
774 vmballoon_batch_set_pa(b->batch_page, idx,
775 (u64)page_to_pfn(p) << PAGE_SHIFT);
776}
777
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400778/*
779 * Inflate the balloon towards its target size. Note that we try to limit
780 * the rate of allocation to make sure we are not choking the rest of the
781 * system.
782 */
783static void vmballoon_inflate(struct vmballoon *b)
784{
Philip P. Moltmann33d268e2015-08-06 15:18:01 -0700785 unsigned rate;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400786 unsigned int allocations = 0;
Xavier Deguillardf220a802015-08-06 15:17:58 -0700787 unsigned int num_pages = 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400788 int error = 0;
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700789 gfp_t flags = VMW_PAGE_ALLOC_NOSLEEP;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700790 bool is_2m_pages;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400791
792 pr_debug("%s - size: %d, target %d\n", __func__, b->size, b->target);
793
794 /*
795 * First try NOSLEEP page allocations to inflate balloon.
796 *
797 * If we do not throttle nosleep allocations, we can drain all
798 * free pages in the guest quickly (if the balloon target is high).
799 * As a side-effect, draining free pages helps to inform (force)
800 * the guest to start swapping if balloon target is not met yet,
801 * which is a desired behavior. However, balloon driver can consume
802 * all available CPU cycles if too many pages are allocated in a
803 * second. Therefore, we throttle nosleep allocations even when
804 * the guest is not under memory pressure. OTOH, if we have already
805 * predicted that the guest is under memory pressure, then we
806 * slowdown page allocations considerably.
807 */
808
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400809 /*
810 * Start with no sleep allocation rate which may be higher
811 * than sleeping allocation rate.
812 */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700813 if (b->slow_allocation_cycles) {
814 rate = b->rate_alloc;
815 is_2m_pages = false;
816 } else {
817 rate = UINT_MAX;
818 is_2m_pages =
819 b->supported_page_sizes == VMW_BALLOON_NUM_PAGE_SIZES;
820 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400821
Philip P. Moltmann33d268e2015-08-06 15:18:01 -0700822 pr_debug("%s - goal: %d, no-sleep rate: %u, sleep rate: %d\n",
Xavier Deguillard4670de4d2015-08-06 15:17:59 -0700823 __func__, b->target - b->size, rate, b->rate_alloc);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400824
Philip P. Moltmann33d268e2015-08-06 15:18:01 -0700825 while (!b->reset_required &&
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700826 b->size + num_pages * vmballoon_page_size(is_2m_pages)
827 < b->target) {
Xavier Deguillard4670de4d2015-08-06 15:17:59 -0700828 struct page *page;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400829
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700830 if (flags == VMW_PAGE_ALLOC_NOSLEEP)
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700831 STATS_INC(b->stats.alloc[is_2m_pages]);
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700832 else
833 STATS_INC(b->stats.sleep_alloc);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400834
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700835 page = vmballoon_alloc_page(flags, is_2m_pages);
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700836 if (!page) {
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700837 STATS_INC(b->stats.alloc_fail[is_2m_pages]);
838
839 if (is_2m_pages) {
840 b->ops->lock(b, num_pages, true, &b->target);
841
842 /*
843 * ignore errors from locking as we now switch
844 * to 4k pages and we might get different
845 * errors.
846 */
847
848 num_pages = 0;
849 is_2m_pages = false;
850 continue;
851 }
852
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700853 if (flags == VMW_PAGE_ALLOC_CANSLEEP) {
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400854 /*
855 * CANSLEEP page allocation failed, so guest
856 * is under severe memory pressure. Quickly
857 * decrease allocation rate.
858 */
859 b->rate_alloc = max(b->rate_alloc / 2,
860 VMW_BALLOON_RATE_ALLOC_MIN);
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700861 STATS_INC(b->stats.sleep_alloc_fail);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400862 break;
863 }
864
865 /*
866 * NOSLEEP page allocation failed, so the guest is
867 * under memory pressure. Let us slow down page
868 * allocations for next few cycles so that the guest
869 * gets out of memory pressure. Also, if we already
870 * allocated b->rate_alloc pages, let's pause,
871 * otherwise switch to sleeping allocations.
872 */
873 b->slow_allocation_cycles = VMW_BALLOON_SLOW_CYCLES;
874
Xavier Deguillard4670de4d2015-08-06 15:17:59 -0700875 if (allocations >= b->rate_alloc)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400876 break;
877
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700878 flags = VMW_PAGE_ALLOC_CANSLEEP;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400879 /* Lower rate for sleeping allocations. */
880 rate = b->rate_alloc;
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700881 continue;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400882 }
883
Xavier Deguillardf220a802015-08-06 15:17:58 -0700884 b->ops->add_page(b, num_pages++, page);
885 if (num_pages == b->batch_max_pages) {
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700886 error = b->ops->lock(b, num_pages, is_2m_pages,
887 &b->target);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700888 num_pages = 0;
889 if (error)
890 break;
891 }
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700892
Philip P. Moltmann33d268e2015-08-06 15:18:01 -0700893 cond_resched();
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400894
Xavier Deguillard4670de4d2015-08-06 15:17:59 -0700895 if (allocations >= rate) {
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400896 /* We allocated enough pages, let's take a break. */
897 break;
898 }
899 }
900
Xavier Deguillardf220a802015-08-06 15:17:58 -0700901 if (num_pages > 0)
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700902 b->ops->lock(b, num_pages, is_2m_pages, &b->target);
Xavier Deguillardf220a802015-08-06 15:17:58 -0700903
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400904 /*
905 * We reached our goal without failures so try increasing
906 * allocation rate.
907 */
Xavier Deguillard4670de4d2015-08-06 15:17:59 -0700908 if (error == 0 && allocations >= b->rate_alloc) {
909 unsigned int mult = allocations / b->rate_alloc;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400910
911 b->rate_alloc =
912 min(b->rate_alloc + mult * VMW_BALLOON_RATE_ALLOC_INC,
913 VMW_BALLOON_RATE_ALLOC_MAX);
914 }
915
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700916 vmballoon_release_refused_pages(b, true);
917 vmballoon_release_refused_pages(b, false);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400918}
919
920/*
921 * Decrease the size of the balloon allowing guest to use more memory.
922 */
923static void vmballoon_deflate(struct vmballoon *b)
924{
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700925 unsigned is_2m_pages;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400926
Philip P. Moltmann33d268e2015-08-06 15:18:01 -0700927 pr_debug("%s - size: %d, target %d\n", __func__, b->size, b->target);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400928
929 /* free pages to reach target */
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700930 for (is_2m_pages = 0; is_2m_pages < b->supported_page_sizes;
931 is_2m_pages++) {
932 struct page *page, *next;
933 unsigned int num_pages = 0;
934 struct vmballoon_page_size *page_size =
935 &b->page_sizes[is_2m_pages];
Xavier Deguillardf220a802015-08-06 15:17:58 -0700936
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700937 list_for_each_entry_safe(page, next, &page_size->pages, lru) {
938 if (b->reset_required ||
939 (b->target > 0 &&
940 b->size - num_pages
941 * vmballoon_page_size(is_2m_pages)
942 < b->target + vmballoon_page_size(true)))
943 break;
Philip P. Moltmann33d268e2015-08-06 15:18:01 -0700944
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700945 list_del(&page->lru);
946 b->ops->add_page(b, num_pages++, page);
947
948 if (num_pages == b->batch_max_pages) {
949 int error;
950
951 error = b->ops->unlock(b, num_pages,
952 is_2m_pages, &b->target);
953 num_pages = 0;
954 if (error)
955 return;
956 }
957
958 cond_resched();
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400959 }
960
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -0700961 if (num_pages > 0)
962 b->ops->unlock(b, num_pages, is_2m_pages, &b->target);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400963 }
Xavier Deguillardf220a802015-08-06 15:17:58 -0700964}
965
966static const struct vmballoon_ops vmballoon_basic_ops = {
967 .add_page = vmballoon_add_page,
968 .lock = vmballoon_lock_page,
969 .unlock = vmballoon_unlock_page
970};
971
972static const struct vmballoon_ops vmballoon_batched_ops = {
973 .add_page = vmballoon_add_batched_page,
974 .lock = vmballoon_lock_batched_page,
975 .unlock = vmballoon_unlock_batched_page
976};
977
978static bool vmballoon_init_batching(struct vmballoon *b)
979{
980 b->page = alloc_page(VMW_PAGE_ALLOC_NOSLEEP);
981 if (!b->page)
982 return false;
983
984 b->batch_page = vmap(&b->page, 1, VM_MAP, PAGE_KERNEL);
985 if (!b->batch_page) {
986 __free_page(b->page);
987 return false;
988 }
989
990 return true;
991}
992
993/*
994 * Perform standard reset sequence by popping the balloon (in case it
995 * is not empty) and then restarting protocol. This operation normally
996 * happens when host responds with VMW_BALLOON_ERROR_RESET to a command.
997 */
998static void vmballoon_reset(struct vmballoon *b)
999{
1000 /* free all pages, skipping monitor unlock */
1001 vmballoon_pop(b);
1002
1003 if (!vmballoon_send_start(b, VMW_BALLOON_CAPABILITIES))
1004 return;
1005
1006 if ((b->capabilities & VMW_BALLOON_BATCHED_CMDS) != 0) {
1007 b->ops = &vmballoon_batched_ops;
1008 b->batch_max_pages = VMW_BALLOON_BATCH_MAX_PAGES;
1009 if (!vmballoon_init_batching(b)) {
1010 /*
1011 * We failed to initialize batching, inform the monitor
1012 * about it by sending a null capability.
1013 *
1014 * The guest will retry in one second.
1015 */
1016 vmballoon_send_start(b, 0);
1017 return;
1018 }
1019 } else if ((b->capabilities & VMW_BALLOON_BASIC_CMDS) != 0) {
1020 b->ops = &vmballoon_basic_ops;
1021 b->batch_max_pages = 1;
1022 }
1023
1024 b->reset_required = false;
1025 if (!vmballoon_send_guest_id(b))
1026 pr_err("failed to send guest ID to the host\n");
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001027}
1028
1029/*
1030 * Balloon work function: reset protocol, if needed, get the new size and
1031 * adjust balloon as needed. Repeat in 1 sec.
1032 */
1033static void vmballoon_work(struct work_struct *work)
1034{
1035 struct delayed_work *dwork = to_delayed_work(work);
1036 struct vmballoon *b = container_of(dwork, struct vmballoon, dwork);
1037 unsigned int target;
1038
1039 STATS_INC(b->stats.timer);
1040
1041 if (b->reset_required)
1042 vmballoon_reset(b);
1043
1044 if (b->slow_allocation_cycles > 0)
1045 b->slow_allocation_cycles--;
1046
1047 if (vmballoon_send_get_target(b, &target)) {
1048 /* update target, adjust size */
1049 b->target = target;
1050
1051 if (b->size < target)
1052 vmballoon_inflate(b);
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001053 else if (target == 0 ||
1054 b->size > target + vmballoon_page_size(true))
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001055 vmballoon_deflate(b);
1056 }
1057
Dmitry Torokhovbeda94d2011-07-26 16:08:56 -07001058 /*
1059 * We are using a freezable workqueue so that balloon operations are
1060 * stopped while the system transitions to/from sleep/hibernation.
1061 */
1062 queue_delayed_work(system_freezable_wq,
1063 dwork, round_jiffies_relative(HZ));
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001064}
1065
1066/*
1067 * DEBUGFS Interface
1068 */
1069#ifdef CONFIG_DEBUG_FS
1070
1071static int vmballoon_debug_show(struct seq_file *f, void *offset)
1072{
1073 struct vmballoon *b = f->private;
1074 struct vmballoon_stats *stats = &b->stats;
1075
Philip P. Moltmannb36e89d2015-08-06 15:18:00 -07001076 /* format capabilities info */
1077 seq_printf(f,
1078 "balloon capabilities: %#4x\n"
1079 "used capabilities: %#4lx\n",
1080 VMW_BALLOON_CAPABILITIES, b->capabilities);
1081
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001082 /* format size info */
1083 seq_printf(f,
1084 "target: %8d pages\n"
1085 "current: %8d pages\n",
1086 b->target, b->size);
1087
1088 /* format rate info */
1089 seq_printf(f,
Philip P. Moltmann33d268e2015-08-06 15:18:01 -07001090 "rateSleepAlloc: %8d pages/sec\n",
1091 b->rate_alloc);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001092
1093 seq_printf(f,
1094 "\n"
1095 "timer: %8u\n"
1096 "start: %8u (%4u failed)\n"
1097 "guestType: %8u (%4u failed)\n"
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001098 "2m-lock: %8u (%4u failed)\n"
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001099 "lock: %8u (%4u failed)\n"
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001100 "2m-unlock: %8u (%4u failed)\n"
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001101 "unlock: %8u (%4u failed)\n"
1102 "target: %8u (%4u failed)\n"
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001103 "prim2mAlloc: %8u (%4u failed)\n"
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001104 "primNoSleepAlloc: %8u (%4u failed)\n"
1105 "primCanSleepAlloc: %8u (%4u failed)\n"
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001106 "prim2mFree: %8u\n"
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001107 "primFree: %8u\n"
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001108 "err2mAlloc: %8u\n"
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001109 "errAlloc: %8u\n"
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001110 "err2mFree: %8u\n"
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001111 "errFree: %8u\n",
1112 stats->timer,
1113 stats->start, stats->start_fail,
1114 stats->guest_type, stats->guest_type_fail,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001115 stats->lock[true], stats->lock_fail[true],
1116 stats->lock[false], stats->lock_fail[false],
1117 stats->unlock[true], stats->unlock_fail[true],
1118 stats->unlock[false], stats->unlock_fail[false],
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001119 stats->target, stats->target_fail,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001120 stats->alloc[true], stats->alloc_fail[true],
1121 stats->alloc[false], stats->alloc_fail[false],
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001122 stats->sleep_alloc, stats->sleep_alloc_fail,
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001123 stats->free[true],
1124 stats->free[false],
1125 stats->refused_alloc[true], stats->refused_alloc[false],
1126 stats->refused_free[true], stats->refused_free[false]);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001127
1128 return 0;
1129}
1130
1131static int vmballoon_debug_open(struct inode *inode, struct file *file)
1132{
1133 return single_open(file, vmballoon_debug_show, inode->i_private);
1134}
1135
1136static const struct file_operations vmballoon_debug_fops = {
1137 .owner = THIS_MODULE,
1138 .open = vmballoon_debug_open,
1139 .read = seq_read,
1140 .llseek = seq_lseek,
1141 .release = single_release,
1142};
1143
1144static int __init vmballoon_debugfs_init(struct vmballoon *b)
1145{
1146 int error;
1147
1148 b->dbg_entry = debugfs_create_file("vmmemctl", S_IRUGO, NULL, b,
1149 &vmballoon_debug_fops);
1150 if (IS_ERR(b->dbg_entry)) {
1151 error = PTR_ERR(b->dbg_entry);
1152 pr_err("failed to create debugfs entry, error: %d\n", error);
1153 return error;
1154 }
1155
1156 return 0;
1157}
1158
1159static void __exit vmballoon_debugfs_exit(struct vmballoon *b)
1160{
1161 debugfs_remove(b->dbg_entry);
1162}
1163
1164#else
1165
1166static inline int vmballoon_debugfs_init(struct vmballoon *b)
1167{
1168 return 0;
1169}
1170
1171static inline void vmballoon_debugfs_exit(struct vmballoon *b)
1172{
1173}
1174
1175#endif /* CONFIG_DEBUG_FS */
1176
1177static int __init vmballoon_init(void)
1178{
1179 int error;
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001180 unsigned is_2m_pages;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001181 /*
1182 * Check if we are running on VMware's hypervisor and bail out
1183 * if we are not.
1184 */
H. Peter Anvina10a5692010-05-09 01:13:42 -07001185 if (x86_hyper != &x86_hyper_vmware)
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001186 return -ENODEV;
1187
Philip P. Moltmann365bd7e2015-08-06 15:18:01 -07001188 for (is_2m_pages = 0; is_2m_pages < VMW_BALLOON_NUM_PAGE_SIZES;
1189 is_2m_pages++) {
1190 INIT_LIST_HEAD(&balloon.page_sizes[is_2m_pages].pages);
1191 INIT_LIST_HEAD(&balloon.page_sizes[is_2m_pages].refused_pages);
1192 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001193
1194 /* initialize rates */
1195 balloon.rate_alloc = VMW_BALLOON_RATE_ALLOC_MAX;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001196
1197 INIT_DELAYED_WORK(&balloon.dwork, vmballoon_work);
1198
1199 /*
1200 * Start balloon.
1201 */
Xavier Deguillardf220a802015-08-06 15:17:58 -07001202 if (!vmballoon_send_start(&balloon, VMW_BALLOON_CAPABILITIES)) {
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001203 pr_err("failed to send start command to the host\n");
Dmitry Torokhovbeda94d2011-07-26 16:08:56 -07001204 return -EIO;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001205 }
1206
Xavier Deguillardf220a802015-08-06 15:17:58 -07001207 if ((balloon.capabilities & VMW_BALLOON_BATCHED_CMDS) != 0) {
1208 balloon.ops = &vmballoon_batched_ops;
1209 balloon.batch_max_pages = VMW_BALLOON_BATCH_MAX_PAGES;
1210 if (!vmballoon_init_batching(&balloon)) {
1211 pr_err("failed to init batching\n");
1212 return -EIO;
1213 }
1214 } else if ((balloon.capabilities & VMW_BALLOON_BASIC_CMDS) != 0) {
1215 balloon.ops = &vmballoon_basic_ops;
1216 balloon.batch_max_pages = 1;
1217 }
1218
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001219 if (!vmballoon_send_guest_id(&balloon)) {
1220 pr_err("failed to send guest ID to the host\n");
Dmitry Torokhovbeda94d2011-07-26 16:08:56 -07001221 return -EIO;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001222 }
1223
1224 error = vmballoon_debugfs_init(&balloon);
1225 if (error)
Dmitry Torokhovbeda94d2011-07-26 16:08:56 -07001226 return error;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001227
Dmitry Torokhovbeda94d2011-07-26 16:08:56 -07001228 queue_delayed_work(system_freezable_wq, &balloon.dwork, 0);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001229
1230 return 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001231}
1232module_init(vmballoon_init);
1233
1234static void __exit vmballoon_exit(void)
1235{
1236 cancel_delayed_work_sync(&balloon.dwork);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001237
1238 vmballoon_debugfs_exit(&balloon);
1239
1240 /*
1241 * Deallocate all reserved memory, and reset connection with monitor.
1242 * Reset connection before deallocating memory to avoid potential for
1243 * additional spurious resets from guest touching deallocated pages.
1244 */
Xavier Deguillardf220a802015-08-06 15:17:58 -07001245 vmballoon_send_start(&balloon, VMW_BALLOON_CAPABILITIES);
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001246 vmballoon_pop(&balloon);
1247}
1248module_exit(vmballoon_exit);