[RFC PATCH V3 17/43] rv64ilp32_abi: riscv: Adapt kasan memory layout
From: guoren
Date: Tue Mar 25 2025 - 08:38:40 EST
From: "Guo Ren (Alibaba DAMO Academy)" <guoren@xxxxxxxxxx>
For generic KASAN, the size of each memory granule is 8, which
needs 1/8 address space. The kernel space is 2GiB in rv64ilp32,
so we need 256MiB range (0x80000000 ~ 0x90000000), and the offset
is 0x7000000 for the whole 4GiB address space.
Virtual kernel memory layout:
fixmap : 0x90a00000 - 0x90ffffff (6144 kB)
pci io : 0x91000000 - 0x91ffffff ( 16 MB)
vmemmap : 0x92000000 - 0x93ffffff ( 32 MB)
vmalloc : 0x94000000 - 0xb3ffffff ( 512 MB)
modules : 0xb4000000 - 0xb7ffffff ( 64 MB)
lowmem : 0xc0000000 - 0xc7ffffff ( 128 MB)
kasan : 0x80000000 - 0x8fffffff ( 256 MB) <=
kernel : 0xb8000000 - 0xbfffffff ( 128 MB)
Signed-off-by: Guo Ren (Alibaba DAMO Academy) <guoren@xxxxxxxxxx>
---
arch/riscv/include/asm/kasan.h | 6 +++++-
arch/riscv/mm/kasan_init.c | 2 +-
2 files changed, 6 insertions(+), 2 deletions(-)
diff --git a/arch/riscv/include/asm/kasan.h b/arch/riscv/include/asm/kasan.h
index e6a0071bdb56..dd3a211bc5d0 100644
--- a/arch/riscv/include/asm/kasan.h
+++ b/arch/riscv/include/asm/kasan.h
@@ -21,7 +21,7 @@
* [KASAN_SHADOW_OFFSET, KASAN_SHADOW_END) cover all 64-bits of virtual
* addresses. So KASAN_SHADOW_OFFSET should satisfy the following equation:
* KASAN_SHADOW_OFFSET = KASAN_SHADOW_END -
- * (1ULL << (64 - KASAN_SHADOW_SCALE_SHIFT))
+ * (1ULL << (BITS_PER_LONG - KASAN_SHADOW_SCALE_SHIFT))
*/
#define KASAN_SHADOW_SCALE_SHIFT 3
@@ -31,7 +31,11 @@
* aligned on PGDIR_SIZE, so force its alignment to ease its population.
*/
#define KASAN_SHADOW_START ((KASAN_SHADOW_END - KASAN_SHADOW_SIZE) & PGDIR_MASK)
+#if defined(CONFIG_64BIT) && (BITS_PER_LONG == 32)
+#define KASAN_SHADOW_END 0x90000000UL
+#else
#define KASAN_SHADOW_END MODULES_LOWEST_VADDR
+#endif
#ifdef CONFIG_KASAN
#define KASAN_SHADOW_OFFSET _AC(CONFIG_KASAN_SHADOW_OFFSET, UL)
diff --git a/arch/riscv/mm/kasan_init.c b/arch/riscv/mm/kasan_init.c
index 41c635d6aca4..1e864598779a 100644
--- a/arch/riscv/mm/kasan_init.c
+++ b/arch/riscv/mm/kasan_init.c
@@ -324,7 +324,7 @@ asmlinkage void __init kasan_early_init(void)
uintptr_t i;
BUILD_BUG_ON(KASAN_SHADOW_OFFSET !=
- KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT)));
+ KASAN_SHADOW_END - (1UL << (BITS_PER_LONG - KASAN_SHADOW_SCALE_SHIFT)));
for (i = 0; i < PTRS_PER_PTE; ++i)
set_pte(kasan_early_shadow_pte + i,
--
2.40.1