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 <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>
Cc: Sebastian Andrzej Siewior <[email protected]>
Cc: "Paul E. McKenney" <[email protected]>
Cc: Will Deacon <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Boqun Feng <[email protected]>
Cc: Alan Stern <[email protected]>
Cc: John Stultz <[email protected]>
Cc: Neeraj Upadhyay <[email protected]>
Cc: Linus Torvalds <[email protected]>
Cc: Boqun Feng <[email protected]>
Cc: Frederic Weisbecker <[email protected]>
Cc: Joel Fernandes <[email protected]>
Cc: Josh Triplett <[email protected]>
Cc: Uladzislau Rezki <[email protected]>
Cc: Steven Rostedt <[email protected]>
Cc: Lai Jiangshan <[email protected]>
Cc: Zqiang <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Waiman Long <[email protected]>
Cc: Mark Rutland <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Vlastimil Babka <[email protected]>
Cc: [email protected]
Cc: Mateusz Guzik <[email protected]>
Cc: Gary Guo <[email protected]>
Cc: Jonas Oberhauser <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---
 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
+       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
-- 
2.39.2


Reply via email to