KVM: x86/pmu: Use binary search to check filtered events
v2 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. And I don't just mean erratum 1292, though that throws even more brokenness into the mix.
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.
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 | 306 ++++++++++++++++++ 6 files changed, 361 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
PATCH v2 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
@@ -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
PATCH v2 2/6 selftests: kvm/x86: Parameterize the CPUID vendor string check
In reply to Jim MattsonRefactor 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
@@ -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
PATCH v2 3/6 selftests: kvm/x86: Introduce is_amd_cpu()
In reply to Jim MattsonReplace 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
@@ -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
@@ -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 vcpuireturn 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
PATCH v2 4/6 selftests: kvm/x86: Export x86_family() for use outside of processor.c
In reply to Jim MattsonMove 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
@@ -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
@@ -1444,18 +1444,6 @@ struct kvm_cpuid2 *vcpu_get_supported_hv_cpuid(struct kvm_vm *vm, uint32_t vcpuireturn 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
PATCH v2 5/6 selftests: kvm/x86: Introduce x86_model()
In reply to Jim MattsonExtract 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
@@ -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
PATCH v2 6/6 selftests: kvm/x86: Add test for KVM_SET_PMU_EVENT_FILTER
In reply to Jim MattsonVerify 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 appears to be 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 | 306 ++++++++++++++++++ 3 files changed, 308 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c
tools/testing/selftests/kvm/.gitignore +1 -0
@@ -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
@@ -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 +306 -0
306 lines not on this page · raw message
Signature · 1 line
2.34.1.703.g22d0c6ccf7-goog
Re: [PATCH v2 6/6] selftests: kvm/x86: Add test for KVM_SET_PMU_EVENT_FILTER
Jim,
The patch set looks good to me. A couple comments and questions related just to this test are inline.
Quoted 7 lines · Jim Mattson
+ * Determining AMD support for a PMU event requires consulting the AMD
+ * PPR for the CPU or reference material derived therefrom.
+ */tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c new file +4
-0+static bool vcpu_supports_amd_zen_br_retired (void ) +{ +struct kvm_cpuid_entry2 *entry; +struct kvm_cpuid2 *cpuid;
tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c line 268
Quoted 10 lines
tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c new file +10
-0+ + cpuid =kvm_get_supported_cpuid (); + entry =kvm_get_supported_cpuid_index (1 ,0 ); +return entry && + ((x86_family (entry->eax) ==0x17 && + (x86_model (entry->eax) ==1 || +x86_model (entry->eax) ==0x31 )) || + (x86_family (entry->eax) ==0x19 && +x86_model (entry->eax) ==1 )); +}
The above function does not verify that the AMD host you are running on supports PMU. In particular, you might be running the KVM test suite within a guest. Is there a way to do that check here without direct access to MSRs? If not, maybe we need a KVM capability to query this information.
tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c line 283
Quoted 5 lines
tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c new file +5
-0+ r =kvm_check_cap (KVM_CAP_PMU_EVENT_FILTER ); +if (!r) { +print_skip ("KVM_CAP_PMU_EVENT_FILTER not supported" ); +exit (KSFT_SKIP ); + }
This capability is still supported even when PMU has been disabled by Like Xu's new module parameter. Should all the PMU related capabilities be gated behind that module parameter?
Dave Dunn
Re: [PATCH v2 6/6] selftests: kvm/x86: Add test for KVM_SET_PMU_EVENT_FILTER
Quoted 23 lines · David Dunn
Jim,
The patch set looks good to me. A couple comments and questions related just to this test are inline.
Quoted 15 lines · Jim Mattson
+ * Determining AMD support for a PMU event requires consulting the AMD
+ * PPR for the CPU or reference material derived therefrom.
+ */tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c new file +12
-0+static bool vcpu_supports_amd_zen_br_retired (void ) +{ +struct kvm_cpuid_entry2 *entry; +struct kvm_cpuid2 *cpuid; + + cpuid =kvm_get_supported_cpuid (); + entry =kvm_get_supported_cpuid_index (1 ,0 ); + return entry && + ((x86_family(entry->eax) == 0x17 && + (x86_model(entry->eax) == 1 || + x86_model(entry->eax) == 0x31)) || + (x86_family(entry->eax) == 0x19 &&
Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ x86_model(entry->eax) == 1)); +}The above function does not verify that the AMD host you are running on supports PMU. In particular, you might be running the KVM test suite within a guest. Is there a way to do that check here without direct access to MSRs? If not, maybe we need a KVM capability to query this information.
On Intel, if the parent hypervisor has constructed the CPUID:0AH leaf correctly, the test will skip because it can't find what it wants in CPUID:0AH, and the CPU family isn't what it is looking for on the AMD side.
On AMD, the test assumes that the PMU is there (as the specification requires). For the Zen line, a VM with vPMU enabled should report CPUID:80000001H:ECX.PerfCtrExtCore[bit 23] set, and a VM with vPMU disabled should report that bit clear.
In v3, I'm adding a PMU sanity check based on Linux's check_hw_exists().
Quoted 9 lines
tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c line 283
Quoted 5 lines
tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c new file +5
-0+ r =kvm_check_cap (KVM_CAP_PMU_EVENT_FILTER ); +if (!r) { +print_skip ("KVM_CAP_PMU_EVENT_FILTER not supported" ); +exit (KSFT_SKIP ); + }This capability is still supported even when PMU has been disabled by Like Xu's new module parameter. Should all the PMU related capabilities be gated behind that module parameter?
Perhaps. That's something for Like Xu to consider with the current rewrite of the module parameter.
Quoted 1 line
Dave Dunn
Quoted 12 lines · Jim Mattson
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. And I don't just mean erratum 1292, though that throws even more brokenness into the mix.
Quoted 6 lines
v1 -> v2
* Drop the check for "AMDisbetter!" inis_amd_cpu()[David Dunn]
* Drop the call tocpuid(0, 0)that fed the original check for CPU vendor string "AuthenticAMD" invm_compute_max_gfn().
* Simplify the inline asm in the selftest by using the compound literal for both input & output.
Queued, thanks.
Paolo
Quoted 17 lines
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: Introduceis_amd_cpu()
selftests: kvm/x86: Exportx86_family()for use outside ofprocessor.c
selftests: kvm/x86: Introducex86_model()
selftests: kvm/x86: Add test forKVM_SET_PMU_EVENT_FILTERarch/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 | 306 ++++++++++++++++++ 6 files changed, 361 insertions(+), 35 deletions(-)create mode 100644
tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c