1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
|
/*********************************************************
* Copyright (C) 2000 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation version 2 and no later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*********************************************************/
/*
* vmballoon.c --
*
* VMware server physical memory management driver for Unix-ish
* (Linux, FreeBSD, Solaris) guests. The driver acts like a
* "balloon" that can be inflated to reclaim physical pages by
* reserving them in the guest and invalidating them in the
* monitor, freeing up the underlying machine pages so they can
* be allocated to other guests. The balloon can also be
* deflated to allow the guest to use more physical memory.
* Higher level policies can control the sizes of balloons in VMs
* in order to manage physical memory resources.
*/
#ifdef __cplusplus
extern "C" {
#endif
/*
* Compile-Time Options
*/
#define BALLOON_RATE_ADAPT (1)
#define BALLOON_DEBUG (1)
#define BALLOON_DEBUG_VERBOSE (0)
#define BALLOON_STATS
#define BALLOON_STATS_PROCFS
/*
* Includes
*/
#include "balloon_def.h"
#include "os.h"
#include "backdoor.h"
#include "backdoor_balloon.h"
#include "vmballoon.h"
/*
* Constants
*/
#ifndef NULL
#define NULL 0
#endif
#define BALLOON_NAME "vmmemctl"
#define BALLOON_NAME_VERBOSE "VMware memory control driver"
#define BALLOON_PROTOCOL_VERSION (2)
#define BALLOON_CHUNK_PAGES (1000)
#define BALLOON_NOSLEEP_ALLOC_MAX (16384)
#define BALLOON_RATE_ALLOC_MIN (512)
#define BALLOON_RATE_ALLOC_MAX (2048)
#define BALLOON_RATE_ALLOC_INC (16)
#define BALLOON_RATE_FREE_MIN (512)
#define BALLOON_RATE_FREE_MAX (16384)
#define BALLOON_RATE_FREE_INC (16)
#define BALLOON_ERROR_PAGES (16)
/*
* When guest is under memory pressure, use a reduced page allocation
* rate for next several cycles.
*/
#define SLOW_PAGE_ALLOCATION_CYCLES (4)
/*
* Move it to bora/public/balloon_def.h later, if needed. Note that
* BALLOON_PAGE_ALLOC_FAILURE is an internal error code used for
* distinguishing page allocation failures from monitor-backdoor errors.
* We use value 1000 because all monitor-backdoor error codes are < 1000.
*/
#define BALLOON_PAGE_ALLOC_FAILURE (1000)
// Maximum number of page allocations without yielding processor
#define BALLOON_ALLOC_YIELD_THRESHOLD (1024)
/*
* Types
*/
typedef struct BalloonChunk {
unsigned long page[BALLOON_CHUNK_PAGES];
uint32 nextPage;
struct BalloonChunk *prev, *next;
} BalloonChunk;
typedef struct {
unsigned long page[BALLOON_ERROR_PAGES];
uint32 nextPage;
} BalloonErrorPages;
typedef struct {
/* sets of reserved physical pages */
BalloonChunk *chunks;
int nChunks;
/* transient list of non-balloonable pages */
BalloonErrorPages errors;
/* balloon size */
int nPages;
int nPagesTarget;
/* reset flag */
int resetFlag;
/* adjustment rates (pages per second) */
int rateAlloc;
int rateFree;
/* slowdown page allocations for next few cycles */
int slowPageAllocationCycles;
/* statistics */
BalloonStats stats;
} Balloon;
/*
* Globals
*/
static Balloon globalBalloon;
/*
* Forward Declarations
*/
static int BalloonGuestType(void);
static unsigned long BalloonPrimAllocPage(BalloonPageAllocType canSleep);
static void BalloonPrimFreePage(unsigned long page);
static int Balloon_AllocPage(Balloon *b, BalloonPageAllocType allocType);
static int Balloon_FreePage(Balloon *b, int monitorUnlock);
static int Balloon_AdjustSize(Balloon *b, uint32 target);
static void Balloon_Reset(Balloon *b);
static void Balloon_StartTimer(Balloon *b);
static void Balloon_StopTimer(Balloon *b);
static void CDECL Balloon_BH(void *data);
static int Balloon_MonitorStart(Balloon *b);
static int Balloon_MonitorGuestType(Balloon *b);
static int Balloon_MonitorGetTarget(Balloon *b, uint32 *nPages);
static int Balloon_MonitorLockPage(Balloon *b, unsigned long addr);
static int Balloon_MonitorUnlockPage(Balloon *b, unsigned long addr);
/*
* Macros
*/
#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif
#ifdef BALLOON_STATS
#define STATS_INC(stat) (stat)++
#else
#define STATS_INC(stat)
#endif
/*
* Macros to generate operations for simple lists of OBJ.
* OBJ must contain next and prev fields.
*
*/
#define GENERATE_LIST_INSERT(OBJ) \
static void OBJ ## _Insert(OBJ **head, OBJ *obj) \
{ \
OBJ *h = *head; \
\
/* add element to head of list */ \
obj->next = h; \
if (h != NULL) { \
h->prev = obj; \
} \
*head = obj; \
obj->prev = NULL; \
}
#define GENERATE_LIST_REMOVE(OBJ) \
static void OBJ ## _Remove(OBJ **head, OBJ *obj) \
{ \
/* splice element out of list */ \
if (obj->prev != NULL) { \
obj->prev->next = obj->next; \
} else { \
*head = obj->next; \
} \
if (obj->next != NULL) { \
obj->next->prev = obj->prev; \
} \
}
/*
* List Operations
*/
GENERATE_LIST_INSERT(BalloonChunk);
GENERATE_LIST_REMOVE(BalloonChunk);
/*
* Procfs Operations
*/
/*
*----------------------------------------------------------------------
*
* BalloonProcRead --
*
* Ballon driver status reporting routine. Note that this is only
* used for Linux.
*
* Results:
* Writes ASCII status information into "buf".
* Returns number of bytes written.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int CDECL
BalloonProcRead(char *buf)
{
int len = 0;
BalloonStats stats;
BalloonGetStats(&stats);
/* format size info */
len += os_sprintf(buf + len,
"target: %8d pages\n"
"current: %8d pages\n",
stats.nPagesTarget,
stats.nPages);
/* format rate info */
len += os_sprintf(buf + len,
"rateNoSleepAlloc: %8d pages/sec\n"
"rateSleepAlloc: %8d pages/sec\n"
"rateFree: %8d pages/sec\n",
BALLOON_NOSLEEP_ALLOC_MAX,
stats.rateAlloc,
stats.rateFree);
#ifdef BALLOON_STATS_PROCFS
len += os_sprintf(buf + len,
"\n"
"timer: %8u\n"
"start: %8u (%4u failed)\n"
"guestType: %8u (%4u failed)\n"
"lock: %8u (%4u failed)\n"
"unlock: %8u (%4u failed)\n"
"target: %8u (%4u failed)\n"
"primNoSleepAlloc: %8u (%4u failed)\n"
"primCanSleepAlloc: %8u (%4u failed)\n"
"primFree: %8u\n"
"errAlloc: %8u\n"
"errFree: %8u\n",
stats.timer,
stats.start, stats.startFail,
stats.guestType, stats.guestTypeFail,
stats.lock, stats.lockFail,
stats.unlock, stats.unlockFail,
stats.target, stats.targetFail,
stats.primAlloc[BALLOON_PAGE_ALLOC_NOSLEEP],
stats.primAllocFail[BALLOON_PAGE_ALLOC_NOSLEEP],
stats.primAlloc[BALLOON_PAGE_ALLOC_CANSLEEP],
stats.primAllocFail[BALLOON_PAGE_ALLOC_CANSLEEP],
stats.primFree,
stats.primErrorPageAlloc,
stats.primErrorPageFree);
#endif
return(len);
}
/*
* Utility Operations
*/
/*
*----------------------------------------------------------------------
*
* AddrToPPN --
*
* Return the physical page number corresponding to the specified
* Linux kernel-mapped address.
*
* Results:
* Returns PPN for "addr".
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static inline unsigned long
AddrToPPN(unsigned long addr)
{
return(os_addr_to_ppn(addr));
}
/*
*----------------------------------------------------------------------
*
* BalloonPrimAllocPage --
*
* Attempts to allocate and reserve a physical page.
*
* If canSleep == 1, i.e., BALLOON_PAGE_ALLOC_CANSLEEP:
* The allocation can wait (sleep) for page writeout (swap)
* by the guest.
* otherwise canSleep == 0, i.e., BALLOON_PAGE_ALLOC_NOSLEEP:
* If allocation of a page requires disk writeout, then
* just fail. DON'T sleep.
*
* Results:
* Returns the physical address of the allocated page, or 0 if error.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static unsigned long
BalloonPrimAllocPage(BalloonPageAllocType canSleep)
{
return(os_alloc_reserved_page(canSleep));
}
/*
*----------------------------------------------------------------------
*
* BalloonPrimFreePage --
*
* Unreserves and deallocates specified physical page.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
BalloonPrimFreePage(unsigned long page)
{
return(os_free_reserved_page(page));
}
/*
*----------------------------------------------------------------------
*
* BalloonGuestType --
*
* Return balloon guest OS identifier obtained by parsing
* system-dependent identity string.
*
* Results:
* Returns one of BALLOON_GUEST_{LINUX,BSD,SOLARIS,UNKNOWN}.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
BalloonGuestType(void)
{
char *identity;
/* obtain OS identify string */
identity = os_identity();
/* unknown if not specified */
if (identity == NULL) {
return(BALLOON_GUEST_UNKNOWN);
}
/* classify based on first letter (avoid defining strcmp) */
switch (identity[0]) {
case 'l':
case 'L':
return(BALLOON_GUEST_LINUX);
case 'b':
case 'B':
return(BALLOON_GUEST_BSD);
case 's':
case 'S':
return(BALLOON_GUEST_SOLARIS);
default:
break;
}
/* unknown */
return(BALLOON_GUEST_UNKNOWN);
}
/*
* Returns information about balloon state, including the current and
* target size, rates for allocating and freeing pages, and statistics
* about past activity.
*/
void BalloonGetStats(BalloonStats *stats)
{
Balloon *b = &globalBalloon;
/*
* Copy statistics out of global structure.
*/
os_memcpy(stats, &b->stats, sizeof (BalloonStats));
/*
* Fill in additional information about size and rates, which is
* normally kept in the Balloon structure itself.
*/
stats->nPages = b->nPages;
stats->nPagesTarget = b->nPagesTarget;
stats->rateAlloc = b->rateAlloc;
stats->rateFree = b->rateFree;
}
/*
* BalloonChunk Operations
*/
/*
*----------------------------------------------------------------------
*
* BalloonChunk_Create --
*
* Creates a new BalloonChunk object capable of tracking
* BALLOON_CHUNK_PAGES PPNs.
*
* We do not bother to define two versions (NOSLEEP and CANSLEEP)
* of os_kmalloc because Chunk_Create does not require a new page
* often.
*
* Results:
* Returns initialized BalloonChunk, or NULL if error.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static BalloonChunk *
BalloonChunk_Create(void)
{
BalloonChunk *chunk;
/* allocate memory, fail if unable */
if ((chunk = (BalloonChunk *) os_kmalloc_nosleep(sizeof(BalloonChunk))) == NULL) {
return(NULL);
}
/* initialize */
os_bzero(chunk, sizeof(BalloonChunk));
/* everything OK */
return(chunk);
}
/*
*----------------------------------------------------------------------
*
* BalloonChunk_Destroy --
*
* Reclaims all storage associated with specified BalloonChunk.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
BalloonChunk_Destroy(BalloonChunk *chunk)
{
/* reclaim storage */
os_kfree(chunk, sizeof (BalloonChunk));
}
/*
* Balloon operations
*/
/*
*----------------------------------------------------------------------
*
* Balloon_Init --
*
* Initializes device state for balloon "b".
*
* Results:
* Returns BALLOON_SUCCESS.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
Balloon_Init(Balloon *b)
{
/* clear state */
os_bzero(b, sizeof(Balloon));
/* initialize rates */
b->rateAlloc = BALLOON_RATE_ALLOC_MAX;
b->rateFree = BALLOON_RATE_FREE_MAX;
/* initialize reset flag */
b->resetFlag = 1;
/* everything OK */
return(BALLOON_SUCCESS);
}
/*
*----------------------------------------------------------------------
*
* Balloon_Deallocate --
*
* Frees all allocated pages.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
Balloon_Deallocate(Balloon *b)
{
unsigned int cnt = 0;
/* free all pages, skipping monitor unlock */
while (b->nChunks > 0) {
(void) Balloon_FreePage(b, FALSE);
if (++cnt >= b->rateFree) {
cnt = 0;
os_yield();
}
}
}
/*
*----------------------------------------------------------------------
*
* Balloon_Reset --
*
* Resets balloon "b" to empty state. Frees all allocated pages
* and attempts to reset contact with the monitor.
*
* Results:
* None.
*
* Side effects:
* Schedules next execution of balloon timer handler.
*
*----------------------------------------------------------------------
*/
static void
Balloon_Reset(Balloon *b)
{
uint32 status;
/* free all pages, skipping monitor unlock */
Balloon_Deallocate(b);
/* send start command */
status = Balloon_MonitorStart(b);
if (status == BALLOON_SUCCESS) {
/* clear flag */
b->resetFlag = 0;
/* report guest type */
(void) Balloon_MonitorGuestType(b);
}
}
/*
*----------------------------------------------------------------------
*
* Balloon_BH --
*
* Balloon bottom half handler. Contacts monitor via backdoor
* to obtain balloon size target, and starts adjusting balloon
* size to achieve target by allocating or deallocating pages.
* Resets balloon if requested by the monitor.
*
* Results:
* None.
*
* Side effects:
* Schedules next execution of balloon timer handler.
*
*----------------------------------------------------------------------
*/
static void CDECL
Balloon_BH(void *data)
{
Balloon *b = (Balloon *) data;
uint32 target;
int status;
/* update stats */
STATS_INC(b->stats.timer);
/* reset, if specified */
if (b->resetFlag) {
Balloon_Reset(b);
}
/* contact monitor via backdoor */
status = Balloon_MonitorGetTarget(b, &target);
/* decrement slowPageAllocationCycles counter */
if (b->slowPageAllocationCycles > 0) {
b->slowPageAllocationCycles--;
}
if (status == BALLOON_SUCCESS) {
/* update target, adjust size */
b->nPagesTarget = target;
(void) Balloon_AdjustSize(b, target);
}
}
/*
*----------------------------------------------------------------------
*
* Balloon_StartTimer --
*
* Schedules next execution of balloon timer handler.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
Balloon_StartTimer(Balloon *b)
{
os_timer_start();
}
/*
*----------------------------------------------------------------------
*
* Balloon_StopTimer --
*
* Deschedules balloon timer handler.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
Balloon_StopTimer(Balloon *b)
{
os_timer_stop();
}
/*
*----------------------------------------------------------------------
*
* Balloon_ErrorPagesAlloc --
*
* Attempt to add "page" to list of non-balloonable pages
* associated with "b".
*
* Results:
* Returns BALLOON_SUCCESS iff successful, or BALLOON_FAILURE
* if non-balloonable page list is already full.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
Balloon_ErrorPagesAlloc(Balloon *b, unsigned long page)
{
/* fail if list already full */
if (b->errors.nextPage >= BALLOON_ERROR_PAGES) {
return(BALLOON_FAILURE);
}
/* add page to list */
b->errors.page[b->errors.nextPage] = page;
b->errors.nextPage++;
STATS_INC(b->stats.primErrorPageAlloc);
return(BALLOON_SUCCESS);
}
/*
*----------------------------------------------------------------------
*
* Balloon_ErrorPagesFree --
*
* Deallocates all pages on the list of non-balloonable pages
* associated with "b".
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
Balloon_ErrorPagesFree(Balloon *b)
{
int i;
/* free all non-balloonable "error" pages */
for (i = 0; i < b->errors.nextPage; i++) {
BalloonPrimFreePage(b->errors.page[i]);
b->errors.page[i] = 0;
STATS_INC(b->stats.primErrorPageFree);
}
b->errors.nextPage = 0;
}
/*
*----------------------------------------------------------------------
*
* Balloon_AllocPage --
*
* Attempts to allocate a physical page, inflating balloon "b".
* Informs monitor of PPN for allocated page via backdoor.
*
* Results:
* Returns BALLOON_SUCCESS if successful, otherwise error code.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
Balloon_AllocPage(Balloon *b, BalloonPageAllocType allocType)
{
BalloonChunk *chunk;
unsigned long page;
int status;
retry:
/* allocate page, fail if unable */
STATS_INC(b->stats.primAlloc[allocType]);
page = BalloonPrimAllocPage(allocType);
if (page == 0) {
STATS_INC(b->stats.primAllocFail[allocType]);
return(BALLOON_PAGE_ALLOC_FAILURE);
}
/* find chunk with space, create if necessary */
chunk = b->chunks;
if ((chunk == NULL) || (chunk->nextPage >= BALLOON_CHUNK_PAGES)) {
/* create new chunk */
if ((chunk = BalloonChunk_Create()) == NULL) {
/* reclaim storage, fail */
BalloonPrimFreePage(page);
return(BALLOON_PAGE_ALLOC_FAILURE);
}
BalloonChunk_Insert(&b->chunks, chunk);
/* update stats */
b->nChunks++;
}
/* inform monitor via backdoor */
status = Balloon_MonitorLockPage(b, page);
if (status != BALLOON_SUCCESS) {
/* place on list of non-balloonable pages, retry allocation */
if ((status != BALLOON_ERROR_RESET) &&
(Balloon_ErrorPagesAlloc(b, page) == BALLOON_SUCCESS)) {
goto retry;
}
/* reclaim storage, fail */
BalloonPrimFreePage(page);
return(status);
}
/* track allocated page */
chunk->page[chunk->nextPage] = page;
chunk->nextPage++;
/* update balloon size */
b->nPages++;
/* everything OK */
return(BALLOON_SUCCESS);
}
/*
*----------------------------------------------------------------------
*
* Balloon_FreePage --
*
* Attempts to deallocate a physical page, deflating balloon "b".
* Informs monitor of PPN for deallocated page via backdoor if
* "monitorUnlock" is specified.
*
* Results:
* Returns BALLOON_SUCCESS if successful, otherwise error code.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
Balloon_FreePage(Balloon *b, int monitorUnlock)
{
BalloonChunk *chunk;
unsigned long page;
int status;
chunk = b->chunks;
while ((chunk != NULL) && (chunk->nextPage == 0)) {
/* destroy empty chunk */
BalloonChunk_Remove(&b->chunks, chunk);
BalloonChunk_Destroy(chunk);
/* update stats */
b->nChunks--;
chunk = b->chunks;
}
if (chunk == NULL) {
return(BALLOON_FAILURE);
}
/* select page to deallocate */
chunk->nextPage--;
page = chunk->page[chunk->nextPage];
/* inform monitor via backdoor */
if (monitorUnlock) {
status = Balloon_MonitorUnlockPage(b, page);
if (status != BALLOON_SUCCESS) {
/* reset next pointer, fail */
chunk->nextPage++;
return(status);
}
}
/* deallocate page */
BalloonPrimFreePage(page);
STATS_INC(b->stats.primFree);
/* update balloon size */
b->nPages--;
/* reclaim chunk, if empty */
if (chunk->nextPage == 0) {
/* destroy empty chunk */
BalloonChunk_Remove(&b->chunks, chunk);
BalloonChunk_Destroy(chunk);
/* update stats */
b->nChunks--;
}
/* everything OK */
return(BALLOON_SUCCESS);
}
/*
*----------------------------------------------------------------------
*
* BalloonDecreaseRateAlloc --
*
* Wrapper to quickly reduce the page allocation rate. This function
* is called only when a CANSLEEP allocation fails. This implies severe
* memory pressure inside the guest, so quickly decrease the rateAlloc.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
BalloonDecreaseRateAlloc(Balloon *b)
{
if (BALLOON_RATE_ADAPT) {
b->rateAlloc = MAX(b->rateAlloc / 2, BALLOON_RATE_ALLOC_MIN);
}
}
/*
*----------------------------------------------------------------------
*
* BalloonIncreaseRateAlloc --
*
* Wrapper to increase the page allocation rate.
*
* This function is called when the balloon target is met or
* b->rateAlloc (or more) pages have been successfully allocated.
* This implies that the guest may not be under high memory
* pressure. So let us increase the rateAlloc.
*
* If meeting balloon target requires less than b->rateAlloc
* pages, then we do not change the page allocation rate.
*
* If the number of pages successfully allocated (nAlloc) is far
* higher than b->rateAlloc, then it implies that NOSLEEP
* allocations are highly successful. Therefore, we predict that
* the guest is under no memory pressure, and so increase
* b->rateAlloc quickly.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
BalloonIncreaseRateAlloc(Balloon *b, uint32 nAlloc)
{
if (BALLOON_RATE_ADAPT) {
if (nAlloc >= b->rateAlloc) {
uint32 mult = nAlloc / b->rateAlloc;
b->rateAlloc = MIN(b->rateAlloc + mult * BALLOON_RATE_ALLOC_INC,
BALLOON_RATE_ALLOC_MAX);
}
}
}
/*
*----------------------------------------------------------------------
*
* BalloonInflate--
*
* Attempts to allocate physical pages to inflate balloon.
*
* Results:
* Returns BALLOON_SUCCESS if successful, otherwise error code.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
BalloonInflate(Balloon *b, uint32 target)
{
int status, allocations = 0;
uint32 i, nAllocNoSleep, nAllocCanSleep;
/*
* First try NOSLEEP page allocations to inflate balloon.
*
* If we do not throttle nosleep allocations, we can drain all
* free pages in the guest quickly (if the balloon target is high).
* As a side-effect, draining free pages helps to inform (force)
* the guest to start swapping if balloon target is not met yet,
* which is a desired behavior. However, balloon driver can consume
* all available CPU cycles if too many pages are allocated in a
* second. Therefore, we throttle nosleep allocations even when
* the guest is not under memory pressure. OTOH, if we have already
* predicted that the guest is under memory pressure, then we
* slowdown page allocations considerably.
*/
if (b->slowPageAllocationCycles > 0) {
nAllocNoSleep = MIN(target - b->nPages, b->rateAlloc);
} else {
nAllocNoSleep = MIN(target - b->nPages, BALLOON_NOSLEEP_ALLOC_MAX);
}
for (i = 0; i < nAllocNoSleep; i++) {
/* Try NOSLEEP allocation */
status = Balloon_AllocPage(b, BALLOON_PAGE_ALLOC_NOSLEEP);
if (status != BALLOON_SUCCESS) {
if (status != BALLOON_PAGE_ALLOC_FAILURE) {
/*
* Not a page allocation failure, so release non-balloonable
* pages, and fail.
*/
Balloon_ErrorPagesFree(b);
return(status);
}
/*
* NOSLEEP page allocation failed, so the guest is under memory
* pressure. Let us slowdown page allocations for next few
* cycles so that the guest gets out of memory pressure.
*/
b->slowPageAllocationCycles = SLOW_PAGE_ALLOCATION_CYCLES;
break;
}
if (++allocations > BALLOON_ALLOC_YIELD_THRESHOLD) {
os_yield();
allocations = 0;
}
}
/*
* Check whether nosleep allocation successfully zapped nAllocNoSleep
* pages.
*/
if (i == nAllocNoSleep) {
BalloonIncreaseRateAlloc(b, nAllocNoSleep);
/* release non-balloonable pages, succeed */
Balloon_ErrorPagesFree(b);
return(BALLOON_SUCCESS);
} else {
/*
* NOSLEEP allocation failed, so the guest is under memory pressure.
* If already b->rateAlloc pages were zapped, then succeed. Otherwise,
* try CANSLEEP allocation.
*/
if (i > b->rateAlloc) {
BalloonIncreaseRateAlloc(b, i);
/* release non-balloonable pages, succeed */
Balloon_ErrorPagesFree(b);
return(BALLOON_SUCCESS);
} else {
/* update successful NOSLEEP allocations, and proceed */
nAllocNoSleep = i;
}
}
/*
* Use CANSLEEP page allocation to inflate balloon if below target.
*
* Sleep allocations are required only when nosleep allocation fails.
* This implies that the guest is already under memory pressure, so
* let us always throttle canSleep allocations. The total number pages
* allocated using noSleep and canSleep methods is throttled at
* b->rateAlloc per second when the guest is under memory pressure.
*/
nAllocCanSleep = target - b->nPages;
nAllocCanSleep = MIN(nAllocCanSleep, b->rateAlloc - nAllocNoSleep);
for (i = 0; i < nAllocCanSleep; i++) {
/* Try CANSLEEP allocation */
status = Balloon_AllocPage(b, BALLOON_PAGE_ALLOC_CANSLEEP);
if(status != BALLOON_SUCCESS) {
if (status == BALLOON_PAGE_ALLOC_FAILURE) {
/*
* CANSLEEP page allocation failed, so guest is under severe
* memory pressure. Quickly decrease rateAlloc.
*/
BalloonDecreaseRateAlloc(b);
}
/* release non-balloonable pages, fail */
Balloon_ErrorPagesFree(b);
return(status);
}
if (++allocations > BALLOON_ALLOC_YIELD_THRESHOLD) {
os_yield();
allocations = 0;
}
}
/*
* Either met the balloon target or b->rateAlloc pages have been
* successfully zapped.
*/
BalloonIncreaseRateAlloc(b, nAllocNoSleep + nAllocCanSleep);
/* release non-balloonable pages, succeed */
Balloon_ErrorPagesFree(b);
return(BALLOON_SUCCESS);
}
/*
*----------------------------------------------------------------------
*
* BalloonDeflate --
*
* Frees physical pages to deflate balloon.
*
* Results:
* Returns BALLOON_SUCCESS if successful, otherwise error code.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
BalloonDeflate(Balloon *b, uint32 target)
{
int status, i;
uint32 nFree = b->nPages - target;
/* limit deallocation rate */
nFree = MIN(nFree, b->rateFree);
/* free pages to reach target */
for (i = 0; i < nFree; i++) {
if ((status = Balloon_FreePage(b, TRUE)) != BALLOON_SUCCESS) {
if (BALLOON_RATE_ADAPT) {
/* quickly decrease rate if error */
b->rateFree = MAX(b->rateFree / 2, BALLOON_RATE_FREE_MIN);
}
return(status);
}
}
if (BALLOON_RATE_ADAPT) {
/* slowly increase rate if no errors */
b->rateFree = MIN(b->rateFree + BALLOON_RATE_FREE_INC,
BALLOON_RATE_FREE_MAX);
}
/* everything OK */
return(BALLOON_SUCCESS);
}
/*
*----------------------------------------------------------------------
*
* Balloon_AdjustSize --
*
* Attempts to allocate or deallocate physical pages in order
* to reach desired "target" size for balloon "b".
*
* Results:
* Returns BALLOON_SUCCESS if successful, otherwise error code.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
Balloon_AdjustSize(Balloon *b, uint32 target)
{
/* done if already at target */
if (b->nPages == target) {
return(BALLOON_SUCCESS);
}
/* inflate balloon if below target */
if (b->nPages < target) {
return BalloonInflate(b, target);
}
/* deflate balloon if above target */
if (b->nPages > target) {
return BalloonDeflate(b, target);
}
/* not reached */
return(BALLOON_FAILURE);
}
/*
* Backdoor Operations
*/
/*
*----------------------------------------------------------------------
*
* Balloon_MonitorStart --
*
* Attempts to contact monitor via backdoor to begin operation.
*
* Results:
* Returns BALLOON_SUCCESS if successful, otherwise error code.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
Balloon_MonitorStart(Balloon *b)
{
uint32 status, target;
Backdoor_proto bp;
/* prepare backdoor args */
bp.in.cx.halfs.low = BALLOON_BDOOR_CMD_START;
bp.in.size = BALLOON_PROTOCOL_VERSION;
/* invoke backdoor */
Backdoor_Balloon(&bp);
/* parse return values */
status = bp.out.ax.word;
target = bp.out.bx.word;
/* update stats */
STATS_INC(b->stats.start);
if (status != BALLOON_SUCCESS) {
STATS_INC(b->stats.startFail);
}
/* everything OK */
return(status);
}
/*
*----------------------------------------------------------------------
*
* Balloon_MonitorGuestType --
*
* Attempts to contact monitor and report guest OS identity.
*
* Results:
* Returns BALLOON_SUCCESS if successful, otherwise error code.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
Balloon_MonitorGuestType(Balloon *b)
{
uint32 status, target;
Backdoor_proto bp;
/* prepare backdoor args */
bp.in.cx.halfs.low = BALLOON_BDOOR_CMD_GUEST_ID;
bp.in.size = BalloonGuestType();
/* invoke backdoor */
Backdoor_Balloon(&bp);
/* parse return values */
status = bp.out.ax.word;
target = bp.out.bx.word;
/* set flag if reset requested */
if (status == BALLOON_ERROR_RESET) {
b->resetFlag = 1;
}
/* update stats */
STATS_INC(b->stats.guestType);
if (status != BALLOON_SUCCESS) {
STATS_INC(b->stats.guestTypeFail);
}
/* everything OK */
return(status);
}
/*
*----------------------------------------------------------------------
*
* Balloon_MonitorGetTarget --
*
* Attempts to contact monitor via backdoor to obtain desired
* balloon size.
*
* Predicts the maximum achievable balloon size and sends it
* to vmm => vmkernel via vEbx register.
*
* os_predict_max_balloon_pages() returns either predicted max balloon
* pages or BALLOON_MAX_SIZE_USE_CONFIG. In the later scenario,
* vmkernel uses global config options for determining a guest's max
* balloon size. Note that older vmballoon drivers set vEbx to
* BALLOON_MAX_SIZE_USE_CONFIG, i.e., value 0 (zero). So vmkernel
* will fallback to config-based max balloon size estimation.
*
* Results:
* If successful, sets "target" to value obtained from monitor,
* and returns BALLOON_SUCCESS. Otherwise returns error code.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
Balloon_MonitorGetTarget(Balloon *b, uint32 *target)
{
Backdoor_proto bp;
uint32 status;
/* prepare backdoor args */
bp.in.cx.halfs.low = BALLOON_BDOOR_CMD_TARGET;
bp.in.size = os_predict_max_balloon_pages();
/* invoke backdoor */
Backdoor_Balloon(&bp);
/* parse return values */
status = bp.out.ax.word;
*target = bp.out.bx.word;
/* set flag if reset requested */
if (status == BALLOON_ERROR_RESET) {
b->resetFlag = 1;
}
/* update stats */
STATS_INC(b->stats.target);
if (status != BALLOON_SUCCESS) {
STATS_INC(b->stats.targetFail);
}
/* everything OK */
return(status);
}
/*
*----------------------------------------------------------------------
*
* Balloon_MonitorLockPage --
*
* Attempts to contact monitor and add PPN containing "addr"
* to set of "balloon locked" pages.
*
* Results:
* Returns BALLOON_SUCCESS if successful, otherwise error code.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
Balloon_MonitorLockPage(Balloon *b, unsigned long addr)
{
unsigned long ppn;
uint32 ppn32;
uint32 status, target;
Backdoor_proto bp;
/* convert kernel-mapped "physical addr" to ppn */
ppn = AddrToPPN(addr);
/* Ensure PPN fits in 32-bits, i.e. guest memory is limited to 16TB. */
ppn32 = (uint32)ppn;
if (ppn32 != ppn) {
return BALLOON_ERROR_PPN_INVALID;
}
/* prepare backdoor args */
bp.in.cx.halfs.low = BALLOON_BDOOR_CMD_LOCK;
bp.in.size = ppn32;
/* invoke backdoor */
Backdoor_Balloon(&bp);
/* parse return values */
status = bp.out.ax.word;
target = bp.out.bx.word;
/* set flag if reset requested */
if (status == BALLOON_ERROR_RESET) {
b->resetFlag = 1;
}
/* update stats */
STATS_INC(b->stats.lock);
if (status != BALLOON_SUCCESS) {
STATS_INC(b->stats.lockFail);
}
/* everything OK */
return(status);
}
/*
*----------------------------------------------------------------------
*
* Balloon_MonitorUnlockPage --
*
* Attempts to contact monitor and remove PPN containing "addr"
* from set of "balloon locked" pages.
*
* Results:
* Returns BALLOON_SUCCESS if successful, otherwise error code.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
Balloon_MonitorUnlockPage(Balloon *b, unsigned long addr)
{
unsigned long ppn;
uint32 ppn32;
uint32 status, target;
Backdoor_proto bp;
/* convert kernel-mapped "physical addr" to ppn */
ppn = AddrToPPN(addr);
/* Ensure PPN fits in 32-bits, i.e. guest memory is limited to 16TB. */
ppn32 = (uint32)ppn;
if (ppn32 != ppn) {
return BALLOON_ERROR_PPN_INVALID;
}
/* prepare backdoor args */
bp.in.cx.halfs.low = BALLOON_BDOOR_CMD_UNLOCK;
bp.in.size = ppn32;
/* invoke backdoor */
Backdoor_Balloon(&bp);
/* parse return values */
status = bp.out.ax.word;
target = bp.out.bx.word;
/* set flag if reset requested */
if (status == BALLOON_ERROR_RESET) {
b->resetFlag = 1;
}
/* update stats */
STATS_INC(b->stats.unlock);
if (status != BALLOON_SUCCESS) {
STATS_INC(b->stats.unlockFail);
}
/* everything OK */
return(status);
}
/*
* Module Operations
*/
static int
BalloonModuleInit(void)
{
static int initialized = 0;
Balloon *b = &globalBalloon;
/* initialize only once */
if (initialized++) {
return(BALLOON_FAILURE);
}
/* initialize global state */
Balloon_Init(b);
/* os-specific initialization */
os_init(BALLOON_NAME, BALLOON_NAME_VERBOSE, BalloonProcRead);
os_timer_init(Balloon_BH, (void *) b, os_timer_hz());
/* start timer */
Balloon_StartTimer(b);
/* everything OK */
return(BALLOON_SUCCESS);
}
static void
BalloonModuleCleanup(void)
{
Balloon *b = &globalBalloon;
/* stop timer */
Balloon_StopTimer(b);
/*
* Deallocate all reserved memory, and reset connection with monitor.
* Reset connection before deallocating memory to avoid potential for
* additional spurious resets from guest touching deallocated pages.
*/
(void) Balloon_MonitorStart(b);
Balloon_Deallocate(b);
/* os-specific cleanup */
os_cleanup();
}
int init_module(void)
{
return(BalloonModuleInit());
}
void cleanup_module(void)
{
BalloonModuleCleanup();
}
#ifdef __cplusplus
}
#endif
|