2024-03-14 09:52:37

by Zhijian Li (Fujitsu)

[permalink] [raw]
Subject: [PATCH] s390/cio: Convert sprintf/snprintf to sysfs_emit

Per filesystems/sysfs.rst, show() should only use sysfs_emit()
or sysfs_emit_at() when formatting the value to be returned to user space.

coccinelle complains that there are still a couple of functions that use
snprintf(). Convert them to sysfs_emit().

sprintf() will be converted as weel if they have.

Generally, this patch is generated by
make coccicheck M=<path/to/file> MODE=patch \
COCCI=scripts/coccinelle/api/device_attr_show.cocci

No functional change intended

CC: Vineeth Vijayan <[email protected]>
CC: Peter Oberparleiter <[email protected]>
CC: Heiko Carstens <[email protected]>
CC: Vasily Gorbik <[email protected]>
CC: Alexander Gordeev <[email protected]>
CC: Christian Borntraeger <[email protected]>
CC: Sven Schnelle <[email protected]>
CC: [email protected]
Signed-off-by: Li Zhijian <[email protected]>
---
This is a part of the work "Fix coccicheck device_attr_show warnings"[1]
Split them per subsystem so that the maintainer can review it easily
[1] https://lore.kernel.org/lkml/[email protected]/
---
drivers/s390/cio/css.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/s390/cio/css.c b/drivers/s390/cio/css.c
index 094431a62ad5..4fec251ec35d 100644
--- a/drivers/s390/cio/css.c
+++ b/drivers/s390/cio/css.c
@@ -309,7 +309,7 @@ static ssize_t type_show(struct device *dev, struct device_attribute *attr,
{
struct subchannel *sch = to_subchannel(dev);

- return sprintf(buf, "%01x\n", sch->st);
+ return sysfs_emit(buf, "%01x\n", sch->st);
}

static DEVICE_ATTR_RO(type);
@@ -319,7 +319,7 @@ static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
{
struct subchannel *sch = to_subchannel(dev);

- return sprintf(buf, "css:t%01X\n", sch->st);
+ return sysfs_emit(buf, "css:t%01X\n", sch->st);
}

static DEVICE_ATTR_RO(modalias);
@@ -345,7 +345,7 @@ static ssize_t driver_override_show(struct device *dev,
ssize_t len;

device_lock(dev);
- len = snprintf(buf, PAGE_SIZE, "%s\n", sch->driver_override);
+ len = sysfs_emit(buf, "%s\n", sch->driver_override);
device_unlock(dev);
return len;
}
@@ -396,7 +396,7 @@ static ssize_t pimpampom_show(struct device *dev,
struct subchannel *sch = to_subchannel(dev);
struct pmcw *pmcw = &sch->schib.pmcw;

- return sprintf(buf, "%02x %02x %02x\n",
+ return sysfs_emit(buf, "%02x %02x %02x\n",
pmcw->pim, pmcw->pam, pmcw->pom);
}
static DEVICE_ATTR_RO(pimpampom);
@@ -881,7 +881,7 @@ static ssize_t real_cssid_show(struct device *dev, struct device_attribute *a,
if (!css->id_valid)
return -EINVAL;

- return sprintf(buf, "%x\n", css->cssid);
+ return sysfs_emit(buf, "%x\n", css->cssid);
}
static DEVICE_ATTR_RO(real_cssid);

@@ -904,7 +904,7 @@ static ssize_t cm_enable_show(struct device *dev, struct device_attribute *a,
int ret;

mutex_lock(&css->mutex);
- ret = sprintf(buf, "%x\n", css->cm_enabled);
+ ret = sysfs_emit(buf, "%x\n", css->cm_enabled);
mutex_unlock(&css->mutex);
return ret;
}
--
2.29.2



2024-03-19 11:18:31

by Heiko Carstens

[permalink] [raw]
Subject: Re: [PATCH] s390/cio: Convert sprintf/snprintf to sysfs_emit

On Thu, Mar 14, 2024 at 05:52:09PM +0800, Li Zhijian wrote:
> Per filesystems/sysfs.rst, show() should only use sysfs_emit()
> or sysfs_emit_at() when formatting the value to be returned to user space.
>
> coccinelle complains that there are still a couple of functions that use
> snprintf(). Convert them to sysfs_emit().
>
> sprintf() will be converted as weel if they have.
>
> Generally, this patch is generated by
> make coccicheck M=<path/to/file> MODE=patch \
> COCCI=scripts/coccinelle/api/device_attr_show.cocci
>
> No functional change intended
>
> CC: Vineeth Vijayan <[email protected]>
> CC: Peter Oberparleiter <[email protected]>
> CC: Heiko Carstens <[email protected]>
> CC: Vasily Gorbik <[email protected]>
> CC: Alexander Gordeev <[email protected]>
> CC: Christian Borntraeger <[email protected]>
> CC: Sven Schnelle <[email protected]>
> CC: [email protected]
> Signed-off-by: Li Zhijian <[email protected]>
> ---
> This is a part of the work "Fix coccicheck device_attr_show warnings"[1]
> Split them per subsystem so that the maintainer can review it easily
> [1] https://lore.kernel.org/lkml/[email protected]/
> ---
> drivers/s390/cio/css.c | 12 ++++++------
> 1 file changed, 6 insertions(+), 6 deletions(-)

Applied, but...

> @@ -396,7 +396,7 @@ static ssize_t pimpampom_show(struct device *dev,
> struct subchannel *sch = to_subchannel(dev);
> struct pmcw *pmcw = &sch->schib.pmcw;
>
> - return sprintf(buf, "%02x %02x %02x\n",
> + return sysfs_emit(buf, "%02x %02x %02x\n",
> pmcw->pim, pmcw->pam, pmcw->pom);

..please make sure to fix such whitespace / indentation errors if you
plan to provide more patches. I fixed it this time.

Thanks!