perf/x86: Fix races when loading guest mediated PMU context
Subsystems (5)
cover perf/x86: Fix races when loading guest mediated PMU context
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
Signature · 1 line
2.56.1.839.g22a8290db9-goog
PATCH 1/2 perf/x86: Prevent races between NMIs and switching LVTPC
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.
--- arch/x86/events/core.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
arch/x86/events/core.c +12 -1
@@ -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);
Signature · 1 line
2.56.1.839.g22a8290db9-goog
PATCH 2/2 KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
In reply to Yosry AhmedWhen 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.
--- arch/x86/kvm/pmu.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/kvm/pmu.c +6 -2
@@ -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);
Signature · 1 line
2.56.1.839.g22a8290db9-goog
[email protected]
AI reviewSashiko has reviewed this 2-patch series and found no issues.
Sashiko has reviewed this 2-patch series and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]
Sean Christopherson
maintainerRe: [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
In reply to Yosry AhmedQuoted 4 lines · Yosry Ahmed
When loading the guest PMU context, clear
PERF_GLOBAL_CTRLafter switching to guest LVTPC. Otherwise, the host perf NMI handler may set bits inPERF_GLOBAL_CTRLafter KVM clears it (specifically on Intel, through__intel_pmu_enable_all()).
But disabling PERF_GLOBAL_CTRL should prevent taking any perf NMIs, no?
Sean Christopherson
maintainerRe: [PATCH 1/2] perf/x86: Prevent races between NMIs and switching LVTPC
In reply to Yosry AhmedQuoted 8 lines · Yosry Ahmed
perf_event_nmi_handler()checksguest_lvtpc_loadedto 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()updatesAPIC_LVTPCbefore settingguest_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_LVTPCto
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.
Quoted 27 lines
unmask PMIs, overwriting it to
APIC_DM_NMIand 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 becauseperf_event_nmi_handler()will observeguest_lvtpc_loadedand bail without unmasking LVTPC. Recovery only happens whenperf_put_guest_lvtpc()restores unmasked host NMIs inAPIC_LVTPC.Set
guest_lvtpc_loadedbefore writingAPIC_LVTPCinperf_load_guest_lvtpc()to prevent a racing NMI from clobbering LVTPC. The current ordering ofperf_put_guest_lvtpc()is correct,guest_lvtpc_loadedshould be cleared before writingAPIC_LVTPCto ensure that NMIs immediately start unmaskingAPIC_LVTPCas needed. Document it as such.Fixes 560ac136f25d ("perf/x86/core: Add APIs to switch to/from mediated PMI vector (for KVM)")Signed-off-by Yosry Ahmed <[email protected]>--- arch/x86/events/core.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)arch/x86/events/core.c +0
-0
arch/x86/events/core.c void perf_load_guest_lvtpc(u32 guest_lvtpc) · line 2233
Quoted 10 lines
arch/x86/events/core.c +7
-0@@ -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().
Quoted 20 lines
arch/x86/events/core.c +5
-1apic_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);Signature · 1 line
2.56.1.839.g22a8290db9-goog
Re: [PATCH 1/2] perf/x86: Prevent races between NMIs and switching LVTPC
Quoted 9 lines · Sean Christopherson
Quoted 7 lines · Yosry Ahmed
perf_event_nmi_handler()checksguest_lvtpc_loadedto 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()updatesAPIC_LVTPCbefore settingguest_lvtpc_loaded. If an NMI arrives in between,perf_event_nmi_handler()will observe !guest_lvtpc_loaded and proceed.
Quoted 5 lines
Quoted 1 line
On Intel, the NMI handler will unconditionally write
APIC_LVTPCtoUnconditionally writing
APIC_LVTCPis 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.
Quoted 31 lines
Quoted 31 lines
unmask PMIs, overwriting it to
APIC_DM_NMIand 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 becauseperf_event_nmi_handler()will observeguest_lvtpc_loadedand bail without unmasking LVTPC. Recovery only happens whenperf_put_guest_lvtpc()restores unmasked host NMIs inAPIC_LVTPC.Set
guest_lvtpc_loadedbefore writingAPIC_LVTPCinperf_load_guest_lvtpc()to prevent a racing NMI from clobbering LVTPC. The current ordering ofperf_put_guest_lvtpc()is correct,guest_lvtpc_loadedshould be cleared before writingAPIC_LVTPCto ensure that NMIs immediately start unmaskingAPIC_LVTPCas needed. Document it as such.Fixes 560ac136f25d ("perf/x86/core: Add APIs to switch to/from mediated PMI vector (for KVM)")Signed-off-by Yosry Ahmed <[email protected]>--- arch/x86/events/core.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)arch/x86/events/core.c +0
-0@@ -2224,15 +2224,26 @@ void perf_load_guest_lvtpc(u32 guest_lvtpc) {u32 masked = guest_lvtpc &APIC_LVT_MASKED ;
Quoted 10 lines
arch/x86/events/core.c void perf_load_guest_lvtpc(u32 guest_lvtpc) · line 2233
Quoted 7 lines
arch/x86/events/core.c +7
-0+/* +* 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 volatileasm().
I was on the fence, so I thought I'd include it and see what others think. I am fine either way.
Quoted 6 lines
Quoted 6 lines
arch/x86/events/core.c +0
-1apic_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);
Re: [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
In reply to Sean ChristophersonQuoted 8 lines · Sean Christopherson
Quoted 4 lines · Yosry Ahmed
When loading the guest PMU context, clear
PERF_GLOBAL_CTRLafter switching to guest LVTPC. Otherwise, the host perf NMI handler may set bits inPERF_GLOBAL_CTRLafter KVM clears it (specifically on Intel, through__intel_pmu_enable_all()).But disabling
PERF_GLOBAL_CTRLshould 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.
Sean Christopherson
maintainerRe: [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
Quoted 8 lines · Yosry Ahmed
Quoted 7 lines · Sean Christopherson
Quoted 4 lines · Yosry Ahmed
When loading the guest PMU context, clear
PERF_GLOBAL_CTRLafter switching to guest LVTPC. Otherwise, the host perf NMI handler may set bits inPERF_GLOBAL_CTRLafter KVM clears it (specifically on Intel, through__intel_pmu_enable_all()).
Quoted 7 lines
Quoted 1 line
But disabling
PERF_GLOBAL_CTRLshould 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.
Re: [PATCH 1/2] perf/x86: Prevent races between NMIs and switching LVTPC
In reply to Yosry AhmedQuoted 35 lines · Yosry Ahmed
perf_event_nmi_handler()checksguest_lvtpc_loadedto 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()updatesAPIC_LVTPCbefore settingguest_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_LVTPCto unmask PMIs, overwriting it toAPIC_DM_NMIand 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 becauseperf_event_nmi_handler()will observeguest_lvtpc_loadedand bail without unmasking LVTPC. Recovery only happens whenperf_put_guest_lvtpc()restores unmasked host NMIs inAPIC_LVTPC.Set
guest_lvtpc_loadedbefore writingAPIC_LVTPCinperf_load_guest_lvtpc()to prevent a racing NMI from clobbering LVTPC. The current ordering ofperf_put_guest_lvtpc()is correct,guest_lvtpc_loadedshould be cleared before writingAPIC_LVTPCto ensure that NMIs immediately start unmaskingAPIC_LVTPCas needed. Document it as such.Fixes 560ac136f25d ("perf/x86/core: Add APIs to switch to/from mediated PMI vector (for KVM)")Signed-off-by Yosry Ahmed <[email protected]>--- arch/x86/events/core.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)arch/x86/events/core.c +0
-0
arch/x86/events/core.c void perf_load_guest_lvtpc(u32 guest_lvtpc) · line 2228
Quoted 5 lines
arch/x86/events/core.c +2
-0@@ -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.
Quoted 22 lines
arch/x86/events/core.c +10
-1+* 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);
Re: [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
In reply to Sean ChristophersonQuoted 7 lines · Sean Christopherson
Quoted 6 lines · Yosry Ahmed
Quoted 5 lines · Sean Christopherson
Quoted 4 lines · Yosry Ahmed
When loading the guest PMU context, clear
PERF_GLOBAL_CTRLafter switching to guest LVTPC. Otherwise, the host perf NMI handler may set bits inPERF_GLOBAL_CTRLafter KVM clears it (specifically on Intel, through__intel_pmu_enable_all()).
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
But disabling
PERF_GLOBAL_CTRLshould 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()).
Re: [PATCH 1/2] perf/x86: Prevent races between NMIs and switching LVTPC
In reply to Mi, DapengQuoted 41 lines · Dapeng
Quoted 38 lines · Yosry Ahmed
perf_event_nmi_handler()checksguest_lvtpc_loadedto 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()updatesAPIC_LVTPCbefore settingguest_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_LVTPCto unmask PMIs, overwriting it toAPIC_DM_NMIand 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 becauseperf_event_nmi_handler()will observeguest_lvtpc_loadedand bail without unmasking LVTPC. Recovery only happens whenperf_put_guest_lvtpc()restores unmasked host NMIs inAPIC_LVTPC.Set
guest_lvtpc_loadedbefore writingAPIC_LVTPCinperf_load_guest_lvtpc()to prevent a racing NMI from clobbering LVTPC. The current ordering ofperf_put_guest_lvtpc()is correct,guest_lvtpc_loadedshould be cleared before writingAPIC_LVTPCto ensure that NMIs immediately start unmaskingAPIC_LVTPCas needed. Document it as such.Fixes 560ac136f25d ("perf/x86/core: Add APIs to switch to/from mediated PMI vector (for KVM)")Signed-off-by Yosry Ahmed <[email protected]>--- arch/x86/events/core.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)arch/x86/events/core.c +0
-0@@ -2224,15 +2224,26 @@ void perf_load_guest_lvtpc(u32 guest_lvtpc) {u32 masked = guest_lvtpc &APIC_LVT_MASKED ;
Quoted 5 lines
arch/x86/events/core.c void perf_load_guest_lvtpc(u32 guest_lvtpc) · line 2228
Quoted 2 lines
arch/x86/events/core.c +2
-0+/* +* 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 .."?
Quoted 24 lines
Quoted 24 lines
arch/x86/events/core.c +10
-1+* 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);
Re: [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
In reply to Mi, DapengQuoted 10 lines · Dapeng
Quoted 7 lines · Sean Christopherson
Quoted 6 lines · Yosry Ahmed
Quoted 5 lines · Sean Christopherson
Quoted 4 lines · Yosry Ahmed
When loading the guest PMU context, clear
PERF_GLOBAL_CTRLafter switching to guest LVTPC. Otherwise, the host perf NMI handler may set bits inPERF_GLOBAL_CTRLafter KVM clears it (specifically on Intel, through__intel_pmu_enable_all()).
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
But disabling
PERF_GLOBAL_CTRLshould 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/[email protected]/.
Quoted 7 lines
perf_load_guest_context()schedule out all host events, andperf_event_nmi_handler()should early return and don't touch any PMU MSRs ifactive_eventsis 0. While the issue is thatactive_eventscurrently 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.
Quoted 7 lines
It's intended to clear
GLOBAL_CTRLMSR before callingperf_load_guest_lvtpc(), it prevents triggering PMI from hit intoperf_load_guest_lvtpc().It looks the better way to fix this issue is to make
active_eventsreally track the active events (increase/decreaseactive_eventsin 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.
Re: [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
Quoted 34 lines · Yosry Ahmed
Quoted 9 lines · Dapeng
Quoted 7 lines · Sean Christopherson
Quoted 6 lines · Yosry Ahmed
Quoted 5 lines · Sean Christopherson
Quoted 4 lines · Yosry Ahmed
When loading the guest PMU context, clear
PERF_GLOBAL_CTRLafter switching to guest LVTPC. Otherwise, the host perf NMI handler may set bits inPERF_GLOBAL_CTRLafter KVM clears it (specifically on Intel, through__intel_pmu_enable_all()).Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
But disabling
PERF_GLOBAL_CTRLshould 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_CTRLafterperf_load_guest_context()is the reason KVM clearsPERF_GLOBAL_CTRLhere to begin with:
https://lore.kernel.org/all/[email protected]/.Quoted 11 lines
perf_load_guest_context()schedule out all host events, andperf_event_nmi_handler()should early return and don't touch any PMU MSRs ifactive_eventsis 0. While the issue is thatactive_eventscurrently 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_CTRLMSR before callingperf_load_guest_lvtpc(), it prevents triggering PMI from hit intoperf_load_guest_lvtpc().
Quoted 5 lines
Quoted 3 lines
It looks the better way to fix this issue is to make
active_eventsreally track the active events (increase/decreaseactive_eventsin x86_pmu_add()/x86_pmu_del()).If
active_eventsis always 0 while the guest LVTPC is loaded, then we can probably dropguest_lvtpc_loaded, right?
I think so.
Quoted 5 lines
Having perf clear
GLOBAL_CTRLwhen no events are active (e.g. inperf_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.
Quoted 4 lines
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.
Re: [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
Quoted 57 lines · Dapeng
Quoted 34 lines · Yosry Ahmed
Quoted 9 lines · Dapeng
Quoted 7 lines · Sean Christopherson
Quoted 6 lines · Yosry Ahmed
Quoted 5 lines · Sean Christopherson
Quoted 4 lines · Yosry Ahmed
When loading the guest PMU context, clear
PERF_GLOBAL_CTRLafter switching to guest LVTPC. Otherwise, the host perf NMI handler may set bits inPERF_GLOBAL_CTRLafter KVM clears it (specifically on Intel, through__intel_pmu_enable_all()).Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
But disabling
PERF_GLOBAL_CTRLshould 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_CTRLafterperf_load_guest_context()is the reason KVM clearsPERF_GLOBAL_CTRLhere to begin with:
https://lore.kernel.org/all/[email protected]/.Quoted 11 lines
perf_load_guest_context()schedule out all host events, andperf_event_nmi_handler()should early return and don't touch any PMU MSRs ifactive_eventsis 0. While the issue is thatactive_eventscurrently 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_CTRLMSR before callingperf_load_guest_lvtpc(), it prevents triggering PMI from hit intoperf_load_guest_lvtpc().Quoted 5 lines
Quoted 3 lines
It looks the better way to fix this issue is to make
active_eventsreally track the active events (increase/decreaseactive_eventsin x86_pmu_add()/x86_pmu_del()).If
active_eventsis always 0 while the guest LVTPC is loaded, then we can probably dropguest_lvtpc_loaded, right?I think so.
Quoted 5 lines
Having perf clear
GLOBAL_CTRLwhen no events are active (e.g. inperf_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 aboveCurrently
GLOBAL_CTRLkeeps 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_CTRLis usually used to ensure all counters are enabled or disabled simultaneously.
Quoted 6 lines
In theory, it can work by making
perf_load_guest_context()clearGLOBAL_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_eventsreally 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.
Re: [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
Quoted 53 lines · Yosry Ahmed
Quoted 52 lines · Dapeng
Quoted 33 lines · Yosry Ahmed
Quoted 8 lines · Dapeng
Quoted 7 lines · Sean Christopherson
Quoted 6 lines · Yosry Ahmed
Quoted 5 lines · Sean Christopherson
Quoted 4 lines · Yosry Ahmed
When loading the guest PMU context, clear
PERF_GLOBAL_CTRLafter switching to guest LVTPC. Otherwise, the host perf NMI handler may set bits inPERF_GLOBAL_CTRLafter KVM clears it (specifically on Intel, through__intel_pmu_enable_all()).Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
But disabling
PERF_GLOBAL_CTRLshould 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_CTRLafterperf_load_guest_context()is the reason KVM clearsPERF_GLOBAL_CTRLhere to begin with:
https://lore.kernel.org/all/[email protected]/.Quoted 11 lines
perf_load_guest_context()schedule out all host events, andperf_event_nmi_handler()should early return and don't touch any PMU MSRs ifactive_eventsis 0. While the issue is thatactive_eventscurrently 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_CTRLMSR before callingperf_load_guest_lvtpc(), it prevents triggering PMI from hit intoperf_load_guest_lvtpc().Quoted 5 lines
Quoted 3 lines
It looks the better way to fix this issue is to make
active_eventsreally track the active events (increase/decreaseactive_eventsin x86_pmu_add()/x86_pmu_del()).If
active_eventsis always 0 while the guest LVTPC is loaded, then we can probably dropguest_lvtpc_loaded, right?I think so.
Quoted 4 lines
Having perf clear
GLOBAL_CTRLwhen no events are active (e.g. inperf_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 aboveCurrently
GLOBAL_CTRLkeeps 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_CTRLis usually used to ensure all counters are enabled or disabled simultaneously.
Quoted 8 lines
Quoted 6 lines
In theory, it can work by making
perf_load_guest_context()clearGLOBAL_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_eventsreally 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_eventsis global, not per-CPU.
Yes, but we can extend the active_events to per-cpu, PMI is per-cpu, right?
Quoted 3 lines
- If
active_eventstracks 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.
Quoted 3 lines
-
active_eventsis 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.
Re: [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
IMO, making
active_eventsreally 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_eventsis global, not per-CPU.Yes, but we can extend the
active_eventsto per-cpu, PMI is per-cpu, right?
Maybe? I didn't take a closer look to check how straightforward it might be.
Quoted 7 lines
Quoted 3 lines
- If
active_eventstracks 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_CTRLis cleared in the entireschedule_inorschedule_outprocess until the scheduling finishes, this would prevent the PMI is triggered again in the event scheduling in theory.
Hmm yeah if we always disable the PMU first (and account for NMI delays).
Quoted 6 lines
Quoted 3 lines
-
active_eventsis 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.
That was also a sticking point in the dynamic PMU context switching RFC, Intel PT and BTS use the same PMI vector but are not mediated (so are not scheduled out).
I think changing the semantics of active_events or adding mutual exclusion between Intel PT/BTS and the mediated vPMU is a larger lift that can be explored separately.
Quoted 4 lines
An alternative way is to move
perf_load_guest_lvtpc()intoperf_load_guest_context(), callperf_load_guest_context()between the disabling perf contexts and enabling perf contexts. Actually it goes back our previous implementation.
I think this works as an alternative to this patch, although it still doesn't guarantee PERF_GLOBAL_CTRL being 0 when LVTPC is switched AFAICT. I think perf_ctx_disable() may not clear PERF_GLOBAL_CTRL if there are no core PMU events being scheduled out. Also, perf_ctx_enable() will re-enable PERF_GLOBAL_CTRL before returning anyway, so KVM still needs to clear it afterwards. So it doesn't buy us much compared to this patch, does it?
Additionally, we'd need to restore the arch hook which I think Peter didn't like in previous implementations:
https://lore.kernel.org/all/[email protected]/
Quoted 1 line
I'm fine with either way.
I think out of all the options I prefer this patch because it's a much smaller change and is more stable-friendly. I am also fine with moving perf_load_guest_lvtpc() into perf_load_guest_context() if others are okay with it.
Re: [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
Quoted 15 lines · Yosry Ahmed
Quoted 5 lines
Quoted 4 lines
Quoted 2 lines
IMO, making
active_eventsreally 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_eventsis global, not per-CPU.Yes, but we can extend the
active_eventsto per-cpu, PMI is per-cpu, right?Maybe? I didn't take a closer look to check how straightforward it might be.
Quoted 6 lines
Quoted 3 lines
- If
active_eventstracks 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_CTRLis cleared in the entireschedule_inorschedule_outprocess until the scheduling finishes, this would prevent the PMI is triggered again in the event scheduling in theory.Hmm yeah if we always disable the PMU first (and account for NMI delays).
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
-
active_eventsis 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.
That was also a sticking point in the dynamic PMU context switching RFC, Intel PT and BTS use the same PMI vector but are not mediated (so are not scheduled out).
Yes, that's what I'm concerning. PT and BTS shares same PMI with PMU, while currently we only disable the PMU instead of all including PT and BTS. If PT and BTS are not disabled, is it safe enough to switch the PMI vector?
Quoted 4 lines
I think changing the semantics of
active_eventsor adding mutual exclusion between Intel PT/BTS and the mediated vPMU is a larger lift that can be explored separately.
I agree. The fix for active_events can be done as a separate patchset, it's more complicated than I initially thought.
Quoted 5 lines
Quoted 5 lines
An alternative way is to move
perf_load_guest_lvtpc()intoperf_load_guest_context(), callperf_load_guest_context()between the disabling perf contexts and enabling perf contexts. Actually it goes back our previous implementation.
Quoted 7 lines
I think this works as an alternative to this patch, although it still doesn't guarantee
PERF_GLOBAL_CTRLbeing 0 when LVTPC is switched AFAICT. I thinkperf_ctx_disable()may not clearPERF_GLOBAL_CTRLif there are no core PMU events being scheduled out. Also,perf_ctx_enable()will re-enablePERF_GLOBAL_CTRLbefore returning anyway, so KVM still needs to clear it afterwards. So it doesn't buy us much compared to this patch, does it?
Did I miss something? perf_ctx_disable() would always call x86_pmu_disable() to disable the whole PMU if it's not skipped, it doesn't check if there are active events.
From my point of view, the inside of perf_load_guest_context() may be the best place to call perf_load_guest_lvtpc() to switch the PMI vector, especially considering if we also need to disable PT and BTS PMU before switching the PMI vector.
But as you mentioned, Peter didn't like it. perf_load_guest_context() is perf/core scope function which shared by all architectures, while perf_load_guest_lvtpc() is a x86 specific function. It makes sense that Peter doesn't want they are mixed together.
Quoted 10 lines
Additionally, we'd need to restore the arch hook which I think Peter didn't like in previous implementations:
https://lore.kernel.org/all/[email protected]/Quoted 1 line
I'm fine with either way.
I think out of all the options I prefer this patch because it's a much smaller change and is more stable-friendly. I am also fine with moving
perf_load_guest_lvtpc()intoperf_load_guest_context()if others are okay with it.
The reason that I don't like current implementation is that the PMU is not fully disabled before switching the PMI vector. Although all host events are scheduled out, it doesn't mean all PMU functionalities are disabled, like the PERF_METRICS. PERF_METRICS has no an extra independent MSR to manage the enabling, which is different with other counters. If PERF_METRICS is not disabled in the GLOBAL_CTRL, a PERF_METRICS overflow PMI could be triggered during the PMI vector switching, it could be unsafe.
As the change log says, we could move the clearing of GLOBAL_CTRL into perf_load_guest_context() and put it before the vector switching, which looks better than current implementation.
If we want to ensure the 100% safe, we may also need to disable PT and BTS along with the PMU before the vector switching.
Thanks.
Re: [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
Quoted 23 lines · Dapeng
Quoted 20 lines · Yosry Ahmed
Quoted 5 lines
Quoted 4 lines
Quoted 2 lines
IMO, making
active_eventsreally 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_eventsis global, not per-CPU.Yes, but we can extend the
active_eventsto per-cpu, PMI is per-cpu, right?Maybe? I didn't take a closer look to check how straightforward it might be.
Quoted 6 lines
Quoted 3 lines
- If
active_eventstracks 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_CTRLis cleared in the entireschedule_inorschedule_outprocess until the scheduling finishes, this would prevent the PMI is triggered again in the event scheduling in theory.Hmm yeah if we always disable the PMU first (and account for NMI delays).
Quoted 5 lines
Quoted 3 lines
-
active_eventsis 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.
Quoted 7 lines
Quoted 3 lines
That was also a sticking point in the dynamic PMU context switching RFC, Intel PT and BTS use the same PMI vector but are not mediated (so are not scheduled out).
Yes, that's what I'm concerning. PT and BTS shares same PMI with PMU, while currently we only disable the PMU instead of all including PT and BTS. If PT and BTS are not disabled, is it safe enough to switch the PMI vector?
I am not sure, but I think that's a separate issue. In the dynamic PMU context switching series, I flag Intel PT and BTS as USES_MEDIATED_PMI PMUs, and put the guest vPMU context on slowpath VM-Exit if any events in those PMUs are active, to keep the status quo (which is probably broken). If we can make Intel PT/BTS mutually exclusive with guest vPMU contexts that would simplify things, but I think that's a different issue to be tackled separately.
Quoted 21 lines
Quoted 3 lines
I think changing the semantics of
active_eventsor adding mutual exclusion between Intel PT/BTS and the mediated vPMU is a larger lift that can be explored separately.I agree. The fix for
active_eventscan be done as a separate patchset, it's more complicated than I initially thought.Quoted 5 lines
Quoted 5 lines
An alternative way is to move
perf_load_guest_lvtpc()intoperf_load_guest_context(), callperf_load_guest_context()between the disabling perf contexts and enabling perf contexts. Actually it goes back our previous implementation.Quoted 7 lines
I think this works as an alternative to this patch, although it still doesn't guarantee
PERF_GLOBAL_CTRLbeing 0 when LVTPC is switched AFAICT. I thinkperf_ctx_disable()may not clearPERF_GLOBAL_CTRLif there are no core PMU events being scheduled out. Also,perf_ctx_enable()will re-enablePERF_GLOBAL_CTRLbefore returning anyway, so KVM still needs to clear it afterwards. So it doesn't buy us much compared to this patch, does it?
Quoted 3 lines
Did I miss something?
perf_ctx_disable()would always callx86_pmu_disable()to disable the whole PMU if it's not skipped, it doesn't check if there are active events.
for_each_epc() only iterates PMUs in ctx->pmu_ctx_list. If no CPU-wide core PMU event was ever created on this CPU and the vCPU task has no task-bound core events, the core PMU won't be in pmu_ctx_list, so perf_ctx_disable() won't call x86_pmu_disable() even if a previous task-bound event left PERF_GLOBAL_CTRL non-zero, right?
Quoted 4 lines
From my point of view, the inside of
perf_load_guest_context()may be the best place to callperf_load_guest_lvtpc()to switch the PMI vector, especially considering if we also need to disable PT and BTS PMU before switching the PMI vector.
If we disable PT/BTS before switching the PMI vector I imagine we will schedule them out like core PMU events, so it shouldn't matter if we switch the PMI vector within perf_load_guest_context() or after it.
Quoted 17 lines
But as you mentioned, Peter didn't like it.
perf_load_guest_context()is perf/core scope function which shared by all architectures, whileperf_load_guest_lvtpc()is a x86 specific function. It makes sense that Peter doesn't want they are mixed together.Quoted 10 lines
Additionally, we'd need to restore the arch hook which I think Peter didn't like in previous implementations:
https://lore.kernel.org/all/[email protected]/Quoted 1 line
I'm fine with either way.
I think out of all the options I prefer this patch because it's a much smaller change and is more stable-friendly. I am also fine with moving
perf_load_guest_lvtpc()intoperf_load_guest_context()if others are okay with it.
Quoted 7 lines
The reason that I don't like current implementation is that the PMU is not fully disabled before switching the PMI vector. Although all host events are scheduled out, it doesn't mean all PMU functionalities are disabled, like the
PERF_METRICS.PERF_METRICShas no an extra independent MSR to manage the enabling, which is different with other counters. IfPERF_METRICSis not disabled in theGLOBAL_CTRL, aPERF_METRICSoverflow PMI could be triggered during the PMI vector switching, it could be unsafe.
Can PERF_METRICS PMIs be triggered if fixed counter 3 is disabled? IIUC fixed counter 3 should be disabled when host events are scheduled out in perf_load_guest_context().
Quoted 3 lines
As the change log says, we could move the clearing of
GLOBAL_CTRLintoperf_load_guest_context()and put it before the vector switching, which looks better than current implementation.
The changelog suggested moving clearing PERF_GLOBAL_CTRL into perf_load_guest_lvtpc(), which semantically isn't a great spot, but it should work. It would be more intrusive as perf has no vendor callback to just clear PERF_GLOBAL_CTRL, and x86_pmu_disable_all() might be too big of a hammer.
Moving it into perf_load_guest_context() would be more tricky because it's arch-neutral. I guess if we go back to adding an arch hook in perf_load_guest_context(), then we can move both perf_load_guest_lvtpc() and clearing GLOBAL_CTRL there (with vendor hooks to clear the MSR). That may be more fitting semantically, but would also be a heavier lift.
Quoted 2 lines
If we want to ensure the 100% safe, we may also need to disable PT and BTS along with the PMU before the vector switching.
As I mentioned above, this should be a separate problem to tackle.
Thanks for the feedback!
Re: [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
Quoted 55 lines · Yosry Ahmed
Quoted 20 lines · Dapeng
Quoted 18 lines · Yosry Ahmed
Quoted 5 lines
Quoted 4 lines
Quoted 2 lines
IMO, making
active_eventsreally 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_eventsis global, not per-CPU.Yes, but we can extend the
active_eventsto per-cpu, PMI is per-cpu, right?Maybe? I didn't take a closer look to check how straightforward it might be.
Quoted 6 lines
Quoted 3 lines
- If
active_eventstracks 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_CTRLis cleared in the entireschedule_inorschedule_outprocess until the scheduling finishes, this would prevent the PMI is triggered again in the event scheduling in theory.Hmm yeah if we always disable the PMU first (and account for NMI delays).
Quoted 3 lines
Quoted 3 lines
-
active_eventsis not necessarily 0 while the guest mediated PMU context is loaded. Other PMUs like Intel PT/BTS could have active events AFAICT.Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Yes, Intel PT is an issue. The simplest way to fix the issue is to disallow PT and mediated vPMU to work in coexistence.
That was also a sticking point in the dynamic PMU context switching RFC, Intel PT and BTS use the same PMI vector but are not mediated (so are not scheduled out).
Yes, that's what I'm concerning. PT and BTS shares same PMI with PMU, while currently we only disable the PMU instead of all including PT and BTS. If PT and BTS are not disabled, is it safe enough to switch the PMI vector?
I am not sure, but I think that's a separate issue. In the dynamic PMU context switching series, I flag Intel PT and BTS as
USES_MEDIATED_PMIPMUs, and put the guest vPMU context on slowpath VM-Exit if any events in those PMUs are active, to keep the status quo (which is probably broken). If we can make Intel PT/BTS mutually exclusive with guest vPMU contexts that would simplify things, but I think that's a different issue to be tackled separately.Quoted 18 lines
Quoted 3 lines
I think changing the semantics of
active_eventsor adding mutual exclusion between Intel PT/BTS and the mediated vPMU is a larger lift that can be explored separately.I agree. The fix for
active_eventscan be done as a separate patchset, it's more complicated than I initially thought.Quoted 11 lines
Quoted 4 lines
An alternative way is to move
perf_load_guest_lvtpc()intoperf_load_guest_context(), callperf_load_guest_context()between the disabling perf contexts and enabling perf contexts. Actually it goes back our previous implementation.I think this works as an alternative to this patch, although it still doesn't guarantee
PERF_GLOBAL_CTRLbeing 0 when LVTPC is switched AFAICT. I thinkperf_ctx_disable()may not clearPERF_GLOBAL_CTRLif there are no core PMU events being scheduled out. Also,perf_ctx_enable()will re-enablePERF_GLOBAL_CTRLbefore returning anyway, so KVM still needs to clear it afterwards. So it doesn't buy us much compared to this patch, does it?
Quoted 8 lines
Quoted 3 lines
Did I miss something?
perf_ctx_disable()would always callx86_pmu_disable()to disable the whole PMU if it's not skipped, it doesn't check if there are active events.
for_each_epc()only iterates PMUs inctx->pmu_ctx_list. If no CPU-wide core PMU event was ever created on this CPU and the vCPU task has no task-bound core events, the core PMU won't be inpmu_ctx_list, soperf_ctx_disable()won't callx86_pmu_disable()even if a previous task-bound event leftPERF_GLOBAL_CTRLnon-zero, right?
Ok, I just ignored that core PMU ctx could be added into pmu_ctx_list only when core events are created. Thanks.
Quoted 8 lines
Quoted 5 lines
From my point of view, the inside of
perf_load_guest_context()may be the best place to callperf_load_guest_lvtpc()to switch the PMI vector, especially considering if we also need to disable PT and BTS PMU before switching the PMI vector.If we disable PT/BTS before switching the PMI vector I imagine we will schedule them out like core PMU events, so it shouldn't matter if we switch the PMI vector within
perf_load_guest_context()or after it.
Yeah.
Quoted 23 lines
Quoted 16 lines
But as you mentioned, Peter didn't like it.
perf_load_guest_context()is perf/core scope function which shared by all architectures, whileperf_load_guest_lvtpc()is a x86 specific function. It makes sense that Peter doesn't want they are mixed together.Quoted 9 lines
Additionally, we'd need to restore the arch hook which I think Peter didn't like in previous implementations:
https://lore.kernel.org/all/[email protected]/Quoted 1 line
I'm fine with either way.
I think out of all the options I prefer this patch because it's a much smaller change and is more stable-friendly. I am also fine with moving
perf_load_guest_lvtpc()intoperf_load_guest_context()if others are okay with it.Quoted 7 lines
The reason that I don't like current implementation is that the PMU is not fully disabled before switching the PMI vector. Although all host events are scheduled out, it doesn't mean all PMU functionalities are disabled, like the
PERF_METRICS.PERF_METRICShas no an extra independent MSR to manage the enabling, which is different with other counters. IfPERF_METRICSis not disabled in theGLOBAL_CTRL, aPERF_METRICSoverflow PMI could be triggered during the PMI vector switching, it could be unsafe.
Quoted 3 lines
Can
PERF_METRICSPMIs be triggered if fixed counter 3 is disabled? IIUC fixed counter 3 should be disabled when host events are scheduled out inperf_load_guest_context().
Indeed. But PERF_METRICS has much gray areas which are not all described on SDM. Not sure some hidden issues could be triggered. Maybe I worried too much.
Quoted 10 lines
Quoted 4 lines
As the change log says, we could move the clearing of
GLOBAL_CTRLintoperf_load_guest_context()and put it before the vector switching, which looks better than current implementation.The changelog suggested moving clearing
PERF_GLOBAL_CTRLintoperf_load_guest_lvtpc(), which semantically isn't a great spot, but it should work. It would be more intrusive as perf has no vendor callback to just clearPERF_GLOBAL_CTRL, andx86_pmu_disable_all()might be too big of a hammer.
Quoted 6 lines
Moving it into
perf_load_guest_context()would be more tricky because it's arch-neutral. I guess if we go back to adding an arch hook inperf_load_guest_context(), then we can move bothperf_load_guest_lvtpc()and clearingGLOBAL_CTRLthere (with vendor hooks to clear the MSR). That may be more fitting semantically, but would also be a heavier lift.
OK, I'm fine with current fix then, let's see Peter/Sean's idea on this.
Thanks.
Quoted 6 lines
Quoted 3 lines
If we want to ensure the 100% safe, we may also need to disable PT and BTS along with the PMU before the vector switching.
As I mentioned above, this should be a separate problem to tackle.
Thanks for the feedback!