This patch replaces the existing sprintf/strcat/strlen patterns in the four dm-sysfs show() callbacks with the sysfs_emit() API, as recommended by Documentation/filesystems/sysfs.rst. The change has no functional impact. The output format remains exactly the same—but it:
- Eliminates manual buffer-length calculations and strcat calls - Protects against potential PAGE_SIZE overruns - Makes intent clearer and aligns with current kernel best practices Signed-off-by: Miguel García <miguelgarciarom...@gmail.com> --- Tested-on: • Built-in dm_mod and loop drivers in a custom vmlinuz via virtme-ng • BusyBox-based initramfs in QEMU (x86_64, 2 GB RAM, 2 vCPU) Test procedure: 1. In the guest shell: # mount -t tmpfs tmpfs /tmp # dd if=/dev/zero of=/tmp/backing.img bs=1M count=8 # losetup /dev/loop0 /tmp/backing.img # sectors=$((8*1024*1024/512)) # echo "0 $sectors linear /dev/loop0 0" | dmsetup create testdm --uuid test-uuid-123 2. Verify each sysfs attribute still matches the original output: # cat /sys/block/dm-*/dm/name → testdm # cat /sys/block/dm-*/dm/uuid → test-uuid-123 # cat /sys/block/dm-*/dm/suspended → 0 # cat /sys/block/dm-*/dm/use_blk_mq → 1 3. Remove and clean up: # dmsetup remove testdm # losetup -d /dev/loop0 Both before and after the patch the attribute contents and formatting are identical, confirming no functional change. drivers/md/dm-sysfs.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/drivers/md/dm-sysfs.c b/drivers/md/dm-sysfs.c index bfaef27ca79f..fed9751970a0 100644 --- a/drivers/md/dm-sysfs.c +++ b/drivers/md/dm-sysfs.c @@ -68,35 +68,33 @@ static ssize_t dm_attr_store(struct kobject *kobj, struct attribute *attr, static ssize_t dm_attr_name_show(struct mapped_device *md, char *buf) { - if (dm_copy_name_and_uuid(md, buf, NULL)) + char name[DM_NAME_LEN]; + + if (dm_copy_name_and_uuid(md, name, NULL)) return -EIO; - strcat(buf, "\n"); - return strlen(buf); + return sysfs_emit(buf, "%s\n", name); } static ssize_t dm_attr_uuid_show(struct mapped_device *md, char *buf) { - if (dm_copy_name_and_uuid(md, NULL, buf)) + char uuid[DM_UUID_LEN]; + + if (dm_copy_name_and_uuid(md, NULL, uuid)) return -EIO; - strcat(buf, "\n"); - return strlen(buf); + return sysfs_emit(buf, "%s\n", uuid); } static ssize_t dm_attr_suspended_show(struct mapped_device *md, char *buf) { - sprintf(buf, "%d\n", dm_suspended_md(md)); - - return strlen(buf); + return sysfs_emit(buf, "%d\n", dm_suspended_md(md)); } static ssize_t dm_attr_use_blk_mq_show(struct mapped_device *md, char *buf) { /* Purely for userspace compatibility */ - sprintf(buf, "%d\n", true); - - return strlen(buf); + return sysfs_emit(buf, "%d\n", true); } static DM_ATTR_RO(name); -- 2.34.1