mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Pierre-Clément Tosi" <ptosi@google.com>
To: Catalin Marinas <catalin.marinas@arm.com>, Will Deacon <will@kernel.org>
Cc: "Mark Rutland" <mark.rutland@arm.com>,
	"Ryan Roberts" <ryan.roberts@arm.com>,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org,
	"Pierre-Clément Tosi" <ptosi@google.com>
Subject: [PATCH 3/5] arm64: Unroll 2x and align mte_tag_*pages() loops
Date: Tue, 01 Dec 2026 16:00:54 +0100	[thread overview]
Message-ID: <20261201-mte-page-optim-v1-3-322b9e4096c7@google.com> (raw)
In-Reply-To: <20261201-mte-page-optim-v1-0-322b9e4096c7@google.com>

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


  parent reply	other threads:[~2026-12-01 15:01 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201-mte-page-optim-v1-3-322b9e4096c7@google.com \
    --to=ptosi@google.com \
    --cc=catalin.marinas@arm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=ryan.roberts@arm.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®