[PATCH 1/5] arm64: Factor out macros in MTE assembly routines

Pierre-Clément Tosi ptosi at google.com
Tue Dec 1 07:00:52 PST 2026


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 at 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




More information about the linux-arm-kernel mailing list