Skip to contentSkip to conversation

Fix TMC resource leak issues

PATCH New Jie Gan 1 participant 3 messages Dec 1

Subsystems (2)
lore
Share & tools
lore

Jie Gan

author
#0 lore raw

v2 cover Fix TMC resource leak issues

Fix the TMC resource leak issues reported by Sashiko:
https://sashiko.dev/#/bug/linux-f23df7b7-bae5-4fa0-96ef-4e83632058e8

---
Changes in v2:
- remove the out label and fix all goto out with return (Suzuki).
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Jie Gan (2):
coresight: tmc: register the crash data interface only on probe success
coresight: tmc: fix the reserved memory mapping leak

 drivers/hwtracing/coresight/coresight-tmc-core.c | 54 ++++++++++--------------
 1 file changed, 22 insertions(+), 32 deletions(-)

---
base-commit: e5aad2bdbbee783611d32599f89cea3f12f9593d
change-id: 20261130-fix-tmc-issues-013ef3a11ea0

Best regards,

Signature · 1 line
Jie Gan <[email protected]>
author
#1 lore raw

PATCH v2 1/2 coresight: tmc: register the crash data interface only on probe success

The crash data interface is registered after the out label of __tmc_probe(), which every error path jumps to. When the probe fails while the reserved regions hold valid crash data, is_tmc_crashdata_valid() and tmc_prepare_crashdata() log through drvdata->csdev, which is then NULL, an error pointer or an unregistered device, and register_crash_dev_interface() can register a misc device whose driver data is freed when the probe failure is unwound.

Only register the crash data interface once the probe has succeeded. The out label then only returns the error code, so return directly from the error paths instead.

Fixes d58a70bdab57 ("coresight: tmc: Add support for reading crash data")
---
 drivers/hwtracing/coresight/coresight-tmc-core.c | 37 +++++++++---------------
 1 file changed, 13 insertions(+), 24 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-core.c +13 -24
index 0ef4b817b297..7ed1a9720a0e 100644
@@ -784,14 +784,10 @@ static int __tmc_probe(struct device *dev, struct resource *res)
 	if (ret)
 		return ret;
 
-	ret = -ENOMEM;
-
 	/* Validity for the resource is already checked by the AMBA core */
 	base = devm_ioremap_resource(dev, res);
-	if (IS_ERR(base)) {
-		ret = PTR_ERR(base);
-		goto out;
-	}
+	if (IS_ERR(base))
+		return PTR_ERR(base);
 
 	drvdata->base = base;
 	desc.access = CSDEV_ACCESS_IOMEM(base);
@@ -831,7 +827,7 @@ static int __tmc_probe(struct device *dev, struct resource *res)
 		desc.ops = &tmc_etr_cs_ops;
 		ret = tmc_etr_setup_caps(dev, devid, &desc.access);
 		if (ret)
-			goto out;
+			return ret;
 		idr_init(&drvdata->idr);
 		mutex_init(&drvdata->idr_mutex);
 		dev_list = "tmc_etr";
@@ -846,30 +842,23 @@ static int __tmc_probe(struct device *dev, struct resource *res)
 		break;
 	default:
 		pr_err("%s: Unsupported TMC config\n", desc.name);
-		ret = -EINVAL;
-		goto out;
+		return -EINVAL;
 	}
 
 	desc.name = coresight_alloc_device_name(dev_list, dev);
-	if (!desc.name) {
-		ret = -ENOMEM;
-		goto out;
-	}
+	if (!desc.name)
+		return -ENOMEM;
 
 	pdata = coresight_get_platform_data(dev);
-	if (IS_ERR(pdata)) {
-		ret = PTR_ERR(pdata);
-		goto out;
-	}
+	if (IS_ERR(pdata))
+		return PTR_ERR(pdata);
 	dev->platform_data = pdata;
 	desc.pdata = pdata;
 
 	coresight_clear_self_claim_tag(&desc.access);
 	drvdata->csdev = coresight_register(&desc);
-	if (IS_ERR(drvdata->csdev)) {
-		ret = PTR_ERR(drvdata->csdev);
-		goto out;
-	}
+	if (IS_ERR(drvdata->csdev))
+		return PTR_ERR(drvdata->csdev);
 
 	drvdata->miscdev.name = desc.name;
 	drvdata->miscdev.minor = MISC_DYNAMIC_MINOR;
@@ -877,14 +866,14 @@ static int __tmc_probe(struct device *dev, struct resource *res)
 	ret = misc_register(&drvdata->miscdev);
 	if (ret) {
 		coresight_unregister(drvdata->csdev);
-		goto out;
+		return ret;
 	}
 
-out:
 	if (is_tmc_crashdata_valid(drvdata) &&
 	    !tmc_prepare_crashdata(drvdata))
 		register_crash_dev_interface(drvdata, desc.name);
-	return ret;
+
+	return 0;
 }
 
 static int tmc_probe(struct amba_device *adev, const struct amba_id *id)
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v2 2/2 coresight: tmc: fix the reserved memory mapping leak

In reply to Jie Gan

tmc_get_reserved_region() maps the reserved trace buffer and crash metadata regions with memremap(), but never unmaps them, neither when the probe fails nor when the device is removed.

Use devm_memremap() so that the mappings are released with the device. devm_memremap() returns an error pointer on failure, so only store a mapping once it succeeded, keeping the buffer pointer NULL otherwise as tmc_has_reserved_buffer() and tmc_has_crash_mdata_buffer() expect.

Fixes 91a2086aa6d1 ("coresight: tmc-etr: Add support to use reserved trace memory")
Fixes 6dbcbcfc4496 ("coresight: tmc: Enable panic sync handling")
---
 drivers/hwtracing/coresight/coresight-tmc-core.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-core.c +9 -8
index 7ed1a9720a0e..3451321bba07 100644
@@ -646,32 +646,33 @@ static void tmc_get_reserved_region(struct device *parent)
 {
 	struct tmc_drvdata *drvdata = dev_get_drvdata(parent);
 	struct resource res;
+	void *vaddr;
 
 	if (of_tmc_get_reserved_resource_by_name(parent, "tracedata", &res))
 		return;
 
-	drvdata->resrv_buf.vaddr = memremap(res.start,
-						resource_size(&res),
-						MEMREMAP_WC);
-	if (IS_ERR_OR_NULL(drvdata->resrv_buf.vaddr)) {
+	vaddr = devm_memremap(parent, res.start, resource_size(&res),
+			      MEMREMAP_WC);
+	if (IS_ERR(vaddr)) {
 		dev_err(parent, "Reserved trace buffer mapping failed\n");
 		return;
 	}
 
+	drvdata->resrv_buf.vaddr = vaddr;
 	drvdata->resrv_buf.paddr = res.start;
 	drvdata->resrv_buf.size  = resource_size(&res);
 
 	if (of_tmc_get_reserved_resource_by_name(parent, "metadata", &res))
 		return;
 
-	drvdata->crash_mdata.vaddr = memremap(res.start,
-					       resource_size(&res),
-					       MEMREMAP_WC);
-	if (IS_ERR_OR_NULL(drvdata->crash_mdata.vaddr)) {
+	vaddr = devm_memremap(parent, res.start, resource_size(&res),
+			      MEMREMAP_WC);
+	if (IS_ERR(vaddr)) {
 		dev_err(parent, "Metadata memory mapping failed\n");
 		return;
 	}
 
+	drvdata->crash_mdata.vaddr = vaddr;
 	drvdata->crash_mdata.paddr = res.start;
 	drvdata->crash_mdata.size  = resource_size(&res);
 }
Signature · 1 line
2.34.1