* [PATCH 1/4] arm64: Introduce mte_tag_of()/mte_tagged() helpers
2026-12-01 14:49 [PATCH 0/4] Expand MTE tagging API to multi-page and !0 tags Pierre-Clément Tosi
@ 2026-12-01 14:49 ` Pierre-Clément Tosi
2026-12-01 14:49 ` [PATCH 2/4] arm64: Introduce <asm/mte-tagging.h> Pierre-Clément Tosi
2026-12-01 14:49 ` [PATCH 3/4] arm64: Introduce mte_tag_page[s]() Pierre-Clément Tosi
2 siblings, 0 replies; 4+ messages in thread
From: Pierre-Clément Tosi @ 2026-12-01 14:49 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon, Lorenzo Stoakes (ARM),
Vincenzo Frascino
Cc: Marc Zyngier, Fuad Tabba, Mark Rutland, Ryan Roberts,
linux-arm-kernel, linux-kernel, Pierre-Clément Tosi
Introduce helpers to abstract away pointer tag insertion and extraction.
Avoid the get/set terminology, to make it clear that the helpers deal
with the pointer's "logical" tag, unlike mte_get_mem_tag() or
mte_set_mem_tag_range(), which access the "allocation" tags in memory.
Provide "__"-prefixed versions compatible with const arithmetic and
proper type-checked C functions.
Signed-off-by: Pierre-Clément Tosi <ptosi@google.com>
---
arch/arm64/include/asm/mte-def.h | 20 ++++++++++++++++++++
arch/arm64/include/asm/mte-kasan.h | 4 ++--
arch/arm64/mm/fault.c | 2 +-
3 files changed, 23 insertions(+), 3 deletions(-)
diff --git a/arch/arm64/include/asm/mte-def.h b/arch/arm64/include/asm/mte-def.h
index 14ee86b019c2..660948a3fa9c 100644
--- a/arch/arm64/include/asm/mte-def.h
+++ b/arch/arm64/include/asm/mte-def.h
@@ -15,4 +15,24 @@
#define __MTE_PREAMBLE ARM64_ASM_PREAMBLE ".arch_extension memtag\n"
+#ifndef __ASSEMBLER__
+
+#include <linux/bitfield.h>
+#include <linux/types.h>
+
+#define __mte_tag_of(addr) FIELD_GET(MTE_TAG_MASK, addr)
+#define __mte_tagged(addr, tag) u64_replace_bits(addr, tag, MTE_TAG_MASK)
+
+static __always_inline u8 mte_tag_of(const void *ptr)
+{
+ return __mte_tag_of((u64)ptr);
+}
+
+static __always_inline void *mte_tagged(const void *ptr, u8 tag)
+{
+ return (void *)__mte_tagged((u64)ptr, tag);
+}
+
+#endif /* __ASSEMBLER__ */
+
#endif /* __ASM_MTE_DEF_H */
diff --git a/arch/arm64/include/asm/mte-kasan.h b/arch/arm64/include/asm/mte-kasan.h
index de910f1c19e1..8b7434a89b5f 100644
--- a/arch/arm64/include/asm/mte-kasan.h
+++ b/arch/arm64/include/asm/mte-kasan.h
@@ -92,7 +92,7 @@ static inline void __mte_enable_tco_async(void)
static inline u8 mte_get_ptr_tag(void *ptr)
{
/* Note: The format of KASAN tags is 0xF<x> */
- u8 tag = 0xF0 | (u8)(((u64)(ptr)) >> MTE_TAG_SHIFT);
+ u8 tag = 0xF0 | mte_tag_of(ptr);
return tag;
}
@@ -160,7 +160,7 @@ static inline void mte_set_mem_tag_range(void *addr, size_t size, u8 tag,
dczid_bs = 4ul << (dczid & 0xf);
dczid_dzp = (dczid >> 4) & 1;
- curr = (u64)__tag_set(addr, tag);
+ curr = (u64)mte_tagged(addr, tag & 0xF);
mask = dczid_bs - 1;
/* STG/STZG up to the end of the first block. */
end1 = curr | mask;
diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
index b22736853fcd..cf855aa1c30f 100644
--- a/arch/arm64/mm/fault.c
+++ b/arch/arm64/mm/fault.c
@@ -915,7 +915,7 @@ static int do_tag_check_fault(unsigned long far, unsigned long esr,
* Otherwise, bits 63:60 of FAR_EL1 are not UNKNOWN.
*/
if (!cpus_have_cap(ARM64_MTE_FAR))
- far = (__untagged_addr(far) & ~MTE_TAG_MASK) | (far & MTE_TAG_MASK);
+ far = __mte_tagged(__untagged_addr(far), __mte_tag_of(far));
do_bad_area(far, esr, regs);
return 0;
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH 2/4] arm64: Introduce <asm/mte-tagging.h>
2026-12-01 14:49 [PATCH 0/4] Expand MTE tagging API to multi-page and !0 tags Pierre-Clément Tosi
2026-12-01 14:49 ` [PATCH 1/4] arm64: Introduce mte_tag_of()/mte_tagged() helpers Pierre-Clément Tosi
@ 2026-12-01 14:49 ` Pierre-Clément Tosi
2026-12-01 14:49 ` [PATCH 3/4] arm64: Introduce mte_tag_page[s]() Pierre-Clément Tosi
2 siblings, 0 replies; 4+ messages in thread
From: Pierre-Clément Tosi @ 2026-12-01 14:49 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon, Lorenzo Stoakes (ARM),
Vincenzo Frascino
Cc: Marc Zyngier, Fuad Tabba, Mark Rutland, Ryan Roberts,
linux-arm-kernel, linux-kernel, Pierre-Clément Tosi
Since mte.h is included by multiple low-level headers such as pgtable.h
or uaccess.h, it is hard to extend it without falling into circular
dependency traps. This is specifically relevant since upcoming patches
will need to call linux/mm.h macros, which would break the build as-is.
One way to get around this issue would be to add new code in *.c files,
like commit da9f24dc4336 ("arm64: mte: Add mte_clear_folio_tags()") had
to do but that isn't ideal for simple helpers which the compiler should
ideally be able to inline.
A cleaner alternative is to split up mte.h into more fundamental headers
and untangle the different clients. For example, pgtable.h only needs
mte.h for swap logic while uaccess.h uses it for TCO-guarded sections.
Therefore, introduce a dedicated header for tagging functions, mostly
intended to be included from *.c files and such that it itself can
include headers such as linux/mm.h without triggering circular includes.
For now, include it from all callers of mte_[zero_]clear_page_tags() and
limit the header to including mte.h. Future commits will build on that.
Signed-off-by: Pierre-Clément Tosi <ptosi@google.com>
---
arch/arm64/include/asm/mte-tagging.h | 13 +++++++++++++
arch/arm64/kernel/cpufeature.c | 1 +
arch/arm64/kernel/mte.c | 1 +
arch/arm64/kvm/guest.c | 1 +
arch/arm64/kvm/mmu.c | 1 +
arch/arm64/mm/fault.c | 1 +
6 files changed, 18 insertions(+)
diff --git a/arch/arm64/include/asm/mte-tagging.h b/arch/arm64/include/asm/mte-tagging.h
new file mode 100644
index 000000000000..5da9c6433fb8
--- /dev/null
+++ b/arch/arm64/include/asm/mte-tagging.h
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * MTE tagging helpers.
+ *
+ * Header dedicated to tagging functions, pulled out of <asm/mte.h> to simplify
+ * the header dependency graph and intended to mostly be included from .c files.
+ */
+#ifndef __ASM_MTE_TAGGING_H
+#define __ASM_MTE_TAGGING_H
+
+#include <asm/mte.h>
+
+#endif /* __ASM_MTE_TAGGING_H */
diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c
index 8265803357c1..e817d49037a6 100644
--- a/arch/arm64/kernel/cpufeature.c
+++ b/arch/arm64/kernel/cpufeature.c
@@ -88,6 +88,7 @@
#include <asm/mmu.h>
#include <asm/mmu_context.h>
#include <asm/mpam.h>
+#include <asm/mte-tagging.h>
#include <asm/mte.h>
#include <asm/hypervisor.h>
#include <asm/processor.h>
diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c
index 850bcee21e77..0aadaabbe31e 100644
--- a/arch/arm64/kernel/mte.c
+++ b/arch/arm64/kernel/mte.c
@@ -21,6 +21,7 @@
#include <asm/barrier.h>
#include <asm/cpufeature.h>
+#include <asm/mte-tagging.h>
#include <asm/mte.h>
#include <asm/ptrace.h>
#include <asm/sysreg.h>
diff --git a/arch/arm64/kvm/guest.c b/arch/arm64/kvm/guest.c
index b7d36c2abfdf..fc6d067b97ef 100644
--- a/arch/arm64/kvm/guest.c
+++ b/arch/arm64/kvm/guest.c
@@ -25,6 +25,7 @@
#include <asm/kvm.h>
#include <asm/kvm_emulate.h>
#include <asm/kvm_nested.h>
+#include <asm/mte-tagging.h>
#include <asm/sigcontext.h>
#include "trace.h"
diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index a0d4981ebb99..0ae2e19d0bc6 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -22,6 +22,7 @@
#include <asm/kvm_pkvm.h>
#include <asm/kvm_asm.h>
#include <asm/kvm_emulate.h>
+#include <asm/mte-tagging.h>
#include <asm/virt.h>
#include "trace.h"
diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
index cf855aa1c30f..e093e8473b5e 100644
--- a/arch/arm64/mm/fault.c
+++ b/arch/arm64/mm/fault.c
@@ -39,6 +39,7 @@
#include <asm/debug-monitors.h>
#include <asm/esr.h>
#include <asm/kprobes.h>
+#include <asm/mte-tagging.h>
#include <asm/mte.h>
#include <asm/processor.h>
#include <asm/sysreg.h>
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH 3/4] arm64: Introduce mte_tag_page[s]()
2026-12-01 14:49 [PATCH 0/4] Expand MTE tagging API to multi-page and !0 tags Pierre-Clément Tosi
2026-12-01 14:49 ` [PATCH 1/4] arm64: Introduce mte_tag_of()/mte_tagged() helpers Pierre-Clément Tosi
2026-12-01 14:49 ` [PATCH 2/4] arm64: Introduce <asm/mte-tagging.h> Pierre-Clément Tosi
@ 2026-12-01 14:49 ` Pierre-Clément Tosi
2 siblings, 0 replies; 4+ messages in thread
From: Pierre-Clément Tosi @ 2026-12-01 14:49 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon, Lorenzo Stoakes (ARM),
Vincenzo Frascino
Cc: Marc Zyngier, Fuad Tabba, Mark Rutland, Ryan Roberts,
linux-arm-kernel, linux-kernel, Pierre-Clément Tosi
The MTE ISA provides two fundamental categories of bulk operations:
1. setting allocation tags (DC GVA, STGM, ST2G) and
2. setting allocation tags while zeroing data (DC GZVA, STZGM, STZ2G)
such that it is natural to have 2 core C primitives, one for each.
Introduce the former, with support for arbitrary tags and multi-page
ranges, implemented through the reworked mte_clear_page_tags() asm
routine, with a C wrapper for argument prepping.
Provide a single-page variant, mte_tag_page(), for convenience.
Define a temporary compatibility wrapper for mte_clear_page_tags().
Note that the new name is now more consistent with PG_mte_tagged and
related helpers set_page_mte_tagged() or folio_set_hugetlb_mte_tagged().
Signed-off-by: Pierre-Clément Tosi <ptosi@google.com>
---
arch/arm64/include/asm/mte-tagging.h | 26 ++++++++++++++++++++++++++
arch/arm64/include/asm/mte.h | 1 -
arch/arm64/lib/mte.S | 15 ++++++++-------
3 files changed, 34 insertions(+), 8 deletions(-)
diff --git a/arch/arm64/include/asm/mte-tagging.h b/arch/arm64/include/asm/mte-tagging.h
index 5da9c6433fb8..f66e606969c8 100644
--- a/arch/arm64/include/asm/mte-tagging.h
+++ b/arch/arm64/include/asm/mte-tagging.h
@@ -10,4 +10,30 @@
#include <asm/mte.h>
+#ifdef CONFIG_ARM64_MTE
+
+void __mte_tag_page(void *p, u64 t);
+static __always_inline void mte_tag_pages(void *addr, unsigned long npages,
+ u8 tag)
+{
+ u64 tags = 0x1111111111111111ULL * tag;
+
+ do {
+ __mte_tag_page(addr, tags);
+ addr += PAGE_SIZE;
+ } while (--npages);
+}
+
+#else /* CONFIG_ARM64_MTE */
+
+static inline void mte_tag_pages(void *addr, unsigned long n, u8 tag) { }
+
+#endif /* CONFIG_ARM64_MTE */
+
+#define mte_clear_page_tags(p) mte_tag_page(p, 0)
+static __always_inline void mte_tag_page(void *addr, u8 tag)
+{
+ mte_tag_pages(addr, 1, tag);
+}
+
#endif /* __ASM_MTE_TAGGING_H */
diff --git a/arch/arm64/include/asm/mte.h b/arch/arm64/include/asm/mte.h
index ff3d78329be0..3d98d5b8768f 100644
--- a/arch/arm64/include/asm/mte.h
+++ b/arch/arm64/include/asm/mte.h
@@ -18,7 +18,6 @@
#include <asm/pgtable-types.h>
-void mte_clear_page_tags(void *addr);
unsigned long mte_copy_tags_from_user(void *to, const void __user *from,
unsigned long n);
unsigned long mte_copy_tags_to_user(void __user *to, void *from,
diff --git a/arch/arm64/lib/mte.S b/arch/arm64/lib/mte.S
index 5018ac03b6bf..2bd59b877ab6 100644
--- a/arch/arm64/lib/mte.S
+++ b/arch/arm64/lib/mte.S
@@ -24,17 +24,18 @@
.endm
/*
- * Clear the tags in a page
- * x0 - address of the page to be cleared
+ * Tag a page.
+ * x0 - page address
+ * x1 - tags (pre-populated for every granule in a GMID_EL1.BS block)
*/
-SYM_FUNC_START(mte_clear_page_tags)
- multitag_transfer_size x1, x2
-1: stgm xzr, [x0]
- add x0, x0, x1
+SYM_FUNC_START(__mte_tag_page)
+ multitag_transfer_size x2, x3
+1: stgm x1, [x0]
+ add x0, x0, x2
tst x0, #(PAGE_SIZE - 1)
b.ne 1b
ret
-SYM_FUNC_END(mte_clear_page_tags)
+SYM_FUNC_END(__mte_tag_page)
/*
* Zero the page and tags at the same time
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply related [flat|nested] 4+ messages in thread