perf/x86: Don't write PEBS_ENABLED on KVM transitions
Subsystems (4)
Sean Christopherson
maintainerv4 cover perf/x86: Don't write PEBS_ENABLED on KVM transitions
Rework the handling of PEBS_ENABLED (and related PEBS MSRs) to *never* touch PEBS_ENABLED if the CPU provides PEBS isolation, in which case disabling counters via PERF_GLOBAL_CTRL is sufficient to prevent generation of unwanted PEBS records. For vCPUs without PEBS enabled, this saves upwards of 7 MSR writes on each roundtrip between the guest and host (KVM performs an immediate WRMSR to zero out PEBS_ENABLED if it's in the load list). For vCPUS with PEBS, this saves 3 MSR writes per roundtrip.
E.g. without PEBS activity in the host, for a guest with a vPMU, this reduces the roundtrip time for a fastpath exit from ~1120 => ~860 cycles on EMR. With host PEBS active, the reduction is ~1450 => ~900 cycles.
However, performance isn't the underlying motiviation (well, at least, it didn't start that way). Jim, Mingwei, and Stephane have been chasing issues where PEBS_ENABLED bits can get "stuck" in a '1' state when running KVM guests while profiling the host with PEBS events. The working theory is that perf throttles PEBS events in NMI context, and thus clears bits in cpuc->pebs_enabled and PEBS_ENABLED, after generating the list of PMU MSRs to context switch but before VM-Entry. And so when the host's PEBS_ENABLED is loaded on VM-Exit, the CPU ends up with a stale PEBS_ENABLED that doesn't get reset until something triggers an explicit reload in perf.
Note, as Peter pointed out, more than likely KVM needs to zero PERF_GLOBAL_CTRL before invoking perf_guest_get_msrs(), as that's the only way to guarantee stable output. I deliberately didn't include that here, as I want to keep this series focused on PEBS. I also wanted to let Jim and company bottom out on their investigation (still ongoing) before pursuing fixes that we'll probably want to send to stable@.
v4:
- Collect reviews. [Dapeng, Jim]
- Clarify that patch 1 is a fix, albeit for something that can't happen in the current code base (I missed that intel_ctrl contains only a mask of PMCs, whereas PEBS_ENABLE has additional "modifier" bits). [Sashiko]
- Tack on a patch to filter out non-counter PEBS_ENABLE bits when checking to see if the host is actually using PEBS. [Sashiko, Dapeng]
v3:
- https://lore.kernel.org/all/[email protected]
- Ensure guest PEBS_ENABLE is a subset of intel_ctrl. [Jim]
- Rename intel_ctrl_{guest,host}_mask to be less confusing. [Jim]
- Do even more cleanup of the cross-mapped handling, and specifically avoid overhead when PEBS isn't in use. [Sashiko]
- Leave behind a FIXME regarding the "disable guest PEBS if host is using PEBS" code. I still don't know for sure why that restriction is in place, and I'm too scared too change it. :-)
v2:
- https://lore.kernel.org/all/[email protected]
- "Load" the host value for the guest when an MSR should remain unchanged, instead of omitting the MSR from the list entirely, as KVM may need to _remove_ the MSR from the list. [Sashiko, Jim]
- Collect Jim's reviews. [Jim]
- Call out that the bug being fixed is theoretical at this point.
- Dropping PEBS_ENABLED from the lists save three MSR writes, not two, as KVM performs an explicit WRMSR prior to VM-Entry to guarantee PEBS is quiesced.
v1: https://lore.kernel.org/all/[email protected]
Sean Christopherson (10):
perf/x86/intel: Ensure guest PEBS path doesn't set unwanted
PERF_GLOBAL_CTRL bits
perf/x86/intel: Don't write PEBS_ENABLED on host<=>guest xfers if CPU
has isolation
perf/x86/intel: Don't context switch DS_AREA (and PEBS config) if PEBS
is unused
perf/x86/intel: Make @data a mandatory param for
intel_guest_get_msrs()
perf/x86/intel: Invert names of intel_ctrl_{guest,host}_mask perf/x86: KVM: Have perf define a dedicated struct for getting guest
PEBS data
perf/x86/intel: KVM: Handle cross-mapped PEBS PMCs entirely within KVM
KVM: VMX: Drop a redundant pmu->global_ctrl check when processing
pebs_enable
KVM: VMX: Only tell perf to enable PEBS counters for fully enabled
PMCs
perf/x86/intel: Check only PMC bits in PEBS_ENABLED when detecting
host PEBS usage
arch/x86/events/core.c | 5 +- arch/x86/events/intel/core.c | 94 +++++++++++++++++++------------ arch/x86/events/intel/lbr.c | 2 +- arch/x86/events/perf_event.h | 7 ++- arch/x86/include/asm/kvm_host.h | 9 --- arch/x86/include/asm/perf_event.h | 11 +++- arch/x86/kvm/vmx/pmu_intel.c | 28 ++++++--- arch/x86/kvm/vmx/vmx.c | 10 ++-- arch/x86/kvm/vmx/vmx.h | 15 ++++- 9 files changed, 115 insertions(+), 66 deletions(-)
base-commit: 6457dc9f2174508a4cc339923a2f18f4d59d17a8
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
Sean Christopherson
maintainerPATCH v4 1/10 perf/x86/intel: Ensure guest PEBS path doesn't set unwanted PERF_GLOBAL_CTRL bits
When reinstating PEBS counters into PERF_GLOBAL_CTRL for a KVM guest, mask the value with perf's desired/original PERF_GLOBAL_CTRL value to ensure KVM doesn't unintentionally set reserved bits in PERF_GLOBAL_CTRL. E.g. if the guest's PEBS_ENABLE value had bit 63, "Enable Precise Store", set, then using the raw guest PEBS value would propagate bit 63 to the guest's PERF_GLOBAL_CTRL value (which thankfully would be a failed VM-Entry, not a VMX Abort).
The only reason this bug isn't reachable is because KVM doesn't support "Enable Precise Store" (which is probably a KVM bug?), i.e. bit 63 can't be set in kvm_pmu->pebs_enable and thus not in arr[pebs_enable].guest. In other words, this _should_ be a glorified nop in the current code base.
--- arch/x86/events/intel/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/intel/core.c +1 -1
@@ -5392,7 +5392,7 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data) arr[pebs_enable].guest &= ~kvm_pmu->host_cross_mapped_mask; arr[global_ctrl].guest &= ~kvm_pmu->host_cross_mapped_mask;/* Set hw GLOBAL_CTRL bits for PEBS counter when it runs for guest */ - arr[global_ctrl].guest |= arr[pebs_enable].guest; + arr[global_ctrl].guest |= intel_ctrl & arr[pebs_enable].guest; }return arr;
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 01/10] perf/x86/intel: Ensure guest PEBS path doesn't set unwanted PERF_GLOBAL_CTRL bits
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines
When reinstating PEBS counters into
PERF_GLOBAL_CTRLfor a KVM guest, mask the value with perf's desired/originalPERF_GLOBAL_CTRLvalue to ensure KVM doesn't unintentionally set reserved bits inPERF_GLOBAL_CTRL. E.g. if the guest'sPEBS_ENABLEvalue had bit 63, "Enable Precise Store", set, then using the raw guest PEBS value would propagate bit 63 to the guest'sPERF_GLOBAL_CTRLvalue (which thankfully would be a failed VM-Entry, not a VMX Abort).
Quoted 8 lines
The only reason this bug isn't reachable is because KVM doesn't support "Enable Precise Store" (which is probably a KVM bug?), i.e. bit 63 can't be set in
kvm_pmu->pebs_enableand thus not in arr[pebs_enable].guest. In other words, this _should_ be a glorified nop in the current code base.Fixes c59a1f106f5c ("KVM: x86/pmu: Add IA32_PEBS_ENABLE MSR emulation for extended PEBS")Reviewed-by Dapeng Mi <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[tip: perf/urgent] perf/x86/intel: Ensure KVM guest PEBS path doesn't set unwanted PERF_GLOBAL_CTRL bits
In reply to Sean ChristophersonThe following commit has been merged into the perf/urgent branch of tip:
The following commit has been merged into the perf/urgent branch of tip:
Commit-ID: cec38d5c098a350dcf084d345025136ade7e6d1e Gitweb: https://git.kernel.org/tip/cec38d5c098a350dcf084d345025136ade7e6d1e Author: Sean Christopherson <[email protected]> AuthorDate: Mon, 21 Sep 2026 12:14:09 -07:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Tue, 22 Sep 2026 11:02:37 +02:00
perf/x86/intel: Ensure KVM guest PEBS path doesn't set unwanted PERF_GLOBAL_CTRL bits
When reinstating PEBS counters into PERF_GLOBAL_CTRL for a KVM guest, mask the value with perf's desired/original PERF_GLOBAL_CTRL value to ensure KVM doesn't unintentionally set reserved bits in PERF_GLOBAL_CTRL. E.g. if the guest's PEBS_ENABLE value had bit 63, "Enable Precise Store", set, then using the raw guest PEBS value would propagate bit 63 to the guest's PERF_GLOBAL_CTRL value (which thankfully would be a failed VM-Entry, not a VMX Abort).
The only reason this bug isn't reachable is because KVM doesn't support "Enable Precise Store" (which is probably a KVM bug?), i.e. bit 63 can't be set in kvm_pmu->pebs_enable and thus not in arr[pebs_enable].guest. In other words, this _should_ be a glorified NOP in the current code base.
--- arch/x86/events/intel/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/intel/core.c +1 -1
@@ -5359,7 +5359,7 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data) arr[pebs_enable].guest &= ~kvm_pmu->host_cross_mapped_mask; arr[global_ctrl].guest &= ~kvm_pmu->host_cross_mapped_mask;/* Set hw GLOBAL_CTRL bits for PEBS counter when it runs for guest */ - arr[global_ctrl].guest |= arr[pebs_enable].guest; + arr[global_ctrl].guest |= intel_ctrl & arr[pebs_enable].guest; }return arr;
Sean Christopherson
maintainerPATCH v4 2/10 perf/x86/intel: Don't write PEBS_ENABLED on host<=>guest xfers if CPU has isolation
In reply to Sean ChristophersonWhen filling the list of MSRs to be loaded by KVM on VM-Enter and VM-Exit, *never* insert an entry for PEBS_ENABLED if the CPU properly isolates PEBS events, in which case disabling counters via PERF_GLOBAL_CTRL is sufficient to prevent unwanted PEBS events in the guest (or host). Because perf loads PEBS_ENABLE with the unfiltered cpu_hw_events.pebs_enabled, i.e. with both host and guest masks, there is no need to load different values for the guest versus host, perf+KVM can and should simply control which counters are enabled/disabled via PERF_GLOBAL_CTRL.
Avoiding touching PEBS_ENABLED "fixes" a bug where PEBS_ENABLED can end up with "stuck" bits if a PEBS event is throttled between generating the list and actually entering the guest (Intel CPUs can't arbtitrarily block NMIs). Fixes in quotes because leaving PEBS_ENABLED as-is doesn't fix the underlying problem of perf (via PMIs) being able to modify state after the perf<=>KVM handoff.
But not writing PEBS_ENABLED is desirable no matter what, as stating the obvious, leaving PEBS_ENABLED as-is avoids three MSR writes on every VMX
transition: one each on entry/exit, and one more explicit WRMSR to zero PEBS_ENABLED before VM-Entry (KVM assumes the only reason PEBS_ENABLED is in the load list is if the CPU lacks isolation and thus needs a quiescent period).
Opportunistically add comments to (better) explain the rules for generating the set of PEBS counters that will be active while the guest is running, along with a FIXME for the suspected hack-a-fix where perf disables guest PEBS if _any_ PEBS event is configured to count in the host (commit 854250329c02 ("KVM: x86/pmu: Disable guest PEBS temporarily in two rare situations") doesn't explain the motivation, at all).
--- arch/x86/events/intel/core.c | 55 ++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 18 deletions(-)
arch/x86/events/intel/core.c +37 -18
@@ -5325,12 +5325,15 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data)struct kvm_pmu *kvm_pmu = (struct kvm_pmu *)data;u64 intel_ctrl =hybrid (cpuc->pmu, intel_ctrl);u64 pebs_mask = cpuc->pebs_enabled & x86_pmu.pebs_capable; -int global_ctrl, pebs_enable; +u64 guest_pebs_mask; +int global_ctrl;/* * In addition to obeying exclude_guest/exclude_host, remove bits being * used for PEBS when running a guest, because PEBS writes to virtual -* addresses (not physical addresses). +* addresses (not physical addresses). If the guest wants to utilize +* PEBS, and PEBS can safely enabled in the guest, bits for the guest's +* PEBS-enabled counters will be OR'd back in as appropriate. */ *nr =0 ; global_ctrl = (*nr)++;
@@ -5377,24 +5380,40 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data) }; } - pebs_enable = (*nr)++; - arr[pebs_enable] = (struct perf_guest_switch_msr ){ - .msr =MSR_IA32_PEBS_ENABLE , - .host = cpuc->pebs_enabled & ~cpuc->intel_ctrl_guest_mask, - .guest = pebs_mask & ~cpuc->intel_ctrl_host_mask & kvm_pmu->pebs_enable, - }; +/* +* Restrict guest PEBS events to counters that (a) perf supports, (b) +* the guest wants to use for PEBS, (c) are not excluded from counting +* in the guest, and (d) _are_ excluded from counting in the host. +*/ + guest_pebs_mask = pebs_mask & intel_ctrl & kvm_pmu->pebs_enable & + ~cpuc->intel_ctrl_host_mask & + cpuc->intel_ctrl_guest_mask; -if (arr[pebs_enable].host) { -/* Disable guest PEBS if host PEBS is enabled. */ - arr[pebs_enable].guest =0 ; - }else { -/* Disable guest PEBS thoroughly for cross-mapped PEBS counters. */ - arr[pebs_enable].guest &= ~kvm_pmu->host_cross_mapped_mask; - arr[global_ctrl].guest &= ~kvm_pmu->host_cross_mapped_mask; -/* Set hw GLOBAL_CTRL bits for PEBS counter when it runs for guest */ - arr[global_ctrl].guest |= intel_ctrl & arr[pebs_enable].guest; - } +/* +* Disable counters where the guest PMC is different than the host PMC +* being used on behalf of the guest, as the PEBS record includes +* PERF_GLOBAL_STATUS, i.e. the guest will see overflow status for the +* wrong counter(s). +*/ + guest_pebs_mask &= ~kvm_pmu->host_cross_mapped_mask; +/* +* FIXME: Allow guest and host usage of PEBS events to co-exist instead +* of disabling guest PEBS entirely if the host is using PEBS. +* What exactly goes wrong if guest and host are using PEBS is +* unknown. +*/ +if (pebs_mask & ~cpuc->intel_ctrl_guest_mask) + guest_pebs_mask =0 ; + +/* +* Do NOT mess with PEBS_ENABLED. As above, disabling counters via +* PERF_GLOBAL_CTRL is sufficient, and loading a stale PEBS_ENABLED, +* e.g. on VM-Exit, can put the system in a bad state. Simply enable +* counters in PERF_GLOBAL_CTRL, as perf load PEBS_ENABLED with the +* full value, i.e. perf *also* relies on PERF_GLOBAL_CTRL. +*/ + arr[global_ctrl].guest |= guest_pebs_mask;return arr; }
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 02/10] perf/x86/intel: Don't write PEBS_ENABLED on host<=>guest xfers if CPU has isolation
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines
When filling the list of MSRs to be loaded by KVM on VM-Enter and VM-Exit, *never* insert an entry for
PEBS_ENABLEDif the CPU properly isolates PEBS events, in which case disabling counters viaPERF_GLOBAL_CTRLis sufficient to prevent unwanted PEBS events in the guest (or host). Because perf loadsPEBS_ENABLEwith the unfiltered cpu_hw_events.pebs_enabled, i.e. with both host and guest masks, there is no need to load different values for the guest versus host, perf+KVM can and should simply control which counters are enabled/disabled viaPERF_GLOBAL_CTRL.Avoiding touching
PEBS_ENABLED"fixes" a bug wherePEBS_ENABLEDcan end up with "stuck" bits if a PEBS event is throttled between generating the list and actually entering the guest (Intel CPUs can't arbtitrarily block NMIs). Fixes in quotes because leavingPEBS_ENABLEDas-is doesn't fix the underlying problem of perf (via PMIs) being able to modify state after the perf<=>KVM handoff.
Quoted 6 lines
[ ... ]Cc Jim Mattson <[email protected]>Cc Mingwei Zhang <[email protected]>Cc Stephane Eranian <[email protected]>Reviewed-by Dapeng Mi <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[tip: perf/urgent] perf/x86/intel: Don't write PEBS_ENABLED on host<=>guest xfers if CPU has PEBS isolation, to fix stuck PEBS_ENABLED
In reply to Sean ChristophersonThe following commit has been merged into the perf/urgent branch of tip:
The following commit has been merged into the perf/urgent branch of tip:
Commit-ID: 4b64dbdc5861477f148e13d1ed127e7fe7182e4f Gitweb: https://git.kernel.org/tip/4b64dbdc5861477f148e13d1ed127e7fe7182e4f Author: Sean Christopherson <[email protected]> AuthorDate: Mon, 21 Sep 2026 12:14:10 -07:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Tue, 22 Sep 2026 11:07:00 +02:00
perf/x86/intel: Don't write PEBS_ENABLED on host<=>guest xfers if CPU has PEBS isolation, to fix stuck PEBS_ENABLED
When filling the list of MSRs to be loaded by KVM on VM-Enter and VM-Exit, *never* insert an entry for PEBS_ENABLED if the CPU properly isolates PEBS events, in which case disabling counters via PERF_GLOBAL_CTRL is sufficient to prevent unwanted PEBS events in the guest (or host). Because perf loads PEBS_ENABLE with the unfiltered cpu_hw_events.pebs_enabled, i.e. with both host and guest masks, there is no need to load different values for the guest versus host, perf+KVM can and should simply control which counters are enabled/disabled via PERF_GLOBAL_CTRL.
Avoiding touching PEBS_ENABLED "fixes" a bug where PEBS_ENABLED can end up with "stuck" bits if a PEBS event is throttled between generating the list and actually entering the guest (Intel CPUs can't arbtitrarily block NMIs). Fixes in quotes because leaving PEBS_ENABLED as-is doesn't fix the underlying problem of perf (via PMIs) being able to modify state after the perf<=>KVM handoff.
But not writing PEBS_ENABLED is desirable no matter what, as stating the obvious, leaving PEBS_ENABLED as-is avoids three MSR writes on every VMX
transition: one each on entry/exit, and one more explicit WRMSR to zero PEBS_ENABLED before VM-Entry (KVM assumes the only reason PEBS_ENABLED is in the load list is if the CPU lacks PEBS isolation and thus needs a quiescent period).
Opportunistically add comments to (better) explain the rules for generating the set of PEBS counters that will be active while the guest is running, along with a FIXME for the suspected hack-a-fix where perf disables guest PEBS if _any_ PEBS event is configured to count in the host (commit 854250329c02 ("KVM: x86/pmu: Disable guest PEBS temporarily in two rare situations") doesn't explain the motivation, at all).
--- arch/x86/events/intel/core.c | 55 +++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 18 deletions(-)
arch/x86/events/intel/core.c +37 -18
@@ -5292,12 +5292,15 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data)struct kvm_pmu *kvm_pmu = (struct kvm_pmu *)data;u64 intel_ctrl =hybrid (cpuc->pmu, intel_ctrl);u64 pebs_mask = cpuc->pebs_enabled & x86_pmu.pebs_capable; -int global_ctrl, pebs_enable; +u64 guest_pebs_mask; +int global_ctrl;/* * In addition to obeying exclude_guest/exclude_host, remove bits being * used for PEBS when running a guest, because PEBS writes to virtual -* addresses (not physical addresses). +* addresses (not physical addresses). If the guest wants to utilize +* PEBS, and PEBS can be safely enabled in the guest, bits for the guest's +* PEBS-enabled counters will be OR'd back in as appropriate. */ *nr =0 ; global_ctrl = (*nr)++;
@@ -5344,24 +5347,40 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data) }; } - pebs_enable = (*nr)++; - arr[pebs_enable] = (struct perf_guest_switch_msr ){ - .msr =MSR_IA32_PEBS_ENABLE , - .host = cpuc->pebs_enabled & ~cpuc->intel_ctrl_guest_mask, - .guest = pebs_mask & ~cpuc->intel_ctrl_host_mask & kvm_pmu->pebs_enable, - }; +/* +* Restrict guest PEBS events to counters that (a) perf supports, (b) +* the guest wants to use for PEBS, (c) are not excluded from counting +* in the guest, and (d) _are_ excluded from counting in the host. +*/ + guest_pebs_mask = pebs_mask & intel_ctrl & kvm_pmu->pebs_enable & + ~cpuc->intel_ctrl_host_mask & + cpuc->intel_ctrl_guest_mask; -if (arr[pebs_enable].host) { -/* Disable guest PEBS if host PEBS is enabled. */ - arr[pebs_enable].guest =0 ; - }else { -/* Disable guest PEBS thoroughly for cross-mapped PEBS counters. */ - arr[pebs_enable].guest &= ~kvm_pmu->host_cross_mapped_mask; - arr[global_ctrl].guest &= ~kvm_pmu->host_cross_mapped_mask; -/* Set hw GLOBAL_CTRL bits for PEBS counter when it runs for guest */ - arr[global_ctrl].guest |= intel_ctrl & arr[pebs_enable].guest; - } +/* +* Disable counters where the guest PMC is different than the host PMC +* being used on behalf of the guest, as the PEBS record includes +* PERF_GLOBAL_STATUS, i.e. the guest will see overflow status for the +* wrong counter(s). +*/ + guest_pebs_mask &= ~kvm_pmu->host_cross_mapped_mask; + +/* +* FIXME: Allow guest and host usage of PEBS events to co-exist instead +* of disabling guest PEBS entirely if the host is using PEBS. +* What exactly goes wrong if guest and host are using PEBS is +* unknown. +*/ +if (pebs_mask & ~cpuc->intel_ctrl_guest_mask) + guest_pebs_mask =0 ; +/* +* Do NOT mess with PEBS_ENABLED. As above, disabling counters via +* PERF_GLOBAL_CTRL is sufficient, and loading a stale PEBS_ENABLED, +* e.g. on VM-Exit, can put the system in a bad state. Simply enable +* counters in PERF_GLOBAL_CTRL, as perf load PEBS_ENABLED with the +* full value, i.e. perf *also* relies on PERF_GLOBAL_CTRL. +*/ + arr[global_ctrl].guest |= guest_pebs_mask;return arr; }
Sean Christopherson
maintainerPATCH v4 3/10 perf/x86/intel: Don't context switch DS_AREA (and PEBS config) if PEBS is unused
In reply to Sean ChristophersonWhen filling the list of MSRs to be loaded by KVM on VM-Enter and VM-Exit, load the guest values for DS_AREA and (conditionally) MSR_PEBS_DATA_CFG if and only if PEBS will be active in the guest, i.e. only if a PEBS record may be generated while running the guest. As shown by the !pebs_ept path, it's perfectly safe to run with the host's DS_AREA, so long as PEBS-enabled counters are disabled via PERF_GLOBAL_CTRL.
Omitting DS_AREA and MSR_PEBS_DATA_CFG when PEBS is unused saves two MSR writes per MSR on each VMX transition, i.e. eliminates two/four pointless MSR writes on each VMX roundtrip when PEBS isn't being used by the guest.
--- arch/x86/events/intel/core.c | 39 +++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 14 deletions(-)
arch/x86/events/intel/core.c +25 -14
@@ -5363,23 +5363,14 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data)return arr; } +/* +* If the guest won't use PEBS or the CPU doesn't support PEBS in the +* guest, then there's nothing more to do as disabling PMCs via +* PERF_GLOBAL_CTRL is sufficient on CPUs with guest/host isolation. +*/ if (!kvm_pmu || !x86_pmu.pebs_ept)return arr; - arr[(*nr)++] = (struct perf_guest_switch_msr ){ - .msr =MSR_IA32_DS_AREA , - .host = (unsigned long )cpuc->ds, - .guest = kvm_pmu->ds_area, - }; - -if (x86_pmu.intel_cap.pebs_baseline) { - arr[(*nr)++] = (struct perf_guest_switch_msr ){ - .msr =MSR_PEBS_DATA_CFG , - .host = cpuc->active_pebs_data_cfg, - .guest = kvm_pmu->pebs_data_cfg, - }; - } -/* * Restrict guest PEBS events to counters that (a) perf supports, (b) * the guest wants to use for PEBS, (c) are not excluded from counting
@@ -5406,6 +5397,26 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data)if (pebs_mask & ~cpuc->intel_ctrl_guest_mask) guest_pebs_mask =0 ; +/* +* Context switch DS_AREA and PEBS_DATA_CFG if and only if PEBS will be +* active in the guest; if no records will be generated while the guest +* is running, then simply keep the host values resident in hardware. +*/ + arr[(*nr)++] = (struct perf_guest_switch_msr ){ + .msr =MSR_IA32_DS_AREA , + .host = (unsigned long )cpuc->ds, + .guest = guest_pebs_mask ? kvm_pmu->ds_area : (unsigned long )cpuc->ds, + }; + +if (x86_pmu.intel_cap.pebs_baseline) { + arr[(*nr)++] = (struct perf_guest_switch_msr ){ + .msr =MSR_PEBS_DATA_CFG , + .host = cpuc->active_pebs_data_cfg, + .guest = guest_pebs_mask ? kvm_pmu->pebs_data_cfg : + cpuc->active_pebs_data_cfg, + }; + } +/* * Do NOT mess with PEBS_ENABLED. As above, disabling counters via * PERF_GLOBAL_CTRL is sufficient, and loading a stale PEBS_ENABLED,
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 03/10] perf/x86/intel: Don't context switch DS_AREA (and PEBS config) if PEBS is unused
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines
When filling the list of MSRs to be loaded by KVM on VM-Enter and VM-Exit, load the guest values for
DS_AREAand (conditionally)MSR_PEBS_DATA_CFGif and only if PEBS will be active in the guest, i.e. only if a PEBS record may be generated while running the guest. As shown by the !pebs_ept path, it's perfectly safe to run with the host'sDS_AREA, so long as PEBS-enabled counters are disabled viaPERF_GLOBAL_CTRL.Omitting
DS_AREAandMSR_PEBS_DATA_CFGwhen PEBS is unused saves two MSR writes per MSR on each VMX transition, i.e. eliminates two/four pointless MSR writes on each VMX roundtrip when PEBS isn't being used by the guest.
Quoted 7 lines
Fixes c59a1f106f5c ("KVM: x86/pmu: Add IA32_PEBS_ENABLE MSR emulation for extended PEBS")Cc Jim Mattson <[email protected]>Cc Mingwei Zhang <[email protected]>Cc Stephane Eranian <[email protected]>Reviewed-by Jim Mattson <[email protected]>Reviewed-by Dapeng Mi <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
[tip: perf/urgent] perf/x86/intel: Don't pointlessly context switch DS_AREA (and PEBS config) if PEBS is unused
In reply to Sean ChristophersonThe following commit has been merged into the perf/urgent branch of tip:
The following commit has been merged into the perf/urgent branch of tip:
Commit-ID: d06260e99eb93d2942b7af4ccd789eb8a6c829d3 Gitweb: https://git.kernel.org/tip/d06260e99eb93d2942b7af4ccd789eb8a6c829d3 Author: Sean Christopherson <[email protected]> AuthorDate: Mon, 21 Sep 2026 12:14:11 -07:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Tue, 22 Sep 2026 11:09:14 +02:00
perf/x86/intel: Don't pointlessly context switch DS_AREA (and PEBS config) if PEBS is unused
When filling the list of MSRs to be loaded by KVM on VM-Enter and VM-Exit, load the guest values for DS_AREA and (conditionally) MSR_PEBS_DATA_CFG if and only if PEBS will be active in the guest, i.e. only if a PEBS record may be generated while running the guest. As shown by the !pebs_ept path, it's perfectly safe to run with the host's DS_AREA, so long as PEBS-enabled counters are disabled via PERF_GLOBAL_CTRL.
Omitting DS_AREA and MSR_PEBS_DATA_CFG when PEBS is unused saves two MSR writes per MSR on each VMX transition, i.e. eliminates two/four pointless MSR writes on each VMX roundtrip when PEBS isn't being used by the guest.
--- arch/x86/events/intel/core.c | 39 ++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 14 deletions(-)
arch/x86/events/intel/core.c +25 -14
@@ -5330,23 +5330,14 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data)return arr; } +/* +* If the guest won't use PEBS or the CPU doesn't support PEBS in the +* guest, then there's nothing more to do as disabling PMCs via +* PERF_GLOBAL_CTRL is sufficient on CPUs with guest/host isolation. +*/ if (!kvm_pmu || !x86_pmu.pebs_ept)return arr; - arr[(*nr)++] = (struct perf_guest_switch_msr ){ - .msr =MSR_IA32_DS_AREA , - .host = (unsigned long )cpuc->ds, - .guest = kvm_pmu->ds_area, - }; - -if (x86_pmu.intel_cap.pebs_baseline) { - arr[(*nr)++] = (struct perf_guest_switch_msr ){ - .msr =MSR_PEBS_DATA_CFG , - .host = cpuc->active_pebs_data_cfg, - .guest = kvm_pmu->pebs_data_cfg, - }; - } -/* * Restrict guest PEBS events to counters that (a) perf supports, (b) * the guest wants to use for PEBS, (c) are not excluded from counting
@@ -5374,6 +5365,26 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data) guest_pebs_mask =0 ;/* +* Context switch DS_AREA and PEBS_DATA_CFG if and only if PEBS will be +* active in the guest; if no records will be generated while the guest +* is running, then simply keep the host values resident in hardware. +*/ + arr[(*nr)++] = (struct perf_guest_switch_msr ){ + .msr =MSR_IA32_DS_AREA , + .host = (unsigned long )cpuc->ds, + .guest = guest_pebs_mask ? kvm_pmu->ds_area : (unsigned long )cpuc->ds, + }; + +if (x86_pmu.intel_cap.pebs_baseline) { + arr[(*nr)++] = (struct perf_guest_switch_msr ){ + .msr =MSR_PEBS_DATA_CFG , + .host = cpuc->active_pebs_data_cfg, + .guest = guest_pebs_mask ? kvm_pmu->pebs_data_cfg : + cpuc->active_pebs_data_cfg, + }; + } + +/* * Do NOT mess with PEBS_ENABLED. As above, disabling counters via * PERF_GLOBAL_CTRL is sufficient, and loading a stale PEBS_ENABLED, * e.g. on VM-Exit, can put the system in a bad state. Simply enable
Sean Christopherson
maintainerPATCH v4 4/10 perf/x86/intel: Make @data a mandatory param for intel_guest_get_msrs()
In reply to Sean ChristophersonDrop "support" for passing a NULL @data/@kvm_pmu param when getting guest MSRs. KVM, the only in-tree user, unconditionally passes a non-NULL pointer, and carrying code that suggests @data may be NULL is confusing, e.g. incorrectly implies that there are scenarios where KVM doesn't pass a PMU context.
--- arch/x86/events/intel/core.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/events/intel/core.c +4 -4
@@ -5364,11 +5364,11 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data) }/* -* If the guest won't use PEBS or the CPU doesn't support PEBS in the -* guest, then there's nothing more to do as disabling PMCs via -* PERF_GLOBAL_CTRL is sufficient on CPUs with guest/host isolation. +* If the CPU doesn't support PEBS in the guest, then there's nothing +* more to do as disabling PMCs via PERF_GLOBAL_CTRL is sufficient on +* CPUs with guest/host isolation. */ -if (!kvm_pmu || !x86_pmu.pebs_ept) +if (!x86_pmu.pebs_ept)return arr;/*
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 04/10] perf/x86/intel: Make @data a mandatory param for intel_guest_get_msrs()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines
Drop "support" for passing a NULL @data/@kvm_pmu param when getting guest MSRs. KVM, the only in-tree user, unconditionally passes a non-NULL pointer, and carrying code that suggests @data may be NULL is confusing, e.g. incorrectly implies that there are scenarios where KVM doesn't pass a PMU context.
Quoted 7 lines
Fixes 8183a538cd95 ("KVM: x86/pmu: Add IA32_DS_AREA MSR emulation to support guest DS")Cc Jim Mattson <[email protected]>Cc Mingwei Zhang <[email protected]>Cc Stephane Eranian <[email protected]>Reviewed-by Jim Mattson <[email protected]>Reviewed-by Dapeng Mi <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
[tip: perf/urgent] perf/x86/intel: Make @data a mandatory param for intel_guest_get_msrs()
In reply to Sean ChristophersonThe following commit has been merged into the perf/urgent branch of tip:
The following commit has been merged into the perf/urgent branch of tip:
Commit-ID: a391618e1d563f099e4c2a704f45d08329ccdf7c Gitweb: https://git.kernel.org/tip/a391618e1d563f099e4c2a704f45d08329ccdf7c Author: Sean Christopherson <[email protected]> AuthorDate: Mon, 21 Sep 2026 12:14:12 -07:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Tue, 22 Sep 2026 11:09:45 +02:00
perf/x86/intel: Make @data a mandatory param for intel_guest_get_msrs()
Drop "support" for passing a NULL @data/@kvm_pmu param when getting guest MSRs. KVM, the only in-tree user, unconditionally passes a non-NULL pointer, and carrying code that suggests @data may be NULL is confusing, e.g. incorrectly implies that there are scenarios where KVM doesn't pass a PMU context.
--- arch/x86/events/intel/core.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/events/intel/core.c +4 -4
@@ -5331,11 +5331,11 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data) }/* -* If the guest won't use PEBS or the CPU doesn't support PEBS in the -* guest, then there's nothing more to do as disabling PMCs via -* PERF_GLOBAL_CTRL is sufficient on CPUs with guest/host isolation. +* If the CPU doesn't support PEBS in the guest, then there's nothing +* more to do as disabling PMCs via PERF_GLOBAL_CTRL is sufficient on +* CPUs with guest/host isolation. */ -if (!kvm_pmu || !x86_pmu.pebs_ept) +if (!x86_pmu.pebs_ept)return arr;/*
Sean Christopherson
maintainerPATCH v4 5/10 perf/x86/intel: Invert names of intel_ctrl_{guest,host}_mask
In reply to Sean ChristophersonRename intel_ctrl_{guest,host}_mask to intel_ctrl_exclude_{host,guest}_mask to more accurately capture what they actually track. Specifically, an event that is excluded from the guest is NOT guaranteed to count in the host, and vice versa, as it legal (albeit bizarre) to configure an event to exclude both the host and the guest, i.e. to not count at all.
Subjectively (though anyone who disagrees is wrong), aligning with perf_event_attr.exclude_{guest,host} also makes all related code much easier to follow.
No functional change intended.
--- arch/x86/events/intel/core.c | 22 +++++++++++----------- arch/x86/events/intel/lbr.c | 2 +- arch/x86/events/perf_event.h | 4 ++-- 3 files changed, 14 insertions(+), 14 deletions(-)
arch/x86/events/intel/core.c +11 -11
@@ -2787,7 +2787,7 @@ static void __intel_pmu_enable_all(int added, bool pmi) }wrmsrq (MSR_CORE_PERF_GLOBAL_CTRL , - intel_ctrl & ~cpuc->intel_ctrl_guest_mask); + intel_ctrl & ~cpuc->intel_ctrl_exclude_host_mask);if (test_bit (INTEL_PMC_IDX_FIXED_BTS , cpuc->active_mask)) { struct perf_event *event =
@@ -2985,9 +2985,9 @@ static inline void intel_set_masks(struct perf_event *event, int idx)struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events);if (event->attr.exclude_host) -__set_bit (idx, (unsigned long *)&cpuc->intel_ctrl_guest_mask); +__set_bit (idx, (unsigned long *)&cpuc->intel_ctrl_exclude_host_mask);if (event->attr.exclude_guest) -__set_bit (idx, (unsigned long *)&cpuc->intel_ctrl_host_mask); +__set_bit (idx, (unsigned long *)&cpuc->intel_ctrl_exclude_guest_mask);if (event_is_checkpointed (event))__set_bit (idx, (unsigned long *)&cpuc->intel_cp_status); }
@@ -2996,8 +2996,8 @@ static inline void intel_clear_masks(struct perf_event *event, int idx) {struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events); -__clear_bit (idx, (unsigned long *)&cpuc->intel_ctrl_guest_mask); -__clear_bit (idx, (unsigned long *)&cpuc->intel_ctrl_host_mask); +__clear_bit (idx, (unsigned long *)&cpuc->intel_ctrl_exclude_host_mask); +__clear_bit (idx, (unsigned long *)&cpuc->intel_ctrl_exclude_guest_mask);__clear_bit (idx, (unsigned long *)&cpuc->intel_cp_status); }
@@ -3779,7 +3779,7 @@ static int x86_pmu_handle_guest_pebs(struct pt_regs *regs, struct perf_sample_data *data) {struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events); -u64 guest_pebs_idxs = cpuc->pebs_enabled & ~cpuc->intel_ctrl_host_mask; +u64 guest_pebs_idxs = cpuc->pebs_enabled & ~cpuc->intel_ctrl_exclude_guest_mask;struct perf_event *event =NULL ;int bit;
@@ -5339,8 +5339,8 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data) global_ctrl = (*nr)++; arr[global_ctrl] = (struct perf_guest_switch_msr ){ .msr =MSR_CORE_PERF_GLOBAL_CTRL , - .host = intel_ctrl & ~cpuc->intel_ctrl_guest_mask, - .guest = intel_ctrl & ~cpuc->intel_ctrl_host_mask & ~pebs_mask, + .host = intel_ctrl & ~cpuc->intel_ctrl_exclude_host_mask, + .guest = intel_ctrl & ~cpuc->intel_ctrl_exclude_guest_mask & ~pebs_mask, };if (!x86_pmu.ds_pebs)
@@ -5377,8 +5377,8 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data)* in the guest, and (d) _are_ excluded from counting in the host. */ guest_pebs_mask = pebs_mask & intel_ctrl & kvm_pmu->pebs_enable & - ~cpuc->intel_ctrl_host_mask & - cpuc->intel_ctrl_guest_mask; + ~cpuc->intel_ctrl_exclude_guest_mask & + cpuc->intel_ctrl_exclude_host_mask;/* * Disable counters where the guest PMC is different than the host PMC
@@ -5394,7 +5394,7 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data)* What exactly goes wrong if guest and host are using PEBS is * unknown. */ -if (pebs_mask & ~cpuc->intel_ctrl_guest_mask) +if (pebs_mask & ~cpuc->intel_ctrl_exclude_host_mask) guest_pebs_mask =0 ;/*
arch/x86/events/intel/lbr.c +1 -1
@@ -714,7 +714,7 @@ static inline bool vlbr_exclude_host(void)struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events);return test_bit (INTEL_PMC_IDX_FIXED_VLBR , - (unsigned long *)&cpuc->intel_ctrl_guest_mask); + (unsigned long *)&cpuc->intel_ctrl_exclude_host_mask); }void intel_pmu_lbr_enable_all (bool pmi)
arch/x86/events/perf_event.h +2 -2
@@ -540,8 +540,8 @@ struct cpu_hw_events {/* * Intel host/guest exclude bits */ -u64 intel_ctrl_guest_mask; -u64 intel_ctrl_host_mask; +u64 intel_ctrl_exclude_host_mask; +u64 intel_ctrl_exclude_guest_mask;struct perf_guest_switch_msr guest_switch_msrs[X86_PMC_IDX_MAX ];/*
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 05/10] perf/x86/intel: Invert names of intel_ctrl_{guest,host}_mask
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines
Rename intel_ctrl_{guest,host}_mask to intel_ctrl_exclude_{host,guest}_mask to more accurately capture what they actually track. Specifically, an event that is excluded from the guest is NOT guaranteed to count in the host, and vice versa, as it legal (albeit bizarre) to configure an event to exclude both the host and the guest, i.e. to not count at all.
Subjectively (though anyone who disagrees is wrong), aligning with perf_event_attr.exclude_{guest,host} also makes all related code much easier to follow.
Quoted 5 lines
No functional change intended.
Suggested-by Jim Mattson <[email protected]>Reviewed-by Dapeng Mi <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
[tip: perf/core] perf/x86/intel: Invert names of intel_ctrl_{guest,host}_mask
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: b7b84fff2bd630b593ee47a733381e3886aac1a7 Gitweb: https://git.kernel.org/tip/b7b84fff2bd630b593ee47a733381e3886aac1a7 Author: Sean Christopherson <[email protected]> AuthorDate: Mon, 21 Sep 2026 12:14:13 -07:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Fri, 25 Sep 2026 12:45:52 +02:00
perf/x86/intel: Invert names of intel_ctrl_{guest,host}_mask
Rename intel_ctrl_{guest,host}_mask to intel_ctrl_exclude_{host,guest}_mask to more accurately capture what they actually track. Specifically, an event that is excluded from the guest is NOT guaranteed to count in the host, and vice versa, as it legal (albeit bizarre) to configure an event to exclude both the host and the guest, i.e. to not count at all.
Subjectively (though anyone who disagrees is wrong), aligning with perf_event_attr.exclude_{guest,host} also makes all related code much easier to follow.
No functional change intended.
--- arch/x86/events/intel/core.c | 22 +++++++++++----------- arch/x86/events/intel/lbr.c | 2 +- arch/x86/events/perf_event.h | 4 ++-- 3 files changed, 14 insertions(+), 14 deletions(-)
arch/x86/events/intel/core.c +11 -11
@@ -2797,7 +2797,7 @@ static void __intel_pmu_enable_all(int added, bool pmi) }wrmsrq (MSR_CORE_PERF_GLOBAL_CTRL , - intel_ctrl & ~cpuc->intel_ctrl_guest_mask); + intel_ctrl & ~cpuc->intel_ctrl_exclude_host_mask);if (test_bit (INTEL_PMC_IDX_FIXED_BTS , cpuc->active_mask)) { struct perf_event *event =
@@ -2995,9 +2995,9 @@ static inline void intel_set_masks(struct perf_event *event, int idx)struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events);if (event->attr.exclude_host) -__set_bit (idx, (unsigned long *)&cpuc->intel_ctrl_guest_mask); +__set_bit (idx, (unsigned long *)&cpuc->intel_ctrl_exclude_host_mask);if (event->attr.exclude_guest) -__set_bit (idx, (unsigned long *)&cpuc->intel_ctrl_host_mask); +__set_bit (idx, (unsigned long *)&cpuc->intel_ctrl_exclude_guest_mask);if (event_is_checkpointed (event))__set_bit (idx, (unsigned long *)&cpuc->intel_cp_status); }
@@ -3006,8 +3006,8 @@ static inline void intel_clear_masks(struct perf_event *event, int idx) {struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events); -__clear_bit (idx, (unsigned long *)&cpuc->intel_ctrl_guest_mask); -__clear_bit (idx, (unsigned long *)&cpuc->intel_ctrl_host_mask); +__clear_bit (idx, (unsigned long *)&cpuc->intel_ctrl_exclude_host_mask); +__clear_bit (idx, (unsigned long *)&cpuc->intel_ctrl_exclude_guest_mask);__clear_bit (idx, (unsigned long *)&cpuc->intel_cp_status); }
@@ -3789,7 +3789,7 @@ static int x86_pmu_handle_guest_pebs(struct pt_regs *regs, struct perf_sample_data *data) {struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events); -u64 guest_pebs_idxs = cpuc->pebs_enabled & ~cpuc->intel_ctrl_host_mask; +u64 guest_pebs_idxs = cpuc->pebs_enabled & ~cpuc->intel_ctrl_exclude_guest_mask;struct perf_event *event =NULL ;int bit;
@@ -5349,8 +5349,8 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data) global_ctrl = (*nr)++; arr[global_ctrl] = (struct perf_guest_switch_msr ){ .msr =MSR_CORE_PERF_GLOBAL_CTRL , - .host = intel_ctrl & ~cpuc->intel_ctrl_guest_mask, - .guest = intel_ctrl & ~cpuc->intel_ctrl_host_mask & ~pebs_mask, + .host = intel_ctrl & ~cpuc->intel_ctrl_exclude_host_mask, + .guest = intel_ctrl & ~cpuc->intel_ctrl_exclude_guest_mask & ~pebs_mask, };if (!x86_pmu.ds_pebs)
@@ -5387,8 +5387,8 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data)* in the guest, and (d) _are_ excluded from counting in the host. */ guest_pebs_mask = pebs_mask & intel_ctrl & kvm_pmu->pebs_enable & - ~cpuc->intel_ctrl_host_mask & - cpuc->intel_ctrl_guest_mask; + ~cpuc->intel_ctrl_exclude_guest_mask & + cpuc->intel_ctrl_exclude_host_mask;/* * Disable counters where the guest PMC is different than the host PMC
@@ -5404,7 +5404,7 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data)* What exactly goes wrong if guest and host are using PEBS is * unknown. */ -if (pebs_mask & ~cpuc->intel_ctrl_guest_mask) +if (pebs_mask & ~cpuc->intel_ctrl_exclude_host_mask) guest_pebs_mask =0 ;/*
arch/x86/events/intel/lbr.c +1 -1
@@ -714,7 +714,7 @@ static inline bool vlbr_exclude_host(void)struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events);return test_bit (INTEL_PMC_IDX_FIXED_VLBR , - (unsigned long *)&cpuc->intel_ctrl_guest_mask); + (unsigned long *)&cpuc->intel_ctrl_exclude_host_mask); }void intel_pmu_lbr_enable_all (bool pmi)
arch/x86/events/perf_event.h +2 -2
@@ -540,8 +540,8 @@ struct cpu_hw_events {/* * Intel host/guest exclude bits */ -u64 intel_ctrl_guest_mask; -u64 intel_ctrl_host_mask; +u64 intel_ctrl_exclude_host_mask; +u64 intel_ctrl_exclude_guest_mask;struct perf_guest_switch_msr guest_switch_msrs[X86_PMC_IDX_MAX ];/*
Sean Christopherson
maintainerPATCH v4 6/10 perf/x86: KVM: Have perf define a dedicated struct for getting guest PEBS data
In reply to Sean ChristophersonHave perf define a struct for getting guest PEBS data from KVM instead of poking into the kvm_pmu structure. Passing in an entire "struct kvm_pmu" _as an opaque pointer_ to get at four fields is silly, especially since one of the fields exists purely to convey information to perf, i.e. isn't used by KVM.
Perf should also own its APIs, i.e. define what fields/data it needs, not rely on KVM to throw fields into data structures that effectively hold KVM-internal state.
Opportunistically rephrase the comment about cross-mapped counters to explain *why* PEBS needs to be disabled.
--- arch/x86/events/core.c | 5 +++-- arch/x86/events/intel/core.c | 16 ++++++++-------- arch/x86/events/perf_event.h | 3 ++- arch/x86/include/asm/kvm_host.h | 9 --------- arch/x86/include/asm/perf_event.h | 12 ++++++++++-- arch/x86/kvm/vmx/pmu_intel.c | 17 ++++++++++++++--- arch/x86/kvm/vmx/vmx.c | 11 ++++++++--- arch/x86/kvm/vmx/vmx.h | 2 +- 8 files changed, 46 insertions(+), 29 deletions(-)
arch/x86/events/core.c +3 -2
@@ -862,9 +862,10 @@ void x86_pmu_disable_all(void) } } -struct perf_guest_switch_msr *perf_guest_get_msrs (int *nr,void *data) +struct perf_guest_switch_msr *perf_guest_get_msrs (int *nr, +struct x86_guest_pebs *guest_pebs) { -return static_call (x86_pmu_guest_get_msrs)(nr, data); +return static_call (x86_pmu_guest_get_msrs)(nr, guest_pebs); }EXPORT_SYMBOL_FOR_KVM (perf_guest_get_msrs);
arch/x86/events/intel/core.c +8 -8
@@ -14,7 +14,6 @@#include <linux/slab.h> #include <linux/export.h> #include <linux/nmi.h> -#include <linux/kvm_host.h> #include <asm/cpufeature.h> #include <asm/cpuid/api.h>
@@ -5318,11 +5317,11 @@ static int intel_pmu_hw_config(struct perf_event *event)* when it uses {RD,WR}MSR, which should be handled by the KVM context, * specifically in the intel_pmu_{get,set}_msr(). */ -static struct perf_guest_switch_msr *intel_guest_get_msrs (int *nr,void *data) +static struct perf_guest_switch_msr *intel_guest_get_msrs (int *nr, +struct x86_guest_pebs *guest_pebs) {struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events);struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs; -struct kvm_pmu *kvm_pmu = (struct kvm_pmu *)data;u64 intel_ctrl =hybrid (cpuc->pmu, intel_ctrl);u64 pebs_mask = cpuc->pebs_enabled & x86_pmu.pebs_capable;u64 guest_pebs_mask;
@@ -5376,7 +5375,7 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data)* the guest wants to use for PEBS, (c) are not excluded from counting * in the guest, and (d) _are_ excluded from counting in the host. */ - guest_pebs_mask = pebs_mask & intel_ctrl & kvm_pmu->pebs_enable & + guest_pebs_mask = pebs_mask & intel_ctrl & guest_pebs->enable & ~cpuc->intel_ctrl_exclude_guest_mask & cpuc->intel_ctrl_exclude_host_mask;
@@ -5386,7 +5385,7 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data)* PERF_GLOBAL_STATUS, i.e. the guest will see overflow status for the * wrong counter(s). */ - guest_pebs_mask &= ~kvm_pmu->host_cross_mapped_mask; + guest_pebs_mask &= ~guest_pebs->cross_mapped_mask;/* * FIXME: Allow guest and host usage of PEBS events to co-exist instead
@@ -5405,14 +5404,14 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data) arr[(*nr)++] = (struct perf_guest_switch_msr ){ .msr =MSR_IA32_DS_AREA , .host = (unsigned long )cpuc->ds, - .guest = guest_pebs_mask ? kvm_pmu->ds_area : (unsigned long )cpuc->ds, + .guest = guest_pebs_mask ? guest_pebs->ds_area : (unsigned long )cpuc->ds, };if (x86_pmu.intel_cap.pebs_baseline) { arr[(*nr)++] = (struct perf_guest_switch_msr ){ .msr =MSR_PEBS_DATA_CFG , .host = cpuc->active_pebs_data_cfg, - .guest = guest_pebs_mask ? kvm_pmu->pebs_data_cfg : + .guest = guest_pebs_mask ? guest_pebs->data_cfg : cpuc->active_pebs_data_cfg, }; }
@@ -5428,7 +5427,8 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data)return arr; } -static struct perf_guest_switch_msr *core_guest_get_msrs (int *nr,void *data) +static struct perf_guest_switch_msr *core_guest_get_msrs (int *nr, +struct x86_guest_pebs *guest_pebs) {struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events);struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs;
arch/x86/events/perf_event.h +2 -1
@@ -1230,7 +1230,8 @@ struct x86_pmu {/* * Intel host/guest support (KVM) */ -struct perf_guest_switch_msr *(*guest_get_msrs)(int *nr,void *data); +struct perf_guest_switch_msr *(*guest_get_msrs)(int *nr, +struct x86_guest_pebs *guest_pebs);/* * Check period value for PERF_EVENT_IOC_PERIOD ioctl.
arch/x86/include/asm/kvm_host.h +0 -9
@@ -608,15 +608,6 @@ struct kvm_pmu {u64 pebs_data_cfg;u64 pebs_data_cfg_rsvd; -/* -* If a guest counter is cross-mapped to host counter with different -* index, its PEBS capability will be temporarily disabled. -* -* The user should make sure that this mask is updated -* after disabling interrupts and before perf_guest_get_msrs(); -*/ -u64 host_cross_mapped_mask; -/* * The gate to release perf_events not marked in * pmc_in_use only once in a vcpu time slice.
arch/x86/include/asm/perf_event.h +10 -2
@@ -819,11 +819,19 @@ extern void perf_load_guest_lvtpc(u32 guest_lvtpc);extern void perf_put_guest_lvtpc (void );#endif +struct x86_guest_pebs { +u64 enable; +u64 ds_area; +u64 data_cfg; +u64 cross_mapped_mask; +};#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 struct perf_guest_switch_msr *perf_guest_get_msrs (int *nr, +struct x86_guest_pebs *guest_pebs);extern void x86_perf_get_lbr (struct x86_pmu_lbr *lbr);#else -struct perf_guest_switch_msr *perf_guest_get_msrs (int *nr,void *data); +struct perf_guest_switch_msr *perf_guest_get_msrs (int *nr, +struct x86_guest_pebs *guest_pebs);static inline void x86_perf_get_lbr (struct x86_pmu_lbr *lbr) {memset (lbr,0 ,sizeof (*lbr));
arch/x86/kvm/vmx/pmu_intel.c +14 -3
@@ -747,11 +747,21 @@ static void intel_pmu_cleanup(struct kvm_vcpu *vcpu)intel_pmu_release_guest_lbr_event (vcpu); } -void intel_pmu_cross_mapped_check (struct kvm_pmu *pmu) +u64 intel_pmu_get_cross_mapped_mask (struct kvm_pmu *pmu) { -struct kvm_pmc *pmc =NULL ; +u64 host_cross_mapped_mask; +struct kvm_pmc *pmc;int bit, hw_idx; +/* +* Provide a mask of counters that are cross-mapped between the guest +* and the host, i.e. where a guest PMC is mapped to a host PMC with a +* different index. PEBS records hold a PERF_GLOBAL_STATUS snapshot, +* and so PEBS-enabled counters need to hold the correct index so as +* not to confuse the guest. +*/ + host_cross_mapped_mask =0 ; +kvm_for_each_pmc (pmu, pmc, bit, (unsigned long *)&pmu->global_ctrl) {if (!pmc_is_locally_enabled (pmc) || !pmc_is_globally_enabled (pmc) || !pmc->perf_event)
@@ -763,8 +773,9 @@ void intel_pmu_cross_mapped_check(struct kvm_pmu *pmu)*/ hw_idx = pmc->perf_event->hw.idx;if (hw_idx != pmc->idx && hw_idx >-1 ) - pmu->host_cross_mapped_mask |=BIT_ULL (hw_idx); + host_cross_mapped_mask |=BIT_ULL (hw_idx); } +return host_cross_mapped_mask; }static bool intel_pmu_is_mediated_pmu_supported (struct x86_pmu_capability *host_pmu)
arch/x86/kvm/vmx/vmx.c +8 -3
@@ -7363,12 +7363,17 @@ static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)if (kvm_vcpu_has_mediated_pmu (&vmx->vcpu))return ; - pmu->host_cross_mapped_mask =0 ; +struct x86_guest_pebs guest_pebs = { + .enable = pmu->pebs_enable, + .ds_area = pmu->ds_area, + .data_cfg = pmu->pebs_data_cfg, + }; +if (pmu->pebs_enable & pmu->global_ctrl) -intel_pmu_cross_mapped_check (pmu); + guest_pebs.cross_mapped_mask =intel_pmu_get_cross_mapped_mask (pmu);/* Note, nr_msrs may be garbage if perf_guest_get_msrs() returns NULL. */ - msrs =perf_guest_get_msrs (&nr_msrs, (void *)pmu); + msrs =perf_guest_get_msrs (&nr_msrs, &guest_pebs);if (!msrs)return ;
arch/x86/kvm/vmx/vmx.h +1 -1
@@ -664,7 +664,7 @@ static __always_inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) return container_of(vcpu, struct vcpu_vmx, vcpu); } -void intel_pmu_cross_mapped_check (struct kvm_pmu *pmu); +u64 intel_pmu_get_cross_mapped_mask (struct kvm_pmu *pmu);int intel_pmu_create_guest_lbr_event (struct kvm_vcpu *vcpu);void vmx_passthrough_lbr_msrs (struct kvm_vcpu *vcpu);
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 06/10] perf/x86: KVM: Have perf define a dedicated struct for getting guest PEBS data
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines
Have perf define a struct for getting guest PEBS data from KVM instead of poking into the
kvm_pmustructure. Passing in an entire "structkvm_pmu"_asan opaquepointer_to get at four fields is silly, especially since one of the fields exists purely to convey information to perf, i.e. isn't used by KVM.Perf should also own its APIs, i.e. define what fields/data it needs, not rely on KVM to throw fields into data structures that effectively hold KVM-internal state.
Quoted 6 lines
Opportunistically rephrase the comment about cross-mapped counters to explain *why* PEBS needs to be disabled.
Reviewed-by Dapeng Mi <[email protected]>Reviewed-by Jim Mattson <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
[tip: perf/core] perf/x86: KVM: Have perf define a dedicated struct for getting guest PEBS data
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: a634e4536ec22a17e2391fd5ed38c06ad99cbf99 Gitweb: https://git.kernel.org/tip/a634e4536ec22a17e2391fd5ed38c06ad99cbf99 Author: Sean Christopherson <[email protected]> AuthorDate: Mon, 21 Sep 2026 12:14:14 -07:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Fri, 25 Sep 2026 12:45:53 +02:00
perf/x86: KVM: Have perf define a dedicated struct for getting guest PEBS data
Have perf define a struct for getting guest PEBS data from KVM instead of poking into the kvm_pmu structure. Passing in an entire "struct kvm_pmu" _as an opaque pointer_ to get at four fields is silly, especially since one of the fields exists purely to convey information to perf, i.e. isn't used by KVM.
Perf should also own its APIs, i.e. define what fields/data it needs, not rely on KVM to throw fields into data structures that effectively hold KVM-internal state.
Opportunistically rephrase the comment about cross-mapped counters to explain *why* PEBS needs to be disabled.
--- arch/x86/events/core.c | 5 +++-- arch/x86/events/intel/core.c | 16 ++++++++-------- arch/x86/events/perf_event.h | 3 ++- arch/x86/include/asm/kvm_host.h | 9 --------- arch/x86/include/asm/perf_event.h | 12 ++++++++++-- arch/x86/kvm/vmx/pmu_intel.c | 17 ++++++++++++++--- arch/x86/kvm/vmx/vmx.c | 11 ++++++++--- arch/x86/kvm/vmx/vmx.h | 2 +- 8 files changed, 46 insertions(+), 29 deletions(-)
arch/x86/events/core.c +3 -2
@@ -862,9 +862,10 @@ void x86_pmu_disable_all(void) } } -struct perf_guest_switch_msr *perf_guest_get_msrs (int *nr,void *data) +struct perf_guest_switch_msr *perf_guest_get_msrs (int *nr, +struct x86_guest_pebs *guest_pebs) { -return static_call (x86_pmu_guest_get_msrs)(nr, data); +return static_call (x86_pmu_guest_get_msrs)(nr, guest_pebs); }EXPORT_SYMBOL_FOR_KVM (perf_guest_get_msrs);
arch/x86/events/intel/core.c +8 -8
@@ -14,7 +14,6 @@#include <linux/slab.h> #include <linux/export.h> #include <linux/nmi.h> -#include <linux/kvm_host.h> #include <asm/cpufeature.h> #include <asm/cpuid/api.h>
@@ -5328,11 +5327,11 @@ static int intel_pmu_hw_config(struct perf_event *event)* when it uses {RD,WR}MSR, which should be handled by the KVM context, * specifically in the intel_pmu_{get,set}_msr(). */ -static struct perf_guest_switch_msr *intel_guest_get_msrs (int *nr,void *data) +static struct perf_guest_switch_msr *intel_guest_get_msrs (int *nr, +struct x86_guest_pebs *guest_pebs) {struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events);struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs; -struct kvm_pmu *kvm_pmu = (struct kvm_pmu *)data;u64 intel_ctrl =hybrid (cpuc->pmu, intel_ctrl);u64 pebs_mask = cpuc->pebs_enabled & x86_pmu.pebs_capable;u64 guest_pebs_mask;
@@ -5386,7 +5385,7 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data)* the guest wants to use for PEBS, (c) are not excluded from counting * in the guest, and (d) _are_ excluded from counting in the host. */ - guest_pebs_mask = pebs_mask & intel_ctrl & kvm_pmu->pebs_enable & + guest_pebs_mask = pebs_mask & intel_ctrl & guest_pebs->enable & ~cpuc->intel_ctrl_exclude_guest_mask & cpuc->intel_ctrl_exclude_host_mask;
@@ -5396,7 +5395,7 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data)* PERF_GLOBAL_STATUS, i.e. the guest will see overflow status for the * wrong counter(s). */ - guest_pebs_mask &= ~kvm_pmu->host_cross_mapped_mask; + guest_pebs_mask &= ~guest_pebs->cross_mapped_mask;/* * FIXME: Allow guest and host usage of PEBS events to co-exist instead
@@ -5415,14 +5414,14 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data) arr[(*nr)++] = (struct perf_guest_switch_msr ){ .msr =MSR_IA32_DS_AREA , .host = (unsigned long )cpuc->ds, - .guest = guest_pebs_mask ? kvm_pmu->ds_area : (unsigned long )cpuc->ds, + .guest = guest_pebs_mask ? guest_pebs->ds_area : (unsigned long )cpuc->ds, };if (x86_pmu.intel_cap.pebs_baseline) { arr[(*nr)++] = (struct perf_guest_switch_msr ){ .msr =MSR_PEBS_DATA_CFG , .host = cpuc->active_pebs_data_cfg, - .guest = guest_pebs_mask ? kvm_pmu->pebs_data_cfg : + .guest = guest_pebs_mask ? guest_pebs->data_cfg : cpuc->active_pebs_data_cfg, }; }
@@ -5438,7 +5437,8 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data)return arr; } -static struct perf_guest_switch_msr *core_guest_get_msrs (int *nr,void *data) +static struct perf_guest_switch_msr *core_guest_get_msrs (int *nr, +struct x86_guest_pebs *guest_pebs) {struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events);struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs;
arch/x86/events/perf_event.h +2 -1
@@ -1226,7 +1226,8 @@ struct x86_pmu {/* * Intel host/guest support (KVM) */ -struct perf_guest_switch_msr *(*guest_get_msrs)(int *nr,void *data); +struct perf_guest_switch_msr *(*guest_get_msrs)(int *nr, +struct x86_guest_pebs *guest_pebs);/* * Check period value for PERF_EVENT_IOC_PERIOD ioctl.
arch/x86/include/asm/kvm_host.h +0 -9
@@ -609,15 +609,6 @@ struct kvm_pmu {u64 pebs_data_cfg_rsvd;/* -* If a guest counter is cross-mapped to host counter with different -* index, its PEBS capability will be temporarily disabled. -* -* The user should make sure that this mask is updated -* after disabling interrupts and before perf_guest_get_msrs(); -*/ -u64 host_cross_mapped_mask; - -/* * The gate to release perf_events not marked in * pmc_in_use only once in a vcpu time slice. */
arch/x86/include/asm/perf_event.h +10 -2
@@ -819,11 +819,19 @@ extern void perf_load_guest_lvtpc(u32 guest_lvtpc);extern void perf_put_guest_lvtpc (void );#endif +struct x86_guest_pebs { +u64 enable; +u64 ds_area; +u64 data_cfg; +u64 cross_mapped_mask; +};#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 struct perf_guest_switch_msr *perf_guest_get_msrs (int *nr, +struct x86_guest_pebs *guest_pebs);extern void x86_perf_get_lbr (struct x86_pmu_lbr *lbr);#else -struct perf_guest_switch_msr *perf_guest_get_msrs (int *nr,void *data); +struct perf_guest_switch_msr *perf_guest_get_msrs (int *nr, +struct x86_guest_pebs *guest_pebs);static inline void x86_perf_get_lbr (struct x86_pmu_lbr *lbr) {memset (lbr,0 ,sizeof (*lbr));
arch/x86/kvm/vmx/pmu_intel.c +14 -3
@@ -747,11 +747,21 @@ static void intel_pmu_cleanup(struct kvm_vcpu *vcpu)intel_pmu_release_guest_lbr_event (vcpu); } -void intel_pmu_cross_mapped_check (struct kvm_pmu *pmu) +u64 intel_pmu_get_cross_mapped_mask (struct kvm_pmu *pmu) { -struct kvm_pmc *pmc =NULL ; +u64 host_cross_mapped_mask; +struct kvm_pmc *pmc;int bit, hw_idx; +/* +* Provide a mask of counters that are cross-mapped between the guest +* and the host, i.e. where a guest PMC is mapped to a host PMC with a +* different index. PEBS records hold a PERF_GLOBAL_STATUS snapshot, +* and so PEBS-enabled counters need to hold the correct index so as +* not to confuse the guest. +*/ + host_cross_mapped_mask =0 ; +kvm_for_each_pmc (pmu, pmc, bit, (unsigned long *)&pmu->global_ctrl) {if (!pmc_is_locally_enabled (pmc) || !pmc_is_globally_enabled (pmc) || !pmc->perf_event)
@@ -763,8 +773,9 @@ void intel_pmu_cross_mapped_check(struct kvm_pmu *pmu)*/ hw_idx = pmc->perf_event->hw.idx;if (hw_idx != pmc->idx && hw_idx >-1 ) - pmu->host_cross_mapped_mask |=BIT_ULL (hw_idx); + host_cross_mapped_mask |=BIT_ULL (hw_idx); } +return host_cross_mapped_mask; }static bool intel_pmu_is_mediated_pmu_supported (struct x86_pmu_capability *host_pmu)
arch/x86/kvm/vmx/vmx.c +8 -3
@@ -7363,12 +7363,17 @@ static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)if (kvm_vcpu_has_mediated_pmu (&vmx->vcpu))return ; - pmu->host_cross_mapped_mask =0 ; +struct x86_guest_pebs guest_pebs = { + .enable = pmu->pebs_enable, + .ds_area = pmu->ds_area, + .data_cfg = pmu->pebs_data_cfg, + }; +if (pmu->pebs_enable & pmu->global_ctrl) -intel_pmu_cross_mapped_check (pmu); + guest_pebs.cross_mapped_mask =intel_pmu_get_cross_mapped_mask (pmu);/* Note, nr_msrs may be garbage if perf_guest_get_msrs() returns NULL. */ - msrs =perf_guest_get_msrs (&nr_msrs, (void *)pmu); + msrs =perf_guest_get_msrs (&nr_msrs, &guest_pebs);if (!msrs)return ;
arch/x86/kvm/vmx/vmx.h +1 -1
@@ -664,7 +664,7 @@ static __always_inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) return container_of(vcpu, struct vcpu_vmx, vcpu); } -void intel_pmu_cross_mapped_check (struct kvm_pmu *pmu); +u64 intel_pmu_get_cross_mapped_mask (struct kvm_pmu *pmu);int intel_pmu_create_guest_lbr_event (struct kvm_vcpu *vcpu);void vmx_passthrough_lbr_msrs (struct kvm_vcpu *vcpu);
Sean Christopherson
maintainerPATCH v4 7/10 perf/x86/intel: KVM: Handle cross-mapped PEBS PMCs entirely within KVM
In reply to Sean ChristophersonNow that perf operates on a KVM-provided snapshot of PMU state, handled cross-mapped PEBS counters entirely in KVM by clearing unusable counters from the to-be-enabled mask instead of foisting the work on perf.
No functional change intended.
--- arch/x86/events/intel/core.c | 8 -------- arch/x86/include/asm/perf_event.h | 1 - arch/x86/kvm/vmx/vmx.c | 10 ++++++++-- 3 files changed, 8 insertions(+), 11 deletions(-)
arch/x86/events/intel/core.c +0 -8
@@ -5379,14 +5379,6 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, ~cpuc->intel_ctrl_exclude_guest_mask & cpuc->intel_ctrl_exclude_host_mask; -/* -* Disable counters where the guest PMC is different than the host PMC -* being used on behalf of the guest, as the PEBS record includes -* PERF_GLOBAL_STATUS, i.e. the guest will see overflow status for the -* wrong counter(s). -*/ - guest_pebs_mask &= ~guest_pebs->cross_mapped_mask; -/* * FIXME: Allow guest and host usage of PEBS events to co-exist instead * of disabling guest PEBS entirely if the host is using PEBS.
arch/x86/include/asm/perf_event.h +0 -1
@@ -823,7 +823,6 @@ struct x86_guest_pebs {u64 enable;u64 ds_area;u64 data_cfg; -u64 cross_mapped_mask; };#if defined(CONFIG_PERF_EVENTS ) && defined(CONFIG_CPU_SUP_INTEL ) extern struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr,
arch/x86/kvm/vmx/vmx.c +8 -2
@@ -7369,8 +7369,14 @@ static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) .data_cfg = pmu->pebs_data_cfg, }; -if (pmu->pebs_enable & pmu->global_ctrl) - guest_pebs.cross_mapped_mask =intel_pmu_get_cross_mapped_mask (pmu); +/* +* Disable counters where the guest PMC is different than the host PMC +* being used on behalf of the guest, as the PEBS record includes +* PERF_GLOBAL_STATUS, i.e. the guest will see overflow status for the +* wrong counter(s). +*/ +if (guest_pebs.enable & pmu->global_ctrl) + guest_pebs.enable &= ~intel_pmu_get_cross_mapped_mask (pmu);/* Note, nr_msrs may be garbage if perf_guest_get_msrs() returns NULL. */ msrs =perf_guest_get_msrs (&nr_msrs, &guest_pebs);
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 07/10] perf/x86/intel: KVM: Handle cross-mapped PEBS PMCs entirely within KVM
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines
Now that perf operates on a KVM-provided snapshot of PMU state, handled cross-mapped PEBS counters entirely in KVM by clearing unusable counters from the to-be-enabled mask instead of foisting the work on perf.
No functional change intended.
Reviewed-by Dapeng Mi <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
[tip: perf/core] perf/x86/intel: KVM: Handle cross-mapped PEBS PMCs entirely within KVM
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: 31f7cf337ce717666ce29fbd793f06435eb48408 Gitweb: https://git.kernel.org/tip/31f7cf337ce717666ce29fbd793f06435eb48408 Author: Sean Christopherson <[email protected]> AuthorDate: Mon, 21 Sep 2026 12:14:15 -07:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Fri, 25 Sep 2026 12:45:53 +02:00
perf/x86/intel: KVM: Handle cross-mapped PEBS PMCs entirely within KVM
Now that perf operates on a KVM-provided snapshot of PMU state, handled cross-mapped PEBS counters entirely in KVM by clearing unusable counters from the to-be-enabled mask instead of foisting the work on perf.
No functional change intended.
--- arch/x86/events/intel/core.c | 8 -------- arch/x86/include/asm/perf_event.h | 1 - arch/x86/kvm/vmx/vmx.c | 10 ++++++++-- 3 files changed, 8 insertions(+), 11 deletions(-)
arch/x86/events/intel/core.c +0 -8
@@ -5390,14 +5390,6 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, cpuc->intel_ctrl_exclude_host_mask;/* -* Disable counters where the guest PMC is different than the host PMC -* being used on behalf of the guest, as the PEBS record includes -* PERF_GLOBAL_STATUS, i.e. the guest will see overflow status for the -* wrong counter(s). -*/ - guest_pebs_mask &= ~guest_pebs->cross_mapped_mask; - -/* * FIXME: Allow guest and host usage of PEBS events to co-exist instead * of disabling guest PEBS entirely if the host is using PEBS. * What exactly goes wrong if guest and host are using PEBS is
arch/x86/include/asm/perf_event.h +0 -1
@@ -823,7 +823,6 @@ struct x86_guest_pebs {u64 enable;u64 ds_area;u64 data_cfg; -u64 cross_mapped_mask; };#if defined(CONFIG_PERF_EVENTS ) && defined(CONFIG_CPU_SUP_INTEL ) extern struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr,
arch/x86/kvm/vmx/vmx.c +8 -2
@@ -7369,8 +7369,14 @@ static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) .data_cfg = pmu->pebs_data_cfg, }; -if (pmu->pebs_enable & pmu->global_ctrl) - guest_pebs.cross_mapped_mask =intel_pmu_get_cross_mapped_mask (pmu); +/* +* Disable counters where the guest PMC is different than the host PMC +* being used on behalf of the guest, as the PEBS record includes +* PERF_GLOBAL_STATUS, i.e. the guest will see overflow status for the +* wrong counter(s). +*/ +if (guest_pebs.enable & pmu->global_ctrl) + guest_pebs.enable &= ~intel_pmu_get_cross_mapped_mask (pmu);/* Note, nr_msrs may be garbage if perf_guest_get_msrs() returns NULL. */ msrs =perf_guest_get_msrs (&nr_msrs, &guest_pebs);
Sean Christopherson
maintainerPATCH v4 8/10 KVM: VMX: Drop a redundant pmu->global_ctrl check when processing pebs_enable
In reply to Sean ChristophersonDrop a redundant check that a PMC is globally enabled when looking for PEBS counters that are cross-mapped between the guest and the host. The for-loop explicitly iterates over pmu->global_ctrl, and since PEBS requires PMU v2+, kvm_pmu_has_perf_global_ctrl() must be true, and thus pmc_is_globally_enabled() is simply checking that the bit is set in pmu->global_ctrl.
No functional change intended.
--- arch/x86/kvm/vmx/pmu_intel.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +1 -2
@@ -763,8 +763,7 @@ u64 intel_pmu_get_cross_mapped_mask(struct kvm_pmu *pmu) host_cross_mapped_mask =0 ;kvm_for_each_pmc (pmu, pmc, bit, (unsigned long *)&pmu->global_ctrl) { -if (!pmc_is_locally_enabled (pmc) || - !pmc_is_globally_enabled (pmc) || !pmc->perf_event) +if (!pmc_is_locally_enabled (pmc) || !pmc->perf_event)continue ;/*
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 08/10] KVM: VMX: Drop a redundant pmu->global_ctrl check when processing pebs_enable
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines
Drop a redundant check that a PMC is globally enabled when looking for PEBS counters that are cross-mapped between the guest and the host. The for-loop explicitly iterates over
pmu->global_ctrl, and since PEBS requires PMU v2+,kvm_pmu_has_perf_global_ctrl()must be true, and thuspmc_is_globally_enabled()is simply checking that the bit is set inpmu->global_ctrl.No functional change intended.
Reviewed-by Dapeng Mi <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
[tip: perf/core] KVM: VMX: Drop a redundant pmu->global_ctrl check when processing pebs_enable
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: 57c76478333012159b5cbec89527f925011cdefd Gitweb: https://git.kernel.org/tip/57c76478333012159b5cbec89527f925011cdefd Author: Sean Christopherson <[email protected]> AuthorDate: Mon, 21 Sep 2026 12:14:16 -07:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Fri, 25 Sep 2026 12:45:53 +02:00
KVM: VMX: Drop a redundant pmu->global_ctrl check when processing pebs_enable
Drop a redundant check that a PMC is globally enabled when looking for PEBS counters that are cross-mapped between the guest and the host. The for-loop explicitly iterates over pmu->global_ctrl, and since PEBS requires PMU v2+, kvm_pmu_has_perf_global_ctrl() must be true, and thus pmc_is_globally_enabled() is simply checking that the bit is set in pmu->global_ctrl.
No functional change intended.
--- arch/x86/kvm/vmx/pmu_intel.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +1 -2
@@ -763,8 +763,7 @@ u64 intel_pmu_get_cross_mapped_mask(struct kvm_pmu *pmu) host_cross_mapped_mask =0 ;kvm_for_each_pmc (pmu, pmc, bit, (unsigned long *)&pmu->global_ctrl) { -if (!pmc_is_locally_enabled (pmc) || - !pmc_is_globally_enabled (pmc) || !pmc->perf_event) +if (!pmc_is_locally_enabled (pmc) || !pmc->perf_event)continue ;/*
Sean Christopherson
maintainerPATCH v4 9/10 KVM: VMX: Only tell perf to enable PEBS counters for fully enabled PMCs
In reply to Sean ChristophersonWhen passing the guest's requested PEBS_ENABLE (or rather, KVM's version of PEBS_ENABLE on behalf of the guest), omit counters that are locally disable and/or don't have a perf event (due to contention), in addition to omitting counters that are cross-mapped in the host.
In practice, this should be a nop as perf will already have disabled the associated counter, i.e. cpuc->pebs_enabled should have been cleared, but paranoia is cheap, and the existing code _looks_ wrong.
--- arch/x86/kvm/vmx/pmu_intel.c | 30 ++++++++++++++++-------------- arch/x86/kvm/vmx/vmx.c | 11 +---------- arch/x86/kvm/vmx/vmx.h | 15 ++++++++++++++- 3 files changed, 31 insertions(+), 25 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +16 -14
@@ -747,34 +747,36 @@ static void intel_pmu_cleanup(struct kvm_vcpu *vcpu)intel_pmu_release_guest_lbr_event (vcpu); } -u64 intel_pmu_get_cross_mapped_mask (struct kvm_pmu *pmu) +u64 __intel_pmu_compute_pebs_enable (struct kvm_pmu *pmu) { -u64 host_cross_mapped_mask; +u64 guest_pebs_enable = pmu->pebs_enable & pmu->global_ctrl; +u64 pebs_enable =0 ;struct kvm_pmc *pmc;int bit, hw_idx;/* -* Provide a mask of counters that are cross-mapped between the guest -* and the host, i.e. where a guest PMC is mapped to a host PMC with a -* different index. PEBS records hold a PERF_GLOBAL_STATUS snapshot, -* and so PEBS-enabled counters need to hold the correct index so as -* not to confuse the guest. +* Omit counters that are locally disabled, don't have a perf event, or +* ended up with a perf event that is using a different counter than +* the guest, i.e. where the guest PMC is different than the host PMC +* being used on behalf of the guest. PEBS records include +* PERF_GLOBAL_STATUS, and so using a counter with a different index +* means the guest will see overflow status for the wrong counter(s). */ - host_cross_mapped_mask =0 ; - -kvm_for_each_pmc (pmu, pmc, bit, (unsigned long *)&pmu->global_ctrl) { +kvm_for_each_pmc (pmu, pmc, bit, (unsigned long *)&guest_pebs_enable) {if (!pmc_is_locally_enabled (pmc) || !pmc->perf_event)continue ;/* -* A negative index indicates the event isn't mapped to a +* Note, a negative index indicates the event isn't mapped to a * physical counter in the host, e.g. due to contention. */ hw_idx = pmc->perf_event->hw.idx; -if (hw_idx != pmc->idx && hw_idx >-1 ) - host_cross_mapped_mask |=BIT_ULL (hw_idx); +if (hw_idx != pmc->idx) +continue ; + + pebs_enable |=BIT_ULL (pmc->idx); } -return host_cross_mapped_mask; +return pebs_enable; }static bool intel_pmu_is_mediated_pmu_supported (struct x86_pmu_capability *host_pmu)
arch/x86/kvm/vmx/vmx.c +1 -10
@@ -7364,20 +7364,11 @@ static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)return ;struct x86_guest_pebs guest_pebs = { - .enable = pmu->pebs_enable, + .enable =intel_pmu_compute_pebs_enable (pmu), .ds_area = pmu->ds_area, .data_cfg = pmu->pebs_data_cfg, }; -/* -* Disable counters where the guest PMC is different than the host PMC -* being used on behalf of the guest, as the PEBS record includes -* PERF_GLOBAL_STATUS, i.e. the guest will see overflow status for the -* wrong counter(s). -*/ -if (guest_pebs.enable & pmu->global_ctrl) - guest_pebs.enable &= ~intel_pmu_get_cross_mapped_mask (pmu); -/* Note, nr_msrs may be garbage if perf_guest_get_msrs() returns NULL. */ msrs =perf_guest_get_msrs (&nr_msrs, &guest_pebs);if (!msrs)
arch/x86/kvm/vmx/vmx.h +14 -1
@@ -664,7 +664,20 @@ static __always_inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) return container_of(vcpu, struct vcpu_vmx, vcpu); } -u64 intel_pmu_get_cross_mapped_mask (struct kvm_pmu *pmu); +u64 __intel_pmu_compute_pebs_enable (struct kvm_pmu *pmu); + +static inline u64 intel_pmu_compute_pebs_enable (struct kvm_pmu *pmu) +{ +/* +* Avoid the function call overhead in the common case that the guest +* isn't using PEBS. +*/ +if (!(pmu->pebs_enable & pmu->global_ctrl)) +return 0 ; + +return __intel_pmu_compute_pebs_enable (pmu); +} +int intel_pmu_create_guest_lbr_event (struct kvm_vcpu *vcpu);void vmx_passthrough_lbr_msrs (struct kvm_vcpu *vcpu);
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 09/10] KVM: VMX: Only tell perf to enable PEBS counters for fully enabled PMCs
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines
When passing the guest's requested
PEBS_ENABLE(or rather, KVM's version ofPEBS_ENABLEon behalf of the guest), omit counters that are locally disable and/or don't have a perf event (due to contention), in addition to omitting counters that are cross-mapped in the host.In practice, this should be a nop as perf will already have disabled the associated counter, i.e.
cpuc->pebs_enabledshould have been cleared, but paranoia is cheap, and the existing code _looks_ wrong.Reviewed-by Dapeng Mi <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
[tip: perf/core] KVM: VMX: Only tell perf to enable PEBS counters for fully enabled PMCs
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: 193ef44e32619550478fce9d0b91edc0046ed8c3 Gitweb: https://git.kernel.org/tip/193ef44e32619550478fce9d0b91edc0046ed8c3 Author: Sean Christopherson <[email protected]> AuthorDate: Mon, 21 Sep 2026 12:14:17 -07:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Fri, 25 Sep 2026 12:45:54 +02:00
KVM: VMX: Only tell perf to enable PEBS counters for fully enabled PMCs
When passing the guest's requested PEBS_ENABLE (or rather, KVM's version of PEBS_ENABLE on behalf of the guest), omit counters that are locally disable and/or don't have a perf event (due to contention), in addition to omitting counters that are cross-mapped in the host.
In practice, this should be a nop as perf will already have disabled the associated counter, i.e. cpuc->pebs_enabled should have been cleared, but paranoia is cheap, and the existing code _looks_ wrong.
--- arch/x86/kvm/vmx/pmu_intel.c | 30 ++++++++++++++++-------------- arch/x86/kvm/vmx/vmx.c | 11 +---------- arch/x86/kvm/vmx/vmx.h | 15 ++++++++++++++- 3 files changed, 31 insertions(+), 25 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +16 -14
@@ -747,34 +747,36 @@ static void intel_pmu_cleanup(struct kvm_vcpu *vcpu)intel_pmu_release_guest_lbr_event (vcpu); } -u64 intel_pmu_get_cross_mapped_mask (struct kvm_pmu *pmu) +u64 __intel_pmu_compute_pebs_enable (struct kvm_pmu *pmu) { -u64 host_cross_mapped_mask; +u64 guest_pebs_enable = pmu->pebs_enable & pmu->global_ctrl; +u64 pebs_enable =0 ;struct kvm_pmc *pmc;int bit, hw_idx;/* -* Provide a mask of counters that are cross-mapped between the guest -* and the host, i.e. where a guest PMC is mapped to a host PMC with a -* different index. PEBS records hold a PERF_GLOBAL_STATUS snapshot, -* and so PEBS-enabled counters need to hold the correct index so as -* not to confuse the guest. +* Omit counters that are locally disabled, don't have a perf event, or +* ended up with a perf event that is using a different counter than +* the guest, i.e. where the guest PMC is different than the host PMC +* being used on behalf of the guest. PEBS records include +* PERF_GLOBAL_STATUS, and so using a counter with a different index +* means the guest will see overflow status for the wrong counter(s). */ - host_cross_mapped_mask =0 ; - -kvm_for_each_pmc (pmu, pmc, bit, (unsigned long *)&pmu->global_ctrl) { +kvm_for_each_pmc (pmu, pmc, bit, (unsigned long *)&guest_pebs_enable) {if (!pmc_is_locally_enabled (pmc) || !pmc->perf_event)continue ;/* -* A negative index indicates the event isn't mapped to a +* Note, a negative index indicates the event isn't mapped to a * physical counter in the host, e.g. due to contention. */ hw_idx = pmc->perf_event->hw.idx; -if (hw_idx != pmc->idx && hw_idx >-1 ) - host_cross_mapped_mask |=BIT_ULL (hw_idx); +if (hw_idx != pmc->idx) +continue ; + + pebs_enable |=BIT_ULL (pmc->idx); } -return host_cross_mapped_mask; +return pebs_enable; }static bool intel_pmu_is_mediated_pmu_supported (struct x86_pmu_capability *host_pmu)
arch/x86/kvm/vmx/vmx.c +1 -10
@@ -7364,20 +7364,11 @@ static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)return ;struct x86_guest_pebs guest_pebs = { - .enable = pmu->pebs_enable, + .enable =intel_pmu_compute_pebs_enable (pmu), .ds_area = pmu->ds_area, .data_cfg = pmu->pebs_data_cfg, }; -/* -* Disable counters where the guest PMC is different than the host PMC -* being used on behalf of the guest, as the PEBS record includes -* PERF_GLOBAL_STATUS, i.e. the guest will see overflow status for the -* wrong counter(s). -*/ -if (guest_pebs.enable & pmu->global_ctrl) - guest_pebs.enable &= ~intel_pmu_get_cross_mapped_mask (pmu); -/* Note, nr_msrs may be garbage if perf_guest_get_msrs() returns NULL. */ msrs =perf_guest_get_msrs (&nr_msrs, &guest_pebs);if (!msrs)
arch/x86/kvm/vmx/vmx.h +14 -1
@@ -664,7 +664,20 @@ static __always_inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) return container_of(vcpu, struct vcpu_vmx, vcpu); } -u64 intel_pmu_get_cross_mapped_mask (struct kvm_pmu *pmu); +u64 __intel_pmu_compute_pebs_enable (struct kvm_pmu *pmu); + +static inline u64 intel_pmu_compute_pebs_enable (struct kvm_pmu *pmu) +{ +/* +* Avoid the function call overhead in the common case that the guest +* isn't using PEBS. +*/ +if (!(pmu->pebs_enable & pmu->global_ctrl)) +return 0 ; + +return __intel_pmu_compute_pebs_enable (pmu); +} +int intel_pmu_create_guest_lbr_event (struct kvm_vcpu *vcpu);void vmx_passthrough_lbr_msrs (struct kvm_vcpu *vcpu);
Sean Christopherson
maintainerPATCH v4 10/10 perf/x86/intel: Check only PMC bits in PEBS_ENABLED when detecting host PEBS usage
In reply to Sean ChristophersonWhen checking to see if guest PEBS usage should be disabled due to host PEBS usage, filter out non-counter bits in PEBS_ENABLE to avoid getting false positives in the unlikely scenario that "Enable Precise Store" is enabled without PEBS actually being active for any PMCs.
Note, the other "hidden" usage of pebs_mask is already explicitly masked with intel_ctrl:
.guest = intel_ctrl & ~cpuc->intel_ctrl_exclude_guest_mask & ~pebs_mask,
I.e. removing the non-counter bits from pebs_mask doesn't change the value that is loaded into arr[global_ctrl].guest.
--- arch/x86/events/intel/core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/events/intel/core.c +2 -2
@@ -5323,7 +5323,7 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr,struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events);struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs;u64 intel_ctrl =hybrid (cpuc->pmu, intel_ctrl); -u64 pebs_mask = cpuc->pebs_enabled & x86_pmu.pebs_capable; +u64 pebs_mask = intel_ctrl & cpuc->pebs_enabled & x86_pmu.pebs_capable;u64 guest_pebs_mask;int global_ctrl;
@@ -5375,7 +5375,7 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr,* the guest wants to use for PEBS, (c) are not excluded from counting * in the guest, and (d) _are_ excluded from counting in the host. */ - guest_pebs_mask = pebs_mask & intel_ctrl & guest_pebs->enable & + guest_pebs_mask = pebs_mask & guest_pebs->enable & ~cpuc->intel_ctrl_exclude_guest_mask & cpuc->intel_ctrl_exclude_host_mask;
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 10/10] perf/x86/intel: Check only PMC bits in PEBS_ENABLED when detecting host PEBS usage
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines
When checking to see if guest PEBS usage should be disabled due to host PEBS usage, filter out non-counter bits in
PEBS_ENABLEto avoid getting false positives in the unlikely scenario that "Enable Precise Store" is enabled without PEBS actually being active for any PMCs.Note, the other "hidden" usage of
pebs_maskis already explicitly masked withintel_ctrl:.guest =
intel_ctrl& ~cpuc->intel_ctrl_exclude_guest_mask & ~pebs_mask,
Quoted 8 lines
I.e. removing the non-counter bits from
pebs_maskdoesn't change the value that is loaded into arr[global_ctrl].guest.Fixes 854250329c02 ("KVM: x86/pmu: Disable guest PEBS temporarily in two rare situations")Suggested-by Dapeng Mi <[email protected]>Reported-by Sashiko Bot <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Re: [PATCH v4 10/10] perf/x86/intel: Check only PMC bits in PEBS_ENABLED when detecting host PEBS usage
In reply to Sean ChristophersonThanks.
Quoted 44 lines · Sean Christopherson
When checking to see if guest PEBS usage should be disabled due to host PEBS usage, filter out non-counter bits in
PEBS_ENABLEto avoid getting false positives in the unlikely scenario that "Enable Precise Store" is enabled without PEBS actually being active for any PMCs.Note, the other "hidden" usage of
pebs_maskis already explicitly masked withintel_ctrl:.guest =
intel_ctrl& ~cpuc->intel_ctrl_exclude_guest_mask & ~pebs_mask,I.e. removing the non-counter bits from
pebs_maskdoesn't change the value that is loaded into arr[global_ctrl].guest.Fixes 854250329c02 ("KVM: x86/pmu: Disable guest PEBS temporarily in two rare situations")Suggested-by Dapeng Mi <[email protected]>Reported-by Sashiko Bot <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/events/intel/core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/x86/events/intel/core.c +2
-2@@ -5323,7 +5323,7 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr,struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events);struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs;u64 intel_ctrl =hybrid (cpuc->pmu, intel_ctrl); -u64 pebs_mask = cpuc->pebs_enabled & x86_pmu.pebs_capable; +u64 pebs_mask = intel_ctrl & cpuc->pebs_enabled & x86_pmu.pebs_capable;u64 guest_pebs_mask;int global_ctrl;@@ -5375,7 +5375,7 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr,* the guest wants to use for PEBS, (c) are not excluded from counting * in the guest, and (d) _are_ excluded from counting in the host. */ - guest_pebs_mask = pebs_mask & intel_ctrl & guest_pebs->enable & + guest_pebs_mask = pebs_mask & guest_pebs->enable & ~cpuc->intel_ctrl_exclude_guest_mask & cpuc->intel_ctrl_exclude_host_mask;
[tip: perf/core] perf/x86/intel: Check only PMC bits in PEBS_ENABLED when detecting host PEBS usage
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: ba29babd881e09a7bb8b01fcfeda3d637d8115c5 Gitweb: https://git.kernel.org/tip/ba29babd881e09a7bb8b01fcfeda3d637d8115c5 Author: Sean Christopherson <[email protected]> AuthorDate: Mon, 21 Sep 2026 12:14:18 -07:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Fri, 25 Sep 2026 12:45:54 +02:00
perf/x86/intel: Check only PMC bits in PEBS_ENABLED when detecting host PEBS usage
When checking to see if guest PEBS usage should be disabled due to host PEBS usage, filter out non-counter bits in PEBS_ENABLE to avoid getting false positives in the unlikely scenario that "Enable Precise Store" is enabled without PEBS actually being active for any PMCs.
Note, the other "hidden" usage of pebs_mask is already explicitly masked with intel_ctrl:
.guest = intel_ctrl & ~cpuc->intel_ctrl_exclude_guest_mask & ~pebs_mask,
I.e. removing the non-counter bits from pebs_mask doesn't change the value that is loaded into arr[global_ctrl].guest.
--- arch/x86/events/intel/core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/events/intel/core.c +2 -2
@@ -5333,7 +5333,7 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr,struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events);struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs;u64 intel_ctrl =hybrid (cpuc->pmu, intel_ctrl); -u64 pebs_mask = cpuc->pebs_enabled & x86_pmu.pebs_capable; +u64 pebs_mask = intel_ctrl & cpuc->pebs_enabled & x86_pmu.pebs_capable;u64 guest_pebs_mask;int global_ctrl;
@@ -5385,7 +5385,7 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr,* the guest wants to use for PEBS, (c) are not excluded from counting * in the guest, and (d) _are_ excluded from counting in the host. */ - guest_pebs_mask = pebs_mask & intel_ctrl & guest_pebs->enable & + guest_pebs_mask = pebs_mask & guest_pebs->enable & ~cpuc->intel_ctrl_exclude_guest_mask & cpuc->intel_ctrl_exclude_host_mask;
Peter Zijlstra
maintainerQuoted 20 lines · Sean Christopherson
Sean Christopherson (10):
perf/x86/intel: Ensure guest PEBS path doesn't set unwanted
PERF_GLOBAL_CTRLbits
perf/x86/intel: Don't writePEBS_ENABLEDon host<=>guest xfers if CPU
has isolation
perf/x86/intel: Don't context switchDS_AREA(and PEBS config) if PEBS
is unused
perf/x86/intel: Make @data a mandatory param for
intel_guest_get_msrs()
perf/x86/intel: Invert names of intel_ctrl_{guest,host}_mask perf/x86: KVM: Have perf define a dedicated struct for getting guest
PEBS data
perf/x86/intel: KVM: Handle cross-mapped PEBS PMCs entirely within KVM
KVM: VMX: Drop a redundantpmu->global_ctrlcheck when processing
pebs_enable
KVM: VMX: Only tell perf to enable PEBS counters for fully enabled
PMCs
perf/x86/intel: Check only PMC bits inPEBS_ENABLEDwhen detecting
host PEBS usage
Quoted 10 lines
arch/x86/events/core.c | 5 +- arch/x86/events/intel/core.c | 94 +++++++++++++++++++------------ arch/x86/events/intel/lbr.c | 2 +- arch/x86/events/perf_event.h | 7 ++- arch/x86/include/asm/kvm_host.h | 9 --- arch/x86/include/asm/perf_event.h | 11 +++- arch/x86/kvm/vmx/pmu_intel.c | 28 ++++++--- arch/x86/kvm/vmx/vmx.c | 10 ++-- arch/x86/kvm/vmx/vmx.h | 15 ++++- 9 files changed, 115 insertions(+), 66 deletions(-)
Thanks, split in two and queued in perf/urgent and perf/core. I'll push to tip in a day or so unless the robots scream.