mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] coresight: tpdm: Fix fwnode reference leak on probe failure
@ 2026-11-26 13:29 Yige Jiang
  2026-11-26 14:18 ` James Clark
  0 siblings, 1 reply; 3+ messages in thread
From: Yige Jiang @ 2026-11-26 13:29 UTC (permalink / raw)
  To: Suzuki K Poulose, Alexander Shishkin
  Cc: Yige Jiang, Mike Leach, James Clark, Leo Yan, coresight,
	linux-arm-kernel, linux-kernel

coresight_get_platform_data() takes a reference on the fwnode of each
output connection's destination. These references are not devres
managed; the core drops them in coresight_release_platform_data(),
called by coresight_register() on failure and by coresight_unregister().
tpdm_probe() gets the platform data first and then returns directly
when the drvdata allocation, the MMIO mapping, the dataset setup or the
device name allocation fails, so these references are leaked. The nodes
come from the base DT, so this only leaves their refcounts unbalanced
unless they are dynamically allocated.

Get the platform data just before coresight_register(), after the other
steps that can fail, as the funnel, replicator and TMC drivers already
do. Nothing before that point uses it.

Fixes: b3c71626a933 ("Coresight: Add coresight TPDM source driver")
Signed-off-by: Yige Jiang <yigejiang86@gmail.com>
---
Based on linux-next next-20261109 (630059a5a632).
Compile-tested: arm64, GCC 14.2.0, W=1, CONFIG_CORESIGHT=m, CONFIG_CORESIGHT_TPDM=m, CONFIG_COMPILE_TEST=y; baseline and patched coresight-tpdm.o, no new warnings.
No hardware or runtime test was performed.
The paths need an allocation failure, an MMIO region already claimed, or a dataset setup failure in tpdm_probe() after the output connections were parsed.

 drivers/hwtracing/coresight/coresight-tpdm.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/drivers/hwtracing/coresight/coresight-tpdm.c b/drivers/hwtracing/coresight/coresight-tpdm.c
index 102b8b7edf87..07e19c1a94b6 100644
--- a/drivers/hwtracing/coresight/coresight-tpdm.c
+++ b/drivers/hwtracing/coresight/coresight-tpdm.c
@@ -1404,11 +1404,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_obj(dev, *drvdata);
 	if (!drvdata)
@@ -1444,6 +1439,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: 630059a5a632377b9b7b487baccde92b7ae1dd47

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

* Re: [PATCH] coresight: tpdm: Fix fwnode reference leak on probe failure
  2026-11-26 13:29 [PATCH] coresight: tpdm: Fix fwnode reference leak on probe failure Yige Jiang
@ 2026-11-26 14:18 ` James Clark
  2026-11-26 15:50   ` Yige Jiang
  0 siblings, 1 reply; 3+ messages in thread
From: James Clark @ 2026-11-26 14:18 UTC (permalink / raw)
  To: Yige Jiang, Leo Yan
  Cc: Mike Leach, coresight, linux-arm-kernel, linux-kernel,
	Suzuki K Poulose, Alexander Shishkin



On 26/11/2026 13:29, Yige Jiang wrote:
> coresight_get_platform_data() takes a reference on the fwnode of each
> output connection's destination. These references are not devres
> managed; the core drops them in coresight_release_platform_data(),


Don't _tnoc_probe(), tpda_probe() and smb_register_sink() have the same 
problem? We should fix all of these at the same time if you'd like to 
send a V2 with a patch for each driver.

It's quite fragile to have to call this in a certain order in every 
probe function though. Doesn't seem like it will survive further changes 
or refactoring for very long. Longer term, Leo is working on refactoring 
the locking and connections. Maybe can do something like make 
coresight_fixup_orphan_conns() re-read the firmware for unconnected 
ports and re-connect them, rather keeping these long lived references to 
fwnodes.

> called by coresight_register() on failure and by coresight_unregister().
> tpdm_probe() gets the platform data first and then returns directly
> when the drvdata allocation, the MMIO mapping, the dataset setup or the
> device name allocation fails, so these references are leaked. The nodes
> come from the base DT, so this only leaves their refcounts unbalanced
> unless they are dynamically allocated.
> 
> Get the platform data just before coresight_register(), after the other
> steps that can fail, as the funnel, replicator and TMC drivers already
> do. Nothing before that point uses it.
> 
> Fixes: b3c71626a933 ("Coresight: Add coresight TPDM source driver")
> Signed-off-by: Yige Jiang <yigejiang86@gmail.com>
> ---
> Based on linux-next next-20261109 (630059a5a632).
> Compile-tested: arm64, GCC 14.2.0, W=1, CONFIG_CORESIGHT=m, CONFIG_CORESIGHT_TPDM=m, CONFIG_COMPILE_TEST=y; baseline and patched coresight-tpdm.o, no new warnings.
> No hardware or runtime test was performed.
> The paths need an allocation failure, an MMIO region already claimed, or a dataset setup failure in tpdm_probe() after the output connections were parsed.
> 
>   drivers/hwtracing/coresight/coresight-tpdm.c | 11 ++++++-----
>   1 file changed, 6 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/hwtracing/coresight/coresight-tpdm.c b/drivers/hwtracing/coresight/coresight-tpdm.c
> index 102b8b7edf87..07e19c1a94b6 100644
> --- a/drivers/hwtracing/coresight/coresight-tpdm.c
> +++ b/drivers/hwtracing/coresight/coresight-tpdm.c
> @@ -1404,11 +1404,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_obj(dev, *drvdata);
>   	if (!drvdata)
> @@ -1444,6 +1439,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: 630059a5a632377b9b7b487baccde92b7ae1dd47

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

* Re: [PATCH] coresight: tpdm: Fix fwnode reference leak on probe failure
  2026-11-26 14:18 ` James Clark
@ 2026-11-26 15:50   ` Yige Jiang
  0 siblings, 0 replies; 3+ messages in thread
From: Yige Jiang @ 2026-11-26 15:50 UTC (permalink / raw)
  To: James Clark
  Cc: Leo Yan, Mike Leach, coresight, linux-arm-kernel, linux-kernel,
	Suzuki K Poulose, Alexander Shishkin

Hi,
Thanks for pointing these out. I checked all the callers of coresight_get_platform_data() and found the same issue in _tnoc_probe() and tpda_probe(). I'll send a v2 with one patch each for TPDM, TNOC and TPDA.

I agree that relying on the order of operations in each probe function is fragile. We're developing a static analysis tool that tracks refcount across driver callbacks to help catch imbalances like this one. And I'm glad to hear Leo is reworking the locking and connection handling in CoreSight.

Thanks,
Yige

> On Nov 26, 2026, at 10:18 PM, James Clark <james.clark@linaro.org> wrote:
> 
> 
> 
> On 26/11/2026 13:29, Yige Jiang wrote:
>> coresight_get_platform_data() takes a reference on the fwnode of each
>> output connection's destination. These references are not devres
>> managed; the core drops them in coresight_release_platform_data(),
> 
> 
> Don't _tnoc_probe(), tpda_probe() and smb_register_sink() have the same problem? We should fix all of these at the same time if you'd like to send a V2 with a patch for each driver.
> 
> It's quite fragile to have to call this in a certain order in every probe function though. Doesn't seem like it will survive further changes or refactoring for very long. Longer term, Leo is working on refactoring the locking and connections. Maybe can do something like make coresight_fixup_orphan_conns() re-read the firmware for unconnected ports and re-connect them, rather keeping these long lived references to fwnodes.
> 
>> called by coresight_register() on failure and by coresight_unregister().
>> tpdm_probe() gets the platform data first and then returns directly
>> when the drvdata allocation, the MMIO mapping, the dataset setup or the
>> device name allocation fails, so these references are leaked. The nodes
>> come from the base DT, so this only leaves their refcounts unbalanced
>> unless they are dynamically allocated.
>> Get the platform data just before coresight_register(), after the other
>> steps that can fail, as the funnel, replicator and TMC drivers already
>> do. Nothing before that point uses it.
>> Fixes: b3c71626a933 ("Coresight: Add coresight TPDM source driver")
>> Signed-off-by: Yige Jiang <yigejiang86@gmail.com>
>> ---
>> Based on linux-next next-20261109 (630059a5a632).
>> Compile-tested: arm64, GCC 14.2.0, W=1, CONFIG_CORESIGHT=m, CONFIG_CORESIGHT_TPDM=m, CONFIG_COMPILE_TEST=y; baseline and patched coresight-tpdm.o, no new warnings.
>> No hardware or runtime test was performed.
>> The paths need an allocation failure, an MMIO region already claimed, or a dataset setup failure in tpdm_probe() after the output connections were parsed.
>>  drivers/hwtracing/coresight/coresight-tpdm.c | 11 ++++++-----
>>  1 file changed, 6 insertions(+), 5 deletions(-)
>> diff --git a/drivers/hwtracing/coresight/coresight-tpdm.c b/drivers/hwtracing/coresight/coresight-tpdm.c
>> index 102b8b7edf87..07e19c1a94b6 100644
>> --- a/drivers/hwtracing/coresight/coresight-tpdm.c
>> +++ b/drivers/hwtracing/coresight/coresight-tpdm.c
>> @@ -1404,11 +1404,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_obj(dev, *drvdata);
>>   if (!drvdata)
>> @@ -1444,6 +1439,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: 630059a5a632377b9b7b487baccde92b7ae1dd47


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

end of thread, other threads:[~2026-11-26 15:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26 13:29 [PATCH] coresight: tpdm: Fix fwnode reference leak on probe failure Yige Jiang
2026-11-26 14:18 ` James Clark
2026-11-26 15:50   ` Yige Jiang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®