[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 01/42] aio: fix qemu_bh_schedule() bh->ctx race condi
From: |
Stefan Hajnoczi |
Subject: |
[Qemu-devel] [PULL 01/42] aio: fix qemu_bh_schedule() bh->ctx race condition |
Date: |
Fri, 6 Jun 2014 18:13:22 +0200 |
qemu_bh_schedule() is supposed to be thread-safe at least the first time
it is called. Unfortunately this is not quite true:
bh->scheduled = 1;
aio_notify(bh->ctx);
Since another thread may run the BH callback once it has been scheduled,
there is a race condition if the callback frees the BH before
aio_notify(bh->ctx) has a chance to run.
Reported-by: Stefan Priebe <address@hidden>
Signed-off-by: Stefan Hajnoczi <address@hidden>
Reviewed-by: Paolo Bonzini <address@hidden>
Tested-by: Stefan Priebe <address@hidden>
---
async.c | 14 ++++++++++----
1 file changed, 10 insertions(+), 4 deletions(-)
diff --git a/async.c b/async.c
index 6930185..5b6fe6b 100644
--- a/async.c
+++ b/async.c
@@ -117,15 +117,21 @@ void qemu_bh_schedule_idle(QEMUBH *bh)
void qemu_bh_schedule(QEMUBH *bh)
{
+ AioContext *ctx;
+
if (bh->scheduled)
return;
+ ctx = bh->ctx;
bh->idle = 0;
- /* Make sure that idle & any writes needed by the callback are done
- * before the locations are read in the aio_bh_poll.
+ /* Make sure that:
+ * 1. idle & any writes needed by the callback are done before the
+ * locations are read in the aio_bh_poll.
+ * 2. ctx is loaded before scheduled is set and the callback has a chance
+ * to execute.
*/
- smp_wmb();
+ smp_mb();
bh->scheduled = 1;
- aio_notify(bh->ctx);
+ aio_notify(ctx);
}
--
1.9.3
- [Qemu-devel] [PULL 00/42] Block patches, Stefan Hajnoczi, 2014/06/06
- [Qemu-devel] [PULL 01/42] aio: fix qemu_bh_schedule() bh->ctx race condition,
Stefan Hajnoczi <=
- [Qemu-devel] [PULL 03/42] block: acquire AioContext in bdrv_*_all(), Stefan Hajnoczi, 2014/06/06
- [Qemu-devel] [PULL 02/42] block: use BlockDriverState AioContext, Stefan Hajnoczi, 2014/06/06
- [Qemu-devel] [PULL 04/42] block: acquire AioContext in bdrv_drain_all(), Stefan Hajnoczi, 2014/06/06
- [Qemu-devel] [PULL 06/42] blkdebug: use BlockDriverState's AioContext, Stefan Hajnoczi, 2014/06/06
- [Qemu-devel] [PULL 08/42] curl: implement .bdrv_detach/attach_aio_context(), Stefan Hajnoczi, 2014/06/06
- [Qemu-devel] [PULL 07/42] blkverify: implement .bdrv_detach/attach_aio_context(), Stefan Hajnoczi, 2014/06/06
- [Qemu-devel] [PULL 05/42] block: add bdrv_set_aio_context(), Stefan Hajnoczi, 2014/06/06
- [Qemu-devel] [PULL 09/42] gluster: use BlockDriverState's AioContext, Stefan Hajnoczi, 2014/06/06
- [Qemu-devel] [PULL 11/42] nbd: implement .bdrv_detach/attach_aio_context(), Stefan Hajnoczi, 2014/06/06
- [Qemu-devel] [PULL 10/42] iscsi: implement .bdrv_detach/attach_aio_context(), Stefan Hajnoczi, 2014/06/06