Enlightened VMCS support for KVM on Hyper-V
Subsystems (6)
Vitaly Kuznetsov
maintainerv4 cover Enlightened VMCS support for KVM on Hyper-V
Changes since v3:
- Add Michael's Reviewed-by to PATCH2,5,6
- PATCH1:
- Decipher 'TLFS' and add a reference where it can be obtained [Christoph Hellwig]
- PATCH4:
- remove vfree() from hv_cpu_die() [Thomas Gleixner]
- free hv_vp_assist_page() in the error path [Thomas Gleixner] While on it, add cpuhp_remove_state() call when we fail to allocate hypercall page to make the error path complete.
- check hv_vp_assist_page != NULL in hv_get_vp_assist_page() [Thomas Gleixner]
- PATCH7:
- split off vmx_evmcs.h [Paolo Bonzini]
- merge get_evmcs_offset() and get_evmcs_offset_cf() [Paolo Bonzini]
- move evmcs_sanitize_exec_ctrls() to setup_vmcs_config [Paolo Bonzini]
- rename vmcs_load_enlightened() -> evmcs_load() [Paolo Bonzini]
- drop evmcs_sanitize_exec_ctrls() from dump_vmcs() [Paolo Bonzini]
When running nested KVM on Hyper-V it's possible to use so called 'Enlightened VMCS' and do normal memory reads/writes instead of doing VMWRITE/VMREAD instructions. In addition, clean field mask provides a huge room for optimization on L0's side.
Tight CPUID loop test shows significant speedup (E5-2667 v4 @ 3.20GHz):
Before: 18890 cycles
After: 8304 cycles
Ladi Prosek (1):
x86/kvm: rename HV_X64_MSR_APIC_ASSIST_PAGE to
HV_X64_MSR_VP_ASSIST_PAGE
Vitaly Kuznetsov (6):
x86/hyper-v: move hyperv.h out of uapi
x86/hyper-v: move definitions from TLFS to hyperv-tlfs.h x86/hyper-v: allocate and use Virtual Processor Assist Pages x86/hyper-v: define struct hv_enlightened_vmcs and clean field bits x86/hyper-v: detect nested features
x86/kvm: use Enlightened VMCS when running on Hyper-V
MAINTAINERS | 2 +- arch/x86/hyperv/hv_init.c | 45 ++- arch/x86/include/asm/hyperv-tlfs.h | 702 +++++++++++++++++++++++++++++++++++ arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/asm/mshyperv.h | 94 +---- arch/x86/include/uapi/asm/hyperv.h | 421 --------------------- arch/x86/include/uapi/asm/kvm_para.h | 1 - arch/x86/kernel/cpu/mshyperv.c | 21 +- arch/x86/kvm/hyperv.c | 8 +- arch/x86/kvm/lapic.h | 2 +- arch/x86/kvm/vmx.c | 301 ++++++++++++++- arch/x86/kvm/vmx_evmcs.h | 324 ++++++++++++++++ arch/x86/kvm/x86.c | 2 +- drivers/hv/connection.c | 1 - drivers/hv/hv.c | 1 - drivers/hv/hyperv_vmbus.h | 1 + drivers/hv/vmbus_drv.c | 1 - include/linux/hyperv.h | 1 - 18 files changed, 1396 insertions(+), 533 deletions(-) create mode 100644 arch/x86/include/asm/hyperv-tlfs.h delete mode 100644 arch/x86/include/uapi/asm/hyperv.h create mode 100644 arch/x86/kvm/vmx_evmcs.h
Signature · 1 line
2.14.3
Vitaly Kuznetsov
maintainerPATCH v4 1/7 x86/hyper-v: move hyperv.h out of uapi
hyperv.h is not part of uapi, there are no (known) users outside of kernel. We are making changes to this file to match current Hyper-V Hypervisor Top-Level Functional Specification (TLFS, see:
https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/reference/tlfs)
and we don't want to maintain backwards compatibility.
Move the file renaming to hyperv-tlfs.h to avoid confusing it with mshyperv.h. In future, all definitions from TLFS should go to it and all kernel objects should go to mshyperv.h or include/linux/hyperv.h.
---
Changes since v3:
- Decipher 'TLFS' and add a reference where it can be obtained [Christoph Hellwig]
--- MAINTAINERS | 2 +- arch/x86/hyperv/hv_init.c | 2 +- arch/x86/include/asm/hyperv-tlfs.h | 428 +++++++++++++++++++++++++++++++++++ arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/asm/mshyperv.h | 2 +- arch/x86/include/uapi/asm/hyperv.h | 421 ---------------------------------- arch/x86/include/uapi/asm/kvm_para.h | 1 - arch/x86/kernel/cpu/mshyperv.c | 2 +- drivers/hv/connection.c | 1 - drivers/hv/hv.c | 1 - drivers/hv/hyperv_vmbus.h | 1 + drivers/hv/vmbus_drv.c | 1 - include/linux/hyperv.h | 1 - 13 files changed, 434 insertions(+), 430 deletions(-) create mode 100644 arch/x86/include/asm/hyperv-tlfs.h delete mode 100644 arch/x86/include/uapi/asm/hyperv.h
MAINTAINERS +1 -1
@@ -6531,7 +6531,7 @@ S: Maintained F: Documentation/networking/netvsc.txt F: arch/x86/include/asm/mshyperv.h F: arch/x86/include/asm/trace/hyperv.h -F: arch/x86/include/uapi/asm/hyperv.h +F: arch/x86/include/asm/hyperv-tlfs.h F: arch/x86/kernel/cpu/mshyperv.c F: arch/x86/hyperv F: drivers/hid/hid-hyperv.c
arch/x86/hyperv/hv_init.c +1 -1
@@ -21,7 +21,7 @@#include <asm/apic.h> #include <asm/desc.h> #include <asm/hypervisor.h> -#include <asm/hyperv.h> +#include <asm/hyperv-tlfs.h> #include <asm/mshyperv.h> #include <linux/version.h> #include <linux/vmalloc.h>
arch/x86/include/asm/hyperv-tlfs.h new file +428 -0
428 lines not on this page · raw message
arch/x86/include/asm/kvm_host.h +1 -0
@@ -34,6 +34,7 @@#include <asm/msr-index.h> #include <asm/asm.h> #include <asm/kvm_page_track.h> +#include <asm/hyperv-tlfs.h> #define KVM_MAX_VCPUS 288#define KVM_SOFT_MAX_VCPUS 240
arch/x86/include/asm/mshyperv.h +1 -1
@@ -6,7 +6,7 @@#include <linux/atomic.h> #include <linux/nmi.h> #include <asm/io.h> -#include <asm/hyperv.h> +#include <asm/hyperv-tlfs.h> #include <asm/nospec-branch.h> /*
arch/x86/include/uapi/asm/hyperv.h deleted +0 -421
421 lines not on this page · raw message
arch/x86/include/uapi/asm/kvm_para.h +0 -1
@@ -3,7 +3,6 @@#define _UAPI_ASM_X86_KVM_PARA_H#include <linux/types.h> -#include <asm/hyperv.h> /* This CPUID returns the signature 'KVMKVMKVM' in ebx, ecx, and edx. It * should be used to determine that a VM is running under KVM.
arch/x86/kernel/cpu/mshyperv.c +1 -1
@@ -22,7 +22,7 @@#include <linux/kexec.h> #include <asm/processor.h> #include <asm/hypervisor.h> -#include <asm/hyperv.h> +#include <asm/hyperv-tlfs.h> #include <asm/mshyperv.h> #include <asm/desc.h> #include <asm/irq_regs.h>
drivers/hv/connection.c +0 -1
@@ -31,7 +31,6 @@#include <linux/vmalloc.h> #include <linux/hyperv.h> #include <linux/export.h> -#include <asm/hyperv.h> #include <asm/mshyperv.h> #include "hyperv_vmbus.h"
drivers/hv/hv.c +0 -1
@@ -29,7 +29,6 @@#include <linux/version.h> #include <linux/interrupt.h> #include <linux/clockchips.h> -#include <asm/hyperv.h> #include <asm/mshyperv.h> #include "hyperv_vmbus.h"
drivers/hv/hyperv_vmbus.h +1 -0
@@ -27,6 +27,7 @@#include <linux/list.h> #include <asm/sync_bitops.h> +#include <asm/hyperv-tlfs.h> #include <linux/atomic.h> #include <linux/hyperv.h> #include <linux/interrupt.h>
drivers/hv/vmbus_drv.c +0 -1
@@ -36,7 +36,6 @@#include <linux/cpu.h> #include <linux/sched/task_stack.h> -#include <asm/hyperv.h> #include <asm/mshyperv.h> #include <linux/notifier.h> #include <linux/ptrace.h>
include/linux/hyperv.h +0 -1
@@ -26,7 +26,6 @@#define _HYPERV_H#include <uapi/linux/hyperv.h> -#include <uapi/asm/hyperv.h> #include <linux/types.h> #include <linux/scatterlist.h>
Signature · 1 line
2.14.3
Vitaly Kuznetsov
maintainerPATCH v4 2/7 x86/hyper-v: move definitions from TLFS to hyperv-tlfs.h
In reply to Vitaly Kuznetsovmshyperv.h now only contains fucntions/variables we define in kernel, all definitions from TLFS should go to hyperv-tlfs.h.
'enum hv_cpuid_function' is removed as we already have this info in hyperv-tlfs.h, code in mshyperv.c is adjusted accordingly.
--- arch/x86/include/asm/hyperv-tlfs.h | 57 ++++++++++++++++++++++++++++ arch/x86/include/asm/mshyperv.h | 78 +------------------------------------- arch/x86/kernel/cpu/mshyperv.c | 15 ++++---- 3 files changed, 66 insertions(+), 84 deletions(-)
arch/x86/include/asm/hyperv-tlfs.h +57 -0
@@ -244,6 +244,55 @@#define HV_X64_MSR_CRASH_PARAMS \ (1 + (HV_X64_MSR_CRASH_P4 - HV_X64_MSR_CRASH_P0)) +/* +* Declare the MSR used to setup pages used to communicate with the hypervisor. +*/ +union hv_x64_msr_hypercall_contents { +u64 as_uint64; +struct { +u64 enable:1 ; +u64 reserved:11 ; +u64 guest_physical_address:52 ; + }; +}; + +/* +* TSC page layout. +*/ +struct ms_hyperv_tsc_page { +volatile u32 tsc_sequence; +u32 reserved1; +volatile u64 tsc_scale; +volatile s64 tsc_offset; +u64 reserved2[509 ]; +}; + +/* +* The guest OS needs to register the guest ID with the hypervisor. +* The guest ID is a 64 bit entity and the structure of this ID is +* specified in the Hyper-V specification: +* +* msdn.microsoft.com/en-us/library/windows/hardware/ff542653%28v=vs.85%29.aspx +* +* While the current guideline does not specify how Linux guest ID(s) +* need to be generated, our plan is to publish the guidelines for +* Linux and other guest operating systems that currently are hosted +* on Hyper-V. The implementation here conforms to this yet +* unpublished guidelines. +* +* +* Bit(s) +* 63 - Indicates if the OS is Open Source or not; 1 is Open Source +* 62:56 - Os Type; Linux is 0x100 +* 55:48 - Distro specific identification +* 47:16 - Linux kernel version number +* 15:0 - Distro specific identification +* +* +*/ + +#define HV_LINUX_VENDOR_ID 0x8100 +/* TSC emulation after migration */ #define HV_X64_MSR_REENLIGHTENMENT_CONTROL 0x40000106
@@ -305,6 +354,14 @@ enum HV_GENERIC_SET_FORMAT { HV_GENERIC_SET_ALL, }; +#define HV_HYPERCALL_RESULT_MASK GENMASK_ULL(15, 0) +#define HV_HYPERCALL_FAST_BIT BIT(16) +#define HV_HYPERCALL_VARHEAD_OFFSET 17 +#define HV_HYPERCALL_REP_COMP_OFFSET 32 +#define HV_HYPERCALL_REP_COMP_MASK GENMASK_ULL(43, 32) +#define HV_HYPERCALL_REP_START_OFFSET 48 +#define HV_HYPERCALL_REP_START_MASK GENMASK_ULL(59, 48) +/* hypercall status code */ #define HV_STATUS_SUCCESS 0#define HV_STATUS_INVALID_HYPERCALL_CODE 2
arch/x86/include/asm/mshyperv.h +1 -77
98 lines not on this page · raw message
arch/x86/kernel/cpu/mshyperv.c +8 -7
@@ -180,8 +180,8 @@ static void __init ms_hyperv_init_platform(void)pr_info ("Hyper-V: features 0x%x, hints 0x%x\n" , ms_hyperv.features, ms_hyperv.hints); - ms_hyperv.max_vp_index =cpuid_eax (HVCPUID_IMPLEMENTATION_LIMITS ); - ms_hyperv.max_lp_index =cpuid_ebx (HVCPUID_IMPLEMENTATION_LIMITS ); + ms_hyperv.max_vp_index =cpuid_eax (HYPERV_CPUID_IMPLEMENT_LIMITS ); + ms_hyperv.max_lp_index =cpuid_ebx (HYPERV_CPUID_IMPLEMENT_LIMITS );pr_debug ("Hyper-V: max %u virtual processors, %u logical processors\n" , ms_hyperv.max_vp_index, ms_hyperv.max_lp_index);
@@ -189,11 +189,12 @@ static void __init ms_hyperv_init_platform(void)/* * Extract host information. */ -if (cpuid_eax (HVCPUID_VENDOR_MAXFUNCTION ) >=HVCPUID_VERSION ) { - hv_host_info_eax =cpuid_eax (HVCPUID_VERSION ); - hv_host_info_ebx =cpuid_ebx (HVCPUID_VERSION ); - hv_host_info_ecx =cpuid_ecx (HVCPUID_VERSION ); - hv_host_info_edx =cpuid_edx (HVCPUID_VERSION ); +if (cpuid_eax (HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS ) >= +HYPERV_CPUID_VERSION ) { + hv_host_info_eax =cpuid_eax (HYPERV_CPUID_VERSION ); + hv_host_info_ebx =cpuid_ebx (HYPERV_CPUID_VERSION ); + hv_host_info_ecx =cpuid_ecx (HYPERV_CPUID_VERSION ); + hv_host_info_edx =cpuid_edx (HYPERV_CPUID_VERSION ); pr_info("Hyper-V Host Build:%d-%d.%d-%d-%d.%d\n", hv_host_info_eax, hv_host_info_ebx >> 16,
Signature · 1 line
2.14.3
Vitaly Kuznetsov
maintainerPATCH v4 3/7 x86/kvm: rename HV_X64_MSR_APIC_ASSIST_PAGE to HV_X64_MSR_VP_ASSIST_PAGE
In reply to Vitaly KuznetsovFrom: Ladi Prosek <[email protected]>
The assist page has been used only for the paravirtual EOI so far, hence the "APIC" in the MSR name. Renaming to match the Hyper-V TLFS where it's called "Virtual VP Assist MSR".
--- arch/x86/include/asm/hyperv-tlfs.h | 10 +++++----- arch/x86/kvm/hyperv.c | 8 ++++---- arch/x86/kvm/lapic.h | 2 +- arch/x86/kvm/x86.c | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-)
arch/x86/include/asm/hyperv-tlfs.h +5 -5
@@ -196,7 +196,7 @@#define HV_X64_MSR_EOI 0x40000070#define HV_X64_MSR_ICR 0x40000071#define HV_X64_MSR_TPR 0x40000072 -#define HV_X64_MSR_APIC_ASSIST_PAGE 0x40000073 +#define HV_X64_MSR_VP_ASSIST_PAGE 0x40000073/* Define synthetic interrupt controller model specific registers. */ #define HV_X64_MSR_SCONTROL 0x40000080
@@ -331,10 +331,10 @@ struct hv_tsc_emulation_status {#define HVCALL_POST_MESSAGE 0x005c#define HVCALL_SIGNAL_EVENT 0x005d -#define HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE 0x00000001 -#define HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT 12 -#define HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_MASK \ - (~((1ull << HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT) - 1)) +#define HV_X64_MSR_VP_ASSIST_PAGE_ENABLE 0x00000001 +#define HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT 12 +#define HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_MASK \ + (~((1ull << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) - 1))#define HV_X64_MSR_TSC_REFERENCE_ENABLE 0x00000001#define HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT 12
arch/x86/kvm/hyperv.c +4 -4
@@ -1009,17 +1009,17 @@ static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)return 1 ; hv->vp_index = (u32 )data;break ; -case HV_X64_MSR_APIC_ASSIST_PAGE : { +case HV_X64_MSR_VP_ASSIST_PAGE : {u64 gfn;unsigned long addr; -if (!(data &HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE )) { +if (!(data &HV_X64_MSR_VP_ASSIST_PAGE_ENABLE )) { hv->hv_vapic = data;if (kvm_lapic_enable_pv_eoi (vcpu,0 ))return 1 ;break ; } - gfn = data >>HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT ; + gfn = data >>HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT ; addr =kvm_vcpu_gfn_to_hva (vcpu, gfn);if (kvm_is_error_hva (addr))return 1 ;
@@ -1129,7 +1129,7 @@ static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)return kvm_hv_vapic_msr_read (vcpu,APIC_ICR , pdata);case HV_X64_MSR_TPR :return kvm_hv_vapic_msr_read (vcpu,APIC_TASKPRI , pdata); -case HV_X64_MSR_APIC_ASSIST_PAGE : +case HV_X64_MSR_VP_ASSIST_PAGE : data = hv->hv_vapic;break ;case HV_X64_MSR_VP_RUNTIME :
arch/x86/kvm/lapic.h +1 -1
@@ -109,7 +109,7 @@ int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data);static inline bool kvm_hv_vapic_assist_page_enabled (struct kvm_vcpu *vcpu) { -return vcpu->arch.hyperv.hv_vapic &HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE ; +return vcpu->arch.hyperv.hv_vapic &HV_X64_MSR_VP_ASSIST_PAGE_ENABLE ; }int kvm_lapic_enable_pv_eoi (struct kvm_vcpu *vcpu,u64 data);
arch/x86/kvm/x86.c +1 -1
@@ -1032,7 +1032,7 @@ static u32 emulated_msrs[] = {HV_X64_MSR_VP_RUNTIME ,HV_X64_MSR_SCONTROL ,HV_X64_MSR_STIMER0_CONFIG , -HV_X64_MSR_APIC_ASSIST_PAGE ,MSR_KVM_ASYNC_PF_EN ,MSR_KVM_STEAL_TIME , +HV_X64_MSR_VP_ASSIST_PAGE ,MSR_KVM_ASYNC_PF_EN ,MSR_KVM_STEAL_TIME ,MSR_KVM_PV_EOI_EN , MSR_IA32_TSC_ADJUST,
Signature · 1 line
2.14.3
Vitaly Kuznetsov
maintainerPATCH v4 4/7 x86/hyper-v: allocate and use Virtual Processor Assist Pages
In reply to Vitaly KuznetsovVirtual Processor Assist Pages usage allows us to do optimized EOI processing for APIC, enable Enlightened VMCS support in KVM and more. struct hv_vp_assist_page is defined according to the Hyper-V TLFS v5.0b.
---
Changes since v3:
- remove vfree() from hv_cpu_die() [Thomas Gleixner]
- free hv_vp_assist_page() in the error path [Thomas Gleixner] While on it, add cpuhp_remove_state() call when we fail to allocate hypercall page to make the error path complete.
- check hv_vp_assist_page != NULL in hv_get_vp_assist_page() [Thomas Gleixner]
--- arch/x86/hyperv/hv_init.c | 43 +++++++++++++++++++++++++++++++++++--- arch/x86/include/asm/hyperv-tlfs.h | 13 ++++++++++++ arch/x86/include/asm/mshyperv.h | 13 ++++++++++++ 3 files changed, 66 insertions(+), 3 deletions(-)
arch/x86/hyperv/hv_init.c +40 -3
@@ -88,6 +88,9 @@ EXPORT_SYMBOL_GPL(hyperv_cs);u32 *hv_vp_index;EXPORT_SYMBOL_GPL (hv_vp_index); +struct hv_vp_assist_page **hv_vp_assist_page; +EXPORT_SYMBOL_GPL (hv_vp_assist_page); +u32 hv_max_vp_index;static int hv_cpu_init (unsigned int cpu)
@@ -101,6 +104,23 @@ static int hv_cpu_init(unsigned int cpu)if (msr_vp_index > hv_max_vp_index) hv_max_vp_index = msr_vp_index; +if (!hv_vp_assist_page) +return 0 ; + +if (!hv_vp_assist_page[smp_processor_id ()]) + hv_vp_assist_page[smp_processor_id ()] = +__vmalloc (PAGE_SIZE ,GFP_KERNEL ,PAGE_KERNEL ); + +if (hv_vp_assist_page[smp_processor_id ()]) { +u64 val; + + val =vmalloc_to_pfn (hv_vp_assist_page[smp_processor_id ()]); + val = (val <<HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT ) | +HV_X64_MSR_VP_ASSIST_PAGE_ENABLE ; + +wrmsrl (HV_X64_MSR_VP_ASSIST_PAGE , val); + } +return 0 ; }
@@ -198,6 +218,9 @@ static int hv_cpu_die(unsigned int cpu)struct hv_reenlightenment_control re_ctrl;unsigned int new_cpu; +if (hv_vp_assist_page && hv_vp_assist_page[cpu]) +wrmsrl (HV_X64_MSR_VP_ASSIST_PAGE ,0 ); +if (hv_reenlightenment_cb ==NULL )return 0 ;
@@ -224,6 +247,7 @@ void hyperv_init(void) {u64 guest_id, required_msrs;union hv_x64_msr_hypercall_contents hypercall_msr; +int cpuhp;if (x86_hyper_type !=X86_HYPER_MS_HYPERV )return ;
@@ -241,9 +265,17 @@ void hyperv_init(void)if (!hv_vp_index)return ; -if (cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"x86/hyperv_init:online" , - hv_cpu_init, hv_cpu_die) <0 ) + hv_vp_assist_page =kcalloc (num_possible_cpus (), +sizeof (*hv_vp_assist_page),GFP_KERNEL ); +if (!hv_vp_assist_page) { + ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED ; goto free_vp_index; + } + + cpuhp =cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"x86/hyperv_init:online" , + hv_cpu_init, hv_cpu_die); +if (cpuhp <0 ) + goto free_vp_assist_page;/* * Setup the hypercall page and enable hypercalls.
@@ -256,7 +288,7 @@ void hyperv_init(void) hv_hypercall_pg =__vmalloc (PAGE_SIZE ,GFP_KERNEL ,PAGE_KERNEL_RX );if (hv_hypercall_pg ==NULL ) {wrmsrl (HV_X64_MSR_GUEST_OS_ID ,0 ); - goto free_vp_index; + goto remove_cpuhp_state; }rdmsrl (HV_X64_MSR_HYPERCALL , hypercall_msr.as_uint64);
@@ -304,6 +336,11 @@ void hyperv_init(void)return ; +remove_cpuhp_state: +cpuhp_remove_state (cpuhp); +free_vp_assist_page: +kfree (hv_vp_assist_page); + hv_vp_assist_page =NULL ; free_vp_index:kfree (hv_vp_index); hv_vp_index =NULL ;
arch/x86/include/asm/hyperv-tlfs.h +13 -0
@@ -163,6 +163,9 @@/* Recommend using the newer ExProcessorMasks interface */ #define HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED (1 << 11) +/* Recommend using enlightened VMCS */ +#define HV_X64_ENLIGHTENED_VMCS_RECOMMENDED (1 << 14) +/* * Crash notification flag. */
@@ -476,6 +479,16 @@ struct hv_timer_message_payload {__u64 delivery_time;/* When the message was delivered */ }; +/* Define virtual processor assist page structure. */ +struct hv_vp_assist_page { +__u32 apic_assist; +__u32 reserved; +__u64 vtl_control[2 ]; +__u64 nested_enlightenments_control[2 ]; +__u32 enlighten_vmentry; +__u64 current_nested_vmcs; +}; +#define HV_STIMER_ENABLE (1ULL << 0)#define HV_STIMER_PERIODIC (1ULL << 1)#define HV_STIMER_LAZY (1ULL << 2)
arch/x86/include/asm/mshyperv.h +13 -0
@@ -218,6 +218,15 @@ static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size,*/ extern u32 *hv_vp_index;extern u32 hv_max_vp_index; +extern struct hv_vp_assist_page **hv_vp_assist_page; + +static inline struct hv_vp_assist_page *hv_get_vp_assist_page (unsigned int cpu) +{ +if (!hv_vp_assist_page) +return NULL ; + +return hv_vp_assist_page[cpu]; +}/** * hv_cpu_number_to_vp_number() - Map CPU to VP.
@@ -254,6 +263,10 @@ static inline void hyperv_setup_mmu_ops(void) {}static inline void set_hv_tscchange_cb (void (*cb)(void )) {}static inline void clear_hv_tscchange_cb (void ) {}static inline void hyperv_stop_tsc_emulation (void ) {}; +static inline struct hv_vp_assist_page *hv_get_vp_assist_page (unsigned int cpu) +{ +return NULL ; +}#endif /* CONFIG_HYPERV */ #ifdef CONFIG_HYPERV_TSCPAGE
Signature · 1 line
2.14.3
Re: [PATCH v4 4/7] x86/hyper-v: allocate and use Virtual Processor Assist Pages
arch/x86/hyperv/hv_init.c static int hv_cpu_init(unsigned int cpu) · line 112
Quoted 11 lines · Vitaly Kuznetsov
static int
hv_cpu_init(unsigned int cpu)arch/x86/hyperv/hv_init.c +6
-0@@ -101,6 +104,23 @@ static int hv_cpu_init(unsigned int cpu)if (msr_vp_index > hv_max_vp_index) hv_max_vp_index = msr_vp_index; +if (!hv_vp_assist_page) +return 0 ; + +if (!hv_vp_assist_page[smp_processor_id ()]) + hv_vp_assist_page[smp_processor_id ()] = +__vmalloc (PAGE_SIZE ,GFP_KERNEL ,PAGE_KERNEL );
Please avoid the ugly line break which makes the statement appear as multiline - which would require curly braces for readability - and simply have a local variable
struct hv_vp_assist_page **hvp = hv_vp_assist_page[smp_processor_id()];
and use that throughout the function.
arch/x86/hyperv/hv_init.c static int hv_cpu_init(unsigned int cpu) · line 124
Quoted 11 lines
arch/x86/hyperv/hv_init.c +10
-0+if (hv_vp_assist_page[smp_processor_id ()]) { +u64 val; + + val =vmalloc_to_pfn (hv_vp_assist_page[smp_processor_id ()]); + val = (val <<HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT ) | +HV_X64_MSR_VP_ASSIST_PAGE_ENABLE ; + +wrmsrl (HV_X64_MSR_VP_ASSIST_PAGE , val); + } +return 0 ;
Other than that:
Vitaly Kuznetsov
maintainerPATCH v4 5/7 x86/hyper-v: define struct hv_enlightened_vmcs and clean field bits
In reply to Vitaly KuznetsovThe definitions are according to the Hyper-V TLFS v5.0. KVM on Hyper-V will use these.
--- arch/x86/include/asm/hyperv-tlfs.h | 200 +++++++++++++++++++++++++++++++++++++ 1 file changed, 200 insertions(+)
arch/x86/include/asm/hyperv-tlfs.h +200 -0
@@ -489,6 +489,206 @@ struct hv_vp_assist_page {__u64 current_nested_vmcs; }; +struct hv_enlightened_vmcs { +u32 revision_id; +u32 abort; + +u16 host_es_selector; +u16 host_cs_selector; +u16 host_ss_selector; +u16 host_ds_selector; +u16 host_fs_selector; +u16 host_gs_selector; +u16 host_tr_selector; + +u64 host_ia32_pat; +u64 host_ia32_efer; + +u64 host_cr0; +u64 host_cr3; +u64 host_cr4; + +u64 host_ia32_sysenter_esp; +u64 host_ia32_sysenter_eip; +u64 host_rip; +u32 host_ia32_sysenter_cs; + +u32 pin_based_vm_exec_control; +u32 vm_exit_controls; +u32 secondary_vm_exec_control; + +u64 io_bitmap_a; +u64 io_bitmap_b; +u64 msr_bitmap; + +u16 guest_es_selector; +u16 guest_cs_selector; +u16 guest_ss_selector; +u16 guest_ds_selector; +u16 guest_fs_selector; +u16 guest_gs_selector; +u16 guest_ldtr_selector; +u16 guest_tr_selector; + +u32 guest_es_limit; +u32 guest_cs_limit; +u32 guest_ss_limit; +u32 guest_ds_limit; +u32 guest_fs_limit; +u32 guest_gs_limit; +u32 guest_ldtr_limit; +u32 guest_tr_limit; +u32 guest_gdtr_limit; +u32 guest_idtr_limit; + +u32 guest_es_ar_bytes; +u32 guest_cs_ar_bytes; +u32 guest_ss_ar_bytes; +u32 guest_ds_ar_bytes; +u32 guest_fs_ar_bytes; +u32 guest_gs_ar_bytes; +u32 guest_ldtr_ar_bytes; +u32 guest_tr_ar_bytes; + +u64 guest_es_base; +u64 guest_cs_base; +u64 guest_ss_base; +u64 guest_ds_base; +u64 guest_fs_base; +u64 guest_gs_base; +u64 guest_ldtr_base; +u64 guest_tr_base; +u64 guest_gdtr_base; +u64 guest_idtr_base; + +u64 padding64_1[3 ]; + +u64 vm_exit_msr_store_addr; +u64 vm_exit_msr_load_addr; +u64 vm_entry_msr_load_addr; + +u64 cr3_target_value0; +u64 cr3_target_value1; +u64 cr3_target_value2; +u64 cr3_target_value3; + +u32 page_fault_error_code_mask; +u32 page_fault_error_code_match; + +u32 cr3_target_count; +u32 vm_exit_msr_store_count; +u32 vm_exit_msr_load_count; +u32 vm_entry_msr_load_count; + +u64 tsc_offset; +u64 virtual_apic_page_addr; +u64 vmcs_link_pointer; + +u64 guest_ia32_debugctl; +u64 guest_ia32_pat; +u64 guest_ia32_efer; + +u64 guest_pdptr0; +u64 guest_pdptr1; +u64 guest_pdptr2; +u64 guest_pdptr3; + +u64 guest_pending_dbg_exceptions; +u64 guest_sysenter_esp; +u64 guest_sysenter_eip; + +u32 guest_activity_state; +u32 guest_sysenter_cs; + +u64 cr0_guest_host_mask; +u64 cr4_guest_host_mask; +u64 cr0_read_shadow; +u64 cr4_read_shadow; +u64 guest_cr0; +u64 guest_cr3; +u64 guest_cr4; +u64 guest_dr7; + +u64 host_fs_base; +u64 host_gs_base; +u64 host_tr_base; +u64 host_gdtr_base; +u64 host_idtr_base; +u64 host_rsp; + +u64 ept_pointer; + +u16 virtual_processor_id; +u16 padding16[3 ]; + +u64 padding64_2[5 ]; +u64 guest_physical_address; + +u32 vm_instruction_error; +u32 vm_exit_reason; +u32 vm_exit_intr_info; +u32 vm_exit_intr_error_code; +u32 idt_vectoring_info_field; +u32 idt_vectoring_error_code; +u32 vm_exit_instruction_len; +u32 vmx_instruction_info; + +u64 exit_qualification; +u64 exit_io_instruction_ecx; +u64 exit_io_instruction_esi; +u64 exit_io_instruction_edi; +u64 exit_io_instruction_eip; + +u64 guest_linear_address; +u64 guest_rsp; +u64 guest_rflags; + +u32 guest_interruptibility_info; +u32 cpu_based_vm_exec_control; +u32 exception_bitmap; +u32 vm_entry_controls; +u32 vm_entry_intr_info_field; +u32 vm_entry_exception_error_code; +u32 vm_entry_instruction_len; +u32 tpr_threshold; + +u64 guest_rip; + +u32 hv_clean_fields; +u32 hv_padding_32; +u32 hv_synthetic_controls; +u32 hv_enlightenments_control; +u32 hv_vp_id; + +u64 hv_vm_id; +u64 partition_assist_page; +u64 padding64_4[4 ]; +u64 guest_bndcfgs; +u64 padding64_5[7 ]; +u64 xss_exit_bitmap; +u64 padding64_6[7 ]; +}; + +#define HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE 0 +#define HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP BIT(0) +#define HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP BIT(1) +#define HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2 BIT(2) +#define HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1 BIT(3) +#define HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC BIT(4) +#define HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT BIT(5) +#define HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY BIT(6) +#define HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN BIT(7) +#define HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR BIT(8) +#define HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT BIT(9) +#define HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC BIT(10) +#define HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1 BIT(11) +#define HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2 BIT(12) +#define HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER BIT(13) +#define HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1 BIT(14) +#define HV_VMX_ENLIGHTENED_CLEAN_FIELD_ENLIGHTENMENTSCONTROL BIT(15) + +#define HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL 0xFFFF +#define HV_STIMER_ENABLE (1ULL << 0)#define HV_STIMER_PERIODIC (1ULL << 1)#define HV_STIMER_LAZY (1ULL << 2)
Signature · 1 line
2.14.3
Vitaly Kuznetsov
maintainerPATCH v4 6/7 x86/hyper-v: detect nested features
In reply to Vitaly KuznetsovTLFS 5.0 says: "Support for an enlightened VMCS interface is reported with CPUID leaf 0x40000004. If an enlightened VMCS interface is supported,
additional nested enlightenments may be discovered by reading the CPUID
leaf 0x4000000A (see 2.4.11)."
--- arch/x86/include/asm/hyperv-tlfs.h | 4 ++++ arch/x86/include/asm/mshyperv.h | 1 + arch/x86/kernel/cpu/mshyperv.c | 4 ++++ 3 files changed, 9 insertions(+)
arch/x86/include/asm/hyperv-tlfs.h +4 -0
@@ -21,6 +21,7 @@#define HYPERV_CPUID_FEATURES 0x40000003#define HYPERV_CPUID_ENLIGHTMENT_INFO 0x40000004#define HYPERV_CPUID_IMPLEMENT_LIMITS 0x40000005 +#define HYPERV_CPUID_NESTED_FEATURES 0x4000000A#define HYPERV_HYPERVISOR_PRESENT_BIT 0x80000000#define HYPERV_CPUID_MIN 0x40000005
@@ -339,6 +340,9 @@ struct hv_tsc_emulation_status {#define HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_MASK \ (~((1ull << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) - 1)) +/* Hyper-V Enlightened VMCS version mask in nested features CPUID */ +#define HV_X64_ENLIGHTENED_VMCS_VERSION 0xff +#define HV_X64_MSR_TSC_REFERENCE_ENABLE 0x00000001#define HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT 12
arch/x86/include/asm/mshyperv.h +1 -0
@@ -13,6 +13,7 @@ struct ms_hyperv_info {u32 features;u32 misc_features;u32 hints; +u32 nested_features;u32 max_vp_index;u32 max_lp_index; };
arch/x86/kernel/cpu/mshyperv.c +4 -0
@@ -208,6 +208,10 @@ static void __init ms_hyperv_init_platform(void) x86_platform.calibrate_cpu = hv_get_tsc_khz; } +if (ms_hyperv.hints &HV_X64_ENLIGHTENED_VMCS_RECOMMENDED ) + ms_hyperv.nested_features = +cpuid_eax (HYPERV_CPUID_NESTED_FEATURES ); +#ifdef CONFIG_X86_LOCAL_APIC if (ms_hyperv.features &HV_X64_ACCESS_FREQUENCY_MSRS && ms_hyperv.misc_features &HV_FEATURE_FREQUENCY_MSRS_AVAILABLE ) {
Signature · 1 line
2.14.3
Re: [PATCH v4 6/7] x86/hyper-v: detect nested features
arch/x86/kernel/cpu/mshyperv.c static void __init ms_hyperv_init_platform(void) · line 213
Quoted 4 lines · Vitaly Kuznetsov
arch/x86/kernel/cpu/mshyperv.c +3
-0+if (ms_hyperv.hints &HV_X64_ENLIGHTENED_VMCS_RECOMMENDED ) + ms_hyperv.nested_features = +cpuid_eax (HYPERV_CPUID_NESTED_FEATURES );
Please add curly braces. Rationale:
https://marc.info/?l=linux-kernel&m=148467980905537
I know it's a pet pieve, ...
Thanks,
tglx
Thomas Gleixner
Reviewed-byRe: [PATCH v4 6/7] x86/hyper-v: detect nested features
Quoted 11 lines · Thomas Gleixner
arch/x86/kernel/cpu/mshyperv.c static void __init ms_hyperv_init_platform(void) · line 213
Quoted 4 lines · Vitaly Kuznetsov
arch/x86/kernel/cpu/mshyperv.c +3
-0+if (ms_hyperv.hints &HV_X64_ENLIGHTENED_VMCS_RECOMMENDED ) + ms_hyperv.nested_features = +cpuid_eax (HYPERV_CPUID_NESTED_FEATURES );Please add curly braces. Rationale:
https://marc.info/?l=linux-kernel&m=148467980905537I know it's a pet pieve, ...
Other than that:
Vitaly Kuznetsov
maintainerPATCH v4 7/7 x86/kvm: use Enlightened VMCS when running on Hyper-V
In reply to Vitaly KuznetsovEnlightened VMCS is just a structure in memory, the main benefit besides avoiding somewhat slower VMREAD/VMWRITE is using clean field
mask: we tell the underlying hypervisor which fields were modified since VMEXIT so there's no need to inspect them all.
Tight CPUID loop test shows significant speedup:
Before: 18890 cycles
After: 8304 cycles
Static key is being used to avoid performance penalty for non-Hyper-V deployments.
---
Changes since v3 [Paolo Bonzini]:
- split off vmx_evmcs.h
- merge get_evmcs_offset() and get_evmcs_offset_cf()
- move evmcs_sanitize_exec_ctrls() to setup_vmcs_config
- rename vmcs_load_enlightened() -> evmcs_load()
- drop evmcs_sanitize_exec_ctrls() from dump_vmcs()
--- arch/x86/kvm/vmx.c | 301 +++++++++++++++++++++++++++++++++++++++++-- arch/x86/kvm/vmx_evmcs.h | 324 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 615 insertions(+), 10 deletions(-) create mode 100644 arch/x86/kvm/vmx_evmcs.h
arch/x86/kvm/vmx.c +291 -10
@@ -53,9 +53,11 @@#include <asm/mmu_context.h> #include <asm/microcode.h> #include <asm/nospec-branch.h> +#include <asm/mshyperv.h> #include "trace.h" #include "pmu.h" +#include "vmx_evmcs.h" #define __ex (x) __kvm_handle_fault_on_reboot(x)#define __ex_clear (x, reg) \
@@ -1000,6 +1002,169 @@ static const u32 vmx_msr_index[] = {MSR_EFER ,MSR_TSC_AUX ,MSR_STAR , }; +DEFINE_STATIC_KEY_FALSE (enable_evmcs); + +#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs)) + +#if IS_ENABLED (CONFIG_HYPERV ) +static bool __read_mostly enlightened_vmcs = true; +module_param (enlightened_vmcs, bool,0444 ); + +#define KVM_EVMCS_VERSION 1 + +static inline void evmcs_write64 (unsigned long field,u64 value) +{ +u16 clean_field; +int offset =get_evmcs_offset (field, &clean_field); + +if (offset <0 ) +return ; + + *(u64 *)((char *)current_evmcs + offset) = value; + + current_evmcs->hv_clean_fields &= ~clean_field; +} + +static inline void evmcs_write32 (unsigned long field,u32 value) +{ +u16 clean_field; +int offset =get_evmcs_offset (field, &clean_field); + +if (offset <0 ) +return ; + + *(u32 *)((char *)current_evmcs + offset) = value; + current_evmcs->hv_clean_fields &= ~clean_field; +} + +static inline void evmcs_write16 (unsigned long field,u16 value) +{ +u16 clean_field; +int offset =get_evmcs_offset (field, &clean_field); + +if (offset <0 ) +return ; + + *(u16 *)((char *)current_evmcs + offset) = value; + current_evmcs->hv_clean_fields &= ~clean_field; +} + +static inline u64 evmcs_read64 (unsigned long field) +{ +int offset =get_evmcs_offset (field,NULL ); + +if (offset <0 ) +return 0 ; + +return *(u64 *)((char *)current_evmcs + offset); +} + +static inline u32 evmcs_read32 (unsigned long field) +{ +int offset =get_evmcs_offset (field,NULL ); + +if (offset <0 ) +return 0 ; + +return *(u32 *)((char *)current_evmcs + offset); +} + +static inline u16 evmcs_read16 (unsigned long field) +{ +int offset =get_evmcs_offset (field,NULL ); + +if (offset <0 ) +return 0 ; + +return *(u16 *)((char *)current_evmcs + offset); +} + +static void evmcs_load (u64 phys_addr) +{ +struct hv_vp_assist_page *vp_ap = +hv_get_vp_assist_page (smp_processor_id ()); + + vp_ap->current_nested_vmcs = phys_addr; + vp_ap->enlighten_vmentry =1 ; +} + +static void evmcs_sanitize_exec_ctrls (struct vmcs_config *vmcs_conf) +{ +/* +* Enlightened VMCSv1 doesn't support these: +* +* POSTED_INTR_NV = 0x00000002, +* GUEST_INTR_STATUS = 0x00000810, +* APIC_ACCESS_ADDR = 0x00002014, +* POSTED_INTR_DESC_ADDR = 0x00002016, +* EOI_EXIT_BITMAP0 = 0x0000201c, +* EOI_EXIT_BITMAP1 = 0x0000201e, +* EOI_EXIT_BITMAP2 = 0x00002020, +* EOI_EXIT_BITMAP3 = 0x00002022, +*/ + vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR ; + vmcs_conf->cpu_based_2nd_exec_ctrl &= + ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY ; + vmcs_conf->cpu_based_2nd_exec_ctrl &= + ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES ; + vmcs_conf->cpu_based_2nd_exec_ctrl &= + ~SECONDARY_EXEC_APIC_REGISTER_VIRT ; + +/* +* GUEST_PML_INDEX = 0x00000812, +* PML_ADDRESS = 0x0000200e, +*/ + vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML ; + +/* VM_FUNCTION_CONTROL = 0x00002018, */ + vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC ; + +/* +* EPTP_LIST_ADDRESS = 0x00002024, +* VMREAD_BITMAP = 0x00002026, +* VMWRITE_BITMAP = 0x00002028, +*/ + vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS ; + +/* +* TSC_MULTIPLIER = 0x00002032, +*/ + vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING ; + +/* +* PLE_GAP = 0x00004020, +* PLE_WINDOW = 0x00004022, +*/ + vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING ; + +/* +* VMX_PREEMPTION_TIMER_VALUE = 0x0000482E, +*/ + vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER ; + +/* +* GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808, +* HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04, +*/ + vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL ; + vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL ; + +/* +* Currently unsupported in KVM: +* GUEST_IA32_RTIT_CTL = 0x00002814, +*/ +} +#else /* !IS_ENABLED(CONFIG_HYPERV) */ +static inline void evmcs_write64 (unsigned long field,u64 value) {} +static inline void evmcs_write32 (unsigned long field,u32 value) {} +static inline void evmcs_write16 (unsigned long field,u16 value) {} +static inline u64 evmcs_read64 (unsigned long field) {return 0 ; } +static inline u32 evmcs_read32 (unsigned long field) {return 0 ; } +static inline u16 evmcs_read16 (unsigned long field) {return 0 ; } +static inline void evmcs_load (u64 phys_addr) {} +static inline void evmcs_sanitize_exec_ctrls (struct vmcs_config *vmcs_conf) {} +#endif /* IS_ENABLED(CONFIG_HYPERV) */ +static inline bool is_exception_n (u32 intr_info,u8 vector) { return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
@@ -1473,6 +1638,9 @@ static void vmcs_load(struct vmcs *vmcs)u64 phys_addr =__pa (vmcs);u8 error; +if (static_branch_unlikely (&enable_evmcs)) +return evmcs_load (phys_addr); + asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0" :"=qm" (error) :"a" (&phys_addr),"m" (phys_addr) :"cc" ,"memory" );
@@ -1646,18 +1814,24 @@ static __always_inline unsigned long __vmcs_readl(unsigned long field) static __always_inline u16 vmcs_read16(unsigned long field) {vmcs_check16 (field); +if (static_branch_unlikely (&enable_evmcs)) +return evmcs_read16 (field);return __vmcs_readl (field); } static __always_inline u32 vmcs_read32(unsigned long field) {vmcs_check32 (field); +if (static_branch_unlikely (&enable_evmcs)) +return evmcs_read32 (field);return __vmcs_readl (field); } static __always_inline u64 vmcs_read64(unsigned long field) {vmcs_check64 (field); +if (static_branch_unlikely (&enable_evmcs)) +return evmcs_read64 (field);#ifdef CONFIG_X86_64 return __vmcs_readl (field);#else
@@ -1668,6 +1842,8 @@ static __always_inline u64 vmcs_read64(unsigned long field)static __always_inline unsigned long vmcs_readl (unsigned long field) {vmcs_checkl (field); +if (static_branch_unlikely (&enable_evmcs)) +return evmcs_read64 (field);return __vmcs_readl (field); }
@@ -1691,18 +1867,27 @@ static __always_inline void __vmcs_writel(unsigned long field, unsigned long val static __always_inline void vmcs_write16(unsigned long field, u16 value) {vmcs_check16 (field); +if (static_branch_unlikely (&enable_evmcs)) +return evmcs_write16 (field, value); +__vmcs_writel (field, value); } static __always_inline void vmcs_write32(unsigned long field, u32 value) {vmcs_check32 (field); +if (static_branch_unlikely (&enable_evmcs)) +return evmcs_write32 (field, value); +__vmcs_writel (field, value); } static __always_inline void vmcs_write64(unsigned long field, u64 value) {vmcs_check64 (field); +if (static_branch_unlikely (&enable_evmcs)) +return evmcs_write64 (field, value); +__vmcs_writel (field, value);#ifndef CONFIG_X86_64 asmvolatile ("" );
@@ -1713,6 +1898,9 @@ static __always_inline void vmcs_write64(unsigned long field, u64 value) static __always_inline void vmcs_writel(unsigned long field, unsigned long value) {vmcs_checkl (field); +if (static_branch_unlikely (&enable_evmcs)) +return evmcs_write64 (field, value); +__vmcs_writel (field, value); }
@@ -1720,6 +1908,9 @@ static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask) {BUILD_BUG_ON_MSG (__builtin_constant_p (field) && ((field) &0x6000 ) ==0x2000 ,"vmcs_clear_bits does not support 64-bit fields" ); +if (static_branch_unlikely (&enable_evmcs)) +return evmcs_write32 (field,evmcs_read32 (field) & ~mask); +__vmcs_writel (field,__vmcs_readl (field) & ~mask); }
@@ -1727,6 +1918,9 @@ static __always_inline void vmcs_set_bits(unsigned long field, u32 mask) {BUILD_BUG_ON_MSG (__builtin_constant_p (field) && ((field) &0x6000 ) ==0x2000 ,"vmcs_set_bits does not support 64-bit fields" ); +if (static_branch_unlikely (&enable_evmcs)) +return evmcs_write32 (field,evmcs_read32 (field) | mask); +__vmcs_writel (field,__vmcs_readl (field) | mask); }
@@ -3596,6 +3790,14 @@ static int hardware_enable(void)if (cr4_read_shadow () &X86_CR4_VMXE )return -EBUSY ; +/* +* This can happen if we hot-added a CPU but failed to allocate +* VP assist page for it. +*/ +if (static_branch_unlikely (&enable_evmcs) && + !hv_get_vp_assist_page (cpu)) +return -EFAULT ; +INIT_LIST_HEAD (&per_cpu (loaded_vmcss_on_cpu, cpu));INIT_LIST_HEAD (&per_cpu (blocked_vcpu_on_cpu, cpu));spin_lock_init (&per_cpu (blocked_vcpu_on_cpu_lock, cpu));
@@ -3829,7 +4031,12 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) vmcs_conf->size = vmx_msr_high &0x1fff ; vmcs_conf->order =get_order (vmcs_conf->size); vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff ; - vmcs_conf->revision_id = vmx_msr_low; + +/* KVM supports Enlightened VMCS v1 only */ +if (static_branch_unlikely (&enable_evmcs)) + vmcs_conf->revision_id =KVM_EVMCS_VERSION ; +else + vmcs_conf->revision_id = vmx_msr_low; vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
@@ -3837,6 +4044,9 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) vmcs_conf->vmexit_ctrl = _vmexit_control; vmcs_conf->vmentry_ctrl = _vmentry_control; +if (static_branch_unlikely (&enable_evmcs)) +evmcs_sanitize_exec_ctrls (vmcs_conf); + cpu_has_load_ia32_efer =allow_1_setting (MSR_IA32_VMX_ENTRY_CTLS ,VM_ENTRY_LOAD_IA32_EFER )
@@ -8784,7 +8994,8 @@ static void dump_vmcs(void)pr_err ("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n" ,vmcs_read64 (GUEST_IA32_DEBUGCTL ),vmcs_readl (GUEST_PENDING_DBG_EXCEPTIONS )); -if (vmentry_ctl &VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL ) +if (cpu_has_load_perf_global_ctrl && + vmentry_ctl &VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL )pr_err ("PerfGlobCtl = 0x%016llx\n" ,vmcs_read64 (GUEST_IA32_PERF_GLOBAL_CTRL ));if (vmentry_ctl &VM_ENTRY_LOAD_BNDCFGS )
@@ -8820,7 +9031,8 @@ static void dump_vmcs(void)pr_err ("EFER = 0x%016llx PAT = 0x%016llx\n" ,vmcs_read64 (HOST_IA32_EFER ),vmcs_read64 (HOST_IA32_PAT )); -if (vmexit_ctl &VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL ) +if (cpu_has_load_perf_global_ctrl && + vmexit_ctl &VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL )pr_err ("PerfGlobCtl = 0x%016llx\n" ,vmcs_read64 (HOST_IA32_PERF_GLOBAL_CTRL ));
@@ -9397,7 +9609,7 @@ static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)static void __noclone vmx_vcpu_run (struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu); -unsigned long cr3, cr4; +unsigned long cr3, cr4, evmcs_rsp;/* Record the guest's net vcpu time for enforced NMI injections. */ if (unlikely(!enable_vnmi &&
@@ -9463,6 +9675,10 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)native_wrmsrl (MSR_IA32_SPEC_CTRL , vmx->spec_ctrl); vmx->__launched = vmx->loaded_vmcs->launched; + + evmcs_rsp =static_branch_unlikely (&enable_evmcs) ? + (unsigned long )¤t_evmcs->host_rsp :0 ; + asm(/* Store host registers */ "push %%" _ASM_DX "; push %%" _ASM_BP ";"
@@ -9471,15 +9687,21 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)"cmp %%" _ASM_SP", %c[host_rsp](%0) \n\t" "je 1f \n\t" "mov %%" _ASM_SP", %c[host_rsp](%0) \n\t" +/* Avoid VMWRITE when Enlightened VMCS is in use */ +"test %%" _ASM_SI", %%" _ASM_SI" \n\t" +"jz 2f \n\t" +"mov %%" _ASM_SP", (%%" _ASM_SI") \n\t" +"jmp 1f \n\t" +"2: \n\t" __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t""1: \n\t" /* Reload cr2 if changed */ "mov %c[cr2](%0), %%" _ASM_AX" \n\t" "mov %%cr2, %%" _ASM_DX" \n\t" "cmp %%" _ASM_AX", %%" _ASM_DX" \n\t" -"je 2f \n\t" +"je 3f \n\t" "mov %%" _ASM_AX", %%cr2 \n\t" -"2: \n\t" +"3: \n\t" /* Check if vmlaunch of vmresume is needed */ "cmpl $0, %c[launched](%0) \n\t" /* Load guest registers. Don't clobber flags. */
@@ -9548,7 +9770,7 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) ".global vmx_return \n\t" "vmx_return: " _ASM_PTR " 2b \n\t" ".popsection" - : : "c"(vmx), "d"((unsigned long)HOST_RSP), + : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp), [launched]"i"(offsetof(struct vcpu_vmx, __launched)), [fail]"i"(offsetof(struct vcpu_vmx, fail)), [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
@@ -9573,10 +9795,10 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) [wordsize]"i"(sizeof(ulong)) : "cc", "memory"#ifdef CONFIG_X86_64 - , "rax", "rbx", "rdi", "rsi" + , "rax", "rbx", "rdi" ,"r8" ,"r9" ,"r10" ,"r11" ,"r12" ,"r13" ,"r14" ,"r15" #else - ,"eax" ,"ebx" ,"edi" ,"esi" + ,"eax" ,"ebx" ,"edi" #endif );
@@ -9604,6 +9826,11 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)/* Eliminate branch target predictions from guest mode */ vmexit_fill_RSB (); +/* All fields are clean at this point */ +if (static_branch_unlikely (&enable_evmcs)) + current_evmcs->hv_clean_fields |= +HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL ; +/* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ if (vmx->host_debugctlmsr)update_debugctlmsr (vmx->host_debugctlmsr);
@@ -12419,7 +12646,38 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {static int __init vmx_init (void ) { -int r =kvm_init (&vmx_x86_ops,sizeof (struct vcpu_vmx ), +int r; + +#if IS_ENABLED (CONFIG_HYPERV ) +/* +* Enlightened VMCS usage should be recommended and the host needs +* to support eVMCS v1 or above. We can also disable eVMCS support +* with module parameter. +*/ +if (enlightened_vmcs && + ms_hyperv.hints &HV_X64_ENLIGHTENED_VMCS_RECOMMENDED && + (ms_hyperv.nested_features &HV_X64_ENLIGHTENED_VMCS_VERSION ) >= +KVM_EVMCS_VERSION ) { +int cpu; + +/* Check that we have assist pages on all online CPUs */ +for_each_online_cpu (cpu) { +if (!hv_get_vp_assist_page (cpu)) { + enlightened_vmcs = false; +break ; + } + } + +if (enlightened_vmcs) { +pr_info ("KVM: vmx: using Hyper-V Enlightened VMCS\n" ); +static_branch_enable (&enable_evmcs); + } + }else { + enlightened_vmcs = false; + } +#endif + + r =kvm_init (&vmx_x86_ops,sizeof (struct vcpu_vmx ), __alignof__(struct vcpu_vmx ),THIS_MODULE );if (r)return r;
@@ -12440,6 +12698,29 @@ static void __exit vmx_exit(void)#endif kvm_exit (); + +#if IS_ENABLED (CONFIG_HYPERV ) +if (static_branch_unlikely (&enable_evmcs)) { +int cpu; +struct hv_vp_assist_page *vp_ap; +/* +* Reset everything to support using non-enlightened VMCS +* access later (e.g. when we reload the module with +* enlightened_vmcs=0) +*/ +for_each_online_cpu (cpu) { + vp_ap =hv_get_vp_assist_page (cpu); + +if (!vp_ap) +continue ; + + vp_ap->current_nested_vmcs =0 ; + vp_ap->enlighten_vmentry =0 ; + } + +static_branch_disable (&enable_evmcs); + } +#endif }module_init (vmx_init)
arch/x86/kvm/vmx_evmcs.h new file +324 -0
324 lines not on this page · raw message
Signature · 1 line
2.14.3
Re: [PATCH v4 7/7] x86/kvm: use Enlightened VMCS when running on Hyper-V
2018-03-16 15:22+0100, Vitaly Kuznetsov:
Quoted 9 lines
Enlightened VMCS is just a structure in memory, the main benefit besides avoiding somewhat slower VMREAD/VMWRITE is using clean field
mask: we tell the underlying hypervisor which fields were modified since VMEXIT so there's no need to inspect them all.Tight CPUID loop test shows significant speedup:
Before: 18890 cycles
After: 8304 cycles
Quoted 5 lines
Static key is being used to avoid performance penalty for non-Hyper-V deployments.
Signed-off-by Vitaly Kuznetsov <[email protected]>---
Looks good, thanks.
Quoted 11 lines · Vitaly Kuznetsov
Ladi Prosek (1):
x86/kvm: renameHV_X64_MSR_APIC_ASSIST_PAGEtoHV_X64_MSR_VP_ASSIST_PAGEVitaly Kuznetsov (6):
x86/hyper-v: movehyperv.hout of uapi
x86/hyper-v: move definitions from TLFS tohyperv-tlfs.hx86/hyper-v: allocate and use Virtual Processor Assist Pages x86/hyper-v: define structhv_enlightened_vmcsand clean field bits x86/hyper-v: detect nested features
x86/kvm: use Enlightened VMCS when running on Hyper-V
Paolo, I have no hyperv related changes pending for 4.17. Feel free to route it through your tree with my;