From: Jason Wang <jasowang@redhat.com>
To: "Michael S. Tsirkin" <mst@redhat.com>
Cc: Andrea Parri <andrea.parri@amarulasolutions.com>,
linux-doc@vger.kernel.org, Peter Zijlstra <peterz@infradead.org>,
Akira Yokosawa <akiyks@gmail.com>,
Will Deacon <will.deacon@arm.com>,
virtualization@lists.linux-foundation.org,
David Howells <dhowells@redhat.com>,
linux-arch@vger.kernel.org, Jonathan Corbet <corbet@lwn.net>,
linux-sparse@vger.kernel.org,
Alan Stern <stern@rowland.harvard.edu>,
Matt Turner <mattst88@gmail.com>,
"Paul E. McKenney" <paulmck@linux.vnet.ibm.com>,
Daniel Lustig <dlustig@nvidia.com>, Arnd Bergmann <arnd@arndb.de>,
Boqun Feng <boqun.feng@gmail.com>,
Nicholas Piggin <npiggin@gmail.com>,
Ivan Kokshaysky <ink@jurassic.park.msu.ru>,
Luc Maranget <luc.maranget@inria.fr>,
Richard Henderson <rth@twiddle.net>,
Jade Alglave <j.alglave@ucl.ac.uk>,
netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-alpha@vger.kernel.orgLuc Van Oostenryck <luc>
Subject: Re: [PATCH RFC 3/4] barriers: convert a control to a data dependency
Date: Mon, 7 Jan 2019 14:50:58 +0800 [thread overview]
Message-ID: <155183f3-d03c-5e79-c267-974c502ff5d7@redhat.com> (raw)
In-Reply-To: <20190106231756-mutt-send-email-mst@kernel.org>
On 2019/1/7 下午12:23, Michael S. Tsirkin wrote:
> On Mon, Jan 07, 2019 at 11:58:23AM +0800, Jason Wang wrote:
>> On 2019/1/3 上午4:57, Michael S. Tsirkin wrote:
>>> It's not uncommon to have two access two unrelated memory locations in a
>>> specific order. At the moment one has to use a memory barrier for this.
>>>
>>> However, if the first access was a read and the second used an address
>>> depending on the first one we would have a data dependency and no
>>> barrier would be necessary.
>>>
>>> This adds a new interface: dependent_ptr_mb which does exactly this: it
>>> returns a pointer with a data dependency on the supplied value.
>>>
>>> Signed-off-by: Michael S. Tsirkin<mst@redhat.com>
>>> ---
>>> Documentation/memory-barriers.txt | 20 ++++++++++++++++++++
>>> arch/alpha/include/asm/barrier.h | 1 +
>>> include/asm-generic/barrier.h | 18 ++++++++++++++++++
>>> include/linux/compiler.h | 4 ++++
>>> 4 files changed, 43 insertions(+)
>>>
>>> diff --git a/Documentation/memory-barriers.txt b/Documentation/memory-barriers.txt
>>> index c1d913944ad8..9dbaa2e1dbf6 100644
>>> --- a/Documentation/memory-barriers.txt
>>> +++ b/Documentation/memory-barriers.txt
>>> @@ -691,6 +691,18 @@ case what's actually required is:
>>> p = READ_ONCE(b);
>>> }
>>> +Alternatively, a control dependency can be converted to a data dependency,
>>> +e.g.:
>>> +
>>> + q = READ_ONCE(a);
>>> + if (q) {
>>> + b = dependent_ptr_mb(b, q);
>>> + p = READ_ONCE(b);
>>> + }
>>> +
>>> +Note how the result of dependent_ptr_mb must be used with the following
>>> +accesses in order to have an effect.
>>> +
>>> However, stores are not speculated. This means that ordering -is- provided
>>> for load-store control dependencies, as in the following example:
>>> @@ -836,6 +848,12 @@ out-guess your code. More generally, although READ_ONCE() does force
>>> the compiler to actually emit code for a given load, it does not force
>>> the compiler to use the results.
>>> +Converting to a data dependency helps with this too:
>>> +
>>> + q = READ_ONCE(a);
>>> + b = dependent_ptr_mb(b, q);
>>> + WRITE_ONCE(b, 1);
>>> +
>>> In addition, control dependencies apply only to the then-clause and
>>> else-clause of the if-statement in question. In particular, it does
>>> not necessarily apply to code following the if-statement:
>>> @@ -875,6 +893,8 @@ to the CPU containing it. See the section on "Multicopy atomicity"
>>> for more information.
>>> +
>>> +
>>> In summary:
>>> (*) Control dependencies can order prior loads against later stores.
>>> diff --git a/arch/alpha/include/asm/barrier.h b/arch/alpha/include/asm/barrier.h
>>> index 92ec486a4f9e..b4934e8c551b 100644
>>> --- a/arch/alpha/include/asm/barrier.h
>>> +++ b/arch/alpha/include/asm/barrier.h
>>> @@ -59,6 +59,7 @@
>>> * as Alpha, "y" could be set to 3 and "x" to 0. Use rmb()
>>> * in cases like this where there are no data dependencies.
>>> */
>>> +#define ARCH_NEEDS_READ_BARRIER_DEPENDS 1
>>> #define read_barrier_depends() __asm__ __volatile__("mb": : :"memory")
>>> #ifdef CONFIG_SMP
>>> diff --git a/include/asm-generic/barrier.h b/include/asm-generic/barrier.h
>>> index 2cafdbb9ae4c..fa2e2ef72b68 100644
>>> --- a/include/asm-generic/barrier.h
>>> +++ b/include/asm-generic/barrier.h
>>> @@ -70,6 +70,24 @@
>>> #define __smp_read_barrier_depends() read_barrier_depends()
>>> #endif
>>> +#if defined(COMPILER_HAS_OPTIMIZER_HIDE_VAR) && \
>>> + !defined(ARCH_NEEDS_READ_BARRIER_DEPENDS)
>>> +
>>> +#define dependent_ptr_mb(ptr, val) ({ \
>>> + long dependent_ptr_mb_val = (long)(val); \
>>> + long dependent_ptr_mb_ptr = (long)(ptr) - dependent_ptr_mb_val; \
>>> + \
>>> + BUILD_BUG_ON(sizeof(val) > sizeof(long)); \
>>> + OPTIMIZER_HIDE_VAR(dependent_ptr_mb_val); \
>>> + (typeof(ptr))(dependent_ptr_mb_ptr + dependent_ptr_mb_val); \
>>> +})
>>> +
>>> +#else
>>> +
>>> +#define dependent_ptr_mb(ptr, val) ({ mb(); (ptr); })
>> So for the example of patch 4, we'd better fall back to rmb() or need a
>> dependent_ptr_rmb()?
>>
>> Thanks
> You mean for strongly ordered architectures like Intel?
> Yes, maybe it makes sense to have dependent_ptr_smp_rmb,
> dependent_ptr_dma_rmb and dependent_ptr_virt_rmb.
>
> mb variant is unused right now so I'll remove it.
>
>
Yes.
Thanks
_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
next prev parent reply other threads:[~2019-01-07 6:50 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-01-02 20:57 [PATCH RFC 0/4] barriers using data dependency Michael S. Tsirkin
2019-01-02 20:57 ` [PATCH RFC 1/4] include/linux/compiler*.h: fix OPTIMIZER_HIDE_VAR Michael S. Tsirkin
2019-01-02 20:57 ` [PATCH RFC 2/4] include/linux/compiler.h: allow memory operands Michael S. Tsirkin
2019-01-02 20:57 ` [PATCH RFC 3/4] barriers: convert a control to a data dependency Michael S. Tsirkin
2019-01-02 20:58 ` [PATCH RFC 4/4] virtio: use dependent_ptr_mb Michael S. Tsirkin
[not found] ` <Pine.LNX.4.44L0.1901021629150.1375-100000@iolanthe.rowland.org>
2019-01-02 23:04 ` [PATCH RFC 0/4] barriers using data dependency Michael S. Tsirkin
[not found] ` <20190102205715.14054-4-mst@redhat.com>
2019-01-02 21:00 ` [PATCH RFC 3/4] barriers: convert a control to a " Matthew Wilcox
[not found] ` <20190102210024.GJ6310@bombadil.infradead.org>
2019-01-02 21:24 ` Michael S. Tsirkin
2019-01-07 3:58 ` Jason Wang
2019-01-07 4:23 ` Michael S. Tsirkin
2019-01-07 6:50 ` Jason Wang [this message]
2019-01-07 9:46 ` Peter Zijlstra
2019-01-07 13:36 ` Michael S. Tsirkin
2019-01-07 15:54 ` Peter Zijlstra
2019-01-07 16:22 ` Michael S. Tsirkin
[not found] ` <20190107190236.GF1215@linux.ibm.com>
2019-01-07 19:13 ` Michael S. Tsirkin
[not found] ` <20190102205715.14054-3-mst@redhat.com>
2019-01-07 17:54 ` [PATCH RFC 2/4] include/linux/compiler.h: allow memory operands Will Deacon
2019-01-07 18:16 ` Michael S. Tsirkin
[not found] ` <20190102205715.14054-2-mst@redhat.com>
[not found] ` <CAKwvOdk3b6mnpTEEyY-JFrej8K12w9oTMLqi7q5aNiRMyJo6Ow@mail.gmail.com>
2019-01-08 18:50 ` [PATCH RFC 1/4] include/linux/compiler*.h: fix OPTIMIZER_HIDE_VAR Michael S. Tsirkin
[not found] ` <CANiq72nEiw8+cZNnBOwF1ehv232ZJe5qzhdbaTEg0rOoV2p4sg@mail.gmail.com>
2019-01-09 14:50 ` Michael S. Tsirkin
2019-01-10 2:36 ` Michael S. Tsirkin
[not found] ` <20190109213543-mutt-send-email-mst@kernel.org>
2019-01-10 13:41 ` Dan Carpenter
[not found] ` <20190109094959-mutt-send-email-mst@kernel.org>
[not found] ` <CANiq72npFGsoHmBQUSNRLohDmMBNQFka8AcmhMz--QU1v2Aorg@mail.gmail.com>
2019-01-20 14:43 ` 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=155183f3-d03c-5e79-c267-974c502ff5d7@redhat.com \
--to=jasowang@redhat.com \
--cc=akiyks@gmail.com \
--cc=andrea.parri@amarulasolutions.com \
--cc=arnd@arndb.de \
--cc=boqun.feng@gmail.com \
--cc=corbet@lwn.net \
--cc=dhowells@redhat.com \
--cc=dlustig@nvidia.com \
--cc=ink@jurassic.park.msu.ru \
--cc=j.alglave@ucl.ac.uk \
--cc=linux-alpha@vger.kernel.orgLuc \
--cc=linux-arch@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sparse@vger.kernel.org \
--cc=luc.maranget@inria.fr \
--cc=mattst88@gmail.com \
--cc=mst@redhat.com \
--cc=netdev@vger.kernel.org \
--cc=npiggin@gmail.com \
--cc=paulmck@linux.vnet.ibm.com \
--cc=peterz@infradead.org \
--cc=rth@twiddle.net \
--cc=stern@rowland.harvard.edu \
--cc=virtualization@lists.linux-foundation.org \
--cc=will.deacon@arm.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 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).