Skip to content

Commit 250552b

Browse files
vittyvkbonzini
authored andcommitted
KVM: nVMX: Don't use Enlightened MSR Bitmap for L3
When KVM runs as a nested hypervisor on top of Hyper-V it uses Enlightened VMCS and enables Enlightened MSR Bitmap feature for its L1s and L2s (which are actually L2s and L3s from Hyper-V's perspective). When MSR bitmap is updated, KVM has to reset HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP from clean fields to make Hyper-V aware of the change. For KVM's L1s, this is done in vmx_disable_intercept_for_msr()/vmx_enable_intercept_for_msr(). MSR bitmap for L2 is build in nested_vmx_prepare_msr_bitmap() by blending MSR bitmap for L1 and L1's idea of MSR bitmap for L2. KVM, however, doesn't check if the resulting bitmap is different and never cleans HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP in eVMCS02. This is incorrect and may result in Hyper-V missing the update. The issue could've been solved by calling evmcs_touch_msr_bitmap() for eVMCS02 from nested_vmx_prepare_msr_bitmap() unconditionally but doing so would not give any performance benefits (compared to not using Enlightened MSR Bitmap at all). 3-level nesting is also not a very common setup nowadays. Don't enable 'Enlightened MSR Bitmap' feature for KVM's L2s (real L3s) for now. Signed-off-by: Vitaly Kuznetsov <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 0fcfb00 commit 250552b

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

arch/x86/kvm/vmx/vmx.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2646,15 +2646,6 @@ int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
26462646
if (!loaded_vmcs->msr_bitmap)
26472647
goto out_vmcs;
26482648
memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
2649-
2650-
if (IS_ENABLED(CONFIG_HYPERV) &&
2651-
static_branch_unlikely(&enable_evmcs) &&
2652-
(ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
2653-
struct hv_enlightened_vmcs *evmcs =
2654-
(struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
2655-
2656-
evmcs->hv_enlightenments_control.msr_bitmap = 1;
2657-
}
26582649
}
26592650

26602651
memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
@@ -6842,6 +6833,19 @@ static int vmx_create_vcpu(struct kvm_vcpu *vcpu)
68426833
if (err < 0)
68436834
goto free_pml;
68446835

6836+
/*
6837+
* Use Hyper-V 'Enlightened MSR Bitmap' feature when KVM runs as a
6838+
* nested (L1) hypervisor and Hyper-V in L0 supports it. Enable the
6839+
* feature only for vmcs01, KVM currently isn't equipped to realize any
6840+
* performance benefits from enabling it for vmcs02.
6841+
*/
6842+
if (IS_ENABLED(CONFIG_HYPERV) && static_branch_unlikely(&enable_evmcs) &&
6843+
(ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
6844+
struct hv_enlightened_vmcs *evmcs = (void *)vmx->vmcs01.vmcs;
6845+
6846+
evmcs->hv_enlightenments_control.msr_bitmap = 1;
6847+
}
6848+
68456849
/* The MSR bitmap starts with all ones */
68466850
bitmap_fill(vmx->shadow_msr_intercept.read, MAX_POSSIBLE_PASSTHROUGH_MSRS);
68476851
bitmap_fill(vmx->shadow_msr_intercept.write, MAX_POSSIBLE_PASSTHROUGH_MSRS);

0 commit comments

Comments
 (0)