Skip to contentSkip to conversation

Misc fixes, for thread-pool, xen, and xen-emulate

GIT PULL Applied Anthony PERARD via 4 participants 9 messages Aug 1, 2023 – Aug 2, 2023

lore
Share & tools
lore
#0 lore raw

The following changes since commit 802341823f1720511dd5cf53ae40285f7978c61b:

Merge tag 'pull-tcg-20230731' of https://gitlab.com/rth7680/qemu into staging (2023-07-31 14:02:51 -0700)

are available in the Git repository at:

https://xenbits.xen.org/git-http/people/aperard/qemu-dm.git tags/pull-xen-20230801

for you to fetch changes up to 856ca10f9ce1fcffeab18546b36a64f79017c905:

xen-platform: do full PCI reset during unplug of IDE devices (2023-08-01 10:22:33 +0100)

----------------------------------------------------------------
Misc fixes, for thread-pool, xen, and xen-emulate

* fix an access to request_cond QemuCond in thread-pool
* fix issue with PCI devices when unplugging IDE devices in Xen guest
* several fixes for issues pointed out by Coverity

----------------------------------------------------------------
Anthony PERARD (2):
xen-block: Avoid leaks on new error path
thread-pool: signal "request_cond" while locked

David Woodhouse (1):
hw/xen: Clarify (lack of) error handling in transaction_commit()

Olaf Hering (1):
xen-platform: do full PCI reset during unplug of IDE devices

Peter Maydell (1):
xen: Don't pass MemoryListener around by value

 hw/arm/xen_arm.c                |  4 ++--
 hw/block/xen-block.c            | 11 ++++++-----
 hw/i386/kvm/xenstore_impl.c     | 12 +++++++++++-
 hw/i386/xen/xen-hvm.c           |  4 ++--
 hw/i386/xen/xen_platform.c      |  7 ++++---
 hw/xen/xen-hvm-common.c         |  8 ++++----
 include/hw/xen/xen-hvm-common.h |  2 +-
 util/thread-pool.c              |  2 +-
 8 files changed, 31 insertions(+), 19 deletions(-)
#1 lore raw

[PULL 2/5] xen-block: Avoid leaks on new error path

From: Anthony PERARD <[email protected]>

Commit 189829399070 ("xen-block: Use specific blockdev driver") introduced a new error path, without taking care of allocated resources.

So only allocate the qdicts after the error check, and free both filename and driver when we are about to return and thus taking care of both success and error path.

Coverity only spotted the leak of qdicts (*_layer variables).

Fixes Coverity CID 1508722, 1398649
Fixes 189829399070 ("xen-block: Use specific blockdev driver")

Message-Id: <[email protected]>

---
 hw/block/xen-block.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
hw/block/xen-block.c +6 -5
index f099914831..3906b9058b 100644
@@ -781,14 +781,15 @@ static XenBlockDrive *xen_block_drive_create(const char *id,
     drive = g_new0(XenBlockDrive, 1);
     drive->id = g_strdup(id);
 
-    file_layer = qdict_new();
-    driver_layer = qdict_new();
-
     rc = stat(filename, &st);
     if (rc) {
         error_setg_errno(errp, errno, "Could not stat file '%s'", filename);
         goto done;
     }
+
+    file_layer = qdict_new();
+    driver_layer = qdict_new();
+
     if (S_ISBLK(st.st_mode)) {
         qdict_put_str(file_layer, "driver", "host_device");
     } else {
@@ -796,7 +797,6 @@ static XenBlockDrive *xen_block_drive_create(const char *id,
     }
 
     qdict_put_str(file_layer, "filename", filename);
-    g_free(filename);
 
     if (mode && *mode != 'w') {
         qdict_put_bool(file_layer, "read-only", true);
@@ -831,7 +831,6 @@ static XenBlockDrive *xen_block_drive_create(const char *id,
     qdict_put_str(file_layer, "locking", "off");
 
     qdict_put_str(driver_layer, "driver", driver);
-    g_free(driver);
 
     qdict_put(driver_layer, "file", file_layer);
 
@@ -842,6 +841,8 @@ static XenBlockDrive *xen_block_drive_create(const char *id,
     qobject_unref(driver_layer);
 
 done:
+    g_free(filename);
+    g_free(driver);
     if (*errp) {
         xen_block_drive_destroy(drive, NULL);
         return NULL;
Signature · 1 line
Anthony PERARD
#2 lore raw

[PULL 4/5] xen: Don't pass MemoryListener around by value

In reply to Anthony PERARD via

From: Peter Maydell <[email protected]>

Coverity points out (CID 1513106, 1513107) that MemoryListener is a 192 byte struct which we are passing around by value. Switch to passing a const pointer into xen_register_ioreq() and then to xen_do_ioreq_register(). We can also make the file-scope MemoryListener variables const, since nothing changes them.

Message-Id: <[email protected]>

---
 hw/arm/xen_arm.c                | 4 ++--
 hw/i386/xen/xen-hvm.c           | 4 ++--
 hw/xen/xen-hvm-common.c         | 8 ++++----
 include/hw/xen/xen-hvm-common.h | 2 +-
 4 files changed, 9 insertions(+), 9 deletions(-)
hw/arm/xen_arm.c +2 -2
index 044093fec7..1d3e6d481a 100644
@@ -37,7 +37,7 @@
 #define TYPE_XEN_ARM  MACHINE_TYPE_NAME("xenpvh")
 OBJECT_DECLARE_SIMPLE_TYPE(XenArmState, XEN_ARM)
 
-static MemoryListener xen_memory_listener = {
+static const MemoryListener xen_memory_listener = {
     .region_add = xen_region_add,
     .region_del = xen_region_del,
     .log_start = NULL,
@@ -108,7 +108,7 @@ static void xen_arm_init(MachineState *machine)
 
     xam->state =  g_new0(XenIOState, 1);
 
-    xen_register_ioreq(xam->state, machine->smp.cpus, xen_memory_listener);
+    xen_register_ioreq(xam->state, machine->smp.cpus, &xen_memory_listener);
 
 #ifdef CONFIG_TPM
     if (xam->cfg.tpm_base_addr) {
hw/i386/xen/xen-hvm.c +2 -2
index 3da5a2b23f..f42621e674 100644
@@ -458,7 +458,7 @@ static void xen_log_global_stop(MemoryListener *listener)
     xen_in_migration = false;
 }
 
-static MemoryListener xen_memory_listener = {
+static const MemoryListener xen_memory_listener = {
     .name = "xen-memory",
     .region_add = xen_region_add,
     .region_del = xen_region_del,
@@ -582,7 +582,7 @@ void xen_hvm_init_pc(PCMachineState *pcms, MemoryRegion **ram_memory)
 
     state = g_new0(XenIOState, 1);
 
-    xen_register_ioreq(state, max_cpus, xen_memory_listener);
+    xen_register_ioreq(state, max_cpus, &xen_memory_listener);
 
     QLIST_INIT(&xen_physmap);
     xen_read_physmap(state);
hw/xen/xen-hvm-common.c +4 -4
index 886c3ee944..565dc39c8f 100644
@@ -765,8 +765,8 @@ void xen_shutdown_fatal_error(const char *fmt, ...)
 }
 
 static void xen_do_ioreq_register(XenIOState *state,
-                                           unsigned int max_cpus,
-                                           MemoryListener xen_memory_listener)
+                                  unsigned int max_cpus,
+                                  const MemoryListener *xen_memory_listener)
 {
     int i, rc;
 
@@ -824,7 +824,7 @@ static void xen_do_ioreq_register(XenIOState *state,
 
     qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);
 
-    state->memory_listener = xen_memory_listener;
+    state->memory_listener = *xen_memory_listener;
     memory_listener_register(&state->memory_listener, &address_space_memory);
 
     state->io_listener = xen_io_listener;
@@ -842,7 +842,7 @@ static void xen_do_ioreq_register(XenIOState *state,
 }
 
 void xen_register_ioreq(XenIOState *state, unsigned int max_cpus,
-                        MemoryListener xen_memory_listener)
+                        const MemoryListener *xen_memory_listener)
 {
     int rc;
 
include/hw/xen/xen-hvm-common.h +1 -1
index f9559e2885..4e9904f1a6 100644
@@ -93,7 +93,7 @@ void xen_device_unrealize(DeviceListener *listener, DeviceState *dev);
 
 void xen_hvm_change_state_handler(void *opaque, bool running, RunState rstate);
 void xen_register_ioreq(XenIOState *state, unsigned int max_cpus,
-                        MemoryListener xen_memory_listener);
+                        const MemoryListener *xen_memory_listener);
 
 void cpu_ioreq_pio(ioreq_t *req);
 #endif /* HW_XEN_HVM_COMMON_H */
Signature · 1 line
Anthony PERARD
#3 lore raw

[PULL 3/5] thread-pool: signal "request_cond" while locked

In reply to Anthony PERARD via

From: Anthony PERARD <[email protected]>

thread_pool_free() might have been called on the pool, which would be a reason for worker_thread() to quit. In this case, pool->request_cond is been destroyed.

If worker_thread() didn't managed to signal request_cond before it been destroyed by thread_pool_free(), we got:

    util/qemu-thread-posix.c:198: qemu_cond_signal: Assertion `cond->initialized' failed.

One backtrace:

    __GI___assert_fail (assertion=0x55555614abcb "cond->initialized", file=0x55555614ab88 "util/qemu-thread-posix.c", line=198,
	function=0x55555614ad80 <__PRETTY_FUNCTION__.17104> "qemu_cond_signal") at assert.c:101
    qemu_cond_signal (cond=0x7fffb800db30) at util/qemu-thread-posix.c:198
    worker_thread (opaque=0x7fffb800dab0) at util/thread-pool.c:129
    qemu_thread_start (args=0x7fffb8000b20) at util/qemu-thread-posix.c:505
    start_thread (arg=<optimized out>) at pthread_create.c:486

Reported here:
https://lore.kernel.org/all/[email protected]/T/#u

To avoid issue, keep lock while sending a signal to request_cond.

Fixes 900fa208f506 ("thread-pool: replace semaphore with condition variable")

Message-Id: <[email protected]>

---
 util/thread-pool.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/thread-pool.c +1 -1
index 0d97888df0..e3d8292d14 100644
@@ -120,13 +120,13 @@ static void *worker_thread(void *opaque)
 
     pool->cur_threads--;
     qemu_cond_signal(&pool->worker_stopped);
-    qemu_mutex_unlock(&pool->lock);
 
     /*
      * Wake up another thread, in case we got a wakeup but decided
      * to exit due to pool->cur_threads > pool->max_threads.
      */
     qemu_cond_signal(&pool->request_cond);
+    qemu_mutex_unlock(&pool->lock);
     return NULL;
 }
 
Signature · 1 line
Anthony PERARD
#4 lore raw

[PULL 1/5] hw/xen: Clarify (lack of) error handling in transaction_commit()

In reply to Anthony PERARD via

From: David Woodhouse <[email protected]>

Coverity was unhappy (CID 1508359) because we didn't check the return of init_walk_op() in transaction_commit(), despite doing so at every other call site.

Strictly speaking, this is a false positive since it can never fail. It only fails for invalid user input (transaction ID or path), and both of those are hard-coded to known sane values in this invocation.

But Coverity doesn't know that, and neither does the casual reader of the code.

Returning an error here would be weird, since the transaction *is* committed by this point; all the walk_op is doing is firing watches on the newly-committed changed nodes. So make it a g_assert(!ret), since it really should never happen.

Message-Id: <[email protected]>

---
 hw/i386/kvm/xenstore_impl.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
hw/i386/kvm/xenstore_impl.c +11 -1
index 305fe75519..d9732b567e 100644
@@ -1022,6 +1022,7 @@ static int transaction_commit(XenstoreImplState *s, XsTransaction *tx)
 {
     struct walk_op op;
     XsNode **n;
+    int ret;
 
     if (s->root_tx != tx->base_tx) {
         return EAGAIN;
@@ -1032,7 +1033,16 @@ static int transaction_commit(XenstoreImplState *s, XsTransaction *tx)
     s->root_tx = tx->tx_id;
     s->nr_nodes = tx->nr_nodes;
 
-    init_walk_op(s, &op, XBT_NULL, tx->dom_id, "/", &n);
+    ret = init_walk_op(s, &op, XBT_NULL, tx->dom_id, "/", &n);
+    /*
+     * There are two reasons why init_walk_op() may fail: an invalid tx_id,
+     * or an invalid path. We pass XBT_NULL and "/", and it cannot fail.
+     * If it does, the world is broken. And returning 'ret' would be weird
+     * because the transaction *was* committed, and all this tree walk is
+     * trying to do is fire the resulting watches on newly-committed nodes.
+     */
+    g_assert(!ret);
+
     op.deleted_in_tx = false;
     op.mutating = true;
 
Signature · 1 line
Anthony PERARD
#5 lore raw

[PULL 5/5] xen-platform: do full PCI reset during unplug of IDE devices

In reply to Anthony PERARD via

From: Olaf Hering <[email protected]>

The IDE unplug function needs to reset the entire PCI device, to make sure all state is initialized to defaults. This is done by calling pci_device_reset, which resets not only the chip specific registers, but also all PCI state. This fixes "unplug" in a Xen HVM domU with the modular legacy xenlinux PV drivers.

Commit ee358e919e38 ("hw/ide/piix: Convert reset handler to DeviceReset") changed the way how the the disks are unplugged. Prior this commit the PCI device remained unchanged. After this change, piix_ide_reset is exercised after the "unplug" command, which was not the case prior that commit. This function resets the command register. As a result the ata_piix driver inside the domU will see a disabled PCI device. The generic PCI code will reenable the PCI device. On the qemu side, this runs pci_default_write_config/pci_update_mappings. Here a changed address is returned by pci_bar_address, this is the address which was truncated in piix_ide_reset. In case of a Xen HVM domU, the address changes from 0xc120 to 0xc100. This truncation was a bug in piix_ide_reset, which was fixed in commit 230dfd9257 ("hw/ide/piix:
properly initialize the BMIBA register"). If pci_xen_ide_unplug had used pci_device_reset, the PCI registers would have been properly reset, and commit ee358e919e38 would have not introduced a regression for this specific domU environment.

While the unplug is supposed to hide the IDE disks, the changed BMIBA address broke the UHCI device. In case the domU has an USB tablet configured, to recive absolute pointer coordinates for the GUI, it will cause a hang during device discovery of the partly discovered USB hid device. Reading the USBSTS word size register will fail. The access ends up in the QEMU piix-bmdma device, instead of the expected uhci device. Here a byte size request is expected, and a value of ~0 is returned. As a result the UCHI driver sees an error state in the register, and turns off the UHCI controller.

Message-Id: <[email protected]>

---
 hw/i386/xen/xen_platform.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
hw/i386/xen/xen_platform.c +4 -3
index 57f1d742c1..17457ff3de 100644
@@ -164,8 +164,9 @@ static void pci_unplug_nics(PCIBus *bus)
  *
  * [1] https://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=docs/misc/hvm-emulated-unplug.pandoc
  */
-static void pci_xen_ide_unplug(DeviceState *dev, bool aux)
+static void pci_xen_ide_unplug(PCIDevice *d, bool aux)
 {
+    DeviceState *dev = DEVICE(d);
     PCIIDEState *pci_ide;
     int i;
     IDEDevice *idedev;
@@ -195,7 +196,7 @@ static void pci_xen_ide_unplug(DeviceState *dev, bool aux)
             blk_unref(blk);
         }
     }
-    device_cold_reset(dev);
+    pci_device_reset(d);
 }
 
 static void unplug_disks(PCIBus *b, PCIDevice *d, void *opaque)
@@ -210,7 +211,7 @@ static void unplug_disks(PCIBus *b, PCIDevice *d, void *opaque)
 
     switch (pci_get_word(d->config + PCI_CLASS_DEVICE)) {
     case PCI_CLASS_STORAGE_IDE:
-        pci_xen_ide_unplug(DEVICE(d), aux);
+        pci_xen_ide_unplug(d, aux);
         break;
 
     case PCI_CLASS_STORAGE_SCSI:
Signature · 1 line
Anthony PERARD
#6 lore raw
In reply to Anthony PERARD via
Quoted 10 lines · Anthony PERARD via

The following changes since commit 802341823f1720511dd5cf53ae40285f7978c61b:

   Merge tag 'pull-tcg-20230731' ofhttps://gitlab.com/rth7680/qemu  into staging (2023-07-31 14:02:51 -0700)

are available in the Git repository at:

   https://xenbits.xen.org/git-http/people/aperard/qemu-dm.git  tags/pull-xen-20230801

for you to fetch changes up to 856ca10f9ce1fcffeab18546b36a64f79017c905:

Quoted 8 lines

xen-platform: do full PCI reset during unplug of IDE devices (2023-08-01 10:22:33 +0100)

----------------------------------------------------------------
Misc fixes, for thread-pool, xen, and xen-emulate

* fix an access to request_cond QemuCond in thread-pool
* fix issue with PCI devices when unplugging IDE devices in Xen guest
* several fixes for issues pointed out by Coverity

Applied, thanks. Please update https://wiki.qemu.org/ChangeLog/8.1 as appropriate.

r~

#7 lore raw
In reply to Anthony PERARD via

01.08.2023 12:40, Anthony PERARD via wrote:
...

Quoted 4 lines

Anthony PERARD (2):
xen-block: Avoid leaks on new error path
thread-pool: signal "request_cond" while locked

Quoted 8 lines

David Woodhouse (1):
hw/xen: Clarify (lack of) error handling in transaction_commit()

Olaf Hering (1):
xen-platform: do full PCI reset during unplug of IDE devices

Peter Maydell (1):
xen: Don't pass MemoryListener around by value

From the descriptions, it looks like

xen-block: Avoid leaks on new error path
thread-pool: signal "request_cond" while locked
xen-platform: do full PCI reset during unplug of IDE devices

should be applied to -stable.

More, it looks like all 3 should be applied to v7.2.x series too, including the PCI reset one, which is a bit problematic for 7.2 because it lacks dfa6ba6baeebe "Replace use of qdev_reset_all() with device_cold_reset()", but that change was mechanical.

Olaf, can you please take a look at
https://gitlab.com/mjt0k/qemu/-/commit/645d74c366c8c6334bf9a3403f780adefe4e410f
(from https://gitlab.com/mjt0k/qemu/-/commits/staging-7.2/ ) and check if it makes sense?

Or if whole thing makes no sense to backport to stable, please let me know as well.

Thanks!

/mjt

#8 lore raw

Wed, 2 Aug 2023 18:18:01 +0300 Michael Tokarev <[email protected]>:

Quoted 2 lines

Or if whole thing makes no sense to backport to stable, please let me know as well.

The xen-platform change does not need to be backported IMHO.

Olaf