[PATCH v3 09/12] arch/x86: don't initialize kasan if it's disabled

Baoquan He
2025-08-20T05:36:42Z

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

Signed-off-by: Baoquan He <bhe@redhat.com>
Cc: x86@kernel.org
---
arch/x86/mm/kasan_init_64.c | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/arch/x86/mm/kasan_init_64.c b/arch/x86/mm/kasan_init_64.c
index 0539efd0d216..0f2f9311e9df 100644
--- a/arch/x86/mm/kasan_init_64.c
+++ b/arch/x86/mm/kasan_init_64.c
@@ -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");
}
--
2.41.0