Skip to contentSkip to conversation

x86/stackprotector: Move stack canary to struct pcpu_hot

PATCH In review Uros Bizjak 3 participants 11 messages Feb 20, 2025 – Feb 21, 2025

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

PATCH 1/1 x86/stackprotector: Move stack canary to struct pcpu_hot

Move stack canary from __stack_chk_guard to struct pcpu_hot and alias __stack_chk_guard to point to the new location in the linker script.

__stack_chk_guard is one of the hottest data structures on x86, so moving it there makes sense even if its benefit cannot be measured 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]>
Cc Brian Gerst <[email protected]>
Cc Ard Biesheuvel <[email protected]>
---
 arch/x86/include/asm/current.h | 13 +++++++++++++
 arch/x86/kernel/cpu/common.c   |  1 -
 arch/x86/kernel/vmlinux.lds.S  |  2 ++
 3 files changed, 15 insertions(+), 1 deletion(-)
arch/x86/include/asm/current.h +13 -0
index bf5953883ec3..e4ff1d15b465 100644
@@ -15,6 +15,9 @@ struct task_struct;
 struct pcpu_hot {
 	union {
 		struct {
+#ifdef CONFIG_STACKPROTECTOR
+			unsigned long		stack_canary;
+#endif
 			struct task_struct	*current_task;
 			int			preempt_count;
 			int			cpu_number;
@@ -35,6 +38,16 @@ struct pcpu_hot {
 };
 static_assert(sizeof(struct pcpu_hot) == 64);
 
+/*
+ * stack_canary should be at the beginning of struct pcpu_hot to avoid:
+ *
+ * Invalid absolute R_X86_64_32S relocation: __stack_chk_guard
+ *
+ * error when aliasing __stack_chk_guard to struct pcpu_hot
+ * - see arch/x86/kernel/vmlinux.lds.S.
+ */
+static_assert(offsetof(struct pcpu_hot, stack_canary) == 0);
+
 DECLARE_PER_CPU_ALIGNED(struct pcpu_hot, pcpu_hot);
 
 /* const-qualified alias to pcpu_hot, aliased by linker. */
arch/x86/kernel/cpu/common.c +0 -1
index 21078907af57..9e54c1b585d2 100644
@@ -2088,7 +2088,6 @@ void syscall_init(void)
 #endif /* CONFIG_X86_64 */
 
 #ifdef CONFIG_STACKPROTECTOR
-DEFINE_PER_CPU(unsigned long, __stack_chk_guard);
 #ifndef CONFIG_SMP
 EXPORT_PER_CPU_SYMBOL(__stack_chk_guard);
 #endif
arch/x86/kernel/vmlinux.lds.S +2 -0
index 1769a7126224..cabb86d505fc 100644
@@ -467,6 +467,8 @@ SECTIONS
 . = ASSERT((_end - LOAD_OFFSET <= KERNEL_IMAGE_SIZE),
 	   "kernel image bigger than KERNEL_IMAGE_SIZE");
 
+PROVIDE(__stack_chk_guard = pcpu_hot);
+
 /* needed for Clang - see arch/x86/entry/entry.S */
 PROVIDE(__ref_stack_chk_guard = __stack_chk_guard);
 
Signature · 1 line
2.42.0
#1 lore raw
Quoted 38 lines · Uros Bizjak

Move stack canary from __stack_chk_guard to struct pcpu_hot and alias __stack_chk_guard to point to the new location in the linker script.

__stack_chk_guard is one of the hottest data structures on x86, so moving it there makes sense even if its benefit cannot be measured 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]>
Cc Brian Gerst <[email protected]>
Cc Ard Biesheuvel <[email protected]>
---
 arch/x86/include/asm/current.h | 13 +++++++++++++
 arch/x86/kernel/cpu/common.c   |  1 -
 arch/x86/kernel/vmlinux.lds.S  |  2 ++
 3 files changed, 15 insertions(+), 1 deletion(-)
arch/x86/include/asm/current.h +3 -0
index bf5953883ec3..e4ff1d15b465 100644
@@ -15,6 +15,9 @@ struct task_struct;
 struct pcpu_hot {
        union {
                struct {
+#ifdef CONFIG_STACKPROTECTOR
+                       unsigned long           stack_canary;
+#endif
                        struct task_struct      *current_task;
                        int                     preempt_count;
                        int                     cpu_number;

arch/x86/include/asm/current.h struct pcpu_hot { · line 44

Quoted 7 lines
arch/x86/include/asm/current.h +4 -0
@@ -35,6 +38,16 @@ struct pcpu_hot {
 };
 static_assert(sizeof(struct pcpu_hot) == 64);
 
+/*
+ * stack_canary should be at the beginning of struct pcpu_hot to avoid:
+ *
+ * Invalid absolute R_X86_64_32S relocation: __stack_chk_guard

This should be R_X86_64_PC32 relocations.

arch/x86/include/asm/current.h struct pcpu_hot { · line 49

Quoted 5 lines
arch/x86/include/asm/current.h +5 -0
+ *
+ * error when aliasing __stack_chk_guard to struct pcpu_hot
+ * - see arch/x86/kernel/vmlinux.lds.S.
+ */
+static_assert(offsetof(struct pcpu_hot, stack_canary) == 0);

The simple solution to this is to add the symbol to the whitelist in tools/relocs.c:
/*
* These symbols are known to be relative, even if the linker marks them
* as absolute (typically defined outside any section in the linker script.) */

I just got rid of hardcoding fixed_percpu_data from the start of
percpu memory. I'd rather not add something similar back in.

Brian Gerst

#2 lore raw
Quoted 43 lines · Brian Gerst
Quoted 41 lines · Uros Bizjak

Move stack canary from __stack_chk_guard to struct pcpu_hot and alias __stack_chk_guard to point to the new location in the linker script.

__stack_chk_guard is one of the hottest data structures on x86, so moving it there makes sense even if its benefit cannot be measured 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]>
Cc Brian Gerst <[email protected]>
Cc Ard Biesheuvel <[email protected]>
---
 arch/x86/include/asm/current.h | 13 +++++++++++++
 arch/x86/kernel/cpu/common.c   |  1 -
 arch/x86/kernel/vmlinux.lds.S  |  2 ++
 3 files changed, 15 insertions(+), 1 deletion(-)
arch/x86/include/asm/current.h +3 -0
index bf5953883ec3..e4ff1d15b465 100644
@@ -15,6 +15,9 @@ struct task_struct;
 struct pcpu_hot {
        union {
                struct {
+#ifdef CONFIG_STACKPROTECTOR
+                       unsigned long           stack_canary;
+#endif
                        struct task_struct      *current_task;
                        int                     preempt_count;
                        int                     cpu_number;
@@ -35,6 +38,16 @@ struct pcpu_hot {
 };
 static_assert(sizeof(struct pcpu_hot) == 64);
 
Quoted 6 lines

arch/x86/include/asm/current.h struct pcpu_hot { · line 44

Quoted 4 lines
arch/x86/include/asm/current.h +4 -0
+/*
+ * stack_canary should be at the beginning of struct pcpu_hot to avoid:
+ *
+ * Invalid absolute R_X86_64_32S relocation: __stack_chk_guard

This should be R_X86_64_PC32 relocations.

This is what the build system reports if any offset (including 0) is added to

PROVIDE(__stack_chk_guard = pcpu_hot);

It is not a warning, but an error that fails the build.

As was discussed in the previous thread, the above is required to handle !SMP case, where mstack-protector-guard=global (used by !SMP builds) ignores the
-mstack-protector-guard-symbol option and always uses __stack_chk_guard.

Uros.

#3 lore raw
Quoted 54 lines · Uros Bizjak
Quoted 43 lines · Brian Gerst
Quoted 41 lines · Uros Bizjak

Move stack canary from __stack_chk_guard to struct pcpu_hot and alias __stack_chk_guard to point to the new location in the linker script.

__stack_chk_guard is one of the hottest data structures on x86, so moving it there makes sense even if its benefit cannot be measured 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]>
Cc Brian Gerst <[email protected]>
Cc Ard Biesheuvel <[email protected]>
---
 arch/x86/include/asm/current.h | 13 +++++++++++++
 arch/x86/kernel/cpu/common.c   |  1 -
 arch/x86/kernel/vmlinux.lds.S  |  2 ++
 3 files changed, 15 insertions(+), 1 deletion(-)
arch/x86/include/asm/current.h +3 -0
index bf5953883ec3..e4ff1d15b465 100644
@@ -15,6 +15,9 @@ struct task_struct;
 struct pcpu_hot {
        union {
                struct {
+#ifdef CONFIG_STACKPROTECTOR
+                       unsigned long           stack_canary;
+#endif
                        struct task_struct      *current_task;
                        int                     preempt_count;
                        int                     cpu_number;
@@ -35,6 +38,16 @@ struct pcpu_hot {
 };
 static_assert(sizeof(struct pcpu_hot) == 64);
 
Quoted 6 lines

arch/x86/include/asm/current.h struct pcpu_hot { · line 44

Quoted 4 lines
arch/x86/include/asm/current.h +4 -0
+/*
+ * stack_canary should be at the beginning of struct pcpu_hot to avoid:
+ *
+ * Invalid absolute R_X86_64_32S relocation: __stack_chk_guard

This should be R_X86_64_PC32 relocations.

This is what the build system reports if any offset (including 0) is added to

Quoted 8 lines

PROVIDE(__stack_chk_guard = pcpu_hot);

It is not a warning, but an error that fails the build.

As was discussed in the previous thread, the above is required to handle !SMP case, where mstack-protector-guard=global (used by !SMP builds) ignores the
-mstack-protector-guard-symbol option and always uses __stack_chk_guard.

I got a warning from the relocs tool, but not a hard error. What compiler/linker are you using?

Does the attached patch build in your configuration?

Brian Gerst

From ee30f3a936e641978c965d82990c11792b598fd8 Mon Sep 17 00:00:00 2001
From: Brian Gerst <[email protected]>
Date: Wed, 19 Feb 2025 12:38:02 -0500
Subject: [PATCH] x86/stackprotector: Move canary to pcpu_hot

The stack protector canary value is frequently accessed on function entry and exit, so move it to the pcpu_hot struct where it will have fast access.

In order to keep non-SMP builds working, rename __ref_stack_chk_guard to __stack_chk_guard.

---
 arch/x86/Makefile                     | 2 +-
 arch/x86/entry/entry.S                | 2 +-
 arch/x86/entry/entry_32.S             | 2 +-
 arch/x86/entry/entry_64.S             | 2 +-
 arch/x86/include/asm/asm-prototypes.h | 2 +-
 arch/x86/include/asm/current.h        | 3 +++
 arch/x86/include/asm/stackprotector.h | 7 +++----
 arch/x86/kernel/asm-offsets.c         | 4 ++++
 arch/x86/kernel/cpu/common.c          | 8 --------
 arch/x86/kernel/module.c              | 2 +-
 arch/x86/kernel/vmlinux.lds.S         | 4 +++-
 arch/x86/tools/relocs.c               | 1 +
 12 files changed, 20 insertions(+), 19 deletions(-)
arch/x86/Makefile +1 -1
index 88a1705366f9..cc619d31ccfe 100644
@@ -197,7 +197,7 @@ endif
 ifeq ($(CONFIG_STACKPROTECTOR),y)
     ifeq ($(CONFIG_SMP),y)
 	KBUILD_CFLAGS += -mstack-protector-guard-reg=$(percpu_seg)
-	KBUILD_CFLAGS += -mstack-protector-guard-symbol=__ref_stack_chk_guard
+	KBUILD_CFLAGS += -mstack-protector-guard-symbol=__stack_chk_guard
     else
 	KBUILD_CFLAGS += -mstack-protector-guard=global
     endif
arch/x86/entry/entry.S +1 -1
index fe5344a249a1..d779381a067c 100644
@@ -63,5 +63,5 @@ THUNK warn_thunk_thunk, __warn_thunk
  * instead.
  */
 #ifdef CONFIG_STACKPROTECTOR
-EXPORT_SYMBOL(__ref_stack_chk_guard);
+EXPORT_SYMBOL(__stack_chk_guard);
 #endif
arch/x86/entry/entry_32.S +1 -1
index 20be5758c2d2..940efc07f2c1 100644
@@ -691,7 +691,7 @@ SYM_CODE_START(__switch_to_asm)
 
 #ifdef CONFIG_STACKPROTECTOR
 	movl	TASK_stack_canary(%edx), %ebx
-	movl	%ebx, PER_CPU_VAR(__stack_chk_guard)
+	movl	%ebx, PER_CPU_VAR(pcpu_hot + X86_stack_canary)
 #endif
 
 	/*
arch/x86/entry/entry_64.S +1 -1
index 33a955aa01d8..ed43559d66dc 100644
@@ -192,7 +192,7 @@ SYM_FUNC_START(__switch_to_asm)
 
 #ifdef CONFIG_STACKPROTECTOR
 	movq	TASK_stack_canary(%rsi), %rbx
-	movq	%rbx, PER_CPU_VAR(__stack_chk_guard)
+	movq	%rbx, PER_CPU_VAR(pcpu_hot + X86_stack_canary)
 #endif
 
 	/*
arch/x86/include/asm/asm-prototypes.h +1 -1
index 3674006e3974..43977c74b22f 100644
@@ -21,5 +21,5 @@ extern void cmpxchg8b_emu(void);
 #endif
 
 #if defined(__GENKSYMS__) && defined(CONFIG_STACKPROTECTOR)
-extern unsigned long __ref_stack_chk_guard;
+extern unsigned long __stack_chk_guard;
 #endif
arch/x86/include/asm/current.h +3 -0
index bf5953883ec3..cf0ce44c1d95 100644
@@ -28,6 +28,9 @@ struct pcpu_hot {
 			bool			hardirq_stack_inuse;
 #else
 			void			*softirq_stack_ptr;
+#endif
+#ifdef CONFIG_STACKPROTECTOR
+			unsigned long		stack_canary;
 #endif
 		};
 		u8	pad[64];
arch/x86/include/asm/stackprotector.h +3 -4
index d43fb589fcf6..4142c88390d3 100644
@@ -17,11 +17,10 @@
 #include <asm/processor.h>
 #include <asm/percpu.h>
 #include <asm/desc.h>
+#include <asm/current.h>
 
 #include <linux/sched.h>
 
-DECLARE_PER_CPU(unsigned long, __stack_chk_guard);
-
 /*
  * Initialize the stackprotector canary value.
  *
@@ -38,12 +37,12 @@ static __always_inline void boot_init_stack_canary(void)
 	unsigned long canary = get_random_canary();
 
 	current->stack_canary = canary;
-	this_cpu_write(__stack_chk_guard, canary);
+	this_cpu_write(pcpu_hot.stack_canary, canary);
 }
 
 static inline void cpu_init_stack_canary(int cpu, struct task_struct *idle)
 {
-	per_cpu(__stack_chk_guard, cpu) = idle->stack_canary;
+	per_cpu(pcpu_hot.stack_canary, cpu) = idle->stack_canary;
 }
 
 #else	/* STACKPROTECTOR */
arch/x86/kernel/asm-offsets.c +4 -0
index a98020bf31bb..4c02f5d247f2 100644
@@ -112,6 +112,10 @@ static void __used common(void)
 #ifdef CONFIG_MITIGATION_CALL_DEPTH_TRACKING
 	OFFSET(X86_call_depth, pcpu_hot, call_depth);
 #endif
+#ifdef CONFIG_STACKPROTECTOR
+	OFFSET(X86_stack_canary, pcpu_hot, stack_canary);
+#endif
+
 #if IS_ENABLED(CONFIG_CRYPTO_ARIA_AESNI_AVX_X86_64)
 	/* Offset for fields in aria_ctx */
 	BLANK();
arch/x86/kernel/cpu/common.c +0 -8
index 8b49b1338f76..1735e4a11d9e 100644
@@ -24,7 +24,6 @@
 #include <linux/io.h>
 #include <linux/syscore_ops.h>
 #include <linux/pgtable.h>
-#include <linux/stackprotector.h>
 #include <linux/utsname.h>
 
 #include <asm/alternative.h>
@@ -2087,13 +2086,6 @@ void syscall_init(void)
 }
 #endif /* CONFIG_X86_64 */
 
-#ifdef CONFIG_STACKPROTECTOR
-DEFINE_PER_CPU(unsigned long, __stack_chk_guard);
-#ifndef CONFIG_SMP
-EXPORT_PER_CPU_SYMBOL(__stack_chk_guard);
-#endif
-#endif
-
 /*
  * Clear all 6 debug registers:
  */
arch/x86/kernel/module.c +1 -1
index a286f32c5503..fbadc78957df 100644
@@ -134,7 +134,7 @@ static int __write_relocate_add(Elf64_Shdr *sechdrs,
 #if defined(CONFIG_STACKPROTECTOR) && \
     defined(CONFIG_CC_IS_CLANG) && CONFIG_CLANG_VERSION < 170000
 		case R_X86_64_REX_GOTPCRELX: {
-			static unsigned long __percpu *const addr = &__stack_chk_guard;
+			static unsigned long __percpu *const addr = &pcpu_hot.stack_canary;
 
 			if (sym->st_value != (u64)addr) {
 				pr_err("%s: Unsupported GOTPCREL relocation\n", me->name);
arch/x86/kernel/vmlinux.lds.S +3 -1
index 1769a7126224..cb0ab873616e 100644
@@ -467,8 +467,10 @@ SECTIONS
 . = ASSERT((_end - LOAD_OFFSET <= KERNEL_IMAGE_SIZE),
 	   "kernel image bigger than KERNEL_IMAGE_SIZE");
 
+#ifdef CONFIG_STACKPROTECTOR
 /* needed for Clang - see arch/x86/entry/entry.S */
-PROVIDE(__ref_stack_chk_guard = __stack_chk_guard);
+__stack_chk_guard = pcpu_hot + X86_stack_canary;
+#endif
 
 #ifdef CONFIG_X86_64
 
arch/x86/tools/relocs.c +1 -0
index 5778bc498415..bf612f1f8c8a 100644
@@ -89,6 +89,7 @@ static const char * const	sym_regex_kernel[S_NSYMTYPES] = {
 	"__end_rodata_aligned|"
 	"__initramfs_start|"
 	"(jiffies|jiffies_64)|"
+	"__stack_chk_guard|"
 #if ELF_BITS == 64
 	"__end_rodata_hpage_align|"
 #endif

base-commit: 01157ddc58dc2fe428ec17dd5a18cc13f134639f

Signature · 1 line
2.48.1
#4 lore raw
Quoted 65 lines · Brian Gerst
Quoted 54 lines · Uros Bizjak
Quoted 43 lines · Brian Gerst
Quoted 41 lines · Uros Bizjak

Move stack canary from __stack_chk_guard to struct pcpu_hot and alias __stack_chk_guard to point to the new location in the linker script.

__stack_chk_guard is one of the hottest data structures on x86, so moving it there makes sense even if its benefit cannot be measured 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]>
Cc Brian Gerst <[email protected]>
Cc Ard Biesheuvel <[email protected]>
---
 arch/x86/include/asm/current.h | 13 +++++++++++++
 arch/x86/kernel/cpu/common.c   |  1 -
 arch/x86/kernel/vmlinux.lds.S  |  2 ++
 3 files changed, 15 insertions(+), 1 deletion(-)
arch/x86/include/asm/current.h +3 -0
index bf5953883ec3..e4ff1d15b465 100644
@@ -15,6 +15,9 @@ struct task_struct;
 struct pcpu_hot {
        union {
                struct {
+#ifdef CONFIG_STACKPROTECTOR
+                       unsigned long           stack_canary;
+#endif
                        struct task_struct      *current_task;
                        int                     preempt_count;
                        int                     cpu_number;
@@ -35,6 +38,16 @@ struct pcpu_hot {
 };
 static_assert(sizeof(struct pcpu_hot) == 64);
 
Quoted 6 lines

arch/x86/include/asm/current.h struct pcpu_hot { · line 44

Quoted 4 lines
arch/x86/include/asm/current.h +4 -0
+/*
+ * stack_canary should be at the beginning of struct pcpu_hot to avoid:
+ *
+ * Invalid absolute R_X86_64_32S relocation: __stack_chk_guard

This should be R_X86_64_PC32 relocations.

This is what the build system reports if any offset (including 0) is added to

Quoted 8 lines

PROVIDE(__stack_chk_guard = pcpu_hot);

It is not a warning, but an error that fails the build.

As was discussed in the previous thread, the above is required to handle !SMP case, where mstack-protector-guard=global (used by !SMP builds) ignores the
-mstack-protector-guard-symbol option and always uses __stack_chk_guard.

Quoted 4 lines

I got a warning from the relocs tool, but not a hard error. What compiler/linker are you using?

Does the attached patch build in your configuration?

Ah, the attached patch is similar to my previous approach, where the build system *warned* on an offset (the patch was abandoned due to Ard's request to put stack_canary to the *beginning* of struct pcpu_hot, and this allowed for a simplified patch).

The attached patch builds for me without warning/error for both, SMP and !SMP build.

You can put my Acked-by: on the patch, and if it is based on my previous patch, I'd be grateful for a Co-developed-by tag.

Thanks.
Uros.

#5 lore raw
Quoted 72 lines · Uros Bizjak
Quoted 65 lines · Brian Gerst
Quoted 54 lines · Uros Bizjak
Quoted 43 lines · Brian Gerst
Quoted 41 lines · Uros Bizjak

Move stack canary from __stack_chk_guard to struct pcpu_hot and alias __stack_chk_guard to point to the new location in the linker script.

__stack_chk_guard is one of the hottest data structures on x86, so moving it there makes sense even if its benefit cannot be measured 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]>
Cc Brian Gerst <[email protected]>
Cc Ard Biesheuvel <[email protected]>
---
 arch/x86/include/asm/current.h | 13 +++++++++++++
 arch/x86/kernel/cpu/common.c   |  1 -
 arch/x86/kernel/vmlinux.lds.S  |  2 ++
 3 files changed, 15 insertions(+), 1 deletion(-)
arch/x86/include/asm/current.h +3 -0
index bf5953883ec3..e4ff1d15b465 100644
@@ -15,6 +15,9 @@ struct task_struct;
 struct pcpu_hot {
        union {
                struct {
+#ifdef CONFIG_STACKPROTECTOR
+                       unsigned long           stack_canary;
+#endif
                        struct task_struct      *current_task;
                        int                     preempt_count;
                        int                     cpu_number;
@@ -35,6 +38,16 @@ struct pcpu_hot {
 };
 static_assert(sizeof(struct pcpu_hot) == 64);
 
Quoted 6 lines

arch/x86/include/asm/current.h struct pcpu_hot { · line 44

Quoted 4 lines
arch/x86/include/asm/current.h +4 -0
+/*
+ * stack_canary should be at the beginning of struct pcpu_hot to avoid:
+ *
+ * Invalid absolute R_X86_64_32S relocation: __stack_chk_guard

This should be R_X86_64_PC32 relocations.

This is what the build system reports if any offset (including 0) is added to

Quoted 8 lines

PROVIDE(__stack_chk_guard = pcpu_hot);

It is not a warning, but an error that fails the build.

As was discussed in the previous thread, the above is required to handle !SMP case, where mstack-protector-guard=global (used by !SMP builds) ignores the
-mstack-protector-guard-symbol option and always uses __stack_chk_guard.

Quoted 4 lines

I got a warning from the relocs tool, but not a hard error. What compiler/linker are you using?

Does the attached patch build in your configuration?

Quoted 8 lines

Ah, the attached patch is similar to my previous approach, where the build system *warned* on an offset (the patch was abandoned due to Ard's request to put stack_canary to the *beginning* of struct pcpu_hot, and this allowed for a simplified patch).

The attached patch builds for me without warning/error for both, SMP and !SMP build.

Did you try building modules too?

#6 lore raw
Quoted 10 lines · Ard Biesheuvel
Quoted 10 lines
Quoted 4 lines

I got a warning from the relocs tool, but not a hard error. What compiler/linker are you using?

Does the attached patch build in your configuration?

Ah, the attached patch is similar to my previous approach, where the build system *warned* on an offset (the patch was abandoned due to Ard's request to put stack_canary to the *beginning* of struct pcpu_hot, and this allowed for a simplified patch).

Quoted 5 lines
Quoted 3 lines

The attached patch builds for me without warning/error for both, SMP and !SMP build.

Did you try building modules too?

make -j 24 olddefconfig prepare modules_prepare bzImage modules

for defconfig, SMP and !SMP.

Uros.

#7 lore raw
Quoted 17 lines · Uros Bizjak
Quoted 14 lines · Ard Biesheuvel
Quoted 13 lines
Quoted 4 lines

I got a warning from the relocs tool, but not a hard error. What compiler/linker are you using?

Does the attached patch build in your configuration?

Ah, the attached patch is similar to my previous approach, where the build system *warned* on an offset (the patch was abandoned due to Ard's request to put stack_canary to the *beginning* of struct pcpu_hot, and this allowed for a simplified patch).

The attached patch builds for me without warning/error for both, SMP and !SMP build.

Quoted 6 lines
Quoted 1 line

Did you try building modules too?

make -j 24 olddefconfig prepare modules_prepare bzImage modules

for defconfig, SMP and !SMP.

OK.

I think I prefer Brian's approach - the only nit is that the placement of stack_canary creates a padding hole in struct pcpu_hot. However, that does not actually matter until we run out of space so I think it is fine as is.

#8 lore raw
Quoted 26 lines · Ard Biesheuvel
Quoted 17 lines · Uros Bizjak
Quoted 14 lines · Ard Biesheuvel
Quoted 13 lines
Quoted 4 lines

I got a warning from the relocs tool, but not a hard error. What compiler/linker are you using?

Does the attached patch build in your configuration?

Ah, the attached patch is similar to my previous approach, where the build system *warned* on an offset (the patch was abandoned due to Ard's request to put stack_canary to the *beginning* of struct pcpu_hot, and this allowed for a simplified patch).

The attached patch builds for me without warning/error for both, SMP and !SMP build.

Quoted 6 lines
Quoted 1 line

Did you try building modules too?

make -j 24 olddefconfig prepare modules_prepare bzImage modules

for defconfig, SMP and !SMP.

Quoted 6 lines

OK.

I think I prefer Brian's approach - the only nit is that the placement of stack_canary creates a padding hole in struct pcpu_hot. However, that does not actually matter until we run out of space so I think it is fine as is.

Going off on a tangent, as struct pcpu_hot grows I think it can be done better as a subsection of percpu data (".data..percpu..hot"). That way each variable has its own symbol again and it reduces header dependencies.

Brian Gerst

#9 lore raw
Quoted 35 lines · Brian Gerst
Quoted 26 lines · Ard Biesheuvel
Quoted 17 lines · Uros Bizjak
Quoted 14 lines · Ard Biesheuvel
Quoted 13 lines
Quoted 4 lines

I got a warning from the relocs tool, but not a hard error. What compiler/linker are you using?

Does the attached patch build in your configuration?

Ah, the attached patch is similar to my previous approach, where the build system *warned* on an offset (the patch was abandoned due to Ard's request to put stack_canary to the *beginning* of struct pcpu_hot, and this allowed for a simplified patch).

The attached patch builds for me without warning/error for both, SMP and !SMP build.

Quoted 6 lines
Quoted 1 line

Did you try building modules too?

make -j 24 olddefconfig prepare modules_prepare bzImage modules

for defconfig, SMP and !SMP.

Quoted 6 lines

OK.

I think I prefer Brian's approach - the only nit is that the placement of stack_canary creates a padding hole in struct pcpu_hot. However, that does not actually matter until we run out of space so I think it is fine as is.

Quoted 4 lines

Going off on a tangent, as struct pcpu_hot grows I think it can be done better as a subsection of percpu data (".data..percpu..hot"). That way each variable has its own symbol again and it reduces header dependencies.

Please note an optimization in common.h:

/* const-qualified alias to pcpu_hot, aliased by linker. */ DECLARE_PER_CPU_ALIGNED(const struct pcpu_hot __percpu_seg_override,

            const_pcpu_hot);

struct pcpu_hot has its const alias, so we are able to use:

    if (IS_ENABLED(CONFIG_USE_X86_SEG_SUPPORT))
        return this_cpu_read_const(const_pcpu_hot.current_task);

where the compiler is able to eliminate a sequence of reads to the const_pcpu_hot.current_task.

LTO does not like the approach, though, but clang does not use x86 seg support, and gcc does not perform LTO.

Uros.

#10 lore raw
Quoted 50 lines · Uros Bizjak
Quoted 35 lines · Brian Gerst
Quoted 26 lines · Ard Biesheuvel
Quoted 17 lines · Uros Bizjak
Quoted 14 lines · Ard Biesheuvel
Quoted 13 lines
Quoted 4 lines

I got a warning from the relocs tool, but not a hard error. What compiler/linker are you using?

Does the attached patch build in your configuration?

Ah, the attached patch is similar to my previous approach, where the build system *warned* on an offset (the patch was abandoned due to Ard's request to put stack_canary to the *beginning* of struct pcpu_hot, and this allowed for a simplified patch).

The attached patch builds for me without warning/error for both, SMP and !SMP build.

Quoted 6 lines
Quoted 1 line

Did you try building modules too?

make -j 24 olddefconfig prepare modules_prepare bzImage modules

for defconfig, SMP and !SMP.

Quoted 6 lines

OK.

I think I prefer Brian's approach - the only nit is that the placement of stack_canary creates a padding hole in struct pcpu_hot. However, that does not actually matter until we run out of space so I think it is fine as is.

Quoted 4 lines

Going off on a tangent, as struct pcpu_hot grows I think it can be done better as a subsection of percpu data (".data..percpu..hot"). That way each variable has its own symbol again and it reduces header dependencies.

Please note an optimization in common.h:

/* const-qualified alias to pcpu_hot, aliased by linker. */ DECLARE_PER_CPU_ALIGNED(const struct pcpu_hot __percpu_seg_override,

            const_pcpu_hot);

struct pcpu_hot has its const alias, so we are able to use:

Quoted 8 lines
    if (IS_ENABLED(CONFIG_USE_X86_SEG_SUPPORT))
        return this_cpu_read_const(const_pcpu_hot.current_task);

where the compiler is able to eliminate a sequence of reads to the const_pcpu_hot.current_task.

LTO does not like the approach, though, but clang does not use x86 seg support, and gcc does not perform LTO.

Noted.

I'll look into this over the weekend.

Brian Gerst