Skip to contentSkip to conversation

riscv: mm: execute local TLB flush after populating vmemmap

PATCH Superseded Vincent Chen 4 participants 6 messages Apr 17, 2023 – Dec 12, 2023 1 message not in the archive RISC-V

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 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 the 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 | 7 +++++++
 arch/riscv/mm/init.c              | 5 +++++
 2 files changed, 12 insertions(+)
arch/riscv/include/asm/tlbflush.h +7 -0
index a09196f8de68..f9d3712bd93b 100644
@@ -61,4 +61,11 @@ static inline void flush_tlb_kernel_range(unsigned long start,
 	flush_tlb_all();
 }
 
+/* Flush a range of kernel pages without broadcasting */
+static inline void local_flush_tlb_kernel_range(unsigned long start,
+						unsigned long end)
+{
+	local_flush_tlb_all();
+}
+
 #endif /* _ASM_RISCV_TLBFLUSH_H */
arch/riscv/mm/init.c +5 -0
index 0f14f4a8d179..bcf365cbbcc1 100644
@@ -1221,6 +1221,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();
 	reserve_crashkernel();
 	memblock_dump_all();
@@ -1230,6 +1234,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
Signature · 7 lines
2.25.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
maintainer
#1 lore raw
Quoted 26 lines · On Sun, 16 Apr 2023 23:06:18 PDT (-0700), [email protected] wrote:

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 the 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 | 7 +++++++
 arch/riscv/mm/init.c              | 5 +++++
 2 files changed, 12 insertions(+)
arch/riscv/include/asm/tlbflush.h +0 -0
index a09196f8de68..f9d3712bd93b 100644

arch/riscv/include/asm/tlbflush.h static inline void flush_tlb_kernel_range(unsigned long start, · line 68

Quoted 8 lines
arch/riscv/include/asm/tlbflush.h +5 -0
@@ -61,4 +61,11 @@ static inline void flush_tlb_kernel_range(unsigned long start,
 	flush_tlb_all();
 }
 
+/* Flush a range of kernel pages without broadcasting */
+static inline void local_flush_tlb_kernel_range(unsigned long start,
+						unsigned long end)
+{
+	local_flush_tlb_all();

Why is this a global flush, istead of a range-based one? We should probably make the same change for flush_tlb_kernel_range(), though that's not a fix.

Quoted 19 lines
arch/riscv/include/asm/tlbflush.h +2 -0
+}
+
 #endif /* _ASM_RISCV_TLBFLUSH_H */
arch/riscv/mm/init.c +4 -0
index 0f14f4a8d179..bcf365cbbcc1 100644
@@ -1221,6 +1221,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();
 	reserve_crashkernel();
 	memblock_dump_all();

arch/riscv/mm/init.c void __init misc_mem_init(void) · line 1240

Quoted 7 lines
arch/riscv/mm/init.c +1 -0
@@ -1230,6 +1234,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

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

#2 lore raw
Quoted 31 lines · Palmer Dabbelt
Quoted 29 lines · On Sun, 16 Apr 2023 23:06:18 PDT (-0700), [email protected] wrote:

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 the 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 | 7 +++++++
 arch/riscv/mm/init.c              | 5 +++++
 2 files changed, 12 insertions(+)
arch/riscv/include/asm/tlbflush.h +0 -0
index a09196f8de68..f9d3712bd93b 100644
@@ -61,4 +61,11 @@ static inline void flush_tlb_kernel_range(unsigned long start,
      flush_tlb_all();
 }
 
Quoted 9 lines

arch/riscv/include/asm/tlbflush.h static inline void flush_tlb_kernel_range(unsigned long start, · line 68

Quoted 5 lines
arch/riscv/include/asm/tlbflush.h +5 -0
+/* Flush a range of kernel pages without broadcasting */
+static inline void local_flush_tlb_kernel_range(unsigned long start,
+                                             unsigned long end)
+{
+     local_flush_tlb_all();

Why is this a global flush, istead of a range-based one? We should probably make the same change for flush_tlb_kernel_range(), though that's not a fix.

You're right, but the current implementation of flush_tlb_range will ultimately call flush_tlb_all if the size > PAGE_SIZE, which is very likely in this case. Anyway, I have a patchset almost ready that improves the tlb flush routines, and I noted that I had to fix this function once merged.

Quoted 19 lines
Quoted 19 lines
arch/riscv/include/asm/tlbflush.h +2 -0
+}
+
 #endif /* _ASM_RISCV_TLBFLUSH_H */
arch/riscv/mm/init.c +4 -0
index 0f14f4a8d179..bcf365cbbcc1 100644
@@ -1221,6 +1221,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();
      reserve_crashkernel();
      memblock_dump_all();
Quoted 8 lines

arch/riscv/mm/init.c void __init misc_mem_init(void) · line 1240

Quoted 8 lines
arch/riscv/mm/init.c +1 -0
@@ -1230,6 +1234,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

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

#3 lore raw
Quoted 43 lines · Alexandre Ghiti
Quoted 31 lines · Palmer Dabbelt
Quoted 29 lines · On Sun, 16 Apr 2023 23:06:18 PDT (-0700), [email protected] wrote:

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 the 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 | 7 +++++++
 arch/riscv/mm/init.c              | 5 +++++
 2 files changed, 12 insertions(+)
arch/riscv/include/asm/tlbflush.h +0 -0
index a09196f8de68..f9d3712bd93b 100644
@@ -61,4 +61,11 @@ static inline void flush_tlb_kernel_range(unsigned long start,
      flush_tlb_all();
 }
 
Quoted 9 lines

arch/riscv/include/asm/tlbflush.h static inline void flush_tlb_kernel_range(unsigned long start, · line 68

Quoted 5 lines
arch/riscv/include/asm/tlbflush.h +5 -0
+/* Flush a range of kernel pages without broadcasting */
+static inline void local_flush_tlb_kernel_range(unsigned long start,
+                                             unsigned long end)
+{
+     local_flush_tlb_all();

Why is this a global flush, istead of a range-based one? We should probably make the same change for flush_tlb_kernel_range(), though that's not a fix.

Quoted 6 lines

You're right, but the current implementation of flush_tlb_range will ultimately call flush_tlb_all if the size > PAGE_SIZE, which is very likely in this case. Anyway, I have a patchset almost ready that improves the tlb flush routines, and I noted that I had to fix this function once merged.

I agree with Alexandre that it's highly probable that the size will be greater than PAGE_SIZE in this case. However, I must admit that this implementation is not suitable for the case where the size is less than or equal to PAGE_SIZE. I am willing to wait for Alexandre's patch and rebase my patch on top of his If you believe it is a better approach.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
arch/riscv/include/asm/tlbflush.h +2 -0
+}
+
 #endif /* _ASM_RISCV_TLBFLUSH_H */
arch/riscv/mm/init.c +4 -0
index 0f14f4a8d179..bcf365cbbcc1 100644
@@ -1221,6 +1221,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();
      reserve_crashkernel();
      memblock_dump_all();
Quoted 8 lines
Quoted 8 lines

arch/riscv/mm/init.c void __init misc_mem_init(void) · line 1240

Quoted 8 lines
arch/riscv/mm/init.c +1 -0
@@ -1230,6 +1234,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

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

#4 lore raw

Hi Vincent,

Quoted 46 lines · Vincent Chen
Quoted 39 lines · Alexandre Ghiti
Quoted 30 lines · Palmer Dabbelt
Quoted 29 lines · On Sun, 16 Apr 2023 23:06:18 PDT (-0700), [email protected] wrote:

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 the 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 | 7 +++++++
  arch/riscv/mm/init.c              | 5 +++++
  2 files changed, 12 insertions(+)
arch/riscv/include/asm/tlbflush.h +0 -0
index a09196f8de68..f9d3712bd93b 100644
@@ -61,4 +61,11 @@ static inline void flush_tlb_kernel_range(unsigned long start,
       flush_tlb_all();
  }
 
Quoted 8 lines

arch/riscv/include/asm/tlbflush.h static inline void flush_tlb_kernel_range(unsigned long start, · line 68

Quoted 5 lines
arch/riscv/include/asm/tlbflush.h +5 -0
+/* Flush a range of kernel pages without broadcasting */
+static inline void local_flush_tlb_kernel_range(unsigned long start,
+                                             unsigned long end)
+{
+     local_flush_tlb_all();

Why is this a global flush, istead of a range-based one? We should probably make the same change for flush_tlb_kernel_range(), though that's not a fix.

Quoted 6 lines

You're right, but the current implementation of flush_tlb_range will ultimately call flush_tlb_all if the size > PAGE_SIZE, which is very likely in this case. Anyway, I have a patchset almost ready that improves the tlb flush routines, and I noted that I had to fix this function once merged.

Quoted 6 lines

I agree with Alexandre that it's highly probable that the size will be greater than PAGE_SIZE in this case. However, I must admit that this implementation is not suitable for the case where the size is less than or equal to PAGE_SIZE. I am willing to wait for Alexandre's patch and rebase my patch on top of his If you believe it is a better approach.

My patchset was merged in 6.6, so you can now rebase this :) Let me know if I can help.

Thanks,

Alex

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
arch/riscv/include/asm/tlbflush.h +2 -0
+}
+
  #endif /* _ASM_RISCV_TLBFLUSH_H */
arch/riscv/mm/init.c +4 -0
index 0f14f4a8d179..bcf365cbbcc1 100644
@@ -1221,6 +1221,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();
       reserve_crashkernel();
       memblock_dump_all();
@@ -1230,6 +1234,7 @@ void __init misc_mem_init(void)
  int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
                              struct vmem_altmap *altmap)
  {
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

arch/riscv/mm/init.c void __init misc_mem_init(void) · line 1240

Quoted 4 lines
arch/riscv/mm/init.c +1 -0
+     /* Defer the required TLB flush until the entire VMEMMAP region has been populated */
       return vmemmap_populate_basepages(start, end, node, NULL);
  }
  #endif

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

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

A message that is not in the archive (sent off-list or not yet received): CABvJ_xhRnK4N8k36h6R2PaxmokM6Sf1a2xpy5LjgEZoywX3OEQ@mail.gmail.com

#6 lore raw
Quoted 86 lines · Vincent Chen

On Mon, Dec 11, 2023 at 4:57 PM Alexandre Ghiti <[email protected]> wrote:

Hi Vincent,

On 04/05/2023 11:13, Vincent Chen wrote:
> On Tue, May 2, 2023 at 8:16 PM Alexandre Ghiti <[email protected]> wrote:
>> On Tue, May 2, 2023 at 2:18 AM Palmer Dabbelt <[email protected]> wrote:
>>> On Sun, 16 Apr 2023 23:06:18 PDT (-0700), [email protected] wrote:
>>>> 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 the
>>>> VMEMMAP area, ensuring that all data related to the address translation
>>>> is visible to the hart.
>>>>
>>>> Fixes: d95f1a542c3d ("RISC-V: Implement sparsemem") >>>> Signed-off-by: Vincent Chen <[email protected]> >>>> Reviewed-by: Alexandre Ghiti <[email protected]> >>>> Reviewed-by: Andrew Jones <[email protected]> >>>> ---
>>>>   arch/riscv/include/asm/tlbflush.h | 7 +++++++
>>>>   arch/riscv/mm/init.c              | 5 +++++
>>>>   2 files changed, 12 insertions(+)
>>>>
>>>> diff --git a/arch/riscv/include/asm/tlbflush.h b/arch/riscv/include/asm/tlbflush.h
>>>> index a09196f8de68..f9d3712bd93b 100644
>>>> --- a/arch/riscv/include/asm/tlbflush.h
>>>> +++ b/arch/riscv/include/asm/tlbflush.h
>>>> @@ -61,4 +61,11 @@ static inline void flush_tlb_kernel_range(unsigned long start,
>>>>        flush_tlb_all();
>>>>   }
>>>>
>>>> +/* Flush a range of kernel pages without broadcasting */ >>>> +static inline void local_flush_tlb_kernel_range(unsigned long start,
>>>> +    unsigned long end)
>>>> +{
>>>> +     local_flush_tlb_all();
>>> Why is this a global flush, istead of a range-based one?  We should
>>> probably make the same change for flush_tlb_kernel_range(), though >>> that's not a fix.
>> You're right, but the current implementation of flush_tlb_range will
>> ultimately call flush_tlb_all if the size > PAGE_SIZE, which is very
>> likely in this case. Anyway, I have a patchset almost ready that >> improves the tlb flush routines, and I noted that I had to fix this >> function once merged.
>>
> I agree with Alexandre that it's highly probable that the size will be
> greater than PAGE_SIZE in this case. However, I must admit that this > implementation is not suitable for the case where the size is less > than or equal to PAGE_SIZE. I am willing to wait for Alexandre's patch
> and rebase my patch on top of his If you believe it is a better > approach.

My patchset was merged in 6.6, so you can now rebase this :) Let me know
if I can help.

Quoted 5 lines

Thanks,

Alex

Thank you for the notification. I will rebase my patch on the Linux 6.6.

Sorry, it actually landed in 6.7, not 6.6.

Thanks!

Alex

Quoted 32 lines

Thanks,
Vincent

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
arch/riscv/include/asm/tlbflush.h +2 -0
+}
+

   #endif /* _ASM_RISCV_TLBFLUSH_H */

arch/riscv/mm/init.c +0 -0
index 0f14f4a8d179..bcf365cbbcc1 100644
@@ -1221,6 +1221,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();
    >>>>        reserve_crashkernel();
    >>>>        memblock_dump_all();
    >>>> @@ -1230,6 +1234,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
Quoted 9 lines
    VMEMMAP region has been populated */
    >>>>        return vmemmap_populate_basepages(start, end, node, NULL);
    >>>>   }
    >>>>   #endif
    > _______________________________________________
    > linux-riscv mailing list
    > [email protected]
    > http://lists.infradead.org/mailman/listinfo/linux-riscv

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