VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxService/VBoxServiceBalloon.cpp@ 98103

Last change on this file since 98103 was 98103, checked in by vboxsync, 17 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 13.8 KB
RevLine 
[26562]1/* $Id: VBoxServiceBalloon.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
[8387]2/** @file
[27111]3 * VBoxService - Memory Ballooning.
[8387]4 */
5
6/*
[98103]7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
[5999]8 *
[96407]9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
[8387]26 */
[27111]27
28
[58052]29/** @page pg_vgsvc_memballoon VBoxService - Memory Ballooning
30 *
31 * The Memory Ballooning subservice works with VBoxGuest, PGM and GMM to
32 * dynamically reallocate memory between VMs.
33 *
34 * Memory ballooning is typically used to deal with overcomitting memory on the
35 * host. It allowes you to borrow memory from one or more VMs and make it
36 * available to others. In theory it could also be used to make memory
37 * available to the host system, however memory fragmentation typically makes
38 * that difficult.
39 *
40 * The memory ballooning subservices talks to PGM, GMM and Main via the VMMDev.
41 * It polls for change requests at an interval and executes them when they
42 * arrive. There are two ways we implement the actual ballooning, either
43 * VBoxGuest allocates kernel memory and donates it to the host, or this service
44 * allocates process memory which VBoxGuest then locks down and donates to the
45 * host. While we prefer the former method it is not practicable on all OS and
46 * we have to use the latter.
47 *
48 */
49
50
[57358]51/*********************************************************************************************************************************
52* Header Files *
53*********************************************************************************************************************************/
[8387]54#include <iprt/assert.h>
[26326]55#include <iprt/mem.h>
[26999]56#include <iprt/stream.h>
[26326]57#include <iprt/string.h>
58#include <iprt/semaphore.h>
59#include <iprt/system.h>
[27111]60#include <iprt/thread.h>
[26326]61#include <iprt/time.h>
[76419]62#include <VBox/err.h>
[26326]63#include <VBox/VBoxGuestLib.h>
64#include "VBoxServiceInternal.h"
65#include "VBoxServiceUtils.h"
[8387]66
[27494]67#ifdef RT_OS_LINUX
[68662]68# include <iprt/param.h>
[27494]69# include <sys/mman.h>
[27496]70# ifndef MADV_DONTFORK
71# define MADV_DONTFORK 10
72# endif
[27494]73#endif
[26326]74
75
[27494]76
[57358]77/*********************************************************************************************************************************
78* Global Variables *
79*********************************************************************************************************************************/
[27111]80/** The balloon size. */
[27023]81static uint32_t g_cMemBalloonChunks = 0;
[26326]82
83/** The semaphore we're blocking on. */
84static RTSEMEVENTMULTI g_MemBalloonEvent = NIL_RTSEMEVENTMULTI;
85
[27494]86/** The array holding the R3 pointers of the balloon. */
[26999]87static void **g_pavBalloon = NULL;
88
[63495]89#ifdef RT_OS_LINUX
[27494]90/** True = madvise(MADV_DONTFORK) works, false otherwise. */
91static bool g_fSysMadviseWorks;
[63495]92#endif
[26999]93
[27494]94
[27023]95/**
[28048]96 * Check whether madvise() works.
[27494]97 */
[58029]98static void vgsvcBalloonInitMadvise(void)
[27494]99{
100#ifdef RT_OS_LINUX
[29543]101 void *pv = (void*)mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
102 if (pv != MAP_FAILED)
103 {
104 g_fSysMadviseWorks = madvise(pv, PAGE_SIZE, MADV_DONTFORK) == 0;
105 munmap(pv, PAGE_SIZE);
106 }
[27494]107#endif
108}
109
110
111/**
112 * Allocate a chunk of the balloon. Fulfil the prerequisite that we can lock this memory
113 * and protect it against fork() in R0. See also suplibOsPageAlloc().
114 */
[58029]115static void *VGSvcBalloonAllocChunk(void)
[27494]116{
117 size_t cb = VMMDEV_MEMORY_BALLOON_CHUNK_SIZE;
118 char *pu8;
119
120#ifdef RT_OS_LINUX
121 if (!g_fSysMadviseWorks)
122 cb += 2 * PAGE_SIZE;
123
124 pu8 = (char*)mmap(NULL, cb, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
125 if (pu8 == MAP_FAILED)
126 return NULL;
127
128 if (g_fSysMadviseWorks)
129 {
130 /*
131 * It is not fatal if we fail here but a forked child (e.g. the ALSA sound server)
132 * could crash. Linux < 2.6.16 does not implement madvise(MADV_DONTFORK) but the
133 * kernel seems to split bigger VMAs and that is all that we want -- later we set the
134 * VM_DONTCOPY attribute in supdrvOSLockMemOne().
135 */
136 madvise(pu8, cb, MADV_DONTFORK);
137 }
138 else
139 {
140 /*
141 * madvise(MADV_DONTFORK) is not available (most probably Linux 2.4). Enclose any
142 * mmapped region by two unmapped pages to guarantee that there is exactly one VM
143 * area struct of the very same size as the mmap area.
144 */
145 RTMemProtect(pu8, PAGE_SIZE, RTMEM_PROT_NONE);
146 RTMemProtect(pu8 + cb - PAGE_SIZE, PAGE_SIZE, RTMEM_PROT_NONE);
147 pu8 += PAGE_SIZE;
148 }
149
150#else
151
152 pu8 = (char*)RTMemPageAlloc(cb);
153 if (!pu8)
154 return pu8;
155
156#endif
157
158 memset(pu8, 0, VMMDEV_MEMORY_BALLOON_CHUNK_SIZE);
159 return pu8;
160}
161
162
163/**
[58029]164 * Free an allocated chunk undoing VGSvcBalloonAllocChunk().
[27494]165 */
[58029]166static void vgsvcBalloonFreeChunk(void *pv)
[27494]167{
168 char *pu8 = (char*)pv;
169 size_t cb = VMMDEV_MEMORY_BALLOON_CHUNK_SIZE;
170
171#ifdef RT_OS_LINUX
172
173 if (!g_fSysMadviseWorks)
174 {
175 cb += 2 * PAGE_SIZE;
176 pu8 -= PAGE_SIZE;
177 /* This is not really necessary */
178 RTMemProtect(pu8, PAGE_SIZE, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
179 RTMemProtect(pu8 + cb - PAGE_SIZE, PAGE_SIZE, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
180 }
181 munmap(pu8, cb);
182
183#else
184
[28317]185 RTMemPageFree(pu8, cb);
[27494]186
187#endif
188}
189
190
191/**
[27023]192 * Adapt the R0 memory balloon by granting/reclaiming 1MB chunks to/from R0.
193 *
194 * returns IPRT status code.
[27111]195 * @param cNewChunks The new number of 1MB chunks in the balloon.
[27023]196 */
[58029]197static int vgsvcBalloonSetUser(uint32_t cNewChunks)
[26999]198{
[27023]199 if (cNewChunks == g_cMemBalloonChunks)
200 return VINF_SUCCESS;
[26999]201
[58031]202 VGSvcVerbose(3, "vgsvcBalloonSetUser: cNewChunks=%u g_cMemBalloonChunks=%u\n", cNewChunks, g_cMemBalloonChunks);
[26999]203 int rc = VINF_SUCCESS;
[27023]204 if (cNewChunks > g_cMemBalloonChunks)
[26999]205 {
206 /* inflate */
[27217]207 g_pavBalloon = (void**)RTMemRealloc(g_pavBalloon, cNewChunks * sizeof(void*));
[26999]208 uint32_t i;
[27023]209 for (i = g_cMemBalloonChunks; i < cNewChunks; i++)
[26999]210 {
[58029]211 void *pv = VGSvcBalloonAllocChunk();
[27494]212 if (!pv)
213 break;
[26999]214 rc = VbglR3MemBalloonChange(pv, /* inflate=*/ true);
215 if (RT_SUCCESS(rc))
216 {
217 g_pavBalloon[i] = pv;
[27217]218#ifndef RT_OS_SOLARIS
219 /*
220 * Protect against access by dangling pointers (ignore errors as it may fail).
[27494]221 * On Solaris it corrupts the address space leaving the process unkillable. This
222 * could perhaps be related to what the underlying segment driver does; currently
223 * just disable it.
[27217]224 */
[27023]225 RTMemProtect(pv, VMMDEV_MEMORY_BALLOON_CHUNK_SIZE, RTMEM_PROT_NONE);
[27217]226#endif
[27023]227 g_cMemBalloonChunks++;
[26999]228 }
229 else
[27111]230 {
[58029]231 vgsvcBalloonFreeChunk(pv);
[26999]232 break;
[27111]233 }
[26999]234 }
[58031]235 VGSvcVerbose(3, "vgsvcBalloonSetUser: inflation complete. chunks=%u rc=%d\n", i, rc);
[26999]236 }
237 else
238 {
239 /* deflate */
240 uint32_t i;
[27023]241 for (i = g_cMemBalloonChunks; i-- > cNewChunks;)
[26999]242 {
[27023]243 void *pv = g_pavBalloon[i];
[26999]244 rc = VbglR3MemBalloonChange(pv, /* inflate=*/ false);
245 if (RT_SUCCESS(rc))
246 {
[27217]247#ifndef RT_OS_SOLARIS
[27023]248 /* unprotect */
249 RTMemProtect(pv, VMMDEV_MEMORY_BALLOON_CHUNK_SIZE, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
[27217]250#endif
[58029]251 vgsvcBalloonFreeChunk(pv);
[27023]252 g_pavBalloon[i] = NULL;
253 g_cMemBalloonChunks--;
[26999]254 }
255 else
256 break;
[58031]257 VGSvcVerbose(3, "vgsvcBalloonSetUser: deflation complete. chunks=%u rc=%d\n", i, rc);
[26999]258 }
259 }
260
[27023]261 return VINF_SUCCESS;
[26999]262}
263
264
[58029]265/**
266 * @interface_method_impl{VBOXSERVICE,pfnInit}
267 */
268static DECLCALLBACK(int) vgsvcBalloonInit(void)
[8387]269{
[58031]270 VGSvcVerbose(3, "vgsvcBalloonInit\n");
[8387]271
[26326]272 int rc = RTSemEventMultiCreate(&g_MemBalloonEvent);
273 AssertRCReturn(rc, rc);
[8387]274
[58029]275 vgsvcBalloonInitMadvise();
[27494]276
[27023]277 g_cMemBalloonChunks = 0;
278 uint32_t cNewChunks = 0;
279 bool fHandleInR3;
[8387]280
281 /* Check balloon size */
[27023]282 rc = VbglR3MemBalloonRefresh(&cNewChunks, &fHandleInR3);
[26326]283 if (RT_SUCCESS(rc))
[27023]284 {
[58031]285 VGSvcVerbose(3, "MemBalloon: New balloon size %d MB (%s memory)\n", cNewChunks, fHandleInR3 ? "R3" : "R0");
[27023]286 if (fHandleInR3)
[58029]287 rc = vgsvcBalloonSetUser(cNewChunks);
[27023]288 else
289 g_cMemBalloonChunks = cNewChunks;
290 }
[29345]291 if (RT_FAILURE(rc))
[29320]292 {
[29345]293 /* If the service was not found, we disable this service without
294 causing VBoxService to fail. */
295 if ( rc == VERR_NOT_IMPLEMENTED
296#ifdef RT_OS_WINDOWS /** @todo r=bird: Windows kernel driver should return VERR_NOT_IMPLEMENTED,
297 * VERR_INVALID_PARAMETER has too many other uses. */
298 || rc == VERR_INVALID_PARAMETER
299#endif
300 )
301 {
[58029]302 VGSvcVerbose(0, "MemBalloon: Memory ballooning support is not available\n");
[29345]303 rc = VERR_SERVICE_DISABLED;
304 }
[29320]305 else
[29345]306 {
[58029]307 VGSvcVerbose(3, "MemBalloon: VbglR3MemBalloonRefresh failed with %Rrc\n", rc);
[29345]308 rc = VERR_SERVICE_DISABLED; /** @todo Playing safe for now, figure out the exact status codes here. */
309 }
[29320]310 RTSemEventMultiDestroy(g_MemBalloonEvent);
311 g_MemBalloonEvent = NIL_RTSEMEVENTMULTI;
312 }
313
314 return rc;
[8387]315}
316
317
[27111]318/**
[27972]319 * Query the size of the memory balloon, given as a page count.
[27111]320 *
[27972]321 * @returns Number of pages.
322 * @param cbPage The page size.
[27111]323 */
[58029]324uint32_t VGSvcBalloonQueryPages(uint32_t cbPage)
[8387]325{
[32813]326 Assert(cbPage > 0);
[27972]327 return g_cMemBalloonChunks * (VMMDEV_MEMORY_BALLOON_CHUNK_SIZE / cbPage);
[8387]328}
329
[27111]330
[58029]331/**
332 * @interface_method_impl{VBOXSERVICE,pfnWorker}
333 */
334static DECLCALLBACK(int) vgsvcBalloonWorker(bool volatile *pfShutdown)
[8387]335{
[26326]336 /* Start monitoring of the stat event change event. */
[27111]337 int rc = VbglR3CtlFilterMask(VMMDEV_EVENT_BALLOON_CHANGE_REQUEST, 0);
[26326]338 if (RT_FAILURE(rc))
[8387]339 {
[58031]340 VGSvcVerbose(3, "vgsvcBalloonInit: VbglR3CtlFilterMask failed with %Rrc\n", rc);
[26326]341 return rc;
[8387]342 }
[26326]343
344 /*
345 * Tell the control thread that it can continue
346 * spawning services.
347 */
348 RTThreadUserSignal(RTThreadSelf());
349
350 /*
351 * Now enter the loop retrieving runtime data continuously.
352 */
353 for (;;)
[8387]354 {
[26326]355 uint32_t fEvents = 0;
[8387]356
[26326]357 /* Check if an update interval change is pending. */
358 rc = VbglR3WaitEvent(VMMDEV_EVENT_BALLOON_CHANGE_REQUEST, 0 /* no wait */, &fEvents);
359 if ( RT_SUCCESS(rc)
360 && (fEvents & VMMDEV_EVENT_BALLOON_CHANGE_REQUEST))
[8387]361 {
[27023]362 uint32_t cNewChunks;
363 bool fHandleInR3;
364 rc = VbglR3MemBalloonRefresh(&cNewChunks, &fHandleInR3);
[26326]365 if (RT_SUCCESS(rc))
[27023]366 {
[58031]367 VGSvcVerbose(3, "vgsvcBalloonInit: new balloon size %d MB (%s memory)\n", cNewChunks, fHandleInR3 ? "R3" : "R0");
[27023]368 if (fHandleInR3)
[27217]369 {
[58029]370 rc = vgsvcBalloonSetUser(cNewChunks);
[27217]371 if (RT_FAILURE(rc))
372 {
[58031]373 VGSvcVerbose(3, "vgsvcBalloonInit: failed to set balloon size %d MB (%s memory)\n",
374 cNewChunks, fHandleInR3 ? "R3" : "R0");
[27217]375 }
376 else
[58031]377 VGSvcVerbose(3, "vgsvcBalloonInit: successfully set requested balloon size %d.\n", cNewChunks);
[27217]378 }
[27023]379 else
380 g_cMemBalloonChunks = cNewChunks;
381 }
[26326]382 else
[58031]383 VGSvcVerbose(3, "vgsvcBalloonInit: VbglR3MemBalloonRefresh failed with %Rrc\n", rc);
[26326]384 }
[8387]385
[26326]386 /*
387 * Block for a while.
388 *
389 * The event semaphore takes care of ignoring interruptions and it
390 * allows us to implement service wakeup later.
391 */
392 if (*pfShutdown)
393 break;
394 int rc2 = RTSemEventMultiWait(g_MemBalloonEvent, 5000);
395 if (*pfShutdown)
396 break;
397 if (rc2 != VERR_TIMEOUT && RT_FAILURE(rc2))
[8387]398 {
[58031]399 VGSvcError("vgsvcBalloonInit: RTSemEventMultiWait failed; rc2=%Rrc\n", rc2);
[26326]400 rc = rc2;
401 break;
[8387]402 }
[21219]403 }
[8387]404
[26326]405 /* Cancel monitoring of the memory balloon change event. */
406 rc = VbglR3CtlFilterMask(0, VMMDEV_EVENT_BALLOON_CHANGE_REQUEST);
407 if (RT_FAILURE(rc))
[58031]408 VGSvcVerbose(3, "vgsvcBalloonInit: VbglR3CtlFilterMask failed with %Rrc\n", rc);
[8387]409
[58031]410 VGSvcVerbose(3, "vgsvcBalloonInit: finished mem balloon change request thread\n");
411 return VINF_SUCCESS;
[8387]412}
413
[58029]414
415/**
416 * @interface_method_impl{VBOXSERVICE,pfnStop}
417 */
418static DECLCALLBACK(void) vgsvcBalloonStop(void)
[26326]419{
420 RTSemEventMultiSignal(g_MemBalloonEvent);
421}
422
423
424/**
[58182]425 * @interface_method_impl{VBOXSERVICE,pfnTerm}
426 */
427static DECLCALLBACK(void) vgsvcBalloonTerm(void)
428{
429 if (g_MemBalloonEvent != NIL_RTSEMEVENTMULTI)
430 {
431 RTSemEventMultiDestroy(g_MemBalloonEvent);
432 g_MemBalloonEvent = NIL_RTSEMEVENTMULTI;
433 }
434}
435
436
437/**
[26326]438 * The 'memballoon' service description.
439 */
440VBOXSERVICE g_MemBalloon =
441{
442 /* pszName. */
443 "memballoon",
444 /* pszDescription. */
445 "Memory Ballooning",
446 /* pszUsage. */
447 NULL,
448 /* pszOptions. */
449 NULL,
450 /* methods */
[58029]451 VGSvcDefaultPreInit,
452 VGSvcDefaultOption,
453 vgsvcBalloonInit,
454 vgsvcBalloonWorker,
455 vgsvcBalloonStop,
[58182]456 vgsvcBalloonTerm
[26326]457};
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use