Skip to contentSkip to conversation

iommu/tegra-smmu: Reference count fixes

PATCH Applied Thierry Reding 2 participants 5 messages Aug 6, 2020 – Sep 4, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover iommu/tegra-smmu: Reference count fixes

From: Thierry Reding <[email protected]>

I was running into some use-after-free conditions after recent changes to the host1x driver cause the child devices to be destroyed upon driver unloading. This in turn caused the IOMMU groups associated with the child devices to also get released and that uncovered a subtle reference count unbalance.

This contains two fixes for these issues and also includes a patch that sets the IOMMU group name for "static" groups to help with debugging.

Thierry

Thierry Reding (3):
iommu/tegra-smmu: Set IOMMU group name iommu/tegra-smmu: Balance IOMMU group reference count iommu/tegra-smmu: Prune IOMMU group when it is released

 drivers/iommu/tegra-smmu.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
Signature · 6 lines
2.27.0

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#1 lore raw

PATCH 1/3 iommu/tegra-smmu: Set IOMMU group name

From: Thierry Reding <[email protected]>

Set the name of static IOMMU groups to help with debugging.

---
 drivers/iommu/tegra-smmu.c | 1 +
 1 file changed, 1 insertion(+)
drivers/iommu/tegra-smmu.c +1 -0
index 124c8848ab7e..1ffdafe892d9 100644
@@ -847,6 +847,7 @@ static struct iommu_group *tegra_smmu_group_get(struct tegra_smmu *smmu,
 		return NULL;
 	}
 
+	iommu_group_set_name(group->group, soc->name);
 	list_add_tail(&group->list, &smmu->groups);
 	mutex_unlock(&smmu->lock);
 
Signature · 6 lines
2.27.0

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#2 lore raw

PATCH 2/3 iommu/tegra-smmu: Balance IOMMU group reference count

In reply to Thierry Reding

From: Thierry Reding <[email protected]>

For groups that are shared between multiple devices, care must be taken to acquire a reference for each device, otherwise the IOMMU core ends up dropping the last reference too early, which will cause the group to be released while consumers may still be thinking that they're holding a reference to it.

---
 drivers/iommu/tegra-smmu.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iommu/tegra-smmu.c +3 -1
index 1ffdafe892d9..c439c0929ef8 100644
@@ -818,6 +818,7 @@ static struct iommu_group *tegra_smmu_group_get(struct tegra_smmu *smmu,
 {
 	const struct tegra_smmu_group_soc *soc;
 	struct tegra_smmu_group *group;
+	struct iommu_group *grp;
 
 	soc = tegra_smmu_find_group(smmu, swgroup);
 	if (!soc)
@@ -827,8 +828,9 @@ static struct iommu_group *tegra_smmu_group_get(struct tegra_smmu *smmu,
 
 	list_for_each_entry(group, &smmu->groups, list)
 		if (group->soc == soc) {
+			grp = iommu_group_ref_get(group->group);
 			mutex_unlock(&smmu->lock);
-			return group->group;
+			return grp;
 		}
 
 	group = devm_kzalloc(smmu->dev, sizeof(*group), GFP_KERNEL);
Signature · 6 lines
2.27.0

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#3 lore raw

PATCH 3/3 iommu/tegra-smmu: Prune IOMMU group when it is released

In reply to Thierry Reding

From: Thierry Reding <[email protected]>

In order to share groups between multiple devices we keep track of them in a per-SMMU list. When an IOMMU group is released, a dangling pointer to it stays around in that list. Fix this by implementing an IOMMU data release callback for groups where the dangling pointer can be removed.

---
 drivers/iommu/tegra-smmu.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/iommu/tegra-smmu.c +13 -0
index c439c0929ef8..2574e716086b 100644
@@ -19,6 +19,7 @@
 
 struct tegra_smmu_group {
 	struct list_head list;
+	struct tegra_smmu *smmu;
 	const struct tegra_smmu_group_soc *soc;
 	struct iommu_group *group;
 };
@@ -813,6 +814,16 @@ tegra_smmu_find_group(struct tegra_smmu *smmu, unsigned int swgroup)
 	return NULL;
 }
 
+static void tegra_smmu_group_release(void *iommu_data)
+{
+	struct tegra_smmu_group *group = iommu_data;
+	struct tegra_smmu *smmu = group->smmu;
+
+	mutex_lock(&smmu->lock);
+	list_del(&group->list);
+	mutex_unlock(&smmu->lock);
+}
+
 static struct iommu_group *tegra_smmu_group_get(struct tegra_smmu *smmu,
 						unsigned int swgroup)
 {
@@ -840,6 +851,7 @@ static struct iommu_group *tegra_smmu_group_get(struct tegra_smmu *smmu,
 	}
 
 	INIT_LIST_HEAD(&group->list);
+	group->smmu = smmu;
 	group->soc = soc;
 
 	group->group = iommu_group_alloc();
@@ -849,6 +861,7 @@ static struct iommu_group *tegra_smmu_group_get(struct tegra_smmu *smmu,
 		return NULL;
 	}
 
+	iommu_group_set_iommudata(group->group, group, tegra_smmu_group_release);
 	iommu_group_set_name(group->group, soc->name);
 	list_add_tail(&group->list, &smmu->groups);
 	mutex_unlock(&smmu->lock);
Signature · 6 lines
2.27.0

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
maintainer Applied
#4 lore raw
In reply to Thierry Reding
Quoted 4 lines · Thierry Reding

Thierry Reding (3):
iommu/tegra-smmu: Set IOMMU group name iommu/tegra-smmu: Balance IOMMU group reference count iommu/tegra-smmu: Prune IOMMU group when it is released

Applied, thanks.