[PATCH v4 02/18] kasan: sw_tags: Support tag widths less than 8 bits

Maciej Wieczor-Retman posted 18 patches 1 month, 3 weeks ago
There is a newer version of this series
[PATCH v4 02/18] kasan: sw_tags: Support tag widths less than 8 bits
Posted by Maciej Wieczor-Retman 1 month, 3 weeks ago
From: Samuel Holland <samuel.holland@sifive.com>

Allow architectures to override KASAN_TAG_KERNEL in asm/kasan.h. This
is needed on RISC-V, which supports 57-bit virtual addresses and 7-bit
pointer tags. For consistency, move the arm64 MTE definition of
KASAN_TAG_MIN to asm/kasan.h, since it is also architecture-dependent;
RISC-V's equivalent extension is expected to support 7-bit hardware
memory tags.

Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com>
Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
---
 arch/arm64/include/asm/kasan.h   |  6 ++++--
 arch/arm64/include/asm/uaccess.h |  1 +
 include/linux/kasan-tags.h       | 13 ++++++++-----
 3 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/arch/arm64/include/asm/kasan.h b/arch/arm64/include/asm/kasan.h
index e1b57c13f8a4..4ab419df8b93 100644
--- a/arch/arm64/include/asm/kasan.h
+++ b/arch/arm64/include/asm/kasan.h
@@ -6,8 +6,10 @@
 
 #include <linux/linkage.h>
 #include <asm/memory.h>
-#include <asm/mte-kasan.h>
-#include <asm/pgtable-types.h>
+
+#ifdef CONFIG_KASAN_HW_TAGS
+#define KASAN_TAG_MIN			0xF0 /* minimum value for random tags */
+#endif
 
 #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/uaccess.h
index 5b91803201ef..f890dadc7b4e 100644
--- a/arch/arm64/include/asm/uaccess.h
+++ b/arch/arm64/include/asm/uaccess.h
@@ -22,6 +22,7 @@
 #include <asm/cpufeature.h>
 #include <asm/mmu.h>
 #include <asm/mte.h>
+#include <asm/mte-kasan.h>
 #include <asm/ptrace.h>
 #include <asm/memory.h>
 #include <asm/extable.h>
diff --git a/include/linux/kasan-tags.h b/include/linux/kasan-tags.h
index 4f85f562512c..e07c896f95d3 100644
--- a/include/linux/kasan-tags.h
+++ b/include/linux/kasan-tags.h
@@ -2,13 +2,16 @@
 #ifndef _LINUX_KASAN_TAGS_H
 #define _LINUX_KASAN_TAGS_H
 
+#include <asm/kasan.h>
+
+#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 tag */
+#define KASAN_TAG_MAX		(KASAN_TAG_KERNEL - 2) /* maximum value for random tags */
 
-#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
 
-- 
2.50.1
Re: [PATCH v4 02/18] kasan: sw_tags: Support tag widths less than 8 bits
Posted by Ada Couprie Diaz 1 month, 3 weeks ago
Hi,

On 12/08/2025 14:23, Maciej Wieczor-Retman wrote:
> From: Samuel Holland <samuel.holland@sifive.com>
>
> Allow architectures to override KASAN_TAG_KERNEL in asm/kasan.h. This
> is needed on RISC-V, which supports 57-bit virtual addresses and 7-bit
> pointer tags. For consistency, move the arm64 MTE definition of
> KASAN_TAG_MIN to asm/kasan.h, since it is also architecture-dependent;
> RISC-V's equivalent extension is expected to support 7-bit hardware
> memory tags.
>
> Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com>
> Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
> Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
> ---
>   arch/arm64/include/asm/kasan.h   |  6 ++++--
>   arch/arm64/include/asm/uaccess.h |  1 +
>   include/linux/kasan-tags.h       | 13 ++++++++-----
>   3 files changed, 13 insertions(+), 7 deletions(-)
>
> diff --git a/arch/arm64/include/asm/kasan.h b/arch/arm64/include/asm/kasan.h
> index e1b57c13f8a4..4ab419df8b93 100644
> --- a/arch/arm64/include/asm/kasan.h
> +++ b/arch/arm64/include/asm/kasan.h
> @@ -6,8 +6,10 @@
>   
>   #include <linux/linkage.h>
>   #include <asm/memory.h>
> -#include <asm/mte-kasan.h>
> -#include <asm/pgtable-types.h>
> +
> +#ifdef CONFIG_KASAN_HW_TAGS
> +#define KASAN_TAG_MIN			0xF0 /* minimum value for random tags */
> +#endif
Building CONFIG_KASAN_HW_TAGS with -Werror on arm64 fails here
due to a warning about KASAN_TAG_MIN being redefined.

On my side the error got triggered when compiling
arch/arm64/kernel/asm-offsets.c due to the ordering of some includes :
from <asm/processor.h>, <linux/kasan-tags.h> ends up being included
(by <asm/cpufeatures.h> including <asm/sysreg.h>) before <asm/kasan.h>.
(Build trace at the end for reference)

Adding `#undef KASAN_TAG_MIN` before redefining the arch version
allows building CONFIG_KASAN_HW_TAGS on arm64 without
further issues, but I don't know if this is most appropriate fix.Thanks, 
Ada ---

   CC      arch/arm64/kernel/asm-offsets.s
In file included from ./arch/arm64/include/asm/processor.h:42,
                  from ./include/asm-generic/qrwlock.h:18,
                  from ./arch/arm64/include/generated/asm/qrwlock.h:1,
                  from ./arch/arm64/include/asm/spinlock.h:9,
                  from ./include/linux/spinlock.h:95,
                  from ./include/linux/mmzone.h:8,
                  from ./include/linux/gfp.h:7,
                  from ./include/linux/slab.h:16,
                  from ./include/linux/resource_ext.h:11,
                  from ./include/linux/acpi.h:13,
                  from ./include/acpi/apei.h:9,
                  from ./include/acpi/ghes.h:5,
                  from ./include/linux/arm_sdei.h:8,
                  from ./arch/arm64/kernel/asm-offsets.c:10:
./arch/arm64/include/asm/kasan.h:11: error: "KASAN_TAG_MIN" redefined [-Werror]
    11 | #define KASAN_TAG_MIN                   0xF0 /* minimum value for random tags */
       |
In file included from ./arch/arm64/include/asm/sysreg.h:14,
                  from ./arch/arm64/include/asm/cputype.h:250,
                  from ./arch/arm64/include/asm/cache.h:43,
                  from ./include/vdso/cache.h:5,
                  from ./include/linux/cache.h:6,
                  from ./include/linux/slab.h:15:
./include/linux/kasan-tags.h:23: note: this is the location of the previous definition
    23 | #define KASAN_TAG_MIN           0x00 /* minimum value for random tags */
       |
Re: [PATCH v4 02/18] kasan: sw_tags: Support tag widths less than 8 bits
Posted by Maciej Wieczor-Retman 1 month, 2 weeks ago
Hi, thanks for pointing it out :).

I'll cross compile for arm64 it with different KASAN settings and fix any such
errors. I did this a while ago and it went okay then, but there were so many
rebases in the meantime I must have missed something.

Kind regards
Maciej Wieczór-Retman

On 2025-08-13 at 15:48:32 +0100, Ada Couprie Diaz wrote:
>Hi,
>
>On 12/08/2025 14:23, Maciej Wieczor-Retman wrote:
>> From: Samuel Holland <samuel.holland@sifive.com>
>> 
>> Allow architectures to override KASAN_TAG_KERNEL in asm/kasan.h. This
>> is needed on RISC-V, which supports 57-bit virtual addresses and 7-bit
>> pointer tags. For consistency, move the arm64 MTE definition of
>> KASAN_TAG_MIN to asm/kasan.h, since it is also architecture-dependent;
>> RISC-V's equivalent extension is expected to support 7-bit hardware
>> memory tags.
>> 
>> Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com>
>> Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
>> Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
>> ---
>>   arch/arm64/include/asm/kasan.h   |  6 ++++--
>>   arch/arm64/include/asm/uaccess.h |  1 +
>>   include/linux/kasan-tags.h       | 13 ++++++++-----
>>   3 files changed, 13 insertions(+), 7 deletions(-)
>> 
>> diff --git a/arch/arm64/include/asm/kasan.h b/arch/arm64/include/asm/kasan.h
>> index e1b57c13f8a4..4ab419df8b93 100644
>> --- a/arch/arm64/include/asm/kasan.h
>> +++ b/arch/arm64/include/asm/kasan.h
>> @@ -6,8 +6,10 @@
>>   #include <linux/linkage.h>
>>   #include <asm/memory.h>
>> -#include <asm/mte-kasan.h>
>> -#include <asm/pgtable-types.h>
>> +
>> +#ifdef CONFIG_KASAN_HW_TAGS
>> +#define KASAN_TAG_MIN			0xF0 /* minimum value for random tags */
>> +#endif
>Building CONFIG_KASAN_HW_TAGS with -Werror on arm64 fails here
>due to a warning about KASAN_TAG_MIN being redefined.
>
>On my side the error got triggered when compiling
>arch/arm64/kernel/asm-offsets.c due to the ordering of some includes :
>from <asm/processor.h>, <linux/kasan-tags.h> ends up being included
>(by <asm/cpufeatures.h> including <asm/sysreg.h>) before <asm/kasan.h>.
>(Build trace at the end for reference)
>
>Adding `#undef KASAN_TAG_MIN` before redefining the arch version
>allows building CONFIG_KASAN_HW_TAGS on arm64 without
>further issues, but I don't know if this is most appropriate fix.Thanks, Ada
>---
>
>  CC      arch/arm64/kernel/asm-offsets.s
>In file included from ./arch/arm64/include/asm/processor.h:42,
>                 from ./include/asm-generic/qrwlock.h:18,
>                 from ./arch/arm64/include/generated/asm/qrwlock.h:1,
>                 from ./arch/arm64/include/asm/spinlock.h:9,
>                 from ./include/linux/spinlock.h:95,
>                 from ./include/linux/mmzone.h:8,
>                 from ./include/linux/gfp.h:7,
>                 from ./include/linux/slab.h:16,
>                 from ./include/linux/resource_ext.h:11,
>                 from ./include/linux/acpi.h:13,
>                 from ./include/acpi/apei.h:9,
>                 from ./include/acpi/ghes.h:5,
>                 from ./include/linux/arm_sdei.h:8,
>                 from ./arch/arm64/kernel/asm-offsets.c:10:
>./arch/arm64/include/asm/kasan.h:11: error: "KASAN_TAG_MIN" redefined [-Werror]
>   11 | #define KASAN_TAG_MIN                   0xF0 /* minimum value for random tags */
>      |
>In file included from ./arch/arm64/include/asm/sysreg.h:14,
>                 from ./arch/arm64/include/asm/cputype.h:250,
>                 from ./arch/arm64/include/asm/cache.h:43,
>                 from ./include/vdso/cache.h:5,
>                 from ./include/linux/cache.h:6,
>                 from ./include/linux/slab.h:15:
>./include/linux/kasan-tags.h:23: note: this is the location of the previous definition
>   23 | #define KASAN_TAG_MIN           0x00 /* minimum value for random tags */
>      |
>