Skip to content

Commit a712f03

Browse files
Xueqin Luorafaeljw
authored andcommitted
PM: sleep: Use sysfs_emit() and sysfs_emit_at() in "show" functions
As Documentation/filesystems/sysfs.rst suggested, show() should only use sysfs_emit() or sysfs_emit_at() when formatting the value to be returned to user space. No functional change intended. Signed-off-by: Xueqin Luo <[email protected]> Link: https://patch.msgid.link/[email protected] [ rjw: Subject edit ] Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent 6306653 commit a712f03

File tree

1 file changed

+39
-37
lines changed

1 file changed

+39
-37
lines changed

kernel/power/main.c

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ int pm_async_enabled = 1;
115115
static ssize_t pm_async_show(struct kobject *kobj, struct kobj_attribute *attr,
116116
char *buf)
117117
{
118-
return sprintf(buf, "%d\n", pm_async_enabled);
118+
return sysfs_emit(buf, "%d\n", pm_async_enabled);
119119
}
120120

121121
static ssize_t pm_async_store(struct kobject *kobj, struct kobj_attribute *attr,
@@ -139,7 +139,7 @@ power_attr(pm_async);
139139
static ssize_t mem_sleep_show(struct kobject *kobj, struct kobj_attribute *attr,
140140
char *buf)
141141
{
142-
char *s = buf;
142+
ssize_t count = 0;
143143
suspend_state_t i;
144144

145145
for (i = PM_SUSPEND_MIN; i < PM_SUSPEND_MAX; i++) {
@@ -149,17 +149,17 @@ static ssize_t mem_sleep_show(struct kobject *kobj, struct kobj_attribute *attr,
149149
const char *label = mem_sleep_states[i];
150150

151151
if (mem_sleep_current == i)
152-
s += sprintf(s, "[%s] ", label);
152+
count += sysfs_emit_at(buf, count, "[%s] ", label);
153153
else
154-
s += sprintf(s, "%s ", label);
154+
count += sysfs_emit_at(buf, count, "%s ", label);
155155
}
156156
}
157157

158158
/* Convert the last space to a newline if needed. */
159-
if (s != buf)
160-
*(s-1) = '\n';
159+
if (count > 0)
160+
buf[count - 1] = '\n';
161161

162-
return (s - buf);
162+
return count;
163163
}
164164

165165
static suspend_state_t decode_suspend_state(const char *buf, size_t n)
@@ -220,7 +220,7 @@ bool sync_on_suspend_enabled = !IS_ENABLED(CONFIG_SUSPEND_SKIP_SYNC);
220220
static ssize_t sync_on_suspend_show(struct kobject *kobj,
221221
struct kobj_attribute *attr, char *buf)
222222
{
223-
return sprintf(buf, "%d\n", sync_on_suspend_enabled);
223+
return sysfs_emit(buf, "%d\n", sync_on_suspend_enabled);
224224
}
225225

226226
static ssize_t sync_on_suspend_store(struct kobject *kobj,
@@ -257,22 +257,22 @@ static const char * const pm_tests[__TEST_AFTER_LAST] = {
257257
static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr,
258258
char *buf)
259259
{
260-
char *s = buf;
260+
ssize_t count = 0;
261261
int level;
262262

263263
for (level = TEST_FIRST; level <= TEST_MAX; level++)
264264
if (pm_tests[level]) {
265265
if (level == pm_test_level)
266-
s += sprintf(s, "[%s] ", pm_tests[level]);
266+
count += sysfs_emit_at(buf, count, "[%s] ", pm_tests[level]);
267267
else
268-
s += sprintf(s, "%s ", pm_tests[level]);
268+
count += sysfs_emit_at(buf, count, "%s ", pm_tests[level]);
269269
}
270270

271-
if (s != buf)
272-
/* convert the last space to a newline */
273-
*(s-1) = '\n';
271+
/* Convert the last space to a newline if needed. */
272+
if (count > 0)
273+
buf[count - 1] = '\n';
274274

275-
return (s - buf);
275+
return count;
276276
}
277277

278278
static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
@@ -390,7 +390,7 @@ static const char * const suspend_step_names[] = {
390390
static ssize_t _name##_show(struct kobject *kobj, \
391391
struct kobj_attribute *attr, char *buf) \
392392
{ \
393-
return sprintf(buf, format_str, suspend_stats._name); \
393+
return sysfs_emit(buf, format_str, suspend_stats._name);\
394394
} \
395395
static struct kobj_attribute _name = __ATTR_RO(_name)
396396

@@ -404,7 +404,7 @@ suspend_attr(max_hw_sleep, "%llu\n");
404404
static ssize_t _name##_show(struct kobject *kobj, \
405405
struct kobj_attribute *attr, char *buf) \
406406
{ \
407-
return sprintf(buf, "%u\n", \
407+
return sysfs_emit(buf, "%u\n", \
408408
suspend_stats.step_failures[step-1]); \
409409
} \
410410
static struct kobj_attribute _name = __ATTR_RO(_name)
@@ -428,7 +428,7 @@ static ssize_t last_failed_dev_show(struct kobject *kobj,
428428
index %= REC_FAILED_NUM;
429429
last_failed_dev = suspend_stats.failed_devs[index];
430430

431-
return sprintf(buf, "%s\n", last_failed_dev);
431+
return sysfs_emit(buf, "%s\n", last_failed_dev);
432432
}
433433
static struct kobj_attribute last_failed_dev = __ATTR_RO(last_failed_dev);
434434

@@ -442,7 +442,7 @@ static ssize_t last_failed_errno_show(struct kobject *kobj,
442442
index %= REC_FAILED_NUM;
443443
last_failed_errno = suspend_stats.errno[index];
444444

445-
return sprintf(buf, "%d\n", last_failed_errno);
445+
return sysfs_emit(buf, "%d\n", last_failed_errno);
446446
}
447447
static struct kobj_attribute last_failed_errno = __ATTR_RO(last_failed_errno);
448448

@@ -456,7 +456,7 @@ static ssize_t last_failed_step_show(struct kobject *kobj,
456456
index %= REC_FAILED_NUM;
457457
step = suspend_stats.failed_steps[index];
458458

459-
return sprintf(buf, "%s\n", suspend_step_names[step]);
459+
return sysfs_emit(buf, "%s\n", suspend_step_names[step]);
460460
}
461461
static struct kobj_attribute last_failed_step = __ATTR_RO(last_failed_step);
462462

@@ -571,7 +571,7 @@ bool pm_print_times_enabled;
571571
static ssize_t pm_print_times_show(struct kobject *kobj,
572572
struct kobj_attribute *attr, char *buf)
573573
{
574-
return sprintf(buf, "%d\n", pm_print_times_enabled);
574+
return sysfs_emit(buf, "%d\n", pm_print_times_enabled);
575575
}
576576

577577
static ssize_t pm_print_times_store(struct kobject *kobj,
@@ -604,7 +604,7 @@ static ssize_t pm_wakeup_irq_show(struct kobject *kobj,
604604
if (!pm_wakeup_irq())
605605
return -ENODATA;
606606

607-
return sprintf(buf, "%u\n", pm_wakeup_irq());
607+
return sysfs_emit(buf, "%u\n", pm_wakeup_irq());
608608
}
609609

610610
power_attr_ro(pm_wakeup_irq);
@@ -620,7 +620,7 @@ EXPORT_SYMBOL_GPL(pm_debug_messages_should_print);
620620
static ssize_t pm_debug_messages_show(struct kobject *kobj,
621621
struct kobj_attribute *attr, char *buf)
622622
{
623-
return sprintf(buf, "%d\n", pm_debug_messages_on);
623+
return sysfs_emit(buf, "%d\n", pm_debug_messages_on);
624624
}
625625

626626
static ssize_t pm_debug_messages_store(struct kobject *kobj,
@@ -668,21 +668,23 @@ struct kobject *power_kobj;
668668
static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
669669
char *buf)
670670
{
671-
char *s = buf;
671+
ssize_t count = 0;
672672
#ifdef CONFIG_SUSPEND
673673
suspend_state_t i;
674674

675675
for (i = PM_SUSPEND_MIN; i < PM_SUSPEND_MAX; i++)
676676
if (pm_states[i])
677-
s += sprintf(s,"%s ", pm_states[i]);
677+
count += sysfs_emit_at(buf, count, "%s ", pm_states[i]);
678678

679679
#endif
680680
if (hibernation_available())
681-
s += sprintf(s, "disk ");
682-
if (s != buf)
683-
/* convert the last space to a newline */
684-
*(s-1) = '\n';
685-
return (s - buf);
681+
count += sysfs_emit_at(buf, count, "disk ");
682+
683+
/* Convert the last space to a newline if needed. */
684+
if (count > 0)
685+
buf[count - 1] = '\n';
686+
687+
return count;
686688
}
687689

688690
static suspend_state_t decode_state(const char *buf, size_t n)
@@ -782,7 +784,7 @@ static ssize_t wakeup_count_show(struct kobject *kobj,
782784
unsigned int val;
783785

784786
return pm_get_wakeup_count(&val, true) ?
785-
sprintf(buf, "%u\n", val) : -EINTR;
787+
sysfs_emit(buf, "%u\n", val) : -EINTR;
786788
}
787789

788790
static ssize_t wakeup_count_store(struct kobject *kobj,
@@ -824,17 +826,17 @@ static ssize_t autosleep_show(struct kobject *kobj,
824826
suspend_state_t state = pm_autosleep_state();
825827

826828
if (state == PM_SUSPEND_ON)
827-
return sprintf(buf, "off\n");
829+
return sysfs_emit(buf, "off\n");
828830

829831
#ifdef CONFIG_SUSPEND
830832
if (state < PM_SUSPEND_MAX)
831-
return sprintf(buf, "%s\n", pm_states[state] ?
833+
return sysfs_emit(buf, "%s\n", pm_states[state] ?
832834
pm_states[state] : "error");
833835
#endif
834836
#ifdef CONFIG_HIBERNATION
835-
return sprintf(buf, "disk\n");
837+
return sysfs_emit(buf, "disk\n");
836838
#else
837-
return sprintf(buf, "error");
839+
return sysfs_emit(buf, "error\n");
838840
#endif
839841
}
840842

@@ -903,7 +905,7 @@ int pm_trace_enabled;
903905
static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
904906
char *buf)
905907
{
906-
return sprintf(buf, "%d\n", pm_trace_enabled);
908+
return sysfs_emit(buf, "%d\n", pm_trace_enabled);
907909
}
908910

909911
static ssize_t
@@ -940,7 +942,7 @@ power_attr_ro(pm_trace_dev_match);
940942
static ssize_t pm_freeze_timeout_show(struct kobject *kobj,
941943
struct kobj_attribute *attr, char *buf)
942944
{
943-
return sprintf(buf, "%u\n", freeze_timeout_msecs);
945+
return sysfs_emit(buf, "%u\n", freeze_timeout_msecs);
944946
}
945947

946948
static ssize_t pm_freeze_timeout_store(struct kobject *kobj,

0 commit comments

Comments
 (0)