Skip to content

Commit 7b5c357

Browse files
sean-jcSasha Levin
authored andcommitted
KVM: x86/pmu: Zero out PMU metadata on AMD if PMU is disabled
[ Upstream commit f933b88 ] Move the purging of common PMU metadata from intel_pmu_refresh() to kvm_pmu_refresh(), and invoke the vendor refresh() hook if and only if the VM is supposed to have a vPMU. KVM already denies access to the PMU based on kvm->arch.enable_pmu, as get_gp_pmc_amd() returns NULL for all PMCs in that case, i.e. KVM already violates AMD's architecture by not virtualizing a PMU (kernels have long since learned to not panic when the PMU is unavailable). But configuring the PMU as if it were enabled causes unwanted side effects, e.g. calls to kvm_pmu_trigger_event() waste an absurd number of cycles due to the all_valid_pmc_idx bitmap being non-zero. Fixes: b1d66da ("KVM: x86/svm: Add module param to control PMU virtualization") Reported-by: Konstantin Khorenko <khorenko@virtuozzo.com> Closes: https://lore.kernel.org/all/20231109180646.2963718-2-khorenko@virtuozzo.com Link: https://lore.kernel.org/r/20231110022857.1273836-2-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com> Stable-dep-of: de120e1 ("KVM: x86/pmu: Set enable bits for GP counters in PERF_GLOBAL_CTRL at "RESET"") Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent f5cd731 commit 7b5c357

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

arch/x86/kvm/pmu.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,8 @@ static void kvm_pmu_reset(struct kvm_vcpu *vcpu)
741741
*/
742742
void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
743743
{
744+
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
745+
744746
if (KVM_BUG_ON(kvm_vcpu_has_run(vcpu), vcpu->kvm))
745747
return;
746748

@@ -750,8 +752,22 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
750752
*/
751753
kvm_pmu_reset(vcpu);
752754

753-
bitmap_zero(vcpu_to_pmu(vcpu)->all_valid_pmc_idx, X86_PMC_IDX_MAX);
754-
static_call(kvm_x86_pmu_refresh)(vcpu);
755+
pmu->version = 0;
756+
pmu->nr_arch_gp_counters = 0;
757+
pmu->nr_arch_fixed_counters = 0;
758+
pmu->counter_bitmask[KVM_PMC_GP] = 0;
759+
pmu->counter_bitmask[KVM_PMC_FIXED] = 0;
760+
pmu->reserved_bits = 0xffffffff00200000ull;
761+
pmu->raw_event_mask = X86_RAW_EVENT_MASK;
762+
pmu->global_ctrl_mask = ~0ull;
763+
pmu->global_status_mask = ~0ull;
764+
pmu->fixed_ctr_ctrl_mask = ~0ull;
765+
pmu->pebs_enable_mask = ~0ull;
766+
pmu->pebs_data_cfg_mask = ~0ull;
767+
bitmap_zero(pmu->all_valid_pmc_idx, X86_PMC_IDX_MAX);
768+
769+
if (vcpu->kvm->arch.enable_pmu)
770+
static_call(kvm_x86_pmu_refresh)(vcpu);
755771
}
756772

757773
void kvm_pmu_init(struct kvm_vcpu *vcpu)

arch/x86/kvm/vmx/pmu_intel.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -491,19 +491,6 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
491491
u64 counter_mask;
492492
int i;
493493

494-
pmu->nr_arch_gp_counters = 0;
495-
pmu->nr_arch_fixed_counters = 0;
496-
pmu->counter_bitmask[KVM_PMC_GP] = 0;
497-
pmu->counter_bitmask[KVM_PMC_FIXED] = 0;
498-
pmu->version = 0;
499-
pmu->reserved_bits = 0xffffffff00200000ull;
500-
pmu->raw_event_mask = X86_RAW_EVENT_MASK;
501-
pmu->global_ctrl_mask = ~0ull;
502-
pmu->global_status_mask = ~0ull;
503-
pmu->fixed_ctr_ctrl_mask = ~0ull;
504-
pmu->pebs_enable_mask = ~0ull;
505-
pmu->pebs_data_cfg_mask = ~0ull;
506-
507494
memset(&lbr_desc->records, 0, sizeof(lbr_desc->records));
508495

509496
/*
@@ -515,8 +502,9 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
515502
return;
516503

517504
entry = kvm_find_cpuid_entry(vcpu, 0xa);
518-
if (!entry || !vcpu->kvm->arch.enable_pmu)
505+
if (!entry)
519506
return;
507+
520508
eax.full = entry->eax;
521509
edx.full = entry->edx;
522510

0 commit comments

Comments
 (0)