Fix TMC resource leak issues
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
---
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 | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
---
base-commit: e5aad2bdbbee783611d32599f89cea3f12f9593d
change-id: 20261130-fix-tmc-issues-013ef3a11ea0
Best regards,
Signature · 1 line
Jie Gan <[email protected]>
PATCH 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.
--- drivers/hwtracing/coresight/coresight-tmc-core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwtracing/coresight/coresight-tmc-core.c +1 -1
@@ -880,10 +880,10 @@ static int __tmc_probe(struct device *dev, struct resource *res) goto out; } -out:if (is_tmc_crashdata_valid (drvdata) && !tmc_prepare_crashdata (drvdata))register_crash_dev_interface (drvdata, desc.name); +out:return ret; }
Signature · 1 line
2.34.1
Suzuki K Poulose
maintainerRe: [PATCH 1/2] coresight: tmc: register the crash data interface only on probe success
Quoted 25 lines · Jie Gan
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()andtmc_prepare_crashdata()log throughdrvdata->csdev, which is then NULL, an error pointer or an unregistered device, andregister_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.
Fixes d58a70bdab57 ("coresight: tmc: Add support for reading crash data")Reported-by Sashiko <[email protected]>Signed-off-by Jie Gan <[email protected]>--- drivers/hwtracing/coresight/coresight-tmc-core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/hwtracing/coresight/coresight-tmc-core.c +0
-0@@ -880,10 +880,10 @@ static int __tmc_probe(struct device *dev, struct resource *res) goto out;
drivers/hwtracing/coresight/coresight-tmc-core.c static int __tmc_probe(struct device *dev, struct resource *res) · line 889
Quoted 10 lines
drivers/hwtracing/coresight/coresight-tmc-core.c +1
-1} -out:if (is_tmc_crashdata_valid (drvdata) && !tmc_prepare_crashdata (drvdata))register_crash_dev_interface (drvdata, desc.name); +out:return ret; }
Goto for retuning just the error code, doesn't look like a good practice. Please could we fix all goto out with return ?
See:
"The goto statement comes in handy when a function exits from multiple locations and some common work such as cleanup has to be done. *If there is no cleanup needed then just return directly.*"
https://www.kernel.org/doc/html/latest/process/coding-style.html#centralized-exiting-of-functions
Cheers
Suzuki
Re: [PATCH 1/2] coresight: tmc: register the crash data interface only on probe success
Quoted 39 lines · Suzuki K Poulose
Quoted 27 lines · Jie Gan
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()andtmc_prepare_crashdata()log throughdrvdata->csdev, which is then NULL, an error pointer or an unregistered device, andregister_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.
Fixes d58a70bdab57 ("coresight: tmc: Add support for reading crash data")Reported-by Sashiko <[email protected]>Closes https://sashiko.dev/#/bug/linux-f23df7b7- bae5-4fa0-96ef-4e83632058e8Signed-off-by Jie Gan <[email protected]>---
drivers/hwtracing/coresight/coresight-tmc-core.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)drivers/hwtracing/coresight/coresight-tmc-core.c +0
-1@@ -880,10 +880,10 @@ static int __tmc_probe(struct device *dev, struct resource *res) goto out; } -out:Quoted 7 lines
if (
is_tmc_crashdata_valid(drvdata)&& !tmc_prepare_crashdata(drvdata))register_crash_dev_interface(drvdata, desc.name); +out:
return ret;
}Goto for retuning just the error code, doesn't look like a good practice. Please could we fix all goto out with return ?
Quoted 6 lines
See:
"The goto statement comes in handy when a function exits from multiple locations and some common work such as cleanup has to be done. *If there is no cleanup needed then just return directly.*"
Acked and noted.
Thanks,
Jie
Quoted 7 lines
https://www.kernel.org/doc/html/latest/process/coding- style.html#centralized-exiting-of-functions
Cheers
Suzuki
PATCH 2/2 coresight: tmc: fix the reserved memory mapping leak
In reply to Jie Gantmc_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.
--- drivers/hwtracing/coresight/coresight-tmc-core.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-core.c +9 -8
@@ -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