stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 01/14] iommu/apple-dart: fix device leak on of_xlate()
       [not found] <20250925122756.10910-1-johan@kernel.org>
@ 2025-09-25 12:27 ` Johan Hovold
  2025-09-25 12:27 ` [PATCH 02/14] iommu/qcom: " Johan Hovold
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: Johan Hovold @ 2025-09-25 12:27 UTC (permalink / raw)
  To: Joerg Roedel, Will Deacon
  Cc: Robin Murphy, Sven Peter, Janne Grunau, Rob Clark,
	Marek Szyprowski, Yong Wu, Matthias Brugger,
	AngeloGioacchino Del Regno, Chen-Yu Tsai, Thierry Reding,
	Krishna Reddy, iommu, linux-kernel, Johan Hovold, stable

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: stable@vger.kernel.org	# 5.15
Cc: Sven Peter <sven@kernel.org>
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/iommu/apple-dart.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c
index 190f28d76615..1aa7c10262a8 100644
--- a/drivers/iommu/apple-dart.c
+++ b/drivers/iommu/apple-dart.c
@@ -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];
-- 
2.49.1


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

* [PATCH 02/14] iommu/qcom: fix device leak on of_xlate()
       [not found] <20250925122756.10910-1-johan@kernel.org>
  2025-09-25 12:27 ` [PATCH 01/14] iommu/apple-dart: fix device leak on of_xlate() Johan Hovold
@ 2025-09-25 12:27 ` Johan Hovold
  2025-09-25 12:27 ` [PATCH 03/14] iommu/exynos: " Johan Hovold
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: Johan Hovold @ 2025-09-25 12:27 UTC (permalink / raw)
  To: Joerg Roedel, Will Deacon
  Cc: Robin Murphy, Sven Peter, Janne Grunau, Rob Clark,
	Marek Szyprowski, Yong Wu, Matthias Brugger,
	AngeloGioacchino Del Regno, Chen-Yu Tsai, Thierry Reding,
	Krishna Reddy, iommu, linux-kernel, Johan Hovold, stable, Yu Kuai

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: stable@vger.kernel.org	# 4.14: e2eae09939a8
Cc: Rob Clark <robin.clark@oss.qualcomm.com>
Cc: Yu Kuai <yukuai3@huawei.com>
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/iommu/arm/arm-smmu/qcom_iommu.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/drivers/iommu/arm/arm-smmu/qcom_iommu.c b/drivers/iommu/arm/arm-smmu/qcom_iommu.c
index c5be95e56031..9c1166a3af6c 100644
--- a/drivers/iommu/arm/arm-smmu/qcom_iommu.c
+++ b/drivers/iommu/arm/arm-smmu/qcom_iommu.c
@@ -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);
-- 
2.49.1


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

* [PATCH 03/14] iommu/exynos: fix device leak on of_xlate()
       [not found] <20250925122756.10910-1-johan@kernel.org>
  2025-09-25 12:27 ` [PATCH 01/14] iommu/apple-dart: fix device leak on of_xlate() Johan Hovold
  2025-09-25 12:27 ` [PATCH 02/14] iommu/qcom: " Johan Hovold
@ 2025-09-25 12:27 ` Johan Hovold
  2025-09-25 12:27 ` [PATCH 04/14] iommu/ipmmu-vmsa: " Johan Hovold
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: Johan Hovold @ 2025-09-25 12:27 UTC (permalink / raw)
  To: Joerg Roedel, Will Deacon
  Cc: Robin Murphy, Sven Peter, Janne Grunau, Rob Clark,
	Marek Szyprowski, Yong Wu, Matthias Brugger,
	AngeloGioacchino Del Regno, Chen-Yu Tsai, Thierry Reding,
	Krishna Reddy, iommu, linux-kernel, Johan Hovold, stable

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: stable@vger.kernel.org	# 4.2
Cc: Marek Szyprowski <m.szyprowski@samsung.com>
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/iommu/exynos-iommu.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/drivers/iommu/exynos-iommu.c b/drivers/iommu/exynos-iommu.c
index b6edd178fe25..ce9e935cb84c 100644
--- a/drivers/iommu/exynos-iommu.c
+++ b/drivers/iommu/exynos-iommu.c
@@ -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);
-- 
2.49.1


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

* [PATCH 04/14] iommu/ipmmu-vmsa: fix device leak on of_xlate()
       [not found] <20250925122756.10910-1-johan@kernel.org>
                   ` (2 preceding siblings ...)
  2025-09-25 12:27 ` [PATCH 03/14] iommu/exynos: " Johan Hovold
@ 2025-09-25 12:27 ` Johan Hovold
  2025-09-25 12:27 ` [PATCH 05/14] iommu/mediatek: " Johan Hovold
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: Johan Hovold @ 2025-09-25 12:27 UTC (permalink / raw)
  To: Joerg Roedel, Will Deacon
  Cc: Robin Murphy, Sven Peter, Janne Grunau, Rob Clark,
	Marek Szyprowski, Yong Wu, Matthias Brugger,
	AngeloGioacchino Del Regno, Chen-Yu Tsai, Thierry Reding,
	Krishna Reddy, iommu, linux-kernel, Johan Hovold, stable,
	Magnus Damm

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: stable@vger.kernel.org	# 4.14
Cc: Magnus Damm <damm+renesas@opensource.se>
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/iommu/ipmmu-vmsa.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/iommu/ipmmu-vmsa.c b/drivers/iommu/ipmmu-vmsa.c
index ffa892f65714..02a2a55ffa0a 100644
--- a/drivers/iommu/ipmmu-vmsa.c
+++ b/drivers/iommu/ipmmu-vmsa.c
@@ -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;
 }
 
-- 
2.49.1


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

* [PATCH 05/14] iommu/mediatek: fix device leak on of_xlate()
       [not found] <20250925122756.10910-1-johan@kernel.org>
                   ` (3 preceding siblings ...)
  2025-09-25 12:27 ` [PATCH 04/14] iommu/ipmmu-vmsa: " Johan Hovold
@ 2025-09-25 12:27 ` Johan Hovold
  2025-09-25 12:27 ` [PATCH 06/14] iommu/mediatek: fix device leaks on probe() Johan Hovold
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: Johan Hovold @ 2025-09-25 12:27 UTC (permalink / raw)
  To: Joerg Roedel, Will Deacon
  Cc: Robin Murphy, Sven Peter, Janne Grunau, Rob Clark,
	Marek Szyprowski, Yong Wu, Matthias Brugger,
	AngeloGioacchino Del Regno, Chen-Yu Tsai, Thierry Reding,
	Krishna Reddy, iommu, linux-kernel, Johan Hovold, stable

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: stable@vger.kernel.org	# 4.6
Cc: Yong Wu <yong.wu@mediatek.com>
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/iommu/mtk_iommu.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/iommu/mtk_iommu.c b/drivers/iommu/mtk_iommu.c
index 0e0285348d2b..8d8e85186188 100644
--- a/drivers/iommu/mtk_iommu.c
+++ b/drivers/iommu/mtk_iommu.c
@@ -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);
-- 
2.49.1


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

* [PATCH 06/14] iommu/mediatek: fix device leaks on probe()
       [not found] <20250925122756.10910-1-johan@kernel.org>
                   ` (4 preceding siblings ...)
  2025-09-25 12:27 ` [PATCH 05/14] iommu/mediatek: " Johan Hovold
@ 2025-09-25 12:27 ` Johan Hovold
  2025-09-25 12:27 ` [PATCH 08/14] iommu/mediatek-v1: fix device leak on probe_device() Johan Hovold
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: Johan Hovold @ 2025-09-25 12:27 UTC (permalink / raw)
  To: Joerg Roedel, Will Deacon
  Cc: Robin Murphy, Sven Peter, Janne Grunau, Rob Clark,
	Marek Szyprowski, Yong Wu, Matthias Brugger,
	AngeloGioacchino Del Regno, Chen-Yu Tsai, Thierry Reding,
	Krishna Reddy, iommu, linux-kernel, Johan Hovold, stable

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: stable@vger.kernel.org	# 4.6
Cc: Yong Wu <yong.wu@mediatek.com>
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/iommu/mtk_iommu.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/drivers/iommu/mtk_iommu.c b/drivers/iommu/mtk_iommu.c
index 8d8e85186188..20a5ba80f983 100644
--- a/drivers/iommu/mtk_iommu.c
+++ b/drivers/iommu/mtk_iommu.c
@@ -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++) {
-- 
2.49.1


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

* [PATCH 08/14] iommu/mediatek-v1: fix device leak on probe_device()
       [not found] <20250925122756.10910-1-johan@kernel.org>
                   ` (5 preceding siblings ...)
  2025-09-25 12:27 ` [PATCH 06/14] iommu/mediatek: fix device leaks on probe() Johan Hovold
@ 2025-09-25 12:27 ` Johan Hovold
  2025-09-25 12:27 ` [PATCH 09/14] iommu/mediatek-v1: fix device leaks on probe() Johan Hovold
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: Johan Hovold @ 2025-09-25 12:27 UTC (permalink / raw)
  To: Joerg Roedel, Will Deacon
  Cc: Robin Murphy, Sven Peter, Janne Grunau, Rob Clark,
	Marek Szyprowski, Yong Wu, Matthias Brugger,
	AngeloGioacchino Del Regno, Chen-Yu Tsai, Thierry Reding,
	Krishna Reddy, iommu, linux-kernel, Johan Hovold, stable,
	Honghui Zhang

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: stable@vger.kernel.org	# 4.8
Cc: Honghui Zhang <honghui.zhang@mediatek.com>
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/iommu/mtk_iommu_v1.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/iommu/mtk_iommu_v1.c b/drivers/iommu/mtk_iommu_v1.c
index 10cc0b1197e8..de9153c0a82f 100644
--- a/drivers/iommu/mtk_iommu_v1.c
+++ b/drivers/iommu/mtk_iommu_v1.c
@@ -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);
-- 
2.49.1


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

* [PATCH 09/14] iommu/mediatek-v1: fix device leaks on probe()
       [not found] <20250925122756.10910-1-johan@kernel.org>
                   ` (6 preceding siblings ...)
  2025-09-25 12:27 ` [PATCH 08/14] iommu/mediatek-v1: fix device leak on probe_device() Johan Hovold
@ 2025-09-25 12:27 ` Johan Hovold
  2025-09-25 12:27 ` [PATCH 11/14] iommu/omap: fix device leaks on probe_device() Johan Hovold
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: Johan Hovold @ 2025-09-25 12:27 UTC (permalink / raw)
  To: Joerg Roedel, Will Deacon
  Cc: Robin Murphy, Sven Peter, Janne Grunau, Rob Clark,
	Marek Szyprowski, Yong Wu, Matthias Brugger,
	AngeloGioacchino Del Regno, Chen-Yu Tsai, Thierry Reding,
	Krishna Reddy, iommu, linux-kernel, Johan Hovold, stable,
	Honghui Zhang

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: stable@vger.kernel.org	# 4.8
Cc: Honghui Zhang <honghui.zhang@mediatek.com>
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/iommu/mtk_iommu_v1.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/drivers/iommu/mtk_iommu_v1.c b/drivers/iommu/mtk_iommu_v1.c
index de9153c0a82f..44b965a2db92 100644
--- a/drivers/iommu/mtk_iommu_v1.c
+++ b/drivers/iommu/mtk_iommu_v1.c
@@ -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)
-- 
2.49.1


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

* [PATCH 11/14] iommu/omap: fix device leaks on probe_device()
       [not found] <20250925122756.10910-1-johan@kernel.org>
                   ` (7 preceding siblings ...)
  2025-09-25 12:27 ` [PATCH 09/14] iommu/mediatek-v1: fix device leaks on probe() Johan Hovold
@ 2025-09-25 12:27 ` Johan Hovold
  2025-10-02 12:05   ` Robin Murphy
  2025-09-25 12:27 ` [PATCH 13/14] iommu/sun50i: fix device leak on of_xlate() Johan Hovold
  2025-09-25 12:27 ` [PATCH 14/14] iommu/tegra: fix device leak on probe_device() Johan Hovold
  10 siblings, 1 reply; 13+ messages in thread
From: Johan Hovold @ 2025-09-25 12:27 UTC (permalink / raw)
  To: Joerg Roedel, Will Deacon
  Cc: Robin Murphy, Sven Peter, Janne Grunau, Rob Clark,
	Marek Szyprowski, Yong Wu, Matthias Brugger,
	AngeloGioacchino Del Regno, Chen-Yu Tsai, Thierry Reding,
	Krishna Reddy, iommu, linux-kernel, Johan Hovold, stable,
	Suman Anna

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: stable@vger.kernel.org	# 3.18
Cc: Suman Anna <s-anna@ti.com>
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/iommu/omap-iommu.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)

diff --git a/drivers/iommu/omap-iommu.c b/drivers/iommu/omap-iommu.c
index 6fb93927bdb9..77023d49bd24 100644
--- a/drivers/iommu/omap-iommu.c
+++ b/drivers/iommu/omap-iommu.c
@@ -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)
-- 
2.49.1


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

* [PATCH 13/14] iommu/sun50i: fix device leak on of_xlate()
       [not found] <20250925122756.10910-1-johan@kernel.org>
                   ` (8 preceding siblings ...)
  2025-09-25 12:27 ` [PATCH 11/14] iommu/omap: fix device leaks on probe_device() Johan Hovold
@ 2025-09-25 12:27 ` Johan Hovold
  2025-09-25 12:27 ` [PATCH 14/14] iommu/tegra: fix device leak on probe_device() Johan Hovold
  10 siblings, 0 replies; 13+ messages in thread
From: Johan Hovold @ 2025-09-25 12:27 UTC (permalink / raw)
  To: Joerg Roedel, Will Deacon
  Cc: Robin Murphy, Sven Peter, Janne Grunau, Rob Clark,
	Marek Szyprowski, Yong Wu, Matthias Brugger,
	AngeloGioacchino Del Regno, Chen-Yu Tsai, Thierry Reding,
	Krishna Reddy, iommu, linux-kernel, Johan Hovold, stable,
	Maxime Ripard

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: stable@vger.kernel.org	# 5.8
Cc: Maxime Ripard <mripard@kernel.org>
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/iommu/sun50i-iommu.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/iommu/sun50i-iommu.c b/drivers/iommu/sun50i-iommu.c
index de10b569d9a9..6306570d57db 100644
--- a/drivers/iommu/sun50i-iommu.c
+++ b/drivers/iommu/sun50i-iommu.c
@@ -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);
 }
 
-- 
2.49.1


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

* [PATCH 14/14] iommu/tegra: fix device leak on probe_device()
       [not found] <20250925122756.10910-1-johan@kernel.org>
                   ` (9 preceding siblings ...)
  2025-09-25 12:27 ` [PATCH 13/14] iommu/sun50i: fix device leak on of_xlate() Johan Hovold
@ 2025-09-25 12:27 ` Johan Hovold
  10 siblings, 0 replies; 13+ messages in thread
From: Johan Hovold @ 2025-09-25 12:27 UTC (permalink / raw)
  To: Joerg Roedel, Will Deacon
  Cc: Robin Murphy, Sven Peter, Janne Grunau, Rob Clark,
	Marek Szyprowski, Yong Wu, Matthias Brugger,
	AngeloGioacchino Del Regno, Chen-Yu Tsai, Thierry Reding,
	Krishna Reddy, iommu, linux-kernel, Johan Hovold, stable,
	Thierry Reding

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: stable@vger.kernel.org	# 3.19
Cc: Thierry Reding <treding@nvidia.com>
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/iommu/tegra-smmu.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/iommu/tegra-smmu.c b/drivers/iommu/tegra-smmu.c
index 36cdd5fbab07..f6f26a072820 100644
--- a/drivers/iommu/tegra-smmu.c
+++ b/drivers/iommu/tegra-smmu.c
@@ -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;
 }
-- 
2.49.1


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

* Re: [PATCH 11/14] iommu/omap: fix device leaks on probe_device()
  2025-09-25 12:27 ` [PATCH 11/14] iommu/omap: fix device leaks on probe_device() Johan Hovold
@ 2025-10-02 12:05   ` Robin Murphy
  2025-10-02 14:45     ` Johan Hovold
  0 siblings, 1 reply; 13+ messages in thread
From: Robin Murphy @ 2025-10-02 12:05 UTC (permalink / raw)
  To: Johan Hovold, Joerg Roedel, Will Deacon
  Cc: Sven Peter, Janne Grunau, Rob Clark, Marek Szyprowski, Yong Wu,
	Matthias Brugger, AngeloGioacchino Del Regno, Chen-Yu Tsai,
	Thierry Reding, Krishna Reddy, iommu, linux-kernel, stable,
	Suman Anna

On 2025-09-25 1:27 pm, Johan Hovold wrote:
> 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: stable@vger.kernel.org	# 3.18
> Cc: Suman Anna <s-anna@ti.com>
> Signed-off-by: Johan Hovold <johan@kernel.org>
> ---
>   drivers/iommu/omap-iommu.c | 27 +++++++++++++++++++--------
>   1 file changed, 19 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/iommu/omap-iommu.c b/drivers/iommu/omap-iommu.c
> index 6fb93927bdb9..77023d49bd24 100644
> --- a/drivers/iommu/omap-iommu.c
> +++ b/drivers/iommu/omap-iommu.c
> @@ -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);

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.

> +
> +	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)

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

* Re: [PATCH 11/14] iommu/omap: fix device leaks on probe_device()
  2025-10-02 12:05   ` Robin Murphy
@ 2025-10-02 14:45     ` Johan Hovold
  0 siblings, 0 replies; 13+ messages in thread
From: Johan Hovold @ 2025-10-02 14:45 UTC (permalink / raw)
  To: Robin Murphy
  Cc: Joerg Roedel, Will Deacon, Sven Peter, Janne Grunau, Rob Clark,
	Marek Szyprowski, Yong Wu, Matthias Brugger,
	AngeloGioacchino Del Regno, Chen-Yu Tsai, Thierry Reding,
	Krishna Reddy, iommu, linux-kernel, stable, Suman Anna

On Thu, Oct 02, 2025 at 01:05:08PM +0100, Robin Murphy wrote:
> On 2025-09-25 1:27 pm, Johan Hovold wrote:

> > @@ -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);
> 
> 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

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

end of thread, other threads:[~2025-10-02 14:45 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20250925122756.10910-1-johan@kernel.org>
2025-09-25 12:27 ` [PATCH 01/14] iommu/apple-dart: fix device leak on of_xlate() Johan Hovold
2025-09-25 12:27 ` [PATCH 02/14] iommu/qcom: " Johan Hovold
2025-09-25 12:27 ` [PATCH 03/14] iommu/exynos: " Johan Hovold
2025-09-25 12:27 ` [PATCH 04/14] iommu/ipmmu-vmsa: " Johan Hovold
2025-09-25 12:27 ` [PATCH 05/14] iommu/mediatek: " Johan Hovold
2025-09-25 12:27 ` [PATCH 06/14] iommu/mediatek: fix device leaks on probe() Johan Hovold
2025-09-25 12:27 ` [PATCH 08/14] iommu/mediatek-v1: fix device leak on probe_device() Johan Hovold
2025-09-25 12:27 ` [PATCH 09/14] iommu/mediatek-v1: fix device leaks on probe() Johan Hovold
2025-09-25 12:27 ` [PATCH 11/14] iommu/omap: fix device leaks on probe_device() Johan Hovold
2025-10-02 12:05   ` Robin Murphy
2025-10-02 14:45     ` Johan Hovold
2025-09-25 12:27 ` [PATCH 13/14] iommu/sun50i: fix device leak on of_xlate() Johan Hovold
2025-09-25 12:27 ` [PATCH 14/14] iommu/tegra: fix device leak on probe_device() Johan Hovold

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).