Skip to contentSkip to conversation

KVM paravirt remote flush tlb

PATCH In review Nikunj A. Dadhania 3 participants 23 messages Aug 21, 2012 – Sep 4, 2012

Subsystems (12)
lore
Share & tools
lore
#0 lore raw

v4 cover KVM paravirt remote flush tlb

Remote flushing api's does a busy wait which is fine in bare-metal scenario. But with-in the guest, the vcpus might have been pre-empted or blocked. In this scenario, the initator vcpu would end up busy-waiting for a long amount of time.

This was discovered in our gang scheduling test and other way to solve this is by para-virtualizing the flush_tlb_others_ipi(now shows up as smp_call_function_many after Alex Shi's TLB optimization)

This patch set implements para-virt flush tlbs making sure that it does not wait for vcpus that are sleeping. And all the sleeping vcpus flush the tlb on guest enter. Idea was discussed here:
https://lkml.org/lkml/2012/2/20/157

This also brings one more dependency for lock-less page walk that is performed by get_user_pages_fast(gup_fast). gup_fast disables the interrupt and assumes that the pages will not be freed during that period. And this was fine as the flush_tlb_others_ipi would wait for all the IPI to be processed and return back. With the new approach of not waiting for the sleeping vcpus, this assumption is not valid anymore. So now HAVE_RCU_TABLE_FREE is used to free the pages. This will make sure that all the cpus would atleast process smp_callback before the pages are freed.

Changelog from v3:
• Add helper for cleaning up vcpu_state information (Marcelo)
• Fix code for checking vs_page and leaking page refs (Marcelo)

Changelog from v2:
• Rebase to 3.5 based linus(commit - f7da9cd) kernel.
• Port PV-Flush to new TLB-Optimization code by Alex Shi
• Use pinned pages to avoid overhead during guest enter/exit (Marcelo)
• Remove kick, as this is not improving much
• Use bit fields in the state(flush_on_enter and vcpu_running) flag to avoid smp barriers (Marcelo)

Changelog from v1:
• Race fixes reported by Vatsa
• Address gup_fast dependency using PeterZ's rcu table free patch
• Fix rcu_table_free for hw pagetable walkers

Here are the results from PLE hardware. Here is the setup details:
• 32 CPUs (HT disabled)
• 64-bit VM
• 32vcpus
• 8GB RAM

base = 3.6-rc1 + ple handler optimization patch
pvflushv4 = 3.6-rc1 + ple handler optimization patch + pvflushv4 patch

kernbench(lower is better)
==========================
base pvflushv4 %improvement
1VM 48.5800 46.8513 3.55846

2VM   108.1823      104.6410       3.27346
3VM   183.2733      163.3547      10.86825

ebizzy(higher is better)
========================

         base         pvflushv4      %improvement
1VM     2414.5000     2089.8750     -13.44481
2VM     2167.6250     2371.7500      9.41699
3VM     1600.1111     2102.5556     31.40060

Thanks Raghu for running the tests.

[1] http://article.gmane.org/gmane.linux.kernel/1329752

---

Nikunj A. Dadhania (6):
KVM Guest: Add VCPU running/pre-empted state for guest
KVM-HV: Add VCPU running/pre-empted state for guest
KVM Guest: Add paravirt kvm_flush_tlb_others
KVM-HV: Add flush_on_enter before guest enter
Enable HAVE_RCU_TABLE_FREE for kvm when PARAVIRT_TLB_FLUSH is enabled
KVM-doc: Add paravirt tlb flush document

Peter Zijlstra (2):
mm, x86: Add HAVE_RCU_TABLE_FREE support
mm: Add missing TLB invalidate to RCU page-table freeing

 Documentation/virtual/kvm/msr.txt                |    4 +
 Documentation/virtual/kvm/paravirt-tlb-flush.txt |   53 ++++++++++++++
 arch/Kconfig                                     |    3 +
 arch/powerpc/Kconfig                             |    1 
 arch/sparc/Kconfig                               |    1 
 arch/x86/Kconfig                                 |   11 +++
 arch/x86/include/asm/kvm_host.h                  |    7 ++
 arch/x86/include/asm/kvm_para.h                  |   13 +++
 arch/x86/include/asm/tlb.h                       |    1 
 arch/x86/include/asm/tlbflush.h                  |   11 +++
 arch/x86/kernel/kvm.c                            |   38 ++++++++++
 arch/x86/kvm/cpuid.c                             |    1 
 arch/x86/kvm/x86.c                               |   84 +++++++++++++++++++++-
 arch/x86/mm/pgtable.c                            |    6 +-
 arch/x86/mm/tlb.c                                |   36 +++++++++
 include/asm-generic/tlb.h                        |    9 ++
 mm/memory.c                                      |   43 ++++++++++-
 17 files changed, 311 insertions(+), 11 deletions(-)
 create mode 100644 Documentation/virtual/kvm/paravirt-tlb-flush.txt
#1 lore raw

PATCH v4 1/8 mm, x86: Add HAVE_RCU_TABLE_FREE support

From: Peter Zijlstra <[email protected]>

Implements optional HAVE_RCU_TABLE_FREE support for x86.

This is useful for things like Xen and KVM where paravirt tlb flush means the software page table walkers like GUP-fast cannot rely on IRQs disabling like regular x86 can.

Not for inclusion - is part of PeterZ's "Unify TLB gather implementations"
http://mid.gmane.org/[email protected]

---
 arch/x86/include/asm/tlb.h |    1 +
 arch/x86/mm/pgtable.c      |    6 +++---
 include/asm-generic/tlb.h  |    9 +++++++++
 3 files changed, 13 insertions(+), 3 deletions(-)
arch/x86/include/asm/tlb.h +1 -0
index 4fef207..f5489f0 100644
@@ -1,6 +1,7 @@
 #ifndef _ASM_X86_TLB_H
 #define _ASM_X86_TLB_H
 
+#define __tlb_remove_table(table) free_page_and_swap_cache(table)
 #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)
arch/x86/mm/pgtable.c +3 -3
index 8573b83..34fa168 100644
@@ -51,21 +51,21 @@ void ___pte_free_tlb(struct mmu_gather *tlb, struct page *pte)
 {
 	pgtable_page_dtor(pte);
 	paravirt_release_pte(page_to_pfn(pte));
-	tlb_remove_page(tlb, pte);
+	tlb_remove_table(tlb, pte);
 }
 
 #if PAGETABLE_LEVELS > 2
 void ___pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd)
 {
 	paravirt_release_pmd(__pa(pmd) >> PAGE_SHIFT);
-	tlb_remove_page(tlb, virt_to_page(pmd));
+	tlb_remove_table(tlb, virt_to_page(pmd));
 }
 
 #if PAGETABLE_LEVELS > 3
 void ___pud_free_tlb(struct mmu_gather *tlb, pud_t *pud)
 {
 	paravirt_release_pud(__pa(pud) >> PAGE_SHIFT);
-	tlb_remove_page(tlb, virt_to_page(pud));
+	tlb_remove_table(tlb, virt_to_page(pud));
 }
 #endif	/* PAGETABLE_LEVELS > 3 */
 #endif	/* PAGETABLE_LEVELS > 2 */
include/asm-generic/tlb.h +9 -0
index ed6642a..d382b22 100644
@@ -19,6 +19,8 @@
 #include <asm/pgalloc.h>
 #include <asm/tlbflush.h>
 
+static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page);
+
 #ifdef CONFIG_HAVE_RCU_TABLE_FREE
 /*
  * Semi RCU freeing of the page directories.
@@ -60,6 +62,13 @@ struct mmu_table_batch {
 extern void tlb_table_flush(struct mmu_gather *tlb);
 extern void tlb_remove_table(struct mmu_gather *tlb, void *table);
 
+#else
+
+static inline void tlb_remove_table(struct mmu_gather *tlb, void *table)
+{
+	tlb_remove_page(tlb, table);
+}
+
 #endif
 
 /*
#2 lore raw

PATCH v4 2/8 mm: Add missing TLB invalidate to RCU page-table freeing

In reply to Nikunj A. Dadhania

From: Peter Zijlstra <[email protected]>

For normal systems we need a TLB invalidate before freeing the page-tables, the generic RCU based page-table freeing code lacked this.

This is because this code originally came from ppc where the hardware never walks the linux page-tables and thus this invalidate is not required.

Others, notably s390 which ran into this problem in cd94154cc6a ("[S390] fix tlb flushing for page table pages"), do very much need this TLB invalidation.

Therefore add it, with a Kconfig option to disable it so as to not unduly slow down PPC and SPARC64 which neither of them need it.

Not for inclusion - is part of PeterZ's "Unify TLB gather implementations"
http://mid.gmane.org/[email protected]

[Fix to check *batch is not NULL]
---
 arch/Kconfig         |    3 +++
 arch/powerpc/Kconfig |    1 +
 arch/sparc/Kconfig   |    1 +
 mm/memory.c          |   43 +++++++++++++++++++++++++++++++++++++------
 4 files changed, 42 insertions(+), 6 deletions(-)
arch/Kconfig +3 -0
index 8c3d957..fec1c9b 100644
@@ -231,6 +231,9 @@ config HAVE_ARCH_MUTEX_CPU_RELAX
 config HAVE_RCU_TABLE_FREE
 	bool
 
+config STRICT_TLB_FILL
+	bool
+
 config ARCH_HAVE_NMI_SAFE_CMPXCHG
 	bool
 
arch/powerpc/Kconfig +1 -0
index 9a5d3cd..fb70260 100644
@@ -127,6 +127,7 @@ config PPC
 	select GENERIC_IRQ_SHOW_LEVEL
 	select IRQ_FORCED_THREADING
 	select HAVE_RCU_TABLE_FREE if SMP
+	select STRICT_TLB_FILL
 	select HAVE_SYSCALL_TRACEPOINTS
 	select HAVE_BPF_JIT if PPC64
 	select HAVE_ARCH_JUMP_LABEL
arch/sparc/Kconfig +1 -0
index e74ff13..126e500 100644
@@ -52,6 +52,7 @@ config SPARC64
 	select HAVE_KRETPROBES
 	select HAVE_KPROBES
 	select HAVE_RCU_TABLE_FREE if SMP
+	select STRICT_TLB_FILL
 	select HAVE_MEMBLOCK
 	select HAVE_MEMBLOCK_NODE_MAP
 	select HAVE_SYSCALL_WRAPPERS
mm/memory.c +37 -6

60 lines not on this page · raw message

#3 lore raw

PATCH v4 3/8 KVM Guest: Add VCPU running/pre-empted state for guest

In reply to Nikunj A. Dadhania

From: Nikunj A. Dadhania <[email protected]>

The patch adds guest code for msr between guest and hypervisor. The msr will export the vcpu running/pre-empted information to the guest from host. This will enable guest to intelligently send ipi to running vcpus and set flag for pre-empted vcpus. This will prevent waiting for vcpus that are not running.

---
 arch/x86/include/asm/kvm_para.h |   13 +++++++++++++
 arch/x86/kernel/kvm.c           |   36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 0 deletions(-)
arch/x86/include/asm/kvm_para.h +13 -0
index 2f7712e..5dfb975 100644
@@ -23,6 +23,7 @@
 #define KVM_FEATURE_ASYNC_PF		4
 #define KVM_FEATURE_STEAL_TIME		5
 #define KVM_FEATURE_PV_EOI		6
+#define KVM_FEATURE_VCPU_STATE          7
 
 /* The last 8 bits are used to indicate how to interpret the flags field
  * in pvclock structure. If no bits are set, all flags are ignored.
@@ -39,6 +40,7 @@
 #define MSR_KVM_ASYNC_PF_EN 0x4b564d02
 #define MSR_KVM_STEAL_TIME  0x4b564d03
 #define MSR_KVM_PV_EOI_EN      0x4b564d04
+#define MSR_KVM_VCPU_STATE  0x4b564d05
 
 struct kvm_steal_time {
 	__u64 steal;
@@ -51,6 +53,17 @@ struct kvm_steal_time {
 #define KVM_STEAL_VALID_BITS ((-1ULL << (KVM_STEAL_ALIGNMENT_BITS + 1)))
 #define KVM_STEAL_RESERVED_MASK (((1 << KVM_STEAL_ALIGNMENT_BITS) - 1 ) << 1)
 
+struct kvm_vcpu_state {
+	__u64 state;
+	__u32 pad[14];
+};
+/* bits in vcpu_state->state */
+#define KVM_VCPU_STATE_IN_GUEST_MODE 0
+#define KVM_VCPU_STATE_SHOULD_FLUSH  1
+
+#define KVM_VCPU_STATE_ALIGN_BITS 5
+#define KVM_VCPU_STATE_VALID_BITS ((-1ULL << (KVM_VCPU_STATE_ALIGN_BITS + 1)))
+
 #define KVM_MAX_MMU_OP_BATCH           32
 
 #define KVM_ASYNC_PF_ENABLED			(1 << 0)
arch/x86/kernel/kvm.c +36 -0
index c1d61ee..37e6599 100644
@@ -66,6 +66,9 @@ static DEFINE_PER_CPU(struct kvm_vcpu_pv_apf_data, apf_reason) __aligned(64);
 static DEFINE_PER_CPU(struct kvm_steal_time, steal_time) __aligned(64);
 static int has_steal_clock = 0;
 
+DEFINE_PER_CPU(struct kvm_vcpu_state, vcpu_state) __aligned(64);
+static int has_vcpu_state;
+
 /*
  * No need for any "IO delay" on KVM
  */
@@ -302,6 +305,22 @@ static void kvm_guest_apic_eoi_write(u32 reg, u32 val)
 	apic_write(APIC_EOI, APIC_EOI_ACK);
 }
 
+static void kvm_register_vcpu_state(void)
+{
+	int cpu = smp_processor_id();
+	struct kvm_vcpu_state *v_state;
+
+	if (!has_vcpu_state)
+		return;
+
+	v_state = &per_cpu(vcpu_state, cpu);
+	memset(v_state, 0, sizeof(*v_state));
+
+	wrmsrl(MSR_KVM_VCPU_STATE, (__pa(v_state) | KVM_MSR_ENABLED));
+	printk(KERN_INFO "kvm-vcpustate: cpu %d, msr %lx\n",
+		cpu, __pa(v_state));
+}
+
 void __cpuinit kvm_guest_cpu_init(void)
 {
 	if (!kvm_para_available())
@@ -330,6 +349,9 @@ void __cpuinit kvm_guest_cpu_init(void)
 
 	if (has_steal_clock)
 		kvm_register_steal_time();
+
+	if (has_vcpu_state)
+		kvm_register_vcpu_state();
 }
 
 static void kvm_pv_disable_apf(void)
@@ -393,6 +415,14 @@ void kvm_disable_steal_time(void)
 	wrmsr(MSR_KVM_STEAL_TIME, 0, 0);
 }
 
+void kvm_disable_vcpu_state(void)
+{
+	if (!has_vcpu_state)
+		return;
+
+	wrmsr(MSR_KVM_VCPU_STATE, 0, 0);
+}
+
 #ifdef CONFIG_SMP
 static void __init kvm_smp_prepare_boot_cpu(void)
 {
@@ -410,6 +440,7 @@ static void __cpuinit kvm_guest_cpu_online(void *dummy)
 
 static void kvm_guest_cpu_offline(void *dummy)
 {
+	kvm_disable_vcpu_state();
 	kvm_disable_steal_time();
 	if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
 		wrmsrl(MSR_KVM_PV_EOI_EN, 0);
@@ -469,6 +500,11 @@ void __init kvm_guest_init(void)
 	if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
 		apic_set_eoi_write(kvm_guest_apic_eoi_write);
 
+#ifdef CONFIG_PARAVIRT_TLB_FLUSH
+	if (kvm_para_has_feature(KVM_FEATURE_VCPU_STATE))
+		has_vcpu_state = 1;
+#endif
+
 #ifdef CONFIG_SMP
 	smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
 	register_cpu_notifier(&kvm_cpu_notifier);
#4 lore raw

Re: [PATCH v4 3/8] KVM Guest: Add VCPU running/pre-empted state for guest

Quoted 101 lines · Nikunj A. Dadhania

From: Nikunj A. Dadhania <[email protected]>

The patch adds guest code for msr between guest and hypervisor. The msr will export the vcpu running/pre-empted information to the guest from host. This will enable guest to intelligently send ipi to running vcpus and set flag for pre-empted vcpus. This will prevent waiting for vcpus that are not running.

---
 arch/x86/include/asm/kvm_para.h |   13 +++++++++++++
 arch/x86/kernel/kvm.c           |   36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 0 deletions(-)
arch/x86/include/asm/kvm_para.h +13 -0
index 2f7712e..5dfb975 100644
@@ -23,6 +23,7 @@
 #define KVM_FEATURE_ASYNC_PF		4
 #define KVM_FEATURE_STEAL_TIME		5
 #define KVM_FEATURE_PV_EOI		6
+#define KVM_FEATURE_VCPU_STATE          7
 
 /* The last 8 bits are used to indicate how to interpret the flags field
  * in pvclock structure. If no bits are set, all flags are ignored.
@@ -39,6 +40,7 @@
 #define MSR_KVM_ASYNC_PF_EN 0x4b564d02
 #define MSR_KVM_STEAL_TIME  0x4b564d03
 #define MSR_KVM_PV_EOI_EN      0x4b564d04
+#define MSR_KVM_VCPU_STATE  0x4b564d05
 
 struct kvm_steal_time {
 	__u64 steal;
@@ -51,6 +53,17 @@ struct kvm_steal_time {
 #define KVM_STEAL_VALID_BITS ((-1ULL << (KVM_STEAL_ALIGNMENT_BITS + 1)))
 #define KVM_STEAL_RESERVED_MASK (((1 << KVM_STEAL_ALIGNMENT_BITS) - 1 ) << 1)
 
+struct kvm_vcpu_state {
+	__u64 state;
+	__u32 pad[14];
+};
+/* bits in vcpu_state->state */
+#define KVM_VCPU_STATE_IN_GUEST_MODE 0
+#define KVM_VCPU_STATE_SHOULD_FLUSH  1
+
+#define KVM_VCPU_STATE_ALIGN_BITS 5
+#define KVM_VCPU_STATE_VALID_BITS ((-1ULL << (KVM_VCPU_STATE_ALIGN_BITS + 1)))
+
 #define KVM_MAX_MMU_OP_BATCH           32
 
 #define KVM_ASYNC_PF_ENABLED			(1 << 0)
arch/x86/kernel/kvm.c +22 -0
index c1d61ee..37e6599 100644
@@ -66,6 +66,9 @@ static DEFINE_PER_CPU(struct kvm_vcpu_pv_apf_data, apf_reason) __aligned(64);
 static DEFINE_PER_CPU(struct kvm_steal_time, steal_time) __aligned(64);
 static int has_steal_clock = 0;
 
+DEFINE_PER_CPU(struct kvm_vcpu_state, vcpu_state) __aligned(64);
+static int has_vcpu_state;
+
 /*
  * No need for any "IO delay" on KVM
  */
@@ -302,6 +305,22 @@ static void kvm_guest_apic_eoi_write(u32 reg, u32 val)
 	apic_write(APIC_EOI, APIC_EOI_ACK);
 }
 
+static void kvm_register_vcpu_state(void)
+{
+	int cpu = smp_processor_id();
+	struct kvm_vcpu_state *v_state;
+
+	if (!has_vcpu_state)
+		return;
+
+	v_state = &per_cpu(vcpu_state, cpu);
+	memset(v_state, 0, sizeof(*v_state));
+
+	wrmsrl(MSR_KVM_VCPU_STATE, (__pa(v_state) | KVM_MSR_ENABLED));
+	printk(KERN_INFO "kvm-vcpustate: cpu %d, msr %lx\n",
+		cpu, __pa(v_state));
+}
+
 void __cpuinit kvm_guest_cpu_init(void)
 {
 	if (!kvm_para_available())
@@ -330,6 +349,9 @@ void __cpuinit kvm_guest_cpu_init(void)
 
 	if (has_steal_clock)
 		kvm_register_steal_time();
+
+	if (has_vcpu_state)
+		kvm_register_vcpu_state();
 }
 
 static void kvm_pv_disable_apf(void)

arch/x86/kernel/kvm.c void kvm_disable_steal_time(void) · line 423

Quoted 9 lines
arch/x86/kernel/kvm.c +6 -0
@@ -393,6 +415,14 @@ void kvm_disable_steal_time(void)
 	wrmsr(MSR_KVM_STEAL_TIME, 0, 0);
 }
 
+void kvm_disable_vcpu_state(void)
+{
+	if (!has_vcpu_state)
+		return;
+
+	wrmsr(MSR_KVM_VCPU_STATE, 0, 0);

wrmsrl (to be consistent).

arch/x86/kernel/kvm.c static void __cpuinit kvm_guest_cpu_online(void *dummy) · line 443

Quoted 10 lines
arch/x86/kernel/kvm.c +3 -0
+}
+
 #ifdef CONFIG_SMP
 static void __init kvm_smp_prepare_boot_cpu(void)
 {
@@ -410,6 +440,7 @@ static void __cpuinit kvm_guest_cpu_online(void *dummy)
 
 static void kvm_guest_cpu_offline(void *dummy)
 {
+	kvm_disable_vcpu_state();

Should disable MSR at kvm_pv_guest_cpu_reboot.

arch/x86/kernel/kvm.c void __init kvm_guest_init(void) · line 506

Quoted 11 lines
arch/x86/kernel/kvm.c +4 -0
 	kvm_disable_steal_time();
 	if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
 		wrmsrl(MSR_KVM_PV_EOI_EN, 0);
@@ -469,6 +500,11 @@ void __init kvm_guest_init(void)
 	if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
 		apic_set_eoi_write(kvm_guest_apic_eoi_write);
 
+#ifdef CONFIG_PARAVIRT_TLB_FLUSH
+	if (kvm_para_has_feature(KVM_FEATURE_VCPU_STATE))
+		has_vcpu_state = 1;
+#endif

Why only this hunk guarded by CONFIG_PARAVIRT_TLB_FLUSH and not the rest of the code?

Is there a switch to enable/disable this feature on the kernel command line? Grep for early_param in kvm.c.

#5 lore raw

Re: [PATCH v4 3/8] KVM Guest: Add VCPU running/pre-empted state for guest

Quoted 11 lines · Marcelo Tosatti

arch/x86/kernel/kvm.c void kvm_disable_steal_time(void) · line 423

Quoted 7 lines · Nikunj A. Dadhania
arch/x86/kernel/kvm.c +6 -0
+void kvm_disable_vcpu_state(void)
+{
+	if (!has_vcpu_state)
+		return;
+
+	wrmsr(MSR_KVM_VCPU_STATE, 0, 0);

wrmsrl (to be consistent).

Sure, will change

Quoted 7 lines
Quoted 7 lines
arch/x86/kernel/kvm.c +2 -0
+}
+
 #ifdef CONFIG_SMP
 static void __init kvm_smp_prepare_boot_cpu(void)
 {
@@ -410,6 +440,7 @@ static void __cpuinit kvm_guest_cpu_online(void *dummy)
 
Quoted 6 lines

arch/x86/kernel/kvm.c static void __cpuinit kvm_guest_cpu_online(void *dummy) · line 443

Quoted 3 lines
arch/x86/kernel/kvm.c +1 -0
 static void kvm_guest_cpu_offline(void *dummy)
 {
+	kvm_disable_vcpu_state();

Should disable MSR at kvm_pv_guest_cpu_reboot.

Sure, can you explain the difference for my understanding?

Quoted 7 lines
Quoted 7 lines
arch/x86/kernel/kvm.c +0 -0
 	kvm_disable_steal_time();
 	if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
 		wrmsrl(MSR_KVM_PV_EOI_EN, 0);
@@ -469,6 +500,11 @@ void __init kvm_guest_init(void)
 	if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
 		apic_set_eoi_write(kvm_guest_apic_eoi_write);
 
Quoted 8 lines

arch/x86/kernel/kvm.c void __init kvm_guest_init(void) · line 506

Quoted 4 lines
arch/x86/kernel/kvm.c +4 -0
+#ifdef CONFIG_PARAVIRT_TLB_FLUSH
+	if (kvm_para_has_feature(KVM_FEATURE_VCPU_STATE))
+		has_vcpu_state = 1;
+#endif

Why only this hunk guarded by CONFIG_PARAVIRT_TLB_FLUSH and not the rest of the code?

The guest should have been compiled with CONFIG_PARAVIRT_TLB_FLUSH, as the config also brings in HAVE_RCU_TABLE_FREE code into picture. We should not enable this code without HAVE_RCU_TABLE_FREE.

Did not want to spray this across all the code, as the compiler will take care of throwing out the kvm_tlb_flush_others

Quoted 3 lines

Is there a switch to enable/disable this feature on the kernel command line?

No, havent added it.

Quoted 2 lines

Grep for early_param in kvm.c.

Let me know if that is required.

Regards
Nikunj

#6 lore raw

Re: [PATCH v4 3/8] KVM Guest: Add VCPU running/pre-empted state for guest

Quoted 21 lines · Nikunj A Dadhania
Quoted 11 lines · Marcelo Tosatti

arch/x86/kernel/kvm.c void kvm_disable_steal_time(void) · line 423

Quoted 7 lines · Nikunj A. Dadhania
arch/x86/kernel/kvm.c +6 -0
+void kvm_disable_vcpu_state(void)
+{
+	if (!has_vcpu_state)
+		return;
+
+	wrmsr(MSR_KVM_VCPU_STATE, 0, 0);

wrmsrl (to be consistent).

Sure, will change

Quoted 7 lines
Quoted 7 lines
arch/x86/kernel/kvm.c +2 -0
+}
+
 #ifdef CONFIG_SMP
 static void __init kvm_smp_prepare_boot_cpu(void)
 {
@@ -410,6 +440,7 @@ static void __cpuinit kvm_guest_cpu_online(void *dummy)
 
Quoted 7 lines
Quoted 6 lines

arch/x86/kernel/kvm.c static void __cpuinit kvm_guest_cpu_online(void *dummy) · line 443

Quoted 3 lines
arch/x86/kernel/kvm.c +1 -0
 static void kvm_guest_cpu_offline(void *dummy)
 {
+	kvm_disable_vcpu_state();

Should disable MSR at kvm_pv_guest_cpu_reboot.

Sure, can you explain the difference for my understanding?

These are different callbacks. One is used for CPU offline, the other during reboot.

Quoted 22 lines
Quoted 7 lines
Quoted 7 lines
arch/x86/kernel/kvm.c +0 -0
 	kvm_disable_steal_time();
 	if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
 		wrmsrl(MSR_KVM_PV_EOI_EN, 0);
@@ -469,6 +500,11 @@ void __init kvm_guest_init(void)
 	if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
 		apic_set_eoi_write(kvm_guest_apic_eoi_write);
 
Quoted 8 lines

arch/x86/kernel/kvm.c void __init kvm_guest_init(void) · line 506

Quoted 4 lines
arch/x86/kernel/kvm.c +4 -0
+#ifdef CONFIG_PARAVIRT_TLB_FLUSH
+	if (kvm_para_has_feature(KVM_FEATURE_VCPU_STATE))
+		has_vcpu_state = 1;
+#endif

Why only this hunk guarded by CONFIG_PARAVIRT_TLB_FLUSH and not the rest of the code?

The guest should have been compiled with CONFIG_PARAVIRT_TLB_FLUSH, as the config also brings in HAVE_RCU_TABLE_FREE code into picture. We should not enable this code without HAVE_RCU_TABLE_FREE.

Did not want to spray this across all the code, as the compiler will take care of throwing out the kvm_tlb_flush_others

Quoted 8 lines
Quoted 3 lines

Is there a switch to enable/disable this feature on the kernel command line?

No, havent added it.

Quoted 2 lines

Grep for early_param in kvm.c.

Let me know if that is required.

Yes, please add it. Its useful.

#7 lore raw

Re: [PATCH v4 3/8] KVM Guest: Add VCPU running/pre-empted state for guest

Quoted 3 lines · Marcelo Tosatti
Quoted 2 lines · Nikunj A Dadhania
Quoted 1 line · Marcelo Tosatti

On Tue, Aug 21, 2012 at 04:56:35PM +0530, Nikunj A. Dadhania wrote:

[...]

Quoted 22 lines
Quoted 22 lines
Quoted 4 lines
Quoted 4 lines
arch/x86/kernel/kvm.c +0 -0
@@ -469,6 +500,11 @@ void __init kvm_guest_init(void)
 	if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
 		apic_set_eoi_write(kvm_guest_apic_eoi_write);
 
Quoted 8 lines

arch/x86/kernel/kvm.c void __init kvm_guest_init(void) · line 506

Quoted 4 lines
arch/x86/kernel/kvm.c +4 -0
+#ifdef CONFIG_PARAVIRT_TLB_FLUSH
+	if (kvm_para_has_feature(KVM_FEATURE_VCPU_STATE))
+		has_vcpu_state = 1;
+#endif

Why only this hunk guarded by CONFIG_PARAVIRT_TLB_FLUSH and not the rest of the code?

The guest should have been compiled with CONFIG_PARAVIRT_TLB_FLUSH, as the config also brings in HAVE_RCU_TABLE_FREE code into picture. We should not enable this code without HAVE_RCU_TABLE_FREE.

Did not want to spray this across all the code, as the compiler will take care of throwing out the kvm_tlb_flush_others

Quoted 3 lines

Is there a switch to enable/disable this feature on the kernel command line?

Quoted 8 lines
Quoted 5 lines

No, havent added it.

Quoted 2 lines

Grep for early_param in kvm.c.

Let me know if that is required.

Yes, please add it. Its useful.

Done, will send it in my next version.

Nikunj

#8 lore raw

PATCH v4 4/8 KVM-HV: Add VCPU running/pre-empted state for guest

In reply to Nikunj A. Dadhania

From: Nikunj A. Dadhania <[email protected]>

Hypervisor code to indicate guest running/pre-empteded status through msr. The page is now pinned during MSR write time and use kmap_atomic/kunmap_atomic to access the shared area vcpu_state area.

---
 arch/x86/include/asm/kvm_host.h |    7 +++
 arch/x86/kvm/cpuid.c            |    1 
 arch/x86/kvm/x86.c              |   88 ++++++++++++++++++++++++++++++++++++++-
 3 files changed, 94 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +7 -0
index 09155d6..441348f 100644
@@ -429,6 +429,13 @@ struct kvm_vcpu_arch {
 		struct kvm_steal_time steal;
 	} st;
 
+	/* indicates vcpu is running or preempted */
+	struct {
+		u64 msr_val;
+		struct page *vs_page;
+		unsigned int vs_offset;
+	} v_state;
+
 	u64 last_guest_tsc;
 	u64 last_kernel_ns;
 	u64 last_host_tsc;
arch/x86/kvm/cpuid.c +1 -0
index 0595f13..37ab364 100644
@@ -411,6 +411,7 @@ static int do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
 			     (1 << KVM_FEATURE_CLOCKSOURCE2) |
 			     (1 << KVM_FEATURE_ASYNC_PF) |
 			     (1 << KVM_FEATURE_PV_EOI) |
+			     (1 << KVM_FEATURE_VCPU_STATE) |
 			     (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT);
 
 		if (sched_info_on())
arch/x86/kvm/x86.c +86 -2

147 lines not on this page · raw message

#9 lore raw

Re: [PATCH v4 4/8] KVM-HV: Add VCPU running/pre-empted state for guest

Quoted 81 lines · Nikunj A. Dadhania

From: Nikunj A. Dadhania <[email protected]>

Hypervisor code to indicate guest running/pre-empteded status through msr. The page is now pinned during MSR write time and use kmap_atomic/kunmap_atomic to access the shared area vcpu_state area.

---
 arch/x86/include/asm/kvm_host.h |    7 +++
 arch/x86/kvm/cpuid.c            |    1 
 arch/x86/kvm/x86.c              |   88 ++++++++++++++++++++++++++++++++++++++-
 3 files changed, 94 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +7 -0
index 09155d6..441348f 100644
@@ -429,6 +429,13 @@ struct kvm_vcpu_arch {
 		struct kvm_steal_time steal;
 	} st;
 
+	/* indicates vcpu is running or preempted */
+	struct {
+		u64 msr_val;
+		struct page *vs_page;
+		unsigned int vs_offset;
+	} v_state;
+
 	u64 last_guest_tsc;
 	u64 last_kernel_ns;
 	u64 last_host_tsc;
arch/x86/kvm/cpuid.c +1 -0
index 0595f13..37ab364 100644
@@ -411,6 +411,7 @@ static int do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
 			     (1 << KVM_FEATURE_CLOCKSOURCE2) |
 			     (1 << KVM_FEATURE_ASYNC_PF) |
 			     (1 << KVM_FEATURE_PV_EOI) |
+			     (1 << KVM_FEATURE_VCPU_STATE) |
 			     (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT);
 
 		if (sched_info_on())
arch/x86/kvm/x86.c +15 -2
index 59b5950..43f2c19 100644
@@ -806,13 +806,13 @@ EXPORT_SYMBOL_GPL(kvm_rdpmc);
  * kvm-specific. Those are put in the beginning of the list.
  */
 
-#define KVM_SAVE_MSRS_BEGIN	9
+#define KVM_SAVE_MSRS_BEGIN	10
 static u32 msrs_to_save[] = {
 	MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
 	MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
 	HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
 	HV_X64_MSR_APIC_ASSIST_PAGE, MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
-	MSR_KVM_PV_EOI_EN,
+	MSR_KVM_VCPU_STATE, MSR_KVM_PV_EOI_EN,
 	MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
 	MSR_STAR,
 #ifdef CONFIG_X86_64
@@ -1557,6 +1557,63 @@ static void record_steal_time(struct kvm_vcpu *vcpu)
 		&vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
 }
 
+static void kvm_set_atomic(u64 *addr, u64 old, u64 new)
+{
+	int loop = 1000000;
+	while (1) {
+		if (cmpxchg(addr, old, new) == old)
+			break;
+		loop--;
+		if (!loop) {
+			pr_info("atomic cur: %lx old: %lx new: %lx\n",
+				*addr, old, new);
+			break;
+		}
+	}

arch/x86/kvm/x86.c static void record_steal_time(struct kvm_vcpu *vcpu) · line 1582

Quoted 10 lines
arch/x86/kvm/x86.c +10 -0
+}
+
+static void kvm_set_vcpu_state(struct kvm_vcpu *vcpu)
+{
+	struct kvm_vcpu_state *vs;
+	char *kaddr;
+
+	if (!((vcpu->arch.v_state.msr_val & KVM_MSR_ENABLED) &&
+			vcpu->arch.v_state.vs_page))
+		return;

It was agreed it was necessary to have valid vs_page only if MSR was enabled? Or was that a misunderstanding?

Looks good otherwise.

#10 lore raw

Re: [PATCH v4 4/8] KVM-HV: Add VCPU running/pre-empted state for guest

Quoted 10 lines · Marcelo Tosatti
Quoted 9 lines · Nikunj A. Dadhania

From: Nikunj A. Dadhania <[email protected]>

Hypervisor code to indicate guest running/pre-empteded status through msr. The page is now pinned during MSR write time and use kmap_atomic/kunmap_atomic to access the shared area vcpu_state area.

---

[...]

Quoted 4 lines
Quoted 4 lines
arch/x86/kvm/x86.c +4 -0
+
+static void kvm_set_vcpu_state(struct kvm_vcpu *vcpu)
+{
+	struct kvm_vcpu_state *vs;
Quoted 9 lines

arch/x86/kvm/x86.c static void record_steal_time(struct kvm_vcpu *vcpu) · line 1582

Quoted 5 lines
arch/x86/kvm/x86.c +5 -0
+	char *kaddr;
+
+	if (!((vcpu->arch.v_state.msr_val & KVM_MSR_ENABLED) &&
+			vcpu->arch.v_state.vs_page))
+		return;

It was agreed it was necessary to have valid vs_page only if MSR was enabled? Or was that a misunderstanding?

There is a case where MSR is enabled but vs_page is NULL, this is gaurding that case. The check is now:

if (!(msr_enabled && vs_page))
return;

I had proposed that here:
http://www.spinics.net/lists/kvm/msg77147.html

Regards
Nikunj

#11 lore raw

Re: [PATCH v4 4/8] KVM-HV: Add VCPU running/pre-empted state for guest

Quoted 27 lines · Nikunj A Dadhania
Quoted 10 lines · Marcelo Tosatti
Quoted 9 lines · Nikunj A. Dadhania

From: Nikunj A. Dadhania <[email protected]>

Hypervisor code to indicate guest running/pre-empteded status through msr. The page is now pinned during MSR write time and use kmap_atomic/kunmap_atomic to access the shared area vcpu_state area.

---

[...]

Quoted 4 lines
Quoted 4 lines
arch/x86/kvm/x86.c +4 -0
+
+static void kvm_set_vcpu_state(struct kvm_vcpu *vcpu)
+{
+	struct kvm_vcpu_state *vs;
Quoted 9 lines

arch/x86/kvm/x86.c static void record_steal_time(struct kvm_vcpu *vcpu) · line 1582

Quoted 5 lines
arch/x86/kvm/x86.c +5 -0
+	char *kaddr;
+
+	if (!((vcpu->arch.v_state.msr_val & KVM_MSR_ENABLED) &&
+			vcpu->arch.v_state.vs_page))
+		return;

It was agreed it was necessary to have valid vs_page only if MSR was enabled? Or was that a misunderstanding?

Quoted 8 lines

There is a case where MSR is enabled but vs_page is NULL, this is gaurding that case. The check is now:

if (!(msr_enabled && vs_page))
return;

I had proposed that here:
http://www.spinics.net/lists/kvm/msg77147.html

OK, its fine.

#12 lore raw

PATCH v4 5/8 KVM Guest: Add paravirt kvm_flush_tlb_others

In reply to Nikunj A. Dadhania

From: Nikunj A. Dadhania <[email protected]>

flush_tlb_others_ipi depends on lot of statics in tlb.c. Replicated the flush_tlb_others_ipi as kvm_flush_tlb_others to further adapt to paravirtualization.

Use the vcpu state information inside the kvm_flush_tlb_others to avoid sending ipi to pre-empted vcpus.

* Do not send ipi's to offline vcpus and set flush_on_enter flag
* For online vcpus: Wait for them to clear the flag

The approach was discussed here: https://lkml.org/lkml/2012/2/20/157

v3:
* use only one state variable for vcpu-running/flush_on_enter
* use cmpxchg to update the state
* adapt to Alex Shi's TLB flush optimization

v2:
* use ACCESS_ONCE so the value is not register cached
* Separate HV and Guest code

--
Pseudo Algo:
------------
Hypervisor
==========
guest_exit()

       if (!(xchg(state, NOT_IN_GUEST) == SHOULD_FLUSH))
           tlb_flush(vcpu);

guest_enter()

       if (!(xchg(state, IN_GUEST) == SHOULD_FLUSH))
	   tlb_flush(vcpu);

    Guest
    =====
    flushcpumask = cpumask;
    for_each_cpu(i, flushmask) {
        state = vs->state;
        if(!test_bit(IN_GUEST_MODE, state)) {
            if (cmpxchg(&vs->state, state,
                        state | (1 << SHOULD_FLUSH)) == SUCCESS)
	       cpumask_clear_cpu(flushmask,i)
        }
    }

    smp_call_function_many(f->flushmask, flush_tlb_func)
---
 arch/x86/include/asm/tlbflush.h |   11 +++++++++++
 arch/x86/kernel/kvm.c           |    4 +++-
 arch/x86/mm/tlb.c               |   36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 50 insertions(+), 1 deletions(-)
arch/x86/include/asm/tlbflush.h +11 -0
index 74a4433..0a343a1 100644
@@ -119,6 +119,13 @@ static inline void native_flush_tlb_others(const struct cpumask *cpumask,
 {
 }
 
+static inline void kvm_flush_tlb_others(const struct cpumask *cpumask,
+					struct mm_struct *mm,
+					unsigned long start,
+					unsigned long end)
+{
+}
+
 static inline void reset_lazy_tlbstate(void)
 {
 }
@@ -153,6 +160,10 @@ void native_flush_tlb_others(const struct cpumask *cpumask,
 				struct mm_struct *mm,
 				unsigned long start, unsigned long end);
 
+void kvm_flush_tlb_others(const struct cpumask *cpumask,
+			struct mm_struct *mm, unsigned long start,
+			unsigned long end);
+
 #define TLBSTATE_OK	1
 #define TLBSTATE_LAZY	2
 
arch/x86/kernel/kvm.c +3 -1
index 37e6599..b538a31 100644
@@ -501,8 +501,10 @@ void __init kvm_guest_init(void)
 		apic_set_eoi_write(kvm_guest_apic_eoi_write);
 
 #ifdef CONFIG_PARAVIRT_TLB_FLUSH
-	if (kvm_para_has_feature(KVM_FEATURE_VCPU_STATE))
+	if (kvm_para_has_feature(KVM_FEATURE_VCPU_STATE)) {
 		has_vcpu_state = 1;
+		pv_mmu_ops.flush_tlb_others = kvm_flush_tlb_others;
+	}
 #endif
 
 #ifdef CONFIG_SMP
arch/x86/mm/tlb.c +36 -0
index 613cd83..645df99 100644
@@ -6,6 +6,7 @@
 #include <linux/interrupt.h>
 #include <linux/module.h>
 #include <linux/cpu.h>
+#include <linux/kvm_para.h>
 
 #include <asm/tlbflush.h>
 #include <asm/mmu_context.h>
@@ -119,6 +120,41 @@ static void flush_tlb_func(void *info)
 
 }
 
+#ifdef CONFIG_KVM_GUEST
+
+DECLARE_PER_CPU(struct kvm_vcpu_state, vcpu_state) __aligned(64);
+
+void kvm_flush_tlb_others(const struct cpumask *cpumask,
+			struct mm_struct *mm, unsigned long start,
+			unsigned long end)
+{
+	struct flush_tlb_info info;
+	struct kvm_vcpu_state *v_state;
+	u64 state;
+	int cpu;
+	cpumask_t flushmask;
+
+	cpumask_copy(&flushmask, cpumask);
+	info.flush_mm = mm;
+	info.flush_start = start;
+	info.flush_end = end;
+	/*
+	 * We have to call flush only on online vCPUs. And
+	 * queue flush_on_enter for pre-empted vCPUs
+	 */
+	for_each_cpu(cpu, to_cpumask(&flushmask)) {
+		v_state = &per_cpu(vcpu_state, cpu);
+		state = v_state->state;
+		if (!test_bit(KVM_VCPU_STATE_IN_GUEST_MODE, &state)) {
+			if (cmpxchg(&v_state->state, state, state | 1 << KVM_VCPU_STATE_SHOULD_FLUSH))
+				cpumask_clear_cpu(cpu, to_cpumask(&flushmask));
+		}
+	}
+
+	smp_call_function_many(&flushmask, flush_tlb_func, &info, 1);
+}
+#endif /* CONFIG_KVM_GUEST */
+
 void native_flush_tlb_others(const struct cpumask *cpumask,
 				 struct mm_struct *mm, unsigned long start,
 				 unsigned long end)
#13 lore raw

PATCH v4 6/8 KVM-HV: Add flush_on_enter before guest enter

In reply to Nikunj A. Dadhania

PV-Flush guest would indicate to flush on enter, flush the TLB before entering and exiting the guest.

---
 arch/x86/kvm/x86.c |   28 ++++++++++++----------------
 1 files changed, 12 insertions(+), 16 deletions(-)
arch/x86/kvm/x86.c +12 -16
index 43f2c19..07fdb0f 100644
@@ -1557,20 +1557,9 @@ static void record_steal_time(struct kvm_vcpu *vcpu)
 		&vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
 }
 
-static void kvm_set_atomic(u64 *addr, u64 old, u64 new)
-{
-	int loop = 1000000;
-	while (1) {
-		if (cmpxchg(addr, old, new) == old)
-			break;
-		loop--;
-		if (!loop) {
-			pr_info("atomic cur: %lx old: %lx new: %lx\n",
-				*addr, old, new);
-			break;
-		}
-	}
-}
+#define VS_NOT_IN_GUEST      (0)
+#define VS_IN_GUEST          (1 << KVM_VCPU_STATE_IN_GUEST_MODE)
+#define VS_SHOULD_FLUSH      (1 << KVM_VCPU_STATE_SHOULD_FLUSH)
 
 static void kvm_set_vcpu_state(struct kvm_vcpu *vcpu)
 {
@@ -1584,7 +1573,13 @@ static void kvm_set_vcpu_state(struct kvm_vcpu *vcpu)
 	kaddr = kmap_atomic(vcpu->arch.v_state.vs_page);
 	kaddr += vcpu->arch.v_state.vs_offset;
 	vs = kaddr;
-	kvm_set_atomic(&vs->state, 0, 1 << KVM_VCPU_STATE_IN_GUEST_MODE);
+	if (xchg(&vs->state, VS_IN_GUEST) == VS_SHOULD_FLUSH) {
+		/* 
+		 * Do TLB_FLUSH before entering the guest, its passed
+		 * the stage of request checking 
+		 */
+		kvm_x86_ops->tlb_flush(vcpu);
+	}
 	kunmap_atomic(kaddr);
 }
 
@@ -1600,7 +1595,8 @@ static void kvm_clear_vcpu_state(struct kvm_vcpu *vcpu)
 	kaddr = kmap_atomic(vcpu->arch.v_state.vs_page);
 	kaddr += vcpu->arch.v_state.vs_offset;
 	vs = kaddr;
-	kvm_set_atomic(&vs->state, 1 << KVM_VCPU_STATE_IN_GUEST_MODE, 0);
+	if (xchg(&vs->state, VS_NOT_IN_GUEST) == VS_SHOULD_FLUSH)
+		kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
 	kunmap_atomic(kaddr);
 }
 
#14 lore raw

PATCH v4 7/8 Enable HAVE_RCU_TABLE_FREE for kvm when PARAVIRT_TLB_FLUSH is enabled

In reply to Nikunj A. Dadhania
---
 arch/x86/Kconfig |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
arch/x86/Kconfig +11 -0
index c70684f..354160d 100644
@@ -612,6 +612,17 @@ config PARAVIRT_SPINLOCKS
 
 	  If you are unsure how to answer this question, answer N.
 
+config PARAVIRT_TLB_FLUSH
+       bool "Paravirtualization layer for TLB Flush"
+       depends on PARAVIRT && SMP && EXPERIMENTAL
+       select HAVE_RCU_TABLE_FREE
+       ---help---
+         Paravirtualized Flush TLB replace the native implementation
+         with something virtualization-friendly (for example, set a
+         flag for sleeping vcpu and do not wait for it).
+
+         If you are unsure how to answer this question, answer N.
+
 config PARAVIRT_CLOCK
 	bool
 
#15 lore raw

PATCH v4 8/8 KVM-doc: Add paravirt tlb flush document

In reply to Nikunj A. Dadhania
---
 Documentation/virtual/kvm/msr.txt                |    4 ++
 Documentation/virtual/kvm/paravirt-tlb-flush.txt |   53 ++++++++++++++++++++++
 2 files changed, 57 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/virtual/kvm/paravirt-tlb-flush.txt
Documentation/virtual/kvm/msr.txt +4 -0
index 7304710..92a6af6 100644
@@ -256,3 +256,7 @@ MSR_KVM_EOI_EN: 0x4b564d04
 	guest must both read the least significant bit in the memory area and
 	clear it using a single CPU instruction, such as test and clear, or
 	compare and exchange.
+
+MSR_KVM_VCPU_STATE: 0x4b564d05
+
+Refer: Documentation/virtual/kvm/paravirt-tlb-flush.txt
Documentation/virtual/kvm/paravirt-tlb-flush.txt new file +53 -0

53 lines not on this page · raw message

#16 lore raw
In reply to Nikunj A. Dadhania
Quoted 94 lines · Nikunj A. Dadhania

Remote flushing api's does a busy wait which is fine in bare-metal scenario. But with-in the guest, the vcpus might have been pre-empted or blocked. In this scenario, the initator vcpu would end up busy-waiting for a long amount of time.

This was discovered in our gang scheduling test and other way to solve this is by para-virtualizing the flush_tlb_others_ipi(now shows up as smp_call_function_many after Alex Shi's TLB optimization)

This patch set implements para-virt flush tlbs making sure that it does not wait for vcpus that are sleeping. And all the sleeping vcpus flush the tlb on guest enter. Idea was discussed here:
https://lkml.org/lkml/2012/2/20/157

This also brings one more dependency for lock-less page walk that is performed by get_user_pages_fast(gup_fast). gup_fast disables the interrupt and assumes that the pages will not be freed during that period. And this was fine as the flush_tlb_others_ipi would wait for all the IPI to be processed and return back. With the new approach of not waiting for the sleeping vcpus, this assumption is not valid anymore. So now HAVE_RCU_TABLE_FREE is used to free the pages. This will make sure that all the cpus would atleast process smp_callback before the pages are freed.

Changelog from v3:
• Add helper for cleaning up vcpu_state information (Marcelo)
• Fix code for checking vs_page and leaking page refs (Marcelo)

Changelog from v2:
• Rebase to 3.5 based linus(commit - f7da9cd) kernel.
• Port PV-Flush to new TLB-Optimization code by Alex Shi
• Use pinned pages to avoid overhead during guest enter/exit (Marcelo)
• Remove kick, as this is not improving much
• Use bit fields in the state(flush_on_enter and vcpu_running) flag to avoid smp barriers (Marcelo)

Changelog from v1:
• Race fixes reported by Vatsa
• Address gup_fast dependency using PeterZ's rcu table free patch
• Fix rcu_table_free for hw pagetable walkers

Here are the results from PLE hardware. Here is the setup details:
• 32 CPUs (HT disabled)
• 64-bit VM
• 32vcpus
• 8GB RAM

base = 3.6-rc1 + ple handler optimization patch
pvflushv4 = 3.6-rc1 + ple handler optimization patch + pvflushv4 patch

kernbench(lower is better)
==========================
base pvflushv4 %improvement
1VM 48.5800 46.8513 3.55846

2VM   108.1823      104.6410       3.27346
3VM   183.2733      163.3547      10.86825

ebizzy(higher is better)
========================

         base         pvflushv4      %improvement
1VM     2414.5000     2089.8750     -13.44481
2VM     2167.6250     2371.7500      9.41699
3VM     1600.1111     2102.5556     31.40060

Thanks Raghu for running the tests.

[1] http://article.gmane.org/gmane.linux.kernel/1329752

---

Nikunj A. Dadhania (6):
KVM Guest: Add VCPU running/pre-empted state for guest
KVM-HV: Add VCPU running/pre-empted state for guest
KVM Guest: Add paravirt kvm_flush_tlb_others
KVM-HV: Add flush_on_enter before guest enter
Enable HAVE_RCU_TABLE_FREE for kvm when PARAVIRT_TLB_FLUSH is enabled
KVM-doc: Add paravirt tlb flush document

Peter Zijlstra (2):
mm, x86: Add HAVE_RCU_TABLE_FREE support
mm: Add missing TLB invalidate to RCU page-table freeing

 Documentation/virtual/kvm/msr.txt                |    4 +
 Documentation/virtual/kvm/paravirt-tlb-flush.txt |   53 ++++++++++++++
 arch/Kconfig                                     |    3 +
 arch/powerpc/Kconfig                             |    1 
 arch/sparc/Kconfig                               |    1 
 arch/x86/Kconfig                                 |   11 +++
 arch/x86/include/asm/kvm_host.h                  |    7 ++
 arch/x86/include/asm/kvm_para.h                  |   13 +++
 arch/x86/include/asm/tlb.h                       |    1 
 arch/x86/include/asm/tlbflush.h                  |   11 +++
 arch/x86/kernel/kvm.c                            |   38 ++++++++++
Quoted 8 lines
 arch/x86/kvm/cpuid.c                             |    1 
 arch/x86/kvm/x86.c                               |   84 +++++++++++++++++++++-
 arch/x86/mm/pgtable.c                            |    6 +-
 arch/x86/mm/tlb.c                                |   36 +++++++++
 include/asm-generic/tlb.h                        |    9 ++
 mm/memory.c                                      |   43 ++++++++++-
 17 files changed, 311 insertions(+), 11 deletions(-)
 create mode 100644 Documentation/virtual/kvm/paravirt-tlb-flush.txt

Avi, PeterZ can you please review?

#17 lore raw
In reply to Nikunj A. Dadhania
Quoted 8 lines · Nikunj A. Dadhania

kernbench(lower is better)
==========================
base pvflushv4 %improvement
1VM 48.5800 46.8513 3.55846

2VM   108.1823      104.6410       3.27346
3VM   183.2733      163.3547      10.86825
Quoted 7 lines

ebizzy(higher is better)
========================

         base         pvflushv4      %improvement
1VM     2414.5000     2089.8750     -13.44481
2VM     2167.6250     2371.7500      9.41699
3VM     1600.1111     2102.5556     31.40060

The regression is worrying. We're improving the contended case at the cost of the non-contended case, this is usually the wrong thing to do. Do we have any clear idea of the cause of the regression?

Signature · 1 line
error compiling committee.c: too many arguments to function
#18 lore raw
Quoted 17 lines · Avi Kivity
Quoted 8 lines · Nikunj A. Dadhania

kernbench(lower is better)
==========================
base pvflushv4 %improvement
1VM 48.5800 46.8513 3.55846

2VM   108.1823      104.6410       3.27346
3VM   183.2733      163.3547      10.86825
Quoted 7 lines

ebizzy(higher is better)
========================

         base         pvflushv4      %improvement
1VM     2414.5000     2089.8750     -13.44481
2VM     2167.6250     2371.7500      9.41699
3VM     1600.1111     2102.5556     31.40060
Quoted 3 lines

The regression is worrying. We're improving the contended case at the cost of the non-contended case, this is usually the wrong thing to do. Do we have any clear idea of the cause of the regression?

For that matter, why do we get an improvement in the 1VM kbuild test? All vcpus should be running most of the time, barring I/O thread activity.

Signature · 1 line
error compiling committee.c: too many arguments to function
#19 lore raw
In reply to Avi Kivity
Quoted 17 lines · Avi Kivity
Quoted 8 lines · Nikunj A. Dadhania

kernbench(lower is better)
==========================
base pvflushv4 %improvement
1VM 48.5800 46.8513 3.55846

2VM   108.1823      104.6410       3.27346
3VM   183.2733      163.3547      10.86825
Quoted 7 lines

ebizzy(higher is better)
========================

         base         pvflushv4      %improvement
1VM     2414.5000     2089.8750     -13.44481
2VM     2167.6250     2371.7500      9.41699
3VM     1600.1111     2102.5556     31.40060
Quoted 4 lines

The regression is worrying. We're improving the contended case at the cost of the non-contended case, this is usually the wrong thing to do. Do we have any clear idea of the cause of the regression?

Previous perf numbers suggest that in 1VM scenario flush_tlb_others_ipi is around 2%, while for contented case its around 10%. That is what is helping contended case.

Regards,
Nikunj

#20 lore raw
Quoted 18 lines · Nikunj A Dadhania
Quoted 17 lines · Avi Kivity
Quoted 15 lines · Nikunj A. Dadhania

kernbench(lower is better)
==========================
base pvflushv4 %improvement
1VM 48.5800 46.8513 3.55846

2VM   108.1823      104.6410       3.27346
3VM   183.2733      163.3547      10.86825

ebizzy(higher is better)
========================

         base         pvflushv4      %improvement
1VM     2414.5000     2089.8750     -13.44481
2VM     2167.6250     2371.7500      9.41699
3VM     1600.1111     2102.5556     31.40060
Quoted 7 lines
Quoted 4 lines

The regression is worrying. We're improving the contended case at the cost of the non-contended case, this is usually the wrong thing to do. Do we have any clear idea of the cause of the regression?

Previous perf numbers suggest that in 1VM scenario flush_tlb_others_ipi is around 2%, while for contented case its around 10%. That is what is helping contended case.

But what is causing the regression for the uncontended case?

Signature · 1 line
error compiling committee.c: too many arguments to function
#21 lore raw
Quoted 23 lines · Avi Kivity
Quoted 22 lines · Nikunj A Dadhania
Quoted 21 lines · Avi Kivity
Quoted 8 lines · Nikunj A. Dadhania

kernbench(lower is better)
==========================
base pvflushv4 %improvement
1VM 48.5800 46.8513 3.55846

2VM   108.1823      104.6410       3.27346
3VM   183.2733      163.3547      10.86825
Quoted 7 lines

ebizzy(higher is better)
========================

         base         pvflushv4      %improvement
1VM     2414.5000     2089.8750     -13.44481
2VM     2167.6250     2371.7500      9.41699
3VM     1600.1111     2102.5556     31.40060

The regression is worrying. We're improving the contended case at the cost of the non-contended case, this is usually the wrong thing to do. Do we have any clear idea of the cause of the regression?

Quoted 6 lines
Quoted 3 lines

Previous perf numbers suggest that in 1VM scenario flush_tlb_others_ipi is around 2%, while for contented case its around 10%. That is what is helping contended case.

But what is causing the regression for the uncontended case?

Haven't been able to nail that, any clue on how to profile would help.

Regards
Nikunj

#22 lore raw
Quoted 24 lines · Nikunj A Dadhania
Quoted 23 lines · Avi Kivity
Quoted 22 lines · Nikunj A Dadhania
Quoted 21 lines · Avi Kivity
Quoted 8 lines · Nikunj A. Dadhania

kernbench(lower is better)
==========================
base pvflushv4 %improvement
1VM 48.5800 46.8513 3.55846

2VM   108.1823      104.6410       3.27346
3VM   183.2733      163.3547      10.86825
Quoted 7 lines

ebizzy(higher is better)
========================

         base         pvflushv4      %improvement
1VM     2414.5000     2089.8750     -13.44481
2VM     2167.6250     2371.7500      9.41699
3VM     1600.1111     2102.5556     31.40060

The regression is worrying. We're improving the contended case at the cost of the non-contended case, this is usually the wrong thing to do. Do we have any clear idea of the cause of the regression?

Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

Previous perf numbers suggest that in 1VM scenario flush_tlb_others_ipi is around 2%, while for contented case its around 10%. That is what is helping contended case.

But what is causing the regression for the uncontended case?

Haven't been able to nail that, any clue on how to profile would help.

perf top, perf kvm top, kvm_stat should help.

Signature · 1 line
error compiling committee.c: too many arguments to function