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

From: Ard Biesheuvel

Date: Mon Nov 23 2026 - 16:23:45 EST



On Mon, 23 Nov 2026, at 20:12, Nick Desaulniers wrote:
> On Sun, Nov 22, 2026 at 2:56 AM Ard Biesheuvel <ardb+git@xxxxxxxxxx> wrote:
>>
>> From: Ard Biesheuvel <ardb@xxxxxxxxxx>
>>
>> When lock debugging is disabled, local_lock_acquire() and friends are
>> empty, and so the per-CPU lock pointer that is passed to them, which is
>> usually computed using this_cpu_ptr(), is not used at all.
>>
>> However, on x86, when the compiler does not support named address spaces
>> (i.e., Clang), this_cpu_ptr() is based on inline asm with a memory
>
> I had to look this one up ("named address spaces").
>
> https://gcc.gnu.org/onlinedocs/gcc/Named-Address-Spaces.html#x86-Named-Address-Spaces
>
> So __seg_fs / __seg_gs ?
>
> CC_HAS_NAMED_AS : arch/x86/Kconfig#2331
>
> Weird, clang _does_ seem to support these, since clang-11 (clang-17 is
> the current minimum supported default).
> https://godbolt.org/z/cfhj8r69E
>
> commit 47ff30cc1be7 ("x86/percpu: Enable named address spaces for all
> capable GCC versions")
> added a
>
> depends on CC_IS_GCC
>
> What happens if you turn that off?
>
> Ah, a week after that commit was authored, the same author filed
> https://github.com/llvm/llvm-project/issues/93449
>
> (currently assigned to Bill; Bill, can you PTAL?)
>
> They also filed
> https://github.com/llvm/llvm-project/issues/93449
>
> https://github.com/llvm/llvm-project/issues/187893 looks like possible
> a dup of one of the above. Bill, can you please take a look at those
> 3?
>
>> operand, and Clang does not discard such inline asm even if its result
>> is unused. So the computation of the per-CPU address of the lock is
>> emitted regardless, e.g., on every local_lock_nested_bh() in
>> process_backlog() and on every local_lock() in the folio batching code.
>
> I'm more so curious how you even noticed this problem? Was there a
> thread? Were you looking at some assembler and wondering what was
> going on here? Or was there another thread about this?
>

No it manifested in local testing of my fgkaslr implementation.

I'm doing some very detailed profiling to ensure it does not regress
performance.

> Very detailed commit message, but you kind of bury the lede here on
> what the actual problem this patch is fixing *is*.
>

Apologies. I'll trim it down,

The TL;DR is that non-volatile asm() statements with memory input
operands are not elided by Clang if their output is never used.