perf cpumap: Make counter as unsigned ints
PATCH 1/1 perf cpumap: Make counter as unsigned ints
These are loop counters which is inherently unsigned. Therefore make them unsigned. Moreover it also fixes alloc-size-larger-than error with gcc-13, where malloc can be called with (-1) due to tmp_len being an int type.
Fixes
| cpumap.c:366:20: error: argument 1 range [18446744065119617024, 18446744073709551612] exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=] | 366 | tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); | | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--- tools/lib/perf/cpumap.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
tools/lib/perf/cpumap.c +5 -5
@@ -351,8 +351,8 @@ struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig, struct perf_cpu_map *other) {struct perf_cpu *tmp_cpus; -int tmp_len; -int i, j, k; +unsigned int tmp_len; +unsigned int i, j, k;struct perf_cpu_map *merged;if (perf_cpu_map__is_subset (orig, other))
@@ -369,7 +369,7 @@ struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig,/* Standard merge algorithm from wikipedia */ i = j = k =0 ; -while (i < orig->nr && j < other->nr) { +while (i < (unsigned int )orig->nr && j < (unsigned int )other->nr) {if (orig->map[i].cpu <= other->map[j].cpu) {if (orig->map[i].cpu == other->map[j].cpu) j++;
@@ -378,10 +378,10 @@ struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig, tmp_cpus[k++] = other->map[j++]; } -while (i < orig->nr) +while (i < (unsigned int )orig->nr) tmp_cpus[k++] = orig->map[i++]; -while (j < other->nr) +while (j < (unsigned int )other->nr) tmp_cpus[k++] = other->map[j++];assert (k <= tmp_len);
Signature · 1 line
2.39.1
Ian Rogers
reviewerQuoted 39 lines · Khem Raj
These are loop counters which is inherently unsigned. Therefore make them unsigned. Moreover it also fixes alloc-size-larger-than error with gcc-13, where malloc can be called with (-1) due to
tmp_lenbeing an int type.Fixes
| cpumap.c:366:20: error: argument 1 range [18446744065119617024, 18446744073709551612] exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=] | 366 | tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); | | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Signed-off-by Khem Raj <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Ingo Molnar <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Mark Rutland <[email protected]>Cc Alexander Shishkin <[email protected]>Cc Jiri Olsa <[email protected]>Cc Namhyung Kim <[email protected]>--- tools/lib/perf/cpumap.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)tools/lib/perf/cpumap.c +2
-2@@ -351,8 +351,8 @@ struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig, struct perf_cpu_map *other) {struct perf_cpu *tmp_cpus; -int tmp_len; -int i, j, k; +unsigned int tmp_len; +unsigned int i, j, k;struct perf_cpu_map *merged;if (perf_cpu_map__is_subset (orig, other))
tools/lib/perf/cpumap.c struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig, · line 372
Quoted 5 lines
tools/lib/perf/cpumap.c +1
-1@@ -369,7 +369,7 @@ struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig,/* Standard merge algorithm from wikipedia */ i = j = k =0 ; -while (i < orig->nr && j < other->nr) { +while (i < (unsigned int )orig->nr && j < (unsigned int )other->nr) {
Rather than cast 'nr' why not add the unsigned to the declaration?
Thanks,
Ian
Quoted 19 lines
tools/lib/perf/cpumap.c +2
-3if (orig->map[i].cpu <= other->map[j].cpu) {if (orig->map[i].cpu == other->map[j].cpu) j++;@@ -378,10 +378,10 @@ struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig, tmp_cpus[k++] = other->map[j++]; } -while (i < orig->nr) +while (i < (unsigned int )orig->nr) tmp_cpus[k++] = orig->map[i++]; -while (j < other->nr) +while (j < (unsigned int )other->nr) tmp_cpus[k++] = other->map[j++];assert (k <= tmp_len); --2.39.1
Quoted 42 lines · Ian Rogers
Quoted 40 lines · Khem Raj
These are loop counters which is inherently unsigned. Therefore make them unsigned. Moreover it also fixes alloc-size-larger-than error with gcc-13, where malloc can be called with (-1) due to
tmp_lenbeing an int type.Fixes
| cpumap.c:366:20: error: argument 1 range [18446744065119617024, 18446744073709551612] exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=] | 366 | tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); | | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Signed-off-by Khem Raj <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Ingo Molnar <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Mark Rutland <[email protected]>Cc Alexander Shishkin <[email protected]>Cc Jiri Olsa <[email protected]>Cc Namhyung Kim <[email protected]>--- tools/lib/perf/cpumap.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)tools/lib/perf/cpumap.c +2
-2@@ -351,8 +351,8 @@ struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig, struct perf_cpu_map *other) {struct perf_cpu *tmp_cpus; -int tmp_len; -int i, j, k; +unsigned int tmp_len; +unsigned int i, j, k;struct perf_cpu_map *merged;if (perf_cpu_map__is_subset (orig, other))@@ -369,7 +369,7 @@ struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig,
Quoted 7 lines
tools/lib/perf/cpumap.c struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig, · line 372
Quoted 4 lines
tools/lib/perf/cpumap.c +1
-1/* Standard merge algorithm from wikipedia */ i = j = k =0 ; -while (i < orig->nr && j < other->nr) { +while (i < (unsigned int )orig->nr && j < (unsigned int )other->nr) {Rather than cast 'nr' why not add the unsigned to the declaration?
if type of nr is changed from int to unsigned int, this will ensue a lot of changes including function signature changes, I am not sure if that is the best approach.
Quoted 22 lines
Thanks,
IanQuoted 19 lines
tools/lib/perf/cpumap.c +2
-3if (orig->map[i].cpu <= other->map[j].cpu) {if (orig->map[i].cpu == other->map[j].cpu) j++;@@ -378,10 +378,10 @@ struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig, tmp_cpus[k++] = other->map[j++]; } -while (i < orig->nr) +while (i < (unsigned int )orig->nr) tmp_cpus[k++] = orig->map[i++]; -while (j < other->nr) +while (j < (unsigned int )other->nr) tmp_cpus[k++] = other->map[j++];assert (k <= tmp_len); --2.39.1
Ian Rogers
reviewerQuoted 52 lines · Khem Raj
Quoted 42 lines · Ian Rogers
Quoted 40 lines · Khem Raj
These are loop counters which is inherently unsigned. Therefore make them unsigned. Moreover it also fixes alloc-size-larger-than error with gcc-13, where malloc can be called with (-1) due to
tmp_lenbeing an int type.Fixes
| cpumap.c:366:20: error: argument 1 range [18446744065119617024, 18446744073709551612] exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=] | 366 | tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); | | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Signed-off-by Khem Raj <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Ingo Molnar <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Mark Rutland <[email protected]>Cc Alexander Shishkin <[email protected]>Cc Jiri Olsa <[email protected]>Cc Namhyung Kim <[email protected]>--- tools/lib/perf/cpumap.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)tools/lib/perf/cpumap.c +2
-2@@ -351,8 +351,8 @@ struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig, struct perf_cpu_map *other) {struct perf_cpu *tmp_cpus; -int tmp_len; -int i, j, k; +unsigned int tmp_len; +unsigned int i, j, k;struct perf_cpu_map *merged;if (perf_cpu_map__is_subset (orig, other))@@ -369,7 +369,7 @@ struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig,Quoted 7 lines
tools/lib/perf/cpumap.c struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig, · line 372
Quoted 4 lines
tools/lib/perf/cpumap.c +1
-1/* Standard merge algorithm from wikipedia */ i = j = k =0 ; -while (i < orig->nr && j < other->nr) { +while (i < (unsigned int )orig->nr && j < (unsigned int )other->nr) {Rather than cast 'nr' why not add the unsigned to the declaration?
Quoted 3 lines
if type of nr is changed from int to unsigned int, this will ensue a lot of changes including function signature changes, I am not sure if that is the best approach.
All non-libperf accesses to 'nr' should go through perf_cpu_map__nr:
https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/tree/tools/lib/perf/include/perf/cpumap.h?h=perf/core#n25
Fwiw, this is done as there is some hope of 1 day landing a better reference count checker.
We should migrate all __nr users to being unsigned. The use of __nr is somewhat confusing, the dummy "program me on any CPU" cpu map has an nr of 1, while the nr for being on all CPUs will be the number of CPUs in the system, possibly +1 for the dummy case. I'm reminded that cpumap needs its API clearing up, perf_cpu_map__empty being especially confusing.
Thanks,
Ian
Quoted 22 lines
Quoted 22 lines
Thanks,
IanQuoted 19 lines
tools/lib/perf/cpumap.c +2
-3if (orig->map[i].cpu <= other->map[j].cpu) {if (orig->map[i].cpu == other->map[j].cpu) j++;@@ -378,10 +378,10 @@ struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig, tmp_cpus[k++] = other->map[j++]; } -while (i < orig->nr) +while (i < (unsigned int )orig->nr) tmp_cpus[k++] = orig->map[i++]; -while (j < other->nr) +while (j < (unsigned int )other->nr) tmp_cpus[k++] = other->map[j++];assert (k <= tmp_len); --2.39.1