Re: [GIT PULL] KVM: x86: PMU changes for 6.9

From: Paolo Bonzini
Date: Mon Mar 11 2024 - 10:41:24 EST


On 3/8/24 23:36, Sean Christopherson wrote:
Lots of PMU fixes and cleanups, along with related selftests. The most notable
fix is to *not* disallow the use of fixed counters and event encodings just
because the CPU doesn't report support for the matching architectural event
encoding.

Note, the selftests changes have several annoying conflicts with "the" selftests
pull request that you'll also receive from me. I recommend merging that one
first, as I found it slightly easier to resolve the conflicts in that order.

P.S. I expect to send another PMU related pull request of 3-4 fixes at some
point during the merge window. But they're all small and urgent (if we had a
few more weeks for 6.8, I'd have tried to squeeze them into 6.8).

The following changes since commit 41bccc98fb7931d63d03f326a746ac4d429c1dd3:

Linux 6.8-rc2 (2024-01-28 17:01:12 -0800)

are available in the Git repository at:

https://github.com/kvm-x86/linux.git tags/kvm-x86-pmu-6.9

for you to fetch changes up to 812d432373f629eb8d6cb696ea6804fca1534efa:

KVM: x86/pmu: Explicitly check NMI from guest to reducee false positives (2024-02-26 15:57:22 -0800)

Pulled, thanks.

Paolo

----------------------------------------------------------------
KVM x86 PMU changes for 6.9:

- Fix several bugs where KVM speciously prevents the guest from utilizing
fixed counters and architectural event encodings based on whether or not
guest CPUID reports support for the _architectural_ encoding.

- Fix a variety of bugs in KVM's emulation of RDPMC, e.g. for "fast" reads,
priority of VMX interception vs #GP, PMC types in architectural PMUs, etc.

- Add a selftest to verify KVM correctly emulates RDMPC, counter availability,
and a variety of other PMC-related behaviors that depend on guest CPUID,
i.e. are difficult to validate via KVM-Unit-Tests.

- Zero out PMU metadata on AMD if the virtual PMU is disabled to avoid wasting
cycles, e.g. when checking if a PMC event needs to be synthesized when
skipping an instruction.

- Optimize triggering of emulated events, e.g. for "count instructions" events
when skipping an instruction, which yields a ~10% performance improvement in
VM-Exit microbenchmarks when a vPMU is exposed to the guest.

- Tighten the check for "PMI in guest" to reduce false positives if an NMI
arrives in the host while KVM is handling an IRQ VM-Exit.

----------------------------------------------------------------
Dapeng Mi (1):
KVM: selftests: Test top-down slots event in x86's pmu_counters_test

Jinrong Liang (7):
KVM: selftests: Add vcpu_set_cpuid_property() to set properties
KVM: selftests: Add pmu.h and lib/pmu.c for common PMU assets
KVM: selftests: Test Intel PMU architectural events on gp counters
KVM: selftests: Test Intel PMU architectural events on fixed counters
KVM: selftests: Test consistency of CPUID with num of gp counters
KVM: selftests: Test consistency of CPUID with num of fixed counters
KVM: selftests: Add functional test for Intel's fixed PMU counters

Like Xu (1):
KVM: x86/pmu: Explicitly check NMI from guest to reducee false positives

Sean Christopherson (32):
KVM: x86/pmu: Always treat Fixed counters as available when supported
KVM: x86/pmu: Allow programming events that match unsupported arch events
KVM: x86/pmu: Remove KVM's enumeration of Intel's architectural encodings
KVM: x86/pmu: Setup fixed counters' eventsel during PMU initialization
KVM: x86/pmu: Get eventsel for fixed counters from perf
KVM: x86/pmu: Don't ignore bits 31:30 for RDPMC index on AMD
KVM: x86/pmu: Prioritize VMX interception over #GP on RDPMC due to bad index
KVM: x86/pmu: Apply "fast" RDPMC only to Intel PMUs
KVM: x86/pmu: Disallow "fast" RDPMC for architectural Intel PMUs
KVM: x86/pmu: Treat "fixed" PMU type in RDPMC as index as a value, not flag
KVM: x86/pmu: Explicitly check for RDPMC of unsupported Intel PMC types
KVM: selftests: Drop the "name" param from KVM_X86_PMU_FEATURE()
KVM: selftests: Extend {kvm,this}_pmu_has() to support fixed counters
KVM: selftests: Expand PMU counters test to verify LLC events
KVM: selftests: Add a helper to query if the PMU module param is enabled
KVM: selftests: Add helpers to read integer module params
KVM: selftests: Query module param to detect FEP in MSR filtering test
KVM: selftests: Move KVM_FEP macro into common library header
KVM: selftests: Test PMC virtualization with forced emulation
KVM: selftests: Add a forced emulation variation of KVM_ASM_SAFE()
KVM: selftests: Add helpers for safe and safe+forced RDMSR, RDPMC, and XGETBV
KVM: selftests: Extend PMU counters test to validate RDPMC after WRMSR
KVM: x86/pmu: Zero out PMU metadata on AMD if PMU is disabled
KVM: x86/pmu: Add common define to capture fixed counters offset
KVM: x86/pmu: Move pmc_idx => pmc translation helper to common code
KVM: x86/pmu: Snapshot and clear reprogramming bitmap before reprogramming
KVM: x86/pmu: Add macros to iterate over all PMCs given a bitmap
KVM: x86/pmu: Process only enabled PMCs when emulating events in software
KVM: x86/pmu: Snapshot event selectors that KVM emulates in software
KVM: x86/pmu: Expand the comment about what bits are check emulating events
KVM: x86/pmu: Check eventsel first when emulating (branch) insns retired
KVM: x86/pmu: Avoid CPL lookup if PMC enabline for USER and KERNEL is the same

arch/x86/include/asm/kvm-x86-pmu-ops.h | 4 +-
arch/x86/include/asm/kvm_host.h | 11 +-
arch/x86/kvm/emulate.c | 2 +-
arch/x86/kvm/kvm_emulate.h | 2 +-
arch/x86/kvm/pmu.c | 163 ++++--
arch/x86/kvm/pmu.h | 57 +-
arch/x86/kvm/svm/pmu.c | 22 +-
arch/x86/kvm/vmx/nested.c | 2 +-
arch/x86/kvm/vmx/pmu_intel.c | 222 +++-----
arch/x86/kvm/x86.c | 15 +-
arch/x86/kvm/x86.h | 6 -
tools/testing/selftests/kvm/Makefile | 2 +
.../testing/selftests/kvm/include/kvm_util_base.h | 4 +
tools/testing/selftests/kvm/include/x86_64/pmu.h | 97 ++++
.../selftests/kvm/include/x86_64/processor.h | 148 +++--
tools/testing/selftests/kvm/lib/kvm_util.c | 62 ++-
tools/testing/selftests/kvm/lib/x86_64/pmu.c | 31 ++
tools/testing/selftests/kvm/lib/x86_64/processor.c | 15 +-
.../selftests/kvm/x86_64/pmu_counters_test.c | 620 +++++++++++++++++++++
.../selftests/kvm/x86_64/pmu_event_filter_test.c | 143 ++---
.../kvm/x86_64/smaller_maxphyaddr_emulation_test.c | 2 +-
.../selftests/kvm/x86_64/userspace_msr_exit_test.c | 29 +-
.../selftests/kvm/x86_64/vmx_pmu_caps_test.c | 2 +-
23 files changed, 1262 insertions(+), 399 deletions(-)
create mode 100644 tools/testing/selftests/kvm/include/x86_64/pmu.h
create mode 100644 tools/testing/selftests/kvm/lib/x86_64/pmu.c
create mode 100644 tools/testing/selftests/kvm/x86_64/pmu_counters_test.c