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

Jie Gan jie.gan at oss.qualcomm.com
Mon Nov 30 01:22:26 PST 2026


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")
Reported-by: Sashiko <sashiko-bot at kernel.org>
Closes: https://sashiko.dev/#/bug/linux-f23df7b7-bae5-4fa0-96ef-4e83632058e8
Signed-off-by: Jie Gan <jie.gan at oss.qualcomm.com>
---
 drivers/hwtracing/coresight/coresight-tmc-core.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/drivers/hwtracing/coresight/coresight-tmc-core.c b/drivers/hwtracing/coresight/coresight-tmc-core.c
index 754a9b10b7d2..fd653cfbd4ae 100644
--- a/drivers/hwtracing/coresight/coresight-tmc-core.c
+++ b/drivers/hwtracing/coresight/coresight-tmc-core.c
@@ -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);
 }

-- 
2.34.1




More information about the linux-arm-kernel mailing list