[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-block] [PATCH v3 2/4] virtio-blk: fix race between .ioeventfd_stop
From: |
Stefan Hajnoczi |
Subject: |
[Qemu-block] [PATCH v3 2/4] virtio-blk: fix race between .ioeventfd_stop() and vq handler |
Date: |
Wed, 7 Mar 2018 14:42:03 +0000 |
If the main loop thread invokes .ioeventfd_stop() just as the vq handler
function begins in the IOThread then the handler may lose the race for
the AioContext lock. By the time the vq handler is able to acquire the
AioContext lock the ioeventfd has already been removed and the handler
isn't supposed to run anymore!
Use the new aio_wait_bh_oneshot() function to perform ioeventfd removal
from within the IOThread. This way no races with the vq handler are
possible.
Signed-off-by: Stefan Hajnoczi <address@hidden>
---
hw/block/dataplane/virtio-blk.c | 24 +++++++++++++++++-------
1 file changed, 17 insertions(+), 7 deletions(-)
diff --git a/hw/block/dataplane/virtio-blk.c b/hw/block/dataplane/virtio-blk.c
index 2cb990997e..d3bb09bc4e 100644
--- a/hw/block/dataplane/virtio-blk.c
+++ b/hw/block/dataplane/virtio-blk.c
@@ -229,6 +229,22 @@ int virtio_blk_data_plane_start(VirtIODevice *vdev)
return -ENOSYS;
}
+/* Stop notifications for new requests from guest.
+ *
+ * Context: BH in IOThread
+ */
+static void virtio_blk_data_plane_stop_bh(void *opaque)
+{
+ VirtIOBlockDataPlane *s = opaque;
+ unsigned i;
+
+ for (i = 0; i < s->conf->num_queues; i++) {
+ VirtQueue *vq = virtio_get_queue(s->vdev, i);
+
+ virtio_queue_aio_set_host_notifier_handler(vq, s->ctx, NULL);
+ }
+}
+
/* Context: QEMU global mutex held */
void virtio_blk_data_plane_stop(VirtIODevice *vdev)
{
@@ -253,13 +269,7 @@ void virtio_blk_data_plane_stop(VirtIODevice *vdev)
trace_virtio_blk_data_plane_stop(s);
aio_context_acquire(s->ctx);
-
- /* Stop notifications for new requests from guest */
- for (i = 0; i < nvqs; i++) {
- VirtQueue *vq = virtio_get_queue(s->vdev, i);
-
- virtio_queue_aio_set_host_notifier_handler(vq, s->ctx, NULL);
- }
+ aio_wait_bh_oneshot(s->ctx, virtio_blk_data_plane_stop_bh, s);
/* Drain and switch bs back to the QEMU main loop */
blk_set_aio_context(s->conf->conf.blk, qemu_get_aio_context());
--
2.14.3
- [Qemu-block] [PATCH v3 0/4] vl: introduce vm_shutdown(), Stefan Hajnoczi, 2018/03/07
- [Qemu-block] [PATCH v3 1/4] block: add aio_wait_bh_oneshot(), Stefan Hajnoczi, 2018/03/07
- [Qemu-block] [PATCH v3 2/4] virtio-blk: fix race between .ioeventfd_stop() and vq handler,
Stefan Hajnoczi <=
- [Qemu-block] [PATCH v3 4/4] vl: introduce vm_shutdown(), Stefan Hajnoczi, 2018/03/07
- [Qemu-block] [PATCH v3 3/4] virtio-scsi: fix race between .ioeventfd_stop() and vq handler, Stefan Hajnoczi, 2018/03/07
- Re: [Qemu-block] [PATCH v3 0/4] vl: introduce vm_shutdown(), Paolo Bonzini, 2018/03/07
- Re: [Qemu-block] [PATCH v3 0/4] vl: introduce vm_shutdown(), Fam Zheng, 2018/03/07
- Re: [Qemu-block] [PATCH v3 0/4] vl: introduce vm_shutdown(), Michael S. Tsirkin, 2018/03/08
- Re: [Qemu-block] [PATCH v3 0/4] vl: introduce vm_shutdown(), Stefan Hajnoczi, 2018/03/08