[PATCH 4/4] arm64: Introduce mte_[tag_]clear_page[s]()

From: Pierre-Clément Tosi

Date: Tue Dec 01 2026 - 09:50:17 EST


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 latter, with support for arbitrary tags and multi-page
ranges, implemented through the reworked mte_zero_clear_page_tags() asm
routine, with a C wrapper for argument prepping.

Provide a single-page variant, mte_tag_clear_page(), for convenience.

Since these functions have the combined effects of mte_tag_page[s]() and
clear_page[s](), name them mte_tag_clear_page[s](). This aligns with
the arch-agnostic tag_clear_highpages() and keeps the term "clear" for
data (instead of "zero"), similarly to common kernel mm APIs such as
clear_page[s](), clear_highpage(), clear_user_page(), etc.

Provide concise and convenient "clear-all" helpers: mte_clear_page[s]().

In total, this results in the following API and effects:

function data tags
-------------------------------- ------ ------
clear_page[s](p[, n]) 0 -
mte_clear_page[s](p[, n]) 0 0
mte_tag_clear_page[s](p[, n], t) 0 t
mte_tag_page[s](p[, n], 0) - 0
mte_tag_page[s](p[, n], t) - t

Define a temporary compatibility wrapper for mte_zero_clear_page_tags().

Signed-off-by: Pierre-Clément Tosi <ptosi@xxxxxxxxxx>
---
arch/arm64/include/asm/mte-tagging.h | 28 ++++++++++++++++++++++++++++
arch/arm64/include/asm/mte.h | 4 ----
arch/arm64/lib/mte.S | 11 ++++-------
arch/arm64/mm/proc.S | 8 ++++----
4 files changed, 36 insertions(+), 15 deletions(-)

diff --git a/arch/arm64/include/asm/mte-tagging.h b/arch/arm64/include/asm/mte-tagging.h
index f66e606969c8..f491682ba418 100644
--- a/arch/arm64/include/asm/mte-tagging.h
+++ b/arch/arm64/include/asm/mte-tagging.h
@@ -24,16 +24,44 @@ static __always_inline void mte_tag_pages(void *addr, unsigned long npages,
} while (--npages);
}

+void __mte_tag_clear_page(void *p);
+static __always_inline void mte_tag_clear_pages(void *addr,
+ unsigned long npages, u8 tag)
+{
+ addr = mte_tagged(addr, tag);
+ do {
+ __mte_tag_clear_page(addr);
+ addr += PAGE_SIZE;
+ } while (--npages);
+}
+
#else /* CONFIG_ARM64_MTE */

static inline void mte_tag_pages(void *addr, unsigned long n, u8 tag) { }
+static inline void mte_tag_clear_pages(void *addr, unsigned long n, u8 tag) { }

#endif /* CONFIG_ARM64_MTE */

+static __always_inline void mte_clear_pages(void *addr, unsigned long npages)
+{
+ mte_tag_clear_pages(addr, npages, 0);
+}
+
#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);
}

+static __always_inline void mte_tag_clear_page(void *addr, u8 tag)
+{
+ mte_tag_clear_pages(addr, 1, tag);
+}
+
+#define mte_zero_clear_page_tags(p) mte_clear_page(p)
+static __always_inline void mte_clear_page(void *addr)
+{
+ mte_clear_pages(addr, 1);
+}
+
#endif /* __ASM_MTE_TAGGING_H */
diff --git a/arch/arm64/include/asm/mte.h b/arch/arm64/include/asm/mte.h
index 3d98d5b8768f..d84f12f20277 100644
--- a/arch/arm64/include/asm/mte.h
+++ b/arch/arm64/include/asm/mte.h
@@ -92,7 +92,6 @@ static inline bool try_page_mte_tagging(struct page *page)
return false;
}

-void mte_zero_clear_page_tags(void *addr);
void mte_clear_folio_tags(struct folio *folio);
void mte_sync_tags(pte_t pte, unsigned int nr_pages);
void mte_copy_page_tags(void *kto, const void *kfrom);
@@ -123,9 +122,6 @@ static inline bool try_page_mte_tagging(struct page *page)
{
return false;
}
-static inline void mte_zero_clear_page_tags(void *addr)
-{
-}
static inline void mte_clear_folio_tags(struct folio *folio)
{
}
diff --git a/arch/arm64/lib/mte.S b/arch/arm64/lib/mte.S
index 2bd59b877ab6..012577641b6b 100644
--- a/arch/arm64/lib/mte.S
+++ b/arch/arm64/lib/mte.S
@@ -38,13 +38,10 @@ SYM_FUNC_START(__mte_tag_page)
SYM_FUNC_END(__mte_tag_page)

/*
- * Zero the page and tags at the same time
- *
- * Parameters:
- * x0 - address to the beginning of the page
+ * Tag a page and clear its data.
+ * x0 - page address (pre-tagged)
*/
-SYM_FUNC_START(mte_zero_clear_page_tags)
- and x0, x0, #(1 << MTE_TAG_SHIFT) - 1 // clear the tag
+SYM_FUNC_START(__mte_tag_clear_page)
mrs x1, dczid_el0
tbnz x1, #4, 2f // Branch if DC GZVA is prohibited
and w1, w1, #0xf
@@ -61,7 +58,7 @@ SYM_FUNC_START(mte_zero_clear_page_tags)
tst x0, #(PAGE_SIZE - 1)
b.ne 2b
ret
-SYM_FUNC_END(mte_zero_clear_page_tags)
+SYM_FUNC_END(__mte_tag_clear_page)

/*
* Copy the tags from the source page to the destination one
diff --git a/arch/arm64/mm/proc.S b/arch/arm64/mm/proc.S
index 12aacc74f764..85b8be719a1e 100644
--- a/arch/arm64/mm/proc.S
+++ b/arch/arm64/mm/proc.S
@@ -50,10 +50,10 @@

#ifdef CONFIG_ARM64_MTE
/*
- * The mte_zero_clear_page_tags() implementation uses DC GZVA, which relies on
- * TBI being enabled at EL1. TCMA1 is needed to treat accesses with the
- * match-all tag (0xF) as Tag Unchecked, irrespective of the SCTLR_EL1.TCF
- * setting.
+ * MTE tagging routines pass tagged kernel addresses to DC G(Z)VA, which would
+ * trigger TTBR1 level 0 translation faults unless we set TBI1. TCMA1 is needed
+ * to treat accesses with the match-all tag (0xF) as Tag Unchecked, irrespective
+ * of the SCTLR_EL1.TCF setting.
*/
#define TCR_MTE_FLAGS TCR_EL1_TCMA1 | TCR_EL1_TBI1 | TCR_EL1_TBID1
#else

--
2.56.1.839.g22a8290db9-goog