Patches for vtd pci passthrough
PATCH 1/1 KVM: PCIPT: free device structure on vm destroy
In reply to [email protected]From: Han Weidong <[email protected]>
--- arch/x86/kvm/x86.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
arch/x86/kvm/x86.c +1 -0
@@ -358,6 +358,7 @@ static void kvm_free_pci_passthrough(struct kvm *kvm)free_irq (pci_pt_dev->pt_dev.host.irq, kvm);list_del (&pci_pt_dev->list); +kfree (pci_pt_dev); }write_unlock_irqrestore (&kvm_pci_pt_lock, flags);
Signature · 1 line
1.5.5.1
Amit,
I gathered the last few patches that Randy sent and that I had for the vtd branch of the PCI passthrough tree.
With these patches passthrough with VT-d seems to work.
We tested this with an e1000 NIC.
Comments appreciated.
Thanks,
Ben
PATCH 1/1 KVM: PCIPT: fix compilation errors
From: Han Weidong <[email protected]>
This patch applies to the PCIPT vtd branch
--- arch/x86/kvm/x86.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +3 -3
@@ -307,9 +307,10 @@ static int kvm_vm_ioctl_pci_pt_dev(struct kvm *kvm, }if (kvm_intel_iommu_found ()) { - r =kvm_iommu_map_guest (kvm, &pci_pt_dev); + r =kvm_iommu_map_guest (kvm, pci_pt_dev);if (r) - goto out; + goto out_free; + }write_lock_irqsave (&kvm_pci_pt_lock, flags);
@@ -1968,7 +1969,6 @@ long kvm_arch_vm_ioctl(struct file *filp, r =kvm_vm_ioctl_pci_pt_dev (kvm, &pci_pt_dev);if (r) goto out; - }break ; }case KVM_GET_PIT : {
Signature · 1 line
1.5.5.1
PATCH 1/1 KVM: PCIPT: VT-d: fix context mapping
In reply to [email protected]From: Ben-Ami Yassour <[email protected]>
When changing the VT-d context mapping, according to the spec, it is required to first set the context to not present, flush and only then apply the new context.
--- drivers/pci/intel-iommu.c | 17 +++++++++++++++++ 1 files changed, 17 insertions(+), 0 deletions(-)
drivers/pci/intel-iommu.c +17 -0
@@ -56,6 +56,7 @@static void flush_unmaps_timeout (unsigned long data); +static void detach_domain_for_dev (struct dmar_domain *domain,u8 bus,u8 devfn);DEFINE_TIMER (unmap_timer, flush_unmaps_timeout,0 ,0 );
@@ -1264,7 +1265,23 @@ static int domain_context_mapping_one(struct dmar_domain *domain,if (!context)return -ENOMEM ;spin_lock_irqsave (&iommu->lock, flags); + +if (context_present (*context) && + (context_domain_id (*context) == domain->id) && + (context_address_width (*context) == domain->agaw) && + (context_address_root (*context) ==virt_to_phys (domain->pgd)) && + (context_translation_type (*context) ==CONTEXT_TT_MULTI_LEVEL ) && + (!context_fault_disable (*context))) { +spin_unlock_irqrestore (&iommu->lock, flags); +return 0 ; + } + +spin_unlock_irqrestore (&iommu->lock, flags); + +detach_domain_for_dev (domain, bus, devfn); +spin_lock_irqsave (&iommu->lock, flags); +context_clear_entry (*context);context_set_domain_id (*context, domain->id);context_set_address_width (*context, domain->agaw);
Signature · 1 line
1.5.5.1
PATCH 1/1 KVM: PCIPT: VT-d: fix guest unmap
In reply to [email protected]From: Han Weidong <[email protected]>
--- arch/x86/kvm/vtd.c | 2 +- drivers/pci/intel-iommu.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/vtd.c +1 -1
@@ -157,7 +157,7 @@ int kvm_iommu_unmap_guest(struct kvm *kvm)struct kvm_pci_pt_dev_list *entry;struct pci_dev *pdev =NULL ; -if (kvm->arch.domain) +if (!kvm->arch.domain)return 0 ;list_for_each_entry (entry, &kvm->arch.pci_pt_dev_head, list) {
drivers/pci/intel-iommu.c +1 -1
@@ -2483,7 +2483,7 @@ EXPORT_SYMBOL_GPL(kvm_intel_iommu_page_mapping);void kvm_intel_iommu_detach_dev (struct dmar_domain *domain,u8 bus,u8 devfn) { -kvm_intel_iommu_detach_dev (domain, bus, devfn); +detach_domain_for_dev (domain, bus, devfn); }EXPORT_SYMBOL_GPL (kvm_intel_iommu_detach_dev);
Signature · 1 line
1.5.5.1
KVM: PCIPT: temporary fix for pio (userspace part)
In reply to [email protected]PIO for passthrough does not work properly since the permissions are not set, it is not enough to call iopl at init time.
This temporary patch calls iopl for every pio request.
A better solution would be have a per thread iopl.
An alternative approach would be to use sysfs for pio.
In the mean time, if you want to use the vtd branch, then you probably need this patch.
PATCH 1/1 KVM: PCIPT: temporary fix for pio (userspace part)
From: Ben-Ami Yassour <[email protected]>
PIO for passthrough does not work properly since the permissions are not set, it is not enough to call iopl at init time.
This temporary patch calls iopl for every pio request.
A better solution would be have a per thread iopl.
An alternative approach would be to use sysfs for pio.
In the mean time, if you want to use the vtd branch, then you probably need this patch.
--- qemu/hw/pci-passthrough.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
qemu/hw/pci-passthrough.c +1 -0
@@ -117,6 +117,7 @@ static void pt_ioport_write##suffix(void *opaque, uint32_t addr, uint32_t value) r_pio, (int )r_access->e_physbase, \ (unsigned long)r_access->r_virtbase, value); \ } \ +iopl (3 ); \ out##suffix(value, r_pio); \ }
Signature · 1 line
1.5.5.1
Re: [PATCH] KVM: PCIPT: VT-d: fix guest unmap
In reply to [email protected]I think the current VT-d code needs some reworking.
We should build the table as the shadow page table gets built. We should suppress iotlb flushes unless the table is actually being updated.
Regards,
Anthony Liguori
Quoted 35 lines · [email protected] wrote:
From: Han Weidong <[email protected]>
Signed-off-by Ben-Ami Yassour <[email protected]>--- arch/x86/kvm/vtd.c | 2 +- drivers/pci/intel-iommu.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)arch/x86/kvm/vtd.c +1
-1@@ -157,7 +157,7 @@ int kvm_iommu_unmap_guest(struct kvm *kvm)struct kvm_pci_pt_dev_list *entry;struct pci_dev *pdev =NULL ; -if (kvm->arch.domain) +if (!kvm->arch.domain)return 0 ;list_for_each_entry (entry, &kvm->arch.pci_pt_dev_head, list) {drivers/pci/intel-iommu.c +1
-1@@ -2483,7 +2483,7 @@ EXPORT_SYMBOL_GPL(kvm_intel_iommu_page_mapping);void kvm_intel_iommu_detach_dev (struct dmar_domain *domain,u8 bus,u8 devfn) { -kvm_intel_iommu_detach_dev (domain, bus, devfn); +detach_domain_for_dev (domain, bus, devfn); }EXPORT_SYMBOL_GPL (kvm_intel_iommu_detach_dev);
Re: [PATCH] KVM: PCIPT: VT-d: fix guest unmap
Quoted 5 lines · Anthony Liguori
I think the current VT-d code needs some reworking.
We should build the table as the shadow page table gets built. We should suppress iotlb flushes unless the table is actually being updated.
I'm not sure what you mean.
The current implementation of vtd for passthrough is a direct map, which means that we map the entire guest memory (and pin it).
In this case there are no iotlb flushes after the first initialization.
Obviously, pinning the entire guest is not desirable since we waste a lot of memory resources, but this is the approach that we currently have. Do you find it good enough for a merge with the main KVM tree, and optimize later?
When you mentioned building a table as the shadow page table, did you mean that we should map the IOMMU on demand?
I'm not sure how we can do that... the guest can send a guest physical address to the device for DMA, even without generating a page-fault on the host for that address... which implies that the host must pin the entire guest memory in advance. agree?
The only way I can think of avoiding that is PVDMA with VT-d, which means that there is a hyper call for each DMA request, but this is a different solution, cause it only applies to PV guests.
Do you see a way to avoid mapping (and pinning) the entire guest memory for fully virtual guests (and without parsing every transaction between the guest and the device to figure out the DMA addresses)?
Regards,
Ben
Quoted 4 lines
Regards,
Anthony Liguori
Re: [PATCH] KVM: PCIPT: VT-d: fix context mapping
In reply to [email protected]Quoted 27 lines · On Tue, Jun 17, 2008 at 10:05:26PM +0300, Ben-Ami Yassour1 wrote:
From: Ben-Ami Yassour <[email protected]>
When changing the VT-d context mapping, according to the spec, it is required to first set the context to not present, flush and only then apply the new context.
Signed-off-by Ben-Ami Yassour <[email protected]>--- drivers/pci/intel-iommu.c | 17 +++++++++++++++++ 1 files changed, 17 insertions(+), 0 deletions(-)drivers/pci/intel-iommu.c +1
-0@@ -56,6 +56,7 @@static void flush_unmaps_timeout (unsigned long data); +static void detach_domain_for_dev (struct dmar_domain *domain,u8 bus,u8 devfn);DEFINE_TIMER (unmap_timer, flush_unmaps_timeout,0 ,0 );@@ -1264,7 +1265,23 @@ static int domain_context_mapping_one(struct dmar_domain *domain,if (!context)return -ENOMEM ;spin_lock_irqsave (&iommu->lock, flags);
drivers/pci/intel-iommu.c static int domain_context_mapping_one(struct dmar_domain *domain, · line 1277
Quoted 10 lines
drivers/pci/intel-iommu.c +10
-0+ +if (context_present (*context) && + (context_domain_id (*context) == domain->id) && + (context_address_width (*context) == domain->agaw) && + (context_address_root (*context) ==virt_to_phys (domain->pgd)) && + (context_translation_type (*context) ==CONTEXT_TT_MULTI_LEVEL ) && + (!context_fault_disable (*context))) { +spin_unlock_irqrestore (&iommu->lock, flags); +return 0 ; + }
Can we wrap this in a descriptively named function, which will also and release the lock? I think it will make the flow more obvious here.
Cheers,
Muli
Re: [PATCH] KVM: PCIPT: VT-d: fix guest unmap
In reply to Ben-Ami YassourQuoted 4 lines · Ben-Ami Yassour
Quoted 2 lines · Anthony Liguori
I think the current VT-d code needs some reworking.
Quoted 9 lines
Quoted 3 lines
We should build the table as the shadow page table gets built. We should suppress iotlb flushes unless the table is actually being updated.
I'm not sure what you mean.
The current implementation of vtd for passthrough is a direct map, which means that we map the entire guest memory (and pin it).
In this case there are no iotlb flushes after the first initialization.
Right. But this is not ideal. Instead of pinning up-front, it would make more sense IMHO to build the VT-d table as the shadow page table gets faulted in. In certain circumstances, this will result in extraneous updates (because a GPA=>HPA mapping is already present) and that's where we should eliminate iotlb flushes.
For now, we should basically do this for all of physical memory but we should have the right infrastructure such that we can be more clever once we have a PVDMA API.
Quoted 5 lines
Obviously, pinning the entire guest is not desirable since we waste a lot of memory resources, but this is the approach that we currently have. Do you find it good enough for a merge with the main KVM tree, and optimize later?
No, it's not safe. What happens mmap(MAP_FIXED) into phys_ram_base? We need to use MMU notifiers to handle such events and appropriately flush the iotlb.
Quoted 3 lines
When you mentioned building a table as the shadow page table, did you mean that we should map the IOMMU on demand?
Yes, but in the absence of a PV guest, there's a very special case where we pre-fault the entire table.
Quoted 5 lines
I'm not sure how we can do that... the guest can send a guest physical address to the device for DMA, even without generating a page-fault on the host for that address... which implies that the host must pin the entire guest memory in advance. agree?
See above. Ideally we would wait until the first PCI config space access for a device before special casing the guest. Otherwise, there's no way to allow a DMA-aware guest to avoid pinning up front.
Quoted 4 lines
The only way I can think of avoiding that is PVDMA with VT-d, which means that there is a hyper call for each DMA request, but this is a different solution, cause it only applies to PV guests.
It doesn't strictly require a hypercall, but yes, that's the general solution.
Quoted 4 lines
Do you see a way to avoid mapping (and pinning) the entire guest memory for fully virtual guests (and without parsing every transaction between the guest and the device to figure out the DMA addresses)?
The key is to support both cases with the same infrastructure. The unmodified guest should just be a special case.
Regards,
Anthony Liguori
Quoted 12 lines
Regards,
BenQuoted 5 lines
Regards,
Anthony Liguori
Re: [PATCH] KVM: PCIPT: VT-d: fix guest unmap
Quoted 5 lines · Anthony Liguori
Right. But this is not ideal. Instead of pinning up-front, it would make more sense IMHO to build the VT-d table as the shadow page table gets faulted in. In certain circumstances, this will result in extraneous updates (because a GPA=>HPA mapping is already present) and that's where we should eliminate iotlb flushes.
As Ben wrote, we can't do this and must fault everything in up-front (assuming no PVDMA API). Assume we don't do this: it is valid for the guest to program the device with a GPA that does not yet have a corresponding HPA (because the guest did not write or read to/from it and thus we haven't yet faulted in a frame for it). Then, once the device DMA's to it, the DMA will be stopped incorrectly.
Quoted 8 lines
Quoted 4 lines
Obviously, pinning the entire guest is not desirable since we waste a lot of memory resources, but this is the approach that we currently have. Do you find it good enough for a merge with the main KVM tree, and optimize later?
No, it's not safe. What happens
mmap(MAP_FIXED)intophys_ram_base? We need to use MMU notifiers to handle such events and appropriately flush the iotlb.
Could you elaborate on what you mean here and what is not safe? Our current approach is to just fault in all of guest memory---are you concerned about a case where some of the guest frames get replaced by other frames because of the mmap()?
I'd like to stress that we are shooting at the moment for the simplest possible solution that is good enough, so that we'll be able to finally merge this into the tree...
Quoted 8 lines
Quoted 4 lines
I'm not sure how we can do that... the guest can send a guest physical address to the device for DMA, even without generating a page-fault on the host for that address... which implies that the host must pin the entire guest memory in advance. agree?
See above. Ideally we would wait until the first PCI config space access for a device before special casing the guest. Otherwise, there's no way to allow a DMA-aware guest to avoid pinning up front.
Err, if the user gave the guest pass-through access to a PCI device, presumably it is because the guest will use it... What do we win by delaying the inevitable?
Cheers,
Muli
Re: [PATCH] KVM: PCIPT: VT-d: fix guest unmap
Quoted 10 lines · Muli Ben-Yehuda
Quoted 6 lines · Anthony Liguori
Right. But this is not ideal. Instead of pinning up-front, it would make more sense IMHO to build the VT-d table as the shadow page table gets faulted in. In certain circumstances, this will result in extraneous updates (because a GPA=>HPA mapping is already present) and that's where we should eliminate iotlb flushes.
Quoted 7 lines
As Ben wrote, we can't do this and must fault everything in up-front (assuming no PVDMA API). Assume we don't do this: it is valid for the guest to program the device with a GPA that does not yet have a corresponding HPA (because the guest did not write or read to/from it and thus we haven't yet faulted in a frame for it). Then, once the device DMA's to it, the DMA will be stopped incorrectly.
As I've said, the lack of PVDMA API is a special case. The key is to use the same internal infrastructure.
Quoted 10 lines
Quoted 9 lines
Quoted 5 lines
Obviously, pinning the entire guest is not desirable since we waste a lot of memory resources, but this is the approach that we currently have. Do you find it good enough for a merge with the main KVM tree, and optimize later?
No, it's not safe. What happens
mmap(MAP_FIXED)intophys_ram_base? We need to use MMU notifiers to handle such events and appropriately flush the iotlb.
Quoted 5 lines
Could you elaborate on what you mean here and what is not safe? Our current approach is to just fault in all of guest memory---are you concerned about a case where some of the guest frames get replaced by other frames because of the
mmap()?
Because the guest is now accessing memory that is not guest memory. When mmu-notifiers forcefully change a mapping, we need to react to it.
Quoted 4 lines
I'd like to stress that we are shooting at the moment for the simplest possible solution that is good enough, so that we'll be able to finally merge this into the tree...
I don't think what I'm suggesting is more code than the current implementation and it fits more cleanly into KVM.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
I'm not sure how we can do that... the guest can send a guest physical address to the device for DMA, even without generating a page-fault on the host for that address... which implies that the host must pin the entire guest memory in advance. agree?
Quoted 9 lines
Quoted 4 lines
See above. Ideally we would wait until the first PCI config space access for a device before special casing the guest. Otherwise, there's no way to allow a DMA-aware guest to avoid pinning up front.
Err, if the user gave the guest pass-through access to a PCI device, presumably it is because the guest will use it... What do we win by delaying the inevitable?
s/DMA-aware/PVDMA-aware/
You do not know if a guest is PVDMA-aware until the guest tells you so. If you pin all of memory before the guest starts running, you may not have needed to allocate all of that memory. As we move to cooperative memory management between the host and guest, I expect the normal circumstance will be to launch a guest with far more memory than it needs relying on the fact that the guest will not touch that memory. Pinning memory unconditionally defeats this.
In terms of merging, I don't think it's going to be reasonable to merge for 2.6.27 so there's not much of an argument for not doing it correctly.
Regards,
Anthony Liguori
Quoted 3 lines
Cheers,
Muli
RE: [PATCH] KVM: PCIPT: VT-d: fix context mapping
In reply to [email protected]Quoted 26 lines · [email protected] wrote:
From: Ben-Ami Yassour <[email protected]>
When changing the VT-d context mapping, according to the spec, it is required
to first set the context to not present, flush and only then apply the new context.Signed-off-by Ben-Ami Yassour <[email protected]>--- drivers/pci/intel-iommu.c | 17 +++++++++++++++++ 1 files changed, 17 insertions(+), 0 deletions(-)drivers/pci/intel-iommu.c +1
-0@@ -56,6 +56,7 @@static void flush_unmaps_timeout (unsigned long data); +static void detach_domain_for_dev (struct dmar_domain *domain,u8 bus,u8 devfn);DEFINE_TIMER (unmap_timer, flush_unmaps_timeout,0 ,0 );
drivers/pci/intel-iommu.c static int domain_context_mapping_one(struct · line 1272
Quoted 8 lines
drivers/pci/intel-iommu.c +5
-0@@ -1264,7 +1265,23 @@ static int domain_context_mapping_one(struct dmar_domain *domain, if (!context) return -ENOMEM;spin_lock_irqsave (&iommu->lock, flags); + + if (context_present(*context) && + (context_domain_id(*context) == domain->id) && + (context_address_width(*context) == domain->agaw) && + (context_address_root(*context) ==
virt_to_phys(domain->pgd)) &&
drivers/pci/intel-iommu.c static int domain_context_mapping_one(struct · line 1273
Quoted 1 line
drivers/pci/intel-iommu.c +1
-0+ (context_translation_type(*context) ==
CONTEXT_TT_MULTI_LEVEL)
drivers/pci/intel-iommu.c static int domain_context_mapping_one(struct dmar_domain *domain, · line 1277
Quoted 4 lines
drivers/pci/intel-iommu.c +3
-0&& + (!context_fault_disable(*context))) { +spin_unlock_irqrestore (&iommu->lock, flags); +return 0 ; + }
Only need to check context_present(*context) according to VT-d spec, which says "software must not modify fields other than the Present (P) field of currently present root-entries or context-entries."
Randy (Weidong)
Quoted 10 lines
drivers/pci/intel-iommu.c +6
-0+ +spin_unlock_irqrestore (&iommu->lock, flags); + +detach_domain_for_dev (domain, bus, devfn); +spin_lock_irqsave (&iommu->lock, flags); +context_clear_entry (*context);context_set_domain_id (*context, domain->id);context_set_address_width (*context, domain->agaw);
RE: [PATCH] KVM: PCIPT: VT-d: fix context mapping
Quoted 47 lines · Weidong
Quoted 26 lines · [email protected] wrote:
From: Ben-Ami Yassour <[email protected]>
When changing the VT-d context mapping, according to the spec, it is required
to first set the context to not present, flush and only then apply the new context.Signed-off-by Ben-Ami Yassour <[email protected]>--- drivers/pci/intel-iommu.c | 17 +++++++++++++++++ 1 files changed, 17 insertions(+), 0 deletions(-)drivers/pci/intel-iommu.c +1
-0@@ -56,6 +56,7 @@static void flush_unmaps_timeout (unsigned long data); +static void detach_domain_for_dev (struct dmar_domain *domain,u8 bus,u8 devfn);DEFINE_TIMER (unmap_timer, flush_unmaps_timeout,0 ,0 );drivers/pci/intel-iommu.c static int domain_context_mapping_one(struct · line 1272
Quoted 8 lines
drivers/pci/intel-iommu.c +5
-0@@ -1264,7 +1265,23 @@ static int domain_context_mapping_one(struct dmar_domain *domain, if (!context) return -ENOMEM;spin_lock_irqsave (&iommu->lock, flags); + + if (context_present(*context) && + (context_domain_id(*context) == domain->id) && + (context_address_width(*context) == domain->agaw) && + (context_address_root(*context) ==
virt_to_phys(domain->pgd)) &&drivers/pci/intel-iommu.c static int domain_context_mapping_one(struct · line 1273
Quoted 1 line
drivers/pci/intel-iommu.c +1
-0+ (context_translation_type(*context) ==
CONTEXT_TT_MULTI_LEVEL)drivers/pci/intel-iommu.c static int domain_context_mapping_one(struct dmar_domain *domain, · line 1277
Quoted 4 lines
drivers/pci/intel-iommu.c +3
-0&& + (!context_fault_disable(*context))) { +spin_unlock_irqrestore (&iommu->lock, flags); +return 0 ; + }Only need to check
context_present(*context)according to VT-d spec, which says "software must not modify fields other than the Present (P) field of currently present root-entries or context-entries."
Quoted 1 line
Randy (Weidong)
The logic here is that, if no change is made to the context then just return ok (0). Otherwise, according to the spec, we need to first invalidate the context, flush it, and only then apply the changes to the context.
The other option is to make sure that before every call to this function the context is invalidated, but disabling it inside the function seems safer. do you agree with that?
Regards,
Ben
Quoted 11 lines
Quoted 11 lines
drivers/pci/intel-iommu.c +6
-0+ +spin_unlock_irqrestore (&iommu->lock, flags); + +detach_domain_for_dev (domain, bus, devfn); +spin_lock_irqsave (&iommu->lock, flags); +context_clear_entry (*context);context_set_domain_id (*context, domain->id);context_set_address_width (*context, domain->agaw);
RE: [PATCH] KVM: PCIPT: VT-d: fix context mapping
Quoted 46 lines · Ben-Ami Yassour
Quoted 43 lines · Weidong
Quoted 35 lines · [email protected] wrote:
From: Ben-Ami Yassour <[email protected]>
When changing the VT-d context mapping, according to the spec, it is required to first set the context to not present, flush and only then apply the new context.
Signed-off-by Ben-Ami Yassour <[email protected]>--- drivers/pci/intel-iommu.c | 17 +++++++++++++++++ 1 files changed, 17 insertions(+), 0 deletions(-)drivers/pci/intel-iommu.c +7
-0+++ b/drivers/pci/intel-iommu.c@@ -56,6 +56,7 @@static void flush_unmaps_timeout (unsigned long data); +static void detach_domain_for_dev (struct dmar_domain *domain,u8 bus,u8 devfn);DEFINE_TIMER (unmap_timer, flush_unmaps_timeout,0 ,0 );@@ -1264,7 +1265,23 @@ static int domain_context_mapping_one(struct dmar_domain *domain, if (!context) return -ENOMEM;spin_lock_irqsave (&iommu->lock, flags); + +if (context_present (*context) && + (context_domain_id (*context) == domain->id) && + (context_address_width (*context) == domain->agaw) && + (context_address_root(*context) == virt_to_phys(domain->pgd))&& + (
context_translation_type(*context)==CONTEXT_TT_MULTI_LEVEL) && +
(!context_fault_disable(*context))) {drivers/pci/intel-iommu.c
Quoted 2 lines
drivers/pci/intel-iommu.c +2
-0+ spin_unlock_irqrestore(&iommu->lock, flags); + return 0; + }Only need to check
context_present(*context)according to VT-d spec, which says "software must not modify fields other than the Present
(P) field of currently present root-entries or context-entries."Quoted 1 line
Randy (Weidong)
Quoted 9 lines
The logic here is that, if no change is made to the context then just return ok (0). Otherwise, according to the spec, we need to first invalidate the context, flush it, and only then apply the changes to the context.
The other option is to make sure that before every call to this function the context is invalidated, but disabling it inside the function seems safer. do you agree with that?
After a device can be assigned to guest with VT-d, it needs a context unmap function. When a device is assigned to a guest, map context for it, while when it is detached from a guest, should unmap its context. With the context unmap function, I think we needn't to implement your logic in domain_context_mapping_one(), instead just check its present. What's your opinion?
Randy (Weidong)
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
drivers/pci/intel-iommu.c +6
-0+ +spin_unlock_irqrestore (&iommu->lock, flags); + +detach_domain_for_dev (domain, bus, devfn); +spin_lock_irqsave (&iommu->lock, flags); +context_clear_entry (*context);context_set_domain_id (*context, domain->id);context_set_address_width (*context, domain->agaw);
RE: [PATCH] KVM: PCIPT: VT-d: fix context mapping
"Han, Weidong" <[email protected]> wrote on 19/06/2008 17:18:00:
Quoted 57 lines
Quoted 46 lines · Ben-Ami Yassour
Quoted 43 lines · Weidong
Quoted 35 lines · [email protected] wrote:
From: Ben-Ami Yassour <[email protected]>
When changing the VT-d context mapping, according to the spec, it is required to first set the context to not present, flush and only then apply the new context.
Signed-off-by Ben-Ami Yassour <[email protected]>--- drivers/pci/intel-iommu.c | 17 +++++++++++++++++ 1 files changed, 17 insertions(+), 0 deletions(-)drivers/pci/intel-iommu.c +7
-0+++ b/drivers/pci/intel-iommu.c@@ -56,6 +56,7 @@static void flush_unmaps_timeout (unsigned long data); +static void detach_domain_for_dev (struct dmar_domain *domain,u8 bus,u8 devfn);DEFINE_TIMER (unmap_timer, flush_unmaps_timeout,0 ,0 );@@ -1264,7 +1265,23 @@ static int domain_context_mapping_one(struct dmar_domain *domain, if (!context) return -ENOMEM;spin_lock_irqsave (&iommu->lock, flags); + +if (context_present (*context) && + (context_domain_id (*context) == domain->id) && + (context_address_width (*context) == domain->agaw) && + (context_address_root(*context) == virt_to_phys(domain->pgd))&& + (
context_translation_type(*context)==CONTEXT_TT_MULTI_LEVEL) && +
(!context_fault_disable(*context))) {drivers/pci/intel-iommu.c
Quoted 2 lines
drivers/pci/intel-iommu.c +2
-0+ spin_unlock_irqrestore(&iommu->lock, flags); + return 0; + }Only need to check
context_present(*context)according to VT-d spec, which says "software must not modify fields other than the Present
(P) field of currently present root-entries or context-entries."Quoted 1 line
Randy (Weidong)
Quoted 9 lines
The logic here is that, if no change is made to the context then just return ok (0). Otherwise, according to the spec, we need to first invalidate the context, flush it, and only then apply the changes to the context.
The other option is to make sure that before every call to this function the context is invalidated, but disabling it inside the function seems safer. do you agree with that?
Quoted 6 lines
After a device can be assigned to guest with VT-d, it needs a context unmap function. When a device is assigned to a guest, map context for it, while when it is detached from a guest, should unmap its context. With the context unmap function, I think we needn't to implement your logic in
domain_context_mapping_one(), instead just check its present. What's your opinion?
Sure, that's fine, this is the other option I mentioned. But we need to add the context unmap function. Something like:
arch/x86/kvm/vtd.c +4 -0
@@ -109,11 +109,17 @@ found:kvm_iommu_unmap_memslots (kvm);return -EFAULT ; } + +kvm_intel_iommu_detach_dev (kvm->arch.domain, + pdev->bus->number, pdev->devfn); +kvm_intel_iommu_context_mapping (kvm->arch.domain, pdev);return 0 ; }EXPORT_SYMBOL_GPL (kvm_iommu_map_guest);
Agree?
Thanks,
Ben
RE: [PATCH] KVM: PCIPT: VT-d: fix context mapping
Quoted 87 lines · Ben-Ami Yassour1 wrote:
"Han, Weidong" <[email protected]> wrote on 19/06/2008 17:18:00:
Quoted 58 lines
Quoted 47 lines · Ben-Ami Yassour
Quoted 44 lines · Weidong
Quoted 32 lines · [email protected] wrote:
From: Ben-Ami Yassour <[email protected]>
When changing the VT-d context mapping, according to the spec, it is required to first set the context to not present, flush and only then apply the new context.
Signed-off-by Ben-Ami Yassour <[email protected]>--- drivers/pci/intel-iommu.c | 17 +++++++++++++++++ 1 files changed, 17 insertions(+), 0 deletions(-)drivers/pci/intel-iommu.c +7
-0+++ b/drivers/pci/intel-iommu.c@@ -56,6 +56,7 @@static void flush_unmaps_timeout (unsigned long data); +static void detach_domain_for_dev (struct dmar_domain *domain,u8 bus,u8 devfn);DEFINE_TIMER (unmap_timer, flush_unmaps_timeout,0 ,0 );@@ -1264,7 +1265,23 @@ static int domain_context_mapping_one(struct dmar_domain *domain, if (!context) return -ENOMEM;spin_lock_irqsave (&iommu->lock, flags); + + if (context_present(*context) && + (context_domain_id(*context) == domain->id) && + (context_address_width(*context) == domain->agaw) && + (context_address_root(*context) ==Quoted 5 lines
virt_to_phys(domain->pgd)) && + (context_translation_type(*context)==CONTEXT_TT_MULTI_LEVEL) &&
+ (!context_fault_disable(*context))) {
+spin_unlock_irqrestore(&iommu->lock, flags); + return
0; + }Only need to check
context_present(*context)according to VT-d spec, which says "software must not modify fields other than the Present (P) field of currently present root-entries or context-entries."Quoted 1 line
Randy (Weidong)
Quoted 9 lines
The logic here is that, if no change is made to the context then just return ok (0). Otherwise, according to the spec, we need to first invalidate the context, flush it, and only then apply the changes to the context.
The other option is to make sure that before every call to this function the context is invalidated, but disabling it inside the function seems safer. do you agree with that?
Quoted 6 lines
After a device can be assigned to guest with VT-d, it needs a context unmap function. When a device is assigned to a guest, map context for it, while when it is detached from a guest, should unmap its context. With the context unmap function, I think we needn't to implement your logic in
domain_context_mapping_one(), instead just check its present. What's your opinion?Sure, that's fine, this is the other option I mentioned. But we need to add the context unmap function. Something like:
arch/x86/kvm/vtd.c +4
-0@@ -109,11 +109,17 @@ found:kvm_iommu_unmap_memslots (kvm);return -EFAULT ; } + +kvm_intel_iommu_detach_dev (kvm->arch.domain, + pdev->bus->number, pdev->devfn); +kvm_intel_iommu_context_mapping (kvm->arch.domain, pdev);return 0 ; }EXPORT_SYMBOL_GPL (kvm_iommu_map_guest);
Quoted 2 lines
Agree?
I don't think it's necessary to add kvm_intel_iommu_detach_dev() here. If a device can be assigned to a guest, it should not be used by other guest (assuming no hotplug support). And also kvm_intel_iommu_detach_dev() is already called in kvm_iommu_unmap_guest(). Normally context won't be present here. Otherwise, there should be some wrong. I attach a patch to change it back to original kernel VT-d code. I think it's correct and clean.
drivers/pci/intel-iommu.c +0 -0
@@ -1264,8 +1264,11 @@ static int domain_context_mapping_one(struct
dmar_domain *domain,
if (!context)
return -ENOMEM;
spin_lock_irqsave(&iommu->lock, flags);
+ if (context_present(*context)) {
+ spin_unlock_irqrestore(&iommu->lock, flags);
+ return 0;
+ }
- context_clear_entry(*context);
context_set_domain_id(*context, domain->id); context_set_address_width(*context, domain->agaw); context_set_address_root(*context, virt_to_phys(domain->pgd));
Randy (Weidong)
Re: [PATCH] KVM: PCIPT: VT-d: fix guest unmap
In reply to Anthony LiguoriQuoted 4 lines · Anthony Liguori
I think the current VT-d code needs some reworking.
We should build the table as the shadow page table gets built. We should suppress iotlb flushes unless the table is actually being updated.
We can't, since we need the iommu tables populated before we issue any dma.
Perhaps we want something like MAP_POPULATE for shadow, which would then affect the iommu tables. Userspace would then do:
mlock()
... set up device assignmentioctl(..., KVM_POPULATE)
Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this signature is too narrow to contain.
Re: [PATCH] KVM: PCIPT: VT-d: fix guest unmap
Quoted 7 lines · Avi Kivity
Quoted 4 lines · Anthony Liguori
I think the current VT-d code needs some reworking.
We should build the table as the shadow page table gets built. We should suppress iotlb flushes unless the table is actually being updated.
We can't, since we need the iommu tables populated before we issue any dma.
Yes, as I've mentioned, the lack of a DMA window notification API can be handled as a special case.
Quoted 5 lines
Perhaps we want something like
MAP_POPULATEfor shadow, which would then affect the iommu tables. Userspace would then do:
mlock()
... set up device assignmentioctl(..., KVM_POPULATE)
Exactly like this :-)
Regards,
Anthony Liguori
RE: [PATCH] KVM: PCIPT: VT-d: fix context mapping
In reply to Han, WeidongQuoted 88 lines · Weidong
Quoted 87 lines · Ben-Ami Yassour1 wrote:
"Han, Weidong" <[email protected]> wrote on 19/06/2008 17:18:00:
Quoted 58 lines
Quoted 47 lines · Ben-Ami Yassour
Quoted 44 lines · Weidong
Quoted 32 lines · [email protected] wrote:
From: Ben-Ami Yassour <[email protected]>
When changing the VT-d context mapping, according to the spec, it is required to first set the context to not present, flush and only then apply the new context.
Signed-off-by Ben-Ami Yassour <[email protected]>--- drivers/pci/intel-iommu.c | 17 +++++++++++++++++ 1 files changed, 17 insertions(+), 0 deletions(-)drivers/pci/intel-iommu.c +7
-0+++ b/drivers/pci/intel-iommu.c@@ -56,6 +56,7 @@static void flush_unmaps_timeout (unsigned long data); +static void detach_domain_for_dev (struct dmar_domain *domain,u8 bus,u8 devfn);DEFINE_TIMER (unmap_timer, flush_unmaps_timeout,0 ,0 );@@ -1264,7 +1265,23 @@ static int domain_context_mapping_one(struct dmar_domain *domain, if (!context) return -ENOMEM;spin_lock_irqsave (&iommu->lock, flags); + + if (context_present(*context) && + (context_domain_id(*context) == domain->id) && + (context_address_width(*context) == domain->agaw) && + (context_address_root(*context) ==Quoted 5 lines
virt_to_phys(domain->pgd)) && + (context_translation_type(*context)==CONTEXT_TT_MULTI_LEVEL) &&
+ (!context_fault_disable(*context))) {
+spin_unlock_irqrestore(&iommu->lock, flags); + return
0; + }Only need to check
context_present(*context)according to VT-d spec, which says "software must not modify fields other than the Present (P) field of currently present root-entries or context-entries."Quoted 1 line
Randy (Weidong)
Quoted 9 lines
The logic here is that, if no change is made to the context then just return ok (0). Otherwise, according to the spec, we need to first invalidate the context, flush it, and only then apply the changes to the context.
The other option is to make sure that before every call to this function the context is invalidated, but disabling it inside the function seems safer. do you agree with that?
Quoted 6 lines
After a device can be assigned to guest with VT-d, it needs a context unmap function. When a device is assigned to a guest, map context for it, while when it is detached from a guest, should unmap its context. With the context unmap function, I think we needn't to implement your logic in
domain_context_mapping_one(), instead just check its present. What's your opinion?Sure, that's fine, this is the other option I mentioned. But we need to add the context unmap function. Something like:
arch/x86/kvm/vtd.c +4
-0@@ -109,11 +109,17 @@ found:kvm_iommu_unmap_memslots (kvm);return -EFAULT ; } + +kvm_intel_iommu_detach_dev (kvm->arch.domain, + pdev->bus->number, pdev->devfn); +kvm_intel_iommu_context_mapping (kvm->arch.domain, pdev);return 0 ; }EXPORT_SYMBOL_GPL (kvm_iommu_map_guest);
Quoted 10 lines
Quoted 2 lines
Agree?
I don't think it's necessary to add
kvm_intel_iommu_detach_dev()here. If a device can be assigned to a guest, it should not be used by other guest (assuming no hotplug support). And alsokvm_intel_iommu_detach_dev()is already called inkvm_iommu_unmap_guest(). Normally context won't be present here. Otherwise, there should be some wrong. I attach a patch to change it back to original kernel VT-d code. I think it's correct and clean.
The problem is with the following scenario:
1. load the host NIC driver
2. unload the host NIC driver
3. start kvm with passthrough for that NIC
In this case the context is not cleaned, and we get VT-d failures. I agree with changing the VT-d driver code back to original. But we do need:
arch/x86/kvm/vtd.c +4 -0
@@ -109,11 +109,17 @@ found:kvm_iommu_unmap_memslots (kvm);return -EFAULT ; } + +kvm_intel_iommu_detach_dev (kvm->arch.domain, + pdev->bus->number, pdev->devfn); +kvm_intel_iommu_context_mapping (kvm->arch.domain, pdev);return 0 ; }EXPORT_SYMBOL_GPL (kvm_iommu_map_guest);
Otherwise kvm_intel_iommu_context_mapping will exit on the context_present(*context) check.
This means that the context is still going to point to the iova of the host NIC.
If you do not agree, please explain what code path clears the context written for the host NIC driver in this scenario (note that empirically I see that my assumption is correct).
Thanks,
Ben
Quoted 28 lines
drivers/pci/intel-iommu.c +4
-1@@ -1264,8 +1264,11 @@ static int domain_context_mapping_one(struct dmar_domain *domain, if (!context) return -ENOMEM;spin_lock_irqsave (&iommu->lock, flags); +if (context_present (*context)) { +spin_unlock_irqrestore (&iommu->lock, flags); +return 0 ; + } -context_clear_entry (*context);context_set_domain_id (*context, domain->id);context_set_address_width (*context, domain->agaw);
context_set_address_root(*context, virt_to_phys(domain->pgd));Randy (Weidong)
--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
RE: [PATCH] KVM: PCIPT: VT-d: fix context mapping
Quoted 124 lines · Ben-Ami Yassour
Quoted 86 lines · Weidong
Quoted 85 lines · Ben-Ami Yassour1 wrote:
"Han, Weidong" <[email protected]> wrote on 19/06/2008 17:18:00:
Quoted 58 lines
Quoted 47 lines · Ben-Ami Yassour
Quoted 44 lines · Weidong
Quoted 32 lines · [email protected] wrote:
From: Ben-Ami Yassour <[email protected]>
When changing the VT-d context mapping, according to the spec, it is required to first set the context to not present, flush and only then apply the new context.
Signed-off-by Ben-Ami Yassour <[email protected]>--- drivers/pci/intel-iommu.c | 17 +++++++++++++++++ 1 files changed, 17 insertions(+), 0 deletions(-)(quoted diff) +0
-0
b/drivers/pci/intel-iommu.cindex 930874f..dcdfa97 100644 ---a/drivers/pci/intel-iommu.c+++b/drivers/pci/intel-iommu.c(quoted diff) +6
-0@@ -56,6 +56,7 @@ static void flush_unmaps_timeout(unsigned long data); +static void detach_domain_for_dev(struct dmar_domain *domain, u8 bus, u8 devfn); DEFINE_TIMER(unmap_timer, flush_unmaps_timeout, 0, 0);@@ -1264,7 +1265,23 @@ static int domain_context_mapping_one(struct dmar_domain *domain, if (!context) return -ENOMEM; spin_lock_irqsave(&iommu->lock, flags); + + if (context_present(*context) && + (context_domain_id(*context) == domain->id) && + (context_address_width(*context) == domain->agaw) && + (context_address_root(*context) ==Quoted 5 lines
virt_to_phys(domain->pgd)) && + (context_translation_type(*context)==CONTEXT_TT_MULTI_LEVEL) && + (!context_fault_disable(*context))) {
+spin_unlock_irqrestore(&iommu->lock, flags); +
return 0; + }Only need to check
context_present(*context)according to VT-d spec, which says "software must not modify fields other than the Present (P) field of currently present root-entries or context-entries."Quoted 1 line
Randy (Weidong)
Quoted 9 lines
The logic here is that, if no change is made to the context then just return ok (0). Otherwise, according to the spec, we need to first invalidate the context, flush it, and only then apply the changes to the context.
The other option is to make sure that before every call to this function the context is invalidated, but disabling it inside the function seems safer. do you agree with that?
Quoted 6 lines
After a device can be assigned to guest with VT-d, it needs a context unmap function. When a device is assigned to a guest, map context for it, while when it is detached from a guest, should unmap its context. With the context unmap function, I think we needn't to implement your logic in
domain_context_mapping_one(), instead just check its present. What's your opinion?Sure, that's fine, this is the other option I mentioned. But we need to add the context unmap function. Something like:
arch/x86/kvm/vtd.c +3
-0@@ -109,11 +109,17 @@ found:kvm_iommu_unmap_memslots (kvm);return -EFAULT ; } + +kvm_intel_iommu_detach_dev (kvm->arch.domain, + pdev->bus->number, pdev->devfn); +kvm_intel_iommu_context_mapping (kvm->arch.domain, pdev);return 0 ; }EXPORT_SYMBOL_GPL (kvm_iommu_map_guest);Quoted 10 lines
Quoted 2 lines
Agree?
I don't think it's necessary to add
kvm_intel_iommu_detach_dev()here. If a device can be assigned to a guest, it should not be used by other guest (assuming no hotplug support). And alsokvm_intel_iommu_detach_dev()is already called inkvm_iommu_unmap_guest(). Normally context won't be present here. Otherwise, there should be some wrong. I attach a patch to change it back to original kernel VT-d code. I think it's correct and clean.The problem is with the following scenario:
1. load the host NIC driver
2. unload the host NIC driver
3. start kvm with passthrough for that NICIn this case the context is not cleaned, and we get VT-d failures. I agree with changing the VT-d driver code back to original. But we do need:
arch/x86/kvm/vtd.c +4
-0@@ -109,11 +109,17 @@ found:kvm_iommu_unmap_memslots (kvm);return -EFAULT ; } + +kvm_intel_iommu_detach_dev (kvm->arch.domain, + pdev->bus->number, pdev->devfn); +kvm_intel_iommu_context_mapping (kvm->arch.domain, pdev);return 0 ; }EXPORT_SYMBOL_GPL (kvm_iommu_map_guest);
Quoted 8 lines
Otherwise kvm_intel_iommu_context_mapping will exit on the context_present(*context) check.
This means that the context is still going to point to the iova of the host NIC.
If you do not agree, please explain what code path clears the context written for the host NIC driver in this scenario (note that empirically I see that my assumption is correct).
Yes, in this load/unload case, kvm_intel_iommu_detach_dev() is necessary, because the device will be mapped in host VT-d page table when its driver is loaded, but it is not unmapped when the driver is unloaded.
Randy (Weidong)