linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/9] kasan: unify kasan_arch_is_ready with kasan_enabled
@ 2025-06-25  9:52 Sabyrzhan Tasbolatov
  2025-06-25  9:52 ` [PATCH 1/9] kasan: unify static kasan_flag_enabled across modes Sabyrzhan Tasbolatov
                   ` (8 more replies)
  0 siblings, 9 replies; 21+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-25  9:52 UTC (permalink / raw)
  To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	catalin.marinas, will, chenhuacai, kernel, maddy, mpe, npiggin,
	christophe.leroy, hca, gor, agordeev, borntraeger, svens, richard,
	anton.ivanov, johannes, dave.hansen, luto, peterz, tglx, mingo,
	bp, x86, hpa, chris, jcmvbkbc, akpm
  Cc: guoweikang.kernel, geert, rppt, tiwei.btw, richard.weiyang,
	benjamin.berg, kevin.brodsky, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-s390, linux-um,
	linux-mm, 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 the patches:

* arm64  (GENERIC, HW_TAGS, SW_TAGS): no regression, same above results.
* x86_64 (GENERIC): no regression, no errors

=== NB

I haven't tested 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

Sabyrzhan Tasbolatov (9):
  kasan: unify static kasan_flag_enabled across modes
  kasan: replace kasan_arch_is_ready with kasan_enabled
  kasan/arm64: 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

 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       | 14 --------------
 arch/powerpc/mm/kasan/init_book3s_64.c |  6 +-----
 arch/s390/kernel/early.c               |  2 +-
 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 ++
 18 files changed, 61 insertions(+), 82 deletions(-)

-- 
2.34.1



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

* [PATCH 1/9] kasan: unify static kasan_flag_enabled across modes
  2025-06-25  9:52 [PATCH 0/9] kasan: unify kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
@ 2025-06-25  9:52 ` Sabyrzhan Tasbolatov
  2025-06-25 10:35   ` Christophe Leroy
  2025-06-25  9:52 ` [PATCH 2/9] kasan: replace kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
                   ` (7 subsequent siblings)
  8 siblings, 1 reply; 21+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-25  9:52 UTC (permalink / raw)
  To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	catalin.marinas, will, chenhuacai, kernel, maddy, mpe, npiggin,
	christophe.leroy, hca, gor, agordeev, borntraeger, svens, richard,
	anton.ivanov, johannes, dave.hansen, luto, peterz, tglx, mingo,
	bp, x86, hpa, chris, jcmvbkbc, akpm
  Cc: guoweikang.kernel, geert, rppt, tiwei.btw, richard.weiyang,
	benjamin.berg, kevin.brodsky, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-s390, linux-um,
	linux-mm, snovitoll

Historically the fast-path static key `kasan_flag_enabled` existed
only for `CONFIG_KASAN_HW_TAGS`. Generic and SW_TAGS either relied on
`kasan_arch_is_ready()` or evaluated KASAN checks unconditionally.
As a result every architecture had to toggle a private flag
in its `kasan_init()`.

This patch turns the flag into a single global runtime predicate that
is built for every `CONFIG_KASAN` mode and adds a helper that flips
the key once KASAN is ready.

Closes: https://bugzilla.kernel.org/show_bug.cgi?id=218315
Signed-off-by: Sabyrzhan Tasbolatov <snovitoll@gmail.com>
---
 include/linux/kasan-enabled.h | 22 ++++++++++++++++------
 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, 42 insertions(+), 13 deletions(-)

diff --git a/include/linux/kasan-enabled.h b/include/linux/kasan-enabled.h
index 6f612d69ea0c..2436eb45cfee 100644
--- a/include/linux/kasan-enabled.h
+++ b/include/linux/kasan-enabled.h
@@ -4,8 +4,12 @@
 
 #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);
 
 static __always_inline bool kasan_enabled(void)
@@ -13,18 +17,24 @@ 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 */
 
-static inline bool kasan_enabled(void)
+#ifdef CONFIG_KASAN_HW_TAGS
+
+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/include/linux/kasan.h b/include/linux/kasan.h
index 890011071f2b..51a8293d1af6 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 ed4873e18c75..525194da25fa 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 d54e89f8c3e7..32c432df24aa 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 9a6927394b54..8e819fc4a260 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 b9382b5b6a37..525bc91e2fcd 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] 21+ messages in thread

* [PATCH 2/9] kasan: replace kasan_arch_is_ready with kasan_enabled
  2025-06-25  9:52 [PATCH 0/9] kasan: unify kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
  2025-06-25  9:52 ` [PATCH 1/9] kasan: unify static kasan_flag_enabled across modes Sabyrzhan Tasbolatov
@ 2025-06-25  9:52 ` Sabyrzhan Tasbolatov
  2025-06-25 10:27   ` Christophe Leroy
  2025-06-25  9:52 ` [PATCH 3/9] kasan/arm64: call kasan_init_generic in kasan_init Sabyrzhan Tasbolatov
                   ` (6 subsequent siblings)
  8 siblings, 1 reply; 21+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-25  9:52 UTC (permalink / raw)
  To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	catalin.marinas, will, chenhuacai, kernel, maddy, mpe, npiggin,
	christophe.leroy, hca, gor, agordeev, borntraeger, svens, richard,
	anton.ivanov, johannes, dave.hansen, luto, peterz, tglx, mingo,
	bp, x86, hpa, chris, jcmvbkbc, akpm
  Cc: guoweikang.kernel, geert, rppt, tiwei.btw, richard.weiyang,
	benjamin.berg, kevin.brodsky, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-s390, linux-um,
	linux-mm, 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>
---
 mm/kasan/common.c  |  8 ++++----
 mm/kasan/generic.c |  6 +++---
 mm/kasan/kasan.h   |  6 ------
 mm/kasan/shadow.c  | 15 +++------------
 4 files changed, 10 insertions(+), 25 deletions(-)

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] 21+ messages in thread

* [PATCH 3/9] kasan/arm64: call kasan_init_generic in kasan_init
  2025-06-25  9:52 [PATCH 0/9] kasan: unify kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
  2025-06-25  9:52 ` [PATCH 1/9] kasan: unify static kasan_flag_enabled across modes Sabyrzhan Tasbolatov
  2025-06-25  9:52 ` [PATCH 2/9] kasan: replace kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
@ 2025-06-25  9:52 ` Sabyrzhan Tasbolatov
  2025-06-25  9:52 ` [PATCH 4/9] kasan/xtensa: " Sabyrzhan Tasbolatov
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 21+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-25  9:52 UTC (permalink / raw)
  To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	catalin.marinas, will, chenhuacai, kernel, maddy, mpe, npiggin,
	christophe.leroy, hca, gor, agordeev, borntraeger, svens, richard,
	anton.ivanov, johannes, dave.hansen, luto, peterz, tglx, mingo,
	bp, x86, hpa, chris, jcmvbkbc, akpm
  Cc: guoweikang.kernel, geert, rppt, tiwei.btw, richard.weiyang,
	benjamin.berg, kevin.brodsky, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-s390, linux-um,
	linux-mm, 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] 21+ messages in thread

* [PATCH 4/9] kasan/xtensa: call kasan_init_generic in kasan_init
  2025-06-25  9:52 [PATCH 0/9] kasan: unify kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
                   ` (2 preceding siblings ...)
  2025-06-25  9:52 ` [PATCH 3/9] kasan/arm64: call kasan_init_generic in kasan_init Sabyrzhan Tasbolatov
@ 2025-06-25  9:52 ` Sabyrzhan Tasbolatov
  2025-06-25  9:52 ` [PATCH 5/9] kasan/loongarch: " Sabyrzhan Tasbolatov
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 21+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-25  9:52 UTC (permalink / raw)
  To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	catalin.marinas, will, chenhuacai, kernel, maddy, mpe, npiggin,
	christophe.leroy, hca, gor, agordeev, borntraeger, svens, richard,
	anton.ivanov, johannes, dave.hansen, luto, peterz, tglx, mingo,
	bp, x86, hpa, chris, jcmvbkbc, akpm
  Cc: guoweikang.kernel, geert, rppt, tiwei.btw, richard.weiyang,
	benjamin.berg, kevin.brodsky, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-s390, linux-um,
	linux-mm, snovitoll

Call kasan_init_generic() which enables the static flag
to mark generic KASAN initialized, 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/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] 21+ messages in thread

* [PATCH 5/9] kasan/loongarch: call kasan_init_generic in kasan_init
  2025-06-25  9:52 [PATCH 0/9] kasan: unify kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
                   ` (3 preceding siblings ...)
  2025-06-25  9:52 ` [PATCH 4/9] kasan/xtensa: " Sabyrzhan Tasbolatov
@ 2025-06-25  9:52 ` Sabyrzhan Tasbolatov
  2025-06-26 13:29   ` Peter Zijlstra
  2025-06-25  9:52 ` [PATCH 6/9] kasan/um: " Sabyrzhan Tasbolatov
                   ` (3 subsequent siblings)
  8 siblings, 1 reply; 21+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-25  9:52 UTC (permalink / raw)
  To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	catalin.marinas, will, chenhuacai, kernel, maddy, mpe, npiggin,
	christophe.leroy, hca, gor, agordeev, borntraeger, svens, richard,
	anton.ivanov, johannes, dave.hansen, luto, peterz, tglx, mingo,
	bp, x86, hpa, chris, jcmvbkbc, akpm
  Cc: guoweikang.kernel, geert, rppt, tiwei.btw, richard.weiyang,
	benjamin.berg, kevin.brodsky, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-s390, linux-um,
	linux-mm, 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.

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] 21+ messages in thread

* [PATCH 6/9] kasan/um: call kasan_init_generic in kasan_init
  2025-06-25  9:52 [PATCH 0/9] kasan: unify kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
                   ` (4 preceding siblings ...)
  2025-06-25  9:52 ` [PATCH 5/9] kasan/loongarch: " Sabyrzhan Tasbolatov
@ 2025-06-25  9:52 ` Sabyrzhan Tasbolatov
  2025-06-25 12:20   ` Johannes Berg
  2025-06-25  9:52 ` [PATCH 7/9] kasan/x86: " Sabyrzhan Tasbolatov
                   ` (2 subsequent siblings)
  8 siblings, 1 reply; 21+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-25  9:52 UTC (permalink / raw)
  To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	catalin.marinas, will, chenhuacai, kernel, maddy, mpe, npiggin,
	christophe.leroy, hca, gor, agordeev, borntraeger, svens, richard,
	anton.ivanov, johannes, dave.hansen, luto, peterz, tglx, mingo,
	bp, x86, hpa, chris, jcmvbkbc, akpm
  Cc: guoweikang.kernel, geert, rppt, tiwei.btw, richard.weiyang,
	benjamin.berg, kevin.brodsky, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-s390, linux-um,
	linux-mm, 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().

Closes: https://bugzilla.kernel.org/show_bug.cgi?id=218315
Signed-off-by: Sabyrzhan Tasbolatov <snovitoll@gmail.com>
---
 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..2632269d530 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-enabled.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] 21+ messages in thread

* [PATCH 7/9] kasan/x86: call kasan_init_generic in kasan_init
  2025-06-25  9:52 [PATCH 0/9] kasan: unify kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
                   ` (5 preceding siblings ...)
  2025-06-25  9:52 ` [PATCH 6/9] kasan/um: " Sabyrzhan Tasbolatov
@ 2025-06-25  9:52 ` Sabyrzhan Tasbolatov
  2025-06-25  9:52 ` [PATCH 8/9] kasan/s390: " Sabyrzhan Tasbolatov
  2025-06-25  9:52 ` [PATCH 9/9] kasan/powerpc: " Sabyrzhan Tasbolatov
  8 siblings, 0 replies; 21+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-25  9:52 UTC (permalink / raw)
  To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	catalin.marinas, will, chenhuacai, kernel, maddy, mpe, npiggin,
	christophe.leroy, hca, gor, agordeev, borntraeger, svens, richard,
	anton.ivanov, johannes, dave.hansen, luto, peterz, tglx, mingo,
	bp, x86, hpa, chris, jcmvbkbc, akpm
  Cc: guoweikang.kernel, geert, rppt, tiwei.btw, richard.weiyang,
	benjamin.berg, kevin.brodsky, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-s390, linux-um,
	linux-mm, 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] 21+ messages in thread

* [PATCH 8/9] kasan/s390: call kasan_init_generic in kasan_init
  2025-06-25  9:52 [PATCH 0/9] kasan: unify kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
                   ` (6 preceding siblings ...)
  2025-06-25  9:52 ` [PATCH 7/9] kasan/x86: " Sabyrzhan Tasbolatov
@ 2025-06-25  9:52 ` Sabyrzhan Tasbolatov
  2025-06-25  9:52 ` [PATCH 9/9] kasan/powerpc: " Sabyrzhan Tasbolatov
  8 siblings, 0 replies; 21+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-25  9:52 UTC (permalink / raw)
  To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	catalin.marinas, will, chenhuacai, kernel, maddy, mpe, npiggin,
	christophe.leroy, hca, gor, agordeev, borntraeger, svens, richard,
	anton.ivanov, johannes, dave.hansen, luto, peterz, tglx, mingo,
	bp, x86, hpa, chris, jcmvbkbc, akpm
  Cc: guoweikang.kernel, geert, rppt, tiwei.btw, richard.weiyang,
	benjamin.berg, kevin.brodsky, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-s390, linux-um,
	linux-mm, 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 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/s390/kernel/early.c b/arch/s390/kernel/early.c
index 54cf0923050..da7a13d9ab7 100644
--- a/arch/s390/kernel/early.c
+++ b/arch/s390/kernel/early.c
@@ -65,7 +65,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] 21+ messages in thread

* [PATCH 9/9] kasan/powerpc: call kasan_init_generic in kasan_init
  2025-06-25  9:52 [PATCH 0/9] kasan: unify kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
                   ` (7 preceding siblings ...)
  2025-06-25  9:52 ` [PATCH 8/9] kasan/s390: " Sabyrzhan Tasbolatov
@ 2025-06-25  9:52 ` Sabyrzhan Tasbolatov
  2025-06-25 10:33   ` Christophe Leroy
  2025-06-26 10:52   ` Christophe Leroy
  8 siblings, 2 replies; 21+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-25  9:52 UTC (permalink / raw)
  To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	catalin.marinas, will, chenhuacai, kernel, maddy, mpe, npiggin,
	christophe.leroy, hca, gor, agordeev, borntraeger, svens, richard,
	anton.ivanov, johannes, dave.hansen, luto, peterz, tglx, mingo,
	bp, x86, hpa, chris, jcmvbkbc, akpm
  Cc: guoweikang.kernel, geert, rppt, tiwei.btw, richard.weiyang,
	benjamin.berg, kevin.brodsky, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-s390, linux-um,
	linux-mm, 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>
---
 arch/powerpc/include/asm/kasan.h       | 14 --------------
 arch/powerpc/mm/kasan/init_book3s_64.c |  6 +-----
 2 files changed, 1 insertion(+), 19 deletions(-)

diff --git a/arch/powerpc/include/asm/kasan.h b/arch/powerpc/include/asm/kasan.h
index b5bbb94c51f..23a06fbec72 100644
--- a/arch/powerpc/include/asm/kasan.h
+++ b/arch/powerpc/include/asm/kasan.h
@@ -52,20 +52,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_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] 21+ messages in thread

* Re: [PATCH 2/9] kasan: replace kasan_arch_is_ready with kasan_enabled
  2025-06-25  9:52 ` [PATCH 2/9] kasan: replace kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
@ 2025-06-25 10:27   ` Christophe Leroy
  2025-06-25 12:23     ` Johannes Berg
  0 siblings, 1 reply; 21+ messages in thread
From: Christophe Leroy @ 2025-06-25 10:27 UTC (permalink / raw)
  To: Sabyrzhan Tasbolatov, ryabinin.a.a, glider, andreyknvl, dvyukov,
	vincenzo.frascino, catalin.marinas, will, chenhuacai, kernel,
	maddy, mpe, npiggin, hca, gor, agordeev, borntraeger, svens,
	richard, anton.ivanov, johannes, dave.hansen, luto, peterz, tglx,
	mingo, bp, x86, hpa, chris, jcmvbkbc, akpm
  Cc: guoweikang.kernel, geert, rppt, tiwei.btw, richard.weiyang,
	benjamin.berg, kevin.brodsky, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-s390, linux-um,
	linux-mm



Le 25/06/2025 à 11:52, Sabyrzhan Tasbolatov a écrit :
> Replace the existing kasan_arch_is_ready() calls with kasan_enabled().
> Drop checks where the caller is already under kasan_enabled() condition.

If I understand correctly, it means that KASAN won't work anymore 
between patch 2 and 9, because until the arch calls kasan_init_generic() 
kasan_enabled() will return false.

The transition should be smooth and your series should remain bisectable.

Or am I missing something ?

Christophe



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

* Re: [PATCH 9/9] kasan/powerpc: call kasan_init_generic in kasan_init
  2025-06-25  9:52 ` [PATCH 9/9] kasan/powerpc: " Sabyrzhan Tasbolatov
@ 2025-06-25 10:33   ` Christophe Leroy
  2025-06-25 12:45     ` Sabyrzhan Tasbolatov
  2025-06-26 10:52   ` Christophe Leroy
  1 sibling, 1 reply; 21+ messages in thread
From: Christophe Leroy @ 2025-06-25 10:33 UTC (permalink / raw)
  To: Sabyrzhan Tasbolatov, ryabinin.a.a, glider, andreyknvl, dvyukov,
	vincenzo.frascino, catalin.marinas, will, chenhuacai, kernel,
	maddy, mpe, npiggin, hca, gor, agordeev, borntraeger, svens,
	richard, anton.ivanov, johannes, dave.hansen, luto, peterz, tglx,
	mingo, bp, x86, hpa, chris, jcmvbkbc, akpm
  Cc: guoweikang.kernel, geert, rppt, tiwei.btw, richard.weiyang,
	benjamin.berg, kevin.brodsky, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-s390, linux-um,
	linux-mm



Le 25/06/2025 à 11:52, Sabyrzhan Tasbolatov a écrit :
> 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.

What about:

arch/powerpc/mm/kasan/init_32.c:void __init kasan_init(void)
arch/powerpc/mm/kasan/init_book3e_64.c:void __init kasan_init(void)

Christophe



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

* Re: [PATCH 1/9] kasan: unify static kasan_flag_enabled across modes
  2025-06-25  9:52 ` [PATCH 1/9] kasan: unify static kasan_flag_enabled across modes Sabyrzhan Tasbolatov
@ 2025-06-25 10:35   ` Christophe Leroy
  2025-06-26  9:31     ` Sabyrzhan Tasbolatov
  0 siblings, 1 reply; 21+ messages in thread
From: Christophe Leroy @ 2025-06-25 10:35 UTC (permalink / raw)
  To: Sabyrzhan Tasbolatov, ryabinin.a.a, glider, andreyknvl, dvyukov,
	vincenzo.frascino, catalin.marinas, will, chenhuacai, kernel,
	maddy, mpe, npiggin, hca, gor, agordeev, borntraeger, svens,
	richard, anton.ivanov, johannes, dave.hansen, luto, peterz, tglx,
	mingo, bp, x86, hpa, chris, jcmvbkbc, akpm
  Cc: guoweikang.kernel, geert, rppt, tiwei.btw, richard.weiyang,
	benjamin.berg, kevin.brodsky, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-s390, linux-um,
	linux-mm



Le 25/06/2025 à 11:52, Sabyrzhan Tasbolatov a écrit :
> Historically the fast-path static key `kasan_flag_enabled` existed
> only for `CONFIG_KASAN_HW_TAGS`. Generic and SW_TAGS either relied on
> `kasan_arch_is_ready()` or evaluated KASAN checks unconditionally.
> As a result every architecture had to toggle a private flag
> in its `kasan_init()`.
> 
> This patch turns the flag into a single global runtime predicate that
> is built for every `CONFIG_KASAN` mode and adds a helper that flips
> the key once KASAN is ready.

Shouldn't kasan_init_generic() also perform the following line to reduce 
even more code duplication between architectures ?

	init_task.kasan_depth = 0;

Christophe



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

* Re: [PATCH 6/9] kasan/um: call kasan_init_generic in kasan_init
  2025-06-25  9:52 ` [PATCH 6/9] kasan/um: " Sabyrzhan Tasbolatov
@ 2025-06-25 12:20   ` Johannes Berg
  0 siblings, 0 replies; 21+ messages in thread
From: Johannes Berg @ 2025-06-25 12:20 UTC (permalink / raw)
  To: Sabyrzhan Tasbolatov, ryabinin.a.a, glider, andreyknvl, dvyukov,
	vincenzo.frascino, catalin.marinas, will, chenhuacai, kernel,
	maddy, mpe, npiggin, christophe.leroy, hca, gor, agordeev,
	borntraeger, svens, richard, anton.ivanov, dave.hansen, luto,
	peterz, tglx, mingo, bp, x86, hpa, chris, jcmvbkbc, akpm
  Cc: guoweikang.kernel, geert, rppt, tiwei.btw, richard.weiyang,
	benjamin.berg, kevin.brodsky, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-s390, linux-um,
	linux-mm

On Wed, 2025-06-25 at 14:52 +0500, Sabyrzhan Tasbolatov wrote:
> 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().
> 
> Closes: https://bugzilla.kernel.org/show_bug.cgi?id=218315
> Signed-off-by: Sabyrzhan Tasbolatov <snovitoll@gmail.com>

Looks fine, I guess. You can test/build it without qemu - on x86 - by
using 'make ARCH=um' or so.

I'm assuming it'll go through some kasan tree since there are
dependencies:

Acked-by: Johannes Berg <johannes@sipsolutions.net>

johannes


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

* Re: [PATCH 2/9] kasan: replace kasan_arch_is_ready with kasan_enabled
  2025-06-25 10:27   ` Christophe Leroy
@ 2025-06-25 12:23     ` Johannes Berg
  2025-06-25 12:50       ` Sabyrzhan Tasbolatov
  0 siblings, 1 reply; 21+ messages in thread
From: Johannes Berg @ 2025-06-25 12:23 UTC (permalink / raw)
  To: Christophe Leroy, Sabyrzhan Tasbolatov, ryabinin.a.a, glider,
	andreyknvl, dvyukov, vincenzo.frascino, catalin.marinas, will,
	chenhuacai, kernel, maddy, mpe, npiggin, hca, gor, agordeev,
	borntraeger, svens, richard, anton.ivanov, dave.hansen, luto,
	peterz, tglx, mingo, bp, x86, hpa, chris, jcmvbkbc, akpm
  Cc: guoweikang.kernel, geert, rppt, tiwei.btw, richard.weiyang,
	benjamin.berg, kevin.brodsky, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-s390, linux-um,
	linux-mm

On Wed, 2025-06-25 at 12:27 +0200, Christophe Leroy wrote:
> 
> Le 25/06/2025 à 11:52, Sabyrzhan Tasbolatov a écrit :
> > Replace the existing kasan_arch_is_ready() calls with kasan_enabled().
> > Drop checks where the caller is already under kasan_enabled() condition.
> 
> If I understand correctly, it means that KASAN won't work anymore 
> between patch 2 and 9, because until the arch calls kasan_init_generic() 
> kasan_enabled() will return false.
> 
> The transition should be smooth and your series should remain bisectable.
> 
> Or am I missing something ?
> 

Seems right to me, it won't work for architectures that define
kasan_arch_is_ready themselves I think?

But since they have to literally #define it, could #ifdef on that
temporarily?

johannes


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

* Re: [PATCH 9/9] kasan/powerpc: call kasan_init_generic in kasan_init
  2025-06-25 10:33   ` Christophe Leroy
@ 2025-06-25 12:45     ` Sabyrzhan Tasbolatov
  0 siblings, 0 replies; 21+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-25 12:45 UTC (permalink / raw)
  To: Christophe Leroy
  Cc: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	catalin.marinas, will, chenhuacai, kernel, maddy, mpe, npiggin,
	hca, gor, agordeev, borntraeger, svens, richard, anton.ivanov,
	johannes, dave.hansen, luto, peterz, tglx, mingo, bp, x86, hpa,
	chris, jcmvbkbc, akpm, guoweikang.kernel, geert, rppt, tiwei.btw,
	richard.weiyang, benjamin.berg, kevin.brodsky, kasan-dev,
	linux-arm-kernel, linux-kernel, loongarch, linuxppc-dev,
	linux-s390, linux-um, linux-mm

On Wed, Jun 25, 2025 at 3:33 PM Christophe Leroy
<christophe.leroy@csgroup.eu> wrote:
>
>
>
> Le 25/06/2025 à 11:52, Sabyrzhan Tasbolatov a écrit :
> > 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.
>
> What about:
>
> arch/powerpc/mm/kasan/init_32.c:void __init kasan_init(void)
> arch/powerpc/mm/kasan/init_book3e_64.c:void __init kasan_init(void)

Thanks, I've missed them. Will add in v2.
I've also found out that I've missed:
arch/arm/mm/kasan_init.c
arch/riscv/mm/kasan_init.c

>
> Christophe
>


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

* Re: [PATCH 2/9] kasan: replace kasan_arch_is_ready with kasan_enabled
  2025-06-25 12:23     ` Johannes Berg
@ 2025-06-25 12:50       ` Sabyrzhan Tasbolatov
  0 siblings, 0 replies; 21+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-25 12:50 UTC (permalink / raw)
  To: Johannes Berg
  Cc: Christophe Leroy, ryabinin.a.a, glider, andreyknvl, dvyukov,
	vincenzo.frascino, catalin.marinas, will, chenhuacai, kernel,
	maddy, mpe, npiggin, hca, gor, agordeev, borntraeger, svens,
	richard, anton.ivanov, dave.hansen, luto, peterz, tglx, mingo, bp,
	x86, hpa, chris, jcmvbkbc, akpm, guoweikang.kernel, geert, rppt,
	tiwei.btw, richard.weiyang, benjamin.berg, kevin.brodsky,
	kasan-dev, linux-arm-kernel, linux-kernel, loongarch,
	linuxppc-dev, linux-s390, linux-um, linux-mm

On Wed, Jun 25, 2025 at 5:24 PM Johannes Berg <johannes@sipsolutions.net> wrote:
>
> On Wed, 2025-06-25 at 12:27 +0200, Christophe Leroy wrote:
> >
> > Le 25/06/2025 à 11:52, Sabyrzhan Tasbolatov a écrit :
> > > Replace the existing kasan_arch_is_ready() calls with kasan_enabled().
> > > Drop checks where the caller is already under kasan_enabled() condition.
> >
> > If I understand correctly, it means that KASAN won't work anymore
> > between patch 2 and 9, because until the arch calls kasan_init_generic()
> > kasan_enabled() will return false.
> >
> > The transition should be smooth and your series should remain bisectable.
> >
> > Or am I missing something ?
> >
>
> Seems right to me, it won't work for architectures that define
> kasan_arch_is_ready themselves I think?
>
> But since they have to literally #define it, could #ifdef on that
> temporarily?

Thanks for catching it. You're right. I need to change the order of patches :

- kasan: unify static kasan_flag_enabled across modes

, then we should apply arch specific changes
where we call kasan_init_generic in kasan_init.

- kasan: replace kasan_arch_is_ready with kasan_enabled

>
> johannes


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

* Re: [PATCH 1/9] kasan: unify static kasan_flag_enabled across modes
  2025-06-25 10:35   ` Christophe Leroy
@ 2025-06-26  9:31     ` Sabyrzhan Tasbolatov
  0 siblings, 0 replies; 21+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-26  9:31 UTC (permalink / raw)
  To: Christophe Leroy
  Cc: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	catalin.marinas, will, chenhuacai, kernel, maddy, mpe, npiggin,
	hca, gor, agordeev, borntraeger, svens, richard, anton.ivanov,
	johannes, dave.hansen, luto, peterz, tglx, mingo, bp, x86, hpa,
	chris, jcmvbkbc, akpm, guoweikang.kernel, geert, rppt, tiwei.btw,
	richard.weiyang, benjamin.berg, kevin.brodsky, kasan-dev,
	linux-arm-kernel, linux-kernel, loongarch, linuxppc-dev,
	linux-s390, linux-um, linux-mm

On Wed, Jun 25, 2025 at 3:35 PM Christophe Leroy
<christophe.leroy@csgroup.eu> wrote:
>
>
>
> Le 25/06/2025 à 11:52, Sabyrzhan Tasbolatov a écrit :
> > Historically the fast-path static key `kasan_flag_enabled` existed
> > only for `CONFIG_KASAN_HW_TAGS`. Generic and SW_TAGS either relied on
> > `kasan_arch_is_ready()` or evaluated KASAN checks unconditionally.
> > As a result every architecture had to toggle a private flag
> > in its `kasan_init()`.
> >
> > This patch turns the flag into a single global runtime predicate that
> > is built for every `CONFIG_KASAN` mode and adds a helper that flips
> > the key once KASAN is ready.
>
> Shouldn't kasan_init_generic() also perform the following line to reduce
> even more code duplication between architectures ?
>
>         init_task.kasan_depth = 0;

I've tried to introduce a new function kasan_mark_ready() to gather
all arch duplicated code in one place:

In mm/kasan/common.c:

void __init kasan_mark_ready(void)
{
        /* Enable error reporting */
        init_task.kasan_depth = 0;
        /* Mark KASAN as ready */
        static_branch_enable(&kasan_flag_enabled);
}

So we could've called it
in mm/kasan/generic.c:
void __init kasan_init_generic(void)
{
        kasan_mark_ready();
        pr_info("KernelAddressSanitizer initialized (generic)\n");
}

in mm/kasan/sw_tags.c:
void __init kasan_init_sw_tags(void)
{
...
        kasan_mark_ready();
        pr_info("KernelAddressSanitizer initialized ..");
}

in mm/kasan/hw_tags.c:
void __init kasan_init_hw_tags(void)
{
...
        kasan_mark_ready();
        pr_info("KernelAddressSanitizer initialized ..");
}

But it works only for CONFIG_KASAN_GENERIC mode,
when arch code calls kasan_init(), for example, arm64:

void __init kasan_init(void)
{
        kasan_init_shadow();
        kasan_init_generic();
}

And for HW_TAGS, SW_TAGS it won't work.
Fails during compiling:
mm/kasan/common.c:45:12: error: no member named 'kasan_depth' in
'struct task_struct'
   45 |         init_task.kasan_depth = 0;

because kasan_init_sw_tags(), kasan_init_hw_tags() are called
once on CPU boot. For arm64, where these KASAN modes are supported,
both functions are called in smp_prepare_boot_cpu().

So I guess, every arch kasan_init() has to set in kasan_init()
       init_task.kasan_depth = 0;
to enable error messages before switching KASAN readiness
via enabling kasan_flag_enabled key.

>
> Christophe
>


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

* Re: [PATCH 9/9] kasan/powerpc: call kasan_init_generic in kasan_init
  2025-06-25  9:52 ` [PATCH 9/9] kasan/powerpc: " Sabyrzhan Tasbolatov
  2025-06-25 10:33   ` Christophe Leroy
@ 2025-06-26 10:52   ` Christophe Leroy
  1 sibling, 0 replies; 21+ messages in thread
From: Christophe Leroy @ 2025-06-26 10:52 UTC (permalink / raw)
  To: Sabyrzhan Tasbolatov, ryabinin.a.a, glider, andreyknvl, dvyukov,
	vincenzo.frascino, catalin.marinas, will, chenhuacai, kernel,
	maddy, mpe, npiggin, hca, gor, agordeev, borntraeger, svens,
	richard, anton.ivanov, johannes, dave.hansen, luto, peterz, tglx,
	mingo, bp, x86, hpa, chris, jcmvbkbc, akpm
  Cc: guoweikang.kernel, geert, rppt, tiwei.btw, richard.weiyang,
	benjamin.berg, kevin.brodsky, kasan-dev, linux-arm-kernel,
	linux-kernel, loongarch, linuxppc-dev, linux-s390, linux-um,
	linux-mm



Le 25/06/2025 à 11:52, Sabyrzhan Tasbolatov a écrit :
> 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>
> ---
>   arch/powerpc/include/asm/kasan.h       | 14 --------------
>   arch/powerpc/mm/kasan/init_book3s_64.c |  6 +-----
>   2 files changed, 1 insertion(+), 19 deletions(-)
> 
> diff --git a/arch/powerpc/include/asm/kasan.h b/arch/powerpc/include/asm/kasan.h
> index b5bbb94c51f..23a06fbec72 100644
> --- a/arch/powerpc/include/asm/kasan.h
> +++ b/arch/powerpc/include/asm/kasan.h
> @@ -52,20 +52,6 @@
>   
>   #endif
>   
> -#ifdef CONFIG_KASAN

The above #ifdef must remain, at the moment I get:

   CC      arch/powerpc/kernel/asm-offsets.s
In file included from ./arch/powerpc/include/asm/nohash/32/pgtable.h:65,
                  from ./arch/powerpc/include/asm/nohash/pgtable.h:13,
                  from ./arch/powerpc/include/asm/pgtable.h:20,
                  from ./include/linux/pgtable.h:6,
                  from ./arch/powerpc/include/asm/kup.h:43,
                  from ./arch/powerpc/include/asm/uaccess.h:8,
                  from ./include/linux/uaccess.h:12,
                  from ./include/linux/sched/task.h:13,
                  from ./include/linux/sched/signal.h:9,
                  from ./include/linux/rcuwait.h:6,
                  from ./include/linux/percpu-rwsem.h:7,
                  from ./include/linux/fs.h:34,
                  from ./include/linux/compat.h:17,
                  from arch/powerpc/kernel/asm-offsets.c:12:
./arch/powerpc/include/asm/kasan.h:70:2: error: #endif without #if
  #endif
   ^~~~~
In file included from ./include/linux/kasan.h:21,
                  from ./include/linux/slab.h:260,
                  from ./include/linux/fs.h:46,
                  from ./include/linux/compat.h:17,
                  from arch/powerpc/kernel/asm-offsets.c:12:
./arch/powerpc/include/asm/kasan.h:70:2: error: #endif without #if
  #endif
   ^~~~~
make[2]: *** [scripts/Makefile.build:182: 
arch/powerpc/kernel/asm-offsets.s] Error 1


> -#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_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) { }



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

* Re: [PATCH 5/9] kasan/loongarch: call kasan_init_generic in kasan_init
  2025-06-25  9:52 ` [PATCH 5/9] kasan/loongarch: " Sabyrzhan Tasbolatov
@ 2025-06-26 13:29   ` Peter Zijlstra
  2025-06-26 13:52     ` Sabyrzhan Tasbolatov
  0 siblings, 1 reply; 21+ messages in thread
From: Peter Zijlstra @ 2025-06-26 13:29 UTC (permalink / raw)
  To: Sabyrzhan Tasbolatov
  Cc: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	catalin.marinas, will, chenhuacai, kernel, maddy, mpe, npiggin,
	christophe.leroy, hca, gor, agordeev, borntraeger, svens, richard,
	anton.ivanov, johannes, dave.hansen, luto, tglx, mingo, bp, x86,
	hpa, chris, jcmvbkbc, akpm, guoweikang.kernel, geert, rppt,
	tiwei.btw, richard.weiyang, benjamin.berg, kevin.brodsky,
	kasan-dev, linux-arm-kernel, linux-kernel, loongarch,
	linuxppc-dev, linux-s390, linux-um, linux-mm

On Wed, Jun 25, 2025 at 02:52:20PM +0500, Sabyrzhan Tasbolatov wrote:
> 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.
> 
> 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");
>  }

This one is weird because its the only arch that does things after
marking early_state false.

Is that really correct, or should kasan_init_generic() be last, like all
the other architectures?

Also, please move init_task.kasan_depth = 0 into the generic thing.
ARM64 might have fooled you with the wrapper function, but they all do
this right before that pr_info you're taking out.


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

* Re: [PATCH 5/9] kasan/loongarch: call kasan_init_generic in kasan_init
  2025-06-26 13:29   ` Peter Zijlstra
@ 2025-06-26 13:52     ` Sabyrzhan Tasbolatov
  0 siblings, 0 replies; 21+ messages in thread
From: Sabyrzhan Tasbolatov @ 2025-06-26 13:52 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
	catalin.marinas, will, chenhuacai, kernel, maddy, mpe, npiggin,
	christophe.leroy, hca, gor, agordeev, borntraeger, svens, richard,
	anton.ivanov, johannes, dave.hansen, luto, tglx, mingo, bp, x86,
	hpa, chris, jcmvbkbc, akpm, guoweikang.kernel, geert, rppt,
	tiwei.btw, richard.weiyang, benjamin.berg, kevin.brodsky,
	kasan-dev, linux-arm-kernel, linux-kernel, loongarch,
	linuxppc-dev, linux-s390, linux-um, linux-mm

On Thu, Jun 26, 2025 at 6:29 PM Peter Zijlstra <peterz@infradead.org> wrote:
>
> On Wed, Jun 25, 2025 at 02:52:20PM +0500, Sabyrzhan Tasbolatov wrote:
> > 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.
> >
> > 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");
> >  }
>
> This one is weird because its the only arch that does things after
> marking early_state false.
>
> Is that really correct, or should kasan_init_generic() be last, like all
> the other architectures?

It really differs from other arch kasan_init(). I can't verify that
kasan_init_generic()
can be placed at the end of kasan_init() because right after
switching the KASAN flag, there's kasan_enabled() check in
kasan_mem_to_shadow().

In arch/loongarch/mm/kasan_init.c:

void *kasan_mem_to_shadow(const void *addr)
{
        if (!kasan_enabled()) {
                return (void *)(kasan_early_shadow_page);
        } else {
...
}

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_init_generic();

        /* Populate the linear mapping */
        for_each_mem_range(i, &pa_start, &pa_end) {
....
        kasan_map_populate((unsigned long)kasan_mem_to_shadow(start),
}

>
> Also, please move init_task.kasan_depth = 0 into the generic thing.
> ARM64 might have fooled you with the wrapper function, but they all do
> this right before that pr_info you're taking out.

Please check "[PATCH 1/9] kasan: unify static kasan_flag_enabled across modes",
where I've replied to Christophe:
https://lore.kernel.org/all/CACzwLxj3KWdy-mBu-te1OFf2FZ8eTp5CieYswF5NVY4qPWD93Q@mail.gmail.com/

I can try to put `init_task.kasan_depth = 0;` in kasan_init_generic(),
but in ARM64 kasan_init() we'll still need to have this line for
HW_TAGS, SW_TAGS mode.


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

end of thread, other threads:[~2025-06-26 13:53 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-25  9:52 [PATCH 0/9] kasan: unify kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
2025-06-25  9:52 ` [PATCH 1/9] kasan: unify static kasan_flag_enabled across modes Sabyrzhan Tasbolatov
2025-06-25 10:35   ` Christophe Leroy
2025-06-26  9:31     ` Sabyrzhan Tasbolatov
2025-06-25  9:52 ` [PATCH 2/9] kasan: replace kasan_arch_is_ready with kasan_enabled Sabyrzhan Tasbolatov
2025-06-25 10:27   ` Christophe Leroy
2025-06-25 12:23     ` Johannes Berg
2025-06-25 12:50       ` Sabyrzhan Tasbolatov
2025-06-25  9:52 ` [PATCH 3/9] kasan/arm64: call kasan_init_generic in kasan_init Sabyrzhan Tasbolatov
2025-06-25  9:52 ` [PATCH 4/9] kasan/xtensa: " Sabyrzhan Tasbolatov
2025-06-25  9:52 ` [PATCH 5/9] kasan/loongarch: " Sabyrzhan Tasbolatov
2025-06-26 13:29   ` Peter Zijlstra
2025-06-26 13:52     ` Sabyrzhan Tasbolatov
2025-06-25  9:52 ` [PATCH 6/9] kasan/um: " Sabyrzhan Tasbolatov
2025-06-25 12:20   ` Johannes Berg
2025-06-25  9:52 ` [PATCH 7/9] kasan/x86: " Sabyrzhan Tasbolatov
2025-06-25  9:52 ` [PATCH 8/9] kasan/s390: " Sabyrzhan Tasbolatov
2025-06-25  9:52 ` [PATCH 9/9] kasan/powerpc: " Sabyrzhan Tasbolatov
2025-06-25 10:33   ` Christophe Leroy
2025-06-25 12:45     ` Sabyrzhan Tasbolatov
2025-06-26 10:52   ` 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).