[PATCH] arm64: Harden MTE inline assembly helpers

From: Pierre-Clément Tosi

Date: Fri Dec 04 2026 - 16:05:31 EST


Currently, nothing prevents the compiler from treating LDG as a pure
function of its input and reordering it w.r.t. memory accesses. For
example, it could treat a LDG-STG-LDG sequence to the same address as
LDG-LDG-STG and CSE it into LDG-STG (or similarly STG-LDG)!

We could fix this with the "memory" clobber but that would signal to the
compiler that LDG might alter values in memory, forcing unnecessary data
reloads across it. Similarly, 'volatile' would prevent the compiler from
folding successive LDGs to the same address, which should be sound.

Instead, indicate that the operand is a memory address ("Q"), which has
the same ordering requirements w.r.t. memory accesses without discarding
already-loaded values. Ideally, we'd want to be able to indicate to the
compiler that LDG supports an immediate offset but there's no way to do
that as "m" requires the instruction to support all the modes of LDR.

Similarly, use 'volatile' on IRG to indicate that it has architectural
side effects (PRNG engine) such that successive IRGs wouldn't be folded.

Fixes: 2cb34276427a ("arm64: kasan: simplify and inline MTE functions")
Signed-off-by: Pierre-Clément Tosi <ptosi@xxxxxxxxxx>
---
arch/arm64/include/asm/mte-kasan.h | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/arch/arm64/include/asm/mte-kasan.h b/arch/arm64/include/asm/mte-kasan.h
index de910f1c19e1..3bca8455ff90 100644
--- a/arch/arm64/include/asm/mte-kasan.h
+++ b/arch/arm64/include/asm/mte-kasan.h
@@ -100,8 +100,9 @@ static inline u8 mte_get_ptr_tag(void *ptr)
/* Get allocation tag for the address. */
static inline u8 mte_get_mem_tag(void *addr)
{
- asm(__MTE_PREAMBLE "ldg %0, [%0]"
- : "+r" (addr));
+ asm(__MTE_PREAMBLE "ldg %0, %1"
+ : "+r" (addr)
+ : "Q" (*(const u8 *)addr));

return mte_get_ptr_tag(addr);
}
@@ -111,8 +112,8 @@ static inline u8 mte_get_random_tag(void)
{
void *addr;

- asm(__MTE_PREAMBLE "irg %0, %0"
- : "=r" (addr));
+ asm volatile(__MTE_PREAMBLE "irg %0, %0"
+ : "=r" (addr));

return mte_get_ptr_tag(addr);
}

---
base-commit: ca56f6d71fa68eee3fe05ba7fc1109d0ae994435
change-id: 20261204-mte-harden-ldg-5a9674c38f47

Best regards,
--
Pierre-Clément Tosi <ptosi@xxxxxxxxxx>