dataplane: fix start/stop races
PATCH 1/2 virtio: add aio handler
In reply to Michael S. TsirkinIn addition to handling IO in vcpu thread and in io thread, blk dataplane introduces yet another mode:
handling it by aio.
This reuses the same handler as previous modes,
which triggers races as these were not designed to be reentrant.
As a temporary fix, add a separate handler just for aio, this will make it possible to disable regular handlers when dataplane is active.
--- include/hw/virtio/virtio.h | 4 ++++ hw/virtio/virtio.c | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 4 deletions(-)
include/hw/virtio/virtio.h +4 -0
@@ -142,6 +142,9 @@ VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,void (*handle_output)(VirtIODevice *, VirtQueue *)); +void virtio_set_queue_aio (VirtQueue *vq, +void (*handle_output)(VirtIODevice *,VirtQueue *)); +void virtio_del_queue (VirtIODevice *vdev,int n);void *virtqueue_alloc_element (size_t sz,unsigned out_num,unsigned in_num);
@@ -253,6 +256,7 @@ void virtio_queue_set_host_notifier_fd_handler(VirtQueue *vq, bool assign,void virtio_queue_aio_set_host_notifier_handler (VirtQueue *vq,AioContext *ctx,bool assign,bool set_handler);void virtio_queue_notify_vq (VirtQueue *vq); +void virtio_queue_notify_aio_vq (VirtQueue *vq);void virtio_irq (VirtQueue *vq);VirtQueue *virtio_vector_first_queue (VirtIODevice *vdev,uint16_t vector);VirtQueue *virtio_vector_next_queue (VirtQueue *vq);
hw/virtio/virtio.c +32 -4
@@ -94,6 +94,7 @@ struct VirtQueueuint16_t vector;void (*handle_output)(VirtIODevice *vdev,VirtQueue *vq); +void (*handle_aio_output)(VirtIODevice *vdev,VirtQueue *vq);VirtIODevice *vdev;EventNotifier guest_notifier;EventNotifier host_notifier;
@@ -1086,6 +1087,16 @@ void virtio_queue_set_align(VirtIODevice *vdev, int n, int align)virtio_queue_update_rings (vdev, n); } +void virtio_queue_notify_aio_vq (VirtQueue *vq) +{ +if (vq->vring.desc && vq->handle_aio_output) { +VirtIODevice *vdev = vq->vdev; + +trace_virtio_queue_notify (vdev, vq - vdev->vq, vq); + vq->handle_aio_output (vdev, vq); + } +} +void virtio_queue_notify_vq (VirtQueue *vq) {if (vq->vring.desc && vq->handle_output) {
@@ -1141,10 +1152,19 @@ VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size, vdev->vq[i].vring.num_default = queue_size; vdev->vq[i].vring.align =VIRTIO_PCI_VRING_ALIGN ; vdev->vq[i].handle_output = handle_output; + vdev->vq[i].handle_aio_output =NULL ;return &vdev->vq[i]; } +void virtio_set_queue_aio (VirtQueue *vq, +void (*handle_output)(VirtIODevice *,VirtQueue *)) +{ +assert (vq->handle_output); + + vq->handle_aio_output = handle_output; +} +void virtio_del_queue (VirtIODevice *vdev,int n) {if (n <0 || n >=VIRTIO_QUEUE_MAX ) {
@@ -1778,11 +1798,11 @@ EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq)return &vq->guest_notifier; } -static void virtio_queue_host_notifier_read (EventNotifier *n) +static void virtio_queue_host_notifier_aio_read (EventNotifier *n) {VirtQueue *vq =container_of (n, VirtQueue, host_notifier);if (event_notifier_test_and_clear (n)) { -virtio_queue_notify_vq (vq); +virtio_queue_notify_aio_vq (vq); } }
@@ -1791,14 +1811,22 @@ void virtio_queue_aio_set_host_notifier_handler(VirtQueue *vq, AioContext *ctx, {if (assign && set_handler) {aio_set_event_notifier (ctx, &vq->host_notifier, true, - virtio_queue_host_notifier_read); + virtio_queue_host_notifier_aio_read); }else {aio_set_event_notifier (ctx, &vq->host_notifier, true,NULL ); }if (!assign) {/* Test and clear notifier before after disabling event, * in case poll callback didn't have time to run. */ -virtio_queue_host_notifier_read (&vq->host_notifier); +virtio_queue_host_notifier_aio_read (&vq->host_notifier); + } +} + +static void virtio_queue_host_notifier_read (EventNotifier *n) +{ +VirtQueue *vq =container_of (n, VirtQueue, host_notifier); +if (event_notifier_test_and_clear (n)) { +virtio_queue_notify_vq (vq); } }
Signature · 1 line
MST
cover dataplane: fix start/stop races
This works around races that data plane introduces simply by exiting immediately if we detect that dataplane is active.
It's a small but ugly patch, it's only justification is that it's minimally intrusive, and that it clearly has no chance to break non data plane users.
The idea is to rework it all post 2.6.
Michael S. Tsirkin (2):
virtio: add aio handler
virtio-blk: use aio handler for data plane
include/hw/virtio/virtio-blk.h | 2 ++ include/hw/virtio/virtio.h | 4 ++++ hw/block/dataplane/virtio-blk.c | 13 +++++++++++++ hw/block/virtio-blk.c | 28 ++++++++++++++++++---------- hw/virtio/virtio.c | 36 ++++++++++++++++++++++++++++++++---- 5 files changed, 69 insertions(+), 14 deletions(-)
Signature · 1 line
MST
PATCH 2/2 virtio-blk: use aio handler for data plane
In addition to handling IO in vcpu thread and in io thread, blk dataplane introduces yet another mode:
handling it by aio.
This reuses the same handler as previous modes,
which triggers races as these were not designed to be reentrant.
As a temporary fix, use a separate handler just for aio, and disable regular handlers when dataplane is active.
--- include/hw/virtio/virtio-blk.h | 2 ++ hw/block/dataplane/virtio-blk.c | 13 +++++++++++++ hw/block/virtio-blk.c | 28 ++++++++++++++++++---------- 3 files changed, 33 insertions(+), 10 deletions(-)
include/hw/virtio/virtio-blk.h +2 -0
@@ -85,4 +85,6 @@ void virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb);void virtio_blk_submit_multireq (BlockBackend *blk,MultiReqBuffer *mrb); +void virtio_blk_handle_vq (VirtIOBlock *s,VirtQueue *vq); +#endif
hw/block/dataplane/virtio-blk.c +13 -0
@@ -184,6 +184,17 @@ void virtio_blk_data_plane_destroy(VirtIOBlockDataPlane *s)g_free (s); } +static void virtio_blk_data_plane_handle_output (VirtIODevice *vdev, +VirtQueue *vq) +{ +VirtIOBlock *s =VIRTIO_BLK (vdev); + +assert (s->dataplane); +assert (s->dataplane_started); + +virtio_blk_handle_vq (s, vq); +} +/* Context: QEMU global mutex held */ void virtio_blk_data_plane_start (VirtIOBlockDataPlane *s) {
@@ -226,6 +237,7 @@ void virtio_blk_data_plane_start(VirtIOBlockDataPlane *s)/* Get this show started by hooking up our callbacks */ aio_context_acquire (s->ctx); +virtio_set_queue_aio (s->vq, virtio_blk_data_plane_handle_output);virtio_queue_aio_set_host_notifier_handler (s->vq, s->ctx, true, true);aio_context_release (s->ctx);return ;
@@ -262,6 +274,7 @@ void virtio_blk_data_plane_stop(VirtIOBlockDataPlane *s)/* Stop notifications for new requests from guest */ virtio_queue_aio_set_host_notifier_handler (s->vq, s->ctx, false, false); +virtio_set_queue_aio (s->vq,NULL );/* Drain and switch bs back to the QEMU main loop */ blk_set_aio_context (s->conf->conf.blk,qemu_get_aio_context ());
hw/block/virtio-blk.c +18 -10
@@ -577,20 +577,11 @@ void virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb) } } -static void virtio_blk_handle_output (VirtIODevice *vdev,VirtQueue *vq) +void virtio_blk_handle_vq (VirtIOBlock *s,VirtQueue *vq) { -VirtIOBlock *s =VIRTIO_BLK (vdev);VirtIOBlockReq *req;MultiReqBuffer mrb = {}; -/* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start -* dataplane here instead of waiting for .set_status(). -*/ -if (s->dataplane && !s->dataplane_started) { -virtio_blk_data_plane_start (s->dataplane); -return ; - } -blk_io_plug (s->blk);while ((req =virtio_blk_get_request (s))) {
@@ -604,6 +595,23 @@ static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)blk_io_unplug (s->blk); } +static void virtio_blk_handle_output (VirtIODevice *vdev,VirtQueue *vq) +{ +VirtIOBlock *s =VIRTIO_BLK (vdev); + +if (s->dataplane) { +/* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start +* dataplane here instead of waiting for .set_status(). +*/ +if (!s->dataplane_started) { +virtio_blk_data_plane_start (s->dataplane); + } +return ; + } + +virtio_blk_handle_vq (s, vq); +} +static void virtio_blk_dma_restart_bh (void *opaque) {VirtIOBlock *s = opaque;
Signature · 1 line
MST
Quoted 9 lines · Michael S. Tsirkin
This works around races that data plane introduces simply by exiting immediately if we detect that dataplane is active.
It's a small but ugly patch, it's only justification is that it's minimally intrusive, and that it clearly has no chance to break non data plane users.
The idea is to rework it all post 2.6.
Testing status: lightly tested.
Quoted 14 lines
Michael S. Tsirkin (2):
virtio: add aio handler
virtio-blk: use aio handler for data planeinclude/hw/virtio/virtio-blk.h | 2 ++ include/hw/virtio/virtio.h | 4 ++++ hw/block/dataplane/virtio-blk.c | 13 +++++++++++++ hw/block/virtio-blk.c | 28 ++++++++++++++++++---------- hw/virtio/virtio.c | 36 ++++++++++++++++++++++++++++++++---- 5 files changed, 69 insertions(+), 14 deletions(-)Signature · 1 line
MST
Re: [Qemu-devel] [PATCH 2/2] virtio-blk: use aio handler for data plane
In reply to Michael S. Tsirkinhw/block/dataplane/virtio-blk.c void virtio_blk_data_plane_stop(VirtIOBlockDataPlane *s) · line 276
Quoted 4 lines · Michael S. Tsirkin
hw/block/dataplane/virtio-blk.c +0
-0@@ -262,6 +274,7 @@ void virtio_blk_data_plane_stop(VirtIOBlockDataPlane *s)/* Stop notifications for new requests from guest */ virtio_queue_aio_set_host_notifier_handler (s->vq, s->ctx, false, false);
I think that this should have been ", true, false" even before your patch; I'd prefer to fix it even if the problem is latent.
The patch looks good, and it might even be an API improvement independent of Conny's patches. The reentrancy _is_ hard to understand, and eliminating it makes the new API not just a hack.
In that case I would unify the new function with virtio_queue_aio_set_host_notifier_handler. In other words
- virtio_queue_aio_set_host_notifier_handler(vq, ctx, NULL) is the same as
virtio_set_queue_aio(s->vq, NULL);
virtio_queue_aio_set_host_notifier_handler(vq, ctx, true, false);- virtio_queue_aio_set_host_notifier_handler(vq, ctx, fn) is the same as
virtio_queue_aio_set_host_notifier_handler(vq, ctx, true, true);
virtio_set_queue_aio(vq, fn);Thanks,
Paolo
Quoted 4 lines
hw/block/dataplane/virtio-blk.c +1
-0+virtio_set_queue_aio (s->vq,NULL );/* Drain and switch bs back to the QEMU main loop */ blk_set_aio_context (s->conf->conf.blk,qemu_get_aio_context ());
Re: [Qemu-devel] [PATCH 2/2] virtio-blk: use aio handler for data plane
Quoted 10 lines · Paolo Bonzini
hw/block/dataplane/virtio-blk.c void virtio_blk_data_plane_stop(VirtIOBlockDataPlane *s) · line 276
Quoted 4 lines · Michael S. Tsirkin
hw/block/dataplane/virtio-blk.c +0
-0@@ -262,6 +274,7 @@ void virtio_blk_data_plane_stop(VirtIOBlockDataPlane *s)/* Stop notifications for new requests from guest */ virtio_queue_aio_set_host_notifier_handler (s->vq, s->ctx, false, false);I think that this should have been ", true, false" even before your patch; I'd prefer to fix it even if the problem is latent.
Makes sense - post a patch?
Quoted 13 lines
The patch looks good, and it might even be an API improvement independent of Conny's patches. The reentrancy _is_ hard to understand, and eliminating it makes the new API not just a hack.
In that case I would unify the new function with
virtio_queue_aio_set_host_notifier_handler. In other words-
virtio_queue_aio_set_host_notifier_handler(vq, ctx, NULL)is the same asvirtio_set_queue_aio(s->vq, NULL); virtio_queue_aio_set_host_notifier_handler(vq, ctx, true, false);
Quoted 8 lines
-
virtio_queue_aio_set_host_notifier_handler(vq, ctx, fn)is the same asvirtio_queue_aio_set_host_notifier_handler(vq, ctx, true, true); virtio_set_queue_aio(vq, fn);Thanks,
Paolo
In that case, we'll have to also change scsi to use the new API. A bit more work, to be sure.
Does scsi have the same problem as blk?
Quoted 4 lines
Quoted 4 lines
hw/block/dataplane/virtio-blk.c +1
-0+virtio_set_queue_aio (s->vq,NULL );/* Drain and switch bs back to the QEMU main loop */ blk_set_aio_context (s->conf->conf.blk,qemu_get_aio_context ());
Re: [Qemu-devel] [PATCH 2/2] virtio-blk: use aio handler for data plane
Quoted 3 lines · Michael S. Tsirkin
In that case, we'll have to also change scsi to use the new API. A bit more work, to be sure.
Does scsi have the same problem as blk?
Yes. The bug is in the virtio core.
Paolo
Re: [Qemu-devel] [PATCH 2/2] virtio-blk: use aio handler for data plane
In reply to Michael S. Tsirkinhw/block/virtio-blk.c static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq) · line 612
Quoted 11 lines · Michael S. Tsirkin
hw/block/virtio-blk.c +11
-0+if (s->dataplane) { +/* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start +* dataplane here instead of waiting for .set_status(). +*/ +if (!s->dataplane_started) { +virtio_blk_data_plane_start (s->dataplane); + } +return ; + } + +virtio_blk_handle_vq (s, vq);
Another small comment, this can be written simply as
if (s->dataplane) {
virtio_blk_data_plane_start(s->dataplane);
} else {
virtio_blk_handle_vq(s, vq);
}Paolo
Re: [Qemu-devel] [PATCH 2/2] virtio-blk: use aio handler for data plane
Quoted 15 lines · Paolo Bonzini
hw/block/virtio-blk.c static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq) · line 612
Quoted 11 lines · Michael S. Tsirkin
hw/block/virtio-blk.c +11
-0+if (s->dataplane) { +/* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start +* dataplane here instead of waiting for .set_status(). +*/ +if (!s->dataplane_started) { +virtio_blk_data_plane_start (s->dataplane); + } +return ; + } + +virtio_blk_handle_vq (s, vq);
Quoted 4 lines
Another small comment, this can be written simply as
if (s->dataplane) { virtio_blk_data_plane_start(s->dataplane);
True, it's best not to poke at dataplane_started.
Quoted 4 lines
} else {
virtio_blk_handle_vq(s, vq);
}I prefer the return style I think, to stress the
fact that this is an unusual, unexpected case.
Quoted 1 line
Paolo
Re: [Qemu-devel] [PATCH 1/2] virtio: add aio handler
In reply to Michael S. Tsirkinhw/virtio/virtio.c void virtio_queue_set_align(VirtIODevice *vdev, int n, int align) · line 1099
Quoted 10 lines · Michael S. Tsirkin
hw/virtio/virtio.c +10
-0+void virtio_queue_notify_aio_vq (VirtQueue *vq) +{ +if (vq->vring.desc && vq->handle_aio_output) { +VirtIODevice *vdev = vq->vdev; + +trace_virtio_queue_notify (vdev, vq - vdev->vq, vq); + vq->handle_aio_output (vdev, vq); + } +} +
This can be static, and virtio_queue_notify_vq probably should too.
Paolo
Re: [Qemu-devel] [PATCH 2/2] virtio-blk: use aio handler for data plane
In reply to Michael S. TsirkinQuoted 7 lines · Michael S. Tsirkin
Quoted 4 lines
Quoted 4 lines
Another small comment, this can be written simply as
if (s->dataplane) { virtio_blk_data_plane_start(s->dataplane);True, it's best not to poke at
dataplane_started.
Quoted 7 lines
Quoted 4 lines
} else { virtio_blk_handle_vq(s, vq); }I prefer the return style I think, to stress the
fact that this is an unusual, unexpected case.
We're getting dangerously close to personal preference, but I've noticed virtio-scsi that you get a very common pattern of:
void virtio_scsi_handle_ctrl_vq(VirtIOSCSI *s, VirtQueue *vq)
{
... virtqueue_pop ...
}static void virtio_scsi_data_plane_handle_ctrl(VirtIODevice *vdev,
VirtQueue *vq)
{
VirtIOSCSI *s = VIRTIO_SCSI(vdev);
assert(dataplane active and started);
virtio_scsi_handle_ctrl_vq(s, vq);
}static void virtio_scsi_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
{
VirtIOSCSI *s = VIRTIO_SCSI(vdev);
if (dataplane active) {
virtio_scsi_dataplane_start(s);
} else {
virtio_scsi_handle_ctrl_vq(s, vq);
}
}so it's not really an unusual, unexpected case but a complete separation between the dataplane case (handle_output starts dataplane) and the non-dataplane case (handle_output just does a cast and calls the actual workhorse).
Paolo
Quoted 14 lines · On Tue, 29 Mar 2016 16:42:09 +0300 "Michael S. Tsirkin" <[email protected]> wrote:
This works around races that data plane introduces simply by exiting immediately if we detect that dataplane is active.
It's a small but ugly patch, it's only justification is that it's minimally intrusive, and that it clearly has no chance to break non data plane users.
The idea is to rework it all post 2.6.
Michael S. Tsirkin (2):
virtio: add aio handler
virtio-blk: use aio handler for data plane
Quoted 7 lines
include/hw/virtio/virtio-blk.h | 2 ++ include/hw/virtio/virtio.h | 4 ++++ hw/block/dataplane/virtio-blk.c | 13 +++++++++++++ hw/block/virtio-blk.c | 28 ++++++++++++++++++---------- hw/virtio/virtio.c | 36 ++++++++++++++++++++++++++++++++---- 5 files changed, 69 insertions(+), 14 deletions(-)
FWIW, this seems to survive an hour or so of reboot loops for me, but that's only a small setup.
Quoted 14 lines · Michael S. Tsirkin
This works around races that data plane introduces simply by exiting immediately if we detect that dataplane is active.
It's a small but ugly patch, it's only justification is that it's minimally intrusive, and that it clearly has no chance to break non data plane users.
The idea is to rework it all post 2.6.
Michael S. Tsirkin (2):
virtio: add aio handler
virtio-blk: use aio handler for data plane
Quoted 7 lines
include/hw/virtio/virtio-blk.h | 2 ++ include/hw/virtio/virtio.h | 4 ++++ hw/block/dataplane/virtio-blk.c | 13 +++++++++++++ hw/block/virtio-blk.c | 28 ++++++++++++++++++---------- hw/virtio/virtio.c | 36 ++++++++++++++++++++++++++++++++---- 5 files changed, 69 insertions(+), 14 deletions(-)
This also seems to help on my setup.Tu Bo, would be good if you can double check this patch set as well on your setup?
Christian
Hi Christian:
Quoted 23 lines · Christian Borntraeger
Quoted 14 lines · Michael S. Tsirkin
This works around races that data plane introduces simply by exiting immediately if we detect that dataplane is active.
It's a small but ugly patch, it's only justification is that it's minimally intrusive, and that it clearly has no chance to break non data plane users.
The idea is to rework it all post 2.6.
Michael S. Tsirkin (2):
virtio: add aio handler
virtio-blk: use aio handler for data planeQuoted 7 lines
include/hw/virtio/virtio-blk.h | 2 ++ include/hw/virtio/virtio.h | 4 ++++ hw/block/dataplane/virtio-blk.c | 13 +++++++++++++ hw/block/virtio-blk.c | 28 ++++++++++++++++++---------- hw/virtio/virtio.c | 36 ++++++++++++++++++++++++++++++++---- 5 files changed, 69 insertions(+), 14 deletions(-)
Quoted 2 lines
This also seems to help on my setup.Tu Bo, would be good if you can double check this patch set as well on your setup?
With qemu master + [PATCH 0/2] dataplane: fix start/stop races,
I did NOT see any crash, result is good in my box. thanks
Quoted 4 lines
Christian
Quoted 27 lines · tu bo
Hi Christian:
Quoted 24 lines · Christian Borntraeger
Quoted 22 lines · Michael S. Tsirkin
This works around races that data plane introduces simply by exiting immediately if we detect that dataplane is active.
It's a small but ugly patch, it's only justification is that it's minimally intrusive, and that it clearly has no chance to break non data plane users.
The idea is to rework it all post 2.6.
Michael S. Tsirkin (2):
virtio: add aio handler
virtio-blk: use aio handler for data planeinclude/hw/virtio/virtio-blk.h | 2 ++ include/hw/virtio/virtio.h | 4 ++++ hw/block/dataplane/virtio-blk.c | 13 +++++++++++++ hw/block/virtio-blk.c | 28 ++++++++++++++++++---------- hw/virtio/virtio.c | 36 ++++++++++++++++++++++++++++++++---- 5 files changed, 69 insertions(+), 14 deletions(-)
Quoted 6 lines
Quoted 2 lines
This also seems to help on my setup.Tu Bo, would be good if you can double check this patch set as well on your setup?
With qemu master + [PATCH 0/2] dataplane: fix start/stop races,
I did NOT see any crash, result is good in my box. thanks
Great, I will send a revised version of Michael's patches with virtio-scsi too.
Paolo