mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v4 0/3] Fix three Clang context analysis issues
@ 2026-11-18 23:09 Bart Van Assche
  2026-11-18 23:09 ` [PATCH v4 1/3] bit_spinlock: Fix the __bitlock() definition Bart Van Assche
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Bart Van Assche @ 2026-11-18 23:09 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Ingo Molnar, Will Deacon, Boqun Feng, Waiman Long, linux-kernel,
	Marco Elver, Paul E . McKenney, Christoph Hellwig,
	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: 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(-)


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v4 1/3] bit_spinlock: Fix the __bitlock() definition
  2026-11-18 23:09 [PATCH v4 0/3] Fix three Clang context analysis issues Bart Van Assche
@ 2026-11-18 23:09 ` Bart Van Assche
  2026-11-18 23:09 ` [PATCH v4 2/3] cleanup: Disable context analysis for __free() helpers Bart Van Assche
  2026-11-18 23:09 ` [PATCH v4 3/3] srcu: Make the srcu_dereference_check() reader lock check conditional Bart Van Assche
  2 siblings, 0 replies; 4+ messages in thread
From: Bart Van Assche @ 2026-11-18 23:09 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Ingo Molnar, Will Deacon, Boqun Feng, Waiman Long, linux-kernel,
	Marco Elver, Paul E . McKenney, Christoph Hellwig,
	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.

Acked-by: Marco Elver <elver@google.com>
Fixes: eb7d96a13bf4 ("bit_spinlock: Support Clang's context analysis")
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 include/linux/bit_spinlock.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/linux/bit_spinlock.h b/include/linux/bit_spinlock.h
index 7869a6e59b6a..c31d4f4d09ee 100644
--- a/include/linux/bit_spinlock.h
+++ b/include/linux/bit_spinlock.h
@@ -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()

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v4 2/3] cleanup: Disable context analysis for __free() helpers
  2026-11-18 23:09 [PATCH v4 0/3] Fix three Clang context analysis issues Bart Van Assche
  2026-11-18 23:09 ` [PATCH v4 1/3] bit_spinlock: Fix the __bitlock() definition Bart Van Assche
@ 2026-11-18 23:09 ` Bart Van Assche
  2026-11-18 23:09 ` [PATCH v4 3/3] srcu: Make the srcu_dereference_check() reader lock check conditional Bart Van Assche
  2 siblings, 0 replies; 4+ messages in thread
From: Bart Van Assche @ 2026-11-18 23:09 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Ingo Molnar, Will Deacon, Boqun Feng, Waiman Long, linux-kernel,
	Marco Elver, Paul E . McKenney, Christoph Hellwig,
	Bart Van Assche, Nathan Chancellor, Dmitry Ilvokhin,
	Christian Brauner, H. Peter Anvin

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.

Acked-by: Marco Elver <elver@google.com>
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 include/linux/cleanup.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
index 1fb8058b897d..f6afe80781a4 100644
--- a/include/linux/cleanup.h
+++ b/include/linux/cleanup.h
@@ -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)
 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v4 3/3] srcu: Make the srcu_dereference_check() reader lock check conditional
  2026-11-18 23:09 [PATCH v4 0/3] Fix three Clang context analysis issues Bart Van Assche
  2026-11-18 23:09 ` [PATCH v4 1/3] bit_spinlock: Fix the __bitlock() definition Bart Van Assche
  2026-11-18 23:09 ` [PATCH v4 2/3] cleanup: Disable context analysis for __free() helpers Bart Van Assche
@ 2026-11-18 23:09 ` Bart Van Assche
  2 siblings, 0 replies; 4+ messages in thread
From: Bart Van Assche @ 2026-11-18 23:09 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Ingo Molnar, Will Deacon, Boqun Feng, Waiman Long, linux-kernel,
	Marco Elver, Paul E . McKenney, Christoph Hellwig,
	Bart Van Assche, Lai Jiangshan, Josh Triplett, Onur Özkan,
	Nathan Chancellor

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 <elver@google.com>
Suggested-by: Paul E. McKenney <paulmck@kernel.org>
Fixes: f0b7ce22d718 ("srcu: Support Clang's context analysis")
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 include/linux/srcu.h | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/include/linux/srcu.h b/include/linux/srcu.h
index d4dcaeb83e2b..380b5964fcb4 100644
--- a/include/linux/srcu.h
+++ b/include/linux/srcu.h
@@ -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.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-18 23:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18 23:09 [PATCH v4 0/3] Fix three Clang context analysis issues Bart Van Assche
2026-11-18 23:09 ` [PATCH v4 1/3] bit_spinlock: Fix the __bitlock() definition Bart Van Assche
2026-11-18 23:09 ` [PATCH v4 2/3] cleanup: Disable context analysis for __free() helpers Bart Van Assche
2026-11-18 23:09 ` [PATCH v4 3/3] srcu: Make the srcu_dereference_check() reader lock check conditional Bart Van Assche

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®