Skip to contentSkip to conversation

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

PATCH Applied Ming Lei 5 participants 9 messages Nov 2, 2019 – Nov 4, 2019

Subsystems (2)
lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 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 of hctx via /sys/block/$DEV/mq/$N/cpu_list.

So use snprintf for avoiding the potential buffer overflow.

This version doesn't change the attribute format, and simply stop to show CPU number if the buffer is to be overflow.

Fixes 676141e48af7("blk-mq: don't dump CPU -> hw queue map on driver load")
---
 block/blk-mq-sysfs.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
block/blk-mq-sysfs.c +10 -5
index a0d3ce30fa08..68996ef1d339 100644
@@ -166,20 +166,25 @@ 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)
 {
+	const size_t size = PAGE_SIZE - 1;
 	unsigned int i, first = 1;
-	ssize_t ret = 0;
+	int ret = 0, pos = 0;
 
 	for_each_cpu(i, hctx->cpumask) {
 		if (first)
-			ret += sprintf(ret + page, "%u", i);
+			ret = snprintf(pos + page, size - pos, "%u", i);
 		else
-			ret += sprintf(ret + page, ", %u", i);
+			ret = snprintf(pos + page, size - pos, ", %u", i);
+
+		if (ret >= size - pos)
+			break;
 
 		first = 0;
+		pos += ret;
 	}
 
-	ret += sprintf(ret + page, "\n");
-	return ret;
+	ret = snprintf(pos + page, size - pos, "\n");
+	return pos + ret;
 }
 
 static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_tags = {
Signature · 1 line
2.20.1
maintainer Applied
#1 lore raw
Quoted 8 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 of hctx via /sys/block/$DEV/mq/$N/cpu_list.

So use snprintf for avoiding the potential buffer overflow.

This version doesn't change the attribute format, and simply stop to show CPU number if the buffer is to be overflow.

Applied, thanks.

Signature · 1 line
Jens Axboe
#2 lore raw
In reply to Ming Lei

Ming,

Quoted 8 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 of hctx via/sys/block/$DEV/mq/$N/cpu_list.

So use snprintf for avoiding the potential buffer overflow.

This version doesn't change the attribute format, and simply stop to show CPU number if the buffer is to be overflow.

Does it make sense to also add a print or WARN_ON in case of overflow ?

maintainer
#3 lore raw
Quoted 7 lines · Chaitanya Kulkarni

Ming,

Quoted 4 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 of hctx via/sys/block/$DEV/mq/$N/cpu_list.

Quoted 6 lines
Quoted 4 lines

So use snprintf for avoiding the potential buffer overflow.

This version doesn't change the attribute format, and simply stop to show CPU number if the buffer is to be overflow.

Does it make sense to also add a print or WARN_ON in case of overflow ?

Just more noise I think, really wouldn't serve any purpose.

Signature · 1 line
Jens Axboe
#4 lore raw

Okay.

Quoted 6 lines

Just more noise I think, really wouldn't serve any purpose.

Signature · 1 line
Jens Axboe
#5 lore raw
In reply to Chaitanya Kulkarni
Quoted 8 lines · Chaitanya Kulkarni

Ming,

Quoted 4 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 of hctx via/sys/block/$DEV/mq/$N/cpu_list.

Quoted 6 lines
Quoted 4 lines

So use snprintf for avoiding the potential buffer overflow.

This version doesn't change the attribute format, and simply stop to show CPU number if the buffer is to be overflow.

Does it make sense to also add a print or WARN_ON in case of overflow ?

Yes, it does, could you cook a patch for that?

Thanks,
Ming Lei

maintainer
#6 lore raw
Quoted 10 lines · Ming Lei
Quoted 8 lines · Chaitanya Kulkarni

Ming,

Quoted 4 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 of hctx via/sys/block/$DEV/mq/$N/cpu_list.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

So use snprintf for avoiding the potential buffer overflow.

This version doesn't change the attribute format, and simply stop to show CPU number if the buffer is to be overflow.

Does it make sense to also add a print or WARN_ON in case of overflow ?

Yes, it does, could you cook a patch for that?

No it doesn't. The WARN_ON brings absolutely nothing. If you're using a script, it gets the same values out and doesn't see the warning. If it's a human cat'ing it, they will probably already realize that we're missing CPUs. Or maybe not even see the warning. It's useless.

We should either make this seqfile, or just kill the file. Those are the only two options that make any sense.

Signature · 1 line
Jens Axboe
#7 lore raw
Quoted 20 lines · Jens Axboe
Quoted 10 lines · Ming Lei
Quoted 8 lines · Chaitanya Kulkarni

Ming,

Quoted 4 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 of hctx via/sys/block/$DEV/mq/$N/cpu_list.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

So use snprintf for avoiding the potential buffer overflow.

This version doesn't change the attribute format, and simply stop to show CPU number if the buffer is to be overflow.

Does it make sense to also add a print or WARN_ON in case of overflow ?

Yes, it does, could you cook a patch for that?

Quoted 8 lines

No it doesn't. The WARN_ON brings absolutely nothing. If you're using a script, it gets the same values out and doesn't see the warning. If it's a human cat'ing it, they will probably already realize that we're missing CPUs. Or maybe not even see the warning. It's useless.

We should either make this seqfile, or just kill the file. Those are the only two options that make any sense.

I'd rather retain that file; it proved really useful when debugging interrupt affinity issues.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke		      Teamlead Storage & Networking
[email protected]			                  +49 911 74053 688
SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 247165 (AG München), GF: Felix Imendörffer
maintainer
#8 lore raw
Quoted 26 lines · Hannes Reinecke
Quoted 25 lines · Jens Axboe
Quoted 10 lines · Ming Lei
Quoted 8 lines · Chaitanya Kulkarni

Ming,

Quoted 4 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 of hctx via/sys/block/$DEV/mq/$N/cpu_list.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

So use snprintf for avoiding the potential buffer overflow.

This version doesn't change the attribute format, and simply stop to show CPU number if the buffer is to be overflow.

Does it make sense to also add a print or WARN_ON in case of overflow ?

Yes, it does, could you cook a patch for that?

No it doesn't. The WARN_ON brings absolutely nothing. If you're using a script, it gets the same values out and doesn't see the warning. If it's a human cat'ing it, they will probably already realize that we're missing CPUs. Or maybe not even see the warning. It's useless.

Quoted 5 lines
Quoted 3 lines

We should either make this seqfile, or just kill the file. Those are the only two options that make any sense.

I'd rather retain that file; it proved really useful when debugging interrupt affinity issues.

Care to take a look at converting to seq_file?

Signature · 1 line
Jens Axboe