Skip to content

Commit 9825bd9

Browse files
ssuthiku-amdjoergroedel
authored andcommitted
iommu/amd: Fix IOMMU page flush when detach device from a domain
When a VM is terminated, the VFIO driver detaches all pass-through devices from VFIO domain by clearing domain id and page table root pointer from each device table entry (DTE), and then invalidates the DTE. Then, the VFIO driver unmap pages and invalidate IOMMU pages. Currently, the IOMMU driver keeps track of which IOMMU and how many devices are attached to the domain. When invalidate IOMMU pages, the driver checks if the IOMMU is still attached to the domain before issuing the invalidate page command. However, since VFIO has already detached all devices from the domain, the subsequent INVALIDATE_IOMMU_PAGES commands are being skipped as there is no IOMMU attached to the domain. This results in data corruption and could cause the PCI device to end up in indeterministic state. Fix this by invalidate IOMMU pages when detach a device, and before decrementing the per-domain device reference counts. Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com> Suggested-by: Joerg Roedel <joro@8bytes.org> Co-developed-by: Brijesh Singh <brijesh.singh@amd.com> Signed-off-by: Brijesh Singh <brijesh.singh@amd.com> Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com> Fixes: 6de8ad9 ('x86/amd-iommu: Make iommu_flush_pages aware of multiple IOMMUs') Signed-off-by: Joerg Roedel <jroedel@suse.de>
1 parent da5d274 commit 9825bd9

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

drivers/iommu/amd_iommu.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,16 +1991,13 @@ static void do_attach(struct iommu_dev_data *dev_data,
19911991

19921992
static void do_detach(struct iommu_dev_data *dev_data)
19931993
{
1994+
struct protection_domain *domain = dev_data->domain;
19941995
struct amd_iommu *iommu;
19951996
u16 alias;
19961997

19971998
iommu = amd_iommu_rlookup_table[dev_data->devid];
19981999
alias = dev_data->alias;
19992000

2000-
/* decrease reference counters */
2001-
dev_data->domain->dev_iommu[iommu->index] -= 1;
2002-
dev_data->domain->dev_cnt -= 1;
2003-
20042001
/* Update data structures */
20052002
dev_data->domain = NULL;
20062003
list_del(&dev_data->list);
@@ -2010,6 +2007,16 @@ static void do_detach(struct iommu_dev_data *dev_data)
20102007

20112008
/* Flush the DTE entry */
20122009
device_flush_dte(dev_data);
2010+
2011+
/* Flush IOTLB */
2012+
domain_flush_tlb_pde(domain);
2013+
2014+
/* Wait for the flushes to finish */
2015+
domain_flush_complete(domain);
2016+
2017+
/* decrease reference counters - needs to happen after the flushes */
2018+
domain->dev_iommu[iommu->index] -= 1;
2019+
domain->dev_cnt -= 1;
20132020
}
20142021

20152022
/*

0 commit comments

Comments
 (0)