Skip to contentSkip to conversation

coresight: fix missing error code when trace ID is invalid

PATCH Superseded Jie Gan 3 participants 6 messages May 11 – May 12

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Jie Gan

author
#0 lore raw

PATCH v3 1/1 coresight: fix missing error code when trace ID is invalid

When coresight_path_assign_trace_id() cannot assign a valid trace ID, coresight_enable_sysfs() takes the err_path goto with ret still 0, returning success to the caller despite no trace session being started.

Fix this by changing coresight_path_assign_trace_id() to return int. Move the IS_VALID_CS_TRACE_ID() check inside the function so it returns -EINVAL on failure and 0 on success. Update coresight_enable_sysfs() to check the return value directly instead of inspecting path->trace_id after the call.

The other caller in coresight-etm-perf.c discards the return value and continues to check path->trace_id via IS_VALID_CS_TRACE_ID() directly. This is unaffected: on failure path->trace_id is no longer written, so it remains 0, which IS_VALID_CS_TRACE_ID() rejects the same as before.

Fixes d87d76d823d1 ("Coresight: Allocate trace ID after building the path")

---
Changes in v3:
- directly return the value for clear expression.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Refactor the coresight_path_assign_trace_id function.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 drivers/hwtracing/coresight/coresight-core.c  | 14 ++++++++++----
 drivers/hwtracing/coresight/coresight-priv.h  |  2 +-
 drivers/hwtracing/coresight/coresight-sysfs.c |  4 ++--
 3 files changed, 13 insertions(+), 7 deletions(-)
drivers/hwtracing/coresight/coresight-core.c +10 -4
index 46f247f73cf6..254db91a8ac9 100644
@@ -739,8 +739,8 @@ static int coresight_get_trace_id(struct coresight_device *csdev,
  * Call this after creating the path and before enabling it. This leaves
  * the trace ID set on the path, or it remains 0 if it couldn't be assigned.
  */
-void coresight_path_assign_trace_id(struct coresight_path *path,
-				    enum cs_mode mode)
+int coresight_path_assign_trace_id(struct coresight_path *path,
+				   enum cs_mode mode)
 {
 	struct coresight_device *sink = coresight_get_sink(path);
 	struct coresight_node *nd;
@@ -755,10 +755,16 @@ void coresight_path_assign_trace_id(struct coresight_path *path,
 		 * Non 0 is either success or fail.
 		 */
 		if (trace_id != 0) {
-			path->trace_id = trace_id;
-			return;
+			if (IS_VALID_CS_TRACE_ID(trace_id)) {
+				path->trace_id = trace_id;
+				return 0;
+			}
+
+			return -EINVAL;
 		}
 	}
+
+	return -EINVAL;
 }
 
 /**
drivers/hwtracing/coresight/coresight-priv.h +1 -1
index 1ea882dffd70..34c7e792adbd 100644
@@ -153,7 +153,7 @@ int coresight_make_links(struct coresight_device *orig,
 void coresight_remove_links(struct coresight_device *orig,
 			    struct coresight_connection *conn);
 u32 coresight_get_sink_id(struct coresight_device *csdev);
-void coresight_path_assign_trace_id(struct coresight_path *path,
+int coresight_path_assign_trace_id(struct coresight_path *path,
 				   enum cs_mode mode);
 
 #if IS_ENABLED(CONFIG_CORESIGHT_SOURCE_ETM3X)
drivers/hwtracing/coresight/coresight-sysfs.c +2 -2
index d2a6ed8bcc74..b6a870399e83 100644
@@ -211,8 +211,8 @@ int coresight_enable_sysfs(struct coresight_device *csdev)
 		goto out;
 	}
 
-	coresight_path_assign_trace_id(path, CS_MODE_SYSFS);
-	if (!IS_VALID_CS_TRACE_ID(path->trace_id))
+	ret = coresight_path_assign_trace_id(path, CS_MODE_SYSFS);
+	if (ret)
 		goto err_path;
 
 	ret = coresight_enable_path(path, CS_MODE_SYSFS);

---
base-commit: 17c7841d09ee7d33557fd075562d9289b6018c90
change-id: 20260508-fix-trace-id-error-dbfdd4d8f2d1

Best regards,

Signature · 1 line
Jie Gan <[email protected]>
#1 lore raw
Quoted 71 lines · Jie Gan

When coresight_path_assign_trace_id() cannot assign a valid trace ID, coresight_enable_sysfs() takes the err_path goto with ret still 0, returning success to the caller despite no trace session being started.

Fix this by changing coresight_path_assign_trace_id() to return int. Move the IS_VALID_CS_TRACE_ID() check inside the function so it returns -EINVAL on failure and 0 on success. Update coresight_enable_sysfs() to check the return value directly instead of inspecting path->trace_id after the call.

The other caller in coresight-etm-perf.c discards the return value and continues to check path->trace_id via IS_VALID_CS_TRACE_ID() directly. This is unaffected: on failure path->trace_id is no longer written, so it remains 0, which IS_VALID_CS_TRACE_ID() rejects the same as before.

Fixes d87d76d823d1 ("Coresight: Allocate trace ID after building the path")

---
Changes in v3:
- directly return the value for clear expression.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Refactor the coresight_path_assign_trace_id function.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 drivers/hwtracing/coresight/coresight-core.c  | 14 ++++++++++----
 drivers/hwtracing/coresight/coresight-priv.h  |  2 +-
 drivers/hwtracing/coresight/coresight-sysfs.c |  4 ++--
 3 files changed, 13 insertions(+), 7 deletions(-)
drivers/hwtracing/coresight/coresight-core.c +10 -4
index 46f247f73cf6..254db91a8ac9 100644
@@ -739,8 +739,8 @@ static int coresight_get_trace_id(struct coresight_device *csdev,
  * Call this after creating the path and before enabling it. This leaves
  * the trace ID set on the path, or it remains 0 if it couldn't be assigned.
  */
-void coresight_path_assign_trace_id(struct coresight_path *path,
-				    enum cs_mode mode)
+int coresight_path_assign_trace_id(struct coresight_path *path,
+				   enum cs_mode mode)
 {
 	struct coresight_device *sink = coresight_get_sink(path);
 	struct coresight_node *nd;
@@ -755,10 +755,16 @@ void coresight_path_assign_trace_id(struct coresight_path *path,
 		 * Non 0 is either success or fail.
 		 */
 		if (trace_id != 0) {
-			path->trace_id = trace_id;
-			return;
+			if (IS_VALID_CS_TRACE_ID(trace_id)) {
+				path->trace_id = trace_id;
+				return 0;
+			}
+
+			return -EINVAL;
 		}
 	}
+
+	return -EINVAL;
 }
 
 /**
drivers/hwtracing/coresight/coresight-priv.h +0 -0
index 1ea882dffd70..34c7e792adbd 100644

drivers/hwtracing/coresight/coresight-priv.h int coresight_make_links(struct coresight_device *orig, · line 157

Quoted 6 lines
drivers/hwtracing/coresight/coresight-priv.h +1 -1
@@ -153,7 +153,7 @@ int coresight_make_links(struct coresight_device *orig,
 void coresight_remove_links(struct coresight_device *orig,
 			    struct coresight_connection *conn);
 u32 coresight_get_sink_id(struct coresight_device *csdev);
-void coresight_path_assign_trace_id(struct coresight_path *path,
+int coresight_path_assign_trace_id(struct coresight_path *path,
 				   enum cs_mode mode);

Hi Jie,

Thanks for your patch,

I'm thinking will "__must_check" should be added so in the future the next caller won't accidently introduce this class of bug ?

Best regards,
Richard Cheng.

Quoted 27 lines
drivers/hwtracing/coresight/coresight-priv.h +0 -0
 #if IS_ENABLED(CONFIG_CORESIGHT_SOURCE_ETM3X)
drivers/hwtracing/coresight/coresight-sysfs.c +2 -3
index d2a6ed8bcc74..b6a870399e83 100644
@@ -211,8 +211,8 @@ int coresight_enable_sysfs(struct coresight_device *csdev)
 		goto out;
 	}
 
-	coresight_path_assign_trace_id(path, CS_MODE_SYSFS);
-	if (!IS_VALID_CS_TRACE_ID(path->trace_id))
+	ret = coresight_path_assign_trace_id(path, CS_MODE_SYSFS);
+	if (ret)
 		goto err_path;
 
 	ret = coresight_enable_path(path, CS_MODE_SYSFS);
 
---

base-commit: 17c7841d09ee7d33557fd075562d9289b6018c90
change-id: 20260508-fix-trace-id-error-dbfdd4d8f2d1

Best regards,

Signature · 1 line
Jie Gan <[email protected]>
author
#2 lore raw
Quoted 81 lines · Richard Cheng
Quoted 71 lines · Jie Gan

When coresight_path_assign_trace_id() cannot assign a valid trace ID, coresight_enable_sysfs() takes the err_path goto with ret still 0, returning success to the caller despite no trace session being started.

Fix this by changing coresight_path_assign_trace_id() to return int. Move the IS_VALID_CS_TRACE_ID() check inside the function so it returns -EINVAL on failure and 0 on success. Update coresight_enable_sysfs() to check the return value directly instead of inspecting path->trace_id after the call.

The other caller in coresight-etm-perf.c discards the return value and continues to check path->trace_id via IS_VALID_CS_TRACE_ID() directly. This is unaffected: on failure path->trace_id is no longer written, so it remains 0, which IS_VALID_CS_TRACE_ID() rejects the same as before.

Fixes d87d76d823d1 ("Coresight: Allocate trace ID after building the path")

---
Changes in v3:
- directly return the value for clear expression.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Refactor the coresight_path_assign_trace_id function.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
  drivers/hwtracing/coresight/coresight-core.c  | 14 ++++++++++----
  drivers/hwtracing/coresight/coresight-priv.h  |  2 +-
  drivers/hwtracing/coresight/coresight-sysfs.c |  4 ++--
  3 files changed, 13 insertions(+), 7 deletions(-)
drivers/hwtracing/coresight/coresight-core.c +10 -4
index 46f247f73cf6..254db91a8ac9 100644
@@ -739,8 +739,8 @@ static int coresight_get_trace_id(struct coresight_device *csdev,
   * Call this after creating the path and before enabling it. This leaves
   * the trace ID set on the path, or it remains 0 if it couldn't be assigned.
   */
-void coresight_path_assign_trace_id(struct coresight_path *path,
-				    enum cs_mode mode)
+int coresight_path_assign_trace_id(struct coresight_path *path,
+				   enum cs_mode mode)
  {
  	struct coresight_device *sink = coresight_get_sink(path);
  	struct coresight_node *nd;
@@ -755,10 +755,16 @@ void coresight_path_assign_trace_id(struct coresight_path *path,
  		 * Non 0 is either success or fail.
  		 */
  		if (trace_id != 0) {
-			path->trace_id = trace_id;
-			return;
+			if (IS_VALID_CS_TRACE_ID(trace_id)) {
+				path->trace_id = trace_id;
+				return 0;
+			}
+
+			return -EINVAL;
  		}
  	}
+
+	return -EINVAL;
  }
 
  /**
drivers/hwtracing/coresight/coresight-priv.h +0 -0
index 1ea882dffd70..34c7e792adbd 100644

drivers/hwtracing/coresight/coresight-priv.h int coresight_make_links(struct coresight_device *orig, · line 157

Quoted 6 lines
drivers/hwtracing/coresight/coresight-priv.h +1 -1
@@ -153,7 +153,7 @@ int coresight_make_links(struct coresight_device *orig,
  void coresight_remove_links(struct coresight_device *orig,
  			    struct coresight_connection *conn);
  u32 coresight_get_sink_id(struct coresight_device *csdev);
-void coresight_path_assign_trace_id(struct coresight_path *path,
+int coresight_path_assign_trace_id(struct coresight_path *path,
  				   enum cs_mode mode);

Hi Jie,

Quoted 5 lines

Thanks for your patch,

I'm thinking will "__must_check" should be added so in the future the next caller won't accidently introduce this class of bug ?

Hi Richard,

The return value has been ignored in perf mode. It will introduce noisy by adding __must_check. So I think its better without __must_check?

Thanks,
Jie

Quoted 30 lines

Best regards,
Richard Cheng.

Quoted 27 lines
drivers/hwtracing/coresight/coresight-priv.h +0 -0
  #if IS_ENABLED(CONFIG_CORESIGHT_SOURCE_ETM3X)
drivers/hwtracing/coresight/coresight-sysfs.c +2 -3
index d2a6ed8bcc74..b6a870399e83 100644
@@ -211,8 +211,8 @@ int coresight_enable_sysfs(struct coresight_device *csdev)
  		goto out;
  	}
 
-	coresight_path_assign_trace_id(path, CS_MODE_SYSFS);
-	if (!IS_VALID_CS_TRACE_ID(path->trace_id))
+	ret = coresight_path_assign_trace_id(path, CS_MODE_SYSFS);
+	if (ret)
  		goto err_path;
 
  	ret = coresight_enable_path(path, CS_MODE_SYSFS);
 
---

base-commit: 17c7841d09ee7d33557fd075562d9289b6018c90
change-id: 20260508-fix-trace-id-error-dbfdd4d8f2d1

Best regards,

Signature · 1 line
Jie Gan <[email protected]>
Reviewed-by
#3 lore raw
Quoted 89 lines · Jie Gan
Quoted 80 lines · Richard Cheng
Quoted 70 lines · Jie Gan

When coresight_path_assign_trace_id() cannot assign a valid trace ID, coresight_enable_sysfs() takes the err_path goto with ret still 0, returning success to the caller despite no trace session being started.

Fix this by changing coresight_path_assign_trace_id() to return int. Move the IS_VALID_CS_TRACE_ID() check inside the function so it returns -EINVAL on failure and 0 on success. Update coresight_enable_sysfs() to check the return value directly instead of inspecting path->trace_id after the call.

The other caller in coresight-etm-perf.c discards the return value and continues to check path->trace_id via IS_VALID_CS_TRACE_ID() directly. This is unaffected: on failure path->trace_id is no longer written, so it remains 0, which IS_VALID_CS_TRACE_ID() rejects the same as before.

Fixes d87d76d823d1 ("Coresight: Allocate trace ID after building the path")

---
Changes in v3:
- directly return the value for clear expression.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Refactor the coresight_path_assign_trace_id function.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
  drivers/hwtracing/coresight/coresight-core.c  | 14 ++++++++++----
  drivers/hwtracing/coresight/coresight-priv.h  |  2 +-
  drivers/hwtracing/coresight/coresight-sysfs.c |  4 ++--
  3 files changed, 13 insertions(+), 7 deletions(-)
drivers/hwtracing/coresight/coresight-core.c +10 -4
index 46f247f73cf6..254db91a8ac9 100644
@@ -739,8 +739,8 @@ static int coresight_get_trace_id(struct coresight_device *csdev,
   * Call this after creating the path and before enabling it. This leaves
   * the trace ID set on the path, or it remains 0 if it couldn't be assigned.
   */
-void coresight_path_assign_trace_id(struct coresight_path *path,
-				    enum cs_mode mode)
+int coresight_path_assign_trace_id(struct coresight_path *path,
+				   enum cs_mode mode)
  {
  	struct coresight_device *sink = coresight_get_sink(path);
  	struct coresight_node *nd;
@@ -755,10 +755,16 @@ void coresight_path_assign_trace_id(struct coresight_path *path,
  		 * Non 0 is either success or fail.
  		 */
  		if (trace_id != 0) {
-			path->trace_id = trace_id;
-			return;
+			if (IS_VALID_CS_TRACE_ID(trace_id)) {
+				path->trace_id = trace_id;
+				return 0;
+			}
+
+			return -EINVAL;
  		}
  	}
+
+	return -EINVAL;
  }
  /**
drivers/hwtracing/coresight/coresight-priv.h +0 -0
index 1ea882dffd70..34c7e792adbd 100644

drivers/hwtracing/coresight/coresight-priv.h int coresight_make_links(struct coresight_device *orig, · line 157

Quoted 6 lines
drivers/hwtracing/coresight/coresight-priv.h +1 -1
@@ -153,7 +153,7 @@ int coresight_make_links(struct coresight_device *orig,
  void coresight_remove_links(struct coresight_device *orig,
  			    struct coresight_connection *conn);
  u32 coresight_get_sink_id(struct coresight_device *csdev);
-void coresight_path_assign_trace_id(struct coresight_path *path,
+int coresight_path_assign_trace_id(struct coresight_path *path,
  				   enum cs_mode mode);

Hi Jie,

Quoted 5 lines

Thanks for your patch,

I'm thinking will "__must_check" should be added so in the future the next caller won't accidently introduce this class of bug ?

Quoted 8 lines

Hi Richard,

The return value has been ignored in perf mode. It will introduce noisy by adding __must_check. So I think its better without __must_check?

Thanks,
Jie

Hi Jie,

Ahh thanks for the head up, my intention was adding a compile-time check there to be safe, no need for __must_check here then.

If any new callers were to appear in the future we'll see how it goes. Thanks.

Best regards,
Richard Cheng.

Quoted 28 lines
Quoted 27 lines

Best regards,
Richard Cheng.

Quoted 24 lines
drivers/hwtracing/coresight/coresight-priv.h +0 -0
  #if IS_ENABLED(CONFIG_CORESIGHT_SOURCE_ETM3X)
drivers/hwtracing/coresight/coresight-sysfs.c +2 -3
index d2a6ed8bcc74..b6a870399e83 100644
@@ -211,8 +211,8 @@ int coresight_enable_sysfs(struct coresight_device *csdev)
  		goto out;
  	}
-	coresight_path_assign_trace_id(path, CS_MODE_SYSFS);
-	if (!IS_VALID_CS_TRACE_ID(path->trace_id))
+	ret = coresight_path_assign_trace_id(path, CS_MODE_SYSFS);
+	if (ret)
  		goto err_path;
  	ret = coresight_enable_path(path, CS_MODE_SYSFS);
 
---

base-commit: 17c7841d09ee7d33557fd075562d9289b6018c90
change-id: 20260508-fix-trace-id-error-dbfdd4d8f2d1

Best regards,

Signature · 1 line
Jie Gan <[email protected]>
reviewer
#4 lore raw
In reply to Jie Gan

On Mon, May 11, 2026 at 05:27:10PM +0800, Jie Gan wrote:

[...]

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/hwtracing/coresight/coresight-core.c +0 -0
@@ -755,10 +755,16 @@ void coresight_path_assign_trace_id(struct coresight_path *path,
  		 * Non 0 is either success or fail.
  		 */
  		if (trace_id != 0) {
Quoted 8 lines
Quoted 8 lines

drivers/hwtracing/coresight/coresight-core.c void coresight_path_assign_trace_id(struct coresight_path *path, · line 763

Quoted 8 lines
drivers/hwtracing/coresight/coresight-core.c +6 -2
-			path->trace_id = trace_id;
-			return;
+			if (IS_VALID_CS_TRACE_ID(trace_id)) {
+				path->trace_id = trace_id;
+				return 0;
+			}
+
+			return -EINVAL;

I'd advocate a bit early exit style, like:

/* 0 means the device has no ID assignment, so keep searching */ if (trace_id == 0)

      continue;

if (!IS_VALID_CS_TRACE_ID(trace_id))

      return -EINVAL;
  path->trace_id = trace_id;
  return 0;

Early exit can reduce indentation depth, and it handles simple cases first and then the complex logic. In some cases (maye not this case), we may benefit a bit from compiler optimization [1].

[1] https://xania.org/202512/18-partial-inlining

[...]

Quoted 2 lines

The return value has been ignored in perf mode. It will introduce noisy by adding __must_check. So I think its better without __must_check?

Wouldn't it need to update perf mode as well?

Regarding __must_check, I searched Documentation but didn't find guidance on when it should be used. I don't want to use this annotation randomly (some functions use it and some not), this will be hard for everyone to follow up.

IMO, it's fine not to use __must_check here. I would leave this to Suzuki and other maintainers if have different opinions.

Thanks,
Leo

author
#5 lore raw
Quoted 29 lines · Leo Yan

On Mon, May 11, 2026 at 05:27:10PM +0800, Jie Gan wrote:

[...]

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/hwtracing/coresight/coresight-core.c +0 -0
@@ -755,10 +755,16 @@ void coresight_path_assign_trace_id(struct coresight_path *path,
   		 * Non 0 is either success or fail.
   		 */
   		if (trace_id != 0) {
Quoted 8 lines
Quoted 8 lines

drivers/hwtracing/coresight/coresight-core.c void coresight_path_assign_trace_id(struct coresight_path *path, · line 763

Quoted 8 lines
drivers/hwtracing/coresight/coresight-core.c +6 -2
-			path->trace_id = trace_id;
-			return;
+			if (IS_VALID_CS_TRACE_ID(trace_id)) {
+				path->trace_id = trace_id;
+				return 0;
+			}
+
+			return -EINVAL;

I'd advocate a bit early exit style, like:

   /* 0 means the device has no ID assignment, so keep searching */
   if (trace_id == 0)
       continue;

   if (!IS_VALID_CS_TRACE_ID(trace_id))
       return -EINVAL;

   path->trace_id = trace_id;
   return 0;
Quoted 4 lines

Early exit can reduce indentation depth, and it handles simple cases first and then the complex logic. In some cases (maye not this case), we may benefit a bit from compiler optimization [1].

Thanks, that's a good suggestion and much simpler than my solution.

Quoted 8 lines

[1] https://xania.org/202512/18-partial-inlining

[...]

Quoted 2 lines

The return value has been ignored in perf mode. It will introduce noisy by adding __must_check. So I think its better without __must_check?

Wouldn't it need to update perf mode as well?

I will also update the perf mode for consistent usage.

Thanks,
Jie

Quoted 11 lines

Regarding __must_check, I searched Documentation but didn't find guidance on when it should be used. I don't want to use this annotation randomly (some functions use it and some not), this will be hard for everyone to follow up.

IMO, it's fine not to use __must_check here. I would leave this to Suzuki and other maintainers if have different opinions.

Thanks,
Leo