block: zram: replace scnprintf() with sysfs_emit() in *_show() functions
Subsystems (2)
PATCH 1/1 block: zram: replace scnprintf() with sysfs_emit() in *_show() functions
Replace scnprintf() with sysfs_emit() or sysfs_emit_at() in sysfs *_show() functions in zram_drv.c to follow the kernel's guidelines from Documentation/filesystems/sysfs.rst.
This improves consistency, safety, and makes the code easier to maintain and update in the future.
--- drivers/block/zram/zram_drv.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
drivers/block/zram/zram_drv.c +11 -11
@@ -373,7 +373,7 @@ static ssize_t initstate_show(struct device *dev, val =init_done (zram);up_read (&zram->init_lock); -return scnprintf (buf,PAGE_SIZE ,"%u\n" , val); +return sysfs_emit (buf,"%u\n" , val); } static ssize_t disksize_show(struct device *dev,
@@ -381,7 +381,7 @@ static ssize_t disksize_show(struct device *dev, {struct zram *zram =dev_to_zram (dev); -return scnprintf (buf,PAGE_SIZE ,"%llu\n" , zram->disksize); +return sysfs_emit (buf,"%llu\n" , zram->disksize); } static ssize_t mem_limit_store(struct device *dev,
@@ -532,7 +532,7 @@ static ssize_t writeback_limit_enable_show(struct device *dev,spin_unlock (&zram->wb_limit_lock);up_read (&zram->init_lock); -return scnprintf (buf,PAGE_SIZE ,"%d\n" , val); +return sysfs_emit (buf,"%d\n" , val); } static ssize_t writeback_limit_store(struct device *dev,
@@ -567,7 +567,7 @@ static ssize_t writeback_limit_show(struct device *dev,spin_unlock (&zram->wb_limit_lock);up_read (&zram->init_lock); -return scnprintf (buf,PAGE_SIZE ,"%llu\n" , val); +return sysfs_emit (buf,"%llu\n" , val); }static void reset_bdev (struct zram *zram)
@@ -1292,7 +1292,7 @@ static ssize_t recomp_algorithm_show(struct device *dev,if (!zram->comp_algs[prio])continue ; - sz +=scnprintf (buf + sz,PAGE_SIZE - sz -2 ,"#%d: " , prio); + sz +=sysfs_emit_at (buf, sz,"#%d: " , prio); sz +=__comp_algorithm_show (zram, prio, buf + sz); }
@@ -1365,7 +1365,7 @@ static ssize_t io_stat_show(struct device *dev,ssize_t ret;down_read (&zram->init_lock); - ret = scnprintf(buf, PAGE_SIZE, + ret = sysfs_emit(buf, "%8llu %8llu 0 %8llu\n", (u64)atomic64_read(&zram->stats.failed_reads), (u64)atomic64_read(&zram->stats.failed_writes),
@@ -1395,7 +1395,7 @@ static ssize_t mm_stat_show(struct device *dev, orig_size =atomic64_read (&zram->stats.pages_stored); max_used =atomic_long_read (&zram->stats.max_used_pages); - ret = scnprintf(buf, PAGE_SIZE, + ret = sysfs_emit(buf, "%8llu %8llu %8llu %8lu %8ld %8llu %8lu %8llu %8llu\n", orig_size << PAGE_SHIFT, (u64)atomic64_read(&zram->stats.compr_data_size),
@@ -1420,8 +1420,8 @@ static ssize_t bd_stat_show(struct device *dev,ssize_t ret;down_read (&zram->init_lock); - ret =scnprintf (buf,PAGE_SIZE , -"%8llu %8llu %8llu\n" , + ret =sysfs_emit (buf, +"%8llu %8llu %8llu\n" ,FOUR_K ((u64 )atomic64_read (&zram->stats.bd_count)),FOUR_K ((u64 )atomic64_read (&zram->stats.bd_reads)),FOUR_K ((u64 )atomic64_read (&zram->stats.bd_writes)));
@@ -1439,7 +1439,7 @@ static ssize_t debug_stat_show(struct device *dev,ssize_t ret;down_read (&zram->init_lock); - ret =scnprintf (buf,PAGE_SIZE , + ret =sysfs_emit (buf,"version: %d\n0 %8llu\n" , version, (u64 )atomic64_read (&zram->stats.miss_free));
@@ -2682,7 +2682,7 @@ static ssize_t hot_add_show(const struct class *class,if (ret <0 )return ret; -return scnprintf (buf,PAGE_SIZE ,"%d\n" , ret); +return sysfs_emit (buf,"%d\n" , ret); }/* This attribute must be set to 0400, so CLASS_ATTR_RO() can not be used */ static struct class_attribute class_attr_hot_add =
Signature · 1 line
2.43.0
Quoted 8 lines · Rahul Kumar
Replace
scnprintf()withsysfs_emit()orsysfs_emit_at()in sysfs*_show()functions inzram_drv.cto follow the kernel's guidelines fromDocumentation/filesystems/sysfs.rst.This improves consistency, safety, and makes the code easier to maintain and update in the future.
Signed-off-by Rahul Kumar <[email protected]>
Sergey Senozhatsky
maintainerOn (25/06/27 09:22), Rahul Kumar wrote:
[..]
drivers/block/zram/zram_drv.c static ssize_t recomp_algorithm_show(struct device *dev, · line 1296
Quoted 8 lines
static void
reset_bdev(struct zram *zram)drivers/block/zram/zram_drv.c +1
-1@@ -1292,7 +1292,7 @@ static ssize_t recomp_algorithm_show(struct device *dev,if (!zram->comp_algs[prio])continue ; - sz +=scnprintf (buf + sz,PAGE_SIZE - sz -2 ,"#%d: " , prio); + sz +=sysfs_emit_at (buf, sz,"#%d: " , prio); sz +=__comp_algorithm_show (zram, prio, buf + sz);
Actually, there is a tiny bug in zcomp. It's unrelated to this patch, but I'll send a fix as followup, to avoid any conflicts.
Sergey Senozhatsky
maintainerPATCH 1/1 zram: pass buffer offset to zcomp_available_show()
In reply to Rahul KumarIn most cases zcomp_available_show() is the only emitting function that is called from sysfs read() handler, so it assumes that there is a whole PAGE_SIZE buffer to work with. There is an exception, however: recomp_algorithm_show().
In recomp_algorithm_show() we prepend the buffer with priority number before we pass it to zcomp_available_show(), so it cannot assume PAGE_SIZE anymore and must take recomp_algorithm_show() modifications into consideration. Therefore we need to pass buffer offset to zcomp_available_show().
Also convert it to use sysfs_emit_at(), to stay aligned with the rest of zram's sysfs read() handlers.
On practice we are never even close to using the whole PAGE_SIZE buffer, so that's not a critical bug, but still.
--- drivers/block/zram/zcomp.c | 15 +++++++-------- drivers/block/zram/zcomp.h | 2 +- drivers/block/zram/zram_drv.c | 9 +++++---- 3 files changed, 13 insertions(+), 13 deletions(-)
drivers/block/zram/zcomp.c +7 -8
@@ -8,6 +8,7 @@#include <linux/sched.h> #include <linux/cpuhotplug.h> #include <linux/vmalloc.h> +#include <linux/sysfs.h> #include "zcomp.h"
@@ -89,23 +90,21 @@ bool zcomp_available_algorithm(const char *comp) }/* show available compressors */ -ssize_t zcomp_available_show (const char *comp,char *buf) +ssize_t zcomp_available_show (const char *comp,char *buf,ssize_t at) { -ssize_t sz =0 ;int i;for (i =0 ; i <ARRAY_SIZE (backends) -1 ; i++) {if (!strcmp (comp, backends[i]->name)) { - sz +=scnprintf (buf + sz,PAGE_SIZE - sz -2 , -"[%s] " , backends[i]->name); + at +=sysfs_emit_at (buf, at,"[%s] " , + backends[i]->name); }else { - sz +=scnprintf (buf + sz,PAGE_SIZE - sz -2 , -"%s " , backends[i]->name); + at +=sysfs_emit_at (buf, at,"%s " , backends[i]->name); } } - sz +=scnprintf (buf + sz,PAGE_SIZE - sz,"\n" ); -return sz; + at +=sysfs_emit_at (buf, at,"\n" ); +return at; }struct zcomp_strm *zcomp_stream_get (struct zcomp *comp)
drivers/block/zram/zcomp.h +1 -1
@@ -79,7 +79,7 @@ struct zcomp {int zcomp_cpu_up_prepare (unsigned int cpu,struct hlist_node *node);int zcomp_cpu_dead (unsigned int cpu,struct hlist_node *node); -ssize_t zcomp_available_show (const char *comp,char *buf); +ssize_t zcomp_available_show (const char *comp,char *buf,ssize_t at);bool zcomp_available_algorithm (const char *comp);struct zcomp *zcomp_create (const char *alg,struct zcomp_params *params);
drivers/block/zram/zram_drv.c +5 -4
@@ -1225,12 +1225,13 @@ static void comp_algorithm_set(struct zram *zram, u32 prio, const char *alg) zram->comp_algs[prio] = alg; } -static ssize_t __comp_algorithm_show (struct zram *zram,u32 prio,char *buf) +static ssize_t __comp_algorithm_show (struct zram *zram,u32 prio, +char *buf,ssize_t at) {ssize_t sz;down_read (&zram->init_lock); - sz =zcomp_available_show (zram->comp_algs[prio], buf); + sz =zcomp_available_show (zram->comp_algs[prio], buf, at);up_read (&zram->init_lock);return sz;
@@ -1387,7 +1388,7 @@ static ssize_t comp_algorithm_show(struct device *dev, {struct zram *zram =dev_to_zram (dev); -return __comp_algorithm_show (zram,ZRAM_PRIMARY_COMP , buf); +return __comp_algorithm_show (zram,ZRAM_PRIMARY_COMP , buf,0 ); } static ssize_t comp_algorithm_store(struct device *dev,
@@ -1416,7 +1417,7 @@ static ssize_t recomp_algorithm_show(struct device *dev,continue ; sz +=sysfs_emit_at (buf, sz,"#%d: " , prio); - sz +=__comp_algorithm_show (zram, prio, buf + sz); + sz +=__comp_algorithm_show (zram, prio, buf, sz); }return sz;
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
Sergey Senozhatsky
maintainerRe: [PATCH] zram: pass buffer offset to zcomp_available_show()
Quoted 11 lines · Sergey Senozhatsky
In most cases
zcomp_available_show()is the only emitting function that is called from sysfsread()handler, so it assumes that there is a wholePAGE_SIZEbuffer to work with. There is an exception, however:recomp_algorithm_show().In
recomp_algorithm_show()we prepend the buffer with priority number before we pass it tozcomp_available_show(), so it cannot assumePAGE_SIZEanymore and must takerecomp_algorithm_show()modifications into consideration. Therefore we need to pass buffer offset tozcomp_available_show().
Quoted 5 lines
Also convert it to use
sysfs_emit_at(), to stay aligned with the rest of zram's sysfsread()handlers.On practice we are never even close to using the whole
PAGE_SIZEbuffer, so that's not a critical bug, but still.
Jens, do you feel like picking these 2 up or should I route them to Andrew's tree?
Quoted 8 lines · Rahul Kumar
Replace
scnprintf()withsysfs_emit()orsysfs_emit_at()in sysfs*_show()functions inzram_drv.cto follow the kernel's guidelines fromDocumentation/filesystems/sysfs.rst.This improves consistency, safety, and makes the code easier to maintain and update in the future.
[...]
Applied, thanks!
[1/1] block: zram: replace scnprintf() with sysfs_emit() in *_show() functions
commit: 264a3fdab2365395e43d3a0b40162a29e61ffa22
[1/1] zram: pass buffer offset to zcomp_available_show()
commit: e74a1c6a8e8af2422fce125c29b14f1d3fab5b5cBest regards,
Signature · 1 line
Jens Axboe