Message ID | 20240928135128.991110-3-mathieu.desnoyers@efficios.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | Introduce ptr_eq() to preserve address dependency | expand |
On Sat, Sep 28, 2024 at 09:51:28AM -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 > --- > Documentation/RCU/rcu_dereference.rst | 34 +++++++++++++++++++++++---- > 1 file changed, 29 insertions(+), 5 deletions(-) > > diff --git a/Documentation/RCU/rcu_dereference.rst b/Documentation/RCU/rcu_dereference.rst > index 2524dcdadde2..c36b8d1721f6 100644 > --- a/Documentation/RCU/rcu_dereference.rst > +++ b/Documentation/RCU/rcu_dereference.rst > @@ -104,11 +104,13 @@ 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 relational operators which preserve address dependencies > + (such as "ptr_eq()") to compare pointers obtained from Nit: ptr_eq() is an inline function, not a relational operator. Say "operations that" instead of "relational operators which". > + rcu_dereference() against non-NULL values or against pointers > + obtained from prior loads. 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 +127,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 a pointer obtained from prior > + loads, the compiler is allowed to use either register for the This is true even when the comparison is against a pointer obtained from a later load. Just say "another pointer" instead of "a pointer obtained from prior loads". (And why would someone need multiple loads to obtain a single pointer?) Also, say "pointer" instead of "register". > + 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); Here you should say that the compiler could use p1->a rather than p2->a, destroying the address dependency. That's the whole point of this; you shouldn't skip over it. > + > + Performing the comparison with "ptr_eq()" ensures the compiler > + preserves the address dependencies. > > However, comparisons are OK in the following cases: > > @@ -204,6 +223,11 @@ readers working properly: > comparison will provide exactly the information that the > compiler needs to deduce the value of the pointer. > > + When in doubt, use relational operators that preserve address Again, "operations" instead of "relational operators". Alan Stern > + dependencies (such as "ptr_eq()") to compare pointers obtained > + from rcu_dereference() against non-NULL values or against > + pointers obtained from prior loads. > + > - 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 >
On 2024-09-28 16:58, Alan Stern wrote: > On Sat, Sep 28, 2024 at 09:51:28AM -0400, Mathieu Desnoyers wrote: [...] >> -- 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 relational operators which preserve address dependencies >> + (such as "ptr_eq()") to compare pointers obtained from > > Nit: ptr_eq() is an inline function, not a relational operator. Say > "operations that" instead of "relational operators which". > >> + rcu_dereference() against non-NULL values or against pointers Note: here I need to update the wording as well: +- 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:: >> + obtained from prior loads. 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 +127,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 a pointer obtained from prior >> + loads, the compiler is allowed to use either register for the > > This is true even when the comparison is against a pointer obtained from > a later load. Just say "another pointer" instead of "a pointer obtained > from prior loads". (And why would someone need multiple loads to > obtain a single pointer?) > > Also, say "pointer" instead of "register". OK. > >> + 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); > > Here you should say that the compiler could use p1->a rather than p2->a, > destroying the address dependency. That's the whole point of this; you > shouldn't skip over it. OK. > >> + >> + Performing the comparison with "ptr_eq()" ensures the compiler >> + preserves the address dependencies. >> >> However, comparisons are OK in the following cases: >> >> @@ -204,6 +223,11 @@ readers working properly: >> comparison will provide exactly the information that the >> compiler needs to deduce the value of the pointer. >> >> + When in doubt, use relational operators that preserve address > > Again, "operations" instead of "relational operators". OK. Will fix in my next round. Thanks, Mathieu > > Alan Stern > >> + dependencies (such as "ptr_eq()") to compare pointers obtained >> + from rcu_dereference() against non-NULL values or against >> + pointers obtained from prior loads. >> + >> - 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 >>
diff --git a/Documentation/RCU/rcu_dereference.rst b/Documentation/RCU/rcu_dereference.rst index 2524dcdadde2..c36b8d1721f6 100644 --- a/Documentation/RCU/rcu_dereference.rst +++ b/Documentation/RCU/rcu_dereference.rst @@ -104,11 +104,13 @@ 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 relational operators which preserve address dependencies + (such as "ptr_eq()") to compare pointers obtained from + rcu_dereference() against non-NULL values or against pointers + obtained from prior loads. 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 +127,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 a pointer obtained from prior + loads, the compiler is allowed to use either register 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); + + Performing the comparison with "ptr_eq()" ensures the compiler + preserves the address dependencies. However, comparisons are OK in the following cases: @@ -204,6 +223,11 @@ readers working properly: comparison will provide exactly the information that the compiler needs to deduce the value of the pointer. + When in doubt, use relational operators that preserve address + dependencies (such as "ptr_eq()") to compare pointers obtained + from rcu_dereference() against non-NULL values or against + pointers obtained from prior loads. + - 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
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 --- Documentation/RCU/rcu_dereference.rst | 34 +++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 5 deletions(-)