KVM: x86/pmu: Check eventsel first when emulating (branch) insns retired
authorSean Christopherson <seanjc@google.com>
Fri, 10 Nov 2023 02:28:56 +0000 (18:28 -0800)
committerSean Christopherson <seanjc@google.com>
Thu, 1 Feb 2024 17:35:48 +0000 (09:35 -0800)
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>
arch/x86/kvm/pmu.c

index d891a954b45ac9365b72d1e604458825efd7bcab..8d81f176ab7beaacf7d8ab8cf74534c1dd629ad3 100644 (file)
@@ -857,9 +857,6 @@ void kvm_pmu_trigger_event(struct kvm_vcpu *vcpu, u64 eventsel)
                return;
 
        kvm_for_each_pmc(pmu, pmc, i, bitmap) {
-               if (!pmc_event_is_allowed(pmc))
-                       continue;
-
                /*
                 * Ignore checks for edge detect (all events currently emulated
                 * but KVM are always rising edges), pin control (unsupported
@@ -874,11 +871,11 @@ void kvm_pmu_trigger_event(struct kvm_vcpu *vcpu, u64 eventsel)
                 * might be wrong if they are defined in the future, but so
                 * could ignoring them, so do the simple thing for now.
                 */
-               if ((pmc->eventsel ^ eventsel) & AMD64_RAW_EVENT_MASK_NB)
+               if (((pmc->eventsel ^ eventsel) & AMD64_RAW_EVENT_MASK_NB) ||
+                   !pmc_event_is_allowed(pmc) || !cpl_is_matched(pmc))
                        continue;
 
-               if (cpl_is_matched(pmc))
-                       kvm_pmu_incr_counter(pmc);
+               kvm_pmu_incr_counter(pmc);
        }
 }
 EXPORT_SYMBOL_GPL(kvm_pmu_trigger_event);