KVM: nVMX: VMX control MSR fixes
v3 cover KVM: nVMX: VMX control MSR fixes
There are a few bits in the VMX entry/exit control MSRs where KVM intervenes. The "load IA32_PERF_GLOBAL_CTRL" and "{load,clear} IA32_BNDCFGS" VMX entry/exit control bits are under KVM control and conditionally exposed based on the guest CPUID. If the guest CPUID provides a supporting vPMU or MPX, the respective VMX control bits are enabled.
These rules have not been upheld in all cases, though. Since commit aedbaf4f6afd ("KVM: x86: Extract kvm_update_cpuid_runtime() from kvm_update_cpuid()") KVM will only apply its updates to the MSRs when the guest CPUID is set. Before, KVM called kvm_update_cpuid() frequently when running a guest, which had the effect of overriding any userspace setting of these MSRs.
If an unsuspecting VMM writes to these VMX control MSRs after the CPUID has been set, KVM fails to configure the appropriate bits. There does not exist any ordering requirements between setting CPUID and writing to an MSR.
At the same time, we probably want to get KVM out of the business of fiddling with these control MSRs. This series adds a quirk that allows userspace to opt-out of KVM tweaks to these MSRs.
[Patch 1-2]
Fix the immediate issue by hooking writes to the VMX control MSRs. If userspace writes to one of the affected MSRs, reapply KVMs tweaks to these registers. Note that these patches employ the minimal change required to fix the issue, in case they are worthy of a backport.
[Patch 3]
Add a quirk allowing sane VMMs to take full ownership of these control MSR bits, when disabled.
[Patch 4-6]
Add tests to verify correct behavior with the quirk enabled (KVM control) and quirk disabled (userspace control).
Applies cleanly to 5.17-rc5. Tested on a Skylake machine with the included selftest.
v2: https://patchwork.kernel.org/project/kvm/cover/[email protected]/
v2 -> v3:
- Fix changelog and blamed commit in patches 1-2 to better capture the history and subsequent breakage of ABI (Sean)
- Skip consolidation of PMU/MPX control MSR updates into a single helper. Sean has some cleanups that are preferrable.
- Add test cases for both MPX and PGC that assert KVM clears the respective bits when the feature dependencies are not present in guest CPUID (Paolo)
Oliver Upton (6):
KVM: nVMX: Keep KVM updates to BNDCFGS ctrl bits across MSR write
KVM: nVMX: Keep KVM updates to PERF_GLOBAL_CTRL ctrl bits across MSR
write
KVM: nVMX: Add a quirk for KVM tweaks to VMX control MSRs
selftests: KVM: Separate static alloc from KVM_GET_SUPPORTED_CPUID
call
selftests: KVM: Add test for PERF_GLOBAL_CTRL VMX control MSR bits
selftests: KVM: Add test for BNDCFGS VMX control MSR bits
arch/x86/include/uapi/asm/kvm.h | 11 +- arch/x86/kvm/vmx/nested.c | 12 + arch/x86/kvm/vmx/vmx.c | 7 +- arch/x86/kvm/vmx/vmx.h | 2 + tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/include/x86_64/processor.h | 1 + .../selftests/kvm/include/x86_64/vmx.h | 2 + .../selftests/kvm/lib/x86_64/processor.c | 33 ++- .../kvm/x86_64/vmx_control_msrs_test.c | 232 ++++++++++++++++++ 10 files changed, 290 insertions(+), 12 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86_64/vmx_control_msrs_test.c
Signature · 1 line
2.35.1.574.g5d30c73bfb-goog
PATCH v3 1/6 KVM: nVMX: Keep KVM updates to BNDCFGS ctrl bits across MSR write
Since commit 5f76f6f5ff96 ("KVM: nVMX: Do not expose MPX VMX controls when guest MPX disabled"), KVM has taken ownership of the "load IA32_BNDCFGS" and "clear IA32_BNDCFGS" VMX entry/exit controls. The ABI is that these bits must be set in the IA32_VMX_TRUE_{ENTRY,EXIT}_CTLS MSRs if the guest's CPUID supports MPX, and clear otherwise.
However, commit aedbaf4f6afd ("KVM: x86: Extract kvm_update_cpuid_runtime() from kvm_update_cpuid()") partially broke KVM ownership of the aforementioned bits. Before, kvm_update_cpuid() was exercised frequently when running a guest and constantly applied its own changes to the BNDCFGS bits. Now, the BNDCFGS bits are only ever updated after a KVM_SET_CPUID/KVM_SET_CPUID2 ioctl, meaning that a subsequent MSR write from userspace will clobber these values.
Uphold the old ABI by reapplying KVM's tweaks to the BNDCFGS bits after an MSR write from userspace.
--- arch/x86/kvm/vmx/nested.c | 9 +++++++++ arch/x86/kvm/vmx/vmx.c | 2 +- arch/x86/kvm/vmx/vmx.h | 2 ++ 3 files changed, 12 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/nested.c +9 -0
@@ -1291,6 +1291,15 @@ vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) *lowp = data; *highp = data >>32 ; + +/* +* Ensure KVM fiddling with these MSRs is preserved after userspace +* write. +*/ +if (msr_index ==MSR_IA32_VMX_TRUE_ENTRY_CTLS || + msr_index ==MSR_IA32_VMX_TRUE_EXIT_CTLS ) +nested_vmx_entry_exit_ctls_update (&vmx->vcpu); +return 0 ; }
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -7242,7 +7242,7 @@ static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)#undef cr4_fixed1_update } -static void nested_vmx_entry_exit_ctls_update (struct kvm_vcpu *vcpu) +void nested_vmx_entry_exit_ctls_update (struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu);
arch/x86/kvm/vmx/vmx.h +2 -0
@@ -423,6 +423,8 @@ static inline void vmx_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr,void vmx_update_cpu_dirty_logging (struct kvm_vcpu *vcpu); +void nested_vmx_entry_exit_ctls_update (struct kvm_vcpu *vcpu); +/* * Note, early Intel manuals have the write-low and read-high bitmap offsets * the wrong way round. The bitmaps control MSRs 0x00000000-0x00001fff and
Signature · 1 line
2.35.1.574.g5d30c73bfb-goog
PATCH v3 2/6 KVM: nVMX: Keep KVM updates to PERF_GLOBAL_CTRL ctrl bits across MSR write
In reply to Oliver UptonSince commit 03a8871add95 ("KVM: nVMX: Expose load IA32_PERF_GLOBAL_CTRL VM-{Entry,Exit} control"), KVM has taken ownership of the "load IA32_PERF_GLOBAL_CTRL" VMX entry/exit control bits. The ABI is that these bits will be set in the IA32_VMX_TRUE_{ENTRY,EXIT}_CTLS MSRs if the guest's CPUID exposes a vPMU that supports the IA32_PERF_GLOBAL_CTRL MSR (CPUID.0AH:EAX[7:0] > 1), and clear otherwise.
However, KVM will only do so if userspace sets the CPUID before writing to the corresponding MSRs. Of course, there are no ordering requirements between these ioctls. Uphold the ABI regardless of ordering by reapplying KVMs tweaks to the VMX control MSRs after userspace has written to them.
Note that older kernels without commit c44d9b34701d ("KVM: x86: Invoke vendor's vcpu_after_set_cpuid() after all common updates") still require that the entry/exit controls be updated from kvm_pmu_refresh(). Leave the benign call in place to allow for cleaner backporting and punt the cleanup to a later change.
Uphold the old ABI by reapplying KVM's tweaks to the BNDCFGS bits after an MSR write from userspace.
--- arch/x86/kvm/vmx/vmx.c | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/kvm/vmx/vmx.c +2 -0
@@ -7257,6 +7257,8 @@ void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu) vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS ; } } + +nested_vmx_pmu_entry_exit_ctls_update (vcpu); }static void update_intel_pt_cfg (struct kvm_vcpu *vcpu)
Signature · 1 line
2.35.1.574.g5d30c73bfb-goog
Re: [PATCH v3 2/6] KVM: nVMX: Keep KVM updates to PERF_GLOBAL_CTRL ctrl bits across MSR write
Quoted 13 lines · Oliver Upton
Since commit
03a8871add95("KVM: nVMX: Expose loadIA32_PERF_GLOBAL_CTRLVM-{Entry,Exit} control"), KVM has taken ownership of the "loadIA32_PERF_GLOBAL_CTRL" VMX entry/exit control bits. The ABI is that these bits will be set in the IA32_VMX_TRUE_{ENTRY,EXIT}_CTLS MSRs if the guest's CPUID exposes a vPMU that supports theIA32_PERF_GLOBAL_CTRLMSR (CPUID.0AH:EAX[7:0] > 1), and clear otherwise.However, KVM will only do so if userspace sets the CPUID before writing to the corresponding MSRs. Of course, there are no ordering requirements between these ioctls. Uphold the ABI regardless of ordering by reapplying KVMs tweaks to the VMX control MSRs after userspace has written to them.
Quoted 9 lines
Note that older kernels without commit
c44d9b34701d("KVM: x86: Invoke vendor'svcpu_after_set_cpuid()after all common updates") still require that the entry/exit controls be updated fromkvm_pmu_refresh(). Leave the benign call in place to allow for cleaner backporting and punt the cleanup to a later change.Uphold the old ABI by reapplying KVM's tweaks to the BNDCFGS bits after an MSR write from userspace.
typo: s/BNDCFGS/"load IA32_PERF_GLOBAL_CTRL"/
I'll adopt this if I need to send another spin of the series. Otherwise, do you mind fixing the typo when applying Paolo?
--
Oliver
PATCH v3 3/6 KVM: nVMX: Add a quirk for KVM tweaks to VMX control MSRs
In reply to Oliver UptonKVM really has no business messing with the vCPU state. Nonetheless, it has become ABI for KVM to adjust certain bits of the VMX entry/exit control MSRs depending on the guest CPUID. Namely, the bits associated with the IA32_PERF_GLOBAL_CTRL and IA32_BNDCFGS MSRs were conditionally enabled if the guest CPUID allows for it.
Allow userspace to opt-out of changes to VMX control MSRs by adding a new KVM quirk.
--- arch/x86/include/uapi/asm/kvm.h | 11 ++++++----- arch/x86/kvm/vmx/nested.c | 3 +++ arch/x86/kvm/vmx/vmx.c | 3 +++ 3 files changed, 12 insertions(+), 5 deletions(-)
arch/x86/include/uapi/asm/kvm.h +6 -5
@@ -428,11 +428,12 @@ struct kvm_sync_regs {struct kvm_vcpu_events events; }; -#define KVM_X86_QUIRK_LINT0_REENABLED (1 << 0) -#define KVM_X86_QUIRK_CD_NW_CLEARED (1 << 1) -#define KVM_X86_QUIRK_LAPIC_MMIO_HOLE (1 << 2) -#define KVM_X86_QUIRK_OUT_7E_INC_RIP (1 << 3) -#define KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT (1 << 4) +#define KVM_X86_QUIRK_LINT0_REENABLED (1 << 0) +#define KVM_X86_QUIRK_CD_NW_CLEARED (1 << 1) +#define KVM_X86_QUIRK_LAPIC_MMIO_HOLE (1 << 2) +#define KVM_X86_QUIRK_OUT_7E_INC_RIP (1 << 3) +#define KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT (1 << 4) +#define KVM_X86_QUIRK_TWEAK_VMX_CTRL_MSRS (1 << 5)#define KVM_STATE_NESTED_FORMAT_VMX 0#define KVM_STATE_NESTED_FORMAT_SVM 1
arch/x86/kvm/vmx/nested.c +3 -0
@@ -4813,6 +4813,9 @@ void nested_vmx_pmu_entry_exit_ctls_update(struct kvm_vcpu *vcpu)if (!nested_vmx_allowed (vcpu))return ; +if (!kvm_check_has_quirk (vcpu->kvm,KVM_X86_QUIRK_TWEAK_VMX_CTRL_MSRS )) +return ; + vmx =to_vmx (vcpu);if (kvm_x86_ops.pmu_ops->is_valid_msr (vcpu,MSR_CORE_PERF_GLOBAL_CTRL )) { vmx->nested.msrs.entry_ctls_high |=
arch/x86/kvm/vmx/vmx.c +3 -0
@@ -7246,6 +7246,9 @@ void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu); +if (!kvm_check_has_quirk (vcpu->kvm,KVM_X86_QUIRK_TWEAK_VMX_CTRL_MSRS )) +return ; +if (kvm_mpx_supported ()) {bool mpx_enabled =guest_cpuid_has (vcpu,X86_FEATURE_MPX );
Signature · 1 line
2.35.1.574.g5d30c73bfb-goog
PATCH v3 4/6 selftests: KVM: Separate static alloc from KVM_GET_SUPPORTED_CPUID call
In reply to Oliver UptonThe library code allows for a single allocation of CPUID to store the value returned by KVM_GET_SUPPORTED_CPUID. Subsequent calls to the helper simply return a pointer to the aforementioned allocation. A subsequent change introduces a selftest that contains test cases which adjust the CPUID value before calling KVM_SET_CPUID2. Using a single definition of CPUID is problematic, as the changes are not isolated to a single test case.
Create a helper that allocates memory for CPUID on a per-call basis.
--- .../selftests/kvm/include/x86_64/processor.h | 1 + .../selftests/kvm/lib/x86_64/processor.c | 33 +++++++++++++++---- 2 files changed, 28 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +1 -0
@@ -390,6 +390,7 @@ void kvm_x86_state_cleanup(struct kvm_x86_state *state);struct kvm_msr_list *kvm_get_msr_index_list (void );uint64_t kvm_get_feature_msr (uint64_t msr_index); +struct kvm_cpuid2 *_kvm_get_supported_cpuid (void );struct kvm_cpuid2 *kvm_get_supported_cpuid (void );struct kvm_cpuid2 *vcpu_get_cpuid (struct kvm_vm *vm,uint32_t vcpuid);
tools/testing/selftests/kvm/lib/x86_64/processor.c +27 -6
@@ -772,17 +772,14 @@ static struct kvm_cpuid2 *allocate_kvm_cpuid2(void)* * Return: The supported KVM CPUID * -* Get the guest CPUID supported by KVM. +* Gets the supported guest CPUID with the KVM_GET_SUPPORTED_CPUID ioctl. */ -struct kvm_cpuid2 *kvm_get_supported_cpuid (void ) +struct kvm_cpuid2 *_kvm_get_supported_cpuid (void ) { -static struct kvm_cpuid2 *cpuid; +struct kvm_cpuid2 *cpuid;int ret;int kvm_fd; -if (cpuid) -return cpuid; - cpuid =allocate_kvm_cpuid2 (); kvm_fd =open_kvm_dev_path_or_exit ();
@@ -794,6 +791,30 @@ struct kvm_cpuid2 *kvm_get_supported_cpuid(void)return cpuid; } +/* +* KVM Supported CPUID Get +* +* Input Args: None +* +* Output Args: None +* +* Return: The supported KVM CPUID +* +* Gets the supported guest CPUID with the KVM_GET_SUPPORTED_CPUID ioctl. +* The first call creates a static allocation of CPUID for the process. +* Subsequent calls will return a pointer to the previously allocated CPUID. +*/ +struct kvm_cpuid2 *kvm_get_supported_cpuid (void ) +{ +static struct kvm_cpuid2 *cpuid; + +if (cpuid) +return cpuid; + + cpuid =_kvm_get_supported_cpuid (); +return cpuid; +} +/* * KVM Get MSR *
Signature · 1 line
2.35.1.574.g5d30c73bfb-goog
PATCH v3 5/6 selftests: KVM: Add test for PERF_GLOBAL_CTRL VMX control MSR bits
In reply to Oliver UptonTest that the default behavior of KVM is to ignore userspace MSR writes and conditionally expose the "load IA32_PERF_GLOBAL_CTRL" bits in the VMX control MSRs if the guest CPUID exposes a supporting vPMU. Additionally, test that when the corresponding quirk is disabled, userspace can still clear these bits regardless of what is exposed in CPUID.
--- tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/vmx_control_msrs_test.c | 145 ++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/vmx_control_msrs_test.c
tools/testing/selftests/kvm/.gitignore +1 -0
@@ -36,6 +36,7 @@ /x86_64/userspace_io_test /x86_64/userspace_msr_exit_test /x86_64/vmx_apic_access_test /x86_64/vmx_close_while_nested_test +/x86_64/vmx_control_msrs_test /x86_64/vmx_dirty_log_test /x86_64/vmx_exception_with_invalid_guest_state
tools/testing/selftests/kvm/Makefile +1 -0
@@ -68,6 +68,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/sync_regs_test TEST_GEN_PROGS_x86_64 += x86_64/userspace_io_test TEST_GEN_PROGS_x86_64 += x86_64/userspace_msr_exit_test TEST_GEN_PROGS_x86_64 += x86_64/vmx_apic_access_test TEST_GEN_PROGS_x86_64 += x86_64/vmx_close_while_nested_test +TEST_GEN_PROGS_x86_64 += x86_64/vmx_control_msrs_test TEST_GEN_PROGS_x86_64 += x86_64/vmx_dirty_log_test TEST_GEN_PROGS_x86_64 += x86_64/vmx_exception_with_invalid_guest_state
tools/testing/selftests/kvm/x86_64/vmx_control_msrs_test.c new file +145 -0
145 lines not on this page · raw message
Signature · 1 line
2.35.1.574.g5d30c73bfb-goog
PATCH v3 6/6 selftests: KVM: Add test for BNDCFGS VMX control MSR bits
In reply to Oliver UptonTest that the default behavior of KVM is to ignore userspace MSR writes and conditionally expose the "{load,clear} IA32_BNDCFGS" bits in the VMX control MSRs if the guest CPUID exposes MPX. Additionally, test that when the corresponding quirk is disabled, userspace can still clear these bits regardless of what is exposed in CPUID.
--- .../selftests/kvm/include/x86_64/vmx.h | 2 + .../kvm/x86_64/vmx_control_msrs_test.c | 87 +++++++++++++++++++ 2 files changed, 89 insertions(+)
tools/testing/selftests/kvm/include/x86_64/vmx.h +2 -0
@@ -80,6 +80,7 @@#define VM_EXIT_SAVE_IA32_EFER 0x00100000#define VM_EXIT_LOAD_IA32_EFER 0x00200000#define VM_EXIT_SAVE_VMX_PREEMPTION_TIMER 0x00400000 +#define VM_EXIT_CLEAR_BNDCFGS 0x00800000#define VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR 0x00036dff
@@ -90,6 +91,7 @@#define VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL 0x00002000#define VM_ENTRY_LOAD_IA32_PAT 0x00004000#define VM_ENTRY_LOAD_IA32_EFER 0x00008000 +#define VM_ENTRY_LOAD_BNDCFGS 0x00010000#define VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR 0x000011ff
tools/testing/selftests/kvm/x86_64/vmx_control_msrs_test.c +87 -0
99 lines not on this page · raw message
Signature · 1 line
2.35.1.574.g5d30c73bfb-goog