kasan: unify kasan_arch_is_ready with kasan_enabled
Subsystems (13)
v2 cover kasan: unify kasan_arch_is_ready with kasan_enabled
This patch series unifies the kasan_arch_is_ready() and kasan_enabled() interfaces by extending the existing kasan_enabled() infrastructure to work consistently across all KASAN modes (Generic, SW_TAGS, HW_TAGS).
Currently, kasan_enabled() only works for HW_TAGS mode using a static key, while other modes either return IS_ENABLED(CONFIG_KASAN) (compile-time constant) or rely on architecture-specific kasan_arch_is_ready() implementations with custom static keys and global variables.
This leads to:
- Code duplication across architectures
- Inconsistent runtime behavior between KASAN modes
- Architecture-specific readiness tracking
After this series:
- All KASAN modes use the same kasan_flag_enabled static key
- Consistent runtime enable/disable behavior across modes
- Simplified architecture code with unified kasan_init_generic() calls
- Elimination of arch specific kasan_arch_is_ready() implementations
- Unified vmalloc integration using kasan_enabled() checks
This addresses the bugzilla issue [1] about making kasan_flag_enabled and kasan_enabled() work for Generic mode, and extends it to provide true unification across all modes.
[1] https://bugzilla.kernel.org/show_bug.cgi?id=217049
=== Current mainline KUnit status
To see if there is any regression, I've tested first on the following commit 739a6c93cc75 ("Merge tag 'nfsd-6.16-1' ofgit://git.kernel.org/pub/scm/linux/kernel/git/cel/linux").
Tested via compiling a kernel with CONFIG_KASAN_KUNIT_TEST and running QEMU VM. There are failing tests in SW_TAGS and GENERIC modes in arm64:
arm64 CONFIG_KASAN_HW_TAGS:
# kasan: pass:62 fail:0 skip:13 total:75 # Totals: pass:62 fail:0 skip:13 total:75 ok 1 kasan
arm64 CONFIG_KASAN_SW_TAGS=y:
# kasan: pass:65 fail:1 skip:9 total:75
# Totals: pass:65 fail:1 skip:9 total:75
not ok 1 kasan
# kasan_strings: EXPECTATION FAILED at mm/kasan/kasan_test_c.c:1598
KASAN failure expected in "strscpy(ptr, src + KASAN_GRANULE_SIZE, KASAN_GRANULE_SIZE)", but none occurred
arm64 CONFIG_KASAN_GENERIC=y, CONFIG_KASAN_OUTLINE=y:
# kasan: pass:61 fail:1 skip:13 total:75 # Totals: pass:61 fail:1 skip:13 total:75 not ok 1 kasan # same failure as above
x86_64 CONFIG_KASAN_GENERIC=y:
# kasan: pass:58 fail:0 skip:17 total:75 # Totals: pass:58 fail:0 skip:17 total:75 ok 1 kasan
=== Testing with patches
Testing in v2:
- Compiled every affected arch with no errors:
$ make CC=clang LD=ld.lld AR=llvm-ar NM=llvm-nm STRIP=llvm-strip \ OBJCOPY=llvm-objcopy OBJDUMP=llvm-objdump READELF=llvm-readelf \ HOSTCC=clang HOSTCXX=clang++ HOSTAR=llvm-ar HOSTLD=ld.lld \ ARCH=$ARCH
$ clang --version ClangBuiltLinux clang version 19.1.4 Target: x86_64-unknown-linux-gnu Thread model: posix
- make ARCH=um produces the warning during compiling:
MODPOST Module.symvers WARNING: modpost: vmlinux: section mismatch in reference: \ kasan_init+0x43 (section: .ltext) -> \ kasan_init_generic (section: .init.text)
AFAIU, it's due to the code in arch/um/kernel/mem.c, where kasan_init() is placed in own section ".kasan_init", which calls kasan_init_generic() which is marked with "__init".
- Booting via qemu-system- and running KUnit tests:
* arm64 (GENERIC, HW_TAGS, SW_TAGS): no regression, same above results.
* x86_64 (GENERIC): no regression, no errors
=== NB
I haven't tested the kernel boot on the following arch. due to the absence of qemu-system- support on those arch on my machine, so I defer this to relevant arch people to test KASAN initialization:
- loongarch
- s390
- um
- xtensa
- powerpc
- riscv
Code changes in v2:
- Replace the order of patches. Move "kasan: replace kasan_arch_is_ready with kasan_enabled" at the end to keep the compatibility.
- arch/arm, arch/riscv: add 2 arch. missed in v1
- arch/powerpc: add kasan_init_generic() in other kasan_init() calls:
arch/powerpc/mm/kasan/init_32.c arch/powerpc/mm/kasan/init_book3e_64.c
- arch/um: add the proper header #include <linux/kasan.h>. Tested via compiling with no errors. In the v1 arch/um changes were acked-by Johannes Berg, though I don't include it due to the changed code in v2.
- arch/powerpc: add back #ifdef CONFIG_KASAN deleted in v1 and tested the compilation.
- arch/loongarch: update git commit message about non-standard flow of calling kasan_init_generic()
Sabyrzhan Tasbolatov (11):
kasan: unify static kasan_flag_enabled across modes kasan/arm64: call kasan_init_generic in kasan_init kasan/arm: call kasan_init_generic in kasan_init kasan/xtensa: call kasan_init_generic in kasan_init kasan/loongarch: call kasan_init_generic in kasan_init kasan/um: call kasan_init_generic in kasan_init kasan/x86: call kasan_init_generic in kasan_init kasan/s390: call kasan_init_generic in kasan_init kasan/powerpc: call kasan_init_generic in kasan_init kasan/riscv: call kasan_init_generic in kasan_init
kasan: replace kasan_arch_is_ready with kasan_enabled
arch/arm/mm/kasan_init.c | 2 +- arch/arm64/mm/kasan_init.c | 4 +--- arch/loongarch/include/asm/kasan.h | 7 ------- arch/loongarch/mm/kasan_init.c | 7 ++----- arch/powerpc/include/asm/kasan.h | 13 ------------- arch/powerpc/mm/kasan/init_32.c | 2 +- arch/powerpc/mm/kasan/init_book3e_64.c | 2 +- arch/powerpc/mm/kasan/init_book3s_64.c | 6 +----- arch/riscv/mm/kasan_init.c | 1 + arch/s390/kernel/early.c | 3 ++- arch/um/include/asm/kasan.h | 5 ----- arch/um/kernel/mem.c | 4 ++-- arch/x86/mm/kasan_init_64.c | 2 +- arch/xtensa/mm/kasan_init.c | 2 +- include/linux/kasan-enabled.h | 22 ++++++++++++++++------ include/linux/kasan.h | 6 ++++++ mm/kasan/common.c | 15 +++++++++++---- mm/kasan/generic.c | 17 ++++++++++++++--- mm/kasan/hw_tags.c | 7 ------- mm/kasan/kasan.h | 6 ------ mm/kasan/shadow.c | 15 +++------------ mm/kasan/sw_tags.c | 2 ++ 22 files changed, 66 insertions(+), 84 deletions(-)
Signature · 1 line
2.34.1
PATCH v2 1/11 kasan: unify static kasan_flag_enabled across modes
Historically, the runtime static key kasan_flag_enabled existed only for CONFIG_KASAN_HW_TAGS mode. Generic and SW_TAGS modes either relied on architecture-specific kasan_arch_is_ready() implementations or evaluated KASAN checks unconditionally, leading to code duplication.
This patch unifies the approach by:
1. Moving kasan_flag_enabled declaration under CONFIG_KASAN (all modes) instead of only CONFIG_KASAN_HW_TAGS
2. Moving the static key definition to common.c for shared usage
3. Adding kasan_init_generic() function that enables the static key and handles initialization for Generic mode
4. Updating SW_TAGS mode to enable the unified static key
5. Removing the duplicate static key definition from HW_TAGS
After this change, all KASAN modes use the same underlying static key infrastructure. The kasan_enabled() function now provides consistent runtime enable behavior across Generic, SW_TAGS, and HW_TAGS modes.
This maintains a backward compatibility - existing architecture code continues to work unchanged, but now benefits from the unified runtime control mechanism. The architecture-specific kasan_arch_is_ready() implementations can be gradually replaced with calls to the new kasan_init_generic() function.
--- include/linux/kasan-enabled.h | 10 ++++++++-- include/linux/kasan.h | 6 ++++++ mm/kasan/common.c | 7 +++++++ mm/kasan/generic.c | 11 +++++++++++ mm/kasan/hw_tags.c | 7 ------- mm/kasan/sw_tags.c | 2 ++ 6 files changed, 34 insertions(+), 9 deletions(-)
include/linux/kasan-enabled.h +8 -2
@@ -4,9 +4,15 @@#include <linux/static_key.h> -#ifdef CONFIG_KASAN_HW_TAGS - +#ifdef CONFIG_KASAN +/* +* Global runtime flag. Starts ‘false’; switched to ‘true’ by +* the appropriate kasan_init_*() once KASAN is fully initialized. +*/ DECLARE_STATIC_KEY_FALSE (kasan_flag_enabled); +#endif + +#ifdef CONFIG_KASAN_HW_TAGS static __always_inline bool kasan_enabled(void) {
include/linux/kasan.h +6 -0
@@ -543,6 +543,12 @@ void kasan_report_async(void);#endif /* CONFIG_KASAN_HW_TAGS */ +#ifdef CONFIG_KASAN_GENERIC +void __init kasan_init_generic (void ); +#else +static inline void kasan_init_generic (void ) { } +#endif +#ifdef CONFIG_KASAN_SW_TAGS void __init kasan_init_sw_tags (void );#else
mm/kasan/common.c +7 -0
@@ -32,6 +32,13 @@#include "kasan.h" #include "../slab.h" +/* +* Definition of the unified static key declared in kasan-enabled.h. +* This provides consistent runtime enable/disable across all KASAN modes. +*/ +DEFINE_STATIC_KEY_FALSE (kasan_flag_enabled); +EXPORT_SYMBOL (kasan_flag_enabled); +struct slab *kasan_addr_to_slab (const void *addr) {if (virt_addr_valid (addr))
mm/kasan/generic.c +11 -0
@@ -36,6 +36,17 @@#include "kasan.h" #include "../slab.h" +/* +* Initialize Generic KASAN and enable runtime checks. +* This should be called from arch kasan_init() once shadow memory is ready. +*/ +void __init kasan_init_generic (void ) +{ +static_branch_enable (&kasan_flag_enabled); + +pr_info ("KernelAddressSanitizer initialized (generic)\n" ); +} +/* * All functions below always inlined so compiler could * perform better optimizations in each of __asan_loadX/__assn_storeX
mm/kasan/hw_tags.c +0 -7
@@ -45,13 +45,6 @@ static enum kasan_arg kasan_arg __ro_after_init;static enum kasan_arg_mode kasan_arg_mode__ro_after_init ;static enum kasan_arg_vmalloc kasan_arg_vmalloc__initdata ; -/* -* Whether KASAN is enabled at all. -* The value remains false until KASAN is initialized by kasan_init_hw_tags(). -*/ -DEFINE_STATIC_KEY_FALSE (kasan_flag_enabled); -EXPORT_SYMBOL (kasan_flag_enabled); -/* * Whether the selected mode is synchronous, asynchronous, or asymmetric. * Defaults to KASAN_MODE_SYNC.
mm/kasan/sw_tags.c +2 -0
@@ -45,6 +45,8 @@ void __init kasan_init_sw_tags(void)kasan_init_tags (); +static_branch_enable (&kasan_flag_enabled); +pr_info ("KernelAddressSanitizer initialized (sw-tags, stacktrace=%s)\n" ,str_on_off (kasan_stack_collection_enabled ())); }
Signature · 1 line
2.34.1
Alexander Gordeev
maintainerRe: [PATCH v2 01/11] kasan: unify static kasan_flag_enabled across modes
On Thu, Jun 26, 2025 at 08:31:37PM +0500, Sabyrzhan Tasbolatov wrote:
Hi Sabyrzhan,
Quoted 5 lines
mm/kasan/generic.c +0
-0
mm/kasan/generic.c line 45
Quoted 10 lines
mm/kasan/generic.c +7
-0@@ -36,6 +36,17 @@#include "kasan.h" #include "../slab.h" +/* +* Initialize Generic KASAN and enable runtime checks. +* This should be called from arch kasan_init() once shadow memory is ready. +*/ +void __init kasan_init_generic (void ) +{ +static_branch_enable (&kasan_flag_enabled);
s390 crashes at this line, when the whole series is applied.
FWIW, it looks like kasan is called while its state is not yet finalized. E.g. whether calling __asan_report_store4_noabort() before kasan_init_generic() is expected?
32e0a54: c0 e5 fe a9 70 56 brasl %r14,80eb00 <__asan_report_store4_noabort>
32e0a5a: c4 28 ff cb bb a3 lgrl %r2,2c581a0 <_GLOBAL_OFFSET_TABLE_+0x70c0>
sort_extable(__start_amode31_ex_table, __stop_amode31_ex_table);
32e0a60: a5 ac 00 1c llihh %r10,28
init_task.kasan_depth = 0;
32e0a64: e3 40 2b c8 01 71 lay %r4,7112(%r2)
32e0a6a: e5 4c 40 00 00 00 mvhi 0(%r4),0
kasan_init_generic(); 32e0a70: c0 e5 00 01 e7 3c brasl %r14,331d8e8 <kasan_init_generic>
mm/kasan/generic.c line 48
Quoted 2 lines
mm/kasan/generic.c +2
-0+pr_info ("KernelAddressSanitizer initialized (generic)\n" ); +}
Thanks!
Heiko Carstens
maintainerRe: [PATCH v2 01/11] kasan: unify static kasan_flag_enabled across modes
Quoted 22 lines · Alexander Gordeev
On Thu, Jun 26, 2025 at 08:31:37PM +0500, Sabyrzhan Tasbolatov wrote:
Hi Sabyrzhan,
Quoted 5 lines
mm/kasan/generic.c +0
-0mm/kasan/generic.c line 45
Quoted 10 lines
mm/kasan/generic.c +7
-0@@ -36,6 +36,17 @@#include "kasan.h" #include "../slab.h" +/* +* Initialize Generic KASAN and enable runtime checks. +* This should be called from arch kasan_init() once shadow memory is ready. +*/ +void __init kasan_init_generic (void ) +{ +static_branch_enable (&kasan_flag_enabled);s390 crashes at this line, when the whole series is applied.
Quoted 3 lines
FWIW, it looks like kasan is called while its state is not yet finalized. E.g. whether calling
__asan_report_store4_noabort()beforekasan_init_generic()is expected?
It crashes because with this conversion a call to static_branch_enable() is introduced. This one get's called way before jump_label_init() init has been called. Therefore the STATIC_KEY_CHECK_USE() in static_key_enable_cpuslocked() triggers.
This again tries to emit a warning. Due to lack of console support that early the kernel crashes.
One possible solution would be to move the kasan init function to arch/s390/kernel/setup.c, after jump_label_init() has been called. If we want this, is a different question.
It seems to work, so I see no reason for not doing that.
Vasily, since you did nearly all of the KASAN work for s390, do you have any opinion about this?
Alexander Gordeev
maintainerRe: [PATCH v2 01/11] kasan: unify static kasan_flag_enabled across modes
Quoted 19 lines · Heiko Carstens
Quoted 10 lines
mm/kasan/generic.c line 45
Quoted 7 lines
mm/kasan/generic.c +7
-0+/* +* Initialize Generic KASAN and enable runtime checks. +* This should be called from arch kasan_init() once shadow memory is ready. +*/ +void __init kasan_init_generic (void ) +{ +static_branch_enable (&kasan_flag_enabled);s390 crashes at this line, when the whole series is applied.
Quoted 3 lines
FWIW, it looks like kasan is called while its state is not yet finalized. E.g. whether calling
__asan_report_store4_noabort()beforekasan_init_generic()is expected?It crashes because with this conversion a call to
static_branch_enable()is introduced. This one get's called way beforejump_label_init()init has been called. Therefore theSTATIC_KEY_CHECK_USE()instatic_key_enable_cpuslocked()triggers.
Quoted 8 lines
This again tries to emit a warning. Due to lack of console support that early the kernel crashes.
One possible solution would be to move the kasan init function to
arch/s390/kernel/setup.c, afterjump_label_init()has been called. If we want this, is a different question.It seems to work, so I see no reason for not doing that.
IIRC, we wanted to have kasan coverage as early as possible. Delaying it past jump_label_init() leaves out pretty big chunk of code?
Quoted 2 lines
Vasily, since you did nearly all of the KASAN work for s390, do you have any opinion about this?
Andrew Morton
maintainerRe: [PATCH v2 01/11] kasan: unify static kasan_flag_enabled across modes
In reply to Alexander GordeevQuoted 9 lines · On Mon, 30 Jun 2025 14:31:00 +0200 Alexander Gordeev <[email protected]>…
mm/kasan/generic.c line 45
Quoted 7 lines
mm/kasan/generic.c +7
-0+/* +* Initialize Generic KASAN and enable runtime checks. +* This should be called from arch kasan_init() once shadow memory is ready. +*/ +void __init kasan_init_generic (void ) +{ +static_branch_enable (&kasan_flag_enabled);s390 crashes at this line, when the whole series is applied.
oop. Thanks, I'll demote this seres to the mm-new branch for now, which takes it out of linux-next.
PATCH v2 2/11 kasan/arm64: call kasan_init_generic in kasan_init
In reply to Sabyrzhan TasbolatovCall kasan_init_generic() which enables the static flag to mark KASAN initialized in CONFIG_KASAN_GENERIC mode, otherwise it's an inline stub, and the flag is enabled in kasan_init_sw_tags() or kasan_init_hw_tags().
--- arch/arm64/mm/kasan_init.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
arch/arm64/mm/kasan_init.c +1 -3
@@ -399,14 +399,12 @@ void __init kasan_init(void) {kasan_init_shadow ();kasan_init_depth (); -#if defined(CONFIG_KASAN_GENERIC ) +kasan_init_generic ();/* * Generic KASAN is now fully initialized. * Software and Hardware Tag-Based modes still require * kasan_init_sw_tags() and kasan_init_hw_tags() correspondingly. */ -pr_info ("KernelAddressSanitizer initialized (generic)\n" ); -#endif }#endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
Signature · 1 line
2.34.1
PATCH v2 3/11 kasan/arm: call kasan_init_generic in kasan_init
In reply to Sabyrzhan TasbolatovCall kasan_init_generic() which enables the static flag to mark KASAN initialized in CONFIG_KASAN_GENERIC mode, otherwise it's an inline stub.
--- arch/arm/mm/kasan_init.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/mm/kasan_init.c +1 -1
@@ -300,6 +300,6 @@ void __init kasan_init(void)local_flush_tlb_all ();memset (kasan_early_shadow_page,0 ,PAGE_SIZE ); -pr_info ("Kernel address sanitizer initialized\n" ); init_task.kasan_depth =0 ; +kasan_init_generic (); }
Signature · 1 line
2.34.1
PATCH v2 4/11 kasan/xtensa: call kasan_init_generic in kasan_init
In reply to Sabyrzhan TasbolatovCall kasan_init_generic() which enables the static flag to mark generic KASAN initialized, otherwise it's an inline stub.
Note that arch/xtensa still uses "current" instead of "init_task" pointer in current->kasan_depth = 0; to enable error messages. I haven't changed this because I can't test the change.
--- arch/xtensa/mm/kasan_init.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/xtensa/mm/kasan_init.c +1 -1
@@ -94,5 +94,5 @@ void __init kasan_init(void)/* At this point kasan is fully initialized. Enable error messages. */ current->kasan_depth =0 ; -pr_info ("KernelAddressSanitizer initialized\n" ); +kasan_init_generic (); }
Signature · 1 line
2.34.1
PATCH v2 5/11 kasan/loongarch: call kasan_init_generic in kasan_init
In reply to Sabyrzhan TasbolatovCall kasan_init_generic() which enables the static flag to mark generic KASAN initialized, otherwise it's an inline stub.
Replace kasan_arch_is_ready with kasan_enabled.
Delete the flag kasan_early_stage in favor of the global static key enabled via kasan_enabled().
printk banner is printed earlier right where kasan_early_stage was flipped, just to keep the same flow.
Note that init_task.kasan_depth = 0; is called after kasan_init_generic(), which is different than in other arch kasan_init(). I've left this unchanged as I can't test it. Defer to loongarch maintainers.
--- arch/loongarch/include/asm/kasan.h | 7 ------- arch/loongarch/mm/kasan_init.c | 7 ++----- 2 files changed, 2 insertions(+), 12 deletions(-)
arch/loongarch/include/asm/kasan.h +0 -7
@@ -66,7 +66,6 @@#define XKPRANGE_WC_SHADOW_OFFSET (KASAN_SHADOW_START + XKPRANGE_WC_KASAN_OFFSET)#define XKVRANGE_VC_SHADOW_OFFSET (KASAN_SHADOW_START + XKVRANGE_VC_KASAN_OFFSET) -extern bool kasan_early_stage;extern unsigned char kasan_early_shadow_page[PAGE_SIZE ];#define kasan_mem_to_shadow kasan_mem_to_shadow
@@ -75,12 +74,6 @@ void *kasan_mem_to_shadow(const void *addr);#define kasan_shadow_to_mem kasan_shadow_to_memconst void *kasan_shadow_to_mem (const void *shadow_addr); -#define kasan_arch_is_ready kasan_arch_is_ready -static __always_inline bool kasan_arch_is_ready(void) -{ -return !kasan_early_stage; -} -#define addr_has_metadata addr_has_metadata static __always_inline bool addr_has_metadata(const void *addr) {
arch/loongarch/mm/kasan_init.c +2 -5
@@ -40,11 +40,9 @@ static pgd_t kasan_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);#define __pte_none (early, pte) (early ? pte_none(pte) : \ ((pte_val(pte) & _PFN_MASK) == (unsigned long)__pa(kasan_early_shadow_page))) -bool kasan_early_stage = true; -void *kasan_mem_to_shadow (const void *addr) { -if (!kasan_arch_is_ready ()) { +if (!kasan_enabled ()) {return (void *)(kasan_early_shadow_page); }else {unsigned long maddr = (unsigned long )addr;
@@ -298,7 +296,7 @@ void __init kasan_init(void)kasan_populate_early_shadow (kasan_mem_to_shadow ((void *)VMALLOC_START ),kasan_mem_to_shadow ((void *)KFENCE_AREA_END )); - kasan_early_stage = false; +kasan_init_generic ();/* Populate the linear mapping */ for_each_mem_range (i, &pa_start, &pa_end) {
@@ -329,5 +327,4 @@ void __init kasan_init(void)/* At this point kasan is fully initialized. Enable error messages */ init_task.kasan_depth =0 ; -pr_info ("KernelAddressSanitizer initialized.\n" ); }
Signature · 1 line
2.34.1
PATCH v2 6/11 kasan/um: call kasan_init_generic in kasan_init
In reply to Sabyrzhan TasbolatovCall kasan_init_generic() which enables the static flag to mark generic KASAN initialized, otherwise it's an inline stub.
Delete the key kasan_um_is_ready in favor of the global static flag in linux/kasan-enabled.h which is enabled with kasan_init_generic().
Note that "kasan_init_generic" has __init macro, which is called by kasan_init() which is not marked with __init in arch/um code.
---
Changes in v2:
- add the proper header #include <linux/kasan.h>
--- arch/um/include/asm/kasan.h | 5 ----- arch/um/kernel/mem.c | 4 ++-- 2 files changed, 2 insertions(+), 7 deletions(-)
arch/um/include/asm/kasan.h +0 -5
@@ -24,11 +24,6 @@#ifdef CONFIG_KASAN void kasan_init (void ); -extern int kasan_um_is_ready; - -#ifdef CONFIG_STATIC_LINK -#define kasan_arch_is_ready () (kasan_um_is_ready) -#endif #else static inline void kasan_init (void ) { }#endif /* CONFIG_KASAN */
arch/um/kernel/mem.c +2 -2
@@ -21,9 +21,9 @@#include <os.h> #include <um_malloc.h> #include <linux/sched/task.h> +#include <linux/kasan.h> #ifdef CONFIG_KASAN -int kasan_um_is_ready;void kasan_init (void ) {/*
@@ -32,7 +32,7 @@ void kasan_init(void)*/ kasan_map_memory ((void *)KASAN_SHADOW_START ,KASAN_SHADOW_SIZE ); init_task.kasan_depth =0 ; - kasan_um_is_ready = true; +kasan_init_generic (); }static void (*kasan_init_ptr)(void )
Signature · 1 line
2.34.1
PATCH v2 7/11 kasan/x86: call kasan_init_generic in kasan_init
In reply to Sabyrzhan TasbolatovCall kasan_init_generic() which enables the static flag to mark generic KASAN initialized, otherwise it's an inline stub. Also prints the banner from the single place.
--- arch/x86/mm/kasan_init_64.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/mm/kasan_init_64.c +1 -1
@@ -451,5 +451,5 @@ void __init kasan_init(void)__flush_tlb_all (); init_task.kasan_depth =0 ; -pr_info ("KernelAddressSanitizer initialized\n" ); +kasan_init_generic (); }
Signature · 1 line
2.34.1
PATCH v2 8/11 kasan/s390: call kasan_init_generic in kasan_init
In reply to Sabyrzhan TasbolatovCall kasan_init_generic() which enables the static flag to mark generic KASAN initialized, otherwise it's an inline stub. Also prints the banner from the single place.
--- arch/s390/kernel/early.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/s390/kernel/early.c +2 -1
@@ -21,6 +21,7 @@#include <linux/kernel.h> #include <asm/asm-extable.h> #include <linux/memblock.h> +#include <linux/kasan.h> #include <asm/access-regs.h> #include <asm/asm-offsets.h> #include <asm/machine.h>
@@ -65,7 +66,7 @@ static void __init kasan_early_init(void) {#ifdef CONFIG_KASAN init_task.kasan_depth =0 ; -pr_info ("KernelAddressSanitizer initialized\n" ); +kasan_init_generic ();#endif }
Signature · 1 line
2.34.1
PATCH v2 9/11 kasan/powerpc: call kasan_init_generic in kasan_init
In reply to Sabyrzhan TasbolatovCall kasan_init_generic() which enables the static flag to mark generic KASAN initialized, otherwise it's an inline stub. Also prints the banner from the single place.
---
Changes in v2:
- Add kasan_init_generic() in other kasan_init() calls:
arch/powerpc/mm/kasan/init_32.c arch/powerpc/mm/kasan/init_book3e_64.c - Add back `#ifdef CONFIG_KASAN` deleted in v1 --- arch/powerpc/include/asm/kasan.h | 13 ------------- arch/powerpc/mm/kasan/init_32.c | 2 +- arch/powerpc/mm/kasan/init_book3e_64.c | 2 +- arch/powerpc/mm/kasan/init_book3s_64.c | 6 +----- 4 files changed, 3 insertions(+), 20 deletions(-)
arch/powerpc/include/asm/kasan.h +0 -13
@@ -53,19 +53,6 @@#endif #ifdef CONFIG_KASAN -#ifdef CONFIG_PPC_BOOK3S_64 -DECLARE_STATIC_KEY_FALSE (powerpc_kasan_enabled_key); - -static __always_inline bool kasan_arch_is_ready(void) -{ -if (static_branch_likely (&powerpc_kasan_enabled_key)) -return true; -return false; -} - -#define kasan_arch_is_ready kasan_arch_is_ready -#endif -void kasan_early_init (void );void kasan_mmu_init (void );void kasan_init (void );
arch/powerpc/mm/kasan/init_32.c +1 -1
@@ -165,7 +165,7 @@ void __init kasan_init(void)/* At this point kasan is fully initialized. Enable error messages */ init_task.kasan_depth =0 ; -pr_info ("KASAN init done\n" ); +kasan_init_generic (); }void __init kasan_late_init (void )
arch/powerpc/mm/kasan/init_book3e_64.c +1 -1
@@ -127,7 +127,7 @@ void __init kasan_init(void)/* Enable error messages */ init_task.kasan_depth =0 ; -pr_info ("KASAN init done\n" ); +kasan_init_generic (); }void __init kasan_late_init (void ) { }
arch/powerpc/mm/kasan/init_book3s_64.c +1 -5
@@ -19,8 +19,6 @@#include <linux/memblock.h> #include <asm/pgalloc.h> -DEFINE_STATIC_KEY_FALSE (powerpc_kasan_enabled_key); -static void __init kasan_init_phys_region (void *start,void *end) {unsigned long k_start, k_end, k_cur;
@@ -92,11 +90,9 @@ void __init kasan_init(void)*/ memset (kasan_early_shadow_page,0 ,PAGE_SIZE ); -static_branch_inc (&powerpc_kasan_enabled_key); -/* Enable error messages */ init_task.kasan_depth =0 ; -pr_info ("KASAN init done\n" ); +kasan_init_generic (); }void __init kasan_early_init (void ) { }
Signature · 1 line
2.34.1
PATCH v2 10/11 kasan/riscv: call kasan_init_generic in kasan_init
In reply to Sabyrzhan TasbolatovCall kasan_init_generic() which enables the static flag to mark generic KASAN initialized, otherwise it's an inline stub. Also prints the banner from the single place.
--- arch/riscv/mm/kasan_init.c | 1 + 1 file changed, 1 insertion(+)
arch/riscv/mm/kasan_init.c +1 -0
@@ -530,6 +530,7 @@ void __init kasan_init(void)memset (kasan_early_shadow_page,KASAN_SHADOW_INIT ,PAGE_SIZE ); init_task.kasan_depth =0 ; +kasan_init_generic ();csr_write (CSR_SATP ,PFN_DOWN (__pa (swapper_pg_dir)) | satp_mode);local_flush_tlb_all ();
Signature · 1 line
2.34.1
PATCH v2 11/11 kasan: replace kasan_arch_is_ready with kasan_enabled
In reply to Sabyrzhan TasbolatovReplace the existing kasan_arch_is_ready() calls with kasan_enabled(). Drop checks where the caller is already under kasan_enabled() condition.
--- include/linux/kasan-enabled.h | 20 ++++++++++++-------- mm/kasan/common.c | 8 ++++---- mm/kasan/generic.c | 6 +++--- mm/kasan/kasan.h | 6 ------ mm/kasan/shadow.c | 15 +++------------ 5 files changed, 22 insertions(+), 33 deletions(-)
include/linux/kasan-enabled.h +12 -8
@@ -5,32 +5,36 @@#include <linux/static_key.h> #ifdef CONFIG_KASAN +/* * Global runtime flag. Starts ‘false’; switched to ‘true’ by * the appropriate kasan_init_*() once KASAN is fully initialized. */ DECLARE_STATIC_KEY_FALSE (kasan_flag_enabled); -#endif - -#ifdef CONFIG_KASAN_HW_TAGS static __always_inline bool kasan_enabled(void) {return static_branch_likely (&kasan_flag_enabled); } -static inline bool kasan_hw_tags_enabled (void ) +#else /* !CONFIG_KASAN */ + +static __always_inline bool kasan_enabled(void) { -return kasan_enabled (); +return false; } -#else /* CONFIG_KASAN_HW_TAGS */ +#endif /* CONFIG_KASAN */ + +#ifdef CONFIG_KASAN_HW_TAGS -static inline bool kasan_enabled (void ) +static inline bool kasan_hw_tags_enabled (void ) { -return IS_ENABLED (CONFIG_KASAN ); +return kasan_enabled (); } +#else /* !CONFIG_KASAN_HW_TAGS */ +static inline bool kasan_hw_tags_enabled (void ) {return false;
mm/kasan/common.c +4 -4
@@ -257,7 +257,7 @@ static inline void poison_slab_object(struct kmem_cache *cache, void *object,bool __kasan_slab_pre_free (struct kmem_cache *cache,void *object,unsigned long ip) { -if (!kasan_arch_is_ready () ||is_kfence_address (object)) +if (!kasan_enabled () ||is_kfence_address (object))return false;return check_slab_allocation (cache, object, ip); }
@@ -265,7 +265,7 @@ bool __kasan_slab_pre_free(struct kmem_cache *cache, void *object,bool __kasan_slab_free (struct kmem_cache *cache,void *object,bool init,bool still_accessible) { -if (!kasan_arch_is_ready () ||is_kfence_address (object)) +if (!kasan_enabled () ||is_kfence_address (object))return false;poison_slab_object (cache, object, init, still_accessible);
@@ -289,7 +289,7 @@ bool __kasan_slab_free(struct kmem_cache *cache, void *object, bool init,static inline bool check_page_allocation (void *ptr,unsigned long ip) { -if (!kasan_arch_is_ready ()) +if (!kasan_enabled ())return false;if (ptr !=page_address (virt_to_head_page (ptr))) {
@@ -518,7 +518,7 @@ bool __kasan_mempool_poison_object(void *ptr, unsigned long ip)return true; } -if (is_kfence_address (ptr) || !kasan_arch_is_ready ()) +if (is_kfence_address (ptr) || !kasan_enabled ())return true; slab =folio_slab (folio);
mm/kasan/generic.c +3 -3
@@ -176,7 +176,7 @@ static __always_inline bool check_region_inline(const void *addr, size_t size, bool write, unsigned long ret_ip) { - if (!kasan_arch_is_ready()) + if (!kasan_enabled()) return true;if (unlikely (size ==0 ))
@@ -204,7 +204,7 @@ bool kasan_byte_accessible(const void *addr) {s8 shadow_byte; -if (!kasan_arch_is_ready ()) +if (!kasan_enabled ())return true; shadow_byte =READ_ONCE (*(s8 *)kasan_mem_to_shadow (addr));
@@ -506,7 +506,7 @@ static void release_alloc_meta(struct kasan_alloc_meta *meta)static void release_free_meta (const void *object,struct kasan_free_meta *meta) { -if (!kasan_arch_is_ready ()) +if (!kasan_enabled ())return ;/* Check if free meta is valid. */
mm/kasan/kasan.h +0 -6
@@ -544,12 +544,6 @@ static inline void kasan_poison_last_granule(const void *address, size_t size) {#endif /* CONFIG_KASAN_GENERIC */ -#ifndef kasan_arch_is_ready -static inline bool kasan_arch_is_ready (void ) {return true; } -#elif !defined(CONFIG_KASAN_GENERIC ) || !defined(CONFIG_KASAN_OUTLINE ) -#error kasan_arch_is_ready only works in KASAN generic outline mode! -#endif -#if IS_ENABLED (CONFIG_KASAN_KUNIT_TEST )void kasan_kunit_test_suite_start (void );
mm/kasan/shadow.c +3 -12
@@ -125,7 +125,7 @@ void kasan_poison(const void *addr, size_t size, u8 value, bool init) {void *shadow_start, *shadow_end; -if (!kasan_arch_is_ready ()) +if (!kasan_enabled ())return ;/*
@@ -150,9 +150,6 @@ EXPORT_SYMBOL_GPL(kasan_poison);#ifdef CONFIG_KASAN_GENERIC void kasan_poison_last_granule (const void *addr,size_t size) { -if (!kasan_arch_is_ready ()) -return ; -if (size &KASAN_GRANULE_MASK ) {u8 *shadow = (u8 *)kasan_mem_to_shadow (addr + size); *shadow = size &KASAN_GRANULE_MASK ;
@@ -390,7 +387,7 @@ int kasan_populate_vmalloc(unsigned long addr, unsigned long size)unsigned long shadow_start, shadow_end;int ret; -if (!kasan_arch_is_ready ()) +if (!kasan_enabled ())return 0 ;if (!is_vmalloc_or_module_addr ((void *)addr))
@@ -560,7 +557,7 @@ void kasan_release_vmalloc(unsigned long start, unsigned long end,unsigned long region_start, region_end;unsigned long size; -if (!kasan_arch_is_ready ()) +if (!kasan_enabled ())return ; region_start =ALIGN (start,KASAN_MEMORY_PER_SHADOW_PAGE );
@@ -611,9 +608,6 @@ void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,* with setting memory tags, so the KASAN_VMALLOC_INIT flag is ignored. */ -if (!kasan_arch_is_ready ()) -return (void *)start; -if (!is_vmalloc_or_module_addr (start))return (void *)start;
@@ -636,9 +630,6 @@ void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,*/ void __kasan_poison_vmalloc (const void *start,unsigned long size) { -if (!kasan_arch_is_ready ()) -return ; -if (!is_vmalloc_or_module_addr (start))return ;
Signature · 1 line
2.34.1
Andrey Konovalov
reviewerQuoted 22 lines · Sabyrzhan Tasbolatov
This patch series unifies the
kasan_arch_is_ready()andkasan_enabled()interfaces by extending the existingkasan_enabled()infrastructure to work consistently across all KASAN modes (Generic,SW_TAGS,HW_TAGS).Currently,
kasan_enabled()only works forHW_TAGSmode using a static key, while other modes either returnIS_ENABLED(CONFIG_KASAN)(compile-time constant) or rely on architecture-specifickasan_arch_is_ready()implementations with custom static keys and global variables.This leads to:
- Code duplication across architectures
- Inconsistent runtime behavior between KASAN modes
- Architecture-specific readiness trackingAfter this series:
- All KASAN modes use the samekasan_flag_enabledstatic key
- Consistent runtime enable/disable behavior across modes
- Simplified architecture code with unifiedkasan_init_generic()calls
- Elimination of arch specifickasan_arch_is_ready()implementations
- Unified vmalloc integration usingkasan_enabled()checks
Quoted 5 lines
This addresses the bugzilla issue [1] about making
kasan_flag_enabledandkasan_enabled()work for Generic mode, and extends it to provide true unification across all modes.
Hi Sabyrzhan,
Thank you for working on this!
One aspect that is missing from the patches is moving the kasan_arch_is_ready() calls into the include/linux/kasan.h (this is not explicitly mentioned in the issue, but this is what the "adding __wrappers" part is about).
Another thing that needs careful consideration is whether it's possible to combine kasan_arch_is_ready() and kasan_enabled() into the same check logically at all. There's one issue mentioned in [1]:
Quoted 5 lines
In
kasan_cache_create()we unconditionally allocate a metadata buffer, but thekasan_init_slab_obj()call to initialise it is guarded bykasan_enabled(). But later parts of the code only check the presence of the buffer before using it, so bad things happen ifkasan_enabled()later turns on (I was getting some error about invalid lock state).
And there might be other callbacks that should be executed even before kasan_init_...() completes. But then for the HW_TAGS mode, if kasan_enabled() is off, then we don't want to execute any callbacks.
So maybe we do actually need a separate static key for kasan_arch_is_ready(). But even if so, it still makes sense to move kasan_arch_is_ready() into the __wrappers for the affected callbacks.
Thanks!
[1] https://lore.kernel.org/linux-mm/CA+fCnZf7JqTH46C7oG2Wk9NnLU7hgiVDEK0EA8RAtyr-KgkHdg@mail.gmail.com/
Quoted 35 lines · Andrey Konovalov
Quoted 22 lines · Sabyrzhan Tasbolatov
This patch series unifies the
kasan_arch_is_ready()andkasan_enabled()interfaces by extending the existingkasan_enabled()infrastructure to work consistently across all KASAN modes (Generic,SW_TAGS,HW_TAGS).Currently,
kasan_enabled()only works forHW_TAGSmode using a static key, while other modes either returnIS_ENABLED(CONFIG_KASAN)(compile-time constant) or rely on architecture-specifickasan_arch_is_ready()implementations with custom static keys and global variables.This leads to:
- Code duplication across architectures
- Inconsistent runtime behavior between KASAN modes
- Architecture-specific readiness trackingAfter this series:
- All KASAN modes use the samekasan_flag_enabledstatic key
- Consistent runtime enable/disable behavior across modes
- Simplified architecture code with unifiedkasan_init_generic()calls
- Elimination of arch specifickasan_arch_is_ready()implementations
- Unified vmalloc integration usingkasan_enabled()checksQuoted 5 lines
This addresses the bugzilla issue [1] about making
kasan_flag_enabledandkasan_enabled()work for Generic mode, and extends it to provide true unification across all modes.Hi Sabyrzhan,
Thank you for working on this!
Quoted 8 lines
One aspect that is missing from the patches is moving the
kasan_arch_is_ready()calls into theinclude/linux/kasan.h(this is not explicitly mentioned in the issue, but this is what the "adding__wrappers" part is about).Another thing that needs careful consideration is whether it's possible to combine
kasan_arch_is_ready()andkasan_enabled()into the same check logically at all. There's one issue mentioned in [1]:
Hello,
I've removed kasan_arch_is_ready() at all in this series:
[PATCH v2 11/11] kasan: replace kasan_arch_is_ready with kasan_enabled
Is it not what's expected by unification?
Quoted 18 lines
Quoted 6 lines
In
kasan_cache_create()we unconditionally allocate a metadata buffer, but thekasan_init_slab_obj()call to initialise it is guarded bykasan_enabled(). But later parts of the code only check the presence of the buffer before using it, so bad things happen ifkasan_enabled()later turns on (I was getting some error about invalid lock state).And there might be other callbacks that should be executed even before kasan_init_...() completes. But then for the
HW_TAGSmode, ifkasan_enabled()is off, then we don't want to execute any callbacks.So maybe we do actually need a separate static key for
kasan_arch_is_ready(). But even if so, it still makes sense to movekasan_arch_is_ready()into the__wrappersfor the affected callbacks.Thanks!
[1] https://lore.kernel.org/linux-mm/CA+fCnZf7JqTH46C7oG2Wk9NnLU7hgiVDEK0EA8RAtyr-KgkHdg@mail.gmail.com/
Andrey Konovalov
reviewerQuoted 46 lines · Sabyrzhan Tasbolatov
Quoted 35 lines · Andrey Konovalov
Quoted 22 lines · Sabyrzhan Tasbolatov
This patch series unifies the
kasan_arch_is_ready()andkasan_enabled()interfaces by extending the existingkasan_enabled()infrastructure to work consistently across all KASAN modes (Generic,SW_TAGS,HW_TAGS).Currently,
kasan_enabled()only works forHW_TAGSmode using a static key, while other modes either returnIS_ENABLED(CONFIG_KASAN)(compile-time constant) or rely on architecture-specifickasan_arch_is_ready()implementations with custom static keys and global variables.This leads to:
- Code duplication across architectures
- Inconsistent runtime behavior between KASAN modes
- Architecture-specific readiness trackingAfter this series:
- All KASAN modes use the samekasan_flag_enabledstatic key
- Consistent runtime enable/disable behavior across modes
- Simplified architecture code with unifiedkasan_init_generic()calls
- Elimination of arch specifickasan_arch_is_ready()implementations
- Unified vmalloc integration usingkasan_enabled()checksQuoted 5 lines
This addresses the bugzilla issue [1] about making
kasan_flag_enabledandkasan_enabled()work for Generic mode, and extends it to provide true unification across all modes.Hi Sabyrzhan,
Thank you for working on this!
Quoted 8 lines
One aspect that is missing from the patches is moving the
kasan_arch_is_ready()calls into theinclude/linux/kasan.h(this is not explicitly mentioned in the issue, but this is what the "adding__wrappers" part is about).Another thing that needs careful consideration is whether it's possible to combine
kasan_arch_is_ready()andkasan_enabled()into the same check logically at all. There's one issue mentioned in [1]:
Quoted 5 lines
Hello,
I've removedkasan_arch_is_ready()at all in this series:
[PATCH v2 11/11] kasan: replacekasan_arch_is_readywithkasan_enabledIs it not what's expected by unification?
I guess the issue description diverged a bit from what needs to be done, sorry about that.
The core 2 things I wanted to address with the unification are:
1. Avoid spraying kasan_arch_is_ready() throughout the KASAN implementation and move these checks into include/linux/kasan.h (and add __wrappers when required).
2. Avoid architectures redefining the same kasan_enabled global variable/static key.
Initially, I thought that s/kasan_arch_is_ready/kasan_enabled + simply moving the calls into affected include/linux/kasan.h functions would be enough. But then, based on [1], turns out it's not that simple.
So now, I think we likely still need two separate checks/flags:kasan_enabled() that controls whether KASAN is enabled at all and kasan_arch_is_ready() that gets turned on by kasan_init() when shadow is initialized (should we rename it to kasan_shadow_initialized()?). But then we can still move kasan_arch_is_ready() into include/linux/kasan.h and use the proper combination of checks for each affected function before calling __wrappers. And we can still remove the duplicated flags/keys code from the arch code.
[1] https://lore.kernel.org/linux-mm/CA+fCnZf7JqTH46C7oG2Wk9NnLU7hgiVDEK0EA8RAtyr-KgkHdg@mail.gmail.com/
Heiko Carstens
maintainerQuoted 26 lines
Quoted 9 lines
Quoted 3 lines
Another thing that needs careful consideration is whether it's possible to combine
kasan_arch_is_ready()andkasan_enabled()into the same check logically at all. There's one issue mentioned in [1]:Hello,
I've removedkasan_arch_is_ready()at all in this series:
[PATCH v2 11/11] kasan: replacekasan_arch_is_readywithkasan_enabledIs it not what's expected by unification?
I guess the issue description diverged a bit from what needs to be done, sorry about that.
The core 2 things I wanted to address with the unification are:
1. Avoid spraying
kasan_arch_is_ready()throughout the KASAN implementation and move these checks intoinclude/linux/kasan.h(and add__wrapperswhen required).2. Avoid architectures redefining the same
kasan_enabledglobal variable/static key.Initially, I thought that s/kasan_arch_is_ready/kasan_enabled + simply moving the calls into affected
include/linux/kasan.hfunctions would be enough. But then, based on [1], turns out it's not that simple.
Quoted 8 lines
So now, I think we likely still need two separate checks/flags:
kasan_enabled()that controls whether KASAN is enabled at all andkasan_arch_is_ready()that gets turned on bykasan_init()when shadow is initialized (should we rename it tokasan_shadow_initialized()?). But then we can still movekasan_arch_is_ready()intoinclude/linux/kasan.hand use the proper combination of checks for each affected function before calling__wrappers. And we can still remove the duplicated flags/keys code from the arch code.
FWIW, as Alexander Gordeev already mentioned: this series breaks s390, since the static_branch_enable() call in kasan_init_generic() is now called way too early, and it isn't necessary at all. Which, as far as I understand, may be the case for other architectures as well. s390 sets up the required KASAN mappings in the decompressor and can start with KASAN enabled nearly from the beginning.
So something like below on top of this series would address that. Given that this series is about to be reworked this is just for illustration :)
arch/s390/Kconfig +1 -0
@@ -172,6 +172,7 @@ config S390 select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_JUMP_LABEL_RELATIVE select HAVE_ARCH_KASAN + select HAVE_ARCH_KASAN_EARLY select HAVE_ARCH_KASAN_VMALLOC select HAVE_ARCH_KCSAN select HAVE_ARCH_KMSAN
include/linux/kasan-enabled.h +4 -0
@@ -10,7 +10,11 @@* Global runtime flag. Starts ‘false’; switched to ‘true’ by * the appropriate kasan_init_*() once KASAN is fully initialized. */ +#ifdef CONFIG_HAVE_ARCH_KASAN_EARLY +DECLARE_STATIC_KEY_TRUE (kasan_flag_enabled); +#else DECLARE_STATIC_KEY_FALSE (kasan_flag_enabled); +#endif static __always_inline bool kasan_enabled(void) {
lib/Kconfig.kasan +7 -0
@@ -4,6 +4,13 @@ config HAVE_ARCH_KASAN bool +config HAVE_ARCH_KASAN_EARLY + bool + help + Architectures should select this if KASAN mappings are setup in + the decompressor and when the kernel can run very early with + KASAN enabled. + config HAVE_ARCH_KASAN_SW_TAGS bool
mm/kasan/common.c +4 -0
@@ -36,7 +36,11 @@* Definition of the unified static key declared in kasan-enabled.h. * This provides consistent runtime enable/disable across all KASAN modes. */ +#ifdef CONFIG_HAVE_ARCH_KASAN_EARLY +DEFINE_STATIC_KEY_TRUE (kasan_flag_enabled); +#else DEFINE_STATIC_KEY_FALSE (kasan_flag_enabled); +#endif EXPORT_SYMBOL (kasan_flag_enabled);struct slab *kasan_addr_to_slab (const void *addr)
mm/kasan/generic.c +2 -1
@@ -42,7 +42,8 @@*/ void __init kasan_init_generic (void ) { -static_branch_enable (&kasan_flag_enabled); +if (!IS_ENABLED (CONFIG_HAVE_ARCH_KASAN_EARLY )) +static_branch_enable (&kasan_flag_enabled);pr_info ("KernelAddressSanitizer initialized (generic)\n" ); }
Le 01/07/2025 à 12:15, Heiko Carstens a écrit :
Quoted 42 lines
Quoted 26 lines
Quoted 9 lines
Quoted 3 lines
Another thing that needs careful consideration is whether it's possible to combine
kasan_arch_is_ready()andkasan_enabled()into the same check logically at all. There's one issue mentioned in [1]:Hello,
I've removedkasan_arch_is_ready()at all in this series:
[PATCH v2 11/11] kasan: replacekasan_arch_is_readywithkasan_enabledIs it not what's expected by unification?
I guess the issue description diverged a bit from what needs to be done, sorry about that.
The core 2 things I wanted to address with the unification are:
1. Avoid spraying
kasan_arch_is_ready()throughout the KASAN implementation and move these checks intoinclude/linux/kasan.h(and add__wrapperswhen required).2. Avoid architectures redefining the same
kasan_enabledglobal variable/static key.Initially, I thought that s/kasan_arch_is_ready/kasan_enabled + simply moving the calls into affected
include/linux/kasan.hfunctions would be enough. But then, based on [1], turns out it's not that simple.Quoted 8 lines
So now, I think we likely still need two separate checks/flags:
kasan_enabled()that controls whether KASAN is enabled at all andkasan_arch_is_ready()that gets turned on bykasan_init()when shadow is initialized (should we rename it tokasan_shadow_initialized()?). But then we can still movekasan_arch_is_ready()intoinclude/linux/kasan.hand use the proper combination of checks for each affected function before calling__wrappers. And we can still remove the duplicated flags/keys code from the arch code.FWIW, as Alexander Gordeev already mentioned: this series breaks s390, since the
static_branch_enable()call inkasan_init_generic()is now called way too early, and it isn't necessary at all. Which, as far as I understand, may be the case for other architectures as well. s390 sets up the required KASAN mappings in the decompressor and can start with KASAN enabled nearly from the beginning.
Quoted 3 lines
So something like below on top of this series would address that. Given that this series is about to be reworked this is just for illustration :)
I had the same kind of comment on powerpc/32. Allthough this series work on powerpc32 as is, it is overkill because it adds code and data for static branches for no real benefit.
Your patch below is simpler than what I proposed, but it keeps the static branches so the overhead remains.
I also proposed a change, it goes further by removing the static branch for architectures that don't need it, see https://patchwork.ozlabs.org/project/linuxppc-dev/cover/[email protected]/#3537388 . Feedback welcome.
Christophe
Quoted 77 lines
arch/s390/Kconfig +1
-0@@ -172,6 +172,7 @@ config S390 select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_JUMP_LABEL_RELATIVE select HAVE_ARCH_KASAN + select HAVE_ARCH_KASAN_EARLY select HAVE_ARCH_KASAN_VMALLOC select HAVE_ARCH_KCSAN select HAVE_ARCH_KMSANinclude/linux/kasan-enabled.h +4
-0@@ -10,7 +10,11 @@* Global runtime flag. Starts ‘false’; switched to ‘true’ by * the appropriate kasan_init_*() once KASAN is fully initialized. */ +#ifdef CONFIG_HAVE_ARCH_KASAN_EARLY +DECLARE_STATIC_KEY_TRUE (kasan_flag_enabled); +#else DECLARE_STATIC_KEY_FALSE (kasan_flag_enabled); +#endif static __always_inline bool kasan_enabled(void) {lib/Kconfig.kasan +7
-0@@ -4,6 +4,13 @@ config HAVE_ARCH_KASAN bool +config HAVE_ARCH_KASAN_EARLY + bool + help + Architectures should select this if KASAN mappings are setup in + the decompressor and when the kernel can run very early with + KASAN enabled. + config HAVE_ARCH_KASAN_SW_TAGS boolmm/kasan/common.c +4
-0@@ -36,7 +36,11 @@* Definition of the unified static key declared in kasan-enabled.h. * This provides consistent runtime enable/disable across all KASAN modes. */ +#ifdef CONFIG_HAVE_ARCH_KASAN_EARLY +DEFINE_STATIC_KEY_TRUE (kasan_flag_enabled); +#else DEFINE_STATIC_KEY_FALSE (kasan_flag_enabled); +#endif EXPORT_SYMBOL (kasan_flag_enabled);struct slab *kasan_addr_to_slab (const void *addr)mm/kasan/generic.c +2
-1@@ -42,7 +42,8 @@*/ void __init kasan_init_generic (void ) { -static_branch_enable (&kasan_flag_enabled); +if (!IS_ENABLED (CONFIG_HAVE_ARCH_KASAN_EARLY )) +static_branch_enable (&kasan_flag_enabled);pr_info ("KernelAddressSanitizer initialized (generic)\n" ); }
Heiko Carstens
maintainerQuoted 6 lines · Christophe Leroy
Your patch below is simpler than what I proposed, but it keeps the static branches so the overhead remains.
I also proposed a change, it goes further by removing the static branch for
architectures that don't need it, see https://patchwork.ozlabs.org/project/linuxppc-dev/cover/[email protected]/#3537388
. Feedback welcome.
Yes, removing the static branches during compile time is of course even better.
Le 26/06/2025 à 17:31, Sabyrzhan Tasbolatov a écrit :
Quoted 9 lines
This patch series unifies the
kasan_arch_is_ready()andkasan_enabled()interfaces by extending the existingkasan_enabled()infrastructure to work consistently across all KASAN modes (Generic,SW_TAGS,HW_TAGS).Currently,
kasan_enabled()only works forHW_TAGSmode using a static key, while other modes either returnIS_ENABLED(CONFIG_KASAN)(compile-time constant) or rely on architecture-specifickasan_arch_is_ready()implementations with custom static keys and global variables.
Quoted 4 lines
This leads to:
- Code duplication across architectures
- Inconsistent runtime behavior between KASAN modes
- Architecture-specific readiness tracking
You should also consider refactoring ARCH_DISABLE_KASAN_INLINE, there is a high dependency between deferring KASAN readiness and not supporting inline KASAN.
Quoted 7 lines
After this series:
- All KASAN modes use the samekasan_flag_enabledstatic key
- Consistent runtime enable/disable behavior across modes
- Simplified architecture code with unifiedkasan_init_generic()calls
- Elimination of arch specifickasan_arch_is_ready()implementations
- Unified vmalloc integration usingkasan_enabled()checks
I dislike that modes which can be enabled from the very begining now also depends on the static key being enabled later.
The size is increased for no valid reason:
$ size vmlinux.kasan* text data bss dec hex filename 13965336 6716942 494912 21177190 1432366 vmlinux.kasan0 ==> outline KASAN before your patch 13965496 6718422 494944 21178862 14329ee vmlinux.kasan1 ==> outline KASAN after your patch 13965336 6716942 494912 21177190 1432366 vmlinux.kasan2 ==> outline KASAN after your patch + below change 32517472 6716958 494912 39729342 25e38be vmlinux.kasani0 ==> inline KASAN before your patch 32518848 6718438 494944 39732230 25e4406 vmlinux.kasani1 ==> inline KASAN after your patch 32517536 6716958 494912 39729406 25e38fe vmlinux.kasani2 ==> inline KASAN after your patch + below change
Below change (atop you series) only makes use of static key when needed:
arch/powerpc/Kconfig +2 -1
@@ -122,6 +122,7 @@ config PPC # Please keep this list sorted alphabetically. # select ARCH_32BIT_OFF_T if PPC32 + select ARCH_DEFER_KASAN if PPC_RADIX_MMU select ARCH_DISABLE_KASAN_INLINE if PPC_RADIX_MMU select ARCH_DMA_DEFAULT_COHERENT if !NOT_COHERENT_CACHE select ARCH_ENABLE_MEMORY_HOTPLUG
@@ -219,7 +220,7 @@ config PPC 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_DEFERED 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
include/linux/kasan-enabled.h +11 -3
@@ -4,7 +4,7 @@#include <linux/static_key.h> -#ifdef CONFIG_KASAN +#ifdef CONFIG_KASAN_DEFER /* * Global runtime flag. Starts âfalseâ; switched to âtrueâ by
@@ -17,13 +17,21 @@ static __always_inline bool kasan_enabled(void)return static_branch_likely (&kasan_flag_enabled); } -#else /* !CONFIG_KASAN */ +static inline void kasan_enable (void ) +{ +static_branch_enable (&kasan_flag_enabled); +} + +#else /* !CONFIG_KASAN_DEFER */ static __always_inline bool kasan_enabled(void) { -return false; +return IS_ENABLED (CONFIG_KASAN ); } +static inline void kasan_enable (void ) +{ +}#endif /* CONFIG_KASAN */ #ifdef CONFIG_KASAN_HW_TAGS
lib/Kconfig.kasan +6 -0
@@ -13,6 +13,9 @@ config HAVE_ARCH_KASAN_HW_TAGS config HAVE_ARCH_KASAN_VMALLOC bool +config ARCH_DEFER_KASAN + bool + config ARCH_DISABLE_KASAN_INLINE bool help
@@ -58,6 +61,9 @@ config CC_HAS_KASAN_MEMINTRINSIC_PREFIX help The compiler is able to prefix memintrinsics with __asan or __hwasan. +config KASAN_DIFER + def_bool ARCH_DIFER_KASAN + choice prompt "KASAN mode" default KASAN_GENERIC
mm/kasan/common.c +2 -0
@@ -36,8 +36,10 @@* Definition of the unified static key declared in kasan-enabled.h. * This provides consistent runtime enable/disable across all KASAN modes. */ +#ifdef CONFIG_KASAN_DEFER DEFINE_STATIC_KEY_FALSE (kasan_flag_enabled);EXPORT_SYMBOL (kasan_flag_enabled); +#endif struct slab *kasan_addr_to_slab (const void *addr) {
mm/kasan/generic.c +1 -1
@@ -42,7 +42,7 @@*/ void __init kasan_init_generic (void ) { -static_branch_enable (&kasan_flag_enabled); +kasan_enable ();pr_info ("KernelAddressSanitizer initialized (generic)\n" ); }
mm/kasan/hw_tags.c +1 -1
@@ -253,7 +253,7 @@ void __init kasan_init_hw_tags(void)kasan_init_tags ();/* KASAN is now initialized, enable it. */ -static_branch_enable (&kasan_flag_enabled); +kasan_enable (); pr_info("KernelAddressSanitizer initialized (hw-tags, mode=%s,
vmalloc=%s, stacktrace=%s)\n",
kasan_mode_info(),
mm/kasan/sw_tags.c +1 -1
@@ -45,7 +45,7 @@ void __init kasan_init_sw_tags(void)kasan_init_tags (); -static_branch_enable (&kasan_flag_enabled); +kasan_enable ();pr_info ("KernelAddressSanitizer initialized (sw-tags, stacktrace=%s)\n" ,str_on_off (kasan_stack_collection_enabled ()));
Quoted 134 lines
This addresses the bugzilla issue [1] about making
kasan_flag_enabledandkasan_enabled()work for Generic mode, and extends it to provide true unification across all modes.[1] https://bugzilla.kernel.org/show_bug.cgi?id=217049
=== Current mainline KUnit status
To see if there is any regression, I've tested first on the following commit
739a6c93cc75("Merge tag 'nfsd-6.16-1' ofgit://git.kernel.org/pub/scm/linux/kernel/git/cel/linux").Tested via compiling a kernel with
CONFIG_KASAN_KUNIT_TESTand running QEMU VM. There are failing tests inSW_TAGSand GENERIC modes in arm64:arm64
CONFIG_KASAN_HW_TAGS:# kasan: pass:62 fail:0 skip:13 total:75 # Totals: pass:62 fail:0 skip:13 total:75 ok 1 kasanarm64
CONFIG_KASAN_SW_TAGS=y:
# kasan: pass:65 fail:1 skip:9 total:75
# Totals: pass:65 fail:1 skip:9 total:75
not ok 1 kasan
#kasan_strings: EXPECTATION FAILED atmm/kasan/kasan_test_c.c:1598
KASAN failure expected in "strscpy(ptr, src + KASAN_GRANULE_SIZE, KASAN_GRANULE_SIZE)", but none occurredarm64
CONFIG_KASAN_GENERIC=y,CONFIG_KASAN_OUTLINE=y:# kasan: pass:61 fail:1 skip:13 total:75 # Totals: pass:61 fail:1 skip:13 total:75 not ok 1 kasan # same failure as above
x86_64CONFIG_KASAN_GENERIC=y:# kasan: pass:58 fail:0 skip:17 total:75 # Totals: pass:58 fail:0 skip:17 total:75 ok 1 kasan=== Testing with patches
Testing in v2:
- Compiled every affected arch with no errors:
$ make CC=clang LD=ld.lld AR=llvm-ar NM=llvm-nm STRIP=llvm-strip \ OBJCOPY=llvm-objcopy OBJDUMP=llvm-objdump READELF=llvm-readelf \ HOSTCC=clang HOSTCXX=clang++ HOSTAR=llvm-ar HOSTLD=ld.lld \ ARCH=$ARCH$ clang --version ClangBuiltLinux clang version 19.1.4 Target: x86_64-unknown-linux-gnu Thread model: posix- make ARCH=um produces the warning during compiling:
MODPOST Module.symvers WARNING: modpost: vmlinux: section mismatch in reference: \ kasan_init+0x43 (section: .ltext) -> \ kasan_init_generic (section: .init.text)AFAIU, it's due to the code in
arch/um/kernel/mem.c, wherekasan_init()is placed in own section ".kasan_init", which callskasan_init_generic()which is marked with "__init".- Booting via qemu-system- and running KUnit tests:
* arm64 (GENERIC,
HW_TAGS,SW_TAGS): no regression, same above results.
*x86_64(GENERIC): no regression, no errors=== NB
I haven't tested the kernel boot on the following arch. due to the absence of qemu-system- support on those arch on my machine, so I defer this to relevant arch people to test KASAN initialization:
- loongarch
- s390
- um
- xtensa
- powerpc
- riscvCode changes in v2:
- Replace the order of patches. Move "kasan: replacekasan_arch_is_readywithkasan_enabled" at the end to keep the compatibility.
-arch/arm,arch/riscv: add 2 arch. missed in v1
-arch/powerpc: addkasan_init_generic()in otherkasan_init()calls:arch/powerpc/mm/kasan/init_32.c arch/powerpc/mm/kasan/init_book3e_64.c-
arch/um: add the proper header#include <linux/kasan.h>. Tested via compiling with no errors. In the v1arch/umchanges were acked-by Johannes Berg, though I don't include it due to the changed code in v2.
-arch/powerpc: add back#ifdef CONFIG_KASANdeleted in v1 and tested the compilation.
-arch/loongarch: update git commit message about non-standard flow of callingkasan_init_generic()Sabyrzhan Tasbolatov (11):
kasan: unify statickasan_flag_enabledacross modes kasan/arm64: callkasan_init_genericinkasan_initkasan/arm: callkasan_init_genericinkasan_initkasan/xtensa: callkasan_init_genericinkasan_initkasan/loongarch: callkasan_init_genericinkasan_initkasan/um: callkasan_init_genericinkasan_initkasan/x86: callkasan_init_genericinkasan_initkasan/s390: callkasan_init_genericinkasan_initkasan/powerpc: callkasan_init_genericinkasan_initkasan/riscv: callkasan_init_genericinkasan_init
kasan: replacekasan_arch_is_readywithkasan_enabledarch/arm/mm/kasan_init.c | 2 +- arch/arm64/mm/kasan_init.c | 4 +--- arch/loongarch/include/asm/kasan.h | 7 ------- arch/loongarch/mm/kasan_init.c | 7 ++----- arch/powerpc/include/asm/kasan.h | 13 ------------- arch/powerpc/mm/kasan/init_32.c | 2 +- arch/powerpc/mm/kasan/init_book3e_64.c | 2 +- arch/powerpc/mm/kasan/init_book3s_64.c | 6 +----- arch/riscv/mm/kasan_init.c | 1 + arch/s390/kernel/early.c | 3 ++- arch/um/include/asm/kasan.h | 5 ----- arch/um/kernel/mem.c | 4 ++-- arch/x86/mm/kasan_init_64.c | 2 +- arch/xtensa/mm/kasan_init.c | 2 +- include/linux/kasan-enabled.h | 22 ++++++++++++++++------ include/linux/kasan.h | 6 ++++++ mm/kasan/common.c | 15 +++++++++++---- mm/kasan/generic.c | 17 ++++++++++++++--- mm/kasan/hw_tags.c | 7 ------- mm/kasan/kasan.h | 6 ------ mm/kasan/shadow.c | 15 +++------------ mm/kasan/sw_tags.c | 2 ++ 22 files changed, 66 insertions(+), 84 deletions(-)