[PATCH] LoongArch: Add DMW2 shadow mapping in KASAN

Kanglong Wang posted 1 patch 2 weeks, 4 days ago
arch/loongarch/include/asm/kasan.h | 11 ++++++++++-
arch/loongarch/mm/kasan_init.c     |  5 +++++
2 files changed, 15 insertions(+), 1 deletion(-)
[PATCH] LoongArch: Add DMW2 shadow mapping in KASAN
Posted by Kanglong Wang 2 weeks, 4 days ago
Currently,the kernel couldn't boot when both CONFIG_ARCH_IOREMAP,
CONFIG_ARCH_WRITECOMBINE and KASAN are enabled. Because the DMW2 is used
by kernel now which is configured as 0xa000000000000000, the KASAN has
no segment mapping. This patch fix this issue.

Solution: Add the relevant definitions for DMW2 in KASAN.

Fixes: 8e02c3b782ec ("LoongArch: Add writecombine support for DMW-based ioremap()")
Signed-off-by: Kanglong Wang <wangkanglong@loongson.cn>
---
 arch/loongarch/include/asm/kasan.h | 11 ++++++++++-
 arch/loongarch/mm/kasan_init.c     |  5 +++++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/arch/loongarch/include/asm/kasan.h b/arch/loongarch/include/asm/kasan.h
index c6bce5fbff57..2636a1160bae 100644
--- a/arch/loongarch/include/asm/kasan.h
+++ b/arch/loongarch/include/asm/kasan.h
@@ -25,6 +25,7 @@
 /* 64-bit segment value. */
 #define XKPRANGE_UC_SEG		(0x8000)
 #define XKPRANGE_CC_SEG		(0x9000)
+#define XKPRANGE_WC_SEG		(0xa000)
 #define XKVRANGE_VC_SEG		(0xffff)
 
 /* Cached */
@@ -41,10 +42,17 @@
 #define XKPRANGE_UC_SHADOW_SIZE		(XKPRANGE_UC_SIZE >> KASAN_SHADOW_SCALE_SHIFT)
 #define XKPRANGE_UC_SHADOW_END		(XKPRANGE_UC_KASAN_OFFSET + XKPRANGE_UC_SHADOW_SIZE)
 
+/* Weak-ordered UnCached */
+#define XKPRANGE_WC_START		WRITECOMBINE_BASE
+#define XKPRANGE_WC_SIZE		XRANGE_SIZE
+#define XKPRANGE_WC_KASAN_OFFSET	XKPRANGE_UC_SHADOW_END
+#define XKPRANGE_WC_SHADOW_SIZE		(XKPRANGE_WC_SIZE >> KASAN_SHADOW_SCALE_SHIFT)
+#define XKPRANGE_WC_SHADOW_END		(XKPRANGE_WC_KASAN_OFFSET + XKPRANGE_WC_SHADOW_SIZE)
+
 /* VMALLOC (Cached or UnCached)  */
 #define XKVRANGE_VC_START		MODULES_VADDR
 #define XKVRANGE_VC_SIZE		round_up(KFENCE_AREA_END - MODULES_VADDR + 1, PGDIR_SIZE)
-#define XKVRANGE_VC_KASAN_OFFSET	XKPRANGE_UC_SHADOW_END
+#define XKVRANGE_VC_KASAN_OFFSET	XKPRANGE_WC_SHADOW_END
 #define XKVRANGE_VC_SHADOW_SIZE		(XKVRANGE_VC_SIZE >> KASAN_SHADOW_SCALE_SHIFT)
 #define XKVRANGE_VC_SHADOW_END		(XKVRANGE_VC_KASAN_OFFSET + XKVRANGE_VC_SHADOW_SIZE)
 
@@ -55,6 +63,7 @@
 
 #define XKPRANGE_CC_SHADOW_OFFSET	(KASAN_SHADOW_START + XKPRANGE_CC_KASAN_OFFSET)
 #define XKPRANGE_UC_SHADOW_OFFSET	(KASAN_SHADOW_START + XKPRANGE_UC_KASAN_OFFSET)
+#define XKPRANGE_WC_SHADOW_OFFSET	(KASAN_SHADOW_START + XKPRANGE_WC_KASAN_OFFSET)
 #define XKVRANGE_VC_SHADOW_OFFSET	(KASAN_SHADOW_START + XKVRANGE_VC_KASAN_OFFSET)
 
 extern bool kasan_early_stage;
diff --git a/arch/loongarch/mm/kasan_init.c b/arch/loongarch/mm/kasan_init.c
index 427d6b1aec09..0babfab73448 100644
--- a/arch/loongarch/mm/kasan_init.c
+++ b/arch/loongarch/mm/kasan_init.c
@@ -55,6 +55,9 @@ void *kasan_mem_to_shadow(const void *addr)
 		case XKPRANGE_UC_SEG:
 			offset = XKPRANGE_UC_SHADOW_OFFSET;
 			break;
+		case XKPRANGE_WC_SEG:
+			offset = XKPRANGE_WC_SHADOW_OFFSET;
+			break;
 		case XKVRANGE_VC_SEG:
 			offset = XKVRANGE_VC_SHADOW_OFFSET;
 			break;
@@ -79,6 +82,8 @@ const void *kasan_shadow_to_mem(const void *shadow_addr)
 
 	if (addr >= XKVRANGE_VC_SHADOW_OFFSET)
 		return (void *)(((addr - XKVRANGE_VC_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT) + XKVRANGE_VC_START);
+	else if (addr >= XKPRANGE_WC_SHADOW_OFFSET)
+		return (void *)(((addr - XKPRANGE_WC_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT) + XKPRANGE_WC_START);
 	else if (addr >= XKPRANGE_UC_SHADOW_OFFSET)
 		return (void *)(((addr - XKPRANGE_UC_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT) + XKPRANGE_UC_START);
 	else if (addr >= XKPRANGE_CC_SHADOW_OFFSET)
-- 
2.20.1
Re: [PATCH] LoongArch: Add DMW2 shadow mapping in KASAN
Posted by Huacai Chen 2 weeks, 2 days ago
Queued, thanks.

Huacai

On Tue, Nov 5, 2024 at 8:30 PM Kanglong Wang <wangkanglong@loongson.cn> wrote:
>
> Currently,the kernel couldn't boot when both CONFIG_ARCH_IOREMAP,
> CONFIG_ARCH_WRITECOMBINE and KASAN are enabled. Because the DMW2 is used
> by kernel now which is configured as 0xa000000000000000, the KASAN has
> no segment mapping. This patch fix this issue.
>
> Solution: Add the relevant definitions for DMW2 in KASAN.
>
> Fixes: 8e02c3b782ec ("LoongArch: Add writecombine support for DMW-based ioremap()")
> Signed-off-by: Kanglong Wang <wangkanglong@loongson.cn>
> ---
>  arch/loongarch/include/asm/kasan.h | 11 ++++++++++-
>  arch/loongarch/mm/kasan_init.c     |  5 +++++
>  2 files changed, 15 insertions(+), 1 deletion(-)
>
> diff --git a/arch/loongarch/include/asm/kasan.h b/arch/loongarch/include/asm/kasan.h
> index c6bce5fbff57..2636a1160bae 100644
> --- a/arch/loongarch/include/asm/kasan.h
> +++ b/arch/loongarch/include/asm/kasan.h
> @@ -25,6 +25,7 @@
>  /* 64-bit segment value. */
>  #define XKPRANGE_UC_SEG                (0x8000)
>  #define XKPRANGE_CC_SEG                (0x9000)
> +#define XKPRANGE_WC_SEG                (0xa000)
>  #define XKVRANGE_VC_SEG                (0xffff)
>
>  /* Cached */
> @@ -41,10 +42,17 @@
>  #define XKPRANGE_UC_SHADOW_SIZE                (XKPRANGE_UC_SIZE >> KASAN_SHADOW_SCALE_SHIFT)
>  #define XKPRANGE_UC_SHADOW_END         (XKPRANGE_UC_KASAN_OFFSET + XKPRANGE_UC_SHADOW_SIZE)
>
> +/* Weak-ordered UnCached */
> +#define XKPRANGE_WC_START              WRITECOMBINE_BASE
> +#define XKPRANGE_WC_SIZE               XRANGE_SIZE
> +#define XKPRANGE_WC_KASAN_OFFSET       XKPRANGE_UC_SHADOW_END
> +#define XKPRANGE_WC_SHADOW_SIZE                (XKPRANGE_WC_SIZE >> KASAN_SHADOW_SCALE_SHIFT)
> +#define XKPRANGE_WC_SHADOW_END         (XKPRANGE_WC_KASAN_OFFSET + XKPRANGE_WC_SHADOW_SIZE)
> +
>  /* VMALLOC (Cached or UnCached)  */
>  #define XKVRANGE_VC_START              MODULES_VADDR
>  #define XKVRANGE_VC_SIZE               round_up(KFENCE_AREA_END - MODULES_VADDR + 1, PGDIR_SIZE)
> -#define XKVRANGE_VC_KASAN_OFFSET       XKPRANGE_UC_SHADOW_END
> +#define XKVRANGE_VC_KASAN_OFFSET       XKPRANGE_WC_SHADOW_END
>  #define XKVRANGE_VC_SHADOW_SIZE                (XKVRANGE_VC_SIZE >> KASAN_SHADOW_SCALE_SHIFT)
>  #define XKVRANGE_VC_SHADOW_END         (XKVRANGE_VC_KASAN_OFFSET + XKVRANGE_VC_SHADOW_SIZE)
>
> @@ -55,6 +63,7 @@
>
>  #define XKPRANGE_CC_SHADOW_OFFSET      (KASAN_SHADOW_START + XKPRANGE_CC_KASAN_OFFSET)
>  #define XKPRANGE_UC_SHADOW_OFFSET      (KASAN_SHADOW_START + XKPRANGE_UC_KASAN_OFFSET)
> +#define XKPRANGE_WC_SHADOW_OFFSET      (KASAN_SHADOW_START + XKPRANGE_WC_KASAN_OFFSET)
>  #define XKVRANGE_VC_SHADOW_OFFSET      (KASAN_SHADOW_START + XKVRANGE_VC_KASAN_OFFSET)
>
>  extern bool kasan_early_stage;
> diff --git a/arch/loongarch/mm/kasan_init.c b/arch/loongarch/mm/kasan_init.c
> index 427d6b1aec09..0babfab73448 100644
> --- a/arch/loongarch/mm/kasan_init.c
> +++ b/arch/loongarch/mm/kasan_init.c
> @@ -55,6 +55,9 @@ void *kasan_mem_to_shadow(const void *addr)
>                 case XKPRANGE_UC_SEG:
>                         offset = XKPRANGE_UC_SHADOW_OFFSET;
>                         break;
> +               case XKPRANGE_WC_SEG:
> +                       offset = XKPRANGE_WC_SHADOW_OFFSET;
> +                       break;
>                 case XKVRANGE_VC_SEG:
>                         offset = XKVRANGE_VC_SHADOW_OFFSET;
>                         break;
> @@ -79,6 +82,8 @@ const void *kasan_shadow_to_mem(const void *shadow_addr)
>
>         if (addr >= XKVRANGE_VC_SHADOW_OFFSET)
>                 return (void *)(((addr - XKVRANGE_VC_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT) + XKVRANGE_VC_START);
> +       else if (addr >= XKPRANGE_WC_SHADOW_OFFSET)
> +               return (void *)(((addr - XKPRANGE_WC_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT) + XKPRANGE_WC_START);
>         else if (addr >= XKPRANGE_UC_SHADOW_OFFSET)
>                 return (void *)(((addr - XKPRANGE_UC_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT) + XKPRANGE_UC_START);
>         else if (addr >= XKPRANGE_CC_SHADOW_OFFSET)
> --
> 2.20.1
>