From: "Miguel García" <miguelgarciaroman8@gmail.com>
To: dm-devel@lists.linux.dev
Cc: agk@redhat.com, snitzer@kernel.org, mpatocka@redhat.com,
linux-kernel@vger.kernel.org,
"Miguel García" <miguelgarciaroman8@gmail.com>
Subject: [PATCH] dm: sysfs: use sysfs_emit() in dm_attr_*_show()
Date: Fri, 1 Aug 2025 15:19:07 +0200 [thread overview]
Message-ID: <20250801131907.371700-1-miguelgarciaroman8@gmail.com> (raw)
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 <miguelgarciaroman8@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
next reply other threads:[~2025-08-01 13:20 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-01 13:19 Miguel García [this message]
2025-08-01 16:20 ` Bart Van Assche
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250801131907.371700-1-miguelgarciaroman8@gmail.com \
--to=miguelgarciaroman8@gmail.com \
--cc=agk@redhat.com \
--cc=dm-devel@lists.linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=mpatocka@redhat.com \
--cc=snitzer@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®