Skip to content

Commit db21575

Browse files
sean-jcPeter Zijlstra
authored andcommitted
KVM: x86: More precisely identify NMI from guest when handling PMI
Differentiate between IRQ and NMI for KVM's PMC overflow callback, which was originally invoked in response to an NMI that arrived while the guest was running, but was inadvertantly changed to fire on IRQs as well when support for perf without PMU/NMI was added to KVM. In practice, this should be a nop as the PMC overflow callback shouldn't be reached, but it's a cheap and easy fix that also better documents the situation. Note, this also doesn't completely prevent false positives if perf somehow ends up calling into KVM, e.g. an NMI can arrive in host after KVM sets its flag. Fixes: dd60d21 ("KVM: x86: Fix perf timer mode IP reporting") Signed-off-by: Sean Christopherson <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Reviewed-by: Paolo Bonzini <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 73cd107 commit db21575

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

arch/x86/kvm/svm/svm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3931,7 +3931,7 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
39313931
}
39323932

39333933
if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3934-
kvm_before_interrupt(vcpu);
3934+
kvm_before_interrupt(vcpu, KVM_HANDLING_NMI);
39353935

39363936
kvm_load_host_xsave_state(vcpu);
39373937
stgi();

arch/x86/kvm/vmx/vmx.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6317,7 +6317,9 @@ void vmx_do_interrupt_nmi_irqoff(unsigned long entry);
63176317
static void handle_interrupt_nmi_irqoff(struct kvm_vcpu *vcpu,
63186318
unsigned long entry)
63196319
{
6320-
kvm_before_interrupt(vcpu);
6320+
bool is_nmi = entry == (unsigned long)asm_exc_nmi_noist;
6321+
6322+
kvm_before_interrupt(vcpu, is_nmi ? KVM_HANDLING_NMI : KVM_HANDLING_IRQ);
63216323
vmx_do_interrupt_nmi_irqoff(entry);
63226324
kvm_after_interrupt(vcpu);
63236325
}

arch/x86/kvm/x86.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9896,7 +9896,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
98969896
* interrupts on processors that implement an interrupt shadow, the
98979897
* stat.exits increment will do nicely.
98989898
*/
9899-
kvm_before_interrupt(vcpu);
9899+
kvm_before_interrupt(vcpu, KVM_HANDLING_IRQ);
99009900
local_irq_enable();
99019901
++vcpu->stat.exits;
99029902
local_irq_disable();

arch/x86/kvm/x86.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,16 @@ static inline bool kvm_cstate_in_guest(struct kvm *kvm)
385385
return kvm->arch.cstate_in_guest;
386386
}
387387

388-
static inline void kvm_before_interrupt(struct kvm_vcpu *vcpu)
388+
enum kvm_intr_type {
389+
/* Values are arbitrary, but must be non-zero. */
390+
KVM_HANDLING_IRQ = 1,
391+
KVM_HANDLING_NMI,
392+
};
393+
394+
static inline void kvm_before_interrupt(struct kvm_vcpu *vcpu,
395+
enum kvm_intr_type intr)
389396
{
390-
WRITE_ONCE(vcpu->arch.handling_intr_from_guest, 1);
397+
WRITE_ONCE(vcpu->arch.handling_intr_from_guest, (u8)intr);
391398
}
392399

393400
static inline void kvm_after_interrupt(struct kvm_vcpu *vcpu)
@@ -397,7 +404,7 @@ static inline void kvm_after_interrupt(struct kvm_vcpu *vcpu)
397404

398405
static inline bool kvm_handling_nmi_from_guest(struct kvm_vcpu *vcpu)
399406
{
400-
return !!vcpu->arch.handling_intr_from_guest;
407+
return vcpu->arch.handling_intr_from_guest == KVM_HANDLING_NMI;
401408
}
402409

403410
static inline bool kvm_pat_valid(u64 data)

0 commit comments

Comments
 (0)