KVM: x86/pmu: Accept 0 for absent PMU MSRs when host-initiated if !enable_pmu
PATCH 1/3 KVM: x86/pmu: Accept 0 for absent PMU MSRs when host-initiated if !enable_pmu
From: Like Xu <[email protected]>
Whenever an MSR is part of KVM_GET_MSR_INDEX_LIST, as is the case for MSR_K7_EVNTSEL0 or MSR_F15H_PERF_CTL0, it has to be always retrievable and settable with KVM_GET_MSR and KVM_SET_MSR.
Accept a zero value for these MSRs to obey the contract.
---
Note, if !enable_pmu, it is easy to reproduce and verify it with selftest.
arch/x86/kvm/pmu.c | 8 ++++++++ arch/x86/kvm/svm/pmu.c | 11 ++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-)
arch/x86/kvm/pmu.c +8 -0
@@ -439,11 +439,19 @@ static void kvm_pmu_mark_pmc_in_use(struct kvm_vcpu *vcpu, u32 msr)int kvm_pmu_get_msr (struct kvm_vcpu *vcpu,struct msr_data *msr_info) { +if (msr_info->host_initiated && !vcpu->kvm->arch.enable_pmu) { + msr_info->data =0 ; +return 0 ; + } +return static_call (kvm_x86_pmu_get_msr)(vcpu, msr_info); }int kvm_pmu_set_msr (struct kvm_vcpu *vcpu,struct msr_data *msr_info) { +if (msr_info->host_initiated && !vcpu->kvm->arch.enable_pmu) +return !!msr_info->data; +kvm_pmu_mark_pmc_in_use (vcpu, msr_info->index);return static_call (kvm_x86_pmu_set_msr)(vcpu, msr_info); }
arch/x86/kvm/svm/pmu.c +10 -1
@@ -182,7 +182,16 @@ static struct kvm_pmc *amd_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,static bool amd_is_valid_msr (struct kvm_vcpu *vcpu,u32 msr,bool host_initiated) {/* All MSRs refer to exactly one PMC, so msr_idx_to_pmc is enough. */ -return false; +if (!host_initiated) +return false; + +switch (msr) { + case MSR_K7_EVNTSEL0 ... MSR_K7_PERFCTR3: + case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5: +return true; +default : +return false; + } }static struct kvm_pmc *amd_msr_idx_to_pmc (struct kvm_vcpu *vcpu,u32 msr)
Signature · 1 line
2.36.1
PATCH 2/3 KVM: x86/pmu: Restrict advanced features based on module enable_pmu
From: Like Xu <[email protected]>
Once vPMU is disabled, the KVM would not expose features like:
PEBS (via clear kvm_pmu_cap.pebs_ept), legacy LBR and ARCH_LBR, CPUID 0xA leaf, PDCM bit and MSR_IA32_PERF_CAPABILITIES, plus PT_MODE_HOST_GUEST mode.
What these associative features have in common is that their use relies on the underlying PMU counter and the host perf_event as a back-end resource requester or sharing part of the irq delivery path.
---
Follow up: a pmu_disable kvm-unit-test will be proposed later.
arch/x86/kvm/pmu.h | 6 ++++-- arch/x86/kvm/vmx/capabilities.h | 6 +++++- arch/x86/kvm/vmx/vmx.c | 7 +++++-- 3 files changed, 14 insertions(+), 5 deletions(-)
arch/x86/kvm/pmu.h +4 -2
@@ -167,9 +167,11 @@ static inline void kvm_init_pmu_capability(void)* For Intel, only support guest architectural pmu * on a host with architectural pmu. */ -if ((is_intel && !kvm_pmu_cap.version) || !kvm_pmu_cap.num_counters_gp) { -memset (&kvm_pmu_cap,0 ,sizeof (kvm_pmu_cap)); +if ((is_intel && !kvm_pmu_cap.version) || !kvm_pmu_cap.num_counters_gp) enable_pmu = false; + +if (!enable_pmu) { +memset (&kvm_pmu_cap,0 ,sizeof (kvm_pmu_cap));return ; }
arch/x86/kvm/vmx/capabilities.h +5 -1
@@ -7,6 +7,7 @@#include "lapic.h" #include "x86.h" #include "pmu.h" +#include "cpuid.h" extern bool __read_mostly enable_vpid;extern bool __read_mostly flexpriority_enabled;
@@ -415,6 +416,9 @@ static inline u64 vmx_get_perf_capabilities(void)u64 perf_cap =PMU_CAP_FW_WRITES ;u64 host_perf_cap =0 ; +if (!enable_pmu) +return 0 ; +if (boot_cpu_has (X86_FEATURE_PDCM ))rdmsrl (MSR_IA32_PERF_CAPABILITIES , host_perf_cap);
@@ -426,7 +430,7 @@ static inline u64 vmx_get_perf_capabilities(void) perf_cap &= ~PERF_CAP_PEBS_BASELINE ; } -if (boot_cpu_has (X86_FEATURE_ARCH_LBR ) && !cpu_has_vmx_arch_lbr ()) +if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR ) && !cpu_has_vmx_arch_lbr ()) perf_cap &= ~PMU_CAP_LBR_FMT ;return perf_cap;
arch/x86/kvm/vmx/vmx.c +5 -2
@@ -7575,11 +7575,14 @@ static __init void vmx_set_cpu_caps(void)kvm_cpu_cap_check_and_set (X86_FEATURE_DS );kvm_cpu_cap_check_and_set (X86_FEATURE_DTES64 ); } -if (!cpu_has_vmx_arch_lbr ()) { +if (!enable_pmu || !cpu_has_vmx_arch_lbr ()) {kvm_cpu_cap_clear (X86_FEATURE_ARCH_LBR ); supported_xss &= ~XFEATURE_MASK_LBR ; } +if (!enable_pmu) +kvm_cpu_cap_clear (X86_FEATURE_PDCM ); +if (!enable_sgx) {kvm_cpu_cap_clear (X86_FEATURE_SGX );kvm_cpu_cap_clear (X86_FEATURE_SGX_LC );
@@ -8269,7 +8272,7 @@ static __init int hardware_setup(void)if (pt_mode !=PT_MODE_SYSTEM && pt_mode !=PT_MODE_HOST_GUEST )return -EINVAL ; -if (!enable_ept || !cpu_has_vmx_intel_pt ()) +if (!enable_ept || !enable_pmu || !cpu_has_vmx_intel_pt ()) pt_mode =PT_MODE_SYSTEM ;if (pt_mode ==PT_MODE_HOST_GUEST ) vmx_init_ops.handle_intel_pt_intr = vmx_handle_intel_pt_intr;
Signature · 1 line
2.36.1
PATCH 3/3 KVM: x86/pmu: Avoid exposing Intel BTS feature
In reply to Like XuFrom: Like Xu <[email protected]>
The BTS feature (including the ability to set the BTS and BTINT bits in the DEBUGCTL MSR) is currently unsupported on KVM.
But we may try using the BTS facility on a PEBS enabled guest like this:
perf record -e branches:u -c 1 -d ls
and then we would encounter the following call trace:
[] unchecked MSR access error: WRMSR to 0x1d9 (tried to write 0x00000000000003c0)
at rIP: 0xffffffff810745e4 (native_write_msr+0x4/0x20) [] Call Trace: [] intel_pmu_enable_bts+0x5d/0x70 [] bts_event_add+0x54/0x70 [] event_sched_in+0xee/0x290
As it lacks any CPUID indicator or perf_capabilities valid bit fields to prompt for this information, the platform would hint the Intel BTS feature unavailable to guest by setting the BTS_UNAVAIL bit in the IA32_MISC_ENABLE.
--- arch/x86/kvm/pmu.h | 3 +++ arch/x86/kvm/vmx/pmu_intel.c | 4 +++- arch/x86/kvm/x86.c | 6 +++--- 3 files changed, 9 insertions(+), 4 deletions(-)
arch/x86/kvm/pmu.h +3 -0
@@ -8,6 +8,9 @@#define pmu_to_vcpu (pmu) (container_of((pmu), struct kvm_vcpu, arch.pmu))#define pmc_to_pmu (pmc) (&(pmc)->vcpu->arch.pmu) +#define MSR_IA32_MISC_ENABLE_PMU_RO_MASK (MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL | \ + MSR_IA32_MISC_ENABLE_BTS_UNAVAIL) +/* retrieve the 4 bits for EN and PMI out of IA32_FIXED_CTR_CTRL */ #define fixed_ctrl_field (ctrl_reg, idx) (((ctrl_reg) >> ((idx)*4)) & 0xf)
arch/x86/kvm/vmx/pmu_intel.c +3 -1
@@ -634,6 +634,9 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu) pmu->pebs_enable_mask = ~0ull ; pmu->pebs_data_cfg_mask = ~0ull ; + vcpu->arch.ia32_misc_enable_msr |= (MSR_IA32_MISC_ENABLE_BTS_UNAVAIL | +MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL ); + entry =kvm_find_cpuid_entry (vcpu,0xa ,0 );if (!entry || !vcpu->kvm->arch.enable_pmu)return ;
@@ -725,7 +728,6 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu) ~((1ull << pmu->nr_arch_gp_counters) -1 ); } }else { - vcpu->arch.ia32_misc_enable_msr |=MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL ; vcpu->arch.perf_capabilities &= ~PERF_CAP_PEBS_MASK ; }
arch/x86/kvm/x86.c +3 -3
@@ -3565,12 +3565,12 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)break ;case MSR_IA32_MISC_ENABLE : {u64 old_val = vcpu->arch.ia32_misc_enable_msr; -u64 pmu_mask =MSR_IA32_MISC_ENABLE_EMON | -MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL ; +u64 pmu_mask =MSR_IA32_MISC_ENABLE_PMU_RO_MASK | +MSR_IA32_MISC_ENABLE_EMON ;/* RO bits */ if (!msr_info->host_initiated && - ((old_val ^ data) &MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL )) + ((old_val ^ data) &MSR_IA32_MISC_ENABLE_PMU_RO_MASK ))return 1 ;/*
Signature · 1 line
2.36.1
Quoted 52 lines · Like Xu
From: Like Xu <[email protected]>
Whenever an MSR is part of
KVM_GET_MSR_INDEX_LIST, as is the case forMSR_K7_EVNTSEL0orMSR_F15H_PERF_CTL0, it has to be always retrievable and settable withKVM_GET_MSRandKVM_SET_MSR.Accept a zero value for these MSRs to obey the contract.
Signed-off-by Like Xu <[email protected]>---
Note, if !enable_pmu, it is easy to reproduce and verify it with selftest.arch/x86/kvm/pmu.c | 8 ++++++++ arch/x86/kvm/svm/pmu.c | 11 ++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-)arch/x86/kvm/pmu.c +8
-0@@ -439,11 +439,19 @@ static void kvm_pmu_mark_pmc_in_use(struct kvm_vcpu *vcpu, u32 msr)int kvm_pmu_get_msr (struct kvm_vcpu *vcpu,struct msr_data *msr_info) { +if (msr_info->host_initiated && !vcpu->kvm->arch.enable_pmu) { + msr_info->data =0 ; +return 0 ; + } +return static_call (kvm_x86_pmu_get_msr)(vcpu, msr_info); }int kvm_pmu_set_msr (struct kvm_vcpu *vcpu,struct msr_data *msr_info) { +if (msr_info->host_initiated && !vcpu->kvm->arch.enable_pmu) +return !!msr_info->data; +kvm_pmu_mark_pmc_in_use (vcpu, msr_info->index);return static_call (kvm_x86_pmu_set_msr)(vcpu, msr_info); }arch/x86/kvm/svm/pmu.c +3
-1@@ -182,7 +182,16 @@ static struct kvm_pmc *amd_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,static bool amd_is_valid_msr (struct kvm_vcpu *vcpu,u32 msr,bool host_initiated) {/* All MSRs refer to exactly one PMC, so msr_idx_to_pmc is enough. */ -return false; +if (!host_initiated) +return false; +
arch/x86/kvm/svm/pmu.c static struct kvm_pmc *amd_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu, · line 197
Quoted 10 lines
arch/x86/kvm/svm/pmu.c +7
-0+switch (msr) { + case MSR_K7_EVNTSEL0 ... MSR_K7_PERFCTR3: + case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5: +return true; +default : +return false; + } }static struct kvm_pmc *amd_msr_idx_to_pmc (struct kvm_vcpu *vcpu,u32 msr)
Queued all three, thanks.
Paolo