Linux-RISC-V Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH V2] riscv: mm: Fixup no5lvl failure when vaddr is invalid
@ 2026-01-25  5:52 guoren
  2026-01-28 13:45 ` fangyu.yu
  2026-05-12  1:15 ` Chen Pei
  0 siblings, 2 replies; 3+ messages in thread
From: guoren @ 2026-01-25  5:52 UTC (permalink / raw)
  To: paul.walmsley, palmer, guoren, alex, bjorn, alexghiti
  Cc: linux-riscv, linux-kernel

From: "Guo Ren (Alibaba DAMO Academy)" <guoren@kernel.org>

Unlike no4lvl, no5lvl still continues detect satp, which
requires va=pa mapping. When pa=0x800000000000, no5lvl
would fail in Sv48 mode due to an illegal VA value of
0x800000000000.

So, prevent detecting the satp flow for no5lvl, when
vaddr is invalid. Add the is_vaddr_valid() function for
checking.

Fixes: 26e7aacb83df ("riscv: Allow to downgrade paging mode from the command line")
Cc: Alexandre Ghiti <alexghiti@rivosinc.com>
Cc: Björn Töpel <bjorn@rivosinc.com>
Signed-off-by: Guo Ren (Alibaba DAMO Academy) <guoren@kernel.org>
---
Changelog:

v2:
 - Use is_vaddr_valid() instead of simple return.
 - Don't change the original no5lvl code logic.

v1:
https://lore.kernel.org/linux-riscv/20260118145441.291302-1-guoren@kernel.org/
---
 arch/riscv/mm/init.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
index addb8a9305be..bfea9f73e703 100644
--- a/arch/riscv/mm/init.c
+++ b/arch/riscv/mm/init.c
@@ -852,6 +852,27 @@ static void __init set_mmap_rnd_bits_max(void)
 	mmap_rnd_bits_max = MMAP_VA_BITS - PAGE_SHIFT - 3;
 }
 
+static bool __init is_vaddr_valid(unsigned long va)
+{
+	unsigned long up = 0;
+
+	switch (satp_mode) {
+	case SATP_MODE_39:
+		up = 1UL << 38;
+		break;
+	case SATP_MODE_48:
+		up = 1UL << 47;
+		break;
+	case SATP_MODE_57:
+		up = 1UL << 56;
+		break;
+	default:
+		return false;
+	}
+
+	return (va < up) || (va >= (ULONG_MAX - up + 1));
+}
+
 /*
  * There is a simple way to determine if 4-level is supported by the
  * underlying hardware: establish 1:1 mapping in 4-level page table mode
@@ -893,6 +914,9 @@ static __init void set_satp_mode(uintptr_t dtb_pa)
 			   set_satp_mode_pmd + PMD_SIZE,
 			   PMD_SIZE, PAGE_KERNEL_EXEC);
 retry:
+	if (!is_vaddr_valid(set_satp_mode_pmd))
+		goto out;
+
 	create_pgd_mapping(early_pg_dir,
 			   set_satp_mode_pmd,
 			   pgtable_l5_enabled ?
@@ -915,6 +939,7 @@ static __init void set_satp_mode(uintptr_t dtb_pa)
 		disable_pgtable_l4();
 	}
 
+out:
 	memset(early_pg_dir, 0, PAGE_SIZE);
 	memset(early_p4d, 0, PAGE_SIZE);
 	memset(early_pud, 0, PAGE_SIZE);
-- 
2.40.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH V2] riscv: mm: Fixup no5lvl failure when vaddr is invalid
  2026-01-25  5:52 [PATCH V2] riscv: mm: Fixup no5lvl failure when vaddr is invalid guoren
@ 2026-01-28 13:45 ` fangyu.yu
  2026-05-12  1:15 ` Chen Pei
  1 sibling, 0 replies; 3+ messages in thread
From: fangyu.yu @ 2026-01-28 13:45 UTC (permalink / raw)
  To: guoren
  Cc: alex, alexghiti, bjorn, linux-kernel, linux-riscv, palmer,
	paul.walmsley, Fangyu Yu

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset=y, Size: 2486 bytes --]

>From: "Guo Ren (Alibaba DAMO Academy)" <guoren@kernel.org>
>
>Unlike no4lvl, no5lvl still continues detect satp, which
>requires va=pa mapping. When pa=0x800000000000, no5lvl
>would fail in Sv48 mode due to an illegal VA value of
>0x800000000000.
>
>So, prevent detecting the satp flow for no5lvl, when
>vaddr is invalid. Add the is_vaddr_valid() function for
>checking.
>
>Fixes: 26e7aacb83df ("riscv: Allow to downgrade paging mode from the command line")
>Cc: Alexandre Ghiti <alexghiti@rivosinc.com>
>Cc: Björn Töpel <bjorn@rivosinc.com>
>Signed-off-by: Guo Ren (Alibaba DAMO Academy) <guoren@kernel.org>
>---
>Changelog:
>
>v2:
> - Use is_vaddr_valid() instead of simple return.
> - Don't change the original no5lvl code logic.
>
>v1:
>https://lore.kernel.org/linux-riscv/20260118145441.291302-1-guoren@kernel.org/
>---
> arch/riscv/mm/init.c | 25 +++++++++++++++++++++++++
> 1 file changed, 25 insertions(+)
>
>diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
>index addb8a9305be..bfea9f73e703 100644
>--- a/arch/riscv/mm/init.c
>+++ b/arch/riscv/mm/init.c
>@@ -852,6 +852,27 @@ static void __init set_mmap_rnd_bits_max(void)
> 	mmap_rnd_bits_max = MMAP_VA_BITS - PAGE_SHIFT - 3;
> }
> 
>+static bool __init is_vaddr_valid(unsigned long va)
>+{
>+	unsigned long up = 0;
>+
>+	switch (satp_mode) {
>+	case SATP_MODE_39:
>+		up = 1UL << 38;
>+		break;
>+	case SATP_MODE_48:
>+		up = 1UL << 47;
>+		break;
>+	case SATP_MODE_57:
>+		up = 1UL << 56;
>+		break;
>+	default:
>+		return false;
>+	}
>+
>+	return (va < up) || (va >= (ULONG_MAX - up + 1));
>+}
>+
> /*
>  * There is a simple way to determine if 4-level is supported by the
>  * underlying hardware: establish 1:1 mapping in 4-level page table mode
>@@ -893,6 +914,9 @@ static __init void set_satp_mode(uintptr_t dtb_pa)
> 			   set_satp_mode_pmd + PMD_SIZE,
> 			   PMD_SIZE, PAGE_KERNEL_EXEC);
> retry:
>+	if (!is_vaddr_valid(set_satp_mode_pmd))
>+		goto out;
>+
> 	create_pgd_mapping(early_pg_dir,
> 			   set_satp_mode_pmd,
> 			   pgtable_l5_enabled ?
>@@ -915,6 +939,7 @@ static __init void set_satp_mode(uintptr_t dtb_pa)
> 		disable_pgtable_l4();
> 	}
> 
>+out:
> 	memset(early_pg_dir, 0, PAGE_SIZE);
> 	memset(early_p4d, 0, PAGE_SIZE);
> 	memset(early_pud, 0, PAGE_SIZE);
>--
>2.40.1

Tested on hardware where DRAM/PA starts at 0x800000000000.  With "no5lvl"
on the kernel command line, the kernel boots successfully after this patch.

Tested-by: Fangyu Yu <fangyu.yu@linux.alibaba.com>

Thanks,
Fangyu


[-- Attachment #2: Type: text/plain, Size: 161 bytes --]

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH V2] riscv: mm: Fixup no5lvl failure when vaddr is invalid
  2026-01-25  5:52 [PATCH V2] riscv: mm: Fixup no5lvl failure when vaddr is invalid guoren
  2026-01-28 13:45 ` fangyu.yu
@ 2026-05-12  1:15 ` Chen Pei
  1 sibling, 0 replies; 3+ messages in thread
From: Chen Pei @ 2026-05-12  1:15 UTC (permalink / raw)
  To: guoren
  Cc: alex, alexghiti, bjorn, linux-kernel, linux-riscv, palmer,
	paul.walmsley, Chen Pei

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset=y, Size: 2392 bytes --]

On Sun, 25 Jan 2026 00:52:12 -0500, guoren@kernel.org wrote:

> Unlike no4lvl, no5lvl still continues detect satp, which
> requires va=pa mapping. When pa=0x800000000000, no5lvl
> would fail in Sv48 mode due to an illegal VA value of
> 0x800000000000.
> 
> So, prevent detecting the satp flow for no5lvl, when
> vaddr is invalid. Add the is_vaddr_valid() function for
> checking.
> 
> Fixes: 26e7aacb83df ("riscv: Allow to downgrade paging mode from the command line")
> Cc: Alexandre Ghiti <alexghiti@rivosinc.com>
> Cc: Björn Töpel <bjorn@rivosinc.com>
> Signed-off-by: Guo Ren (Alibaba DAMO Academy) <guoren@kernel.org>
> ---
> Changelog:
> 
> v2:
>  - Use is_vaddr_valid() instead of simple return.
>  - Don't change the original no5lvl code logic.
> 
> v1:
> https://lore.kernel.org/linux-riscv/20260118145441.291302-1-guoren@kernel.org/
> ---
>  arch/riscv/mm/init.c | 25 +++++++++++++++++++++++++
>  1 file changed, 25 insertions(+)
> 
> diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
> index addb8a9305be..bfea9f73e703 100644
> --- a/arch/riscv/mm/init.c
> +++ b/arch/riscv/mm/init.c
> @@ -852,6 +852,27 @@ static void __init set_mmap_rnd_bits_max(void)
>  	mmap_rnd_bits_max = MMAP_VA_BITS - PAGE_SHIFT - 3;
>  }
>  
> +static bool __init is_vaddr_valid(unsigned long va)
> +{
> +	unsigned long up = 0;
> +
> +	switch (satp_mode) {
> +	case SATP_MODE_39:
> +		up = 1UL << 38;
> +		break;
> +	case SATP_MODE_48:
> +		up = 1UL << 47;
> +		break;
> +	case SATP_MODE_57:
> +		up = 1UL << 56;
> +		break;
> +	default:
> +		return false;
> +	}
> +
> +	return (va < up) || (va >= (ULONG_MAX - up + 1));
> +}
> +
>  /*
>   * There is a simple way to determine if 4-level is supported by the
>   * underlying hardware: establish 1:1 mapping in 4-level page table mode
> @@ -893,6 +914,9 @@ static __init void set_satp_mode(uintptr_t dtb_pa)
>  			   set_satp_mode_pmd + PMD_SIZE,
>  			   PMD_SIZE, PAGE_KERNEL_EXEC);
>  retry:
> +	if (!is_vaddr_valid(set_satp_mode_pmd))
> +		goto out;
> +
>  	create_pgd_mapping(early_pg_dir,
>  			   set_satp_mode_pmd,
>  			   pgtable_l5_enabled ?
> @@ -915,6 +939,7 @@ static __init void set_satp_mode(uintptr_t dtb_pa)
>  		disable_pgtable_l4();
>  	}
>  
> +out:
>  	memset(early_pg_dir, 0, PAGE_SIZE);
>  	memset(early_p4d, 0, PAGE_SIZE);
>  	memset(early_pud, 0, PAGE_SIZE);

Tested-by: Chen Pei <cp0613@linux.alibaba.com>

Thanks,
Pei


[-- Attachment #2: Type: text/plain, Size: 161 bytes --]

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-05-12  1:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-25  5:52 [PATCH V2] riscv: mm: Fixup no5lvl failure when vaddr is invalid guoren
2026-01-28 13:45 ` fangyu.yu
2026-05-12  1:15 ` Chen Pei

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox