KVM: SVM: nSVM: Implement Enlightened MSR-Bitmap for Hyper-V-on-KVM
Subsystems (4)
Vitaly Kuznetsov
maintainerv2 cover KVM: SVM: nSVM: Implement Enlightened MSR-Bitmap for Hyper-V-on-KVM
Changes since v1:
- Patches 1/2 from "[PATCH 0/5] KVM: SVM: nSVM: Implement Enlightened MSR-Bitmap for Hyper-V-on-KVM and fix it for KVM-on-Hyper-V" are already merged, dropped.
- Fix build when !CONFIG_HYPERV (PATCH3 "KVM: nSVM: Split off common definitions for Hyper-V on KVM and KVM on Hyper-V" added).
Description:
Enlightened MSR-Bitmap feature implements a PV protocol for L0 and L1 hypervisors to collaborate and skip unneeded updates to MSR-Bitmap. KVM already implements the feature for KVM-on-Hyper-V.
Vitaly Kuznetsov (4):
KVM: nSVM: Track whether changes in L0 require MSR bitmap for L2 to be
rebuilt
KVM: x86: Make kvm_hv_hypercall_enabled() static inline
KVM: nSVM: Split off common definitions for Hyper-V on KVM and KVM on
Hyper-V
KVM: nSVM: Implement Enlightened MSR-Bitmap feature
arch/x86/kvm/hyperv.c | 12 +-------- arch/x86/kvm/hyperv.h | 6 ++++- arch/x86/kvm/svm/hyperv.h | 35 ++++++++++++++++++++++++ arch/x86/kvm/svm/nested.c | 47 ++++++++++++++++++++++++++++----- arch/x86/kvm/svm/svm.c | 3 ++- arch/x86/kvm/svm/svm.h | 11 ++++++++ arch/x86/kvm/svm/svm_onhyperv.h | 25 +----------------- 7 files changed, 95 insertions(+), 44 deletions(-) create mode 100644 arch/x86/kvm/svm/hyperv.h
Signature · 1 line
2.34.1
Vitaly Kuznetsov
maintainerPATCH v2 1/4 KVM: nSVM: Track whether changes in L0 require MSR bitmap for L2 to be rebuilt
Similar to nVMX commit ed2a4800ae9d ("KVM: nVMX: Track whether changes in L0 require MSR bitmap for L2 to be rebuilt"), introduce a flag to keep track of whether MSR bitmap for L2 needs to be rebuilt due to changes in MSR bitmap for L1 or switching to a different L2.
--- arch/x86/kvm/svm/nested.c | 6 ++++++ arch/x86/kvm/svm/svm.c | 3 ++- arch/x86/kvm/svm/svm.h | 9 +++++++++ 3 files changed, 17 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/nested.c +6 -0
@@ -193,6 +193,8 @@ static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm) svm->nested.msrpm[p] = svm->msrpm[p] | value; } + svm->nested.force_msr_bitmap_recalc = false; + svm->vmcb->control.msrpm_base_pa =__sme_set (__pa (svm->nested.msrpm));return true;
@@ -494,6 +496,7 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12if (svm->nested.vmcb12_gpa != svm->nested.last_vmcb12_gpa) { new_vmcb12 = true; svm->nested.last_vmcb12_gpa = svm->nested.vmcb12_gpa; + svm->nested.force_msr_bitmap_recalc = true; }if (unlikely (new_vmcb12 ||vmcb_is_dirty (vmcb12,VMCB_SEG ))) {
@@ -1494,6 +1497,9 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,svm_switch_vmcb (svm, &svm->nested.vmcb02);nested_vmcb02_prepare_control (svm); + + svm->nested.force_msr_bitmap_recalc = true; +kvm_make_request (KVM_REQ_GET_NESTED_STATE_PAGES , vcpu); ret =0 ; out_free:
arch/x86/kvm/svm/svm.c +2 -1
@@ -664,6 +664,7 @@ static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)static void set_msr_interception_bitmap (struct kvm_vcpu *vcpu,u32 *msrpm,u32 msr,int read,int write) { +struct vcpu_svm *svm =to_svm (vcpu);u8 bit_read, bit_write;unsigned long tmp;u32 offset;
@@ -694,7 +695,7 @@ static void set_msr_interception_bitmap(struct kvm_vcpu *vcpu, u32 *msrpm, msrpm[offset] = tmp;svm_hv_vmcb_dirty_nested_enlightenments (vcpu); - + svm->nested.force_msr_bitmap_recalc = true; } void set_msr_interception(struct kvm_vcpu *vcpu, u32 *msrpm, u32 msr,
arch/x86/kvm/svm/svm.h +9 -0
@@ -163,6 +163,15 @@ struct svm_nested_state {struct vmcb_save_area_cached save;bool initialized; + +/* +* Indicates whether MSR bitmap for L2 needs to be rebuilt due to +* changes in MSR bitmap for L1 or switching to a different L2. Note, +* this flag can only be used reliably in conjunction with a paravirt L1 +* which informs L0 whether any changes to MSR bitmap for L2 were done +* on its side. +*/ +bool force_msr_bitmap_recalc; };struct vcpu_sev_es_state {
Signature · 1 line
2.34.1
Vitaly Kuznetsov
maintainerPATCH v2 2/4 KVM: x86: Make kvm_hv_hypercall_enabled() static inline
In reply to Vitaly KuznetsovIn preparation for using kvm_hv_hypercall_enabled() from SVM code, make it static inline to avoid the need to export it. The function is a simple check with only two call sites currently.
--- arch/x86/kvm/hyperv.c | 5 ----- arch/x86/kvm/hyperv.h | 6 +++++- 2 files changed, 5 insertions(+), 6 deletions(-)
arch/x86/kvm/hyperv.c +0 -5
@@ -2017,11 +2017,6 @@ int kvm_hv_set_enforce_cpuid(struct kvm_vcpu *vcpu, bool enforce)return ret; } -bool kvm_hv_hypercall_enabled (struct kvm_vcpu *vcpu) -{ -return vcpu->arch.hyperv_enabled &&to_kvm_hv (vcpu->kvm)->hv_guest_os_id; -} -static void kvm_hv_hypercall_set_result (struct kvm_vcpu *vcpu,u64 result) {bool longmode;
arch/x86/kvm/hyperv.h +5 -1
@@ -89,7 +89,11 @@ static inline u32 kvm_hv_get_vpindex(struct kvm_vcpu *vcpu)int kvm_hv_set_msr_common (struct kvm_vcpu *vcpu,u32 msr,u64 data,bool host);int kvm_hv_get_msr_common (struct kvm_vcpu *vcpu,u32 msr,u64 *pdata,bool host); -bool kvm_hv_hypercall_enabled (struct kvm_vcpu *vcpu); +static inline bool kvm_hv_hypercall_enabled (struct kvm_vcpu *vcpu) +{ +return vcpu->arch.hyperv_enabled &&to_kvm_hv (vcpu->kvm)->hv_guest_os_id; +} +int kvm_hv_hypercall (struct kvm_vcpu *vcpu);void kvm_hv_irq_routing_update (struct kvm *kvm);
Signature · 1 line
2.34.1
Vitaly Kuznetsov
maintainerPATCH v2 3/4 KVM: nSVM: Split off common definitions for Hyper-V on KVM and KVM on Hyper-V
In reply to Vitaly KuznetsovIn preparation to implementing Enlightened MSR-Bitmap feature for Hyper-V on KVM, split off the required definitions into common 'svm/hyperv.h' header.
No functional change intended.
--- arch/x86/kvm/svm/hyperv.h | 35 +++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm_onhyperv.h | 25 +---------------------- 2 files changed, 36 insertions(+), 24 deletions(-) create mode 100644 arch/x86/kvm/svm/hyperv.h
arch/x86/kvm/svm/hyperv.h new file +35 -0
@@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Common Hyper-V on KVM and KVM on Hyper-V definitions (SVM). +*/ + +#ifndef __ARCH_X86_KVM_SVM_HYPERV_H__ +#define __ARCH_X86_KVM_SVM_HYPERV_H__ + +#include <asm/mshyperv.h> + +#include "../hyperv.h" + +/* +* Hyper-V uses the software reserved 32 bytes in VMCB +* control area to expose SVM enlightenments to guests. +*/ +struct hv_enlightenments { + struct __packed hv_enlightenments_control { +u32 nested_flush_hypercall:1 ; +u32 msr_bitmap:1 ; +u32 enlightened_npt_tlb:1 ; +u32 reserved:29 ; + } __packed hv_enlightenments_control; +u32 hv_vp_id; +u64 hv_vm_id; +u64 partition_assist_page; +u64 reserved; +} __packed; + +/* +* Hyper-V uses the software reserved clean bit in VMCB +*/ +#define VMCB_HV_NESTED_ENLIGHTENMENTS VMCB_SW + +#endif /* __ARCH_X86_KVM_SVM_HYPERV_H__ */
arch/x86/kvm/svm/svm_onhyperv.h +1 -24
@@ -7,35 +7,12 @@#define __ARCH_X86_KVM_SVM_ONHYPERV_H__#if IS_ENABLED (CONFIG_HYPERV ) -#include <asm/mshyperv.h> -#include "hyperv.h" #include "kvm_onhyperv.h" +#include "svm/hyperv.h" static struct kvm_x86_ops svm_x86_ops; -/* -* Hyper-V uses the software reserved 32 bytes in VMCB -* control area to expose SVM enlightenments to guests. -*/ -struct hv_enlightenments { - struct __packed hv_enlightenments_control { -u32 nested_flush_hypercall:1 ; -u32 msr_bitmap:1 ; -u32 enlightened_npt_tlb:1 ; -u32 reserved:29 ; - } __packed hv_enlightenments_control; -u32 hv_vp_id; -u64 hv_vm_id; -u64 partition_assist_page; -u64 reserved; -} __packed; - -/* -* Hyper-V uses the software reserved clean bit in VMCB -*/ -#define VMCB_HV_NESTED_ENLIGHTENMENTS VMCB_SW -int svm_hv_enable_direct_tlbflush (struct kvm_vcpu *vcpu);static inline void svm_hv_init_vmcb (struct vmcb *vmcb)
Signature · 1 line
2.34.1
Vitaly Kuznetsov
maintainerPATCH v2 4/4 KVM: nSVM: Implement Enlightened MSR-Bitmap feature
In reply to Vitaly KuznetsovSimilar to nVMX commit 502d2bf5f2fd ("KVM: nVMX: Implement Enlightened MSR Bitmap feature"), add support for the feature for nSVM (Hyper-V on KVM).
Notable differences from nVMX implementation:
- As the feature uses SW reserved fields in VMCB control, KVM needs to make sure it's dealing with a Hyper-V guest (kvm_hv_hypercall_enabled()).
- 'msrpm_base_pa' needs to be always be overwritten in nested_svm_vmrun_msrpm(), even when the update is skipped. As an optimization, nested_vmcb02_prepare_control() copies it from VMCB01 so when MSR-Bitmap feature for L2 is disabled nothing needs to be done.
- 'struct vmcb_ctrl_area_cached' needs to be extended with clean fields/sw reserved data and __nested_copy_vmcb_control_to_cache() needs to copy it so nested_svm_vmrun_msrpm() can use it later.
--- arch/x86/kvm/hyperv.c | 7 +------ arch/x86/kvm/svm/nested.c | 41 ++++++++++++++++++++++++++++++++------- arch/x86/kvm/svm/svm.h | 2 ++ 3 files changed, 37 insertions(+), 13 deletions(-)
arch/x86/kvm/hyperv.c +1 -6
@@ -2412,10 +2412,6 @@ int kvm_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid,if (kvm_x86_ops.nested_ops->get_evmcs_version) evmcs_ver = kvm_x86_ops.nested_ops->get_evmcs_version (vcpu); -/* Skip NESTED_FEATURES if eVMCS is not supported */ -if (!evmcs_ver) - --nent; -if (cpuid->nent < nent)return -E2BIG ;
@@ -2515,8 +2511,7 @@ int kvm_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid,case HYPERV_CPUID_NESTED_FEATURES : ent->eax = evmcs_ver; -if (evmcs_ver) - ent->eax |=HV_X64_NESTED_MSR_BITMAP ; + ent->eax |=HV_X64_NESTED_MSR_BITMAP ;break ;
arch/x86/kvm/svm/nested.c +34 -7
92 lines not on this page · raw message
arch/x86/kvm/svm/svm.h +2 -0
@@ -137,6 +137,8 @@ struct vmcb_ctrl_area_cached {u32 event_inj_err;u64 nested_cr3;u64 virt_ext; +u32 clean; +u8 reserved_sw[32 ]; };struct svm_nested_state {
Signature · 1 line
2.34.1
Quoted 29 lines · Vitaly Kuznetsov
Changes since v1:
- Patches 1/2 from "[PATCH 0/5] KVM: SVM: nSVM: Implement Enlightened MSR-Bitmap for Hyper-V-on-KVM and fix it for KVM-on-Hyper-V" are already merged, dropped.
- Fix build when !CONFIG_HYPERV (PATCH3 "KVM: nSVM: Split off common definitions for Hyper-V on KVM and KVM on Hyper-V" added).Description:
Enlightened MSR-Bitmap feature implements a PV protocol for L0 and L1 hypervisors to collaborate and skip unneeded updates to MSR-Bitmap. KVM already implements the feature for KVM-on-Hyper-V.
Vitaly Kuznetsov (4):
KVM: nSVM: Track whether changes in L0 require MSR bitmap for L2 to be
rebuilt
KVM: x86: Makekvm_hv_hypercall_enabled()static inline
KVM: nSVM: Split off common definitions for Hyper-V on KVM and KVM on
Hyper-V
KVM: nSVM: Implement Enlightened MSR-Bitmap featurearch/x86/kvm/hyperv.c | 12 +-------- arch/x86/kvm/hyperv.h | 6 ++++- arch/x86/kvm/svm/hyperv.h | 35 ++++++++++++++++++++++++ arch/x86/kvm/svm/nested.c | 47 ++++++++++++++++++++++++++++----- arch/x86/kvm/svm/svm.c | 3 ++- arch/x86/kvm/svm/svm.h | 11 ++++++++ arch/x86/kvm/svm/svm_onhyperv.h | 25 +----------------- 7 files changed, 95 insertions(+), 44 deletions(-)
Quoted 2 lines
create mode 100644
arch/x86/kvm/svm/hyperv.h
Queued, thanks.
Paolo
Vitaly Kuznetsov
maintainerQuoted 1 line · Paolo Bonzini
On 2/2/22 10:50, Vitaly Kuznetsov wrote:
...
Quoted 1 line
Quoted 1 line
KVM: nSVM: Implement Enlightened MSR-Bitmap feature
...
Quoted 2 lines
Queued, thanks.
While writing a selftest for the feature, I've discovered an embarassing
bug: in nested_svm_vmrun_msrpm() I check bit number instead of bit mask. It worked in testing by pure luck, it seems genuine Hyper-V sets some other clean fields simultaneously.
Would it be possible to squash the attached patch in? Thanks!
I'll be sending out selftests shortly.
Signature · 1 line
Vitaly
From cfb538876ccc59dade7cadde553863bea8312f90 Mon Sep 17 00:00:00 2001
From: Vitaly Kuznetsov <[email protected]>
Date: Thu, 3 Feb 2022 10:22:30 +0100
Subject: [PATCH] KVM: nSVM: Fix the check for VMCB_HV_NESTED_ENLIGHTENMENTS
bin in nested_svm_vmrun_msrpm()
VMCB_HV_NESTED_ENLIGHTENMENTS (VMCB_SW) is a bit number, not a bit mask.
--- arch/x86/kvm/svm/nested.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/svm/nested.c +1 -1
@@ -188,7 +188,7 @@ static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)if (!svm->nested.force_msr_bitmap_recalc &&kvm_hv_hypercall_enabled (&svm->vcpu) && hve->hv_enlightenments_control.msr_bitmap && - (svm->nested.ctl.clean &VMCB_HV_NESTED_ENLIGHTENMENTS )) + (svm->nested.ctl.clean &BIT (VMCB_HV_NESTED_ENLIGHTENMENTS ))) goto set_msrpm_base_pa;if (!(vmcb12_is_intercept (&svm->nested.ctl,INTERCEPT_MSR_PROT )))
Signature · 1 line
2.34.1
Paolo Bonzini
maintainerQuoted 1 line · Vitaly Kuznetsov
Would it be possible to squash the attached patch in? Thanks!
Yes, of course. Tests FTW. :)
Paolo