tlb_flush_range optimization
Subsystems (6)
v2 cover tlb_flush_range optimization
Remove [email protected] and add lkml.
This is the second version of patchset, 3 main changes:
1, move tlb info getting function to cpu vendor specific files, that is friendly for other x86 cpu vendor developers to adding their code
2, add a interface to give the different optimization factor for different cpu type 3, tested 3 intel's cpu families: NHM, SNB, core2 and set the specific optimizing factors for them.
Riel: would you like to update you ack to this version?
Boris: you can enable AMD cpu base on this version.
Happy coding!
and
Comments appreciated!
Alex Shi
PATCH v2 1/5 x86/tlb_info: get last level TLB entry number of CPU
For 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.
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);#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.0.1
PATCH v2 2/5 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 | 19 +++---- 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(+), 43 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 -10
@@ -77,7 +77,7 @@ 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.
@@ -115,7 +115,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) { }
@@ -133,17 +134,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
@@ -163,7 +161,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
@@ -1239,7 +1239,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;
@@ -1251,7 +1252,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 */
@@ -1264,11 +1265,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.0.1
PATCH v2 3/5 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 | 27 +++++++++++++++++++++++++++ 1 files changed, 27 insertions(+), 0 deletions(-)
arch/x86/mm/tlb.c +27 -0
@@ -316,12 +316,35 @@ void flush_tlb_mm(struct mm_struct *mm)#define FLUSHALL_BAR 16 +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; +for (addr = start; 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 ; +} +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 +367,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.0.1
A message that is not in the archive (sent off-list or not yet received): CAJd=RBDi1cJ=HmjoLdMG1hMU5GZNDV5ysXqvCT9PP09rEi342w@mail.gmail.com
Re: [PATH v2 3/5] x86/tlb: fall back to flush all when meet a THP large page
Quoted 117 lines · Hillf Danton
Hi Alex,
Quoted 65 lines · Alex Shi
We don't need to flush large pages by
PAGE_SIZEstep, 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 200nsSigned-off-by Alex Shi <[email protected]>---
arch/x86/mm/tlb.c | 27 +++++++++++++++++++++++++++1 files changed, 27 insertions(+), 0 deletions(-)arch/x86/mm/tlb.c +23
-0@@ -316,12 +316,35 @@ void flush_tlb_mm(struct mm_struct *mm) #define FLUSHALL_BAR 16 +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; + for (addr = start; 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; +} + 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 +367,10 @@ void flush_tlb_range(struct vm_area_struct *vma, if ((end - start)/PAGE_SIZE > act_entries/FLUSHALL_BAR)arch/x86/mm/tlb.c void flush_tlb_range(struct vm_area_struct *vma,
Quoted 10 lines
arch/x86/mm/tlb.c +4
-1local_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); --Perhaps huge pages could be handled alternatively, tho dunno the point to flush tlb manually.
x86-mm-tlb.c Fri May 4 20:01:36 2012 renamed from x86-mm-tlb.c Fri May 4 19:53:40 2012 +2
-1@@ -2,7 +2,7 @@ void flush_tlb_range(struct vm_area_stru { struct mm_struct *mm; - if (!cpu_has_invlpg || vma->vm_flags & VM_HUGETLB) { + if (!cpu_has_invlpg) { flush_tlb_mm(vma->vm_mm); return; }@@ -13,6 +13,7 @@ void flush_tlb_range(struct vm_area_stru if (current->mm) { unsigned long addr, vmflag = vma->vm_flags; unsigned act_entries, tlb_entries = 0; + unsigned long pg_sz; if (vmflag & VM_EXEC) tlb_entries = tlb_lli_4k[ENTRIES];@@ -22,11 +23,15 @@ void flush_tlb_range(struct vm_area_struact_entries =
tlb_entries>mm->total_vm?mm->total_vm:tlb_entries;- if ((end - start)/PAGE_SIZE > act_entries/FLUSHALL_BAR)
+pg_sz= (transparent_hugepage_enabled(vma)||
+is_vm_hugetlb_page(vma)) ?
+HPAGE_SIZE:PAGE_SIZE;
+
+ if ((end - start)/pg_sz > act_entries/FLUSHALL_BAR)local_flush_tlb();
else {
for (addr = start; addr <= end;
- addr +=PAGE_SIZE)
+ addr +=pg_sz)__flush_tlb_single(addr);
Quoted 2 lines
if (cpumask_any_but(mm_cpumask(mm), --
���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m����������?�I�
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATH v2 3/5] x86/tlb: fall back to flush all when meet a THP large page
Quoted 10 lines · Alex Shi
Quoted 8 lines · Hillf Danton
Hi Alex,
if (vmflag &
VM_EXEC)tlb_entries= tlb_lli_4k[ENTRIES];(quoted diff) +0
-0@@ -22,11 +23,15 @@ void flush_tlb_range(struct vm_area_stru
act_entries=tlb_entries>mm->total_vm?mm->total_vm:tlb_entries;
Quoted 10 lines
Quoted 6 lines
- if ((end - start)/PAGE_SIZE > act_entries/FLUSHALL_BAR)
+pg_sz= (transparent_hugepage_enabled(vma)||
+is_vm_hugetlb_page(vma)) ?
+HPAGE_SIZE:PAGE_SIZE;
+
+ if ((end - start)/pg_sz > act_entries/FLUSHALL_BAR)Your patch should base on the second patch that expel the HUGETLB page.
The diff is only for showing idea.
Quoted 1 line
But the key fact is I did not find invlpg is help on HUGETLB.
Yep, I only assume flushing huge TLB is not far from regular TLB. ���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m����������?�I�
PATCH v2 4/5 x86/tlb: add tlb flush all factor for specific CPUs
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 maybe 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 factors 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; but on model 15 core2 Xeon using invlpg has nothing help.
For untested machine, no optimization now.
--- arch/x86/include/asm/processor.h | 2 ++ arch/x86/kernel/cpu/common.c | 13 +++++++++++-- arch/x86/kernel/cpu/intel.c | 30 ++++++++++++++++++++++++++++++ arch/x86/mm/tlb.c | 8 ++++---- 4 files changed, 47 insertions(+), 6 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 u16 __read_mostly tlb_flushall_factor; +/* * 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 +11 -2
@@ -459,16 +459,25 @@ 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_factor shows the balance point in replacing cr3 write with +* multiple 'invlpg'. Different CPU type has different this value, which +* get from a macro benchmark named mproctect.c that published in lkml. +*/ +u16 __read_mostly tlb_flushall_factor; +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_factor is 1/%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 ]); + tlb_lld_2m[ENTRIES ], tlb_lld_4m[ENTRIES ], + tlb_flushall_factor); }void __cpuinit detect_ht (struct cpuinfo_x86 *c)
arch/x86/kernel/cpu/intel.c +30 -0
@@ -610,6 +610,35 @@ void intel_tlb_lookup(const unsigned char desc) } } +void intel_tlb_flushall_factor_set (struct cpuinfo_x86 *c) +{ +switch (c->x86_model) { +case 15 :/* original 65 nm celeron/pentium/core2/xeon, "Merom"/"Conroe" */ + tlb_flushall_factor =0 ; +break ; +case 26 :/* 45 nm nehalem, "Bloomfield" */ +case 30 :/* 45 nm nehalem, "Lynnfield" */ +case 37 :/* 32 nm nehalem, "Clarkdale" */ +case 44 :/* 32 nm nehalem, "Gulftown" */ +case 46 :/* 45 nm nehalem-ex, "Beckton" */ + tlb_flushall_factor =64 ; +break ; +case 42 :/* SandyBridge */ +case 45 :/* SandyBridge, "Romely-EP" */ + tlb_flushall_factor =32 ; +break ; +case 28 :/* Atom */ +case 47 :/* 32 nm Xeon E7 */ +case 14 :/* 65 nm core solo/duo, "Yonah" */ +case 22 :/* single-core 65 nm celeron/core2solo "Merom-L"/"Conroe-L" */ +case 23 :/* current 45 nm celeron/core2/xeon "Penryn"/"Wolfdale" */ +case 29 :/* six-core 45 nm xeon "Dunnington" */ + +default : + tlb_flushall_factor =0 ; + } +} +void intel_cpu_detect_tlb (struct cpuinfo_x86 *c) {int i, j, n;
@@ -630,6 +659,7 @@ void intel_cpu_detect_tlb(struct cpuinfo_x86 *c)for (j =1 ; j <16 ; j++)intel_tlb_lookup (desc[j]); } +intel_tlb_flushall_factor_set (c); }static const struct cpu_dev __cpuinitconst intel_cpu_dev = {
arch/x86/mm/tlb.c +4 -4
@@ -314,8 +314,6 @@ void flush_tlb_mm(struct mm_struct *mm)preempt_enable (); } -#define FLUSHALL_BAR 16 -static inline int has_large_page (struct mm_struct *mm,unsigned long start,unsigned long end) {
@@ -343,7 +341,8 @@ void flush_tlb_range(struct vm_area_struct *vma, {struct mm_struct *mm; -if (!cpu_has_invlpg || vma->vm_flags &VM_HUGETLB ) { +if (!cpu_has_invlpg || vma->vm_flags &VM_HUGETLB + || !tlb_flushall_factor) { flush_all:flush_tlb_mm (vma->vm_mm);return ;
@@ -364,7 +363,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_SIZE > + act_entries/tlb_flushall_factor)local_flush_tlb ();else {if (has_large_page (mm, start, end)) {
Signature · 1 line
1.7.0.1
PATCH v2 5/5 x86/tlb: remove comments for tlb_flush_range implement suggestion
In reply to Alex ShiThanks to this comments that inspired me using invlpg to replace cr3 rewrite.
Now, it is time to remove from code to avoid reader confusing.
--- arch/x86/include/asm/tlbflush.h | 4 ---- 1 files changed, 0 insertions(+), 4 deletions(-)
arch/x86/include/asm/tlbflush.h +0 -4
@@ -81,10 +81,6 @@ static inline void __flush_tlb_one(unsigned long addr)* * ..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
Signature · 1 line
1.7.0.1