All of lore.kernel.org
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: Pranith Kumar <bobby.prani@gmail.com>
Cc: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] aio: reg. smp_read_barrier_depends() in aio_bh_poll()
Date: Mon, 5 Sep 2016 13:31:45 +0200	[thread overview]
Message-ID: <ea7ab846-ffc6-cbd2-07f9-abf35c770467@redhat.com> (raw)
In-Reply-To: <87r3926u0f.fsf@gmail.com>



On 02/09/2016 20:23, Pranith Kumar wrote:
> If I understand you correctly, this is what might happen without the
> barrier():
> 
>             P0                        P1
>       ----------------------------------------
>       // bh = ctx->first_bh;  optimized       
>       if (ctx->first_bh) {
>       // next = ctx->first_bh->next;
>                                    lock(bh_lock);
>                                    new_bh->next = ctx->first_bh;
>                                    smp_wmb();  // this alone is not sufficient
>                                                // for Alpha
>                                    ctx->first_bh = new_bh;
>                                    unlock(bh_lock);
>       // bh = next;
>       bh = ctx->first_bh->next;
> 
>       if (bh) {do something}
>       }
> 
> Is this what might happen? If so, inserting a barrier() after the first load
> into bh will prevent the compiler from optimizing the load into bh since the
> compiler cannot optimize away loads and stores past the barrier().

Yes, this is what you can expect from a compiler.

> And on Alpha processors barrier() should really be smp_read_barrier_depends()
> to prevent this from happening because of it's memory model(issue a barrier
> after loading a pointer to shared memory and before dereferencing it).

Yes; the actual effect you could see on the Alpha it's even crazier.
Without the barrier, the "bh = ctx->first_bh" and "next = bh->next" can
be reordered.  If you take both threads into account, "next = bh->next"
can use a value from before P1's "ctx->first = new_bh".  Indeed, it can
use a value from before P1's smp_wmb() or before new_bh->next =
ctx->first_bh.  For example, "next" could load a NULL value.

FWIW, RISCv currently has the same memory model as the Alpha, but they
plan to fix it before they finalize the relevant specs!

Paolo

>> >
>> > So instead of smp_read_barrier_depends() you could load ctx->first_bh
>> > and bh->next with the consume memory order, but you do need _something_.
> OK, if the above situation is possible, then I think I understand the need for
> this barrier.

      reply	other threads:[~2016-09-05 11:31 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-02 14:33 [Qemu-devel] aio: reg. smp_read_barrier_depends() in aio_bh_poll() Pranith Kumar
2016-09-02 15:38 ` Paolo Bonzini
2016-09-02 18:23   ` Pranith Kumar
2016-09-05 11:31     ` Paolo Bonzini [this message]

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=ea7ab846-ffc6-cbd2-07f9-abf35c770467@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=bobby.prani@gmail.com \
    --cc=qemu-devel@nongnu.org \
    /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.