* [PATCH 0/2] perf/x86: Fix races when loading guest mediated PMU context
@ 2026-12-01 17:14 Yosry Ahmed
2026-12-01 17:14 ` [PATCH 1/2] perf/x86: Prevent races between NMIs and switching LVTPC Yosry Ahmed
` (2 more replies)
0 siblings, 3 replies; 16+ messages in thread
From: Yosry Ahmed @ 2026-12-01 17:14 UTC (permalink / raw)
To: Peter Zijlstra, Sean Christopherson
Cc: Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter,
James Clark, Paolo Bonzini, x86, Dapeng Mi, Mingwei Zhang,
linux-perf-users, kvm, linux-kernel, Yosry Ahmed
Fix a couple of race conditions that can happen while loading the guest
mediated PMU context, resulting in either clobbered PMI handling or
leaking host events into guest counters.
The series is based on tip/perf/urgent.
An LLM was used to reproduce both issues (using kprobe + BPF to force
races), and verify the fixes.
Yosry Ahmed (2):
perf/x86: Prevent races between NMIs and switching LVTPC
KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
arch/x86/events/core.c | 13 ++++++++++++-
arch/x86/kvm/pmu.c | 8 ++++++--
2 files changed, 18 insertions(+), 3 deletions(-)
base-commit: 67b34211a9eb103aba474bc179fab3dd2912f07e
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH 1/2] perf/x86: Prevent races between NMIs and switching LVTPC
2026-12-01 17:14 [PATCH 0/2] perf/x86: Fix races when loading guest mediated PMU context Yosry Ahmed
@ 2026-12-01 17:14 ` Yosry Ahmed
2026-12-01 23:56 ` Sean Christopherson
2026-12-02 2:44 ` Mi, Dapeng
2026-12-01 17:14 ` [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC Yosry Ahmed
2026-12-01 17:20 ` [PATCH 0/2] perf/x86: Fix races when loading guest mediated PMU context sashiko-bot
2 siblings, 2 replies; 16+ messages in thread
From: Yosry Ahmed @ 2026-12-01 17:14 UTC (permalink / raw)
To: Peter Zijlstra, Sean Christopherson
Cc: Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter,
James Clark, Paolo Bonzini, x86, Dapeng Mi, Mingwei Zhang,
linux-perf-users, kvm, linux-kernel, Yosry Ahmed, stable
perf_event_nmi_handler() checks guest_lvtpc_loaded to skip handling NMIs
and unmasking LVTPC on Intel (as it is automasked by hardware on PMI).
However, when loading the guest LVTPC, perf_load_guest_lvtpc() updates
APIC_LVTPC before setting guest_lvtpc_loaded. If an NMI arrives in
between, perf_event_nmi_handler() will observe !guest_lvtpc_loaded and
proceed.
On Intel, the NMI handler will unconditionally write APIC_LVTPC to
unmask PMIs, overwriting it to APIC_DM_NMI and clobbering the guest PMI
vector. The first guest PMI is delivered as an unknown host NMI (which
results in a panic if unknown_nmi_panic/panic_on_unrecovered_nmi is
set). Subsequent guest PMIs will be completely lost because
perf_event_nmi_handler() will observe guest_lvtpc_loaded and bail
without unmasking LVTPC. Recovery only happens when
perf_put_guest_lvtpc() restores unmasked host NMIs in APIC_LVTPC.
Set guest_lvtpc_loaded before writing APIC_LVTPC in
perf_load_guest_lvtpc() to prevent a racing NMI from clobbering LVTPC.
The current ordering of perf_put_guest_lvtpc() is correct,
guest_lvtpc_loaded should be cleared before writing APIC_LVTPC to ensure
that NMIs immediately start unmasking APIC_LVTPC as needed. Document it
as such.
Fixes: 560ac136f25d ("perf/x86/core: Add APIs to switch to/from mediated PMI vector (for KVM)")
Cc: stable@vger.kernel.org
Signed-off-by: Yosry Ahmed <yosry@kernel.org>
---
arch/x86/events/core.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
index 4e344fb1ae3ed..c6608ada06ebd 100644
--- a/arch/x86/events/core.c
+++ b/arch/x86/events/core.c
@@ -2224,15 +2224,26 @@ void perf_load_guest_lvtpc(u32 guest_lvtpc)
{
u32 masked = guest_lvtpc & APIC_LVT_MASKED;
+ /*
+ * Set guest_lvtpc_loaded before writing APIC_LVTPC, otherwise an NMI
+ * arriving in between will unconditionally write APIC_DM_NMI to
+ * APIC_LVTPC on Intel, clobbering the guest's mediated PMI vector.
+ */
+ this_cpu_write(guest_lvtpc_loaded, true);
+ barrier();
apic_write(APIC_LVTPC,
APIC_DM_FIXED | PERF_GUEST_MEDIATED_PMI_VECTOR | masked);
- this_cpu_write(guest_lvtpc_loaded, true);
}
EXPORT_SYMBOL_FOR_KVM(perf_load_guest_lvtpc);
void perf_put_guest_lvtpc(void)
{
+ /*
+ * Clear guest_lvtpc_loaded before writing APIC_LVTPC, otherwise an NMI
+ * arriving in between will skip unmasking APIC_LVTPC on Intel.
+ */
this_cpu_write(guest_lvtpc_loaded, false);
+ barrier();
apic_write(APIC_LVTPC, APIC_DM_NMI);
}
EXPORT_SYMBOL_FOR_KVM(perf_put_guest_lvtpc);
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
2026-12-01 17:14 [PATCH 0/2] perf/x86: Fix races when loading guest mediated PMU context Yosry Ahmed
2026-12-01 17:14 ` [PATCH 1/2] perf/x86: Prevent races between NMIs and switching LVTPC Yosry Ahmed
@ 2026-12-01 17:14 ` Yosry Ahmed
2026-12-01 23:50 ` Sean Christopherson
2026-12-01 17:20 ` [PATCH 0/2] perf/x86: Fix races when loading guest mediated PMU context sashiko-bot
2 siblings, 1 reply; 16+ messages in thread
From: Yosry Ahmed @ 2026-12-01 17:14 UTC (permalink / raw)
To: Peter Zijlstra, Sean Christopherson
Cc: Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter,
James Clark, Paolo Bonzini, x86, Dapeng Mi, Mingwei Zhang,
linux-perf-users, kvm, linux-kernel, Yosry Ahmed, stable
When loading the guest PMU context, clear PERF_GLOBAL_CTRL after
switching to guest LVTPC. Otherwise, the host perf NMI handler may set
bits in PERF_GLOBAL_CTRL after KVM clears it (specifically on Intel,
through __intel_pmu_enable_all()). This results in PERF_GLOBAL_CTRL not
being cleared when the guest counters and event selectors are loaded, so
the guest counters could start counting in the host.
Switching LVTPC while PERF_GLOBAL_CTRL is non-zero should be safe as all
host events should be disabled at this point, and guest
counters/selectors are not loaded yet. Although it is worth noting that
switching LVTPC while PERF_GLOBAL_CTRL was cleared was intentional [1].
Alternatively, clearing PERF_GLOBAL_CTRL could be moved inside
perf_load_guest_lvtpc(), after setting guest_lvtpc_loaded but before
writing APIC_LVTPC. This would make sure PERF_GLOBAL_CTRL is cleared
after host NMIs can no longer set it, and that switching LVTPC happens
with PERF_GLOBAL_CTRL cleared. However, this would require more plumbing
on the perf side.
Fixes: 56bb27369750 ("KVM: x86/pmu: Load/put mediated PMU context when entering/exiting guest")
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/20250818143204.GH3289052@noisy.programming.kicks-ass.net # [1]
Signed-off-by: Yosry Ahmed <yosry@kernel.org>
---
arch/x86/kvm/pmu.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
index eef2f792dc9e8..1a41cd4c4ecb4 100644
--- a/arch/x86/kvm/pmu.c
+++ b/arch/x86/kvm/pmu.c
@@ -1355,6 +1355,8 @@ void kvm_mediated_pmu_load(struct kvm_vcpu *vcpu)
perf_load_guest_context();
+ perf_load_guest_lvtpc(kvm_lapic_get_reg(vcpu->arch.apic, APIC_LVTPC));
+
/*
* Explicitly clear PERF_GLOBAL_CTRL, as "loading" the guest's context
* disables all individual counters (if any were enabled), but doesn't
@@ -1367,11 +1369,13 @@ void kvm_mediated_pmu_load(struct kvm_vcpu *vcpu)
* configuring all events to be GUEST_ONLY. Clear PERF_GLOBAL_CONTROL
* even for SVM to minimize the damage if a perf event is left enabled,
* and to ensure a consistent starting state.
+ *
+ * Clear PERF_GLOBAL_CTRL after switching to the guest LVTPC, as the
+ * host perf NMI handler may set bits in PERF_GLOBAL_CTRL even if events
+ * are disabled.
*/
wrmsrq(kvm_pmu_ops.PERF_GLOBAL_CTRL, 0);
- perf_load_guest_lvtpc(kvm_lapic_get_reg(vcpu->arch.apic, APIC_LVTPC));
-
kvm_pmu_load_guest_pmcs(vcpu);
kvm_pmu_call(mediated_load)(vcpu);
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH 0/2] perf/x86: Fix races when loading guest mediated PMU context
2026-12-01 17:14 [PATCH 0/2] perf/x86: Fix races when loading guest mediated PMU context Yosry Ahmed
2026-12-01 17:14 ` [PATCH 1/2] perf/x86: Prevent races between NMIs and switching LVTPC Yosry Ahmed
2026-12-01 17:14 ` [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC Yosry Ahmed
@ 2026-12-01 17:20 ` sashiko-bot
2 siblings, 0 replies; 16+ messages in thread
From: sashiko-bot @ 2026-12-01 17:20 UTC (permalink / raw)
To: Yosry Ahmed; +Cc: linux-perf-users
Sashiko has reviewed this 2-patch series and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201171413.2289887-1-yosry@kernel.org
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
2026-12-01 17:14 ` [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC Yosry Ahmed
@ 2026-12-01 23:50 ` Sean Christopherson
2026-12-02 0:46 ` Yosry Ahmed
0 siblings, 1 reply; 16+ messages in thread
From: Sean Christopherson @ 2026-12-01 23:50 UTC (permalink / raw)
To: Yosry Ahmed
Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark, Paolo Bonzini, x86,
Dapeng Mi, Mingwei Zhang, linux-perf-users, kvm, linux-kernel,
stable
On Tue, Dec 01, 2026, Yosry Ahmed wrote:
> When loading the guest PMU context, clear PERF_GLOBAL_CTRL after
> switching to guest LVTPC. Otherwise, the host perf NMI handler may set
> bits in PERF_GLOBAL_CTRL after KVM clears it (specifically on Intel,
> through __intel_pmu_enable_all()).
But disabling PERF_GLOBAL_CTRL should prevent taking any perf NMIs, no?
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 1/2] perf/x86: Prevent races between NMIs and switching LVTPC
2026-12-01 17:14 ` [PATCH 1/2] perf/x86: Prevent races between NMIs and switching LVTPC Yosry Ahmed
@ 2026-12-01 23:56 ` Sean Christopherson
2026-12-02 0:39 ` Yosry Ahmed
2026-12-02 2:44 ` Mi, Dapeng
1 sibling, 1 reply; 16+ messages in thread
From: Sean Christopherson @ 2026-12-01 23:56 UTC (permalink / raw)
To: Yosry Ahmed
Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark, Paolo Bonzini, x86,
Dapeng Mi, Mingwei Zhang, linux-perf-users, kvm, linux-kernel,
stable
On Tue, Dec 01, 2026, Yosry Ahmed wrote:
> perf_event_nmi_handler() checks guest_lvtpc_loaded to skip handling NMIs
> and unmasking LVTPC on Intel (as it is automasked by hardware on PMI).
> However, when loading the guest LVTPC, perf_load_guest_lvtpc() updates
> APIC_LVTPC before setting guest_lvtpc_loaded. If an NMI arrives in
> between, perf_event_nmi_handler() will observe !guest_lvtpc_loaded and
> proceed.
>
> On Intel, the NMI handler will unconditionally write APIC_LVTPC to
Unconditionally writing APIC_LVTCP is fine. The issue is specifically that
it does a straight write, not a read-modify-write operation. That's kinda
sorta mentioned below, but it really should be the focus of the changelog.
> unmask PMIs, overwriting it to APIC_DM_NMI and clobbering the guest PMI
> vector. The first guest PMI is delivered as an unknown host NMI (which
> results in a panic if unknown_nmi_panic/panic_on_unrecovered_nmi is
> set). Subsequent guest PMIs will be completely lost because
> perf_event_nmi_handler() will observe guest_lvtpc_loaded and bail
> without unmasking LVTPC. Recovery only happens when
> perf_put_guest_lvtpc() restores unmasked host NMIs in APIC_LVTPC.
>
> Set guest_lvtpc_loaded before writing APIC_LVTPC in
> perf_load_guest_lvtpc() to prevent a racing NMI from clobbering LVTPC.
> The current ordering of perf_put_guest_lvtpc() is correct,
> guest_lvtpc_loaded should be cleared before writing APIC_LVTPC to ensure
> that NMIs immediately start unmasking APIC_LVTPC as needed. Document it
> as such.
>
> Fixes: 560ac136f25d ("perf/x86/core: Add APIs to switch to/from mediated PMI vector (for KVM)")
> Cc: stable@vger.kernel.org
> Signed-off-by: Yosry Ahmed <yosry@kernel.org>
> ---
> arch/x86/events/core.c | 13 ++++++++++++-
> 1 file changed, 12 insertions(+), 1 deletion(-)
>
> diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
> index 4e344fb1ae3ed..c6608ada06ebd 100644
> --- a/arch/x86/events/core.c
> +++ b/arch/x86/events/core.c
> @@ -2224,15 +2224,26 @@ void perf_load_guest_lvtpc(u32 guest_lvtpc)
> {
> u32 masked = guest_lvtpc & APIC_LVT_MASKED;
>
> + /*
> + * Set guest_lvtpc_loaded before writing APIC_LVTPC, otherwise an NMI
> + * arriving in between will unconditionally write APIC_DM_NMI to
> + * APIC_LVTPC on Intel, clobbering the guest's mediated PMI vector.
> + */
> + this_cpu_write(guest_lvtpc_loaded, true);
> + barrier();
I doubt the barrier() is necessary, though it doesn't harm anything either. WRMSR
(for x2APIC) and MMIO accesses (xAPIC) are both volatile asm().
> apic_write(APIC_LVTPC,
> APIC_DM_FIXED | PERF_GUEST_MEDIATED_PMI_VECTOR | masked);
> - this_cpu_write(guest_lvtpc_loaded, true);
> }
> EXPORT_SYMBOL_FOR_KVM(perf_load_guest_lvtpc);
>
> void perf_put_guest_lvtpc(void)
> {
> + /*
> + * Clear guest_lvtpc_loaded before writing APIC_LVTPC, otherwise an NMI
> + * arriving in between will skip unmasking APIC_LVTPC on Intel.
> + */
> this_cpu_write(guest_lvtpc_loaded, false);
> + barrier();
> apic_write(APIC_LVTPC, APIC_DM_NMI);
> }
> EXPORT_SYMBOL_FOR_KVM(perf_put_guest_lvtpc);
> --
> 2.56.1.839.g22a8290db9-goog
>
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 1/2] perf/x86: Prevent races between NMIs and switching LVTPC
2026-12-01 23:56 ` Sean Christopherson
@ 2026-12-02 0:39 ` Yosry Ahmed
0 siblings, 0 replies; 16+ messages in thread
From: Yosry Ahmed @ 2026-12-02 0:39 UTC (permalink / raw)
To: Sean Christopherson
Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark, Paolo Bonzini, x86,
Dapeng Mi, Mingwei Zhang, linux-perf-users, kvm, linux-kernel,
stable
On Tue, Dec 1, 2026 at 3:57 PM Sean Christopherson <seanjc@google.com> wrote:
>
> On Tue, Dec 01, 2026, Yosry Ahmed wrote:
> > perf_event_nmi_handler() checks guest_lvtpc_loaded to skip handling NMIs
> > and unmasking LVTPC on Intel (as it is automasked by hardware on PMI).
> > However, when loading the guest LVTPC, perf_load_guest_lvtpc() updates
> > APIC_LVTPC before setting guest_lvtpc_loaded. If an NMI arrives in
> > between, perf_event_nmi_handler() will observe !guest_lvtpc_loaded and
> > proceed.
> >
> > On Intel, the NMI handler will unconditionally write APIC_LVTPC to
>
> Unconditionally writing APIC_LVTCP is fine. The issue is specifically that
> it does a straight write, not a read-modify-write operation. That's kinda
> sorta mentioned below, but it really should be the focus of the changelog.
Ack, will clarify in the next version.
>
> > unmask PMIs, overwriting it to APIC_DM_NMI and clobbering the guest PMI
> > vector. The first guest PMI is delivered as an unknown host NMI (which
> > results in a panic if unknown_nmi_panic/panic_on_unrecovered_nmi is
> > set). Subsequent guest PMIs will be completely lost because
> > perf_event_nmi_handler() will observe guest_lvtpc_loaded and bail
> > without unmasking LVTPC. Recovery only happens when
> > perf_put_guest_lvtpc() restores unmasked host NMIs in APIC_LVTPC.
> >
> > Set guest_lvtpc_loaded before writing APIC_LVTPC in
> > perf_load_guest_lvtpc() to prevent a racing NMI from clobbering LVTPC.
> > The current ordering of perf_put_guest_lvtpc() is correct,
> > guest_lvtpc_loaded should be cleared before writing APIC_LVTPC to ensure
> > that NMIs immediately start unmasking APIC_LVTPC as needed. Document it
> > as such.
> >
> > Fixes: 560ac136f25d ("perf/x86/core: Add APIs to switch to/from mediated PMI vector (for KVM)")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Yosry Ahmed <yosry@kernel.org>
> > ---
> > arch/x86/events/core.c | 13 ++++++++++++-
> > 1 file changed, 12 insertions(+), 1 deletion(-)
> >
> > diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
> > index 4e344fb1ae3ed..c6608ada06ebd 100644
> > --- a/arch/x86/events/core.c
> > +++ b/arch/x86/events/core.c
> > @@ -2224,15 +2224,26 @@ void perf_load_guest_lvtpc(u32 guest_lvtpc)
> > {
> > u32 masked = guest_lvtpc & APIC_LVT_MASKED;
> >
> > + /*
> > + * Set guest_lvtpc_loaded before writing APIC_LVTPC, otherwise an NMI
> > + * arriving in between will unconditionally write APIC_DM_NMI to
> > + * APIC_LVTPC on Intel, clobbering the guest's mediated PMI vector.
> > + */
> > + this_cpu_write(guest_lvtpc_loaded, true);
> > + barrier();
>
> I doubt the barrier() is necessary, though it doesn't harm anything either. WRMSR
> (for x2APIC) and MMIO accesses (xAPIC) are both volatile asm().
I was on the fence, so I thought I'd include it and see what others
think. I am fine either way.
>
> > apic_write(APIC_LVTPC,
> > APIC_DM_FIXED | PERF_GUEST_MEDIATED_PMI_VECTOR | masked);
> > - this_cpu_write(guest_lvtpc_loaded, true);
> > }
> > EXPORT_SYMBOL_FOR_KVM(perf_load_guest_lvtpc);
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
2026-12-01 23:50 ` Sean Christopherson
@ 2026-12-02 0:46 ` Yosry Ahmed
2026-12-02 1:14 ` Sean Christopherson
0 siblings, 1 reply; 16+ messages in thread
From: Yosry Ahmed @ 2026-12-02 0:46 UTC (permalink / raw)
To: Sean Christopherson
Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark, Paolo Bonzini, x86,
Dapeng Mi, Mingwei Zhang, linux-perf-users, kvm, linux-kernel,
stable
On Tue, Dec 1, 2026 at 3:50 PM Sean Christopherson <seanjc@google.com> wrote:
>
> On Tue, Dec 01, 2026, Yosry Ahmed wrote:
> > When loading the guest PMU context, clear PERF_GLOBAL_CTRL after
> > switching to guest LVTPC. Otherwise, the host perf NMI handler may set
> > bits in PERF_GLOBAL_CTRL after KVM clears it (specifically on Intel,
> > through __intel_pmu_enable_all()).
>
> But disabling PERF_GLOBAL_CTRL should prevent taking any perf NMIs, no?
If I am reading the code correctly, perf_event_nmi_handler() ->
intel_pmu_handle_irq() -> __intel_pmu_enable_all() will be executed on
an NMI even if it wasn't a perf NMI (__intel_pmu_enable_all() is
called in the done label even if status is clear), but I might be
mistaken.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
2026-12-02 0:46 ` Yosry Ahmed
@ 2026-12-02 1:14 ` Sean Christopherson
2026-12-02 3:39 ` Mi, Dapeng
0 siblings, 1 reply; 16+ messages in thread
From: Sean Christopherson @ 2026-12-02 1:14 UTC (permalink / raw)
To: Yosry Ahmed
Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark, Paolo Bonzini, x86,
Dapeng Mi, Mingwei Zhang, linux-perf-users, kvm, linux-kernel,
stable
On Tue, Dec 01, 2026, Yosry Ahmed wrote:
> On Tue, Dec 1, 2026 at 3:50 PM Sean Christopherson <seanjc@google.com> wrote:
> >
> > On Tue, Dec 01, 2026, Yosry Ahmed wrote:
> > > When loading the guest PMU context, clear PERF_GLOBAL_CTRL after
> > > switching to guest LVTPC. Otherwise, the host perf NMI handler may set
> > > bits in PERF_GLOBAL_CTRL after KVM clears it (specifically on Intel,
> > > through __intel_pmu_enable_all()).
> >
> > But disabling PERF_GLOBAL_CTRL should prevent taking any perf NMIs, no?
>
> If I am reading the code correctly, perf_event_nmi_handler() ->
> intel_pmu_handle_irq() -> __intel_pmu_enable_all() will be executed on
> an NMI even if it wasn't a perf NMI (__intel_pmu_enable_all() is
> called in the done label even if status is clear), but I might be
> mistaken.
That feels like a perf bug? I would expect perf_load_guest_context() to prevent
trying to enable perf events.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 1/2] perf/x86: Prevent races between NMIs and switching LVTPC
2026-12-01 17:14 ` [PATCH 1/2] perf/x86: Prevent races between NMIs and switching LVTPC Yosry Ahmed
2026-12-01 23:56 ` Sean Christopherson
@ 2026-12-02 2:44 ` Mi, Dapeng
2026-12-02 4:18 ` Yosry Ahmed
1 sibling, 1 reply; 16+ messages in thread
From: Mi, Dapeng @ 2026-12-02 2:44 UTC (permalink / raw)
To: Yosry Ahmed, Peter Zijlstra, Sean Christopherson
Cc: Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter,
James Clark, Paolo Bonzini, x86, Mingwei Zhang, linux-perf-users,
kvm, linux-kernel, stable
On 12/2/2026 1:14 AM, Yosry Ahmed wrote:
> perf_event_nmi_handler() checks guest_lvtpc_loaded to skip handling NMIs
> and unmasking LVTPC on Intel (as it is automasked by hardware on PMI).
> However, when loading the guest LVTPC, perf_load_guest_lvtpc() updates
> APIC_LVTPC before setting guest_lvtpc_loaded. If an NMI arrives in
> between, perf_event_nmi_handler() will observe !guest_lvtpc_loaded and
> proceed.
>
> On Intel, the NMI handler will unconditionally write APIC_LVTPC to
> unmask PMIs, overwriting it to APIC_DM_NMI and clobbering the guest PMI
> vector. The first guest PMI is delivered as an unknown host NMI (which
> results in a panic if unknown_nmi_panic/panic_on_unrecovered_nmi is
> set). Subsequent guest PMIs will be completely lost because
> perf_event_nmi_handler() will observe guest_lvtpc_loaded and bail
> without unmasking LVTPC. Recovery only happens when
> perf_put_guest_lvtpc() restores unmasked host NMIs in APIC_LVTPC.
>
> Set guest_lvtpc_loaded before writing APIC_LVTPC in
> perf_load_guest_lvtpc() to prevent a racing NMI from clobbering LVTPC.
> The current ordering of perf_put_guest_lvtpc() is correct,
> guest_lvtpc_loaded should be cleared before writing APIC_LVTPC to ensure
> that NMIs immediately start unmasking APIC_LVTPC as needed. Document it
> as such.
>
> Fixes: 560ac136f25d ("perf/x86/core: Add APIs to switch to/from mediated PMI vector (for KVM)")
> Cc: stable@vger.kernel.org
> Signed-off-by: Yosry Ahmed <yosry@kernel.org>
> ---
> arch/x86/events/core.c | 13 ++++++++++++-
> 1 file changed, 12 insertions(+), 1 deletion(-)
>
> diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
> index 4e344fb1ae3ed..c6608ada06ebd 100644
> --- a/arch/x86/events/core.c
> +++ b/arch/x86/events/core.c
> @@ -2224,15 +2224,26 @@ void perf_load_guest_lvtpc(u32 guest_lvtpc)
> {
> u32 masked = guest_lvtpc & APIC_LVT_MASKED;
>
> + /*
> + * Set guest_lvtpc_loaded before writing APIC_LVTPC, otherwise an NMI
I suppose "an NMI" here is a non-PMI NMI, right? If so, we'd better
emphasize it in the comments.
> + * arriving in between will unconditionally write APIC_DM_NMI to
> + * APIC_LVTPC on Intel, clobbering the guest's mediated PMI vector.
> + */
> + this_cpu_write(guest_lvtpc_loaded, true);
> + barrier();
> apic_write(APIC_LVTPC,
> APIC_DM_FIXED | PERF_GUEST_MEDIATED_PMI_VECTOR | masked);
> - this_cpu_write(guest_lvtpc_loaded, true);
> }
> EXPORT_SYMBOL_FOR_KVM(perf_load_guest_lvtpc);
>
> void perf_put_guest_lvtpc(void)
> {
> + /*
> + * Clear guest_lvtpc_loaded before writing APIC_LVTPC, otherwise an NMI
> + * arriving in between will skip unmasking APIC_LVTPC on Intel.
> + */
> this_cpu_write(guest_lvtpc_loaded, false);
> + barrier();
> apic_write(APIC_LVTPC, APIC_DM_NMI);
> }
> EXPORT_SYMBOL_FOR_KVM(perf_put_guest_lvtpc);
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
2026-12-02 1:14 ` Sean Christopherson
@ 2026-12-02 3:39 ` Mi, Dapeng
2026-12-02 4:32 ` Yosry Ahmed
0 siblings, 1 reply; 16+ messages in thread
From: Mi, Dapeng @ 2026-12-02 3:39 UTC (permalink / raw)
To: Sean Christopherson, Yosry Ahmed
Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark, Paolo Bonzini, x86,
Mingwei Zhang, linux-perf-users, kvm, linux-kernel, stable
On 12/2/2026 9:14 AM, Sean Christopherson wrote:
> On Tue, Dec 01, 2026, Yosry Ahmed wrote:
>> On Tue, Dec 1, 2026 at 3:50 PM Sean Christopherson <seanjc@google.com> wrote:
>>> On Tue, Dec 01, 2026, Yosry Ahmed wrote:
>>>> When loading the guest PMU context, clear PERF_GLOBAL_CTRL after
>>>> switching to guest LVTPC. Otherwise, the host perf NMI handler may set
>>>> bits in PERF_GLOBAL_CTRL after KVM clears it (specifically on Intel,
>>>> through __intel_pmu_enable_all()).
>>> But disabling PERF_GLOBAL_CTRL should prevent taking any perf NMIs, no?
>> If I am reading the code correctly, perf_event_nmi_handler() ->
>> intel_pmu_handle_irq() -> __intel_pmu_enable_all() will be executed on
>> an NMI even if it wasn't a perf NMI (__intel_pmu_enable_all() is
>> called in the done label even if status is clear), but I might be
>> mistaken.
> That feels like a perf bug? I would expect perf_load_guest_context() to prevent
> trying to enable perf events.
perf_load_guest_context() schedule out all host events, and
perf_event_nmi_handler() should early return and don't touch any PMU MSRs
if active_events is 0. While the issue is that active_events currently
doesn't really track the active events as what it intends. Instead, it
seems to track the created events regardless of it's active. This is a perf
bug.
It's intended to clear GLOBAL_CTRL MSR before calling
perf_load_guest_lvtpc(), it prevents triggering PMI from hit into
perf_load_guest_lvtpc().
It looks the better way to fix this issue is to make active_events really
track the active events (increase/decrease active_events in
x86_pmu_add()/x86_pmu_del()).
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 1/2] perf/x86: Prevent races between NMIs and switching LVTPC
2026-12-02 2:44 ` Mi, Dapeng
@ 2026-12-02 4:18 ` Yosry Ahmed
0 siblings, 0 replies; 16+ messages in thread
From: Yosry Ahmed @ 2026-12-02 4:18 UTC (permalink / raw)
To: Mi, Dapeng
Cc: Peter Zijlstra, Sean Christopherson, Ingo Molnar,
Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter,
James Clark, Paolo Bonzini, x86, Mingwei Zhang, linux-perf-users,
kvm, linux-kernel, stable
On Tue, Dec 1, 2026 at 6:44 PM Mi, Dapeng <dapeng1.mi@linux.intel.com> wrote:
>
>
> On 12/2/2026 1:14 AM, Yosry Ahmed wrote:
> > perf_event_nmi_handler() checks guest_lvtpc_loaded to skip handling NMIs
> > and unmasking LVTPC on Intel (as it is automasked by hardware on PMI).
> > However, when loading the guest LVTPC, perf_load_guest_lvtpc() updates
> > APIC_LVTPC before setting guest_lvtpc_loaded. If an NMI arrives in
> > between, perf_event_nmi_handler() will observe !guest_lvtpc_loaded and
> > proceed.
> >
> > On Intel, the NMI handler will unconditionally write APIC_LVTPC to
> > unmask PMIs, overwriting it to APIC_DM_NMI and clobbering the guest PMI
> > vector. The first guest PMI is delivered as an unknown host NMI (which
> > results in a panic if unknown_nmi_panic/panic_on_unrecovered_nmi is
> > set). Subsequent guest PMIs will be completely lost because
> > perf_event_nmi_handler() will observe guest_lvtpc_loaded and bail
> > without unmasking LVTPC. Recovery only happens when
> > perf_put_guest_lvtpc() restores unmasked host NMIs in APIC_LVTPC.
> >
> > Set guest_lvtpc_loaded before writing APIC_LVTPC in
> > perf_load_guest_lvtpc() to prevent a racing NMI from clobbering LVTPC.
> > The current ordering of perf_put_guest_lvtpc() is correct,
> > guest_lvtpc_loaded should be cleared before writing APIC_LVTPC to ensure
> > that NMIs immediately start unmasking APIC_LVTPC as needed. Document it
> > as such.
> >
> > Fixes: 560ac136f25d ("perf/x86/core: Add APIs to switch to/from mediated PMI vector (for KVM)")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Yosry Ahmed <yosry@kernel.org>
> > ---
> > arch/x86/events/core.c | 13 ++++++++++++-
> > 1 file changed, 12 insertions(+), 1 deletion(-)
> >
> > diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
> > index 4e344fb1ae3ed..c6608ada06ebd 100644
> > --- a/arch/x86/events/core.c
> > +++ b/arch/x86/events/core.c
> > @@ -2224,15 +2224,26 @@ void perf_load_guest_lvtpc(u32 guest_lvtpc)
> > {
> > u32 masked = guest_lvtpc & APIC_LVT_MASKED;
> >
> > + /*
> > + * Set guest_lvtpc_loaded before writing APIC_LVTPC, otherwise an NMI
>
> I suppose "an NMI" here is a non-PMI NMI, right? If so, we'd better
> emphasize it in the comments.
If everything works correctly, yes, it should be a non-perf NMI. I can
emphasize that, maybe something like "otherwise a (non-PMI) NMI
arriving .."?
>
>
> > + * arriving in between will unconditionally write APIC_DM_NMI to
> > + * APIC_LVTPC on Intel, clobbering the guest's mediated PMI vector.
> > + */
> > + this_cpu_write(guest_lvtpc_loaded, true);
> > + barrier();
> > apic_write(APIC_LVTPC,
> > APIC_DM_FIXED | PERF_GUEST_MEDIATED_PMI_VECTOR | masked);
> > - this_cpu_write(guest_lvtpc_loaded, true);
> > }
> > EXPORT_SYMBOL_FOR_KVM(perf_load_guest_lvtpc);
> >
> > void perf_put_guest_lvtpc(void)
> > {
> > + /*
> > + * Clear guest_lvtpc_loaded before writing APIC_LVTPC, otherwise an NMI
> > + * arriving in between will skip unmasking APIC_LVTPC on Intel.
> > + */
> > this_cpu_write(guest_lvtpc_loaded, false);
> > + barrier();
> > apic_write(APIC_LVTPC, APIC_DM_NMI);
> > }
> > EXPORT_SYMBOL_FOR_KVM(perf_put_guest_lvtpc);
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
2026-12-02 3:39 ` Mi, Dapeng
@ 2026-12-02 4:32 ` Yosry Ahmed
2026-12-02 5:26 ` Mi, Dapeng
0 siblings, 1 reply; 16+ messages in thread
From: Yosry Ahmed @ 2026-12-02 4:32 UTC (permalink / raw)
To: Mi, Dapeng
Cc: Sean Christopherson, Peter Zijlstra, Ingo Molnar,
Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter,
James Clark, Paolo Bonzini, x86, Mingwei Zhang, linux-perf-users,
kvm, linux-kernel, stable
On Tue, Dec 1, 2026 at 7:39 PM Mi, Dapeng <dapeng1.mi@linux.intel.com> wrote:
>
>
> On 12/2/2026 9:14 AM, Sean Christopherson wrote:
> > On Tue, Dec 01, 2026, Yosry Ahmed wrote:
> >> On Tue, Dec 1, 2026 at 3:50 PM Sean Christopherson <seanjc@google.com> wrote:
> >>> On Tue, Dec 01, 2026, Yosry Ahmed wrote:
> >>>> When loading the guest PMU context, clear PERF_GLOBAL_CTRL after
> >>>> switching to guest LVTPC. Otherwise, the host perf NMI handler may set
> >>>> bits in PERF_GLOBAL_CTRL after KVM clears it (specifically on Intel,
> >>>> through __intel_pmu_enable_all()).
> >>> But disabling PERF_GLOBAL_CTRL should prevent taking any perf NMIs, no?
> >> If I am reading the code correctly, perf_event_nmi_handler() ->
> >> intel_pmu_handle_irq() -> __intel_pmu_enable_all() will be executed on
> >> an NMI even if it wasn't a perf NMI (__intel_pmu_enable_all() is
> >> called in the done label even if status is clear), but I might be
> >> mistaken.
> > That feels like a perf bug? I would expect perf_load_guest_context() to prevent
> > trying to enable perf events.
IIUC, perf not clearing PERF_GLOBAL_CTRL after
perf_load_guest_context() is the reason KVM clears PERF_GLOBAL_CTRL
here to begin with:
https://lore.kernel.org/all/aR43LoV1ti5-2WRD@google.com/.
> perf_load_guest_context() schedule out all host events, and
> perf_event_nmi_handler() should early return and don't touch any PMU MSRs
> if active_events is 0. While the issue is that active_events currently
> doesn't really track the active events as what it intends. Instead, it
> seems to track the created events regardless of it's active. This is a perf
> bug.
>
> It's intended to clear GLOBAL_CTRL MSR before calling
> perf_load_guest_lvtpc(), it prevents triggering PMI from hit into
> perf_load_guest_lvtpc().
>
> It looks the better way to fix this issue is to make active_events really
> track the active events (increase/decrease active_events in
> x86_pmu_add()/x86_pmu_del()).
If active_events is always 0 while the guest LVTPC is loaded, then we
can probably drop guest_lvtpc_loaded, right?
Having perf clear GLOBAL_CTRL when no events are active (e.g. in
perf_load_guest_context() and in the NMI handler) would also let KVM
drop the MSR write completely. It seems like that's what Peter and
Sean initially expected based on the discussion linked above
Anyway, either of the above seems like it may be a more intrusive fix
for stable, but if Sean/Peter think it's fine then it works for me
too.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
2026-12-02 4:32 ` Yosry Ahmed
@ 2026-12-02 5:26 ` Mi, Dapeng
2026-12-02 5:48 ` Yosry Ahmed
0 siblings, 1 reply; 16+ messages in thread
From: Mi, Dapeng @ 2026-12-02 5:26 UTC (permalink / raw)
To: Yosry Ahmed
Cc: Sean Christopherson, Peter Zijlstra, Ingo Molnar,
Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter,
James Clark, Paolo Bonzini, x86, Mingwei Zhang, linux-perf-users,
kvm, linux-kernel, stable
On 12/2/2026 12:32 PM, Yosry Ahmed wrote:
> On Tue, Dec 1, 2026 at 7:39 PM Mi, Dapeng <dapeng1.mi@linux.intel.com> wrote:
>>
>> On 12/2/2026 9:14 AM, Sean Christopherson wrote:
>>> On Tue, Dec 01, 2026, Yosry Ahmed wrote:
>>>> On Tue, Dec 1, 2026 at 3:50 PM Sean Christopherson <seanjc@google.com> wrote:
>>>>> On Tue, Dec 01, 2026, Yosry Ahmed wrote:
>>>>>> When loading the guest PMU context, clear PERF_GLOBAL_CTRL after
>>>>>> switching to guest LVTPC. Otherwise, the host perf NMI handler may set
>>>>>> bits in PERF_GLOBAL_CTRL after KVM clears it (specifically on Intel,
>>>>>> through __intel_pmu_enable_all()).
>>>>> But disabling PERF_GLOBAL_CTRL should prevent taking any perf NMIs, no?
>>>> If I am reading the code correctly, perf_event_nmi_handler() ->
>>>> intel_pmu_handle_irq() -> __intel_pmu_enable_all() will be executed on
>>>> an NMI even if it wasn't a perf NMI (__intel_pmu_enable_all() is
>>>> called in the done label even if status is clear), but I might be
>>>> mistaken.
>>> That feels like a perf bug? I would expect perf_load_guest_context() to prevent
>>> trying to enable perf events.
> IIUC, perf not clearing PERF_GLOBAL_CTRL after
> perf_load_guest_context() is the reason KVM clears PERF_GLOBAL_CTRL
> here to begin with:
> https://lore.kernel.org/all/aR43LoV1ti5-2WRD@google.com/.
>
>> perf_load_guest_context() schedule out all host events, and
>> perf_event_nmi_handler() should early return and don't touch any PMU MSRs
>> if active_events is 0. While the issue is that active_events currently
>> doesn't really track the active events as what it intends. Instead, it
>> seems to track the created events regardless of it's active. This is a perf
>> bug.
>>
>> It's intended to clear GLOBAL_CTRL MSR before calling
>> perf_load_guest_lvtpc(), it prevents triggering PMI from hit into
>> perf_load_guest_lvtpc().
>>
>> It looks the better way to fix this issue is to make active_events really
>> track the active events (increase/decrease active_events in
>> x86_pmu_add()/x86_pmu_del()).
> If active_events is always 0 while the guest LVTPC is loaded, then we
> can probably drop guest_lvtpc_loaded, right?
I think so.
>
> Having perf clear GLOBAL_CTRL when no events are active (e.g. in
> perf_load_guest_context() and in the NMI handler) would also let KVM
> drop the MSR write completely. It seems like that's what Peter and
> Sean initially expected based on the discussion linked above
Currently GLOBAL_CTRL keeps enabled for all counters by default even though
there is no active event. Whether a single event is enabled depends on its
EVENTSEL "enable" field. GLOBAL_CTRL is usually used to ensure all counters
are enabled or disabled simultaneously.
In theory, it can work by making perf_load_guest_context() clear
GLOBAL_CTRL, but it won't be a simple change and could break current x86
perf convention. It may cause some corner issues.
IMO, making active_events really track active events could be the simplest
and straightforward way to fix this issue.
Thanks.
>
> Anyway, either of the above seems like it may be a more intrusive fix
> for stable, but if Sean/Peter think it's fine then it works for me
> too.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
2026-12-02 5:26 ` Mi, Dapeng
@ 2026-12-02 5:48 ` Yosry Ahmed
2026-12-02 7:18 ` Mi, Dapeng
0 siblings, 1 reply; 16+ messages in thread
From: Yosry Ahmed @ 2026-12-02 5:48 UTC (permalink / raw)
To: Mi, Dapeng
Cc: Sean Christopherson, Peter Zijlstra, Ingo Molnar,
Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter,
James Clark, Paolo Bonzini, x86, Mingwei Zhang, linux-perf-users,
kvm, linux-kernel, stable
On Tue, Dec 1, 2026 at 9:26 PM Mi, Dapeng <dapeng1.mi@linux.intel.com> wrote:
>
>
> On 12/2/2026 12:32 PM, Yosry Ahmed wrote:
> > On Tue, Dec 1, 2026 at 7:39 PM Mi, Dapeng <dapeng1.mi@linux.intel.com> wrote:
> >>
> >> On 12/2/2026 9:14 AM, Sean Christopherson wrote:
> >>> On Tue, Dec 01, 2026, Yosry Ahmed wrote:
> >>>> On Tue, Dec 1, 2026 at 3:50 PM Sean Christopherson <seanjc@google.com> wrote:
> >>>>> On Tue, Dec 01, 2026, Yosry Ahmed wrote:
> >>>>>> When loading the guest PMU context, clear PERF_GLOBAL_CTRL after
> >>>>>> switching to guest LVTPC. Otherwise, the host perf NMI handler may set
> >>>>>> bits in PERF_GLOBAL_CTRL after KVM clears it (specifically on Intel,
> >>>>>> through __intel_pmu_enable_all()).
> >>>>> But disabling PERF_GLOBAL_CTRL should prevent taking any perf NMIs, no?
> >>>> If I am reading the code correctly, perf_event_nmi_handler() ->
> >>>> intel_pmu_handle_irq() -> __intel_pmu_enable_all() will be executed on
> >>>> an NMI even if it wasn't a perf NMI (__intel_pmu_enable_all() is
> >>>> called in the done label even if status is clear), but I might be
> >>>> mistaken.
> >>> That feels like a perf bug? I would expect perf_load_guest_context() to prevent
> >>> trying to enable perf events.
> > IIUC, perf not clearing PERF_GLOBAL_CTRL after
> > perf_load_guest_context() is the reason KVM clears PERF_GLOBAL_CTRL
> > here to begin with:
> > https://lore.kernel.org/all/aR43LoV1ti5-2WRD@google.com/.
> >
> >> perf_load_guest_context() schedule out all host events, and
> >> perf_event_nmi_handler() should early return and don't touch any PMU MSRs
> >> if active_events is 0. While the issue is that active_events currently
> >> doesn't really track the active events as what it intends. Instead, it
> >> seems to track the created events regardless of it's active. This is a perf
> >> bug.
> >>
> >> It's intended to clear GLOBAL_CTRL MSR before calling
> >> perf_load_guest_lvtpc(), it prevents triggering PMI from hit into
> >> perf_load_guest_lvtpc().
> >>
> >> It looks the better way to fix this issue is to make active_events really
> >> track the active events (increase/decrease active_events in
> >> x86_pmu_add()/x86_pmu_del()).
> > If active_events is always 0 while the guest LVTPC is loaded, then we
> > can probably drop guest_lvtpc_loaded, right?
>
> I think so.
>
>
> >
> > Having perf clear GLOBAL_CTRL when no events are active (e.g. in
> > perf_load_guest_context() and in the NMI handler) would also let KVM
> > drop the MSR write completely. It seems like that's what Peter and
> > Sean initially expected based on the discussion linked above
>
> Currently GLOBAL_CTRL keeps enabled for all counters by default even though
> there is no active event. Whether a single event is enabled depends on its
> EVENTSEL "enable" field. GLOBAL_CTRL is usually used to ensure all counters
> are enabled or disabled simultaneously.
>
> In theory, it can work by making perf_load_guest_context() clear
> GLOBAL_CTRL, but it won't be a simple change and could break current x86
> perf convention. It may cause some corner issues.
>
> IMO, making active_events really track active events could be the simplest
> and straightforward way to fix this issue.
Taking a closer look, I don't think this is viable:
- active_events is global, not per-CPU.
- If active_events tracks currently scheduled events (which is what we
need here AFAICT), bailing in the NMI handler can miss a PMI if a PMI
was in-flight or delayed when the event is scheduled out.
- active_events is not necessarily 0 while the guest mediated PMU
context is loaded. Other PMUs like Intel PT/BTS could have active
events AFAICT.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
2026-12-02 5:48 ` Yosry Ahmed
@ 2026-12-02 7:18 ` Mi, Dapeng
0 siblings, 0 replies; 16+ messages in thread
From: Mi, Dapeng @ 2026-12-02 7:18 UTC (permalink / raw)
To: Yosry Ahmed
Cc: Sean Christopherson, Peter Zijlstra, Ingo Molnar,
Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter,
James Clark, Paolo Bonzini, x86, Mingwei Zhang, linux-perf-users,
kvm, linux-kernel, stable
On 12/2/2026 1:48 PM, Yosry Ahmed wrote:
> On Tue, Dec 1, 2026 at 9:26 PM Mi, Dapeng <dapeng1.mi@linux.intel.com> wrote:
>>
>> On 12/2/2026 12:32 PM, Yosry Ahmed wrote:
>>> On Tue, Dec 1, 2026 at 7:39 PM Mi, Dapeng <dapeng1.mi@linux.intel.com> wrote:
>>>> On 12/2/2026 9:14 AM, Sean Christopherson wrote:
>>>>> On Tue, Dec 01, 2026, Yosry Ahmed wrote:
>>>>>> On Tue, Dec 1, 2026 at 3:50 PM Sean Christopherson <seanjc@google.com> wrote:
>>>>>>> On Tue, Dec 01, 2026, Yosry Ahmed wrote:
>>>>>>>> When loading the guest PMU context, clear PERF_GLOBAL_CTRL after
>>>>>>>> switching to guest LVTPC. Otherwise, the host perf NMI handler may set
>>>>>>>> bits in PERF_GLOBAL_CTRL after KVM clears it (specifically on Intel,
>>>>>>>> through __intel_pmu_enable_all()).
>>>>>>> But disabling PERF_GLOBAL_CTRL should prevent taking any perf NMIs, no?
>>>>>> If I am reading the code correctly, perf_event_nmi_handler() ->
>>>>>> intel_pmu_handle_irq() -> __intel_pmu_enable_all() will be executed on
>>>>>> an NMI even if it wasn't a perf NMI (__intel_pmu_enable_all() is
>>>>>> called in the done label even if status is clear), but I might be
>>>>>> mistaken.
>>>>> That feels like a perf bug? I would expect perf_load_guest_context() to prevent
>>>>> trying to enable perf events.
>>> IIUC, perf not clearing PERF_GLOBAL_CTRL after
>>> perf_load_guest_context() is the reason KVM clears PERF_GLOBAL_CTRL
>>> here to begin with:
>>> https://lore.kernel.org/all/aR43LoV1ti5-2WRD@google.com/.
>>>
>>>> perf_load_guest_context() schedule out all host events, and
>>>> perf_event_nmi_handler() should early return and don't touch any PMU MSRs
>>>> if active_events is 0. While the issue is that active_events currently
>>>> doesn't really track the active events as what it intends. Instead, it
>>>> seems to track the created events regardless of it's active. This is a perf
>>>> bug.
>>>>
>>>> It's intended to clear GLOBAL_CTRL MSR before calling
>>>> perf_load_guest_lvtpc(), it prevents triggering PMI from hit into
>>>> perf_load_guest_lvtpc().
>>>>
>>>> It looks the better way to fix this issue is to make active_events really
>>>> track the active events (increase/decrease active_events in
>>>> x86_pmu_add()/x86_pmu_del()).
>>> If active_events is always 0 while the guest LVTPC is loaded, then we
>>> can probably drop guest_lvtpc_loaded, right?
>> I think so.
>>
>>
>>> Having perf clear GLOBAL_CTRL when no events are active (e.g. in
>>> perf_load_guest_context() and in the NMI handler) would also let KVM
>>> drop the MSR write completely. It seems like that's what Peter and
>>> Sean initially expected based on the discussion linked above
>> Currently GLOBAL_CTRL keeps enabled for all counters by default even though
>> there is no active event. Whether a single event is enabled depends on its
>> EVENTSEL "enable" field. GLOBAL_CTRL is usually used to ensure all counters
>> are enabled or disabled simultaneously.
>>
>> In theory, it can work by making perf_load_guest_context() clear
>> GLOBAL_CTRL, but it won't be a simple change and could break current x86
>> perf convention. It may cause some corner issues.
>>
>> IMO, making active_events really track active events could be the simplest
>> and straightforward way to fix this issue.
> Taking a closer look, I don't think this is viable:
> - active_events is global, not per-CPU.
Yes, but we can extend the active_events to per-cpu, PMI is per-cpu, right?
> - If active_events tracks currently scheduled events (which is what we
> need here AFAICT), bailing in the NMI handler can miss a PMI if a PMI
> was in-flight or delayed when the event is scheduled out.
Generally the GLOBAL_CTRL is cleared in the entire schedule_in or
schedule_out process until the scheduling finishes, this would prevent the
PMI is triggered again in the event scheduling in theory.
> - active_events is not necessarily 0 while the guest mediated PMU
> context is loaded. Other PMUs like Intel PT/BTS could have active
> events AFAICT.
Yes, Intel PT is an issue. The simplest way to fix the issue is to disallow
PT and mediated vPMU to work in coexistence.
An alternative way is to move perf_load_guest_lvtpc() into
perf_load_guest_context(), call perf_load_guest_context() between the
disabling perf contexts and enabling perf contexts. Actually it goes back
our previous implementation.
I'm fine with either way.
^ permalink raw reply [flat|nested] 16+ messages in thread
end of thread, other threads:[~2026-12-02 7:18 UTC | newest]
Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 17:14 [PATCH 0/2] perf/x86: Fix races when loading guest mediated PMU context Yosry Ahmed
2026-12-01 17:14 ` [PATCH 1/2] perf/x86: Prevent races between NMIs and switching LVTPC Yosry Ahmed
2026-12-01 23:56 ` Sean Christopherson
2026-12-02 0:39 ` Yosry Ahmed
2026-12-02 2:44 ` Mi, Dapeng
2026-12-02 4:18 ` Yosry Ahmed
2026-12-01 17:14 ` [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC Yosry Ahmed
2026-12-01 23:50 ` Sean Christopherson
2026-12-02 0:46 ` Yosry Ahmed
2026-12-02 1:14 ` Sean Christopherson
2026-12-02 3:39 ` Mi, Dapeng
2026-12-02 4:32 ` Yosry Ahmed
2026-12-02 5:26 ` Mi, Dapeng
2026-12-02 5:48 ` Yosry Ahmed
2026-12-02 7:18 ` Mi, Dapeng
2026-12-01 17:20 ` [PATCH 0/2] perf/x86: Fix races when loading guest mediated PMU context sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox