Skip to contentSkip to conversation

rcu: rcupdate.h: Add missing parentheses around macro pointer dereference

RFC PATCH In review Mathieu Desnoyers 5 participants 7 messages May 3, 2023 – May 5, 2023 RCU

lore
Share & tools
lore

Mathieu Desnoyers

author reviewer
#0 lore raw

RFC PATCH 1/1 rcu: rcupdate.h: Add missing parentheses around macro pointer dereference

linux/rcupdate.h macros use the *p parameter without parentheses, e.g.:

typeof(*p)

rather than

typeof(*(p))

The following test-case shows how it can generate confusion due to C operator precedence being reversed compared to the expectations:

    #define m(p) \
    do { \
            __typeof__(*p) v = 0; \
    } while (0)

    void fct(unsigned long long *p1)
    {
            m(p1 + 1);      /* works */
            m(1 + p1);      /* broken */
    }
Cc "Paul E. McKenney" <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Boqun Feng <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Zqiang <[email protected]>
---
 include/linux/rcupdate.h | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
include/linux/rcupdate.h +9 -9
index dcd2cf1e8326..1565012fa47f 100644
@@ -430,16 +430,16 @@ static inline void rcu_preempt_sleep_check(void) { }
 
 #ifdef __CHECKER__
 #define rcu_check_sparse(p, space) \
-	((void)(((typeof(*p) space *)p) == p))
+	((void)(((typeof(*(p)) space *)p) == p))
 #else /* #ifdef __CHECKER__ */
 #define rcu_check_sparse(p, space)
 #endif /* #else #ifdef __CHECKER__ */
 
 #define __unrcu_pointer(p, local)					\
 ({									\
-	typeof(*p) *local = (typeof(*p) *__force)(p);			\
+	typeof(*(p)) *local = (typeof(*(p)) *__force)(p);		\
 	rcu_check_sparse(p, __rcu);					\
-	((typeof(*p) __force __kernel *)(local)); 			\
+	((typeof(*(p)) __force __kernel *)(local));			\
 })
 /**
  * unrcu_pointer - mark a pointer as not being RCU protected
@@ -452,29 +452,29 @@ static inline void rcu_preempt_sleep_check(void) { }
 
 #define __rcu_access_pointer(p, local, space) \
 ({ \
-	typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
+	typeof(*(p)) *local = (typeof(*(p)) *__force)READ_ONCE(p); \
 	rcu_check_sparse(p, space); \
-	((typeof(*p) __force __kernel *)(local)); \
+	((typeof(*(p)) __force __kernel *)(local)); \
 })
 #define __rcu_dereference_check(p, local, c, space) \
 ({ \
 	/* Dependency order vs. p above. */ \
-	typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
+	typeof(*(p)) *local = (typeof(*(p)) *__force)READ_ONCE(p); \
 	RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_check() usage"); \
 	rcu_check_sparse(p, space); \
-	((typeof(*p) __force __kernel *)(local)); \
+	((typeof(*(p)) __force __kernel *)(local)); \
 })
 #define __rcu_dereference_protected(p, local, c, space) \
 ({ \
 	RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_protected() usage"); \
 	rcu_check_sparse(p, space); \
-	((typeof(*p) __force __kernel *)(p)); \
+	((typeof(*(p)) __force __kernel *)(p)); \
 })
 #define __rcu_dereference_raw(p, local) \
 ({ \
 	/* Dependency order vs. p above. */ \
 	typeof(p) local = READ_ONCE(p); \
-	((typeof(*p) __force __kernel *)(local)); \
+	((typeof(*(p)) __force __kernel *)(local)); \
 })
 #define rcu_dereference_raw(p) __rcu_dereference_raw(p, __UNIQUE_ID(rcu))
 
Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 22 lines · Mathieu Desnoyers

linux/rcupdate.h macros use the *p parameter without parentheses, e.g.:

typeof(*p)

rather than

typeof(*(p))

The following test-case shows how it can generate confusion due to C operator precedence being reversed compared to the expectations:

    #define m(p) \
    do { \
            __typeof__(*p) v = 0; \
    } while (0)

    void fct(unsigned long long *p1)
    {
            m(p1 + 1);      /* works */
            m(1 + p1);      /* broken */
    }
Quoted 8 lines
Cc "Paul E. McKenney" <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Boqun Feng <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Zqiang <[email protected]>

Regards,
Boqun

Quoted 67 lines
---
 include/linux/rcupdate.h | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
include/linux/rcupdate.h +9 -9
index dcd2cf1e8326..1565012fa47f 100644
@@ -430,16 +430,16 @@ static inline void rcu_preempt_sleep_check(void) { }
 
 #ifdef __CHECKER__
 #define rcu_check_sparse(p, space) \
-	((void)(((typeof(*p) space *)p) == p))
+	((void)(((typeof(*(p)) space *)p) == p))
 #else /* #ifdef __CHECKER__ */
 #define rcu_check_sparse(p, space)
 #endif /* #else #ifdef __CHECKER__ */
 
 #define __unrcu_pointer(p, local)					\
 ({									\
-	typeof(*p) *local = (typeof(*p) *__force)(p);			\
+	typeof(*(p)) *local = (typeof(*(p)) *__force)(p);		\
 	rcu_check_sparse(p, __rcu);					\
-	((typeof(*p) __force __kernel *)(local)); 			\
+	((typeof(*(p)) __force __kernel *)(local));			\
 })
 /**
  * unrcu_pointer - mark a pointer as not being RCU protected
@@ -452,29 +452,29 @@ static inline void rcu_preempt_sleep_check(void) { }
 
 #define __rcu_access_pointer(p, local, space) \
 ({ \
-	typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
+	typeof(*(p)) *local = (typeof(*(p)) *__force)READ_ONCE(p); \
 	rcu_check_sparse(p, space); \
-	((typeof(*p) __force __kernel *)(local)); \
+	((typeof(*(p)) __force __kernel *)(local)); \
 })
 #define __rcu_dereference_check(p, local, c, space) \
 ({ \
 	/* Dependency order vs. p above. */ \
-	typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
+	typeof(*(p)) *local = (typeof(*(p)) *__force)READ_ONCE(p); \
 	RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_check() usage"); \
 	rcu_check_sparse(p, space); \
-	((typeof(*p) __force __kernel *)(local)); \
+	((typeof(*(p)) __force __kernel *)(local)); \
 })
 #define __rcu_dereference_protected(p, local, c, space) \
 ({ \
 	RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_protected() usage"); \
 	rcu_check_sparse(p, space); \
-	((typeof(*p) __force __kernel *)(p)); \
+	((typeof(*(p)) __force __kernel *)(p)); \
 })
 #define __rcu_dereference_raw(p, local) \
 ({ \
 	/* Dependency order vs. p above. */ \
 	typeof(p) local = READ_ONCE(p); \
-	((typeof(*p) __force __kernel *)(local)); \
+	((typeof(*(p)) __force __kernel *)(local)); \
 })
 #define rcu_dereference_raw(p) __rcu_dereference_raw(p, __UNIQUE_ID(rcu))
 
Signature · 1 line
2.25.1
reviewer Reviewed-by on v1 1/1
#2 lore raw
In reply to Mathieu Desnoyers
Quoted 39 lines · On Wed, 3 May 2023 16:32:36 -0400 Mathieu Desnoyers…

linux/rcupdate.h macros use the *p parameter without parentheses, e.g.:

typeof(*p)

rather than

typeof(*(p))

The following test-case shows how it can generate confusion due to C operator precedence being reversed compared to the expectations:

    #define m(p) \
    do { \
            __typeof__(*p) v = 0; \
    } while (0)

    void fct(unsigned long long *p1)
    {
            m(p1 + 1);      /* works */
            m(1 + p1);      /* broken */
    }
Cc "Paul E. McKenney" <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Boqun Feng <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Zqiang <[email protected]>
---
 include/linux/rcupdate.h | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
include/linux/rcupdate.h +0 -0
index dcd2cf1e8326..1565012fa47f 100644

include/linux/rcupdate.h static inline void rcu_preempt_sleep_check(void) { } · line 433

Quoted 5 lines
include/linux/rcupdate.h +1 -1
@@ -430,16 +430,16 @@ static inline void rcu_preempt_sleep_check(void) { }
 
 #ifdef __CHECKER__
 #define rcu_check_sparse(p, space) \
-	((void)(((typeof(*p) space *)p) == p))
+	((void)(((typeof(*(p)) space *)p) == p))

Hmm, should we have that be:

	((void)(((typeof(*(p)) space *)(p)) == (p)))

In case of the 1 + p1, which would end up as:

	((void)(((typeof(*(1 + p1)) __rcu *)1 + p1 == 1 + p1;

I don't know how that __rcu get's passed around via the + statement there, so it may be fine. May not even make sense to have that. But I like to error on more parenthesis. ;-)

The rest looks fine.

-- Steve

Quoted 50 lines
include/linux/rcupdate.h +8 -8
 #else /* #ifdef __CHECKER__ */
 #define rcu_check_sparse(p, space)
 #endif /* #else #ifdef __CHECKER__ */
 
 #define __unrcu_pointer(p, local)					\
 ({									\
-	typeof(*p) *local = (typeof(*p) *__force)(p);			\
+	typeof(*(p)) *local = (typeof(*(p)) *__force)(p);		\
 	rcu_check_sparse(p, __rcu);					\
-	((typeof(*p) __force __kernel *)(local)); 			\
+	((typeof(*(p)) __force __kernel *)(local));			\
 })
 /**
  * unrcu_pointer - mark a pointer as not being RCU protected
@@ -452,29 +452,29 @@ static inline void rcu_preempt_sleep_check(void) { }
 
 #define __rcu_access_pointer(p, local, space) \
 ({ \
-	typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
+	typeof(*(p)) *local = (typeof(*(p)) *__force)READ_ONCE(p); \
 	rcu_check_sparse(p, space); \
-	((typeof(*p) __force __kernel *)(local)); \
+	((typeof(*(p)) __force __kernel *)(local)); \
 })
 #define __rcu_dereference_check(p, local, c, space) \
 ({ \
 	/* Dependency order vs. p above. */ \
-	typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
+	typeof(*(p)) *local = (typeof(*(p)) *__force)READ_ONCE(p); \
 	RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_check() usage"); \
 	rcu_check_sparse(p, space); \
-	((typeof(*p) __force __kernel *)(local)); \
+	((typeof(*(p)) __force __kernel *)(local)); \
 })
 #define __rcu_dereference_protected(p, local, c, space) \
 ({ \
 	RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_protected() usage"); \
 	rcu_check_sparse(p, space); \
-	((typeof(*p) __force __kernel *)(p)); \
+	((typeof(*(p)) __force __kernel *)(p)); \
 })
 #define __rcu_dereference_raw(p, local) \
 ({ \
 	/* Dependency order vs. p above. */ \
 	typeof(p) local = READ_ONCE(p); \
-	((typeof(*p) __force __kernel *)(local)); \
+	((typeof(*(p)) __force __kernel *)(local)); \
 })
 #define rcu_dereference_raw(p) __rcu_dereference_raw(p, __UNIQUE_ID(rcu))
 
maintainer Reviewed-by Boqun Feng Reviewed-by Steven Rostedt (Google) Reviewed-by Joel Fernandes (Google)
#3 lore raw
Quoted 55 lines · Steven Rostedt
Quoted 39 lines · On Wed, 3 May 2023 16:32:36 -0400 Mathieu Desnoyers…

linux/rcupdate.h macros use the *p parameter without parentheses, e.g.:

typeof(*p)

rather than

typeof(*(p))

The following test-case shows how it can generate confusion due to C operator precedence being reversed compared to the expectations:

    #define m(p) \
    do { \
            __typeof__(*p) v = 0; \
    } while (0)

    void fct(unsigned long long *p1)
    {
            m(p1 + 1);      /* works */
            m(1 + p1);      /* broken */
    }
Cc "Paul E. McKenney" <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Boqun Feng <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Zqiang <[email protected]>
---
 include/linux/rcupdate.h | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
include/linux/rcupdate.h +0 -0
index dcd2cf1e8326..1565012fa47f 100644

include/linux/rcupdate.h static inline void rcu_preempt_sleep_check(void) { } · line 433

Quoted 5 lines
include/linux/rcupdate.h +1 -1
@@ -430,16 +430,16 @@ static inline void rcu_preempt_sleep_check(void) { }
 
 #ifdef __CHECKER__
 #define rcu_check_sparse(p, space) \
-	((void)(((typeof(*p) space *)p) == p))
+	((void)(((typeof(*(p)) space *)p) == p))

Hmm, should we have that be:

	((void)(((typeof(*(p)) space *)(p)) == (p)))

In case of the 1 + p1, which would end up as:

	((void)(((typeof(*(1 + p1)) __rcu *)1 + p1 == 1 + p1;
Quoted 7 lines

I don't know how that __rcu get's passed around via the + statement there, so it may be fine. May not even make sense to have that. But I like to error on more parenthesis. ;-)

The rest looks fine.

Thank you all! I applied Steve's suggested change with attribution as shown below. Please let me know if I messed anything up.

Thanx, Paul

------------------------------------------------------------------------

commit d3d734216c88fb7c13205dc62178ff5011da415b

Author: Mathieu Desnoyers <[email protected]>
Date:   Wed May 3 16:32:36 2023 -0400

rcu: Add missing parentheses around rcu_dereference() "p" parameter

linux/rcupdate.h macros use the *p parameter without parentheses, e.g.:

typeof(*p)

rather than

typeof(*(p))

The following test-case shows how it can generate confusion due to C operator precedence being reversed compared to the expectations:

#define m(p) \
do { \
__typeof__(*p) v = 0; \
} while (0)

void fct(unsigned long long *p1)
{
m(p1 + 1); /* works */
m(1 + p1); /* broken */
}

[ paulmck: Apply Steve Rostedt additional () feedback. ]

include/linux/rcupdate.h +9 -9
index ddd42efc6224..cb938a89a923 100644
@@ -405,16 +405,16 @@ static inline void rcu_preempt_sleep_check(void) { }
 
 #ifdef __CHECKER__
 #define rcu_check_sparse(p, space) \
-	((void)(((typeof(*p) space *)p) == p))
+	((void)(((typeof(*(p)) space *)(p)) == (p)))
 #else /* #ifdef __CHECKER__ */
 #define rcu_check_sparse(p, space)
 #endif /* #else #ifdef __CHECKER__ */
 
 #define __unrcu_pointer(p, local)					\
 ({									\
-	typeof(*p) *local = (typeof(*p) *__force)(p);			\
+	typeof(*(p)) *local = (typeof(*(p)) *__force)(p);		\
 	rcu_check_sparse(p, __rcu);					\
-	((typeof(*p) __force __kernel *)(local)); 			\
+	((typeof(*(p)) __force __kernel *)(local));			\
 })
 /**
  * unrcu_pointer - mark a pointer as not being RCU protected
@@ -427,29 +427,29 @@ static inline void rcu_preempt_sleep_check(void) { }
 
 #define __rcu_access_pointer(p, local, space) \
 ({ \
-	typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
+	typeof(*(p)) *local = (typeof(*(p)) *__force)READ_ONCE(p); \
 	rcu_check_sparse(p, space); \
-	((typeof(*p) __force __kernel *)(local)); \
+	((typeof(*(p)) __force __kernel *)(local)); \
 })
 #define __rcu_dereference_check(p, local, c, space) \
 ({ \
 	/* Dependency order vs. p above. */ \
-	typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
+	typeof(*(p)) *local = (typeof(*(p)) *__force)READ_ONCE(p); \
 	RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_check() usage"); \
 	rcu_check_sparse(p, space); \
-	((typeof(*p) __force __kernel *)(local)); \
+	((typeof(*(p)) __force __kernel *)(local)); \
 })
 #define __rcu_dereference_protected(p, local, c, space) \
 ({ \
 	RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_protected() usage"); \
 	rcu_check_sparse(p, space); \
-	((typeof(*p) __force __kernel *)(p)); \
+	((typeof(*(p)) __force __kernel *)(p)); \
 })
 #define __rcu_dereference_raw(p, local) \
 ({ \
 	/* Dependency order vs. p above. */ \
 	typeof(p) local = READ_ONCE(p); \
-	((typeof(*p) __force __kernel *)(local)); \
+	((typeof(*(p)) __force __kernel *)(local)); \
 })
 #define rcu_dereference_raw(p) __rcu_dereference_raw(p, __UNIQUE_ID(rcu))
 
author reviewer
#4 lore raw
Quoted 60 lines · Paul E. McKenney
Quoted 59 lines · Steven Rostedt
Quoted 39 lines · On Wed, 3 May 2023 16:32:36 -0400 Mathieu Desnoyers…

linux/rcupdate.h macros use the *p parameter without parentheses, e.g.:

   typeof(*p)

rather than

   typeof(*(p))

The following test-case shows how it can generate confusion due to C operator precedence being reversed compared to the expectations:

     #define m(p) \
     do { \
             __typeof__(*p) v = 0; \
     } while (0)

     void fct(unsigned long long *p1)
     {
             m(p1 + 1);      /* works */
             m(1 + p1);      /* broken */
     }
Cc "Paul E. McKenney" <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Boqun Feng <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Zqiang <[email protected]>
---
  include/linux/rcupdate.h | 18 +++++++++---------
  1 file changed, 9 insertions(+), 9 deletions(-)
include/linux/rcupdate.h +0 -0
index dcd2cf1e8326..1565012fa47f 100644

include/linux/rcupdate.h static inline void rcu_preempt_sleep_check(void) { } · line 433

Quoted 5 lines
include/linux/rcupdate.h +1 -1
@@ -430,16 +430,16 @@ static inline void rcu_preempt_sleep_check(void) { }
 
  #ifdef __CHECKER__
  #define rcu_check_sparse(p, space) \
-	((void)(((typeof(*p) space *)p) == p))
+	((void)(((typeof(*(p)) space *)p) == p))

Hmm, should we have that be:

	((void)(((typeof(*(p)) space *)(p)) == (p)))

In case of the 1 + p1, which would end up as:

	((void)(((typeof(*(1 + p1)) __rcu *)1 + p1 == 1 + p1;

I don't know how that __rcu get's passed around via the + statement there, so it may be fine. May not even make sense to have that. But I like to error on more parenthesis. ;-)

Quoted 6 lines
Quoted 3 lines

The rest looks fine.

Thank you all! I applied Steve's suggested change with attribution as shown below. Please let me know if I messed anything up.

Hi Paul,

I've done a new version of that patch which fixes other issues in rcupdate.h in the next round. Can you hold merging this until I remove the "RFC PATCH" tag please ? My goal is to gather feedback first to make sure everyone is OK with the overall changes across headers, so everything can become consistent.

Thanks,

Mathieu

Quoted 108 lines

Thanx, Paul

------------------------------------------------------------------------

commit d3d734216c88fb7c13205dc62178ff5011da415b

Author: Mathieu Desnoyers <[email protected]>
Date:   Wed May 3 16:32:36 2023 -0400

rcu: Add missing parentheses around rcu_dereference() "p" parameter

linux/rcupdate.h macros use the *p parameter without parentheses, e.g.:

typeof(*p)

rather than

typeof(*(p))

The following test-case shows how it can generate confusion due to C operator precedence being reversed compared to the expectations:

#define m(p) \
do { \
__typeof__(*p) v = 0; \
} while (0)

void fct(unsigned long long *p1)
{
m(p1 + 1); /* works */
m(1 + p1); /* broken */
}

[ paulmck: Apply Steve Rostedt additional () feedback. ]

include/linux/rcupdate.h +9 -9
index ddd42efc6224..cb938a89a923 100644
@@ -405,16 +405,16 @@ static inline void rcu_preempt_sleep_check(void) { }
 
  #ifdef __CHECKER__
  #define rcu_check_sparse(p, space) \
-	((void)(((typeof(*p) space *)p) == p))
+	((void)(((typeof(*(p)) space *)(p)) == (p)))
  #else /* #ifdef __CHECKER__ */
  #define rcu_check_sparse(p, space)
  #endif /* #else #ifdef __CHECKER__ */
 
  #define __unrcu_pointer(p, local)					\
  ({									\
-	typeof(*p) *local = (typeof(*p) *__force)(p);			\
+	typeof(*(p)) *local = (typeof(*(p)) *__force)(p);		\
  	rcu_check_sparse(p, __rcu);					\
-	((typeof(*p) __force __kernel *)(local)); 			\
+	((typeof(*(p)) __force __kernel *)(local));			\
  })
  /**
   * unrcu_pointer - mark a pointer as not being RCU protected
@@ -427,29 +427,29 @@ static inline void rcu_preempt_sleep_check(void) { }
 
  #define __rcu_access_pointer(p, local, space) \
  ({ \
-	typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
+	typeof(*(p)) *local = (typeof(*(p)) *__force)READ_ONCE(p); \
  	rcu_check_sparse(p, space); \
-	((typeof(*p) __force __kernel *)(local)); \
+	((typeof(*(p)) __force __kernel *)(local)); \
  })
  #define __rcu_dereference_check(p, local, c, space) \
  ({ \
  	/* Dependency order vs. p above. */ \
-	typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
+	typeof(*(p)) *local = (typeof(*(p)) *__force)READ_ONCE(p); \
  	RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_check() usage"); \
  	rcu_check_sparse(p, space); \
-	((typeof(*p) __force __kernel *)(local)); \
+	((typeof(*(p)) __force __kernel *)(local)); \
  })
  #define __rcu_dereference_protected(p, local, c, space) \
  ({ \
  	RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_protected() usage"); \
  	rcu_check_sparse(p, space); \
-	((typeof(*p) __force __kernel *)(p)); \
+	((typeof(*(p)) __force __kernel *)(p)); \
  })
  #define __rcu_dereference_raw(p, local) \
  ({ \
  	/* Dependency order vs. p above. */ \
  	typeof(p) local = READ_ONCE(p); \
-	((typeof(*p) __force __kernel *)(local)); \
+	((typeof(*(p)) __force __kernel *)(local)); \
  })
  #define rcu_dereference_raw(p) __rcu_dereference_raw(p, __UNIQUE_ID(rcu))
 
Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
maintainer
#5 lore raw
Quoted 67 lines · Mathieu Desnoyers
Quoted 59 lines · Paul E. McKenney
Quoted 58 lines · Steven Rostedt
Quoted 39 lines · On Wed, 3 May 2023 16:32:36 -0400 Mathieu Desnoyers…

linux/rcupdate.h macros use the *p parameter without parentheses, e.g.:

   typeof(*p)

rather than

   typeof(*(p))

The following test-case shows how it can generate confusion due to C operator precedence being reversed compared to the expectations:

     #define m(p) \
     do { \
             __typeof__(*p) v = 0; \
     } while (0)

     void fct(unsigned long long *p1)
     {
             m(p1 + 1);      /* works */
             m(1 + p1);      /* broken */
     }
Cc "Paul E. McKenney" <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Boqun Feng <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Zqiang <[email protected]>
---
  include/linux/rcupdate.h | 18 +++++++++---------
  1 file changed, 9 insertions(+), 9 deletions(-)
include/linux/rcupdate.h +0 -0
index dcd2cf1e8326..1565012fa47f 100644

include/linux/rcupdate.h static inline void rcu_preempt_sleep_check(void) { } · line 432

Quoted 4 lines
include/linux/rcupdate.h +1 -1
@@ -430,16 +430,16 @@ static inline void rcu_preempt_sleep_check(void) { }
  #ifdef __CHECKER__
  #define rcu_check_sparse(p, space) \
-	((void)(((typeof(*p) space *)p) == p))
+	((void)(((typeof(*(p)) space *)p) == p))

Hmm, should we have that be:

	((void)(((typeof(*(p)) space *)(p)) == (p)))

In case of the 1 + p1, which would end up as:

	((void)(((typeof(*(1 + p1)) __rcu *)1 + p1 == 1 + p1;

I don't know how that __rcu get's passed around via the + statement there, so it may be fine. May not even make sense to have that. But I like to error on more parenthesis. ;-)

Quoted 6 lines
Quoted 3 lines

The rest looks fine.

Thank you all! I applied Steve's suggested change with attribution as shown below. Please let me know if I messed anything up.

Quoted 7 lines

Hi Paul,

I've done a new version of that patch which fixes other issues in rcupdate.h in the next round. Can you hold merging this until I remove the "RFC PATCH" tag please ? My goal is to gather feedback first to make sure everyone is OK with the overall changes across headers, so everything can become consistent.

Hello, Mathieu,

Very well, I have removed it.

Thanx, Paul

Quoted 105 lines

Thanks,

Mathieu

Quoted 95 lines

Thanx, Paul

------------------------------------------------------------------------

commit d3d734216c88fb7c13205dc62178ff5011da415b

Author: Mathieu Desnoyers <[email protected]>
Date:   Wed May 3 16:32:36 2023 -0400

rcu: Add missing parentheses around rcu_dereference() "p" parameter linux/rcupdate.h macros use the *p parameter without parentheses, e.g.:
typeof(*p)
rather than
typeof(*(p))
The following test-case shows how it can generate confusion due to C operator precedence being reversed compared to the expectations:
#define m(p) \
do { \
__typeof__(*p) v = 0; \
} while (0)
void fct(unsigned long long *p1)
{
m(p1 + 1); /* works */
m(1 + p1); /* broken */
}

[ paulmck: Apply Steve Rostedt additional () feedback. ]
Cc "Paul E. McKenney" <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Boqun Feng <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Zqiang <[email protected]>
include/linux/rcupdate.h +9 -9
index ddd42efc6224..cb938a89a923 100644
@@ -405,16 +405,16 @@ static inline void rcu_preempt_sleep_check(void) { }
  #ifdef __CHECKER__
  #define rcu_check_sparse(p, space) \
-	((void)(((typeof(*p) space *)p) == p))
+	((void)(((typeof(*(p)) space *)(p)) == (p)))
  #else /* #ifdef __CHECKER__ */
  #define rcu_check_sparse(p, space)
  #endif /* #else #ifdef __CHECKER__ */
  #define __unrcu_pointer(p, local)					\
  ({									\
-	typeof(*p) *local = (typeof(*p) *__force)(p);			\
+	typeof(*(p)) *local = (typeof(*(p)) *__force)(p);		\
  	rcu_check_sparse(p, __rcu);					\
-	((typeof(*p) __force __kernel *)(local)); 			\
+	((typeof(*(p)) __force __kernel *)(local));			\
  })
  /**
   * unrcu_pointer - mark a pointer as not being RCU protected
@@ -427,29 +427,29 @@ static inline void rcu_preempt_sleep_check(void) { }
  #define __rcu_access_pointer(p, local, space) \
  ({ \
-	typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
+	typeof(*(p)) *local = (typeof(*(p)) *__force)READ_ONCE(p); \
  	rcu_check_sparse(p, space); \
-	((typeof(*p) __force __kernel *)(local)); \
+	((typeof(*(p)) __force __kernel *)(local)); \
  })
  #define __rcu_dereference_check(p, local, c, space) \
  ({ \
  	/* Dependency order vs. p above. */ \
-	typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
+	typeof(*(p)) *local = (typeof(*(p)) *__force)READ_ONCE(p); \
  	RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_check() usage"); \
  	rcu_check_sparse(p, space); \
-	((typeof(*p) __force __kernel *)(local)); \
+	((typeof(*(p)) __force __kernel *)(local)); \
  })
  #define __rcu_dereference_protected(p, local, c, space) \
  ({ \
  	RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_protected() usage"); \
  	rcu_check_sparse(p, space); \
-	((typeof(*p) __force __kernel *)(p)); \
+	((typeof(*(p)) __force __kernel *)(p)); \
  })
  #define __rcu_dereference_raw(p, local) \
  ({ \
  	/* Dependency order vs. p above. */ \
  	typeof(p) local = READ_ONCE(p); \
-	((typeof(*p) __force __kernel *)(local)); \
+	((typeof(*(p)) __force __kernel *)(local)); \
  })
  #define rcu_dereference_raw(p) __rcu_dereference_raw(p, __UNIQUE_ID(rcu))
Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
Reviewed-by on v1 1/1
#6 lore raw
In reply to Mathieu Desnoyers
Quoted 23 lines · Mathieu Desnoyers

linux/rcupdate.h macros use the *p parameter without parentheses, e.g.:

typeof(*p)

rather than

typeof(*(p))

The following test-case shows how it can generate confusion due to C operator precedence being reversed compared to the expectations:

    #define m(p) \
    do { \
            __typeof__(*p) v = 0; \
    } while (0)

    void fct(unsigned long long *p1)
    {
            m(p1 + 1);      /* works */
            m(1 + p1);      /* broken */
    }
Quoted 8 lines
Cc "Paul E. McKenney" <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Boqun Feng <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Zqiang <[email protected]>

thanks,

- Joel

Quoted 67 lines
---
 include/linux/rcupdate.h | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
include/linux/rcupdate.h +9 -10
index dcd2cf1e8326..1565012fa47f 100644
@@ -430,16 +430,16 @@ static inline void rcu_preempt_sleep_check(void) { }
 
 #ifdef __CHECKER__
 #define rcu_check_sparse(p, space) \
-       ((void)(((typeof(*p) space *)p) == p))
+       ((void)(((typeof(*(p)) space *)p) == p))
 #else /* #ifdef __CHECKER__ */
 #define rcu_check_sparse(p, space)
 #endif /* #else #ifdef __CHECKER__ */
 
 #define __unrcu_pointer(p, local)                                      \
 ({                                                                     \
-       typeof(*p) *local = (typeof(*p) *__force)(p);                   \
+       typeof(*(p)) *local = (typeof(*(p)) *__force)(p);               \
        rcu_check_sparse(p, __rcu);                                     \
-       ((typeof(*p) __force __kernel *)(local));                       \
+       ((typeof(*(p)) __force __kernel *)(local));                     \
 })
 /**
  * unrcu_pointer - mark a pointer as not being RCU protected
@@ -452,29 +452,29 @@ static inline void rcu_preempt_sleep_check(void) { }
 
 #define __rcu_access_pointer(p, local, space) \
 ({ \
-       typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
+       typeof(*(p)) *local = (typeof(*(p)) *__force)READ_ONCE(p); \
        rcu_check_sparse(p, space); \
-       ((typeof(*p) __force __kernel *)(local)); \
+       ((typeof(*(p)) __force __kernel *)(local)); \
 })
 #define __rcu_dereference_check(p, local, c, space) \
 ({ \
        /* Dependency order vs. p above. */ \
-       typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
+       typeof(*(p)) *local = (typeof(*(p)) *__force)READ_ONCE(p); \
        RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_check() usage"); \
        rcu_check_sparse(p, space); \
-       ((typeof(*p) __force __kernel *)(local)); \
+       ((typeof(*(p)) __force __kernel *)(local)); \
 })
 #define __rcu_dereference_protected(p, local, c, space) \
 ({ \
        RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_protected() usage"); \
        rcu_check_sparse(p, space); \
-       ((typeof(*p) __force __kernel *)(p)); \
+       ((typeof(*(p)) __force __kernel *)(p)); \
 })
 #define __rcu_dereference_raw(p, local) \
 ({ \
        /* Dependency order vs. p above. */ \
        typeof(p) local = READ_ONCE(p); \
-       ((typeof(*p) __force __kernel *)(local)); \
+       ((typeof(*(p)) __force __kernel *)(local)); \
 })
 #define rcu_dereference_raw(p) __rcu_dereference_raw(p, __UNIQUE_ID(rcu))
 
--

2.25.1