Skip to contentSkip to conversation

iommu: Don't call .probe_finalize() under group->mutex

PATCH In review Joerg Roedel 2 participants 2 messages May 19, 2020 – May 20, 2020

Subsystems (2)
lore
Share & tools
lore

Joerg Roedel

author maintainer
#0 lore raw

PATCH 1/1 iommu: Don't call .probe_finalize() under group->mutex

From: Joerg Roedel <[email protected]>

The .probe_finalize() call-back of some IOMMU drivers calls into arm_iommu_attach_device(). This function will call back into the IOMMU core code, where it tries to take group->mutex again, resulting in a deadlock.

As there is no reason why .probe_finalize() needs to be called under that mutex, move it after the lock has been released to fix the deadlock.

Cc Yong Wu <[email protected]>
Fixes deac0b3bed26 ("iommu: Split off default domain allocation from group assignment")
---
 drivers/iommu/iommu.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)
drivers/iommu/iommu.c +18 -10
index 629d209b8e88..d5d9fcbc9714 100644
@@ -1683,17 +1683,8 @@ static void probe_alloc_default_domain(struct bus_type *bus,
 static int iommu_group_do_dma_attach(struct device *dev, void *data)
 {
 	struct iommu_domain *domain = data;
-	const struct iommu_ops *ops;
-	int ret;
-
-	ret = __iommu_attach_device(domain, dev);
-
-	ops = domain->ops;
-
-	if (ret == 0 && ops->probe_finalize)
-		ops->probe_finalize(dev);
 
-	return ret;
+	return __iommu_attach_device(domain, dev);
 }
 
 static int __iommu_group_dma_attach(struct iommu_group *group)
@@ -1702,6 +1693,21 @@ static int __iommu_group_dma_attach(struct iommu_group *group)
 					  iommu_group_do_dma_attach);
 }
 
+static int iommu_group_do_probe_finalize(struct device *dev, void *data)
+{
+	struct iommu_domain *domain = data;
+
+	if (domain->ops->probe_finalize)
+		domain->ops->probe_finalize(dev);
+
+	return 0;
+}
+
+static void __iommu_group_dma_finalize(struct iommu_group *group)
+{
+	__iommu_group_for_each_dev(group, group->default_domain,
+				   iommu_group_do_probe_finalize);
+}
 static int iommu_do_create_direct_mappings(struct device *dev, void *data)
 {
 	struct iommu_group *group = data;
@@ -1754,6 +1760,8 @@ int bus_iommu_probe(struct bus_type *bus)
 
 		if (ret)
 			break;
+
+		__iommu_group_dma_finalize(group);
 	}
 
 	return ret;
Signature · 1 line
2.25.1
Tested-by on v1 1/1
#1 lore raw
Quoted 7 lines · Joerg Roedel

From: Joerg Roedel <[email protected]>

The .probe_finalize() call-back of some IOMMU drivers calls into arm_iommu_attach_device(). This function will call back into the IOMMU core code, where it tries to take group->mutex again, resulting in a deadlock.

Quoted 8 lines

As there is no reason why .probe_finalize() needs to be called under that mutex, move it after the lock has been released to fix the deadlock.

Cc Yong Wu <[email protected]>
Fixes deac0b3bed26 ("iommu: Split off default domain allocation from group assignment")

Tested on MediaTek-v1 mt2701 evb board.