Skip to contentSkip to conversation

device assignemnt: updated patches

PATCH In review Ben-Ami Yassour 5 participants 19 messages Jul 22, 2008 – Jul 28, 2008

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

Following are the device assignment patches with the fixes of the comments that were sent for the previous version.

Here is the list of changes that were made with respect to the previous version:
1. Replace the interrupt ack hook patches with the notifiers list patch by Avi.
2. Remove code from ioapic - the ack notifier is called before the checking the irr bit.
3. Remove the interrupt ack work queue and handle it directly.
4. Minimize the function for finding an assigned device.
5. Remove the pt_lock, after making the changes above it is no longer needed.
6. Move declarations from kvm_para.h
7. Add irq array to ioctl API. Note that this is only a change to the API, currently only single irq is supported.
8. Renaming: use "assigned device" and not "passthrough device"
9. Fix device release error path
10. Moving the assigned devices list pointer to the device struct itself and remove the extra structure.

Pending comment: shared guest interrputs are not tested.

Comments are appriciated.

Regards,
Ben

#1 lore raw

PATCH 1/4 KVM: Add irq ack notifier list

From: Avi Kivity <[email protected]>

This can be used by kvm subsystems that are interested in when interrupts
are acked, for example time drift compenstation.

---
 arch/x86/kvm/irq.c         |   22 ++++++++++++++++++++++
 arch/x86/kvm/irq.h         |    5 +++++
 include/asm-x86/kvm_host.h |    7 +++++++
 3 files changed, 34 insertions(+), 0 deletions(-)
arch/x86/kvm/irq.c +22 -0
index 0d9e552..9091195 100644
@@ -111,3 +111,25 @@ void kvm_set_irq(struct kvm *kvm, int irq, int level)
 	kvm_ioapic_set_irq(kvm->arch.vioapic, irq, level);
 	kvm_pic_set_irq(pic_irqchip(kvm), irq, level);
 }
+
+void kvm_notify_acked_irq(struct kvm *kvm, unsigned gsi)
+{
+	struct kvm_irq_ack_notifier *kian;
+	struct hlist_node *n;
+
+	hlist_for_each_entry(kian, n, &kvm->arch.irq_ack_notifier_list, link)
+		if (kian->gsi == gsi)
+			kian->irq_acked(kian);
+}
+
+void kvm_register_irq_ack_notifier(struct kvm *kvm,
+				   struct kvm_irq_ack_notifier *kian)
+{
+	hlist_add_head(&kian->link, &kvm->arch.irq_ack_notifier_list);
+}
+
+void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
+				     struct kvm_irq_ack_notifier *kian)
+{
+	hlist_del(&kian->link);
+}
arch/x86/kvm/irq.h +5 -0
index 07ff2ae..95fe718 100644
@@ -83,6 +83,11 @@ static inline int irqchip_in_kernel(struct kvm *kvm)
 void kvm_pic_reset(struct kvm_kpic_state *s);
 
 void kvm_set_irq(struct kvm *kvm, int irq, int level);
+void kvm_notify_acked_irq(struct kvm *kvm, unsigned gsi);
+void kvm_register_irq_ack_notifier(struct kvm *kvm,
+				   struct kvm_irq_ack_notifier *kian);
+void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
+				     struct kvm_irq_ack_notifier *kian);
 
 void kvm_timer_intr_post(struct kvm_vcpu *vcpu, int vec);
 void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu);
include/asm-x86/kvm_host.h +7 -0
index 4a47859..e2864e6 100644
@@ -319,6 +319,12 @@ struct kvm_mem_alias {
 	gfn_t target_gfn;
 };
 
+struct kvm_irq_ack_notifier {
+	struct hlist_node link;
+	unsigned gsi;
+	void (*irq_acked)(struct kvm_irq_ack_notifier *kian);
+};
+
 struct kvm_arch{
 	int naliases;
 	struct kvm_mem_alias aliases[KVM_ALIAS_SLOTS];
@@ -334,6 +340,7 @@ struct kvm_arch{
 	struct kvm_pic *vpic;
 	struct kvm_ioapic *vioapic;
 	struct kvm_pit *vpit;
+	struct hlist_head irq_ack_notifier_list;
 
 	int round_robin_prev_vcpu;
 	unsigned int tss_addr;
Signature · 1 line
1.5.6
#2 lore raw

PATCH 2/4 KVM: pci device assignment

Based on a patch from: Amit Shah <[email protected]>

This patch adds support for handling PCI devices that are assigned to the guest.

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. The VT-d extension is required to enable the device to perform DMA. Another alternative is PVDMA.

---
 arch/x86/kvm/i8259.c       |    5 +-
 arch/x86/kvm/irq.c         |    2 +-
 arch/x86/kvm/irq.h         |    3 +-
 arch/x86/kvm/x86.c         |  215 ++++++++++++++++++++++++++++++++++++++++++++
 include/asm-x86/kvm.h      |    1 +
 include/asm-x86/kvm_host.h |   20 ++++
 include/asm-x86/kvm_para.h |    1 -
 include/linux/kvm.h        |   21 +++++
 virt/kvm/ioapic.c          |    4 +
 virt/kvm/ioapic.h          |    1 +
 10 files changed, 269 insertions(+), 4 deletions(-)
arch/x86/kvm/i8259.c +4 -1
index 55e179a..d6793f0 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,8 @@ int kvm_pic_read_irq(struct kvm_pic *s)
 		irq = 7;
 		intno = s->pics[0].irq_base + irq;
 	}
+	kvm_notify_acked_irq(kvm, irq);
+
 	pic_update_irq(s);
 
 	return intno;
arch/x86/kvm/irq.c +1 -1
index 9091195..3c508af 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 95fe718..479a3d2 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)
arch/x86/kvm/x86.c +215 -0

257 lines not on this page · raw message

include/asm-x86/kvm.h +1 -0
index 8f13749..12b4b25 100644
@@ -208,4 +208,5 @@ struct kvm_pit_channel_state {
 struct kvm_pit_state {
 	struct kvm_pit_channel_state channels[3];
 };
+
 #endif
include/asm-x86/kvm_host.h +20 -0
index e2864e6..34eb3e7 100644
@@ -325,6 +325,25 @@ struct kvm_irq_ack_notifier {
 	void (*irq_acked)(struct kvm_irq_ack_notifier *kian);
 };
 
+/* 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_assigned_dev_work {
+	struct work_struct work;
+	struct kvm_assigned_dev_kernel *assigned_dev;
+};
+
+struct kvm_assigned_dev_kernel {
+	struct kvm_irq_ack_notifier ack_notifier;
+	struct list_head list;
+	struct kvm_assigned_dev_info guest;
+	struct kvm_assigned_dev_info host;
+	struct kvm_assigned_dev_work int_work;
+	struct pci_dev *dev;
+	struct kvm *kvm;
+};
+
 struct kvm_arch{
 	int naliases;
 	struct kvm_mem_alias aliases[KVM_ALIAS_SLOTS];
@@ -337,6 +356,7 @@ struct kvm_arch{
 	 * Hash table of struct kvm_mmu_page.
 	 */
 	struct list_head active_mmu_pages;
+	struct list_head assigned_dev_head;
 	struct kvm_pic *vpic;
 	struct kvm_ioapic *vioapic;
 	struct kvm_pit *vpit;
include/asm-x86/kvm_para.h +0 -1
index 76f3921..3aa1731 100644
@@ -143,5 +143,4 @@ static inline unsigned int kvm_arch_para_features(void)
 }
 
 #endif
-
 #endif
include/linux/kvm.h +21 -0
index 6edba45..c436c08 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_DEVICE_ASSIGNMENT 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_ASSIGNED_DEVICE _IOR(KVMIO, 0x69,		\
+					struct kvm_assigned_dev)
 
 /*
  * ioctls for vcpu fds
@@ -475,4 +478,22 @@ struct kvm_trace_rec {
 #define KVM_TRC_STLB_INVAL       (KVM_TRC_HANDLER + 0x18)
 #define KVM_TRC_PPC_INSTR        (KVM_TRC_HANDLER + 0x19)
 
+#define ASSIGNED_DEV_MAX_IRQ 16
+
+/* Stores information for identifying host PCI devices assigned to the
+ * guest: this is used in the host kernel and in the userspace.
+ */
+struct kvm_assigned_dev_info {
+	__u32 busnr;
+	__u32 devfn;
+	__u32 irq[ASSIGNED_DEV_MAX_IRQ];
+	__u32 num_valid_irqs; /* currently only 1 is supported */
+};
+
+/* Mapping between host and guest PCI device */
+struct kvm_assigned_dev {
+	struct kvm_assigned_dev_info guest;
+	struct kvm_assigned_dev_info host;
+};
+
 #endif
virt/kvm/ioapic.c +4 -0
index c0d2287..5d68d0b 100644
@@ -39,6 +39,7 @@
 
 #include "ioapic.h"
 #include "lapic.h"
+#include "irq.h"
 
 #if 0
 #define ioapic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg)
@@ -293,6 +294,9 @@ static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int gsi)
 	ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
 
 	ent->fields.remote_irr = 0;
+
+	kvm_notify_acked_irq(ioapic->kvm, gsi);
+
 	if (!ent->fields.mask && (ioapic->irr & (1 << gsi)))
 		ioapic_service(ioapic, gsi);
 }
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
#3 lore raw

PATCH 3/4 VT-d: changes to support KVM

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
#4 lore raw

PATCH 4/4 KVM: Device assignemnt with VT-d

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

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         |  182 ++++++++++++++++++++++++++++++++++++++++++++
 arch/x86/kvm/x86.c         |   11 +++
 include/asm-x86/kvm_host.h |    3 +
 include/linux/kvm_host.h   |    6 ++
 virt/kvm/kvm_main.c        |    8 ++-
 6 files changed, 210 insertions(+), 2 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 +182 -0

182 lines not on this page · raw message

arch/x86/kvm/x86.c +11 -0
index d9aa931..e57c9e4 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>
@@ -253,9 +254,17 @@ kvm_vm_ioctl_device_assignment(struct kvm *kvm,
 	}
 
 
+	if (intel_iommu_found()) {
+		r = kvm_iommu_map_guest(kvm, assigned_dev);
+		if (r)
+			goto out_intr;
+	}
+
 out:
 	mutex_unlock(&kvm->lock);
 	return r;
+out_intr:
+	free_irq(dev->irq, match);
 out_list_del:
 	list_del(&match->list);
 	pci_release_regions(dev);
@@ -4171,6 +4180,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_assigned_devices(kvm);
 	kvm_free_pit(kvm);
 	kfree(kvm->arch.vpic);
include/asm-x86/kvm_host.h +3 -0
index 34eb3e7..75b088b 100644
@@ -357,6 +357,7 @@ struct kvm_arch{
 	 */
 	struct list_head active_mmu_pages;
 	struct list_head assigned_dev_head;
+	struct dmar_domain *intel_iommu_domain;
 	struct kvm_pic *vpic;
 	struct kvm_ioapic *vioapic;
 	struct kvm_pit *vpit;
@@ -506,6 +507,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 {
include/linux/kvm_host.h +6 -0
index 3798097..506b595 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_assigned_dev *assigned_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 +7 -1
index 5b470a1..49c4ed3 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>
@@ -76,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));
@@ -431,6 +432,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
#5 lore raw

device assignment - userspace part

Followed is the userspace patch for device assignment

#6 lore raw

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

Based on a patch from: Amit Shah <[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 assigning the device to a guest. 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).

---
 libkvm/libkvm-x86.c         |    8 +
 libkvm/libkvm.h             |   15 ++
 qemu/Makefile.target        |    1 +
 qemu/hw/device-assignment.c |  575 +++++++++++++++++++++++++++++++++++++++++++
 qemu/hw/device-assignment.h |   95 +++++++
 qemu/hw/isa.h               |    2 +
 qemu/hw/pc.c                |    9 +
 qemu/hw/pci.c               |   12 +
 qemu/hw/pci.h               |    1 +
 qemu/hw/piix_pci.c          |   19 ++
 qemu/vl.c                   |   17 ++
 11 files changed, 754 insertions(+), 0 deletions(-)
 create mode 100644 qemu/hw/device-assignment.c
 create mode 100644 qemu/hw/device-assignment.h
libkvm/libkvm-x86.c +8 -0
index ea97bdd..ea4f0ef 100644
@@ -126,6 +126,14 @@ static int kvm_init_tss(kvm_context_t kvm)
 	return 0;
 }
 
+#ifdef KVM_CAP_DEVICE_ASSIGNMENT
+int kvm_update_assigned_device(kvm_context_t kvm,
+			       struct kvm_assigned_dev *assigned_dev)
+{
+	return ioctl(kvm->vm_fd, KVM_UPDATE_ASSIGNED_DEVICE, assigned_dev);
+}
+#endif
+
 int kvm_arch_create_default_phys_mem(kvm_context_t kvm,
 				       unsigned long phys_mem_bytes,
 				       void **vm_mem)
libkvm/libkvm.h +15 -0
index 9f06fcc..276f6f0 100644
@@ -647,6 +647,21 @@ int kvm_disable_tpr_access_reporting(kvm_context_t kvm, int vcpu);
 
 int kvm_enable_vapic(kvm_context_t kvm, int vcpu, uint64_t vapic);
 
+#ifdef KVM_CAP_DEVICE_ASSIGNMENT
+/*!
+ * \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 assigned_dev Parameters like irq, PCI bus, devfn number, etc
+ */
+int kvm_update_assigned_device(kvm_context_t kvm,
+			       struct kvm_assigned_dev *assigned_dev);
+#endif
 #endif
 
 #if defined(__s390__)
qemu/Makefile.target +1 -0
index 54480e4..94a6393 100644
@@ -602,6 +602,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+= device-assignment.o
 ifeq ($(USE_KVM_PIT), 1)
 OBJS+= i8254-kvm.o
 endif
qemu/hw/device-assignment.c new file +575 -0

575 lines not on this page · raw message

qemu/hw/device-assignment.h new file +95 -0

95 lines not on this page · raw message

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 da60199..71a491d 100644
@@ -32,6 +32,7 @@
 #include "smbus.h"
 #include "boards.h"
 #include "console.h"
+#include "device-assignment.h"
 
 #include "qemu-kvm.h"
 
@@ -994,6 +995,14 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size,
         }
     }
 
+    /* Initialize device assignment */
+    if (pci_enabled) {
+	    int r = -1;
+	    do {
+		    init_assigned_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.c +12 -0
index 92683d1..d45d0ce 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);
+static void assigned_dev_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_DEVICE_ASSIGNMENT
+    if (kvm_enabled() && qemu_kvm_irqchip_in_kernel() &&
+	address >= 0x60 && address <= 0x63)
+	assigned_dev_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..9ba1d8e 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 d9b7db2..04ca724 100644
@@ -37,6 +37,7 @@
 #include "qemu-char.h"
 #include "block.h"
 #include "audio/audio.h"
+#include "hw/device-assignment.h"
 #include "migration.h"
 #include "qemu-kvm.h"
 
@@ -7953,6 +7954,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"
@@ -8076,6 +8082,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,
@@ -8165,6 +8174,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 },
@@ -9047,6 +9059,11 @@ int main(int argc, char **argv)
 		kvm_pit = 0;
 		break;
 	    }
+#if defined(TARGET_I386) || defined(TARGET_X86_64)
+	    case QEMU_OPTION_pcidevice:
+		add_assigned_device(optarg);
+		break;
+#endif
 #endif
             case QEMU_OPTION_usb:
                 usb_enabled = 1;
Signature · 1 line
1.5.6
#7 lore raw

Re: [PATCH 4/4] KVM: Device assignemnt with VT-d

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

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

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         |  182 ++++++++++++++++++++++++++++++++++++++++++++
 arch/x86/kvm/x86.c         |   11 +++
 include/asm-x86/kvm_host.h |    3 +
 include/linux/kvm_host.h   |    6 ++
 virt/kvm/kvm_main.c        |    8 ++-
 6 files changed, 210 insertions(+), 2 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 +36 -0
new file mode 100644
index 0000000..7a3cf4e
@@ -0,0 +1,182 @@
+/*
+ * Copyright (c) 2006, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place - Suite 330, Boston, MA 02111-1307 USA.
+ *
+ * Copyright (C) 2006-2008 Intel Corporation
+ * Author: Allen M. Kay <[email protected]>
+ * Author: Weidong Han <[email protected]>
+ */
+
+#include <linux/list.h>
+#include <linux/kvm_host.h>
+#include <linux/pci.h>
+#include <linux/dmar.h>
+#include <linux/intel-iommu.h>
+
+static int kvm_iommu_unmap_memslots(struct kvm *kvm);
+
+int kvm_iommu_map_pages(struct kvm *kvm,
+			gfn_t base_gfn, unsigned long npages)
+{
+	gfn_t gfn = base_gfn;
+	pfn_t pfn;
+	int i, rc;
+	struct dmar_domain *domain = kvm->arch.intel_iommu_domain;

arch/x86/kvm/vtd.c line 46

Quoted 10 lines
arch/x86/kvm/vtd.c new file +10 -0
+
+	if (!domain)
+		return -EFAULT;
+
+	for (i = 0; i < npages; i++) {
+		pfn = gfn_to_pfn(kvm, gfn);
+		if (!is_mmio_pfn(pfn)) {
+			rc = intel_iommu_page_mapping(domain,
+						      gfn << PAGE_SHIFT,
+						      pfn << PAGE_SHIFT,

This overflows on i386, where gfn and pfn are longs while gpas and hpas are u64s.

gfn_to_gpa() gets the first one right. There should also be a pfn_to_phys(), but there isn't.

arch/x86/kvm/vtd.c line 51

Quoted 6 lines
arch/x86/kvm/vtd.c new file +5 -0
+						      PAGE_SIZE,
+						      DMA_PTE_READ |
+						      DMA_PTE_WRITE);
+			if (rc)
+				kvm_release_pfn_clean(pfn);

You're never actually returning rc, so any errors will be swallowed silently.

arch/x86/kvm/vtd.c line 60

Quoted 10 lines
arch/x86/kvm/vtd.c new file +9 -0
+		} else {
+			printk(KERN_DEBUG "kvm_iommu_map_page:"
+			       "invalid pfn=%lx\n", pfn);
+			return 0;
+		}
+
+		gfn++;
+	}
+	return 0;

Isn't this slow on large guests? (not a merge barrier).

Quoted 9 lines
arch/x86/kvm/vtd.c new file +8 -0
+}
+
+int kvm_iommu_map_guest(struct kvm *kvm,
+			struct kvm_assigned_dev *assigned_dev)
+{
+	struct pci_dev *pdev = NULL;
+
+	printk(KERN_DEBUG "VT-d direct map: host bdf = %x:%x:%x\n",

arch/x86/kvm/vtd.c line 90

Quoted 10 lines
arch/x86/kvm/vtd.c new file +10 -0
+	       assigned_dev->host.busnr,
+	       PCI_SLOT(assigned_dev->host.devfn),
+	       PCI_FUNC(assigned_dev->host.devfn));
+
+	for_each_pci_dev(pdev) {
+		if ((pdev->bus->number == assigned_dev->host.busnr) &&
+		    (pdev->devfn == assigned_dev->host.devfn)) {
+			break;
+		}
+	}

Why not keep pdev in kvm_assigned_dev? We've claimed it, so it's ours.

Quoted 5 lines
arch/x86/kvm/vtd.c new file +4 -0
+
+	if (pdev == NULL) {
+		if (kvm->arch.intel_iommu_domain) {
+			intel_iommu_domain_exit(kvm->arch.intel_iommu_domain);

arch/x86/kvm/vtd.c line 104

Quoted 10 lines
arch/x86/kvm/vtd.c new file +10 -0
+			kvm->arch.intel_iommu_domain = NULL;
+		}
+		return -ENODEV;
+	}
+
+	kvm->arch.intel_iommu_domain = intel_iommu_domain_alloc(pdev);
+
+	if (kvm_iommu_map_memslots(kvm)) {
+		kvm_iommu_unmap_memslots(kvm);
+		return -EFAULT;

EFAULT is for the kernel touching an invalid userspace page. You should simply propagate the original error.

arch/x86/kvm/vtd.c line 114

Quoted 11 lines
arch/x86/kvm/vtd.c new file +10 -0
+	}
+
+	intel_iommu_detach_dev(kvm->arch.intel_iommu_domain,
+			       pdev->bus->number, pdev->devfn);
+
+	if (intel_iommu_context_mapping(kvm->arch.intel_iommu_domain,
+					pdev)) {
+		printk(KERN_ERR "Domain context map for %s failed",
+		       pci_name(pdev));
+		return -EFAULT;

Ditto.

Quoted 5 lines
arch/x86/kvm/vtd.c new file +4 -0
+	}
+	return 0;
+}
+

arch/x86/kvm/vtd.c line 128

Quoted 10 lines
arch/x86/kvm/vtd.c new file +10 -0
+static int kvm_iommu_put_pages(struct kvm *kvm,
+			       gfn_t base_gfn, unsigned long npages)
+{
+	gfn_t gfn = base_gfn;
+	pfn_t pfn;
+	struct dmar_domain *domain = kvm->arch.intel_iommu_domain;
+	int i;
+
+	if (!domain)
+		return -EFAULT;

Ditto.

arch/x86/kvm/vtd.c line 132

Quoted 5 lines
arch/x86/kvm/vtd.c new file +4 -0
+
+	for (i = 0; i < npages; i++) {
+		pfn = (pfn_t)intel_iommu_iova_to_pfn(domain,
+						     gfn << PAGE_SHIFT);

Overflow.

Quoted 9 lines
arch/x86/kvm/vtd.c new file +8 -0
+		kvm_release_pfn_clean(pfn);
+		gfn++;
+	}
+	return 0;
+}
+
+static int kvm_iommu_unmap_memslots(struct kvm *kvm)
+{

arch/x86/kvm/vtd.c line 150

Quoted 10 lines
arch/x86/kvm/vtd.c new file +10 -0
+	int i, rc;
+	for (i = 0; i < kvm->nmemslots; i++) {
+		rc = kvm_iommu_put_pages(kvm, kvm->memslots[i].base_gfn,
+					 kvm->memslots[i].npages);
+		if (rc)
+			return rc;
+	}
+	return 0;
+}
+

Unmapping should be unfailable, since there's not way to recover from it.

I'm unhappy with this wanton taking-of-references, but as long as mmu notifiers are unmerged, I see no other way.

Quoted 21 lines
arch/x86/kvm/x86.c +5 -0
index d9aa931..e57c9e4 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>
@@ -253,9 +254,17 @@ kvm_vm_ioctl_device_assignment(struct kvm *kvm,
 	}
 
 
+	if (intel_iommu_found()) {
+		r = kvm_iommu_map_guest(kvm, assigned_dev);
+		if (r)
+			goto out_intr;

arch/x86/kvm/x86.c kvm_vm_ioctl_device_assignment(struct kvm *kvm, · line 270

Quoted 10 lines
arch/x86/kvm/x86.c +4 -0
+	}
+
 out:
 	mutex_unlock(&kvm->lock);
 	return r;
+out_intr:
+	free_irq(dev->irq, match);
 out_list_del:
 	list_del(&match->list);
 	pci_release_regions(dev);

The split between device assignment and irq assignment I suggested earlier would definitely help this function.

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

RE: [PATCH 4/4] KVM: Device assignemnt with VT-d

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

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

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         |  182
 ++++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c     
 |   11 +++ include/asm-x86/kvm_host.h |    3 +
 include/linux/kvm_host.h   |    6 ++
 virt/kvm/kvm_main.c        |    8 ++-
 6 files changed, 210 insertions(+), 2 deletions(-)
 create mode 100644 arch/x86/kvm/vtd.c
virt/kvm/kvm_main.c +1 -0
index 5b470a1..49c4ed3 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>

virt/kvm/kvm_main.c static inline int valid_vcpu(int n) · line 83

Quoted 8 lines
virt/kvm/kvm_main.c +1 -1
@@ -76,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));

I cannot find this is_mmio_pfn() definition on main KVM tree, and failed to patch it.

Randy (Weidong)

#9 lore raw

RE: [PATCH 4/4] KVM: Device assignemnt with VT-d

Quoted 9 lines · Weidong

virt/kvm/kvm_main.c static inline int valid_vcpu(int n) · line 83

Quoted 5 lines · Ben-Ami Yassour

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

{

virt/kvm/kvm_main.c +0 -0
 	if (pfn_valid(pfn))
 		return PageReserved(pfn_to_page(pfn));

I cannot find this is_mmio_pfn() definition on main KVM tree, and failed to patch it.

This patch was missing, I now resent all the patches with the missing one.

Thanks,
Ben

#10 lore raw

Re: [PATCH 2/4] KVM: pci device assignment

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

Based on a patch from: Amit Shah <[email protected]>

This patch adds support for handling PCI devices that are assigned to the guest.

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. The VT-d extension is required to enable the device to perform DMA. Another alternative is PVDMA.

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

Quoted 2 lines
arch/x86/kvm/x86.c +2 -0
+struct kvm_assigned_dev_kernel
+*kvm_find_assigned_dev(struct list_head *head,

Keep these two on the same line.

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

Quoted 4 lines
arch/x86/kvm/x86.c +3 -0
+
+static int
+kvm_vm_ioctl_device_assignment(struct kvm *kvm,

Ditto.

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

Quoted 12 lines
arch/x86/kvm/x86.c +11 -0
+			       struct kvm_assigned_dev *assigned_dev)
+{
+	int r = 0;
+	struct kvm_assigned_dev_kernel *match;
+	struct pci_dev *dev;
+
+	if (assigned_dev->host.num_valid_irqs != 1) {
+		printk(KERN_INFO "%s: Unsupported number of irqs %d\n",
+		       __func__, assigned_dev->host.num_valid_irqs);
+		return -EINVAL;
+	}

We also support zero irqs. While this patch doesn't do much with the information (only claim the device), this is still useful.

include/asm-x86/kvm.h struct kvm_pit_channel_state { · line 212

Quoted 11 lines
include/asm-x86/kvm.h +1 -0
index 8f13749..12b4b25 100644
@@ -208,4 +208,5 @@ struct kvm_pit_channel_state {
 struct kvm_pit_state {
 	struct kvm_pit_channel_state channels[3];
 };
+
 #endif

Unrelated.

Quoted 14 lines
include/asm-x86/kvm_host.h +6 -0
index e2864e6..34eb3e7 100644
@@ -325,6 +325,25 @@ struct kvm_irq_ack_notifier {
 	void (*irq_acked)(struct kvm_irq_ack_notifier *kian);
 };
 
+/* 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_assigned_dev_work {
+	struct work_struct work;

include/asm-x86/kvm_host.h struct kvm_irq_ack_notifier { · line 343

Quoted 10 lines
include/asm-x86/kvm_host.h +9 -0
+	struct kvm_assigned_dev_kernel *assigned_dev;
+};
+
+struct kvm_assigned_dev_kernel {
+	struct kvm_irq_ack_notifier ack_notifier;
+	struct list_head list;
+	struct kvm_assigned_dev_info guest;
+	struct kvm_assigned_dev_info host;
+	struct kvm_assigned_dev_work int_work;
 

Just put the work_struct here, and use container_of() instead of ->assigned_dev.

include/asm-x86/kvm_host.h struct kvm_arch{ · line 360

Quoted 9 lines
include/asm-x86/kvm_host.h +1 -0
 struct kvm_arch{
 	int naliases;
 	struct kvm_mem_alias aliases[KVM_ALIAS_SLOTS];
@@ -337,6 +356,7 @@ struct kvm_arch{
 	 * Hash table of struct kvm_mmu_page.
 	 */
 	struct list_head active_mmu_pages;
+	struct list_head assigned_dev_head;
 

This is useful for non-x86 (except s390). It's okay to leave it to the ia64/ppc maintainers, though.

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

Quoted 13 lines
include/asm-x86/kvm_host.h +0 -0
 	struct kvm_pic *vpic;
 	struct kvm_ioapic *vioapic;
include/asm-x86/kvm_host.h +0 -0
 	struct kvm_pit *vpit;
include/asm-x86/kvm_para.h +0 -1
index 76f3921..3aa1731 100644
@@ -143,5 +143,4 @@ static inline unsigned int kvm_arch_para_features(void)
 }
 
 #endif
-
 

Spurious.

Quoted 28 lines

#endif

include/linux/kvm.h +5 -0
index 6edba45..c436c08 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_DEVICE_ASSIGNMENT 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_ASSIGNED_DEVICE _IOR(KVMIO, 0x69,		\
+					struct kvm_assigned_dev)
 
 /*
  * ioctls for vcpu fds
@@ -475,4 +478,22 @@ struct kvm_trace_rec {
 #define KVM_TRC_STLB_INVAL       (KVM_TRC_HANDLER + 0x18)
 #define KVM_TRC_PPC_INSTR        (KVM_TRC_HANDLER + 0x19)
 
+#define ASSIGNED_DEV_MAX_IRQ 16
+

include/linux/kvm.h struct kvm_trace_rec { · line 492

Quoted 10 lines
include/linux/kvm.h +9 -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_assigned_dev_info {
+	__u32 busnr;
+	__u32 devfn;
+	__u32 irq[ASSIGNED_DEV_MAX_IRQ];
+	__u32 num_valid_irqs; /* currently only 1 is supported */
+};
 

Move num_valid_irqs before the array. Add padding so the structure is a multiple of 64 bits (needed so that the i386 and x86_64 ABIs are identical).

include/linux/kvm.h struct kvm_trace_rec { · line 498

Quoted 8 lines
include/linux/kvm.h +7 -0
+
+/* Mapping between host and guest PCI device */
+struct kvm_assigned_dev {
+	struct kvm_assigned_dev_info guest;
+	struct kvm_assigned_dev_info host;
+};
+

guest.busnr and guest.devfn are meaningless; that's a wart.

What do you think of this API:

 struct kvm_assigned_pci_dev {
     __u32 assigned_dev_id;
     __u32 busnr;
     __u32 devfn;
     __u32 flags;

};

 struct kvm_assigned_irq {
     __u32 assigned_dev_id;
     __u32 host_irq;
     __u32 guest_irq;
     __u32 flags;

};

This automatically handles zero, one, or many irqs, and also allows for non-pci devices.

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

Re: [PATCH 2/4] KVM: pci device assignment

In reply to Ben-Ami Yassour
Quoted 10 lines · On Tuesday 22 July 2008 20:13:53 Ben-Ami Yassour wrote:
arch/x86/kvm/i8259.c +2 -1
-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) {

arch/x86/kvm/i8259.c int kvm_pic_read_irq(struct kvm_pic *s) · line 194

Quoted 8 lines
arch/x86/kvm/i8259.c +2 -0
@@ -186,6 +187,8 @@ int kvm_pic_read_irq(struct kvm_pic *s)
 		irq = 7;
 		intno = s->pics[0].irq_base + irq;
 	}
+	kvm_notify_acked_irq(kvm, irq);
+
 	pic_update_irq(s);
 
 	return intno;

One coding style suggestion,

struct kvm_pic has *irq_request_opaque which is struct kvm indeed. How about contain kvm in kvm_pic explicitly? (seems cleaner, though needs more modification).

Another thing is about host share IRQ: Do we want follow the straight forward way to add it? That's it, return IRQ_HANDLED from irq handler and wait for EOI of guest?

I found it's not easy to provide support for MSI without shared IRQ support. We don't know when guest want to enable MSI or it decided to shared IRQ, maybe we can intercept the unmask of IOAPIC to know that, but it's a little tricky.

Signature · 2 lines
regards
Yang, Sheng
#12 lore raw

Re: [PATCH 2/4] KVM: pci device assignment

Quoted 18 lines · Sheng
Quoted 17 lines · On Tuesday 22 July 2008 20:13:53 Ben-Ami Yassour wrote:
arch/x86/kvm/i8259.c +4 -1
-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,8 @@ int kvm_pic_read_irq(struct kvm_pic *s)
 		irq = 7;
 		intno = s->pics[0].irq_base + irq;
 	}
+	kvm_notify_acked_irq(kvm, irq);
+
 	pic_update_irq(s);
 
Quoted 7 lines

arch/x86/kvm/i8259.c int kvm_pic_read_irq(struct kvm_pic *s) · line 194

Quoted 1 line
arch/x86/kvm/i8259.c +0 -0
 	return intno;

One coding style suggestion,

struct kvm_pic has *irq_request_opaque which is struct kvm indeed. How about contain kvm in kvm_pic explicitly? (seems cleaner, though needs more modification).

Your suggestion is applied in the next set of patches that I sent.

Quoted 5 lines

Another thing is about host share IRQ: Do we want follow the straight forward way to add it? That's it, return IRQ_HANDLED from irq handler and wait for EOI of guest?

That's what the code in the current patches doing now. It also disables the irq until the EOI of the guest. If we don't do that, then the interrupt handler keeps getting called (which causes many guest exit) until the guest finally interacts with the device to release the interrupt line.

Thanks,
Ben

#13 lore raw

Re: [PATCH 2/4] KVM: pci device assignment

In reply to Ben-Ami Yassour
Quoted 23 lines · * On Tuesday 22 Jul 2008 17:43:53 Ben-Ami Yassour wrote:

Based on a patch from: Amit Shah <[email protected]>

This patch adds support for handling PCI devices that are assigned to the guest.

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. The VT-d extension is required to enable the device to perform DMA. Another alternative is PVDMA.

arch/x86/kvm/x86.c +1 -0
+	if (pci_enable_device(dev)) {

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

Quoted 10 lines
arch/x86/kvm/x86.c +10 -0
+		printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
+		r = -EBUSY;
+		goto out_put;
+	}
+	r = pci_request_regions(dev, "kvm_assigned_device");
+	if (r) {
+		printk(KERN_INFO "%s: Could not get access to device regions\n",
+		       __func__);
+		goto out_disable;
+	}

A driver might already be loaded for this device. If we fail to get the regions, it might mean the device was enabled and in use. That's the reason I didn't originally have a pci_disable_device() when pci_request_regions fails.

Quoted 14 lines
arch/x86/kvm/x86.c +14 -0
+out:
+	mutex_unlock(&kvm->lock);
+	return r;
+out_list_del:
+	list_del(&match->list);
+	pci_release_regions(dev);
+out_disable:
+	pci_disable_device(dev);
+out_put:
+	pci_dev_put(dev);
+out_free:
+	kfree(match);
+	mutex_unlock(&kvm->lock);
+	return r;
#14 lore raw

Re: [PATCH 1/4] KVM: Add irq ack notifier list

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

From: Avi Kivity <[email protected]>

This can be used by kvm subsystems that are interested in when interrupts
are acked, for example time drift compenstation.

Please add the pic and ioapic glue code to this patch.

Quoted 2 lines

(You need to add your signoff to patches you send; even if you didn't modify them).

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
#15 lore raw
In reply to Ben-Ami Yassour
Quoted 21 lines · Ben-Ami Yassour

Following are the device assignment patches with the fixes of the comments that were sent for the previous version.

Here is the list of changes that were made with respect to the previous version:
1. Replace the interrupt ack hook patches with the notifiers list patch by Avi.
2. Remove code from ioapic - the ack notifier is called before the checking the irr bit.
3. Remove the interrupt ack work queue and handle it directly.
4. Minimize the function for finding an assigned device.
5. Remove the pt_lock, after making the changes above it is no longer needed.
6. Move declarations from kvm_para.h
7. Add irq array to ioctl API. Note that this is only a change to the API, currently only single irq is supported.
8. Renaming: use "assigned device" and not "passthrough device"
9. Fix device release error path
10. Moving the assigned devices list pointer to the device struct itself and remove the extra structure.

Quoted 3 lines

Pending comment: shared guest interrputs are not tested.

(and not implemented, either).

Good progress; there are still many issues, but this is inevitable with such a large and complex patchset.

How are we standing with merging the changes to the VT-d driver, which are a prerequisite?

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
#16 lore raw
Quoted 26 lines · Avi Kivity
Quoted 21 lines · Ben-Ami Yassour

Following are the device assignment patches with the fixes of the comments that were sent for the previous version.

Here is the list of changes that were made with respect to the previous version:
1. Replace the interrupt ack hook patches with the notifiers list patch by Avi.
2. Remove code from ioapic - the ack notifier is called before the checking the irr bit.
3. Remove the interrupt ack work queue and handle it directly.
4. Minimize the function for finding an assigned device.
5. Remove the pt_lock, after making the changes above it is no longer needed.
6. Move declarations from kvm_para.h
7. Add irq array to ioctl API. Note that this is only a change to the API, currently only single irq is supported.
8. Renaming: use "assigned device" and not "passthrough device"
9. Fix device release error path
10. Moving the assigned devices list pointer to the device struct itself and remove the extra structure.

Quoted 3 lines

Pending comment: shared guest interrputs are not tested.

Quoted 8 lines

(and not implemented, either).

Good progress; there are still many issues, but this is inevitable with
such a large and complex patchset.

How are we standing with merging the changes to the VT-d driver, which are a prerequisite?

You mean push the changes into VT-d driver first. But the VT-d driver modification patch maybe need some changes during following development. How about making it stable in KVM first, then push it into VT-d driver?

Randy (Weidong)

Quoted 5 lines

--
Do not meddle in the internals of kernels, for they are subtle and quick to panic.

#17 lore raw
Quoted 8 lines · Weidong
Quoted 3 lines

How are we standing with merging the changes to the VT-d driver, which are a prerequisite?

You mean push the changes into VT-d driver first. But the VT-d driver modification patch maybe need some changes during following development. How about making it stable in KVM first, then push it into VT-d driver?

Yeah, we do have a chicken-and-egg situation here. I guess I can carry that patch while we're working this out.

Longer term, we will have to move to a vendor neutral API so as to support amd iommu and non-x86 iommus.

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
#18 lore raw
Quoted 11 lines · Avi Kivity
Quoted 9 lines · Weidong
Quoted 3 lines

How are we standing with merging the changes to the VT-d driver, which are a prerequisite?

You mean push the changes into VT-d driver first. But the VT-d driver modification patch maybe need some changes during following development. How about making it stable in KVM first, then push it into VT-d driver?

Quoted 5 lines

Yeah, we do have a chicken-and-egg situation here. I guess I can carry that patch while we're working this out.

Longer term, we will have to move to a vendor neutral API so as to support amd iommu and non-x86 iommus.

I agree arch-independent support is direction. But we should make one work first, then make it generic later.

Randy (Weidong)