Skip to content

Commit db909bd

Browse files
guoren83Paul Walmsley
authored andcommitted
riscv: mm: Fixup no5lvl failure when vaddr is invalid
Unlike no4lvl, no5lvl still continues to 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: 26e7aac ("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> Tested-by: Fangyu Yu <fangyu.yu@linux.alibaba.com> Link: https://patch.msgid.link/20260125055212.433163-1-guoren@kernel.org [pjw@kernel.org: cleaned up commit message] Signed-off-by: Paul Walmsley <pjw@kernel.org>
1 parent 6ebcbb5 commit db909bd

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

arch/riscv/mm/init.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,27 @@ static void __init set_mmap_rnd_bits_max(void)
792792
mmap_rnd_bits_max = MMAP_VA_BITS - PAGE_SHIFT - 3;
793793
}
794794

795+
static bool __init is_vaddr_valid(unsigned long va)
796+
{
797+
unsigned long up = 0;
798+
799+
switch (satp_mode) {
800+
case SATP_MODE_39:
801+
up = 1UL << 38;
802+
break;
803+
case SATP_MODE_48:
804+
up = 1UL << 47;
805+
break;
806+
case SATP_MODE_57:
807+
up = 1UL << 56;
808+
break;
809+
default:
810+
return false;
811+
}
812+
813+
return (va < up) || (va >= (ULONG_MAX - up + 1));
814+
}
815+
795816
/*
796817
* There is a simple way to determine if 4-level is supported by the
797818
* underlying hardware: establish 1:1 mapping in 4-level page table mode
@@ -833,6 +854,9 @@ static __init void set_satp_mode(uintptr_t dtb_pa)
833854
set_satp_mode_pmd + PMD_SIZE,
834855
PMD_SIZE, PAGE_KERNEL_EXEC);
835856
retry:
857+
if (!is_vaddr_valid(set_satp_mode_pmd))
858+
goto out;
859+
836860
create_pgd_mapping(early_pg_dir,
837861
set_satp_mode_pmd,
838862
pgtable_l5_enabled ?
@@ -855,6 +879,7 @@ static __init void set_satp_mode(uintptr_t dtb_pa)
855879
disable_pgtable_l4();
856880
}
857881

882+
out:
858883
memset(early_pg_dir, 0, PAGE_SIZE);
859884
memset(early_p4d, 0, PAGE_SIZE);
860885
memset(early_pud, 0, PAGE_SIZE);

0 commit comments

Comments
 (0)