Skip to contentSkip to conversation

VT-d pci passthrough patches

PATCH In review Ben-Ami Yassour 4 participants 12 messages Jul 10, 2008 – Jul 14, 2008

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

The following two patches contains the VT-d support for pci passthrough devices.
The first patch contains the changes that are required to the generic VT-d code.
The second patch contains the changes to KVM.

These patches contains fixes based on comments by Anthony, on the pervious version.

The VT-d code is used by the pci-passthrough code to map the entire guest memory in the IOMMU.
Once mmu-notifiers go in, we will be able to support finer grained forms of protection.

The main differences from the previous version, sent by Weidong, are fixes for the page pinning.
In the destruction path we need to call put_page for each page that was mapped in the iommu and pinned during initialization..
The KVM process page tables are already released by the time the KVM vm destroy path is executed.
Therefore we can not use gfn_to_pfn (which uses get_user_pages) to get the pages.
The approach in the current patch is to find the pages to unpin by looking at the IOMMU page table.

This patch applies to the Amit's PCI passthrough tree.

Any comments are welcome.

Regards,
Ben

#2 lore raw

PATCH 1/1 KVM: PCIPT: VT-d support

In reply to 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]

---
 arch/x86/kvm/Makefile      |    2 +-
 arch/x86/kvm/vtd.c         |  176 ++++++++++++++++++++++++++++++++++++++++++++
 arch/x86/kvm/x86.c         |   10 +++
 include/asm-x86/kvm_host.h |    1 +
 include/linux/kvm_host.h   |    6 ++
 virt/kvm/kvm_main.c        |    6 ++
 6 files changed, 200 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 +10 -0
index d4d4e0c..19d2ae3 100644
@@ -32,6 +32,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>
@@ -333,6 +334,13 @@ static int kvm_vm_ioctl_pci_pt_dev(struct kvm *kvm,
 	if (irqchip_in_kernel(kvm))
 		set_bit(dev->irq, pt_irq_handled);
 	write_unlock_irqrestore(&kvm_pci_pt_lock, flags);
+
+	if (intel_iommu_found()) {
+		r = kvm_iommu_map_guest(kvm, pci_pt_dev);
+		if (r)
+			goto out_put;
+	}
+
 out:
 	return r;
 out_put:
@@ -4272,6 +4280,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 0c6699f..953080e 100644
@@ -354,6 +354,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 d220b49..a16e2e3 100644
@@ -278,6 +278,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 531d635..dc67d90 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>
@@ -422,6 +423,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
#1 lore raw

PATCH 1/1 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
#3 lore raw

Re: [PATCH] KVM: PCIPT: VT-d support

In reply to Ben-Ami Yassour
Quoted 34 lines · On Thursday 10 July 2008 17:14:42 Ben-Ami Yassour wrote:

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

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

[Ben: fixed memory pinning]

---
 arch/x86/kvm/Makefile      |    2 +-
 arch/x86/kvm/vtd.c         |  176
++++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c    
    |   10 +++
 include/asm-x86/kvm_host.h |    1 +
 include/linux/kvm_host.h   |    6 ++
 virt/kvm/kvm_main.c        |    6 ++
 6 files changed, 200 insertions(+), 1 deletions(-)
 create mode 100644 arch/x86/kvm/vtd.c
virt/kvm/kvm_main.c +1 -0
index 531d635..dc67d90 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 int __kvm_set_memory_region(struct kvm *kvm, · line 430

Quoted 8 lines
virt/kvm/kvm_main.c +5 -0
@@ -422,6 +423,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);
+

I don't understand why we need this along with kvm_iommu_map_memslots(). This works during the memory setup, and in kvm_iommu_map_guest() we do it again with the overlapped memory region?

I think even if we need pin all pages, we still just need do it once...

--
Thanks
Yang, Sheng

#4 lore raw

Re: [PATCH] KVM: PCIPT: VT-d support

Quoted 49 lines · Sheng
Quoted 34 lines · On Thursday 10 July 2008 17:14:42 Ben-Ami Yassour wrote:

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

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

[Ben: fixed memory pinning]

---
 arch/x86/kvm/Makefile      |    2 +-
 arch/x86/kvm/vtd.c         |  176
++++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c    
    |   10 +++
 include/asm-x86/kvm_host.h |    1 +
 include/linux/kvm_host.h   |    6 ++
 virt/kvm/kvm_main.c        |    6 ++
 6 files changed, 200 insertions(+), 1 deletions(-)
 create mode 100644 arch/x86/kvm/vtd.c
virt/kvm/kvm_main.c +1 -0
index 531d635..dc67d90 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 int __kvm_set_memory_region(struct kvm *kvm, · line 430

Quoted 8 lines
virt/kvm/kvm_main.c +5 -0
@@ -422,6 +423,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);
+

I don't understand why we need this along with kvm_iommu_map_memslots(). This works during the memory setup, and in kvm_iommu_map_guest() we do it again with the overlapped memory region?

Quoted 2 lines

I think even if we need pin all pages, we still just need do it once...

We map the entire guest memory on initialization by going over all the existing memory slots.
If a new slot is created later then we need to map it as well, this is the call that you see here.

Regards,
Ben

#5 lore raw

Re: [PATCH] KVM: PCIPT: VT-d support

Quoted 52 lines · On Thursday 10 July 2008 17:51:53 Ben-Ami Yassour wrote:
Quoted 51 lines · Sheng
Quoted 36 lines · On Thursday 10 July 2008 17:14:42 Ben-Ami Yassour wrote:

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

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

[Ben: fixed memory pinning]

---
 arch/x86/kvm/Makefile      |    2 +-
 arch/x86/kvm/vtd.c         |  176

++++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c

    |   10 +++
 include/asm-x86/kvm_host.h |    1 +
 include/linux/kvm_host.h   |    6 ++
 virt/kvm/kvm_main.c        |    6 ++
 6 files changed, 200 insertions(+), 1 deletions(-)
 create mode 100644 arch/x86/kvm/vtd.c
virt/kvm/kvm_main.c +1 -0
index 531d635..dc67d90 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 int __kvm_set_memory_region(struct kvm · line 430

Quoted 8 lines
virt/kvm/kvm_main.c +5 -0
@@ -422,6 +423,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);
+

I don't understand why we need this along with kvm_iommu_map_memslots(). This works during the memory setup, and in kvm_iommu_map_guest() we do it again with the overlapped memory region?

Quoted 7 lines
Quoted 2 lines

I think even if we need pin all pages, we still just need do it once...

We map the entire guest memory on initialization by going over all the existing memory slots.
If a new slot is created later then we need to map it as well, this is the call that you see here.

I think it's may be unnecessary to map pages when device assigned. The table can be set up along with set_memory_region(), it covered all memory slots already, or I miss something here?

Signature · 2 lines
Thanks
Yang, Sheng
#6 lore raw

Re: [PATCH] KVM: PCIPT: VT-d support

Quoted 3 lines · Sheng

I think it's may be unnecessary to map pages when device assigned. The table can be set up along with set_memory_region(), it covered all memory slots already, or I miss something here?

VT-d is only initialized after the slots are originally created, so when VT-d is initialized is map all of the existing slots separately, and then for each new slot that may be added we'll catch it via set_memory_region().

Cheers,
Muli

#7 lore raw

Re: [PATCH] KVM: PCIPT: VT-d support

Quoted 8 lines · Muli Ben-Yehuda
Quoted 4 lines · Sheng

I think it's may be unnecessary to map pages when device assigned. The table can be set up along with set_memory_region(), it covered all memory slots already, or I miss something here?

Quoted 5 lines

VT-d is only initialized after the slots are originally created, so when VT-d is initialized is map all of the existing slots separately, and then for each new slot that may be added we'll catch it via set_memory_region().

It makes sense to initialize VT-d before. Since memslots can be created and destroyed dynamically, with the current implementation we can see

create slot
create slot
init VT-d
create slot

which means we need to support both slot-creation-after-VT-d and init-VT-d-after-slot-creation. If we initialize VT-d up front, we only need to support (and test) one scenario.

On the other hand, this means that you will not be able to assign devices unless you specified this when creating the VM; but I think this is fair.

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

Re: [PATCH] KVM: PCIPT: VT-d support

Quoted 4 lines · Avi Kivity

It makes sense to initialize VT-d before. Since memslots can be created and destroyed dynamically, with the current implementation we can see

Quoted 8 lines

create slot
create slot
init VT-d
create slot

which means we need to support both slot-creation-after-VT-d and init-VT-d-after-slot-creation. If we initialize VT-d up front, we only need to support (and test) one scenario.

Fair enough---we'll take a look at whether initializing VT-d first introduces any complications other than hot-plug (see below).

Quoted 3 lines

On the other hand, this means that you will not be able to assign devices unless you specified this when creating the VM; but I think this is fair.

It will be nice to support hot-plugged pass-through devices some time in the future.

Cheers,
Muli

#9 lore raw

Re: [PATCH] KVM: PCIPT: VT-d support

Quoted 10 lines · Muli Ben-Yehuda
Quoted 5 lines

On the other hand, this means that you will not be able to assign devices unless you specified this when creating the VM; but I think this is fair.

It will be nice to support hot-plugged pass-through devices some time in the future.

And it should be easy to do too. The question is whether we need to allocate the iommu domain up front (which would mean that a command line parameter would be needed, meaning roughly 'support pci device assignment on this guest'. Kind of like -usb, which means 'support usb device assignment on this guest'.

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

Re: [PATCH] KVM: PCIPT: VT-d support

Quoted 12 lines · Avi Kivity
Quoted 10 lines · Muli Ben-Yehuda
Quoted 5 lines

On the other hand, this means that you will not be able to assign devices unless you specified this when creating the VM; but I think this is fair.

It will be nice to support hot-plugged pass-through devices some time in the future.

Quoted 6 lines

And it should be easy to do too. The question is whether we need to allocate the iommu domain up front (which would mean that a command line parameter would be needed, meaning roughly 'support pci device assignment on this guest'. Kind of like -usb, which means 'support usb device assignment on this guest'.

I don't think that its that simple...
On a single machine you can have multiple iommu units, each of which controls certain PCI slots.
To configure the iommu for a specific device you need to configure the iommu unit that controls that device.
This means that we need to know the bus/dev/func of the device before we create the VT-d domain.
For hot-plug devices it means that by the time we can create the domain we already have memory slots that we need to add to the VT-d mapping.

In addition, some times it is desirable to be able to map the memory as late as possible. For example if the guest supports PVDMA then there is no need to map the entire guest memory, however the host can not tell if the guest supports PVDMA before the memory slots are created. In this case we would like to postpone the mapping of the entire guest memory until we know we have to.

Regards,
Ben

#11 lore raw

Re: [PATCH] KVM: PCIPT: VT-d support

Quoted 18 lines · Ben-Ami Yassour
Quoted 7 lines · Avi Kivity
Quoted 5 lines · Muli Ben-Yehuda
Quoted 5 lines

On the other hand, this means that you will not be able to assign devices unless you specified this when creating the VM; but I think this is fair.

Quoted 8 lines
Quoted 5 lines

It will be nice to support hot-plugged pass-through devices some time in the future.

And it should be easy to do too. The question is whether we need to allocate the iommu domain up front (which would mean that a command line parameter would be needed, meaning roughly 'support pci device assignment on this guest'. Kind of like -usb, which means 'support usb device assignment on this guest'.

Quoted 7 lines

I don't think that its that simple...
On a single machine you can have multiple iommu units, each of which controls certain PCI slots.
To configure the iommu for a specific device you need to configure the iommu unit that controls that device.
This means that we need to know the bus/dev/func of the device before we create the VT-d domain.
For hot-plug devices it means that by the time we can create the domain we already have memory slots that we need to add to the VT-d mapping.

Thanks for the info. Looks like we have to support both memslot-before-iommu and memslot-after-iommu then.

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