Re: [PATCH v3 3/3] srcu: Remove check from srcu_dereference_check()
From: Paul E. McKenney
Date: Tue Nov 17 2026 - 15:19:59 EST
On Tue, Nov 17, 2026 at 12:03:52PM -0800, Bart Van Assche wrote:
> On 11/17/26 10:44 AM, Paul E. McKenney wrote:
> > Right you are! Maybe __is_constexpr() would be better?
>
> Hi Paul,
>
> Yes, it is. __is_constexpr() is a sizeof() trick, so its result is
> always an integer constant expression that the compiler front end
> computes. That's the result the Clang context analysis needs.
> Unlike __builtin_constant_p(), nothing is left for the optimizer to
> decide. This removes the limitation I described: if 'c' is a plain
> variable, function argument or structure member, the check is now
> skipped, as it should be.
>
> include/linux/compiler.h already has a helper built on top of it:
>
> #define const_true(x) \
> __builtin_choose_expr(__is_constexpr(x), x, false)
>
> so I plan to write this as follows:
>
> if (const_true(!(c)))
> __srcu_read_lock_must_hold(ssp);
>
> Writing '__is_constexpr(c) && !(c)' instead gives the same results.
>
> Results with clang -Wthread-safety, at -O0 and -O2:
>
> c == 0 or false -> checked
> c == 1 -> not checked
> c == lockdep_is_held(&m) -> not checked
> c == ({ (void)(&m); 1; }) -> not checked
> (lockdep_is_held() with CONFIG_LOCKDEP=n)
> c == v, foo->flag, function arg -> not checked (with
> __builtin_constant_p()
> these were false positives)
> c == lockdep_is_held(&m) || !v -> not checked
>
> GCC builds it without warnings. GCC doesn't run the context
> analysis, and __srcu_read_lock_must_hold() is an empty inline
> function, so the generated code doesn't change.
>
> I will make this change in v4, together with the other changes I
> listed earlier.
Sounds good, and thank you for bearing with me!
Thanx, Paul