From nobody Thu Apr 2 01:37:56 2026 Received: from mail-24418.protonmail.ch (mail-24418.protonmail.ch [109.224.244.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3CEFA2EACF2 for ; Mon, 30 Mar 2026 14:33:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=109.224.244.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774881205; cv=none; b=e71Xh4+gK9C1R05qhAWp4hrQW7g9MfRM/Je4wbdO96e0BkAT9AHUTEPMFbLmuuzU3ywnDY2IpQjTuryeDOv4W61hky5LWcgvuy11GeOfiB1QNWPblgsGQWcvU0AZxKzTojvnlne0dWBitGL0aKCSFeFpW97dmMX7gqiCc3/cT+Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774881205; c=relaxed/simple; bh=fZO8AL7K0FxGd4ROb7TxMN4qL/lNTujVpCbN7BisJ/Q=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Js/QluZ5Q9f9QWAE5fysrJ+z2u1iEEhvDfmZMC02l9EznDSHPza5hZQ3e4KvIMZ4Ah1S71nowk+NYGuCrBZN68I4koHevMjzYTRtoLe1vwDFsir++iuqOkFvsNNNjHcaPIDaDga9eze1hR3X698G2rZis+DVFGbnrgOfxOhc218= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b=CRGzF8JZ; arc=none smtp.client-ip=109.224.244.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b="CRGzF8JZ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1774881200; x=1775140400; bh=C6RPAE+IMjAOmkl7tc8DZi3umBRg/ZmgJ/efKnqWEoc=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=CRGzF8JZycNd3OpbKU8omrgqB8xu8NCxoq5VTplJozAziqnZGGS9uVmkE6/WhDVjb 55FDa21vBc1qB9w2GttFaUQ/hAvhTU4vFtugCVBsn/WmQEtl7O7yljs093gv0ys55s 1tTUUnRVHu1P6ae3QIi70DA1P+vlUbyyHIaw44q+0Tdl7nsXMSLFALYMk5SjQ38bYq HiHi2/QOTkQzHpkgS1rvrniaVt6dJYZcsDx1xIQmNUmb1BHHlqdEHGWfhVHX43iqL+ f+/7APqDjR8ZMqsReRqYKgxLCO75ehGM3Sfl1efLuitfGo5EfnZui9o6PchOP5Uh1V YhqGrbAtNT2rg== Date: Mon, 30 Mar 2026 14:33:16 +0000 To: Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Catalin Marinas , Will Deacon , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko From: Maciej Wieczor-Retman Cc: m.wieczorretman@pm.me, Samuel Holland , Maciej Wieczor-Retman , linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org Subject: [PATCH v12 02/15] kasan: arm64: x86: Make special tags arch specific Message-ID: <6080be7964fc726327186d5bf7979e16ddd282bb.1774872838.git.m.wieczorretman@pm.me> In-Reply-To: References: Feedback-ID: 164464600:user:proton X-Pm-Message-ID: 0ce3648d813dbd335bc37a151748b3b43270f6c0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Samuel Holland KASAN's tag-based mode defines multiple special tag values. They're reserved for: - Native kernel value. On arm64 it's 0xFF and it causes an early return in the tag checking function. - Invalid value. 0xFE marks an area as freed / unallocated. It's also the value that is used to initialize regions of shadow memory. - Min and max values. 0xFD is the highest value that can be randomly generated for a new tag. 0 is the minimal value with the exception of arm64's hardware mode where it is equal to 0xF0. Metadata macro is also defined: - Tag width equal to 8. Tag-based mode on x86 is going to use 4 bit wide tags so all the above values need to be changed accordingly. Make tag width and native kernel tag arch specific for x86 and arm64. Base the invalid tag value and the max value on the native kernel tag since they follow the same pattern on both mentioned architectures. Also generalize KASAN_SHADOW_INIT and 0xff used in various page_kasan_tag* helpers. Give KASAN_TAG_MIN the default value of zero, and move the special value for hw_tags arm64 to its arch specific kasan-tags.h. Signed-off-by: Samuel Holland Co-developed-by: Maciej Wieczor-Retman Signed-off-by: Maciej Wieczor-Retman Acked-by: Will Deacon (for the arm part) Reviewed-by: Andrey Konovalov Reviewed-by: Andrey Ryabinin --- Changelog v9: - Add Andrey Ryabinin's Reviewed-by tag. - Add Andrey Konovalov's Reviewed-by tag. Changelog v8: - Add Will's Acked-by tag. Changelog v7: - Reorder defines of arm64 tag width to prevent redefinition warnings. - Remove KASAN_TAG_MASK so it's only defined in mmzone.h (Andrey Konovalov) - Merge the 'support tag widths less than 8 bits' with this patch since they do similar things and overwrite each other. (Alexander) Changelog v6: - Add hardware tags KASAN_TAG_WIDTH value to the arm64 arch file. - Keep KASAN_TAG_MASK in the mmzone.h. - Remove ifndef from KASAN_SHADOW_INIT. Changelog v5: - Move KASAN_TAG_MIN to the arm64 kasan-tags.h for the hardware KASAN mode case. Changelog v4: - Move KASAN_TAG_MASK to kasan-tags.h. Changelog v2: - Remove risc-v from the patch. MAINTAINERS | 2 +- arch/arm64/include/asm/kasan-tags.h | 14 ++++++++++++++ arch/arm64/include/asm/kasan.h | 2 -- arch/arm64/include/asm/uaccess.h | 1 + arch/x86/include/asm/kasan-tags.h | 9 +++++++++ include/linux/kasan-tags.h | 19 ++++++++++++++----- include/linux/kasan.h | 3 +-- include/linux/mm.h | 6 +++--- include/linux/page-flags-layout.h | 9 +-------- 9 files changed, 44 insertions(+), 21 deletions(-) create mode 100644 arch/arm64/include/asm/kasan-tags.h create mode 100644 arch/x86/include/asm/kasan-tags.h diff --git a/MAINTAINERS b/MAINTAINERS index 16874c32e288..897210732d30 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -13735,7 +13735,7 @@ L: kasan-dev@googlegroups.com S: Maintained B: https://bugzilla.kernel.org/buglist.cgi?component=3DSanitizers&product= =3DMemory%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-tags.h b/arch/arm64/include/asm/k= asan-tags.h new file mode 100644 index 000000000000..259952677443 --- /dev/null +++ b/arch/arm64/include/asm/kasan-tags.h @@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __ASM_KASAN_TAGS_H +#define __ASM_KASAN_TAGS_H + +#define KASAN_TAG_KERNEL 0xFF /* native kernel pointers tag */ + +#ifdef CONFIG_KASAN_HW_TAGS +#define KASAN_TAG_MIN 0xF0 /* minimum value for random tags */ +#define KASAN_TAG_WIDTH 4 +#else +#define KASAN_TAG_WIDTH 8 +#endif + +#endif /* ASM_KASAN_TAGS_H */ diff --git a/arch/arm64/include/asm/kasan.h b/arch/arm64/include/asm/kasan.h index b167e9d3da91..fd4a8557d736 100644 --- a/arch/arm64/include/asm/kasan.h +++ b/arch/arm64/include/asm/kasan.h @@ -6,8 +6,6 @@ =20 #include #include -#include -#include =20 #define arch_kasan_set_tag(addr, tag) __tag_set(addr, tag) #define arch_kasan_reset_tag(addr) __tag_reset(addr) diff --git a/arch/arm64/include/asm/uaccess.h b/arch/arm64/include/asm/uacc= ess.h index 9810106a3f66..5465bc97ccdd 100644 --- a/arch/arm64/include/asm/uaccess.h +++ b/arch/arm64/include/asm/uaccess.h @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include diff --git a/arch/x86/include/asm/kasan-tags.h b/arch/x86/include/asm/kasan= -tags.h new file mode 100644 index 000000000000..68ba385bc75c --- /dev/null +++ b/arch/x86/include/asm/kasan-tags.h @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __ASM_KASAN_TAGS_H +#define __ASM_KASAN_TAGS_H + +#define KASAN_TAG_KERNEL 0xF /* native kernel pointers tag */ + +#define KASAN_TAG_WIDTH 4 + +#endif /* ASM_KASAN_TAGS_H */ diff --git a/include/linux/kasan-tags.h b/include/linux/kasan-tags.h index 4f85f562512c..ad5c11950233 100644 --- a/include/linux/kasan-tags.h +++ b/include/linux/kasan-tags.h @@ -2,13 +2,22 @@ #ifndef _LINUX_KASAN_TAGS_H #define _LINUX_KASAN_TAGS_H =20 +#if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS) +#include +#endif + +#ifndef KASAN_TAG_WIDTH +#define KASAN_TAG_WIDTH 0 +#endif + +#ifndef KASAN_TAG_KERNEL #define KASAN_TAG_KERNEL 0xFF /* native kernel pointers tag */ -#define KASAN_TAG_INVALID 0xFE /* inaccessible memory tag */ -#define KASAN_TAG_MAX 0xFD /* maximum value for random tags */ +#endif + +#define KASAN_TAG_INVALID (KASAN_TAG_KERNEL - 1) /* inaccessible memory ta= g */ +#define KASAN_TAG_MAX (KASAN_TAG_KERNEL - 2) /* maximum value for random = tags */ =20 -#ifdef CONFIG_KASAN_HW_TAGS -#define KASAN_TAG_MIN 0xF0 /* minimum value for random tags */ -#else +#ifndef KASAN_TAG_MIN #define KASAN_TAG_MIN 0x00 /* minimum value for random tags */ #endif =20 diff --git a/include/linux/kasan.h b/include/linux/kasan.h index fbff1b759c85..e18908f3ad6e 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -40,8 +40,7 @@ typedef unsigned int __bitwise kasan_vmalloc_flags_t; /* Software KASAN implementations use shadow memory. */ =20 #ifdef CONFIG_KASAN_SW_TAGS -/* This matches KASAN_TAG_INVALID. */ -#define KASAN_SHADOW_INIT 0xFE +#define KASAN_SHADOW_INIT KASAN_TAG_INVALID #else #define KASAN_SHADOW_INIT 0 #endif diff --git a/include/linux/mm.h b/include/linux/mm.h index 633bbf9a184a..09044934dda8 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2440,7 +2440,7 @@ static inline u8 page_kasan_tag(const struct page *pa= ge) =20 if (kasan_enabled()) { tag =3D (page->flags.f >> KASAN_TAG_PGSHIFT) & KASAN_TAG_MASK; - tag ^=3D 0xff; + tag ^=3D KASAN_TAG_KERNEL; } =20 return tag; @@ -2453,7 +2453,7 @@ static inline void page_kasan_tag_set(struct page *pa= ge, u8 tag) if (!kasan_enabled()) return; =20 - tag ^=3D 0xff; + tag ^=3D KASAN_TAG_KERNEL; old_flags =3D READ_ONCE(page->flags.f); do { flags =3D old_flags; @@ -2472,7 +2472,7 @@ static inline void page_kasan_tag_reset(struct page *= page) =20 static inline u8 page_kasan_tag(const struct page *page) { - return 0xff; + return KASAN_TAG_KERNEL; } =20 static inline void page_kasan_tag_set(struct page *page, u8 tag) { } diff --git a/include/linux/page-flags-layout.h b/include/linux/page-flags-l= ayout.h index 760006b1c480..b2cc4cb870e0 100644 --- a/include/linux/page-flags-layout.h +++ b/include/linux/page-flags-layout.h @@ -3,6 +3,7 @@ #define PAGE_FLAGS_LAYOUT_H =20 #include +#include #include =20 /* @@ -72,14 +73,6 @@ #define NODE_NOT_IN_PAGE_FLAGS 1 #endif =20 -#if defined(CONFIG_KASAN_SW_TAGS) -#define KASAN_TAG_WIDTH 8 -#elif defined(CONFIG_KASAN_HW_TAGS) -#define KASAN_TAG_WIDTH 4 -#else -#define KASAN_TAG_WIDTH 0 -#endif - #ifdef CONFIG_NUMA_BALANCING #define LAST__PID_SHIFT 8 #define LAST__PID_MASK ((1 << LAST__PID_SHIFT)-1) --=20 2.53.0