linux-rdma.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH-next 0/4] RDMA: sprintf to sysfs_emit conversions
@ 2020-10-08  2:36 Joe Perches
  2020-10-08  2:36 ` [PATCH 1/4] RDMA: Convert sysfs device * show functions to use sysfs_emit() Joe Perches
                   ` (6 more replies)
  0 siblings, 7 replies; 26+ messages in thread
From: Joe Perches @ 2020-10-08  2:36 UTC (permalink / raw)
  To: Doug Ledford, Jason Gunthorpe, linux-rdma, target-devel; +Cc: linux-kernel

A recent commit added a sysfs_emit and sysfs_emit_at to allow various
sysfs show functions to ensure that the PAGE_SIZE buffer argument is
never overrun and always NUL terminated.

Convert the RDMA/InfiniBand subsystem to use these new functions.

The first 2 patches exclusively used coccinelle to convert uses.
The third and fourth patches were done manually.

Compiled allyesconfig and defconfig with all infiniband options selected
no warnings, but untested, no hardward

Overall object size is reduced

total size: allyesconfig x86-64
new: 8364003	1680968	 131520	10176491 9b47eb	(TOTALS)
old: 8365883	1681032	 131520	10178435 9b4f83	(TOTALS)

total size: defconfig x86-64 with all infiniband selected
new; 1359153	 131228	   1910  1492291 16c543	(TOTALS)
old: 1359422	 131228	   1910  1492560 16c650	(TOTALS)

Joe Perches (4):
  RDMA: Convert sysfs device * show functions to use sysfs_emit()
  RDMA: Convert sysfs kobject * show functions to use sysfs_emit()
  RDMA: manual changes for sysfs_emit and neatening
  RDMA: Convert various random sprintf sysfs _show uses to sysfs_emit

 drivers/infiniband/core/cm.c                  |   4 +-
 drivers/infiniband/core/cma_configfs.c        |   4 +-
 drivers/infiniband/core/sysfs.c               | 155 ++++++++++--------
 drivers/infiniband/core/ucma.c                |   2 +-
 drivers/infiniband/core/user_mad.c            |   6 +-
 drivers/infiniband/core/uverbs_main.c         |   4 +-
 drivers/infiniband/hw/bnxt_re/main.c          |   4 +-
 drivers/infiniband/hw/cxgb4/provider.c        |  13 +-
 drivers/infiniband/hw/hfi1/sysfs.c            |  62 ++++---
 drivers/infiniband/hw/i40iw/i40iw_verbs.c     |   6 +-
 drivers/infiniband/hw/mlx4/main.c             |   9 +-
 drivers/infiniband/hw/mlx4/mcg.c              |  82 +++++----
 drivers/infiniband/hw/mlx4/sysfs.c            |  70 ++++----
 drivers/infiniband/hw/mlx5/main.c             |  13 +-
 drivers/infiniband/hw/mthca/mthca_provider.c  |  33 ++--
 drivers/infiniband/hw/ocrdma/ocrdma_main.c    |   4 +-
 drivers/infiniband/hw/qedr/main.c             |  10 +-
 drivers/infiniband/hw/qib/qib_sysfs.c         |  91 +++++-----
 drivers/infiniband/hw/usnic/usnic_ib_sysfs.c  | 104 +++++-------
 .../infiniband/hw/vmw_pvrdma/pvrdma_main.c    |   6 +-
 drivers/infiniband/sw/rxe/rxe_verbs.c         |   2 +-
 drivers/infiniband/ulp/ipoib/ipoib_cm.c       |   4 +-
 drivers/infiniband/ulp/ipoib/ipoib_main.c     |   7 +-
 drivers/infiniband/ulp/ipoib/ipoib_vlan.c     |   2 +-
 drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c  |  60 +++----
 drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c  |  20 +--
 drivers/infiniband/ulp/srp/ib_srp.c           |  49 +++---
 drivers/infiniband/ulp/srpt/ib_srpt.c         |  14 +-
 28 files changed, 423 insertions(+), 417 deletions(-)

-- 
2.26.0


^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH 1/4] RDMA: Convert sysfs device * show functions to use sysfs_emit()
  2020-10-08  2:36 [PATCH-next 0/4] RDMA: sprintf to sysfs_emit conversions Joe Perches
@ 2020-10-08  2:36 ` Joe Perches
  2020-10-09 14:07   ` Jason Gunthorpe
  2020-10-08  2:36 ` [PATCH 2/4] RDMA: Convert sysfs kobject " Joe Perches
                   ` (5 subsequent siblings)
  6 siblings, 1 reply; 26+ messages in thread
From: Joe Perches @ 2020-10-08  2:36 UTC (permalink / raw)
  To: Doug Ledford, Jason Gunthorpe, Selvin Xavier, Devesh Sharma,
	Somnath Kotur, Sriharsha Basavapatna, Naresh Kumar PBS,
	Potnuri Bharat Teja, Mike Marciniszyn, Dennis Dalessandro,
	Faisal Latif, Shiraz Saleem, Yishai Hadas, Leon Romanovsky,
	Michal Kalderon, Ariel Elior, Christian Benvenuti,
	Nelson Escobar, Parvi Kaustubhi, Adit Ranadive,
	VMware PV-Drivers, Zhu Yanjun, Danil Kipnis, Jack Wang,
	Bart Van Assche
  Cc: linux-rdma

Done with cocci script:

@@
identifier d_show;
identifier dev, attr, buf;
@@

ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	<...
	return
-	sprintf(buf,
+	sysfs_emit(buf,
	...);
	...>
}

@@
identifier d_show;
identifier dev, attr, buf;
@@

ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	<...
	return
-	snprintf(buf, PAGE_SIZE,
+	sysfs_emit(buf,
	...);
	...>
}

@@
identifier d_show;
identifier dev, attr, buf;
@@

ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	<...
	return
-	scnprintf(buf, PAGE_SIZE,
+	sysfs_emit(buf,
	...);
	...>
}

@@
identifier d_show;
identifier dev, attr, buf;
expression chr;
@@

ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	<...
	return
-	strcpy(buf, chr);
+	sysfs_emit(buf, chr);
	...>
}

@@
identifier d_show;
identifier dev, attr, buf;
identifier len;
@@

ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	<...
	len =
-	sprintf(buf,
+	sysfs_emit(buf,
	...);
	...>
	return len;
}

@@
identifier d_show;
identifier dev, attr, buf;
identifier len;
@@

ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	<...
	len =
-	snprintf(buf, PAGE_SIZE,
+	sysfs_emit(buf,
	...);
	...>
	return len;
}

@@
identifier d_show;
identifier dev, attr, buf;
identifier len;
@@

ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	<...
	len =
-	scnprintf(buf, PAGE_SIZE,
+	sysfs_emit(buf,
	...);
	...>
	return len;
}

@@
identifier d_show;
identifier dev, attr, buf;
identifier len;
@@

ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	<...
-	len += scnprintf(buf + len, PAGE_SIZE - len,
+	len += sysfs_emit_at(buf, len,
	...);
	...>
	return len;
}

@@
identifier d_show;
identifier dev, attr, buf;
expression chr;
@@

ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	...
-	strcpy(buf, chr);
-	return strlen(buf);
+	return sysfs_emit(buf, chr);
}

Signed-off-by: Joe Perches <joe@perches.com>
---
 drivers/infiniband/core/sysfs.c               | 47 +++++++++++--------
 drivers/infiniband/core/ucma.c                |  2 +-
 drivers/infiniband/core/user_mad.c            |  4 +-
 drivers/infiniband/core/uverbs_main.c         |  4 +-
 drivers/infiniband/hw/bnxt_re/main.c          |  4 +-
 drivers/infiniband/hw/cxgb4/provider.c        | 10 ++--
 drivers/infiniband/hw/hfi1/sysfs.c            | 16 +++----
 drivers/infiniband/hw/i40iw/i40iw_verbs.c     |  6 +--
 drivers/infiniband/hw/mlx4/main.c             |  8 ++--
 drivers/infiniband/hw/mlx4/sysfs.c            | 30 ++++++------
 drivers/infiniband/hw/mlx5/main.c             | 13 ++---
 drivers/infiniband/hw/mthca/mthca_provider.c  | 14 +++---
 drivers/infiniband/hw/ocrdma/ocrdma_main.c    |  4 +-
 drivers/infiniband/hw/qedr/main.c             | 10 ++--
 drivers/infiniband/hw/qib/qib_sysfs.c         | 30 ++++++------
 drivers/infiniband/hw/usnic/usnic_ib_sysfs.c  | 16 +++----
 .../infiniband/hw/vmw_pvrdma/pvrdma_main.c    |  6 +--
 drivers/infiniband/sw/rxe/rxe_verbs.c         |  2 +-
 drivers/infiniband/ulp/ipoib/ipoib_cm.c       |  4 +-
 drivers/infiniband/ulp/ipoib/ipoib_main.c     |  7 +--
 drivers/infiniband/ulp/ipoib/ipoib_vlan.c     |  2 +-
 drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c  | 17 ++++---
 drivers/infiniband/ulp/srp/ib_srp.c           | 41 ++++++++--------
 23 files changed, 156 insertions(+), 141 deletions(-)

diff --git a/drivers/infiniband/core/sysfs.c b/drivers/infiniband/core/sysfs.c
index 914cddea525d..79327200da70 100644
--- a/drivers/infiniband/core/sysfs.c
+++ b/drivers/infiniband/core/sysfs.c
@@ -1230,14 +1230,23 @@ static ssize_t node_type_show(struct device *device,
 	struct ib_device *dev = rdma_device_to_ibdev(device);
 
 	switch (dev->node_type) {
-	case RDMA_NODE_IB_CA:	  return sprintf(buf, "%d: CA\n", dev->node_type);
-	case RDMA_NODE_RNIC:	  return sprintf(buf, "%d: RNIC\n", dev->node_type);
-	case RDMA_NODE_USNIC:	  return sprintf(buf, "%d: usNIC\n", dev->node_type);
-	case RDMA_NODE_USNIC_UDP: return sprintf(buf, "%d: usNIC UDP\n", dev->node_type);
-	case RDMA_NODE_UNSPECIFIED: return sprintf(buf, "%d: unspecified\n", dev->node_type);
-	case RDMA_NODE_IB_SWITCH: return sprintf(buf, "%d: switch\n", dev->node_type);
-	case RDMA_NODE_IB_ROUTER: return sprintf(buf, "%d: router\n", dev->node_type);
-	default:		  return sprintf(buf, "%d: <unknown>\n", dev->node_type);
+	case RDMA_NODE_IB_CA:	  return sysfs_emit(buf, "%d: CA\n",
+							 dev->node_type);
+	case RDMA_NODE_RNIC:	  return sysfs_emit(buf, "%d: RNIC\n",
+							dev->node_type);
+	case RDMA_NODE_USNIC:	  return sysfs_emit(buf, "%d: usNIC\n",
+							 dev->node_type);
+	case RDMA_NODE_USNIC_UDP: return sysfs_emit(buf, "%d: usNIC UDP\n",
+						    dev->node_type);
+	case RDMA_NODE_UNSPECIFIED: return sysfs_emit(buf,
+						      "%d: unspecified\n",
+						      dev->node_type);
+	case RDMA_NODE_IB_SWITCH: return sysfs_emit(buf, "%d: switch\n",
+						    dev->node_type);
+	case RDMA_NODE_IB_ROUTER: return sysfs_emit(buf, "%d: router\n",
+						    dev->node_type);
+	default:		  return sysfs_emit(buf, "%d: <unknown>\n",
+						    dev->node_type);
 	}
 }
 static DEVICE_ATTR_RO(node_type);
@@ -1247,11 +1256,11 @@ static ssize_t sys_image_guid_show(struct device *device,
 {
 	struct ib_device *dev = rdma_device_to_ibdev(device);
 
-	return sprintf(buf, "%04x:%04x:%04x:%04x\n",
-		       be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[0]),
-		       be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[1]),
-		       be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[2]),
-		       be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[3]));
+	return sysfs_emit(buf, "%04x:%04x:%04x:%04x\n",
+			  be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[0]),
+			  be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[1]),
+			  be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[2]),
+			  be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[3]));
 }
 static DEVICE_ATTR_RO(sys_image_guid);
 
@@ -1260,11 +1269,11 @@ static ssize_t node_guid_show(struct device *device,
 {
 	struct ib_device *dev = rdma_device_to_ibdev(device);
 
-	return sprintf(buf, "%04x:%04x:%04x:%04x\n",
-		       be16_to_cpu(((__be16 *) &dev->node_guid)[0]),
-		       be16_to_cpu(((__be16 *) &dev->node_guid)[1]),
-		       be16_to_cpu(((__be16 *) &dev->node_guid)[2]),
-		       be16_to_cpu(((__be16 *) &dev->node_guid)[3]));
+	return sysfs_emit(buf, "%04x:%04x:%04x:%04x\n",
+			  be16_to_cpu(((__be16 *) &dev->node_guid)[0]),
+			  be16_to_cpu(((__be16 *) &dev->node_guid)[1]),
+			  be16_to_cpu(((__be16 *) &dev->node_guid)[2]),
+			  be16_to_cpu(((__be16 *) &dev->node_guid)[3]));
 }
 static DEVICE_ATTR_RO(node_guid);
 
@@ -1273,7 +1282,7 @@ static ssize_t node_desc_show(struct device *device,
 {
 	struct ib_device *dev = rdma_device_to_ibdev(device);
 
-	return sprintf(buf, "%.64s\n", dev->node_desc);
+	return sysfs_emit(buf, "%.64s\n", dev->node_desc);
 }
 
 static ssize_t node_desc_store(struct device *device,
diff --git a/drivers/infiniband/core/ucma.c b/drivers/infiniband/core/ucma.c
index 08a628246bd6..4368d4e4d6c3 100644
--- a/drivers/infiniband/core/ucma.c
+++ b/drivers/infiniband/core/ucma.c
@@ -1824,7 +1824,7 @@ static ssize_t show_abi_version(struct device *dev,
 				struct device_attribute *attr,
 				char *buf)
 {
-	return sprintf(buf, "%d\n", RDMA_USER_CM_ABI_VERSION);
+	return sysfs_emit(buf, "%d\n", RDMA_USER_CM_ABI_VERSION);
 }
 static DEVICE_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
 
diff --git a/drivers/infiniband/core/user_mad.c b/drivers/infiniband/core/user_mad.c
index b0d0b522cc76..7e759f5b2a75 100644
--- a/drivers/infiniband/core/user_mad.c
+++ b/drivers/infiniband/core/user_mad.c
@@ -1191,7 +1191,7 @@ static ssize_t ibdev_show(struct device *dev, struct device_attribute *attr,
 	if (!port)
 		return -ENODEV;
 
-	return sprintf(buf, "%s\n", dev_name(&port->ib_dev->dev));
+	return sysfs_emit(buf, "%s\n", dev_name(&port->ib_dev->dev));
 }
 static DEVICE_ATTR_RO(ibdev);
 
@@ -1203,7 +1203,7 @@ static ssize_t port_show(struct device *dev, struct device_attribute *attr,
 	if (!port)
 		return -ENODEV;
 
-	return sprintf(buf, "%d\n", port->port_num);
+	return sysfs_emit(buf, "%d\n", port->port_num);
 }
 static DEVICE_ATTR_RO(port);
 
diff --git a/drivers/infiniband/core/uverbs_main.c b/drivers/infiniband/core/uverbs_main.c
index 4bb7c642f80c..f173ecd102dc 100644
--- a/drivers/infiniband/core/uverbs_main.c
+++ b/drivers/infiniband/core/uverbs_main.c
@@ -1046,7 +1046,7 @@ static ssize_t ibdev_show(struct device *device, struct device_attribute *attr,
 	srcu_key = srcu_read_lock(&dev->disassociate_srcu);
 	ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
 	if (ib_dev)
-		ret = sprintf(buf, "%s\n", dev_name(&ib_dev->dev));
+		ret = sysfs_emit(buf, "%s\n", dev_name(&ib_dev->dev));
 	srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
 
 	return ret;
@@ -1065,7 +1065,7 @@ static ssize_t abi_version_show(struct device *device,
 	srcu_key = srcu_read_lock(&dev->disassociate_srcu);
 	ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
 	if (ib_dev)
-		ret = sprintf(buf, "%u\n", ib_dev->ops.uverbs_abi_ver);
+		ret = sysfs_emit(buf, "%u\n", ib_dev->ops.uverbs_abi_ver);
 	srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
 
 	return ret;
diff --git a/drivers/infiniband/hw/bnxt_re/main.c b/drivers/infiniband/hw/bnxt_re/main.c
index 53aee5a42ab8..202a0e229d44 100644
--- a/drivers/infiniband/hw/bnxt_re/main.c
+++ b/drivers/infiniband/hw/bnxt_re/main.c
@@ -608,7 +608,7 @@ static ssize_t hw_rev_show(struct device *device, struct device_attribute *attr,
 	struct bnxt_re_dev *rdev =
 		rdma_device_to_drv_device(device, struct bnxt_re_dev, ibdev);
 
-	return scnprintf(buf, PAGE_SIZE, "0x%x\n", rdev->en_dev->pdev->vendor);
+	return sysfs_emit(buf, "0x%x\n", rdev->en_dev->pdev->vendor);
 }
 static DEVICE_ATTR_RO(hw_rev);
 
@@ -618,7 +618,7 @@ static ssize_t hca_type_show(struct device *device,
 	struct bnxt_re_dev *rdev =
 		rdma_device_to_drv_device(device, struct bnxt_re_dev, ibdev);
 
-	return scnprintf(buf, PAGE_SIZE, "%s\n", rdev->ibdev.node_desc);
+	return sysfs_emit(buf, "%s\n", rdev->ibdev.node_desc);
 }
 static DEVICE_ATTR_RO(hca_type);
 
diff --git a/drivers/infiniband/hw/cxgb4/provider.c b/drivers/infiniband/hw/cxgb4/provider.c
index 4b76f2f3f4e4..b312a7ec40bf 100644
--- a/drivers/infiniband/hw/cxgb4/provider.c
+++ b/drivers/infiniband/hw/cxgb4/provider.c
@@ -322,8 +322,8 @@ static ssize_t hw_rev_show(struct device *dev,
 			rdma_device_to_drv_device(dev, struct c4iw_dev, ibdev);
 
 	pr_debug("dev 0x%p\n", dev);
-	return sprintf(buf, "%d\n",
-		       CHELSIO_CHIP_RELEASE(c4iw_dev->rdev.lldi.adapter_type));
+	return sysfs_emit(buf, "%d\n",
+			  CHELSIO_CHIP_RELEASE(c4iw_dev->rdev.lldi.adapter_type));
 }
 static DEVICE_ATTR_RO(hw_rev);
 
@@ -337,7 +337,7 @@ static ssize_t hca_type_show(struct device *dev,
 
 	pr_debug("dev 0x%p\n", dev);
 	lldev->ethtool_ops->get_drvinfo(lldev, &info);
-	return sprintf(buf, "%s\n", info.driver);
+	return sysfs_emit(buf, "%s\n", info.driver);
 }
 static DEVICE_ATTR_RO(hca_type);
 
@@ -348,8 +348,8 @@ static ssize_t board_id_show(struct device *dev, struct device_attribute *attr,
 			rdma_device_to_drv_device(dev, struct c4iw_dev, ibdev);
 
 	pr_debug("dev 0x%p\n", dev);
-	return sprintf(buf, "%x.%x\n", c4iw_dev->rdev.lldi.pdev->vendor,
-		       c4iw_dev->rdev.lldi.pdev->device);
+	return sysfs_emit(buf, "%x.%x\n", c4iw_dev->rdev.lldi.pdev->vendor,
+			  c4iw_dev->rdev.lldi.pdev->device);
 }
 static DEVICE_ATTR_RO(board_id);
 
diff --git a/drivers/infiniband/hw/hfi1/sysfs.c b/drivers/infiniband/hw/hfi1/sysfs.c
index 074ec71772d2..e2a88f3cea7f 100644
--- a/drivers/infiniband/hw/hfi1/sysfs.c
+++ b/drivers/infiniband/hw/hfi1/sysfs.c
@@ -500,7 +500,7 @@ static ssize_t hw_rev_show(struct device *device, struct device_attribute *attr,
 	struct hfi1_ibdev *dev =
 		rdma_device_to_drv_device(device, struct hfi1_ibdev, rdi.ibdev);
 
-	return sprintf(buf, "%x\n", dd_from_dev(dev)->minrev);
+	return sysfs_emit(buf, "%x\n", dd_from_dev(dev)->minrev);
 }
 static DEVICE_ATTR_RO(hw_rev);
 
@@ -515,7 +515,7 @@ static ssize_t board_id_show(struct device *device,
 	if (!dd->boardname)
 		ret = -EINVAL;
 	else
-		ret = scnprintf(buf, PAGE_SIZE, "%s\n", dd->boardname);
+		ret = sysfs_emit(buf, "%s\n", dd->boardname);
 	return ret;
 }
 static DEVICE_ATTR_RO(board_id);
@@ -528,7 +528,7 @@ static ssize_t boardversion_show(struct device *device,
 	struct hfi1_devdata *dd = dd_from_dev(dev);
 
 	/* The string printed here is already newline-terminated. */
-	return scnprintf(buf, PAGE_SIZE, "%s", dd->boardversion);
+	return sysfs_emit(buf, "%s", dd->boardversion);
 }
 static DEVICE_ATTR_RO(boardversion);
 
@@ -545,9 +545,9 @@ static ssize_t nctxts_show(struct device *device,
 	 * and a receive context, so returning the smaller of the two counts
 	 * give a more accurate picture of total contexts available.
 	 */
-	return scnprintf(buf, PAGE_SIZE, "%u\n",
-			 min(dd->num_user_contexts,
-			     (u32)dd->sc_sizes[SC_USER].count));
+	return sysfs_emit(buf, "%u\n",
+			  min(dd->num_user_contexts,
+			      (u32)dd->sc_sizes[SC_USER].count));
 }
 static DEVICE_ATTR_RO(nctxts);
 
@@ -559,7 +559,7 @@ static ssize_t nfreectxts_show(struct device *device,
 	struct hfi1_devdata *dd = dd_from_dev(dev);
 
 	/* Return the number of free user ports (contexts) available. */
-	return scnprintf(buf, PAGE_SIZE, "%u\n", dd->freectxts);
+	return sysfs_emit(buf, "%u\n", dd->freectxts);
 }
 static DEVICE_ATTR_RO(nfreectxts);
 
@@ -570,7 +570,7 @@ static ssize_t serial_show(struct device *device,
 		rdma_device_to_drv_device(device, struct hfi1_ibdev, rdi.ibdev);
 	struct hfi1_devdata *dd = dd_from_dev(dev);
 
-	return scnprintf(buf, PAGE_SIZE, "%s", dd->serial);
+	return sysfs_emit(buf, "%s", dd->serial);
 }
 static DEVICE_ATTR_RO(serial);
 
diff --git a/drivers/infiniband/hw/i40iw/i40iw_verbs.c b/drivers/infiniband/hw/i40iw/i40iw_verbs.c
index 747b4de6faca..4f4c9f7097fe 100644
--- a/drivers/infiniband/hw/i40iw/i40iw_verbs.c
+++ b/drivers/infiniband/hw/i40iw/i40iw_verbs.c
@@ -2056,7 +2056,7 @@ static ssize_t hw_rev_show(struct device *dev,
 		rdma_device_to_drv_device(dev, struct i40iw_ib_device, ibdev);
 	u32 hw_rev = iwibdev->iwdev->sc_dev.hw_rev;
 
-	return sprintf(buf, "%x\n", hw_rev);
+	return sysfs_emit(buf, "%x\n", hw_rev);
 }
 static DEVICE_ATTR_RO(hw_rev);
 
@@ -2066,7 +2066,7 @@ static DEVICE_ATTR_RO(hw_rev);
 static ssize_t hca_type_show(struct device *dev,
 			     struct device_attribute *attr, char *buf)
 {
-	return sprintf(buf, "I40IW\n");
+	return sysfs_emit(buf, "I40IW\n");
 }
 static DEVICE_ATTR_RO(hca_type);
 
@@ -2076,7 +2076,7 @@ static DEVICE_ATTR_RO(hca_type);
 static ssize_t board_id_show(struct device *dev,
 			     struct device_attribute *attr, char *buf)
 {
-	return sprintf(buf, "%.*s\n", 32, "I40IW Board ID");
+	return sysfs_emit(buf, "%.*s\n", 32, "I40IW Board ID");
 }
 static DEVICE_ATTR_RO(board_id);
 
diff --git a/drivers/infiniband/hw/mlx4/main.c b/drivers/infiniband/hw/mlx4/main.c
index 753c70402498..ad3c8f985921 100644
--- a/drivers/infiniband/hw/mlx4/main.c
+++ b/drivers/infiniband/hw/mlx4/main.c
@@ -2024,7 +2024,7 @@ static ssize_t hca_type_show(struct device *device,
 {
 	struct mlx4_ib_dev *dev =
 		rdma_device_to_drv_device(device, struct mlx4_ib_dev, ib_dev);
-	return sprintf(buf, "MT%d\n", dev->dev->persist->pdev->device);
+	return sysfs_emit(buf, "MT%d\n", dev->dev->persist->pdev->device);
 }
 static DEVICE_ATTR_RO(hca_type);
 
@@ -2033,7 +2033,7 @@ static ssize_t hw_rev_show(struct device *device,
 {
 	struct mlx4_ib_dev *dev =
 		rdma_device_to_drv_device(device, struct mlx4_ib_dev, ib_dev);
-	return sprintf(buf, "%x\n", dev->dev->rev_id);
+	return sysfs_emit(buf, "%x\n", dev->dev->rev_id);
 }
 static DEVICE_ATTR_RO(hw_rev);
 
@@ -2043,8 +2043,8 @@ static ssize_t board_id_show(struct device *device,
 	struct mlx4_ib_dev *dev =
 		rdma_device_to_drv_device(device, struct mlx4_ib_dev, ib_dev);
 
-	return sprintf(buf, "%.*s\n", MLX4_BOARD_ID_LEN,
-		       dev->dev->board_id);
+	return sysfs_emit(buf, "%.*s\n", MLX4_BOARD_ID_LEN,
+			  dev->dev->board_id);
 }
 static DEVICE_ATTR_RO(board_id);
 
diff --git a/drivers/infiniband/hw/mlx4/sysfs.c b/drivers/infiniband/hw/mlx4/sysfs.c
index ea1f3a081b05..26c717a4911f 100644
--- a/drivers/infiniband/hw/mlx4/sysfs.c
+++ b/drivers/infiniband/hw/mlx4/sysfs.c
@@ -56,7 +56,7 @@ static ssize_t show_admin_alias_guid(struct device *dev,
 					      mlx4_ib_iov_dentry->entry_num,
 					      port->num);
 
-	return sprintf(buf, "%llx\n", be64_to_cpu(sysadmin_ag_val));
+	return sysfs_emit(buf, "%llx\n", be64_to_cpu(sysadmin_ag_val));
 }
 
 /* store_admin_alias_guid stores the (new) administratively assigned value of that GUID.
@@ -123,15 +123,15 @@ static ssize_t show_port_gid(struct device *dev,
 				  mlx4_ib_iov_dentry->entry_num, &gid, 1);
 	if (ret)
 		return ret;
-	ret = sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
-		      be16_to_cpu(((__be16 *) gid.raw)[0]),
-		      be16_to_cpu(((__be16 *) gid.raw)[1]),
-		      be16_to_cpu(((__be16 *) gid.raw)[2]),
-		      be16_to_cpu(((__be16 *) gid.raw)[3]),
-		      be16_to_cpu(((__be16 *) gid.raw)[4]),
-		      be16_to_cpu(((__be16 *) gid.raw)[5]),
-		      be16_to_cpu(((__be16 *) gid.raw)[6]),
-		      be16_to_cpu(((__be16 *) gid.raw)[7]));
+	ret = sysfs_emit(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
+			 be16_to_cpu(((__be16 *) gid.raw)[0]),
+			 be16_to_cpu(((__be16 *) gid.raw)[1]),
+			 be16_to_cpu(((__be16 *) gid.raw)[2]),
+			 be16_to_cpu(((__be16 *) gid.raw)[3]),
+			 be16_to_cpu(((__be16 *) gid.raw)[4]),
+			 be16_to_cpu(((__be16 *) gid.raw)[5]),
+			 be16_to_cpu(((__be16 *) gid.raw)[6]),
+			 be16_to_cpu(((__be16 *) gid.raw)[7]));
 	return ret;
 }
 
@@ -151,7 +151,7 @@ static ssize_t show_phys_port_pkey(struct device *dev,
 	if (ret)
 		return ret;
 
-	return sprintf(buf, "0x%04x\n", pkey);
+	return sysfs_emit(buf, "0x%04x\n", pkey);
 }
 
 #define DENTRY_REMOVE(_dentry)						\
@@ -545,9 +545,9 @@ static ssize_t sysfs_show_smi_enabled(struct device *dev,
 	ssize_t len = 0;
 
 	if (mlx4_vf_smi_enabled(p->dev->dev, p->slave, p->port_num))
-		len = sprintf(buf, "%d\n", 1);
+		len = sysfs_emit(buf, "%d\n", 1);
 	else
-		len = sprintf(buf, "%d\n", 0);
+		len = sysfs_emit(buf, "%d\n", 0);
 
 	return len;
 }
@@ -561,9 +561,9 @@ static ssize_t sysfs_show_enable_smi_admin(struct device *dev,
 	ssize_t len = 0;
 
 	if (mlx4_vf_get_enable_smi_admin(p->dev->dev, p->slave, p->port_num))
-		len = sprintf(buf, "%d\n", 1);
+		len = sysfs_emit(buf, "%d\n", 1);
 	else
-		len = sprintf(buf, "%d\n", 0);
+		len = sysfs_emit(buf, "%d\n", 0);
 
 	return len;
 }
diff --git a/drivers/infiniband/hw/mlx5/main.c b/drivers/infiniband/hw/mlx5/main.c
index 7082172b5b61..8b9f6f857120 100644
--- a/drivers/infiniband/hw/mlx5/main.c
+++ b/drivers/infiniband/hw/mlx5/main.c
@@ -2628,7 +2628,7 @@ static ssize_t fw_pages_show(struct device *device,
 	struct mlx5_ib_dev *dev =
 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
 
-	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
+	return sysfs_emit(buf, "%d\n", dev->mdev->priv.fw_pages);
 }
 static DEVICE_ATTR_RO(fw_pages);
 
@@ -2638,7 +2638,8 @@ static ssize_t reg_pages_show(struct device *device,
 	struct mlx5_ib_dev *dev =
 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
 
-	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
+	return sysfs_emit(buf, "%d\n",
+			  atomic_read(&dev->mdev->priv.reg_pages));
 }
 static DEVICE_ATTR_RO(reg_pages);
 
@@ -2648,7 +2649,7 @@ static ssize_t hca_type_show(struct device *device,
 	struct mlx5_ib_dev *dev =
 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
 
-	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
+	return sysfs_emit(buf, "MT%d\n", dev->mdev->pdev->device);
 }
 static DEVICE_ATTR_RO(hca_type);
 
@@ -2658,7 +2659,7 @@ static ssize_t hw_rev_show(struct device *device,
 	struct mlx5_ib_dev *dev =
 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
 
-	return sprintf(buf, "%x\n", dev->mdev->rev_id);
+	return sysfs_emit(buf, "%x\n", dev->mdev->rev_id);
 }
 static DEVICE_ATTR_RO(hw_rev);
 
@@ -2668,8 +2669,8 @@ static ssize_t board_id_show(struct device *device,
 	struct mlx5_ib_dev *dev =
 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
 
-	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
-		       dev->mdev->board_id);
+	return sysfs_emit(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
+			  dev->mdev->board_id);
 }
 static DEVICE_ATTR_RO(board_id);
 
diff --git a/drivers/infiniband/hw/mthca/mthca_provider.c b/drivers/infiniband/hw/mthca/mthca_provider.c
index 31b558ff8218..b6e4254e4558 100644
--- a/drivers/infiniband/hw/mthca/mthca_provider.c
+++ b/drivers/infiniband/hw/mthca/mthca_provider.c
@@ -961,7 +961,7 @@ static ssize_t hw_rev_show(struct device *device,
 	struct mthca_dev *dev =
 		rdma_device_to_drv_device(device, struct mthca_dev, ib_dev);
 
-	return sprintf(buf, "%x\n", dev->rev_id);
+	return sysfs_emit(buf, "%x\n", dev->rev_id);
 }
 static DEVICE_ATTR_RO(hw_rev);
 
@@ -973,16 +973,16 @@ static ssize_t hca_type_show(struct device *device,
 
 	switch (dev->pdev->device) {
 	case PCI_DEVICE_ID_MELLANOX_TAVOR:
-		return sprintf(buf, "MT23108\n");
+		return sysfs_emit(buf, "MT23108\n");
 	case PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT:
-		return sprintf(buf, "MT25208 (MT23108 compat mode)\n");
+		return sysfs_emit(buf, "MT25208 (MT23108 compat mode)\n");
 	case PCI_DEVICE_ID_MELLANOX_ARBEL:
-		return sprintf(buf, "MT25208\n");
+		return sysfs_emit(buf, "MT25208\n");
 	case PCI_DEVICE_ID_MELLANOX_SINAI:
 	case PCI_DEVICE_ID_MELLANOX_SINAI_OLD:
-		return sprintf(buf, "MT25204\n");
+		return sysfs_emit(buf, "MT25204\n");
 	default:
-		return sprintf(buf, "unknown\n");
+		return sysfs_emit(buf, "unknown\n");
 	}
 }
 static DEVICE_ATTR_RO(hca_type);
@@ -993,7 +993,7 @@ static ssize_t board_id_show(struct device *device,
 	struct mthca_dev *dev =
 		rdma_device_to_drv_device(device, struct mthca_dev, ib_dev);
 
-	return sprintf(buf, "%.*s\n", MTHCA_BOARD_ID_LEN, dev->board_id);
+	return sysfs_emit(buf, "%.*s\n", MTHCA_BOARD_ID_LEN, dev->board_id);
 }
 static DEVICE_ATTR_RO(board_id);
 
diff --git a/drivers/infiniband/hw/ocrdma/ocrdma_main.c b/drivers/infiniband/hw/ocrdma/ocrdma_main.c
index d8c47d24d6d6..e95641b26653 100644
--- a/drivers/infiniband/hw/ocrdma/ocrdma_main.c
+++ b/drivers/infiniband/hw/ocrdma/ocrdma_main.c
@@ -119,7 +119,7 @@ static ssize_t hw_rev_show(struct device *device,
 	struct ocrdma_dev *dev =
 		rdma_device_to_drv_device(device, struct ocrdma_dev, ibdev);
 
-	return scnprintf(buf, PAGE_SIZE, "0x%x\n", dev->nic_info.pdev->vendor);
+	return sysfs_emit(buf, "0x%x\n", dev->nic_info.pdev->vendor);
 }
 static DEVICE_ATTR_RO(hw_rev);
 
@@ -129,7 +129,7 @@ static ssize_t hca_type_show(struct device *device,
 	struct ocrdma_dev *dev =
 		rdma_device_to_drv_device(device, struct ocrdma_dev, ibdev);
 
-	return scnprintf(buf, PAGE_SIZE, "%s\n", &dev->model_number[0]);
+	return sysfs_emit(buf, "%s\n", &dev->model_number[0]);
 }
 static DEVICE_ATTR_RO(hca_type);
 
diff --git a/drivers/infiniband/hw/qedr/main.c b/drivers/infiniband/hw/qedr/main.c
index 7c0aac3e635b..c1eaceac5016 100644
--- a/drivers/infiniband/hw/qedr/main.c
+++ b/drivers/infiniband/hw/qedr/main.c
@@ -124,7 +124,7 @@ static ssize_t hw_rev_show(struct device *device, struct device_attribute *attr,
 	struct qedr_dev *dev =
 		rdma_device_to_drv_device(device, struct qedr_dev, ibdev);
 
-	return scnprintf(buf, PAGE_SIZE, "0x%x\n", dev->attr.hw_ver);
+	return sysfs_emit(buf, "0x%x\n", dev->attr.hw_ver);
 }
 static DEVICE_ATTR_RO(hw_rev);
 
@@ -134,10 +134,10 @@ static ssize_t hca_type_show(struct device *device,
 	struct qedr_dev *dev =
 		rdma_device_to_drv_device(device, struct qedr_dev, ibdev);
 
-	return scnprintf(buf, PAGE_SIZE, "FastLinQ QL%x %s\n",
-			 dev->pdev->device,
-			 rdma_protocol_iwarp(&dev->ibdev, 1) ?
-			 "iWARP" : "RoCE");
+	return sysfs_emit(buf, "FastLinQ QL%x %s\n",
+			  dev->pdev->device,
+			  rdma_protocol_iwarp(&dev->ibdev, 1) ?
+			  "iWARP" : "RoCE");
 }
 static DEVICE_ATTR_RO(hca_type);
 
diff --git a/drivers/infiniband/hw/qib/qib_sysfs.c b/drivers/infiniband/hw/qib/qib_sysfs.c
index 021df0654ba7..2963915d0851 100644
--- a/drivers/infiniband/hw/qib/qib_sysfs.c
+++ b/drivers/infiniband/hw/qib/qib_sysfs.c
@@ -565,7 +565,7 @@ static ssize_t hw_rev_show(struct device *device, struct device_attribute *attr,
 	struct qib_ibdev *dev =
 		rdma_device_to_drv_device(device, struct qib_ibdev, rdi.ibdev);
 
-	return sprintf(buf, "%x\n", dd_from_dev(dev)->minrev);
+	return sysfs_emit(buf, "%x\n", dd_from_dev(dev)->minrev);
 }
 static DEVICE_ATTR_RO(hw_rev);
 
@@ -580,7 +580,7 @@ static ssize_t hca_type_show(struct device *device,
 	if (!dd->boardname)
 		ret = -EINVAL;
 	else
-		ret = scnprintf(buf, PAGE_SIZE, "%s\n", dd->boardname);
+		ret = sysfs_emit(buf, "%s\n", dd->boardname);
 	return ret;
 }
 static DEVICE_ATTR_RO(hca_type);
@@ -590,7 +590,7 @@ static ssize_t version_show(struct device *device,
 			    struct device_attribute *attr, char *buf)
 {
 	/* The string printed here is already newline-terminated. */
-	return scnprintf(buf, PAGE_SIZE, "%s", (char *)ib_qib_version);
+	return sysfs_emit(buf, "%s", (char *)ib_qib_version);
 }
 static DEVICE_ATTR_RO(version);
 
@@ -602,7 +602,7 @@ static ssize_t boardversion_show(struct device *device,
 	struct qib_devdata *dd = dd_from_dev(dev);
 
 	/* The string printed here is already newline-terminated. */
-	return scnprintf(buf, PAGE_SIZE, "%s", dd->boardversion);
+	return sysfs_emit(buf, "%s", dd->boardversion);
 }
 static DEVICE_ATTR_RO(boardversion);
 
@@ -614,7 +614,7 @@ static ssize_t localbus_info_show(struct device *device,
 	struct qib_devdata *dd = dd_from_dev(dev);
 
 	/* The string printed here is already newline-terminated. */
-	return scnprintf(buf, PAGE_SIZE, "%s", dd->lbus_info);
+	return sysfs_emit(buf, "%s", dd->lbus_info);
 }
 static DEVICE_ATTR_RO(localbus_info);
 
@@ -628,9 +628,9 @@ static ssize_t nctxts_show(struct device *device,
 	/* Return the number of user ports (contexts) available. */
 	/* The calculation below deals with a special case where
 	 * cfgctxts is set to 1 on a single-port board. */
-	return scnprintf(buf, PAGE_SIZE, "%u\n",
-			(dd->first_user_ctxt > dd->cfgctxts) ? 0 :
-			(dd->cfgctxts - dd->first_user_ctxt));
+	return sysfs_emit(buf, "%u\n",
+			  (dd->first_user_ctxt > dd->cfgctxts) ? 0 :
+			  (dd->cfgctxts - dd->first_user_ctxt));
 }
 static DEVICE_ATTR_RO(nctxts);
 
@@ -642,7 +642,7 @@ static ssize_t nfreectxts_show(struct device *device,
 	struct qib_devdata *dd = dd_from_dev(dev);
 
 	/* Return the number of free user ports (contexts) available. */
-	return scnprintf(buf, PAGE_SIZE, "%u\n", dd->freectxts);
+	return sysfs_emit(buf, "%u\n", dd->freectxts);
 }
 static DEVICE_ATTR_RO(nfreectxts);
 
@@ -703,12 +703,12 @@ static ssize_t tempsense_show(struct device *device,
 		regvals[idx] = ret;
 	}
 	if (idx == 8)
-		ret = scnprintf(buf, PAGE_SIZE, "%d %d %02X %02X %d %d\n",
-				*(signed char *)(regvals),
-				*(signed char *)(regvals + 1),
-				regvals[2], regvals[3],
-				*(signed char *)(regvals + 5),
-				*(signed char *)(regvals + 7));
+		ret = sysfs_emit(buf, "%d %d %02X %02X %d %d\n",
+				 *(signed char *)(regvals),
+				 *(signed char *)(regvals + 1),
+				 regvals[2], regvals[3],
+				 *(signed char *)(regvals + 5),
+				 *(signed char *)(regvals + 7));
 	return ret;
 }
 static DEVICE_ATTR_RO(tempsense);
diff --git a/drivers/infiniband/hw/usnic/usnic_ib_sysfs.c b/drivers/infiniband/hw/usnic/usnic_ib_sysfs.c
index c85d48ae7442..38da539c61e0 100644
--- a/drivers/infiniband/hw/usnic/usnic_ib_sysfs.c
+++ b/drivers/infiniband/hw/usnic/usnic_ib_sysfs.c
@@ -57,7 +57,7 @@ static ssize_t board_id_show(struct device *device,
 	subsystem_device_id = us_ibdev->pdev->subsystem_device;
 	mutex_unlock(&us_ibdev->usdev_lock);
 
-	return scnprintf(buf, PAGE_SIZE, "%hu\n", subsystem_device_id);
+	return sysfs_emit(buf, "%hu\n", subsystem_device_id);
 }
 static DEVICE_ATTR_RO(board_id);
 
@@ -132,8 +132,8 @@ iface_show(struct device *device, struct device_attribute *attr, char *buf)
 	struct usnic_ib_dev *us_ibdev =
 		rdma_device_to_drv_device(device, struct usnic_ib_dev, ib_dev);
 
-	return scnprintf(buf, PAGE_SIZE, "%s\n",
-			netdev_name(us_ibdev->netdev));
+	return sysfs_emit(buf, "%s\n",
+			  netdev_name(us_ibdev->netdev));
 }
 static DEVICE_ATTR_RO(iface);
 
@@ -143,8 +143,8 @@ max_vf_show(struct device *device, struct device_attribute *attr, char *buf)
 	struct usnic_ib_dev *us_ibdev =
 		rdma_device_to_drv_device(device, struct usnic_ib_dev, ib_dev);
 
-	return scnprintf(buf, PAGE_SIZE, "%u\n",
-			kref_read(&us_ibdev->vf_cnt));
+	return sysfs_emit(buf, "%u\n",
+			  kref_read(&us_ibdev->vf_cnt));
 }
 static DEVICE_ATTR_RO(max_vf);
 
@@ -158,7 +158,7 @@ qp_per_vf_show(struct device *device, struct device_attribute *attr, char *buf)
 	qp_per_vf = max(us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_WQ],
 			us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_RQ]);
 
-	return scnprintf(buf, PAGE_SIZE,
+	return sysfs_emit(buf,
 				"%d\n", qp_per_vf);
 }
 static DEVICE_ATTR_RO(qp_per_vf);
@@ -169,8 +169,8 @@ cq_per_vf_show(struct device *device, struct device_attribute *attr, char *buf)
 	struct usnic_ib_dev *us_ibdev =
 		rdma_device_to_drv_device(device, struct usnic_ib_dev, ib_dev);
 
-	return scnprintf(buf, PAGE_SIZE, "%d\n",
-			us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_CQ]);
+	return sysfs_emit(buf, "%d\n",
+			  us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_CQ]);
 }
 static DEVICE_ATTR_RO(cq_per_vf);
 
diff --git a/drivers/infiniband/hw/vmw_pvrdma/pvrdma_main.c b/drivers/infiniband/hw/vmw_pvrdma/pvrdma_main.c
index 780fd2dfc07e..f7fbe2044571 100644
--- a/drivers/infiniband/hw/vmw_pvrdma/pvrdma_main.c
+++ b/drivers/infiniband/hw/vmw_pvrdma/pvrdma_main.c
@@ -68,21 +68,21 @@ static int pvrdma_del_gid(const struct ib_gid_attr *attr, void **context);
 static ssize_t hca_type_show(struct device *device,
 			     struct device_attribute *attr, char *buf)
 {
-	return sprintf(buf, "VMW_PVRDMA-%s\n", DRV_VERSION);
+	return sysfs_emit(buf, "VMW_PVRDMA-%s\n", DRV_VERSION);
 }
 static DEVICE_ATTR_RO(hca_type);
 
 static ssize_t hw_rev_show(struct device *device,
 			   struct device_attribute *attr, char *buf)
 {
-	return sprintf(buf, "%d\n", PVRDMA_REV_ID);
+	return sysfs_emit(buf, "%d\n", PVRDMA_REV_ID);
 }
 static DEVICE_ATTR_RO(hw_rev);
 
 static ssize_t board_id_show(struct device *device,
 			     struct device_attribute *attr, char *buf)
 {
-	return sprintf(buf, "%d\n", PVRDMA_BOARD_ID);
+	return sysfs_emit(buf, "%d\n", PVRDMA_BOARD_ID);
 }
 static DEVICE_ATTR_RO(board_id);
 
diff --git a/drivers/infiniband/sw/rxe/rxe_verbs.c b/drivers/infiniband/sw/rxe/rxe_verbs.c
index f368dc16281a..afa9674aa35c 100644
--- a/drivers/infiniband/sw/rxe/rxe_verbs.c
+++ b/drivers/infiniband/sw/rxe/rxe_verbs.c
@@ -1033,7 +1033,7 @@ static ssize_t parent_show(struct device *device,
 	struct rxe_dev *rxe =
 		rdma_device_to_drv_device(device, struct rxe_dev, ib_dev);
 
-	return scnprintf(buf, PAGE_SIZE, "%s\n", rxe_parent_name(rxe, 1));
+	return sysfs_emit(buf, "%s\n", rxe_parent_name(rxe, 1));
 }
 
 static DEVICE_ATTR_RO(parent);
diff --git a/drivers/infiniband/ulp/ipoib/ipoib_cm.c b/drivers/infiniband/ulp/ipoib/ipoib_cm.c
index 8f0b598a46ec..d5d592bdab35 100644
--- a/drivers/infiniband/ulp/ipoib/ipoib_cm.c
+++ b/drivers/infiniband/ulp/ipoib/ipoib_cm.c
@@ -1514,9 +1514,9 @@ static ssize_t show_mode(struct device *d, struct device_attribute *attr,
 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
 
 	if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
-		return sprintf(buf, "connected\n");
+		return sysfs_emit(buf, "connected\n");
 	else
-		return sprintf(buf, "datagram\n");
+		return sysfs_emit(buf, "datagram\n");
 }
 
 static ssize_t set_mode(struct device *d, struct device_attribute *attr,
diff --git a/drivers/infiniband/ulp/ipoib/ipoib_main.c b/drivers/infiniband/ulp/ipoib/ipoib_main.c
index abfab89423f4..a6f413491321 100644
--- a/drivers/infiniband/ulp/ipoib/ipoib_main.c
+++ b/drivers/infiniband/ulp/ipoib/ipoib_main.c
@@ -2266,7 +2266,7 @@ static ssize_t show_pkey(struct device *dev,
 	struct net_device *ndev = to_net_dev(dev);
 	struct ipoib_dev_priv *priv = ipoib_priv(ndev);
 
-	return sprintf(buf, "0x%04x\n", priv->pkey);
+	return sysfs_emit(buf, "0x%04x\n", priv->pkey);
 }
 static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
 
@@ -2276,7 +2276,8 @@ static ssize_t show_umcast(struct device *dev,
 	struct net_device *ndev = to_net_dev(dev);
 	struct ipoib_dev_priv *priv = ipoib_priv(ndev);
 
-	return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
+	return sysfs_emit(buf, "%d\n",
+			  test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
 }
 
 void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
@@ -2446,7 +2447,7 @@ static ssize_t dev_id_show(struct device *dev,
 			"\"%s\" wants to know my dev_id. Should it look at dev_port instead? See Documentation/ABI/testing/sysfs-class-net for more info.\n",
 			current->comm);
 
-	return sprintf(buf, "%#x\n", ndev->dev_id);
+	return sysfs_emit(buf, "%#x\n", ndev->dev_id);
 }
 static DEVICE_ATTR_RO(dev_id);
 
diff --git a/drivers/infiniband/ulp/ipoib/ipoib_vlan.c b/drivers/infiniband/ulp/ipoib/ipoib_vlan.c
index 4c50a87ed7cc..5958840dbeed 100644
--- a/drivers/infiniband/ulp/ipoib/ipoib_vlan.c
+++ b/drivers/infiniband/ulp/ipoib/ipoib_vlan.c
@@ -46,7 +46,7 @@ static ssize_t show_parent(struct device *d, struct device_attribute *attr,
 	struct net_device *dev = to_net_dev(d);
 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
 
-	return sprintf(buf, "%s\n", priv->parent->name);
+	return sysfs_emit(buf, "%s\n", priv->parent->name);
 }
 static DEVICE_ATTR(parent, S_IRUGO, show_parent, NULL);
 
diff --git a/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c b/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c
index ac4c49cbf153..7f71f10126fc 100644
--- a/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c
+++ b/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c
@@ -52,7 +52,8 @@ static ssize_t max_reconnect_attempts_show(struct device *dev,
 {
 	struct rtrs_clt *clt = container_of(dev, struct rtrs_clt, dev);
 
-	return sprintf(page, "%d\n", rtrs_clt_get_max_reconnect_attempts(clt));
+	return sysfs_emit(page, "%d\n",
+			  rtrs_clt_get_max_reconnect_attempts(clt));
 }
 
 static ssize_t max_reconnect_attempts_store(struct device *dev,
@@ -95,11 +96,13 @@ static ssize_t mpath_policy_show(struct device *dev,
 
 	switch (clt->mp_policy) {
 	case MP_POLICY_RR:
-		return sprintf(page, "round-robin (RR: %d)\n", clt->mp_policy);
+		return sysfs_emit(page, "round-robin (RR: %d)\n",
+				  clt->mp_policy);
 	case MP_POLICY_MIN_INFLIGHT:
-		return sprintf(page, "min-inflight (MI: %d)\n", clt->mp_policy);
+		return sysfs_emit(page, "min-inflight (MI: %d)\n",
+				  clt->mp_policy);
 	default:
-		return sprintf(page, "Unknown (%d)\n", clt->mp_policy);
+		return sysfs_emit(page, "Unknown (%d)\n", clt->mp_policy);
 	}
 }
 
@@ -138,9 +141,9 @@ static DEVICE_ATTR_RW(mpath_policy);
 static ssize_t add_path_show(struct device *dev,
 			     struct device_attribute *attr, char *page)
 {
-	return scnprintf(page, PAGE_SIZE,
-			 "Usage: echo [<source addr>@]<destination addr> > %s\n\n*addr ::= [ ip:<ipv4|ipv6> | gid:<gid> ]\n",
-			 attr->attr.name);
+	return sysfs_emit(page,
+			  "Usage: echo [<source addr>@]<destination addr> > %s\n\n*addr ::= [ ip:<ipv4|ipv6> | gid:<gid> ]\n",
+			  attr->attr.name);
 }
 
 static ssize_t add_path_store(struct device *dev,
diff --git a/drivers/infiniband/ulp/srp/ib_srp.c b/drivers/infiniband/ulp/srp/ib_srp.c
index d8fcd21ab472..c9284f767ec1 100644
--- a/drivers/infiniband/ulp/srp/ib_srp.c
+++ b/drivers/infiniband/ulp/srp/ib_srp.c
@@ -2896,7 +2896,7 @@ static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
 {
 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
 
-	return sprintf(buf, "0x%016llx\n", be64_to_cpu(target->id_ext));
+	return sysfs_emit(buf, "0x%016llx\n", be64_to_cpu(target->id_ext));
 }
 
 static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
@@ -2904,7 +2904,7 @@ static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
 {
 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
 
-	return sprintf(buf, "0x%016llx\n", be64_to_cpu(target->ioc_guid));
+	return sysfs_emit(buf, "0x%016llx\n", be64_to_cpu(target->ioc_guid));
 }
 
 static ssize_t show_service_id(struct device *dev,
@@ -2914,8 +2914,8 @@ static ssize_t show_service_id(struct device *dev,
 
 	if (target->using_rdma_cm)
 		return -ENOENT;
-	return sprintf(buf, "0x%016llx\n",
-		       be64_to_cpu(target->ib_cm.service_id));
+	return sysfs_emit(buf, "0x%016llx\n",
+			  be64_to_cpu(target->ib_cm.service_id));
 }
 
 static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
@@ -2925,7 +2925,7 @@ static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
 
 	if (target->using_rdma_cm)
 		return -ENOENT;
-	return sprintf(buf, "0x%04x\n", be16_to_cpu(target->ib_cm.pkey));
+	return sysfs_emit(buf, "0x%04x\n", be16_to_cpu(target->ib_cm.pkey));
 }
 
 static ssize_t show_sgid(struct device *dev, struct device_attribute *attr,
@@ -2933,7 +2933,7 @@ static ssize_t show_sgid(struct device *dev, struct device_attribute *attr,
 {
 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
 
-	return sprintf(buf, "%pI6\n", target->sgid.raw);
+	return sysfs_emit(buf, "%pI6\n", target->sgid.raw);
 }
 
 static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
@@ -2944,7 +2944,7 @@ static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
 
 	if (target->using_rdma_cm)
 		return -ENOENT;
-	return sprintf(buf, "%pI6\n", ch->ib_cm.path.dgid.raw);
+	return sysfs_emit(buf, "%pI6\n", ch->ib_cm.path.dgid.raw);
 }
 
 static ssize_t show_orig_dgid(struct device *dev,
@@ -2954,7 +2954,7 @@ static ssize_t show_orig_dgid(struct device *dev,
 
 	if (target->using_rdma_cm)
 		return -ENOENT;
-	return sprintf(buf, "%pI6\n", target->ib_cm.orig_dgid.raw);
+	return sysfs_emit(buf, "%pI6\n", target->ib_cm.orig_dgid.raw);
 }
 
 static ssize_t show_req_lim(struct device *dev,
@@ -2968,7 +2968,7 @@ static ssize_t show_req_lim(struct device *dev,
 		ch = &target->ch[i];
 		req_lim = min(req_lim, ch->req_lim);
 	}
-	return sprintf(buf, "%d\n", req_lim);
+	return sysfs_emit(buf, "%d\n", req_lim);
 }
 
 static ssize_t show_zero_req_lim(struct device *dev,
@@ -2976,7 +2976,7 @@ static ssize_t show_zero_req_lim(struct device *dev,
 {
 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
 
-	return sprintf(buf, "%d\n", target->zero_req_lim);
+	return sysfs_emit(buf, "%d\n", target->zero_req_lim);
 }
 
 static ssize_t show_local_ib_port(struct device *dev,
@@ -2984,7 +2984,7 @@ static ssize_t show_local_ib_port(struct device *dev,
 {
 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
 
-	return sprintf(buf, "%d\n", target->srp_host->port);
+	return sysfs_emit(buf, "%d\n", target->srp_host->port);
 }
 
 static ssize_t show_local_ib_device(struct device *dev,
@@ -2992,8 +2992,8 @@ static ssize_t show_local_ib_device(struct device *dev,
 {
 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
 
-	return sprintf(buf, "%s\n",
-		       dev_name(&target->srp_host->srp_dev->dev->dev));
+	return sysfs_emit(buf, "%s\n",
+			  dev_name(&target->srp_host->srp_dev->dev->dev));
 }
 
 static ssize_t show_ch_count(struct device *dev, struct device_attribute *attr,
@@ -3001,7 +3001,7 @@ static ssize_t show_ch_count(struct device *dev, struct device_attribute *attr,
 {
 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
 
-	return sprintf(buf, "%d\n", target->ch_count);
+	return sysfs_emit(buf, "%d\n", target->ch_count);
 }
 
 static ssize_t show_comp_vector(struct device *dev,
@@ -3009,7 +3009,7 @@ static ssize_t show_comp_vector(struct device *dev,
 {
 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
 
-	return sprintf(buf, "%d\n", target->comp_vector);
+	return sysfs_emit(buf, "%d\n", target->comp_vector);
 }
 
 static ssize_t show_tl_retry_count(struct device *dev,
@@ -3017,7 +3017,7 @@ static ssize_t show_tl_retry_count(struct device *dev,
 {
 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
 
-	return sprintf(buf, "%d\n", target->tl_retry_count);
+	return sysfs_emit(buf, "%d\n", target->tl_retry_count);
 }
 
 static ssize_t show_cmd_sg_entries(struct device *dev,
@@ -3025,7 +3025,7 @@ static ssize_t show_cmd_sg_entries(struct device *dev,
 {
 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
 
-	return sprintf(buf, "%u\n", target->cmd_sg_cnt);
+	return sysfs_emit(buf, "%u\n", target->cmd_sg_cnt);
 }
 
 static ssize_t show_allow_ext_sg(struct device *dev,
@@ -3033,7 +3033,8 @@ static ssize_t show_allow_ext_sg(struct device *dev,
 {
 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
 
-	return sprintf(buf, "%s\n", target->allow_ext_sg ? "true" : "false");
+	return sysfs_emit(buf, "%s\n",
+			  target->allow_ext_sg ? "true" : "false");
 }
 
 static DEVICE_ATTR(id_ext,	    S_IRUGO, show_id_ext,	   NULL);
@@ -3893,7 +3894,7 @@ static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
 {
 	struct srp_host *host = container_of(dev, struct srp_host, dev);
 
-	return sprintf(buf, "%s\n", dev_name(&host->srp_dev->dev->dev));
+	return sysfs_emit(buf, "%s\n", dev_name(&host->srp_dev->dev->dev));
 }
 
 static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
@@ -3903,7 +3904,7 @@ static ssize_t show_port(struct device *dev, struct device_attribute *attr,
 {
 	struct srp_host *host = container_of(dev, struct srp_host, dev);
 
-	return sprintf(buf, "%d\n", host->port);
+	return sysfs_emit(buf, "%d\n", host->port);
 }
 
 static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
-- 
2.26.0


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH 2/4] RDMA: Convert sysfs kobject * show functions to use sysfs_emit()
  2020-10-08  2:36 [PATCH-next 0/4] RDMA: sprintf to sysfs_emit conversions Joe Perches
  2020-10-08  2:36 ` [PATCH 1/4] RDMA: Convert sysfs device * show functions to use sysfs_emit() Joe Perches
@ 2020-10-08  2:36 ` Joe Perches
  2020-10-09 14:09   ` Jason Gunthorpe
  2020-10-12  5:21   ` Jinpu Wang
  2020-10-08  2:36 ` [PATCH 3/4] RDMA: manual changes for sysfs_emit and neatening Joe Perches
                   ` (4 subsequent siblings)
  6 siblings, 2 replies; 26+ messages in thread
From: Joe Perches @ 2020-10-08  2:36 UTC (permalink / raw)
  To: Doug Ledford, Jason Gunthorpe, Danil Kipnis, Jack Wang; +Cc: linux-rdma

Done with cocci script:

@@
identifier k_show;
identifier arg1, arg2, arg3;
@@
ssize_t k_show(struct kobject *
-	arg1
+	kobj
	, struct kobj_attribute *
-	arg2
+	attr
	, char *
-	arg3
+	buf
	)
{
	...
(
-	arg1
+	kobj
|
-	arg2
+	attr
|
-	arg3
+	buf
)
	...
}

@@
identifier k_show;
identifier kobj, attr, buf;
@@

ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
	<...
	return
-	sprintf(buf,
+	sysfs_emit(buf,
	...);
	...>
}

@@
identifier k_show;
identifier kobj, attr, buf;
@@

ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
	<...
	return
-	snprintf(buf, PAGE_SIZE,
+	sysfs_emit(buf,
	...);
	...>
}

@@
identifier k_show;
identifier kobj, attr, buf;
@@

ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
	<...
	return
-	scnprintf(buf, PAGE_SIZE,
+	sysfs_emit(buf,
	...);
	...>
}

@@
identifier k_show;
identifier kobj, attr, buf;
expression chr;
@@

ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
	<...
	return
-	strcpy(buf, chr);
+	sysfs_emit(buf, chr);
	...>
}

@@
identifier k_show;
identifier kobj, attr, buf;
identifier len;
@@

ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
	<...
	len =
-	sprintf(buf,
+	sysfs_emit(buf,
	...);
	...>
	return len;
}

@@
identifier k_show;
identifier kobj, attr, buf;
identifier len;
@@

ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
	<...
	len =
-	snprintf(buf, PAGE_SIZE,
+	sysfs_emit(buf,
	...);
	...>
	return len;
}

@@
identifier k_show;
identifier kobj, attr, buf;
identifier len;
@@

ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
	<...
	len =
-	scnprintf(buf, PAGE_SIZE,
+	sysfs_emit(buf,
	...);
	...>
	return len;
}

@@
identifier k_show;
identifier kobj, attr, buf;
identifier len;
@@

ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
	<...
-	len += scnprintf(buf + len, PAGE_SIZE - len,
+	len += sysfs_emit_at(buf, len,
	...);
	...>
	return len;
}

@@
identifier k_show;
identifier kobj, attr, buf;
expression chr;
@@

ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
	...
-	strcpy(buf, chr);
-	return strlen(buf);
+	return sysfs_emit(buf, chr);
}

Signed-off-by: Joe Perches <joe@perches.com>
---
 drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c | 26 ++++++++++----------
 drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c | 14 +++++------
 2 files changed, 20 insertions(+), 20 deletions(-)

diff --git a/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c b/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c
index 7f71f10126fc..0c767582286b 100644
--- a/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c
+++ b/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c
@@ -187,9 +187,9 @@ static ssize_t rtrs_clt_state_show(struct kobject *kobj,
 
 	sess = container_of(kobj, struct rtrs_clt_sess, kobj);
 	if (sess->state == RTRS_CLT_CONNECTED)
-		return sprintf(page, "connected\n");
+		return sysfs_emit(page, "connected\n");
 
-	return sprintf(page, "disconnected\n");
+	return sysfs_emit(page, "disconnected\n");
 }
 
 static struct kobj_attribute rtrs_clt_state_attr =
@@ -197,10 +197,10 @@ static struct kobj_attribute rtrs_clt_state_attr =
 
 static ssize_t rtrs_clt_reconnect_show(struct kobject *kobj,
 					struct kobj_attribute *attr,
-					char *page)
+					char *buf)
 {
-	return scnprintf(page, PAGE_SIZE, "Usage: echo 1 > %s\n",
-			 attr->attr.name);
+	return sysfs_emit(buf, "Usage: echo 1 > %s\n",
+			  attr->attr.name);
 }
 
 static ssize_t rtrs_clt_reconnect_store(struct kobject *kobj,
@@ -229,10 +229,10 @@ static struct kobj_attribute rtrs_clt_reconnect_attr =
 
 static ssize_t rtrs_clt_disconnect_show(struct kobject *kobj,
 					 struct kobj_attribute *attr,
-					 char *page)
+					 char *buf)
 {
-	return scnprintf(page, PAGE_SIZE, "Usage: echo 1 > %s\n",
-			 attr->attr.name);
+	return sysfs_emit(buf, "Usage: echo 1 > %s\n",
+			  attr->attr.name);
 }
 
 static ssize_t rtrs_clt_disconnect_store(struct kobject *kobj,
@@ -261,10 +261,10 @@ static struct kobj_attribute rtrs_clt_disconnect_attr =
 
 static ssize_t rtrs_clt_remove_path_show(struct kobject *kobj,
 					  struct kobj_attribute *attr,
-					  char *page)
+					  char *buf)
 {
-	return scnprintf(page, PAGE_SIZE, "Usage: echo 1 > %s\n",
-			 attr->attr.name);
+	return sysfs_emit(buf, "Usage: echo 1 > %s\n",
+			  attr->attr.name);
 }
 
 static ssize_t rtrs_clt_remove_path_store(struct kobject *kobj,
@@ -327,7 +327,7 @@ static ssize_t rtrs_clt_hca_port_show(struct kobject *kobj,
 
 	sess = container_of(kobj, typeof(*sess), kobj);
 
-	return scnprintf(page, PAGE_SIZE, "%u\n", sess->hca_port);
+	return sysfs_emit(page, "%u\n", sess->hca_port);
 }
 
 static struct kobj_attribute rtrs_clt_hca_port_attr =
@@ -341,7 +341,7 @@ static ssize_t rtrs_clt_hca_name_show(struct kobject *kobj,
 
 	sess = container_of(kobj, struct rtrs_clt_sess, kobj);
 
-	return scnprintf(page, PAGE_SIZE, "%s\n", sess->hca_name);
+	return sysfs_emit(page, "%s\n", sess->hca_name);
 }
 
 static struct kobj_attribute rtrs_clt_hca_name_attr =
diff --git a/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c b/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
index 07fbb063555d..381a776ce404 100644
--- a/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
+++ b/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
@@ -28,10 +28,10 @@ static struct kobj_type ktype = {
 
 static ssize_t rtrs_srv_disconnect_show(struct kobject *kobj,
 					 struct kobj_attribute *attr,
-					 char *page)
+					 char *buf)
 {
-	return scnprintf(page, PAGE_SIZE, "Usage: echo 1 > %s\n",
-			 attr->attr.name);
+	return sysfs_emit(buf, "Usage: echo 1 > %s\n",
+			  attr->attr.name);
 }
 
 static ssize_t rtrs_srv_disconnect_store(struct kobject *kobj,
@@ -72,8 +72,8 @@ static ssize_t rtrs_srv_hca_port_show(struct kobject *kobj,
 	sess = container_of(kobj, typeof(*sess), kobj);
 	usr_con = sess->s.con[0];
 
-	return scnprintf(page, PAGE_SIZE, "%u\n",
-			 usr_con->cm_id->port_num);
+	return sysfs_emit(page, "%u\n",
+			  usr_con->cm_id->port_num);
 }
 
 static struct kobj_attribute rtrs_srv_hca_port_attr =
@@ -87,8 +87,8 @@ static ssize_t rtrs_srv_hca_name_show(struct kobject *kobj,
 
 	sess = container_of(kobj, struct rtrs_srv_sess, kobj);
 
-	return scnprintf(page, PAGE_SIZE, "%s\n",
-			 sess->s.dev->ib_dev->name);
+	return sysfs_emit(page, "%s\n",
+			  sess->s.dev->ib_dev->name);
 }
 
 static struct kobj_attribute rtrs_srv_hca_name_attr =
-- 
2.26.0


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH 3/4] RDMA: manual changes for sysfs_emit and neatening
  2020-10-08  2:36 [PATCH-next 0/4] RDMA: sprintf to sysfs_emit conversions Joe Perches
  2020-10-08  2:36 ` [PATCH 1/4] RDMA: Convert sysfs device * show functions to use sysfs_emit() Joe Perches
  2020-10-08  2:36 ` [PATCH 2/4] RDMA: Convert sysfs kobject " Joe Perches
@ 2020-10-08  2:36 ` Joe Perches
  2020-10-09 15:20   ` Jason Gunthorpe
                     ` (2 more replies)
  2020-10-08  2:36 ` [PATCH 4/4] RDMA: Convert various random sprintf sysfs _show uses to sysfs_emit Joe Perches
                   ` (3 subsequent siblings)
  6 siblings, 3 replies; 26+ messages in thread
From: Joe Perches @ 2020-10-08  2:36 UTC (permalink / raw)
  To: Doug Ledford, Jason Gunthorpe, Potnuri Bharat Teja,
	Mike Marciniszyn, Dennis Dalessandro, Yishai Hadas,
	Leon Romanovsky, Christian Benvenuti, Nelson Escobar,
	Parvi Kaustubhi, Danil Kipnis, Jack Wang, Bart Van Assche
  Cc: linux-rdma

Make changes to use sysfs_emit in the RDMA code as cocci scripts can not
be written to handle _all_ the possible variants of various sprintf family
uses in sysfs show functions.

While there, make the code more legible and update its style to be more
like the typical kernel styles.

Miscellanea:

o Use intermediate pointers for dereferences
o Add and use string lookup functions
o return early when any intermediate call fails so normal return is
  at the bottom of the function
o mlx4/mcg.c:sysfs_show_group: use scnprintf to format intermediate strings

Signed-off-by: Joe Perches <joe@perches.com>
---
 drivers/infiniband/core/sysfs.c              | 60 +++++++-------
 drivers/infiniband/hw/cxgb4/provider.c       |  5 +-
 drivers/infiniband/hw/hfi1/sysfs.c           | 38 ++++-----
 drivers/infiniband/hw/mlx4/main.c            |  5 +-
 drivers/infiniband/hw/mlx4/mcg.c             | 82 +++++++++++---------
 drivers/infiniband/hw/mlx4/sysfs.c           | 47 ++++++-----
 drivers/infiniband/hw/mlx5/main.c            |  4 +-
 drivers/infiniband/hw/mthca/mthca_provider.c | 29 ++++---
 drivers/infiniband/hw/qib/qib_sysfs.c        | 45 +++++------
 drivers/infiniband/hw/usnic/usnic_ib_sysfs.c | 66 +++++++---------
 drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c | 21 +++--
 drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c | 13 ++--
 drivers/infiniband/ulp/srp/ib_srp.c          |  4 +
 13 files changed, 206 insertions(+), 213 deletions(-)

diff --git a/drivers/infiniband/core/sysfs.c b/drivers/infiniband/core/sysfs.c
index 79327200da70..cbc8326b935d 100644
--- a/drivers/infiniband/core/sysfs.c
+++ b/drivers/infiniband/core/sysfs.c
@@ -1224,30 +1224,27 @@ static int add_port(struct ib_core_device *coredev, int port_num)
 	return ret;
 }
 
+static const char *node_type_string(int node_type)
+{
+	switch (node_type) {
+	case RDMA_NODE_IB_CA:		return "CA";
+	case RDMA_NODE_IB_SWITCH:	return "switch";
+	case RDMA_NODE_IB_ROUTER:	return "router";
+	case RDMA_NODE_RNIC:		return "RNIC";
+	case RDMA_NODE_USNIC:		return "usNIC";
+	case RDMA_NODE_USNIC_UDP:	return "usNIC UDP";
+	case RDMA_NODE_UNSPECIFIED:	return "unspecified";
+	}
+	return "<unknown>";
+}
+
 static ssize_t node_type_show(struct device *device,
 			      struct device_attribute *attr, char *buf)
 {
 	struct ib_device *dev = rdma_device_to_ibdev(device);
 
-	switch (dev->node_type) {
-	case RDMA_NODE_IB_CA:	  return sysfs_emit(buf, "%d: CA\n",
-							 dev->node_type);
-	case RDMA_NODE_RNIC:	  return sysfs_emit(buf, "%d: RNIC\n",
-							dev->node_type);
-	case RDMA_NODE_USNIC:	  return sysfs_emit(buf, "%d: usNIC\n",
-							 dev->node_type);
-	case RDMA_NODE_USNIC_UDP: return sysfs_emit(buf, "%d: usNIC UDP\n",
-						    dev->node_type);
-	case RDMA_NODE_UNSPECIFIED: return sysfs_emit(buf,
-						      "%d: unspecified\n",
-						      dev->node_type);
-	case RDMA_NODE_IB_SWITCH: return sysfs_emit(buf, "%d: switch\n",
-						    dev->node_type);
-	case RDMA_NODE_IB_ROUTER: return sysfs_emit(buf, "%d: router\n",
-						    dev->node_type);
-	default:		  return sysfs_emit(buf, "%d: <unknown>\n",
-						    dev->node_type);
-	}
+	return sysfs_emit(buf, "%d: %s\n",
+			  dev->node_type, node_type_string(dev->node_type));
 }
 static DEVICE_ATTR_RO(node_type);
 
@@ -1255,12 +1252,13 @@ static ssize_t sys_image_guid_show(struct device *device,
 				   struct device_attribute *dev_attr, char *buf)
 {
 	struct ib_device *dev = rdma_device_to_ibdev(device);
+	__be16 *guid = (__be16 *)&dev->attrs.sys_image_guid;
 
 	return sysfs_emit(buf, "%04x:%04x:%04x:%04x\n",
-			  be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[0]),
-			  be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[1]),
-			  be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[2]),
-			  be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[3]));
+			  be16_to_cpu(guid[0]),
+			  be16_to_cpu(guid[1]),
+			  be16_to_cpu(guid[2]),
+			  be16_to_cpu(guid[3]));
 }
 static DEVICE_ATTR_RO(sys_image_guid);
 
@@ -1268,12 +1266,13 @@ static ssize_t node_guid_show(struct device *device,
 			      struct device_attribute *attr, char *buf)
 {
 	struct ib_device *dev = rdma_device_to_ibdev(device);
+	__be16 *node_guid = (__be16 *)&dev->node_guid;
 
 	return sysfs_emit(buf, "%04x:%04x:%04x:%04x\n",
-			  be16_to_cpu(((__be16 *) &dev->node_guid)[0]),
-			  be16_to_cpu(((__be16 *) &dev->node_guid)[1]),
-			  be16_to_cpu(((__be16 *) &dev->node_guid)[2]),
-			  be16_to_cpu(((__be16 *) &dev->node_guid)[3]));
+			  be16_to_cpu(node_guid[0]),
+			  be16_to_cpu(node_guid[1]),
+			  be16_to_cpu(node_guid[2]),
+			  be16_to_cpu(node_guid[3]));
 }
 static DEVICE_ATTR_RO(node_guid);
 
@@ -1309,10 +1308,11 @@ static ssize_t fw_ver_show(struct device *device, struct device_attribute *attr,
 			   char *buf)
 {
 	struct ib_device *dev = rdma_device_to_ibdev(device);
+	char version[IB_FW_VERSION_NAME_MAX] = {};
+
+	ib_get_device_fw_str(dev, version);
 
-	ib_get_device_fw_str(dev, buf);
-	strlcat(buf, "\n", IB_FW_VERSION_NAME_MAX);
-	return strlen(buf);
+	return sysfs_emit(buf, "%s\n", version);
 }
 static DEVICE_ATTR_RO(fw_ver);
 
diff --git a/drivers/infiniband/hw/cxgb4/provider.c b/drivers/infiniband/hw/cxgb4/provider.c
index b312a7ec40bf..080f34625145 100644
--- a/drivers/infiniband/hw/cxgb4/provider.c
+++ b/drivers/infiniband/hw/cxgb4/provider.c
@@ -337,6 +337,7 @@ static ssize_t hca_type_show(struct device *dev,
 
 	pr_debug("dev 0x%p\n", dev);
 	lldev->ethtool_ops->get_drvinfo(lldev, &info);
+
 	return sysfs_emit(buf, "%s\n", info.driver);
 }
 static DEVICE_ATTR_RO(hca_type);
@@ -348,7 +349,9 @@ static ssize_t board_id_show(struct device *dev, struct device_attribute *attr,
 			rdma_device_to_drv_device(dev, struct c4iw_dev, ibdev);
 
 	pr_debug("dev 0x%p\n", dev);
-	return sysfs_emit(buf, "%x.%x\n", c4iw_dev->rdev.lldi.pdev->vendor,
+
+	return sysfs_emit(buf, "%x.%x\n",
+			  c4iw_dev->rdev.lldi.pdev->vendor,
 			  c4iw_dev->rdev.lldi.pdev->device);
 }
 static DEVICE_ATTR_RO(board_id);
diff --git a/drivers/infiniband/hw/hfi1/sysfs.c b/drivers/infiniband/hw/hfi1/sysfs.c
index e2a88f3cea7f..6b545f0f065f 100644
--- a/drivers/infiniband/hw/hfi1/sysfs.c
+++ b/drivers/infiniband/hw/hfi1/sysfs.c
@@ -510,13 +510,11 @@ static ssize_t board_id_show(struct device *device,
 	struct hfi1_ibdev *dev =
 		rdma_device_to_drv_device(device, struct hfi1_ibdev, rdi.ibdev);
 	struct hfi1_devdata *dd = dd_from_dev(dev);
-	int ret;
 
 	if (!dd->boardname)
-		ret = -EINVAL;
-	else
-		ret = sysfs_emit(buf, "%s\n", dd->boardname);
-	return ret;
+		return -EINVAL;
+
+	return sysfs_emit(buf, "%s\n", dd->boardname);
 }
 static DEVICE_ATTR_RO(board_id);
 
@@ -570,6 +568,7 @@ static ssize_t serial_show(struct device *device,
 		rdma_device_to_drv_device(device, struct hfi1_ibdev, rdi.ibdev);
 	struct hfi1_devdata *dd = dd_from_dev(dev);
 
+	/* dd->serial is already newline terminated in chip.c */
 	return sysfs_emit(buf, "%s", dd->serial);
 }
 static DEVICE_ATTR_RO(serial);
@@ -598,9 +597,8 @@ static DEVICE_ATTR_WO(chip_reset);
  * Convert the reported temperature from an integer (reported in
  * units of 0.25C) to a floating point number.
  */
-#define temp2str(temp, buf, size, idx)					\
-	scnprintf((buf) + (idx), (size) - (idx), "%u.%02u ",		\
-			      ((temp) >> 2), ((temp) & 0x3) * 25)
+#define temp_d(t)	((t) >> 2)
+#define temp_f(t)	(((t) & 0x3) * 25u)
 
 /*
  * Dump tempsense values, in decimal, to ease shell-scripts.
@@ -615,19 +613,17 @@ static ssize_t tempsense_show(struct device *device,
 	int ret;
 
 	ret = hfi1_tempsense_rd(dd, &temp);
-	if (!ret) {
-		int idx = 0;
-
-		idx += temp2str(temp.curr, buf, PAGE_SIZE, idx);
-		idx += temp2str(temp.lo_lim, buf, PAGE_SIZE, idx);
-		idx += temp2str(temp.hi_lim, buf, PAGE_SIZE, idx);
-		idx += temp2str(temp.crit_lim, buf, PAGE_SIZE, idx);
-		idx += scnprintf(buf + idx, PAGE_SIZE - idx,
-				"%u %u %u\n", temp.triggers & 0x1,
-				temp.triggers & 0x2, temp.triggers & 0x4);
-		ret = idx;
-	}
-	return ret;
+	if (ret)
+		return ret;
+
+	return sysfs_emit(buf, "%u.%02u %u.%02u %u.%02u %u.%02u %u %u %u\n",
+			  temp_d(temp.curr), temp_f(temp.curr),
+			  temp_d(temp.lo_lim), temp_f(temp.lo_lim),
+			  temp_d(temp.hi_lim), temp_f(temp.hi_lim),
+			  temp_d(temp.crit_lim), temp_f(temp.crit_lim),
+			  temp.triggers & 0x1,
+			  temp.triggers & 0x2,
+			  temp.triggers & 0x4);
 }
 static DEVICE_ATTR_RO(tempsense);
 
diff --git a/drivers/infiniband/hw/mlx4/main.c b/drivers/infiniband/hw/mlx4/main.c
index ad3c8f985921..ce4f8e263c58 100644
--- a/drivers/infiniband/hw/mlx4/main.c
+++ b/drivers/infiniband/hw/mlx4/main.c
@@ -2024,6 +2024,7 @@ static ssize_t hca_type_show(struct device *device,
 {
 	struct mlx4_ib_dev *dev =
 		rdma_device_to_drv_device(device, struct mlx4_ib_dev, ib_dev);
+
 	return sysfs_emit(buf, "MT%d\n", dev->dev->persist->pdev->device);
 }
 static DEVICE_ATTR_RO(hca_type);
@@ -2033,6 +2034,7 @@ static ssize_t hw_rev_show(struct device *device,
 {
 	struct mlx4_ib_dev *dev =
 		rdma_device_to_drv_device(device, struct mlx4_ib_dev, ib_dev);
+
 	return sysfs_emit(buf, "%x\n", dev->dev->rev_id);
 }
 static DEVICE_ATTR_RO(hw_rev);
@@ -2043,8 +2045,7 @@ static ssize_t board_id_show(struct device *device,
 	struct mlx4_ib_dev *dev =
 		rdma_device_to_drv_device(device, struct mlx4_ib_dev, ib_dev);
 
-	return sysfs_emit(buf, "%.*s\n", MLX4_BOARD_ID_LEN,
-			  dev->dev->board_id);
+	return sysfs_emit(buf, "%.*s\n", MLX4_BOARD_ID_LEN, dev->dev->board_id);
 }
 static DEVICE_ATTR_RO(board_id);
 
diff --git a/drivers/infiniband/hw/mlx4/mcg.c b/drivers/infiniband/hw/mlx4/mcg.c
index 5e4ec9786081..e4e480bffdfa 100644
--- a/drivers/infiniband/hw/mlx4/mcg.c
+++ b/drivers/infiniband/hw/mlx4/mcg.c
@@ -988,53 +988,63 @@ int mlx4_ib_mcg_multiplex_handler(struct ib_device *ibdev, int port,
 }
 
 static ssize_t sysfs_show_group(struct device *dev,
-		struct device_attribute *attr, char *buf)
+				struct device_attribute *attr, char *buf)
 {
 	struct mcast_group *group =
 		container_of(attr, struct mcast_group, dentry);
 	struct mcast_req *req = NULL;
-	char pending_str[40];
 	char state_str[40];
-	ssize_t len = 0;
-	int f;
+	char pending_str[40];
+	int len;
+	int i;
+	u32 hoplimit;
 
 	if (group->state == MCAST_IDLE)
-		sprintf(state_str, "%s", get_state_string(group->state));
+		scnprintf(state_str, sizeof(state_str), "%s",
+			  get_state_string(group->state));
 	else
-		sprintf(state_str, "%s(TID=0x%llx)",
-				get_state_string(group->state),
-				be64_to_cpu(group->last_req_tid));
+		scnprintf(state_str, sizeof(state_str), "%s(TID=0x%llx)",
+			  get_state_string(group->state),
+			  be64_to_cpu(group->last_req_tid));
+
 	if (list_empty(&group->pending_list)) {
-		sprintf(pending_str, "No");
+		scnprintf(pending_str, sizeof(pending_str), "No");
 	} else {
-		req = list_first_entry(&group->pending_list, struct mcast_req, group_list);
-		sprintf(pending_str, "Yes(TID=0x%llx)",
-				be64_to_cpu(req->sa_mad.mad_hdr.tid));
+		req = list_first_entry(&group->pending_list,
+				       struct mcast_req, group_list);
+		scnprintf(pending_str, sizeof(pending_str), "Yes(TID=0x%llx)",
+			  be64_to_cpu(req->sa_mad.mad_hdr.tid));
+	}
+
+	len = sysfs_emit(buf, "%1d [%02d,%02d,%02d] %4d %4s %5s     ",
+			 group->rec.scope_join_state & 0xf,
+			 group->members[2],
+			 group->members[1],
+			 group->members[0],
+			 atomic_read(&group->refcount),
+			 pending_str,
+			 state_str);
+
+	for (i = 0; i < MAX_VFS; i++) {
+		if (group->func[i].state == MCAST_MEMBER)
+			len += sysfs_emit_at(buf, len, "%d[%1x] ",
+					     i, group->func[i].join_state);
 	}
-	len += sprintf(buf + len, "%1d [%02d,%02d,%02d] %4d %4s %5s     ",
-			group->rec.scope_join_state & 0xf,
-			group->members[2], group->members[1], group->members[0],
-			atomic_read(&group->refcount),
-			pending_str,
-			state_str);
-	for (f = 0; f < MAX_VFS; ++f)
-		if (group->func[f].state == MCAST_MEMBER)
-			len += sprintf(buf + len, "%d[%1x] ",
-					f, group->func[f].join_state);
-
-	len += sprintf(buf + len, "\t\t(%4hx %4x %2x %2x %2x %2x %2x "
-		"%4x %4x %2x %2x)\n",
-		be16_to_cpu(group->rec.pkey),
-		be32_to_cpu(group->rec.qkey),
-		(group->rec.mtusel_mtu & 0xc0) >> 6,
-		group->rec.mtusel_mtu & 0x3f,
-		group->rec.tclass,
-		(group->rec.ratesel_rate & 0xc0) >> 6,
-		group->rec.ratesel_rate & 0x3f,
-		(be32_to_cpu(group->rec.sl_flowlabel_hoplimit) & 0xf0000000) >> 28,
-		(be32_to_cpu(group->rec.sl_flowlabel_hoplimit) & 0x0fffff00) >> 8,
-		be32_to_cpu(group->rec.sl_flowlabel_hoplimit) & 0x000000ff,
-		group->rec.proxy_join);
+
+	hoplimit = be32_to_cpu(group->rec.sl_flowlabel_hoplimit);
+	len += sysfs_emit_at(buf, len,
+			     "\t\t(%4hx %4x %2x %2x %2x %2x %2x %4x %4x %2x %2x)\n",
+			     be16_to_cpu(group->rec.pkey),
+			     be32_to_cpu(group->rec.qkey),
+			     (group->rec.mtusel_mtu & 0xc0) >> 6,
+			     (group->rec.mtusel_mtu & 0x3f),
+			     group->rec.tclass,
+			     (group->rec.ratesel_rate & 0xc0) >> 6,
+			     (group->rec.ratesel_rate & 0x3f),
+			     (hoplimit & 0xf0000000) >> 28,
+			     (hoplimit & 0x0fffff00) >> 8,
+			     (hoplimit & 0x000000ff),
+			     group->rec.proxy_join);
 
 	return len;
 }
diff --git a/drivers/infiniband/hw/mlx4/sysfs.c b/drivers/infiniband/hw/mlx4/sysfs.c
index 26c717a4911f..75d50383da89 100644
--- a/drivers/infiniband/hw/mlx4/sysfs.c
+++ b/drivers/infiniband/hw/mlx4/sysfs.c
@@ -117,22 +117,25 @@ static ssize_t show_port_gid(struct device *dev,
 	struct mlx4_ib_iov_port *port = mlx4_ib_iov_dentry->ctx;
 	struct mlx4_ib_dev *mdev = port->dev;
 	union ib_gid gid;
-	ssize_t ret;
+	int ret;
+	__be16 *raw;
 
 	ret = __mlx4_ib_query_gid(&mdev->ib_dev, port->num,
 				  mlx4_ib_iov_dentry->entry_num, &gid, 1);
 	if (ret)
 		return ret;
-	ret = sysfs_emit(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
-			 be16_to_cpu(((__be16 *) gid.raw)[0]),
-			 be16_to_cpu(((__be16 *) gid.raw)[1]),
-			 be16_to_cpu(((__be16 *) gid.raw)[2]),
-			 be16_to_cpu(((__be16 *) gid.raw)[3]),
-			 be16_to_cpu(((__be16 *) gid.raw)[4]),
-			 be16_to_cpu(((__be16 *) gid.raw)[5]),
-			 be16_to_cpu(((__be16 *) gid.raw)[6]),
-			 be16_to_cpu(((__be16 *) gid.raw)[7]));
-	return ret;
+
+	raw = (__be16 *)gid.raw;
+
+	return sysfs_emit(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
+			  be16_to_cpu(raw[0]),
+			  be16_to_cpu(raw[1]),
+			  be16_to_cpu(raw[2]),
+			  be16_to_cpu(raw[3]),
+			  be16_to_cpu(raw[4]),
+			  be16_to_cpu(raw[5]),
+			  be16_to_cpu(raw[6]),
+			  be16_to_cpu(raw[7]));
 }
 
 static ssize_t show_phys_port_pkey(struct device *dev,
@@ -542,14 +545,11 @@ static ssize_t sysfs_show_smi_enabled(struct device *dev,
 {
 	struct mlx4_port *p =
 		container_of(attr, struct mlx4_port, smi_enabled);
-	ssize_t len = 0;
 
-	if (mlx4_vf_smi_enabled(p->dev->dev, p->slave, p->port_num))
-		len = sysfs_emit(buf, "%d\n", 1);
-	else
-		len = sysfs_emit(buf, "%d\n", 0);
-
-	return len;
+	return sysfs_emit(buf, "%d\n",
+			  !!mlx4_vf_smi_enabled(p->dev->dev,
+						p->slave,
+						p->port_num));
 }
 
 static ssize_t sysfs_show_enable_smi_admin(struct device *dev,
@@ -558,14 +558,11 @@ static ssize_t sysfs_show_enable_smi_admin(struct device *dev,
 {
 	struct mlx4_port *p =
 		container_of(attr, struct mlx4_port, enable_smi_admin);
-	ssize_t len = 0;
-
-	if (mlx4_vf_get_enable_smi_admin(p->dev->dev, p->slave, p->port_num))
-		len = sysfs_emit(buf, "%d\n", 1);
-	else
-		len = sysfs_emit(buf, "%d\n", 0);
 
-	return len;
+	return sysfs_emit(buf, "%d\n",
+			  !!mlx4_vf_get_enable_smi_admin(p->dev->dev,
+							 p->slave,
+							 p->port_num));
 }
 
 static ssize_t sysfs_store_enable_smi_admin(struct device *dev,
diff --git a/drivers/infiniband/hw/mlx5/main.c b/drivers/infiniband/hw/mlx5/main.c
index 8b9f6f857120..453eaf35dec7 100644
--- a/drivers/infiniband/hw/mlx5/main.c
+++ b/drivers/infiniband/hw/mlx5/main.c
@@ -2669,8 +2669,8 @@ static ssize_t board_id_show(struct device *device,
 	struct mlx5_ib_dev *dev =
 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
 
-	return sysfs_emit(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
-			  dev->mdev->board_id);
+	return sysfs_emit(buf, "%.*s\n",
+			  MLX5_BOARD_ID_LEN, dev->mdev->board_id);
 }
 static DEVICE_ATTR_RO(board_id);
 
diff --git a/drivers/infiniband/hw/mthca/mthca_provider.c b/drivers/infiniband/hw/mthca/mthca_provider.c
index b6e4254e4558..fd5d4e1e5b8b 100644
--- a/drivers/infiniband/hw/mthca/mthca_provider.c
+++ b/drivers/infiniband/hw/mthca/mthca_provider.c
@@ -965,25 +965,30 @@ static ssize_t hw_rev_show(struct device *device,
 }
 static DEVICE_ATTR_RO(hw_rev);
 
-static ssize_t hca_type_show(struct device *device,
-			     struct device_attribute *attr, char *buf)
+static const char *hca_type_string(int hca_type)
 {
-	struct mthca_dev *dev =
-		rdma_device_to_drv_device(device, struct mthca_dev, ib_dev);
-
-	switch (dev->pdev->device) {
+	switch (hca_type) {
 	case PCI_DEVICE_ID_MELLANOX_TAVOR:
-		return sysfs_emit(buf, "MT23108\n");
+		return "MT23108";
 	case PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT:
-		return sysfs_emit(buf, "MT25208 (MT23108 compat mode)\n");
+		return "MT25208 (MT23108 compat mode)";
 	case PCI_DEVICE_ID_MELLANOX_ARBEL:
-		return sysfs_emit(buf, "MT25208\n");
+		return "MT25208";
 	case PCI_DEVICE_ID_MELLANOX_SINAI:
 	case PCI_DEVICE_ID_MELLANOX_SINAI_OLD:
-		return sysfs_emit(buf, "MT25204\n");
-	default:
-		return sysfs_emit(buf, "unknown\n");
+		return "MT25204";
 	}
+
+	return "unknown";
+}
+
+static ssize_t hca_type_show(struct device *device,
+			     struct device_attribute *attr, char *buf)
+{
+	struct mthca_dev *dev =
+		rdma_device_to_drv_device(device, struct mthca_dev, ib_dev);
+
+	return sysfs_emit(buf, "%s\n", hca_type_string(dev->pdev->device));
 }
 static DEVICE_ATTR_RO(hca_type);
 
diff --git a/drivers/infiniband/hw/qib/qib_sysfs.c b/drivers/infiniband/hw/qib/qib_sysfs.c
index 2963915d0851..02ad73ffcd5e 100644
--- a/drivers/infiniband/hw/qib/qib_sysfs.c
+++ b/drivers/infiniband/hw/qib/qib_sysfs.c
@@ -575,13 +575,11 @@ static ssize_t hca_type_show(struct device *device,
 	struct qib_ibdev *dev =
 		rdma_device_to_drv_device(device, struct qib_ibdev, rdi.ibdev);
 	struct qib_devdata *dd = dd_from_dev(dev);
-	int ret;
 
 	if (!dd->boardname)
-		ret = -EINVAL;
-	else
-		ret = sysfs_emit(buf, "%s\n", dd->boardname);
-	return ret;
+		return -EINVAL;
+
+	return sysfs_emit(buf, "%s\n", dd->boardname);
 }
 static DEVICE_ATTR_RO(hca_type);
 static DEVICE_ATTR(board_id, 0444, hca_type_show, NULL);
@@ -653,10 +651,7 @@ static ssize_t serial_show(struct device *device,
 		rdma_device_to_drv_device(device, struct qib_ibdev, rdi.ibdev);
 	struct qib_devdata *dd = dd_from_dev(dev);
 
-	buf[sizeof(dd->serial)] = '\0';
-	memcpy(buf, dd->serial, sizeof(dd->serial));
-	strcat(buf, "\n");
-	return strlen(buf);
+	return sysfs_emit(buf, "%s\n", dd->serial);
 }
 static DEVICE_ATTR_RO(serial);
 
@@ -689,27 +684,27 @@ static ssize_t tempsense_show(struct device *device,
 	struct qib_ibdev *dev =
 		rdma_device_to_drv_device(device, struct qib_ibdev, rdi.ibdev);
 	struct qib_devdata *dd = dd_from_dev(dev);
-	int ret;
-	int idx;
+	int i;
 	u8 regvals[8];
 
-	ret = -ENXIO;
-	for (idx = 0; idx < 8; ++idx) {
-		if (idx == 6)
+	for (i = 0; i < 8; i++) {
+		int ret;
+
+		if (i == 6)
 			continue;
-		ret = dd->f_tempsense_rd(dd, idx);
+		ret = dd->f_tempsense_rd(dd, i);
 		if (ret < 0)
-			break;
-		regvals[idx] = ret;
+			return ret;	/* return error on bad read */
+		regvals[i] = ret;
 	}
-	if (idx == 8)
-		ret = sysfs_emit(buf, "%d %d %02X %02X %d %d\n",
-				 *(signed char *)(regvals),
-				 *(signed char *)(regvals + 1),
-				 regvals[2], regvals[3],
-				 *(signed char *)(regvals + 5),
-				 *(signed char *)(regvals + 7));
-	return ret;
+
+	return sysfs_emit(buf, "%d %d %02X %02X %d %d\n",
+			  (signed char)regvals[0],
+			  (signed char)regvals[1],
+			  regvals[2],
+			  regvals[3],
+			  (signed char)regvals[5],
+			  (signed char)regvals[7]);
 }
 static DEVICE_ATTR_RO(tempsense);
 
diff --git a/drivers/infiniband/hw/usnic/usnic_ib_sysfs.c b/drivers/infiniband/hw/usnic/usnic_ib_sysfs.c
index 38da539c61e0..eddad4f0bd2d 100644
--- a/drivers/infiniband/hw/usnic/usnic_ib_sysfs.c
+++ b/drivers/infiniband/hw/usnic/usnic_ib_sysfs.c
@@ -57,7 +57,7 @@ static ssize_t board_id_show(struct device *device,
 	subsystem_device_id = us_ibdev->pdev->subsystem_device;
 	mutex_unlock(&us_ibdev->usdev_lock);
 
-	return sysfs_emit(buf, "%hu\n", subsystem_device_id);
+	return sysfs_emit(buf, "%u\n", subsystem_device_id);
 }
 static DEVICE_ATTR_RO(board_id);
 
@@ -69,19 +69,13 @@ config_show(struct device *device, struct device_attribute *attr, char *buf)
 {
 	struct usnic_ib_dev *us_ibdev =
 		rdma_device_to_drv_device(device, struct usnic_ib_dev, ib_dev);
-	char *ptr;
-	unsigned left;
-	unsigned n;
 	enum usnic_vnic_res_type res_type;
-
-	/* Buffer space limit is 1 page */
-	ptr = buf;
-	left = PAGE_SIZE;
+	int len;
 
 	mutex_lock(&us_ibdev->usdev_lock);
 	if (kref_read(&us_ibdev->vf_cnt) > 0) {
 		char *busname;
-
+		char *sep = "";
 		/*
 		 * bus name seems to come with annoying prefix.
 		 * Remove it if it is predictable
@@ -90,39 +84,36 @@ config_show(struct device *device, struct device_attribute *attr, char *buf)
 		if (strncmp(busname, "PCI Bus ", 8) == 0)
 			busname += 8;
 
-		n = scnprintf(ptr, left,
-			"%s: %s:%d.%d, %s, %pM, %u VFs\n Per VF:",
-			dev_name(&us_ibdev->ib_dev.dev),
-			busname,
-			PCI_SLOT(us_ibdev->pdev->devfn),
-			PCI_FUNC(us_ibdev->pdev->devfn),
-			netdev_name(us_ibdev->netdev),
-			us_ibdev->ufdev->mac,
-			kref_read(&us_ibdev->vf_cnt));
-		UPDATE_PTR_LEFT(n, ptr, left);
+		len = sysfs_emit(buf, "%s: %s:%d.%d, %s, %pM, %u VFs\n",
+				 dev_name(&us_ibdev->ib_dev.dev),
+				 busname,
+				 PCI_SLOT(us_ibdev->pdev->devfn),
+				 PCI_FUNC(us_ibdev->pdev->devfn),
+				 netdev_name(us_ibdev->netdev),
+				 us_ibdev->ufdev->mac,
+				 kref_read(&us_ibdev->vf_cnt));
 
+		len += sysfs_emit_at(buf, len, " Per VF:");
 		for (res_type = USNIC_VNIC_RES_TYPE_EOL;
-				res_type < USNIC_VNIC_RES_TYPE_MAX;
-				res_type++) {
+		     res_type < USNIC_VNIC_RES_TYPE_MAX;
+		     res_type++) {
 			if (us_ibdev->vf_res_cnt[res_type] == 0)
 				continue;
-			n = scnprintf(ptr, left, " %d %s%s",
-				us_ibdev->vf_res_cnt[res_type],
-				usnic_vnic_res_type_to_str(res_type),
-				(res_type < (USNIC_VNIC_RES_TYPE_MAX - 1)) ?
-				 "," : "");
-			UPDATE_PTR_LEFT(n, ptr, left);
+			len += sysfs_emit_at(buf, len, "%s %d %s",
+					     sep,
+					     us_ibdev->vf_res_cnt[res_type],
+					     usnic_vnic_res_type_to_str(res_type));
+			sep = ",";
 		}
-		n = scnprintf(ptr, left, "\n");
-		UPDATE_PTR_LEFT(n, ptr, left);
+		len += sysfs_emit_at(buf, len, "\n");
 	} else {
-		n = scnprintf(ptr, left, "%s: no VFs\n",
-				dev_name(&us_ibdev->ib_dev.dev));
-		UPDATE_PTR_LEFT(n, ptr, left);
+		len = sysfs_emit(buf, "%s: no VFs\n",
+				 dev_name(&us_ibdev->ib_dev.dev));
 	}
+
 	mutex_unlock(&us_ibdev->usdev_lock);
 
-	return ptr - buf;
+	return len;
 }
 static DEVICE_ATTR_RO(config);
 
@@ -132,8 +123,7 @@ iface_show(struct device *device, struct device_attribute *attr, char *buf)
 	struct usnic_ib_dev *us_ibdev =
 		rdma_device_to_drv_device(device, struct usnic_ib_dev, ib_dev);
 
-	return sysfs_emit(buf, "%s\n",
-			  netdev_name(us_ibdev->netdev));
+	return sysfs_emit(buf, "%s\n", netdev_name(us_ibdev->netdev));
 }
 static DEVICE_ATTR_RO(iface);
 
@@ -143,8 +133,7 @@ max_vf_show(struct device *device, struct device_attribute *attr, char *buf)
 	struct usnic_ib_dev *us_ibdev =
 		rdma_device_to_drv_device(device, struct usnic_ib_dev, ib_dev);
 
-	return sysfs_emit(buf, "%u\n",
-			  kref_read(&us_ibdev->vf_cnt));
+	return sysfs_emit(buf, "%u\n", kref_read(&us_ibdev->vf_cnt));
 }
 static DEVICE_ATTR_RO(max_vf);
 
@@ -158,8 +147,7 @@ qp_per_vf_show(struct device *device, struct device_attribute *attr, char *buf)
 	qp_per_vf = max(us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_WQ],
 			us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_RQ]);
 
-	return sysfs_emit(buf,
-				"%d\n", qp_per_vf);
+	return sysfs_emit(buf, "%d\n", qp_per_vf);
 }
 static DEVICE_ATTR_RO(qp_per_vf);
 
diff --git a/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c b/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c
index 0c767582286b..51ba82fc425c 100644
--- a/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c
+++ b/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c
@@ -196,11 +196,10 @@ static struct kobj_attribute rtrs_clt_state_attr =
 	__ATTR(state, 0444, rtrs_clt_state_show, NULL);
 
 static ssize_t rtrs_clt_reconnect_show(struct kobject *kobj,
-					struct kobj_attribute *attr,
-					char *buf)
+				       struct kobj_attribute *attr,
+				       char *buf)
 {
-	return sysfs_emit(buf, "Usage: echo 1 > %s\n",
-			  attr->attr.name);
+	return sysfs_emit(buf, "Usage: echo 1 > %s\n", attr->attr.name);
 }
 
 static ssize_t rtrs_clt_reconnect_store(struct kobject *kobj,
@@ -228,11 +227,10 @@ static struct kobj_attribute rtrs_clt_reconnect_attr =
 	       rtrs_clt_reconnect_store);
 
 static ssize_t rtrs_clt_disconnect_show(struct kobject *kobj,
-					 struct kobj_attribute *attr,
-					 char *buf)
+					struct kobj_attribute *attr,
+					char *buf)
 {
-	return sysfs_emit(buf, "Usage: echo 1 > %s\n",
-			  attr->attr.name);
+	return sysfs_emit(buf, "Usage: echo 1 > %s\n", attr->attr.name);
 }
 
 static ssize_t rtrs_clt_disconnect_store(struct kobject *kobj,
@@ -260,11 +258,10 @@ static struct kobj_attribute rtrs_clt_disconnect_attr =
 	       rtrs_clt_disconnect_store);
 
 static ssize_t rtrs_clt_remove_path_show(struct kobject *kobj,
-					  struct kobj_attribute *attr,
-					  char *buf)
+					 struct kobj_attribute *attr,
+					 char *buf)
 {
-	return sysfs_emit(buf, "Usage: echo 1 > %s\n",
-			  attr->attr.name);
+	return sysfs_emit(buf, "Usage: echo 1 > %s\n", attr->attr.name);
 }
 
 static ssize_t rtrs_clt_remove_path_store(struct kobject *kobj,
diff --git a/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c b/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
index 381a776ce404..6e7bebe4e064 100644
--- a/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
+++ b/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
@@ -27,11 +27,10 @@ static struct kobj_type ktype = {
 };
 
 static ssize_t rtrs_srv_disconnect_show(struct kobject *kobj,
-					 struct kobj_attribute *attr,
-					 char *buf)
+					struct kobj_attribute *attr,
+					char *buf)
 {
-	return sysfs_emit(buf, "Usage: echo 1 > %s\n",
-			  attr->attr.name);
+	return sysfs_emit(buf, "Usage: echo 1 > %s\n", attr->attr.name);
 }
 
 static ssize_t rtrs_srv_disconnect_store(struct kobject *kobj,
@@ -72,8 +71,7 @@ static ssize_t rtrs_srv_hca_port_show(struct kobject *kobj,
 	sess = container_of(kobj, typeof(*sess), kobj);
 	usr_con = sess->s.con[0];
 
-	return sysfs_emit(page, "%u\n",
-			  usr_con->cm_id->port_num);
+	return sysfs_emit(page, "%u\n", usr_con->cm_id->port_num);
 }
 
 static struct kobj_attribute rtrs_srv_hca_port_attr =
@@ -87,8 +85,7 @@ static ssize_t rtrs_srv_hca_name_show(struct kobject *kobj,
 
 	sess = container_of(kobj, struct rtrs_srv_sess, kobj);
 
-	return sysfs_emit(page, "%s\n",
-			  sess->s.dev->ib_dev->name);
+	return sysfs_emit(page, "%s\n", sess->s.dev->ib_dev->name);
 }
 
 static struct kobj_attribute rtrs_srv_hca_name_attr =
diff --git a/drivers/infiniband/ulp/srp/ib_srp.c b/drivers/infiniband/ulp/srp/ib_srp.c
index c9284f767ec1..4a5a4f522fd5 100644
--- a/drivers/infiniband/ulp/srp/ib_srp.c
+++ b/drivers/infiniband/ulp/srp/ib_srp.c
@@ -2925,6 +2925,7 @@ static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
 
 	if (target->using_rdma_cm)
 		return -ENOENT;
+
 	return sysfs_emit(buf, "0x%04x\n", be16_to_cpu(target->ib_cm.pkey));
 }
 
@@ -2944,6 +2945,7 @@ static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
 
 	if (target->using_rdma_cm)
 		return -ENOENT;
+
 	return sysfs_emit(buf, "%pI6\n", ch->ib_cm.path.dgid.raw);
 }
 
@@ -2954,6 +2956,7 @@ static ssize_t show_orig_dgid(struct device *dev,
 
 	if (target->using_rdma_cm)
 		return -ENOENT;
+
 	return sysfs_emit(buf, "%pI6\n", target->ib_cm.orig_dgid.raw);
 }
 
@@ -2968,6 +2971,7 @@ static ssize_t show_req_lim(struct device *dev,
 		ch = &target->ch[i];
 		req_lim = min(req_lim, ch->req_lim);
 	}
+
 	return sysfs_emit(buf, "%d\n", req_lim);
 }
 
-- 
2.26.0


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH 4/4] RDMA: Convert various random sprintf sysfs _show uses to sysfs_emit
  2020-10-08  2:36 [PATCH-next 0/4] RDMA: sprintf to sysfs_emit conversions Joe Perches
                   ` (2 preceding siblings ...)
  2020-10-08  2:36 ` [PATCH 3/4] RDMA: manual changes for sysfs_emit and neatening Joe Perches
@ 2020-10-08  2:36 ` Joe Perches
  2020-10-09 15:27   ` Jason Gunthorpe
                     ` (2 more replies)
  2020-10-08  5:41 ` [PATCH-next 0/4] RDMA: sprintf to sysfs_emit conversions Leon Romanovsky
                   ` (2 subsequent siblings)
  6 siblings, 3 replies; 26+ messages in thread
From: Joe Perches @ 2020-10-08  2:36 UTC (permalink / raw)
  To: Doug Ledford, Jason Gunthorpe, Mike Marciniszyn,
	Dennis Dalessandro, Yishai Hadas, Christian Benvenuti,
	Nelson Escobar, Parvi Kaustubhi, Danil Kipnis, Jack Wang,
	Bart Van Assche
  Cc: linux-rdma

Manual changes for sysfs_emit as cocci scripts can't easily convert them.

Signed-off-by: Joe Perches <joe@perches.com>
---
 drivers/infiniband/core/cm.c                 |  4 +-
 drivers/infiniband/core/cma_configfs.c       |  4 +-
 drivers/infiniband/core/sysfs.c              | 98 +++++++++++---------
 drivers/infiniband/core/user_mad.c           |  2 +-
 drivers/infiniband/hw/hfi1/sysfs.c           | 10 +-
 drivers/infiniband/hw/mlx4/sysfs.c           | 19 ++--
 drivers/infiniband/hw/qib/qib_sysfs.c        | 30 +++---
 drivers/infiniband/hw/usnic/usnic_ib_sysfs.c | 34 +++----
 drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c | 14 +--
 drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c |  7 +-
 drivers/infiniband/ulp/srp/ib_srp.c          |  4 +-
 drivers/infiniband/ulp/srpt/ib_srpt.c        | 14 +--
 12 files changed, 119 insertions(+), 121 deletions(-)

diff --git a/drivers/infiniband/core/cm.c b/drivers/infiniband/core/cm.c
index 5740d1ba3568..020136497459 100644
--- a/drivers/infiniband/core/cm.c
+++ b/drivers/infiniband/core/cm.c
@@ -4271,8 +4271,8 @@ static ssize_t cm_show_counter(struct kobject *obj, struct attribute *attr,
 	group = container_of(obj, struct cm_counter_group, obj);
 	cm_attr = container_of(attr, struct cm_counter_attribute, attr);
 
-	return sprintf(buf, "%ld\n",
-		       atomic_long_read(&group->counter[cm_attr->index]));
+	return sysfs_emit(buf, "%ld\n",
+			  atomic_long_read(&group->counter[cm_attr->index]));
 }
 
 static const struct sysfs_ops cm_counter_ops = {
diff --git a/drivers/infiniband/core/cma_configfs.c b/drivers/infiniband/core/cma_configfs.c
index 7ec4af2ed87a..7f70e5a7de10 100644
--- a/drivers/infiniband/core/cma_configfs.c
+++ b/drivers/infiniband/core/cma_configfs.c
@@ -115,7 +115,7 @@ static ssize_t default_roce_mode_show(struct config_item *item,
 	if (gid_type < 0)
 		return gid_type;
 
-	return sprintf(buf, "%s\n", ib_cache_gid_type_str(gid_type));
+	return sysfs_emit(buf, "%s\n", ib_cache_gid_type_str(gid_type));
 }
 
 static ssize_t default_roce_mode_store(struct config_item *item,
@@ -157,7 +157,7 @@ static ssize_t default_roce_tos_show(struct config_item *item, char *buf)
 	tos = cma_get_default_roce_tos(cma_dev, group->port_num);
 	cma_configfs_params_put(cma_dev);
 
-	return sprintf(buf, "%u\n", tos);
+	return sysfs_emit(buf, "%u\n", tos);
 }
 
 static ssize_t default_roce_tos_store(struct config_item *item,
diff --git a/drivers/infiniband/core/sysfs.c b/drivers/infiniband/core/sysfs.c
index cbc8326b935d..bffed464509d 100644
--- a/drivers/infiniband/core/sysfs.c
+++ b/drivers/infiniband/core/sysfs.c
@@ -165,9 +165,11 @@ static ssize_t state_show(struct ib_port *p, struct port_attribute *unused,
 	if (ret)
 		return ret;
 
-	return sprintf(buf, "%d: %s\n", attr.state,
-		       attr.state >= 0 && attr.state < ARRAY_SIZE(state_name) ?
-		       state_name[attr.state] : "UNKNOWN");
+	return sysfs_emit(buf, "%d: %s\n",
+			  attr.state,
+			  attr.state >= 0 &&
+			  attr.state < ARRAY_SIZE(state_name) ?
+			  state_name[attr.state] : "UNKNOWN");
 }
 
 static ssize_t lid_show(struct ib_port *p, struct port_attribute *unused,
@@ -180,7 +182,7 @@ static ssize_t lid_show(struct ib_port *p, struct port_attribute *unused,
 	if (ret)
 		return ret;
 
-	return sprintf(buf, "0x%x\n", attr.lid);
+	return sysfs_emit(buf, "0x%x\n", attr.lid);
 }
 
 static ssize_t lid_mask_count_show(struct ib_port *p,
@@ -194,7 +196,7 @@ static ssize_t lid_mask_count_show(struct ib_port *p,
 	if (ret)
 		return ret;
 
-	return sprintf(buf, "%d\n", attr.lmc);
+	return sysfs_emit(buf, "%d\n", attr.lmc);
 }
 
 static ssize_t sm_lid_show(struct ib_port *p, struct port_attribute *unused,
@@ -207,7 +209,7 @@ static ssize_t sm_lid_show(struct ib_port *p, struct port_attribute *unused,
 	if (ret)
 		return ret;
 
-	return sprintf(buf, "0x%x\n", attr.sm_lid);
+	return sysfs_emit(buf, "0x%x\n", attr.sm_lid);
 }
 
 static ssize_t sm_sl_show(struct ib_port *p, struct port_attribute *unused,
@@ -220,7 +222,7 @@ static ssize_t sm_sl_show(struct ib_port *p, struct port_attribute *unused,
 	if (ret)
 		return ret;
 
-	return sprintf(buf, "%d\n", attr.sm_sl);
+	return sysfs_emit(buf, "%d\n", attr.sm_sl);
 }
 
 static ssize_t cap_mask_show(struct ib_port *p, struct port_attribute *unused,
@@ -233,7 +235,7 @@ static ssize_t cap_mask_show(struct ib_port *p, struct port_attribute *unused,
 	if (ret)
 		return ret;
 
-	return sprintf(buf, "0x%08x\n", attr.port_cap_flags);
+	return sysfs_emit(buf, "0x%08x\n", attr.port_cap_flags);
 }
 
 static ssize_t rate_show(struct ib_port *p, struct port_attribute *unused,
@@ -284,9 +286,9 @@ static ssize_t rate_show(struct ib_port *p, struct port_attribute *unused,
 	if (rate < 0)
 		return -EINVAL;
 
-	return sprintf(buf, "%d%s Gb/sec (%dX%s)\n",
-		       rate / 10, rate % 10 ? ".5" : "",
-		       ib_width_enum_to_int(attr.active_width), speed);
+	return sysfs_emit(buf, "%d%s Gb/sec (%dX%s)\n",
+			  rate / 10, rate % 10 ? ".5" : "",
+			  ib_width_enum_to_int(attr.active_width), speed);
 }
 
 static const char *phys_state_to_str(enum ib_port_phys_state phys_state)
@@ -318,21 +320,28 @@ static ssize_t phys_state_show(struct ib_port *p, struct port_attribute *unused,
 	if (ret)
 		return ret;
 
-	return sprintf(buf, "%d: %s\n", attr.phys_state,
-		       phys_state_to_str(attr.phys_state));
+	return sysfs_emit(buf, "%d: %s\n",
+			  attr.phys_state, phys_state_to_str(attr.phys_state));
 }
 
 static ssize_t link_layer_show(struct ib_port *p, struct port_attribute *unused,
 			       char *buf)
 {
+	const char *output;
+
 	switch (rdma_port_get_link_layer(p->ibdev, p->port_num)) {
 	case IB_LINK_LAYER_INFINIBAND:
-		return sprintf(buf, "%s\n", "InfiniBand");
+		output = "InfiniBand";
+		break;
 	case IB_LINK_LAYER_ETHERNET:
-		return sprintf(buf, "%s\n", "Ethernet");
+		output = "Ethernet";
+		break;
 	default:
-		return sprintf(buf, "%s\n", "Unknown");
+		output = "Unknown";
+		break;
 	}
+
+	return sysfs_emit(buf, "%s\n", output);
 }
 
 static PORT_ATTR_RO(state);
@@ -358,27 +367,28 @@ static struct attribute *port_default_attrs[] = {
 	NULL
 };
 
-static size_t print_ndev(const struct ib_gid_attr *gid_attr, char *buf)
+static ssize_t print_ndev(const struct ib_gid_attr *gid_attr, char *buf)
 {
 	struct net_device *ndev;
-	size_t ret = -EINVAL;
+	int ret = -EINVAL;
 
 	rcu_read_lock();
 	ndev = rcu_dereference(gid_attr->ndev);
 	if (ndev)
-		ret = sprintf(buf, "%s\n", ndev->name);
+		ret = sysfs_emit(buf, "%s\n", ndev->name);
 	rcu_read_unlock();
 	return ret;
 }
 
-static size_t print_gid_type(const struct ib_gid_attr *gid_attr, char *buf)
+static ssize_t print_gid_type(const struct ib_gid_attr *gid_attr, char *buf)
 {
-	return sprintf(buf, "%s\n", ib_cache_gid_type_str(gid_attr->gid_type));
+	return sysfs_emit(buf, "%s\n",
+			  ib_cache_gid_type_str(gid_attr->gid_type));
 }
 
 static ssize_t _show_port_gid_attr(
 	struct ib_port *p, struct port_attribute *attr, char *buf,
-	size_t (*print)(const struct ib_gid_attr *gid_attr, char *buf))
+	ssize_t (*print)(const struct ib_gid_attr *gid_attr, char *buf))
 {
 	struct port_table_attribute *tab_attr =
 		container_of(attr, struct port_table_attribute, attr);
@@ -401,7 +411,7 @@ static ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr,
 	struct port_table_attribute *tab_attr =
 		container_of(attr, struct port_table_attribute, attr);
 	const struct ib_gid_attr *gid_attr;
-	ssize_t ret;
+	int len;
 
 	gid_attr = rdma_get_gid_attr(p->ibdev, p->port_num, tab_attr->index);
 	if (IS_ERR(gid_attr)) {
@@ -416,12 +426,12 @@ static ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr,
 		 * space throwing such error on fail to read gid, return zero
 		 * GID as before. This maintains backward compatibility.
 		 */
-		return sprintf(buf, "%pI6\n", zgid.raw);
+		return sysfs_emit(buf, "%pI6\n", zgid.raw);
 	}
 
-	ret = sprintf(buf, "%pI6\n", gid_attr->gid.raw);
+	len = sysfs_emit(buf, "%pI6\n", gid_attr->gid.raw);
 	rdma_put_gid_attr(gid_attr);
-	return ret;
+	return len;
 }
 
 static ssize_t show_port_gid_attr_ndev(struct ib_port *p,
@@ -443,13 +453,13 @@ static ssize_t show_port_pkey(struct ib_port *p, struct port_attribute *attr,
 	struct port_table_attribute *tab_attr =
 		container_of(attr, struct port_table_attribute, attr);
 	u16 pkey;
-	ssize_t ret;
+	int ret;
 
 	ret = ib_query_pkey(p->ibdev, p->port_num, tab_attr->index, &pkey);
 	if (ret)
 		return ret;
 
-	return sprintf(buf, "0x%04x\n", pkey);
+	return sysfs_emit(buf, "0x%04x\n", pkey);
 }
 
 #define PORT_PMA_ATTR(_name, _counter, _width, _offset)			\
@@ -521,8 +531,9 @@ static ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
 		container_of(attr, struct port_table_attribute, attr);
 	int offset = tab_attr->index & 0xffff;
 	int width  = (tab_attr->index >> 16) & 0xff;
-	ssize_t ret;
+	int ret;
 	u8 data[8];
+	int len;
 
 	ret = get_perf_mad(p->ibdev, p->port_num, tab_attr->attr_id, &data,
 			40 + offset / 8, sizeof(data));
@@ -531,30 +542,27 @@ static ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
 
 	switch (width) {
 	case 4:
-		ret = sprintf(buf, "%u\n", (*data >>
-					    (4 - (offset % 8))) & 0xf);
+		len = sysfs_emit(buf, "%u\n",
+				 (*data >> (4 - (offset % 8))) & 0xf);
 		break;
 	case 8:
-		ret = sprintf(buf, "%u\n", *data);
+		len = sysfs_emit(buf, "%u\n", *data);
 		break;
 	case 16:
-		ret = sprintf(buf, "%u\n",
-			      be16_to_cpup((__be16 *)data));
+		len = sysfs_emit(buf, "%u\n", be16_to_cpup((__be16 *)data));
 		break;
 	case 32:
-		ret = sprintf(buf, "%u\n",
-			      be32_to_cpup((__be32 *)data));
+		len = sysfs_emit(buf, "%u\n", be32_to_cpup((__be32 *)data));
 		break;
 	case 64:
-		ret = sprintf(buf, "%llu\n",
-				be64_to_cpup((__be64 *)data));
+		len = sysfs_emit(buf, "%llu\n", be64_to_cpup((__be64 *)data));
 		break;
-
 	default:
-		ret = 0;
+		len = 0;
+		break;
 	}
 
-	return ret;
+	return len;
 }
 
 static PORT_PMA_ATTR(symbol_error		    ,  0, 16,  32);
@@ -815,12 +823,12 @@ static int update_hw_stats(struct ib_device *dev, struct rdma_hw_stats *stats,
 	return 0;
 }
 
-static ssize_t print_hw_stat(struct ib_device *dev, int port_num,
-			     struct rdma_hw_stats *stats, int index, char *buf)
+static int print_hw_stat(struct ib_device *dev, int port_num,
+			 struct rdma_hw_stats *stats, int index, char *buf)
 {
 	u64 v = rdma_counter_get_hwstat_value(dev, port_num, index);
 
-	return sprintf(buf, "%llu\n", stats->value[index] + v);
+	return sysfs_emit(buf, "%llu\n", stats->value[index] + v);
 }
 
 static ssize_t show_hw_stats(struct kobject *kobj, struct attribute *attr,
@@ -877,7 +885,7 @@ static ssize_t show_stats_lifespan(struct kobject *kobj,
 	msecs = jiffies_to_msecs(stats->lifespan);
 	mutex_unlock(&stats->lock);
 
-	return sprintf(buf, "%d\n", msecs);
+	return sysfs_emit(buf, "%d\n", msecs);
 }
 
 static ssize_t set_stats_lifespan(struct kobject *kobj,
diff --git a/drivers/infiniband/core/user_mad.c b/drivers/infiniband/core/user_mad.c
index 7e759f5b2a75..19104a675691 100644
--- a/drivers/infiniband/core/user_mad.c
+++ b/drivers/infiniband/core/user_mad.c
@@ -1222,7 +1222,7 @@ static char *umad_devnode(struct device *dev, umode_t *mode)
 static ssize_t abi_version_show(struct class *class,
 				struct class_attribute *attr, char *buf)
 {
-	return sprintf(buf, "%d\n", IB_USER_MAD_ABI_VERSION);
+	return sysfs_emit(buf, "%d\n", IB_USER_MAD_ABI_VERSION);
 }
 static CLASS_ATTR_RO(abi_version);
 
diff --git a/drivers/infiniband/hw/hfi1/sysfs.c b/drivers/infiniband/hw/hfi1/sysfs.c
index 6b545f0f065f..dcea59e0721a 100644
--- a/drivers/infiniband/hw/hfi1/sysfs.c
+++ b/drivers/infiniband/hw/hfi1/sysfs.c
@@ -151,7 +151,7 @@ struct hfi1_port_attr {
 
 static ssize_t cc_prescan_show(struct hfi1_pportdata *ppd, char *buf)
 {
-	return sprintf(buf, "%s\n", ppd->cc_prescan ? "on" : "off");
+	return sysfs_emit(buf, "%s\n", ppd->cc_prescan ? "on" : "off");
 }
 
 static ssize_t cc_prescan_store(struct hfi1_pportdata *ppd, const char *buf,
@@ -296,7 +296,7 @@ static ssize_t sc2vl_attr_show(struct kobject *kobj, struct attribute *attr,
 		container_of(kobj, struct hfi1_pportdata, sc2vl_kobj);
 	struct hfi1_devdata *dd = ppd->dd;
 
-	return sprintf(buf, "%u\n", *((u8 *)dd->sc2vl + sattr->sc));
+	return sysfs_emit(buf, "%u\n", *((u8 *)dd->sc2vl + sattr->sc));
 }
 
 static const struct sysfs_ops hfi1_sc2vl_ops = {
@@ -401,7 +401,7 @@ static ssize_t sl2sc_attr_show(struct kobject *kobj, struct attribute *attr,
 		container_of(kobj, struct hfi1_pportdata, sl2sc_kobj);
 	struct hfi1_ibport *ibp = &ppd->ibport_data;
 
-	return sprintf(buf, "%u\n", ibp->sl_to_sc[sattr->sl]);
+	return sysfs_emit(buf, "%u\n", ibp->sl_to_sc[sattr->sl]);
 }
 
 static const struct sysfs_ops hfi1_sl2sc_ops = {
@@ -475,7 +475,7 @@ static ssize_t vl2mtu_attr_show(struct kobject *kobj, struct attribute *attr,
 		container_of(kobj, struct hfi1_pportdata, vl2mtu_kobj);
 	struct hfi1_devdata *dd = ppd->dd;
 
-	return sprintf(buf, "%u\n", dd->vld[vlattr->vl].mtu);
+	return sysfs_emit(buf, "%u\n", dd->vld[vlattr->vl].mtu);
 }
 
 static const struct sysfs_ops hfi1_vl2mtu_ops = {
@@ -813,7 +813,7 @@ static ssize_t sde_show_vl(struct sdma_engine *sde, char *buf)
 	if (vl < 0)
 		return vl;
 
-	return snprintf(buf, PAGE_SIZE, "%d\n", vl);
+	return sysfs_emit(buf, "%d\n", vl);
 }
 
 static SDE_ATTR(cpu_list, S_IWUSR | S_IRUGO,
diff --git a/drivers/infiniband/hw/mlx4/sysfs.c b/drivers/infiniband/hw/mlx4/sysfs.c
index 75d50383da89..fc6c98b608d3 100644
--- a/drivers/infiniband/hw/mlx4/sysfs.c
+++ b/drivers/infiniband/hw/mlx4/sysfs.c
@@ -444,16 +444,17 @@ static ssize_t show_port_pkey(struct mlx4_port *p, struct port_attribute *attr,
 {
 	struct port_table_attribute *tab_attr =
 		container_of(attr, struct port_table_attribute, attr);
-	ssize_t ret = -ENODEV;
+	int len;
+	struct pkey_mgt *m = &p->dev->pkeys;
+	u8 key = m->virt2phys_pkey[p->slave][p->port_num - 1][tab_attr->index];
 
-	if (p->dev->pkeys.virt2phys_pkey[p->slave][p->port_num - 1][tab_attr->index] >=
-	    (p->dev->dev->caps.pkey_table_len[p->port_num]))
-		ret = sprintf(buf, "none\n");
+	if (key >= p->dev->dev->caps.pkey_table_len[p->port_num])
+		len = sysfs_emit(buf, "none\n");
 	else
-		ret = sprintf(buf, "%d\n",
-			      p->dev->pkeys.virt2phys_pkey[p->slave]
-			      [p->port_num - 1][tab_attr->index]);
-	return ret;
+		len = sysfs_emit(buf, "%d\n",
+				 p->dev->pkeys.virt2phys_pkey[p->slave]
+				 [p->port_num - 1][tab_attr->index]);
+	return len;
 }
 
 static ssize_t store_port_pkey(struct mlx4_port *p, struct port_attribute *attr,
@@ -491,7 +492,7 @@ static ssize_t store_port_pkey(struct mlx4_port *p, struct port_attribute *attr,
 static ssize_t show_port_gid_idx(struct mlx4_port *p,
 				 struct port_attribute *attr, char *buf)
 {
-	return sprintf(buf, "%d\n", p->slave);
+	return sysfs_emit(buf, "%d\n", p->slave);
 }
 
 static struct attribute **
diff --git a/drivers/infiniband/hw/qib/qib_sysfs.c b/drivers/infiniband/hw/qib/qib_sysfs.c
index 02ad73ffcd5e..4b1e2ca0a0d0 100644
--- a/drivers/infiniband/hw/qib/qib_sysfs.c
+++ b/drivers/infiniband/hw/qib/qib_sysfs.c
@@ -43,11 +43,8 @@
 static ssize_t show_hrtbt_enb(struct qib_pportdata *ppd, char *buf)
 {
 	struct qib_devdata *dd = ppd->dd;
-	int ret;
 
-	ret = dd->f_get_ib_cfg(ppd, QIB_IB_CFG_HRTBT);
-	ret = scnprintf(buf, PAGE_SIZE, "%d\n", ret);
-	return ret;
+	return sysfs_emit(buf, "%d\n", dd->f_get_ib_cfg(ppd, QIB_IB_CFG_HRTBT));
 }
 
 static ssize_t store_hrtbt_enb(struct qib_pportdata *ppd, const char *buf,
@@ -106,14 +103,10 @@ static ssize_t store_led_override(struct qib_pportdata *ppd, const char *buf,
 
 static ssize_t show_status(struct qib_pportdata *ppd, char *buf)
 {
-	ssize_t ret;
-
 	if (!ppd->statusp)
-		ret = -EINVAL;
-	else
-		ret = scnprintf(buf, PAGE_SIZE, "0x%llx\n",
-				(unsigned long long) *(ppd->statusp));
-	return ret;
+		return -EINVAL;
+
+	return sysfs_emit(buf, "0x%llx\n", (unsigned long long)*(ppd->statusp));
 }
 
 /*
@@ -392,7 +385,7 @@ static ssize_t sl2vl_attr_show(struct kobject *kobj, struct attribute *attr,
 		container_of(kobj, struct qib_pportdata, sl2vl_kobj);
 	struct qib_ibport *qibp = &ppd->ibport_data;
 
-	return sprintf(buf, "%u\n", qibp->sl_to_vl[sattr->sl]);
+	return sysfs_emit(buf, "%u\n", qibp->sl_to_vl[sattr->sl]);
 }
 
 static const struct sysfs_ops qib_sl2vl_ops = {
@@ -501,17 +494,18 @@ static ssize_t diagc_attr_show(struct kobject *kobj, struct attribute *attr,
 	struct qib_pportdata *ppd =
 		container_of(kobj, struct qib_pportdata, diagc_kobj);
 	struct qib_ibport *qibp = &ppd->ibport_data;
+	u64 val;
 
 	if (!strncmp(dattr->attr.name, "rc_acks", 7))
-		return sprintf(buf, "%llu\n", READ_PER_CPU_CNTR(rc_acks));
+		val = READ_PER_CPU_CNTR(rc_acks);
 	else if (!strncmp(dattr->attr.name, "rc_qacks", 8))
-		return sprintf(buf, "%llu\n", READ_PER_CPU_CNTR(rc_qacks));
+		val = READ_PER_CPU_CNTR(rc_qacks);
 	else if (!strncmp(dattr->attr.name, "rc_delayed_comp", 15))
-		return sprintf(buf, "%llu\n",
-					READ_PER_CPU_CNTR(rc_delayed_comp));
+		val = READ_PER_CPU_CNTR(rc_delayed_comp);
 	else
-		return sprintf(buf, "%u\n",
-				*(u32 *)((char *)qibp + dattr->counter));
+		val = *(u32 *)((char *)qibp + dattr->counter);
+
+	return sysfs_emit(buf, "%llu\n", val);
 }
 
 static ssize_t diagc_attr_store(struct kobject *kobj, struct attribute *attr,
diff --git a/drivers/infiniband/hw/usnic/usnic_ib_sysfs.c b/drivers/infiniband/hw/usnic/usnic_ib_sysfs.c
index eddad4f0bd2d..7f971fed1cc0 100644
--- a/drivers/infiniband/hw/usnic/usnic_ib_sysfs.c
+++ b/drivers/infiniband/hw/usnic/usnic_ib_sysfs.c
@@ -205,43 +205,35 @@ struct qpn_attribute qpn_attr_##NAME = __ATTR_RO(NAME)
 
 static ssize_t context_show(struct usnic_ib_qp_grp *qp_grp, char *buf)
 {
-	return scnprintf(buf, PAGE_SIZE, "0x%p\n", qp_grp->ctx);
+	return sysfs_emit(buf, "0x%p\n", qp_grp->ctx);
 }
 
 static ssize_t summary_show(struct usnic_ib_qp_grp *qp_grp, char *buf)
 {
-	int i, j, n;
-	int left;
-	char *ptr;
+	int i, j;
 	struct usnic_vnic_res_chunk *res_chunk;
 	struct usnic_vnic_res *vnic_res;
+	int len;
 
-	left = PAGE_SIZE;
-	ptr = buf;
-
-	n = scnprintf(ptr, left,
-			"QPN: %d State: (%s) PID: %u VF Idx: %hu ",
-			qp_grp->ibqp.qp_num,
-			usnic_ib_qp_grp_state_to_string(qp_grp->state),
-			qp_grp->owner_pid,
-			usnic_vnic_get_index(qp_grp->vf->vnic));
-	UPDATE_PTR_LEFT(n, ptr, left);
+	len = sysfs_emit(buf, "QPN: %d State: (%s) PID: %u VF Idx: %hu ",
+			 qp_grp->ibqp.qp_num,
+			 usnic_ib_qp_grp_state_to_string(qp_grp->state),
+			 qp_grp->owner_pid,
+			 usnic_vnic_get_index(qp_grp->vf->vnic));
 
 	for (i = 0; qp_grp->res_chunk_list[i]; i++) {
 		res_chunk = qp_grp->res_chunk_list[i];
 		for (j = 0; j < res_chunk->cnt; j++) {
 			vnic_res = res_chunk->res[j];
-			n = scnprintf(ptr, left, "%s[%d] ",
-				usnic_vnic_res_type_to_str(vnic_res->type),
-				vnic_res->vnic_idx);
-			UPDATE_PTR_LEFT(n, ptr, left);
+			len += sysfs_emit_at(buf, len, "%s[%d] ",
+					     usnic_vnic_res_type_to_str(vnic_res->type),
+					     vnic_res->vnic_idx);
 		}
 	}
 
-	n = scnprintf(ptr, left, "\n");
-	UPDATE_PTR_LEFT(n, ptr, left);
+	len = sysfs_emit_at(buf, len, "\n");
 
-	return ptr - buf;
+	return len;
 }
 
 static QPN_ATTR_RO(context);
diff --git a/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c b/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c
index 51ba82fc425c..ebac1e87dd71 100644
--- a/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c
+++ b/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c
@@ -349,12 +349,13 @@ static ssize_t rtrs_clt_src_addr_show(struct kobject *kobj,
 				       char *page)
 {
 	struct rtrs_clt_sess *sess;
-	int cnt;
+	int len;
 
 	sess = container_of(kobj, struct rtrs_clt_sess, kobj);
-	cnt = sockaddr_to_str((struct sockaddr *)&sess->s.src_addr,
+	len = sockaddr_to_str((struct sockaddr *)&sess->s.src_addr,
 			      page, PAGE_SIZE);
-	return cnt + scnprintf(page + cnt, PAGE_SIZE - cnt, "\n");
+	len += sysfs_emit_at(page, len, "\n");
+	return len;
 }
 
 static struct kobj_attribute rtrs_clt_src_addr_attr =
@@ -365,12 +366,13 @@ static ssize_t rtrs_clt_dst_addr_show(struct kobject *kobj,
 				       char *page)
 {
 	struct rtrs_clt_sess *sess;
-	int cnt;
+	int len;
 
 	sess = container_of(kobj, struct rtrs_clt_sess, kobj);
-	cnt = sockaddr_to_str((struct sockaddr *)&sess->s.dst_addr,
+	len = sockaddr_to_str((struct sockaddr *)&sess->s.dst_addr,
 			      page, PAGE_SIZE);
-	return cnt + scnprintf(page + cnt, PAGE_SIZE - cnt, "\n");
+	len += sysfs_emit_at(page, len, "\n");
+	return len;
 }
 
 static struct kobj_attribute rtrs_clt_dst_addr_attr =
diff --git a/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c b/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
index 6e7bebe4e064..27a40b41c916 100644
--- a/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
+++ b/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
@@ -112,12 +112,13 @@ static ssize_t rtrs_srv_dst_addr_show(struct kobject *kobj,
 				       char *page)
 {
 	struct rtrs_srv_sess *sess;
-	int cnt;
+	int len;
 
 	sess = container_of(kobj, struct rtrs_srv_sess, kobj);
-	cnt = sockaddr_to_str((struct sockaddr *)&sess->s.src_addr,
+	len = sockaddr_to_str((struct sockaddr *)&sess->s.src_addr,
 			      page, PAGE_SIZE);
-	return cnt + scnprintf(page + cnt, PAGE_SIZE - cnt, "\n");
+	len += sysfs_emit_at(page, len, "\n");
+	return len;
 }
 
 static struct kobj_attribute rtrs_srv_dst_addr_attr =
diff --git a/drivers/infiniband/ulp/srp/ib_srp.c b/drivers/infiniband/ulp/srp/ib_srp.c
index 4a5a4f522fd5..c3ea036de661 100644
--- a/drivers/infiniband/ulp/srp/ib_srp.c
+++ b/drivers/infiniband/ulp/srp/ib_srp.c
@@ -169,9 +169,9 @@ static int srp_tmo_get(char *buffer, const struct kernel_param *kp)
 	int tmo = *(int *)kp->arg;
 
 	if (tmo >= 0)
-		return sprintf(buffer, "%d\n", tmo);
+		return sysfs_emit(buffer, "%d\n", tmo);
 	else
-		return sprintf(buffer, "off\n");
+		return sysfs_emit(buffer, "off\n");
 }
 
 static int srp_tmo_set(const char *val, const struct kernel_param *kp)
diff --git a/drivers/infiniband/ulp/srpt/ib_srpt.c b/drivers/infiniband/ulp/srpt/ib_srpt.c
index 0065eb17ae36..6017d525084a 100644
--- a/drivers/infiniband/ulp/srpt/ib_srpt.c
+++ b/drivers/infiniband/ulp/srpt/ib_srpt.c
@@ -3445,7 +3445,7 @@ static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
 	struct se_portal_group *se_tpg = attrib_to_tpg(item);
 	struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
 
-	return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
+	return sysfs_emit(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
 }
 
 static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
@@ -3482,7 +3482,7 @@ static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
 	struct se_portal_group *se_tpg = attrib_to_tpg(item);
 	struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
 
-	return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
+	return sysfs_emit(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
 }
 
 static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
@@ -3519,7 +3519,7 @@ static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
 	struct se_portal_group *se_tpg = attrib_to_tpg(item);
 	struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
 
-	return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
+	return sysfs_emit(page, "%u\n", sport->port_attrib.srp_sq_size);
 }
 
 static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
@@ -3556,7 +3556,7 @@ static ssize_t srpt_tpg_attrib_use_srq_show(struct config_item *item,
 	struct se_portal_group *se_tpg = attrib_to_tpg(item);
 	struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
 
-	return sprintf(page, "%d\n", sport->port_attrib.use_srq);
+	return sysfs_emit(page, "%d\n", sport->port_attrib.use_srq);
 }
 
 static ssize_t srpt_tpg_attrib_use_srq_store(struct config_item *item,
@@ -3646,7 +3646,7 @@ static struct rdma_cm_id *srpt_create_rdma_id(struct sockaddr *listen_addr)
 
 static ssize_t srpt_rdma_cm_port_show(struct config_item *item, char *page)
 {
-	return sprintf(page, "%d\n", rdma_cm_port);
+	return sysfs_emit(page, "%d\n", rdma_cm_port);
 }
 
 static ssize_t srpt_rdma_cm_port_store(struct config_item *item,
@@ -3702,7 +3702,7 @@ static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
 	struct se_portal_group *se_tpg = to_tpg(item);
 	struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
 
-	return snprintf(page, PAGE_SIZE, "%d\n", sport->enabled);
+	return sysfs_emit(page, "%d\n", sport->enabled);
 }
 
 static ssize_t srpt_tpg_enable_store(struct config_item *item,
@@ -3809,7 +3809,7 @@ static void srpt_drop_tport(struct se_wwn *wwn)
 
 static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
 {
-	return scnprintf(buf, PAGE_SIZE, "\n");
+	return sysfs_emit(buf, "\n");
 }
 
 CONFIGFS_ATTR_RO(srpt_wwn_, version);
-- 
2.26.0


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* Re: [PATCH-next 0/4] RDMA: sprintf to sysfs_emit conversions
  2020-10-08  2:36 [PATCH-next 0/4] RDMA: sprintf to sysfs_emit conversions Joe Perches
                   ` (3 preceding siblings ...)
  2020-10-08  2:36 ` [PATCH 4/4] RDMA: Convert various random sprintf sysfs _show uses to sysfs_emit Joe Perches
@ 2020-10-08  5:41 ` Leon Romanovsky
  2020-10-08  5:52   ` Joe Perches
  2020-10-15  5:29   ` Joe Perches
  2020-10-26 22:54 ` Jason Gunthorpe
  2020-10-28 18:27 ` Jason Gunthorpe
  6 siblings, 2 replies; 26+ messages in thread
From: Leon Romanovsky @ 2020-10-08  5:41 UTC (permalink / raw)
  To: Joe Perches
  Cc: Doug Ledford, Jason Gunthorpe, linux-rdma, target-devel, linux-kernel

On Wed, Oct 07, 2020 at 07:36:23PM -0700, Joe Perches wrote:
> A recent commit added a sysfs_emit and sysfs_emit_at to allow various
> sysfs show functions to ensure that the PAGE_SIZE buffer argument is
> never overrun and always NUL terminated.

Unfortunately but the sysfs_emit commit is not in rdma-next tree yet.

Thanks

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH-next 0/4] RDMA: sprintf to sysfs_emit conversions
  2020-10-08  5:41 ` [PATCH-next 0/4] RDMA: sprintf to sysfs_emit conversions Leon Romanovsky
@ 2020-10-08  5:52   ` Joe Perches
  2020-10-08  7:02     ` Leon Romanovsky
  2020-10-15  5:29   ` Joe Perches
  1 sibling, 1 reply; 26+ messages in thread
From: Joe Perches @ 2020-10-08  5:52 UTC (permalink / raw)
  To: Leon Romanovsky
  Cc: Doug Ledford, Jason Gunthorpe, linux-rdma, target-devel, linux-kernel

On Thu, 2020-10-08 at 08:41 +0300, Leon Romanovsky wrote:
> On Wed, Oct 07, 2020 at 07:36:23PM -0700, Joe Perches wrote:
> > A recent commit added a sysfs_emit and sysfs_emit_at to allow various
> > sysfs show functions to ensure that the PAGE_SIZE buffer argument is
> > never overrun and always NUL terminated.
> 
> Unfortunately but the sysfs_emit commit is not in rdma-next tree yet.

Likely it'll still apply fairly well when the sysfs_emit commit is...



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH-next 0/4] RDMA: sprintf to sysfs_emit conversions
  2020-10-08  5:52   ` Joe Perches
@ 2020-10-08  7:02     ` Leon Romanovsky
  0 siblings, 0 replies; 26+ messages in thread
From: Leon Romanovsky @ 2020-10-08  7:02 UTC (permalink / raw)
  To: Joe Perches
  Cc: Doug Ledford, Jason Gunthorpe, linux-rdma, target-devel, linux-kernel

On Wed, Oct 07, 2020 at 10:52:15PM -0700, Joe Perches wrote:
> On Thu, 2020-10-08 at 08:41 +0300, Leon Romanovsky wrote:
> > On Wed, Oct 07, 2020 at 07:36:23PM -0700, Joe Perches wrote:
> > > A recent commit added a sysfs_emit and sysfs_emit_at to allow various
> > > sysfs show functions to ensure that the PAGE_SIZE buffer argument is
> > > never overrun and always NUL terminated.
> >
> > Unfortunately but the sysfs_emit commit is not in rdma-next tree yet.
>
> Likely it'll still apply fairly well when the sysfs_emit commit is...

Of course, we just can't take it yet and test it in automatic way like
we are doing now.

Thanks

>
>

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 1/4] RDMA: Convert sysfs device * show functions to use sysfs_emit()
  2020-10-08  2:36 ` [PATCH 1/4] RDMA: Convert sysfs device * show functions to use sysfs_emit() Joe Perches
@ 2020-10-09 14:07   ` Jason Gunthorpe
  0 siblings, 0 replies; 26+ messages in thread
From: Jason Gunthorpe @ 2020-10-09 14:07 UTC (permalink / raw)
  To: Joe Perches
  Cc: Doug Ledford, Selvin Xavier, Devesh Sharma, Somnath Kotur,
	Sriharsha Basavapatna, Naresh Kumar PBS, Potnuri Bharat Teja,
	Mike Marciniszyn, Dennis Dalessandro, Faisal Latif,
	Shiraz Saleem, Yishai Hadas, Leon Romanovsky, Michal Kalderon,
	Ariel Elior, Christian Benvenuti, Nelson Escobar,
	Parvi Kaustubhi, Adit Ranadive, VMware PV-Drivers, Zhu Yanjun,
	Danil Kipnis, Jack Wang, Bart Van Assche, linux-rdma

On Wed, Oct 07, 2020 at 07:36:24PM -0700, Joe Perches wrote:
> Done with cocci script:
.. 
> Signed-off-by: Joe Perches <joe@perches.com>
> ---
>  drivers/infiniband/core/sysfs.c               | 47 +++++++++++--------
>  drivers/infiniband/core/ucma.c                |  2 +-
>  drivers/infiniband/core/user_mad.c            |  4 +-
>  drivers/infiniband/core/uverbs_main.c         |  4 +-
>  drivers/infiniband/hw/bnxt_re/main.c          |  4 +-
>  drivers/infiniband/hw/cxgb4/provider.c        | 10 ++--
>  drivers/infiniband/hw/hfi1/sysfs.c            | 16 +++----
>  drivers/infiniband/hw/i40iw/i40iw_verbs.c     |  6 +--
>  drivers/infiniband/hw/mlx4/main.c             |  8 ++--
>  drivers/infiniband/hw/mlx4/sysfs.c            | 30 ++++++------
>  drivers/infiniband/hw/mlx5/main.c             | 13 ++---
>  drivers/infiniband/hw/mthca/mthca_provider.c  | 14 +++---
>  drivers/infiniband/hw/ocrdma/ocrdma_main.c    |  4 +-
>  drivers/infiniband/hw/qedr/main.c             | 10 ++--
>  drivers/infiniband/hw/qib/qib_sysfs.c         | 30 ++++++------
>  drivers/infiniband/hw/usnic/usnic_ib_sysfs.c  | 16 +++----
>  .../infiniband/hw/vmw_pvrdma/pvrdma_main.c    |  6 +--
>  drivers/infiniband/sw/rxe/rxe_verbs.c         |  2 +-
>  drivers/infiniband/ulp/ipoib/ipoib_cm.c       |  4 +-
>  drivers/infiniband/ulp/ipoib/ipoib_main.c     |  7 +--
>  drivers/infiniband/ulp/ipoib/ipoib_vlan.c     |  2 +-
>  drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c  | 17 ++++---
>  drivers/infiniband/ulp/srp/ib_srp.c           | 41 ++++++++--------
>  23 files changed, 156 insertions(+), 141 deletions(-)

I didn't notice cocci getting any thing wrong here

Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>

Will have to wait till rc1 though

Jason

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 2/4] RDMA: Convert sysfs kobject * show functions to use sysfs_emit()
  2020-10-08  2:36 ` [PATCH 2/4] RDMA: Convert sysfs kobject " Joe Perches
@ 2020-10-09 14:09   ` Jason Gunthorpe
  2020-10-09 16:25     ` Joe Perches
  2020-10-12  5:21   ` Jinpu Wang
  1 sibling, 1 reply; 26+ messages in thread
From: Jason Gunthorpe @ 2020-10-09 14:09 UTC (permalink / raw)
  To: Joe Perches; +Cc: Doug Ledford, Danil Kipnis, Jack Wang, linux-rdma

On Wed, Oct 07, 2020 at 07:36:25PM -0700, Joe Perches wrote:
> Done with cocci script:
> 
> @@
> identifier k_show;
> identifier arg1, arg2, arg3;
> @@
> ssize_t k_show(struct kobject *
> -	arg1
> +	kobj
> 	, struct kobj_attribute *
> -	arg2
> +	attr
> 	, char *
> -	arg3
> +	buf
> 	)
> {
> 	...
> (
> -	arg1
> +	kobj
> |
> -	arg2
> +	attr
> |
> -	arg3
> +	buf
> )
> 	...
> }

This is nice

> Signed-off-by: Joe Perches <joe@perches.com>
> ---
>  drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c | 26 ++++++++++----------
>  drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c | 14 +++++------
>  2 files changed, 20 insertions(+), 20 deletions(-)

Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>

Thanks,
Jason

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 3/4] RDMA: manual changes for sysfs_emit and neatening
  2020-10-08  2:36 ` [PATCH 3/4] RDMA: manual changes for sysfs_emit and neatening Joe Perches
@ 2020-10-09 15:20   ` Jason Gunthorpe
  2020-10-12  5:30   ` Jinpu Wang
  2020-10-28 17:25   ` Jason Gunthorpe
  2 siblings, 0 replies; 26+ messages in thread
From: Jason Gunthorpe @ 2020-10-09 15:20 UTC (permalink / raw)
  To: Joe Perches
  Cc: Doug Ledford, Potnuri Bharat Teja, Mike Marciniszyn,
	Dennis Dalessandro, Yishai Hadas, Leon Romanovsky,
	Christian Benvenuti, Nelson Escobar, Parvi Kaustubhi,
	Danil Kipnis, Jack Wang, Bart Van Assche, linux-rdma

On Wed, Oct 07, 2020 at 07:36:26PM -0700, Joe Perches wrote:
> Make changes to use sysfs_emit in the RDMA code as cocci scripts can not
> be written to handle _all_ the possible variants of various sprintf family
> uses in sysfs show functions.
> 
> While there, make the code more legible and update its style to be more
> like the typical kernel styles.
> 
> Miscellanea:
> 
> o Use intermediate pointers for dereferences
> o Add and use string lookup functions
> o return early when any intermediate call fails so normal return is
>   at the bottom of the function
> o mlx4/mcg.c:sysfs_show_group: use scnprintf to format intermediate strings
> 
> Signed-off-by: Joe Perches <joe@perches.com>
> ---
>  drivers/infiniband/core/sysfs.c              | 60 +++++++-------
>  drivers/infiniband/hw/cxgb4/provider.c       |  5 +-
>  drivers/infiniband/hw/hfi1/sysfs.c           | 38 ++++-----
>  drivers/infiniband/hw/mlx4/main.c            |  5 +-
>  drivers/infiniband/hw/mlx4/mcg.c             | 82 +++++++++++---------
>  drivers/infiniband/hw/mlx4/sysfs.c           | 47 ++++++-----
>  drivers/infiniband/hw/mlx5/main.c            |  4 +-
>  drivers/infiniband/hw/mthca/mthca_provider.c | 29 ++++---
>  drivers/infiniband/hw/qib/qib_sysfs.c        | 45 +++++------
>  drivers/infiniband/hw/usnic/usnic_ib_sysfs.c | 66 +++++++---------
>  drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c | 21 +++--
>  drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c | 13 ++--
>  drivers/infiniband/ulp/srp/ib_srp.c          |  4 +
>  13 files changed, 206 insertions(+), 213 deletions(-)

Some of thee are a bit exciting, so usnic, hfi1, qib and mlx4 folks
should check their bits over the next two weeks

Thanks,
Jason

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 4/4] RDMA: Convert various random sprintf sysfs _show uses to sysfs_emit
  2020-10-08  2:36 ` [PATCH 4/4] RDMA: Convert various random sprintf sysfs _show uses to sysfs_emit Joe Perches
@ 2020-10-09 15:27   ` Jason Gunthorpe
  2020-10-12  5:25   ` Jinpu Wang
  2020-10-28 18:12   ` Jason Gunthorpe
  2 siblings, 0 replies; 26+ messages in thread
From: Jason Gunthorpe @ 2020-10-09 15:27 UTC (permalink / raw)
  To: Joe Perches
  Cc: Doug Ledford, Mike Marciniszyn, Dennis Dalessandro, Yishai Hadas,
	Christian Benvenuti, Nelson Escobar, Parvi Kaustubhi,
	Danil Kipnis, Jack Wang, Bart Van Assche, linux-rdma

On Wed, Oct 07, 2020 at 07:36:27PM -0700, Joe Perches wrote:
> Manual changes for sysfs_emit as cocci scripts can't easily convert them.
> 
> Signed-off-by: Joe Perches <joe@perches.com>
> ---
>  drivers/infiniband/core/cm.c                 |  4 +-
>  drivers/infiniband/core/cma_configfs.c       |  4 +-
>  drivers/infiniband/core/sysfs.c              | 98 +++++++++++---------
>  drivers/infiniband/core/user_mad.c           |  2 +-
>  drivers/infiniband/hw/hfi1/sysfs.c           | 10 +-
>  drivers/infiniband/hw/mlx4/sysfs.c           | 19 ++--
>  drivers/infiniband/hw/qib/qib_sysfs.c        | 30 +++---
>  drivers/infiniband/hw/usnic/usnic_ib_sysfs.c | 34 +++----
>  drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c | 14 +--
>  drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c |  7 +-
>  drivers/infiniband/ulp/srp/ib_srp.c          |  4 +-
>  drivers/infiniband/ulp/srpt/ib_srpt.c        | 14 +--
>  12 files changed, 119 insertions(+), 121 deletions(-)

Didn't see anything profound

Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>

Jason

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 2/4] RDMA: Convert sysfs kobject * show functions to use sysfs_emit()
  2020-10-09 14:09   ` Jason Gunthorpe
@ 2020-10-09 16:25     ` Joe Perches
  0 siblings, 0 replies; 26+ messages in thread
From: Joe Perches @ 2020-10-09 16:25 UTC (permalink / raw)
  To: Jason Gunthorpe; +Cc: Doug Ledford, Danil Kipnis, Jack Wang, linux-rdma

On Fri, 2020-10-09 at 11:09 -0300, Jason Gunthorpe wrote:
> On Wed, Oct 07, 2020 at 07:36:25PM -0700, Joe Perches wrote:
> > Done with cocci script:
> > 
> > @@
> > identifier k_show;
> > identifier arg1, arg2, arg3;
> > @@
> > ssize_t k_show(struct kobject *
> > -	arg1
> > +	kobj
> > 	, struct kobj_attribute *
> > -	arg2
> > +	attr
> > 	, char *
> > -	arg3
> > +	buf
> > 	)
> > {
> > 	...
> > (
> > -	arg1
> > +	kobj
> > -	arg2
> > +	attr
> > -	arg3
> > +	buf
> > )
> > 	...
> > }
> 
> This is nice

The 1/4 patch for device * cannot do this as there are
conflicts with dev/device naming within the functions.

It'd be nice to do the conversions though.

Maybe rename

	dev->ibdev
	device->dev

so that all sysfs _show function are consistently

ssize_t <foo>_show(struct device *dev, struct device_attribute *attr, char *buf);



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 2/4] RDMA: Convert sysfs kobject * show functions to use sysfs_emit()
  2020-10-08  2:36 ` [PATCH 2/4] RDMA: Convert sysfs kobject " Joe Perches
  2020-10-09 14:09   ` Jason Gunthorpe
@ 2020-10-12  5:21   ` Jinpu Wang
  1 sibling, 0 replies; 26+ messages in thread
From: Jinpu Wang @ 2020-10-12  5:21 UTC (permalink / raw)
  To: Joe Perches; +Cc: Doug Ledford, Jason Gunthorpe, Danil Kipnis, linux-rdma

On Thu, Oct 8, 2020 at 4:36 AM Joe Perches <joe@perches.com> wrote:
>
> Done with cocci script:
>
> @@
> identifier k_show;
> identifier arg1, arg2, arg3;
> @@
> ssize_t k_show(struct kobject *
> -       arg1
> +       kobj
>         , struct kobj_attribute *
> -       arg2
> +       attr
>         , char *
> -       arg3
> +       buf
>         )
> {
>         ...
> (
> -       arg1
> +       kobj
> |
> -       arg2
> +       attr
> |
> -       arg3
> +       buf
> )
>         ...
> }
>
> @@
> identifier k_show;
> identifier kobj, attr, buf;
> @@
>
> ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
> {
>         <...
>         return
> -       sprintf(buf,
> +       sysfs_emit(buf,
>         ...);
>         ...>
> }
>
> @@
> identifier k_show;
> identifier kobj, attr, buf;
> @@
>
> ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
> {
>         <...
>         return
> -       snprintf(buf, PAGE_SIZE,
> +       sysfs_emit(buf,
>         ...);
>         ...>
> }
>
> @@
> identifier k_show;
> identifier kobj, attr, buf;
> @@
>
> ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
> {
>         <...
>         return
> -       scnprintf(buf, PAGE_SIZE,
> +       sysfs_emit(buf,
>         ...);
>         ...>
> }
>
> @@
> identifier k_show;
> identifier kobj, attr, buf;
> expression chr;
> @@
>
> ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
> {
>         <...
>         return
> -       strcpy(buf, chr);
> +       sysfs_emit(buf, chr);
>         ...>
> }
>
> @@
> identifier k_show;
> identifier kobj, attr, buf;
> identifier len;
> @@
>
> ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
> {
>         <...
>         len =
> -       sprintf(buf,
> +       sysfs_emit(buf,
>         ...);
>         ...>
>         return len;
> }
>
> @@
> identifier k_show;
> identifier kobj, attr, buf;
> identifier len;
> @@
>
> ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
> {
>         <...
>         len =
> -       snprintf(buf, PAGE_SIZE,
> +       sysfs_emit(buf,
>         ...);
>         ...>
>         return len;
> }
>
> @@
> identifier k_show;
> identifier kobj, attr, buf;
> identifier len;
> @@
>
> ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
> {
>         <...
>         len =
> -       scnprintf(buf, PAGE_SIZE,
> +       sysfs_emit(buf,
>         ...);
>         ...>
>         return len;
> }
>
> @@
> identifier k_show;
> identifier kobj, attr, buf;
> identifier len;
> @@
>
> ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
> {
>         <...
> -       len += scnprintf(buf + len, PAGE_SIZE - len,
> +       len += sysfs_emit_at(buf, len,
>         ...);
>         ...>
>         return len;
> }
>
> @@
> identifier k_show;
> identifier kobj, attr, buf;
> expression chr;
> @@
>
> ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
> {
>         ...
> -       strcpy(buf, chr);
> -       return strlen(buf);
> +       return sysfs_emit(buf, chr);
> }
>
> Signed-off-by: Joe Perches <joe@perches.com>
Thanks,
Acked-by: Jack Wang <jinpu.wang@cloud.ionos.com>
> ---
>  drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c | 26 ++++++++++----------
>  drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c | 14 +++++------
>  2 files changed, 20 insertions(+), 20 deletions(-)
>
> diff --git a/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c b/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c
> index 7f71f10126fc..0c767582286b 100644
> --- a/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c
> +++ b/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c
> @@ -187,9 +187,9 @@ static ssize_t rtrs_clt_state_show(struct kobject *kobj,
>
>         sess = container_of(kobj, struct rtrs_clt_sess, kobj);
>         if (sess->state == RTRS_CLT_CONNECTED)
> -               return sprintf(page, "connected\n");
> +               return sysfs_emit(page, "connected\n");
>
> -       return sprintf(page, "disconnected\n");
> +       return sysfs_emit(page, "disconnected\n");
>  }
>
>  static struct kobj_attribute rtrs_clt_state_attr =
> @@ -197,10 +197,10 @@ static struct kobj_attribute rtrs_clt_state_attr =
>
>  static ssize_t rtrs_clt_reconnect_show(struct kobject *kobj,
>                                         struct kobj_attribute *attr,
> -                                       char *page)
> +                                       char *buf)
>  {
> -       return scnprintf(page, PAGE_SIZE, "Usage: echo 1 > %s\n",
> -                        attr->attr.name);
> +       return sysfs_emit(buf, "Usage: echo 1 > %s\n",
> +                         attr->attr.name);
>  }
>
>  static ssize_t rtrs_clt_reconnect_store(struct kobject *kobj,
> @@ -229,10 +229,10 @@ static struct kobj_attribute rtrs_clt_reconnect_attr =
>
>  static ssize_t rtrs_clt_disconnect_show(struct kobject *kobj,
>                                          struct kobj_attribute *attr,
> -                                        char *page)
> +                                        char *buf)
>  {
> -       return scnprintf(page, PAGE_SIZE, "Usage: echo 1 > %s\n",
> -                        attr->attr.name);
> +       return sysfs_emit(buf, "Usage: echo 1 > %s\n",
> +                         attr->attr.name);
>  }
>
>  static ssize_t rtrs_clt_disconnect_store(struct kobject *kobj,
> @@ -261,10 +261,10 @@ static struct kobj_attribute rtrs_clt_disconnect_attr =
>
>  static ssize_t rtrs_clt_remove_path_show(struct kobject *kobj,
>                                           struct kobj_attribute *attr,
> -                                         char *page)
> +                                         char *buf)
>  {
> -       return scnprintf(page, PAGE_SIZE, "Usage: echo 1 > %s\n",
> -                        attr->attr.name);
> +       return sysfs_emit(buf, "Usage: echo 1 > %s\n",
> +                         attr->attr.name);
>  }
>
>  static ssize_t rtrs_clt_remove_path_store(struct kobject *kobj,
> @@ -327,7 +327,7 @@ static ssize_t rtrs_clt_hca_port_show(struct kobject *kobj,
>
>         sess = container_of(kobj, typeof(*sess), kobj);
>
> -       return scnprintf(page, PAGE_SIZE, "%u\n", sess->hca_port);
> +       return sysfs_emit(page, "%u\n", sess->hca_port);
>  }
>
>  static struct kobj_attribute rtrs_clt_hca_port_attr =
> @@ -341,7 +341,7 @@ static ssize_t rtrs_clt_hca_name_show(struct kobject *kobj,
>
>         sess = container_of(kobj, struct rtrs_clt_sess, kobj);
>
> -       return scnprintf(page, PAGE_SIZE, "%s\n", sess->hca_name);
> +       return sysfs_emit(page, "%s\n", sess->hca_name);
>  }
>
>  static struct kobj_attribute rtrs_clt_hca_name_attr =
> diff --git a/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c b/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
> index 07fbb063555d..381a776ce404 100644
> --- a/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
> +++ b/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
> @@ -28,10 +28,10 @@ static struct kobj_type ktype = {
>
>  static ssize_t rtrs_srv_disconnect_show(struct kobject *kobj,
>                                          struct kobj_attribute *attr,
> -                                        char *page)
> +                                        char *buf)
>  {
> -       return scnprintf(page, PAGE_SIZE, "Usage: echo 1 > %s\n",
> -                        attr->attr.name);
> +       return sysfs_emit(buf, "Usage: echo 1 > %s\n",
> +                         attr->attr.name);
>  }
>
>  static ssize_t rtrs_srv_disconnect_store(struct kobject *kobj,
> @@ -72,8 +72,8 @@ static ssize_t rtrs_srv_hca_port_show(struct kobject *kobj,
>         sess = container_of(kobj, typeof(*sess), kobj);
>         usr_con = sess->s.con[0];
>
> -       return scnprintf(page, PAGE_SIZE, "%u\n",
> -                        usr_con->cm_id->port_num);
> +       return sysfs_emit(page, "%u\n",
> +                         usr_con->cm_id->port_num);
>  }
>
>  static struct kobj_attribute rtrs_srv_hca_port_attr =
> @@ -87,8 +87,8 @@ static ssize_t rtrs_srv_hca_name_show(struct kobject *kobj,
>
>         sess = container_of(kobj, struct rtrs_srv_sess, kobj);
>
> -       return scnprintf(page, PAGE_SIZE, "%s\n",
> -                        sess->s.dev->ib_dev->name);
> +       return sysfs_emit(page, "%s\n",
> +                         sess->s.dev->ib_dev->name);
>  }
>
>  static struct kobj_attribute rtrs_srv_hca_name_attr =
> --
> 2.26.0
>

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 4/4] RDMA: Convert various random sprintf sysfs _show uses to sysfs_emit
  2020-10-08  2:36 ` [PATCH 4/4] RDMA: Convert various random sprintf sysfs _show uses to sysfs_emit Joe Perches
  2020-10-09 15:27   ` Jason Gunthorpe
@ 2020-10-12  5:25   ` Jinpu Wang
  2020-10-28 18:12   ` Jason Gunthorpe
  2 siblings, 0 replies; 26+ messages in thread
From: Jinpu Wang @ 2020-10-12  5:25 UTC (permalink / raw)
  To: Joe Perches
  Cc: Doug Ledford, Jason Gunthorpe, Mike Marciniszyn,
	Dennis Dalessandro, Yishai Hadas, Christian Benvenuti,
	Nelson Escobar, Parvi Kaustubhi, Danil Kipnis, Bart Van Assche,
	linux-rdma

On Thu, Oct 8, 2020 at 4:36 AM Joe Perches <joe@perches.com> wrote:
>
> Manual changes for sysfs_emit as cocci scripts can't easily convert them.
>
> Signed-off-by: Joe Perches <joe@perches.com>
> ---

>  drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c | 14 +--
>  drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c |  7 +-
For rtrs,
Acked-by: Jack Wang <jinpu.wang@cloud.ionos.com>


>  static ssize_t default_roce_mode_store(struct config_item *item,
> @@ -157,7 +157,7 @@ static ssize_t default_roce_tos_show(struct config_item *item, char *buf)
>         tos = cma_get_default_roce_tos(cma_dev, group->port_num);
>         cma_configfs_params_put(cma_dev);
>
> -       return sprintf(buf, "%u\n", tos);
> +       return sysfs_emit(buf, "%u\n", tos);
>  }
>
>  static ssize_t default_roce_tos_store(struct config_item *item,
> diff --git a/drivers/infiniband/core/sysfs.c b/drivers/infiniband/core/sysfs.c
> index cbc8326b935d..bffed464509d 100644
> --- a/drivers/infiniband/core/sysfs.c
> +++ b/drivers/infiniband/core/sysfs.c
> @@ -165,9 +165,11 @@ static ssize_t state_show(struct ib_port *p, struct port_attribute *unused,
>         if (ret)
>                 return ret;
>
> -       return sprintf(buf, "%d: %s\n", attr.state,
> -                      attr.state >= 0 && attr.state < ARRAY_SIZE(state_name) ?
> -                      state_name[attr.state] : "UNKNOWN");
> +       return sysfs_emit(buf, "%d: %s\n",
> +                         attr.state,
> +                         attr.state >= 0 &&
> +                         attr.state < ARRAY_SIZE(state_name) ?
> +                         state_name[attr.state] : "UNKNOWN");
>  }
>
>  static ssize_t lid_show(struct ib_port *p, struct port_attribute *unused,
> @@ -180,7 +182,7 @@ static ssize_t lid_show(struct ib_port *p, struct port_attribute *unused,
>         if (ret)
>                 return ret;
>
> -       return sprintf(buf, "0x%x\n", attr.lid);
> +       return sysfs_emit(buf, "0x%x\n", attr.lid);
>  }
>
>  static ssize_t lid_mask_count_show(struct ib_port *p,
> @@ -194,7 +196,7 @@ static ssize_t lid_mask_count_show(struct ib_port *p,
>         if (ret)
>                 return ret;
>
> -       return sprintf(buf, "%d\n", attr.lmc);
> +       return sysfs_emit(buf, "%d\n", attr.lmc);
>  }
>
>  static ssize_t sm_lid_show(struct ib_port *p, struct port_attribute *unused,
> @@ -207,7 +209,7 @@ static ssize_t sm_lid_show(struct ib_port *p, struct port_attribute *unused,
>         if (ret)
>                 return ret;
>
> -       return sprintf(buf, "0x%x\n", attr.sm_lid);
> +       return sysfs_emit(buf, "0x%x\n", attr.sm_lid);
>  }
>
>  static ssize_t sm_sl_show(struct ib_port *p, struct port_attribute *unused,
> @@ -220,7 +222,7 @@ static ssize_t sm_sl_show(struct ib_port *p, struct port_attribute *unused,
>         if (ret)
>                 return ret;
>
> -       return sprintf(buf, "%d\n", attr.sm_sl);
> +       return sysfs_emit(buf, "%d\n", attr.sm_sl);
>  }
>
>  static ssize_t cap_mask_show(struct ib_port *p, struct port_attribute *unused,
> @@ -233,7 +235,7 @@ static ssize_t cap_mask_show(struct ib_port *p, struct port_attribute *unused,
>         if (ret)
>                 return ret;
>
> -       return sprintf(buf, "0x%08x\n", attr.port_cap_flags);
> +       return sysfs_emit(buf, "0x%08x\n", attr.port_cap_flags);
>  }
>
>  static ssize_t rate_show(struct ib_port *p, struct port_attribute *unused,
> @@ -284,9 +286,9 @@ static ssize_t rate_show(struct ib_port *p, struct port_attribute *unused,
>         if (rate < 0)
>                 return -EINVAL;
>
> -       return sprintf(buf, "%d%s Gb/sec (%dX%s)\n",
> -                      rate / 10, rate % 10 ? ".5" : "",
> -                      ib_width_enum_to_int(attr.active_width), speed);
> +       return sysfs_emit(buf, "%d%s Gb/sec (%dX%s)\n",
> +                         rate / 10, rate % 10 ? ".5" : "",
> +                         ib_width_enum_to_int(attr.active_width), speed);
>  }
>
>  static const char *phys_state_to_str(enum ib_port_phys_state phys_state)
> @@ -318,21 +320,28 @@ static ssize_t phys_state_show(struct ib_port *p, struct port_attribute *unused,
>         if (ret)
>                 return ret;
>
> -       return sprintf(buf, "%d: %s\n", attr.phys_state,
> -                      phys_state_to_str(attr.phys_state));
> +       return sysfs_emit(buf, "%d: %s\n",
> +                         attr.phys_state, phys_state_to_str(attr.phys_state));
>  }
>
>  static ssize_t link_layer_show(struct ib_port *p, struct port_attribute *unused,
>                                char *buf)
>  {
> +       const char *output;
> +
>         switch (rdma_port_get_link_layer(p->ibdev, p->port_num)) {
>         case IB_LINK_LAYER_INFINIBAND:
> -               return sprintf(buf, "%s\n", "InfiniBand");
> +               output = "InfiniBand";
> +               break;
>         case IB_LINK_LAYER_ETHERNET:
> -               return sprintf(buf, "%s\n", "Ethernet");
> +               output = "Ethernet";
> +               break;
>         default:
> -               return sprintf(buf, "%s\n", "Unknown");
> +               output = "Unknown";
> +               break;
>         }
> +
> +       return sysfs_emit(buf, "%s\n", output);
>  }
>
>  static PORT_ATTR_RO(state);
> @@ -358,27 +367,28 @@ static struct attribute *port_default_attrs[] = {
>         NULL
>  };
>
> -static size_t print_ndev(const struct ib_gid_attr *gid_attr, char *buf)
> +static ssize_t print_ndev(const struct ib_gid_attr *gid_attr, char *buf)
>  {
>         struct net_device *ndev;
> -       size_t ret = -EINVAL;
> +       int ret = -EINVAL;
>
>         rcu_read_lock();
>         ndev = rcu_dereference(gid_attr->ndev);
>         if (ndev)
> -               ret = sprintf(buf, "%s\n", ndev->name);
> +               ret = sysfs_emit(buf, "%s\n", ndev->name);
>         rcu_read_unlock();
>         return ret;
>  }
>
> -static size_t print_gid_type(const struct ib_gid_attr *gid_attr, char *buf)
> +static ssize_t print_gid_type(const struct ib_gid_attr *gid_attr, char *buf)
>  {
> -       return sprintf(buf, "%s\n", ib_cache_gid_type_str(gid_attr->gid_type));
> +       return sysfs_emit(buf, "%s\n",
> +                         ib_cache_gid_type_str(gid_attr->gid_type));
>  }
>
>  static ssize_t _show_port_gid_attr(
>         struct ib_port *p, struct port_attribute *attr, char *buf,
> -       size_t (*print)(const struct ib_gid_attr *gid_attr, char *buf))
> +       ssize_t (*print)(const struct ib_gid_attr *gid_attr, char *buf))
>  {
>         struct port_table_attribute *tab_attr =
>                 container_of(attr, struct port_table_attribute, attr);
> @@ -401,7 +411,7 @@ static ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr,
>         struct port_table_attribute *tab_attr =
>                 container_of(attr, struct port_table_attribute, attr);
>         const struct ib_gid_attr *gid_attr;
> -       ssize_t ret;
> +       int len;
>
>         gid_attr = rdma_get_gid_attr(p->ibdev, p->port_num, tab_attr->index);
>         if (IS_ERR(gid_attr)) {
> @@ -416,12 +426,12 @@ static ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr,
>                  * space throwing such error on fail to read gid, return zero
>                  * GID as before. This maintains backward compatibility.
>                  */
> -               return sprintf(buf, "%pI6\n", zgid.raw);
> +               return sysfs_emit(buf, "%pI6\n", zgid.raw);
>         }
>
> -       ret = sprintf(buf, "%pI6\n", gid_attr->gid.raw);
> +       len = sysfs_emit(buf, "%pI6\n", gid_attr->gid.raw);
>         rdma_put_gid_attr(gid_attr);
> -       return ret;
> +       return len;
>  }
>
>  static ssize_t show_port_gid_attr_ndev(struct ib_port *p,
> @@ -443,13 +453,13 @@ static ssize_t show_port_pkey(struct ib_port *p, struct port_attribute *attr,
>         struct port_table_attribute *tab_attr =
>                 container_of(attr, struct port_table_attribute, attr);
>         u16 pkey;
> -       ssize_t ret;
> +       int ret;
>
>         ret = ib_query_pkey(p->ibdev, p->port_num, tab_attr->index, &pkey);
>         if (ret)
>                 return ret;
>
> -       return sprintf(buf, "0x%04x\n", pkey);
> +       return sysfs_emit(buf, "0x%04x\n", pkey);
>  }
>
>  #define PORT_PMA_ATTR(_name, _counter, _width, _offset)                        \
> @@ -521,8 +531,9 @@ static ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
>                 container_of(attr, struct port_table_attribute, attr);
>         int offset = tab_attr->index & 0xffff;
>         int width  = (tab_attr->index >> 16) & 0xff;
> -       ssize_t ret;
> +       int ret;
>         u8 data[8];
> +       int len;
>
>         ret = get_perf_mad(p->ibdev, p->port_num, tab_attr->attr_id, &data,
>                         40 + offset / 8, sizeof(data));
> @@ -531,30 +542,27 @@ static ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
>
>         switch (width) {
>         case 4:
> -               ret = sprintf(buf, "%u\n", (*data >>
> -                                           (4 - (offset % 8))) & 0xf);
> +               len = sysfs_emit(buf, "%u\n",
> +                                (*data >> (4 - (offset % 8))) & 0xf);
>                 break;
>         case 8:
> -               ret = sprintf(buf, "%u\n", *data);
> +               len = sysfs_emit(buf, "%u\n", *data);
>                 break;
>         case 16:
> -               ret = sprintf(buf, "%u\n",
> -                             be16_to_cpup((__be16 *)data));
> +               len = sysfs_emit(buf, "%u\n", be16_to_cpup((__be16 *)data));
>                 break;
>         case 32:
> -               ret = sprintf(buf, "%u\n",
> -                             be32_to_cpup((__be32 *)data));
> +               len = sysfs_emit(buf, "%u\n", be32_to_cpup((__be32 *)data));
>                 break;
>         case 64:
> -               ret = sprintf(buf, "%llu\n",
> -                               be64_to_cpup((__be64 *)data));
> +               len = sysfs_emit(buf, "%llu\n", be64_to_cpup((__be64 *)data));
>                 break;
> -
>         default:
> -               ret = 0;
> +               len = 0;
> +               break;
>         }
>
> -       return ret;
> +       return len;
>  }
>
>  static PORT_PMA_ATTR(symbol_error                  ,  0, 16,  32);
> @@ -815,12 +823,12 @@ static int update_hw_stats(struct ib_device *dev, struct rdma_hw_stats *stats,
>         return 0;
>  }
>
> -static ssize_t print_hw_stat(struct ib_device *dev, int port_num,
> -                            struct rdma_hw_stats *stats, int index, char *buf)
> +static int print_hw_stat(struct ib_device *dev, int port_num,
> +                        struct rdma_hw_stats *stats, int index, char *buf)
>  {
>         u64 v = rdma_counter_get_hwstat_value(dev, port_num, index);
>
> -       return sprintf(buf, "%llu\n", stats->value[index] + v);
> +       return sysfs_emit(buf, "%llu\n", stats->value[index] + v);
>  }
>
>  static ssize_t show_hw_stats(struct kobject *kobj, struct attribute *attr,
> @@ -877,7 +885,7 @@ static ssize_t show_stats_lifespan(struct kobject *kobj,
>         msecs = jiffies_to_msecs(stats->lifespan);
>         mutex_unlock(&stats->lock);
>
> -       return sprintf(buf, "%d\n", msecs);
> +       return sysfs_emit(buf, "%d\n", msecs);
>  }
>
>  static ssize_t set_stats_lifespan(struct kobject *kobj,
> diff --git a/drivers/infiniband/core/user_mad.c b/drivers/infiniband/core/user_mad.c
> index 7e759f5b2a75..19104a675691 100644
> --- a/drivers/infiniband/core/user_mad.c
> +++ b/drivers/infiniband/core/user_mad.c
> @@ -1222,7 +1222,7 @@ static char *umad_devnode(struct device *dev, umode_t *mode)
>  static ssize_t abi_version_show(struct class *class,
>                                 struct class_attribute *attr, char *buf)
>  {
> -       return sprintf(buf, "%d\n", IB_USER_MAD_ABI_VERSION);
> +       return sysfs_emit(buf, "%d\n", IB_USER_MAD_ABI_VERSION);
>  }
>  static CLASS_ATTR_RO(abi_version);
>
> diff --git a/drivers/infiniband/hw/hfi1/sysfs.c b/drivers/infiniband/hw/hfi1/sysfs.c
> index 6b545f0f065f..dcea59e0721a 100644
> --- a/drivers/infiniband/hw/hfi1/sysfs.c
> +++ b/drivers/infiniband/hw/hfi1/sysfs.c
> @@ -151,7 +151,7 @@ struct hfi1_port_attr {
>
>  static ssize_t cc_prescan_show(struct hfi1_pportdata *ppd, char *buf)
>  {
> -       return sprintf(buf, "%s\n", ppd->cc_prescan ? "on" : "off");
> +       return sysfs_emit(buf, "%s\n", ppd->cc_prescan ? "on" : "off");
>  }
>
>  static ssize_t cc_prescan_store(struct hfi1_pportdata *ppd, const char *buf,
> @@ -296,7 +296,7 @@ static ssize_t sc2vl_attr_show(struct kobject *kobj, struct attribute *attr,
>                 container_of(kobj, struct hfi1_pportdata, sc2vl_kobj);
>         struct hfi1_devdata *dd = ppd->dd;
>
> -       return sprintf(buf, "%u\n", *((u8 *)dd->sc2vl + sattr->sc));
> +       return sysfs_emit(buf, "%u\n", *((u8 *)dd->sc2vl + sattr->sc));
>  }
>
>  static const struct sysfs_ops hfi1_sc2vl_ops = {
> @@ -401,7 +401,7 @@ static ssize_t sl2sc_attr_show(struct kobject *kobj, struct attribute *attr,
>                 container_of(kobj, struct hfi1_pportdata, sl2sc_kobj);
>         struct hfi1_ibport *ibp = &ppd->ibport_data;
>
> -       return sprintf(buf, "%u\n", ibp->sl_to_sc[sattr->sl]);
> +       return sysfs_emit(buf, "%u\n", ibp->sl_to_sc[sattr->sl]);
>  }
>
>  static const struct sysfs_ops hfi1_sl2sc_ops = {
> @@ -475,7 +475,7 @@ static ssize_t vl2mtu_attr_show(struct kobject *kobj, struct attribute *attr,
>                 container_of(kobj, struct hfi1_pportdata, vl2mtu_kobj);
>         struct hfi1_devdata *dd = ppd->dd;
>
> -       return sprintf(buf, "%u\n", dd->vld[vlattr->vl].mtu);
> +       return sysfs_emit(buf, "%u\n", dd->vld[vlattr->vl].mtu);
>  }
>
>  static const struct sysfs_ops hfi1_vl2mtu_ops = {
> @@ -813,7 +813,7 @@ static ssize_t sde_show_vl(struct sdma_engine *sde, char *buf)
>         if (vl < 0)
>                 return vl;
>
> -       return snprintf(buf, PAGE_SIZE, "%d\n", vl);
> +       return sysfs_emit(buf, "%d\n", vl);
>  }
>
>  static SDE_ATTR(cpu_list, S_IWUSR | S_IRUGO,
> diff --git a/drivers/infiniband/hw/mlx4/sysfs.c b/drivers/infiniband/hw/mlx4/sysfs.c
> index 75d50383da89..fc6c98b608d3 100644
> --- a/drivers/infiniband/hw/mlx4/sysfs.c
> +++ b/drivers/infiniband/hw/mlx4/sysfs.c
> @@ -444,16 +444,17 @@ static ssize_t show_port_pkey(struct mlx4_port *p, struct port_attribute *attr,
>  {
>         struct port_table_attribute *tab_attr =
>                 container_of(attr, struct port_table_attribute, attr);
> -       ssize_t ret = -ENODEV;
> +       int len;
> +       struct pkey_mgt *m = &p->dev->pkeys;
> +       u8 key = m->virt2phys_pkey[p->slave][p->port_num - 1][tab_attr->index];
>
> -       if (p->dev->pkeys.virt2phys_pkey[p->slave][p->port_num - 1][tab_attr->index] >=
> -           (p->dev->dev->caps.pkey_table_len[p->port_num]))
> -               ret = sprintf(buf, "none\n");
> +       if (key >= p->dev->dev->caps.pkey_table_len[p->port_num])
> +               len = sysfs_emit(buf, "none\n");
>         else
> -               ret = sprintf(buf, "%d\n",
> -                             p->dev->pkeys.virt2phys_pkey[p->slave]
> -                             [p->port_num - 1][tab_attr->index]);
> -       return ret;
> +               len = sysfs_emit(buf, "%d\n",
> +                                p->dev->pkeys.virt2phys_pkey[p->slave]
> +                                [p->port_num - 1][tab_attr->index]);
> +       return len;
>  }
>
>  static ssize_t store_port_pkey(struct mlx4_port *p, struct port_attribute *attr,
> @@ -491,7 +492,7 @@ static ssize_t store_port_pkey(struct mlx4_port *p, struct port_attribute *attr,
>  static ssize_t show_port_gid_idx(struct mlx4_port *p,
>                                  struct port_attribute *attr, char *buf)
>  {
> -       return sprintf(buf, "%d\n", p->slave);
> +       return sysfs_emit(buf, "%d\n", p->slave);
>  }
>
>  static struct attribute **
> diff --git a/drivers/infiniband/hw/qib/qib_sysfs.c b/drivers/infiniband/hw/qib/qib_sysfs.c
> index 02ad73ffcd5e..4b1e2ca0a0d0 100644
> --- a/drivers/infiniband/hw/qib/qib_sysfs.c
> +++ b/drivers/infiniband/hw/qib/qib_sysfs.c
> @@ -43,11 +43,8 @@
>  static ssize_t show_hrtbt_enb(struct qib_pportdata *ppd, char *buf)
>  {
>         struct qib_devdata *dd = ppd->dd;
> -       int ret;
>
> -       ret = dd->f_get_ib_cfg(ppd, QIB_IB_CFG_HRTBT);
> -       ret = scnprintf(buf, PAGE_SIZE, "%d\n", ret);
> -       return ret;
> +       return sysfs_emit(buf, "%d\n", dd->f_get_ib_cfg(ppd, QIB_IB_CFG_HRTBT));
>  }
>
>  static ssize_t store_hrtbt_enb(struct qib_pportdata *ppd, const char *buf,
> @@ -106,14 +103,10 @@ static ssize_t store_led_override(struct qib_pportdata *ppd, const char *buf,
>
>  static ssize_t show_status(struct qib_pportdata *ppd, char *buf)
>  {
> -       ssize_t ret;
> -
>         if (!ppd->statusp)
> -               ret = -EINVAL;
> -       else
> -               ret = scnprintf(buf, PAGE_SIZE, "0x%llx\n",
> -                               (unsigned long long) *(ppd->statusp));
> -       return ret;
> +               return -EINVAL;
> +
> +       return sysfs_emit(buf, "0x%llx\n", (unsigned long long)*(ppd->statusp));
>  }
>
>  /*
> @@ -392,7 +385,7 @@ static ssize_t sl2vl_attr_show(struct kobject *kobj, struct attribute *attr,
>                 container_of(kobj, struct qib_pportdata, sl2vl_kobj);
>         struct qib_ibport *qibp = &ppd->ibport_data;
>
> -       return sprintf(buf, "%u\n", qibp->sl_to_vl[sattr->sl]);
> +       return sysfs_emit(buf, "%u\n", qibp->sl_to_vl[sattr->sl]);
>  }
>
>  static const struct sysfs_ops qib_sl2vl_ops = {
> @@ -501,17 +494,18 @@ static ssize_t diagc_attr_show(struct kobject *kobj, struct attribute *attr,
>         struct qib_pportdata *ppd =
>                 container_of(kobj, struct qib_pportdata, diagc_kobj);
>         struct qib_ibport *qibp = &ppd->ibport_data;
> +       u64 val;
>
>         if (!strncmp(dattr->attr.name, "rc_acks", 7))
> -               return sprintf(buf, "%llu\n", READ_PER_CPU_CNTR(rc_acks));
> +               val = READ_PER_CPU_CNTR(rc_acks);
>         else if (!strncmp(dattr->attr.name, "rc_qacks", 8))
> -               return sprintf(buf, "%llu\n", READ_PER_CPU_CNTR(rc_qacks));
> +               val = READ_PER_CPU_CNTR(rc_qacks);
>         else if (!strncmp(dattr->attr.name, "rc_delayed_comp", 15))
> -               return sprintf(buf, "%llu\n",
> -                                       READ_PER_CPU_CNTR(rc_delayed_comp));
> +               val = READ_PER_CPU_CNTR(rc_delayed_comp);
>         else
> -               return sprintf(buf, "%u\n",
> -                               *(u32 *)((char *)qibp + dattr->counter));
> +               val = *(u32 *)((char *)qibp + dattr->counter);
> +
> +       return sysfs_emit(buf, "%llu\n", val);
>  }
>
>  static ssize_t diagc_attr_store(struct kobject *kobj, struct attribute *attr,
> diff --git a/drivers/infiniband/hw/usnic/usnic_ib_sysfs.c b/drivers/infiniband/hw/usnic/usnic_ib_sysfs.c
> index eddad4f0bd2d..7f971fed1cc0 100644
> --- a/drivers/infiniband/hw/usnic/usnic_ib_sysfs.c
> +++ b/drivers/infiniband/hw/usnic/usnic_ib_sysfs.c
> @@ -205,43 +205,35 @@ struct qpn_attribute qpn_attr_##NAME = __ATTR_RO(NAME)
>
>  static ssize_t context_show(struct usnic_ib_qp_grp *qp_grp, char *buf)
>  {
> -       return scnprintf(buf, PAGE_SIZE, "0x%p\n", qp_grp->ctx);
> +       return sysfs_emit(buf, "0x%p\n", qp_grp->ctx);
>  }
>
>  static ssize_t summary_show(struct usnic_ib_qp_grp *qp_grp, char *buf)
>  {
> -       int i, j, n;
> -       int left;
> -       char *ptr;
> +       int i, j;
>         struct usnic_vnic_res_chunk *res_chunk;
>         struct usnic_vnic_res *vnic_res;
> +       int len;
>
> -       left = PAGE_SIZE;
> -       ptr = buf;
> -
> -       n = scnprintf(ptr, left,
> -                       "QPN: %d State: (%s) PID: %u VF Idx: %hu ",
> -                       qp_grp->ibqp.qp_num,
> -                       usnic_ib_qp_grp_state_to_string(qp_grp->state),
> -                       qp_grp->owner_pid,
> -                       usnic_vnic_get_index(qp_grp->vf->vnic));
> -       UPDATE_PTR_LEFT(n, ptr, left);
> +       len = sysfs_emit(buf, "QPN: %d State: (%s) PID: %u VF Idx: %hu ",
> +                        qp_grp->ibqp.qp_num,
> +                        usnic_ib_qp_grp_state_to_string(qp_grp->state),
> +                        qp_grp->owner_pid,
> +                        usnic_vnic_get_index(qp_grp->vf->vnic));
>
>         for (i = 0; qp_grp->res_chunk_list[i]; i++) {
>                 res_chunk = qp_grp->res_chunk_list[i];
>                 for (j = 0; j < res_chunk->cnt; j++) {
>                         vnic_res = res_chunk->res[j];
> -                       n = scnprintf(ptr, left, "%s[%d] ",
> -                               usnic_vnic_res_type_to_str(vnic_res->type),
> -                               vnic_res->vnic_idx);
> -                       UPDATE_PTR_LEFT(n, ptr, left);
> +                       len += sysfs_emit_at(buf, len, "%s[%d] ",
> +                                            usnic_vnic_res_type_to_str(vnic_res->type),
> +                                            vnic_res->vnic_idx);
>                 }
>         }
>
> -       n = scnprintf(ptr, left, "\n");
> -       UPDATE_PTR_LEFT(n, ptr, left);
> +       len = sysfs_emit_at(buf, len, "\n");
>
> -       return ptr - buf;
> +       return len;
>  }
>
>  static QPN_ATTR_RO(context);
> diff --git a/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c b/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c
> index 51ba82fc425c..ebac1e87dd71 100644
> --- a/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c
> +++ b/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c
> @@ -349,12 +349,13 @@ static ssize_t rtrs_clt_src_addr_show(struct kobject *kobj,
>                                        char *page)
>  {
>         struct rtrs_clt_sess *sess;
> -       int cnt;
> +       int len;
>
>         sess = container_of(kobj, struct rtrs_clt_sess, kobj);
> -       cnt = sockaddr_to_str((struct sockaddr *)&sess->s.src_addr,
> +       len = sockaddr_to_str((struct sockaddr *)&sess->s.src_addr,
>                               page, PAGE_SIZE);
> -       return cnt + scnprintf(page + cnt, PAGE_SIZE - cnt, "\n");
> +       len += sysfs_emit_at(page, len, "\n");
> +       return len;
>  }
>
>  static struct kobj_attribute rtrs_clt_src_addr_attr =
> @@ -365,12 +366,13 @@ static ssize_t rtrs_clt_dst_addr_show(struct kobject *kobj,
>                                        char *page)
>  {
>         struct rtrs_clt_sess *sess;
> -       int cnt;
> +       int len;
>
>         sess = container_of(kobj, struct rtrs_clt_sess, kobj);
> -       cnt = sockaddr_to_str((struct sockaddr *)&sess->s.dst_addr,
> +       len = sockaddr_to_str((struct sockaddr *)&sess->s.dst_addr,
>                               page, PAGE_SIZE);
> -       return cnt + scnprintf(page + cnt, PAGE_SIZE - cnt, "\n");
> +       len += sysfs_emit_at(page, len, "\n");
> +       return len;
>  }
>
>  static struct kobj_attribute rtrs_clt_dst_addr_attr =
> diff --git a/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c b/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
> index 6e7bebe4e064..27a40b41c916 100644
> --- a/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
> +++ b/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
> @@ -112,12 +112,13 @@ static ssize_t rtrs_srv_dst_addr_show(struct kobject *kobj,
>                                        char *page)
>  {
>         struct rtrs_srv_sess *sess;
> -       int cnt;
> +       int len;
>
>         sess = container_of(kobj, struct rtrs_srv_sess, kobj);
> -       cnt = sockaddr_to_str((struct sockaddr *)&sess->s.src_addr,
> +       len = sockaddr_to_str((struct sockaddr *)&sess->s.src_addr,
>                               page, PAGE_SIZE);
> -       return cnt + scnprintf(page + cnt, PAGE_SIZE - cnt, "\n");
> +       len += sysfs_emit_at(page, len, "\n");
> +       return len;
>  }
>
>  static struct kobj_attribute rtrs_srv_dst_addr_attr =
> diff --git a/drivers/infiniband/ulp/srp/ib_srp.c b/drivers/infiniband/ulp/srp/ib_srp.c
> index 4a5a4f522fd5..c3ea036de661 100644
> --- a/drivers/infiniband/ulp/srp/ib_srp.c
> +++ b/drivers/infiniband/ulp/srp/ib_srp.c
> @@ -169,9 +169,9 @@ static int srp_tmo_get(char *buffer, const struct kernel_param *kp)
>         int tmo = *(int *)kp->arg;
>
>         if (tmo >= 0)
> -               return sprintf(buffer, "%d\n", tmo);
> +               return sysfs_emit(buffer, "%d\n", tmo);
>         else
> -               return sprintf(buffer, "off\n");
> +               return sysfs_emit(buffer, "off\n");
>  }
>
>  static int srp_tmo_set(const char *val, const struct kernel_param *kp)
> diff --git a/drivers/infiniband/ulp/srpt/ib_srpt.c b/drivers/infiniband/ulp/srpt/ib_srpt.c
> index 0065eb17ae36..6017d525084a 100644
> --- a/drivers/infiniband/ulp/srpt/ib_srpt.c
> +++ b/drivers/infiniband/ulp/srpt/ib_srpt.c
> @@ -3445,7 +3445,7 @@ static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
>         struct se_portal_group *se_tpg = attrib_to_tpg(item);
>         struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
>
> -       return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
> +       return sysfs_emit(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
>  }
>
>  static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
> @@ -3482,7 +3482,7 @@ static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
>         struct se_portal_group *se_tpg = attrib_to_tpg(item);
>         struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
>
> -       return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
> +       return sysfs_emit(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
>  }
>
>  static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
> @@ -3519,7 +3519,7 @@ static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
>         struct se_portal_group *se_tpg = attrib_to_tpg(item);
>         struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
>
> -       return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
> +       return sysfs_emit(page, "%u\n", sport->port_attrib.srp_sq_size);
>  }
>
>  static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
> @@ -3556,7 +3556,7 @@ static ssize_t srpt_tpg_attrib_use_srq_show(struct config_item *item,
>         struct se_portal_group *se_tpg = attrib_to_tpg(item);
>         struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
>
> -       return sprintf(page, "%d\n", sport->port_attrib.use_srq);
> +       return sysfs_emit(page, "%d\n", sport->port_attrib.use_srq);
>  }
>
>  static ssize_t srpt_tpg_attrib_use_srq_store(struct config_item *item,
> @@ -3646,7 +3646,7 @@ static struct rdma_cm_id *srpt_create_rdma_id(struct sockaddr *listen_addr)
>
>  static ssize_t srpt_rdma_cm_port_show(struct config_item *item, char *page)
>  {
> -       return sprintf(page, "%d\n", rdma_cm_port);
> +       return sysfs_emit(page, "%d\n", rdma_cm_port);
>  }
>
>  static ssize_t srpt_rdma_cm_port_store(struct config_item *item,
> @@ -3702,7 +3702,7 @@ static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
>         struct se_portal_group *se_tpg = to_tpg(item);
>         struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
>
> -       return snprintf(page, PAGE_SIZE, "%d\n", sport->enabled);
> +       return sysfs_emit(page, "%d\n", sport->enabled);
>  }
>
>  static ssize_t srpt_tpg_enable_store(struct config_item *item,
> @@ -3809,7 +3809,7 @@ static void srpt_drop_tport(struct se_wwn *wwn)
>
>  static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
>  {
> -       return scnprintf(buf, PAGE_SIZE, "\n");
> +       return sysfs_emit(buf, "\n");
>  }
>
>  CONFIGFS_ATTR_RO(srpt_wwn_, version);
> --
> 2.26.0
>

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 3/4] RDMA: manual changes for sysfs_emit and neatening
  2020-10-08  2:36 ` [PATCH 3/4] RDMA: manual changes for sysfs_emit and neatening Joe Perches
  2020-10-09 15:20   ` Jason Gunthorpe
@ 2020-10-12  5:30   ` Jinpu Wang
  2020-10-28 17:25   ` Jason Gunthorpe
  2 siblings, 0 replies; 26+ messages in thread
From: Jinpu Wang @ 2020-10-12  5:30 UTC (permalink / raw)
  To: Joe Perches
  Cc: Doug Ledford, Jason Gunthorpe, Potnuri Bharat Teja,
	Mike Marciniszyn, Dennis Dalessandro, Yishai Hadas,
	Leon Romanovsky, Christian Benvenuti, Nelson Escobar,
	Parvi Kaustubhi, Danil Kipnis, Bart Van Assche, linux-rdma

On Thu, Oct 8, 2020 at 4:36 AM Joe Perches <joe@perches.com> wrote:
>
> Make changes to use sysfs_emit in the RDMA code as cocci scripts can not
> be written to handle _all_ the possible variants of various sprintf family
> uses in sysfs show functions.
>
> While there, make the code more legible and update its style to be more
> like the typical kernel styles.
>
> Miscellanea:
>
> o Use intermediate pointers for dereferences
> o Add and use string lookup functions
> o return early when any intermediate call fails so normal return is
>   at the bottom of the function
> o mlx4/mcg.c:sysfs_show_group: use scnprintf to format intermediate strings
>
> Signed-off-by: Joe Perches <joe@perches.com>
> ---
>  drivers/infiniband/core/sysfs.c              | 60 +++++++-------
>  drivers/infiniband/hw/cxgb4/provider.c       |  5 +-
>  drivers/infiniband/hw/hfi1/sysfs.c           | 38 ++++-----
>  drivers/infiniband/hw/mlx4/main.c            |  5 +-
>  drivers/infiniband/hw/mlx4/mcg.c             | 82 +++++++++++---------
>  drivers/infiniband/hw/mlx4/sysfs.c           | 47 ++++++-----
>  drivers/infiniband/hw/mlx5/main.c            |  4 +-
>  drivers/infiniband/hw/mthca/mthca_provider.c | 29 ++++---
>  drivers/infiniband/hw/qib/qib_sysfs.c        | 45 +++++------
>  drivers/infiniband/hw/usnic/usnic_ib_sysfs.c | 66 +++++++---------
>  drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c | 21 +++--
>  drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c | 13 ++--
>  drivers/infiniband/ulp/srp/ib_srp.c          |  4 +
>  13 files changed, 206 insertions(+), 213 deletions(-)
>

>
> diff --git a/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c b/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c
> index 0c767582286b..51ba82fc425c 100644
> --- a/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c
> +++ b/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c
> @@ -196,11 +196,10 @@ static struct kobj_attribute rtrs_clt_state_attr =
>         __ATTR(state, 0444, rtrs_clt_state_show, NULL);
>
>  static ssize_t rtrs_clt_reconnect_show(struct kobject *kobj,
> -                                       struct kobj_attribute *attr,
> -                                       char *buf)
> +                                      struct kobj_attribute *attr,
> +                                      char *buf)
>  {
> -       return sysfs_emit(buf, "Usage: echo 1 > %s\n",
> -                         attr->attr.name);
> +       return sysfs_emit(buf, "Usage: echo 1 > %s\n", attr->attr.name);
>  }
>
>  static ssize_t rtrs_clt_reconnect_store(struct kobject *kobj,
> @@ -228,11 +227,10 @@ static struct kobj_attribute rtrs_clt_reconnect_attr =
>                rtrs_clt_reconnect_store);
>
>  static ssize_t rtrs_clt_disconnect_show(struct kobject *kobj,
> -                                        struct kobj_attribute *attr,
> -                                        char *buf)
> +                                       struct kobj_attribute *attr,
> +                                       char *buf)
>  {
> -       return sysfs_emit(buf, "Usage: echo 1 > %s\n",
> -                         attr->attr.name);
> +       return sysfs_emit(buf, "Usage: echo 1 > %s\n", attr->attr.name);
>  }
>
>  static ssize_t rtrs_clt_disconnect_store(struct kobject *kobj,
> @@ -260,11 +258,10 @@ static struct kobj_attribute rtrs_clt_disconnect_attr =
>                rtrs_clt_disconnect_store);
>
>  static ssize_t rtrs_clt_remove_path_show(struct kobject *kobj,
> -                                         struct kobj_attribute *attr,
> -                                         char *buf)
> +                                        struct kobj_attribute *attr,
> +                                        char *buf)
>  {
> -       return sysfs_emit(buf, "Usage: echo 1 > %s\n",
> -                         attr->attr.name);
> +       return sysfs_emit(buf, "Usage: echo 1 > %s\n", attr->attr.name);
>  }
>
>  static ssize_t rtrs_clt_remove_path_store(struct kobject *kobj,
> diff --git a/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c b/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
> index 381a776ce404..6e7bebe4e064 100644
> --- a/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
> +++ b/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
> @@ -27,11 +27,10 @@ static struct kobj_type ktype = {
>  };
>
>  static ssize_t rtrs_srv_disconnect_show(struct kobject *kobj,
> -                                        struct kobj_attribute *attr,
> -                                        char *buf)
> +                                       struct kobj_attribute *attr,
> +                                       char *buf)
>  {
> -       return sysfs_emit(buf, "Usage: echo 1 > %s\n",
> -                         attr->attr.name);
> +       return sysfs_emit(buf, "Usage: echo 1 > %s\n", attr->attr.name);
>  }
>
>  static ssize_t rtrs_srv_disconnect_store(struct kobject *kobj,
> @@ -72,8 +71,7 @@ static ssize_t rtrs_srv_hca_port_show(struct kobject *kobj,
>         sess = container_of(kobj, typeof(*sess), kobj);
>         usr_con = sess->s.con[0];
>
> -       return sysfs_emit(page, "%u\n",
> -                         usr_con->cm_id->port_num);
> +       return sysfs_emit(page, "%u\n", usr_con->cm_id->port_num);
>  }
>
>  static struct kobj_attribute rtrs_srv_hca_port_attr =
> @@ -87,8 +85,7 @@ static ssize_t rtrs_srv_hca_name_show(struct kobject *kobj,
>
>         sess = container_of(kobj, struct rtrs_srv_sess, kobj);
>
> -       return sysfs_emit(page, "%s\n",
> -                         sess->s.dev->ib_dev->name);
> +       return sysfs_emit(page, "%s\n", sess->s.dev->ib_dev->name);
>  }
>
For rtrs, looks good to me!
Thanks,
Acked-by: Jack Wang <jinpu.wang@cloud.ionos.com>

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH-next 0/4] RDMA: sprintf to sysfs_emit conversions
  2020-10-08  5:41 ` [PATCH-next 0/4] RDMA: sprintf to sysfs_emit conversions Leon Romanovsky
  2020-10-08  5:52   ` Joe Perches
@ 2020-10-15  5:29   ` Joe Perches
  1 sibling, 0 replies; 26+ messages in thread
From: Joe Perches @ 2020-10-15  5:29 UTC (permalink / raw)
  To: Leon Romanovsky
  Cc: Doug Ledford, Jason Gunthorpe, linux-rdma, target-devel, linux-kernel

On Thu, 2020-10-08 at 08:41 +0300, Leon Romanovsky wrote:
> On Wed, Oct 07, 2020 at 07:36:23PM -0700, Joe Perches wrote:
> > A recent commit added a sysfs_emit and sysfs_emit_at to allow various
> > sysfs show functions to ensure that the PAGE_SIZE buffer argument is
> > never overrun and always NUL terminated.
> 
> Unfortunately but the sysfs_emit commit is not in rdma-next tree yet.

It is in Linus' tree now.



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH-next 0/4] RDMA: sprintf to sysfs_emit conversions
  2020-10-08  2:36 [PATCH-next 0/4] RDMA: sprintf to sysfs_emit conversions Joe Perches
                   ` (4 preceding siblings ...)
  2020-10-08  5:41 ` [PATCH-next 0/4] RDMA: sprintf to sysfs_emit conversions Leon Romanovsky
@ 2020-10-26 22:54 ` Jason Gunthorpe
  2020-10-28 18:27 ` Jason Gunthorpe
  6 siblings, 0 replies; 26+ messages in thread
From: Jason Gunthorpe @ 2020-10-26 22:54 UTC (permalink / raw)
  To: Joe Perches; +Cc: Doug Ledford, linux-rdma, target-devel, linux-kernel

On Wed, Oct 07, 2020 at 07:36:23PM -0700, Joe Perches wrote:
> A recent commit added a sysfs_emit and sysfs_emit_at to allow various
> sysfs show functions to ensure that the PAGE_SIZE buffer argument is
> never overrun and always NUL terminated.
> 
> Convert the RDMA/InfiniBand subsystem to use these new functions.
> 
> The first 2 patches exclusively used coccinelle to convert uses.
> The third and fourth patches were done manually.
> 
> Compiled allyesconfig and defconfig with all infiniband options selected
> no warnings, but untested, no hardward
> 
> Overall object size is reduced
> 
> total size: allyesconfig x86-64
> new: 8364003	1680968	 131520	10176491 9b47eb	(TOTALS)
> old: 8365883	1681032	 131520	10178435 9b4f83	(TOTALS)
> 
> total size: defconfig x86-64 with all infiniband selected
> new; 1359153	 131228	   1910  1492291 16c543	(TOTALS)
> old: 1359422	 131228	   1910  1492560 16c650	(TOTALS)
> 
> Joe Perches (4):
>   RDMA: Convert sysfs device * show functions to use sysfs_emit()
>   RDMA: Convert sysfs kobject * show functions to use sysfs_emit()

First two applied to for-next

>   RDMA: manual changes for sysfs_emit and neatening
>   RDMA: Convert various random sprintf sysfs _show uses to sysfs_emit

Will probably do these two later this week/next

Thanks,
Jason

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 3/4] RDMA: manual changes for sysfs_emit and neatening
  2020-10-08  2:36 ` [PATCH 3/4] RDMA: manual changes for sysfs_emit and neatening Joe Perches
  2020-10-09 15:20   ` Jason Gunthorpe
  2020-10-12  5:30   ` Jinpu Wang
@ 2020-10-28 17:25   ` Jason Gunthorpe
  2020-10-28 17:54     ` Joe Perches
  2 siblings, 1 reply; 26+ messages in thread
From: Jason Gunthorpe @ 2020-10-28 17:25 UTC (permalink / raw)
  To: Joe Perches
  Cc: Doug Ledford, Potnuri Bharat Teja, Mike Marciniszyn,
	Dennis Dalessandro, Yishai Hadas, Leon Romanovsky,
	Christian Benvenuti, Nelson Escobar, Parvi Kaustubhi,
	Danil Kipnis, Jack Wang, Bart Van Assche, linux-rdma

On Wed, Oct 07, 2020 at 07:36:26PM -0700, Joe Perches wrote:

> @@ -653,10 +651,7 @@ static ssize_t serial_show(struct device *device,
>  		rdma_device_to_drv_device(device, struct qib_ibdev, rdi.ibdev);
>  	struct qib_devdata *dd = dd_from_dev(dev);
>  
> -	buf[sizeof(dd->serial)] = '\0';
> -	memcpy(buf, dd->serial, sizeof(dd->serial));
> -	strcat(buf, "\n");
> -	return strlen(buf);
> +	return sysfs_emit(buf, "%s\n", dd->serial);
>  }

This is not the same thing? dd->serial does not look null terminated,
eg it is filled like this:

		memcpy(dd->serial, ifp->if_serial, sizeof(ifp->if_serial));

From data read off the flash

Jason

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 3/4] RDMA: manual changes for sysfs_emit and neatening
  2020-10-28 17:25   ` Jason Gunthorpe
@ 2020-10-28 17:54     ` Joe Perches
  2020-10-28 18:23       ` Jason Gunthorpe
  2020-10-29 17:16       ` Joe Perches
  0 siblings, 2 replies; 26+ messages in thread
From: Joe Perches @ 2020-10-28 17:54 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Doug Ledford, Potnuri Bharat Teja, Mike Marciniszyn,
	Dennis Dalessandro, Yishai Hadas, Leon Romanovsky,
	Christian Benvenuti, Nelson Escobar, Parvi Kaustubhi,
	Danil Kipnis, Jack Wang, Bart Van Assche, linux-rdma

On Wed, 2020-10-28 at 14:25 -0300, Jason Gunthorpe wrote:
> On Wed, Oct 07, 2020 at 07:36:26PM -0700, Joe Perches wrote:
> 
> > @@ -653,10 +651,7 @@ static ssize_t serial_show(struct device *device,
> >  		rdma_device_to_drv_device(device, struct qib_ibdev, rdi.ibdev);
> >  	struct qib_devdata *dd = dd_from_dev(dev);
> >  
> > 
> > -	buf[sizeof(dd->serial)] = '\0';
> > -	memcpy(buf, dd->serial, sizeof(dd->serial));
> > -	strcat(buf, "\n");
> > -	return strlen(buf);
> > +	return sysfs_emit(buf, "%s\n", dd->serial);
> >  }
> 
> This is not the same thing? dd->serial does not look null terminated,
> eg it is filled like this:
> 
> 		memcpy(dd->serial, ifp->if_serial, sizeof(ifp->if_serial));
> 
> From data read off the flash

It seems you are correct.

Maybe instead:
---
static ssize_t serial_show(struct device *device,
			   struct device_attribute *attr, char *buf)
{
	struct qib_ibdev *dev =
		rdma_device_to_drv_device(device, struct qib_ibdev, rdi.ibdev);
	struct qib_devdata *dd = dd_from_dev(dev);
	const u8 *end = memchr(dd->serial, 0, ARRAY_SIZE(dd->serial));
	int size = end ? end - dd->serial : ARRAY_SIZE(dd->serial);

	return sysfs_emit(buf, "%*s\n", size, dd->serial);
}
static DEVICE_ATTR_RO(serial);



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 4/4] RDMA: Convert various random sprintf sysfs _show uses to sysfs_emit
  2020-10-08  2:36 ` [PATCH 4/4] RDMA: Convert various random sprintf sysfs _show uses to sysfs_emit Joe Perches
  2020-10-09 15:27   ` Jason Gunthorpe
  2020-10-12  5:25   ` Jinpu Wang
@ 2020-10-28 18:12   ` Jason Gunthorpe
  2020-10-28 18:22     ` Joe Perches
  2 siblings, 1 reply; 26+ messages in thread
From: Jason Gunthorpe @ 2020-10-28 18:12 UTC (permalink / raw)
  To: Joe Perches
  Cc: Doug Ledford, Mike Marciniszyn, Dennis Dalessandro, Yishai Hadas,
	Christian Benvenuti, Nelson Escobar, Parvi Kaustubhi,
	Danil Kipnis, Jack Wang, Bart Van Assche, linux-rdma

On Wed, Oct 07, 2020 at 07:36:27PM -0700, Joe Perches wrote:
> diff --git a/drivers/infiniband/hw/mlx4/sysfs.c b/drivers/infiniband/hw/mlx4/sysfs.c
> index 75d50383da89..fc6c98b608d3 100644
> +++ b/drivers/infiniband/hw/mlx4/sysfs.c
> @@ -444,16 +444,17 @@ static ssize_t show_port_pkey(struct mlx4_port *p, struct port_attribute *attr,
>  {
>  	struct port_table_attribute *tab_attr =
>  		container_of(attr, struct port_table_attribute, attr);
> -	ssize_t ret = -ENODEV;
> +	int len;
> +	struct pkey_mgt *m = &p->dev->pkeys;
> +	u8 key = m->virt2phys_pkey[p->slave][p->port_num - 1][tab_attr->index];
>  
> -	if (p->dev->pkeys.virt2phys_pkey[p->slave][p->port_num - 1][tab_attr->index] >=
> -	    (p->dev->dev->caps.pkey_table_len[p->port_num]))
> -		ret = sprintf(buf, "none\n");
> +	if (key >= p->dev->dev->caps.pkey_table_len[p->port_num])
> +		len = sysfs_emit(buf, "none\n");
>  	else
> -		ret = sprintf(buf, "%d\n",
> -			      p->dev->pkeys.virt2phys_pkey[p->slave]
> -			      [p->port_num - 1][tab_attr->index]);
> -	return ret;
> +		len = sysfs_emit(buf, "%d\n",
> +				 p->dev->pkeys.virt2phys_pkey[p->slave]
> +				 [p->port_num - 1][tab_attr->index]);
> +	return len;
>  }

This duplication of virt2phys_pkey can be simplified to:

static ssize_t show_port_pkey(struct mlx4_port *p, struct port_attribute *attr,
			      char *buf)
{
	struct port_table_attribute *tab_attr =
		container_of(attr, struct port_table_attribute, attr);
	struct pkey_mgt *m = &p->dev->pkeys;
	u8 key = m->virt2phys_pkey[p->slave][p->port_num - 1][tab_attr->index];

	if (key >= p->dev->dev->caps.pkey_table_len[p->port_num])
		return sysfs_emit(buf, "none\n");
	return sysfs_emit(buf, "%d\n", key);
}

(I adjusted it)

Jason

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 4/4] RDMA: Convert various random sprintf sysfs _show uses to sysfs_emit
  2020-10-28 18:12   ` Jason Gunthorpe
@ 2020-10-28 18:22     ` Joe Perches
  0 siblings, 0 replies; 26+ messages in thread
From: Joe Perches @ 2020-10-28 18:22 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Doug Ledford, Mike Marciniszyn, Dennis Dalessandro, Yishai Hadas,
	Christian Benvenuti, Nelson Escobar, Parvi Kaustubhi,
	Danil Kipnis, Jack Wang, Bart Van Assche, linux-rdma

On Wed, 2020-10-28 at 15:12 -0300, Jason Gunthorpe wrote:
> On Wed, Oct 07, 2020 at 07:36:27PM -0700, Joe Perches wrote:
> > diff --git a/drivers/infiniband/hw/mlx4/sysfs.c b/drivers/infiniband/hw/mlx4/sysfs.c
[]
> > @@ -444,16 +444,17 @@ static ssize_t show_port_pkey(struct mlx4_port *p, struct port_attribute *attr,
> >  {
[]
> > +	int len;
> > +	struct pkey_mgt *m = &p->dev->pkeys;
> > +	u8 key = m->virt2phys_pkey[p->slave][p->port_num - 1][tab_attr->index];
> >  
> > 
> > -	if (p->dev->pkeys.virt2phys_pkey[p->slave][p->port_num - 1][tab_attr->index] >=
> > -	    (p->dev->dev->caps.pkey_table_len[p->port_num]))
> > -		ret = sprintf(buf, "none\n");
> > +	if (key >= p->dev->dev->caps.pkey_table_len[p->port_num])
> > +		len = sysfs_emit(buf, "none\n");
> >  	else
> > -		ret = sprintf(buf, "%d\n",
> > -			      p->dev->pkeys.virt2phys_pkey[p->slave]
> > -			      [p->port_num - 1][tab_attr->index]);
> > -	return ret;
> > +		len = sysfs_emit(buf, "%d\n",
> > +				 p->dev->pkeys.virt2phys_pkey[p->slave]
> > +				 [p->port_num - 1][tab_attr->index]);
> > +	return len;
> >  }
> 
> This duplication of virt2phys_pkey can be simplified to:
> 
> static ssize_t show_port_pkey(struct mlx4_port *p, struct port_attribute *attr,
> 			      char *buf)
> {
> 	struct port_table_attribute *tab_attr =
> 		container_of(attr, struct port_table_attribute, attr);
> 	struct pkey_mgt *m = &p->dev->pkeys;
> 	u8 key = m->virt2phys_pkey[p->slave][p->port_num - 1][tab_attr->index];
> 
> 	if (key >= p->dev->dev->caps.pkey_table_len[p->port_num])
> 		return sysfs_emit(buf, "none\n");
> 	return sysfs_emit(buf, "%d\n", key);
> }
> 
> (I adjusted it)

Thanks, it seems better without using the len automatic too.



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 3/4] RDMA: manual changes for sysfs_emit and neatening
  2020-10-28 17:54     ` Joe Perches
@ 2020-10-28 18:23       ` Jason Gunthorpe
  2020-10-29 17:16       ` Joe Perches
  1 sibling, 0 replies; 26+ messages in thread
From: Jason Gunthorpe @ 2020-10-28 18:23 UTC (permalink / raw)
  To: Joe Perches
  Cc: Doug Ledford, Potnuri Bharat Teja, Mike Marciniszyn,
	Dennis Dalessandro, Yishai Hadas, Leon Romanovsky,
	Christian Benvenuti, Nelson Escobar, Parvi Kaustubhi,
	Danil Kipnis, Jack Wang, Bart Van Assche, linux-rdma

On Wed, Oct 28, 2020 at 10:54:19AM -0700, Joe Perches wrote:
> On Wed, 2020-10-28 at 14:25 -0300, Jason Gunthorpe wrote:
> > On Wed, Oct 07, 2020 at 07:36:26PM -0700, Joe Perches wrote:
> > 
> > > @@ -653,10 +651,7 @@ static ssize_t serial_show(struct device *device,
> > >  		rdma_device_to_drv_device(device, struct qib_ibdev, rdi.ibdev);
> > >  	struct qib_devdata *dd = dd_from_dev(dev);
> > >  
> > > 
> > > -	buf[sizeof(dd->serial)] = '\0';
> > > -	memcpy(buf, dd->serial, sizeof(dd->serial));
> > > -	strcat(buf, "\n");
> > > -	return strlen(buf);
> > > +	return sysfs_emit(buf, "%s\n", dd->serial);
> > >  }
> > 
> > This is not the same thing? dd->serial does not look null terminated,
> > eg it is filled like this:
> > 
> > 		memcpy(dd->serial, ifp->if_serial, sizeof(ifp->if_serial));
> > 
> > From data read off the flash
> 
> It seems you are correct.
> 
> Maybe instead:
> static ssize_t serial_show(struct device *device,
> 			   struct device_attribute *attr, char *buf)
> {
> 	struct qib_ibdev *dev =
> 		rdma_device_to_drv_device(device, struct qib_ibdev, rdi.ibdev);
> 	struct qib_devdata *dd = dd_from_dev(dev);
> 	const u8 *end = memchr(dd->serial, 0, ARRAY_SIZE(dd->serial));
> 	int size = end ? end - dd->serial : ARRAY_SIZE(dd->serial);
> 
> 	return sysfs_emit(buf, "%*s\n", size, dd->serial);
> }
> static DEVICE_ATTR_RO(serial);

I adjusted it

Thanks,
Jason

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH-next 0/4] RDMA: sprintf to sysfs_emit conversions
  2020-10-08  2:36 [PATCH-next 0/4] RDMA: sprintf to sysfs_emit conversions Joe Perches
                   ` (5 preceding siblings ...)
  2020-10-26 22:54 ` Jason Gunthorpe
@ 2020-10-28 18:27 ` Jason Gunthorpe
  6 siblings, 0 replies; 26+ messages in thread
From: Jason Gunthorpe @ 2020-10-28 18:27 UTC (permalink / raw)
  To: Joe Perches; +Cc: Doug Ledford, linux-rdma, target-devel, linux-kernel

On Wed, Oct 07, 2020 at 07:36:23PM -0700, Joe Perches wrote:
> Joe Perches (4):
>   RDMA: manual changes for sysfs_emit and neatening
>   RDMA: Convert various random sprintf sysfs _show uses to sysfs_emit

Applied to rdma for-next, thanks

Jason

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 3/4] RDMA: manual changes for sysfs_emit and neatening
  2020-10-28 17:54     ` Joe Perches
  2020-10-28 18:23       ` Jason Gunthorpe
@ 2020-10-29 17:16       ` Joe Perches
  2020-10-31  0:04         ` Jason Gunthorpe
  1 sibling, 1 reply; 26+ messages in thread
From: Joe Perches @ 2020-10-29 17:16 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Doug Ledford, Potnuri Bharat Teja, Mike Marciniszyn,
	Dennis Dalessandro, Yishai Hadas, Leon Romanovsky,
	Christian Benvenuti, Nelson Escobar, Parvi Kaustubhi,
	Danil Kipnis, Jack Wang, Bart Van Assche, linux-rdma

On Wed, 2020-10-28 at 10:54 -0700, Joe Perches wrote:
> On Wed, 2020-10-28 at 14:25 -0300, Jason Gunthorpe wrote:
> > On Wed, Oct 07, 2020 at 07:36:26PM -0700, Joe Perches wrote:
> > 
> > > @@ -653,10 +651,7 @@ static ssize_t serial_show(struct device *device,
> > >  		rdma_device_to_drv_device(device, struct qib_ibdev, rdi.ibdev);
> > >  	struct qib_devdata *dd = dd_from_dev(dev);
> > >  
> > > 
> > > -	buf[sizeof(dd->serial)] = '\0';
> > > -	memcpy(buf, dd->serial, sizeof(dd->serial));
> > > -	strcat(buf, "\n");
> > > -	return strlen(buf);
> > > +	return sysfs_emit(buf, "%s\n", dd->serial);
> > >  }
> > 
> > This is not the same thing? dd->serial does not look null terminated,
> > eg it is filled like this:
> > 
> > 		memcpy(dd->serial, ifp->if_serial, sizeof(ifp->if_serial));
> > 
> > From data read off the flash
> 
> It seems you are correct.
> 
> Maybe instead:
> ---
> static ssize_t serial_show(struct device *device,
> 			   struct device_attribute *attr, char *buf)
> {
> 	struct qib_ibdev *dev =
> 		rdma_device_to_drv_device(device, struct qib_ibdev, rdi.ibdev);
> 	struct qib_devdata *dd = dd_from_dev(dev);
> 	const u8 *end = memchr(dd->serial, 0, ARRAY_SIZE(dd->serial));
> 	int size = end ? end - dd->serial : ARRAY_SIZE(dd->serial);
> 
> 	return sysfs_emit(buf, "%*s\n", size, dd->serial);

I believe for this to actually be correct, this should be:

	return sysfs_emit(buf, "%.*s\n", size, dd->serial);



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 3/4] RDMA: manual changes for sysfs_emit and neatening
  2020-10-29 17:16       ` Joe Perches
@ 2020-10-31  0:04         ` Jason Gunthorpe
  0 siblings, 0 replies; 26+ messages in thread
From: Jason Gunthorpe @ 2020-10-31  0:04 UTC (permalink / raw)
  To: Joe Perches, Dennis Dalessandro
  Cc: Doug Ledford, Potnuri Bharat Teja, Mike Marciniszyn,
	Yishai Hadas, Leon Romanovsky, Christian Benvenuti,
	Nelson Escobar, Parvi Kaustubhi, Danil Kipnis, Jack Wang,
	Bart Van Assche, linux-rdma

On Thu, Oct 29, 2020 at 10:16:46AM -0700, Joe Perches wrote:
> On Wed, 2020-10-28 at 10:54 -0700, Joe Perches wrote:
> > On Wed, 2020-10-28 at 14:25 -0300, Jason Gunthorpe wrote:
> > > On Wed, Oct 07, 2020 at 07:36:26PM -0700, Joe Perches wrote:
> > > 
> > > > @@ -653,10 +651,7 @@ static ssize_t serial_show(struct device *device,
> > > >  		rdma_device_to_drv_device(device, struct qib_ibdev, rdi.ibdev);
> > > >  	struct qib_devdata *dd = dd_from_dev(dev);
> > > >  
> > > > 
> > > > -	buf[sizeof(dd->serial)] = '\0';
> > > > -	memcpy(buf, dd->serial, sizeof(dd->serial));
> > > > -	strcat(buf, "\n");
> > > > -	return strlen(buf);
> > > > +	return sysfs_emit(buf, "%s\n", dd->serial);
> > > >  }
> > > 
> > > This is not the same thing? dd->serial does not look null terminated,
> > > eg it is filled like this:
> > > 
> > > 		memcpy(dd->serial, ifp->if_serial, sizeof(ifp->if_serial));
> > > 
> > > From data read off the flash
> > 
> > It seems you are correct.
> > 
> > Maybe instead:
> > static ssize_t serial_show(struct device *device,
> > 			   struct device_attribute *attr, char *buf)
> > {
> > 	struct qib_ibdev *dev =
> > 		rdma_device_to_drv_device(device, struct qib_ibdev, rdi.ibdev);
> > 	struct qib_devdata *dd = dd_from_dev(dev);
> > 	const u8 *end = memchr(dd->serial, 0, ARRAY_SIZE(dd->serial));
> > 	int size = end ? end - dd->serial : ARRAY_SIZE(dd->serial);
> > 
> > 	return sysfs_emit(buf, "%*s\n", size, dd->serial);
> 
> I believe for this to actually be correct, this should be:
> 
> 	return sysfs_emit(buf, "%.*s\n", size, dd->serial);

Yes, I think so, I squished it in

Maybe Dennis can check it in the wip/jgg/for-next branch?

Thanks,
Jason

^ permalink raw reply	[flat|nested] 26+ messages in thread

end of thread, other threads:[~2020-10-31  0:04 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-08  2:36 [PATCH-next 0/4] RDMA: sprintf to sysfs_emit conversions Joe Perches
2020-10-08  2:36 ` [PATCH 1/4] RDMA: Convert sysfs device * show functions to use sysfs_emit() Joe Perches
2020-10-09 14:07   ` Jason Gunthorpe
2020-10-08  2:36 ` [PATCH 2/4] RDMA: Convert sysfs kobject " Joe Perches
2020-10-09 14:09   ` Jason Gunthorpe
2020-10-09 16:25     ` Joe Perches
2020-10-12  5:21   ` Jinpu Wang
2020-10-08  2:36 ` [PATCH 3/4] RDMA: manual changes for sysfs_emit and neatening Joe Perches
2020-10-09 15:20   ` Jason Gunthorpe
2020-10-12  5:30   ` Jinpu Wang
2020-10-28 17:25   ` Jason Gunthorpe
2020-10-28 17:54     ` Joe Perches
2020-10-28 18:23       ` Jason Gunthorpe
2020-10-29 17:16       ` Joe Perches
2020-10-31  0:04         ` Jason Gunthorpe
2020-10-08  2:36 ` [PATCH 4/4] RDMA: Convert various random sprintf sysfs _show uses to sysfs_emit Joe Perches
2020-10-09 15:27   ` Jason Gunthorpe
2020-10-12  5:25   ` Jinpu Wang
2020-10-28 18:12   ` Jason Gunthorpe
2020-10-28 18:22     ` Joe Perches
2020-10-08  5:41 ` [PATCH-next 0/4] RDMA: sprintf to sysfs_emit conversions Leon Romanovsky
2020-10-08  5:52   ` Joe Perches
2020-10-08  7:02     ` Leon Romanovsky
2020-10-15  5:29   ` Joe Perches
2020-10-26 22:54 ` Jason Gunthorpe
2020-10-28 18:27 ` Jason Gunthorpe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).