KVM: x86: Add support for mediated vPMUs
Subsystems (14)
Sean Christopherson
maintainerv6 cover 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
L2Sandipan 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
Sean Christopherson
maintainerPATCH v6 1/44 perf: Skip pmu_ctx based on event_type
From: Kan Liang <[email protected]>
To optimize the cgroup context switch, the perf_event_pmu_context iteration skips the PMUs without cgroup events. A bool cgroup was introduced to indicate the case. It can work, but this way is hard to extend for other cases, e.g. skipping non-mediated PMUs. It doesn't make sense to keep adding bool variables.
Pass the event_type instead of the specific bool variable. Check both the event_type and related pmu_ctx variables to decide whether skipping a PMU.
Event flags, e.g., EVENT_CGROUP, should be cleard in the ctx->is_active. Add EVENT_FLAGS to indicate such event flags.
No functional change.
--- kernel/events/core.c | 74 ++++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 34 deletions(-)
kernel/events/core.c +40 -34
@@ -164,7 +164,7 @@ enum event_type_t {/* see ctx_resched() for details */ EVENT_CPU =0x10 ,EVENT_CGROUP =0x20 , - +EVENT_FLAGS =EVENT_CGROUP ,/* compound helpers */ EVENT_ALL =EVENT_FLEXIBLE |EVENT_PINNED , EVENT_TIME_FROZEN = EVENT_TIME | EVENT_FROZEN,
@@ -778,27 +778,37 @@ do { \ ___p; \ }) -#define for_each_epc (_epc, _ctx, _pmu, _cgroup) \ +static bool perf_skip_pmu_ctx (struct perf_event_pmu_context *pmu_ctx, +enum event_type_t event_type) +{ +if ((event_type &EVENT_CGROUP ) && !pmu_ctx->nr_cgroups) +return true; +return false; +} + +#define for_each_epc (_epc, _ctx, _pmu, _event_type) \ list_for_each_entry(_epc, &((_ctx)->pmu_ctx_list), pmu_ctx_entry) \ - if (_cgroup && !_epc->nr_cgroups) \ + if (perf_skip_pmu_ctx(_epc, _event_type)) \ continue; \ else if (_pmu && _epc->pmu != _pmu) \ continue; \ else -static void perf_ctx_disable (struct perf_event_context *ctx,bool cgroup) +static void perf_ctx_disable (struct perf_event_context *ctx, +enum event_type_t event_type) {struct perf_event_pmu_context *pmu_ctx; -for_each_epc (pmu_ctx, ctx,NULL , cgroup) +for_each_epc (pmu_ctx, ctx,NULL , event_type)perf_pmu_disable (pmu_ctx->pmu); } -static void perf_ctx_enable (struct perf_event_context *ctx,bool cgroup) +static void perf_ctx_enable (struct perf_event_context *ctx, +enum event_type_t event_type) {struct perf_event_pmu_context *pmu_ctx; -for_each_epc (pmu_ctx, ctx,NULL , cgroup) +for_each_epc (pmu_ctx, ctx,NULL , event_type)perf_pmu_enable (pmu_ctx->pmu); }
@@ -963,8 +973,7 @@ static void perf_cgroup_switch(struct task_struct *task)return ;WARN_ON_ONCE (cpuctx->ctx.nr_cgroups ==0 ); - -perf_ctx_disable (&cpuctx->ctx, true); +perf_ctx_disable (&cpuctx->ctx,EVENT_CGROUP );ctx_sched_out (&cpuctx->ctx,NULL ,EVENT_ALL |EVENT_CGROUP );/*
@@ -980,7 +989,7 @@ static void perf_cgroup_switch(struct task_struct *task)*/ ctx_sched_in (&cpuctx->ctx,NULL ,EVENT_ALL |EVENT_CGROUP ); -perf_ctx_enable (&cpuctx->ctx, true); +perf_ctx_enable (&cpuctx->ctx,EVENT_CGROUP ); } static int perf_cgroup_ensure_storage(struct perf_event *event,
@@ -2904,11 +2913,11 @@ static void ctx_resched(struct perf_cpu_context *cpuctx, event_type &=EVENT_ALL ; -for_each_epc (epc, &cpuctx->ctx, pmu, false) +for_each_epc (epc, &cpuctx->ctx, pmu,0 )perf_pmu_disable (epc->pmu);if (task_ctx) { -for_each_epc (epc, task_ctx, pmu, false) +for_each_epc (epc, task_ctx, pmu,0 )perf_pmu_disable (epc->pmu);task_ctx_sched_out (task_ctx, pmu, event_type);
@@ -2928,11 +2937,11 @@ static void ctx_resched(struct perf_cpu_context *cpuctx,perf_event_sched_in (cpuctx, task_ctx, pmu); -for_each_epc (epc, &cpuctx->ctx, pmu, false) +for_each_epc (epc, &cpuctx->ctx, pmu,0 )perf_pmu_enable (epc->pmu);if (task_ctx) { -for_each_epc (epc, task_ctx, pmu, false) +for_each_epc (epc, task_ctx, pmu,0 )perf_pmu_enable (epc->pmu); } }
@@ -3481,11 +3490,10 @@ static void ctx_sched_out(struct perf_event_context *ctx, struct pmu *pmu, enum event_type_t event_type) { struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); +enum event_type_t active_type = event_type & ~EVENT_FLAGS ;struct perf_event_pmu_context *pmu_ctx;int is_active = ctx->is_active; -bool cgroup = event_type &EVENT_CGROUP ; - event_type &= ~EVENT_CGROUP ;lockdep_assert_held (&ctx->lock);
@@ -3516,7 +3524,7 @@ ctx_sched_out(struct perf_event_context *ctx, struct pmu *pmu, enum event_type_t* see __load_acquire() in perf_event_time_now() */ barrier (); - ctx->is_active &= ~event_type; + ctx->is_active &= ~active_type;if (!(ctx->is_active &EVENT_ALL )) {/*
@@ -3537,7 +3545,7 @@ ctx_sched_out(struct perf_event_context *ctx, struct pmu *pmu, enum event_type_t is_active ^= ctx->is_active;/* changed bits */ -for_each_epc (pmu_ctx, ctx, pmu, cgroup) +for_each_epc (pmu_ctx, ctx, pmu, event_type)__pmu_ctx_sched_out (pmu_ctx, is_active); }
@@ -3693,7 +3701,7 @@ perf_event_context_sched_out(struct task_struct *task, struct task_struct *next)raw_spin_lock_nested (&next_ctx->lock,SINGLE_DEPTH_NESTING );if (context_equiv (ctx, next_ctx)) { -perf_ctx_disable (ctx, false); +perf_ctx_disable (ctx,0 );/* PMIs are disabled; ctx->nr_no_switch_fast is stable. */ if (local_read(&ctx->nr_no_switch_fast) ||
@@ -3717,7 +3725,7 @@ perf_event_context_sched_out(struct task_struct *task, struct task_struct *next)perf_ctx_sched_task_cb (ctx, task, false); -perf_ctx_enable (ctx, false); +perf_ctx_enable (ctx,0 );/* * RCU_INIT_POINTER here is safe because we've not
@@ -3741,13 +3749,13 @@ perf_event_context_sched_out(struct task_struct *task, struct task_struct *next)if (do_switch) {raw_spin_lock (&ctx->lock); -perf_ctx_disable (ctx, false); +perf_ctx_disable (ctx,0 ); inside_switch:perf_ctx_sched_task_cb (ctx, task, false);task_ctx_sched_out (ctx,NULL ,EVENT_ALL ); -perf_ctx_enable (ctx, false); +perf_ctx_enable (ctx,0 );raw_spin_unlock (&ctx->lock); } }
@@ -4056,11 +4064,9 @@ static void ctx_sched_in(struct perf_event_context *ctx, struct pmu *pmu, enum event_type_t event_type) { struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); +enum event_type_t active_type = event_type & ~EVENT_FLAGS ;struct perf_event_pmu_context *pmu_ctx;int is_active = ctx->is_active; -bool cgroup = event_type &EVENT_CGROUP ; - - event_type &= ~EVENT_CGROUP ;lockdep_assert_held (&ctx->lock);
@@ -4078,7 +4084,7 @@ ctx_sched_in(struct perf_event_context *ctx, struct pmu *pmu, enum event_type_tbarrier (); } - ctx->is_active |= (event_type |EVENT_TIME ); + ctx->is_active |= active_type |EVENT_TIME ;if (ctx->task) {if (!(is_active &EVENT_ALL )) cpuctx->task_ctx = ctx;
@@ -4093,13 +4099,13 @@ ctx_sched_in(struct perf_event_context *ctx, struct pmu *pmu, enum event_type_t* in order to give them the best chance of going on. */ if (is_active &EVENT_PINNED ) { -for_each_epc (pmu_ctx, ctx, pmu, cgroup) +for_each_epc (pmu_ctx, ctx, pmu, event_type)__pmu_ctx_sched_in (pmu_ctx,EVENT_PINNED ); }/* Then walk through the lower prio flexible groups */ if (is_active &EVENT_FLEXIBLE ) { -for_each_epc (pmu_ctx, ctx, pmu, cgroup) +for_each_epc (pmu_ctx, ctx, pmu, event_type)__pmu_ctx_sched_in (pmu_ctx,EVENT_FLEXIBLE ); } }
@@ -4116,11 +4122,11 @@ static void perf_event_context_sched_in(struct task_struct *task)if (cpuctx->task_ctx == ctx) {perf_ctx_lock (cpuctx, ctx); -perf_ctx_disable (ctx, false); +perf_ctx_disable (ctx,0 );perf_ctx_sched_task_cb (ctx, task, true); -perf_ctx_enable (ctx, false); +perf_ctx_enable (ctx,0 );perf_ctx_unlock (cpuctx, ctx); goto rcu_unlock; }
@@ -4133,7 +4139,7 @@ static void perf_event_context_sched_in(struct task_struct *task)if (!ctx->nr_events) goto unlock; -perf_ctx_disable (ctx, false); +perf_ctx_disable (ctx,0 );/* * We want to keep the following priority order: * cpu pinned (that don't need to move), task pinned,
@@ -4143,7 +4149,7 @@ static void perf_event_context_sched_in(struct task_struct *task)* events, no need to flip the cpuctx's events around. */ if (!RB_EMPTY_ROOT (&ctx->pinned_groups.tree)) { -perf_ctx_disable (&cpuctx->ctx, false); +perf_ctx_disable (&cpuctx->ctx,0 );ctx_sched_out (&cpuctx->ctx,NULL ,EVENT_FLEXIBLE ); }
@@ -4152,9 +4158,9 @@ static void perf_event_context_sched_in(struct task_struct *task)perf_ctx_sched_task_cb (cpuctx->task_ctx, task, true);if (!RB_EMPTY_ROOT (&ctx->pinned_groups.tree)) -perf_ctx_enable (&cpuctx->ctx, false); +perf_ctx_enable (&cpuctx->ctx,0 ); -perf_ctx_enable (ctx, false); +perf_ctx_enable (ctx,0 ); unlock:perf_ctx_unlock (cpuctx, ctx);
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
[tip: perf/core] perf: Skip pmu_ctx based on event_type
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: b825444b6179eb071e66ca3da5ac12d4dbd808d5 Gitweb: https://git.kernel.org/tip/b825444b6179eb071e66ca3da5ac12d4dbd808d5 Author: Kan Liang <[email protected]> AuthorDate: Fri, 05 Dec 2025 16:16:37 -08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Dec 2025 13:31:03 +01:00
perf: Skip pmu_ctx based on event_type
To optimize the cgroup context switch, the perf_event_pmu_context iteration skips the PMUs without cgroup events. A bool cgroup was introduced to indicate the case. It can work, but this way is hard to extend for other cases, e.g. skipping non-mediated PMUs. It doesn't make sense to keep adding bool variables.
Pass the event_type instead of the specific bool variable. Check both the event_type and related pmu_ctx variables to decide whether skipping a PMU.
Event flags, e.g., EVENT_CGROUP, should be cleard in the ctx->is_active. Add EVENT_FLAGS to indicate such event flags.
No functional change.
--- kernel/events/core.c | 74 +++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 34 deletions(-)
kernel/events/core.c +40 -34
@@ -165,7 +165,7 @@ enum event_type_t {/* see ctx_resched() for details */ EVENT_CPU =0x10 ,EVENT_CGROUP =0x20 , - +EVENT_FLAGS =EVENT_CGROUP ,/* compound helpers */ EVENT_ALL =EVENT_FLEXIBLE |EVENT_PINNED , EVENT_TIME_FROZEN = EVENT_TIME | EVENT_FROZEN,
@@ -779,27 +779,37 @@ do { \ ___p; \ }) -#define for_each_epc (_epc, _ctx, _pmu, _cgroup) \ +static bool perf_skip_pmu_ctx (struct perf_event_pmu_context *pmu_ctx, +enum event_type_t event_type) +{ +if ((event_type &EVENT_CGROUP ) && !pmu_ctx->nr_cgroups) +return true; +return false; +} + +#define for_each_epc (_epc, _ctx, _pmu, _event_type) \ list_for_each_entry(_epc, &((_ctx)->pmu_ctx_list), pmu_ctx_entry) \ - if (_cgroup && !_epc->nr_cgroups) \ + if (perf_skip_pmu_ctx(_epc, _event_type)) \ continue; \ else if (_pmu && _epc->pmu != _pmu) \ continue; \ else -static void perf_ctx_disable (struct perf_event_context *ctx,bool cgroup) +static void perf_ctx_disable (struct perf_event_context *ctx, +enum event_type_t event_type) {struct perf_event_pmu_context *pmu_ctx; -for_each_epc (pmu_ctx, ctx,NULL , cgroup) +for_each_epc (pmu_ctx, ctx,NULL , event_type)perf_pmu_disable (pmu_ctx->pmu); } -static void perf_ctx_enable (struct perf_event_context *ctx,bool cgroup) +static void perf_ctx_enable (struct perf_event_context *ctx, +enum event_type_t event_type) {struct perf_event_pmu_context *pmu_ctx; -for_each_epc (pmu_ctx, ctx,NULL , cgroup) +for_each_epc (pmu_ctx, ctx,NULL , event_type)perf_pmu_enable (pmu_ctx->pmu); }
@@ -964,8 +974,7 @@ static void perf_cgroup_switch(struct task_struct *task)return ;WARN_ON_ONCE (cpuctx->ctx.nr_cgroups ==0 ); - -perf_ctx_disable (&cpuctx->ctx, true); +perf_ctx_disable (&cpuctx->ctx,EVENT_CGROUP );ctx_sched_out (&cpuctx->ctx,NULL ,EVENT_ALL |EVENT_CGROUP );/*
@@ -981,7 +990,7 @@ static void perf_cgroup_switch(struct task_struct *task)*/ ctx_sched_in (&cpuctx->ctx,NULL ,EVENT_ALL |EVENT_CGROUP ); -perf_ctx_enable (&cpuctx->ctx, true); +perf_ctx_enable (&cpuctx->ctx,EVENT_CGROUP ); } static int perf_cgroup_ensure_storage(struct perf_event *event,
@@ -2902,11 +2911,11 @@ static void ctx_resched(struct perf_cpu_context *cpuctx, event_type &=EVENT_ALL ; -for_each_epc (epc, &cpuctx->ctx, pmu, false) +for_each_epc (epc, &cpuctx->ctx, pmu,0 )perf_pmu_disable (epc->pmu);if (task_ctx) { -for_each_epc (epc, task_ctx, pmu, false) +for_each_epc (epc, task_ctx, pmu,0 )perf_pmu_disable (epc->pmu);task_ctx_sched_out (task_ctx, pmu, event_type);
@@ -2926,11 +2935,11 @@ static void ctx_resched(struct perf_cpu_context *cpuctx,perf_event_sched_in (cpuctx, task_ctx, pmu); -for_each_epc (epc, &cpuctx->ctx, pmu, false) +for_each_epc (epc, &cpuctx->ctx, pmu,0 )perf_pmu_enable (epc->pmu);if (task_ctx) { -for_each_epc (epc, task_ctx, pmu, false) +for_each_epc (epc, task_ctx, pmu,0 )perf_pmu_enable (epc->pmu); } }
@@ -3479,11 +3488,10 @@ static void ctx_sched_out(struct perf_event_context *ctx, struct pmu *pmu, enum event_type_t event_type) { struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); +enum event_type_t active_type = event_type & ~EVENT_FLAGS ;struct perf_event_pmu_context *pmu_ctx;int is_active = ctx->is_active; -bool cgroup = event_type &EVENT_CGROUP ; - event_type &= ~EVENT_CGROUP ;lockdep_assert_held (&ctx->lock);
@@ -3514,7 +3522,7 @@ ctx_sched_out(struct perf_event_context *ctx, struct pmu *pmu, enum event_type_t* see __load_acquire() in perf_event_time_now() */ barrier (); - ctx->is_active &= ~event_type; + ctx->is_active &= ~active_type;if (!(ctx->is_active &EVENT_ALL )) {/*
@@ -3535,7 +3543,7 @@ ctx_sched_out(struct perf_event_context *ctx, struct pmu *pmu, enum event_type_t is_active ^= ctx->is_active;/* changed bits */ -for_each_epc (pmu_ctx, ctx, pmu, cgroup) +for_each_epc (pmu_ctx, ctx, pmu, event_type)__pmu_ctx_sched_out (pmu_ctx, is_active); }
@@ -3691,7 +3699,7 @@ perf_event_context_sched_out(struct task_struct *task, struct task_struct *next)raw_spin_lock_nested (&next_ctx->lock,SINGLE_DEPTH_NESTING );if (context_equiv (ctx, next_ctx)) { -perf_ctx_disable (ctx, false); +perf_ctx_disable (ctx,0 );/* PMIs are disabled; ctx->nr_no_switch_fast is stable. */ if (local_read(&ctx->nr_no_switch_fast) ||
@@ -3715,7 +3723,7 @@ perf_event_context_sched_out(struct task_struct *task, struct task_struct *next)perf_ctx_sched_task_cb (ctx, task, false); -perf_ctx_enable (ctx, false); +perf_ctx_enable (ctx,0 );/* * RCU_INIT_POINTER here is safe because we've not
@@ -3739,13 +3747,13 @@ unlock:if (do_switch) {raw_spin_lock (&ctx->lock); -perf_ctx_disable (ctx, false); +perf_ctx_disable (ctx,0 ); inside_switch:perf_ctx_sched_task_cb (ctx, task, false);task_ctx_sched_out (ctx,NULL ,EVENT_ALL ); -perf_ctx_enable (ctx, false); +perf_ctx_enable (ctx,0 );raw_spin_unlock (&ctx->lock); } }
@@ -4054,11 +4062,9 @@ static void ctx_sched_in(struct perf_event_context *ctx, struct pmu *pmu, enum event_type_t event_type) { struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); +enum event_type_t active_type = event_type & ~EVENT_FLAGS ;struct perf_event_pmu_context *pmu_ctx;int is_active = ctx->is_active; -bool cgroup = event_type &EVENT_CGROUP ; - - event_type &= ~EVENT_CGROUP ;lockdep_assert_held (&ctx->lock);
@@ -4076,7 +4082,7 @@ ctx_sched_in(struct perf_event_context *ctx, struct pmu *pmu, enum event_type_tbarrier (); } - ctx->is_active |= (event_type |EVENT_TIME ); + ctx->is_active |= active_type |EVENT_TIME ;if (ctx->task) {if (!(is_active &EVENT_ALL )) cpuctx->task_ctx = ctx;
@@ -4091,13 +4097,13 @@ ctx_sched_in(struct perf_event_context *ctx, struct pmu *pmu, enum event_type_t* in order to give them the best chance of going on. */ if (is_active &EVENT_PINNED ) { -for_each_epc (pmu_ctx, ctx, pmu, cgroup) +for_each_epc (pmu_ctx, ctx, pmu, event_type)__pmu_ctx_sched_in (pmu_ctx,EVENT_PINNED ); }/* Then walk through the lower prio flexible groups */ if (is_active &EVENT_FLEXIBLE ) { -for_each_epc (pmu_ctx, ctx, pmu, cgroup) +for_each_epc (pmu_ctx, ctx, pmu, event_type)__pmu_ctx_sched_in (pmu_ctx,EVENT_FLEXIBLE ); } }
@@ -4114,11 +4120,11 @@ static void perf_event_context_sched_in(struct task_struct *task)if (cpuctx->task_ctx == ctx) {perf_ctx_lock (cpuctx, ctx); -perf_ctx_disable (ctx, false); +perf_ctx_disable (ctx,0 );perf_ctx_sched_task_cb (ctx, task, true); -perf_ctx_enable (ctx, false); +perf_ctx_enable (ctx,0 );perf_ctx_unlock (cpuctx, ctx); goto rcu_unlock; }
@@ -4131,7 +4137,7 @@ static void perf_event_context_sched_in(struct task_struct *task)if (!ctx->nr_events) goto unlock; -perf_ctx_disable (ctx, false); +perf_ctx_disable (ctx,0 );/* * We want to keep the following priority order: * cpu pinned (that don't need to move), task pinned,
@@ -4141,7 +4147,7 @@ static void perf_event_context_sched_in(struct task_struct *task)* events, no need to flip the cpuctx's events around. */ if (!RB_EMPTY_ROOT (&ctx->pinned_groups.tree)) { -perf_ctx_disable (&cpuctx->ctx, false); +perf_ctx_disable (&cpuctx->ctx,0 );ctx_sched_out (&cpuctx->ctx,NULL ,EVENT_FLEXIBLE ); }
@@ -4150,9 +4156,9 @@ static void perf_event_context_sched_in(struct task_struct *task)perf_ctx_sched_task_cb (cpuctx->task_ctx, task, true);if (!RB_EMPTY_ROOT (&ctx->pinned_groups.tree)) -perf_ctx_enable (&cpuctx->ctx, false); +perf_ctx_enable (&cpuctx->ctx,0 ); -perf_ctx_enable (ctx, false); +perf_ctx_enable (ctx,0 ); unlock:perf_ctx_unlock (cpuctx, ctx);
Sean Christopherson
maintainerPATCH v6 2/44 perf: Add generic exclude_guest support
In reply to Sean ChristophersonFrom: Kan Liang <[email protected]>
Only KVM knows the exact time when a guest is entering/exiting. Expose two interfaces to KVM to switch the ownership of the PMU resources.
All the pinned events must be scheduled in first. Extend the perf_event_sched_in() helper to support extra flag, e.g., EVENT_GUEST.
--- kernel/events/core.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
kernel/events/core.c +8 -7
@@ -2872,14 +2872,15 @@ static void task_ctx_sched_out(struct perf_event_context *ctx,static void perf_event_sched_in (struct perf_cpu_context *cpuctx,struct perf_event_context *ctx, -struct pmu *pmu) +struct pmu *pmu, +enum event_type_t event_type) { -ctx_sched_in (&cpuctx->ctx, pmu,EVENT_PINNED ); +ctx_sched_in (&cpuctx->ctx, pmu,EVENT_PINNED | event_type);if (ctx) -ctx_sched_in (ctx, pmu,EVENT_PINNED ); -ctx_sched_in (&cpuctx->ctx, pmu,EVENT_FLEXIBLE ); +ctx_sched_in (ctx, pmu,EVENT_PINNED | event_type); +ctx_sched_in (&cpuctx->ctx, pmu,EVENT_FLEXIBLE | event_type);if (ctx) -ctx_sched_in (ctx, pmu,EVENT_FLEXIBLE ); +ctx_sched_in (ctx, pmu,EVENT_FLEXIBLE | event_type); }/*
@@ -2935,7 +2936,7 @@ static void ctx_resched(struct perf_cpu_context *cpuctx,else if (event_type &EVENT_PINNED )ctx_sched_out (&cpuctx->ctx, pmu,EVENT_FLEXIBLE ); -perf_event_sched_in (cpuctx, task_ctx, pmu); +perf_event_sched_in (cpuctx, task_ctx, pmu,0 );for_each_epc (epc, &cpuctx->ctx, pmu,0 )perf_pmu_enable (epc->pmu);
@@ -4153,7 +4154,7 @@ static void perf_event_context_sched_in(struct task_struct *task)ctx_sched_out (&cpuctx->ctx,NULL ,EVENT_FLEXIBLE ); } -perf_event_sched_in (cpuctx, ctx,NULL ); +perf_event_sched_in (cpuctx, ctx,NULL ,0 );perf_ctx_sched_task_cb (cpuctx->task_ctx, task, true);
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
[tip: perf/core] perf: Add generic exclude_guest support
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: b9e52b11d2e5e403afaf69a7f8d6b29f8380ed38 Gitweb: https://git.kernel.org/tip/b9e52b11d2e5e403afaf69a7f8d6b29f8380ed38 Author: Kan Liang <[email protected]> AuthorDate: Fri, 05 Dec 2025 16:16:38 -08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Dec 2025 13:31:03 +01:00
perf: Add generic exclude_guest support
Only KVM knows the exact time when a guest is entering/exiting. Expose two interfaces to KVM to switch the ownership of the PMU resources.
All the pinned events must be scheduled in first. Extend the perf_event_sched_in() helper to support extra flag, e.g., EVENT_GUEST.
--- kernel/events/core.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
kernel/events/core.c +8 -7
@@ -2870,14 +2870,15 @@ static void task_ctx_sched_out(struct perf_event_context *ctx,static void perf_event_sched_in (struct perf_cpu_context *cpuctx,struct perf_event_context *ctx, -struct pmu *pmu) +struct pmu *pmu, +enum event_type_t event_type) { -ctx_sched_in (&cpuctx->ctx, pmu,EVENT_PINNED ); +ctx_sched_in (&cpuctx->ctx, pmu,EVENT_PINNED | event_type);if (ctx) -ctx_sched_in (ctx, pmu,EVENT_PINNED ); -ctx_sched_in (&cpuctx->ctx, pmu,EVENT_FLEXIBLE ); +ctx_sched_in (ctx, pmu,EVENT_PINNED | event_type); +ctx_sched_in (&cpuctx->ctx, pmu,EVENT_FLEXIBLE | event_type);if (ctx) -ctx_sched_in (ctx, pmu,EVENT_FLEXIBLE ); +ctx_sched_in (ctx, pmu,EVENT_FLEXIBLE | event_type); }/*
@@ -2933,7 +2934,7 @@ static void ctx_resched(struct perf_cpu_context *cpuctx,else if (event_type &EVENT_PINNED )ctx_sched_out (&cpuctx->ctx, pmu,EVENT_FLEXIBLE ); -perf_event_sched_in (cpuctx, task_ctx, pmu); +perf_event_sched_in (cpuctx, task_ctx, pmu,0 );for_each_epc (epc, &cpuctx->ctx, pmu,0 )perf_pmu_enable (epc->pmu);
@@ -4151,7 +4152,7 @@ static void perf_event_context_sched_in(struct task_struct *task)ctx_sched_out (&cpuctx->ctx,NULL ,EVENT_FLEXIBLE ); } -perf_event_sched_in (cpuctx, ctx,NULL ); +perf_event_sched_in (cpuctx, ctx,NULL ,0 );perf_ctx_sched_task_cb (cpuctx->task_ctx, task, true);
Sean Christopherson
maintainerPATCH v6 3/44 perf: Move security_perf_event_free() call to __free_event()
In reply to Sean ChristophersonMove the freeing of any security state associated with a perf event from _free_event() to __free_event(), i.e. invoke security_perf_event_free() in the error paths for perf_event_alloc(). This will allow adding potential error paths in perf_event_alloc() that can occur after allocating security state.
Note, kfree() and thus security_perf_event_free() is a nop if event->security is NULL, i.e. calling security_perf_event_free() even if security_perf_event_alloc() fails or is never reached is functionality ok.
--- kernel/events/core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/events/core.c +2 -2
@@ -5602,6 +5602,8 @@ static void __free_event(struct perf_event *event) {struct pmu *pmu = event->pmu; +security_perf_event_free (event); +if (event->attach_state &PERF_ATTACH_CALLCHAIN )put_callchain_buffers ();
@@ -5665,8 +5667,6 @@ static void _free_event(struct perf_event *event)unaccount_event (event); -security_perf_event_free (event); -if (event->rb) {/* * Can happen when we close an event with re-directed output.
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
[tip: perf/core] perf: Move security_perf_event_free() call to __free_event()
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 991bdf7e9d6cc74c1de215d1a05c23ff61076bf0 Gitweb: https://git.kernel.org/tip/991bdf7e9d6cc74c1de215d1a05c23ff61076bf0 Author: Sean Christopherson <[email protected]> AuthorDate: Fri, 05 Dec 2025 16:16:39 -08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Dec 2025 13:31:04 +01:00
perf: Move security_perf_event_free() call to __free_event()
Move the freeing of any security state associated with a perf event from _free_event() to __free_event(), i.e. invoke security_perf_event_free() in the error paths for perf_event_alloc(). This will allow adding potential error paths in perf_event_alloc() that can occur after allocating security state.
Note, kfree() and thus security_perf_event_free() is a nop if event->security is NULL, i.e. calling security_perf_event_free() even if security_perf_event_alloc() fails or is never reached is functionality ok.
--- kernel/events/core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/events/core.c +2 -2
@@ -5601,6 +5601,8 @@ static void __free_event(struct perf_event *event) {struct pmu *pmu = event->pmu; +security_perf_event_free (event); +if (event->attach_state &PERF_ATTACH_CALLCHAIN )put_callchain_buffers ();
@@ -5664,8 +5666,6 @@ static void _free_event(struct perf_event *event)unaccount_event (event); -security_perf_event_free (event); -if (event->rb) {/* * Can happen when we close an event with re-directed output.
Sean Christopherson
maintainerPATCH v6 4/44 perf: Add APIs to create/release mediated guest vPMUs
In reply to Sean ChristophersonFrom: Kan Liang <[email protected]>
Currently, exposing PMU capabilities to a KVM guest is done by emulating guest PMCs via host perf events, i.e. by having KVM be "just" another user of perf. As a result, the guest and host are effectively competing for resources, and emulating guest accesses to vPMU resources requires expensive actions (expensive relative to the native instruction). The overhead and resource competition results in degraded guest performance and ultimately very poor vPMU accuracy.
To address the issues with the perf-emulated vPMU, introduce a "mediated vPMU", where the data plane (PMCs and enable/disable knobs) is exposed directly to the guest, but the control plane (event selectors and access to fixed counters) is managed by KVM (via MSR interceptions). To allow host perf usage of the PMU to (partially) co-exist with KVM/guest usage of the PMU, KVM and perf will coordinate to a world switch between host perf context and guest vPMU context near VM-Enter/VM-Exit.
Add two exported APIs, perf_{create,release}_mediated_pmu(), to allow KVM to create and release a mediated PMU instance (per VM). Because host perf context will be deactivated while the guest is running, mediated PMU usage will be mutually exclusive with perf analysis of the guest, i.e. perf events that do NOT exclude the guest will not behave as expected.
To avoid silent failure of !exclude_guest perf events, disallow creating a mediated PMU if there are active !exclude_guest events, and on the perf side, disallowing creating new !exclude_guest perf events while there is at least one active mediated PMU.
Exempt PMU resources that do not support mediated PMU usage, i.e. that are outside the scope/view of KVM's vPMU and will not be swapped out while the guest is running.
Guard mediated PMU with a new kconfig to help readers identify code paths that are unique to mediated PMU support, and to allow for adding arch- specific hooks without stubs. KVM x86 is expected to be the only KVM architecture to support a mediated PMU in the near future (e.g. arm64 is trending toward a partitioned PMU implementation), and KVM x86 will select PERF_GUEST_MEDIATED_PMU unconditionally, i.e. won't need stubs.
Immediately select PERF_GUEST_MEDIATED_PMU when KVM x86 is enabled so that all paths are compile tested. Full KVM support is on its way...
--- arch/x86/kvm/Kconfig | 1 + include/linux/perf_event.h | 6 +++ init/Kconfig | 4 ++ kernel/events/core.c | 82 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+)
arch/x86/kvm/Kconfig +1 -0
@@ -37,6 +37,7 @@ config KVM_X86 select SCHED_INFO select PERF_EVENTS select GUEST_PERF_EVENTS + select PERF_GUEST_MEDIATED_PMU select HAVE_KVM_MSI select HAVE_KVM_CPU_RELAX_INTERCEPT select HAVE_KVM_NO_POLL
include/linux/perf_event.h +6 -0
@@ -305,6 +305,7 @@ struct perf_event_pmu_context;#define PERF_PMU_CAP_EXTENDED_HW_TYPE 0x0100#define PERF_PMU_CAP_AUX_PAUSE 0x0200#define PERF_PMU_CAP_AUX_PREFER_LARGE 0x0400 +#define PERF_PMU_CAP_MEDIATED_VPMU 0x0800/** * pmu::scope
@@ -1914,6 +1915,11 @@ extern int perf_event_account_interrupt(struct perf_event *event);extern int perf_event_period (struct perf_event *event,u64 value);extern u64 perf_event_pause (struct perf_event *event,bool reset); +#ifdef CONFIG_PERF_GUEST_MEDIATED_PMU +int perf_create_mediated_pmu (void ); +void perf_release_mediated_pmu (void ); +#endif +#else /* !CONFIG_PERF_EVENTS: */ static inline void *
init/Kconfig +4 -0
@@ -2010,6 +2010,10 @@ config GUEST_PERF_EVENTS bool depends on HAVE_PERF_EVENTS +config PERF_GUEST_MEDIATED_PMU + bool + depends on GUEST_PERF_EVENTS + config PERF_USE_VMALLOC bool help
kernel/events/core.c +82 -0
106 lines not on this page · raw message
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Peter Zijlstra
maintainerRe: [PATCH v6 04/44] perf: Add APIs to create/release mediated guest vPMUs
Quoted 39 lines · Sean Christopherson
kernel/events/core.c +37
-0+static atomic_t nr_include_guest_events__read_mostly ; + +static atomic_t nr_mediated_pmu_vms__read_mostly ; +static DEFINE_MUTEX (perf_mediated_pmu_mutex); +static int mediated_pmu_account_event (struct perf_event *event) +{ +if (!is_include_guest_event (event)) +return 0 ; + +guard (mutex)(&perf_mediated_pmu_mutex); + +if (atomic_read (&nr_mediated_pmu_vms)) +return -EOPNOTSUPP ; + +atomic_inc (&nr_include_guest_events); +return 0 ; +} + +static void mediated_pmu_unaccount_event (struct perf_event *event) +{ +if (!is_include_guest_event (event)) +return ; + +atomic_dec (&nr_include_guest_events); +} +int perf_create_mediated_pmu (void ) +{ +guard (mutex)(&perf_mediated_pmu_mutex); +if (atomic_inc_not_zero (&nr_mediated_pmu_vms)) +return 0 ; + +if (atomic_read (&nr_include_guest_events)) +return -EBUSY ; + +atomic_inc (&nr_mediated_pmu_vms); +return 0 ; +}
kernel/events/core.c u64 perf_event_pause(struct perf_event *event, bool reset) · line 6263
Quoted 10 lines
kernel/events/core.c +10
-0+EXPORT_SYMBOL_GPL (perf_create_mediated_pmu); + +void perf_release_mediated_pmu (void ) +{ +if (WARN_ON_ONCE (!atomic_read (&nr_mediated_pmu_vms))) +return ; + +atomic_dec (&nr_mediated_pmu_vms); +} +EXPORT_SYMBOL_GPL (perf_release_mediated_pmu);
These two things are supposed to be symmetric, but are implemented differently; what gives?
That is, should not both have the general shape:
if (atomic_inc_not_zero(&A)) return 0; guard(mutex)(&lock); if (atomic_read(&B)) return -EBUSY; atomic_inc(&A); return 0;
Similarly, I would imagine both release variants to have the underflow warn on like:
if (WARN_ON_ONCE(!atomic_read(&A))) return; atomic_dec(&A);
Hmm?
Also, EXPORT_SYMBOL_FOR_KVM() ?
I can make these edits when applying, if/when we get to applying. Let me continue reading.
Signature · 3 lines
kvm-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kvm-riscv
Sean Christopherson
maintainerRe: [PATCH v6 04/44] perf: Add APIs to create/release mediated guest vPMUs
Quoted 71 lines · Peter Zijlstra
Quoted 39 lines · Sean Christopherson
(quoted diff) +37
-0+static atomic_t nr_include_guest_events __read_mostly; + +static atomic_t nr_mediated_pmu_vms __read_mostly; +static DEFINE_MUTEX(perf_mediated_pmu_mutex); +static int mediated_pmu_account_event(struct perf_event *event) +{ + if (!is_include_guest_event(event)) + return 0; + + guard(mutex)(&perf_mediated_pmu_mutex); + + if (atomic_read(&nr_mediated_pmu_vms)) + return -EOPNOTSUPP; + + atomic_inc(&nr_include_guest_events); + return 0; +} + +static void mediated_pmu_unaccount_event(struct perf_event *event) +{ + if (!is_include_guest_event(event)) + return; + + atomic_dec(&nr_include_guest_events); +} +int perf_create_mediated_pmu(void) +{ + guard(mutex)(&perf_mediated_pmu_mutex); + if (atomic_inc_not_zero(&nr_mediated_pmu_vms)) + return 0; + + if (atomic_read(&nr_include_guest_events)) + return -EBUSY; + + atomic_inc(&nr_mediated_pmu_vms); + return 0; +}Review
Quoted 10 lines
(quoted diff) +10
-0+EXPORT_SYMBOL_GPL(perf_create_mediated_pmu); + +void perf_release_mediated_pmu(void) +{ + if (WARN_ON_ONCE(!atomic_read(&nr_mediated_pmu_vms))) + return; + + atomic_dec(&nr_mediated_pmu_vms); +} +EXPORT_SYMBOL_GPL(perf_release_mediated_pmu);These two things are supposed to be symmetric, but are implemented differently; what gives?
That is, should not both have the general shape:
if (atomic_inc_not_zero(&A)) return 0; guard(mutex)(&lock); if (atomic_read(&B)) return -EBUSY; atomic_inc(&A); return 0;Similarly, I would imagine both release variants to have the underflow warn on like:
Quoted 6 lines
if (WARN_ON_ONCE(!atomic_read(&A))) return; atomic_dec(&A);Hmm?
IIUC, you're suggesting someting like this? If so, that makes perfect sense to me.
kernel/events/core.c +2 -1
@@ -6356,7 +6356,8 @@ static int mediated_pmu_account_event(struct perf_event *event)static void mediated_pmu_unaccount_event (struct perf_event *event) { -if (!is_include_guest_event (event)) +if (!is_include_guest_event (event) || +WARN_ON_ONCE (!atomic_read (&nr_include_guest_events)))return ;atomic_dec (&nr_include_guest_events);
Quoted 1 line
Also,
EXPORT_SYMBOL_FOR_KVM()?
Ya, for sure. I posted this against a branch without EXPORT_SYMBOL_FOR_KVM(), because there are also hard dependencies on the for-6.19 KVM pull requests, and I didn't want to wait to post until 6.19-rc1 because of the impending winter break. Though I also simply forgot about these exports :-(
These could also use EXPORT_SYMBOL_FOR_KVM():
EXPORT_SYMBOL_FOR_MODULES(perf_load_guest_lvtpc, "kvm"); EXPORT_SYMBOL_FOR_MODULES(perf_put_guest_lvtpc, "kvm");
Quoted 3 lines
I can make these edits when applying, if/when we get to applying. Let me continue reading.
[tip: perf/core] perf: Use EXPORT_SYMBOL_FOR_KVM() for the mediated APIs
In reply to Peter ZijlstraThe following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 23faa33d88df6d126cebd3121ea2cff3586e7b95 Gitweb: https://git.kernel.org/tip/23faa33d88df6d126cebd3121ea2cff3586e7b95 Author: Peter Zijlstra <[email protected]> AuthorDate: Wed, 17 Dec 2025 13:23:59 +01:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Dec 2025 13:31:09 +01:00
perf: Use EXPORT_SYMBOL_FOR_KVM() for the mediated APIs
--- arch/x86/events/core.c | 5 +++-- kernel/events/core.c | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-)
arch/x86/events/core.c +3 -2
@@ -30,6 +30,7 @@#include <linux/device.h> #include <linux/nospec.h> #include <linux/static_call.h> +#include <linux/kvm_types.h> #include <asm/apic.h> #include <asm/stacktrace.h>
@@ -1771,14 +1772,14 @@ void perf_load_guest_lvtpc(u32 guest_lvtpc) APIC_DM_FIXED | PERF_GUEST_MEDIATED_PMI_VECTOR | masked);this_cpu_write (guest_lvtpc_loaded, true); } -EXPORT_SYMBOL_FOR_MODULES (perf_load_guest_lvtpc,"kvm" ); +EXPORT_SYMBOL_FOR_KVM (perf_load_guest_lvtpc);void perf_put_guest_lvtpc (void ) {this_cpu_write (guest_lvtpc_loaded, false);apic_write (APIC_LVTPC ,APIC_DM_NMI ); } -EXPORT_SYMBOL_FOR_MODULES (perf_put_guest_lvtpc,"kvm" ); +EXPORT_SYMBOL_FOR_KVM (perf_put_guest_lvtpc);#endif /* CONFIG_PERF_GUEST_MEDIATED_PMU */ static int
kernel/events/core.c +3 -2
@@ -57,6 +57,7 @@#include <linux/task_work.h> #include <linux/percpu-rwsem.h> #include <linux/unwind_deferred.h> +#include <linux/kvm_types.h> #include "internal.h"
@@ -6388,7 +6389,7 @@ int perf_create_mediated_pmu(void)atomic_inc (&nr_mediated_pmu_vms);return 0 ; } -EXPORT_SYMBOL_GPL (perf_create_mediated_pmu); +EXPORT_SYMBOL_FOR_KVM (perf_create_mediated_pmu);void perf_release_mediated_pmu (void ) {
@@ -6397,7 +6398,7 @@ void perf_release_mediated_pmu(void)atomic_dec (&nr_mediated_pmu_vms); } -EXPORT_SYMBOL_GPL (perf_release_mediated_pmu); +EXPORT_SYMBOL_FOR_KVM (perf_release_mediated_pmu);/* When loading a guest's mediated PMU, schedule out all exclude_guest events. */ void perf_load_guest_context (void )
Peter Zijlstra
maintainerRe: [tip: perf/core] perf: Use EXPORT_SYMBOL_FOR_KVM() for the mediated APIs
Quoted 22 lines · On Wed, Dec 17, 2025 at 12:37:57PM -0000, tip-bot2 for Peter Zijlstra wrote:
kernel/events/core.c +2
-1@@ -57,6 +57,7 @@#include <linux/task_work.h> #include <linux/percpu-rwsem.h> #include <linux/unwind_deferred.h> +#include <linux/kvm_types.h> #include "internal.h" @@ -6388,7 +6389,7 @@ int perf_create_mediated_pmu(void)atomic_inc (&nr_mediated_pmu_vms);return 0 ; } -EXPORT_SYMBOL_GPL (perf_create_mediated_pmu); +EXPORT_SYMBOL_FOR_KVM (perf_create_mediated_pmu);void perf_release_mediated_pmu (void ) {
kernel/events/core.c void perf_release_mediated_pmu(void) · line 6404
Quoted 8 lines
kernel/events/core.c +1
-1@@ -6397,7 +6398,7 @@ void perf_release_mediated_pmu(void)atomic_dec (&nr_mediated_pmu_vms); } -EXPORT_SYMBOL_GPL (perf_release_mediated_pmu); +EXPORT_SYMBOL_FOR_KVM (perf_release_mediated_pmu);/* When loading a guest's mediated PMU, schedule out all exclude_guest events. */ void perf_load_guest_context (void )
Bah, so the !KVM architectures hate on this.
Sean, would something like this be acceptable?
---
Subject: kvm: Fix linux/kvm_types.h for !KVM architectures
As is, <linux/kvm_types.h> hard relies on architectures having <asm/kvm_types.h> which (obviously) breaks for architectures that don't have KVM support.
This means generic code (kernel/events/ in this case) cannot use EXPORT_SYMBOL_FOR_KVM().
Rearrange things just so that <linux/kvm_types.h> becomes usable and provides the (expected) empty stub for EXPORT_SYMBOL_FOR_KVM() for !KVM.
---
include/linux/kvm_types.h +8 -5
@@ -6,6 +6,8 @@#include <linux/bits.h> #include <linux/export.h> #include <linux/types.h> + +#ifdef CONFIG_KVM #include <asm/kvm_types.h> #ifdef KVM_SUB_MODULES
@@ -20,13 +22,14 @@* if there are no submodules, e.g. to allow suppressing exports if KVM=m, but * kvm.ko won't actually be built (due to lack of at least one submodule). */ -#ifndef EXPORT_SYMBOL_FOR_KVM -#if IS_MODULE (CONFIG_KVM ) +#if defined(EXPORT_SYMBOL_FOR_KVM ) &&IS_MODULE (CONFIG_KVM )#define EXPORT_SYMBOL_FOR_KVM (symbol) EXPORT_SYMBOL_FOR_MODULES(symbol, "kvm") -#else -#define EXPORT_SYMBOL_FOR_KVM (symbol)#endif /* IS_MODULE(CONFIG_KVM) */ -#endif /* EXPORT_SYMBOL_FOR_KVM */ +#endif /* KVM_SUB_MODULES */ +#endif + +#ifndef EXPORT_SYMBOL_FOR_KVM +#define EXPORTEXPORT_SYMBOL_FOR_KVM (symbol)#endif #ifndef __ASSEMBLER__
Peter Zijlstra
maintainerRe: [tip: perf/core] perf: Use EXPORT_SYMBOL_FOR_KVM() for the mediated APIs
Quoted 29 lines · Peter Zijlstra
Quoted 28 lines · On Wed, Dec 17, 2025 at 12:37:57PM -0000, tip-bot2 for Peter Zijlstra wrote:
kernel/events/core.c +3
-2@@ -57,6 +57,7 @@#include <linux/task_work.h> #include <linux/percpu-rwsem.h> #include <linux/unwind_deferred.h> +#include <linux/kvm_types.h> #include "internal.h" @@ -6388,7 +6389,7 @@ int perf_create_mediated_pmu(void)atomic_inc (&nr_mediated_pmu_vms);return 0 ; } -EXPORT_SYMBOL_GPL (perf_create_mediated_pmu); +EXPORT_SYMBOL_FOR_KVM (perf_create_mediated_pmu);void perf_release_mediated_pmu (void ) {@@ -6397,7 +6398,7 @@ void perf_release_mediated_pmu(void)atomic_dec (&nr_mediated_pmu_vms); } -EXPORT_SYMBOL_GPL (perf_release_mediated_pmu); +EXPORT_SYMBOL_FOR_KVM (perf_release_mediated_pmu);
Quoted 6 lines
kernel/events/core.c void perf_release_mediated_pmu(void) · line 6404
Quoted 2 lines
kernel/events/core.c +0
-0/* When loading a guest's mediated PMU, schedule out all exclude_guest events. */ void perf_load_guest_context (void )Bah, so the !KVM architectures hate on this.
Sean, would something like this be acceptable?
Hmm, the other option is doing something like so:
kernel/events/core.c +2 -1
@@ -57,7 +57,6 @@#include <linux/task_work.h> #include <linux/percpu-rwsem.h> #include <linux/unwind_deferred.h> -#include <linux/kvm_types.h> #include "internal.h"
@@ -6325,6 +6324,8 @@ u64 perf_event_pause(struct perf_event *event, bool reset)EXPORT_SYMBOL_GPL (perf_event_pause);#ifdef CONFIG_PERF_GUEST_MEDIATED_PMU +#include <linux/kvm_types.h> +static atomic_t nr_include_guest_events__read_mostly ;static atomic_t nr_mediated_pmu_vms__read_mostly ;
Quoted 48 lines
---
Subject: kvm: Fixlinux/kvm_types.hfor !KVM architecturesAs is, <
linux/kvm_types.h> hard relies on architectures having <asm/kvm_types.h> which (obviously) breaks for architectures that don't have KVM support.This means generic code (
kernel/events/in this case) cannot useEXPORT_SYMBOL_FOR_KVM().Rearrange things just so that <
linux/kvm_types.h> becomes usable and provides the (expected) empty stub forEXPORT_SYMBOL_FOR_KVM()for !KVM.Signed-off-by Peter Zijlstra (Intel) <[email protected]>---
include/linux/kvm_types.h +8
-5@@ -6,6 +6,8 @@#include <linux/bits.h> #include <linux/export.h> #include <linux/types.h> + +#ifdef CONFIG_KVM #include <asm/kvm_types.h> #ifdef KVM_SUB_MODULES @@ -20,13 +22,14 @@* if there are no submodules, e.g. to allow suppressing exports if KVM=m, but * kvm.ko won't actually be built (due to lack of at least one submodule). */ -#ifndef EXPORT_SYMBOL_FOR_KVM -#if IS_MODULE (CONFIG_KVM ) +#if defined(EXPORT_SYMBOL_FOR_KVM ) &&IS_MODULE (CONFIG_KVM )#define EXPORT_SYMBOL_FOR_KVM (symbol) EXPORT_SYMBOL_FOR_MODULES(symbol, "kvm") -#else -#define EXPORT_SYMBOL_FOR_KVM (symbol)#endif /* IS_MODULE(CONFIG_KVM) */ -#endif /* EXPORT_SYMBOL_FOR_KVM */ +#endif /* KVM_SUB_MODULES */ +#endif + +#ifndef EXPORT_SYMBOL_FOR_KVM +#define EXPORTEXPORT_SYMBOL_FOR_KVM (symbol)#endif #ifndef __ASSEMBLER__
Sean Christopherson
maintainerRe: [tip: perf/core] perf: Use EXPORT_SYMBOL_FOR_KVM() for the mediated APIs
Quoted 30 lines · Peter Zijlstra
Quoted 12 lines · Peter Zijlstra
kernel/events/core.c line 60
Quoted 9 lines · On Wed, Dec 17, 2025 at 12:37:57PM -0000, tip-bot2 for Peter Zijlstra wrote:
kernel/events/core.c +1
-0@@ -57,6 +57,7 @@#include <linux/task_work.h> #include <linux/percpu-rwsem.h> #include <linux/unwind_deferred.h> +#include <linux/kvm_types.h> Bah, so the !KVM architectures hate on this.
Quoted 1 line
Sean, would something like this be acceptable?
Hmm, the other option is doing something like so:
kernel/events/core.c +0
-1@@ -57,7 +57,6 @@#include <linux/task_work.h> #include <linux/percpu-rwsem.h> #include <linux/unwind_deferred.h> -#include <linux/kvm_types.h> #include "internal.h"
kernel/events/core.c u64 perf_event_pause(struct perf_event *event, bool reset) · line 6327
Quoted 4 lines
kernel/events/core.c +1
-0@@ -6325,6 +6324,8 @@ u64 perf_event_pause(struct perf_event *event, bool reset)EXPORT_SYMBOL_GPL (perf_event_pause);#ifdef CONFIG_PERF_GUEST_MEDIATED_PMU +#include <linux/kvm_types.h>
Hrm, quick and dirty, but I don't love the idea of punting on the underlying issue, because not being able to include kvm_types.h will be a big deterrent to using EXPORT_SYMBOL_FOR_KVM().
Quoted 17 lines
kernel/events/core.c +0
-0static atomic_t nr_include_guest_events__read_mostly ;static atomic_t nr_mediated_pmu_vms__read_mostly ;Quoted 14 lines
---
Subject: kvm: Fixlinux/kvm_types.hfor !KVM architecturesAs is, <
linux/kvm_types.h> hard relies on architectures having <asm/kvm_types.h> which (obviously) breaks for architectures that don't have KVM support.This means generic code (
kernel/events/in this case) cannot useEXPORT_SYMBOL_FOR_KVM().Rearrange things just so that <
linux/kvm_types.h> becomes usable and provides the (expected) empty stub forEXPORT_SYMBOL_FOR_KVM()for !KVM.
Quoted 13 lines
include/linux/kvm_types.h line 11
Quoted 13 lines
Signed-off-by Peter Zijlstra (Intel) <[email protected]>---
include/linux/kvm_types.h +2
-0@@ -6,6 +6,8 @@#include <linux/bits.h> #include <linux/export.h> #include <linux/types.h> + +#ifdef CONFIG_KVM #include <asm/kvm_types.h>
This won't work, because asm/kvm_types.h #defines KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE, which guards the "struct kvm_mmu_memory_cache" definition. E.g. on x86 with CONFIG_KVM=n, that yields errors like:
In file included from include/linux/kvm_host.h:45,
from arch/x86/events/intel/core.c:17:
arch/x86/include/asm/kvm_host.h:854:37: error: field ‘mmu_pte_list_desc_cache’ has incomplete type
854 | struct kvm_mmu_memory_cache mmu_pte_list_desc_cache;
| ^~~~~~~~~~~~~~~~~~~~~~~In general, I'm hesitant to guard an include with a conditional Kconfig, precisely because doing so has a tendency to result in wonky, config-specific build errors.
Rather than gate the check on KVM being enabled, what if we restrict the asm include to architectures that support KVM in any capacity? Alternatively, we could add a HAVE_KVM, but I'd rather not add HAVE_KVM, because then we'll end up with the same mess if architectures get clever and conditionally select HAVE_KVM (IIRC, that's exactly what happened when HAVE_KVM was a thing in the past).
Compiled tested on all KVM architectures along with csky (and an include of kvm_types.h in init/main.c).
--
From: Sean Christopherson <[email protected]>
Date: Thu, 18 Dec 2025 15:47:59 +0000
Subject: [PATCH] KVM: Allow linux/kvm_types.h to be included on non-KVM
architectures
Include the arch-defined asm/kvm_types.h if and only if the kernel is being compiled for an architecture that supports KVM so that kvm_types.h can be included in generic code without having to guard _those_ includes, and without having to add "generic-y += kvm_types.h" for all architectures that don't support KVM.
Assert that KVM=n if asm/kvm_types.h isn't included to provide a more helpful error message if an arch name changes (highly unlikely) or a new arch that supports KVM comes along.
--- include/linux/kvm_types.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
include/linux/kvm_types.h +16 -0
@@ -6,7 +6,23 @@#include <linux/bits.h> #include <linux/export.h> #include <linux/types.h> + +/* +* Include the arch-defined kvm_types.h if and only if the target architecture +* supports KVM, so that linux/kvm_types.h can be included in generic code +* without requiring _all_ architectures to add generic-y += kvm_types.h. +*/ +#if defined(CONFIG_ARM64 ) || \ + defined(CONFIG_LOONGARCH ) || \ + defined(CONFIG_MIPS ) || \ + defined(CONFIG_PPC ) || \ + defined(CONFIG_RISCV ) || \ + defined(CONFIG_S390 ) || \ + defined(CONFIG_X86 )#include <asm/kvm_types.h> +#else +static_assert (!IS_ENABLED (CONFIG_KVM )); +#endif #ifdef KVM_SUB_MODULES #define EXPORT_SYMBOL_FOR_KVM_INTERNAL (symbol) \
base-commit: 8f0b4cce4481fb22653697cced8d0d04027cb1e8
Peter Zijlstra
maintainerRe: [tip: perf/core] perf: Use EXPORT_SYMBOL_FOR_KVM() for the mediated APIs
Quoted 5 lines · Sean Christopherson
Include the arch-defined
asm/kvm_types.hif and only if the kernel is being compiled for an architecture that supports KVM so thatkvm_types.hcan be included in generic code without having to guard _those_ includes, and without having to add "generic-y +=kvm_types.h" for all architectures that don't support KVM.
Something jogged my brain and the below seems to work for the few architectures I've tried. Let me update the patch and see if the build robot still finds fail.
---
include/asm-generic/Kbuild +1 -0
@@ -32,6 +32,7 @@ mandatory-y += irq_work.h mandatory-y += kdebug.h mandatory-y += kmap_size.h mandatory-y += kprobes.h +mandatory-y += kvm_types.h mandatory-y += linkage.h mandatory-y += local.h mandatory-y += local64.h
Sean Christopherson
maintainerRe: [tip: perf/core] perf: Use EXPORT_SYMBOL_FOR_KVM() for the mediated APIs
Quoted 9 lines · Peter Zijlstra
Quoted 5 lines · Sean Christopherson
Include the arch-defined
asm/kvm_types.hif and only if the kernel is being compiled for an architecture that supports KVM so thatkvm_types.hcan be included in generic code without having to guard _those_ includes, and without having to add "generic-y +=kvm_types.h" for all architectures that don't support KVM.
Quoted 3 lines
Something jogged my brain and the below seems to work for the few architectures I've tried. Let me update the patch and see if the build robot still finds fail.
Nice! Works on my end as well. Just when I think I've learned most of the build system's tricks...
Quoted 13 lines
---
include/asm-generic/Kbuild +1
-0@@ -32,6 +32,7 @@ mandatory-y += irq_work.h mandatory-y += kdebug.h mandatory-y += kmap_size.h mandatory-y += kprobes.h +mandatory-y += kvm_types.h mandatory-y += linkage.h mandatory-y += local.h mandatory-y += local64.h
[tip: perf/core] perf: Clean up mediated vPMU accounting
In reply to Peter ZijlstraThe following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 3cb3c2f6886f9489df13de8efe7a1e803a3f21ea Gitweb: https://git.kernel.org/tip/3cb3c2f6886f9489df13de8efe7a1e803a3f21ea Author: Peter Zijlstra <[email protected]> AuthorDate: Wed, 17 Dec 2025 12:08:01 +01:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Dec 2025 13:31:09 +01:00
perf: Clean up mediated vPMU accounting
The mediated_pmu_account_event() and perf_create_mediated_pmu() functions implement the exclusion between '!exclude_guest' counters and mediated vPMUs. Their implementation is basically identical, except mirrored in what they count/check.
Make sure the actual implementations reflect this similarity.
Notably:
- while perf_release_mediated_pmu() has an underflow check; mediated_pmu_unaccount_event() did not.
- while perf_create_mediated_pmu() has an inc_not_zero() path; mediated_pmu_account_event() did not.
Also, the inc_not_zero() path can be outsite of perf_mediated_pmu_mutex. The mutex must guard the 0->1 (of either nr_include_guest_events or nr_mediated_pmu_vms) transition, but once a counter is already non-zero, it can safely be incremented further.
--- kernel/events/core.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
kernel/events/core.c +7 -2
@@ -6344,8 +6344,10 @@ static int mediated_pmu_account_event(struct perf_event *event)if (!is_include_guest_event (event))return 0 ; -guard (mutex)(&perf_mediated_pmu_mutex); +if (atomic_inc_not_zero (&nr_include_guest_events)) +return 0 ; +guard (mutex)(&perf_mediated_pmu_mutex);if (atomic_read (&nr_mediated_pmu_vms))return -EOPNOTSUPP ;
@@ -6358,6 +6360,9 @@ static void mediated_pmu_unaccount_event(struct perf_event *event)if (!is_include_guest_event (event))return ; +if (WARN_ON_ONCE (!atomic_read (&nr_include_guest_events))) +return ; +atomic_dec (&nr_include_guest_events); }
@@ -6373,10 +6378,10 @@ static void mediated_pmu_unaccount_event(struct perf_event *event)*/ int perf_create_mediated_pmu (void ) { -guard (mutex)(&perf_mediated_pmu_mutex);if (atomic_inc_not_zero (&nr_mediated_pmu_vms))return 0 ; +guard (mutex)(&perf_mediated_pmu_mutex);if (atomic_read (&nr_include_guest_events))return -EBUSY ;
[tip: perf/core] perf: Use EXPORT_SYMBOL_FOR_KVM() for the mediated APIs
In reply to Peter ZijlstraThe following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 01122b89361e565b3c88b9fbebe92dc5c7420cb7 Gitweb: https://git.kernel.org/tip/01122b89361e565b3c88b9fbebe92dc5c7420cb7 Author: Peter Zijlstra <[email protected]> AuthorDate: Wed, 17 Dec 2025 13:23:59 +01:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Fri, 19 Dec 2025 08:54:59 +01:00
perf: Use EXPORT_SYMBOL_FOR_KVM() for the mediated APIs
--- arch/x86/events/core.c | 5 +++-- include/asm-generic/Kbuild | 1 + kernel/events/core.c | 5 +++-- 3 files changed, 7 insertions(+), 4 deletions(-)
arch/x86/events/core.c +3 -2
@@ -30,6 +30,7 @@#include <linux/device.h> #include <linux/nospec.h> #include <linux/static_call.h> +#include <linux/kvm_types.h> #include <asm/apic.h> #include <asm/stacktrace.h>
@@ -1771,14 +1772,14 @@ void perf_load_guest_lvtpc(u32 guest_lvtpc) APIC_DM_FIXED | PERF_GUEST_MEDIATED_PMI_VECTOR | masked);this_cpu_write (guest_lvtpc_loaded, true); } -EXPORT_SYMBOL_FOR_MODULES (perf_load_guest_lvtpc,"kvm" ); +EXPORT_SYMBOL_FOR_KVM (perf_load_guest_lvtpc);void perf_put_guest_lvtpc (void ) {this_cpu_write (guest_lvtpc_loaded, false);apic_write (APIC_LVTPC ,APIC_DM_NMI ); } -EXPORT_SYMBOL_FOR_MODULES (perf_put_guest_lvtpc,"kvm" ); +EXPORT_SYMBOL_FOR_KVM (perf_put_guest_lvtpc);#endif /* CONFIG_PERF_GUEST_MEDIATED_PMU */ static int
include/asm-generic/Kbuild +1 -0
@@ -32,6 +32,7 @@ mandatory-y += irq_work.h mandatory-y += kdebug.h mandatory-y += kmap_size.h mandatory-y += kprobes.h +mandatory-y += kvm_types.h mandatory-y += linkage.h mandatory-y += local.h mandatory-y += local64.h
kernel/events/core.c +3 -2
@@ -57,6 +57,7 @@#include <linux/task_work.h> #include <linux/percpu-rwsem.h> #include <linux/unwind_deferred.h> +#include <linux/kvm_types.h> #include "internal.h"
@@ -6388,7 +6389,7 @@ int perf_create_mediated_pmu(void)atomic_inc (&nr_mediated_pmu_vms);return 0 ; } -EXPORT_SYMBOL_GPL (perf_create_mediated_pmu); +EXPORT_SYMBOL_FOR_KVM (perf_create_mediated_pmu);void perf_release_mediated_pmu (void ) {
@@ -6397,7 +6398,7 @@ void perf_release_mediated_pmu(void)atomic_dec (&nr_mediated_pmu_vms); } -EXPORT_SYMBOL_GPL (perf_release_mediated_pmu); +EXPORT_SYMBOL_FOR_KVM (perf_release_mediated_pmu);/* When loading a guest's mediated PMU, schedule out all exclude_guest events. */ void perf_load_guest_context (void )
[tip: perf/core] perf: Add APIs to create/release mediated guest vPMUs
In reply to Sean ChristophersonThe following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: eff95e170275d9e80b968f335cd03d0ac250d2d1 Gitweb: https://git.kernel.org/tip/eff95e170275d9e80b968f335cd03d0ac250d2d1 Author: Kan Liang <[email protected]> AuthorDate: Fri, 05 Dec 2025 16:16:40 -08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Dec 2025 13:31:04 +01:00
perf: Add APIs to create/release mediated guest vPMUs
Currently, exposing PMU capabilities to a KVM guest is done by emulating guest PMCs via host perf events, i.e. by having KVM be "just" another user of perf. As a result, the guest and host are effectively competing for resources, and emulating guest accesses to vPMU resources requires expensive actions (expensive relative to the native instruction). The overhead and resource competition results in degraded guest performance and ultimately very poor vPMU accuracy.
To address the issues with the perf-emulated vPMU, introduce a "mediated vPMU", where the data plane (PMCs and enable/disable knobs) is exposed directly to the guest, but the control plane (event selectors and access to fixed counters) is managed by KVM (via MSR interceptions). To allow host perf usage of the PMU to (partially) co-exist with KVM/guest usage of the PMU, KVM and perf will coordinate to a world switch between host perf context and guest vPMU context near VM-Enter/VM-Exit.
Add two exported APIs, perf_{create,release}_mediated_pmu(), to allow KVM to create and release a mediated PMU instance (per VM). Because host perf context will be deactivated while the guest is running, mediated PMU usage will be mutually exclusive with perf analysis of the guest, i.e. perf events that do NOT exclude the guest will not behave as expected.
To avoid silent failure of !exclude_guest perf events, disallow creating a mediated PMU if there are active !exclude_guest events, and on the perf side, disallowing creating new !exclude_guest perf events while there is at least one active mediated PMU.
Exempt PMU resources that do not support mediated PMU usage, i.e. that are outside the scope/view of KVM's vPMU and will not be swapped out while the guest is running.
Guard mediated PMU with a new kconfig to help readers identify code paths that are unique to mediated PMU support, and to allow for adding arch- specific hooks without stubs. KVM x86 is expected to be the only KVM architecture to support a mediated PMU in the near future (e.g. arm64 is trending toward a partitioned PMU implementation), and KVM x86 will select PERF_GUEST_MEDIATED_PMU unconditionally, i.e. won't need stubs.
Immediately select PERF_GUEST_MEDIATED_PMU when KVM x86 is enabled so that all paths are compile tested. Full KVM support is on its way...
--- arch/x86/kvm/Kconfig | 1 +- include/linux/perf_event.h | 6 +++- init/Kconfig | 4 ++- kernel/events/core.c | 82 +++++++++++++++++++++++++++++++++++++- 4 files changed, 93 insertions(+)
arch/x86/kvm/Kconfig +1 -0
@@ -37,6 +37,7 @@ config KVM_X86 select SCHED_INFO select PERF_EVENTS select GUEST_PERF_EVENTS + select PERF_GUEST_MEDIATED_PMU select HAVE_KVM_MSI select HAVE_KVM_CPU_RELAX_INTERCEPT select HAVE_KVM_NO_POLL
include/linux/perf_event.h +6 -0
@@ -305,6 +305,7 @@ struct perf_event_pmu_context;#define PERF_PMU_CAP_EXTENDED_HW_TYPE 0x0100#define PERF_PMU_CAP_AUX_PAUSE 0x0200#define PERF_PMU_CAP_AUX_PREFER_LARGE 0x0400 +#define PERF_PMU_CAP_MEDIATED_VPMU 0x0800/** * pmu::scope
@@ -1914,6 +1915,11 @@ extern int perf_event_account_interrupt(struct perf_event *event);extern int perf_event_period (struct perf_event *event,u64 value);extern u64 perf_event_pause (struct perf_event *event,bool reset); +#ifdef CONFIG_PERF_GUEST_MEDIATED_PMU +int perf_create_mediated_pmu (void ); +void perf_release_mediated_pmu (void ); +#endif +#else /* !CONFIG_PERF_EVENTS: */ static inline void *
init/Kconfig +4 -0
@@ -2061,6 +2061,10 @@ config GUEST_PERF_EVENTS bool depends on HAVE_PERF_EVENTS +config PERF_GUEST_MEDIATED_PMU + bool + depends on GUEST_PERF_EVENTS + config PERF_USE_VMALLOC bool help
kernel/events/core.c +82 -0
106 lines not on this page · raw message
Sean Christopherson
maintainerPATCH v6 5/44 perf: Clean up perf ctx time
In reply to Sean ChristophersonFrom: Kan Liang <[email protected]>
The current perf tracks two timestamps for the normal ctx and cgroup. The same type of variables and similar codes are used to track the timestamps. In the following patch, the third timestamp to track the guest time will be introduced.
To avoid the code duplication, add a new struct perf_time_ctx and factor out a generic function update_perf_time_ctx().
No functional change.
--- include/linux/perf_event.h | 13 +++---- kernel/events/core.c | 70 +++++++++++++++++--------------------- 2 files changed, 39 insertions(+), 44 deletions(-)
include/linux/perf_event.h +7 -6
@@ -999,6 +999,11 @@ struct perf_event_groups {u64 index; }; +struct perf_time_ctx { +u64 time; +u64 stamp; +u64 offset; +};/** * struct perf_event_context - event context structure
@@ -1037,9 +1042,7 @@ struct perf_event_context {/* * Context clock, runs when context enabled. */ -u64 time; -u64 timestamp; -u64 timeoffset; +struct perf_time_ctx time;/* * These fields let us detect when two contexts have both
@@ -1172,9 +1175,7 @@ struct bpf_perf_event_data_kern {* This is a per-cpu dynamically allocated data structure. */ struct perf_cgroup_info { -u64 time; -u64 timestamp; -u64 timeoffset; +struct perf_time_ctx time;int active; };
kernel/events/core.c +32 -38
146 lines not on this page · raw message
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
[tip: perf/core] perf: Clean up perf ctx time
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: f5c7de8f84a152d559256aa4d0fc953118b73ca4 Gitweb: https://git.kernel.org/tip/f5c7de8f84a152d559256aa4d0fc953118b73ca4 Author: Kan Liang <[email protected]> AuthorDate: Fri, 05 Dec 2025 16:16:41 -08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Dec 2025 13:31:04 +01:00
perf: Clean up perf ctx time
The current perf tracks two timestamps for the normal ctx and cgroup. The same type of variables and similar codes are used to track the timestamps. In the following patch, the third timestamp to track the guest time will be introduced.
To avoid the code duplication, add a new struct perf_time_ctx and factor out a generic function update_perf_time_ctx().
No functional change.
--- include/linux/perf_event.h | 13 +++---- kernel/events/core.c | 70 ++++++++++++++++--------------------- 2 files changed, 39 insertions(+), 44 deletions(-)
include/linux/perf_event.h +7 -6
@@ -999,6 +999,11 @@ struct perf_event_groups {u64 index; }; +struct perf_time_ctx { +u64 time; +u64 stamp; +u64 offset; +};/** * struct perf_event_context - event context structure
@@ -1037,9 +1042,7 @@ struct perf_event_context {/* * Context clock, runs when context enabled. */ -u64 time; -u64 timestamp; -u64 timeoffset; +struct perf_time_ctx time;/* * These fields let us detect when two contexts have both
@@ -1172,9 +1175,7 @@ struct bpf_perf_event_data_kern {* This is a per-cpu dynamically allocated data structure. */ struct perf_cgroup_info { -u64 time; -u64 timestamp; -u64 timeoffset; +struct perf_time_ctx time;int active; };
kernel/events/core.c +32 -38
146 lines not on this page · raw message
Sean Christopherson
maintainerPATCH v6 6/44 perf: Add a EVENT_GUEST flag
In reply to Sean ChristophersonFrom: Kan Liang <[email protected]>
Current perf doesn't explicitly schedule out all exclude_guest events while the guest is running. There is no problem with the current emulated vPMU. Because perf owns all the PMU counters. It can mask the counter which is assigned to an exclude_guest event when a guest is running (Intel way), or set the corresponding HOSTONLY bit in evsentsel (AMD way). The counter doesn't count when a guest is running.
However, either way doesn't work with the introduced mediated vPMU. A guest owns all the PMU counters when it's running. The host should not mask any counters. The counter may be used by the guest. The evsentsel may be overwritten.
Perf should explicitly schedule out all exclude_guest events to release the PMU resources when entering a guest, and resume the counting when exiting the guest.
It's possible that an exclude_guest event is created when a guest is running. The new event should not be scheduled in as well.
The ctx time is shared among different PMUs. The time cannot be stopped when a guest is running. It is required to calculate the time for events from other PMUs, e.g., uncore events. Add timeguest to track the guest run time. For an exclude_guest event, the elapsed time equals the ctx time - guest time.
Cgroup has dedicated times. Use the same method to deduct the guest time from the cgroup time as well.
--- include/linux/perf_event.h | 6 + kernel/events/core.c | 232 ++++++++++++++++++++++++++++--------- 2 files changed, 186 insertions(+), 52 deletions(-)
include/linux/perf_event.h +6 -0
@@ -1044,6 +1044,11 @@ struct perf_event_context {*/ struct perf_time_ctx time; +/* +* Context clock, runs when in the guest mode. +*/ +struct perf_time_ctx timeguest; +/* * These fields let us detect when two contexts have both * been cloned (inherited) from a common ancestor.
@@ -1176,6 +1181,7 @@ struct bpf_perf_event_data_kern {*/ struct perf_cgroup_info {struct perf_time_ctx time; +struct perf_time_ctx timeguest;int active; };
kernel/events/core.c +180 -52
434 lines not on this page · raw message
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
[tip: perf/core] perf: Add a EVENT_GUEST flag
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 4593b4b6e218a0f21afbacc8124cf469d2d04094 Gitweb: https://git.kernel.org/tip/4593b4b6e218a0f21afbacc8124cf469d2d04094 Author: Kan Liang <[email protected]> AuthorDate: Fri, 05 Dec 2025 16:16:42 -08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Dec 2025 13:31:05 +01:00
perf: Add a EVENT_GUEST flag
Current perf doesn't explicitly schedule out all exclude_guest events while the guest is running. There is no problem with the current emulated vPMU. Because perf owns all the PMU counters. It can mask the counter which is assigned to an exclude_guest event when a guest is running (Intel way), or set the corresponding HOSTONLY bit in evsentsel (AMD way). The counter doesn't count when a guest is running.
However, either way doesn't work with the introduced mediated vPMU. A guest owns all the PMU counters when it's running. The host should not mask any counters. The counter may be used by the guest. The evsentsel may be overwritten.
Perf should explicitly schedule out all exclude_guest events to release the PMU resources when entering a guest, and resume the counting when exiting the guest.
It's possible that an exclude_guest event is created when a guest is running. The new event should not be scheduled in as well.
The ctx time is shared among different PMUs. The time cannot be stopped when a guest is running. It is required to calculate the time for events from other PMUs, e.g., uncore events. Add timeguest to track the guest run time. For an exclude_guest event, the elapsed time equals the ctx time - guest time.
Cgroup has dedicated times. Use the same method to deduct the guest time from the cgroup time as well.
--- include/linux/perf_event.h | 6 +- kernel/events/core.c | 230 ++++++++++++++++++++++++++++-------- 2 files changed, 185 insertions(+), 51 deletions(-)
include/linux/perf_event.h +6 -0
@@ -1045,6 +1045,11 @@ struct perf_event_context {struct perf_time_ctx time;/* +* Context clock, runs when in the guest mode. +*/ +struct perf_time_ctx timeguest; + +/* * These fields let us detect when two contexts have both * been cloned (inherited) from a common ancestor. */
@@ -1176,6 +1181,7 @@ struct bpf_perf_event_data_kern {*/ struct perf_cgroup_info {struct perf_time_ctx time; +struct perf_time_ctx timeguest;int active; };
kernel/events/core.c +179 -51
432 lines not on this page · raw message
Sean Christopherson
maintainerPATCH v6 7/44 perf: Add APIs to load/put guest mediated PMU context
In reply to Sean ChristophersonFrom: Kan Liang <[email protected]>
Add exported APIs to load/put a guest mediated PMU context. KVM will load the guest PMU shortly before VM-Enter, and put the guest PMU shortly after VM-Exit.
On the perf side of things, schedule out all exclude_guest events when the guest context is loaded, and schedule them back in when the guest context is put. I.e. yield the hardware PMU resources to the guest, by way of KVM.
Note, perf is only responsible for managing host context. KVM is responsible for loading/storing guest state to/from hardware.
--- include/linux/perf_event.h | 2 ++ kernel/events/core.c | 61 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+)
include/linux/perf_event.h +2 -0
@@ -1925,6 +1925,8 @@ extern u64 perf_event_pause(struct perf_event *event, bool reset);#ifdef CONFIG_PERF_GUEST_MEDIATED_PMU int perf_create_mediated_pmu (void );void perf_release_mediated_pmu (void ); +void perf_load_guest_context (void ); +void perf_put_guest_context (void );#endif #else /* !CONFIG_PERF_EVENTS: */
kernel/events/core.c +61 -0
77 lines not on this page · raw message
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
[tip: perf/core] perf: Add APIs to load/put guest mediated PMU context
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 42457a7fb6cacca83be4deaf202ac3e45830daf2 Gitweb: https://git.kernel.org/tip/42457a7fb6cacca83be4deaf202ac3e45830daf2 Author: Kan Liang <[email protected]> AuthorDate: Fri, 05 Dec 2025 16:16:43 -08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Dec 2025 13:31:05 +01:00
perf: Add APIs to load/put guest mediated PMU context
Add exported APIs to load/put a guest mediated PMU context. KVM will load the guest PMU shortly before VM-Enter, and put the guest PMU shortly after VM-Exit.
On the perf side of things, schedule out all exclude_guest events when the guest context is loaded, and schedule them back in when the guest context is put. I.e. yield the hardware PMU resources to the guest, by way of KVM.
Note, perf is only responsible for managing host context. KVM is responsible for loading/storing guest state to/from hardware.
--- include/linux/perf_event.h | 2 +- kernel/events/core.c | 61 +++++++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+)
include/linux/perf_event.h +2 -0
@@ -1925,6 +1925,8 @@ extern u64 perf_event_pause(struct perf_event *event, bool reset);#ifdef CONFIG_PERF_GUEST_MEDIATED_PMU int perf_create_mediated_pmu (void );void perf_release_mediated_pmu (void ); +void perf_load_guest_context (void ); +void perf_put_guest_context (void );#endif #else /* !CONFIG_PERF_EVENTS: */
kernel/events/core.c +61 -0
77 lines not on this page · raw message
Sean Christopherson
maintainerPATCH v6 8/44 perf/x86/core: Register a new vector for handling mediated guest PMIs
In reply to Sean ChristophersonWire up system vector 0xf5 for handling PMIs (i.e. interrupts delivered through the LVTPC) while running KVM guests with a mediated PMU. Perf currently delivers all PMIs as NMIs, e.g. so that events that trigger while IRQs are disabled aren't delayed and generate useless records, but due to the multiplexing of NMIs throughout the system, correctly identifying NMIs for a mediated PMU is practically infeasible.
To (greatly) simplify identifying guest mediated PMU PMIs, perf will switch the CPU's LVTPC between PERF_GUEST_MEDIATED_PMI_VECTOR and NMI when guest PMU context is loaded/put. I.e. PMIs that are generated by the CPU while the guest is active will be identified purely based on the IRQ vector.
Route the vector through perf, e.g. as opposed to letting KVM attach a handler directly a la posted interrupt notification vectors, as perf owns the LVTPC and thus is the rightful owner of PERF_GUEST_MEDIATED_PMI_VECTOR. Functionally, having KVM directly own the vector would be fine (both KVM and perf will be completely aware of when a mediated PMU is active), but would lead to an undesirable split in ownership: perf would be responsible for installing the vector, but not handling the resulting IRQs.
Add a new perf_guest_info_callbacks hook (and static call) to allow KVM to register its handler with perf when running guests with mediated PMUs.
Note, because KVM always runs guests with host IRQs enabled, there is no danger of a PMI being delayed from the guest's perspective due to using a regular IRQ instead of an NMI.
--- arch/x86/entry/entry_fred.c | 1 + arch/x86/include/asm/hardirq.h | 3 +++ arch/x86/include/asm/idtentry.h | 6 ++++++ arch/x86/include/asm/irq_vectors.h | 4 +++- arch/x86/kernel/idt.c | 3 +++ arch/x86/kernel/irq.c | 19 +++++++++++++++++++ include/linux/perf_event.h | 8 ++++++++ kernel/events/core.c | 9 +++++++-- .../beauty/arch/x86/include/asm/irq_vectors.h | 3 ++- virt/kvm/kvm_main.c | 3 +++ 10 files changed, 55 insertions(+), 4 deletions(-)
arch/x86/entry/entry_fred.c +1 -0
@@ -114,6 +114,7 @@ static idtentry_t sysvec_table[NR_SYSTEM_VECTORS] __ro_after_init = {SYSVEC (IRQ_WORK_VECTOR , irq_work), +SYSVEC (PERF_GUEST_MEDIATED_PMI_VECTOR , perf_guest_mediated_pmi_handler),SYSVEC (POSTED_INTR_VECTOR , kvm_posted_intr_ipi),SYSVEC (POSTED_INTR_WAKEUP_VECTOR , kvm_posted_intr_wakeup_ipi), SYSVEC(POSTED_INTR_NESTED_VECTOR, kvm_posted_intr_nested_ipi),
arch/x86/include/asm/hardirq.h +3 -0
@@ -18,6 +18,9 @@ typedef struct {unsigned int kvm_posted_intr_ipis;unsigned int kvm_posted_intr_wakeup_ipis;unsigned int kvm_posted_intr_nested_ipis; +#endif +#ifdef CONFIG_GUEST_PERF_EVENTS +unsigned int perf_guest_mediated_pmis;#endif unsigned int x86_platform_ipis;/* arch dependent */ unsigned int apic_perf_irqs;
arch/x86/include/asm/idtentry.h +6 -0
@@ -746,6 +746,12 @@ DECLARE_IDTENTRY_SYSVEC(POSTED_INTR_NESTED_VECTOR, sysvec_kvm_posted_intr_nested# define fred_sysvec_kvm_posted_intr_nested_ipi NULL#endif +# ifdef CONFIG_GUEST_PERF_EVENTS +DECLARE_IDTENTRY_SYSVEC (PERF_GUEST_MEDIATED_PMI_VECTOR , sysvec_perf_guest_mediated_pmi_handler); +#else +# define fred_sysvec_perf_guest_mediated_pmi_handler NULL +#endif +# ifdef CONFIG_X86_POSTED_MSI DECLARE_IDTENTRY_SYSVEC (POSTED_MSI_NOTIFICATION_VECTOR , sysvec_posted_msi_notification);#else
arch/x86/include/asm/irq_vectors.h +3 -1
@@ -77,7 +77,9 @@*/ #define IRQ_WORK_VECTOR 0xf6 -/* 0xf5 - unused, was UV_BAU_MESSAGE */ +/* IRQ vector for PMIs when running a guest with a mediated PMU. */ +#define PERF_GUEST_MEDIATED_PMI_VECTOR 0xf5 +#define DEFERRED_ERROR_VECTOR 0xf4/* Vector on which hypervisor callbacks will be delivered */
arch/x86/kernel/idt.c +3 -0
@@ -158,6 +158,9 @@ static const __initconst struct idt_data apic_idts[] = { INTG(POSTED_INTR_WAKEUP_VECTOR, asm_sysvec_kvm_posted_intr_wakeup_ipi), INTG(POSTED_INTR_NESTED_VECTOR, asm_sysvec_kvm_posted_intr_nested_ipi),# endif +#ifdef CONFIG_GUEST_PERF_EVENTS + INTG(PERF_GUEST_MEDIATED_PMI_VECTOR, asm_sysvec_perf_guest_mediated_pmi_handler), +#endif # ifdef CONFIG_IRQ_WORK INTG(IRQ_WORK_VECTOR, asm_sysvec_irq_work),# endif
arch/x86/kernel/irq.c +19 -0
@@ -191,6 +191,13 @@ int arch_show_interrupts(struct seq_file *p, int prec) irq_stats(j)->kvm_posted_intr_wakeup_ipis);seq_puts (p," Posted-interrupt wakeup event\n" );#endif +#ifdef CONFIG_GUEST_PERF_EVENTS +seq_printf (p,"%*s: " , prec,"VPMI" ); +for_each_online_cpu (j) +seq_printf (p,"%10u " , +irq_stats (j)->perf_guest_mediated_pmis); +seq_puts (p," Perf Guest Mediated PMI\n" ); +#endif #ifdef CONFIG_X86_POSTED_MSI seq_printf (p,"%*s: " , prec,"PMN" );for_each_online_cpu (j)
@@ -348,6 +355,18 @@ DEFINE_IDTENTRY_SYSVEC(sysvec_x86_platform_ipi) }#endif +#ifdef CONFIG_GUEST_PERF_EVENTS +/* +* Handler for PERF_GUEST_MEDIATED_PMI_VECTOR. +*/ +DEFINE_IDTENTRY_SYSVEC (sysvec_perf_guest_mediated_pmi_handler) +{ +apic_eoi (); +inc_irq_stat (perf_guest_mediated_pmis); +perf_guest_handle_mediated_pmi (); +} +#endif +#if IS_ENABLED (CONFIG_KVM )static void dummy_handler (void ) {}static void (*kvm_posted_intr_wakeup_handler)(void ) = dummy_handler;
include/linux/perf_event.h +8 -0
@@ -1677,6 +1677,8 @@ struct perf_guest_info_callbacks {unsigned int (*state)(void );unsigned long (*get_ip)(void );unsigned int (*handle_intel_pt_intr)(void ); + +void (*handle_mediated_pmi)(void ); };#ifdef CONFIG_GUEST_PERF_EVENTS
@@ -1686,6 +1688,7 @@ extern struct perf_guest_info_callbacks __rcu *perf_guest_cbs;DECLARE_STATIC_CALL (__perf_guest_state, *perf_guest_cbs->state);DECLARE_STATIC_CALL (__perf_guest_get_ip, *perf_guest_cbs->get_ip);DECLARE_STATIC_CALL (__perf_guest_handle_intel_pt_intr, *perf_guest_cbs->handle_intel_pt_intr); +DECLARE_STATIC_CALL (__perf_guest_handle_mediated_pmi, *perf_guest_cbs->handle_mediated_pmi);static inline unsigned int perf_guest_state (void ) {
@@ -1702,6 +1705,11 @@ static inline unsigned int perf_guest_handle_intel_pt_intr(void)return static_call (__perf_guest_handle_intel_pt_intr)(); } +static inline void perf_guest_handle_mediated_pmi (void ) +{ +static_call (__perf_guest_handle_mediated_pmi)(); +} +extern void perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *cbs);extern void perf_unregister_guest_info_callbacks (struct perf_guest_info_callbacks *cbs);
kernel/events/core.c +7 -2
@@ -7645,6 +7645,7 @@ struct perf_guest_info_callbacks __rcu *perf_guest_cbs;DEFINE_STATIC_CALL_RET0 (__perf_guest_state, *perf_guest_cbs->state);DEFINE_STATIC_CALL_RET0 (__perf_guest_get_ip, *perf_guest_cbs->get_ip);DEFINE_STATIC_CALL_RET0 (__perf_guest_handle_intel_pt_intr, *perf_guest_cbs->handle_intel_pt_intr); +DEFINE_STATIC_CALL_RET0 (__perf_guest_handle_mediated_pmi, *perf_guest_cbs->handle_mediated_pmi);void perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *cbs) {
@@ -7659,6 +7660,10 @@ void perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)if (cbs->handle_intel_pt_intr)static_call_update (__perf_guest_handle_intel_pt_intr, cbs->handle_intel_pt_intr); + +if (cbs->handle_mediated_pmi) +static_call_update (__perf_guest_handle_mediated_pmi, + cbs->handle_mediated_pmi); }EXPORT_SYMBOL_GPL (perf_register_guest_info_callbacks);
@@ -7670,8 +7675,8 @@ void perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)rcu_assign_pointer (perf_guest_cbs,NULL );static_call_update (__perf_guest_state, (void *)&__static_call_return0);static_call_update (__perf_guest_get_ip, (void *)&__static_call_return0); -static_call_update (__perf_guest_handle_intel_pt_intr, - (void *)&__static_call_return0); +static_call_update (__perf_guest_handle_intel_pt_intr, (void *)&__static_call_return0); +static_call_update (__perf_guest_handle_mediated_pmi, (void *)&__static_call_return0);synchronize_rcu (); }EXPORT_SYMBOL_GPL (perf_unregister_guest_info_callbacks);
tools/perf/trace/beauty/arch/x86/include/asm/irq_vectors.h +2 -1
@@ -77,7 +77,8 @@*/ #define IRQ_WORK_VECTOR 0xf6 -/* 0xf5 - unused, was UV_BAU_MESSAGE */ +#define PERF_GUEST_MEDIATED_PMI_VECTOR 0xf5 +#define DEFERRED_ERROR_VECTOR 0xf4/* Vector on which hypervisor callbacks will be delivered */
virt/kvm/kvm_main.c +3 -0
@@ -6471,11 +6471,14 @@ static struct perf_guest_info_callbacks kvm_guest_cbs = { .state = kvm_guest_state, .get_ip = kvm_guest_get_ip, .handle_intel_pt_intr = NULL, + .handle_mediated_pmi = NULL, };void kvm_register_perf_callbacks (unsigned int (*pt_intr_handler)(void )) { kvm_guest_cbs.handle_intel_pt_intr = pt_intr_handler; + kvm_guest_cbs.handle_mediated_pmi =NULL ; +perf_register_guest_info_callbacks (&kvm_guest_cbs); }void kvm_unregister_perf_callbacks (void )
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
[tip: perf/core] perf/x86/core: Register a new vector for handling mediated guest PMIs
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: a05385d84b2af64600fc84b027bea481e8f6261d Gitweb: https://git.kernel.org/tip/a05385d84b2af64600fc84b027bea481e8f6261d Author: Sean Christopherson <[email protected]> AuthorDate: Fri, 05 Dec 2025 16:16:44 -08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Dec 2025 13:31:05 +01:00
perf/x86/core: Register a new vector for handling mediated guest PMIs
Wire up system vector 0xf5 for handling PMIs (i.e. interrupts delivered through the LVTPC) while running KVM guests with a mediated PMU. Perf currently delivers all PMIs as NMIs, e.g. so that events that trigger while IRQs are disabled aren't delayed and generate useless records, but due to the multiplexing of NMIs throughout the system, correctly identifying NMIs for a mediated PMU is practically infeasible.
To (greatly) simplify identifying guest mediated PMU PMIs, perf will switch the CPU's LVTPC between PERF_GUEST_MEDIATED_PMI_VECTOR and NMI when guest PMU context is loaded/put. I.e. PMIs that are generated by the CPU while the guest is active will be identified purely based on the IRQ vector.
Route the vector through perf, e.g. as opposed to letting KVM attach a handler directly a la posted interrupt notification vectors, as perf owns the LVTPC and thus is the rightful owner of PERF_GUEST_MEDIATED_PMI_VECTOR. Functionally, having KVM directly own the vector would be fine (both KVM and perf will be completely aware of when a mediated PMU is active), but would lead to an undesirable split in ownership: perf would be responsible for installing the vector, but not handling the resulting IRQs.
Add a new perf_guest_info_callbacks hook (and static call) to allow KVM to register its handler with perf when running guests with mediated PMUs.
Note, because KVM always runs guests with host IRQs enabled, there is no danger of a PMI being delayed from the guest's perspective due to using a regular IRQ instead of an NMI.
--- arch/x86/entry/entry_fred.c | 1 +- arch/x86/include/asm/hardirq.h | 3 +- arch/x86/include/asm/idtentry.h | 6 ++- arch/x86/include/asm/irq_vectors.h | 4 +- arch/x86/kernel/idt.c | 3 +- arch/x86/kernel/irq.c | 19 +++++++- include/linux/perf_event.h | 8 +++- kernel/events/core.c | 9 ++- tools/perf/trace/beauty/arch/x86/include/asm/irq_vectors.h | 3 +- virt/kvm/kvm_main.c | 3 +- 10 files changed, 55 insertions(+), 4 deletions(-)
arch/x86/entry/entry_fred.c +1 -0
@@ -114,6 +114,7 @@ static idtentry_t sysvec_table[NR_SYSTEM_VECTORS] __ro_after_init = {SYSVEC (IRQ_WORK_VECTOR , irq_work), +SYSVEC (PERF_GUEST_MEDIATED_PMI_VECTOR , perf_guest_mediated_pmi_handler),SYSVEC (POSTED_INTR_VECTOR , kvm_posted_intr_ipi),SYSVEC (POSTED_INTR_WAKEUP_VECTOR , kvm_posted_intr_wakeup_ipi), SYSVEC(POSTED_INTR_NESTED_VECTOR, kvm_posted_intr_nested_ipi),
arch/x86/include/asm/hardirq.h +3 -0
@@ -19,6 +19,9 @@ typedef struct {unsigned int kvm_posted_intr_wakeup_ipis;unsigned int kvm_posted_intr_nested_ipis;#endif +#ifdef CONFIG_GUEST_PERF_EVENTS +unsigned int perf_guest_mediated_pmis; +#endif unsigned int x86_platform_ipis;/* arch dependent */ unsigned int apic_perf_irqs;unsigned int apic_irq_work_irqs;
arch/x86/include/asm/idtentry.h +6 -0
@@ -746,6 +746,12 @@ DECLARE_IDTENTRY_SYSVEC(POSTED_INTR_NESTED_VECTOR, sysvec_kvm_posted_intr_nested# define fred_sysvec_kvm_posted_intr_nested_ipi NULL#endif +# ifdef CONFIG_GUEST_PERF_EVENTS +DECLARE_IDTENTRY_SYSVEC (PERF_GUEST_MEDIATED_PMI_VECTOR , sysvec_perf_guest_mediated_pmi_handler); +#else +# define fred_sysvec_perf_guest_mediated_pmi_handler NULL +#endif +# ifdef CONFIG_X86_POSTED_MSI DECLARE_IDTENTRY_SYSVEC (POSTED_MSI_NOTIFICATION_VECTOR , sysvec_posted_msi_notification);#else
arch/x86/include/asm/irq_vectors.h +3 -1
@@ -77,7 +77,9 @@*/ #define IRQ_WORK_VECTOR 0xf6 -/* 0xf5 - unused, was UV_BAU_MESSAGE */ +/* IRQ vector for PMIs when running a guest with a mediated PMU. */ +#define PERF_GUEST_MEDIATED_PMI_VECTOR 0xf5 +#define DEFERRED_ERROR_VECTOR 0xf4/* Vector on which hypervisor callbacks will be delivered */
arch/x86/kernel/idt.c +3 -0
@@ -158,6 +158,9 @@ static const __initconst struct idt_data apic_idts[] = { INTG(POSTED_INTR_WAKEUP_VECTOR, asm_sysvec_kvm_posted_intr_wakeup_ipi), INTG(POSTED_INTR_NESTED_VECTOR, asm_sysvec_kvm_posted_intr_nested_ipi),# endif +#ifdef CONFIG_GUEST_PERF_EVENTS + INTG(PERF_GUEST_MEDIATED_PMI_VECTOR, asm_sysvec_perf_guest_mediated_pmi_handler), +#endif # ifdef CONFIG_IRQ_WORK INTG(IRQ_WORK_VECTOR, asm_sysvec_irq_work),# endif
arch/x86/kernel/irq.c +19 -0
@@ -192,6 +192,13 @@ int arch_show_interrupts(struct seq_file *p, int prec) irq_stats(j)->kvm_posted_intr_wakeup_ipis);seq_puts (p," Posted-interrupt wakeup event\n" );#endif +#ifdef CONFIG_GUEST_PERF_EVENTS +seq_printf (p,"%*s: " , prec,"VPMI" ); +for_each_online_cpu (j) +seq_printf (p,"%10u " , +irq_stats (j)->perf_guest_mediated_pmis); +seq_puts (p," Perf Guest Mediated PMI\n" ); +#endif #ifdef CONFIG_X86_POSTED_MSI seq_printf (p,"%*s: " , prec,"PMN" );for_each_online_cpu (j)
@@ -349,6 +356,18 @@ DEFINE_IDTENTRY_SYSVEC(sysvec_x86_platform_ipi) }#endif +#ifdef CONFIG_GUEST_PERF_EVENTS +/* +* Handler for PERF_GUEST_MEDIATED_PMI_VECTOR. +*/ +DEFINE_IDTENTRY_SYSVEC (sysvec_perf_guest_mediated_pmi_handler) +{ +apic_eoi (); +inc_irq_stat (perf_guest_mediated_pmis); +perf_guest_handle_mediated_pmi (); +} +#endif +#if IS_ENABLED (CONFIG_KVM )static void dummy_handler (void ) {}static void (*kvm_posted_intr_wakeup_handler)(void ) = dummy_handler;
include/linux/perf_event.h +8 -0
@@ -1677,6 +1677,8 @@ struct perf_guest_info_callbacks {unsigned int (*state)(void );unsigned long (*get_ip)(void );unsigned int (*handle_intel_pt_intr)(void ); + +void (*handle_mediated_pmi)(void ); };#ifdef CONFIG_GUEST_PERF_EVENTS
@@ -1686,6 +1688,7 @@ extern struct perf_guest_info_callbacks __rcu *perf_guest_cbs;DECLARE_STATIC_CALL (__perf_guest_state, *perf_guest_cbs->state);DECLARE_STATIC_CALL (__perf_guest_get_ip, *perf_guest_cbs->get_ip);DECLARE_STATIC_CALL (__perf_guest_handle_intel_pt_intr, *perf_guest_cbs->handle_intel_pt_intr); +DECLARE_STATIC_CALL (__perf_guest_handle_mediated_pmi, *perf_guest_cbs->handle_mediated_pmi);static inline unsigned int perf_guest_state (void ) {
@@ -1702,6 +1705,11 @@ static inline unsigned int perf_guest_handle_intel_pt_intr(void)return static_call (__perf_guest_handle_intel_pt_intr)(); } +static inline void perf_guest_handle_mediated_pmi (void ) +{ +static_call (__perf_guest_handle_mediated_pmi)(); +} +extern void perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *cbs);extern void perf_unregister_guest_info_callbacks (struct perf_guest_info_callbacks *cbs);
kernel/events/core.c +7 -2
@@ -7644,6 +7644,7 @@ struct perf_guest_info_callbacks __rcu *perf_guest_cbs;DEFINE_STATIC_CALL_RET0 (__perf_guest_state, *perf_guest_cbs->state);DEFINE_STATIC_CALL_RET0 (__perf_guest_get_ip, *perf_guest_cbs->get_ip);DEFINE_STATIC_CALL_RET0 (__perf_guest_handle_intel_pt_intr, *perf_guest_cbs->handle_intel_pt_intr); +DEFINE_STATIC_CALL_RET0 (__perf_guest_handle_mediated_pmi, *perf_guest_cbs->handle_mediated_pmi);void perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *cbs) {
@@ -7658,6 +7659,10 @@ void perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)if (cbs->handle_intel_pt_intr)static_call_update (__perf_guest_handle_intel_pt_intr, cbs->handle_intel_pt_intr); + +if (cbs->handle_mediated_pmi) +static_call_update (__perf_guest_handle_mediated_pmi, + cbs->handle_mediated_pmi); }EXPORT_SYMBOL_GPL (perf_register_guest_info_callbacks);
@@ -7669,8 +7674,8 @@ void perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)rcu_assign_pointer (perf_guest_cbs,NULL );static_call_update (__perf_guest_state, (void *)&__static_call_return0);static_call_update (__perf_guest_get_ip, (void *)&__static_call_return0); -static_call_update (__perf_guest_handle_intel_pt_intr, - (void *)&__static_call_return0); +static_call_update (__perf_guest_handle_intel_pt_intr, (void *)&__static_call_return0); +static_call_update (__perf_guest_handle_mediated_pmi, (void *)&__static_call_return0);synchronize_rcu (); }EXPORT_SYMBOL_GPL (perf_unregister_guest_info_callbacks);
tools/perf/trace/beauty/arch/x86/include/asm/irq_vectors.h +2 -1
@@ -77,7 +77,8 @@*/ #define IRQ_WORK_VECTOR 0xf6 -/* 0xf5 - unused, was UV_BAU_MESSAGE */ +#define PERF_GUEST_MEDIATED_PMI_VECTOR 0xf5 +#define DEFERRED_ERROR_VECTOR 0xf4/* Vector on which hypervisor callbacks will be delivered */
virt/kvm/kvm_main.c +3 -0
@@ -6467,11 +6467,14 @@ static struct perf_guest_info_callbacks kvm_guest_cbs = { .state = kvm_guest_state, .get_ip = kvm_guest_get_ip, .handle_intel_pt_intr = NULL, + .handle_mediated_pmi = NULL, };void kvm_register_perf_callbacks (unsigned int (*pt_intr_handler)(void )) { kvm_guest_cbs.handle_intel_pt_intr = pt_intr_handler; + kvm_guest_cbs.handle_mediated_pmi =NULL ; +perf_register_guest_info_callbacks (&kvm_guest_cbs); }void kvm_unregister_perf_callbacks (void )
Sean Christopherson
maintainerPATCH v6 9/44 perf/x86/core: Add APIs to switch to/from mediated PMI vector (for KVM)
In reply to Sean ChristophersonAdd APIs (exported only for KVM) to switch PMIs to the dedicated mediated PMU IRQ vector when loading guest context, and back to perf's standard NMI when the guest context is put. I.e. route PMIs to PERF_GUEST_MEDIATED_PMI_VECTOR when the guest context is active, and to NMIs while the host context is active.
While running with guest context loaded, ignore all NMIs (in perf). Any NMI that arrives while the LVTPC points at the mediated PMU IRQ vector can't possibly be due to a host perf event.
--- arch/x86/events/core.c | 32 +++++++++++++++++++++++++++++++ arch/x86/include/asm/perf_event.h | 5 +++++ 2 files changed, 37 insertions(+)
arch/x86/events/core.c +32 -0
@@ -55,6 +55,8 @@ DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { .pmu = &pmu, }; +static DEFINE_PER_CPU(bool, guest_lvtpc_loaded); +DEFINE_STATIC_KEY_FALSE (rdpmc_never_available_key);DEFINE_STATIC_KEY_FALSE (rdpmc_always_available_key);DEFINE_STATIC_KEY_FALSE (perf_is_hybrid);
@@ -1749,6 +1751,25 @@ void perf_events_lapic_init(void)apic_write (APIC_LVTPC ,APIC_DM_NMI ); } +#ifdef CONFIG_PERF_GUEST_MEDIATED_PMU +void perf_load_guest_lvtpc (u32 guest_lvtpc) +{ +u32 masked = guest_lvtpc &APIC_LVT_MASKED ; + +apic_write (APIC_LVTPC , +APIC_DM_FIXED |PERF_GUEST_MEDIATED_PMI_VECTOR | masked); +this_cpu_write (guest_lvtpc_loaded, true); +} +EXPORT_SYMBOL_FOR_MODULES (perf_load_guest_lvtpc,"kvm" ); + +void perf_put_guest_lvtpc (void ) +{ +this_cpu_write (guest_lvtpc_loaded, false); +apic_write (APIC_LVTPC ,APIC_DM_NMI ); +} +EXPORT_SYMBOL_FOR_MODULES (perf_put_guest_lvtpc,"kvm" ); +#endif /* CONFIG_PERF_GUEST_MEDIATED_PMU */ +static int perf_event_nmi_handler (unsigned int cmd,struct pt_regs *regs) {
@@ -1756,6 +1777,17 @@ perf_event_nmi_handler(unsigned int cmd, struct pt_regs *regs)u64 finish_clock;int ret; +/* +* Ignore all NMIs when the CPU's LVTPC is configured to route PMIs to +* PERF_GUEST_MEDIATED_PMI_VECTOR, i.e. when an NMI time can't be due +* to a PMI. Attempting to handle a PMI while the guest's context is +* loaded will generate false positives and clobber guest state. Note, +* the LVTPC is switched to/from the dedicated mediated PMI IRQ vector +* while host events are quiesced. +*/ +if (this_cpu_read (guest_lvtpc_loaded)) +return NMI_DONE ; +/* * All PMUs/events that share this PMI handler should make sure to * increment active_events for their events.
arch/x86/include/asm/perf_event.h +5 -0
@@ -651,6 +651,11 @@ static inline void perf_events_lapic_init(void) { }static inline void perf_check_microcode (void ) { }#endif +#ifdef CONFIG_PERF_GUEST_MEDIATED_PMU +extern void perf_load_guest_lvtpc (u32 guest_lvtpc); +extern void perf_put_guest_lvtpc (void ); +#endif +#if defined(CONFIG_PERF_EVENTS ) && defined(CONFIG_CPU_SUP_INTEL )extern struct perf_guest_switch_msr *perf_guest_get_msrs (int *nr,void *data);extern void x86_perf_get_lbr (struct x86_pmu_lbr *lbr);
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
[tip: perf/core] perf/x86/core: Add APIs to switch to/from mediated PMI vector (for KVM)
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 560ac136f25da2da44a8b68d581adfdc8230b7e2 Gitweb: https://git.kernel.org/tip/560ac136f25da2da44a8b68d581adfdc8230b7e2 Author: Sean Christopherson <[email protected]> AuthorDate: Fri, 05 Dec 2025 16:16:45 -08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Dec 2025 13:31:06 +01:00
perf/x86/core: Add APIs to switch to/from mediated PMI vector (for KVM)
Add APIs (exported only for KVM) to switch PMIs to the dedicated mediated PMU IRQ vector when loading guest context, and back to perf's standard NMI when the guest context is put. I.e. route PMIs to PERF_GUEST_MEDIATED_PMI_VECTOR when the guest context is active, and to NMIs while the host context is active.
While running with guest context loaded, ignore all NMIs (in perf). Any NMI that arrives while the LVTPC points at the mediated PMU IRQ vector can't possibly be due to a host perf event.
--- arch/x86/events/core.c | 32 ++++++++++++++++++++++++++++++- arch/x86/include/asm/perf_event.h | 5 +++++- 2 files changed, 37 insertions(+)
arch/x86/events/core.c +32 -0
@@ -56,6 +56,8 @@ DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { .pmu = &pmu, }; +static DEFINE_PER_CPU(bool, guest_lvtpc_loaded); +DEFINE_STATIC_KEY_FALSE (rdpmc_never_available_key);DEFINE_STATIC_KEY_FALSE (rdpmc_always_available_key);DEFINE_STATIC_KEY_FALSE (perf_is_hybrid);
@@ -1760,6 +1762,25 @@ void perf_events_lapic_init(void)apic_write (APIC_LVTPC ,APIC_DM_NMI ); } +#ifdef CONFIG_PERF_GUEST_MEDIATED_PMU +void perf_load_guest_lvtpc (u32 guest_lvtpc) +{ +u32 masked = guest_lvtpc &APIC_LVT_MASKED ; + +apic_write (APIC_LVTPC , +APIC_DM_FIXED |PERF_GUEST_MEDIATED_PMI_VECTOR | masked); +this_cpu_write (guest_lvtpc_loaded, true); +} +EXPORT_SYMBOL_FOR_MODULES (perf_load_guest_lvtpc,"kvm" ); + +void perf_put_guest_lvtpc (void ) +{ +this_cpu_write (guest_lvtpc_loaded, false); +apic_write (APIC_LVTPC ,APIC_DM_NMI ); +} +EXPORT_SYMBOL_FOR_MODULES (perf_put_guest_lvtpc,"kvm" ); +#endif /* CONFIG_PERF_GUEST_MEDIATED_PMU */ +static int perf_event_nmi_handler (unsigned int cmd,struct pt_regs *regs) {
@@ -1768,6 +1789,17 @@ perf_event_nmi_handler(unsigned int cmd, struct pt_regs *regs)int ret;/* +* Ignore all NMIs when the CPU's LVTPC is configured to route PMIs to +* PERF_GUEST_MEDIATED_PMI_VECTOR, i.e. when an NMI time can't be due +* to a PMI. Attempting to handle a PMI while the guest's context is +* loaded will generate false positives and clobber guest state. Note, +* the LVTPC is switched to/from the dedicated mediated PMI IRQ vector +* while host events are quiesced. +*/ +if (this_cpu_read (guest_lvtpc_loaded)) +return NMI_DONE ; + +/* * All PMUs/events that share this PMI handler should make sure to * increment active_events for their events. */
arch/x86/include/asm/perf_event.h +5 -0
@@ -759,6 +759,11 @@ static inline void perf_events_lapic_init(void) { }static inline void perf_check_microcode (void ) { }#endif +#ifdef CONFIG_PERF_GUEST_MEDIATED_PMU +extern void perf_load_guest_lvtpc (u32 guest_lvtpc); +extern void perf_put_guest_lvtpc (void ); +#endif +#if defined(CONFIG_PERF_EVENTS ) && defined(CONFIG_CPU_SUP_INTEL )extern struct perf_guest_switch_msr *perf_guest_get_msrs (int *nr,void *data);extern void x86_perf_get_lbr (struct x86_pmu_lbr *lbr);
Sean Christopherson
maintainerPATCH v6 10/44 perf/x86/core: Do not set bit width for unavailable counters
In reply to Sean ChristophersonFrom: Sandipan Das <[email protected]>
Not all x86 processors have fixed counters. It may also be the case that a processor has only fixed counters and no general-purpose counters. Set the bit widths corresponding to each counter type only if such counters are available.
--- arch/x86/events/core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/events/core.c +2 -2
@@ -3132,8 +3132,8 @@ void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap) cap->version = x86_pmu.version; cap->num_counters_gp =x86_pmu_num_counters (NULL ); cap->num_counters_fixed =x86_pmu_num_counters_fixed (NULL ); - cap->bit_width_gp = x86_pmu.cntval_bits; - cap->bit_width_fixed = x86_pmu.cntval_bits; + cap->bit_width_gp = cap->num_counters_gp ? x86_pmu.cntval_bits :0 ; + cap->bit_width_fixed = cap->num_counters_fixed ? x86_pmu.cntval_bits :0 ; cap->events_mask = (unsigned int )x86_pmu.events_maskl; cap->events_mask_len = x86_pmu.events_mask_len; cap->pebs_ept = x86_pmu.pebs_ept;
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
[tip: perf/core] perf/x86/core: Do not set bit width for unavailable counters
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: b456a6ba5756b6fb7e651775343e713bd08418e7 Gitweb: https://git.kernel.org/tip/b456a6ba5756b6fb7e651775343e713bd08418e7 Author: Sandipan Das <[email protected]> AuthorDate: Fri, 05 Dec 2025 16:16:46 -08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Dec 2025 13:31:06 +01:00
perf/x86/core: Do not set bit width for unavailable counters
Not all x86 processors have fixed counters. It may also be the case that a processor has only fixed counters and no general-purpose counters. Set the bit widths corresponding to each counter type only if such counters are available.
--- arch/x86/events/core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/events/core.c +2 -2
@@ -3105,8 +3105,8 @@ void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap) cap->version = x86_pmu.version; cap->num_counters_gp =x86_pmu_num_counters (NULL ); cap->num_counters_fixed =x86_pmu_num_counters_fixed (NULL ); - cap->bit_width_gp = x86_pmu.cntval_bits; - cap->bit_width_fixed = x86_pmu.cntval_bits; + cap->bit_width_gp = cap->num_counters_gp ? x86_pmu.cntval_bits :0 ; + cap->bit_width_fixed = cap->num_counters_fixed ? x86_pmu.cntval_bits :0 ; cap->events_mask = (unsigned int )x86_pmu.events_maskl; cap->events_mask_len = x86_pmu.events_mask_len; cap->pebs_ept = x86_pmu.pebs_ept;
Sean Christopherson
maintainerPATCH v6 11/44 perf/x86/core: Plumb mediated PMU capability from x86_pmu to x86_pmu_cap
In reply to Sean ChristophersonFrom: Mingwei Zhang <[email protected]>
Plumb mediated PMU capability to x86_pmu_cap in order to let any kernel entity such as KVM know that host PMU support mediated PMU mode and has the implementation.
--- arch/x86/events/core.c | 1 + arch/x86/include/asm/perf_event.h | 1 + 2 files changed, 2 insertions(+)
arch/x86/events/core.c +1 -0
@@ -3137,6 +3137,7 @@ void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap) cap->events_mask = (unsigned int )x86_pmu.events_maskl; cap->events_mask_len = x86_pmu.events_mask_len; cap->pebs_ept = x86_pmu.pebs_ept; + cap->mediated = !!(pmu.capabilities &PERF_PMU_CAP_MEDIATED_VPMU ); }EXPORT_SYMBOL_GPL (perf_get_x86_pmu_capability);
arch/x86/include/asm/perf_event.h +1 -0
@@ -296,6 +296,7 @@ struct x86_pmu_capability {unsigned int events_mask;int events_mask_len; unsigned int pebs_ept :1; + unsigned int mediated :1; };/*
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
[tip: perf/core] perf/x86/core: Plumb mediated PMU capability from x86_pmu to x86_pmu_cap
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: c8824a95d9673763a0a9d642f8c79b2162296923 Gitweb: https://git.kernel.org/tip/c8824a95d9673763a0a9d642f8c79b2162296923 Author: Mingwei Zhang <[email protected]> AuthorDate: Fri, 05 Dec 2025 16:16:47 -08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Dec 2025 13:31:06 +01:00
perf/x86/core: Plumb mediated PMU capability from x86_pmu to x86_pmu_cap
Plumb mediated PMU capability to x86_pmu_cap in order to let any kernel entity such as KVM know that host PMU support mediated PMU mode and has the implementation.
--- arch/x86/events/core.c | 1 + arch/x86/include/asm/perf_event.h | 1 + 2 files changed, 2 insertions(+)
arch/x86/events/core.c +1 -0
@@ -3110,6 +3110,7 @@ void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap) cap->events_mask = (unsigned int )x86_pmu.events_maskl; cap->events_mask_len = x86_pmu.events_mask_len; cap->pebs_ept = x86_pmu.pebs_ept; + cap->mediated = !!(pmu.capabilities &PERF_PMU_CAP_MEDIATED_VPMU ); }EXPORT_SYMBOL_FOR_KVM (perf_get_x86_pmu_capability);
arch/x86/include/asm/perf_event.h +1 -0
@@ -301,6 +301,7 @@ struct x86_pmu_capability {unsigned int events_mask;int events_mask_len; unsigned int pebs_ept :1; + unsigned int mediated :1; };/*
Sean Christopherson
maintainerPATCH v6 12/44 perf/x86/intel: Support PERF_PMU_CAP_MEDIATED_VPMU
In reply to Sean ChristophersonFrom: Kan Liang <[email protected]>
Apply the PERF_PMU_CAP_MEDIATED_VPMU for Intel core PMU. It only indicates that the perf side of core PMU is ready to support the mediated vPMU. Besides the capability, the hypervisor, a.k.a. KVM, still needs to check the PMU version and other PMU features/capabilities to decide whether to enable support mediated vPMUs.
--- arch/x86/events/intel/core.c | 5 +++++ 1 file changed, 5 insertions(+)
arch/x86/events/intel/core.c +5 -0
@@ -5319,6 +5319,8 @@ static void intel_pmu_check_hybrid_pmus(struct x86_hybrid_pmu *pmu)else pmu->intel_ctrl &= ~GLOBAL_CTRL_EN_PERF_METRICS ; + pmu->pmu.capabilities |=PERF_PMU_CAP_MEDIATED_VPMU ; + intel_pmu_check_event_constraints(pmu->event_constraints, pmu->cntr_mask64, pmu->fixed_cntr_mask64,
@@ -6936,6 +6938,9 @@ __init int intel_pmu_init(void)pr_cont (" AnyThread deprecated, " ); } +/* The perf side of core PMU is ready to support the mediated vPMU. */ +x86_get_pmu (smp_processor_id ())->capabilities |=PERF_PMU_CAP_MEDIATED_VPMU ; +/* * Many features on and after V6 require dynamic constraint, * e.g., Arch PEBS, ACR.
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
[tip: perf/core] perf/x86/intel: Support PERF_PMU_CAP_MEDIATED_VPMU
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 4280d79587a3fd4bf9415705536fe385467c5f44 Gitweb: https://git.kernel.org/tip/4280d79587a3fd4bf9415705536fe385467c5f44 Author: Kan Liang <[email protected]> AuthorDate: Fri, 05 Dec 2025 16:16:48 -08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Dec 2025 13:31:06 +01:00
perf/x86/intel: Support PERF_PMU_CAP_MEDIATED_VPMU
Apply the PERF_PMU_CAP_MEDIATED_VPMU for Intel core PMU. It only indicates that the perf side of core PMU is ready to support the mediated vPMU. Besides the capability, the hypervisor, a.k.a. KVM, still needs to check the PMU version and other PMU features/capabilities to decide whether to enable support mediated vPMUs.
--- arch/x86/events/intel/core.c | 5 +++++ 1 file changed, 5 insertions(+)
arch/x86/events/intel/core.c +5 -0
@@ -5695,6 +5695,8 @@ static void intel_pmu_check_hybrid_pmus(struct x86_hybrid_pmu *pmu)else pmu->intel_ctrl &= ~GLOBAL_CTRL_EN_PERF_METRICS ; + pmu->pmu.capabilities |=PERF_PMU_CAP_MEDIATED_VPMU ; +intel_pmu_check_event_constraints_all (&pmu->pmu);intel_pmu_check_extra_regs (pmu->extra_regs);
@@ -7314,6 +7316,9 @@ __init int intel_pmu_init(void)pr_cont (" AnyThread deprecated, " ); } +/* The perf side of core PMU is ready to support the mediated vPMU. */ +x86_get_pmu (smp_processor_id ())->capabilities |=PERF_PMU_CAP_MEDIATED_VPMU ; +/* * Many features on and after V6 require dynamic constraint, * e.g., Arch PEBS, ACR.
Sean Christopherson
maintainerPATCH v6 13/44 perf/x86/amd: Support PERF_PMU_CAP_MEDIATED_VPMU for AMD host
In reply to Sean ChristophersonFrom: Sandipan Das <[email protected]>
Apply the PERF_PMU_CAP_MEDIATED_VPMU flag for version 2 and later implementations of the core PMU. Aside from having Global Control and Status registers, virtualizing the PMU using the mediated model requires an interface to set or clear the overflow bits in the Global Status MSRs while restoring or saving the PMU context of a vCPU.
PerfMonV2-capable hardware has additional MSRs for this purpose, namely PerfCntrGlobalStatusSet and PerfCntrGlobalStatusClr, thereby making it suitable for use with mediated vPMU.
--- arch/x86/events/amd/core.c | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/events/amd/core.c +2 -0
@@ -1433,6 +1433,8 @@ static int __init amd_core_pmu_init(void) amd_pmu_global_cntr_mask = x86_pmu.cntr_mask64; +x86_get_pmu (smp_processor_id ())->capabilities |=PERF_PMU_CAP_MEDIATED_VPMU ; +/* Update PMC handling functions */ x86_pmu.enable_all = amd_pmu_v2_enable_all; x86_pmu.disable_all = amd_pmu_v2_disable_all;
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
[tip: perf/core] perf/x86/amd: Support PERF_PMU_CAP_MEDIATED_VPMU for AMD host
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 65eb3a9a8a34fa9188e0ab5e657d84ce4fa242a7 Gitweb: https://git.kernel.org/tip/65eb3a9a8a34fa9188e0ab5e657d84ce4fa242a7 Author: Sandipan Das <[email protected]> AuthorDate: Fri, 05 Dec 2025 16:16:49 -08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Dec 2025 13:31:07 +01:00
perf/x86/amd: Support PERF_PMU_CAP_MEDIATED_VPMU for AMD host
Apply the PERF_PMU_CAP_MEDIATED_VPMU flag for version 2 and later implementations of the core PMU. Aside from having Global Control and Status registers, virtualizing the PMU using the mediated model requires an interface to set or clear the overflow bits in the Global Status MSRs while restoring or saving the PMU context of a vCPU.
PerfMonV2-capable hardware has additional MSRs for this purpose, namely PerfCntrGlobalStatusSet and PerfCntrGlobalStatusClr, thereby making it suitable for use with mediated vPMU.
--- arch/x86/events/amd/core.c | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/events/amd/core.c +2 -0
@@ -1439,6 +1439,8 @@ static int __init amd_core_pmu_init(void) amd_pmu_global_cntr_mask = x86_pmu.cntr_mask64; +x86_get_pmu (smp_processor_id ())->capabilities |=PERF_PMU_CAP_MEDIATED_VPMU ; +/* Update PMC handling functions */ x86_pmu.enable_all = amd_pmu_v2_enable_all; x86_pmu.disable_all = amd_pmu_v2_disable_all;
Sean Christopherson
maintainerRe: [tip: perf/core] perf/x86/amd: Support PERF_PMU_CAP_MEDIATED_VPMU for AMD host
Quoted 8 lines · On Wed, Dec 17, 2025, tip-bot2 for Sandipan Das wrote:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 65eb3a9a8a34fa9188e0ab5e657d84ce4fa242a7 Gitweb: https://git.kernel.org/tip/65eb3a9a8a34fa9188e0ab5e657d84ce4fa242a7 Author: Sandipan Das <[email protected]> AuthorDate: Fri, 05 Dec 2025 16:16:49 -08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Dec 2025 13:31:07 +01:00
Can you provide a stable tag/branch for perf/core as of commit:
01122b89361e ("perf: Use EXPORT_SYMBOL_FOR_KVM() for the mediated APIs")
I'm finally getting around to applying stuff for 6.20.
Thanks!
Sean Christopherson
maintainerRe: [tip: perf/core] perf/x86/amd: Support PERF_PMU_CAP_MEDIATED_VPMU for AMD host
Quoted 10 lines · Sean Christopherson
Quoted 8 lines · On Wed, Dec 17, 2025, tip-bot2 for Sandipan Das wrote:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 65eb3a9a8a34fa9188e0ab5e657d84ce4fa242a7 Gitweb: https://git.kernel.org/tip/65eb3a9a8a34fa9188e0ab5e657d84ce4fa242a7 Author: Sandipan Das <[email protected]> AuthorDate: Fri, 05 Dec 2025 16:16:49 -08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Dec 2025 13:31:07 +01:00
Quoted 5 lines
Can you provide a stable tag/branch for perf/core as of commit:
01122b89361e("perf: UseEXPORT_SYMBOL_FOR_KVM()for the mediated APIs")I'm finally getting around to applying stuff for 6.20.
Ping. Or just pinky-swear that you won't change the hashes. :-)
Note, in case it matters for whatever reason, I've had the KVM side of things in linux-next since last week, based on the above commit.
Peter Zijlstra
maintainerRe: [tip: perf/core] perf/x86/amd: Support PERF_PMU_CAP_MEDIATED_VPMU for AMD host
In reply to Sean ChristophersonQuoted 10 lines · Sean Christopherson
Quoted 8 lines · On Wed, Dec 17, 2025, tip-bot2 for Sandipan Das wrote:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 65eb3a9a8a34fa9188e0ab5e657d84ce4fa242a7 Gitweb: https://git.kernel.org/tip/65eb3a9a8a34fa9188e0ab5e657d84ce4fa242a7 Author: Sandipan Das <[email protected]> AuthorDate: Fri, 05 Dec 2025 16:16:49 -08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Dec 2025 13:31:07 +01:00
Quoted 5 lines
Can you provide a stable tag/branch for perf/core as of commit:
01122b89361e("perf: UseEXPORT_SYMBOL_FOR_KVM()for the mediated APIs")I'm finally getting around to applying stuff for 6.20.
Oh, sorry, I missed this. Let me try and remember how to do tags.
Peter Zijlstra
maintainerRe: [tip: perf/core] perf/x86/amd: Support PERF_PMU_CAP_MEDIATED_VPMU for AMD host
Quoted 11 lines · Peter Zijlstra
Quoted 10 lines · Sean Christopherson
Quoted 8 lines · On Wed, Dec 17, 2025, tip-bot2 for Sandipan Das wrote:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 65eb3a9a8a34fa9188e0ab5e657d84ce4fa242a7 Gitweb: https://git.kernel.org/tip/65eb3a9a8a34fa9188e0ab5e657d84ce4fa242a7 Author: Sandipan Das <[email protected]> AuthorDate: Fri, 05 Dec 2025 16:16:49 -08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Dec 2025 13:31:07 +01:00
Quoted 7 lines
Quoted 5 lines
Can you provide a stable tag/branch for perf/core as of commit:
01122b89361e("perf: UseEXPORT_SYMBOL_FOR_KVM()for the mediated APIs")I'm finally getting around to applying stuff for 6.20.
Oh, sorry, I missed this. Let me try and remember how to do tags.
I *think* I managed to push a tag: perf-core-kvm-mediated-pmu to tip. It is however now signed, because for some reason I cannot get gpg-agent forwarding working today :-(
I hate computers.
Peter Zijlstra
maintainerRe: [tip: perf/core] perf/x86/amd: Support PERF_PMU_CAP_MEDIATED_VPMU for AMD host
Quoted 14 lines · Peter Zijlstra
Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Sean Christopherson
Quoted 8 lines · On Wed, Dec 17, 2025, tip-bot2 for Sandipan Das wrote:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 65eb3a9a8a34fa9188e0ab5e657d84ce4fa242a7 Gitweb: https://git.kernel.org/tip/65eb3a9a8a34fa9188e0ab5e657d84ce4fa242a7 Author: Sandipan Das <[email protected]> AuthorDate: Fri, 05 Dec 2025 16:16:49 -08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Dec 2025 13:31:07 +01:00Can you provide a stable tag/branch for perf/core as of commit:
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
01122b89361e("perf: UseEXPORT_SYMBOL_FOR_KVM()for the mediated APIs")I'm finally getting around to applying stuff for 6.20.
Oh, sorry, I missed this. Let me try and remember how to do tags.
I *think* I managed to push a tag: perf-core-kvm-mediated-pmu to tip. It is however now signed, because for some reason I cannot get
NOT signed, can't type neither apparently..
Quoted 3 lines
gpg-agent forwarding working today :-(
I hate computers.
Sean Christopherson
maintainerRe: [tip: perf/core] perf/x86/amd: Support PERF_PMU_CAP_MEDIATED_VPMU for AMD host
Quoted 17 lines · Peter Zijlstra
Quoted 16 lines · Peter Zijlstra
Quoted 15 lines · Peter Zijlstra
Quoted 14 lines · Sean Christopherson
Quoted 8 lines · On Wed, Dec 17, 2025, tip-bot2 for Sandipan Das wrote:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 65eb3a9a8a34fa9188e0ab5e657d84ce4fa242a7 Gitweb: https://git.kernel.org/tip/65eb3a9a8a34fa9188e0ab5e657d84ce4fa242a7 Author: Sandipan Das <[email protected]> AuthorDate: Fri, 05 Dec 2025 16:16:49 -08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Dec 2025 13:31:07 +01:00Can you provide a stable tag/branch for perf/core as of commit:
01122b89361e("perf: UseEXPORT_SYMBOL_FOR_KVM()for the mediated APIs")
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
I'm finally getting around to applying stuff for 6.20.
Oh, sorry, I missed this. Let me try and remember how to do tags.
I *think* I managed to push a tag: perf-core-kvm-mediated-pmu to tip. It is however now signed, because for some reason I cannot get
NOT signed, can't type neither apparently..
Unsigned is totally fine, I just wanted to make sure I didn't send a pull request with stale commits. Thanks much!
Quoted 1 line
Quoted 1 line
gpg-agent forwarding working today :-(
LOL, sounds like me trying to do encrypted email.
Quoted 1 line
Quoted 1 line
I hate computers.
Sean Christopherson
maintainerPATCH v6 14/44 KVM: Add a simplified wrapper for registering perf callbacks
In reply to Sean ChristophersonAdd a parameter-less API for registering perf callbacks in anticipation of introducing another x86-only parameter for handling mediated PMU PMIs.
No functional change intended.
--- arch/arm64/kvm/arm.c | 2 +- arch/loongarch/kvm/main.c | 2 +- arch/riscv/kvm/main.c | 2 +- arch/x86/kvm/x86.c | 2 +- include/linux/kvm_host.h | 11 +++++++++-- virt/kvm/kvm_main.c | 5 +++-- 6 files changed, 16 insertions(+), 8 deletions(-)
arch/arm64/kvm/arm.c +1 -1
@@ -2357,7 +2357,7 @@ static int __init init_subsystems(void)if (err) goto out; -kvm_register_perf_callbacks (NULL ); +kvm_register_perf_callbacks (); out:if (err)
arch/loongarch/kvm/main.c +1 -1
@@ -394,7 +394,7 @@ static int kvm_loongarch_env_init(void) }kvm_init_gcsr_flag (); -kvm_register_perf_callbacks (NULL ); +kvm_register_perf_callbacks ();/* Register LoongArch IPI interrupt controller interface. */ ret =kvm_loongarch_register_ipi_device ();
arch/riscv/kvm/main.c +1 -1
@@ -174,7 +174,7 @@ static int __init riscv_kvm_init(void)kvm_riscv_setup_vendor_features (); -kvm_register_perf_callbacks (NULL ); +kvm_register_perf_callbacks (); rc =kvm_init (sizeof (struct kvm_vcpu ),0 ,THIS_MODULE );if (rc) {
arch/x86/kvm/x86.c +1 -1
@@ -10107,7 +10107,7 @@ int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)set_hv_tscchange_cb (kvm_hyperv_tsc_notifier);#endif -kvm_register_perf_callbacks (ops->handle_intel_pt_intr); +__kvm_register_perf_callbacks (ops->handle_intel_pt_intr,NULL );if (IS_ENABLED (CONFIG_KVM_SW_PROTECTED_VM ) && tdp_mmu_enabled) kvm_caps.supported_vm_types |=BIT (KVM_X86_SW_PROTECTED_VM );
include/linux/kvm_host.h +9 -2
@@ -1749,10 +1749,17 @@ static inline bool kvm_arch_intc_initialized(struct kvm *kvm)#ifdef CONFIG_GUEST_PERF_EVENTS unsigned long kvm_arch_vcpu_get_ip (struct kvm_vcpu *vcpu); -void kvm_register_perf_callbacks (unsigned int (*pt_intr_handler)(void )); +void __kvm_register_perf_callbacks (unsigned int (*pt_intr_handler)(void ), +void (*mediated_pmi_handler)(void )); + +static inline void kvm_register_perf_callbacks (void ) +{ +__kvm_register_perf_callbacks (NULL ,NULL ); +} +void kvm_unregister_perf_callbacks (void );#else -static inline void kvm_register_perf_callbacks (void *ign) {} +static inline void kvm_register_perf_callbacks (void ) {}static inline void kvm_unregister_perf_callbacks (void ) {}#endif /* CONFIG_GUEST_PERF_EVENTS */
virt/kvm/kvm_main.c +3 -2
@@ -6474,10 +6474,11 @@ static struct perf_guest_info_callbacks kvm_guest_cbs = { .handle_mediated_pmi = NULL, }; -void kvm_register_perf_callbacks (unsigned int (*pt_intr_handler)(void )) +void __kvm_register_perf_callbacks (unsigned int (*pt_intr_handler)(void ), +void (*mediated_pmi_handler)(void )) { kvm_guest_cbs.handle_intel_pt_intr = pt_intr_handler; - kvm_guest_cbs.handle_mediated_pmi =NULL ; + kvm_guest_cbs.handle_mediated_pmi = mediated_pmi_handler;perf_register_guest_info_callbacks (&kvm_guest_cbs); }
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH v6 15/44 KVM: x86/pmu: Snapshot host (i.e. perf's) reported PMU capabilities
In reply to Sean ChristophersonTake a snapshot of the unadulterated PMU capabilities provided by perf so that KVM can compare guest vPMU capabilities against hardware capabilities when determining whether or not to intercept PMU MSRs (and RDPMC).
--- arch/x86/kvm/pmu.c | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/kvm/pmu.c +2 -0
@@ -108,6 +108,8 @@ void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops)bool is_intel = boot_cpu_data.x86_vendor ==X86_VENDOR_INTEL ;int min_nr_gp_ctrs = pmu_ops->MIN_NR_GP_COUNTERS; +perf_get_x86_pmu_capability (&kvm_host_pmu); +/* * Hybrid PMUs don't play nice with virtualization without careful * configuration by userspace, and KVM's APIs for reporting supported
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Re: [PATCH v6 15/44] KVM: x86/pmu: Snapshot host (i.e. perf's) reported PMU capabilities
Quoted 16 lines · Sean Christopherson
Take a snapshot of the unadulterated PMU capabilities provided by perf so that KVM can compare guest vPMU capabilities against hardware capabilities when determining whether or not to intercept PMU MSRs (and RDPMC).
Reviewed-by Sandipan Das <[email protected]>Tested-by Xudong Hao <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/pmu.c | 2 ++ 1 file changed, 2 insertions(+)arch/x86/kvm/pmu.c +0
-0
arch/x86/kvm/pmu.c void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops) · line 115
Quoted 8 lines
arch/x86/kvm/pmu.c +2
-0@@ -108,6 +108,8 @@ void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops)bool is_intel = boot_cpu_data.x86_vendor ==X86_VENDOR_INTEL ;int min_nr_gp_ctrs = pmu_ops->MIN_NR_GP_COUNTERS; +perf_get_x86_pmu_capability (&kvm_host_pmu); +/* * Hybrid PMUs don't play nice with virtualization without careful * configuration by userspace, and KVM's APIs for reporting supported
Hi Sean,
It looks a merging error here. We don't need this patch.
The original patch "51f34b1 ("KVM: x86/pmu: Snapshot host (i.e. perf's) reported PMU capabilities")" had been merged into upstream and subsequently we submitted a new patch "034417c1439a ("KVM: x86/pmu: Don't try to get perf capabilities for hybrid CPUs")" to fix the warning introduced from previous patch "51f34b1 ("KVM: x86/pmu: Snapshot host (i.e. perf's) reported PMU capabilities")". Thanks.
-Dapeng Mi
Sean Christopherson
maintainerRe: [PATCH v6 15/44] KVM: x86/pmu: Snapshot host (i.e. perf's) reported PMU capabilities
Quoted 29 lines · Dapeng Mi
Quoted 16 lines · Sean Christopherson
Take a snapshot of the unadulterated PMU capabilities provided by perf so that KVM can compare guest vPMU capabilities against hardware capabilities when determining whether or not to intercept PMU MSRs (and RDPMC).
Reviewed-by Sandipan Das <[email protected]>Tested-by Xudong Hao <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/pmu.c | 2 ++ 1 file changed, 2 insertions(+)arch/x86/kvm/pmu.c +0
-0arch/x86/kvm/pmu.c void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops) · line 115
Quoted 8 lines
arch/x86/kvm/pmu.c +2
-0@@ -108,6 +108,8 @@ void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops)bool is_intel = boot_cpu_data.x86_vendor ==X86_VENDOR_INTEL ;int min_nr_gp_ctrs = pmu_ops->MIN_NR_GP_COUNTERS; +perf_get_x86_pmu_capability (&kvm_host_pmu); +/* * Hybrid PMUs don't play nice with virtualization without careful * configuration by userspace, and KVM's APIs for reporting supported Hi Sean,
Quoted 1 line
It looks a merging error here. We don't need this patch.
Gah, right you are. I overlooked it because it didn't conflict on a rebase, and I once again forgot to test on a hybrid PMU, grr.
Thanks!
Quoted 10 lines
The original patch "51f34b1 ("KVM: x86/pmu: Snapshot host (i.e. perf's) reported PMU capabilities")" had been merged into upstream and subsequently we submitted a new patch "
034417c1439a("KVM: x86/pmu: Don't try to get perf capabilities for hybrid CPUs")" to fix the warning introduced from previous patch "51f34b1 ("KVM: x86/pmu: Snapshot host (i.e. perf's) reported PMU capabilities")". Thanks.-Dapeng Mi
Sean Christopherson
maintainerPATCH v6 16/44 KVM: x86/pmu: Start stubbing in mediated PMU support
In reply to Sean ChristophersonFrom: Dapeng Mi <[email protected]>
Introduce enable_mediated_pmu as a global variable, with the intent of exposing it to userspace a vendor module parameter, to control and reflect mediated vPMU support. Wire up the perf plumbing to create+release a mediated PMU, but defer exposing the parameter to userspace until KVM support for a mediated PMUs is fully landed.
To (a) minimize compatibility issues, (b) to give userspace a chance to opt out of the restrictive side-effects of perf_create_mediated_pmu(), and (c) to avoid adding new dependencies between enabling an in-kernel irqchip and a mediated vPMU, defer "creating" a mediated PMU in perf until the first vCPU is created.
Regarding userspace compatibility, an alternative solution would be to make the mediated PMU fully opt-in, e.g. to avoid unexpected failure due to perf_create_mediated_pmu() failing. Ironically, that approach creates an even bigger compatibility issue, as turning on enable_mediated_pmu would silently break VMMs that don't utilize KVM_CAP_PMU_CAPABILITY (well, silently until the guest tried to access PMU assets).
Regarding an in-kernel irqchip, create a mediated PMU if and only if the VM has an in-kernel local APIC, as the mediated PMU will take a hard dependency on forwarding PMIs to the guest without bouncing through host userspace. Silently "drop" the PMU instead of rejecting KVM_CREATE_VCPU, as KVM's existing vPMU support doesn't function correctly if the local APIC is emulated by userspace, e.g. PMIs will never be delivered. I.e. it's far, far more likely that rejecting KVM_CREATE_VCPU would cause problems, e.g. for tests or userspace daemons that just want to probe basic KVM functionality.
Note! Deliberately make mediated PMU creation "sticky", i.e. don't unwind it on failure to create a vCPU. Practically speaking, there's no harm to having a VM with a mediated PMU and no vCPUs. To avoid an "impossible" VM setup, reject KVM_CAP_PMU_CAPABILITY if a mediated PMU has been created, i.e. don't let userspace disable PMU support after failed vCPU creation (with PMU support enabled).
Defer vendor specific requirements and constraints to the future.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/pmu.c | 4 ++++ arch/x86/kvm/pmu.h | 7 +++++++ arch/x86/kvm/x86.c | 37 +++++++++++++++++++++++++++++++-- arch/x86/kvm/x86.h | 1 + 5 files changed, 48 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1484,6 +1484,7 @@ struct kvm_arch {bool bus_lock_detection_enabled;bool enable_pmu; +bool created_mediated_pmu;u32 notify_window;u32 notify_vmexit_flags;
arch/x86/kvm/pmu.c +4 -0
@@ -137,6 +137,10 @@ void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops) enable_pmu = false; } +if (!enable_pmu || !enable_mediated_pmu || !kvm_host_pmu.mediated || + !pmu_ops->is_mediated_pmu_supported (&kvm_host_pmu)) + enable_mediated_pmu = false; +if (!enable_pmu) {memset (&kvm_pmu_cap,0 ,sizeof (kvm_pmu_cap));return ;
arch/x86/kvm/pmu.h +7 -0
@@ -37,6 +37,8 @@ struct kvm_pmu_ops {void (*deliver_pmi)(struct kvm_vcpu *vcpu);void (*cleanup)(struct kvm_vcpu *vcpu); +bool (*is_mediated_pmu_supported)(struct x86_pmu_capability *host_pmu); +const u64 EVENTSEL_EVENT ;const int MAX_NR_GP_COUNTERS ;const int MIN_NR_GP_COUNTERS ;
@@ -58,6 +60,11 @@ static inline bool kvm_pmu_has_perf_global_ctrl(struct kvm_pmu *pmu)return pmu->version >1 ; } +static inline bool kvm_vcpu_has_mediated_pmu (struct kvm_vcpu *vcpu) +{ +return enable_mediated_pmu &&vcpu_to_pmu (vcpu)->version; +} +/* * KVM tracks all counters in 64-bit bitmaps, with general purpose counters * mapped to bits 31:0 and fixed counters mapped to 63:32, e.g. fixed counter 0
arch/x86/kvm/x86.c +35 -2
@@ -183,6 +183,10 @@ bool __read_mostly enable_pmu = true;EXPORT_SYMBOL_FOR_KVM_INTERNAL (enable_pmu);module_param (enable_pmu, bool,0444 ); +/* Enable/disabled mediated PMU virtualization. */ +bool __read_mostly enable_mediated_pmu; +EXPORT_SYMBOL_FOR_KVM_INTERNAL (enable_mediated_pmu); +bool __read_mostly eager_page_split = true;module_param (eager_page_split, bool,0644 );
@@ -6854,7 +6858,7 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,break ;mutex_lock (&kvm->lock); -if (!kvm->created_vcpus) { +if (!kvm->created_vcpus && !kvm->arch.created_mediated_pmu) { kvm->arch.enable_pmu = !(cap->args[0 ] &KVM_PMU_CAP_DISABLE ); r =0 ; }
@@ -12641,8 +12645,13 @@ static int sync_regs(struct kvm_vcpu *vcpu)return 0 ; } +#define PERF_MEDIATED_PMU_MSG \ + "Failed to enable mediated vPMU, try disabling system wide perf events and nmi_watchdog.\n" +int kvm_arch_vcpu_precreate (struct kvm *kvm,unsigned int id) { +int r; +if (kvm_check_tsc_unstable () && kvm->created_vcpus)pr_warn_once ("SMP vm created on host with unstable TSC; " "guest TSC will not be reliable\n" );
@@ -12653,7 +12662,29 @@ int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)if (id >= kvm->arch.max_vcpu_ids)return -EINVAL ; -return kvm_x86_call (vcpu_precreate)(kvm); +/* +* Note, any actions done by .vcpu_create() must be idempotent with +* respect to creating multiple vCPUs, and therefore are not undone if +* creating a vCPU fails (including failure during pre-create). +*/ + r =kvm_x86_call (vcpu_precreate)(kvm); +if (r) +return r; + +if (enable_mediated_pmu && kvm->arch.enable_pmu && + !kvm->arch.created_mediated_pmu) { +if (irqchip_in_kernel (kvm)) { + r =perf_create_mediated_pmu (); +if (r) { +pr_warn_ratelimited (PERF_MEDIATED_PMU_MSG ); +return r; + } + kvm->arch.created_mediated_pmu = true; + }else { + kvm->arch.enable_pmu = false; + } + } +return 0 ; }int kvm_arch_vcpu_create (struct kvm_vcpu *vcpu)
@@ -13319,6 +13350,8 @@ void kvm_arch_destroy_vm(struct kvm *kvm)__x86_set_memory_region (kvm,TSS_PRIVATE_MEMSLOT ,0 ,0 );mutex_unlock (&kvm->slots_lock); } +if (kvm->arch.created_mediated_pmu) +perf_release_mediated_pmu ();kvm_destroy_vcpus (kvm);kvm_free_msr_filter (srcu_dereference_check (kvm->arch.msr_filter, &kvm->srcu,1 ));#ifdef CONFIG_KVM_IOAPIC
arch/x86/kvm/x86.h +1 -0
@@ -470,6 +470,7 @@ extern struct kvm_caps kvm_caps;extern struct kvm_host_values kvm_host;extern bool enable_pmu; +extern bool enable_mediated_pmu;/* * Get a filtered version of KVM's supported XCR0 that strips out dynamic
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH v6 17/44 KVM: x86/pmu: Implement Intel mediated PMU requirements and constraints
In reply to Sean ChristophersonFrom: Dapeng Mi <[email protected]>
Implement Intel PMU requirements and constraints for mediated PMU support. Require host PMU version 4+ so that PERF_GLOBAL_STATUS_SET can be used to precisely load the guest's status value into hardware, and require full- width writes so that KVM can precisely load guest counter values.
Disable PEBS and LBRs if mediated PMU support is enabled, as they won't be supported in the initial implementation.
--- arch/x86/kvm/vmx/capabilities.h | 3 ++- arch/x86/kvm/vmx/pmu_intel.c | 17 +++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 3 ++- 3 files changed, 21 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/capabilities.h +2 -1
@@ -395,7 +395,8 @@ static inline bool vmx_pt_mode_is_host_guest(void)static inline bool vmx_pebs_supported (void ) { -return boot_cpu_has (X86_FEATURE_PEBS ) && kvm_pmu_cap.pebs_ept; +return boot_cpu_has (X86_FEATURE_PEBS ) && kvm_pmu_cap.pebs_ept && + !enable_mediated_pmu; }static inline bool cpu_has_notify_vmexit (void )
arch/x86/kvm/vmx/pmu_intel.c +17 -0
@@ -767,6 +767,20 @@ void intel_pmu_cross_mapped_check(struct kvm_pmu *pmu) } } +static bool intel_pmu_is_mediated_pmu_supported (struct x86_pmu_capability *host_pmu) +{ +u64 host_perf_cap =0 ; + +if (boot_cpu_has (X86_FEATURE_PDCM )) +rdmsrq (MSR_IA32_PERF_CAPABILITIES , host_perf_cap); + +/* +* Require v4+ for MSR_CORE_PERF_GLOBAL_STATUS_SET, and full-width +* writes so that KVM can precisely load guest counter values. +*/ +return host_pmu->version >=4 && host_perf_cap &PERF_CAP_FW_WRITES ; +} +struct kvm_pmu_ops intel_pmu_ops__initdata = { .rdpmc_ecx_to_pmc = intel_rdpmc_ecx_to_pmc, .msr_idx_to_pmc = intel_msr_idx_to_pmc,
@@ -778,6 +792,9 @@ struct kvm_pmu_ops intel_pmu_ops __initdata = { .reset = intel_pmu_reset, .deliver_pmi = intel_pmu_deliver_pmi, .cleanup = intel_pmu_cleanup, + + .is_mediated_pmu_supported = intel_pmu_is_mediated_pmu_supported, + .EVENTSEL_EVENT = ARCH_PERFMON_EVENTSEL_EVENT, .MAX_NR_GP_COUNTERS = KVM_MAX_NR_INTEL_GP_COUNTERS, .MIN_NR_GP_COUNTERS = 1,
arch/x86/kvm/vmx/vmx.c +2 -1
@@ -7958,7 +7958,8 @@ static __init u64 vmx_get_perf_capabilities(void)if (boot_cpu_has (X86_FEATURE_PDCM ))rdmsrq (MSR_IA32_PERF_CAPABILITIES , host_perf_cap); -if (!cpu_feature_enabled (X86_FEATURE_ARCH_LBR )) { +if (!cpu_feature_enabled (X86_FEATURE_ARCH_LBR ) && + !enable_mediated_pmu) {x86_perf_get_lbr (&vmx_lbr_caps);/*
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH v6 18/44 KVM: x86/pmu: Implement AMD mediated PMU requirements
In reply to Sean ChristophersonRequire host PMU version 2+ for AMD mediated PMU support, as PERF_GLOBAL_CTRL and friends are hard requirements for the mediated PMU.
--- arch/x86/kvm/svm/pmu.c | 8 ++++++++ 1 file changed, 8 insertions(+)
arch/x86/kvm/svm/pmu.c +8 -0
@@ -227,6 +227,11 @@ static void amd_pmu_init(struct kvm_vcpu *vcpu) } } +static bool amd_pmu_is_mediated_pmu_supported (struct x86_pmu_capability *host_pmu) +{ +return host_pmu->version >=2 ; +} +struct kvm_pmu_ops amd_pmu_ops__initdata = { .rdpmc_ecx_to_pmc = amd_rdpmc_ecx_to_pmc, .msr_idx_to_pmc = amd_msr_idx_to_pmc,
@@ -236,6 +241,9 @@ struct kvm_pmu_ops amd_pmu_ops __initdata = { .set_msr = amd_pmu_set_msr, .refresh = amd_pmu_refresh, .init = amd_pmu_init, + + .is_mediated_pmu_supported = amd_pmu_is_mediated_pmu_supported, + .EVENTSEL_EVENT = AMD64_EVENTSEL_EVENT, .MAX_NR_GP_COUNTERS = KVM_MAX_NR_AMD_GP_COUNTERS, .MIN_NR_GP_COUNTERS = AMD64_NUM_COUNTERS,
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH v6 19/44 KVM: x86/pmu: Register PMI handler for mediated vPMU
In reply to Sean ChristophersonFrom: Xiong Zhang <[email protected]>
Register a dedicated PMI handler with perf's callback when mediated PMU support is enabled. Perf routes PMIs that arrive while guest context is loaded to the provided callback, by modifying the CPU's LVTPC to point at a dedicated mediated PMI IRQ vector.
WARN upon receipt of a mediated PMI if there is no active vCPU, or if the vCPU doesn't have a mediated PMU. Even if a PMI manages to skid past VM-Exit, it should never be delayed all the way beyond unloading the vCPU. And while running vCPUs without a mediated PMU, the LVTPC should never be wired up to the mediated PMI IRQ vector, i.e. should always be routed through perf's NMI handler.
--- arch/x86/kvm/pmu.c | 10 ++++++++++ arch/x86/kvm/pmu.h | 2 ++ arch/x86/kvm/x86.c | 3 ++- 3 files changed, 14 insertions(+), 1 deletion(-)
arch/x86/kvm/pmu.c +10 -0
@@ -159,6 +159,16 @@ void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops)perf_get_hw_event_config (PERF_COUNT_HW_BRANCH_INSTRUCTIONS ); } +void kvm_handle_guest_mediated_pmi (void ) +{ +struct kvm_vcpu *vcpu =kvm_get_running_vcpu (); + +if (WARN_ON_ONCE (!vcpu || !kvm_vcpu_has_mediated_pmu (vcpu))) +return ; + +kvm_make_request (KVM_REQ_PMI , vcpu); +} +static inline void __kvm_perf_overflow (struct kvm_pmc *pmc,bool in_pmi) {struct kvm_pmu *pmu =pmc_to_pmu (pmc);
arch/x86/kvm/pmu.h +2 -0
@@ -46,6 +46,8 @@ struct kvm_pmu_ops {void kvm_pmu_ops_update (const struct kvm_pmu_ops *pmu_ops); +void kvm_handle_guest_mediated_pmi (void ); +static inline bool kvm_pmu_has_perf_global_ctrl (struct kvm_pmu *pmu) {/*
arch/x86/kvm/x86.c +2 -1
@@ -10111,7 +10111,8 @@ int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)set_hv_tscchange_cb (kvm_hyperv_tsc_notifier);#endif -__kvm_register_perf_callbacks (ops->handle_intel_pt_intr,NULL ); +__kvm_register_perf_callbacks (ops->handle_intel_pt_intr, + enable_mediated_pmu ? kvm_handle_guest_mediated_pmi :NULL );if (IS_ENABLED (CONFIG_KVM_SW_PROTECTED_VM ) && tdp_mmu_enabled) kvm_caps.supported_vm_types |=BIT (KVM_X86_SW_PROTECTED_VM );
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH v6 20/44 KVM: x86/pmu: Disable RDPMC interception for compatible mediated vPMU
In reply to Sean ChristophersonFrom: Dapeng Mi <[email protected]>
Disable RDPMC interception for vCPUs with a mediated vPMU that is compatible with the host PMU, i.e. that doesn't require KVM emulation of RDPMC to honor the guest's vCPU model. With a mediated vPMU, all guest state accessible via RDPMC is loaded into hardware while the guest is running.
Adust RDPMC interception only for non-TDX guests, as the TDX module is responsible for managing RDPMC intercepts based on the TD configuration.
--- arch/x86/kvm/pmu.c | 26 ++++++++++++++++++++++++++ arch/x86/kvm/pmu.h | 1 + arch/x86/kvm/svm/svm.c | 5 +++++ arch/x86/kvm/vmx/vmx.c | 7 +++++++ arch/x86/kvm/x86.c | 1 + 5 files changed, 40 insertions(+)
arch/x86/kvm/pmu.c +26 -0
@@ -716,6 +716,32 @@ int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)return 0 ; } +bool kvm_need_rdpmc_intercept (struct kvm_vcpu *vcpu) +{ +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); + +if (!kvm_vcpu_has_mediated_pmu (vcpu)) +return true; + +/* +* VMware allows access to these Pseduo-PMCs even when read via RDPMC +* in Ring3 when CR4.PCE=0. +*/ +if (enable_vmware_backdoor) +return true; + +/* +* Note! Check *host* PMU capabilities, not KVM's PMU capabilities, as +* KVM's capabilities are constrained based on KVM support, i.e. KVM's +* capabilities themselves may be a subset of hardware capabilities. +*/ +return pmu->nr_arch_gp_counters != kvm_host_pmu.num_counters_gp || + pmu->nr_arch_fixed_counters != kvm_host_pmu.num_counters_fixed || + pmu->counter_bitmask[KVM_PMC_GP ] != (BIT_ULL (kvm_host_pmu.bit_width_gp) -1 ) || + pmu->counter_bitmask[KVM_PMC_FIXED ] != (BIT_ULL (kvm_host_pmu.bit_width_fixed) -1 ); +} +EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_need_rdpmc_intercept); +void kvm_pmu_deliver_pmi (struct kvm_vcpu *vcpu) {if (lapic_in_kernel (vcpu)) {
arch/x86/kvm/pmu.h +1 -0
@@ -238,6 +238,7 @@ void kvm_pmu_instruction_retired(struct kvm_vcpu *vcpu);void kvm_pmu_branch_retired (struct kvm_vcpu *vcpu);bool is_vmware_backdoor_pmc (u32 pmc_idx); +bool kvm_need_rdpmc_intercept (struct kvm_vcpu *vcpu);extern struct kvm_pmu_ops intel_pmu_ops;extern struct kvm_pmu_ops amd_pmu_ops;
arch/x86/kvm/svm/svm.c +5 -0
@@ -1011,6 +1011,11 @@ static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu) svm->vmcb->control.virt_ext |=VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK ; } } + +if (kvm_need_rdpmc_intercept (vcpu)) +svm_set_intercept (svm,INTERCEPT_RDPMC ); +else +svm_clr_intercept (svm,INTERCEPT_RDPMC ); }static void svm_recalc_intercepts (struct kvm_vcpu *vcpu)
arch/x86/kvm/vmx/vmx.c +7 -0
@@ -4300,8 +4300,15 @@ static void vmx_recalc_msr_intercepts(struct kvm_vcpu *vcpu)*/ } +static void vmx_recalc_instruction_intercepts (struct kvm_vcpu *vcpu) +{ +exec_controls_changebit (to_vmx (vcpu),CPU_BASED_RDPMC_EXITING , +kvm_need_rdpmc_intercept (vcpu)); +} +void vmx_recalc_intercepts (struct kvm_vcpu *vcpu) { +vmx_recalc_instruction_intercepts (vcpu);vmx_recalc_msr_intercepts (vcpu); }
arch/x86/kvm/x86.c +1 -0
@@ -3945,6 +3945,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) vcpu->arch.perf_capabilities = data;kvm_pmu_refresh (vcpu); +kvm_make_request (KVM_REQ_RECALC_INTERCEPTS , vcpu);break ;case MSR_IA32_PRED_CMD : {u64 reserved_bits = ~(PRED_CMD_IBPB |PRED_CMD_SBPB );
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH v6 21/44 KVM: x86/pmu: Load/save GLOBAL_CTRL via entry/exit fields for mediated PMU
In reply to Sean ChristophersonFrom: Dapeng Mi <[email protected]>
When running a guest with a mediated PMU, context switch PERF_GLOBAL_CTRL via the dedicated VMCS fields for both host and guest. For the host, always zero GLOBAL_CTRL on exit as the guest's state will still be loaded in hardware (KVM will context switch the bulk of PMU state outside of the inner run loop). For the guest, use the dedicated fields to atomically load and save PERF_GLOBAL_CTRL on all entry/exits.
For now, require VM_EXIT_SAVE_IA32_PERF_GLOBAL_CTRL support (introduced by Sapphire Rapids). KVM can support such CPUs by saving PERF_GLOBAL_CTRL via the MSR save list, a.k.a. the MSR auto-store list, but defer that support as it adds a small amount of complexity and is somewhat unique.
To minimize VM-Entry latency, propagate IA32_PERF_GLOBAL_CTRL to the VMCS on-demand. But to minimize complexity, read IA32_PERF_GLOBAL_CTRL out of the VMCS on all non-failing VM-Exits. I.e. partially cache the MSR. KVM could track GLOBAL_CTRL as an EXREG and defer all reads, but writes are rare, i.e. the dirty tracking for an EXREG is unnecessary, and it's not obvious that shaving ~15-20 cycles per exit is meaningful given the total overhead associated with mediated PMU context switches.
--- arch/x86/include/asm/kvm-x86-pmu-ops.h | 2 ++ arch/x86/include/asm/vmx.h | 1 + arch/x86/kvm/pmu.c | 13 +++++++++-- arch/x86/kvm/pmu.h | 3 ++- arch/x86/kvm/vmx/capabilities.h | 6 +++++ arch/x86/kvm/vmx/pmu_intel.c | 25 ++++++++++++++++++++- arch/x86/kvm/vmx/vmx.c | 31 +++++++++++++++++++++++++- arch/x86/kvm/vmx/vmx.h | 3 ++- 8 files changed, 78 insertions(+), 6 deletions(-)
arch/x86/include/asm/kvm-x86-pmu-ops.h +2 -0
@@ -23,5 +23,7 @@ KVM_X86_PMU_OP_OPTIONAL(reset) KVM_X86_PMU_OP_OPTIONAL(deliver_pmi) KVM_X86_PMU_OP_OPTIONAL(cleanup) +KVM_X86_PMU_OP_OPTIONAL(write_global_ctrl) +#undef KVM_X86_PMU_OP#undef KVM_X86_PMU_OP_OPTIONAL
arch/x86/include/asm/vmx.h +1 -0
@@ -107,6 +107,7 @@#define VM_EXIT_PT_CONCEAL_PIP 0x01000000#define VM_EXIT_CLEAR_IA32_RTIT_CTL 0x02000000#define VM_EXIT_LOAD_CET_STATE 0x10000000 +#define VM_EXIT_SAVE_IA32_PERF_GLOBAL_CTRL 0x40000000#define VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR 0x00036dff
arch/x86/kvm/pmu.c +11 -2
@@ -103,7 +103,7 @@ void kvm_pmu_ops_update(const struct kvm_pmu_ops *pmu_ops)#undef __KVM_X86_PMU_OP } -void kvm_init_pmu_capability (const struct kvm_pmu_ops *pmu_ops) +void kvm_init_pmu_capability (struct kvm_pmu_ops *pmu_ops) {bool is_intel = boot_cpu_data.x86_vendor ==X86_VENDOR_INTEL ;int min_nr_gp_ctrs = pmu_ops->MIN_NR_GP_COUNTERS;
@@ -141,6 +141,9 @@ void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops) !pmu_ops->is_mediated_pmu_supported(&kvm_host_pmu)) enable_mediated_pmu = false; +if (!enable_mediated_pmu) + pmu_ops->write_global_ctrl =NULL ; +if (!enable_pmu) {memset (&kvm_pmu_cap,0 ,sizeof (kvm_pmu_cap));return ;
@@ -836,6 +839,9 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) diff = pmu->global_ctrl ^ data; pmu->global_ctrl = data;reprogram_counters (pmu, diff); + +if (kvm_vcpu_has_mediated_pmu (vcpu)) +kvm_pmu_call (write_global_ctrl)(data); }break ;case MSR_CORE_PERF_GLOBAL_OVF_CTRL :
@@ -930,8 +936,11 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu)* in the global controls). Emulate that behavior when refreshing the * PMU so that userspace doesn't need to manually set PERF_GLOBAL_CTRL. */ -if (kvm_pmu_has_perf_global_ctrl (pmu) && pmu->nr_arch_gp_counters) +if (kvm_pmu_has_perf_global_ctrl (pmu) && pmu->nr_arch_gp_counters) { pmu->global_ctrl =GENMASK_ULL (pmu->nr_arch_gp_counters -1 ,0 ); +if (kvm_vcpu_has_mediated_pmu (vcpu)) +kvm_pmu_call (write_global_ctrl)(pmu->global_ctrl); + }bitmap_set (pmu->all_valid_pmc_idx,0 , pmu->nr_arch_gp_counters); bitmap_set(pmu->all_valid_pmc_idx, KVM_FIXED_PMC_BASE_IDX,
arch/x86/kvm/pmu.h +2 -1
@@ -38,6 +38,7 @@ struct kvm_pmu_ops {void (*cleanup)(struct kvm_vcpu *vcpu);bool (*is_mediated_pmu_supported)(struct x86_pmu_capability *host_pmu); +void (*write_global_ctrl)(u64 global_ctrl);const u64 EVENTSEL_EVENT ;const int MAX_NR_GP_COUNTERS ;
@@ -183,7 +184,7 @@ static inline bool pmc_is_locally_enabled(struct kvm_pmc *pmc)extern struct x86_pmu_capability kvm_pmu_cap; -void kvm_init_pmu_capability (const struct kvm_pmu_ops *pmu_ops); +void kvm_init_pmu_capability (struct kvm_pmu_ops *pmu_ops);void kvm_pmu_recalc_pmc_emulation (struct kvm_pmu *pmu,struct kvm_pmc *pmc);
arch/x86/kvm/vmx/capabilities.h +6 -0
@@ -109,6 +109,12 @@ static inline bool cpu_has_load_cet_ctrl(void) {return (vmcs_config.vmentry_ctrl &VM_ENTRY_LOAD_CET_STATE ); } + +static inline bool cpu_has_save_perf_global_ctrl (void ) +{ +return vmcs_config.vmexit_ctrl &VM_EXIT_SAVE_IA32_PERF_GLOBAL_CTRL ; +} +static inline bool cpu_has_vmx_mpx (void ) {return vmcs_config.vmentry_ctrl &VM_ENTRY_LOAD_BNDCFGS ;
arch/x86/kvm/vmx/pmu_intel.c +24 -1
@@ -778,7 +778,29 @@ static bool intel_pmu_is_mediated_pmu_supported(struct x86_pmu_capability *host_* Require v4+ for MSR_CORE_PERF_GLOBAL_STATUS_SET, and full-width * writes so that KVM can precisely load guest counter values. */ -return host_pmu->version >=4 && host_perf_cap &PERF_CAP_FW_WRITES ; +if (host_pmu->version <4 || !(host_perf_cap &PERF_CAP_FW_WRITES )) +return false; + +/* +* All CPUs that support a mediated PMU are expected to support loading +* PERF_GLOBAL_CTRL via dedicated VMCS fields. +*/ +if (WARN_ON_ONCE (!cpu_has_load_perf_global_ctrl ())) +return false; + +/* +* KVM doesn't yet support mediated PMU on CPUs without support for +* saving PERF_GLOBAL_CTRL via a dedicated VMCS field. +*/ +if (!cpu_has_save_perf_global_ctrl ()) +return false; + +return true; +} + +static void intel_pmu_write_global_ctrl (u64 global_ctrl) +{ +vmcs_write64 (GUEST_IA32_PERF_GLOBAL_CTRL , global_ctrl); }struct kvm_pmu_ops intel_pmu_ops__initdata = {
@@ -794,6 +816,7 @@ struct kvm_pmu_ops intel_pmu_ops __initdata = { .cleanup = intel_pmu_cleanup, .is_mediated_pmu_supported = intel_pmu_is_mediated_pmu_supported, + .write_global_ctrl = intel_pmu_write_global_ctrl, .EVENTSEL_EVENT = ARCH_PERFMON_EVENTSEL_EVENT, .MAX_NR_GP_COUNTERS = KVM_MAX_NR_INTEL_GP_COUNTERS,
arch/x86/kvm/vmx/vmx.c +30 -1
@@ -4294,6 +4294,18 @@ static void vmx_recalc_msr_intercepts(struct kvm_vcpu *vcpu)vmx_set_intercept_for_msr (vcpu,MSR_IA32_S_CET ,MSR_TYPE_RW , intercept); } +if (enable_mediated_pmu) { +bool is_mediated_pmu =kvm_vcpu_has_mediated_pmu (vcpu); +struct vcpu_vmx *vmx =to_vmx (vcpu); + +vm_entry_controls_changebit (vmx, +VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL , is_mediated_pmu); + +vm_exit_controls_changebit (vmx, +VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL | +VM_EXIT_SAVE_IA32_PERF_GLOBAL_CTRL , is_mediated_pmu); + } +/* * x2APIC and LBR MSR intercepts are modified on-demand and cannot be * filtered by userspace.
@@ -4476,6 +4488,16 @@ void vmx_set_constant_host_state(struct vcpu_vmx *vmx)vmcs_writel (HOST_SSP ,0 );vmcs_writel (HOST_INTR_SSP_TABLE ,0 ); } + +/* +* When running a guest with a mediated PMU, guest state is resident in +* hardware after VM-Exit. Zero PERF_GLOBAL_CTRL on exit so that host +* activity doesn't bleed into the guest counters. When running with +* an emulated PMU, PERF_GLOBAL_CTRL is dynamically computed on every +* entry/exit to merge guest and host PMU usage. +*/ +if (enable_mediated_pmu) +vmcs_write64 (HOST_IA32_PERF_GLOBAL_CTRL ,0 ); }void set_cr4_guest_host_mask (struct vcpu_vmx *vmx)
@@ -4543,7 +4565,8 @@ static u32 vmx_get_initial_vmexit_ctrl(void) VM_EXIT_CLEAR_IA32_RTIT_CTL);/* Loading of EFER and PERF_GLOBAL_CTRL are toggled dynamically */ return vmexit_ctrl & - ~(VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL |VM_EXIT_LOAD_IA32_EFER ); + ~(VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL |VM_EXIT_LOAD_IA32_EFER | +VM_EXIT_SAVE_IA32_PERF_GLOBAL_CTRL ); }void vmx_refresh_apicv_exec_ctrl (struct kvm_vcpu *vcpu)
@@ -7270,6 +7293,9 @@ static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)struct perf_guest_switch_msr *msrs;struct kvm_pmu *pmu =vcpu_to_pmu (&vmx->vcpu); +if (kvm_vcpu_has_mediated_pmu (&vmx->vcpu)) +return ; + pmu->host_cross_mapped_mask =0 ;if (pmu->pebs_enable & pmu->global_ctrl)intel_pmu_cross_mapped_check (pmu);
@@ -7572,6 +7598,9 @@ fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags) vmx->loaded_vmcs->launched =1 ; +if (!msr_write_intercepted (vmx,MSR_CORE_PERF_GLOBAL_CTRL )) +vcpu_to_pmu (vcpu)->global_ctrl =vmcs_read64 (GUEST_IA32_PERF_GLOBAL_CTRL ); +vmx_recover_nmi_blocking (vmx);vmx_complete_interrupts (vmx);
arch/x86/kvm/vmx/vmx.h +2 -1
@@ -510,7 +510,8 @@ static inline u8 vmx_get_rvi(void) VM_EXIT_CLEAR_BNDCFGS | \ VM_EXIT_PT_CONCEAL_PIP | \ VM_EXIT_CLEAR_IA32_RTIT_CTL | \ - VM_EXIT_LOAD_CET_STATE) + VM_EXIT_LOAD_CET_STATE | \ + VM_EXIT_SAVE_IA32_PERF_GLOBAL_CTRL)#define KVM_REQUIRED_VMX_PIN_BASED_VM_EXEC_CONTROL \ (PIN_BASED_EXT_INTR_MASK | \
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH v6 22/44 KVM: x86/pmu: Disable interception of select PMU MSRs for mediated vPMUs
In reply to Sean ChristophersonFrom: Dapeng Mi <[email protected]>
For vCPUs with a mediated vPMU, disable interception of counter MSRs for PMCs that are exposed to the guest, and for GLOBAL_CTRL and related MSRs if they are fully supported according to the vCPU model, i.e. if the MSRs and all bits supported by hardware exist from the guest's point of view.
Do NOT passthrough event selector or fixed counter control MSRs, so that KVM can enforce userspace-defined event filters, e.g. to prevent use of AnyThread events (which is unfortunately a setting in the fixed counter control MSR).
Defer support for nested passthrough of mediated PMU MSRs to the future, as the logic for nested MSR interception is unfortunately vendor specific.
--- arch/x86/kvm/pmu.c | 41 +++++++++++++++++-------- arch/x86/kvm/pmu.h | 1 + arch/x86/kvm/svm/svm.c | 36 ++++++++++++++++++++++ arch/x86/kvm/vmx/pmu_intel.c | 13 -------- arch/x86/kvm/vmx/pmu_intel.h | 15 +++++++++ arch/x86/kvm/vmx/vmx.c | 59 +++++++++++++++++++++++++++++------- 6 files changed, 128 insertions(+), 37 deletions(-)
arch/x86/kvm/pmu.c +28 -13
@@ -719,27 +719,41 @@ int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)return 0 ; } -bool kvm_need_rdpmc_intercept (struct kvm_vcpu *vcpu) +static bool kvm_need_any_pmc_intercept (struct kvm_vcpu *vcpu) {struct kvm_pmu *pmu =vcpu_to_pmu (vcpu);if (!kvm_vcpu_has_mediated_pmu (vcpu))return true; -/* -* VMware allows access to these Pseduo-PMCs even when read via RDPMC -* in Ring3 when CR4.PCE=0. -*/ -if (enable_vmware_backdoor) -return true; -/* * Note! Check *host* PMU capabilities, not KVM's PMU capabilities, as * KVM's capabilities are constrained based on KVM support, i.e. KVM's * capabilities themselves may be a subset of hardware capabilities. */ return pmu->nr_arch_gp_counters != kvm_host_pmu.num_counters_gp || - pmu->nr_arch_fixed_counters != kvm_host_pmu.num_counters_fixed || + pmu->nr_arch_fixed_counters != kvm_host_pmu.num_counters_fixed; +} + +bool kvm_need_perf_global_ctrl_intercept (struct kvm_vcpu *vcpu) +{ +return kvm_need_any_pmc_intercept (vcpu) || + !kvm_pmu_has_perf_global_ctrl (vcpu_to_pmu (vcpu)); +} +EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_need_perf_global_ctrl_intercept); + +bool kvm_need_rdpmc_intercept (struct kvm_vcpu *vcpu) +{ +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); + +/* +* VMware allows access to these Pseduo-PMCs even when read via RDPMC +* in Ring3 when CR4.PCE=0. +*/ +if (enable_vmware_backdoor) +return true; + +return kvm_need_any_pmc_intercept (vcpu) || pmu->counter_bitmask[KVM_PMC_GP ] != (BIT_ULL (kvm_host_pmu.bit_width_gp) -1 ) || pmu->counter_bitmask[KVM_PMC_FIXED ] != (BIT_ULL (kvm_host_pmu.bit_width_fixed) -1 ); }
@@ -936,11 +950,12 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu)* in the global controls). Emulate that behavior when refreshing the * PMU so that userspace doesn't need to manually set PERF_GLOBAL_CTRL. */ -if (kvm_pmu_has_perf_global_ctrl (pmu) && pmu->nr_arch_gp_counters) { +if (pmu->nr_arch_gp_counters && + (kvm_pmu_has_perf_global_ctrl (pmu) ||kvm_vcpu_has_mediated_pmu (vcpu))) pmu->global_ctrl =GENMASK_ULL (pmu->nr_arch_gp_counters -1 ,0 ); -if (kvm_vcpu_has_mediated_pmu (vcpu)) -kvm_pmu_call (write_global_ctrl)(pmu->global_ctrl); - } + +if (kvm_vcpu_has_mediated_pmu (vcpu)) +kvm_pmu_call (write_global_ctrl)(pmu->global_ctrl);bitmap_set (pmu->all_valid_pmc_idx,0 , pmu->nr_arch_gp_counters); bitmap_set(pmu->all_valid_pmc_idx, KVM_FIXED_PMC_BASE_IDX,
arch/x86/kvm/pmu.h +1 -0
@@ -239,6 +239,7 @@ void kvm_pmu_instruction_retired(struct kvm_vcpu *vcpu);void kvm_pmu_branch_retired (struct kvm_vcpu *vcpu);bool is_vmware_backdoor_pmc (u32 pmc_idx); +bool kvm_need_perf_global_ctrl_intercept (struct kvm_vcpu *vcpu);bool kvm_need_rdpmc_intercept (struct kvm_vcpu *vcpu);extern struct kvm_pmu_ops intel_pmu_ops;
arch/x86/kvm/svm/svm.c +36 -0
@@ -730,6 +730,40 @@ void svm_vcpu_free_msrpm(void *msrpm)__free_pages (virt_to_page (msrpm),get_order (MSRPM_SIZE )); } +static void svm_recalc_pmu_msr_intercepts (struct kvm_vcpu *vcpu) +{ +bool intercept = !kvm_vcpu_has_mediated_pmu (vcpu); +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); +int i; + +if (!enable_mediated_pmu) +return ; + +/* Legacy counters are always available for AMD CPUs with a PMU. */ +for (i =0 ; i <min (pmu->nr_arch_gp_counters,AMD64_NUM_COUNTERS ); i++) +svm_set_intercept_for_msr (vcpu,MSR_K7_PERFCTR0 + i, +MSR_TYPE_RW , intercept); + + intercept |= !guest_cpu_cap_has (vcpu,X86_FEATURE_PERFCTR_CORE ); +for (i =0 ; i < pmu->nr_arch_gp_counters; i++) +svm_set_intercept_for_msr (vcpu,MSR_F15H_PERF_CTR +2 * i, +MSR_TYPE_RW , intercept); + +for ( ; i < kvm_pmu_cap.num_counters_gp; i++) +svm_enable_intercept_for_msr (vcpu,MSR_F15H_PERF_CTR +2 * i, +MSR_TYPE_RW ); + + intercept =kvm_need_perf_global_ctrl_intercept (vcpu); +svm_set_intercept_for_msr (vcpu,MSR_AMD64_PERF_CNTR_GLOBAL_CTL , +MSR_TYPE_RW , intercept); +svm_set_intercept_for_msr (vcpu,MSR_AMD64_PERF_CNTR_GLOBAL_STATUS , +MSR_TYPE_RW , intercept); +svm_set_intercept_for_msr (vcpu,MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR , +MSR_TYPE_RW , intercept); +svm_set_intercept_for_msr (vcpu,MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET , +MSR_TYPE_RW , intercept); +} +static void svm_recalc_msr_intercepts (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu);
@@ -798,6 +832,8 @@ static void svm_recalc_msr_intercepts(struct kvm_vcpu *vcpu)if (sev_es_guest (vcpu->kvm))sev_es_recalc_msr_intercepts (vcpu); +svm_recalc_pmu_msr_intercepts (vcpu); +/* * x2APIC intercepts are modified on-demand and cannot be filtered by * userspace.
arch/x86/kvm/vmx/pmu_intel.c +0 -13
@@ -128,19 +128,6 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,return &counters[array_index_nospec (idx, num_counters)]; } -static inline u64 vcpu_get_perf_capabilities (struct kvm_vcpu *vcpu) -{ -if (!guest_cpu_cap_has (vcpu,X86_FEATURE_PDCM )) -return 0 ; - -return vcpu->arch.perf_capabilities; -} - -static inline bool fw_writes_is_enabled (struct kvm_vcpu *vcpu) -{ -return (vcpu_get_perf_capabilities (vcpu) &PERF_CAP_FW_WRITES ) !=0 ; -} -static inline struct kvm_pmc *get_fw_gp_pmc (struct kvm_pmu *pmu,u32 msr) {if (!fw_writes_is_enabled (pmu_to_vcpu (pmu)))
arch/x86/kvm/vmx/pmu_intel.h +15 -0
@@ -4,6 +4,21 @@#include <linux/kvm_host.h> +#include "cpuid.h" + +static inline u64 vcpu_get_perf_capabilities (struct kvm_vcpu *vcpu) +{ +if (!guest_cpu_cap_has (vcpu,X86_FEATURE_PDCM )) +return 0 ; + +return vcpu->arch.perf_capabilities; +} + +static inline bool fw_writes_is_enabled (struct kvm_vcpu *vcpu) +{ +return (vcpu_get_perf_capabilities (vcpu) &PERF_CAP_FW_WRITES ) !=0 ; +} +bool intel_pmu_lbr_is_enabled (struct kvm_vcpu *vcpu);int intel_pmu_create_guest_lbr_event (struct kvm_vcpu *vcpu);
arch/x86/kvm/vmx/vmx.c +48 -11
71 lines not on this page · raw message
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH v6 23/44 KVM: x86/pmu: Bypass perf checks when emulating mediated PMU counter accesses
In reply to Sean ChristophersonFrom: Dapeng Mi <[email protected]>
When emulating a PMC counter read or write for a mediated PMU, bypass the perf checks and emulated_counter logic as the counters aren't proxied through perf, i.e. pmc->counter always holds the guest's up-to-date value, and thus there's no need to defer emulated overflow checks.
--- arch/x86/kvm/pmu.c | 5 +++++ arch/x86/kvm/pmu.h | 3 +++ 2 files changed, 8 insertions(+)
arch/x86/kvm/pmu.c +5 -0
@@ -381,6 +381,11 @@ static void pmc_update_sample_period(struct kvm_pmc *pmc)void pmc_write_counter (struct kvm_pmc *pmc,u64 val) { +if (kvm_vcpu_has_mediated_pmu (pmc->vcpu)) { + pmc->counter = val &pmc_bitmask (pmc); +return ; + } +/* * Drop any unconsumed accumulated counts, the WRMSR is a write, not a * read-modify-write. Adjust the counter value so that its value is
arch/x86/kvm/pmu.h +3 -0
@@ -111,6 +111,9 @@ static inline u64 pmc_read_counter(struct kvm_pmc *pmc) {u64 counter, enabled, running; +if (kvm_vcpu_has_mediated_pmu (pmc->vcpu)) +return pmc->counter &pmc_bitmask (pmc); + counter = pmc->counter + pmc->emulated_counter;if (pmc->perf_event && !pmc->is_paused)
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH v6 24/44 KVM: x86/pmu: Introduce eventsel_hw to prepare for pmu event filtering
In reply to Sean ChristophersonFrom: Mingwei Zhang <[email protected]>
Introduce eventsel_hw and fixed_ctr_ctrl_hw to store the actual HW value in PMU event selector MSRs. In mediated PMU checks events before allowing the event values written to the PMU MSRs. However, to match the HW behavior, when PMU event checks fails, KVM should allow guest to read the value back.
This essentially requires an extra variable to separate the guest requested value from actual PMU MSR value. Note this only applies to event selectors.
--- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/pmu.c | 7 +++++-- arch/x86/kvm/svm/pmu.c | 1 + arch/x86/kvm/vmx/pmu_intel.c | 2 ++ 4 files changed, 10 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -529,6 +529,7 @@ struct kvm_pmc {*/ u64 emulated_counter;u64 eventsel; +u64 eventsel_hw;struct perf_event *perf_event;struct kvm_vcpu *vcpu;/*
@@ -557,6 +558,7 @@ struct kvm_pmu {unsigned nr_arch_fixed_counters;unsigned available_event_types;u64 fixed_ctr_ctrl; +u64 fixed_ctr_ctrl_hw;u64 fixed_ctr_ctrl_rsvd;u64 global_ctrl;u64 global_status;
arch/x86/kvm/pmu.c +5 -2
@@ -902,11 +902,14 @@ static void kvm_pmu_reset(struct kvm_vcpu *vcpu) pmc->counter =0 ; pmc->emulated_counter =0 ; -if (pmc_is_gp (pmc)) +if (pmc_is_gp (pmc)) { pmc->eventsel =0 ; + pmc->eventsel_hw =0 ; + } } - pmu->fixed_ctr_ctrl = pmu->global_ctrl = pmu->global_status =0 ; + pmu->fixed_ctr_ctrl = pmu->fixed_ctr_ctrl_hw =0 ; + pmu->global_ctrl = pmu->global_status =0 ;kvm_pmu_call (reset)(vcpu); }
arch/x86/kvm/svm/pmu.c +1 -0
@@ -166,6 +166,7 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) data &= ~pmu->reserved_bits;if (data != pmc->eventsel) { pmc->eventsel = data; + pmc->eventsel_hw = data;kvm_pmu_request_counter_reprogram (pmc); }return 0 ;
arch/x86/kvm/vmx/pmu_intel.c +2 -0
@@ -61,6 +61,7 @@ static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data)int i; pmu->fixed_ctr_ctrl = data; + pmu->fixed_ctr_ctrl_hw = data;for (i =0 ; i < pmu->nr_arch_fixed_counters; i++) {u8 new_ctrl =fixed_ctrl_field (data, i);u8 old_ctrl =fixed_ctrl_field (old_fixed_ctr_ctrl, i);
@@ -430,6 +431,7 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)if (data != pmc->eventsel) { pmc->eventsel = data; + pmc->eventsel_hw = data;kvm_pmu_request_counter_reprogram (pmc); }break ;
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH v6 25/44 KVM: x86/pmu: Reprogram mediated PMU event selectors on event filter updates
In reply to Sean ChristophersonFrom: Dapeng Mi <[email protected]>
Refresh the event selectors that are programmed into hardware when a PMC is "reprogrammed" for a mediated PMU, i.e. if userspace changes the PMU event filters
Note, KVM doesn't utilize the reprogramming infrastructure to handle counter overflow for mediated PMUs, as there's no need to reprogram a non-existent perf event.
--- arch/x86/kvm/pmu.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
arch/x86/kvm/pmu.c +24 -0
@@ -522,6 +522,25 @@ static bool pmc_is_event_allowed(struct kvm_pmc *pmc)return is_fixed_event_allowed (filter, pmc->idx); } +static void kvm_mediated_pmu_refresh_event_filter (struct kvm_pmc *pmc) +{ +bool allowed =pmc_is_event_allowed (pmc); +struct kvm_pmu *pmu =pmc_to_pmu (pmc); + +if (pmc_is_gp (pmc)) { + pmc->eventsel_hw &= ~ARCH_PERFMON_EVENTSEL_ENABLE ; +if (allowed) + pmc->eventsel_hw |= pmc->eventsel & +ARCH_PERFMON_EVENTSEL_ENABLE ; + }else { +u64 mask =intel_fixed_bits_by_idx (pmc->idx -KVM_FIXED_PMC_BASE_IDX ,0xf ); + + pmu->fixed_ctr_ctrl_hw &= ~mask; +if (allowed) + pmu->fixed_ctr_ctrl_hw |= pmu->fixed_ctr_ctrl & mask; + } +} +static int reprogram_counter (struct kvm_pmc *pmc) {struct kvm_pmu *pmu =pmc_to_pmu (pmc);
@@ -530,6 +549,11 @@ static int reprogram_counter(struct kvm_pmc *pmc)bool emulate_overflow;u8 fixed_ctr_ctrl; +if (kvm_vcpu_has_mediated_pmu (pmu_to_vcpu (pmu))) { +kvm_mediated_pmu_refresh_event_filter (pmc); +return 0 ; + } + emulate_overflow =pmc_pause_counter (pmc); if (!pmc_is_globally_enabled(pmc) || !pmc_is_locally_enabled(pmc) ||
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH v6 26/44 KVM: x86/pmu: Always stuff GuestOnly=1,HostOnly=0 for mediated PMCs on AMD
In reply to Sean ChristophersonFrom: Sandipan Das <[email protected]>
On AMD platforms, there is no way to restore PerfCntrGlobalCtl at VM-Entry or clear it at VM-Exit. Since the register states will be restored before entering and saved after exiting guest context, the counters can keep ticking and even overflow leading to chaos while still in host context.
To avoid this, intecept event selectors, which is already done by mediated PMU. In addition, always set the GuestOnly bit and clear the HostOnly bit for PMU selectors on AMD. Doing so allows the counters run only in guest context even if their enable bits are still set after VM exit and before host/guest PMU context switch.
--- arch/x86/kvm/svm/pmu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/pmu.c +2 -1
@@ -166,7 +166,8 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) data &= ~pmu->reserved_bits;if (data != pmc->eventsel) { pmc->eventsel = data; - pmc->eventsel_hw = data; + pmc->eventsel_hw = (data & ~AMD64_EVENTSEL_HOSTONLY ) | +AMD64_EVENTSEL_GUESTONLY ;kvm_pmu_request_counter_reprogram (pmc); }return 0 ;
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH v6 27/44 KVM: x86/pmu: Load/put mediated PMU context when entering/exiting guest
In reply to Sean ChristophersonFrom: Dapeng Mi <[email protected]>
Implement the PMU "world switch" between host perf and guest mediated PMU. When loading guest state, call into perf to switch from host to guest, and then load guest state into hardware, and then reverse those actions when putting guest state.
On the KVM side, when loading guest state, zero PERF_GLOBAL_CTRL to ensure all counters are disabled, then load selectors and counters, and finally call into vendor code to load control/status information. While VMX and SVM use different mechanisms to avoid counting host activity while guest controls are loaded, both implementations require PERF_GLOBAL_CTRL to be zeroed when the event selectors are in flux.
When putting guest state, reverse the order, and save and zero controls and status prior to saving+zeroing selectors and counters. Defer clearing PERF_GLOBAL_CTRL to vendor code, as only SVM needs to manually clear the MSR; VMX configures PERF_GLOBAL_CTRL to be atomically cleared by the CPU on VM-Exit.
Handle the difference in MSR layouts between Intel and AMD by communicating the bases and stride via kvm_pmu_ops. Because KVM requires Intel v4 (and full-width writes) and AMD v2, the MSRs to load/save are constant for a given vendor, i.e. do not vary based on the guest PMU, and do not vary based on host PMU (because KVM will simply disable mediated PMU support if the necessary MSRs are unsupported).
Except for retrieving the guest's PERF_GLOBAL_CTRL, which needs to be read before invoking any fastpath handler (spoiler alert), perform the context switch around KVM's inner run loop. State only needs to be synchronized from hardware before KVM can access the software "caches".
Note, VMX already grabs the guest's PERF_GLOBAL_CTRL immediately after VM-Exit, as hardware saves value into the VMCS.
--- arch/x86/include/asm/kvm-x86-pmu-ops.h | 2 + arch/x86/include/asm/msr-index.h | 1 + arch/x86/kvm/pmu.c | 130 ++++++++++++++++++++++++- arch/x86/kvm/pmu.h | 10 ++ arch/x86/kvm/svm/pmu.c | 34 +++++++ arch/x86/kvm/svm/svm.c | 3 + arch/x86/kvm/vmx/pmu_intel.c | 44 +++++++++ arch/x86/kvm/x86.c | 4 + 8 files changed, 225 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm-x86-pmu-ops.h +2 -0
@@ -24,6 +24,8 @@ KVM_X86_PMU_OP_OPTIONAL(deliver_pmi) KVM_X86_PMU_OP_OPTIONAL(cleanup) KVM_X86_PMU_OP_OPTIONAL(write_global_ctrl) +KVM_X86_PMU_OP(mediated_load) +KVM_X86_PMU_OP(mediated_put)#undef KVM_X86_PMU_OP#undef KVM_X86_PMU_OP_OPTIONAL
arch/x86/include/asm/msr-index.h +1 -0
@@ -1191,6 +1191,7 @@#define MSR_CORE_PERF_GLOBAL_STATUS 0x0000038e#define MSR_CORE_PERF_GLOBAL_CTRL 0x0000038f#define MSR_CORE_PERF_GLOBAL_OVF_CTRL 0x00000390 +#define MSR_CORE_PERF_GLOBAL_STATUS_SET 0x00000391#define MSR_PERF_METRICS 0x00000329
arch/x86/kvm/pmu.c +127 -3
140 lines not on this page · raw message
arch/x86/kvm/pmu.h +10 -0
@@ -38,11 +38,19 @@ struct kvm_pmu_ops {void (*cleanup)(struct kvm_vcpu *vcpu);bool (*is_mediated_pmu_supported)(struct x86_pmu_capability *host_pmu); +void (*mediated_load)(struct kvm_vcpu *vcpu); +void (*mediated_put)(struct kvm_vcpu *vcpu);void (*write_global_ctrl)(u64 global_ctrl);const u64 EVENTSEL_EVENT ;const int MAX_NR_GP_COUNTERS ;const int MIN_NR_GP_COUNTERS ; + +const u32 PERF_GLOBAL_CTRL ; +const u32 GP_EVENTSEL_BASE ; +const u32 GP_COUNTER_BASE ; +const u32 FIXED_COUNTER_BASE ; +const u32 MSR_STRIDE ; };void kvm_pmu_ops_update (const struct kvm_pmu_ops *pmu_ops);
@@ -240,6 +248,8 @@ void kvm_pmu_destroy(struct kvm_vcpu *vcpu);int kvm_vm_ioctl_set_pmu_event_filter (struct kvm *kvm,void __user *argp);void kvm_pmu_instruction_retired (struct kvm_vcpu *vcpu);void kvm_pmu_branch_retired (struct kvm_vcpu *vcpu); +void kvm_mediated_pmu_load (struct kvm_vcpu *vcpu); +void kvm_mediated_pmu_put (struct kvm_vcpu *vcpu);bool is_vmware_backdoor_pmc (u32 pmc_idx);bool kvm_need_perf_global_ctrl_intercept (struct kvm_vcpu *vcpu);
arch/x86/kvm/svm/pmu.c +34 -0
@@ -234,6 +234,32 @@ static bool amd_pmu_is_mediated_pmu_supported(struct x86_pmu_capability *host_pmreturn host_pmu->version >=2 ; } +static void amd_mediated_pmu_load (struct kvm_vcpu *vcpu) +{ +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); +u64 global_status; + +rdmsrq (MSR_AMD64_PERF_CNTR_GLOBAL_STATUS , global_status); +/* Clear host global_status MSR if non-zero. */ +if (global_status) +wrmsrq (MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR , global_status); + +wrmsrq (MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET , pmu->global_status); +wrmsrq (MSR_AMD64_PERF_CNTR_GLOBAL_CTL , pmu->global_ctrl); +} + +static void amd_mediated_pmu_put (struct kvm_vcpu *vcpu) +{ +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); + +wrmsrq (MSR_AMD64_PERF_CNTR_GLOBAL_CTL ,0 ); +rdmsrq (MSR_AMD64_PERF_CNTR_GLOBAL_STATUS , pmu->global_status); + +/* Clear global status bits if non-zero */ +if (pmu->global_status) +wrmsrq (MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR , pmu->global_status); +} +struct kvm_pmu_ops amd_pmu_ops__initdata = { .rdpmc_ecx_to_pmc = amd_rdpmc_ecx_to_pmc, .msr_idx_to_pmc = amd_msr_idx_to_pmc,
@@ -245,8 +271,16 @@ struct kvm_pmu_ops amd_pmu_ops __initdata = { .init = amd_pmu_init, .is_mediated_pmu_supported = amd_pmu_is_mediated_pmu_supported, + .mediated_load = amd_mediated_pmu_load, + .mediated_put = amd_mediated_pmu_put, .EVENTSEL_EVENT = AMD64_EVENTSEL_EVENT, .MAX_NR_GP_COUNTERS = KVM_MAX_NR_AMD_GP_COUNTERS, .MIN_NR_GP_COUNTERS = AMD64_NUM_COUNTERS, + + .PERF_GLOBAL_CTRL = MSR_AMD64_PERF_CNTR_GLOBAL_CTL, + .GP_EVENTSEL_BASE = MSR_F15H_PERF_CTL0, + .GP_COUNTER_BASE = MSR_F15H_PERF_CTR0, + .FIXED_COUNTER_BASE = 0, + .MSR_STRIDE = 2, };
arch/x86/kvm/svm/svm.c +3 -0
@@ -4368,6 +4368,9 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags) vcpu->arch.regs_avail &= ~SVM_REGS_LAZY_LOAD_SET ; +if (!msr_write_intercepted (vcpu,MSR_AMD64_PERF_CNTR_GLOBAL_CTL )) +rdmsrq (MSR_AMD64_PERF_CNTR_GLOBAL_CTL ,vcpu_to_pmu (vcpu)->global_ctrl); +trace_kvm_exit (vcpu,KVM_ISA_SVM );svm_complete_interrupts (vcpu);
arch/x86/kvm/vmx/pmu_intel.c +44 -0
59 lines not on this page · raw message
arch/x86/kvm/x86.c +4 -0
@@ -11334,6 +11334,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) run_flags |=KVM_RUN_LOAD_DEBUGCTL ; vcpu->arch.host_debugctl = debug_ctl; +kvm_mediated_pmu_load (vcpu); +guest_timing_enter_irqoff ();/*
@@ -11372,6 +11374,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)kvm_load_host_pkru (vcpu); +kvm_mediated_pmu_put (vcpu); +/* * Do this here before restoring debug registers on the host. And * since we do this before handling the vmexit, a DR access vmexit
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH v6 28/44 KVM: x86/pmu: Disallow emulation in the fastpath if mediated PMCs are active
In reply to Sean ChristophersonDon't handle exits in the fastpath if emulation is required, i.e. if an instruction needs to be skipped, the mediated PMU is enabled, and one or more PMCs is counting instructions. With the mediated PMU, KVM's cache of PMU state is inconsistent with respect to hardware until KVM exits the inner run loop (when the mediated PMU is "put").
--- arch/x86/kvm/pmu.h | 10 ++++++++++ arch/x86/kvm/x86.c | 9 +++++++++ 2 files changed, 19 insertions(+)
arch/x86/kvm/pmu.h +10 -0
@@ -234,6 +234,16 @@ static inline bool pmc_is_globally_enabled(struct kvm_pmc *pmc)return test_bit (pmc->idx, (unsigned long *)&pmu->global_ctrl); } +static inline bool kvm_pmu_is_fastpath_emulation_allowed (struct kvm_vcpu *vcpu) +{ +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); + +return !kvm_vcpu_has_mediated_pmu (vcpu) || + !bitmap_intersects (pmu->pmc_counting_instructions, + (unsigned long *)&pmu->global_ctrl, +X86_PMC_IDX_MAX ); +} +void kvm_pmu_deliver_pmi (struct kvm_vcpu *vcpu);void kvm_pmu_handle_event (struct kvm_vcpu *vcpu);int kvm_pmu_rdpmc (struct kvm_vcpu *vcpu,unsigned pmc,u64 *data);
arch/x86/kvm/x86.c +9 -0
@@ -2215,6 +2215,9 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_emulate_invd);fastpath_t handle_fastpath_invd (struct kvm_vcpu *vcpu) { +if (!kvm_pmu_is_fastpath_emulation_allowed (vcpu)) +return EXIT_FASTPATH_NONE ; +if (!kvm_emulate_invd (vcpu))return EXIT_FASTPATH_EXIT_USERSPACE ;
@@ -2271,6 +2274,9 @@ static inline bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu)static fastpath_t __handle_fastpath_wrmsr (struct kvm_vcpu *vcpu,u32 msr,u64 data) { +if (!kvm_pmu_is_fastpath_emulation_allowed (vcpu)) +return EXIT_FASTPATH_NONE ; +switch (msr) {case APIC_BASE_MSR + (APIC_ICR >>4 ): if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(vcpu->arch.apic) ||
@@ -11714,6 +11720,9 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_emulate_halt);fastpath_t handle_fastpath_hlt (struct kvm_vcpu *vcpu) { +if (!kvm_pmu_is_fastpath_emulation_allowed (vcpu)) +return EXIT_FASTPATH_NONE ; +if (!kvm_emulate_halt (vcpu))return EXIT_FASTPATH_EXIT_USERSPACE ;
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH v6 29/44 KVM: x86/pmu: Handle emulated instruction for mediated vPMU
In reply to Sean ChristophersonFrom: Dapeng Mi <[email protected]>
Mediated vPMU needs to accumulate the emulated instructions into counter and load the counter into HW at vm-entry.
Moreover, if the accumulation leads to counter overflow, KVM needs to update GLOBAL_STATUS and inject PMI into guest as well.
--- arch/x86/kvm/pmu.c | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-)
arch/x86/kvm/pmu.c +37 -2
@@ -1033,10 +1033,45 @@ void kvm_pmu_destroy(struct kvm_vcpu *vcpu)kvm_pmu_reset (vcpu); } +static bool pmc_is_pmi_enabled (struct kvm_pmc *pmc) +{ +u8 fixed_ctr_ctrl; + +if (pmc_is_gp (pmc)) +return pmc->eventsel &ARCH_PERFMON_EVENTSEL_INT ; + + fixed_ctr_ctrl =fixed_ctrl_field (pmc_to_pmu (pmc)->fixed_ctr_ctrl, + pmc->idx -KVM_FIXED_PMC_BASE_IDX ); +return fixed_ctr_ctrl &INTEL_FIXED_0_ENABLE_PMI ; +} +static void kvm_pmu_incr_counter (struct kvm_pmc *pmc) { - pmc->emulated_counter++; -kvm_pmu_request_counter_reprogram (pmc); +struct kvm_vcpu *vcpu = pmc->vcpu; + +/* +* For perf-based PMUs, accumulate software-emulated events separately +* from pmc->counter, as pmc->counter is offset by the count of the +* associated perf event. Request reprogramming, which will consult +* both emulated and hardware-generated events to detect overflow. +*/ +if (!kvm_vcpu_has_mediated_pmu (vcpu)) { + pmc->emulated_counter++; +kvm_pmu_request_counter_reprogram (pmc); +return ; + } + +/* +* For mediated PMUs, pmc->counter is updated when the vCPU's PMU is +* put, and will be loaded into hardware when the PMU is loaded. Simply +* increment the counter and signal overflow if it wraps to zero. +*/ + pmc->counter = (pmc->counter +1 ) &pmc_bitmask (pmc); +if (!pmc->counter) { +pmc_to_pmu (pmc)->global_status |=BIT_ULL (pmc->idx); +if (pmc_is_pmi_enabled (pmc)) +kvm_make_request (KVM_REQ_PMI , vcpu); + } }static inline bool cpl_is_matched (struct kvm_pmc *pmc)
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH v6 30/44 KVM: nVMX: Add macros to simplify nested MSR interception setting
In reply to Sean ChristophersonFrom: Dapeng Mi <[email protected]>
Add macros nested_vmx_merge_msr_bitmaps_xxx() to simplify nested MSR interception setting. No function change intended.
--- arch/x86/kvm/vmx/nested.c | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-)
arch/x86/kvm/vmx/nested.c +19 -16
@@ -617,6 +617,19 @@ static inline void nested_vmx_set_intercept_for_msr(struct vcpu_vmx *vmx, msr_bitmap_l0, msr); } +#define nested_vmx_merge_msr_bitmaps (msr, type) \ + nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, \ + msr_bitmap_l0, msr, type) + +#define nested_vmx_merge_msr_bitmaps_read (msr) \ + nested_vmx_merge_msr_bitmaps(msr, MSR_TYPE_R) + +#define nested_vmx_merge_msr_bitmaps_write (msr) \ + nested_vmx_merge_msr_bitmaps(msr, MSR_TYPE_W) + +#define nested_vmx_merge_msr_bitmaps_rw (msr) \ + nested_vmx_merge_msr_bitmaps(msr, MSR_TYPE_RW) +/* * Merge L0's and L1's MSR bitmap, return false to indicate that * we do not use the hardware.
@@ -700,23 +713,13 @@ static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,* other runtime changes to vmcs01's bitmap, e.g. dynamic pass-through. */ #ifdef CONFIG_X86_64 -nested_vmx_set_intercept_for_msr (vmx, msr_bitmap_l1, msr_bitmap_l0, -MSR_FS_BASE ,MSR_TYPE_RW ); - -nested_vmx_set_intercept_for_msr (vmx, msr_bitmap_l1, msr_bitmap_l0, -MSR_GS_BASE ,MSR_TYPE_RW ); - -nested_vmx_set_intercept_for_msr (vmx, msr_bitmap_l1, msr_bitmap_l0, -MSR_KERNEL_GS_BASE ,MSR_TYPE_RW ); +nested_vmx_merge_msr_bitmaps_rw (MSR_FS_BASE ); +nested_vmx_merge_msr_bitmaps_rw (MSR_GS_BASE ); +nested_vmx_merge_msr_bitmaps_rw (MSR_KERNEL_GS_BASE );#endif -nested_vmx_set_intercept_for_msr (vmx, msr_bitmap_l1, msr_bitmap_l0, -MSR_IA32_SPEC_CTRL ,MSR_TYPE_RW ); - -nested_vmx_set_intercept_for_msr (vmx, msr_bitmap_l1, msr_bitmap_l0, -MSR_IA32_PRED_CMD ,MSR_TYPE_W ); - -nested_vmx_set_intercept_for_msr (vmx, msr_bitmap_l1, msr_bitmap_l0, -MSR_IA32_FLUSH_CMD ,MSR_TYPE_W ); +nested_vmx_merge_msr_bitmaps_rw (MSR_IA32_SPEC_CTRL ); +nested_vmx_merge_msr_bitmaps_write (MSR_IA32_PRED_CMD ); +nested_vmx_merge_msr_bitmaps_write (MSR_IA32_FLUSH_CMD );nested_vmx_set_intercept_for_msr (vmx, msr_bitmap_l1, msr_bitmap_l0,MSR_IA32_APERF ,MSR_TYPE_R );
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH v6 31/44 KVM: nVMX: Disable PMU MSR interception as appropriate while running L2
In reply to Sean ChristophersonFrom: Mingwei Zhang <[email protected]>
Merge KVM's PMU MSR interception bitmaps with those of L1, i.e. merge the bitmaps of vmcs01 and vmcs12, e.g. so that KVM doesn't interpose on MSR accesses unnecessarily if L1 exposes a mediated PMU (or equivalent) to L2.
--- arch/x86/kvm/vmx/nested.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
arch/x86/kvm/vmx/nested.c +30 -0
@@ -630,6 +630,34 @@ static inline void nested_vmx_set_intercept_for_msr(struct vcpu_vmx *vmx,#define nested_vmx_merge_msr_bitmaps_rw (msr) \ nested_vmx_merge_msr_bitmaps(msr, MSR_TYPE_RW) +static void nested_vmx_merge_pmu_msr_bitmaps (struct kvm_vcpu *vcpu, +unsigned long *msr_bitmap_l1, +unsigned long *msr_bitmap_l0) +{ +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); +struct vcpu_vmx *vmx =to_vmx (vcpu); +int i; + +/* +* Skip the merges if the vCPU doesn't have a mediated PMU MSR, i.e. if +* none of the MSRs can possibly be passed through to L1. +*/ +if (!kvm_vcpu_has_mediated_pmu (vcpu)) +return ; + +for (i =0 ; i < pmu->nr_arch_gp_counters; i++) { +nested_vmx_merge_msr_bitmaps_rw (MSR_IA32_PERFCTR0 + i); +nested_vmx_merge_msr_bitmaps_rw (MSR_IA32_PMC0 + i); + } + +for (i =0 ; i < pmu->nr_arch_fixed_counters; i++) +nested_vmx_merge_msr_bitmaps_rw (MSR_CORE_PERF_FIXED_CTR0 + i); + +nested_vmx_merge_msr_bitmaps_rw (MSR_CORE_PERF_GLOBAL_CTRL ); +nested_vmx_merge_msr_bitmaps_read (MSR_CORE_PERF_GLOBAL_STATUS ); +nested_vmx_merge_msr_bitmaps_write (MSR_CORE_PERF_GLOBAL_OVF_CTRL ); +} +/* * Merge L0's and L1's MSR bitmap, return false to indicate that * we do not use the hardware.
@@ -745,6 +773,8 @@ static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,nested_vmx_set_intercept_for_msr (vmx, msr_bitmap_l1, msr_bitmap_l0,MSR_IA32_PL3_SSP ,MSR_TYPE_RW ); +nested_vmx_merge_pmu_msr_bitmaps (vcpu, msr_bitmap_l1, msr_bitmap_l0); +kvm_vcpu_unmap (vcpu, &map); vmx->nested.force_msr_bitmap_recalc = false;
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH v6 32/44 KVM: nSVM: Disable PMU MSR interception as appropriate while running L2
In reply to Sean ChristophersonAdd MSRs that might be passed through to L1 when running with a mediated PMU to the nested SVM's set of to-be-merged MSR indices, i.e. disable interception of PMU MSRs when running L2 if both KVM (L0) and L1 disable interception. There is no need for KVM to interpose on such MSR accesses, e.g. if L1 exposes a mediated PMU (or equivalent) to L2.
--- arch/x86/kvm/svm/nested.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/nested.c +17 -1
@@ -194,7 +194,7 @@ void recalc_intercepts(struct vcpu_svm *svm)* Hardcode the capacity of the array based on the maximum number of _offsets_. * MSRs are batched together, so there are fewer offsets than MSRs. */ -static int nested_svm_msrpm_merge_offsets[7 ]__ro_after_init ; +static int nested_svm_msrpm_merge_offsets[10 ]__ro_after_init ;static int nested_svm_nr_msrpm_merge_offsets__ro_after_init ;typedef unsigned long nsvm_msrpm_merge_t ;
@@ -222,6 +222,22 @@ int __init nested_svm_init_msrpm_merge_offsets(void)MSR_IA32_LASTBRANCHTOIP ,MSR_IA32_LASTINTFROMIP ,MSR_IA32_LASTINTTOIP , + +MSR_K7_PERFCTR0 , +MSR_K7_PERFCTR1 , +MSR_K7_PERFCTR2 , +MSR_K7_PERFCTR3 , +MSR_F15H_PERF_CTR0 , +MSR_F15H_PERF_CTR1 , +MSR_F15H_PERF_CTR2 , +MSR_F15H_PERF_CTR3 , +MSR_F15H_PERF_CTR4 , +MSR_F15H_PERF_CTR5 , + +MSR_AMD64_PERF_CNTR_GLOBAL_CTL , +MSR_AMD64_PERF_CNTR_GLOBAL_STATUS , +MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR , + MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET, };int i, j;
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH v6 33/44 KVM: x86/pmu: Expose enable_mediated_pmu parameter to user space
In reply to Sean ChristophersonFrom: Dapeng Mi <[email protected]>
Expose enable_mediated_pmu parameter to user space, i.e. allow userspace to enable/disable mediated vPMU support.
Document the mediated versus perf-based behavior as part of the kernel-parameters.txt entry, and opportunistically add an entry for the core enable_pmu param as well.
--- .../admin-guide/kernel-parameters.txt | 49 +++++++++++++++++++ arch/x86/kvm/svm/svm.c | 2 + arch/x86/kvm/vmx/vmx.c | 2 + 3 files changed, 53 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +49 -0
@@ -2926,6 +2926,26 @@ Default is Y (on). + kvm.enable_pmu=[KVM,X86] + If enabled, KVM will virtualize PMU functionality based + on the virtual CPU model defined by userspace. This + can be overridden on a per-VM basis via + KVM_CAP_PMU_CAPABILITY. + + If disabled, KVM will not virtualize PMU functionality, + e.g. MSRs, PMCs, PMIs, etc., even if userspace defines + a virtual CPU model that contains PMU assets. + + Note, KVM's vPMU support implicitly requires running + with an in-kernel local APIC, e.g. to deliver PMIs to + the guest. Running without an in-kernel local APIC is + not supported, though KVM will allow such a combination + (with severely degraded functionality). + + See also enable_mediated_pmu. + + Default is Y (on). + kvm.enable_virt_at_load=[KVM,ARM64,LOONGARCH,MIPS,RISCV,X86] If enabled, KVM will enable virtualization in hardware when KVM is loaded, and disable virtualization when KVM
@@ -2972,6 +2992,35 @@ If the value is 0 (the default), KVM will pick a period based on the ratio, such that a page is zapped after 1 hour on average. + kvm-{amd,intel}.enable_mediated_pmu=[KVM,AMD,INTEL] + If enabled, KVM will provide a mediated virtual PMU, + instead of the default perf-based virtual PMU (if + kvm.enable_pmu is true and PMU is enumerated via the + virtual CPU model). + + With a perf-based vPMU, KVM operates as a user of perf, + i.e. emulates guest PMU counters using perf events. + KVM-created perf events are managed by perf as regular + (guest-only) events, e.g. are scheduled in/out, contend + for hardware resources, etc. Using a perf-based vPMU + allows guest and host usage of the PMU to co-exist, but + incurs non-trivial overhead and can result in silently + dropped guest events (due to resource contention). + + With a mediated vPMU, hardware PMU state is context + switched around the world switch to/from the guest. + KVM mediates which events the guest can utilize, but + gives the guest direct access to all other PMU assets + when possible (KVM may intercept some accesses if the + virtual CPU model provides a subset of hardware PMU + functionality). Using a mediated vPMU significantly + reduces PMU virtualization overhead and eliminates lost + guest events, but is mutually exclusive with using perf + to profile KVM guests and adds latency to most VM-Exits + (to context switch PMU state). + + Default is N (off). + kvm-amd.nested= [KVM,AMD] Control nested virtualization feature in KVM/SVM. Default is 1 (enabled).
arch/x86/kvm/svm/svm.c +2 -0
@@ -170,6 +170,8 @@ module_param(intercept_smi, bool, 0444);bool vnmi = true;module_param (vnmi, bool,0444 ); +module_param (enable_mediated_pmu, bool,0444 ); +static bool svm_gp_erratum_intercept = true;static u8 rsm_ins_bytes[] ="\x0f\xaa" ;
arch/x86/kvm/vmx/vmx.c +2 -0
@@ -150,6 +150,8 @@ module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);extern bool __read_mostly allow_smaller_maxphyaddr;module_param (allow_smaller_maxphyaddr, bool,S_IRUGO ); +module_param (enable_mediated_pmu, bool,0444 ); +#define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD)#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE#define KVM_VM_CR0_ALWAYS_ON \
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH v6 34/44 KVM: x86/pmu: Elide WRMSRs when loading guest PMCs if values already match
In reply to Sean ChristophersonWhen loading a mediated PMU state, elide the WRMSRs to load PMCs with the guest's value if the value in hardware already matches the guest's value. For the relatively common case where neither the guest nor the host is actively using the PMU, i.e. when all/many counters are '0', eliding the WRMSRs reduces the latency of handling VM-Exit by a measurable amount (WRMSR is significantly more expensive than RDPMC).
As measured by KVM-Unit-Tests' CPUID VM-Exit testcase, this provides a a ~25% reduction in latency (4k => 3k cycles) on Intel Emerald Rapids, and a ~13% reduction (6.2k => 5.3k cycles) on AMD Turin.
--- arch/x86/kvm/pmu.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
arch/x86/kvm/pmu.c +4 -2
@@ -1314,13 +1314,15 @@ static void kvm_pmu_load_guest_pmcs(struct kvm_vcpu *vcpu)for (i =0 ; i < pmu->nr_arch_gp_counters; i++) { pmc = &pmu->gp_counters[i]; -wrmsrl (gp_counter_msr (i), pmc->counter); +if (pmc->counter !=rdpmc (i)) +wrmsrl (gp_counter_msr (i), pmc->counter);wrmsrl (gp_eventsel_msr (i), pmc->eventsel_hw); }for (i =0 ; i < pmu->nr_arch_fixed_counters; i++) { pmc = &pmu->fixed_counters[i]; -wrmsrl (fixed_counter_msr (i), pmc->counter); +if (pmc->counter !=rdpmc (INTEL_PMC_FIXED_RDPMC_BASE | i)) +wrmsrl (fixed_counter_msr (i), pmc->counter); } }
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH v6 35/44 KVM: VMX: Drop intermediate "guest" field from msr_autostore
In reply to Sean ChristophersonDrop the intermediate "guest" field from vcpu_vmx.msr_autostore as the value saved on VM-Exit isn't guaranteed to be the guest's value, it's purely whatever is in hardware at the time of VM-Exit. E.g. KVM's only use of the store list at the momemnt is to snapshot TSC at VM-Exit, and the value saved is always the raw TSC even if TSC-offseting and/or TSC-scaling is enabled for the guest.
And unlike msr_autoload, there is no need differentiate between "on-entry" and "on-exit".
No functional change intended.
--- arch/x86/kvm/vmx/nested.c | 10 +++++----- arch/x86/kvm/vmx/vmx.c | 2 +- arch/x86/kvm/vmx/vmx.h | 4 +--- 3 files changed, 7 insertions(+), 9 deletions(-)
arch/x86/kvm/vmx/nested.c +5 -5
@@ -1076,11 +1076,11 @@ static bool nested_vmx_get_vmexit_msr_value(struct kvm_vcpu *vcpu,* VM-exit in L0, use the more accurate value. */ if (msr_index ==MSR_IA32_TSC ) { -int i =vmx_find_loadstore_msr_slot (&vmx->msr_autostore.guest, +int i =vmx_find_loadstore_msr_slot (&vmx->msr_autostore,MSR_IA32_TSC );if (i >=0 ) { -u64 val = vmx->msr_autostore.guest.val[i].value; +u64 val = vmx->msr_autostore.val[i].value; *data =kvm_read_l1_tsc (vcpu, val);return true;
@@ -1167,7 +1167,7 @@ static void prepare_vmx_msr_autostore_list(struct kvm_vcpu *vcpu, u32 msr_index) {struct vcpu_vmx *vmx =to_vmx (vcpu); -struct vmx_msrs *autostore = &vmx->msr_autostore.guest; +struct vmx_msrs *autostore = &vmx->msr_autostore;bool in_vmcs12_store_list;int msr_autostore_slot;bool in_autostore_list;
@@ -2366,7 +2366,7 @@ static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)* addresses are constant (for vmcs02), the counts can change based * on L2's behavior, e.g. switching to/from long mode. */ -vmcs_write64 (VM_EXIT_MSR_STORE_ADDR ,__pa (vmx->msr_autostore.guest.val)); +vmcs_write64 (VM_EXIT_MSR_STORE_ADDR ,__pa (vmx->msr_autostore.val));vmcs_write64 (VM_EXIT_MSR_LOAD_ADDR ,__pa (vmx->msr_autoload.host.val));vmcs_write64 (VM_ENTRY_MSR_LOAD_ADDR ,__pa (vmx->msr_autoload.guest.val));
@@ -2704,7 +2704,7 @@ static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)*/ prepare_vmx_msr_autostore_list (&vmx->vcpu,MSR_IA32_TSC ); -vmcs_write32 (VM_EXIT_MSR_STORE_COUNT , vmx->msr_autostore.guest.nr); +vmcs_write32 (VM_EXIT_MSR_STORE_COUNT , vmx->msr_autostore.nr);vmcs_write32 (VM_EXIT_MSR_LOAD_COUNT , vmx->msr_autoload.host.nr);vmcs_write32 (VM_ENTRY_MSR_LOAD_COUNT , vmx->msr_autoload.guest.nr);
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -6567,7 +6567,7 @@ void dump_vmcs(struct kvm_vcpu *vcpu)if (vmcs_read32 (VM_ENTRY_MSR_LOAD_COUNT ) >0 )vmx_dump_msrs ("guest autoload" , &vmx->msr_autoload.guest);if (vmcs_read32 (VM_EXIT_MSR_STORE_COUNT ) >0 ) -vmx_dump_msrs ("guest autostore" , &vmx->msr_autostore.guest); +vmx_dump_msrs ("autostore" , &vmx->msr_autostore);if (vmentry_ctl &VM_ENTRY_LOAD_CET_STATE ) pr_err("S_CET = 0x%016lx, SSP = 0x%016lx, SSP TABLE = 0x%016lx\n",
arch/x86/kvm/vmx/vmx.h +1 -3
@@ -245,9 +245,7 @@ struct vcpu_vmx {struct vmx_msrs host; } msr_autoload; -struct msr_autostore { -struct vmx_msrs guest; - } msr_autostore; +struct vmx_msrs msr_autostore;struct {int vm86_active;
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Re: [PATCH v6 35/44] KVM: VMX: Drop intermediate "guest" field from msr_autostore
Quoted 83 lines · Sean Christopherson
Drop the intermediate "guest" field from vcpu_vmx.msr_autostore as the value saved on VM-Exit isn't guaranteed to be the guest's value, it's purely whatever is in hardware at the time of VM-Exit. E.g. KVM's only use of the store list at the momemnt is to snapshot TSC at VM-Exit, and the value saved is always the raw TSC even if TSC-offseting and/or TSC-scaling is enabled for the guest.
And unlike
msr_autoload, there is no need differentiate between "on-entry" and "on-exit".No functional change intended.
Cc Jim Mattson <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/nested.c | 10 +++++----- arch/x86/kvm/vmx/vmx.c | 2 +- arch/x86/kvm/vmx/vmx.h | 4 +--- 3 files changed, 7 insertions(+), 9 deletions(-)arch/x86/kvm/vmx/nested.c +5
-5@@ -1076,11 +1076,11 @@ static bool nested_vmx_get_vmexit_msr_value(struct kvm_vcpu *vcpu,* VM-exit in L0, use the more accurate value. */ if (msr_index ==MSR_IA32_TSC ) { -int i =vmx_find_loadstore_msr_slot (&vmx->msr_autostore.guest, +int i =vmx_find_loadstore_msr_slot (&vmx->msr_autostore,MSR_IA32_TSC );if (i >=0 ) { -u64 val = vmx->msr_autostore.guest.val[i].value; +u64 val = vmx->msr_autostore.val[i].value; *data =kvm_read_l1_tsc (vcpu, val);return true;@@ -1167,7 +1167,7 @@ static void prepare_vmx_msr_autostore_list(struct kvm_vcpu *vcpu, u32 msr_index) {struct vcpu_vmx *vmx =to_vmx (vcpu); -struct vmx_msrs *autostore = &vmx->msr_autostore.guest; +struct vmx_msrs *autostore = &vmx->msr_autostore;bool in_vmcs12_store_list;int msr_autostore_slot;bool in_autostore_list;@@ -2366,7 +2366,7 @@ static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)* addresses are constant (for vmcs02), the counts can change based * on L2's behavior, e.g. switching to/from long mode. */ -vmcs_write64 (VM_EXIT_MSR_STORE_ADDR ,__pa (vmx->msr_autostore.guest.val)); +vmcs_write64 (VM_EXIT_MSR_STORE_ADDR ,__pa (vmx->msr_autostore.val));vmcs_write64 (VM_EXIT_MSR_LOAD_ADDR ,__pa (vmx->msr_autoload.host.val));vmcs_write64 (VM_ENTRY_MSR_LOAD_ADDR ,__pa (vmx->msr_autoload.guest.val));@@ -2704,7 +2704,7 @@ static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)*/ prepare_vmx_msr_autostore_list (&vmx->vcpu,MSR_IA32_TSC ); -vmcs_write32 (VM_EXIT_MSR_STORE_COUNT , vmx->msr_autostore.guest.nr); +vmcs_write32 (VM_EXIT_MSR_STORE_COUNT , vmx->msr_autostore.nr);vmcs_write32 (VM_EXIT_MSR_LOAD_COUNT , vmx->msr_autoload.host.nr);vmcs_write32 (VM_ENTRY_MSR_LOAD_COUNT , vmx->msr_autoload.guest.nr);arch/x86/kvm/vmx/vmx.c +1
-1@@ -6567,7 +6567,7 @@ void dump_vmcs(struct kvm_vcpu *vcpu)if (vmcs_read32 (VM_ENTRY_MSR_LOAD_COUNT ) >0 )vmx_dump_msrs ("guest autoload" , &vmx->msr_autoload.guest);if (vmcs_read32 (VM_EXIT_MSR_STORE_COUNT ) >0 ) -vmx_dump_msrs ("guest autostore" , &vmx->msr_autostore.guest); +vmx_dump_msrs ("autostore" , &vmx->msr_autostore);if (vmentry_ctl &VM_ENTRY_LOAD_CET_STATE ) pr_err("S_CET = 0x%016lx, SSP = 0x%016lx, SSP TABLE = 0x%016lx\n",arch/x86/kvm/vmx/vmx.h +0
-0
arch/x86/kvm/vmx/vmx.h struct vcpu_vmx { · line 251
Quoted 10 lines
arch/x86/kvm/vmx/vmx.h +1
-3@@ -245,9 +245,7 @@ struct vcpu_vmx {struct vmx_msrs host; } msr_autoload; -struct msr_autostore { -struct vmx_msrs guest; - } msr_autostore; +struct vmx_msrs msr_autostore;struct {int vm86_active;
LGTM.
Sean Christopherson
maintainerPATCH v6 36/44 KVM: nVMX: Don't update msr_autostore count when saving TSC for vmcs12
In reply to Sean ChristophersonRework nVMX's use of the MSR auto-store list to snapshot TSC to sneak MSR_IA32_TSC into the list _without_ updating KVM's software tracking, and drop the generic functionality so that future usage of the store list for nested specific logic needs to consider the implications of modifying the list. Updating the list only for vmcs02 and only on nested VM-Enter is a disaster waiting to happen, as it means vmcs01 is stale relative to the software tracking, and KVM could unintentionally leave an MSR in the store list in perpetuity while running L1, e.g. if KVM addressed the first issue and updated vmcs01 on nested VM-Exit without removing TSC from the list.
Furthermore, mixing KVM's desire to save an MSR with L1's desire to save an MSR result KVM clobbering/ignoring the needs of vmcs01 or vmcs02. E.g. if KVM added MSR_IA32_TSC to the store list for its own purposes, and then _removed_ MSR_IA32_TSC from the list after emulating nested VM-Enter, then KVM would remove MSR_IA32_TSC from the list even though saving TSC on VM-Exit from L2 is still desirable (to provide L1 with an accurate TSC).
Similarly, removing an MSR from the list based on vmcs12's settings could drop an MSR that KVM wants to save for its own purposes.
In practice, the issues are currently benign, because KVM doesn't use the store list for vmcs01. But that will change with upcoming mediated PMU support.
Alternatively, a "full" solution would be to track MSR list entries for vmcs12 separately from KVM's standard lists, but MSR_IA32_TSC is likely the only MSR that KVM would ever want to save on _every_ VM-Exit purely based on vmcs12. I.e. the added complexity isn't remotely justified at this time.
Opportunistically escalate from a pr_warn_ratelimited() to a full WARN as KVM reserves eight entries in each MSR list, and as above KVM uses at most one entry.
Opportunistically make vmx_find_loadstore_msr_slot() local to vmx.c as using it directly from nested code is unsafe due to the potential for mixing vmcs01 and vmcs02 state (see above).
--- arch/x86/kvm/vmx/nested.c | 71 ++++++++++++--------------------------- arch/x86/kvm/vmx/vmx.c | 2 +- arch/x86/kvm/vmx/vmx.h | 2 +- 3 files changed, 24 insertions(+), 51 deletions(-)
arch/x86/kvm/vmx/nested.c +22 -49
@@ -1075,16 +1075,12 @@ static bool nested_vmx_get_vmexit_msr_value(struct kvm_vcpu *vcpu,* does not include the time taken for emulation of the L2->L1 * VM-exit in L0, use the more accurate value. */ -if (msr_index ==MSR_IA32_TSC ) { -int i =vmx_find_loadstore_msr_slot (&vmx->msr_autostore, -MSR_IA32_TSC ); +if (msr_index ==MSR_IA32_TSC && vmx->nested.tsc_autostore_slot >=0 ) { +int slot = vmx->nested.tsc_autostore_slot; +u64 host_tsc = vmx->msr_autostore.val[slot].value; -if (i >=0 ) { -u64 val = vmx->msr_autostore.val[i].value; - - *data =kvm_read_l1_tsc (vcpu, val); -return true; - } + *data =kvm_read_l1_tsc (vcpu, host_tsc); +return true; }if (kvm_emulate_msr_read (vcpu, msr_index, data)) {
@@ -1163,42 +1159,6 @@ static bool nested_msr_store_list_has_msr(struct kvm_vcpu *vcpu, u32 msr_index)return false; } -static void prepare_vmx_msr_autostore_list (struct kvm_vcpu *vcpu, -u32 msr_index) -{ -struct vcpu_vmx *vmx =to_vmx (vcpu); -struct vmx_msrs *autostore = &vmx->msr_autostore; -bool in_vmcs12_store_list; -int msr_autostore_slot; -bool in_autostore_list; -int last; - - msr_autostore_slot =vmx_find_loadstore_msr_slot (autostore, msr_index); - in_autostore_list = msr_autostore_slot >=0 ; - in_vmcs12_store_list =nested_msr_store_list_has_msr (vcpu, msr_index); - -if (in_vmcs12_store_list && !in_autostore_list) { -if (autostore->nr ==MAX_NR_LOADSTORE_MSRS ) { -/* -* Emulated VMEntry does not fail here. Instead a less -* accurate value will be returned by -* nested_vmx_get_vmexit_msr_value() by reading KVM's -* internal MSR state instead of reading the value from -* the vmcs02 VMExit MSR-store area. -*/ -pr_warn_ratelimited ( -"Not enough msr entries in msr_autostore. Can't add msr %x\n" , - msr_index); -return ; - } - last = autostore->nr++; - autostore->val[last].index = msr_index; - }else if (!in_vmcs12_store_list && in_autostore_list) { - last = --autostore->nr; - autostore->val[msr_autostore_slot] = autostore->val[last]; - } -} -/* * Load guest's/host's cr3 at nested entry/exit. @nested_ept is true if we are * emulating VM-Entry into a guest with EPT enabled. On failure, the expected
@@ -2699,12 +2659,25 @@ static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) }/* -* Make sure the msr_autostore list is up to date before we set the -* count in the vmcs02. +* If vmcs12 is configured to save TSC on exit via the auto-store list, +* append the MSR to vmcs02's auto-store list so that KVM effectively +* reads TSC at the time of VM-Exit from L2. The saved value will be +* propagated to vmcs12's list on nested VM-Exit. +* +* Don't increment the number of MSRs in the vCPU structure, as saving +* TSC is specific to this particular incarnation of vmcb02, i.e. must +* not bleed into vmcs01. */ -prepare_vmx_msr_autostore_list (&vmx->vcpu,MSR_IA32_TSC ); +if (nested_msr_store_list_has_msr (&vmx->vcpu,MSR_IA32_TSC ) && + !WARN_ON_ONCE (vmx->msr_autostore.nr >=ARRAY_SIZE (vmx->msr_autostore.val))) { + vmx->nested.tsc_autostore_slot = vmx->msr_autostore.nr; + vmx->msr_autostore.val[vmx->msr_autostore.nr].index =MSR_IA32_TSC ; -vmcs_write32 (VM_EXIT_MSR_STORE_COUNT , vmx->msr_autostore.nr); +vmcs_write32 (VM_EXIT_MSR_STORE_COUNT , vmx->msr_autostore.nr +1 ); + }else { + vmx->nested.tsc_autostore_slot =-1 ; +vmcs_write32 (VM_EXIT_MSR_STORE_COUNT , vmx->msr_autostore.nr); + }vmcs_write32 (VM_EXIT_MSR_LOAD_COUNT , vmx->msr_autoload.host.nr);vmcs_write32 (VM_ENTRY_MSR_LOAD_COUNT , vmx->msr_autoload.guest.nr);
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -1029,7 +1029,7 @@ static __always_inline void clear_atomic_switch_msr_special(struct vcpu_vmx *vmxvm_exit_controls_clearbit (vmx, exit); } -int vmx_find_loadstore_msr_slot (struct vmx_msrs *m,u32 msr) +static int vmx_find_loadstore_msr_slot (struct vmx_msrs *m,u32 msr) {unsigned int i;
arch/x86/kvm/vmx/vmx.h +1 -1
@@ -191,6 +191,7 @@ struct nested_vmx {u16 vpid02;u16 last_vpid; +int tsc_autostore_slot;struct nested_vmx_msrs msrs;/* SMM related state */
@@ -383,7 +384,6 @@ void vmx_spec_ctrl_restore_host(struct vcpu_vmx *vmx, unsigned int flags);unsigned int __vmx_vcpu_run_flags (struct vcpu_vmx *vmx);bool __vmx_vcpu_run (struct vcpu_vmx *vmx,unsigned long *regs,unsigned int flags); -int vmx_find_loadstore_msr_slot (struct vmx_msrs *m,u32 msr);void vmx_ept_load_pdptrs (struct kvm_vcpu *vcpu);void vmx_set_intercept_for_msr (struct kvm_vcpu *vcpu,u32 msr,int type,bool set);
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH v6 37/44 KVM: VMX: Dedup code for removing MSR from VMCS's auto-load list
In reply to Sean ChristophersonAdd a helper to remove an MSR from an auto-{load,store} list to dedup the msr_autoload code, and in anticipation of adding similar functionality for msr_autostore.
No functional change intended.
--- arch/x86/kvm/vmx/vmx.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-)
arch/x86/kvm/vmx/vmx.c +16 -15
@@ -1040,9 +1040,22 @@ static int vmx_find_loadstore_msr_slot(struct vmx_msrs *m, u32 msr)return -ENOENT ; } +static void vmx_remove_auto_msr (struct vmx_msrs *m,u32 msr, +unsigned long vmcs_count_field) +{ +int i; + + i =vmx_find_loadstore_msr_slot (m, msr); +if (i <0 ) +return ; + + --m->nr; + m->val[i] = m->val[m->nr]; +vmcs_write32 (vmcs_count_field, m->nr); +} +static void clear_atomic_switch_msr (struct vcpu_vmx *vmx,unsigned msr) { -int i;struct msr_autoload *m = &vmx->msr_autoload;switch (msr) {
@@ -1063,21 +1076,9 @@ static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) }break ; } - i =vmx_find_loadstore_msr_slot (&m->guest, msr); -if (i <0 ) - goto skip_guest; - --m->guest.nr; - m->guest.val[i] = m->guest.val[m->guest.nr]; -vmcs_write32 (VM_ENTRY_MSR_LOAD_COUNT , m->guest.nr); -skip_guest: - i =vmx_find_loadstore_msr_slot (&m->host, msr); -if (i <0 ) -return ; - - --m->host.nr; - m->host.val[i] = m->host.val[m->host.nr]; -vmcs_write32 (VM_EXIT_MSR_LOAD_COUNT , m->host.nr); +vmx_remove_auto_msr (&m->guest, msr,VM_ENTRY_MSR_LOAD_COUNT ); +vmx_remove_auto_msr (&m->host, msr,VM_EXIT_MSR_LOAD_COUNT ); } static __always_inline void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Re: [PATCH v6 37/44] KVM: VMX: Dedup code for removing MSR from VMCS's auto-load list
Quoted 20 lines · Sean Christopherson
Add a helper to remove an MSR from an auto-{load,store} list to dedup the
msr_autoloadcode, and in anticipation of adding similar functionality formsr_autostore.No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-)arch/x86/kvm/vmx/vmx.c +1
-0@@ -1040,9 +1040,22 @@ static int vmx_find_loadstore_msr_slot(struct vmx_msrs *m, u32 msr)return -ENOENT ; } +static void vmx_remove_auto_msr(struct vmx_msrs *m, u32 msr,
arch/x86/kvm/vmx/vmx.c static int vmx_find_loadstore_msr_slot(struct vmx_msrs *m, u32 msr) · line 1053
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +10
-0+ unsigned long vmcs_count_field) +{ +int i; + + i =vmx_find_loadstore_msr_slot (m, msr); +if (i <0 ) +return ; + + --m->nr; + m->val[i] = m->val[m->nr];
Sometimes the order of MSR writing does matter, e.g., PERF_GLOBAL_CTRL MSR should be written at last after all PMU MSR writing. So directly moving the last MSR entry into cleared one could break the MSR writing sequence and may cause issue in theory.
I know this won't really cause issue since currently vPMU won't use the MSR auto-load feature to save any PMU MSR, but it's still unsafe for future uses.
I'm not sure if it's worthy to do the strict MSR entry shift right now. Perhaps we could add a message to warn users at least.
Thanks.
Quoted 33 lines
arch/x86/kvm/vmx/vmx.c +5
-15+vmcs_write32 (vmcs_count_field, m->nr); +} +static void clear_atomic_switch_msr (struct vcpu_vmx *vmx,unsigned msr) { -int i;struct msr_autoload *m = &vmx->msr_autoload;switch (msr) {@@ -1063,21 +1076,9 @@ static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) }break ; } - i =vmx_find_loadstore_msr_slot (&m->guest, msr); -if (i <0 ) - goto skip_guest; - --m->guest.nr; - m->guest.val[i] = m->guest.val[m->guest.nr]; -vmcs_write32 (VM_ENTRY_MSR_LOAD_COUNT , m->guest.nr); -skip_guest: - i =vmx_find_loadstore_msr_slot (&m->host, msr); -if (i <0 ) -return ; - - --m->host.nr; - m->host.val[i] = m->host.val[m->host.nr]; -vmcs_write32 (VM_EXIT_MSR_LOAD_COUNT , m->host.nr); +vmx_remove_auto_msr (&m->guest, msr,VM_ENTRY_MSR_LOAD_COUNT ); +vmx_remove_auto_msr (&m->host, msr,VM_EXIT_MSR_LOAD_COUNT ); } static __always_inline void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
Sean Christopherson
maintainerRe: [PATCH v6 37/44] KVM: VMX: Dedup code for removing MSR from VMCS's auto-load list
Quoted 21 lines · Dapeng Mi
Quoted 19 lines · Sean Christopherson
Add a helper to remove an MSR from an auto-{load,store} list to dedup the
msr_autoloadcode, and in anticipation of adding similar functionality formsr_autostore.No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-)arch/x86/kvm/vmx/vmx.c +0
-0@@ -1040,9 +1040,22 @@ static int vmx_find_loadstore_msr_slot(struct vmx_msrs *m, u32 msr)return -ENOENT ; }
Quoted 14 lines
arch/x86/kvm/vmx/vmx.c static int vmx_find_loadstore_msr_slot(struct vmx_msrs *m, u32 msr) · line 1053
Quoted 11 lines
arch/x86/kvm/vmx/vmx.c +11
-0+static void vmx_remove_auto_msr (struct vmx_msrs *m,u32 msr, +unsigned long vmcs_count_field) +{ +int i; + + i =vmx_find_loadstore_msr_slot (m, msr); +if (i <0 ) +return ; + + --m->nr; + m->val[i] = m->val[m->nr];Sometimes the order of MSR writing does matter, e.g.,
PERF_GLOBAL_CTRLMSR should be written at last after all PMU MSR writing.
Hmm, no. _If_ KVM were writing event selectors using the auto-load lists, then KVM would need to bookend the event selector MSRs with PERF_GLOBAL_CTRL=0 and PERF_GLOBAL_CTRL=<new context (guest vs. host)>. E.g. so that guest PMC counts aren't polluted with host events, and vice versa.
As things stand today, the only other MSRs are PEBS and the DS area configuration stuff, and kinda to my earlier point, KVM pre-zeroes MSR_IA32_PEBS_ENABLE as part of add_atomic_switch_msr() to ensure a quiescent period before VM-Enter.
Heh, and writing PERF_GLOBAL_CTRL last for that sequence might actually be problematic. E.g. load host PEBS with guest PERF_GLOBAL_CTRL active.
Anyways, I agree that this might be brittle, but this is all pre-existing behavior so I don't want to tackle that here unless it's absolutely necessary.
Or wait, by "writing" do you mean "writing MSRs to memory", as opposed to "writing values to MSRs"? Regardless, I think my answer is the same: this isn't a problem today, so I'd prefer to not shuffle the ordering unless it's absolutely necessary.
Quoted 9 lines
So directly moving the last MSR entry into cleared one could break the MSR writing sequence and may cause issue in theory.
I know this won't really cause issue since currently vPMU won't use the MSR auto-load feature to save any PMU MSR, but it's still unsafe for future uses.
I'm not sure if it's worthy to do the strict MSR entry shift right now.
Perhaps we could add a message to warn users at least.
Hmm, yeah, but I'm not entirely sure where/how best to document this. Because it's not just that vmx_remove_auto_msr() arbitrarily maniuplates the order, e.g. multiple calls to vmx_add_auto_msr() aren't guaranteed to provide ordering because one or more MSRs may already be in the list. And the "special" MSRs that can be switched via dedicated VMCS fields further muddy the waters.
So I'm tempted to not add a comment to the helpers, or even the struct fields, because unfortunately it's largely a "Here be dragons!" type warning. :-/
Re: [PATCH v6 37/44] KVM: VMX: Dedup code for removing MSR from VMCS's auto-load list
Quoted 43 lines · Sean Christopherson
Quoted 20 lines · Dapeng Mi
Quoted 19 lines · Sean Christopherson
Add a helper to remove an MSR from an auto-{load,store} list to dedup the
msr_autoloadcode, and in anticipation of adding similar functionality formsr_autostore.No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-)arch/x86/kvm/vmx/vmx.c +0
-0@@ -1040,9 +1040,22 @@ static int vmx_find_loadstore_msr_slot(struct vmx_msrs *m, u32 msr)return -ENOENT ; }Quoted 13 lines
arch/x86/kvm/vmx/vmx.c static int vmx_find_loadstore_msr_slot(struct vmx_msrs *m, u32 msr) · line 1053
Quoted 11 lines
arch/x86/kvm/vmx/vmx.c +11
-0+static void vmx_remove_auto_msr (struct vmx_msrs *m,u32 msr, +unsigned long vmcs_count_field) +{ +int i; + + i =vmx_find_loadstore_msr_slot (m, msr); +if (i <0 ) +return ; + + --m->nr; + m->val[i] = m->val[m->nr];Sometimes the order of MSR writing does matter, e.g.,
PERF_GLOBAL_CTRLMSR should be written at last after all PMU MSR writing.Hmm, no. _If_ KVM were writing event selectors using the auto-load lists, then KVM would need to bookend the event selector MSRs with PERF_GLOBAL_CTRL=0 and PERF_GLOBAL_CTRL=<new context (guest vs. host)>. E.g. so that guest PMC counts aren't polluted with host events, and vice versa.
As things stand today, the only other MSRs are PEBS and the DS area configuration stuff, and kinda to my earlier point, KVM pre-zeroes
MSR_IA32_PEBS_ENABLEas part ofadd_atomic_switch_msr()to ensure a quiescent period before VM-Enter.
Quoted 5 lines
Heh, and writing
PERF_GLOBAL_CTRLlast for that sequence might actually be problematic. E.g. load host PEBS with guestPERF_GLOBAL_CTRLactive.Anyways, I agree that this might be brittle, but this is all pre-existing behavior so I don't want to tackle that here unless it's absolutely necessary.
Yeah, this won't cause any real issue for current code.
Quoted 4 lines
Or wait, by "writing" do you mean "writing MSRs to memory", as opposed to "writing values to MSRs"? Regardless, I think my answer is the same: this isn't a problem today, so I'd prefer to not shuffle the ordering unless it's absolutely necessary.
Actually both. As SDM suggests, current any manipulation for PMU MSRs, the below order should be followed.
- disable all PMU counters by writing PERF_GLOBAL_CTRL to 0.
- manipulate PMU MSRs.
- Enable the needed PMU counters by writing PERF_GLOBAL_CTRL.
So there is a implicit order on the MSR writing in fact.
For KVM, if MSR auto-load list feature is used to automatically restore the host/guest MSRs, KVM needs to follow the above order to fill the MSR auto-load list and then expect these MSRs are written by VMX in the designed order.
But since it won't cause any real issue now, it's fine for me to handle it until there is a real necessity.
Quoted 10 lines
Quoted 10 lines
So directly moving the last MSR entry into cleared one could break the MSR writing sequence and may cause issue in theory.
I know this won't really cause issue since currently vPMU won't use the MSR auto-load feature to save any PMU MSR, but it's still unsafe for future uses.
I'm not sure if it's worthy to do the strict MSR entry shift right now.
Perhaps we could add a message to warn users at least.
Quoted 8 lines
Hmm, yeah, but I'm not entirely sure where/how best to document this. Because it's not just that
vmx_remove_auto_msr()arbitrarily maniuplates the order, e.g. multiple calls tovmx_add_auto_msr()aren't guaranteed to provide ordering because one or more MSRs may already be in the list. And the "special" MSRs that can be switched via dedicated VMCS fields further muddy the waters.So I'm tempted to not add a comment to the helpers, or even the struct fields, because unfortunately it's largely a "Here be dragons!" type warning. :-/
I see. Thanks.
Sean Christopherson
maintainerPATCH v6 38/44 KVM: VMX: Drop unused @entry_only param from add_atomic_switch_msr()
In reply to Sean ChristophersonDrop the "on VM-Enter only" parameter from add_atomic_switch_msr() as it is no longer used, and for all intents and purposes was never used. The functionality was added, under embargo, by commit 989e3992d2ec ("x86/KVM/VMX: Extend add_atomic_switch_msr() to allow VMENTER only MSRs"), and then ripped out by commit 2f055947ae5e ("x86/kvm: Drop L1TF MSR list approach") just a few commits later.
2f055947ae5e x86/kvm: Drop L1TF MSR list approach
72c6d2db64fa x86/litf: Introduce vmx status variable
215af5499d9e cpu/hotplug: Online siblings when SMT control is turned on 390d975e0c4e x86/KVM/VMX: Use MSR save list for IA32_FLUSH_CMD if required 989e3992d2ec x86/KVM/VMX: Extend add_atomic_switch_msr() to allow VMENTER only MSRs
Furthermore, it's extremely unlikely KVM will ever _need_ to load an MSR value via the auto-load lists only on VM-Enter. MSRs writes via the lists aren't optimized in any way, and so the only reason to use the lists instead of a WRMSR are for cases where the MSR _must_ be load atomically with respect to VM-Enter (and/or VM-Exit). While one could argue that command MSRs, e.g. IA32_FLUSH_CMD, "need" to be done exact at VM-Enter, in practice doing such flushes within a few instructons of VM-Enter is more than sufficient.
Note, the shortlog and changelog for commit 390d975e0c4e ("x86/KVM/VMX: Use MSR save list for IA32_FLUSH_CMD if required") are misleading and wrong. That commit added MSR_IA32_FLUSH_CMD to the VM-Enter _load_ list, not the VM-Enter save list (which doesn't exist, only VM-Exit has a store/save list).
--- arch/x86/kvm/vmx/vmx.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)
arch/x86/kvm/vmx/vmx.c +4 -9
@@ -1094,7 +1094,7 @@ static __always_inline void add_atomic_switch_msr_special(struct vcpu_vmx *vmx, }static void add_atomic_switch_msr (struct vcpu_vmx *vmx,unsigned msr, -u64 guest_val,u64 host_val,bool entry_only) +u64 guest_val,u64 host_val) {int i, j =0 ;struct msr_autoload *m = &vmx->msr_autoload;
@@ -1132,8 +1132,7 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, } i =vmx_find_loadstore_msr_slot (&m->guest, msr); -if (!entry_only) - j =vmx_find_loadstore_msr_slot (&m->host, msr); + j =vmx_find_loadstore_msr_slot (&m->host, msr);if ((i <0 && m->guest.nr ==MAX_NR_LOADSTORE_MSRS ) || (j <0 && m->host.nr ==MAX_NR_LOADSTORE_MSRS )) {
@@ -1148,9 +1147,6 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, m->guest.val[i].index = msr; m->guest.val[i].value = guest_val; -if (entry_only) -return ; -if (j <0 ) { j = m->host.nr++;vmcs_write32 (VM_EXIT_MSR_LOAD_COUNT , m->host.nr);
@@ -1190,8 +1186,7 @@ static bool update_transition_efer(struct vcpu_vmx *vmx)if (!(guest_efer &EFER_LMA )) guest_efer &= ~EFER_LME ;if (guest_efer != kvm_host.efer) -add_atomic_switch_msr (vmx,MSR_EFER , - guest_efer, kvm_host.efer, false); +add_atomic_switch_msr (vmx,MSR_EFER , guest_efer, kvm_host.efer);else clear_atomic_switch_msr (vmx,MSR_EFER );return false;
@@ -7350,7 +7345,7 @@ static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)clear_atomic_switch_msr (vmx, msrs[i].msr); else add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, - msrs[i].host, false); + msrs[i].host); }static void vmx_update_hv_timer (struct kvm_vcpu *vcpu,bool force_immediate_exit)
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Re: [PATCH v6 38/44] KVM: VMX: Drop unused @entry_only param from add_atomic_switch_msr()
Quoted 77 lines · Sean Christopherson
Drop the "on VM-Enter only" parameter from
add_atomic_switch_msr()as it is no longer used, and for all intents and purposes was never used. The functionality was added, under embargo, by commit989e3992d2ec("x86/KVM/VMX: Extendadd_atomic_switch_msr()to allow VMENTER only MSRs"), and then ripped out by commit2f055947ae5e("x86/kvm: Drop L1TF MSR list approach") just a few commits later.
2f055947ae5ex86/kvm: Drop L1TF MSR list approach
72c6d2db64fax86/litf: Introduce vmx status variable
215af5499d9ecpu/hotplug: Online siblings when SMT control is turned on390d975e0c4ex86/KVM/VMX: Use MSR save list forIA32_FLUSH_CMDif required989e3992d2ecx86/KVM/VMX: Extendadd_atomic_switch_msr()to allow VMENTER only MSRsFurthermore, it's extremely unlikely KVM will ever _need_ to load an MSR value via the auto-load lists only on VM-Enter. MSRs writes via the lists aren't optimized in any way, and so the only reason to use the lists instead of a WRMSR are for cases where the MSR _must_ be load atomically with respect to VM-Enter (and/or VM-Exit). While one could argue that command MSRs, e.g.
IA32_FLUSH_CMD, "need" to be done exact at VM-Enter, in practice doing such flushes within a few instructons of VM-Enter is more than sufficient.Note, the shortlog and changelog for commit
390d975e0c4e("x86/KVM/VMX: Use MSR save list forIA32_FLUSH_CMDif required") are misleading and wrong. That commit addedMSR_IA32_FLUSH_CMDto the VM-Enter _load_ list, not the VM-Enter save list (which doesn't exist, only VM-Exit has a store/save list).Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)arch/x86/kvm/vmx/vmx.c +3
-8@@ -1094,7 +1094,7 @@ static __always_inline void add_atomic_switch_msr_special(struct vcpu_vmx *vmx, }static void add_atomic_switch_msr (struct vcpu_vmx *vmx,unsigned msr, -u64 guest_val,u64 host_val,bool entry_only) +u64 guest_val,u64 host_val) {int i, j =0 ;struct msr_autoload *m = &vmx->msr_autoload;@@ -1132,8 +1132,7 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, } i =vmx_find_loadstore_msr_slot (&m->guest, msr); -if (!entry_only) - j =vmx_find_loadstore_msr_slot (&m->host, msr); + j =vmx_find_loadstore_msr_slot (&m->host, msr);if ((i <0 && m->guest.nr ==MAX_NR_LOADSTORE_MSRS ) || (j <0 && m->host.nr ==MAX_NR_LOADSTORE_MSRS )) {@@ -1148,9 +1147,6 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, m->guest.val[i].index = msr; m->guest.val[i].value = guest_val; -if (entry_only) -return ; -if (j <0 ) { j = m->host.nr++;vmcs_write32 (VM_EXIT_MSR_LOAD_COUNT , m->host.nr);@@ -1190,8 +1186,7 @@ static bool update_transition_efer(struct vcpu_vmx *vmx)if (!(guest_efer &EFER_LMA )) guest_efer &= ~EFER_LME ;if (guest_efer != kvm_host.efer) -add_atomic_switch_msr (vmx,MSR_EFER , - guest_efer, kvm_host.efer, false); +add_atomic_switch_msr (vmx,MSR_EFER , guest_efer, kvm_host.efer);else clear_atomic_switch_msr (vmx,MSR_EFER );return false;
arch/x86/kvm/vmx/vmx.c static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) · line 7351
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +1
-1@@ -7350,7 +7345,7 @@ static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)clear_atomic_switch_msr (vmx, msrs[i].msr); else add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, - msrs[i].host, false); + msrs[i].host); }static void vmx_update_hv_timer (struct kvm_vcpu *vcpu,bool force_immediate_exit)
LGTM.
Sean Christopherson
maintainerPATCH v6 39/44 KVM: VMX: Bug the VM if either MSR auto-load list is full
In reply to Sean ChristophersonWARN and bug the VM if either MSR auto-load list is full when adding an MSR to the lists, as the set of MSRs that KVM loads via the lists is finite and entirely KVM controlled, i.e. overflowing the lists shouldn't be possible in a fully released version of KVM. Terminate the VM as the core KVM infrastructure has no insight as to _why_ an MSR is being added to the list, and failure to load an MSR on VM-Enter and/or VM-Exit could be fatal to the host. E.g. running the host with a guest-controlled PEBS MSR could generate unexpected writes to the DS buffer and crash the host.
--- arch/x86/kvm/vmx/vmx.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/vmx.c +4 -5
@@ -1098,6 +1098,7 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, {int i, j =0 ;struct msr_autoload *m = &vmx->msr_autoload; +struct kvm *kvm = vmx->vcpu.kvm;switch (msr) {case MSR_EFER :
@@ -1134,12 +1135,10 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, i =vmx_find_loadstore_msr_slot (&m->guest, msr); j =vmx_find_loadstore_msr_slot (&m->host, msr); -if ((i <0 && m->guest.nr ==MAX_NR_LOADSTORE_MSRS ) || - (j <0 && m->host.nr ==MAX_NR_LOADSTORE_MSRS )) { -printk_once (KERN_WARNING "Not enough msr switch entries. " -"Can't add msr %x\n" , msr); +if (KVM_BUG_ON (i <0 && m->guest.nr ==MAX_NR_LOADSTORE_MSRS , kvm) || +KVM_BUG_ON (j <0 && m->host.nr ==MAX_NR_LOADSTORE_MSRS , kvm))return ; - } +if (i <0 ) { i = m->guest.nr++;vmcs_write32 (VM_ENTRY_MSR_LOAD_COUNT , m->guest.nr);
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Re: [PATCH v6 39/44] KVM: VMX: Bug the VM if either MSR auto-load list is full
Quoted 32 lines · Sean Christopherson
WARN and bug the VM if either MSR auto-load list is full when adding an MSR to the lists, as the set of MSRs that KVM loads via the lists is finite and entirely KVM controlled, i.e. overflowing the lists shouldn't be possible in a fully released version of KVM. Terminate the VM as the core KVM infrastructure has no insight as to _why_ an MSR is being added to the list, and failure to load an MSR on VM-Enter and/or VM-Exit could be fatal to the host. E.g. running the host with a guest-controlled PEBS MSR could generate unexpected writes to the DS buffer and crash the host.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)arch/x86/kvm/vmx/vmx.c +1
-2@@ -1098,6 +1098,7 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, {int i, j =0 ;struct msr_autoload *m = &vmx->msr_autoload; +struct kvm *kvm = vmx->vcpu.kvm;switch (msr) {case MSR_EFER :@@ -1134,12 +1135,10 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, i =vmx_find_loadstore_msr_slot (&m->guest, msr); j =vmx_find_loadstore_msr_slot (&m->host, msr); -if ((i <0 && m->guest.nr ==MAX_NR_LOADSTORE_MSRS ) || - (j <0 && m->host.nr ==MAX_NR_LOADSTORE_MSRS )) {
arch/x86/kvm/vmx/vmx.c static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, · line 1144
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +3
-3-printk_once (KERN_WARNING "Not enough msr switch entries. " -"Can't add msr %x\n" , msr); +if (KVM_BUG_ON (i <0 && m->guest.nr ==MAX_NR_LOADSTORE_MSRS , kvm) || +KVM_BUG_ON (j <0 && m->host.nr ==MAX_NR_LOADSTORE_MSRS , kvm))return ; - } +if (i <0 ) { i = m->guest.nr++;vmcs_write32 (VM_ENTRY_MSR_LOAD_COUNT , m->guest.nr);
Re: [PATCH v6 39/44] KVM: VMX: Bug the VM if either MSR auto-load list is full
In reply to Sean ChristophersonQuoted 27 lines · Sean Christopherson
WARN and bug the VM if either MSR auto-load list is full when adding an MSR to the lists, as the set of MSRs that KVM loads via the lists is finite and entirely KVM controlled, i.e. overflowing the lists shouldn't be possible in a fully released version of KVM. Terminate the VM as the core KVM infrastructure has no insight as to _why_ an MSR is being added to the list, and failure to load an MSR on VM-Enter and/or VM-Exit could be fatal to the host. E.g. running the host with a guest-controlled PEBS MSR could generate unexpected writes to the DS buffer and crash the host.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)arch/x86/kvm/vmx/vmx.c +1
-0@@ -1098,6 +1098,7 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, {int i, j =0 ;struct msr_autoload *m = &vmx->msr_autoload; +struct kvm *kvm = vmx->vcpu.kvm;switch (msr) {case MSR_EFER :
arch/x86/kvm/vmx/vmx.c static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, · line 1139
Quoted 9 lines
arch/x86/kvm/vmx/vmx.c +2
-4@@ -1134,12 +1135,10 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, i =vmx_find_loadstore_msr_slot (&m->guest, msr); j =vmx_find_loadstore_msr_slot (&m->host, msr); -if ((i <0 && m->guest.nr ==MAX_NR_LOADSTORE_MSRS ) || - (j <0 && m->host.nr ==MAX_NR_LOADSTORE_MSRS )) { -printk_once (KERN_WARNING "Not enough msr switch entries. " -"Can't add msr %x\n" , msr); +if (KVM_BUG_ON (i <0 && m->guest.nr ==MAX_NR_LOADSTORE_MSRS , kvm) || +KVM_BUG_ON (j <0 && m->host.nr ==MAX_NR_LOADSTORE_MSRS , kvm))
nit: Remove one extra space before "m->host.nr".
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +1
-1return ; - } +if (i <0 ) { i = m->guest.nr++;vmcs_write32 (VM_ENTRY_MSR_LOAD_COUNT , m->guest.nr);
Sean Christopherson
maintainerRe: [PATCH v6 39/44] KVM: VMX: Bug the VM if either MSR auto-load list is full
Quoted 17 lines · Dapeng Mi
Quoted 16 lines · Sean Christopherson
arch/x86/kvm/vmx/vmx.c +1
-0@@ -1098,6 +1098,7 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, {int i, j =0 ;struct msr_autoload *m = &vmx->msr_autoload; +struct kvm *kvm = vmx->vcpu.kvm;switch (msr) {case MSR_EFER :@@ -1134,12 +1135,10 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, i =vmx_find_loadstore_msr_slot (&m->guest, msr); j =vmx_find_loadstore_msr_slot (&m->host, msr);
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, · line 1139
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +2
-4-if ((i <0 && m->guest.nr ==MAX_NR_LOADSTORE_MSRS ) || - (j <0 && m->host.nr ==MAX_NR_LOADSTORE_MSRS )) { -printk_once (KERN_WARNING "Not enough msr switch entries. " -"Can't add msr %x\n" , msr); +if (KVM_BUG_ON (i <0 && m->guest.nr ==MAX_NR_LOADSTORE_MSRS , kvm) || +KVM_BUG_ON (j <0 && m->host.nr ==MAX_NR_LOADSTORE_MSRS , kvm))nit: Remove one extra space before "
m->host.nr".
Oh, that's intentional, so that the rest of the line is aligned with the "guest" line above.
Re: [PATCH v6 39/44] KVM: VMX: Bug the VM if either MSR auto-load list is full
Quoted 25 lines · Sean Christopherson
Quoted 17 lines · Dapeng Mi
Quoted 16 lines · Sean Christopherson
arch/x86/kvm/vmx/vmx.c +1
-0@@ -1098,6 +1098,7 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, {int i, j =0 ;struct msr_autoload *m = &vmx->msr_autoload; +struct kvm *kvm = vmx->vcpu.kvm;switch (msr) {case MSR_EFER :@@ -1134,12 +1135,10 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, i =vmx_find_loadstore_msr_slot (&m->guest, msr); j =vmx_find_loadstore_msr_slot (&m->host, msr);Quoted 7 lines
arch/x86/kvm/vmx/vmx.c static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, · line 1139
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +2
-4-if ((i <0 && m->guest.nr ==MAX_NR_LOADSTORE_MSRS ) || - (j <0 && m->host.nr ==MAX_NR_LOADSTORE_MSRS )) { -printk_once (KERN_WARNING "Not enough msr switch entries. " -"Can't add msr %x\n" , msr); +if (KVM_BUG_ON (i <0 && m->guest.nr ==MAX_NR_LOADSTORE_MSRS , kvm) || +KVM_BUG_ON (j <0 && m->host.nr ==MAX_NR_LOADSTORE_MSRS , kvm))nit: Remove one extra space before "
m->host.nr".
Quoted 2 lines
Oh, that's intentional, so that the rest of the line is aligned with the "guest" line above.
Good to know. Thanks.
Sean Christopherson
maintainerPATCH v6 40/44 KVM: VMX: Set MSR index auto-load entry if and only if entry is "new"
In reply to Sean ChristophersonWhen adding an MSR to the auto-load lists, update the MSR index in the list entry if and only if a new entry is being inserted, as 'i' can only be non-negative if vmx_find_loadstore_msr_slot() found an entry with the MSR's index. Unnecessarily setting the index is benign, but it makes it harder to see that updating the value is necessary even when an existing entry for the MSR was found.
No functional change intended.
--- arch/x86/kvm/vmx/vmx.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/vmx.c +2 -2
@@ -1141,16 +1141,16 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,if (i <0 ) { i = m->guest.nr++; + m->guest.val[i].index = msr;vmcs_write32 (VM_ENTRY_MSR_LOAD_COUNT , m->guest.nr); } - m->guest.val[i].index = msr; m->guest.val[i].value = guest_val;if (j <0 ) { j = m->host.nr++; + m->host.val[j].index = msr;vmcs_write32 (VM_EXIT_MSR_LOAD_COUNT , m->host.nr); } - m->host.val[j].index = msr; m->host.val[j].value = host_val; }
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Re: [PATCH v6 40/44] KVM: VMX: Set MSR index auto-load entry if and only if entry is "new"
Quoted 27 lines · Sean Christopherson
When adding an MSR to the auto-load lists, update the MSR index in the list entry if and only if a new entry is being inserted, as 'i' can only be non-negative if
vmx_find_loadstore_msr_slot()found an entry with the MSR's index. Unnecessarily setting the index is benign, but it makes it harder to see that updating the value is necessary even when an existing entry for the MSR was found.No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/x86/kvm/vmx/vmx.c +1
-1@@ -1141,16 +1141,16 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,if (i <0 ) { i = m->guest.nr++; + m->guest.val[i].index = msr;vmcs_write32 (VM_ENTRY_MSR_LOAD_COUNT , m->guest.nr); } - m->guest.val[i].index = msr; m->guest.val[i].value = guest_val;
arch/x86/kvm/vmx/vmx.c static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, · line 1156
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +1
-1if (j <0 ) { j = m->host.nr++; + m->host.val[j].index = msr;vmcs_write32 (VM_EXIT_MSR_LOAD_COUNT , m->host.nr); } - m->host.val[j].index = msr; m->host.val[j].value = host_val; }
Sean Christopherson
maintainerPATCH v6 41/44 KVM: VMX: Compartmentalize adding MSRs to host vs. guest auto-load list
In reply to Sean ChristophersonUndo the bundling of the "host" and "guest" MSR auto-load list logic so that the code can be deduplicated by factoring out the logic to a separate helper. Now that "list full" situations are treated as fatal to the VM, there is no need to pre-check both lists.
For all intents and purposes, this reverts the add_atomic_switch_msr() changes made by commit 3190709335dd ("x86/KVM/VMX: Separate the VMX AUTOLOAD guest/host number accounting").
--- arch/x86/kvm/vmx/vmx.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-)
arch/x86/kvm/vmx/vmx.c +12 -11
@@ -1096,9 +1096,9 @@ static __always_inline void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,static void add_atomic_switch_msr (struct vcpu_vmx *vmx,unsigned msr,u64 guest_val,u64 host_val) { -int i, j =0 ;struct msr_autoload *m = &vmx->msr_autoload;struct kvm *kvm = vmx->vcpu.kvm; +int i;switch (msr) {case MSR_EFER :
@@ -1133,25 +1133,26 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, } i =vmx_find_loadstore_msr_slot (&m->guest, msr); - j =vmx_find_loadstore_msr_slot (&m->host, msr); - -if (KVM_BUG_ON (i <0 && m->guest.nr ==MAX_NR_LOADSTORE_MSRS , kvm) || -KVM_BUG_ON (j <0 && m->host.nr ==MAX_NR_LOADSTORE_MSRS , kvm)) -return ; -if (i <0 ) { +if (KVM_BUG_ON (m->guest.nr ==MAX_NR_LOADSTORE_MSRS , kvm)) +return ; + i = m->guest.nr++; m->guest.val[i].index = msr;vmcs_write32 (VM_ENTRY_MSR_LOAD_COUNT , m->guest.nr); } m->guest.val[i].value = guest_val; -if (j <0 ) { - j = m->host.nr++; - m->host.val[j].index = msr; + i =vmx_find_loadstore_msr_slot (&m->host, msr); +if (i <0 ) { +if (KVM_BUG_ON (m->host.nr ==MAX_NR_LOADSTORE_MSRS , kvm)) +return ; + + i = m->host.nr++; + m->host.val[i].index = msr;vmcs_write32 (VM_EXIT_MSR_LOAD_COUNT , m->host.nr); } - m->host.val[j].value = host_val; + m->host.val[i].value = host_val; }static bool update_transition_efer (struct vcpu_vmx *vmx)
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Re: [PATCH v6 41/44] KVM: VMX: Compartmentalize adding MSRs to host vs. guest auto-load list
Quoted 56 lines · Sean Christopherson
Undo the bundling of the "host" and "guest" MSR auto-load list logic so that the code can be deduplicated by factoring out the logic to a separate helper. Now that "list full" situations are treated as fatal to the VM, there is no need to pre-check both lists.
For all intents and purposes, this reverts the
add_atomic_switch_msr()changes made by commit3190709335dd("x86/KVM/VMX: Separate the VMX AUTOLOAD guest/host number accounting").Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-)arch/x86/kvm/vmx/vmx.c +8
-10@@ -1096,9 +1096,9 @@ static __always_inline void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,static void add_atomic_switch_msr (struct vcpu_vmx *vmx,unsigned msr,u64 guest_val,u64 host_val) { -int i, j =0 ;struct msr_autoload *m = &vmx->msr_autoload;struct kvm *kvm = vmx->vcpu.kvm; +int i;switch (msr) {case MSR_EFER :@@ -1133,25 +1133,26 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, } i =vmx_find_loadstore_msr_slot (&m->guest, msr); - j =vmx_find_loadstore_msr_slot (&m->host, msr); - -if (KVM_BUG_ON (i <0 && m->guest.nr ==MAX_NR_LOADSTORE_MSRS , kvm) || -KVM_BUG_ON (j <0 && m->host.nr ==MAX_NR_LOADSTORE_MSRS , kvm)) -return ; -if (i <0 ) { +if (KVM_BUG_ON (m->guest.nr ==MAX_NR_LOADSTORE_MSRS , kvm)) +return ; + i = m->guest.nr++; m->guest.val[i].index = msr;vmcs_write32 (VM_ENTRY_MSR_LOAD_COUNT , m->guest.nr); } m->guest.val[i].value = guest_val; -if (j <0 ) { - j = m->host.nr++; - m->host.val[j].index = msr; + i =vmx_find_loadstore_msr_slot (&m->host, msr); +if (i <0 ) { +if (KVM_BUG_ON (m->host.nr ==MAX_NR_LOADSTORE_MSRS , kvm)) +return ;
arch/x86/kvm/vmx/vmx.c static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, · line 1158
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +4
-1+ + i = m->host.nr++; + m->host.val[i].index = msr;vmcs_write32 (VM_EXIT_MSR_LOAD_COUNT , m->host.nr); } - m->host.val[j].value = host_val; + m->host.val[i].value = host_val; }static bool update_transition_efer (struct vcpu_vmx *vmx)
Sean Christopherson
maintainerPATCH v6 42/44 KVM: VMX: Dedup code for adding MSR to VMCS's auto list
In reply to Sean ChristophersonAdd a helper to add an MSR to a VMCS's "auto" list to deduplicate the code in add_atomic_switch_msr(), and so that the functionality can be used in the future for managing the MSR auto-store list.
No functional change intended.
--- arch/x86/kvm/vmx/vmx.c | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-)
arch/x86/kvm/vmx/vmx.c +19 -22
@@ -1093,12 +1093,28 @@ static __always_inline void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,vm_exit_controls_setbit (vmx, exit); } +static void vmx_add_auto_msr (struct vmx_msrs *m,u32 msr,u64 value, +unsigned long vmcs_count_field,struct kvm *kvm) +{ +int i; + + i =vmx_find_loadstore_msr_slot (m, msr); +if (i <0 ) { +if (KVM_BUG_ON (m->nr ==MAX_NR_LOADSTORE_MSRS , kvm)) +return ; + + i = m->nr++; + m->val[i].index = msr; +vmcs_write32 (vmcs_count_field, m->nr); + } + m->val[i].value = value; +} +static void add_atomic_switch_msr (struct vcpu_vmx *vmx,unsigned msr,u64 guest_val,u64 host_val) {struct msr_autoload *m = &vmx->msr_autoload;struct kvm *kvm = vmx->vcpu.kvm; -int i;switch (msr) {case MSR_EFER :
@@ -1132,27 +1148,8 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,wrmsrq (MSR_IA32_PEBS_ENABLE ,0 ); } - i =vmx_find_loadstore_msr_slot (&m->guest, msr); -if (i <0 ) { -if (KVM_BUG_ON (m->guest.nr ==MAX_NR_LOADSTORE_MSRS , kvm)) -return ; - - i = m->guest.nr++; - m->guest.val[i].index = msr; -vmcs_write32 (VM_ENTRY_MSR_LOAD_COUNT , m->guest.nr); - } - m->guest.val[i].value = guest_val; - - i =vmx_find_loadstore_msr_slot (&m->host, msr); -if (i <0 ) { -if (KVM_BUG_ON (m->host.nr ==MAX_NR_LOADSTORE_MSRS , kvm)) -return ; - - i = m->host.nr++; - m->host.val[i].index = msr; -vmcs_write32 (VM_EXIT_MSR_LOAD_COUNT , m->host.nr); - } - m->host.val[i].value = host_val; +vmx_add_auto_msr (&m->guest, msr, guest_val,VM_ENTRY_MSR_LOAD_COUNT , kvm); +vmx_add_auto_msr (&m->guest, msr, host_val,VM_EXIT_MSR_LOAD_COUNT , kvm); }static bool update_transition_efer (struct vcpu_vmx *vmx)
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Re: [PATCH v6 42/44] KVM: VMX: Dedup code for adding MSR to VMCS's auto list
Quoted 65 lines · Sean Christopherson
Add a helper to add an MSR to a VMCS's "auto" list to deduplicate the code in
add_atomic_switch_msr(), and so that the functionality can be used in the future for managing the MSR auto-store list.No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-)arch/x86/kvm/vmx/vmx.c +17
-17@@ -1093,12 +1093,28 @@ static __always_inline void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,vm_exit_controls_setbit (vmx, exit); } +static void vmx_add_auto_msr (struct vmx_msrs *m,u32 msr,u64 value, +unsigned long vmcs_count_field,struct kvm *kvm) +{ +int i; + + i =vmx_find_loadstore_msr_slot (m, msr); +if (i <0 ) { +if (KVM_BUG_ON (m->nr ==MAX_NR_LOADSTORE_MSRS , kvm)) +return ; + + i = m->nr++; + m->val[i].index = msr; +vmcs_write32 (vmcs_count_field, m->nr); + } + m->val[i].value = value; +} +static void add_atomic_switch_msr (struct vcpu_vmx *vmx,unsigned msr,u64 guest_val,u64 host_val) {struct msr_autoload *m = &vmx->msr_autoload;struct kvm *kvm = vmx->vcpu.kvm; -int i;switch (msr) {case MSR_EFER :@@ -1132,27 +1148,8 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,wrmsrq (MSR_IA32_PEBS_ENABLE ,0 ); } - i =vmx_find_loadstore_msr_slot (&m->guest, msr); -if (i <0 ) { -if (KVM_BUG_ON (m->guest.nr ==MAX_NR_LOADSTORE_MSRS , kvm)) -return ; - - i = m->guest.nr++; - m->guest.val[i].index = msr; -vmcs_write32 (VM_ENTRY_MSR_LOAD_COUNT , m->guest.nr); - } - m->guest.val[i].value = guest_val; - - i =vmx_find_loadstore_msr_slot (&m->host, msr); -if (i <0 ) { -if (KVM_BUG_ON (m->host.nr ==MAX_NR_LOADSTORE_MSRS , kvm)) -return ; -
arch/x86/kvm/vmx/vmx.c static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, · line 1155
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +2
-5- i = m->host.nr++; - m->host.val[i].index = msr; -vmcs_write32 (VM_EXIT_MSR_LOAD_COUNT , m->host.nr); - } - m->host.val[i].value = host_val; +vmx_add_auto_msr (&m->guest, msr, guest_val,VM_ENTRY_MSR_LOAD_COUNT , kvm); +vmx_add_auto_msr (&m->guest, msr, host_val,VM_EXIT_MSR_LOAD_COUNT , kvm); }static bool update_transition_efer (struct vcpu_vmx *vmx)
Namhyung Kim
maintainerRe: [PATCH v6 42/44] KVM: VMX: Dedup code for adding MSR to VMCS's auto list
In reply to Sean ChristophersonHello,
Quoted 62 lines · Sean Christopherson
Add a helper to add an MSR to a VMCS's "auto" list to deduplicate the code in
add_atomic_switch_msr(), and so that the functionality can be used in the future for managing the MSR auto-store list.No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-)arch/x86/kvm/vmx/vmx.c +17
-14@@ -1093,12 +1093,28 @@ static __always_inline void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,vm_exit_controls_setbit (vmx, exit); } +static void vmx_add_auto_msr (struct vmx_msrs *m,u32 msr,u64 value, +unsigned long vmcs_count_field,struct kvm *kvm) +{ +int i; + + i =vmx_find_loadstore_msr_slot (m, msr); +if (i <0 ) { +if (KVM_BUG_ON (m->nr ==MAX_NR_LOADSTORE_MSRS , kvm)) +return ; + + i = m->nr++; + m->val[i].index = msr; +vmcs_write32 (vmcs_count_field, m->nr); + } + m->val[i].value = value; +} +static void add_atomic_switch_msr (struct vcpu_vmx *vmx,unsigned msr,u64 guest_val,u64 host_val) {struct msr_autoload *m = &vmx->msr_autoload;struct kvm *kvm = vmx->vcpu.kvm; -int i;switch (msr) {case MSR_EFER :@@ -1132,27 +1148,8 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,wrmsrq (MSR_IA32_PEBS_ENABLE ,0 ); } - i =vmx_find_loadstore_msr_slot (&m->guest, msr); -if (i <0 ) { -if (KVM_BUG_ON (m->guest.nr ==MAX_NR_LOADSTORE_MSRS , kvm)) -return ; - - i = m->guest.nr++; - m->guest.val[i].index = msr; -vmcs_write32 (VM_ENTRY_MSR_LOAD_COUNT , m->guest.nr); - } - m->guest.val[i].value = guest_val; - - i =vmx_find_loadstore_msr_slot (&m->host, msr); -if (i <0 ) {
arch/x86/kvm/vmx/vmx.c static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, · line 1152
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +2
-8-if (KVM_BUG_ON (m->host.nr ==MAX_NR_LOADSTORE_MSRS , kvm)) -return ; - - i = m->host.nr++; - m->host.val[i].index = msr; -vmcs_write32 (VM_EXIT_MSR_LOAD_COUNT , m->host.nr); - } - m->host.val[i].value = host_val; +vmx_add_auto_msr (&m->guest, msr, guest_val,VM_ENTRY_MSR_LOAD_COUNT , kvm); +vmx_add_auto_msr (&m->guest, msr, host_val,VM_EXIT_MSR_LOAD_COUNT , kvm);
Shouldn't it be &m->host for the host_val?
Thanks,
Namhyung
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +0
-0}static bool update_transition_efer (struct vcpu_vmx *vmx)Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerRe: [PATCH v6 42/44] KVM: VMX: Dedup code for adding MSR to VMCS's auto list
Quoted 69 lines · Namhyung Kim
Hello,
Quoted 66 lines · Sean Christopherson
Add a helper to add an MSR to a VMCS's "auto" list to deduplicate the code in
add_atomic_switch_msr(), and so that the functionality can be used in the future for managing the MSR auto-store list.No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-)arch/x86/kvm/vmx/vmx.c +17
-18@@ -1093,12 +1093,28 @@ static __always_inline void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,vm_exit_controls_setbit (vmx, exit); } +static void vmx_add_auto_msr (struct vmx_msrs *m,u32 msr,u64 value, +unsigned long vmcs_count_field,struct kvm *kvm) +{ +int i; + + i =vmx_find_loadstore_msr_slot (m, msr); +if (i <0 ) { +if (KVM_BUG_ON (m->nr ==MAX_NR_LOADSTORE_MSRS , kvm)) +return ; + + i = m->nr++; + m->val[i].index = msr; +vmcs_write32 (vmcs_count_field, m->nr); + } + m->val[i].value = value; +} +static void add_atomic_switch_msr (struct vcpu_vmx *vmx,unsigned msr,u64 guest_val,u64 host_val) {struct msr_autoload *m = &vmx->msr_autoload;struct kvm *kvm = vmx->vcpu.kvm; -int i;switch (msr) {case MSR_EFER :@@ -1132,27 +1148,8 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,wrmsrq (MSR_IA32_PEBS_ENABLE ,0 ); } - i =vmx_find_loadstore_msr_slot (&m->guest, msr); -if (i <0 ) { -if (KVM_BUG_ON (m->guest.nr ==MAX_NR_LOADSTORE_MSRS , kvm)) -return ; - - i = m->guest.nr++; - m->guest.val[i].index = msr; -vmcs_write32 (VM_ENTRY_MSR_LOAD_COUNT , m->guest.nr); - } - m->guest.val[i].value = guest_val; - - i =vmx_find_loadstore_msr_slot (&m->host, msr); -if (i <0 ) { -if (KVM_BUG_ON (m->host.nr ==MAX_NR_LOADSTORE_MSRS , kvm)) -return ; - - i = m->host.nr++;
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, · line 1152
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +2
-4- m->host.val[i].index = msr; -vmcs_write32 (VM_EXIT_MSR_LOAD_COUNT , m->host.nr); - } - m->host.val[i].value = host_val; +vmx_add_auto_msr (&m->guest, msr, guest_val,VM_ENTRY_MSR_LOAD_COUNT , kvm); +vmx_add_auto_msr (&m->guest, msr, host_val,VM_EXIT_MSR_LOAD_COUNT , kvm);Shouldn't it be
&m->hostfor thehost_val?
Ouch. Yes. How on earth did this escape testing... Ah, because in practice only MSR_IA32_PEBS_ENABLE goes through the load lists, and the VM-Entry load list will use the guest's value due to VM_ENTRY_MSR_LOAD_COUNT not covering the bad host value.
Did you happen to run into problems when using PEBS events in the host?
Regardless, do you want to send a patch? Either way, I'll figure out a way to verify the bug and the fix.
Namhyung Kim
maintainerRe: [PATCH v6 42/44] KVM: VMX: Dedup code for adding MSR to VMCS's auto list
Quoted 79 lines · Sean Christopherson
Quoted 69 lines · Namhyung Kim
Hello,
Quoted 66 lines · Sean Christopherson
Add a helper to add an MSR to a VMCS's "auto" list to deduplicate the code in
add_atomic_switch_msr(), and so that the functionality can be used in the future for managing the MSR auto-store list.No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-)arch/x86/kvm/vmx/vmx.c +17
-18@@ -1093,12 +1093,28 @@ static __always_inline void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,vm_exit_controls_setbit (vmx, exit); } +static void vmx_add_auto_msr (struct vmx_msrs *m,u32 msr,u64 value, +unsigned long vmcs_count_field,struct kvm *kvm) +{ +int i; + + i =vmx_find_loadstore_msr_slot (m, msr); +if (i <0 ) { +if (KVM_BUG_ON (m->nr ==MAX_NR_LOADSTORE_MSRS , kvm)) +return ; + + i = m->nr++; + m->val[i].index = msr; +vmcs_write32 (vmcs_count_field, m->nr); + } + m->val[i].value = value; +} +static void add_atomic_switch_msr (struct vcpu_vmx *vmx,unsigned msr,u64 guest_val,u64 host_val) {struct msr_autoload *m = &vmx->msr_autoload;struct kvm *kvm = vmx->vcpu.kvm; -int i;switch (msr) {case MSR_EFER :@@ -1132,27 +1148,8 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,wrmsrq (MSR_IA32_PEBS_ENABLE ,0 ); } - i =vmx_find_loadstore_msr_slot (&m->guest, msr); -if (i <0 ) { -if (KVM_BUG_ON (m->guest.nr ==MAX_NR_LOADSTORE_MSRS , kvm)) -return ; - - i = m->guest.nr++; - m->guest.val[i].index = msr; -vmcs_write32 (VM_ENTRY_MSR_LOAD_COUNT , m->guest.nr); - } - m->guest.val[i].value = guest_val; - - i =vmx_find_loadstore_msr_slot (&m->host, msr); -if (i <0 ) { -if (KVM_BUG_ON (m->host.nr ==MAX_NR_LOADSTORE_MSRS , kvm)) -return ; - - i = m->host.nr++;Quoted 8 lines
arch/x86/kvm/vmx/vmx.c static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, · line 1152
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +2
-4- m->host.val[i].index = msr; -vmcs_write32 (VM_EXIT_MSR_LOAD_COUNT , m->host.nr); - } - m->host.val[i].value = host_val; +vmx_add_auto_msr (&m->guest, msr, guest_val,VM_ENTRY_MSR_LOAD_COUNT , kvm); +vmx_add_auto_msr (&m->guest, msr, host_val,VM_EXIT_MSR_LOAD_COUNT , kvm);Shouldn't it be
&m->hostfor thehost_val?
Quoted 6 lines
Ouch. Yes. How on earth did this escape testing... Ah, because in practice only
MSR_IA32_PEBS_ENABLEgoes through the load lists, and the VM-Entry load list will use the guest's value due toVM_ENTRY_MSR_LOAD_COUNTnot covering the bad host value.Did you happen to run into problems when using PEBS events in the host?
No, I just found it by reading the patch.
Quoted 3 lines
Regardless, do you want to send a patch? Either way, I'll figure out a way to verify the bug and the fix.
Sure, will do.
Thanks,
Namhyung
Sean Christopherson
maintainerPATCH v6 43/44 KVM: VMX: Initialize vmcs01.VM_EXIT_MSR_STORE_ADDR with list address
In reply to Sean ChristophersonInitialize vmcs01.VM_EXIT_MSR_STORE_ADDR to point at the vCPU's msr_autostore list in anticipation of utilizing the auto-store functionality, and to harden KVM against stray reads to pfn 0 (or, in theory, a random pfn if the underlying CPU uses a complex scheme for encoding VMCS data). The MSR auto lists are supposed to be ignored if the associated COUNT VMCS field is '0', but leaving the ADDR field zero-initialized in memory is an unnecessary risk (albeit a minuscule risk) given that the cost is a single VMWRITE during vCPU creation.
--- arch/x86/kvm/vmx/vmx.c | 1 + 1 file changed, 1 insertion(+)
arch/x86/kvm/vmx/vmx.c +1 -0
@@ -4933,6 +4933,7 @@ static void init_vmcs(struct vcpu_vmx *vmx)vmcs_write64 (VM_FUNCTION_CONTROL ,0 );vmcs_write32 (VM_EXIT_MSR_STORE_COUNT ,0 ); +vmcs_write64 (VM_EXIT_MSR_STORE_ADDR ,__pa (vmx->msr_autostore.val));vmcs_write32 (VM_EXIT_MSR_LOAD_COUNT ,0 );vmcs_write64 (VM_EXIT_MSR_LOAD_ADDR ,__pa (vmx->msr_autoload.host.val));vmcs_write32 (VM_ENTRY_MSR_LOAD_COUNT ,0 );
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH v6 44/44 KVM: VMX: Add mediated PMU support for CPUs without "save perf global ctrl"
In reply to Sean ChristophersonExtend mediated PMU support for Intel CPUs without support for saving PERF_GLOBAL_CONTROL into the guest VMCS field on VM-Exit, e.g. for Skylake and its derivatives, as well as Icelake. While supporting CPUs without VM_EXIT_SAVE_IA32_PERF_GLOBAL_CTRL isn't completely trivial, it's not that complex either. And not supporting such CPUs would mean not supporting 7+ years of Intel CPUs released in the past 10 years.
On VM-Exit, immediately propagate the saved PERF_GLOBAL_CTRL to the VMCS as well as KVM's software cache so that KVM doesn't need to add full EXREG tracking of PERF_GLOBAL_CTRL. In practice, the vast majority of VM-Exits won't trigger software writes to guest PERF_GLOBAL_CTRL, so deferring the VMWRITE to the next VM-Enter would only delay the inevitable without batching/avoiding VMWRITEs.
Note! Take care to refresh VM_EXIT_MSR_STORE_COUNT on nested VM-Exit, as it's unfortunately possible that KVM could recalculate MSR intercepts while L2 is active, e.g. if userspace loads nested state and _then_ sets PERF_CAPABILITIES. Eating the VMWRITE on every nested VM-Exit is unfortunate, but that's a pre-existing problem and can/should be solved separately, e.g. modifying the number of auto-load entries while L2 is active is also uncommon on modern CPUs.
--- arch/x86/kvm/vmx/nested.c | 6 ++++- arch/x86/kvm/vmx/pmu_intel.c | 7 ----- arch/x86/kvm/vmx/vmx.c | 52 ++++++++++++++++++++++++++++++++---- 3 files changed, 52 insertions(+), 13 deletions(-)
arch/x86/kvm/vmx/nested.c +5 -1
@@ -5142,7 +5142,11 @@ void __nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,kvm_nested_vmexit_handle_ibrs (vcpu); -/* Update any VMCS fields that might have changed while L2 ran */ +/* +* Update any VMCS fields that might have changed while vmcs02 was the +* active VMCS. The tracking is per-vCPU, not per-VMCS. +*/ +vmcs_write32 (VM_EXIT_MSR_STORE_COUNT , vmx->msr_autostore.nr);vmcs_write32 (VM_EXIT_MSR_LOAD_COUNT , vmx->msr_autoload.host.nr);vmcs_write32 (VM_ENTRY_MSR_LOAD_COUNT , vmx->msr_autoload.guest.nr);vmcs_write64 (TSC_OFFSET , vcpu->arch.tsc_offset);
arch/x86/kvm/vmx/pmu_intel.c +0 -7
@@ -777,13 +777,6 @@ static bool intel_pmu_is_mediated_pmu_supported(struct x86_pmu_capability *host_if (WARN_ON_ONCE (!cpu_has_load_perf_global_ctrl ()))return false; -/* -* KVM doesn't yet support mediated PMU on CPUs without support for -* saving PERF_GLOBAL_CTRL via a dedicated VMCS field. -*/ -if (!cpu_has_save_perf_global_ctrl ()) -return false; -return true; }
arch/x86/kvm/vmx/vmx.c +47 -5
85 lines not on this page · raw message
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Re: [PATCH v6 44/44] KVM: VMX: Add mediated PMU support for CPUs without "save perf global ctrl"
Quoted 149 lines · Sean Christopherson
Extend mediated PMU support for Intel CPUs without support for saving
PERF_GLOBAL_CONTROLinto the guest VMCS field on VM-Exit, e.g. for Skylake and its derivatives, as well as Icelake. While supporting CPUs withoutVM_EXIT_SAVE_IA32_PERF_GLOBAL_CTRLisn't completely trivial, it's not that complex either. And not supporting such CPUs would mean not supporting 7+ years of Intel CPUs released in the past 10 years.On VM-Exit, immediately propagate the saved
PERF_GLOBAL_CTRLto the VMCS as well as KVM's software cache so that KVM doesn't need to add full EXREG tracking ofPERF_GLOBAL_CTRL. In practice, the vast majority of VM-Exits won't trigger software writes to guestPERF_GLOBAL_CTRL, so deferring the VMWRITE to the next VM-Enter would only delay the inevitable without batching/avoiding VMWRITEs.Note! Take care to refresh
VM_EXIT_MSR_STORE_COUNTon nested VM-Exit, as it's unfortunately possible that KVM could recalculate MSR intercepts while L2 is active, e.g. if userspace loads nested state and _then_ setsPERF_CAPABILITIES. Eating the VMWRITE on every nested VM-Exit is unfortunate, but that's a pre-existing problem and can/should be solved separately, e.g. modifying the number of auto-load entries while L2 is active is also uncommon on modern CPUs.Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/nested.c | 6 ++++- arch/x86/kvm/vmx/pmu_intel.c | 7 ----- arch/x86/kvm/vmx/vmx.c | 52 ++++++++++++++++++++++++++++++++---- 3 files changed, 52 insertions(+), 13 deletions(-)arch/x86/kvm/vmx/nested.c +5
-1@@ -5142,7 +5142,11 @@ void __nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,kvm_nested_vmexit_handle_ibrs (vcpu); -/* Update any VMCS fields that might have changed while L2 ran */ +/* +* Update any VMCS fields that might have changed while vmcs02 was the +* active VMCS. The tracking is per-vCPU, not per-VMCS. +*/ +vmcs_write32 (VM_EXIT_MSR_STORE_COUNT , vmx->msr_autostore.nr);vmcs_write32 (VM_EXIT_MSR_LOAD_COUNT , vmx->msr_autoload.host.nr);vmcs_write32 (VM_ENTRY_MSR_LOAD_COUNT , vmx->msr_autoload.guest.nr);vmcs_write64 (TSC_OFFSET , vcpu->arch.tsc_offset);arch/x86/kvm/vmx/pmu_intel.c +0
-7@@ -777,13 +777,6 @@ static bool intel_pmu_is_mediated_pmu_supported(struct x86_pmu_capability *host_if (WARN_ON_ONCE (!cpu_has_load_perf_global_ctrl ()))return false; -/* -* KVM doesn't yet support mediated PMU on CPUs without support for -* saving PERF_GLOBAL_CTRL via a dedicated VMCS field. -*/ -if (!cpu_has_save_perf_global_ctrl ()) -return false; -return true; }arch/x86/kvm/vmx/vmx.c +46
-3@@ -1204,6 +1204,17 @@ static bool update_transition_efer(struct vcpu_vmx *vmx)return true; } +static void vmx_add_autostore_msr (struct vcpu_vmx *vmx,u32 msr) +{ +vmx_add_auto_msr (&vmx->msr_autostore, msr,0 ,VM_EXIT_MSR_STORE_COUNT , + vmx->vcpu.kvm); +} + +static void vmx_remove_autostore_msr (struct vcpu_vmx *vmx,u32 msr) +{ +vmx_remove_auto_msr (&vmx->msr_autostore, msr,VM_EXIT_MSR_STORE_COUNT ); +} +#ifdef CONFIG_X86_32 /* * On 32-bit kernels, VM exits still load the FS and GS bases from the @@ -4225,6 +4236,8 @@ void pt_update_intercept_for_msr(struct kvm_vcpu *vcpu)static void vmx_recalc_pmu_msr_intercepts (struct kvm_vcpu *vcpu) { +u64 vm_exit_controls_bits =VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL | +VM_EXIT_SAVE_IA32_PERF_GLOBAL_CTRL ;bool has_mediated_pmu =kvm_vcpu_has_mediated_pmu (vcpu);struct kvm_pmu *pmu =vcpu_to_pmu (vcpu);struct vcpu_vmx *vmx =to_vmx (vcpu);@@ -4234,12 +4247,19 @@ static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)if (!enable_mediated_pmu)return ; +if (!cpu_has_save_perf_global_ctrl ()) { + vm_exit_controls_bits &= ~VM_EXIT_SAVE_IA32_PERF_GLOBAL_CTRL ; + +if (has_mediated_pmu) +vmx_add_autostore_msr (vmx,MSR_CORE_PERF_GLOBAL_CTRL ); +else +vmx_remove_autostore_msr (vmx,MSR_CORE_PERF_GLOBAL_CTRL ); + } +vm_entry_controls_changebit (vmx,VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL , has_mediated_pmu); -vm_exit_controls_changebit (vmx,VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL | -VM_EXIT_SAVE_IA32_PERF_GLOBAL_CTRL , - has_mediated_pmu); +vm_exit_controls_changebit (vmx, vm_exit_controls_bits, has_mediated_pmu);for (i =0 ; i < pmu->nr_arch_gp_counters; i++) { vmx_set_intercept_for_msr(vcpu, MSR_IA32_PERFCTR0 + i,@@ -7346,6 +7366,29 @@ static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) msrs[i].host); } +static void vmx_refresh_guest_perf_global_control (struct kvm_vcpu *vcpu) +{ +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); +struct vcpu_vmx *vmx =to_vmx (vcpu); + +if (msr_write_intercepted (vmx,MSR_CORE_PERF_GLOBAL_CTRL )) +return ; + +if (!cpu_has_save_perf_global_ctrl ()) { +int slot =vmx_find_loadstore_msr_slot (&vmx->msr_autostore, +MSR_CORE_PERF_GLOBAL_CTRL ); + +if (WARN_ON_ONCE (slot <0 )) +return ; + + pmu->global_ctrl = vmx->msr_autostore.val[slot].value; +vmcs_write64 (GUEST_IA32_PERF_GLOBAL_CTRL , pmu->global_ctrl); +return ; + } + + pmu->global_ctrl =vmcs_read64 (GUEST_IA32_PERF_GLOBAL_CTRL ); +} +static void vmx_update_hv_timer (struct kvm_vcpu *vcpu,bool force_immediate_exit) {struct vcpu_vmx *vmx =to_vmx (vcpu);
arch/x86/kvm/vmx/vmx.c fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags) · line 7680
Quoted 9 lines
arch/x86/kvm/vmx/vmx.c +1
-2@@ -7631,8 +7674,7 @@ fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags) vmx->loaded_vmcs->launched =1 ; -if (!msr_write_intercepted (vmx,MSR_CORE_PERF_GLOBAL_CTRL )) -vcpu_to_pmu (vcpu)->global_ctrl =vmcs_read64 (GUEST_IA32_PERF_GLOBAL_CTRL ); +vmx_refresh_guest_perf_global_control (vcpu);vmx_recover_nmi_blocking (vmx);vmx_complete_interrupts (vmx);
The change looks good to me, but I still have no bandwidth to test the code on Ice lake, I would go back after I finish the tests. Thanks.
Re: [PATCH v6 44/44] KVM: VMX: Add mediated PMU support for CPUs without "save perf global ctrl"
Quoted 153 lines · Dapeng
Quoted 152 lines · Sean Christopherson
Extend mediated PMU support for Intel CPUs without support for saving
PERF_GLOBAL_CONTROLinto the guest VMCS field on VM-Exit, e.g. for Skylake and its derivatives, as well as Icelake. While supporting CPUs withoutVM_EXIT_SAVE_IA32_PERF_GLOBAL_CTRLisn't completely trivial, it's not that complex either. And not supporting such CPUs would mean not supporting 7+ years of Intel CPUs released in the past 10 years.On VM-Exit, immediately propagate the saved
PERF_GLOBAL_CTRLto the VMCS as well as KVM's software cache so that KVM doesn't need to add full EXREG tracking ofPERF_GLOBAL_CTRL. In practice, the vast majority of VM-Exits won't trigger software writes to guestPERF_GLOBAL_CTRL, so deferring the VMWRITE to the next VM-Enter would only delay the inevitable without batching/avoiding VMWRITEs.Note! Take care to refresh
VM_EXIT_MSR_STORE_COUNTon nested VM-Exit, as it's unfortunately possible that KVM could recalculate MSR intercepts while L2 is active, e.g. if userspace loads nested state and _then_ setsPERF_CAPABILITIES. Eating the VMWRITE on every nested VM-Exit is unfortunate, but that's a pre-existing problem and can/should be solved separately, e.g. modifying the number of auto-load entries while L2 is active is also uncommon on modern CPUs.Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/nested.c | 6 ++++- arch/x86/kvm/vmx/pmu_intel.c | 7 ----- arch/x86/kvm/vmx/vmx.c | 52 ++++++++++++++++++++++++++++++++---- 3 files changed, 52 insertions(+), 13 deletions(-)arch/x86/kvm/vmx/nested.c +5
-1@@ -5142,7 +5142,11 @@ void __nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,kvm_nested_vmexit_handle_ibrs (vcpu); -/* Update any VMCS fields that might have changed while L2 ran */ +/* +* Update any VMCS fields that might have changed while vmcs02 was the +* active VMCS. The tracking is per-vCPU, not per-VMCS. +*/ +vmcs_write32 (VM_EXIT_MSR_STORE_COUNT , vmx->msr_autostore.nr);vmcs_write32 (VM_EXIT_MSR_LOAD_COUNT , vmx->msr_autoload.host.nr);vmcs_write32 (VM_ENTRY_MSR_LOAD_COUNT , vmx->msr_autoload.guest.nr);vmcs_write64 (TSC_OFFSET , vcpu->arch.tsc_offset);arch/x86/kvm/vmx/pmu_intel.c +0
-7@@ -777,13 +777,6 @@ static bool intel_pmu_is_mediated_pmu_supported(struct x86_pmu_capability *host_if (WARN_ON_ONCE (!cpu_has_load_perf_global_ctrl ()))return false; -/* -* KVM doesn't yet support mediated PMU on CPUs without support for -* saving PERF_GLOBAL_CTRL via a dedicated VMCS field. -*/ -if (!cpu_has_save_perf_global_ctrl ()) -return false; -return true; }arch/x86/kvm/vmx/vmx.c +46
-3@@ -1204,6 +1204,17 @@ static bool update_transition_efer(struct vcpu_vmx *vmx)return true; } +static void vmx_add_autostore_msr (struct vcpu_vmx *vmx,u32 msr) +{ +vmx_add_auto_msr (&vmx->msr_autostore, msr,0 ,VM_EXIT_MSR_STORE_COUNT , + vmx->vcpu.kvm); +} + +static void vmx_remove_autostore_msr (struct vcpu_vmx *vmx,u32 msr) +{ +vmx_remove_auto_msr (&vmx->msr_autostore, msr,VM_EXIT_MSR_STORE_COUNT ); +} +#ifdef CONFIG_X86_32 /* * On 32-bit kernels, VM exits still load the FS and GS bases from the @@ -4225,6 +4236,8 @@ void pt_update_intercept_for_msr(struct kvm_vcpu *vcpu)static void vmx_recalc_pmu_msr_intercepts (struct kvm_vcpu *vcpu) { +u64 vm_exit_controls_bits =VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL | +VM_EXIT_SAVE_IA32_PERF_GLOBAL_CTRL ;bool has_mediated_pmu =kvm_vcpu_has_mediated_pmu (vcpu);struct kvm_pmu *pmu =vcpu_to_pmu (vcpu);struct vcpu_vmx *vmx =to_vmx (vcpu);@@ -4234,12 +4247,19 @@ static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)if (!enable_mediated_pmu)return ; +if (!cpu_has_save_perf_global_ctrl ()) { + vm_exit_controls_bits &= ~VM_EXIT_SAVE_IA32_PERF_GLOBAL_CTRL ; + +if (has_mediated_pmu) +vmx_add_autostore_msr (vmx,MSR_CORE_PERF_GLOBAL_CTRL ); +else +vmx_remove_autostore_msr (vmx,MSR_CORE_PERF_GLOBAL_CTRL ); + } +vm_entry_controls_changebit (vmx,VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL , has_mediated_pmu); -vm_exit_controls_changebit (vmx,VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL | -VM_EXIT_SAVE_IA32_PERF_GLOBAL_CTRL , - has_mediated_pmu); +vm_exit_controls_changebit (vmx, vm_exit_controls_bits, has_mediated_pmu);for (i =0 ; i < pmu->nr_arch_gp_counters; i++) { vmx_set_intercept_for_msr(vcpu, MSR_IA32_PERFCTR0 + i,@@ -7346,6 +7366,29 @@ static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) msrs[i].host); } +static void vmx_refresh_guest_perf_global_control (struct kvm_vcpu *vcpu) +{ +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); +struct vcpu_vmx *vmx =to_vmx (vcpu); + +if (msr_write_intercepted (vmx,MSR_CORE_PERF_GLOBAL_CTRL )) +return ; + +if (!cpu_has_save_perf_global_ctrl ()) { +int slot =vmx_find_loadstore_msr_slot (&vmx->msr_autostore, +MSR_CORE_PERF_GLOBAL_CTRL ); + +if (WARN_ON_ONCE (slot <0 )) +return ; + + pmu->global_ctrl = vmx->msr_autostore.val[slot].value; +vmcs_write64 (GUEST_IA32_PERF_GLOBAL_CTRL , pmu->global_ctrl); +return ; + } + + pmu->global_ctrl =vmcs_read64 (GUEST_IA32_PERF_GLOBAL_CTRL ); +} +static void vmx_update_hv_timer (struct kvm_vcpu *vcpu,bool force_immediate_exit) {struct vcpu_vmx *vmx =to_vmx (vcpu);@@ -7631,8 +7674,7 @@ fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags) vmx->loaded_vmcs->launched =1 ;
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags) · line 7680
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +1
-2-if (!msr_write_intercepted (vmx,MSR_CORE_PERF_GLOBAL_CTRL )) -vcpu_to_pmu (vcpu)->global_ctrl =vmcs_read64 (GUEST_IA32_PERF_GLOBAL_CTRL ); +vmx_refresh_guest_perf_global_control (vcpu);vmx_recover_nmi_blocking (vmx);vmx_complete_interrupts (vmx);The change looks good to me, but I still have no bandwidth to test the code on Ice lake, I would go back after I finish the tests. Thanks.
Tested below test cases on Skylake and Sapphire Rapids servers for both mediated vPMU and Perf based emulated vPMU, no issue is found. Thanks.
Kselftests: pmu_counters_test/pmu_event_filter_test/vmx_pmu_caps_test
KVM-unit-tests: pmu/pmu_lbr/pmu_pebs
Quoted 2 lines
Peter Zijlstra
maintainerQuoted 20 lines · Sean Christopherson
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.
Kan Liang (7):
perf: Skippmu_ctxbased onevent_type
perf: Add genericexclude_guestsupport
perf: Add APIs to create/release mediated guest vPMUs
perf: Clean up perf ctx time
perf: Add aEVENT_GUESTflag
perf: Add APIs to load/put guest mediated PMU context perf/x86/intel: SupportPERF_PMU_CAP_MEDIATED_VPMUMingwei Zhang (3):
perf/x86/core: Plumb mediated PMU capability fromx86_pmutox86_pmu_capSandipan Das (3):
perf/x86/core: Do not set bit width for unavailable counters perf/x86/amd: SupportPERF_PMU_CAP_MEDIATED_VPMUfor AMD host
Quoted 5 lines
Sean Christopherson (19):
perf: Movesecurity_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)
That all looks to be in decent shape; lets go get this merged. There is the nit on patch 4 and I think a number of the exports want to be EXPORT_SYMBOL_FOR_KVM() instead, but we can do that on top.
I'll queue these patches after rc1.
Signature · 3 lines
kvm-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kvm-riscv
Manali Shukla
Tested-byQuoted 27 lines · Peter Zijlstra
Quoted 20 lines · Sean Christopherson
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.
Kan Liang (7):
perf: Skippmu_ctxbased onevent_type
perf: Add genericexclude_guestsupport
perf: Add APIs to create/release mediated guest vPMUs
perf: Clean up perf ctx time
perf: Add aEVENT_GUESTflag
perf: Add APIs to load/put guest mediated PMU context perf/x86/intel: SupportPERF_PMU_CAP_MEDIATED_VPMUMingwei Zhang (3):
perf/x86/core: Plumb mediated PMU capability fromx86_pmutox86_pmu_capSandipan Das (3):
perf/x86/core: Do not set bit width for unavailable counters perf/x86/amd: SupportPERF_PMU_CAP_MEDIATED_VPMUfor AMD hostQuoted 5 lines
Sean Christopherson (19):
perf: Movesecurity_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)
Quoted 5 lines
That all looks to be in decent shape; lets go get this merged. There is the nit on patch 4 and I think a number of the exports want to be
EXPORT_SYMBOL_FOR_KVM()instead, but we can do that on top.I'll queue these patches after rc1.
I have tested this series on AMD EPYC 9745 (Turin, 128-Core Processor) with 12 hours of perf_fuzzer runs across multiple configurations. No new failures were observed.
Test Platform:
--------------
Hardware: AMD EPYC 9745 128-Core Processor (Turin) Test Duration: 12 hours of perf_fuzzer per configuration
Configuration 1: enable_mediated_pmu=Y
---------------------------------------
SVM guests:
-cpu host
-cpu host,-perfmon-v2
-cpu host,-perfctr-core
SEV-ES guest:
-cpu host
SEV-SNP guest:
-cpu host
Configuration 2: enable_mediated_pmu=N
---------------------------------------
SVM guest:
-cpu EPYC-Genoa-v2,+pmu
-cpu EPYC-Genoa-v2,+pmu,-perfmon-v2
-cpu EPYC-Genoa-v2,+pmu,-perfctr-core
SEV-ES guest:
-cpu EPYC-Genoa-v2,+pmu
SEV-SNP guest:
-cpu EPYC-Genoa-v2,+pmu
KVM Unit Tests (KUT):
---------------------
Tested all combinations of:
- enable_mediated_pmu={0,1}
- force_emulation_prefix={0,1}
CPU configurations tested:
-cpu host
-cpu host,-perfmon-v2
-cpu host,-perfctr-core
All KUT tests passed as expected across all combinations.
-Manali
Sean Christopherson
maintainerQuoted 9 lines · Sean Christopherson
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.
[...]
Applied the KVM patches to kvm-x86 pmu, thanks!
[14/44] KVM: Add a simplified wrapper for registering perf callbacks
https://github.com/kvm-x86/linux/commit/4b24910c0569 [15/44] KVM: Add a simplified wrapper for registering perf callbacks [DROPPED - already merged (and then fixed)] [16/44] KVM: x86/pmu: Start stubbing in mediated PMU support https://github.com/kvm-x86/linux/commit/3e51822b2fdf [17/44] KVM: x86/pmu: Implement Intel mediated PMU requirements and constraints https://github.com/kvm-x86/linux/commit/bfee4f07d880 [18/44] KVM: x86/pmu: Implement AMD mediated PMU requirements https://github.com/kvm-x86/linux/commit/9ba0bb4ae76a [19/44] KVM: x86/pmu: Register PMI handler for mediated vPMU https://github.com/kvm-x86/linux/commit/1c4ba7286afb [20/44] KVM: x86/pmu: Disable RDPMC interception for compatible mediated vPMU https://github.com/kvm-x86/linux/commit/80624272129e [21/44] KVM: x86/pmu: Load/save GLOBAL_CTRL via entry/exit fields for mediated PMU https://github.com/kvm-x86/linux/commit/d3ba32d1ff2a [22/44] KVM: x86/pmu: Disable interception of select PMU MSRs for mediated vPMUs https://github.com/kvm-x86/linux/commit/2904df6692f4 [23/44] KVM: x86/pmu: Bypass perf checks when emulating mediated PMU counter accesses https://github.com/kvm-x86/linux/commit/0ea0d6314870 [24/44] KVM: x86/pmu: Introduce eventsel_hw to prepare for pmu event filtering https://github.com/kvm-x86/linux/commit/02918f007792 [25/44] KVM: x86/pmu: Reprogram mediated PMU event selectors on event filter updates https://github.com/kvm-x86/linux/commit/3db871fe185b [26/44] KVM: x86/pmu: Always stuff GuestOnly=1,HostOnly=0 for mediated PMCs on AMD https://github.com/kvm-x86/linux/commit/a2f4ba534cc5 [27/44] KVM: x86/pmu: Load/put mediated PMU context when entering/exiting guest https://github.com/kvm-x86/linux/commit/56bb27369750 [28/44] KVM: x86/pmu: Disallow emulation in the fastpath if mediated PMCs are active https://github.com/kvm-x86/linux/commit/f7a65e58d643 [29/44] KVM: x86/pmu: Handle emulated instruction for mediated vPMU https://github.com/kvm-x86/linux/commit/283a5aa57b22 [30/44] KVM: nVMX: Add macros to simplify nested MSR interception setting https://github.com/kvm-x86/linux/commit/cb58327c4c8a [31/44] KVM: nVMX: Disable PMU MSR interception as appropriate while running L2 https://github.com/kvm-x86/linux/commit/88ebc2a3199c [32/44] KVM: nSVM: Disable PMU MSR interception as appropriate while running L2 https://github.com/kvm-x86/linux/commit/3b36160d9406 [33/44] KVM: x86/pmu: Expose enable_mediated_pmu parameter to user space https://github.com/kvm-x86/linux/commit/860bcb1021f5 [34/44] KVM: x86/pmu: Elide WRMSRs when loading guest PMCs if values already match https://github.com/kvm-x86/linux/commit/b0b6a8d3be16 [35/44] KVM: VMX: Drop intermediate "guest" field from msr_autostore https://github.com/kvm-x86/linux/commit/462f092dc55c [36/44] KVM: nVMX: Don't update msr_autostore count when saving TSC for vmcs12 https://github.com/kvm-x86/linux/commit/58f21a01417f [37/44] KVM: VMX: Dedup code for removing MSR from VMCS's auto-load list https://github.com/kvm-x86/linux/commit/0bd29379114b [38/44] KVM: VMX: Drop unused @entry_only param from add_atomic_switch_msr() https://github.com/kvm-x86/linux/commit/84ac00042a28 [39/44] KVM: VMX: Bug the VM if either MSR auto-load list is full https://github.com/kvm-x86/linux/commit/2ed57bb89976 [40/44] KVM: VMX: Set MSR index auto-load entry if and only if entry is "new" https://github.com/kvm-x86/linux/commit/0c4ff0866fc1 [41/44] KVM: VMX: Compartmentalize adding MSRs to host vs. guest auto-load list https://github.com/kvm-x86/linux/commit/2239d137a71d [42/44] KVM: VMX: Dedup code for adding MSR to VMCS's auto list https://github.com/kvm-x86/linux/commit/c3d6a7210a4d [43/44] KVM: VMX: Initialize vmcs01.VM_EXIT_MSR_STORE_ADDR with list address https://github.com/kvm-x86/linux/commit/9757a5aebcd6 [44/44] KVM: VMX: Add mediated PMU support for CPUs without "save perf global ctrl" https://github.com/kvm-x86/linux/commit/d374b89edbb9