Skip to contentSkip to conversation

x86 tlb optimisations

PATCH Superseded Alex Shi 9 participants 48 messages May 23, 2012 – May 25, 2012

Subsystems (10)

v7 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Alex Shi

author
#0 lore raw

v7 cover x86 tlb optimisations

This version remove the 'x86/tlb: unify TLB_FLUSH_ALL definition', since it was picked up by Ingo.
And combined with the 7th/8th patches. that were send out days ago.

Any comments are appreciated!

Thanks!
Alex Shi
[PATCH v7 1/8] x86/tlb_info: get last level TLB entry number of CPU [PATCH v7 2/8] x86/flush_tlb: try flush_tlb_single one by one in [PATCH v7 3/8] x86/tlb: fall back to flush all when meet a THP large [PATCH v7 4/8] x86/tlb: add tlb_flushall_shift for specific CPU [PATCH v7 5/8] x86/tlb: enable tlb flush range support for generic [PATCH v7 6/8] x86/tlb: add tlb_flushall_shift knob into debugfs [PATCH v7 7/8] x86/tlb: replace INVALIDATE_TLB_VECTOR by [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

author
#1 lore raw

PATCH v7 1/8 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.

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
index 4fa7dcc..797faca 100644
@@ -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
index cf79302..0152082 100644
@@ -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
index 8bacc78..78db1d9 100644
@@ -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
author
#2 lore raw

PATCH v7 2/8 x86/flush_tlb: try flush_tlb_single one by one in flush_tlb_range

In reply to Alex Shi

x86 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
index aa0f913..03da4ab 100644
@@ -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
index 8e8b9a4..600a5fcac9 100644
@@ -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
index 7e8a096..c39c94e 100644
@@ -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
index 3bb9491..b47c2a8 100644
@@ -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
index 3ae0e61..0df5ad2 100644
@@ -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
index 69f5857..900d91e 100644
@@ -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
index 92f1a79..15ba03b 100644
@@ -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
#3 lore raw

Re: [PATCH v7 2/8] x86/flush_tlb: try flush_tlb_single one by one in flush_tlb_range

Quoted 5 lines
Quoted 1 line
Quoted 1 line · Alex Shi
arch/x86/xen/mmu.c +0 -0
@@ -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));
 

arch/x86/xen/mmu.c static void xen_flush_tlb_others(const struct cpumask *cpus, · line 1280

Quoted 10 lines
arch/x86/xen/mmu.c +2 -2
-	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);

Unless there is an implicit assumption that 'start' and 'end' are on the same page (which I doubt, as then it would be pointless to add 'end' here), this one is definitely wrong - you'd either have to issue multiple MMUEXT_INVLPG_MULTI-s, or you'd have to also use MMUEXT_TLB_FLUSH_MULTI for the multi-page case.

The same would appear to apply to the UV case, albeit I don't know enough about that code to be certain.

Jan

author
#4 lore raw

Re: [PATCH v7 2/8] x86/flush_tlb: try flush_tlb_single one by one in flush_tlb_range

Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · Alex Shi
(quoted diff) +2 -2
@@ -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;
 	}
 
Quoted 7 lines

Review static void xen_flush_tlb_others(const struct cpumask *cpus, · line 1280

Quoted 1 line
(quoted diff) +0 -0
 	MULTI_mmuext_op(mcs.mc, &args->op, 1, NULL, DOMID_SELF);

Unless there is an implicit assumption that 'start' and 'end' are on the same page (which I doubt, as then it would be pointless to add 'end' here), this one is definitely wrong - you'd either have to issue multiple MMUEXT_INVLPG_MULTI-s, or you'd have to also use MMUEXT_TLB_FLUSH_MULTI for the multi-page case.

Thanks comments!
So, the following change should be more safe for PV?

- if (va == 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.cmd = MMUEXT_TLB_FLUSH_MULTI;
+       if (start != TLB_FLUSH_ALL)

+ args->op.arg1.linear_addr = start;

        MULTI_mmuext_op(mcs.mc, &args->op, 1, NULL, DOMID_SELF);

Do you know why the old patch is past the performance testing of Yongjie?
https://lkml.org/lkml/2012/5/4/20

---

Quoted 1 line

From a43af44448039f15427de1901f9c2c7bf3cca698 Mon Sep 17 00:00:00 2001

From: Alex Shi <[email protected]>
Date: Wed, 9 May 2012 15:09:08 -0600
Subject: [PATCH 2/8] x86/flush_tlb: try flush_tlb_single one by one in
flush_tlb_range

x86 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                    |   14 ++---
 include/trace/events/xen.h            |   12 +++--
 8 files changed, 114 insertions(+), 51 deletions(-)
arch/x86/include/asm/paravirt.h +3 -2
index aa0f913..03da4ab 100644
@@ -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
index 8e8b9a4..600a5fcac9 100644
@@ -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
index c0e108e..51f8b1c 100644
@@ -77,14 +77,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
@@ -115,7 +111,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 +130,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 +157,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
index 3bb9491..b47c2a8 100644
@@ -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
index 3ae0e61..0df5ad2 100644
@@ -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 +6 -8
index 69f5857..34c9bad 100644
@@ -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,12 +1270,9 @@ 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) {
-		args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;
-	} else {
-		args->op.cmd = MMUEXT_INVLPG_MULTI;
-		args->op.arg1.linear_addr = va;
-	}
+	args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;
+	if (start != TLB_FLUSH_ALL)
+		args->op.arg1.linear_addr = start;
 
 	MULTI_mmuext_op(mcs.mc, &args->op, 1, NULL, DOMID_SELF);
 
include/trace/events/xen.h +7 -5
index 92f1a79..15ba03b 100644
@@ -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
#5 lore raw

Re: [PATCH v7 2/8] x86/flush_tlb: try flush_tlb_single one by one in flush_tlb_range

Quoted 11 lines
Quoted 1 line
Quoted 1 line · Alex Shi
Quoted 5 lines · Jan Beulich

Unless there is an implicit assumption that 'start' and 'end' are on the same page (which I doubt, as then it would be pointless to add 'end' here), this one is definitely wrong - you'd either have to issue multiple MMUEXT_INVLPG_MULTI-s, or you'd have to also use MMUEXT_TLB_FLUSH_MULTI for the multi-page case.

Thanks comments!
So, the following change should be more safe for PV?

arch/x86/xen/mmu.c static void xen_flush_tlb_others(const struct cpumask *cpus, · line 1273

Quoted 7 lines
arch/x86/xen/mmu.c +1 -6
-       if (va == 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.cmd = MMUEXT_TLB_FLUSH_MULTI;

This would be safe ...

arch/x86/xen/mmu.c static void xen_flush_tlb_others(const struct cpumask *cpus, · line 1275

Quoted 2 lines
arch/x86/xen/mmu.c +2 -0
+       if (start != TLB_FLUSH_ALL)
+               args->op.arg1.linear_addr = start;

... and then this superfluous, but it'd result in an unconditional full TLB flush. When start and end (or perhaps end-1, assuming end is not inclusive) are on the same page, MMUEXT_INVLPG_MULTI should be used; MMUEXT_TLB_FLUSH_MULTI might need to be used in all other cases, unless you want to split multi-page, non- global invalidations into multiple MMUEXT_INVLPG_MULTI-s (which would appear to be what the whole patch aims at).

Perhaps the abstraction layer needs to be changed instead:
Have the low level routines (Xen, UV, native) just deal with single pages, and do the splitting in common code (using the TLB size metrics).

But then again these metrics will become stale after a migration (not only on Xen, but in all virtualization scenarios), so some additional aspects will need to be taken care of anyway.

Quoted 5 lines
        MULTI_mmuext_op(mcs.mc, &args->op, 1, NULL, DOMID_SELF);

Do you know why the old patch is past the performance testing of Yongjie?
https://lkml.org/lkml/2012/5/4/20

No - mere luck perhaps (possibly because the invalidation policy inside the hypervisor is strict enough to paper over the flaw here)?

Jan

author
#6 lore raw

Re: [PATCH v7 2/8] x86/flush_tlb: try flush_tlb_single one by one in flush_tlb_range

Quoted 24 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · Alex Shi
Quoted 5 lines · Jan Beulich

Unless there is an implicit assumption that 'start' and 'end' are on the same page (which I doubt, as then it would be pointless to add 'end' here), this one is definitely wrong - you'd either have to issue multiple MMUEXT_INVLPG_MULTI-s, or you'd have to also use MMUEXT_TLB_FLUSH_MULTI for the multi-page case.

Thanks comments!
So, the following change should be more safe for PV?

arch/x86/xen/mmu.c static void xen_flush_tlb_others(const struct cpumask *cpus, · line 1273

Quoted 7 lines
arch/x86/xen/mmu.c +1 -6
-       if (va == 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.cmd = MMUEXT_TLB_FLUSH_MULTI;

This would be safe ...

arch/x86/xen/mmu.c static void xen_flush_tlb_others(const struct cpumask *cpus, · line 1275

Quoted 2 lines
arch/x86/xen/mmu.c +2 -0
+       if (start != TLB_FLUSH_ALL)
+               args->op.arg1.linear_addr = start;
Quoted 7 lines

... and then this superfluous, but it'd result in an unconditional full TLB flush. When start and end (or perhaps end-1, assuming end is not inclusive) are on the same page, MMUEXT_INVLPG_MULTI should be used; MMUEXT_TLB_FLUSH_MULTI might need to be used in all other cases, unless you want to split multi-page, non- global invalidations into multiple MMUEXT_INVLPG_MULTI-s (which would appear to be what the whole patch aims at).

        args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;
-       if (start != TLB_FLUSH_ALL)
+       if (start != TLB_FLUSH_ALL && (end - start) < PAGE_SIZE) {
+               args->op.cmd = MMUEXT_INVLPG_MULTI;
                args->op.arg1.linear_addr = start;

+ }

So, above it correct code for xen?
As to the xen optimisation of flush range, it is may better to be done in a separate patch.

Quoted 9 lines

Perhaps the abstraction layer needs to be changed instead:
Have the low level routines (Xen, UV, native) just deal with single pages, and do the splitting in common code (using the TLB size metrics).

But then again these metrics will become stale after a migration (not only on Xen, but in all virtualization scenarios), so some additional aspects will need to be taken care of anyway.

Sure, thanks for your care!

#7 lore raw

Re: [PATCH v7 2/8] x86/flush_tlb: try flush_tlb_single one by one in flush_tlb_range

Quoted 29 lines
Quoted 1 line
Quoted 1 line · Alex Shi
Quoted 16 lines · Jan Beulich

arch/x86/xen/mmu.c static void xen_flush_tlb_others(const struct cpumask *cpus, · line 1273

Quoted 10 lines
Quoted 1 line
Quoted 1 line · Alex Shi

So, the following change should be more safe for PV?

arch/x86/xen/mmu.c +1 -6
-       if (va == 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.cmd = MMUEXT_TLB_FLUSH_MULTI;

This would be safe ...

arch/x86/xen/mmu.c static void xen_flush_tlb_others(const struct cpumask *cpus, · line 1275

Quoted 2 lines
arch/x86/xen/mmu.c +2 -0
+       if (start != TLB_FLUSH_ALL)
+               args->op.arg1.linear_addr = start;
Quoted 7 lines

... and then this superfluous, but it'd result in an unconditional full TLB flush. When start and end (or perhaps end-1, assuming end is not inclusive) are on the same page, MMUEXT_INVLPG_MULTI should be used; MMUEXT_TLB_FLUSH_MULTI might need to be used in all other cases, unless you want to split multi-page, non- global invalidations into multiple MMUEXT_INVLPG_MULTI-s (which would appear to be what the whole patch aims at).

        args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;

Review

Quoted 2 lines
(quoted diff) +1 -1
-       if (start != TLB_FLUSH_ALL)
+       if (start != TLB_FLUSH_ALL && (end - start) < PAGE_SIZE) {

Assuming 'end' is not inclusive (you didn't clarify that),

       if (start != TLB_FLUSH_ALL && (end - start) <= PAGE_SIZE) {

(and only if 'start' is guaranteed to be page aligned).

Quoted 7 lines
(quoted diff) +2 -0
+               args->op.cmd = MMUEXT_INVLPG_MULTI;
                args->op.arg1.linear_addr = start;
+       }

So, above it correct code for xen?
As to the xen optimisation of flush range, it is may better to be done in a separate patch.

Agreed.

Jan

author
#8 lore raw

Re: [PATCH v7 2/8] x86/flush_tlb: try flush_tlb_single one by one in flush_tlb_range

Quoted 32 lines · Jan Beulich
Quoted 29 lines
Quoted 1 line
Quoted 1 line · Alex Shi
Quoted 16 lines · Jan Beulich

arch/x86/xen/mmu.c static void xen_flush_tlb_others(const struct cpumask *cpus, · line 1273

Quoted 10 lines
Quoted 1 line
Quoted 1 line · Alex Shi

So, the following change should be more safe for PV?

arch/x86/xen/mmu.c +1 -6
-       if (va == 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.cmd = MMUEXT_TLB_FLUSH_MULTI;

This would be safe ...

arch/x86/xen/mmu.c static void xen_flush_tlb_others(const struct cpumask *cpus, · line 1275

Quoted 2 lines
arch/x86/xen/mmu.c +2 -0
+       if (start != TLB_FLUSH_ALL)
+               args->op.arg1.linear_addr = start;
Quoted 7 lines

... and then this superfluous, but it'd result in an unconditional full TLB flush. When start and end (or perhaps end-1, assuming end is not inclusive) are on the same page, MMUEXT_INVLPG_MULTI should be used; MMUEXT_TLB_FLUSH_MULTI might need to be used in all other cases, unless you want to split multi-page, non- global invalidations into multiple MMUEXT_INVLPG_MULTI-s (which would appear to be what the whole patch aims at).

        args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;

Review

Quoted 2 lines
(quoted diff) +1 -1
-       if (start != TLB_FLUSH_ALL)
+       if (start != TLB_FLUSH_ALL && (end - start) < PAGE_SIZE) {
Quoted 5 lines

Assuming 'end' is not inclusive (you didn't clarify that),

       if (start != TLB_FLUSH_ALL && (end - start) <= PAGE_SIZE) {

(and only if 'start' is guaranteed to be page aligned).

I still a idiot for xen and a newbie for kernel. :)
AFAIK, if system just flush one page, it won't set end there. But anyway using '<=' has no problem.

Quoted 10 lines
Quoted 8 lines
(quoted diff) +2 -0
+               args->op.cmd = MMUEXT_INVLPG_MULTI;
                args->op.arg1.linear_addr = start;
+       }

So, above it correct code for xen?
As to the xen optimisation of flush range, it is may better to be done in a separate patch.

Agreed.

Thanks for all comments!

Quoted 3 lines

Jan

author
#9 lore raw

Re: [PATCH v7 2/8] x86/flush_tlb: try flush_tlb_single one by one in flush_tlb_range

In reply to Jan Beulich
Quoted 12 lines

Assuming 'end' is not inclusive (you didn't clarify that),

       if (start != TLB_FLUSH_ALL && (end - start) <= PAGE_SIZE) {

(and only if 'start' is guaranteed to be page aligned).

Quoted 4 lines
(quoted diff) +2 -0
+               args->op.cmd = MMUEXT_INVLPG_MULTI;
                args->op.arg1.linear_addr = start;
+       }
Quoted 8 lines
Quoted 3 lines

So, above it correct code for xen?
As to the xen optimisation of flush range, it is may better to be done in a separate patch.

Agreed.

Jan

Thanks for comments.

this the updated patch according to your suggestion.

-----

Quoted 1 line

From 82dfb90a26e9a27e7a073b2072684049aa2f5e12 Mon Sep 17 00:00:00 2001

From: Alex Shi <[email protected]>
Date: Wed, 9 May 2012 15:09:08 -0600
Subject: [PATCH 2/8] x86/flush_tlb: try flush_tlb_single one by one in
flush_tlb_range

x86 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                    |   12 ++--
 include/trace/events/xen.h            |   12 +++--
 8 files changed, 114 insertions(+), 49 deletions(-)
arch/x86/include/asm/paravirt.h +3 -2
index aa0f913..03da4ab 100644
@@ -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
index 8e8b9a4..600a5fcac9 100644
@@ -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
index c0e108e..51f8b1c 100644
@@ -77,14 +77,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
@@ -115,7 +111,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 +130,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 +157,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
index 3bb9491..b47c2a8 100644
@@ -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
index 3ae0e61..0df5ad2 100644
@@ -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 +6 -6
index 69f5857..e731140 100644
@@ -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,10 @@ 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) {
-		args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;
-	} else {
+	args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;
+	if (start != TLB_FLUSH_ALL && (end - start) <= PAGE_SIZE) {
 		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
index 92f1a79..15ba03b 100644
@@ -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
author
#10 lore raw

PATCH v7 3/8 x86/tlb: fall back to flush all when meet a THP large page

In reply to Alex Shi

We 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
index 7d92079..22e5bb1 100644
@@ -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
author
#11 lore raw

PATCH v7 4/8 x86/tlb: add tlb_flushall_shift for specific CPU

In reply to Alex Shi

Testing 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
index 797faca..a1667f0 100644
@@ -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
index 0152082..690e95d 100644
@@ -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
index 28ecd1b..bb90754 100644
@@ -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
index 22e5bb1..6a7ca47 100644
@@ -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
index f96a5b5..75e888b 100644
@@ -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
author
#12 lore raw

PATCH v7 5/8 x86/tlb: enable tlb flush range support for generic mmu and x86

In reply to Alex Shi

Not 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.
For x86, need to re-implement 'tlb_flush'.

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.

My macro benchmark 'mummap' http://lkml.org/lkml/2012/5/17/59
show that the random memory access on other CPU has 0~50% speed up on a 2P * 4cores * HT NHM EP.

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               |  123 ++++++++++++++++++--------------------
 include/asm-generic/tlb.h       |    2 +
 mm/memory.c                     |    9 +++
 5 files changed, 86 insertions(+), 66 deletions(-)
arch/x86/include/asm/tlb.h +8 -1
index 829215f..4fef207 100644
@@ -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
index c39c94e..1562ed0 100644
@@ -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 +58 -65

148 lines not on this page · raw message

include/asm-generic/tlb.h +2 -0
index 75e888b..ed6642a 100644
@@ -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
index 1e77da6..c22d9f5 100644
@@ -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
author
#13 lore raw

PATCH v7 6/8 x86/tlb: add tlb_flushall_shift knob into debugfs

In reply to Alex Shi

kernel 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 of
CONFIG_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
index e46c214..b322f12 100644
@@ -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
author
#14 lore raw

PATCH v7 7/8 x86/tlb: replace INVALIDATE_TLB_VECTOR by CALL_FUNCTION_VECTOR

In reply to Alex Shi

There are only 32 INVALIDATE_TLB_VECTOR now in kernel. but modern x86 sever has more cpu number. That causes heavy lock contentation in TLB flushing.

Now, useing generic smp call function to replace it.
In the NHM EX machine 4P * 8cores * HT = 64 CPUs, hackbench pthread has 3% performance increase.
And no clear performance changes on NHM EP(16CPUs), WSM EP(24CPU) and SNB EP(32CPU) machines.

---
 arch/x86/include/asm/entry_arch.h  |    9 --
 arch/x86/include/asm/irq_vectors.h |   11 --
 arch/x86/kernel/entry_64.S         |   18 ---
 arch/x86/kernel/irqinit.c          |   73 -----------
 arch/x86/mm/tlb.c                  |  250 +++++-------------------------------
 5 files changed, 33 insertions(+), 328 deletions(-)
arch/x86/include/asm/entry_arch.h +0 -9
index 0baa628..40afa00 100644
@@ -15,15 +15,6 @@ BUILD_INTERRUPT(call_function_interrupt,CALL_FUNCTION_VECTOR)
 BUILD_INTERRUPT(call_function_single_interrupt,CALL_FUNCTION_SINGLE_VECTOR)
 BUILD_INTERRUPT(irq_move_cleanup_interrupt,IRQ_MOVE_CLEANUP_VECTOR)
 BUILD_INTERRUPT(reboot_interrupt,REBOOT_VECTOR)
-
-.irp idx,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, \
-	16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
-.if NUM_INVALIDATE_TLB_VECTORS > \idx
-BUILD_INTERRUPT3(invalidate_interrupt\idx,
-		 (INVALIDATE_TLB_VECTOR_START)+\idx,
-		 smp_invalidate_interrupt)
-.endif
-.endr
 #endif
 
 BUILD_INTERRUPT(x86_platform_ipi, X86_PLATFORM_IPI_VECTOR)
arch/x86/include/asm/irq_vectors.h +0 -11
index 4b44487..1508e51 100644
@@ -119,17 +119,6 @@
  */
 #define LOCAL_TIMER_VECTOR		0xef
 
-/* up to 32 vectors used for spreading out TLB flushes: */
-#if NR_CPUS <= 32
-# define NUM_INVALIDATE_TLB_VECTORS	(NR_CPUS)
-#else
-# define NUM_INVALIDATE_TLB_VECTORS	(32)
-#endif
-
-#define INVALIDATE_TLB_VECTOR_END	(0xee)
-#define INVALIDATE_TLB_VECTOR_START	\
-	(INVALIDATE_TLB_VECTOR_END-NUM_INVALIDATE_TLB_VECTORS+1)
-
 #define NR_VECTORS			 256
 
 #define FPU_IRQ				  13
arch/x86/kernel/entry_64.S +0 -18
index cdc79b5..0fa34c9 100644
@@ -1015,24 +1015,6 @@ apicinterrupt LOCAL_TIMER_VECTOR \
 apicinterrupt X86_PLATFORM_IPI_VECTOR \
 	x86_platform_ipi smp_x86_platform_ipi
 
-#ifdef CONFIG_SMP
-	ALIGN
-	INTR_FRAME
-.irp idx,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, \
-	16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
-.if NUM_INVALIDATE_TLB_VECTORS > \idx
-ENTRY(invalidate_interrupt\idx)
-	pushq_cfi $~(INVALIDATE_TLB_VECTOR_START+\idx)
-	jmp .Lcommon_invalidate_interrupt0
-	CFI_ADJUST_CFA_OFFSET -8
-END(invalidate_interrupt\idx)
-.endif
-.endr
-	CFI_ENDPROC
-apicinterrupt INVALIDATE_TLB_VECTOR_START, \
-	invalidate_interrupt0, smp_invalidate_interrupt
-#endif
-
 apicinterrupt THRESHOLD_APIC_VECTOR \
 	threshold_interrupt smp_threshold_interrupt
 apicinterrupt THERMAL_APIC_VECTOR \
arch/x86/kernel/irqinit.c +0 -73

79 lines not on this page · raw message

arch/x86/mm/tlb.c +35 -219

289 lines not on this page · raw message

Signature · 1 line
1.7.5.4
author
#15 lore raw

PATCH v7 8/8 x86/tlb: just do tlb flush on one of siblings of SMT

In reply to Alex Shi

According to Intel's SDM, flush tlb on both of siblings of SMT is just wasting time, no any benefit and hurt performance. Because SMT siblings share the all levels TLB and page structure caches.

Random flush sibling can make mulitiple thread run more balance. Here rand calculated from jiffies, that is a bit less heavy than random32()(save 2/3 time on my NHM EP, and 1/2 on my SNB EP)

The patched tested with my macro benchmark munmap, that sent to lkml before. http://lkml.org/lkml/2012/5/17/59

On my 2P * 4 cores * HT NHM EP machine, munmap system call speed increased 10~15%, while average random memory access speed on other LCPUs increase 12%.

On my 2P * 8 cores * HT SNB EP machine, munmap system call speed increased 10~13%, while average random memory access speed on other LCPUs increase 4~20%.

---
 arch/x86/mm/tlb.c |   30 +++++++++++++++++++++++++++---
 1 files changed, 27 insertions(+), 3 deletions(-)
arch/x86/mm/tlb.c +27 -3
index 0232e24..bc0a6fc 100644
@@ -85,22 +85,46 @@ void native_flush_tlb_others(const struct cpumask *cpumask,
 				 struct mm_struct *mm, unsigned long start,
 				 unsigned long end)
 {
+	int cpu;
+	unsigned long rand;
 	struct flush_tlb_info info;
+	cpumask_t flush_mask, *sblmask;
+
 	info.flush_mm = mm;
 	info.flush_start = start;
 	info.flush_end = end;
 
+	/* doing flush on both siblings of SMT is just wasting time */
+	cpumask_copy(&flush_mask, cpumask);
+	if (likely(smp_num_siblings > 1)) {
+		rand = jiffies;
+		/* See "Numerical Recipes in C", second edition, p. 284 */
+		rand = rand * 1664525L + 1013904223L;
+		rand &= 0x1;
+
+		for_each_cpu(cpu, &flush_mask) {
+			sblmask = cpu_sibling_mask(cpu);
+			if (cpumask_subset(sblmask, &flush_mask)) {
+				if (rand == 0)
+					cpu_clear(cpu, flush_mask);
+				else
+					cpu_clear(cpumask_next(cpu, sblmask),
+								flush_mask);
+			}
+		}
+	}
+
 	if (is_uv_system()) {
 		unsigned int cpu;
 
 		cpu = smp_processor_id();
-		cpumask = uv_flush_tlb_others(cpumask, mm, start, end, cpu);
+		cpumask = uv_flush_tlb_others(&flush_mask, mm, start, end, cpu);
 		if (cpumask)
-			smp_call_function_many(cpumask, flush_tlb_func,
+			smp_call_function_many(&flush_mask, flush_tlb_func,
 								&info, 1);
 		return;
 	}
-	smp_call_function_many(cpumask, flush_tlb_func, &info, 1);
+	smp_call_function_many(&flush_mask, flush_tlb_func, &info, 1);
 }
 
 void flush_tlb_current_task(void)
Signature · 1 line
1.7.5.4
#16 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

Quoted 11 lines
Quoted 1 line
Quoted 1 line · Alex Shi
arch/x86/mm/tlb.c +10 -0
+	/* doing flush on both siblings of SMT is just wasting time */
+	cpumask_copy(&flush_mask, cpumask);
+	if (likely(smp_num_siblings > 1)) {
+		rand = jiffies;
+		/* See "Numerical Recipes in C", second edition, p. 284 */
+		rand = rand * 1664525L + 1013904223L;
+		rand &= 0x1;
+
+		for_each_cpu(cpu, &flush_mask) {
+			sblmask = cpu_sibling_mask(cpu);

arch/x86/mm/tlb.c void native_flush_tlb_others(const struct cpumask *cpumask, · line 116

Quoted 10 lines
arch/x86/mm/tlb.c +10 -0
+			if (cpumask_subset(sblmask, &flush_mask)) {
+				if (rand == 0)
+					cpu_clear(cpu, flush_mask);
+				else
+					cpu_clear(cpumask_next(cpu, sblmask),
+								flush_mask);
+			}
+		}
+	}
+

There is no comment or anything else indicating that this is suitable for dual-thread CPUs only - when there are more than 2 threads per core, the intended effect won't be achieved. I'd recommend making the logic generic from the beginning, but if that doesn't seem feasible to you, at least a comment stating the limitation should be added imo.

Jan

#17 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

Quoted 17 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · Alex Shi
arch/x86/mm/tlb.c +16 -0
+	/* doing flush on both siblings of SMT is just wasting time */
+	cpumask_copy(&flush_mask, cpumask);
+	if (likely(smp_num_siblings > 1)) {
+		rand = jiffies;
+		/* See "Numerical Recipes in C", second edition, p. 284 */
+		rand = rand * 1664525L + 1013904223L;
+		rand &= 0x1;
+
+		for_each_cpu(cpu, &flush_mask) {
+			sblmask = cpu_sibling_mask(cpu);
+			if (cpumask_subset(sblmask, &flush_mask)) {
+				if (rand == 0)
+					cpu_clear(cpu, flush_mask);
+				else
+					cpu_clear(cpumask_next(cpu, sblmask),
+								flush_mask);
Quoted 8 lines

arch/x86/mm/tlb.c void native_flush_tlb_others(const struct cpumask *cpumask, · line 116

Quoted 4 lines
arch/x86/mm/tlb.c +4 -0
+			}
+		}
+	}
+

There is no comment or anything else indicating that this is suitable for dual-thread CPUs only - when there are more than 2 threads per core, the intended effect won't be achieved.

Why would that be? Won't higher thread count still share the same resources just more so?

Quoted 4 lines

I'd
recommend making the logic generic from the beginning, but if that doesn't seem feasible to you, at least a comment stating the limitation should be added imo.

My objection to the whole lot is that its looks mightily expensive on large machines, cpumask operations aren't cheap when you've got 4k cpus etc..

Also, you very much cannot put cpumask_t on stack.

maintainer
#18 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

Quoted 6 lines · Peter Zijlstra
Quoted 3 lines

There is no comment or anything else indicating that this is suitable for dual-thread CPUs only - when there are more than 2 threads per core, the intended effect won't be achieved.

Why would that be? Won't higher thread count still share the same resources just more so?

Ah, I see, you're saying his code is buggy for >2 threads. Agreed.

#19 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

Quoted 10 lines · Peter Zijlstra
Quoted 6 lines · Peter Zijlstra
Quoted 3 lines

There is no comment or anything else indicating that this is suitable for dual-thread CPUs only - when there are more than 2 threads per core, the intended effect won't be achieved.

Why would that be? Won't higher thread count still share the same resources just more so?

Ah, I see, you're saying his code is buggy for >2 threads. Agreed.

An evil knob to statically choose which SMT sibling gets the interrupt would be nice. Then my compute-intensive thread could be (mostly) unaffected by the other thread on a different core that calls munmap frequently.

--Andy

author
#20 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

Quoted 9 lines · Andrew Lutomirski
Quoted 8 lines · Peter Zijlstra
Quoted 6 lines · Peter Zijlstra
Quoted 3 lines

There is no comment or anything else indicating that this is suitable for dual-thread CPUs only - when there are more than 2 threads per core, the intended effect won't be achieved.

Why would that be? Won't higher thread count still share the same resources just more so?

Quoted 7 lines
Quoted 2 lines

Ah, I see, you're saying his code is buggy for >2 threads. Agreed.

An evil knob to statically choose which SMT sibling gets the interrupt would be nice. Then my compute-intensive thread could be (mostly) unaffected by the other thread on a different core that calls munmap frequently.

How to know we are in such situation? :)

Quoted 2 lines

--Andy

#21 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

Quoted 14 lines · Alex Shi
Quoted 12 lines · Andrew Lutomirski
Quoted 10 lines · Peter Zijlstra
Quoted 6 lines · Peter Zijlstra
Quoted 3 lines

There is no comment or anything else indicating that this is suitable for dual-thread CPUs only - when there are more than 2 threads per core, the intended effect won't be achieved.

Why would that be? Won't higher thread count still share the same resources just more so?

Ah, I see, you're saying his code is buggy for >2 threads. Agreed.

Quoted 6 lines
Quoted 4 lines

An evil knob to statically choose which SMT sibling gets the interrupt would be nice. Then my compute-intensive thread could be (mostly) unaffected by the other thread on a different core that calls munmap frequently.

How to know we are in such situation? :)

Please, not yet another knob, we have too many as it is.

Can't you figure this out from the topology, smt siblings or whatever this is called:

/sys/devices/system/cpu/cpu0/topology/thread_siblings

?

Signature · 8 lines
Regards/Gruss,
Boris.

Advanced Micro Devices GmbH
Einsteinring 24, 85609 Dornach
GM: Alberto Bozzo
Reg: Dornach, Landkreis Muenchen
HRB Nr. 43632 WEEE Registernr: 129 19551
maintainer
#22 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

In reply to Andrew Lutomirski
Quoted 9 lines · Andrew Lutomirski
Quoted 8 lines · Peter Zijlstra
Quoted 6 lines · Peter Zijlstra
Quoted 3 lines

There is no comment or anything else indicating that this is suitable for dual-thread CPUs only - when there are more than 2 threads per core, the intended effect won't be achieved.

Why would that be? Won't higher thread count still share the same resources just more so?

Quoted 7 lines
Quoted 2 lines

Ah, I see, you're saying his code is buggy for >2 threads. Agreed.

An evil knob to statically choose which SMT sibling gets the interrupt would be nice. Then my compute-intensive thread could be (mostly) unaffected by the other thread on a different core that calls munmap frequently.

Just make sure the two workloads never share a core and this should already happen since TLB invalidates are only broadcast to the mm cpumask.

#23 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Andrew Lutomirski
Quoted 10 lines · Peter Zijlstra
Quoted 6 lines · Peter Zijlstra
Quoted 3 lines

There is no comment or anything else indicating that this is suitable for dual-thread CPUs only - when there are more than 2 threads per core, the intended effect won't be achieved.

Why would that be? Won't higher thread count still share the same resources just more so?

Ah, I see, you're saying his code is buggy for >2 threads. Agreed.

Quoted 9 lines
Quoted 4 lines

An evil knob to statically choose which SMT sibling gets the interrupt would be nice.  Then my compute-intensive thread could be (mostly) unaffected by the other thread on a different core that calls munmap frequently.

Just make sure the two workloads never share a core and this should already happen since TLB invalidates are only broadcast to the mm cpumask.

I already do that. But these two workloads share the same mm (they're threads in the same process), so I'd gain performance if the invalidations were sent from the core that called munmap to the SMT sibling of the other thread.

A decent heuristic might be to prefer idle SMT siblings for TLB invalidation. I don't know what effect that would have on power consumption (it would be rather bad if idling one SMT thread while the other one is busy saves much power).

--Andy

maintainer
#24 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

Quoted 5 lines · Andrew Lutomirski

A decent heuristic might be to prefer idle SMT siblings for TLB invalidation. I don't know what effect that would have on power consumption (it would be rather bad if idling one SMT thread while the other one is busy saves much power).

Right, I've never really understood how C-states and SMT go together. Arjan recently implied waking a thread sibling from C-state was 'expensive' which on first thought seems daft, the core is running already.

#25 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

Quoted 6 lines · Peter Zijlstra
Quoted 5 lines · Andrew Lutomirski

A decent heuristic might be to prefer idle SMT siblings for TLB invalidation. I don't know what effect that would have on power consumption (it would be rather bad if idling one SMT thread while the other one is busy saves much power).

we really really shouldn't do flushing of tlb's on only one half of SMT. SMT sibblings have their own TLB pool at least on some of Intels chips.

Also, note that on anything sane, we flush the tlb's in software before going to an Idle state, so that we don't have to wake idle cpus up to flush their TLBs (except for "global tlbs", but those change very very very rarely hopefully)

Quoted 5 lines

Right, I've never really understood how C-states and SMT go together. Arjan recently implied waking a thread sibling from C-state was 'expensive' which on first thought seems daft, the core is running already.

in order to wake *anything* you need to send an IPI to it, it has to exit the idle loop etc etc. It's not expensive-expensive, but it certainly is not free either.

author
#26 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

Quoted 10 lines · Arjan van de Ven
Quoted 6 lines · Peter Zijlstra
Quoted 5 lines · Andrew Lutomirski

A decent heuristic might be to prefer idle SMT siblings for TLB invalidation. I don't know what effect that would have on power consumption (it would be rather bad if idling one SMT thread while the other one is busy saves much power).

we really really shouldn't do flushing of tlb's on only one half of SMT. SMT sibblings have their own TLB pool at least on some of Intels chips.

That is also the biggest question I want to know. Actually, some documents, wiki said the SMT sibling just has process registers and interrupt part, no any tlb/l1 cache etc, (like intel's doc vol6iss1_hyper_threading_technology.pdf). And the patch runs well on NHM EP/WSM EP/NHM EX/SNB EP CPUs.

But hard to get such clearly per cpu info of SMT/HT, so, what the detailed Intel chips has 'TLB pool' on SMT?

Quoted 15 lines

Also, note that on anything sane, we flush the tlb's in software before going to an Idle state, so that we don't have to wake idle cpus up to flush their TLBs (except for "global tlbs", but those change very very very rarely hopefully)

Quoted 5 lines

Right, I've never really understood how C-states and SMT go together. Arjan recently implied waking a thread sibling from C-state was 'expensive' which on first thought seems daft, the core is running already.

in order to wake *anything* you need to send an IPI to it, it has to exit the idle loop etc etc. It's not expensive-expensive, but it certainly is not free either.

#27 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

Quoted 14 lines · Alex Shi
Quoted 10 lines · Arjan van de Ven
Quoted 6 lines · Peter Zijlstra
Quoted 5 lines · Andrew Lutomirski

A decent heuristic might be to prefer idle SMT siblings for TLB invalidation. I don't know what effect that would have on power consumption (it would be rather bad if idling one SMT thread while the other one is busy saves much power).

we really really shouldn't do flushing of tlb's on only one half of SMT. SMT sibblings have their own TLB pool at least on some of Intels chips.

Quoted 8 lines

That is also the biggest question I want to know. Actually, some documents, wiki said the SMT sibling just has process registers and interrupt part, no any tlb/l1 cache etc, (like intel's doc vol6iss1_hyper_threading_technology.pdf). And the patch runs well on NHM EP/WSM EP/NHM EX/SNB EP CPUs.

But hard to get such clearly per cpu info of SMT/HT, so, what the detailed Intel chips has 'TLB pool' on SMT?

all of them.

the TLB pool is shared as physical resource (dynamic or static, that depends), but each tlb entry will be tagged for which of the two HT pairs it's for, and on a logical level, they are completely separate as a result (as they should be)

author
#28 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

Quoted 14 lines
Quoted 5 lines
Quoted 3 lines

we really really shouldn't do flushing of tlb's on only one half of SMT.

SMT sibblings have their own TLB pool at least on some of Intels chips.

Quoted 8 lines

That is also the biggest question I want to know. Actually, some documents, wiki said the SMT sibling just has process registers and interrupt part, no any tlb/l1 cache etc, (like intel's doc vol6iss1_hyper_threading_technology.pdf). And the patch runs well on NHM EP/WSM EP/NHM EX/SNB EP CPUs.

But hard to get such clearly per cpu info of SMT/HT, so, what the detailed Intel chips has 'TLB pool' on SMT?

Quoted 6 lines

all of them.

the TLB pool is shared as physical resource (dynamic or static, that depends), but each tlb entry will be tagged for which of the two HT pairs it's for, and on a logical level, they are completely separate as a result (as they should be)

But, why just flush part of SMT doesn't crash kernel on many benchmarks testing? Does it means flush tlb without PCID (doesn't enable in current kernel) will flush both of 'TLB pool'?

Oh, lots of questions of the TLB pool details. :) Could you like share the URL of related documents?

Quoted 1 line
#29 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

Quoted 6 lines · Alex Shi
Quoted 5 lines

the TLB pool is shared as physical resource (dynamic or static, that depends), but each tlb entry will be tagged for which of the two HT pairs it's for, and on a logical level, they are completely separate as a result (as they should be)

Quoted 7 lines

But, why just flush part of SMT doesn't crash kernel on many benchmarks testing? Does it means flush tlb without PCID (doesn't enable in current kernel) will flush both of 'TLB pool'?

Oh, lots of questions of the TLB pool details. :) Could you like share the URL of related documents?

Hang on here... there is a huge difference between what a particular CPU implementation does and what is architecturally guaranteed.

Both wearing my Linux x86 maintainer hat, and wearing my Intel employee hat, I want to categorically state that Linux cannot rely on behavior that isn't architecturally guaranteed. Unless we can get an architectural guarantee that this elision is safe, it cannot go in. It doesn't work the other way -- the burden of proof is to prove that the change is safe, not that the change cannot be proven unsafe.

-hpa

author
#30 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

Quoted 18 lines · H. Peter Anvin
Quoted 6 lines · Alex Shi
Quoted 5 lines

the TLB pool is shared as physical resource (dynamic or static, that depends), but each tlb entry will be tagged for which of the two HT pairs it's for, and on a logical level, they are completely separate as a result (as they should be)

Quoted 7 lines

But, why just flush part of SMT doesn't crash kernel on many benchmarks testing? Does it means flush tlb without PCID (doesn't enable in current kernel) will flush both of 'TLB pool'?

Oh, lots of questions of the TLB pool details. :) Could you like share the URL of related documents?

Hang on here... there is a huge difference between what a particular CPU implementation does and what is architecturally guaranteed.

Quoted 6 lines

Both wearing my Linux x86 maintainer hat, and wearing my Intel employee hat, I want to categorically state that Linux cannot rely on behavior that isn't architecturally guaranteed. Unless we can get an architectural guarantee that this elision is safe, it cannot go in. It doesn't work the other way -- the burden of proof is to prove that the change is safe, not that the change cannot be proven unsafe.

Understand and thanks for all of your time!

Quoted 3 lines

-hpa

#31 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

In reply to Alex Shi
Quoted 16 lines · Alex Shi
Quoted 16 lines
Quoted 5 lines
Quoted 3 lines

we really really shouldn't do flushing of tlb's on only one half of SMT.

SMT sibblings have their own TLB pool at least on some of Intels chips.

Quoted 8 lines

That is also the biggest question I want to know. Actually, some documents, wiki said the SMT sibling just has process registers and interrupt part, no any tlb/l1 cache etc, (like intel's doc vol6iss1_hyper_threading_technology.pdf). And the patch runs well on NHM EP/WSM EP/NHM EX/SNB EP CPUs.

But hard to get such clearly per cpu info of SMT/HT, so, what the detailed Intel chips has 'TLB pool' on SMT?

all of them.

Quoted 8 lines
Quoted 4 lines

the TLB pool is shared as physical resource (dynamic or static, that depends), but each tlb entry will be tagged for which of the two HT pairs it's for, and on a logical level, they are completely separate as a result (as they should be)

But, why just flush part of SMT doesn't crash kernel on many benchmarks testing?

stale tlb's don't crash the kernel
they do random weird **** to userspace processes.

you REALLY don't want to be debugging those.

There is absolutely NO GUARANTEE that a full tlbflush on one thread flushes the other one. (in fact I'd be surprised if it actually did).

Also remember that there are several levels of TLB and tlb caches, and you HAVE to flush all.
Intel put out a very clear document about the rules around TLBs (it's on the same URL as the SDM, if it hasn't been absorbed into the SDM already)... and we have to follow those rules.
(Linux already followed those rules even prior to that doc existing, but the rules are at least very explicit now)

author
#32 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

Quoted 29 lines · Arjan van de Ven
Quoted 16 lines · Alex Shi
Quoted 16 lines
Quoted 5 lines
Quoted 3 lines

we really really shouldn't do flushing of tlb's on only one half of SMT.

SMT sibblings have their own TLB pool at least on some of Intels chips.

Quoted 8 lines

That is also the biggest question I want to know. Actually, some documents, wiki said the SMT sibling just has process registers and interrupt part, no any tlb/l1 cache etc, (like intel's doc vol6iss1_hyper_threading_technology.pdf). And the patch runs well on NHM EP/WSM EP/NHM EX/SNB EP CPUs.

But hard to get such clearly per cpu info of SMT/HT, so, what the detailed Intel chips has 'TLB pool' on SMT?

all of them.

Quoted 8 lines
Quoted 4 lines

the TLB pool is shared as physical resource (dynamic or static, that depends), but each tlb entry will be tagged for which of the two HT pairs it's for, and on a logical level, they are completely separate as a result (as they should be)

But, why just flush part of SMT doesn't crash kernel on many benchmarks testing?

stale tlb's don't crash the kernel
they do random weird **** to userspace processes.

Quoted 7 lines

you REALLY don't want to be debugging those.

There is absolutely NO GUARANTEE that a full tlbflush on one thread flushes the other one. (in fact I'd be surprised if it actually did).

Also remember that there are several levels of TLB and tlb caches, and you HAVE to flush all.

Thanks for comments!
BTW,
As my limited knowledge, rewrite cr3 or invlpg will flush all levels TLB entries in CPU. So, what's you mean of 'HAVE to flush all'?

#33 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

On

Quoted 2 lines

As my limited knowledge, rewrite cr3 or invlpg will flush all levels TLB entries in CPU. So, what's you mean of 'HAVE to flush all'?

neither of these will flush global tlb entries.

(see the "global" bit in the page table format)

author
#34 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

In reply to Peter Zijlstra
Quoted 27 lines · Peter Zijlstra
Quoted 17 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · Alex Shi
arch/x86/mm/tlb.c +16 -0
+	/* doing flush on both siblings of SMT is just wasting time */
+	cpumask_copy(&flush_mask, cpumask);
+	if (likely(smp_num_siblings > 1)) {
+		rand = jiffies;
+		/* See "Numerical Recipes in C", second edition, p. 284 */
+		rand = rand * 1664525L + 1013904223L;
+		rand &= 0x1;
+
+		for_each_cpu(cpu, &flush_mask) {
+			sblmask = cpu_sibling_mask(cpu);
+			if (cpumask_subset(sblmask, &flush_mask)) {
+				if (rand == 0)
+					cpu_clear(cpu, flush_mask);
+				else
+					cpu_clear(cpumask_next(cpu, sblmask),
+								flush_mask);
Quoted 8 lines

arch/x86/mm/tlb.c void native_flush_tlb_others(const struct cpumask *cpumask, · line 116

Quoted 4 lines
arch/x86/mm/tlb.c +4 -0
+			}
+		}
+	}
+

There is no comment or anything else indicating that this is suitable for dual-thread CPUs only - when there are more than 2 threads per core, the intended effect won't be achieved.

Quoted 7 lines

Why would that be? Won't higher thread count still share the same resources just more so?

Quoted 4 lines

I'd
recommend making the logic generic from the beginning, but if that doesn't seem feasible to you, at least a comment stating the limitation should be added imo.

Sure. but just want to know how many commercial x86 CPU uses >2 SMTs? Write a short, quick function to do random selection in SMT is quite complicate considering cpumask maybe just contain random number SMT siblings in a core.

Quoted 6 lines

My objection to the whole lot is that its looks mightily expensive on large machines, cpumask operations aren't cheap when you've got 4k cpus etc..

Also, you very much cannot put cpumask_t on stack.

Sure, and do you has related data for this?

I just measured the cost of this function on my Romely EP(32 LCPUs) with cpumask_t and NR_CPUS = 32/256/512/4096, the cost are similar with 256/512/4096 and that increased about 20% time cost from 32.

I also tried to use cpumask_var_t and alloc it in heap(use CPUMASK_OFFSTACK), actually, it cost same time with cpumask_t in stack. But, the allocation bring another big cost. So, I use cpumask_t in stack. The performance gain data in commit log is getting with NR_CPUS = 256.

#35 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

Quoted 1 line · Alex Shi

So, I use cpumask_t in stack.

cpumask_t is 512 bytes with NR_CPUS=4096, that's generally considered too big to be on stack.

A number of people spend a lot of time removing cpumask_t from stacks a while ago, I'm very sure they'll not be happy if you're going to add it back.

Rusty, what was the plan on removing cpumask_t altogether?

author
#36 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

Quoted 9 lines · Peter Zijlstra
Quoted 1 line · Alex Shi

So, I use cpumask_t in stack.

cpumask_t is 512 bytes with NR_CPUS=4096, that's generally considered too big to be on stack.

A number of people spend a lot of time removing cpumask_t from stacks a while ago, I'm very sure they'll not be happy if you're going to add it back.

In my testing, allocate a cpumask_var_t is more worse than cpumask_t. So, another choice is using percpu pre-allocatd cpumask for this, but I am wondering if it is acceptable. What's suggestion for this point?

Quoted 3 lines

Rusty, what was the plan on removing cpumask_t altogether?

#37 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

Quoted 8 lines · Alex Shi
Quoted 6 lines · Peter Zijlstra
Quoted 1 line · Alex Shi

So, I use cpumask_t in stack.

cpumask_t is 512 bytes with NR_CPUS=4096, that's generally considered too big to be on stack.

Quoted 8 lines
Quoted 3 lines

A number of people spend a lot of time removing cpumask_t from stacks a while ago, I'm very sure they'll not be happy if you're going to add it back.

In my testing, allocate a cpumask_var_t is more worse than cpumask_t. So, another choice is using percpu pre-allocatd cpumask for this, but I am wondering if it is acceptable. What's suggestion for this point?

Thanks for the ping Peter!

Please don't use cpus_ operations: they're deprecated. Use cpumask_. Similarly, avoid cpumask_t.

And yes, if you configure for thousands of CPUs, it's not free! If it's a significant, you will want to use a per-cpu cpumask_var_t.

My other thought: your patch seems optimal as far as avoiding IPIs goes, but I wonder how often it folds down to a single CPU? That case is easier to fast-path without using a new cpumask.

Cheers,
Rusty.

author
#38 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

Quoted 14 lines · Rusty Russell
Quoted 13 lines · Alex Shi
Quoted 9 lines · Peter Zijlstra
Quoted 1 line · Alex Shi

So, I use cpumask_t in stack.

cpumask_t is 512 bytes with NR_CPUS=4096, that's generally considered too big to be on stack.

A number of people spend a lot of time removing cpumask_t from stacks a while ago, I'm very sure they'll not be happy if you're going to add it back.

Quoted 8 lines
Quoted 3 lines

In my testing, allocate a cpumask_var_t is more worse than cpumask_t. So, another choice is using percpu pre-allocatd cpumask for this, but I am wondering if it is acceptable. What's suggestion for this point?

Thanks for the ping Peter!

Please don't use cpus_ operations: they're deprecated. Use cpumask_. Similarly, avoid cpumask_t.

Thanks, Rusty and Peter!

Quoted 3 lines

And yes, if you configure for thousands of CPUs, it's not free! If it's a significant, you will want to use a per-cpu cpumask_var_t.

I see.

Quoted 4 lines

My other thought: your patch seems optimal as far as avoiding IPIs goes, but I wonder how often it folds down to a single CPU? That case is easier to fast-path without using a new cpumask.

It will be quite often if threads number of user level APP is more than a half of LCPUs.
It needs a new cpumask because we can not remove SMT bit on mm->cpu_vm_mask_var directly.

Quoted 3 lines

Cheers,
Rusty.

author
#39 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

In reply to Peter Zijlstra
Quoted 5 lines · Peter Zijlstra
Quoted 1 line · Alex Shi

So, I use cpumask_t in stack.

cpumask_t is 512 bytes with NR_CPUS=4096, that's generally considered too big to be on stack.

I am not consistent with cpumask_t.
I just want to know why it is too big on stack? Since if it causes trouble in using, that mean current kernel will fail to run on 4096 CPU system.

#40 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

Quoted 3 lines · Alex Shi

I just want to know why it is too big on stack? Since if it causes trouble in using, that mean current kernel will fail to run on 4096 CPU system.

Because the stack is only 8k or so. If you add functions with 512+ bytes of footprint you're done very very quickly indeed.

Now if you were able to build a call-graph using a static analysis tool and were able to weight each edge with the stack usage of every function, finding the actual max stack would be doable. Lacking this its a matter of policy and 'luck'.

Our policy is to use as little stack as possible and 512 bytes is definitely out.

Our 'luck' is that every so often people over-run their stack and we get to spend time on figuring out wtf happened.

#41 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

In reply to Alex Shi
Quoted 6 lines
Quoted 1 line
Quoted 1 line · Alex Shi
Quoted 3 lines · Peter Zijlstra
Quoted 1 line · Alex Shi

So, I use cpumask_t in stack.

Quoted 8 lines
Quoted 2 lines

cpumask_t is 512 bytes with NR_CPUS=4096, that's generally considered too big to be on stack.

I am not consistent with cpumask_t.
I just want to know why it is too big on stack? Since if it causes trouble in using, that mean current kernel will fail to run on 4096 CPU system.

Why? Did you spot critical left-over instances of on-stack cpumask_t-s anywhere?

Jan

author
#42 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

Quoted 10 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · Alex Shi
Quoted 5 lines · Peter Zijlstra
Quoted 1 line · Alex Shi

So, I use cpumask_t in stack.

cpumask_t is 512 bytes with NR_CPUS=4096, that's generally considered too big to be on stack.

Quoted 7 lines
Quoted 4 lines

I am not consistent with cpumask_t.
I just want to know why it is too big on stack? Since if it causes trouble in using, that mean current kernel will fail to run on 4096 CPU system.

Why? Did you spot critical left-over instances of on-stack cpumask_t-s anywhere?

I aware it is a stupid question just after the e-mail sent out. Thanks for you and PeterZ's nice reply!

Quoted 3 lines

Jan

#43 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

In reply to Alex Shi
Quoted 1 line · Alex Shi

The performance gain data in commit log is getting with NR_CPUS = 256.

Remember that most disto kernels are build with NR_CPUS=4096, so that is what most people will run with.

#44 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

In reply to Alex Shi
Quoted 39 lines
Quoted 1 line
Quoted 1 line · Alex Shi
Quoted 27 lines · Peter Zijlstra
Quoted 17 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · Alex Shi
arch/x86/mm/tlb.c +16 -0
+	/* doing flush on both siblings of SMT is just wasting time */
+	cpumask_copy(&flush_mask, cpumask);
+	if (likely(smp_num_siblings > 1)) {
+		rand = jiffies;
+		/* See "Numerical Recipes in C", second edition, p. 284 */
+		rand = rand * 1664525L + 1013904223L;
+		rand &= 0x1;
+
+		for_each_cpu(cpu, &flush_mask) {
+			sblmask = cpu_sibling_mask(cpu);
+			if (cpumask_subset(sblmask, &flush_mask)) {
+				if (rand == 0)
+					cpu_clear(cpu, flush_mask);
+				else
+					cpu_clear(cpumask_next(cpu, sblmask),
+								flush_mask);
Quoted 8 lines

arch/x86/mm/tlb.c void native_flush_tlb_others(const struct cpumask *cpumask, · line 116

Quoted 4 lines
arch/x86/mm/tlb.c +4 -0
+			}
+		}
+	}
+

There is no comment or anything else indicating that this is suitable for dual-thread CPUs only - when there are more than 2 threads per core, the intended effect won't be achieved.

Quoted 7 lines

Why would that be? Won't higher thread count still share the same resources just more so?

Quoted 4 lines

I'd
recommend making the logic generic from the beginning, but if that doesn't seem feasible to you, at least a comment stating the limitation should be added imo.

Quoted 4 lines

Sure. but just want to know how many commercial x86 CPU uses >2 SMTs? Write a short, quick function to do random selection in SMT is quite complicate considering cpumask maybe just contain random number SMT siblings in a core.

Which is why I wrote that a second best solution would be to merely document the restriction in the source.

However, picking one out of more than 2 siblings shouldn't be _that_ difficult.

Quoted 9 lines
Quoted 5 lines

My objection to the whole lot is that its looks mightily expensive on large machines, cpumask operations aren't cheap when you've got 4k cpus etc..

Also, you very much cannot put cpumask_t on stack.

Sure, and do you has related data for this?

Quoted 8 lines

I just measured the cost of this function on my Romely EP(32 LCPUs) with cpumask_t and NR_CPUS = 32/256/512/4096, the cost are similar with 256/512/4096 and that increased about 20% time cost from 32.

I also tried to use cpumask_var_t and alloc it in heap(use CPUMASK_OFFSTACK), actually, it cost same time with cpumask_t in stack. But, the allocation bring another big cost. So, I use cpumask_t in stack. The performance gain data in commit log is getting with NR_CPUS = 256.

Perhaps using a per-CPU cpumask would be the better choice here (I can't see how preemption could validly be enabled when this code is utilized).

Jan

author
#45 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

Quoted 6 lines
Quoted 6 lines

I just measured the cost of this function on my Romely EP(32 LCPUs) with cpumask_t and NR_CPUS = 32/256/512/4096, the cost are similar with 256/512/4096 and that increased about 20% time cost from 32.

Quoted 6 lines
Quoted 4 lines

I also tried to use cpumask_var_t and alloc it in heap(use CPUMASK_OFFSTACK), actually, it cost same time with cpumask_t in stack. But, the allocation bring another big cost. So, I use cpumask_t in stack. The performance gain data in commit log is getting with NR_CPUS = 256.

Perhaps using a per-CPU cpumask would be the better choice here

See.

Quoted 2 lines

(I can't see how preemption could validly be enabled when this code is utilized).

Sorry, What's your meaning here?, the function is always in pre-empt safe mode.

Quoted 3 lines

Jan

#46 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

Quoted 15 lines
Quoted 7 lines
Quoted 7 lines · Alex Shi

I just measured the cost of this function on my Romely EP(32 LCPUs) with cpumask_t and NR_CPUS = 32/256/512/4096, the cost are similar with 256/512/4096 and that increased about 20% time cost from 32.

Quoted 6 lines
Quoted 4 lines

I also tried to use cpumask_var_t and alloc it in heap(use CPUMASK_OFFSTACK), actually, it cost same time with cpumask_t in stack. But, the allocation bring another big cost. So, I use cpumask_t in stack. The performance gain data in commit log is getting with NR_CPUS = 256.

Perhaps using a per-CPU cpumask would be the better choice here

Quoted 8 lines

See.

Quoted 2 lines

(I can't see how preemption could validly be enabled when this code is utilized).

Sorry, What's your meaning here?, the function is always in pre-empt safe mode.

That's exactly what I implied (I was merely pointing this out to prove that using per-CPU data here is possible).

Jan

#47 lore raw

Re: [PATCH v7 8/8] x86/tlb: just do tlb flush on one of siblings of SMT

In reply to Alex Shi
Quoted 6 lines · Alex Shi

Sure. but just want to know how many commercial x86 CPU uses >2 SMTs? Write a short, quick function to do random selection in SMT is quite complicate considering cpumask maybe just contain random number SMT siblings in a core.

Wrong question. This is a forward compatibility issue. You have to take into account potential future CPUs as well.

And no, it isn't hard.

-hpa