[PATCH 2/4] mm/kasan: move kasan= code to common place

Baoquan He
2025-08-05T06:24:01Z

This allows generic and sw_tags to be set in kernel cmdline too.

When at it, rename 'kasan_arg' to 'kasan_arg_disabled' as a bool
variable. And expose 'kasan_flag_enabled' to kasan common place
too.

This is prepared for later adding kernel parameter kasan=on|off for
all kasan modes.

Signed-off-by: Baoquan He <bhe@redhat.com>
---
include/linux/kasan-enabled.h | 4 +++-
mm/kasan/common.c | 27 +++++++++++++++++++++++++++
mm/kasan/hw_tags.c | 35 ++---------------------------------
3 files changed, 32 insertions(+), 34 deletions(-)

diff --git a/include/linux/kasan-enabled.h b/include/linux/kasan-enabled.h
index 6f612d69ea0c..32f2d19f599f 100644
--- a/include/linux/kasan-enabled.h
+++ b/include/linux/kasan-enabled.h
@@ -4,10 +4,12 @@

#include <linux/static_key.h>

-#ifdef CONFIG_KASAN_HW_TAGS
+extern bool kasan_arg_disabled;

DECLARE_STATIC_KEY_FALSE(kasan_flag_enabled);

+#ifdef CONFIG_KASAN_HW_TAGS
+
static __always_inline bool kasan_enabled(void)
{
return static_branch_likely(&kasan_flag_enabled);
diff --git a/mm/kasan/common.c b/mm/kasan/common.c
index ed4873e18c75..fe6937654203 100644
--- a/mm/kasan/common.c
+++ b/mm/kasan/common.c
@@ -32,6 +32,33 @@
#include "kasan.h"
#include "../slab.h"

+/*
+ * Whether KASAN is enabled at all.
+ * The value remains false until KASAN is initialized.
+ */
+DEFINE_STATIC_KEY_FALSE(kasan_flag_enabled);
+EXPORT_SYMBOL(kasan_flag_enabled);
+
+bool kasan_arg_disabled;
+/* kasan=off/on */
+static int __init early_kasan_flag(char *arg)
+{
+ if (!arg)
+ return -EINVAL;
+
+ if (!strcmp(arg, "off"))
+ kasan_arg_disabled = true;
+ else if (!strcmp(arg, "on"))
+ kasan_arg_disabled = false;
+ else
+ return -EINVAL;
+
+ return 0;
+}
+early_param("kasan", early_kasan_flag);
+
+
+
struct slab *kasan_addr_to_slab(const void *addr)
{
if (virt_addr_valid(addr))
diff --git a/mm/kasan/hw_tags.c b/mm/kasan/hw_tags.c
index 9a6927394b54..377e9c285a74 100644
--- a/mm/kasan/hw_tags.c
+++ b/mm/kasan/hw_tags.c
@@ -22,12 +22,6 @@

#include "kasan.h"

-enum kasan_arg {
- KASAN_ARG_DEFAULT,
- KASAN_ARG_OFF,
- KASAN_ARG_ON,
-};
-
enum kasan_arg_mode {
KASAN_ARG_MODE_DEFAULT,
KASAN_ARG_MODE_SYNC,
@@ -41,17 +35,9 @@ enum kasan_arg_vmalloc {
KASAN_ARG_VMALLOC_ON,
};

-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.
@@ -85,23 +71,6 @@ unsigned int kasan_page_alloc_sample_order = PAGE_ALLOC_SAMPLE_ORDER_DEFAULT;

DEFINE_PER_CPU(long, kasan_page_alloc_skip);

-/* kasan=off/on */
-static int __init early_kasan_flag(char *arg)
-{
- if (!arg)
- return -EINVAL;
-
- if (!strcmp(arg, "off"))
- kasan_arg = KASAN_ARG_OFF;
- else if (!strcmp(arg, "on"))
- kasan_arg = KASAN_ARG_ON;
- else
- return -EINVAL;
-
- return 0;
-}
-early_param("kasan", early_kasan_flag);
-
/* kasan.mode=sync/async/asymm */
static int __init early_kasan_mode(char *arg)
{
@@ -209,7 +178,7 @@ void kasan_init_hw_tags_cpu(void)
* When this function is called, kasan_flag_enabled is not yet
* set by kasan_init_hw_tags(). Thus, check kasan_arg instead.
*/
- if (kasan_arg == KASAN_ARG_OFF)
+ if (kasan_arg_disabled)
return;

/*
@@ -227,7 +196,7 @@ void __init kasan_init_hw_tags(void)
return;

/* If KASAN is disabled via command line, don't initialize it. */
- if (kasan_arg == KASAN_ARG_OFF)
+ if (kasan_arg_disabled)
return;

switch (kasan_arg_mode) {
--
2.41.0