Skip to contentSkip to conversation

KVM: Fix simultaneous NMIs

RFC Superseded Avi Kivity 3 participants 14 messages Sep 15, 2011 – Sep 20, 2011

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

RFC 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; collapsing happens when the NMI window reopens.

---

Not sure whether this interacts correctly with NMI-masked-by-STI or with save/restore.

 arch/x86/include/asm/kvm_host.h |    2 +-
 arch/x86/kvm/svm.c              |    1 +
 arch/x86/kvm/vmx.c              |    3 ++-
 arch/x86/kvm/x86.c              |   33 +++++++++++++++------------------
 arch/x86/kvm/x86.h              |    7 +++++++
 5 files changed, 26 insertions(+), 20 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
index 6ab4241..3a95885 100644
@@ -413,7 +413,7 @@ struct kvm_vcpu_arch {
 	u32  tsc_catchup_mult;
 	s8   tsc_catchup_shift;
 
-	bool nmi_pending;
+	atomic_t nmi_pending;
 	bool nmi_injected;
 
 	struct mtrr_state_type mtrr_state;
arch/x86/kvm/svm.c +1 -0
index e7ed4b1..d4c792f 100644
@@ -3609,6 +3609,7 @@ static void svm_complete_interrupts(struct vcpu_svm *svm)
 	if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
 	    && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
 		svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
+		kvm_collapse_pending_nmis(&svm->vcpu);
 		kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
 	}
 
arch/x86/kvm/vmx.c +2 -1
index a0d6bd9..745dadb 100644
@@ -4761,6 +4761,7 @@ static int handle_nmi_window(struct kvm_vcpu *vcpu)
 	cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
 	vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
 	++vcpu->stat.nmi_window_exits;
+	kvm_collapse_pending_nmis(vcpu);
 	kvm_make_request(KVM_REQ_EVENT, vcpu);
 
 	return 1;
@@ -5790,7 +5791,7 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu)
 		if (vmx_interrupt_allowed(vcpu)) {
 			vmx->soft_vnmi_blocked = 0;
 		} else if (vmx->vnmi_blocked_time > 1000000000LL &&
-			   vcpu->arch.nmi_pending) {
+			   atomic_read(&vcpu->arch.nmi_pending)) {
 			/*
 			 * This CPU don't support us in finding the end of an
 			 * NMI-blocked window if the guest runs with IRQs
arch/x86/kvm/x86.c +15 -18
index 6b37f18..d4f45e0 100644
@@ -359,8 +359,8 @@ void kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
 
 void kvm_inject_nmi(struct kvm_vcpu *vcpu)
 {
+	atomic_inc(&vcpu->arch.nmi_pending);
 	kvm_make_request(KVM_REQ_EVENT, vcpu);
-	vcpu->arch.nmi_pending = 1;
 }
 EXPORT_SYMBOL_GPL(kvm_inject_nmi);
 
@@ -2844,7 +2844,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 = atomic_read(&vcpu->arch.nmi_pending) != 0;
 	events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
 	events->nmi.pad = 0;
 
@@ -2878,7 +2878,7 @@ static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
 
 	vcpu->arch.nmi_injected = events->nmi.injected;
 	if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
-		vcpu->arch.nmi_pending = events->nmi.pending;
+		atomic_set(&vcpu->arch.nmi_pending, events->nmi.pending);
 	kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
 
 	if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR)
@@ -4763,7 +4763,7 @@ int kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
 	kvm_set_rflags(vcpu, ctxt->eflags);
 
 	if (irq == NMI_VECTOR)
-		vcpu->arch.nmi_pending = false;
+		atomic_set(&vcpu->arch.nmi_pending, 0);
 	else
 		vcpu->arch.interrupt.pending = false;
 
@@ -5570,9 +5570,9 @@ static void inject_pending_event(struct kvm_vcpu *vcpu)
 	}
 
 	/* try to inject new event if pending */
-	if (vcpu->arch.nmi_pending) {
+	if (atomic_read(&vcpu->arch.nmi_pending)) {
 		if (kvm_x86_ops->nmi_allowed(vcpu)) {
-			vcpu->arch.nmi_pending = false;
+			atomic_dec(&vcpu->arch.nmi_pending);
 			vcpu->arch.nmi_injected = true;
 			kvm_x86_ops->set_nmi(vcpu);
 		}
@@ -5604,10 +5604,14 @@ static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)
 	}
 }
 
+static bool nmi_in_progress(struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.nmi_injected || kvm_x86_ops->get_nmi_mask(vcpu);
+}
+
 static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 {
 	int r;
-	bool nmi_pending;
 	bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
 		vcpu->run->request_interrupt_window;
 
@@ -5654,19 +5658,12 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 	if (unlikely(r))
 		goto out;
 
-	/*
-	 * An NMI can be injected between local nmi_pending read and
-	 * vcpu->arch.nmi_pending read inside inject_pending_event().
-	 * But in that case, KVM_REQ_EVENT will be set, which makes
-	 * the race described above benign.
-	 */
-	nmi_pending = ACCESS_ONCE(vcpu->arch.nmi_pending);
-
 	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
 		inject_pending_event(vcpu);
 
 		/* enable NMI/IRQ window open exits if needed */
-		if (nmi_pending)
+		if (atomic_read(&vcpu->arch.nmi_pending)
+		    && nmi_in_progress(vcpu))
 			kvm_x86_ops->enable_nmi_window(vcpu);
 		else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
 			kvm_x86_ops->enable_irq_window(vcpu);
@@ -6374,7 +6371,7 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
 
 int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
 {
-	vcpu->arch.nmi_pending = false;
+	atomic_set(&vcpu->arch.nmi_pending, 0);
 	vcpu->arch.nmi_injected = false;
 
 	vcpu->arch.switch_db_regs = 0;
@@ -6649,7 +6646,7 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
 		!vcpu->arch.apf.halted)
 		|| !list_empty_careful(&vcpu->async_pf.done)
 		|| vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
-		|| vcpu->arch.nmi_pending ||
+		|| atomic_read(&vcpu->arch.nmi_pending) ||
 		(kvm_arch_interrupt_allowed(vcpu) &&
 		 kvm_cpu_has_interrupt(vcpu));
 }
arch/x86/kvm/x86.h +7 -0
index d36fe23..ed04e2b 100644
@@ -125,4 +125,11 @@ int kvm_write_guest_virt_system(struct x86_emulate_ctxt *ctxt,
 	gva_t addr, void *val, unsigned int bytes,
 	struct x86_exception *exception);
 
+static inline void kvm_collapse_pending_nmis(struct kvm_vcpu *vcpu)
+{
+	/* Collapse all NMIs queued while an NMI handler was running to one */
+	if (atomic_read(&vcpu->arch.nmi_pending))
+		atomic_set(&vcpu->arch.nmi_pending, 1);
+}
+
 #endif
Signature · 1 line
1.7.6.3
#1 lore raw
Quoted 3 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.

Can you describe the race in a few more details here ("sometimes" sounds like "I don't know when" :) )?

Quoted 68 lines

Fix by using a counter for pending NMIs instead of a bool; collapsing happens when the NMI window reopens.

---

Not sure whether this interacts correctly with NMI-masked-by-STI or with save/restore.

 arch/x86/include/asm/kvm_host.h |    2 +-
 arch/x86/kvm/svm.c              |    1 +
 arch/x86/kvm/vmx.c              |    3 ++-
 arch/x86/kvm/x86.c              |   33 +++++++++++++++------------------
 arch/x86/kvm/x86.h              |    7 +++++++
 5 files changed, 26 insertions(+), 20 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
index 6ab4241..3a95885 100644
@@ -413,7 +413,7 @@ struct kvm_vcpu_arch {
 	u32  tsc_catchup_mult;
 	s8   tsc_catchup_shift;
 
-	bool nmi_pending;
+	atomic_t nmi_pending;
 	bool nmi_injected;
 
 	struct mtrr_state_type mtrr_state;
arch/x86/kvm/svm.c +1 -0
index e7ed4b1..d4c792f 100644
@@ -3609,6 +3609,7 @@ static void svm_complete_interrupts(struct vcpu_svm *svm)
 	if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
 	    && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
 		svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
+		kvm_collapse_pending_nmis(&svm->vcpu);
 		kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
 	}
 
arch/x86/kvm/vmx.c +2 -1
index a0d6bd9..745dadb 100644
@@ -4761,6 +4761,7 @@ static int handle_nmi_window(struct kvm_vcpu *vcpu)
 	cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
 	vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
 	++vcpu->stat.nmi_window_exits;
+	kvm_collapse_pending_nmis(vcpu);
 	kvm_make_request(KVM_REQ_EVENT, vcpu);
 
 	return 1;
@@ -5790,7 +5791,7 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu)
 		if (vmx_interrupt_allowed(vcpu)) {
 			vmx->soft_vnmi_blocked = 0;
 		} else if (vmx->vnmi_blocked_time > 1000000000LL &&
-			   vcpu->arch.nmi_pending) {
+			   atomic_read(&vcpu->arch.nmi_pending)) {
 			/*
 			 * This CPU don't support us in finding the end of an
 			 * NMI-blocked window if the guest runs with IRQs
arch/x86/kvm/x86.c +0 -0
index 6b37f18..d4f45e0 100644

arch/x86/kvm/x86.c void kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault) · line 363

Quoted 6 lines
arch/x86/kvm/x86.c +1 -1
@@ -359,8 +359,8 @@ void kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
 
 void kvm_inject_nmi(struct kvm_vcpu *vcpu)
 {
+	atomic_inc(&vcpu->arch.nmi_pending);
 	kvm_make_request(KVM_REQ_EVENT, vcpu);
-	vcpu->arch.nmi_pending = 1;

Does the reordering matter? Do we need barriers?

Quoted 31 lines
arch/x86/kvm/x86.c +3 -3
 }
 EXPORT_SYMBOL_GPL(kvm_inject_nmi);
 
@@ -2844,7 +2844,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 = atomic_read(&vcpu->arch.nmi_pending) != 0;
 	events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
 	events->nmi.pad = 0;
 
@@ -2878,7 +2878,7 @@ static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
 
 	vcpu->arch.nmi_injected = events->nmi.injected;
 	if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
-		vcpu->arch.nmi_pending = events->nmi.pending;
+		atomic_set(&vcpu->arch.nmi_pending, events->nmi.pending);
 	kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
 
 	if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR)
@@ -4763,7 +4763,7 @@ int kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
 	kvm_set_rflags(vcpu, ctxt->eflags);
 
 	if (irq == NMI_VECTOR)
-		vcpu->arch.nmi_pending = false;
+		atomic_set(&vcpu->arch.nmi_pending, 0);
 	else
 		vcpu->arch.interrupt.pending = false;
 

arch/x86/kvm/x86.c static void inject_pending_event(struct kvm_vcpu *vcpu) · line 5575

Quoted 8 lines
arch/x86/kvm/x86.c +2 -2
@@ -5570,9 +5570,9 @@ static void inject_pending_event(struct kvm_vcpu *vcpu)
 	}
 
 	/* try to inject new event if pending */
-	if (vcpu->arch.nmi_pending) {
+	if (atomic_read(&vcpu->arch.nmi_pending)) {
 		if (kvm_x86_ops->nmi_allowed(vcpu)) {
-			vcpu->arch.nmi_pending = false;
+			atomic_dec(&vcpu->arch.nmi_pending);

Here we lost NMIs in the past by overwriting nmi_pending while another one was already queued, right?

Quoted 28 lines
arch/x86/kvm/x86.c +5 -6
 			vcpu->arch.nmi_injected = true;
 			kvm_x86_ops->set_nmi(vcpu);
 		}
@@ -5604,10 +5604,14 @@ static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)
 	}
 }
 
+static bool nmi_in_progress(struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.nmi_injected || kvm_x86_ops->get_nmi_mask(vcpu);
+}
+
 static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 {
 	int r;
-	bool nmi_pending;
 	bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
 		vcpu->run->request_interrupt_window;
 
@@ -5654,19 +5658,12 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 	if (unlikely(r))
 		goto out;
 
-	/*
-	 * An NMI can be injected between local nmi_pending read and
-	 * vcpu->arch.nmi_pending read inside inject_pending_event().
-	 * But in that case, KVM_REQ_EVENT will be set, which makes
-	 * the race described above benign.

arch/x86/kvm/x86.c static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · line 5666

Quoted 10 lines
arch/x86/kvm/x86.c +2 -4
-	 */
-	nmi_pending = ACCESS_ONCE(vcpu->arch.nmi_pending);
-
 	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
 		inject_pending_event(vcpu);
 
 		/* enable NMI/IRQ window open exits if needed */
-		if (nmi_pending)
+		if (atomic_read(&vcpu->arch.nmi_pending)
+		    && nmi_in_progress(vcpu))

Is nmi_pending && !nmi_in_progress possible at all? Is it rather a BUG condition? If not, what will happen next?

Quoted 26 lines
arch/x86/kvm/x86.c +2 -2
 			kvm_x86_ops->enable_nmi_window(vcpu);
 		else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
 			kvm_x86_ops->enable_irq_window(vcpu);
@@ -6374,7 +6371,7 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
 
 int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
 {
-	vcpu->arch.nmi_pending = false;
+	atomic_set(&vcpu->arch.nmi_pending, 0);
 	vcpu->arch.nmi_injected = false;
 
 	vcpu->arch.switch_db_regs = 0;
@@ -6649,7 +6646,7 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
 		!vcpu->arch.apf.halted)
 		|| !list_empty_careful(&vcpu->async_pf.done)
 		|| vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
-		|| vcpu->arch.nmi_pending ||
+		|| atomic_read(&vcpu->arch.nmi_pending) ||
 		(kvm_arch_interrupt_allowed(vcpu) &&
 		 kvm_cpu_has_interrupt(vcpu));
 }
arch/x86/kvm/x86.h +0 -0
index d36fe23..ed04e2b 100644

arch/x86/kvm/x86.h int kvm_write_guest_virt_system(struct x86_emulate_ctxt *ctxt, · line 132

Quoted 8 lines
arch/x86/kvm/x86.h +5 -0
@@ -125,4 +125,11 @@ int kvm_write_guest_virt_system(struct x86_emulate_ctxt *ctxt,
 	gva_t addr, void *val, unsigned int bytes,
 	struct x86_exception *exception);
 
+static inline void kvm_collapse_pending_nmis(struct kvm_vcpu *vcpu)
+{
+	/* Collapse all NMIs queued while an NMI handler was running to one */
+	if (atomic_read(&vcpu->arch.nmi_pending))
+		atomic_set(&vcpu->arch.nmi_pending, 1);

Is it OK that NMIs injected after the collapse will increment this to > 1 again? Or is that impossible?

arch/x86/kvm/x86.h int kvm_write_guest_virt_system(struct x86_emulate_ctxt *ctxt, · line 135

Quoted 3 lines
arch/x86/kvm/x86.h +2 -0
+}
+
 #endif

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
author
#2 lore raw
Quoted 7 lines · Jan Kiszka
Quoted 3 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.

Can you describe the race in a few more details here ("sometimes" sounds like "I don't know when" :) )?

In this case it was "I'm in a hurry".

Quoted 8 lines
Quoted 6 lines

void kvm_inject_nmi(struct kvm_vcpu *vcpu)
{
+ atomic_inc(&vcpu->arch.nmi_pending);
kvm_make_request(KVM_REQ_EVENT, vcpu);
- vcpu->arch.nmi_pending = 1;

Does the reordering matter?

I think so. Suppose the vcpu enters just after kvm_make_request(); it sees KVM_REQ_EVENT and clears it, but doesn't see nmi_pending because it wasn't set set. Then comes a kick, the guest is reentered with nmi_pending set but KVM_REQ_EVENT clear and sails through the check and enters the guest. The NMI is delayed until the next KVM_REQ_EVENT.

Quoted 1 line

Do we need barriers?

Yes.

Quoted 11 lines
Quoted 10 lines

@@ -5570,9 +5570,9 @@ static void inject_pending_event(struct kvm_vcpu *vcpu)
}

/* try to inject new event if pending */
- if (vcpu->arch.nmi_pending) {
+ if (atomic_read(&vcpu->arch.nmi_pending)) {
if (kvm_x86_ops->nmi_allowed(vcpu)) {
- vcpu->arch.nmi_pending = false;
+ atomic_dec(&vcpu->arch.nmi_pending);

Quoted 2 lines

Here we lost NMIs in the past by overwriting nmi_pending while another one was already queued, right?

One place, yes. The other is kvm_inject_nmi() - if the first nmi didn't get picked up by the vcpu by the time the second nmi arrives, we lose the second nmi.

Quoted 9 lines
Quoted 7 lines
  	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
  		inject_pending_event(vcpu);

/* enable NMI/IRQ window open exits if needed */
- if (nmi_pending)
+ if (atomic_read(&vcpu->arch.nmi_pending)
+ && nmi_in_progress(vcpu))

Is nmi_pending&& !nmi_in_progress possible at all?

Yes, due to NMI-blocked-by-STI. A really touchy area.

Quoted 2 lines

Is it rather a BUG
condition?

No.

Quoted 1 line

If not, what will happen next?

The NMI window will open and we'll inject the NMI. But I think we have a bug here - we should only kvm_collapse_nmis() if an NMI handler was indeed running, yet we do it unconditionally.

Quoted 10 lines
Quoted 6 lines

+static inline void kvm_collapse_pending_nmis(struct kvm_vcpu *vcpu) +{
+ /* Collapse all NMIs queued while an NMI handler was running to one */
+ if (atomic_read(&vcpu->arch.nmi_pending))
+ atomic_set(&vcpu->arch.nmi_pending, 1);

Is it OK that NMIs injected after the collapse will increment this to> 1 again? Or is that impossible?

It's possible and okay. We're now completing execution of IRET. Doing atomic_set() after atomic_inc() means the NMI happened before IRET completed, and vice versa. Since these events are asynchronous, we're free to choose one or the other (a self-IPI-NMI just before the IRET must be swallowed, and a self-IPI-NMI just after the IRET would only be executed after the next time around the handler).

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#3 lore raw
Quoted 18 lines · Avi Kivity
Quoted 7 lines · Jan Kiszka
Quoted 3 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.

Can you describe the race in a few more details here ("sometimes" sounds like "I don't know when" :) )?

In this case it was "I'm in a hurry".

Quoted 7 lines
Quoted 6 lines

void kvm_inject_nmi(struct kvm_vcpu *vcpu)
{
+ atomic_inc(&vcpu->arch.nmi_pending);
kvm_make_request(KVM_REQ_EVENT, vcpu);
- vcpu->arch.nmi_pending = 1;

Quoted 7 lines
Quoted 1 line

Does the reordering matter?

I think so. Suppose the vcpu enters just after kvm_make_request(); it sees KVM_REQ_EVENT and clears it, but doesn't see nmi_pending because it wasn't set set. Then comes a kick, the guest is reentered with nmi_pending set but KVM_REQ_EVENT clear and sails through the check and enters the guest. The NMI is delayed until the next KVM_REQ_EVENT.

That makes sense - and the old code looks more strange now.

Quoted 16 lines
Quoted 2 lines

Do we need barriers?

Yes.

Quoted 11 lines
Quoted 10 lines

@@ -5570,9 +5570,9 @@ static void inject_pending_event(struct kvm_vcpu *vcpu)
}

/* try to inject new event if pending */
- if (vcpu->arch.nmi_pending) {
+ if (atomic_read(&vcpu->arch.nmi_pending)) {
if (kvm_x86_ops->nmi_allowed(vcpu)) {
- vcpu->arch.nmi_pending = false;
+ atomic_dec(&vcpu->arch.nmi_pending);

Quoted 6 lines
Quoted 2 lines

Here we lost NMIs in the past by overwriting nmi_pending while another one was already queued, right?

One place, yes. The other is kvm_inject_nmi() - if the first nmi didn't get picked up by the vcpu by the time the second nmi arrives, we lose the second nmi.

Thinking this through again, it's actually not yet clear to me what we are modeling here: If two NMI events arrive almost perfectly in parallel, does the real hardware guarantee that they will always cause two NMI events in the CPU? Then this is required.

Otherwise I just lost understanding again why we were loosing NMIs here and in kvm_inject_nmi (maybe elsewhere then?).

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
  	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
  		inject_pending_event(vcpu);
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

/* enable NMI/IRQ window open exits if needed */
- if (nmi_pending)
+ if (atomic_read(&vcpu->arch.nmi_pending)
+ && nmi_in_progress(vcpu))

Is nmi_pending&& !nmi_in_progress possible at all?

Yes, due to NMI-blocked-by-STI. A really touchy area.

And we don't need the window exit notification then? I don't understand what nmi_in_progress is supposed to do here.

Quoted 9 lines
Quoted 3 lines

Is it rather a BUG
condition?

No.

Quoted 1 line

If not, what will happen next?

The NMI window will open and we'll inject the NMI.

How will we know this? We do not request the exit, that's my worry.

Quoted 15 lines

But I think we have
a bug here - we should only kvm_collapse_nmis() if an NMI handler was indeed running, yet we do it unconditionally.

Quoted 10 lines
Quoted 6 lines

+static inline void kvm_collapse_pending_nmis(struct kvm_vcpu *vcpu) +{
+ /* Collapse all NMIs queued while an NMI handler was running to one */
+ if (atomic_read(&vcpu->arch.nmi_pending))
+ atomic_set(&vcpu->arch.nmi_pending, 1);

Is it OK that NMIs injected after the collapse will increment this to> 1 again? Or is that impossible?

Quoted 6 lines

It's possible and okay. We're now completing execution of IRET. Doing atomic_set() after atomic_inc() means the NMI happened before IRET completed, and vice versa. Since these events are asynchronous, we're free to choose one or the other (a self-IPI-NMI just before the IRET must be swallowed, and a self-IPI-NMI just after the IRET would only be executed after the next time around the handler).

Need to think through this separately.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
author
#4 lore raw
Quoted 8 lines · Jan Kiszka
Quoted 6 lines

I think so. Suppose the vcpu enters just after kvm_make_request(); it sees KVM_REQ_EVENT and clears it, but doesn't see nmi_pending because it wasn't set set. Then comes a kick, the guest is reentered with nmi_pending set but KVM_REQ_EVENT clear and sails through the check and enters the guest. The NMI is delayed until the next KVM_REQ_EVENT.

That makes sense - and the old code looks more strange now.

I think it dates to the time all NMIs were synchronous.

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 7 lines
   	/* try to inject new event if pending */
  -	if (vcpu->arch.nmi_pending) {
  +	if (atomic_read(&vcpu->arch.nmi_pending)) {
   		if (kvm_x86_ops->nmi_allowed(vcpu)) {
  -			vcpu->arch.nmi_pending = false;
  +			atomic_dec(&vcpu->arch.nmi_pending);

Here we lost NMIs in the past by overwriting nmi_pending while another one was already queued, right?

Quoted 8 lines
Quoted 3 lines

One place, yes. The other is kvm_inject_nmi() - if the first nmi didn't get picked up by the vcpu by the time the second nmi arrives, we lose the second nmi.

Thinking this through again, it's actually not yet clear to me what we are modeling here: If two NMI events arrive almost perfectly in parallel, does the real hardware guarantee that they will always cause two NMI events in the CPU? Then this is required.

It's not 100% clear from the SDM, but this is what I understood from it. And it's needed - the NMI handlers are now being reworked to handle just one NMI source (hopefully the cheapest) in the handler, and if we detect a back-to-back NMI, handle all possible NMI sources. This optimization is needed in turn so we can use Jeremy's paravirt spinlock framework, which requires a sleep primitive and a wake-up-even-if-the-sleeper-has-interrupts-disabled primitive. i thought of using HLT and NMIs respectively, but that means we need a cheap handler (i.e. don't go reading PMU MSRs).

Quoted 2 lines

Otherwise I just lost understanding again why we were loosing NMIs here and in kvm_inject_nmi (maybe elsewhere then?).

Because of that.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
   	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
   		inject_pending_event(vcpu);
   		/* enable NMI/IRQ window open exits if needed */

- if (nmi_pending)
+ if (atomic_read(&vcpu->arch.nmi_pending)
+ && nmi_in_progress(vcpu))

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Is nmi_pending&& !nmi_in_progress possible at all?

Yes, due to NMI-blocked-by-STI. A really touchy area.

And we don't need the window exit notification then? I don't understand what nmi_in_progress is supposed to do here.

We need the window notification in both cases. If we're recovering from STI, then we don't need to collapse NMIs. If we're completing an NMI handler, then we do need to collapse NMIs (since the queue length is two, and we just completed one).

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

If not, what will happen next?

The NMI window will open and we'll inject the NMI.

How will we know this? We do not request the exit, that's my worry.

I think we do? Oh, but this patch breaks it.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#5 lore raw
Quoted 58 lines · Avi Kivity
Quoted 8 lines · Jan Kiszka
Quoted 6 lines

I think so. Suppose the vcpu enters just after kvm_make_request(); it sees KVM_REQ_EVENT and clears it, but doesn't see nmi_pending because it wasn't set set. Then comes a kick, the guest is reentered with nmi_pending set but KVM_REQ_EVENT clear and sails through the check and enters the guest. The NMI is delayed until the next KVM_REQ_EVENT.

That makes sense - and the old code looks more strange now.

I think it dates to the time all NMIs were synchronous.

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 7 lines
   	/* try to inject new event if pending */
  -	if (vcpu->arch.nmi_pending) {
  +	if (atomic_read(&vcpu->arch.nmi_pending)) {
   		if (kvm_x86_ops->nmi_allowed(vcpu)) {
  -			vcpu->arch.nmi_pending = false;
  +			atomic_dec(&vcpu->arch.nmi_pending);

Here we lost NMIs in the past by overwriting nmi_pending while another one was already queued, right?

Quoted 8 lines
Quoted 3 lines

One place, yes. The other is kvm_inject_nmi() - if the first nmi didn't get picked up by the vcpu by the time the second nmi arrives, we lose the second nmi.

Thinking this through again, it's actually not yet clear to me what we are modeling here: If two NMI events arrive almost perfectly in parallel, does the real hardware guarantee that they will always cause two NMI events in the CPU? Then this is required.

It's not 100% clear from the SDM, but this is what I understood from it. And it's needed - the NMI handlers are now being reworked to handle just one NMI source (hopefully the cheapest) in the handler, and if we detect a back-to-back NMI, handle all possible NMI sources. This optimization is needed in turn so we can use Jeremy's paravirt spinlock framework, which requires a sleep primitive and a wake-up-even-if-the-sleeper-has-interrupts-disabled primitive. i thought of using HLT and NMIs respectively, but that means we need a cheap handler (i.e. don't go reading PMU MSRs).

Quoted 2 lines

Otherwise I just lost understanding again why we were loosing NMIs here and in kvm_inject_nmi (maybe elsewhere then?).

Because of that.

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 8 lines
   	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
   		inject_pending_event(vcpu);
   		/* enable NMI/IRQ window open exits if needed */

- if (nmi_pending)
+ if (atomic_read(&vcpu->arch.nmi_pending)
+ && nmi_in_progress(vcpu))

Is nmi_pending&& !nmi_in_progress possible at all?

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Yes, due to NMI-blocked-by-STI. A really touchy area.

And we don't need the window exit notification then? I don't understand what nmi_in_progress is supposed to do here.

We need the window notification in both cases. If we're recovering from STI, then we don't need to collapse NMIs. If we're completing an NMI handler, then we do need to collapse NMIs (since the queue length is two, and we just completed one).

I don't understand what is the point with nmi_in_progress, and the above hunk, either. Can't inject_nmi do:

if (nmi_injected + atomic_read(nmi_pending) < 2)
atomic_inc(nmi_pending)

Instead of collapsing somewhere else? You'd also have to change nmi_injected handling in arch code so its value is not "hidden", in complete_interrupts().

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

If not, what will happen next?

The NMI window will open and we'll inject the NMI.

How will we know this? We do not request the exit, that's my worry.

I think we do? Oh, but this patch breaks it.

author
#6 lore raw
Quoted 10 lines · Marcelo Tosatti
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

Yes, due to NMI-blocked-by-STI. A really touchy area.

And we don't need the window exit notification then? I don't understand what nmi_in_progress is supposed to do here.

We need the window notification in both cases. If we're recovering from STI, then we don't need to collapse NMIs. If we're completing an NMI handler, then we do need to collapse NMIs (since the queue length is two, and we just completed one).

Quoted 7 lines

I don't understand what is the point with nmi_in_progress, and the above hunk, either. Can't inject_nmi do:

if (nmi_injected + atomic_read(nmi_pending)< 2)
atomic_inc(nmi_pending)

Instead of collapsing somewhere else?

We could. It's not atomic though - two threads executing in parallel could raise the value to three. Could do a cmpxchg loop does an increment bounded to two. I guess this is a lot clearer, thanks.

Quoted 3 lines

You'd also have to change
nmi_injected handling in arch code so its value is not "hidden", in complete_interrupts().

Or maybe make raising nmi_injected not decrement nmi_pending. So:

nmi_pending: total number of interrupts in queue
nmi_injected: of these, how many are currently being injected

yes?

Signature · 1 line
error compiling committee.c: too many arguments to function
#7 lore raw
Quoted 28 lines · Avi Kivity
Quoted 10 lines · Marcelo Tosatti
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

Yes, due to NMI-blocked-by-STI. A really touchy area.

And we don't need the window exit notification then? I don't understand what nmi_in_progress is supposed to do here.

We need the window notification in both cases. If we're recovering from STI, then we don't need to collapse NMIs. If we're completing an NMI handler, then we do need to collapse NMIs (since the queue length is two, and we just completed one).

Quoted 7 lines

I don't understand what is the point with nmi_in_progress, and the above hunk, either. Can't inject_nmi do:

if (nmi_injected + atomic_read(nmi_pending)< 2)
atomic_inc(nmi_pending)

Instead of collapsing somewhere else?

We could. It's not atomic though - two threads executing in parallel could raise the value to three. Could do a cmpxchg loop does an increment bounded to two. I guess this is a lot clearer, thanks.

Quoted 3 lines

You'd also have to change
nmi_injected handling in arch code so its value is not "hidden", in complete_interrupts().

Quoted 6 lines

Or maybe make raising nmi_injected not decrement nmi_pending. So:

nmi_pending: total number of interrupts in queue
nmi_injected: of these, how many are currently being injected

yes?

Yes, at the expense of decrementing on subarch code (which is fine, apparently).

author
#8 lore raw
Quoted 31 lines · Marcelo Tosatti
Quoted 30 lines · Avi Kivity
Quoted 10 lines · Marcelo Tosatti
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

Yes, due to NMI-blocked-by-STI. A really touchy area.

And we don't need the window exit notification then? I don't understand what nmi_in_progress is supposed to do here.

We need the window notification in both cases. If we're recovering from STI, then we don't need to collapse NMIs. If we're completing an NMI handler, then we do need to collapse NMIs (since the queue length is two, and we just completed one).

Quoted 7 lines

I don't understand what is the point with nmi_in_progress, and the above hunk, either. Can't inject_nmi do:

if (nmi_injected + atomic_read(nmi_pending)< 2)
atomic_inc(nmi_pending)

Instead of collapsing somewhere else?

We could. It's not atomic though - two threads executing in parallel could raise the value to three. Could do a cmpxchg loop does an increment bounded to two. I guess this is a lot clearer, thanks.

Quoted 3 lines

You'd also have to change
nmi_injected handling in arch code so its value is not "hidden", in complete_interrupts().

Or maybe make raising nmi_injected not decrement nmi_pending. So:

Quoted 8 lines
Quoted 4 lines

nmi_pending: total number of interrupts in queue
nmi_injected: of these, how many are currently being injected

yes?

Yes, at the expense of decrementing on subarch code (which is fine, apparently).

Hm, we have no place to decrement. We need to do that when IRET executes, but we don't want to request an NMI window exit in the common case of nmi_pending = 1.

Signature · 1 line
error compiling committee.c: too many arguments to function
author
#9 lore raw
Quoted 39 lines · Avi Kivity
Quoted 38 lines · Marcelo Tosatti
Quoted 4 lines · Avi Kivity
Quoted 3 lines · Marcelo Tosatti
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines

Yes, due to NMI-blocked-by-STI. A really touchy area.

And we don't need the window exit notification then? I don't

understand

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

what nmi_in_progress is supposed to do here.

We need the window notification in both cases. If we're

recovering

Quoted 1 line
Quoted 1 line
Quoted 1 line

from STI, then we don't need to collapse NMIs. If we're

completing

Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

an NMI handler, then we do need to collapse NMIs (since the queue length is two, and we just completed one).

I don't understand what is the point with nmi_in_progress, and the

above

Quoted 21 lines
Quoted 6 lines

hunk, either. Can't inject_nmi do:

if (nmi_injected + atomic_read(nmi_pending)< 2)
atomic_inc(nmi_pending)

Instead of collapsing somewhere else?

We could. It's not atomic though - two threads executing in parallel could raise the value to three. Could do a cmpxchg loop does an increment bounded to two. I guess this is a lot clearer, thanks.

Quoted 3 lines

You'd also have to change
nmi_injected handling in arch code so its value is not "hidden", in complete_interrupts().

Or maybe make raising nmi_injected not decrement nmi_pending. So:

nmi_pending: total number of interrupts in queue
nmi_injected: of these, how many are currently being injected

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

yes?

Yes, at the expense of decrementing on subarch code (which is fine, apparently).

Hm, we have no place to decrement. We need to do that when IRET executes, but we don't want to request an NMI window exit in the common case of nmi_pending = 1.

I guess we have to change kvm_inject_nmi to run in vcpu context, where it has access to more stuff.

Signature · 1 line
error compiling committee.c: too many arguments to function
#10 lore raw
In reply to Avi Kivity
Quoted 35 lines · Avi Kivity
Quoted 34 lines · Marcelo Tosatti
Quoted 33 lines · Avi Kivity
Quoted 10 lines · Marcelo Tosatti
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

Yes, due to NMI-blocked-by-STI. A really touchy area.

And we don't need the window exit notification then? I don't understand what nmi_in_progress is supposed to do here.

We need the window notification in both cases. If we're recovering from STI, then we don't need to collapse NMIs. If we're completing an NMI handler, then we do need to collapse NMIs (since the queue length is two, and we just completed one).

Quoted 7 lines

I don't understand what is the point with nmi_in_progress, and the above hunk, either. Can't inject_nmi do:

if (nmi_injected + atomic_read(nmi_pending)< 2)
atomic_inc(nmi_pending)

Instead of collapsing somewhere else?

We could. It's not atomic though - two threads executing in parallel could raise the value to three. Could do a cmpxchg loop does an increment bounded to two. I guess this is a lot clearer, thanks.

Quoted 3 lines

You'd also have to change
nmi_injected handling in arch code so its value is not "hidden", in complete_interrupts().

Or maybe make raising nmi_injected not decrement nmi_pending. So:

nmi_pending: total number of interrupts in queue
nmi_injected: of these, how many are currently being injected

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

yes?

Yes, at the expense of decrementing on subarch code (which is fine, apparently).

Hm, we have no place to decrement.

Decrement when setting nmi_injected = false, increment when setting nmi_injected = true, in vmx/svm.c.

Quoted 3 lines

We need to do that when IRET
executes, but we don't want to request an NMI window exit in the common case of nmi_pending = 1.

Do not enable nmi window if nmi_injected = true?

author
#11 lore raw
Quoted 38 lines · Marcelo Tosatti
Quoted 37 lines · Avi Kivity
Quoted 36 lines · Marcelo Tosatti
Quoted 34 lines · Avi Kivity
Quoted 10 lines · Marcelo Tosatti
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

Yes, due to NMI-blocked-by-STI. A really touchy area.

And we don't need the window exit notification then? I don't understand what nmi_in_progress is supposed to do here.

We need the window notification in both cases. If we're recovering from STI, then we don't need to collapse NMIs. If we're completing an NMI handler, then we do need to collapse NMIs (since the queue length is two, and we just completed one).

Quoted 7 lines

I don't understand what is the point with nmi_in_progress, and the above hunk, either. Can't inject_nmi do:

if (nmi_injected + atomic_read(nmi_pending)< 2)
atomic_inc(nmi_pending)

Instead of collapsing somewhere else?

We could. It's not atomic though - two threads executing in parallel could raise the value to three. Could do a cmpxchg loop does an increment bounded to two. I guess this is a lot clearer, thanks.

Quoted 3 lines

You'd also have to change
nmi_injected handling in arch code so its value is not "hidden", in complete_interrupts().

Or maybe make raising nmi_injected not decrement nmi_pending. So:

nmi_pending: total number of interrupts in queue
nmi_injected: of these, how many are currently being injected

yes?

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Yes, at the expense of decrementing on subarch code (which is fine, apparently).

Hm, we have no place to decrement.

Decrement when setting nmi_injected = false, increment when setting nmi_injected = true, in vmx/svm.c.

That gives a queue length of 3: one running nmi and nmi_pending = 2.

Quoted 6 lines
Quoted 3 lines

We need to do that when IRET
executes, but we don't want to request an NMI window exit in the common case of nmi_pending = 1.

Do not enable nmi window if nmi_injected = true?

We have to, since we need a back-to-back nmi if the queue length > 1 (including the running nmi).

Signature · 1 line
error compiling committee.c: too many arguments to function
#12 lore raw
Quoted 43 lines · Avi Kivity
Quoted 42 lines · Marcelo Tosatti
Quoted 41 lines · Avi Kivity
Quoted 39 lines · Marcelo Tosatti
Quoted 28 lines · Avi Kivity
Quoted 10 lines · Marcelo Tosatti
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

Yes, due to NMI-blocked-by-STI. A really touchy area.

And we don't need the window exit notification then? I don't understand what nmi_in_progress is supposed to do here.

We need the window notification in both cases. If we're recovering from STI, then we don't need to collapse NMIs. If we're completing an NMI handler, then we do need to collapse NMIs (since the queue length is two, and we just completed one).

Quoted 7 lines

I don't understand what is the point with nmi_in_progress, and the above hunk, either. Can't inject_nmi do:

if (nmi_injected + atomic_read(nmi_pending)< 2)
atomic_inc(nmi_pending)

Instead of collapsing somewhere else?

We could. It's not atomic though - two threads executing in parallel could raise the value to three. Could do a cmpxchg loop does an increment bounded to two. I guess this is a lot clearer, thanks.

Quoted 3 lines

You'd also have to change
nmi_injected handling in arch code so its value is not "hidden", in complete_interrupts().

Quoted 6 lines

Or maybe make raising nmi_injected not decrement nmi_pending. So:

nmi_pending: total number of interrupts in queue
nmi_injected: of these, how many are currently being injected

yes?

Yes, at the expense of decrementing on subarch code (which is fine, apparently).

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Hm, we have no place to decrement.

Decrement when setting nmi_injected = false, increment when setting nmi_injected = true, in vmx/svm.c.

That gives a queue length of 3: one running nmi and nmi_pending = 2.

Increment through the same wrapper that will collapse the second and next, also used by kvm_inject_nmi.

Quoted 12 lines
Quoted 6 lines
Quoted 3 lines

We need to do that when IRET
executes, but we don't want to request an NMI window exit in the common case of nmi_pending = 1.

Do not enable nmi window if nmi_injected = true?

We have to, since we need a back-to-back nmi if the queue length > 1 (including the running nmi).

Signature · 1 line
error compiling committee.c: too many arguments to function
author
#13 lore raw
Quoted 9 lines · Marcelo Tosatti
Quoted 4 lines
Quoted 2 lines

Decrement when setting nmi_injected = false, increment when setting nmi_injected = true, in vmx/svm.c.

That gives a queue length of 3: one running nmi and nmi_pending = 2.

Increment through the same wrapper that will collapse the second and next, also used by kvm_inject_nmi.

Cannot be done outside vcpu context.

Signature · 1 line
error compiling committee.c: too many arguments to function