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 -- 2.34.1.703.g22d0c6ccf7-goog