Skip to contentSkip to conversation

blk-mq: avoid sysfs buffer overflow by too many CPU cores

PATCH In review Ming Lei 5 participants 6 messages Aug 16, 2019 – Aug 19, 2019

Subsystems (2)
lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH v2 1/1 blk-mq: avoid sysfs buffer overflow by too many CPU cores

It is reported that sysfs buffer overflow can be triggered in case of too many CPU cores(>841 on 4K PAGE_SIZE) when showing CPUs in blk_mq_hw_sysfs_cpus_show().

So use cpumap_print_to_pagebuf() to print the info and fix the potential buffer overflow issue.

Cc Mark Ray <[email protected]>
Cc Greg KH <[email protected]>
Fixes 676141e48af7("blk-mq: don't dump CPU -> hw queue map on driver load")
---
 block/blk-mq-sysfs.c | 15 +--------------
 1 file changed, 1 insertion(+), 14 deletions(-)
block/blk-mq-sysfs.c +1 -14
index d6e1a9bd7131..4d0d32377ba3 100644
@@ -166,20 +166,7 @@ static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx,
 
 static ssize_t blk_mq_hw_sysfs_cpus_show(struct blk_mq_hw_ctx *hctx, char *page)
 {
-	unsigned int i, first = 1;
-	ssize_t ret = 0;
-
-	for_each_cpu(i, hctx->cpumask) {
-		if (first)
-			ret += sprintf(ret + page, "%u", i);
-		else
-			ret += sprintf(ret + page, ", %u", i);
-
-		first = 0;
-	}
-
-	ret += sprintf(ret + page, "\n");
-	return ret;
+	return cpumap_print_to_pagebuf(true, page, hctx->cpumask);
 }
 
 static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_tags = {
Signature · 1 line
2.20.1
#1 lore raw
Quoted 32 lines · Ming Lei

It is reported that sysfs buffer overflow can be triggered in case of too many CPU cores(>841 on 4K PAGE_SIZE) when showing CPUs in blk_mq_hw_sysfs_cpus_show().

So use cpumap_print_to_pagebuf() to print the info and fix the potential buffer overflow issue.

Cc Mark Ray <[email protected]>
Cc Greg KH <[email protected]>
Fixes 676141e48af7("blk-mq: don't dump CPU -> hw queue map on driver load")
---
  block/blk-mq-sysfs.c | 15 +--------------
  1 file changed, 1 insertion(+), 14 deletions(-)
block/blk-mq-sysfs.c +0 -8
index d6e1a9bd7131..4d0d32377ba3 100644
@@ -166,20 +166,7 @@ static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx,
 
  static ssize_t blk_mq_hw_sysfs_cpus_show(struct blk_mq_hw_ctx *hctx, char *page)
  {
-	unsigned int i, first = 1;
-	ssize_t ret = 0;
-
-	for_each_cpu(i, hctx->cpumask) {
-		if (first)
-			ret += sprintf(ret + page, "%u", i);
-		else
-			ret += sprintf(ret + page, ", %u", i);

block/blk-mq-sysfs.c static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx, · line 172

Quoted 10 lines
block/blk-mq-sysfs.c +1 -6
-
-		first = 0;
-	}
-
-	ret += sprintf(ret + page, "\n");
-	return ret;
+	return cpumap_print_to_pagebuf(true, page, hctx->cpumask);
  }
 
  static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_tags = {

Although this patch looks fine to me, shouldn't this attribute be documented under Documentation/ABI/?

Thanks,

Bart.

#2 lore raw
Quoted 43 lines · Bart Van Assche
Quoted 41 lines · Ming Lei

It is reported that sysfs buffer overflow can be triggered in case of too many CPU cores(>841 on 4K PAGE_SIZE) when showing CPUs in blk_mq_hw_sysfs_cpus_show().

So use cpumap_print_to_pagebuf() to print the info and fix the potential buffer overflow issue.

Cc Mark Ray <[email protected]>
Cc Greg KH <[email protected]>
Fixes 676141e48af7("blk-mq: don't dump CPU -> hw queue map on driver load")
---
  block/blk-mq-sysfs.c | 15 +--------------
  1 file changed, 1 insertion(+), 14 deletions(-)
block/blk-mq-sysfs.c +1 -14
index d6e1a9bd7131..4d0d32377ba3 100644
@@ -166,20 +166,7 @@ static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx,
 
  static ssize_t blk_mq_hw_sysfs_cpus_show(struct blk_mq_hw_ctx *hctx, char *page)
  {
-     unsigned int i, first = 1;
-     ssize_t ret = 0;
-
-     for_each_cpu(i, hctx->cpumask) {
-             if (first)
-                     ret += sprintf(ret + page, "%u", i);
-             else
-                     ret += sprintf(ret + page, ", %u", i);
-
-             first = 0;
-     }
-
-     ret += sprintf(ret + page, "\n");
-     return ret;
+     return cpumap_print_to_pagebuf(true, page, hctx->cpumask);
  }
 
Quoted 4 lines

block/blk-mq-sysfs.c static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx, · line 172

Quoted 1 line
block/blk-mq-sysfs.c +0 -0
  static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_tags = {

Although this patch looks fine to me, shouldn't this attribute be documented under Documentation/ABI/?

That is another problem, not closely related with this buffer-overflow issue.

I suggest to fix the buffer overflow first, which is triggered from userspace.

Thanks,
Ming Lei

maintainer
#3 lore raw
Quoted 44 lines · Ming Lei
Quoted 43 lines · Bart Van Assche
Quoted 41 lines · Ming Lei

It is reported that sysfs buffer overflow can be triggered in case of too many CPU cores(>841 on 4K PAGE_SIZE) when showing CPUs in blk_mq_hw_sysfs_cpus_show().

So use cpumap_print_to_pagebuf() to print the info and fix the potential buffer overflow issue.

Cc Mark Ray <[email protected]>
Cc Greg KH <[email protected]>
Fixes 676141e48af7("blk-mq: don't dump CPU -> hw queue map on driver load")
---
  block/blk-mq-sysfs.c | 15 +--------------
  1 file changed, 1 insertion(+), 14 deletions(-)
block/blk-mq-sysfs.c +1 -14
index d6e1a9bd7131..4d0d32377ba3 100644
@@ -166,20 +166,7 @@ static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx,
 
  static ssize_t blk_mq_hw_sysfs_cpus_show(struct blk_mq_hw_ctx *hctx, char *page)
  {
-     unsigned int i, first = 1;
-     ssize_t ret = 0;
-
-     for_each_cpu(i, hctx->cpumask) {
-             if (first)
-                     ret += sprintf(ret + page, "%u", i);
-             else
-                     ret += sprintf(ret + page, ", %u", i);
-
-             first = 0;
-     }
-
-     ret += sprintf(ret + page, "\n");
-     return ret;
+     return cpumap_print_to_pagebuf(true, page, hctx->cpumask);
  }
 
Quoted 8 lines
Quoted 4 lines

block/blk-mq-sysfs.c static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx, · line 172

Quoted 1 line
block/blk-mq-sysfs.c +0 -0
  static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_tags = {

Although this patch looks fine to me, shouldn't this attribute be documented under Documentation/ABI/?

That is another problem, not closely related with this buffer-overflow issue.

I suggest to fix the buffer overflow first, which is triggered from userspace.

I suggest you just delete this whole sysfs attribute, which will solve the buffer overflow, as no one should be using it and it is incorrect to have.

thanks,

greg k-h

maintainer
#4 lore raw
In reply to Ming Lei
Quoted 7 lines · Ming Lei

It is reported that sysfs buffer overflow can be triggered in case of too many CPU cores(>841 on 4K PAGE_SIZE) when showing CPUs in blk_mq_hw_sysfs_cpus_show().

So use cpumap_print_to_pagebuf() to print the info and fix the potential buffer overflow issue.

Quoted 8 lines
Cc Mark Ray <[email protected]>
Cc Greg KH <[email protected]>
Fixes 676141e48af7("blk-mq: don't dump CPU -> hw queue map on driver load")
---
 block/blk-mq-sysfs.c | 15 +--------------
 1 file changed, 1 insertion(+), 14 deletions(-)

No list of what changed from v1 under here?

Anyway, no, just delete the attribute please.

thanks,

greg k-h

Reviewed-by on v2 1/1
#5 lore raw
In reply to Ming Lei
Quoted 33 lines · Ming Lei

It is reported that sysfs buffer overflow can be triggered in case of too many CPU cores(>841 on 4K PAGE_SIZE) when showing CPUs in blk_mq_hw_sysfs_cpus_show().

So use cpumap_print_to_pagebuf() to print the info and fix the potential buffer overflow issue.

Cc Mark Ray <[email protected]>
Cc Greg KH <[email protected]>
Fixes 676141e48af7("blk-mq: don't dump CPU -> hw queue map on driver load")
---
 block/blk-mq-sysfs.c | 15 +--------------
 1 file changed, 1 insertion(+), 14 deletions(-)
block/blk-mq-sysfs.c +0 -8
index d6e1a9bd7131..4d0d32377ba3 100644
@@ -166,20 +166,7 @@ static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx,
 
 static ssize_t blk_mq_hw_sysfs_cpus_show(struct blk_mq_hw_ctx *hctx, char *page)
 {
-	unsigned int i, first = 1;
-	ssize_t ret = 0;
-
-	for_each_cpu(i, hctx->cpumask) {
-		if (first)
-			ret += sprintf(ret + page, "%u", i);
-		else
-			ret += sprintf(ret + page, ", %u", i);

block/blk-mq-sysfs.c static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx, · line 172

Quoted 10 lines
block/blk-mq-sysfs.c +1 -6
-
-		first = 0;
-	}
-
-	ret += sprintf(ret + page, "\n");
-	return ret;
+	return cpumap_print_to_pagebuf(true, page, hctx->cpumask);
 }
 
 static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_tags = {

And maybe you should send a update to the kABI documentation to keep Greg KH happy :-)

Cheers,

Hannes

Signature · 5 lines
Dr. Hannes Reinecke		   Teamlead Storage & Networking
[email protected]			               +49 911 74053 688
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Felix Imendörffer, Mary Higgins, Sri Rasiah
HRB 21284 (AG Nürnberg)