Skip to content

KVM: x86: Add support for mediated vPMUs

Sean Christopherson kvm kvm-riscv kvmarm linux-arm-kernel linux-perf-users linux-riscv lkml loongarch 2 versions – Applied

Status from this message: The following commit has been merged into the perf/core branch of tip:

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v6 Latest44 patches 35 repliesApplied
  2. v544 patches 33 repliesSuperseded

Changes in v6 from the cover letter

- Add back support for pre-SPR Intel CPUs, i.e. CPUs with PMU v4 but not "save PERF_GLOBAL_CTRL on VM-Exit". See
https://lore.kernel.org/all/[email protected] for details.
- Add an x86-only API to update the LVTPC entry on load/put of a mediated vPMU. [PeterZ]
- Don't tie RDPMC interception to PERF_GLOBAL_CTRL interception, as AMD CPUs have scenarios where KVM needs to intercept PERF_GLOBAL_CTRL, but not all PMC accesses. [Sandipan]
- Fix a s/Turing/Turin typo. [Manali]
- Collect tags. [Anup, Sandipan, Xudong]

Patches in v6 Applied

#Subject and filesSizeRepliesApplied
1/44perf: Skip pmu_ctx based on event_typekernel/events/core.c+40 -340 replies
2/44perf: Add generic exclude_guest supportkernel/events/core.c+8 -70 replies
3/44perf: Move security_perf_event_free() call to __free_event()kernel/events/core.c+2 -20 replies
4/44perf: Add APIs to create/release mediated guest vPMUsarch/x86/kvm/Kconfig include/linux/perf_event.h init/Kconfig +1 more+93 -07 replies
5/44perf: Clean up perf ctx timeinclude/linux/perf_event.h kernel/events/core.c+39 -440 replies
6/44perf: Add a EVENT_GUEST flaginclude/linux/perf_event.h kernel/events/core.c+186 -520 replies
7/44perf: Add APIs to load/put guest mediated PMU contextinclude/linux/perf_event.h kernel/events/core.c+63 -00 replies
8/44perf/x86/core: Register a new vector for handling mediated guest PMIsarch/x86/entry/entry_fred.c arch/x86/include/asm/hardirq.h arch/x86/include/asm/idtentry.h +7 more+55 -40 replies
9/44perf/x86/core: Add APIs to switch to/from mediated PMI vector (for KVM)arch/x86/events/core.c arch/x86/include/asm/perf_event.h+37 -00 replies
10/44perf/x86/core: Do not set bit width for unavailable countersarch/x86/events/core.c+2 -20 replies
11/44perf/x86/core: Plumb mediated PMU capability from x86_pmu to x86_pmu_caparch/x86/events/core.c arch/x86/include/asm/perf_event.h+2 -00 replies
12/44perf/x86/intel: Support PERF_PMU_CAP_MEDIATED_VPMUarch/x86/events/intel/core.c+5 -00 replies
13/44perf/x86/amd: Support PERF_PMU_CAP_MEDIATED_VPMU for AMD hostarch/x86/events/amd/core.c+2 -06 replies
14/44KVM: Add a simplified wrapper for registering perf callbacksarch/arm64/kvm/arm.c arch/loongarch/kvm/main.c arch/riscv/kvm/main.c +3 more+16 -80 replies
15/44KVM: x86/pmu: Snapshot host (i.e. perf's) reported PMU capabilitiesarch/x86/kvm/pmu.c+2 -02 replies
16/44KVM: x86/pmu: Start stubbing in mediated PMU supportarch/x86/include/asm/kvm_host.h arch/x86/kvm/pmu.c arch/x86/kvm/pmu.h +2 more+48 -20 replies
17/44KVM: x86/pmu: Implement Intel mediated PMU requirements and constraintsarch/x86/kvm/vmx/capabilities.h arch/x86/kvm/vmx/pmu_intel.c arch/x86/kvm/vmx/vmx.c+21 -20 replies
18/44KVM: x86/pmu: Implement AMD mediated PMU requirementsarch/x86/kvm/svm/pmu.c+8 -00 replies
19/44KVM: x86/pmu: Register PMI handler for mediated vPMUarch/x86/kvm/pmu.c arch/x86/kvm/pmu.h arch/x86/kvm/x86.c+14 -10 replies
20/44KVM: x86/pmu: Disable RDPMC interception for compatible mediated vPMUarch/x86/kvm/pmu.c arch/x86/kvm/pmu.h arch/x86/kvm/svm/svm.c +2 more+40 -00 replies
21/44KVM: x86/pmu: Load/save GLOBAL_CTRL via entry/exit fields for mediated PMUarch/x86/include/asm/kvm-x86-pmu-ops.h arch/x86/include/asm/vmx.h arch/x86/kvm/pmu.c +5 more+78 -60 replies
22/44KVM: x86/pmu: Disable interception of select PMU MSRs for mediated vPMUsarch/x86/kvm/pmu.c arch/x86/kvm/pmu.h arch/x86/kvm/svm/svm.c +3 more+128 -370 replies
23/44KVM: x86/pmu: Bypass perf checks when emulating mediated PMU counter accessesarch/x86/kvm/pmu.c arch/x86/kvm/pmu.h+8 -00 replies
24/44KVM: x86/pmu: Introduce eventsel_hw to prepare for pmu event filteringarch/x86/include/asm/kvm_host.h arch/x86/kvm/pmu.c arch/x86/kvm/svm/pmu.c +1 more+10 -20 replies
25/44KVM: x86/pmu: Reprogram mediated PMU event selectors on event filter updatesarch/x86/kvm/pmu.c+24 -00 replies
26/44KVM: x86/pmu: Always stuff GuestOnly=1,HostOnly=0 for mediated PMCs on AMDarch/x86/kvm/svm/pmu.c+2 -10 replies
27/44KVM: x86/pmu: Load/put mediated PMU context when entering/exiting guestarch/x86/include/asm/kvm-x86-pmu-ops.h arch/x86/include/asm/msr-index.h arch/x86/kvm/pmu.c +5 more+225 -30 replies
28/44KVM: x86/pmu: Disallow emulation in the fastpath if mediated PMCs are activearch/x86/kvm/pmu.h arch/x86/kvm/x86.c+19 -00 replies
29/44KVM: x86/pmu: Handle emulated instruction for mediated vPMUarch/x86/kvm/pmu.c+37 -20 replies
30/44KVM: nVMX: Add macros to simplify nested MSR interception settingarch/x86/kvm/vmx/nested.c+19 -160 replies
31/44KVM: nVMX: Disable PMU MSR interception as appropriate while running L2arch/x86/kvm/vmx/nested.c+30 -00 replies
32/44KVM: nSVM: Disable PMU MSR interception as appropriate while running L2arch/x86/kvm/svm/nested.c+17 -10 replies
33/44KVM: x86/pmu: Expose enable_mediated_pmu parameter to user spaceDocumentation/admin-guide/kernel-parameters.txt arch/x86/kvm/svm/svm.c arch/x86/kvm/vmx/vmx.c+53 -00 replies
34/44KVM: x86/pmu: Elide WRMSRs when loading guest PMCs if values already matcharch/x86/kvm/pmu.c+4 -20 replies
35/44KVM: VMX: Drop intermediate "guest" field from msr_autostorearch/x86/kvm/vmx/nested.c arch/x86/kvm/vmx/vmx.c arch/x86/kvm/vmx/vmx.h+7 -91 reply
36/44KVM: nVMX: Don't update msr_autostore count when saving TSC for vmcs12arch/x86/kvm/vmx/nested.c arch/x86/kvm/vmx/vmx.c arch/x86/kvm/vmx/vmx.h+24 -510 replies
37/44KVM: VMX: Dedup code for removing MSR from VMCS's auto-load listarch/x86/kvm/vmx/vmx.c+16 -153 replies
38/44KVM: VMX: Drop unused @entry_only param from add_atomic_switch_msr()arch/x86/kvm/vmx/vmx.c+4 -91 reply
39/44KVM: VMX: Bug the VM if either MSR auto-load list is fullarch/x86/kvm/vmx/vmx.c+4 -54 replies
40/44KVM: VMX: Set MSR index auto-load entry if and only if entry is "new"arch/x86/kvm/vmx/vmx.c+2 -21 reply
41/44KVM: VMX: Compartmentalize adding MSRs to host vs. guest auto-load listarch/x86/kvm/vmx/vmx.c+12 -111 reply
42/44KVM: VMX: Dedup code for adding MSR to VMCS's auto listarch/x86/kvm/vmx/vmx.c+19 -224 replies
43/44KVM: VMX: Initialize vmcs01.VM_EXIT_MSR_STORE_ADDR with list addressarch/x86/kvm/vmx/vmx.c+1 -00 replies
44/44KVM: VMX: Add mediated PMU support for CPUs without "save perf global ctrl"arch/x86/kvm/vmx/nested.c arch/x86/kvm/vmx/pmu_intel.c arch/x86/kvm/vmx/vmx.c+52 -132 replies

Reviews trailers per patch

12 of 44 patches have a Reviewed-by or Acked-by.

PatchYongwei MaXudong HaoAnup PatelSandipan DasDapeng MiManali Shukla
SeriesT-b
1/44T-b T-b
2/44T-b
3/44T-b
4/44T-b
5/44T-b
6/44T-b
7/44T-b
8/44T-b
9/44
10/44T-b
11/44T-b
12/44T-b
13/44T-b
14/44T-b A-b
15/44T-b R-b
16/44T-b
17/44T-b
18/44T-b R-b
19/44T-b
20/44T-b
21/44T-b
22/44T-b
23/44T-b R-b
24/44T-b
25/44T-b
26/44T-b
27/44T-b
28/44T-b R-b
29/44T-b
30/44T-b
31/44T-b
32/44T-b
33/44T-b
34/44T-b
35/44R-b
36/44
37/44
38/44R-b
39/44R-b
40/44R-b
41/44R-b
42/44R-b
43/44
44/44R-b T-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 6 reviewers.

Read the v6 thread

Cover letter [PATCH v6 00/44] KVM: x86: Add support for mediated vPMUs

This series is based on 'https://github.com/kvm-x86/linux next', but except for one minor conflict in perf should apply on Linus' tree once the KVM 6.19 pull request lands. I considered waiting until 6.19-rc1 to post this, but I've had this in a "ready" state for a few weeks and want to get it out there.

My hope/plan is that the perf changes will go through the tip tree with a stable tag/branch, and the KVM changes will go the kvm-x86 tree.

Non-x86 KVM folks, y'all are getting Cc'd due to minor changes in "KVM: Add a simplified wrapper for registering perf callbacks".

The full set is also available at:

https://github.com/sean-jc/linux.git tags/mediated-vpmu-v6

Add support for mediated vPMUs in KVM x86, where "mediated" aligns with the standard definition of intercepting control operations (e.g. event selectors), while allowing the guest to perform data operations (e.g. read PMCs, toggle counters on/off) without KVM getting involed.

For an in-depth description of the what and why, please see the cover letter from the original RFC:

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

All KVM tests pass (or fail the same before and after), and I've manually verified MSR/PMC are passed through as expected, but I haven't done much at all to actually utilize the PMU in a guest.

Despite merging almost all of the tangentially related prep work for 6.19, the patch count remains the same as v5 thanks to the VMX MSR auto-store cleanup and support for pre-SPR Intel CPUs.

v6:
- Add back support for pre-SPR Intel CPUs, i.e. CPUs with PMU v4 but not "save PERF_GLOBAL_CTRL on VM-Exit". See
https://lore.kernel.org/all/[email protected] for details.
- Add an x86-only API to update the LVTPC entry on load/put of a mediated vPMU. [PeterZ]
- Don't tie RDPMC interception to PERF_GLOBAL_CTRL interception, as AMD CPUs have scenarios where KVM needs to intercept PERF_GLOBAL_CTRL, but not all PMC accesses. [Sandipan]
- Fix a s/Turing/Turin typo. [Manali]
- Collect tags. [Anup, Sandipan, Xudong]

v5:
- https://lore.kernel.org/all/[email protected]
- Add a patch to call security_perf_event_free() from __free_event() instead of _free_event() (necessitated by the __cleanup() changes).
- Add CONFIG_PERF_GUEST_MEDIATED_PMU to guard the new perf functionality.
- Ensure the PMU is fully disabled in perf_{load,put}_guest_context() when when switching between guest and host context. [Kan, Namhyung]
- Route the new system IRQ, PERF_GUEST_MEDIATED_PMI_VECTOR, through perf, not KVM, and play nice with FRED.
- Rename and combine perf_{guest,host}_{enter,exit}() to a single set of APIs, perf_{load,put}_guest_context().
- Rename perf_{get,put}_mediated_pmu() to perf_{create,release}_mediated_pmu() to (hopefully) better differentiate them from perf_{load,put}_guest_context().
- Change the param to the load/put APIs from "u32 guest_lvtpc" to "unsigned long data" to decouple arch code as much as possible. E.g. if a non-x86 arch were to ever support a mediated vPMU, @data could be used to pass a pointer to a struct.
- Use pmu->version to detect if a vCPU has a mediated PMU.
- Use a kvm_x86_ops hook to check for mediated PMU support.
- Cull "passthrough" from as many places as I could find.
- Improve the changelog/documentation related to RDPMC interception.
- Check harware capabilities, not KVM capabilities, when calculating MSR and RDPMC intercepts.
- Rework intercept (re)calculation to use a request and the existing (well, will be existing as of 6.17-rc1) vendor hooks for recalculating intercepts.
- Always read PERF_GLOBAL_CTRL on VM-Exit if writes weren't intercepted while running the vCPU.
- Call setup_vmcs_config() before kvm_x86_vendor_init() so that the golden VMCS configuration is known before kvm_init_pmu_capability() is called.
- Keep as much refresh/init code in common x86 as possible.
- Context switch PMCs and event selectors in common x86, not vendor code.
- Bail from the VM-Exit fastpath if the guest is counting instructions retired and the mediated PMU is enabled (because guest state hasn't yet been synchronized with hardware).
- Don't require an userspace to opt-in via KVM_CAP_PMU_CAPABILITY, and instead automatically "create" a mediated PMU on the first KVM_CREATE_VCPU call if the VM has an in-kernel local APIC.
- Add entries in kernel-parameters.txt for the PMU params.
- Add a patch to elide PMC writes when possible.
- Many more fixups and tweaks...

v4:
- https://lore.kernel.org/all/[email protected]
- Rebase whole patchset on 6.14-rc3 base.
- Address Peter's comments on Perf part.
- Address Sean's comments on KVM part.
* Change key word "passthrough" to "mediated" in all patches
* Change static enabling to user space dynamic enabling via KVM_CAP_PMU_CAPABILITY.
* Only support GLOBAL_CTRL save/restore with VMCS exec_ctrl, drop the MSR save/retore list support for GLOBAL_CTRL, thus the support of mediated vPMU is constrained to SapphireRapids and later CPUs on Intel side.
* Merge some small changes into a single patch.
- Address Sandipan's comment on invalid pmu pointer.
- Add back "eventsel_hw" and "fixed_ctr_ctrl_hw" to avoid to directly manipulate pmc->eventsel and pmu->fixed_ctr_ctrl.

v3: https://lore.kernel.org/all/[email protected]
v2: https://lore.kernel.org/all/[email protected]
v1: https://lore.kernel.org/all/[email protected]

Dapeng Mi (11):
KVM: x86/pmu: Start stubbing in mediated PMU support
KVM: x86/pmu: Implement Intel mediated PMU requirements and
constraints
KVM: x86/pmu: Disable RDPMC interception for compatible mediated vPMU
KVM: x86/pmu: Load/save GLOBAL_CTRL via entry/exit fields for mediated
PMU
KVM: x86/pmu: Disable interception of select PMU MSRs for mediated
vPMUs
KVM: x86/pmu: Bypass perf checks when emulating mediated PMU counter
accesses
KVM: x86/pmu: Reprogram mediated PMU event selectors on event filter
updates
KVM: x86/pmu: Load/put mediated PMU context when entering/exiting
guest
KVM: x86/pmu: Handle emulated instruction for mediated vPMU
KVM: nVMX: Add macros to simplify nested MSR interception setting
KVM: x86/pmu: Expose enable_mediated_pmu parameter to user space

Kan Liang (7):
perf: Skip pmu_ctx based on event_type
perf: Add generic exclude_guest support
perf: Add APIs to create/release mediated guest vPMUs
perf: Clean up perf ctx time
perf: Add a EVENT_GUEST flag
perf: Add APIs to load/put guest mediated PMU context perf/x86/intel: Support PERF_PMU_CAP_MEDIATED_VPMU

Mingwei Zhang (3):
perf/x86/core: Plumb mediated PMU capability from x86_pmu to

    x86_pmu_cap
  KVM: x86/pmu: Introduce eventsel_hw to prepare for pmu event filtering
  KVM: nVMX: Disable PMU MSR interception as appropriate while running
    L2

Sandipan Das (3):
perf/x86/core: Do not set bit width for unavailable counters perf/x86/amd: Support PERF_PMU_CAP_MEDIATED_VPMU for AMD host
KVM: x86/pmu: Always stuff GuestOnly=1,HostOnly=0 for mediated PMCs on
AMD

Sean Christopherson (19):
perf: Move security_perf_event_free() call to __free_event() perf/x86/core: Register a new vector for handling mediated guest PMIs perf/x86/core: Add APIs to switch to/from mediated PMI vector (for
KVM)
KVM: Add a simplified wrapper for registering perf callbacks
KVM: x86/pmu: Snapshot host (i.e. perf's) reported PMU capabilities
KVM: x86/pmu: Implement AMD mediated PMU requirements
KVM: x86/pmu: Disallow emulation in the fastpath if mediated PMCs are
active
KVM: nSVM: Disable PMU MSR interception as appropriate while running

    L2

KVM: x86/pmu: Elide WRMSRs when loading guest PMCs if values already
match
KVM: VMX: Drop intermediate "guest" field from msr_autostore
KVM: nVMX: Don't update msr_autostore count when saving TSC for vmcs12
KVM: VMX: Dedup code for removing MSR from VMCS's auto-load list
KVM: VMX: Drop unused @entry_only param from add_atomic_switch_msr()
KVM: VMX: Bug the VM if either MSR auto-load list is full
KVM: VMX: Set MSR index auto-load entry if and only if entry is "new"
KVM: VMX: Compartmentalize adding MSRs to host vs. guest auto-load
list
KVM: VMX: Dedup code for adding MSR to VMCS's auto list
KVM: VMX: Initialize vmcs01.VM_EXIT_MSR_STORE_ADDR with list address
KVM: VMX: Add mediated PMU support for CPUs without "save perf global
ctrl"

Xiong Zhang (1):
KVM: x86/pmu: Register PMI handler for mediated vPMU

 .../admin-guide/kernel-parameters.txt         |  49 ++
 arch/arm64/kvm/arm.c                          |   2 +-
 arch/loongarch/kvm/main.c                     |   2 +-
 arch/riscv/kvm/main.c                         |   2 +-
 arch/x86/entry/entry_fred.c                   |   1 +
 arch/x86/events/amd/core.c                    |   2 +
 arch/x86/events/core.c                        |  37 +-
 arch/x86/events/intel/core.c                  |   5 +
 arch/x86/include/asm/hardirq.h                |   3 +
 arch/x86/include/asm/idtentry.h               |   6 +
 arch/x86/include/asm/irq_vectors.h            |   4 +-
 arch/x86/include/asm/kvm-x86-pmu-ops.h        |   4 +
 arch/x86/include/asm/kvm_host.h               |   3 +
 arch/x86/include/asm/msr-index.h              |   1 +
 arch/x86/include/asm/perf_event.h             |   6 +
 arch/x86/include/asm/vmx.h                    |   1 +
 arch/x86/kernel/idt.c                         |   3 +
 arch/x86/kernel/irq.c                         |  19 +
 arch/x86/kvm/Kconfig                          |   1 +
 arch/x86/kvm/pmu.c                            | 271 ++++++++-
 arch/x86/kvm/pmu.h                            |  37 +-
 arch/x86/kvm/svm/nested.c                     |  18 +-
 arch/x86/kvm/svm/pmu.c                        |  44 ++
 arch/x86/kvm/svm/svm.c                        |  46 ++
 arch/x86/kvm/vmx/capabilities.h               |   9 +-
 arch/x86/kvm/vmx/nested.c                     | 144 ++---
 arch/x86/kvm/vmx/pmu_intel.c                  |  92 +++-
 arch/x86/kvm/vmx/pmu_intel.h                  |  15 +
 arch/x86/kvm/vmx/vmx.c                        | 212 +++++--
 arch/x86/kvm/vmx/vmx.h                        |   9 +-
 arch/x86/kvm/x86.c                            |  54 +-
 arch/x86/kvm/x86.h                            |   1 +
 include/linux/kvm_host.h                      |  11 +-
 include/linux/perf_event.h                    |  35 +-
 init/Kconfig                                  |   4 +
 kernel/events/core.c                          | 517 ++++++++++++++----
 .../beauty/arch/x86/include/asm/irq_vectors.h |   3 +-
 virt/kvm/kvm_main.c                           |   6 +-
 38 files changed, 1396 insertions(+), 283 deletions(-)

base-commit: 5d3e2d9ba9ed68576c70c127e4f7446d896f2af2

Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog