[PATCH 07/24] arm64: Skip redundant TCO clear post-kernel entry
From: Pierre-Clément Tosi
Date: Sat Nov 07 2026 - 11:26:09 EST
For performance reasons, kernel entry might skip clearing TCO. Still,
tasks expecting synchronous uaccess tag faults to be reported (and
therefore raised) require it to be clear; this is why thread switching
and MTE enabling functions were calling into mte_disable_tco_entry().
However, when the kernel itself uses MTE (for KASAN), it must have
cleared TCO on kernel entry.
Use that invariant to skip redundantly clearing TCO and/or checking the
task's SCTLR. As kasan_hw_tags_enabled() uses a static branch, TCO
maintenance is completely optimized out the moment HW KASAN is enabled.
Cc: Peter Collingbourne <peter@xxxxxxxxx>
Signed-off-by: Pierre-Clément Tosi <ptosi@xxxxxxxxxx>
---
arch/arm64/kernel/mte.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c
index d1dd9b80173a..8025ee1f7fdb 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-enabled.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/prctl.h>
@@ -290,7 +291,8 @@ void mte_thread_switch(struct task_struct *next)
mte_update_gcr_excl(next);
/* TCO may not have been disabled on exception entry for the current task. */
- mte_disable_tco_entry(next);
+ if (!kasan_hw_tags_enabled())
+ mte_disable_tco_entry(next);
if (!system_uses_mte_async_or_asymm_mode())
return;
@@ -409,7 +411,8 @@ long set_mte_ctrl(struct task_struct *task, unsigned long arg)
mte_update_sctlr_user(task);
mte_update_gcr_excl(task);
update_sctlr_el1(task->thread.sctlr_user);
- mte_disable_tco_entry(task);
+ if (!kasan_hw_tags_enabled())
+ mte_disable_tco_entry(task);
preempt_enable();
}
--
2.56.0.455.g6c5a503b38-goog