This focuses on the abused cases in macros. 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() and scnprintf() will be converted as well 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: "James E.J. Bottomley" <jejb@xxxxxxxxxxxxx> CC: "Martin K. Petersen" <martin.petersen@xxxxxxxxxx> CC: linux-scsi@xxxxxxxxxxxxxxx Signed-off-by: Li Zhijian <lizhijian@xxxxxxxxxxx> --- 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/20240116041129.3937800-1-lizhijian@xxxxxxxxxxx/ --- drivers/scsi/scsi_transport_sas.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/drivers/scsi/scsi_transport_sas.c b/drivers/scsi/scsi_transport_sas.c index baf2aaf361d2..21d3b13c543a 100644 --- a/drivers/scsi/scsi_transport_sas.c +++ b/drivers/scsi/scsi_transport_sas.c @@ -427,7 +427,7 @@ show_sas_phy_##name(struct device *dev, \ { \ struct sas_phy *phy = transport_class_to_phy(dev); \ \ - return snprintf(buf, 20, format_string, cast phy->field); \ + return sysfs_emit(buf, format_string, cast phy->field); \ } #define sas_phy_simple_attr(field, name, format_string, type) \ @@ -442,7 +442,7 @@ show_sas_phy_##name(struct device *dev, \ struct sas_phy *phy = transport_class_to_phy(dev); \ \ if (!phy->field) \ - return snprintf(buf, 20, "none\n"); \ + return sysfs_emit(buf, "none\n"); \ return get_sas_protocol_names(phy->field, buf); \ } @@ -507,7 +507,7 @@ show_sas_phy_##field(struct device *dev, \ error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0; \ if (error) \ return error; \ - return snprintf(buf, 20, "%u\n", phy->field); \ + return sysfs_emit(buf, "%u\n", phy->field); \ } #define sas_phy_linkerror_attr(field) \ @@ -798,7 +798,7 @@ show_sas_port_##name(struct device *dev, \ { \ struct sas_port *port = transport_class_to_sas_port(dev); \ \ - return snprintf(buf, 20, format_string, cast port->field); \ + return sysfs_emit(buf, format_string, cast port->field); \ } #define sas_port_simple_attr(field, name, format_string, type) \ @@ -1145,7 +1145,7 @@ show_sas_rphy_##name(struct device *dev, \ { \ struct sas_rphy *rphy = transport_class_to_rphy(dev); \ \ - return snprintf(buf, 20, format_string, cast rphy->field); \ + return sysfs_emit(buf, format_string, cast rphy->field); \ } #define sas_rphy_simple_attr(field, name, format_string, type) \ @@ -1161,7 +1161,7 @@ show_sas_rphy_##name(struct device *dev, \ struct sas_rphy *rphy = transport_class_to_rphy(dev); \ \ if (!rphy->field) \ - return snprintf(buf, 20, "none\n"); \ + return sysfs_emit(buf, "none\n"); \ return get_sas_protocol_names(rphy->field, buf); \ } @@ -1280,7 +1280,7 @@ show_sas_end_dev_##name(struct device *dev, \ struct sas_rphy *rphy = transport_class_to_rphy(dev); \ struct sas_end_device *rdev = rphy_to_end_device(rphy); \ \ - return snprintf(buf, 20, format_string, cast rdev->field); \ + return sysfs_emit(buf, format_string, cast rdev->field); \ } #define sas_end_dev_simple_attr(field, name, format_string, type) \ @@ -1309,7 +1309,7 @@ show_sas_expander_##name(struct device *dev, \ struct sas_rphy *rphy = transport_class_to_rphy(dev); \ struct sas_expander_device *edev = rphy_to_expander_device(rphy); \ \ - return snprintf(buf, 20, format_string, cast edev->field); \ + return sysfs_emit(buf, format_string, cast edev->field); \ } #define sas_expander_simple_attr(field, name, format_string, type) \ -- 2.29.2