From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AE20C1DE3DC for ; Thu, 26 Jun 2025 22:22:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750976557; cv=none; b=E6F2B2Y5CAfPYbwpXkzNSlb6lXQUVsMYcl9b6feUNlphYLeB1+L+KsQ/klYMfAI1HrEQHGtp/pNUnxqbywjNHy06sI+gzfid57C+szg6DD+psBywWNfM7bG3WRjjp/wwVVBnq4grxWeDy8dvTXWwhjNz89MfVJYftmy8yD0cSGs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750976557; c=relaxed/simple; bh=6xwf0LURCOPYIj1Z7S8t4FMR/3lRWidfrRvFf7y9Zp4=; h=Date:To:From:Subject:Message-Id; b=mk77Yidy7/FURNgnPMVy3YhETgPMI+co98SM/1QzdWOx4dC6qmiELqhGczvM5hd7ousp52T2aRsoipqkxjltA/lqTDp/E2KmIIBjUZ9AuCHGIfKufIa8WDkkPAdq7ZG5ZIZyyXjuClJ22usNRpr/vy81vbs8ACRo/VYxPFfkxIw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b=aL59SUEC; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b="aL59SUEC" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 11F4FC4CEEB; Thu, 26 Jun 2025 22:22:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1750976556; bh=6xwf0LURCOPYIj1Z7S8t4FMR/3lRWidfrRvFf7y9Zp4=; h=Date:To:From:Subject:From; b=aL59SUEC2IiCaLvzY2wnqprqE0z+8h/f2UOFgZdv8E91sIAGU1mqMAH/WflwqYWp3 iKgaXAR9tOZphH3getB7UlDoSmQ12HnaB5Hui5kRy7VfywMPnX0DRonnDVnTxfLqd5 XVIioccLaYa+OL9XfpmOPI+AsqDYLDOkvmGbs6c8= Date: Thu, 26 Jun 2025 15:22:35 -0700 To: mm-commits@vger.kernel.org,vincenzo.frascino@arm.com,glider@google.com,dvyukov@google.com,andreyknvl@gmail.com,snovitoll@gmail.com,akpm@linux-foundation.org From: Andrew Morton Subject: + kasan-unify-static-kasan_flag_enabled-across-modes.patch added to mm-new branch Message-Id: <20250626222236.11F4FC4CEEB@smtp.kernel.org> Precedence: bulk X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: The patch titled Subject: kasan: unify static kasan_flag_enabled across modes has been added to the -mm mm-new branch. Its filename is kasan-unify-static-kasan_flag_enabled-across-modes.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/kasan-unify-static-kasan_flag_enabled-across-modes.patch This patch will later appear in the mm-new branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new. Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via the mm-everything branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there every 2-3 working days ------------------------------------------------------ From: Sabyrzhan Tasbolatov Subject: kasan: unify static kasan_flag_enabled across modes Date: Thu, 26 Jun 2025 20:31:37 +0500 Patch series "Patch series "kasan: unify kasan_arch_is_ready with kasan_enabled"", v2. This patch series unifies the kasan_arch_is_ready() and kasan_enabled() interfaces by extending the existing kasan_enabled() infrastructure to work consistently across all KASAN modes (Generic, SW_TAGS, HW_TAGS). Currently, kasan_enabled() only works for HW_TAGS mode using a static key, while other modes either return IS_ENABLED(CONFIG_KASAN) (compile-time constant) or rely on architecture-specific kasan_arch_is_ready() implementations with custom static keys and global variables. This leads to: - Code duplication across architectures - Inconsistent runtime behavior between KASAN modes - Architecture-specific readiness tracking After this series: - All KASAN modes use the same kasan_flag_enabled static key - Consistent runtime enable/disable behavior across modes - Simplified architecture code with unified kasan_init_generic() calls - Elimination of arch specific kasan_arch_is_ready() implementations - Unified vmalloc integration using kasan_enabled() checks This addresses the bugzilla issue [1] about making kasan_flag_enabled and kasan_enabled() work for Generic mode, and extends it to provide true unification across all modes. [1] https://bugzilla.kernel.org/show_bug.cgi?id=217049 === Current mainline KUnit status To see if there is any regression, I've tested first on the following commit 739a6c93cc75 ("Merge tag 'nfsd-6.16-1' of git://git.kernel.org/pub/scm/linux/kernel/git/cel/linux"). Tested via compiling a kernel with CONFIG_KASAN_KUNIT_TEST and running QEMU VM. There are failing tests in SW_TAGS and GENERIC modes in arm64: arm64 CONFIG_KASAN_HW_TAGS: # kasan: pass:62 fail:0 skip:13 total:75 # Totals: pass:62 fail:0 skip:13 total:75 ok 1 kasan arm64 CONFIG_KASAN_SW_TAGS=y: # kasan: pass:65 fail:1 skip:9 total:75 # Totals: pass:65 fail:1 skip:9 total:75 not ok 1 kasan # kasan_strings: EXPECTATION FAILED at mm/kasan/kasan_test_c.c:1598 KASAN failure expected in "strscpy(ptr, src + KASAN_GRANULE_SIZE, KASAN_GRANULE_SIZE)", but none occurred arm64 CONFIG_KASAN_GENERIC=y, CONFIG_KASAN_OUTLINE=y: # kasan: pass:61 fail:1 skip:13 total:75 # Totals: pass:61 fail:1 skip:13 total:75 not ok 1 kasan # same failure as above x86_64 CONFIG_KASAN_GENERIC=y: # kasan: pass:58 fail:0 skip:17 total:75 # Totals: pass:58 fail:0 skip:17 total:75 ok 1 kasan === Testing with patches Testing in v2: - Compiled every affected arch with no errors: $ make CC=clang LD=ld.lld AR=llvm-ar NM=llvm-nm STRIP=llvm-strip \ OBJCOPY=llvm-objcopy OBJDUMP=llvm-objdump READELF=llvm-readelf \ HOSTCC=clang HOSTCXX=clang++ HOSTAR=llvm-ar HOSTLD=ld.lld \ ARCH=$ARCH $ clang --version ClangBuiltLinux clang version 19.1.4 Target: x86_64-unknown-linux-gnu Thread model: posix - make ARCH=um produces the warning during compiling: MODPOST Module.symvers WARNING: modpost: vmlinux: section mismatch in reference: \ kasan_init+0x43 (section: .ltext) -> \ kasan_init_generic (section: .init.text) AFAIU, it's due to the code in arch/um/kernel/mem.c, where kasan_init() is placed in own section ".kasan_init", which calls kasan_init_generic() which is marked with "__init". - Booting via qemu-system- and running KUnit tests: * arm64 (GENERIC, HW_TAGS, SW_TAGS): no regression, same above results. * x86_64 (GENERIC): no regression, no errors === NB I haven't tested the kernel boot on the following arch. due to the absence of qemu-system- support on those arch on my machine, so I defer this to relevant arch people to test KASAN initialization: - loongarch - s390 - um - xtensa - powerpc - riscv This patch (of 11): Historically, the runtime static key kasan_flag_enabled existed only for CONFIG_KASAN_HW_TAGS mode. Generic and SW_TAGS modes either relied on architecture-specific kasan_arch_is_ready() implementations or evaluated KASAN checks unconditionally, leading to code duplication. This patch unifies the approach by: 1. Moving kasan_flag_enabled declaration under CONFIG_KASAN (all modes) instead of only CONFIG_KASAN_HW_TAGS 2. Moving the static key definition to common.c for shared usage 3. Adding kasan_init_generic() function that enables the static key and handles initialization for Generic mode 4. Updating SW_TAGS mode to enable the unified static key 5. Removing the duplicate static key definition from HW_TAGS After this change, all KASAN modes use the same underlying static key infrastructure. The kasan_enabled() function now provides consistent runtime enable behavior across Generic, SW_TAGS, and HW_TAGS modes. This maintains a backward compatibility - existing architecture code continues to work unchanged, but now benefits from the unified runtime control mechanism. The architecture-specific kasan_arch_is_ready() implementations can be gradually replaced with calls to the new kasan_init_generic() function. Link: https://lkml.kernel.org/r/20250626153147.145312-1-snovitoll@gmail.com Link: https://lkml.kernel.org/r/20250626153147.145312-2-snovitoll@gmail.com Signed-off-by: Sabyrzhan Tasbolatov Closes: https://bugzilla.kernel.org/show_bug.cgi?id=218315 Cc: Alexander Potapenko Cc: Andrey Konovalov Cc: Dmitry Vyukov Cc: Vincenzo Frascino Signed-off-by: Andrew Morton --- include/linux/kasan-enabled.h | 10 ++++++++-- include/linux/kasan.h | 6 ++++++ mm/kasan/common.c | 7 +++++++ mm/kasan/generic.c | 11 +++++++++++ mm/kasan/hw_tags.c | 7 ------- mm/kasan/sw_tags.c | 2 ++ 6 files changed, 34 insertions(+), 9 deletions(-) --- a/include/linux/kasan-enabled.h~kasan-unify-static-kasan_flag_enabled-across-modes +++ a/include/linux/kasan-enabled.h @@ -4,9 +4,15 @@ #include -#ifdef CONFIG_KASAN_HW_TAGS - +#ifdef CONFIG_KASAN +/* + * Global runtime flag. Starts ‘false’; switched to ‘true’ by + * the appropriate kasan_init_*() once KASAN is fully initialized. + */ DECLARE_STATIC_KEY_FALSE(kasan_flag_enabled); +#endif + +#ifdef CONFIG_KASAN_HW_TAGS static __always_inline bool kasan_enabled(void) { --- a/include/linux/kasan.h~kasan-unify-static-kasan_flag_enabled-across-modes +++ a/include/linux/kasan.h @@ -543,6 +543,12 @@ void kasan_report_async(void); #endif /* CONFIG_KASAN_HW_TAGS */ +#ifdef CONFIG_KASAN_GENERIC +void __init kasan_init_generic(void); +#else +static inline void kasan_init_generic(void) { } +#endif + #ifdef CONFIG_KASAN_SW_TAGS void __init kasan_init_sw_tags(void); #else --- a/mm/kasan/common.c~kasan-unify-static-kasan_flag_enabled-across-modes +++ a/mm/kasan/common.c @@ -32,6 +32,13 @@ #include "kasan.h" #include "../slab.h" +/* + * Definition of the unified static key declared in kasan-enabled.h. + * This provides consistent runtime enable/disable across all KASAN modes. + */ +DEFINE_STATIC_KEY_FALSE(kasan_flag_enabled); +EXPORT_SYMBOL(kasan_flag_enabled); + struct slab *kasan_addr_to_slab(const void *addr) { if (virt_addr_valid(addr)) --- a/mm/kasan/generic.c~kasan-unify-static-kasan_flag_enabled-across-modes +++ a/mm/kasan/generic.c @@ -37,6 +37,17 @@ #include "../slab.h" /* + * Initialize Generic KASAN and enable runtime checks. + * This should be called from arch kasan_init() once shadow memory is ready. + */ +void __init kasan_init_generic(void) +{ + static_branch_enable(&kasan_flag_enabled); + + pr_info("KernelAddressSanitizer initialized (generic)\n"); +} + +/* * All functions below always inlined so compiler could * perform better optimizations in each of __asan_loadX/__assn_storeX * depending on memory access size X. --- a/mm/kasan/hw_tags.c~kasan-unify-static-kasan_flag_enabled-across-modes +++ a/mm/kasan/hw_tags.c @@ -46,13 +46,6 @@ static enum kasan_arg_mode kasan_arg_mod 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. */ --- a/mm/kasan/sw_tags.c~kasan-unify-static-kasan_flag_enabled-across-modes +++ a/mm/kasan/sw_tags.c @@ -45,6 +45,8 @@ void __init kasan_init_sw_tags(void) kasan_init_tags(); + static_branch_enable(&kasan_flag_enabled); + pr_info("KernelAddressSanitizer initialized (sw-tags, stacktrace=%s)\n", str_on_off(kasan_stack_collection_enabled())); } _ Patches currently in -mm which might be from snovitoll@gmail.com are mm-unexport-globally-copy_to_kernel_nofault.patch mm-unexport-globally-copy_to_kernel_nofault-v2.patch kasan-unify-static-kasan_flag_enabled-across-modes.patch kasan-arm64-call-kasan_init_generic-in-kasan_init.patch kasan-arm-call-kasan_init_generic-in-kasan_init.patch kasan-xtensa-call-kasan_init_generic-in-kasan_init.patch kasan-loongarch-call-kasan_init_generic-in-kasan_init.patch kasan-um-call-kasan_init_generic-in-kasan_init.patch kasan-x86-call-kasan_init_generic-in-kasan_init.patch kasan-s390-call-kasan_init_generic-in-kasan_init.patch kasan-powerpc-call-kasan_init_generic-in-kasan_init.patch kasan-riscv-call-kasan_init_generic-in-kasan_init.patch kasan-replace-kasan_arch_is_ready-with-kasan_enabled.patch