* [PATCH 0/5] Optimize the MTE page tagging API
@ 2026-12-01 15:00 Pierre-Clément Tosi
2026-12-01 15:00 ` [PATCH 1/5] arm64: Factor out macros in MTE assembly routines Pierre-Clément Tosi
` (4 more replies)
0 siblings, 5 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
(Based on my previous generalization of the API [1])
This series aims to speed up the MTE page-tagging functions, to improve
performance of tagged memory allocation and, once it starts using it,
page-based KASAN poisoning.
It achieves this as follows:
- patch 2 replaces STZ2G with STZGM when DCZID_EL0.DZP=1, which behaves
better on every MTE-capable Tensor core, expect for older Cortex-A5xx
- patch 3 unrolls the loops 2x and removes the branch-per-page by moving
the per-page loop into the ASM routines. This helps the in-order cores
- patch 5 selects between the DC and always-available altertive with a
static branch
Note that DC GVA is not used by this series, as it introduces great
gains on the little cores but also regressions. I'll do that in a
separate series once the mitigations to the regressions have also been
merged. At that point, the tag-only routine will also make use of the
ARM64_HAS_DC_ZVA cpucap introduced for patch 5.
Here are the per-call speedups I measure, when called on a single page:
(cache=warm) mte_tag_pages(1) mte_clear_pages(1)
============== ========================== ==========================
[SoC:CPU]Model DZP=0 DZP=1 DZP=0 DZP=1
============== ============ ============ ============ ============
[G3:3]A510 1.31-1.32x 1.31x 0.94-0.96x 0.97x
[G4:3]A520 1.73x 1.73x 0.96x ~
[G5:1]A520 1.72-1.76x 1.73-1.77x 0.93-1.00x 1.02-1.04x
[G3:7]A715 1.28x 1.28x 1.17-1.18x 2.35-2.37x
[G4:6]A720 1.37-1.42x 1.39-1.46x 1.22-1.32x 2.46-2.48x
[G5:4]A725 1.30-1.35x 1.29-1.36x 1.24-1.29x 2.36-2.37x
[G5:6]A725 1.32-1.34x 1.31-1.38x 1.26-1.30x 2.38-2.41x
[G6:1]C1-Pro 1.20-1.23x 1.18-1.22x 1.10-1.17x 2.09-2.15x
[G6:5]C1-Pro 1.20-1.24x 1.18-1.21x 1.08-1.13x 2.11-2.20x
[G3:8]X3 1.16x 1.15-1.17x 1.16x 2.31-2.33x
[G4:7]X4 1.17-1.18x 1.16-1.18x 1.16-1.17x 2.09x
[G5:7]X4 1.15-1.18x 1.16-1.17x 1.15-1.17x 2.08-2.09x
[G6:6]C1-Ultra 1.14-1.22x 1.16-1.18x 1.15-1.23x 9.56-9.57x
============== ============ ============ ============ ============
(cache=cold) mte_tag_pages(1) mte_clear_pages(1)
============== ========================== ==========================
[SoC:CPU]Model DZP=0 DZP=1 DZP=0 DZP=1
============== ============ ============ ============ ============
[G3:3]A510 1.10x 1.10x ~ 0.91x
[G4:3]A520 1.11x 1.11x 1.03x 0.94x
[G5:1]A520 1.02-1.03x 1.03x ~ ~
[G3:7]A715 ~ ~ ~ ~
[G4:6]A720 ~ ~ ~ ~
[G5:4]A725 1.02-1.03x ~ ~ 0.93-0.94x
[G5:6]A725 1.03x 1.02-1.03x 1.02-1.03x 0.93-0.95x
[G6:1]C1-Pro ~ ~ 0.96-1.02x 0.93-0.95x
[G6:5]C1-Pro ~ ~ 0.97-0.99x 0.94-0.96x
[G3:8]X3 0.88-0.90x 0.87-0.91x 1.01-1.05x 0.96-0.99x
[G4:7]X4 1.39-1.45x 1.35-1.40x 1.32-1.50x 1.33-1.50x
[G5:7]X4 1.02-1.03x 1.03x ~ ~
[G6:6]C1-Ultra 3.59-3.81x 2.88-4.22x ~ 4.88-5.04x
============== ============ ============ ============ ============
At higher page counts, the Cortex-A5xx keep their gains from the unroll
on warm pages (~1.15-1.60x at 4x4K pages) and assymptotically reach
~1.10x gains on larger (or cold) ranges, measure up to 512x4k pages.
On the Cortex-A7xx/X3/X4 and C1-Pro, the STZGM gains remain at ~1.6-2.2x
for 4x4k warm pages, and ~1.3-1.6x for Cortex-A7xx/X3 and ~1.9-2.6x for
Cortex-X4 at 512x4k "warm" (non-L1) pages.
The gains on the C1-Ultra from both unrolling and STZGM are spectacular,
likely because it now reliably enters write streaming mode and stops
stalling on cache misses: mte_tag_pages() is still ~1.1-1.15x faster on
4x4k cold pages but most impressive is mte_clear_pages() under DZP=1
being ~10x faster on 4x4k warm pages, ~23x on 512x4k "warm" pages, ~25x
on 4x4k cold pages and ~29x on 512x4k cold pages.
Methodology for measuring function-level speedups:
- This series was profiled on Google Tensor G3 through G6, running the
Android Common Kernel (branch android16-6.12). Unfortunately, all were
pre-production devices. Although I am fairly confident in the 'warm'
results, I recommend taking some 'cold' results with a grain of salt,
as they might not accurately represent actual production devices. It's
worth noting that all of the evaluated CPUs were Arm designs and I
would appreciate confirmation that the results presented here carry
over to other microarchitectures.
- Per-commit improvements were measured by loading variants of the
functions with and without the change under test. They were called
repeatedly and their execution timed, each iteration interleaving them
at random and using one of 16 separately allocated tagged buffers
(assumed scattered around memory, to alleviate channel interference).
Before each run, the D-cache entries for the buffer were populated
through accesses ("warm") or tags and data invalidated ("cold"). These
were measured across multiple insmods (to reduce code placement
impact), with the task pinned, IRQs masked, the CPU pinned to the
mid-point of its OPP range, while DSU and DDR were pinned at 100%.
- Every device had DCZID_EL0.DZP=0 so the DZP=1 fallbacks were tested by
adding a mask to each MRS, provided to the module at insmod (i.e. not
optimizable by the compiler), with only one value passed per insmod,
as the branch predictors were otherwise impacting the measurements.
- Function execution time is measured through PMCCNTR_EL0 samples.
Specifically, the execution included no DSB before the post-run sample
(only ISB). This is because I wanted to measure function-level CPU
latency, not HW properties. In real-world scenarios, I am assuming
that the code after the execution of these high-bandwidth calls will
statistically have a significantly lower store rate. Therefore, under
these conditions, implementations that manage to "pack" the CPU
writeback queue are favored as they permit the CPU to already execute
what follows. A DSB would add an artificial stall to that execution.
- Most commits in this series (and the tables above) report speedup
ranges; those represent one standard deviation around the mean of the
paired-observation speedup ratios (old over new version) between their
interquartile means (IQM), measured across 10 insmods each of 2000
executions, interleaved as described above.
[1]: https://lore.kernel.org/linux-arm-kernel/20261201-mte-page-api-v1-0-5e38d19462ea@google.com
Signed-off-by: Pierre-Clément Tosi <ptosi@google.com>
---
Pierre-Clément Tosi (5):
arm64: Factor out macros in MTE assembly routines
arm64: Use STZGM in __mte_tag_clear_page()
arm64: Unroll 2x and align mte_tag_*pages() loops
arm64: cpufeature: Add cpucap for DCZID_EL0.DZP
arm64: Use ARM64_HAS_DC_ZVA for MTE page tagging
arch/arm64/include/asm/mte-tagging.h | 20 ++++----
arch/arm64/kernel/cpufeature.c | 14 ++++++
arch/arm64/lib/mte.S | 91 ++++++++++++++++++++++++------------
arch/arm64/tools/cpucaps | 1 +
4 files changed, 86 insertions(+), 40 deletions(-)
---
base-commit: 012e4bc01e339a65dc7c59181b5efb33b171d12c
change-id: 20261129-mte-page-optim-b66637b0b427
prerequisite-change-id: 20261129-mte-page-api-f285a514f1ad:v1
prerequisite-patch-id: fc2943d14606f01e9594700b3b8e616a4e66d1d7
prerequisite-patch-id: 87fc04e73de4eaf7d0d5ab4be5718e362dc47409
prerequisite-patch-id: 0925d5cf3e744a9d6a71462a7198cfea73bb737c
prerequisite-patch-id: 1ef6c57e30b209f30577f98d8e289e77977964ed
Best regards,
--
Pierre-Clément Tosi <ptosi@google.com>
^ permalink raw reply [flat|nested] 6+ messages in thread
* [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
end of thread, other threads:[~2026-12-01 15:01 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this 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 ` [PATCH 2/5] arm64: Use STZGM in __mte_tag_clear_page() 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
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®