[PATCH v5 04/15] mm/kasan: make kasan=on|off take effect for all three modes

Baoquan He
2026-02-25T08:17:20Z

Now everything is ready, setting kasan=off can disable kasan for all
three modes.

Signed-off-by: Baoquan He <bhe@redhat.com>
---
include/linux/kasan-enabled.h | 12 +++++-------
mm/kasan/common.c | 4 ----
2 files changed, 5 insertions(+), 11 deletions(-)

diff --git a/include/linux/kasan-enabled.h b/include/linux/kasan-enabled.h
index b7cb906825ca..cec21a08446e 100644
--- a/include/linux/kasan-enabled.h
+++ b/include/linux/kasan-enabled.h
@@ -4,10 +4,11 @@

#include <linux/static_key.h>

-#if defined(CONFIG_ARCH_DEFER_KASAN) || defined(CONFIG_KASAN_HW_TAGS)
+#ifdef CONFIG_KASAN
+extern bool kasan_arg_disabled;
+
/*
* Global runtime flag for KASAN modes that need runtime control.
- * Used by ARCH_DEFER_KASAN architectures and HW_TAGS mode.
*/
DECLARE_STATIC_KEY_FALSE(kasan_flag_enabled);

@@ -25,18 +26,15 @@ static inline void kasan_enable(void)
static_branch_enable(&kasan_flag_enabled);
}
#else
-/* For architectures that can enable KASAN early, use compile-time check. */
static __always_inline bool kasan_enabled(void)
{
- return IS_ENABLED(CONFIG_KASAN);
+ return false;
}

static inline void kasan_enable(void) {}
-#endif /* CONFIG_ARCH_DEFER_KASAN || CONFIG_KASAN_HW_TAGS */
+#endif

#ifdef CONFIG_KASAN_HW_TAGS
-extern bool kasan_arg_disabled;
-
static inline bool kasan_hw_tags_enabled(void)
{
return kasan_enabled();
diff --git a/mm/kasan/common.c b/mm/kasan/common.c
index 0d788a468e96..fc6513fa5795 100644
--- a/mm/kasan/common.c
+++ b/mm/kasan/common.c
@@ -33,16 +33,13 @@
#include "kasan.h"
#include "../slab.h"

-#if defined(CONFIG_ARCH_DEFER_KASAN) || defined(CONFIG_KASAN_HW_TAGS)
/*
* Definition of the unified static key declared in kasan-enabled.h.
* This provides consistent runtime enable/disable across KASAN modes.
*/
DEFINE_STATIC_KEY_FALSE(kasan_flag_enabled);
EXPORT_SYMBOL_GPL(kasan_flag_enabled);
-#endif

-#ifdef CONFIG_KASAN_HW_TAGS
bool kasan_arg_disabled __ro_after_init;

/* kasan=off/on */
@@ -61,7 +58,6 @@ static int __init early_kasan_flag(char *arg)
return 0;
}
early_param("kasan", early_kasan_flag);
-#endif

struct slab *kasan_addr_to_slab(const void *addr)
{
--
2.52.0