Skip to contentSkip to conversation

virtio-scsi: dataplane: one fix and one optimization

PATCH Applied Ming Lei 4 participants 11 messages Nov 11, 2014 – Nov 12, 2014

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

cover virtio-scsi: dataplane: one fix and one optimization

Hi,

The 1st patch fixes an allocation problem.

The 2nd one supresses writing eventfd a lot(~30K/sec in my test).

V1:
- use g_new() in 1/2
- avoid to dereference VIRTIO_SCSI() directly 2/2
- add build check on queue depth

Thanks,
Ming Lei

author
#1 lore raw

PATCH 1/2 virtio-scsi: dataplane: fix allocation for 'cmd_vrings'

The size of each element should be sizeof(VirtIOSCSIVring *).

---
 hw/scsi/virtio-scsi-dataplane.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/scsi/virtio-scsi-dataplane.c +1 -1
index 855439e..df17229 100644
@@ -239,7 +239,7 @@ void virtio_scsi_dataplane_start(VirtIOSCSI *s)
     if (!s->event_vring) {
         goto fail_vrings;
     }
-    s->cmd_vrings = g_malloc0(sizeof(VirtIOSCSIVring) * vs->conf.num_queues);
+    s->cmd_vrings = g_new(VirtIOSCSIVring *, vs->conf.num_queues);
     for (i = 0; i < vs->conf.num_queues; i++) {
         s->cmd_vrings[i] =
             virtio_scsi_vring_init(s, vs->cmd_vqs[i],
Signature · 1 line
1.7.9.5
Reviewed-by on v1 1/2
#2 lore raw

Re: [Qemu-devel] [PATCH v1 1/2] virtio-scsi: dataplane: fix allocation for 'cmd_vrings'

Quoted 23 lines · Ming Lei

The size of each element should be sizeof(VirtIOSCSIVring *).

---
 hw/scsi/virtio-scsi-dataplane.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/scsi/virtio-scsi-dataplane.c +1 -1
index 855439e..df17229 100644
@@ -239,7 +239,7 @@ void virtio_scsi_dataplane_start(VirtIOSCSI *s)
     if (!s->event_vring) {
         goto fail_vrings;
     }
-    s->cmd_vrings = g_malloc0(sizeof(VirtIOSCSIVring) * vs->conf.num_queues);
+    s->cmd_vrings = g_new(VirtIOSCSIVring *, vs->conf.num_queues);
     for (i = 0; i < vs->conf.num_queues; i++) {
         s->cmd_vrings[i] =
             virtio_scsi_vring_init(s, vs->cmd_vqs[i],
Signature · 1 line
1.7.9.5
#3 lore raw

Re: [Qemu-devel] [PATCH v1 1/2] virtio-scsi: dataplane: fix allocation for 'cmd_vrings'

In reply to Ming Lei
Quoted 13 lines · Ming Lei

The size of each element should be sizeof(VirtIOSCSIVring *).

---
 hw/scsi/virtio-scsi-dataplane.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/scsi/virtio-scsi-dataplane.c +0 -0
index 855439e..df17229 100644

hw/scsi/virtio-scsi-dataplane.c void virtio_scsi_dataplane_start(VirtIOSCSI *s) · line 245

Quoted 8 lines
hw/scsi/virtio-scsi-dataplane.c +1 -1
@@ -239,7 +239,7 @@ void virtio_scsi_dataplane_start(VirtIOSCSI *s)
     if (!s->event_vring) {
         goto fail_vrings;
     }
-    s->cmd_vrings = g_malloc0(sizeof(VirtIOSCSIVring) * vs->conf.num_queues);
+    s->cmd_vrings = g_new(VirtIOSCSIVring *, vs->conf.num_queues);
     for (i = 0; i < vs->conf.num_queues; i++) {
         s->cmd_vrings[i] =
             virtio_scsi_vring_init(s, vs->cmd_vqs[i],

Applied, thanks.

Paolo

author
#4 lore raw

PATCH 2/2 virtio-scsi: dataplane: notify guest as batch

In reply to Ming Lei

It isn't necessery to notify guest each time when one request is completed, and it should be enough to just notify one time for each running of virtio_scsi_iothread_handle_cmd().

This patch supresses about 30K/sec write on eventfd.

---
 hw/scsi/virtio-scsi-dataplane.c |    4 +++-
 hw/scsi/virtio-scsi.c           |   26 +++++++++++++++++++++++++-
 include/hw/virtio/virtio-scsi.h |    4 ++++
 3 files changed, 32 insertions(+), 2 deletions(-)
hw/scsi/virtio-scsi-dataplane.c +3 -1
index df17229..294515a 100644
@@ -62,6 +62,7 @@ static VirtIOSCSIVring *virtio_scsi_vring_init(VirtIOSCSI *s,
     aio_set_event_notifier(s->ctx, &r->host_notifier, handler);
 
     r->parent = s;
+    r->qid = n;
 
     if (!vring_setup(&r->vring, VIRTIO_DEVICE(s), n)) {
         fprintf(stderr, "virtio-scsi: VRing setup failed\n");
@@ -95,7 +96,8 @@ void virtio_scsi_vring_push_notify(VirtIOSCSIReq *req)
 {
     vring_push(&req->vring->vring, &req->elem,
                req->qsgl.size + req->resp_iov.size);
-    event_notifier_set(&req->vring->guest_notifier);
+    req->dev->pending_guest_notify |= 1 << (req->vring->qid - 2);
+    qemu_bh_schedule(req->dev->guest_notify_bh);
 }
 
 static void virtio_scsi_iothread_handle_ctrl(EventNotifier *notifier)
hw/scsi/virtio-scsi.c +25 -1
index 6e34a2c..98411ef 100644
@@ -86,6 +86,21 @@ static void virtio_scsi_complete_req(VirtIOSCSIReq *req)
     virtio_scsi_free_req(req);
 }
 
+static void notify_guest_bh(void *opaque)
+{
+    VirtIOSCSI *s = opaque;
+    unsigned int qid;
+    uint64_t pending = s->pending_guest_notify;
+
+    s->pending_guest_notify = 0;
+
+    while ((qid = ffsl(pending))) {
+        qid--;
+        event_notifier_set(&s->cmd_vrings[qid]->guest_notifier);
+        pending &= ~(1 << qid);
+    }
+}
+
 static void virtio_scsi_bad_req(void)
 {
     error_report("wrong size for virtio-scsi headers");
@@ -824,7 +839,12 @@ void virtio_scsi_common_realize(DeviceState *dev, Error **errp,
     }
 
     if (s->conf.iothread) {
-        virtio_scsi_set_iothread(VIRTIO_SCSI(s), s->conf.iothread);
+        VirtIOSCSI *vis = VIRTIO_SCSI(s);
+
+        QEMU_BUILD_BUG_ON(VIRTIO_PCI_QUEUE_MAX > 64);
+        virtio_scsi_set_iothread(vis, s->conf.iothread);
+        vis->pending_guest_notify = 0;
+        vis->guest_notify_bh = aio_bh_new(vis->ctx, notify_guest_bh, vis);
     }
 }
 
@@ -901,7 +921,11 @@ void virtio_scsi_common_unrealize(DeviceState *dev, Error **errp)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
+    VirtIOSCSI *s = VIRTIO_SCSI(vs);
 
+    if (vs->conf.iothread) {
+        qemu_bh_delete(s->guest_notify_bh);
+    }
     g_free(vs->cmd_vqs);
     virtio_cleanup(vdev);
 }
include/hw/virtio/virtio-scsi.h +4 -0
index 9e1a49c..5e6c57e 100644
@@ -163,6 +163,7 @@ typedef struct {
     Vring vring;
     EventNotifier host_notifier;
     EventNotifier guest_notifier;
+    uint32_t qid;
 } VirtIOSCSIVring;
 
 typedef struct VirtIOSCSICommon {
@@ -198,6 +199,9 @@ typedef struct VirtIOSCSI {
     bool dataplane_fenced;
     Error *blocker;
     Notifier migration_state_notifier;
+
+    QEMUBH *guest_notify_bh;
+    uint64_t pending_guest_notify;
 } VirtIOSCSI;
 
 typedef struct VirtIOSCSIReq {
Signature · 1 line
1.7.9.5
#5 lore raw

Re: [Qemu-devel] [PATCH v1 2/2] virtio-scsi: dataplane: notify guest as batch

Quoted 44 lines · Ming Lei

It isn't necessery to notify guest each time when one request is completed, and it should be enough to just notify one time for each running of virtio_scsi_iothread_handle_cmd().

This patch supresses about 30K/sec write on eventfd.

---
 hw/scsi/virtio-scsi-dataplane.c |    4 +++-
 hw/scsi/virtio-scsi.c           |   26 +++++++++++++++++++++++++-
 include/hw/virtio/virtio-scsi.h |    4 ++++
 3 files changed, 32 insertions(+), 2 deletions(-)
hw/scsi/virtio-scsi-dataplane.c +3 -1
index df17229..294515a 100644
@@ -62,6 +62,7 @@ static VirtIOSCSIVring *virtio_scsi_vring_init(VirtIOSCSI *s,
     aio_set_event_notifier(s->ctx, &r->host_notifier, handler);
 
     r->parent = s;
+    r->qid = n;
 
     if (!vring_setup(&r->vring, VIRTIO_DEVICE(s), n)) {
         fprintf(stderr, "virtio-scsi: VRing setup failed\n");
@@ -95,7 +96,8 @@ void virtio_scsi_vring_push_notify(VirtIOSCSIReq *req)
 {
     vring_push(&req->vring->vring, &req->elem,
                req->qsgl.size + req->resp_iov.size);
-    event_notifier_set(&req->vring->guest_notifier);
+    req->dev->pending_guest_notify |= 1 << (req->vring->qid - 2);
+    qemu_bh_schedule(req->dev->guest_notify_bh);
 }
 
 static void virtio_scsi_iothread_handle_ctrl(EventNotifier *notifier)
hw/scsi/virtio-scsi.c +1 -0
index 6e34a2c..98411ef 100644
@@ -86,6 +86,21 @@ static void virtio_scsi_complete_req(VirtIOSCSIReq *req)
     virtio_scsi_free_req(req);
 }
 
+static void notify_guest_bh(void *opaque)

hw/scsi/virtio-scsi.c static void virtio_scsi_complete_req(VirtIOSCSIReq *req) · line 99

Quoted 10 lines
hw/scsi/virtio-scsi.c +10 -0
+{
+    VirtIOSCSI *s = opaque;
+    unsigned int qid;
+    uint64_t pending = s->pending_guest_notify;
+
+    s->pending_guest_notify = 0;
+
+    while ((qid = ffsl(pending))) {
+        qid--;
+        event_notifier_set(&s->cmd_vrings[qid]->guest_notifier);

Don't we need to handle ctrlq and eventq as well?

Quoted 38 lines
hw/scsi/virtio-scsi.c +14 -1
+        pending &= ~(1 << qid);
+    }
+}
+
 static void virtio_scsi_bad_req(void)
 {
     error_report("wrong size for virtio-scsi headers");
@@ -824,7 +839,12 @@ void virtio_scsi_common_realize(DeviceState *dev, Error **errp,
     }
 
     if (s->conf.iothread) {
-        virtio_scsi_set_iothread(VIRTIO_SCSI(s), s->conf.iothread);
+        VirtIOSCSI *vis = VIRTIO_SCSI(s);
+
+        QEMU_BUILD_BUG_ON(VIRTIO_PCI_QUEUE_MAX > 64);
+        virtio_scsi_set_iothread(vis, s->conf.iothread);
+        vis->pending_guest_notify = 0;
+        vis->guest_notify_bh = aio_bh_new(vis->ctx, notify_guest_bh, vis);
     }
 }
 
@@ -901,7 +921,11 @@ void virtio_scsi_common_unrealize(DeviceState *dev, Error **errp)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
+    VirtIOSCSI *s = VIRTIO_SCSI(vs);
 
+    if (vs->conf.iothread) {
+        qemu_bh_delete(s->guest_notify_bh);
+    }
     g_free(vs->cmd_vqs);
     virtio_cleanup(vdev);
 }
include/hw/virtio/virtio-scsi.h +0 -0
index 9e1a49c..5e6c57e 100644

include/hw/virtio/virtio-scsi.h typedef struct { · line 166

Quoted 4 lines
include/hw/virtio/virtio-scsi.h +1 -0
@@ -163,6 +163,7 @@ typedef struct {
     Vring vring;
     EventNotifier host_notifier;
     EventNotifier guest_notifier;
+    uint32_t qid;

Could this "bool notify_pending"? In this case pending_guest_notify in VirtIOSCSI is not necessary. I guess looking into no more than 64 VirtIOSCSIVring elements in guest_notify_bh is not _that_ expensive so it's not worth the complexity.

include/hw/virtio/virtio-scsi.h typedef struct VirtIOSCSI { · line 204

Quoted 10 lines
include/hw/virtio/virtio-scsi.h +3 -0
 } VirtIOSCSIVring;
 
 typedef struct VirtIOSCSICommon {
@@ -198,6 +199,9 @@ typedef struct VirtIOSCSI {
     bool dataplane_fenced;
     Error *blocker;
     Notifier migration_state_notifier;
+
+    QEMUBH *guest_notify_bh;
+    uint64_t pending_guest_notify;

Fam

Quoted 6 lines
include/hw/virtio/virtio-scsi.h +0 -0
 } VirtIOSCSI;
 
 typedef struct VirtIOSCSIReq {
Signature · 1 line
1.7.9.5
author
#6 lore raw

Re: [Qemu-devel] [PATCH v1 2/2] virtio-scsi: dataplane: notify guest as batch

Quoted 44 lines · Fam Zheng
Quoted 43 lines · Ming Lei

It isn't necessery to notify guest each time when one request is completed, and it should be enough to just notify one time for each running of virtio_scsi_iothread_handle_cmd().

This patch supresses about 30K/sec write on eventfd.

---
 hw/scsi/virtio-scsi-dataplane.c |    4 +++-
 hw/scsi/virtio-scsi.c           |   26 +++++++++++++++++++++++++-
 include/hw/virtio/virtio-scsi.h |    4 ++++
 3 files changed, 32 insertions(+), 2 deletions(-)
hw/scsi/virtio-scsi-dataplane.c +3 -1
index df17229..294515a 100644
@@ -62,6 +62,7 @@ static VirtIOSCSIVring *virtio_scsi_vring_init(VirtIOSCSI *s,
     aio_set_event_notifier(s->ctx, &r->host_notifier, handler);
 
     r->parent = s;
+    r->qid = n;
 
     if (!vring_setup(&r->vring, VIRTIO_DEVICE(s), n)) {
         fprintf(stderr, "virtio-scsi: VRing setup failed\n");
@@ -95,7 +96,8 @@ void virtio_scsi_vring_push_notify(VirtIOSCSIReq *req)
 {
     vring_push(&req->vring->vring, &req->elem,
                req->qsgl.size + req->resp_iov.size);
-    event_notifier_set(&req->vring->guest_notifier);
+    req->dev->pending_guest_notify |= 1 << (req->vring->qid - 2);
+    qemu_bh_schedule(req->dev->guest_notify_bh);
 }
 
 static void virtio_scsi_iothread_handle_ctrl(EventNotifier *notifier)
hw/scsi/virtio-scsi.c +0 -0
index 6e34a2c..98411ef 100644
@@ -86,6 +86,21 @@ static void virtio_scsi_complete_req(VirtIOSCSIReq *req)
     virtio_scsi_free_req(req);
 }
 
Quoted 13 lines

hw/scsi/virtio-scsi.c static void virtio_scsi_complete_req(VirtIOSCSIReq *req) · line 99

Quoted 11 lines
hw/scsi/virtio-scsi.c +11 -0
+static void notify_guest_bh(void *opaque)
+{
+    VirtIOSCSI *s = opaque;
+    unsigned int qid;
+    uint64_t pending = s->pending_guest_notify;
+
+    s->pending_guest_notify = 0;
+
+    while ((qid = ffsl(pending))) {
+        qid--;
+        event_notifier_set(&s->cmd_vrings[qid]->guest_notifier);

Don't we need to handle ctrlq and eventq as well?

Most of times no frequent activity in ctrl and event vq, so it isn't necessary to make them involved.

Quoted 40 lines
Quoted 40 lines
hw/scsi/virtio-scsi.c +14 -1
+        pending &= ~(1 << qid);
+    }
+}
+
 static void virtio_scsi_bad_req(void)
 {
     error_report("wrong size for virtio-scsi headers");
@@ -824,7 +839,12 @@ void virtio_scsi_common_realize(DeviceState *dev, Error **errp,
     }
 
     if (s->conf.iothread) {
-        virtio_scsi_set_iothread(VIRTIO_SCSI(s), s->conf.iothread);
+        VirtIOSCSI *vis = VIRTIO_SCSI(s);
+
+        QEMU_BUILD_BUG_ON(VIRTIO_PCI_QUEUE_MAX > 64);
+        virtio_scsi_set_iothread(vis, s->conf.iothread);
+        vis->pending_guest_notify = 0;
+        vis->guest_notify_bh = aio_bh_new(vis->ctx, notify_guest_bh, vis);
     }
 }
 
@@ -901,7 +921,11 @@ void virtio_scsi_common_unrealize(DeviceState *dev, Error **errp)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
+    VirtIOSCSI *s = VIRTIO_SCSI(vs);
 
+    if (vs->conf.iothread) {
+        qemu_bh_delete(s->guest_notify_bh);
+    }
     g_free(vs->cmd_vqs);
     virtio_cleanup(vdev);
 }
include/hw/virtio/virtio-scsi.h +0 -0
index 9e1a49c..5e6c57e 100644
@@ -163,6 +163,7 @@ typedef struct {
     Vring vring;
Quoted 8 lines

include/hw/virtio/virtio-scsi.h typedef struct { · line 166

Quoted 3 lines
include/hw/virtio/virtio-scsi.h +1 -0
     EventNotifier host_notifier;
     EventNotifier guest_notifier;
+    uint32_t qid;

Could this "bool notify_pending"? In this case pending_guest_notify in VirtIOSCSI is not necessary. I guess looking into no more than 64 VirtIOSCSIVring elements in guest_notify_bh is not _that_ expensive so it's not worth the complexity.

We need to know which queue the pending notifier is sent to in BH handler, and 64 is the limit for queue number, not element number.

Actually it is a bit expensive, and 30K/sec write syscall is wasted in the unnecessary & repeated notifying.

And the similar approach is applied in virtio-blk dataplane too, :-)

Thanks,
Ming Lei

#7 lore raw

Re: [Qemu-devel] [PATCH v1 2/2] virtio-scsi: dataplane: notify guest as batch

Quoted 59 lines · Ming Lei
Quoted 44 lines · Fam Zheng
Quoted 43 lines · Ming Lei

It isn't necessery to notify guest each time when one request is completed, and it should be enough to just notify one time for each running of virtio_scsi_iothread_handle_cmd().

This patch supresses about 30K/sec write on eventfd.

---
 hw/scsi/virtio-scsi-dataplane.c |    4 +++-
 hw/scsi/virtio-scsi.c           |   26 +++++++++++++++++++++++++-
 include/hw/virtio/virtio-scsi.h |    4 ++++
 3 files changed, 32 insertions(+), 2 deletions(-)
hw/scsi/virtio-scsi-dataplane.c +3 -1
index df17229..294515a 100644
@@ -62,6 +62,7 @@ static VirtIOSCSIVring *virtio_scsi_vring_init(VirtIOSCSI *s,
     aio_set_event_notifier(s->ctx, &r->host_notifier, handler);
 
     r->parent = s;
+    r->qid = n;
 
     if (!vring_setup(&r->vring, VIRTIO_DEVICE(s), n)) {
         fprintf(stderr, "virtio-scsi: VRing setup failed\n");
@@ -95,7 +96,8 @@ void virtio_scsi_vring_push_notify(VirtIOSCSIReq *req)
 {
     vring_push(&req->vring->vring, &req->elem,
                req->qsgl.size + req->resp_iov.size);
-    event_notifier_set(&req->vring->guest_notifier);
+    req->dev->pending_guest_notify |= 1 << (req->vring->qid - 2);
+    qemu_bh_schedule(req->dev->guest_notify_bh);
 }
 
 static void virtio_scsi_iothread_handle_ctrl(EventNotifier *notifier)
hw/scsi/virtio-scsi.c +0 -0
index 6e34a2c..98411ef 100644
@@ -86,6 +86,21 @@ static void virtio_scsi_complete_req(VirtIOSCSIReq *req)
     virtio_scsi_free_req(req);
 }
 
Quoted 13 lines

hw/scsi/virtio-scsi.c static void virtio_scsi_complete_req(VirtIOSCSIReq *req) · line 99

Quoted 11 lines
hw/scsi/virtio-scsi.c +11 -0
+static void notify_guest_bh(void *opaque)
+{
+    VirtIOSCSI *s = opaque;
+    unsigned int qid;
+    uint64_t pending = s->pending_guest_notify;
+
+    s->pending_guest_notify = 0;
+
+    while ((qid = ffsl(pending))) {
+        qid--;
+        event_notifier_set(&s->cmd_vrings[qid]->guest_notifier);

Don't we need to handle ctrlq and eventq as well?

Quoted 2 lines

Most of times no frequent activity in ctrl and event vq, so it isn't necessary to make them involved.

I mean virtio_scsi_vring_push_notify is not only used to notify cmd, it should also work for ctrl and event, because its caller virtio_scsi_complete_req is also called in virtio_scsi_handle_ctrl_req and virtio_scsi_push_event.

Quoted 50 lines
Quoted 41 lines
Quoted 41 lines
hw/scsi/virtio-scsi.c +14 -1
+        pending &= ~(1 << qid);
+    }
+}
+
 static void virtio_scsi_bad_req(void)
 {
     error_report("wrong size for virtio-scsi headers");
@@ -824,7 +839,12 @@ void virtio_scsi_common_realize(DeviceState *dev, Error **errp,
     }
 
     if (s->conf.iothread) {
-        virtio_scsi_set_iothread(VIRTIO_SCSI(s), s->conf.iothread);
+        VirtIOSCSI *vis = VIRTIO_SCSI(s);
+
+        QEMU_BUILD_BUG_ON(VIRTIO_PCI_QUEUE_MAX > 64);
+        virtio_scsi_set_iothread(vis, s->conf.iothread);
+        vis->pending_guest_notify = 0;
+        vis->guest_notify_bh = aio_bh_new(vis->ctx, notify_guest_bh, vis);
     }
 }
 
@@ -901,7 +921,11 @@ void virtio_scsi_common_unrealize(DeviceState *dev, Error **errp)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
+    VirtIOSCSI *s = VIRTIO_SCSI(vs);
 
+    if (vs->conf.iothread) {
+        qemu_bh_delete(s->guest_notify_bh);
+    }
     g_free(vs->cmd_vqs);
     virtio_cleanup(vdev);
 }
include/hw/virtio/virtio-scsi.h +0 -0
index 9e1a49c..5e6c57e 100644
@@ -163,6 +163,7 @@ typedef struct {
     Vring vring;
Quoted 8 lines

include/hw/virtio/virtio-scsi.h typedef struct { · line 166

Quoted 3 lines
include/hw/virtio/virtio-scsi.h +1 -0
     EventNotifier host_notifier;
     EventNotifier guest_notifier;
+    uint32_t qid;

Could this "bool notify_pending"? In this case pending_guest_notify in VirtIOSCSI is not necessary. I guess looking into no more than 64 VirtIOSCSIVring elements in guest_notify_bh is not _that_ expensive so it's not worth the complexity.

Quoted 8 lines

We need to know which queue the pending notifier is sent to in BH handler, and 64 is the limit for queue number, not element number.

Actually it is a bit expensive, and 30K/sec write syscall is wasted in the unnecessary & repeated notifying.

And the similar approach is applied in virtio-blk dataplane too, :-)

I mean something like:

include/hw/virtio/virtio-scsi.h +3 -1
index 9e1a49c..5e6c57e 100644
@@ -163,6 +163,7 @@ typedef struct {
     Vring vring;
     EventNotifier host_notifier;
     EventNotifier guest_notifier;
+    bool notify_pending;
 
@@ -95,7 +96,8 @@ void virtio_scsi_vring_push_notify(VirtIOSCSIReq *req)
 {
     vring_push(&req->vring->vring, &req->elem,
                req->qsgl.size + req->resp_iov.size);
-    event_notifier_set(&req->vring->guest_notifier);
+    req->vring->pending_notify = true;
+    qemu_bh_schedule(req->dev->guest_notify_bh);
 }
 

And in notify_guest_bh you go through all the vrings and call event_notifier_set on those with vring->pending_notify == true.

Fam

author
#8 lore raw

Re: [Qemu-devel] [PATCH v1 2/2] virtio-scsi: dataplane: notify guest as batch

Quoted 147 lines · Fam Zheng
Quoted 59 lines · Ming Lei
Quoted 44 lines · Fam Zheng
Quoted 43 lines · Ming Lei

It isn't necessery to notify guest each time when one request is completed, and it should be enough to just notify one time for each running of virtio_scsi_iothread_handle_cmd().

This patch supresses about 30K/sec write on eventfd.

---
 hw/scsi/virtio-scsi-dataplane.c |    4 +++-
 hw/scsi/virtio-scsi.c           |   26 +++++++++++++++++++++++++-
 include/hw/virtio/virtio-scsi.h |    4 ++++
 3 files changed, 32 insertions(+), 2 deletions(-)
hw/scsi/virtio-scsi-dataplane.c +3 -1
index df17229..294515a 100644
@@ -62,6 +62,7 @@ static VirtIOSCSIVring *virtio_scsi_vring_init(VirtIOSCSI *s,
     aio_set_event_notifier(s->ctx, &r->host_notifier, handler);
 
     r->parent = s;
+    r->qid = n;
 
     if (!vring_setup(&r->vring, VIRTIO_DEVICE(s), n)) {
         fprintf(stderr, "virtio-scsi: VRing setup failed\n");
@@ -95,7 +96,8 @@ void virtio_scsi_vring_push_notify(VirtIOSCSIReq *req)
 {
     vring_push(&req->vring->vring, &req->elem,
                req->qsgl.size + req->resp_iov.size);
-    event_notifier_set(&req->vring->guest_notifier);
+    req->dev->pending_guest_notify |= 1 << (req->vring->qid - 2);
+    qemu_bh_schedule(req->dev->guest_notify_bh);
 }
 
 static void virtio_scsi_iothread_handle_ctrl(EventNotifier *notifier)
hw/scsi/virtio-scsi.c +0 -0
index 6e34a2c..98411ef 100644
@@ -86,6 +86,21 @@ static void virtio_scsi_complete_req(VirtIOSCSIReq *req)
     virtio_scsi_free_req(req);
 }
 
Quoted 13 lines

hw/scsi/virtio-scsi.c static void virtio_scsi_complete_req(VirtIOSCSIReq *req) · line 99

Quoted 11 lines
hw/scsi/virtio-scsi.c +11 -0
+static void notify_guest_bh(void *opaque)
+{
+    VirtIOSCSI *s = opaque;
+    unsigned int qid;
+    uint64_t pending = s->pending_guest_notify;
+
+    s->pending_guest_notify = 0;
+
+    while ((qid = ffsl(pending))) {
+        qid--;
+        event_notifier_set(&s->cmd_vrings[qid]->guest_notifier);

Don't we need to handle ctrlq and eventq as well?

Quoted 2 lines

Most of times no frequent activity in ctrl and event vq, so it isn't necessary to make them involved.

I mean virtio_scsi_vring_push_notify is not only used to notify cmd, it should also work for ctrl and event, because its caller virtio_scsi_complete_req is also called in virtio_scsi_handle_ctrl_req and virtio_scsi_push_event.

Quoted 50 lines
Quoted 41 lines
Quoted 41 lines
hw/scsi/virtio-scsi.c +14 -1
+        pending &= ~(1 << qid);
+    }
+}
+
 static void virtio_scsi_bad_req(void)
 {
     error_report("wrong size for virtio-scsi headers");
@@ -824,7 +839,12 @@ void virtio_scsi_common_realize(DeviceState *dev, Error **errp,
     }
 
     if (s->conf.iothread) {
-        virtio_scsi_set_iothread(VIRTIO_SCSI(s), s->conf.iothread);
+        VirtIOSCSI *vis = VIRTIO_SCSI(s);
+
+        QEMU_BUILD_BUG_ON(VIRTIO_PCI_QUEUE_MAX > 64);
+        virtio_scsi_set_iothread(vis, s->conf.iothread);
+        vis->pending_guest_notify = 0;
+        vis->guest_notify_bh = aio_bh_new(vis->ctx, notify_guest_bh, vis);
     }
 }
 
@@ -901,7 +921,11 @@ void virtio_scsi_common_unrealize(DeviceState *dev, Error **errp)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
+    VirtIOSCSI *s = VIRTIO_SCSI(vs);
 
+    if (vs->conf.iothread) {
+        qemu_bh_delete(s->guest_notify_bh);
+    }
     g_free(vs->cmd_vqs);
     virtio_cleanup(vdev);
 }
include/hw/virtio/virtio-scsi.h +0 -0
index 9e1a49c..5e6c57e 100644
@@ -163,6 +163,7 @@ typedef struct {
     Vring vring;
Quoted 8 lines

include/hw/virtio/virtio-scsi.h typedef struct {

Quoted 3 lines
include/hw/virtio/virtio-scsi.h +1 -0
     EventNotifier host_notifier;
     EventNotifier guest_notifier;
+    uint32_t qid;

Could this "bool notify_pending"? In this case pending_guest_notify in VirtIOSCSI is not necessary. I guess looking into no more than 64 VirtIOSCSIVring elements in guest_notify_bh is not _that_ expensive so it's not worth the complexity.

Quoted 8 lines

We need to know which queue the pending notifier is sent to in BH handler, and 64 is the limit for queue number, not element number.

Actually it is a bit expensive, and 30K/sec write syscall is wasted in the unnecessary & repeated notifying.

And the similar approach is applied in virtio-blk dataplane too, :-)

I mean something like:

include/hw/virtio/virtio-scsi.h +3 -1
index 9e1a49c..5e6c57e 100644
@@ -163,6 +163,7 @@ typedef struct {
     Vring vring;
     EventNotifier host_notifier;
     EventNotifier guest_notifier;
+    bool notify_pending;
 
@@ -95,7 +96,8 @@ void virtio_scsi_vring_push_notify(VirtIOSCSIReq *req)
 {
     vring_push(&req->vring->vring, &req->elem,
                req->qsgl.size + req->resp_iov.size);
-    event_notifier_set(&req->vring->guest_notifier);
+    req->vring->pending_notify = true;
+    qemu_bh_schedule(req->dev->guest_notify_bh);
 }
 
Quoted 2 lines

And in notify_guest_bh you go through all the vrings and call event_notifier_set on those with vring->pending_notify == true.

You are right, and I missed ctrl and event vq, and it should be simpler to introduce vring->pending_notify.

Will do that in v2, thanks.

Thanks,

#9 lore raw

Re: [Qemu-devel] [PATCH v1 2/2] virtio-scsi: dataplane: notify guest as batch

In reply to Ming Lei
Quoted 4 lines · Ming Lei
hw/scsi/virtio-scsi.c +4 -0
+static void notify_guest_bh(void *opaque)
+{
+    VirtIOSCSI *s = opaque;
+    unsigned int qid;

hw/scsi/virtio-scsi.c static void virtio_scsi_complete_req(VirtIOSCSIReq *req) · line 102

Quoted 10 lines
hw/scsi/virtio-scsi.c +10 -0
+    uint64_t pending = s->pending_guest_notify;
+
+    s->pending_guest_notify = 0;
+
+    while ((qid = ffsl(pending))) {
+        qid--;
+        event_notifier_set(&s->cmd_vrings[qid]->guest_notifier);
+        pending &= ~(1 << qid);
+    }
+}

Looks like we're not honoring virtio's usual interrupt mitigation mechanism (e.g. EVENT_IDX) for virtio-scsi. Why is vring_should_notify() not used?

Quoted 5 lines
hw/scsi/virtio-scsi.c +1 -0
+
 static void virtio_scsi_bad_req(void)
 {
     error_report("wrong size for virtio-scsi headers");

hw/scsi/virtio-scsi.c void virtio_scsi_common_realize(DeviceState *dev, Error **errp, · line 847

Quoted 10 lines
hw/scsi/virtio-scsi.c +6 -1
@@ -824,7 +839,12 @@ void virtio_scsi_common_realize(DeviceState *dev, Error **errp,
     }
 
     if (s->conf.iothread) {
-        virtio_scsi_set_iothread(VIRTIO_SCSI(s), s->conf.iothread);
+        VirtIOSCSI *vis = VIRTIO_SCSI(s);
+
+        QEMU_BUILD_BUG_ON(VIRTIO_PCI_QUEUE_MAX > 64);
+        virtio_scsi_set_iothread(vis, s->conf.iothread);
+        vis->pending_guest_notify = 0;
+        vis->guest_notify_bh = aio_bh_new(vis->ctx, notify_guest_bh, vis);

Have you checked state transitions like PCI or virtio reset? They need to cancel the BH and clear ->pending_guest_notify.

There is also a transition from dataplane to non-dataplane mode for live migration.

Please make sure no interrupts are dropped or stale data is kept across these transitions.

author
#10 lore raw

Re: [Qemu-devel] [PATCH v1 2/2] virtio-scsi: dataplane: notify guest as batch

Quoted 11 lines · Stefan Hajnoczi
Quoted 10 lines · Ming Lei
hw/scsi/virtio-scsi.c +10 -0
+static void notify_guest_bh(void *opaque)
+{
+    VirtIOSCSI *s = opaque;
+    unsigned int qid;
+    uint64_t pending = s->pending_guest_notify;
+
+    s->pending_guest_notify = 0;
+
+    while ((qid = ffsl(pending))) {
+        qid--;
Quoted 8 lines

hw/scsi/virtio-scsi.c static void virtio_scsi_complete_req(VirtIOSCSIReq *req) · line 102

Quoted 4 lines
hw/scsi/virtio-scsi.c +4 -0
+        event_notifier_set(&s->cmd_vrings[qid]->guest_notifier);
+        pending &= ~(1 << qid);
+    }
+}

Looks like we're not honoring virtio's usual interrupt mitigation mechanism (e.g. EVENT_IDX) for virtio-scsi. Why is vring_should_notify() not used?

Yes, that should be used, but in virtio-blk, BH still can suppress about ~13K/sec write(), and for virtio-scsi, it should be more since there may be more queues.

Quoted 20 lines
Quoted 6 lines
hw/scsi/virtio-scsi.c +1 -0
+
 static void virtio_scsi_bad_req(void)
 {
     error_report("wrong size for virtio-scsi headers");

hw/scsi/virtio-scsi.c void virtio_scsi_common_realize(DeviceState *dev, Error **errp, · line 847

Quoted 10 lines
hw/scsi/virtio-scsi.c +6 -1
@@ -824,7 +839,12 @@ void virtio_scsi_common_realize(DeviceState *dev, Error **errp,
     }
 
     if (s->conf.iothread) {
-        virtio_scsi_set_iothread(VIRTIO_SCSI(s), s->conf.iothread);
+        VirtIOSCSI *vis = VIRTIO_SCSI(s);
+
+        QEMU_BUILD_BUG_ON(VIRTIO_PCI_QUEUE_MAX > 64);
+        virtio_scsi_set_iothread(vis, s->conf.iothread);
+        vis->pending_guest_notify = 0;
+        vis->guest_notify_bh = aio_bh_new(vis->ctx, notify_guest_bh, vis);

Have you checked state transitions like PCI or virtio reset? They need to cancel the BH and clear ->pending_guest_notify.

Quoted 5 lines

There is also a transition from dataplane to non-dataplane mode for live migration.

Please make sure no interrupts are dropped or stale data is kept across these transitions.

Good catch, and I will make sure that in next version, and virtio-blk dataplane need to consider that too.

Thanks,