Skip to contentSkip to conversation

thermal: intel: int340x: Use sysfs_emit{_at} in sysfs show functions

PATCH Applied Thorsten Blum 3 participants 3 messages Dec 20, 2025 – Jan 7 Thermal

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 thermal: intel: int340x: Use sysfs_emit{_at} in sysfs show functions

Replace sprintf() with sysfs_emit() and sysfs_emit_at() in sysfs show functions. sysfs_emit() and sysfs_emit_at() are preferred to format sysfs output as it provides better bounds checking.

---
 .../intel/int340x_thermal/processor_thermal_device.c     | 5 +++--
 .../intel/int340x_thermal/processor_thermal_rfim.c       | 9 +++++----
 .../intel/int340x_thermal/processor_thermal_wt_req.c     | 7 ++++---
 3 files changed, 12 insertions(+), 9 deletions(-)
drivers/thermal/intel/int340x_thermal/processor_thermal_device.c +3 -2
index 48e7849d4816..f80dbe2ca7e4 100644
@@ -8,6 +8,7 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/pci.h>
+#include <linux/sysfs.h>
 #include <linux/thermal.h>
 #include <asm/msr.h>
 #include "int340x_thermal_zone.h"
@@ -23,7 +24,7 @@ static ssize_t power_limit_##index##_##suffix##_show(struct device *dev, \
 { \
 	struct proc_thermal_device *proc_dev = dev_get_drvdata(dev); \
 	\
-	return sprintf(buf, "%lu\n",\
+	return sysfs_emit(buf, "%lu\n",\
 	(unsigned long)proc_dev->power_limits[index].suffix * 1000); \
 }
 
@@ -143,7 +144,7 @@ static ssize_t tcc_offset_degree_celsius_show(struct device *dev,
 	if (offset < 0)
 		return offset;
 
-	return sprintf(buf, "%d\n", offset);
+	return sysfs_emit(buf, "%d\n", offset);
 }
 
 static ssize_t tcc_offset_degree_celsius_store(struct device *dev,
drivers/thermal/intel/int340x_thermal/processor_thermal_rfim.c +5 -4
index 589a3a71f0c4..bb9398dfa3c1 100644
@@ -7,6 +7,7 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/pci.h>
+#include <linux/sysfs.h>
 #include "processor_thermal_device.h"
 
 MODULE_IMPORT_NS("INT340X_THERMAL");
@@ -211,9 +212,9 @@ static ssize_t suffix##_show(struct device *dev,\
 	ret = (reg_val >> mmio_regs[ret].shift) & mmio_regs[ret].mask;\
 	err = get_mapped_string(mapping, attr->attr.name, ret, &str);\
 	if (!err)\
-		return sprintf(buf, "%s\n", str);\
+		return sysfs_emit(buf, "%s\n", str);\
 	if (err == -EOPNOTSUPP)\
-		return sprintf(buf, "%u\n", ret);\
+		return sysfs_emit(buf, "%u\n", ret);\
 	return err;\
 }
 
@@ -398,7 +399,7 @@ static ssize_t rfi_restriction_show(struct device *dev,
 	if (ret)
 		return ret;
 
-	return sprintf(buf, "%llu\n", resp);
+	return sysfs_emit(buf, "%llu\n", resp);
 }
 
 static ssize_t ddr_data_rate_show(struct device *dev,
@@ -413,7 +414,7 @@ static ssize_t ddr_data_rate_show(struct device *dev,
 	if (ret)
 		return ret;
 
-	return sprintf(buf, "%llu\n", resp);
+	return sysfs_emit(buf, "%llu\n", resp);
 }
 
 static DEVICE_ATTR_RW(rfi_restriction);
drivers/thermal/intel/int340x_thermal/processor_thermal_wt_req.c +4 -3
index b95810f4a011..2372f5202019 100644
@@ -7,6 +7,7 @@
  */
 
 #include <linux/pci.h>
+#include <linux/sysfs.h>
 #include "processor_thermal_device.h"
 
 /* List of workload types */
@@ -28,9 +29,9 @@ static ssize_t workload_available_types_show(struct device *dev,
 	int ret = 0;
 
 	while (workload_types[i] != NULL)
-		ret += sprintf(&buf[ret], "%s ", workload_types[i++]);
+		ret += sysfs_emit_at(buf, ret, "%s ", workload_types[i++]);
 
-	ret += sprintf(&buf[ret], "\n");
+	ret += sysfs_emit_at(buf, ret, "\n");
 
 	return ret;
 }
@@ -85,7 +86,7 @@ static ssize_t workload_type_show(struct device *dev,
 	if (cmd_resp > ARRAY_SIZE(workload_types) - 1)
 		return -EINVAL;
 
-	return sprintf(buf, "%s\n", workload_types[cmd_resp]);
+	return sysfs_emit(buf, "%s\n", workload_types[cmd_resp]);
 }
 
 static DEVICE_ATTR_RW(workload_type);
Signature · 2 lines
Thorsten Blum <[email protected]>
GPG: 1D60 735E 8AEF 3BE4 73B6  9D84 7336 78FD 8DFE EAD4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Thorsten Blum

Replace sprintf() with sysfs_emit() and sysfs_emit_at() in sysfs show functions. sysfs_emit() and sysfs_emit_at() are preferred to format sysfs output as it provides better bounds checking.

Quoted 134 lines

---
 .../intel/int340x_thermal/processor_thermal_device.c     | 5 +++-- .../intel/int340x_thermal/processor_thermal_rfim.c       | 9 +++++-- --
 .../intel/int340x_thermal/processor_thermal_wt_req.c     | 7 ++++---

 3 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/drivers/thermal/intel/int340x_thermal/processor_thermal_device.c b/drivers/thermal/intel/int340x_thermal/processor_thermal_device.c index 48e7849d4816..f80dbe2ca7e4 100644
---
a/drivers/thermal/intel/int340x_thermal/processor_thermal_device.c
+++
b/drivers/thermal/intel/int340x_thermal/processor_thermal_device.c

drivers/thermal/intel/int340x_thermal/processor_thermal_device.c +2 -1
@@ -8,6 +8,7 @@
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/pci.h>
+#include <linux/sysfs.h>
  #include <linux/thermal.h>
  #include <asm/msr.h>
  #include "int340x_thermal_zone.h"
@@ -23,7 +24,7 @@ static ssize_t
 power_limit_##index##_##suffix##_show(struct device *dev, \
  { \
  	struct proc_thermal_device *proc_dev = dev_get_drvdata(dev);
 \
  	\
-	return sprintf(buf, "%lu\n",\
+	return sysfs_emit(buf, "%lu\n",\
  	(unsigned long)proc_dev->power_limits[index].suffix * 1000);

\
 }

drivers/thermal/intel/int340x_thermal/processor_thermal_device.c +0 -0
@@ -143,7 +144,7 @@ static ssize_t
 tcc_offset_degree_celsius_show(struct device *dev,
  	if (offset < 0)
  		return offset;

drivers/thermal/intel/int340x_thermal/processor_thermal_device.c +1 -1
-	return sprintf(buf, "%d\n", offset);
+	return sysfs_emit(buf, "%d\n", offset);

 }

 static ssize_t tcc_offset_degree_celsius_store(struct device *dev, diff --git a/drivers/thermal/intel/int340x_thermal/processor_thermal_rfim.c b/drivers/thermal/intel/int340x_thermal/processor_thermal_rfim.c index 589a3a71f0c4..bb9398dfa3c1 100644

drivers/thermal/intel/int340x_thermal/processor_thermal_rfim.c +1 -0
@@ -7,6 +7,7 @@
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/pci.h>
+#include <linux/sysfs.h>
  #include "processor_thermal_device.h"


 MODULE_IMPORT_NS("INT340X_THERMAL");

drivers/thermal/intel/int340x_thermal/processor_thermal_rfim.c +0 -0
@@ -211,9 +212,9 @@ static ssize_t suffix##_show(struct device *dev,\

  ret = (reg_val >> mmio_regs[ret].shift) & mmio_regs[ret].mask;\
  err = get_mapped_string(mapping, attr->attr.name, ret, &str);\
  if (!err)\

drivers/thermal/intel/int340x_thermal/processor_thermal_rfim.c +1 -1
-		return sprintf(buf, "%s\n", str);\
+		return sysfs_emit(buf, "%s\n", str);\

  if (err == -EOPNOTSUPP)\

drivers/thermal/intel/int340x_thermal/processor_thermal_rfim.c +1 -1
-		return sprintf(buf, "%u\n", ret);\
+		return sysfs_emit(buf, "%u\n", ret);\

  return err;\
 }

drivers/thermal/intel/int340x_thermal/processor_thermal_rfim.c +0 -0
@@ -398,7 +399,7 @@ static ssize_t rfi_restriction_show(struct device
 *dev,
  	if (ret)
  		return ret;

drivers/thermal/intel/int340x_thermal/processor_thermal_rfim.c +1 -1
-	return sprintf(buf, "%llu\n", resp);
+	return sysfs_emit(buf, "%llu\n", resp);

 }

 static ssize_t ddr_data_rate_show(struct device *dev,

drivers/thermal/intel/int340x_thermal/processor_thermal_rfim.c +0 -0
@@ -413,7 +414,7 @@ static ssize_t ddr_data_rate_show(struct device
 *dev,
  	if (ret)
  		return ret;

drivers/thermal/intel/int340x_thermal/processor_thermal_rfim.c +1 -1
-	return sprintf(buf, "%llu\n", resp);
+	return sysfs_emit(buf, "%llu\n", resp);

 }

 static DEVICE_ATTR_RW(rfi_restriction);
diff --git a/drivers/thermal/intel/int340x_thermal/processor_thermal_wt_req.c b/drivers/thermal/intel/int340x_thermal/processor_thermal_wt_req.c index b95810f4a011..2372f5202019 100644
---
a/drivers/thermal/intel/int340x_thermal/processor_thermal_wt_req.c
+++
b/drivers/thermal/intel/int340x_thermal/processor_thermal_wt_req.c

drivers/thermal/intel/int340x_thermal/processor_thermal_rfim.c +0 -0
@@ -7,6 +7,7 @@
   */


 #include <linux/pci.h>
+#include <linux/sysfs.h>
 #include "processor_thermal_device.h"

 /* List of workload types */

drivers/thermal/intel/int340x_thermal/processor_thermal_rfim.c +0 -0
@@ -28,9 +29,9 @@ static ssize_t workload_available_types_show(struct
 device *dev,
  	int ret = 0;


  while (workload_types[i] != NULL)

drivers/thermal/intel/int340x_thermal/processor_thermal_rfim.c +0 -1
-		ret += sprintf(&buf[ret], "%s ",

workload_types[i++]);

drivers/thermal/intel/int340x_thermal/processor_thermal_rfim.c +1 -0
+		ret += sysfs_emit_at(buf, ret, "%s ",

workload_types[i++]);

drivers/thermal/intel/int340x_thermal/processor_thermal_rfim.c +1 -1
-	ret += sprintf(&buf[ret], "\n");
+	ret += sysfs_emit_at(buf, ret, "\n");


  return ret;
 }

drivers/thermal/intel/int340x_thermal/processor_thermal_rfim.c +0 -0
@@ -85,7 +86,7 @@ static ssize_t workload_type_show(struct device
 *dev,
  	if (cmd_resp > ARRAY_SIZE(workload_types) - 1)
  		return -EINVAL;

drivers/thermal/intel/int340x_thermal/processor_thermal_rfim.c +1 -1
-	return sprintf(buf, "%s\n", workload_types[cmd_resp]);
+	return sysfs_emit(buf, "%s\n", workload_types[cmd_resp]);

 }

 static DEVICE_ATTR_RW(workload_type);