From: "tip-bot2 for Bart Van Assche" <tip-bot2@linutronix.de>
To: linux-tip-commits@vger.kernel.org
Cc: "Paul E. McKenney" <paulmck@kernel.org>,
Bart Van Assche <bvanassche@acm.org>,
"Peter Zijlstra (Intel)" <peterz@infradead.org>,
Ingo Molnar <mingo@kernel.org>,
x86@kernel.org, linux-kernel@vger.kernel.org
Subject: [tip: locking/urgent] srcu: Make the srcu_dereference_check() reader lock check conditional
Date: Sun, 22 Nov 2026 22:49:03 -0000 [thread overview]
Message-ID: <179538774382.92412.6167912138225242134.tip-bot2@tip-bot2> (raw)
In-Reply-To: <459250d4f2a6aa38ebab3fb51f6d4a63815bb4a1.1795043161.git.bvanassche@acm.org>
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 <bvanassche@acm.org>
AuthorDate: Wed, 18 Nov 2026 15:09:59 -08:00
Committer: Ingo Molnar <mingo@kernel.org>
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.
Fixes: f0b7ce22d718 ("srcu: Support Clang's context analysis")
Suggested-by: Paul E. McKenney <paulmck@kernel.org>
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Reviewed-by: Paul E. McKenney <paulmck@kernel.org>
Link: https://patch.msgid.link/459250d4f2a6aa38ebab3fb51f6d4a63815bb4a1.1795043161.git.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 d4dcaeb..380b596 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.
next prev parent reply other threads:[~2026-11-22 22:49 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
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-22 22:49 ` [tip: locking/urgent] bit_spinlock: Fix the __bitlock() macro definition tip-bot2 for Bart Van Assche
2026-11-23 9:44 ` tip-bot2 for Bart Van Assche
2026-11-18 23:09 ` [PATCH v4 2/3] cleanup: Disable context analysis for __free() helpers Bart Van Assche
2026-11-22 22:49 ` [tip: locking/urgent] " tip-bot2 for Bart Van Assche
2026-11-23 9:44 ` tip-bot2 for 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
2026-11-18 23:47 ` Paul E. McKenney
2026-11-22 22:49 ` tip-bot2 for Bart Van Assche [this message]
2026-11-23 9:44 ` [tip: locking/urgent] " tip-bot2 for Bart Van Assche
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=179538774382.92412.6167912138225242134.tip-bot2@tip-bot2 \
--to=tip-bot2@linutronix.de \
--cc=bvanassche@acm.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-tip-commits@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=paulmck@kernel.org \
--cc=peterz@infradead.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®