blk-mq: avoid sysfs buffer overflow by too many CPU cores
Subsystems (2)
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 in one hctx.
So use snprintf for avoiding the potential buffer overflow.
--- block/blk-mq-sysfs.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-)
block/blk-mq-sysfs.c +18 -12
@@ -164,22 +164,28 @@ static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx,return sprintf (page,"%u\n" , hctx->tags->nr_reserved_tags); } +/* avoid overflow by too many CPU cores */ 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 ; +unsigned int cpu =cpumask_first (hctx->cpumask); +ssize_t len =snprintf (page,PAGE_SIZE -1 ,"%u" , cpu); +int last_len = len; + +while ((cpu =cpumask_next (cpu, hctx->cpumask)) < nr_cpu_ids) { +int cur_len =snprintf (page + len,PAGE_SIZE -1 - len, +", %u" , cpu); +if (cur_len >=PAGE_SIZE -1 - len) { + len -= last_len; + len +=snprintf (page + len,PAGE_SIZE -1 - len, +"..." ); +break ; + } + len += cur_len; + last_len = cur_len; } - ret +=sprintf (ret + page,"\n" ); -return ret; + len +=snprintf (page + len,PAGE_SIZE -1 - len,"\n" ); +return len; }static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_tags = {
Signature · 1 line
2.20.1
Greg KH
maintainerQuoted 48 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 one hctx.So use snprintf for avoiding the potential buffer overflow.
Cc Mark Ray <[email protected]>Fixes 676141e48af7("blk-mq: don't dump CPU -> hw queue map on driver load")Signed-off-by Ming Lei <[email protected]>--- block/blk-mq-sysfs.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-)block/blk-mq-sysfs.c +14
-10@@ -164,22 +164,28 @@ static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx,return sprintf (page,"%u\n" , hctx->tags->nr_reserved_tags); } +/* avoid overflow by too many CPU cores */ 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 ; +unsigned int cpu =cpumask_first (hctx->cpumask); +ssize_t len =snprintf (page,PAGE_SIZE -1 ,"%u" , cpu); +int last_len = len; + +while ((cpu =cpumask_next (cpu, hctx->cpumask)) < nr_cpu_ids) { +int cur_len =snprintf (page + len,PAGE_SIZE -1 - len, +", %u" , cpu); +if (cur_len >=PAGE_SIZE -1 - len) { + len -= last_len; + len +=snprintf (page + len,PAGE_SIZE -1 - len, +"..." ); +break ; + }
block/blk-mq-sysfs.c static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx, · line 190
Quoted 10 lines
block/blk-mq-sysfs.c +4
-2+ len += cur_len; + last_len = cur_len; } - ret +=sprintf (ret + page,"\n" ); -return ret; + len +=snprintf (page + len,PAGE_SIZE -1 - len,"\n" ); +return len; }
What????
sysfs is "one value per file". You should NEVER have to care about the size of the sysfs buffer. If you do, you are doing something wrong.
What excatly are you trying to show in this sysfs file? I can't seem to find the Documenatation/ABI/ entry for it, am I just missing it because I don't know the filename for it?
thanks,
greg k-h
Quoted 62 lines · Greg KH
Quoted 48 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 one hctx.So use snprintf for avoiding the potential buffer overflow.
Cc Mark Ray <[email protected]>Fixes 676141e48af7("blk-mq: don't dump CPU -> hw queue map on driver load")Signed-off-by Ming Lei <[email protected]>--- block/blk-mq-sysfs.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-)block/blk-mq-sysfs.c +14
-10@@ -164,22 +164,28 @@ static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx,return sprintf (page,"%u\n" , hctx->tags->nr_reserved_tags); } +/* avoid overflow by too many CPU cores */ 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 ; +unsigned int cpu =cpumask_first (hctx->cpumask); +ssize_t len =snprintf (page,PAGE_SIZE -1 ,"%u" , cpu); +int last_len = len; + +while ((cpu =cpumask_next (cpu, hctx->cpumask)) < nr_cpu_ids) { +int cur_len =snprintf (page + len,PAGE_SIZE -1 - len, +", %u" , cpu); +if (cur_len >=PAGE_SIZE -1 - len) { + len -= last_len; + len +=snprintf (page + len,PAGE_SIZE -1 - len, +"..." ); +break ; + }block/blk-mq-sysfs.c static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx, · line 190
Quoted 10 lines
block/blk-mq-sysfs.c +4
-2+ len += cur_len; + last_len = cur_len; } - ret +=sprintf (ret + page,"\n" ); -return ret; + len +=snprintf (page + len,PAGE_SIZE -1 - len,"\n" ); +return len; }What????
Quoted 6 lines
sysfs is "one value per file". You should NEVER have to care about the size of the sysfs buffer. If you do, you are doing something wrong.
What excatly are you trying to show in this sysfs file? I can't seem to find the Documenatation/ABI/ entry for it, am I just missing it because I don't know the filename for it?
It is /sys/block/$DEV/mq/$N/cpu_list, all CPUs in this hctx($N) will be shown via sysfs buffer. The buffer size is one PAGE, how can it hold when there are too many CPUs(close to 1K)?
Thanks,
Ming
Greg KH
maintainerQuoted 66 lines · Ming Lei
Quoted 65 lines · Greg KH
Quoted 48 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 one hctx.So use snprintf for avoiding the potential buffer overflow.
Cc Mark Ray <[email protected]>Fixes 676141e48af7("blk-mq: don't dump CPU -> hw queue map on driver load")Signed-off-by Ming Lei <[email protected]>--- block/blk-mq-sysfs.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-)block/blk-mq-sysfs.c +14
-10@@ -164,22 +164,28 @@ static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx,return sprintf (page,"%u\n" , hctx->tags->nr_reserved_tags); } +/* avoid overflow by too many CPU cores */ 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 ; +unsigned int cpu =cpumask_first (hctx->cpumask); +ssize_t len =snprintf (page,PAGE_SIZE -1 ,"%u" , cpu); +int last_len = len; + +while ((cpu =cpumask_next (cpu, hctx->cpumask)) < nr_cpu_ids) { +int cur_len =snprintf (page + len,PAGE_SIZE -1 - len, +", %u" , cpu); +if (cur_len >=PAGE_SIZE -1 - len) { + len -= last_len; + len +=snprintf (page + len,PAGE_SIZE -1 - len, +"..." ); +break ; + }block/blk-mq-sysfs.c static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx, · line 190
Quoted 10 lines
block/blk-mq-sysfs.c +4
-2+ len += cur_len; + last_len = cur_len; } - ret +=sprintf (ret + page,"\n" ); -return ret; + len +=snprintf (page + len,PAGE_SIZE -1 - len,"\n" ); +return len; }What????
sysfs is "one value per file". You should NEVER have to care about the size of the sysfs buffer. If you do, you are doing something wrong.
Quoted 7 lines
Quoted 3 lines
What excatly are you trying to show in this sysfs file? I can't seem to find the Documenatation/ABI/ entry for it, am I just missing it because I don't know the filename for it?
It is
/sys/block/$DEV/mq/$N/cpu_list, all CPUs in this hctx($N) will be shown via sysfs buffer. The buffer size is one PAGE, how can it hold when there are too many CPUs(close to 1K)?
Looks like I only see 1 cpu listed on my machines in those files, what am I doing wrong?
Also, I don't see cpu_list in any of the documentation files, so I have no idea what you are trying to have this file show.
And again, "one value per file" is the sysfs rule. "all cpus in the system" is not "one value" :)
thanks,
greg k-h
Quoted 71 lines · Greg KH
Quoted 70 lines · Ming Lei
Quoted 68 lines · Greg KH
Quoted 48 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 one hctx.So use snprintf for avoiding the potential buffer overflow.
Cc Mark Ray <[email protected]>Fixes 676141e48af7("blk-mq: don't dump CPU -> hw queue map on driver load")Signed-off-by Ming Lei <[email protected]>--- block/blk-mq-sysfs.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-)block/blk-mq-sysfs.c +14
-10@@ -164,22 +164,28 @@ static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx,return sprintf (page,"%u\n" , hctx->tags->nr_reserved_tags); } +/* avoid overflow by too many CPU cores */ 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 ; +unsigned int cpu =cpumask_first (hctx->cpumask); +ssize_t len =snprintf (page,PAGE_SIZE -1 ,"%u" , cpu); +int last_len = len; + +while ((cpu =cpumask_next (cpu, hctx->cpumask)) < nr_cpu_ids) { +int cur_len =snprintf (page + len,PAGE_SIZE -1 - len, +", %u" , cpu); +if (cur_len >=PAGE_SIZE -1 - len) { + len -= last_len; + len +=snprintf (page + len,PAGE_SIZE -1 - len, +"..." ); +break ; + }block/blk-mq-sysfs.c static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx, · line 190
Quoted 10 lines
block/blk-mq-sysfs.c +4
-2+ len += cur_len; + last_len = cur_len; } - ret +=sprintf (ret + page,"\n" ); -return ret; + len +=snprintf (page + len,PAGE_SIZE -1 - len,"\n" ); +return len; }What????
sysfs is "one value per file". You should NEVER have to care about the size of the sysfs buffer. If you do, you are doing something wrong.
What excatly are you trying to show in this sysfs file? I can't seem to find the Documenatation/ABI/ entry for it, am I just missing it because I don't know the filename for it?
Quoted 6 lines
Quoted 3 lines
It is
/sys/block/$DEV/mq/$N/cpu_list, all CPUs in this hctx($N) will be shown via sysfs buffer. The buffer size is one PAGE, how can it hold when there are too many CPUs(close to 1K)?Looks like I only see 1 cpu listed on my machines in those files, what am I doing wrong?
It depends on machine. The issue is reported on one machine with 896 CPU cores, when 4K buffer can only hold 841 cores.
Quoted 6 lines
Also, I don't see
cpu_listin any of the documentation files, so I have no idea what you are trying to have this file show.And again, "one value per file" is the sysfs rule. "all cpus in the system" is not "one value" :)
I agree, and this file shouldn't be there, given each CPU will have one kobject dir under the hctx dir.
We may kill the 'cpu_list' attribute, is there anyone who objects?
Thanks,
Ming
Greg KH
maintainerQuoted 86 lines · Ming Lei
Quoted 71 lines · Greg KH
Quoted 70 lines · Ming Lei
Quoted 68 lines · Greg KH
Quoted 48 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 one hctx.So use snprintf for avoiding the potential buffer overflow.
Cc Mark Ray <[email protected]>Fixes 676141e48af7("blk-mq: don't dump CPU -> hw queue map on driver load")Signed-off-by Ming Lei <[email protected]>--- block/blk-mq-sysfs.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-)block/blk-mq-sysfs.c +14
-10@@ -164,22 +164,28 @@ static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx,return sprintf (page,"%u\n" , hctx->tags->nr_reserved_tags); } +/* avoid overflow by too many CPU cores */ 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 ; +unsigned int cpu =cpumask_first (hctx->cpumask); +ssize_t len =snprintf (page,PAGE_SIZE -1 ,"%u" , cpu); +int last_len = len; + +while ((cpu =cpumask_next (cpu, hctx->cpumask)) < nr_cpu_ids) { +int cur_len =snprintf (page + len,PAGE_SIZE -1 - len, +", %u" , cpu); +if (cur_len >=PAGE_SIZE -1 - len) { + len -= last_len; + len +=snprintf (page + len,PAGE_SIZE -1 - len, +"..." ); +break ; + }block/blk-mq-sysfs.c static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx, · line 190
Quoted 10 lines
block/blk-mq-sysfs.c +4
-2+ len += cur_len; + last_len = cur_len; } - ret +=sprintf (ret + page,"\n" ); -return ret; + len +=snprintf (page + len,PAGE_SIZE -1 - len,"\n" ); +return len; }What????
sysfs is "one value per file". You should NEVER have to care about the size of the sysfs buffer. If you do, you are doing something wrong.
What excatly are you trying to show in this sysfs file? I can't seem to find the Documenatation/ABI/ entry for it, am I just missing it because I don't know the filename for it?
Quoted 6 lines
Quoted 3 lines
It is
/sys/block/$DEV/mq/$N/cpu_list, all CPUs in this hctx($N) will be shown via sysfs buffer. The buffer size is one PAGE, how can it hold when there are too many CPUs(close to 1K)?Looks like I only see 1 cpu listed on my machines in those files, what am I doing wrong?
It depends on machine. The issue is reported on one machine with 896 CPU cores, when 4K buffer can only hold 841 cores.
Quoted 4 lines
Also, I don't see
cpu_listin any of the documentation files, so I have no idea what you are trying to have this file show.
Quoted 7 lines
Quoted 2 lines
And again, "one value per file" is the sysfs rule. "all cpus in the system" is not "one value" :)
I agree, and this file shouldn't be there, given each CPU will have one kobject dir under the hctx dir.
We may kill the '
cpu_list' attribute, is there anyone who objects?
Given that it was never documented, perhaps no one actually uses it :)
greg k-h
Hi Ming,
In the customer case, the cpu_list file was not needed. It was just part of a SAP Hana script to collect all the block device data (similar to sosreport). So they were just dumping everything, and it picks up the mq-related files.
I know with IRQs, we have bitmaps/mask, and can represent the list such as "0-27", without listing every CPU. I'm sure there's lots of options to address this, and getting rid of the cpu_list is one of them.
Best Regards,
Mark Ray
HPE Global Solutions Engineering
[email protected]
-----Original Message-----
From: Ming Lei [mailto:[email protected]]
Sent: Thursday, August 15, 2019 9:43 PM
To: Greg KH <[email protected]>
Cc: Jens Axboe <[email protected]>; [email protected]; [email protected]; Ray, Mark C (Global Solutions Engineering (GSE)) <[email protected]>
Subject: Re: [PATCH] blk-mq: avoid sysfs buffer overflow by too many CPU cores
Quoted 71 lines · Greg KH
Quoted 70 lines · Ming Lei
Quoted 68 lines · Greg KH
Quoted 48 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 one hctx.So use snprintf for avoiding the potential buffer overflow.
Cc Mark Ray <[email protected]>Fixes 676141e48af7("blk-mq: don't dump CPU -> hw queue map ondriver load")
Signed-off-by Ming Lei <[email protected]>--- block/blk-mq-sysfs.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-)block/blk-mq-sysfs.c index renamed from block/blk-mq-sysfs.c +0
-0
d6e1a9bd7131..e75f41a98415100644block/blk-mq-sysfs.c +14
-10@@ -164,22 +164,28 @@ static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx,return sprintf (page,"%u\n" , hctx->tags->nr_reserved_tags); } +/* avoid overflow by too many CPU cores */ 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 ; +unsigned int cpu =cpumask_first (hctx->cpumask); +ssize_t len =snprintf (page,PAGE_SIZE -1 ,"%u" , cpu); +int last_len = len; + +while ((cpu =cpumask_next (cpu, hctx->cpumask)) < nr_cpu_ids) { +int cur_len =snprintf (page + len,PAGE_SIZE -1 - len, +", %u" , cpu); +if (cur_len >=PAGE_SIZE -1 - len) { + len -= last_len; + len +=snprintf (page + len,PAGE_SIZE -1 - len, +"..." ); +break ; + }block/blk-mq-sysfs.c static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx, · line 189
Quoted 10 lines
block/blk-mq-sysfs.c +4
-2+ len += cur_len; + last_len = cur_len; } - ret +=sprintf (ret + page,"\n" ); -return ret; + len +=snprintf (page + len,PAGE_SIZE -1 - len,"\n" ); +return len; }What????
sysfs is "one value per file". You should NEVER have to care about the size of the sysfs buffer. If you do, you are doing something wrong.
What excatly are you trying to show in this sysfs file? I can't seem to find the Documenatation/ABI/ entry for it, am I just missing it because I don't know the filename for it?
Quoted 6 lines
Quoted 3 lines
It is
/sys/block/$DEV/mq/$N/cpu_list, all CPUs in this hctx($N) will be shown via sysfs buffer. The buffer size is one PAGE, how can it hold when there are too many CPUs(close to 1K)?Looks like I only see 1 cpu listed on my machines in those files, what am I doing wrong?
It depends on machine. The issue is reported on one machine with 896 CPU cores, when 4K buffer can only hold 841 cores.
Quoted 6 lines
Also, I don't see
cpu_listin any of the documentation files, so I have no idea what you are trying to have this file show.And again, "one value per file" is the sysfs rule. "all cpus in the system" is not "one value" :)
I agree, and this file shouldn't be there, given each CPU will have one kobject dir under the hctx dir.
We may kill the 'cpu_list' attribute, is there anyone who objects?
Thanks,
Ming
Quoted 5 lines · Mark C (Global Solutions Engineering (GSE))
Hi Ming,
In the customer case, the
cpu_listfile was not needed. It was just part of a SAP Hana script to collect all the block device data (similar to sosreport). So they were just dumping everything, and it picks up the mq-related files.I know with IRQs, we have bitmaps/mask, and can represent the list such as "0-27", without listing every CPU. I'm sure there's lots of options to address this, and getting rid of the
cpu_listis one of them.
Indeed, same with several attributes under /sys/devices/system/cpu/, actually we can use cpumap_print_to_pagebuf() to print the CPUs.
Thanks,
Ming
Greg KH
maintainerQuoted 9 lines · Ming Lei
Quoted 5 lines · Mark C (Global Solutions Engineering (GSE))
Hi Ming,
In the customer case, the
cpu_listfile was not needed. It was just part of a SAP Hana script to collect all the block device data (similar to sosreport). So they were just dumping everything, and it picks up the mq-related files.I know with IRQs, we have bitmaps/mask, and can represent the list such as "0-27", without listing every CPU. I'm sure there's lots of options to address this, and getting rid of the
cpu_listis one of them.Indeed, same with several attributes under
/sys/devices/system/cpu/, actually we can usecpumap_print_to_pagebuf()to print the CPUs.
And that is changing the format of the file, which means it is obvious no one is using it, so just please delete the thing.
thanks,
greg k-h
Jens Axboe
maintainerQuoted 6 lines · Greg KH
Quoted 5 lines · Ming Lei
Quoted 4 lines · Mark C (Global Solutions Engineering (GSE))
Hi Ming,
In the customer case, the
cpu_listfile was not needed. It was just part of a SAP Hana script to collect all the block device data (similar to sosreport). So they were just dumping everything, and it picks up the mq-related files.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
I know with IRQs, we have bitmaps/mask, and can represent the list such as "0-27", without listing every CPU. I'm sure there's lots of options to address this, and getting rid of the
cpu_listis one of them.Indeed, same with several attributes under
/sys/devices/system/cpu/, actually we can usecpumap_print_to_pagebuf()to print the CPUs.And that is changing the format of the file, which means it is obvious no one is using it, so just please delete the thing.
IFF that patch was valid, then yes, it follows that you could delete it. But that's not a given.
Signature · 1 line
Jens Axboe