[PATCH] LoongArch: Make KASAN usable for variable cpu_vabits

Huacai Chen posted 1 patch 1 month, 1 week ago
arch/loongarch/include/asm/kasan.h | 2 +-
arch/loongarch/mm/kasan_init.c     | 4 ++++
2 files changed, 5 insertions(+), 1 deletion(-)
[PATCH] LoongArch: Make KASAN usable for variable cpu_vabits
Posted by Huacai Chen 1 month, 1 week ago
Currently, KASAN on LoongArch assume the CPU VA bits is 48, which is
true for Loongson-3 series, but not for Loongson-2 series (only 40 or
lower), this patch fix that issue and make KASAN usable for variable
cpu_vabits.

1. Define XRANGE_SHADOW_SHIFT which means valid address length from
   VA_BITS to min(cpu_vabits, VA_BITS).
2. In kasan_mem_to_shadow() let DMW addresses which exceed XRANGE_SIZE
   to return kasan_early_shadow_page.

Cc: stable@vger.kernel.org
Signed-off-by: Kanglong Wang <wangkanglong@loongson.cn>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
---
 arch/loongarch/include/asm/kasan.h | 2 +-
 arch/loongarch/mm/kasan_init.c     | 4 ++++
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/arch/loongarch/include/asm/kasan.h b/arch/loongarch/include/asm/kasan.h
index cd6084f4e153..c6bce5fbff57 100644
--- a/arch/loongarch/include/asm/kasan.h
+++ b/arch/loongarch/include/asm/kasan.h
@@ -16,7 +16,7 @@
 #define XRANGE_SHIFT (48)
 
 /* Valid address length */
-#define XRANGE_SHADOW_SHIFT	(PGDIR_SHIFT + PAGE_SHIFT - 3)
+#define XRANGE_SHADOW_SHIFT	min(cpu_vabits, VA_BITS)
 /* Used for taking out the valid address */
 #define XRANGE_SHADOW_MASK	GENMASK_ULL(XRANGE_SHADOW_SHIFT - 1, 0)
 /* One segment whole address space size */
diff --git a/arch/loongarch/mm/kasan_init.c b/arch/loongarch/mm/kasan_init.c
index 427d6b1aec09..e5ecc8c12034 100644
--- a/arch/loongarch/mm/kasan_init.c
+++ b/arch/loongarch/mm/kasan_init.c
@@ -48,6 +48,10 @@ void *kasan_mem_to_shadow(const void *addr)
 			return (void *)(kasan_early_shadow_page);
 
 		maddr &= XRANGE_SHADOW_MASK;
+
+		if (maddr >= XRANGE_SIZE)
+			return (void *)(kasan_early_shadow_page);
+
 		switch (xrange) {
 		case XKPRANGE_CC_SEG:
 			offset = XKPRANGE_CC_SHADOW_OFFSET;
-- 
2.43.5