[PATCH 18/24] arm64: Inline and remove mte_check_tfsr_*()

From: Pierre-Clément Tosi

Date: Sat Nov 07 2026 - 10:52:59 EST


Inline mte_check_tfsr_{entry,exit}() into the kernel entry/exit helpers,
which should ever be their only callers and therefore were an unneeded
extra layer of indirection. This allows deduplicating checks that were
performed within the functions and outside.

Inline mte_check_tfsr_el1() as the now-trivial check-and-report,
improving readability of the code and clearly showing the dependency on
KASAN, while abstracting away synchronization for mte_suspend_enter().
Use an early kasan_hw_tags_enabled() guard to let the compiler optimize
it all out when !KASAN_HW_TAGS.

As a result, <mte.h> finally drops its dependency on KASAN.

Note that mte_enter_from_kernel_mode() now clears TCO before checking
TFSR, which should be functionally equivalent and even provide a
negligibly higher coverage.

Signed-off-by: Pierre-Clément Tosi <ptosi@xxxxxxxxxx>
---
arch/arm64/include/asm/mte.h | 45 ----------------------------------------
arch/arm64/kernel/entry-common.c | 22 ++++++++++++++++----
arch/arm64/kernel/mte.c | 28 ++++++++-----------------
arch/arm64/mm/copypage.c | 1 +
4 files changed, 28 insertions(+), 68 deletions(-)

diff --git a/arch/arm64/include/asm/mte.h b/arch/arm64/include/asm/mte.h
index 1308e37c43c6..1e20a43c0a67 100644
--- a/arch/arm64/include/asm/mte.h
+++ b/arch/arm64/include/asm/mte.h
@@ -12,7 +12,6 @@
#ifndef __ASSEMBLER__

#include <linux/bitfield.h>
-#include <linux/kasan-enabled.h>
#include <linux/page-flags.h>
#include <linux/sched.h>
#include <linux/types.h>
@@ -264,49 +263,5 @@ static inline bool folio_try_hugetlb_mte_tagging(struct folio *folio)
}
#endif

-#ifdef CONFIG_KASAN_HW_TAGS
-void mte_check_tfsr_el1(void);
-
-static inline void mte_check_tfsr_entry(void)
-{
- if (!kasan_hw_tags_enabled())
- return;
-
- if (!system_uses_mte_async_or_asymm_mode())
- return;
-
- mte_check_tfsr_el1();
-}
-
-static inline void mte_check_tfsr_exit(void)
-{
- if (!kasan_hw_tags_enabled())
- return;
-
- if (!system_uses_mte_async_or_asymm_mode())
- return;
-
- /*
- * The asynchronous faults are sync'ed automatically with
- * TFSR_EL1 on kernel entry but for exit an explicit dsb()
- * is required.
- */
- dsb(nsh);
- isb();
-
- mte_check_tfsr_el1();
-}
-#else
-static inline void mte_check_tfsr_el1(void)
-{
-}
-static inline void mte_check_tfsr_entry(void)
-{
-}
-static inline void mte_check_tfsr_exit(void)
-{
-}
-#endif /* CONFIG_KASAN_HW_TAGS */
-
#endif /* __ASSEMBLER__ */
#endif /* __ASM_MTE_H */
diff --git a/arch/arm64/kernel/entry-common.c b/arch/arm64/kernel/entry-common.c
index ea385f579269..db4ff003a5b6 100644
--- a/arch/arm64/kernel/entry-common.c
+++ b/arch/arm64/kernel/entry-common.c
@@ -73,19 +73,29 @@ static __always_inline void irqentry_exit_pcpu_adjust(struct pt_regs *regs)

static __always_inline void mte_enter_from_kernel_mode(void)
{
- mte_check_tfsr_entry();
-
/*
* Taking an exception sets TCO but clearing it can be expensive on
* some architectures so first check that MTE is actually required.
*/
if (kasan_hw_tags_enabled() || uaccess_requires_mte(current))
set_pstate_tco(0);
+
+ if (!kasan_hw_tags_enabled())
+ return;
+
+ /* Asynchronous faults are sync'ed automatically on kernel entry. */
+ if (system_uses_mte_async_or_asymm_mode() && __mte_get_reset_tfsr())
+ kasan_report_async();
}

static __always_inline void mte_exit_to_kernel_mode(void)
{
- mte_check_tfsr_exit();
+ if (!kasan_hw_tags_enabled())
+ return;
+
+ /* Asynchronous faults require explicit sync'ing on kernel exit. */
+ if (system_uses_mte_async_or_asymm_mode() && mte_get_reset_tfsr())
+ kasan_report_async();
}

static __always_inline void mte_enter_from_user_mode(void)
@@ -96,7 +106,11 @@ static __always_inline void mte_enter_from_user_mode(void)

static __always_inline void mte_exit_to_user_mode(void)
{
- mte_check_tfsr_exit();
+ if (!kasan_hw_tags_enabled())
+ return;
+
+ if (system_uses_mte_async_or_asymm_mode() && mte_get_reset_tfsr())
+ kasan_report_async();
}

/*
diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c
index 725f48faec3e..b65e3aa3bd07 100644
--- a/arch/arm64/kernel/mte.c
+++ b/arch/arm64/kernel/mte.c
@@ -5,6 +5,7 @@

#include <linux/bitops.h>
#include <linux/cpu.h>
+#include <linux/kasan.h>
#include <linux/kasan-enabled.h>
#include <linux/kernel.h>
#include <linux/mm.h>
@@ -180,14 +181,6 @@ int mte_enable_kernel_store_only(void)
}
#endif

-#ifdef CONFIG_KASAN_HW_TAGS
-void mte_check_tfsr_el1(void)
-{
- if (__mte_get_reset_tfsr())
- kasan_report_async();
-}
-#endif
-
/*
* This is where we actually resolve the system and process MTE mode
* configuration into an actual value in SCTLR_EL1 that affects
@@ -285,6 +278,9 @@ void mte_thread_switch(struct task_struct *next)
if (!kasan_hw_tags_enabled() && uaccess_requires_mte(next))
set_pstate_tco(0);

+ if (!kasan_hw_tags_enabled())
+ return;
+
if (!system_uses_mte_async_or_asymm_mode())
return;

@@ -296,7 +292,8 @@ void mte_thread_switch(struct task_struct *next)
* are synchronized before this point.
*/
isb();
- mte_check_tfsr_el1();
+ if (__mte_get_reset_tfsr())
+ kasan_report_async();
}

void mte_cpu_setup(void)
@@ -348,18 +345,11 @@ void mte_suspend_enter(void)
if (!system_supports_mte())
return;

- if (!system_uses_mte_async_or_asymm_mode())
+ if (!kasan_hw_tags_enabled())
return;

- /*
- * The barriers are required to guarantee that the indirect writes
- * to TFSR_EL1 are synchronized before we report the state.
- */
- dsb(nsh);
- isb();
-
- /* Report SYS_TFSR_EL1 before suspend entry */
- mte_check_tfsr_el1();
+ if (system_uses_mte_async_or_asymm_mode() && mte_get_reset_tfsr())
+ kasan_report_async();
}

void mte_suspend_exit(void)
diff --git a/arch/arm64/mm/copypage.c b/arch/arm64/mm/copypage.c
index cd5912ba617b..e6796bf1fad1 100644
--- a/arch/arm64/mm/copypage.c
+++ b/arch/arm64/mm/copypage.c
@@ -7,6 +7,7 @@
*/

#include <linux/bitops.h>
+#include <linux/kasan-enabled.h>
#include <linux/mm.h>

#include <asm/page.h>

--
2.56.0.455.g6c5a503b38-goog