Skip to contentSkip to conversation

aio: fix qemu_bh_schedule() bh->ctx race condition

PATCH Applied Stefan Hajnoczi 4 participants 5 messages Jun 3, 2014 – Jun 4, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 aio: fix qemu_bh_schedule() bh->ctx race condition

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.

---
 async.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
async.c +10 -4
index 6930185..5b6fe6b 100644
@@ -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);
 }
 
 
Signature · 1 line
1.9.3
Reviewed-by on v1 1/1
#1 lore raw

Il 03/06/2014 11:21, Stefan Hajnoczi ha scritto:

Quoted 37 lines

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.

---
 async.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
async.c +7 -2
index 6930185..5b6fe6b 100644
@@ -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

async.c void qemu_bh_schedule_idle(QEMUBH *bh) · line 137

Quoted 10 lines
async.c +3 -2
+     *    to execute.
      */
-    smp_wmb();
+    smp_mb();
     bh->scheduled = 1;
-    aio_notify(bh->ctx);
+    aio_notify(ctx);
 }
 
 
#2 lore raw
In reply to Stefan Hajnoczi
Quoted 10 lines · Stefan Hajnoczi

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.

Quoted 5 lines
---
 async.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

Applied to my block tree:
https://github.com/stefanha/qemu/commits/block

Stefan

Tested-by on v1 1/1
#3 lore raw
In reply to Stefan Hajnoczi

Am 03.06.2014 11:21, schrieb Stefan Hajnoczi:

Quoted 47 lines

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.

---
 async.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
async.c +10 -4
index 6930185..5b6fe6b 100644
@@ -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);
 }