]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
PM: hibernate: Use sysfs_emit() and sysfs_emit_at() in "show" functions
authorXueqin Luo <luoxueqin@kylinos.cn>
Thu, 1 Aug 2024 08:31:55 +0000 (16:31 +0800)
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>
Fri, 2 Aug 2024 14:03:51 +0000 (16:03 +0200)
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 <luoxueqin@kylinos.cn>
Link: https://patch.msgid.link/20240801083156.2513508-2-luoxueqin@kylinos.cn
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
kernel/power/hibernate.c

index 0a213f69a9e4ac0be17020ffb18d7456b7105762..e35829d360390f97b75f4a3a2ddb067b0b26b7c1 100644 (file)
@@ -1123,11 +1123,11 @@ static const char * const hibernation_modes[] = {
 static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
                         char *buf)
 {
+       ssize_t count = 0;
        int i;
-       char *start = buf;
 
        if (!hibernation_available())
-               return sprintf(buf, "[disabled]\n");
+               return sysfs_emit(buf, "[disabled]\n");
 
        for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
                if (!hibernation_modes[i])
@@ -1147,12 +1147,16 @@ static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
                        continue;
                }
                if (i == hibernation_mode)
-                       buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
+                       count += sysfs_emit_at(buf, count, "[%s] ", hibernation_modes[i]);
                else
-                       buf += sprintf(buf, "%s ", hibernation_modes[i]);
+                       count += sysfs_emit_at(buf, count, "%s ", hibernation_modes[i]);
        }
-       buf += sprintf(buf, "\n");
-       return buf-start;
+
+       /* Convert the last space to a newline if needed. */
+       if (count > 0)
+               buf[count - 1] = '\n';
+
+       return count;
 }
 
 static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
@@ -1210,8 +1214,8 @@ power_attr(disk);
 static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
                           char *buf)
 {
-       return sprintf(buf, "%d:%d\n", MAJOR(swsusp_resume_device),
-                      MINOR(swsusp_resume_device));
+       return sysfs_emit(buf, "%d:%d\n", MAJOR(swsusp_resume_device),
+                         MINOR(swsusp_resume_device));
 }
 
 static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
@@ -1270,7 +1274,7 @@ power_attr(resume);
 static ssize_t resume_offset_show(struct kobject *kobj,
                                  struct kobj_attribute *attr, char *buf)
 {
-       return sprintf(buf, "%llu\n", (unsigned long long)swsusp_resume_block);
+       return sysfs_emit(buf, "%llu\n", (unsigned long long)swsusp_resume_block);
 }
 
 static ssize_t resume_offset_store(struct kobject *kobj,
@@ -1293,7 +1297,7 @@ power_attr(resume_offset);
 static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
                               char *buf)
 {
-       return sprintf(buf, "%lu\n", image_size);
+       return sysfs_emit(buf, "%lu\n", image_size);
 }
 
 static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
@@ -1314,7 +1318,7 @@ power_attr(image_size);
 static ssize_t reserved_size_show(struct kobject *kobj,
                                  struct kobj_attribute *attr, char *buf)
 {
-       return sprintf(buf, "%lu\n", reserved_size);
+       return sysfs_emit(buf, "%lu\n", reserved_size);
 }
 
 static ssize_t reserved_size_store(struct kobject *kobj,