mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] x86/percpu: Use address space loads for per-CPU reads when using Clang
@ 2026-11-25 10:49 Ard Biesheuvel
  2026-11-25 19:05 ` Nick Desaulniers
  0 siblings, 1 reply; 3+ messages in thread
From: Ard Biesheuvel @ 2026-11-25 10:49 UTC (permalink / raw)
  To: linux-kernel
  Cc: Ard Biesheuvel, Sebastian Andrzej Siewior, Peter Zijlstra,
	Ingo Molnar, Nathan Chancellor, Nick Desaulniers, Bill Wendling,
	Justin Stitt, llvm, x86

From: Ard Biesheuvel <ardb@kernel.org>

Clang never discards inline asm with memory operands, even if its result
is unused [0]. This means unused per-CPU reads are always emitted,
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.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Nick Desaulniers <ndesaulniers@google.com>
Cc: Bill Wendling <morbo@google.com>
Cc: Justin Stitt <justinstitt@google.com>
Cc: llvm@lists.linux.dev
Cc: x86@kernel.org

This supersedes "locking/local_lock: Don't evaluate the per-CPU lock
pointer if unused"

https://lore.kernel.org/all/20261122105646.2223131-2-ardb+git@google.com/

 arch/x86/include/asm/percpu.h | 25 ++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/arch/x86/include/asm/percpu.h b/arch/x86/include/asm/percpu.h
index cef9a4ca9841..40262edef878 100644
--- a/arch/x86/include/asm/percpu.h
+++ b/arch/x86/include/asm/percpu.h
@@ -32,13 +32,13 @@
 
 #define __force_percpu_prefix	"%%"__stringify(__percpu_seg)":"
 
-#ifdef CONFIG_CC_HAS_NAMED_AS
-
 #ifdef __CHECKER__
 # define __seg_gs		__attribute__((address_space(__seg_gs)))
 # define __seg_fs		__attribute__((address_space(__seg_fs)))
 #endif
 
+#ifdef CONFIG_CC_HAS_NAMED_AS
+
 #define __percpu_prefix
 
 #else /* !CONFIG_CC_HAS_NAMED_AS: */
@@ -144,6 +144,25 @@ do {										\
 
 #else /* !CONFIG_USE_X86_SEG_SUPPORT: */
 
+#if defined(CONFIG_SMP) && defined(CONFIG_CC_IS_CLANG) && !defined(CONFIG_KMSAN)
+
+/*
+ * Clang never discards inline asm with memory operands, even if its result is
+ * unused. Its named address space support is sufficient for plain loads, so
+ * use those instead, allowing unused per-CPU reads to be elided. KMSAN is
+ * excluded as it ignores the segment base when instrumenting such loads.
+ *
+ * Once CONFIG_USE_X86_SEG_SUPPORT is fully implemented in Clang, this version
+ * can be dropped.
+ */
+#define __raw_cpu_read(size, qual, _var)				\
+({									\
+	*(qual TYPEOF_UNQUAL(_var) CONCATENATE(__seg_, __percpu_seg) * __force) \
+		(__force uintptr_t)&(_var);				\
+})
+
+#else
+
 #define __raw_cpu_read(size, qual, _var)				\
 ({									\
 	__pcpu_type_##size pfo_val__;					\
@@ -156,6 +175,8 @@ do {										\
 	(typeof(_var))(unsigned long) pfo_val__;			\
 })
 
+#endif
+
 #define __raw_cpu_write(size, qual, _var, _val)				\
 do {									\
 	__pcpu_type_##size pto_val__ = __pcpu_cast_##size(_val);	\
-- 
2.56.1.831.g720f3f5937-goog


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] x86/percpu: Use address space loads for per-CPU reads when using Clang
  2026-11-25 10:49 [PATCH] x86/percpu: Use address space loads for per-CPU reads when using Clang Ard Biesheuvel
@ 2026-11-25 19:05 ` Nick Desaulniers
  2026-11-26  8:51   ` Ard Biesheuvel
  0 siblings, 1 reply; 3+ messages in thread
From: Nick Desaulniers @ 2026-11-25 19:05 UTC (permalink / raw)
  To: Ard Biesheuvel
  Cc: linux-kernel, Ard Biesheuvel, Sebastian Andrzej Siewior,
	Peter Zijlstra, Ingo Molnar, Nathan Chancellor, Bill Wendling,
	Justin Stitt, llvm, x86, Alexander Potapenko, Marco Elver,
	Dmitry Vyukov, Uros Bizjak

On Wed, Nov 25, 2026 at 2:50 AM Ard Biesheuvel <ardb+git@google.com> wrote:
>
> From: Ard Biesheuvel <ardb@kernel.org>
>
> 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.

+ Uros for good measure.

>
> Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
> ---
> Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
> Cc: Peter Zijlstra <peterz@infradead.org>
> Cc: Ingo Molnar <mingo@redhat.com>
> Cc: Nathan Chancellor <nathan@kernel.org>
> Cc: Nick Desaulniers <ndesaulniers@google.com>
> Cc: Bill Wendling <morbo@google.com>
> Cc: Justin Stitt <justinstitt@google.com>
> Cc: llvm@lists.linux.dev
> Cc: x86@kernel.org
>
> This supersedes "locking/local_lock: Don't evaluate the per-CPU lock
> pointer if unused"
>
> https://lore.kernel.org/all/20261122105646.2223131-2-ardb+git@google.com/
>
>  arch/x86/include/asm/percpu.h | 25 ++++++++++++++++++--
>  1 file changed, 23 insertions(+), 2 deletions(-)
>
> diff --git a/arch/x86/include/asm/percpu.h b/arch/x86/include/asm/percpu.h
> index cef9a4ca9841..40262edef878 100644
> --- a/arch/x86/include/asm/percpu.h
> +++ b/arch/x86/include/asm/percpu.h
> @@ -32,13 +32,13 @@
>
>  #define __force_percpu_prefix  "%%"__stringify(__percpu_seg)":"
>
> -#ifdef CONFIG_CC_HAS_NAMED_AS
> -
>  #ifdef __CHECKER__
>  # define __seg_gs              __attribute__((address_space(__seg_gs)))
>  # define __seg_fs              __attribute__((address_space(__seg_fs)))
>  #endif
>
> +#ifdef CONFIG_CC_HAS_NAMED_AS
> +
>  #define __percpu_prefix
>
>  #else /* !CONFIG_CC_HAS_NAMED_AS: */
> @@ -144,6 +144,25 @@ do {                                                                               \
>
>  #else /* !CONFIG_USE_X86_SEG_SUPPORT: */
>
> +#if defined(CONFIG_SMP) && defined(CONFIG_CC_IS_CLANG) && !defined(CONFIG_KMSAN)
> +
> +/*
> + * Clang never discards inline asm with memory operands, even if its result is
> + * unused. Its named address space support is sufficient for plain loads, so

After the first sentence, I'd link to
https://github.com/llvm/llvm-project/issues/245738. Since this code is
meant to be temporary until that is fixed in clang, a future traveler
may simply open the link, see it's closed, then see what they can do
about cleaning up this code.  It's handy for Nathan or myself to use
`grep` to find links to llvm issues, then verify if they're stale and
can be cleaned up (once we have newer toolchains).

> + * use those instead, allowing unused per-CPU reads to be elided. KMSAN is
> + * excluded as it ignores the segment base when instrumenting such loads.

Then I'd link to your to be filed bug report, for the same reason.

> + *
> + * Once CONFIG_USE_X86_SEG_SUPPORT is fully implemented in Clang, this version
> + * can be dropped.

And finally here I'd link to the final issue.  That way it's clear
there's 3 different issues on the toolchain side conspiring here.

> + */
> +#define __raw_cpu_read(size, qual, _var)                               \
> +({                                                                     \
> +       *(qual TYPEOF_UNQUAL(_var) CONCATENATE(__seg_, __percpu_seg) * __force) \
> +               (__force uintptr_t)&(_var);                             \

Beautiful.

> +})
> +
> +#else
> +
>  #define __raw_cpu_read(size, qual, _var)                               \
>  ({                                                                     \
>         __pcpu_type_##size pfo_val__;                                   \
> @@ -156,6 +175,8 @@ do {                                                                                \
>         (typeof(_var))(unsigned long) pfo_val__;                        \
>  })
>
> +#endif
> +
>  #define __raw_cpu_write(size, qual, _var, _val)                                \
>  do {                                                                   \
>         __pcpu_type_##size pto_val__ = __pcpu_cast_##size(_val);        \
> --
> 2.56.1.831.g720f3f5937-goog
>


-- 
Thanks,
~Nick Desaulniers

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] x86/percpu: Use address space loads for per-CPU reads when using Clang
  2026-11-25 19:05 ` Nick Desaulniers
@ 2026-11-26  8:51   ` Ard Biesheuvel
  0 siblings, 0 replies; 3+ messages in thread
From: Ard Biesheuvel @ 2026-11-26  8:51 UTC (permalink / raw)
  To: Nick Desaulniers, Ard Biesheuvel
  Cc: linux-kernel, Sebastian Andrzej Siewior, Peter Zijlstra,
	Ingo Molnar, Nathan Chancellor, Bill Wendling, Justin Stitt,
	llvm, x86, Alexander Potapenko, Marco Elver, Dmitry Vyukov,
	Uros Bizjak



On Wed, 25 Nov 2026, at 20:05, Nick Desaulniers wrote:
> On Wed, Nov 25, 2026 at 2:50 AM Ard Biesheuvel <ardb+git@google.com> wrote:
>>
>> From: Ard Biesheuvel <ardb@kernel.org>
>>
>> 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,


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-26  8:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25 10:49 [PATCH] x86/percpu: Use address space loads for per-CPU reads when using Clang Ard Biesheuvel
2026-11-25 19:05 ` Nick Desaulniers
2026-11-26  8:51   ` Ard Biesheuvel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®