Commit ef41aa7e authored by Suravee Suthikulpanit's avatar Suravee Suthikulpanit Committed by Greg Kroah-Hartman

iommu/amd: Fix IOMMU page flush when detach device from a domain

[ Upstream commit 9825bd94 ]

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: default avatarJoerg Roedel <joro@8bytes.org>
Co-developed-by: default avatarBrijesh Singh <brijesh.singh@amd.com>
Signed-off-by: default avatarBrijesh Singh <brijesh.singh@amd.com>
Signed-off-by: default avatarSuravee Suthikulpanit <suravee.suthikulpanit@amd.com>
Fixes: 6de8ad9b ('x86/amd-iommu: Make iommu_flush_pages aware of multiple IOMMUs')
Signed-off-by: default avatarJoerg Roedel <jroedel@suse.de>
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
parent 34f02c1d
...@@ -1919,6 +1919,7 @@ static void do_attach(struct iommu_dev_data *dev_data, ...@@ -1919,6 +1919,7 @@ static void do_attach(struct iommu_dev_data *dev_data,
static void do_detach(struct iommu_dev_data *dev_data) static void do_detach(struct iommu_dev_data *dev_data)
{ {
struct protection_domain *domain = dev_data->domain;
struct amd_iommu *iommu; struct amd_iommu *iommu;
u16 alias; u16 alias;
...@@ -1934,10 +1935,6 @@ static void do_detach(struct iommu_dev_data *dev_data) ...@@ -1934,10 +1935,6 @@ static void do_detach(struct iommu_dev_data *dev_data)
iommu = amd_iommu_rlookup_table[dev_data->devid]; iommu = amd_iommu_rlookup_table[dev_data->devid];
alias = dev_data->alias; alias = dev_data->alias;
/* decrease reference counters */
dev_data->domain->dev_iommu[iommu->index] -= 1;
dev_data->domain->dev_cnt -= 1;
/* Update data structures */ /* Update data structures */
dev_data->domain = NULL; dev_data->domain = NULL;
list_del(&dev_data->list); list_del(&dev_data->list);
...@@ -1947,6 +1944,16 @@ static void do_detach(struct iommu_dev_data *dev_data) ...@@ -1947,6 +1944,16 @@ static void do_detach(struct iommu_dev_data *dev_data)
/* Flush the DTE entry */ /* Flush the DTE entry */
device_flush_dte(dev_data); device_flush_dte(dev_data);
/* Flush IOTLB */
domain_flush_tlb_pde(domain);
/* Wait for the flushes to finish */
domain_flush_complete(domain);
/* decrease reference counters - needs to happen after the flushes */
domain->dev_iommu[iommu->index] -= 1;
domain->dev_cnt -= 1;
} }
/* /*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment