Skip to content

Commit d2b321e

Browse files
committed
KVM: x86/pmu: Process only enabled PMCs when emulating events in software
Mask off disabled counters based on PERF_GLOBAL_CTRL *before* iterating over PMCs to emulate (branch) instruction required events in software. In the common case where the guest isn't utilizing the PMU, pre-checking for enabled counters turns a relatively expensive search into a few AND uops and a Jcc. Sadly, PMUs without PERF_GLOBAL_CTRL, e.g. most existing AMD CPUs, are out of luck as there is no way to check that a PMC isn't being used without checking the PMC's event selector. Cc: Konstantin Khorenko <khorenko@virtuozzo.com> Link: https://lore.kernel.org/r/20231110022857.1273836-7-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent e5a65d4 commit d2b321e

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

arch/x86/kvm/pmu.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,11 +847,20 @@ static inline bool cpl_is_matched(struct kvm_pmc *pmc)
847847

848848
void kvm_pmu_trigger_event(struct kvm_vcpu *vcpu, u64 perf_hw_id)
849849
{
850+
DECLARE_BITMAP(bitmap, X86_PMC_IDX_MAX);
850851
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
851852
struct kvm_pmc *pmc;
852853
int i;
853854

854-
kvm_for_each_pmc(pmu, pmc, i, pmu->all_valid_pmc_idx) {
855+
BUILD_BUG_ON(sizeof(pmu->global_ctrl) * BITS_PER_BYTE != X86_PMC_IDX_MAX);
856+
857+
if (!kvm_pmu_has_perf_global_ctrl(pmu))
858+
bitmap_copy(bitmap, pmu->all_valid_pmc_idx, X86_PMC_IDX_MAX);
859+
else if (!bitmap_and(bitmap, pmu->all_valid_pmc_idx,
860+
(unsigned long *)&pmu->global_ctrl, X86_PMC_IDX_MAX))
861+
return;
862+
863+
kvm_for_each_pmc(pmu, pmc, i, bitmap) {
855864
if (!pmc_event_is_allowed(pmc))
856865
continue;
857866

0 commit comments

Comments
 (0)