Re: [PATCH] locking/local_lock: Don't evaluate the per-CPU lock pointer if unused

From: Ard Biesheuvel

Date: Wed Nov 25 2026 - 03:04:13 EST




On Tue, 24 Nov 2026, at 23:44, Nick Desaulniers wrote:
> On Tue, Nov 24, 2026 at 2:25 PM Ard Biesheuvel <ardb@xxxxxxxxxx> wrote:
>>
>>
>>
>> On Tue, 24 Nov 2026, at 20:32, Nick Desaulniers wrote:
>> > On Tue, Nov 24, 2026 at 8:39 AM Sebastian Andrzej Siewior
>> > <bigeasy@xxxxxxxxxxxxx> wrote:
>> >>
>> >> On 2026-11-23 13:52:09 [-0800], Nick Desaulniers wrote:
>> >> > On Mon, Nov 23, 2026 at 1:23 PM Ard Biesheuvel <ardb@xxxxxxxxxx> wrote:
>> >> > >
>> >> > >
>> >> +/*
>> >> + * Only evaluate @lock if it is actually used, i.e., for local_trylock_t, or
>> >> + * when lock debugging is enabled (see local_lock_acquire() above).
>> >> + */
>> >> #define __local_lock_acquire(lock) \
>> >> do { \
>> >> - local_trylock_t *__tl; \
>> >> - local_lock_t *__l; \
>> >> - \
>> >> - __l = (local_lock_t *)(lock); \
>> >> - __tl = (local_trylock_t *)__l; \
>> >> _Generic((lock), \
>> >> local_trylock_t *: ({ \
>> >> + local_trylock_t *__tl = (local_trylock_t *)(lock); \
>> >> + \
>> >
>> > Why do we need the temporary?
>> >
>>
>> Because 'lock' is a macro argument that we should evaluate only once.
>
> Ah, right! Don't we have __auto_type or something for that though?
> Maybe cleaner than the cast? Ah, that also doesn't work for the same
> reason as below.
>
>>
>> >> lockdep_assert(__tl->acquired == 0); \
>> >> WRITE_ONCE(__tl->acquired, 1); \
>> >> + local_lock_acquire((local_lock_t *)__tl); \
>> >> }), \
>> >> - local_lock_t *: (void)0); \
>> >> - local_lock_acquire(__l); \
>> >> + local_lock_t *: ({ \
>> >> + local_lock_acquire((local_lock_t *)(lock)); \
>> >
>> > Is this cast necessary?
>> >
>>
>> Surprisingly, yes.
>>
>> https://godbolt.org/z/E8rGchfev
>
> What the f...
>
> Anyways, with the additional context (git trailers) linking to the
> clang bugs (and missed optimizations):
>
> Reviewed-by: Nick Desaulniers <ndesaulniers@xxxxxxxxxx>

It turns out we should be able to work around this treewide by using
__seg_gs only for __raw_cpu_read(). I'll send that out and we can
discuss there.