All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] x86: replace stack cookie hack with named address space
@ 2026-11-27 14:23 Ard Biesheuvel
  2026-11-27 14:23 ` [PATCH 1/2] compiler.h: Use the symbol's own pointer type in __ADDRESSABLE() Ard Biesheuvel
  2026-11-27 14:23 ` [PATCH 2/2] x86/stackprotector: Use a named address space for the Clang stack guard Ard Biesheuvel
  0 siblings, 2 replies; 4+ messages in thread
From: Ard Biesheuvel @ 2026-11-27 14:23 UTC (permalink / raw)
  To: x86, linux-kernel
  Cc: Ard Biesheuvel, Uros Bizjak, Nathan Chancellor, Nick Desaulniers,
	llvm

From: Ard Biesheuvel <ardb@kernel.org>

Currently, on x86, the C name of the stack check cookie per-CPU variable
is deliberately different from the one passed to the compiler command
line option -mstack-check-guard-symbol=

This was done to work around a Clang issue, where a non-TLS C
declaration of the variable results in Clang dropping the segment
prefixes.

As it turns out, all Clang versions that Linux supports today have some
support for address space annotations, and annotating the C declaration
with the correct address space is sufficient to coerce Clang into
generating the appropriate prefixes for the memory operands. This means
the linker and asm hacks can be dropped.

Patch #1 makes EXPORT_SYMBOL() work as expected for symbols with an
address space annotation, so that patch #2 can export __stack_chk_guard
normally, rather than from an unrelated .S file.

Cc: Uros Bizjak <ubizjak@gmail.com>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Nick Desaulniers <ndesaulniers@google.com>
Cc: llvm@lists.linux.dev

Ard Biesheuvel (2):
  compiler.h: Use the symbol's own pointer type in __ADDRESSABLE()
  x86/stackprotector: Use a named address space for the Clang stack
    guard

 arch/x86/Makefile                     |  2 +-
 arch/x86/entry/entry.S                | 14 ----------
 arch/x86/include/asm/asm-prototypes.h |  4 ---
 arch/x86/include/asm/percpu.h         | 10 +++----
 arch/x86/include/asm/stackprotector.h | 29 ++++++++++++++++++--
 arch/x86/kernel/cpu/common.c          |  4 +--
 arch/x86/kernel/vmlinux.lds.S         |  3 --
 include/linux/compiler.h              |  4 +--
 8 files changed, 35 insertions(+), 35 deletions(-)

-- 
2.56.1.839.g22a8290db9-goog


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

* [PATCH 1/2] compiler.h: Use the symbol's own pointer type in __ADDRESSABLE()
  2026-11-27 14:23 [PATCH 0/2] x86: replace stack cookie hack with named address space Ard Biesheuvel
@ 2026-11-27 14:23 ` Ard Biesheuvel
  2026-11-27 14:23 ` [PATCH 2/2] x86/stackprotector: Use a named address space for the Clang stack guard Ard Biesheuvel
  1 sibling, 0 replies; 4+ messages in thread
From: Ard Biesheuvel @ 2026-11-27 14:23 UTC (permalink / raw)
  To: x86, linux-kernel
  Cc: Ard Biesheuvel, Uros Bizjak, Nathan Chancellor, Nick Desaulniers,
	llvm

From: Ard Biesheuvel <ardb@kernel.org>

Casting the address of a variable in a named address space to void * is
rejected by GCC, which is why it is currently cast via uintptr_t. Clang
folds that into an addrspacecast, which it cannot emit in a static
initializer, preventing such variables from being exported.

Use a pointer of the symbol's own type instead, which needs no cast at
all.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 include/linux/compiler.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/include/linux/compiler.h b/include/linux/compiler.h
index 0ea7150dba7b..9983f8eed085 100644
--- a/include/linux/compiler.h
+++ b/include/linux/compiler.h
@@ -286,8 +286,8 @@ static inline void *offset_to_ptr(const int *off)
  * visible to the compiler.
  */
 #define ___ADDRESSABLE(sym, __attrs)						\
-	static void * __used __attrs						\
-	__UNIQUE_ID(__PASTE(addressable_, sym)) = (void *)(uintptr_t)&sym;
+	static typeof(&sym) __used __attrs					\
+	__UNIQUE_ID(__PASTE(addressable_, sym)) = &sym;
 
 #define __ADDRESSABLE(sym) \
 	___ADDRESSABLE(sym, __section(".discard.addressable"))
-- 
2.56.1.839.g22a8290db9-goog


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

* [PATCH 2/2] x86/stackprotector: Use a named address space for the Clang stack guard
  2026-11-27 14:23 [PATCH 0/2] x86: replace stack cookie hack with named address space Ard Biesheuvel
  2026-11-27 14:23 ` [PATCH 1/2] compiler.h: Use the symbol's own pointer type in __ADDRESSABLE() Ard Biesheuvel
@ 2026-11-27 14:23 ` Ard Biesheuvel
  1 sibling, 0 replies; 4+ messages in thread
From: Ard Biesheuvel @ 2026-11-27 14:23 UTC (permalink / raw)
  To: x86, linux-kernel
  Cc: Ard Biesheuvel, Uros Bizjak, Nathan Chancellor, Nick Desaulniers,
	llvm

From: Ard Biesheuvel <ardb@kernel.org>

When Clang finds an existing declaration of the variable passed via
-mstack-protector-guard-symbol=, it uses it as is, and only emits the
segment prefix if it resides in the matching named address space. This is
why the guard is currently referenced via an alias that never appears in
C code.

Instead, declare __stack_chk_guard in the %gs/%fs named address space when
building with Clang, and access it via an unqualified per-CPU lvalue from
C code, permitting the alias, and the export from asm, to be dropped.

Once CONFIG_USE_X86_SEG_SUPPORT=y is implemented by all supported
compilers, this workaround can be dropped.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 arch/x86/Makefile                     |  2 +-
 arch/x86/entry/entry.S                | 14 ----------
 arch/x86/include/asm/asm-prototypes.h |  4 ---
 arch/x86/include/asm/percpu.h         | 10 +++----
 arch/x86/include/asm/stackprotector.h | 29 ++++++++++++++++++--
 arch/x86/kernel/cpu/common.c          |  4 +--
 arch/x86/kernel/vmlinux.lds.S         |  3 --
 7 files changed, 33 insertions(+), 33 deletions(-)

diff --git a/arch/x86/Makefile b/arch/x86/Makefile
index b7e08519b8c1..116700da7988 100644
--- a/arch/x86/Makefile
+++ b/arch/x86/Makefile
@@ -183,7 +183,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
diff --git a/arch/x86/entry/entry.S b/arch/x86/entry/entry.S
index 2bc217bb5475..f88021f4467e 100644
--- a/arch/x86/entry/entry.S
+++ b/arch/x86/entry/entry.S
@@ -62,20 +62,6 @@ EXPORT_SYMBOL_FOR_KVM(x86_verw_sel);
 
 THUNK warn_thunk_thunk, __warn_thunk
 
-/*
- * Clang's implementation of TLS stack cookies requires the variable in
- * question to be a TLS variable. If the variable happens to be defined as an
- * ordinary variable with external linkage in the same compilation unit (which
- * amounts to the whole of vmlinux with LTO enabled), Clang will drop the
- * segment register prefix from the references, resulting in broken code. Work
- * around this by avoiding the symbol used in -mstack-protector-guard-symbol=
- * entirely in the C code, and use an alias emitted by the linker script
- * instead.
- */
-#if defined(CONFIG_STACKPROTECTOR) && defined(CONFIG_SMP)
-EXPORT_SYMBOL(__ref_stack_chk_guard);
-#endif
-
 #if IS_ENABLED(CONFIG_KVM_INTEL)
 .macro IDT_DO_EVENT_IRQOFF call_insn call_target
 	/*
diff --git a/arch/x86/include/asm/asm-prototypes.h b/arch/x86/include/asm/asm-prototypes.h
index 11c6fecc3ad7..b04015ef12a9 100644
--- a/arch/x86/include/asm/asm-prototypes.h
+++ b/arch/x86/include/asm/asm-prototypes.h
@@ -19,7 +19,3 @@
 #ifndef CONFIG_X86_CX8
 extern void cmpxchg8b_emu(void);
 #endif
-
-#ifdef CONFIG_STACKPROTECTOR
-extern unsigned long __ref_stack_chk_guard;
-#endif
diff --git a/arch/x86/include/asm/percpu.h b/arch/x86/include/asm/percpu.h
index cef9a4ca9841..f7e37722913e 100644
--- a/arch/x86/include/asm/percpu.h
+++ b/arch/x86/include/asm/percpu.h
@@ -28,17 +28,17 @@
 #include <linux/stringify.h>
 #include <asm/asm.h>
 
+#ifdef __CHECKER__
+# define __seg_gs		__attribute__((address_space(__seg_gs)))
+# define __seg_fs		__attribute__((address_space(__seg_fs)))
+#endif
+
 #ifdef CONFIG_SMP
 
 #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
-
 #define __percpu_prefix
 
 #else /* !CONFIG_CC_HAS_NAMED_AS: */
diff --git a/arch/x86/include/asm/stackprotector.h b/arch/x86/include/asm/stackprotector.h
index cd761b14eb02..184c630452a7 100644
--- a/arch/x86/include/asm/stackprotector.h
+++ b/arch/x86/include/asm/stackprotector.h
@@ -20,7 +20,28 @@
 
 #include <linux/sched.h>
 
-DECLARE_PER_CPU_CACHE_HOT(unsigned long, __stack_chk_guard);
+/*
+ * If a per-CPU C declaration of __stack_chk_guard is observable by Clang, it
+ * will emit the memory operand with the segment prefix only when the C
+ * declaration is annotated with the correct address space.  Otherwise, Clang
+ * will ignore -mstack-protector-guard-reg=%fs/%gs, and emit the bare unshifted
+ * memory operand. 'Observable' means part of the same translation unit, which
+ * amounts to all of vmlinux when LTO is enabled.
+ *
+ * So use an address space annotation for the stack canary even when using a
+ * Clang version that does not support CONFIG_USE_X86_SEG_SUPPORT=y fully.
+ */
+#if defined(CONFIG_SMP) && defined(CONFIG_CC_IS_CLANG) && \
+    !defined(CONFIG_USE_X86_SEG_SUPPORT)
+#define __stack_chk_guard_seg	CONCATENATE(__seg_, __percpu_seg)
+#define __pcpu_stack_chk_guard	\
+	*(unsigned long __percpu *)(__force unsigned long)&__stack_chk_guard
+#else
+#define __stack_chk_guard_seg
+#define __pcpu_stack_chk_guard	__stack_chk_guard
+#endif
+
+DECLARE_PER_CPU_CACHE_HOT(unsigned long __stack_chk_guard_seg, __stack_chk_guard);
 
 /*
  * Initialize the stackprotector canary value.
@@ -38,14 +59,16 @@ 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_stack_chk_guard, 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_stack_chk_guard, cpu) = idle->stack_canary;
 }
 
+#undef __pcpu_stack_chk_guard
+
 #else	/* STACKPROTECTOR */
 
 /* dummy boot_init_stack_canary() is defined in linux/stackprotector.h */
diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 9bd50c68cd7e..5d0d29ec8974 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -2322,11 +2322,9 @@ void syscall_init(void)
 #endif /* CONFIG_X86_64 */
 
 #ifdef CONFIG_STACKPROTECTOR
-DEFINE_PER_CPU_CACHE_HOT(unsigned long, __stack_chk_guard);
-#ifndef CONFIG_SMP
+DEFINE_PER_CPU_CACHE_HOT(unsigned long __stack_chk_guard_seg, __stack_chk_guard);
 EXPORT_PER_CPU_SYMBOL(__stack_chk_guard);
 #endif
-#endif
 
 static void initialize_debug_regs(void)
 {
diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S
index 2438b89a4620..61173c38a951 100644
--- a/arch/x86/kernel/vmlinux.lds.S
+++ b/arch/x86/kernel/vmlinux.lds.S
@@ -475,9 +475,6 @@ SECTIONS
 	   "kernel image bigger than KERNEL_IMAGE_SIZE");
 #endif
 
-/* needed for Clang - see arch/x86/entry/entry.S */
-PROVIDE(__ref_stack_chk_guard = __stack_chk_guard);
-
 #ifdef CONFIG_X86_64
 
 #ifdef CONFIG_MITIGATION_UNRET_ENTRY
-- 
2.56.1.839.g22a8290db9-goog


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

* Re: [PATCH 1/2] compiler.h: Use the symbol's own pointer type in __ADDRESSABLE()
@ 2026-11-29  8:16 kernel test robot
  0 siblings, 0 replies; 4+ messages in thread
From: kernel test robot @ 2026-11-29  8:16 UTC (permalink / raw)
  To: oe-kbuild; +Cc: lkp, Dan Carpenter

Unfiltered warning: this smatch report is sent as-is and has not been
reviewed for false positives, so please treat it with due skepticism.

BCC: lkp@intel.com
CC: oe-kbuild-all@lists.linux.dev
In-Reply-To: <20261127142346.788995-5-ardb+git@google.com>
References: <20261127142346.788995-5-ardb+git@google.com>
TO: Ard Biesheuvel <ardb+git@google.com>
TO: x86@kernel.org
TO: linux-kernel@vger.kernel.org
CC: Ard Biesheuvel <ardb@kernel.org>
CC: Uros Bizjak <ubizjak@gmail.com>
CC: Nathan Chancellor <nathan@kernel.org>
CC: Nick Desaulniers <ndesaulniers@google.com>
CC: llvm@lists.linux.dev

Hi Ard,

kernel test robot noticed the following build warnings:

[auto build test WARNING on tip/x86/core]
[also build test WARNING on tip/master tip/x86/asm linus/master v7.4-rc4 next-20261127]
[cannot apply to tip/auto-latest]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Ard-Biesheuvel/compiler-h-Use-the-symbol-s-own-pointer-type-in-__ADDRESSABLE/20261127-152348
base:   tip/x86/core
patch link:    https://lore.kernel.org/r/20261127142346.788995-5-ardb%2Bgit%40google.com
patch subject: [PATCH 1/2] compiler.h: Use the symbol's own pointer type in __ADDRESSABLE()
:::::: branch date: 2 days ago
:::::: commit date: 2 days ago
config: sh-randconfig-2200-20261129 (https://download.01.org/0day-ci/archive/20261129/20261129091029.VOYaTAQ6-lkp@intel.com/config)
compiler: sh4-linux-gcc (GCC) 10.5.0
smatch: v0.5.0-9187-g5189e3fb

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Reported-by: Dan Carpenter <error27@gmail.com>
| Closes: https://lore.kernel.org/r/20261129091029.VOYaTAQ6-lkp@intel.com/

smatch warnings:
arch/sh/drivers/dma/dma-api.c:130 dma_wait_for_completion() error: 'channel' dereferencing possible ERR_PTR()
arch/sh/drivers/dma/dma-api.c:157 dma_xfer() error: 'channel' dereferencing possible ERR_PTR()

vim +/channel +130 arch/sh/drivers/dma/dma-api.c

^1da177e4c3f41 Linus Torvalds 2005-04-16  124  
^1da177e4c3f41 Linus Torvalds 2005-04-16  125  void dma_wait_for_completion(unsigned int chan)
^1da177e4c3f41 Linus Torvalds 2005-04-16  126  {
^1da177e4c3f41 Linus Torvalds 2005-04-16  127  	struct dma_info *info = get_dma_info(chan);
db9b99d461ddbb Mark Glaisher  2006-11-24  128  	struct dma_channel *channel = get_dma_channel(chan);
^1da177e4c3f41 Linus Torvalds 2005-04-16  129  
^1da177e4c3f41 Linus Torvalds 2005-04-16 @130  	if (channel->flags & DMA_TEI_CAPABLE) {
^1da177e4c3f41 Linus Torvalds 2005-04-16  131  		wait_event(channel->wait_queue,
^1da177e4c3f41 Linus Torvalds 2005-04-16  132  			   (info->ops->get_residue(channel) == 0));
^1da177e4c3f41 Linus Torvalds 2005-04-16  133  		return;
^1da177e4c3f41 Linus Torvalds 2005-04-16  134  	}
^1da177e4c3f41 Linus Torvalds 2005-04-16  135  
^1da177e4c3f41 Linus Torvalds 2005-04-16  136  	while (info->ops->get_residue(channel))
^1da177e4c3f41 Linus Torvalds 2005-04-16  137  		cpu_relax();
^1da177e4c3f41 Linus Torvalds 2005-04-16  138  }
db9b99d461ddbb Mark Glaisher  2006-11-24  139  EXPORT_SYMBOL(dma_wait_for_completion);
db9b99d461ddbb Mark Glaisher  2006-11-24  140  
^1da177e4c3f41 Linus Torvalds 2005-04-16  141  void dma_configure_channel(unsigned int chan, unsigned long flags)
^1da177e4c3f41 Linus Torvalds 2005-04-16  142  {
^1da177e4c3f41 Linus Torvalds 2005-04-16  143  	struct dma_info *info = get_dma_info(chan);
db9b99d461ddbb Mark Glaisher  2006-11-24  144  	struct dma_channel *channel = get_dma_channel(chan);
^1da177e4c3f41 Linus Torvalds 2005-04-16  145  
^1da177e4c3f41 Linus Torvalds 2005-04-16  146  	if (info->ops->configure)
^1da177e4c3f41 Linus Torvalds 2005-04-16  147  		info->ops->configure(channel, flags);
^1da177e4c3f41 Linus Torvalds 2005-04-16  148  }
db9b99d461ddbb Mark Glaisher  2006-11-24  149  EXPORT_SYMBOL(dma_configure_channel);
^1da177e4c3f41 Linus Torvalds 2005-04-16  150  
^1da177e4c3f41 Linus Torvalds 2005-04-16  151  int dma_xfer(unsigned int chan, unsigned long from,
^1da177e4c3f41 Linus Torvalds 2005-04-16  152  	     unsigned long to, size_t size, unsigned int mode)
^1da177e4c3f41 Linus Torvalds 2005-04-16  153  {
^1da177e4c3f41 Linus Torvalds 2005-04-16  154  	struct dma_info *info = get_dma_info(chan);
db9b99d461ddbb Mark Glaisher  2006-11-24  155  	struct dma_channel *channel = get_dma_channel(chan);
^1da177e4c3f41 Linus Torvalds 2005-04-16  156  
^1da177e4c3f41 Linus Torvalds 2005-04-16 @157  	channel->sar	= from;
^1da177e4c3f41 Linus Torvalds 2005-04-16  158  	channel->dar	= to;
^1da177e4c3f41 Linus Torvalds 2005-04-16  159  	channel->count	= size;
^1da177e4c3f41 Linus Torvalds 2005-04-16  160  	channel->mode	= mode;
^1da177e4c3f41 Linus Torvalds 2005-04-16  161  
^1da177e4c3f41 Linus Torvalds 2005-04-16  162  	return info->ops->xfer(channel);
^1da177e4c3f41 Linus Torvalds 2005-04-16  163  }
db9b99d461ddbb Mark Glaisher  2006-11-24  164  EXPORT_SYMBOL(dma_xfer);
db9b99d461ddbb Mark Glaisher  2006-11-24  165  

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

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

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

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 14:23 [PATCH 0/2] x86: replace stack cookie hack with named address space Ard Biesheuvel
2026-11-27 14:23 ` [PATCH 1/2] compiler.h: Use the symbol's own pointer type in __ADDRESSABLE() Ard Biesheuvel
2026-11-27 14:23 ` [PATCH 2/2] x86/stackprotector: Use a named address space for the Clang stack guard Ard Biesheuvel
  -- strict thread matches above, loose matches on Subject: below --
2026-11-29  8:16 [PATCH 1/2] compiler.h: Use the symbol's own pointer type in __ADDRESSABLE() kernel test robot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.