958e1b
From 068000dc97228f85b878634e3d49d3354f5cbafe Mon Sep 17 00:00:00 2001
958e1b
From: John Snow <jsnow@redhat.com>
958e1b
Date: Fri, 11 Jul 2014 19:07:24 -0500
958e1b
Subject: [CHANGE 20/29] aio: fix qemu_bh_schedule() bh->ctx race condition
958e1b
To: rhvirt-patches@redhat.com,
958e1b
    jen@redhat.com
958e1b
958e1b
RH-Author: John Snow <jsnow@redhat.com>
958e1b
Message-id: <1405105644-21039-1-git-send-email-jsnow@redhat.com>
958e1b
Patchwork-id: 59866
958e1b
O-Subject: [RHEL-7.1 qemu-kvm PATCH] aio: fix qemu_bh_schedule() bh->ctx race condition
958e1b
Bugzilla: 1116728
958e1b
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
958e1b
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
958e1b
RH-Acked-by: Fam Zheng <famz@redhat.com>
958e1b
958e1b
From: Stefan Hajnoczi <stefanha@redhat.com>
958e1b
958e1b
bz: https://bugzilla.redhat.com/show_bug.cgi?id=1116728
958e1b
brew: https://brewweb.devel.redhat.com/taskinfo?taskID=7690195
958e1b
upstream: 924fe1293c3e7a3c787bbdfb351e7f168caee3e9
958e1b
958e1b
qemu_bh_schedule() is supposed to be thread-safe at least the first time
958e1b
it is called.  Unfortunately this is not quite true:
958e1b
958e1b
  bh->scheduled = 1;
958e1b
  aio_notify(bh->ctx);
958e1b
958e1b
Since another thread may run the BH callback once it has been scheduled,
958e1b
there is a race condition if the callback frees the BH before
958e1b
aio_notify(bh->ctx) has a chance to run.
958e1b
958e1b
Reported-by: Stefan Priebe <s.priebe@profihost.ag>
958e1b
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
958e1b
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
958e1b
Tested-by: Stefan Priebe <s.priebe@profihost.ag>
958e1b
(cherry picked from commit 924fe1293c3e7a3c787bbdfb351e7f168caee3e9)
958e1b
Signed-off-by: John Snow <jsnow@redhat.com>
958e1b
---
958e1b
 async.c | 14 ++++++++++----
958e1b
 1 file changed, 10 insertions(+), 4 deletions(-)
958e1b
958e1b
Signed-off-by: jen <jen@redhat.com>
958e1b
---
958e1b
 async.c | 14 ++++++++++----
958e1b
 1 file changed, 10 insertions(+), 4 deletions(-)
958e1b
958e1b
diff --git a/async.c b/async.c
958e1b
index 5ce3633..d7ec1ea 100644
958e1b
--- a/async.c
958e1b
+++ b/async.c
958e1b
@@ -117,15 +117,21 @@ void qemu_bh_schedule_idle(QEMUBH *bh)
958e1b
 
958e1b
 void qemu_bh_schedule(QEMUBH *bh)
958e1b
 {
958e1b
+    AioContext *ctx;
958e1b
+
958e1b
     if (bh->scheduled)
958e1b
         return;
958e1b
+    ctx = bh->ctx;
958e1b
     bh->idle = 0;
958e1b
-    /* Make sure that idle & any writes needed by the callback are done
958e1b
-     * before the locations are read in the aio_bh_poll.
958e1b
+    /* Make sure that:
958e1b
+     * 1. idle & any writes needed by the callback are done before the
958e1b
+     *    locations are read in the aio_bh_poll.
958e1b
+     * 2. ctx is loaded before scheduled is set and the callback has a chance
958e1b
+     *    to execute.
958e1b
      */
958e1b
-    smp_wmb();
958e1b
+    smp_mb();
958e1b
     bh->scheduled = 1;
958e1b
-    aio_notify(bh->ctx);
958e1b
+    aio_notify(ctx);
958e1b
 }
958e1b
 
958e1b
 
958e1b
-- 
958e1b
1.9.3
958e1b