[PATCH 22/24] arm64: Consolidate MTE-based KASAN in mte-kasan.h

From: Pierre-Clément Tosi

Date: Sat Nov 07 2026 - 10:51:24 EST


As the KASAN-agnostic logic has been carved out of these functions into
standalone MTE functions and since all their non-KASAN callers have been
migrated to those KASAN-agnostic functions and/or arch-agnostic KASAN
functions, the only remaining callers are in KASAN core code and access
them through their arch_*() aliases, which are now an unnecessary level
of indirection.

Therefore, fold the aliases into __always_inline functions.

First, note that this now exposes arch_*() as proper C symbols. Although
the aliasing macros were necessary when first introduced by commit
ccbe2aaba1ed ("arm64: kasan: add arch layer for memory tagging helpers")
because of mm/kasan/kasan.h's reliance on #ifndef to provide fallbacks,
they have since been made redundant by commit e34f1e2ee098 ("kasan: drop
empty tagging-related defines").

Second, it moves the KASAN arch definitions out of <asm/memory.h>. It
isn't clear to me why they were originally placed there but since they
are only for arch-agnostic KASAN code and are guarded by KASAN_HW_TAGS,
it seems more appropriate to have them in a KASAN arch header.

Drop the !ARM64_MTE fallbacks for mte_*() instead of renaming them since
they were deadcode, being only called through arch_*(), which were only
defined when KASAN_HW_TAGS and given that KASAN_HW_TAGS means ARM64_MTE.

Move the declarations of mte_enable_kernel_*() to <asm/mte.h> and
provide no alternatives when !ARM64_MTE since their signatures imply
that they never fail (return void) so a currently non-existent caller
could get confused. A future patch might rework that and add fallbacks.

Cc: Alexander Potapenko <glider@xxxxxxxxxx>
Cc: Andrey Konovalov <andreyknvl@xxxxxxxxx>
Cc: Andrey Ryabinin <ryabinin.a.a@xxxxxxxxx>
Cc: Dmitry Vyukov <dvyukov@xxxxxxxxxx>
Cc: kasan-dev@xxxxxxxxxxxxxxxx
Signed-off-by: Pierre-Clément Tosi <ptosi@xxxxxxxxxx>
---
arch/arm64/include/asm/memory.h | 14 -------
arch/arm64/include/asm/mte-kasan.h | 86 ++++++++++++++------------------------
arch/arm64/include/asm/mte.h | 5 +++
3 files changed, 36 insertions(+), 69 deletions(-)

diff --git a/arch/arm64/include/asm/memory.h b/arch/arm64/include/asm/memory.h
index 29e7537f5e58..2aa14788c050 100644
--- a/arch/arm64/include/asm/memory.h
+++ b/arch/arm64/include/asm/memory.h
@@ -309,20 +309,6 @@ static inline const void *__tag_set(const void *addr, u8 tag)
return (const void *)(__addr | __tag_shifted(tag));
}

-#ifdef CONFIG_KASAN_HW_TAGS
-#define arch_enable_tag_checks_sync() mte_enable_kernel_sync()
-#define arch_enable_tag_checks_async() mte_enable_kernel_async()
-#define arch_enable_tag_checks_asymm() mte_enable_kernel_asymm()
-#define arch_enable_tag_checks_write_only() mte_enable_kernel_store_only()
-#define arch_suppress_tag_checks_start() mte_enable_tco()
-#define arch_suppress_tag_checks_stop() mte_disable_tco()
-#define arch_flush_async_tag_fault() mte_get_reset_tfsr()
-#define arch_set_random_tag(addr) mte_set_random_tag(addr)
-#define arch_get_mem_tag(addr) mte_get_mem_tag(addr)
-#define arch_set_mem_tag_range(addr, size, tag, init) \
- mte_set_mem_tag_range((addr), (size), (tag), (init))
-#endif /* CONFIG_KASAN_HW_TAGS */
-
/*
* Physical vs virtual RAM address space conversion. These are
* private definitions which should NOT be used outside memory.h
diff --git a/arch/arm64/include/asm/mte-kasan.h b/arch/arm64/include/asm/mte-kasan.h
index 81a8cf3b9a95..c0224a2bb515 100644
--- a/arch/arm64/include/asm/mte-kasan.h
+++ b/arch/arm64/include/asm/mte-kasan.h
@@ -46,98 +46,74 @@ static __always_inline u8 kasan_tag_from_mte(u8 tag)
return u8_replace_bits(KASAN_TAG_MIN, tag, KASAN_TAG_MTE_MASK);
}

-#ifdef CONFIG_ARM64_MTE
+#ifdef CONFIG_KASAN_HW_TAGS

/*
- * These functions are meant to be only used from KASAN runtime through
- * the arch_*() interface defined in asm/memory.h.
* These functions don't include system_supports_mte() checks,
* as KASAN only calls them when MTE is supported and enabled.
*/

-static inline void mte_disable_tco(void)
+static __always_inline void arch_enable_tag_checks_sync(void)
{
- set_pstate_tco(0);
+ mte_enable_kernel_sync();
}

-static inline void mte_enable_tco(void)
+static __always_inline void arch_enable_tag_checks_async(void)
{
- set_pstate_tco(1);
+ mte_enable_kernel_async();
}

-/* Get allocation tag for the address. */
-static inline u8 mte_get_mem_tag(void *addr)
+static __always_inline void arch_enable_tag_checks_asymm(void)
{
- return kasan_tag_from_mte(__mte_tag_of(__ldg((u64)addr)));
+ mte_enable_kernel_asymm();
}

-/* Generate a random tag. */
-static inline void *mte_set_random_tag(const void *addr)
+static __always_inline int arch_enable_tag_checks_write_only(void)
{
- /* Writes the random MTE tag in bits [59:56]. */
- addr = (const void *)__irg((u64)addr);
-
- /* Sets bits [63:60] as KASAN expects. */
- return (void *)__tag_set(addr, kasan_tag_from_mte(mte_tag_of(addr)));
+ return mte_enable_kernel_store_only();
}

-static inline void mte_set_mem_tag_range(void *addr, size_t size, u8 tag,
- bool zero)
+static __always_inline void arch_suppress_tag_checks_stop(void)
{
- if (zero)
- mte_tag_and_zero_range(addr, size, kasan_tag_to_mte(tag));
- else
- mte_tag_range(addr, size, kasan_tag_to_mte(tag));
-}
-
-void mte_enable_kernel_sync(void);
-void mte_enable_kernel_async(void);
-void mte_enable_kernel_asymm(void);
-int mte_enable_kernel_store_only(void);
-
-#else /* CONFIG_ARM64_MTE */
-
-static inline void mte_disable_tco(void)
-{
-}
-
-static inline void mte_enable_tco(void)
-{
-}
-
-static inline u8 mte_get_mem_tag(void *addr)
-{
- return 0xFF;
+ set_pstate_tco(0);
}

-static inline void *mte_set_random_tag(const void *addr)
+static __always_inline void arch_suppress_tag_checks_start(void)
{
- return (void *)__tag_set(addr, 0xFF);
+ set_pstate_tco(1);
}

-static inline void mte_set_mem_tag_range(void *addr, size_t size,
- u8 tag, bool init)
+static __always_inline bool arch_flush_async_tag_fault(void)
{
+ return mte_get_reset_tfsr();
}

-static inline void mte_enable_kernel_sync(void)
+/* Get allocation tag for the address. */
+static __always_inline u8 arch_get_mem_tag(void *addr)
{
+ return kasan_tag_from_mte(__mte_tag_of(__ldg((u64)addr)));
}

-static inline void mte_enable_kernel_async(void)
+/* Generate a random tag. */
+static __always_inline void *arch_set_random_tag(const void *addr)
{
-}
+ /* Writes the random MTE tag in bits [59:56]. */
+ addr = (const void *)__irg((u64)addr);

-static inline void mte_enable_kernel_asymm(void)
-{
+ /* Sets bits [63:60] as KASAN expects. */
+ return (void *)__tag_set(addr, kasan_tag_from_mte(mte_tag_of(addr)));
}

-static inline int mte_enable_kernel_store_only(void)
+static __always_inline void arch_set_mem_tag_range(void *addr, size_t size,
+ u8 tag, bool zero)
{
- return -EINVAL;
+ if (zero)
+ mte_tag_and_zero_range(addr, size, kasan_tag_to_mte(tag));
+ else
+ mte_tag_range(addr, size, kasan_tag_to_mte(tag));
}

-#endif /* CONFIG_ARM64_MTE */
+#endif /* CONFIG_KASAN_HW_TAGS */

#endif /* __ASSEMBLER__ */

diff --git a/arch/arm64/include/asm/mte.h b/arch/arm64/include/asm/mte.h
index 9ba215b2b0fd..34d1438019ed 100644
--- a/arch/arm64/include/asm/mte.h
+++ b/arch/arm64/include/asm/mte.h
@@ -47,6 +47,11 @@ static inline bool system_uses_async_mte_faults(void)
{
return static_branch_unlikely(&mte_async_or_asymm_mode);
}
+
+void mte_enable_kernel_sync(void);
+void mte_enable_kernel_async(void);
+void mte_enable_kernel_asymm(void);
+int mte_enable_kernel_store_only(void);
#else
static inline bool system_uses_sync_mte_faults(void)
{

--
2.56.0.455.g6c5a503b38-goog