tlb flush optimization on x86
Subsystems (10)
v6 cover tlb flush optimization on x86
Thanks Peter Z, Peter Anvin, Nick Piggin, and many others' comments!
The main change of this version is on generic mmu_gather code. It was tested with arm cross-compiler.
Thanks Rongjie's testing, that show the real case performance gain.
Alex Shi
[PATCH v6 1/7] x86/tlb: unify TLB_FLUSH_ALL definition [PATCH v6 2/7] x86/tlb_info: get last level TLB entry number of CPU [PATCH v6 3/7] x86/flush_tlb: try flush_tlb_single one by one in [PATCH v6 4/7] x86/tlb: fall back to flush all when meet a THP large [PATCH v6 5/7] x86/tlb: add tlb_flushall_shift for specific CPU [PATCH v6 6/7] x86/tlb: enable tlb flush range support for generic [PATCH v6 7/7] x86/tlb: add tlb_flushall_shift knob into debugfs
PATCH v6 1/7 x86/tlb: unify TLB_FLUSH_ALL definition
32bits kernel is using ILP32 model. 64 bits kernel is using LP64 model. So, here TLB_FLUSH_ALL can be defined as '-1UL', that fit in both x86_32/x86_64 mode.
Thanks for Rob Landley and Peter Avin's infos
http://www.unix.org/whitepapers/64bit.html
http://www.unix.org/version2/whatsnew/lp64_wp.html
--- arch/x86/include/asm/tlbflush.h | 6 +----- 1 files changed, 1 insertions(+), 5 deletions(-)
arch/x86/include/asm/tlbflush.h +1 -5
@@ -62,11 +62,7 @@ static inline void __flush_tlb_one(unsigned long addr)__flush_tlb (); } -#ifdef CONFIG_X86_32 -# define TLB_FLUSH_ALL 0xffffffff -#else -# define TLB_FLUSH_ALL -1ULL -#endif +#define TLB_FLUSH_ALL -1UL/* * TLB flushing:
Signature · 1 line
1.7.5.4
PATCH v6 2/7 x86/tlb_info: get last level TLB entry number of CPU
In reply to Alex ShiFor 4KB pages, x86 CPU has 2 or 1 level TLB, first level is data TLB and instruction TLB, second level is shared TLB for both data and instructions.
For hupe page TLB, usually there is just one level and seperated by 2MB/4MB and 1GB.
Although each levels TLB size is important for performance tuning, but for genernal and rude optimizing, last level TLB entry number is suitable. And in fact, last level TLB always has the biggest entry number.
This patch will get the biggest TLB entry number and use it in furture TLB optimizing.
Accroding Borislav's suggestion, except tlb_ll[i/d]_* array, other function and data will be released after system boot up.
For all kinds of x86 vendor friendly, vendor specific code was moved to its specific files.
--- arch/x86/include/asm/processor.h | 11 +++ arch/x86/kernel/cpu/common.c | 21 ++++++ arch/x86/kernel/cpu/cpu.h | 9 +++ arch/x86/kernel/cpu/intel.c | 141 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 182 insertions(+), 0 deletions(-)
arch/x86/include/asm/processor.h +11 -0
@@ -61,6 +61,17 @@ static inline void *current_text_addr(void)# define ARCH_MIN_MMSTRUCT_ALIGN 0#endif +enum tlb_infos { +ENTRIES , +NR_INFO +}; + +extern u16 __read_mostly tlb_lli_4k[NR_INFO ]; +extern u16 __read_mostly tlb_lli_2m[NR_INFO ]; +extern u16 __read_mostly tlb_lli_4m[NR_INFO ]; +extern u16 __read_mostly tlb_lld_4k[NR_INFO ]; +extern u16 __read_mostly tlb_lld_2m[NR_INFO ]; +extern u16 __read_mostly tlb_lld_4m[NR_INFO ];/* * CPU type and hardware bug flags. Kept separately for each CPU. * Members of this structure are referenced in head.S, so think twice
arch/x86/kernel/cpu/common.c +21 -0
@@ -452,6 +452,25 @@ void __cpuinit cpu_detect_cache_sizes(struct cpuinfo_x86 *c) c->x86_cache_size = l2size; } +u16 __read_mostly tlb_lli_4k[NR_INFO ]; +u16 __read_mostly tlb_lli_2m[NR_INFO ]; +u16 __read_mostly tlb_lli_4m[NR_INFO ]; +u16 __read_mostly tlb_lld_4k[NR_INFO ]; +u16 __read_mostly tlb_lld_2m[NR_INFO ]; +u16 __read_mostly tlb_lld_4m[NR_INFO ]; + +void __cpuinit cpu_detect_tlb (struct cpuinfo_x86 *c) +{ +if (c->x86_vendor ==X86_VENDOR_INTEL ) +intel_cpu_detect_tlb (c); + +printk (KERN_INFO "Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n" \ +"Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d\n" , + tlb_lli_4k[ENTRIES ], tlb_lli_2m[ENTRIES ], + tlb_lli_4m[ENTRIES ], tlb_lld_4k[ENTRIES ], + tlb_lld_2m[ENTRIES ], tlb_lld_4m[ENTRIES ]); +} +void __cpuinit detect_ht (struct cpuinfo_x86 *c) {#ifdef CONFIG_X86_HT
@@ -911,6 +930,8 @@ void __init identify_boot_cpu(void)#else vgetcpu_set_mode ();#endif +if (boot_cpu_data.cpuid_level >=2 ) +cpu_detect_tlb (&boot_cpu_data); }void __cpuinit identify_secondary_cpu (struct cpuinfo_x86 *c)
arch/x86/kernel/cpu/cpu.h +9 -0
@@ -24,6 +24,14 @@ struct cpu_dev {int c_x86_vendor; }; +struct _tlb_table { +unsigned char descriptor; +char tlb_type; +unsigned int entries; +/* unsigned int ways; */ +char info[128 ]; +}; +#define cpu_dev_register (cpu_devX) \ static const struct cpu_dev *const __cpu_dev_##cpu_devX __used \ __attribute__((__section__(".x86_cpu_dev.init"))) = \
@@ -34,4 +42,5 @@ extern const struct cpu_dev *const __x86_cpu_dev_start[],extern void get_cpu_cap (struct cpuinfo_x86 *c);extern void cpu_detect_cache_sizes (struct cpuinfo_x86 *c); +extern void intel_cpu_detect_tlb (struct cpuinfo_x86 *c)__cpuinit ;#endif /* ARCH_X86_CPU_H */
arch/x86/kernel/cpu/intel.c +141 -0
147 lines not on this page · raw message
Signature · 1 line
1.7.5.4
PATCH v6 3/7 x86/flush_tlb: try flush_tlb_single one by one in flush_tlb_range
In reply to Alex Shix86 has no flush_tlb_range support in instruction level. Currently the flush_tlb_range just implemented by flushing all page table. That is not the best solution for all scenarios. In fact, if we just use 'invlpg' to flush few lines from TLB, we can get the performance gain from later remain TLB lines accessing.
But the 'invlpg' instruction costs much of time. Its execution time can compete with cr3 rewriting, and even a bit more on SNB CPU.
So, on a 512 4KB TLB entries CPU, the balance points is at:
(512 - X) * 100ns(assumed TLB refill cost) = X(TLB flush entries) * 100ns(assumed invlpg cost)
Here, X is 256, that is 1/2 of 512 entries.
But with the mysterious CPU pre-fetcher and page miss handler Unit, the assumed TLB refill cost is far lower then 100ns in sequential access. And 2 HT siblings in one core makes the memory access more faster if they are accessing the same memory. So, in the patch, I just do the change when the target entries is less than 1/16 of whole active tlb entries. Actually, I have no data support for the percentage '1/16', so any suggestions are welcomed.
As to hugetlb, guess due to smaller page table, and smaller active TLB entries, I didn't see benefit via my benchmark, so no optimizing now.
My macro benchmark show in ideal scenarios, the performance improves 70 percent in reading. And in worst scenario, the reading/writing performance is similar with unpatched 3.4-rc4 kernel.
Here is the reading data on my 2P * 4cores *HT NHM EP machine, with THP 'always':
multi thread testing, '-t' paramter is thread number:
with patch unpatched 3.4-rc4
./mprotect -t 1 14ns 24ns ./mprotect -t 2 13ns 22ns ./mprotect -t 4 12ns 19ns ./mprotect -t 8 14ns 16ns ./mprotect -t 16 28ns 26ns ./mprotect -t 32 54ns 51ns ./mprotect -t 128 200ns 199ns
Single process with sequencial flushing and memory accessing:
with patch unpatched 3.4-rc4
./mprotect 7ns 11ns
./mprotect -p 4096 -l 8 -n 10240
21ns 21ns
I also tried other benchmarks on Intel core2/NHM/SNB EP and NHM EX machine. No clear performance change on specjbb2005 with openjdk, and oltp reading.
--- arch/x86/include/asm/paravirt.h | 5 +- arch/x86/include/asm/paravirt_types.h | 3 +- arch/x86/include/asm/tlbflush.h | 23 +++----- arch/x86/include/asm/uv/uv.h | 5 +- arch/x86/mm/tlb.c | 97 +++++++++++++++++++++++++++------ arch/x86/platform/uv/tlb_uv.c | 6 +- arch/x86/xen/mmu.c | 9 ++-- include/trace/events/xen.h | 12 +++-- 8 files changed, 113 insertions(+), 47 deletions(-)
arch/x86/include/asm/paravirt.h +3 -2
@@ -397,9 +397,10 @@ static inline void __flush_tlb_single(unsigned long addr)static inline void flush_tlb_others (const struct cpumask *cpumask,struct mm_struct *mm, -unsigned long va) +unsigned long start, +unsigned long end) { -PVOP_VCALL3 (pv_mmu_ops.flush_tlb_others, cpumask, mm, va); +PVOP_VCALL4 (pv_mmu_ops.flush_tlb_others, cpumask, mm, start, end); }static inline int paravirt_pgd_alloc (struct mm_struct *mm)
arch/x86/include/asm/paravirt_types.h +2 -1
@@ -250,7 +250,8 @@ struct pv_mmu_ops {void (*flush_tlb_single)(unsigned long addr);void (*flush_tlb_others)(const struct cpumask *cpus,struct mm_struct *mm, -unsigned long va); +unsigned long start, +unsigned long end);/* Hooks for allocating and freeing a pagetable top-level */ int (*pgd_alloc)(struct mm_struct *mm);
arch/x86/include/asm/tlbflush.h +9 -14
@@ -73,14 +73,10 @@ static inline void __flush_tlb_one(unsigned long addr)* - flush_tlb_page(vma, vmaddr) flushes one page * - flush_tlb_range(vma, start, end) flushes a range of pages * - flush_tlb_kernel_range(start, end) flushes a range of kernel pages -* - flush_tlb_others(cpumask, mm, va) flushes TLBs on other cpus +* - flush_tlb_others(cpumask, mm, start, end) flushes TLBs on other cpus * * ..but the i386 has somewhat limited tlb flushing capabilities, * and page-granular flushes are available only on i486 and up. -* -* x86-64 can only flush individual pages or full VMs. For a range flush -* we always do the full VM. Might be worth trying if for a small -* range a few INVLPGs in a row are a win. */ #ifndef CONFIG_SMP
@@ -111,7 +107,8 @@ static inline void flush_tlb_range(struct vm_area_struct *vma,static inline void native_flush_tlb_others (const struct cpumask *cpumask,struct mm_struct *mm, -unsigned long va) +unsigned long start, +unsigned long end) { }
@@ -129,17 +126,14 @@ extern void flush_tlb_all(void);extern void flush_tlb_current_task (void );extern void flush_tlb_mm (struct mm_struct *);extern void flush_tlb_page (struct vm_area_struct *,unsigned long ); +extern void flush_tlb_range (struct vm_area_struct *vma, +unsigned long start,unsigned long end);#define flush_tlb () flush_tlb_current_task() -static inline void flush_tlb_range (struct vm_area_struct *vma, -unsigned long start,unsigned long end) -{ -flush_tlb_mm (vma->vm_mm); -} -void native_flush_tlb_others (const struct cpumask *cpumask, -struct mm_struct *mm,unsigned long va); +struct mm_struct *mm, +unsigned long start,unsigned long end);#define TLBSTATE_OK 1#define TLBSTATE_LAZY 2
@@ -159,7 +153,8 @@ static inline void reset_lazy_tlbstate(void)#endif /* SMP */ #ifndef CONFIG_PARAVIRT -#define flush_tlb_others (mask, mm, va) native_flush_tlb_others(mask, mm, va) +#define flush_tlb_others (mask, mm, start, end) \ + native_flush_tlb_others(mask, mm, start, end)#endif static inline void flush_tlb_kernel_range(unsigned long start,
arch/x86/include/asm/uv/uv.h +3 -2
@@ -15,7 +15,8 @@ extern void uv_nmi_init(void);extern void uv_system_init (void );extern const struct cpumask *uv_flush_tlb_others (const struct cpumask *cpumask,struct mm_struct *mm, -unsigned long va, +unsigned long start, +unsigned end,unsigned int cpu);#else /* X86_UV */
@@ -26,7 +27,7 @@ static inline void uv_cpu_init(void) { }static inline void uv_system_init (void ) { }static inline const struct cpumask *uv_flush_tlb_others (const struct cpumask *cpumask,struct mm_struct *mm, -unsigned long va,unsigned int cpu) +unsigned long start,unsigned long end,unsigned int cpu) {return cpumask; }#endif /* X86_UV */
arch/x86/mm/tlb.c +81 -16
168 lines not on this page · raw message
arch/x86/platform/uv/tlb_uv.c +3 -3
@@ -1068,8 +1068,8 @@ static int set_distrib_bits(struct cpumask *flush_mask, struct bau_control *bcp,* done. The returned pointer is valid till preemption is re-enabled. */ const struct cpumask *uv_flush_tlb_others (const struct cpumask *cpumask, -struct mm_struct *mm,unsigned long va, -unsigned int cpu) +struct mm_struct *mm,unsigned long start, +unsigned end,unsigned int cpu) {int locals =0 ;int remotes =0 ;
@@ -1112,7 +1112,7 @@ const struct cpumask *uv_flush_tlb_others(const struct cpumask *cpumask,record_send_statistics (stat, locals, hubs, remotes, bau_desc); - bau_desc->payload.address = va; + bau_desc->payload.address = start; bau_desc->payload.sending_cpu = cpu;/* * uv_flush_send_and_wait returns 0 if all cpu's were messaged,
arch/x86/xen/mmu.c +5 -4
@@ -1244,7 +1244,8 @@ static void xen_flush_tlb_single(unsigned long addr) }static void xen_flush_tlb_others (const struct cpumask *cpus, -struct mm_struct *mm,unsigned long va) +struct mm_struct *mm,unsigned long start, +unsigned long end) {struct {struct mmuext_op op;
@@ -1256,7 +1257,7 @@ static void xen_flush_tlb_others(const struct cpumask *cpus, } *args;struct multicall_space mcs; -trace_xen_mmu_flush_tlb_others (cpus, mm, va); +trace_xen_mmu_flush_tlb_others (cpus, mm, start, end);if (cpumask_empty (cpus))return ;/* nothing to do */
@@ -1269,11 +1270,11 @@ static void xen_flush_tlb_others(const struct cpumask *cpus,cpumask_and (to_cpumask (args->mask), cpus, cpu_online_mask);cpumask_clear_cpu (smp_processor_id (),to_cpumask (args->mask)); -if (va ==TLB_FLUSH_ALL ) { +if (start ==TLB_FLUSH_ALL ) { args->op.cmd =MMUEXT_TLB_FLUSH_MULTI ; }else { args->op.cmd =MMUEXT_INVLPG_MULTI ; - args->op.arg1.linear_addr = va; + args->op.arg1.linear_addr = start; }MULTI_mmuext_op (mcs.mc, &args->op,1 ,NULL ,DOMID_SELF );
include/trace/events/xen.h +7 -5
@@ -397,18 +397,20 @@ TRACE_EVENT(xen_mmu_flush_tlb_single,TRACE_EVENT (xen_mmu_flush_tlb_others, TP_PROTO(const struct cpumask *cpus, struct mm_struct *mm, - unsigned long addr), -TP_ARGS (cpus, mm, addr), + unsigned long addr, unsigned long end), +TP_ARGS (cpus, mm, addr, end),TP_STRUCT__entry ( __field(unsigned, ncpus) __field(struct mm_struct *, mm) __field(unsigned long, addr) + __field(unsigned long, end) ), TP_fast_assign(__entry->ncpus = cpumask_weight(cpus); __entry->mm = mm; - __entry->addr = addr), -TP_printk ("ncpus %d mm %p addr %lx" , - __entry->ncpus, __entry->mm, __entry->addr) + __entry->addr = addr, + __entry->end = end), +TP_printk ("ncpus %d mm %p addr %lx, end %lx" , + __entry->ncpus, __entry->mm, __entry->addr, __entry->end) ); TRACE_EVENT(xen_mmu_write_cr3,
Signature · 1 line
1.7.5.4
PATCH v6 4/7 x86/tlb: fall back to flush all when meet a THP large page
In reply to Alex ShiWe don't need to flush large pages by PAGE_SIZE step, that just waste time. and actually, large page don't need 'invlpg' optimizing according to our macro benchmark. So, just flush whole TLB is enough for them.
The following result is tested on a 2CPU * 4cores * 2HT NHM EP machine, with THP 'always' setting.
Multi-thread testing, '-t' paramter is thread number:
without this patch with this patch
./mprotect -t 1 14ns 13ns ./mprotect -t 2 13ns 13ns ./mprotect -t 4 12ns 11ns ./mprotect -t 8 14ns 10ns ./mprotect -t 16 28ns 28ns ./mprotect -t 32 54ns 52ns ./mprotect -t 128 200ns 200ns
--- arch/x86/mm/tlb.c | 34 ++++++++++++++++++++++++++++++++++ 1 files changed, 34 insertions(+), 0 deletions(-)
arch/x86/mm/tlb.c +34 -0
@@ -316,12 +316,42 @@ void flush_tlb_mm(struct mm_struct *mm)#define FLUSHALL_BAR 16 +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +static inline int has_large_page (struct mm_struct *mm, +unsigned long start,unsigned long end) +{ +pgd_t *pgd; +pud_t *pud; +pmd_t *pmd; +unsigned long addr =ALIGN (start,HPAGE_SIZE ); +for (; addr < end; addr +=HPAGE_SIZE ) { + pgd =pgd_offset (mm, addr); +if (likely (!pgd_none (*pgd))) { + pud =pud_offset (pgd, addr); +if (likely (!pud_none (*pud))) { + pmd =pmd_offset (pud, addr); +if (likely (!pmd_none (*pmd))) +if (pmd_large (*pmd)) +return 1 ; + } + } + } +return 0 ; +} +#else +static inline int has_large_page (struct mm_struct *mm, +unsigned long start,unsigned long end) +{ +return 0 ; +} +#endif void flush_tlb_range (struct vm_area_struct *vma,unsigned long start,unsigned long end) {struct mm_struct *mm;if (!cpu_has_invlpg || vma->vm_flags &VM_HUGETLB ) { +flush_all:flush_tlb_mm (vma->vm_mm);return ; }
@@ -344,6 +374,10 @@ void flush_tlb_range(struct vm_area_struct *vma,if ((end - start)/PAGE_SIZE > act_entries/FLUSHALL_BAR )local_flush_tlb ();else { +if (has_large_page (mm, start, end)) { +preempt_enable (); + goto flush_all; + }for (addr = start; addr <= end; addr +=PAGE_SIZE )__flush_tlb_single (addr);
Signature · 1 line
1.7.5.4
PATCH v6 5/7 x86/tlb: add tlb_flushall_shift for specific CPU
In reply to Alex ShiTesting show different CPU type(micro architectures and NUMA mode) has different balance points between the TLB flush all and multiple invlpg. And there also has cases the tlb flush change has no any help.
This patch give a interface to let x86 vendor developers have a chance to set different shift for different CPU type.
like some machine in my hands, balance points is 16 entries on Romely-EP; while it is at 8 entries on Bloomfield NHM-EP; and is 256 on IVB mobile CPU. but on model 15 core2 Xeon using invlpg has nothing help.
For untested machine, do a conservative optimization, same as NHM CPU.
--- arch/x86/include/asm/processor.h | 2 ++ arch/x86/kernel/cpu/common.c | 14 ++++++++++++-- arch/x86/kernel/cpu/intel.c | 34 ++++++++++++++++++++++++++++++++++ arch/x86/mm/tlb.c | 7 +++---- include/asm-generic/tlb.h | 3 ++- 5 files changed, 53 insertions(+), 7 deletions(-)
arch/x86/include/asm/processor.h +2 -0
@@ -72,6 +72,8 @@ extern u16 __read_mostly tlb_lli_4m[NR_INFO];extern u16 __read_mostly tlb_lld_4k[NR_INFO ];extern u16 __read_mostly tlb_lld_2m[NR_INFO ];extern u16 __read_mostly tlb_lld_4m[NR_INFO ]; +extern s8 __read_mostly tlb_flushall_shift; +/* * CPU type and hardware bug flags. Kept separately for each CPU. * Members of this structure are referenced in head.S, so think twice
arch/x86/kernel/cpu/common.c +12 -2
@@ -459,16 +459,26 @@ u16 __read_mostly tlb_lld_4k[NR_INFO];u16 __read_mostly tlb_lld_2m[NR_INFO ];u16 __read_mostly tlb_lld_4m[NR_INFO ]; +/* +* tlb_flushall_shift shows the balance point in replacing cr3 write +* with multiple 'invlpg'. It will do this replacement when +* flush_tlb_lines <= active_lines/2^tlb_flushall_shift. +* If tlb_flushall_shift is -1, means the replacement will be disabled. +*/ +s8 __read_mostly tlb_flushall_shift; +void __cpuinit cpu_detect_tlb (struct cpuinfo_x86 *c) {if (c->x86_vendor ==X86_VENDOR_INTEL )intel_cpu_detect_tlb (c);printk (KERN_INFO "Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n" \ -"Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d\n" , +"Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d\n" \ +"tlb_flushall_shift is 0x%x\n" , tlb_lli_4k[ENTRIES ], tlb_lli_2m[ENTRIES ], tlb_lli_4m[ENTRIES ], tlb_lld_4k[ENTRIES ], - tlb_lld_2m[ENTRIES ], tlb_lld_4m[ENTRIES ]); + tlb_lld_2m[ENTRIES ], tlb_lld_4m[ENTRIES ], + tlb_flushall_shift); }void __cpuinit detect_ht (struct cpuinfo_x86 *c)
arch/x86/kernel/cpu/intel.c +34 -0
@@ -610,6 +610,39 @@ static void __cpuinit intel_tlb_lookup(const unsigned char desc) } } +static void __cpuinit intel_tlb_flushall_shift_set (struct cpuinfo_x86 *c) +{ +if (!cpu_has_invlpg) { + tlb_flushall_shift =-1 ; +return ; + } +switch ((c->x86 <<8 ) + c->x86_model) { +case 0x60f :/* original 65 nm celeron/pentium/core2/xeon, "Merom"/"Conroe" */ +case 0x616 :/* single-core 65 nm celeron/core2solo "Merom-L"/"Conroe-L" */ +case 0x617 :/* current 45 nm celeron/core2/xeon "Penryn"/"Wolfdale" */ +case 0x61d :/* six-core 45 nm xeon "Dunnington" */ + tlb_flushall_shift =-1 ; +break ; +case 0x61a :/* 45 nm nehalem, "Bloomfield" */ +case 0x61e :/* 45 nm nehalem, "Lynnfield" */ +case 0x625 :/* 32 nm nehalem, "Clarkdale" */ +case 0x62c :/* 32 nm nehalem, "Gulftown" */ +case 0x62e :/* 45 nm nehalem-ex, "Beckton" */ +case 0x62f :/* 32 nm Xeon E7 */ + tlb_flushall_shift =6 ; +break ; +case 0x62a :/* SandyBridge */ +case 0x62d :/* SandyBridge, "Romely-EP" */ + tlb_flushall_shift =5 ; +break ; +case 0x63a :/* Ivybridge */ + tlb_flushall_shift =1 ; +break ; +default : + tlb_flushall_shift =6 ; + } +} +void __cpuinit intel_cpu_detect_tlb (struct cpuinfo_x86 *c) {int i, j, n;
@@ -630,6 +663,7 @@ void __cpuinit intel_cpu_detect_tlb(struct cpuinfo_x86 *c)for (j =1 ; j <16 ; j++)intel_tlb_lookup (desc[j]); } +intel_tlb_flushall_shift_set (c); }static const struct cpu_dev __cpuinitconst intel_cpu_dev = {
arch/x86/mm/tlb.c +3 -4
@@ -314,8 +314,6 @@ void flush_tlb_mm(struct mm_struct *mm)preempt_enable (); } -#define FLUSHALL_BAR 16 -#ifdef CONFIG_TRANSPARENT_HUGEPAGE static inline int has_large_page (struct mm_struct *mm,unsigned long start,unsigned long end)
@@ -350,7 +348,7 @@ void flush_tlb_range(struct vm_area_struct *vma, {struct mm_struct *mm; -if (!cpu_has_invlpg || vma->vm_flags &VM_HUGETLB ) { +if (vma->vm_flags &VM_HUGETLB || tlb_flushall_shift ==-1 ) { flush_all:flush_tlb_mm (vma->vm_mm);return ;
@@ -371,7 +369,8 @@ flush_all: act_entries = tlb_entries > mm->total_vm ? mm->total_vm : tlb_entries; -if ((end - start)/PAGE_SIZE > act_entries/FLUSHALL_BAR ) +if ((end - start) >>PAGE_SHIFT > + act_entries >> tlb_flushall_shift)local_flush_tlb ();else {if (has_large_page (mm, start, end)) {
include/asm-generic/tlb.h +2 -1
@@ -113,7 +113,8 @@ static inline int tlb_fast_mode(struct mmu_gather *tlb)void tlb_gather_mmu (struct mmu_gather *tlb,struct mm_struct *mm,bool fullmm);void tlb_flush_mmu (struct mmu_gather *tlb); -void tlb_finish_mmu (struct mmu_gather *tlb,unsigned long start,unsigned long end); +void tlb_finish_mmu (struct mmu_gather *tlb,unsigned long start, +unsigned long end);int __tlb_remove_page (struct mmu_gather *tlb,struct page *page);/* tlb_remove_page
Signature · 1 line
1.7.5.4
PATCH v6 6/7 x86/tlb: enable tlb flush range support for generic mmu on x86
In reply to Alex ShiNot every tlb_flush execution moment is really need to evacuate all TLB entries, like in munmap, just few 'invlpg' is better for whole process performance, since it leaves most of TLB entries for later accessing.
All of tlb interfaces in mm/memory.c are reused by all architectures except few of them for generic mmu_gather were protected under HAVE_GENERIC_MMU_GATHER. So, for x86, just re-implement 'tlb_flush' is enough.
This patch also rewrite flush_tlb_range for 2 purposes:
1, split it out to get flush_blt_mm_range function.
2, clean up to reduce line breaking, thanks for Borislav's input.
Thanks for Peter Zijlstra time and time reminder for multiple architecture code safe!
--- arch/x86/include/asm/tlb.h | 9 +++- arch/x86/include/asm/tlbflush.h | 9 +++ arch/x86/mm/tlb.c | 117 ++++++++++++++++++--------------------- include/asm-generic/tlb.h | 2 + mm/memory.c | 9 +++ 5 files changed, 81 insertions(+), 65 deletions(-)
arch/x86/include/asm/tlb.h +8 -1
@@ -4,7 +4,14 @@#define tlb_start_vma (tlb, vma) do { } while (0)#define tlb_end_vma (tlb, vma) do { } while (0)#define __tlb_remove_tlb_entry (tlb, ptep, address) do { } while (0) -#define tlb_flush (tlb) flush_tlb_mm((tlb)->mm) + +#define tlb_flush (tlb) \ +{ \ + if (tlb->fullmm == 0) \ + flush_tlb_mm_range(tlb->mm, tlb->start, tlb->end, 0UL); \ + else \ + flush_tlb_mm_range(tlb->mm, 0UL, TLB_FLUSH_ALL, 0UL); \ +}#include <asm-generic/tlb.h>
arch/x86/include/asm/tlbflush.h +9 -0
@@ -105,6 +105,13 @@ static inline void flush_tlb_range(struct vm_area_struct *vma,__flush_tlb (); } +static inline void flush_tlb_mm_range (struct vm_area_struct *vma, +unsigned long start,unsigned long end,unsigned long vmflag) +{ +if (mm == current->active_mm) +__flush_tlb (); +} + static inline void native_flush_tlb_others(const struct cpumask *cpumask, struct mm_struct *mm, unsigned long start,
@@ -128,6 +135,8 @@ extern void flush_tlb_mm(struct mm_struct *);extern void flush_tlb_page (struct vm_area_struct *,unsigned long );extern void flush_tlb_range (struct vm_area_struct *vma,unsigned long start,unsigned long end); +extern void flush_tlb_mm_range (struct mm_struct *mm,unsigned long start, +unsigned long end,unsigned long vmflag);#define flush_tlb () flush_tlb_current_task()
arch/x86/mm/tlb.c +53 -64
143 lines not on this page · raw message
include/asm-generic/tlb.h +2 -0
@@ -86,6 +86,8 @@ struct mmu_gather {#ifdef CONFIG_HAVE_RCU_TABLE_FREE struct mmu_table_batch *batch;#endif +unsigned long start; +unsigned long end; unsigned int need_flush : 1, /* Did free PTEs */ fast_mode : 1; /* No batching */
mm/memory.c +9 -0
@@ -206,6 +206,8 @@ void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, bool fullmm) tlb->mm = mm; tlb->fullmm = fullmm; + tlb->start =-1UL ; + tlb->end =0 ; tlb->need_flush =0 ; tlb->fast_mode = (num_possible_cpus () ==1 ); tlb->local.next =NULL ;
@@ -248,6 +250,8 @@ void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long e {struct mmu_gather_batch *batch, *next; + tlb->start = start; + tlb->end = end;tlb_flush_mmu (tlb);/* keep the page table cache within bounds */
@@ -1204,6 +1208,11 @@ again:*/ if (force_flush) { force_flush =0 ; + +#ifdef HAVE_GENERIC_MMU_GATHER + tlb->start = addr; + tlb->end = end; +#endif tlb_flush_mmu (tlb);if (addr != end) goto again;
Signature · 1 line
1.7.5.4
PATCH v6 7/7 x86/tlb: add tlb_flushall_shift knob into debugfs
In reply to Alex Shikernel will replace cr3 rewrite with invlpg when
tlb_flush_entries <= active_tlb_entries / 2^tlb_flushall_factor
if tlb_flushall_factor is -1, kernel won't do this replacement.
User can modify its value according to specific CPU/applications.
Thanks for Borislav providing the help message ofCONFIG_DEBUG_TLBFLUSH.
--- arch/x86/Kconfig.debug | 19 +++++++++++++++++ arch/x86/mm/tlb.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 0 deletions(-)
arch/x86/Kconfig.debug +19 -0
@@ -129,6 +129,25 @@ config DOUBLEFAULT option saves about 4k and might cause you much additional grey hair. +config DEBUG_TLBFLUSH + bool "Set upper limit of TLB entries to flush one-by-one" + depends on DEBUG_KERNEL && (X86_64 || X86_INVLPG) + ---help--- + + X86-only for now. + + This option allows the user to tune the amount of TLB entries the + kernel flushes one-by-one instead of doing a full TLB flush. In + certain situations, the former is cheaper. This is controlled by the + tlb_flushall_shift knob under /sys/kernel/debug/x86. If you set it + to -1, the code flushes the whole TLB unconditionally. Otherwise, + for positive values of it, the kernel will use single TLB entry + invalidating instructions according to the following formula: + + flush_entries <= active_tlb_entries / 2^tlb_flushall_shift + + If in doubt, say "N". + config IOMMU_DEBUG bool "Enable IOMMU debugging" depends on GART_IOMMU && DEBUG_KERNEL
arch/x86/mm/tlb.c +51 -0
60 lines not on this page · raw message
Signature · 1 line
1.7.5.4
Quoted 9 lines · Alex Shi
Thanks Peter Z, Peter Anvin, Nick Piggin, and many others' comments!
The main change of this version is on generic
mmu_gathercode. It was tested with arm cross-compiler.Thanks Rongjie's testing, that show the real case performance gain.
Alex Shi
Quoted 7 lines
[PATCH v6 1/7] x86/tlb: unify
TLB_FLUSH_ALLdefinition [PATCH v6 2/7] x86/tlb_info: get last level TLB entry number of CPU [PATCH v6 3/7] x86/flush_tlb: tryflush_tlb_singleone by one in [PATCH v6 4/7] x86/tlb: fall back to flush all when meet a THP large [PATCH v6 5/7] x86/tlb: addtlb_flushall_shiftfor specific CPU [PATCH v6 6/7] x86/tlb: enable tlb flush range support for generic [PATCH v6 7/7] x86/tlb: addtlb_flushall_shiftknob into debugfs
Here is the macro benchmark to measure munmap change:
tlb_flushall_shift = -1
[alexs@lkp-ne04 tlb]$
[alexs@lkp-ne04 tlb]$ for t in echo 4 8 16 ; do echo "=============== t = $t ===================="; for i in echo 8 16 32 ; do sudo ./munmap -t $t -n $i; done done =============== t = 4 ====================
munmap use 164ms 5032ns/time, memory access uses 81605 times/thread/ms, cost 12ns/time
munmap use 86ms 5251ns/time, memory access uses 83378 times/thread/ms, cost 11ns/time
munmap use 46ms 5642ns/time, memory access uses 87212 times/thread/ms, cost 11ns/time
=============== t = 8 ====================
munmap use 197ms 6036ns/time, memory access uses 69295 times/thread/ms, cost 14ns/time
munmap use 96ms 5896ns/time, memory access uses 71895 times/thread/ms, cost 13ns/time
munmap use 62ms 7608ns/time, memory access uses 83895 times/thread/ms, cost 11ns/time
=============== t = 16 ====================
munmap use 274ms 8367ns/time, memory access uses 37860 times/thread/ms, cost 26ns/time
munmap use 139ms 8543ns/time, memory access uses 38137 times/thread/ms, cost 26ns/time
munmap use 74ms 9033ns/time, memory access uses 38349 times/thread/ms, cost 26ns/time
[alexs@lkp-ne04 tlb]$
[alexs@lkp-ne04 tlb]$tlb_flushall_shift = 5
[alexs@lkp-ne04 tlb]$ for t in echo 4 8 16 ; do echo "=============== t = $t ===================="; for i in echo 8 16 32 ; do sudo ./munmap -t $t -n $i; done done =============== t = 4 ====================
munmap use 212ms 6485ns/time, memory access uses 114003 times/thread/ms, cost 8ns/time
munmap use 130ms 7972ns/time, memory access uses 110725 times/thread/ms, cost 9ns/time
munmap use 45ms 5581ns/time, memory access uses 87866 times/thread/ms, cost 11ns/time
=============== t = 8 ====================
munmap use 253ms 7734ns/time, memory access uses 94578 times/thread/ms, cost 10ns/time
munmap use 147ms 9012ns/time, memory access uses 83851 times/thread/ms, cost 11ns/time
munmap use 63ms 7713ns/time, memory access uses 87473 times/thread/ms, cost 11ns/time
=============== t = 16 ====================
munmap use 369ms 11284ns/time, memory access uses 38854 times/thread/ms, cost 25ns/time
munmap use 264ms 16131ns/time, memory access uses 37870 times/thread/ms, cost 26ns/time
munmap use 73ms 8981ns/time, memory access uses 38309 times/thread/ms, cost 26ns/time
The munmap.c file is here:
---
/*
munmap.c
This is a macrobenchmark for TLB flush range testing.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Copyright (C) Intel 2012
Coypright Alex Shi [email protected]
gcc -o munmap munmap.c -lrt -lpthread -O2
#perf stat -e r881,r882,r884 -e r801,r802,r810,r820,r840,r880,r807 -e rc01 -e r4901,r4902,r4910,r4920,r4940,r4980 -e r5f01 -e rbd01,rdb20 -e r4f02 -e r8004,r8201,r8501,r8502,r8504,r8510,r8520,r8540,r8580 -e rae01,rc820,rc102,rc900 -e r8600 -e rcb10 ./munmap
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/mman.h>
#include <time.h>
#include <sys/types.h>
#include <pthread.h>
#define FILE_SIZE (1024*1024*1024)
#define PAGE_SIZE 4096
#define HPAGE_SIZE 4096*512
#ifndef MAP_HUGETLB
#define MAP_HUGETLB 0x40000
#endif
long getnsec(clockid_t clockid) {
struct timespec ts;
if (clock_gettime(clockid, &ts) == -1)
perror("clock_gettime failed");
return (long) ts.tv_sec * 1000000000 + (long) ts.tv_nsec;
}//data for threads
struct data{
int *readp;
void *startaddr;
int rw;
int loop;
};
volatile int * threadstart;
//thread for memory accessing
void *accessmm(void *data){
struct data *d = data;
long *actimes;
char x;
int i, k;
int randn[PAGE_SIZE];
for (i=0;i<PAGE_SIZE; i++)
randn[i] = rand();
actimes = malloc(sizeof(long));
while (*threadstart == 0 )
usleep(1);
if (d->rw == 0)
for (*actimes=0; *threadstart == 1; (*actimes)++)
for (k=0; k < *d->readp; k++)
x = *(volatile char *)(d->startaddr + randn[k]%FILE_SIZE);
else
for (*actimes=0; *threadstart == 1; (*actimes)++)
for (k=0; k < *d->readp; k++)
*(char *)(d->startaddr + randn[k]%FILE_SIZE) = 1;
return actimes;
}int main(int argc, char *argv[])
{
static char optstr[] = "n:l:p:w:ht:";
int n = 8; /* default flush entries number */
int l = 1; /* default loop times */
int p = 512; /* default accessed page number, after munmap */
int er = 0, rw = 0, h = 0, t = 0; /* d: debug; h: use huge page; t thread number */
int pagesize = PAGE_SIZE; /*default for regular page */
volatile char x;
long protindex = 0;
int i, j, k, c;
void *m1, *startaddr;
unsigned long *startaddr2[1024*512];
volatile void *tempaddr;
clockid_t clockid = CLOCK_MONOTONIC;
unsigned long start, stop, mptime, actime;
int randn[PAGE_SIZE];
pthread_t pid[1024];
void * res;
struct data data;
for (i=0;i<PAGE_SIZE; i++)
randn[i] = rand();
while ((c = getopt(argc, argv, optstr)) != EOF)
switch (c) {
case 'n':
n = atoi(optarg);
break;
case 'p':
p = atoi(optarg);
break;
case 'h':
h = 1;
break;
case 'w':
rw = atoi(optarg);
break;
case 't':
t = atoi(optarg);
break;
case '?':
er = 1;
break;
}
if (er) {
printf("usage: %s %s\n", argv[0], optstr);
exit(1);
}
//printf("my pid is %d n=%d p=%d t=%d\n", getpid(), n, p, t);
if (h == 0){
startaddr = mmap(0, FILE_SIZE, PROT_READ|PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
for (j = 0; j < FILE_SIZE/PAGE_SIZE/n; j++) {
startaddr2[j] = mmap(0, PAGE_SIZE*n, PROT_READ|PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
if (startaddr2[j] == MAP_FAILED) {
perror("mmap");
exit(1);
}
*startaddr2[j] = 1;
}
pagesize = PAGE_SIZE;
} else {
startaddr = mmap(0, FILE_SIZE, PROT_READ|PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED | MAP_HUGETLB, -1, 0);
for (j = 0; j < FILE_SIZE/HPAGE_SIZE/n; j++) {
startaddr2[j] = mmap(0, HPAGE_SIZE*n, PROT_READ|PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
if (startaddr2[j] == MAP_FAILED) {
perror("mmap");
exit(1);
}
*startaddr2[j] = 1;
}
pagesize = HPAGE_SIZE;
}
if (startaddr == MAP_FAILED) {
perror("mmap");
exit(1);
}
start = getnsec(clockid);
//access whole memory, will generate many page faults
for (tempaddr = startaddr; tempaddr < startaddr + FILE_SIZE; tempaddr += pagesize)
memset((char *)tempaddr, 0, 1);
stop = getnsec(clockid);// printf("get 256K pages with one byte writing uses %lums, %luns/time \n",
// (stop - start)/1000000, (stop-start)*pagesize/FILE_SIZE);
//thread created, and goes to sleep
threadstart = malloc(sizeof(int));
*threadstart = 0;
data.readp = &p; data.startaddr = startaddr; data.rw = rw; data.loop = l;
for (i=0; i< t; i++)
if(pthread_create(&pid[i], NULL, accessmm, &data))
perror("pthread create");
//wait for randn[] filling.
if (t!=0) sleep(1);
mptime = actime = 0;
if (t != 0)
start = getnsec(clockid);
//kick threads, let them running.
*threadstart = 1;
for (j = 0; j < FILE_SIZE/pagesize/n; j++) {
if (t == 0)
start = getnsec(clockid);
if(munmap(startaddr2[j], n*pagesize)==-1) {
perror("munmap");
goto end;
}
if (t == 0) {
stop = getnsec(clockid);
mptime += stop - start;
}
if (t == 0) {
// access p number pages
start = stop;
if (rw == 0)
for (k=0; k < p; k++)
x = *(volatile char *)(startaddr + randn[k]%FILE_SIZE);
else
for (k=0; k < p; k++)
*(char *)(startaddr + randn[k]%FILE_SIZE) = 1;
actime += getnsec(clockid) - start;
}
}
//to avoid accessmm miss *threadstart == 1
usleep(10000);//sleep 10ms
*threadstart = 0;
if (t != 0) {
stop = getnsec(clockid);
mptime += stop - start;
}
//get threads' result.
for (i=0; i< t; i++) {
if (pthread_join(pid[i], &res))
perror("pthread_join");
actime += *(long*)res;
}
l = FILE_SIZE/pagesize/n;
end:
if ( t == 0 )
printf("munmap use %lums %luns/time, memory access uses %lums %luns/time \n",
mptime/1000000, mptime/(l), actime/1000000, actime/p/l);
else
printf("munmap use %lums %luns/time, memory access uses %ld times/thread/ms, cost %ldns/time\n",
mptime/1000000, mptime/(l), actime*p*1000000/t/mptime, mptime*t/(actime*p));
exit(0);
}Added a little more info.
the machine is 2P * 4core* HT NHM EP with 12GB memory, and THP set 'always'.
Quoted 35 lines
Here is the macro benchmark to measure munmap change:
tlb_flushall_shift= -1
[alexs@lkp-ne04 tlb]$
[alexs@lkp-ne04 tlb]$ for t inecho 4 8 16; do echo "=============== t = $t ===================="; for i inecho 8 16 32; do sudo ./munmap -t $t -n $i; done done =============== t = 4 ====================
munmap use 164ms 5032ns/time, memory access uses 81605 times/thread/ms, cost 12ns/time
munmap use 86ms 5251ns/time, memory access uses 83378 times/thread/ms, cost 11ns/time
munmap use 46ms 5642ns/time, memory access uses 87212 times/thread/ms, cost 11ns/time
=============== t = 8 ====================
munmap use 197ms 6036ns/time, memory access uses 69295 times/thread/ms, cost 14ns/time
munmap use 96ms 5896ns/time, memory access uses 71895 times/thread/ms, cost 13ns/time
munmap use 62ms 7608ns/time, memory access uses 83895 times/thread/ms, cost 11ns/time
=============== t = 16 ====================
munmap use 274ms 8367ns/time, memory access uses 37860 times/thread/ms, cost 26ns/time
munmap use 139ms 8543ns/time, memory access uses 38137 times/thread/ms, cost 26ns/time
munmap use 74ms 9033ns/time, memory access uses 38349 times/thread/ms, cost 26ns/time
[alexs@lkp-ne04 tlb]$
[alexs@lkp-ne04 tlb]$tlb_flushall_shift= 5
[alexs@lkp-ne04 tlb]$ for t inecho 4 8 16; do echo "=============== t = $t ===================="; for i inecho 8 16 32; do sudo ./munmap -t $t -n $i; done done =============== t = 4 ====================
munmap use 212ms 6485ns/time, memory access uses 114003 times/thread/ms, cost 8ns/time
munmap use 130ms 7972ns/time, memory access uses 110725 times/thread/ms, cost 9ns/time
munmap use 45ms 5581ns/time, memory access uses 87866 times/thread/ms, cost 11ns/time
=============== t = 8 ====================
munmap use 253ms 7734ns/time, memory access uses 94578 times/thread/ms, cost 10ns/time
munmap use 147ms 9012ns/time, memory access uses 83851 times/thread/ms, cost 11ns/time
munmap use 63ms 7713ns/time, memory access uses 87473 times/thread/ms, cost 11ns/time
=============== t = 16 ====================
munmap use 369ms 11284ns/time, memory access uses 38854 times/thread/ms, cost 25ns/time
munmap use 264ms 16131ns/time, memory access uses 37870 times/thread/ms, cost 26ns/time
munmap use 73ms 8981ns/time, memory access uses 38309 times/thread/ms, cost 26ns/time
Quoted 9 lines · Alex Shi
Thanks Peter Z, Peter Anvin, Nick Piggin, and many others' comments!
The main change of this version is on generic
mmu_gathercode. It was tested with arm cross-compiler.Thanks Rongjie's testing, that show the real case performance gain.
Alex Shi
Quoted 7 lines
[PATCH v6 1/7] x86/tlb: unify
TLB_FLUSH_ALLdefinition [PATCH v6 2/7] x86/tlb_info: get last level TLB entry number of CPU [PATCH v6 3/7] x86/flush_tlb: tryflush_tlb_singleone by one in [PATCH v6 4/7] x86/tlb: fall back to flush all when meet a THP large [PATCH v6 5/7] x86/tlb: addtlb_flushall_shiftfor specific CPU [PATCH v6 6/7] x86/tlb: enable tlb flush range support for generic [PATCH v6 7/7] x86/tlb: addtlb_flushall_shiftknob into debugfs
Anyone like to pick up this patchset? It has benefit not only in theory, but also in real word. And seems more modern CPU has more performance gain.
Thanks
Alex Shi