diff mbox series

[2/4] RDMA: Convert sysfs kobject * show functions to use sysfs_emit()

Message ID 7761c1efaebb96c432c85171d58405c25a824ccd.1602122880.git.joe@perches.com (mailing list archive)
State Accepted
Delegated to: Jason Gunthorpe
Headers show
Series RDMA: sprintf to sysfs_emit conversions | expand

Commit Message

Joe Perches Oct. 8, 2020, 2:36 a.m. UTC
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(-)

Comments

Jason Gunthorpe Oct. 9, 2020, 2:09 p.m. UTC | #1
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
Joe Perches Oct. 9, 2020, 4:25 p.m. UTC | #2
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);
Jinpu Wang Oct. 12, 2020, 5:21 a.m. UTC | #3
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
>
diff mbox series

Patch

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 =