From: Jean Guyader Subject: Re: IOMMU faults Date: Thu, 16 Jun 2011 10:47:57 +0100 Message-ID: <20110616094757.GG12480@spongy.cam.xci-test.com> References: <20110616092509.GH17634@whitby.uk.xensource.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="EeQfGwPcQSOJBaQU" Return-path: Content-Disposition: inline In-Reply-To: <20110616092509.GH17634@whitby.uk.xensource.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: Tim Deegan Cc: Wei Wang , "xen-devel@lists.xensource.com" , Allen Kay , Jean Guyader List-Id: xen-devel@lists.xenproject.org --EeQfGwPcQSOJBaQU Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline On 16/06 10:25, Tim Deegan wrote: > Hi, IOMMU maintainers, > > What should Xen do when an IOMMU fault happens? As far as I can > see both the AMD and Intel code clears the error in the IOMMU and > carries on, but I suspect some more vigorous action is appropriate. > I've seen traces from an Intel machine that seemed to be livelocked on > IOMMU faults from a passed-through VGA card, until it was killed by the > watchdog. I think I can see two things that contribute to that: > > - The Intel IOMMU fault handler prints quite a lot of info in interrupt > context, making it easier to livelock. Still I think the general > problem applies on AMD too. > - Domain destruction re-assigns passed though cards to dom0, but the > cards don't seem to get reset. So there's nothing to stop a card > battering away at DMA in the meantime. That seems like a problem > independent of livelock, actually. > > In any case, it seems like it would be a good idea to stop a > broken/malicious/deassigned card from flooding Xen with IOMMU faults. > > I was considering just writing 0 to the faulting card's PCI command > register, but I'm told that's not always enough to properly deactivate > a card, and it might be a little over-zealous to do it on the first > offence. > > Ideas? > Hi Tim, We have seed such behavior when we were testing GPU assignement especially the Intel GPU. The problem is that domain destruction in Xen is assynchronous and right now the pci device reset is done in dom0 with some help of the toolstack. In the Intel GPU case we need to make sure that the guest memory and the IOMMU are still in place while we perform to reset otherwise the device drift into an unstable state. There is probably other ways to do that in a cleaner way but we decided to move the pci reset code into Xen, so we are sure we perform the reset while the device is in a known state (functionning state). Attached is the patch we have in XenClient that move the pci reset into Xen. The modifications we have made to the VT-d code should go in the IOMMU generic section. I appologise but this patch is based on Xen 3.4, if we think this is the right way to do it, I can submit a proper patch against unstable and 4.1. Regards, Jean --EeQfGwPcQSOJBaQU Content-Type: text/plain; charset="us-ascii" Content-Disposition: attachment; filename="iommu-vtd-pci-reset-on-reassign" diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index feda3ba..2f5a69a 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -510,8 +510,6 @@ void arch_domain_destroy(struct domain *d) pci_release_devices(d); free_domain_pirqs(d); - if ( !is_idle_domain(d) ) - iommu_domain_destroy(d); paging_final_teardown(d); diff --git a/xen/common/domain.c b/xen/common/domain.c index 32f1d48..a7b0207 100644 --- a/xen/common/domain.c +++ b/xen/common/domain.c @@ -385,6 +385,7 @@ int domain_kill(struct domain *d) v4v_destroy(d); evtchn_destroy(d); gnttab_release_mappings(d); + iommu_domain_destroy(d); /* fallthrough */ case DOMDYING_dying: rc = domain_relinquish_resources(d); diff --git a/xen/drivers/passthrough/pci.c b/xen/drivers/passthrough/pci.c index 723cff0..e90a335 100644 --- a/xen/drivers/passthrough/pci.c +++ b/xen/drivers/passthrough/pci.c @@ -75,6 +75,81 @@ struct pci_dev *pci_get_pdev(int bus, int devfn) return NULL; } +struct pci_dev *pci_get_bridge(u8 secondary) +{ + struct pci_dev *pdev = NULL, *m = NULL; + + spin_lock(&pcidevs_lock); + + list_for_each_entry ( pdev, &alldevs_list, alldevs_list ) + { + u8 type = pci_conf_read8(pdev->bus, + PCI_SLOT(pdev->devfn), + PCI_FUNC(pdev->devfn), + PCI_HEADER_TYPE); + if ( (type & 3) == PCI_HEADER_TYPE_BRIDGE ) + { + u8 sec = pci_conf_read8(pdev->bus, + PCI_SLOT(pdev->devfn), + PCI_FUNC(pdev->devfn), + PCI_SECONDARY_BUS); + if ( sec == secondary ) + { + m = pdev; + goto out; + } + } + } +out: + spin_unlock(&pcidevs_lock); + return m; +} + +struct pci_dev *pci_get_gfx(struct domain *d) +{ + struct pci_dev *pdev = NULL, *m = NULL; + + spin_lock(&pcidevs_lock); + + list_for_each_entry ( pdev, &alldevs_list, alldevs_list ) + { + if ( d == NULL || pdev->domain == d ) + { + u16 class = pci_conf_read16(pdev->bus, + PCI_SLOT(pdev->devfn), + PCI_FUNC(pdev->devfn), + PCI_CLASS_DEVICE); + + if ( class == 0x0300 ) + { + m = pdev; + goto out; + } + } + } +out: + spin_unlock(&pcidevs_lock); + return m; + +} + +struct pci_dev *pci_match_pdev(int (*match)(struct pci_dev *)) +{ + struct pci_dev *pdev = NULL, *m = NULL; + + spin_lock(&pcidevs_lock); + + list_for_each_entry ( pdev, &alldevs_list, alldevs_list ) + if ( match(pdev) ) + { + m = pdev; + goto out; + } +out: + spin_unlock(&pcidevs_lock); + return m; +} + struct pci_dev *pci_get_pdev_by_domain(struct domain *d, int bus, int devfn) { struct pci_dev *pdev = NULL; diff --git a/xen/drivers/passthrough/vtd/Makefile b/xen/drivers/passthrough/vtd/Makefile index 0e6f163..00ba797 100644 --- a/xen/drivers/passthrough/vtd/Makefile +++ b/xen/drivers/passthrough/vtd/Makefile @@ -6,3 +6,4 @@ obj-y += dmar.o obj-y += utils.o obj-y += qinval.o obj-y += intremap.o +obj-y += pci_reset.o diff --git a/xen/drivers/passthrough/vtd/iommu.c b/xen/drivers/passthrough/vtd/iommu.c index 687f3fd..df88dc7 100644 --- a/xen/drivers/passthrough/vtd/iommu.c +++ b/xen/drivers/passthrough/vtd/iommu.c @@ -21,7 +21,6 @@ #include #include -#include #include #include #include @@ -32,11 +31,13 @@ #include #include #include +#include #include #include "iommu.h" #include "dmar.h" #include "extern.h" #include "vtd.h" +#include "pci_reset.h" #define domain_iommu_domid(d) ((d)->arch.hvm_domain.hvm_iommu.iommu_domid) @@ -1503,6 +1504,15 @@ static int reassign_device_ownership( if ( (drhd = acpi_find_matched_drhd_unit(pdev)) == NULL ) return -ENODEV; + + /* Only do the reset here if target isn't dom0. + ** For the reassign to dom0 it's done in the iommu_domain_teardown + ** function because reassign_device_ownership happen too late in + ** the process. + */ + if (target->domain_id != 0) + pci_reset_device(bus, devfn); + pdev_iommu = drhd->iommu; domain_context_unmap(source, bus, devfn); @@ -1534,10 +1544,14 @@ static int reassign_device_ownership( void iommu_domain_teardown(struct domain *d) { struct hvm_iommu *hd = domain_hvm_iommu(d); + struct pci_dev *pdev = NULL; if ( list_empty(&acpi_drhd_units) ) return; + if ((pdev = pci_get_gfx(d))) + pci_reset_device(pdev->bus, pdev->devfn); + spin_lock(&hd->mapping_lock); iommu_free_pagetable(hd->pgd_maddr, agaw_to_level(hd->agaw)); hd->pgd_maddr = 0; diff --git a/xen/include/xen/pci.h b/xen/include/xen/pci.h index 6827e0d..9ed6f60 100644 --- a/xen/include/xen/pci.h +++ b/xen/include/xen/pci.h @@ -77,6 +77,9 @@ int pci_remove_device(u8 bus, u8 devfn); int pci_add_device_ext(u8 bus, u8 devfn, struct pci_dev_info *info); struct pci_dev *pci_get_pdev(int bus, int devfn); struct pci_dev *pci_get_pdev_by_domain(struct domain *d, int bus, int devfn); +struct pci_dev *pci_match_pdev(int (*match)(struct pci_dev *)); +struct pci_dev *pci_get_bridge(u8 secondary); +struct pci_dev *pci_get_gfx(struct domain *d); uint8_t pci_conf_read8( unsigned int bus, unsigned int dev, unsigned int func, unsigned int reg); diff --git a/xen/include/xen/pci_regs.h b/xen/include/xen/pci_regs.h index 361554c..20b00e8 100644 --- a/xen/include/xen/pci_regs.h +++ b/xen/include/xen/pci_regs.h @@ -210,6 +210,7 @@ #define PCI_CAP_ID_AGP3 0x0E /* AGP Target PCI-PCI bridge */ #define PCI_CAP_ID_EXP 0x10 /* PCI Express */ #define PCI_CAP_ID_MSIX 0x11 /* MSI-X */ +#define PCI_CAP_ID_AF 0x13 /* PCI Advanced Features */ #define PCI_CAP_LIST_NEXT 1 /* Next capability in the list */ #define PCI_CAP_FLAGS 2 /* Capability defined flags (16 bits) */ #define PCI_CAP_SIZEOF 4 @@ -315,6 +316,17 @@ #define PCI_CHSWP_EXT 0x40 /* ENUM# status - extraction */ #define PCI_CHSWP_INS 0x80 /* ENUM# status - insertion */ +/* PCI Advanced Feature registers */ + +#define PCI_AF_LENGTH 2 +#define PCI_AF_CAP 3 +#define PCI_AF_CAP_TP 0x01 +#define PCI_AF_CAP_FLR 0x02 +#define PCI_AF_CTRL 4 +#define PCI_AF_CTRL_FLR 0x01 +#define PCI_AF_STATUS 5 +#define PCI_AF_STATUS_TP 0x01 + /* PCI-X registers */ #define PCI_X_CMD 2 /* Modes & Features */ --EeQfGwPcQSOJBaQU Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --EeQfGwPcQSOJBaQU--