linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: "Paul E. McKenney" <paulmck@kernel.org>
To: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
	linux-kernel@vger.kernel.org,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
	Will Deacon <will@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Boqun Feng <boqun.feng@gmail.com>,
	Alan Stern <stern@rowland.harvard.edu>,
	John Stultz <jstultz@google.com>,
	Neeraj Upadhyay <Neeraj.Upadhyay@amd.com>,
	Frederic Weisbecker <frederic@kernel.org>,
	Joel Fernandes <joel@joelfernandes.org>,
	Josh Triplett <josh@joshtriplett.org>,
	Uladzislau Rezki <urezki@gmail.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Lai Jiangshan <jiangshanlai@gmail.com>,
	Zqiang <qiang.zhang1211@gmail.com>,
	Ingo Molnar <mingo@redhat.com>, Waiman Long <longman@redhat.com>,
	Mark Rutland <mark.rutland@arm.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Vlastimil Babka <vbabka@suse.cz>,
	maged.michael@gmail.com, Mateusz Guzik <mjguzik@gmail.com>,
	Gary Guo <gary@garyguo.net>,
	Jonas Oberhauser <jonas.oberhauser@huaweicloud.com>,
	rcu@vger.kernel.org, linux-mm@kvack.org, lkmm@lists.linux.dev,
	Nikita Popov <github@npopov.com>,
	llvm@lists.linux.dev
Subject: Re: [PATCH v1 2/2] Documentation: RCU: Refer to ptr_eq()
Date: Sun, 29 Sep 2024 10:07:35 -0700	[thread overview]
Message-ID: <cf2139e2-41cd-4213-89e9-c10873498edf@paulmck-laptop> (raw)
In-Reply-To: <3a298b64-dd7c-4a0d-950e-8e5b98b39fee@efficios.com>

On Sun, Sep 29, 2024 at 12:09:54PM -0400, Mathieu Desnoyers wrote:
> On 2024-09-29 17:51, Paul E. McKenney wrote:
> > On Sun, Sep 29, 2024 at 07:16:08AM -0400, Mathieu Desnoyers wrote:
> > > Refer to ptr_eq() in the rcu_dereference() documentation.
> > > 
> > > ptr_eq() is a mechanism that preserves address dependencies when
> > > comparing pointers, and should be favored when comparing a pointer
> > > obtained from rcu_dereference() against another pointer.
> > > 
> > > Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
> > > Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> > > Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
> > > Cc: "Paul E. McKenney" <paulmck@kernel.org>
> > > Cc: Will Deacon <will@kernel.org>
> > > Cc: Peter Zijlstra <peterz@infradead.org>
> > > Cc: Boqun Feng <boqun.feng@gmail.com>
> > > Cc: Alan Stern <stern@rowland.harvard.edu>
> > > Cc: John Stultz <jstultz@google.com>
> > > Cc: Neeraj Upadhyay <Neeraj.Upadhyay@amd.com>
> > > Cc: Linus Torvalds <torvalds@linux-foundation.org>
> > > Cc: Boqun Feng <boqun.feng@gmail.com>
> > > Cc: Frederic Weisbecker <frederic@kernel.org>
> > > Cc: Joel Fernandes <joel@joelfernandes.org>
> > > Cc: Josh Triplett <josh@joshtriplett.org>
> > > Cc: Uladzislau Rezki <urezki@gmail.com>
> > > Cc: Steven Rostedt <rostedt@goodmis.org>
> > > Cc: Lai Jiangshan <jiangshanlai@gmail.com>
> > > Cc: Zqiang <qiang.zhang1211@gmail.com>
> > > Cc: Ingo Molnar <mingo@redhat.com>
> > > Cc: Waiman Long <longman@redhat.com>
> > > Cc: Mark Rutland <mark.rutland@arm.com>
> > > Cc: Thomas Gleixner <tglx@linutronix.de>
> > > Cc: Vlastimil Babka <vbabka@suse.cz>
> > > Cc: maged.michael@gmail.com
> > > Cc: Mateusz Guzik <mjguzik@gmail.com>
> > > Cc: Gary Guo <gary@garyguo.net>
> > > Cc: Jonas Oberhauser <jonas.oberhauser@huaweicloud.com>
> > > Cc: rcu@vger.kernel.org
> > > Cc: linux-mm@kvack.org
> > > Cc: lkmm@lists.linux.dev
> > > Cc: Nikita Popov <github@npopov.com>
> > > Cc: llvm@lists.linux.dev
> > > ---
> > > Changes since v0:
> > > - Include feedback from Alan Stern.
> > > ---
> > >   Documentation/RCU/rcu_dereference.rst | 32 ++++++++++++++++++++++-----
> > >   1 file changed, 27 insertions(+), 5 deletions(-)
> > > 
> > > diff --git a/Documentation/RCU/rcu_dereference.rst b/Documentation/RCU/rcu_dereference.rst
> > > index 2524dcdadde2..9ef97b7ca74d 100644
> > > --- a/Documentation/RCU/rcu_dereference.rst
> > > +++ b/Documentation/RCU/rcu_dereference.rst
> > > @@ -104,11 +104,12 @@ readers working properly:
> > >   	after such branches, but can speculate loads, which can again
> > >   	result in misordering bugs.
> > > --	Be very careful about comparing pointers obtained from
> > > -	rcu_dereference() against non-NULL values.  As Linus Torvalds
> > > -	explained, if the two pointers are equal, the compiler could
> > > -	substitute the pointer you are comparing against for the pointer
> > > -	obtained from rcu_dereference().  For example::
> > > +-	Use operations that preserve address dependencies (such as
> > > +	"ptr_eq()") to compare pointers obtained from rcu_dereference()
> > > +	against non-NULL pointers. As Linus Torvalds explained, if the
> > > +	two pointers are equal, the compiler could substitute the
> > > +	pointer you are comparing against for the pointer obtained from
> > > +	rcu_dereference().  For example::
> > >   		p = rcu_dereference(gp);
> > >   		if (p == &default_struct)
> > > @@ -125,6 +126,23 @@ readers working properly:
> > >   	On ARM and Power hardware, the load from "default_struct.a"
> > >   	can now be speculated, such that it might happen before the
> > >   	rcu_dereference().  This could result in bugs due to misordering.
> > > +	Performing the comparison with "ptr_eq()" ensures the compiler
> > > +	does not perform such transformation.
> > > +
> > > +	If the comparison is against another pointer, the compiler is
> > > +	allowed to use either pointer for the following accesses, which
> > > +	loses the address dependency and allows weakly-ordered
> > > +	architectures such as ARM and PowerPC to speculate the
> > > +	address-dependent load before rcu_dereference().  For example::
> > > +
> > > +		p1 = READ_ONCE(gp);
> > > +		p2 = rcu_dereference(gp);
> > > +		if (p1 == p2)
> > > +			do_default(p2->a);
> > > +
> > > +	The compiler can use p1->a rather than p2->a, destroying the
> > > +	address dependency.  Performing the comparison with "ptr_eq()"
> > > +	ensures the compiler preserves the address dependencies.
> > 
> > Bitter experience leads me to suggest a "// BUGGY" comment on the "if"
> > statement in the above example, and a corrected code snippet right here.  :-/
> 
> Changing for the following:
> 
> +               p1 = READ_ONCE(gp);
> +               p2 = rcu_dereference(gp);
> +               if (p1 == p2)  /* BUGGY!!! */
> +                       do_default(p2->a);
> +
> +       The compiler can use p1->a rather than p2->a, destroying the
> +       address dependency.  Performing the comparison with "ptr_eq()"
> +       ensures the compiler preserves the address dependencies.
> +       Corrected code::
> +
> +               p1 = READ_ONCE(gp);
> +               p2 = rcu_dereference(gp);
> +               if (ptr_eq(p1, p2))
> +                       do_default(p2->a);
> 
> > 
> > Other than that, loks good!
> 
> Let me know if I should add an acked-by from you on this
> documentation patch as well.

Much better!

Acked-by: Paul E. McKenney <paulmck@kernel.org>

> Thanks,
> 
> Mathieu
> 
> > 
> > 							Thanx, Paul
> > 
> > >   	However, comparisons are OK in the following cases:
> > > @@ -204,6 +222,10 @@ readers working properly:
> > >   		comparison will provide exactly the information that the
> > >   		compiler needs to deduce the value of the pointer.
> > > +	When in doubt, use operations that preserve address dependencies
> > > +	(such as "ptr_eq()") to compare pointers obtained from
> > > +	rcu_dereference() against non-NULL pointers.
> > > +
> > >   -	Disable any value-speculation optimizations that your compiler
> > >   	might provide, especially if you are making use of feedback-based
> > >   	optimizations that take data collected from prior runs.  Such
> > > -- 
> > > 2.39.2
> > > 
> 
> -- 
> Mathieu Desnoyers
> EfficiOS Inc.
> https://www.efficios.com
> 


  reply	other threads:[~2024-09-29 17:07 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-29 11:16 [PATCH v1 0/2] Introduce ptr_eq() to preserve address dependency Mathieu Desnoyers
2024-09-29 11:16 ` [PATCH v1 1/2] compiler.h: " Mathieu Desnoyers
2024-09-29 11:16 ` [PATCH v1 2/2] Documentation: RCU: Refer to ptr_eq() Mathieu Desnoyers
2024-09-29 15:51   ` Paul E. McKenney
2024-09-29 16:09     ` Mathieu Desnoyers
2024-09-29 17:07       ` Paul E. McKenney [this message]
2024-09-29 15:50 ` [PATCH v1 0/2] Introduce ptr_eq() to preserve address dependency Alan Stern

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=cf2139e2-41cd-4213-89e9-c10873498edf@paulmck-laptop \
    --to=paulmck@kernel.org \
    --cc=Neeraj.Upadhyay@amd.com \
    --cc=bigeasy@linutronix.de \
    --cc=boqun.feng@gmail.com \
    --cc=frederic@kernel.org \
    --cc=gary@garyguo.net \
    --cc=github@npopov.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jiangshanlai@gmail.com \
    --cc=joel@joelfernandes.org \
    --cc=jonas.oberhauser@huaweicloud.com \
    --cc=josh@joshtriplett.org \
    --cc=jstultz@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lkmm@lists.linux.dev \
    --cc=llvm@lists.linux.dev \
    --cc=longman@redhat.com \
    --cc=maged.michael@gmail.com \
    --cc=mark.rutland@arm.com \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mingo@redhat.com \
    --cc=mjguzik@gmail.com \
    --cc=peterz@infradead.org \
    --cc=qiang.zhang1211@gmail.com \
    --cc=rcu@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=stern@rowland.harvard.edu \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    --cc=urezki@gmail.com \
    --cc=vbabka@suse.cz \
    --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