Skip to contentSkip to conversation

locking/barriers: evaluate the argument to lockless_dereference only once

PATCH In review Mateusz Guzik 2 participants 2 messages Jun 7, 2016 – Jun 14, 2016 Sparse checker

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 locking/barriers: evaluate the argument to lockless_dereference only once

The commit 25841ee0e9d2a ("Validate lockless_dereference() is used on a pointer type") added a second use of the parameter to the macro.

This leads to trouble with consumers which use arguments with side effects.

Instead, reuse the value which was read the first time.

---
 include/linux/compiler.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/compiler.h +1 -1
index e9c6417..06f27fd 100644
@@ -551,8 +551,8 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s
  */
 #define lockless_dereference(p) \
 ({ \
-	__maybe_unused const void * const _________p2 = p; \
 	typeof(p) _________p1 = READ_ONCE(p); \
+	__maybe_unused const void * const _________p2 = _________p1; \
 	smp_read_barrier_depends(); /* Dependency order vs. p above. */ \
 	(_________p1); \
 })
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 5 lines · Mateusz Guzik

The commit 25841ee0e9d2a ("Validate lockless_dereference() is used on a pointer type") added a second use of the parameter to the macro.

This leads to trouble with consumers which use arguments with side effects.

Current tip/locking/core doesn't have that patch anymore IIRC, Ingo folded your thing and we now have:

commit 331b6d8c7afc2e5b900b9dcd850c265e1ba8d8e7

Author: Peter Zijlstra <[email protected]>
Date:   Sun May 22 12:48:27 2016 +0200

locking/barriers: Validate lockless_dereference() is used on a pointer type

Use the type to validate the argument @p is indeed a pointer type.

Cc Alexey Dobriyan <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Paul McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
include/linux/compiler.h +4 -0
index 793c0829e3a3..06f27fd9d760 100644
@@ -545,10 +545,14 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s
  * Similar to rcu_dereference(), but for situations where the pointed-to
  * object's lifetime is managed by something other than RCU.  That
  * "something other" might be reference counting or simple immortality.
+ *
+ * The seemingly unused void * variable is to validate @p is indeed a pointer
+ * type. All pointer types silently cast to void *.
  */
 #define lockless_dereference(p) \
 ({ \
 	typeof(p) _________p1 = READ_ONCE(p); \
+	__maybe_unused const void * const _________p2 = _________p1; \
 	smp_read_barrier_depends(); /* Dependency order vs. p above. */ \
 	(_________p1); \
 })