From: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
To: Darren Hart <dvhart@infradead.org>
Cc: Jianyu Zhan <nasa4836@gmail.com>,
LKML <linux-kernel@vger.kernel.org>,
Peter Zijlstra <peterz@infradead.org>,
Thomas Gleixner <tglx@linutronix.de>,
dave@stgolabs.net, Andrew Morton <akpm@linux-foundation.org>,
Ingo Molnar <mingo@kernel.org>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
dvhart@linux.intel.com,
Christian Borntraeger <borntraeger@de.ibm.com>,
Fengguang Wu <fengguang.wu@intel.com>,
bigeasy@linutronix.de
Subject: Re: [PATCH] futex: replace bare barrier() with more lightweight READ_ONCE()
Date: Fri, 4 Mar 2016 14:45:11 -0800 [thread overview]
Message-ID: <20160304224511.GX3577@linux.vnet.ibm.com> (raw)
In-Reply-To: <20160304223801.GG1092@dvhart-mobl5.amr.corp.intel.com>
On Fri, Mar 04, 2016 at 02:38:01PM -0800, Darren Hart wrote:
> On Fri, Mar 04, 2016 at 01:57:20PM -0800, Paul McKenney wrote:
> > On Fri, Mar 04, 2016 at 01:05:24PM -0800, Darren Hart wrote:
> > > On Fri, Mar 04, 2016 at 09:12:31AM +0800, Jianyu Zhan wrote:
> > > > On Fri, Mar 4, 2016 at 1:05 AM, Darren Hart <dvhart@infradead.org> wrote:
> > > > > I thought I provided a corrected comment block.... maybe I didn't. We have been
> > > > > working on improving the futex documentation, so we're paying close attention to
> > > > > terminology as well as grammar. This one needs a couple minor tweaks. I suggest:
> > > > >
> > > > > /*
> > > > > * Use READ_ONCE to forbid the compiler from reloading q->lock_ptr and
> > > > > * optimizing lock_ptr out of the logic below.
> > > > > */
> > > > >
> > > > > The bit about q->lock_ptr possibly changing is already covered by the large
> > > > > comment block below the spin_lock(lock_ptr) call.
> > > >
> > > > The large comment block is explaining the why the retry logic is required.
> > > > To achieve this semantic requirement, the READ_ONCE is needed to prevent
> > > > compiler optimizing it by doing double loads.
> > > >
> > > > So I think the comment above should explain this tricky part.
> > >
> > > Fair point. Consider:
> > >
> > >
> > > /*
> > > * q->lock_ptr can change between this read and the following spin_lock.
> > > * Use READ_ONCE to forbid the compiler from reloading q->lock_ptr and
> > > * optimizing lock_ptr out of the logic below.
> > > */
> > >
> > > >
> > > > > /* Use READ_ONCE to forbid the compiler from reloading q->lock_ptr in spin_lock() */
> > > >
> > > > And as for preventing from optimizing the lock_ptr out of the retry
> > > > code block, I have consult
> > > > Paul Mckenney, he suggests one more READ_ONCE should be added here:
> > >
> > > Let's keep this discussion together so we have a record of the
> > > justification.
> > >
> > > +Paul McKenney
> > >
> > > Paul, my understanding was that spin_lock was a CPU memory barrier,
> > > which in turn is an implicit compiler barrier (aka barrier()), of which
> > > READ_ONCE is described as a weaker form. Reviewing this, I realize the
> > > scope of barrier() wasn't clear to me. It seems while barrier() ensures
> > > ordering, it does not offer the same guarantee regarding reloading that
> > > READ_ONCE offers. So READ_ONCE is not strictly a weaker form of
> > > barrier() as I had gathered from a spotty reading of
> > > memory-barriers.txt, but it also offers guarantees regarding memory
> > > references that barrier() does not.
> > >
> > > Correct?
> >
> > If q->lock_ptr is never changed except under that lock, then there is
> > indeed no reason for the ACCESS_ONCE().
>
> The only location where a q->lock_ptr is updated without that lock being held is
> in queue_lock(). This is safe as the futex_q is not yet queued onto an hb until
> after the lock is held (so unqueue_me() cannot race with queue_lock()).
>
> > So, is q->lock_ptr ever changed while the lock is -not- held? If so,
> > I suggest that you put an ACCESS_ONCE() there.
>
> It is not.
If I followed that correctly, then I agree that you don't need an
ACCESS_ONCE() in this case.
Thanx, Paul
next prev parent reply other threads:[~2016-03-04 22:45 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-03-03 15:38 [PATCH] futex: replace bare barrier() with more lightweight READ_ONCE() Jianyu Zhan
2016-03-03 17:05 ` Darren Hart
2016-03-04 1:12 ` Jianyu Zhan
2016-03-04 21:05 ` Darren Hart
2016-03-04 21:57 ` Paul E. McKenney
2016-03-04 22:38 ` Darren Hart
2016-03-04 22:45 ` Paul E. McKenney [this message]
2016-03-04 22:53 ` Darren Hart
2016-03-07 1:32 ` [PATCH v3] " Jianyu Zhan
2016-03-08 11:26 ` Darren Hart
2016-03-08 16:09 ` [tip:locking/core] futex: Replace barrier() in unqueue_me() with READ_ONCE() tip-bot for Jianyu Zhan
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=20160304224511.GX3577@linux.vnet.ibm.com \
--to=paulmck@linux.vnet.ibm.com \
--cc=akpm@linux-foundation.org \
--cc=bigeasy@linutronix.de \
--cc=borntraeger@de.ibm.com \
--cc=dave@stgolabs.net \
--cc=dvhart@infradead.org \
--cc=dvhart@linux.intel.com \
--cc=fengguang.wu@intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=mingo@kernel.org \
--cc=nasa4836@gmail.com \
--cc=peterz@infradead.org \
--cc=tglx@linutronix.de \
/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).