Skip to contentSkip to conversation

KVM/x86/hyper-V: Introduce PV guest address space mapping flush support

PATCH Applied Tianyu Lan 5 participants 15 messages Jul 19, 2018 – Jul 23, 2018

Subsystems (4)
lore
Share & tools
lore

Tianyu Lan

author
#0 lore raw

v3 cover KVM/x86/hyper-V: Introduce PV guest address space mapping flush support

Hyper-V provides a para-virtualization hypercall HvFlushGuestPhysicalAddressSpace to flush nested VM address space mapping in l1 hypervisor and it's to reduce overhead of flushing ept tlb among vcpus. The tradition way is to send IPIs to all affected vcpus and executes INVEPT on each vcpus. It will trigger several vmexits for IPI and INVEPT emulation. The pv hypercall can help to flush specified ept table on all vcpus via one single hypercall.

Change since v2:
- Make ept_pointers_match as tristate "check", "match" and "mismatch". Set "check" in vmx_set_cr3(), check all ept table pointers in hv_remote_flush_tlb() and call hypercall when all ept pointers are same.
- Rename kvm_arch_hv_flush_remote_tlb with kvm_arch_flush_remote_tlb and Rename kvm_x86_ops->hv_tlb_remote_flush with kvm_x86_ops->tlb_remote_flush
- Fix issue that ignore updating tlbs_dirty during calling kvm_arch_flush_remote_tlbs()
- Merge patch "KVM/VMX: Add identical ept table pointer check" and
patch "KVM/x86: Add tlb_remote_flush callback support for vmx"

Change since v1:
- Fix compilation error for non-x86 platform.
- Use ept_pointers_match to check condition of identical ept table pointer and get ept pointer from struct vcpu_vmx->ept_pointer.
- Add hyperv_nested_flush_guest_mapping ftrace support

Lan Tianyu (4):
X86/Hyper-V: Add flush HvFlushGuestPhysicalAddressSpace hypercall
support
X86/Hyper-V: Add hyperv_nested_flush_guest_mapping ftrace support
KVM: Add tlb remote flush callback in kvm_x86_ops. KVM/x86: Add tlb_remote_flush callback support for vmx

 arch/x86/hyperv/Makefile            |  2 +-
 arch/x86/hyperv/nested.c            | 67 ++++++++++++++++++++++++++++++++++
 arch/x86/include/asm/hyperv-tlfs.h  |  8 +++++
 arch/x86/include/asm/kvm_host.h     | 11 ++++++
 arch/x86/include/asm/mshyperv.h     |  2 ++
 arch/x86/include/asm/trace/hyperv.h | 14 ++++++++
 arch/x86/kvm/vmx.c                  | 72 ++++++++++++++++++++++++++++++++++++-
 include/linux/kvm_host.h            |  7 ++++
 virt/kvm/kvm_main.c                 |  3 +-
 9 files changed, 183 insertions(+), 3 deletions(-)
 create mode 100644 arch/x86/hyperv/nested.c
Signature · 1 line
2.14.3
author
#1 lore raw

PATCH v3 1/4 X86/Hyper-V: Add flush HvFlushGuestPhysicalAddressSpace hypercall support

Hyper-V supports a pv hypercall HvFlushGuestPhysicalAddressSpace to flush nested VM address space mapping in l1 hypervisor and it's to reduce overhead of flushing ept tlb among vcpus. This patch is to implement it.

---
 arch/x86/hyperv/Makefile           |  2 +-
 arch/x86/hyperv/nested.c           | 64 ++++++++++++++++++++++++++++++++++++++
 arch/x86/include/asm/hyperv-tlfs.h |  8 +++++
 arch/x86/include/asm/mshyperv.h    |  2 ++
 4 files changed, 75 insertions(+), 1 deletion(-)
 create mode 100644 arch/x86/hyperv/nested.c
arch/x86/hyperv/Makefile +1 -1
index b173d404e3df..b21ee65c4101 100644
@@ -1,2 +1,2 @@
-obj-y			:= hv_init.o mmu.o
+obj-y			:= hv_init.o mmu.o nested.o
 obj-$(CONFIG_X86_64)	+= hv_apic.o
arch/x86/hyperv/nested.c new file +64 -0

64 lines not on this page · raw message

arch/x86/include/asm/hyperv-tlfs.h +8 -0
index b8c89265baf0..08e24f552030 100644
@@ -309,6 +309,7 @@ struct ms_hyperv_tsc_page {
 #define HV_X64_MSR_REENLIGHTENMENT_CONTROL	0x40000106
 
 /* Nested features (CPUID 0x4000000A) EAX */
+#define HV_X64_NESTED_GUEST_MAPPING_FLUSH	BIT(18)
 #define HV_X64_NESTED_MSR_BITMAP		BIT(19)
 
 struct hv_reenlightenment_control {
@@ -350,6 +351,7 @@ struct hv_tsc_emulation_status {
 #define HVCALL_SEND_IPI_EX			0x0015
 #define HVCALL_POST_MESSAGE			0x005c
 #define HVCALL_SIGNAL_EVENT			0x005d
+#define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE 0x00af
 
 #define HV_X64_MSR_VP_ASSIST_PAGE_ENABLE	0x00000001
 #define HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT	12
@@ -741,6 +743,12 @@ struct ipi_arg_ex {
 	struct hv_vpset vp_set;
 };
 
+/* HvFlushGuestPhysicalAddressSpace hypercalls */
+struct hv_guest_mapping_flush {
+	u64 address_space;
+	u64 flags;
+};
+
 /* HvFlushVirtualAddressSpace, HvFlushVirtualAddressList hypercalls */
 struct hv_tlb_flush {
 	u64 address_space;
arch/x86/include/asm/mshyperv.h +2 -0
index 3cd14311edfa..a6a615b49876 100644
@@ -302,6 +302,7 @@ void hyperv_reenlightenment_intr(struct pt_regs *regs);
 void set_hv_tscchange_cb(void (*cb)(void));
 void clear_hv_tscchange_cb(void);
 void hyperv_stop_tsc_emulation(void);
+int hyperv_flush_guest_mapping(u64 as);
 
 #ifdef CONFIG_X86_64
 void hv_apic_init(void);
@@ -321,6 +322,7 @@ static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
 {
 	return NULL;
 }
+static inline int hyperv_flush_guest_mapping(u64 as) { return -1; }
 #endif /* CONFIG_HYPERV */
 
 #ifdef CONFIG_HYPERV_TSCPAGE
Signature · 1 line
2.14.3
#2 lore raw

Re: [PATCH V3 1/4] X86/Hyper-V: Add flush HvFlushGuestPhysicalAddressSpace hypercall support

Quoted 9 lines · Tianyu Lan
arch/x86/hyperv/nested.c new file +8 -0
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Hyper-V nested virtualization code.
+ *
+ * Copyright (C) 2018, Microsoft, Inc.
+ *
+ * Author : Lan Tianyu <[email protected]>

arch/x86/hyperv/nested.c line 18

Quoted 10 lines
arch/x86/hyperv/nested.c new file +10 -0
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
+ * NON INFRINGEMENT.  See the GNU General Public License for more
+ * details.

You already have the SPDX identifier. So the GPL boilerplate is not really required, unless your legal departement insist on it.

Thanks,

tglx

author
#3 lore raw

Re: [PATCH V3 1/4] X86/Hyper-V: Add flush HvFlushGuestPhysicalAddressSpace hypercall support

Quoted 15 lines · Thomas Gleixner
Quoted 14 lines · Tianyu Lan
arch/x86/hyperv/nested.c new file +13 -0
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Hyper-V nested virtualization code.
+ *
+ * Copyright (C) 2018, Microsoft, Inc.
+ *
+ * Author : Lan Tianyu <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published
+ * by the Free Software Foundation.
+ *
Quoted 9 lines

arch/x86/hyperv/nested.c line 18

Quoted 5 lines
arch/x86/hyperv/nested.c new file +5 -0
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
+ * NON INFRINGEMENT.  See the GNU General Public License for more
+ * details.

You already have the SPDX identifier. So the GPL boilerplate is not really required, unless your legal departement insist on it.

Hi Thomas:
Thanks for your reminder. How about the following?

+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Hyper-V nested virtualization code.
+ *
+ * Copyright (C) 2018, Microsoft, Inc.
+ *
+ * Author : Lan Tianyu <[email protected]>
+ */

Quoted 4 lines

Thanks,

tglx

#4 lore raw

Re: [PATCH V3 1/4] X86/Hyper-V: Add flush HvFlushGuestPhysicalAddressSpace hypercall support

Quoted 9 lines · Tianyu Lan
Quoted 3 lines · Thomas Gleixner

You already have the SPDX identifier. So the GPL boilerplate is not really required, unless your legal departement insist on it.

Hi Thomas:
Thanks for your reminder. How about the following?

arch/x86/hyperv/nested.c line 20

Quoted 9 lines
arch/x86/hyperv/nested.c new file +9 -0
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Hyper-V nested virtualization code.
+ *
+ * Copyright (C) 2018, Microsoft, Inc.
+ *
+ * Author : Lan Tianyu <[email protected]>
+ */

Perfect :)

author
#5 lore raw

PATCH v3 1/4 X86/Hyper-V: Add flush HvFlushGuestPhysicalAddressSpace hypercall support

In reply to Tianyu Lan

Hyper-V supports a pv hypercall HvFlushGuestPhysicalAddressSpace to flush nested VM address space mapping in l1 hypervisor and it's to reduce overhead of flushing ept tlb among vcpus. This patch is to implement it.

---
Change since v3
Remove GPL boilerplate.

 arch/x86/hyperv/Makefile           |  2 +-
 arch/x86/hyperv/nested.c           | 53 ++++++++++++++++++++++++++++++++++++++
 arch/x86/include/asm/hyperv-tlfs.h |  8 ++++++
 arch/x86/include/asm/mshyperv.h    |  2 ++
 4 files changed, 64 insertions(+), 1 deletion(-)
 create mode 100644 arch/x86/hyperv/nested.c
arch/x86/hyperv/Makefile +1 -1
index b173d404e3df..b21ee65c4101 100644
@@ -1,2 +1,2 @@
-obj-y			:= hv_init.o mmu.o
+obj-y			:= hv_init.o mmu.o nested.o
 obj-$(CONFIG_X86_64)	+= hv_apic.o
arch/x86/hyperv/nested.c new file +53 -0

53 lines not on this page · raw message

arch/x86/include/asm/hyperv-tlfs.h +8 -0
index b8c89265baf0..08e24f552030 100644
@@ -309,6 +309,7 @@ struct ms_hyperv_tsc_page {
 #define HV_X64_MSR_REENLIGHTENMENT_CONTROL	0x40000106
 
 /* Nested features (CPUID 0x4000000A) EAX */
+#define HV_X64_NESTED_GUEST_MAPPING_FLUSH	BIT(18)
 #define HV_X64_NESTED_MSR_BITMAP		BIT(19)
 
 struct hv_reenlightenment_control {
@@ -350,6 +351,7 @@ struct hv_tsc_emulation_status {
 #define HVCALL_SEND_IPI_EX			0x0015
 #define HVCALL_POST_MESSAGE			0x005c
 #define HVCALL_SIGNAL_EVENT			0x005d
+#define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE 0x00af
 
 #define HV_X64_MSR_VP_ASSIST_PAGE_ENABLE	0x00000001
 #define HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT	12
@@ -741,6 +743,12 @@ struct ipi_arg_ex {
 	struct hv_vpset vp_set;
 };
 
+/* HvFlushGuestPhysicalAddressSpace hypercalls */
+struct hv_guest_mapping_flush {
+	u64 address_space;
+	u64 flags;
+};
+
 /* HvFlushVirtualAddressSpace, HvFlushVirtualAddressList hypercalls */
 struct hv_tlb_flush {
 	u64 address_space;
arch/x86/include/asm/mshyperv.h +2 -0
index 3cd14311edfa..a6a615b49876 100644
@@ -302,6 +302,7 @@ void hyperv_reenlightenment_intr(struct pt_regs *regs);
 void set_hv_tscchange_cb(void (*cb)(void));
 void clear_hv_tscchange_cb(void);
 void hyperv_stop_tsc_emulation(void);
+int hyperv_flush_guest_mapping(u64 as);
 
 #ifdef CONFIG_X86_64
 void hv_apic_init(void);
@@ -321,6 +322,7 @@ static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
 {
 	return NULL;
 }
+static inline int hyperv_flush_guest_mapping(u64 as) { return -1; }
 #endif /* CONFIG_HYPERV */
 
 #ifdef CONFIG_HYPERV_TSCPAGE
Signature · 1 line
2.14.3
author
#6 lore raw

PATCH v3 2/4 X86/Hyper-V: Add hyperv_nested_flush_guest_mapping ftrace support

In reply to Tianyu Lan

This patch is to add hyperv_nested_flush_guest_mapping support to trace hvFlushGuestPhysicalAddressSpace hypercall.

---
 arch/x86/hyperv/nested.c            |  3 +++
 arch/x86/include/asm/trace/hyperv.h | 14 ++++++++++++++
 2 files changed, 17 insertions(+)
arch/x86/hyperv/nested.c +3 -0
index 74dd38b5221d..42a3232f2835 100644
@@ -25,6 +25,8 @@
 #include <asm/mshyperv.h>
 #include <asm/tlbflush.h>
 
+#include <asm/trace/hyperv.h>
+
 int hyperv_flush_guest_mapping(u64 as)
 {
 	struct hv_guest_mapping_flush **flush_pcpu;
@@ -59,6 +61,7 @@ int hyperv_flush_guest_mapping(u64 as)
 		ret = 0;
 
 fault:
+	trace_hyperv_nested_flush_guest_mapping(as, ret);
 	return ret;
 }
 EXPORT_SYMBOL_GPL(hyperv_flush_guest_mapping);
arch/x86/include/asm/trace/hyperv.h +14 -0
index 4253bca99989..e1ffe61de8d6 100644
@@ -28,6 +28,20 @@ TRACE_EVENT(hyperv_mmu_flush_tlb_others,
 		      __entry->addr, __entry->end)
 	);
 
+TRACE_EVENT(hyperv_nested_flush_guest_mapping,
+	    TP_PROTO(u64 as, int ret),
+	    TP_ARGS(as, ret),
+
+	    TP_STRUCT__entry(
+		    __field(u64, as)
+		    __field(int, ret)
+		    ),
+	    TP_fast_assign(__entry->as = as;
+			   __entry->ret = ret;
+		    ),
+	    TP_printk("address space %llx ret %d", __entry->as, __entry->ret)
+	);
+
 #endif /* CONFIG_HYPERV */
 
 #undef TRACE_INCLUDE_PATH
Signature · 1 line
2.14.3
author
#7 lore raw

PATCH v3 3/4 KVM: Add tlb remote flush callback in kvm_x86_ops.

In reply to Tianyu Lan

This patch is to provide a way for platforms to register hv tlb remote flush callback and this helps to optimize operation of tlb flush among vcpus for nested virtualization case.

---
Change since v2:
Rename kvm_arch_hv_flush_remote_tlb with kvm_arch_flush_remote_tlb Rename kvm_x86_ops->hv_tlb_remote_flush with kvm_x86_ops->tlb_remote_flush Fix issue that ignore updating tlbs_dirty during calling kvm_arch_flush_remote_tlbs()
Change since v1:
Add kvm_arch_hv_flush_remote_tlb() to avoid compilation issue
for non-x86 platform.

---
 arch/x86/include/asm/kvm_host.h | 11 +++++++++++
 include/linux/kvm_host.h        |  7 +++++++
 virt/kvm/kvm_main.c             |  3 ++-
 3 files changed, 20 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +11 -0
index c13cd28d9d1b..76e806bc4ef8 100644
@@ -973,6 +973,7 @@ struct kvm_x86_ops {
 	void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
 
 	void (*tlb_flush)(struct kvm_vcpu *vcpu, bool invalidate_gpa);
+	int  (*tlb_remote_flush)(struct kvm *kvm);
 
 	void (*run)(struct kvm_vcpu *vcpu);
 	int (*handle_exit)(struct kvm_vcpu *vcpu);
@@ -1117,6 +1118,16 @@ static inline void kvm_arch_free_vm(struct kvm *kvm)
 	return kvm_x86_ops->vm_free(kvm);
 }
 
+#define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLB
+static inline int kvm_arch_flush_remote_tlb(struct kvm *kvm)
+{
+	if (kvm_x86_ops->tlb_remote_flush &&
+	    !kvm_x86_ops->tlb_remote_flush(kvm))
+		return 0;
+	else
+		return -EFAULT;
+}
+
 int kvm_mmu_module_init(void);
 void kvm_mmu_module_exit(void);
 
include/linux/kvm_host.h +7 -0
index 4ee7bc548a83..0781d94200df 100644
@@ -827,6 +827,13 @@ static inline void kvm_arch_free_vm(struct kvm *kvm)
 }
 #endif
 
+#ifndef __KVM_HAVE_ARCH_FLUSH_REMOTE_TLB
+static inline int kvm_arch_flush_remote_tlb(struct kvm *kvm)
+{
+	return -EFAULT;
+}
+#endif
+
 #ifdef __KVM_HAVE_ARCH_NONCOHERENT_DMA
 void kvm_arch_register_noncoherent_dma(struct kvm *kvm);
 void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm);
virt/kvm/kvm_main.c +2 -1
index 8b47507faab5..1a13cdb2822b 100644
@@ -273,7 +273,8 @@ void kvm_flush_remote_tlbs(struct kvm *kvm)
 	 * kvm_make_all_cpus_request() reads vcpu->mode. We reuse that
 	 * barrier here.
 	 */
-	if (kvm_make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
+	if (!kvm_arch_flush_remote_tlb(kvm)
+	    || kvm_make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
 		++kvm->stat.remote_tlb_flush;
 	cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
 }
Signature · 1 line
2.14.3
maintainer
#8 lore raw
In reply to Tianyu Lan
Quoted 6 lines · Tianyu Lan

Hyper-V provides a para-virtualization hypercall HvFlushGuestPhysicalAddressSpace to flush nested VM address space mapping in l1 hypervisor and it's to reduce overhead of flushing ept tlb among vcpus. The tradition way is to send IPIs to all affected vcpus and executes INVEPT on each vcpus. It will trigger several vmexits for IPI and INVEPT emulation. The pv hypercall can help to flush specified ept table on all vcpus via one single hypercall.

Thanks, this looks good apart from a global replace of EFAULT with ENOTSUP (which can be done when applying). Can I have an explicit ack for patches 1 and 2 from the Hyper-V people?

Thanks,

Paolo

Quoted 37 lines

Change since v2:
- Make ept_pointers_match as tristate "check", "match" and "mismatch". Set "check" in vmx_set_cr3(), check all ept table pointers in hv_remote_flush_tlb() and call hypercall when all ept pointers are same.
- Rename kvm_arch_hv_flush_remote_tlb with kvm_arch_flush_remote_tlb and Rename kvm_x86_ops->hv_tlb_remote_flush with kvm_x86_ops->tlb_remote_flush
- Fix issue that ignore updating tlbs_dirty during calling kvm_arch_flush_remote_tlbs()
- Merge patch "KVM/VMX: Add identical ept table pointer check" and
patch "KVM/x86: Add tlb_remote_flush callback support for vmx"

Change since v1:
- Fix compilation error for non-x86 platform.
- Use ept_pointers_match to check condition of identical ept table pointer and get ept pointer from struct vcpu_vmx->ept_pointer.
- Add hyperv_nested_flush_guest_mapping ftrace support

Lan Tianyu (4):
X86/Hyper-V: Add flush HvFlushGuestPhysicalAddressSpace hypercall
support
X86/Hyper-V: Add hyperv_nested_flush_guest_mapping ftrace support
KVM: Add tlb remote flush callback in kvm_x86_ops. KVM/x86: Add tlb_remote_flush callback support for vmx

 arch/x86/hyperv/Makefile            |  2 +-
 arch/x86/hyperv/nested.c            | 67 ++++++++++++++++++++++++++++++++++
 arch/x86/include/asm/hyperv-tlfs.h  |  8 +++++
 arch/x86/include/asm/kvm_host.h     | 11 ++++++
 arch/x86/include/asm/mshyperv.h     |  2 ++
 arch/x86/include/asm/trace/hyperv.h | 14 ++++++++
 arch/x86/kvm/vmx.c                  | 72 ++++++++++++++++++++++++++++++++++++-
 include/linux/kvm_host.h            |  7 ++++
 virt/kvm/kvm_main.c                 |  3 +-
 9 files changed, 183 insertions(+), 3 deletions(-)
 create mode 100644 arch/x86/hyperv/nested.c
author
#9 lore raw
Quoted 11 lines · Paolo Bonzini
Quoted 6 lines · Tianyu Lan

Hyper-V provides a para-virtualization hypercall HvFlushGuestPhysicalAddressSpace to flush nested VM address space mapping in l1 hypervisor and it's to reduce overhead of flushing ept tlb among vcpus. The tradition way is to send IPIs to all affected vcpus and executes INVEPT on each vcpus. It will trigger several vmexits for IPI and INVEPT emulation. The pv hypercall can help to flush specified ept table on all vcpus via one single hypercall.

Thanks, this looks good apart from a global replace of EFAULT with ENOTSUP (which can be done when applying). Can I have an explicit ack for patches 1 and 2 from the Hyper-V people?

Thanks, Paolo. I will try to hyper-V people's ack.

Quoted 43 lines

Thanks,

Paolo

Quoted 37 lines

Change since v2:
- Make ept_pointers_match as tristate "check", "match" and "mismatch". Set "check" in vmx_set_cr3(), check all ept table pointers in hv_remote_flush_tlb() and call hypercall when all ept pointers are same.
- Rename kvm_arch_hv_flush_remote_tlb with kvm_arch_flush_remote_tlb and Rename kvm_x86_ops->hv_tlb_remote_flush with kvm_x86_ops->tlb_remote_flush
- Fix issue that ignore updating tlbs_dirty during calling kvm_arch_flush_remote_tlbs()
- Merge patch "KVM/VMX: Add identical ept table pointer check" and
patch "KVM/x86: Add tlb_remote_flush callback support for vmx"

Change since v1:
- Fix compilation error for non-x86 platform.
- Use ept_pointers_match to check condition of identical ept table pointer and get ept pointer from struct vcpu_vmx->ept_pointer.
- Add hyperv_nested_flush_guest_mapping ftrace support

Lan Tianyu (4):
X86/Hyper-V: Add flush HvFlushGuestPhysicalAddressSpace hypercall
support
X86/Hyper-V: Add hyperv_nested_flush_guest_mapping ftrace support
KVM: Add tlb remote flush callback in kvm_x86_ops. KVM/x86: Add tlb_remote_flush callback support for vmx

  arch/x86/hyperv/Makefile            |  2 +-
  arch/x86/hyperv/nested.c            | 67 ++++++++++++++++++++++++++++++++++
  arch/x86/include/asm/hyperv-tlfs.h  |  8 +++++
  arch/x86/include/asm/kvm_host.h     | 11 ++++++
  arch/x86/include/asm/mshyperv.h     |  2 ++
  arch/x86/include/asm/trace/hyperv.h | 14 ++++++++
  arch/x86/kvm/vmx.c                  | 72 ++++++++++++++++++++++++++++++++++++-
  include/linux/kvm_host.h            |  7 ++++
  virt/kvm/kvm_main.c                 |  3 +-
  9 files changed, 183 insertions(+), 3 deletions(-)

create mode 100644 arch/x86/hyperv/nested.c

author
#10 lore raw

PATCH v3 4/4 KVM/x86: Add tlb_remote_flush callback support for vmx

In reply to Tianyu Lan

Register tlb_remote_flush callback for vmx when hyperv capability of nested guest mapping flush is detected. The interface can help to reduce overhead when flush ept table among vcpus for nested VM. The tradition way is to send IPIs to all affected vcpus and executes INVEPT on each vcpus. It will trigger several vmexits for IPI and INVEPT emulation. Hyper-V provides such hypercall to do flush for all vcpus and call the hypercall when all ept table pointers of single VM are same.

---
Change since v2:
Make ept_pointers_match as tristate "check", "match" and
"mismatch". Set "check" in vmx_set_cr3(), check all ept table pointers in hv_remote_flush_tlb() and call hypercall when all ept pointers are same.

Change since v1:
Replace identical_ept_pointer with ept_pointers_match and
check kvm_x86_ops->tlb_remote_flush in check_ept_pointer().

---
 arch/x86/kvm/vmx.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 71 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx.c +71 -1
index 1689f433f3a0..601ee37937a9 100644
@@ -188,12 +188,21 @@ module_param(ple_window_max, uint, 0444);
 
 extern const ulong vmx_return;
 
+enum ept_pointers_status {
+	EPT_POINTERS_CHECK = 0,
+	EPT_POINTERS_MATCH = 1,
+	EPT_POINTERS_MISMATCH = 2
+};
+
 struct kvm_vmx {
 	struct kvm kvm;
 
 	unsigned int tss_addr;
 	bool ept_identity_pagetable_done;
 	gpa_t ept_identity_map_addr;
+
+	enum ept_pointers_status ept_pointers_match;
+	spinlock_t ept_pointer_lock;
 };
 
 #define NR_AUTOLOAD_MSRS 8
@@ -853,6 +862,7 @@ struct vcpu_vmx {
 	 */
 	u64 msr_ia32_feature_control;
 	u64 msr_ia32_feature_control_valid_bits;
+	u64 ept_pointer;
 };
 
 enum segment_cache_field {
@@ -4774,6 +4784,48 @@ static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
 	}
 }
 
+/* check_ept_pointer() should be under protection of ept_pointer_lock. */
+static void check_ept_pointer(struct kvm *kvm)
+{
+	struct kvm_vcpu *vcpu;
+	u64 tmp_eptp = INVALID_PAGE;
+	int i;
+
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		if (!VALID_PAGE(tmp_eptp)) {
+			tmp_eptp = to_vmx(vcpu)->ept_pointer;
+		} else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
+			to_kvm_vmx(kvm)->ept_pointers_match
+				= EPT_POINTERS_MISMATCH;
+			return;
+		}
+	}
+
+	to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
+}
+
+static int hv_remote_flush_tlb(struct kvm *kvm)
+{
+	int ret;
+
+	spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
+
+	if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
+		check_ept_pointer(kvm);
+
+	if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
+		ret = -EFAULT;
+		goto out;
+	}
+
+	ret = hyperv_flush_guest_mapping(
+			to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer);
+
+out:
+	spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
+	return ret;
+}
+
 static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
 {
 	__vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
@@ -4960,6 +5012,7 @@ static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
 
 static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
 {
+	struct kvm *kvm = vcpu->kvm;
 	unsigned long guest_cr3;
 	u64 eptp;
 
@@ -4967,11 +5020,20 @@ static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
 	if (enable_ept) {
 		eptp = construct_eptp(vcpu, cr3);
 		vmcs_write64(EPT_POINTER, eptp);
+
+		if (kvm_x86_ops->tlb_remote_flush) {
+			spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
+			to_vmx(vcpu)->ept_pointer = eptp;
+			to_kvm_vmx(kvm)->ept_pointers_match
+				= EPT_POINTERS_CHECK;
+			spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
+		}
+
 		if (enable_unrestricted_guest || is_paging(vcpu) ||
 		    is_guest_mode(vcpu))
 			guest_cr3 = kvm_read_cr3(vcpu);
 		else
-			guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
+			guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
 		ept_load_pdptrs(vcpu);
 	}
 
@@ -7538,6 +7600,12 @@ static __init int hardware_setup(void)
 	if (enable_ept && !cpu_has_vmx_ept_2m_page())
 		kvm_disable_largepages();
 
+#if IS_ENABLED(CONFIG_HYPERV)
+	if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
+	    && enable_ept)
+		kvm_x86_ops->tlb_remote_flush = hv_remote_flush_tlb;
+#endif
+
 	if (!cpu_has_vmx_ple()) {
 		ple_gap = 0;
 		ple_window = 0;
@@ -10383,6 +10451,8 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
 
 static int vmx_vm_init(struct kvm *kvm)
 {
+	spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
+
 	if (!ple_gap)
 		kvm->arch.pause_in_guest = true;
 	return 0;
Signature · 1 line
2.14.3
Acked-by on the v3 series
#11 lore raw
In reply to Tianyu Lan
Quoted 56 lines

-----Original Message-----
From: Tianyu Lan
Sent: Thursday, July 19, 2018 1:40 AM
Cc: Tianyu Lan <[email protected]>; [email protected]; Haiyang Zhang <[email protected]>; [email protected]; [email protected]; KY Srinivasan <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; Stephen Hemminger <[email protected]>; [email protected]; [email protected]; Michael Kelley (EOSG) <[email protected]>; [email protected]
Subject: [PATCH V3 0/4] KVM/x86/hyper-V: Introduce PV guest address space mapping flush support

Hyper-V provides a para-virtualization hypercall HvFlushGuestPhysicalAddressSpace
to flush nested VM address space mapping in l1 hypervisor and it's to reduce overhead
of flushing ept tlb among vcpus. The tradition way is to send IPIs to all affected
vcpus and executes INVEPT on each vcpus. It will trigger several vmexits for IPI and
INVEPT emulation. The pv hypercall can help to flush specified ept table on all vcpus
via one single hypercall.

Change since v2:
- Make ept_pointers_match as tristate "check", "match" and "mismatch". Set "check" in vmx_set_cr3(), check all ept table pointers in
hv_remote_flush_tlb()
and call hypercall when all ept pointers are same.
- Rename kvm_arch_hv_flush_remote_tlb with
kvm_arch_flush_remote_tlb and

       Rename kvm_x86_ops->hv_tlb_remote_flush with kvm_x86_ops-
Quoted 1 line

tlb_remote_flush

- Fix issue that ignore updating tlbs_dirty during calling
kvm_arch_flush_remote_tlbs()
- Merge patch "KVM/VMX: Add identical ept table pointer check" and patch "KVM/x86: Add tlb_remote_flush callback support for vmx"

Change since v1:
- Fix compilation error for non-x86 platform.
- Use ept_pointers_match to check condition of identical ept table pointer and get ept pointer from struct vcpu_vmx->ept_pointer.
- Add hyperv_nested_flush_guest_mapping ftrace support

Lan Tianyu (4):
X86/Hyper-V: Add flush HvFlushGuestPhysicalAddressSpace hypercall
support
X86/Hyper-V: Add hyperv_nested_flush_guest_mapping ftrace support
KVM: Add tlb remote flush callback in kvm_x86_ops. KVM/x86: Add tlb_remote_flush callback support for vmx

Quoted 13 lines
 arch/x86/hyperv/Makefile            |  2 +-
 arch/x86/hyperv/nested.c            | 67
++++++++++++++++++++++++++++++++++
 arch/x86/include/asm/hyperv-tlfs.h  |  8 +++++
 arch/x86/include/asm/kvm_host.h     | 11 ++++++
 arch/x86/include/asm/mshyperv.h     |  2 ++
 arch/x86/include/asm/trace/hyperv.h | 14 ++++++++
 arch/x86/kvm/vmx.c                  | 72
++++++++++++++++++++++++++++++++++++-
 include/linux/kvm_host.h            |  7 ++++
 virt/kvm/kvm_main.c                 |  3 +-
 9 files changed, 183 insertions(+), 3 deletions(-)
 create mode 100644 arch/x86/hyperv/nested.c
Quoted 3 lines

--
2.14.3

maintainer Applied
#12 lore raw
Quoted 70 lines · KY Srinivasan
Quoted 56 lines

-----Original Message-----
From: Tianyu Lan
Sent: Thursday, July 19, 2018 1:40 AM
Cc: Tianyu Lan <[email protected]>; [email protected]; Haiyang Zhang <[email protected]>; [email protected]; [email protected]; KY Srinivasan <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; Stephen Hemminger <[email protected]>; [email protected]; [email protected]; Michael Kelley (EOSG) <[email protected]>; [email protected]
Subject: [PATCH V3 0/4] KVM/x86/hyper-V: Introduce PV guest address space mapping flush support

Hyper-V provides a para-virtualization hypercall HvFlushGuestPhysicalAddressSpace
to flush nested VM address space mapping in l1 hypervisor and it's to reduce overhead
of flushing ept tlb among vcpus. The tradition way is to send IPIs to all affected
vcpus and executes INVEPT on each vcpus. It will trigger several vmexits for IPI and
INVEPT emulation. The pv hypercall can help to flush specified ept table on all vcpus
via one single hypercall.

Change since v2:
- Make ept_pointers_match as tristate "check", "match" and "mismatch". Set "check" in vmx_set_cr3(), check all ept table pointers in
hv_remote_flush_tlb()
and call hypercall when all ept pointers are same.
- Rename kvm_arch_hv_flush_remote_tlb with
kvm_arch_flush_remote_tlb and

       Rename kvm_x86_ops->hv_tlb_remote_flush with kvm_x86_ops-
Quoted 1 line

tlb_remote_flush

- Fix issue that ignore updating tlbs_dirty during calling
kvm_arch_flush_remote_tlbs()
- Merge patch "KVM/VMX: Add identical ept table pointer check" and patch "KVM/x86: Add tlb_remote_flush callback support for vmx"

Change since v1:
- Fix compilation error for non-x86 platform.
- Use ept_pointers_match to check condition of identical ept table pointer and get ept pointer from struct vcpu_vmx->ept_pointer.
- Add hyperv_nested_flush_guest_mapping ftrace support

Lan Tianyu (4):
X86/Hyper-V: Add flush HvFlushGuestPhysicalAddressSpace hypercall
support
X86/Hyper-V: Add hyperv_nested_flush_guest_mapping ftrace support
KVM: Add tlb remote flush callback in kvm_x86_ops. KVM/x86: Add tlb_remote_flush callback support for vmx

Quoted 13 lines
 arch/x86/hyperv/Makefile            |  2 +-
 arch/x86/hyperv/nested.c            | 67
++++++++++++++++++++++++++++++++++
 arch/x86/include/asm/hyperv-tlfs.h  |  8 +++++
 arch/x86/include/asm/kvm_host.h     | 11 ++++++
 arch/x86/include/asm/mshyperv.h     |  2 ++
 arch/x86/include/asm/trace/hyperv.h | 14 ++++++++
 arch/x86/kvm/vmx.c                  | 72
++++++++++++++++++++++++++++++++++++-
 include/linux/kvm_host.h            |  7 ++++
 virt/kvm/kvm_main.c                 |  3 +-
 9 files changed, 183 insertions(+), 3 deletions(-)
 create mode 100644 arch/x86/hyperv/nested.c
Quoted 1 line

Queued, thanks!

Paolo

#13 lore raw
Quoted 72 lines · Paolo Bonzini
Quoted 70 lines · KY Srinivasan
Quoted 69 lines

-----Original Message-----
From: Tianyu Lan
Sent: Thursday, July 19, 2018 1:40 AM
Cc: Tianyu Lan <[email protected]>; [email protected]; Haiyang Zhang <[email protected]>; [email protected]; [email protected]; KY Srinivasan <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; Stephen Hemminger <[email protected]>; [email protected]; [email protected]; Michael Kelley (EOSG) <[email protected]>; [email protected]
Subject: [PATCH V3 0/4] KVM/x86/hyper-V: Introduce PV guest address space mapping flush support

Hyper-V provides a para-virtualization hypercall HvFlushGuestPhysicalAddressSpace
to flush nested VM address space mapping in l1 hypervisor and it's to reduce overhead
of flushing ept tlb among vcpus. The tradition way is to send IPIs to all affected
vcpus and executes INVEPT on each vcpus. It will trigger several vmexits for IPI and
INVEPT emulation. The pv hypercall can help to flush specified ept table on all vcpus
via one single hypercall.

Change since v2:
- Make ept_pointers_match as tristate "check", "match" and "mismatch". Set "check" in vmx_set_cr3(), check all ept table pointers in
hv_remote_flush_tlb()
and call hypercall when all ept pointers are same.
- Rename kvm_arch_hv_flush_remote_tlb with
kvm_arch_flush_remote_tlb and

       Rename kvm_x86_ops->hv_tlb_remote_flush with kvm_x86_ops-
Quoted 1 line

tlb_remote_flush

- Fix issue that ignore updating tlbs_dirty during calling
kvm_arch_flush_remote_tlbs()
- Merge patch "KVM/VMX: Add identical ept table pointer check" and patch "KVM/x86: Add tlb_remote_flush callback support for vmx"

Change since v1:
- Fix compilation error for non-x86 platform.
- Use ept_pointers_match to check condition of identical ept table pointer and get ept pointer from struct vcpu_vmx->ept_pointer.
- Add hyperv_nested_flush_guest_mapping ftrace support

Lan Tianyu (4):
X86/Hyper-V: Add flush HvFlushGuestPhysicalAddressSpace hypercall
support
X86/Hyper-V: Add hyperv_nested_flush_guest_mapping ftrace support
KVM: Add tlb remote flush callback in kvm_x86_ops. KVM/x86: Add tlb_remote_flush callback support for vmx

 arch/x86/hyperv/Makefile            |  2 +-
 arch/x86/hyperv/nested.c            | 67
++++++++++++++++++++++++++++++++++
 arch/x86/include/asm/hyperv-tlfs.h  |  8 +++++
 arch/x86/include/asm/kvm_host.h     | 11 ++++++
 arch/x86/include/asm/mshyperv.h     |  2 ++
 arch/x86/include/asm/trace/hyperv.h | 14 ++++++++
 arch/x86/kvm/vmx.c                  | 72
++++++++++++++++++++++++++++++++++++-
 include/linux/kvm_host.h            |  7 ++++
 virt/kvm/kvm_main.c                 |  3 +-
 9 files changed, 183 insertions(+), 3 deletions(-)
 create mode 100644 arch/x86/hyperv/nested.c
Quoted 3 lines
Quoted 1 line

Queued, thanks!

My CONFIG_HYPERV is disabled, there is a warning when compiling kvm/queue.
warning: ‘hv_remote_flush_tlb’ defined but not used [-Wunused-function]
static int hv_remote_flush_tlb(struct kvm *kvm)

Regards,
Wanpeng Li
_______________________________________________
devel mailing list
[email protected]
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

author
#14 lore raw
Quoted 73 lines · Wanpeng Li
Quoted 72 lines · Paolo Bonzini
Quoted 70 lines · KY Srinivasan
Quoted 69 lines

-----Original Message-----
From: Tianyu Lan
Sent: Thursday, July 19, 2018 1:40 AM
Cc: Tianyu Lan <[email protected]>; [email protected]; Haiyang Zhang <[email protected]>; [email protected]; [email protected]; KY Srinivasan <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; Stephen Hemminger <[email protected]>; [email protected]; [email protected]; Michael Kelley (EOSG) <[email protected]>; [email protected]
Subject: [PATCH V3 0/4] KVM/x86/hyper-V: Introduce PV guest address space mapping flush support

Hyper-V provides a para-virtualization hypercall HvFlushGuestPhysicalAddressSpace
to flush nested VM address space mapping in l1 hypervisor and it's to reduce overhead
of flushing ept tlb among vcpus. The tradition way is to send IPIs to all affected
vcpus and executes INVEPT on each vcpus. It will trigger several vmexits for IPI and
INVEPT emulation. The pv hypercall can help to flush specified ept table on all vcpus
via one single hypercall.

Change since v2:
- Make ept_pointers_match as tristate "check", "match" and "mismatch". Set "check" in vmx_set_cr3(), check all ept table pointers in
hv_remote_flush_tlb()
and call hypercall when all ept pointers are same.
- Rename kvm_arch_hv_flush_remote_tlb with
kvm_arch_flush_remote_tlb and

        Rename kvm_x86_ops->hv_tlb_remote_flush with kvm_x86_ops-
Quoted 1 line

tlb_remote_flush

- Fix issue that ignore updating tlbs_dirty during calling
kvm_arch_flush_remote_tlbs()
- Merge patch "KVM/VMX: Add identical ept table pointer check" and patch "KVM/x86: Add tlb_remote_flush callback support for vmx"

Change since v1:
- Fix compilation error for non-x86 platform.
- Use ept_pointers_match to check condition of identical ept table pointer and get ept pointer from struct vcpu_vmx->ept_pointer.
- Add hyperv_nested_flush_guest_mapping ftrace support

Lan Tianyu (4):
X86/Hyper-V: Add flush HvFlushGuestPhysicalAddressSpace hypercall
support
X86/Hyper-V: Add hyperv_nested_flush_guest_mapping ftrace support
KVM: Add tlb remote flush callback in kvm_x86_ops. KVM/x86: Add tlb_remote_flush callback support for vmx

  arch/x86/hyperv/Makefile            |  2 +-
  arch/x86/hyperv/nested.c            | 67
++++++++++++++++++++++++++++++++++
  arch/x86/include/asm/hyperv-tlfs.h  |  8 +++++
  arch/x86/include/asm/kvm_host.h     | 11 ++++++
  arch/x86/include/asm/mshyperv.h     |  2 ++
  arch/x86/include/asm/trace/hyperv.h | 14 ++++++++
  arch/x86/kvm/vmx.c                  | 72
++++++++++++++++++++++++++++++++++++-
  include/linux/kvm_host.h            |  7 ++++
  virt/kvm/kvm_main.c                 |  3 +-
  9 files changed, 183 insertions(+), 3 deletions(-)

create mode 100644 arch/x86/hyperv/nested.c

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Queued, thanks!

My CONFIG_HYPERV is disabled, there is a warning when compiling kvm/queue.
warning: ‘hv_remote_flush_tlb’ defined but not used [-Wunused-function]
static int hv_remote_flush_tlb(struct kvm *kvm)

Thanks. Wanpeng! Just send a fix patch.

Quoted 4 lines

Regards,
Wanpeng Li