Blame SOURCES/kvm-aio-fix-qemu_bh_schedule-bh-ctx-race-condition.patch

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