Skip to contentSkip to conversation

riscv: mm: execute local TLB flush after populating vmemmap

PATCH In review Vincent Chen 2 participants 3 messages Mar 20, 2023 – Apr 13, 2023 1 message not in the archive RISC-V

lore
Share & tools
lore
#0 lore raw

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

The vmemmap_populate() creates VA to PA mapping for the VMEMMAP area, where all "strcut 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 core to fail to perform page table work because some data related to the address translation is invisible to the core. To solve this issue, here let zone_sizes_init() call local_flush_tlb_kernel_range() to execute a sfence.vma instruction for the VMEMMAP area, ensuring that all data related to the address translation is visible to the core.

---
 arch/riscv/include/asm/tlbflush.h | 7 +++++++
 arch/riscv/mm/init.c              | 3 +++
 2 files changed, 10 insertions(+)
arch/riscv/include/asm/tlbflush.h +7 -0
index 801019381dea..b7696974e3c6 100644
@@ -59,4 +59,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 +3 -0
index 2c4a64e97aec..62eea656e341 100644
@@ -72,6 +72,9 @@ static void __init zone_sizes_init(void)
 #endif
 	max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
 
+#ifdef CONFIG_SPARSEMEM_VMEMMAP
+	local_flush_tlb_kernel_range(VMEMMAP_START, VMEMMAP_END);
+#endif
 	free_area_init(max_zone_pfns);
 }
 
Signature · 7 lines
2.25.1


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

Hi Vincent,

Quoted 38 lines · Vincent Chen

The vmemmap_populate() creates VA to PA mapping for the VMEMMAP area, where all "strcut 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 core to fail to perform page table work because some data related to the address translation is invisible to the core. To solve this issue, here let zone_sizes_init() call local_flush_tlb_kernel_range() to execute a sfence.vma instruction for the VMEMMAP area, ensuring that all data related to the address translation is visible to the core.

---
  arch/riscv/include/asm/tlbflush.h | 7 +++++++
  arch/riscv/mm/init.c              | 3 +++
  2 files changed, 10 insertions(+)
arch/riscv/include/asm/tlbflush.h +7 -0
index 801019381dea..b7696974e3c6 100644
@@ -59,4 +59,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 +0 -0
index 2c4a64e97aec..62eea656e341 100644

arch/riscv/mm/init.c static void __init zone_sizes_init(void) · line 80

Quoted 9 lines
arch/riscv/mm/init.c +3 -0
@@ -72,6 +72,9 @@ static void __init zone_sizes_init(void)
  #endif
  	max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
 
+#ifdef CONFIG_SPARSEMEM_VMEMMAP
+	local_flush_tlb_kernel_range(VMEMMAP_START, VMEMMAP_END);
+#endif
  	free_area_init(max_zone_pfns);
  }
 

Just one nit: I would have added the flush to the vmemmap_populate function instead, so that we don't flush uselessly when !SPARSE_VMEMMAP and we know exactly why we flush.

Otherwise, you can add:

Thanks,

Alex

_______________________________________________
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_xi_oYMk30A7-aR2v_ULbH0x=E1Hwq0pGf-5sBmKGxjXMg@mail.gmail.com

#3 lore raw

Hi Vincent,

Quoted 68 lines · Vincent Chen

On Wed, Mar 22, 2023 at 12:11 AM Alexandre Ghiti <[email protected]> wrote:

    Hi Vincent,

    On 3/20/23 07:53, Vincent Chen wrote:
    > The vmemmap_populate() creates VA to PA mapping for the VMEMMAP
    area, where
    > all "strcut 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 core
    to fail to
    > perform page table work because some data related to the address
    > translation is invisible to the core. To solve this issue, here let
    > zone_sizes_init() call local_flush_tlb_kernel_range() to execute a
    > sfence.vma instruction for the VMEMMAP area, ensuring that all
    data related
    > to the address translation is visible to the core.
    >
    > Signed-off-by: Vincent Chen <[email protected]>
    > ---
    >   arch/riscv/include/asm/tlbflush.h | 7 +++++++
    >   arch/riscv/mm/init.c              | 3 +++
    >   2 files changed, 10 insertions(+)
    >
    > diff --git a/arch/riscv/include/asm/tlbflush.h
    b/arch/riscv/include/asm/tlbflush.h
    > index 801019381dea..b7696974e3c6 100644
    > --- a/arch/riscv/include/asm/tlbflush.h
    > +++ b/arch/riscv/include/asm/tlbflush.h
    > @@ -59,4 +59,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 */
    > diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
    > index 2c4a64e97aec..62eea656e341 100644
    > --- a/arch/riscv/mm/init.c
    > +++ b/arch/riscv/mm/init.c
    > @@ -72,6 +72,9 @@ static void __init zone_sizes_init(void)
    >   #endif
    >       max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
    >
    > +#ifdef CONFIG_SPARSEMEM_VMEMMAP
    > +     local_flush_tlb_kernel_range(VMEMMAP_START, VMEMMAP_END);
    > +#endif
    >       free_area_init(max_zone_pfns);
    >   }
    >

    Just one nit: I would have added the flush to the vmemmap_populate
    function instead, so that we don't flush uselessly when
    !SPARSE_VMEMMAP
    and we know exactly why we flush.
Quoted 3 lines

Sorry for the late reply.

To be honest, initially, I placed the TLB flush in the vmemmap_populate function like you suggested. However, I later discovered that this function could be called multiple times during the booting process, causing the TLB flush to be executed multiple times. To improve performance, I decided to move the TLB flush to the zone_sizes_init function instead, as this is where the kernel first accesses the VMEMMAP region after vmemmap_populate. This allows the kernel to perform only one TLB flush for the region, but the tradeoff is that the code readability is reduced. Do you think this tradeoff is worth it? or Maybe I can add some comments in the vmemmap_populate function to describe the purpose of this TLB flush. Is it OK with you?

I see, I did not know but it is indeed called many times. To me zone_sizes_init() is not the right place, what about adding this flush (with a comment) right after sparse_init() in misc_mem_init()? So that if someday we move zone_sizes_init(), we don't lose the flush!

Thanks,

Alex

Quoted 10 lines

Otherwise, you can add:

Thanks,

Alex

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