linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] iommu/amd: Fix IOMMU page flush when detach all devices from a domain
@ 2019-01-16  4:16 Suthikulpanit, Suravee
  2019-01-16 13:26 ` joro
  0 siblings, 1 reply; 13+ messages in thread
From: Suthikulpanit, Suravee @ 2019-01-16  4:16 UTC (permalink / raw)
  To: linux-kernel, iommu
  Cc: joro, Suthikulpanit, Suravee, Boris Ostrovsky, Singh, Brijesh

From: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>

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 always issuing the IOMMU pages invalidate command when
device count is zero, which is the case when detaching all the devices
from the domain.

Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Signed-off-by: Brijesh Singh <brijesh.singh@amd.com>
Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
---
 drivers/iommu/amd_iommu.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/iommu/amd_iommu.c b/drivers/iommu/amd_iommu.c
index 525659b88ade..ab31ba75da1b 100644
--- a/drivers/iommu/amd_iommu.c
+++ b/drivers/iommu/amd_iommu.c
@@ -1248,7 +1248,13 @@ static void __domain_flush_pages(struct protection_domain *domain,
 	build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
 
 	for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
-		if (!domain->dev_iommu[i])
+		/*
+		 * The dev_cnt is zero when all devices are detached
+		 * from the domain. This is the case when VFIO detaches
+		 * all devices from the group before flushing IOMMU pages.
+		 * So, always issue the flush command.
+		 */
+		if (domain->dev_cnt && !domain->dev_iommu[i])
 			continue;
 
 		/*
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2019-01-24  8:04 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-16  4:16 [PATCH] iommu/amd: Fix IOMMU page flush when detach all devices from a domain Suthikulpanit, Suravee
2019-01-16 13:26 ` joro
2019-01-16 14:08   ` Suthikulpanit, Suravee
2019-01-16 17:08     ` joro
2019-01-17  8:44       ` Suthikulpanit, Suravee
2019-01-17 11:48         ` Suthikulpanit, Suravee
2019-01-22 10:44         ` joro
2019-01-22 15:53           ` Suthikulpanit, Suravee
2019-01-23  7:56             ` joro
2019-01-24  3:25               ` Suthikulpanit, Suravee
2019-01-24  8:04                 ` joro
2019-01-16 14:40   ` Suthikulpanit, Suravee
2019-01-16 17:09     ` joro

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).