From: Peter Zijlstra <peterz@infradead.org>
To: Waiman Long <waiman.long@hp.com>
Cc: Davidlohr Bueso <davidlohr.bueso@hp.com>,
Rik van Riel <riel@redhat.com>,
Linus Torvalds <torvalds@linux-foundation.org>,
Andrew Morton <akpm@linux-foundation.org>,
Thomas Gleixner <tglx@linutronix.de>,
"Paul E. McKenney" <paulmck@linux.vnet.ibm.com>,
David Howells <dhowells@redhat.com>,
Ingo Molnar <mingo@kernel.org>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] mutex: Fix mutex_can_spin_on_owner
Date: Sat, 20 Jul 2013 13:16:48 +0200 [thread overview]
Message-ID: <20130720111648.GQ27075@twins.programming.kicks-ass.net> (raw)
In-Reply-To: <51E98EB4.3080307@hp.com>
On Fri, Jul 19, 2013 at 03:08:36PM -0400, Waiman Long wrote:
> On 07/19/2013 02:31 PM, Peter Zijlstra wrote:
> >mutex_can_spin_on_owner() is broken in that it would allow the compiler
> >to load lock->owner twice, seeing a pointer first time and a MULL
> >pointer the second time.
> >
> >Signed-off-by: Peter Zijlstra<peterz@infradead.org>
> >---
> > kernel/mutex.c | 6 ++++--
> > 1 file changed, 4 insertions(+), 2 deletions(-)
> >
> >diff --git a/kernel/mutex.c b/kernel/mutex.c
> >index ff05f4b..7ff48c5 100644
> >--- a/kernel/mutex.c
> >+++ b/kernel/mutex.c
> >@@ -209,11 +209,13 @@ int mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner)
> > */
> > static inline int mutex_can_spin_on_owner(struct mutex *lock)
> > {
> >+ struct task_struct *owner;
> > int retval = 1;
> >
> > rcu_read_lock();
> >- if (lock->owner)
> >- retval = lock->owner->on_cpu;
> >+ owner = ACCESS_ONCE(lock->owner);
> >+ if (owner)
> >+ retval = owner->on_cpu;
> > rcu_read_unlock();
> > /*
> > * if lock->owner is not set, the mutex owner may have just acquired
>
> I am fine with this change. However, the compiler is smart enough to not do
> two memory accesses to the same memory location. So this will not change the
> generated code. Below is the relevant x86 code for that section of code:
Yes I know, but the compiler would be allowed to do so; not so after the
change.
Also, GCC can be surprisingly stupid at times, depending on the options
given, never rely/trust on anything you don't have to.
next prev parent reply other threads:[~2013-07-20 11:17 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-07-19 18:31 Peter Zijlstra
2013-07-19 18:36 ` Davidlohr Bueso
2013-07-19 19:08 ` Waiman Long
2013-07-19 19:41 ` Thomas Gleixner
2013-07-19 19:48 ` Linus Torvalds
2013-07-19 20:58 ` Waiman Long
2013-07-25 12:18 ` Jan-Simon Möller
2013-07-20 11:16 ` Peter Zijlstra [this message]
2013-07-19 19:36 ` Rik van Riel
2013-07-23 7:46 ` [tip:core/locking] mutex: Fix/ document access-once assumption in mutex_can_spin_on_owner() tip-bot for Peter Zijlstra
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=20130720111648.GQ27075@twins.programming.kicks-ass.net \
--to=peterz@infradead.org \
--cc=akpm@linux-foundation.org \
--cc=davidlohr.bueso@hp.com \
--cc=dhowells@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=paulmck@linux.vnet.ibm.com \
--cc=riel@redhat.com \
--cc=tglx@linutronix.de \
--cc=torvalds@linux-foundation.org \
--cc=waiman.long@hp.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
Powered by JetHome