Skip to contentSkip to conversation

bcache: replace snprintf in show functions with sysfs_emit

PATCH In review Qing Wang 2 participants 4 messages Oct 13, 2021 1 message not in the archive bcache (block layer cache)

lore
Share & tools
lore

Qing Wang

author
#0 lore raw

PATCH 1/1 bcache: replace snprintf in show functions with sysfs_emit

coccicheck complains about the use of snprintf() in sysfs show functions.

Fix the coccicheck warning:
WARNING: use scnprintf or sprintf.

Use sysfs_emit instead of scnprintf or sprintf makes more sense.

---
 drivers/md/bcache/sysfs.c | 2 +-
 drivers/md/bcache/sysfs.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/md/bcache/sysfs.c +1 -1
index 05ac1d6..e146c9e 100644
@@ -639,7 +639,7 @@ static int bch_bset_print_stats(struct cache_set *c, char *buf)
 	if (ret < 0)
 		return ret;
 
-	return snprintf(buf, PAGE_SIZE,
+	return sysfs_emit(buf,
 			"btree nodes:		%zu\n"
 			"written sets:		%zu\n"
 			"unwritten sets:		%zu\n"
drivers/md/bcache/sysfs.h +1 -1
index 215df32..8bec32d 100644
@@ -51,7 +51,7 @@ STORE(fn)								\
 #define sysfs_printf(file, fmt, ...)					\
 do {									\
 	if (attr == &sysfs_ ## file)					\
-		return snprintf(buf, PAGE_SIZE, fmt "\n", __VA_ARGS__);	\
+		return sysfs_emit(buf, fmt "\n", __VA_ARGS__);	\
 } while (0)
 
 #define sysfs_print(file, var)						\
Signature · 1 line
2.7.4
#1 lore raw
Quoted 18 lines · Qing Wang

coccicheck complains about the use of snprintf() in sysfs show functions.

Fix the coccicheck warning:
WARNING: use scnprintf or sprintf.

Use sysfs_emit instead of scnprintf or sprintf makes more sense.

---
  drivers/md/bcache/sysfs.c | 2 +-
  drivers/md/bcache/sysfs.h | 2 +-
  2 files changed, 2 insertions(+), 2 deletions(-)
drivers/md/bcache/sysfs.c +0 -0
index 05ac1d6..e146c9e 100644

drivers/md/bcache/sysfs.c static int bch_bset_print_stats(struct cache_set *c, char *buf) · line 645

Quoted 8 lines
drivers/md/bcache/sysfs.c +1 -1
@@ -639,7 +639,7 @@ static int bch_bset_print_stats(struct cache_set *c, char *buf)
  	if (ret < 0)
  		return ret;
 
-	return snprintf(buf, PAGE_SIZE,
+	return sysfs_emit(buf,
  			"btree nodes:		%zu\n"
  			"written sets:		%zu\n"
  			"unwritten sets:		%zu\n"

Let's keep this location as snprintf(), to make code style be consistent.

drivers/md/bcache/sysfs.h STORE(fn) \ · line 57

Quoted 13 lines
drivers/md/bcache/sysfs.h +1 -1
index 215df32..8bec32d 100644
@@ -51,7 +51,7 @@ STORE(fn)								\
  #define sysfs_printf(file, fmt, ...)					\
  do {									\
  	if (attr == &sysfs_ ## file)					\
-		return snprintf(buf, PAGE_SIZE, fmt "\n", __VA_ARGS__);	\
+		return sysfs_emit(buf, fmt "\n", __VA_ARGS__);	\
  } while (0)
 
  #define sysfs_print(file, var)						\

IMHO I like this change :-) BTW, for snprint() from util.h, it seems only to be used by sysfs_print() in syfs.h, would you like to change it to use sysfs_emit too ?

Thanks for the patch.

Coly Li

A message that is not in the archive (sent off-list or not yet received): AI*[email protected]

author
#3 lore raw
Quoted 20 lines
Quoted 13 lines
drivers/md/bcache/sysfs.h +1 -1
index 215df32..8bec32d 100644
@@ -51,7 +51,7 @@ STORE(fn)                                                           \
    #define sysfs_printf(file, fmt, ...)                                        \
    do {                                                                        \
         if (attr == &sysfs_ ## file)                                    \
-             return snprintf(buf, PAGE_SIZE, fmt "\n", __VA_ARGS__); \
+             return sysfs_emit(buf, fmt "\n", __VA_ARGS__);  \
    } while (0)
Quoted 2 lines


   #define sysfs_print(file, var)                                              \

IMHO I like this change :-) BTW, for snprint() from util.h, it seems only to be used by sysfs_print() in syfs.h, would you like to change it to use sysfs_emit too ?

Quoted 4 lines

Thanks for the patch.

Coly Li

sysfs_print() should also be modified if size is a fixed value PAGE_SIZE, but here is param, it would be appreciate if you have better method.

Thanks,

Qing

#4 lore raw
Quoted 17 lines · 王擎
Quoted 17 lines

drivers/md/bcache/sysfs.h STORE(fn)                                                           \ · line 57

Quoted 13 lines
drivers/md/bcache/sysfs.h +1 -1
index 215df32..8bec32d 100644
@@ -51,7 +51,7 @@ STORE(fn)                                                           \
    #define sysfs_printf(file, fmt, ...)                                        \
    do {                                                                        \
         if (attr == &sysfs_ ## file)                                    \
-             return snprintf(buf, PAGE_SIZE, fmt "\n", __VA_ARGS__); \
+             return sysfs_emit(buf, fmt "\n", __VA_ARGS__);  \
    } while (0)
 
    #define sysfs_print(file, var)                                              \

IMHO I like this change :-) BTW, for snprint() from util.h, it seems only to be used by sysfs_print() in syfs.h, would you like to change it to use sysfs_emit too ?

Quoted 6 lines
Quoted 4 lines

Thanks for the patch.

Coly Li

sysfs_print() should also be modified if size is a fixed value PAGE_SIZE, but here is param, it would be appreciate if you have better method.

Yes, I meant you may implement sysfs_print() by sysfs_emit() directly in the way as snprint() does. Then you may remove snprint() since no one uses it any more.

Coly Li