blob: ffb56340d0c7c28bf2daaf0f512ae835fa659f38 [file] [log] [blame]
Dmitry Torokhov453dc652010-04-23 13:18:08 -04001/*
2 * VMware Balloon driver.
3 *
4 * Copyright (C) 2000-2010, VMware, Inc. All Rights Reserved.
5 *
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>
40#include <linux/sched.h>
41#include <linux/module.h>
42#include <linux/workqueue.h>
43#include <linux/debugfs.h>
44#include <linux/seq_file.h>
H. Peter Anvina10a5692010-05-09 01:13:42 -070045#include <asm/hypervisor.h>
Dmitry Torokhov453dc652010-04-23 13:18:08 -040046
47MODULE_AUTHOR("VMware, Inc.");
48MODULE_DESCRIPTION("VMware Memory Control (Balloon) Driver");
Xavier Deguillardeb79100f2015-06-12 11:43:23 -070049MODULE_VERSION("1.3.0.0-k");
Dmitry Torokhov453dc652010-04-23 13:18:08 -040050MODULE_ALIAS("dmi:*:svnVMware*:*");
51MODULE_ALIAS("vmware_vmmemctl");
52MODULE_LICENSE("GPL");
53
54/*
55 * Various constants controlling rate of inflaint/deflating balloon,
56 * measured in pages.
57 */
58
59/*
60 * Rate of allocating memory when there is no memory pressure
61 * (driver performs non-sleeping allocations).
62 */
63#define VMW_BALLOON_NOSLEEP_ALLOC_MAX 16384U
64
65/*
66 * Rates of memory allocaton when guest experiences memory pressure
67 * (driver performs sleeping allocations).
68 */
69#define VMW_BALLOON_RATE_ALLOC_MIN 512U
70#define VMW_BALLOON_RATE_ALLOC_MAX 2048U
71#define VMW_BALLOON_RATE_ALLOC_INC 16U
72
73/*
74 * Rates for releasing pages while deflating balloon.
75 */
76#define VMW_BALLOON_RATE_FREE_MIN 512U
77#define VMW_BALLOON_RATE_FREE_MAX 16384U
78#define VMW_BALLOON_RATE_FREE_INC 16U
79
80/*
81 * When guest is under memory pressure, use a reduced page allocation
82 * rate for next several cycles.
83 */
84#define VMW_BALLOON_SLOW_CYCLES 4
85
86/*
87 * Use __GFP_HIGHMEM to allow pages from HIGHMEM zone. We don't
88 * allow wait (__GFP_WAIT) for NOSLEEP page allocations. Use
89 * __GFP_NOWARN, to suppress page allocation failure warnings.
90 */
91#define VMW_PAGE_ALLOC_NOSLEEP (__GFP_HIGHMEM|__GFP_NOWARN)
92
93/*
94 * Use GFP_HIGHUSER when executing in a separate kernel thread
95 * context and allocation can sleep. This is less stressful to
96 * the guest memory system, since it allows the thread to block
97 * while memory is reclaimed, and won't take pages from emergency
98 * low-memory pools.
99 */
100#define VMW_PAGE_ALLOC_CANSLEEP (GFP_HIGHUSER)
101
102/* Maximum number of page allocations without yielding processor */
103#define VMW_BALLOON_YIELD_THRESHOLD 1024
104
Dmitry Torokhov55adaa42010-06-04 14:14:52 -0700105/* Maximum number of refused pages we accumulate during inflation cycle */
106#define VMW_BALLOON_MAX_REFUSED 16
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400107
108/*
109 * Hypervisor communication port definitions.
110 */
111#define VMW_BALLOON_HV_PORT 0x5670
112#define VMW_BALLOON_HV_MAGIC 0x456c6d6f
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400113#define VMW_BALLOON_GUEST_ID 1 /* Linux */
114
Xavier Deguillardeb79100f2015-06-12 11:43:23 -0700115enum vmwballoon_capabilities {
116 /*
117 * Bit 0 is reserved and not associated to any capability.
118 */
119 VMW_BALLOON_BASIC_CMDS = (1 << 1),
120 VMW_BALLOON_BATCHED_CMDS = (1 << 2)
121};
122
123#define VMW_BALLOON_CAPABILITIES (VMW_BALLOON_BASIC_CMDS)
124
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400125#define VMW_BALLOON_CMD_START 0
126#define VMW_BALLOON_CMD_GET_TARGET 1
127#define VMW_BALLOON_CMD_LOCK 2
128#define VMW_BALLOON_CMD_UNLOCK 3
129#define VMW_BALLOON_CMD_GUEST_ID 4
130
131/* error codes */
Xavier Deguillardeb79100f2015-06-12 11:43:23 -0700132#define VMW_BALLOON_SUCCESS 0
133#define VMW_BALLOON_FAILURE -1
134#define VMW_BALLOON_ERROR_CMD_INVALID 1
135#define VMW_BALLOON_ERROR_PPN_INVALID 2
136#define VMW_BALLOON_ERROR_PPN_LOCKED 3
137#define VMW_BALLOON_ERROR_PPN_UNLOCKED 4
138#define VMW_BALLOON_ERROR_PPN_PINNED 5
139#define VMW_BALLOON_ERROR_PPN_NOTNEEDED 6
140#define VMW_BALLOON_ERROR_RESET 7
141#define VMW_BALLOON_ERROR_BUSY 8
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400142
Xavier Deguillardeb79100f2015-06-12 11:43:23 -0700143#define VMW_BALLOON_SUCCESS_WITH_CAPABILITIES (0x03000000)
144
145#define VMWARE_BALLOON_CMD(cmd, data, result) \
146({ \
147 unsigned long __status, __dummy1, __dummy2; \
148 __asm__ __volatile__ ("inl %%dx" : \
149 "=a"(__status), \
150 "=c"(__dummy1), \
151 "=d"(__dummy2), \
152 "=b"(result) : \
153 "0"(VMW_BALLOON_HV_MAGIC), \
154 "1"(VMW_BALLOON_CMD_##cmd), \
155 "2"(VMW_BALLOON_HV_PORT), \
156 "3"(data) : \
157 "memory"); \
158 if (VMW_BALLOON_CMD_##cmd == VMW_BALLOON_CMD_START) \
159 result = __dummy1; \
160 result &= -1UL; \
161 __status & -1UL; \
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400162})
163
164#ifdef CONFIG_DEBUG_FS
165struct vmballoon_stats {
166 unsigned int timer;
167
Rakib Mullick2ca02df2011-11-02 13:40:07 -0700168 /* allocation statistics */
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400169 unsigned int alloc;
170 unsigned int alloc_fail;
171 unsigned int sleep_alloc;
172 unsigned int sleep_alloc_fail;
173 unsigned int refused_alloc;
174 unsigned int refused_free;
175 unsigned int free;
176
177 /* monitor operations */
178 unsigned int lock;
179 unsigned int lock_fail;
180 unsigned int unlock;
181 unsigned int unlock_fail;
182 unsigned int target;
183 unsigned int target_fail;
184 unsigned int start;
185 unsigned int start_fail;
186 unsigned int guest_type;
187 unsigned int guest_type_fail;
188};
189
190#define STATS_INC(stat) (stat)++
191#else
192#define STATS_INC(stat)
193#endif
194
195struct vmballoon {
196
197 /* list of reserved physical pages */
198 struct list_head pages;
199
200 /* transient list of non-balloonable pages */
201 struct list_head refused_pages;
Dmitry Torokhov55adaa42010-06-04 14:14:52 -0700202 unsigned int n_refused_pages;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400203
204 /* balloon size in pages */
205 unsigned int size;
206 unsigned int target;
207
208 /* reset flag */
209 bool reset_required;
210
211 /* adjustment rates (pages per second) */
212 unsigned int rate_alloc;
213 unsigned int rate_free;
214
215 /* slowdown page allocations for next few cycles */
216 unsigned int slow_allocation_cycles;
217
218#ifdef CONFIG_DEBUG_FS
219 /* statistics */
220 struct vmballoon_stats stats;
221
222 /* debugfs file exporting statistics */
223 struct dentry *dbg_entry;
224#endif
225
226 struct sysinfo sysinfo;
227
228 struct delayed_work dwork;
229};
230
231static struct vmballoon balloon;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400232
233/*
234 * Send "start" command to the host, communicating supported version
235 * of the protocol.
236 */
237static bool vmballoon_send_start(struct vmballoon *b)
238{
Xavier Deguillardeb79100f2015-06-12 11:43:23 -0700239 unsigned long status, capabilities;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400240
241 STATS_INC(b->stats.start);
242
Xavier Deguillardeb79100f2015-06-12 11:43:23 -0700243 status = VMWARE_BALLOON_CMD(START, VMW_BALLOON_CAPABILITIES,
244 capabilities);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400245 if (status == VMW_BALLOON_SUCCESS)
246 return true;
247
248 pr_debug("%s - failed, hv returns %ld\n", __func__, status);
249 STATS_INC(b->stats.start_fail);
250 return false;
251}
252
253static bool vmballoon_check_status(struct vmballoon *b, unsigned long status)
254{
255 switch (status) {
256 case VMW_BALLOON_SUCCESS:
257 return true;
258
259 case VMW_BALLOON_ERROR_RESET:
260 b->reset_required = true;
261 /* fall through */
262
263 default:
264 return false;
265 }
266}
267
268/*
269 * Communicate guest type to the host so that it can adjust ballooning
270 * algorithm to the one most appropriate for the guest. This command
271 * is normally issued after sending "start" command and is part of
272 * standard reset sequence.
273 */
274static bool vmballoon_send_guest_id(struct vmballoon *b)
275{
276 unsigned long status, dummy;
277
278 status = VMWARE_BALLOON_CMD(GUEST_ID, VMW_BALLOON_GUEST_ID, dummy);
279
280 STATS_INC(b->stats.guest_type);
281
282 if (vmballoon_check_status(b, status))
283 return true;
284
285 pr_debug("%s - failed, hv returns %ld\n", __func__, status);
286 STATS_INC(b->stats.guest_type_fail);
287 return false;
288}
289
290/*
291 * Retrieve desired balloon size from the host.
292 */
293static bool vmballoon_send_get_target(struct vmballoon *b, u32 *new_target)
294{
295 unsigned long status;
296 unsigned long target;
297 unsigned long limit;
298 u32 limit32;
299
300 /*
301 * si_meminfo() is cheap. Moreover, we want to provide dynamic
302 * max balloon size later. So let us call si_meminfo() every
303 * iteration.
304 */
305 si_meminfo(&b->sysinfo);
306 limit = b->sysinfo.totalram;
307
308 /* Ensure limit fits in 32-bits */
309 limit32 = (u32)limit;
310 if (limit != limit32)
311 return false;
312
313 /* update stats */
314 STATS_INC(b->stats.target);
315
316 status = VMWARE_BALLOON_CMD(GET_TARGET, limit, target);
317 if (vmballoon_check_status(b, status)) {
318 *new_target = target;
319 return true;
320 }
321
322 pr_debug("%s - failed, hv returns %ld\n", __func__, status);
323 STATS_INC(b->stats.target_fail);
324 return false;
325}
326
327/*
328 * Notify the host about allocated page so that host can use it without
329 * fear that guest will need it. Host may reject some pages, we need to
330 * check the return value and maybe submit a different page.
331 */
Danny Kukawka3e5ba462012-01-30 23:00:08 +0100332static int vmballoon_send_lock_page(struct vmballoon *b, unsigned long pfn,
Dmitry Torokhovd27a0c02011-01-12 17:01:07 -0800333 unsigned int *hv_status)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400334{
335 unsigned long status, dummy;
336 u32 pfn32;
337
338 pfn32 = (u32)pfn;
339 if (pfn32 != pfn)
Danny Kukawka3e5ba462012-01-30 23:00:08 +0100340 return -1;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400341
342 STATS_INC(b->stats.lock);
343
Dmitry Torokhovd27a0c02011-01-12 17:01:07 -0800344 *hv_status = status = VMWARE_BALLOON_CMD(LOCK, pfn, dummy);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400345 if (vmballoon_check_status(b, status))
Danny Kukawka3e5ba462012-01-30 23:00:08 +0100346 return 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400347
348 pr_debug("%s - ppn %lx, hv returns %ld\n", __func__, pfn, status);
349 STATS_INC(b->stats.lock_fail);
Danny Kukawka3e5ba462012-01-30 23:00:08 +0100350 return 1;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400351}
352
353/*
354 * Notify the host that guest intends to release given page back into
355 * the pool of available (to the guest) pages.
356 */
357static bool vmballoon_send_unlock_page(struct vmballoon *b, unsigned long pfn)
358{
359 unsigned long status, dummy;
360 u32 pfn32;
361
362 pfn32 = (u32)pfn;
363 if (pfn32 != pfn)
364 return false;
365
366 STATS_INC(b->stats.unlock);
367
368 status = VMWARE_BALLOON_CMD(UNLOCK, pfn, dummy);
369 if (vmballoon_check_status(b, status))
370 return true;
371
372 pr_debug("%s - ppn %lx, hv returns %ld\n", __func__, pfn, status);
373 STATS_INC(b->stats.unlock_fail);
374 return false;
375}
376
377/*
378 * Quickly release all pages allocated for the balloon. This function is
379 * called when host decides to "reset" balloon for one reason or another.
380 * Unlike normal "deflate" we do not (shall not) notify host of the pages
381 * being released.
382 */
383static void vmballoon_pop(struct vmballoon *b)
384{
385 struct page *page, *next;
386 unsigned int count = 0;
387
388 list_for_each_entry_safe(page, next, &b->pages, lru) {
389 list_del(&page->lru);
390 __free_page(page);
391 STATS_INC(b->stats.free);
392 b->size--;
393
394 if (++count >= b->rate_free) {
395 count = 0;
396 cond_resched();
397 }
398 }
399}
400
401/*
402 * Perform standard reset sequence by popping the balloon (in case it
403 * is not empty) and then restarting protocol. This operation normally
404 * happens when host responds with VMW_BALLOON_ERROR_RESET to a command.
405 */
406static void vmballoon_reset(struct vmballoon *b)
407{
408 /* free all pages, skipping monitor unlock */
409 vmballoon_pop(b);
410
411 if (vmballoon_send_start(b)) {
412 b->reset_required = false;
413 if (!vmballoon_send_guest_id(b))
414 pr_err("failed to send guest ID to the host\n");
415 }
416}
417
418/*
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700419 * Notify the host of a ballooned page. If host rejects the page put it on the
420 * refuse list, those refused page are then released at the end of the
421 * inflation cycle.
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400422 */
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700423static int vmballoon_lock_page(struct vmballoon *b, struct page *page)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400424{
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700425 int locked, hv_status;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400426
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700427 locked = vmballoon_send_lock_page(b, page_to_pfn(page), &hv_status);
428 if (locked > 0) {
429 STATS_INC(b->stats.refused_alloc);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400430
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700431 if (hv_status == VMW_BALLOON_ERROR_RESET ||
432 hv_status == VMW_BALLOON_ERROR_PPN_NOTNEEDED) {
433 __free_page(page);
434 return -EIO;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400435 }
436
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700437 /*
438 * Place page on the list of non-balloonable pages
439 * and retry allocation, unless we already accumulated
440 * too many of them, in which case take a breather.
441 */
442 if (b->n_refused_pages < VMW_BALLOON_MAX_REFUSED) {
443 b->n_refused_pages++;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400444 list_add(&page->lru, &b->refused_pages);
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700445 } else {
446 __free_page(page);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400447 }
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700448 return -EIO;
449 }
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400450
451 /* track allocated page */
452 list_add(&page->lru, &b->pages);
453
454 /* update balloon size */
455 b->size++;
456
457 return 0;
458}
459
460/*
461 * Release the page allocated for the balloon. Note that we first notify
462 * the host so it can make sure the page will be available for the guest
463 * to use, if needed.
464 */
465static int vmballoon_release_page(struct vmballoon *b, struct page *page)
466{
467 if (!vmballoon_send_unlock_page(b, page_to_pfn(page)))
468 return -EIO;
469
470 list_del(&page->lru);
471
472 /* deallocate page */
473 __free_page(page);
474 STATS_INC(b->stats.free);
475
476 /* update balloon size */
477 b->size--;
478
479 return 0;
480}
481
482/*
483 * Release pages that were allocated while attempting to inflate the
484 * balloon but were refused by the host for one reason or another.
485 */
486static void vmballoon_release_refused_pages(struct vmballoon *b)
487{
488 struct page *page, *next;
489
490 list_for_each_entry_safe(page, next, &b->refused_pages, lru) {
491 list_del(&page->lru);
492 __free_page(page);
493 STATS_INC(b->stats.refused_free);
494 }
Dmitry Torokhov55adaa42010-06-04 14:14:52 -0700495
496 b->n_refused_pages = 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400497}
498
499/*
500 * Inflate the balloon towards its target size. Note that we try to limit
501 * the rate of allocation to make sure we are not choking the rest of the
502 * system.
503 */
504static void vmballoon_inflate(struct vmballoon *b)
505{
506 unsigned int goal;
507 unsigned int rate;
508 unsigned int i;
509 unsigned int allocations = 0;
510 int error = 0;
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700511 gfp_t flags = VMW_PAGE_ALLOC_NOSLEEP;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400512
513 pr_debug("%s - size: %d, target %d\n", __func__, b->size, b->target);
514
515 /*
516 * First try NOSLEEP page allocations to inflate balloon.
517 *
518 * If we do not throttle nosleep allocations, we can drain all
519 * free pages in the guest quickly (if the balloon target is high).
520 * As a side-effect, draining free pages helps to inform (force)
521 * the guest to start swapping if balloon target is not met yet,
522 * which is a desired behavior. However, balloon driver can consume
523 * all available CPU cycles if too many pages are allocated in a
524 * second. Therefore, we throttle nosleep allocations even when
525 * the guest is not under memory pressure. OTOH, if we have already
526 * predicted that the guest is under memory pressure, then we
527 * slowdown page allocations considerably.
528 */
529
530 goal = b->target - b->size;
531 /*
532 * Start with no sleep allocation rate which may be higher
533 * than sleeping allocation rate.
534 */
535 rate = b->slow_allocation_cycles ?
536 b->rate_alloc : VMW_BALLOON_NOSLEEP_ALLOC_MAX;
537
538 pr_debug("%s - goal: %d, no-sleep rate: %d, sleep rate: %d\n",
539 __func__, goal, rate, b->rate_alloc);
540
541 for (i = 0; i < goal; i++) {
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700542 struct page *page;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400543
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700544 if (flags == VMW_PAGE_ALLOC_NOSLEEP)
545 STATS_INC(b->stats.alloc);
546 else
547 STATS_INC(b->stats.sleep_alloc);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400548
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700549 page = alloc_page(flags);
550 if (!page) {
551 if (flags == VMW_PAGE_ALLOC_CANSLEEP) {
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400552 /*
553 * CANSLEEP page allocation failed, so guest
554 * is under severe memory pressure. Quickly
555 * decrease allocation rate.
556 */
557 b->rate_alloc = max(b->rate_alloc / 2,
558 VMW_BALLOON_RATE_ALLOC_MIN);
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700559 STATS_INC(b->stats.sleep_alloc_fail);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400560 break;
561 }
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700562 STATS_INC(b->stats.alloc_fail);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400563
564 /*
565 * NOSLEEP page allocation failed, so the guest is
566 * under memory pressure. Let us slow down page
567 * allocations for next few cycles so that the guest
568 * gets out of memory pressure. Also, if we already
569 * allocated b->rate_alloc pages, let's pause,
570 * otherwise switch to sleeping allocations.
571 */
572 b->slow_allocation_cycles = VMW_BALLOON_SLOW_CYCLES;
573
574 if (i >= b->rate_alloc)
575 break;
576
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700577 flags = VMW_PAGE_ALLOC_CANSLEEP;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400578 /* Lower rate for sleeping allocations. */
579 rate = b->rate_alloc;
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700580 continue;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400581 }
582
Xavier Deguillardef0f8f12015-06-12 11:43:22 -0700583 error = vmballoon_lock_page(b, page);
584 if (error)
585 break;
586
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400587 if (++allocations > VMW_BALLOON_YIELD_THRESHOLD) {
588 cond_resched();
589 allocations = 0;
590 }
591
592 if (i >= rate) {
593 /* We allocated enough pages, let's take a break. */
594 break;
595 }
596 }
597
598 /*
599 * We reached our goal without failures so try increasing
600 * allocation rate.
601 */
602 if (error == 0 && i >= b->rate_alloc) {
603 unsigned int mult = i / b->rate_alloc;
604
605 b->rate_alloc =
606 min(b->rate_alloc + mult * VMW_BALLOON_RATE_ALLOC_INC,
607 VMW_BALLOON_RATE_ALLOC_MAX);
608 }
609
610 vmballoon_release_refused_pages(b);
611}
612
613/*
614 * Decrease the size of the balloon allowing guest to use more memory.
615 */
616static void vmballoon_deflate(struct vmballoon *b)
617{
618 struct page *page, *next;
619 unsigned int i = 0;
620 unsigned int goal;
621 int error;
622
623 pr_debug("%s - size: %d, target %d\n", __func__, b->size, b->target);
624
625 /* limit deallocation rate */
626 goal = min(b->size - b->target, b->rate_free);
627
628 pr_debug("%s - goal: %d, rate: %d\n", __func__, goal, b->rate_free);
629
630 /* free pages to reach target */
631 list_for_each_entry_safe(page, next, &b->pages, lru) {
632 error = vmballoon_release_page(b, page);
633 if (error) {
634 /* quickly decrease rate in case of error */
635 b->rate_free = max(b->rate_free / 2,
636 VMW_BALLOON_RATE_FREE_MIN);
637 return;
638 }
639
640 if (++i >= goal)
641 break;
642 }
643
644 /* slowly increase rate if there were no errors */
645 b->rate_free = min(b->rate_free + VMW_BALLOON_RATE_FREE_INC,
646 VMW_BALLOON_RATE_FREE_MAX);
647}
648
649/*
650 * Balloon work function: reset protocol, if needed, get the new size and
651 * adjust balloon as needed. Repeat in 1 sec.
652 */
653static void vmballoon_work(struct work_struct *work)
654{
655 struct delayed_work *dwork = to_delayed_work(work);
656 struct vmballoon *b = container_of(dwork, struct vmballoon, dwork);
657 unsigned int target;
658
659 STATS_INC(b->stats.timer);
660
661 if (b->reset_required)
662 vmballoon_reset(b);
663
664 if (b->slow_allocation_cycles > 0)
665 b->slow_allocation_cycles--;
666
667 if (vmballoon_send_get_target(b, &target)) {
668 /* update target, adjust size */
669 b->target = target;
670
671 if (b->size < target)
672 vmballoon_inflate(b);
673 else if (b->size > target)
674 vmballoon_deflate(b);
675 }
676
Dmitry Torokhovbeda94d2011-07-26 16:08:56 -0700677 /*
678 * We are using a freezable workqueue so that balloon operations are
679 * stopped while the system transitions to/from sleep/hibernation.
680 */
681 queue_delayed_work(system_freezable_wq,
682 dwork, round_jiffies_relative(HZ));
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400683}
684
685/*
686 * DEBUGFS Interface
687 */
688#ifdef CONFIG_DEBUG_FS
689
690static int vmballoon_debug_show(struct seq_file *f, void *offset)
691{
692 struct vmballoon *b = f->private;
693 struct vmballoon_stats *stats = &b->stats;
694
695 /* format size info */
696 seq_printf(f,
697 "target: %8d pages\n"
698 "current: %8d pages\n",
699 b->target, b->size);
700
701 /* format rate info */
702 seq_printf(f,
703 "rateNoSleepAlloc: %8d pages/sec\n"
704 "rateSleepAlloc: %8d pages/sec\n"
705 "rateFree: %8d pages/sec\n",
706 VMW_BALLOON_NOSLEEP_ALLOC_MAX,
707 b->rate_alloc, b->rate_free);
708
709 seq_printf(f,
710 "\n"
711 "timer: %8u\n"
712 "start: %8u (%4u failed)\n"
713 "guestType: %8u (%4u failed)\n"
714 "lock: %8u (%4u failed)\n"
715 "unlock: %8u (%4u failed)\n"
716 "target: %8u (%4u failed)\n"
717 "primNoSleepAlloc: %8u (%4u failed)\n"
718 "primCanSleepAlloc: %8u (%4u failed)\n"
719 "primFree: %8u\n"
720 "errAlloc: %8u\n"
721 "errFree: %8u\n",
722 stats->timer,
723 stats->start, stats->start_fail,
724 stats->guest_type, stats->guest_type_fail,
725 stats->lock, stats->lock_fail,
726 stats->unlock, stats->unlock_fail,
727 stats->target, stats->target_fail,
728 stats->alloc, stats->alloc_fail,
729 stats->sleep_alloc, stats->sleep_alloc_fail,
730 stats->free,
731 stats->refused_alloc, stats->refused_free);
732
733 return 0;
734}
735
736static int vmballoon_debug_open(struct inode *inode, struct file *file)
737{
738 return single_open(file, vmballoon_debug_show, inode->i_private);
739}
740
741static const struct file_operations vmballoon_debug_fops = {
742 .owner = THIS_MODULE,
743 .open = vmballoon_debug_open,
744 .read = seq_read,
745 .llseek = seq_lseek,
746 .release = single_release,
747};
748
749static int __init vmballoon_debugfs_init(struct vmballoon *b)
750{
751 int error;
752
753 b->dbg_entry = debugfs_create_file("vmmemctl", S_IRUGO, NULL, b,
754 &vmballoon_debug_fops);
755 if (IS_ERR(b->dbg_entry)) {
756 error = PTR_ERR(b->dbg_entry);
757 pr_err("failed to create debugfs entry, error: %d\n", error);
758 return error;
759 }
760
761 return 0;
762}
763
764static void __exit vmballoon_debugfs_exit(struct vmballoon *b)
765{
766 debugfs_remove(b->dbg_entry);
767}
768
769#else
770
771static inline int vmballoon_debugfs_init(struct vmballoon *b)
772{
773 return 0;
774}
775
776static inline void vmballoon_debugfs_exit(struct vmballoon *b)
777{
778}
779
780#endif /* CONFIG_DEBUG_FS */
781
782static int __init vmballoon_init(void)
783{
784 int error;
785
786 /*
787 * Check if we are running on VMware's hypervisor and bail out
788 * if we are not.
789 */
H. Peter Anvina10a5692010-05-09 01:13:42 -0700790 if (x86_hyper != &x86_hyper_vmware)
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400791 return -ENODEV;
792
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400793 INIT_LIST_HEAD(&balloon.pages);
794 INIT_LIST_HEAD(&balloon.refused_pages);
795
796 /* initialize rates */
797 balloon.rate_alloc = VMW_BALLOON_RATE_ALLOC_MAX;
798 balloon.rate_free = VMW_BALLOON_RATE_FREE_MAX;
799
800 INIT_DELAYED_WORK(&balloon.dwork, vmballoon_work);
801
802 /*
803 * Start balloon.
804 */
805 if (!vmballoon_send_start(&balloon)) {
806 pr_err("failed to send start command to the host\n");
Dmitry Torokhovbeda94d2011-07-26 16:08:56 -0700807 return -EIO;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400808 }
809
810 if (!vmballoon_send_guest_id(&balloon)) {
811 pr_err("failed to send guest ID to the host\n");
Dmitry Torokhovbeda94d2011-07-26 16:08:56 -0700812 return -EIO;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400813 }
814
815 error = vmballoon_debugfs_init(&balloon);
816 if (error)
Dmitry Torokhovbeda94d2011-07-26 16:08:56 -0700817 return error;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400818
Dmitry Torokhovbeda94d2011-07-26 16:08:56 -0700819 queue_delayed_work(system_freezable_wq, &balloon.dwork, 0);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400820
821 return 0;
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400822}
823module_init(vmballoon_init);
824
825static void __exit vmballoon_exit(void)
826{
827 cancel_delayed_work_sync(&balloon.dwork);
Dmitry Torokhov453dc652010-04-23 13:18:08 -0400828
829 vmballoon_debugfs_exit(&balloon);
830
831 /*
832 * Deallocate all reserved memory, and reset connection with monitor.
833 * Reset connection before deallocating memory to avoid potential for
834 * additional spurious resets from guest touching deallocated pages.
835 */
836 vmballoon_send_start(&balloon);
837 vmballoon_pop(&balloon);
838}
839module_exit(vmballoon_exit);