On a Tuesday in 2020, Michal Privoznik wrote:
So far, Libvirt configures memory-backend-* for memory hotplug,

*libvirt

possibly NUMA nodes and in a few other cases. This patch
switches to constructing the memory-backend-* command line for
all cases. To keep ability to migrate guests a little hack is
used: the ID of the object is set to the one that QEMU uses
internally anyways. These IDs are stable (first started to appear
somewhere around v0.13.0-rc0~96) and can't change.

In fact, this patch does exactly what QEMU does internally. The
reason for moving the logic into Libvirt is that QEMU wants to

*libvirt

deprecate the old style of specifying memory.

So far, only x84_64 test cases are changed, because tests for
other architectures use older capabilities, which still lack the
QEMU_CAPS_MACHINE_MEMORY_BACKEND capability and they don't report
the RAM ID.

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1836043

Signed-off-by: Michal Privoznik <mpriv...@redhat.com>
---
src/qemu/qemu_command.c                       | 69 ++++++++++++++++---
.../blkdeviotune-group-num.x86_64-latest.args |  3 +-
...blkdeviotune-max-length.x86_64-latest.args |  3 +-
.../blkdeviotune-max.x86_64-latest.args       |  3 +-
.../channel-unix-guestfwd.x86_64-latest.args  |  3 +-
.../console-virtio-unix.x86_64-latest.args    |  3 +-
.../controller-virtio-scsi.x86_64-latest.args |  3 +-
...-Icelake-Server-pconfig.x86_64-latest.args |  3 +-
.../cpu-translation.x86_64-latest.args        |  3 +-
.../cputune-cpuset-big-id.x86_64-latest.args  |  3 +-
.../disk-aio-io_uring.x86_64-latest.args      |  3 +-
.../disk-aio.x86_64-latest.args               |  3 +-
...-backing-chains-noindex.x86_64-latest.args |  3 +-
.../disk-cache.x86_64-latest.args             |  4 +-
.../disk-cdrom-bus-other.x86_64-latest.args   |  3 +-
...m-empty-network-invalid.x86_64-latest.args |  3 +-
.../disk-cdrom-network.x86_64-latest.args     |  3 +-
.../disk-cdrom-tray.x86_64-latest.args        |  3 +-
.../disk-cdrom.x86_64-latest.args             |  3 +-
.../disk-copy_on_read.x86_64-latest.args      |  3 +-
.../disk-detect-zeroes.x86_64-latest.args     |  3 +-
.../disk-discard.x86_64-latest.args           |  3 +-
.../disk-error-policy.x86_64-latest.args      |  3 +-
.../disk-floppy-q35-2_11.x86_64-latest.args   |  4 +-
.../disk-floppy-q35-2_9.x86_64-latest.args    |  4 +-
.../disk-floppy.x86_64-latest.args            |  3 +-
.../disk-network-gluster.x86_64-latest.args   |  3 +-
.../disk-network-http.x86_64-latest.args      |  3 +-
.../disk-network-iscsi.x86_64-latest.args     |  3 +-
.../disk-network-nbd.x86_64-latest.args       |  3 +-
.../disk-network-rbd.x86_64-latest.args       |  3 +-
.../disk-network-sheepdog.x86_64-latest.args  |  3 +-
...isk-network-source-auth.x86_64-latest.args |  3 +-
...isk-network-tlsx509-nbd.x86_64-latest.args |  3 +-
.../disk-nvme.x86_64-latest.args              |  3 +-
.../disk-readonly-disk.x86_64-latest.args     |  3 +-
.../disk-scsi-device-auto.x86_64-latest.args  |  3 +-
.../disk-scsi.x86_64-latest.args              |  3 +-
.../disk-shared.x86_64-latest.args            |  3 +-
.../disk-slices.x86_64-latest.args            |  3 +-
...irtio-scsi-reservations.x86_64-latest.args |  3 +-
.../eoi-disabled.x86_64-latest.args           |  3 +-
.../eoi-enabled.x86_64-latest.args            |  3 +-
.../floppy-drive-fat.x86_64-latest.args       |  3 +-
.../qemuxml2argvdata/fs9p.x86_64-latest.args  |  3 +-
.../genid-auto.x86_64-latest.args             |  3 +-
.../qemuxml2argvdata/genid.x86_64-latest.args |  3 +-
...egl-headless-rendernode.x86_64-latest.args |  3 +-
.../graphics-egl-headless.x86_64-latest.args  |  3 +-
...pice-gl-auto-rendernode.x86_64-latest.args |  3 +-
...graphics-vnc-tls-secret.x86_64-latest.args |  3 +-
.../graphics-vnc-tls.x86_64-latest.args       |  3 +-
...tdev-mdev-display-ramfb.x86_64-latest.args |  3 +-
...play-spice-egl-headless.x86_64-latest.args |  3 +-
...ev-display-spice-opengl.x86_64-latest.args |  3 +-
...isplay-vnc-egl-headless.x86_64-latest.args |  3 +-
...ostdev-mdev-display-vnc.x86_64-latest.args |  3 +-
.../hostdev-scsi-lsi.x86_64-latest.args       |  3 +-
...ostdev-scsi-virtio-scsi.x86_64-latest.args |  3 +-
.../hugepages-nvdimm.x86_64-latest.args       |  6 +-
.../hyperv-off.x86_64-latest.args             |  3 +-
.../hyperv-panic.x86_64-latest.args           |  3 +-
.../hyperv-stimer-direct.x86_64-latest.args   |  3 +-
.../hyperv.x86_64-latest.args                 |  3 +-
.../intel-iommu-aw-bits.x86_64-latest.args    |  4 +-
...ntel-iommu-caching-mode.x86_64-latest.args |  4 +-
...ntel-iommu-device-iotlb.x86_64-latest.args |  4 +-
.../intel-iommu-eim.x86_64-latest.args        |  4 +-
.../intel-iommu.x86_64-latest.args            |  3 +-
...threads-virtio-scsi-pci.x86_64-latest.args |  3 +-
.../kvmclock+eoi-disabled.x86_64-latest.args  |  3 +-
...luks-disks-source-qcow2.x86_64-latest.args |  4 +-
...memory-default-hugepage.x86_64-latest.args |  8 ++-
.../memfd-memory-numa.x86_64-latest.args      |  8 ++-
...y-hotplug-nvdimm-access.x86_64-latest.args |  4 +-
...ry-hotplug-nvdimm-align.x86_64-latest.args |  4 +-
...ry-hotplug-nvdimm-label.x86_64-latest.args |  4 +-
...ory-hotplug-nvdimm-pmem.x86_64-latest.args |  4 +-
...hotplug-nvdimm-readonly.x86_64-latest.args |  4 +-
.../memory-hotplug-nvdimm.x86_64-latest.args  |  4 +-
.../mlock-off.x86_64-latest.args              |  3 +-
.../mlock-on.x86_64-latest.args               |  3 +-
.../net-vhostuser.x86_64-latest.args          |  3 +-
.../numatune-hmat.x86_64-latest.args          |  4 +-
.../os-firmware-bios.x86_64-latest.args       |  4 +-
...os-firmware-efi-secboot.x86_64-latest.args |  4 +-
.../os-firmware-efi.x86_64-latest.args        |  4 +-
.../parallel-unix-chardev.x86_64-latest.args  |  3 +-
...cie-root-port-nohotplug.x86_64-latest.args |  3 +-
.../pv-spinlock-disabled.x86_64-latest.args   |  3 +-
.../pv-spinlock-enabled.x86_64-latest.args    |  3 +-
.../qemu-ns.x86_64-latest.args                |  3 +-
.../serial-unix-chardev.x86_64-latest.args    |  3 +-
...rtcard-passthrough-unix.x86_64-latest.args |  3 +-
.../tpm-emulator-tpm2-enc.x86_64-latest.args  |  4 +-
.../tpm-emulator-tpm2.x86_64-latest.args      |  4 +-
.../tpm-emulator.x86_64-latest.args           |  4 +-
.../tpm-passthrough-crb.x86_64-latest.args    |  4 +-
.../tpm-passthrough.x86_64-latest.args        |  4 +-
.../tseg-explicit-size.x86_64-latest.args     |  4 +-
.../usb-redir-unix.x86_64-latest.args         |  3 +-
...vhost-user-fs-fd-memory.x86_64-latest.args |  4 +-
...vhost-user-fs-hugepages.x86_64-latest.args |  5 +-
...host-user-gpu-secondary.x86_64-latest.args |  3 +-
.../vhost-user-vga.x86_64-latest.args         |  3 +-
.../vhost-vsock-auto.x86_64-latest.args       |  4 +-
.../vhost-vsock.x86_64-latest.args            |  3 +-
...eo-bochs-display-device.x86_64-latest.args |  3 +-
...video-qxl-device-vram64.x86_64-latest.args |  3 +-
...o-qxl-sec-device-vram64.x86_64-latest.args |  3 +-
...eo-ramfb-display-device.x86_64-latest.args |  3 +-
.../virtio-9p-multidevs.x86_64-latest.args    |  3 +-
...virtio-non-transitional.x86_64-latest.args |  3 +-
...-options-controller-ats.x86_64-latest.args |  3 +-
...ptions-controller-iommu.x86_64-latest.args |  3 +-
...tions-controller-packed.x86_64-latest.args |  3 +-
...virtio-options-disk-ats.x86_64-latest.args |  3 +-
...rtio-options-disk-iommu.x86_64-latest.args |  3 +-
...tio-options-disk-packed.x86_64-latest.args |  3 +-
.../virtio-options-fs-ats.x86_64-latest.args  |  3 +-
...virtio-options-fs-iommu.x86_64-latest.args |  3 +-
...irtio-options-fs-packed.x86_64-latest.args |  3 +-
...irtio-options-input-ats.x86_64-latest.args |  3 +-
...tio-options-input-iommu.x86_64-latest.args |  3 +-
...io-options-input-packed.x86_64-latest.args |  3 +-
...-options-memballoon-ats.x86_64-latest.args |  3 +-
...ptions-memballoon-iommu.x86_64-latest.args |  3 +-
...tions-memballoon-packed.x86_64-latest.args |  3 +-
.../virtio-options-net-ats.x86_64-latest.args |  3 +-
...irtio-options-net-iommu.x86_64-latest.args |  3 +-
...rtio-options-net-packed.x86_64-latest.args |  3 +-
.../virtio-options-rng-ats.x86_64-latest.args |  3 +-
...irtio-options-rng-iommu.x86_64-latest.args |  3 +-
...rtio-options-rng-packed.x86_64-latest.args |  3 +-
...irtio-options-video-ats.x86_64-latest.args |  3 +-
...tio-options-video-iommu.x86_64-latest.args |  3 +-
...io-options-video-packed.x86_64-latest.args |  3 +-
.../virtio-options.x86_64-latest.args         |  3 +-
.../virtio-rng-builtin.x86_64-latest.args     |  3 +-
.../virtio-rng-egd-unix.x86_64-latest.args    |  3 +-
.../virtio-transitional.x86_64-latest.args    |  3 +-
...-default-cpu-kvm-pc-4.2.x86_64-latest.args |  4 +-
...default-cpu-kvm-q35-4.2.x86_64-latest.args |  4 +-
...-default-cpu-tcg-pc-4.2.x86_64-latest.args |  4 +-
...default-cpu-tcg-q35-4.2.x86_64-latest.args |  4 +-
.../x86_64-pc-graphics.x86_64-latest.args     |  3 +-
.../x86_64-pc-headless.x86_64-latest.args     |  3 +-
.../x86_64-q35-graphics.x86_64-latest.args    |  3 +-
.../x86_64-q35-headless.x86_64-latest.args    |  3 +-
149 files changed, 395 insertions(+), 163 deletions(-)

diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index ecbdd226a4..b62dc78893 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -7015,6 +7015,14 @@ qemuBuildMachineCommandLine(virCommandPtr cmd,
    if (virDomainNumaHasHMAT(def->numa))
        virBufferAddLit(&buf, ",hmat=on");

+    if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_MACHINE_MEMORY_BACKEND)) {
+        const char *defaultRAMid = virQEMUCapsGetMachineDefaultRAMid(qemuCaps,
+                                                                     
def->virtType,
+                                                                     
def->os.machine);
+        if (defaultRAMid)
+            virBufferAsprintf(&buf, ",memory-backend=%s", defaultRAMid);
+    }
+
    virCommandAddArgBuffer(cmd, &buf);

    return 0;
@@ -7124,12 +7132,42 @@ qemuBuildMemPathStr(const virDomainDef *def,
}


+static int
+qemuBuildMemCommandLineMemoryBackendNew(virCommandPtr cmd,

s/BackendNew/DefaultBackend/

or

MachineBackend

'New' does not age well.

+                                        const virDomainDef *def,
+                                        qemuDomainObjPrivatePtr priv,
+                                        const char *defaultRAMid)
+{
+    g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(priv->driver);
+    g_autoptr(virJSONValue) props = NULL;
+    g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
+    virDomainMemoryDef mem = { 0 };
+
+    mem.size = virDomainDefGetMemoryInitial(def);
+    mem.targetNode = -1;
+    mem.info.alias = (char *) defaultRAMid;
+
+    if (qemuBuildMemoryBackendProps(&props, defaultRAMid, cfg,
+                                    priv, def, &mem, false) < 0)
+        return -1;
+
+    if (virQEMUBuildObjectCommandlineFromJSON(&buf, props) < 0)
+        return -1;
+
+    virCommandAddArg(cmd, "-object");
+    virCommandAddArgBuffer(cmd, &buf);
+    return 0;
+}
+
+
static int
qemuBuildMemCommandLine(virCommandPtr cmd,
                        const virDomainDef *def,
                        virQEMUCapsPtr qemuCaps,
                        qemuDomainObjPrivatePtr priv)
{
+    const char *defaultRAMid = NULL;
+
    virCommandAddArg(cmd, "-m");

    if (virDomainDefHasMemoryHotplug(def)) {
@@ -7144,19 +7182,28 @@ qemuBuildMemCommandLine(virCommandPtr cmd,
                              virDomainDefGetMemoryInitial(def) / 1024);
    }

-    if (def->mem.allocation == VIR_DOMAIN_MEMORY_ALLOCATION_IMMEDIATE) {
-        virCommandAddArgList(cmd, "-mem-prealloc", NULL);
-        priv->memPrealloc = true;
+    defaultRAMid = virQEMUCapsGetMachineDefaultRAMid(qemuCaps,
+                                                     def->virtType,
+                                                     def->os.machine);
+
+    if (defaultRAMid &&
+        virQEMUCapsGet(qemuCaps, QEMU_CAPS_MACHINE_MEMORY_BACKEND)) {
+        qemuBuildMemCommandLineMemoryBackendNew(cmd, def, priv, defaultRAMid);
+    } else {
+        if (def->mem.allocation == VIR_DOMAIN_MEMORY_ALLOCATION_IMMEDIATE) {
+            virCommandAddArgList(cmd, "-mem-prealloc", NULL);
+            priv->memPrealloc = true;
+        }
+
+        /*
+         * Add '-mem-path' (and '-mem-prealloc') parameter here if
+         * the hugepages and no numa node is specified.
+         */
+        if (!virDomainNumaGetNodeCount(def->numa) &&
+            qemuBuildMemPathStr(def, cmd, priv) < 0)
+            return -1;
    }

-    /*
-     * Add '-mem-path' (and '-mem-prealloc') parameter here if
-     * the hugepages and no numa node is specified.
-     */
-    if (!virDomainNumaGetNodeCount(def->numa) &&
-        qemuBuildMemPathStr(def, cmd, priv) < 0)
-        return -1;
-
    if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_OVERCOMMIT)) {
        virCommandAddArg(cmd, "-overcommit");
        virCommandAddArgFormat(cmd, "mem-lock=%s", def->mem.locked ? "on" : 
"off");

diff --git 
a/tests/qemuxml2argvdata/memfd-memory-default-hugepage.x86_64-latest.args 
b/tests/qemuxml2argvdata/memfd-memory-default-hugepage.x86_64-latest.args
index 5d256c42bc..b43e7d9c3c 100644
--- a/tests/qemuxml2argvdata/memfd-memory-default-hugepage.x86_64-latest.args
+++ b/tests/qemuxml2argvdata/memfd-memory-default-hugepage.x86_64-latest.args
@@ -12,14 +12,16 @@ QEMU_AUDIO_DRV=none \
-S \
-object secret,id=masterKey0,format=raw,\
file=/tmp/lib/domain--1-instance-00000092/master-key.aes \
--machine pc-i440fx-2.3,accel=kvm,usb=off,dump-guest-core=off \
+-machine pc-i440fx-2.3,accel=kvm,usb=off,dump-guest-core=off,\
+memory-backend=pc.ram \
-cpu qemu64 \
-m 14336 \
--mem-prealloc \
+-object memory-backend-memfd,id=pc.ram,hugetlb=yes,hugetlbsize=2097152,\
+share=yes,prealloc=yes,size=15032385536 \
-overcommit mem-lock=off \
-smp 8,sockets=1,dies=1,cores=8,threads=1 \
-object memory-backend-memfd,id=ram-node0,hugetlb=yes,hugetlbsize=2097152,\
-share=yes,size=15032385536,host-nodes=3,policy=preferred \
+share=yes,prealloc=yes,size=15032385536,host-nodes=3,policy=preferred \
-numa node,nodeid=0,cpus=0-7,memdev=ram-node0 \
-uuid 126f2720-6f8e-45ab-a886-ec9277079a67 \
-display none \

Should we format all the fields twice in these cases?

Jano

Attachment: signature.asc
Description: PGP signature

Reply via email to