Skip to contentSkip to conversation

Shared guest irq support

PATCH In review Amit Shah 3 participants 6 messages Oct 13, 2008 – Oct 14, 2008 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

Amit Shah

author
#1 lore raw

PATCH v2 1/1 Shared guest irq support

In reply to a message not in the archive

Sheng, can you check if this is fine? I'll need some time to test this.

Amit

Quoted 1 line

From 920dced6c7ef35959924321ab8d8a9f036fee59c Mon Sep 17 00:00:00 2001

From: Sheng Yang <[email protected]>
Date: Mon, 13 Oct 2008 12:21:35 +0530
Subject: [PATCH 1/1] KVM: Fix guest shared interrupt with in-kernel irqchip

Every call of kvm_set_irq() should offer an irq_source_id, which is allocated by kvm_request_irq_source_id(). Based on irq_source_id, we identify the irq source and implement logical OR for shared level interrupts.

The allocated irq_source_id can be freed by kvm_free_irq_source_id().

Currently, we support at most sizeof(unsigned long) different irq sources.

[Amit: - rebase to kvm.git HEAD
- move kvm_request_irq_source_id to the update_irq ioctl]

---
 arch/x86/kvm/i8254.c       |   11 +++++++++--
 arch/x86/kvm/i8254.h       |    1 +
 arch/x86/kvm/irq.h         |    2 ++
 arch/x86/kvm/x86.c         |    6 +++++-
 include/asm-x86/kvm_host.h |    3 +++
 include/linux/kvm_host.h   |    5 ++++-
 virt/kvm/irq_comm.c        |   42 +++++++++++++++++++++++++++++++++++++++---
 virt/kvm/kvm_main.c        |   12 ++++++++----
 8 files changed, 71 insertions(+), 11 deletions(-)
arch/x86/kvm/i8254.c +9 -2
index 6144d3f..da06919 100644
@@ -545,6 +545,12 @@ struct kvm_pit *kvm_create_pit(struct kvm *kvm)
 	if (!pit)
 		return NULL;
 
+	mutex_lock(&kvm->lock);
+	pit->irq_source_id = kvm_request_irq_source_id(kvm);
+	mutex_unlock(&kvm->lock);
+	if (pit->irq_source_id < 0)
+		return NULL;
+
 	mutex_init(&pit->pit_state.lock);
 	mutex_lock(&pit->pit_state.lock);
 	spin_lock_init(&pit->pit_state.inject_lock);
@@ -587,6 +593,7 @@ void kvm_free_pit(struct kvm *kvm)
 		mutex_lock(&kvm->arch.vpit->pit_state.lock);
 		timer = &kvm->arch.vpit->pit_state.pit_timer.timer;
 		hrtimer_cancel(timer);
+		kvm_free_irq_source_id(kvm, kvm->arch.vpit->irq_source_id);
 		mutex_unlock(&kvm->arch.vpit->pit_state.lock);
 		kfree(kvm->arch.vpit);
 	}
@@ -598,8 +605,8 @@ static void __inject_pit_timer_intr(struct kvm *kvm)
 	int i;
 
 	mutex_lock(&kvm->lock);
-	kvm_set_irq(kvm, 0, 1);
-	kvm_set_irq(kvm, 0, 0);
+	kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 1);
+	kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 0);
 	mutex_unlock(&kvm->lock);
 
 	/*
arch/x86/kvm/i8254.h +1 -0
index e436d49..4178022 100644
@@ -44,6 +44,7 @@ struct kvm_pit {
 	struct kvm_io_device speaker_dev;
 	struct kvm *kvm;
 	struct kvm_kpit_state pit_state;
+	int irq_source_id;
 };
 
 #define KVM_PIT_BASE_ADDRESS	    0x40
arch/x86/kvm/irq.h +2 -0
index 71e37a5..6f0af16 100644
@@ -32,6 +32,8 @@
 
 #define PIC_NUM_PINS 16
 
+#define KVM_USERSPACE_IRQ_SOURCE_ID	0
+
 struct kvm;
 struct kvm_vcpu;
 
arch/x86/kvm/x86.c +5 -1
index dda478e..5d29c50 100644
@@ -1816,7 +1816,8 @@ long kvm_arch_vm_ioctl(struct file *filp,
 			goto out;
 		if (irqchip_in_kernel(kvm)) {
 			mutex_lock(&kvm->lock);
-			kvm_set_irq(kvm, irq_event.irq, irq_event.level);
+			kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
+				    irq_event.irq, irq_event.level);
 			mutex_unlock(&kvm->lock);
 			r = 0;
 		}
@@ -4115,6 +4116,9 @@ struct  kvm *kvm_arch_create_vm(void)
 	INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
 	INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
 
+	/* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
+	kvm->arch.irq_sources_bitmap = 1;
+
 	return kvm;
 }
 
include/asm-x86/kvm_host.h +3 -0
index 4b06ca8..f01e92e 100644
@@ -368,6 +368,9 @@ struct kvm_arch{
 
 	struct page *ept_identity_pagetable;
 	bool ept_identity_pagetable_done;
+
+	unsigned long irq_sources_bitmap;
+	unsigned long irq_states[KVM_IOAPIC_NUM_PINS];
 };
 
 struct kvm_vm_stat {
include/linux/kvm_host.h +4 -1
index 3833c48..30f575d 100644
@@ -306,15 +306,18 @@ struct kvm_assigned_dev_kernel {
 	int host_irq;
 	int guest_irq;
 	int irq_requested;
+	int irq_source_id;
 	struct pci_dev *dev;
 	struct kvm *kvm;
 };
-void kvm_set_irq(struct kvm *kvm, int irq, int level);
+void kvm_set_irq(struct kvm *kvm, int irq_source_id, int irq, int level);
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned gsi);
 void kvm_register_irq_ack_notifier(struct kvm *kvm,
 				   struct kvm_irq_ack_notifier *kian);
 void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
 				     struct kvm_irq_ack_notifier *kian);
+int kvm_request_irq_source_id(struct kvm *kvm);
+void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id);
 
 #ifdef CONFIG_DMAR
 int kvm_iommu_map_pages(struct kvm *kvm, gfn_t base_gfn,
virt/kvm/irq_comm.c +39 -3

57 lines not on this page · raw message

virt/kvm/kvm_main.c +8 -4
index cf0ab8e..a87f45e 100644
@@ -105,14 +105,12 @@ static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
 	 */
 	mutex_lock(&assigned_dev->kvm->lock);
 	kvm_set_irq(assigned_dev->kvm,
+		    assigned_dev->irq_source_id,
 		    assigned_dev->guest_irq, 1);
 	mutex_unlock(&assigned_dev->kvm->lock);
 	kvm_put_kvm(assigned_dev->kvm);
 }
 
-/* FIXME: Implement the OR logic needed to make shared interrupts on
- * this line behave properly
- */
 static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
 {
 	struct kvm_assigned_dev_kernel *assigned_dev =
@@ -134,7 +132,7 @@ static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
 
 	dev = container_of(kian, struct kvm_assigned_dev_kernel,
 			   ack_notifier);
-	kvm_set_irq(dev->kvm, dev->guest_irq, 0);
+	kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
 	enable_irq(dev->host_irq);
 }
 
@@ -146,6 +144,7 @@ static void kvm_free_assigned_device(struct kvm *kvm,
 		free_irq(assigned_dev->host_irq, (void *)assigned_dev);
 
 	kvm_unregister_irq_ack_notifier(kvm, &assigned_dev->ack_notifier);
+	kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
 
 	if (cancel_work_sync(&assigned_dev->interrupt_work))
 		/* We had pending work. That means we will have to take
@@ -215,6 +214,11 @@ static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
 		match->ack_notifier.gsi = assigned_irq->guest_irq;
 		match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
 		kvm_register_irq_ack_notifier(kvm, &match->ack_notifier);
+		r = kvm_request_irq_source_id(kvm);
+		if (r < 0)
+			goto out_release;
+		else
+			match->irq_source_id = r;
 
 		/* Even though this is PCI, we don't want to use shared
 		 * interrupts. Sharing host devices with guest-assigned devices
Signature · 1 line
1.5.5.1
#2 lore raw

Hi, Amit
If your test goes well, I will check it for ia64 side. Thanks!
Xiantao

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Amit Shah
Sent: Monday, October 13, 2008 3:31 PM
To: Sheng Yang
Cc: avi; kvm; Zhang, Xiantao
Subject: [v2] Shared guest irq support

Sheng, can you check if this is fine? I'll need some time to test this.

Amit

author
#3 lore raw
Quoted 3 lines · ----- "Xiantao Zhang

Hi, Amit
If your test goes well, I will check it for ia64 side. Thanks!
Xiantao

Hello Xiantao,

You can test the build and the working with the previous patch as well (which surely works) -- this patch removes some code that wasn't used.

Amit.

#4 lore raw
Quoted 11 lines · Amit Shah
Quoted 3 lines · ----- "Xiantao Zhang

Hi, Amit
If your test goes well, I will check it for ia64 side. Thanks!
Xiantao

Hello Xiantao,

You can test the build and the working with the previous patch as well (which surely works) -- this patch removes some code that wasn't used.

Okay, thanks, I will verify its status on ia64. :) Xiantao

#5 lore raw
In reply to Amit Shah
Quoted 2 lines · On Monday 13 October 2008 15:31:12 Amit Shah wrote:

Sheng, can you check if this is fine? I'll need some time to test this.

I think it's fine.

Thanks!

--
regards
Yang, Sheng

author
#6 lore raw
In reply to Amit Shah
Quoted 1 line · * On Monday 13 Oct 2008 13:01:12 Amit Shah wrote:

Sheng, can you check if this is fine? I'll need some time to test this.

I tested this; it works fine. Avi, please merge.

Amit.