linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Paul E. McKenney" <paulmck@kernel.org>
To: Will Deacon <will@kernel.org>
Cc: Qian Cai <cai@lca.pw>, Elver Marco <elver@google.com>,
	LKML <linux-kernel@vger.kernel.org>,
	Ingo Molnar <mingo@redhat.com>,
	"Peter Zijlstra (Intel)" <peterz@infradead.org>
Subject: Re: [PATCH -next v2] locking/osq_lock: annotate a data race in osq_lock
Date: Mon, 11 May 2020 11:07:33 -0700	[thread overview]
Message-ID: <20200511180733.GA2869@paulmck-ThinkPad-P72> (raw)
In-Reply-To: <20200511173412.GC23081@willie-the-truck>

On Mon, May 11, 2020 at 06:34:13PM +0100, Will Deacon wrote:
> On Mon, May 11, 2020 at 10:29:18AM -0700, Paul E. McKenney wrote:
> > On Mon, May 11, 2020 at 05:52:17PM +0100, Will Deacon wrote:
> > > On Mon, May 11, 2020 at 09:43:19AM -0700, Paul E. McKenney wrote:
> > > > On Mon, May 11, 2020 at 04:58:13PM +0100, Will Deacon wrote:
> > > > > On Sat, May 09, 2020 at 02:36:54PM -0700, Paul E. McKenney wrote:
> > > > > > diff --git a/kernel/locking/osq_lock.c b/kernel/locking/osq_lock.c
> > > > > > index 1f77349..1de006e 100644
> > > > > > --- a/kernel/locking/osq_lock.c
> > > > > > +++ b/kernel/locking/osq_lock.c
> > > > > > @@ -154,7 +154,11 @@ bool osq_lock(struct optimistic_spin_queue *lock)
> > > > > >  	 */
> > > > > >  
> > > > > >  	for (;;) {
> > > > > > -		if (prev->next == node &&
> > > > > > +		/*
> > > > > > +		 * cpu_relax() below implies a compiler barrier which would
> > > > > > +		 * prevent this comparison being optimized away.
> > > > > > +		 */
> > > > > > +		if (data_race(prev->next) == node &&
> > > > > >  		    cmpxchg(&prev->next, node, NULL) == node)
> > > > > >  			break;
> > > > > 
> > > > > I'm fine with the data_race() placement, but I don't find the comment
> > > > > very helpful. We assign the result of a READ_ONCE() to 'prev' in the
> > > > > loop, so I don't think that the cpu_relax() is really relevant.
> > > > 
> > > > Suppose that the compiler loaded a value that was not equal to "node".
> > > > In that case, the cmpxchg() won't happen, so something else must force
> > > > the compiler to do the reload in order to avoid an infinite loop, right?
> > > > Or am I missing something here?
> > > 
> > > Then we just go round the loop and reload prev:
> > > 
> > > 	prev = READ_ONCE(node->prev);
> > > 
> > > which should be enough to stop the compiler, no?
> > 
> > Yes, that would also work.  Either have the cpu_relax() or a barrier()
> > or whatever on the one hand, or, as you say, turn the data_race() into
> > a READ_ONCE().  I personally prefer the READ_ONCE() myself, unless that
> > would undesirably suppress other KCSAN warnings.
> 
> No, I mean here is the code after this patch is applied:
> 
> 	for (;;) {
> 		if (data_race(prev->next) == node &&
> 		    cmpxchg(&prev->next, node, NULL) == node)
> 			break;
> 
> 		/*
> 		 * We can only fail the cmpxchg() racing against an unlock(),
> 		 * in which case we should observe @node->locked becomming
> 		 * true.
> 		 */
> 		if (smp_load_acquire(&node->locked))
> 			return true;
> 
> 		cpu_relax();
> 
> 		/*
> 		 * Or we race against a concurrent unqueue()'s step-B, in which
> 		 * case its step-C will write us a new @node->prev pointer.
> 		 */
> 		prev = READ_ONCE(node->prev);
> 	}
> 
> I'm saying that this READ_ONCE at the end of the loop should be sufficient
> to stop the compiler making value assumptions about prev->next. Do you
> agree?

Good point, and I would certainly hope so!

							Thanx, Paul

  reply	other threads:[~2020-05-11 18:07 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-11 13:54 [PATCH -next v2] locking/osq_lock: annotate a data race in osq_lock Qian Cai
2020-05-08 20:59 ` Qian Cai
2020-05-09  4:33   ` Paul E. McKenney
2020-05-09 13:01     ` Qian Cai
2020-05-09 16:12       ` Paul E. McKenney
2020-05-09 16:53         ` Qian Cai
2020-05-09 21:36           ` Paul E. McKenney
2020-05-11 15:58             ` Will Deacon
2020-05-11 16:43               ` Paul E. McKenney
2020-05-11 16:52                 ` Will Deacon
2020-05-11 17:29                   ` Paul E. McKenney
2020-05-11 17:34                     ` Will Deacon
2020-05-11 18:07                       ` Paul E. McKenney [this message]
2020-05-11 16:44               ` Qian Cai
2020-05-11 16:54                 ` Will Deacon
2020-05-11 17:10                   ` Qian Cai

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=20200511180733.GA2869@paulmck-ThinkPad-P72 \
    --to=paulmck@kernel.org \
    --cc=cai@lca.pw \
    --cc=elver@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=will@kernel.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 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).