Skip to contentSkip to conversation

riscv: mm: execute local TLB flush after populating vmemmap

PATCH Superseded Vincent Chen 2 participants 3 messages Dec 20, 2023 – Jan 17, 2024 RISC-V

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 riscv: mm: execute local TLB flush after populating vmemmap

The spare_init() calls memmap_populate() many times to create VA to PA mapping for the VMEMMAP area, where all "struct page" are located once CONFIG_SPARSEMEM_VMEMMAP is defined. These "struct page" are later initialized in the zone_sizes_init() function. However, during this process, no sfence.vma instruction is executed for this VMEMMAP area. This omission may cause the hart to fail to perform page table walk because some data related to the address translation is invisible to the hart. To solve this issue, the local_flush_tlb_kernel_range() is called right after the spare_init() to execute a sfence.vma instruction for this VMEMMAP area, ensuring that all data related to the address translation is visible to the hart.

Fixes d95f1a542c3d ("RISC-V: Implement sparsemem")
---
 arch/riscv/include/asm/tlbflush.h | 2 ++
 arch/riscv/mm/init.c              | 5 +++++
 arch/riscv/mm/tlbflush.c          | 6 ++++++
 3 files changed, 13 insertions(+)
arch/riscv/include/asm/tlbflush.h +2 -0
index 8f3418c5f172..525267379ccb 100644
@@ -46,6 +46,7 @@ void flush_tlb_kernel_range(unsigned long start, unsigned long end);
 void flush_pmd_tlb_range(struct vm_area_struct *vma, unsigned long start,
 			unsigned long end);
 #endif
+void local_flush_tlb_kernel_range(unsigned long start, unsigned long end);
 #else /* CONFIG_SMP && CONFIG_MMU */
 
 #define flush_tlb_all() local_flush_tlb_all()
@@ -66,6 +67,7 @@ static inline void flush_tlb_kernel_range(unsigned long start,
 
 #define flush_tlb_mm(mm) flush_tlb_all()
 #define flush_tlb_mm_range(mm, start, end, page_size) flush_tlb_all()
+#define local_flush_tlb_kernel_range(start, end) flush_tlb_all()
 #endif /* !CONFIG_SMP || !CONFIG_MMU */
 
 #endif /* _ASM_RISCV_TLBFLUSH_H */
arch/riscv/mm/init.c +5 -0
index 2e011cbddf3a..cc56a0945120 100644
@@ -1377,6 +1377,10 @@ void __init misc_mem_init(void)
 	early_memtest(min_low_pfn << PAGE_SHIFT, max_low_pfn << PAGE_SHIFT);
 	arch_numa_init();
 	sparse_init();
+#ifdef CONFIG_SPARSEMEM_VMEMMAP
+	/* The entire VMEMMAP region has been populated. Flush TLB for this region */
+	local_flush_tlb_kernel_range(VMEMMAP_START, VMEMMAP_END);
+#endif
 	zone_sizes_init();
 	arch_reserve_crashkernel();
 	memblock_dump_all();
@@ -1386,6 +1390,7 @@ void __init misc_mem_init(void)
 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
 			       struct vmem_altmap *altmap)
 {
+	/* Defer the required TLB flush until the entire VMEMMAP region has been populated */
 	return vmemmap_populate_basepages(start, end, node, NULL);
 }
 #endif
arch/riscv/mm/tlbflush.c +6 -0
index e6659d7368b3..d11a4ae87ec1 100644
@@ -193,6 +193,12 @@ void flush_tlb_kernel_range(unsigned long start, unsigned long end)
 	__flush_tlb_range(NULL, start, end - start, PAGE_SIZE);
 }
 
+/* Flush a range of kernel pages without broadcasting */
+void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
+{
+	local_flush_tlb_range_asid(start, end - start, PAGE_SIZE, FLUSH_TLB_NO_ASID);
+}
+
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 void flush_pmd_tlb_range(struct vm_area_struct *vma, unsigned long start,
 			unsigned long end)
Signature · 7 lines
2.34.1


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

Hi Vincent,

Quoted 11 lines · Vincent Chen

The spare_init() calls memmap_populate() many times to create VA to PA mapping for the VMEMMAP area, where all "struct page" are located once CONFIG_SPARSEMEM_VMEMMAP is defined. These "struct page" are later initialized in the zone_sizes_init() function. However, during this process, no sfence.vma instruction is executed for this VMEMMAP area. This omission may cause the hart to fail to perform page table walk because some data related to the address translation is invisible to the hart. To solve this issue, the local_flush_tlb_kernel_range() is called right after the spare_init() to execute a sfence.vma instruction for this VMEMMAP area, ensuring that all data related to the address translation is visible to the hart.

s/spare_init/sparse_init

Quoted 59 lines
Fixes d95f1a542c3d ("RISC-V: Implement sparsemem")
---
  arch/riscv/include/asm/tlbflush.h | 2 ++
  arch/riscv/mm/init.c              | 5 +++++
  arch/riscv/mm/tlbflush.c          | 6 ++++++
  3 files changed, 13 insertions(+)
arch/riscv/include/asm/tlbflush.h +2 -0
index 8f3418c5f172..525267379ccb 100644
@@ -46,6 +46,7 @@ void flush_tlb_kernel_range(unsigned long start, unsigned long end);
  void flush_pmd_tlb_range(struct vm_area_struct *vma, unsigned long start,
  			unsigned long end);
  #endif
+void local_flush_tlb_kernel_range(unsigned long start, unsigned long end);
  #else /* CONFIG_SMP && CONFIG_MMU */
 
  #define flush_tlb_all() local_flush_tlb_all()
@@ -66,6 +67,7 @@ static inline void flush_tlb_kernel_range(unsigned long start,
 
  #define flush_tlb_mm(mm) flush_tlb_all()
  #define flush_tlb_mm_range(mm, start, end, page_size) flush_tlb_all()
+#define local_flush_tlb_kernel_range(start, end) flush_tlb_all()
  #endif /* !CONFIG_SMP || !CONFIG_MMU */
 
  #endif /* _ASM_RISCV_TLBFLUSH_H */
arch/riscv/mm/init.c +5 -0
index 2e011cbddf3a..cc56a0945120 100644
@@ -1377,6 +1377,10 @@ void __init misc_mem_init(void)
  	early_memtest(min_low_pfn << PAGE_SHIFT, max_low_pfn << PAGE_SHIFT);
  	arch_numa_init();
  	sparse_init();
+#ifdef CONFIG_SPARSEMEM_VMEMMAP
+	/* The entire VMEMMAP region has been populated. Flush TLB for this region */
+	local_flush_tlb_kernel_range(VMEMMAP_START, VMEMMAP_END);
+#endif
  	zone_sizes_init();
  	arch_reserve_crashkernel();
  	memblock_dump_all();
@@ -1386,6 +1390,7 @@ void __init misc_mem_init(void)
  int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
  			       struct vmem_altmap *altmap)
  {
+	/* Defer the required TLB flush until the entire VMEMMAP region has been populated */
  	return vmemmap_populate_basepages(start, end, node, NULL);
  }
  #endif
arch/riscv/mm/tlbflush.c +0 -0
index e6659d7368b3..d11a4ae87ec1 100644
@@ -193,6 +193,12 @@ void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  	__flush_tlb_range(NULL, start, end - start, PAGE_SIZE);
  }

arch/riscv/mm/tlbflush.c void flush_tlb_kernel_range(unsigned long start, unsigned long end) · line 204

Quoted 10 lines
arch/riscv/mm/tlbflush.c +6 -0
 
+/* Flush a range of kernel pages without broadcasting */
+void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
+{
+	local_flush_tlb_range_asid(start, end - start, PAGE_SIZE, FLUSH_TLB_NO_ASID);
+}
+
  #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  void flush_pmd_tlb_range(struct vm_area_struct *vma, unsigned long start,
  			unsigned long end)

Otherwise, this looks good to me and should go to -fixes, you can add:

Thanks!

Alex

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

#2 lore raw
Quoted 93 lines · Alexandre Ghiti

Hi Vincent,

Quoted 11 lines · Vincent Chen

The spare_init() calls memmap_populate() many times to create VA to PA mapping for the VMEMMAP area, where all "struct page" are located once CONFIG_SPARSEMEM_VMEMMAP is defined. These "struct page" are later initialized in the zone_sizes_init() function. However, during this process, no sfence.vma instruction is executed for this VMEMMAP area. This omission may cause the hart to fail to perform page table walk because some data related to the address translation is invisible to the hart. To solve this issue, the local_flush_tlb_kernel_range() is called right after the spare_init() to execute a sfence.vma instruction for this VMEMMAP area, ensuring that all data related to the address translation is visible to the hart.

s/spare_init/sparse_init

Quoted 59 lines
Fixes d95f1a542c3d ("RISC-V: Implement sparsemem")
---
  arch/riscv/include/asm/tlbflush.h | 2 ++
  arch/riscv/mm/init.c              | 5 +++++
  arch/riscv/mm/tlbflush.c          | 6 ++++++
  3 files changed, 13 insertions(+)
arch/riscv/include/asm/tlbflush.h +2 -0
index 8f3418c5f172..525267379ccb 100644
@@ -46,6 +46,7 @@ void flush_tlb_kernel_range(unsigned long start, unsigned long end);
  void flush_pmd_tlb_range(struct vm_area_struct *vma, unsigned long start,
                      unsigned long end);
  #endif
+void local_flush_tlb_kernel_range(unsigned long start, unsigned long end);
  #else /* CONFIG_SMP && CONFIG_MMU */
 
  #define flush_tlb_all() local_flush_tlb_all()
@@ -66,6 +67,7 @@ static inline void flush_tlb_kernel_range(unsigned long start,
 
  #define flush_tlb_mm(mm) flush_tlb_all()
  #define flush_tlb_mm_range(mm, start, end, page_size) flush_tlb_all()
+#define local_flush_tlb_kernel_range(start, end) flush_tlb_all()
  #endif /* !CONFIG_SMP || !CONFIG_MMU */
 
  #endif /* _ASM_RISCV_TLBFLUSH_H */
arch/riscv/mm/init.c +5 -0
index 2e011cbddf3a..cc56a0945120 100644
@@ -1377,6 +1377,10 @@ void __init misc_mem_init(void)
      early_memtest(min_low_pfn << PAGE_SHIFT, max_low_pfn << PAGE_SHIFT);
      arch_numa_init();
      sparse_init();
+#ifdef CONFIG_SPARSEMEM_VMEMMAP
+     /* The entire VMEMMAP region has been populated. Flush TLB for this region */
+     local_flush_tlb_kernel_range(VMEMMAP_START, VMEMMAP_END);
+#endif
      zone_sizes_init();
      arch_reserve_crashkernel();
      memblock_dump_all();
@@ -1386,6 +1390,7 @@ void __init misc_mem_init(void)
  int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
                             struct vmem_altmap *altmap)
  {
+     /* Defer the required TLB flush until the entire VMEMMAP region has been populated */
      return vmemmap_populate_basepages(start, end, node, NULL);
  }
  #endif
arch/riscv/mm/tlbflush.c +0 -0
index e6659d7368b3..d11a4ae87ec1 100644
@@ -193,6 +193,12 @@ void flush_tlb_kernel_range(unsigned long start, unsigned long end)
      __flush_tlb_range(NULL, start, end - start, PAGE_SIZE);
  }

arch/riscv/mm/tlbflush.c void flush_tlb_kernel_range(unsigned long start, unsigned long end) · line 204

Quoted 10 lines
arch/riscv/mm/tlbflush.c +6 -0
 
+/* Flush a range of kernel pages without broadcasting */
+void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
+{
+     local_flush_tlb_range_asid(start, end - start, PAGE_SIZE, FLUSH_TLB_NO_ASID);
+}
+
  #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  void flush_pmd_tlb_range(struct vm_area_struct *vma, unsigned long start,
                      unsigned long end)

Otherwise, this looks good to me and should go to -fixes, you can add:

Quoted 3 lines

Thanks!

Apologies for the delayed response, and thank you for the review. I will fix the typo and add your 'Reviewed-by' in the v6 patch.

Vincent

Quoted 3 lines

Alex

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