Skip to contentSkip to conversation

riscv: mm: check the SV39 rule

PATCH In review Cheng Chao 2 participants 3 messages Sep 28, 2024 – Oct 7, 2024 RISC-V

lore
Share & tools
lore

Cheng Chao

author
#0 lore raw

PATCH 1/1 riscv: mm: check the SV39 rule

SV39 rule: the address of bits[63..39] should be the same as bit[38], it is easy to violate if configure PAGE_OFFSET too small. for instance, PAGE_OFFSET=0xffffffc0_0000_0000,
the FIXADDR_START will be the 0xfffffffb0_xxxx_xxxx, bit[39] == 0'b1, bit[38] == 0'b0, when access the FIXADDR, which cause the page fault.
It's difficult to debug, check it when compile is necessary.

---
 arch/riscv/mm/init.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
arch/riscv/mm/init.c +7 -1
index bfa2dea95354..a5fb3fc2b2db 100644
@@ -119,7 +119,7 @@ static inline void print_mlt(char *name, unsigned long b, unsigned long t)
 
 static inline void print_ml(char *name, unsigned long b, unsigned long t)
 {
-	unsigned long diff = t - b;
+	unsigned long diff = t - b + 1;
 
 	if (IS_ENABLED(CONFIG_64BIT) && (diff >> LOG2_SZ_1T) >= 10)
 		print_mlt(name, b, t);
@@ -1387,6 +1387,12 @@ static void __init arch_reserve_crashkernel(void)
 
 void __init paging_init(void)
 {
+#ifdef CONFIG_64BIT
+	BUILD_BUG_ON_MSG((VA_BITS == VA_BITS_SV39) &&
+			(((FIXADDR_START & BIT(39)) >> 39)
+			 != ((FIXADDR_START & BIT(38)) >> 38)),
+			"violate SV39 rule: bits[63..39] should be same as bit[38]");
+#endif
 	setup_bootmem();
 	setup_vm_final();
 
Signature · 7 lines
2.26.3


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#1 lore raw

Hi Cheng,

Quoted 3 lines · Cheng Chao

SV39 rule: the address of bits[63..39] should be the same as bit[38], it is easy to violate if configure PAGE_OFFSET too small. for instance, PAGE_OFFSET=0xffffffc0_0000_0000,

Out of curiosity, why do you try to modify the current memory layout?

Quoted 24 lines

the FIXADDR_START will be the 0xfffffffb0_xxxx_xxxx, bit[39] == 0'b1, bit[38] == 0'b0, when access the FIXADDR, which cause the page fault.
It's difficult to debug, check it when compile is necessary.

---
  arch/riscv/mm/init.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
arch/riscv/mm/init.c +1 -1
index bfa2dea95354..a5fb3fc2b2db 100644
@@ -119,7 +119,7 @@ static inline void print_mlt(char *name, unsigned long b, unsigned long t)
 
  static inline void print_ml(char *name, unsigned long b, unsigned long t)
  {
-	unsigned long diff = t - b;
+	unsigned long diff = t - b + 1;
 
  	if (IS_ENABLED(CONFIG_64BIT) && (diff >> LOG2_SZ_1T) >= 10)
  		print_mlt(name, b, t);

arch/riscv/mm/init.c static void __init arch_reserve_crashkernel(void) · line 1391

Quoted 5 lines
arch/riscv/mm/init.c +2 -0
@@ -1387,6 +1387,12 @@ static void __init arch_reserve_crashkernel(void)
 
  void __init paging_init(void)
  {
+#ifdef CONFIG_64BIT
+	BUILD_BUG_ON_MSG((VA_BITS == VA_BITS_SV39) &&

VA_BITS is determined at runtime, either by setting "noXlvl" on the kernel command line or by probing the HW capabilities, so the above can't be determined at build time, or am I missing something?

arch/riscv/mm/init.c static void __init arch_reserve_crashkernel(void) · line 1398

Quoted 7 lines
arch/riscv/mm/init.c +4 -0
+			(((FIXADDR_START & BIT(39)) >> 39)
+			 != ((FIXADDR_START & BIT(38)) >> 38)),
+			"violate SV39 rule: bits[63..39] should be same as bit[38]");
+#endif
  	setup_bootmem();
  	setup_vm_final();
 

Thanks,

Alex

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

author
#2 lore raw

Hi Alexandre,
Thanks for reviewing this patch. I don't reply on time due to the
National Day :)

Quoted 10 lines · Alexandre Ghiti

Hi Cheng,

Quoted 3 lines · Cheng Chao

SV39 rule: the address of bits[63..39] should be the same as bit[38], it is easy to violate if configure PAGE_OFFSET too small. for instance, PAGE_OFFSET=0xffffffc0_0000_0000,

Out of curiosity, why do you try to modify the current memory layout?

It's a long story. I'm working on kernel-5.10, and the default PAGE_OFFSET 0xffffffe0_0000_0000 can only map the 128G physical address,
and we need to map more than 128G in the real world.

After I changed PAGE_OFFSET to a smaller value which can map a more physical address.
the kernel panic(page fault) when earlycon printk, the cause is 0xf, badaddr is 0xffffffbx_xxxx_xxxx.
It took me several days to debug this panic, at the beginning, I considered MMU doesn't work for earlycon, so I traced the pgd/pmd/pte, all are correct before page fault.
Occasionally, I found the SV39 rule: the address of bits[63..39] should be the same as bit[38], the address 0xffffffbx_xxxx_xxxx violates the rule.
The root cause is that the kernel doesn't sense the SV39 rule, so when I adjust the PAGE_OFFSET, the FIXADDR_START will adjust too without any check.
when accessing the [FIXADDR, FIXADDR + PMD_SIZE] , the kernel will have a page fault.

Quoted 18 lines
Quoted 18 lines

the FIXADDR_START will be the 0xfffffffb0_xxxx_xxxx, bit[39] == 0'b1, bit[38] == 0'b0, when access the FIXADDR, which cause the page fault.
It's difficult to debug, check it when compile is necessary.

---
  arch/riscv/mm/init.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
arch/riscv/mm/init.c +0 -0
index bfa2dea95354..a5fb3fc2b2db 100644
@@ -1387,6 +1387,12 @@ static void __init arch_reserve_crashkernel(void)
 
Quoted 10 lines

arch/riscv/mm/init.c static void __init arch_reserve_crashkernel(void) · line 1391

Quoted 4 lines
arch/riscv/mm/init.c +2 -0
  void __init paging_init(void)
  {
+#ifdef CONFIG_64BIT
+     BUILD_BUG_ON_MSG((VA_BITS == VA_BITS_SV39) &&

VA_BITS is determined at runtime, either by setting "noXlvl" on the kernel command line or by probing the HW capabilities, so the above can't be determined at build time, or am I missing something?

you are right,
in case of CONFIG_XIP_KERNEL=y, VA_BITS will be VA_BITS_SV39, this patch works.
other cases, disable_pgtable_l4 will enable SATP_MODE_39, we also check SV39 rule here?
@@ -763,6 +763,11 @@ static void __init disable_pgtable_l4(void)

        pgtable_l4_enabled = false;
        kernel_map.page_offset = PAGE_OFFSET_L3;
        satp_mode = SATP_MODE_39;

+
+ if ((VA_BITS == VA_BITS_SV39) &&
+ (((FIXADDR_START & BIT(39)) >> 39)
+ != ((FIXADDR_START & BIT(38)) >> 38)))
+ panic("violate SV39 rule: bits[63..39] should be same
as bit[38]\n");
}

Quoted 10 lines

arch/riscv/mm/init.c static void __init arch_reserve_crashkernel(void) · line 1398

Quoted 8 lines
arch/riscv/mm/init.c +4 -0
+                     (((FIXADDR_START & BIT(39)) >> 39)
+                      != ((FIXADDR_START & BIT(38)) >> 38)),
+                     "violate SV39 rule: bits[63..39] should be same as bit[38]");
+#endif
      setup_bootmem();
      setup_vm_final();
 
Quoted 4 lines

Thanks,

Alex

Thanks,
Cheng Chao

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv