[PATCH v2 14/14] x86: Make software tag-based kasan available

From: Maciej Wieczor-Retman
Date: Tue Feb 18 2025 - 03:29:25 EST


Make CONFIG_KASAN_SW_TAGS available for x86 machines if they have
ADDRESS_MASKING enabled (LAM) as that works similarly to Top-Byte Ignore
(TBI) that allows the software tag-based mode on arm64 platform.

Set scale macro based on KASAN mode: in software tag-based mode 32 bytes
of memory map to one shadow byte and 16 in generic mode.

Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@xxxxxxxxx>
---
Changelog v2:
- Remove KASAN dense code.

arch/x86/Kconfig | 6 ++++++
arch/x86/boot/compressed/misc.h | 1 +
arch/x86/include/asm/kasan.h | 2 +-
arch/x86/kernel/setup.c | 2 ++
4 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index f4ef64bf824a..dc48eb5b664f 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -195,6 +195,7 @@ config X86
select HAVE_ARCH_JUMP_LABEL_RELATIVE
select HAVE_ARCH_KASAN if X86_64
select HAVE_ARCH_KASAN_VMALLOC if X86_64
+ select HAVE_ARCH_KASAN_SW_TAGS if ADDRESS_MASKING
select HAVE_ARCH_KFENCE
select HAVE_ARCH_KMSAN if X86_64
select HAVE_ARCH_KGDB
@@ -402,6 +403,11 @@ config KASAN_SHADOW_OFFSET
hex
default 0xdffffc0000000000 if KASAN_GENERIC

+config KASAN_SHADOW_SCALE_SHIFT
+ int
+ default 4 if KASAN_SW_TAGS
+ default 3
+
config HAVE_INTEL_TXT
def_bool y
depends on INTEL_IOMMU && ACPI
diff --git a/arch/x86/boot/compressed/misc.h b/arch/x86/boot/compressed/misc.h
index dd8d1a85f671..f6a87e9ad200 100644
--- a/arch/x86/boot/compressed/misc.h
+++ b/arch/x86/boot/compressed/misc.h
@@ -13,6 +13,7 @@
#undef CONFIG_PARAVIRT_SPINLOCKS
#undef CONFIG_KASAN
#undef CONFIG_KASAN_GENERIC
+#undef CONFIG_KASAN_SW_TAGS

#define __NO_FORTIFY

diff --git a/arch/x86/include/asm/kasan.h b/arch/x86/include/asm/kasan.h
index 4bfd3641af84..cfc31e4a2f70 100644
--- a/arch/x86/include/asm/kasan.h
+++ b/arch/x86/include/asm/kasan.h
@@ -6,7 +6,7 @@
#include <linux/kasan-tags.h>
#include <linux/types.h>

-#define KASAN_SHADOW_SCALE_SHIFT 3
+#define KASAN_SHADOW_SCALE_SHIFT CONFIG_KASAN_SHADOW_SCALE_SHIFT

/*
* Compiler uses shadow offset assuming that addresses start
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
index cebee310e200..768990c573ea 100644
--- a/arch/x86/kernel/setup.c
+++ b/arch/x86/kernel/setup.c
@@ -1124,6 +1124,8 @@ void __init setup_arch(char **cmdline_p)

kasan_init();

+ kasan_init_sw_tags();
+
/*
* Sync back kernel address range.
*
--
2.47.1