Skip to contentSkip to conversation

block: sysfs cleanup for deprecated API usage

PATCH New Jackie Liu 1 participant 11 messages Apr 1

Subsystems (2)
lore
Share & tools
lore

Jackie Liu

author
#0 lore raw

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
author
#1 lore raw

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
index d479f5481b66..94f4795da7c9 100644
@@ -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
author
#2 lore raw

PATCH 2/10 block: convert blk-mq hw sysfs show to sysfs_emit/sysfs_emit_at

In reply to Jackie Liu

From: 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
index 58ec293373c6..ed9546de7466 100644
@@ -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
author
#3 lore raw

PATCH 3/10 block: convert disk events sysfs show to sysfs_emit/sysfs_emit_at

In reply to Jackie Liu

From: 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
index 9f9f9f8a2d6b..b1680d6a1027 100644
@@ -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
author
#4 lore raw

PATCH 4/10 block: convert partition sysfs show to sysfs_emit()

In reply to Jackie Liu

From: 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
index 740228750aaf..41b5c09d4551 100644
@@ -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
author
#5 lore raw

PATCH 5/10 block: convert part_timeout_show() to sysfs_emit()

In reply to Jackie Liu

From: 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
index 1b8de0417fc1..eab6422fad4b 100644
@@ -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
author
#6 lore raw

PATCH 6/10 block: convert kyber latency show to sysfs_emit()

In reply to Jackie Liu

From: 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
index b84163d1f851..4c8070563042 100644
@@ -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
author
#7 lore raw

PATCH 7/10 block: convert bfq_var_show() to sysfs_emit()

In reply to Jackie Liu

From: 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
index 141c602d5e85..4123f42faf39 100644
@@ -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
author
#8 lore raw

PATCH 8/10 block: use kstrtol() instead of sscanf() in disk_events_poll_msecs_store()

In reply to Jackie Liu

From: 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
index b1680d6a1027..b6233b0d5575 100644
@@ -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
author
#9 lore raw

PATCH 9/10 block: replace simple_strtoul() with kstrtoint() in part_timeout_store()

In reply to Jackie Liu

From: 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
index eab6422fad4b..ea3c5451d6cf 100644
@@ -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
author
#10 lore raw

PATCH 10/10 block: replace sscanf() with kstrtoint() in part_fail_store()

In reply to Jackie Liu

From: 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
index 7d6854fd28e9..369bd8c13bbf 100644
@@ -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