x86,tlb,mm: make lazy TLB mode even lazier
Subsystems (9)
v5 cover x86,tlb,mm: make lazy TLB mode even lazier
Song noticed switch_mm_irqs_off taking a lot of CPU time in recent kernels, using 1.9% of a 48 CPU system during a netperf run. Digging into the profile, the atomic operations in cpumask_clear_cpu and cpumask_set_cpu are responsible for about half of that CPU use.
However, the CPUs running netperf are simply switching back and forth between netperf and the idle task, which does not require any changes to the mm_cpumask if lazy TLB mode were used.
Additionally, the init_mm cpumask ends up being the most heavily contended one in the system, for no reason at all.
Making really lazy TLB mode work again on modern kernels, by sending a shootdown IPI only when page table pages are being unmapped, we get back some performance.
v5 of the series fixes the preempt bug and string overflow compiler warnings pointed out by Mike Galbraith.
On memcache workloads on 2 socket systems, this patch series seems to reduce total system CPU use by 1-2%. On Song's netbench tests, CPU use in the context switch time is about cut in half.
These patches also provide a little memory savings by shrinking the size of mm_struct, especially on distro kernels compiled with a gigantically large NR_CPUS.
PATCH v5 1/7 mm: allocate mm_cpumask dynamically based on nr_cpu_ids
The mm_struct always contains a cpumask bitmap, regardless of CONFIG_CPUMASK_OFFSTACK. That means the first step can be to simplify things, and simply have one bitmask at the end of the mm_struct for the mm_cpumask.
This does necessitate moving everything else in mm_struct into an anonymous sub-structure, which can be randomized when struct randomization is enabled.
The second step is to determine the correct size for the mm_struct slab object from the size of the mm_struct (excluding the cpu bitmap) and the size the cpumask.
For init_mm we can simply allocate the maximum size this kernel is compiled for, since we only have one init_mm in the system, anyway.
Pointer magic by Mike Galbraith, to evade -Wstringop-overflow getting confused by the dynamically sized array.
--- drivers/firmware/efi/efi.c | 1 + include/linux/mm_types.h | 241 +++++++++++++++++++++++---------------------- kernel/fork.c | 15 +-- mm/init-mm.c | 11 +++ 4 files changed, 145 insertions(+), 123 deletions(-)
drivers/firmware/efi/efi.c +1 -0
@@ -82,6 +82,7 @@ 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), + .cpu_bitmap = { [BITS_TO_LONGS(NR_CPUS)] = 0}, };static bool disable_runtime;
include/linux/mm_types.h +124 -117
300 lines not on this page · raw message
kernel/fork.c +9 -6
@@ -2253,6 +2253,8 @@ static void sighand_ctor(void *data)void __init proc_caches_init (void ) { +unsigned int mm_size; + sighand_cachep = kmem_cache_create("sighand_cache", sizeof(struct sighand_struct), 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_TYPESAFE_BY_RCU|
@@ -2269,15 +2271,16 @@ void __init proc_caches_init(void)sizeof (struct fs_struct ),0 ,SLAB_HWCACHE_ALIGN |SLAB_PANIC |SLAB_ACCOUNT , NULL); +/* -* FIXME! The "sizeof(struct mm_struct)" currently includes the -* whole struct cpumask for the OFFSTACK case. We could change -* this to *only* allocate as much of it as required by the -* maximum number of CPU's we can ever have. The cpumask_allocation -* is at the end of the structure, exactly for that reason. +* The mm_cpumask is located at the end of mm_struct, and is +* dynamically sized based on the maximum CPU number this system +* can have, taking hotplug into account (nr_cpu_ids). */ + mm_size =sizeof (struct mm_struct ) +cpumask_size (); + mm_cachep = kmem_cache_create_usercopy("mm_struct", - sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN, + mm_size, ARCH_MIN_MMSTRUCT_ALIGN, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, offsetof(struct mm_struct, saved_auxv), sizeof_field(struct mm_struct, saved_auxv),
mm/init-mm.c +11 -0
@@ -15,6 +15,16 @@#define INIT_MM_CONTEXT (name)#endif +/* +* For dynamically allocated mm_structs, there is a dynamically sized cpumask +* at the end of the structure, the size of which depends on the maximum CPU +* number the system can see. That way we allocate only as much memory for +* mm_cpumask() as needed for the hundreds, or thousands of processes that +* a system typically runs. +* +* Since there is only one init_mm in the entire system, keep it simple +* and size this cpu_bitmask to NR_CPUS. +*/ struct mm_struct init_mm = { .mm_rb =RB_ROOT , .pgd = swapper_pg_dir,
@@ -25,5 +35,6 @@ struct mm_struct init_mm = { .arg_lock = __SPIN_LOCK_UNLOCKED(init_mm.arg_lock), .mmlist = LIST_HEAD_INIT(init_mm.mmlist), .user_ns = &init_user_ns, + .cpu_bitmap = { [BITS_TO_LONGS(NR_CPUS)] = 0}, INIT_MM_CONTEXT(init_mm) };
Signature · 1 line
2.14.4
Re: [PATCH 1/7] mm: allocate mm_cpumask dynamically based on nr_cpu_ids
Quoted 17 lines · Rik van Riel
The
mm_structalways contains a cpumask bitmap, regardless ofCONFIG_CPUMASK_OFFSTACK. That means the first step can be to simplify things, and simply have one bitmask at the end of themm_structfor themm_cpumask.This does necessitate moving everything else in
mm_structinto an anonymous sub-structure, which can be randomized when struct randomization is enabled.The second step is to determine the correct size for the
mm_structslab object from the size of themm_struct(excluding the cpu bitmap) and the size the cpumask.For
init_mmwe can simply allocate the maximum size this kernel is compiled for, since we only have oneinit_mmin the system, anyway.
Quoted 5 lines
Pointer magic by Mike Galbraith, to evade -Wstringop-overflow getting confused by the dynamically sized array.
Signed-off-by Rik van Riel <[email protected]>Signed-off-by Mike Galbraith <[email protected]>
Is this an Acked-by in disguise, or did this patch route via Mike?
Thanks,
Ingo
Re: [PATCH 1/7] mm: allocate mm_cpumask dynamically based on nr_cpu_ids
Quoted 19 lines · Ingo Molnar
Quoted 17 lines · Rik van Riel
The
mm_structalways contains a cpumask bitmap, regardless ofCONFIG_CPUMASK_OFFSTACK. That means the first step can be to simplify things, and simply have one bitmask at the end of themm_structfor themm_cpumask.This does necessitate moving everything else in
mm_structinto an anonymous sub-structure, which can be randomized when struct randomization is enabled.The second step is to determine the correct size for the
mm_structslab object from the size of themm_struct(excluding the cpu bitmap) and the size the cpumask.For
init_mmwe can simply allocate the maximum size this kernel is compiled for, since we only have oneinit_mmin the system, anyway.
Quoted 7 lines
Quoted 5 lines
Pointer magic by Mike Galbraith, to evade -Wstringop-overflow getting confused by the dynamically sized array.
Signed-off-by Rik van Riel <[email protected]>Signed-off-by Mike Galbraith <[email protected]>Is this an Acked-by in disguise, or did this patch route via Mike?
Mike found an issue with the patch and sent a
fix, so I added his S-o-b to this patch as
well.
Signature · 1 line
All Rights Reversed.
Re: [PATCH 1/7] mm: allocate mm_cpumask dynamically based on nr_cpu_ids
Quoted 23 lines · Rik van Riel
Quoted 22 lines · Ingo Molnar
Quoted 20 lines · Rik van Riel
The
mm_structalways contains a cpumask bitmap, regardless ofCONFIG_CPUMASK_OFFSTACK. That means the first step can be to simplify things, and simply have one bitmask at the end of themm_structfor themm_cpumask.This does necessitate moving everything else in
mm_structinto an anonymous sub-structure, which can be randomized when struct randomization is enabled.The second step is to determine the correct size for the
mm_structslab object from the size of themm_struct(excluding the cpu bitmap) and the size the cpumask.For
init_mmwe can simply allocate the maximum size this kernel is compiled for, since we only have oneinit_mmin the system, anyway.Pointer magic by Mike Galbraith, to evade -Wstringop-overflow getting confused by the dynamically sized array.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Signed-off-by Rik van Riel <[email protected]>Signed-off-by Mike Galbraith <[email protected]>Is this an Acked-by in disguise, or did this patch route via Mike?
Mike found an issue with the patch and sent a
fix, so I added his S-o-b to this patch as
well.
Makes sense - I'd suggest such a SoB chain:
... it's a bit non-standard but we've used it in similar cases and it makes the routing and evolution of the patch pretty clear.
Thanks,
Ingo
PATCH v5 2/7 x86,tlb: leave lazy TLB mode at page table free time
In reply to Rik van RielAndy discovered that speculative memory accesses while in lazy TLB mode can crash a system, when a CPU tries to dereference a speculative access using memory contents that used to be valid page table memory, but have since been reused for something else and point into la-la land.
The latter problem can be prevented in two ways. The first is to always send a TLB shootdown IPI to CPUs in lazy TLB mode, while the second one is to only send the TLB shootdown at page table freeing time.
The second should result in fewer IPIs, since operationgs like mprotect and madvise are very common with some workloads, but do not involve page table freeing. Also, on munmap, batching of page table freeing covers much larger ranges of virtual memory than the batching of unmapped user pages.
--- arch/x86/include/asm/tlbflush.h | 5 +++++ arch/x86/mm/tlb.c | 27 +++++++++++++++++++++++++++ include/asm-generic/tlb.h | 10 ++++++++++ mm/memory.c | 22 ++++++++++++++-------- 4 files changed, 56 insertions(+), 8 deletions(-)
arch/x86/include/asm/tlbflush.h +5 -0
@@ -554,4 +554,9 @@ extern void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch); native_flush_tlb_others(mask, info)#endif +extern void tlb_flush_remove_tables (struct mm_struct *mm); +extern void tlb_flush_remove_tables_local (void *arg); + +#define HAVE_TLB_FLUSH_REMOVE_TABLES +#endif /* _ASM_X86_TLBFLUSH_H */
arch/x86/mm/tlb.c +27 -0
@@ -646,6 +646,33 @@ void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,put_cpu (); } +void tlb_flush_remove_tables_local (void *arg) +{ +struct mm_struct *mm = arg; + +if (this_cpu_read (cpu_tlbstate.loaded_mm) == mm && +this_cpu_read (cpu_tlbstate.is_lazy)) { +/* +* We're in lazy mode. We need to at least flush our +* paging-structure cache to avoid speculatively reading +* garbage into our TLB. Since switching to init_mm is barely +* slower than a minimal flush, just switch to init_mm. +*/ +switch_mm_irqs_off (NULL , &init_mm,NULL ); + } +} + +void tlb_flush_remove_tables (struct mm_struct *mm) +{ +int cpu =get_cpu (); +/* +* XXX: this really only needs to be called for CPUs in lazy TLB mode. +*/ +if (cpumask_any_but (mm_cpumask (mm), cpu) < nr_cpu_ids) +smp_call_function_many (mm_cpumask (mm), tlb_flush_remove_tables_local, (void *)mm,1 ); + +put_cpu (); +}static void do_flush_tlb_all (void *info) {
include/asm-generic/tlb.h +10 -0
@@ -295,4 +295,14 @@ static inline void tlb_remove_check_page_size_change(struct mmu_gather *tlb,#define tlb_migrate_finish (mm) do {} while (0) +/* +* Used to flush the TLB when page tables are removed, when lazy +* TLB mode may cause a CPU to retain intermediate translations +* pointing to about-to-be-freed page table memory. +*/ +#ifndef HAVE_TLB_FLUSH_REMOVE_TABLES +#define tlb_flush_remove_tables (mm) do {} while (0) +#define tlb_flush_remove_tables_local (mm) do {} while (0) +#endif +#endif /* _ASM_GENERIC__TLB_H */
mm/memory.c +14 -8
@@ -326,16 +326,20 @@ bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page, int page_#ifdef CONFIG_HAVE_RCU_TABLE_FREE -/* -* See the comment near struct mmu_table_batch. -*/ -static void tlb_remove_table_smp_sync (void *arg) { -/* Simply deliver the interrupt */ +struct mm_struct __maybe_unused *mm = arg; +/* +* On most architectures this does nothing. Simply delivering the +* interrupt is enough to prevent races with software page table +* walking like that done in get_user_pages_fast. +* +* See the comment near struct mmu_table_batch. +*/ +tlb_flush_remove_tables_local (mm); } -static void tlb_remove_table_one (void *table) +static void tlb_remove_table_one (void *table,struct mmu_gather *tlb) {/* * This isn't an RCU grace period and hence the page-tables cannot be
@@ -344,7 +348,7 @@ static void tlb_remove_table_one(void *table)* It is however sufficient for software page-table walkers that rely on * IRQ disabling. See the comment near struct mmu_table_batch. */ -smp_call_function (tlb_remove_table_smp_sync,NULL ,1 ); +smp_call_function (tlb_remove_table_smp_sync, tlb->mm,1 );__tlb_remove_table (table); }
@@ -365,6 +369,8 @@ void tlb_table_flush(struct mmu_gather *tlb) {struct mmu_table_batch **batch = &tlb->batch; +tlb_flush_remove_tables (tlb->mm); +if (*batch) {call_rcu_sched (&(*batch)->rcu, tlb_remove_table_rcu); *batch =NULL ;
@@ -387,7 +393,7 @@ void tlb_remove_table(struct mmu_gather *tlb, void *table)if (*batch ==NULL ) { *batch = (struct mmu_table_batch *)__get_free_page (GFP_NOWAIT | __GFP_NOWARN);if (*batch ==NULL ) { -tlb_remove_table_one (table); +tlb_remove_table_one (table, tlb);return ; } (*batch)->nr =0 ;
Signature · 1 line
2.14.4
PATCH v5 3/7 x86,mm: restructure switch_mm_irqs_off
In reply to Rik van RielMove some code that will be needed for the lazy -> !lazy state transition when a lazy TLB CPU has gotten out of date.
No functional changes, since the if (real_prev == next) branch always returns.
--- arch/x86/mm/tlb.c | 60 +++++++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 30 deletions(-)
arch/x86/mm/tlb.c +30 -30
@@ -187,6 +187,8 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,u16 prev_asid =this_cpu_read (cpu_tlbstate.loaded_mm_asid);unsigned cpu =smp_processor_id ();u64 next_tlb_gen; +bool need_flush; +u16 new_asid;/* * NB: The scheduler will call us with prev == next when switching
@@ -252,8 +254,6 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,return ; }else { -u16 new_asid; -bool need_flush;u64 last_ctx_id =this_cpu_read (cpu_tlbstate.last_ctx_id);/*
@@ -297,41 +297,41 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, next_tlb_gen =atomic64_read (&next->context.tlb_gen);choose_new_asid (next, next_tlb_gen, &new_asid, &need_flush); + } -if (need_flush) { -this_cpu_write (cpu_tlbstate.ctxs[new_asid].ctx_id, next->context.ctx_id); -this_cpu_write (cpu_tlbstate.ctxs[new_asid].tlb_gen, next_tlb_gen); -load_new_mm_cr3 (next->pgd, new_asid, true); - -/* -* NB: This gets called via leave_mm() in the idle path -* where RCU functions differently. Tracing normally -* uses RCU, so we need to use the _rcuidle variant. -* -* (There is no good reason for this. The idle code should -* be rearranged to call this before rcu_idle_enter().) -*/ -trace_tlb_flush_rcuidle (TLB_FLUSH_ON_TASK_SWITCH ,TLB_FLUSH_ALL ); - }else { -/* The new ASID is already up to date. */ -load_new_mm_cr3 (next->pgd, new_asid, false); - -/* See above wrt _rcuidle. */ -trace_tlb_flush_rcuidle (TLB_FLUSH_ON_TASK_SWITCH ,0 ); - } +if (need_flush) { +this_cpu_write (cpu_tlbstate.ctxs[new_asid].ctx_id, next->context.ctx_id); +this_cpu_write (cpu_tlbstate.ctxs[new_asid].tlb_gen, next_tlb_gen); +load_new_mm_cr3 (next->pgd, new_asid, true);/* -* Record last user mm's context id, so we can avoid -* flushing branch buffer with IBPB if we switch back -* to the same user. +* NB: This gets called via leave_mm() in the idle path +* where RCU functions differently. Tracing normally +* uses RCU, so we need to use the _rcuidle variant. +* +* (There is no good reason for this. The idle code should +* be rearranged to call this before rcu_idle_enter().) */ -if (next != &init_mm) -this_cpu_write (cpu_tlbstate.last_ctx_id, next->context.ctx_id); +trace_tlb_flush_rcuidle (TLB_FLUSH_ON_TASK_SWITCH ,TLB_FLUSH_ALL ); + }else { +/* The new ASID is already up to date. */ +load_new_mm_cr3 (next->pgd, new_asid, false); -this_cpu_write (cpu_tlbstate.loaded_mm, next); -this_cpu_write (cpu_tlbstate.loaded_mm_asid, new_asid); +/* See above wrt _rcuidle. */ +trace_tlb_flush_rcuidle (TLB_FLUSH_ON_TASK_SWITCH ,0 ); } +/* +* Record last user mm's context id, so we can avoid +* flushing branch buffer with IBPB if we switch back +* to the same user. +*/ +if (next != &init_mm) +this_cpu_write (cpu_tlbstate.last_ctx_id, next->context.ctx_id); + +this_cpu_write (cpu_tlbstate.loaded_mm, next); +this_cpu_write (cpu_tlbstate.loaded_mm_asid, new_asid); +load_mm_cr4 (next);switch_ldt (real_prev, next); }
Signature · 1 line
2.14.4
PATCH v5 4/7 x86,tlb: make lazy TLB mode lazier
In reply to Rik van RielLazy TLB mode can result in an idle CPU being woken up by a TLB flush, when all it really needs to do is reload %CR3 at the next context switch, assuming no page table pages got freed.
Memory ordering is used to prevent race conditions between switch_mm_irqs_off, which checks whether .tlb_gen changed, and the TLB invalidation code, which increments .tlb_gen whenever page table entries get invalidated.
The atomic increment in inc_mm_tlb_gen is its own barrier; the context switch code adds an explicit barrier between reading tlbstate.is_lazy and next->context.tlb_gen.
Unlike the 2016 version of this patch, CPUs with cpu_tlbstate.is_lazy set are not removed from the mm_cpumask(mm), since that would prevent the TLB flush IPIs at page table free time from being sent to all the CPUs that need them.
This patch reduces total CPU use in the system by about 1-2% for a memcache workload on two socket systems, and by about 1% for a heavily multi-process netperf between two systems.
--- arch/x86/mm/tlb.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 9 deletions(-)
arch/x86/mm/tlb.c +59 -9
@@ -7,6 +7,7 @@#include <linux/export.h> #include <linux/cpu.h> #include <linux/debugfs.h> +#include <linux/gfp.h> #include <asm/tlbflush.h> #include <asm/mmu_context.h>
@@ -185,6 +186,7 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, {struct mm_struct *real_prev =this_cpu_read (cpu_tlbstate.loaded_mm);u16 prev_asid =this_cpu_read (cpu_tlbstate.loaded_mm_asid); +bool was_lazy =this_cpu_read (cpu_tlbstate.is_lazy);unsigned cpu =smp_processor_id ();u64 next_tlb_gen;bool need_flush;
@@ -242,17 +244,40 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, next->context.ctx_id);/* -* We don't currently support having a real mm loaded without -* our cpu set in mm_cpumask(). We have all the bookkeeping -* in place to figure out whether we would need to flush -* if our cpu were cleared in mm_cpumask(), but we don't -* currently use it. +* Even in lazy TLB mode, the CPU should stay set in the +* mm_cpumask. The TLB shootdown code can figure out from +* from cpu_tlbstate.is_lazy whether or not to send an IPI. */ if (WARN_ON_ONCE (real_prev != &init_mm && !cpumask_test_cpu (cpu,mm_cpumask (next))))cpumask_set_cpu (cpu,mm_cpumask (next)); -return ; +/* +* If the CPU is not in lazy TLB mode, we are just switching +* from one thread in a process to another thread in the same +* process. No TLB flush required. +*/ +if (!was_lazy) +return ; + +/* +* Read the tlb_gen to check whether a flush is needed. +* If the TLB is up to date, just use it. +* The barrier synchronizes with the tlb_gen increment in +* the TLB shootdown code. +*/ +smp_mb (); + next_tlb_gen =atomic64_read (&next->context.tlb_gen); +if (this_cpu_read (cpu_tlbstate.ctxs[prev_asid].tlb_gen) == + next_tlb_gen) +return ; + +/* +* TLB contents went out of date while we were in lazy +* mode. Fall through to the TLB switching code below. +*/ + new_asid = prev_asid; + need_flush = true; }else {u64 last_ctx_id =this_cpu_read (cpu_tlbstate.last_ctx_id);
@@ -454,6 +479,9 @@ static void flush_tlb_func_common(const struct flush_tlb_info *f,* paging-structure cache to avoid speculatively reading * garbage into our TLB. Since switching to init_mm is barely * slower than a minimal flush, just switch to init_mm. +* +* This should be rare, with native_flush_tlb_others skipping +* IPIs to lazy TLB mode CPUs. */ switch_mm_irqs_off (NULL , &init_mm,NULL );return ;
@@ -560,6 +588,9 @@ static void flush_tlb_func_remote(void *info)void native_flush_tlb_others (const struct cpumask *cpumask,const struct flush_tlb_info *info) { +cpumask_var_t lazymask; +unsigned int cpu; +count_vm_tlb_event (NR_TLB_REMOTE_FLUSH );if (info->end ==TLB_FLUSH_ALL )trace_tlb_flush (TLB_REMOTE_SEND_IPI ,TLB_FLUSH_ALL );
@@ -583,8 +614,6 @@ void native_flush_tlb_others(const struct cpumask *cpumask,* that UV should be updated so that smp_call_function_many(), * etc, are optimal on UV. */ -unsigned int cpu; - cpu =smp_processor_id (); cpumask =uv_flush_tlb_others (cpumask, info);if (cpumask)
@@ -592,8 +621,29 @@ void native_flush_tlb_others(const struct cpumask *cpumask, (void *)info, 1);return ; } -smp_call_function_many (cpumask, flush_tlb_func_remote, + +/* +* A temporary cpumask is used in order to skip sending IPIs +* to CPUs in lazy TLB state, while keeping them in mm_cpumask(mm). +* If the allocation fails, simply IPI every CPU in mm_cpumask. +*/ +if (!alloc_cpumask_var (&lazymask,GFP_ATOMIC )) { +smp_call_function_many (cpumask, flush_tlb_func_remote, + (void *)info,1 ); +return ; + } + +cpumask_copy (lazymask, cpumask); + +for_each_cpu (cpu, lazymask) { +if (per_cpu (cpu_tlbstate.is_lazy, cpu)) +cpumask_clear_cpu (cpu, lazymask); + } + +smp_call_function_many (lazymask, flush_tlb_func_remote, (void *)info,1 ); + +free_cpumask_var (lazymask); }/*
Signature · 1 line
2.14.4
PATCH v5 5/7 x86,tlb: only send page table free TLB flush to lazy TLB CPUs
In reply to Rik van RielCPUs in !is_lazy have either received TLB flush IPIs earlier on during the munmap (when the user memory was unmapped), or have context switched and reloaded during that stage of the munmap.
Page table free TLB flushes only need to be sent to CPUs in lazy TLB mode, which TLB contents might not yet be up to date yet.
--- arch/x86/mm/tlb.c | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-)
arch/x86/mm/tlb.c +39 -4
@@ -712,15 +712,50 @@ void tlb_flush_remove_tables_local(void *arg) } } +static void mm_fill_lazy_tlb_cpu_mask (struct mm_struct *mm, +struct cpumask *lazy_cpus) +{ +int cpu; + +for_each_cpu (cpu,mm_cpumask (mm)) { +if (!per_cpu (cpu_tlbstate.is_lazy, cpu)) +cpumask_set_cpu (cpu, lazy_cpus); + } +} +void tlb_flush_remove_tables (struct mm_struct *mm) {int cpu =get_cpu (); +cpumask_var_t lazy_cpus; + +if (cpumask_any_but (mm_cpumask (mm), cpu) >= nr_cpu_ids) { +put_cpu (); +return ; + } + +if (!zalloc_cpumask_var (&lazy_cpus,GFP_ATOMIC )) { +/* +* If the cpumask allocation fails, do a brute force flush +* on all the CPUs that have this mm loaded. +*/ +smp_call_function_many (mm_cpumask (mm), + tlb_flush_remove_tables_local, (void *)mm,1 ); +put_cpu (); +return ; + } +/* -* XXX: this really only needs to be called for CPUs in lazy TLB mode. +* CPUs with !is_lazy either received a TLB flush IPI while the user +* pages in this address range were unmapped, or have context switched +* and reloaded %CR3 since then. +* +* Shootdown IPIs at page table freeing time only need to be sent to +* CPUs that may have out of date TLB contents. */ -if (cpumask_any_but (mm_cpumask (mm), cpu) < nr_cpu_ids) -smp_call_function_many (mm_cpumask (mm), tlb_flush_remove_tables_local, (void *)mm,1 ); - +mm_fill_lazy_tlb_cpu_mask (mm, lazy_cpus); +smp_call_function_many (lazy_cpus, + tlb_flush_remove_tables_local, (void *)mm,1 ); +free_cpumask_var (lazy_cpus);put_cpu (); }
Signature · 1 line
2.14.4
PATCH v5 6/7 x86,mm: always use lazy TLB mode
In reply to Rik van RielNow that CPUs in lazy TLB mode no longer receive TLB shootdown IPIs, except at page table freeing time, and idle CPUs will no longer get shootdown IPIs for things like mprotect and madvise, we can always use lazy TLB mode.
--- arch/x86/include/asm/tlbflush.h | 16 ---------------- arch/x86/mm/tlb.c | 15 +-------------- 2 files changed, 1 insertion(+), 30 deletions(-)
arch/x86/include/asm/tlbflush.h +0 -16
@@ -148,22 +148,6 @@ static inline unsigned long build_cr3_noflush(pgd_t *pgd, u16 asid)#define __flush_tlb_one_user (addr) __native_flush_tlb_one_user(addr)#endif -static inline bool tlb_defer_switch_to_init_mm (void ) -{ -/* -* If we have PCID, then switching to init_mm is reasonably -* fast. If we don't have PCID, then switching to init_mm is -* quite slow, so we try to defer it in the hopes that we can -* avoid it entirely. The latter approach runs the risk of -* receiving otherwise unnecessary IPIs. -* -* This choice is just a heuristic. The tlb code can handle this -* function returning true or false regardless of whether we have -* PCID. -*/ -return !static_cpu_has (X86_FEATURE_PCID ); -} -struct tlb_context {u64 ctx_id;u64 tlb_gen;
arch/x86/mm/tlb.c +1 -14
@@ -379,20 +379,7 @@ void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)if (this_cpu_read (cpu_tlbstate.loaded_mm) == &init_mm)return ; -if (tlb_defer_switch_to_init_mm ()) { -/* -* There's a significant optimization that may be possible -* here. We have accurate enough TLB flush tracking that we -* don't need to maintain coherence of TLB per se when we're -* lazy. We do, however, need to maintain coherence of -* paging-structure caches. We could, in principle, leave our -* old mm loaded and only switch to init_mm when -* tlb_remove_page() happens. -*/ -this_cpu_write (cpu_tlbstate.is_lazy, true); - }else { -switch_mm (NULL , &init_mm,NULL ); - } +this_cpu_write (cpu_tlbstate.is_lazy, true); }/*
Signature · 1 line
2.14.4
PATCH v5 7/7 x86,switch_mm: skip atomic operations for init_mm
In reply to Rik van RielSong noticed switch_mm_irqs_off taking a lot of CPU time in recent kernels,using 1.8% of a 48 CPU system during a netperf to localhost run. Digging into the profile, we noticed that cpumask_clear_cpu and cpumask_set_cpu together take about half of the CPU time taken by switch_mm_irqs_off.
However, the CPUs running netperf end up switching back and forth between netperf and the idle task, which does not require changes to the mm_cpumask. Furthermore, the init_mm cpumask ends up being the most heavily contended one in the system.
Simply skipping changes to mm_cpumask(&init_mm) reduces overhead.
--- arch/x86/mm/tlb.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
arch/x86/mm/tlb.c +12 -5
@@ -310,15 +310,22 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,sync_current_stack_to_mm (next); } -/* Stop remote flushes for the previous mm */ -VM_WARN_ON_ONCE (!cpumask_test_cpu (cpu,mm_cpumask (real_prev)) && - real_prev != &init_mm); -cpumask_clear_cpu (cpu,mm_cpumask (real_prev)); +/* +* Stop remote flushes for the previous mm. +* Skip the idle task; we never send init_mm TLB flushing IPIs, +* but the bitmap manipulation can cause cache line contention. +*/ +if (real_prev != &init_mm) { +VM_WARN_ON_ONCE (!cpumask_test_cpu (cpu, +mm_cpumask (real_prev))); +cpumask_clear_cpu (cpu,mm_cpumask (real_prev)); + }/* * Start remote flushes and then read tlb_gen. */ -cpumask_set_cpu (cpu,mm_cpumask (next)); +if (next != &init_mm) +cpumask_set_cpu (cpu,mm_cpumask (next)); next_tlb_gen =atomic64_read (&next->context.tlb_gen);choose_new_asid (next, next_tlb_gen, &new_asid, &need_flush);
Signature · 1 line
2.14.4
Re: [PATCH 7/7] x86,switch_mm: skip atomic operations for init_mm
arch/x86/mm/tlb.c void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, · line 321
Quoted 9 lines · Rik van Riel
arch/x86/mm/tlb.c +9
-0+/* +* Stop remote flushes for the previous mm. +* Skip the idle task; we never send init_mm TLB flushing IPIs, +* but the bitmap manipulation can cause cache line contention. +*/ +if (real_prev != &init_mm) { +VM_WARN_ON_ONCE (!cpumask_test_cpu (cpu, +mm_cpumask (real_prev))); +cpumask_clear_cpu (cpu,mm_cpumask (real_prev));
BTW., could this optimization be (safely) extended to all (or most) !task->mm kernel threads?
In particular softirq and threaded irq handlers could benefit greatly I suspect in certain networking intense workloads that happen to active them.
Thanks,
Ingo
Re: [PATCH 7/7] x86,switch_mm: skip atomic operations for init_mm
Quoted 20 lines · Ingo Molnar
Quoted 12 lines · Rik van Riel
arch/x86/mm/tlb.c +3
-0+/* +* Stop remote flushes for the previous mm. +* Skip the idle task; we never send init_mm TLB flushing IPIs,
(quoted diff) +1
-0+ * but the bitmap manipulation can cause cacheline contention.
arch/x86/mm/tlb.c +4
-0+*/ + if (real_prev != &init_mm) { + VM_WARN_ON_ONCE(!cpumask_test_cpu(cpu, + mm_cpumask(real_prev)));
(quoted diff) +1
-0+ cpumask_clear_cpu(cpu,Quoted 1 line
mm_cpumask(real_prev));BTW., could this optimization be (safely) extended to all (or most) !task->mm
kernel threads?
Quoted 3 lines
In particular softirq and threaded irq handlers could benefit greatly I suspect in
certain networking intense workloads that happen to active them.
Yes, it could.
Are there kernel threads that use something other than init_mm today?
Signature · 1 line
All Rights Reversed.
Re: [PATCH 7/7] x86,switch_mm: skip atomic operations for init_mm
Quoted 21 lines · Rik van Riel
Quoted 20 lines · Ingo Molnar
Quoted 12 lines · Rik van Riel
arch/x86/mm/tlb.c +3
-0+/* +* Stop remote flushes for the previous mm. +* Skip the idle task; we never send init_mm TLB flushing IPIs,
(quoted diff) +1
-0+ * but the bitmap manipulation can cause cacheline contention.
arch/x86/mm/tlb.c +4
-0+*/ + if (real_prev != &init_mm) { + VM_WARN_ON_ONCE(!cpumask_test_cpu(cpu, + mm_cpumask(real_prev)));
(quoted diff) +1
-0+ cpumask_clear_cpu(cpu,Quoted 1 line
mm_cpumask(real_prev));BTW., could this optimization be (safely) extended to all (or most) !task->mm
kernel threads?
Quoted 8 lines
Quoted 3 lines
In particular softirq and threaded irq handlers could benefit greatly I suspect in
certain networking intense workloads that happen to active them.Yes, it could.
Are there kernel threads that use something other than
init_mmtoday?
Yeah, I think that's the typical case - so at minimum the comment should be fixed:
Quoted 1 line
Quoted 1 line
arch/x86/mm/tlb.c void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, · line 315
Quoted 1 line
arch/x86/mm/tlb.c +1
-0+* Skip the idle task; we never send init_mm TLB flushing IPIs,
and it should say 'skip kernel threads', right?
Thanks,
Ingo
Re: [PATCH 7/7] x86,switch_mm: skip atomic operations for init_mm
Quoted 36 lines · Ingo Molnar
Quoted 29 lines · Rik van Riel
Quoted 23 lines · Ingo Molnar
Quoted 14 lines · Rik van Riel
arch/x86/mm/tlb.c +3
-0+/* +* Stop remote flushes for the previous mm. +* Skip the idle task; we never send init_mm TLB
flushing IPIs,(quoted diff) +1
-0+ * but the bitmap manipulation can cause cacheline contention.
arch/x86/mm/tlb.c +4
-0+*/ + if (real_prev != &init_mm) { + VM_WARN_ON_ONCE(!cpumask_test_cpu(cpu, + mm_cpumask(rea
l_pr
ev)));(quoted diff) +1
-0+ cpumask_clear_cpu(cpu,Quoted 1 line
mm_cpumask(real_prev));BTW., could this optimization be (safely) extended to all (or most)
!task->mm
kernel threads?Quoted 4 lines
In particular softirq and threaded irq handlers could benefit greatly
I suspect in
certain networking intense workloads that happen to active them.Quoted 4 lines
Yes, it could.
Are there kernel threads that use something other than
init_mmtoday?
Quoted 7 lines
Yeah, I think that's the typical case - so at minimum the comment should be fixed:
Quoted 1 line
Quoted 1 line
Review
Quoted 1 line
(quoted diff) +1
-0+ * Skip the idle task; we never send init_mm TLBflushing IPIs,
and it should say 'skip kernel threads', right?
I will send a v6 that improves this comment, and has the S-o-b thing you suggested for patch 1/7.
I think that addresses all the comments people had on this patch series.
Signature · 1 line
All Rights Reversed.