Skip to contentSkip to conversation

Make kvm_cpu_(has|get)_interrupt() work for userspace irqchip too.

PATCH In review Gleb Natapov 3 participants 10 messages Apr 7, 2009 – Apr 11, 2009

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 Make kvm_cpu_(has|get)_interrupt() work for userspace irqchip too.

---

 arch/x86/kvm/irq.c |   39 +++++++++++++++++++++++----------------
 arch/x86/kvm/svm.c |   11 +++++++----
 arch/x86/kvm/vmx.c |   18 +++++++++---------
 arch/x86/kvm/x86.c |    4 ++--
 4 files changed, 41 insertions(+), 31 deletions(-)
arch/x86/kvm/irq.c +23 -16
index cf17ed5..6974e7c 100644
@@ -24,6 +24,7 @@
 
 #include "irq.h"
 #include "i8254.h"
+#include "x86.h"
 
 /*
  * check if there are pending timer events
@@ -48,14 +49,17 @@ int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
 {
 	struct kvm_pic *s;
 
-	if (kvm_apic_has_interrupt(v) == -1) {	/* LAPIC */
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);	/* PIC */
-			return s->output;
-		} else
-			return 0;
+	if (irqchip_in_kernel(v->kvm)) {
+		if (kvm_apic_has_interrupt(v) == -1) {	/* LAPIC */
+			if (kvm_apic_accept_pic_intr(v)) {
+				s = pic_irqchip(v->kvm);	/* PIC */
+				return s->output;
+			} else
+				return 0;
+		}
+		return 1;
 	}
-	return 1;
+	return v->arch.irq_summary;
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_has_interrupt);
 
@@ -64,18 +68,21 @@ EXPORT_SYMBOL_GPL(kvm_cpu_has_interrupt);
  */
 int kvm_cpu_get_interrupt(struct kvm_vcpu *v)
 {
-	struct kvm_pic *s;
-	int vector;
+	if (irqchip_in_kernel(v->kvm)) {
+		struct kvm_pic *s;
+		int vector;
 
-	vector = kvm_get_apic_interrupt(v);	/* APIC */
-	if (vector == -1) {
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);
-			s->output = 0;		/* PIC */
-			vector = kvm_pic_read_irq(v->kvm);
+		vector = kvm_get_apic_interrupt(v);	/* APIC */
+		if (vector == -1) {
+			if (kvm_apic_accept_pic_intr(v)) {
+				s = pic_irqchip(v->kvm);
+				s->output = 0;		/* PIC */
+				vector = kvm_pic_read_irq(v->kvm);
+			}
 		}
+		return vector;
 	}
-	return vector;
+	return kvm_pop_irq(v);
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_get_interrupt);
 
arch/x86/kvm/svm.c +7 -4
index 053f3c5..1903c27 100644
@@ -2089,8 +2089,9 @@ static int interrupt_window_interception(struct vcpu_svm *svm,
 	 * If the user space waits to inject interrupts, exit as soon as
 	 * possible
 	 */
-	if (kvm_run->request_interrupt_window &&
-	    !svm->vcpu.arch.irq_summary) {
+	if (!irqchip_in_kernel(svm->vcpu.kvm) &&
+	    kvm_run->request_interrupt_window &&
+	    !kvm_cpu_has_interrupt(&svm->vcpu)) {
 		++svm->vcpu.stat.irq_window_exits;
 		kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
 		return 0;
@@ -2371,7 +2372,8 @@ static void do_interrupt_requests(struct kvm_vcpu *vcpu,
 		 (svm->vmcb->save.rflags & X86_EFLAGS_IF) &&
 		 (svm->vcpu.arch.hflags & HF_GIF_MASK));
 
-	if (svm->vcpu.arch.interrupt_window_open && svm->vcpu.arch.irq_summary)
+	if (svm->vcpu.arch.interrupt_window_open &&
+	    kvm_cpu_has_interrupt(&svm->vcpu))
 		/*
 		 * If interrupts enabled, and not blocked by sti or mov ss. Good.
 		 */
@@ -2381,7 +2383,8 @@ static void do_interrupt_requests(struct kvm_vcpu *vcpu,
 	 * Interrupts blocked.  Wait for unblock.
 	 */
 	if (!svm->vcpu.arch.interrupt_window_open &&
-	    (svm->vcpu.arch.irq_summary || kvm_run->request_interrupt_window))
+	    (kvm_cpu_has_interrupt(&svm->vcpu) ||
+	     kvm_run->request_interrupt_window))
 		svm_set_vintr(svm);
 	else
 		svm_clear_vintr(svm);
arch/x86/kvm/vmx.c +9 -9
index c6997c0..b3292c1 100644
@@ -2535,21 +2535,20 @@ static void do_interrupt_requests(struct kvm_vcpu *vcpu,
 		vmx_inject_nmi(vcpu);
 		if (vcpu->arch.nmi_pending)
 			enable_nmi_window(vcpu);
-		else if (vcpu->arch.irq_summary
-			 || kvm_run->request_interrupt_window)
+		else if (kvm_cpu_has_interrupt(vcpu) ||
+			 kvm_run->request_interrupt_window)
 			enable_irq_window(vcpu);
 		return;
 	}
 
 	if (vcpu->arch.interrupt_window_open) {
-		if (vcpu->arch.irq_summary && !vcpu->arch.interrupt.pending)
-			kvm_queue_interrupt(vcpu, kvm_pop_irq(vcpu));
+		if (kvm_cpu_has_interrupt(vcpu) && !vcpu->arch.interrupt.pending)
+			kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu));
 
 		if (vcpu->arch.interrupt.pending)
 			vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
-	}
-	if (!vcpu->arch.interrupt_window_open &&
-	    (vcpu->arch.irq_summary || kvm_run->request_interrupt_window))
+	} else if(kvm_cpu_has_interrupt(vcpu) ||
+		  kvm_run->request_interrupt_window)
 		enable_irq_window(vcpu);
 }
 
@@ -2976,8 +2975,9 @@ static int handle_interrupt_window(struct kvm_vcpu *vcpu,
 	 * If the user space waits to inject interrupts, exit as soon as
 	 * possible
 	 */
-	if (kvm_run->request_interrupt_window &&
-	    !vcpu->arch.irq_summary) {
+	if (!irqchip_in_kernel(vcpu->kvm) &&
+	    kvm_run->request_interrupt_window &&
+	    !kvm_cpu_has_interrupt(vcpu)) {
 		kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
 		return 0;
 	}
arch/x86/kvm/x86.c +2 -2
index 52c7a29..6e30cef 100644
@@ -3064,7 +3064,7 @@ EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
 static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
 					  struct kvm_run *kvm_run)
 {
-	return (!vcpu->arch.irq_summary &&
+	return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
 		kvm_run->request_interrupt_window &&
 		vcpu->arch.interrupt_window_open &&
 		(kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
@@ -3081,7 +3081,7 @@ static void post_kvm_run_save(struct kvm_vcpu *vcpu,
 	else
 		kvm_run->ready_for_interrupt_injection =
 					(vcpu->arch.interrupt_window_open &&
-					 vcpu->arch.irq_summary == 0);
+					 !kvm_cpu_has_interrupt(vcpu));
 }
 
 static void vapic_enter(struct kvm_vcpu *vcpu)
#1 lore raw

PATCH 2/3 Consolidate userspace and kernel interrupt injection for VMX.

Use the same callback to inject irq/nmi events no matter what irqchip is in use. Only from VMX for now.

---

 arch/x86/include/asm/kvm_host.h |    2 +
 arch/x86/kvm/svm.c              |    2 +
 arch/x86/kvm/vmx.c              |   71 +++++++++------------------------------
 arch/x86/kvm/x86.c              |    2 +
 4 files changed, 19 insertions(+), 58 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
index e672ca5..4e39c40 100644
@@ -520,7 +520,7 @@ struct kvm_x86_ops {
 	void (*queue_exception)(struct kvm_vcpu *vcpu, unsigned nr,
 				bool has_error_code, u32 error_code);
 	bool (*exception_injected)(struct kvm_vcpu *vcpu);
-	void (*inject_pending_irq)(struct kvm_vcpu *vcpu);
+	void (*inject_pending_irq)(struct kvm_vcpu *vcpu, struct kvm_run *run);
 	void (*inject_pending_vectors)(struct kvm_vcpu *vcpu,
 				       struct kvm_run *run);
 	int (*interrupt_allowed)(struct kvm_vcpu *vcpu);
arch/x86/kvm/svm.c +1 -1
index 1903c27..674a249 100644
@@ -2296,7 +2296,7 @@ static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
 		(svm->vcpu.arch.hflags & HF_GIF_MASK);
 }
 
-static void svm_intr_assist(struct kvm_vcpu *vcpu)
+static void svm_intr_assist(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
 	struct vmcb *vmcb = svm->vmcb;
arch/x86/kvm/vmx.c +16 -55

104 lines not on this page · raw message

arch/x86/kvm/x86.c +1 -1
index 6e30cef..1c19215 100644
@@ -3168,7 +3168,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 	if (vcpu->arch.exception.pending)
 		__queue_exception(vcpu);
 	else if (irqchip_in_kernel(vcpu->kvm))
-		kvm_x86_ops->inject_pending_irq(vcpu);
+		kvm_x86_ops->inject_pending_irq(vcpu, kvm_run);
 	else
 		kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
 
#2 lore raw

Re: [PATCH 2/3] Consolidate userspace and kernel interrupt injection for VMX.

Quoted 17 lines · Gleb Natapov

Use the same callback to inject irq/nmi events no matter what irqchip is in use. Only from VMX for now.

---

 arch/x86/include/asm/kvm_host.h |    2 +
 arch/x86/kvm/svm.c              |    2 +
 arch/x86/kvm/vmx.c              |   71 +++++++++------------------------------
 arch/x86/kvm/x86.c              |    2 +
 4 files changed, 19 insertions(+), 58 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -0
index e672ca5..4e39c40 100644

arch/x86/include/asm/kvm_host.h struct kvm_x86_ops { · line 524

Quoted 6 lines
arch/x86/include/asm/kvm_host.h +1 -1
@@ -520,7 +520,7 @@ struct kvm_x86_ops {
 	void (*queue_exception)(struct kvm_vcpu *vcpu, unsigned nr,
 				bool has_error_code, u32 error_code);
 	bool (*exception_injected)(struct kvm_vcpu *vcpu);
-	void (*inject_pending_irq)(struct kvm_vcpu *vcpu);
+	void (*inject_pending_irq)(struct kvm_vcpu *vcpu, struct kvm_run *run);
 

kvm_run is available as vcpu->run, so this isn't needed. But better to keep it for now and drop it in a later patch.

Quoted 17 lines
arch/x86/include/asm/kvm_host.h +0 -0
 static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
 {
arch/x86/include/asm/kvm_host.h +4 -1
 	int ret;
@@ -3351,8 +3309,11 @@ static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
 	}
 }
 
-static void vmx_intr_assist(struct kvm_vcpu *vcpu)
+static void vmx_intr_assist(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 {
+	bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
+		kvm_run->request_interrupt_window;
+
 	update_tpr_threshold(vcpu);
 
 	vmx_update_window_states(vcpu);

arch/x86/include/asm/kvm_host.h static void vmx_intr_assist(struct kvm_vcpu *vcpu) · line 3337

Quoted 4 lines
arch/x86/include/asm/kvm_host.h +0 -0
@@ -3373,25 +3334,25 @@ static void vmx_intr_assist(struct kvm_vcpu *vcpu)
 			return;
 		}
 	}
 

Why not convert the 'enable_nmi_window(); return;' above to a 'goto out' like you do elsewhere?

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
#3 lore raw

Re: [PATCH 2/3] Consolidate userspace and kernel interrupt injection for VMX.

Quoted 42 lines · Avi Kivity
Quoted 17 lines · Gleb Natapov

Use the same callback to inject irq/nmi events no matter what irqchip is in use. Only from VMX for now.

---

 arch/x86/include/asm/kvm_host.h |    2 +
 arch/x86/kvm/svm.c              |    2 +
 arch/x86/kvm/vmx.c              |   71 +++++++++------------------------------
 arch/x86/kvm/x86.c              |    2 +
 4 files changed, 19 insertions(+), 58 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -0
index e672ca5..4e39c40 100644

arch/x86/include/asm/kvm_host.h struct kvm_x86_ops { · line 524

Quoted 6 lines
arch/x86/include/asm/kvm_host.h +1 -1
@@ -520,7 +520,7 @@ struct kvm_x86_ops {
 	void (*queue_exception)(struct kvm_vcpu *vcpu, unsigned nr,
 				bool has_error_code, u32 error_code);
 	bool (*exception_injected)(struct kvm_vcpu *vcpu);
-	void (*inject_pending_irq)(struct kvm_vcpu *vcpu);
+	void (*inject_pending_irq)(struct kvm_vcpu *vcpu, struct kvm_run *run);
 

kvm_run is available as vcpu->run, so this isn't needed. But better to keep it for now and drop it in a later patch.

Quoted 14 lines
arch/x86/include/asm/kvm_host.h +0 -0
 static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
 {
arch/x86/include/asm/kvm_host.h +4 -0
 	int ret;
@@ -3351,8 +3309,11 @@ static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
 	}
 }
 -static void vmx_intr_assist(struct kvm_vcpu *vcpu)
+static void vmx_intr_assist(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 {
+	bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
+		kvm_run->request_interrupt_window;
+
 	update_tpr_threshold(vcpu);
  	vmx_update_window_states(vcpu);
Quoted 10 lines

arch/x86/include/asm/kvm_host.h static void vmx_intr_assist(struct kvm_vcpu *vcpu) · line 3337

Quoted 5 lines
arch/x86/include/asm/kvm_host.h +0 -0
@@ -3373,25 +3334,25 @@ static void vmx_intr_assist(struct kvm_vcpu *vcpu)
 			return;
 		}
 	}
 

Why not convert the 'enable_nmi_window(); return;' above to a 'goto out' like you do elsewhere?

Mainly because of the next patch in the series that reworks this code anyway.

--
Gleb.

#4 lore raw

PATCH 3/3 Cleanup vmx_intr_assist()

In reply to Gleb Natapov

---

 arch/x86/kvm/vmx.c |   55 ++++++++++++++++++++++++++++------------------------
 1 files changed, 30 insertions(+), 25 deletions(-)
arch/x86/kvm/vmx.c +30 -25
index 06252f7..9eb518f 100644
@@ -3309,6 +3309,34 @@ static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
 	}
 }
 
+static void vmx_intr_inject(struct kvm_vcpu *vcpu)
+{
+	/* try to reinject previous events if any */
+	if (vcpu->arch.nmi_injected) {
+		vmx_inject_nmi(vcpu);
+		return;
+	}
+
+	if (vcpu->arch.interrupt.pending) {
+		vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
+		return;
+	}
+
+	/* try to inject new event if pending */
+	if (vcpu->arch.nmi_pending) {
+		if (vcpu->arch.nmi_window_open) {
+			vcpu->arch.nmi_pending = false;
+			vcpu->arch.nmi_injected = true;
+			vmx_inject_nmi(vcpu);
+		}
+	} else if (kvm_cpu_has_interrupt(vcpu)) {
+		if (vcpu->arch.interrupt_window_open) {
+			kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu));
+			vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
+		}
+	}
+}
+
 static void vmx_intr_assist(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 {
 	bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
@@ -3323,32 +3351,9 @@ static void vmx_intr_assist(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 				GUEST_INTR_STATE_STI |
 				GUEST_INTR_STATE_MOV_SS);
 
-	if (vcpu->arch.nmi_pending && !vcpu->arch.nmi_injected) {
-		if (vcpu->arch.interrupt.pending) {
-			enable_nmi_window(vcpu);
-		} else if (vcpu->arch.nmi_window_open) {
-			vcpu->arch.nmi_pending = false;
-			vcpu->arch.nmi_injected = true;
-		} else {
-			enable_nmi_window(vcpu);
-			return;
-		}
-	}
-
-	if (vcpu->arch.nmi_injected) {
-		vmx_inject_nmi(vcpu);
-		goto out;
-	}
-
-	if (!vcpu->arch.interrupt.pending && kvm_cpu_has_interrupt(vcpu)) {
-		if (vcpu->arch.interrupt_window_open)
-			kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu));
-	}
-
-	if (vcpu->arch.interrupt.pending)
-		vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
+	vmx_intr_inject(vcpu);
 
-out:
+	/* enable NMI/IRQ window open exits if needed */
 	if (vcpu->arch.nmi_pending)
 		enable_nmi_window(vcpu);
 	else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
#5 lore raw

Re: [PATCH 3/3] Cleanup vmx_intr_assist()

Quoted 71 lines · Gleb Natapov

---

 arch/x86/kvm/vmx.c |   55 ++++++++++++++++++++++++++++------------------------
 1 files changed, 30 insertions(+), 25 deletions(-)
arch/x86/kvm/vmx.c +28 -21
index 06252f7..9eb518f 100644
@@ -3309,6 +3309,34 @@ static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
 	}
 }
 
+static void vmx_intr_inject(struct kvm_vcpu *vcpu)
+{
+	/* try to reinject previous events if any */
+	if (vcpu->arch.nmi_injected) {
+		vmx_inject_nmi(vcpu);
+		return;
+	}
+
+	if (vcpu->arch.interrupt.pending) {
+		vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
+		return;
+	}
+
+	/* try to inject new event if pending */
+	if (vcpu->arch.nmi_pending) {
+		if (vcpu->arch.nmi_window_open) {
+			vcpu->arch.nmi_pending = false;
+			vcpu->arch.nmi_injected = true;
+			vmx_inject_nmi(vcpu);
+		}
+	} else if (kvm_cpu_has_interrupt(vcpu)) {
+		if (vcpu->arch.interrupt_window_open) {
+			kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu));
+			vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
+		}
+	}
+}
+
 static void vmx_intr_assist(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 {
 	bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
@@ -3323,32 +3351,9 @@ static void vmx_intr_assist(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 				GUEST_INTR_STATE_STI |
 				GUEST_INTR_STATE_MOV_SS);
 
-	if (vcpu->arch.nmi_pending && !vcpu->arch.nmi_injected) {
-		if (vcpu->arch.interrupt.pending) {
-			enable_nmi_window(vcpu);
-		} else if (vcpu->arch.nmi_window_open) {
-			vcpu->arch.nmi_pending = false;
-			vcpu->arch.nmi_injected = true;
-		} else {
-			enable_nmi_window(vcpu);
-			return;
-		}
-	}
-
-	if (vcpu->arch.nmi_injected) {
-		vmx_inject_nmi(vcpu);
-		goto out;
-	}
-
-	if (!vcpu->arch.interrupt.pending && kvm_cpu_has_interrupt(vcpu)) {
-		if (vcpu->arch.interrupt_window_open)
-			kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu));
-	}

arch/x86/kvm/vmx.c static void vmx_intr_assist(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) · line 3359

Quoted 10 lines
arch/x86/kvm/vmx.c +2 -4
-
-	if (vcpu->arch.interrupt.pending)
-		vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
+	vmx_intr_inject(vcpu);
 
-out:
+	/* enable NMI/IRQ window open exits if needed */
 	if (vcpu->arch.nmi_pending)
 		enable_nmi_window(vcpu);
 	else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)

Not sure I understand the motivation. Just replace a 'goto out' with a return?

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
#6 lore raw

Re: [PATCH 3/3] Cleanup vmx_intr_assist()

Quoted 74 lines · Avi Kivity
Quoted 73 lines · Gleb Natapov

---

 arch/x86/kvm/vmx.c |   55 ++++++++++++++++++++++++++++------------------------
 1 files changed, 30 insertions(+), 25 deletions(-)
arch/x86/kvm/vmx.c +28 -23
index 06252f7..9eb518f 100644
@@ -3309,6 +3309,34 @@ static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
 	}
 }
 +static void vmx_intr_inject(struct kvm_vcpu *vcpu)
+{
+	/* try to reinject previous events if any */
+	if (vcpu->arch.nmi_injected) {
+		vmx_inject_nmi(vcpu);
+		return;
+	}
+
+	if (vcpu->arch.interrupt.pending) {
+		vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
+		return;
+	}
+
+	/* try to inject new event if pending */
+	if (vcpu->arch.nmi_pending) {
+		if (vcpu->arch.nmi_window_open) {
+			vcpu->arch.nmi_pending = false;
+			vcpu->arch.nmi_injected = true;
+			vmx_inject_nmi(vcpu);
+		}
+	} else if (kvm_cpu_has_interrupt(vcpu)) {
+		if (vcpu->arch.interrupt_window_open) {
+			kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu));
+			vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
+		}
+	}
+}
+
 static void vmx_intr_assist(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 {
 	bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
@@ -3323,32 +3351,9 @@ static void vmx_intr_assist(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 				GUEST_INTR_STATE_STI |
 				GUEST_INTR_STATE_MOV_SS);
 -	if (vcpu->arch.nmi_pending && !vcpu->arch.nmi_injected) {
-		if (vcpu->arch.interrupt.pending) {
-			enable_nmi_window(vcpu);
-		} else if (vcpu->arch.nmi_window_open) {
-			vcpu->arch.nmi_pending = false;
-			vcpu->arch.nmi_injected = true;
-		} else {
-			enable_nmi_window(vcpu);
-			return;
-		}
-	}
-
-	if (vcpu->arch.nmi_injected) {
-		vmx_inject_nmi(vcpu);
-		goto out;
-	}
-
-	if (!vcpu->arch.interrupt.pending && kvm_cpu_has_interrupt(vcpu)) {
-		if (vcpu->arch.interrupt_window_open)
-			kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu));
-	}
-
-	if (vcpu->arch.interrupt.pending)
-		vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
+	vmx_intr_inject(vcpu);
 -out:
Quoted 9 lines

arch/x86/kvm/vmx.c static void vmx_intr_assist(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) · line 3359

Quoted 5 lines
arch/x86/kvm/vmx.c +1 -0
+	/* enable NMI/IRQ window open exits if needed */
 	if (vcpu->arch.nmi_pending)
 		enable_nmi_window(vcpu);
 	else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)

Not sure I understand the motivation. Just replace a 'goto out' with a return?

Yes. The code is much cleaner this way. It is easy to see that no matter what happened during injection irq/nmi windows is enabled if there are pending events. With gotos and returns scattered over the function you'll need to check every single path that may or may not lead to the window enable code.

--
Gleb.

#7 lore raw
In reply to Gleb Natapov
Quoted 34 lines · On Tuesday 07 April 2009 17:08:12 Gleb Natapov wrote:

---

 arch/x86/kvm/irq.c |   39 +++++++++++++++++++++++----------------
 arch/x86/kvm/svm.c |   11 +++++++----
 arch/x86/kvm/vmx.c |   18 +++++++++---------
 arch/x86/kvm/x86.c |    4 ++--
 4 files changed, 41 insertions(+), 31 deletions(-)
arch/x86/kvm/irq.c +4 -6
index cf17ed5..6974e7c 100644
@@ -24,6 +24,7 @@
 
 #include "irq.h"
 #include "i8254.h"
+#include "x86.h"
 
 /*
  * check if there are pending timer events
@@ -48,14 +49,17 @@ int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
 {
 	struct kvm_pic *s;
 
-	if (kvm_apic_has_interrupt(v) == -1) {	/* LAPIC */
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);	/* PIC */
-			return s->output;
-		} else
-			return 0;
+	if (irqchip_in_kernel(v->kvm)) {
+		if (kvm_apic_has_interrupt(v) == -1) {	/* LAPIC */
+			if (kvm_apic_accept_pic_intr(v)) {

arch/x86/kvm/irq.c int kvm_cpu_has_interrupt(struct kvm_vcpu *v) · line 63

Quoted 10 lines
arch/x86/kvm/irq.c +7 -1
+				s = pic_irqchip(v->kvm);	/* PIC */
+				return s->output;
+			} else
+				return 0;
+		}
+		return 1;
 	}
-	return 1;
+	return v->arch.irq_summary;
 }

Use if (!irqchip_in_kernel(v->kvm)) for userspace seems more simple(rather than a series of indention...).

Quoted 104 lines
arch/x86/kvm/irq.c +12 -9
 EXPORT_SYMBOL_GPL(kvm_cpu_has_interrupt);
 
@@ -64,18 +68,21 @@ EXPORT_SYMBOL_GPL(kvm_cpu_has_interrupt);
  */
 int kvm_cpu_get_interrupt(struct kvm_vcpu *v)
 {
-	struct kvm_pic *s;
-	int vector;
+	if (irqchip_in_kernel(v->kvm)) {
+		struct kvm_pic *s;
+		int vector;
 
-	vector = kvm_get_apic_interrupt(v);	/* APIC */
-	if (vector == -1) {
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);
-			s->output = 0;		/* PIC */
-			vector = kvm_pic_read_irq(v->kvm);
+		vector = kvm_get_apic_interrupt(v);	/* APIC */
+		if (vector == -1) {
+			if (kvm_apic_accept_pic_intr(v)) {
+				s = pic_irqchip(v->kvm);
+				s->output = 0;		/* PIC */
+				vector = kvm_pic_read_irq(v->kvm);
+			}
 		}
+		return vector;
 	}
-	return vector;
+	return kvm_pop_irq(v);
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_get_interrupt);
 
arch/x86/kvm/svm.c +0 -0
index 053f3c5..1903c27 100644
@@ -2089,8 +2089,9 @@ static int interrupt_window_interception(struct

vcpu_svm *svm, * If the user space waits to inject interrupts, exit as soon as * possible

arch/x86/kvm/svm.c +7 -4
 	 */
-	if (kvm_run->request_interrupt_window &&
-	    !svm->vcpu.arch.irq_summary) {
+	if (!irqchip_in_kernel(svm->vcpu.kvm) &&
+	    kvm_run->request_interrupt_window &&
+	    !kvm_cpu_has_interrupt(&svm->vcpu)) {
 		++svm->vcpu.stat.irq_window_exits;
 		kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
 		return 0;
@@ -2371,7 +2372,8 @@ static void do_interrupt_requests(struct kvm_vcpu
 *vcpu, (svm->vmcb->save.rflags & X86_EFLAGS_IF) &&
 		 (svm->vcpu.arch.hflags & HF_GIF_MASK));
 
-	if (svm->vcpu.arch.interrupt_window_open && svm->vcpu.arch.irq_summary)
+	if (svm->vcpu.arch.interrupt_window_open &&
+	    kvm_cpu_has_interrupt(&svm->vcpu))
 		/*
 		 * If interrupts enabled, and not blocked by sti or mov ss. Good.
 		 */
@@ -2381,7 +2383,8 @@ static void do_interrupt_requests(struct kvm_vcpu
 *vcpu, * Interrupts blocked.  Wait for unblock.
 	 */
 	if (!svm->vcpu.arch.interrupt_window_open &&
-	    (svm->vcpu.arch.irq_summary || kvm_run->request_interrupt_window))
+	    (kvm_cpu_has_interrupt(&svm->vcpu) ||
+	     kvm_run->request_interrupt_window))
 		svm_set_vintr(svm);
 	else
 		svm_clear_vintr(svm);
arch/x86/kvm/vmx.c +6 -7
index c6997c0..b3292c1 100644
@@ -2535,21 +2535,20 @@ static void do_interrupt_requests(struct kvm_vcpu
 *vcpu, vmx_inject_nmi(vcpu);
 		if (vcpu->arch.nmi_pending)
 			enable_nmi_window(vcpu);
-		else if (vcpu->arch.irq_summary
-			 || kvm_run->request_interrupt_window)
+		else if (kvm_cpu_has_interrupt(vcpu) ||
+			 kvm_run->request_interrupt_window)
 			enable_irq_window(vcpu);
 		return;
 	}
 
 	if (vcpu->arch.interrupt_window_open) {
-		if (vcpu->arch.irq_summary && !vcpu->arch.interrupt.pending)
-			kvm_queue_interrupt(vcpu, kvm_pop_irq(vcpu));
+		if (kvm_cpu_has_interrupt(vcpu) && !vcpu->arch.interrupt.pending)
+			kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu));
 
 		if (vcpu->arch.interrupt.pending)
 			vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
-	}
-	if (!vcpu->arch.interrupt_window_open &&
-	    (vcpu->arch.irq_summary || kvm_run->request_interrupt_window))
+	} else if(kvm_cpu_has_interrupt(vcpu) ||
+		  kvm_run->request_interrupt_window)
 		enable_irq_window(vcpu);
 }
 
@@ -2976,8 +2975,9 @@ static int handle_interrupt_window(struct kvm_vcpu
 *vcpu, * If the user space waits to inject interrupts, exit as soon as *

possible

arch/x86/kvm/vmx.c static int handle_interrupt_window(struct kvm_vcpu *vcpu, · line 2979

Quoted 5 lines
arch/x86/kvm/vmx.c +2 -2
 	 */
-	if (kvm_run->request_interrupt_window &&
-	    !vcpu->arch.irq_summary) {
+	if (!irqchip_in_kernel(vcpu->kvm) &&
+	    kvm_run->request_interrupt_window &&

I think kvm_run->request_interrupt_window can indicate it's userspace irqchip?

Quoted 9 lines
arch/x86/kvm/vmx.c +1 -0
+	    !kvm_cpu_has_interrupt(vcpu)) {
 		kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
 		return 0;
 	}
arch/x86/kvm/x86.c +0 -0
index 52c7a29..6e30cef 100644

arch/x86/kvm/x86.c EXPORT_SYMBOL_GPL(kvm_emulate_cpuid); · line 3068

Quoted 6 lines
arch/x86/kvm/x86.c +1 -1
@@ -3064,7 +3064,7 @@ EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
 static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
 					  struct kvm_run *kvm_run)
 {
-	return (!vcpu->arch.irq_summary &&
+	return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
 		kvm_run->request_interrupt_window &&

ditto.

Signature · 2 lines
regards
Yang, Sheng
Quoted 16 lines
arch/x86/kvm/x86.c +1 -2
 		vcpu->arch.interrupt_window_open &&
 		(kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
@@ -3081,7 +3081,7 @@ static void post_kvm_run_save(struct kvm_vcpu *vcpu,
 	else
 		kvm_run->ready_for_interrupt_injection =
 					(vcpu->arch.interrupt_window_open &&
-					 vcpu->arch.irq_summary == 0);
+					 !kvm_cpu_has_interrupt(vcpu));
 }
 
 static void vapic_enter(struct kvm_vcpu *vcpu)
 
--

To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#8 lore raw
Quoted 19 lines
Quoted 19 lines

* check if there are pending timer events

arch/x86/kvm/irq.c +8 -6
@@ -48,14 +49,17 @@ int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
 {
 	struct kvm_pic *s;
 
-	if (kvm_apic_has_interrupt(v) == -1) {	/* LAPIC */
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);	/* PIC */
-			return s->output;
-		} else
-			return 0;
+	if (irqchip_in_kernel(v->kvm)) {
+		if (kvm_apic_has_interrupt(v) == -1) {	/* LAPIC */
+			if (kvm_apic_accept_pic_intr(v)) {
+				s = pic_irqchip(v->kvm);	/* PIC */
+				return s->output;
+			} else
+				return 0;
+		}
Quoted 9 lines

arch/x86/kvm/irq.c int kvm_cpu_has_interrupt(struct kvm_vcpu *v) · line 63

Quoted 5 lines
arch/x86/kvm/irq.c +2 -1
+		return 1;
 	}
-	return 1;
+	return v->arch.irq_summary;
 }

Use if (!irqchip_in_kernel(v->kvm)) for userspace seems more simple(rather than a series of indention...).

As long as lines are smaller then 80 chars I don't care much :) If new version of the patch will be needed I'll change this.

Quoted 12 lines

arch/x86/kvm/vmx.c static int handle_interrupt_window(struct kvm_vcpu *vcpu, · line 2979

Quoted 9 lines
(quoted diff) +0 -0
@@ -2976,8 +2975,9 @@ static int handle_interrupt_window(struct kvm_vcpu
 *vcpu, * If the user space waits to inject interrupts, exit as soon as *

possible

arch/x86/kvm/vmx.c +2 -2
 	 */
-	if (kvm_run->request_interrupt_window &&
-	    !vcpu->arch.irq_summary) {
+	if (!irqchip_in_kernel(vcpu->kvm) &&
+	    kvm_run->request_interrupt_window &&

I think kvm_run->request_interrupt_window can indicate it's userspace irqchip?

You are correct, but this value is directly controlled by userspase, so I added irqchip_in_kernel() check to protect from buggy userspace.

--
Gleb.

#9 lore raw
Quoted 28 lines · Gleb Natapov
Quoted 18 lines
Quoted 18 lines

* check if there are pending timer events

arch/x86/kvm/irq.c +7 -6
@@ -48,14 +49,17 @@ int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
 {
 	struct kvm_pic *s;
 
-	if (kvm_apic_has_interrupt(v) == -1) {	/* LAPIC */
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);	/* PIC */
-			return s->output;
-		} else
-			return 0;
+	if (irqchip_in_kernel(v->kvm)) {
+		if (kvm_apic_has_interrupt(v) == -1) {	/* LAPIC */
+			if (kvm_apic_accept_pic_intr(v)) {
+				s = pic_irqchip(v->kvm);	/* PIC */
+				return s->output;
+			} else
+				return 0;
Quoted 10 lines

arch/x86/kvm/irq.c int kvm_cpu_has_interrupt(struct kvm_vcpu *v) · line 65

Quoted 7 lines
arch/x86/kvm/irq.c +3 -1
+		}
+		return 1;
 	}
-	return 1;
+	return v->arch.irq_summary;
 }
 

Use if (!irqchip_in_kernel(v->kvm)) for userspace seems more simple(rather than a series of indention...).

Quoted 3 lines

As long as lines are smaller then 80 chars I don't care much :) If new version of the patch will be needed I'll change this.

Please change it, the less indentation levels the better.

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.