Skip to contentSkip to conversation

x86/percpu: Use address space loads for per-CPU reads when using Clang

PATCH New Ard Biesheuvel 1 participant 1 message Nov 25

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/percpu: Use address space loads for per-CPU reads when using Clang

From: Ard Biesheuvel <[email protected]>

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.

---
Cc: Sebastian Andrzej Siewior <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Nick Desaulniers <[email protected]>
Cc: Bill Wendling <[email protected]>
Cc: Justin Stitt <[email protected]>
Cc: [email protected]
Cc: [email protected]

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

https://lore.kernel.org/all/[email protected]/

 arch/x86/include/asm/percpu.h | 25 ++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)
arch/x86/include/asm/percpu.h +23 -2
index cef9a4ca9841..40262edef878 100644
@@ -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);	\
Signature · 1 line
2.56.1.831.g720f3f5937-goog