arm64 switch_mm improvements
Subsystems (3)
cover arm64 switch_mm improvements
Hello,
This series aims to improve out switch_mm implementation and bring the ASID allocator up-to-speed with what we already have for arch/arm/.
In particular, this series:
- Introduces routines for CPU-local I-cache and TLB invalidation, and converts relevant callers to use these in preference to the inner-shareable variants
- Rewrites the ASID allocator to use the bitmap algorithm implemented for arch/arm/ plus the new relaxed atomics introduced with 4.3 (a subsequent patch series will optimise these for arm64).
- Degrades fullm TLB flushing on exit/execve to a NOP, since the ASID allocator will never re-allocate a dirty ASID
- Kills mm_cpumask, as it is no longer used and therefore doesn't need to be kept up-to-date
- Removes some redundant DSB instructions for synchronising page-table updates
Feedback (particularly as a result of testing!) is more than welcome.
Will
--->8
Will Deacon (10):
arm64: mm: remove unused cpu_set_idmap_tcr_t0sz function
arm64: proc: de-scope TLBI operation during cold boot
arm64: flush: use local TLB and I-cache invalidation
arm64: mm: rewrite ASID allocator and MM context-switching code
arm64: tlbflush: remove redundant ASID casts to (unsigned long)
arm64: tlbflush: avoid flushing when fullmm == 1
arm64: switch_mm: simplify mm and CPU checks
arm64: mm: kill mm_cpumask usage
arm64: tlb: remove redundant barrier from __flush_tlb_pgtable
arm64: mm: remove dsb from update_mmu_cache
arch/arm64/include/asm/cacheflush.h | 7 ++ arch/arm64/include/asm/mmu.h | 10 +- arch/arm64/include/asm/mmu_context.h | 113 ++++------------- arch/arm64/include/asm/pgtable.h | 6 +- arch/arm64/include/asm/thread_info.h | 1 - arch/arm64/include/asm/tlb.h | 26 ++-- arch/arm64/include/asm/tlbflush.h | 18 ++- arch/arm64/kernel/asm-offsets.c | 2 +- arch/arm64/kernel/efi.c | 5 +- arch/arm64/kernel/smp.c | 9 +- arch/arm64/kernel/suspend.c | 2 +- arch/arm64/mm/context.c | 236 +++++++++++++++++++++-------------- arch/arm64/mm/mmu.c | 2 +- arch/arm64/mm/proc.S | 6 +- 14 files changed, 217 insertions(+), 226 deletions(-)
Signature · 1 line
2.1.4
PATCH 1/10 arm64: mm: remove unused cpu_set_idmap_tcr_t0sz function
With commit b08d4640a3dc ("arm64: remove dead code"), cpu_set_idmap_tcr_t0sz is no longer called and can therefore be removed from the kernel.
This patch removes the function and effectively inlines the helper function __cpu_set_tcr_t0sz into cpu_set_default_tcr_t0sz.
--- arch/arm64/include/asm/mmu_context.h | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-)
arch/arm64/include/asm/mmu_context.h +12 -23
@@ -77,34 +77,23 @@ static inline bool __cpu_uses_extended_idmap(void) unlikely(idmap_t0sz != TCR_T0SZ(VA_BITS))); } -static inline void __cpu_set_tcr_t0sz (u64 t0sz) -{ -unsigned long tcr; - -if (__cpu_uses_extended_idmap ()) - asmvolatile ( -" mrs %0, tcr_el1 ;" -" bfi %0, %1, %2, %3 ;" -" msr tcr_el1, %0 ;" -" isb" - :"=&r" (tcr) - :"r" (t0sz),"I" (TCR_T0SZ_OFFSET ),"I" (TCR_TxSZ_WIDTH)); -} - -/* -* Set TCR.T0SZ to the value appropriate for activating the identity map. -*/ -static inline void cpu_set_idmap_tcr_t0sz (void ) -{ -__cpu_set_tcr_t0sz (idmap_t0sz); -} -/* * Set TCR.T0SZ to its default value (based on VA_BITS) */ static inline void cpu_set_default_tcr_t0sz (void ) { -__cpu_set_tcr_t0sz (TCR_T0SZ (VA_BITS )); +unsigned long tcr; + +if (!__cpu_uses_extended_idmap ()) +return ; + + asmvolatile ( +" mrs %0, tcr_el1 ;" +" bfi %0, %1, %2, %3 ;" +" msr tcr_el1, %0 ;" +" isb" + :"=&r" (tcr) + :"r" (TCR_T0SZ (VA_BITS )),"I" (TCR_T0SZ_OFFSET ),"I" (TCR_TxSZ_WIDTH)); }static inline void switch_new_context (struct mm_struct *mm)
Signature · 1 line
2.1.4
PATCH 2/10 arm64: proc: de-scope TLBI operation during cold boot
In reply to Will DeaconWhen cold-booting a CPU, we must invalidate any junk entries from the local TLB prior to enabling the MMU. This doesn't require broadcasting within the inner-shareable domain, so de-scope the operation to apply only to the local CPU.
--- arch/arm64/mm/proc.S | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/mm/proc.S +2 -2
@@ -146,8 +146,8 @@ ENDPROC(cpu_do_switch_mm) * value of the SCTLR_EL1 register. */ ENTRY(__cpu_setup) - tlbi vmalle1is // invalidate I + D TLBs - dsb ish + tlbi vmalle1 // Invalidate local TLB + dsb nsh mov x0, #3 << 20 msr cpacr_el1, x0 // Enable FP/ASIMD
Signature · 1 line
2.1.4
PATCH 3/10 arm64: flush: use local TLB and I-cache invalidation
In reply to Will DeaconThere are a number of places where a single CPU is running with a private page-table and we need to perform maintenance on the TLB and I-cache in order to ensure correctness, but do not require the operation to be broadcast to other CPUs.
This patch adds local variants of tlb_flush_all and __flush_icache_all to support these use-cases and updates the callers respectively. __local_flush_icache_all also implies an isb, since it is intended to be used synchronously.
--- arch/arm64/include/asm/cacheflush.h | 7 +++++++ arch/arm64/include/asm/tlbflush.h | 8 ++++++++ arch/arm64/kernel/efi.c | 4 ++-- arch/arm64/kernel/smp.c | 2 +- arch/arm64/kernel/suspend.c | 2 +- arch/arm64/mm/context.c | 4 ++-- arch/arm64/mm/mmu.c | 2 +- 7 files changed, 22 insertions(+), 7 deletions(-)
arch/arm64/include/asm/cacheflush.h +7 -0
@@ -115,6 +115,13 @@ extern void copy_to_user_page(struct vm_area_struct *, struct page *,#define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 1extern void flush_dcache_page (struct page *); +static inline void __local_flush_icache_all (void ) +{ + asm("ic iallu" ); +dsb (nsh); +isb (); +} +static inline void __flush_icache_all (void ) { asm("ic ialluis" );
arch/arm64/include/asm/tlbflush.h +8 -0
@@ -63,6 +63,14 @@* only require the D-TLB to be invalidated. * - kaddr - Kernel virtual memory address */ +static inline void local_flush_tlb_all (void ) +{ +dsb (nshst); + asm("tlbi vmalle1" ); +dsb (nsh); +isb (); +} +static inline void flush_tlb_all (void ) {dsb (ishst);
arch/arm64/kernel/efi.c +2 -2
@@ -343,9 +343,9 @@ static void efi_set_pgd(struct mm_struct *mm)else cpu_switch_mm (mm->pgd, mm); -flush_tlb_all (); +local_flush_tlb_all ();if (icache_is_aivivt ()) -__flush_icache_all (); +__local_flush_icache_all (); }void efi_virtmap_load (void )
arch/arm64/kernel/smp.c +1 -1
@@ -152,7 +152,7 @@ asmlinkage void secondary_start_kernel(void)* point to zero page to avoid speculatively fetching new entries. */ cpu_set_reserved_ttbr0 (); -flush_tlb_all (); +local_flush_tlb_all ();cpu_set_default_tcr_t0sz ();preempt_disable ();
arch/arm64/kernel/suspend.c +1 -1
@@ -90,7 +90,7 @@ int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))else cpu_switch_mm (mm->pgd, mm); -flush_tlb_all (); +local_flush_tlb_all ();/* * Restore per-cpu offset before any kernel
arch/arm64/mm/context.c +2 -2
@@ -48,9 +48,9 @@ static void flush_context(void) {/* set the reserved TTBR0 before flushing the TLB */ cpu_set_reserved_ttbr0 (); -flush_tlb_all (); +local_flush_tlb_all ();if (icache_is_aivivt ()) -__flush_icache_all (); +__local_flush_icache_all (); }static void set_mm_context (struct mm_struct *mm,unsigned int asid)
arch/arm64/mm/mmu.c +1 -1
@@ -456,7 +456,7 @@ void __init paging_init(void)* point to zero page to avoid speculatively fetching new entries. */ cpu_set_reserved_ttbr0 (); -flush_tlb_all (); +local_flush_tlb_all ();cpu_set_default_tcr_t0sz (); }
Signature · 1 line
2.1.4
PATCH 4/10 arm64: mm: rewrite ASID allocator and MM context-switching code
In reply to Will DeaconOur current switch_mm implementation suffers from a number of problems:
(1) The ASID allocator relies on IPIs to synchronise the CPUs on a rollover event
(2) Because of (1), we cannot allocate ASIDs with interrupts disabled and therefore make use of a TIF_SWITCH_MM flag to postpone the actual switch to finish_arch_post_lock_switch
(3) We run context switch with a reserved (invalid) TTBR0 value, even though the ASID and pgd are updated atomically
(4) We take a global spinlock (cpu_asid_lock) during context-switch
(5) We use h/w broadcast TLB operations when they are not required (e.g. in flush_context)
This patch addresses these problems by rewriting the ASID algorithm to match the bitmap-based arch/arm/ implementation more closely. This in turn allows us to remove much of the complications surrounding switch_mm, including the ugly thread flag.
--- arch/arm64/include/asm/mmu.h | 10 +- arch/arm64/include/asm/mmu_context.h | 76 ++--------- arch/arm64/include/asm/thread_info.h | 1 - arch/arm64/kernel/asm-offsets.c | 2 +- arch/arm64/kernel/efi.c | 1 - arch/arm64/mm/context.c | 238 +++++++++++++++++++++-------------- arch/arm64/mm/proc.S | 2 +- 7 files changed, 161 insertions(+), 169 deletions(-)
arch/arm64/include/asm/mmu.h +3 -7
@@ -17,15 +17,11 @@#define __ASM_MMU_Htypedef struct { -unsigned int id; -raw_spinlock_t id_lock; -void *vdso; +atomic64_t id; +void *vdso; }mm_context_t ; -#define INIT_MM_CONTEXT (name) \ - .context.id_lock = __RAW_SPIN_LOCK_UNLOCKED(name.context.id_lock), - -#define ASID (mm) ((mm)->context.id & 0xffff) +#define ASID (mm) ((mm)->context.id.counter & 0xffff)extern void paging_init (void );extern void __iomem *early_io_map (phys_addr_t phys,unsigned long virt);
arch/arm64/include/asm/mmu_context.h +11 -65
90 lines not on this page · raw message
arch/arm64/include/asm/thread_info.h +0 -1
@@ -111,7 +111,6 @@ static inline struct thread_info *current_thread_info(void)#define TIF_RESTORE_SIGMASK 20#define TIF_SINGLESTEP 21#define TIF_32BIT 22/* 32bit process */ -#define TIF_SWITCH_MM 23/* deferred switch_mm */ #define _TIF_SIGPENDING (1 << TIF_SIGPENDING)#define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
arch/arm64/kernel/asm-offsets.c +1 -1
@@ -60,7 +60,7 @@ int main(void)DEFINE (S_SYSCALLNO , offsetof(struct pt_regs , syscallno));DEFINE (S_FRAME_SIZE ,sizeof (struct pt_regs ));BLANK (); - DEFINE(MM_CONTEXT_ID, offsetof(struct mm_struct, context.id)); + DEFINE(MM_CONTEXT_ID, offsetof(struct mm_struct, context.id.counter));BLANK ();DEFINE (VMA_VM_MM , offsetof(struct vm_area_struct , vm_mm));DEFINE (VMA_VM_FLAGS , offsetof(struct vm_area_struct , vm_flags));
arch/arm64/kernel/efi.c +0 -1
@@ -48,7 +48,6 @@ static struct mm_struct efi_mm = { .mmap_sem = __RWSEM_INITIALIZER(efi_mm.mmap_sem), .page_table_lock = __SPIN_LOCK_UNLOCKED(efi_mm.page_table_lock), .mmlist = LIST_HEAD_INIT(efi_mm.mmlist), - INIT_MM_CONTEXT(efi_mm) };static int uefi_debug__initdata ;
arch/arm64/mm/context.c +145 -93
280 lines not on this page · raw message
arch/arm64/mm/proc.S +1 -1
@@ -130,7 +130,7 @@ ENDPROC(cpu_do_resume) * - pgd_phys - physical address of new TTB */ ENTRY(cpu_do_switch_mm) - mmid w1, x1 // get mm->context.id + mmid x1, x1 // get mm->context.id bfi x0, x1, #48, #16 // set the ASID msr ttbr0_el1, x0 // set TTBR0 isb
Signature · 1 line
2.1.4
PATCH 5/10 arm64: tlbflush: remove redundant ASID casts to (unsigned long)
In reply to Will DeaconThe ASID macro returns a 64-bit (long long) value, so there is no need to cast to (unsigned long) before shifting prior to a TLBI operation.
--- arch/arm64/include/asm/tlbflush.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
arch/arm64/include/asm/tlbflush.h +4 -5
@@ -81,7 +81,7 @@ static inline void flush_tlb_all(void)static inline void flush_tlb_mm (struct mm_struct *mm) { -unsigned long asid = (unsigned long )ASID (mm) <<48 ; +unsigned long asid =ASID (mm) <<48 ;dsb (ishst); asm("tlbi aside1is, %0" : :"r" (asid));
@@ -91,8 +91,7 @@ static inline void flush_tlb_mm(struct mm_struct *mm)static inline void flush_tlb_page (struct vm_area_struct *vma,unsigned long uaddr) { -unsigned long addr = uaddr >>12 | - ((unsigned long )ASID (vma->vm_mm) <<48 ); +unsigned long addr = uaddr >>12 | (ASID (vma->vm_mm) <<48 );dsb (ishst); asm("tlbi vale1is, %0" : :"r" (addr));
@@ -109,7 +108,7 @@ static inline void __flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end, bool last_level) { - unsigned long asid = (unsigned long)ASID(vma->vm_mm) << 48; + unsigned long asid = ASID(vma->vm_mm) << 48;unsigned long addr;if ((end - start) >MAX_TLB_RANGE ) {
@@ -162,7 +161,7 @@ static inline void flush_tlb_kernel_range(unsigned long start, unsigned long endstatic inline void __flush_tlb_pgtable (struct mm_struct *mm,unsigned long uaddr) { -unsigned long addr = uaddr >>12 | ((unsigned long )ASID (mm) <<48 ); +unsigned long addr = uaddr >>12 | (ASID (mm) <<48 );dsb (ishst); asm("tlbi vae1is, %0" : :"r" (addr));
Signature · 1 line
2.1.4
PATCH 6/10 arm64: tlbflush: avoid flushing when fullmm == 1
In reply to Will DeaconThe TLB gather code sets fullmm=1 when tearing down the entire address space for an mm_struct on exit or execve. Given that the ASID allocator will never re-allocate a dirty ASID, this flushing is not needed and can simply be avoided in the flushing code.
--- arch/arm64/include/asm/tlb.h | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-)
arch/arm64/include/asm/tlb.h +15 -11
@@ -37,17 +37,21 @@ static inline void __tlb_remove_table(void *_table)static inline void tlb_flush (struct mmu_gather *tlb) { -if (tlb->fullmm) { -flush_tlb_mm (tlb->mm); - }else { -struct vm_area_struct vma = { .vm_mm = tlb->mm, }; -/* -* The intermediate page table levels are already handled by -* the __(pte|pmd|pud)_free_tlb() functions, so last level -* TLBI is sufficient here. -*/ -__flush_tlb_range (&vma, tlb->start, tlb->end, true); - } +struct vm_area_struct vma = { .vm_mm = tlb->mm, }; + +/* +* The ASID allocator will either invalidate the ASID or mark +* it as used. +*/ +if (tlb->fullmm) +return ; + +/* +* The intermediate page table levels are already handled by +* the __(pte|pmd|pud)_free_tlb() functions, so last level +* TLBI is sufficient here. +*/ +__flush_tlb_range (&vma, tlb->start, tlb->end, true); } static inline void __pte_free_tlb(struct mmu_gather *tlb, pgtable_t pte,
Signature · 1 line
2.1.4
PATCH 7/10 arm64: switch_mm: simplify mm and CPU checks
In reply to Will Deaconswitch_mm performs some checks to try and avoid entering the ASID allocator:
(1) If we're switching to the init_mm (no user mappings), then simply set a reserved TTBR0 value with no page table (the zero page)
(2) If prev == next *and* the mm_cpumask indicates that we've run on this CPU before, then we can skip the allocator.
However, there is plenty of redundancy here. With the new ASID allocator, if prev == next, then we know that our ASID is valid and do not need to worry about re-allocation. Consequently, we can drop the mm_cpumask check in (2) and move the prev == next check before the init_mm check, since if prev == next == init_mm then there's nothing to do.
--- arch/arm64/include/asm/mmu_context.h | 6 ++++-- arch/arm64/mm/context.c | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-)
arch/arm64/include/asm/mmu_context.h +4 -2
@@ -129,6 +129,9 @@ switch_mm(struct mm_struct *prev, struct mm_struct *next, {unsigned int cpu =smp_processor_id (); +if (prev == next) +return ; +/* * init_mm.pgd does not contain any user mappings and it is always * active for kernel addresses in TTBR1. Just set the reserved TTBR0.
@@ -138,8 +141,7 @@ switch_mm(struct mm_struct *prev, struct mm_struct *next,return ; } -if (!cpumask_test_and_set_cpu (cpu,mm_cpumask (next)) || prev != next) -check_and_switch_context (next, tsk); +check_and_switch_context (next, cpu); }#define deactivate_mm (tsk,mm) do { } while (0)
arch/arm64/mm/context.c +1 -1
@@ -166,10 +166,10 @@ void check_and_switch_context(struct mm_struct *mm, unsigned int cpu)local_flush_tlb_all ();atomic64_set (&per_cpu (active_asids, cpu), asid); -cpumask_set_cpu (cpu,mm_cpumask (mm));raw_spin_unlock_irqrestore (&cpu_asid_lock, flags); switch_mm_fastpath: +cpumask_set_cpu (cpu,mm_cpumask (mm));cpu_switch_mm (mm->pgd, mm); }
Signature · 1 line
2.1.4
PATCH 8/10 arm64: mm: kill mm_cpumask usage
In reply to Will Deaconmm_cpumask isn't actually used for anything on arm64, so remove all the code trying to keep it up-to-date.
--- arch/arm64/kernel/smp.c | 7 ------- arch/arm64/mm/context.c | 2 -- 2 files changed, 9 deletions(-)
arch/arm64/kernel/smp.c +0 -7
@@ -142,7 +142,6 @@ asmlinkage void secondary_start_kernel(void)*/ atomic_inc (&mm->mm_count); current->active_mm = mm; -cpumask_set_cpu (cpu,mm_cpumask (mm));set_my_cpu_offset (per_cpu_offset (smp_processor_id ()));printk ("CPU%u: Booted secondary processor\n" , cpu);
@@ -233,12 +232,6 @@ int __cpu_disable(void)* OK - migrate IRQs away from this CPU */ migrate_irqs (); - -/* -* Remove this CPU from the vm mask set of all processes. -*/ -clear_tasks_mm_cpumask (cpu); -return 0 ; }
arch/arm64/mm/context.c +0 -2
@@ -132,7 +132,6 @@ set_asid: bump_gen: asid |= generation; -cpumask_clear (mm_cpumask (mm));return asid; }
@@ -169,7 +168,6 @@ void check_and_switch_context(struct mm_struct *mm, unsigned int cpu)raw_spin_unlock_irqrestore (&cpu_asid_lock, flags); switch_mm_fastpath: -cpumask_set_cpu (cpu,mm_cpumask (mm));cpu_switch_mm (mm->pgd, mm); }
Signature · 1 line
2.1.4
PATCH 9/10 arm64: tlb: remove redundant barrier from __flush_tlb_pgtable
In reply to Will Deacon__flush_tlb_pgtable is used to invalidate intermediate page table entries after they have been cleared and are about to be freed. Since pXd_clear imply memory barriers, we don't need the extra one here.
--- arch/arm64/include/asm/tlbflush.h | 1 - 1 file changed, 1 deletion(-)
arch/arm64/include/asm/tlbflush.h +0 -1
@@ -163,7 +163,6 @@ static inline void __flush_tlb_pgtable(struct mm_struct *mm, {unsigned long addr = uaddr >>12 | (ASID (mm) <<48 ); -dsb (ishst); asm("tlbi vae1is, %0" : :"r" (addr));dsb (ish); }
Signature · 1 line
2.1.4
PATCH 10/10 arm64: mm: remove dsb from update_mmu_cache
In reply to Will Deaconupdate_mmu_cache() consists of a dsb(ishst) instruction so that new user mappings are guaranteed to be visible to the page table walker on exception return.
In reality this can be a very expensive operation which is rarely needed. Removing this barrier shows a modest improvement in hackbench scores and , in the worst case, we re-take the user fault and establish that there was nothing to do.
--- arch/arm64/include/asm/pgtable.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/include/asm/pgtable.h +3 -3
@@ -650,10 +650,10 @@ static inline void update_mmu_cache(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep) {/* -* set_pte() does not have a DSB for user mappings, so make sure that -* the page table write is visible. +* We don't do anything here, so there's a very small chance of +* us retaking a user fault which we just fixed up. The alternative +* is doing a dsb(ishst), but that penalises the fastpath. */ -dsb (ishst ); }#define update_mmu_cache_pmd (vma, address, pmd) do { } while (0)
Signature · 1 line
2.1.4
Catalin Marinas
maintainerPATCH 4/10 arm64: mm: rewrite ASID allocator and MM context-switching code
In reply to Will DeaconQuoted 43 lines · Will Deacon
Our current
switch_mmimplementation suffers from a number of problems:(1) The ASID allocator relies on IPIs to synchronise the CPUs on a rollover event
(2) Because of (1), we cannot allocate ASIDs with interrupts disabled and therefore make use of a
TIF_SWITCH_MMflag to postpone the actual switch tofinish_arch_post_lock_switch(3) We run context switch with a reserved (invalid) TTBR0 value, even though the ASID and pgd are updated atomically
(4) We take a global spinlock (
cpu_asid_lock) during context-switch(5) We use h/w broadcast TLB operations when they are not required (e.g. in
flush_context)This patch addresses these problems by rewriting the ASID algorithm to match the bitmap-based
arch/arm/implementation more closely. This in turn allows us to remove much of the complications surroundingswitch_mm, including the ugly thread flag.Signed-off-by Will Deacon <[email protected]>--- arch/arm64/include/asm/mmu.h | 10 +- arch/arm64/include/asm/mmu_context.h | 76 ++--------- arch/arm64/include/asm/thread_info.h | 1 - arch/arm64/kernel/asm-offsets.c | 2 +- arch/arm64/kernel/efi.c | 1 - arch/arm64/mm/context.c | 238 +++++++++++++++++++++-------------- arch/arm64/mm/proc.S | 2 +- 7 files changed, 161 insertions(+), 169 deletions(-)arch/arm64/include/asm/mmu.h +0
-2@@ -17,15 +17,11 @@#define __ASM_MMU_H typedef struct { - unsigned int id; - raw_spinlock_t id_lock;
arch/arm64/include/asm/mmu.h line 24
Quoted 10 lines
arch/arm64/include/asm/mmu.h +3
-5-void *vdso; +atomic64_t id; +void *vdso; } mm_context_t; -#define INIT_MM_CONTEXT (name) \ - .context.id_lock = __RAW_SPIN_LOCK_UNLOCKED(name.context.id_lock), - -#define ASID (mm) ((mm)->context.id & 0xffff) +#define ASID (mm) ((mm)->context.id.counter & 0xffff)
If you changed the id to atomic64_t, can you not use atomic64_read() here?
Quoted 33 lines
arch/arm64/mm/context.c +8
-9@@ -17,135 +17,187 @@* along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include <linux/init.h> +#include <linux/bitops.h> #include <linux/sched.h> +#include <linux/slab.h> #include <linux/mm.h> -#include <linux/smp.h> -#include <linux/percpu.h> +#include <asm/cpufeature.h> #include <asm/mmu_context.h> #include <asm/tlbflush.h> -#include <asm/cachetype.h> -#define asid_bits (reg) \ - (((read_cpuid(ID_AA64MMFR0_EL1) & 0xf0) >> 2) + 8) +static u32 asid_bits; +static DEFINE_RAW_SPINLOCK (cpu_asid_lock); -#define ASID_FIRST_VERSION (1 << MAX_ASID_BITS) +static atomic64_t asid_generation; +static unsigned long *asid_map; -static DEFINE_RAW_SPINLOCK (cpu_asid_lock); -unsigned int cpu_last_asid =ASID_FIRST_VERSION ; +static DEFINE_PER_CPU(atomic64_t, active_asids);
arch/arm64/mm/context.c line 41
Quoted 10 lines
arch/arm64/mm/context.c +5
-4+static DEFINE_PER_CPU(u64, reserved_asids); +static cpumask_t tlb_flush_pending; -/* -* We fork()ed a process, and we need a new context for the child to run in. -*/ -void __init_new_context (struct task_struct *tsk,struct mm_struct *mm) +#define ASID_MASK (~GENMASK(asid_bits - 1, 0)) +#define ASID_FIRST_VERSION (1UL << asid_bits) +#define NUM_USER_ASIDS ASID_FIRST_VERSION
Apart from NUM_USER_ASIDS, I think we can live with constants for ASID_MASK and ASID_FIRST_VERSION (as per 16-bit ASIDs, together with some shifts converted to a constant), marginally more optimal code generation which avoids reading asid_bits all the time. We should be ok with 48-bit generation field.
Quoted 37 lines
arch/arm64/mm/context.c +33
-2+static void flush_context (unsigned int cpu) { - mm->context.id =0 ; -raw_spin_lock_init (&mm->context.id_lock); +int i; +u64 asid; + +/* Update the list of reserved ASIDs and the ASID bitmap. */ +bitmap_clear (asid_map,0 ,NUM_USER_ASIDS ); + +/* +* Ensure the generation bump is observed before we xchg the +* active_asids. +*/ +smp_wmb (); + +for_each_possible_cpu (i) { + asid =atomic64_xchg_relaxed (&per_cpu (active_asids, i),0 ); +/* +* If this CPU has already been through a +* rollover, but hasn't run another task in +* the meantime, we must preserve its reserved +* ASID, as this is the only trace we have of +* the process it is still running. +*/ +if (asid ==0 ) + asid =per_cpu (reserved_asids, i); +__set_bit (asid & ~ASID_MASK , asid_map); +per_cpu (reserved_asids, i) = asid; + } + +/* Queue a TLB invalidate and flush the I-cache if necessary. */ +cpumask_setall (&tlb_flush_pending); + +if (icache_is_aivivt ()) +__flush_icache_all (); }
[...]
Quoted 15 lines
arch/arm64/mm/context.c +6
-6+void check_and_switch_context (struct mm_struct *mm,unsigned int cpu) { -unsigned int asid; -unsigned int cpu =smp_processor_id (); -struct mm_struct *mm = current->active_mm; +unsigned long flags; +u64 asid; + + asid =atomic64_read (&mm->context.id);/* -* current->active_mm could be init_mm for the idle thread immediately -* after secondary CPU boot or hotplug. TTBR0_EL1 is already set to -* the reserved value, so no need to reset any context. +* The memory ordering here is subtle. We rely on the control
arch/arm64/mm/context.c line 155
Quoted 10 lines
arch/arm64/mm/context.c +7
-2+* dependency between the generation read and the update of +* active_asids to ensure that we are synchronised with a +* parallel rollover (i.e. this pairs with the smp_wmb() in +* flush_context). */ -if (mm == &init_mm) -return ; +if (!((asid ^atomic64_read (&asid_generation)) >> asid_bits) + &&atomic64_xchg_relaxed (&per_cpu (active_asids, cpu), asid)) + goto switch_mm_fastpath;
Just trying to make sense of this ;). At a parallel roll-over, we have two cases for the asid check above: it either (1) sees the new generation or (2) the old one.
(1) is simple since it falls back on the slow path.
(2) means that it goes on and performs an atomic64_xchg. This may happen before or after the active_asids xchg in flush_context(). We now have two sub-cases:
a) if the code above sees the updated (in flush_context()) active_asids, it falls back on the slow path since xchg returns 0. Here we are guaranteed that another read of asid_generation returns the new value (by the smp_wmb() in flush_context).
b) the code above sees the old active_asids, goes to the fast path just like a roll-over hasn't happened (on this CPU). On the CPU doing the roll-over, we want the active_asids xchg to see the new asid. That's guaranteed by the atomicity of the xchg implementation (otherwise it would be case (a) above).
So what the control dependency actually buys us is that a store (exclusive) is not architecturally visible if the generation check fails. I guess this only works (with respect to the load) because of the exclusiveness of the memory accesses.
Quoted 13 lines
arch/arm64/mm/context.c +9
-2+raw_spin_lock_irqsave (&cpu_asid_lock, flags); +/* Check that our ASID belongs to the current generation. */ + asid =atomic64_read (&mm->context.id); +if ((asid ^atomic64_read (&asid_generation)) >> asid_bits) { + asid =new_context (mm, cpu); +atomic64_set (&mm->context.id, asid); + } -smp_rmb (); - asid = cpu_last_asid + cpu; +if (cpumask_test_and_clear_cpu (cpu, &tlb_flush_pending)) +local_flush_tlb_all ();
arch/arm64/mm/context.c line 174
Quoted 10 lines
arch/arm64/mm/context.c +4
-3-flush_context (); -set_mm_context (mm, asid); +atomic64_set (&per_cpu (active_asids, cpu), asid); +cpumask_set_cpu (cpu,mm_cpumask (mm)); +raw_spin_unlock_irqrestore (&cpu_asid_lock, flags); -/* set the new ASID */ +switch_mm_fastpath:cpu_switch_mm (mm->pgd, mm); }
And on the slow path, races with roll-overs on other CPUs are serialised by cpu_asid_lock.
Signature · 1 line
Catalin
Catalin Marinas
maintainerPATCH 6/10 arm64: tlbflush: avoid flushing when fullmm == 1
In reply to Will DeaconQuoted 27 lines · Will Deacon
The TLB gather code sets fullmm=1 when tearing down the entire address space for an
mm_structon exit or execve. Given that the ASID allocator will never re-allocate a dirty ASID, this flushing is not needed and can simply be avoided in the flushing code.Signed-off-by Will Deacon <[email protected]>--- arch/arm64/include/asm/tlb.h | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-)arch/arm64/include/asm/tlb.h +0
-9@@ -37,17 +37,21 @@ static inline void __tlb_remove_table(void *_table)static inline void tlb_flush (struct mmu_gather *tlb) { -if (tlb->fullmm) { -flush_tlb_mm (tlb->mm); - }else { -struct vm_area_struct vma = { .vm_mm = tlb->mm, }; -/* -* The intermediate page table levels are already handled by -* the __(pte|pmd|pud)_free_tlb() functions, so last level -* TLBI is sufficient here. -*/
arch/arm64/include/asm/tlb.h static inline void __tlb_remove_table(void *_table) · line 47
Quoted 10 lines
arch/arm64/include/asm/tlb.h +8
-2-__flush_tlb_range (&vma, tlb->start, tlb->end, true); - } +struct vm_area_struct vma = { .vm_mm = tlb->mm, }; + +/* +* The ASID allocator will either invalidate the ASID or mark +* it as used. +*/ +if (tlb->fullmm) +return ;
BTW, do we actually need this flush_tlb_mm() with the current ASID allocator? It doesn't reuse old ASIDs either before a full TLBI (just trying to remember if we had any logic; or maybe it was needed before non-lazy __pte_free_tlb).
Signature · 1 line
Catalin
Catalin Marinas
maintainercover arm64 switch_mm improvements
In reply to Will DeaconQuoted 11 lines · Will Deacon
Will Deacon (10):
arm64: mm: remove unusedcpu_set_idmap_tcr_t0szfunction
arm64: proc: de-scope TLBI operation during cold boot
arm64: flush: use local TLB and I-cache invalidation
arm64: mm: rewrite ASID allocator and MM context-switching code
arm64: tlbflush: remove redundant ASID casts to (unsigned long)
arm64: tlbflush: avoid flushing when fullmm == 1
arm64:switch_mm: simplify mm and CPU checks
arm64: mm: killmm_cpumaskusage
arm64: tlb: remove redundant barrier from__flush_tlb_pgtable
arm64: mm: remove dsb fromupdate_mmu_cache
Apart from some minor comments on patch 4, the series looks fine to me.
I'll do some more tests in the meantime.
Signature · 1 line
Catalin
PATCH 4/10 arm64: mm: rewrite ASID allocator and MM context-switching code
In reply to Catalin MarinasHi Catalin,
Quoted 16 lines · Catalin Marinas
Quoted 15 lines · Will Deacon
arch/arm64/include/asm/mmu.h +2
-3@@ -17,15 +17,11 @@#define __ASM_MMU_Htypedef struct { -unsigned int id; -raw_spinlock_t id_lock; -void *vdso; +atomic64_t id; +void *vdso; }mm_context_t ;
Quoted 8 lines
arch/arm64/include/asm/mmu.h line 24
Quoted 5 lines
arch/arm64/include/asm/mmu.h +1
-4-#define INIT_MM_CONTEXT (name) \ - .context.id_lock = __RAW_SPIN_LOCK_UNLOCKED(name.context.id_lock), - -#define ASID (mm) ((mm)->context.id & 0xffff) +#define ASID (mm) ((mm)->context.id.counter & 0xffff)If you changed the id to
atomic64_t, can you not useatomic64_read()here?
I could, but it forces the access to be volatile which I don't think is necessary for any of the users of this macro (i.e. the tlbflushing code).
Quoted 15 lines
Quoted 15 lines
arch/arm64/include/asm/mmu.h +7
-5-#define asid_bits (reg) \ - (((read_cpuid(ID_AA64MMFR0_EL1) & 0xf0) >> 2) + 8) +static u32 asid_bits; +static DEFINE_RAW_SPINLOCK (cpu_asid_lock); -#define ASID_FIRST_VERSION (1 << MAX_ASID_BITS) +static atomic64_t asid_generation; +static unsigned long *asid_map; -static DEFINE_RAW_SPINLOCK (cpu_asid_lock); -unsigned int cpu_last_asid =ASID_FIRST_VERSION ; +static DEFINE_PER_CPU(atomic64_t, active_asids); +static DEFINE_PER_CPU(u64, reserved_asids); +static cpumask_t tlb_flush_pending;
Quoted 13 lines
arch/arm64/include/asm/mmu.h
Quoted 7 lines
arch/arm64/include/asm/mmu.h +3
-4-/* -* We fork()ed a process, and we need a new context for the child to run in. -*/ -void __init_new_context (struct task_struct *tsk,struct mm_struct *mm) +#define ASID_MASK (~GENMASK(asid_bits - 1, 0)) +#define ASID_FIRST_VERSION (1UL << asid_bits) +#define NUM_USER_ASIDS ASID_FIRST_VERSIONApart from
NUM_USER_ASIDS, I think we can live with constants forASID_MASKandASID_FIRST_VERSION(as per 16-bit ASIDs, together with some shifts converted to a constant), marginally more optimal code generation which avoids readingasid_bitsall the time. We should be ok with 48-bit generation field.
The main reason for writing it like this is that it's easy to test the code with different asid sizes -- you just change asid_bits and all of the masks change accordingly. If we hardcode ASID_MASK then we'll break flush_context (which uses it to generate a bitmap index) and, given that ASID_MASK and ASID_FIRST_VERSION are only used on the slow-path, I'd favour the current code over a micro-optimisation.
Quoted 47 lines
Quoted 15 lines
arch/arm64/include/asm/mmu.h +6
-6+void check_and_switch_context (struct mm_struct *mm,unsigned int cpu) { -unsigned int asid; -unsigned int cpu =smp_processor_id (); -struct mm_struct *mm = current->active_mm; +unsigned long flags; +u64 asid; + + asid =atomic64_read (&mm->context.id);/* -* current->active_mm could be init_mm for the idle thread immediately -* after secondary CPU boot or hotplug. TTBR0_EL1 is already set to -* the reserved value, so no need to reset any context. +* The memory ordering here is subtle. We rely on the control arch/arm64/include/asm/mmu.h
Quoted 10 lines
arch/arm64/include/asm/mmu.h +7
-2+* dependency between the generation read and the update of +* active_asids to ensure that we are synchronised with a +* parallel rollover (i.e. this pairs with the smp_wmb() in +* flush_context). */ -if (mm == &init_mm) -return ; +if (!((asid ^atomic64_read (&asid_generation)) >> asid_bits) + &&atomic64_xchg_relaxed (&per_cpu (active_asids, cpu), asid)) + goto switch_mm_fastpath;Just trying to make sense of this ;). At a parallel roll-over, we have two cases for the asid check above: it either (1) sees the new generation or (2) the old one.
(1) is simple since it falls back on the slow path.
(2) means that it goes on and performs an
atomic64_xchg. This may happen before or after theactive_asidsxchg inflush_context(). We now have two sub-cases:a) if the code above sees the updated (in
flush_context())active_asids, it falls back on the slow path since xchg returns 0. Here we are guaranteed that another read ofasid_generationreturns the new value (by thesmp_wmb()inflush_context).b) the code above sees the old
active_asids, goes to the fast path just like a roll-over hasn't happened (on this CPU). On the CPU doing the roll-over, we want theactive_asidsxchg to see the new asid. That's guaranteed by the atomicity of the xchg implementation (otherwise it would be case (a) above).
Quoted 4 lines
So what the control dependency actually buys us is that a store (exclusive) is not architecturally visible if the generation check fails. I guess this only works (with respect to the load) because of the exclusiveness of the memory accesses.
This is also the case for non-exclusive stores (i.e. a control dependency from a load to a store creates order) since we don't permit speculative writes. So here, the control dependency is between the atomic64_read of the generation and the store-exclusive part of the xchg. The exclusiveness then guarantees that we replay the load-exclusive part of the xchg in the face of contention (due to a parallel rollover).
You seem to have the gist of it, though.
Will
PATCH 6/10 arm64: tlbflush: avoid flushing when fullmm == 1
In reply to Catalin MarinasQuoted 35 lines · Catalin Marinas
Quoted 34 lines · Will Deacon
The TLB gather code sets fullmm=1 when tearing down the entire address space for an
mm_structon exit or execve. Given that the ASID allocator will never re-allocate a dirty ASID, this flushing is not needed and can simply be avoided in the flushing code.Signed-off-by Will Deacon <[email protected]>--- arch/arm64/include/asm/tlb.h | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-)arch/arm64/include/asm/tlb.h +5
-11@@ -37,17 +37,21 @@ static inline void __tlb_remove_table(void *_table)static inline void tlb_flush (struct mmu_gather *tlb) { -if (tlb->fullmm) { -flush_tlb_mm (tlb->mm); - }else { -struct vm_area_struct vma = { .vm_mm = tlb->mm, }; -/* -* The intermediate page table levels are already handled by -* the __(pte|pmd|pud)_free_tlb() functions, so last level -* TLBI is sufficient here. -*/ -__flush_tlb_range (&vma, tlb->start, tlb->end, true); - } +struct vm_area_struct vma = { .vm_mm = tlb->mm, }; + +/* +* The ASID allocator will either invalidate the ASID or mark +* it as used.
Quoted 8 lines
arch/arm64/include/asm/tlb.h static inline void __tlb_remove_table(void *_table) · line 47
Quoted 3 lines
arch/arm64/include/asm/tlb.h +3
-0+*/ +if (tlb->fullmm) +return ;BTW, do we actually need this
flush_tlb_mm()with the current ASID allocator? It doesn't reuse old ASIDs either before a full TLBI (just trying to remember if we had any logic; or maybe it was needed before non-lazy__pte_free_tlb).
I'm afraid I don't follow you here. This diff is removing the flush_tlb_mm because, as you point out, it's no longer needed.
What am I missing?
Will
Catalin Marinas
maintainerPATCH 4/10 arm64: mm: rewrite ASID allocator and MM context-switching code
In reply to Will DeaconQuoted 26 lines · Will Deacon
Quoted 16 lines · Catalin Marinas
Quoted 15 lines · Will Deacon
arch/arm64/include/asm/mmu.h +2
-3@@ -17,15 +17,11 @@#define __ASM_MMU_Htypedef struct { -unsigned int id; -raw_spinlock_t id_lock; -void *vdso; +atomic64_t id; +void *vdso; }mm_context_t ;Quoted 8 lines
arch/arm64/include/asm/mmu.h line 24
Quoted 5 lines
arch/arm64/include/asm/mmu.h +1
-4-#define INIT_MM_CONTEXT (name) \ - .context.id_lock = __RAW_SPIN_LOCK_UNLOCKED(name.context.id_lock), - -#define ASID (mm) ((mm)->context.id & 0xffff) +#define ASID (mm) ((mm)->context.id.counter & 0xffff)If you changed the id to
atomic64_t, can you not useatomic64_read()here?
Quoted 2 lines
I could, but it forces the access to be volatile which I don't think is necessary for any of the users of this macro (i.e. the tlbflushing code).
OK. But please add a small comment (it can be a separate patch, up to you).
Quoted 29 lines
Quoted 15 lines
Quoted 15 lines
arch/arm64/include/asm/mmu.h +7
-5-#define asid_bits (reg) \ - (((read_cpuid(ID_AA64MMFR0_EL1) & 0xf0) >> 2) + 8) +static u32 asid_bits; +static DEFINE_RAW_SPINLOCK (cpu_asid_lock); -#define ASID_FIRST_VERSION (1 << MAX_ASID_BITS) +static atomic64_t asid_generation; +static unsigned long *asid_map; -static DEFINE_RAW_SPINLOCK (cpu_asid_lock); -unsigned int cpu_last_asid =ASID_FIRST_VERSION ; +static DEFINE_PER_CPU(atomic64_t, active_asids); +static DEFINE_PER_CPU(u64, reserved_asids); +static cpumask_t tlb_flush_pending;Quoted 13 lines
arch/arm64/include/asm/mmu.h
Quoted 7 lines
arch/arm64/include/asm/mmu.h +3
-4-/* -* We fork()ed a process, and we need a new context for the child to run in. -*/ -void __init_new_context (struct task_struct *tsk,struct mm_struct *mm) +#define ASID_MASK (~GENMASK(asid_bits - 1, 0)) +#define ASID_FIRST_VERSION (1UL << asid_bits) +#define NUM_USER_ASIDS ASID_FIRST_VERSIONApart from
NUM_USER_ASIDS, I think we can live with constants forASID_MASKandASID_FIRST_VERSION(as per 16-bit ASIDs, together with some shifts converted to a constant), marginally more optimal code generation which avoids readingasid_bitsall the time. We should be ok with 48-bit generation field.
Quoted 3 lines
The main reason for writing it like this is that it's easy to test the code with different asid sizes -- you just change
asid_bitsand all of the masks change accordingly.
My point was that an inclusive mask should be enough as long as NUM_USER_ASIDS changes.
Quoted 2 lines
If we hardcode
ASID_MASKthen we'll breakflush_context(which uses it to generate a bitmap index)
I don't fully get how it would break if the generation always starts from bit 16 and the asids are capped to NUM_USER_ASIDS. But I probably miss something.
Quoted 2 lines
and, given that
ASID_MASKandASID_FIRST_VERSIONare only used on the slow-path, I'd favour the current code over a micro-optimisation.
That's a good point. So leave it as it is, or maybe just avoid negating it twice and use (GENMASK(...)) directly.
Signature · 1 line
Catalin
Catalin Marinas
maintainerPATCH 6/10 arm64: tlbflush: avoid flushing when fullmm == 1
In reply to Will DeaconQuoted 45 lines · Will Deacon
Quoted 35 lines · Catalin Marinas
Quoted 34 lines · Will Deacon
The TLB gather code sets fullmm=1 when tearing down the entire address space for an
mm_structon exit or execve. Given that the ASID allocator will never re-allocate a dirty ASID, this flushing is not needed and can simply be avoided in the flushing code.Signed-off-by Will Deacon <[email protected]>--- arch/arm64/include/asm/tlb.h | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-)arch/arm64/include/asm/tlb.h +5
-11@@ -37,17 +37,21 @@ static inline void __tlb_remove_table(void *_table)static inline void tlb_flush (struct mmu_gather *tlb) { -if (tlb->fullmm) { -flush_tlb_mm (tlb->mm); - }else { -struct vm_area_struct vma = { .vm_mm = tlb->mm, }; -/* -* The intermediate page table levels are already handled by -* the __(pte|pmd|pud)_free_tlb() functions, so last level -* TLBI is sufficient here. -*/ -__flush_tlb_range (&vma, tlb->start, tlb->end, true); - } +struct vm_area_struct vma = { .vm_mm = tlb->mm, }; + +/* +* The ASID allocator will either invalidate the ASID or mark +* it as used. Quoted 8 lines
arch/arm64/include/asm/tlb.h static inline void __tlb_remove_table(void *_table) · line 47
Quoted 3 lines
arch/arm64/include/asm/tlb.h +3
-0+*/ +if (tlb->fullmm) +return ;BTW, do we actually need this
flush_tlb_mm()with the current ASID allocator? It doesn't reuse old ASIDs either before a full TLBI (just trying to remember if we had any logic; or maybe it was needed before non-lazy__pte_free_tlb).
Quoted 2 lines
I'm afraid I don't follow you here. This diff is removing the
flush_tlb_mmbecause, as you point out, it's no longer needed.
I don't think you are missing anything, just wondering why we had it before (probably forgot to remove it when the non-lazy __pte_free_tlb was added).
Signature · 1 line
Catalin