Skip to contentSkip to conversation

riscv: mm: Fixup no5lvl failure when vaddr is invalid

PATCH Superseded [email protected] 3 participants 3 messages Jan 25 – May 12 RISC-V

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 riscv: mm: Fixup no5lvl failure when vaddr is invalid

From: "Guo Ren (Alibaba DAMO Academy)" <[email protected]>

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 <[email protected]>
Cc Björn Töpel <[email protected]>

---
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/[email protected]/

---
 arch/riscv/mm/init.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
arch/riscv/mm/init.c +25 -0
index addb8a9305be..bfea9f73e703 100644
@@ -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);
Signature · 1 line
2.40.1
Tested-by on v2 1/1
#1 lore raw
Quoted 79 lines

From: "Guo Ren (Alibaba DAMO Academy)" <[email protected]>

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 <[email protected]>
Cc Björn Töpel <[email protected]>

---
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/[email protected]/
---
arch/riscv/mm/init.c | 25 +++++++++++++++++++++++++

1 file changed, 25 insertions(+)
arch/riscv/mm/init.c +21 -0
index addb8a9305be..bfea9f73e703 100644
@@ -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:

arch/riscv/mm/init.c +3 -0
+	if (!is_vaddr_valid(set_satp_mode_pmd))
+		goto out;
+

create_pgd_mapping(early_pg_dir,
set_satp_mode_pmd,
pgtable_l5_enabled ?

arch/riscv/mm/init.c +1 -1
@@ -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);
--
Quoted 1 line

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.

Thanks,
Fangyu

Tested-by on v2 1/1
#2 lore raw
In reply to [email protected]
Quoted 69 lines · On Sun, 25 Jan 2026 00:52:12 -0500, [email protected] 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 <[email protected]>
Cc Björn Töpel <[email protected]>

---
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/[email protected]/

---
 arch/riscv/mm/init.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
arch/riscv/mm/init.c +24 -0
index addb8a9305be..bfea9f73e703 100644
@@ -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 ?

arch/riscv/mm/init.c static __init void set_satp_mode(uintptr_t dtb_pa) · line 945

Quoted 7 lines
arch/riscv/mm/init.c +1 -0
@@ -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);

Thanks,
Pei