* [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 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 1 sibling, 2 replies; 9+ 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] 9+ 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; 9+ 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] 9+ 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; 9+ 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] 9+ 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 1 sibling, 0 replies; 9+ 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] 9+ 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 2026-12-01 23:50 ` Sean Christopherson 1 sibling, 1 reply; 9+ 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] 9+ 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; 9+ 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] 9+ 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; 9+ 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] 9+ 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; 9+ 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] 9+ 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 0 siblings, 0 replies; 9+ 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] 9+ messages in thread
end of thread, other threads:[~2026-12-02 3:39 UTC | newest]
Thread overview: 9+ 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 23:56 ` Sean Christopherson
2026-12-02 0:39 ` Yosry Ahmed
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 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
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox