linux-s390.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 00/11] kasan: unify kasan_arch_is_ready with kasan_enabled
@ 2025-06-26 15:31 Sabyrzhan Tasbolatov
  2025-06-26 15:31 ` [PATCH v2 01/11] kasan: unify static kasan_flag_enabled across modes Sabyrzhan Tasbolatov
                   ` (12 more replies)
  0 siblings, 13 replies; 23+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-26 15:31 UTC (permalink / raw)
  To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	linux, catalin.marinas, will, chenhuacai, kernel, maddy, mpe,
	npiggin, christophe.leroy, paul.walmsley, palmer, aou, alex, hca,
	gor, agordeev, borntraeger, svens, richard, anton.ivanov,
	johannes, dave.hansen, luto, peterz, tglx, mingo, bp, x86, hpa,
	chris, jcmvbkbc, akpm, nathan, nick.desaulniers+lkml, morbo,
	justinstitt
  Cc: arnd, rppt, geert, mcgrof, guoweikang.kernel, tiwei.btw,
	kevin.brodsky, benjamin.berg, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-riscv, linux-s390,
	linux-um, linux-mm, llvm, snovitoll

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' of
git://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(-)

-- 
2.34.1


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

* [PATCH v2 01/11] kasan: unify static kasan_flag_enabled across modes
  2025-06-26 15:31 [PATCH v2 00/11] kasan: unify kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
@ 2025-06-26 15:31 ` Sabyrzhan Tasbolatov
  2025-06-30 12:31   ` Alexander Gordeev
  2025-06-26 15:31 ` [PATCH v2 02/11] kasan/arm64: call kasan_init_generic in kasan_init Sabyrzhan Tasbolatov
                   ` (11 subsequent siblings)
  12 siblings, 1 reply; 23+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-26 15:31 UTC (permalink / raw)
  To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	linux, catalin.marinas, will, chenhuacai, kernel, maddy, mpe,
	npiggin, christophe.leroy, paul.walmsley, palmer, aou, alex, hca,
	gor, agordeev, borntraeger, svens, richard, anton.ivanov,
	johannes, dave.hansen, luto, peterz, tglx, mingo, bp, x86, hpa,
	chris, jcmvbkbc, akpm, nathan, nick.desaulniers+lkml, morbo,
	justinstitt
  Cc: arnd, rppt, geert, mcgrof, guoweikang.kernel, tiwei.btw,
	kevin.brodsky, benjamin.berg, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-riscv, linux-s390,
	linux-um, linux-mm, llvm, snovitoll

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.

Closes: https://bugzilla.kernel.org/show_bug.cgi?id=218315
Signed-off-by: Sabyrzhan Tasbolatov <snovitoll@gmail.com>
---
 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(-)

diff --git a/include/linux/kasan-enabled.h b/include/linux/kasan-enabled.h
index 6f612d69ea0..2b1351c30c6 100644
--- a/include/linux/kasan-enabled.h
+++ b/include/linux/kasan-enabled.h
@@ -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)
 {
diff --git a/include/linux/kasan.h b/include/linux/kasan.h
index 890011071f2..51a8293d1af 100644
--- a/include/linux/kasan.h
+++ b/include/linux/kasan.h
@@ -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
diff --git a/mm/kasan/common.c b/mm/kasan/common.c
index ed4873e18c7..525194da25f 100644
--- a/mm/kasan/common.c
+++ b/mm/kasan/common.c
@@ -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))
diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c
index d54e89f8c3e..32c432df24a 100644
--- a/mm/kasan/generic.c
+++ b/mm/kasan/generic.c
@@ -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
diff --git a/mm/kasan/hw_tags.c b/mm/kasan/hw_tags.c
index 9a6927394b5..8e819fc4a26 100644
--- a/mm/kasan/hw_tags.c
+++ b/mm/kasan/hw_tags.c
@@ -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.
diff --git a/mm/kasan/sw_tags.c b/mm/kasan/sw_tags.c
index b9382b5b6a3..525bc91e2fc 100644
--- a/mm/kasan/sw_tags.c
+++ b/mm/kasan/sw_tags.c
@@ -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()));
 }
-- 
2.34.1


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

* [PATCH v2 02/11] kasan/arm64: call kasan_init_generic in kasan_init
  2025-06-26 15:31 [PATCH v2 00/11] kasan: unify kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
  2025-06-26 15:31 ` [PATCH v2 01/11] kasan: unify static kasan_flag_enabled across modes Sabyrzhan Tasbolatov
@ 2025-06-26 15:31 ` Sabyrzhan Tasbolatov
  2025-06-26 15:31 ` [PATCH v2 03/11] kasan/arm: " Sabyrzhan Tasbolatov
                   ` (10 subsequent siblings)
  12 siblings, 0 replies; 23+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-26 15:31 UTC (permalink / raw)
  To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	linux, catalin.marinas, will, chenhuacai, kernel, maddy, mpe,
	npiggin, christophe.leroy, paul.walmsley, palmer, aou, alex, hca,
	gor, agordeev, borntraeger, svens, richard, anton.ivanov,
	johannes, dave.hansen, luto, peterz, tglx, mingo, bp, x86, hpa,
	chris, jcmvbkbc, akpm, nathan, nick.desaulniers+lkml, morbo,
	justinstitt
  Cc: arnd, rppt, geert, mcgrof, guoweikang.kernel, tiwei.btw,
	kevin.brodsky, benjamin.berg, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-riscv, linux-s390,
	linux-um, linux-mm, llvm, snovitoll

Call 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().

Closes: https://bugzilla.kernel.org/show_bug.cgi?id=218315
Signed-off-by: Sabyrzhan Tasbolatov <snovitoll@gmail.com>
---
 arch/arm64/mm/kasan_init.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/arch/arm64/mm/kasan_init.c b/arch/arm64/mm/kasan_init.c
index d541ce45dae..abeb81bf6eb 100644
--- a/arch/arm64/mm/kasan_init.c
+++ b/arch/arm64/mm/kasan_init.c
@@ -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 */
-- 
2.34.1


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

* [PATCH v2 03/11] kasan/arm: call kasan_init_generic in kasan_init
  2025-06-26 15:31 [PATCH v2 00/11] kasan: unify kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
  2025-06-26 15:31 ` [PATCH v2 01/11] kasan: unify static kasan_flag_enabled across modes Sabyrzhan Tasbolatov
  2025-06-26 15:31 ` [PATCH v2 02/11] kasan/arm64: call kasan_init_generic in kasan_init Sabyrzhan Tasbolatov
@ 2025-06-26 15:31 ` Sabyrzhan Tasbolatov
  2025-06-26 15:31 ` [PATCH v2 04/11] kasan/xtensa: " Sabyrzhan Tasbolatov
                   ` (9 subsequent siblings)
  12 siblings, 0 replies; 23+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-26 15:31 UTC (permalink / raw)
  To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	linux, catalin.marinas, will, chenhuacai, kernel, maddy, mpe,
	npiggin, christophe.leroy, paul.walmsley, palmer, aou, alex, hca,
	gor, agordeev, borntraeger, svens, richard, anton.ivanov,
	johannes, dave.hansen, luto, peterz, tglx, mingo, bp, x86, hpa,
	chris, jcmvbkbc, akpm, nathan, nick.desaulniers+lkml, morbo,
	justinstitt
  Cc: arnd, rppt, geert, mcgrof, guoweikang.kernel, tiwei.btw,
	kevin.brodsky, benjamin.berg, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-riscv, linux-s390,
	linux-um, linux-mm, llvm, snovitoll

Call kasan_init_generic() which enables the static flag to mark KASAN
initialized in CONFIG_KASAN_GENERIC mode, otherwise it's an inline stub.

Closes: https://bugzilla.kernel.org/show_bug.cgi?id=218315
Signed-off-by: Sabyrzhan Tasbolatov <snovitoll@gmail.com>
---
 arch/arm/mm/kasan_init.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/arm/mm/kasan_init.c b/arch/arm/mm/kasan_init.c
index 111d4f70313..c6625e808bf 100644
--- a/arch/arm/mm/kasan_init.c
+++ b/arch/arm/mm/kasan_init.c
@@ -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();
 }
-- 
2.34.1


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

* [PATCH v2 04/11] kasan/xtensa: call kasan_init_generic in kasan_init
  2025-06-26 15:31 [PATCH v2 00/11] kasan: unify kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
                   ` (2 preceding siblings ...)
  2025-06-26 15:31 ` [PATCH v2 03/11] kasan/arm: " Sabyrzhan Tasbolatov
@ 2025-06-26 15:31 ` Sabyrzhan Tasbolatov
  2025-06-26 15:31 ` [PATCH v2 05/11] kasan/loongarch: " Sabyrzhan Tasbolatov
                   ` (8 subsequent siblings)
  12 siblings, 0 replies; 23+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-26 15:31 UTC (permalink / raw)
  To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	linux, catalin.marinas, will, chenhuacai, kernel, maddy, mpe,
	npiggin, christophe.leroy, paul.walmsley, palmer, aou, alex, hca,
	gor, agordeev, borntraeger, svens, richard, anton.ivanov,
	johannes, dave.hansen, luto, peterz, tglx, mingo, bp, x86, hpa,
	chris, jcmvbkbc, akpm, nathan, nick.desaulniers+lkml, morbo,
	justinstitt
  Cc: arnd, rppt, geert, mcgrof, guoweikang.kernel, tiwei.btw,
	kevin.brodsky, benjamin.berg, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-riscv, linux-s390,
	linux-um, linux-mm, llvm, snovitoll

Call 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.

Closes: https://bugzilla.kernel.org/show_bug.cgi?id=218315
Signed-off-by: Sabyrzhan Tasbolatov <snovitoll@gmail.com>
---
 arch/xtensa/mm/kasan_init.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/xtensa/mm/kasan_init.c b/arch/xtensa/mm/kasan_init.c
index f39c4d83173..0524b9ed5e6 100644
--- a/arch/xtensa/mm/kasan_init.c
+++ b/arch/xtensa/mm/kasan_init.c
@@ -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();
 }
-- 
2.34.1


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

* [PATCH v2 05/11] kasan/loongarch: call kasan_init_generic in kasan_init
  2025-06-26 15:31 [PATCH v2 00/11] kasan: unify kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
                   ` (3 preceding siblings ...)
  2025-06-26 15:31 ` [PATCH v2 04/11] kasan/xtensa: " Sabyrzhan Tasbolatov
@ 2025-06-26 15:31 ` Sabyrzhan Tasbolatov
  2025-06-26 15:31 ` [PATCH v2 06/11] kasan/um: " Sabyrzhan Tasbolatov
                   ` (7 subsequent siblings)
  12 siblings, 0 replies; 23+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-26 15:31 UTC (permalink / raw)
  To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	linux, catalin.marinas, will, chenhuacai, kernel, maddy, mpe,
	npiggin, christophe.leroy, paul.walmsley, palmer, aou, alex, hca,
	gor, agordeev, borntraeger, svens, richard, anton.ivanov,
	johannes, dave.hansen, luto, peterz, tglx, mingo, bp, x86, hpa,
	chris, jcmvbkbc, akpm, nathan, nick.desaulniers+lkml, morbo,
	justinstitt
  Cc: arnd, rppt, geert, mcgrof, guoweikang.kernel, tiwei.btw,
	kevin.brodsky, benjamin.berg, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-riscv, linux-s390,
	linux-um, linux-mm, llvm, snovitoll

Call 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.

Closes: https://bugzilla.kernel.org/show_bug.cgi?id=218315
Signed-off-by: Sabyrzhan Tasbolatov <snovitoll@gmail.com>
---
 arch/loongarch/include/asm/kasan.h | 7 -------
 arch/loongarch/mm/kasan_init.c     | 7 ++-----
 2 files changed, 2 insertions(+), 12 deletions(-)

diff --git a/arch/loongarch/include/asm/kasan.h b/arch/loongarch/include/asm/kasan.h
index 7f52bd31b9d..b0b74871257 100644
--- a/arch/loongarch/include/asm/kasan.h
+++ b/arch/loongarch/include/asm/kasan.h
@@ -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_mem
 const 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)
 {
diff --git a/arch/loongarch/mm/kasan_init.c b/arch/loongarch/mm/kasan_init.c
index d2681272d8f..cf8315f9119 100644
--- a/arch/loongarch/mm/kasan_init.c
+++ b/arch/loongarch/mm/kasan_init.c
@@ -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");
 }
-- 
2.34.1


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

* [PATCH v2 06/11] kasan/um: call kasan_init_generic in kasan_init
  2025-06-26 15:31 [PATCH v2 00/11] kasan: unify kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
                   ` (4 preceding siblings ...)
  2025-06-26 15:31 ` [PATCH v2 05/11] kasan/loongarch: " Sabyrzhan Tasbolatov
@ 2025-06-26 15:31 ` Sabyrzhan Tasbolatov
  2025-06-26 15:31 ` [PATCH v2 07/11] kasan/x86: " Sabyrzhan Tasbolatov
                   ` (6 subsequent siblings)
  12 siblings, 0 replies; 23+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-26 15:31 UTC (permalink / raw)
  To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	linux, catalin.marinas, will, chenhuacai, kernel, maddy, mpe,
	npiggin, christophe.leroy, paul.walmsley, palmer, aou, alex, hca,
	gor, agordeev, borntraeger, svens, richard, anton.ivanov,
	johannes, dave.hansen, luto, peterz, tglx, mingo, bp, x86, hpa,
	chris, jcmvbkbc, akpm, nathan, nick.desaulniers+lkml, morbo,
	justinstitt
  Cc: arnd, rppt, geert, mcgrof, guoweikang.kernel, tiwei.btw,
	kevin.brodsky, benjamin.berg, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-riscv, linux-s390,
	linux-um, linux-mm, llvm, snovitoll

Call 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.

Closes: https://bugzilla.kernel.org/show_bug.cgi?id=218315
Signed-off-by: Sabyrzhan Tasbolatov <snovitoll@gmail.com>
---
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(-)

diff --git a/arch/um/include/asm/kasan.h b/arch/um/include/asm/kasan.h
index f97bb1f7b85..81bcdc0f962 100644
--- a/arch/um/include/asm/kasan.h
+++ b/arch/um/include/asm/kasan.h
@@ -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 */
diff --git a/arch/um/kernel/mem.c b/arch/um/kernel/mem.c
index 76bec7de81b..058cb70e330 100644
--- a/arch/um/kernel/mem.c
+++ b/arch/um/kernel/mem.c
@@ -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)
-- 
2.34.1


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

* [PATCH v2 07/11] kasan/x86: call kasan_init_generic in kasan_init
  2025-06-26 15:31 [PATCH v2 00/11] kasan: unify kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
                   ` (5 preceding siblings ...)
  2025-06-26 15:31 ` [PATCH v2 06/11] kasan/um: " Sabyrzhan Tasbolatov
@ 2025-06-26 15:31 ` Sabyrzhan Tasbolatov
  2025-06-26 15:31 ` [PATCH v2 08/11] kasan/s390: " Sabyrzhan Tasbolatov
                   ` (5 subsequent siblings)
  12 siblings, 0 replies; 23+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-26 15:31 UTC (permalink / raw)
  To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	linux, catalin.marinas, will, chenhuacai, kernel, maddy, mpe,
	npiggin, christophe.leroy, paul.walmsley, palmer, aou, alex, hca,
	gor, agordeev, borntraeger, svens, richard, anton.ivanov,
	johannes, dave.hansen, luto, peterz, tglx, mingo, bp, x86, hpa,
	chris, jcmvbkbc, akpm, nathan, nick.desaulniers+lkml, morbo,
	justinstitt
  Cc: arnd, rppt, geert, mcgrof, guoweikang.kernel, tiwei.btw,
	kevin.brodsky, benjamin.berg, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-riscv, linux-s390,
	linux-um, linux-mm, llvm, snovitoll

Call 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.

Closes: https://bugzilla.kernel.org/show_bug.cgi?id=218315
Signed-off-by: Sabyrzhan Tasbolatov <snovitoll@gmail.com>
---
 arch/x86/mm/kasan_init_64.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/mm/kasan_init_64.c b/arch/x86/mm/kasan_init_64.c
index 0539efd0d21..998b6010d6d 100644
--- a/arch/x86/mm/kasan_init_64.c
+++ b/arch/x86/mm/kasan_init_64.c
@@ -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();
 }
-- 
2.34.1


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

* [PATCH v2 08/11] kasan/s390: call kasan_init_generic in kasan_init
  2025-06-26 15:31 [PATCH v2 00/11] kasan: unify kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
                   ` (6 preceding siblings ...)
  2025-06-26 15:31 ` [PATCH v2 07/11] kasan/x86: " Sabyrzhan Tasbolatov
@ 2025-06-26 15:31 ` Sabyrzhan Tasbolatov
  2025-06-26 15:31 ` [PATCH v2 09/11] kasan/powerpc: " Sabyrzhan Tasbolatov
                   ` (4 subsequent siblings)
  12 siblings, 0 replies; 23+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-26 15:31 UTC (permalink / raw)
  To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	linux, catalin.marinas, will, chenhuacai, kernel, maddy, mpe,
	npiggin, christophe.leroy, paul.walmsley, palmer, aou, alex, hca,
	gor, agordeev, borntraeger, svens, richard, anton.ivanov,
	johannes, dave.hansen, luto, peterz, tglx, mingo, bp, x86, hpa,
	chris, jcmvbkbc, akpm, nathan, nick.desaulniers+lkml, morbo,
	justinstitt
  Cc: arnd, rppt, geert, mcgrof, guoweikang.kernel, tiwei.btw,
	kevin.brodsky, benjamin.berg, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-riscv, linux-s390,
	linux-um, linux-mm, llvm, snovitoll

Call 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.

Closes: https://bugzilla.kernel.org/show_bug.cgi?id=218315
Signed-off-by: Sabyrzhan Tasbolatov <snovitoll@gmail.com>
---
 arch/s390/kernel/early.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/s390/kernel/early.c b/arch/s390/kernel/early.c
index 54cf0923050..7ada1324f6a 100644
--- a/arch/s390/kernel/early.c
+++ b/arch/s390/kernel/early.c
@@ -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
 }
 
-- 
2.34.1


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

* [PATCH v2 09/11] kasan/powerpc: call kasan_init_generic in kasan_init
  2025-06-26 15:31 [PATCH v2 00/11] kasan: unify kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
                   ` (7 preceding siblings ...)
  2025-06-26 15:31 ` [PATCH v2 08/11] kasan/s390: " Sabyrzhan Tasbolatov
@ 2025-06-26 15:31 ` Sabyrzhan Tasbolatov
  2025-06-26 15:31 ` [PATCH v2 10/11] kasan/riscv: " Sabyrzhan Tasbolatov
                   ` (3 subsequent siblings)
  12 siblings, 0 replies; 23+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-26 15:31 UTC (permalink / raw)
  To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	linux, catalin.marinas, will, chenhuacai, kernel, maddy, mpe,
	npiggin, christophe.leroy, paul.walmsley, palmer, aou, alex, hca,
	gor, agordeev, borntraeger, svens, richard, anton.ivanov,
	johannes, dave.hansen, luto, peterz, tglx, mingo, bp, x86, hpa,
	chris, jcmvbkbc, akpm, nathan, nick.desaulniers+lkml, morbo,
	justinstitt
  Cc: arnd, rppt, geert, mcgrof, guoweikang.kernel, tiwei.btw,
	kevin.brodsky, benjamin.berg, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-riscv, linux-s390,
	linux-um, linux-mm, llvm, snovitoll

Call 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.

Closes: https://bugzilla.kernel.org/show_bug.cgi?id=218315
Fixes: 55d77bae7342 ("kasan: fix Oops due to missing calls to kasan_arch_is_ready()")
Signed-off-by: Sabyrzhan Tasbolatov <snovitoll@gmail.com>
---
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(-)

diff --git a/arch/powerpc/include/asm/kasan.h b/arch/powerpc/include/asm/kasan.h
index b5bbb94c51f..73466d3ff30 100644
--- a/arch/powerpc/include/asm/kasan.h
+++ b/arch/powerpc/include/asm/kasan.h
@@ -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);
diff --git a/arch/powerpc/mm/kasan/init_32.c b/arch/powerpc/mm/kasan/init_32.c
index 03666d790a5..1d083597464 100644
--- a/arch/powerpc/mm/kasan/init_32.c
+++ b/arch/powerpc/mm/kasan/init_32.c
@@ -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)
diff --git a/arch/powerpc/mm/kasan/init_book3e_64.c b/arch/powerpc/mm/kasan/init_book3e_64.c
index 60c78aac0f6..0d3a73d6d4b 100644
--- a/arch/powerpc/mm/kasan/init_book3e_64.c
+++ b/arch/powerpc/mm/kasan/init_book3e_64.c
@@ -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) { }
diff --git a/arch/powerpc/mm/kasan/init_book3s_64.c b/arch/powerpc/mm/kasan/init_book3s_64.c
index 7d959544c07..dcafa641804 100644
--- a/arch/powerpc/mm/kasan/init_book3s_64.c
+++ b/arch/powerpc/mm/kasan/init_book3s_64.c
@@ -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) { }
-- 
2.34.1


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

* [PATCH v2 10/11] kasan/riscv: call kasan_init_generic in kasan_init
  2025-06-26 15:31 [PATCH v2 00/11] kasan: unify kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
                   ` (8 preceding siblings ...)
  2025-06-26 15:31 ` [PATCH v2 09/11] kasan/powerpc: " Sabyrzhan Tasbolatov
@ 2025-06-26 15:31 ` Sabyrzhan Tasbolatov
  2025-06-26 15:31 ` [PATCH v2 11/11] kasan: replace kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
                   ` (2 subsequent siblings)
  12 siblings, 0 replies; 23+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-26 15:31 UTC (permalink / raw)
  To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	linux, catalin.marinas, will, chenhuacai, kernel, maddy, mpe,
	npiggin, christophe.leroy, paul.walmsley, palmer, aou, alex, hca,
	gor, agordeev, borntraeger, svens, richard, anton.ivanov,
	johannes, dave.hansen, luto, peterz, tglx, mingo, bp, x86, hpa,
	chris, jcmvbkbc, akpm, nathan, nick.desaulniers+lkml, morbo,
	justinstitt
  Cc: arnd, rppt, geert, mcgrof, guoweikang.kernel, tiwei.btw,
	kevin.brodsky, benjamin.berg, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-riscv, linux-s390,
	linux-um, linux-mm, llvm, snovitoll

Call 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.

Closes: https://bugzilla.kernel.org/show_bug.cgi?id=218315
Signed-off-by: Sabyrzhan Tasbolatov <snovitoll@gmail.com>
---
 arch/riscv/mm/kasan_init.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/riscv/mm/kasan_init.c b/arch/riscv/mm/kasan_init.c
index 41c635d6aca..ba2709b1eec 100644
--- a/arch/riscv/mm/kasan_init.c
+++ b/arch/riscv/mm/kasan_init.c
@@ -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();
-- 
2.34.1


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

* [PATCH v2 11/11] kasan: replace kasan_arch_is_ready with kasan_enabled
  2025-06-26 15:31 [PATCH v2 00/11] kasan: unify kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
                   ` (9 preceding siblings ...)
  2025-06-26 15:31 ` [PATCH v2 10/11] kasan/riscv: " Sabyrzhan Tasbolatov
@ 2025-06-26 15:31 ` Sabyrzhan Tasbolatov
  2025-06-28 10:56 ` [PATCH v2 00/11] kasan: unify " Andrey Konovalov
  2025-06-28 11:26 ` Christophe Leroy
  12 siblings, 0 replies; 23+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-26 15:31 UTC (permalink / raw)
  To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	linux, catalin.marinas, will, chenhuacai, kernel, maddy, mpe,
	npiggin, christophe.leroy, paul.walmsley, palmer, aou, alex, hca,
	gor, agordeev, borntraeger, svens, richard, anton.ivanov,
	johannes, dave.hansen, luto, peterz, tglx, mingo, bp, x86, hpa,
	chris, jcmvbkbc, akpm, nathan, nick.desaulniers+lkml, morbo,
	justinstitt
  Cc: arnd, rppt, geert, mcgrof, guoweikang.kernel, tiwei.btw,
	kevin.brodsky, benjamin.berg, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-riscv, linux-s390,
	linux-um, linux-mm, llvm, snovitoll

Replace the existing kasan_arch_is_ready() calls with kasan_enabled().
Drop checks where the caller is already under kasan_enabled() condition.

Closes: https://bugzilla.kernel.org/show_bug.cgi?id=218315
Signed-off-by: Sabyrzhan Tasbolatov <snovitoll@gmail.com>
---
 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(-)

diff --git a/include/linux/kasan-enabled.h b/include/linux/kasan-enabled.h
index 2b1351c30c6..2436eb45cfe 100644
--- a/include/linux/kasan-enabled.h
+++ b/include/linux/kasan-enabled.h
@@ -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;
diff --git a/mm/kasan/common.c b/mm/kasan/common.c
index 525194da25f..0f3648335a6 100644
--- a/mm/kasan/common.c
+++ b/mm/kasan/common.c
@@ -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);
diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c
index ab9ab30caf4..af2f2077a45 100644
--- a/mm/kasan/generic.c
+++ b/mm/kasan/generic.c
@@ -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. */
diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h
index 129178be5e6..e0ffc16495d 100644
--- a/mm/kasan/kasan.h
+++ b/mm/kasan/kasan.h
@@ -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);
diff --git a/mm/kasan/shadow.c b/mm/kasan/shadow.c
index d2c70cd2afb..9db8548ccb4 100644
--- a/mm/kasan/shadow.c
+++ b/mm/kasan/shadow.c
@@ -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;
 
-- 
2.34.1


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

* Re: [PATCH v2 00/11] kasan: unify kasan_arch_is_ready with kasan_enabled
  2025-06-26 15:31 [PATCH v2 00/11] kasan: unify kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
                   ` (10 preceding siblings ...)
  2025-06-26 15:31 ` [PATCH v2 11/11] kasan: replace kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
@ 2025-06-28 10:56 ` Andrey Konovalov
  2025-06-28 13:25   ` Sabyrzhan Tasbolatov
  2025-06-28 11:26 ` Christophe Leroy
  12 siblings, 1 reply; 23+ messages in thread
From: Andrey Konovalov @ 2025-06-28 10:56 UTC (permalink / raw)
  To: Sabyrzhan Tasbolatov
  Cc: ryabinin.a.a, glider, dvyukov, vincenzo.frascino, linux,
	catalin.marinas, will, chenhuacai, kernel, maddy, mpe, npiggin,
	christophe.leroy, paul.walmsley, palmer, aou, alex, hca, gor,
	agordeev, borntraeger, svens, richard, anton.ivanov, johannes,
	dave.hansen, luto, peterz, tglx, mingo, bp, x86, hpa, chris,
	jcmvbkbc, akpm, nathan, nick.desaulniers+lkml, morbo, justinstitt,
	arnd, rppt, geert, mcgrof, guoweikang.kernel, tiwei.btw,
	kevin.brodsky, benjamin.berg, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-riscv, linux-s390,
	linux-um, linux-mm, llvm

On Thu, Jun 26, 2025 at 5:32 PM Sabyrzhan Tasbolatov
<snovitoll@gmail.com> wrote:
>
> 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

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]:

> In kasan_cache_create() we unconditionally allocate a metadata buffer,
> but the kasan_init_slab_obj() call to initialise it is guarded by
> kasan_enabled(). But later parts of the code only check the presence of
> the buffer before using it, so bad things happen if kasan_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/

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

* Re: [PATCH v2 00/11] kasan: unify kasan_arch_is_ready with kasan_enabled
  2025-06-26 15:31 [PATCH v2 00/11] kasan: unify kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
                   ` (11 preceding siblings ...)
  2025-06-28 10:56 ` [PATCH v2 00/11] kasan: unify " Andrey Konovalov
@ 2025-06-28 11:26 ` Christophe Leroy
  12 siblings, 0 replies; 23+ messages in thread
From: Christophe Leroy @ 2025-06-28 11:26 UTC (permalink / raw)
  To: Sabyrzhan Tasbolatov, ryabinin.a.a, glider, andreyknvl, dvyukov,
	vincenzo.frascino, linux, catalin.marinas, will, chenhuacai,
	kernel, maddy, mpe, npiggin, paul.walmsley, palmer, aou, alex,
	hca, gor, agordeev, borntraeger, svens, richard, anton.ivanov,
	johannes, dave.hansen, luto, peterz, tglx, mingo, bp, x86, hpa,
	chris, jcmvbkbc, akpm, nathan, nick.desaulniers+lkml, morbo,
	justinstitt
  Cc: arnd, rppt, geert, mcgrof, guoweikang.kernel, tiwei.btw,
	kevin.brodsky, benjamin.berg, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-riscv, linux-s390,
	linux-um, linux-mm, llvm



Le 26/06/2025 à 17:31, Sabyrzhan Tasbolatov a écrit :
> 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

You should also consider refactoring ARCH_DISABLE_KASAN_INLINE, there is 
a high dependency between deferring KASAN readiness and not supporting 
inline KASAN.

> 
> 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

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:

diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
index c3e0cc83f120..7a8e5db603cc 100644
--- a/arch/powerpc/Kconfig
+++ b/arch/powerpc/Kconfig
@@ -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
diff --git a/include/linux/kasan-enabled.h b/include/linux/kasan-enabled.h
index 2436eb45cfee..fda86e77fe4f 100644
--- a/include/linux/kasan-enabled.h
+++ b/include/linux/kasan-enabled.h
@@ -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
diff --git a/lib/Kconfig.kasan b/lib/Kconfig.kasan
index f82889a830fa..e0c300f55c07 100644
--- a/lib/Kconfig.kasan
+++ b/lib/Kconfig.kasan
@@ -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
diff --git a/mm/kasan/common.c b/mm/kasan/common.c
index 0f3648335a6b..01f56eed9d20 100644
--- a/mm/kasan/common.c
+++ b/mm/kasan/common.c
@@ -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)
  {
diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c
index a3b112868be7..516b49accc4f 100644
--- a/mm/kasan/generic.c
+++ b/mm/kasan/generic.c
@@ -42,7 +42,7 @@
   */
  void __init kasan_init_generic(void)
  {
-	static_branch_enable(&kasan_flag_enabled);
+	kasan_enable();

  	pr_info("KernelAddressSanitizer initialized (generic)\n");
  }
diff --git a/mm/kasan/hw_tags.c b/mm/kasan/hw_tags.c
index 8e819fc4a260..c8289a3feabf 100644
--- a/mm/kasan/hw_tags.c
+++ b/mm/kasan/hw_tags.c
@@ -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(),
diff --git a/mm/kasan/sw_tags.c b/mm/kasan/sw_tags.c
index 525bc91e2fcd..275bcbbf6120 100644
--- a/mm/kasan/sw_tags.c
+++ b/mm/kasan/sw_tags.c
@@ -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()));


> 
> 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' of
> git://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(-)
> 


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

* Re: [PATCH v2 00/11] kasan: unify kasan_arch_is_ready with kasan_enabled
  2025-06-28 10:56 ` [PATCH v2 00/11] kasan: unify " Andrey Konovalov
@ 2025-06-28 13:25   ` Sabyrzhan Tasbolatov
  2025-06-29 20:05     ` Andrey Konovalov
  0 siblings, 1 reply; 23+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-28 13:25 UTC (permalink / raw)
  To: Andrey Konovalov
  Cc: ryabinin.a.a, glider, dvyukov, vincenzo.frascino, linux,
	catalin.marinas, will, chenhuacai, kernel, maddy, mpe, npiggin,
	christophe.leroy, paul.walmsley, palmer, aou, alex, hca, gor,
	agordeev, borntraeger, svens, richard, anton.ivanov, johannes,
	dave.hansen, luto, peterz, tglx, mingo, bp, x86, hpa, chris,
	jcmvbkbc, akpm, nathan, nick.desaulniers+lkml, morbo, justinstitt,
	arnd, rppt, geert, mcgrof, guoweikang.kernel, tiwei.btw,
	kevin.brodsky, benjamin.berg, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-riscv, linux-s390,
	linux-um, linux-mm, llvm

On Sat, Jun 28, 2025 at 3:57 PM Andrey Konovalov <andreyknvl@gmail.com> wrote:
>
> On Thu, Jun 26, 2025 at 5:32 PM Sabyrzhan Tasbolatov
> <snovitoll@gmail.com> wrote:
> >
> > 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
>
> 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]:

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?

>
> > In kasan_cache_create() we unconditionally allocate a metadata buffer,
> > but the kasan_init_slab_obj() call to initialise it is guarded by
> > kasan_enabled(). But later parts of the code only check the presence of
> > the buffer before using it, so bad things happen if kasan_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/

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

* Re: [PATCH v2 00/11] kasan: unify kasan_arch_is_ready with kasan_enabled
  2025-06-28 13:25   ` Sabyrzhan Tasbolatov
@ 2025-06-29 20:05     ` Andrey Konovalov
  2025-07-01 10:15       ` Heiko Carstens
  0 siblings, 1 reply; 23+ messages in thread
From: Andrey Konovalov @ 2025-06-29 20:05 UTC (permalink / raw)
  To: Sabyrzhan Tasbolatov
  Cc: ryabinin.a.a, glider, dvyukov, vincenzo.frascino, linux,
	catalin.marinas, will, chenhuacai, kernel, maddy, mpe, npiggin,
	christophe.leroy, paul.walmsley, palmer, aou, alex, hca, gor,
	agordeev, borntraeger, svens, richard, anton.ivanov, johannes,
	dave.hansen, luto, peterz, tglx, mingo, bp, x86, hpa, chris,
	jcmvbkbc, akpm, nathan, nick.desaulniers+lkml, morbo, justinstitt,
	arnd, rppt, geert, mcgrof, guoweikang.kernel, tiwei.btw,
	kevin.brodsky, benjamin.berg, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-riscv, linux-s390,
	linux-um, linux-mm, llvm

On Sat, Jun 28, 2025 at 3:25 PM Sabyrzhan Tasbolatov
<snovitoll@gmail.com> wrote:
>
> On Sat, Jun 28, 2025 at 3:57 PM Andrey Konovalov <andreyknvl@gmail.com> wrote:
> >
> > On Thu, Jun 26, 2025 at 5:32 PM Sabyrzhan Tasbolatov
> > <snovitoll@gmail.com> wrote:
> > >
> > > 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
> >
> > 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]:
>
> 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?

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/

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

* Re: [PATCH v2 01/11] kasan: unify static kasan_flag_enabled across modes
  2025-06-26 15:31 ` [PATCH v2 01/11] kasan: unify static kasan_flag_enabled across modes Sabyrzhan Tasbolatov
@ 2025-06-30 12:31   ` Alexander Gordeev
  2025-06-30 14:39     ` Heiko Carstens
  2025-07-01  0:05     ` Andrew Morton
  0 siblings, 2 replies; 23+ messages in thread
From: Alexander Gordeev @ 2025-06-30 12:31 UTC (permalink / raw)
  To: Sabyrzhan Tasbolatov
  Cc: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	linux, catalin.marinas, will, chenhuacai, kernel, maddy, mpe,
	npiggin, christophe.leroy, paul.walmsley, palmer, aou, alex, hca,
	gor, borntraeger, svens, richard, anton.ivanov, johannes,
	dave.hansen, luto, peterz, tglx, mingo, bp, x86, hpa, chris,
	jcmvbkbc, akpm, nathan, nick.desaulniers+lkml, morbo, justinstitt,
	arnd, rppt, geert, mcgrof, guoweikang.kernel, tiwei.btw,
	kevin.brodsky, benjamin.berg, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-riscv, linux-s390,
	linux-um, linux-mm, llvm

On Thu, Jun 26, 2025 at 08:31:37PM +0500, Sabyrzhan Tasbolatov wrote:

Hi Sabyrzhan,

> diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c
> index d54e89f8c3e..32c432df24a 100644
> --- a/mm/kasan/generic.c
> +++ b/mm/kasan/generic.c
> @@ -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>

> +	pr_info("KernelAddressSanitizer initialized (generic)\n");
> +}

Thanks!

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

* Re: [PATCH v2 01/11] kasan: unify static kasan_flag_enabled across modes
  2025-06-30 12:31   ` Alexander Gordeev
@ 2025-06-30 14:39     ` Heiko Carstens
  2025-06-30 15:23       ` Alexander Gordeev
  2025-07-01  0:05     ` Andrew Morton
  1 sibling, 1 reply; 23+ messages in thread
From: Heiko Carstens @ 2025-06-30 14:39 UTC (permalink / raw)
  To: Alexander Gordeev, Vasily Gorbik
  Cc: Sabyrzhan Tasbolatov, ryabinin.a.a, glider, andreyknvl, dvyukov,
	vincenzo.frascino, linux, catalin.marinas, will, chenhuacai,
	kernel, maddy, mpe, npiggin, christophe.leroy, paul.walmsley,
	palmer, aou, alex, gor, borntraeger, svens, richard, anton.ivanov,
	johannes, dave.hansen, luto, peterz, tglx, mingo, bp, x86, hpa,
	chris, jcmvbkbc, akpm, nathan, nick.desaulniers+lkml, morbo,
	justinstitt, arnd, rppt, geert, mcgrof, guoweikang.kernel,
	tiwei.btw, kevin.brodsky, benjamin.berg, kasan-dev,
	linux-arm-kernel, linux-kernel, loongarch, linuxppc-dev,
	linux-riscv, linux-s390, linux-um, linux-mm, llvm

On Mon, Jun 30, 2025 at 02:31:00PM +0200, Alexander Gordeev wrote:
> On Thu, Jun 26, 2025 at 08:31:37PM +0500, Sabyrzhan Tasbolatov wrote:
> 
> Hi Sabyrzhan,
> 
> > diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c
> > index d54e89f8c3e..32c432df24a 100644
> > --- a/mm/kasan/generic.c
> > +++ b/mm/kasan/generic.c
> > @@ -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?

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?

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

* Re: [PATCH v2 01/11] kasan: unify static kasan_flag_enabled across modes
  2025-06-30 14:39     ` Heiko Carstens
@ 2025-06-30 15:23       ` Alexander Gordeev
  0 siblings, 0 replies; 23+ messages in thread
From: Alexander Gordeev @ 2025-06-30 15:23 UTC (permalink / raw)
  To: Heiko Carstens
  Cc: Vasily Gorbik, Sabyrzhan Tasbolatov, ryabinin.a.a, glider,
	andreyknvl, dvyukov, vincenzo.frascino, linux, catalin.marinas,
	will, chenhuacai, kernel, maddy, mpe, npiggin, christophe.leroy,
	paul.walmsley, palmer, aou, alex, borntraeger, svens, richard,
	anton.ivanov, johannes, dave.hansen, luto, peterz, tglx, mingo,
	bp, x86, hpa, chris, jcmvbkbc, akpm, nathan,
	nick.desaulniers+lkml, morbo, justinstitt, arnd, rppt, geert,
	mcgrof, guoweikang.kernel, tiwei.btw, kevin.brodsky,
	benjamin.berg, kasan-dev, linux-arm-kernel, linux-kernel,
	loongarch, linuxppc-dev, linux-riscv, linux-s390, linux-um,
	linux-mm, llvm

On Mon, Jun 30, 2025 at 04:39:34PM +0200, Heiko Carstens wrote:
> > > +/*
> > > + * 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?
> 
> 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.

IIRC, we wanted to have kasan coverage as early as possible.
Delaying it past jump_label_init() leaves out pretty big chunk of code?

> Vasily, since you did nearly all of the KASAN work for s390, do you have any
> opinion about this?

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

* Re: [PATCH v2 01/11] kasan: unify static kasan_flag_enabled across modes
  2025-06-30 12:31   ` Alexander Gordeev
  2025-06-30 14:39     ` Heiko Carstens
@ 2025-07-01  0:05     ` Andrew Morton
  1 sibling, 0 replies; 23+ messages in thread
From: Andrew Morton @ 2025-07-01  0:05 UTC (permalink / raw)
  To: Alexander Gordeev
  Cc: Sabyrzhan Tasbolatov, ryabinin.a.a, glider, andreyknvl, dvyukov,
	vincenzo.frascino, linux, catalin.marinas, will, chenhuacai,
	kernel, maddy, mpe, npiggin, christophe.leroy, paul.walmsley,
	palmer, aou, alex, hca, gor, borntraeger, svens, richard,
	anton.ivanov, johannes, dave.hansen, luto, peterz, tglx, mingo,
	bp, x86, hpa, chris, jcmvbkbc, nathan, nick.desaulniers+lkml,
	morbo, justinstitt, arnd, rppt, geert, mcgrof, guoweikang.kernel,
	tiwei.btw, kevin.brodsky, benjamin.berg, kasan-dev,
	linux-arm-kernel, linux-kernel, loongarch, linuxppc-dev,
	linux-riscv, linux-s390, linux-um, linux-mm, llvm

On Mon, 30 Jun 2025 14:31:00 +0200 Alexander Gordeev <agordeev@linux.ibm.com> wrote:

> > +/*
> > + * 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.

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

* Re: [PATCH v2 00/11] kasan: unify kasan_arch_is_ready with kasan_enabled
  2025-06-29 20:05     ` Andrey Konovalov
@ 2025-07-01 10:15       ` Heiko Carstens
  2025-07-01 10:25         ` Christophe Leroy
  0 siblings, 1 reply; 23+ messages in thread
From: Heiko Carstens @ 2025-07-01 10:15 UTC (permalink / raw)
  To: Andrey Konovalov
  Cc: Sabyrzhan Tasbolatov, ryabinin.a.a, glider, dvyukov,
	vincenzo.frascino, linux, catalin.marinas, will, chenhuacai,
	kernel, maddy, mpe, npiggin, christophe.leroy, paul.walmsley,
	palmer, aou, alex, gor, agordeev, borntraeger, svens, richard,
	anton.ivanov, johannes, dave.hansen, luto, peterz, tglx, mingo,
	bp, x86, hpa, chris, jcmvbkbc, akpm, nathan,
	nick.desaulniers+lkml, morbo, justinstitt, arnd, rppt, geert,
	mcgrof, guoweikang.kernel, tiwei.btw, kevin.brodsky,
	benjamin.berg, kasan-dev, linux-arm-kernel, linux-kernel,
	loongarch, linuxppc-dev, linux-riscv, linux-s390, linux-um,
	linux-mm, llvm

> > > 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]:
> >
> > 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?
> 
> 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.

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 :)

diff --git a/arch/s390/Kconfig b/arch/s390/Kconfig
index 0c16dc443e2f..c2f51ac39a91 100644
--- a/arch/s390/Kconfig
+++ b/arch/s390/Kconfig
@@ -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
diff --git a/include/linux/kasan-enabled.h b/include/linux/kasan-enabled.h
index 2436eb45cfee..049270a2269f 100644
--- a/include/linux/kasan-enabled.h
+++ b/include/linux/kasan-enabled.h
@@ -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)
 {
diff --git a/lib/Kconfig.kasan b/lib/Kconfig.kasan
index f82889a830fa..1407374e83b9 100644
--- a/lib/Kconfig.kasan
+++ b/lib/Kconfig.kasan
@@ -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
 
diff --git a/mm/kasan/common.c b/mm/kasan/common.c
index 0f3648335a6b..2aae0ce659b4 100644
--- a/mm/kasan/common.c
+++ b/mm/kasan/common.c
@@ -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)
diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c
index a3b112868be7..455376d5f1c3 100644
--- a/mm/kasan/generic.c
+++ b/mm/kasan/generic.c
@@ -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");
 }

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

* Re: [PATCH v2 00/11] kasan: unify kasan_arch_is_ready with kasan_enabled
  2025-07-01 10:15       ` Heiko Carstens
@ 2025-07-01 10:25         ` Christophe Leroy
  2025-07-01 13:37           ` Heiko Carstens
  0 siblings, 1 reply; 23+ messages in thread
From: Christophe Leroy @ 2025-07-01 10:25 UTC (permalink / raw)
  To: Heiko Carstens, Andrey Konovalov
  Cc: Sabyrzhan Tasbolatov, ryabinin.a.a, glider, dvyukov,
	vincenzo.frascino, linux, catalin.marinas, will, chenhuacai,
	kernel, maddy, mpe, npiggin, paul.walmsley, palmer, aou, alex,
	gor, agordeev, borntraeger, svens, richard, anton.ivanov,
	johannes, dave.hansen, luto, peterz, tglx, mingo, bp, x86, hpa,
	chris, jcmvbkbc, akpm, nathan, nick.desaulniers+lkml, morbo,
	justinstitt, arnd, rppt, geert, mcgrof, guoweikang.kernel,
	tiwei.btw, kevin.brodsky, benjamin.berg, kasan-dev,
	linux-arm-kernel, linux-kernel, loongarch, linuxppc-dev,
	linux-riscv, linux-s390, linux-um, linux-mm, llvm



Le 01/07/2025 à 12:15, Heiko Carstens a écrit :
>>>> 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]:
>>>
>>> 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?
>>
>> 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.
> 
> 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 :)

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/20250626153147.145312-1-snovitoll@gmail.com/#3537388 
. Feedback welcome.

Christophe

> 
> diff --git a/arch/s390/Kconfig b/arch/s390/Kconfig
> index 0c16dc443e2f..c2f51ac39a91 100644
> --- a/arch/s390/Kconfig
> +++ b/arch/s390/Kconfig
> @@ -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
> diff --git a/include/linux/kasan-enabled.h b/include/linux/kasan-enabled.h
> index 2436eb45cfee..049270a2269f 100644
> --- a/include/linux/kasan-enabled.h
> +++ b/include/linux/kasan-enabled.h
> @@ -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)
>   {
> diff --git a/lib/Kconfig.kasan b/lib/Kconfig.kasan
> index f82889a830fa..1407374e83b9 100644
> --- a/lib/Kconfig.kasan
> +++ b/lib/Kconfig.kasan
> @@ -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
>   
> diff --git a/mm/kasan/common.c b/mm/kasan/common.c
> index 0f3648335a6b..2aae0ce659b4 100644
> --- a/mm/kasan/common.c
> +++ b/mm/kasan/common.c
> @@ -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)
> diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c
> index a3b112868be7..455376d5f1c3 100644
> --- a/mm/kasan/generic.c
> +++ b/mm/kasan/generic.c
> @@ -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");
>   }


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

* Re: [PATCH v2 00/11] kasan: unify kasan_arch_is_ready with kasan_enabled
  2025-07-01 10:25         ` Christophe Leroy
@ 2025-07-01 13:37           ` Heiko Carstens
  0 siblings, 0 replies; 23+ messages in thread
From: Heiko Carstens @ 2025-07-01 13:37 UTC (permalink / raw)
  To: Christophe Leroy
  Cc: Andrey Konovalov, Sabyrzhan Tasbolatov, ryabinin.a.a, glider,
	dvyukov, vincenzo.frascino, linux, catalin.marinas, will,
	chenhuacai, kernel, maddy, mpe, npiggin, paul.walmsley, palmer,
	aou, alex, gor, agordeev, borntraeger, svens, richard,
	anton.ivanov, johannes, dave.hansen, luto, peterz, tglx, mingo,
	bp, x86, hpa, chris, jcmvbkbc, akpm, nathan,
	nick.desaulniers+lkml, morbo, justinstitt, arnd, rppt, geert,
	mcgrof, guoweikang.kernel, tiwei.btw, kevin.brodsky,
	benjamin.berg, kasan-dev, linux-arm-kernel, linux-kernel,
	loongarch, linuxppc-dev, linux-riscv, linux-s390, linux-um,
	linux-mm, llvm

On Tue, Jul 01, 2025 at 12:25:32PM +0200, Christophe Leroy wrote:
> 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/20250626153147.145312-1-snovitoll@gmail.com/#3537388
> . Feedback welcome.

Yes, removing the static branches during compile time is of course
even better.

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

end of thread, other threads:[~2025-07-01 13:38 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-26 15:31 [PATCH v2 00/11] kasan: unify kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
2025-06-26 15:31 ` [PATCH v2 01/11] kasan: unify static kasan_flag_enabled across modes Sabyrzhan Tasbolatov
2025-06-30 12:31   ` Alexander Gordeev
2025-06-30 14:39     ` Heiko Carstens
2025-06-30 15:23       ` Alexander Gordeev
2025-07-01  0:05     ` Andrew Morton
2025-06-26 15:31 ` [PATCH v2 02/11] kasan/arm64: call kasan_init_generic in kasan_init Sabyrzhan Tasbolatov
2025-06-26 15:31 ` [PATCH v2 03/11] kasan/arm: " Sabyrzhan Tasbolatov
2025-06-26 15:31 ` [PATCH v2 04/11] kasan/xtensa: " Sabyrzhan Tasbolatov
2025-06-26 15:31 ` [PATCH v2 05/11] kasan/loongarch: " Sabyrzhan Tasbolatov
2025-06-26 15:31 ` [PATCH v2 06/11] kasan/um: " Sabyrzhan Tasbolatov
2025-06-26 15:31 ` [PATCH v2 07/11] kasan/x86: " Sabyrzhan Tasbolatov
2025-06-26 15:31 ` [PATCH v2 08/11] kasan/s390: " Sabyrzhan Tasbolatov
2025-06-26 15:31 ` [PATCH v2 09/11] kasan/powerpc: " Sabyrzhan Tasbolatov
2025-06-26 15:31 ` [PATCH v2 10/11] kasan/riscv: " Sabyrzhan Tasbolatov
2025-06-26 15:31 ` [PATCH v2 11/11] kasan: replace kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
2025-06-28 10:56 ` [PATCH v2 00/11] kasan: unify " Andrey Konovalov
2025-06-28 13:25   ` Sabyrzhan Tasbolatov
2025-06-29 20:05     ` Andrey Konovalov
2025-07-01 10:15       ` Heiko Carstens
2025-07-01 10:25         ` Christophe Leroy
2025-07-01 13:37           ` Heiko Carstens
2025-06-28 11:26 ` Christophe Leroy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).