* [PATCH 1/5] arm64: Factor out macros in MTE assembly routines
2026-12-01 15:00 [PATCH 0/5] Optimize the MTE page tagging API Pierre-Clément Tosi
@ 2026-12-01 15:00 ` Pierre-Clément Tosi
2026-12-01 15:00 ` [PATCH 2/5] arm64: Use STZGM in __mte_tag_clear_page() Pierre-Clément Tosi
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Pierre-Clément Tosi @ 2026-12-01 15:00 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon
Cc: Mark Rutland, Ryan Roberts, linux-arm-kernel, linux-kernel,
Pierre-Clément Tosi
Factor out extraction of DCZID_EL0.BS and jumping to a DZP=1 fallback
into a helper macro, dczid_bs.
Introduce blk_ops (and its blk_op abstraction) to centralize the single
instruction loop pattern used to implement each of the sub-routines.
This will allow future changes to be limited that macro.
No functional change intended.
Signed-off-by: Pierre-Clément Tosi <ptosi@google.com>
---
arch/arm64/lib/mte.S | 56 ++++++++++++++++++++++++++++++++++------------------
1 file changed, 37 insertions(+), 19 deletions(-)
diff --git a/arch/arm64/lib/mte.S b/arch/arm64/lib/mte.S
index 012577641b6b..1b1b76472f62 100644
--- a/arch/arm64/lib/mte.S
+++ b/arch/arm64/lib/mte.S
@@ -23,18 +23,45 @@
lsl \reg, \tmp, \reg
.endm
+/*
+ * dczid_bs - set \bs to DCZID_EL0.BS and branch to \dzp_label when DZP=1.
+ */
+ .macro dczid_bs, bs, tmp, dczid, dzp_label
+ mrs \dczid, dczid_el0
+ and \bs, \dczid, #DCZID_EL0_BS_MASK
+ mov \tmp, #4
+ lsl \bs, \tmp, \bs
+ tbnz \dczid, #DCZID_EL0_DZP_SHIFT, \dzp_label
+ .endm
+
+/*
+ * blk_ops - execute \insn \arg every \bs over the page at \addr.
+ */
+ .macro blk_op, insn, arg, addr, step:vararg
+ .ifc \insn, dc
+ dc \arg, \addr
+ .else
+ \insn \arg, [\addr]
+ .endif
+ add \addr, \addr, \step
+ .endm
+
+ .macro blk_ops, insn, arg, addr, bs
+.Lblk_loop_\@:
+ blk_op \insn, \arg, \addr, \bs
+ tst \addr, #(PAGE_SIZE - 1)
+ b.ne .Lblk_loop_\@
+ ret
+ .endm
+
/*
* Tag a page.
* x0 - page address
* x1 - tags (pre-populated for every granule in a GMID_EL1.BS block)
*/
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
+ multitag_transfer_size x5, x6
+ blk_ops stgm, x1, x0, x5
SYM_FUNC_END(__mte_tag_page)
/*
@@ -42,22 +69,13 @@ SYM_FUNC_END(__mte_tag_page)
* x0 - page address (pre-tagged)
*/
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
- mov x2, #4
- lsl x1, x2, x1
-
-1: dc gzva, x0
- add x0, x0, x1
+ dczid_bs x5, x6, x7, 1f
+ blk_ops dc, gzva, x0, x5
+1:
+ stz2g x0, [x0], #(MTE_GRANULE_SIZE * 2)
tst x0, #(PAGE_SIZE - 1)
b.ne 1b
ret
-
-2: stz2g x0, [x0], #(MTE_GRANULE_SIZE * 2)
- tst x0, #(PAGE_SIZE - 1)
- b.ne 2b
- ret
SYM_FUNC_END(__mte_tag_clear_page)
/*
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH 2/5] arm64: Use STZGM in __mte_tag_clear_page()
2026-12-01 15:00 [PATCH 0/5] Optimize the MTE page tagging API Pierre-Clément Tosi
2026-12-01 15:00 ` [PATCH 1/5] arm64: Factor out macros in MTE assembly routines Pierre-Clément Tosi
@ 2026-12-01 15:00 ` Pierre-Clément Tosi
2026-12-01 15:00 ` [PATCH 3/5] arm64: Unroll 2x and align mte_tag_*pages() loops Pierre-Clément Tosi
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Pierre-Clément Tosi @ 2026-12-01 15:00 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon
Cc: Mark Rutland, Ryan Roberts, linux-arm-kernel, linux-kernel,
Pierre-Clément Tosi
Prefer STZGM over STZ2G when DC GZVA isn't available (DCZID_EL0.DZP=1).
This speeds up mte_clear_page() by ~1.7-2.0x on Cortex-A7xx and C1-Pro
and ~1.85-1.95x on Cortex-X3/X4 with warm caches. With cold caches,
STZGM stalls seem to be slightly worse (~0.90-0.96x) on these.
The big gains are with the C1-Ultra: ~8.15x (!!) with warm caches and
~4-5x (!) when cold, probably because it now enters streaming mode.
Unfortunately, there is a non-negligible regression on Cortex-A5xx of
~0.68-0.75x (warm) and ~0.85-1.03x (cold) but this seems to be worth the
gains on the larger cores.
Signed-off-by: Pierre-Clément Tosi <ptosi@google.com>
---
arch/arm64/include/asm/mte-tagging.h | 4 ++--
arch/arm64/lib/mte.S | 6 ++----
2 files changed, 4 insertions(+), 6 deletions(-)
diff --git a/arch/arm64/include/asm/mte-tagging.h b/arch/arm64/include/asm/mte-tagging.h
index f491682ba418..9c8b39d2dd97 100644
--- a/arch/arm64/include/asm/mte-tagging.h
+++ b/arch/arm64/include/asm/mte-tagging.h
@@ -24,13 +24,13 @@ static __always_inline void mte_tag_pages(void *addr, unsigned long npages,
} while (--npages);
}
-void __mte_tag_clear_page(void *p);
+void __mte_tag_clear_page(void *p, u64 t);
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);
+ __mte_tag_clear_page(addr, tag);
addr += PAGE_SIZE;
} while (--npages);
}
diff --git a/arch/arm64/lib/mte.S b/arch/arm64/lib/mte.S
index 1b1b76472f62..0b753db99114 100644
--- a/arch/arm64/lib/mte.S
+++ b/arch/arm64/lib/mte.S
@@ -67,15 +67,13 @@ SYM_FUNC_END(__mte_tag_page)
/*
* Tag a page and clear its data.
* x0 - page address (pre-tagged)
+ * x1 - tag
*/
SYM_FUNC_START(__mte_tag_clear_page)
dczid_bs x5, x6, x7, 1f
blk_ops dc, gzva, x0, x5
1:
- stz2g x0, [x0], #(MTE_GRANULE_SIZE * 2)
- tst x0, #(PAGE_SIZE - 1)
- b.ne 1b
- ret
+ blk_ops stzgm, x1, x0, x5
SYM_FUNC_END(__mte_tag_clear_page)
/*
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH 3/5] arm64: Unroll 2x and align mte_tag_*pages() loops
2026-12-01 15:00 [PATCH 0/5] Optimize the MTE page tagging API Pierre-Clément Tosi
2026-12-01 15:00 ` [PATCH 1/5] arm64: Factor out macros in MTE assembly routines Pierre-Clément Tosi
2026-12-01 15:00 ` [PATCH 2/5] arm64: Use STZGM in __mte_tag_clear_page() Pierre-Clément Tosi
@ 2026-12-01 15:00 ` Pierre-Clément Tosi
2026-12-01 15:00 ` [PATCH 4/5] arm64: cpufeature: Add cpucap for DCZID_EL0.DZP Pierre-Clément Tosi
2026-12-01 15:00 ` [PATCH 5/5] arm64: Use ARM64_HAS_DC_ZVA for MTE page tagging Pierre-Clément Tosi
4 siblings, 0 replies; 6+ messages in thread
From: Pierre-Clément Tosi @ 2026-12-01 15:00 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon
Cc: Mark Rutland, Ryan Roberts, linux-arm-kernel, linux-kernel,
Pierre-Clément Tosi
Move the outer loop to assembly, to keep it tightly coupled with the
inner one, and align the result to L1_CACHE_SHIFT since every routine
fits in a single I-cache line. Keep 2 loops instead of a single larger
one so that trained branch predictors always guess correctly the
PAGE_SIZEd inner one (4 KiB page at BS=64 means 32 iterations).
Unroll the inner loop 2x using interleaved GRP accesses to minimize
register-level dependencies and maximize writeback buffer load but
preserve ascending address order to avoid disturbing write-streaming
mode (e.g. C1-Ultra). Even with the largest block size (2 KiB) on the
smallest PAGE_SIZE (4 KiB), a 2x unrolled loop can't overshoot.
Note that 4x unrolling was attempted but regressed on Cortex-A7xx with a
notable increase in STALL_BACKEND_{BUSY,CPUBOUND}. Unrolling 2x keeps
the gains on Cortex-A5xx, Cortex-X and C1-Ultra.
Signed-off-by: Pierre-Clément Tosi <ptosi@google.com>
---
arch/arm64/include/asm/mte-tagging.h | 15 ++++---------
arch/arm64/lib/mte.S | 41 ++++++++++++++++++++++--------------
2 files changed, 29 insertions(+), 27 deletions(-)
diff --git a/arch/arm64/include/asm/mte-tagging.h b/arch/arm64/include/asm/mte-tagging.h
index 9c8b39d2dd97..944636ef3559 100644
--- a/arch/arm64/include/asm/mte-tagging.h
+++ b/arch/arm64/include/asm/mte-tagging.h
@@ -12,27 +12,20 @@
#ifdef CONFIG_ARM64_MTE
-void __mte_tag_page(void *p, u64 t);
+void __mte_tag_pages(void *p, unsigned long n, 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);
+ __mte_tag_pages(addr, npages, tags);
}
-void __mte_tag_clear_page(void *p, u64 t);
+void __mte_tag_clear_pages(void *p, unsigned long n, u64 t);
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, tag);
- addr += PAGE_SIZE;
- } while (--npages);
+ __mte_tag_clear_pages(mte_tagged(addr, tag), npages, tag);
}
#else /* CONFIG_ARM64_MTE */
diff --git a/arch/arm64/lib/mte.S b/arch/arm64/lib/mte.S
index 0b753db99114..7d971dd1a36e 100644
--- a/arch/arm64/lib/mte.S
+++ b/arch/arm64/lib/mte.S
@@ -6,6 +6,7 @@
#include <asm/asm-uaccess.h>
#include <asm/assembler.h>
+#include <asm/cache.h>
#include <asm/mte.h>
#include <asm/page.h>
#include <asm/sysreg.h>
@@ -35,7 +36,7 @@
.endm
/*
- * blk_ops - execute \insn \arg every \bs over the page at \addr.
+ * blk_ops - execute \insn \arg every \bs over \npages pages at \addr.
*/
.macro blk_op, insn, arg, addr, step:vararg
.ifc \insn, dc
@@ -46,35 +47,43 @@
add \addr, \addr, \step
.endm
- .macro blk_ops, insn, arg, addr, bs
+ .macro blk_ops, insn, arg, addr, npages, bs, tmp
+ add \tmp, \addr, \bs
.Lblk_loop_\@:
- blk_op \insn, \arg, \addr, \bs
+ blk_op \insn, \arg, \addr, \bs, lsl #1
+ blk_op \insn, \arg, \tmp, \bs, lsl #1
tst \addr, #(PAGE_SIZE - 1)
b.ne .Lblk_loop_\@
+ subs \npages, \npages, #1
+ b.ne .Lblk_loop_\@
ret
.endm
/*
- * Tag a page.
- * x0 - page address
- * x1 - tags (pre-populated for every granule in a GMID_EL1.BS block)
+ * Tag a page-aligned region.
+ * x0 - region start
+ * x1 - number of pages
+ * x2 - tags (pre-populated for every granule in a GMID_EL1.BS block)
*/
-SYM_FUNC_START(__mte_tag_page)
+ .p2align L1_CACHE_SHIFT
+SYM_FUNC_START(__mte_tag_pages)
multitag_transfer_size x5, x6
- blk_ops stgm, x1, x0, x5
-SYM_FUNC_END(__mte_tag_page)
+ blk_ops stgm, x2, x0, x1, x5, x6
+SYM_FUNC_END(__mte_tag_pages)
/*
- * Tag a page and clear its data.
- * x0 - page address (pre-tagged)
- * x1 - tag
+ * Tag a page-aligned region and clear its data.
+ * x0 - region start (pre-tagged)
+ * x1 - number of pages
+ * x2 - tag
*/
-SYM_FUNC_START(__mte_tag_clear_page)
+ .p2align L1_CACHE_SHIFT
+SYM_FUNC_START(__mte_tag_clear_pages)
dczid_bs x5, x6, x7, 1f
- blk_ops dc, gzva, x0, x5
+ blk_ops dc, gzva, x0, x1, x5, x6
1:
- blk_ops stzgm, x1, x0, x5
-SYM_FUNC_END(__mte_tag_clear_page)
+ blk_ops stzgm, x2, x0, x1, x5, x6
+SYM_FUNC_END(__mte_tag_clear_pages)
/*
* Copy the tags from the source page to the destination one
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH 4/5] arm64: cpufeature: Add cpucap for DCZID_EL0.DZP
2026-12-01 15:00 [PATCH 0/5] Optimize the MTE page tagging API Pierre-Clément Tosi
` (2 preceding siblings ...)
2026-12-01 15:00 ` [PATCH 3/5] arm64: Unroll 2x and align mte_tag_*pages() loops Pierre-Clément Tosi
@ 2026-12-01 15:00 ` Pierre-Clément Tosi
2026-12-01 15:00 ` [PATCH 5/5] arm64: Use ARM64_HAS_DC_ZVA for MTE page tagging Pierre-Clément Tosi
4 siblings, 0 replies; 6+ messages in thread
From: Pierre-Clément Tosi @ 2026-12-01 15:00 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon
Cc: Mark Rutland, Ryan Roberts, linux-arm-kernel, linux-kernel,
Pierre-Clément Tosi, Marc Zyngier
Enable the use of static branches on a system-wide CPU capability
matching a (sanitized) DCZID_EL0.DZP by introducing ARM64_HAS_DC_ZVA.
Note that the architecture does not guarantee this field to be constant
(EL2 can alter it for EL1, and EL1 for EL0) but it seems impossible to
me to have a well-behaved system using DZP-dependent instructions under
conditions where the bit flips dynamically as the check MRS DCZID_EL0
followed by DC *VA is inherently TOCTOU-prone. If EL2 lets Linux boot
with DZP=0 and then decides to set it, it must emulate the instructions.
Also note that the polarity of the capability means that instructions
patched with this cpucap default to an always-valid fallback e.g. STZGM
instead of DC GZVA. Similarly, heterogeneous systems with differing DZP
values get sanitized to not having the capability.
Cc: Marc Zyngier <maz@kernel.org>
Signed-off-by: Pierre-Clément Tosi <ptosi@google.com>
---
arch/arm64/kernel/cpufeature.c | 14 ++++++++++++++
arch/arm64/tools/cpucaps | 1 +
2 files changed, 15 insertions(+)
diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c
index e817d49037a6..09302f5ebc91 100644
--- a/arch/arm64/kernel/cpufeature.c
+++ b/arch/arm64/kernel/cpufeature.c
@@ -2424,6 +2424,13 @@ test_has_gicv5_legacy(const struct arm64_cpu_capabilities *entry, int scope)
return !!(read_sysreg_s(SYS_ICC_IDR0_EL1) & ICC_IDR0_EL1_GCIE_LEGACY);
}
+static bool has_dc_zva(const struct arm64_cpu_capabilities *entry, int scope)
+{
+ u64 dczid = read_scoped_sysreg(entry, scope);
+
+ return !SYS_FIELD_GET(DCZID_EL0, DZP, dczid);
+}
+
static const struct arm64_cpu_capabilities arm64_features[] = {
{
.capability = ARM64_ALWAYS_BOOT,
@@ -2619,6 +2626,13 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
ARM64_CPUID_FIELDS(ID_AA64PFR0_EL1, AMU, IMP)
},
#endif /* CONFIG_ARM64_AMU_EXTN */
+ {
+ .desc = "Data cache zero by VA permitted (DCZID_EL0.DZP == 0)",
+ .capability = ARM64_HAS_DC_ZVA,
+ .type = ARM64_CPUCAP_SYSTEM_FEATURE,
+ .sys_reg = SYS_DCZID_EL0,
+ .matches = has_dc_zva,
+ },
{
.desc = "Data cache clean to the PoU not required for I/D coherence",
.capability = ARM64_HAS_CACHE_IDC,
diff --git a/arch/arm64/tools/cpucaps b/arch/arm64/tools/cpucaps
index 51f5683374b0..283cc1c8fbe6 100644
--- a/arch/arm64/tools/cpucaps
+++ b/arch/arm64/tools/cpucaps
@@ -21,6 +21,7 @@ HAS_CNP
HAS_CRC32
HAS_DCPODP
HAS_DCPOP
+HAS_DC_ZVA
HAS_DIT
HAS_E0PD
HAS_ECV
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH 5/5] arm64: Use ARM64_HAS_DC_ZVA for MTE page tagging
2026-12-01 15:00 [PATCH 0/5] Optimize the MTE page tagging API Pierre-Clément Tosi
` (3 preceding siblings ...)
2026-12-01 15:00 ` [PATCH 4/5] arm64: cpufeature: Add cpucap for DCZID_EL0.DZP Pierre-Clément Tosi
@ 2026-12-01 15:00 ` Pierre-Clément Tosi
4 siblings, 0 replies; 6+ messages in thread
From: Pierre-Clément Tosi @ 2026-12-01 15:00 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon
Cc: Mark Rutland, Ryan Roberts, linux-arm-kernel, linux-kernel,
Pierre-Clément Tosi
Use ARM64_HAS_DC_ZVA to static branch to either the STZGM- or DC-based
backend of mte_tag_clear_pages() and split __mte_tag_clear_pages
accordingly.
Do this from the thin mte-tagging.h inline wrapper so that clients
directly call into the appropriate routine. This gives an always-safe
mte_tag_clear_pages() since alternative_has_cap_likely() defaults to
false i.e. STZGM, which is always available when MTE2 is supported.
Note that we still read DCZID_EL0 to extract the block size, which a
later commit will optimize out.
Signed-off-by: Pierre-Clément Tosi <ptosi@google.com>
---
arch/arm64/include/asm/mte-tagging.h | 7 ++++++-
arch/arm64/lib/mte.S | 26 +++++++++++++++++---------
2 files changed, 23 insertions(+), 10 deletions(-)
diff --git a/arch/arm64/include/asm/mte-tagging.h b/arch/arm64/include/asm/mte-tagging.h
index 944636ef3559..eb93e4c44db6 100644
--- a/arch/arm64/include/asm/mte-tagging.h
+++ b/arch/arm64/include/asm/mte-tagging.h
@@ -8,6 +8,7 @@
#ifndef __ASM_MTE_TAGGING_H
#define __ASM_MTE_TAGGING_H
+#include <asm/alternative-macros.h>
#include <asm/mte.h>
#ifdef CONFIG_ARM64_MTE
@@ -22,10 +23,14 @@ static __always_inline void mte_tag_pages(void *addr, unsigned long npages,
}
void __mte_tag_clear_pages(void *p, unsigned long n, u64 t);
+void __mte_tag_clear_pages_dc(void *p, unsigned long n);
static __always_inline void mte_tag_clear_pages(void *addr,
unsigned long npages, u8 tag)
{
- __mte_tag_clear_pages(mte_tagged(addr, tag), npages, tag);
+ if (alternative_has_cap_likely(ARM64_HAS_DC_ZVA))
+ __mte_tag_clear_pages_dc(mte_tagged(addr, tag), npages);
+ else
+ __mte_tag_clear_pages(addr, npages, tag);
}
#else /* CONFIG_ARM64_MTE */
diff --git a/arch/arm64/lib/mte.S b/arch/arm64/lib/mte.S
index 7d971dd1a36e..962a8f4eb63c 100644
--- a/arch/arm64/lib/mte.S
+++ b/arch/arm64/lib/mte.S
@@ -25,14 +25,13 @@
.endm
/*
- * dczid_bs - set \bs to DCZID_EL0.BS and branch to \dzp_label when DZP=1.
+ * dczid_bs - set \bs to DCZID_EL0.BS.
*/
- .macro dczid_bs, bs, tmp, dczid, dzp_label
- mrs \dczid, dczid_el0
- and \bs, \dczid, #DCZID_EL0_BS_MASK
+ .macro dczid_bs, bs, tmp
+ mrs \tmp, dczid_el0
+ and \bs, \tmp, #DCZID_EL0_BS_MASK
mov \tmp, #4
lsl \bs, \tmp, \bs
- tbnz \dczid, #DCZID_EL0_DZP_SHIFT, \dzp_label
.endm
/*
@@ -73,18 +72,27 @@ SYM_FUNC_END(__mte_tag_pages)
/*
* Tag a page-aligned region and clear its data.
- * x0 - region start (pre-tagged)
+ * x0 - region start
* x1 - number of pages
* x2 - tag
*/
.p2align L1_CACHE_SHIFT
SYM_FUNC_START(__mte_tag_clear_pages)
- dczid_bs x5, x6, x7, 1f
- blk_ops dc, gzva, x0, x1, x5, x6
-1:
+ dczid_bs x5, x6
blk_ops stzgm, x2, x0, x1, x5, x6
SYM_FUNC_END(__mte_tag_clear_pages)
+/*
+ * Tag a page-aligned region and clear its data (requires DCZID_EL0.DZP=0).
+ * x0 - region start (pre-tagged)
+ * x1 - number of pages
+ */
+ .p2align L1_CACHE_SHIFT
+SYM_FUNC_START(__mte_tag_clear_pages_dc)
+ dczid_bs x5, x6
+ blk_ops dc, gzva, x0, x1, x5, x6
+SYM_FUNC_END(__mte_tag_clear_pages_dc)
+
/*
* Copy the tags from the source page to the destination one
* x0 - address of the destination page
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 6+ messages in thread