History

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.

Johannes Berg committed 10 years ago

Commit d7127b51 parent c6935931 file · +4 −3Browse files

Showing the changes to include/linux (1 of 1 file). Show all

Unified · Split

include/linux/compiler.h+4−3
@@ -527,13 +527,14 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s · 526 unmodified lines
527527 * object's lifetime is managed by something other than RCU. That
528528 * "something other" might be reference counting or simple immortality.
529529 *
530− * The seemingly unused size_t variable is to validate @p is indeed a pointer
531− * type by making sure it can be dereferenced.
530+ * The seemingly unused variable ___typecheck_p validates that @p is
531+ * indeed a pointer type by using a pointer to typeof(*p) as the type.
532+ * Taking a pointer to typeof(*p) again is needed in case p is void *.
532533 */
533534 #define lockless_dereference(p) \
534535 ({ \
535536 typeof(p) _________p1 = READ_ONCE(p); \
536− size_t __maybe_unused __size_of_ptr = sizeof(*(p)); \
537+ typeof(*(p)) *___typecheck_p __maybe_unused; \
537538 smp_read_barrier_depends(); /* Dependency order vs. p above. */ \
538539 (_________p1); \
539540 })