block: sysfs cleanup for deprecated API usage
Subsystems (2)
cover block: sysfs cleanup for deprecated API usage
From: Jackie Liu <[email protected]>
A set of straightforward cleanups across the block layer sysfs interfaces:
Patches 1-7 convert remaining sprintf()/snprintf() calls in sysfs show functions to sysfs_emit()/sysfs_emit_at().
Patches 8-10 replace deprecated sscanf()/simple_strtoul() with kstrtol()/kstrtoint() in sysfs store functions.
Jackie Liu (10):
block: convert to sysfs_emit() in blk_ia_range show()
block: convert blk-mq hw sysfs show to sysfs_emit/sysfs_emit_at
block: convert disk events sysfs show to sysfs_emit/sysfs_emit_at
block: convert partition sysfs show to sysfs_emit()
block: convert part_timeout_show() to sysfs_emit()
block: convert kyber latency show to sysfs_emit()
block: convert bfq_var_show() to sysfs_emit()
block: use kstrtol() instead of sscanf() in
disk_events_poll_msecs_store()
block: replace simple_strtoul() with kstrtoint() in
part_timeout_store()
block: replace sscanf() with kstrtoint() in part_fail_store()
block/bfq-iosched.c | 2 +- block/blk-ia-ranges.c | 4 ++-- block/blk-mq-sysfs.c | 19 ++++++++++--------- block/blk-timeout.c | 18 ++++++++---------- block/disk-events.c | 12 ++++++------ block/genhd.c | 16 +++++++++------- block/kyber-iosched.c | 2 +- block/partitions/core.c | 10 +++++----- 8 files changed, 42 insertions(+), 41 deletions(-)
Signature · 1 line
2.51.1
PATCH 1/10 block: convert to sysfs_emit() in blk_ia_range show()
From: Jackie Liu <[email protected]>
Use sysfs_emit() instead of sprintf() per the sysfs output guidelines.
--- block/blk-ia-ranges.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
block/blk-ia-ranges.c +2 -2
@@ -15,14 +15,14 @@ static ssize_t blk_ia_range_sector_show(struct blk_independent_access_range *iar, char *buf) { - return sprintf(buf, "%llu\n", iar->sector); + return sysfs_emit(buf, "%llu\n", iar->sector); }static ssize_t blk_ia_range_nr_sectors_show (struct blk_independent_access_range *iar,char *buf) { -return sprintf (buf,"%llu\n" , iar->nr_sectors); +return sysfs_emit (buf,"%llu\n" , iar->nr_sectors); } struct blk_ia_range_sysfs_entry {
Signature · 1 line
2.51.1
PATCH 2/10 block: convert blk-mq hw sysfs show to sysfs_emit/sysfs_emit_at
In reply to Jackie LiuFrom: Jackie Liu <[email protected]>
Use sysfs_emit() for nr_tags and nr_reserved_tags, and convert cpus_show to use sysfs_emit_at() which also drops the manual PAGE_SIZE tracking.
--- block/blk-mq-sysfs.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
block/blk-mq-sysfs.c +10 -9
@@ -69,36 +69,37 @@ static ssize_t blk_mq_hw_sysfs_show(struct kobject *kobj,static ssize_t blk_mq_hw_sysfs_nr_tags_show (struct blk_mq_hw_ctx *hctx,char *page) { -return sprintf (page,"%u\n" , hctx->tags->nr_tags); +return sysfs_emit (page,"%u\n" , hctx->tags->nr_tags); }static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show (struct blk_mq_hw_ctx *hctx,char *page) { -return sprintf (page,"%u\n" , hctx->tags->nr_reserved_tags); +return sysfs_emit (page,"%u\n" , hctx->tags->nr_reserved_tags); }static ssize_t blk_mq_hw_sysfs_cpus_show (struct blk_mq_hw_ctx *hctx,char *page) { -const size_t size =PAGE_SIZE -1 ;unsigned int i, first =1 ; -int ret =0 , pos =0 ; +int ret, pos =0 ;for_each_cpu (i, hctx->cpumask) {if (first) - ret =snprintf (pos + page, size - pos,"%u" , i); + ret =sysfs_emit_at (page, pos,"%u" , i);else - ret =snprintf (pos + page, size - pos,", %u" , i); + ret =sysfs_emit_at (page, pos,", %u" , i); -if (ret >= size - pos) +if (ret <=0 )break ; first =0 ; pos += ret; } - ret =snprintf (pos + page, size +1 - pos,"\n" ); -return pos + ret; + ret =sysfs_emit_at (page, pos,"\n" ); +if (ret >0 ) + pos += ret; +return pos; }static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_tags = {
Signature · 1 line
2.51.1
PATCH 3/10 block: convert disk events sysfs show to sysfs_emit/sysfs_emit_at
In reply to Jackie LiuFrom: Jackie Liu <[email protected]>
Use sysfs_emit()/sysfs_emit_at() instead of sprintf() for the disk events sysfs attributes.
--- block/disk-events.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
block/disk-events.c +5 -5
@@ -329,12 +329,12 @@ static ssize_t __disk_events_show(unsigned int events, char *buf)for (i =0 ; i <ARRAY_SIZE (disk_events_strs); i++)if (events & (1 << i)) { - pos +=sprintf (buf + pos,"%s%s" , - delim, disk_events_strs[i]); + pos +=sysfs_emit_at (buf, pos,"%s%s" , + delim, disk_events_strs[i]); delim =" " ; }if (pos) - pos +=sprintf (buf + pos,"\n" ); + pos +=sysfs_emit_at (buf, pos,"\n" );return pos; }
@@ -361,8 +361,8 @@ static ssize_t disk_events_poll_msecs_show(struct device *dev,struct gendisk *disk =dev_to_disk (dev);if (!disk->ev) -return sprintf (buf,"-1\n" ); -return sprintf (buf,"%ld\n" , disk->ev->poll_msecs); +return sysfs_emit (buf,"-1\n" ); +return sysfs_emit (buf,"%ld\n" , disk->ev->poll_msecs); } static ssize_t disk_events_poll_msecs_store(struct device *dev,
Signature · 1 line
2.51.1
PATCH 4/10 block: convert partition sysfs show to sysfs_emit()
In reply to Jackie LiuFrom: Jackie Liu <[email protected]>
Use sysfs_emit() instead of sprintf() for partition attribute show functions.
--- block/partitions/core.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
block/partitions/core.c +5 -5
@@ -177,31 +177,31 @@ static struct parsed_partitions *check_partition(struct gendisk *hd)static ssize_t part_partition_show (struct device *dev,struct device_attribute *attr,char *buf) { -return sprintf (buf,"%d\n" ,bdev_partno (dev_to_bdev (dev))); +return sysfs_emit (buf,"%d\n" ,bdev_partno (dev_to_bdev (dev))); }static ssize_t part_start_show (struct device *dev,struct device_attribute *attr,char *buf) { -return sprintf (buf,"%llu\n" ,dev_to_bdev (dev)->bd_start_sect); +return sysfs_emit (buf,"%llu\n" ,dev_to_bdev (dev)->bd_start_sect); }static ssize_t part_ro_show (struct device *dev,struct device_attribute *attr,char *buf) { -return sprintf (buf,"%d\n" ,bdev_read_only (dev_to_bdev (dev))); +return sysfs_emit (buf,"%d\n" ,bdev_read_only (dev_to_bdev (dev))); }static ssize_t part_alignment_offset_show (struct device *dev,struct device_attribute *attr,char *buf) { -return sprintf (buf,"%u\n" ,bdev_alignment_offset (dev_to_bdev (dev))); +return sysfs_emit (buf,"%u\n" ,bdev_alignment_offset (dev_to_bdev (dev))); }static ssize_t part_discard_alignment_show (struct device *dev,struct device_attribute *attr,char *buf) { -return sprintf (buf,"%u\n" ,bdev_discard_alignment (dev_to_bdev (dev))); +return sysfs_emit (buf,"%u\n" ,bdev_discard_alignment (dev_to_bdev (dev))); } static DEVICE_ATTR(partition, 0444, part_partition_show, NULL);
Signature · 1 line
2.51.1
PATCH 5/10 block: convert part_timeout_show() to sysfs_emit()
In reply to Jackie LiuFrom: Jackie Liu <[email protected]>
Use sysfs_emit() instead of sprintf() for the fail_io timeout sysfs show function.
--- block/blk-timeout.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/blk-timeout.c +1 -1
@@ -42,7 +42,7 @@ ssize_t part_timeout_show(struct device *dev, struct device_attribute *attr,struct gendisk *disk =dev_to_disk (dev);int set =test_bit (QUEUE_FLAG_FAIL_IO , &disk->queue->queue_flags); -return sprintf (buf,"%d\n" , set !=0 ); +return sysfs_emit (buf,"%d\n" , set !=0 ); } ssize_t part_timeout_store(struct device *dev, struct device_attribute *attr,
Signature · 1 line
2.51.1
PATCH 6/10 block: convert kyber latency show to sysfs_emit()
In reply to Jackie LiuFrom: Jackie Liu <[email protected]>
Use sysfs_emit() instead of sprintf() in the KYBER_LAT_SHOW_STORE macro for the kyber I/O scheduler latency target show functions.
--- block/kyber-iosched.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/kyber-iosched.c +1 -1
@@ -852,7 +852,7 @@ static ssize_t kyber_##name##_lat_show(struct elevator_queue *e, \ { \struct kyber_queue_data *kqd = e->elevator_data; \ \ -return sprintf (page,"%llu\n" , kqd->latency_targets[domain]); \ +return sysfs_emit (page,"%llu\n" , kqd->latency_targets[domain]); \ } \ \ static ssize_t kyber_##name##_lat_store(struct elevator_queue *e, \
Signature · 1 line
2.51.1
PATCH 7/10 block: convert bfq_var_show() to sysfs_emit()
In reply to Jackie LiuFrom: Jackie Liu <[email protected]>
Use sysfs_emit() instead of sprintf() in bfq_var_show(), which is the common helper used by all BFQ sysfs show functions.
--- block/bfq-iosched.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/bfq-iosched.c +1 -1
@@ -7379,7 +7379,7 @@ static int __init bfq_slab_setup(void)static ssize_t bfq_var_show (unsigned int var,char *page) { -return sprintf (page,"%u\n" , var); +return sysfs_emit (page,"%u\n" , var); }static int bfq_var_store (unsigned long *var,const char *page)
Signature · 1 line
2.51.1
PATCH 8/10 block: use kstrtol() instead of sscanf() in disk_events_poll_msecs_store()
In reply to Jackie LiuFrom: Jackie Liu <[email protected]>
kstrtol() is the preferred way to parse a single integer from sysfs input. It also makes the !count check unnecessary since kstrtol() returns -EINVAL on empty or malformed strings.
--- block/disk-events.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/disk-events.c +1 -1
@@ -372,7 +372,7 @@ static ssize_t disk_events_poll_msecs_store(struct device *dev,struct gendisk *disk =dev_to_disk (dev);long intv; -if (!count || !sscanf (buf,"%ld" , &intv)) +if (kstrtol (buf,10 , &intv))return -EINVAL ;if (intv <0 && intv !=-1 )
Signature · 1 line
2.51.1
PATCH 9/10 block: replace simple_strtoul() with kstrtoint() in part_timeout_store()
In reply to Jackie LiuFrom: Jackie Liu <[email protected]>
simple_strtoul() is deprecated. Switch to kstrtoint() which also removes the need for the const-discarding cast and the if (count) wrapper.
--- block/blk-timeout.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)
block/blk-timeout.c +7 -9
@@ -49,18 +49,16 @@ ssize_t part_timeout_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) {struct gendisk *disk =dev_to_disk (dev); +struct request_queue *q = disk->queue;int val; -if (count) { -struct request_queue *q = disk->queue; -char *p = (char *) buf; +if (kstrtoint (buf,10 , &val)) +return -EINVAL ; - val =simple_strtoul (p, &p,10 ); -if (val) -blk_queue_flag_set (QUEUE_FLAG_FAIL_IO , q); -else -blk_queue_flag_clear (QUEUE_FLAG_FAIL_IO , q); - } +if (val) +blk_queue_flag_set (QUEUE_FLAG_FAIL_IO , q); +else +blk_queue_flag_clear (QUEUE_FLAG_FAIL_IO , q);return count; }
Signature · 1 line
2.51.1
PATCH 10/10 block: replace sscanf() with kstrtoint() in part_fail_store()
In reply to Jackie LiuFrom: Jackie Liu <[email protected]>
sscanf() is overly lenient for parsing a single integer from sysfs. Use kstrtoint() which also properly rejects malformed input and makes the count > 0 guard unnecessary.
--- block/genhd.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
block/genhd.c +9 -7
@@ -1186,14 +1186,16 @@ ssize_t part_fail_store(struct device *dev,struct device_attribute *attr, const char *buf, size_t count) { - int i; + int val; + +if (kstrtoint (buf,10 , &val)) +return -EINVAL ; + +if (val) +bdev_set_flag (dev_to_bdev (dev),BD_MAKE_IT_FAIL ); +else +bdev_clear_flag (dev_to_bdev (dev),BD_MAKE_IT_FAIL ); -if (count >0 &&sscanf (buf,"%d" , &i) >0 ) { -if (i) -bdev_set_flag (dev_to_bdev (dev),BD_MAKE_IT_FAIL ); -else -bdev_clear_flag (dev_to_bdev (dev),BD_MAKE_IT_FAIL ); - }return count; }
Signature · 1 line
2.51.1