[PATCH 3/5] arm64: Unroll 2x and align mte_tag_*pages() loops
From: Pierre-Clément Tosi
Date: Tue Dec 01 2026 - 10:01:44 EST
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@xxxxxxxxxx>
---
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