Skip to contentSkip to conversation

coresight: Fix fwnode reference leaks on probe failure

PATCH New Yige Jiang 1 participant 4 messages Dec 2

Subsystems (2)
lore
Share & tools
lore

Yige Jiang

author
#0 lore raw

v3 cover coresight: Fix fwnode reference leaks on probe failure

The CoreSight core releases the destination fwnode references acquired during platform data lookup when registration fails or the device is unregistered. Move the lookups in TPDM, TNOC and TPDA after the other probe steps that can fail. TNOC and TPDA also return the trace IDs they allocated if lookup or registration fails.

Changes in v3:
- Group the three patches in one thread following James Clark's feedback. No code changes from v2.
- Review: https://lore.kernel.org/r/[email protected]
- v2 was posted as three separate threads:
https://lore.kernel.org/r/[email protected]
https://lore.kernel.org/r/[email protected]
https://lore.kernel.org/r/[email protected]

Changes in v2:
- Rebase the TPDM fix on the CoreSight next tree.
- Add separate TNOC and TPDA fixes following the review of v1.
- v1: https://lore.kernel.org/all/[email protected]/

Testing on the v2 code: the baseline and patched TPDM, TNOC and TPDA objects built on arm64 with GCC 14.2.0 and W=1, without warnings. No hardware or runtime tests were performed.

Yige Jiang (3):
coresight: tpdm: Fix fwnode reference leak on probe failure
coresight: tnoc: Fix fwnode reference leak on probe failure
coresight: tpda: Fix fwnode reference leak on probe failure

 drivers/hwtracing/coresight/coresight-tnoc.c | 22 +++++++++++++--------
 drivers/hwtracing/coresight/coresight-tpda.c | 29 ++++++++++++++++++----------
 drivers/hwtracing/coresight/coresight-tpdm.c | 11 ++++++-----
 3 files changed, 39 insertions(+), 23 deletions(-)

base-commit: b7d7462c7f973b65de511db6bebb99fb29946ed8

author
#1 lore raw

PATCH v3 1/3 coresight: tpdm: Fix fwnode reference leak on probe failure

coresight_get_platform_data() takes a reference to the destination firmware node of each output connection. The core drops those references on registration failure or when the device is unregistered. Device resource cleanup alone does not release them.

tpdm_probe() can return before registration if driver data allocation, MMIO mapping, dataset setup or device name allocation fails. Move the platform data lookup after these steps so every successful lookup reaches coresight_register().

Static analysis flagged the TPDM error paths.

Fixes b3c71626a933 ("Coresight: Add coresight TPDM source driver")
---
 drivers/hwtracing/coresight/coresight-tpdm.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/hwtracing/coresight/coresight-tpdm.c +6 -5
index 8464edbba2d45efa5ba041dae70149a946c20465..f4b879bfca1a153f50012757e1b078605fa87944 100644
@@ -1408,11 +1408,6 @@ static int tpdm_probe(struct device *dev, struct resource *res)
 	struct coresight_desc desc = { 0 };
 	int ret;
 
-	pdata = coresight_get_platform_data(dev);
-	if (IS_ERR(pdata))
-		return PTR_ERR(pdata);
-	dev->platform_data = pdata;
-
 	/* driver data*/
 	drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
 	if (!drvdata)
@@ -1448,6 +1443,12 @@ static int tpdm_probe(struct device *dev, struct resource *res)
 	desc.name = coresight_alloc_device_name("tpdm", dev);
 	if (!desc.name)
 		return -ENOMEM;
+
+	pdata = coresight_get_platform_data(dev);
+	if (IS_ERR(pdata))
+		return PTR_ERR(pdata);
+	dev->platform_data = pdata;
+
 	desc.type = CORESIGHT_DEV_TYPE_SOURCE;
 	desc.subtype.source_subtype = CORESIGHT_DEV_SUBTYPE_SOURCE_TPDM;
 	desc.ops = &tpdm_cs_ops;

base-commit: b7d7462c7f973b65de511db6bebb99fb29946ed8

author
#2 lore raw

PATCH v3 2/3 coresight: tnoc: Fix fwnode reference leak on probe failure

In reply to Yige Jiang

_tnoc_probe() parses the output connections before allocating driver data, enabling clocks, mapping registers and setting up the trace ID. If any of these steps fails, probe returns before coresight_register() can take ownership of the destination fwnode references.

Get the platform data after trace ID setup. On either lookup or registration failure, return the ID if the AMBA device acquired one. The platform-device variant does not allocate a trace ID.

Fixes e54a52a28a36 ("coresight: add coresight Trace Network On Chip driver")
---
 drivers/hwtracing/coresight/coresight-tnoc.c | 22 +++++++++++++-------
 1 file changed, 14 insertions(+), 8 deletions(-)
drivers/hwtracing/coresight/coresight-tnoc.c +14 -8
index ec28b439d73d083ffa0de52ef454a9cd54b88175..2f3e2044676358925233c59ff3977fa36555c2c3 100644
@@ -191,11 +191,6 @@ static int _tnoc_probe(struct device *dev, struct resource *res)
 	if (!desc.name)
 		return -ENOMEM;
 
-	pdata = coresight_get_platform_data(dev);
-	if (IS_ERR(pdata))
-		return PTR_ERR(pdata);
-	dev->platform_data = pdata;
-
 	drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
 	if (!drvdata)
 		return -ENOMEM;
@@ -217,6 +212,13 @@ static int _tnoc_probe(struct device *dev, struct resource *res)
 	if (ret)
 		return ret;
 
+	pdata = coresight_get_platform_data(dev);
+	if (IS_ERR(pdata)) {
+		ret = PTR_ERR(pdata);
+		goto err_put_id;
+	}
+	dev->platform_data = pdata;
+
 	desc.ops = &trace_noc_cs_ops;
 	desc.type = CORESIGHT_DEV_TYPE_LINK;
 	desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_MERG;
@@ -226,12 +228,16 @@ static int _tnoc_probe(struct device *dev, struct resource *res)
 	desc.groups = coresight_tnoc_groups;
 	drvdata->csdev = coresight_register(&desc);
 	if (IS_ERR(drvdata->csdev)) {
-		if (drvdata->atid > 0)
-			coresight_trace_id_put_system_id(drvdata->atid);
-		return PTR_ERR(drvdata->csdev);
+		ret = PTR_ERR(drvdata->csdev);
+		goto err_put_id;
 	}
 
 	return 0;
+
+err_put_id:
+	if (drvdata->atid > 0)
+		coresight_trace_id_put_system_id(drvdata->atid);
+	return ret;
 }
 
 static int trace_noc_probe(struct amba_device *adev, const struct amba_id *id)

base-commit: b7d7462c7f973b65de511db6bebb99fb29946ed8

author
#3 lore raw

PATCH v3 3/3 coresight: tpda: Fix fwnode reference leak on probe failure

In reply to Yige Jiang

tpda_probe() gets the platform data before allocating driver data, mapping the registers, reserving a trace ID and allocating a device name. Failure at any of these steps leaves the destination fwnodes of the output connections referenced because the core has yet to take ownership of the platform data.

Allocate the name before reserving the trace ID, then get the platform data immediately before registration. Return the ID if either the platform data lookup or registration fails.

Fixes 5b7916625c01 ("Coresight: Add TPDA link driver")
---
 drivers/hwtracing/coresight/coresight-tpda.c | 29 +++++++++++++-------
 1 file changed, 19 insertions(+), 10 deletions(-)
drivers/hwtracing/coresight/coresight-tpda.c +19 -10
index 63a97931246302f74c0d2fb5e70f43d31f0475b9..62e15fc9b6ec9ad7801257ff706778abd2487fa3 100644
@@ -566,11 +566,6 @@ static int tpda_probe(struct amba_device *adev, const struct amba_id *id)
 	struct coresight_desc desc = { 0 };
 	void __iomem *base;
 
-	pdata = coresight_get_platform_data(dev);
-	if (IS_ERR(pdata))
-		return PTR_ERR(pdata);
-	adev->dev.platform_data = pdata;
-
 	drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
 	if (!drvdata)
 		return -ENOMEM;
@@ -585,13 +580,21 @@ static int tpda_probe(struct amba_device *adev, const struct amba_id *id)
 
 	spin_lock_init(&drvdata->spinlock);
 
+	desc.name = coresight_alloc_device_name("tpda", dev);
+	if (!desc.name)
+		return -ENOMEM;
+
 	ret = tpda_init_default_data(drvdata);
 	if (ret)
 		return ret;
 
-	desc.name = coresight_alloc_device_name("tpda", dev);
-	if (!desc.name)
-		return -ENOMEM;
+	pdata = coresight_get_platform_data(dev);
+	if (IS_ERR(pdata)) {
+		ret = PTR_ERR(pdata);
+		goto err_put_id;
+	}
+	adev->dev.platform_data = pdata;
+
 	desc.type = CORESIGHT_DEV_TYPE_LINK;
 	desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_MERG;
 	desc.ops = &tpda_cs_ops;
@@ -600,13 +603,19 @@ static int tpda_probe(struct amba_device *adev, const struct amba_id *id)
 	desc.groups = tpda_attr_grps;
 	desc.access = CSDEV_ACCESS_IOMEM(base);
 	drvdata->csdev = coresight_register(&desc);
-	if (IS_ERR(drvdata->csdev))
-		return PTR_ERR(drvdata->csdev);
+	if (IS_ERR(drvdata->csdev)) {
+		ret = PTR_ERR(drvdata->csdev);
+		goto err_put_id;
+	}
 
 	pm_runtime_put(&adev->dev);
 
 	dev_dbg(drvdata->dev, "TPDA initialized\n");
 	return 0;
+
+err_put_id:
+	coresight_trace_id_put_system_id(drvdata->atid);
+	return ret;
 }
 
 static void tpda_remove(struct amba_device *adev)

base-commit: b7d7462c7f973b65de511db6bebb99fb29946ed8