Skip to contentSkip to conversation

PCI passthrough with VT-d - native performance

PATCH In review Ben-Ami Yassour 6 participants 41 messages Jul 16, 2008 – Jul 24, 2008

Subsystems (6)
x86 KVM PCI KVM x86 +2 more subsystems
lore
Share & tools
lore
#0 lore raw

In last few tests that we made with PCI-passthrough and VT-d using iperf, we were able to get the same throughput as on native OS with a 1G NIC (with higher CPU utilization).

The following patches are the PCI-passthrough patches that Amit sent (re-based on the last kvm tree), followed by a few improvements and the VT-d extension.
I am also sending the userspace patches: the patch that Amit sent for PCI passthrough and the direct-mmio extension for userspace (note that without the direct mmio extension we get less then half the throughput).

Comments are welcome.

Regards,
Ben

#5 lore raw

PATCH 5/8 KVM: PCIPT: change order of device release

In reply to Ben-Ami Yassour
---
 arch/x86/kvm/x86.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/x86/kvm/x86.c +1 -1
index 8d25b4a..65b307d 100644
@@ -343,9 +343,9 @@ static void kvm_free_pci_passthrough(struct kvm *kvm)
 		pci_pt_dev = list_entry(ptr, struct kvm_pci_pt_dev_list, list);
 
 		/* Search for this device got us a refcount */
-		pci_dev_put(pci_pt_dev->pt_dev.dev);
 		pci_release_regions(pci_pt_dev->pt_dev.dev);
 		pci_disable_device(pci_pt_dev->pt_dev.dev);
+		pci_dev_put(pci_pt_dev->pt_dev.dev);
 
 		list_del(&pci_pt_dev->list);
 		kfree(pci_pt_dev);
Signature · 1 line
1.5.6
#4 lore raw

PATCH 4/8 KVM: PCIPT: fix interrupt handling

In reply to Ben-Ami Yassour

This patch fixes a few problems with the interrupt handling for passthrough devices.

1. Pass the interrupt handler the pointer to the device, so we do not need to lock the pcipt lock in the interrupt handler.

2. Remove the pt_irq_handled bitmap - it is no longer needed.

3. Split kvm_pci_pt_work_fn into two functions, one for interrupt injection and another for the ack - is much simpler code this way.

4. Change the passthrough initialization order - add the device structure to the list, before registering the interrupt handler.

5. On passthrough destruction path, free the interrupt handler before cleaning queued work.

---
 arch/x86/kvm/x86.c         |  156 ++++++++++++++++++-------------------------
 include/asm-x86/kvm_host.h |    5 +-
 virt/kvm/ioapic.c          |    5 +-
 3 files changed, 69 insertions(+), 97 deletions(-)
arch/x86/kvm/x86.c +65 -91
index c07ca2b..8d25b4a 100644
@@ -145,49 +145,37 @@ kvm_find_pci_pt_dev(struct list_head *head,
 	return NULL;
 }
 
-static DECLARE_BITMAP(pt_irq_handled, NR_IRQS);
-
-static void kvm_pci_pt_work_fn(struct work_struct *work)
+static void kvm_pci_pt_int_work_fn(struct work_struct *work)
 {
-	struct kvm_pci_pt_dev_list *match;
 	struct kvm_pci_pt_work *int_work;
-	int source;
-	unsigned long flags;
-	int guest_irq;
-	int host_irq;
 
 	int_work = container_of(work, struct kvm_pci_pt_work, work);
 
-	source = int_work->source ? KVM_PT_SOURCE_IRQ_ACK : KVM_PT_SOURCE_IRQ;
-
 	/* This is taken to safely inject irq inside the guest. When
 	 * the interrupt injection (or the ioapic code) uses a
 	 * finer-grained lock, update this
 	 */
-	mutex_lock(&int_work->kvm->lock);
-	read_lock_irqsave(&kvm_pci_pt_lock, flags);
-	match = kvm_find_pci_pt_dev(&int_work->kvm->arch.pci_pt_dev_head, NULL,
-				    int_work->irq, source);
-	if (!match) {
-		printk(KERN_ERR "%s: no matching device assigned to guest "
-		       "found for irq %d, source = %d!\n",
-		       __func__, int_work->irq, int_work->source);
-		read_unlock_irqrestore(&kvm_pci_pt_lock, flags);
-		goto out;
-	}
-	guest_irq = match->pt_dev.guest.irq;
-	host_irq = match->pt_dev.host.irq;
-	read_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+	mutex_lock(&int_work->pt_dev->kvm->lock);
+	kvm_set_irq(int_work->pt_dev->kvm, int_work->pt_dev->guest.irq, 1);
+	mutex_unlock(&int_work->pt_dev->kvm->lock);
+	kvm_put_kvm(int_work->pt_dev->kvm);
+}
 
-	if (source == KVM_PT_SOURCE_IRQ)
-		kvm_set_irq(int_work->kvm, guest_irq, 1);
-	else {
-		kvm_set_irq(int_work->kvm, int_work->irq, 0);
-		enable_irq(host_irq);
-	}
-out:
-	mutex_unlock(&int_work->kvm->lock);
-	kvm_put_kvm(int_work->kvm);
+static void kvm_pci_pt_ack_work_fn(struct work_struct *work)
+{
+	struct kvm_pci_pt_work *ack_work;
+
+	ack_work = container_of(work, struct kvm_pci_pt_work, work);
+
+	/* This is taken to safely inject irq inside the guest. When
+	 * the interrupt injection (or the ioapic code) uses a
+	 * finer-grained lock, update this
+	 */
+	mutex_lock(&ack_work->pt_dev->kvm->lock);
+	kvm_set_irq(ack_work->pt_dev->kvm, ack_work->pt_dev->guest.irq, 0);
+	enable_irq(ack_work->pt_dev->host.irq);
+	mutex_unlock(&ack_work->pt_dev->kvm->lock);
+	kvm_put_kvm(ack_work->pt_dev->kvm);
 }
 
 /* FIXME: Implement the OR logic needed to make shared interrupts on
@@ -195,28 +183,11 @@ out:
  */
 static irqreturn_t kvm_pci_pt_dev_intr(int irq, void *dev_id)
 {
-	struct kvm *kvm = (struct kvm *) dev_id;
-	struct kvm_pci_pt_dev_list *pci_pt_dev;
-
-	if (!test_bit(irq, pt_irq_handled))
-		return IRQ_NONE;
-
-	read_lock(&kvm_pci_pt_lock);
-	pci_pt_dev = kvm_find_pci_pt_dev(&kvm->arch.pci_pt_dev_head, NULL,
-					 irq, KVM_PT_SOURCE_IRQ);
-	if (!pci_pt_dev) {
-		read_unlock(&kvm_pci_pt_lock);
-		return IRQ_NONE;
-	}
-
-	pci_pt_dev->pt_dev.int_work.irq = irq;
-	pci_pt_dev->pt_dev.int_work.kvm = kvm;
-	pci_pt_dev->pt_dev.int_work.source = 0;
-
-	kvm_get_kvm(kvm);
-	schedule_work(&pci_pt_dev->pt_dev.int_work.work);
-	read_unlock(&kvm_pci_pt_lock);
+	struct kvm_pci_passthrough_dev_kernel *pt_dev =
+		(struct kvm_pci_passthrough_dev_kernel *) dev_id;
 
+	kvm_get_kvm(pt_dev->kvm);
+	schedule_work(&pt_dev->int_work.work);
 	disable_irq_nosync(irq);
 	return IRQ_HANDLED;
 }
@@ -226,25 +197,20 @@ static void kvm_pci_pt_ack_irq(void *opaque, int irq)
 {
 	struct kvm *kvm = opaque;
 	struct kvm_pci_pt_dev_list *pci_pt_dev;
-	unsigned long flags;
 
 	if (irq == -1)
 		return;
 
-	read_lock_irqsave(&kvm_pci_pt_lock, flags);
+	read_lock(&kvm_pci_pt_lock);
 	pci_pt_dev = kvm_find_pci_pt_dev(&kvm->arch.pci_pt_dev_head, NULL, irq,
 					 KVM_PT_SOURCE_IRQ_ACK);
 	if (!pci_pt_dev) {
-		read_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+		read_unlock(&kvm_pci_pt_lock);
 		return;
 	}
-	pci_pt_dev->pt_dev.ack_work.irq = irq;
-	pci_pt_dev->pt_dev.ack_work.kvm = kvm;
-	pci_pt_dev->pt_dev.ack_work.source = 1;
-
 	kvm_get_kvm(kvm);
+	read_unlock(&kvm_pci_pt_lock);
 	schedule_work(&pci_pt_dev->pt_dev.ack_work.work);
-	read_unlock_irqrestore(&kvm_pci_pt_lock, flags);
 }
 
 static int kvm_vm_ioctl_pci_pt_dev(struct kvm *kvm,
@@ -252,10 +218,9 @@ static int kvm_vm_ioctl_pci_pt_dev(struct kvm *kvm,
 {
 	int r = 0;
 	struct kvm_pci_pt_dev_list *match;
-	unsigned long flags;
 	struct pci_dev *dev;
 
-	write_lock_irqsave(&kvm_pci_pt_lock, flags);
+	write_lock(&kvm_pci_pt_lock);
 
 	/* Check if this is a request to update the irq of the device
 	 * in the guest (BIOS/ kernels can dynamically reprogram irq
@@ -266,10 +231,10 @@ static int kvm_vm_ioctl_pci_pt_dev(struct kvm *kvm,
 				    &pci_pt_dev->host, 0, KVM_PT_SOURCE_UPDATE);
 	if (match) {
 		match->pt_dev.guest.irq = pci_pt_dev->guest.irq;
-		write_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+		write_unlock(&kvm_pci_pt_lock);
 		goto out;
 	}
-	write_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+	write_unlock(&kvm_pci_pt_lock);
 
 	match = kzalloc(sizeof(struct kvm_pci_pt_dev_list), GFP_KERNEL);
 	if (match == NULL) {
@@ -298,42 +263,49 @@ static int kvm_vm_ioctl_pci_pt_dev(struct kvm *kvm,
 	}
 	match->pt_dev.guest.busnr = pci_pt_dev->guest.busnr;
 	match->pt_dev.guest.devfn = pci_pt_dev->guest.devfn;
-	match->pt_dev.host.busnr  = pci_pt_dev->host.busnr;
-	match->pt_dev.host.devfn  = pci_pt_dev->host.devfn;
-	match->pt_dev.dev         = dev;
+	match->pt_dev.host.busnr = pci_pt_dev->host.busnr;
+	match->pt_dev.host.devfn = pci_pt_dev->host.devfn;
+	match->pt_dev.dev = dev;
+
+	write_lock(&kvm_pci_pt_lock);
+
+	INIT_WORK(&match->pt_dev.int_work.work, kvm_pci_pt_int_work_fn);
+	INIT_WORK(&match->pt_dev.ack_work.work, kvm_pci_pt_ack_work_fn);
+
+	match->pt_dev.kvm = kvm;
+	match->pt_dev.int_work.pt_dev = &match->pt_dev;
+	match->pt_dev.ack_work.pt_dev = &match->pt_dev;
+
+	list_add(&match->list, &kvm->arch.pci_pt_dev_head);
+
+	write_unlock(&kvm_pci_pt_lock);
 
 	if (irqchip_in_kernel(kvm)) {
+		match->pt_dev.guest.irq = pci_pt_dev->guest.irq;
+		match->pt_dev.host.irq = dev->irq;
+		if (kvm->arch.vioapic)
+			kvm->arch.vioapic->ack_notifier = kvm_pci_pt_ack_irq;
+		if (kvm->arch.vpic)
+			kvm->arch.vpic->ack_notifier = kvm_pci_pt_ack_irq;
+
 		/* Even though this is PCI, we don't want to use shared
 		 * interrupts. Sharing host devices with guest-assigned devices
 		 * on the same interrupt line is not a happy situation: there
 		 * are going to be long delays in accepting, acking, etc.
 		 */
 		if (request_irq(dev->irq, kvm_pci_pt_dev_intr, 0,
-				"kvm_pt_device", (void *)kvm)) {
+				"kvm_pt_device", (void *)&match->pt_dev)) {
 			printk(KERN_INFO "%s: couldn't allocate irq for pv "
 			       "device\n", __func__);
 			r = -EIO;
-			goto out_put;
+			goto out_list_del;
 		}
-		match->pt_dev.guest.irq = pci_pt_dev->guest.irq;
-		match->pt_dev.host.irq  = dev->irq;
-		if (kvm->arch.vioapic)
-			kvm->arch.vioapic->ack_notifier = kvm_pci_pt_ack_irq;
-		if (kvm->arch.vpic)
-			kvm->arch.vpic->ack_notifier = kvm_pci_pt_ack_irq;
 	}
-	write_lock_irqsave(&kvm_pci_pt_lock, flags);
-
-	INIT_WORK(&match->pt_dev.int_work.work, kvm_pci_pt_work_fn);
-	INIT_WORK(&match->pt_dev.ack_work.work, kvm_pci_pt_work_fn);
 
-	list_add(&match->list, &kvm->arch.pci_pt_dev_head);
-
-	if (irqchip_in_kernel(kvm))
-		set_bit(dev->irq, pt_irq_handled);
-	write_unlock_irqrestore(&kvm_pci_pt_lock, flags);
 out:
 	return r;
+out_list_del:
+	list_del(&match->list);
 out_put:
 	pci_dev_put(dev);
 out_free:
@@ -345,11 +317,15 @@ static void kvm_free_pci_passthrough(struct kvm *kvm)
 {
 	struct list_head *ptr, *ptr2;
 	struct kvm_pci_pt_dev_list *pci_pt_dev;
-	unsigned long flags;
 
-	write_lock_irqsave(&kvm_pci_pt_lock, flags);
+	write_lock(&kvm_pci_pt_lock);
 	list_for_each_safe(ptr, ptr2, &kvm->arch.pci_pt_dev_head) {
 		pci_pt_dev = list_entry(ptr, struct kvm_pci_pt_dev_list, list);
+
+		if (irqchip_in_kernel(kvm) && pci_pt_dev->pt_dev.host.irq)
+			free_irq(pci_pt_dev->pt_dev.host.irq,
+				 (void *)&pci_pt_dev->pt_dev);
+
 		if (cancel_work_sync(&pci_pt_dev->pt_dev.int_work.work))
 			/* We had pending work. That means we will have to take
 			 * care of kvm_put_kvm.
@@ -366,8 +342,6 @@ static void kvm_free_pci_passthrough(struct kvm *kvm)
 	list_for_each_safe(ptr, ptr2, &kvm->arch.pci_pt_dev_head) {
 		pci_pt_dev = list_entry(ptr, struct kvm_pci_pt_dev_list, list);
 
-		if (irqchip_in_kernel(kvm) && pci_pt_dev->pt_dev.host.irq)
-			free_irq(pci_pt_dev->pt_dev.host.irq, kvm);
 		/* Search for this device got us a refcount */
 		pci_dev_put(pci_pt_dev->pt_dev.dev);
 		pci_release_regions(pci_pt_dev->pt_dev.dev);
@@ -376,7 +350,7 @@ static void kvm_free_pci_passthrough(struct kvm *kvm)
 		list_del(&pci_pt_dev->list);
 		kfree(pci_pt_dev);
 	}
-	write_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+	write_unlock(&kvm_pci_pt_lock);
 }
 
 unsigned long segment_base(u16 selector)
include/asm-x86/kvm_host.h +2 -3
index 2346c9f..f6973e0 100644
@@ -331,9 +331,7 @@ struct kvm_mem_alias {
  */
 struct kvm_pci_pt_work {
 	struct work_struct work;
-	struct kvm *kvm;
-	int irq;
-	bool source;
+	struct kvm_pci_passthrough_dev_kernel *pt_dev;
 };
 
 struct kvm_pci_passthrough_dev_kernel {
@@ -342,6 +340,7 @@ struct kvm_pci_passthrough_dev_kernel {
 	struct kvm_pci_pt_work int_work;
 	struct kvm_pci_pt_work ack_work;
 	struct pci_dev *dev;
+	struct kvm *kvm;
 };
 
 /* This list is to store the guest bus:device:function-irq and host
virt/kvm/ioapic.c +2 -3
index 0b5bc40..6ec99fd 100644
@@ -289,17 +289,16 @@ static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int gsi)
 {
 	union ioapic_redir_entry *ent;
 	struct kvm_pci_pt_dev_list *match;
-	unsigned long flags;
 
 	ent = &ioapic->redirtbl[gsi];
 	ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
 
 	ent->fields.remote_irr = 0;
 
-	read_lock_irqsave(&kvm_pci_pt_lock, flags);
+	read_lock(&kvm_pci_pt_lock);
 	match = kvm_find_pci_pt_dev(&ioapic->kvm->arch.pci_pt_dev_head, NULL,
 				    gsi, KVM_PT_SOURCE_IRQ_ACK);
-	read_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+	read_unlock(&kvm_pci_pt_lock);
 	if (!match) {
 		if (!ent->fields.mask && (ioapic->irr & (1 << gsi)))
 			ioapic_service(ioapic, gsi);
Signature · 1 line
1.5.6
#2 lore raw

PATCH 2/8 KVM: Introduce a callback routine for PIC ack handling

In reply to Ben-Ami Yassour

From: Amit Shah <[email protected]>

This is useful for acking irqs of assigned devices

---
 arch/x86/kvm/i8259.c |    6 +++++-
 arch/x86/kvm/irq.c   |    2 +-
 arch/x86/kvm/irq.h   |    3 ++-
 3 files changed, 8 insertions(+), 3 deletions(-)
arch/x86/kvm/i8259.c +5 -1
index 55e179a..c1a4110 100644
@@ -159,9 +159,10 @@ static inline void pic_intack(struct kvm_kpic_state *s, int irq)
 		s->irr &= ~(1 << irq);
 }
 
-int kvm_pic_read_irq(struct kvm_pic *s)
+int kvm_pic_read_irq(struct kvm *kvm)
 {
 	int irq, irq2, intno;
+	struct kvm_pic *s = pic_irqchip(kvm);
 
 	irq = pic_get_irq(&s->pics[0]);
 	if (irq >= 0) {
@@ -186,6 +187,9 @@ int kvm_pic_read_irq(struct kvm_pic *s)
 		irq = 7;
 		intno = s->pics[0].irq_base + irq;
 	}
+	if (kvm->arch.vpic->ack_notifier)
+		kvm->arch.vpic->ack_notifier(kvm, irq);
+
 	pic_update_irq(s);
 
 	return intno;
arch/x86/kvm/irq.c +1 -1
index 0d9e552..3529620 100644
@@ -72,7 +72,7 @@ int kvm_cpu_get_interrupt(struct kvm_vcpu *v)
 		if (kvm_apic_accept_pic_intr(v)) {
 			s = pic_irqchip(v->kvm);
 			s->output = 0;		/* PIC */
-			vector = kvm_pic_read_irq(s);
+			vector = kvm_pic_read_irq(v->kvm);
 		}
 	}
 	return vector;
arch/x86/kvm/irq.h +2 -1
index 07ff2ae..ac71a04 100644
@@ -63,11 +63,12 @@ struct kvm_pic {
 	void *irq_request_opaque;
 	int output;		/* intr from master PIC */
 	struct kvm_io_device dev;
+	void (*ack_notifier)(void *opaque, int irq);
 };
 
 struct kvm_pic *kvm_create_pic(struct kvm *kvm);
 void kvm_pic_set_irq(void *opaque, int irq, int level);
-int kvm_pic_read_irq(struct kvm_pic *s);
+int kvm_pic_read_irq(struct kvm *kvm);
 void kvm_pic_update_irq(struct kvm_pic *s);
 
 static inline struct kvm_pic *pic_irqchip(struct kvm *kvm)
Signature · 1 line
1.5.6
#3 lore raw

PATCH 3/8 KVM: Handle device assignment to guests

From: Han, Weidong <[email protected]>

This patch adds support for handling PCI devices that are assigned to the guest ("PCI passthrough").

The device to be assigned to the guest is registered in the host kernel and interrupt delivery is handled. If a device is already assigned, or the
device driver for it is still loaded on the host, the device assignment is
failed by conveying a -EBUSY reply to the userspace.

Devices that share their interrupt line are not supported at the moment.

By itself, this patch will not make devices work within the guest. There has to be some mechanism of translating guest DMA addresses into machine addresses.
This support comes from one of three approaches:
1. If you have recent Intel hardware with VT-d support, you can use the patches in

git.kernel.org/pub/scm/linux/kernel/git/amit/kvm.git vtd git.kernel.org/pub/scm/linux/kernel/git/amit/kvm-userspace.git vtd

These patches are for the host kernel.
2. For paravirtualised Linux guests, you can use the patches in

git.kernel.org/pub/scm/linux/kernel/git/amit/kvm.git pvdma git.kernel.org/pub/scm/linux/kernel/git/amit/kvm-userspace.git pvdma

This kernel tree has patches for host as well as guest kernels.

3. 1-1 mapping of guest in host address space

The patches to do this are available on the kvm / lkml list archive:
http://thread.gmane.org/gmane.comp.emulators.kvm.devel/18722/focus=18753

---
 arch/x86/kvm/x86.c         |  293 ++++++++++++++++++++++++++++++++++++++++++++
 include/asm-x86/kvm_host.h |   38 ++++++
 include/asm-x86/kvm_para.h |   16 +++-
 include/linux/kvm.h        |    3 +
 virt/kvm/ioapic.c          |   13 ++-
 5 files changed, 360 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +293 -0
index 3167006..c07ca2b 100644
@@ -4,10 +4,12 @@
  * derived from drivers/kvm/kvm_main.c
  *
  * Copyright (C) 2006 Qumranet, Inc.
+ * Copyright (C) 2008 Qumranet, Inc.
  *
  * Authors:
  *   Avi Kivity   <[email protected]>
  *   Yaniv Kamay  <[email protected]>
+ *   Amit Shah    <[email protected]>
  *
  * This work is licensed under the terms of the GNU GPL, version 2.  See
  * the COPYING file in the top-level directory.
@@ -23,8 +25,10 @@
 #include "x86.h"
 
 #include <linux/clocksource.h>
+#include <linux/interrupt.h>
 #include <linux/kvm.h>
 #include <linux/fs.h>
+#include <linux/pci.h>
 #include <linux/vmalloc.h>
 #include <linux/module.h>
 #include <linux/mman.h>
@@ -98,6 +102,282 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {
 	{ NULL }
 };
 
+DEFINE_RWLOCK(kvm_pci_pt_lock);
+
+/*
+ * Used to find a registered host PCI device (a "passthrough" device)
+ * during ioctls, interrupts or EOI
+ */
+struct kvm_pci_pt_dev_list *
+kvm_find_pci_pt_dev(struct list_head *head,
+		struct kvm_pci_pt_info *pt_pci_info, int irq, int source)
+{
+	struct list_head *ptr;
+	struct kvm_pci_pt_dev_list *match;
+
+	list_for_each(ptr, head) {
+		match = list_entry(ptr, struct kvm_pci_pt_dev_list, list);
+
+		switch (source) {
+		case KVM_PT_SOURCE_IRQ:
+			/*
+			 * Used to find a registered host device
+			 * during interrupt context on host
+			 */
+			if (match->pt_dev.host.irq == irq)
+				return match;
+			break;
+		case KVM_PT_SOURCE_IRQ_ACK:
+			/*
+			 * Used to find a registered host device when
+			 * the guest acks an interrupt
+			 */
+			if (match->pt_dev.guest.irq == irq)
+				return match;
+			break;
+		case KVM_PT_SOURCE_UPDATE:
+			if ((match->pt_dev.host.busnr == pt_pci_info->busnr) &&
+			    (match->pt_dev.host.devfn == pt_pci_info->devfn))
+				return match;
+			break;
+		}
+	}
+	return NULL;
+}
+
+static DECLARE_BITMAP(pt_irq_handled, NR_IRQS);
+
+static void kvm_pci_pt_work_fn(struct work_struct *work)
+{
+	struct kvm_pci_pt_dev_list *match;
+	struct kvm_pci_pt_work *int_work;
+	int source;
+	unsigned long flags;
+	int guest_irq;
+	int host_irq;
+
+	int_work = container_of(work, struct kvm_pci_pt_work, work);
+
+	source = int_work->source ? KVM_PT_SOURCE_IRQ_ACK : KVM_PT_SOURCE_IRQ;
+
+	/* This is taken to safely inject irq inside the guest. When
+	 * the interrupt injection (or the ioapic code) uses a
+	 * finer-grained lock, update this
+	 */
+	mutex_lock(&int_work->kvm->lock);
+	read_lock_irqsave(&kvm_pci_pt_lock, flags);
+	match = kvm_find_pci_pt_dev(&int_work->kvm->arch.pci_pt_dev_head, NULL,
+				    int_work->irq, source);
+	if (!match) {
+		printk(KERN_ERR "%s: no matching device assigned to guest "
+		       "found for irq %d, source = %d!\n",
+		       __func__, int_work->irq, int_work->source);
+		read_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+		goto out;
+	}
+	guest_irq = match->pt_dev.guest.irq;
+	host_irq = match->pt_dev.host.irq;
+	read_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+
+	if (source == KVM_PT_SOURCE_IRQ)
+		kvm_set_irq(int_work->kvm, guest_irq, 1);
+	else {
+		kvm_set_irq(int_work->kvm, int_work->irq, 0);
+		enable_irq(host_irq);
+	}
+out:
+	mutex_unlock(&int_work->kvm->lock);
+	kvm_put_kvm(int_work->kvm);
+}
+
+/* FIXME: Implement the OR logic needed to make shared interrupts on
+ * this line behave properly
+ */
+static irqreturn_t kvm_pci_pt_dev_intr(int irq, void *dev_id)
+{
+	struct kvm *kvm = (struct kvm *) dev_id;
+	struct kvm_pci_pt_dev_list *pci_pt_dev;
+
+	if (!test_bit(irq, pt_irq_handled))
+		return IRQ_NONE;
+
+	read_lock(&kvm_pci_pt_lock);
+	pci_pt_dev = kvm_find_pci_pt_dev(&kvm->arch.pci_pt_dev_head, NULL,
+					 irq, KVM_PT_SOURCE_IRQ);
+	if (!pci_pt_dev) {
+		read_unlock(&kvm_pci_pt_lock);
+		return IRQ_NONE;
+	}
+
+	pci_pt_dev->pt_dev.int_work.irq = irq;
+	pci_pt_dev->pt_dev.int_work.kvm = kvm;
+	pci_pt_dev->pt_dev.int_work.source = 0;
+
+	kvm_get_kvm(kvm);
+	schedule_work(&pci_pt_dev->pt_dev.int_work.work);
+	read_unlock(&kvm_pci_pt_lock);
+
+	disable_irq_nosync(irq);
+	return IRQ_HANDLED;
+}
+
+/* Ack the irq line for a passthrough device */
+static void kvm_pci_pt_ack_irq(void *opaque, int irq)
+{
+	struct kvm *kvm = opaque;
+	struct kvm_pci_pt_dev_list *pci_pt_dev;
+	unsigned long flags;
+
+	if (irq == -1)
+		return;
+
+	read_lock_irqsave(&kvm_pci_pt_lock, flags);
+	pci_pt_dev = kvm_find_pci_pt_dev(&kvm->arch.pci_pt_dev_head, NULL, irq,
+					 KVM_PT_SOURCE_IRQ_ACK);
+	if (!pci_pt_dev) {
+		read_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+		return;
+	}
+	pci_pt_dev->pt_dev.ack_work.irq = irq;
+	pci_pt_dev->pt_dev.ack_work.kvm = kvm;
+	pci_pt_dev->pt_dev.ack_work.source = 1;
+
+	kvm_get_kvm(kvm);
+	schedule_work(&pci_pt_dev->pt_dev.ack_work.work);
+	read_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+}
+
+static int kvm_vm_ioctl_pci_pt_dev(struct kvm *kvm,
+				   struct kvm_pci_passthrough_dev *pci_pt_dev)
+{
+	int r = 0;
+	struct kvm_pci_pt_dev_list *match;
+	unsigned long flags;
+	struct pci_dev *dev;
+
+	write_lock_irqsave(&kvm_pci_pt_lock, flags);
+
+	/* Check if this is a request to update the irq of the device
+	 * in the guest (BIOS/ kernels can dynamically reprogram irq
+	 * numbers).  This also protects us from adding the same
+	 * device twice.
+	 */
+	match = kvm_find_pci_pt_dev(&kvm->arch.pci_pt_dev_head,
+				    &pci_pt_dev->host, 0, KVM_PT_SOURCE_UPDATE);
+	if (match) {
+		match->pt_dev.guest.irq = pci_pt_dev->guest.irq;
+		write_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+		goto out;
+	}
+	write_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+
+	match = kzalloc(sizeof(struct kvm_pci_pt_dev_list), GFP_KERNEL);
+	if (match == NULL) {
+		printk(KERN_INFO "%s: Couldn't allocate memory\n",
+		       __func__);
+		r = -ENOMEM;
+		goto out;
+	}
+	dev = pci_get_bus_and_slot(pci_pt_dev->host.busnr,
+				   pci_pt_dev->host.devfn);
+	if (!dev) {
+		printk(KERN_INFO "%s: host device not found\n", __func__);
+		r = -EINVAL;
+		goto out_free;
+	}
+	if (pci_enable_device(dev)) {
+		printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
+		r = -EBUSY;
+		goto out_put;
+	}
+	r = pci_request_regions(dev, "kvm_pt_device");
+	if (r) {
+		printk(KERN_INFO "%s: Could not get access to device regions\n",
+		       __func__);
+		goto out_put;
+	}
+	match->pt_dev.guest.busnr = pci_pt_dev->guest.busnr;
+	match->pt_dev.guest.devfn = pci_pt_dev->guest.devfn;
+	match->pt_dev.host.busnr  = pci_pt_dev->host.busnr;
+	match->pt_dev.host.devfn  = pci_pt_dev->host.devfn;
+	match->pt_dev.dev         = dev;
+
+	if (irqchip_in_kernel(kvm)) {
+		/* Even though this is PCI, we don't want to use shared
+		 * interrupts. Sharing host devices with guest-assigned devices
+		 * on the same interrupt line is not a happy situation: there
+		 * are going to be long delays in accepting, acking, etc.
+		 */
+		if (request_irq(dev->irq, kvm_pci_pt_dev_intr, 0,
+				"kvm_pt_device", (void *)kvm)) {
+			printk(KERN_INFO "%s: couldn't allocate irq for pv "
+			       "device\n", __func__);
+			r = -EIO;
+			goto out_put;
+		}
+		match->pt_dev.guest.irq = pci_pt_dev->guest.irq;
+		match->pt_dev.host.irq  = dev->irq;
+		if (kvm->arch.vioapic)
+			kvm->arch.vioapic->ack_notifier = kvm_pci_pt_ack_irq;
+		if (kvm->arch.vpic)
+			kvm->arch.vpic->ack_notifier = kvm_pci_pt_ack_irq;
+	}
+	write_lock_irqsave(&kvm_pci_pt_lock, flags);
+
+	INIT_WORK(&match->pt_dev.int_work.work, kvm_pci_pt_work_fn);
+	INIT_WORK(&match->pt_dev.ack_work.work, kvm_pci_pt_work_fn);
+
+	list_add(&match->list, &kvm->arch.pci_pt_dev_head);
+
+	if (irqchip_in_kernel(kvm))
+		set_bit(dev->irq, pt_irq_handled);
+	write_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+out:
+	return r;
+out_put:
+	pci_dev_put(dev);
+out_free:
+	kfree(match);
+	goto out;
+}
+
+static void kvm_free_pci_passthrough(struct kvm *kvm)
+{
+	struct list_head *ptr, *ptr2;
+	struct kvm_pci_pt_dev_list *pci_pt_dev;
+	unsigned long flags;
+
+	write_lock_irqsave(&kvm_pci_pt_lock, flags);
+	list_for_each_safe(ptr, ptr2, &kvm->arch.pci_pt_dev_head) {
+		pci_pt_dev = list_entry(ptr, struct kvm_pci_pt_dev_list, list);
+		if (cancel_work_sync(&pci_pt_dev->pt_dev.int_work.work))
+			/* We had pending work. That means we will have to take
+			 * care of kvm_put_kvm.
+			 */
+			kvm_put_kvm(kvm);
+
+		if (cancel_work_sync(&pci_pt_dev->pt_dev.ack_work.work))
+			/* We had pending work. That means we will have to take
+			 * care of kvm_put_kvm.
+			 */
+			kvm_put_kvm(kvm);
+	}
+
+	list_for_each_safe(ptr, ptr2, &kvm->arch.pci_pt_dev_head) {
+		pci_pt_dev = list_entry(ptr, struct kvm_pci_pt_dev_list, list);
+
+		if (irqchip_in_kernel(kvm) && pci_pt_dev->pt_dev.host.irq)
+			free_irq(pci_pt_dev->pt_dev.host.irq, kvm);
+		/* Search for this device got us a refcount */
+		pci_dev_put(pci_pt_dev->pt_dev.dev);
+		pci_release_regions(pci_pt_dev->pt_dev.dev);
+		pci_disable_device(pci_pt_dev->pt_dev.dev);
+
+		list_del(&pci_pt_dev->list);
+		kfree(pci_pt_dev);
+	}
+	write_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+}
 
 unsigned long segment_base(u16 selector)
 {
@@ -1746,6 +2026,17 @@ long kvm_arch_vm_ioctl(struct file *filp,
 		r = 0;
 		break;
 	}
+	case KVM_UPDATE_PCI_PT_DEV: {
+		struct kvm_pci_passthrough_dev pci_pt_dev;
+
+		r = -EFAULT;
+		if (copy_from_user(&pci_pt_dev, argp, sizeof pci_pt_dev))
+			goto out;
+		r = kvm_vm_ioctl_pci_pt_dev(kvm, &pci_pt_dev);
+		if (r)
+			goto out;
+		break;
+	}
 	case KVM_GET_PIT: {
 		struct kvm_pit_state ps;
 		r = -EFAULT;
@@ -3948,6 +4239,7 @@ struct  kvm *kvm_arch_create_vm(void)
 		return ERR_PTR(-ENOMEM);
 
 	INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
+	INIT_LIST_HEAD(&kvm->arch.pci_pt_dev_head);
 
 	return kvm;
 }
@@ -3980,6 +4272,7 @@ static void kvm_free_vcpus(struct kvm *kvm)
 
 void kvm_arch_destroy_vm(struct kvm *kvm)
 {
+	kvm_free_pci_passthrough(kvm);
 	kvm_free_pit(kvm);
 	kfree(kvm->arch.vpic);
 	kfree(kvm->arch.vioapic);
include/asm-x86/kvm_host.h +38 -0
index 4a47859..2346c9f 100644
@@ -88,6 +88,7 @@
 #define KVM_NR_VAR_MTRR 8
 
 extern spinlock_t kvm_lock;
+extern rwlock_t kvm_pci_pt_lock;
 extern struct list_head vm_list;
 
 struct kvm_vcpu;
@@ -319,6 +320,38 @@ struct kvm_mem_alias {
 	gfn_t target_gfn;
 };
 
+/* Some definitions for devices assigned to the guest by the host */
+#define KVM_PT_SOURCE_IRQ	1
+#define KVM_PT_SOURCE_IRQ_ACK	2
+#define KVM_PT_SOURCE_UPDATE	3
+
+/* For assigned devices, we schedule work in the system workqueue to
+ * inject interrupts into the guest when an interrupt occurs on the
+ * physical device and also when the guest acks the interrupt.
+ */
+struct kvm_pci_pt_work {
+	struct work_struct work;
+	struct kvm *kvm;
+	int irq;
+	bool source;
+};
+
+struct kvm_pci_passthrough_dev_kernel {
+	struct kvm_pci_pt_info guest;
+	struct kvm_pci_pt_info host;
+	struct kvm_pci_pt_work int_work;
+	struct kvm_pci_pt_work ack_work;
+	struct pci_dev *dev;
+};
+
+/* This list is to store the guest bus:device:function-irq and host
+ * bus:device:function-irq mapping for assigned devices.
+ */
+struct kvm_pci_pt_dev_list {
+	struct list_head list;
+	struct kvm_pci_passthrough_dev_kernel pt_dev;
+};
+
 struct kvm_arch{
 	int naliases;
 	struct kvm_mem_alias aliases[KVM_ALIAS_SLOTS];
@@ -331,6 +364,7 @@ struct kvm_arch{
 	 * Hash table of struct kvm_mmu_page.
 	 */
 	struct list_head active_mmu_pages;
+	struct list_head pci_pt_dev_head;
 	struct kvm_pic *vpic;
 	struct kvm_ioapic *vioapic;
 	struct kvm_pit *vpit;
@@ -577,6 +611,10 @@ void kvm_disable_tdp(void);
 int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3);
 int complete_pio(struct kvm_vcpu *vcpu);
 
+struct kvm_pci_pt_dev_list *
+kvm_find_pci_pt_dev(struct list_head *head,
+		    struct kvm_pci_pt_info *pt_pci_info, int irq, int source);
+
 static inline struct kvm_mmu_page *page_header(hpa_t shadow_page)
 {
 	struct page *page = pfn_to_page(shadow_page >> PAGE_SHIFT);
include/asm-x86/kvm_para.h +15 -1
index 76f3921..b6c5d00 100644
@@ -142,6 +142,20 @@ static inline unsigned int kvm_arch_para_features(void)
 	return cpuid_eax(KVM_CPUID_FEATURES);
 }
 
-#endif
+#endif /* KERNEL */
 
+/* Stores information for identifying host PCI devices assigned to the
+ * guest: this is used in the host kernel and in the userspace.
+ */
+struct kvm_pci_pt_info {
+	unsigned char busnr;
+	unsigned int devfn;
+	__u32 irq;
+};
+
+/* Mapping between host and guest PCI device */
+struct kvm_pci_passthrough_dev {
+	struct kvm_pci_pt_info guest;
+	struct kvm_pci_pt_info host;
+};
 #endif
include/linux/kvm.h +3 -0
index 6edba45..3370e80 100644
@@ -382,6 +382,7 @@ struct kvm_trace_rec {
 #define KVM_CAP_PV_MMU 13
 #define KVM_CAP_MP_STATE 14
 #define KVM_CAP_COALESCED_MMIO 15
+#define KVM_CAP_PCI_PASSTHROUGH 16
 
 /*
  * ioctls for VM fds
@@ -411,6 +412,8 @@ struct kvm_trace_rec {
 			_IOW(KVMIO,  0x67, struct kvm_coalesced_mmio_zone)
 #define KVM_UNREGISTER_COALESCED_MMIO \
 			_IOW(KVMIO,  0x68, struct kvm_coalesced_mmio_zone)
+#define KVM_UPDATE_PCI_PT_DEV	  _IOR(KVMIO, 0x69, \
+				       struct kvm_pci_passthrough_dev)
 
 /*
  * ioctls for vcpu fds
virt/kvm/ioapic.c +11 -2
index 8ce93c7..0b5bc40 100644
@@ -288,13 +288,22 @@ void kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
 static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int gsi)
 {
 	union ioapic_redir_entry *ent;
+	struct kvm_pci_pt_dev_list *match;
+	unsigned long flags;
 
 	ent = &ioapic->redirtbl[gsi];
 	ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
 
 	ent->fields.remote_irr = 0;
-	if (!ent->fields.mask && (ioapic->irr & (1 << gsi)))
-		ioapic_service(ioapic, gsi);
+
+	read_lock_irqsave(&kvm_pci_pt_lock, flags);
+	match = kvm_find_pci_pt_dev(&ioapic->kvm->arch.pci_pt_dev_head, NULL,
+				    gsi, KVM_PT_SOURCE_IRQ_ACK);
+	read_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+	if (!match) {
+		if (!ent->fields.mask && (ioapic->irr & (1 << gsi)))
+			ioapic_service(ioapic, gsi);
+	}
 
 	if (ioapic->ack_notifier)
 		ioapic->ack_notifier(ioapic->kvm, gsi);
Signature · 1 line
1.5.6
#7 lore raw

PATCH 7/8 KVM: PCIPT: VT-d support

In reply to Ben-Ami Yassour

This patch includes the functions to support VT-d for passthrough devices.

[Ben: fixed memory pinning, cleanup]

---
 arch/x86/kvm/Makefile      |    2 +-
 arch/x86/kvm/vtd.c         |  176 ++++++++++++++++++++++++++++++++++++++++++++
 arch/x86/kvm/x86.c         |   11 +++
 include/asm-x86/kvm_host.h |    1 +
 include/linux/kvm_host.h   |    6 ++
 virt/kvm/kvm_main.c        |    6 ++
 6 files changed, 201 insertions(+), 1 deletions(-)
 create mode 100644 arch/x86/kvm/vtd.c
arch/x86/kvm/Makefile +1 -1
index d0e940b..5d9d079 100644
@@ -11,7 +11,7 @@ endif
 EXTRA_CFLAGS += -Ivirt/kvm -Iarch/x86/kvm
 
 kvm-objs := $(common-objs) x86.o mmu.o x86_emulate.o i8259.o irq.o lapic.o \
-	i8254.o
+	i8254.o vtd.o
 obj-$(CONFIG_KVM) += kvm.o
 kvm-intel-objs = vmx.o
 obj-$(CONFIG_KVM_INTEL) += kvm-intel.o
arch/x86/kvm/vtd.c new file +176 -0

176 lines not on this page · raw message

arch/x86/kvm/x86.c +11 -0
index 65b307d..9a1caf0 100644
@@ -33,6 +33,7 @@
 #include <linux/module.h>
 #include <linux/mman.h>
 #include <linux/highmem.h>
+#include <linux/intel-iommu.h>
 
 #include <asm/uaccess.h>
 #include <asm/msr.h>
@@ -302,8 +303,16 @@ static int kvm_vm_ioctl_pci_pt_dev(struct kvm *kvm,
 		}
 	}
 
+	if (intel_iommu_found()) {
+		r = kvm_iommu_map_guest(kvm, pci_pt_dev);
+		if (r)
+			goto out_intr;
+	}
+
 out:
 	return r;
+out_intr:
+	free_irq(dev->irq, &match->pt_dev);
 out_list_del:
 	list_del(&match->list);
 out_put:
@@ -4246,6 +4255,8 @@ static void kvm_free_vcpus(struct kvm *kvm)
 
 void kvm_arch_destroy_vm(struct kvm *kvm)
 {
+	if (intel_iommu_found())
+		kvm_iommu_unmap_guest(kvm);
 	kvm_free_pci_passthrough(kvm);
 	kvm_free_pit(kvm);
 	kfree(kvm->arch.vpic);
include/asm-x86/kvm_host.h +1 -0
index f6973e0..6185ed7 100644
@@ -364,6 +364,7 @@ struct kvm_arch{
 	 */
 	struct list_head active_mmu_pages;
 	struct list_head pci_pt_dev_head;
+	struct dmar_domain *intel_iommu_domain;
 	struct kvm_pic *vpic;
 	struct kvm_ioapic *vioapic;
 	struct kvm_pit *vpit;
include/linux/kvm_host.h +6 -0
index 3798097..e9eae80 100644
@@ -279,6 +279,12 @@ int kvm_cpu_has_interrupt(struct kvm_vcpu *v);
 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu);
 void kvm_vcpu_kick(struct kvm_vcpu *vcpu);
 
+int kvm_iommu_map_pages(struct kvm *kvm, gfn_t base_gfn,
+			unsigned long npages);
+int kvm_iommu_map_guest(struct kvm *kvm,
+			struct kvm_pci_passthrough_dev *pci_pt_dev);
+int kvm_iommu_unmap_guest(struct kvm *kvm);
+
 static inline void kvm_guest_enter(void)
 {
 	account_system_vtime(current);
virt/kvm/kvm_main.c +6 -0
index 825fbd3..77d7001 100644
@@ -41,6 +41,7 @@
 #include <linux/pagemap.h>
 #include <linux/mman.h>
 #include <linux/swap.h>
+#include <linux/intel-iommu.h>
 
 #include <asm/processor.h>
 #include <asm/io.h>
@@ -425,6 +426,11 @@ int __kvm_set_memory_region(struct kvm *kvm,
 	}
 
 	kvm_free_physmem_slot(&old, &new);
+
+	/* map the pages in iommu page table */
+	if (intel_iommu_found())
+		kvm_iommu_map_pages(kvm, base_gfn, npages);
+
 	return 0;
 
 out_free:
Signature · 1 line
1.5.6
#8 lore raw

PATCH 8/8 KVM: PCIPT: VT-d: dont map mmio memory slots

Avoid mapping mmio memory slots.

---
 arch/x86/kvm/vtd.c         |   20 +++++++++++++-------
 include/asm-x86/kvm_host.h |    2 ++
 virt/kvm/kvm_main.c        |    2 +-
 3 files changed, 16 insertions(+), 8 deletions(-)
arch/x86/kvm/vtd.c +13 -7
index 83efb8a..77044fb 100644
@@ -40,14 +40,20 @@ int kvm_iommu_map_pages(struct kvm *kvm,
 
 	for (i = 0; i < npages; i++) {
 		pfn = gfn_to_pfn(kvm, gfn);
-		rc = intel_iommu_page_mapping(domain,
-					      gfn << PAGE_SHIFT,
+		if (!is_mmio_pfn(pfn)) {
+			rc = intel_iommu_page_mapping(domain,
+						      gfn << PAGE_SHIFT,
 						      pfn << PAGE_SHIFT,
-					      PAGE_SIZE,
-					      DMA_PTE_READ |
-					      DMA_PTE_WRITE);
-		if (rc)
-			kvm_release_pfn_clean(pfn);
+						      PAGE_SIZE,
+						      DMA_PTE_READ |
+						      DMA_PTE_WRITE);
+			if (rc)
+				kvm_release_pfn_clean(pfn);
+		} else {
+			printk(KERN_DEBUG "kvm_iommu_map_page:"
+			       "invalid pfn=%lx\n", pfn);
+			return 0;
+		}
 
 		gfn++;
 	}
include/asm-x86/kvm_host.h +2 -0
index 6185ed7..ee4685c 100644
@@ -513,6 +513,8 @@ int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
 int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
 		  gpa_t addr, unsigned long *ret);
 
+int is_mmio_pfn(pfn_t pfn);
+
 extern bool tdp_enabled;
 
 enum emulation_result {
virt/kvm/kvm_main.c +1 -1
index 77d7001..0653ec1 100644
@@ -77,7 +77,7 @@ static inline int valid_vcpu(int n)
 	return likely(n >= 0 && n < KVM_MAX_VCPUS);
 }
 
-static inline int is_mmio_pfn(pfn_t pfn)
+inline int is_mmio_pfn(pfn_t pfn)
 {
 	if (pfn_valid(pfn))
 		return PageReserved(pfn_to_page(pfn));
Signature · 1 line
1.5.6
#6 lore raw

PATCH 6/8 VT-d: changes to support KVM

In reply to Ben-Ami Yassour

From: Kay, Allen M <[email protected]>

This patch extends the VT-d driver to support KVM

[Ben: fixed memory pinning]

---
 drivers/pci/dmar.c                           |    4 +-
 drivers/pci/intel-iommu.c                    |  117 +++++++++++++++++++++++++-
 drivers/pci/iova.c                           |    2 +-
 {drivers/pci => include/linux}/intel-iommu.h |   11 +++
 {drivers/pci => include/linux}/iova.h        |    0 
 5 files changed, 127 insertions(+), 7 deletions(-)
 rename {drivers/pci => include/linux}/intel-iommu.h (94%)
 rename {drivers/pci => include/linux}/iova.h (100%)
drivers/pci/dmar.c +2 -2
index f941f60..a58a5b0 100644
@@ -26,8 +26,8 @@
 
 #include <linux/pci.h>
 #include <linux/dmar.h>
-#include "iova.h"
-#include "intel-iommu.h"
+#include <linux/iova.h>
+#include <linux/intel-iommu.h>
 
 #undef PREFIX
 #define PREFIX "DMAR:"
drivers/pci/intel-iommu.c +113 -4

144 lines not on this page · raw message

drivers/pci/iova.c +1 -1
index 3ef4ac0..2287116 100644
@@ -7,7 +7,7 @@
  * Author: Anil S Keshavamurthy <[email protected]>
  */
 
-#include "iova.h"
+#include <linux/iova.h>
 
 void
 init_iova_domain(struct iova_domain *iovad, unsigned long pfn_32bit)
include/linux/intel-iommu.h renamed from drivers/pci/intel-iommu.h +11 -0
similarity index 94%
rename from drivers/pci/intel-iommu.h
rename to include/linux/intel-iommu.h
index afc0ad9..1490fc0 100644
@@ -341,4 +341,15 @@ static inline void iommu_prepare_gfx_mapping(void)
 }
 #endif /* !CONFIG_DMAR_GFX_WA */
 
+void intel_iommu_domain_exit(struct dmar_domain *domain);
+struct dmar_domain *intel_iommu_domain_alloc(struct pci_dev *pdev);
+int intel_iommu_context_mapping(struct dmar_domain *domain,
+				struct pci_dev *pdev);
+int intel_iommu_page_mapping(struct dmar_domain *domain, dma_addr_t iova,
+			     u64 hpa, size_t size, int prot);
+void intel_iommu_detach_dev(struct dmar_domain *domain, u8 bus, u8 devfn);
+struct dmar_domain *intel_iommu_find_domain(struct pci_dev *pdev);
+int intel_iommu_found(void);
+u64 intel_iommu_iova_to_pfn(struct dmar_domain *domain, u64 iova);
+
 #endif
include/linux/iova.h renamed from drivers/pci/iova.h +0 -0
similarity index 100%
rename from drivers/pci/iova.h
rename to include/linux/iova.h
Signature · 1 line
1.5.6
#1 lore raw

PATCH 1/8 KVM: Introduce a callback routine for IOAPIC ack handling

In reply to Ben-Ami Yassour

From: Amit Shah <[email protected]>

This will be useful for acking irqs of assigned devices

---
 virt/kvm/ioapic.c |    3 +++
 virt/kvm/ioapic.h |    1 +
 2 files changed, 4 insertions(+), 0 deletions(-)
virt/kvm/ioapic.c +3 -0
index c0d2287..8ce93c7 100644
@@ -295,6 +295,9 @@ static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int gsi)
 	ent->fields.remote_irr = 0;
 	if (!ent->fields.mask && (ioapic->irr & (1 << gsi)))
 		ioapic_service(ioapic, gsi);
+
+	if (ioapic->ack_notifier)
+		ioapic->ack_notifier(ioapic->kvm, gsi);
 }
 
 void kvm_ioapic_update_eoi(struct kvm *kvm, int vector)
virt/kvm/ioapic.h +1 -0
index 7f16675..a42743f 100644
@@ -58,6 +58,7 @@ struct kvm_ioapic {
 	} redirtbl[IOAPIC_NUM_PINS];
 	struct kvm_io_device dev;
 	struct kvm *kvm;
+	void (*ack_notifier)(void *opaque, int irq);
 };
 
 #ifdef DEBUG
Signature · 1 line
1.5.6
#10 lore raw

PATCH 1/2 KVM/userspace: Support for assigning PCI devices to guest

From: Or Sagi <[email protected]>

From: Nir Peleg <[email protected]>
From: Amit Shah <[email protected]>
From: Glauber de Oliveira Costa <[email protected]>

We can assign a device from the host machine to a guest. The original code comes from Neocleus.

A new command-line option, -pcidevice is added. For example, to invoke it for an Ethernet device sitting at PCI bus:dev.fn 04:08.0 with host IRQ 18, use this:

        -pcidevice Ethernet/04:08.0

The host ethernet driver is to be removed before doing the passthrough. If not, the device assignment fails but the guest continues without the assignment.

If kvm uses the in-kernel irqchip, interrupts are routed to the guest via the kvm module (accompanied kernel changes are necessary).

If -no-kvm-irqchip is used, the 'irqhook' module, available separately, is to be used for interrupt injection into the guest. In this case, an extra parameter, -<intr-number> is to be appended to the above-mentioned pcidevice parameter.

---
 libkvm/libkvm-x86.c       |    9 +-
 libkvm/libkvm.h           |   16 ++
 qemu/Makefile.target      |    1 +
 qemu/hw/isa.h             |    2 +
 qemu/hw/pc.c              |    9 +
 qemu/hw/pci-passthrough.c |  594 +++++++++++++++++++++++++++++++++++++++++++++
 qemu/hw/pci-passthrough.h |   93 +++++++
 qemu/hw/pci.c             |   12 +
 qemu/hw/pci.h             |    1 +
 qemu/hw/piix_pci.c        |   19 ++
 qemu/vl.c                 |   17 ++
 11 files changed, 772 insertions(+), 1 deletions(-)
 create mode 100644 qemu/hw/pci-passthrough.c
 create mode 100644 qemu/hw/pci-passthrough.h
libkvm/libkvm-x86.c +8 -1
index ea97bdd..0c4cdbe 100644
@@ -126,6 +126,14 @@ static int kvm_init_tss(kvm_context_t kvm)
 	return 0;
 }
 
+#ifdef KVM_CAP_PCI_PASSTHROUGH
+int kvm_update_pci_pt_device(kvm_context_t kvm,
+			     struct kvm_pci_passthrough_dev *pci_pt_dev)
+{
+	return ioctl(kvm->vm_fd, KVM_UPDATE_PCI_PT_DEV, pci_pt_dev);
+}
+#endif
+
 int kvm_arch_create_default_phys_mem(kvm_context_t kvm,
 				       unsigned long phys_mem_bytes,
 				       void **vm_mem)
@@ -435,7 +443,6 @@ void kvm_show_code(kvm_context_t kvm, int vcpu)
 	fprintf(stderr, "code:%s\n", code_str);
 }
 
-
 /*
  * Returns available msr list.  User must free.
  */
libkvm/libkvm.h +16 -0
index ad6e26a..ccb086f 100644
@@ -12,6 +12,7 @@
 #endif
 
 #include <linux/kvm.h>
+#include <linux/kvm_para.h>
 
 #include <signal.h>
 
@@ -639,4 +640,19 @@ int kvm_enable_vapic(kvm_context_t kvm, int vcpu, uint64_t vapic);
 
 #endif
 
+#ifdef KVM_CAP_PCI_PASSTHROUGH
+/*!
+ * \brief Notifies host kernel about changes to a PCI device assigned to guest
+ *
+ * Used for PCI device assignment, this function notifies the host
+ * kernel about the assigning of the physical PCI device and the guest
+ * PCI parameters or updates to the PCI config space from the guest
+ * (mainly the device irq)
+ *
+ * \param kvm Pointer to the current kvm_context
+ * \param pci_pt_dev Parameters like irq, PCI bus, devfn number, etc
+ */
+int kvm_update_pci_pt_device(kvm_context_t kvm,
+			     struct kvm_pci_passthrough_dev *pci_pt_dev);
+#endif
 #endif
qemu/Makefile.target +1 -0
index 7a51cd0..4ad858b 100644
@@ -605,6 +605,7 @@ OBJS+= ide.o pckbd.o ps2.o vga.o $(SOUND_HW) dma.o
 OBJS+= fdc.o mc146818rtc.o serial.o i8259.o i8254.o pcspk.o pc.o
 OBJS+= cirrus_vga.o apic.o parallel.o acpi.o piix_pci.o
 OBJS+= usb-uhci.o vmmouse.o vmport.o vmware_vga.o extboot.o
+OBJS+= pci-passthrough.o
 ifeq ($(USE_KVM_PIT), 1)
 OBJS+= i8254-kvm.o
 endif
qemu/hw/isa.h +2 -0
index 89b3004..c720f5e 100644
@@ -1,5 +1,7 @@
 /* ISA bus */
 
+#include "hw.h"
+
 extern target_phys_addr_t isa_mem_base;
 
 int register_ioport_read(int start, int length, int size,
qemu/hw/pc.c +9 -0
index d578e68..090d92c 100644
@@ -32,6 +32,7 @@
 #include "smbus.h"
 #include "boards.h"
 #include "console.h"
+#include "pci-passthrough.h"
 
 #include "qemu-kvm.h"
 
@@ -994,6 +995,14 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size,
         }
     }
 
+    /* Initialize pass-through */
+    if (pci_enabled) {
+        int r = -1;
+        do {
+            pt_init_device(pci_bus, &r);
+	} while (r >= 0);
+    }
+
     rtc_state = rtc_init(0x70, i8259[8]);
 
     qemu_register_boot_set(pc_boot_set, rtc_state);
qemu/hw/pci-passthrough.c new file +594 -0

594 lines not on this page · raw message

qemu/hw/pci-passthrough.h new file +93 -0

93 lines not on this page · raw message

qemu/hw/pci.c +12 -0
index 92683d1..ff21b83 100644
@@ -50,6 +50,7 @@ struct PCIBus {
 
 static void pci_update_mappings(PCIDevice *d);
 static void pci_set_irq(void *opaque, int irq_num, int level);
+void pci_pt_update_irq(PCIDevice *d);
 
 target_phys_addr_t pci_mem_base;
 static int pci_irq_index;
@@ -453,6 +454,12 @@ void pci_default_write_config(PCIDevice *d,
         val >>= 8;
     }
 
+#ifdef KVM_CAP_PCI_PASSTHROUGH
+    if (kvm_enabled() && qemu_kvm_irqchip_in_kernel() &&
+	address >= 0x60 && address <= 0x63)
+	pci_pt_update_irq(d);
+#endif
+
     end = address + len;
     if (end > PCI_COMMAND && address < (PCI_COMMAND + 2)) {
         /* if the command register is modified, we must modify the mappings */
@@ -555,6 +562,11 @@ static void pci_set_irq(void *opaque, int irq_num, int level)
     bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
 }
 
+int pci_map_irq(PCIDevice *pci_dev, int pin)
+{
+	return pci_dev->bus->map_irq(pci_dev, pin);
+}
+
 /***********************************************************/
 /* monitor info on PCI */
 
qemu/hw/pci.h +1 -0
index 60e4094..e11fbbf 100644
@@ -81,6 +81,7 @@ void pci_register_io_region(PCIDevice *pci_dev, int region_num,
                             uint32_t size, int type,
                             PCIMapIORegionFunc *map_func);
 
+int pci_map_irq(PCIDevice *pci_dev, int pin);
 uint32_t pci_default_read_config(PCIDevice *d,
                                  uint32_t address, int len);
 void pci_default_write_config(PCIDevice *d,
qemu/hw/piix_pci.c +19 -0
index 90cb3a6..112381a 100644
@@ -237,6 +237,25 @@ static void piix3_set_irq(qemu_irq *pic, int irq_num, int level)
     }
 }
 
+int piix3_get_pin(int pic_irq)
+{
+    int i;
+    for (i = 0; i < 4; i++)
+        if (piix3_dev->config[0x60+i] == pic_irq)
+            return i;
+    return -1;
+}
+
+int piix_get_irq(int pin)
+{
+    if (piix3_dev)
+        return piix3_dev->config[0x60+pin];
+    if (piix4_dev)
+        return piix4_dev->config[0x60+pin];
+
+    return 0;
+}
+
 static void piix3_reset(PCIDevice *d)
 {
     uint8_t *pci_conf = d->config;
qemu/vl.c +17 -0
index db7095f..ca63b2e 100644
@@ -37,6 +37,7 @@
 #include "qemu-char.h"
 #include "block.h"
 #include "audio/audio.h"
+#include "hw/pci-passthrough.h"
 #include "migration.h"
 #include "qemu-kvm.h"
 
@@ -7968,6 +7969,11 @@ static void help(int exitcode)
 #endif
 	   "-no-kvm-irqchip disable KVM kernel mode PIC/IOAPIC/LAPIC\n"
 	   "-no-kvm-pit	    disable KVM kernel mode PIT\n"
+#if defined(TARGET_I386) || defined(TARGET_X86_64)
+	   "-pcidevice name/bus:dev.func\n"
+	   "                expose a PCI device to the guest OS.\n"
+	   "                'name' is just used for debug logs.\n"
+#endif
 #endif
 #ifdef TARGET_I386
            "-std-vga        simulate a standard VGA card with VESA Bochs Extensions\n"
@@ -8093,6 +8099,9 @@ enum {
     QEMU_OPTION_no_kvm,
     QEMU_OPTION_no_kvm_irqchip,
     QEMU_OPTION_no_kvm_pit,
+#if defined(TARGET_I386) || defined(TARGET_X86_64)
+    QEMU_OPTION_pcidevice,
+#endif
     QEMU_OPTION_no_reboot,
     QEMU_OPTION_no_shutdown,
     QEMU_OPTION_show_cursor,
@@ -8182,6 +8191,9 @@ const QEMUOption qemu_options[] = {
 #endif
     { "no-kvm-irqchip", 0, QEMU_OPTION_no_kvm_irqchip },
     { "no-kvm-pit", 0, QEMU_OPTION_no_kvm_pit },
+#if defined(TARGET_I386) || defined(TARGET_X86_64)
+    { "pcidevice", HAS_ARG, QEMU_OPTION_pcidevice },
+#endif
 #endif
 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
     { "g", 1, QEMU_OPTION_g },
@@ -9095,6 +9107,11 @@ int main(int argc, char **argv)
 		kvm_pit = 0;
 		break;
 	    }
+#if defined(TARGET_I386) || defined(TARGET_X86_64)
+	    case QEMU_OPTION_pcidevice:
+		add_pci_passthrough_device(optarg);
+		break;
+#endif
 #endif
             case QEMU_OPTION_usb:
                 usb_enabled = 1;
Signature · 1 line
1.5.6
#11 lore raw

PATCH 2/2 PCIPT: direct mmio

Enable a guest to access a device's memory mapped I/O regions directly. When the guest changes the I/O memory mappings for a passthrough device, send the updated mmio region to the kernel.

---
 libkvm/libkvm.c           |   24 ++++++++----
 qemu/hw/pci-passthrough.c |   88 +++++++++++---------------------------------
 qemu/hw/pci-passthrough.h |    2 +
 3 files changed, 40 insertions(+), 74 deletions(-)
libkvm/libkvm.c +16 -8
index a49cbdc..35bc411 100644
@@ -400,7 +400,7 @@ void *kvm_create_userspace_phys_mem(kvm_context_t kvm, unsigned long phys_start,
 {
 	int r;
 	int prot = PROT_READ;
-	void *ptr;
+	void *ptr = NULL;
 	struct kvm_userspace_memory_region memory = {
 		.memory_size = len,
 		.guest_phys_addr = phys_start,
@@ -410,16 +410,24 @@ void *kvm_create_userspace_phys_mem(kvm_context_t kvm, unsigned long phys_start,
 	if (writable)
 		prot |= PROT_WRITE;
 
-	ptr = mmap(NULL, len, prot, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
-	if (ptr == MAP_FAILED) {
-		fprintf(stderr, "create_userspace_phys_mem: %s", strerror(errno));
-		return 0;
-	}
+	if (len > 0) {
+		ptr = mmap(NULL, len, prot, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
+		if (ptr == MAP_FAILED) {
+			fprintf(stderr, "create_userspace_phys_mem: %s",
+				strerror(errno));
+			return 0;
+		}
 
-	memset(ptr, 0, len);
+		memset(ptr, 0, len);
+	}
 
 	memory.userspace_addr = (unsigned long)ptr;
-	memory.slot = get_free_slot(kvm);
+
+	if (len > 0)
+		memory.slot = get_free_slot(kvm);
+	else
+		memory.slot = get_slot(phys_start);
+
 	r = ioctl(kvm->vm_fd, KVM_SET_USER_MEMORY_REGION, &memory);
 	if (r == -1) {
 		fprintf(stderr, "create_userspace_phys_mem: %s", strerror(errno));
qemu/hw/pci-passthrough.c +22 -66

118 lines not on this page · raw message

qemu/hw/pci-passthrough.h +2 -0
index 60df017..7a98b0e 100644
@@ -65,6 +65,8 @@ typedef struct pt_region_s {
 	uint32_t memory_index;
 	void *r_virtbase;	/* mmapped access address */
 	int num;		/* our index within v_addrs[] */
+	uint32_t e_size;        /* emulated size of region in bytes */
+	uint32_t r_size;        /* real size of region in bytes */
 	uint32_t debug;
 } pt_region_t;
 
Signature · 1 line
1.5.6
#33 lore raw
In reply to Ben-Ami Yassour
Quoted 3 lines · Ben-Ami Yassour

In last few tests that we made with PCI-passthrough and VT-d using iperf, we were able to get the same throughput as on native OS with a 1G NIC

Excellent!

Quoted 2 lines

(with higher CPU utilization).

How much higher?

Quoted 7 lines

The following patches are the PCI-passthrough patches that Amit sent (re-based on the last kvm tree), followed by a few improvements and the VT-d extension.
I am also sending the userspace patches: the patch that Amit sent for PCI passthrough and the direct-mmio extension for userspace (note that without the direct mmio extension we get less then half the throughput).

Is mmio passthrough the reason for the performance improvement? If not, what was the problem?

Signature · 1 line
error compiling committee.c: too many arguments to function
#21 lore raw

Re: [PATCH 3/8] KVM: Handle device assignment to guests

In reply to Ben-Ami Yassour
Quoted 36 lines · Ben-Ami Yassour

From: Han, Weidong <[email protected]>

This patch adds support for handling PCI devices that are assigned to the guest ("PCI passthrough").

arch/x86/kvm/x86.c +31 -0
+
+/*
+ * Used to find a registered host PCI device (a "passthrough" device)
+ * during ioctls, interrupts or EOI
+ */
+struct kvm_pci_pt_dev_list *
+kvm_find_pci_pt_dev(struct list_head *head,
+		struct kvm_pci_pt_info *pt_pci_info, int irq, int source)
+{
+	struct list_head *ptr;
+	struct kvm_pci_pt_dev_list *match;
+
+	list_for_each(ptr, head) {
+		match = list_entry(ptr, struct kvm_pci_pt_dev_list, list);
+
+		switch (source) {
+		case KVM_PT_SOURCE_IRQ:
+			/*
+			 * Used to find a registered host device
+			 * during interrupt context on host
+			 */
+			if (match->pt_dev.host.irq == irq)
+				return match;
+			break;
+		case KVM_PT_SOURCE_IRQ_ACK:
+			/*
+			 * Used to find a registered host device when
+			 * the guest acks an interrupt
+			 */
+			if (match->pt_dev.guest.irq == irq)
+				return match;

arch/x86/kvm/x86.c struct kvm_stats_debugfs_item debugfs_entries[] = { · line 146

Quoted 10 lines
arch/x86/kvm/x86.c +10 -0
+			break;
+		case KVM_PT_SOURCE_UPDATE:
+			if ((match->pt_dev.host.busnr == pt_pci_info->busnr) &&
+			    (match->pt_dev.host.devfn == pt_pci_info->devfn))
+				return match;
+			break;
+		}
+	}
+	return NULL;
+}

This monster is best split into three functions each handling a separate case, without the 'source' argument.

Quoted 38 lines
arch/x86/kvm/x86.c +37 -0
+static void kvm_pci_pt_work_fn(struct work_struct *work)
+{
+	struct kvm_pci_pt_dev_list *match;
+	struct kvm_pci_pt_work *int_work;
+	int source;
+	unsigned long flags;
+	int guest_irq;
+	int host_irq;
+
+	int_work = container_of(work, struct kvm_pci_pt_work, work);
+
+	source = int_work->source ? KVM_PT_SOURCE_IRQ_ACK : KVM_PT_SOURCE_IRQ;
+
+	/* This is taken to safely inject irq inside the guest. When
+	 * the interrupt injection (or the ioapic code) uses a
+	 * finer-grained lock, update this
+	 */
+	mutex_lock(&int_work->kvm->lock);
+	read_lock_irqsave(&kvm_pci_pt_lock, flags);
+	match = kvm_find_pci_pt_dev(&int_work->kvm->arch.pci_pt_dev_head, NULL,
+				    int_work->irq, source);
+	if (!match) {
+		printk(KERN_ERR "%s: no matching device assigned to guest "
+		       "found for irq %d, source = %d!\n",
+		       __func__, int_work->irq, int_work->source);
+		read_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+		goto out;
+	}
+	guest_irq = match->pt_dev.guest.irq;
+	host_irq = match->pt_dev.host.irq;
+	read_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+
+	if (source == KVM_PT_SOURCE_IRQ)
+		kvm_set_irq(int_work->kvm, guest_irq, 1);
+	else {
+		kvm_set_irq(int_work->kvm, int_work->irq, 0);
+		enable_irq(host_irq);

arch/x86/kvm/x86.c struct kvm_stats_debugfs_item debugfs_entries[] = { · line 195

Quoted 10 lines
arch/x86/kvm/x86.c +9 -0
+	}
+out:
+	mutex_unlock(&int_work->kvm->lock);
+	kvm_put_kvm(int_work->kvm);
+}
 
+
+/* FIXME: Implement the OR logic needed to make shared interrupts on
+ * this line behave properly
+ */

Isn't this a showstopper? There is no easy way for a user to avoid sharing, especially as we have only three pci irqs at present.

Quoted 6 lines
arch/x86/kvm/x86.c +5 -0
+static irqreturn_t kvm_pci_pt_dev_intr(int irq, void *dev_id)
+{
+	struct kvm *kvm = (struct kvm *) dev_id;
+	struct kvm_pci_pt_dev_list *pci_pt_dev;
+

arch/x86/kvm/x86.c struct kvm_stats_debugfs_item debugfs_entries[] = { · line 210

Quoted 10 lines
arch/x86/kvm/x86.c +10 -0
+	if (!test_bit(irq, pt_irq_handled))
+		return IRQ_NONE;
+
+	read_lock(&kvm_pci_pt_lock);
+	pci_pt_dev = kvm_find_pci_pt_dev(&kvm->arch.pci_pt_dev_head, NULL,
+					 irq, KVM_PT_SOURCE_IRQ);
+	if (!pci_pt_dev) {
+		read_unlock(&kvm_pci_pt_lock);
+		return IRQ_NONE;
+	}

I see we don't reuse the result of the search. I guess we can't, since the list may change between the interrupt and the execution of the work function.

arch/x86/kvm/x86.c struct kvm_stats_debugfs_item debugfs_entries[] = { · line 215

Quoted 6 lines
arch/x86/kvm/x86.c +5 -0
+
+	pci_pt_dev->pt_dev.int_work.irq = irq;
+	pci_pt_dev->pt_dev.int_work.kvm = kvm;
+	pci_pt_dev->pt_dev.int_work.source = 0;
+

For a bool, use false, not 0. But 'source' isn't really a good name for a boolean. Perhaps 'is_ack'?

Quoted 17 lines
arch/x86/kvm/x86.c +16 -0
+
+/* Ack the irq line for a passthrough device */
+static void kvm_pci_pt_ack_irq(void *opaque, int irq)
+{
+	struct kvm *kvm = opaque;
+	struct kvm_pci_pt_dev_list *pci_pt_dev;
+	unsigned long flags;
+
+	if (irq == -1)
+		return;
+
+	read_lock_irqsave(&kvm_pci_pt_lock, flags);
+	pci_pt_dev = kvm_find_pci_pt_dev(&kvm->arch.pci_pt_dev_head, NULL, irq,
+					 KVM_PT_SOURCE_IRQ_ACK);
+	if (!pci_pt_dev) {
+		read_unlock_irqrestore(&kvm_pci_pt_lock, flags);

arch/x86/kvm/x86.c struct kvm_stats_debugfs_item debugfs_entries[] = { · line 248

Quoted 10 lines
arch/x86/kvm/x86.c +10 -0
+		return;
+	}
+	pci_pt_dev->pt_dev.ack_work.irq = irq;
+	pci_pt_dev->pt_dev.ack_work.kvm = kvm;
+	pci_pt_dev->pt_dev.ack_work.source = 1;
+
+	kvm_get_kvm(kvm);
+	schedule_work(&pci_pt_dev->pt_dev.ack_work.work);
+	read_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+}

Why do we have to schedule this? We're already in process context, no?

Is this to avoid recursion into the pic/ioapic code? If so, we can use a queue to avoid this. That's not a merge blocker, however, so it can be done later.

include/asm-x86/kvm_para.h static inline unsigned int kvm_arch_para_features(void) · line 146

Quoted 11 lines
include/asm-x86/kvm_para.h +1 -1
index 76f3921..b6c5d00 100644
@@ -142,6 +142,20 @@ static inline unsigned int kvm_arch_para_features(void)
 	return cpuid_eax(KVM_CPUID_FEATURES);
 }
 
-#endif
+#endif /* KERNEL */
 

Move the following to linux/kvm.h. This used to be oriented at paravirt, but is really a host->kernel communication API.

include/asm-x86/kvm_para.h static inline unsigned int kvm_arch_para_features(void)

Quoted 10 lines
include/asm-x86/kvm_para.h +8 -0
+/* Stores information for identifying host PCI devices assigned to the
+ * guest: this is used in the host kernel and in the userspace.
+ */
+struct kvm_pci_pt_info {
+	unsigned char busnr;
+	unsigned int devfn;
+	__u32 irq;
+};
 

Badly aligned. Please use __u32 for all fields, and add a __u32 padding field at the end so we have the same ABI for i386 and x86_64.

Some pci devices support more than one irq. Should we make irq an array? Alternatively, decouple irq assignment from pci information and let userspace handle everything. This lets us assign non-pci devices (like serial ports, etc.).

Also, I don't like the term "passthrough", and very much dislike "pt", especially in public interfaces. It's simply too generic. Can we switch to "device assignment"?

Quoted 18 lines
include/asm-x86/kvm_para.h +0 -0
  * ioctls for vcpu fds
virt/kvm/ioapic.c +2 -2
index 8ce93c7..0b5bc40 100644
@@ -288,13 +288,22 @@ void kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
 static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int gsi)
 {
 	union ioapic_redir_entry *ent;
+	struct kvm_pci_pt_dev_list *match;
+	unsigned long flags;
 
 	ent = &ioapic->redirtbl[gsi];
 	ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
 
 	ent->fields.remote_irr = 0;
-	if (!ent->fields.mask && (ioapic->irr & (1 << gsi)))
-		ioapic_service(ioapic, gsi);

virt/kvm/ioapic.c void kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level) · line 307

Quoted 10 lines
virt/kvm/ioapic.c +9 -0
+
+	read_lock_irqsave(&kvm_pci_pt_lock, flags);
+	match = kvm_find_pci_pt_dev(&ioapic->kvm->arch.pci_pt_dev_head, NULL,
+				    gsi, KVM_PT_SOURCE_IRQ_ACK);
+	read_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+	if (!match) {
+		if (!ent->fields.mask && (ioapic->irr & (1 << gsi)))
+			ioapic_service(ioapic, gsi);
+	}
 

What's device assignment code doing in the ioapic? This should be done through the notifier (if it needs to return a value, great).

Quoted 4 lines
virt/kvm/ioapic.c +0 -0
 	if (ioapic->ack_notifier)
 		ioapic->ack_notifier(ioapic->kvm, gsi);
Signature · 1 line
error compiling committee.c: too many arguments to function
#16 lore raw

Re: [PATCH 4/8] KVM: PCIPT: fix interrupt handling

In reply to Ben-Ami Yassour
Quoted 4 lines · Ben-Ami Yassour

This patch fixes a few problems with the interrupt handling for passthrough devices.

Well, fold it into the patch it fixes. There is no point in sending a buggy patch and a fix in the same patchset.

Signature · 1 line
error compiling committee.c: too many arguments to function
#15 lore raw

Re: [PATCH 5/8] KVM: PCIPT: change order of device release

In reply to Ben-Ami Yassour

Ben-Ami Yassour wrote:

Fold into parent patch please.

Signature · 1 line
error compiling committee.c: too many arguments to function
#34 lore raw
In reply to Avi Kivity
Quoted 11 lines · Avi Kivity
Quoted 3 lines · Ben-Ami Yassour

In last few tests that we made with PCI-passthrough and VT-d using iperf, we were able to get the same throughput as on native OS with a 1G NIC

Excellent!

Quoted 2 lines

(with higher CPU utilization).

How much higher?

Here are some numbers for running iperf -l 1M:

e1000 NIC (behind a PCI bridge)
Bandwidth (Mbit/sec) CPU utilization

Native OS                   771                      18%
Native OS with VT-d         760                      18% 
KVM VT-d                    390                      95% 

KVM VT-d with direct mmio 770 84%
KVM emulated 57 100%

Comment: its not clear to me why the native linux can not get closer to 1G for this NIC, (I verified that its not external network issues). But clearly we shouldn't hope to get more then the host does with a KVM guest (especially if the guest and host are the same OS as in this case...).

e1000e NIC (onboard)
Bandwidth (Mbit/sec) CPU utilization

Native OS                   915                      18%
Native OS with VT-d         915                      18%

KVM VT-d with direct mmio 914 98%

Clearly we need to try and improve the CPU utilization, but I think that this is good enough for the first phase.

Quoted 9 lines
Quoted 8 lines

The following patches are the PCI-passthrough patches that Amit sent (re-based on the last kvm tree), followed by a few improvements and the VT-d extension.
I am also sending the userspace patches: the patch that Amit sent for PCI passthrough and the direct-mmio extension for userspace (note that without the direct mmio extension we get less then half the throughput).

Quoted 3 lines

Is mmio passthrough the reason for the performance improvement? If not, what was the problem?

Direct mmio was definitely a major improvement, without it we got half the throughput, as you can see above.
In addition patch 4/8 improves the interrupt handling and removes unnecessary locks, and I assume that it also fixed performance issues (I did not investigate exactly in what way).

Regards,
Ben

#35 lore raw
Quoted 17 lines · Ben-Ami Yassour
Quoted 5 lines
Quoted 3 lines

(with higher CPU utilization).

How much higher?

Here are some numbers for running iperf -l 1M:

e1000 NIC (behind a PCI bridge)
Bandwidth (Mbit/sec) CPU utilization

Native OS                   771                      18%
Native OS with VT-d         760                      18% KVM VT-d                    390                      95% KVM VT-d with direct mmio   770                      84%

KVM emulated 57 100%

Comment: its not clear to me why the native linux can not get closer to 1G for this NIC,
(I verified that its not external network issues). But clearly we shouldn't hope to get more then the host does with a KVM guest (especially if the guest and host are the same OS as in this case...).

Quoted 9 lines

e1000e NIC (onboard)
Bandwidth (Mbit/sec) CPU utilization

Native OS                   915                      18%
Native OS with VT-d         915                      18%

KVM VT-d with direct mmio 914 98%

Clearly we need to try and improve the CPU utilization, but I think that this is good enough for the first phase.

Agree; part of the higher utilization is of course not the fault of the device assignment code, rather it is ordinary virtualization overhead. We'll have to tune this.

Signature · 1 line
error compiling committee.c: too many arguments to function
#36 lore raw
In reply to Ben-Ami Yassour
Quoted 17 lines · Ben-Ami Yassour
Quoted 6 lines · Avi Kivity
Quoted 4 lines · Ben-Ami Yassour

In last few tests that we made with PCI-passthrough and VT-d using iperf, we were able to get the same throughput as on native OS with a 1G NIC

Quoted 8 lines

Excellent!

Quoted 3 lines

(with higher CPU utilization).

How much higher?

Quoted 8 lines

Here are some numbers for running iperf -l 1M:

e1000 NIC (behind a PCI bridge)
Bandwidth (Mbit/sec) CPU utilization

Native OS                   771                      18%
Native OS with VT-d         760                      18% KVM VT-d                    390                      95% KVM VT-d with direct mmio   770                      84%

KVM emulated 57 100%

What about virtio? Also, which emulated is this?

That CPU utilization is extremely high and somewhat illogical if native w/vt-d has almost no CPU impact. Have you run oprofile yet or have any insight into where CPU is being burnt?

What does kvm_stat look like? I wonder if there are a large number of PIO exits. What does the interrupt count look like on native vs. KVM with VT-d?

Regards,

Anthony Liguori

Quoted 33 lines

Comment: its not clear to me why the native linux can not get closer to 1G for this NIC,
(I verified that its not external network issues). But clearly we shouldn't hope to get more then the host does with a KVM guest (especially if the guest and host are the same OS as in this case...).

e1000e NIC (onboard)
Bandwidth (Mbit/sec) CPU utilization

Native OS                   915                      18%
Native OS with VT-d         915                      18%

KVM VT-d with direct mmio 914 98%

Clearly we need to try and improve the CPU utilization, but I think that this is good enough for the first phase.

Quoted 11 lines
Quoted 8 lines

The following patches are the PCI-passthrough patches that Amit sent (re-based on the last kvm tree), followed by a few improvements and the VT-d extension.
I am also sending the userspace patches: the patch that Amit sent for PCI passthrough and the direct-mmio extension for userspace (note that without the direct mmio extension we get less then half the throughput).

Is mmio passthrough the reason for the performance improvement? If not, what was the problem?

Direct mmio was definitely a major improvement, without it we got half the throughput, as you can see above.
In addition patch 4/8 improves the interrupt handling and removes unnecessary locks, and I assume that it also fixed performance issues (I did not investigate exactly in what way).

Regards,
Ben

#37 lore raw
Quoted 2 lines · Anthony Liguori

What about virtio?

didn't try it yet.

Quoted 1 line

Also, which emulated is this?

the default

Quoted 5 lines

That CPU utilization is extremely high and somewhat illogical if native w/vt-d has almost no CPU impact. Have you run oprofile yet or have any insight into where CPU is being burnt?

Quoted 8 lines

What does kvm_stat look like? I wonder if there are a large number of PIO exits. What does the interrupt count look like on native vs. KVM with VT-d?

Regards,

Anthony Liguori

These are all good points and questions, I agree that we need to take a deeper look into the performance issues, but I think that we need to merge with the main KVM tree first.

Regards,
Ben

#38 lore raw
Quoted 5 lines · Ben-Ami Yassour
Quoted 5 lines

That CPU utilization is extremely high and somewhat illogical if native w/vt-d has almost no CPU impact. Have you run oprofile yet or have any insight into where CPU is being burnt?

What does kvm_stat look like? I wonder if there are a large number of PIO exits. What does the interrupt count look like on native vs. KVM with VT-d?

Quoted 8 lines
Quoted 5 lines

Regards,

Anthony Liguori

These are all good points and questions, I agree that we need to take a deeper look into the performance issues, but I think that we need to merge with the main KVM tree first.

It would be good to get the host interrupt rate, to confirm that the host isn't flooded with interrupts. A deeper analysis can wait.

Signature · 1 line
error compiling committee.c: too many arguments to function
#22 lore raw

RE: [PATCH 3/8] KVM: Handle device assignment to guests

In reply to Avi Kivity
Quoted 11 lines · Avi Kivity
Quoted 11 lines
arch/x86/kvm/x86.c +10 -0
+static void kvm_pci_pt_work_fn(struct work_struct *work) +{
+	struct kvm_pci_pt_dev_list *match;
+	struct kvm_pci_pt_work *int_work;
+	int source;
+	unsigned long flags;
+	int guest_irq;
+	int host_irq;
+
+	int_work = container_of(work, struct kvm_pci_pt_work, work); +
+	source = int_work->source ? KVM_PT_SOURCE_IRQ_ACK :

KVM_PT_SOURCE_IRQ; + + /* This is taken to safely inject irq

inside

Quoted 6 lines

arch/x86/kvm/x86.c struct kvm_stats_debugfs_item debugfs_entries[] = {

Quoted 6 lines

the guest. When + * the interrupt injection (or the ioapic code)
uses a + * finer-grained lock, update this

arch/x86/kvm/x86.c +4 -0
+	 */
+	mutex_lock(&int_work->kvm->lock);
+	read_lock_irqsave(&kvm_pci_pt_lock, flags);
+	match =

kvm_find_pci_pt_dev(&int_work->kvm->arch.pci_pt_dev_head,

Quoted 3 lines

Review

Quoted 3 lines

NULL, + int_work->irq, source);

(quoted diff) +2 -0
+	if (!match) {
+		printk(KERN_ERR "%s: no matching device assigned to

guest "

Quoted 3 lines

arch/x86/kvm/x86.c struct kvm_stats_debugfs_item debugfs_entries[] = {

Quoted 3 lines
arch/x86/kvm/x86.c +3 -0
+		       "found for irq %d, source = %d!\n",
+		       __func__, int_work->irq, int_work->source);
+		read_unlock_irqrestore(&kvm_pci_pt_lock, flags); +

goto out;

Quoted 16 lines
Quoted 16 lines
arch/x86/kvm/x86.c +15 -0
+	}
+	guest_irq = match->pt_dev.guest.irq;
+	host_irq = match->pt_dev.host.irq;
+	read_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+
+	if (source == KVM_PT_SOURCE_IRQ)
+		kvm_set_irq(int_work->kvm, guest_irq, 1);
+	else {
+		kvm_set_irq(int_work->kvm, int_work->irq, 0);
+		enable_irq(host_irq);
+	}
+out:
+	mutex_unlock(&int_work->kvm->lock);
+	kvm_put_kvm(int_work->kvm);
+}
Quoted 8 lines
Quoted 4 lines
(quoted diff) +2 -0
+
+/* FIXME: Implement the OR logic needed to make shared interrupts

on + * this line behave properly + */

Isn't this a showstopper? There is no easy way for a user to avoid sharing, especially as we have only three pci irqs at present.

Currently it's not easy to avoid sharing. I think we can support MSI for assgined device to solve sharing problem.

Randy (Weidong)

#23 lore raw

Re: [PATCH 3/8] KVM: Handle device assignment to guests

Quoted 60 lines · Weidong
Quoted 11 lines · Avi Kivity
Quoted 11 lines
arch/x86/kvm/x86.c +10 -0
+static void kvm_pci_pt_work_fn(struct work_struct *work) +{
+	struct kvm_pci_pt_dev_list *match;
+	struct kvm_pci_pt_work *int_work;
+	int source;
+	unsigned long flags;
+	int guest_irq;
+	int host_irq;
+
+	int_work = container_of(work, struct kvm_pci_pt_work, work); +
+	source = int_work->source ? KVM_PT_SOURCE_IRQ_ACK :

KVM_PT_SOURCE_IRQ; + + /* This is taken to safely inject irq

inside

Quoted 6 lines

arch/x86/kvm/x86.c struct kvm_stats_debugfs_item debugfs_entries[] = {

Quoted 6 lines

the guest. When + * the interrupt injection (or the ioapic
code) uses a + * finer-grained lock, update this

arch/x86/kvm/x86.c +4 -0
+	 */
+	mutex_lock(&int_work->kvm->lock);
+	read_lock_irqsave(&kvm_pci_pt_lock, flags);
+	match =

kvm_find_pci_pt_dev(&int_work->kvm->arch.pci_pt_dev_head,

Quoted 3 lines

Review

Quoted 3 lines

NULL, + int_work->irq, source);

(quoted diff) +2 -0
+	if (!match) {
+		printk(KERN_ERR "%s: no matching device assigned to

guest "

Quoted 3 lines

arch/x86/kvm/x86.c struct kvm_stats_debugfs_item debugfs_entries[] = {

Quoted 3 lines
arch/x86/kvm/x86.c +3 -0
+		       "found for irq %d, source = %d!\n",
+		       __func__, int_work->irq, int_work->source);
+		read_unlock_irqrestore(&kvm_pci_pt_lock, flags); +

goto out;

Quoted 16 lines
Quoted 16 lines
arch/x86/kvm/x86.c +15 -0
+	}
+	guest_irq = match->pt_dev.guest.irq;
+	host_irq = match->pt_dev.host.irq;
+	read_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+
+	if (source == KVM_PT_SOURCE_IRQ)
+		kvm_set_irq(int_work->kvm, guest_irq, 1);
+	else {
+		kvm_set_irq(int_work->kvm, int_work->irq, 0);
+		enable_irq(host_irq);
+	}
+out:
+	mutex_unlock(&int_work->kvm->lock);
+	kvm_put_kvm(int_work->kvm);
+}
Quoted 7 lines
Quoted 3 lines
(quoted diff) +2 -0
+
+/* FIXME: Implement the OR logic needed to make shared

interrupts on + * this line behave properly + */

Isn't this a showstopper? There is no easy way for a user to avoid sharing, especially as we have only three pci irqs at present.

Quoted 3 lines

Currently it's not easy to avoid sharing. I think we can support MSI for assgined device to solve sharing problem.

I am working on the MSI support now.

Signature · 2 lines
regards
Yang, Sheng
#40 lore raw
In reply to Anthony Liguori
Quoted 31 lines · Anthony Liguori
Quoted 19 lines · Ben-Ami Yassour
Quoted 6 lines · Avi Kivity
Quoted 4 lines · Ben-Ami Yassour

In last few tests that we made with PCI-passthrough and VT-d using iperf, we were able to get the same throughput as on native OS with a 1G NIC

Quoted 8 lines

Excellent!

Quoted 3 lines

(with higher CPU utilization).

How much higher?

Here are some numbers for running iperf -l 1M:

Quoted 8 lines

e1000 NIC (behind a PCI bridge)
Bandwidth (Mbit/sec) CPU utilization

Native OS                   771                      18%
Native OS with VT-d         760                      18%
KVM VT-d                    390                      95%

KVM VT-d with direct mmio 770 84%
KVM emulated 57 100%

What about virtio? Also, which emulated is this?

Quoted 8 lines

That CPU utilization is extremely high and somewhat illogical if native w/vt-d has almost no CPU impact. Have you run oprofile yet or have any insight into where CPU is being burnt?

What does kvm_stat look like? I wonder if there are a large number of PIO exits. What does the interrupt count look like on native vs. KVM with VT-d?

e1000 NIC doesn't use PIO.

Randy (Weidong)

#24 lore raw

Re: [PATCH 3/8] KVM: Handle device assignment to guests

In reply to Han, Weidong
Quoted 63 lines · Weidong
Quoted 4 lines · Avi Kivity
Quoted 4 lines
arch/x86/kvm/x86.c +4 -0
+static void kvm_pci_pt_work_fn(struct work_struct *work) +{
+	struct kvm_pci_pt_dev_list *match;
+	struct kvm_pci_pt_work *int_work;
+	int source;
Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/x86.c +6 -0
+	unsigned long flags;
+	int guest_irq;
+	int host_irq;
+
+	int_work = container_of(work, struct kvm_pci_pt_work, work); +
+	source = int_work->source ? KVM_PT_SOURCE_IRQ_ACK :

KVM_PT_SOURCE_IRQ; + + /* This is taken to safely inject irq

inside

Quoted 7 lines

arch/x86/kvm/x86.c struct kvm_stats_debugfs_item debugfs_entries[] = {

Quoted 7 lines

the guest. When + * the interrupt injection (or the ioapic code)
uses a + * finer-grained lock, update this

arch/x86/kvm/x86.c +4 -0
+	 */
+	mutex_lock(&int_work->kvm->lock);
+	read_lock_irqsave(&kvm_pci_pt_lock, flags);
+	match =

kvm_find_pci_pt_dev(&int_work->kvm->arch.pci_pt_dev_head,

Quoted 4 lines

Review

Quoted 4 lines

NULL, + int_work->irq, source);

(quoted diff) +2 -0
+	if (!match) {
+		printk(KERN_ERR "%s: no matching device assigned to

guest "

Quoted 4 lines

arch/x86/kvm/x86.c struct kvm_stats_debugfs_item debugfs_entries[] = {

Quoted 4 lines
arch/x86/kvm/x86.c +3 -0
+		       "found for irq %d, source = %d!\n",
+		       __func__, int_work->irq, int_work->source);
+		read_unlock_irqrestore(&kvm_pci_pt_lock, flags); +

goto out;

Quoted 16 lines
Quoted 16 lines
arch/x86/kvm/x86.c +15 -0
+	}
+	guest_irq = match->pt_dev.guest.irq;
+	host_irq = match->pt_dev.host.irq;
+	read_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+
+	if (source == KVM_PT_SOURCE_IRQ)
+		kvm_set_irq(int_work->kvm, guest_irq, 1);
+	else {
+		kvm_set_irq(int_work->kvm, int_work->irq, 0);
+		enable_irq(host_irq);
+	}
+out:
+	mutex_unlock(&int_work->kvm->lock);
+	kvm_put_kvm(int_work->kvm);
+}
Quoted 9 lines
Quoted 5 lines
(quoted diff) +2 -0
+
+/* FIXME: Implement the OR logic needed to make shared interrupts

on + * this line behave properly + */

Isn't this a showstopper? There is no easy way for a user to avoid sharing, especially as we have only three pci irqs at present.

Quoted 3 lines

Currently it's not easy to avoid sharing. I think we can support MSI for assgined device to solve sharing problem.

MSI is definitely the right direction, but we also need to support the OR logic for guests that do not support MSI.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#39 lore raw
In reply to Avi Kivity
Quoted 16 lines · Avi Kivity
Quoted 15 lines · Ben-Ami Yassour
Quoted 14 lines

That CPU utilization is extremely high and somewhat illogical if native w/vt-d has almost no CPU impact. Have you run oprofile yet or have any insight into where CPU is being burnt?

What does kvm_stat look like? I wonder if there are a large number of PIO exits. What does the interrupt count look like on native vs. KVM with VT-d?

Regards,

Anthony Liguori

Quoted 9 lines
Quoted 4 lines

These are all good points and questions, I agree that we need to take a deeper look into the performance issues, but I think that we need to merge with the main KVM tree first.

It would be good to get the host interrupt rate, to confirm that the host isn't flooded with interrupts. A deeper analysis can wait.

Here is an example of a kvm_stat while running iperf:
efer_reload 13 0
exits 11372838 58214

fpu_reload                709524         0
halt_exits                139306         0
halt_wakeup                 3580         0
host_state_reload        5070783     29031
hypercalls                635636         1
insn_emulation           4306311     22289
insn_emulation_fail            0         0
invlpg                         0         0
io_exits                  978192       646
irq_exits                2795848     20879
irq_window                     0         0
largepages                     0         0
mmio_exits                 15437         0
mmu_cache_miss             60299         0
mmu_flooded                43213         0
mmu_pde_zapped             49610         0
mmu_pte_updated           576608         1
mmu_pte_write            1203336         1
mmu_recycled                   0         0
mmu_shadow_zapped          59348         0
nmi_window                     0         0
pf_fixed                  571970         0
pf_guest                  103889         0
remote_tlb_flush               0         0
request_irq                    0         0
signal_exits                   1         0
tlb_flush                 725366         7

The average host interrupt rate is around 22,200 per second.
Note: I would assume that this is bigger then irq_exits, because some of the interrupts occur while we are in host mode so it does not cause an exit.
BTW, the interrupt counter on the host and guest is very close.

Regards,
Ben

#12 lore raw

RE: [PATCH 1/2] KVM/userspace: Support for assigning PCI devices to guest

In reply to Ben-Ami Yassour
Quoted 27 lines · Ben-Ami Yassour

From: Or Sagi <[email protected]>

From: Nir Peleg <[email protected]>
From: Amit Shah <[email protected]>
From: Glauber de Oliveira Costa <[email protected]>

We can assign a device from the host machine to a guest. The original code comes from Neocleus.

A new command-line option, -pcidevice is added. For example, to invoke it for an Ethernet device sitting at PCI bus:dev.fn 04:08.0 with host IRQ 18, use this:

        -pcidevice Ethernet/04:08.0

The host ethernet driver is to be removed before doing the passthrough. If not, the device assignment fails but the guest continues without the assignment.

If kvm uses the in-kernel irqchip, interrupts are routed to the guest via the kvm module (accompanied kernel changes are necessary).

If -no-kvm-irqchip is used, the 'irqhook' module, available separately, is to be used for interrupt injection into the guest. In this case, an extra parameter, -<intr-number> is to be appended to the above-mentioned pcidevice parameter.

Quoted 4 lines

---
+static pt_dev_t *register_real_device(PCIBus *e_bus, const char
*e_dev_name, + int e_devfn,

uint8_t r_bus, uint8_t r_dev,

qemu/hw/pci-passthrough.c

Quoted 11 lines
qemu/hw/pci-passthrough.c new file +11 -0
+				      uint8_t r_func)
+{
+	int rc;
+	pt_dev_t *pci_dev;
+	uint8_t e_device, e_intx;
+
+	DEBUG("register_real_device: Registering real physical "
+	      "device %s (devfn=0x%x)\n", e_dev_name, e_devfn);
+
+	pci_dev = (pt_dev_t *) pci_register_device(e_bus, e_dev_name,
+						   sizeof(pt_dev_t),

e_devfn,

qemu/hw/pci-passthrough.c

Quoted 10 lines
qemu/hw/pci-passthrough.c new file +10 -0
+						   pt_pci_read_config,
+						   pt_pci_write_config);
+
+	if (NULL == pci_dev) {
+		fprintf(stderr, "register_real_device: Error: Couldn't "
+			"register real device %s\n", e_dev_name);
+		return NULL;
+	}
+	if (pt_get_real_device(pci_dev, r_bus, r_dev, r_func)) {
+		fprintf(stderr, "register_real_device: Error: Couldn't

get "

qemu/hw/pci-passthrough.c

Quoted 7 lines
qemu/hw/pci-passthrough.c new file +7 -0
+			"real device (%s)!\n", e_dev_name);
+		return NULL;
+	}
+
+	/* handle real device's MMIO/PIO BARs */
+	if (pt_register_regions(pci_dev->real_device.regions,
+				pci_dev->real_device.region_number,

pci_dev))

Quoted 8 lines
qemu/hw/pci-passthrough.c new file +8 -0
+		return NULL;
+
+	/* handle interrupt routing */
+	e_device = (pci_dev->dev.devfn >> 3) & 0x1f;
+	e_intx = pci_dev->dev.config[0x3d] - 1;
+	pci_dev->intpin = e_intx;
+	pci_dev->run = 0;
+	pci_dev->girq = 0;

qemu/hw/pci-passthrough.c line 448

Quoted 10 lines
qemu/hw/pci-passthrough.c new file +10 -0
+	pci_dev->h_busnr = r_bus;
+	pci_dev->h_devfn = PCI_DEVFN(r_dev, r_func);
+
+#ifdef KVM_CAP_PCI_PASSTHROUGH
+	if (kvm_enabled()) {
+		struct kvm_pci_passthrough_dev pci_pt_dev;
+
+		memset(&pci_pt_dev, 0, sizeof(pci_pt_dev));
+		pci_pt_dev.guest.busnr = pci_bus_num(e_bus);
+		pci_pt_dev.guest.devfn = PCI_DEVFN(e_device, r_func);

Why combine e_device and r_func? The guest devfn is e_devfn.

Quoted 12 lines
qemu/hw/pci-passthrough.c new file +12 -0
+		pci_pt_dev.host.busnr  = pci_dev->h_busnr;
+		pci_pt_dev.host.devfn  = pci_dev->h_devfn;
+
+		/* We'll set the value of the guest irq as and when
+		 * the piix config gets updated. See pci_pt_update_irq.
+		 * The host irq field never gets used anyway
+		 */
+
+		rc = kvm_update_pci_pt_device(kvm_context, &pci_pt_dev);
+		if (rc < 0) {
+			fprintf(stderr, "Could not notify kernel about "
+				"passthrough device\n");

qemu/hw/pci-passthrough.c line 470

Quoted 10 lines
qemu/hw/pci-passthrough.c new file +10 -0
+			perror("pt-ioctl");
+			return NULL;
+		}
+	}
+#endif
+
+	fprintf(logfile, "Registered host PCI device %02x:%02x.%1x "
+		"as guest device %02x:%02x.%1x\n",
+		r_bus, r_dev, r_func,
+		pci_bus_num(e_bus), e_device, r_func);

Second "r_func" should replaced by e_devfn & 0x7.

Randy (Weidong)

#25 lore raw

Re: [PATCH 3/8] KVM: Handle device assignment to guests

In reply to Avi Kivity
Quoted 54 lines · Avi Kivity
Quoted 53 lines · Weidong
Quoted 4 lines · Avi Kivity
Quoted 4 lines
arch/x86/kvm/x86.c +4 -0
+static void kvm_pci_pt_work_fn(struct work_struct *work) +{
+	struct kvm_pci_pt_dev_list *match;
+	struct kvm_pci_pt_work *int_work;
+	int source;
Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/x86.c +6 -0
+	unsigned long flags;
+	int guest_irq;
+	int host_irq;
+
+	int_work = container_of(work, struct kvm_pci_pt_work, work); +
+	source = int_work->source ? KVM_PT_SOURCE_IRQ_ACK :

KVM_PT_SOURCE_IRQ; + + /* This is taken to safely inject irq

inside

Quoted 7 lines

arch/x86/kvm/x86.c struct kvm_stats_debugfs_item debugfs_entries[] = {

Quoted 7 lines

the guest. When + * the interrupt injection (or the ioapic code)
uses a + * finer-grained lock, update this

arch/x86/kvm/x86.c +4 -0
+	 */
+	mutex_lock(&int_work->kvm->lock);
+	read_lock_irqsave(&kvm_pci_pt_lock, flags);
+	match =

kvm_find_pci_pt_dev(&int_work->kvm->arch.pci_pt_dev_head,

Quoted 4 lines

Review

Quoted 4 lines

NULL, + int_work->irq, source);

(quoted diff) +2 -0
+	if (!match) {
+		printk(KERN_ERR "%s: no matching device assigned to

guest "

Quoted 4 lines

arch/x86/kvm/x86.c struct kvm_stats_debugfs_item debugfs_entries[] = {

Quoted 4 lines
arch/x86/kvm/x86.c +3 -0
+		       "found for irq %d, source = %d!\n",
+		       __func__, int_work->irq, int_work->source);
+		read_unlock_irqrestore(&kvm_pci_pt_lock, flags); +

goto out;

Quoted 16 lines
Quoted 16 lines
arch/x86/kvm/x86.c +15 -0
+	}
+	guest_irq = match->pt_dev.guest.irq;
+	host_irq = match->pt_dev.host.irq;
+	read_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+
+	if (source == KVM_PT_SOURCE_IRQ)
+		kvm_set_irq(int_work->kvm, guest_irq, 1);
+	else {
+		kvm_set_irq(int_work->kvm, int_work->irq, 0);
+		enable_irq(host_irq);
+	}
+out:
+	mutex_unlock(&int_work->kvm->lock);
+	kvm_put_kvm(int_work->kvm);
+}
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
(quoted diff) +2 -0
+
+/* FIXME: Implement the OR logic needed to make shared interrupts

on + * this line behave properly + */

Isn't this a showstopper? There is no easy way for a user to avoid sharing, especially as we have only three pci irqs at present.

What do you mean by only 3 (for passthrough we use whatever interrupt the host is using for that device)?

Quoted 11 lines
Quoted 6 lines
Quoted 2 lines

Currently it's not easy to avoid sharing. I think we can support MSI for assgined device to solve sharing problem.

MSI is definitely the right direction, but we also need to support the
OR logic for guests that do not support MSI.

The main problems that I am ware of with shared interrupt are as follows:

1. Protection
A bad guest can keep the interrupt line up, and if its shared then it interferes with other devices that might not be assigned to this guest.

2. Performance
The current implementation disables the irq when the interrupt is received on the host and re-enables it when the guest acks the interrupt on the virtual ioapic.

Clearly its not nice to do that when other devices uses the same irq...

When removing the enable-disable of the irq, it causes serious performance degradation.
The throughput goes down from 700 to 400 Mb/sec.
The number of interrupts per second on the host (from the NIC) is 130000 (compared to 22200 before).

Regardless, I think that the right way to go would be to merge the current code (with the non-shared interrupt limitation) and then continue to work on additional features like shared interrupts support. Do you agree?

Regards,
Ben

#26 lore raw

Re: [PATCH 3/8] KVM: Handle device assignment to guests

Quoted 9 lines · Ben-Ami Yassour
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 6 lines
(quoted diff) +2 -0
+
+/* FIXME: Implement the OR logic needed to make shared interrupts

on + * this line behave properly + */

Isn't this a showstopper? There is no easy way for a user to avoid sharing, especially as we have only three pci irqs at present.

Quoted 4 lines

What do you mean by only 3 (for passthrough we use whatever interrupt the host is using for that device)?

There are two issue:
- shared host interrupts
- shared guest interrupts

For shared host interrupts, I don't think there's a cost-effective solution, especially as hosts are transitioning to MSI. But the problem the comment describes is shared guest interrupts, where the assigned device's interrupt is shared with another device (assigned or virtual).

And currently we only have three shareable interrupts: 5, 10, and 11.

See qemu's pci_set_irq() for the logic used to share interrupts.

Quoted 23 lines
Quoted 10 lines
Quoted 6 lines
Quoted 2 lines

Currently it's not easy to avoid sharing. I think we can support MSI for assgined device to solve sharing problem.

MSI is definitely the right direction, but we also need to support the OR logic for guests that do not support MSI.

The main problems that I am ware of with shared interrupt are as follows:

1. Protection
A bad guest can keep the interrupt line up, and if its shared then it interferes with other devices that might not be assigned to this guest.

2. Performance
The current implementation disables the irq when the interrupt is received on the host and re-enables it when the guest acks the interrupt on the virtual ioapic.

Quoted 9 lines

Clearly its not nice to do that when other devices uses the same irq...

When removing the enable-disable of the irq, it causes serious performance degradation.
The throughput goes down from 700 to 400 Mb/sec.
The number of interrupts per second on the host (from the NIC) is 130000 (compared to 22200 before).

These are all shared host interrupt problems.

Quoted 4 lines

Regardless, I think that the right way to go would be to merge the current code (with the non-shared interrupt limitation) and then continue to work on additional features like shared interrupts support. Do you agree?

The current code will be incredibly easy to break; all you need is more than three pci devices. I think the sharing code can be easily added (as a separate patch); all that's needed is to make sure the API is good.

Signature · 1 line
error compiling committee.c: too many arguments to function
#29 lore raw

Re: [PATCH 3/8] KVM: Handle device assignment to guests

In reply to Avi Kivity
Quoted 14 lines · Avi Kivity

include/asm-x86/kvm_para.h static inline unsigned int kvm_arch_para_features(void) · line 154

Quoted 9 lines · Ben-Ami Yassour
include/asm-x86/kvm_para.h +8 -0
+/* Stores information for identifying host PCI devices assigned to the
+ * guest: this is used in the host kernel and in the userspace.
+ */
+struct kvm_pci_pt_info {
+	unsigned char busnr;
+	unsigned int devfn;
+	__u32 irq;
+};

Badly aligned. Please use __u32 for all fields, and add a __u32 padding field at the end so we have the same ABI for i386 and x86_64.

Quoted 5 lines

Some pci devices support more than one irq. Should we make irq an array? Alternatively, decouple irq assignment from pci information and
let userspace handle everything. This lets us assign non-pci devices (like serial ports, etc.).

Can we limit the first version to single irq devices, and handle this after the merge?

Quoted 15 lines
Quoted 15 lines

* ioctls for vcpu fds

virt/kvm/ioapic.c +2 -0
index 8ce93c7..0b5bc40 100644
@@ -288,13 +288,22 @@ void kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
 static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int gsi)
 {
 	union ioapic_redir_entry *ent;
+	struct kvm_pci_pt_dev_list *match;
+	unsigned long flags;
 
 	ent = &ioapic->redirtbl[gsi];
 	ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
 
Quoted 17 lines

virt/kvm/ioapic.c void kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level) · line 307

Quoted 13 lines
virt/kvm/ioapic.c +9 -2
 	ent->fields.remote_irr = 0;
-	if (!ent->fields.mask && (ioapic->irr & (1 << gsi)))
-		ioapic_service(ioapic, gsi);
+
+	read_lock_irqsave(&kvm_pci_pt_lock, flags);
+	match = kvm_find_pci_pt_dev(&ioapic->kvm->arch.pci_pt_dev_head, NULL,
+				    gsi, KVM_PT_SOURCE_IRQ_ACK);
+	read_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+	if (!match) {
+		if (!ent->fields.mask && (ioapic->irr & (1 << gsi)))
+			ioapic_service(ioapic, gsi);
+	}
 

What's device assignment code doing in the ioapic? This should be done through the notifier (if it needs to return a value, great).

This purpose of this code is to avoid the call to ioapic_service for assigned devices, because it is not required and causes a double injection of the interrupt.

Can you please explain why as part of eoi we inject a new interrupt?

Thanks,
Ben

#30 lore raw

Re: [PATCH 3/8] KVM: Handle device assignment to guests

Quoted 16 lines · Ben-Ami Yassour
Quoted 14 lines · Avi Kivity

include/asm-x86/kvm_para.h static inline unsigned int kvm_arch_para_features(void) · line 154

Quoted 10 lines · Ben-Ami Yassour
include/asm-x86/kvm_para.h +8 -0
+/* Stores information for identifying host PCI devices assigned to the
+ * guest: this is used in the host kernel and in the userspace.
+ */
+struct kvm_pci_pt_info {
+	unsigned char busnr;
+	unsigned int devfn;
+	__u32 irq;
+};

Badly aligned. Please use __u32 for all fields, and add a __u32 padding field at the end so we have the same ABI for i386 and x86_64.

Quoted 7 lines
Quoted 3 lines

Some pci devices support more than one irq. Should we make irq an array? Alternatively, decouple irq assignment from pci information and let userspace handle everything. This lets us assign non-pci devices (like serial ports, etc.).

Can we limit the first version to single irq devices, and handle this after the merge?

Yes. But we need to get at least the userspace interface right (and perhaps only partially implement it). Breaking the userspace interface later is possible, but I'd rather avoid it.

Quoted 33 lines
Quoted 15 lines
Quoted 15 lines

* ioctls for vcpu fds

virt/kvm/ioapic.c +2 -0
index 8ce93c7..0b5bc40 100644
@@ -288,13 +288,22 @@ void kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
 static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int gsi)
 {
 	union ioapic_redir_entry *ent;
+	struct kvm_pci_pt_dev_list *match;
+	unsigned long flags;
 
 	ent = &ioapic->redirtbl[gsi];
 	ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
 
Quoted 17 lines
Quoted 4 lines
virt/kvm/ioapic.c +1 -2
 	ent->fields.remote_irr = 0;
-	if (!ent->fields.mask && (ioapic->irr & (1 << gsi)))
-		ioapic_service(ioapic, gsi);
+

virt/kvm/ioapic.c void kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)

Quoted 10 lines
virt/kvm/ioapic.c +8 -0
+	read_lock_irqsave(&kvm_pci_pt_lock, flags);
+	match = kvm_find_pci_pt_dev(&ioapic->kvm->arch.pci_pt_dev_head, NULL,
+				    gsi, KVM_PT_SOURCE_IRQ_ACK);
+	read_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+	if (!match) {
+		if (!ent->fields.mask && (ioapic->irr & (1 << gsi)))
+			ioapic_service(ioapic, gsi);
+	}
 
 

What's device assignment code doing in the ioapic? This should be done through the notifier (if it needs to return a value, great).

Quoted 6 lines

This purpose of this code is to avoid the call to ioapic_service for assigned devices, because it is not required and causes a double injection of the interrupt.

Can you please explain why as part of eoi we inject a new interrupt?

If a level triggered interrupt remains active after the eoi, the ioapic has to inject it. This is used to support shared interrupts, or when the device has re-raised the line by the time the ack arrives.

I don't see why it should behave differently for assigned devices.

Signature · 1 line
error compiling committee.c: too many arguments to function
#31 lore raw

Re: [PATCH 3/8] KVM: Handle device assignment to guests

Quoted 10 lines · Avi Kivity
Quoted 2 lines · Ben-Ami Yassour

On Wed, 2008-07-16 at 18:04 +0300, Avi Kivity wrote:

If a level triggered interrupt remains active after the eoi, the ioapic has to inject it. This is used to support shared interrupts, or when the device has re-raised the line by the time the ack arrives.

I don't see why it should behave differently for assigned devices.

The difference is that for emulated devices, qemu is resetting the irr bit. For assigned devices it does not, and that's the difference. The first chance that we can clear the irr bit for real devices is the eoi function, and actually this is what the ack notify handler is doing (by calling pci_set_irq(kvm,irq,0) ).
I was able to remove the code in ioapic by calling the ack notify handler before the irr check, and it seems to work fine.
(to make it work, I also had to remove the queuing of the ack handler which was not necessary, as you mentioned in earlier comment)

The eoi function now looks like this:
static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int gsi)

{
	union ioapic_redir_entry *ent;

	ent = &ioapic->redirtbl[gsi];
	ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);

	ent->fields.remote_irr = 0;

	if (ioapic->ack_notifier)
		ioapic->ack_notifier(ioapic->kvm, gsi);

	if (!ent->fields.mask && (ioapic->irr & (1 << gsi)))
		ioapic_service(ioapic, gsi);
}

Any comments on such an approach?

Thanks,
Ben

#27 lore raw

Re: [PATCH 3/8] KVM: Handle device assignment to guests

In reply to Avi Kivity
Quoted 19 lines · Avi Kivity
Quoted 9 lines · Ben-Ami Yassour
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 6 lines
(quoted diff) +2 -0
+
+/* FIXME: Implement the OR logic needed to make shared interrupts

on + * this line behave properly + */

Isn't this a showstopper? There is no easy way for a user to avoid sharing, especially as we have only three pci irqs at present.

Quoted 4 lines

What do you mean by only 3 (for passthrough we use whatever interrupt the host is using for that device)?

There are two issue:
- shared host interrupts
- shared guest interrupts

Quoted 8 lines

For shared host interrupts, I don't think there's a cost-effective solution, especially as hosts are transitioning to MSI. But the problem the comment describes is shared guest interrupts, where the assigned device's interrupt is shared with another device (assigned or virtual).

And currently we only have three shareable interrupts: 5, 10, and 11.

See qemu's pci_set_irq() for the logic used to share interrupts.

I think we'll leave this issue until we fix the more basic comments. In any case, how can we make qemu configure the assigned device to share an interrupt with another device (assigned or virtual)?

Thanks,
Ben

#32 lore raw

Re: [PATCH 3/8] KVM: Handle device assignment to guests

In reply to Ben-Ami Yassour
Quoted 13 lines · Ben-Ami Yassour
Quoted 10 lines · Avi Kivity
Quoted 3 lines · Ben-Ami Yassour

On Wed, 2008-07-16 at 18:04 +0300, Avi Kivity wrote:

If a level triggered interrupt remains active after the eoi, the ioapic has to inject it. This is used to support shared interrupts, or when the device has re-raised the line by the time the ack arrives.

I don't see why it should behave differently for assigned devices.

Quoted 8 lines

The difference is that for emulated devices, qemu is resetting the irr bit. For assigned devices it does not, and that's the difference. The first chance that we can clear the irr bit for real devices is the eoi function, and actually this is what the ack notify handler is doing (by calling pci_set_irq(kvm,irq,0) ).
I was able to remove the code in ioapic by calling the ack notify handler before the irr check, and it seems to work fine.

Okay.

Quoted 20 lines

(to make it work, I also had to remove the queuing of the ack handler which was not necessary, as you mentioned in earlier comment)

The eoi function now looks like this:
static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int gsi)

{
	union ioapic_redir_entry *ent;

	ent = &ioapic->redirtbl[gsi];
	ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);

	ent->fields.remote_irr = 0;

	if (ioapic->ack_notifier)
		ioapic->ack_notifier(ioapic->kvm, gsi);

	if (!ent->fields.mask && (ioapic->irr & (1 << gsi)))
		ioapic_service(ioapic, gsi);
}
Quoted 3 lines

Any comments on such an approach?

I think it's fine. The point where the ack notifier is called is between the end of service of the old interrupt, and the beginning of service of a potential new interrupt (from the same device or some other device on the same guest line).

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#28 lore raw

Re: [PATCH 3/8] KVM: Handle device assignment to guests

In reply to Ben-Ami Yassour
Quoted 26 lines · Ben-Ami Yassour
Quoted 24 lines · Avi Kivity
Quoted 11 lines · Ben-Ami Yassour
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 7 lines
(quoted diff) +2 -0
+
+/* FIXME: Implement the OR logic needed to make shared interrupts

on + * this line behave properly + */

Isn't this a showstopper? There is no easy way for a user to avoid sharing, especially as we have only three pci irqs at present.

Quoted 5 lines

What do you mean by only 3 (for passthrough we use whatever interrupt the host is using for that device)?

There are two issue:
- shared host interrupts
- shared guest interrupts

For shared host interrupts, I don't think there's a cost-effective solution, especially as hosts are transitioning to MSI. But the problem the comment describes is shared guest interrupts, where the assigned device's interrupt is shared with another device (assigned or virtual).

Quoted 10 lines
Quoted 4 lines

And currently we only have three shareable interrupts: 5, 10, and 11.

See qemu's pci_set_irq() for the logic used to share interrupts.

I think we'll leave this issue until we fix the more basic comments. In any case, how can we make qemu configure the assigned device to share an interrupt with another device (assigned or virtual)?

It's the guest's responsibility to assign irq lines, when running in acpi mode. In no-acpi mode, the bios assigns lines according to an algorithm in bios/rombios32.c:pci_bios_init_device().

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#13 lore raw

RE: [PATCH 1/2] KVM/userspace: Support for assigning PCI devices to guest

In reply to Han, Weidong
Quoted 76 lines · Weidong
Quoted 27 lines · Ben-Ami Yassour

From: Or Sagi <[email protected]>

From: Nir Peleg <[email protected]>
From: Amit Shah <[email protected]>
From: Glauber de Oliveira Costa <[email protected]>

We can assign a device from the host machine to a guest. The original code comes from Neocleus.

A new command-line option, -pcidevice is added. For example, to invoke it for an Ethernet device sitting at PCI bus:dev.fn 04:08.0 with host IRQ 18, use this:

        -pcidevice Ethernet/04:08.0

The host ethernet driver is to be removed before doing the passthrough. If not, the device assignment fails but the guest continues without the assignment.

If kvm uses the in-kernel irqchip, interrupts are routed to the guest via the kvm module (accompanied kernel changes are necessary).

If -no-kvm-irqchip is used, the 'irqhook' module, available separately, is to be used for interrupt injection into the guest. In this case, an extra parameter, -<intr-number> is to be appended to the above-mentioned pcidevice parameter.

Quoted 4 lines

---
+static pt_dev_t *register_real_device(PCIBus *e_bus, const char
*e_dev_name, + int e_devfn,

uint8_t r_bus, uint8_t r_dev,

qemu/hw/pci-passthrough.c

Quoted 11 lines
qemu/hw/pci-passthrough.c new file +11 -0
+				      uint8_t r_func)
+{
+	int rc;
+	pt_dev_t *pci_dev;
+	uint8_t e_device, e_intx;
+
+	DEBUG("register_real_device: Registering real physical "
+	      "device %s (devfn=0x%x)\n", e_dev_name, e_devfn);
+
+	pci_dev = (pt_dev_t *) pci_register_device(e_bus, e_dev_name,
+						   sizeof(pt_dev_t),

e_devfn,

qemu/hw/pci-passthrough.c

Quoted 10 lines
qemu/hw/pci-passthrough.c new file +10 -0
+						   pt_pci_read_config,
+						   pt_pci_write_config);
+
+	if (NULL == pci_dev) {
+		fprintf(stderr, "register_real_device: Error: Couldn't "
+			"register real device %s\n", e_dev_name);
+		return NULL;
+	}
+	if (pt_get_real_device(pci_dev, r_bus, r_dev, r_func)) {
+		fprintf(stderr, "register_real_device: Error: Couldn't

get "

qemu/hw/pci-passthrough.c

Quoted 7 lines
qemu/hw/pci-passthrough.c new file +7 -0
+			"real device (%s)!\n", e_dev_name);
+		return NULL;
+	}
+
+	/* handle real device's MMIO/PIO BARs */
+	if (pt_register_regions(pci_dev->real_device.regions,
+				pci_dev->real_device.region_number,

pci_dev))

Quoted 12 lines
qemu/hw/pci-passthrough.c new file +12 -0
+		return NULL;
+
+	/* handle interrupt routing */
+	e_device = (pci_dev->dev.devfn >> 3) & 0x1f;
+	e_intx = pci_dev->dev.config[0x3d] - 1;
+	pci_dev->intpin = e_intx;
+	pci_dev->run = 0;
+	pci_dev->girq = 0;
+	pci_dev->h_busnr = r_bus;
+	pci_dev->h_devfn = PCI_DEVFN(r_dev, r_func);
+
+#ifdef KVM_CAP_PCI_PASSTHROUGH
Quoted 8 lines

qemu/hw/pci-passthrough.c line 448

Quoted 6 lines
qemu/hw/pci-passthrough.c new file +6 -0
+	if (kvm_enabled()) {
+		struct kvm_pci_passthrough_dev pci_pt_dev;
+
+		memset(&pci_pt_dev, 0, sizeof(pci_pt_dev));
+		pci_pt_dev.guest.busnr = pci_bus_num(e_bus);
+		pci_pt_dev.guest.devfn = PCI_DEVFN(e_device, r_func);

Why combine e_device and r_func? The guest devfn is e_devfn.

the current assumption is that e_func and r_func are identical.

Quoted 17 lines
Quoted 17 lines
qemu/hw/pci-passthrough.c new file +16 -0
+		pci_pt_dev.host.busnr  = pci_dev->h_busnr;
+		pci_pt_dev.host.devfn  = pci_dev->h_devfn;
+
+		/* We'll set the value of the guest irq as and when
+		 * the piix config gets updated. See pci_pt_update_irq.
+		 * The host irq field never gets used anyway
+		 */
+
+		rc = kvm_update_pci_pt_device(kvm_context, &pci_pt_dev);
+		if (rc < 0) {
+			fprintf(stderr, "Could not notify kernel about "
+				"passthrough device\n");
+			perror("pt-ioctl");
+			return NULL;
+		}
+	}
Quoted 8 lines

qemu/hw/pci-passthrough.c line 470

Quoted 6 lines
qemu/hw/pci-passthrough.c new file +6 -0
+#endif
+
+	fprintf(logfile, "Registered host PCI device %02x:%02x.%1x "
+		"as guest device %02x:%02x.%1x\n",
+		r_bus, r_dev, r_func,
+		pci_bus_num(e_bus), e_device, r_func);

Second "r_func" should replaced by e_devfn & 0x7.

same as above.

Quoted 3 lines

Randy (Weidong)

#14 lore raw

RE: [PATCH 1/2] KVM/userspace: Support for assigning PCI devicesto guest

Quoted 45 lines · Ben-Ami Yassour
Quoted 44 lines · Weidong
Quoted 27 lines · Ben-Ami Yassour

From: Or Sagi <[email protected]>

From: Nir Peleg <[email protected]>
From: Amit Shah <[email protected]>
From: Glauber de Oliveira Costa <[email protected]>

We can assign a device from the host machine to a guest. The original code comes from Neocleus.

A new command-line option, -pcidevice is added. For example, to invoke it for an Ethernet device sitting at PCI bus:dev.fn 04:08.0 with host IRQ 18, use this:

        -pcidevice Ethernet/04:08.0

The host ethernet driver is to be removed before doing the passthrough. If not, the device assignment fails but the guest continues without the assignment.

If kvm uses the in-kernel irqchip, interrupts are routed to the guest via the kvm module (accompanied kernel changes are necessary).

If -no-kvm-irqchip is used, the 'irqhook' module, available separately, is to be used for interrupt injection into the guest. In this case, an extra parameter, -<intr-number> is to be appended to the above-mentioned pcidevice parameter.

Quoted 4 lines

---
+static pt_dev_t *register_real_device(PCIBus *e_bus, const char
*e_dev_name, + int e_devfn,

uint8_t r_bus, uint8_t r_dev,

qemu/hw/pci-passthrough.c

Quoted 10 lines
qemu/hw/pci-passthrough.c new file +10 -0
+				      uint8_t r_func)
+{
+	int rc;
+	pt_dev_t *pci_dev;
+	uint8_t e_device, e_intx;
+
+	DEBUG("register_real_device: Registering real physical "
+	      "device %s (devfn=0x%x)\n", e_dev_name, e_devfn); +
+	pci_dev = (pt_dev_t *) pci_register_device(e_bus, e_dev_name,
+						   sizeof(pt_dev_t),

e_devfn,

Quoted 10 lines
Quoted 10 lines

qemu/hw/pci-passthrough.c

Quoted 10 lines
qemu/hw/pci-passthrough.c new file +10 -0
+						   pt_pci_read_config,
+						   pt_pci_write_config);
+
+	if (NULL == pci_dev) {
+		fprintf(stderr, "register_real_device: Error: Couldn't "
+			"register real device %s\n", e_dev_name);
+		return NULL;
+	}
+	if (pt_get_real_device(pci_dev, r_bus, r_dev, r_func)) {
+		fprintf(stderr, "register_real_device: Error: Couldn't

get "

Quoted 7 lines
Quoted 7 lines

qemu/hw/pci-passthrough.c

Quoted 7 lines
qemu/hw/pci-passthrough.c new file +7 -0
+			"real device (%s)!\n", e_dev_name);
+		return NULL;
+	}
+
+	/* handle real device's MMIO/PIO BARs */
+	if (pt_register_regions(pci_dev->real_device.regions,
+				pci_dev->real_device.region_number,

pci_dev))

Quoted 20 lines
Quoted 12 lines
Quoted 12 lines
qemu/hw/pci-passthrough.c new file +12 -0
+		return NULL;
+
+	/* handle interrupt routing */
+	e_device = (pci_dev->dev.devfn >> 3) & 0x1f;
+	e_intx = pci_dev->dev.config[0x3d] - 1;
+	pci_dev->intpin = e_intx;
+	pci_dev->run = 0;
+	pci_dev->girq = 0;
+	pci_dev->h_busnr = r_bus;
+	pci_dev->h_devfn = PCI_DEVFN(r_dev, r_func);
+
+#ifdef KVM_CAP_PCI_PASSTHROUGH
Quoted 8 lines

qemu/hw/pci-passthrough.c line 448

Quoted 6 lines
qemu/hw/pci-passthrough.c new file +6 -0
+	if (kvm_enabled()) {
+		struct kvm_pci_passthrough_dev pci_pt_dev;
+
+		memset(&pci_pt_dev, 0, sizeof(pci_pt_dev));
+		pci_pt_dev.guest.busnr = pci_bus_num(e_bus);
+		pci_pt_dev.guest.devfn = PCI_DEVFN(e_device, r_func);

Why combine e_device and r_func? The guest devfn is e_devfn.

Quoted 1 line

the current assumption is that e_func and r_func are identical.

But you can find they are not the same in guest. Why do we need such assumption?

Randy (Weidong)

Quoted 11 lines
Quoted 11 lines

qemu/hw/pci-passthrough.c line 457

Quoted 11 lines
qemu/hw/pci-passthrough.c new file +9 -0
+		pci_pt_dev.host.busnr  = pci_dev->h_busnr;
+		pci_pt_dev.host.devfn  = pci_dev->h_devfn;
+
+		/* We'll set the value of the guest irq as and when
+		 * the piix config gets updated. See pci_pt_update_irq.
+		 * The host irq field never gets used anyway
+		 */
+
+		rc = kvm_update_pci_pt_device(kvm_context, &pci_pt_dev);

+ if

Quoted 1 line
Quoted 1 line
Quoted 1 line

(rc < 0) { + fprintf(stderr, "Could not

notify kernel about "

Quoted 1 line
Quoted 1 line

Review

Quoted 1 line
(quoted diff) +1 -0
+				"passthrough device\n"); +

perror("pt-ioctl");

Quoted 14 lines
Quoted 14 lines

qemu/hw/pci-passthrough.c line 470

Quoted 9 lines
qemu/hw/pci-passthrough.c new file +9 -0
+			return NULL;
+		}
+	}
+#endif
+
+	fprintf(logfile, "Registered host PCI device %02x:%02x.%1x "
+		"as guest device %02x:%02x.%1x\n",
+		r_bus, r_dev, r_func,
+		pci_bus_num(e_bus), e_device, r_func);

Second "r_func" should replaced by e_devfn & 0x7. same as above.

Randy (Weidong)

#17 lore raw

Re: [PATCH 4/8] KVM: PCIPT: fix interrupt handling

In reply to Ben-Ami Yassour
Quoted 20 lines · * On Wednesday 16 Jul 2008 18:47:01 Ben-Ami Yassour wrote:

This patch fixes a few problems with the interrupt handling for passthrough devices.

1. Pass the interrupt handler the pointer to the device, so we do not need to lock the pcipt lock in the interrupt handler.

2. Remove the pt_irq_handled bitmap - it is no longer needed.

3. Split kvm_pci_pt_work_fn into two functions, one for interrupt injection and another for the ack - is much simpler code this way.

4. Change the passthrough initialization order - add the device structure to the list, before registering the interrupt handler.

5. On passthrough destruction path, free the interrupt handler before cleaning queued work.

---

arch/x86/kvm/x86.c static int kvm_vm_ioctl_pci_pt_dev(struct kvm *kvm, · line 290

Quoted 8 lines
arch/x86/kvm/x86.c +7 -0
 	if (irqchip_in_kernel(kvm)) {
+		match->pt_dev.guest.irq = pci_pt_dev->guest.irq;
+		match->pt_dev.host.irq = dev->irq;
+		if (kvm->arch.vioapic)
+			kvm->arch.vioapic->ack_notifier = kvm_pci_pt_ack_irq;
+		if (kvm->arch.vpic)
+			kvm->arch.vpic->ack_notifier = kvm_pci_pt_ack_irq;
+

We shouldn't register this notifier unless we get the irq below to avoid unneeded function calls and checks.

#18 lore raw

Re: [PATCH 4/8] KVM: PCIPT: fix interrupt handling

Quoted 21 lines · Amit Shah
Quoted 19 lines · * On Wednesday 16 Jul 2008 18:47:01 Ben-Ami Yassour wrote:

This patch fixes a few problems with the interrupt handling for passthrough devices.

1. Pass the interrupt handler the pointer to the device, so we do not need to lock the pcipt lock in the interrupt handler.

2. Remove the pt_irq_handled bitmap - it is no longer needed.

3. Split kvm_pci_pt_work_fn into two functions, one for interrupt injection and another for the ack - is much simpler code this way.

4. Change the passthrough initialization order - add the device structure to the list, before registering the interrupt handler.

5. On passthrough destruction path, free the interrupt handler before cleaning queued work.

---

Quoted 11 lines

arch/x86/kvm/x86.c static int kvm_vm_ioctl_pci_pt_dev(struct kvm *kvm, · line 290

Quoted 8 lines
arch/x86/kvm/x86.c +7 -0
 	if (irqchip_in_kernel(kvm)) {
+		match->pt_dev.guest.irq = pci_pt_dev->guest.irq;
+		match->pt_dev.host.irq = dev->irq;
+		if (kvm->arch.vioapic)
+			kvm->arch.vioapic->ack_notifier = kvm_pci_pt_ack_irq;
+		if (kvm->arch.vpic)
+			kvm->arch.vpic->ack_notifier = kvm_pci_pt_ack_irq;
+

We shouldn't register this notifier unless we get the irq below to avoid unneeded function calls and checks.

Note: This code was changed in the last version of the code but the comment is still relevant.

Do you mean that we need to postpone registering the notification? In the case of an assigned device this is means the we postpone it for a few seconds, and implementing it like above it cleaner. So I don't see the real value in postponing it.

Thanks,
Ben

#19 lore raw

Re: [PATCH 4/8] KVM: PCIPT: fix interrupt handling

Quoted 34 lines · * On Thursday 24 Jul 2008 16:58:57 Ben-Ami Yassour wrote:
Quoted 21 lines · Amit Shah
Quoted 20 lines · * On Wednesday 16 Jul 2008 18:47:01 Ben-Ami Yassour wrote:

This patch fixes a few problems with the interrupt handling for passthrough devices.

1. Pass the interrupt handler the pointer to the device, so we do not need to lock the pcipt lock in the interrupt handler.

2. Remove the pt_irq_handled bitmap - it is no longer needed.

3. Split kvm_pci_pt_work_fn into two functions, one for interrupt injection and another for the ack - is much simpler code this way.

4. Change the passthrough initialization order - add the device structure to the list, before registering the interrupt handler.

5. On passthrough destruction path, free the interrupt handler before cleaning queued work.

---

Quoted 11 lines

arch/x86/kvm/x86.c static int kvm_vm_ioctl_pci_pt_dev(struct kvm *kvm, · line 290

Quoted 8 lines
arch/x86/kvm/x86.c +7 -0
 	if (irqchip_in_kernel(kvm)) {
+		match->pt_dev.guest.irq = pci_pt_dev->guest.irq;
+		match->pt_dev.host.irq = dev->irq;
+		if (kvm->arch.vioapic)
+			kvm->arch.vioapic->ack_notifier = kvm_pci_pt_ack_irq;
+		if (kvm->arch.vpic)
+			kvm->arch.vpic->ack_notifier = kvm_pci_pt_ack_irq;
+

We shouldn't register this notifier unless we get the irq below to avoid unneeded function calls and checks.

Quoted 4 lines

Note: This code was changed in the last version of the code but the comment is still relevant.

Do you mean that we need to postpone registering the notification?

I mean we can register these function pointers after the request_irq succeeds.

Quoted 3 lines

In the case of an assigned device this is means the we postpone it for a few seconds, and implementing it like above it cleaner. So I don't see the real value in postponing it.

Sorry, don't get what you mean here.

#20 lore raw

Re: [PATCH 4/8] KVM: PCIPT: fix interrupt handling

Quoted 16 lines · Amit Shah
Quoted 15 lines · * On Thursday 24 Jul 2008 16:58:57 Ben-Ami Yassour wrote:
Quoted 13 lines · Amit Shah

arch/x86/kvm/x86.c static int kvm_vm_ioctl_pci_pt_dev(struct kvm *kvm, · line 290

Quoted 9 lines · * On Wednesday 16 Jul 2008 18:47:01 Ben-Ami Yassour wrote:
arch/x86/kvm/x86.c +7 -0
 	if (irqchip_in_kernel(kvm)) {
+		match->pt_dev.guest.irq = pci_pt_dev->guest.irq;
+		match->pt_dev.host.irq = dev->irq;
+		if (kvm->arch.vioapic)
+			kvm->arch.vioapic->ack_notifier = kvm_pci_pt_ack_irq;
+		if (kvm->arch.vpic)
+			kvm->arch.vpic->ack_notifier = kvm_pci_pt_ack_irq;
+

We shouldn't register this notifier unless we get the irq below to avoid unneeded function calls and checks.

Quoted 6 lines
Quoted 4 lines

Note: This code was changed in the last version of the code but the comment is still relevant.

Do you mean that we need to postpone registering the notification?

I mean we can register these function pointers after the request_irq succeeds.

request_irq should be the last initialization operation, since every thing should be ready when in case an interrupt is received

Quoted 6 lines
Quoted 4 lines

In the case of an assigned device this is means the we postpone it for a few seconds, and implementing it like above it cleaner. So I don't see the real value in postponing it.

Sorry, don't get what you mean here.

never mind... see the answer is above.