KVM: x86/pmu: Misc PMU MSR fixes
Subsystems (4)
Sean Christopherson
maintainercover KVM: x86/pmu: Misc PMU MSR fixes
Fix a handful of minor PMU MSR issues, mostly related to KVM's reporting and handling of MSRs when the PMU is disabled. E.g. running the state_test selftest with enable_pmu=0 fails because KVM tells userspace to save/restore the fixed counter MSRs, but then rejects attempts to access said MSRs from userspace.
Aaron and Like,
My plan is to apply this rather quickly, e.g. later this week, as the AMD PMU v2 series can be simplified by taking a dependency on the kvm_pmu_cap.num_counters_gp fix (patch 1). But I'll make sure to place the patches at the end of the pmu queue so that it'll be easy to unwind if there's a fatal bug without affecting the hashes of other PMU stuff destined for 6.3.
In other words, please review/test sometime in the next few weeks, even if I queue it.
Thanks!
Based on:
[email protected]:kvm-x86/linux.git pmu
Like Xu (1):
KVM: x86/pmu: Don't tell userspace to save MSRs for non-existent fixed
PMCs
Sean Christopherson (5):
KVM: x86/pmu: Cap kvm_pmu_cap.num_counters_gp at KVM's internal max
KVM: x86/pmu: Gate all "unimplemented MSR" prints on
report_ignored_msrs
KVM: x86/pmu: Use separate array for defining "PMU MSRs to save"
KVM: x86/pmu: Don't tell userspace to save PMU MSRs if PMU is disabled
KVM: x86/pmu: Provide "error" semantics for unsupported-but-known PMU
MSRs
arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/hyperv.c | 10 +- arch/x86/kvm/pmu.h | 5 +- arch/x86/kvm/svm/pmu.c | 1 + arch/x86/kvm/svm/svm.c | 5 +- arch/x86/kvm/vmx/pmu_intel.c | 1 + arch/x86/kvm/vmx/vmx.c | 4 +- arch/x86/kvm/x86.c | 229 +++++++++++++++++--------------- arch/x86/kvm/x86.h | 12 ++ 9 files changed, 149 insertions(+), 119 deletions(-)
base-commit: 647ffac11ebbbd21e04dd11a7125decb99eeee65
Signature · 1 line
2.39.1.456.gfc5497dd1b-goog
Sean Christopherson
maintainerPATCH 1/6 KVM: x86/pmu: Cap kvm_pmu_cap.num_counters_gp at KVM's internal max
Limit kvm_pmu_cap.num_counters_gp during kvm_init_pmu_capability() based on the vendor PMU capabilities so that consuming num_counters_gp naturally does the right thing. This fixes a mostly theoretical bug where KVM could over-report its PMU support in KVM_GET_SUPPORTED_CPUID for leaf 0xA, e.g. if the number of counters reported by perf is greater than KVM's hardcoded internal limit. Incorporating input from the AMD PMU also avoids over-reporting MSRs to save when running on AMD.
--- arch/x86/kvm/pmu.h | 5 ++++- arch/x86/kvm/svm/pmu.c | 1 + arch/x86/kvm/vmx/pmu_intel.c | 1 + arch/x86/kvm/x86.c | 6 +++--- 4 files changed, 9 insertions(+), 4 deletions(-)
arch/x86/kvm/pmu.h +4 -1
@@ -42,6 +42,7 @@ struct kvm_pmu_ops {void (*cleanup)(struct kvm_vcpu *vcpu);const u64 EVENTSEL_EVENT ; +const int MAX_NR_GP_COUNTERS ; };void kvm_pmu_ops_update (const struct kvm_pmu_ops *pmu_ops);
@@ -163,7 +164,7 @@ static inline bool pmc_speculative_in_use(struct kvm_pmc *pmc)extern struct x86_pmu_capability kvm_pmu_cap; -static inline void kvm_init_pmu_capability (void ) +static inline void kvm_init_pmu_capability (const struct kvm_pmu_ops *pmu_ops) {bool is_intel = boot_cpu_data.x86_vendor ==X86_VENDOR_INTEL ;
@@ -182,6 +183,8 @@ static inline void kvm_init_pmu_capability(void) } kvm_pmu_cap.version =min (kvm_pmu_cap.version,2 ); + kvm_pmu_cap.num_counters_gp =min (kvm_pmu_cap.num_counters_gp, + pmu_ops->MAX_NR_GP_COUNTERS); kvm_pmu_cap.num_counters_fixed =min (kvm_pmu_cap.num_counters_fixed,KVM_PMC_MAX_FIXED ); }
arch/x86/kvm/svm/pmu.c +1 -0
@@ -232,4 +232,5 @@ struct kvm_pmu_ops amd_pmu_ops __initdata = { .init = amd_pmu_init, .reset = amd_pmu_reset, .EVENTSEL_EVENT = AMD64_EVENTSEL_EVENT, + .MAX_NR_GP_COUNTERS = KVM_AMD_PMC_MAX_GENERIC, };
arch/x86/kvm/vmx/pmu_intel.c +1 -0
@@ -812,4 +812,5 @@ struct kvm_pmu_ops intel_pmu_ops __initdata = { .deliver_pmi = intel_pmu_deliver_pmi, .cleanup = intel_pmu_cleanup, .EVENTSEL_EVENT = ARCH_PERFMON_EVENTSEL_EVENT, + .MAX_NR_GP_COUNTERS = KVM_INTEL_PMC_MAX_GENERIC, };
arch/x86/kvm/x86.c +3 -3
@@ -7061,12 +7061,12 @@ static void kvm_init_msr_list(void)break ; case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR_MAX:if (msrs_to_save_all[i] -MSR_ARCH_PERFMON_PERFCTR0 >= -min (KVM_INTEL_PMC_MAX_GENERIC , kvm_pmu_cap.num_counters_gp)) + kvm_pmu_cap.num_counters_gp)continue ;break ; case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL_MAX:if (msrs_to_save_all[i] -MSR_ARCH_PERFMON_EVENTSEL0 >= -min (KVM_INTEL_PMC_MAX_GENERIC , kvm_pmu_cap.num_counters_gp)) + kvm_pmu_cap.num_counters_gp)continue ;break ;case MSR_IA32_XFD :
@@ -9386,7 +9386,7 @@ static int __kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)if (boot_cpu_has (X86_FEATURE_XSAVES ))rdmsrl (MSR_IA32_XSS , host_xss); -kvm_init_pmu_capability (); +kvm_init_pmu_capability (ops->pmu_ops); r = ops->hardware_setup ();if (r !=0 )
Signature · 1 line
2.39.1.456.gfc5497dd1b-goog
Sean Christopherson
maintainerPATCH 2/6 KVM: x86/pmu: Gate all "unimplemented MSR" prints on report_ignored_msrs
In reply to Sean ChristophersonAdd helpers to print unimplemented MSR accesses and condition all such prints on report_ignored_msrs, i.e. honor userspace's request to not print unimplemented MSRs. Even though vcpu_unimpl() is ratelimited, printing can still be problematic, e.g. if a print gets stalled when host userspace is writing MSRs during live migration, an effective stall can result in very noticeable disruption in the guest.
E.g. the profile below was taken while calling KVM_SET_MSRS on the PMU counters while the PMU was disabled in KVM.
- 99.75% 0.00% [.] __ioctl
- __ioctl
- 99.74% entry_SYSCALL_64_after_hwframe do_syscall_64
sys_ioctl
- do_vfs_ioctl
- 92.48% kvm_vcpu_ioctl
- kvm_arch_vcpu_ioctl
- 85.12% kvm_set_msr_ignored_check svm_set_msr kvm_set_msr_common printk
vprintk_func vprintk_default vprintk_emit console_unlock call_console_drivers univ8250_console_write serial8250_console_write uart_console_write
--- arch/x86/kvm/hyperv.c | 10 ++++------ arch/x86/kvm/svm/svm.c | 5 ++--- arch/x86/kvm/vmx/vmx.c | 4 +--- arch/x86/kvm/x86.c | 18 +++++------------- arch/x86/kvm/x86.h | 12 ++++++++++++ 5 files changed, 24 insertions(+), 25 deletions(-)
arch/x86/kvm/hyperv.c +4 -6
@@ -1430,8 +1430,7 @@ static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data, case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:return syndbg_set_msr (vcpu, msr, data, host);default : -vcpu_unimpl (vcpu,"Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n" , - msr, data); +kvm_pr_unimpl_wrmsr (vcpu, msr, data);return 1 ; }return 0 ;
@@ -1552,8 +1551,7 @@ static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)return 1 ;break ;default : -vcpu_unimpl (vcpu,"Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n" , - msr, data); +kvm_pr_unimpl_wrmsr (vcpu, msr, data);return 1 ; }
@@ -1608,7 +1606,7 @@ static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:return syndbg_get_msr (vcpu, msr, pdata, host);default : -vcpu_unimpl (vcpu,"Hyper-V unhandled rdmsr: 0x%x\n" , msr); +kvm_pr_unimpl_rdmsr (vcpu, msr);return 1 ; }
@@ -1673,7 +1671,7 @@ static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, data =APIC_BUS_FREQUENCY ;break ;default : -vcpu_unimpl (vcpu,"Hyper-V unhandled rdmsr: 0x%x\n" , msr); +kvm_pr_unimpl_rdmsr (vcpu, msr);return 1 ; } *pdata = data;
arch/x86/kvm/svm/svm.c +2 -3
@@ -3015,8 +3015,7 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)break ;case MSR_IA32_DEBUGCTLMSR :if (!lbrv) { -vcpu_unimpl (vcpu,"%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n" , - __func__, data); +kvm_pr_unimpl_wrmsr (vcpu, ecx, data);break ; }if (data &DEBUGCTL_RESERVED_BITS )
@@ -3045,7 +3044,7 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)case MSR_VM_CR :return svm_set_vm_cr (vcpu, data);case MSR_VM_IGNNE : -vcpu_unimpl (vcpu,"unimplemented wrmsr: 0x%x data 0x%llx\n" , ecx, data); +kvm_pr_unimpl_wrmsr (vcpu, ecx, data);break ;case MSR_AMD64_DE_CFG : {struct kvm_msr_entry msr_entry;
arch/x86/kvm/vmx/vmx.c +1 -3
@@ -2206,9 +2206,7 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) invalid = data & ~vmx_get_supported_debugctl (vcpu, msr_info->host_initiated);if (invalid & (DEBUGCTLMSR_BTF |DEBUGCTLMSR_LBR )) { -if (report_ignored_msrs) -vcpu_unimpl (vcpu,"%s: BTF|LBR in IA32_DEBUGCTLMSR 0x%llx, nop\n" , - __func__, data); +kvm_pr_unimpl_wrmsr (vcpu, msr_index, data); data &= ~(DEBUGCTLMSR_BTF |DEBUGCTLMSR_LBR ); invalid &= ~(DEBUGCTLMSR_BTF |DEBUGCTLMSR_LBR ); }
arch/x86/kvm/x86.c +5 -13
@@ -3560,7 +3560,6 @@ static void record_steal_time(struct kvm_vcpu *vcpu)int kvm_set_msr_common (struct kvm_vcpu *vcpu,struct msr_data *msr_info) { -bool pr = false;u32 msr = msr_info->index;u64 data = msr_info->data;
@@ -3606,15 +3605,13 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)if (data ==BIT_ULL (18 )) { vcpu->arch.msr_hwcr = data; }else if (data !=0 ) { -vcpu_unimpl (vcpu,"unimplemented HWCR wrmsr: 0x%llx\n" , - data); +kvm_pr_unimpl_wrmsr (vcpu, msr, data);return 1 ; }break ;case MSR_FAM10H_MMIO_CONF_BASE :if (data !=0 ) { -vcpu_unimpl (vcpu,"unimplemented MMIO_CONF_BASE wrmsr: " -"0x%llx\n" , data); +kvm_pr_unimpl_wrmsr (vcpu, msr, data);return 1 ; }break ;
@@ -3794,16 +3791,13 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3: case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1: - pr = true; - fallthrough; case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3: case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:if (kvm_pmu_is_valid_msr (vcpu, msr))return kvm_pmu_set_msr (vcpu, msr_info); -if (pr || data !=0 ) -vcpu_unimpl (vcpu,"disabled perfctr wrmsr: " -"0x%x data 0x%llx\n" , msr, data); +if (data) +kvm_pr_unimpl_wrmsr (vcpu, msr, data);break ;case MSR_K7_CLK_CTL :/*
@@ -3831,9 +3825,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)/* Drop writes to this legacy MSR -- see rdmsr * counterpart for further detail. */ -if (report_ignored_msrs) -vcpu_unimpl (vcpu,"ignored wrmsr: 0x%x data 0x%llx\n" , - msr, data); +kvm_pr_unimpl_wrmsr (vcpu, msr, data);break ;case MSR_AMD64_OSVW_ID_LENGTH :if (!guest_cpuid_has (vcpu,X86_FEATURE_OSVW ))
arch/x86/kvm/x86.h +12 -0
@@ -331,6 +331,18 @@ extern bool report_ignored_msrs;extern bool eager_page_split; +static inline void kvm_pr_unimpl_wrmsr (struct kvm_vcpu *vcpu,u32 msr,u64 data) +{ +if (report_ignored_msrs) +vcpu_unimpl (vcpu,"Unhandled WRMSR(0x%x) = 0x%llx\n" , msr, data); +} + +static inline void kvm_pr_unimpl_rdmsr (struct kvm_vcpu *vcpu,u32 msr) +{ +if (report_ignored_msrs) +vcpu_unimpl (vcpu,"Unhandled RDMSR(0x%x)\n" , msr); +} +static inline u64 nsec_to_cycles (struct kvm_vcpu *vcpu,u64 nsec) { return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,
Signature · 1 line
2.39.1.456.gfc5497dd1b-goog
Re: [PATCH 2/6] KVM: x86/pmu: Gate all "unimplemented MSR" prints on report_ignored_msrs
Quoted 157 lines · Sean Christopherson
Add helpers to print unimplemented MSR accesses and condition all such prints on
report_ignored_msrs, i.e. honor userspace's request to not print unimplemented MSRs. Even thoughvcpu_unimpl()is ratelimited, printing can still be problematic, e.g. if a print gets stalled when host userspace is writing MSRs during live migration, an effective stall can result in very noticeable disruption in the guest.E.g. the profile below was taken while calling
KVM_SET_MSRSon the PMU counters while the PMU was disabled in KVM.- 99.75% 0.00% [.]
__ioctl
-__ioctl
- 99.74%entry_SYSCALL_64_after_hwframedo_syscall_64
sys_ioctl
-do_vfs_ioctl
- 92.48%kvm_vcpu_ioctl
-kvm_arch_vcpu_ioctl
- 85.12%kvm_set_msr_ignored_checksvm_set_msrkvm_set_msr_commonprintk
vprintk_funcvprintk_defaultvprintk_emitconsole_unlockcall_console_driversuniv8250_console_writeserial8250_console_writeuart_console_writeReported-by Aaron Lewis <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/hyperv.c | 10 ++++------ arch/x86/kvm/svm/svm.c | 5 ++--- arch/x86/kvm/vmx/vmx.c | 4 +--- arch/x86/kvm/x86.c | 18 +++++------------- arch/x86/kvm/x86.h | 12 ++++++++++++ 5 files changed, 24 insertions(+), 25 deletions(-)arch/x86/kvm/hyperv.c +4
-6@@ -1430,8 +1430,7 @@ static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data, case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:return syndbg_set_msr (vcpu, msr, data, host);default : -vcpu_unimpl (vcpu,"Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n" , - msr, data); +kvm_pr_unimpl_wrmsr (vcpu, msr, data);return 1 ; }return 0 ;@@ -1552,8 +1551,7 @@ static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)return 1 ;break ;default : -vcpu_unimpl (vcpu,"Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n" , - msr, data); +kvm_pr_unimpl_wrmsr (vcpu, msr, data);return 1 ; }@@ -1608,7 +1606,7 @@ static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:return syndbg_get_msr (vcpu, msr, pdata, host);default : -vcpu_unimpl (vcpu,"Hyper-V unhandled rdmsr: 0x%x\n" , msr); +kvm_pr_unimpl_rdmsr (vcpu, msr);return 1 ; }@@ -1673,7 +1671,7 @@ static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, data =APIC_BUS_FREQUENCY ;break ;default : -vcpu_unimpl (vcpu,"Hyper-V unhandled rdmsr: 0x%x\n" , msr); +kvm_pr_unimpl_rdmsr (vcpu, msr);return 1 ; } *pdata = data;arch/x86/kvm/svm/svm.c +2
-3@@ -3015,8 +3015,7 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)break ;case MSR_IA32_DEBUGCTLMSR :if (!lbrv) { -vcpu_unimpl (vcpu,"%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n" , - __func__, data); +kvm_pr_unimpl_wrmsr (vcpu, ecx, data);break ; }if (data &DEBUGCTL_RESERVED_BITS )@@ -3045,7 +3044,7 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)case MSR_VM_CR :return svm_set_vm_cr (vcpu, data);case MSR_VM_IGNNE : -vcpu_unimpl (vcpu,"unimplemented wrmsr: 0x%x data 0x%llx\n" , ecx, data); +kvm_pr_unimpl_wrmsr (vcpu, ecx, data);break ;case MSR_AMD64_DE_CFG : {struct kvm_msr_entry msr_entry;arch/x86/kvm/vmx/vmx.c +1
-3@@ -2206,9 +2206,7 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) invalid = data & ~vmx_get_supported_debugctl (vcpu, msr_info->host_initiated);if (invalid & (DEBUGCTLMSR_BTF |DEBUGCTLMSR_LBR )) { -if (report_ignored_msrs) -vcpu_unimpl (vcpu,"%s: BTF|LBR in IA32_DEBUGCTLMSR 0x%llx, nop\n" , - __func__, data); +kvm_pr_unimpl_wrmsr (vcpu, msr_index, data); data &= ~(DEBUGCTLMSR_BTF |DEBUGCTLMSR_LBR ); invalid &= ~(DEBUGCTLMSR_BTF |DEBUGCTLMSR_LBR ); }arch/x86/kvm/x86.c +2
-7@@ -3560,7 +3560,6 @@ static void record_steal_time(struct kvm_vcpu *vcpu)int kvm_set_msr_common (struct kvm_vcpu *vcpu,struct msr_data *msr_info) { -bool pr = false;u32 msr = msr_info->index;u64 data = msr_info->data;@@ -3606,15 +3605,13 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)if (data ==BIT_ULL (18 )) { vcpu->arch.msr_hwcr = data; }else if (data !=0 ) { -vcpu_unimpl (vcpu,"unimplemented HWCR wrmsr: 0x%llx\n" , - data); +kvm_pr_unimpl_wrmsr (vcpu, msr, data);return 1 ; }break ;case MSR_FAM10H_MMIO_CONF_BASE :if (data !=0 ) { -vcpu_unimpl (vcpu,"unimplemented MMIO_CONF_BASE wrmsr: " -"0x%llx\n" , data); +kvm_pr_unimpl_wrmsr (vcpu, msr, data);return 1 ; }break ;@@ -3794,16 +3791,13 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3: case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1: - pr = true; - fallthrough;
arch/x86/kvm/x86.c int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 3800
Quoted 10 lines
arch/x86/kvm/x86.c +2
-3case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3: case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:if (kvm_pmu_is_valid_msr (vcpu, msr))return kvm_pmu_set_msr (vcpu, msr_info); -if (pr || data !=0 ) -vcpu_unimpl (vcpu,"disabled perfctr wrmsr: " -"0x%x data 0x%llx\n" , msr, data); +if (data) +kvm_pr_unimpl_wrmsr (vcpu, msr, data);
The logic here was that "*_PERFCTR*" MSRs are reported even when 'data == 0' but looking at the commit 5753785fa977 ("KVM: do not #GP on perf MSR writes when vPMU is disabled") I can't really say why it was needed.
Quoted 27 lines
arch/x86/kvm/x86.c +1
-3break ;case MSR_K7_CLK_CTL :/* @@ -3831,9 +3825,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)/* Drop writes to this legacy MSR -- see rdmsr * counterpart for further detail. */ -if (report_ignored_msrs) -vcpu_unimpl (vcpu,"ignored wrmsr: 0x%x data 0x%llx\n" , - msr, data); +kvm_pr_unimpl_wrmsr (vcpu, msr, data);break ;case MSR_AMD64_OSVW_ID_LENGTH :if (!guest_cpuid_has (vcpu,X86_FEATURE_OSVW ))arch/x86/kvm/x86.h +5
-0@@ -331,6 +331,18 @@ extern bool report_ignored_msrs;extern bool eager_page_split; +static inline void kvm_pr_unimpl_wrmsr (struct kvm_vcpu *vcpu,u32 msr,u64 data) +{ +if (report_ignored_msrs) +vcpu_unimpl (vcpu,"Unhandled WRMSR(0x%x) = 0x%llx\n" , msr, data); +}
arch/x86/kvm/x86.h extern bool report_ignored_msrs; · line 348
Quoted 10 lines
arch/x86/kvm/x86.h +7
-0+ +static inline void kvm_pr_unimpl_rdmsr (struct kvm_vcpu *vcpu,u32 msr) +{ +if (report_ignored_msrs) +vcpu_unimpl (vcpu,"Unhandled RDMSR(0x%x)\n" , msr); +} +static inline u64 nsec_to_cycles (struct kvm_vcpu *vcpu,u64 nsec) { return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,
Signature · 1 line
Vitaly
Re: [PATCH 2/6] KVM: x86/pmu: Gate all "unimplemented MSR" prints on report_ignored_msrs
In reply to Sean ChristophersonQuoted 6 lines · Sean Christopherson
arch/x86/kvm/hyperv.c | 10 ++++------ arch/x86/kvm/svm/svm.c | 5 ++--- arch/x86/kvm/vmx/vmx.c | 4 +--- arch/x86/kvm/x86.c | 18 +++++------------- arch/x86/kvm/x86.h | 12 ++++++++++++ 5 files changed, 24 insertions(+), 25 deletions(-)
Nit: those changes don't just involve PMU, better to remove pmu suffix from the patch title for more eyes.
Sean Christopherson
maintainerRe: [PATCH 2/6] KVM: x86/pmu: Gate all "unimplemented MSR" prints on report_ignored_msrs
Quoted 10 lines · Like Xu
Quoted 6 lines · Sean Christopherson
arch/x86/kvm/hyperv.c | 10 ++++------ arch/x86/kvm/svm/svm.c | 5 ++--- arch/x86/kvm/vmx/vmx.c | 4 +--- arch/x86/kvm/x86.c | 18 +++++------------- arch/x86/kvm/x86.h | 12 ++++++++++++ 5 files changed, 24 insertions(+), 25 deletions(-)Nit: those changes don't just involve PMU, better to remove pmu suffix from the patch title for more eyes.
Ah, right, I got a bit overzealous. Thanks!
Sean Christopherson
maintainerPATCH 3/6 KVM: x86/pmu: Use separate array for defining "PMU MSRs to save"
In reply to Sean ChristophersonMove all potential to-be-saved PMU MSRs into a separate array so that a future patch can easily omit all PMU MSRs from the list when the PMU is disabled.
No functional change intended.
--- arch/x86/kvm/x86.c | 153 ++++++++++++++++++++++++--------------------- 1 file changed, 82 insertions(+), 71 deletions(-)
arch/x86/kvm/x86.c +82 -71
@@ -1419,7 +1419,7 @@ EXPORT_SYMBOL_GPL(kvm_emulate_rdpmc);* may depend on host virtualization features rather than host cpu features. */ -static const u32 msrs_to_save_all[] = { +static const u32 msrs_to_save_base[] = {MSR_IA32_SYSENTER_CS ,MSR_IA32_SYSENTER_ESP ,MSR_IA32_SYSENTER_EIP ,MSR_STAR , #ifdef CONFIG_X86_64
@@ -1436,6 +1436,10 @@ static const u32 msrs_to_save_all[] = {MSR_IA32_RTIT_ADDR3_A ,MSR_IA32_RTIT_ADDR3_B ,MSR_IA32_UMWAIT_CONTROL , + MSR_IA32_XFD, MSR_IA32_XFD_ERR, +}; + +static const u32 msrs_to_save_pmu[] = {MSR_ARCH_PERFMON_FIXED_CTR0 ,MSR_ARCH_PERFMON_FIXED_CTR1 ,MSR_ARCH_PERFMON_FIXED_CTR0 +2 , MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS,
@@ -1460,11 +1464,10 @@ static const u32 msrs_to_save_all[] = {MSR_F15H_PERF_CTL3 ,MSR_F15H_PERF_CTL4 ,MSR_F15H_PERF_CTL5 ,MSR_F15H_PERF_CTR0 ,MSR_F15H_PERF_CTR1 ,MSR_F15H_PERF_CTR2 , MSR_F15H_PERF_CTR3, MSR_F15H_PERF_CTR4, MSR_F15H_PERF_CTR5, - - MSR_IA32_XFD, MSR_IA32_XFD_ERR, }; -static u32 msrs_to_save[ARRAY_SIZE (msrs_to_save_all)]; +static u32 msrs_to_save[ARRAY_SIZE (msrs_to_save_base) + +ARRAY_SIZE (msrs_to_save_pmu)];static unsigned num_msrs_to_save; static const u32 emulated_msrs_all[] = {
@@ -6994,84 +6997,92 @@ long kvm_arch_vm_ioctl(struct file *filp,return r; } -static void kvm_init_msr_list (void ) +static void kvm_probe_msr_to_save (u32 msr_index) {u32 dummy[2 ]; + +if (rdmsr_safe (msr_index, &dummy[0 ], &dummy[1 ])) +return ; + +/* +* Even MSRs that are valid in the host may not be exposed to guests in +* some cases. +*/ +switch (msr_index) { +case MSR_IA32_BNDCFGS : +if (!kvm_mpx_supported ()) +return ; +break ; +case MSR_TSC_AUX : +if (!kvm_cpu_cap_has (X86_FEATURE_RDTSCP ) && + !kvm_cpu_cap_has (X86_FEATURE_RDPID )) +return ; +break ; +case MSR_IA32_UMWAIT_CONTROL : +if (!kvm_cpu_cap_has (X86_FEATURE_WAITPKG )) +return ; +break ; +case MSR_IA32_RTIT_CTL : +case MSR_IA32_RTIT_STATUS : +if (!kvm_cpu_cap_has (X86_FEATURE_INTEL_PT )) +return ; +break ; +case MSR_IA32_RTIT_CR3_MATCH : +if (!kvm_cpu_cap_has (X86_FEATURE_INTEL_PT ) || + !intel_pt_validate_hw_cap (PT_CAP_cr3_filtering)) +return ; +break ; +case MSR_IA32_RTIT_OUTPUT_BASE : +case MSR_IA32_RTIT_OUTPUT_MASK : +if (!kvm_cpu_cap_has (X86_FEATURE_INTEL_PT ) || + (!intel_pt_validate_hw_cap (PT_CAP_topa_output) && + !intel_pt_validate_hw_cap (PT_CAP_single_range_output))) +return ; +break ; + case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: +if (!kvm_cpu_cap_has (X86_FEATURE_INTEL_PT ) || + (msr_index -MSR_IA32_RTIT_ADDR0_A >= +intel_pt_validate_hw_cap (PT_CAP_num_address_ranges) *2 )) +return ; +break ; + case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR_MAX: +if (msr_index -MSR_ARCH_PERFMON_PERFCTR0 >= + kvm_pmu_cap.num_counters_gp) +return ; +break ; + case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL_MAX: +if (msr_index -MSR_ARCH_PERFMON_EVENTSEL0 >= + kvm_pmu_cap.num_counters_gp) +return ; +break ; +case MSR_IA32_XFD : +case MSR_IA32_XFD_ERR : +if (!kvm_cpu_cap_has (X86_FEATURE_XFD )) +return ; +break ; +default : +break ; + } + + msrs_to_save[num_msrs_to_save++] = msr_index; +} + +static void kvm_init_msr_list (void ) +{unsigned i;BUILD_BUG_ON_MSG (KVM_PMC_MAX_FIXED !=3 , -"Please update the fixed PMCs in msrs_to_saved_all[]" ); +"Please update the fixed PMCs in msrs_to_save_pmu[]" ); num_msrs_to_save =0 ; num_emulated_msrs =0 ; num_msr_based_features =0 ; -for (i =0 ; i <ARRAY_SIZE (msrs_to_save_all); i++) { -if (rdmsr_safe (msrs_to_save_all[i], &dummy[0 ], &dummy[1 ]) <0 ) -continue ; +for (i =0 ; i <ARRAY_SIZE (msrs_to_save_base); i++) +kvm_probe_msr_to_save (msrs_to_save_base[i]); -/* -* Even MSRs that are valid in the host may not be exposed -* to the guests in some cases. -*/ -switch (msrs_to_save_all[i]) { -case MSR_IA32_BNDCFGS : -if (!kvm_mpx_supported ()) -continue ; -break ; -case MSR_TSC_AUX : -if (!kvm_cpu_cap_has (X86_FEATURE_RDTSCP ) && - !kvm_cpu_cap_has (X86_FEATURE_RDPID )) -continue ; -break ; -case MSR_IA32_UMWAIT_CONTROL : -if (!kvm_cpu_cap_has (X86_FEATURE_WAITPKG )) -continue ; -break ; -case MSR_IA32_RTIT_CTL : -case MSR_IA32_RTIT_STATUS : -if (!kvm_cpu_cap_has (X86_FEATURE_INTEL_PT )) -continue ; -break ; -case MSR_IA32_RTIT_CR3_MATCH : -if (!kvm_cpu_cap_has (X86_FEATURE_INTEL_PT ) || - !intel_pt_validate_hw_cap (PT_CAP_cr3_filtering)) -continue ; -break ; -case MSR_IA32_RTIT_OUTPUT_BASE : -case MSR_IA32_RTIT_OUTPUT_MASK : -if (!kvm_cpu_cap_has (X86_FEATURE_INTEL_PT ) || - (!intel_pt_validate_hw_cap (PT_CAP_topa_output) && - !intel_pt_validate_hw_cap (PT_CAP_single_range_output))) -continue ; -break ; - case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: -if (!kvm_cpu_cap_has (X86_FEATURE_INTEL_PT ) || - msrs_to_save_all[i] -MSR_IA32_RTIT_ADDR0_A >= -intel_pt_validate_hw_cap (PT_CAP_num_address_ranges) *2 ) -continue ; -break ; - case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR_MAX: -if (msrs_to_save_all[i] -MSR_ARCH_PERFMON_PERFCTR0 >= - kvm_pmu_cap.num_counters_gp) -continue ; -break ; - case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL_MAX: -if (msrs_to_save_all[i] -MSR_ARCH_PERFMON_EVENTSEL0 >= - kvm_pmu_cap.num_counters_gp) -continue ; -break ; -case MSR_IA32_XFD : -case MSR_IA32_XFD_ERR : -if (!kvm_cpu_cap_has (X86_FEATURE_XFD )) -continue ; -break ; -default : -break ; - } - - msrs_to_save[num_msrs_to_save++] = msrs_to_save_all[i]; - } +for (i =0 ; i <ARRAY_SIZE (msrs_to_save_pmu); i++) +kvm_probe_msr_to_save (msrs_to_save_pmu[i]);for (i =0 ; i <ARRAY_SIZE (emulated_msrs_all); i++) {if (!static_call (kvm_x86_has_emulated_msr)(NULL , emulated_msrs_all[i]))
Signature · 1 line
2.39.1.456.gfc5497dd1b-goog
Re: [PATCH 3/6] KVM: x86/pmu: Use separate array for defining "PMU MSRs to save"
Quoted 16 lines · Sean Christopherson
Move all potential to-be-saved PMU MSRs into a separate array so that a future patch can easily omit all PMU MSRs from the list when the PMU is disabled.
No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/x86.c | 153 ++++++++++++++++++++++++--------------------- 1 file changed, 82 insertions(+), 71 deletions(-)arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c EXPORT_SYMBOL_GPL(kvm_emulate_rdpmc); · line 1422
Quoted 5 lines
arch/x86/kvm/x86.c +1
-1@@ -1419,7 +1419,7 @@ EXPORT_SYMBOL_GPL(kvm_emulate_rdpmc);* may depend on host virtualization features rather than host cpu features. */ -static const u32 msrs_to_save_all[] = { +static const u32 msrs_to_save_base[] = {
I guess the comments relating to msr_to_save_all should be updated as well. :)
arch/x86/kvm/x86.c +2 -2
@@ -1413,7 +1413,7 @@ EXPORT_SYMBOL_GPL(kvm_emulate_rdpmc);* * The three MSR lists(msrs_to_save, emulated_msrs, msr_based_features) * extract the supported MSRs from the related const lists. -* msrs_to_save is selected from the msrs_to_save_all to reflect the +* msrs_to_save is selected from the msrs_to_save_base to reflect the * capabilities of the host cpu. This capabilities test skips MSRs that are * kvm-specific. Those are put in emulated_msrs_all; filtering of emulated_msrs * may depend on host virtualization features rather than host cpu features.
@@ -1516,7 +1516,7 @@ static const u32 emulated_msrs_all[] = {* by arch/x86/kvm/vmx/nested.c based on CPUID or other MSRs. * We always support the "true" VMX control MSRs, even if the host * processor does not, so I am putting these registers here rather -* than in msrs_to_save_all. +* than in msrs_to_save_base. */ MSR_IA32_VMX_BASIC , MSR_IA32_VMX_TRUE_PINBASED_CTLS,
For the comments of emulated_msrs_all, after renaming msrs_to_save_all to msrs_to_save_base, maybe we do not need to take pains to explain that?
B.R.
Yu
Sean Christopherson
maintainerRe: [PATCH 3/6] KVM: x86/pmu: Use separate array for defining "PMU MSRs to save"
Quoted 20 lines · Yu Zhang
Quoted 19 lines · Sean Christopherson
Move all potential to-be-saved PMU MSRs into a separate array so that a future patch can easily omit all PMU MSRs from the list when the PMU is disabled.
No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/x86.c | 153 ++++++++++++++++++++++++--------------------- 1 file changed, 82 insertions(+), 71 deletions(-)arch/x86/kvm/x86.c +0
-0@@ -1419,7 +1419,7 @@ EXPORT_SYMBOL_GPL(kvm_emulate_rdpmc);* may depend on host virtualization features rather than host cpu features. */
Quoted 4 lines
arch/x86/kvm/x86.c EXPORT_SYMBOL_GPL(kvm_emulate_rdpmc); · line 1422
Quoted 2 lines
arch/x86/kvm/x86.c +1
-1-static const u32 msrs_to_save_all[] = { +static const u32 msrs_to_save_base[] = {I guess the comments relating to
msr_to_save_allshould be updated as well. :)
Gah, yes. I caught that at one point but lost track of it. I'll get it fixed in v3.
Thanks!
Sean Christopherson
maintainerPATCH 4/6 KVM: x86/pmu: Don't tell userspace to save PMU MSRs if PMU is disabled
In reply to Sean ChristophersonOmit all PMU MSRs from the "MSRs to save" list if the PMU is disabled so that userspace doesn't waste time saving and restoring dummy values. KVM provides "error" semantics (read zeros, drop writes) for such known-but- unsupported MSRs, i.e. has fudged around this issue for quite some time. Keep the "error" semantics as-is for now, the logic will be cleaned up in a separate patch.
--- arch/x86/kvm/x86.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +4 -2
@@ -7081,8 +7081,10 @@ static void kvm_init_msr_list(void)for (i =0 ; i <ARRAY_SIZE (msrs_to_save_base); i++)kvm_probe_msr_to_save (msrs_to_save_base[i]); -for (i =0 ; i <ARRAY_SIZE (msrs_to_save_pmu); i++) -kvm_probe_msr_to_save (msrs_to_save_pmu[i]); +if (enable_pmu) { +for (i =0 ; i <ARRAY_SIZE (msrs_to_save_pmu); i++) +kvm_probe_msr_to_save (msrs_to_save_pmu[i]); + }for (i =0 ; i <ARRAY_SIZE (emulated_msrs_all); i++) {if (!static_call (kvm_x86_has_emulated_msr)(NULL , emulated_msrs_all[i]))
Signature · 1 line
2.39.1.456.gfc5497dd1b-goog
Sean Christopherson
maintainerPATCH 5/6 KVM: x86/pmu: Don't tell userspace to save MSRs for non-existent fixed PMCs
In reply to Sean ChristophersonFrom: Like Xu <[email protected]>
Limit the set of MSRs for fixed PMU counters based on the number of fixed counters actually supported by the host so that userspace doesn't waste time saving and restoring dummy values.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 5 +++++ 2 files changed, 6 insertions(+)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -514,6 +514,7 @@ struct kvm_pmc {#define MSR_ARCH_PERFMON_PERFCTR_MAX (MSR_ARCH_PERFMON_PERFCTR0 + KVM_INTEL_PMC_MAX_GENERIC - 1)#define MSR_ARCH_PERFMON_EVENTSEL_MAX (MSR_ARCH_PERFMON_EVENTSEL0 + KVM_INTEL_PMC_MAX_GENERIC - 1)#define KVM_PMC_MAX_FIXED 3 +#define MSR_ARCH_PERFMON_FIXED_CTR_MAX (MSR_ARCH_PERFMON_FIXED_CTR0 + KVM_PMC_MAX_FIXED - 1)#define KVM_AMD_PMC_MAX_GENERIC 6struct kvm_pmu {unsigned nr_arch_gp_counters;
arch/x86/kvm/x86.c +5 -0
@@ -7055,6 +7055,11 @@ static void kvm_probe_msr_to_save(u32 msr_index) kvm_pmu_cap.num_counters_gp)return ;break ; + case MSR_ARCH_PERFMON_FIXED_CTR0 ... MSR_ARCH_PERFMON_FIXED_CTR_MAX: +if (msr_index -MSR_ARCH_PERFMON_FIXED_CTR0 >= + kvm_pmu_cap.num_counters_fixed) +return ; +break ;case MSR_IA32_XFD :case MSR_IA32_XFD_ERR :if (!kvm_cpu_cap_has (X86_FEATURE_XFD ))
Signature · 1 line
2.39.1.456.gfc5497dd1b-goog
Sean Christopherson
maintainerPATCH 6/6 KVM: x86/pmu: Provide "error" semantics for unsupported-but-known PMU MSRs
In reply to Sean ChristophersonProvide "error" semantics (read zeros, drop writes) for userspace accesses to MSRs that are ultimately unsupported for whatever reason, but for which KVM told userspace to save and restore the MSR, i.e. for MSRs that KVM included in KVM_GET_MSR_INDEX_LIST.
Previously, KVM special cased a few PMU MSRs that were problematic at one point or another. Extend the treatment to all PMU MSRs, e.g. to avoid spurious unsupported accesses.
Note, the logic can also be used for non-PMU MSRs, but as of today only PMU MSRs can end up being unsupported after KVM told userspace to save and restore them.
--- arch/x86/kvm/x86.c | 51 ++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 22 deletions(-)
arch/x86/kvm/x86.c +29 -22
@@ -3561,6 +3561,18 @@ static void record_steal_time(struct kvm_vcpu *vcpu)mark_page_dirty_in_slot (vcpu->kvm, ghc->memslot,gpa_to_gfn (ghc->gpa)); } +static bool kvm_is_msr_to_save (u32 msr_index) +{ +unsigned int i; + +for (i =0 ; i < num_msrs_to_save; i++) { +if (msrs_to_save[i] == msr_index) +return true; + } + +return false; +} +int kvm_set_msr_common (struct kvm_vcpu *vcpu,struct msr_data *msr_info) {u32 msr = msr_info->index;
@@ -3876,20 +3888,18 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) vcpu->arch.guest_fpu.xfd_err = data;break ;#endif -case MSR_IA32_PEBS_ENABLE : -case MSR_IA32_DS_AREA : -case MSR_PEBS_DATA_CFG : - case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5: +default :if (kvm_pmu_is_valid_msr (vcpu, msr))return kvm_pmu_set_msr (vcpu, msr_info); +/* * Userspace is allowed to write '0' to MSRs that KVM reports * as to-be-saved, even if an MSRs isn't fully supported. */ -return !msr_info->host_initiated || data; -default : -if (kvm_pmu_is_valid_msr (vcpu, msr)) -return kvm_pmu_set_msr (vcpu, msr_info); +if (msr_info->host_initiated && !data && +kvm_is_msr_to_save (msr)) +break ; +return KVM_MSR_RET_INVALID ; }return 0 ;
@@ -3979,20 +3989,6 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)case MSR_DRAM_ENERGY_STATUS :/* DRAM controller */ msr_info->data =0 ;break ; -case MSR_IA32_PEBS_ENABLE : -case MSR_IA32_DS_AREA : -case MSR_PEBS_DATA_CFG : - case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5: -if (kvm_pmu_is_valid_msr (vcpu, msr_info->index)) -return kvm_pmu_get_msr (vcpu, msr_info); -/* -* Userspace is allowed to read MSRs that KVM reports as -* to-be-saved, even if an MSR isn't fully supported. -*/ -if (!msr_info->host_initiated) -return 1 ; - msr_info->data =0 ; -break ; case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3: case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3: case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
@@ -4248,6 +4244,17 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)default :if (kvm_pmu_is_valid_msr (vcpu, msr_info->index))return kvm_pmu_get_msr (vcpu, msr_info); + +/* +* Userspace is allowed to read MSRs that KVM reports as +* to-be-saved, even if an MSR isn't fully supported. +*/ +if (msr_info->host_initiated && +kvm_is_msr_to_save (msr_info->index)) { + msr_info->data =0 ; +break ; + } +return KVM_MSR_RET_INVALID ; }return 0 ;
Signature · 1 line
2.39.1.456.gfc5497dd1b-goog
Quoted 8 lines · Sean Christopherson
Fix a handful of minor PMU MSR issues, mostly related to KVM's reporting and handling of MSRs when the PMU is disabled. E.g. running the
state_testselftest with enable_pmu=0 fails because KVM tells userspace to save/restore the fixed counter MSRs, but then rejects attempts to access said MSRs from userspace.[...]
Applied to kvm-x86 pmu. As mentioned in the cover letter (trimmed by b4), I'll yank these out of the queue if any objects and/or there are problems.
[1/6] KVM: x86/pmu: Cap kvm_pmu_cap.num_counters_gp at KVM's internal max
https://github.com/kvm-x86/linux/commit/8911ce66697e [2/6] KVM: x86/pmu: Gate all "unimplemented MSR" prints on report_ignored_msrs https://github.com/kvm-x86/linux/commit/e76ae52747a8 [3/6] KVM: x86/pmu: Use separate array for defining "PMU MSRs to save" https://github.com/kvm-x86/linux/commit/2374b7310b66 [4/6] KVM: x86/pmu: Don't tell userspace to save PMU MSRs if PMU is disabled https://github.com/kvm-x86/linux/commit/c3531edc79a7 [5/6] KVM: x86/pmu: Don't tell userspace to save MSRs for non-existent fixed PMCs https://github.com/kvm-x86/linux/commit/e33b6d79acac [6/6] KVM: x86/pmu: Provide "error" semantics for unsupported-but-known PMU MSRs https://github.com/kvm-x86/linux/commit/2de154f541fc
--
https://github.com/kvm-x86/linux/tree/next
https://github.com/kvm-x86/linux/tree/fixes