Skip to contentSkip to conversation

riscv: move memblock_allow_resize() after lm is ready

PATCH Superseded Woody Zhang 4 participants 7 messages Jun 9, 2023 – Jun 14, 2023 RISC-V

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: move memblock_allow_resize() after lm is ready

The initial memblock metadata is accessed from kernel image mapping. The regions arrays need to "reallocated" from memblock and accessed through linear mapping to cover more memblock regions. So the resizing should not be allowed until linear mapping is ready. Note that there are memblock allocations when building linear mapping.

---
 arch/riscv/mm/init.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/riscv/mm/init.c +3 -1
index 9e9da69720ce..8a33ecbb4d0f 100644
@@ -258,7 +258,6 @@ static void __init setup_bootmem(void)
 	dma_contiguous_reserve(dma32_phys_limit);
 	if (IS_ENABLED(CONFIG_64BIT))
 		hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT);
-	memblock_allow_resize();
 }
 
 #ifdef CONFIG_MMU
@@ -1250,6 +1249,9 @@ static void __init setup_vm_final(void)
 	csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | satp_mode);
 	local_flush_tlb_all();
 
+	/* Depend on that Linear Mapping is ready */
+	memblock_allow_resize();
+
 	pt_ops_set_late();
 }
 #else
Signature · 1 line
2.39.2
#1 lore raw

On Sat, Jun 10, 2023 at 07:43:20AM +0800, Woody Zhang wrote:
$subject: "riscv: move memblock_allow_resize() after lm is ready"

s/lm/linear map/ if you resubmit, please.

Quoted 7 lines

The initial memblock metadata is accessed from kernel image mapping. The regions arrays need to "reallocated" from memblock and accessed through linear mapping to cover more memblock regions. So the resizing should not be allowed until linear mapping is ready. Note that there are memblock allocations when building linear mapping.

What commit does this fix?

Cheers,
Conor.

#2 lore raw
Quoted 4 lines · Conor Dooley

On Sat, Jun 10, 2023 at 07:43:20AM +0800, Woody Zhang wrote:
$subject: "riscv: move memblock_allow_resize() after lm is ready"

s/lm/linear map/ if you resubmit, please.

Thanks, will send a v2.

Quoted 10 lines
Quoted 8 lines

The initial memblock metadata is accessed from kernel image mapping. The regions arrays need to "reallocated" from memblock and accessed through linear mapping to cover more memblock regions. So the resizing should not be allowed until linear mapping is ready. Note that there are memblock allocations when building linear mapping.

What commit does this fix?

Fixes 671f9a3e2e24 ("RISC-V: Setup initial page tables in two stages")

Will add this tag in v2.

Woody

#3 lore raw
Quoted 15 lines · Woody Zhang
Quoted 4 lines · Conor Dooley

On Sat, Jun 10, 2023 at 07:43:20AM +0800, Woody Zhang wrote:
$subject: "riscv: move memblock_allow_resize() after lm is ready"

s/lm/linear map/ if you resubmit, please.

Thanks, will send a v2.

Quoted 7 lines
Quoted 7 lines

The initial memblock metadata is accessed from kernel image mapping. The regions arrays need to "reallocated" from memblock and accessed through linear mapping to cover more memblock regions. So the resizing should not be allowed until linear mapping is ready. Note that there are memblock allocations when building linear mapping.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

What commit does this fix?

Fixes 671f9a3e2e24 ("RISC-V: Setup initial page tables in two stages")

Will add this tag in v2.

b4 should pick up fixes tags from replies, you shouldn't need to resubmit for that alone.

Cheers,
Conor.

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

#4 lore raw
In reply to Woody Zhang

Hi Woody,

Quoted 35 lines · Woody Zhang

The initial memblock metadata is accessed from kernel image mapping. The regions arrays need to "reallocated" from memblock and accessed through linear mapping to cover more memblock regions. So the resizing should not be allowed until linear mapping is ready. Note that there are memblock allocations when building linear mapping.

---
 arch/riscv/mm/init.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/riscv/mm/init.c +3 -2
index 9e9da69720ce..8a33ecbb4d0f 100644
@@ -258,7 +258,6 @@ static void __init setup_bootmem(void)
        dma_contiguous_reserve(dma32_phys_limit);
        if (IS_ENABLED(CONFIG_64BIT))
                hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT);
-       memblock_allow_resize();
 }
 
 #ifdef CONFIG_MMU
@@ -1250,6 +1249,9 @@ static void __init setup_vm_final(void)
        csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | satp_mode);
        local_flush_tlb_all();
 
+       /* Depend on that Linear Mapping is ready */
+       memblock_allow_resize();
+
        pt_ops_set_late();
 }
 #else
--
Quoted 2 lines

2.39.2

The commit log does not describe the issue thoroughly enough to me, maybe you could point to the arm64 commit that did the same? I mean commit 24cc61d8cb5a ("arm64: memblock: don't permit memblock resizing until linear mapping is up").

Another point is that I would not put this call into setup_vm_final(), I'd rather add it in paging_init() as it does not seem like a good fit for setup_vm_final(). But that's a nit so up to you of course.

Anyway, that's a good catch, thanks!

Alex

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

#5 lore raw
Quoted 31 lines · Alexandre Ghiti

Hi Woody,

Quoted 28 lines · Woody Zhang

The initial memblock metadata is accessed from kernel image mapping. The regions arrays need to "reallocated" from memblock and accessed through linear mapping to cover more memblock regions. So the resizing should not be allowed until linear mapping is ready. Note that there are memblock allocations when building linear mapping.

---
  arch/riscv/mm/init.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
arch/riscv/mm/init.c +0 -1
index 9e9da69720ce..8a33ecbb4d0f 100644
@@ -258,7 +258,6 @@ static void __init setup_bootmem(void)
         dma_contiguous_reserve(dma32_phys_limit);
         if (IS_ENABLED(CONFIG_64BIT))
                 hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT);
-       memblock_allow_resize();
  }
 
  #ifdef CONFIG_MMU
@@ -1250,6 +1249,9 @@ static void __init setup_vm_final(void)
         csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | satp_mode);
         local_flush_tlb_all();
 
Quoted 14 lines
Quoted 9 lines
arch/riscv/mm/init.c +3 -1
+       /* Depend on that Linear Mapping is ready */
+       memblock_allow_resize();
+
         pt_ops_set_late();
  }
  #else
--

2.39.2

The commit log does not describe the issue thoroughly enough to me, maybe you could point to the arm64 commit that did the same? I mean commit 24cc61d8cb5a ("arm64: memblock: don't permit memblock resizing until linear mapping is up").

@Alex

I reproduced the problem as the arm64 commit describes. You can find the complete log via this link: https://termbin.com/bx0o

I constructed the dtb with numerous discrete /memreserve/ regions
(the numbers of these regions approximate INIT_MEMBLOCK_REGIONS) which full the reserved regions up.

When memblock_allow_resize was set, the calling of memblock_reserve() would double/resize the reserved regions and do the __memcopy() from the old regions ( mapped by kernel) to the new ones (provided by __va()).
But before the linear mapping was ready (like: during the creating of linear mapping), memblock_reserve() was called and the memcopy would trigger a Store/AMO page fault.

Quoted 5 lines

Another point is that I would not put this call into setup_vm_final(), I'd rather add it in paging_init() as it does not seem like a good fit for setup_vm_final(). But that's a nit so up to you of course.

I agree.

@Woody

I noticed your V2 [1] didn't take this suggestion, maybe you can take it at V3. And it will be more sound if you supplement the commit-msg with the panic info from the log.

[1]: https://lore.kernel.org/linux-riscv/[email protected]/

Quoted 8 lines

Anyway, that's a good catch, thanks!

Alex

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

Signature · 2 lines
Thanks
Song Shuai
#6 lore raw
Quoted 71 lines · Song Shuai
Quoted 31 lines · Alexandre Ghiti

Hi Woody,

Quoted 28 lines · Woody Zhang

The initial memblock metadata is accessed from kernel image mapping. The regions arrays need to "reallocated" from memblock and accessed through linear mapping to cover more memblock regions. So the resizing should not be allowed until linear mapping is ready. Note that there are memblock allocations when building linear mapping.

---
  arch/riscv/mm/init.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
arch/riscv/mm/init.c +0 -1
index 9e9da69720ce..8a33ecbb4d0f 100644
@@ -258,7 +258,6 @@ static void __init setup_bootmem(void)
         dma_contiguous_reserve(dma32_phys_limit);
         if (IS_ENABLED(CONFIG_64BIT))
                 hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT);
-       memblock_allow_resize();
  }
 
  #ifdef CONFIG_MMU
@@ -1250,6 +1249,9 @@ static void __init setup_vm_final(void)
         csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | satp_mode);
         local_flush_tlb_all();
 
Quoted 14 lines
Quoted 9 lines
arch/riscv/mm/init.c +3 -1
+       /* Depend on that Linear Mapping is ready */
+       memblock_allow_resize();
+
         pt_ops_set_late();
  }
  #else
--

2.39.2

The commit log does not describe the issue thoroughly enough to me, maybe you could point to the arm64 commit that did the same? I mean commit 24cc61d8cb5a ("arm64: memblock: don't permit memblock resizing until linear mapping is up").

@Alex

I reproduced the problem as the arm64 commit describes. You can find the complete log via this link: https://termbin.com/bx0o

I constructed the dtb with numerous discrete /memreserve/ regions (the numbers of these regions approximate INIT_MEMBLOCK_REGIONS) which full the reserved regions up.

When memblock_allow_resize was set, the calling of memblock_reserve() would double/resize the reserved regions and do the __memcopy() from the old regions ( mapped by kernel)
to the new ones (provided by __va()).
But before the linear mapping was ready (like: during the creating of linear mapping),
memblock_reserve() was called and the memcopy would trigger a Store/AMO page fault.

Quoted 5 lines

Another point is that I would not put this call into setup_vm_final(), I'd rather add it in paging_init() as it does not seem like a good fit for setup_vm_final(). But that's a nit so up to you of course.

Quoted 9 lines

I agree.

@Woody

I noticed your V2 [1] didn't take this suggestion, maybe you can take it at V3.
And it will be more sound if you supplement the commit-msg with the panic info from the log.

Thanks for your comments. I will update it with your panic log in next version.

Woody

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