riscv: toggle mmu_enabled flag in a precise manner
PATCH 1/1 riscv: toggle mmu_enabled flag in a precise manner
From: Vitaly Wool <[email protected]>
Cuurently mmu_enabled flag is set to true way later than the actual MMU enablement takes place. This leads to hard-to-track races in e. g. SBI earlycon initialization taking wrong path configuring fixmap.
To fix that, move mmu_enabled toggling to head.S and rename it to _mmu_enabled to avoid possible name clashes since it's not a static variable any more.
--- arch/riscv/kernel/head.S | 9 +++++++++ arch/riscv/mm/init.c | 13 +++++-------- 2 files changed, 14 insertions(+), 8 deletions(-)
arch/riscv/kernel/head.S +9 -0
@@ -248,6 +248,10 @@ clear_bss_done: call relocate #endif /* CONFIG_MMU */ + la a0, _mmu_enabled + li a1, 1 + sw a1, (a0) + call setup_trap_vector /* Restore C environment */ la tp, init_task
@@ -370,6 +374,11 @@ ENTRY(reset_regs) END(reset_regs) #endif /* CONFIG_RISCV_M_MODE */ +.section ".data" + .global _mmu_enabled +_mmu_enabled: + .word 0 + __PAGE_ALIGNED_BSS /* Empty zero page */ .balign PAGE_SIZE
arch/riscv/mm/init.c +5 -8
@@ -211,7 +211,7 @@ EXPORT_SYMBOL(pfn_base); pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss; pgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss; pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss; -static bool mmu_enabled; +extern bool _mmu_enabled;#define MAX_EARLY_MAPPING_SIZE SZ_128M
@@ -236,7 +236,7 @@ void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot)static pte_t *__init get_pte_virt (phys_addr_t pa) { -if (mmu_enabled) { +if (_mmu_enabled) {clear_fixmap (FIX_PTE );return (pte_t *)set_fixmap_offset (FIX_PTE , pa); }else {
@@ -250,7 +250,7 @@ static phys_addr_t __init alloc_pte(uintptr_t va)* We only create PMD or PGD early mappings so we * should never reach here with MMU disabled. */ -BUG_ON (!mmu_enabled); +BUG_ON (!_mmu_enabled);return memblock_phys_alloc (PAGE_SIZE ,PAGE_SIZE ); }
@@ -281,7 +281,7 @@ pmd_t early_pmd[PTRS_PER_PMD * NUM_EARLY_PMDS] __initdata __aligned(PAGE_SIZE);static pmd_t *__init get_pmd_virt (phys_addr_t pa) { -if (mmu_enabled) { +if (_mmu_enabled) {clear_fixmap (FIX_PMD );return (pmd_t *)set_fixmap_offset (FIX_PMD , pa); }else {
@@ -293,7 +293,7 @@ static phys_addr_t __init alloc_pmd(uintptr_t va) {uintptr_t pmd_num; -if (mmu_enabled) +if (_mmu_enabled)return memblock_phys_alloc (PAGE_SIZE ,PAGE_SIZE ); pmd_num = (va -PAGE_OFFSET ) >>PGDIR_SHIFT ;
@@ -467,9 +467,6 @@ static void __init setup_vm_final(void)phys_addr_t pa, start, end;struct memblock_region *reg; -/* Set mmu_enabled flag */ - mmu_enabled = true; -/* Setup swapper PGD for fixmap */ create_pgd_mapping(swapper_pg_dir, FIXADDR_START, __pa_symbol(fixmap_pgd_next),
Signature · 7 lines
2.20.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Quoted 8 lines · On Tue, Dec 1, 2020 at 1:01 AM <[email protected]> wrote:
From: Vitaly Wool <[email protected]>
Cuurently
mmu_enabledflag is set to true way later than the actual MMU enablement takes place. This leads to hard-to-track races in e. g. SBI earlycon initialization taking wrong path configuring fixmap.
This code path is significantly changed in 5.10-rcX with UEFI series.
https://patchwork.kernel.org/project/linux-riscv/patch/[email protected]/
Can you check if you can still reproduce the issue you were seeing with the latest upstream kernel ?
If yes, please share the steps to reproduce the issue.
Quoted 104 lines
To fix that, move
mmu_enabledtoggling tohead.Sand rename it to_mmu_enabledto avoid possible name clashes since it's not a static variable any more.Signed-off-by Vitaly Wool <[email protected]>--- arch/riscv/kernel/head.S | 9 +++++++++ arch/riscv/mm/init.c | 13 +++++-------- 2 files changed, 14 insertions(+), 8 deletions(-)arch/riscv/kernel/head.S +9
-0@@ -248,6 +248,10 @@ clear_bss_done: call relocate #endif /* CONFIG_MMU */ + la a0, _mmu_enabled + li a1, 1 + sw a1, (a0) + call setup_trap_vector /* Restore C environment */ la tp, init_task@@ -370,6 +374,11 @@ ENTRY(reset_regs) END(reset_regs) #endif /* CONFIG_RISCV_M_MODE */ +.section ".data" + .global _mmu_enabled +_mmu_enabled: + .word 0 + __PAGE_ALIGNED_BSS /* Empty zero page */ .balign PAGE_SIZEarch/riscv/mm/init.c +5
-9@@ -211,7 +211,7 @@ EXPORT_SYMBOL(pfn_base); pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss; pgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss; pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss; -static bool mmu_enabled; +extern bool _mmu_enabled;#define MAX_EARLY_MAPPING_SIZE SZ_128M@@ -236,7 +236,7 @@ void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot)static pte_t *__init get_pte_virt (phys_addr_t pa) { -if (mmu_enabled) { +if (_mmu_enabled) {clear_fixmap (FIX_PTE );return (pte_t *)set_fixmap_offset (FIX_PTE , pa); }else {@@ -250,7 +250,7 @@ static phys_addr_t __init alloc_pte(uintptr_t va)* We only create PMD or PGD early mappings so we * should never reach here with MMU disabled. */ -BUG_ON (!mmu_enabled); +BUG_ON (!_mmu_enabled);return memblock_phys_alloc (PAGE_SIZE ,PAGE_SIZE ); }@@ -281,7 +281,7 @@ pmd_t early_pmd[PTRS_PER_PMD * NUM_EARLY_PMDS] __initdata __aligned(PAGE_SIZE);static pmd_t *__init get_pmd_virt (phys_addr_t pa) { -if (mmu_enabled) { +if (_mmu_enabled) {clear_fixmap (FIX_PMD );return (pmd_t *)set_fixmap_offset (FIX_PMD , pa); }else {@@ -293,7 +293,7 @@ static phys_addr_t __init alloc_pmd(uintptr_t va) {uintptr_t pmd_num; -if (mmu_enabled) +if (_mmu_enabled)return memblock_phys_alloc (PAGE_SIZE ,PAGE_SIZE ); pmd_num = (va -PAGE_OFFSET ) >>PGDIR_SHIFT ;@@ -467,9 +467,6 @@ static void __init setup_vm_final(void)phys_addr_t pa, start, end;struct memblock_region *reg; -/* Set mmu_enabled flag */ - mmu_enabled = true; -/* Setup swapper PGD for fixmap */ create_pgd_mapping(swapper_pg_dir, FIXADDR_START, __pa_symbol(fixmap_pgd_next), --2.20.1
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Signature · 7 lines
Regards, Atish _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Quoted 11 lines · Atish Patra
Quoted 8 lines · On Tue, Dec 1, 2020 at 1:01 AM <[email protected]> wrote:
From: Vitaly Wool <[email protected]>
Cuurently
mmu_enabledflag is set to true way later than the actual MMU enablement takes place. This leads to hard-to-track races in e. g. SBI earlycon initialization taking wrong path configuring fixmap.
Quoted 6 lines
This code path is significantly changed in 5.10-rcX with UEFI series.
https://patchwork.kernel.org/project/linux-riscv/patch/[email protected]/Can you check if you can still reproduce the issue you were seeing with the latest upstream kernel ?
If yes, please share the steps to reproduce the issue.
No, I don't think I can reproduce it now, thanks!
~Vitaly
Quoted 100 lines
Quoted 100 lines
To fix that, move
mmu_enabledtoggling tohead.Sand rename it to_mmu_enabledto avoid possible name clashes since it's not a static variable any more.Signed-off-by Vitaly Wool <[email protected]>--- arch/riscv/kernel/head.S | 9 +++++++++ arch/riscv/mm/init.c | 13 +++++-------- 2 files changed, 14 insertions(+), 8 deletions(-)arch/riscv/kernel/head.S +9
-0@@ -248,6 +248,10 @@ clear_bss_done: call relocate #endif /* CONFIG_MMU */ + la a0, _mmu_enabled + li a1, 1 + sw a1, (a0) + call setup_trap_vector /* Restore C environment */ la tp, init_task@@ -370,6 +374,11 @@ ENTRY(reset_regs) END(reset_regs) #endif /* CONFIG_RISCV_M_MODE */ +.section ".data" + .global _mmu_enabled +_mmu_enabled: + .word 0 + __PAGE_ALIGNED_BSS /* Empty zero page */ .balign PAGE_SIZEarch/riscv/mm/init.c +5
-9@@ -211,7 +211,7 @@ EXPORT_SYMBOL(pfn_base); pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss; pgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss; pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss; -static bool mmu_enabled; +extern bool _mmu_enabled;#define MAX_EARLY_MAPPING_SIZE SZ_128M@@ -236,7 +236,7 @@ void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot)static pte_t *__init get_pte_virt (phys_addr_t pa) { -if (mmu_enabled) { +if (_mmu_enabled) {clear_fixmap (FIX_PTE );return (pte_t *)set_fixmap_offset (FIX_PTE , pa); }else {@@ -250,7 +250,7 @@ static phys_addr_t __init alloc_pte(uintptr_t va)* We only create PMD or PGD early mappings so we * should never reach here with MMU disabled. */ -BUG_ON (!mmu_enabled); +BUG_ON (!_mmu_enabled);return memblock_phys_alloc (PAGE_SIZE ,PAGE_SIZE ); }@@ -281,7 +281,7 @@ pmd_t early_pmd[PTRS_PER_PMD * NUM_EARLY_PMDS] __initdata __aligned(PAGE_SIZE);static pmd_t *__init get_pmd_virt (phys_addr_t pa) { -if (mmu_enabled) { +if (_mmu_enabled) {clear_fixmap (FIX_PMD );return (pmd_t *)set_fixmap_offset (FIX_PMD , pa); }else {@@ -293,7 +293,7 @@ static phys_addr_t __init alloc_pmd(uintptr_t va) {uintptr_t pmd_num; -if (mmu_enabled) +if (_mmu_enabled)return memblock_phys_alloc (PAGE_SIZE ,PAGE_SIZE ); pmd_num = (va -PAGE_OFFSET ) >>PGDIR_SHIFT ;@@ -467,9 +467,6 @@ static void __init setup_vm_final(void)phys_addr_t pa, start, end;struct memblock_region *reg; -/* Set mmu_enabled flag */ - mmu_enabled = true; -/* Setup swapper PGD for fixmap */ create_pgd_mapping(swapper_pg_dir, FIXADDR_START, __pa_symbol(fixmap_pgd_next), --2.20.1
Quoted 10 lines
Quoted 4 lines
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv--
Regards,
Atish
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv