VirtualBox

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

Last change on this file since 98613 was 98103, checked in by vboxsync, 23 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
Line 
1/* $Id: VBoxServiceBalloon.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * VBoxService - Memory Ballooning.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
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
26 */
27
28
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
51/*********************************************************************************************************************************
52* Header Files *
53*********************************************************************************************************************************/
54#include <iprt/assert.h>
55#include <iprt/mem.h>
56#include <iprt/stream.h>
57#include <iprt/string.h>
58#include <iprt/semaphore.h>
59#include <iprt/system.h>
60#include <iprt/thread.h>
61#include <iprt/time.h>
62#include <VBox/err.h>
63#include <VBox/VBoxGuestLib.h>
64#include "VBoxServiceInternal.h"
65#include "VBoxServiceUtils.h"
66
67#ifdef RT_OS_LINUX
68# include <iprt/param.h>
69# include <sys/mman.h>
70# ifndef MADV_DONTFORK
71# define MADV_DONTFORK 10
72# endif
73#endif
74
75
76
77/*********************************************************************************************************************************
78* Global Variables *
79*********************************************************************************************************************************/
80/** The balloon size. */
81static uint32_t g_cMemBalloonChunks = 0;
82
83/** The semaphore we're blocking on. */
84static RTSEMEVENTMULTI g_MemBalloonEvent = NIL_RTSEMEVENTMULTI;
85
86/** The array holding the R3 pointers of the balloon. */
87static void **g_pavBalloon = NULL;
88
89#ifdef RT_OS_LINUX
90/** True = madvise(MADV_DONTFORK) works, false otherwise. */
91static bool g_fSysMadviseWorks;
92#endif
93
94
95/**
96 * Check whether madvise() works.
97 */
98static void vgsvcBalloonInitMadvise(void)
99{
100#ifdef RT_OS_LINUX
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 }
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 */
115static void *VGSvcBalloonAllocChunk(void)
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/**
164 * Free an allocated chunk undoing VGSvcBalloonAllocChunk().
165 */
166static void vgsvcBalloonFreeChunk(void *pv)
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
185 RTMemPageFree(pu8, cb);
186
187#endif
188}
189
190
191/**
192 * Adapt the R0 memory balloon by granting/reclaiming 1MB chunks to/from R0.
193 *
194 * returns IPRT status code.
195 * @param cNewChunks The new number of 1MB chunks in the balloon.
196 */
197static int vgsvcBalloonSetUser(uint32_t cNewChunks)
198{
199 if (cNewChunks == g_cMemBalloonChunks)
200 return VINF_SUCCESS;
201
202 VGSvcVerbose(3, "vgsvcBalloonSetUser: cNewChunks=%u g_cMemBalloonChunks=%u\n", cNewChunks, g_cMemBalloonChunks);
203 int rc = VINF_SUCCESS;
204 if (cNewChunks > g_cMemBalloonChunks)
205 {
206 /* inflate */
207 g_pavBalloon = (void**)RTMemRealloc(g_pavBalloon, cNewChunks * sizeof(void*));
208 uint32_t i;
209 for (i = g_cMemBalloonChunks; i < cNewChunks; i++)
210 {
211 void *pv = VGSvcBalloonAllocChunk();
212 if (!pv)
213 break;
214 rc = VbglR3MemBalloonChange(pv, /* inflate=*/ true);
215 if (RT_SUCCESS(rc))
216 {
217 g_pavBalloon[i] = pv;
218#ifndef RT_OS_SOLARIS
219 /*
220 * Protect against access by dangling pointers (ignore errors as it may fail).
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.
224 */
225 RTMemProtect(pv, VMMDEV_MEMORY_BALLOON_CHUNK_SIZE, RTMEM_PROT_NONE);
226#endif
227 g_cMemBalloonChunks++;
228 }
229 else
230 {
231 vgsvcBalloonFreeChunk(pv);
232 break;
233 }
234 }
235 VGSvcVerbose(3, "vgsvcBalloonSetUser: inflation complete. chunks=%u rc=%d\n", i, rc);
236 }
237 else
238 {
239 /* deflate */
240 uint32_t i;
241 for (i = g_cMemBalloonChunks; i-- > cNewChunks;)
242 {
243 void *pv = g_pavBalloon[i];
244 rc = VbglR3MemBalloonChange(pv, /* inflate=*/ false);
245 if (RT_SUCCESS(rc))
246 {
247#ifndef RT_OS_SOLARIS
248 /* unprotect */
249 RTMemProtect(pv, VMMDEV_MEMORY_BALLOON_CHUNK_SIZE, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
250#endif
251 vgsvcBalloonFreeChunk(pv);
252 g_pavBalloon[i] = NULL;
253 g_cMemBalloonChunks--;
254 }
255 else
256 break;
257 VGSvcVerbose(3, "vgsvcBalloonSetUser: deflation complete. chunks=%u rc=%d\n", i, rc);
258 }
259 }
260
261 return VINF_SUCCESS;
262}
263
264
265/**
266 * @interface_method_impl{VBOXSERVICE,pfnInit}
267 */
268static DECLCALLBACK(int) vgsvcBalloonInit(void)
269{
270 VGSvcVerbose(3, "vgsvcBalloonInit\n");
271
272 int rc = RTSemEventMultiCreate(&g_MemBalloonEvent);
273 AssertRCReturn(rc, rc);
274
275 vgsvcBalloonInitMadvise();
276
277 g_cMemBalloonChunks = 0;
278 uint32_t cNewChunks = 0;
279 bool fHandleInR3;
280
281 /* Check balloon size */
282 rc = VbglR3MemBalloonRefresh(&cNewChunks, &fHandleInR3);
283 if (RT_SUCCESS(rc))
284 {
285 VGSvcVerbose(3, "MemBalloon: New balloon size %d MB (%s memory)\n", cNewChunks, fHandleInR3 ? "R3" : "R0");
286 if (fHandleInR3)
287 rc = vgsvcBalloonSetUser(cNewChunks);
288 else
289 g_cMemBalloonChunks = cNewChunks;
290 }
291 if (RT_FAILURE(rc))
292 {
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 {
302 VGSvcVerbose(0, "MemBalloon: Memory ballooning support is not available\n");
303 rc = VERR_SERVICE_DISABLED;
304 }
305 else
306 {
307 VGSvcVerbose(3, "MemBalloon: VbglR3MemBalloonRefresh failed with %Rrc\n", rc);
308 rc = VERR_SERVICE_DISABLED; /** @todo Playing safe for now, figure out the exact status codes here. */
309 }
310 RTSemEventMultiDestroy(g_MemBalloonEvent);
311 g_MemBalloonEvent = NIL_RTSEMEVENTMULTI;
312 }
313
314 return rc;
315}
316
317
318/**
319 * Query the size of the memory balloon, given as a page count.
320 *
321 * @returns Number of pages.
322 * @param cbPage The page size.
323 */
324uint32_t VGSvcBalloonQueryPages(uint32_t cbPage)
325{
326 Assert(cbPage > 0);
327 return g_cMemBalloonChunks * (VMMDEV_MEMORY_BALLOON_CHUNK_SIZE / cbPage);
328}
329
330
331/**
332 * @interface_method_impl{VBOXSERVICE,pfnWorker}
333 */
334static DECLCALLBACK(int) vgsvcBalloonWorker(bool volatile *pfShutdown)
335{
336 /* Start monitoring of the stat event change event. */
337 int rc = VbglR3CtlFilterMask(VMMDEV_EVENT_BALLOON_CHANGE_REQUEST, 0);
338 if (RT_FAILURE(rc))
339 {
340 VGSvcVerbose(3, "vgsvcBalloonInit: VbglR3CtlFilterMask failed with %Rrc\n", rc);
341 return rc;
342 }
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 (;;)
354 {
355 uint32_t fEvents = 0;
356
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))
361 {
362 uint32_t cNewChunks;
363 bool fHandleInR3;
364 rc = VbglR3MemBalloonRefresh(&cNewChunks, &fHandleInR3);
365 if (RT_SUCCESS(rc))
366 {
367 VGSvcVerbose(3, "vgsvcBalloonInit: new balloon size %d MB (%s memory)\n", cNewChunks, fHandleInR3 ? "R3" : "R0");
368 if (fHandleInR3)
369 {
370 rc = vgsvcBalloonSetUser(cNewChunks);
371 if (RT_FAILURE(rc))
372 {
373 VGSvcVerbose(3, "vgsvcBalloonInit: failed to set balloon size %d MB (%s memory)\n",
374 cNewChunks, fHandleInR3 ? "R3" : "R0");
375 }
376 else
377 VGSvcVerbose(3, "vgsvcBalloonInit: successfully set requested balloon size %d.\n", cNewChunks);
378 }
379 else
380 g_cMemBalloonChunks = cNewChunks;
381 }
382 else
383 VGSvcVerbose(3, "vgsvcBalloonInit: VbglR3MemBalloonRefresh failed with %Rrc\n", rc);
384 }
385
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))
398 {
399 VGSvcError("vgsvcBalloonInit: RTSemEventMultiWait failed; rc2=%Rrc\n", rc2);
400 rc = rc2;
401 break;
402 }
403 }
404
405 /* Cancel monitoring of the memory balloon change event. */
406 rc = VbglR3CtlFilterMask(0, VMMDEV_EVENT_BALLOON_CHANGE_REQUEST);
407 if (RT_FAILURE(rc))
408 VGSvcVerbose(3, "vgsvcBalloonInit: VbglR3CtlFilterMask failed with %Rrc\n", rc);
409
410 VGSvcVerbose(3, "vgsvcBalloonInit: finished mem balloon change request thread\n");
411 return VINF_SUCCESS;
412}
413
414
415/**
416 * @interface_method_impl{VBOXSERVICE,pfnStop}
417 */
418static DECLCALLBACK(void) vgsvcBalloonStop(void)
419{
420 RTSemEventMultiSignal(g_MemBalloonEvent);
421}
422
423
424/**
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/**
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 */
451 VGSvcDefaultPreInit,
452 VGSvcDefaultOption,
453 vgsvcBalloonInit,
454 vgsvcBalloonWorker,
455 vgsvcBalloonStop,
456 vgsvcBalloonTerm
457};
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette