Skip to content

Commit f933b88

Browse files
committed
KVM: x86/pmu: Zero out PMU metadata on AMD if PMU is disabled
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 <[email protected]> Closes: https://lore.kernel.org/all/[email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent a8a37f5 commit f933b88

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
@@ -749,6 +749,8 @@ static void kvm_pmu_reset(struct kvm_vcpu *vcpu)
749749
*/
750750
void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
751751
{
752+
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
753+
752754
if (KVM_BUG_ON(kvm_vcpu_has_run(vcpu), vcpu->kvm))
753755
return;
754756

@@ -758,8 +760,22 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
758760
*/
759761
kvm_pmu_reset(vcpu);
760762

761-
bitmap_zero(vcpu_to_pmu(vcpu)->all_valid_pmc_idx, X86_PMC_IDX_MAX);
762-
static_call(kvm_x86_pmu_refresh)(vcpu);
763+
pmu->version = 0;
764+
pmu->nr_arch_gp_counters = 0;
765+
pmu->nr_arch_fixed_counters = 0;
766+
pmu->counter_bitmask[KVM_PMC_GP] = 0;
767+
pmu->counter_bitmask[KVM_PMC_FIXED] = 0;
768+
pmu->reserved_bits = 0xffffffff00200000ull;
769+
pmu->raw_event_mask = X86_RAW_EVENT_MASK;
770+
pmu->global_ctrl_mask = ~0ull;
771+
pmu->global_status_mask = ~0ull;
772+
pmu->fixed_ctr_ctrl_mask = ~0ull;
773+
pmu->pebs_enable_mask = ~0ull;
774+
pmu->pebs_data_cfg_mask = ~0ull;
775+
bitmap_zero(pmu->all_valid_pmc_idx, X86_PMC_IDX_MAX);
776+
777+
if (vcpu->kvm->arch.enable_pmu)
778+
static_call(kvm_x86_pmu_refresh)(vcpu);
763779
}
764780

765781
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
@@ -471,19 +471,6 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
471471
u64 counter_mask;
472472
int i;
473473

474-
pmu->nr_arch_gp_counters = 0;
475-
pmu->nr_arch_fixed_counters = 0;
476-
pmu->counter_bitmask[KVM_PMC_GP] = 0;
477-
pmu->counter_bitmask[KVM_PMC_FIXED] = 0;
478-
pmu->version = 0;
479-
pmu->reserved_bits = 0xffffffff00200000ull;
480-
pmu->raw_event_mask = X86_RAW_EVENT_MASK;
481-
pmu->global_ctrl_mask = ~0ull;
482-
pmu->global_status_mask = ~0ull;
483-
pmu->fixed_ctr_ctrl_mask = ~0ull;
484-
pmu->pebs_enable_mask = ~0ull;
485-
pmu->pebs_data_cfg_mask = ~0ull;
486-
487474
memset(&lbr_desc->records, 0, sizeof(lbr_desc->records));
488475

489476
/*
@@ -495,8 +482,9 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
495482
return;
496483

497484
entry = kvm_find_cpuid_entry(vcpu, 0xa);
498-
if (!entry || !vcpu->kvm->arch.enable_pmu)
485+
if (!entry)
499486
return;
487+
500488
eax.full = entry->eax;
501489
edx.full = entry->edx;
502490

0 commit comments

Comments
 (0)