From 3009edff8192991293fe9e2b50b0d90db83c4a89 Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Mon, 9 Nov 2020 17:43:55 +0000 Subject: vhost-user: fix VHOST_USER_ADD/REM_MEM_REG truncation QEMU currently truncates the mmap_offset field when sending VHOST_USER_ADD_MEM_REG and VHOST_USER_REM_MEM_REG messages. The struct layout looks like this: typedef struct VhostUserMemoryRegion { uint64_t guest_phys_addr; uint64_t memory_size; uint64_t userspace_addr; uint64_t mmap_offset; } VhostUserMemoryRegion; typedef struct VhostUserMemRegMsg { uint32_t padding; /* WARNING: there is a 32-bit hole here! */ VhostUserMemoryRegion region; } VhostUserMemRegMsg; The payload size is calculated as follows when sending the message in hw/virtio/vhost-user.c: msg->hdr.size = sizeof(msg->payload.mem_reg.padding) + sizeof(VhostUserMemoryRegion); This calculation produces an incorrect result of only 36 bytes. sizeof(VhostUserMemRegMsg) is actually 40 bytes. The consequence of this is that the final field, mmap_offset, is truncated. This breaks x86_64 TCG guests on s390 hosts. Other guest/host combinations may get lucky if either of the following holds: 1. The guest memory layout does not need mmap_offset != 0. 2. The host is little-endian and mmap_offset <= 0xffffffff so the truncation has no effect. Fix this by extending the existing 32-bit padding field to 64-bit. Now the padding reflects the actual compiler padding. This can be verified using pahole(1). Also document the layout properly in the vhost-user specification. The vhost-user spec did not document the exact layout. It would be impossible to implement the spec without looking at the QEMU source code. Existing vhost-user frontends and device backends continue to work after this fix has been applied. The only change in the wire protocol is that QEMU now sets hdr.size to 40 instead of 36. If a vhost-user implementation has a hardcoded size check for 36 bytes, then it will fail with new QEMUs. Both QEMU and DPDK/SPDK don't check the exact payload size, so they continue to work. Fixes: f1aeb14b0809e313c74244d838645ed25e85ea63 ("Transmit vhost-user memory regions individually") Cc: Raphael Norwitz Cc: Cornelia Huck Cc: Michael S. Tsirkin Cc: Christian Borntraeger Signed-off-by: Stefan Hajnoczi Message-Id: <20201109174355.1069147-1-stefanha@redhat.com> Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin Fixes: f1aeb14b0809 ("Transmit vhost-user memory regions individually") Reviewed-by: Cornelia Huck Reviewed-by: Raphael Norwitz --- hw/virtio/vhost-user.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'hw') diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index 9c5b4f7fbc..2fdd5daf74 100644 --- a/hw/virtio/vhost-user.c +++ b/hw/virtio/vhost-user.c @@ -149,7 +149,7 @@ typedef struct VhostUserMemory { } VhostUserMemory; typedef struct VhostUserMemRegMsg { - uint32_t padding; + uint64_t padding; VhostUserMemoryRegion region; } VhostUserMemRegMsg; @@ -800,8 +800,7 @@ static int vhost_user_add_remove_regions(struct vhost_dev *dev, uint64_t shadow_pcb[VHOST_USER_MAX_RAM_SLOTS] = {}; int nr_add_reg, nr_rem_reg; - msg->hdr.size = sizeof(msg->payload.mem_reg.padding) + - sizeof(VhostUserMemoryRegion); + msg->hdr.size = sizeof(msg->payload.mem_reg); /* Find the regions which need to be removed or added. */ scrub_shadow_regions(dev, add_reg, &nr_add_reg, rem_reg, &nr_rem_reg, -- cgit v1.2.3 From 727a06326c9ebf4ad8ed80eb533278bc60202804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Date: Sat, 7 Nov 2020 20:40:45 +0100 Subject: hw/i386/acpi-build: Fix maybe-uninitialized error when ACPI hotplug off MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GCC 9.3.0 thinks that 'method' can be left uninitialized. This code is already in the "if (bsel || pcihp_bridge_en)" block statement, but it isn't smart enough to figure it out. Restrict the code to be used only in the "if (bsel || pcihp_bridge_en)" block statement to fix (on Ubuntu): ../hw/i386/acpi-build.c: In function 'build_append_pci_bus_devices': ../hw/i386/acpi-build.c:496:9: error: 'method' may be used uninitialized in this function [-Werror=maybe-uninitialized] 496 | aml_append(parent_scope, method); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ cc1: all warnings being treated as errors Fixes: df4008c9c59 ("piix4: don't reserve hw resources when hotplug is off globally") Signed-off-by: Philippe Mathieu-Daudé Message-Id: <20201107194045.438027-1-philmd@redhat.com> Acked-by: Ani Sinha Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/i386/acpi-build.c | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) (limited to 'hw') diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c index 4f66642d88..1f5c211245 100644 --- a/hw/i386/acpi-build.c +++ b/hw/i386/acpi-build.c @@ -465,34 +465,31 @@ static void build_append_pci_bus_devices(Aml *parent_scope, PCIBus *bus, */ if (bsel || pcihp_bridge_en) { method = aml_method("PCNT", 0, AML_NOTSERIALIZED); - } - /* If bus supports hotplug select it and notify about local events */ - if (bsel) { - uint64_t bsel_val = qnum_get_uint(qobject_to(QNum, bsel)); - aml_append(method, aml_store(aml_int(bsel_val), aml_name("BNUM"))); - aml_append(method, - aml_call2("DVNT", aml_name("PCIU"), aml_int(1) /* Device Check */) - ); - aml_append(method, - aml_call2("DVNT", aml_name("PCID"), aml_int(3)/* Eject Request */) - ); - } + /* If bus supports hotplug select it and notify about local events */ + if (bsel) { + uint64_t bsel_val = qnum_get_uint(qobject_to(QNum, bsel)); - /* Notify about child bus events in any case */ - if (pcihp_bridge_en) { - QLIST_FOREACH(sec, &bus->child, sibling) { - int32_t devfn = sec->parent_dev->devfn; + aml_append(method, aml_store(aml_int(bsel_val), aml_name("BNUM"))); + aml_append(method, aml_call2("DVNT", aml_name("PCIU"), + aml_int(1))); /* Device Check */ + aml_append(method, aml_call2("DVNT", aml_name("PCID"), + aml_int(3))); /* Eject Request */ + } - if (pci_bus_is_root(sec) || pci_bus_is_express(sec)) { - continue; - } + /* Notify about child bus events in any case */ + if (pcihp_bridge_en) { + QLIST_FOREACH(sec, &bus->child, sibling) { + int32_t devfn = sec->parent_dev->devfn; + + if (pci_bus_is_root(sec) || pci_bus_is_express(sec)) { + continue; + } - aml_append(method, aml_name("^S%.02X.PCNT", devfn)); + aml_append(method, aml_name("^S%.02X.PCNT", devfn)); + } } - } - if (bsel || pcihp_bridge_en) { aml_append(parent_scope, method); } qobject_unref(bsel); -- cgit v1.2.3