Skip to contentSkip to conversation

coresight: fix resource leaks on path build failure

PATCH In review Jie Gan 4 participants 11 messages May 13 – Nov 10

Subsystems (2)
lore
Share & tools
lore

Jie Gan

author
#0 lore raw

PATCH 1/1 coresight: fix resource leaks on path build failure

Two related leaks when _coresight_build_path() encounters an error after coresight_grab_device() has already incremented the pm_runtime, module, and device references for a node:

1. In _coresight_build_path(), if kzalloc_obj() for the path node fails after coresight_grab_device() succeeds, coresight_drop_device() was never called, permanently leaking all three references.

2. In coresight_build_path(), on failure the partial path was freed with kfree(path) instead of coresight_release_path(path). kfree() only frees the coresight_path struct itself; it does not iterate path_list to call coresight_drop_device() and kfree() for each coresight_node already added by deeper recursive calls, leaking both the pm_runtime, module, and device references and the node memory for every element on the partial path.

Fix both by adding coresight_drop_device() in the OOM unwind of _coresight_build_path(), and replacing kfree(path) with coresight_release_path(path) in coresight_build_path().

Fixes 32b0707a4182 ("coresight: Add try_get_module() in coresight_grab_device()")
Fixes b3e94405941e ("coresight: associating path with session rather than tracer")
---
 drivers/hwtracing/coresight/coresight-core.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/hwtracing/coresight/coresight-core.c +4 -2
index 46f247f73cf6..c1354ea8e11d 100644
@@ -825,8 +825,10 @@ static int _coresight_build_path(struct coresight_device *csdev,
 		return ret;
 
 	node = kzalloc_obj(struct coresight_node);
-	if (!node)
+	if (!node) {
+		coresight_drop_device(csdev);
 		return -ENOMEM;
+	}
 
 	node->csdev = csdev;
 	list_add(&node->link, &path->path_list);
@@ -851,7 +853,7 @@ struct coresight_path *coresight_build_path(struct coresight_device *source,
 
 	rc = _coresight_build_path(source, source, sink, path);
 	if (rc) {
-		kfree(path);
+		coresight_release_path(path);
 		return ERR_PTR(rc);
 	}
 

---
base-commit: e98d21c170b01ddef366f023bbfcf6b31509fa83
change-id: 20260513-fix-memory-leak-issue-034b4a45265e

Best regards,

Signature · 1 line
Jie Gan <[email protected]>
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 57 lines · Jie Gan

Two related leaks when _coresight_build_path() encounters an error after coresight_grab_device() has already incremented the pm_runtime, module, and device references for a node:

1. In _coresight_build_path(), if kzalloc_obj() for the path node fails after coresight_grab_device() succeeds, coresight_drop_device() was never called, permanently leaking all three references.

2. In coresight_build_path(), on failure the partial path was freed with kfree(path) instead of coresight_release_path(path). kfree() only frees the coresight_path struct itself; it does not iterate path_list to call coresight_drop_device() and kfree() for each coresight_node already added by deeper recursive calls, leaking both the pm_runtime, module, and device references and the node memory for every element on the partial path.

Fix both by adding coresight_drop_device() in the OOM unwind of _coresight_build_path(), and replacing kfree(path) with coresight_release_path(path) in coresight_build_path().

Fixes 32b0707a4182 ("coresight: Add try_get_module() in coresight_grab_device()")
Fixes b3e94405941e ("coresight: associating path with session rather than tracer")
---
  drivers/hwtracing/coresight/coresight-core.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/hwtracing/coresight/coresight-core.c +4 -3
index 46f247f73cf6..c1354ea8e11d 100644
@@ -825,8 +825,10 @@ static int _coresight_build_path(struct coresight_device *csdev,
  		return ret;
 
  	node = kzalloc_obj(struct coresight_node);
-	if (!node)
+	if (!node) {
+		coresight_drop_device(csdev);
  		return -ENOMEM;
+	}
 
  	node->csdev = csdev;
  	list_add(&node->link, &path->path_list);
@@ -851,7 +853,7 @@ struct coresight_path *coresight_build_path(struct coresight_device *source,
 
  	rc = _coresight_build_path(source, source, sink, path);
  	if (rc) {
-		kfree(path);
+		coresight_release_path(path);
  		return ERR_PTR(rc);
  	}
 
 
---

base-commit: e98d21c170b01ddef366f023bbfcf6b31509fa83
change-id: 20260513-fix-memory-leak-issue-034b4a45265e

Quoted 1 line

Best regards,

Looks good to me, but sashiko is complaining: https://sashiko.dev/#/patchset/20260513-fix-memory-leak-issue-v1-1-49822d7bc7d4%40oss.qualcomm.com

I'm trying to understand why it's saying that, but I think the scenario is that if there are multiple correct paths to a sink, when one path partially fails and a second path succeeds you could get a path_list with some garbage entries in it.

That's kind of a different and existing issue to the one you've fixed, and assumes that multiple paths to one sink are possible, which I'm not sure is supported?

It might be as easy as breaking the loop early for any return value other than -ENODEV, but I'll leave it to you to decide whether to do that here or not.

author
#2 lore raw
Quoted 58 lines · James Clark
Quoted 55 lines · Jie Gan

Two related leaks when _coresight_build_path() encounters an error after coresight_grab_device() has already incremented the pm_runtime, module, and device references for a node:

1. In _coresight_build_path(), if kzalloc_obj() for the path node fails after coresight_grab_device() succeeds, coresight_drop_device() was never called, permanently leaking all three references.

2. In coresight_build_path(), on failure the partial path was freed with kfree(path) instead of coresight_release_path(path).  kfree() only frees the coresight_path struct itself; it does not iterate path_list to call coresight_drop_device() and kfree() for each coresight_node already added by deeper recursive calls, leaking both the pm_runtime, module, and device references and the node memory for every element on the partial path.

Fix both by adding coresight_drop_device() in the OOM unwind of _coresight_build_path(), and replacing kfree(path) with coresight_release_path(path) in coresight_build_path().

Fixes 32b0707a4182 ("coresight: Add try_get_module() in coresight_grab_device()")
Fixes b3e94405941e ("coresight: associating path with session rather than tracer")

---
  drivers/hwtracing/coresight/coresight-core.c | 6 ++++--

  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/hwtracing/coresight/coresight-core.c +4 -3
index 46f247f73cf6..c1354ea8e11d 100644
@@ -825,8 +825,10 @@ static int _coresight_build_path(struct 
 coresight_device *csdev,
           return ret;
       node = kzalloc_obj(struct coresight_node);
-    if (!node)
+    if (!node) {
+        coresight_drop_device(csdev);
           return -ENOMEM;
+    }
       node->csdev = csdev;
       list_add(&node->link, &path->path_list);
@@ -851,7 +853,7 @@ struct coresight_path *coresight_build_path(struct 
 coresight_device *source,
       rc = _coresight_build_path(source, source, sink, path);
       if (rc) {
-        kfree(path);
+        coresight_release_path(path);
           return ERR_PTR(rc);
       }
 
---

base-commit: e98d21c170b01ddef366f023bbfcf6b31509fa83
change-id: 20260513-fix-memory-leak-issue-034b4a45265e

Quoted 5 lines
Quoted 1 line

Best regards,

Looks good to me, but sashiko is complaining: https://sashiko.dev/#/ patchset/20260513-fix-memory-leak-issue- v1-1-49822d7bc7d4%40oss.qualcomm.com

I'm trying to understand why it's saying that, but I think the scenario is that if there are multiple correct paths to a sink, when one path partially fails and a second path succeeds you could get a path_list with some garbage entries in it.

I think the coresight_release_path is added to address this situation.
We suffered the path partially failure, and we need release all nodes already added to the path.

Quoted 2 lines

That's kind of a different and existing issue to the one you've fixed, and assumes that multiple paths to one sink are possible, which I'm not sure is supported?

Each path is unique. We only deal with the issue path for balancing the reference count.

Thanks,
Jie

Quoted 5 lines

It might be as easy as breaking the loop early for any return value other than -ENODEV, but I'll leave it to you to decide whether to do that here or not.

reviewer
#3 lore raw
Quoted 61 lines · Jie Gan
Quoted 58 lines · James Clark
Quoted 55 lines · Jie Gan

Two related leaks when _coresight_build_path() encounters an error after coresight_grab_device() has already incremented the pm_runtime, module, and device references for a node:

1. In _coresight_build_path(), if kzalloc_obj() for the path node fails after coresight_grab_device() succeeds, coresight_drop_device() was never called, permanently leaking all three references.

2. In coresight_build_path(), on failure the partial path was freed with kfree(path) instead of coresight_release_path(path).  kfree() only frees the coresight_path struct itself; it does not iterate path_list to call coresight_drop_device() and kfree() for each coresight_node already added by deeper recursive calls, leaking both the pm_runtime, module, and device references and the node memory for every element on the partial path.

Fix both by adding coresight_drop_device() in the OOM unwind of _coresight_build_path(), and replacing kfree(path) with coresight_release_path(path) in coresight_build_path().

Fixes 32b0707a4182 ("coresight: Add try_get_module() in coresight_grab_device()")
Fixes b3e94405941e ("coresight: associating path with session rather than tracer")

---
  drivers/hwtracing/coresight/coresight-core.c | 6 ++++--

  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/hwtracing/coresight/coresight-core.c +4 -3
index 46f247f73cf6..c1354ea8e11d 100644
@@ -825,8 +825,10 @@ static int _coresight_build_path(struct 
 coresight_device *csdev,
           return ret;
       node = kzalloc_obj(struct coresight_node);
-    if (!node)
+    if (!node) {
+        coresight_drop_device(csdev);
           return -ENOMEM;
+    }
       node->csdev = csdev;
       list_add(&node->link, &path->path_list);
@@ -851,7 +853,7 @@ struct coresight_path 
 *coresight_build_path(struct coresight_device *source,
       rc = _coresight_build_path(source, source, sink, path);
       if (rc) {
-        kfree(path);
+        coresight_release_path(path);
           return ERR_PTR(rc);
       }
 
---

base-commit: e98d21c170b01ddef366f023bbfcf6b31509fa83
change-id: 20260513-fix-memory-leak-issue-034b4a45265e

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

Best regards,

Looks good to me, but sashiko is complaining: https://sashiko.dev/#/ patchset/20260513-fix-memory-leak-issue- v1-1-49822d7bc7d4%40oss.qualcomm.com

I'm trying to understand why it's saying that, but I think the scenario is that if there are multiple correct paths to a sink, when one path partially fails and a second path succeeds you could get a path_list with some garbage entries in it.

I think the coresight_release_path is added to address this situation.
We suffered the path partially failure, and we need release all nodes already added to the path.

It wouldn't call coresight_release_path() in this case though. If one path ends up building to success but a parallel path partially failed then _coresight_build_path() still returns success. During the search it would have still added the nodes from the partially failed path to the path_list. This is only an issue if there are multiple correct paths.

Quoted 8 lines
Quoted 2 lines

That's kind of a different and existing issue to the one you've fixed, and assumes that multiple paths to one sink are possible, which I'm not sure is supported?

Each path is unique. We only deal with the issue path for balancing the reference count.

Thanks,
Jie

I'm not exactly sure what you mean by unique, but the same source and sink could potentially be connected through two different sets of links.

Quoted 6 lines
Quoted 5 lines

It might be as easy as breaking the loop early for any return value other than -ENODEV, but I'll leave it to you to decide whether to do that here or not.

reviewer
#4 lore raw
Quoted 115 lines

-----Original Message-----
From: James Clark <[email protected]>
Sent: Wednesday, May 20, 2026 9:38 AM
To: Jie Gan <[email protected]>
Cc: [email protected]; [email protected]; linux- [email protected]; Suzuki Poulose <[email protected]>; Mike Leach <[email protected]>; Leo Yan <[email protected]>; Alexander Shishkin <[email protected]>; Mathieu Poirier <[email protected]>; Tingwei Zhang <[email protected]>; Greg Kroah-Hartman <[email protected]>
Subject: Re: [PATCH] coresight: fix resource leaks on path build failure

Quoted 8 lines · Jie Gan
Quoted 5 lines · James Clark
Quoted 2 lines · Jie Gan

Two related leaks when _coresight_build_path() encounters an error after coresight_grab_device() has already incremented the pm_runtime,

module,

Quoted 64 lines
Quoted 64 lines
Quoted 58 lines

and device references for a node:

1. In _coresight_build_path(), if kzalloc_obj() for the path node fails after coresight_grab_device() succeeds, coresight_drop_device() was never called, permanently leaking all three references.

2. In coresight_build_path(), on failure the partial path was freed with kfree(path) instead of coresight_release_path(path).  kfree() only frees the coresight_path struct itself; it does not iterate path_list
    to call coresight_drop_device() and kfree() for each coresight_node already added by deeper recursive calls, leaking both the pm_runtime,
    module, and device references and the node memory for every element on the partial path.

Fix both by adding coresight_drop_device() in the OOM unwind of _coresight_build_path(), and replacing kfree(path) with coresight_release_path(path) in coresight_build_path().

Fixes 32b0707a4182 ("coresight: Add try_get_module() in

coresight_grab_device()")

Fixes b3e94405941e ("coresight: associating path with session rather

than tracer")

---
  drivers/hwtracing/coresight/coresight-core.c | 6 ++++--

  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/ renamed from drivers/hwtracing/coresight/coresight-core.c +0 -0

hwtracing/coresight/coresight-core.c
index 46f247f73cf6..c1354ea8e11d 100644

drivers/hwtracing/coresight/coresight-core.c +4 -3
@@ -825,8 +825,10 @@ static int _coresight_build_path(struct
 coresight_device *csdev,
           return ret;
       node = kzalloc_obj(struct coresight_node);
-    if (!node)
+    if (!node) {
+        coresight_drop_device(csdev);
           return -ENOMEM;
+    }
       node->csdev = csdev;
       list_add(&node->link, &path->path_list);
@@ -851,7 +853,7 @@ struct coresight_path
 *coresight_build_path(struct coresight_device *source,
       rc = _coresight_build_path(source, source, sink, path);
       if (rc) {
-        kfree(path);
+        coresight_release_path(path);
           return ERR_PTR(rc);
       }
 
---

base-commit: e98d21c170b01ddef366f023bbfcf6b31509fa83
change-id: 20260513-fix-memory-leak-issue-034b4a45265e

Quoted 1 line

Best regards,

Looks good to me, but sashiko is complaining: https://sashiko.dev/#/
patchset/20260513-fix-memory-leak-issue- v1-1-49822d7bc7d4%40oss.qualcomm.com

Quoted 9 lines
Quoted 4 lines

I'm trying to understand why it's saying that, but I think the scenario is that if there are multiple correct paths to a sink, when one path partially fails and a second path succeeds you could get a path_list with some garbage entries in it.

I think the coresight_release_path is added to address this situation. We suffered the path partially failure, and we need release all nodes already added to the path.

It wouldn't call coresight_release_path() in this case though. If one path ends up building to success but a parallel path partially failed then _coresight_build_path() still returns success. During the search it would have still added the nodes from the partially failed path to the path_list. This is only an issue if there are multiple correct paths.

Quoted 8 lines
Quoted 4 lines

That's kind of a different and existing issue to the one you've fixed, and assumes that multiple paths to one sink are possible, which I'm not sure is supported?

Each path is unique. We only deal with the issue path for balancing the reference count.

Quoted 7 lines
Quoted 3 lines

Thanks,
Jie

I'm not exactly sure what you mean by unique, but the same source and sink could potentially be connected through two different sets of links.

Multiple paths between a source and sink are not permitted under the CoreSight spec.

If such a system was to be built - then a fix would need to be in the declaration of connections - e.g. miss one path out in the device tree for example. Not up to the Coresight drivers to handle out of specification hardware.

Mike

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

It might be as easy as breaking the loop early for any return value other than -ENODEV, but I'll leave it to you to decide whether to do that here or not.

author
#5 lore raw
Quoted 101 lines · Mike Leach
Quoted 101 lines

-----Original Message-----
From: James Clark <[email protected]>
Sent: Wednesday, May 20, 2026 9:38 AM
To: Jie Gan <[email protected]>
Cc: [email protected]; [email protected]; linux- [email protected]; Suzuki Poulose <[email protected]>; Mike Leach <[email protected]>; Leo Yan <[email protected]>; Alexander Shishkin <[email protected]>; Mathieu Poirier <[email protected]>; Tingwei Zhang <[email protected]>; Greg Kroah-Hartman <[email protected]>
Subject: Re: [PATCH] coresight: fix resource leaks on path build failure

Quoted 8 lines · Jie Gan
Quoted 5 lines · James Clark
Quoted 2 lines · Jie Gan

Two related leaks when _coresight_build_path() encounters an error after coresight_grab_device() has already incremented the pm_runtime,

module,

Quoted 73 lines
Quoted 64 lines
Quoted 58 lines

and device references for a node:

1. In _coresight_build_path(), if kzalloc_obj() for the path node fails after coresight_grab_device() succeeds, coresight_drop_device() was never called, permanently leaking all three references.

2. In coresight_build_path(), on failure the partial path was freed with kfree(path) instead of coresight_release_path(path).  kfree() only frees the coresight_path struct itself; it does not iterate
path_list
    to call coresight_drop_device() and kfree() for each coresight_node already added by deeper recursive calls, leaking both the
pm_runtime,
    module, and device references and the node memory for every element on the partial path.

Fix both by adding coresight_drop_device() in the OOM unwind of _coresight_build_path(), and replacing kfree(path) with coresight_release_path(path) in coresight_build_path().

Fixes 32b0707a4182 ("coresight: Add try_get_module() in

coresight_grab_device()")

Fixes b3e94405941e ("coresight: associating path with session rather

than tracer")

---
   drivers/hwtracing/coresight/coresight-core.c | 6 ++++--
   1 file changed, 4 insertions(+), 2 deletions(-)
drivers/ renamed from drivers/hwtracing/coresight/coresight-core.c +0 -0

hwtracing/coresight/coresight-core.c
index 46f247f73cf6..c1354ea8e11d 100644

drivers/hwtracing/coresight/coresight-core.c +4 -3
@@ -825,8 +825,10 @@ static int _coresight_build_path(struct
 coresight_device *csdev,
           return ret;
       node = kzalloc_obj(struct coresight_node);
-    if (!node)
+    if (!node) {
+        coresight_drop_device(csdev);
           return -ENOMEM;
+    }
       node->csdev = csdev;
       list_add(&node->link, &path->path_list);
@@ -851,7 +853,7 @@ struct coresight_path
 *coresight_build_path(struct coresight_device *source,
       rc = _coresight_build_path(source, source, sink, path);
       if (rc) {
-        kfree(path);
+        coresight_release_path(path);
           return ERR_PTR(rc);
       }
 
---

base-commit: e98d21c170b01ddef366f023bbfcf6b31509fa83
change-id: 20260513-fix-memory-leak-issue-034b4a45265e

Quoted 1 line

Best regards,

Looks good to me, but sashiko is complaining: https://sashiko.dev/#/
patchset/20260513-fix-memory-leak-issue- v1-1-49822d7bc7d4%40oss.qualcomm.com

Quoted 4 lines

I'm trying to understand why it's saying that, but I think the scenario is that if there are multiple correct paths to a sink, when one path partially fails and a second path succeeds you could get a path_list with some garbage entries in it.

I think the coresight_release_path is added to address this situation. We suffered the path partially failure, and we need release all nodes already added to the path.

Quoted 6 lines
Quoted 6 lines

It wouldn't call coresight_release_path() in this case though. If one path ends up building to success but a parallel path partially failed then _coresight_build_path() still returns success. During the search it would have still added the nodes from the partially failed path to the path_list. This is only an issue if there are multiple correct paths.

The point here is there are multiple routes from the same source device to the same sink device, am right?

I have no experience on this scenario. So with the scenario, the build_path may succeeded in one route and failed in another route, but finally, the _coresight_build_path still returns success, is that correct?

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
Quoted 4 lines

That's kind of a different and existing issue to the one you've fixed, and assumes that multiple paths to one sink are possible, which I'm not sure is supported?

Each path is unique. We only deal with the issue path for balancing the reference count.

Thanks,
Jie

Quoted 6 lines
Quoted 3 lines

I'm not exactly sure what you mean by unique, but the same source and sink could potentially be connected through two different sets of links.

Multiple paths between a source and sink are not permitted under the CoreSight spec.

As Mike mentioned, my understanding is that a source device is only allowed to be added to one valid path—this is what I mean by “unique.”

Thanks,
Jie

Quoted 14 lines

If such a system was to be built - then a fix would need to be in the declaration of connections - e.g. miss one path out in the device tree for example. Not up to the Coresight drivers to handle out of specification hardware.

Mike

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

It might be as easy as breaking the loop early for any return value other than -ENODEV, but I'll leave it to you to decide whether to do that here or not.

reviewer
#6 lore raw
Quoted 131 lines · Jie Gan
Quoted 101 lines · Mike Leach
Quoted 101 lines

-----Original Message-----
From: James Clark <[email protected]>
Sent: Wednesday, May 20, 2026 9:38 AM
To: Jie Gan <[email protected]>
Cc: [email protected]; [email protected]; linux- [email protected]; Suzuki Poulose <[email protected]>; Mike Leach <[email protected]>; Leo Yan <[email protected]>; Alexander Shishkin <[email protected]>; Mathieu Poirier <[email protected]>; Tingwei Zhang <[email protected]>; Greg Kroah-Hartman <[email protected]>
Subject: Re: [PATCH] coresight: fix resource leaks on path build failure

Quoted 8 lines · Jie Gan
Quoted 5 lines · James Clark
Quoted 2 lines · Jie Gan

Two related leaks when _coresight_build_path() encounters an error after coresight_grab_device() has already incremented the pm_runtime,

module,

Quoted 73 lines
Quoted 64 lines
Quoted 58 lines

and device references for a node:

1. In _coresight_build_path(), if kzalloc_obj() for the path node fails after coresight_grab_device() succeeds, coresight_drop_device() was never called, permanently leaking all three references.

2. In coresight_build_path(), on failure the partial path was freed with kfree(path) instead of coresight_release_path(path).  kfree() only frees the coresight_path struct itself; it does not iterate path_list
     to call coresight_drop_device() and kfree() for each coresight_node already added by deeper recursive calls, leaking both the pm_runtime,
     module, and device references and the node memory for every element on the partial path.

Fix both by adding coresight_drop_device() in the OOM unwind of _coresight_build_path(), and replacing kfree(path) with coresight_release_path(path) in coresight_build_path().

Fixes 32b0707a4182 ("coresight: Add try_get_module() in

coresight_grab_device()")

Fixes b3e94405941e ("coresight: associating path with session rather

than tracer")

---
   drivers/hwtracing/coresight/coresight-core.c | 6 ++++--

   1 file changed, 4 insertions(+), 2 deletions(-)
drivers/ renamed from drivers/hwtracing/coresight/coresight-core.c +0 -0

hwtracing/coresight/coresight-core.c
index 46f247f73cf6..c1354ea8e11d 100644

drivers/hwtracing/coresight/coresight-core.c +4 -3
@@ -825,8 +825,10 @@ static int _coresight_build_path(struct
 coresight_device *csdev,
            return ret;
        node = kzalloc_obj(struct coresight_node);
-    if (!node)
+    if (!node) {
+        coresight_drop_device(csdev);
            return -ENOMEM;
+    }
        node->csdev = csdev;
        list_add(&node->link, &path->path_list);
@@ -851,7 +853,7 @@ struct coresight_path
 *coresight_build_path(struct coresight_device *source,
        rc = _coresight_build_path(source, source, sink, path);
        if (rc) {
-        kfree(path);
+        coresight_release_path(path);
            return ERR_PTR(rc);
        }
 
---

base-commit: e98d21c170b01ddef366f023bbfcf6b31509fa83
change-id: 20260513-fix-memory-leak-issue-034b4a45265e

Quoted 1 line

Best regards,

Looks good to me, but sashiko is complaining: https://sashiko.dev/#/
patchset/20260513-fix-memory-leak-issue- v1-1-49822d7bc7d4%40oss.qualcomm.com

Quoted 4 lines

I'm trying to understand why it's saying that, but I think the scenario is that if there are multiple correct paths to a sink, when one path partially fails and a second path succeeds you could get a path_list with some garbage entries in it.

I think the coresight_release_path is added to address this situation. We suffered the path partially failure, and we need release all nodes already added to the path.

Quoted 6 lines
Quoted 6 lines

It wouldn't call coresight_release_path() in this case though. If one path ends up building to success but a parallel path partially failed then _coresight_build_path() still returns success. During the search it would have still added the nodes from the partially failed path to the path_list. This is only an issue if there are multiple correct paths.

The point here is there are multiple routes from the same source device to the same sink device, am right?

I have no experience on this scenario. So with the scenario, the build_path may succeeded in one route and failed in another route, but finally, the _coresight_build_path still returns success, is that correct?

Quoted 16 lines
Quoted 15 lines
Quoted 11 lines
Quoted 4 lines

That's kind of a different and existing issue to the one you've fixed, and assumes that multiple paths to one sink are possible, which I'm not sure is supported?

Each path is unique. We only deal with the issue path for balancing the reference count.

Thanks,
Jie

I'm not exactly sure what you mean by unique, but the same source and sink could potentially be connected through two different sets of links.

Quoted 8 lines
Quoted 2 lines

Multiple paths between a source and sink are not permitted under the CoreSight spec.

As Mike mentioned, my understanding is that a source device is only allowed to be added to one valid path—this is what I mean by “unique.”

Thanks,
Jie

That's ok then we can ignore this for this patch. But it would be good to enforce that in _coresight_build_path() with some kind of assert. Or at least add a comment to appease the AI reviewers.

Quoted 15 lines
Quoted 14 lines

If such a system was to be built - then a fix would need to be in the declaration of connections - e.g. miss one path out in the device tree for example. Not up to the Coresight drivers to handle out of specification hardware.

Mike

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

It might be as easy as breaking the loop early for any return value other than -ENODEV, but I'll leave it to you to decide whether to do that here or not.

author
#7 lore raw
In reply to Jie Gan
Quoted 16 lines · Jie Gan

Two related leaks when _coresight_build_path() encounters an error after coresight_grab_device() has already incremented the pm_runtime, module, and device references for a node:

1. In _coresight_build_path(), if kzalloc_obj() for the path node fails after coresight_grab_device() succeeds, coresight_drop_device() was never called, permanently leaking all three references.

2. In coresight_build_path(), on failure the partial path was freed with kfree(path) instead of coresight_release_path(path). kfree() only frees the coresight_path struct itself; it does not iterate path_list to call coresight_drop_device() and kfree() for each coresight_node already added by deeper recursive calls, leaking both the pm_runtime, module, and device references and the node memory for every element on the partial path.

Quoted 7 lines

Fix both by adding coresight_drop_device() in the OOM unwind of _coresight_build_path(), and replacing kfree(path) with coresight_release_path(path) in coresight_build_path().

Fixes 32b0707a4182 ("coresight: Add try_get_module() in coresight_grab_device()")
Fixes b3e94405941e ("coresight: associating path with session rather than tracer")

Gentle ping.

Quoted 35 lines
---
  drivers/hwtracing/coresight/coresight-core.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/hwtracing/coresight/coresight-core.c +4 -3
index 46f247f73cf6..c1354ea8e11d 100644
@@ -825,8 +825,10 @@ static int _coresight_build_path(struct coresight_device *csdev,
  		return ret;
 
  	node = kzalloc_obj(struct coresight_node);
-	if (!node)
+	if (!node) {
+		coresight_drop_device(csdev);
  		return -ENOMEM;
+	}
 
  	node->csdev = csdev;
  	list_add(&node->link, &path->path_list);
@@ -851,7 +853,7 @@ struct coresight_path *coresight_build_path(struct coresight_device *source,
 
  	rc = _coresight_build_path(source, source, sink, path);
  	if (rc) {
-		kfree(path);
+		coresight_release_path(path);
  		return ERR_PTR(rc);
  	}
 
 
---

base-commit: e98d21c170b01ddef366f023bbfcf6b31509fa83
change-id: 20260513-fix-memory-leak-issue-034b4a45265e

Best regards,

reviewer
#8 lore raw
Quoted 46 lines · Jie Gan
Quoted 16 lines · Jie Gan

Two related leaks when _coresight_build_path() encounters an error after coresight_grab_device() has already incremented the pm_runtime, module, and device references for a node:

1. In _coresight_build_path(), if kzalloc_obj() for the path node fails after coresight_grab_device() succeeds, coresight_drop_device() was never called, permanently leaking all three references.

2. In coresight_build_path(), on failure the partial path was freed with kfree(path) instead of coresight_release_path(path).  kfree() only frees the coresight_path struct itself; it does not iterate path_list to call coresight_drop_device() and kfree() for each coresight_node already added by deeper recursive calls, leaking both the pm_runtime, module, and device references and the node memory for every element on the partial path.

Quoted 7 lines

Fix both by adding coresight_drop_device() in the OOM unwind of _coresight_build_path(), and replacing kfree(path) with coresight_release_path(path) in coresight_build_path().

Fixes 32b0707a4182 ("coresight: Add try_get_module() in coresight_grab_device()")
Fixes b3e94405941e ("coresight: associating path with session rather than tracer")

Gentle ping.

Quoted 17 lines

---
  drivers/hwtracing/coresight/coresight-core.c | 6 ++++--

  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/hwtracing/coresight/coresight-core.c +3 -1
index 46f247f73cf6..c1354ea8e11d 100644
@@ -825,8 +825,10 @@ static int _coresight_build_path(struct 
 coresight_device *csdev,
           return ret;
       node = kzalloc_obj(struct coresight_node);
-    if (!node)
+    if (!node) {
+        coresight_drop_device(csdev);
           return -ENOMEM;
+    }
Quoted 8 lines

drivers/hwtracing/coresight/coresight-core.c struct coresight_path *coresight_build_path(struct · line 856

Quoted 8 lines
drivers/hwtracing/coresight/coresight-core.c +1 -1
       node->csdev = csdev;
       list_add(&node->link, &path->path_list);
@@ -851,7 +853,7 @@ struct coresight_path *coresight_build_path(struct 
 coresight_device *source,
       rc = _coresight_build_path(source, source, sink, path);
       if (rc) {
-        kfree(path);
+        coresight_release_path(path);

@Suzuki, there's a conflict here. This will need to be coresight_release_path_locked() if we also take "coresight: etm-perf: Serialize AUX topology access" from my Sashiko fixes.

Quoted 9 lines
Quoted 8 lines
drivers/hwtracing/coresight/coresight-core.c +0 -1
           return ERR_PTR(rc);
       }
 
---

base-commit: e98d21c170b01ddef366f023bbfcf6b31509fa83
change-id: 20260513-fix-memory-leak-issue-034b4a45265e

Best regards,

maintainer
#9 lore raw
Quoted 52 lines · James Clark
Quoted 49 lines · Jie Gan
Quoted 16 lines · Jie Gan

Two related leaks when _coresight_build_path() encounters an error after coresight_grab_device() has already incremented the pm_runtime, module, and device references for a node:

1. In _coresight_build_path(), if kzalloc_obj() for the path node fails after coresight_grab_device() succeeds, coresight_drop_device() was never called, permanently leaking all three references.

2. In coresight_build_path(), on failure the partial path was freed with kfree(path) instead of coresight_release_path(path).  kfree() only frees the coresight_path struct itself; it does not iterate path_list to call coresight_drop_device() and kfree() for each coresight_node already added by deeper recursive calls, leaking both the pm_runtime, module, and device references and the node memory for every element on the partial path.

Quoted 7 lines

Fix both by adding coresight_drop_device() in the OOM unwind of _coresight_build_path(), and replacing kfree(path) with coresight_release_path(path) in coresight_build_path().

Fixes 32b0707a4182 ("coresight: Add try_get_module() in coresight_grab_device()")
Fixes b3e94405941e ("coresight: associating path with session rather than tracer")

Gentle ping.

Quoted 20 lines

---
  drivers/hwtracing/coresight/coresight-core.c | 6 ++++--

  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/hwtracing/coresight/coresight-core.c +3 -1
index 46f247f73cf6..c1354ea8e11d 100644
@@ -825,8 +825,10 @@ static int _coresight_build_path(struct 
 coresight_device *csdev,
           return ret;
       node = kzalloc_obj(struct coresight_node);
-    if (!node)
+    if (!node) {
+        coresight_drop_device(csdev);
           return -ENOMEM;
+    }
       node->csdev = csdev;
       list_add(&node->link, &path->path_list);
@@ -851,7 +853,7 @@ struct coresight_path 
Quoted 8 lines
Quoted 5 lines

drivers/hwtracing/coresight/coresight-core.c struct coresight_path *coresight_build_path(struct coresight_device *source, · line 856

Quoted 5 lines
drivers/hwtracing/coresight/coresight-core.c +1 -1
 *coresight_build_path(struct coresight_device *source,
       rc = _coresight_build_path(source, source, sink, path);
       if (rc) {
-        kfree(path);
+        coresight_release_path(path);

@Suzuki, there's a conflict here. This will need to be
coresight_release_path_locked() if we also take "coresight: etm-perf: Serialize AUX topology access" from my Sashiko fixes.

The changes needed by the patch are already what your patches does. Apologies Jie Gan, I have merged Jamess series which solves everything. So, we can ignore this one.

Cheers
Suzuki

author
#10 lore raw
Quoted 62 lines · Suzuki K Poulose
Quoted 52 lines · James Clark
Quoted 49 lines · Jie Gan
Quoted 16 lines · Jie Gan

Two related leaks when _coresight_build_path() encounters an error after coresight_grab_device() has already incremented the pm_runtime, module, and device references for a node:

1. In _coresight_build_path(), if kzalloc_obj() for the path node fails after coresight_grab_device() succeeds, coresight_drop_device() was never called, permanently leaking all three references.

2. In coresight_build_path(), on failure the partial path was freed with kfree(path) instead of coresight_release_path(path).  kfree() only frees the coresight_path struct itself; it does not iterate path_list to call coresight_drop_device() and kfree() for each coresight_node already added by deeper recursive calls, leaking both the pm_runtime, module, and device references and the node memory for every element on the partial path.

Quoted 7 lines

Fix both by adding coresight_drop_device() in the OOM unwind of _coresight_build_path(), and replacing kfree(path) with coresight_release_path(path) in coresight_build_path().

Fixes 32b0707a4182 ("coresight: Add try_get_module() in coresight_grab_device()")
Fixes b3e94405941e ("coresight: associating path with session rather than tracer")

Gentle ping.

Quoted 20 lines

---
  drivers/hwtracing/coresight/coresight-core.c | 6 ++++--

  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/hwtracing/coresight/coresight-core.c +3 -1
index 46f247f73cf6..c1354ea8e11d 100644
@@ -825,8 +825,10 @@ static int _coresight_build_path(struct 
 coresight_device *csdev,
           return ret;
       node = kzalloc_obj(struct coresight_node);
-    if (!node)
+    if (!node) {
+        coresight_drop_device(csdev);
           return -ENOMEM;
+    }
       node->csdev = csdev;
       list_add(&node->link, &path->path_list);
@@ -851,7 +853,7 @@ struct coresight_path 
Quoted 8 lines
Quoted 5 lines

drivers/hwtracing/coresight/coresight-core.c struct coresight_path *coresight_build_path(struct coresight_device *source, · line 856

Quoted 5 lines
drivers/hwtracing/coresight/coresight-core.c +1 -1
 *coresight_build_path(struct coresight_device *source,
       rc = _coresight_build_path(source, source, sink, path);
       if (rc) {
-        kfree(path);
+        coresight_release_path(path);

@Suzuki, there's a conflict here. This will need to be
coresight_release_path_locked() if we also take "coresight: etm-perf: Serialize AUX topology access" from my Sashiko fixes.

Quoted 3 lines

The changes needed by the patch are already what your patches does. Apologies Jie Gan, I have merged Jamess series which solves everything. So, we can ignore this one.

Got it. Glad to hear the issue to be fixed.

Thanks,
Jie

Quoted 4 lines

Cheers
Suzuki