Skip to contentSkip to conversation

x86/percpu: Fix "const_pcpu_hot" version generation failure

PATCH Superseded Uros Bizjak 2 participants 5 messages Dec 3, 2023

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 x86/percpu: Fix "const_pcpu_hot" version generation failure

Version generation for "const_pcpu_hot" symbol failed because genksyms doesn't know the __seg_gs keyword. Revert commit 4604c052b84d "x86/percpu: Declare const_pcpu_hot as extern const variable" and use this_cpu_read_const instead to avoid "sparse: dereference of noderef expression" warning when reading const_pcpu_hot.

Cc Andy Lutomirski <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Denys Vlasenko <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Josh Poimboeuf <[email protected]>
---
 arch/x86/include/asm/current.h   | 5 +++--
 arch/x86/include/asm/percpu.h    | 7 +++++++
 arch/x86/include/asm/processor.h | 2 +-
 3 files changed, 11 insertions(+), 3 deletions(-)
arch/x86/include/asm/current.h +3 -2
index 9fbd7cb2dc86..c8c5674d69f6 100644
@@ -37,12 +37,13 @@ static_assert(sizeof(struct pcpu_hot) == 64);
 DECLARE_PER_CPU_ALIGNED(struct pcpu_hot, pcpu_hot);
 
 /* const-qualified alias to pcpu_hot, aliased by linker. */
-extern const struct pcpu_hot __percpu_seg_override const_pcpu_hot;
+DECLARE_PER_CPU_ALIGNED(const struct pcpu_hot __percpu_seg_override,
+			const_pcpu_hot);
 
 static __always_inline struct task_struct *get_current(void)
 {
 	if (IS_ENABLED(CONFIG_USE_X86_SEG_SUPPORT))
-		return const_pcpu_hot.current_task;
+		return this_cpu_read_const(const_pcpu_hot.current_task);
 
 	return this_cpu_read_stable(pcpu_hot.current_task);
 }
arch/x86/include/asm/percpu.h +7 -0
index 0f12b2004b94..7f6e978e21b1 100644
@@ -28,6 +28,7 @@
 
 #else /* ...!ASSEMBLY */
 
+#include <linux/bug.h>
 #include <linux/kernel.h>
 #include <linux/stringify.h>
 
@@ -462,6 +463,7 @@ do {									\
 #define this_cpu_write_8(pcp, val)	__raw_cpu_write(volatile, pcp, val)
 #endif
 
+#define this_cpu_read_const(pcp)	__raw_cpu_read(, pcp)
 #else /* CONFIG_USE_X86_SEG_SUPPORT */
 
 #define raw_cpu_read_1(pcp)		percpu_from_op(1, , "mov", pcp)
@@ -486,6 +488,11 @@ do {									\
 #define this_cpu_write_8(pcp, val)	percpu_to_op(8, volatile, "mov", (pcp), val)
 #endif
 
+/*
+ * The generic per-cpu infrastrucutre is not suitable for
+ * reading const-qualified variables.
+ */
+#define this_cpu_read_const(pcp)	({ BUG(); (typeof(pcp))0; })
 #endif /* CONFIG_USE_X86_SEG_SUPPORT */
 
 #define raw_cpu_add_1(pcp, val)		percpu_add_op(1, , (pcp), val)
arch/x86/include/asm/processor.h +1 -1
index 061aa86b4662..1188e8bf76a2 100644
@@ -534,7 +534,7 @@ static __always_inline unsigned long current_top_of_stack(void)
 	 *  entry trampoline.
 	 */
 	if (IS_ENABLED(CONFIG_USE_X86_SEG_SUPPORT))
-		return const_pcpu_hot.top_of_stack;
+		return this_cpu_read_const(const_pcpu_hot.top_of_stack);
 
 	return this_cpu_read_stable(pcpu_hot.top_of_stack);
 }
Signature · 1 line
2.42.0
#1 lore raw

PATCH 2/3 x86/traps: Use current_top_of_stack() helper in traps.c

Use current_top_of_stack() helper in sync_regs() and vc_switch_off_ist() instead of reading top_of_stack percpu variable explicitly.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
---
 arch/x86/kernel/traps.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/traps.c +2 -2
index c876f1d36a81..78b1d1a6ed2c 100644
@@ -772,7 +772,7 @@ DEFINE_IDTENTRY_RAW(exc_int3)
  */
 asmlinkage __visible noinstr struct pt_regs *sync_regs(struct pt_regs *eregs)
 {
-	struct pt_regs *regs = (struct pt_regs *)this_cpu_read(pcpu_hot.top_of_stack) - 1;
+	struct pt_regs *regs = (struct pt_regs *)current_top_of_stack() - 1;
 	if (regs != eregs)
 		*regs = *eregs;
 	return regs;
@@ -790,7 +790,7 @@ asmlinkage __visible noinstr struct pt_regs *vc_switch_off_ist(struct pt_regs *r
 	 * trust it and switch to the current kernel stack
 	 */
 	if (ip_within_syscall_gap(regs)) {
-		sp = this_cpu_read(pcpu_hot.top_of_stack);
+		sp = current_top_of_stack();
 		goto sync;
 	}
 
Signature · 1 line
2.42.0
#2 lore raw

PATCH 3/3 x86/percpu: Avoid sparse warning with cast to named address space

In reply to Uros Bizjak

Currently sparse does not know about __seg_fs and __seg_fs named address space qualifiers. Avoid thousands of warnings about unexpected keyword at the end of cast operator by removing named address space qualifier from __my_cpu_type() when __CHECKER__ is defined.

---
 arch/x86/include/asm/percpu.h | 9 +++++++++
 1 file changed, 9 insertions(+)
arch/x86/include/asm/percpu.h +9 -0
index 7f6e978e21b1..07ee2aedb1f6 100644
@@ -96,6 +96,15 @@
 
 #endif /* CONFIG_SMP */
 
+/*
+ * FIXME: Drop this hack once sparse learns how to ignore
+ * __seg_fs and __seg_gs named address space qualifiers.
+ */
+#ifdef __CHECKER__
+#undef __percpu_seg_override
+#define __percpu_seg_override
+#endif
+
 #define __my_cpu_type(var)	typeof(var) __percpu_seg_override
 #define __my_cpu_ptr(ptr)	(__my_cpu_type(*ptr) *)(uintptr_t)(ptr)
 #define __my_cpu_var(var)	(*__my_cpu_ptr(&var))
Signature · 1 line
2.42.0
#3 lore raw
In reply to Uros Bizjak

arch/x86/include/asm/percpu.h do { \ · line 495

Quoted 6 lines · Uros Bizjak
arch/x86/include/asm/percpu.h +5 -0
+/*
+ * The generic per-cpu infrastrucutre is not suitable for
+ * reading const-qualified variables.
+ */
+#define this_cpu_read_const(pcp)       ({ BUG(); (typeof(pcp))0; })

NAK. Absolutely not.

No way in hell is it acceptable to make this a run-time BUG. If it doesn't work, it needs to be a compile failure. End of story.

Linus

#4 lore raw
Quoted 11 lines · Linus Torvalds

arch/x86/include/asm/percpu.h do { \ · line 495

Quoted 6 lines · Uros Bizjak
arch/x86/include/asm/percpu.h +5 -0
+/*
+ * The generic per-cpu infrastrucutre is not suitable for
+ * reading const-qualified variables.
+ */
+#define this_cpu_read_const(pcp)       ({ BUG(); (typeof(pcp))0; })

NAK. Absolutely not.

Quoted 2 lines

No way in hell is it acceptable to make this a run-time BUG. If it doesn't work, it needs to be a compile failure. End of story.

Thanks - BUILD_BUG() also works here and is indeed more appropriate:

/**
* BUILD_BUG - break compile if used.
*
* If you have some code that you expect the compiler to eliminate at
* build time, you should use BUILD_BUG to detect if it is
* unexpectedly used.
*/

v2 is in testing.

Thanks,
Uros.