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

From: Bart Van Assche

Date: Tue Nov 17 2026 - 11:54:03 EST


On 11/16/26 4:51 PM, Paul E. McKenney wrote:
> 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.