Skip to contentSkip to conversation

coresight: etm3x: Fix cntr_val_show() to match cntr_val_store() behavior

PATCH Superseded Kuan-Wei Chiu 3 participants 4 messages Dec 1, 2025

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 coresight: etm3x: Fix cntr_val_show() to match cntr_val_store() behavior

The cntr_val_show() function was intended to print the values of all counters using a loop. However, due to a buffer overwrite issue with sprintf(), it effectively only displayed the value of the last counter.

The companion function, cntr_val_store(), allows users to modify a specific counter selected by 'cntr_idx'. To maintain consistency between read and write operations and to align with the ETM4x driver behavior, modify cntr_val_show() to report only the value of the currently selected counter.

This change removes the loop and the "counter %d:" prefix, printing only the hexadecimal value. It also adopts sysfs_emit() for standard sysfs output formatting.

Fixes a939fc5a71ad ("coresight-etm: add CoreSight ETM/PTM driver")

---
Build test only.

Changes in v2:
- Redesigned the fix to match cntr_val_store() logic (display only the currently selected counter) instead of attempting to list all counters.
- Removed the loop and the "counter %d:" prefix.
- Switched from sprintf() to sysfs_emit().
- Refactored control flow to use a single return point.

v1: https://lore.kernel.org/lkml/[email protected]/

 .../hwtracing/coresight/coresight-etm3x-sysfs.c   | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)
drivers/hwtracing/coresight/coresight-etm3x-sysfs.c +4 -11
index 762109307b86..a6a650331196 100644
@@ -717,26 +717,19 @@ static DEVICE_ATTR_RW(cntr_rld_event);
 static ssize_t cntr_val_show(struct device *dev,
 			     struct device_attribute *attr, char *buf)
 {
-	int i, ret = 0;
 	u32 val;
 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 	struct etm_config *config = &drvdata->config;
 
 	if (!coresight_get_mode(drvdata->csdev)) {
 		spin_lock(&drvdata->spinlock);
-		for (i = 0; i < drvdata->nr_cntr; i++)
-			ret += sprintf(buf, "counter %d: %x\n",
-				       i, config->cntr_val[i]);
+		val = config->cntr_val[config->cntr_idx];
 		spin_unlock(&drvdata->spinlock);
-		return ret;
-	}
-
-	for (i = 0; i < drvdata->nr_cntr; i++) {
-		val = etm_readl(drvdata, ETMCNTVRn(i));
-		ret += sprintf(buf, "counter %d: %x\n", i, val);
+	} else {
+		val = etm_readl(drvdata, ETMCNTVRn(config->cntr_idx));
 	}
 
-	return ret;
+	return sysfs_emit(buf, "%x\n", val);
 }
 
 static ssize_t cntr_val_store(struct device *dev,
Signature · 1 line
2.52.0.487.g5c8c507ade-goog
#1 lore raw
Quoted 27 lines · Kuan-Wei Chiu

The cntr_val_show() function was intended to print the values of all counters using a loop. However, due to a buffer overwrite issue with sprintf(), it effectively only displayed the value of the last counter.

The companion function, cntr_val_store(), allows users to modify a specific counter selected by 'cntr_idx'. To maintain consistency between read and write operations and to align with the ETM4x driver behavior, modify cntr_val_show() to report only the value of the currently selected counter.

This change removes the loop and the "counter %d:" prefix, printing only the hexadecimal value. It also adopts sysfs_emit() for standard sysfs output formatting.

Fixes a939fc5a71ad ("coresight-etm: add CoreSight ETM/PTM driver")

---
Build test only.

Changes in v2:
- Redesigned the fix to match cntr_val_store() logic (display only the currently selected counter) instead of attempting to list all counters.
- Removed the loop and the "counter %d:" prefix.
- Switched from sprintf() to sysfs_emit().
- Refactored control flow to use a single return point.

Quoted 5 lines

v1: https://lore.kernel.org/lkml/[email protected]/

 .../hwtracing/coresight/coresight-etm3x-sysfs.c   | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)

Hi,

This is the friendly patch-bot of Greg Kroah-Hartman. You have sent him a patch that has triggered this response. He used to manually respond to these common problems, but in order to save his sanity (he kept writing the same thing over and over, yet to different people), I was created. Hopefully you will not take offence and will fix the problem in your patch and resubmit it so that it can be accepted into the Linux kernel tree.

You are receiving this message because of the following common error(s) as indicated below:

- You have marked a patch with a "Fixes:" tag for a commit that is in an older released kernel, yet you do not have a cc: stable line in the signed-off-by area at all, which means that the patch will not be applied to any older kernel releases. To properly fix this, please follow the documented rules in the Documentation/process/stable-kernel-rules.rst file for how to resolve this.

If you wish to discuss this problem further, or you have questions about how to resolve this issue, please feel free to respond to this email and Greg will reply once he has dug out from the pending patches received from other developers.

thanks,

greg k-h's patch email bot

reviewer
#2 lore raw
In reply to Kuan-Wei Chiu
Quoted 54 lines · Kuan-Wei Chiu

The cntr_val_show() function was intended to print the values of all counters using a loop. However, due to a buffer overwrite issue with sprintf(), it effectively only displayed the value of the last counter.

The companion function, cntr_val_store(), allows users to modify a specific counter selected by 'cntr_idx'. To maintain consistency between read and write operations and to align with the ETM4x driver behavior, modify cntr_val_show() to report only the value of the currently selected counter.

This change removes the loop and the "counter %d:" prefix, printing only the hexadecimal value. It also adopts sysfs_emit() for standard sysfs output formatting.

Fixes a939fc5a71ad ("coresight-etm: add CoreSight ETM/PTM driver")

---
Build test only.

Changes in v2:
- Redesigned the fix to match cntr_val_store() logic (display only the currently selected counter) instead of attempting to list all counters.
- Removed the loop and the "counter %d:" prefix.
- Switched from sprintf() to sysfs_emit().
- Refactored control flow to use a single return point.

v1: https://lore.kernel.org/lkml/[email protected]/

  .../hwtracing/coresight/coresight-etm3x-sysfs.c   | 15 ++++-----------
  1 file changed, 4 insertions(+), 11 deletions(-)
drivers/hwtracing/coresight/coresight-etm3x-sysfs.c +1 -6
index 762109307b86..a6a650331196 100644
@@ -717,26 +717,19 @@ static DEVICE_ATTR_RW(cntr_rld_event);
  static ssize_t cntr_val_show(struct device *dev,
  			     struct device_attribute *attr, char *buf)
  {
-	int i, ret = 0;
  	u32 val;
  	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
  	struct etm_config *config = &drvdata->config;
 
  	if (!coresight_get_mode(drvdata->csdev)) {
  		spin_lock(&drvdata->spinlock);
-		for (i = 0; i < drvdata->nr_cntr; i++)
-			ret += sprintf(buf, "counter %d: %x\n",
-				       i, config->cntr_val[i]);
+		val = config->cntr_val[config->cntr_idx];
  		spin_unlock(&drvdata->spinlock);
-		return ret;
-	}

drivers/hwtracing/coresight/coresight-etm3x-sysfs.c static DEVICE_ATTR_RW(cntr_rld_event); · line 732

Quoted 10 lines
drivers/hwtracing/coresight/coresight-etm3x-sysfs.c +3 -5
-
-	for (i = 0; i < drvdata->nr_cntr; i++) {
-		val = etm_readl(drvdata, ETMCNTVRn(i));
-		ret += sprintf(buf, "counter %d: %x\n", i, val);
+	} else {
+		val = etm_readl(drvdata, ETMCNTVRn(config->cntr_idx));
  	}
 
-	return ret;
+	return sysfs_emit(buf, "%x\n", val);

Sorry I missed this on the previous version, but this should be %#x to add the "0x" prefix. All the other ones in this file have it. Otherwise you can't tell the difference between decimal and hex.

Quoted 3 lines
drivers/hwtracing/coresight/coresight-etm3x-sysfs.c +0 -0
  }
 
  static ssize_t cntr_val_store(struct device *dev,
#3 lore raw
Quoted 61 lines · James Clark
Quoted 58 lines · Kuan-Wei Chiu

The cntr_val_show() function was intended to print the values of all counters using a loop. However, due to a buffer overwrite issue with sprintf(), it effectively only displayed the value of the last counter.

The companion function, cntr_val_store(), allows users to modify a specific counter selected by 'cntr_idx'. To maintain consistency between read and write operations and to align with the ETM4x driver behavior, modify cntr_val_show() to report only the value of the currently selected counter.

This change removes the loop and the "counter %d:" prefix, printing only the hexadecimal value. It also adopts sysfs_emit() for standard sysfs output formatting.

Fixes a939fc5a71ad ("coresight-etm: add CoreSight ETM/PTM driver")

---
Build test only.

Changes in v2:
- Redesigned the fix to match cntr_val_store() logic (display only the currently selected counter) instead of attempting to list all counters.
- Removed the loop and the "counter %d:" prefix.
- Switched from sprintf() to sysfs_emit().
- Refactored control flow to use a single return point.

v1: https://lore.kernel.org/lkml/[email protected]/

  .../hwtracing/coresight/coresight-etm3x-sysfs.c   | 15 ++++-----------
  1 file changed, 4 insertions(+), 11 deletions(-)
drivers/hwtracing/coresight/coresight-etm3x-sysfs.c +2 -10
index 762109307b86..a6a650331196 100644
@@ -717,26 +717,19 @@ static DEVICE_ATTR_RW(cntr_rld_event);
  static ssize_t cntr_val_show(struct device *dev,
  			     struct device_attribute *attr, char *buf)
  {
-	int i, ret = 0;
  	u32 val;
  	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
  	struct etm_config *config = &drvdata->config;
  	if (!coresight_get_mode(drvdata->csdev)) {
  		spin_lock(&drvdata->spinlock);
-		for (i = 0; i < drvdata->nr_cntr; i++)
-			ret += sprintf(buf, "counter %d: %x\n",
-				       i, config->cntr_val[i]);
+		val = config->cntr_val[config->cntr_idx];
  		spin_unlock(&drvdata->spinlock);
-		return ret;
-	}
-
-	for (i = 0; i < drvdata->nr_cntr; i++) {
-		val = etm_readl(drvdata, ETMCNTVRn(i));
-		ret += sprintf(buf, "counter %d: %x\n", i, val);
+	} else {
Quoted 9 lines

drivers/hwtracing/coresight/coresight-etm3x-sysfs.c static DEVICE_ATTR_RW(cntr_rld_event); · line 732

Quoted 4 lines
drivers/hwtracing/coresight/coresight-etm3x-sysfs.c +2 -1
+		val = etm_readl(drvdata, ETMCNTVRn(config->cntr_idx));
  	}
-	return ret;
+	return sysfs_emit(buf, "%x\n", val);

Sorry I missed this on the previous version, but this should be %#x to add the "0x" prefix. All the other ones in this file have it. Otherwise you can't tell the difference between decimal and hex.

Thanks for catching that. I will fix it in v3.

I'll wait a day or two to see if there are any other comments, and then respin with the Cc stable tag.

Regards,
Kuan-Wei

Quoted 3 lines
Quoted 2 lines
drivers/hwtracing/coresight/coresight-etm3x-sysfs.c +0 -0
  }
  static ssize_t cntr_val_store(struct device *dev,