All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stefan Priebe - Profihost AG <s.priebe@profihost.ag>
To: Stefan Hajnoczi <stefanha@redhat.com>, qemu-devel@nongnu.org
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	Ping Fan Liu <pingfank@linux.vnet.ibm.com>,
	qemu-stable@nongnu.org
Subject: Re: [Qemu-devel] [PATCH] aio: fix qemu_bh_schedule() bh->ctx race condition
Date: Tue, 03 Jun 2014 14:52:40 +0200	[thread overview]
Message-ID: <538DC518.8080007@profihost.ag> (raw)
In-Reply-To: <1401787261-30166-1-git-send-email-stefanha@redhat.com>

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);
>  }
>  
>  
> 

  parent reply	other threads:[~2014-06-03 12:52 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2014-06-04  7:55   ` Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=538DC518.8080007@profihost.ag \
    --to=s.priebe@profihost.ag \
    --cc=pbonzini@redhat.com \
    --cc=pingfank@linux.vnet.ibm.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-stable@nongnu.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.