Commit 45808361 authored by Joe Perches's avatar Joe Perches Committed by Jason Gunthorpe
Browse files

RDMA: Manual changes for sysfs_emit and neatening

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

Link: https://lore.kernel.org/r/f5c9e4c9d8dafca1b7b70bd597ee7f8f219c31c8.1602122880.git.joe@perches.com


Signed-off-by: default avatarJoe Perches <joe@perches.com>
Acked-by: default avatarJack Wang <jinpu.wang@cloud.ionos.com>
Signed-off-by: default avatarJason Gunthorpe <jgg@nvidia.com>
parent 32053e58
Loading
Loading
Loading
Loading
+36 −29
Original line number Diff line number Diff line
@@ -1224,29 +1224,34 @@ static int add_port(struct ib_core_device *coredev, int port_num)
	return ret;
}

static ssize_t node_type_show(struct device *device,
			      struct device_attribute *attr, char *buf)
static const char *node_type_string(int node_type)
{
	struct ib_device *dev = rdma_device_to_ibdev(device);

	switch (dev->node_type) {
	switch (node_type) {
	case RDMA_NODE_IB_CA:
		return sysfs_emit(buf, "%d: CA\n", dev->node_type);
		return "CA";
	case RDMA_NODE_IB_SWITCH:
		return "switch";
	case RDMA_NODE_IB_ROUTER:
		return "router";
	case RDMA_NODE_RNIC:
		return sysfs_emit(buf, "%d: RNIC\n", dev->node_type);
		return "RNIC";
	case RDMA_NODE_USNIC:
		return sysfs_emit(buf, "%d: usNIC\n", dev->node_type);
		return "usNIC";
	case RDMA_NODE_USNIC_UDP:
		return sysfs_emit(buf, "%d: usNIC UDP\n", dev->node_type);
		return "usNIC UDP";
	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 "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);

	return sysfs_emit(buf, "%d: %s\n", dev->node_type,
			  node_type_string(dev->node_type));
}
static DEVICE_ATTR_RO(node_type);

@@ -1254,13 +1259,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]));
	return sysfs_emit(buf, "%04x:%04x:%04x:%04x\n",
			  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 +1273,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 +1315,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);

+17 −21
Original line number Diff line number Diff line
@@ -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;
	}
	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);

+2 −0
Original line number Diff line number Diff line
@@ -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);
+46 −36
Original line number Diff line number Diff line
@@ -993,47 +993,57 @@ static ssize_t sysfs_show_group(struct device *dev,
	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)",
		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)",
		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 += sprintf(buf + len, "%1d [%02d,%02d,%02d] %4d %4s %5s     ",

	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],
			 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",
	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);
	}

	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.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.ratesel_rate & 0x3f),
			     (hoplimit & 0xf0000000) >> 28,
			     (hoplimit & 0x0fffff00) >> 8,
			     (hoplimit & 0x000000ff),
			     group->rec.proxy_join);

	return len;
+19 −25
Original line number Diff line number Diff line
@@ -117,22 +117,24 @@ 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 +544,10 @@ 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 +556,10 @@ 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,
Loading