qemu_domain: Partially validate memory amounts when auto-adding NUMA node

When automatically adding a NUMA node (qemuDomainDefNumaAutoAdd()) the
memory size of the node is computed as:

  total_memory - sum(memory devices)

And we have a nice helper for that: virDomainDefGetMemoryInitial() so
it looks logical to just call it. Except, this code runs in post parse
callback, i.e. memory sizes were not validated and it may happen that
the sum is greater than the total memory. This would be caught by
virDomainDefPostParseMemory() but that runs only after driver specific
callbacks (i.e. after qemuDomainDefNumaAutoAdd()) and because the
domain config was changed and memory was increased to this huge
number no error is caught.

So let's do what virDomainDefGetMemoryInitial() would do, but
with error checking.

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2216236
Fixes: f5d4f5c8ee
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Kristina Hanicova <khanicov@redhat.com>
This commit is contained in:
Michal Privoznik 2023-07-21 12:57:39 +02:00
parent 5b6badcfbf
commit baeefe0327

View File

@ -4812,7 +4812,7 @@ qemuDomainDefNumaAutoAdd(virDomainDef *def,
unsigned int parseFlags)
{
bool abiUpdate = !!(parseFlags & VIR_DOMAIN_DEF_PARSE_ABI_UPDATE);
unsigned long long initialMem;
unsigned long long nodeMem;
size_t i;
if (!abiUpdate ||
@ -4821,17 +4821,24 @@ qemuDomainDefNumaAutoAdd(virDomainDef *def,
return 0;
}
initialMem = virDomainDefGetMemoryInitial(def);
nodeMem = virDomainDefGetMemoryTotal(def);
if (!def->numa)
def->numa = virDomainNumaNew();
virDomainNumaSetNodeCount(def->numa, 1);
virDomainNumaSetNodeMemorySize(def->numa, 0, initialMem);
for (i = 0; i < def->nmems; i++) {
virDomainMemoryDef *mem = def->mems[i];
if (mem->size > nodeMem) {
virReportError(VIR_ERR_XML_ERROR, "%s",
_("Total size of memory devices exceeds the total memory size"));
return -1;
}
nodeMem -= mem->size;
switch (mem->model) {
case VIR_DOMAIN_MEMORY_MODEL_DIMM:
case VIR_DOMAIN_MEMORY_MODEL_NVDIMM:
@ -4848,6 +4855,8 @@ qemuDomainDefNumaAutoAdd(virDomainDef *def,
}
}
virDomainNumaSetNodeMemorySize(def->numa, 0, nodeMem);
return 0;
}