virtio-scsi-dataplane: one fix and one optimization
cover virtio-scsi-dataplane: one fix and one optimization
The 1st patch fixes an allocation problem.
The 2nd one supresses writing eventfd a lot(~30K/sec in my test).
Thanks,
Ming Lei
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
@@ -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_malloc0 (sizeof (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
Re: [Qemu-devel] [PATCH 1/2] virtio-scsi-dataplane: fix allocation for 'cmd_vrings'
Quoted 12 lines · Ming Lei
The size of each element should be
sizeof(VirtIOSCSIVring *).Signed-off-by Ming Lei <[email protected]>--- hw/scsi/virtio-scsi-dataplane.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/scsi/virtio-scsi-dataplane.c +0
-0
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_malloc0 (sizeof (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],
Please use something like
s->cmd_vrings = g_new0(VirtIOSCSIVring *, vs->conf.num_queues);
This one crept in since I cleaned up g_malloc() use globally:
commit 02c4f26b1517d9e403ec10d6f6ca3c0276d19e43
Author: Markus Armbruster <[email protected]> Date: Tue Aug 19 10:31:09 2014 +0200
block: Use g_new() & friends to avoid multiplying sizes
g_new(T, n) is safer than g_malloc(sizeof(*v) * n) for two reasons. One, it catches multiplication overflowing size_t. Two, it returns T * rather than void *, which lets the compiler catch more type errors.
Perhaps a conversion to g_malloc_n() would be neater in places, but that's merely four years old, and we can't use such newfangled stuff.
This commit only touches allocations with size arguments of the form sizeof(T), plus two that use 4 instead of sizeof(uint32_t). We can make the others safe by converting to g_malloc_n() when it becomes available to us in a couple of years.
Re: [Qemu-devel] [PATCH 1/2] virtio-scsi-dataplane: fix allocation for 'cmd_vrings'
Quoted 25 lines · Markus Armbruster
Quoted 12 lines · Ming Lei
The size of each element should be
sizeof(VirtIOSCSIVring *).Signed-off-by Ming Lei <[email protected]>--- hw/scsi/virtio-scsi-dataplane.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/scsi/virtio-scsi-dataplane.c +0
-0hw/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_malloc0 (sizeof (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],Please use something like
Quoted 3 lines
s->cmd_vrings = g_new0(VirtIOSCSIVring *, vs->conf.num_queues);This one crept in since I cleaned up
g_malloc()use globally:
Your idea is good, but this one is a fix patch, and I think the g_new() conversion should be done in another patch since the two changes are different logically.
Thanks,
Re: [Qemu-devel] [PATCH 1/2] virtio-scsi-dataplane: fix allocation for 'cmd_vrings'
Am 10.11.2014 um 10:14 hat Ming Lei geschrieben:
Quoted 26 lines · Markus Armbruster
Quoted 25 lines
Quoted 12 lines · Ming Lei
The size of each element should be
sizeof(VirtIOSCSIVring *).Signed-off-by Ming Lei <[email protected]>--- hw/scsi/virtio-scsi-dataplane.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/scsi/virtio-scsi-dataplane.c +0
-0hw/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_malloc0 (sizeof (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],Please use something like
Quoted 7 lines
Quoted 3 lines
s->cmd_vrings = g_new0(VirtIOSCSIVring *, vs->conf.num_queues);This one crept in since I cleaned up
g_malloc()use globally:Your idea is good, but this one is a fix patch, and I think the
g_new()conversion should be done in another patch since the two changes are different logically.
It's not really unrelated: g_new() would have caught the incorrect type and made it a compiler error. So changing to g_new() in a patch fixing such a bug is actually a logical conclusion and makes it more obvious that your patch is correct.
Kevin
Re: [Qemu-devel] [PATCH 1/2] virtio-scsi-dataplane: fix allocation for 'cmd_vrings'
Quoted 31 lines · Kevin Wolf
Am 10.11.2014 um 10:14 hat Ming Lei geschrieben:
Quoted 30 lines · Markus Armbruster
Quoted 28 lines
Quoted 12 lines · Ming Lei
The size of each element should be
sizeof(VirtIOSCSIVring *).Signed-off-by Ming Lei <[email protected]>--- hw/scsi/virtio-scsi-dataplane.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/scsi/virtio-scsi-dataplane.c +0
-0hw/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_malloc0 (sizeof (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],Please use something like
s->cmd_vrings = g_new0(VirtIOSCSIVring *, vs->conf.num_queues);This one crept in since I cleaned up
g_malloc()use globally:
Quoted 8 lines
Quoted 3 lines
Your idea is good, but this one is a fix patch, and I think the
g_new()conversion should be done in another patch since the two changes are different logically.It's not really unrelated:
g_new()would have caught the incorrect type and made it a compiler error. So changing tog_new()in a patch fixing such a bug is actually a logical conclusion and makes it more obvious that your patch is correct.
Fair enough, will post v1 with g_new() conversion.
Thanks,
PATCH 2/2 virtio-scsi-dataplane: notify guest as batch
In reply to Ming LeiIt 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 | 22 ++++++++++++++++++++++ include/hw/virtio/virtio-scsi.h | 4 ++++ 3 files changed, 29 insertions(+), 1 deletion(-)
hw/scsi/virtio-scsi-dataplane.c +3 -1
@@ -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 +22 -0
@@ -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" );
@@ -825,6 +840,10 @@ void virtio_scsi_common_realize(DeviceState *dev, Error **errp,if (s->conf.iothread) {virtio_scsi_set_iothread (VIRTIO_SCSI (s), s->conf.iothread); +VIRTIO_SCSI (s)->pending_guest_notify =0 ; +VIRTIO_SCSI (s)->guest_notify_bh =aio_bh_new (VIRTIO_SCSI (s)->ctx, + notify_guest_bh, +VIRTIO_SCSI (s)); } }
@@ -902,6 +921,9 @@ void virtio_scsi_common_unrealize(DeviceState *dev, Error **errp)VirtIODevice *vdev =VIRTIO_DEVICE (dev);VirtIOSCSICommon *vs =VIRTIO_SCSI_COMMON (dev); +if (vs->conf.iothread) { +qemu_bh_delete (VIRTIO_SCSI (vs)->guest_notify_bh); + }g_free (vs->cmd_vqs);virtio_cleanup (vdev); }
include/hw/virtio/virtio-scsi.h +4 -0
@@ -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
Re: [Qemu-devel] [PATCH 2/2] virtio-scsi-dataplane: notify guest as batch
Quoted 62 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.
Signed-off-by Ming Lei <[email protected]>--- hw/scsi/virtio-scsi-dataplane.c | 4 +++- hw/scsi/virtio-scsi.c | 22 ++++++++++++++++++++++ include/hw/virtio/virtio-scsi.h | 4 ++++ 3 files changed, 29 insertions(+), 1 deletion(-)hw/scsi/virtio-scsi-dataplane.c +3
-1@@ -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 +15
-0@@ -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" );
hw/scsi/virtio-scsi.c void virtio_scsi_common_realize(DeviceState *dev, Error **errp, · line 846
Quoted 7 lines
hw/scsi/virtio-scsi.c +4
-0@@ -825,6 +840,10 @@ void virtio_scsi_common_realize(DeviceState *dev, Error **errp,if (s->conf.iothread) {virtio_scsi_set_iothread (VIRTIO_SCSI (s), s->conf.iothread); +VIRTIO_SCSI (s)->pending_guest_notify =0 ; +VIRTIO_SCSI (s)->guest_notify_bh =aio_bh_new (VIRTIO_SCSI (s)->ctx, + notify_guest_bh, +VIRTIO_SCSI (s));
Please use a separete variable to host VIRTIO_SCSI(s).
This is usually the standard if the result of the cast is dereferenced.
hw/scsi/virtio-scsi.c void virtio_scsi_common_unrealize(DeviceState *dev, Error **errp) · line 925
Quoted 9 lines
hw/scsi/virtio-scsi.c +2
-0} }@@ -902,6 +921,9 @@ void virtio_scsi_common_unrealize(DeviceState *dev, Error **errp)VirtIODevice *vdev =VIRTIO_DEVICE (dev);VirtIOSCSICommon *vs =VIRTIO_SCSI_COMMON (dev); +if (vs->conf.iothread) { +qemu_bh_delete (VIRTIO_SCSI (vs)->guest_notify_bh);
Here too.
Quoted 17 lines
hw/scsi/virtio-scsi.c +1
-0+ }g_free (vs->cmd_vqs);virtio_cleanup (vdev); }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; } VirtIOSCSIVring; typedef struct VirtIOSCSICommon {
include/hw/virtio/virtio-scsi.h typedef struct VirtIOSCSI { · line 204
Quoted 6 lines
include/hw/virtio/virtio-scsi.h +3
-0@@ -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;
Please add a
BUILD_BUG_ON(VIRTIO_PCI_QUEUE_MAX <= 64);
too.
include/hw/virtio/virtio-scsi.h typedef struct VirtIOSCSI { · line 207
Quoted 4 lines
include/hw/virtio/virtio-scsi.h +0
-0} VirtIOSCSI; typedef struct VirtIOSCSIReq {
Thanks,
Paolo