On hosts supporting PERF_METRICS, if either the PERF_METRICS MSR or fixed counter 3 is absent from the guest, keep GLOBAL_CTRL and RDPMC intercepted so the guest cannot set GLOBAL_CTRL[48] or read metrics via RDPMC without fixed counter 3. KVM does not expose PERF_METRICS or fixed counter 3 to guests yet, so this keeps GLOBAL_CTRL and RDPMC intercepted on hosts that support PERF_METRICS until both are virtualized. Opportunistically, rename kvm_need_any_pmc_intercept() to kvm_pmu_incomplete_passthrough(). The function does not decide whether a particular PMC MSR needs interception. It reports whether the guest PMU differs from the host PMU, and the PERF_METRICS check widens that further. Signed-off-by: Zide Chen --- v10: new patch. --- arch/x86/include/asm/msr-index.h | 1 + arch/x86/kvm/pmu.c | 22 +++++++++++++++++----- arch/x86/kvm/pmu.h | 5 +++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h index 3a8e51a0c9e8..8fad944197fb 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -331,6 +331,7 @@ #define PERF_CAP_PEBS_FORMAT 0xf00 #define PERF_CAP_FW_WRITES BIT_ULL(13) #define PERF_CAP_PEBS_BASELINE BIT_ULL(14) +#define PERF_CAP_PERF_METRICS BIT_ULL(15) #define PERF_CAP_PEBS_TIMING_INFO BIT_ULL(17) #define PERF_CAP_PEBS_MASK (PERF_CAP_PEBS_TRAP | PERF_CAP_ARCH_REG | \ PERF_CAP_PEBS_FORMAT | PERF_CAP_PEBS_BASELINE | \ diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index dba651d4e449..4574cac5f148 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -778,7 +778,7 @@ int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data) return 0; } -static bool kvm_need_any_pmc_intercept(struct kvm_vcpu *vcpu) +static bool kvm_pmu_incomplete_passthrough(struct kvm_vcpu *vcpu) { struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); @@ -790,13 +790,25 @@ static bool kvm_need_any_pmc_intercept(struct kvm_vcpu *vcpu) * KVM's capabilities are constrained based on KVM support, i.e. KVM's * capabilities themselves may be a subset of hardware capabilities. */ - return pmu->nr_arch_gp_counters != kvm_host_pmu.num_counters_gp || - pmu->nr_arch_fixed_counters != kvm_host_pmu.num_counters_fixed; + if (pmu->nr_arch_gp_counters != kvm_host_pmu.num_counters_gp || + pmu->nr_arch_fixed_counters != kvm_host_pmu.num_counters_fixed) + return true; + + /* + * RDPMC passthrough must be blocked if fixed counter 3 is missing + * to avoid leaking host metrics, as IA32_PERF_METRICS is not + * context-switched without it. + * + * That condition is already caught by the counter count check above, + * so only check for PERF_METRICS here. + */ + return (kvm_host_pmu.perf_capabilities & PERF_CAP_PERF_METRICS) && + !kvm_vcpu_has_perf_metrics(vcpu); } bool kvm_need_perf_global_ctrl_intercept(struct kvm_vcpu *vcpu) { - return kvm_need_any_pmc_intercept(vcpu) || + return kvm_pmu_incomplete_passthrough(vcpu) || !kvm_pmu_has_perf_global_ctrl(vcpu_to_pmu(vcpu)); } EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_need_perf_global_ctrl_intercept); @@ -812,7 +824,7 @@ bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu) if (enable_vmware_backdoor) return true; - return kvm_need_any_pmc_intercept(vcpu) || + return kvm_pmu_incomplete_passthrough(vcpu) || pmu->counter_bitmask[KVM_PMC_GP] != (BIT_ULL(kvm_host_pmu.bit_width_gp) - 1) || pmu->counter_bitmask[KVM_PMC_FIXED] != (BIT_ULL(kvm_host_pmu.bit_width_fixed) - 1); } diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h index 2a9c1bbe5c40..ae1e6545df40 100644 --- a/arch/x86/kvm/pmu.h +++ b/arch/x86/kvm/pmu.h @@ -281,6 +281,11 @@ static inline u64 kvm_vcpu_get_perf_caps(struct kvm_vcpu *vcpu) return vcpu->arch.perf_capabilities; } +static inline bool kvm_vcpu_has_perf_metrics(struct kvm_vcpu *vcpu) +{ + return kvm_vcpu_get_perf_caps(vcpu) & PERF_CAP_PERF_METRICS; +} + void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu); int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data); int kvm_pmu_check_rdpmc_early(struct kvm_vcpu *vcpu, unsigned int idx); -- 2.56.1