Skip to contentSkip to conversation

Fix three Clang context analysis issues

PATCH In review Bart Van Assche 3 participants 13 messages Nov 16 – Nov 18 Sleepable read-copy update (SRCU)

lore
Share & tools
lore
#0 lore raw

cover Fix three Clang context analysis issues

Hi Peter and Marco,

This patch series includes three context analysis fixes:
- bit_spinlock: Surround the bitnum argument and macro expansion in __bitlock() with parentheses to avoid operator precedence issues.
- cleanup: Mark __free() helper functions generated by DEFINE_FREE() as __context_unsafe() because cleanup expressions that unlock trigger Clang thread-safety false positives.
- srcu: Move __srcu_read_lock_must_hold() out of srcu_dereference_check() and into srcu_dereference() and srcu_dereference_notrace() so update-side callers satisfying the lockdep condition do not fail context analysis.

Please consider this patch series for the next merge window.

Thanks,

Bart.

Changes compared to v2:
- Split out only the three standalone context analysis fixes (v2 patches 1, 2, and 4) into this series.
- Dropped the lockdep and RCU assertion behavior changes (v2 patches 8-14: lockdep_assume_*(), lockdep_assume_in_rcu_*(), and removing context assumptions from lockdep_assert_held*() and lockdep_assert_in_rcu_*()), as agreed in the review discussion [2].
- Dropped the preparatory __must_hold*() annotation patches (v2 patches 5-7) and the tracing branch profiling Kconfig patch (v2 patch 3) from this series.
- Updated the commit message of "cleanup: Disable context analysis for __free() helpers" with concrete examples of __free() unlock helpers in response to review feedback.
- Rebased onto current mainline.

Bart Van Assche (3):
bit_spinlock: Fix the __bitlock() definition
cleanup: Disable context analysis for __free() helpers
srcu: Remove check from srcu_dereference_check()

 include/linux/bit_spinlock.h |  2 +-
 include/linux/cleanup.h      |  6 ++++--
 include/linux/srcu.h         | 20 +++++++++++++++-----
 3 files changed, 20 insertions(+), 8 deletions(-)
#1 lore raw

PATCH v3 1/3 bit_spinlock: Fix the __bitlock() definition

Surround the bitnum argument and the entire __bitlock() macro expansion with parentheses because bitnum could be an expression that includes an operator with a lower precedence than the addition operator (e.g. the exclusive-or operator) and to prevent operator precedence issues in callers.

Fixes eb7d96a13bf4 ("bit_spinlock: Support Clang's context analysis")
---
 include/linux/bit_spinlock.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/bit_spinlock.h +1 -1
index 7869a6e59b6a..c31d4f4d09ee 100644
@@ -17,7 +17,7 @@
  * static analysis can use.
  */
 context_lock_struct(__context_bitlock) { };
-#define __bitlock(bitnum, addr) (struct __context_bitlock *)(bitnum + (addr))
+#define __bitlock(bitnum, addr) ((struct __context_bitlock *)((bitnum) + (addr)))
 
 /*
  *  bit-based spin_lock()
#2 lore raw

Re: [PATCH v3 1/3] bit_spinlock: Fix the __bitlock() definition

Quoted 10 lines · Bart Van Assche

Surround the bitnum argument and the entire __bitlock() macro expansion with parentheses because bitnum could be an expression that includes an operator with a lower precedence than the addition operator (e.g. the exclusive-or operator) and to prevent operator precedence issues in callers.

Fixes eb7d96a13bf4 ("bit_spinlock: Support Clang's context analysis")

My Reviewed-by on this is still valid.

Thanks,
-- Marco

#3 lore raw

PATCH v3 2/3 cleanup: Disable context analysis for __free() helpers

In reply to Bart Van Assche

The __free_##_name() helper functions defined by DEFINE_FREE() call the free expression that has been passed to DEFINE_FREE(). If that expression releases a lock, e.g. because it is a call to an unlock function, the Clang thread-safety analyzer complains because it cannot know that the variable that is passed to the helper function holds a lock. This is needed by:
- __drm_for_each_bridge_in_chain_cleanup (include/drm/drm_bridge.h), used by drm_for_each_bridge_in_chain_scoped() and drm_for_each_bridge_in_chain_from_scoped().
- netdev_unlock (net/core/dev.h), used by for_each_netdev_lock_scoped().
- netdev_unlock_ops_compat (net/core/dev.h), used by for_each_netdev_lock_ops_compat_scoped().

Disable context analysis for the __free() helper functions.

---
 include/linux/cleanup.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
include/linux/cleanup.h +4 -2
index 1fb8058b897d..f6afe80781a4 100644
@@ -207,8 +207,10 @@
  * Without the NULL test it turns into a mess and the compiler can't help us.
  */
 
-#define DEFINE_FREE(_name, _type, _free) \
-	static __always_inline void __free_##_name(void *p) { _type _T = *(_type *)p; _free; }
+#define DEFINE_FREE(_name, _type, _free)				\
+	static __always_inline void __free_##_name(void *p)		\
+		__context_unsafe(/* _free may release a lock */)		\
+	{ _type _T = *(_type *)p; _free; }
 
 #define __free(_name)	__cleanup(__free_##_name)
 
#4 lore raw

PATCH v3 3/3 srcu: Remove check from srcu_dereference_check()

In reply to Bart Van Assche

Users of srcu_dereference_check() must either hold an SRCU reader lock or satisfy the lockdep condition 'c', e.g. by holding the update-side lock. However, srcu_dereference_check() calls __srcu_read_lock_must_hold(), which tells the Clang context analyzer that the SRCU reader lock must be held. That requirement is too strict for update-side callers. Hence, remove the __srcu_read_lock_must_hold() call from srcu_dereference_check() while keeping the fake RCU acquire and release around __rcu_dereference_check(). Retain the runtime lockdep check in srcu_dereference_check().

srcu_dereference() and srcu_dereference_notrace() do not accept a lockdep condition and hence may only be used by SRCU readers. Call __srcu_read_lock_must_hold() from these macros such that Clang context analysis keeps verifying at compile time that their callers hold the SRCU reader lock.

Cc Marco Elver <[email protected]>
Fixes f0b7ce22d718 ("srcu: Support Clang's context analysis")
---
 include/linux/srcu.h | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
include/linux/srcu.h +15 -5
index d4dcaeb83e2b..5ef07357507b 100644
@@ -278,8 +278,11 @@ static inline int srcu_read_lock_held(const struct srcu_struct *ssp)
  * No-op helper to denote that ssp must be held. Because SRCU-protected pointers
  * should still be marked with __rcu_guarded, and we do not want to mark them
  * with __guarded_by(ssp) as it would complicate annotations for writers, we
- * choose the following strategy: srcu_dereference_check() calls this helper
- * that checks that the passed ssp is held, and then fake-acquires 'RCU'.
+ * choose the following strategy: srcu_dereference() and
+ * srcu_dereference_notrace() call this helper that checks that the passed ssp
+ * is held, and srcu_dereference_check() fake-acquires 'RCU'.
+ * srcu_dereference_check() does not call this helper because its callers may
+ * satisfy the condition @c instead of holding ssp, e.g. on the update side.
  */
 static inline void __srcu_read_lock_must_hold(const struct srcu_struct *ssp) __must_hold_shared(ssp) { }
 
@@ -297,7 +300,6 @@ static inline void __srcu_read_lock_must_hold(const struct srcu_struct *ssp) __m
  */
 #define srcu_dereference_check(p, ssp, c)					\
 ({										\
-	__srcu_read_lock_must_hold(ssp);					\
 	__acquire_shared_ctx_lock(RCU);					\
 	__auto_type __v = __rcu_dereference_check((p), __UNIQUE_ID(rcu),	\
 				(c) || srcu_read_lock_held(ssp), __rcu);	\
@@ -315,7 +317,11 @@ static inline void __srcu_read_lock_must_hold(const struct srcu_struct *ssp) __m
  * is enabled, invoking this outside of an RCU read-side critical
  * section will result in an RCU-lockdep splat.
  */
-#define srcu_dereference(p, ssp) srcu_dereference_check((p), (ssp), 0)
+#define srcu_dereference(p, ssp)					\
+({									\
+	__srcu_read_lock_must_hold(ssp);				\
+	srcu_dereference_check((p), (ssp), 0);				\
+})
 
 /**
  * srcu_dereference_notrace - no tracing and no lockdep calls from here
@@ -323,7 +329,11 @@ static inline void __srcu_read_lock_must_hold(const struct srcu_struct *ssp) __m
  * @ssp: pointer to the srcu_struct, which is used to check that we
  *	really are in an SRCU read-side critical section.
  */
-#define srcu_dereference_notrace(p, ssp) srcu_dereference_check((p), (ssp), 1)
+#define srcu_dereference_notrace(p, ssp)				\
+({									\
+	__srcu_read_lock_must_hold(ssp);				\
+	srcu_dereference_check((p), (ssp), 1);				\
+})
 
 /**
  * srcu_read_lock - register a new reader for an SRCU-protected structure.
maintainer
#5 lore raw

Re: [PATCH v3 3/3] srcu: Remove check from srcu_dereference_check()

Quoted 16 lines · Bart Van Assche

Users of srcu_dereference_check() must either hold an SRCU reader lock or satisfy the lockdep condition 'c', e.g. by holding the update-side lock. However, srcu_dereference_check() calls __srcu_read_lock_must_hold(), which tells the Clang context analyzer that the SRCU reader lock must be held. That requirement is too strict for update-side callers. Hence, remove the __srcu_read_lock_must_hold() call from srcu_dereference_check() while keeping the fake RCU acquire and release around __rcu_dereference_check(). Retain the runtime lockdep check in srcu_dereference_check().

srcu_dereference() and srcu_dereference_notrace() do not accept a lockdep condition and hence may only be used by SRCU readers. Call __srcu_read_lock_must_hold() from these macros such that Clang context analysis keeps verifying at compile time that their callers hold the SRCU reader lock.

Quoted 3 lines
Cc Marco Elver <[email protected]>
Fixes f0b7ce22d718 ("srcu: Support Clang's context analysis")

I clearly wasn't paying attention, and thank you for finding this!

Quoted 23 lines
---
 include/linux/srcu.h | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
include/linux/srcu.h +5 -2
index d4dcaeb83e2b..5ef07357507b 100644
@@ -278,8 +278,11 @@ static inline int srcu_read_lock_held(const struct srcu_struct *ssp)
  * No-op helper to denote that ssp must be held. Because SRCU-protected pointers
  * should still be marked with __rcu_guarded, and we do not want to mark them
  * with __guarded_by(ssp) as it would complicate annotations for writers, we
- * choose the following strategy: srcu_dereference_check() calls this helper
- * that checks that the passed ssp is held, and then fake-acquires 'RCU'.
+ * choose the following strategy: srcu_dereference() and
+ * srcu_dereference_notrace() call this helper that checks that the passed ssp
+ * is held, and srcu_dereference_check() fake-acquires 'RCU'.
+ * srcu_dereference_check() does not call this helper because its callers may
+ * satisfy the condition @c instead of holding ssp, e.g. on the update side.
  */
 static inline void __srcu_read_lock_must_hold(const struct srcu_struct *ssp) __must_hold_shared(ssp) { }
 

include/linux/srcu.h static inline void __srcu_read_lock_must_hold(const struct srcu_struct *ssp) __m · line 300

Quoted 4 lines
include/linux/srcu.h +0 -1
@@ -297,7 +300,6 @@ static inline void __srcu_read_lock_must_hold(const struct srcu_struct *ssp) __m
  */
 #define srcu_dereference_check(p, ssp, c)					\
 ({										\
-	__srcu_read_lock_must_hold(ssp);					\

Should we invoke __srcu_read_lock_must_hold() conditionally, only if the value of c is zero?

Thanx, Paul

Quoted 29 lines
include/linux/srcu.h +10 -2
 	__acquire_shared_ctx_lock(RCU);					\
 	__auto_type __v = __rcu_dereference_check((p), __UNIQUE_ID(rcu),	\
 				(c) || srcu_read_lock_held(ssp), __rcu);	\
@@ -315,7 +317,11 @@ static inline void __srcu_read_lock_must_hold(const struct srcu_struct *ssp) __m
  * is enabled, invoking this outside of an RCU read-side critical
  * section will result in an RCU-lockdep splat.
  */
-#define srcu_dereference(p, ssp) srcu_dereference_check((p), (ssp), 0)
+#define srcu_dereference(p, ssp)					\
+({									\
+	__srcu_read_lock_must_hold(ssp);				\
+	srcu_dereference_check((p), (ssp), 0);				\
+})
 
 /**
  * srcu_dereference_notrace - no tracing and no lockdep calls from here
@@ -323,7 +329,11 @@ static inline void __srcu_read_lock_must_hold(const struct srcu_struct *ssp) __m
  * @ssp: pointer to the srcu_struct, which is used to check that we
  *	really are in an SRCU read-side critical section.
  */
-#define srcu_dereference_notrace(p, ssp) srcu_dereference_check((p), (ssp), 1)
+#define srcu_dereference_notrace(p, ssp)				\
+({									\
+	__srcu_read_lock_must_hold(ssp);				\
+	srcu_dereference_check((p), (ssp), 1);				\
+})
 
 /**
  * srcu_read_lock - register a new reader for an SRCU-protected structure.
#6 lore raw

Re: [PATCH v3 3/3] srcu: Remove check from srcu_dereference_check()

Quoted 2 lines · Paul E. McKenney

Should we invoke __srcu_read_lock_must_hold() conditionally, only if the value of c is zero?

Hi Paul,

Thanks for the review. I don't think that would help. The Clang context analysis is a compile-time analysis. It does not know the runtime value of 'c', so a conditional call to __srcu_read_lock_must_hold() only behaves differently from an unconditional call if the compiler can evaluate 'c' at compile time:

- If 'c' is the constant 0, Clang removes the always-false edge and the call is checked. This is what srcu_dereference() does with this patch.

- If 'c' is the constant 1, the call is never checked. This is what srcu_dereference_notrace() would get. srcu_dereference_notrace() calls __srcu_read_lock_must_hold() directly, so it keeps the check.

- If 'c' is a real lockdep expression, e.g. lockdep_is_held(&foo->mutex), Clang can't evaluate it. The call stays reachable and is checked. That brings back the false positive for update-side callers that this patch fixes. With CONFIG_LOCKDEP=n, lockdep_is_held() becomes
({ (void)(lock); 1; }), so the result would also depend on the kernel config.

I checked this with a small test program built with clang -Wthread-safety:

#define deref_check(c) ({ if (!(c)) must_hold(&s); 0; })

deref_check(0); /* warns, as expected */
deref_check(1); /* no warning */
deref_check(is_held(&m)); /* warns: false positive */
deref_check(({ (void)&m; 1; })) /* no warning (lockdep off) */

A conditional call would also evaluate 'c' outside the lockdep checks, so it would run even when lockdep is disabled. Today 'c' is only evaluated inside __rcu_dereference_check() when CONFIG_PROVE_RCU=y.

So the only case where the conditional call helps is a constant 0, and srcu_dereference() already covers that. Callers that pass a literal 0 directly to srcu_dereference_check() can switch to srcu_dereference(). The runtime check is unchanged: lockdep still checks '(c) || srcu_read_lock_held(ssp)'.

Thanks,

Bart.

maintainer
#7 lore raw

Re: [PATCH v3 3/3] srcu: Remove check from srcu_dereference_check()

Quoted 44 lines · Bart Van Assche
Quoted 2 lines · Paul E. McKenney

Should we invoke __srcu_read_lock_must_hold() conditionally, only if the value of c is zero?

Hi Paul,

Thanks for the review. I don't think that would help. The Clang context analysis is a compile-time analysis. It does not know the runtime value of 'c', so a conditional call to __srcu_read_lock_must_hold() only behaves differently from an unconditional call if the compiler can evaluate 'c' at compile time:

- If 'c' is the constant 0, Clang removes the always-false edge and the call is checked. This is what srcu_dereference() does with this patch.

- If 'c' is the constant 1, the call is never checked. This is what srcu_dereference_notrace() would get. srcu_dereference_notrace() calls __srcu_read_lock_must_hold() directly, so it keeps the check.

- If 'c' is a real lockdep expression, e.g. lockdep_is_held(&foo->mutex), Clang can't evaluate it. The call stays reachable and is checked. That brings back the false positive for update-side callers that this patch fixes. With CONFIG_LOCKDEP=n, lockdep_is_held() becomes
({ (void)(lock); 1; }), so the result would also depend on the kernel config.

I checked this with a small test program built with clang -Wthread-safety:

#define deref_check(c) ({ if (!(c)) must_hold(&s); 0; })

deref_check(0); /* warns, as expected */
deref_check(1); /* no warning */
deref_check(is_held(&m)); /* warns: false positive */
deref_check(({ (void)&m; 1; })) /* no warning (lockdep off) */

A conditional call would also evaluate 'c' outside the lockdep checks, so it would run even when lockdep is disabled. Today 'c' is only evaluated inside __rcu_dereference_check() when CONFIG_PROVE_RCU=y.

Quoted 5 lines

So the only case where the conditional call helps is a constant 0, and srcu_dereference() already covers that. Callers that pass a literal 0 directly to srcu_dereference_check() can switch to srcu_dereference(). The runtime check is unchanged: lockdep still checks '(c) || srcu_read_lock_held(ssp)'.

Would __builtin_constant_p() help here?

If we don't have a build-time constant, don't check.

If we do have a build-time constant, check only if that constant has the value zero, that is, there is no update-side protection.

Or am I still confused about what this is trying to do?

Thanx, Paul

#8 lore raw

Re: [PATCH v3 3/3] srcu: Remove check from srcu_dereference_check()

Quoted 6 lines · Paul E. McKenney

Would __builtin_constant_p() help here?

If we don't have a build-time constant, don't check.

If we do have a build-time constant, check only if that constant has the value zero, that is, there is no update-side protection.

Hi Paul,

Yes, that helps. I had missed this option. Something like the following does what you describe:

	#define srcu_dereference_check(p, ssp, c)			\
	({								\
		if (__builtin_constant_p(c) && !(c))			\
			__srcu_read_lock_must_hold(ssp);		\
		__acquire_shared_ctx_lock(RCU);				\
		...

This also fixes the runtime problem I mentioned: the argument of __builtin_constant_p() is never evaluated, and '!(c)' is only evaluated if 'c' is a constant.

I tested this with clang -Wthread-safety at -O0 and -O2:

	c == 0                            -> checked (warns without
	                                     the SRCU read lock)
	c == 1                            -> not checked
	c == lockdep_is_held(&m)          -> not checked
	c == ({ (void)(&m); 1; })         -> not checked
	     (lockdep_is_held() with CONFIG_LOCKDEP=n)

There is one limitation. If 'c' is a plain variable, function argument or structure member, e.g. srcu_dereference_check(p, ssp, foo->flag), Clang does not resolve __builtin_constant_p() in the front end. It leaves that to the optimizer, which may still find a constant later. The context analysis then treats the call to __srcu_read_lock_must_hold() as reachable and reports a false positive. Clang does resolve __builtin_constant_p() to 0 when 'c' contains a function call, and that covers lockdep_is_held(), srcu_read_lock_held() and refcount_read(). In the current tree, every srcu_dereference_check() caller passes either a constant or an expression built from such calls, so this limitation doesn't affect any existing caller.

So in v4 I plan to:
- call __srcu_read_lock_must_hold() from srcu_dereference_check() only if 'c' is the constant 0, as shown above;
- keep srcu_dereference() defined as srcu_dereference_check((p), (ssp), 0), which now gets the check automatically;
- keep the explicit __srcu_read_lock_must_hold() call in srcu_dereference_notrace(), because it passes 1 for 'c';
- update the comment above __srcu_read_lock_must_hold() and the patch description to match.

Does that sound good to you?

Thanks,

Bart.

maintainer
#9 lore raw

Re: [PATCH v3 3/3] srcu: Remove check from srcu_dereference_check()

Quoted 33 lines · Bart Van Assche
Quoted 6 lines · Paul E. McKenney

Would __builtin_constant_p() help here?

If we don't have a build-time constant, don't check.

If we do have a build-time constant, check only if that constant has the value zero, that is, there is no update-side protection.

Hi Paul,

Yes, that helps. I had missed this option. Something like the following does what you describe:

	#define srcu_dereference_check(p, ssp, c)			\
	({								\
		if (__builtin_constant_p(c) && !(c))			\
			__srcu_read_lock_must_hold(ssp);		\
		__acquire_shared_ctx_lock(RCU);				\
		...

This also fixes the runtime problem I mentioned: the argument of __builtin_constant_p() is never evaluated, and '!(c)' is only evaluated if 'c' is a constant.

I tested this with clang -Wthread-safety at -O0 and -O2:

	c == 0                            -> checked (warns without
	                                     the SRCU read lock)
	c == 1                            -> not checked
	c == lockdep_is_held(&m)          -> not checked
	c == ({ (void)(&m); 1; })         -> not checked
	     (lockdep_is_held() with CONFIG_LOCKDEP=n)
Quoted 12 lines

There is one limitation. If 'c' is a plain variable, function argument or structure member, e.g. srcu_dereference_check(p, ssp, foo->flag), Clang does not resolve __builtin_constant_p() in the front end. It leaves that to the optimizer, which may still find a constant later. The context analysis then treats the call to __srcu_read_lock_must_hold() as reachable and reports a false positive. Clang does resolve __builtin_constant_p() to 0 when 'c' contains a function call, and that covers lockdep_is_held(), srcu_read_lock_held() and refcount_read(). In the current tree, every srcu_dereference_check() caller passes either a constant or an expression built from such calls, so this limitation doesn't affect any existing caller.

Right you are! Maybe __is_constexpr() would be better?

Quoted 11 lines

So in v4 I plan to:
- call __srcu_read_lock_must_hold() from srcu_dereference_check() only if 'c' is the constant 0, as shown above;
- keep srcu_dereference() defined as srcu_dereference_check((p), (ssp), 0), which now gets the check automatically;
- keep the explicit __srcu_read_lock_must_hold() call in srcu_dereference_notrace(), because it passes 1 for 'c';
- update the comment above __srcu_read_lock_must_hold() and the patch description to match.

Quoted 1 line

Does that sound good to you?

Give or take potentially fixing my poor choice of macro to __is_constexpr(), yes, please!

Thanx, Paul

#10 lore raw

Re: [PATCH v3 3/3] srcu: Remove check from srcu_dereference_check()

Quoted 1 line · Paul E. McKenney

Right you are! Maybe __is_constexpr() would be better?

Hi Paul,

Yes, it is. __is_constexpr() is a sizeof() trick, so its result is always an integer constant expression that the compiler front end computes. That's the result the Clang context analysis needs. Unlike __builtin_constant_p(), nothing is left for the optimizer to decide. This removes the limitation I described: if 'c' is a plain variable, function argument or structure member, the check is now skipped, as it should be.

include/linux/compiler.h already has a helper built on top of it:

	#define const_true(x) \
		__builtin_choose_expr(__is_constexpr(x), x, false)

so I plan to write this as follows:

	if (const_true(!(c)))
		__srcu_read_lock_must_hold(ssp);

Writing '__is_constexpr(c) && !(c)' instead gives the same results.

Results with clang -Wthread-safety, at -O0 and -O2:

	c == 0 or false                    -> checked
	c == 1                             -> not checked
	c == lockdep_is_held(&m)           -> not checked
	c == ({ (void)(&m); 1; })          -> not checked
	     (lockdep_is_held() with CONFIG_LOCKDEP=n)
	c == v, foo->flag, function arg    -> not checked (with
	                                      __builtin_constant_p()
	                                      these were false positives)
	c == lockdep_is_held(&m) || !v     -> not checked

GCC builds it without warnings. GCC doesn't run the context analysis, and __srcu_read_lock_must_hold() is an empty inline function, so the generated code doesn't change.

I will make this change in v4, together with the other changes I listed earlier.

Thanks,

Bart.

maintainer
#11 lore raw

Re: [PATCH v3 3/3] srcu: Remove check from srcu_dereference_check()

Quoted 37 lines · Bart Van Assche
Quoted 1 line · Paul E. McKenney

Right you are! Maybe __is_constexpr() would be better?

Hi Paul,

Yes, it is. __is_constexpr() is a sizeof() trick, so its result is always an integer constant expression that the compiler front end computes. That's the result the Clang context analysis needs. Unlike __builtin_constant_p(), nothing is left for the optimizer to decide. This removes the limitation I described: if 'c' is a plain variable, function argument or structure member, the check is now skipped, as it should be.

include/linux/compiler.h already has a helper built on top of it:

	#define const_true(x) \
		__builtin_choose_expr(__is_constexpr(x), x, false)

so I plan to write this as follows:

	if (const_true(!(c)))
		__srcu_read_lock_must_hold(ssp);

Writing '__is_constexpr(c) && !(c)' instead gives the same results.

Results with clang -Wthread-safety, at -O0 and -O2:

	c == 0 or false                    -> checked
	c == 1                             -> not checked
	c == lockdep_is_held(&m)           -> not checked
	c == ({ (void)(&m); 1; })          -> not checked
	     (lockdep_is_held() with CONFIG_LOCKDEP=n)
	c == v, foo->flag, function arg    -> not checked (with
	                                      __builtin_constant_p()
	                                      these were false positives)
	c == lockdep_is_held(&m) || !v     -> not checked
Quoted 6 lines

GCC builds it without warnings. GCC doesn't run the context analysis, and __srcu_read_lock_must_hold() is an empty inline function, so the generated code doesn't change.

I will make this change in v4, together with the other changes I listed earlier.

Sounds good, and thank you for bearing with me!

Thanx, Paul

Acked-by on the v1 series
#12 lore raw
In reply to Bart Van Assche
Quoted 14 lines · Bart Van Assche

Hi Peter and Marco,

This patch series includes three context analysis fixes:
- bit_spinlock: Surround the bitnum argument and macro expansion in __bitlock() with parentheses to avoid operator precedence issues.
- cleanup: Mark __free() helper functions generated by DEFINE_FREE() as __context_unsafe() because cleanup expressions that unlock trigger Clang thread-safety false positives.
- srcu: Move __srcu_read_lock_must_hold() out of srcu_dereference_check() and into srcu_dereference() and srcu_dereference_notrace() so update-side callers satisfying the lockdep condition do not fail context analysis.

Quoted 1 line

Please consider this patch series for the next merge window.

... modulo pending v2 that addresses the srcu patch's concerns from Paul.

Quoted 29 lines

Thanks,

Bart.

Changes compared to v2:
- Split out only the three standalone context analysis fixes (v2 patches 1, 2, and 4) into this series.
- Dropped the lockdep and RCU assertion behavior changes (v2 patches 8-14: lockdep_assume_*(), lockdep_assume_in_rcu_*(), and removing context assumptions from lockdep_assert_held*() and lockdep_assert_in_rcu_*()), as agreed in the review discussion [2].
- Dropped the preparatory __must_hold*() annotation patches (v2 patches 5-7) and the tracing branch profiling Kconfig patch (v2 patch 3) from this series.
- Updated the commit message of "cleanup: Disable context analysis for __free() helpers" with concrete examples of __free() unlock helpers in response to review feedback.
- Rebased onto current mainline.

Bart Van Assche (3):
bit_spinlock: Fix the __bitlock() definition
cleanup: Disable context analysis for __free() helpers
srcu: Remove check from srcu_dereference_check()

 include/linux/bit_spinlock.h |  2 +-
 include/linux/cleanup.h      |  6 ++++--
 include/linux/srcu.h         | 20 +++++++++++++++-----
 3 files changed, 20 insertions(+), 8 deletions(-)