qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v5 0/3] virtio-scsi: restart DMA after iothread
@ 2019-06-20 17:35 Stefan Hajnoczi
  2019-06-20 17:35 ` [Qemu-devel] [PATCH v5 1/3] vl: add qemu_add_vm_change_state_handler_prio() Stefan Hajnoczi
  0 siblings, 1 reply; 5+ messages in thread
From: Stefan Hajnoczi @ 2019-06-20 17:35 UTC (permalink / raw)
  To: qemu-devel
  Cc: Fam Zheng, Paolo Bonzini, Stefan Hajnoczi, Kevin Wolf,
	Michael S. Tsirkin

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

-- 
2.21.0



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Qemu-devel] [PATCH v5 1/3] vl: add qemu_add_vm_change_state_handler_prio()
  2019-06-20 17:35 [Qemu-devel] [PATCH v5 0/3] virtio-scsi: restart DMA after iothread Stefan Hajnoczi
@ 2019-06-20 17:35 ` Stefan Hajnoczi
  0 siblings, 0 replies; 5+ messages in thread
From: Stefan Hajnoczi @ 2019-06-20 17:35 UTC (permalink / raw)
  To: qemu-devel
  Cc: Fam Zheng, Paolo Bonzini, Stefan Hajnoczi, Kevin Wolf,
	Michael S. Tsirkin

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.

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 include/sysemu/sysemu.h |  2 ++
 vl.c                    | 59 ++++++++++++++++++++++++++++++++---------
 2 files changed, 49 insertions(+), 12 deletions(-)

diff --git a/include/sysemu/sysemu.h b/include/sysemu/sysemu.h
index 61579ae71e..984c439ac9 100644
--- a/include/sysemu/sysemu.h
+++ b/include/sysemu/sysemu.h
@@ -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);
 
diff --git a/vl.c b/vl.c
index 99a56b5556..7fac2ae7ca 100644
--- a/vl.c
+++ b/vl.c
@@ -1471,28 +1471,57 @@ static int machine_help_func(QemuOpts *opts, MachineState *machine)
 struct vm_change_state_entry {
     VMChangeStateHandler *cb;
     void *opaque;
-    QLIST_ENTRY (vm_change_state_entry) entries;
+    QTAILQ_ENTRY(vm_change_state_entry) entries;
+    int priority;
 };
 
-static QLIST_HEAD(, vm_change_state_entry) vm_change_state_head;
+static QTAILQ_HEAD(, vm_change_state_entry) vm_change_state_head;
 
-VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
-                                                     void *opaque)
+/**
+ * qemu_add_vm_change_state_handler_prio:
+ * @cb: the callback to invoke
+ * @opaque: user data passed to the callback
+ * @priority: low priorities execute first when the vm runs and the reverse is
+ *            true when the vm stops
+ *
+ * Register a callback function that is invoked when the vm starts or stops
+ * running.
+ *
+ * Returns: an entry to be freed using qemu_del_vm_change_state_handler()
+ */
+VMChangeStateEntry *qemu_add_vm_change_state_handler_prio(
+        VMChangeStateHandler *cb, void *opaque, int priority)
 {
     VMChangeStateEntry *e;
+    VMChangeStateEntry *other;
 
-    e = g_malloc0(sizeof (*e));
-
+    e = g_malloc0(sizeof(*e));
     e->cb = cb;
     e->opaque = opaque;
-    QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
+    e->priority = priority;
+
+    /* Keep list sorted in ascending priority order */
+    QTAILQ_FOREACH(other, &vm_change_state_head, entries) {
+        if (priority < other->priority) {
+            QTAILQ_INSERT_BEFORE(other, e, entries);
+            return e;
+        }
+    }
+
+    QTAILQ_INSERT_TAIL(&vm_change_state_head, e, entries);
     return e;
 }
 
+VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
+                                                     void *opaque)
+{
+    return qemu_add_vm_change_state_handler_prio(cb, opaque, 0);
+}
+
 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
 {
-    QLIST_REMOVE (e, entries);
-    g_free (e);
+    QTAILQ_REMOVE(&vm_change_state_head, e, entries);
+    g_free(e);
 }
 
 void vm_state_notify(int running, RunState state)
@@ -1501,8 +1530,14 @@ void vm_state_notify(int running, RunState state)
 
     trace_vm_state_notify(running, state, RunState_str(state));
 
-    QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
-        e->cb(e->opaque, running, state);
+    if (running) {
+        QTAILQ_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
+            e->cb(e->opaque, running, state);
+        }
+    } else {
+        QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head, entries, next) {
+            e->cb(e->opaque, running, state);
+        }
     }
 }
 
@@ -3025,7 +3060,7 @@ int main(int argc, char **argv, char **envp)
         exit(1);
     }
 
-    QLIST_INIT (&vm_change_state_head);
+    QTAILQ_INIT(&vm_change_state_head);
     os_setup_early_signal_handling();
 
     cpu_option = NULL;
-- 
2.21.0



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [Qemu-devel] [PATCH v5 0/3] virtio-scsi: restart DMA after iothread
@ 2019-06-20 17:37 Stefan Hajnoczi
  2019-06-20 18:07 ` Paolo Bonzini
  2019-06-27 11:03 ` Kevin Wolf
  0 siblings, 2 replies; 5+ messages in thread
From: Stefan Hajnoczi @ 2019-06-20 17:37 UTC (permalink / raw)
  To: qemu-devel
  Cc: Kevin Wolf, Paolo Bonzini, Fam Zheng, Stefan Hajnoczi,
	Michael S. Tsirkin

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

-- 
2.21.0



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH v5 0/3] virtio-scsi: restart DMA after iothread
  2019-06-20 17:37 [Qemu-devel] [PATCH v5 0/3] virtio-scsi: restart DMA after iothread Stefan Hajnoczi
@ 2019-06-20 18:07 ` Paolo Bonzini
  2019-06-27 11:03 ` Kevin Wolf
  1 sibling, 0 replies; 5+ messages in thread
From: Paolo Bonzini @ 2019-06-20 18:07 UTC (permalink / raw)
  To: Stefan Hajnoczi, qemu-devel; +Cc: Kevin Wolf, Fam Zheng, Michael S. Tsirkin

On 20/06/19 19:37, Stefan Hajnoczi wrote:
> 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?

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH v5 0/3] virtio-scsi: restart DMA after iothread
  2019-06-20 17:37 [Qemu-devel] [PATCH v5 0/3] virtio-scsi: restart DMA after iothread Stefan Hajnoczi
  2019-06-20 18:07 ` Paolo Bonzini
@ 2019-06-27 11:03 ` Kevin Wolf
  1 sibling, 0 replies; 5+ messages in thread
From: Kevin Wolf @ 2019-06-27 11:03 UTC (permalink / raw)
  To: Stefan Hajnoczi; +Cc: Fam Zheng, Paolo Bonzini, qemu-devel, Michael S. Tsirkin

Am 20.06.2019 um 19:37 hat Stefan Hajnoczi geschrieben:
> 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?

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-06-27 11:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-06-20 17:35 [Qemu-devel] [PATCH v5 0/3] virtio-scsi: restart DMA after iothread Stefan Hajnoczi
2019-06-20 17:35 ` [Qemu-devel] [PATCH v5 1/3] vl: add qemu_add_vm_change_state_handler_prio() Stefan Hajnoczi
  -- strict thread matches above, loose matches on Subject: below --
2019-06-20 17:37 [Qemu-devel] [PATCH v5 0/3] virtio-scsi: restart DMA after iothread Stefan Hajnoczi
2019-06-20 18:07 ` Paolo Bonzini
2019-06-27 11:03 ` Kevin Wolf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).