riscv: asid: switch to alternative way to fix stale TLB entries
Subsystems (3)
cover riscv: asid: switch to alternative way to fix stale TLB entries
Hi all,
Some time ago two different patches have been posted to fix stale TLB entries that caused applications crashes.
The patch [0] suggested 'aggregating' mm_cpumask, i.e. current cpu is not cleared for the switched-out task in switch_mm function. For additional explanations see the commit message by Guo Ren. The same approach is used by arc architecture, so another good comment is for switch_mm in arch/arc/include/asm/mmu_context.h.
The patch [1] attempted to reduce the number of TLB flushes by deferring (and possibly avoiding) them for CPUs not running the task.
Patch [1] has been merged. However we already have two bug reports from different vendors. So apparently something is missing in the approach suggested in [1]. In both cases the patch [0] fixed the issue.
This patch series reverts [1] and replaces it by [0].
Regards,
Sergey
[0] https://lore.kernel.org/linux-riscv/[email protected]/
[1] https://lore.kernel.org/linux-riscv/[email protected]/
Guo Ren (1):
riscv: asid: Fixup stale TLB entry cause application crash
Sergey Matyukevich (1):
Revert "riscv: mm: notify remote harts about mmu cache updates"
arch/riscv/include/asm/mmu.h | 2 -- arch/riscv/include/asm/tlbflush.h | 18 -------------- arch/riscv/mm/context.c | 40 +++++++++++++++---------------- arch/riscv/mm/tlbflush.c | 28 +++++++++++++--------- 4 files changed, 37 insertions(+), 51 deletions(-)
Signature · 7 lines
2.39.2 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH 1/2 Revert "riscv: mm: notify remote harts about mmu cache updates"
From: Sergey Matyukevich <[email protected]>
This reverts the remaining bits of commit 4bd1d80efb5a ("riscv: mm:
notify remote harts harts about mmu cache updates").
According to bug reports, suggested approach to fix stale TLB entries is not sufficient. It needs to be replaced by a more robust solution.
--- arch/riscv/include/asm/mmu.h | 2 -- arch/riscv/include/asm/tlbflush.h | 18 ------------------ arch/riscv/mm/context.c | 10 ---------- arch/riscv/mm/tlbflush.c | 28 +++++++++++++++++----------- 4 files changed, 17 insertions(+), 41 deletions(-)
arch/riscv/include/asm/mmu.h +0 -2
@@ -19,8 +19,6 @@ typedef struct {#ifdef CONFIG_SMP /* A local icache flush is needed before user execution can resume. */ cpumask_t icache_stale_mask; -/* A local tlb flush is needed before user execution can resume. */ -cpumask_t tlb_stale_mask;#endif } mm_context_t;
arch/riscv/include/asm/tlbflush.h +0 -18
@@ -22,24 +22,6 @@ static inline void local_flush_tlb_page(unsigned long addr) {ALT_FLUSH_TLB_PAGE (__asm__ __volatile__ ("sfence.vma %0" : :"r" (addr) :"memory" )); } - -static inline void local_flush_tlb_all_asid (unsigned long asid) -{ - __asm__ __volatile__ ("sfence.vma x0, %0" - : - :"r" (asid) - :"memory" ); -} - -static inline void local_flush_tlb_page_asid (unsigned long addr, -unsigned long asid) -{ - __asm__ __volatile__ ("sfence.vma %0, %1" - : - :"r" (addr),"r" (asid) - :"memory" ); -} -#else /* CONFIG_MMU */ #define local_flush_tlb_all () do { } while (0)#define local_flush_tlb_page (addr) do { } while (0)
arch/riscv/mm/context.c +0 -10
@@ -196,16 +196,6 @@ static void set_mm_asid(struct mm_struct *mm, unsigned int cpu)if (need_flush_tlb)local_flush_tlb_all (); -#ifdef CONFIG_SMP - else { -cpumask_t *mask = &mm->context.tlb_stale_mask; - -if (cpumask_test_cpu (cpu, mask)) { -cpumask_clear_cpu (cpu, mask); -local_flush_tlb_all_asid (cntx & asid_mask); - } - } -#endif }static void set_mm_noasid (struct mm_struct *mm)
arch/riscv/mm/tlbflush.c +17 -11
@@ -5,7 +5,23 @@#include <linux/sched.h> #include <asm/sbi.h> #include <asm/mmu_context.h> -#include <asm/tlbflush.h> + +static inline void local_flush_tlb_all_asid (unsigned long asid) +{ + __asm__ __volatile__ ("sfence.vma x0, %0" + : + :"r" (asid) + :"memory" ); +} + +static inline void local_flush_tlb_page_asid (unsigned long addr, +unsigned long asid) +{ + __asm__ __volatile__ ("sfence.vma %0, %1" + : + :"r" (addr),"r" (asid) + :"memory" ); +}void flush_tlb_all (void ) {
@@ -15,7 +31,6 @@ void flush_tlb_all(void)static void __sbi_tlb_flush_range (struct mm_struct *mm,unsigned long start,unsigned long size,unsigned long stride) { -struct cpumask *pmask = &mm->context.tlb_stale_mask;struct cpumask *cmask =mm_cpumask (mm);unsigned int cpuid;bool broadcast;
@@ -29,15 +44,6 @@ static void __sbi_tlb_flush_range(struct mm_struct *mm, unsigned long start,if (static_branch_unlikely (&use_asid_allocator)) {unsigned long asid =atomic_long_read (&mm->context.id); -/* -* TLB will be immediately flushed on harts concurrently -* executing this MM context. TLB flush on other harts -* is deferred until this MM context migrates there. -*/ -cpumask_setall (pmask); -cpumask_clear_cpu (cpuid, pmask); -cpumask_andnot (pmask, pmask, cmask); -if (broadcast) {sbi_remote_sfence_vma_asid (cmask, start, size, asid); }else if (size <= stride) {
Signature · 7 lines
2.39.2 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Re: [PATCH 1/2] Revert "riscv: mm: notify remote harts about mmu cache updates"
Thx
Quoted 141 lines · Sergey Matyukevich
From: Sergey Matyukevich <[email protected]>
This reverts the remaining bits of commit
4bd1d80efb5a("riscv: mm:
notify remote harts harts about mmu cache updates").According to bug reports, suggested approach to fix stale TLB entries is not sufficient. It needs to be replaced by a more robust solution.
Fixes 4bd1d80efb5a ("riscv: mm: notify remote harts about mmu cache updates")Reported-by Zong Li <[email protected]>Reported-by Lad Prabhakar <[email protected]>Signed-off-by Sergey Matyukevich <[email protected]>--- arch/riscv/include/asm/mmu.h | 2 -- arch/riscv/include/asm/tlbflush.h | 18 ------------------ arch/riscv/mm/context.c | 10 ---------- arch/riscv/mm/tlbflush.c | 28 +++++++++++++++++----------- 4 files changed, 17 insertions(+), 41 deletions(-)arch/riscv/include/asm/mmu.h +0
-2@@ -19,8 +19,6 @@ typedef struct {#ifdef CONFIG_SMP /* A local icache flush is needed before user execution can resume. */ cpumask_t icache_stale_mask; -/* A local tlb flush is needed before user execution can resume. */ -cpumask_t tlb_stale_mask;#endif } mm_context_t;arch/riscv/include/asm/tlbflush.h +0
-18@@ -22,24 +22,6 @@ static inline void local_flush_tlb_page(unsigned long addr) {ALT_FLUSH_TLB_PAGE (__asm__ __volatile__ ("sfence.vma %0" : :"r" (addr) :"memory" )); } - -static inline void local_flush_tlb_all_asid (unsigned long asid) -{ - __asm__ __volatile__ ("sfence.vma x0, %0" - : - :"r" (asid) - :"memory" ); -} - -static inline void local_flush_tlb_page_asid (unsigned long addr, -unsigned long asid) -{ - __asm__ __volatile__ ("sfence.vma %0, %1" - : - :"r" (addr),"r" (asid) - :"memory" ); -} -#else /* CONFIG_MMU */ #define local_flush_tlb_all () do { } while (0)#define local_flush_tlb_page (addr) do { } while (0)arch/riscv/mm/context.c +0
-10@@ -196,16 +196,6 @@ static void set_mm_asid(struct mm_struct *mm, unsigned int cpu)if (need_flush_tlb)local_flush_tlb_all (); -#ifdef CONFIG_SMP - else { -cpumask_t *mask = &mm->context.tlb_stale_mask; - -if (cpumask_test_cpu (cpu, mask)) { -cpumask_clear_cpu (cpu, mask); -local_flush_tlb_all_asid (cntx & asid_mask); - } - } -#endif }static void set_mm_noasid (struct mm_struct *mm)arch/riscv/mm/tlbflush.c +17
-12@@ -5,7 +5,23 @@#include <linux/sched.h> #include <asm/sbi.h> #include <asm/mmu_context.h> -#include <asm/tlbflush.h> + +static inline void local_flush_tlb_all_asid (unsigned long asid) +{ + __asm__ __volatile__ ("sfence.vma x0, %0" + : + :"r" (asid) + :"memory" ); +} + +static inline void local_flush_tlb_page_asid (unsigned long addr, +unsigned long asid) +{ + __asm__ __volatile__ ("sfence.vma %0, %1" + : + :"r" (addr),"r" (asid) + :"memory" ); +}void flush_tlb_all (void ) {@@ -15,7 +31,6 @@ void flush_tlb_all(void)static void __sbi_tlb_flush_range (struct mm_struct *mm,unsigned long start,unsigned long size,unsigned long stride) { -struct cpumask *pmask = &mm->context.tlb_stale_mask;struct cpumask *cmask =mm_cpumask (mm);unsigned int cpuid;bool broadcast;@@ -29,15 +44,6 @@ static void __sbi_tlb_flush_range(struct mm_struct *mm, unsigned long start,if (static_branch_unlikely (&use_asid_allocator)) {unsigned long asid =atomic_long_read (&mm->context.id); -/* -* TLB will be immediately flushed on harts concurrently -* executing this MM context. TLB flush on other harts -* is deferred until this MM context migrates there. -*/ -cpumask_setall (pmask); -cpumask_clear_cpu (cpuid, pmask); -cpumask_andnot (pmask, pmask, cmask); -if (broadcast) {sbi_remote_sfence_vma_asid (cmask, start, size, asid); }else if (size <= stride) { --2.39.2
Signature · 7 lines
Best Regards Guo Ren _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH 2/2 riscv: asid: Fixup stale TLB entry cause application crash
In reply to Sergey MatyukevichFrom: Guo Ren <[email protected]>
After use_asid_allocator is enabled, the userspace application will crash by stale TLB entries. Because only using cpumask_clear_cpu without local_flush_tlb_all couldn't guarantee CPU's TLB entries were fresh. Then set_mm_asid would cause the user space application to get a stale value by stale TLB entry, but set_mm_noasid is okay.
Here is the symptom of the bug:
unhandled signal 11 code 0x1 (coredump)
0x0000003fd6d22524 <+4>: auipc s0,0x70 0x0000003fd6d22528 <+8>: ld s0,-148(s0) # 0x3fd6d92490 => 0x0000003fd6d2252c <+12>: ld a5,0(s0)
(gdb) i r s0
s0 0x8082ed1cc3198b21 0x8082ed1cc3198b21
(gdb) x /2x 0x3fd6d924900x3fd6d92490: 0xd80ac8a8 0x0000003f
The core dump file shows that register s0 is wrong, but the value in memory is correct. Because 'ld s0, -148(s0)' used a stale mapping entry in TLB and got a wrong result from an incorrect physical address.
When the task ran on CPU0, which loaded/speculative-loaded the value of address(0x3fd6d92490), then the first version of the mapping entry was PTWed into CPU0's TLB.
When the task switched from CPU0 to CPU1 (No local_tlb_flush_all here by asid), it happened to write a value on the address (0x3fd6d92490). It caused do_page_fault -> wp_page_copy -> ptep_clear_flush -> ptep_get_and_clear & flush_tlb_page.
The flush_tlb_page used mm_cpumask(mm) to determine which CPUs need TLB flush, but CPU0 had cleared the CPU0's mm_cpumask in the previous switch_mm. So we only flushed the CPU1 TLB and set the second version mapping of the PTE. When the task switched from CPU1 to CPU0 again, CPU0 still used a stale TLB mapping entry which contained a wrong target physical address. It raised a bug when the task happened to read that value.
CPU0 CPU1
- switch 'task' in
- read addr (Fill stale mapping
entry into TLB)
- switch 'task' out (no tlb_flush)
- switch 'task' in (no tlb_flush)
- write addr cause pagefault do_page_fault() (change to new addr mapping) wp_page_copy() ptep_clear_flush() ptep_get_and_clear() & flush_tlb_page() write new value into addr
- switch 'task' out (no tlb_flush)
- switch 'task' in (no tlb_flush)
- read addr again (Use stale
mapping entry in TLB)
get wrong value from old phyical
addr, BUG!
The solution is to keep all CPUs' footmarks of cpumask(mm) in switch_mm, which could guarantee to invalidate all stale TLB entries during TLB flush.
--- arch/riscv/mm/context.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-)
arch/riscv/mm/context.c +20 -10
@@ -205,12 +205,24 @@ static void set_mm_noasid(struct mm_struct *mm)local_flush_tlb_all (); } -static inline void set_mm (struct mm_struct *mm,unsigned int cpu) +static inline void set_mm (struct mm_struct *prev, +struct mm_struct *next,unsigned int cpu) { -if (static_branch_unlikely (&use_asid_allocator)) -set_mm_asid (mm, cpu); -else -set_mm_noasid (mm); +/* +* The mm_cpumask indicates which harts' TLBs contain the virtual +* address mapping of the mm. Compared to noasid, using asid +* can't guarantee that stale TLB entries are invalidated because +* the asid mechanism wouldn't flush TLB for every switch_mm for +* performance. So when using asid, keep all CPUs footmarks in +* cpumask() until mm reset. +*/ +cpumask_set_cpu (cpu,mm_cpumask (next)); +if (static_branch_unlikely (&use_asid_allocator)) { +set_mm_asid (next, cpu); + }else { +cpumask_clear_cpu (cpu,mm_cpumask (prev)); +set_mm_noasid (next); + } }static int __init asids_init (void )
@@ -264,7 +276,8 @@ static int __init asids_init(void) }early_initcall (asids_init);#else -static inline void set_mm (struct mm_struct *mm,unsigned int cpu) +static inline void set_mm (struct mm_struct *prev, +struct mm_struct *next,unsigned int cpu) {/* Nothing to do here when there is no MMU */ }
@@ -317,10 +330,7 @@ void switch_mm(struct mm_struct *prev, struct mm_struct *next,*/ cpu =smp_processor_id (); -cpumask_clear_cpu (cpu,mm_cpumask (prev)); -cpumask_set_cpu (cpu,mm_cpumask (next)); - -set_mm (next, cpu); +set_mm (prev, next, cpu);flush_icache_deferred (next, cpu); }
Signature · 7 lines
2.39.2 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Re: [PATCH 2/2] riscv: asid: Fixup stale TLB entry cause application crash
Quoted 134 lines · Sergey Matyukevich
From: Guo Ren <[email protected]>
After
use_asid_allocatoris enabled, the userspace application will crash by stale TLB entries. Because only usingcpumask_clear_cpuwithoutlocal_flush_tlb_allcouldn't guarantee CPU's TLB entries were fresh. Thenset_mm_asidwould cause the user space application to get a stale value by stale TLB entry, butset_mm_noasidis okay.Here is the symptom of the bug:
unhandled signal 11 code0x1(coredump)0x0000003fd6d22524 <+4>: auipc s0,0x70 0x0000003fd6d22528 <+8>: ld s0,-148(s0) # 0x3fd6d92490 => 0x0000003fd6d2252c <+12>: ld a5,0(s0)(gdb) i r s0
s00x8082ed1cc3198b210x8082ed1cc3198b21
(gdb) x /2x0x3fd6d924900x3fd6d92490:0xd80ac8a80x0000003f
The core dump file shows that register s0 is wrong, but the value in memory is correct. Because 'ld s0, -148(s0)' used a stale mapping entry in TLB and got a wrong result from an incorrect physical address.When the task ran on CPU0, which loaded/speculative-loaded the value of address(0x3fd6d92490), then the first version of the mapping entry was PTWed into CPU0's TLB.
When the task switched from CPU0 to CPU1 (Nolocal_tlb_flush_allhere by asid), it happened to write a value on the address (0x3fd6d92490). It causeddo_page_fault->wp_page_copy->ptep_clear_flush->ptep_get_and_clear&flush_tlb_page.
Theflush_tlb_pageusedmm_cpumask(mm)to determine which CPUs need TLB flush, but CPU0 had cleared the CPU0'smm_cpumaskin the previousswitch_mm. So we only flushed the CPU1 TLB and set the second version mapping of the PTE. When the task switched from CPU1 to CPU0 again, CPU0 still used a stale TLB mapping entry which contained a wrong target physical address. It raised a bug when the task happened to read that value.CPU0 CPU1
- switch 'task' in
- read addr (Fill stale mapping
entry into TLB)
- switch 'task' out (notlb_flush)
- switch 'task' in (notlb_flush)
- write addr cause pagefaultdo_page_fault()(change to new addr mapping)wp_page_copy()ptep_clear_flush()ptep_get_and_clear()&flush_tlb_page()write new value into addr
- switch 'task' out (notlb_flush)
- switch 'task' in (notlb_flush)
- read addr again (Use stale
mapping entry in TLB)
get wrong value from old phyical
addr, BUG!The solution is to keep all CPUs' footmarks of cpumask(mm) in
switch_mm, which could guarantee to invalidate all stale TLB entries during TLB flush.Fixes 65d4b9c53017 ("RISC-V: Implement ASID allocator")Signed-off-by Guo Ren <[email protected]>Signed-off-by Guo Ren <[email protected]>Tested-by Lad Prabhakar <[email protected]>Tested-by Zong Li <[email protected]>Tested-by Sergey Matyukevich <[email protected]>Cc Anup Patel <[email protected]>Cc Palmer Dabbelt <[email protected]>--- arch/riscv/mm/context.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-)arch/riscv/mm/context.c +20
-10@@ -205,12 +205,24 @@ static void set_mm_noasid(struct mm_struct *mm)local_flush_tlb_all (); } -static inline void set_mm (struct mm_struct *mm,unsigned int cpu) +static inline void set_mm (struct mm_struct *prev, +struct mm_struct *next,unsigned int cpu) { -if (static_branch_unlikely (&use_asid_allocator)) -set_mm_asid (mm, cpu); -else -set_mm_noasid (mm); +/* +* The mm_cpumask indicates which harts' TLBs contain the virtual +* address mapping of the mm. Compared to noasid, using asid +* can't guarantee that stale TLB entries are invalidated because +* the asid mechanism wouldn't flush TLB for every switch_mm for +* performance. So when using asid, keep all CPUs footmarks in +* cpumask() until mm reset. +*/ +cpumask_set_cpu (cpu,mm_cpumask (next)); +if (static_branch_unlikely (&use_asid_allocator)) { +set_mm_asid (next, cpu); + }else { +cpumask_clear_cpu (cpu,mm_cpumask (prev)); +set_mm_noasid (next); + } }static int __init asids_init (void )@@ -264,7 +276,8 @@ static int __init asids_init(void) }early_initcall (asids_init);#else -static inline void set_mm (struct mm_struct *mm,unsigned int cpu) +static inline void set_mm (struct mm_struct *prev, +struct mm_struct *next,unsigned int cpu) {/* Nothing to do here when there is no MMU */ }@@ -317,10 +330,7 @@ void switch_mm(struct mm_struct *prev, struct mm_struct *next,*/ cpu =smp_processor_id (); -cpumask_clear_cpu (cpu,mm_cpumask (prev)); -cpumask_set_cpu (cpu,mm_cpumask (next)); - -set_mm (next, cpu); +set_mm (prev, next, cpu);flush_icache_deferred (next, cpu); }Signature · 1 line
2.39.2
This is identical to what I reviewed before, so my r-b could have been kept, anyway here it is again
Thanks,
drew
This series was applied to riscv/linux.git (fixes) by Palmer Dabbelt <[email protected]>:
Hello:
This series was applied to riscv/linux.git (fixes) by Palmer Dabbelt <[email protected]>:
Quoted 5 lines · On Sun, 26 Feb 2023 18:01:35 +0300 you wrote:
Hi all,
Some time ago two different patches have been posted to fix stale TLB entries that caused applications crashes.
Quoted 7 lines
The patch [0] suggested 'aggregating'
mm_cpumask, i.e. current cpu is not cleared for the switched-out task inswitch_mmfunction. For additional explanations see the commit message by Guo Ren. The same approach is used by arc architecture, so another good comment is forswitch_mminarch/arc/include/asm/mmu_context.h.[...]
Here is the summary with links:
- [1/2] Revert "riscv: mm: notify remote harts about mmu cache updates"
https://git.kernel.org/riscv/c/e921050022f1 - [2/2] riscv: asid: Fixup stale TLB entry cause application crash https://git.kernel.org/riscv/c/82dd33fde026
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html