Skip to contentSkip to conversation

mm/kasan: make kasan=on|off work for all three modes

PATCH Superseded Baoquan He 7 participants 22 messages Aug 5, 2025 – Aug 12, 2025

Subsystems (13)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Baoquan He

author
#0 lore raw

cover mm/kasan: make kasan=on|off work for all three modes

Currently only hw_tags mode of kasan can be enabled or disabled with kernel parameter kasan=on|off for built kernel. For kasan generic and sw_tags mode, there's no way to disable them once kernel is built. This is not convenient sometime, e.g in system kdump is configured. When the 1st kernel has KASAN enabled and crash triggered to switch to kdump kernel, the generic or sw_tags mode will cost much extra memory for kasan shadow while in fact it's meaningless to have kasan in kdump kernel.

So this patchset moves the kasan=on|off out of hw_tags scope and into common code to make it visible in generic and sw_tags mode too. Then we can add kasan=off in kdump kernel to reduce the unneeded meomry cost for kasan.

Test:
=====
I only took test on x86_64 for generic mode, and on arm64 for generic, sw_tags and hw_tags mode. All of them works well.

However when I tested sw_tags on a HPE apollo arm64 machine, it always breaks kernel with a KASAN bug. Even w/o this patchset applied, the bug can always be seen too.

"BUG: KASAN: invalid-access in pcpu_alloc_noprof+0x42c/0x9a8"

I haven't got root cause of the bug, will report the bug later in another thread.
====

Baoquan He (4):
mm/kasan: add conditional checks in functions to return directly if
kasan is disabled
mm/kasan: move kasan= code to common place
mm/kasan: don't initialize kasan if it's disabled mm/kasan: make kasan=on|off take effect for all three modes

 arch/arm/mm/kasan_init.c               |  6 +++++
 arch/arm64/mm/kasan_init.c             |  7 ++++++
 arch/loongarch/mm/kasan_init.c         |  5 ++++
 arch/powerpc/mm/kasan/init_32.c        |  8 +++++-
 arch/powerpc/mm/kasan/init_book3e_64.c |  6 +++++
 arch/powerpc/mm/kasan/init_book3s_64.c |  6 +++++
 arch/riscv/mm/kasan_init.c             |  6 +++++
 arch/um/kernel/mem.c                   |  6 +++++
 arch/x86/mm/kasan_init_64.c            |  6 +++++
 arch/xtensa/mm/kasan_init.c            |  6 +++++
 include/linux/kasan-enabled.h          | 11 ++------
 mm/kasan/common.c                      | 27 ++++++++++++++++++++
 mm/kasan/generic.c                     | 20 +++++++++++++--
 mm/kasan/hw_tags.c                     | 35 ++------------------------
 mm/kasan/init.c                        |  6 +++++
 mm/kasan/quarantine.c                  |  3 +++
 mm/kasan/shadow.c                      | 23 ++++++++++++++++-
 mm/kasan/sw_tags.c                     |  9 +++++++
 18 files changed, 150 insertions(+), 46 deletions(-)
Signature · 1 line
2.41.0
author
#1 lore raw

PATCH 1/4 mm/kasan: add conditional checks in functions to return directly if kasan is disabled

The current code only does the check if kasan is disabled for hw_tags mode. Here add the conditional checks for functional functions of generic mode and sw_tags mode.

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

---
 mm/kasan/generic.c    | 20 ++++++++++++++++++--
 mm/kasan/init.c       |  6 ++++++
 mm/kasan/quarantine.c |  3 +++
 mm/kasan/shadow.c     | 23 ++++++++++++++++++++++-
 mm/kasan/sw_tags.c    |  3 +++
 5 files changed, 52 insertions(+), 3 deletions(-)
mm/kasan/generic.c +18 -2
index d54e89f8c3e7..ee4ddc1e7127 100644
@@ -165,6 +165,9 @@ static __always_inline bool check_region_inline(const void *addr,
 						size_t size, bool write,
 						unsigned long ret_ip)
 {
+	if (!kasan_enabled())
+		return true;
+
 	if (!kasan_arch_is_ready())
 		return true;
 
@@ -203,12 +206,13 @@ bool kasan_byte_accessible(const void *addr)
 
 void kasan_cache_shrink(struct kmem_cache *cache)
 {
-	kasan_quarantine_remove_cache(cache);
+	if (kasan_enabled())
+		kasan_quarantine_remove_cache(cache);
 }
 
 void kasan_cache_shutdown(struct kmem_cache *cache)
 {
-	if (!__kmem_cache_empty(cache))
+	if (kasan_enabled() && !__kmem_cache_empty(cache))
 		kasan_quarantine_remove_cache(cache);
 }
 
@@ -228,6 +232,9 @@ void __asan_register_globals(void *ptr, ssize_t size)
 	int i;
 	struct kasan_global *globals = ptr;
 
+	if (!kasan_enabled())
+		return;
+
 	for (i = 0; i < size; i++)
 		register_global(&globals[i]);
 }
@@ -358,6 +365,9 @@ void kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
 	unsigned int rem_free_meta_size;
 	unsigned int orig_alloc_meta_offset;
 
+	if (!kasan_enabled())
+		return;
+
 	if (!kasan_requires_meta())
 		return;
 
@@ -510,6 +520,9 @@ size_t kasan_metadata_size(struct kmem_cache *cache, bool in_object)
 {
 	struct kasan_cache *info = &cache->kasan_info;
 
+	if (!kasan_enabled())
+		return 0;
+
 	if (!kasan_requires_meta())
 		return 0;
 
@@ -535,6 +548,9 @@ void kasan_record_aux_stack(void *addr)
 	struct kasan_alloc_meta *alloc_meta;
 	void *object;
 
+	if (!kasan_enabled())
+		return;
+
 	if (is_kfence_address(addr) || !slab)
 		return;
 
mm/kasan/init.c +6 -0
index ced6b29fcf76..43d95f329675 100644
@@ -449,6 +449,9 @@ void kasan_remove_zero_shadow(void *start, unsigned long size)
 	unsigned long addr, end, next;
 	pgd_t *pgd;
 
+	if (!kasan_enabled())
+		return;
+
 	addr = (unsigned long)kasan_mem_to_shadow(start);
 	end = addr + (size >> KASAN_SHADOW_SCALE_SHIFT);
 
@@ -484,6 +487,9 @@ int kasan_add_zero_shadow(void *start, unsigned long size)
 	int ret;
 	void *shadow_start, *shadow_end;
 
+	if (!kasan_enabled())
+		return 0;
+
 	shadow_start = kasan_mem_to_shadow(start);
 	shadow_end = shadow_start + (size >> KASAN_SHADOW_SCALE_SHIFT);
 
mm/kasan/quarantine.c +3 -0
index 6958aa713c67..a6dc2c3d8a15 100644
@@ -405,6 +405,9 @@ static int __init kasan_cpu_quarantine_init(void)
 {
 	int ret = 0;
 
+	if (!kasan_enabled())
+		return 0;
+
 	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "mm/kasan:online",
 				kasan_cpu_online, kasan_cpu_offline);
 	if (ret < 0)
mm/kasan/shadow.c +22 -1
index d2c70cd2afb1..637f2d02d2a3 100644
@@ -125,6 +125,9 @@ void kasan_poison(const void *addr, size_t size, u8 value, bool init)
 {
 	void *shadow_start, *shadow_end;
 
+	if (!kasan_enabled())
+		return;
+
 	if (!kasan_arch_is_ready())
 		return;
 
@@ -150,6 +153,9 @@ EXPORT_SYMBOL_GPL(kasan_poison);
 #ifdef CONFIG_KASAN_GENERIC
 void kasan_poison_last_granule(const void *addr, size_t size)
 {
+	if (!kasan_enabled())
+		return;
+
 	if (!kasan_arch_is_ready())
 		return;
 
@@ -164,6 +170,8 @@ void kasan_unpoison(const void *addr, size_t size, bool init)
 {
 	u8 tag = get_tag(addr);
 
+	if (!kasan_enabled())
+		return;
 	/*
 	 * Perform shadow offset calculation based on untagged address, as
 	 * some of the callers (e.g. kasan_unpoison_new_object) pass tagged
@@ -277,7 +285,8 @@ static int __meminit kasan_mem_notifier(struct notifier_block *nb,
 
 static int __init kasan_memhotplug_init(void)
 {
-	hotplug_memory_notifier(kasan_mem_notifier, DEFAULT_CALLBACK_PRI);
+	if (kasan_enabled())
+		hotplug_memory_notifier(kasan_mem_notifier, DEFAULT_CALLBACK_PRI);
 
 	return 0;
 }
@@ -390,6 +399,9 @@ int kasan_populate_vmalloc(unsigned long addr, unsigned long size)
 	unsigned long shadow_start, shadow_end;
 	int ret;
 
+	if (!kasan_enabled())
+		return 0;
+
 	if (!kasan_arch_is_ready())
 		return 0;
 
@@ -560,6 +572,9 @@ void kasan_release_vmalloc(unsigned long start, unsigned long end,
 	unsigned long region_start, region_end;
 	unsigned long size;
 
+	if (!kasan_enabled())
+		return;
+
 	if (!kasan_arch_is_ready())
 		return;
 
@@ -655,6 +670,9 @@ int kasan_alloc_module_shadow(void *addr, size_t size, gfp_t gfp_mask)
 	size_t shadow_size;
 	unsigned long shadow_start;
 
+	if (!kasan_enabled())
+		return 0;
+
 	shadow_start = (unsigned long)kasan_mem_to_shadow(addr);
 	scaled_size = (size + KASAN_GRANULE_SIZE - 1) >>
 				KASAN_SHADOW_SCALE_SHIFT;
@@ -691,6 +709,9 @@ int kasan_alloc_module_shadow(void *addr, size_t size, gfp_t gfp_mask)
 
 void kasan_free_module_shadow(const struct vm_struct *vm)
 {
+	if (!kasan_enabled())
+		return;
+
 	if (IS_ENABLED(CONFIG_UML))
 		return;
 
mm/kasan/sw_tags.c +3 -0
index b9382b5b6a37..01f19bc4a326 100644
@@ -78,6 +78,9 @@ bool kasan_check_range(const void *addr, size_t size, bool write,
 	u8 *shadow_first, *shadow_last, *shadow;
 	void *untagged_addr;
 
+	if (!kasan_enabled())
+		return true;
+
 	if (unlikely(size == 0))
 		return true;
 
Signature · 1 line
2.41.0
author
#2 lore raw

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

In reply to Baoquan He

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.

---
 include/linux/kasan-enabled.h |  4 +++-
 mm/kasan/common.c             | 27 +++++++++++++++++++++++++++
 mm/kasan/hw_tags.c            | 35 ++---------------------------------
 3 files changed, 32 insertions(+), 34 deletions(-)
include/linux/kasan-enabled.h +3 -1
index 6f612d69ea0c..32f2d19f599f 100644
@@ -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);
mm/kasan/common.c +27 -0
index ed4873e18c75..fe6937654203 100644
@@ -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))
mm/kasan/hw_tags.c +2 -33
index 9a6927394b54..377e9c285a74 100644
@@ -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) {
Signature · 1 line
2.41.0
#3 lore raw

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

Quoted 41 lines · Baoquan He' via kasan-dev

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.

---
 include/linux/kasan-enabled.h |  4 +++-
 mm/kasan/common.c             | 27 +++++++++++++++++++++++++++
 mm/kasan/hw_tags.c            | 35 ++---------------------------------
 3 files changed, 32 insertions(+), 34 deletions(-)
include/linux/kasan-enabled.h +3 -1
index 6f612d69ea0c..32f2d19f599f 100644
@@ -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);
mm/kasan/common.c +0 -0
index ed4873e18c75..fe6937654203 100644
@@ -32,6 +32,33 @@
 #include "kasan.h"

mm/kasan/common.c line 42

Quoted 10 lines
mm/kasan/common.c +8 -0
 #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;

You lost __ro_after_init

Quoted 9 lines
mm/kasan/common.c +9 -0
+/* 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"))

mm/kasan/common.c line 61

Quoted 10 lines
mm/kasan/common.c +10 -0
+               kasan_arg_disabled = false;
+       else
+               return -EINVAL;
+
+       return 0;
+}
+early_param("kasan", early_kasan_flag);
+
+
+

Why extra blank lines?

author
#4 lore raw

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

Quoted 45 lines · Marco Elver
Quoted 43 lines · Baoquan He' via kasan-dev

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.

---
 include/linux/kasan-enabled.h |  4 +++-
 mm/kasan/common.c             | 27 +++++++++++++++++++++++++++
 mm/kasan/hw_tags.c            | 35 ++---------------------------------
 3 files changed, 32 insertions(+), 34 deletions(-)
include/linux/kasan-enabled.h +3 -1
index 6f612d69ea0c..32f2d19f599f 100644
@@ -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);
mm/kasan/common.c +0 -0
index ed4873e18c75..fe6937654203 100644
@@ -32,6 +32,33 @@
 #include "kasan.h"
 #include "../slab.h"
 
Quoted 10 lines

mm/kasan/common.c line 42

Quoted 8 lines
mm/kasan/common.c +8 -0
+/*
+ * 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;

You lost __ro_after_init

Right, thanks for careful reviewing.

Quoted 14 lines
Quoted 14 lines
mm/kasan/common.c +13 -0
+/* 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;
+
Quoted 8 lines

mm/kasan/common.c line 61

Quoted 6 lines
mm/kasan/common.c +6 -0
+       return 0;
+}
+early_param("kasan", early_kasan_flag);
+
+
+

Why extra blank lines?

Good catch, will remove it in v2. Thanks.

author
#5 lore raw

PATCH 3/4 mm/kasan: don't initialize kasan if it's disabled

In reply to Baoquan He

This is mainly done in all architectures which support kasan, and also need be done in sw_tags init funciton kasan_init_sw_tags().

And also add code to enable kasan_flag_enabled, this is for later usage.

---
 arch/arm/mm/kasan_init.c               | 6 ++++++
 arch/arm64/mm/kasan_init.c             | 7 +++++++
 arch/loongarch/mm/kasan_init.c         | 5 +++++
 arch/powerpc/mm/kasan/init_32.c        | 8 +++++++-
 arch/powerpc/mm/kasan/init_book3e_64.c | 6 ++++++
 arch/powerpc/mm/kasan/init_book3s_64.c | 6 ++++++
 arch/riscv/mm/kasan_init.c             | 6 ++++++
 arch/um/kernel/mem.c                   | 6 ++++++
 arch/x86/mm/kasan_init_64.c            | 6 ++++++
 arch/xtensa/mm/kasan_init.c            | 6 ++++++
 mm/kasan/sw_tags.c                     | 6 ++++++
 11 files changed, 67 insertions(+), 1 deletion(-)
arch/arm/mm/kasan_init.c +6 -0
index 111d4f703136..c764e1b9c9c5 100644
@@ -212,6 +212,8 @@ void __init kasan_init(void)
 	phys_addr_t pa_start, pa_end;
 	u64 i;
 
+	if (kasan_arg_disabled)
+		return;
 	/*
 	 * We are going to perform proper setup of shadow memory.
 	 *
@@ -300,6 +302,10 @@ void __init kasan_init(void)
 	local_flush_tlb_all();
 
 	memset(kasan_early_shadow_page, 0, PAGE_SIZE);
+
+	/* KASAN is now initialized, enable it. */
+	static_branch_enable(&kasan_flag_enabled);
+
 	pr_info("Kernel address sanitizer initialized\n");
 	init_task.kasan_depth = 0;
 }
arch/arm64/mm/kasan_init.c +7 -0
index d541ce45daeb..0e4ffe3f5d0e 100644
@@ -384,6 +384,9 @@ void __init kasan_populate_early_vm_area_shadow(void *start, unsigned long size)
 {
 	unsigned long shadow_start, shadow_end;
 
+	if (!kasan_enabled())
+		return;
+
 	if (!is_vmalloc_or_module_addr(start))
 		return;
 
@@ -397,6 +400,9 @@ void __init kasan_populate_early_vm_area_shadow(void *start, unsigned long size)
 
 void __init kasan_init(void)
 {
+	if (kasan_arg_disabled)
+		return;
+
 	kasan_init_shadow();
 	kasan_init_depth();
 #if defined(CONFIG_KASAN_GENERIC)
@@ -405,6 +411,7 @@ void __init kasan_init(void)
 	 * Software and Hardware Tag-Based modes still require
 	 * kasan_init_sw_tags() and kasan_init_hw_tags() correspondingly.
 	 */
+	static_branch_enable(&kasan_flag_enabled);
 	pr_info("KernelAddressSanitizer initialized (generic)\n");
 #endif
 }
arch/loongarch/mm/kasan_init.c +5 -0
index d2681272d8f0..0c32eee6910f 100644
@@ -267,6 +267,8 @@ void __init kasan_init(void)
 	u64 i;
 	phys_addr_t pa_start, pa_end;
 
+	if (kasan_arg_disabled)
+		return;
 	/*
 	 * If PGDIR_SIZE is too large for cpu_vabits, KASAN_SHADOW_END will
 	 * overflow UINTPTR_MAX and then looks like a user space address.
@@ -327,6 +329,9 @@ void __init kasan_init(void)
 	csr_write64(__pa_symbol(swapper_pg_dir), LOONGARCH_CSR_PGDH);
 	local_flush_tlb_all();
 
+	/* KASAN is now initialized, enable it. */
+	static_branch_enable(&kasan_flag_enabled);
+
 	/* At this point kasan is fully initialized. Enable error messages */
 	init_task.kasan_depth = 0;
 	pr_info("KernelAddressSanitizer initialized.\n");
arch/powerpc/mm/kasan/init_32.c +7 -1
index 03666d790a53..b0c465f3fbf5 100644
@@ -141,6 +141,9 @@ void __init kasan_init(void)
 	u64 i;
 	int ret;
 
+	if (kasan_arg_disabled)
+		return;
+
 	for_each_mem_range(i, &base, &end) {
 		phys_addr_t top = min(end, total_lowmem);
 
@@ -163,6 +166,9 @@ void __init kasan_init(void)
 
 	clear_page(kasan_early_shadow_page);
 
+	/* KASAN is now initialized, enable it. */
+	static_branch_enable(&kasan_flag_enabled);
+
 	/* At this point kasan is fully initialized. Enable error messages */
 	init_task.kasan_depth = 0;
 	pr_info("KASAN init done\n");
@@ -170,7 +176,7 @@ void __init kasan_init(void)
 
 void __init kasan_late_init(void)
 {
-	if (IS_ENABLED(CONFIG_KASAN_VMALLOC))
+	if (IS_ENABLED(CONFIG_KASAN_VMALLOC) && kasan_enabled())
 		kasan_unmap_early_shadow_vmalloc();
 }
 
arch/powerpc/mm/kasan/init_book3e_64.c +6 -0
index 60c78aac0f63..1e1c10467a2b 100644
@@ -111,6 +111,9 @@ void __init kasan_init(void)
 	u64 i;
 	pte_t zero_pte = pfn_pte(virt_to_pfn(kasan_early_shadow_page), PAGE_KERNEL_RO);
 
+	if (kasan_arg_disabled)
+		return;
+
 	for_each_mem_range(i, &start, &end)
 		kasan_init_phys_region(phys_to_virt(start), phys_to_virt(end));
 
@@ -125,6 +128,9 @@ void __init kasan_init(void)
 
 	memset(kasan_early_shadow_page, 0, PAGE_SIZE);
 
+	/* KASAN is now initialized, enable it. */
+	static_branch_enable(&kasan_flag_enabled);
+
 	/* Enable error messages */
 	init_task.kasan_depth = 0;
 	pr_info("KASAN init done\n");
arch/powerpc/mm/kasan/init_book3s_64.c +6 -0
index 7d959544c077..9c5cf2354c8b 100644
@@ -56,6 +56,9 @@ void __init kasan_init(void)
 	u64 i;
 	pte_t zero_pte = pfn_pte(virt_to_pfn(kasan_early_shadow_page), PAGE_KERNEL);
 
+	if (kasan_arg_disabled)
+		return;
+
 	if (!early_radix_enabled()) {
 		pr_warn("KASAN not enabled as it requires radix!");
 		return;
@@ -94,6 +97,9 @@ void __init kasan_init(void)
 
 	static_branch_inc(&powerpc_kasan_enabled_key);
 
+	/* KASAN is now initialized, enable it. */
+	static_branch_enable(&kasan_flag_enabled);
+
 	/* Enable error messages */
 	init_task.kasan_depth = 0;
 	pr_info("KASAN init done\n");
arch/riscv/mm/kasan_init.c +6 -0
index 41c635d6aca4..ac3ac227c765 100644
@@ -485,6 +485,9 @@ void __init kasan_init(void)
 	phys_addr_t p_start, p_end;
 	u64 i;
 
+	if (kasan_arg_disabled)
+		return;
+
 	create_tmp_mapping();
 	csr_write(CSR_SATP, PFN_DOWN(__pa(tmp_pg_dir)) | satp_mode);
 
@@ -531,6 +534,9 @@ void __init kasan_init(void)
 	memset(kasan_early_shadow_page, KASAN_SHADOW_INIT, PAGE_SIZE);
 	init_task.kasan_depth = 0;
 
+	/* KASAN is now initialized, enable it. */
+	static_branch_enable(&kasan_flag_enabled);
+
 	csr_write(CSR_SATP, PFN_DOWN(__pa(swapper_pg_dir)) | satp_mode);
 	local_flush_tlb_all();
 }
arch/um/kernel/mem.c +6 -0
index 76bec7de81b5..6961841daa12 100644
@@ -26,6 +26,9 @@
 int kasan_um_is_ready;
 void kasan_init(void)
 {
+
+	if (kasan_arg_disabled)
+		return;
 	/*
 	 * kasan_map_memory will map all of the required address space and
 	 * the host machine will allocate physical memory as necessary.
@@ -33,6 +36,9 @@ 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 is now initialized, enable it. */
+	static_branch_enable(&kasan_flag_enabled);
 }
 
 static void (*kasan_init_ptr)(void)
arch/x86/mm/kasan_init_64.c +6 -0
index 0539efd0d216..d7e8c59da435 100644
@@ -343,6 +343,9 @@ void __init kasan_init(void)
 	unsigned long shadow_cea_begin, shadow_cea_per_cpu_begin, shadow_cea_end;
 	int i;
 
+	if (kasan_arg_disabled)
+		return;
+
 	memcpy(early_top_pgt, init_top_pgt, sizeof(early_top_pgt));
 
 	/*
@@ -450,6 +453,9 @@ void __init kasan_init(void)
 	/* Flush TLBs again to be sure that write protection applied. */
 	__flush_tlb_all();
 
+	/* KASAN is now initialized, enable it. */
+	static_branch_enable(&kasan_flag_enabled);
+
 	init_task.kasan_depth = 0;
 	pr_info("KernelAddressSanitizer initialized\n");
 }
arch/xtensa/mm/kasan_init.c +6 -0
index f39c4d83173a..4a7b77f47225 100644
@@ -70,6 +70,9 @@ void __init kasan_init(void)
 {
 	int i;
 
+	if (kasan_arg_disabled)
+		return;
+
 	BUILD_BUG_ON(KASAN_SHADOW_OFFSET != KASAN_SHADOW_START -
 		     (KASAN_START_VADDR >> KASAN_SHADOW_SCALE_SHIFT));
 	BUILD_BUG_ON(VMALLOC_START < KASAN_START_VADDR);
@@ -92,6 +95,9 @@ void __init kasan_init(void)
 	local_flush_tlb_all();
 	memset(kasan_early_shadow_page, 0, PAGE_SIZE);
 
+	/* KASAN is now initialized, enable it. */
+	static_branch_enable(&kasan_flag_enabled);
+
 	/* At this point kasan is fully initialized. Enable error messages. */
 	current->kasan_depth = 0;
 	pr_info("KernelAddressSanitizer initialized\n");
mm/kasan/sw_tags.c +6 -0
index 01f19bc4a326..dd963ba4d143 100644
@@ -40,11 +40,17 @@ void __init kasan_init_sw_tags(void)
 {
 	int cpu;
 
+	if (kasan_arg_disabled)
+		return;
+
 	for_each_possible_cpu(cpu)
 		per_cpu(prng_state, cpu) = (u32)get_cycles();
 
 	kasan_init_tags();
 
+	/* KASAN is now initialized, enable it. */
+	static_branch_enable(&kasan_flag_enabled);
+
 	pr_info("KernelAddressSanitizer initialized (sw-tags, stacktrace=%s)\n",
 		str_on_off(kasan_stack_collection_enabled()));
 }
Signature · 1 line
2.41.0
author
#6 lore raw

PATCH 4/4 mm/kasan: make kasan=on|off take effect for all three modes

In reply to Baoquan He

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

---
 include/linux/kasan-enabled.h | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)
include/linux/kasan-enabled.h +1 -10
index 32f2d19f599f..b5857e15ef14 100644
@@ -8,30 +8,21 @@ 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);
 }
 
+#ifdef CONFIG_KASAN_HW_TAGS
 static inline bool kasan_hw_tags_enabled(void)
 {
 	return kasan_enabled();
 }
-
 #else /* CONFIG_KASAN_HW_TAGS */
-
-static inline bool kasan_enabled(void)
-{
-	return IS_ENABLED(CONFIG_KASAN);
-}
-
 static inline bool kasan_hw_tags_enabled(void)
 {
 	return false;
 }
-
 #endif /* CONFIG_KASAN_HW_TAGS */
 
 #endif /* LINUX_KASAN_ENABLED_H */
Signature · 1 line
2.41.0
#7 lore raw

Re: [PATCH 4/4] mm/kasan: make kasan=on|off take effect for all three modes

Hello Baoqua,

Quoted 13 lines · On Tue, 5 Aug 2025 14:23:33 +0800 Baoquan He <[email protected]> wrote:

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

---
 include/linux/kasan-enabled.h | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)
include/linux/kasan-enabled.h +0 -0
index 32f2d19f599f..b5857e15ef14 100644

include/linux/kasan-enabled.h extern bool kasan_arg_disabled; · line 14

Quoted 9 lines
include/linux/kasan-enabled.h +0 -2
@@ -8,30 +8,21 @@ 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);
 }

I found mm-new build fails when CONFIG_KASAN is unset as below, and 'git bisect' points this patch.

      LD      .tmp_vmlinux1
    ld: lib/stackdepot.o:(__jump_table+0x8): undefined reference to `kasan_flag_enabled'

Since kasna_flag_enabled is defined in mm/kasan/common.c, I confirmed diff like below fixes this. I think it may not be a correct fix though, since I didn't read this patchset thoroughly.

include/linux/kasan-enabled.h +11 -0
index b5857e15ef14..a53d112b1020 100644
@@ -8,11 +8,22 @@ extern bool kasan_arg_disabled;
 
 DECLARE_STATIC_KEY_FALSE(kasan_flag_enabled);
 
+#ifdef CONFIG_KASAN
+
 static __always_inline bool kasan_enabled(void)
 {
 	return static_branch_likely(&kasan_flag_enabled);
 }
 
+#else /* CONFIG_KASAN */
+
+static inline bool kasan_enabled(void)
+{
+	return false;
+}
+
+#endif
+
 #ifdef CONFIG_KASAN_HW_TAGS
 static inline bool kasan_hw_tags_enabled(void)
 {

[...]

Thanks,
SJ

author
#8 lore raw

Re: [PATCH 4/4] mm/kasan: make kasan=on|off take effect for all three modes

Quoted 30 lines · SeongJae Park

Hello Baoqua,

Quoted 13 lines · On Tue, 5 Aug 2025 14:23:33 +0800 Baoquan He <[email protected]> wrote:

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

---
 include/linux/kasan-enabled.h | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)
include/linux/kasan-enabled.h +0 -0
index 32f2d19f599f..b5857e15ef14 100644

include/linux/kasan-enabled.h extern bool kasan_arg_disabled; · line 14

Quoted 9 lines
include/linux/kasan-enabled.h +0 -2
@@ -8,30 +8,21 @@ 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);
 }

I found mm-new build fails when CONFIG_KASAN is unset as below, and 'git bisect' points this patch.

Quoted 6 lines
      LD      .tmp_vmlinux1
    ld: lib/stackdepot.o:(__jump_table+0x8): undefined reference to `kasan_flag_enabled'

Since kasna_flag_enabled is defined in mm/kasan/common.c, I confirmed diff like below fixes this. I think it may not be a correct fix though, since I didn't read this patchset thoroughly.

Thanks a lot for the reporting and fix. The below code is great to fix the error. I reproduced it and tested with below fix, it works.

Since there's other reviewing comments, I will merge this into v2 post.

Quoted 35 lines
include/linux/kasan-enabled.h +11 -0
index b5857e15ef14..a53d112b1020 100644
@@ -8,11 +8,22 @@ extern bool kasan_arg_disabled;
 
 DECLARE_STATIC_KEY_FALSE(kasan_flag_enabled);
 
+#ifdef CONFIG_KASAN
+
 static __always_inline bool kasan_enabled(void)
 {
 	return static_branch_likely(&kasan_flag_enabled);
 }
 
+#else /* CONFIG_KASAN */
+
+static inline bool kasan_enabled(void)
+{
+	return false;
+}
+
+#endif
+
 #ifdef CONFIG_KASAN_HW_TAGS
 static inline bool kasan_hw_tags_enabled(void)
 {
 
 
[...]

Thanks,
SJ

#9 lore raw

Re: [PATCH 4/4] mm/kasan: make kasan=on|off take effect for all three modes

In reply to SeongJae Park
Quoted 20 lines · SeongJae Park

Hello Baoqua,

Quoted 16 lines · On Tue, 5 Aug 2025 14:23:33 +0800 Baoquan He <[email protected]> wrote:

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

---
 include/linux/kasan-enabled.h | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)
include/linux/kasan-enabled.h +0 -0
index 32f2d19f599f..b5857e15ef14 100644
@@ -8,30 +8,21 @@ extern bool kasan_arg_disabled;
 
 DECLARE_STATIC_KEY_FALSE(kasan_flag_enabled);
 
Quoted 9 lines

include/linux/kasan-enabled.h extern bool kasan_arg_disabled; · line 16

Quoted 6 lines
include/linux/kasan-enabled.h +0 -2
-#ifdef CONFIG_KASAN_HW_TAGS
-
 static __always_inline bool kasan_enabled(void)
 {
 	return static_branch_likely(&kasan_flag_enabled);
 }

I found mm-new build fails when CONFIG_KASAN is unset as below, and 'git bisect' points this patch.

Yup just hit this + bisected here.

Quoted 13 lines
      LD      .tmp_vmlinux1
    ld: lib/stackdepot.o:(__jump_table+0x8): undefined reference to `kasan_flag_enabled'

Since kasna_flag_enabled is defined in mm/kasan/common.c, I confirmed diff like below fixes this. I think it may not be a correct fix though, since I didn't read this patchset thoroughly.

include/linux/kasan-enabled.h +0 -0
index b5857e15ef14..a53d112b1020 100644

include/linux/kasan-enabled.h extern bool kasan_arg_disabled; · line 12

Quoted 5 lines
include/linux/kasan-enabled.h +2 -0
@@ -8,11 +8,22 @@ extern bool kasan_arg_disabled;
 
 DECLARE_STATIC_KEY_FALSE(kasan_flag_enabled);
 
+#ifdef CONFIG_KASAN
+

Shouldn't we put this above the static key declaration?

Feels like the whole header should be included really.

Quoted 21 lines
include/linux/kasan-enabled.h +9 -0
 static __always_inline bool kasan_enabled(void)
 {
 	return static_branch_likely(&kasan_flag_enabled);
 }
 
+#else /* CONFIG_KASAN */
+
+static inline bool kasan_enabled(void)
+{
+	return false;
+}
+
+#endif
+
 #ifdef CONFIG_KASAN_HW_TAGS
 static inline bool kasan_hw_tags_enabled(void)
 {
 
 
[...]
Quoted 3 lines

Thanks,
SJ

Cheers, Lorenzo

author
#10 lore raw

Re: [PATCH 4/4] mm/kasan: make kasan=on|off take effect for all three modes

Quoted 31 lines · Lorenzo Stoakes
Quoted 20 lines · SeongJae Park

Hello Baoqua,

Quoted 16 lines · On Tue, 5 Aug 2025 14:23:33 +0800 Baoquan He <[email protected]> wrote:

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

---
 include/linux/kasan-enabled.h | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)
include/linux/kasan-enabled.h +0 -0
index 32f2d19f599f..b5857e15ef14 100644
@@ -8,30 +8,21 @@ extern bool kasan_arg_disabled;
 
 DECLARE_STATIC_KEY_FALSE(kasan_flag_enabled);
 
Quoted 9 lines

include/linux/kasan-enabled.h extern bool kasan_arg_disabled; · line 16

Quoted 6 lines
include/linux/kasan-enabled.h +0 -2
-#ifdef CONFIG_KASAN_HW_TAGS
-
 static __always_inline bool kasan_enabled(void)
 {
 	return static_branch_likely(&kasan_flag_enabled);
 }

I found mm-new build fails when CONFIG_KASAN is unset as below, and 'git bisect' points this patch.

Quoted 1 line

Yup just hit this + bisected here.

Sorry for the trouble and thanks for reporting.

Quoted 15 lines
Quoted 15 lines

      LD      .tmp_vmlinux1
    ld: lib/stackdepot.o:(__jump_table+0x8): undefined reference to `kasan_flag_enabled'

Since kasna_flag_enabled is defined in mm/kasan/common.c, I confirmed diff like below fixes this. I think it may not be a correct fix though, since I didn't read this patchset thoroughly.

include/linux/kasan-enabled.h +0 -0
index b5857e15ef14..a53d112b1020 100644
@@ -8,11 +8,22 @@ extern bool kasan_arg_disabled;
 
Quoted 8 lines

include/linux/kasan-enabled.h extern bool kasan_arg_disabled; · line 12

Quoted 4 lines
include/linux/kasan-enabled.h +2 -0
 DECLARE_STATIC_KEY_FALSE(kasan_flag_enabled);
 
+#ifdef CONFIG_KASAN
+

Shouldn't we put this above the static key declaration?

Feels like the whole header should be included really.

You are right, kasan_flag_enabled should be included in CONFIG_KASAN ifdeffery scope.

Since CONFIG_KASAN_HW_TAGS depends on CONFIG_KASAN, we may not need include below CONFIG_KASAN_HW_TAGS ifdeffery into CONFIG_KASAN ifdeffery scope. Not sure if this is incorrect.

Thanks a lot for checking this.

Quoted 28 lines
Quoted 22 lines
include/linux/kasan-enabled.h +9 -0
 static __always_inline bool kasan_enabled(void)
 {
 	return static_branch_likely(&kasan_flag_enabled);
 }
 
+#else /* CONFIG_KASAN */
+
+static inline bool kasan_enabled(void)
+{
+	return false;
+}
+
+#endif
+
 #ifdef CONFIG_KASAN_HW_TAGS
 static inline bool kasan_hw_tags_enabled(void)
 {
 
 
[...]
Quoted 3 lines

Thanks,
SJ

Cheers, Lorenzo

#11 lore raw

Re: [PATCH 4/4] mm/kasan: make kasan=on|off take effect for all three modes

Quoted 7 lines · Baoquan He
Quoted 4 lines · Lorenzo Stoakes
Quoted 2 lines

I found mm-new build fails when CONFIG_KASAN is unset as below, and 'git bisect' points this patch.

Yup just hit this + bisected here.

Sorry for the trouble and thanks for reporting.

No worries!

Quoted 23 lines
Quoted 23 lines
Quoted 15 lines


      LD      .tmp_vmlinux1
    ld: lib/stackdepot.o:(__jump_table+0x8): undefined reference to `kasan_flag_enabled'

Since kasna_flag_enabled is defined in mm/kasan/common.c, I confirmed diff like below fixes this. I think it may not be a correct fix though, since I didn't read this patchset thoroughly.

include/linux/kasan-enabled.h +0 -0
index b5857e15ef14..a53d112b1020 100644

include/linux/kasan-enabled.h extern bool kasan_arg_disabled; · line 12

Quoted 5 lines
include/linux/kasan-enabled.h +2 -0
@@ -8,11 +8,22 @@ extern bool kasan_arg_disabled;
 
 DECLARE_STATIC_KEY_FALSE(kasan_flag_enabled);
 
+#ifdef CONFIG_KASAN
+

Shouldn't we put this above the static key declaration?

Quoted 4 lines
Quoted 1 line

Feels like the whole header should be included really.

You are right, kasan_flag_enabled should be included in CONFIG_KASAN ifdeffery scope.

Firstly I _LOVE_ the term 'ifdeffery scope'. Fantastic :)

Quoted 4 lines

Since CONFIG_KASAN_HW_TAGS depends on CONFIG_KASAN, we may not need include below CONFIG_KASAN_HW_TAGS ifdeffery into CONFIG_KASAN ifdeffery scope. Not sure if this is incorrect.

Well I don't think CONFIG_KASAN_HW_TAGS is necessarily implied right? So these should remain I think, just nested in CONFIG_KASAN, should be fine.

Quoted 2 lines

Thanks a lot for checking this.

No problem! Just ran in to it while doing other stuff in mm-new :)

Cheers, Lorenzo

author
#12 lore raw

Re: [PATCH 4/4] mm/kasan: make kasan=on|off take effect for all three modes

Quoted 2 lines · Lorenzo Stoakes
Quoted 1 line · Baoquan He

On 08/06/25 at 05:26pm, Lorenzo Stoakes wrote:

......

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

include/linux/kasan-enabled.h extern bool kasan_arg_disabled; · line 12

Quoted 10 lines
include/linux/kasan-enabled.h +2 -0
index b5857e15ef14..a53d112b1020 100644
@@ -8,11 +8,22 @@ extern bool kasan_arg_disabled;
 
 DECLARE_STATIC_KEY_FALSE(kasan_flag_enabled);
 
+#ifdef CONFIG_KASAN
+

Shouldn't we put this above the static key declaration?

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Feels like the whole header should be included really.

You are right, kasan_flag_enabled should be included in CONFIG_KASAN ifdeffery scope.

Firstly I _LOVE_ the term 'ifdeffery scope'. Fantastic :)

Learned from upstream people with expertise on both english and kernel, :-)

Quoted 8 lines
Quoted 5 lines

Since CONFIG_KASAN_HW_TAGS depends on CONFIG_KASAN, we may not need include below CONFIG_KASAN_HW_TAGS ifdeffery into CONFIG_KASAN ifdeffery scope. Not sure if this is incorrect.

Well I don't think CONFIG_KASAN_HW_TAGS is necessarily implied right? So these should remain I think, just nested in CONFIG_KASAN, should be fine.

After investigation, I keep the CONFIG_KASAN_HW_TAGS ifdeffery scope out of CONFIG_KASAN scope. Otherwise, I need define the dummy kasan_hw_tags_enabled() function twice. I am personally not fan of the style. While if that is preferred in kernel, I can change it.

#ifdef CONFIG_KASAN

#ifdef CONFIG_KASAN_HW_TAGS
......
#ifdef CONFIG_KASAN_HW_TAGS
static inline bool kasan_hw_tags_enabled(void)

{
        return kasan_enabled();
}
#else /* CONFIG_KASAN_HW_TAGS */
static inline bool kasan_hw_tags_enabled(void)
{
        return false;
}

#endif /* CONFIG_KASAN_HW_TAGS */
.....
#else /* CONFIG_KASAN */
static inline bool kasan_hw_tags_enabled(void)

{
        return false;
}

#endif

#13 lore raw

Re: [PATCH 4/4] mm/kasan: make kasan=on|off take effect for all three modes

Quoted 3 lines · Baoquan He
Quoted 1 line

Firstly I _LOVE_ the term 'ifdeffery scope'. Fantastic :)

Learned from upstream people with expertise on both english and kernel, :-)

:)

Quoted 19 lines

After investigation, I keep the CONFIG_KASAN_HW_TAGS ifdeffery scope out of CONFIG_KASAN scope. Otherwise, I need define the dummy kasan_hw_tags_enabled() function twice. I am personally not fan of the style. While if that is preferred in kernel, I can change it.

#ifdef CONFIG_KASAN

#ifdef CONFIG_KASAN_HW_TAGS
......
#ifdef CONFIG_KASAN_HW_TAGS
static inline bool kasan_hw_tags_enabled(void)

{
        return kasan_enabled();
}
#else /* CONFIG_KASAN_HW_TAGS */
static inline bool kasan_hw_tags_enabled(void)
{
        return false;
}
Quoted 9 lines

#endif /* CONFIG_KASAN_HW_TAGS */
.....
#else /* CONFIG_KASAN */
static inline bool kasan_hw_tags_enabled(void)

{
        return false;
}

#endif

This is fine, as CONFIG_KASAN_HW_TAGS implies CONFIG_KASAN anyway.

#14 lore raw

Re: [PATCH 4/4] mm/kasan: make kasan=on|off take effect for all three modes

In reply to Baoquan He
kernel test robot noticed the following build errors:

Hi Baoquan,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Baoquan-He/mm-kasan-add-conditional-checks-in-functions-to-return-directly-if-kasan-is-disabled/20250806-104340
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20250805062333.121553-5-bhe%40redhat.com
patch subject: [PATCH 4/4] mm/kasan: make kasan=on|off take effect for all three modes
config: i386-buildonly-randconfig-003-20250806 (https://download.01.org/0day-ci/archive/20250806/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250806/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> ld.lld: error: undefined symbol: kasan_flag_enabled
   >>> referenced by stackdepot.c
   >>>               lib/stackdepot.o:(__jump_table+0x8) in archive vmlinux.a
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#15 lore raw

Re: [PATCH 4/4] mm/kasan: make kasan=on|off take effect for all three modes

In reply to Baoquan He
Quoted 4 lines · Baoquan He

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

Hello,

I've been working on this already and a different approach with the Kconfig ARCH_DEFER_KASAN has been proposed.

Please see v4 thread.
https://lore.kernel.org/all/[email protected]/

It also covers the printing in a single KASAN codebase, instead of printing "KASAN intiilaized" in arch/* code.
Also covers the enabling KASAN via kasan_enable() for all 3 modes.

It's up to KASAN maintainers to choose either version.
I just need the confirmation now if I should proceed with v5, or your version if it covers all arch and cases should be picked up.

Thanks

Quoted 45 lines
---
 include/linux/kasan-enabled.h | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)
include/linux/kasan-enabled.h +1 -11
index 32f2d19f599f..b5857e15ef14 100644
@@ -8,30 +8,21 @@ 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);
 }
 
+#ifdef CONFIG_KASAN_HW_TAGS
 static inline bool kasan_hw_tags_enabled(void)
 {
        return kasan_enabled();
 }
-
 #else /* CONFIG_KASAN_HW_TAGS */
-
-static inline bool kasan_enabled(void)
-{
-       return IS_ENABLED(CONFIG_KASAN);
-}
-
 static inline bool kasan_hw_tags_enabled(void)
 {
        return false;
 }
-
 #endif /* CONFIG_KASAN_HW_TAGS */
 
 #endif /* LINUX_KASAN_ENABLED_H */
--

2.41.0

author
#16 lore raw

Re: [PATCH 4/4] mm/kasan: make kasan=on|off take effect for all three modes

Quoted 10 lines · Sabyrzhan Tasbolatov
Quoted 4 lines · Baoquan He

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

Hello,

I've been working on this already and a different approach with the Kconfig ARCH_DEFER_KASAN has been proposed.

Thanks for telling, I don't always watch MM mailing list, so missed your earlier posting.

I went through your v5 series, we are doing different work. I am adding kasan=on|off to generic/sw_tags, and have added kasan_enabled() to needed places. In fact, based on this patchset, we can remove kasan_arch_is_ready() more easily since in all places kasan_enabled() has been added there. Before seeing your patches, this is what I planned to do to remove kasan_arch_is_ready(). I will see what can be done better. Maybe I can carry your patch in v2. I will try tomorrow.

Quoted 60 lines

Please see v4 thread.
https://lore.kernel.org/all/[email protected]/

It also covers the printing in a single KASAN codebase, instead of printing "KASAN intiilaized" in arch/* code.
Also covers the enabling KASAN via kasan_enable() for all 3 modes.

It's up to KASAN maintainers to choose either version.
I just need the confirmation now if I should proceed with v5, or your version if it covers all arch and cases should be picked up.

Thanks

Quoted 45 lines
---
 include/linux/kasan-enabled.h | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)
include/linux/kasan-enabled.h +1 -11
index 32f2d19f599f..b5857e15ef14 100644
@@ -8,30 +8,21 @@ 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);
 }
 
+#ifdef CONFIG_KASAN_HW_TAGS
 static inline bool kasan_hw_tags_enabled(void)
 {
        return kasan_enabled();
 }
-
 #else /* CONFIG_KASAN_HW_TAGS */
-
-static inline bool kasan_enabled(void)
-{
-       return IS_ENABLED(CONFIG_KASAN);
-}
-
 static inline bool kasan_hw_tags_enabled(void)
 {
        return false;
 }
-
 #endif /* CONFIG_KASAN_HW_TAGS */
 
 #endif /* LINUX_KASAN_ENABLED_H */
--

2.41.0

#17 lore raw
In reply to Baoquan He
Quoted 9 lines · Baoquan He' via kasan-dev

Currently only hw_tags mode of kasan can be enabled or disabled with kernel parameter kasan=on|off for built kernel. For kasan generic and sw_tags mode, there's no way to disable them once kernel is built. This is not convenient sometime, e.g in system kdump is configured. When the 1st kernel has KASAN enabled and crash triggered to switch to kdump kernel, the generic or sw_tags mode will cost much extra memory for kasan shadow while in fact it's meaningless to have kasan in kdump kernel.

Are you using KASAN generic or SW-tags is production?
If in a test environment, is the overhead of the kdump kernel really unacceptable?

Quoted 9 lines

So this patchset moves the kasan=on|off out of hw_tags scope and into common code to make it visible in generic and sw_tags mode too. Then we can add kasan=off in kdump kernel to reduce the unneeded meomry cost for kasan.

Test:
=====
I only took test on x86_64 for generic mode, and on arm64 for generic, sw_tags and hw_tags mode. All of them works well.

Does it also work for CONFIG_KASAN_INLINE?

Quoted 44 lines

However when I tested sw_tags on a HPE apollo arm64 machine, it always breaks kernel with a KASAN bug. Even w/o this patchset applied, the bug can always be seen too.

"BUG: KASAN: invalid-access in pcpu_alloc_noprof+0x42c/0x9a8"

I haven't got root cause of the bug, will report the bug later in another thread.
====

Baoquan He (4):
mm/kasan: add conditional checks in functions to return directly if
kasan is disabled
mm/kasan: move kasan= code to common place
mm/kasan: don't initialize kasan if it's disabled mm/kasan: make kasan=on|off take effect for all three modes

 arch/arm/mm/kasan_init.c               |  6 +++++
 arch/arm64/mm/kasan_init.c             |  7 ++++++
 arch/loongarch/mm/kasan_init.c         |  5 ++++
 arch/powerpc/mm/kasan/init_32.c        |  8 +++++-
 arch/powerpc/mm/kasan/init_book3e_64.c |  6 +++++
 arch/powerpc/mm/kasan/init_book3s_64.c |  6 +++++
 arch/riscv/mm/kasan_init.c             |  6 +++++
 arch/um/kernel/mem.c                   |  6 +++++
 arch/x86/mm/kasan_init_64.c            |  6 +++++
 arch/xtensa/mm/kasan_init.c            |  6 +++++
 include/linux/kasan-enabled.h          | 11 ++------
 mm/kasan/common.c                      | 27 ++++++++++++++++++++
 mm/kasan/generic.c                     | 20 +++++++++++++--
 mm/kasan/hw_tags.c                     | 35 ++------------------------
 mm/kasan/init.c                        |  6 +++++
 mm/kasan/quarantine.c                  |  3 +++
 mm/kasan/shadow.c                      | 23 ++++++++++++++++-
 mm/kasan/sw_tags.c                     |  9 +++++++
 18 files changed, 150 insertions(+), 46 deletions(-)

--
2.41.0

--
You received this message because you are subscribed to the Google Groups "kasan-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion visit https://groups.google.com/d/msgid/kasan-dev/20250805062333.121553-1-bhe%40redhat.com.

maintainer
#18 lore raw
Quoted 12 lines · Marco Elver
Quoted 9 lines · Baoquan He' via kasan-dev

Currently only hw_tags mode of kasan can be enabled or disabled with kernel parameter kasan=on|off for built kernel. For kasan generic and sw_tags mode, there's no way to disable them once kernel is built. This is not convenient sometime, e.g in system kdump is configured. When the 1st kernel has KASAN enabled and crash triggered to switch to kdump kernel, the generic or sw_tags mode will cost much extra memory for kasan shadow while in fact it's meaningless to have kasan in kdump kernel.

Quoted 4 lines

Are you using KASAN generic or SW-tags is production?
If in a test environment, is the overhead of the kdump kernel really unacceptable?

kdump kernel operates with limited amount of memory, whatever was provided in 'crashkernel=' for the primary kernel. So it's quite easily can ran out of memory.

By default kdump uses same as currently running kernel, but it can be configured to use a different one.

At least in fedora it's in /etc/sysconfig/kdump:

$ cat /etc/sysconfig/kdump
# Kernel Version string for the -kdump kernel, such as 2.6.13-1544.FC5kdump
# If no version is specified, then the init script will try to find a
# kdump kernel with the same version number as the running kernel.
KDUMP_KERNELVER=""
Quoted 5 lines
Quoted 5 lines

So this patchset moves the kasan=on|off out of hw_tags scope and into common code to make it visible in generic and sw_tags mode too. Then we can add kasan=off in kdump kernel to reduce the unneeded meomry cost for kasan.

Quoted 7 lines
Quoted 4 lines

Test:
=====
I only took test on x86_64 for generic mode, and on arm64 for generic, sw_tags and hw_tags mode. All of them works well.

Does it also work for CONFIG_KASAN_INLINE?

I think it should. Because we don't initialize init_task.kasan_depth we always bail out in kasan_report().

author
#19 lore raw
In reply to Marco Elver
Quoted 12 lines · Marco Elver
Quoted 9 lines · Baoquan He' via kasan-dev

Currently only hw_tags mode of kasan can be enabled or disabled with kernel parameter kasan=on|off for built kernel. For kasan generic and sw_tags mode, there's no way to disable them once kernel is built. This is not convenient sometime, e.g in system kdump is configured. When the 1st kernel has KASAN enabled and crash triggered to switch to kdump kernel, the generic or sw_tags mode will cost much extra memory for kasan shadow while in fact it's meaningless to have kasan in kdump kernel.

Quoted 3 lines

Are you using KASAN generic or SW-tags is production?
If in a test environment, is the overhead of the kdump kernel really unacceptable?

Thanks for checking this.

I don't use KASAN in production environment. But in Redhat, our CI will run test cases on debug kernel with KASAN enabled by default. Then the crashkernel setting will be uncertain. E.g usually crashkernel=256M is enough for most of system. However, KASAN would make the crashkernel reservation need to reach to 768M on one ampere arm64 system. This is not the extra 1/8 of system ram as we expected because we have vmalloc mapping to create shaddow too. In this case, QE or other kernel developer who is not familiar with KASAN may need spend time to dig out what's going on here. And they may need adjust crashkernel= value to get an appropriate one to make system work. This is not good because we don't need KASAN feature in kdump kernel at all while we need tackle the unexpected crashkernel= setting.

This can be fixed with a very easy way, a knob to disable kasan in kdump kernel can perfectly handle it.

Quoted 6 lines
Quoted 6 lines

So this patchset moves the kasan=on|off out of hw_tags scope and into common code to make it visible in generic and sw_tags mode too. Then we can add kasan=off in kdump kernel to reduce the unneeded meomry cost for kasan.

Quoted 6 lines
Quoted 4 lines

Test:
=====
I only took test on x86_64 for generic mode, and on arm64 for generic, sw_tags and hw_tags mode. All of them works well.

Does it also work for CONFIG_KASAN_INLINE?

Yes, Andrey said in reply, I did investigation. You can see that KASAN_INLINE will bloat vmlinux by ~30M. This is not a big problem of kdump kernel.

CONFIG_KASAN_OUTLINE=y
[root@ampere-mtsnow-altra-08 linux]# ll vmlinux
-rwxr-xr-x. 1 root root 124859016 Aug 6 06:08 vmlinux
[root@ampere-mtsnow-altra-08 linux]# ll /boot/vmlinuz-*
-rwxr-xr-x. 1 root root 15938048 Aug 3 00:15 /boot/vmlinuz-0-rescue-f81ab6a509e444e3857153cfa3fc6497 -rwxr-xr-x. 1 root root 15938048 Jul 23 20:00 /boot/vmlinuz-6.15.8-200.fc42.aarch64 -rwxr-xr-x. 1 root root 20644352 Aug 6 06:11 /boot/vmlinuz-6.16.0+

CONFIG_KASAN_INLINE=y [root@ampere-mtsnow-altra-08 linux]# ll vmlinux -rwxr-xr-x. 1 root root 150483592 Aug 6 10:53 vmlinux [root@ampere-mtsnow-altra-08 linux]# ll /boot/vmlinuz-*

-rwxr-xr-x. 1 root root  15938048 Aug  3 00:15 /boot/vmlinuz-0-rescue-f81ab6a509e444e3857153cfa3fc6497
-rwxr-xr-x. 1 root root  15938048 Jul 23 20:00 /boot/vmlinuz-6.15.8-200.fc42.aarch64
-rwxr-xr-x. 1 root root  27779584 Aug  6 10:55 /boot/vmlinuz-6.16.0+
Quoted 46 lines
Quoted 45 lines

However when I tested sw_tags on a HPE apollo arm64 machine, it always breaks kernel with a KASAN bug. Even w/o this patchset applied, the bug can always be seen too.

"BUG: KASAN: invalid-access in pcpu_alloc_noprof+0x42c/0x9a8"

I haven't got root cause of the bug, will report the bug later in another thread.
====

Baoquan He (4):
mm/kasan: add conditional checks in functions to return directly if
kasan is disabled
mm/kasan: move kasan= code to common place
mm/kasan: don't initialize kasan if it's disabled mm/kasan: make kasan=on|off take effect for all three modes

 arch/arm/mm/kasan_init.c               |  6 +++++
 arch/arm64/mm/kasan_init.c             |  7 ++++++
 arch/loongarch/mm/kasan_init.c         |  5 ++++
 arch/powerpc/mm/kasan/init_32.c        |  8 +++++-
 arch/powerpc/mm/kasan/init_book3e_64.c |  6 +++++
 arch/powerpc/mm/kasan/init_book3s_64.c |  6 +++++
 arch/riscv/mm/kasan_init.c             |  6 +++++
 arch/um/kernel/mem.c                   |  6 +++++
 arch/x86/mm/kasan_init_64.c            |  6 +++++
 arch/xtensa/mm/kasan_init.c            |  6 +++++
 include/linux/kasan-enabled.h          | 11 ++------
 mm/kasan/common.c                      | 27 ++++++++++++++++++++
 mm/kasan/generic.c                     | 20 +++++++++++++--
 mm/kasan/hw_tags.c                     | 35 ++------------------------
 mm/kasan/init.c                        |  6 +++++
 mm/kasan/quarantine.c                  |  3 +++
 mm/kasan/shadow.c                      | 23 ++++++++++++++++-
 mm/kasan/sw_tags.c                     |  9 +++++++
 18 files changed, 150 insertions(+), 46 deletions(-)

--
2.41.0

--
You received this message because you are subscribed to the Google Groups "kasan-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion visit https://groups.google.com/d/msgid/kasan-dev/20250805062333.121553-1-bhe%40redhat.com.

maintainer
#20 lore raw
In reply to Baoquan He
Quoted 9 lines · Baoquan He

Currently only hw_tags mode of kasan can be enabled or disabled with kernel parameter kasan=on|off for built kernel. For kasan generic and sw_tags mode, there's no way to disable them once kernel is built. This is not convenient sometime, e.g in system kdump is configured. When the 1st kernel has KASAN enabled and crash triggered to switch to kdump kernel, the generic or sw_tags mode will cost much extra memory for kasan shadow while in fact it's meaningless to have kasan in kdump kernel.

Ideally this problem should be solved by having kdump kernel with different config. Because if we want only reliably collect crash dumps, than we probably don't want other debug features, e.g. like VM_BUG_ON() crashing our kdump kernel.

Quoted 47 lines

So this patchset moves the kasan=on|off out of hw_tags scope and into common code to make it visible in generic and sw_tags mode too. Then we can add kasan=off in kdump kernel to reduce the unneeded meomry cost for kasan.

Test:
=====
I only took test on x86_64 for generic mode, and on arm64 for generic, sw_tags and hw_tags mode. All of them works well.

However when I tested sw_tags on a HPE apollo arm64 machine, it always breaks kernel with a KASAN bug. Even w/o this patchset applied, the bug can always be seen too.

"BUG: KASAN: invalid-access in pcpu_alloc_noprof+0x42c/0x9a8"

I haven't got root cause of the bug, will report the bug later in another thread.
====

Baoquan He (4):
mm/kasan: add conditional checks in functions to return directly if
kasan is disabled
mm/kasan: move kasan= code to common place
mm/kasan: don't initialize kasan if it's disabled mm/kasan: make kasan=on|off take effect for all three modes

 arch/arm/mm/kasan_init.c               |  6 +++++
 arch/arm64/mm/kasan_init.c             |  7 ++++++
 arch/loongarch/mm/kasan_init.c         |  5 ++++
 arch/powerpc/mm/kasan/init_32.c        |  8 +++++-
 arch/powerpc/mm/kasan/init_book3e_64.c |  6 +++++
 arch/powerpc/mm/kasan/init_book3s_64.c |  6 +++++
 arch/riscv/mm/kasan_init.c             |  6 +++++
 arch/um/kernel/mem.c                   |  6 +++++
 arch/x86/mm/kasan_init_64.c            |  6 +++++
 arch/xtensa/mm/kasan_init.c            |  6 +++++
 include/linux/kasan-enabled.h          | 11 ++------
 mm/kasan/common.c                      | 27 ++++++++++++++++++++
 mm/kasan/generic.c                     | 20 +++++++++++++--
 mm/kasan/hw_tags.c                     | 35 ++------------------------
 mm/kasan/init.c                        |  6 +++++
 mm/kasan/quarantine.c                  |  3 +++
 mm/kasan/shadow.c                      | 23 ++++++++++++++++-
 mm/kasan/sw_tags.c                     |  9 +++++++
 18 files changed, 150 insertions(+), 46 deletions(-)
author
#21 lore raw
Quoted 13 lines · Andrey Ryabinin
Quoted 9 lines · Baoquan He

Currently only hw_tags mode of kasan can be enabled or disabled with kernel parameter kasan=on|off for built kernel. For kasan generic and sw_tags mode, there's no way to disable them once kernel is built. This is not convenient sometime, e.g in system kdump is configured. When the 1st kernel has KASAN enabled and crash triggered to switch to kdump kernel, the generic or sw_tags mode will cost much extra memory for kasan shadow while in fact it's meaningless to have kasan in kdump kernel.

Quoted 3 lines

Ideally this problem should be solved by having kdump kernel with different config. Because if we want only reliably collect crash dumps, than we probably don't want other debug features, e.g. like VM_BUG_ON() crashing our kdump kernel.

Yeah, we have done that in Redhat's internal CI testing. While we still want to switch back to let kdump take the same kernel as the 1st kernel. Like this, we have chance to test debug kernel for vmcore dumping. In this case, KASAN is the main barrier. For other debug features, VM_BUG_ON() should be captured in 1st kernel's running, we won't wait to run kdump kernel to catch it. I am planning to check and adding feature switch for kdump to disable if it's not needed in kdump kernel. E.g I have done in ima=on|off, and the existing 'kfence.sample_interval=0' for kfence.

And the public kasan=on|off kernel parameter can make kasan feature more flexible. It can be used in production environment with kasan=off, and can switch to the same kernel to catch issues easily by stripping the cmdline setting. As adding a cmdline is much easier than setting kernel config and rebuild kernel.

Besides, based on this patchset, we can easily remove kasan_arch_is_ready() by detecting the arch's support and disable kasan_flag_enabled. And when I testing generic/sw_tags/hw_tags on arm64, I feel if adding a kernel parameter for choosing different KASAN mode is much more convenient than changing kernel config and rebuild. If we choose to KASAN_OUTLINE, this even doesn't impact much in production environment. I would like to hear your suggestion.

Thanks
Baoquan

Quoted 50 lines
Quoted 49 lines

So this patchset moves the kasan=on|off out of hw_tags scope and into common code to make it visible in generic and sw_tags mode too. Then we can add kasan=off in kdump kernel to reduce the unneeded meomry cost for kasan.

Test:
=====
I only took test on x86_64 for generic mode, and on arm64 for generic, sw_tags and hw_tags mode. All of them works well.

However when I tested sw_tags on a HPE apollo arm64 machine, it always breaks kernel with a KASAN bug. Even w/o this patchset applied, the bug can always be seen too.

"BUG: KASAN: invalid-access in pcpu_alloc_noprof+0x42c/0x9a8"

I haven't got root cause of the bug, will report the bug later in another thread.
====

Baoquan He (4):
mm/kasan: add conditional checks in functions to return directly if
kasan is disabled
mm/kasan: move kasan= code to common place
mm/kasan: don't initialize kasan if it's disabled mm/kasan: make kasan=on|off take effect for all three modes

 arch/arm/mm/kasan_init.c               |  6 +++++
 arch/arm64/mm/kasan_init.c             |  7 ++++++
 arch/loongarch/mm/kasan_init.c         |  5 ++++
 arch/powerpc/mm/kasan/init_32.c        |  8 +++++-
 arch/powerpc/mm/kasan/init_book3e_64.c |  6 +++++
 arch/powerpc/mm/kasan/init_book3s_64.c |  6 +++++
 arch/riscv/mm/kasan_init.c             |  6 +++++
 arch/um/kernel/mem.c                   |  6 +++++
 arch/x86/mm/kasan_init_64.c            |  6 +++++
 arch/xtensa/mm/kasan_init.c            |  6 +++++
 include/linux/kasan-enabled.h          | 11 ++------
 mm/kasan/common.c                      | 27 ++++++++++++++++++++
 mm/kasan/generic.c                     | 20 +++++++++++++--
 mm/kasan/hw_tags.c                     | 35 ++------------------------
 mm/kasan/init.c                        |  6 +++++
 mm/kasan/quarantine.c                  |  3 +++
 mm/kasan/shadow.c                      | 23 ++++++++++++++++-
 mm/kasan/sw_tags.c                     |  9 +++++++
 18 files changed, 150 insertions(+), 46 deletions(-)