All of lore.kernel.org
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: "Michael S. Tsirkin" <mst@redhat.com>
Cc: aik@ozlabs.ru, aliguori@us.ibm.com, rusty@rustcorp.com.au,
	qemu-devel@nongnu.org, agraf@suse.de
Subject: Re: [Qemu-devel] [PATCH] virtio: Make memory barriers be memory barriers
Date: Mon, 05 Sep 2011 09:41:19 +0200	[thread overview]
Message-ID: <4E647D1F.6050307@redhat.com> (raw)
In-Reply-To: <20110904091643.GA20795@redhat.com>

On 09/04/2011 11:16 AM, Michael S. Tsirkin wrote:
>> >  I mean argue for a richer set of barriers, with per-arch minimal
>> >  implementations instead of the large but portable hammer of
>> >  sync_synchronize, if you will.
>
> That's what I'm saying really. On x86 the richer set of barriers
> need not insert code at all for both wmb and rmb macros. All we
> might need is an 'optimization barrier'- e.g. linux does
>   __asm__ __volatile__("": : :"memory")
> ppc needs something like sync_synchronize there.

No, rmb and wmb need to generate code.  You are right that in some 
places there will be some extra barriers.

If you want a richer set of barriers, that must be something like 
{rr,rw,wr,ww}_mb{_acq,_rel,} (again not counting the Alpha).  On x86, 
then, all the rr/rw/ww barriers will be compiler barriers because the 
hardware already enforces ordering.  The other three map to 
lfence/sfence/mfence:

    barrier     assembly  why?
    ---------------------------------------------------------------------
    wr_mb_acq   lfence    prevents the read from moving up -> acquire
    wr_mb_rel   sfence    prevents the write from moving down -> release
    wr_mb       mfence    (full barrier)

But if you stick to rmb/wmb/mb, then the correct definition of rmb is 
"the least strict barrier that provides all three of rr_mb(), 
rw_mb_rel() and wr_mb_acq()".  This is, as expected, an lfence. 
Similarly, wmb must provide all three of ww_mb(), wr_mb_rel() and 
rw_mb_acq(), and this is an sfence.

So the right place to put an #ifdef is not "wmb()", but the _uses_ of 
wmb() where you know you need a barrier that is less strict.  That's why 
I say David patch is correct; on top of that you may change the 
particular uses of wmb() in virtio.c to compiler barriers, for example 
when you only care about ordering writes after writes.

Likewise, there may even be places in which you could #ifdef out a full 
memory barrier.  For example, if you only care about ordering writes 
with respect to reads, x86 hardware is already providing that and you 
could omit the mb().

I think in general it is premature optimization, though.

Regarding specific examples in virtio where lfence and sfence could be 
used, there may be one when using event signaling.  In the backend you 
write first the index of your response, then you check whether to 
generate an event.  (I think) the following requirements hold:

* if you read the event-index too early, you might skip an event and 
deadlock.  So you need at least a read barrier.

* you can write the response-index after reading the event-index, as 
long as you write it before waking up the guest.

So, in that case an x86 lfence should be enough, though again without 
more consideration I would use a full barrier just to be sure.

Paolo

  parent reply	other threads:[~2011-09-05  7:41 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-01  6:09 [Qemu-devel] [PATCH] virtio: Make memory barriers be memory barriers David Gibson
2011-09-01  6:47 ` Paolo Bonzini
2011-09-02  0:08   ` David Gibson
2011-09-02  6:49     ` Paolo Bonzini
2011-09-03 11:53       ` Blue Swirl
2011-09-01  7:37 ` Sasha Levin
2011-09-01  7:38   ` Sasha Levin
2011-09-01  7:56   ` Paolo Bonzini
2011-09-02  0:09     ` David Gibson
2011-09-01 15:30 ` Michael S. Tsirkin
2011-09-01 16:14   ` Paolo Bonzini
2011-09-01 16:34     ` Michael S. Tsirkin
2011-09-01 20:31       ` Paolo Bonzini
2011-09-02 15:45         ` Michael S. Tsirkin
2011-09-03 14:46           ` David Gibson
2011-09-04  9:16             ` Michael S. Tsirkin
2011-09-05  4:43               ` David Gibson
2011-09-05  9:19                 ` Michael S. Tsirkin
2011-09-06  3:12                   ` David Gibson
2011-09-06  6:55                     ` Paolo Bonzini
2011-09-06  9:02                       ` David Gibson
2011-09-06  9:28                       ` Avi Kivity
2011-09-06  9:35                         ` Michael S. Tsirkin
2011-09-06  9:38                         ` Paolo Bonzini
2011-09-05  7:41               ` Paolo Bonzini [this message]
2011-09-05  8:06                 ` Michael S. Tsirkin
2011-09-05  9:42                   ` Paolo Bonzini
2011-09-03 16:19           ` Paolo Bonzini
2011-09-04  8:47             ` Michael S. Tsirkin
2011-09-02  0:11     ` David Gibson
2011-09-02  6:11       ` Paolo Bonzini
2011-09-02 15:57         ` Michael S. Tsirkin

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=4E647D1F.6050307@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=agraf@suse.de \
    --cc=aik@ozlabs.ru \
    --cc=aliguori@us.ibm.com \
    --cc=mst@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=rusty@rustcorp.com.au \
    /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.