Skip to content

RFC KVM: SVM: Support for PMC virtualization

Sandipan Das kvm linux-perf-users 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 RFC Latest7 patches 2 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/7perf: Add a capability for hardware virtualized PMUsarch/x86/events/core.c arch/x86/include/asm/perf_event.h include/linux/perf_event.h+3 -00 replies
2/7x86/cpufeatures: Add PerfCtrVirt feature bitarch/x86/include/asm/cpufeatures.h+1 -00 replies
3/7perf/x86/amd/core: Set PERF_PMU_CAP_VIRTUALIZED_VPMUarch/x86/events/amd/core.c+3 -00 replies
4/7KVM: x86/pmu: Add support for hardware virtualized PMUsarch/x86/kvm/pmu.c arch/x86/kvm/pmu.h arch/x86/kvm/svm/pmu.c +3 more+89 -182 replies
5/7KVM: SVM: Add VMCB fields for PMC virtualizationarch/x86/include/asm/svm.h+9 -20 replies
6/7KVM: SVM: Add support for PMC virtualizationarch/x86/include/asm/svm.h arch/x86/kvm/svm/pmu.c arch/x86/kvm/svm/svm.c +1 more+154 -00 replies
7/7KVM: SVM: Adjust MSR index for legacy guestsarch/x86/kvm/svm/pmu.c+14 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [RFC PATCH 0/7] KVM: SVM: Support for PMC virtualization

AMD Zen 5 processors introduced PMC virtualization. When the feature is enabled, hardware can restore and save the guest PMU state at VM-Entry and VM-Exit respectively. The host PMU state still needs to be managed by software and this is done with the help of the Mediated PMU framework which can schedule the host perf events in or out.

This feature is documented under Section 15.39 in Volume 2 of the AMD64 Architecture Programmer’s Manual which can be found below.
https://bugzilla.kernel.org/attachment.cgi?id=306250

The guest PMU state is saved in the VMCB. Hence, struct kvm_pmu goes out of sync and does not have the latest PMU state. In some cases, such as during event filtering or instruction emulation, the VMCB needs to be accessed for reading or writing the current register state. This is done using host-initiated MSR accesses but the method may not be ideal and perhaps requires additional PMU ops to be introduced. The goal is to have data in vendor-specific structures like VMCB functions accessible to some of the common KVM x86 PMU functions.

Any feedback is appreciated.

Patch Summary
* The first three patches add new feature bits and capability flags to detect and advertise support for hardware PMU virtualization.
* The fourth patch extends Mediated PMU to make use of hardware PMU virtualization.
* The remaining patches add the vendor-specific changes for PMC virtualization.

TODOs
* Add appropriate KVM PMU ops to access MSR states from the VMCB.
* Make MSR states in the VMCB accessible via KVM_{GET,SET}_MSRS.
* Support for SEV-ES and SEV-SNP guests.
* Support for nested guests (nSVM).
* Support for the IRPerfCount register which is a free-running instructions retired counter.

Testing
* Used three different guest PMU configurations.
* PerfMonV2 capable - 6 counters with Global Control and Status registers (Zen 4 and later).
* PerfCtrExtCore capable - 6 counters but no Global Control and Status registers (Zen 3 and older).
* Legacy Guest - 4 counters only (pre-Zen, older than Family 15h).
* KVM Unit Tests passed.
* Perf Fuzzer passed.

The patches should be applied over v5 of the Mediated PMU series [1] along with an RDPMC interception fix [2].

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

Sandipan Das (7):
perf: Add a capability for hardware virtualized PMUs x86/cpufeatures: Add PerfCtrVirt feature bit perf/x86/amd/core: Set PERF_PMU_CAP_VIRTUALIZED_VPMU
KVM: x86/pmu: Add support for hardware virtualized PMUs
KVM: SVM: Add VMCB fields for PMC virtualization
KVM: SVM: Add support for PMC virtualization
KVM: SVM: Adjust MSR index for legacy guests

 arch/x86/events/amd/core.c         |   3 +
 arch/x86/events/core.c             |   1 +
 arch/x86/include/asm/cpufeatures.h |   1 +
 arch/x86/include/asm/perf_event.h  |   1 +
 arch/x86/include/asm/svm.h         |  12 ++-
 arch/x86/kvm/pmu.c                 |  94 ++++++++++++++++++-----
 arch/x86/kvm/pmu.h                 |   6 ++
 arch/x86/kvm/svm/pmu.c             | 115 +++++++++++++++++++++++++++++
 arch/x86/kvm/svm/svm.c             |  52 +++++++++++++
 arch/x86/kvm/svm/svm.h             |   1 +
 arch/x86/kvm/vmx/pmu_intel.c       |   1 +
 arch/x86/kvm/x86.c                 |   4 +
 arch/x86/kvm/x86.h                 |   1 +
 include/linux/perf_event.h         |   1 +
 14 files changed, 273 insertions(+), 20 deletions(-)
Signature · 1 line
2.43.0