Skip to content

Commit 9974f32

Browse files
ssuthiku-amdgregkh
authored andcommitted
iommu/amd: Fix IOMMU page flush when detach device from a domain
[ Upstream commit 9825bd9 ] 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> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 81f097d commit 9974f32

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

drivers/iommu/amd_iommu.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,6 +1982,7 @@ static void do_attach(struct iommu_dev_data *dev_data,
19821982

19831983
static void do_detach(struct iommu_dev_data *dev_data)
19841984
{
1985+
struct protection_domain *domain = dev_data->domain;
19851986
struct amd_iommu *iommu;
19861987
u16 alias;
19871988

@@ -1997,10 +1998,6 @@ static void do_detach(struct iommu_dev_data *dev_data)
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)