perf: Fix failure to set cpumask when only one cpu
PATCH 1/2 perf: Fix failure to set cpumask when only one cpu
From: He Zhe <[email protected]>
The buffer containing string used to set cpumask is overwritten by end of string later in cpu_map__snprint_mask due to not enough memory space, when there is only one cpu. And thus causes the following failure.
$ perf ftrace ls failed to reset ftrace
This patch fixes the calculation of cpumask string size.
--- tools/perf/builtin-ftrace.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/builtin-ftrace.c +1 -1
@@ -173,7 +173,7 @@ static int set_tracing_cpumask(struct cpu_map *cpumap)int last_cpu; last_cpu =cpu_map__cpu (cpumap, cpumap->nr -1 ); - mask_size = (last_cpu +3 ) /4 +1 ; + mask_size = last_cpu /4 +2 ;/* one more byte for EOS */ mask_size += last_cpu /32 ;/* ',' is needed for every 32th cpus */ cpumask =malloc (mask_size);
Signature · 1 line
2.7.4
PATCH 2/2 perf: Fix writing to illegal memory in handling cpumap mask
From: He Zhe <[email protected]>
cpu_map__snprint_mask would write to illegal memory pointed by zalloc(0) when there is only one cpu.
This patch fixes the calculation and adds sanity check against the input parameters.
--- tools/perf/util/cpumap.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
tools/perf/util/cpumap.c +4 -1
@@ -751,7 +751,10 @@ size_t cpu_map__snprint_mask(struct cpu_map *map, char *buf, size_t size)unsigned char *bitmap;int last_cpu =cpu_map__cpu (map, map->nr -1 ); - bitmap =zalloc ((last_cpu +7 ) /8 ); +if (buf ==NULL ) +return 0 ; + + bitmap =zalloc (last_cpu /8 +1 );if (bitmap ==NULL ) { buf[0 ] ='\0' ;return 0 ;
Signature · 1 line
2.7.4
Arnaldo Carvalho de Melo
AppliedRe: [PATCH 2/2] perf: Fix writing to illegal memory in handling cpumap mask
Em Fri, Aug 02, 2019 at 04:29:52PM +0800, [email protected] escreveu:
Quoted 7 lines
From: He Zhe <[email protected]>
cpu_map__snprint_maskwould write to illegal memory pointed by zalloc(0) when there is only one cpu.This patch fixes the calculation and adds sanity check against the input parameters.
Thanks, applied, and added the missing:
- Arnaldo
Quoted 23 lines
Signed-off-by He Zhe <[email protected]>--- tools/perf/util/cpumap.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)tools/perf/util/cpumap.c +4
-1@@ -751,7 +751,10 @@ size_t cpu_map__snprint_mask(struct cpu_map *map, char *buf, size_t size)unsigned char *bitmap;int last_cpu =cpu_map__cpu (map, map->nr -1 ); - bitmap =zalloc ((last_cpu +7 ) /8 ); +if (buf ==NULL ) +return 0 ; + + bitmap =zalloc (last_cpu /8 +1 );if (bitmap ==NULL ) { buf[0 ] ='\0' ;return 0 ;Signature · 1 line
2.7.4
Signature · 1 line
- Arnaldo
tip-bot for He Zhe
merge bot[tip:perf/urgent] perf cpumap: Fix writing to illegal memory in handling cpumap mask
In reply to [email protected]perf cpumap: Fix writing to illegal memory in handling cpumap mask
Commit-ID: 5f5e25f1c7933a6e1673515c0b1d5acd82fea1ed Gitweb: https://git.kernel.org/tip/5f5e25f1c7933a6e1673515c0b1d5acd82fea1ed Author: He Zhe <[email protected]> AuthorDate: Fri, 2 Aug 2019 16:29:52 +0800 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Thu, 8 Aug 2019 15:41:10 -0300
perf cpumap: Fix writing to illegal memory in handling cpumap mask
cpu_map__snprint_mask() would write to illegal memory pointed by zalloc(0) when there is only one cpu.
This patch fixes the calculation and adds sanity check against the input parameters.
--- tools/perf/util/cpumap.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
tools/perf/util/cpumap.c +4 -1
@@ -751,7 +751,10 @@ size_t cpu_map__snprint_mask(struct cpu_map *map, char *buf, size_t size)unsigned char *bitmap;int last_cpu =cpu_map__cpu (map, map->nr -1 ); - bitmap =zalloc ((last_cpu +7 ) /8 ); +if (buf ==NULL ) +return 0 ; + + bitmap =zalloc (last_cpu /8 +1 );if (bitmap ==NULL ) { buf[0 ] ='\0' ;return 0 ;
Quoted 21 lines · On Fri, Aug 02, 2019 at 04:29:51PM +0800, [email protected] wrote:
From: He Zhe <[email protected]>
The buffer containing string used to set cpumask is overwritten by end of string later in
cpu_map__snprint_maskdue to not enough memory space, when there is only one cpu. And thus causes the following failure.$ perf ftrace ls failed to reset ftraceThis patch fixes the calculation of cpumask string size.
Signed-off-by He Zhe <[email protected]>--- tools/perf/builtin-ftrace.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/perf/builtin-ftrace.c +0
-0
tools/perf/builtin-ftrace.c static int set_tracing_cpumask(struct cpu_map *cpumap) · line 177
Quoted 6 lines
tools/perf/builtin-ftrace.c +1
-1@@ -173,7 +173,7 @@ static int set_tracing_cpumask(struct cpu_map *cpumap)int last_cpu; last_cpu =cpu_map__cpu (cpumap, cpumap->nr -1 ); - mask_size = (last_cpu +3 ) /4 +1 ; + mask_size = last_cpu /4 +2 ;/* one more byte for EOS */ mask_size += last_cpu /32 ;/* ',' is needed for every 32th cpus */
ugh.. why do we care about last_cpu value in here at all?
feels like using static buffer would be more reasonable
jirka
Quoted 5 lines
tools/perf/builtin-ftrace.c +0
-0cpumask =malloc (mask_size);Signature · 1 line
2.7.4
Quoted 24 lines · Jiri Olsa
Quoted 23 lines · On Fri, Aug 02, 2019 at 04:29:51PM +0800, [email protected] wrote:
From: He Zhe <[email protected]>
The buffer containing string used to set cpumask is overwritten by end of string later in
cpu_map__snprint_maskdue to not enough memory space, when there is only one cpu. And thus causes the following failure.$ perf ftrace ls failed to reset ftraceThis patch fixes the calculation of cpumask string size.
Signed-off-by He Zhe <[email protected]>--- tools/perf/builtin-ftrace.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/perf/builtin-ftrace.c +0
-0@@ -173,7 +173,7 @@ static int set_tracing_cpumask(struct cpu_map *cpumap)int last_cpu;
Quoted 7 lines
tools/perf/builtin-ftrace.c static int set_tracing_cpumask(struct cpu_map *cpumap) · line 177
Quoted 4 lines
tools/perf/builtin-ftrace.c +1
-1last_cpu =cpu_map__cpu (cpumap, cpumap->nr -1 ); - mask_size = (last_cpu +3 ) /4 +1 ; + mask_size = last_cpu /4 +2 ;/* one more byte for EOS */ mask_size += last_cpu /32 ;/* ',' is needed for every 32th cpus */ ugh.. why do we care about
last_cpuvalue in here at all?feels like using static buffer would be more reasonable
Thanks, and yes, a static buffer would be easy to handle. A 2KB buffer is enough to cover 8196 cpus, the maximum numbers of cpus we can run with for now.
Let's see if there is any other concerns.
Zhe
Quoted 8 lines
jirka
Quoted 5 lines
tools/perf/builtin-ftrace.c +0
-0cpumask =malloc (mask_size);Signature · 1 line
2.7.4
Arnaldo Carvalho de Melo
AppliedEm Fri, Aug 02, 2019 at 04:29:51PM +0800, [email protected] escreveu:
Quoted 10 lines
From: He Zhe <[email protected]>
The buffer containing string used to set cpumask is overwritten by end of string later in
cpu_map__snprint_maskdue to not enough memory space, when there is only one cpu. And thus causes the following failure.$ perf ftrace ls failed to reset ftraceThis patch fixes the calculation of cpumask string size.
Please next time add this as well:
Applied,
- Arnaldo
Quoted 20 lines
Signed-off-by He Zhe <[email protected]>--- tools/perf/builtin-ftrace.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/perf/builtin-ftrace.c +1
-1@@ -173,7 +173,7 @@ static int set_tracing_cpumask(struct cpu_map *cpumap)int last_cpu; last_cpu =cpu_map__cpu (cpumap, cpumap->nr -1 ); - mask_size = (last_cpu +3 ) /4 +1 ; + mask_size = last_cpu /4 +2 ;/* one more byte for EOS */ mask_size += last_cpu /32 ;/* ',' is needed for every 32th cpus */ cpumask =malloc (mask_size);Signature · 1 line
2.7.4
Signature · 1 line
- Arnaldo
tip-bot for He Zhe
merge bot[tip:perf/urgent] perf ftrace: Fix failure to set cpumask when only one cpu is present
In reply to [email protected]perf ftrace: Fix failure to set cpumask when only one cpu is present
Commit-ID: cf30ae726c011e0372fd4c2d588466c8b50a8907 Gitweb: https://git.kernel.org/tip/cf30ae726c011e0372fd4c2d588466c8b50a8907 Author: He Zhe <[email protected]> AuthorDate: Fri, 2 Aug 2019 16:29:51 +0800 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Thu, 8 Aug 2019 15:41:10 -0300
perf ftrace: Fix failure to set cpumask when only one cpu is present
The buffer containing the string used to set cpumask is overwritten at the end of the string later in cpu_map__snprint_mask due to not enough memory space, when there is only one cpu.
And thus causes the following failure:
$ perf ftrace ls failed to reset ftrace $
This patch fixes the calculation of the cpumask string size.
--- tools/perf/builtin-ftrace.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/builtin-ftrace.c +1 -1
@@ -173,7 +173,7 @@ static int set_tracing_cpumask(struct cpu_map *cpumap)int last_cpu; last_cpu =cpu_map__cpu (cpumap, cpumap->nr -1 ); - mask_size = (last_cpu +3 ) /4 +1 ; + mask_size = last_cpu /4 +2 ;/* one more byte for EOS */ mask_size += last_cpu /32 ;/* ',' is needed for every 32th cpus */ cpumask =malloc (mask_size);