KVM: Fix simultaneous NMIs
PATCH v2 1/1 KVM: Fix simultaneous NMIs
If simultaneous NMIs happen, we're supposed to queue the second and next (collapsing them), but currently we sometimes collapse the second into the first.
Fix by using a counter for pending NMIs instead of a bool; since the counter limit depends on whether the processor is currently in an NMI handler, which can only be checked in vcpu context (via the NMI mask), we add a new KVM_REQ_NMI to request recalculation of the counter.
--- arch/x86/include/asm/kvm_host.h | 5 ++- arch/x86/kvm/x86.c | 48 +++++++++++++++++++++++++------------- include/linux/kvm_host.h | 1 + 3 files changed, 35 insertions(+), 19 deletions(-)
arch/x86/include/asm/kvm_host.h +3 -2
@@ -413,8 +413,9 @@ struct kvm_vcpu_arch {u32 tsc_catchup_mult;s8 tsc_catchup_shift; -bool nmi_pending; -bool nmi_injected; +atomic_t nmi_queued;/* unprocessed asynchronous NMIs */ +unsigned nmi_pending;/* NMI queued after currently running handler */ +bool nmi_injected;/* Trying to inject an NMI this entry */ struct mtrr_state_type mtrr_state;u32 pat;
arch/x86/kvm/x86.c +31 -17
127 lines not on this page · raw message
include/linux/kvm_host.h +1 -0
@@ -49,6 +49,7 @@#define KVM_REQ_EVENT 11#define KVM_REQ_APF_HALT 12#define KVM_REQ_STEAL_UPDATE 13 +#define KVM_REQ_NMI 14#define KVM_USERSPACE_IRQ_SOURCE_ID 0
Signature · 1 line
1.7.6.3
Quoted 65 lines · Avi Kivity
If simultaneous NMIs happen, we're supposed to queue the second and next (collapsing them), but currently we sometimes collapse the second into the first.
Fix by using a counter for pending NMIs instead of a bool; since the counter limit depends on whether the processor is currently in an NMI handler, which can only be checked in vcpu context (via the NMI mask), we add a new
KVM_REQ_NMIto request recalculation of the counter.Signed-off-by Avi Kivity <[email protected]>--- arch/x86/include/asm/kvm_host.h | 5 ++- arch/x86/kvm/x86.c | 48 +++++++++++++++++++++++++------------- include/linux/kvm_host.h | 1 + 3 files changed, 35 insertions(+), 19 deletions(-)arch/x86/include/asm/kvm_host.h +3
-2@@ -413,8 +413,9 @@ struct kvm_vcpu_arch {u32 tsc_catchup_mult;s8 tsc_catchup_shift; -bool nmi_pending; -bool nmi_injected; +atomic_t nmi_queued;/* unprocessed asynchronous NMIs */ +unsigned nmi_pending;/* NMI queued after currently running handler */ +bool nmi_injected;/* Trying to inject an NMI this entry */ struct mtrr_state_type mtrr_state;u32 pat;arch/x86/kvm/x86.c +4
-2@@ -83,6 +83,7 @@static void update_cr8_intercept (struct kvm_vcpu *vcpu);static int kvm_dev_ioctl_get_supported_cpuid (struct kvm_cpuid2 *cpuid,struct kvm_cpuid_entry2 __user *entries); +static void process_nmi (struct kvm_vcpu *vcpu);struct kvm_x86_ops *kvm_x86_ops;EXPORT_SYMBOL_GPL (kvm_x86_ops);@@ -359,8 +360,8 @@ void kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)void kvm_inject_nmi (struct kvm_vcpu *vcpu) { -kvm_make_request (KVM_REQ_EVENT , vcpu); - vcpu->arch.nmi_pending =1 ; +atomic_inc (&vcpu->arch.nmi_queued); +kvm_make_request (KVM_REQ_NMI , vcpu); }EXPORT_SYMBOL_GPL (kvm_inject_nmi);@@ -2827,6 +2828,7 @@ static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,static void kvm_vcpu_ioctl_x86_get_vcpu_events (struct kvm_vcpu *vcpu,struct kvm_vcpu_events *events) { +process_nmi (vcpu); events->exception.injected = vcpu->arch.exception.pending && !kvm_exception_is_soft (vcpu->arch.exception.nr);
arch/x86/kvm/x86.c static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu, · line 2851
Quoted 7 lines
arch/x86/kvm/x86.c +1
-1@@ -2844,7 +2846,7 @@ static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu, KVM_X86_SHADOW_INT_MOV_SS | KVM_X86_SHADOW_INT_STI); events->nmi.injected = vcpu->arch.nmi_injected; - events->nmi.pending = vcpu->arch.nmi_pending; + events->nmi.pending = vcpu->arch.nmi_pending !=0 ; events->nmi.masked = kvm_x86_ops->get_nmi_mask (vcpu); events->nmi.pad =0 ;
nmi_queued should also be saved and restored. Not sure if its necessary though.
Should at least reset nmi_queued somewhere (set_vcpu_events?).
arch/x86/kvm/x86.c static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu, · line 2869
Quoted 5 lines
arch/x86/kvm/x86.c +1
-0@@ -2864,6 +2866,7 @@ static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu, | KVM_VCPUEVENT_VALID_SHADOW))return -EINVAL ; +process_nmi (vcpu);
This should be after nmi fields are set, not before?
Quoted 18 lines · Marcelo Tosatti
Quoted 12 lines
@@ -2827,6 +2828,7 @@ static int kvm_vcpu_ioctl_x86_set_mce(struct
kvm_vcpu*vcpu,
static void kvm_vcpu_ioctl_x86_get_vcpu_events(structkvm_vcpu*vcpu,
structkvm_vcpu_events*events)
{
+process_nmi(vcpu);
events->exception.injected=
vcpu->arch.exception.pending&& !kvm_exception_is_soft(vcpu->arch.exception.nr);
@@ -2844,7 +2846,7 @@ static void kvm_vcpu_ioctl_x86_get_vcpu_events(structkvm_vcpu*vcpu,
KVM_X86_SHADOW_INT_MOV_SS|KVM_X86_SHADOW_INT_STI);Quoted 5 lines
events->nmi.injected=vcpu->arch.nmi_injected;
-events->nmi.pending=vcpu->arch.nmi_pending;
+events->nmi.pending=vcpu->arch.nmi_pending!= 0;
events->nmi.masked=kvm_x86_ops->get_nmi_mask(vcpu);events->nmi.pad= 0;
Quoted 4 lines
nmi_queuedshould also be saved and restored. Not sure if its necessary though.Should at least reset
nmi_queuedsomewhere (set_vcpu_events?).
Did you miss the call to process_nmi()?
We do have a small issue. If we exit during NMI-blocked-by-STI and nmi_pending == 2, then we lose the second interrupt. Should rarely happen, since external interrupts never exit in that condition, but it's a wart.
Quoted 9 lines
Quoted 6 lines
@@ -2864,6 +2866,7 @@ static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct
kvm_vcpu*vcpu,
|KVM_VCPUEVENT_VALID_SHADOW))
return-EINVAL;+
process_nmi(vcpu);This should be after nmi fields are set, not before?
It's actually to clear queued NMIs in case we set nmi_pending (which should never happen unless the machine is completely quiet, since it's asynchronous to the vcpu; same as the IRR).
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 19 lines · Avi Kivity
Quoted 18 lines · Marcelo Tosatti
Quoted 17 lines
@@ -2827,6 +2828,7 @@ static int kvm_vcpu_ioctl_x86_set_mce(struct
kvm_vcpu*vcpu,
static void kvm_vcpu_ioctl_x86_get_vcpu_events(structkvm_vcpu*vcpu,
structkvm_vcpu_events*events)
{
+process_nmi(vcpu);
events->exception.injected=
vcpu->arch.exception.pending&& !kvm_exception_is_soft(vcpu->arch.exception.nr);
@@ -2844,7 +2846,7 @@ static void kvm_vcpu_ioctl_x86_get_vcpu_events(structkvm_vcpu*vcpu,
KVM_X86_SHADOW_INT_MOV_SS|KVM_X86_SHADOW_INT_STI);
events->nmi.injected=vcpu->arch.nmi_injected;
-events->nmi.pending=vcpu->arch.nmi_pending;
+events->nmi.pending=vcpu->arch.nmi_pending!= 0;
events->nmi.masked=kvm_x86_ops->get_nmi_mask(vcpu);events->nmi.pad= 0;
Quoted 6 lines
Quoted 4 lines
nmi_queuedshould also be saved and restored. Not sure if its necessary though.Should at least reset
nmi_queuedsomewhere (set_vcpu_events?).Did you miss the call to
process_nmi()?
It transfers nmi_queued to nmi_pending with capping. What i mean is that upon system reset, nmi_queued (which refers to pre-reset system state) should be zeroed.
Quoted 4 lines
We do have a small issue. If we exit during NMI-blocked-by-STI and
nmi_pending== 2, then we lose the second interrupt. Should rarely happen, since external interrupts never exit in that condition, but it's a wart.
And the above system reset case, you should be able to handle it by saving/restoring nmi_queued (so that QEMU can zero it in vcpu_reset).
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
@@ -2864,6 +2866,7 @@ static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct
kvm_vcpu*vcpu,
|KVM_VCPUEVENT_VALID_SHADOW))
return-EINVAL;
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
+
process_nmi(vcpu);This should be after nmi fields are set, not before?
It's actually to clear queued NMIs in case we set
nmi_pending(which should never happen unless the machine is completely quiet, since it's asynchronous to the vcpu; same as the IRR).
OK.
Quoted 23 lines · Marcelo Tosatti
Quoted 22 lines · Avi Kivity
Quoted 21 lines · Marcelo Tosatti
Quoted 9 lines
@@ -2827,6 +2828,7 @@ static int kvm_vcpu_ioctl_x86_set_mce(struct
kvm_vcpu*vcpu,
static void kvm_vcpu_ioctl_x86_get_vcpu_events(structkvm_vcpu*vcpu,
structkvm_vcpu_events*events)
{+ process_nmi(vcpu); events->exception.injected = vcpu->arch.exception.pending&& !kvm_exception_is_soft(vcpu->arch.exception.nr);Quoted 8 lines
@@ -2844,7 +2846,7 @@ static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct
kvm_vcpu*vcpu,
KVM_X86_SHADOW_INT_MOV_SS|KVM_X86_SHADOW_INT_STI);events->nmi.injected = vcpu->arch.nmi_injected; - events->nmi.pending = vcpu->arch.nmi_pending; + events->nmi.pending = vcpu->arch.nmi_pending != 0; events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu); events->nmi.pad = 0;
nmi_queuedshould also be saved and restored. Not sure if its necessary though.
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Should at least reset
nmi_queuedsomewhere (set_vcpu_events?).Did you miss the call to
process_nmi()?It transfers
nmi_queuedtonmi_pendingwith capping. What i mean is that upon system reset,nmi_queued(which refers to pre-reset system state) should be zeroed.
The is get_events(); for set_events(), process_nmi() does zero nmi_queued() (and then we overwrite nmi_pending).
Quoted 8 lines
Quoted 5 lines
We do have a small issue. If we exit during NMI-blocked-by-STI and
nmi_pending== 2, then we lose the second interrupt. Should rarely happen, since external interrupts never exit in that condition, but it's a wart.And the above system reset case, you should be able to handle it by saving/restoring
nmi_queued(so that QEMU can zero it invcpu_reset).
We could just add a KVM_CAP (and flag) that extends nmi_pending from a bool to a counter.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 26 lines · Avi Kivity
Quoted 25 lines · Marcelo Tosatti
Quoted 24 lines · Avi Kivity
Quoted 22 lines · Marcelo Tosatti
Quoted 9 lines
@@ -2827,6 +2828,7 @@ static int kvm_vcpu_ioctl_x86_set_mce(struct
kvm_vcpu*vcpu,
static void kvm_vcpu_ioctl_x86_get_vcpu_events(structkvm_vcpu*vcpu,
structkvm_vcpu_events*events)
{+ process_nmi(vcpu); events->exception.injected = vcpu->arch.exception.pending&& !kvm_exception_is_soft(vcpu->arch.exception.nr);Quoted 8 lines
@@ -2844,7 +2846,7 @@ static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct
kvm_vcpu*vcpu,
KVM_X86_SHADOW_INT_MOV_SS|KVM_X86_SHADOW_INT_STI);events->nmi.injected = vcpu->arch.nmi_injected; - events->nmi.pending = vcpu->arch.nmi_pending; + events->nmi.pending = vcpu->arch.nmi_pending != 0; events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu); events->nmi.pad = 0;
nmi_queuedshould also be saved and restored. Not sure if its necessary though.Should at least reset
nmi_queuedsomewhere (set_vcpu_events?).
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Did you miss the call to
process_nmi()?It transfers
nmi_queuedtonmi_pendingwith capping. What i mean is that upon system reset,nmi_queued(which refers to pre-reset system state) should be zeroed.The is
get_events(); forset_events(),process_nmi()does zeronmi_queued()(and then we overwritenmi_pending).
Right.
Quoted 11 lines
Quoted 8 lines
Quoted 5 lines
We do have a small issue. If we exit during NMI-blocked-by-STI and
nmi_pending== 2, then we lose the second interrupt. Should rarely happen, since external interrupts never exit in that condition, but it's a wart.And the above system reset case, you should be able to handle it by saving/restoring
nmi_queued(so that QEMU can zero it invcpu_reset).We could just add a
KVM_CAP(and flag) that extendsnmi_pendingfrom a bool to a counter.
Or just add a new field to the pad.
Quoted 6 lines · Marcelo Tosatti
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
We do have a small issue. If we exit during NMI-blocked-by-STI and
nmi_pending== 2, then we lose the second interrupt. Should rarely happen, since external interrupts never exit in that condition, but it's a wart.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
And the above system reset case, you should be able to handle it by saving/restoring
nmi_queued(so that QEMU can zero it invcpu_reset).We could just add a
KVM_CAP(and flag) that extendsnmi_pendingfrom a bool to a counter.Or just add a new field to the pad.
Okay; I'll address this in a follow-on patch (my preference is making nmi_pending a counter).
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 13 lines · Avi Kivity
Quoted 12 lines · Marcelo Tosatti
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
We do have a small issue. If we exit during
NMI-blocked-by-STI and
Quoted 1 line
Quoted 1 line
Quoted 1 line
nmi_pending== 2, then we lose the second interrupt. Shouldrarely
Quoted 1 line
Quoted 1 line
Quoted 1 line
happen, since external interrupts never exit in that
condition, but
Quoted 5 lines
Quoted 4 lines
Quoted 1 line
it's a wart.
And the above system reset case, you should be able to handle it by saving/restoring
nmi_queued(so that QEMU can zero it invcpu_reset).
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
We could just add a
KVM_CAP(and flag) that extendsnmi_pendingfrom a bool to a counter.Or just add a new field to the pad.
Okay; I'll address this in a follow-on patch (my preference is making
nmi_pendinga counter).
Yet another way to do this is to redefine .injected (just in the API) to mean: inject immediately, unless blocked by interrupt shadow; in this case inject in the next instruction. No KVM_CAP or anything.
The drawback is that if we hit the corner case of two NMIs queued and held back by interrupt shadow, an older kvm live migration target will not run the guest (exit with invalid state). The advantage is that no user space or API changes are necessary.
Given that to get into this corner you need an NMI intensive load AND a sti; blah pair that spans two pages AND have the second page unavailable when those NMIs hit, I think it's better to avoid the API change. Opinions?
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 10 lines · Avi Kivity
Quoted 9 lines · Avi Kivity
Quoted 8 lines · Marcelo Tosatti
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
We do have a small issue. If we exit during
NMI-blocked-by-STI and
Quoted 1 line
Quoted 1 line
Quoted 1 line
nmi_pending== 2, then we lose the second interrupt.Should rarely
Quoted 1 line
Quoted 1 line
Quoted 1 line
happen, since external interrupts never exit in that
condition, but
Quoted 1 line
Quoted 1 line
Quoted 1 line
it's a wart.
Actually exits in the window between
- increase of nmi_queued
and
- transfer to nmi_pending/nmi_injected
Lose all nmi_queued values, no?
Quoted 28 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
And the above system reset case, you should be able to handle it by saving/restoring
nmi_queued(so that QEMU can zero it invcpu_reset).Quoted 9 lines
Quoted 5 lines
Quoted 2 lines
We could just add a
KVM_CAP(and flag) that extendsnmi_pendingfrom a bool to a counter.Or just add a new field to the pad.
Okay; I'll address this in a follow-on patch (my preference is making
nmi_pendinga counter).Yet another way to do this is to redefine .injected (just in the API) to mean: inject immediately, unless blocked by interrupt shadow; in this case inject in the next instruction. No
KVM_CAPor anything.The drawback is that if we hit the corner case of two NMIs queued and held back by interrupt shadow, an older kvm live migration target will not run the guest (exit with invalid state). The advantage is that no user space or API changes are necessary.
Given that to get into this corner you need an NMI intensive load AND a sti; blah pair that spans two pages AND have the second page unavailable when those NMIs hit, I think it's better to avoid the API change. Opinions?
Marcelo Tosatti
AppliedQuoted 12 lines · Marcelo Tosatti
Quoted 10 lines · Avi Kivity
Quoted 9 lines · Avi Kivity
Quoted 8 lines · Marcelo Tosatti
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
We do have a small issue. If we exit during
NMI-blocked-by-STI and
Quoted 1 line
Quoted 1 line
Quoted 1 line
nmi_pending== 2, then we lose the second interrupt.Should rarely
Quoted 1 line
Quoted 1 line
Quoted 1 line
happen, since external interrupts never exit in that
condition, but
Quoted 1 line
Quoted 1 line
Quoted 1 line
it's a wart.
Quoted 7 lines
Actually exits in the window between
- increase of
nmi_queued
and
- transfer to nmi_pending/nmi_injectedLose all
nmi_queuedvalues, no?
Applied the patch, please fix save/restore later, thanks.