Skip to content

Commit 8dba0fd

Browse files
viveky1794rafaeljw
authored andcommitted
cpuidle: sysfs: Use sysfs_emit()/sysfs_emit_at() instead of sprintf()/scnprintf()
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> Link: https://patch.msgid.link/20250919165657.233349-1-vivekyadav1207731111@gmail.com [ rjw: Minor subject edits ] Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent a2d100c commit 8dba0fd

1 file changed

Lines changed: 17 additions & 17 deletions

File tree

drivers/cpuidle/sysfs.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ static ssize_t show_available_governors(struct device *dev,
2727

2828
mutex_lock(&cpuidle_lock);
2929
list_for_each_entry(tmp, &cpuidle_governors, governor_list) {
30-
if (i >= (ssize_t) (PAGE_SIZE - (CPUIDLE_NAME_LEN + 2)))
30+
if (i >= (ssize_t)(PAGE_SIZE - (CPUIDLE_NAME_LEN + 2)))
3131
goto out;
3232

33-
i += scnprintf(&buf[i], CPUIDLE_NAME_LEN + 1, "%s ", tmp->name);
33+
i += sysfs_emit_at(buf, i, "%.*s ", CPUIDLE_NAME_LEN, tmp->name);
3434
}
3535

3636
out:
37-
i+= sprintf(&buf[i], "\n");
37+
i += sysfs_emit_at(buf, i, "\n");
3838
mutex_unlock(&cpuidle_lock);
3939
return i;
4040
}
@@ -49,9 +49,9 @@ static ssize_t show_current_driver(struct device *dev,
4949
spin_lock(&cpuidle_driver_lock);
5050
drv = cpuidle_get_driver();
5151
if (drv)
52-
ret = sprintf(buf, "%s\n", drv->name);
52+
ret = sysfs_emit(buf, "%s\n", drv->name);
5353
else
54-
ret = sprintf(buf, "none\n");
54+
ret = sysfs_emit(buf, "none\n");
5555
spin_unlock(&cpuidle_driver_lock);
5656

5757
return ret;
@@ -65,9 +65,9 @@ static ssize_t show_current_governor(struct device *dev,
6565

6666
mutex_lock(&cpuidle_lock);
6767
if (cpuidle_curr_governor)
68-
ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name);
68+
ret = sysfs_emit(buf, "%s\n", cpuidle_curr_governor->name);
6969
else
70-
ret = sprintf(buf, "none\n");
70+
ret = sysfs_emit(buf, "none\n");
7171
mutex_unlock(&cpuidle_lock);
7272

7373
return ret;
@@ -230,15 +230,15 @@ static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0644, show, store)
230230
static ssize_t show_state_##_name(struct cpuidle_state *state, \
231231
struct cpuidle_state_usage *state_usage, char *buf) \
232232
{ \
233-
return sprintf(buf, "%u\n", state->_name);\
233+
return sysfs_emit(buf, "%u\n", state->_name);\
234234
}
235235

236236
#define define_show_state_ull_function(_name) \
237237
static ssize_t show_state_##_name(struct cpuidle_state *state, \
238238
struct cpuidle_state_usage *state_usage, \
239239
char *buf) \
240240
{ \
241-
return sprintf(buf, "%llu\n", state_usage->_name);\
241+
return sysfs_emit(buf, "%llu\n", state_usage->_name);\
242242
}
243243

244244
#define define_show_state_str_function(_name) \
@@ -247,16 +247,16 @@ static ssize_t show_state_##_name(struct cpuidle_state *state, \
247247
char *buf) \
248248
{ \
249249
if (state->_name[0] == '\0')\
250-
return sprintf(buf, "<null>\n");\
251-
return sprintf(buf, "%s\n", state->_name);\
250+
return sysfs_emit(buf, "<null>\n");\
251+
return sysfs_emit(buf, "%s\n", state->_name);\
252252
}
253253

254254
#define define_show_state_time_function(_name) \
255255
static ssize_t show_state_##_name(struct cpuidle_state *state, \
256256
struct cpuidle_state_usage *state_usage, \
257257
char *buf) \
258258
{ \
259-
return sprintf(buf, "%llu\n", ktime_to_us(state->_name##_ns)); \
259+
return sysfs_emit(buf, "%llu\n", ktime_to_us(state->_name##_ns)); \
260260
}
261261

262262
define_show_state_time_function(exit_latency)
@@ -273,14 +273,14 @@ static ssize_t show_state_time(struct cpuidle_state *state,
273273
struct cpuidle_state_usage *state_usage,
274274
char *buf)
275275
{
276-
return sprintf(buf, "%llu\n", ktime_to_us(state_usage->time_ns));
276+
return sysfs_emit(buf, "%llu\n", ktime_to_us(state_usage->time_ns));
277277
}
278278

279279
static ssize_t show_state_disable(struct cpuidle_state *state,
280280
struct cpuidle_state_usage *state_usage,
281281
char *buf)
282282
{
283-
return sprintf(buf, "%llu\n",
283+
return sysfs_emit(buf, "%llu\n",
284284
state_usage->disable & CPUIDLE_STATE_DISABLED_BY_USER);
285285
}
286286

@@ -310,7 +310,7 @@ static ssize_t show_state_default_status(struct cpuidle_state *state,
310310
struct cpuidle_state_usage *state_usage,
311311
char *buf)
312312
{
313-
return sprintf(buf, "%s\n",
313+
return sysfs_emit(buf, "%s\n",
314314
state->flags & CPUIDLE_FLAG_OFF ? "disabled" : "enabled");
315315
}
316316

@@ -358,7 +358,7 @@ static ssize_t show_state_s2idle_##_name(struct cpuidle_state *state, \
358358
struct cpuidle_state_usage *state_usage, \
359359
char *buf) \
360360
{ \
361-
return sprintf(buf, "%llu\n", state_usage->s2idle_##_name);\
361+
return sysfs_emit(buf, "%llu\n", state_usage->s2idle_##_name);\
362362
}
363363

364364
define_show_state_s2idle_ull_function(usage);
@@ -550,7 +550,7 @@ static ssize_t show_driver_name(struct cpuidle_driver *drv, char *buf)
550550
ssize_t ret;
551551

552552
spin_lock(&cpuidle_driver_lock);
553-
ret = sprintf(buf, "%s\n", drv ? drv->name : "none");
553+
ret = sysfs_emit(buf, "%s\n", drv ? drv->name : "none");
554554
spin_unlock(&cpuidle_driver_lock);
555555

556556
return ret;

0 commit comments

Comments
 (0)