sparc64: Fix race in TLB batch processing.
David Miller
maintainerPATCH 1/1 sparc64: Fix race in TLB batch processing.
As reported by Dave Kleikamp, when we emit cross calls to do batched TLB flush processing we have a race because we do not synchronize on the sibling cpus completing the cross call.
So meanwhile the TLB batch can be reset (tb->tlb_nr set to zero, etc.) and either flushes are missed or flushes will flush the wrong addresses.
Fix this by using generic infrastructure to synchonize on the completion of the cross call.
This first required getting the flush_tlb_pending() call out from switch_to() which operates with locks held and interrupts disabled. The problem is that smp_call_function_many() cannot be invoked with IRQs disabled and this is explicitly checked for with WARN_ON_ONCE().
We get the batch processing outside of locked IRQ disabled sections by using some ideas from the powerpc port. Namely, we only batch inside of arch_{enter,leave}_lazy_mmu_mode() calls. If we're not in such a region, we flush TLBs synchronously.
1) Get rid of xcall_flush_tlb_pending and per-cpu type implementations.
2) Do TLB batch cross calls instead via:
smp_call_function_many() tlb_pending_func() __flush_tlb_pending()
3) Batch only in lazy mmu sequences:
a) Add 'active' member to struct tlb_batch
b) Define __HAVE_ARCH_ENTER_LAZY_MMU_MODE
c) Set 'active' in arch_enter_lazy_mmu_mode()
d) Run batch and clear 'active' in arch_leave_lazy_mmu_mode()
e) Check 'active' in tlb_batch_add_one() and do a synchronous flush if it's clear.
4) Add infrastructure for synchronous TLB page flushes.
a) Implement __flush_tlb_page and per-cpu variants, patch as needed.
b) Likewise for xcall_flush_tlb_page.
c) Implement smp_flush_tlb_page() to invoke the cross-call.
d) Wire up global_flush_tlb_page() to the right routine based upon CONFIG_SMP
5) It turns out that singleton batches are very common, 2 out of every 3 batch flushes have only a single entry in them.
The batch flush waiting is very expensive, both because of the poll on sibling cpu completeion, as well as because passing the tlb batch pointer to the sibling cpus invokes a shared memory dereference.
Therefore, in flush_tlb_pending(), if there is only one entry in the batch perform a completely asynchronous global_flush_tlb_page() instead.
---
This is the final version of the patch which I checked in to the 'sparc' GIT tree. I'll queue this up for -stable too.
arch/sparc/include/asm/pgtable_64.h | 1 + arch/sparc/include/asm/switch_to_64.h | 3 +- arch/sparc/include/asm/tlbflush_64.h | 37 +++++++++-- arch/sparc/kernel/smp_64.c | 41 ++++++++++-- arch/sparc/mm/tlb.c | 38 +++++++++-- arch/sparc/mm/tsb.c | 57 +++++++++++----- arch/sparc/mm/ultra.S | 119 +++++++++++++++++++++++++++------- 7 files changed, 241 insertions(+), 55 deletions(-)
arch/sparc/include/asm/pgtable_64.h +1 -0
@@ -915,6 +915,7 @@ static inline int io_remap_pfn_range(struct vm_area_struct *vma,return remap_pfn_range (vma, from, phys_base >>PAGE_SHIFT , size, prot); } +#include <asm/tlbflush.h> #include <asm-generic/pgtable.h> /* We provide our own get_unmapped_area to cope with VA holes and
arch/sparc/include/asm/switch_to_64.h +1 -2
@@ -18,8 +18,7 @@ do { \* and 2 stores in this critical code path. -DaveM */ #define switch_to(prev, next, last) \ -do { flush_tlb_pending(); \ - save_and_clear_fpu(); \ +do { save_and_clear_fpu(); \ /* If you are tempted to conditionalize the following */ \/* so that ASI is only written if it changes, think again. */ \ __asm__ __volatile__("wr %%g0, %0, %%asi" \
arch/sparc/include/asm/tlbflush_64.h +31 -6
@@ -11,24 +11,40 @@struct tlb_batch {struct mm_struct *mm;unsigned long tlb_nr; +unsigned long active;unsigned long vaddrs[TLB_BATCH_NR ]; };extern void flush_tsb_kernel_range (unsigned long start,unsigned long end);extern void flush_tsb_user (struct tlb_batch *tb); +extern void flush_tsb_user_page (struct mm_struct *mm,unsigned long vaddr);/* TLB flush operations. */ -extern void flush_tlb_pending (void ); +static inline void flush_tlb_mm (struct mm_struct *mm) +{ +} + +static inline void flush_tlb_page (struct vm_area_struct *vma, +unsigned long vmaddr) +{ +} + +static inline void flush_tlb_range (struct vm_area_struct *vma, +unsigned long start,unsigned long end) +{ +} + +#define __HAVE_ARCH_ENTER_LAZY_MMU_MODE -#define flush_tlb_range (vma,start,end) \ - do { (void)(start); flush_tlb_pending(); } while (0) -#define flush_tlb_page (vma,addr) flush_tlb_pending() -#define flush_tlb_mm (mm) flush_tlb_pending() +extern void flush_tlb_pending (void ); +extern void arch_enter_lazy_mmu_mode (void ); +extern void arch_leave_lazy_mmu_mode (void ); +#define arch_flush_lazy_mmu_mode () do {} while (0)/* Local cpu only. */ extern void __flush_tlb_all (void ); - +extern void __flush_tlb_page (unsigned long context,unsigned long vaddr);extern void __flush_tlb_kernel_range (unsigned long start,unsigned long end);#ifndef CONFIG_SMP
@@ -38,15 +54,24 @@ do { flush_tsb_kernel_range(start,end); \__flush_tlb_kernel_range (start,end); \ } while (0) +static inline void global_flush_tlb_page(struct mm_struct *mm, unsigned long vaddr) +{ +__flush_tlb_page (CTX_HWBITS (mm->context), vaddr); +} +#else /* CONFIG_SMP */ extern void smp_flush_tlb_kernel_range (unsigned long start,unsigned long end); +extern void smp_flush_tlb_page (struct mm_struct *mm,unsigned long vaddr);#define flush_tlb_kernel_range (start, end) \ do { flush_tsb_kernel_range(start,end); \ smp_flush_tlb_kernel_range(start, end); \ } while (0) +#define global_flush_tlb_page (mm, vaddr) \ + smp_flush_tlb_page(mm, vaddr) +#endif /* ! CONFIG_SMP */ #endif /* _SPARC64_TLBFLUSH_H */
arch/sparc/kernel/smp_64.c +37 -4
67 lines not on this page · raw message
arch/sparc/mm/tlb.c +34 -4
@@ -24,11 +24,17 @@ static DEFINE_PER_CPU(struct tlb_batch, tlb_batch);void flush_tlb_pending (void ) {struct tlb_batch *tb = &get_cpu_var (tlb_batch); +struct mm_struct *mm = tb->mm; -if (tb->tlb_nr) { -flush_tsb_user (tb); +if (!tb->tlb_nr) + goto out; -if (CTX_VALID (tb->mm->context)) { +flush_tsb_user (tb); + +if (CTX_VALID (mm->context)) { +if (tb->tlb_nr =1 ) { +global_flush_tlb_page (mm, tb->vaddrs[0 ]); + }else {#ifdef CONFIG_SMP smp_flush_tlb_pending (tb->mm, tb->tlb_nr, &tb->vaddrs[0 ]);
@@ -37,12 +43,30 @@ void flush_tlb_pending(void) tb->tlb_nr, &tb->vaddrs[0]);#endif } - tb->tlb_nr =0 ; } + tb->tlb_nr =0 ; + +out:put_cpu_var (tlb_batch); } +void arch_enter_lazy_mmu_mode (void ) +{ +struct tlb_batch *tb = &__get_cpu_var (tlb_batch); + + tb->active =1 ; +} + +void arch_leave_lazy_mmu_mode (void ) +{ +struct tlb_batch *tb = &__get_cpu_var (tlb_batch); + +if (tb->tlb_nr) +flush_tlb_pending (); + tb->active =0 ; +} +static void tlb_batch_add_one (struct mm_struct *mm,unsigned long vaddr,bool exec) {
@@ -60,6 +84,12 @@ static void tlb_batch_add_one(struct mm_struct *mm, unsigned long vaddr, nr =0 ; } +if (!tb->active) { +global_flush_tlb_page (mm, vaddr); +flush_tsb_user_page (mm, vaddr); +return ; + } +if (nr =0 ) tb->mm = mm;
arch/sparc/mm/tsb.c +42 -15
82 lines not on this page · raw message
arch/sparc/mm/ultra.S +95 -24
190 lines not on this page · raw message
Signature · 1 line
1.7.11.7