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

From: Paul E. McKenney

Date: Tue Nov 17 2026 - 12:53:34 EST


On Tue, Nov 17, 2026 at 08:53:52AM -0800, Bart Van Assche wrote:
> 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)'.

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