Skip to contentSkip to conversation

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

PATCH In review Kuan-Wei Chiu 3 participants 7 messages Dec 2, 2025 – Jun 9

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 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 v3:
- Switch format specifier to %#x to include the 0x prefix.
- Add Cc stable

v2: 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..b3c67e96a82a 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.158.g65b55ccf14-goog
reviewer Reviewed-by on v3 1/1
#1 lore raw
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 v3:
- Switch format specifier to %#x to include the 0x prefix.
- Add Cc stable

v2: 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 -9
index 762109307b86..b3c67e96a82a 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));

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

Quoted 10 lines
drivers/hwtracing/coresight/coresight-etm3x-sysfs.c +3 -2
-		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,
#2 lore raw
Quoted 58 lines · James Clark
Quoted 55 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 v3:
- Switch format specifier to %#x to include the 0x prefix.
- Add Cc stable

v2: 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..b3c67e96a82a 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 735

Quoted 6 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);
  }
  static ssize_t cntr_val_store(struct device *dev,

Thanks for the review!
Is there anything else I need to do for this fix to land?

Regards,
Kuan-Wei

maintainer
#3 lore raw

Hello

Quoted 68 lines · Kuan-Wei Chiu
Quoted 58 lines · James Clark
Quoted 55 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 v3:
- Switch format specifier to %#x to include the 0x prefix.
- Add Cc stable

v2: 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..b3c67e96a82a 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 735

Quoted 6 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);
   }
   static ssize_t cntr_val_store(struct device *dev,
Quoted 2 lines

Thanks for the review!
Is there anything else I need to do for this fix to land?

Thanks for the patch, I will queue this for the next release (v7.1).

Suzuki

Quoted 4 lines

Regards,
Kuan-Wei

#4 lore raw

Hi Suzuki,

Quoted 71 lines · Suzuki K Poulose

Hello

Quoted 68 lines · Kuan-Wei Chiu
Quoted 67 lines · James Clark
Quoted 51 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 v3:
- Switch format specifier to %#x to include the 0x prefix.
- Add Cc stable

v2: 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 -7
index 762109307b86..b3c67e96a82a 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 -4
-	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,
Quoted 5 lines
Quoted 2 lines

Thanks for the review!
Is there anything else I need to do for this fix to land?

Thanks for the patch, I will queue this for the next release (v7.1).

Just a gentle ping.

Since the v7.1 merge window is presumably opening in about a week, I noticed this patch isn't in linux-next yet and wanted to send a quick reminder. Thanks.

Regards,
Kuan-Wei

#5 lore raw

Hi Suzuki,

Quoted 77 lines · Kuan-Wei Chiu

Hi Suzuki,

Quoted 74 lines · Suzuki K Poulose

Hello

Quoted 70 lines · Kuan-Wei Chiu
Quoted 58 lines · James Clark
Quoted 55 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 v3:
- Switch format specifier to %#x to include the 0x prefix.
- Add Cc stable

v2: 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..b3c67e96a82a 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 735

Quoted 6 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);
   }
   static ssize_t cntr_val_store(struct device *dev,

Thanks for the review!
Is there anything else I need to do for this fix to land?

Quoted 8 lines
Quoted 2 lines

Thanks for the patch, I will queue this for the next release (v7.1).

Just a gentle ping.

Since the v7.1 merge window is presumably opening in about a week, I noticed this patch isn't in linux-next yet and wanted to send a quick reminder. Thanks.

This patch still applies cleanly on top of linux-next. I suspect this patch may have fallen through the cracks. Would you still be willing to pick it up?

Regards,
Kuan-Wei

maintainer
#6 lore raw
Quoted 84 lines · Kuan-Wei Chiu

Hi Suzuki,

Quoted 81 lines · Kuan-Wei Chiu

Hi Suzuki,

Quoted 71 lines · Suzuki K Poulose

Hello

Quoted 68 lines · Kuan-Wei Chiu
Quoted 67 lines · James Clark
Quoted 51 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 v3:
- Switch format specifier to %#x to include the 0x prefix.
- Add Cc stable

v2: 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 -7
index 762109307b86..b3c67e96a82a 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 -4
-	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,
Quoted 5 lines
Quoted 2 lines

Thanks for the review!
Is there anything else I need to do for this fix to land?

Thanks for the patch, I will queue this for the next release (v7.1).

Just a gentle ping.

Quoted 7 lines
Quoted 4 lines

Since the v7.1 merge window is presumably opening in about a week, I noticed this patch isn't in linux-next yet and wanted to send a quick reminder. Thanks.

This patch still applies cleanly on top of linux-next. I suspect this patch may have fallen through the cracks. Would you still be willing to pick it up?

Apologies, it did. I will pick this up, if we have sufficient fixes, I might send it as fixes for v7.2, otherwise , queue it for v7.3

Once again, apologies.

Suzuki

Quoted 3 lines

Regards,
Kuan-Wei