Dmitry Torokhov | 453dc65 | 2010-04-23 13:18:08 -0400 | [diff] [blame] | 1 | /* |
| 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 | * |
| 20 | * Maintained by: Dmitry Torokhov <dtor@vmware.com> |
| 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * This is VMware physical memory management driver for Linux. The driver |
| 25 | * acts like a "balloon" that can be inflated to reclaim physical pages by |
| 26 | * reserving them in the guest and invalidating them in the monitor, |
| 27 | * freeing up the underlying machine pages so they can be allocated to |
| 28 | * other guests. The balloon can also be deflated to allow the guest to |
| 29 | * use more physical memory. Higher level policies can control the sizes |
| 30 | * of balloons in VMs in order to manage physical memory resources. |
| 31 | */ |
| 32 | |
| 33 | //#define DEBUG |
| 34 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 35 | |
| 36 | #include <linux/types.h> |
| 37 | #include <linux/kernel.h> |
| 38 | #include <linux/mm.h> |
| 39 | #include <linux/sched.h> |
| 40 | #include <linux/module.h> |
| 41 | #include <linux/workqueue.h> |
| 42 | #include <linux/debugfs.h> |
| 43 | #include <linux/seq_file.h> |
H. Peter Anvin | a10a569 | 2010-05-09 01:13:42 -0700 | [diff] [blame] | 44 | #include <asm/hypervisor.h> |
Dmitry Torokhov | 453dc65 | 2010-04-23 13:18:08 -0400 | [diff] [blame] | 45 | |
| 46 | MODULE_AUTHOR("VMware, Inc."); |
| 47 | MODULE_DESCRIPTION("VMware Memory Control (Balloon) Driver"); |
Dmitry Torokhov | d27a0c0 | 2011-01-12 17:01:07 -0800 | [diff] [blame] | 48 | MODULE_VERSION("1.2.1.2-k"); |
Dmitry Torokhov | 453dc65 | 2010-04-23 13:18:08 -0400 | [diff] [blame] | 49 | MODULE_ALIAS("dmi:*:svnVMware*:*"); |
| 50 | MODULE_ALIAS("vmware_vmmemctl"); |
| 51 | MODULE_LICENSE("GPL"); |
| 52 | |
| 53 | /* |
| 54 | * Various constants controlling rate of inflaint/deflating balloon, |
| 55 | * measured in pages. |
| 56 | */ |
| 57 | |
| 58 | /* |
| 59 | * Rate of allocating memory when there is no memory pressure |
| 60 | * (driver performs non-sleeping allocations). |
| 61 | */ |
| 62 | #define VMW_BALLOON_NOSLEEP_ALLOC_MAX 16384U |
| 63 | |
| 64 | /* |
| 65 | * Rates of memory allocaton when guest experiences memory pressure |
| 66 | * (driver performs sleeping allocations). |
| 67 | */ |
| 68 | #define VMW_BALLOON_RATE_ALLOC_MIN 512U |
| 69 | #define VMW_BALLOON_RATE_ALLOC_MAX 2048U |
| 70 | #define VMW_BALLOON_RATE_ALLOC_INC 16U |
| 71 | |
| 72 | /* |
| 73 | * Rates for releasing pages while deflating balloon. |
| 74 | */ |
| 75 | #define VMW_BALLOON_RATE_FREE_MIN 512U |
| 76 | #define VMW_BALLOON_RATE_FREE_MAX 16384U |
| 77 | #define VMW_BALLOON_RATE_FREE_INC 16U |
| 78 | |
| 79 | /* |
| 80 | * When guest is under memory pressure, use a reduced page allocation |
| 81 | * rate for next several cycles. |
| 82 | */ |
| 83 | #define VMW_BALLOON_SLOW_CYCLES 4 |
| 84 | |
| 85 | /* |
| 86 | * Use __GFP_HIGHMEM to allow pages from HIGHMEM zone. We don't |
| 87 | * allow wait (__GFP_WAIT) for NOSLEEP page allocations. Use |
| 88 | * __GFP_NOWARN, to suppress page allocation failure warnings. |
| 89 | */ |
| 90 | #define VMW_PAGE_ALLOC_NOSLEEP (__GFP_HIGHMEM|__GFP_NOWARN) |
| 91 | |
| 92 | /* |
| 93 | * Use GFP_HIGHUSER when executing in a separate kernel thread |
| 94 | * context and allocation can sleep. This is less stressful to |
| 95 | * the guest memory system, since it allows the thread to block |
| 96 | * while memory is reclaimed, and won't take pages from emergency |
| 97 | * low-memory pools. |
| 98 | */ |
| 99 | #define VMW_PAGE_ALLOC_CANSLEEP (GFP_HIGHUSER) |
| 100 | |
| 101 | /* Maximum number of page allocations without yielding processor */ |
| 102 | #define VMW_BALLOON_YIELD_THRESHOLD 1024 |
| 103 | |
Dmitry Torokhov | 55adaa4 | 2010-06-04 14:14:52 -0700 | [diff] [blame] | 104 | /* Maximum number of refused pages we accumulate during inflation cycle */ |
| 105 | #define VMW_BALLOON_MAX_REFUSED 16 |
Dmitry Torokhov | 453dc65 | 2010-04-23 13:18:08 -0400 | [diff] [blame] | 106 | |
| 107 | /* |
| 108 | * Hypervisor communication port definitions. |
| 109 | */ |
| 110 | #define VMW_BALLOON_HV_PORT 0x5670 |
| 111 | #define VMW_BALLOON_HV_MAGIC 0x456c6d6f |
| 112 | #define VMW_BALLOON_PROTOCOL_VERSION 2 |
| 113 | #define VMW_BALLOON_GUEST_ID 1 /* Linux */ |
| 114 | |
| 115 | #define VMW_BALLOON_CMD_START 0 |
| 116 | #define VMW_BALLOON_CMD_GET_TARGET 1 |
| 117 | #define VMW_BALLOON_CMD_LOCK 2 |
| 118 | #define VMW_BALLOON_CMD_UNLOCK 3 |
| 119 | #define VMW_BALLOON_CMD_GUEST_ID 4 |
| 120 | |
| 121 | /* error codes */ |
| 122 | #define VMW_BALLOON_SUCCESS 0 |
| 123 | #define VMW_BALLOON_FAILURE -1 |
| 124 | #define VMW_BALLOON_ERROR_CMD_INVALID 1 |
| 125 | #define VMW_BALLOON_ERROR_PPN_INVALID 2 |
| 126 | #define VMW_BALLOON_ERROR_PPN_LOCKED 3 |
| 127 | #define VMW_BALLOON_ERROR_PPN_UNLOCKED 4 |
| 128 | #define VMW_BALLOON_ERROR_PPN_PINNED 5 |
| 129 | #define VMW_BALLOON_ERROR_PPN_NOTNEEDED 6 |
| 130 | #define VMW_BALLOON_ERROR_RESET 7 |
| 131 | #define VMW_BALLOON_ERROR_BUSY 8 |
| 132 | |
| 133 | #define VMWARE_BALLOON_CMD(cmd, data, result) \ |
| 134 | ({ \ |
| 135 | unsigned long __stat, __dummy1, __dummy2; \ |
| 136 | __asm__ __volatile__ ("inl (%%dx)" : \ |
| 137 | "=a"(__stat), \ |
| 138 | "=c"(__dummy1), \ |
| 139 | "=d"(__dummy2), \ |
| 140 | "=b"(result) : \ |
| 141 | "0"(VMW_BALLOON_HV_MAGIC), \ |
| 142 | "1"(VMW_BALLOON_CMD_##cmd), \ |
| 143 | "2"(VMW_BALLOON_HV_PORT), \ |
| 144 | "3"(data) : \ |
| 145 | "memory"); \ |
| 146 | result &= -1UL; \ |
| 147 | __stat & -1UL; \ |
| 148 | }) |
| 149 | |
| 150 | #ifdef CONFIG_DEBUG_FS |
| 151 | struct vmballoon_stats { |
| 152 | unsigned int timer; |
| 153 | |
| 154 | /* allocation statustics */ |
| 155 | unsigned int alloc; |
| 156 | unsigned int alloc_fail; |
| 157 | unsigned int sleep_alloc; |
| 158 | unsigned int sleep_alloc_fail; |
| 159 | unsigned int refused_alloc; |
| 160 | unsigned int refused_free; |
| 161 | unsigned int free; |
| 162 | |
| 163 | /* monitor operations */ |
| 164 | unsigned int lock; |
| 165 | unsigned int lock_fail; |
| 166 | unsigned int unlock; |
| 167 | unsigned int unlock_fail; |
| 168 | unsigned int target; |
| 169 | unsigned int target_fail; |
| 170 | unsigned int start; |
| 171 | unsigned int start_fail; |
| 172 | unsigned int guest_type; |
| 173 | unsigned int guest_type_fail; |
| 174 | }; |
| 175 | |
| 176 | #define STATS_INC(stat) (stat)++ |
| 177 | #else |
| 178 | #define STATS_INC(stat) |
| 179 | #endif |
| 180 | |
| 181 | struct vmballoon { |
| 182 | |
| 183 | /* list of reserved physical pages */ |
| 184 | struct list_head pages; |
| 185 | |
| 186 | /* transient list of non-balloonable pages */ |
| 187 | struct list_head refused_pages; |
Dmitry Torokhov | 55adaa4 | 2010-06-04 14:14:52 -0700 | [diff] [blame] | 188 | unsigned int n_refused_pages; |
Dmitry Torokhov | 453dc65 | 2010-04-23 13:18:08 -0400 | [diff] [blame] | 189 | |
| 190 | /* balloon size in pages */ |
| 191 | unsigned int size; |
| 192 | unsigned int target; |
| 193 | |
| 194 | /* reset flag */ |
| 195 | bool reset_required; |
| 196 | |
| 197 | /* adjustment rates (pages per second) */ |
| 198 | unsigned int rate_alloc; |
| 199 | unsigned int rate_free; |
| 200 | |
| 201 | /* slowdown page allocations for next few cycles */ |
| 202 | unsigned int slow_allocation_cycles; |
| 203 | |
| 204 | #ifdef CONFIG_DEBUG_FS |
| 205 | /* statistics */ |
| 206 | struct vmballoon_stats stats; |
| 207 | |
| 208 | /* debugfs file exporting statistics */ |
| 209 | struct dentry *dbg_entry; |
| 210 | #endif |
| 211 | |
| 212 | struct sysinfo sysinfo; |
| 213 | |
| 214 | struct delayed_work dwork; |
| 215 | }; |
| 216 | |
| 217 | static struct vmballoon balloon; |
| 218 | static struct workqueue_struct *vmballoon_wq; |
| 219 | |
| 220 | /* |
| 221 | * Send "start" command to the host, communicating supported version |
| 222 | * of the protocol. |
| 223 | */ |
| 224 | static bool vmballoon_send_start(struct vmballoon *b) |
| 225 | { |
| 226 | unsigned long status, dummy; |
| 227 | |
| 228 | STATS_INC(b->stats.start); |
| 229 | |
| 230 | status = VMWARE_BALLOON_CMD(START, VMW_BALLOON_PROTOCOL_VERSION, dummy); |
| 231 | if (status == VMW_BALLOON_SUCCESS) |
| 232 | return true; |
| 233 | |
| 234 | pr_debug("%s - failed, hv returns %ld\n", __func__, status); |
| 235 | STATS_INC(b->stats.start_fail); |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | static bool vmballoon_check_status(struct vmballoon *b, unsigned long status) |
| 240 | { |
| 241 | switch (status) { |
| 242 | case VMW_BALLOON_SUCCESS: |
| 243 | return true; |
| 244 | |
| 245 | case VMW_BALLOON_ERROR_RESET: |
| 246 | b->reset_required = true; |
| 247 | /* fall through */ |
| 248 | |
| 249 | default: |
| 250 | return false; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * Communicate guest type to the host so that it can adjust ballooning |
| 256 | * algorithm to the one most appropriate for the guest. This command |
| 257 | * is normally issued after sending "start" command and is part of |
| 258 | * standard reset sequence. |
| 259 | */ |
| 260 | static bool vmballoon_send_guest_id(struct vmballoon *b) |
| 261 | { |
| 262 | unsigned long status, dummy; |
| 263 | |
| 264 | status = VMWARE_BALLOON_CMD(GUEST_ID, VMW_BALLOON_GUEST_ID, dummy); |
| 265 | |
| 266 | STATS_INC(b->stats.guest_type); |
| 267 | |
| 268 | if (vmballoon_check_status(b, status)) |
| 269 | return true; |
| 270 | |
| 271 | pr_debug("%s - failed, hv returns %ld\n", __func__, status); |
| 272 | STATS_INC(b->stats.guest_type_fail); |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * Retrieve desired balloon size from the host. |
| 278 | */ |
| 279 | static bool vmballoon_send_get_target(struct vmballoon *b, u32 *new_target) |
| 280 | { |
| 281 | unsigned long status; |
| 282 | unsigned long target; |
| 283 | unsigned long limit; |
| 284 | u32 limit32; |
| 285 | |
| 286 | /* |
| 287 | * si_meminfo() is cheap. Moreover, we want to provide dynamic |
| 288 | * max balloon size later. So let us call si_meminfo() every |
| 289 | * iteration. |
| 290 | */ |
| 291 | si_meminfo(&b->sysinfo); |
| 292 | limit = b->sysinfo.totalram; |
| 293 | |
| 294 | /* Ensure limit fits in 32-bits */ |
| 295 | limit32 = (u32)limit; |
| 296 | if (limit != limit32) |
| 297 | return false; |
| 298 | |
| 299 | /* update stats */ |
| 300 | STATS_INC(b->stats.target); |
| 301 | |
| 302 | status = VMWARE_BALLOON_CMD(GET_TARGET, limit, target); |
| 303 | if (vmballoon_check_status(b, status)) { |
| 304 | *new_target = target; |
| 305 | return true; |
| 306 | } |
| 307 | |
| 308 | pr_debug("%s - failed, hv returns %ld\n", __func__, status); |
| 309 | STATS_INC(b->stats.target_fail); |
| 310 | return false; |
| 311 | } |
| 312 | |
| 313 | /* |
| 314 | * Notify the host about allocated page so that host can use it without |
| 315 | * fear that guest will need it. Host may reject some pages, we need to |
| 316 | * check the return value and maybe submit a different page. |
| 317 | */ |
Dmitry Torokhov | d27a0c0 | 2011-01-12 17:01:07 -0800 | [diff] [blame] | 318 | static bool vmballoon_send_lock_page(struct vmballoon *b, unsigned long pfn, |
| 319 | unsigned int *hv_status) |
Dmitry Torokhov | 453dc65 | 2010-04-23 13:18:08 -0400 | [diff] [blame] | 320 | { |
| 321 | unsigned long status, dummy; |
| 322 | u32 pfn32; |
| 323 | |
| 324 | pfn32 = (u32)pfn; |
| 325 | if (pfn32 != pfn) |
| 326 | return false; |
| 327 | |
| 328 | STATS_INC(b->stats.lock); |
| 329 | |
Dmitry Torokhov | d27a0c0 | 2011-01-12 17:01:07 -0800 | [diff] [blame] | 330 | *hv_status = status = VMWARE_BALLOON_CMD(LOCK, pfn, dummy); |
Dmitry Torokhov | 453dc65 | 2010-04-23 13:18:08 -0400 | [diff] [blame] | 331 | if (vmballoon_check_status(b, status)) |
| 332 | return true; |
| 333 | |
| 334 | pr_debug("%s - ppn %lx, hv returns %ld\n", __func__, pfn, status); |
| 335 | STATS_INC(b->stats.lock_fail); |
| 336 | return false; |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * Notify the host that guest intends to release given page back into |
| 341 | * the pool of available (to the guest) pages. |
| 342 | */ |
| 343 | static bool vmballoon_send_unlock_page(struct vmballoon *b, unsigned long pfn) |
| 344 | { |
| 345 | unsigned long status, dummy; |
| 346 | u32 pfn32; |
| 347 | |
| 348 | pfn32 = (u32)pfn; |
| 349 | if (pfn32 != pfn) |
| 350 | return false; |
| 351 | |
| 352 | STATS_INC(b->stats.unlock); |
| 353 | |
| 354 | status = VMWARE_BALLOON_CMD(UNLOCK, pfn, dummy); |
| 355 | if (vmballoon_check_status(b, status)) |
| 356 | return true; |
| 357 | |
| 358 | pr_debug("%s - ppn %lx, hv returns %ld\n", __func__, pfn, status); |
| 359 | STATS_INC(b->stats.unlock_fail); |
| 360 | return false; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Quickly release all pages allocated for the balloon. This function is |
| 365 | * called when host decides to "reset" balloon for one reason or another. |
| 366 | * Unlike normal "deflate" we do not (shall not) notify host of the pages |
| 367 | * being released. |
| 368 | */ |
| 369 | static void vmballoon_pop(struct vmballoon *b) |
| 370 | { |
| 371 | struct page *page, *next; |
| 372 | unsigned int count = 0; |
| 373 | |
| 374 | list_for_each_entry_safe(page, next, &b->pages, lru) { |
| 375 | list_del(&page->lru); |
| 376 | __free_page(page); |
| 377 | STATS_INC(b->stats.free); |
| 378 | b->size--; |
| 379 | |
| 380 | if (++count >= b->rate_free) { |
| 381 | count = 0; |
| 382 | cond_resched(); |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * Perform standard reset sequence by popping the balloon (in case it |
| 389 | * is not empty) and then restarting protocol. This operation normally |
| 390 | * happens when host responds with VMW_BALLOON_ERROR_RESET to a command. |
| 391 | */ |
| 392 | static void vmballoon_reset(struct vmballoon *b) |
| 393 | { |
| 394 | /* free all pages, skipping monitor unlock */ |
| 395 | vmballoon_pop(b); |
| 396 | |
| 397 | if (vmballoon_send_start(b)) { |
| 398 | b->reset_required = false; |
| 399 | if (!vmballoon_send_guest_id(b)) |
| 400 | pr_err("failed to send guest ID to the host\n"); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * Allocate (or reserve) a page for the balloon and notify the host. If host |
| 406 | * refuses the page put it on "refuse" list and allocate another one until host |
| 407 | * is satisfied. "Refused" pages are released at the end of inflation cycle |
| 408 | * (when we allocate b->rate_alloc pages). |
| 409 | */ |
| 410 | static int vmballoon_reserve_page(struct vmballoon *b, bool can_sleep) |
| 411 | { |
| 412 | struct page *page; |
| 413 | gfp_t flags; |
Dmitry Torokhov | d27a0c0 | 2011-01-12 17:01:07 -0800 | [diff] [blame] | 414 | unsigned int hv_status; |
Dmitry Torokhov | 453dc65 | 2010-04-23 13:18:08 -0400 | [diff] [blame] | 415 | bool locked = false; |
| 416 | |
| 417 | do { |
| 418 | if (!can_sleep) |
| 419 | STATS_INC(b->stats.alloc); |
| 420 | else |
| 421 | STATS_INC(b->stats.sleep_alloc); |
| 422 | |
| 423 | flags = can_sleep ? VMW_PAGE_ALLOC_CANSLEEP : VMW_PAGE_ALLOC_NOSLEEP; |
| 424 | page = alloc_page(flags); |
| 425 | if (!page) { |
| 426 | if (!can_sleep) |
| 427 | STATS_INC(b->stats.alloc_fail); |
| 428 | else |
| 429 | STATS_INC(b->stats.sleep_alloc_fail); |
| 430 | return -ENOMEM; |
| 431 | } |
| 432 | |
| 433 | /* inform monitor */ |
Dmitry Torokhov | d27a0c0 | 2011-01-12 17:01:07 -0800 | [diff] [blame] | 434 | locked = vmballoon_send_lock_page(b, page_to_pfn(page), &hv_status); |
Dmitry Torokhov | 453dc65 | 2010-04-23 13:18:08 -0400 | [diff] [blame] | 435 | if (!locked) { |
Dmitry Torokhov | 55adaa4 | 2010-06-04 14:14:52 -0700 | [diff] [blame] | 436 | STATS_INC(b->stats.refused_alloc); |
| 437 | |
Dmitry Torokhov | d27a0c0 | 2011-01-12 17:01:07 -0800 | [diff] [blame] | 438 | if (hv_status == VMW_BALLOON_ERROR_RESET || |
| 439 | hv_status == VMW_BALLOON_ERROR_PPN_NOTNEEDED) { |
Dmitry Torokhov | 453dc65 | 2010-04-23 13:18:08 -0400 | [diff] [blame] | 440 | __free_page(page); |
| 441 | return -EIO; |
| 442 | } |
| 443 | |
Dmitry Torokhov | 55adaa4 | 2010-06-04 14:14:52 -0700 | [diff] [blame] | 444 | /* |
| 445 | * Place page on the list of non-balloonable pages |
| 446 | * and retry allocation, unless we already accumulated |
| 447 | * too many of them, in which case take a breather. |
| 448 | */ |
Dmitry Torokhov | 453dc65 | 2010-04-23 13:18:08 -0400 | [diff] [blame] | 449 | list_add(&page->lru, &b->refused_pages); |
Dmitry Torokhov | 55adaa4 | 2010-06-04 14:14:52 -0700 | [diff] [blame] | 450 | if (++b->n_refused_pages >= VMW_BALLOON_MAX_REFUSED) |
| 451 | return -EIO; |
Dmitry Torokhov | 453dc65 | 2010-04-23 13:18:08 -0400 | [diff] [blame] | 452 | } |
| 453 | } while (!locked); |
| 454 | |
| 455 | /* track allocated page */ |
| 456 | list_add(&page->lru, &b->pages); |
| 457 | |
| 458 | /* update balloon size */ |
| 459 | b->size++; |
| 460 | |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | /* |
| 465 | * Release the page allocated for the balloon. Note that we first notify |
| 466 | * the host so it can make sure the page will be available for the guest |
| 467 | * to use, if needed. |
| 468 | */ |
| 469 | static int vmballoon_release_page(struct vmballoon *b, struct page *page) |
| 470 | { |
| 471 | if (!vmballoon_send_unlock_page(b, page_to_pfn(page))) |
| 472 | return -EIO; |
| 473 | |
| 474 | list_del(&page->lru); |
| 475 | |
| 476 | /* deallocate page */ |
| 477 | __free_page(page); |
| 478 | STATS_INC(b->stats.free); |
| 479 | |
| 480 | /* update balloon size */ |
| 481 | b->size--; |
| 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * Release pages that were allocated while attempting to inflate the |
| 488 | * balloon but were refused by the host for one reason or another. |
| 489 | */ |
| 490 | static void vmballoon_release_refused_pages(struct vmballoon *b) |
| 491 | { |
| 492 | struct page *page, *next; |
| 493 | |
| 494 | list_for_each_entry_safe(page, next, &b->refused_pages, lru) { |
| 495 | list_del(&page->lru); |
| 496 | __free_page(page); |
| 497 | STATS_INC(b->stats.refused_free); |
| 498 | } |
Dmitry Torokhov | 55adaa4 | 2010-06-04 14:14:52 -0700 | [diff] [blame] | 499 | |
| 500 | b->n_refused_pages = 0; |
Dmitry Torokhov | 453dc65 | 2010-04-23 13:18:08 -0400 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /* |
| 504 | * Inflate the balloon towards its target size. Note that we try to limit |
| 505 | * the rate of allocation to make sure we are not choking the rest of the |
| 506 | * system. |
| 507 | */ |
| 508 | static void vmballoon_inflate(struct vmballoon *b) |
| 509 | { |
| 510 | unsigned int goal; |
| 511 | unsigned int rate; |
| 512 | unsigned int i; |
| 513 | unsigned int allocations = 0; |
| 514 | int error = 0; |
| 515 | bool alloc_can_sleep = false; |
| 516 | |
| 517 | pr_debug("%s - size: %d, target %d\n", __func__, b->size, b->target); |
| 518 | |
| 519 | /* |
| 520 | * First try NOSLEEP page allocations to inflate balloon. |
| 521 | * |
| 522 | * If we do not throttle nosleep allocations, we can drain all |
| 523 | * free pages in the guest quickly (if the balloon target is high). |
| 524 | * As a side-effect, draining free pages helps to inform (force) |
| 525 | * the guest to start swapping if balloon target is not met yet, |
| 526 | * which is a desired behavior. However, balloon driver can consume |
| 527 | * all available CPU cycles if too many pages are allocated in a |
| 528 | * second. Therefore, we throttle nosleep allocations even when |
| 529 | * the guest is not under memory pressure. OTOH, if we have already |
| 530 | * predicted that the guest is under memory pressure, then we |
| 531 | * slowdown page allocations considerably. |
| 532 | */ |
| 533 | |
| 534 | goal = b->target - b->size; |
| 535 | /* |
| 536 | * Start with no sleep allocation rate which may be higher |
| 537 | * than sleeping allocation rate. |
| 538 | */ |
| 539 | rate = b->slow_allocation_cycles ? |
| 540 | b->rate_alloc : VMW_BALLOON_NOSLEEP_ALLOC_MAX; |
| 541 | |
| 542 | pr_debug("%s - goal: %d, no-sleep rate: %d, sleep rate: %d\n", |
| 543 | __func__, goal, rate, b->rate_alloc); |
| 544 | |
| 545 | for (i = 0; i < goal; i++) { |
| 546 | |
| 547 | error = vmballoon_reserve_page(b, alloc_can_sleep); |
| 548 | if (error) { |
| 549 | if (error != -ENOMEM) { |
| 550 | /* |
| 551 | * Not a page allocation failure, stop this |
| 552 | * cycle. Maybe we'll get new target from |
| 553 | * the host soon. |
| 554 | */ |
| 555 | break; |
| 556 | } |
| 557 | |
| 558 | if (alloc_can_sleep) { |
| 559 | /* |
| 560 | * CANSLEEP page allocation failed, so guest |
| 561 | * is under severe memory pressure. Quickly |
| 562 | * decrease allocation rate. |
| 563 | */ |
| 564 | b->rate_alloc = max(b->rate_alloc / 2, |
| 565 | VMW_BALLOON_RATE_ALLOC_MIN); |
| 566 | break; |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | * NOSLEEP page allocation failed, so the guest is |
| 571 | * under memory pressure. Let us slow down page |
| 572 | * allocations for next few cycles so that the guest |
| 573 | * gets out of memory pressure. Also, if we already |
| 574 | * allocated b->rate_alloc pages, let's pause, |
| 575 | * otherwise switch to sleeping allocations. |
| 576 | */ |
| 577 | b->slow_allocation_cycles = VMW_BALLOON_SLOW_CYCLES; |
| 578 | |
| 579 | if (i >= b->rate_alloc) |
| 580 | break; |
| 581 | |
| 582 | alloc_can_sleep = true; |
| 583 | /* Lower rate for sleeping allocations. */ |
| 584 | rate = b->rate_alloc; |
| 585 | } |
| 586 | |
| 587 | 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 | */ |
| 616 | static 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 | */ |
| 653 | static 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 | |
| 677 | queue_delayed_work(vmballoon_wq, dwork, round_jiffies_relative(HZ)); |
| 678 | } |
| 679 | |
| 680 | /* |
| 681 | * DEBUGFS Interface |
| 682 | */ |
| 683 | #ifdef CONFIG_DEBUG_FS |
| 684 | |
| 685 | static int vmballoon_debug_show(struct seq_file *f, void *offset) |
| 686 | { |
| 687 | struct vmballoon *b = f->private; |
| 688 | struct vmballoon_stats *stats = &b->stats; |
| 689 | |
| 690 | /* format size info */ |
| 691 | seq_printf(f, |
| 692 | "target: %8d pages\n" |
| 693 | "current: %8d pages\n", |
| 694 | b->target, b->size); |
| 695 | |
| 696 | /* format rate info */ |
| 697 | seq_printf(f, |
| 698 | "rateNoSleepAlloc: %8d pages/sec\n" |
| 699 | "rateSleepAlloc: %8d pages/sec\n" |
| 700 | "rateFree: %8d pages/sec\n", |
| 701 | VMW_BALLOON_NOSLEEP_ALLOC_MAX, |
| 702 | b->rate_alloc, b->rate_free); |
| 703 | |
| 704 | seq_printf(f, |
| 705 | "\n" |
| 706 | "timer: %8u\n" |
| 707 | "start: %8u (%4u failed)\n" |
| 708 | "guestType: %8u (%4u failed)\n" |
| 709 | "lock: %8u (%4u failed)\n" |
| 710 | "unlock: %8u (%4u failed)\n" |
| 711 | "target: %8u (%4u failed)\n" |
| 712 | "primNoSleepAlloc: %8u (%4u failed)\n" |
| 713 | "primCanSleepAlloc: %8u (%4u failed)\n" |
| 714 | "primFree: %8u\n" |
| 715 | "errAlloc: %8u\n" |
| 716 | "errFree: %8u\n", |
| 717 | stats->timer, |
| 718 | stats->start, stats->start_fail, |
| 719 | stats->guest_type, stats->guest_type_fail, |
| 720 | stats->lock, stats->lock_fail, |
| 721 | stats->unlock, stats->unlock_fail, |
| 722 | stats->target, stats->target_fail, |
| 723 | stats->alloc, stats->alloc_fail, |
| 724 | stats->sleep_alloc, stats->sleep_alloc_fail, |
| 725 | stats->free, |
| 726 | stats->refused_alloc, stats->refused_free); |
| 727 | |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | static int vmballoon_debug_open(struct inode *inode, struct file *file) |
| 732 | { |
| 733 | return single_open(file, vmballoon_debug_show, inode->i_private); |
| 734 | } |
| 735 | |
| 736 | static const struct file_operations vmballoon_debug_fops = { |
| 737 | .owner = THIS_MODULE, |
| 738 | .open = vmballoon_debug_open, |
| 739 | .read = seq_read, |
| 740 | .llseek = seq_lseek, |
| 741 | .release = single_release, |
| 742 | }; |
| 743 | |
| 744 | static int __init vmballoon_debugfs_init(struct vmballoon *b) |
| 745 | { |
| 746 | int error; |
| 747 | |
| 748 | b->dbg_entry = debugfs_create_file("vmmemctl", S_IRUGO, NULL, b, |
| 749 | &vmballoon_debug_fops); |
| 750 | if (IS_ERR(b->dbg_entry)) { |
| 751 | error = PTR_ERR(b->dbg_entry); |
| 752 | pr_err("failed to create debugfs entry, error: %d\n", error); |
| 753 | return error; |
| 754 | } |
| 755 | |
| 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | static void __exit vmballoon_debugfs_exit(struct vmballoon *b) |
| 760 | { |
| 761 | debugfs_remove(b->dbg_entry); |
| 762 | } |
| 763 | |
| 764 | #else |
| 765 | |
| 766 | static inline int vmballoon_debugfs_init(struct vmballoon *b) |
| 767 | { |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | static inline void vmballoon_debugfs_exit(struct vmballoon *b) |
| 772 | { |
| 773 | } |
| 774 | |
| 775 | #endif /* CONFIG_DEBUG_FS */ |
| 776 | |
| 777 | static int __init vmballoon_init(void) |
| 778 | { |
| 779 | int error; |
| 780 | |
| 781 | /* |
| 782 | * Check if we are running on VMware's hypervisor and bail out |
| 783 | * if we are not. |
| 784 | */ |
H. Peter Anvin | a10a569 | 2010-05-09 01:13:42 -0700 | [diff] [blame] | 785 | if (x86_hyper != &x86_hyper_vmware) |
Dmitry Torokhov | 453dc65 | 2010-04-23 13:18:08 -0400 | [diff] [blame] | 786 | return -ENODEV; |
| 787 | |
Tejun Heo | 58a69cb | 2011-02-16 09:25:31 +0100 | [diff] [blame] | 788 | vmballoon_wq = create_freezable_workqueue("vmmemctl"); |
Dmitry Torokhov | 453dc65 | 2010-04-23 13:18:08 -0400 | [diff] [blame] | 789 | if (!vmballoon_wq) { |
| 790 | pr_err("failed to create workqueue\n"); |
| 791 | return -ENOMEM; |
| 792 | } |
| 793 | |
| 794 | INIT_LIST_HEAD(&balloon.pages); |
| 795 | INIT_LIST_HEAD(&balloon.refused_pages); |
| 796 | |
| 797 | /* initialize rates */ |
| 798 | balloon.rate_alloc = VMW_BALLOON_RATE_ALLOC_MAX; |
| 799 | balloon.rate_free = VMW_BALLOON_RATE_FREE_MAX; |
| 800 | |
| 801 | INIT_DELAYED_WORK(&balloon.dwork, vmballoon_work); |
| 802 | |
| 803 | /* |
| 804 | * Start balloon. |
| 805 | */ |
| 806 | if (!vmballoon_send_start(&balloon)) { |
| 807 | pr_err("failed to send start command to the host\n"); |
| 808 | error = -EIO; |
| 809 | goto fail; |
| 810 | } |
| 811 | |
| 812 | if (!vmballoon_send_guest_id(&balloon)) { |
| 813 | pr_err("failed to send guest ID to the host\n"); |
| 814 | error = -EIO; |
| 815 | goto fail; |
| 816 | } |
| 817 | |
| 818 | error = vmballoon_debugfs_init(&balloon); |
| 819 | if (error) |
| 820 | goto fail; |
| 821 | |
| 822 | queue_delayed_work(vmballoon_wq, &balloon.dwork, 0); |
| 823 | |
| 824 | return 0; |
| 825 | |
| 826 | fail: |
| 827 | destroy_workqueue(vmballoon_wq); |
| 828 | return error; |
| 829 | } |
| 830 | module_init(vmballoon_init); |
| 831 | |
| 832 | static void __exit vmballoon_exit(void) |
| 833 | { |
| 834 | cancel_delayed_work_sync(&balloon.dwork); |
| 835 | destroy_workqueue(vmballoon_wq); |
| 836 | |
| 837 | vmballoon_debugfs_exit(&balloon); |
| 838 | |
| 839 | /* |
| 840 | * Deallocate all reserved memory, and reset connection with monitor. |
| 841 | * Reset connection before deallocating memory to avoid potential for |
| 842 | * additional spurious resets from guest touching deallocated pages. |
| 843 | */ |
| 844 | vmballoon_send_start(&balloon); |
| 845 | vmballoon_pop(&balloon); |
| 846 | } |
| 847 | module_exit(vmballoon_exit); |