From 3221f270cf2eba0a22fd4f92319d664eacb92889 Mon Sep 17 00:00:00 2001 From: Jan Beulich Date: Tue, 5 Apr 2022 14:16:10 +0200 Subject: [PATCH] VT-d: fix add/remove ordering when RMRRs are in use MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit In the event that the RMRR mappings are essential for device operation, they should be established before updating the device's context entry, while they should be torn down only after the device's context entry was successfully cleared. Also switch to %pd in related log messages. Fixes: fa88cfadf918 ("vt-d: Map RMRR in intel_iommu_add_device() if the device has RMRR") Fixes: 8b99f4400b69 ("VT-d: fix RMRR related error handling") Signed-off-by: Jan Beulich Reviewed-by: Roger Pau Monné Reviewed-by: Kevin Tian --- xen/drivers/passthrough/vtd/iommu.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/xen/drivers/passthrough/vtd/iommu.c b/xen/drivers/passthrough/vtd/iommu.c index 752024ee10..06ba21aad8 100644 --- a/xen/drivers/passthrough/vtd/iommu.c +++ b/xen/drivers/passthrough/vtd/iommu.c @@ -1992,14 +1992,6 @@ static int cf_check intel_iommu_add_device(u8 devfn, struct pci_dev *pdev) if ( !pdev->domain ) return -EINVAL; - ret = domain_context_mapping(pdev->domain, devfn, pdev); - if ( ret ) - { - dprintk(XENLOG_ERR VTDPREFIX, "d%d: context mapping failed\n", - pdev->domain->domain_id); - return ret; - } - for_each_rmrr_device ( rmrr, bdf, i ) { if ( rmrr->segment == pdev->seg && @@ -2016,12 +2008,17 @@ static int cf_check intel_iommu_add_device(u8 devfn, struct pci_dev *pdev) rmrr->base_address, rmrr->end_address, 0); if ( ret ) - dprintk(XENLOG_ERR VTDPREFIX, "d%d: RMRR mapping failed\n", - pdev->domain->domain_id); + dprintk(XENLOG_ERR VTDPREFIX, "%pd: RMRR mapping failed\n", + pdev->domain); } } - return 0; + ret = domain_context_mapping(pdev->domain, devfn, pdev); + if ( ret ) + dprintk(XENLOG_ERR VTDPREFIX, "%pd: context mapping failed\n", + pdev->domain); + + return ret; } static int cf_check intel_iommu_enable_device(struct pci_dev *pdev) @@ -2043,11 +2040,15 @@ static int cf_check intel_iommu_remove_device(u8 devfn, struct pci_dev *pdev) { struct acpi_rmrr_unit *rmrr; u16 bdf; - int i; + int ret, i; if ( !pdev->domain ) return -EINVAL; + ret = domain_context_unmap(pdev->domain, devfn, pdev); + if ( ret ) + return ret; + for_each_rmrr_device ( rmrr, bdf, i ) { if ( rmrr->segment != pdev->seg || @@ -2063,7 +2064,7 @@ static int cf_check intel_iommu_remove_device(u8 devfn, struct pci_dev *pdev) rmrr->end_address, 0); } - return domain_context_unmap(pdev->domain, devfn, pdev); + return 0; } static int __hwdom_init cf_check setup_hwdom_device( -- 2.30.2