* [PATCH v2] cpuidle: sysfs: Use sysfs_emit/sysfs_emit_at instead of sprintf/scnprintf
@ 2025-09-15 17:41 vivekyadav1207731111
0 siblings, 0 replies; 3+ messages in thread
From: vivekyadav1207731111 @ 2025-09-15 17:41 UTC (permalink / raw)
To: rafael, daniel.lezcano; +Cc: linux-pm, linux-kernel, Vivek Yadav, Joe Perches
From: Vivek Yadav <vivekyadav1207731111@gmail.com>
The ->show() callbacks in sysfs should use sysfs_emit() or
sysfs_emit_at() when formatting values for user space. These helpers
are the recommended way to ensure correct buffer handling and
consistency across the kernel.
See Documentation/filesystems/sysfs.rst for details.
No functional change intended.
Suggested-by: Joe Perches <joe@perches.com>
Signed-off-by: vivek yadav <vivekyadav1207731111@gmail.com>
---
Changes in v2:
- Do not modify the variable 'i'; leave it unchanged.
---
drivers/cpuidle/sysfs.c | 34 +++++++++++++++++-----------------
1 file changed, 17 insertions(+), 17 deletions(-)
diff --git a/drivers/cpuidle/sysfs.c b/drivers/cpuidle/sysfs.c
index d6f5da61cb7d..61de64817604 100644
--- a/drivers/cpuidle/sysfs.c
+++ b/drivers/cpuidle/sysfs.c
@@ -27,14 +27,14 @@ static ssize_t show_available_governors(struct device *dev,
mutex_lock(&cpuidle_lock);
list_for_each_entry(tmp, &cpuidle_governors, governor_list) {
- if (i >= (ssize_t) (PAGE_SIZE - (CPUIDLE_NAME_LEN + 2)))
+ if (i >= (ssize_t)(PAGE_SIZE - (CPUIDLE_NAME_LEN + 2)))
goto out;
- i += scnprintf(&buf[i], CPUIDLE_NAME_LEN + 1, "%s ", tmp->name);
+ i += sysfs_emit_at(buf, i, "%.*s ", CPUIDLE_NAME_LEN, tmp->name);
}
out:
- i+= sprintf(&buf[i], "\n");
+ i += sysfs_emit_at(buf, i, "\n");
mutex_unlock(&cpuidle_lock);
return i;
}
@@ -49,9 +49,9 @@ static ssize_t show_current_driver(struct device *dev,
spin_lock(&cpuidle_driver_lock);
drv = cpuidle_get_driver();
if (drv)
- ret = sprintf(buf, "%s\n", drv->name);
+ ret = sysfs_emit(buf, "%s\n", drv->name);
else
- ret = sprintf(buf, "none\n");
+ ret = sysfs_emit(buf, "none\n");
spin_unlock(&cpuidle_driver_lock);
return ret;
@@ -65,9 +65,9 @@ static ssize_t show_current_governor(struct device *dev,
mutex_lock(&cpuidle_lock);
if (cpuidle_curr_governor)
- ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name);
+ ret = sysfs_emit(buf, "%s\n", cpuidle_curr_governor->name);
else
- ret = sprintf(buf, "none\n");
+ ret = sysfs_emit(buf, "none\n");
mutex_unlock(&cpuidle_lock);
return ret;
@@ -230,7 +230,7 @@ static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0644, show, store)
static ssize_t show_state_##_name(struct cpuidle_state *state, \
struct cpuidle_state_usage *state_usage, char *buf) \
{ \
- return sprintf(buf, "%u\n", state->_name);\
+ return sysfs_emit(buf, "%u\n", state->_name);\
}
#define define_show_state_ull_function(_name) \
@@ -238,7 +238,7 @@ static ssize_t show_state_##_name(struct cpuidle_state *state, \
struct cpuidle_state_usage *state_usage, \
char *buf) \
{ \
- return sprintf(buf, "%llu\n", state_usage->_name);\
+ return sysfs_emit(buf, "%llu\n", state_usage->_name);\
}
#define define_show_state_str_function(_name) \
@@ -247,8 +247,8 @@ static ssize_t show_state_##_name(struct cpuidle_state *state, \
char *buf) \
{ \
if (state->_name[0] == '\0')\
- return sprintf(buf, "<null>\n");\
- return sprintf(buf, "%s\n", state->_name);\
+ return sysfs_emit(buf, "<null>\n");\
+ return sysfs_emit(buf, "%s\n", state->_name);\
}
#define define_show_state_time_function(_name) \
@@ -256,7 +256,7 @@ static ssize_t show_state_##_name(struct cpuidle_state *state, \
struct cpuidle_state_usage *state_usage, \
char *buf) \
{ \
- return sprintf(buf, "%llu\n", ktime_to_us(state->_name##_ns)); \
+ return sysfs_emit(buf, "%llu\n", ktime_to_us(state->_name##_ns)); \
}
define_show_state_time_function(exit_latency)
@@ -273,14 +273,14 @@ static ssize_t show_state_time(struct cpuidle_state *state,
struct cpuidle_state_usage *state_usage,
char *buf)
{
- return sprintf(buf, "%llu\n", ktime_to_us(state_usage->time_ns));
+ return sysfs_emit(buf, "%llu\n", ktime_to_us(state_usage->time_ns));
}
static ssize_t show_state_disable(struct cpuidle_state *state,
struct cpuidle_state_usage *state_usage,
char *buf)
{
- return sprintf(buf, "%llu\n",
+ return sysfs_emit(buf, "%llu\n",
state_usage->disable & CPUIDLE_STATE_DISABLED_BY_USER);
}
@@ -310,7 +310,7 @@ static ssize_t show_state_default_status(struct cpuidle_state *state,
struct cpuidle_state_usage *state_usage,
char *buf)
{
- return sprintf(buf, "%s\n",
+ return sysfs_emit(buf, "%s\n",
state->flags & CPUIDLE_FLAG_OFF ? "disabled" : "enabled");
}
@@ -358,7 +358,7 @@ static ssize_t show_state_s2idle_##_name(struct cpuidle_state *state, \
struct cpuidle_state_usage *state_usage, \
char *buf) \
{ \
- return sprintf(buf, "%llu\n", state_usage->s2idle_##_name);\
+ return sysfs_emit(buf, "%llu\n", state_usage->s2idle_##_name);\
}
define_show_state_s2idle_ull_function(usage);
@@ -550,7 +550,7 @@ static ssize_t show_driver_name(struct cpuidle_driver *drv, char *buf)
ssize_t ret;
spin_lock(&cpuidle_driver_lock);
- ret = sprintf(buf, "%s\n", drv ? drv->name : "none");
+ ret = sysfs_emit(buf, "%s\n", drv ? drv->name : "none");
spin_unlock(&cpuidle_driver_lock);
return ret;
--
2.25.1
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] cpuidle: sysfs: Use sysfs_emit/sysfs_emit_at instead of sprintf/scnprintf
@ 2025-09-15 14:31 Rafael J. Wysocki
2025-09-19 16:56 ` [PATCH v2] " vivekyadav1207731111
0 siblings, 1 reply; 3+ messages in thread
From: Rafael J. Wysocki @ 2025-09-15 14:31 UTC (permalink / raw)
To: vivek yadav
Cc: Joe Perches, Rafael J. Wysocki, daniel.lezcano, linux-kernel, linux-pm
On Sun, Sep 14, 2025 at 5:20 PM vivek yadav
<vivekyadav1207731111@gmail.com> wrote:
>
> Hi @Rafael J. Wysocki ,
>
> Is there any update for me on this patch ?
As I said before, please do not rename the variable.
Thanks!
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v2] cpuidle: sysfs: Use sysfs_emit/sysfs_emit_at instead of sprintf/scnprintf
2025-09-15 14:31 [PATCH] " Rafael J. Wysocki
@ 2025-09-19 16:56 ` vivekyadav1207731111
2025-09-20 11:07 ` Rafael J. Wysocki
0 siblings, 1 reply; 3+ messages in thread
From: vivekyadav1207731111 @ 2025-09-19 16:56 UTC (permalink / raw)
To: joe, rafael, daniel.lezcano, linux-kernel, linux-pm, skhan,
linux-kernel-mentees
Cc: Vivek Yadav
From: Vivek Yadav <vivekyadav1207731111@gmail.com>
The ->show() callbacks in sysfs should use sysfs_emit() or
sysfs_emit_at() when formatting values for user space. These helpers
are the recommended way to ensure correct buffer handling and
consistency across the kernel.
See Documentation/filesystems/sysfs.rst for details.
No functional change intended.
Suggested-by: Joe Perches <joe@perches.com>
Signed-off-by: Vivek Yadav <vivekyadav1207731111@gmail.com>
---
drivers/cpuidle/sysfs.c | 34 +++++++++++++++++-----------------
1 file changed, 17 insertions(+), 17 deletions(-)
diff --git a/drivers/cpuidle/sysfs.c b/drivers/cpuidle/sysfs.c
index d6f5da61cb7d..61de64817604 100644
--- a/drivers/cpuidle/sysfs.c
+++ b/drivers/cpuidle/sysfs.c
@@ -27,14 +27,14 @@ static ssize_t show_available_governors(struct device *dev,
mutex_lock(&cpuidle_lock);
list_for_each_entry(tmp, &cpuidle_governors, governor_list) {
- if (i >= (ssize_t) (PAGE_SIZE - (CPUIDLE_NAME_LEN + 2)))
+ if (i >= (ssize_t)(PAGE_SIZE - (CPUIDLE_NAME_LEN + 2)))
goto out;
- i += scnprintf(&buf[i], CPUIDLE_NAME_LEN + 1, "%s ", tmp->name);
+ i += sysfs_emit_at(buf, i, "%.*s ", CPUIDLE_NAME_LEN, tmp->name);
}
out:
- i+= sprintf(&buf[i], "\n");
+ i += sysfs_emit_at(buf, i, "\n");
mutex_unlock(&cpuidle_lock);
return i;
}
@@ -49,9 +49,9 @@ static ssize_t show_current_driver(struct device *dev,
spin_lock(&cpuidle_driver_lock);
drv = cpuidle_get_driver();
if (drv)
- ret = sprintf(buf, "%s\n", drv->name);
+ ret = sysfs_emit(buf, "%s\n", drv->name);
else
- ret = sprintf(buf, "none\n");
+ ret = sysfs_emit(buf, "none\n");
spin_unlock(&cpuidle_driver_lock);
return ret;
@@ -65,9 +65,9 @@ static ssize_t show_current_governor(struct device *dev,
mutex_lock(&cpuidle_lock);
if (cpuidle_curr_governor)
- ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name);
+ ret = sysfs_emit(buf, "%s\n", cpuidle_curr_governor->name);
else
- ret = sprintf(buf, "none\n");
+ ret = sysfs_emit(buf, "none\n");
mutex_unlock(&cpuidle_lock);
return ret;
@@ -230,7 +230,7 @@ static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0644, show, store)
static ssize_t show_state_##_name(struct cpuidle_state *state, \
struct cpuidle_state_usage *state_usage, char *buf) \
{ \
- return sprintf(buf, "%u\n", state->_name);\
+ return sysfs_emit(buf, "%u\n", state->_name);\
}
#define define_show_state_ull_function(_name) \
@@ -238,7 +238,7 @@ static ssize_t show_state_##_name(struct cpuidle_state *state, \
struct cpuidle_state_usage *state_usage, \
char *buf) \
{ \
- return sprintf(buf, "%llu\n", state_usage->_name);\
+ return sysfs_emit(buf, "%llu\n", state_usage->_name);\
}
#define define_show_state_str_function(_name) \
@@ -247,8 +247,8 @@ static ssize_t show_state_##_name(struct cpuidle_state *state, \
char *buf) \
{ \
if (state->_name[0] == '\0')\
- return sprintf(buf, "<null>\n");\
- return sprintf(buf, "%s\n", state->_name);\
+ return sysfs_emit(buf, "<null>\n");\
+ return sysfs_emit(buf, "%s\n", state->_name);\
}
#define define_show_state_time_function(_name) \
@@ -256,7 +256,7 @@ static ssize_t show_state_##_name(struct cpuidle_state *state, \
struct cpuidle_state_usage *state_usage, \
char *buf) \
{ \
- return sprintf(buf, "%llu\n", ktime_to_us(state->_name##_ns)); \
+ return sysfs_emit(buf, "%llu\n", ktime_to_us(state->_name##_ns)); \
}
define_show_state_time_function(exit_latency)
@@ -273,14 +273,14 @@ static ssize_t show_state_time(struct cpuidle_state *state,
struct cpuidle_state_usage *state_usage,
char *buf)
{
- return sprintf(buf, "%llu\n", ktime_to_us(state_usage->time_ns));
+ return sysfs_emit(buf, "%llu\n", ktime_to_us(state_usage->time_ns));
}
static ssize_t show_state_disable(struct cpuidle_state *state,
struct cpuidle_state_usage *state_usage,
char *buf)
{
- return sprintf(buf, "%llu\n",
+ return sysfs_emit(buf, "%llu\n",
state_usage->disable & CPUIDLE_STATE_DISABLED_BY_USER);
}
@@ -310,7 +310,7 @@ static ssize_t show_state_default_status(struct cpuidle_state *state,
struct cpuidle_state_usage *state_usage,
char *buf)
{
- return sprintf(buf, "%s\n",
+ return sysfs_emit(buf, "%s\n",
state->flags & CPUIDLE_FLAG_OFF ? "disabled" : "enabled");
}
@@ -358,7 +358,7 @@ static ssize_t show_state_s2idle_##_name(struct cpuidle_state *state, \
struct cpuidle_state_usage *state_usage, \
char *buf) \
{ \
- return sprintf(buf, "%llu\n", state_usage->s2idle_##_name);\
+ return sysfs_emit(buf, "%llu\n", state_usage->s2idle_##_name);\
}
define_show_state_s2idle_ull_function(usage);
@@ -550,7 +550,7 @@ static ssize_t show_driver_name(struct cpuidle_driver *drv, char *buf)
ssize_t ret;
spin_lock(&cpuidle_driver_lock);
- ret = sprintf(buf, "%s\n", drv ? drv->name : "none");
+ ret = sysfs_emit(buf, "%s\n", drv ? drv->name : "none");
spin_unlock(&cpuidle_driver_lock);
return ret;
--
2.43.0
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH v2] cpuidle: sysfs: Use sysfs_emit/sysfs_emit_at instead of sprintf/scnprintf
2025-09-19 16:56 ` [PATCH v2] " vivekyadav1207731111
@ 2025-09-20 11:07 ` Rafael J. Wysocki
0 siblings, 0 replies; 3+ messages in thread
From: Rafael J. Wysocki @ 2025-09-20 11:07 UTC (permalink / raw)
To: vivekyadav1207731111
Cc: joe, rafael, daniel.lezcano, linux-kernel, linux-pm, skhan,
linux-kernel-mentees
On Fri, Sep 19, 2025 at 6:57 PM <vivekyadav1207731111@gmail.com> wrote:
>
> From: Vivek Yadav <vivekyadav1207731111@gmail.com>
>
> The ->show() callbacks in sysfs should use sysfs_emit() or
> sysfs_emit_at() when formatting values for user space. These helpers
> are the recommended way to ensure correct buffer handling and
> consistency across the kernel.
>
> See Documentation/filesystems/sysfs.rst for details.
>
> No functional change intended.
>
> Suggested-by: Joe Perches <joe@perches.com>
> Signed-off-by: Vivek Yadav <vivekyadav1207731111@gmail.com>
> ---
> drivers/cpuidle/sysfs.c | 34 +++++++++++++++++-----------------
> 1 file changed, 17 insertions(+), 17 deletions(-)
>
> diff --git a/drivers/cpuidle/sysfs.c b/drivers/cpuidle/sysfs.c
> index d6f5da61cb7d..61de64817604 100644
> --- a/drivers/cpuidle/sysfs.c
> +++ b/drivers/cpuidle/sysfs.c
> @@ -27,14 +27,14 @@ static ssize_t show_available_governors(struct device *dev,
>
> mutex_lock(&cpuidle_lock);
> list_for_each_entry(tmp, &cpuidle_governors, governor_list) {
> - if (i >= (ssize_t) (PAGE_SIZE - (CPUIDLE_NAME_LEN + 2)))
> + if (i >= (ssize_t)(PAGE_SIZE - (CPUIDLE_NAME_LEN + 2)))
> goto out;
>
> - i += scnprintf(&buf[i], CPUIDLE_NAME_LEN + 1, "%s ", tmp->name);
> + i += sysfs_emit_at(buf, i, "%.*s ", CPUIDLE_NAME_LEN, tmp->name);
> }
>
> out:
> - i+= sprintf(&buf[i], "\n");
> + i += sysfs_emit_at(buf, i, "\n");
> mutex_unlock(&cpuidle_lock);
> return i;
> }
> @@ -49,9 +49,9 @@ static ssize_t show_current_driver(struct device *dev,
> spin_lock(&cpuidle_driver_lock);
> drv = cpuidle_get_driver();
> if (drv)
> - ret = sprintf(buf, "%s\n", drv->name);
> + ret = sysfs_emit(buf, "%s\n", drv->name);
> else
> - ret = sprintf(buf, "none\n");
> + ret = sysfs_emit(buf, "none\n");
> spin_unlock(&cpuidle_driver_lock);
>
> return ret;
> @@ -65,9 +65,9 @@ static ssize_t show_current_governor(struct device *dev,
>
> mutex_lock(&cpuidle_lock);
> if (cpuidle_curr_governor)
> - ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name);
> + ret = sysfs_emit(buf, "%s\n", cpuidle_curr_governor->name);
> else
> - ret = sprintf(buf, "none\n");
> + ret = sysfs_emit(buf, "none\n");
> mutex_unlock(&cpuidle_lock);
>
> return ret;
> @@ -230,7 +230,7 @@ static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0644, show, store)
> static ssize_t show_state_##_name(struct cpuidle_state *state, \
> struct cpuidle_state_usage *state_usage, char *buf) \
> { \
> - return sprintf(buf, "%u\n", state->_name);\
> + return sysfs_emit(buf, "%u\n", state->_name);\
> }
>
> #define define_show_state_ull_function(_name) \
> @@ -238,7 +238,7 @@ static ssize_t show_state_##_name(struct cpuidle_state *state, \
> struct cpuidle_state_usage *state_usage, \
> char *buf) \
> { \
> - return sprintf(buf, "%llu\n", state_usage->_name);\
> + return sysfs_emit(buf, "%llu\n", state_usage->_name);\
> }
>
> #define define_show_state_str_function(_name) \
> @@ -247,8 +247,8 @@ static ssize_t show_state_##_name(struct cpuidle_state *state, \
> char *buf) \
> { \
> if (state->_name[0] == '\0')\
> - return sprintf(buf, "<null>\n");\
> - return sprintf(buf, "%s\n", state->_name);\
> + return sysfs_emit(buf, "<null>\n");\
> + return sysfs_emit(buf, "%s\n", state->_name);\
> }
>
> #define define_show_state_time_function(_name) \
> @@ -256,7 +256,7 @@ static ssize_t show_state_##_name(struct cpuidle_state *state, \
> struct cpuidle_state_usage *state_usage, \
> char *buf) \
> { \
> - return sprintf(buf, "%llu\n", ktime_to_us(state->_name##_ns)); \
> + return sysfs_emit(buf, "%llu\n", ktime_to_us(state->_name##_ns)); \
> }
>
> define_show_state_time_function(exit_latency)
> @@ -273,14 +273,14 @@ static ssize_t show_state_time(struct cpuidle_state *state,
> struct cpuidle_state_usage *state_usage,
> char *buf)
> {
> - return sprintf(buf, "%llu\n", ktime_to_us(state_usage->time_ns));
> + return sysfs_emit(buf, "%llu\n", ktime_to_us(state_usage->time_ns));
> }
>
> static ssize_t show_state_disable(struct cpuidle_state *state,
> struct cpuidle_state_usage *state_usage,
> char *buf)
> {
> - return sprintf(buf, "%llu\n",
> + return sysfs_emit(buf, "%llu\n",
> state_usage->disable & CPUIDLE_STATE_DISABLED_BY_USER);
> }
>
> @@ -310,7 +310,7 @@ static ssize_t show_state_default_status(struct cpuidle_state *state,
> struct cpuidle_state_usage *state_usage,
> char *buf)
> {
> - return sprintf(buf, "%s\n",
> + return sysfs_emit(buf, "%s\n",
> state->flags & CPUIDLE_FLAG_OFF ? "disabled" : "enabled");
> }
>
> @@ -358,7 +358,7 @@ static ssize_t show_state_s2idle_##_name(struct cpuidle_state *state, \
> struct cpuidle_state_usage *state_usage, \
> char *buf) \
> { \
> - return sprintf(buf, "%llu\n", state_usage->s2idle_##_name);\
> + return sysfs_emit(buf, "%llu\n", state_usage->s2idle_##_name);\
> }
>
> define_show_state_s2idle_ull_function(usage);
> @@ -550,7 +550,7 @@ static ssize_t show_driver_name(struct cpuidle_driver *drv, char *buf)
> ssize_t ret;
>
> spin_lock(&cpuidle_driver_lock);
> - ret = sprintf(buf, "%s\n", drv ? drv->name : "none");
> + ret = sysfs_emit(buf, "%s\n", drv ? drv->name : "none");
> spin_unlock(&cpuidle_driver_lock);
>
> return ret;
> --
Applied as 6.18 material, thanks!
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-09-20 11:07 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-09-15 17:41 [PATCH v2] cpuidle: sysfs: Use sysfs_emit/sysfs_emit_at instead of sprintf/scnprintf vivekyadav1207731111
-- strict thread matches above, loose matches on Subject: below --
2025-09-15 14:31 [PATCH] " Rafael J. Wysocki
2025-09-19 16:56 ` [PATCH v2] " vivekyadav1207731111
2025-09-20 11:07 ` Rafael J. Wysocki
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®