Skip to contentSkip to conversation

KVM: x86/pmu: Use binary search to check filtered events

PATCH New Jim Mattson 1 participant 7 messages Jan 15, 2022

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v3 cover KVM: x86/pmu: Use binary search to check filtered events

This started out as a simple change to sort the (up to 300 element) PMU filtered event list and to use binary search rather than linear search to see if an event is in the list.

I thought it would be nice to add a directed test for the PMU event filter, and that's when things got complicated. The Intel side was fine, but the AMD side was a bit ugly, until I did a few refactorings. Imagine my dismay when I discovered that the PMU event filter works fine on the AMD side, but that fundamental PMU virtualization is broken.

I'm not referring to erratum 1292, though that throws even more brokenness into the mix. Apparently, a #VMEXIT counts as a "retired branch instruction." The Zen family PPRs do say, "This includes all types of architectural control flow changes, including exceptions and interrupts," so apparently everything is working as intended. However, this means that if the hypervisor doesn't adjust the counts, the results are not only different from bare metal, but they are non-deterministic as well (because a physical interrupt can occur at any time and bump the count up).

v1 -> v2
* Drop the check for "AMDisbetter!" in is_amd_cpu() [David Dunn]
* Drop the call to cpuid(0, 0) that fed the original check for CPU vendor string "AuthenticAMD" in vm_compute_max_gfn().
* Simplify the inline asm in the selftest by using the compound literal for both input & output.

v2 -> v3 [only the selftest is modified]
* Literally copy ARCH_PERFMON_EVENTSEL_{OS,ENABLE} from perf_event.h, rather than defining semantic equivalents.
* Copy cpuid10_e[ab]x from perf_event.h for improved readability of the code that checks CPUID.0AH.
* Preface the guest code with a PMU sanity check, so that the test will skip rather than fail if the PMU is non-functional (disabled by module parameter, unimplemented or disabled by parent hypervisor, &c). [David Dunn]
* Refactor and rename the Intel and AMD PMU checks.
* Use the is_amd_cpu() code from an earlier commit in the series (as well as the is_intel_cpu() code).
* Add a check for at least one general-purpose counter on the Intel side.
* Change each of the Zen family/model checks to include the sixteen model number range specified in AMD's "Revision Guide" for each generation of CPU, rather than the single model number specified in the associated PPRs.

Jim Mattson (6):
KVM: x86/pmu: Use binary search to check filtered events
selftests: kvm/x86: Parameterize the CPUID vendor string check
selftests: kvm/x86: Introduce is_amd_cpu()
selftests: kvm/x86: Export x86_family() for use outside of processor.c
selftests: kvm/x86: Introduce x86_model()
selftests: kvm/x86: Add test for KVM_SET_PMU_EVENT_FILTER

 arch/x86/kvm/pmu.c                            |  30 +-
 tools/testing/selftests/kvm/.gitignore        |   1 +
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../selftests/kvm/include/x86_64/processor.h  |  18 +
 .../selftests/kvm/lib/x86_64/processor.c      |  40 +-
 .../kvm/x86_64/pmu_event_filter_test.c        | 437 ++++++++++++++++++
 6 files changed, 492 insertions(+), 35 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c
Signature · 1 line
2.34.1.703.g22d0c6ccf7-goog
#1 lore raw

PATCH v3 1/6 KVM: x86/pmu: Use binary search to check filtered events

The PMU event filter may contain up to 300 events. Replace the linear search in reprogram_gp_counter() with a binary search.

---
 arch/x86/kvm/pmu.c | 30 +++++++++++++++++++-----------
 1 file changed, 19 insertions(+), 11 deletions(-)
arch/x86/kvm/pmu.c +19 -11
index e632693a2266..2c98f3ee8df4 100644
@@ -13,6 +13,8 @@
 #include <linux/types.h>
 #include <linux/kvm_host.h>
 #include <linux/perf_event.h>
+#include <linux/bsearch.h>
+#include <linux/sort.h>
 #include <asm/perf_event.h>
 #include "x86.h"
 #include "cpuid.h"
@@ -172,12 +174,16 @@ static bool pmc_resume_counter(struct kvm_pmc *pmc)
 	return true;
 }
 
+static int cmp_u64(const void *a, const void *b)
+{
+	return *(__u64 *)a - *(__u64 *)b;
+}
+
 void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel)
 {
 	unsigned config, type = PERF_TYPE_RAW;
 	struct kvm *kvm = pmc->vcpu->kvm;
 	struct kvm_pmu_event_filter *filter;
-	int i;
 	bool allow_event = true;
 
 	if (eventsel & ARCH_PERFMON_EVENTSEL_PIN_CONTROL)
@@ -192,16 +198,13 @@ void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel)
 
 	filter = srcu_dereference(kvm->arch.pmu_event_filter, &kvm->srcu);
 	if (filter) {
-		for (i = 0; i < filter->nevents; i++)
-			if (filter->events[i] ==
-			    (eventsel & AMD64_RAW_EVENT_MASK_NB))
-				break;
-		if (filter->action == KVM_PMU_EVENT_ALLOW &&
-		    i == filter->nevents)
-			allow_event = false;
-		if (filter->action == KVM_PMU_EVENT_DENY &&
-		    i < filter->nevents)
-			allow_event = false;
+		__u64 key = eventsel & AMD64_RAW_EVENT_MASK_NB;
+
+		if (bsearch(&key, filter->events, filter->nevents,
+			    sizeof(__u64), cmp_u64))
+			allow_event = filter->action == KVM_PMU_EVENT_ALLOW;
+		else
+			allow_event = filter->action == KVM_PMU_EVENT_DENY;
 	}
 	if (!allow_event)
 		return;
@@ -576,6 +579,11 @@ int kvm_vm_ioctl_set_pmu_event_filter(struct kvm *kvm, void __user *argp)
 	/* Ensure nevents can't be changed between the user copies. */
 	*filter = tmp;
 
+	/*
+	 * Sort the in-kernel list so that we can search it with bsearch.
+	 */
+	sort(&filter->events, filter->nevents, sizeof(__u64), cmp_u64, NULL);
+
 	mutex_lock(&kvm->lock);
 	filter = rcu_replace_pointer(kvm->arch.pmu_event_filter, filter,
 				     mutex_is_locked(&kvm->lock));
Signature · 1 line
2.34.1.703.g22d0c6ccf7-goog
#2 lore raw

PATCH v3 2/6 selftests: kvm/x86: Parameterize the CPUID vendor string check

In reply to Jim Mattson

Refactor is_intel_cpu() to make it easier to reuse the bulk of the code for other vendors in the future.

---
 tools/testing/selftests/kvm/lib/x86_64/processor.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/lib/x86_64/processor.c +7 -3
index eef7b34756d5..355a3f6f1970 100644
@@ -1198,10 +1198,10 @@ void vcpu_load_state(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_x86_state *s
 	}
 }
 
-bool is_intel_cpu(void)
+static bool cpu_vendor_string_is(const char *vendor)
 {
+	const uint32_t *chunk = (const uint32_t *)vendor;
 	int eax, ebx, ecx, edx;
-	const uint32_t *chunk;
 	const int leaf = 0;
 
 	__asm__ __volatile__(
@@ -1210,10 +1210,14 @@ bool is_intel_cpu(void)
 		  "=c"(ecx), "=d"(edx)
 		: /* input */ "0"(leaf), "2"(0));
 
-	chunk = (const uint32_t *)("GenuineIntel");
 	return (ebx == chunk[0] && edx == chunk[1] && ecx == chunk[2]);
 }
 
+bool is_intel_cpu(void)
+{
+	return cpu_vendor_string_is("GenuineIntel");
+}
+
 uint32_t kvm_get_cpuid_max_basic(void)
 {
 	return kvm_get_supported_cpuid_entry(0)->eax;
Signature · 1 line
2.34.1.703.g22d0c6ccf7-goog
#3 lore raw

PATCH v3 3/6 selftests: kvm/x86: Introduce is_amd_cpu()

In reply to Jim Mattson

Replace the one ad hoc "AuthenticAMD" CPUID vendor string comparison with a new function, is_amd_cpu().

---
 .../selftests/kvm/include/x86_64/processor.h   |  1 +
 .../selftests/kvm/lib/x86_64/processor.c       | 18 +++++++++---------
 2 files changed, 10 insertions(+), 9 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +1 -0
index 05e65ca1c30c..69eaf9a69bb7 100644
@@ -347,6 +347,7 @@ static inline unsigned long get_xmm(int n)
 }
 
 bool is_intel_cpu(void);
+bool is_amd_cpu(void);
 
 struct kvm_x86_state;
 struct kvm_x86_state *vcpu_save_state(struct kvm_vm *vm, uint32_t vcpuid);
tools/testing/selftests/kvm/lib/x86_64/processor.c +9 -9
index 355a3f6f1970..b969e38bc02e 100644
@@ -1218,6 +1218,14 @@ bool is_intel_cpu(void)
 	return cpu_vendor_string_is("GenuineIntel");
 }
 
+/*
+ * Exclude early K5 samples with a vendor string of "AMDisbetter!"
+ */
+bool is_amd_cpu(void)
+{
+	return cpu_vendor_string_is("AuthenticAMD");
+}
+
 uint32_t kvm_get_cpuid_max_basic(void)
 {
 	return kvm_get_supported_cpuid_entry(0)->eax;
@@ -1436,10 +1444,6 @@ struct kvm_cpuid2 *vcpu_get_supported_hv_cpuid(struct kvm_vm *vm, uint32_t vcpui
 	return cpuid;
 }
 
-#define X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx 0x68747541
-#define X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx 0x444d4163
-#define X86EMUL_CPUID_VENDOR_AuthenticAMD_edx 0x69746e65
-
 static inline unsigned x86_family(unsigned int eax)
 {
         unsigned int x86;
@@ -1461,11 +1465,7 @@ unsigned long vm_compute_max_gfn(struct kvm_vm *vm)
 	max_gfn = (1ULL << (vm->pa_bits - vm->page_shift)) - 1;
 
 	/* Avoid reserved HyperTransport region on AMD processors.  */
-	eax = ecx = 0;
-	cpuid(&eax, &ebx, &ecx, &edx);
-	if (ebx != X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx ||
-	    ecx != X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx ||
-	    edx != X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
+	if (!is_amd_cpu())
 		return max_gfn;
 
 	/* On parts with <40 physical address bits, the area is fully hidden */
Signature · 1 line
2.34.1.703.g22d0c6ccf7-goog
#4 lore raw

PATCH v3 4/6 selftests: kvm/x86: Export x86_family() for use outside of processor.c

In reply to Jim Mattson

Move this static inline function to processor.h, so that it can be used in individual tests, as needed.

Opportunistically replace the bare 'unsigned' with 'unsigned int.'

---
 .../testing/selftests/kvm/include/x86_64/processor.h | 12 ++++++++++++
 tools/testing/selftests/kvm/lib/x86_64/processor.c   | 12 ------------
 2 files changed, 12 insertions(+), 12 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +12 -0
index 69eaf9a69bb7..c5306e29edd4 100644
@@ -349,6 +349,18 @@ static inline unsigned long get_xmm(int n)
 bool is_intel_cpu(void);
 bool is_amd_cpu(void);
 
+static inline unsigned int x86_family(unsigned int eax)
+{
+        unsigned int x86;
+
+        x86 = (eax >> 8) & 0xf;
+
+        if (x86 == 0xf)
+                x86 += (eax >> 20) & 0xff;
+
+        return x86;
+}
+
 struct kvm_x86_state;
 struct kvm_x86_state *vcpu_save_state(struct kvm_vm *vm, uint32_t vcpuid);
 void vcpu_load_state(struct kvm_vm *vm, uint32_t vcpuid,
tools/testing/selftests/kvm/lib/x86_64/processor.c +0 -12
index b969e38bc02e..286ae9605501 100644
@@ -1444,18 +1444,6 @@ struct kvm_cpuid2 *vcpu_get_supported_hv_cpuid(struct kvm_vm *vm, uint32_t vcpui
 	return cpuid;
 }
 
-static inline unsigned x86_family(unsigned int eax)
-{
-        unsigned int x86;
-
-        x86 = (eax >> 8) & 0xf;
-
-        if (x86 == 0xf)
-                x86 += (eax >> 20) & 0xff;
-
-        return x86;
-}
-
 unsigned long vm_compute_max_gfn(struct kvm_vm *vm)
 {
 	const unsigned long num_ht_pages = 12 << (30 - vm->page_shift); /* 12 GiB */
Signature · 1 line
2.34.1.703.g22d0c6ccf7-goog
#5 lore raw

PATCH v3 5/6 selftests: kvm/x86: Introduce x86_model()

In reply to Jim Mattson

Extract the x86 model number from CPUID.01H:EAX.

---
 tools/testing/selftests/kvm/include/x86_64/processor.h | 5 +++++
 1 file changed, 5 insertions(+)
tools/testing/selftests/kvm/include/x86_64/processor.h +5 -0
index c5306e29edd4..b723163ca9ba 100644
@@ -361,6 +361,11 @@ static inline unsigned int x86_family(unsigned int eax)
         return x86;
 }
 
+static inline unsigned int x86_model(unsigned int eax)
+{
+	return ((eax >> 12) & 0xf0) | ((eax >> 4) & 0x0f);
+}
+
 struct kvm_x86_state;
 struct kvm_x86_state *vcpu_save_state(struct kvm_vm *vm, uint32_t vcpuid);
 void vcpu_load_state(struct kvm_vm *vm, uint32_t vcpuid,
Signature · 1 line
2.34.1.703.g22d0c6ccf7-goog
#6 lore raw

PATCH v3 6/6 selftests: kvm/x86: Add test for KVM_SET_PMU_EVENT_FILTER

In reply to Jim Mattson

Verify that the PMU event filter works as expected.

Note that the virtual PMU doesn't work as expected on AMD Zen CPUs (an intercepted rdmsr is counted as a retired branch instruction), but the PMU event filter does work.

---
 tools/testing/selftests/kvm/.gitignore        |   1 +
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../kvm/x86_64/pmu_event_filter_test.c        | 438 ++++++++++++++++++
 3 files changed, 440 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c
tools/testing/selftests/kvm/.gitignore +1 -0
index 8c129961accf..7834e03ab159 100644
@@ -22,6 +22,7 @@
 /x86_64/mmio_warning_test
 /x86_64/mmu_role_test
 /x86_64/platform_info_test
+/x86_64/pmu_event_filter_test
 /x86_64/set_boot_cpu_id
 /x86_64/set_sregs_test
 /x86_64/sev_migrate_tests
tools/testing/selftests/kvm/Makefile +1 -0
index c407ebbec2c1..899413a6588f 100644
@@ -56,6 +56,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/kvm_pv_test
 TEST_GEN_PROGS_x86_64 += x86_64/mmio_warning_test
 TEST_GEN_PROGS_x86_64 += x86_64/mmu_role_test
 TEST_GEN_PROGS_x86_64 += x86_64/platform_info_test
+TEST_GEN_PROGS_x86_64 += x86_64/pmu_event_filter_test
 TEST_GEN_PROGS_x86_64 += x86_64/set_boot_cpu_id
 TEST_GEN_PROGS_x86_64 += x86_64/set_sregs_test
 TEST_GEN_PROGS_x86_64 += x86_64/smm_test
tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c new file +438 -0

438 lines not on this page · raw message

Signature · 1 line
2.34.1.703.g22d0c6ccf7-goog