* [Qemu-devel] [PATCH] aio: fix qemu_bh_schedule() bh->ctx race condition
@ 2014-06-03 9:21 Stefan Hajnoczi
2014-06-03 11:12 ` Paolo Bonzini
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Stefan Hajnoczi @ 2014-06-03 9:21 UTC (permalink / raw)
To: qemu-devel
Cc: Paolo Bonzini, Ping Fan Liu, qemu-stable, Stefan Hajnoczi,
Stefan Priebe
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 <s.priebe@profihost.ag>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
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
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] [PATCH] aio: fix qemu_bh_schedule() bh->ctx race condition
2014-06-03 9:21 [Qemu-devel] [PATCH] aio: fix qemu_bh_schedule() bh->ctx race condition Stefan Hajnoczi
@ 2014-06-03 11:12 ` Paolo Bonzini
2014-06-03 12:49 ` Stefan Hajnoczi
2014-06-03 12:52 ` Stefan Priebe - Profihost AG
2 siblings, 0 replies; 5+ messages in thread
From: Paolo Bonzini @ 2014-06-03 11:12 UTC (permalink / raw)
To: Stefan Hajnoczi, qemu-devel; +Cc: Ping Fan Liu, qemu-stable, Stefan Priebe
Il 03/06/2014 11:21, Stefan Hajnoczi ha scritto:
> 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 <s.priebe@profihost.ag>
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> ---
> 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);
> }
>
>
>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] [PATCH] aio: fix qemu_bh_schedule() bh->ctx race condition
2014-06-03 9:21 [Qemu-devel] [PATCH] aio: fix qemu_bh_schedule() bh->ctx race condition Stefan Hajnoczi
2014-06-03 11:12 ` Paolo Bonzini
@ 2014-06-03 12:49 ` Stefan Hajnoczi
2014-06-03 12:52 ` Stefan Priebe - Profihost AG
2 siblings, 0 replies; 5+ messages in thread
From: Stefan Hajnoczi @ 2014-06-03 12:49 UTC (permalink / raw)
To: Stefan Hajnoczi
Cc: Paolo Bonzini, Stefan Priebe, Ping Fan Liu, qemu-devel,
qemu-stable
On Tue, Jun 03, 2014 at 11:21:01AM +0200, Stefan Hajnoczi wrote:
> 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 <s.priebe@profihost.ag>
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> ---
> async.c | 14 ++++++++++----
> 1 file changed, 10 insertions(+), 4 deletions(-)
Applied to my block tree:
https://github.com/stefanha/qemu/commits/block
Stefan
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] [PATCH] aio: fix qemu_bh_schedule() bh->ctx race condition
2014-06-03 9:21 [Qemu-devel] [PATCH] aio: fix qemu_bh_schedule() bh->ctx race condition Stefan Hajnoczi
2014-06-03 11:12 ` Paolo Bonzini
2014-06-03 12:49 ` Stefan Hajnoczi
@ 2014-06-03 12:52 ` Stefan Priebe - Profihost AG
2014-06-04 7:55 ` Stefan Hajnoczi
2 siblings, 1 reply; 5+ messages in thread
From: Stefan Priebe - Profihost AG @ 2014-06-03 12:52 UTC (permalink / raw)
To: Stefan Hajnoczi, qemu-devel; +Cc: Paolo Bonzini, Ping Fan Liu, qemu-stable
Tested-by: Stefan Priebe <s.priebe@profihost.ag>
Am 03.06.2014 11:21, schrieb 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.
>
> Reported-by: Stefan Priebe <s.priebe@profihost.ag>
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> ---
> 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);
> }
>
>
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] [PATCH] aio: fix qemu_bh_schedule() bh->ctx race condition
2014-06-03 12:52 ` Stefan Priebe - Profihost AG
@ 2014-06-04 7:55 ` Stefan Hajnoczi
0 siblings, 0 replies; 5+ messages in thread
From: Stefan Hajnoczi @ 2014-06-04 7:55 UTC (permalink / raw)
To: Stefan Priebe - Profihost AG
Cc: Paolo Bonzini, Ping Fan Liu, qemu-devel, Stefan Hajnoczi,
qemu-stable
On Tue, Jun 03, 2014 at 02:52:40PM +0200, Stefan Priebe - Profihost AG wrote:
> Tested-by: Stefan Priebe <s.priebe@profihost.ag>
Thanks!
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2014-06-04 7:55 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-06-03 9:21 [Qemu-devel] [PATCH] aio: fix qemu_bh_schedule() bh->ctx race condition Stefan Hajnoczi
2014-06-03 11:12 ` Paolo Bonzini
2014-06-03 12:49 ` Stefan Hajnoczi
2014-06-03 12:52 ` Stefan Priebe - Profihost AG
2014-06-04 7:55 ` Stefan Hajnoczi
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).