Skip to contentSkip to conversation

virtio-scsi: restart DMA after iothread

PATCH Applied Stefan Hajnoczi 3 participants 6 messages Jun 20, 2019 – Jun 27, 2019

lore
Share & tools
lore
#0 lore raw

v5 cover virtio-scsi: restart DMA after iothread

v5:
* Plumbing vm change state handlers into DeviceClass/BusClass is a rather large bug fix. Instead I've combined the previous priorities approach with the observation from Kevin and Paolo that we really want to order by qdev tree depth.

The new qdev_add_vm_change_state_handler() API lets DeviceStates register callbacks that execute in qdev tree depth order. This solves the virtio-scsi bug since the virtio-scsi device's callback must complete before its child scsi-disk's callback runs.

Is this a good compromise for everyone?

Stefan Hajnoczi (3):
vl: add qemu_add_vm_change_state_handler_prio()
qdev: add qdev_add_vm_change_state_handler()
virtio-scsi: restart DMA after iothread

 hw/core/Makefile.objs             |  1 +
 include/hw/qdev-core.h            |  5 +++
 include/sysemu/sysemu.h           |  2 +
 hw/core/vm-change-state-handler.c | 61 +++++++++++++++++++++++++++++++
 hw/scsi/scsi-bus.c                |  4 +-
 hw/virtio/virtio.c                |  4 +-
 vl.c                              | 59 ++++++++++++++++++++++++------
 7 files changed, 120 insertions(+), 16 deletions(-)
 create mode 100644 hw/core/vm-change-state-handler.c
Signature · 1 line
2.21.0
#1 lore raw

PATCH v5 1/3 vl: add qemu_add_vm_change_state_handler_prio()

Add an API for registering vm change state handlers with a well-defined ordering. This is necessary when handlers depend on each other.

Small coding style fixes are included to make checkpatch.pl happy.

---
 include/sysemu/sysemu.h |  2 ++
 vl.c                    | 59 ++++++++++++++++++++++++++++++++---------
 2 files changed, 49 insertions(+), 12 deletions(-)
include/sysemu/sysemu.h +2 -0
index 61579ae71e..984c439ac9 100644
@@ -29,6 +29,8 @@ typedef void VMChangeStateHandler(void *opaque, int running, RunState state);
 
 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
                                                      void *opaque);
+VMChangeStateEntry *qemu_add_vm_change_state_handler_prio(
+        VMChangeStateHandler *cb, void *opaque, int priority);
 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e);
 void vm_state_notify(int running, RunState state);
 
vl.c +47 -12

90 lines not on this page · raw message

Signature · 1 line
2.21.0
#2 lore raw

PATCH v5 2/3 qdev: add qdev_add_vm_change_state_handler()

In reply to Stefan Hajnoczi

Children sometimes depend on their parent's vm change state handler having completed. Add a vm change state handler API for devices that guarantees tree depth ordering.

---
 hw/core/Makefile.objs             |  1 +
 include/hw/qdev-core.h            |  5 +++
 hw/core/vm-change-state-handler.c | 61 +++++++++++++++++++++++++++++++
 3 files changed, 67 insertions(+)
 create mode 100644 hw/core/vm-change-state-handler.c
hw/core/Makefile.objs +1 -0
index a799c83815..dc0895ee9a 100644
@@ -7,6 +7,7 @@ common-obj-$(CONFIG_SOFTMMU) += fw-path-provider.o
 common-obj-y += irq.o
 common-obj-y += hotplug.o
 common-obj-$(CONFIG_SOFTMMU) += nmi.o
+common-obj-$(CONFIG_SOFTMMU) += vm-change-state-handler.o
 
 common-obj-$(CONFIG_EMPTY_SLOT) += empty_slot.o
 common-obj-$(CONFIG_XILINX_AXI) += stream.o
include/hw/qdev-core.h +5 -0
index fa55dc10ae..e157fc4acd 100644
@@ -6,6 +6,7 @@
 #include "qom/object.h"
 #include "hw/irq.h"
 #include "hw/hotplug.h"
+#include "sysemu/sysemu.h"
 
 enum {
     DEV_NVECTORS_UNSPECIFIED = -1,
@@ -450,4 +451,8 @@ static inline bool qbus_is_hotpluggable(BusState *bus)
 void device_listener_register(DeviceListener *listener);
 void device_listener_unregister(DeviceListener *listener);
 
+VMChangeStateEntry *qdev_add_vm_change_state_handler(DeviceState *dev,
+                                                     VMChangeStateHandler *cb,
+                                                     void *opaque);
+
 #endif
hw/core/vm-change-state-handler.c new file +61 -0

61 lines not on this page · raw message

Signature · 1 line
2.21.0
#3 lore raw

PATCH v5 3/3 virtio-scsi: restart DMA after iothread

In reply to Stefan Hajnoczi

When the 'cont' command resumes guest execution the vm change state handlers are invoked. Unfortunately there is no explicit ordering between classic qemu_add_vm_change_state_handler() callbacks. When two layers of code both use vm change state handlers, we don't control which handler runs first.

virtio-scsi with iothreads hits a deadlock when a failed SCSI command is restarted and completes before the iothread is re-initialized.

This patch uses the new qdev_add_vm_change_state_handler() API to guarantee that virtio-scsi's virtio change state handler executes before the SCSI bus children. This way DMA is restarted after the iothread has re-initialized.

---
 hw/scsi/scsi-bus.c | 4 ++--
 hw/virtio/virtio.c | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
hw/scsi/scsi-bus.c +2 -2
index 196136a307..fdc3a0e4e0 100644
@@ -207,8 +207,8 @@ static void scsi_qdev_realize(DeviceState *qdev, Error **errp)
         error_propagate(errp, local_err);
         return;
     }
-    dev->vmsentry = qemu_add_vm_change_state_handler(scsi_dma_restart_cb,
-                                                     dev);
+    dev->vmsentry = qdev_add_vm_change_state_handler(DEVICE(dev),
+            scsi_dma_restart_cb, dev);
 }
 
 static void scsi_qdev_unrealize(DeviceState *qdev, Error **errp)
hw/virtio/virtio.c +2 -2
index e1e90fcfd6..e42e6710d2 100644
@@ -2354,8 +2354,8 @@ void virtio_init(VirtIODevice *vdev, const char *name,
     } else {
         vdev->config = NULL;
     }
-    vdev->vmstate = qemu_add_vm_change_state_handler(virtio_vmstate_change,
-                                                     vdev);
+    vdev->vmstate = qdev_add_vm_change_state_handler(DEVICE(vdev),
+            virtio_vmstate_change, vdev);
     vdev->device_endian = virtio_default_endian();
     vdev->use_guest_notifier_mask = true;
 }
Signature · 1 line
2.21.0
#4 lore raw
In reply to Stefan Hajnoczi
Quoted 6 lines · Stefan Hajnoczi

v5:
* Plumbing vm change state handlers into DeviceClass/BusClass is a rather large bug fix. Instead I've combined the previous priorities approach with the observation from Kevin and Paolo that we really want to order by qdev tree depth.

Quoted 6 lines

The new qdev_add_vm_change_state_handler() API lets DeviceStates register callbacks that execute in qdev tree depth order. This solves the virtio-scsi bug since the virtio-scsi device's callback must complete before its child scsi-disk's callback runs.

Is this a good compromise for everyone?

Yes! Perhaps a bit of a hack, but it works and both the API and the implementation are very sane. Converting other devices to use qdev_add_vm_change_state_handler() is left as an exercise for the reviewer, I guess? :)

Paolo

Applied
#5 lore raw
In reply to Stefan Hajnoczi

Am 20.06.2019 um 19:37 hat Stefan Hajnoczi geschrieben:

Quoted 6 lines

v5:
* Plumbing vm change state handlers into DeviceClass/BusClass is a rather large bug fix. Instead I've combined the previous priorities approach with the observation from Kevin and Paolo that we really want to order by qdev tree depth.

Quoted 6 lines

The new qdev_add_vm_change_state_handler() API lets DeviceStates register callbacks that execute in qdev tree depth order. This solves the virtio-scsi bug since the virtio-scsi device's callback must complete before its child scsi-disk's callback runs.

Is this a good compromise for everyone?

I'd still call it a hack, but I can also see that doing the real thing would be a lot of work that might not be worth the effort.

Thanks, applied to the block branch.

Kevin