[PATCH] cpu/hotplug: replace snprintf and sprintf in show functions with sysfs_emit

From: cgel . zte
Date: Mon Nov 08 2021 - 04:12:15 EST


From: Ye Guojin <ye.guojin@xxxxxxxxxx>

coccicheck complains about the use of snprintf() in sysfs show
functions:
WARNING use scnprintf or sprintf

Use sysfs_emit or sysfs_emit_at instead of snprintf or sprintf makes
more sense.

Reported-by: Zeal Robot <zealci@xxxxxxxxxx>
Signed-off-by: Ye Guojin <ye.guojin@xxxxxxxxxx>
---
kernel/cpu.c | 15 ++++++---------
1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/kernel/cpu.c b/kernel/cpu.c
index fffe8b738201..fcaac665c53e 100644
--- a/kernel/cpu.c
+++ b/kernel/cpu.c
@@ -2261,7 +2261,7 @@ static ssize_t state_show(struct device *dev,
{
struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);

- return sprintf(buf, "%d\n", st->state);
+ return sysfs_emit(buf, "%d\n", st->state);
}
static DEVICE_ATTR_RO(state);

@@ -2309,7 +2309,7 @@ static ssize_t target_show(struct device *dev,
{
struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);

- return sprintf(buf, "%d\n", st->target);
+ return sysfs_emit(buf, "%d\n", st->target);
}
static DEVICE_ATTR_RW(target);

@@ -2368,7 +2368,7 @@ static ssize_t fail_show(struct device *dev,
{
struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);

- return sprintf(buf, "%d\n", st->fail);
+ return sysfs_emit(buf, "%d\n", st->fail);
}

static DEVICE_ATTR_RW(fail);
@@ -2397,8 +2397,7 @@ static ssize_t states_show(struct device *dev,
struct cpuhp_step *sp = cpuhp_get_step(i);

if (sp->name) {
- cur = sprintf(buf, "%3d: %s\n", i, sp->name);
- buf += cur;
+ cur = sysfs_emit_at(buf, res, "%3d: %s\n", i, sp->name);
res += cur;
}
}
@@ -2481,9 +2480,7 @@ static const char *smt_states[] = {
static ssize_t control_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
- const char *state = smt_states[cpu_smt_control];
-
- return snprintf(buf, PAGE_SIZE - 2, "%s\n", state);
+ return sysfs_emit(buf, "%s\n", smt_states[cpu_smt_control]);
}

static ssize_t control_store(struct device *dev, struct device_attribute *attr,
@@ -2496,7 +2493,7 @@ static DEVICE_ATTR_RW(control);
static ssize_t active_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
- return snprintf(buf, PAGE_SIZE - 2, "%d\n", sched_smt_active());
+ return sysfs_emit(buf, "%d\n", sched_smt_active());
}
static DEVICE_ATTR_RO(active);

--
2.25.1