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>,
	"Vincenzo Frascino" <vincenzo.frascino@arm.com>,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org,
	"Alexander Potapenko" <glider@google.com>,
	"Andrey Konovalov" <andreyknvl@gmail.com>,
	"Andrey Ryabinin" <ryabinin.a.a@gmail.com>,
	"Dmitry Vyukov" <dvyukov@google.com>,
	kasan-dev@googlegroups.com,
	"Pierre-Clément Tosi" <ptosi@google.com>
Subject: [PATCH 22/24] arm64: Consolidate MTE-based KASAN in mte-kasan.h
Date: Sat, 07 Nov 2026 16:36:28 +0100	[thread overview]
Message-ID: <20261107-mte-kasan-split-v1-22-d84ea5f30f94@google.com> (raw)
In-Reply-To: <20261107-mte-kasan-split-v1-0-d84ea5f30f94@google.com>

As the KASAN-agnostic logic has been carved out of these functions into
standalone MTE functions and since all their non-KASAN callers have been
migrated to those KASAN-agnostic functions and/or arch-agnostic KASAN
functions, the only remaining callers are in KASAN core code and access
them through their arch_*() aliases, which are now an unnecessary level
of indirection.

Therefore, fold the aliases into __always_inline functions.

First, note that this now exposes arch_*() as proper C symbols. Although
the aliasing macros were necessary when first introduced by commit
ccbe2aaba1ed ("arm64: kasan: add arch layer for memory tagging helpers")
because of mm/kasan/kasan.h's reliance on #ifndef to provide fallbacks,
they have since been made redundant by commit e34f1e2ee098 ("kasan: drop
empty tagging-related defines").

Second, it moves the KASAN arch definitions out of <asm/memory.h>. It
isn't clear to me why they were originally placed there but since they
are only for arch-agnostic KASAN code and are guarded by KASAN_HW_TAGS,
it seems more appropriate to have them in a KASAN arch header.

Drop the !ARM64_MTE fallbacks for mte_*() instead of renaming them since
they were deadcode, being only called through arch_*(), which were only
defined when KASAN_HW_TAGS and given that KASAN_HW_TAGS means ARM64_MTE.

Move the declarations of mte_enable_kernel_*() to <asm/mte.h> and
provide no alternatives when !ARM64_MTE since their signatures imply
that they never fail (return void) so a currently non-existent caller
could get confused. A future patch might rework that and add fallbacks.

Cc: Alexander Potapenko <glider@google.com>
Cc: Andrey Konovalov <andreyknvl@gmail.com>
Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: kasan-dev@googlegroups.com
Signed-off-by: Pierre-Clément Tosi <ptosi@google.com>
---
 arch/arm64/include/asm/memory.h    | 14 -------
 arch/arm64/include/asm/mte-kasan.h | 86 ++++++++++++++------------------------
 arch/arm64/include/asm/mte.h       |  5 +++
 3 files changed, 36 insertions(+), 69 deletions(-)

diff --git a/arch/arm64/include/asm/memory.h b/arch/arm64/include/asm/memory.h
index 29e7537f5e58..2aa14788c050 100644
--- a/arch/arm64/include/asm/memory.h
+++ b/arch/arm64/include/asm/memory.h
@@ -309,20 +309,6 @@ static inline const void *__tag_set(const void *addr, u8 tag)
 	return (const void *)(__addr | __tag_shifted(tag));
 }
 
-#ifdef CONFIG_KASAN_HW_TAGS
-#define arch_enable_tag_checks_sync()		mte_enable_kernel_sync()
-#define arch_enable_tag_checks_async()		mte_enable_kernel_async()
-#define arch_enable_tag_checks_asymm()		mte_enable_kernel_asymm()
-#define arch_enable_tag_checks_write_only()	mte_enable_kernel_store_only()
-#define arch_suppress_tag_checks_start()	mte_enable_tco()
-#define arch_suppress_tag_checks_stop()		mte_disable_tco()
-#define arch_flush_async_tag_fault()		mte_get_reset_tfsr()
-#define arch_set_random_tag(addr)		mte_set_random_tag(addr)
-#define arch_get_mem_tag(addr)			mte_get_mem_tag(addr)
-#define arch_set_mem_tag_range(addr, size, tag, init)	\
-			mte_set_mem_tag_range((addr), (size), (tag), (init))
-#endif /* CONFIG_KASAN_HW_TAGS */
-
 /*
  * Physical vs virtual RAM address space conversion.  These are
  * private definitions which should NOT be used outside memory.h
diff --git a/arch/arm64/include/asm/mte-kasan.h b/arch/arm64/include/asm/mte-kasan.h
index 81a8cf3b9a95..c0224a2bb515 100644
--- a/arch/arm64/include/asm/mte-kasan.h
+++ b/arch/arm64/include/asm/mte-kasan.h
@@ -46,98 +46,74 @@ static __always_inline u8 kasan_tag_from_mte(u8 tag)
 	return u8_replace_bits(KASAN_TAG_MIN, tag, KASAN_TAG_MTE_MASK);
 }
 
-#ifdef CONFIG_ARM64_MTE
+#ifdef CONFIG_KASAN_HW_TAGS
 
 /*
- * These functions are meant to be only used from KASAN runtime through
- * the arch_*() interface defined in asm/memory.h.
  * These functions don't include system_supports_mte() checks,
  * as KASAN only calls them when MTE is supported and enabled.
  */
 
-static inline void mte_disable_tco(void)
+static __always_inline void arch_enable_tag_checks_sync(void)
 {
-	set_pstate_tco(0);
+	mte_enable_kernel_sync();
 }
 
-static inline void mte_enable_tco(void)
+static __always_inline void arch_enable_tag_checks_async(void)
 {
-	set_pstate_tco(1);
+	mte_enable_kernel_async();
 }
 
-/* Get allocation tag for the address. */
-static inline u8 mte_get_mem_tag(void *addr)
+static __always_inline void arch_enable_tag_checks_asymm(void)
 {
-	return kasan_tag_from_mte(__mte_tag_of(__ldg((u64)addr)));
+	mte_enable_kernel_asymm();
 }
 
-/* Generate a random tag. */
-static inline void *mte_set_random_tag(const void *addr)
+static __always_inline int arch_enable_tag_checks_write_only(void)
 {
-	/* Writes the random MTE tag in bits [59:56]. */
-	addr = (const void *)__irg((u64)addr);
-
-	/* Sets bits [63:60] as KASAN expects. */
-	return (void *)__tag_set(addr, kasan_tag_from_mte(mte_tag_of(addr)));
+	return mte_enable_kernel_store_only();
 }
 
-static inline void mte_set_mem_tag_range(void *addr, size_t size, u8 tag,
-					 bool zero)
+static __always_inline void arch_suppress_tag_checks_stop(void)
 {
-	if (zero)
-		mte_tag_and_zero_range(addr, size, kasan_tag_to_mte(tag));
-	else
-		mte_tag_range(addr, size, kasan_tag_to_mte(tag));
-}
-
-void mte_enable_kernel_sync(void);
-void mte_enable_kernel_async(void);
-void mte_enable_kernel_asymm(void);
-int mte_enable_kernel_store_only(void);
-
-#else /* CONFIG_ARM64_MTE */
-
-static inline void mte_disable_tco(void)
-{
-}
-
-static inline void mte_enable_tco(void)
-{
-}
-
-static inline u8 mte_get_mem_tag(void *addr)
-{
-	return 0xFF;
+	set_pstate_tco(0);
 }
 
-static inline void *mte_set_random_tag(const void *addr)
+static __always_inline void arch_suppress_tag_checks_start(void)
 {
-	return (void *)__tag_set(addr, 0xFF);
+	set_pstate_tco(1);
 }
 
-static inline void mte_set_mem_tag_range(void *addr, size_t size,
-						u8 tag, bool init)
+static __always_inline bool arch_flush_async_tag_fault(void)
 {
+	return mte_get_reset_tfsr();
 }
 
-static inline void mte_enable_kernel_sync(void)
+/* Get allocation tag for the address. */
+static __always_inline u8 arch_get_mem_tag(void *addr)
 {
+	return kasan_tag_from_mte(__mte_tag_of(__ldg((u64)addr)));
 }
 
-static inline void mte_enable_kernel_async(void)
+/* Generate a random tag. */
+static __always_inline void *arch_set_random_tag(const void *addr)
 {
-}
+	/* Writes the random MTE tag in bits [59:56]. */
+	addr = (const void *)__irg((u64)addr);
 
-static inline void mte_enable_kernel_asymm(void)
-{
+	/* Sets bits [63:60] as KASAN expects. */
+	return (void *)__tag_set(addr, kasan_tag_from_mte(mte_tag_of(addr)));
 }
 
-static inline int mte_enable_kernel_store_only(void)
+static __always_inline void arch_set_mem_tag_range(void *addr, size_t size,
+						   u8 tag, bool zero)
 {
-	return -EINVAL;
+	if (zero)
+		mte_tag_and_zero_range(addr, size, kasan_tag_to_mte(tag));
+	else
+		mte_tag_range(addr, size, kasan_tag_to_mte(tag));
 }
 
-#endif /* CONFIG_ARM64_MTE */
+#endif /* CONFIG_KASAN_HW_TAGS */
 
 #endif /* __ASSEMBLER__ */
 
diff --git a/arch/arm64/include/asm/mte.h b/arch/arm64/include/asm/mte.h
index 9ba215b2b0fd..34d1438019ed 100644
--- a/arch/arm64/include/asm/mte.h
+++ b/arch/arm64/include/asm/mte.h
@@ -47,6 +47,11 @@ static inline bool system_uses_async_mte_faults(void)
 {
 	return static_branch_unlikely(&mte_async_or_asymm_mode);
 }
+
+void mte_enable_kernel_sync(void);
+void mte_enable_kernel_async(void);
+void mte_enable_kernel_asymm(void);
+int mte_enable_kernel_store_only(void);
 #else
 static inline bool system_uses_sync_mte_faults(void)
 {

-- 
2.56.0.455.g6c5a503b38-goog


  parent reply	other threads:[~2026-11-07 15:37 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-07 15:36 [PATCH 00/24] arm64: Decouple MTE code from KASAN support Pierre-Clément Tosi
2026-11-07 15:36 ` [PATCH 01/24] arm64: Fix lazy TCO clear when enabling user MTE Pierre-Clément Tosi
2026-11-07 15:36 ` [PATCH 02/24] kselftest/arm64: Test lazy PSTATE.TCO handling Pierre-Clément Tosi
2026-11-07 15:36 ` [PATCH 03/24] arm64: Fix volatility of IRG inline assembly Pierre-Clément Tosi
2026-11-07 15:36 ` [PATCH 04/24] arm64: Skip TCO on privileged uaccess without kMTE Pierre-Clément Tosi
2026-11-07 15:36 ` [PATCH 05/24] arm64: Check MTE once per fault-handling section Pierre-Clément Tosi
2026-11-07 15:36 ` [PATCH 06/24] arm64: Harden is_ttbr1_addr() on tagged addresses Pierre-Clément Tosi
2026-11-07 15:36 ` [PATCH 07/24] arm64: Skip redundant TCO clear post-kernel entry Pierre-Clément Tosi
2026-11-07 15:36 ` [PATCH 08/24] arm64: Introduce mte_get_reset_tfsr() helpers Pierre-Clément Tosi
2026-11-07 15:36 ` [PATCH 09/24] kasan: rework hw_{force=>flush}_async_tag_fault() Pierre-Clément Tosi
2026-11-07 15:36 ` [PATCH 10/24] kasan: rework hw_{get=>set}_random_tag() Pierre-Clément Tosi
2026-11-07 15:36 ` [PATCH 11/24] kasan: provide empty stub for kasan_report_async() Pierre-Clément Tosi
2026-11-07 15:36 ` [PATCH 12/24] kasan: drop mte-kasan.h from arch-agnostic header Pierre-Clément Tosi
2026-11-07 15:36 ` [PATCH 13/24] arm64: Introduce set_pstate_tco() helper macro Pierre-Clément Tosi
2026-11-07 15:36 ` [PATCH 14/24] arm64: Add mte_{enter,exit}_*_{kernel,user}_mode() Pierre-Clément Tosi
2026-11-07 15:36 ` [PATCH 15/24] arm64: Refactor and remove mte_disable_tco_entry() Pierre-Clément Tosi
2026-11-07 15:36 ` [PATCH 16/24] arm64: Refactor KERNEL_GCR_EL1 out of <sysreg.h> Pierre-Clément Tosi
2026-11-07 15:36 ` [PATCH 17/24] arm64: Introduce MTE & KASAN tag handling helpers Pierre-Clément Tosi
2026-11-07 15:36 ` [PATCH 18/24] arm64: Inline and remove mte_check_tfsr_*() Pierre-Clément Tosi
2026-11-07 15:36 ` [PATCH 19/24] arm64: Rework static branches for kernel MTE Pierre-Clément Tosi
2026-11-07 15:36 ` [PATCH 20/24] arm64: Move MTE insn wrappers to <asm/mte-def.h> Pierre-Clément Tosi
2026-11-07 15:36 ` [PATCH 21/24] arm64: Introduce mte_tag[_and_zero]_range() Pierre-Clément Tosi
2026-11-07 15:36 ` Pierre-Clément Tosi [this message]
2026-11-07 15:36 ` [PATCH 23/24] arm64: Fold <asm/mte-kasan.h> into <asm/kasan.h> Pierre-Clément Tosi
2026-11-07 15:36 ` [PATCH 24/24] arm64: Introduce CONFIG_ARM64_MTE_KERNEL 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=20261107-mte-kasan-split-v1-22-d84ea5f30f94@google.com \
    --to=ptosi@google.com \
    --cc=andreyknvl@gmail.com \
    --cc=catalin.marinas@arm.com \
    --cc=dvyukov@google.com \
    --cc=glider@google.com \
    --cc=kasan-dev@googlegroups.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=ryabinin.a.a@gmail.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

all inboxes | Powered by JetHome®