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

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

This includes 32bit, book3s/64 and book3e/64.

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

Signed-off-by: Baoquan He <bhe@redhat.com>
Cc: linuxppc-dev@lists.ozlabs.org
---
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 ++++++
3 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/mm/kasan/init_32.c b/arch/powerpc/mm/kasan/init_32.c
index 03666d790a53..b0c465f3fbf5 100644
--- a/arch/powerpc/mm/kasan/init_32.c
+++ b/arch/powerpc/mm/kasan/init_32.c
@@ -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();
}

diff --git a/arch/powerpc/mm/kasan/init_book3e_64.c b/arch/powerpc/mm/kasan/init_book3e_64.c
index 60c78aac0f63..1e1c10467a2b 100644
--- a/arch/powerpc/mm/kasan/init_book3e_64.c
+++ b/arch/powerpc/mm/kasan/init_book3e_64.c
@@ -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");
diff --git a/arch/powerpc/mm/kasan/init_book3s_64.c b/arch/powerpc/mm/kasan/init_book3s_64.c
index 7d959544c077..9c5cf2354c8b 100644
--- a/arch/powerpc/mm/kasan/init_book3s_64.c
+++ b/arch/powerpc/mm/kasan/init_book3s_64.c
@@ -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");
--
2.41.0