Skip to contentSkip to conversation

locking/barriers: don't use sizeof(void) in lockless_dereference()

PATCH New Johannes Berg 2 participants 2 messages Aug 26, 2016 – Sep 5, 2016 Sparse checker

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 locking/barriers: don't use sizeof(void) in lockless_dereference()

From: Johannes Berg <[email protected]>

My previous commit 112dc0c8069e ("locking/barriers: Suppress sparse warnings in lockless_dereference()") caused sparse to complain that (in radix-tree.h) we use sizeof(void) since that rcu_dereference()s a void *.

Really, all we need is to have the expression *p in here somewhere to make sure p is a pointer type, and sizeof(*p) was the thing that came to my mind first to make sure that's done without really doing anything at runtime.

Another thing I had considered was using typeof(*p), but obviously we can't just declare a typeof(*p) variable either, since that may end up being void. Declaring a variable as typeof(*p)* gets around that, and still checks that typeof(*p) is valid, so do that. This type construction can't be done for _________p1 because that will actually be used and causes sparse address space warnings, so keep a separate unused variable for it.

Fixes 112dc0c8069e ("locking/barriers: Suppress sparse warnings in lockless_dereference()")
---
 include/linux/compiler.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
include/linux/compiler.h +4 -3
index 436aa4e42221..668569844d37 100644
@@ -527,13 +527,14 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s
  * object's lifetime is managed by something other than RCU.  That
  * "something other" might be reference counting or simple immortality.
  *
- * The seemingly unused size_t variable is to validate @p is indeed a pointer
- * type by making sure it can be dereferenced.
+ * The seemingly unused variable ___typecheck_p validates that @p is
+ * indeed a pointer type by using a pointer to typeof(*p) as the type.
+ * Taking a pointer to typeof(*p) again is needed in case p is void *.
  */
 #define lockless_dereference(p) \
 ({ \
 	typeof(p) _________p1 = READ_ONCE(p); \
-	size_t __maybe_unused __size_of_ptr = sizeof(*(p)); \
+	typeof(*(p)) *___typecheck_p __maybe_unused; \
 	smp_read_barrier_depends(); /* Dependency order vs. p above. */ \
 	(_________p1); \
 })
Signature · 1 line
2.8.1
#1 lore raw

[tip:locking/urgent] locking/barriers: Don't use sizeof(void) in lockless_dereference()

locking/barriers: Don't use sizeof(void) in lockless_dereference()
Commit-ID:  d7127b5e5fa0551be21b86640f1648b224e36d43
Gitweb:     http://git.kernel.org/tip/d7127b5e5fa0551be21b86640f1648b224e36d43
Author:     Johannes Berg <[email protected]>
AuthorDate: Fri, 26 Aug 2016 08:16:00 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 5 Sep 2016 11:50:42 +0200

locking/barriers: Don't use sizeof(void) in lockless_dereference()

My previous commit:

112dc0c8069e ("locking/barriers: Suppress sparse warnings in lockless_dereference()")

caused sparse to complain that (in radix-tree.h) we use sizeof(void) since that rcu_dereference()s a void *.

Really, all we need is to have the expression *p in here somewhere to make sure p is a pointer type, and sizeof(*p) was the thing that came to my mind first to make sure that's done without really doing anything at runtime.

Another thing I had considered was using typeof(*p), but obviously we can't just declare a typeof(*p) variable either, since that may end up being void. Declaring a variable as typeof(*p)* gets around that, and still checks that typeof(*p) is valid, so do that. This type construction can't be done for _________p1 because that will actually be used and causes sparse address space warnings, so keep a separate unused variable for it.

Cc Linus Torvalds <[email protected]>
Cc Paul E . McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
Fixes 112dc0c8069e ("locking/barriers: Suppress sparse warnings in lockless_dereference()")
---
 include/linux/compiler.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
include/linux/compiler.h +4 -3
index 436aa4e..6685698 100644
@@ -527,13 +527,14 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s
  * object's lifetime is managed by something other than RCU.  That
  * "something other" might be reference counting or simple immortality.
  *
- * The seemingly unused size_t variable is to validate @p is indeed a pointer
- * type by making sure it can be dereferenced.
+ * The seemingly unused variable ___typecheck_p validates that @p is
+ * indeed a pointer type by using a pointer to typeof(*p) as the type.
+ * Taking a pointer to typeof(*p) again is needed in case p is void *.
  */
 #define lockless_dereference(p) \
 ({ \
 	typeof(p) _________p1 = READ_ONCE(p); \
-	size_t __maybe_unused __size_of_ptr = sizeof(*(p)); \
+	typeof(*(p)) *___typecheck_p __maybe_unused; \
 	smp_read_barrier_depends(); /* Dependency order vs. p above. */ \
 	(_________p1); \
 })