Skip to contentSkip to conversation

iommu: fix device leaks

PATCH Superseded Johan Hovold 3 participants 26 messages Sep 25, 2025 – Oct 7, 2025

Subsystems (12)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover iommu: fix device leaks

This series fixes device leaks in the iommu drivers, which pretty consistently failed to drop the reference taken by of_find_device_by_node() when looking up iommu platform devices.

Included are also a couple of related cleanups.

Johan

Johan Hovold (14):
iommu/apple-dart: fix device leak on of_xlate() iommu/qcom: fix device leak on of_xlate() iommu/exynos: fix device leak on of_xlate() iommu/ipmmu-vmsa: fix device leak on of_xlate() iommu/mediatek: fix device leak on of_xlate() iommu/mediatek: fix device leaks on probe() iommu/mediatek: simplify dt parsing error handling iommu/mediatek-v1: fix device leak on probe_device() iommu/mediatek-v1: fix device leaks on probe() iommu/mediatek-v1: add missing larb count sanity check iommu/omap: fix device leaks on probe_device() iommu/omap: simplify probe_device() error handling iommu/sun50i: fix device leak on of_xlate() iommu/tegra: fix device leak on probe_device()

 drivers/iommu/apple-dart.c              |  2 ++
 drivers/iommu/arm/arm-smmu/qcom_iommu.c | 10 +++-----
 drivers/iommu/exynos-iommu.c            |  9 +++----
 drivers/iommu/ipmmu-vmsa.c              |  2 ++
 drivers/iommu/mtk_iommu.c               | 33 +++++++++++++++++--------
 drivers/iommu/mtk_iommu_v1.c            | 28 +++++++++++++++++----
 drivers/iommu/omap-iommu.c              | 32 +++++++++++++++---------
 drivers/iommu/sun50i-iommu.c            |  2 ++
 drivers/iommu/tegra-smmu.c              |  5 ++--
 9 files changed, 81 insertions(+), 42 deletions(-)
Signature · 1 line
2.49.1
#1 lore raw

PATCH 1/14 iommu/apple-dart: fix device leak on of_xlate()

Make sure to drop the reference taken to the iommu platform device when looking up its driver data during of_xlate().

Fixes 46d1fb072e76 ("iommu/dart: Add DART iommu driver")
Cc Sven Peter <[email protected]>
---
 drivers/iommu/apple-dart.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iommu/apple-dart.c +2 -0
index 190f28d76615..1aa7c10262a8 100644
@@ -790,6 +790,8 @@ static int apple_dart_of_xlate(struct device *dev,
 	struct apple_dart *cfg_dart;
 	int i, sid;
 
+	put_device(&iommu_pdev->dev);
+
 	if (args->args_count != 1)
 		return -EINVAL;
 	sid = args->args[0];
Signature · 1 line
2.49.1
#2 lore raw

PATCH 2/14 iommu/qcom: fix device leak on of_xlate()

In reply to Johan Hovold

Make sure to drop the reference taken to the iommu platform device when looking up its driver data during of_xlate().

Note that commit e2eae09939a8 ("iommu/qcom: add missing put_device() call in qcom_iommu_of_xlate()") fixed the leak in a couple of error paths, but the reference is still leaking on success and late failures.

Fixes 0ae349a0f33f ("iommu/qcom: Add qcom_iommu")
Cc [email protected] # 4.14: e2eae09939a8
Cc Rob Clark <[email protected]>
Cc Yu Kuai <[email protected]>
---
 drivers/iommu/arm/arm-smmu/qcom_iommu.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/iommu/arm/arm-smmu/qcom_iommu.c +4 -6
index c5be95e56031..9c1166a3af6c 100644
@@ -565,14 +565,14 @@ static int qcom_iommu_of_xlate(struct device *dev,
 
 	qcom_iommu = platform_get_drvdata(iommu_pdev);
 
+	put_device(&iommu_pdev->dev);
+
 	/* make sure the asid specified in dt is valid, so we don't have
 	 * to sanity check this elsewhere:
 	 */
 	if (WARN_ON(asid > qcom_iommu->max_asid) ||
-	    WARN_ON(qcom_iommu->ctxs[asid] == NULL)) {
-		put_device(&iommu_pdev->dev);
+	    WARN_ON(qcom_iommu->ctxs[asid] == NULL))
 		return -EINVAL;
-	}
 
 	if (!dev_iommu_priv_get(dev)) {
 		dev_iommu_priv_set(dev, qcom_iommu);
@@ -581,10 +581,8 @@ static int qcom_iommu_of_xlate(struct device *dev,
 		 * multiple different iommu devices.  Multiple context
 		 * banks are ok, but multiple devices are not:
 		 */
-		if (WARN_ON(qcom_iommu != dev_iommu_priv_get(dev))) {
-			put_device(&iommu_pdev->dev);
+		if (WARN_ON(qcom_iommu != dev_iommu_priv_get(dev)))
 			return -EINVAL;
-		}
 	}
 
 	return iommu_fwspec_add_ids(dev, &asid, 1);
Signature · 1 line
2.49.1
#3 lore raw

PATCH 3/14 iommu/exynos: fix device leak on of_xlate()

In reply to Johan Hovold

Make sure to drop the reference taken to the iommu platform device when looking up its driver data during of_xlate().

Fixes aa759fd376fb ("iommu/exynos: Add callback for initializing devices from device tree")
Cc Marek Szyprowski <[email protected]>
---
 drivers/iommu/exynos-iommu.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/iommu/exynos-iommu.c +3 -6
index b6edd178fe25..ce9e935cb84c 100644
@@ -1446,17 +1446,14 @@ static int exynos_iommu_of_xlate(struct device *dev,
 		return -ENODEV;
 
 	data = platform_get_drvdata(sysmmu);
-	if (!data) {
-		put_device(&sysmmu->dev);
+	put_device(&sysmmu->dev);
+	if (!data)
 		return -ENODEV;
-	}
 
 	if (!owner) {
 		owner = kzalloc(sizeof(*owner), GFP_KERNEL);
-		if (!owner) {
-			put_device(&sysmmu->dev);
+		if (!owner)
 			return -ENOMEM;
-		}
 
 		INIT_LIST_HEAD(&owner->controllers);
 		mutex_init(&owner->rpm_lock);
Signature · 1 line
2.49.1
#4 lore raw

PATCH 4/14 iommu/ipmmu-vmsa: fix device leak on of_xlate()

In reply to Johan Hovold

Make sure to drop the reference taken to the iommu platform device when looking up its driver data during of_xlate().

Fixes 7b2d59611fef ("iommu/ipmmu-vmsa: Replace local utlb code with fwspec ids")
Cc Magnus Damm <[email protected]>
---
 drivers/iommu/ipmmu-vmsa.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iommu/ipmmu-vmsa.c +2 -0
index ffa892f65714..02a2a55ffa0a 100644
@@ -720,6 +720,8 @@ static int ipmmu_init_platform_device(struct device *dev,
 
 	dev_iommu_priv_set(dev, platform_get_drvdata(ipmmu_pdev));
 
+	put_device(&ipmmu_pdev->dev);
+
 	return 0;
 }
 
Signature · 1 line
2.49.1
#5 lore raw

PATCH 5/14 iommu/mediatek: fix device leak on of_xlate()

In reply to Johan Hovold

Make sure to drop the reference taken to the iommu platform device when looking up its driver data during of_xlate().

Fixes 0df4fabe208d ("iommu/mediatek: Add mt8173 IOMMU driver")
Cc Yong Wu <[email protected]>
---
 drivers/iommu/mtk_iommu.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iommu/mtk_iommu.c +2 -0
index 0e0285348d2b..8d8e85186188 100644
@@ -974,6 +974,8 @@ static int mtk_iommu_of_xlate(struct device *dev,
 			return -EINVAL;
 
 		dev_iommu_priv_set(dev, platform_get_drvdata(m4updev));
+
+		put_device(&m4updev->dev);
 	}
 
 	return iommu_fwspec_add_ids(dev, args->args, 1);
Signature · 1 line
2.49.1
#6 lore raw

PATCH 6/14 iommu/mediatek: fix device leaks on probe()

In reply to Johan Hovold

Make sure to drop the references taken to the larb devices during probe on probe failure (e.g. probe deferral) and on driver unbind.

Note that commit 26593928564c ("iommu/mediatek: Add error path for loop of mm_dts_parse") fixed the leaks in a couple of error paths, but the references are still leaking on success and late failures.

Fixes 0df4fabe208d ("iommu/mediatek: Add mt8173 IOMMU driver")
Cc Yong Wu <[email protected]>
---
 drivers/iommu/mtk_iommu.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
drivers/iommu/mtk_iommu.c +18 -6
index 8d8e85186188..20a5ba80f983 100644
@@ -1216,13 +1216,17 @@ static int mtk_iommu_mm_dts_parse(struct device *dev, struct component_match **m
 		platform_device_put(plarbdev);
 	}
 
-	if (!frst_avail_smicomm_node)
-		return -EINVAL;
+	if (!frst_avail_smicomm_node) {
+		ret = -EINVAL;
+		goto err_larbdev_put;
+	}
 
 	pcommdev = of_find_device_by_node(frst_avail_smicomm_node);
 	of_node_put(frst_avail_smicomm_node);
-	if (!pcommdev)
-		return -ENODEV;
+	if (!pcommdev) {
+		ret = -ENODEV;
+		goto err_larbdev_put;
+	}
 	data->smicomm_dev = &pcommdev->dev;
 
 	link = device_link_add(data->smicomm_dev, dev,
@@ -1230,7 +1234,8 @@ static int mtk_iommu_mm_dts_parse(struct device *dev, struct component_match **m
 	platform_device_put(pcommdev);
 	if (!link) {
 		dev_err(dev, "Unable to link %s.\n", dev_name(data->smicomm_dev));
-		return -EINVAL;
+		ret = -EINVAL;
+		goto err_larbdev_put;
 	}
 	return 0;
 
@@ -1402,8 +1407,12 @@ static int mtk_iommu_probe(struct platform_device *pdev)
 	iommu_device_sysfs_remove(&data->iommu);
 out_list_del:
 	list_del(&data->list);
-	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM))
+	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
 		device_link_remove(data->smicomm_dev, dev);
+
+		for (i = 0; i < MTK_LARB_NR_MAX; i++)
+			put_device(data->larb_imu[i].dev);
+	}
 out_runtime_disable:
 	pm_runtime_disable(dev);
 	return ret;
@@ -1423,6 +1432,9 @@ static void mtk_iommu_remove(struct platform_device *pdev)
 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
 		device_link_remove(data->smicomm_dev, &pdev->dev);
 		component_master_del(&pdev->dev, &mtk_iommu_com_ops);
+
+		for (i = 0; i < MTK_LARB_NR_MAX; i++)
+			put_device(data->larb_imu[i].dev);
 	}
 	pm_runtime_disable(&pdev->dev);
 	for (i = 0; i < data->plat_data->banks_num; i++) {
Signature · 1 line
2.49.1
#7 lore raw

PATCH 7/14 iommu/mediatek: simplify dt parsing error handling

In reply to Johan Hovold

As previously documented by commit 26593928564c ("iommu/mediatek: Add error path for loop of mm_dts_parse"), the id mapping may not be linear so the whole larb array needs to be iterated on devicetree parsing errors.

Simplify the loop by iterating from index zero while dropping the redundant NULL check for consistency with later cleanups.

Also add back the comment which was removed by commit 462e768b55a2 ("iommu/mediatek: Fix forever loop in error handling") to prevent anyone from trying to optimise the loop by iterating backwards from 'i'.

---
 drivers/iommu/mtk_iommu.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iommu/mtk_iommu.c +3 -4
index 20a5ba80f983..24bb8b646edc 100644
@@ -1240,11 +1240,10 @@ static int mtk_iommu_mm_dts_parse(struct device *dev, struct component_match **m
 	return 0;
 
 err_larbdev_put:
-	for (i = MTK_LARB_NR_MAX - 1; i >= 0; i--) {
-		if (!data->larb_imu[i].dev)
-			continue;
+	/* id mapping may not be linear, loop the whole array */
+	for (i = 0; i < MTK_LARB_NR_MAX; i++)
 		put_device(data->larb_imu[i].dev);
-	}
+
 	return ret;
 }
 
Signature · 1 line
2.49.1
#8 lore raw

PATCH 8/14 iommu/mediatek-v1: fix device leak on probe_device()

In reply to Johan Hovold

Make sure to drop the reference taken to the iommu platform device when looking up its driver data during probe_device().

Fixes b17336c55d89 ("iommu/mediatek: add support for mtk iommu generation one HW")
Cc Honghui Zhang <[email protected]>
---
 drivers/iommu/mtk_iommu_v1.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iommu/mtk_iommu_v1.c +2 -0
index 10cc0b1197e8..de9153c0a82f 100644
@@ -435,6 +435,8 @@ static int mtk_iommu_v1_create_mapping(struct device *dev,
 			return -EINVAL;
 
 		dev_iommu_priv_set(dev, platform_get_drvdata(m4updev));
+
+		put_device(&m4updev->dev);
 	}
 
 	ret = iommu_fwspec_add_ids(dev, args->args, 1);
Signature · 1 line
2.49.1
#9 lore raw

PATCH 9/14 iommu/mediatek-v1: fix device leaks on probe()

In reply to Johan Hovold

Make sure to drop the references taken to the larb devices during probe on probe failure (e.g. probe deferral) and on driver unbind.

Fixes b17336c55d89 ("iommu/mediatek: add support for mtk iommu generation one HW")
Cc Honghui Zhang <[email protected]>
---
 drivers/iommu/mtk_iommu_v1.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
drivers/iommu/mtk_iommu_v1.c +18 -5
index de9153c0a82f..44b965a2db92 100644
@@ -648,8 +648,10 @@ static int mtk_iommu_v1_probe(struct platform_device *pdev)
 		struct platform_device *plarbdev;
 
 		larbnode = of_parse_phandle(dev->of_node, "mediatek,larbs", i);
-		if (!larbnode)
-			return -EINVAL;
+		if (!larbnode) {
+			ret = -EINVAL;
+			goto out_put_larbs;
+		}
 
 		if (!of_device_is_available(larbnode)) {
 			of_node_put(larbnode);
@@ -659,11 +661,14 @@ static int mtk_iommu_v1_probe(struct platform_device *pdev)
 		plarbdev = of_find_device_by_node(larbnode);
 		if (!plarbdev) {
 			of_node_put(larbnode);
-			return -ENODEV;
+			ret = -ENODEV;
+			goto out_put_larbs;
 		}
 		if (!plarbdev->dev.driver) {
 			of_node_put(larbnode);
-			return -EPROBE_DEFER;
+			put_device(&plarbdev->dev);
+			ret = -EPROBE_DEFER;
+			goto out_put_larbs;
 		}
 		data->larb_imu[i].dev = &plarbdev->dev;
 
@@ -675,7 +680,7 @@ static int mtk_iommu_v1_probe(struct platform_device *pdev)
 
 	ret = mtk_iommu_v1_hw_init(data);
 	if (ret)
-		return ret;
+		goto out_put_larbs;
 
 	ret = iommu_device_sysfs_add(&data->iommu, &pdev->dev, NULL,
 				     dev_name(&pdev->dev));
@@ -697,12 +702,17 @@ static int mtk_iommu_v1_probe(struct platform_device *pdev)
 	iommu_device_sysfs_remove(&data->iommu);
 out_clk_unprepare:
 	clk_disable_unprepare(data->bclk);
+out_put_larbs:
+	for (i = 0; i < MTK_LARB_NR_MAX; i++)
+		put_device(data->larb_imu[i].dev);
+
 	return ret;
 }
 
 static void mtk_iommu_v1_remove(struct platform_device *pdev)
 {
 	struct mtk_iommu_v1_data *data = platform_get_drvdata(pdev);
+	int i;
 
 	iommu_device_sysfs_remove(&data->iommu);
 	iommu_device_unregister(&data->iommu);
@@ -710,6 +720,9 @@ static void mtk_iommu_v1_remove(struct platform_device *pdev)
 	clk_disable_unprepare(data->bclk);
 	devm_free_irq(&pdev->dev, data->irq, data);
 	component_master_del(&pdev->dev, &mtk_iommu_v1_com_ops);
+
+	for (i = 0; i < MTK_LARB_NR_MAX; i++)
+		put_device(data->larb_imu[i].dev);
 }
 
 static int __maybe_unused mtk_iommu_v1_suspend(struct device *dev)
Signature · 1 line
2.49.1
#10 lore raw

PATCH 10/14 iommu/mediatek-v1: add missing larb count sanity check

In reply to Johan Hovold

Add the missing larb count sanity check to avoid writing beyond a fixed sized array in case of a malformed devicetree.

---
 drivers/iommu/mtk_iommu_v1.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/iommu/mtk_iommu_v1.c +3 -0
index 44b965a2db92..55d6615a41a9 100644
@@ -643,6 +643,9 @@ static int mtk_iommu_v1_probe(struct platform_device *pdev)
 	if (larb_nr < 0)
 		return larb_nr;
 
+	if (larb_nr > MTK_LARB_NR_MAX)
+		return -EINVAL;
+
 	for (i = 0; i < larb_nr; i++) {
 		struct device_node *larbnode;
 		struct platform_device *plarbdev;
Signature · 1 line
2.49.1
#11 lore raw

PATCH 11/14 iommu/omap: fix device leaks on probe_device()

In reply to Johan Hovold

Make sure to drop the reference taken to the iommu platform devices during probe_device() on errors and when the device is later released.

Fixes 9d5018deec86 ("iommu/omap: Add support to program multiple iommus")
Fixes 7d6827748d54 ("iommu/omap: Fix iommu archdata name for DT-based devices")
Cc Suman Anna <[email protected]>
---
 drivers/iommu/omap-iommu.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)
drivers/iommu/omap-iommu.c +19 -8
index 6fb93927bdb9..77023d49bd24 100644
@@ -1636,7 +1636,7 @@ static struct iommu_device *omap_iommu_probe_device(struct device *dev)
 	struct platform_device *pdev;
 	struct omap_iommu *oiommu;
 	struct device_node *np;
-	int num_iommus, i;
+	int num_iommus, i, ret;
 
 	/*
 	 * Allocate the per-device iommu structure for DT-based devices.
@@ -1663,22 +1663,22 @@ static struct iommu_device *omap_iommu_probe_device(struct device *dev)
 	for (i = 0, tmp = arch_data; i < num_iommus; i++, tmp++) {
 		np = of_parse_phandle(dev->of_node, "iommus", i);
 		if (!np) {
-			kfree(arch_data);
-			return ERR_PTR(-EINVAL);
+			ret = -EINVAL;
+			goto err_put_iommus;
 		}
 
 		pdev = of_find_device_by_node(np);
 		if (!pdev) {
 			of_node_put(np);
-			kfree(arch_data);
-			return ERR_PTR(-ENODEV);
+			ret = -ENODEV;
+			goto err_put_iommus;
 		}
 
 		oiommu = platform_get_drvdata(pdev);
 		if (!oiommu) {
 			of_node_put(np);
-			kfree(arch_data);
-			return ERR_PTR(-EINVAL);
+			ret = -EINVAL;
+			goto err_put_iommus;
 		}
 
 		tmp->iommu_dev = oiommu;
@@ -1697,17 +1697,28 @@ static struct iommu_device *omap_iommu_probe_device(struct device *dev)
 	oiommu = arch_data->iommu_dev;
 
 	return &oiommu->iommu;
+
+err_put_iommus:
+	for (tmp = arch_data; tmp->dev; tmp++)
+		put_device(tmp->dev);
+
+	kfree(arch_data);
+
+	return ERR_PTR(ret);
 }
 
 static void omap_iommu_release_device(struct device *dev)
 {
 	struct omap_iommu_arch_data *arch_data = dev_iommu_priv_get(dev);
+	struct omap_iommu_arch_data *tmp;
 
 	if (!dev->of_node || !arch_data)
 		return;
 
-	kfree(arch_data);
+	for (tmp = arch_data; tmp->dev; tmp++)
+		put_device(tmp->dev);
 
+	kfree(arch_data);
 }
 
 static int omap_iommu_of_xlate(struct device *dev, const struct of_phandle_args *args)
Signature · 1 line
2.49.1
#14 lore raw

PATCH 12/14 iommu/omap: simplify probe_device() error handling

In reply to Johan Hovold

Simplify the probe_device() error handling by dropping the iommu OF node reference sooner.

---
 drivers/iommu/omap-iommu.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/iommu/omap-iommu.c +1 -4
index 77023d49bd24..844def804777 100644
@@ -1668,23 +1668,20 @@ static struct iommu_device *omap_iommu_probe_device(struct device *dev)
 		}
 
 		pdev = of_find_device_by_node(np);
+		of_node_put(np);
 		if (!pdev) {
-			of_node_put(np);
 			ret = -ENODEV;
 			goto err_put_iommus;
 		}
 
 		oiommu = platform_get_drvdata(pdev);
 		if (!oiommu) {
-			of_node_put(np);
 			ret = -EINVAL;
 			goto err_put_iommus;
 		}
 
 		tmp->iommu_dev = oiommu;
 		tmp->dev = &pdev->dev;
-
-		of_node_put(np);
 	}
 
 	dev_iommu_priv_set(dev, arch_data);
Signature · 1 line
2.49.1
#15 lore raw

PATCH 13/14 iommu/sun50i: fix device leak on of_xlate()

In reply to Johan Hovold

Make sure to drop the reference taken to the iommu platform device when looking up its driver data during of_xlate().

Fixes 4100b8c229b3 ("iommu: Add Allwinner H6 IOMMU driver")
Cc Maxime Ripard <[email protected]>
---
 drivers/iommu/sun50i-iommu.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iommu/sun50i-iommu.c +2 -0
index de10b569d9a9..6306570d57db 100644
@@ -839,6 +839,8 @@ static int sun50i_iommu_of_xlate(struct device *dev,
 
 	dev_iommu_priv_set(dev, platform_get_drvdata(iommu_pdev));
 
+	put_device(&iommu_pdev->dev);
+
 	return iommu_fwspec_add_ids(dev, &id, 1);
 }
 
Signature · 1 line
2.49.1
#16 lore raw

PATCH 14/14 iommu/tegra: fix device leak on probe_device()

In reply to Johan Hovold

Make sure to drop the reference taken to the iommu platform device when looking up its driver data during probe_device().

Fixes 891846516317 ("memory: Add NVIDIA Tegra memory controller support")
Cc Thierry Reding <[email protected]>
---
 drivers/iommu/tegra-smmu.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/iommu/tegra-smmu.c +2 -3
index 36cdd5fbab07..f6f26a072820 100644
@@ -830,10 +830,9 @@ static struct tegra_smmu *tegra_smmu_find(struct device_node *np)
 		return NULL;
 
 	mc = platform_get_drvdata(pdev);
-	if (!mc) {
-		put_device(&pdev->dev);
+	put_device(&pdev->dev);
+	if (!mc)
 		return NULL;
-	}
 
 	return mc->smmu;
 }
Signature · 1 line
2.49.1
#17 lore raw
In reply to Johan Hovold
Quoted 3 lines · Johan Hovold

This series fixes device leaks in the iommu drivers, which pretty consistently failed to drop the reference taken by of_find_device_by_node() when looking up iommu platform devices.

Yes, they are mis-designed in many ways :\

IDK if it is worth fixing like this, or if more effort should be put to make the drivers use of_xlate properly - the arm smmu drivers show the only way to use it..

But if staying like this then maybe add a little helper?

void *iommu_xlate_to_iommu_drvdata(const struct of_phandle_args *args);

Put the whole racy of_find_device_by_node / put_device / platform_get_drvdata sequence is in one tidy function.. With documentation it is not safe don't use it in new code?

Jason

reviewer
#18 lore raw
Quoted 6 lines · Jason Gunthorpe
Quoted 3 lines · Johan Hovold

This series fixes device leaks in the iommu drivers, which pretty consistently failed to drop the reference taken by of_find_device_by_node() when looking up iommu platform devices.

Yes, they are mis-designed in many ways :\

Historically they weren't really leaks either, just spare references on a device which at that point could definitely never go away. I suppose now that we finally have the .of_xlate calls in IOMMU registration, it would be possible for some error during probe to cause the IOMMU driver to fail to bind, at which point the references could rightly be considered leaked, however these are all embedded platforms with essentially zero chance of platform device hotplug, especially not of IOMMU devices, so realistically those references still don't matter to anything other than code checkers.

In summary; meh.

Quoted 3 lines

IDK if it is worth fixing like this, or if more effort should be put to make the drivers use of_xlate properly - the arm smmu drivers show the only way to use it..

The SMMU drivers are really doing the same thing, they just defer that lookup operation to .probe_device time (largely for historical reasons, I think), and they use bus_find_device_by_node() rather than the specific of_ version since they support ACPI too. And they do happen to include the put_device(), since apparently I was paying full attention that day.

Quoted 7 lines

But if staying like this then maybe add a little helper?

void *iommu_xlate_to_iommu_drvdata(const struct of_phandle_args *args);

Put the whole racy of_find_device_by_node / put_device / platform_get_drvdata sequence is in one tidy function.. With documentation it is not safe don't use it in new code?

It's not racy; if we're calling the .of_xlate op (or really any op for that matter), it's because an IOMMU driver has registered (or is registering) for the given fwnode, which means it is bound to the corresponding struct device. Thus as above, in that context said struct device, nor said IOMMU driver's drvdata, ain't goin' nowhere.

Thanks,
Robin.

#22 lore raw
In reply to Jason Gunthorpe
Quoted 6 lines · Jason Gunthorpe
Quoted 3 lines · Johan Hovold

This series fixes device leaks in the iommu drivers, which pretty consistently failed to drop the reference taken by of_find_device_by_node() when looking up iommu platform devices.

Yes, they are mis-designed in many ways :\

This seems to be more a case of developers not reading documentation and copying implementations from existing drivers.

I amended the documentation for of_find_device_by_node() in 2016 and at least of some these drivers were merged later. Fixing up the existing uses will hopefully reduce the likelihood of further leaks being introduced (e.g. in places were it matters more).

Quoted 3 lines

IDK if it is worth fixing like this, or if more effort should be put to make the drivers use of_xlate properly - the arm smmu drivers show the only way to use it..

As Robin pointed out, those drivers just drop the reference as they should (even if I'd drop the reference after looking up the driver data).

Quoted 7 lines

But if staying like this then maybe add a little helper?

void *iommu_xlate_to_iommu_drvdata(const struct of_phandle_args *args);

Put the whole racy of_find_device_by_node / put_device / platform_get_drvdata sequence is in one tidy function.. With documentation it is not safe don't use it in new code?

It's not racy in the context of iommu drivers as Robin also explained.

Johan

#19 lore raw
In reply to Robin Murphy
Quoted 17 lines · Robin Murphy
Quoted 6 lines · Jason Gunthorpe
Quoted 3 lines · Johan Hovold

This series fixes device leaks in the iommu drivers, which pretty consistently failed to drop the reference taken by of_find_device_by_node() when looking up iommu platform devices.

Yes, they are mis-designed in many ways :\

Historically they weren't really leaks either, just spare references on a device which at that point could definitely never go away. I suppose now that we finally have the .of_xlate calls in IOMMU registration, it would be possible for some error during probe to cause the IOMMU driver to fail to bind, at which point the references could rightly be considered leaked, however these are all embedded platforms with essentially zero chance of platform device hotplug, especially not of IOMMU devices, so realistically those references still don't matter to anything other than code checkers.

Quoted 1 line

In summary; meh.

Yeah, it isn't a real practical bug, but it still ugly and no doubt upsets static tools.

Quoted 7 lines
Quoted 3 lines

IDK if it is worth fixing like this, or if more effort should be put to make the drivers use of_xlate properly - the arm smmu drivers show the only way to use it..

The SMMU drivers are really doing the same thing, they just defer that lookup operation to .probe_device time (largely for historical reasons, I think), and they use bus_find_device_by_node()

However the SMMU drivers are doing this under the iommu_probe_device_lock and not stashing the pointer into a drvdata where there is no locking protecting it.

Quoted 4 lines

It's not racy; if we're calling the .of_xlate op (or really any op for that matter), it's because an IOMMU driver has registered (or is registering) for the given fwnode, which means it is bound to the corresponding struct device.

It is not racy if you make the very practical assumption there is no iommu driver unplug.

Anyhow, I drafted a nice fix for all of this. After all the rework it is trivial for the core code to pass in the struct iommu_device * to probe and then most of the drivers just drop this ugly code completely.

https://github.com/jgunthorpe/linux/commits/iommu-fwspec/

Jason

commit cca42a9b5325b96bfd3d74e24628511f537afbe9

Author: Jason Gunthorpe <[email protected]>
Date:   Wed Oct 1 09:18:13 2025 -0300

iommu: Add a probe_device_fwspec() op

For fwspec using drivers the core code has already determined which struct iommu_device instance the fwspec is linked to. It can trivially pass that instance pointer into the driver.

This frees fwspec drivers from having to repeat the work of trying to find the struct iommu_device for the fwspec.

Non-fwspec drivers (x86/etc) continue to use the old probe function which is called on the first non-fwspec iommu driver registered.

This is only usable by drivers that support a single iommu instance per device. There are some drivers (msm, exynos, dart) that do have an iommus property that list multiple iommu_devices with multiple IDs. They cannot use this simplified mechanism.

drivers/iommu/iommu.c +78 -32
index 060ebe330ee163..718a1da8f54710 100644
@@ -142,6 +142,8 @@ static void __iommu_group_free_device(struct iommu_group *group,
 				      struct group_device *grp_dev);
 static void iommu_domain_init(struct iommu_domain *domain, unsigned int type,
 			      const struct iommu_ops *ops);
+static struct iommu_device *
+iommu_from_fwnode(const struct fwnode_handle *fwnode);
 
 #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store)		\
 struct iommu_group_attribute iommu_group_attr_##_name =		\
@@ -406,13 +408,75 @@ void dev_iommu_priv_set(struct device *dev, void *priv)
 }
 EXPORT_SYMBOL_GPL(dev_iommu_priv_set);
 
+static int iommu_do_probe_device(struct device *dev)
+{
+	struct iommu_device *iommu;
+	int ret;
+
+	lockdep_assert_held(&iommu_probe_device_lock);
+
+	if (dev->iommu->fwspec) {
+		struct iommu_device *fwspec_iommu;
+
+		fwspec_iommu =
+			iommu_from_fwnode(dev->iommu->fwspec->iommu_fwnode);
+		if (!fwspec_iommu)
+			return -ENODEV;
+
+		if (fwspec_iommu->ops->probe_device_fwspec) {
+			ret = fwspec_iommu->ops->probe_device_fwspec(
+				fwspec_iommu, dev);
+			if (ret)
+				return ret;
+			iommu = fwspec_iommu;
+		} else {
+			iommu = fwspec_iommu->ops->probe_device(dev);
+			if (IS_ERR(iommu))
+				return PTR_ERR(iommu);
+			if (WARN_ON(iommu != fwspec_iommu)) {
+				ret = -EINVAL;
+				goto err_release;
+			}
+		}
+	} else {
+		/*
+		 * At this point, relevant devices either now have a fwspec
+		 * which will match ops registered with a non-NULL fwnode, or we
+		 * can reasonably assume that only one of Intel, AMD, s390, PAMU
+		 * or legacy SMMUv2 can be present, and that any of their
+		 * registered instances has suitable ops for probing, and thus
+		 * cheekily co-opt the same mechanism.
+		 */
+		iommu = iommu_from_fwnode(NULL);
+		if (!iommu)
+			return -ENODEV;
+		/* Non fwspec drivers must identify their instance internally */
+		if (WARN_ON(!iommu->ops->probe_device))
+			return -EINVAL;
+		iommu = iommu->ops->probe_device(dev);
+		if (IS_ERR(iommu))
+			return PTR_ERR(iommu);
+	}
+
+	if (!try_module_get(iommu->ops->owner)) {
+		ret = -EINVAL;
+		goto err_release;
+	}
+
+	dev->iommu->iommu_dev = iommu;
+
+err_release:
+	if (iommu->ops->release_device)
+		iommu->ops->release_device(dev);
+	return ret;
+}
+
 /*
  * Init the dev->iommu and dev->iommu_group in the struct device and get the
  * driver probed
  */
 static int iommu_init_device(struct device *dev)
 {
-	const struct iommu_ops *ops;
 	struct iommu_device *iommu_dev;
 	struct iommu_group *group;
 	int ret;
@@ -434,36 +498,17 @@ static int iommu_init_device(struct device *dev)
 		if (!dev->iommu || dev->iommu_group)
 			return -ENODEV;
 	}
-	/*
-	 * At this point, relevant devices either now have a fwspec which will
-	 * match ops registered with a non-NULL fwnode, or we can reasonably
-	 * assume that only one of Intel, AMD, s390, PAMU or legacy SMMUv2 can
-	 * be present, and that any of their registered instances has suitable
-	 * ops for probing, and thus cheekily co-opt the same mechanism.
-	 */
-	ops = iommu_fwspec_ops(dev->iommu->fwspec);
-	if (!ops) {
-		ret = -ENODEV;
-		goto err_free;
-	}
 
-	if (!try_module_get(ops->owner)) {
-		ret = -EINVAL;
+	ret = iommu_do_probe_device(dev);
+	if (ret)
 		goto err_free;
-	}
-
-	iommu_dev = ops->probe_device(dev);
-	if (IS_ERR(iommu_dev)) {
-		ret = PTR_ERR(iommu_dev);
-		goto err_module_put;
-	}
-	dev->iommu->iommu_dev = iommu_dev;
+	iommu_dev = dev->iommu->iommu_dev;
 
 	ret = iommu_device_link(iommu_dev, dev);
 	if (ret)
 		goto err_release;
 
-	group = ops->device_group(dev);
+	group = iommu_dev->ops->device_group(dev);
 	if (WARN_ON_ONCE(group == NULL))
 		group = ERR_PTR(-EINVAL);
 	if (IS_ERR(group)) {
@@ -473,17 +518,17 @@ static int iommu_init_device(struct device *dev)
 	dev->iommu_group = group;
 
 	dev->iommu->max_pasids = dev_iommu_get_max_pasids(dev);
-	if (ops->is_attach_deferred)
-		dev->iommu->attach_deferred = ops->is_attach_deferred(dev);
+	if (iommu_dev->ops->is_attach_deferred)
+		dev->iommu->attach_deferred =
+			iommu_dev->ops->is_attach_deferred(dev);
 	return 0;
 
 err_unlink:
 	iommu_device_unlink(iommu_dev, dev);
 err_release:
-	if (ops->release_device)
-		ops->release_device(dev);
-err_module_put:
-	module_put(ops->owner);
+	if (iommu_dev->ops->release_device)
+		iommu_dev->ops->release_device(dev);
+	module_put(iommu_dev->ops->owner);
 err_free:
 	dev->iommu->iommu_dev = NULL;
 	dev_iommu_free(dev);
@@ -2855,9 +2900,10 @@ bool iommu_default_passthrough(void)
 }
 EXPORT_SYMBOL_GPL(iommu_default_passthrough);
 
-static const struct iommu_device *iommu_from_fwnode(const struct fwnode_handle *fwnode)
+static struct iommu_device *iommu_from_fwnode(
+	const struct fwnode_handle *fwnode)
 {
-	const struct iommu_device *iommu, *ret = NULL;
+	struct iommu_device *iommu, *ret = NULL;
 
 	spin_lock(&iommu_device_lock);
 	list_for_each_entry(iommu, &iommu_device_list, list)
include/linux/iommu.h +3 -0
index c30d12e16473df..52e74ccdb4dc79 100644
@@ -48,6 +48,7 @@ struct iommufd_ctx;
 struct iommufd_viommu;
 struct msi_desc;
 struct msi_msg;
+struct iommu_device;
 
 #define IOMMU_FAULT_PERM_READ	(1 << 0) /* read */
 #define IOMMU_FAULT_PERM_WRITE	(1 << 1) /* write */
@@ -680,6 +681,8 @@ struct iommu_ops {
 		struct device *dev, struct iommu_domain *parent, u32 flags,
 		const struct iommu_user_data *user_data);
 
+	int (*probe_device_fwspec)(struct iommu_device *iommu,
+				   struct device *dev);
 	struct iommu_device *(*probe_device)(struct device *dev);
 	void (*release_device)(struct device *dev);
 	void (*probe_finalize)(struct device *dev);
#23 lore raw
In reply to Johan Hovold
Quoted 2 lines · Johan Hovold

This seems to be more a case of developers not reading documentation and copying implementations from existing drivers.

IMHO the drivers are mis-using of_xlate, it shouldn't be looking up drvdata at all.

Quoted 7 lines
Quoted 3 lines

IDK if it is worth fixing like this, or if more effort should be put to make the drivers use of_xlate properly - the arm smmu drivers show the only way to use it..

As Robin pointed out, those drivers just drop the reference as they should (even if I'd drop the reference after looking up the driver data).

So I wrote the series to clean this up and drop the calls to of_find_device_by_node() and so on.

I left dart, exynos, msm and omap as-is, so I suggest you trim this series to only those drivers and rely on my version to fix the rest.

Jason

reviewer
#20 lore raw
In reply to Jason Gunthorpe
Quoted 27 lines · Jason Gunthorpe
Quoted 17 lines · Robin Murphy
Quoted 6 lines · Jason Gunthorpe
Quoted 3 lines · Johan Hovold

This series fixes device leaks in the iommu drivers, which pretty consistently failed to drop the reference taken by of_find_device_by_node() when looking up iommu platform devices.

Yes, they are mis-designed in many ways :\

Historically they weren't really leaks either, just spare references on a device which at that point could definitely never go away. I suppose now that we finally have the .of_xlate calls in IOMMU registration, it would be possible for some error during probe to cause the IOMMU driver to fail to bind, at which point the references could rightly be considered leaked, however these are all embedded platforms with essentially zero chance of platform device hotplug, especially not of IOMMU devices, so realistically those references still don't matter to anything other than code checkers.

Quoted 1 line

In summary; meh.

Yeah, it isn't a real practical bug, but it still ugly and no doubt upsets static tools.

Quoted 4 lines
Quoted 3 lines

IDK if it is worth fixing like this, or if more effort should be put to make the drivers use of_xlate properly - the arm smmu drivers show the only way to use it..

Quoted 7 lines
Quoted 3 lines

The SMMU drivers are really doing the same thing, they just defer that lookup operation to .probe_device time (largely for historical reasons, I think), and they use bus_find_device_by_node()

However the SMMU drivers are doing this under the iommu_probe_device_lock and not stashing the pointer into a drvdata where there is no locking protecting it.

Huh? Every .of_xlate call is under probe_device_lock just as much as .probe_device calls are; they *have* to be, since they too are in the position of working with dev->iommu before dev->iommu_group is set to guarantee its stability.

Quoted 7 lines
Quoted 4 lines

It's not racy; if we're calling the .of_xlate op (or really any op for that matter), it's because an IOMMU driver has registered (or is registering) for the given fwnode, which means it is bound to the corresponding struct device.

It is not racy if you make the very practical assumption there is no iommu driver unplug.

If the IOMMU code allowed a driver to be removed while it's in the middle of calling into that driver, that would hardly be the driver's fault. And indeed the driver cannot be removed, because we hold a module reference around the call (and if a buggy driver failed to suppress manual sysfs unbinding, this case is frankly one of the smallest drops in the ocean of catastrophic consequences there.)

Quoted 6 lines

Anyhow, I drafted a nice fix for all of this. After all the rework it is trivial for the core code to pass in the struct iommu_device * to probe and then most of the drivers just drop this ugly code completely.

https://github.com/jgunthorpe/linux/commits/iommu-fwspec/

Eww, that is neither nice nor a "fix". Once again it's just piling on a load of extra complexity to have multiple confusingly-overlapping-but-different ways of doing the same thing, one of which is still the exact same one you've decided to object to because you've failed to understand it (as demonstrated by the commit message below, the obvious bug in the hideous mess below that, and at a glance the other patches actively *breaking* at least one driver.)

Thanks,
Robin.

Quoted 215 lines

Jason

commit cca42a9b5325b96bfd3d74e24628511f537afbe9

Author: Jason Gunthorpe <[email protected]>
Date:   Wed Oct 1 09:18:13 2025 -0300

iommu: Add a probe_device_fwspec() op

For fwspec using drivers the core code has already determined which struct iommu_device instance the fwspec is linked to. It can trivially pass that instance pointer into the driver.

This frees fwspec drivers from having to repeat the work of trying to find the struct iommu_device for the fwspec.

Non-fwspec drivers (x86/etc) continue to use the old probe function which is called on the first non-fwspec iommu driver registered.

This is only usable by drivers that support a single iommu instance per device. There are some drivers (msm, exynos, dart) that do have an iommus property that list multiple iommu_devices with multiple IDs. They cannot use this simplified mechanism.

drivers/iommu/iommu.c +78 -32
index 060ebe330ee163..718a1da8f54710 100644
@@ -142,6 +142,8 @@ static void __iommu_group_free_device(struct iommu_group *group,
  				      struct group_device *grp_dev);
  static void iommu_domain_init(struct iommu_domain *domain, unsigned int type,
  			      const struct iommu_ops *ops);
+static struct iommu_device *
+iommu_from_fwnode(const struct fwnode_handle *fwnode);
 
  #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store)		\
  struct iommu_group_attribute iommu_group_attr_##_name =		\
@@ -406,13 +408,75 @@ void dev_iommu_priv_set(struct device *dev, void *priv)
  }
  EXPORT_SYMBOL_GPL(dev_iommu_priv_set);
 
+static int iommu_do_probe_device(struct device *dev)
+{
+	struct iommu_device *iommu;
+	int ret;
+
+	lockdep_assert_held(&iommu_probe_device_lock);
+
+	if (dev->iommu->fwspec) {
+		struct iommu_device *fwspec_iommu;
+
+		fwspec_iommu =
+			iommu_from_fwnode(dev->iommu->fwspec->iommu_fwnode);
+		if (!fwspec_iommu)
+			return -ENODEV;
+
+		if (fwspec_iommu->ops->probe_device_fwspec) {
+			ret = fwspec_iommu->ops->probe_device_fwspec(
+				fwspec_iommu, dev);
+			if (ret)
+				return ret;
+			iommu = fwspec_iommu;
+		} else {
+			iommu = fwspec_iommu->ops->probe_device(dev);
+			if (IS_ERR(iommu))
+				return PTR_ERR(iommu);
+			if (WARN_ON(iommu != fwspec_iommu)) {
+				ret = -EINVAL;
+				goto err_release;
+			}
+		}
+	} else {
+		/*
+		 * At this point, relevant devices either now have a fwspec
+		 * which will match ops registered with a non-NULL fwnode, or we
+		 * can reasonably assume that only one of Intel, AMD, s390, PAMU
+		 * or legacy SMMUv2 can be present, and that any of their
+		 * registered instances has suitable ops for probing, and thus
+		 * cheekily co-opt the same mechanism.
+		 */
+		iommu = iommu_from_fwnode(NULL);
+		if (!iommu)
+			return -ENODEV;
+		/* Non fwspec drivers must identify their instance internally */
+		if (WARN_ON(!iommu->ops->probe_device))
+			return -EINVAL;
+		iommu = iommu->ops->probe_device(dev);
+		if (IS_ERR(iommu))
+			return PTR_ERR(iommu);
+	}
+
+	if (!try_module_get(iommu->ops->owner)) {
+		ret = -EINVAL;
+		goto err_release;
+	}
+
+	dev->iommu->iommu_dev = iommu;
+
+err_release:
+	if (iommu->ops->release_device)
+		iommu->ops->release_device(dev);
+	return ret;
+}
+
  /*
   * Init the dev->iommu and dev->iommu_group in the struct device and get the
   * driver probed
   */
  static int iommu_init_device(struct device *dev)
  {
-	const struct iommu_ops *ops;
  	struct iommu_device *iommu_dev;
  	struct iommu_group *group;
  	int ret;
@@ -434,36 +498,17 @@ static int iommu_init_device(struct device *dev)
  		if (!dev->iommu || dev->iommu_group)
  			return -ENODEV;
  	}
-	/*
-	 * At this point, relevant devices either now have a fwspec which will
-	 * match ops registered with a non-NULL fwnode, or we can reasonably
-	 * assume that only one of Intel, AMD, s390, PAMU or legacy SMMUv2 can
-	 * be present, and that any of their registered instances has suitable
-	 * ops for probing, and thus cheekily co-opt the same mechanism.
-	 */
-	ops = iommu_fwspec_ops(dev->iommu->fwspec);
-	if (!ops) {
-		ret = -ENODEV;
-		goto err_free;
-	}
 
-	if (!try_module_get(ops->owner)) {
-		ret = -EINVAL;
+	ret = iommu_do_probe_device(dev);
+	if (ret)
  		goto err_free;
-	}
-
-	iommu_dev = ops->probe_device(dev);
-	if (IS_ERR(iommu_dev)) {
-		ret = PTR_ERR(iommu_dev);
-		goto err_module_put;
-	}
-	dev->iommu->iommu_dev = iommu_dev;
+	iommu_dev = dev->iommu->iommu_dev;
 
  	ret = iommu_device_link(iommu_dev, dev);
  	if (ret)
  		goto err_release;
 
-	group = ops->device_group(dev);
+	group = iommu_dev->ops->device_group(dev);
  	if (WARN_ON_ONCE(group == NULL))
  		group = ERR_PTR(-EINVAL);
  	if (IS_ERR(group)) {
@@ -473,17 +518,17 @@ static int iommu_init_device(struct device *dev)
  	dev->iommu_group = group;
 
  	dev->iommu->max_pasids = dev_iommu_get_max_pasids(dev);
-	if (ops->is_attach_deferred)
-		dev->iommu->attach_deferred = ops->is_attach_deferred(dev);
+	if (iommu_dev->ops->is_attach_deferred)
+		dev->iommu->attach_deferred =
+			iommu_dev->ops->is_attach_deferred(dev);
  	return 0;
 
  err_unlink:
  	iommu_device_unlink(iommu_dev, dev);
  err_release:
-	if (ops->release_device)
-		ops->release_device(dev);
-err_module_put:
-	module_put(ops->owner);
+	if (iommu_dev->ops->release_device)
+		iommu_dev->ops->release_device(dev);
+	module_put(iommu_dev->ops->owner);
  err_free:
  	dev->iommu->iommu_dev = NULL;
  	dev_iommu_free(dev);
@@ -2855,9 +2900,10 @@ bool iommu_default_passthrough(void)
  }
  EXPORT_SYMBOL_GPL(iommu_default_passthrough);
 
-static const struct iommu_device *iommu_from_fwnode(const struct fwnode_handle *fwnode)
+static struct iommu_device *iommu_from_fwnode(
+	const struct fwnode_handle *fwnode)
  {
-	const struct iommu_device *iommu, *ret = NULL;
+	struct iommu_device *iommu, *ret = NULL;
 
  	spin_lock(&iommu_device_lock);
  	list_for_each_entry(iommu, &iommu_device_list, list)
include/linux/iommu.h +3 -0
index c30d12e16473df..52e74ccdb4dc79 100644
@@ -48,6 +48,7 @@ struct iommufd_ctx;
  struct iommufd_viommu;
  struct msi_desc;
  struct msi_msg;
+struct iommu_device;
 
  #define IOMMU_FAULT_PERM_READ	(1 << 0) /* read */
  #define IOMMU_FAULT_PERM_WRITE	(1 << 1) /* write */
@@ -680,6 +681,8 @@ struct iommu_ops {
  		struct device *dev, struct iommu_domain *parent, u32 flags,
  		const struct iommu_user_data *user_data);
 
+	int (*probe_device_fwspec)(struct iommu_device *iommu,
+				   struct device *dev);
  	struct iommu_device *(*probe_device)(struct device *dev);
  	void (*release_device)(struct device *dev);
  	void (*probe_finalize)(struct device *dev);
reviewer
#12 lore raw

Re: [PATCH 11/14] iommu/omap: fix device leaks on probe_device()

In reply to Johan Hovold
Quoted 55 lines · Johan Hovold

Make sure to drop the reference taken to the iommu platform devices during probe_device() on errors and when the device is later released.

Fixes 9d5018deec86 ("iommu/omap: Add support to program multiple iommus")
Fixes 7d6827748d54 ("iommu/omap: Fix iommu archdata name for DT-based devices")
Cc Suman Anna <[email protected]>
---
  drivers/iommu/omap-iommu.c | 27 +++++++++++++++++++--------
  1 file changed, 19 insertions(+), 8 deletions(-)
drivers/iommu/omap-iommu.c +7 -7
index 6fb93927bdb9..77023d49bd24 100644
@@ -1636,7 +1636,7 @@ static struct iommu_device *omap_iommu_probe_device(struct device *dev)
  	struct platform_device *pdev;
  	struct omap_iommu *oiommu;
  	struct device_node *np;
-	int num_iommus, i;
+	int num_iommus, i, ret;
 
  	/*
  	 * Allocate the per-device iommu structure for DT-based devices.
@@ -1663,22 +1663,22 @@ static struct iommu_device *omap_iommu_probe_device(struct device *dev)
  	for (i = 0, tmp = arch_data; i < num_iommus; i++, tmp++) {
  		np = of_parse_phandle(dev->of_node, "iommus", i);
  		if (!np) {
-			kfree(arch_data);
-			return ERR_PTR(-EINVAL);
+			ret = -EINVAL;
+			goto err_put_iommus;
  		}
 
  		pdev = of_find_device_by_node(np);
  		if (!pdev) {
  			of_node_put(np);
-			kfree(arch_data);
-			return ERR_PTR(-ENODEV);
+			ret = -ENODEV;
+			goto err_put_iommus;
  		}
 
  		oiommu = platform_get_drvdata(pdev);
  		if (!oiommu) {
  			of_node_put(np);
-			kfree(arch_data);
-			return ERR_PTR(-EINVAL);
+			ret = -EINVAL;
+			goto err_put_iommus;
  		}
 
  		tmp->iommu_dev = oiommu;

drivers/iommu/omap-iommu.c static struct iommu_device *omap_iommu_probe_device(struct device *dev) · line 1703

Quoted 7 lines
drivers/iommu/omap-iommu.c +4 -0
@@ -1697,17 +1697,28 @@ static struct iommu_device *omap_iommu_probe_device(struct device *dev)
  	oiommu = arch_data->iommu_dev;
 
  	return &oiommu->iommu;
+
+err_put_iommus:
+	for (tmp = arch_data; tmp->dev; tmp++)
+		put_device(tmp->dev);

This should just pair with the of_node_put() calls (other than the first one, of course), i.e. do it in the success path as well and drop the release_device change below. It doesn't serve any purpose for client devices to hold additional references on the IOMMU device when those are strictly within the lifetime of the IOMMU driver being bound to it anyway.

Thanks,
Robin.

Quoted 22 lines
drivers/iommu/omap-iommu.c +8 -1
+
+	kfree(arch_data);
+
+	return ERR_PTR(ret);
  }
 
  static void omap_iommu_release_device(struct device *dev)
  {
  	struct omap_iommu_arch_data *arch_data = dev_iommu_priv_get(dev);
+	struct omap_iommu_arch_data *tmp;
 
  	if (!dev->of_node || !arch_data)
  		return;
 
-	kfree(arch_data);
+	for (tmp = arch_data; tmp->dev; tmp++)
+		put_device(tmp->dev);
 
+	kfree(arch_data);
  }
 
  static int omap_iommu_of_xlate(struct device *dev, const struct of_phandle_args *args)
reviewer Acked-by on the v1 series
#25 lore raw
In reply to Johan Hovold
Quoted 5 lines · Johan Hovold

This series fixes device leaks in the iommu drivers, which pretty consistently failed to drop the reference taken by of_find_device_by_node() when looking up iommu platform devices.

Included are also a couple of related cleanups.

Modulo the nitpick for OMAP,

We could in fact also clean up nearly all the NULL checks in these areas that are now entirely redundant since per-instance ops lookup, but that might just le;ad to more patches from the static checker brigade trying to put them back... :/

Thanks,
Robin.

Quoted 30 lines

Johan

Johan Hovold (14):
iommu/apple-dart: fix device leak on of_xlate() iommu/qcom: fix device leak on of_xlate() iommu/exynos: fix device leak on of_xlate() iommu/ipmmu-vmsa: fix device leak on of_xlate() iommu/mediatek: fix device leak on of_xlate() iommu/mediatek: fix device leaks on probe() iommu/mediatek: simplify dt parsing error handling iommu/mediatek-v1: fix device leak on probe_device() iommu/mediatek-v1: fix device leaks on probe() iommu/mediatek-v1: add missing larb count sanity check iommu/omap: fix device leaks on probe_device() iommu/omap: simplify probe_device() error handling iommu/sun50i: fix device leak on of_xlate() iommu/tegra: fix device leak on probe_device()

  drivers/iommu/apple-dart.c              |  2 ++
  drivers/iommu/arm/arm-smmu/qcom_iommu.c | 10 +++-----
  drivers/iommu/exynos-iommu.c            |  9 +++----
  drivers/iommu/ipmmu-vmsa.c              |  2 ++
  drivers/iommu/mtk_iommu.c               | 33 +++++++++++++++++--------
  drivers/iommu/mtk_iommu_v1.c            | 28 +++++++++++++++++----
  drivers/iommu/omap-iommu.c              | 32 +++++++++++++++---------
  drivers/iommu/sun50i-iommu.c            |  2 ++
  drivers/iommu/tegra-smmu.c              |  5 ++--
  9 files changed, 81 insertions(+), 42 deletions(-)
#21 lore raw
In reply to Robin Murphy
Quoted 8 lines · Robin Murphy
Quoted 3 lines

However the SMMU drivers are doing this under the iommu_probe_device_lock and not stashing the pointer into a drvdata where there is no locking protecting it.

Huh? Every .of_xlate call is under probe_device_lock just as much as .probe_device calls are; they *have* to be, since they too are in the position of working with dev->iommu before dev->iommu_group is set to guarantee its stability.

Yes, of_xlate is under the lock, but IIRC there are still cases where probe gets deferred, the probe_device_lock is unlocked, and the drvdata continues to exist.

Quoted 2 lines

indeed the driver cannot be removed, because we hold a module reference around the call

That's not how module reference counts work. Drivers can be unbound through sysfs at any time.

Quoted 7 lines
Quoted 6 lines

Anyhow, I drafted a nice fix for all of this. After all the rework it is trivial for the core code to pass in the struct iommu_device * to probe and then most of the drivers just drop this ugly code completely.

https://github.com/jgunthorpe/linux/commits/iommu-fwspec/

Quoted 7 lines

Eww, that is neither nice nor a "fix". Once again it's just piling on a load of extra complexity to have multiple confusingly-overlapping-but-different ways of doing the same thing, one of which is still the exact same one you've decided to object to because you've failed to understand it (as demonstrated by the commit message below, the obvious bug in the hideous mess below that, and at a glance the other patches actively *breaking* at least one driver.)

It is hard to take you seriously with such vauge objections Robin. Please try to be constructive. If you are specific I'll go fix the things and maybe other people besides you can understand this stuff.

I'm shocked you disagree with the core code helping the drivers find their iommu_driver. This seems like very basic obvious good stuff. We don't need all sorts of different open coded ugly ways for drivers to do this. It removes 200 lines of junk for drivers :\

Quoted 4 lines

drivers/iommu/iommu.c void dev_iommu_priv_set(struct device *dev, void *priv) · line 436

Quoted 4 lines
drivers/iommu/iommu.c +4 -0
+			iommu = fwspec_iommu->ops->probe_device(dev);
+			if (IS_ERR(iommu))
+				return PTR_ERR(iommu);
+			if (WARN_ON(iommu != fwspec_iommu)) {

This is at least one typo.

Jason

#13 lore raw

Re: [PATCH 11/14] iommu/omap: fix device leaks on probe_device()

In reply to Robin Murphy
Quoted 34 lines · Robin Murphy
Quoted 32 lines · Johan Hovold
drivers/iommu/omap-iommu.c +6 -6
@@ -1663,22 +1663,22 @@ static struct iommu_device *omap_iommu_probe_device(struct device *dev)
  	for (i = 0, tmp = arch_data; i < num_iommus; i++, tmp++) {
  		np = of_parse_phandle(dev->of_node, "iommus", i);
  		if (!np) {
-			kfree(arch_data);
-			return ERR_PTR(-EINVAL);
+			ret = -EINVAL;
+			goto err_put_iommus;
  		}
 
  		pdev = of_find_device_by_node(np);
  		if (!pdev) {
  			of_node_put(np);
-			kfree(arch_data);
-			return ERR_PTR(-ENODEV);
+			ret = -ENODEV;
+			goto err_put_iommus;
  		}
 
  		oiommu = platform_get_drvdata(pdev);
  		if (!oiommu) {
  			of_node_put(np);
-			kfree(arch_data);
-			return ERR_PTR(-EINVAL);
+			ret = -EINVAL;
+			goto err_put_iommus;
  		}
 
  		tmp->iommu_dev = oiommu;
@@ -1697,17 +1697,28 @@ static struct iommu_device *omap_iommu_probe_device(struct device *dev)
  	oiommu = arch_data->iommu_dev;
 
Quoted 11 lines

drivers/iommu/omap-iommu.c static struct iommu_device *omap_iommu_probe_device(struct device *dev) · line 1703

Quoted 5 lines
drivers/iommu/omap-iommu.c +4 -0
  	return &oiommu->iommu;
+
+err_put_iommus:
+	for (tmp = arch_data; tmp->dev; tmp++)
+		put_device(tmp->dev);

This should just pair with the of_node_put() calls (other than the first one, of course), i.e. do it in the success path as well and drop the release_device change below. It doesn't serve any purpose for client devices to hold additional references on the IOMMU device when those are strictly within the lifetime of the IOMMU driver being bound to it anyway.

I kept the reference until release() (even if not strictly needed) as I mistakenly thought the driver was using the arch data device pointer directly.

Turns out that one has never been used so I'll drop it as well as part of v2.

Johan

#24 lore raw
In reply to Jason Gunthorpe
Quoted 7 lines · Jason Gunthorpe
Quoted 2 lines · Johan Hovold

This seems to be more a case of developers not reading documentation and copying implementations from existing drivers.

IMHO the drivers are mis-using of_xlate, it shouldn't be looking up drvdata at all.

Ok, but that would in any case be a separate issue.

Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

IDK if it is worth fixing like this, or if more effort should be put to make the drivers use of_xlate properly - the arm smmu drivers show the only way to use it..

As Robin pointed out, those drivers just drop the reference as they should (even if I'd drop the reference after looking up the driver data).

Quoted 5 lines

So I wrote the series to clean this up and drop the calls to of_find_device_by_node() and so on.

I left dart, exynos, msm and omap as-is, so I suggest you trim this series to only those drivers and rely on my version to fix the rest.

Looks like there's some room for unification and code reuse, but that can be done on top of these fixes.

Johan