]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
qemu: Account for both memballoon and virtio-mem
authorMichal Privoznik <mprivozn@redhat.com>
Mon, 15 Mar 2021 16:18:56 +0000 (17:18 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Fri, 1 Oct 2021 09:04:57 +0000 (11:04 +0200)
Reporting how much memory is exposed to the guest happens under
<currentMemory/> which is taken from def->mem.cur_balloon. The
reported amount should account for both balloon size and the sum
of @currentsize of all virtio-mems. For instance, if domain has
4GiB via balloon and additional 2GiB via virtio-mem, then the
domain XML should report 6GiB. The same applies for domain
statistics.

The way to achieve this is to account for either balloon or
virtio-mem when the size of the other is changed, e.g. on balloon
change we have to add all @currentsize (for non virtio-mem these
will be zero, so the check for memory model is needless, but
makes it more obvious what's happening), and vice versa.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/qemu/qemu_driver.c
src/qemu/qemu_process.c

index cd11b41905e4fece8634c4e0c35ce3c28fff04a3..9f3154e548bcc00288f2037eb74d4f168f0b7318 100644 (file)
@@ -4253,6 +4253,7 @@ processMemoryDeviceSizeChange(virQEMUDriver *driver,
 {
     virDomainMemoryDef *mem = NULL;
     virObjectEvent *event = NULL;
+    unsigned long long balloon;
 
     if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_MODIFY) < 0)
         return;
@@ -4268,7 +4269,15 @@ processMemoryDeviceSizeChange(virQEMUDriver *driver,
         goto endjob;
     }
 
+    /* If this looks weird it's because it is. The balloon size
+     * as reported by QEMU does not include any of @currentsize.
+     * It really contains just the balloon size. But in domain
+     * definition we want to report also sum of @currentsize. Do
+     * a bit of math to fix the domain definition. */
+    balloon = vm->def->mem.cur_balloon - mem->currentsize;
     mem->currentsize = VIR_DIV_UP(info->size, 1024);
+    balloon += mem->currentsize;
+    vm->def->mem.cur_balloon = balloon;
 
     event = virDomainEventMemoryDeviceSizeChangeNewFromObj(vm,
                                                            info->devAlias,
index 6e6de835407f32cdea8af5bea10f4fdd107687c6..1d0165af6daaf44db6db28110f5c3b445bae15e7 100644 (file)
@@ -1213,10 +1213,22 @@ qemuProcessHandleBalloonChange(qemuMonitor *mon G_GNUC_UNUSED,
     virQEMUDriver *driver = opaque;
     virObjectEvent *event = NULL;
     g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
+    size_t i;
 
     virObjectLock(vm);
     event = virDomainEventBalloonChangeNewFromObj(vm, actual);
 
+    /* We want the balloon size stored in domain definition to
+     * account for the actual size of virtio-mem too. But the
+     * balloon size as reported by QEMU (@actual) contains just
+     * the balloon size without any virtio-mem. Do a wee bit of
+     * math to fix it. */
+    VIR_DEBUG("balloon size before fix is %lld", actual);
+    for (i = 0; i < vm->def->nmems; i++) {
+        if (vm->def->mems[i]->model == VIR_DOMAIN_MEMORY_MODEL_VIRTIO_MEM)
+            actual += vm->def->mems[i]->currentsize;
+    }
+
     VIR_DEBUG("Updating balloon from %lld to %lld kb",
               vm->def->mem.cur_balloon, actual);
     vm->def->mem.cur_balloon = actual;
@@ -2343,6 +2355,7 @@ qemuProcessRefreshBalloonState(virQEMUDriver *driver,
                                int asyncJob)
 {
     unsigned long long balloon;
+    size_t i;
     int rc;
 
     /* if no ballooning is available, the current size equals to the current
@@ -2359,6 +2372,18 @@ qemuProcessRefreshBalloonState(virQEMUDriver *driver,
     if (qemuDomainObjExitMonitor(driver, vm) < 0 || rc < 0)
         return -1;
 
+    /* We want the balloon size stored in domain definition to
+     * account for the actual size of virtio-mem too. But the
+     * balloon size as reported by QEMU (@balloon) contains just
+     * the balloon size without any virtio-mem. Do a wee bit of
+     * math to fix it. */
+    VIR_DEBUG("balloon size before fix is %lld", balloon);
+    for (i = 0; i < vm->def->nmems; i++) {
+        if (vm->def->mems[i]->model == VIR_DOMAIN_MEMORY_MODEL_VIRTIO_MEM)
+            balloon += vm->def->mems[i]->currentsize;
+    }
+    VIR_DEBUG("Updating balloon from %lld to %lld kb",
+              vm->def->mem.cur_balloon, balloon);
     vm->def->mem.cur_balloon = balloon;
 
     return 0;