All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 1/3] iommu: Attach device group to old domain in error path
@ 2023-02-15  5:26 Vasant Hegde
  2023-02-15  5:26 ` [PATCH v3 2/3] iommu/amd: Skip attach device domain is same as new domain Vasant Hegde
                   ` (3 more replies)
  0 siblings, 4 replies; 14+ messages in thread
From: Vasant Hegde @ 2023-02-15  5:26 UTC (permalink / raw)
  To: iommu, joro, baolu.lu
  Cc: suravee.suthikulpanit, robin.murphy, will, Vasant Hegde

iommu_attach_group() attaches all devices in a group to domain and then
sets group domain (group->domain). Current code (__iommu_attach_group())
does not handle error path. This creates problem as devices to domain
attachment is in inconsistent state.

Flow:
  - During boot iommu attach devices to default domain
  - Later some device driver (like amd/iommu_v2 or vfio) tries to attach
    device to new domain.
  - In iommu_attach_group() path we detach device from current domain.
    Then it tries to attach devices to new domain.
  - If it fails to attach device to new domain then device to domain link
    is broken.
  - iommu_attach_group() returns error.
  - At this stage iommu_attach_group() caller thinks, attaching device to
    new domain failed and devices are still attached to old domain.
  - But in reality device to old domain link is broken. It will result
    in all sort of failures (like IO page fault) later.

To recover from this situation, we need to attach all devices back to the
old domain. Also log warning if it fails attach device back to old domain.

Suggested-by: Lu Baolu <baolu.lu@linux.intel.com>
Signed-off-by: Vasant Hegde <vasant.hegde@amd.com>
---
Robin, Joerg,
  As explained in previous version [1], I still kept the changes in
  iommu core layer. I hope this is fine with you.

[1] https://lore.kernel.org/linux-iommu/f2151603-de54-7717-60f9-748e9205139e@amd.com/

-Vasant

 drivers/iommu/iommu.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index de91dd88705b..e3336a11b6b9 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -2124,8 +2124,22 @@ static int __iommu_attach_group(struct iommu_domain *domain,
 
 	ret = __iommu_group_for_each_dev(group, domain,
 					 iommu_group_do_attach_device);
-	if (ret == 0)
+	if (ret == 0) {
 		group->domain = domain;
+	} else {
+		/*
+		 * To recover from the case when certain device within the
+		 * group fails to attach to the new domain, we need force
+		 * attaching all devices back to the old domain. The old
+		 * domain is compatible for all devices in the group,
+		 * hence the iommu driver should always return success.
+		 */
+		struct iommu_domain *old_domain = group->domain;
+
+		group->domain = NULL;
+		WARN(__iommu_group_set_domain(group, old_domain),
+		     "iommu driver failed to attach a compatible domain");
+	}
 
 	return ret;
 }
-- 
2.31.1


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

end of thread, other threads:[~2023-03-13  6:29 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-02-15  5:26 [PATCH v3 1/3] iommu: Attach device group to old domain in error path Vasant Hegde
2023-02-15  5:26 ` [PATCH v3 2/3] iommu/amd: Skip attach device domain is same as new domain Vasant Hegde
2023-02-15  5:26 ` [PATCH v3 3/3] iommu/amd: Improve page fault error reporting Vasant Hegde
2023-02-16  9:41   ` Thorsten Leemhuis
2023-02-17  5:56     ` Vasant Hegde
2023-02-17  6:11       ` Linux regression tracking (Thorsten Leemhuis)
2023-02-16 10:18   ` Joerg Roedel
2023-02-17  5:53 ` [PATCH v3 1/3] iommu: Attach device group to old domain in error path Vasant Hegde
2023-02-18 14:42   ` Joerg Roedel
2023-02-19 10:17     ` Vasant Hegde
2023-03-12 14:42   ` Linux regression tracking (Thorsten Leemhuis)
2023-03-13  4:29     ` Vasant Hegde
2023-03-13  6:29       ` Linux regression tracking (Thorsten Leemhuis)
2023-02-17 13:44 ` Jason Gunthorpe

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.