[PATCH 23/24] arm64: Fold <asm/mte-kasan.h> into <asm/kasan.h>

Pierre-Clément Tosi ptosi at google.com
Sat Nov 7 07:36:29 PST 2026


Now that all MTE-specific implementation details have been extracted to
<asm/mte-def.h> and <asm/mte.h>, <asm/mte-kasan.h> only contains a thin
KASAN-to-MTE wrapping layer so fold it directly into <asm/kasan.h> to
prevent confusion or misuse and simplify header dependencies.

Update MAINTAINERS to remove the pattern that was specifically added to
track this header. This silences a checkpatch warning. If future arm64
KASAN headers are added, they should probably favor the arch-agnostic
header naming convention <asm/kasan*.h> instead, anyway.

Cc: Alexander Potapenko <glider at google.com>
Cc: Andrey Konovalov <andreyknvl at gmail.com>
Cc: Andrey Ryabinin <ryabinin.a.a at gmail.com>
Cc: Dmitry Vyukov <dvyukov at google.com>
Cc: kasan-dev at googlegroups.com
Signed-off-by: Pierre-Clément Tosi <ptosi at google.com>
---
 MAINTAINERS                        |   2 +-
 arch/arm64/include/asm/kasan.h     | 100 ++++++++++++++++++++++++++++++-
 arch/arm64/include/asm/mte-kasan.h | 120 -------------------------------------
 arch/arm64/kernel/entry.S          |   2 +-
 arch/arm64/kernel/mte.c            |   2 +-
 5 files changed, 101 insertions(+), 125 deletions(-)

diff --git a/MAINTAINERS b/MAINTAINERS
index 93abbb315065..990237c9ac32 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -14358,7 +14358,7 @@ L:	kasan-dev at googlegroups.com
 S:	Maintained
 B:	https://bugzilla.kernel.org/buglist.cgi?component=Sanitizers&product=Memory%20Management
 F:	Documentation/dev-tools/kasan.rst
-F:	arch/*/include/asm/*kasan.h
+F:	arch/*/include/asm/kasan.h
 F:	arch/*/mm/kasan_init*
 F:	include/linux/kasan*.h
 F:	lib/Kconfig.kasan
diff --git a/arch/arm64/include/asm/kasan.h b/arch/arm64/include/asm/kasan.h
index 3106a2271781..45af725d6fd3 100644
--- a/arch/arm64/include/asm/kasan.h
+++ b/arch/arm64/include/asm/kasan.h
@@ -2,18 +2,54 @@
 #ifndef __ASM_KASAN_H
 #define __ASM_KASAN_H
 
+#include <linux/bits.h>
+#include <linux/const.h>
+#include <linux/kasan-tags.h>
+
+#include <asm/mte-def.h>
+
+#define __KASAN_TAG_MTE_MASK		GENMASK(MTE_TAG_SIZE - 1, 0)
+/*
+ * KASAN always uses a whole byte for its tags. With CONFIG_KASAN_HW_TAGS it
+ * only uses tags in the range 0xF0-0xFF, which we map to MTE tags 0x0-0xF.
+ */
+#define __kasan_tag_to_mte(tag)		((tag) & __KASAN_TAG_MTE_MASK)
+#define __kasan_tag_from_mte(tag)	((KASAN_TAG_MIN & ~__KASAN_TAG_MTE_MASK) | (tag))
+
+#ifdef CONFIG_KASAN_HW_TAGS
+#define KASAN_GCR_INCL \
+	GENMASK(__kasan_tag_to_mte(KASAN_TAG_MAX), __kasan_tag_to_mte(KASAN_TAG_MIN))
+#else
+#define KASAN_GCR_INCL		UL(0)
+#endif
+
 #ifndef __ASSEMBLER__
 
+#include <linux/bitfield.h>
 #include <linux/linkage.h>
+#include <linux/types.h>
+
 #include <asm/memory.h>
-#include <asm/mte-def.h>
-#include <asm/mte-kasan.h>
+#include <asm/mte.h>
 #include <asm/pgtable-types.h>
+#include <asm/sysreg.h>
 
 #define arch_kasan_set_tag(addr, tag)	__tag_set(addr, tag)
 #define arch_kasan_reset_tag(addr)	__tag_reset(addr)
 #define arch_kasan_get_tag(addr)	__tag_get(addr)
 
+#define KASAN_TAG_MTE_MASK		GENMASK_U8(MTE_TAG_SIZE - 1, 0)
+
+static __always_inline u8 kasan_tag_to_mte(u8 tag)
+{
+	return u8_get_bits(tag, KASAN_TAG_MTE_MASK);
+}
+
+static __always_inline u8 kasan_tag_from_mte(u8 tag)
+{
+	return u8_replace_bits(KASAN_TAG_MIN, tag, KASAN_TAG_MTE_MASK);
+}
+
 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
 
 asmlinkage void kasan_early_init(void);
@@ -24,7 +60,67 @@ static inline void kasan_init(void) { }
 #endif
 
 #ifdef CONFIG_KASAN_HW_TAGS
+
 #define KASAN_GRANULE_SIZE	MTE_GRANULE_SIZE
+
+static __always_inline void arch_enable_tag_checks_sync(void)
+{
+	mte_enable_kernel_sync();
+}
+
+static __always_inline void arch_enable_tag_checks_async(void)
+{
+	mte_enable_kernel_async();
+}
+
+static __always_inline void arch_enable_tag_checks_asymm(void)
+{
+	mte_enable_kernel_asymm();
+}
+
+static __always_inline int arch_enable_tag_checks_write_only(void)
+{
+	return mte_enable_kernel_store_only();
+}
+
+static __always_inline void arch_suppress_tag_checks_stop(void)
+{
+	set_pstate_tco(0);
+}
+
+static __always_inline void arch_suppress_tag_checks_start(void)
+{
+	set_pstate_tco(1);
+}
+
+static __always_inline bool arch_flush_async_tag_fault(void)
+{
+	return mte_get_reset_tfsr();
+}
+
+static __always_inline u8 arch_get_mem_tag(void *addr)
+{
+	return kasan_tag_from_mte(__mte_tag_of(__ldg((u64)addr)));
+}
+
+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);
+
+	/* Sets bits [63:60] as KASAN expects. */
+	return (void *)__tag_set(addr, kasan_tag_from_mte(mte_tag_of(addr)));
+}
+
+static __always_inline void arch_set_mem_tag_range(void *addr, size_t size,
+						   u8 tag, bool zero)
+{
+	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_KASAN_HW_TAGS */
 
 #endif
diff --git a/arch/arm64/include/asm/mte-kasan.h b/arch/arm64/include/asm/mte-kasan.h
deleted file mode 100644
index c0224a2bb515..000000000000
--- a/arch/arm64/include/asm/mte-kasan.h
+++ /dev/null
@@ -1,120 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 */
-/*
- * Copyright (C) 2020 ARM Ltd.
- */
-#ifndef __ASM_MTE_KASAN_H
-#define __ASM_MTE_KASAN_H
-
-#include <linux/bits.h>
-#include <linux/const.h>
-#include <linux/kasan-tags.h>
-
-#include <asm/mte-def.h>
-
-#define __KASAN_TAG_MTE_MASK		GENMASK(MTE_TAG_SIZE - 1, 0)
-/*
- * KASAN always uses a whole byte for its tags. With CONFIG_KASAN_HW_TAGS it
- * only uses tags in the range 0xF0-0xFF, which we map to MTE tags 0x0-0xF.
- */
-#define __kasan_tag_to_mte(tag)		((tag) & __KASAN_TAG_MTE_MASK)
-#define __kasan_tag_from_mte(tag)	((KASAN_TAG_MIN & ~__KASAN_TAG_MTE_MASK) | (tag))
-
-#ifdef CONFIG_KASAN_HW_TAGS
-#define KASAN_GCR_INCL \
-	GENMASK(__kasan_tag_to_mte(KASAN_TAG_MAX), __kasan_tag_to_mte(KASAN_TAG_MIN))
-#else
-#define KASAN_GCR_INCL		UL(0)
-#endif
-
-#ifndef __ASSEMBLER__
-
-#include <linux/bitfield.h>
-#include <linux/types.h>
-
-#include <asm/mte.h>
-#include <asm/sysreg.h>
-
-#define KASAN_TAG_MTE_MASK		GENMASK_U8(MTE_TAG_SIZE - 1, 0)
-
-static __always_inline u8 kasan_tag_to_mte(u8 tag)
-{
-	return u8_get_bits(tag, KASAN_TAG_MTE_MASK);
-}
-
-static __always_inline u8 kasan_tag_from_mte(u8 tag)
-{
-	return u8_replace_bits(KASAN_TAG_MIN, tag, KASAN_TAG_MTE_MASK);
-}
-
-#ifdef CONFIG_KASAN_HW_TAGS
-
-/*
- * These functions don't include system_supports_mte() checks,
- * as KASAN only calls them when MTE is supported and enabled.
- */
-
-static __always_inline void arch_enable_tag_checks_sync(void)
-{
-	mte_enable_kernel_sync();
-}
-
-static __always_inline void arch_enable_tag_checks_async(void)
-{
-	mte_enable_kernel_async();
-}
-
-static __always_inline void arch_enable_tag_checks_asymm(void)
-{
-	mte_enable_kernel_asymm();
-}
-
-static __always_inline int arch_enable_tag_checks_write_only(void)
-{
-	return mte_enable_kernel_store_only();
-}
-
-static __always_inline void arch_suppress_tag_checks_stop(void)
-{
-	set_pstate_tco(0);
-}
-
-static __always_inline void arch_suppress_tag_checks_start(void)
-{
-	set_pstate_tco(1);
-}
-
-static __always_inline bool arch_flush_async_tag_fault(void)
-{
-	return mte_get_reset_tfsr();
-}
-
-/* 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)));
-}
-
-/* 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);
-
-	/* Sets bits [63:60] as KASAN expects. */
-	return (void *)__tag_set(addr, kasan_tag_from_mte(mte_tag_of(addr)));
-}
-
-static __always_inline void arch_set_mem_tag_range(void *addr, size_t size,
-						   u8 tag, bool zero)
-{
-	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_KASAN_HW_TAGS */
-
-#endif /* __ASSEMBLER__ */
-
-#endif /* __ASM_MTE_KASAN_H  */
diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S
index d1d9a7a049a7..2da704f53b5a 100644
--- a/arch/arm64/kernel/entry.S
+++ b/arch/arm64/kernel/entry.S
@@ -20,8 +20,8 @@
 #include <asm/errno.h>
 #include <asm/esr.h>
 #include <asm/irq.h>
+#include <asm/kasan.h>
 #include <asm/memory.h>
-#include <asm/mte-kasan.h>
 #include <asm/mmu.h>
 #include <asm/processor.h>
 #include <asm/ptrace.h>
diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c
index 675ba6a803cf..d072cb76ad05 100644
--- a/arch/arm64/kernel/mte.c
+++ b/arch/arm64/kernel/mte.c
@@ -24,8 +24,8 @@
 
 #include <asm/barrier.h>
 #include <asm/cpufeature.h>
+#include <asm/kasan.h>
 #include <asm/mte.h>
-#include <asm/mte-kasan.h>
 #include <asm/ptrace.h>
 #include <asm/sysreg.h>
 

-- 
2.56.0.455.g6c5a503b38-goog




More information about the linux-arm-kernel mailing list