]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
qemuDomainSetMemoryFlags: Take virtio-mem into consideration
authorMichal Privoznik <mprivozn@redhat.com>
Tue, 16 Mar 2021 18:09:27 +0000 (19:09 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Fri, 1 Oct 2021 09:05:02 +0000 (11:05 +0200)
The qemuDomainSetMemoryFlags() allows for memballoon
(<currentMemory/>) changes for both active and inactive guests.
And just before doing any change, we have to make sure that the
new size is not greater than the total memory (<memory/>).

However, the total memory includes not only the regular guest
memory, but also sum of maximum sizes of all virtio-mems (in fact
all memory devices for that matter). But virtio-mem devices are
modified differently (via virDomainUpdateDevice()) and thus the
upper limit for new balloon size has to be lowered.

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

index 9f3154e548bcc00288f2037eb74d4f168f0b7318..760d30a867f75069b958d4b427e5532b7af8473c 100644 (file)
@@ -2381,12 +2381,28 @@ static int qemuDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem,
     } else {
         /* resize the current memory */
         unsigned long oldmax = 0;
+        size_t i;
 
-        if (def)
+        if (def) {
             oldmax = virDomainDefGetMemoryTotal(def);
+
+            /* While virtio-mem is regular mem from guest POV, it can't be
+             * modified through this API. */
+            for (i = 0; i < def->nmems; i++) {
+                if (def->mems[i]->model == VIR_DOMAIN_MEMORY_MODEL_VIRTIO_MEM)
+                    oldmax -= def->mems[i]->size;
+            }
+        }
+
         if (persistentDef) {
-            if (!oldmax || oldmax > virDomainDefGetMemoryTotal(persistentDef))
+            if (!def || oldmax > virDomainDefGetMemoryTotal(persistentDef)) {
                 oldmax = virDomainDefGetMemoryTotal(persistentDef);
+
+                for (i = 0; i < persistentDef->nmems; i++) {
+                    if (persistentDef->mems[i]->model == VIR_DOMAIN_MEMORY_MODEL_VIRTIO_MEM)
+                        oldmax -= persistentDef->mems[i]->size;
+                }
+            }
         }
 
         if (newmem > oldmax) {