KVM: VMX: PERF_GLOBAL_CTRL fixes
Sean Christopherson
maintainercover KVM: VMX: PERF_GLOBAL_CTRL fixes
This is intended as a replacement for commit 00590a384408 ("Revert "KVM:
nVMX: Expose load IA32_PERF_GLOBAL_CTRL VM-{Entry,Exit} control"").
The basic idea is the same, except instead of skipping the load if the desired value is the same, skip the load if the MSR doesn't exist. AFAICT, that plugs all holes where a mischievious usersepace can get KVM to WARN.
I dropped the CR4 changes for now. I'd still prefer to give userspace control, but AFAIK it doesn't cause problems, yet...
Paolo Bonzini (1):
Revert "KVM: nVMX: Expose load IA32_PERF_GLOBAL_CTRL VM-{Entry,Exit}
control"
Sean Christopherson (4):
Revert "Revert "KVM: nVMX: Expose load IA32_PERF_GLOBAL_CTRL
VM-{Entry,Exit} control""
KVM: VMX: Mark all PERF_GLOBAL_(OVF)_CTRL bits reserved if there's no
vPMU
KVM: VMX: Add helper to check if the guest PMU has PERF_GLOBAL_CTRL
KVM: nVMX: Attempt to load PERF_GLOBAL_CTRL on nVMX xfer iff it exists
arch/x86/kvm/vmx/nested.c | 6 +++--- arch/x86/kvm/vmx/pmu_intel.c | 6 ++++-- arch/x86/kvm/vmx/vmx.h | 12 ++++++++++++ 3 files changed, 19 insertions(+), 5 deletions(-)
base-commit: 1a4d88a361af4f2e91861d632c6a1fe87a9665c2
Signature · 1 line
2.37.1.359.gd136c6c3e2-goog
Sean Christopherson
maintainerPATCH 1/5 Revert "Revert "KVM: nVMX: Expose load IA32_PERF_GLOBAL_CTRL VM-{Entry,Exit} control""
This reverts commit 00590a3844086384b584eb9e7c8155baa6e33e49.
Intended to be fixup, not a standalone revert.
--- arch/x86/kvm/vmx/nested.c | 26 +++++++++++++++++++++++--- arch/x86/kvm/vmx/nested.h | 2 ++ arch/x86/kvm/vmx/pmu_intel.c | 3 +++ 3 files changed, 28 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/nested.c +23 -3
@@ -2623,7 +2623,6 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, }if ((vmcs12->vm_entry_controls &VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL ) && - vmcs12->guest_ia32_perf_global_ctrl !=vcpu_to_pmu (vcpu)->global_ctrl &&WARN_ON_ONCE (kvm_set_msr (vcpu,MSR_CORE_PERF_GLOBAL_CTRL , vmcs12->guest_ia32_perf_global_ctrl))) { *entry_failure_code =ENTRY_FAIL_DEFAULT ;
@@ -4334,8 +4333,7 @@ static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,vmcs_write64 (GUEST_IA32_PAT , vmcs12->host_ia32_pat); vcpu->arch.pat = vmcs12->host_ia32_pat; } -if ((vmcs12->vm_exit_controls &VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL ) - && vmcs12->host_ia32_perf_global_ctrl !=vcpu_to_pmu (vcpu)->global_ctrl) +if (vmcs12->vm_exit_controls &VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL )WARN_ON_ONCE (kvm_set_msr (vcpu,MSR_CORE_PERF_GLOBAL_CTRL , vmcs12->host_ia32_perf_global_ctrl));
@@ -4824,6 +4822,28 @@ int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification,return 0 ; } +void nested_vmx_pmu_refresh (struct kvm_vcpu *vcpu, +bool vcpu_has_perf_global_ctrl) +{ +struct vcpu_vmx *vmx; + +if (!nested_vmx_allowed (vcpu)) +return ; + + vmx =to_vmx (vcpu); +if (vcpu_has_perf_global_ctrl) { + vmx->nested.msrs.entry_ctls_high |= +VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL ; + vmx->nested.msrs.exit_ctls_high |= +VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL ; + }else { + vmx->nested.msrs.entry_ctls_high &= + ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL ; + vmx->nested.msrs.exit_ctls_high &= + ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL ; + } +} +static int nested_vmx_get_vmptr (struct kvm_vcpu *vcpu,gpa_t *vmpointer,int *ret) {
arch/x86/kvm/vmx/nested.h +2 -0
@@ -32,6 +32,8 @@ int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);int vmx_get_vmx_msr (struct nested_vmx_msrs *msrs,u32 msr_index,u64 *pdata);int get_vmx_mem_address (struct kvm_vcpu *vcpu,unsigned long exit_qualification,u32 vmx_instruction_info,bool wr,int len,gva_t *ret); +void nested_vmx_pmu_refresh (struct kvm_vcpu *vcpu, +bool vcpu_has_perf_global_ctrl);void nested_mark_vmcs12_pages_dirty (struct kvm_vcpu *vcpu);bool nested_vmx_check_io_bitmaps (struct kvm_vcpu *vcpu,unsigned int port,int size);
arch/x86/kvm/vmx/pmu_intel.c +3 -0
@@ -590,6 +590,9 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)bitmap_set (pmu->all_valid_pmc_idx,INTEL_PMC_MAX_GENERIC , pmu->nr_arch_fixed_counters); +nested_vmx_pmu_refresh (vcpu, +intel_is_valid_msr (vcpu,MSR_CORE_PERF_GLOBAL_CTRL )); +if (cpuid_model_is_consistent (vcpu))x86_perf_get_lbr (&lbr_desc->records);else
Signature · 1 line
2.37.1.359.gd136c6c3e2-goog
Sean Christopherson
maintainerPATCH 2/5 KVM: VMX: Mark all PERF_GLOBAL_(OVF)_CTRL bits reserved if there's no vPMU
In reply to Sean ChristophersonMark all MSR_CORE_PERF_GLOBAL_CTRL and MSR_CORE_PERF_GLOBAL_OVF_CTRL bits as reserved if there is no guest vPMU. The nVMX VM-Entry consistency checks do not check for a valid vPMU prior to consuming the masks via kvm_valid_perf_global_ctrl(), i.e. may incorrectly allow a non-zero mask to be loaded via VM-Enter or VM-Exit (well, attempted to be loaded, the actual MSR load will be rejected by intel_is_valid_msr()).
--- arch/x86/kvm/vmx/pmu_intel.c | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/kvm/vmx/pmu_intel.c +2 -0
@@ -527,6 +527,8 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu) pmu->version =0 ; pmu->reserved_bits =0xffffffff00200000ull ; pmu->raw_event_mask =X86_RAW_EVENT_MASK ; + pmu->global_ctrl_mask = ~0ull ; + pmu->global_ovf_ctrl_mask = ~0ull ; pmu->fixed_ctr_ctrl_mask = ~0ull ; pmu->pebs_enable_mask = ~0ull ; pmu->pebs_data_cfg_mask = ~0ull ;
Signature · 1 line
2.37.1.359.gd136c6c3e2-goog
Sean Christopherson
maintainerPATCH 3/5 KVM: VMX: Add helper to check if the guest PMU has PERF_GLOBAL_CTRL
In reply to Sean ChristophersonAdd a helper to check of the guest PMU has PERF_GLOBAL_CTRL, which is unintuitive _and_ diverges from Intel's architecturally defined behavior. Even worse, KVM currently implements the check using two different (but equivalent) checks, _and_ there has been at least one attempt to add a _third_ flavor.
--- arch/x86/kvm/vmx/pmu_intel.c | 4 ++-- arch/x86/kvm/vmx/vmx.h | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +2 -2
@@ -111,7 +111,7 @@ static bool intel_pmc_is_enabled(struct kvm_pmc *pmc) {struct kvm_pmu *pmu =pmc_to_pmu (pmc); -if (pmu->version <2 ) +if (!intel_pmu_has_perf_global_ctrl (pmu))return true;return test_bit (pmc->idx, (unsigned long *)&pmu->global_ctrl);
@@ -207,7 +207,7 @@ static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)case MSR_CORE_PERF_GLOBAL_STATUS :case MSR_CORE_PERF_GLOBAL_CTRL :case MSR_CORE_PERF_GLOBAL_OVF_CTRL : - ret = pmu->version >1 ; +return intel_pmu_has_perf_global_ctrl (pmu);break ;case MSR_IA32_PEBS_ENABLE : ret =vcpu_get_perf_capabilities (vcpu) &PERF_CAP_PEBS_FORMAT ;
arch/x86/kvm/vmx/vmx.h +12 -0
@@ -91,6 +91,18 @@ union vmx_exit_reason {u32 full; }; +static inline bool intel_pmu_has_perf_global_ctrl (struct kvm_pmu *pmu) +{ +/* +* Architecturally, Intel's SDM states that IA32_PERF_GLOBAL_CTRL is +* supported if "CPUID.0AH: EAX[7:0] > 0", i.e. if the PMU version is +* greater than zero. However, KVM only exposes and emulates the MSR +* to/for the guest if the guest PMU supports at least "Architectural +* Performance Monitoring Version 2". +*/ +return pmu->version >1 ; +} +#define vcpu_to_lbr_desc (vcpu) (&to_vmx(vcpu)->lbr_desc)#define vcpu_to_lbr_records (vcpu) (&to_vmx(vcpu)->lbr_desc.records)
Signature · 1 line
2.37.1.359.gd136c6c3e2-goog
Sean Christopherson
maintainerPATCH 4/5 KVM: nVMX: Attempt to load PERF_GLOBAL_CTRL on nVMX xfer iff it exists
In reply to Sean ChristophersonAttempt to load PERF_GLOBAL_CTRL during nested VM-Enter/VM-Exit if and only if the MSR exists (according to the guest vCPU model). KVM has very misguided handling of VM_{ENTRY,EXIT}_LOAD_IA32_PERF_GLOBAL_CTRL and attempts to force the nVMX MSR settings to match the vPMU model, i.e. to hide/expose the control based on whether or not the MSR exists from the guest's perspective.
KVM's modifications fail to handle the scenario where the vPMU is hidden from the guest _after_ being exposed to the guest, e.g. by userspace doing multiple KVM_SET_CPUID2 calls, which is allowed if done before any KVM_RUN. nested_vmx_pmu_refresh() is called if and only if there's a recognized vPMU, i.e. KVM will leave the bits in the allow state and then ultimately reject the MSR load and WARN.
KVM should not force the VMX MSRs in the first place. KVM taking control of the MSRs was a misguided attempt at mimicking what commit 5f76f6f5ff96 ("KVM: nVMX: Do not expose MPX VMX controls when guest MPX disabled", 2018-10-01) did for MPX. However, the MPX commit was a workaround for another KVM bug and not something that should be imitated (and it should never been done in the first place).
In other words, KVM's ABI _should_ be that userspace has full control over the MSRs, at which point triggering the WARN that loading the MSR must not fail is trivial.
The intent of the WARN is still valid; KVM has consistency checks to ensure that vmcs12->{guest,host}_ia32_perf_global_ctrl is valid. The problem is that '0' must be considered a valid value at all times, and so the simple/obvious solution is to just not actually load the MSR when it does not exist. It is userspace's responsibility to provide a sane vCPU model, i.e. KVM is well within its ABI and Intel's VMX architecture to skip the loads if the MSR does not exist.
--- arch/x86/kvm/vmx/nested.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/nested.c +3 -1
@@ -2623,6 +2623,7 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, }if ((vmcs12->vm_entry_controls &VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL ) && +intel_pmu_has_perf_global_ctrl (vcpu_to_pmu (vcpu)) &&WARN_ON_ONCE (kvm_set_msr (vcpu,MSR_CORE_PERF_GLOBAL_CTRL , vmcs12->guest_ia32_perf_global_ctrl))) { *entry_failure_code =ENTRY_FAIL_DEFAULT ;
@@ -4333,7 +4334,8 @@ static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,vmcs_write64 (GUEST_IA32_PAT , vmcs12->host_ia32_pat); vcpu->arch.pat = vmcs12->host_ia32_pat; } -if (vmcs12->vm_exit_controls &VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL ) +if ((vmcs12->vm_exit_controls &VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL ) && +intel_pmu_has_perf_global_ctrl (vcpu_to_pmu (vcpu)))WARN_ON_ONCE (kvm_set_msr (vcpu,MSR_CORE_PERF_GLOBAL_CTRL , vmcs12->host_ia32_perf_global_ctrl));
Signature · 1 line
2.37.1.359.gd136c6c3e2-goog
Sean Christopherson
maintainerPATCH 5/5 Revert "KVM: nVMX: Expose load IA32_PERF_GLOBAL_CTRL VM-{Entry,Exit} control"
In reply to Sean ChristophersonFrom: Paolo Bonzini <[email protected]>
This reverts commit 03a8871add95213827e2bea84c12133ae5df952e.
Since 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, trying to set these bits in the IA32_VMX_TRUE_{ENTRY,EXIT}_CTLS MSRs if the guest's CPUID supports the architectural PMU (CPUID[EAX=0Ah].EAX[7:0]=1), and clear otherwise.
This was a misguided attempt at mimicking what commit 5f76f6f5ff96 ("KVM: nVMX: Do not expose MPX VMX controls when guest MPX disabled", 2018-10-01) did for MPX. However, that commit was a workaround for another KVM bug and not something that should be imitated. Mucking with the VMX MSRs creates a subtle, difficult to maintain ABI as KVM must ensure that any internal changes, e.g. to how KVM handles _any_ guest CPUID changes, yield the same functional result. Therefore, KVM's policy is to let userspace have full control of the guest vCPU model so long as the host kernel is not at risk.
Now that KVM really truly ensures kvm_set_msr() will succeed by loading PERF_GLOBAL_CTRL if and only if it exists, revert KVM's misguided and roundabout behavior.
--- arch/x86/kvm/vmx/nested.c | 22 ---------------------- arch/x86/kvm/vmx/nested.h | 2 -- arch/x86/kvm/vmx/pmu_intel.c | 3 --- 3 files changed, 27 deletions(-)
arch/x86/kvm/vmx/nested.c +0 -22
@@ -4824,28 +4824,6 @@ int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification,return 0 ; } -void nested_vmx_pmu_refresh (struct kvm_vcpu *vcpu, -bool vcpu_has_perf_global_ctrl) -{ -struct vcpu_vmx *vmx; - -if (!nested_vmx_allowed (vcpu)) -return ; - - vmx =to_vmx (vcpu); -if (vcpu_has_perf_global_ctrl) { - vmx->nested.msrs.entry_ctls_high |= -VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL ; - vmx->nested.msrs.exit_ctls_high |= -VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL ; - }else { - vmx->nested.msrs.entry_ctls_high &= - ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL ; - vmx->nested.msrs.exit_ctls_high &= - ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL ; - } -} -static int nested_vmx_get_vmptr (struct kvm_vcpu *vcpu,gpa_t *vmpointer,int *ret) {
arch/x86/kvm/vmx/nested.h +0 -2
@@ -32,8 +32,6 @@ int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);int vmx_get_vmx_msr (struct nested_vmx_msrs *msrs,u32 msr_index,u64 *pdata);int get_vmx_mem_address (struct kvm_vcpu *vcpu,unsigned long exit_qualification,u32 vmx_instruction_info,bool wr,int len,gva_t *ret); -void nested_vmx_pmu_refresh (struct kvm_vcpu *vcpu, -bool vcpu_has_perf_global_ctrl);void nested_mark_vmcs12_pages_dirty (struct kvm_vcpu *vcpu);bool nested_vmx_check_io_bitmaps (struct kvm_vcpu *vcpu,unsigned int port,int size);
arch/x86/kvm/vmx/pmu_intel.c +0 -3
@@ -592,9 +592,6 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)bitmap_set (pmu->all_valid_pmc_idx,INTEL_PMC_MAX_GENERIC , pmu->nr_arch_fixed_counters); -nested_vmx_pmu_refresh (vcpu, -intel_is_valid_msr (vcpu,MSR_CORE_PERF_GLOBAL_CTRL )); -if (cpuid_model_is_consistent (vcpu))x86_perf_get_lbr (&lbr_desc->records);else
Signature · 1 line
2.37.1.359.gd136c6c3e2-goog
Queued, thanks.
Paolo