KVM: x86/pmu: Avoid CPL lookup if PMC enabline for USER and KERNEL is the same
authorSean Christopherson <seanjc@google.com>
Fri, 10 Nov 2023 02:28:57 +0000 (18:28 -0800)
committerSean Christopherson <seanjc@google.com>
Thu, 1 Feb 2024 17:35:48 +0000 (09:35 -0800)
Don't bother querying the CPL if a PMC is (not) counting for both USER and
KERNEL, i.e. if the end result is guaranteed to be the same regardless of
the CPL.  Querying the CPL on Intel requires a VMREAD, i.e. isn't free,
and a single CMP+Jcc is cheap.

Link: https://lore.kernel.org/r/20231110022857.1273836-11-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/kvm/pmu.c

index 8d81f176ab7beaacf7d8ab8cf74534c1dd629ad3..c397b28e3d1b680788249daa32f36c12c80bd1a1 100644 (file)
@@ -838,6 +838,13 @@ static inline bool cpl_is_matched(struct kvm_pmc *pmc)
                select_user = config & 0x2;
        }
 
+       /*
+        * Skip the CPL lookup, which isn't free on Intel, if the result will
+        * be the same regardless of the CPL.
+        */
+       if (select_os == select_user)
+               return select_os;
+
        return (static_call(kvm_x86_get_cpl)(pmc->vcpu) == 0) ? select_os : select_user;
 }