Skip to content

Commit e35529f

Browse files
committed
KVM: x86/pmu: Check eventsel first when emulating (branch) insns retired
When triggering events, i.e. emulating PMC events in software, check for a matching event selector before checking the event is allowed. The "is allowed" check *might* be cheap, but it could also be very costly, e.g. if userspace has defined a large PMU event filter. The event selector check on the other hand is all but guaranteed to be <10 uops, e.g. looks something like: 0xffffffff8105e615 <+5>: movabs $0xf0000ffff,%rax 0xffffffff8105e61f <+15>: xor %rdi,%rsi 0xffffffff8105e622 <+18>: test %rax,%rsi 0xffffffff8105e625 <+21>: sete %al Link: https://lore.kernel.org/r/20231110022857.1273836-10-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent afda2d7 commit e35529f

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

arch/x86/kvm/pmu.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -857,9 +857,6 @@ void kvm_pmu_trigger_event(struct kvm_vcpu *vcpu, u64 eventsel)
857857
return;
858858

859859
kvm_for_each_pmc(pmu, pmc, i, bitmap) {
860-
if (!pmc_event_is_allowed(pmc))
861-
continue;
862-
863860
/*
864861
* Ignore checks for edge detect (all events currently emulated
865862
* but KVM are always rising edges), pin control (unsupported
@@ -874,11 +871,11 @@ void kvm_pmu_trigger_event(struct kvm_vcpu *vcpu, u64 eventsel)
874871
* might be wrong if they are defined in the future, but so
875872
* could ignoring them, so do the simple thing for now.
876873
*/
877-
if ((pmc->eventsel ^ eventsel) & AMD64_RAW_EVENT_MASK_NB)
874+
if (((pmc->eventsel ^ eventsel) & AMD64_RAW_EVENT_MASK_NB) ||
875+
!pmc_event_is_allowed(pmc) || !cpl_is_matched(pmc))
878876
continue;
879877

880-
if (cpl_is_matched(pmc))
881-
kvm_pmu_incr_counter(pmc);
878+
kvm_pmu_incr_counter(pmc);
882879
}
883880
}
884881
EXPORT_SYMBOL_GPL(kvm_pmu_trigger_event);

0 commit comments

Comments
 (0)