diff mbox series

[for-next,03/10] RDMA/rtrs: Use sysfs_emit instead of s*printf function for sysfs show

Message ID 20210730131832.118865-4-jinpu.wang@ionos.com (mailing list archive)
State Changes Requested
Headers show
Series Misc update for RTRS | expand

Commit Message

Jinpu Wang July 30, 2021, 1:18 p.m. UTC
From: Md Haris Iqbal <haris.iqbal@ionos.com>

sysfs_emit function was added to be aware of the PAGE_SIZE maximum of
the temporary buffer used for outputting sysfs content, so there is no
possible overruns. So replace the uses of any s*printf functions for
the sysfs show functions with sysfs_emit.

Signed-off-by: Md Haris Iqbal <haris.iqbal@ionos.com>
Signed-off-by: Jack Wang <jinpu.wang@ionos.com>
---
 drivers/infiniband/ulp/rtrs/rtrs-clt-stats.c | 24 +++++++++-----------
 drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c |  2 +-
 2 files changed, 12 insertions(+), 14 deletions(-)

Comments

Leon Romanovsky Aug. 2, 2021, 6:52 a.m. UTC | #1
On Fri, Jul 30, 2021 at 03:18:25PM +0200, Jack Wang wrote:
> From: Md Haris Iqbal <haris.iqbal@ionos.com>
> 
> sysfs_emit function was added to be aware of the PAGE_SIZE maximum of
> the temporary buffer used for outputting sysfs content, so there is no
> possible overruns. So replace the uses of any s*printf functions for
> the sysfs show functions with sysfs_emit.
> 
> Signed-off-by: Md Haris Iqbal <haris.iqbal@ionos.com>
> Signed-off-by: Jack Wang <jinpu.wang@ionos.com>
> ---
>  drivers/infiniband/ulp/rtrs/rtrs-clt-stats.c | 24 +++++++++-----------
>  drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c |  2 +-
>  2 files changed, 12 insertions(+), 14 deletions(-)
> 
> diff --git a/drivers/infiniband/ulp/rtrs/rtrs-clt-stats.c b/drivers/infiniband/ulp/rtrs/rtrs-clt-stats.c
> index 26bbe5d6dff5..c5c047aa45a4 100644
> --- a/drivers/infiniband/ulp/rtrs/rtrs-clt-stats.c
> +++ b/drivers/infiniband/ulp/rtrs/rtrs-clt-stats.c
> @@ -45,24 +45,23 @@ int rtrs_clt_stats_migration_cnt_to_str(struct rtrs_clt_stats *stats,
>  	size_t used;
>  	int cpu;
>  
> -	used = scnprintf(buf, len, "    ");
> +	used = sysfs_emit(buf, "    ");
>  	for_each_possible_cpu(cpu)
> -		used += scnprintf(buf + used, len - used, " CPU%u", cpu);
> +		used += sysfs_emit_at(buf, used, " CPU%u", cpu);
>  
> -	used += scnprintf(buf + used, len - used, "\nfrom:");
> +	used += sysfs_emit_at(buf, used, "\nfrom:");

"\nfrom" and "\nto" are abuse of sysfs rules.
https://lore.kernel.org/kernelnewbies/7a353c64-a81f-a149-9541-ef328a197761@gmail.com/T/#m8bf898fcdb4a5371781bbc9646993a50fa950fbc
https://lore.kernel.org/kernelnewbies/7a353c64-a81f-a149-9541-ef328a197761@gmail.com/T/#m9ce6f045a863597922012d71a6719d6d90c8e0a6

Thanks
Haris Iqbal Aug. 2, 2021, 2:18 p.m. UTC | #2
On Mon, Aug 2, 2021 at 8:52 AM Leon Romanovsky <leon@kernel.org> wrote:
>
> On Fri, Jul 30, 2021 at 03:18:25PM +0200, Jack Wang wrote:
> > From: Md Haris Iqbal <haris.iqbal@ionos.com>
> >
> > sysfs_emit function was added to be aware of the PAGE_SIZE maximum of
> > the temporary buffer used for outputting sysfs content, so there is no
> > possible overruns. So replace the uses of any s*printf functions for
> > the sysfs show functions with sysfs_emit.
> >
> > Signed-off-by: Md Haris Iqbal <haris.iqbal@ionos.com>
> > Signed-off-by: Jack Wang <jinpu.wang@ionos.com>
> > ---
> >  drivers/infiniband/ulp/rtrs/rtrs-clt-stats.c | 24 +++++++++-----------
> >  drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c |  2 +-
> >  2 files changed, 12 insertions(+), 14 deletions(-)
> >
> > diff --git a/drivers/infiniband/ulp/rtrs/rtrs-clt-stats.c b/drivers/infiniband/ulp/rtrs/rtrs-clt-stats.c
> > index 26bbe5d6dff5..c5c047aa45a4 100644
> > --- a/drivers/infiniband/ulp/rtrs/rtrs-clt-stats.c
> > +++ b/drivers/infiniband/ulp/rtrs/rtrs-clt-stats.c
> > @@ -45,24 +45,23 @@ int rtrs_clt_stats_migration_cnt_to_str(struct rtrs_clt_stats *stats,
> >       size_t used;
> >       int cpu;
> >
> > -     used = scnprintf(buf, len, "    ");
> > +     used = sysfs_emit(buf, "    ");
> >       for_each_possible_cpu(cpu)
> > -             used += scnprintf(buf + used, len - used, " CPU%u", cpu);
> > +             used += sysfs_emit_at(buf, used, " CPU%u", cpu);
> >
> > -     used += scnprintf(buf + used, len - used, "\nfrom:");
> > +     used += sysfs_emit_at(buf, used, "\nfrom:");
>
> "\nfrom" and "\nto" are abuse of sysfs rules.
> https://lore.kernel.org/kernelnewbies/7a353c64-a81f-a149-9541-ef328a197761@gmail.com/T/#m8bf898fcdb4a5371781bbc9646993a50fa950fbc
> https://lore.kernel.org/kernelnewbies/7a353c64-a81f-a149-9541-ef328a197761@gmail.com/T/#m9ce6f045a863597922012d71a6719d6d90c8e0a6

I see. We will discuss internally and see what can be done about this.

Thanks

>
> Thanks
diff mbox series

Patch

diff --git a/drivers/infiniband/ulp/rtrs/rtrs-clt-stats.c b/drivers/infiniband/ulp/rtrs/rtrs-clt-stats.c
index 26bbe5d6dff5..c5c047aa45a4 100644
--- a/drivers/infiniband/ulp/rtrs/rtrs-clt-stats.c
+++ b/drivers/infiniband/ulp/rtrs/rtrs-clt-stats.c
@@ -45,24 +45,23 @@  int rtrs_clt_stats_migration_cnt_to_str(struct rtrs_clt_stats *stats,
 	size_t used;
 	int cpu;
 
-	used = scnprintf(buf, len, "    ");
+	used = sysfs_emit(buf, "    ");
 	for_each_possible_cpu(cpu)
-		used += scnprintf(buf + used, len - used, " CPU%u", cpu);
+		used += sysfs_emit_at(buf, used, " CPU%u", cpu);
 
-	used += scnprintf(buf + used, len - used, "\nfrom:");
+	used += sysfs_emit_at(buf, used, "\nfrom:");
 	for_each_possible_cpu(cpu) {
 		s = per_cpu_ptr(stats->pcpu_stats, cpu);
-		used += scnprintf(buf + used, len - used, " %d",
+		used += sysfs_emit_at(buf, used, " %d",
 				  atomic_read(&s->cpu_migr.from));
 	}
 
-	used += scnprintf(buf + used, len - used, "\nto  :");
+	used += sysfs_emit_at(buf, used, "\nto  :");
 	for_each_possible_cpu(cpu) {
 		s = per_cpu_ptr(stats->pcpu_stats, cpu);
-		used += scnprintf(buf + used, len - used, " %d",
-				  s->cpu_migr.to);
+		used += sysfs_emit_at(buf, used, " %d", s->cpu_migr.to);
 	}
-	used += scnprintf(buf + used, len - used, "\n");
+	used += sysfs_emit_at(buf, used, "\n");
 
 	return used;
 }
@@ -70,9 +69,8 @@  int rtrs_clt_stats_migration_cnt_to_str(struct rtrs_clt_stats *stats,
 int rtrs_clt_stats_reconnects_to_str(struct rtrs_clt_stats *stats, char *buf,
 				      size_t len)
 {
-	return scnprintf(buf, len, "%d %d\n",
-			 stats->reconnects.successful_cnt,
-			 stats->reconnects.fail_cnt);
+	return sysfs_emit(buf, "%d %d\n", stats->reconnects.successful_cnt,
+			  stats->reconnects.fail_cnt);
 }
 
 ssize_t rtrs_clt_stats_rdma_to_str(struct rtrs_clt_stats *stats,
@@ -94,7 +92,7 @@  ssize_t rtrs_clt_stats_rdma_to_str(struct rtrs_clt_stats *stats,
 		sum.failover_cnt	  += r->failover_cnt;
 	}
 
-	return scnprintf(page, len, "%llu %llu %llu %llu %u %llu\n",
+	return sysfs_emit(page, "%llu %llu %llu %llu %u %llu\n",
 			 sum.dir[READ].cnt, sum.dir[READ].size_total,
 			 sum.dir[WRITE].cnt, sum.dir[WRITE].size_total,
 			 atomic_read(&stats->inflight), sum.failover_cnt);
@@ -103,7 +101,7 @@  ssize_t rtrs_clt_stats_rdma_to_str(struct rtrs_clt_stats *stats,
 ssize_t rtrs_clt_reset_all_help(struct rtrs_clt_stats *s,
 				 char *page, size_t len)
 {
-	return scnprintf(page, len, "echo 1 to reset all statistics\n");
+	return sysfs_emit(page, "echo 1 to reset all statistics\n");
 }
 
 int rtrs_clt_reset_rdma_stats(struct rtrs_clt_stats *stats, bool enable)
diff --git a/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c b/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
index 20efd44297fb..9c43ce5ba1c1 100644
--- a/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
+++ b/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
@@ -102,7 +102,7 @@  static ssize_t rtrs_srv_src_addr_show(struct kobject *kobj,
 	sess = container_of(kobj, struct rtrs_srv_sess, kobj);
 	cnt = sockaddr_to_str((struct sockaddr *)&sess->s.dst_addr,
 			      page, PAGE_SIZE);
-	return cnt + scnprintf(page + cnt, PAGE_SIZE - cnt, "\n");
+	return cnt + sysfs_emit_at(page, cnt, "\n");
 }
 
 static struct kobj_attribute rtrs_srv_src_addr_attr =