* [PATCH 1/2] perf/x86: Prevent races between NMIs and switching LVTPC
[not found] <20261201171413.2289887-1-yosry@kernel.org>
@ 2026-12-01 17:14 ` Yosry Ahmed
2026-12-01 17:14 ` [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC Yosry Ahmed
1 sibling, 0 replies; 2+ 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] 2+ messages in thread
* [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
[not found] <20261201171413.2289887-1-yosry@kernel.org>
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
1 sibling, 0 replies; 2+ 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] 2+ messages in thread
end of thread, other threads:[~2026-12-01 17:14 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20261201171413.2289887-1-yosry@kernel.org>
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox