powerpc/kasan: require memintrinsic prefix support for KASAN
PATCH v3 1/1 powerpc/kasan: require memintrinsic prefix support for KASAN
powerpc unconditionally selects GENERIC_ENTRY. The GENERIC_ENTRY infrastructure relies on the compiler emitting __asan_mem*() calls at instrumented mem*() sites rather than plain memset/memcpy/memmove, so that entry/exit paths calling those functions are not instrumented.
This assumption is encoded in two places:
mm/kasan/shadow.c:
#if !defined(CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX) &&
!defined(CONFIG_GENERIC_ENTRY)
include/linux/fortify-string.h:
#if !defined(CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX) &&
!defined(CONFIG_GENERIC_ENTRY)When GENERIC_ENTRY is set, both guards suppress the C wrappers for memset/memcpy/memmove and the __underlying_mem*() redirections. This is only safe when the compiler supports the prefixed __asan_mem*() intrinsics. On older toolchains (e.g. GCC 9) that lack this support, plain mem*() calls from instrumented code fall through to the raw assembly implementations in mem_64.S / copy_32.S, completely bypassing the KASAN shadow check.
Other arches with GENERIC_ENTRY (x86, s390, loongarch, riscv) do not hit this because their CI toolchains are always new enough to support the prefix flag.
Background: the !GENERIC_ENTRY guard was introduced by commit 69d4c0d32186 ("entry, kasan, x86: Disallow overriding mem*() functions", Peter Zijlstra, Jan 2023). The root problem is that the KASAN C wrappers override the linker symbol memset/memcpy/memmove globally, so any call from noinstr or __no_sanitize_address code (e.g. irqentry_enter/irqentry_exit) would still reach the KASAN shadow-check wrapper -- at a point where KASAN invariants may not hold. The compiler prefix approach (Marco Elver, Feb 2023, commit 51287dcb00cc) solves this by having the compiler emit __asan_memset at instrumented call sites and bare memset inside __no_sanitize_address functions, splitting the decision at code-generation time rather than at link time.
A manual C-level override cannot replicate this split: a single linker symbol cannot be made to resolve differently depending on the caller.
x86 also placed its raw memset/memcpy/memmove implementations in .noinstr.text (same commit, 69d4c0d32186), which is the other half of the fix: noinstr callers hit the raw assembly directly, safely bypassing KASAN. PowerPC has not done this. Placing mem_64.S / memcpy_64.S / copy_32.S implementations in .noinstr.text would be the complementary long-term fix that could re-enable KASAN on older toolchains, but it requires care around linker stub overflow on large PPC64 kernels (the same reason powerpc uses NOKPROBE_SYMBOL rather than noinstr for its interrupt handlers -- see the comment in asm/interrupt.h). That work is left as a follow-up.
For now, introduce PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX, an arch-local compiler probe that mirrors the same check as CC_HAS_KASAN_MEMINTRINSIC_PREFIX in lib/Kconfig.kasan but lives outside the 'if KASAN' block to avoid a recursive dependency (CC_HAS_KASAN_MEMINTRINSIC_PREFIX depends on KASAN which depends on HAVE_ARCH_KASAN). Gate the three HAVE_ARCH_KASAN selects on this new symbol so that KASAN is not offered as a config option on toolchains that cannot support it correctly with GENERIC_ENTRY.
Since KASAN on powerpc now unconditionally implies CC_HAS_KASAN_MEMINTRINSIC_PREFIX, the old !CC_HAS_KASAN_MEMINTRINSIC_PREFIX code paths in asm/kasan.h and asm/string.h are dead. Clean them up:
- asm/kasan.h: remove the dual-entry-point variant of _GLOBAL_KASAN / _GLOBAL_TOC_KASAN / EXPORT_SYMBOL_KASAN that emitted both memset and __memset as entry points to the same assembly. These aliases were only needed so the C KASAN wrappers in shadow.c could call __memset() to reach raw memory ops; with the compiler prefix approach those wrappers are not used for mem* on powerpc.
- asm/string.h: remove the separate __memset/__memcpy/__memmove symbol declarations and the memset/memcpy/memmove macro redirections for uninstrumented files that were needed on old toolchains. Simplify the CONFIG_KASAN block to just the three #define aliases (still used by shadow.c as raw backends to bypass KASAN checking).
- cputable.c, prom_init.c: the memcpy is not required now as the manual instrumentation of memcpy is removed. Hence directly use *dest = *src for this.
---
Changelog:
V2 -> V3:
- *dest = *src used instead of memcpy in cputable.c and prom_init.c
V2: https://lore.kernel.org/all/[email protected]
V1 -> V2:
- Comments reworded
- Commit message reworded
V1: https://lore.kernel.org/all/[email protected]
arch/powerpc/Kconfig | 10 +++++++--- arch/powerpc/include/asm/kasan.h | 11 ----------- arch/powerpc/include/asm/string.h | 21 +-------------------- arch/powerpc/kernel/cputable.c | 14 ++------------ arch/powerpc/kernel/prom_init.c | 10 ++-------- 5 files changed, 12 insertions(+), 54 deletions(-)
arch/powerpc/Kconfig +7 -3
@@ -7,6 +7,10 @@ config CC_HAS_ELFV2 config CC_HAS_PREFIXED def_bool PPC64 && $(cc-option, -mcpu=power10 -mprefixed) +config PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX + def_bool (CC_IS_CLANG && $(cc-option,-fsanitize=kernel-address -mllvm -asan-kernel-mem-intrinsic-prefix=1)) || \ + (CC_IS_GCC && $(cc-option,-fsanitize=kernel-address --param asan-kernel-mem-intrinsic-prefix=1)) + config CC_HAS_PCREL # Clang has a bug (https://github.com/llvm/llvm-project/issues/62372) # where pcrel code is not generated if -msoft-float, -mno-altivec, or
@@ -220,9 +224,9 @@ config PPC select HAVE_ARCH_HUGE_VMAP if PPC_RADIX_MMU || PPC_8xx select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_JUMP_LABEL_RELATIVE - select HAVE_ARCH_KASAN if PPC32 && PAGE_SHIFT <= 14 - select HAVE_ARCH_KASAN if PPC_RADIX_MMU - select HAVE_ARCH_KASAN if PPC_BOOK3E_64 + select HAVE_ARCH_KASAN if PPC32 && PAGE_SHIFT <= 14 && PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX + select HAVE_ARCH_KASAN if PPC_RADIX_MMU && PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX + select HAVE_ARCH_KASAN if PPC_BOOK3E_64 && PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX select HAVE_ARCH_KASAN_VMALLOC if HAVE_ARCH_KASAN select HAVE_ARCH_KCSAN select HAVE_ARCH_KFENCE if ARCH_SUPPORTS_DEBUG_PAGEALLOC
arch/powerpc/include/asm/kasan.h +0 -11
@@ -2,20 +2,9 @@#ifndef __ASM_KASAN_H#define __ASM_KASAN_H -#if defined(CONFIG_KASAN ) && !defined(CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX ) -#define _GLOBAL_KASAN (fn) \ - _GLOBAL(fn); \ - _GLOBAL(__##fn) -#define _GLOBAL_TOC_KASAN (fn) \ - _GLOBAL_TOC(fn); \ - _GLOBAL_TOC(__##fn) -#define EXPORT_SYMBOL_KASAN (fn) \ - EXPORT_SYMBOL(__##fn) -#else /* CONFIG_KASAN && !CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX */ #define _GLOBAL_KASAN (fn) _GLOBAL(fn)#define _GLOBAL_TOC_KASAN (fn) _GLOBAL_TOC(fn)#define EXPORT_SYMBOL_KASAN (fn) -#endif /* CONFIG_KASAN && !CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX */ #ifndef __ASSEMBLER__
arch/powerpc/include/asm/string.h +1 -20
@@ -29,29 +29,10 @@ extern void * memchr(const void *,int,__kernel_size_t);void memcpy_flushcache (void *dest,const void *src,size_t size);#ifdef CONFIG_KASAN -/* __mem variants are used by KASAN to implement instrumented meminstrinsics. */ -#ifdef CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX +/* Used by mm/kasan/shadow.c as raw backends to bypass KASAN checking. */ #define __memset memset#define __memcpy memcpy#define __memmove memmove -#else /* CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX */ -void *__memset (void *s,int c,__kernel_size_t count); -void *__memcpy (void *to,const void *from,__kernel_size_t n); -void *__memmove (void *to,const void *from,__kernel_size_t n); -#ifndef __SANITIZE_ADDRESS__ -/* -* For files that are not instrumented (e.g. mm/slub.c) we -* should use not instrumented version of mem* functions. -*/ -#define memcpy (dst, src, len) __memcpy(dst, src, len) -#define memmove (dst, src, len) __memmove(dst, src, len) -#define memset (s, c, n) __memset(s, c, n) - -#ifndef __NO_FORTIFY -#define __NO_FORTIFY/* FORTIFY_SOURCE uses __builtin_memcpy, etc. */ -#endif -#endif /* !__SANITIZE_ADDRESS__ */ -#endif /* CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX */ #endif /* CONFIG_KASAN */ #ifdef CONFIG_PPC64
arch/powerpc/kernel/cputable.c +2 -12
@@ -35,12 +35,7 @@ void __init set_cur_cpu_spec(struct cpu_spec *s)struct cpu_spec *t = &the_cpu_spec; t =PTRRELOC (t); -/* -* use memcpy() instead of *t = *s so that GCC replaces it -* by __memcpy() when KASAN is active -*/ -memcpy (t, s,sizeof (*t)); - + *t = *s; *PTRRELOC (&cur_cpu_spec) = &the_cpu_spec; }
@@ -52,12 +47,7 @@ static struct cpu_spec * __init setup_cpu_spec(unsigned long offset, t =PTRRELOC (t); old = *t; - -/* -* Copy everything, then do fixups. Use memcpy() instead of *t = *s -* so that GCC replaces it by __memcpy() when KASAN is active -*/ -memcpy (t, s,sizeof (*t)); + *t = *s;/* * If we are overriding a previous value derived from the real
arch/powerpc/kernel/prom_init.c +2 -8
@@ -1362,14 +1362,8 @@ static void __init prom_check_platform_support(void)int prop_len =prom_getproplen (prom.chosen,"ibm,arch-vec-5-platform-support" ); -/* -* First copy the architecture vec template -* -* use memcpy() instead of *vec = *vec_template so that GCC replaces it -* by __memcpy() when KASAN is active -*/ -memcpy (&ibm_architecture_vec, &ibm_architecture_vec_template, -sizeof (ibm_architecture_vec)); +/* First copy the architecture vec template */ + ibm_architecture_vec = ibm_architecture_vec_template;prom_strscpy_pad (ibm_architecture_vec.vec7.os_id, linux_banner,256 );
Signature · 1 line
2.55.0
Le 15/09/2026 à 12:09, Mukesh Kumar Chaurasiya (IBM) a écrit :
Quoted 78 lines
powerpc unconditionally selects
GENERIC_ENTRY. TheGENERIC_ENTRYinfrastructure relies on the compiler emitting __asan_mem*() calls at instrumented mem*() sites rather than plain memset/memcpy/memmove, so that entry/exit paths calling those functions are not instrumented.This assumption is encoded in two places:
mm/kasan/shadow.c: #if !defined(CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX) && !defined(CONFIG_GENERIC_ENTRY) include/linux/fortify-string.h: #if !defined(CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX) && !defined(CONFIG_GENERIC_ENTRY)When
GENERIC_ENTRYis set, both guards suppress the C wrappers for memset/memcpy/memmove and the __underlying_mem*() redirections. This is only safe when the compiler supports the prefixed __asan_mem*() intrinsics. On older toolchains (e.g. GCC 9) that lack this support, plain mem*() calls from instrumented code fall through to the raw assembly implementations inmem_64.S/copy_32.S, completely bypassing the KASAN shadow check.Other arches with
GENERIC_ENTRY(x86, s390, loongarch, riscv) do not hit this because their CI toolchains are always new enough to support the prefix flag.Background: the !GENERIC_ENTRY guard was introduced by commit
69d4c0d32186("entry, kasan, x86: Disallow overriding mem*() functions", Peter Zijlstra, Jan 2023). The root problem is that the KASAN C wrappers override the linker symbol memset/memcpy/memmove globally, so any call from noinstr or__no_sanitize_addresscode (e.g. irqentry_enter/irqentry_exit) would still reach the KASAN shadow-check wrapper -- at a point where KASAN invariants may not hold. The compiler prefix approach (Marco Elver, Feb 2023, commit51287dcb00cc) solves this by having the compiler emit__asan_memsetat instrumented call sites and bare memset inside__no_sanitize_addressfunctions, splitting the decision at code-generation time rather than at link time.A manual C-level override cannot replicate this split: a single linker symbol cannot be made to resolve differently depending on the caller.
x86 also placed its raw memset/memcpy/memmove implementations in .noinstr.text (same commit,
69d4c0d32186), which is the other half of the fix: noinstr callers hit the raw assembly directly, safely bypassing KASAN. PowerPC has not done this. Placingmem_64.S/memcpy_64.S/copy_32.Simplementations in .noinstr.text would be the complementary long-term fix that could re-enable KASAN on older toolchains, but it requires care around linker stub overflow on large PPC64 kernels (the same reason powerpc usesNOKPROBE_SYMBOLrather than noinstr for its interrupt handlers -- see the comment inasm/interrupt.h). That work is left as a follow-up.For now, introduce
PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX, an arch-local compiler probe that mirrors the same check asCC_HAS_KASAN_MEMINTRINSIC_PREFIXinlib/Kconfig.kasanbut lives outside the 'if KASAN' block to avoid a recursive dependency (CC_HAS_KASAN_MEMINTRINSIC_PREFIXdepends on KASAN which depends onHAVE_ARCH_KASAN). Gate the threeHAVE_ARCH_KASANselects on this new symbol so that KASAN is not offered as a config option on toolchains that cannot support it correctly withGENERIC_ENTRY.Since KASAN on powerpc now unconditionally implies
CC_HAS_KASAN_MEMINTRINSIC_PREFIX, the old !CC_HAS_KASAN_MEMINTRINSIC_PREFIX code paths inasm/kasan.handasm/string.hare dead. Clean them up:-
asm/kasan.h: remove the dual-entry-point variant of_GLOBAL_KASAN/_GLOBAL_TOC_KASAN/EXPORT_SYMBOL_KASANthat emitted both memset and__memsetas entry points to the same assembly. These aliases were only needed so the C KASAN wrappers inshadow.ccould call__memset()to reach raw memory ops; with the compiler prefix approach those wrappers are not used for mem* on powerpc.-
asm/string.h: remove the separate __memset/__memcpy/__memmove symbol declarations and the memset/memcpy/memmove macro redirections for uninstrumented files that were needed on old toolchains. Simplify theCONFIG_KASANblock to just the three #define aliases (still used byshadow.cas raw backends to bypass KASAN checking).
Quoted 8 lines
-
cputable.c,prom_init.c: the memcpy is not required now as the manual instrumentation of memcpy is removed. Hence directly use *dest = *src for this.
Quoted 4 lines
---
Changelog:
V2 -> V3:
- *dest = *src used instead of memcpy incputable.candprom_init.c
We could have left the memcpy() there but I'm fine either way. What was important was to remove the comment as it was valid only for the non-CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX case.
If for some reason you have to send a new version, it would be good to mention it is a revert of commit adcf59187e27 ("powerpc: don't use direct assignation during early boot.") as it is not necessary anymore with CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX
Christophe
Quoted 155 lines
V2: https://lore.kernel.org/all/[email protected]
V1 -> V2:
- Comments reworded
- Commit message reworded
V1: https://lore.kernel.org/all/[email protected]arch/powerpc/Kconfig | 10 +++++++--- arch/powerpc/include/asm/kasan.h | 11 ----------- arch/powerpc/include/asm/string.h | 21 +-------------------- arch/powerpc/kernel/cputable.c | 14 ++------------ arch/powerpc/kernel/prom_init.c | 10 ++-------- 5 files changed, 12 insertions(+), 54 deletions(-)arch/powerpc/Kconfig +7
-3@@ -7,6 +7,10 @@ config CC_HAS_ELFV2 config CC_HAS_PREFIXED def_bool PPC64 && $(cc-option, -mcpu=power10 -mprefixed) +config PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX + def_bool (CC_IS_CLANG && $(cc-option,-fsanitize=kernel-address -mllvm -asan-kernel-mem-intrinsic-prefix=1)) || \ + (CC_IS_GCC && $(cc-option,-fsanitize=kernel-address --param asan-kernel-mem-intrinsic-prefix=1)) + config CC_HAS_PCREL # Clang has a bug (https://github.com/llvm/llvm-project/issues/62372) # where pcrel code is not generated if -msoft-float, -mno-altivec, or@@ -220,9 +224,9 @@ config PPC select HAVE_ARCH_HUGE_VMAP if PPC_RADIX_MMU || PPC_8xx select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_JUMP_LABEL_RELATIVE - select HAVE_ARCH_KASAN if PPC32 && PAGE_SHIFT <= 14 - select HAVE_ARCH_KASAN if PPC_RADIX_MMU - select HAVE_ARCH_KASAN if PPC_BOOK3E_64 + select HAVE_ARCH_KASAN if PPC32 && PAGE_SHIFT <= 14 && PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX + select HAVE_ARCH_KASAN if PPC_RADIX_MMU && PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX + select HAVE_ARCH_KASAN if PPC_BOOK3E_64 && PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX select HAVE_ARCH_KASAN_VMALLOC if HAVE_ARCH_KASAN select HAVE_ARCH_KCSAN select HAVE_ARCH_KFENCE if ARCH_SUPPORTS_DEBUG_PAGEALLOCarch/powerpc/include/asm/kasan.h +0
-11@@ -2,20 +2,9 @@#ifndef __ASM_KASAN_H#define __ASM_KASAN_H -#if defined(CONFIG_KASAN ) && !defined(CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX ) -#define _GLOBAL_KASAN (fn) \ - _GLOBAL(fn); \ - _GLOBAL(__##fn) -#define _GLOBAL_TOC_KASAN (fn) \ - _GLOBAL_TOC(fn); \ - _GLOBAL_TOC(__##fn) -#define EXPORT_SYMBOL_KASAN (fn) \ - EXPORT_SYMBOL(__##fn) -#else /* CONFIG_KASAN && !CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX */ #define _GLOBAL_KASAN (fn) _GLOBAL(fn)#define _GLOBAL_TOC_KASAN (fn) _GLOBAL_TOC(fn)#define EXPORT_SYMBOL_KASAN (fn) -#endif /* CONFIG_KASAN && !CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX */ #ifndef __ASSEMBLER__arch/powerpc/include/asm/string.h +1
-20@@ -29,29 +29,10 @@ extern void * memchr(const void *,int,__kernel_size_t);void memcpy_flushcache (void *dest,const void *src,size_t size);#ifdef CONFIG_KASAN -/* __mem variants are used by KASAN to implement instrumented meminstrinsics. */ -#ifdef CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX +/* Used by mm/kasan/shadow.c as raw backends to bypass KASAN checking. */ #define __memset memset#define __memcpy memcpy#define __memmove memmove -#else /* CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX */ -void *__memset (void *s,int c,__kernel_size_t count); -void *__memcpy (void *to,const void *from,__kernel_size_t n); -void *__memmove (void *to,const void *from,__kernel_size_t n); -#ifndef __SANITIZE_ADDRESS__ -/* -* For files that are not instrumented (e.g. mm/slub.c) we -* should use not instrumented version of mem* functions. -*/ -#define memcpy (dst, src, len) __memcpy(dst, src, len) -#define memmove (dst, src, len) __memmove(dst, src, len) -#define memset (s, c, n) __memset(s, c, n) - -#ifndef __NO_FORTIFY -#define __NO_FORTIFY/* FORTIFY_SOURCE uses __builtin_memcpy, etc. */ -#endif -#endif /* !__SANITIZE_ADDRESS__ */ -#endif /* CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX */ #endif /* CONFIG_KASAN */ #ifdef CONFIG_PPC64 arch/powerpc/kernel/cputable.c +2
-12@@ -35,12 +35,7 @@ void __init set_cur_cpu_spec(struct cpu_spec *s)struct cpu_spec *t = &the_cpu_spec; t =PTRRELOC (t); -/* -* use memcpy() instead of *t = *s so that GCC replaces it -* by __memcpy() when KASAN is active -*/ -memcpy (t, s,sizeof (*t)); - + *t = *s; *PTRRELOC (&cur_cpu_spec) = &the_cpu_spec; }@@ -52,12 +47,7 @@ static struct cpu_spec * __init setup_cpu_spec(unsigned long offset, t =PTRRELOC (t); old = *t; - -/* -* Copy everything, then do fixups. Use memcpy() instead of *t = *s -* so that GCC replaces it by __memcpy() when KASAN is active -*/ -memcpy (t, s,sizeof (*t)); + *t = *s;/* * If we are overriding a previous value derived from the real arch/powerpc/kernel/prom_init.c +2
-8@@ -1362,14 +1362,8 @@ static void __init prom_check_platform_support(void)int prop_len =prom_getproplen (prom.chosen,"ibm,arch-vec-5-platform-support" ); -/* -* First copy the architecture vec template -* -* use memcpy() instead of *vec = *vec_template so that GCC replaces it -* by __memcpy() when KASAN is active -*/ -memcpy (&ibm_architecture_vec, &ibm_architecture_vec_template, -sizeof (ibm_architecture_vec)); +/* First copy the architecture vec template */ + ibm_architecture_vec = ibm_architecture_vec_template;prom_strscpy_pad (ibm_architecture_vec.vec7.os_id, linux_banner,256 );
Quoted 92 lines · Christophe Leroy (CS GROUP)
Le 15/09/2026 à 12:09, Mukesh Kumar Chaurasiya (IBM) a écrit :
Quoted 78 lines
powerpc unconditionally selects
GENERIC_ENTRY. TheGENERIC_ENTRYinfrastructure relies on the compiler emitting __asan_mem*() calls at instrumented mem*() sites rather than plain memset/memcpy/memmove, so that entry/exit paths calling those functions are not instrumented.This assumption is encoded in two places:
mm/kasan/shadow.c: #if !defined(CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX) && !defined(CONFIG_GENERIC_ENTRY) include/linux/fortify-string.h: #if !defined(CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX) && !defined(CONFIG_GENERIC_ENTRY)When
GENERIC_ENTRYis set, both guards suppress the C wrappers for memset/memcpy/memmove and the __underlying_mem*() redirections. This is only safe when the compiler supports the prefixed __asan_mem*() intrinsics. On older toolchains (e.g. GCC 9) that lack this support, plain mem*() calls from instrumented code fall through to the raw assembly implementations inmem_64.S/copy_32.S, completely bypassing the KASAN shadow check.Other arches with
GENERIC_ENTRY(x86, s390, loongarch, riscv) do not hit this because their CI toolchains are always new enough to support the prefix flag.Background: the !GENERIC_ENTRY guard was introduced by commit
69d4c0d32186("entry, kasan, x86: Disallow overriding mem*() functions", Peter Zijlstra, Jan 2023). The root problem is that the KASAN C wrappers override the linker symbol memset/memcpy/memmove globally, so any call from noinstr or__no_sanitize_addresscode (e.g. irqentry_enter/irqentry_exit) would still reach the KASAN shadow-check wrapper -- at a point where KASAN invariants may not hold. The compiler prefix approach (Marco Elver, Feb 2023, commit51287dcb00cc) solves this by having the compiler emit__asan_memsetat instrumented call sites and bare memset inside__no_sanitize_addressfunctions, splitting the decision at code-generation time rather than at link time.A manual C-level override cannot replicate this split: a single linker symbol cannot be made to resolve differently depending on the caller.
x86 also placed its raw memset/memcpy/memmove implementations in .noinstr.text (same commit,
69d4c0d32186), which is the other half of the fix: noinstr callers hit the raw assembly directly, safely bypassing KASAN. PowerPC has not done this. Placingmem_64.S/memcpy_64.S/copy_32.Simplementations in .noinstr.text would be the complementary long-term fix that could re-enable KASAN on older toolchains, but it requires care around linker stub overflow on large PPC64 kernels (the same reason powerpc usesNOKPROBE_SYMBOLrather than noinstr for its interrupt handlers -- see the comment inasm/interrupt.h). That work is left as a follow-up.For now, introduce
PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX, an arch-local compiler probe that mirrors the same check asCC_HAS_KASAN_MEMINTRINSIC_PREFIXinlib/Kconfig.kasanbut lives outside the 'if KASAN' block to avoid a recursive dependency (CC_HAS_KASAN_MEMINTRINSIC_PREFIXdepends on KASAN which depends onHAVE_ARCH_KASAN). Gate the threeHAVE_ARCH_KASANselects on this new symbol so that KASAN is not offered as a config option on toolchains that cannot support it correctly withGENERIC_ENTRY.Since KASAN on powerpc now unconditionally implies
CC_HAS_KASAN_MEMINTRINSIC_PREFIX, the old !CC_HAS_KASAN_MEMINTRINSIC_PREFIX code paths inasm/kasan.handasm/string.hare dead. Clean them up:-
asm/kasan.h: remove the dual-entry-point variant of_GLOBAL_KASAN/_GLOBAL_TOC_KASAN/EXPORT_SYMBOL_KASANthat emitted both memset and__memsetas entry points to the same assembly. These aliases were only needed so the C KASAN wrappers inshadow.ccould call__memset()to reach raw memory ops; with the compiler prefix approach those wrappers are not used for mem* on powerpc.-
asm/string.h: remove the separate __memset/__memcpy/__memmove symbol declarations and the memset/memcpy/memmove macro redirections for uninstrumented files that were needed on old toolchains. Simplify theCONFIG_KASANblock to just the three #define aliases (still used byshadow.cas raw backends to bypass KASAN checking).Quoted 8 lines
-
cputable.c,prom_init.c: the memcpy is not required now as the manual instrumentation of memcpy is removed. Hence directly use *dest = *src for this.Reviewed-by Christophe Leroy (CS GROUP) <[email protected]>
Quoted 9 lines
Quoted 4 lines
---
Changelog:
V2 -> V3:
- *dest = *src used instead of memcpy incputable.candprom_init.cWe could have left the
memcpy()there but I'm fine either way. What was important was to remove the comment as it was valid only for the non-CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX case.
I thought this would make the code look simpler.
Quoted 5 lines
If for some reason you have to send a new version, it would be good to mention it is a revert of commit
adcf59187e27("powerpc: don't use direct assignation during early boot.") as it is not necessary anymore withCONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX
I can send out a new version, it's just a commit message change.
Quoted 2 lines
Christophe
Thanks for all the effort in review and explanation.
Regards,
Mukesh
Quoted 143 lines
Quoted 142 lines
V2: https://lore.kernel.org/all/[email protected]
V1 -> V2:
- Comments reworded
- Commit message reworded
V1: https://lore.kernel.org/all/[email protected]arch/powerpc/Kconfig | 10 +++++++--- arch/powerpc/include/asm/kasan.h | 11 ----------- arch/powerpc/include/asm/string.h | 21 +-------------------- arch/powerpc/kernel/cputable.c | 14 ++------------ arch/powerpc/kernel/prom_init.c | 10 ++-------- 5 files changed, 12 insertions(+), 54 deletions(-)arch/powerpc/Kconfig +7
-3@@ -7,6 +7,10 @@ config CC_HAS_ELFV2 config CC_HAS_PREFIXED def_bool PPC64 && $(cc-option, -mcpu=power10 -mprefixed) +config PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX + def_bool (CC_IS_CLANG && $(cc-option,-fsanitize=kernel-address -mllvm -asan-kernel-mem-intrinsic-prefix=1)) || \ + (CC_IS_GCC && $(cc-option,-fsanitize=kernel-address --param asan-kernel-mem-intrinsic-prefix=1)) + config CC_HAS_PCREL # Clang has a bug (https://github.com/llvm/llvm-project/issues/62372) # where pcrel code is not generated if -msoft-float, -mno-altivec, or@@ -220,9 +224,9 @@ config PPC select HAVE_ARCH_HUGE_VMAP if PPC_RADIX_MMU || PPC_8xx select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_JUMP_LABEL_RELATIVE - select HAVE_ARCH_KASAN if PPC32 && PAGE_SHIFT <= 14 - select HAVE_ARCH_KASAN if PPC_RADIX_MMU - select HAVE_ARCH_KASAN if PPC_BOOK3E_64 + select HAVE_ARCH_KASAN if PPC32 && PAGE_SHIFT <= 14 && PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX + select HAVE_ARCH_KASAN if PPC_RADIX_MMU && PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX + select HAVE_ARCH_KASAN if PPC_BOOK3E_64 && PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX select HAVE_ARCH_KASAN_VMALLOC if HAVE_ARCH_KASAN select HAVE_ARCH_KCSAN select HAVE_ARCH_KFENCE if ARCH_SUPPORTS_DEBUG_PAGEALLOCarch/powerpc/include/asm/kasan.h +0
-11@@ -2,20 +2,9 @@#ifndef __ASM_KASAN_H#define __ASM_KASAN_H -#if defined(CONFIG_KASAN ) && !defined(CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX ) -#define _GLOBAL_KASAN (fn) \ - _GLOBAL(fn); \ - _GLOBAL(__##fn) -#define _GLOBAL_TOC_KASAN (fn) \ - _GLOBAL_TOC(fn); \ - _GLOBAL_TOC(__##fn) -#define EXPORT_SYMBOL_KASAN (fn) \ - EXPORT_SYMBOL(__##fn) -#else /* CONFIG_KASAN && !CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX */ #define _GLOBAL_KASAN (fn) _GLOBAL(fn)#define _GLOBAL_TOC_KASAN (fn) _GLOBAL_TOC(fn)#define EXPORT_SYMBOL_KASAN (fn) -#endif /* CONFIG_KASAN && !CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX */ #ifndef __ASSEMBLER__arch/powerpc/include/asm/string.h +1
-20@@ -29,29 +29,10 @@ extern void * memchr(const void *,int,__kernel_size_t);void memcpy_flushcache (void *dest,const void *src,size_t size);#ifdef CONFIG_KASAN -/* __mem variants are used by KASAN to implement instrumented meminstrinsics. */ -#ifdef CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX +/* Used by mm/kasan/shadow.c as raw backends to bypass KASAN checking. */ #define __memset memset#define __memcpy memcpy#define __memmove memmove -#else /* CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX */ -void *__memset (void *s,int c,__kernel_size_t count); -void *__memcpy (void *to,const void *from,__kernel_size_t n); -void *__memmove (void *to,const void *from,__kernel_size_t n); -#ifndef __SANITIZE_ADDRESS__ -/* -* For files that are not instrumented (e.g. mm/slub.c) we -* should use not instrumented version of mem* functions. -*/ -#define memcpy (dst, src, len) __memcpy(dst, src, len) -#define memmove (dst, src, len) __memmove(dst, src, len) -#define memset (s, c, n) __memset(s, c, n) - -#ifndef __NO_FORTIFY -#define __NO_FORTIFY/* FORTIFY_SOURCE uses __builtin_memcpy, etc. */ -#endif -#endif /* !__SANITIZE_ADDRESS__ */ -#endif /* CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX */ #endif /* CONFIG_KASAN */ #ifdef CONFIG_PPC64 arch/powerpc/kernel/cputable.c +2
-12@@ -35,12 +35,7 @@ void __init set_cur_cpu_spec(struct cpu_spec *s)struct cpu_spec *t = &the_cpu_spec; t =PTRRELOC (t); -/* -* use memcpy() instead of *t = *s so that GCC replaces it -* by __memcpy() when KASAN is active -*/ -memcpy (t, s,sizeof (*t)); - + *t = *s; *PTRRELOC (&cur_cpu_spec) = &the_cpu_spec; }@@ -52,12 +47,7 @@ static struct cpu_spec * __init setup_cpu_spec(unsigned long offset, t =PTRRELOC (t); old = *t; - -/* -* Copy everything, then do fixups. Use memcpy() instead of *t = *s -* so that GCC replaces it by __memcpy() when KASAN is active -*/ -memcpy (t, s,sizeof (*t)); + *t = *s;/* * If we are overriding a previous value derived from the real arch/powerpc/kernel/prom_init.c +2
-8@@ -1362,14 +1362,8 @@ static void __init prom_check_platform_support(void)int prop_len =prom_getproplen (prom.chosen,"ibm,arch-vec-5-platform-support" ); -/* -* First copy the architecture vec template -* -* use memcpy() instead of *vec = *vec_template so that GCC replaces it -* by __memcpy() when KASAN is active -*/ -memcpy (&ibm_architecture_vec, &ibm_architecture_vec_template, -sizeof (ibm_architecture_vec)); +/* First copy the architecture vec template */ + ibm_architecture_vec = ibm_architecture_vec_template;prom_strscpy_pad (ibm_architecture_vec.vec7.os_id, linux_banner,256 );
Andrey Ryabinin
maintainer"Mukesh Kumar Chaurasiya (IBM)" <[email protected]> writes:
Hi,
I fed this patch to an AI for review, and the review results are included below. Please take a look. From my side, I agree with all of the points raised by AI in the
review, and I think they all need to be addressed. There is also a diff with the suggested changes at the very end of this mail.
Quoted 19 lines
powerpc unconditionally selects
GENERIC_ENTRY. TheGENERIC_ENTRYinfrastructure relies on the compiler emitting __asan_mem*() calls at instrumented mem*() sites rather than plain memset/memcpy/memmove, so that entry/exit paths calling those functions are not instrumented.[ ... ]
When
GENERIC_ENTRYis set, both guards suppress the C wrappers for memset/memcpy/memmove and the __underlying_mem*() redirections. This is only safe when the compiler supports the prefixed __asan_mem*() intrinsics. On older toolchains (e.g. GCC 9) that lack this support, plain mem*() calls from instrumented code fall through to the raw assembly implementations inmem_64.S/copy_32.S, completely bypassing the KASAN shadow check.Other arches with
GENERIC_ENTRY(x86, s390, loongarch, riscv) do not hit this because their CI toolchains are always new enough to support the prefix flag.
Quoted 6 lines
[ ... ]
The thread in the Closes: link is about an early boot hang, but the commit message only describes mem*() calls bypassing the shadow check. Bypassed checks would lose coverage, not hang the machine. Is the mechanism of the hang understood?
Looking at the state before this commit, the !CC_HAS_KASAN_MEMINTRINSIC_PREFIX macros that bee25f97ad24 ("powerpc: Enable GENERIC_ENTRY feature") added to asm/kasan.h emit two ELFv2 global entry points back to back:
#define _GLOBAL_TOC_KASAN(fn)
_GLOBAL_TOC(fn);
_GLOBAL_TOC(__##fn)With the _GLOBAL_TOC() definition from asm/ppc_asm.h that expands, for memcpy_64.S, to:
memcpy:
0: addis r2,r12,(.TOC.-0b)@ha
addi r2,r2,(.TOC.-0b)@l
.localentry memcpy,.-memcpy <- local entry is memcpy+8
__memcpy:
0: addis r2,r12,(.TOC.-0b)@ha <- this is memcpy+8
addi r2,r2,(.TOC.-0b)@l
.localentry __memcpy,.-__memcpyAssembling exactly that for powerpc64le gives memcpy st_other 0x60 (local entry offset 8), and memcpy+8 is the __memcpy TOC prologue.
Every same-TOC caller of memcpy() or memmove() is resolved by the linker to the local entry, so it lands on that second prologue with r12 holding whatever the caller left there, and returns with r2 pointing at garbage. Same-TOC callers do not reload r2 after the call. With GENERIC_ENTRY, mm/kasan/shadow.c no longer provides memcpy(), so on a toolchain without the prefix parameter every instrumented file calls the memcpy symbol directly and hits this. That matches an early hang that only shows up with GCC 9.
This commit makes the hang go away because the dual-entry macro is deleted, but the commit message attributes the fix to something else. Could the message describe the r2 corruption, and since this repairs a regression from the GENERIC_ENTRY conversion, should it carry:
On the claim that other GENERIC_ENTRY architectures "do not hit this because their CI toolchains are always new enough": x86, s390, riscv and loongarch with GCC 8 to 12 build exactly the same configuration, CONFIG_KASAN=y without CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX, and run with mem*() unchecked. scripts/Makefile.kasan documents that as the intended behaviour:
# Instrument memcpy/memset/memmove calls by using instrumented __asan_mem*()
# instead. With compilers that don't support this option, compiler-inserted # memintrinsics won't be checked by KASAN on GENERIC_ENTRY architectures.
and mm/kasan/kasan_test_c.c skips the affected tests with "Test requires checked mem*()". So the situation the message describes is the accepted upstream state for old toolchains, not something specific to powerpc.
Quoted 15 lines
arch/powerpc/Kconfig +4
-0@@ -7,6 +7,10 @@ config CC_HAS_ELFV2 config CC_HAS_PREFIXED def_bool PPC64 && $(cc-option, -mcpu=power10 -mprefixed) +config PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX + def_bool (CC_IS_CLANG && $(cc-option,-fsanitize=kernel-address -mllvm -asan-kernel-mem-intrinsic-prefix=1)) || \ + (CC_IS_GCC && $(cc-option,-fsanitize=kernel-address --param asan-kernel-mem-intrinsic-prefix=1)) + config CC_HAS_PCREL # Clang has a bug (https://github.com/llvm/llvm-project/issues/62372) # where pcrel code is not generated if -msoft-float, -mno-altivec, or
[ ... ]
arch/powerpc/Kconfig config PPC · line 232
Quoted 13 lines
arch/powerpc/Kconfig +3
-3@@ -220,9 +224,9 @@ config PPC select HAVE_ARCH_HUGE_VMAP if PPC_RADIX_MMU || PPC_8xx select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_JUMP_LABEL_RELATIVE - select HAVE_ARCH_KASAN if PPC32 && PAGE_SHIFT <= 14 - select HAVE_ARCH_KASAN if PPC_RADIX_MMU - select HAVE_ARCH_KASAN if PPC_BOOK3E_64 + select HAVE_ARCH_KASAN if PPC32 && PAGE_SHIFT <= 14 && PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX + select HAVE_ARCH_KASAN if PPC_RADIX_MMU && PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX + select HAVE_ARCH_KASAN if PPC_BOOK3E_64 && PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX select HAVE_ARCH_KASAN_VMALLOC if HAVE_ARCH_KASAN select HAVE_ARCH_KCSAN select HAVE_ARCH_KFENCE if ARCH_SUPPORTS_DEBUG_PAGEALLOC
This drops KASAN from powerpc entirely for GCC 8 through 12, which are inside the supported range in Documentation/process/changes.rst (GNU C 8.1 minimum). An existing .config with CONFIG_KASAN=y silently loses it on olddefconfig once HAVE_ARCH_KASAN is no longer selected.
Given that the actual breakage is the broken memcpy/memmove entry points, is it necessary to go this far? Keeping one _GLOBAL_TOC() prologue for __memcpy and making memcpy a plain alias of it (a second label plus a matching .localentry, or a global entry that branches to __memcpy) would restore the pre-GENERIC_ENTRY behaviour, with mem*() unchecked on old compilers exactly like the other GENERIC_ENTRY architectures. That also keeps a Fixes-tagged backport candidate from removing a feature on stable kernels.
Quoted 15 lines
arch/powerpc/include/asm/kasan.h +0
-7@@ -2,20 +2,9 @@#ifndef __ASM_KASAN_H#define __ASM_KASAN_H -#if defined(CONFIG_KASAN ) && !defined(CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX ) -#define _GLOBAL_KASAN (fn) \ - _GLOBAL(fn); \ - _GLOBAL(__##fn) -#define _GLOBAL_TOC_KASAN (fn) \ - _GLOBAL_TOC(fn); \ - _GLOBAL_TOC(__##fn)
arch/powerpc/include/asm/kasan.h line 10
Quoted 10 lines
arch/powerpc/include/asm/kasan.h +0
-4-#define EXPORT_SYMBOL_KASAN (fn) \ - EXPORT_SYMBOL(__##fn) -#else /* CONFIG_KASAN && !CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX */ #define _GLOBAL_KASAN (fn) _GLOBAL(fn)#define _GLOBAL_TOC_KASAN (fn) _GLOBAL_TOC(fn)#define EXPORT_SYMBOL_KASAN (fn) -#endif /* CONFIG_KASAN && !CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX */ #ifndef __ASSEMBLER__
this isn't a bug, but after this change _GLOBAL_KASAN(), _GLOBAL_TOC_KASAN() and EXPORT_SYMBOL_KASAN() are unconditional identity macros with a single empty one. Should the five users in mem_64.S, memcpy_64.S and copy_32.S switch to _GLOBAL()/_GLOBAL_TOC() and the macros go away?
Related leftover: arch/powerpc/kernel/prom_init_check.sh still has
has_renamed_memintrinsics()
{
grep -q "^CONFIG_KASAN=y$" "${KCONFIG_CONFIG}" && \
! grep -q
"^CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX=y" "${KCONFIG_CONFIG}"
}
if has_renamed_memintrinsics
then
MEM_FUNCS="__memcpy __memset"
which can no longer be true on powerpc after this commit. Should that branch be removed in the same cleanup?
arch/powerpc/include/asm/string.h extern void * memchr(const void *,int,__kernel_size_t); · line 33
Quoted 12 lines
arch/powerpc/include/asm/string.h +1
-2@@ -29,29 +29,10 @@ extern void * memchr(const void *,int,__kernel_size_t);void memcpy_flushcache (void *dest,const void *src,size_t size);#ifdef CONFIG_KASAN -/* __mem variants are used by KASAN to implement instrumented meminstrinsics. */ -#ifdef CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX +/* Used by mm/kasan/shadow.c as raw backends to bypass KASAN checking. */ #define __memset memset
this isn't a bug, but the new comment names only mm/kasan/shadow.c. The same __memset()/__memcpy() names are used by mm/kasan/generic.c as well (DEFINE_ASAN_SET_SHADOW() and release_alloc_meta()), so would "used by mm/kasan as raw backends" be more accurate?
---
arch/powerpc/Kconfig +0 -4
@@ -7,10 +7,6 @@ config CC_HAS_ELFV2 config CC_HAS_PREFIXED def_bool PPC64 && $(cc-option, -mcpu=power10 -mprefixed) -config PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX - def_bool (CC_IS_CLANG && $(cc-option,-fsanitize=kernel-address -mllvm -asan-kernel-mem-intrinsic-prefix=1)) || \ - (CC_IS_GCC && $(cc-option,-fsanitize=kernel-address --param
asan-kernel-mem-intrinsic-prefix=1))
-
config CC_HAS_PCREL
# Clang has a bug (https://github.com/llvm/llvm-project/issues/62372)
# where pcrel code is not generated if -msoft-float, -mno-altivec, or @@ -224,9 +220,9 @@ config PPC select HAVE_ARCH_HUGE_VMAP if PPC_RADIX_MMU || PPC_8xx select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_JUMP_LABEL_RELATIVE - select HAVE_ARCH_KASAN if PPC32 && PAGE_SHIFT <= 14 && PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX - select HAVE_ARCH_KASAN if PPC_RADIX_MMU && PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX - select HAVE_ARCH_KASAN if PPC_BOOK3E_64 && PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX + select HAVE_ARCH_KASAN if PPC32 && PAGE_SHIFT <= 14 + select HAVE_ARCH_KASAN if PPC_RADIX_MMU + select HAVE_ARCH_KASAN if PPC_BOOK3E_64 select HAVE_ARCH_KASAN_VMALLOC if HAVE_ARCH_KASAN select HAVE_ARCH_KCSAN select HAVE_ARCH_KFENCE if ARCH_SUPPORTS_DEBUG_PAGEALLOC
arch/powerpc/include/asm/kasan.h +0 -4
@@ -2,10 +2,6 @@#ifndef __ASM_KASAN_H#define __ASM_KASAN_H -#define _GLOBAL_KASAN (fn) _GLOBAL(fn) -#define _GLOBAL_TOC_KASAN (fn) _GLOBAL_TOC(fn) -#define EXPORT_SYMBOL_KASAN (fn) -#ifndef __ASSEMBLER__#include <asm/page.h>
(quoted diff) +0 -0
b/arch/powerpc/kernel/prom_init_check.sh
index 3090b97258ae..3155cc722e48 100644
arch/powerpc/kernel/prom_init_check.sh +1 -14
@@ -13,21 +13,8 @@ # If you really need to reference something from prom_init.o add # it to the list below: -has_renamed_memintrinsics() -{ - grep -q "^CONFIG_KASAN=y$" "${KCONFIG_CONFIG}" && \ - ! grep -q "^CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX=y" "${KCONFIG_CONFIG}" -} - -if has_renamed_memintrinsics -then - MEM_FUNCS="__memcpy __memset" -else - MEM_FUNCS="memcpy memset" -fi - WHITELIST="add_reloc_offset __bss_start __bss_stop copy_and_flush -_end enter_prom $MEM_FUNCS reloc_offset __secondary_hold +_end enter_prom memcpy memset reloc_offset __secondary_hold __secondary_hold_acknowledge __secondary_hold_spinloop __start logo_linux_clut224 btext_prepare_BAT reloc_got2 kernstart_addr memstart_addr linux_banner _stext
arch/powerpc/lib/copy_32.S +3 -7
@@ -10,7 +10,6 @@ #include <asm/errno.h> #include <asm/ppc_asm.h> #include <asm/code-patching-asm.h> -#include <asm/kasan.h> #define COPY_16_BYTES \ lwz r7,4(r4); \
@@ -87,7 +86,7 @@ EXPORT_SYMBOL(memset16) * We therefore skip the optimised bloc that uses dcbz. This jump is * replaced by a nop once cache is active. This is done in machine_init() */ -_GLOBAL_KASAN(memset) +_GLOBAL(memset) cmplwi 0,r5,4 blt 7f
@@ -147,7 +146,6 @@ _GLOBAL_KASAN(memset) bdnz 9b blr EXPORT_SYMBOL(memset) -EXPORT_SYMBOL_KASAN(memset) /* * This version uses dcbz on the complete cache lines in the
@@ -160,12 +158,12 @@ EXPORT_SYMBOL_KASAN(memset) * We therefore jump to generic_memcpy which doesn't use dcbz. This jump is * replaced by a nop once cache is active. This is done in machine_init() */ -_GLOBAL_KASAN(memmove) +_GLOBAL(memmove) cmplw 0,r3,r4 bgt backwards_memcpy /* fall through */ -_GLOBAL_KASAN(memcpy) +_GLOBAL(memcpy) 1: b generic_memcpy patch_site 1b, patch__memcpy_nocache
@@ -241,8 +239,6 @@ _GLOBAL_KASAN(memcpy) 65: blr EXPORT_SYMBOL(memcpy) EXPORT_SYMBOL(memmove) -EXPORT_SYMBOL_KASAN(memcpy) -EXPORT_SYMBOL_KASAN(memmove) generic_memcpy: srwi. r7,r5,3
arch/powerpc/lib/mem_64.S +2 -5
@@ -8,7 +8,6 @@ #include <asm/processor.h> #include <asm/errno.h> #include <asm/ppc_asm.h> -#include <asm/kasan.h> #ifndef CONFIG_KASAN _GLOBAL(__memset16)
@@ -29,7 +28,7 @@ EXPORT_SYMBOL(__memset32) EXPORT_SYMBOL(__memset64) #endif -_GLOBAL_KASAN(memset) +_GLOBAL(memset) neg r0,r3 rlwimi r4,r4,8,16,23 andi. r0,r0,7 /* # bytes to be 8-byte aligned */
@@ -95,9 +94,8 @@ _GLOBAL_KASAN(memset) stb r4,0(r6) blr EXPORT_SYMBOL(memset) -EXPORT_SYMBOL_KASAN(memset) -_GLOBAL_TOC_KASAN(memmove) +_GLOBAL_TOC(memmove) cmplw 0,r3,r4 bgt backwards_memcpy b memcpy
@@ -139,4 +137,3 @@ _GLOBAL(backwards_memcpy) mtctr r7 b 1b EXPORT_SYMBOL(memmove) -EXPORT_SYMBOL_KASAN(memmove)
arch/powerpc/lib/memcpy_64.S +1 -3
@@ -7,7 +7,6 @@ #include <asm/ppc_asm.h> #include <asm/asm-compat.h> #include <asm/feature-fixups.h> -#include <asm/kasan.h> #ifndef SELFTEST_CASE /* For big-endian, 0 == most CPUs, 1 == POWER6, 2 == Cell */
@@ -15,7 +14,7 @@ #endif .align 7 -_GLOBAL_TOC_KASAN(memcpy) +_GLOBAL_TOC(memcpy) BEGIN_FTR_SECTION #ifdef __LITTLE_ENDIAN__ cmpdi cr7,r5,0
@@ -227,4 +226,3 @@ END_FTR_SECTION_IFCLR(CPU_FTR_UNALIGNED_LD_STD) blr #endif EXPORT_SYMBOL(memcpy) -EXPORT_SYMBOL_KASAN(memcpy)
Quoted 10 lines · Andrey Ryabinin
"Mukesh Kumar Chaurasiya (IBM)" <[email protected]> writes:
Hi,
I fed this patch to an AI for review, and the review results are included below. Please take a look. From my side, I agree with all of the points raised by AI in the
review, and I think they all need to be addressed. There is also a diff with the suggested changes at the very end of this mail.
Hey Andrey,
Thanks for the effort.
Quoted 63 lines
Quoted 19 lines
powerpc unconditionally selects
GENERIC_ENTRY. TheGENERIC_ENTRYinfrastructure relies on the compiler emitting __asan_mem*() calls at instrumented mem*() sites rather than plain memset/memcpy/memmove, so that entry/exit paths calling those functions are not instrumented.[ ... ]
When
GENERIC_ENTRYis set, both guards suppress the C wrappers for memset/memcpy/memmove and the __underlying_mem*() redirections. This is only safe when the compiler supports the prefixed __asan_mem*() intrinsics. On older toolchains (e.g. GCC 9) that lack this support, plain mem*() calls from instrumented code fall through to the raw assembly implementations inmem_64.S/copy_32.S, completely bypassing the KASAN shadow check.Other arches with
GENERIC_ENTRY(x86, s390, loongarch, riscv) do not hit this because their CI toolchains are always new enough to support the prefix flag.Quoted 6 lines
[ ... ]
The thread in the Closes: link is about an early boot hang, but the commit message only describes mem*() calls bypassing the shadow check. Bypassed checks would lose coverage, not hang the machine. Is the mechanism of the hang understood?
Looking at the state before this commit, the !CC_HAS_KASAN_MEMINTRINSIC_PREFIX macros that
bee25f97ad24("powerpc: EnableGENERIC_ENTRYfeature") added toasm/kasan.hemit two ELFv2 global entry points back to back:#define _GLOBAL_TOC_KASAN(fn) _GLOBAL_TOC(fn); _GLOBAL_TOC(__##fn)With the
_GLOBAL_TOC()definition fromasm/ppc_asm.hthat expands, formemcpy_64.S, to:memcpy:
0: addis r2,r12,(.TOC.-0b)@ha
addi r2,r2,(.TOC.-0b)@l
.localentry memcpy,.-memcpy <- local entry is memcpy+8
__memcpy:
0: addis r2,r12,(.TOC.-0b)@ha <- this is memcpy+8addi r2,r2,(.TOC.-0b)@l .localentry __memcpy,.-__memcpyAssembling exactly that for powerpc64le gives memcpy
st_other0x60(local entry offset 8), and memcpy+8 is the__memcpyTOC prologue.Every same-TOC caller of
memcpy()ormemmove()is resolved by the linker to the local entry, so it lands on that second prologue with r12 holding whatever the caller left there, and returns with r2 pointing at garbage. Same-TOC callers do not reload r2 after the call. WithGENERIC_ENTRY,mm/kasan/shadow.cno longer providesmemcpy(), so on a toolchain without the prefix parameter every instrumented file calls the memcpy symbol directly and hits this. That matches an early hang that only shows up with GCC 9.
Quoted 6 lines
This commit makes the hang go away because the dual-entry macro is deleted, but the commit message attributes the fix to something else. Could the message describe the r2 corruption, and since this repairs a regression from the
GENERIC_ENTRYconversion, should it carry:Fixes bee25f97ad24 ("powerpc: Enable GENERIC_ENTRY feature")
Yeah the commit message needs to clearly specify the corruption of r2, and the Fixes tag should be there.
Quoted 54 lines
On the claim that other
GENERIC_ENTRYarchitectures "do not hit this because their CI toolchains are always new enough": x86, s390, riscv and loongarch with GCC 8 to 12 build exactly the same configuration,CONFIG_KASAN=ywithoutCONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX, and run with mem*() unchecked.scripts/Makefile.kasandocuments that as the intended behaviour:# Instrument memcpy/memset/memmove calls by using instrumented __asan_mem*()
# instead. With compilers that don't support this option, compiler-inserted # memintrinsics won't be checked by KASAN on GENERIC_ENTRY architectures.and
mm/kasan/kasan_test_c.cskips the affected tests with "Test requires checked mem*()". So the situation the message describes is the accepted upstream state for old toolchains, not something specific to powerpc.Quoted 15 lines
arch/powerpc/Kconfig +4
-0@@ -7,6 +7,10 @@ config CC_HAS_ELFV2 config CC_HAS_PREFIXED def_bool PPC64 && $(cc-option, -mcpu=power10 -mprefixed) +config PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX + def_bool (CC_IS_CLANG && $(cc-option,-fsanitize=kernel-address -mllvm -asan-kernel-mem-intrinsic-prefix=1)) || \ + (CC_IS_GCC && $(cc-option,-fsanitize=kernel-address --param asan-kernel-mem-intrinsic-prefix=1)) + config CC_HAS_PCREL # Clang has a bug (https://github.com/llvm/llvm-project/issues/62372) # where pcrel code is not generated if -msoft-float, -mno-altivec, or[ ... ]
arch/powerpc/Kconfig config PPC · line 232
Quoted 13 lines
arch/powerpc/Kconfig +3
-3@@ -220,9 +224,9 @@ config PPC select HAVE_ARCH_HUGE_VMAP if PPC_RADIX_MMU || PPC_8xx select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_JUMP_LABEL_RELATIVE - select HAVE_ARCH_KASAN if PPC32 && PAGE_SHIFT <= 14 - select HAVE_ARCH_KASAN if PPC_RADIX_MMU - select HAVE_ARCH_KASAN if PPC_BOOK3E_64 + select HAVE_ARCH_KASAN if PPC32 && PAGE_SHIFT <= 14 && PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX + select HAVE_ARCH_KASAN if PPC_RADIX_MMU && PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX + select HAVE_ARCH_KASAN if PPC_BOOK3E_64 && PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX select HAVE_ARCH_KASAN_VMALLOC if HAVE_ARCH_KASAN select HAVE_ARCH_KCSAN select HAVE_ARCH_KFENCE if ARCH_SUPPORTS_DEBUG_PAGEALLOCThis drops KASAN from powerpc entirely for GCC 8 through 12, which are inside the supported range in
Documentation/process/changes.rst(GNU C 8.1 minimum). An existing .config withCONFIG_KASAN=ysilently loses it on olddefconfig onceHAVE_ARCH_KASANis no longer selected.
Quoted 9 lines
Given that the actual breakage is the broken memcpy/memmove entry points, is it necessary to go this far? Keeping one
_GLOBAL_TOC()prologue for__memcpyand making memcpy a plain alias of it (a second label plus a matching .localentry, or a global entry that branches to__memcpy) would restore the pre-GENERIC_ENTRY behaviour, with mem*() unchecked on old compilers exactly like the otherGENERIC_ENTRYarchitectures. That also keeps a Fixes-tagged backport candidate from removing a feature on stable kernels.
Ok, yeah this is too aggressive. I will rectify this in next revision.
Quoted 41 lines
Quoted 16 lines
arch/powerpc/include/asm/kasan.h +0
-7@@ -2,20 +2,9 @@#ifndef __ASM_KASAN_H#define __ASM_KASAN_H -#if defined(CONFIG_KASAN ) && !defined(CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX ) -#define _GLOBAL_KASAN (fn) \ - _GLOBAL(fn); \ - _GLOBAL(__##fn) -#define _GLOBAL_TOC_KASAN (fn) \ - _GLOBAL_TOC(fn); \ - _GLOBAL_TOC(__##fn)arch/powerpc/include/asm/kasan.h line 10
Quoted 10 lines
arch/powerpc/include/asm/kasan.h +0
-4-#define EXPORT_SYMBOL_KASAN (fn) \ - EXPORT_SYMBOL(__##fn) -#else /* CONFIG_KASAN && !CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX */ #define _GLOBAL_KASAN (fn) _GLOBAL(fn)#define _GLOBAL_TOC_KASAN (fn) _GLOBAL_TOC(fn)#define EXPORT_SYMBOL_KASAN (fn) -#endif /* CONFIG_KASAN && !CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX */ #ifndef __ASSEMBLER__this isn't a bug, but after this change
_GLOBAL_KASAN(),_GLOBAL_TOC_KASAN()andEXPORT_SYMBOL_KASAN()are unconditional identity macros with a single empty one. Should the five users inmem_64.S,memcpy_64.Sandcopy_32.Sswitch to _GLOBAL()/_GLOBAL_TOC() and the macros go away?Related leftover:
arch/powerpc/kernel/prom_init_check.shstill has
has_renamed_memintrinsics()
{
grep -q "^CONFIG_KASAN=y$" "${KCONFIG_CONFIG}" && \
! grep -q
"^CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX=y" "${KCONFIG_CONFIG}"
}
Quoted 7 lines
if
has_renamed_memintrinsics
thenMEM_FUNCS="__memcpy __memset"which can no longer be true on powerpc after this commit. Should that branch be removed in the same cleanup?
Yes it should be. It's a dead code now.
Quoted 208 lines
arch/powerpc/include/asm/string.h extern void * memchr(const void *,int,__kernel_size_t); · line 33
Quoted 12 lines
arch/powerpc/include/asm/string.h +1
-2@@ -29,29 +29,10 @@ extern void * memchr(const void *,int,__kernel_size_t);void memcpy_flushcache (void *dest,const void *src,size_t size);#ifdef CONFIG_KASAN -/* __mem variants are used by KASAN to implement instrumented meminstrinsics. */ -#ifdef CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX +/* Used by mm/kasan/shadow.c as raw backends to bypass KASAN checking. */ #define __memset memsetthis isn't a bug, but the new comment names only
mm/kasan/shadow.c. The same __memset()/__memcpy() names are used bymm/kasan/generic.cas well (DEFINE_ASAN_SET_SHADOW()andrelease_alloc_meta()), so would "used by mm/kasan as raw backends" be more accurate?---
arch/powerpc/Kconfig +0
-6@@ -7,10 +7,6 @@ config CC_HAS_ELFV2 config CC_HAS_PREFIXED def_bool PPC64 && $(cc-option, -mcpu=power10 -mprefixed) -config PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIX - def_bool (CC_IS_CLANG && $(cc-option,-fsanitize=kernel-address -mllvm -asan-kernel-mem-intrinsic-prefix=1)) || \ - (CC_IS_GCC && $(cc-option,-fsanitize=kernel-address --param asan-kernel-mem-intrinsic-prefix=1)) - config CC_HAS_PCREL # Clang has a bug (https://github.com/llvm/llvm-project/issues/62372) # where pcrel code is not generated if -msoft-float, -mno-altivec, or@@ -224,9 +220,9 @@ config PPC select HAVE_ARCH_HUGE_VMAP if PPC_RADIX_MMU || PPC_8xx select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_JUMP_LABEL_RELATIVE - select HAVE_ARCH_KASAN if PPC32 && PAGE_SHIFT <= 14 &&
PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIXarch/powerpc/Kconfig +0
-1- select HAVE_ARCH_KASAN if PPC_RADIX_MMU &&
PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIXarch/powerpc/Kconfig +0
-1- select HAVE_ARCH_KASAN if PPC_BOOK3E_64 &&
PPC_CC_HAS_KASAN_MEMINTRINSIC_PREFIXarch/powerpc/Kconfig +3
-0+ select HAVE_ARCH_KASAN if PPC32 && PAGE_SHIFT <= 14 + select HAVE_ARCH_KASAN if PPC_RADIX_MMU + select HAVE_ARCH_KASAN if PPC_BOOK3E_64 select HAVE_ARCH_KASAN_VMALLOC if HAVE_ARCH_KASAN select HAVE_ARCH_KCSAN select HAVE_ARCH_KFENCE if ARCH_SUPPORTS_DEBUG_PAGEALLOCarch/powerpc/include/asm/kasan.h +0
-4@@ -2,10 +2,6 @@#ifndef __ASM_KASAN_H#define __ASM_KASAN_H -#define _GLOBAL_KASAN (fn) _GLOBAL(fn) -#define _GLOBAL_TOC_KASAN (fn) _GLOBAL_TOC(fn) -#define EXPORT_SYMBOL_KASAN (fn) -#ifndef __ASSEMBLER__#include <asm/page.h> arch/powerpc/kernel/prom_init_check.sh +1
-14@@ -13,21 +13,8 @@ # If you really need to reference something from prom_init.o add # it to the list below: -has_renamed_memintrinsics() -{ - grep -q "^CONFIG_KASAN=y$" "${KCONFIG_CONFIG}" && \ - ! grep -q "^CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX=y" "${KCONFIG_CONFIG}" -} - -if has_renamed_memintrinsics -then - MEM_FUNCS="__memcpy __memset" -else - MEM_FUNCS="memcpy memset" -fi - WHITELIST="add_reloc_offset __bss_start __bss_stop copy_and_flush -_end enter_prom $MEM_FUNCS reloc_offset __secondary_hold +_end enter_prom memcpy memset reloc_offset __secondary_hold __secondary_hold_acknowledge __secondary_hold_spinloop __start logo_linux_clut224 btext_prepare_BAT reloc_got2 kernstart_addr memstart_addr linux_banner _stextarch/powerpc/lib/copy_32.S +3
-7@@ -10,7 +10,6 @@ #include <asm/errno.h> #include <asm/ppc_asm.h> #include <asm/code-patching-asm.h> -#include <asm/kasan.h> #define COPY_16_BYTES \ lwz r7,4(r4); \@@ -87,7 +86,7 @@ EXPORT_SYMBOL(memset16) * We therefore skip the optimised bloc that uses dcbz. This jump is * replaced by a nop once cache is active. This is done in machine_init() */ -_GLOBAL_KASAN(memset) +_GLOBAL(memset) cmplwi 0,r5,4 blt 7f@@ -147,7 +146,6 @@ _GLOBAL_KASAN(memset) bdnz 9b blr EXPORT_SYMBOL(memset) -EXPORT_SYMBOL_KASAN(memset) /* * This version uses dcbz on the complete cache lines in the@@ -160,12 +158,12 @@ EXPORT_SYMBOL_KASAN(memset) * We therefore jump to generic_memcpy which doesn't use dcbz. This jump is * replaced by a nop once cache is active. This is done in machine_init() */ -_GLOBAL_KASAN(memmove) +_GLOBAL(memmove) cmplw 0,r3,r4 bgt backwards_memcpy /* fall through */ -_GLOBAL_KASAN(memcpy) +_GLOBAL(memcpy) 1: b generic_memcpy patch_site 1b, patch__memcpy_nocache@@ -241,8 +239,6 @@ _GLOBAL_KASAN(memcpy) 65: blr EXPORT_SYMBOL(memcpy) EXPORT_SYMBOL(memmove) -EXPORT_SYMBOL_KASAN(memcpy) -EXPORT_SYMBOL_KASAN(memmove) generic_memcpy: srwi. r7,r5,3arch/powerpc/lib/mem_64.S +2
-5@@ -8,7 +8,6 @@ #include <asm/processor.h> #include <asm/errno.h> #include <asm/ppc_asm.h> -#include <asm/kasan.h> #ifndef CONFIG_KASAN _GLOBAL(__memset16)@@ -29,7 +28,7 @@ EXPORT_SYMBOL(__memset32) EXPORT_SYMBOL(__memset64) #endif -_GLOBAL_KASAN(memset) +_GLOBAL(memset) neg r0,r3 rlwimi r4,r4,8,16,23 andi. r0,r0,7 /* # bytes to be 8-byte aligned */@@ -95,9 +94,8 @@ _GLOBAL_KASAN(memset) stb r4,0(r6) blr EXPORT_SYMBOL(memset) -EXPORT_SYMBOL_KASAN(memset) -_GLOBAL_TOC_KASAN(memmove) +_GLOBAL_TOC(memmove) cmplw 0,r3,r4 bgt backwards_memcpy b memcpy@@ -139,4 +137,3 @@ _GLOBAL(backwards_memcpy) mtctr r7 b 1b EXPORT_SYMBOL(memmove) -EXPORT_SYMBOL_KASAN(memmove)arch/powerpc/lib/memcpy_64.S +1
-2@@ -7,7 +7,6 @@ #include <asm/ppc_asm.h> #include <asm/asm-compat.h> #include <asm/feature-fixups.h> -#include <asm/kasan.h> #ifndef SELFTEST_CASE /* For big-endian, 0 == most CPUs, 1 == POWER6, 2 == Cell */@@ -15,7 +14,7 @@ #endif .align 7 -_GLOBAL_TOC_KASAN(memcpy) +_GLOBAL_TOC(memcpy) BEGIN_FTR_SECTION #ifdef __LITTLE_ENDIAN__ cmpdi cr7,r5,0
arch/powerpc/lib/memcpy_64.S END_FTR_SECTION_IFCLR(CPU_FTR_UNALIGNED_LD_STD) · line 230
Quoted 4 lines
arch/powerpc/lib/memcpy_64.S +0
-1@@ -227,4 +226,3 @@ END_FTR_SECTION_IFCLR(CPU_FTR_UNALIGNED_LD_STD) blr #endif EXPORT_SYMBOL(memcpy) -EXPORT_SYMBOL_KASAN(memcpy)
Yeah this seems like it could work. Letme try this.
Thanks,
Mukesh