diff mbox series

[RFC,v2,1/3] virtio: add vdc->vmchange_state() callback

Message ID 20190523134409.18673-2-stefanha@redhat.com
State New
Headers show
Series scsi: restart dma after vm change state handlers | expand

Commit Message

Stefan Hajnoczi May 23, 2019, 1:44 p.m. UTC
The core virtio code invokes ->set_status() followed by
->ioeventfd_start() when the guest resumes execution.  Both of these
functions are also called in other cases unrelated to vm change state.

This patch introduces ->vmstate_change() so that devices can act on
guest pause/resume.  The existing qemu_add_vm_change_state_handler() API
isn't usable by virtio devices since the ordering between vm change
state handlers is undefined.  The new ->vmstate_change() callback is
always invoked after ->set_status() and ->ioeventfd_start() when
resuming a guest.

A later patch makes use of this new callback.

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 include/hw/virtio/virtio.h | 7 +++++++
 hw/virtio/virtio.c         | 9 +++++++++
 2 files changed, 16 insertions(+)

Comments

Stefan Hajnoczi May 24, 2019, 6:26 p.m. UTC | #1
On Thu, May 23, 2019 at 2:55 PM Stefan Hajnoczi <stefanha@redhat.com> wrote:
> @@ -2253,10 +2254,18 @@ static void virtio_vmstate_change(void *opaque, int running, RunState state)
>          virtio_set_status(vdev, vdev->status);
>      }
>
> +    if (!backend_run && k->vmstate_change) {
> +        vdc->vmstate_change(vdev, backend_run);
> +    }
> +
>      if (k->vmstate_change) {
>          k->vmstate_change(qbus->parent, backend_run);
>      }
>
> +    if (backend_run && k->vmstate_change) {
> +        vdc->vmstate_change(vdev, backend_run);
> +    }

Oops, k->vmstate_change should be vdc->vmstate_change.  I'll send a
new revision.

Stefan
diff mbox series

Patch

diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h
index 7140381e3a..46e54362dc 100644
--- a/include/hw/virtio/virtio.h
+++ b/include/hw/virtio/virtio.h
@@ -156,6 +156,13 @@  typedef struct VirtioDeviceClass {
     void (*save)(VirtIODevice *vdev, QEMUFile *f);
     int (*load)(VirtIODevice *vdev, QEMUFile *f, int version_id);
     const VMStateDescription *vmsd;
+
+    /* Called when the device should start/stop running because the guest was
+     * resumed/paused.  Note that this takes VIRTIO_CONFIG_S_DRIVER_OK into
+     * account so running is true iff the guest is resumed and the guest driver
+     * has already indicated it is ready.
+     */
+    void (*vmstate_change)(VirtIODevice *vdev, bool running);
 } VirtioDeviceClass;
 
 void virtio_instance_init_common(Object *proxy_obj, void *data,
diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
index 28056a7ef7..f9f31b5325 100644
--- a/hw/virtio/virtio.c
+++ b/hw/virtio/virtio.c
@@ -2246,6 +2246,7 @@  static void virtio_vmstate_change(void *opaque, int running, RunState state)
     VirtIODevice *vdev = opaque;
     BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
+    VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
     bool backend_run = running && (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK);
     vdev->vm_running = running;
 
@@ -2253,10 +2254,18 @@  static void virtio_vmstate_change(void *opaque, int running, RunState state)
         virtio_set_status(vdev, vdev->status);
     }
 
+    if (!backend_run && k->vmstate_change) {
+        vdc->vmstate_change(vdev, backend_run);
+    }
+
     if (k->vmstate_change) {
         k->vmstate_change(qbus->parent, backend_run);
     }
 
+    if (backend_run && k->vmstate_change) {
+        vdc->vmstate_change(vdev, backend_run);
+    }
+
     if (!backend_run) {
         virtio_set_status(vdev, vdev->status);
     }