Skip to contentSkip to conversation

perf: Fix failure to set cpumask when only one cpu

PATCH Applied [email protected] 4 participants 8 messages Aug 2, 2019 – Aug 8, 2019 perf events

lore
Share & tools
lore
#0 lore raw

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
index 66d5a66..0193128 100644
@@ -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
#1 lore raw

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
index 3acfbe3..39cce66 100644
@@ -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
#2 lore raw

Re: [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_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.

Thanks, applied, and added the missing:

Fixes 4400ac8a9a90 ("perf cpumap: Introduce cpu_map__snprint_mask()")

- Arnaldo

Quoted 23 lines
---
 tools/perf/util/cpumap.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tools/perf/util/cpumap.c +4 -1
index 3acfbe3..39cce66 100644
@@ -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
#3 lore raw

[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.

Cc Alexander Shishkin <[email protected]>
Cc Alexey Budankov <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Kan Liang <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephane Eranian <[email protected]>
Fixes 4400ac8a9a90 ("perf cpumap: Introduce cpu_map__snprint_mask()")
---
 tools/perf/util/cpumap.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tools/perf/util/cpumap.c +4 -1
index 3acfbe34ebaf..39cce66b4ebc 100644
@@ -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;
#4 lore raw
In reply to [email protected]
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_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 +0 -0
index 66d5a66..0193128 100644

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 -0
 	cpumask = malloc(mask_size);
Signature · 1 line
2.7.4
author
#5 lore raw
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_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 +0 -0
index 66d5a66..0193128 100644
@@ -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 -1
 	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

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 -0
 	cpumask = malloc(mask_size);
Signature · 1 line
2.7.4
#6 lore raw
In reply to [email protected]

Em 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_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.

Please next time add this as well:

Fixes dc23103278c5 ("perf ftrace: Add support for -a and -C option")

Applied,

- Arnaldo

Quoted 20 lines
---
 tools/perf/builtin-ftrace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/builtin-ftrace.c +1 -1
index 66d5a66..0193128 100644
@@ -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
#7 lore raw

[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.

Cc Alexander Shishkin <[email protected]>
Cc Alexey Budankov <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Kan Liang <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephane Eranian <[email protected]>
Fixes dc23103278c5 ("perf ftrace: Add support for -a and -C option")
---
 tools/perf/builtin-ftrace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/builtin-ftrace.c +1 -1
index 66d5a6658daf..019312810405 100644
@@ -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);