Skip to content

Commit 1647b52

Browse files
committed
KVM: x86/pmu: Reset the PMU, i.e. stop counters, before refreshing
Stop all counters and release all perf events before refreshing the vPMU, i.e. before reconfiguring the vPMU to respond to changes in the vCPU model. Clear need_cleanup in kvm_pmu_reset() as well so that KVM doesn't prematurely stop counters, e.g. if KVM enters the guest and enables counters before the vCPU is scheduled out. Cc: stable@vger.kernel.org Reviewed-by: Dapeng Mi <dapeng1.mi@linux.intel.com> Link: https://lore.kernel.org/r/20231103230541.352265-3-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent cbb359d commit 1647b52

1 file changed

Lines changed: 22 additions & 13 deletions

File tree

arch/x86/kvm/pmu.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -657,25 +657,14 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
657657
return 0;
658658
}
659659

660-
/* refresh PMU settings. This function generally is called when underlying
661-
* settings are changed (such as changes of PMU CPUID by guest VMs), which
662-
* should rarely happen.
663-
*/
664-
void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
665-
{
666-
if (KVM_BUG_ON(kvm_vcpu_has_run(vcpu), vcpu->kvm))
667-
return;
668-
669-
bitmap_zero(vcpu_to_pmu(vcpu)->all_valid_pmc_idx, X86_PMC_IDX_MAX);
670-
static_call(kvm_x86_pmu_refresh)(vcpu);
671-
}
672-
673660
void kvm_pmu_reset(struct kvm_vcpu *vcpu)
674661
{
675662
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
676663
struct kvm_pmc *pmc;
677664
int i;
678665

666+
pmu->need_cleanup = false;
667+
679668
bitmap_zero(pmu->reprogram_pmi, X86_PMC_IDX_MAX);
680669

681670
for_each_set_bit(i, pmu->all_valid_pmc_idx, X86_PMC_IDX_MAX) {
@@ -695,6 +684,26 @@ void kvm_pmu_reset(struct kvm_vcpu *vcpu)
695684
static_call_cond(kvm_x86_pmu_reset)(vcpu);
696685
}
697686

687+
688+
/*
689+
* Refresh the PMU configuration for the vCPU, e.g. if userspace changes CPUID
690+
* and/or PERF_CAPABILITIES.
691+
*/
692+
void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
693+
{
694+
if (KVM_BUG_ON(kvm_vcpu_has_run(vcpu), vcpu->kvm))
695+
return;
696+
697+
/*
698+
* Stop/release all existing counters/events before realizing the new
699+
* vPMU model.
700+
*/
701+
kvm_pmu_reset(vcpu);
702+
703+
bitmap_zero(vcpu_to_pmu(vcpu)->all_valid_pmc_idx, X86_PMC_IDX_MAX);
704+
static_call(kvm_x86_pmu_refresh)(vcpu);
705+
}
706+
698707
void kvm_pmu_init(struct kvm_vcpu *vcpu)
699708
{
700709
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);

0 commit comments

Comments
 (0)