Skip to contentSkip to conversation

Fix three Clang context analysis issues

PATCH Applied Bart Van Assche 3 participants 11 messages Nov 18 – Nov 23 Sleepable read-copy update (SRCU)

lore
Share & tools
lore
#0 lore raw

v4 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: Guard the __srcu_read_lock_must_hold() call in srcu_dereference_check() with const_true(!(c)) 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 v3:
- Reworked patch 3/3 such that __srcu_read_lock_must_hold() remains in srcu_dereference_check().

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: Make the srcu_dereference_check() reader lock check conditional

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

PATCH v4 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

[tip: locking/urgent] bit_spinlock: Fix the __bitlock() macro definition

The following commit has been merged into the locking/urgent branch of tip:

The following commit has been merged into the locking/urgent branch of tip:

Commit-ID:     389327330cfe23adce88b55ee78c0d21fed9957e
Gitweb:        https://git.kernel.org/tip/389327330cfe23adce88b55ee78c0d21fed9957e
Author:        Bart Van Assche <[email protected]>
AuthorDate:    Wed, 18 Nov 2026 15:09:57 -08:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Sun, 22 Nov 2026 15:59:42 +01:00

bit_spinlock: Fix the __bitlock() macro 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.

---
 include/linux/bit_spinlock.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/bit_spinlock.h +1 -1
index 7869a6e..c31d4f4 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()
#3 lore raw

[tip: locking/urgent] bit_spinlock: Fix the __bitlock() macro definition

In reply to Bart Van Assche
The following commit has been merged into the locking/urgent branch of tip:

The following commit has been merged into the locking/urgent branch of tip:

Commit-ID:     af1a4ab4bf3bcb969ad5ee9e7fca1d23703adf32
Gitweb:        https://git.kernel.org/tip/af1a4ab4bf3bcb969ad5ee9e7fca1d23703adf32
Author:        Bart Van Assche <[email protected]>
AuthorDate:    Wed, 18 Nov 2026 15:09:57 -08:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Mon, 23 Nov 2026 10:38:52 +01:00

bit_spinlock: Fix the __bitlock() macro 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.

---
 include/linux/bit_spinlock.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/bit_spinlock.h +1 -1
index 7869a6e..c31d4f4 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()
#4 lore raw

PATCH v4 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)
 
#5 lore raw

[tip: locking/urgent] cleanup: Disable context analysis for __free() helpers

The following commit has been merged into the locking/urgent branch of tip:

The following commit has been merged into the locking/urgent branch of tip:

Commit-ID:     f7c70c05bba097d99dd06346b037f01c5ab44fbf
Gitweb:        https://git.kernel.org/tip/f7c70c05bba097d99dd06346b037f01c5ab44fbf
Author:        Bart Van Assche <[email protected]>
AuthorDate:    Wed, 18 Nov 2026 15:09:58 -08:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Sun, 22 Nov 2026 16:03:03 +01:00

cleanup: Disable context analysis for __free() helpers

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 via __context_unsafe().

---
 include/linux/cleanup.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
include/linux/cleanup.h +4 -2
index 1fb8058..1e09aea 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)
 
#6 lore raw

[tip: locking/urgent] cleanup: Disable context analysis for __free() helpers

In reply to Bart Van Assche
The following commit has been merged into the locking/urgent branch of tip:

The following commit has been merged into the locking/urgent branch of tip:

Commit-ID:     da198fa75403535626b91cfddff7c2189b5f894f
Gitweb:        https://git.kernel.org/tip/da198fa75403535626b91cfddff7c2189b5f894f
Author:        Bart Van Assche <[email protected]>
AuthorDate:    Wed, 18 Nov 2026 15:09:58 -08:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Mon, 23 Nov 2026 10:38:52 +01:00

cleanup: Disable context analysis for __free() helpers

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 via __context_unsafe().

---
 include/linux/cleanup.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
include/linux/cleanup.h +4 -2
index 1fb8058..1e09aea 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)
 
#7 lore raw

PATCH v4 3/3 srcu: Make the srcu_dereference_check() reader lock check conditional

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, only call __srcu_read_lock_must_hold() from srcu_dereference_check() if 'c' is the compile-time constant 0, i.e. if there is no update-side protection. Keep the fake RCU acquire and release around __rcu_dereference_check() and also keep the runtime lockdep check in srcu_dereference_check().

Use const_true() instead of __builtin_constant_p() to test whether 'c' is the constant 0. const_true() is based on __is_constexpr(), a sizeof() trick that the compiler front end always evaluates, so the Clang context analysis sees an integer constant expression. If 'c' is a plain variable, function argument or structure member, Clang may leave __builtin_constant_p(c) for the optimizer to decide. The context analysis then considers the __srcu_read_lock_must_hold() call reachable and reports a false positive. Additionally, const_true() never evaluates a non-constant 'c' at runtime, so the runtime behavior does not change.

srcu_dereference() passes 0 as 'c' and hence gets the compile-time check automatically. srcu_dereference_notrace() does not accept a lockdep condition and hence may only be used by SRCU readers. Since it passes 1 as 'c', call __srcu_read_lock_must_hold() explicitly from srcu_dereference_notrace() such that the Clang context analysis keeps verifying at compile time that its callers hold the SRCU reader lock.

Cc Marco Elver <[email protected]>
Fixes f0b7ce22d718 ("srcu: Support Clang's context analysis")
---
 include/linux/srcu.h | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
include/linux/srcu.h +11 -2
index d4dcaeb83e2b..380b5964fcb4 100644
@@ -280,6 +280,10 @@ static inline int srcu_read_lock_held(const struct srcu_struct *ssp)
  * 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'.
+ * srcu_dereference_check() only calls this helper if @c is the compile-time
+ * constant 0 because otherwise its callers may satisfy the condition @c
+ * instead of holding ssp, e.g. on the update side. srcu_dereference_notrace()
+ * passes 1 as @c and hence calls this helper explicitly.
  */
 static inline void __srcu_read_lock_must_hold(const struct srcu_struct *ssp) __must_hold_shared(ssp) { }
 
@@ -297,7 +301,8 @@ 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);					\
+	if (const_true(!(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);	\
@@ -323,7 +328,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 Reviewed-by on v4 3/3
#8 lore raw

Re: [PATCH v4 3/3] srcu: Make the srcu_dereference_check() reader lock check conditional

Quoted 29 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, only call __srcu_read_lock_must_hold() from srcu_dereference_check() if 'c' is the compile-time constant 0, i.e. if there is no update-side protection. Keep the fake RCU acquire and release around __rcu_dereference_check() and also keep the runtime lockdep check in srcu_dereference_check().

Use const_true() instead of __builtin_constant_p() to test whether 'c' is the constant 0. const_true() is based on __is_constexpr(), a sizeof() trick that the compiler front end always evaluates, so the Clang context analysis sees an integer constant expression. If 'c' is a plain variable, function argument or structure member, Clang may leave __builtin_constant_p(c) for the optimizer to decide. The context analysis then considers the __srcu_read_lock_must_hold() call reachable and reports a false positive. Additionally, const_true() never evaluates a non-constant 'c' at runtime, so the runtime behavior does not change.

srcu_dereference() passes 0 as 'c' and hence gets the compile-time check automatically. srcu_dereference_notrace() does not accept a lockdep condition and hence may only be used by SRCU readers. Since it passes 1 as 'c', call __srcu_read_lock_must_hold() explicitly from srcu_dereference_notrace() such that the Clang context analysis keeps verifying at compile time that its callers hold the SRCU reader lock.

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

Much better, thank you!

Quoted 42 lines
---
 include/linux/srcu.h | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
include/linux/srcu.h +11 -2
index d4dcaeb83e2b..380b5964fcb4 100644
@@ -280,6 +280,10 @@ static inline int srcu_read_lock_held(const struct srcu_struct *ssp)
  * 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'.
+ * srcu_dereference_check() only calls this helper if @c is the compile-time
+ * constant 0 because otherwise its callers may satisfy the condition @c
+ * instead of holding ssp, e.g. on the update side. srcu_dereference_notrace()
+ * passes 1 as @c and hence calls this helper explicitly.
  */
 static inline void __srcu_read_lock_must_hold(const struct srcu_struct *ssp) __must_hold_shared(ssp) { }
 
@@ -297,7 +301,8 @@ 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);					\
+	if (const_true(!(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);	\
@@ -323,7 +328,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.
#9 lore raw

[tip: locking/urgent] srcu: Make the srcu_dereference_check() reader lock check conditional

In reply to Bart Van Assche
The following commit has been merged into the locking/urgent branch of tip:

The following commit has been merged into the locking/urgent branch of tip:

Commit-ID:     8fec49960edd6adc9e579c8eb127873a74f76bb7
Gitweb:        https://git.kernel.org/tip/8fec49960edd6adc9e579c8eb127873a74f76bb7
Author:        Bart Van Assche <[email protected]>
AuthorDate:    Wed, 18 Nov 2026 15:09:59 -08:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Sun, 22 Nov 2026 16:03:18 +01:00

srcu: Make the srcu_dereference_check() reader lock check conditional

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, only call __srcu_read_lock_must_hold() from srcu_dereference_check() if 'c' is the compile-time constant 0, i.e. if there is no update-side protection. Keep the fake RCU acquire and release around __rcu_dereference_check() and also keep the runtime lockdep check in srcu_dereference_check().

Use const_true() instead of __builtin_constant_p() to test whether 'c' is the constant 0. const_true() is based on __is_constexpr(), a sizeof() trick that the compiler front end always evaluates, so the Clang context analysis sees an integer constant expression. If 'c' is a plain variable, function argument or structure member, Clang may leave __builtin_constant_p(c) for the optimizer to decide. The context analysis then considers the __srcu_read_lock_must_hold() call reachable and reports a false positive. Additionally, const_true() never evaluates a non-constant 'c' at runtime, so the runtime behavior does not change.

srcu_dereference() passes 0 as 'c' and hence gets the compile-time check automatically. srcu_dereference_notrace() does not accept a lockdep condition and hence may only be used by SRCU readers. Since it passes 1 as 'c', call __srcu_read_lock_must_hold() explicitly from srcu_dereference_notrace() such that the Clang context analysis keeps verifying at compile time that its callers hold the SRCU reader lock.

---
 include/linux/srcu.h | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
include/linux/srcu.h +11 -2
index d4dcaeb..380b596 100644
@@ -280,6 +280,10 @@ static inline int srcu_read_lock_held(const struct srcu_struct *ssp)
  * 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'.
+ * srcu_dereference_check() only calls this helper if @c is the compile-time
+ * constant 0 because otherwise its callers may satisfy the condition @c
+ * instead of holding ssp, e.g. on the update side. srcu_dereference_notrace()
+ * passes 1 as @c and hence calls this helper explicitly.
  */
 static inline void __srcu_read_lock_must_hold(const struct srcu_struct *ssp) __must_hold_shared(ssp) { }
 
@@ -297,7 +301,8 @@ 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);					\
+	if (const_true(!(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);	\
@@ -323,7 +328,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.
#10 lore raw

[tip: locking/urgent] srcu: Make the srcu_dereference_check() reader lock check conditional

In reply to Bart Van Assche
The following commit has been merged into the locking/urgent branch of tip:

The following commit has been merged into the locking/urgent branch of tip:

Commit-ID:     e2652119b5944e7732ff856ba8057afaa28f5ac8
Gitweb:        https://git.kernel.org/tip/e2652119b5944e7732ff856ba8057afaa28f5ac8
Author:        Bart Van Assche <[email protected]>
AuthorDate:    Wed, 18 Nov 2026 15:09:59 -08:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Mon, 23 Nov 2026 10:38:52 +01:00

srcu: Make the srcu_dereference_check() reader lock check conditional

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, only call __srcu_read_lock_must_hold() from srcu_dereference_check() if 'c' is the compile-time constant 0, i.e. if there is no update-side protection. Keep the fake RCU acquire and release around __rcu_dereference_check() and also keep the runtime lockdep check in srcu_dereference_check().

Use const_true() instead of __builtin_constant_p() to test whether 'c' is the constant 0. const_true() is based on __is_constexpr(), a sizeof() trick that the compiler front end always evaluates, so the Clang context analysis sees an integer constant expression. If 'c' is a plain variable, function argument or structure member, Clang may leave __builtin_constant_p(c) for the optimizer to decide. The context analysis then considers the __srcu_read_lock_must_hold() call reachable and reports a false positive. Additionally, const_true() never evaluates a non-constant 'c' at runtime, so the runtime behavior does not change.

srcu_dereference() passes 0 as 'c' and hence gets the compile-time check automatically. srcu_dereference_notrace() does not accept a lockdep condition and hence may only be used by SRCU readers. Since it passes 1 as 'c', call __srcu_read_lock_must_hold() explicitly from srcu_dereference_notrace() such that the Clang context analysis keeps verifying at compile time that its callers hold the SRCU reader lock.

---
 include/linux/srcu.h | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
include/linux/srcu.h +11 -2
index d4dcaeb..380b596 100644
@@ -280,6 +280,10 @@ static inline int srcu_read_lock_held(const struct srcu_struct *ssp)
  * 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'.
+ * srcu_dereference_check() only calls this helper if @c is the compile-time
+ * constant 0 because otherwise its callers may satisfy the condition @c
+ * instead of holding ssp, e.g. on the update side. srcu_dereference_notrace()
+ * passes 1 as @c and hence calls this helper explicitly.
  */
 static inline void __srcu_read_lock_must_hold(const struct srcu_struct *ssp) __must_hold_shared(ssp) { }
 
@@ -297,7 +301,8 @@ 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);					\
+	if (const_true(!(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);	\
@@ -323,7 +328,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.