Re: [PATCH] x86/percpu: Use address space loads for per-CPU reads when using Clang
From: Ard Biesheuvel
Date: Thu Nov 26 2026 - 03:51:43 EST
On Wed, 25 Nov 2026, at 20:05, Nick Desaulniers wrote:
> On Wed, Nov 25, 2026 at 2:50 AM Ard Biesheuvel <ardb+git@xxxxxxxxxx> wrote:
>>
>> From: Ard Biesheuvel <ardb@xxxxxxxxxx>
>>
>> Clang never discards inline asm with memory operands, even if its result
>> is unused [0]. This means unused per-CPU reads are always emitted,
>
> Ack on this missed optimization. I _just_ got beers with Bill last
> night, and forgot to bring this up!
>
>> including the load of this_cpu_off by this_cpu_ptr() in cases where the
>> resulting pointer is never dereferenced.
>>
>> Clang's support for the __seg_gs/__seg_fs named address space qualifiers
>> is insufficient for full CONFIG_USE_X86_SEG_SUPPORT, but works well
>> enough [1] for plain loads, and is present in all versions of Clang that
>> Linux still cares about. So use those to implement __raw_cpu_read() on
>> SMP, permitting the compiler to elide them when the result is unused.
>>
>> On a v7.4-rc1 defconfig build, this gets rid of 174 spurious per-CPU
>> loads, 136 of which are loads of this_cpu_off from this_cpu_ptr().
>>
>> [0] https://github.com/llvm/llvm-project/issues/245738
>> [1] Exclude KMSAN, which ignores the segment base when instrumenting
>> such loads.
>
> + Maintainers of KMSAN. Ard, can we get a formal bug report about that?
> https://github.com/llvm/llvm-project/issues
> With the report in hand, it would be handy to reference it below.
>
Posted a fix here:
https://github.com/llvm/llvm-project/pull/246269
Will add the reference to v2, and incorporate the rest of your feedback.
Thanks,