Linux-ARM-Kernel Archive on lore.kernel.org
 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>,
	 "Lorenzo Stoakes (ARM)" <ljs@kernel.org>,
	Vincenzo Frascino <vincenzo.frascino@arm.com>
Cc: "Marc Zyngier" <maz@kernel.org>,
	"Fuad Tabba" <fuad.tabba@linux.dev>,
	"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 1/4] arm64: Introduce mte_tag_of()/mte_tagged() helpers
Date: Tue, 01 Dec 2026 15:49:17 +0100	[thread overview]
Message-ID: <20261201-mte-page-api-v1-1-5e38d19462ea@google.com> (raw)
In-Reply-To: <20261201-mte-page-api-v1-0-5e38d19462ea@google.com>

Introduce helpers to abstract away pointer tag insertion and extraction.

Avoid the get/set terminology, to make it clear that the helpers deal
with the pointer's "logical" tag, unlike mte_get_mem_tag() or
mte_set_mem_tag_range(), which access the "allocation" tags in memory.

Provide "__"-prefixed versions compatible with const arithmetic and
proper type-checked C functions.

Signed-off-by: Pierre-Clément Tosi <ptosi@google.com>
---
 arch/arm64/include/asm/mte-def.h   | 20 ++++++++++++++++++++
 arch/arm64/include/asm/mte-kasan.h |  4 ++--
 arch/arm64/mm/fault.c              |  2 +-
 3 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/arch/arm64/include/asm/mte-def.h b/arch/arm64/include/asm/mte-def.h
index 14ee86b019c2..660948a3fa9c 100644
--- a/arch/arm64/include/asm/mte-def.h
+++ b/arch/arm64/include/asm/mte-def.h
@@ -15,4 +15,24 @@
 
 #define __MTE_PREAMBLE		ARM64_ASM_PREAMBLE ".arch_extension memtag\n"
 
+#ifndef __ASSEMBLER__
+
+#include <linux/bitfield.h>
+#include <linux/types.h>
+
+#define __mte_tag_of(addr)	FIELD_GET(MTE_TAG_MASK, addr)
+#define __mte_tagged(addr, tag)	u64_replace_bits(addr, tag, MTE_TAG_MASK)
+
+static __always_inline u8 mte_tag_of(const void *ptr)
+{
+	return __mte_tag_of((u64)ptr);
+}
+
+static __always_inline void *mte_tagged(const void *ptr, u8 tag)
+{
+	return (void *)__mte_tagged((u64)ptr, tag);
+}
+
+#endif /* __ASSEMBLER__ */
+
 #endif /* __ASM_MTE_DEF_H  */
diff --git a/arch/arm64/include/asm/mte-kasan.h b/arch/arm64/include/asm/mte-kasan.h
index de910f1c19e1..8b7434a89b5f 100644
--- a/arch/arm64/include/asm/mte-kasan.h
+++ b/arch/arm64/include/asm/mte-kasan.h
@@ -92,7 +92,7 @@ static inline void __mte_enable_tco_async(void)
 static inline u8 mte_get_ptr_tag(void *ptr)
 {
 	/* Note: The format of KASAN tags is 0xF<x> */
-	u8 tag = 0xF0 | (u8)(((u64)(ptr)) >> MTE_TAG_SHIFT);
+	u8 tag = 0xF0 | mte_tag_of(ptr);
 
 	return tag;
 }
@@ -160,7 +160,7 @@ static inline void mte_set_mem_tag_range(void *addr, size_t size, u8 tag,
 	dczid_bs = 4ul << (dczid & 0xf);
 	dczid_dzp = (dczid >> 4) & 1;
 
-	curr = (u64)__tag_set(addr, tag);
+	curr = (u64)mte_tagged(addr, tag & 0xF);
 	mask = dczid_bs - 1;
 	/* STG/STZG up to the end of the first block. */
 	end1 = curr | mask;
diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
index b22736853fcd..cf855aa1c30f 100644
--- a/arch/arm64/mm/fault.c
+++ b/arch/arm64/mm/fault.c
@@ -915,7 +915,7 @@ static int do_tag_check_fault(unsigned long far, unsigned long esr,
 	 * Otherwise, bits 63:60 of FAR_EL1 are not UNKNOWN.
 	 */
 	if (!cpus_have_cap(ARM64_MTE_FAR))
-		far = (__untagged_addr(far) & ~MTE_TAG_MASK) | (far & MTE_TAG_MASK);
+		far = __mte_tagged(__untagged_addr(far), __mte_tag_of(far));
 
 	do_bad_area(far, esr, regs);
 	return 0;

-- 
2.56.1.839.g22a8290db9-goog



  reply	other threads:[~2026-12-01 14:50 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 14:49 [PATCH 0/4] Expand MTE tagging API to multi-page and !0 tags Pierre-Clément Tosi
2026-12-01 14:49 ` Pierre-Clément Tosi [this message]
2026-12-01 14:49 ` [PATCH 2/4] arm64: Introduce <asm/mte-tagging.h> Pierre-Clément Tosi
2026-12-01 14:49 ` [PATCH 3/4] arm64: Introduce mte_tag_page[s]() 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-api-v1-1-5e38d19462ea@google.com \
    --to=ptosi@google.com \
    --cc=catalin.marinas@arm.com \
    --cc=fuad.tabba@linux.dev \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ljs@kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=maz@kernel.org \
    --cc=ryan.roberts@arm.com \
    --cc=vincenzo.frascino@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