Skip to contentSkip to conversation

virtio: host notifier API cleanup

PATCH In review Michael S. Tsirkin 2 participants 7 messages Apr 7, 2015

lore
Share & tools
lore
#0 lore raw

cover virtio: host notifier API cleanup

This refactors the code for enabling/disabling host notifiers. Not useful by itself, but seems to be needed for other projects. So I'm not merging this yet, will be included with other patches.
Posting now for early review.

Michael S. Tsirkin (3):
virtio: interface to enable/disable host notifiers dataplane/virtio-blk.c: new API to enable notifiers dataplane/virtio-scsi: new API to enable host notifiers

 include/hw/virtio/virtio.h      |  4 ++-
 hw/block/dataplane/virtio-blk.c |  6 ++---
 hw/scsi/virtio-scsi-dataplane.c | 14 +++-------
 hw/virtio/vhost.c               | 45 ++-----------------------------
 hw/virtio/virtio.c              | 59 ++++++++++++++++++++++++++++++++++++++++-
 5 files changed, 70 insertions(+), 58 deletions(-)
Signature · 1 line
MST
#1 lore raw

PATCH 1/3 virtio: interface to enable/disable host notifiers

generalize and move these from vhost to virtio.

---
 include/hw/virtio/virtio.h |  3 +++
 hw/virtio/vhost.c          | 45 ++--------------------------------------
 hw/virtio/virtio.c         | 51 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 56 insertions(+), 43 deletions(-)
include/hw/virtio/virtio.h +3 -0
index d95f8b6..e1cff22 100644
@@ -219,6 +219,9 @@ void virtio_queue_set_host_notifier_fd_handler(VirtQueue *vq, bool assign,
 void virtio_queue_notify_vq(VirtQueue *vq);
 void virtio_irq(VirtQueue *vq);
 
+int virtio_enable_host_notifiers(VirtIODevice *vdev, int startvq, int nvqs);
+void virtio_disable_host_notifiers(VirtIODevice *vdev, int startvq, int nvqs);
+
 static inline void virtio_add_feature(uint32_t *features, unsigned int fbit)
 {
     assert(fbit < 32);
hw/virtio/vhost.c +2 -43

57 lines not on this page · raw message

hw/virtio/virtio.c +51 -0

57 lines not on this page · raw message

Signature · 1 line
MST
#3 lore raw

PATCH 2/3 dataplane/virtio-blk.c: new API to enable notifiers

In reply to Michael S. Tsirkin

Switch to new API, no functional changes.

---
 hw/block/dataplane/virtio-blk.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/block/dataplane/virtio-blk.c +3 -3
index cd41478..c9ac70e 100644
@@ -263,7 +263,7 @@ void virtio_blk_data_plane_start(VirtIOBlockDataPlane *s)
     s->guest_notifier = virtio_queue_get_guest_notifier(vq);
 
     /* Set up virtqueue notify */
-    r = k->set_host_notifier(qbus->parent, 0, true);
+    r = virtio_enable_host_notifiers(s->vdev, 0, 1);
     if (r != 0) {
         fprintf(stderr, "virtio-blk failed to set host notifier (%d)\n", r);
         goto fail_host_notifier;
@@ -289,7 +289,7 @@ void virtio_blk_data_plane_start(VirtIOBlockDataPlane *s)
     return;
 
   fail_host_notifier:
-    k->set_guest_notifiers(qbus->parent, 1, false);
+    virtio_disable_host_notifiers(s->vdev, 0, 1);
   fail_guest_notifiers:
     vring_teardown(&s->vring, s->vdev, 0);
     s->disabled = true;
@@ -332,7 +332,7 @@ void virtio_blk_data_plane_stop(VirtIOBlockDataPlane *s)
      */
     vring_teardown(&s->vring, s->vdev, 0);
 
-    k->set_host_notifier(qbus->parent, 0, false);
+    virtio_disable_host_notifiers(s->vdev, 0, 1);
 
     /* Clean up guest notifier (irq) */
     k->set_guest_notifiers(qbus->parent, 1, false);
Signature · 1 line
MST
#5 lore raw

PATCH 3/3 dataplane/virtio-scsi: new API to enable host notifiers

In reply to Michael S. Tsirkin

Switch to new API.

---
 include/hw/virtio/virtio.h      |  1 -
 hw/scsi/virtio-scsi-dataplane.c | 14 ++++----------
 hw/virtio/virtio.c              |  8 +++++++-
 3 files changed, 11 insertions(+), 12 deletions(-)
include/hw/virtio/virtio.h +0 -1
index e1cff22..7e70193 100644
@@ -217,7 +217,6 @@ EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq);
 void virtio_queue_set_host_notifier_fd_handler(VirtQueue *vq, bool assign,
                                                bool set_handler);
 void virtio_queue_notify_vq(VirtQueue *vq);
-void virtio_irq(VirtQueue *vq);
 
 int virtio_enable_host_notifiers(VirtIODevice *vdev, int startvq, int nvqs);
 void virtio_disable_host_notifiers(VirtIODevice *vdev, int startvq, int nvqs);
hw/scsi/virtio-scsi-dataplane.c +4 -10
index 5575648..965ccb8 100644
@@ -43,13 +43,11 @@ static VirtIOSCSIVring *virtio_scsi_vring_init(VirtIOSCSI *s,
                                                EventNotifierHandler *handler,
                                                int n)
 {
-    BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
-    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
     VirtIOSCSIVring *r;
     int rc;
 
     /* Set up virtqueue notify */
-    rc = k->set_host_notifier(qbus->parent, n, true);
+    rc = virtio_enable_host_notifiers(VIRTIO_DEVICE(s), n, 1);
     if (rc != 0) {
         fprintf(stderr, "virtio-scsi: Failed to set host notifier (%d)\n",
                 rc);
@@ -72,7 +70,7 @@ static VirtIOSCSIVring *virtio_scsi_vring_init(VirtIOSCSI *s,
 
 fail_vring:
     aio_set_event_notifier(s->ctx, &r->host_notifier, NULL);
-    k->set_host_notifier(qbus->parent, n, false);
+    virtio_disable_host_notifiers(VIRTIO_DEVICE(s), n, 1);
     g_slice_free(VirtIOSCSIVring, r);
     return NULL;
 }
@@ -261,9 +259,7 @@ fail_vrings:
     virtio_scsi_clear_aio(s);
     aio_context_release(s->ctx);
     virtio_scsi_vring_teardown(s);
-    for (i = 0; i < vs->conf.num_queues + 2; i++) {
-        k->set_host_notifier(qbus->parent, i, false);
-    }
+    virtio_disable_host_notifiers(VIRTIO_DEVICE(s), 0, vs->conf.num_queues + 2);
     k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
 fail_guest_notifiers:
     s->dataplane_starting = false;
@@ -305,9 +301,7 @@ void virtio_scsi_dataplane_stop(VirtIOSCSI *s)
      */
     virtio_scsi_vring_teardown(s);
 
-    for (i = 0; i < vs->conf.num_queues + 2; i++) {
-        k->set_host_notifier(qbus->parent, i, false);
-    }
+    virtio_disable_host_notifiers(VIRTIO_DEVICE(s), 0, vs->conf.num_queues + 2);
 
     /* Clean up guest notifier (irq) */
     k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
hw/virtio/virtio.c +7 -1
index e7ee069..60d10cd 100644
@@ -84,6 +84,9 @@ struct VirtQueue
 
     int inuse;
 
+    /* Host notifier enabled? */
+    bool host_notifier_enabled;
+
     uint16_t vector;
     void (*handle_output)(VirtIODevice *vdev, VirtQueue *vq);
     VirtIODevice *vdev;
@@ -813,7 +816,7 @@ void virtio_del_queue(VirtIODevice *vdev, int n)
     vdev->vq[n].vring.num = 0;
 }
 
-void virtio_irq(VirtQueue *vq)
+static void virtio_irq(VirtQueue *vq)
 {
     trace_virtio_irq(vq);
     vq->vdev->isr |= 0x01;
@@ -1301,11 +1304,13 @@ int virtio_enable_host_notifiers(VirtIODevice *vdev, int startvq, int nvqs)
             fprintf(stderr, "vhost VQ %d notifier binding failed: %d\n", i, -r);
             goto fail_vq;
         }
+        vdev->vq[startvq + i].host_notifier_enabled = true;
     }
 
     return 0;
 fail_vq:
     while (--i >= 0) {
+        vdev->vq[startvq + i].host_notifier_enabled = false;
         r = k->set_host_notifier(qbus->parent, startvq + i, false);
         if (r < 0) {
             fprintf(stderr, "vhost VQ %d notifier cleanup error: %d\n", i, -r);
@@ -1325,6 +1330,7 @@ void virtio_disable_host_notifiers(VirtIODevice *vdev, int startvq, int nvqs)
     int i, r;
 
     for (i = 0; i < nvqs; ++i) {
+        vdev->vq[startvq + i].host_notifier_enabled = false;
         r = k->set_host_notifier(qbus->parent, startvq + i, false);
         if (r < 0) {
             fprintf(stderr, "vhost VQ %d notifier cleanup failed: %d\n", i, -r);
Signature · 1 line
MST
#2 lore raw

Re: [Qemu-devel] [PATCH 1/3] virtio: interface to enable/disable host notifiers

In reply to Michael S. Tsirkin
Quoted 15 lines · On Tue, 7 Apr 2015 12:45:56 +0200 "Michael S. Tsirkin" <[email protected]> wrote:

generalize and move these from vhost to virtio.

---
 include/hw/virtio/virtio.h |  3 +++
 hw/virtio/vhost.c          | 45 ++--------------------------------------
 hw/virtio/virtio.c         | 51 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 56 insertions(+), 43 deletions(-)
hw/virtio/virtio.c +0 -0
index 17c1260..e7ee069 100644

hw/virtio/virtio.c void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name) · line 1286

Quoted 4 lines
hw/virtio/virtio.c +1 -0
@@ -1283,6 +1283,57 @@ void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name)
     vdev->bus_name = g_strdup(bus_name);
 }
 
+int virtio_enable_host_notifiers(VirtIODevice *vdev, int startvq, int nvqs)

I realize that you just move the function, but would it make sense to verify that startvq + nvqs is actually still a valid queue?

Quoted 5 lines
hw/virtio/virtio.c +5 -0
+{
+    BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
+    VirtioBusState *vbus = VIRTIO_BUS(qbus);
+    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
+    int i, r;

hw/virtio/virtio.c void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name) · line 1301

Quoted 10 lines
hw/virtio/virtio.c +10 -0
+    if (!k->set_host_notifier) {
+        fprintf(stderr, "binding does not support host notifiers\n");
+        r = -ENOSYS;
+        goto fail;
+    }
+
+    for (i = 0; i < nvqs; ++i) {
+        r = k->set_host_notifier(qbus->parent, startvq + i, true);
+        if (r < 0) {
+            fprintf(stderr, "vhost VQ %d notifier binding failed: %d\n", i, -r);

I think you'll want to get rid of the vhost in the message here. And the number of the failing virtqueue is startvq+i, no?

Also: would it make sense to convert this to the error reporting infrastructure?

Quoted 17 lines
hw/virtio/virtio.c +17 -0
+            goto fail_vq;
+        }
+    }
+
+    return 0;
+fail_vq:
+    while (--i >= 0) {
+        r = k->set_host_notifier(qbus->parent, startvq + i, false);
+        if (r < 0) {
+            fprintf(stderr, "vhost VQ %d notifier cleanup error: %d\n", i, -r);
+            fflush(stderr);
+        }
+        assert (r >= 0);
+    }
+fail:
+    return r;
+}

hw/virtio/virtio.c void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name) · line 1328

Quoted 10 lines
hw/virtio/virtio.c +10 -0
+
+void virtio_disable_host_notifiers(VirtIODevice *vdev, int startvq, int nvqs)
+{
+    BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
+    VirtioBusState *vbus = VIRTIO_BUS(qbus);
+    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
+    int i, r;
+
+    for (i = 0; i < nvqs; ++i) {
+        r = k->set_host_notifier(qbus->parent, startvq + i, false);

Should a generic function maybe check for ->set_host_notifier and bail out early if it doesn't exist? Probably depends on how the callers want to handle non-existence of host notifiers.

Quoted 11 lines
hw/virtio/virtio.c +8 -0
+        if (r < 0) {
+            fprintf(stderr, "vhost VQ %d notifier cleanup failed: %d\n", i, -r);
+            fflush(stderr);
+        }
+        assert (r >= 0);
+    }
+}
+
 static void virtio_device_realize(DeviceState *dev, Error **errp)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
#4 lore raw

Re: [Qemu-devel] [PATCH 2/3] dataplane/virtio-blk.c: new API to enable notifiers

In reply to Michael S. Tsirkin
Quoted 21 lines · On Tue, 7 Apr 2015 12:45:58 +0200 "Michael S. Tsirkin" <[email protected]> wrote:

Switch to new API, no functional changes.

---
 hw/block/dataplane/virtio-blk.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/block/dataplane/virtio-blk.c +1 -1
index cd41478..c9ac70e 100644
@@ -263,7 +263,7 @@ void virtio_blk_data_plane_start(VirtIOBlockDataPlane *s)
     s->guest_notifier = virtio_queue_get_guest_notifier(vq);
 
     /* Set up virtqueue notify */
-    r = k->set_host_notifier(qbus->parent, 0, true);
+    r = virtio_enable_host_notifiers(s->vdev, 0, 1);
     if (r != 0) {
         fprintf(stderr, "virtio-blk failed to set host notifier (%d)\n", r);
         goto fail_host_notifier;

hw/block/dataplane/virtio-blk.c void virtio_blk_data_plane_start(VirtIOBlockDataPlane *s) · line 292

Quoted 5 lines
hw/block/dataplane/virtio-blk.c +1 -1
@@ -289,7 +289,7 @@ void virtio_blk_data_plane_start(VirtIOBlockDataPlane *s)
     return;
 
   fail_host_notifier:
-    k->set_guest_notifiers(qbus->parent, 1, false);
+    virtio_disable_host_notifiers(s->vdev, 0, 1);

This one is supposed to disable the guest notifier, not the host notifier.

Quoted 12 lines
hw/block/dataplane/virtio-blk.c +1 -1
   fail_guest_notifiers:
     vring_teardown(&s->vring, s->vdev, 0);
     s->disabled = true;
@@ -332,7 +332,7 @@ void virtio_blk_data_plane_stop(VirtIOBlockDataPlane *s)
      */
     vring_teardown(&s->vring, s->vdev, 0);
 
-    k->set_host_notifier(qbus->parent, 0, false);
+    virtio_disable_host_notifiers(s->vdev, 0, 1);
 
     /* Clean up guest notifier (irq) */
     k->set_guest_notifiers(qbus->parent, 1, false);
#6 lore raw

Re: [Qemu-devel] [PATCH 3/3] dataplane/virtio-scsi: new API to enable host notifiers

In reply to Michael S. Tsirkin
Quoted 14 lines · On Tue, 7 Apr 2015 12:46:01 +0200 "Michael S. Tsirkin" <[email protected]> wrote:

Switch to new API.

---
 include/hw/virtio/virtio.h      |  1 -
 hw/scsi/virtio-scsi-dataplane.c | 14 ++++----------
 hw/virtio/virtio.c              |  8 +++++++-
 3 files changed, 11 insertions(+), 12 deletions(-)
include/hw/virtio/virtio.h +0 -0
index e1cff22..7e70193 100644

include/hw/virtio/virtio.h EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq); · line 220

Quoted 4 lines
include/hw/virtio/virtio.h +0 -1
@@ -217,7 +217,6 @@ EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq);
 void virtio_queue_set_host_notifier_fd_handler(VirtQueue *vq, bool assign,
                                                bool set_handler);
 void virtio_queue_notify_vq(VirtQueue *vq);
-void virtio_irq(VirtQueue *vq);

Unrelated cleanup?

Quoted 3 lines
include/hw/virtio/virtio.h +0 -0
 int virtio_enable_host_notifiers(VirtIODevice *vdev, int startvq, int nvqs);
 void virtio_disable_host_notifiers(VirtIODevice *vdev, int startvq, int nvqs);

(...)

hw/virtio/virtio.c struct VirtQueue · line 88

Quoted 10 lines
hw/virtio/virtio.c +2 -0
index e7ee069..60d10cd 100644
@@ -84,6 +84,9 @@ struct VirtQueue
 
     int inuse;
 
+    /* Host notifier enabled? */
+    bool host_notifier_enabled;

This looks to be write-only. Have you maybe mixed in some unrelated changes?

Quoted 35 lines
hw/virtio/virtio.c +5 -1
+
     uint16_t vector;
     void (*handle_output)(VirtIODevice *vdev, VirtQueue *vq);
     VirtIODevice *vdev;
@@ -813,7 +816,7 @@ void virtio_del_queue(VirtIODevice *vdev, int n)
     vdev->vq[n].vring.num = 0;
 }
 
-void virtio_irq(VirtQueue *vq)
+static void virtio_irq(VirtQueue *vq)
 {
     trace_virtio_irq(vq);
     vq->vdev->isr |= 0x01;
@@ -1301,11 +1304,13 @@ int virtio_enable_host_notifiers(VirtIODevice *vdev, int startvq, int nvqs)
             fprintf(stderr, "vhost VQ %d notifier binding failed: %d\n", i, -r);
             goto fail_vq;
         }
+        vdev->vq[startvq + i].host_notifier_enabled = true;
     }
 
     return 0;
 fail_vq:
     while (--i >= 0) {
+        vdev->vq[startvq + i].host_notifier_enabled = false;
         r = k->set_host_notifier(qbus->parent, startvq + i, false);
         if (r < 0) {
             fprintf(stderr, "vhost VQ %d notifier cleanup error: %d\n", i, -r);
@@ -1325,6 +1330,7 @@ void virtio_disable_host_notifiers(VirtIODevice *vdev, int startvq, int nvqs)
     int i, r;
 
     for (i = 0; i < nvqs; ++i) {
+        vdev->vq[startvq + i].host_notifier_enabled = false;
         r = k->set_host_notifier(qbus->parent, startvq + i, false);
         if (r < 0) {
             fprintf(stderr, "vhost VQ %d notifier cleanup failed: %d\n", i, -r);