tools/lib/perf: Fix -Werror=alloc-size-larger-than in cpumap.c
PATCH 1/1 tools/lib/perf: Fix -Werror=alloc-size-larger-than in cpumap.c
perf build break observed when using gcc 13-3 (FC39 ppc64le) with the following error.
cpumap.c: In function 'perf_cpu_map__merge':
cpumap.c:414:20: error: argument 1 range [18446744069414584320, 18446744073709551614] exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=]
414 | tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from cpumap.c:4:
/usr/include/stdlib.h:672:14: note: in a call to allocation function 'malloc' declared here
672 | extern void *malloc (size_t __size) __THROW __attribute_malloc__
| ^~~~~~cc1: all warnings being treated as errors
Error happens to be only in gcc13-3 and not in latest gcc 14. Even though git-bisect pointed bad commit as:
'commit f5b07010c13c ("libperf: Don't remove -g when EXTRA_CFLAGS are used")', issue is with tmp_len being "int". It holds number of cpus and making it "unsigned int" fixes the issues.
After the fix:
CC util/pmu-flex.o CC util/expr-flex.o LD util/perf-util-in.o LD perf-util-in.o AR libperf-util.a LINK perf GEN python/perf.cpython-312-powerpc64le-linux-gnu.so
--- tools/lib/perf/cpumap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/perf/cpumap.c +1 -1
@@ -398,7 +398,7 @@ bool perf_cpu_map__is_subset(const struct perf_cpu_map *a, const struct perf_cpuint perf_cpu_map__merge (struct perf_cpu_map **orig,struct perf_cpu_map *other) {struct perf_cpu *tmp_cpus; -int tmp_len; +unsigned int tmp_len;int i, j, k;struct perf_cpu_map *merged;
Signature · 1 line
2.43.5
Quoted 33 lines · Likhitha Korrapati
perf build break observed when using gcc 13-3 (FC39 ppc64le) with the following error.
cpumap.c: In function 'perf_cpu_map__merge':
cpumap.c:414:20: error: argument 1 range [18446744069414584320, 18446744073709551614] exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=]414 | tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from cpumap.c:4: /usr/include/stdlib.h:672:14: note: in a call to allocation function 'malloc' declared here 672 | extern void *malloc (size_t __size) __THROW __attribute_malloc__ | ^~~~~~cc1: all warnings being treated as errors
Error happens to be only in gcc13-3 and not in latest gcc 14. Even though git-bisect pointed bad commit as:
'commitf5b07010c13c("libperf: Don't remove -g whenEXTRA_CFLAGSare used")', issue is withtmp_lenbeing "int". It holds number of cpus and making it "unsigned int" fixes the issues.After the fix:
CC util/pmu-flex.o CC util/expr-flex.o LD util/perf-util-in.o LD perf-util-in.o AR libperf-util.a LINK perf GEN python/perf.cpython-312-powerpc64le-linux-gnu.so
Quoted 1 line
Signed-off-by Likhitha Korrapati <[email protected]>
Looks good to me
Thanks
Athira
Quoted 20 lines
---
tools/lib/perf/cpumap.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)tools/lib/perf/cpumap.c +1
-1@@ -398,7 +398,7 @@ bool perf_cpu_map__is_subset(const struct perf_cpu_map *a, const struct perf_cpuint perf_cpu_map__merge (struct perf_cpu_map **orig,struct perf_cpu_map *other) {struct perf_cpu *tmp_cpus; -int tmp_len; +unsigned int tmp_len;int i, j, k;struct perf_cpu_map *merged;Signature · 1 line
2.43.5
Venkat Rao Bagalkote
Tested-byQuoted 32 lines · Athira Rajeev
Quoted 32 lines · Likhitha Korrapati
perf build break observed when using gcc 13-3 (FC39 ppc64le) with the following error.
cpumap.c: In function 'perf_cpu_map__merge':
cpumap.c:414:20: error: argument 1 range [18446744069414584320, 18446744073709551614] exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=]414 | tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from cpumap.c:4: /usr/include/stdlib.h:672:14: note: in a call to allocation function 'malloc' declared here 672 | extern void *malloc (size_t __size) __THROW __attribute_malloc__ | ^~~~~~cc1: all warnings being treated as errors
Error happens to be only in gcc13-3 and not in latest gcc 14. Even though git-bisect pointed bad commit as:
'commitf5b07010c13c("libperf: Don't remove -g whenEXTRA_CFLAGSare used")', issue is withtmp_lenbeing "int". It holds number of cpus and making it "unsigned int" fixes the issues.After the fix:
CC util/pmu-flex.o CC util/expr-flex.o LD util/perf-util-in.o LD perf-util-in.o AR libperf-util.a LINK perf GEN python/perf.cpython-312-powerpc64le-linux-gnu.so
Quoted 4 lines
Quoted 1 line
Signed-off-by Likhitha Korrapati <[email protected]>Looks good to me
Reviewed-by Athira Rajeev <[email protected]>
Tested this patch on perf-tools-next repo, and this patch fixes the issue.
Regards,
Venkat.
Quoted 24 lines
Thanks
AthiraQuoted 20 lines
---
tools/lib/perf/cpumap.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)tools/lib/perf/cpumap.c +1
-1@@ -398,7 +398,7 @@ bool perf_cpu_map__is_subset(const struct perf_cpu_map *a, const struct perf_cpuint perf_cpu_map__merge (struct perf_cpu_map **orig,struct perf_cpu_map *other) {struct perf_cpu *tmp_cpus; -int tmp_len; +unsigned int tmp_len;int i, j, k;struct perf_cpu_map *merged;Signature · 1 line
2.43.5
Quoted 34 lines · Venkat Rao Bagalkote
Quoted 32 lines · Athira Rajeev
Quoted 32 lines · Likhitha Korrapati
perf build break observed when using gcc 13-3 (FC39 ppc64le) with the following error.
cpumap.c: In function 'perf_cpu_map__merge':
cpumap.c:414:20: error: argument 1 range [18446744069414584320, 18446744073709551614] exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=] 414 |tmp_cpus=malloc(tmp_len * sizeof(struct perf_cpu));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from cpumap.c:4:/usr/include/stdlib.h:672:14: note: in a call to allocation function 'malloc' declared here
672 | extern void *malloc (size_t__size)__THROW__attribute_malloc__
| ^~~~~~
cc1: all warnings being treated as errorsError happens to be only in gcc13-3 and not in latest gcc 14. Even though git-bisect pointed bad commit as:
'commitf5b07010c13c("libperf: Don't remove -g whenEXTRA_CFLAGSare used")', issue is withtmp_lenbeing "int". It holds number of cpus and making it "unsigned int" fixes the issues.After the fix:
CC
util/pmu-flex.o
CCutil/expr-flex.o
LDutil/perf-util-in.o
LD perf-util-in.o
AR libperf-util.a
LINK perf
GEN python/perf.cpython-312-powerpc64le-linux-gnu.so
Quoted 10 lines
Quoted 4 lines
Quoted 1 line
Signed-off-by Likhitha Korrapati <[email protected]>Looks good to me
Reviewed-by Athira Rajeev <[email protected]>Tested this patch on perf-tools-next repo, and this patch fixes the issue.
Tested-by Venkat Rao Bagalkote <[email protected]>
Arnaldo, Namhyung,
can you consider pulling this fix? since it is breaking the build in gcc13-3 or if you have any comments do let us know.
Thanks
Maddy
Quoted 28 lines
Regards,
Venkat.
Quoted 24 lines
Thanks
AthiraQuoted 20 lines
---
tools/lib/perf/cpumap.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)tools/lib/perf/cpumap.c +1
-1@@ -398,7 +398,7 @@ bool perf_cpu_map__is_subset(const struct perf_cpu_map *a, const struct perf_cpuint perf_cpu_map__merge (struct perf_cpu_map **orig,struct perf_cpu_map *other) {struct perf_cpu *tmp_cpus; -int tmp_len; +unsigned int tmp_len;int i, j, k;struct perf_cpu_map *merged;--
2.43.5
Quoted 52 lines · Madhavan Srinivasan
Quoted 34 lines · Venkat Rao Bagalkote
Quoted 32 lines · Athira Rajeev
Quoted 32 lines · Likhitha Korrapati
perf build break observed when using gcc 13-3 (FC39 ppc64le) with the following error.
cpumap.c: In function 'perf_cpu_map__merge':
cpumap.c:414:20: error: argument 1 range [18446744069414584320, 18446744073709551614] exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=]414 | tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from cpumap.c:4: /usr/include/stdlib.h:672:14: note: in a call to allocation function 'malloc' declared here 672 | extern void *malloc (size_t __size) __THROW __attribute_malloc__ | ^~~~~~cc1: all warnings being treated as errors
Error happens to be only in gcc13-3 and not in latest gcc 14. Even though git-bisect pointed bad commit as:
'commitf5b07010c13c("libperf: Don't remove -g whenEXTRA_CFLAGSare used")', issue is withtmp_lenbeing "int". It holds number of cpus and making it "unsigned int" fixes the issues.After the fix:
CC util/pmu-flex.o CC util/expr-flex.o LD util/perf-util-in.o LD perf-util-in.o AR libperf-util.a LINK perf GEN python/perf.cpython-312-powerpc64le-linux-gnu.soQuoted 10 lines
Quoted 4 lines
Quoted 1 line
Signed-off-by Likhitha Korrapati <[email protected]>Looks good to me
Reviewed-by Athira Rajeev <[email protected]>Tested this patch on perf-tools-next repo, and this patch fixes the issue.
Tested-by Venkat Rao Bagalkote <[email protected]>
Quoted 7 lines
Arnaldo, Namhyung,
can you consider pulling this fix? since it is breaking the build in gcc13-3 or if you have any comments do let us know.
Thanks
Maddy
Hi,
Can we get this pulled in if the change looks good ? This is breaking build on gcc-13-3 Looking for feedback on this patch..
Thanks
Athira
Quoted 29 lines
Quoted 29 lines
Regards,
Venkat.
Quoted 22 lines
Thanks
AthiraQuoted 19 lines
---
tools/lib/perf/cpumap.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)tools/lib/perf/cpumap.c +1
-1@@ -398,7 +398,7 @@ bool perf_cpu_map__is_subset(const struct perf_cpu_map *a, const struct perf_cpuint perf_cpu_map__merge (struct perf_cpu_map **orig,struct perf_cpu_map *other) {struct perf_cpu *tmp_cpus; -int tmp_len; +unsigned int tmp_len;int i, j, k;struct perf_cpu_map *merged;Signature · 1 line
2.43.5
Arnaldo Carvalho de Melo
maintainerQuoted 39 lines · Athira Rajeev
Quoted 38 lines · Madhavan Srinivasan
Quoted 36 lines · Venkat Rao Bagalkote
Quoted 35 lines · Athira Rajeev
Quoted 31 lines · Likhitha Korrapati
perf build break observed when using gcc 13-3 (FC39 ppc64le) with the following error.
cpumap.c: In function 'perf_cpu_map__merge':
cpumap.c:414:20: error: argument 1 range [18446744069414584320, 18446744073709551614] exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=]414 | tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from cpumap.c:4: /usr/include/stdlib.h:672:14: note: in a call to allocation function 'malloc' declared here 672 | extern void *malloc (size_t __size) __THROW __attribute_malloc__ | ^~~~~~cc1: all warnings being treated as errors
Error happens to be only in gcc13-3 and not in latest gcc 14. Even though git-bisect pointed bad commit as:
'commitf5b07010c13c("libperf: Don't remove -g whenEXTRA_CFLAGSare used")', issue is withtmp_lenbeing "int". It holds number of cpus and making it "unsigned int" fixes the issues.After the fix:
CC util/pmu-flex.o CC util/expr-flex.o LD util/perf-util-in.o LD perf-util-in.o AR libperf-util.a LINK perf GEN python/perf.cpython-312-powerpc64le-linux-gnu.soQuoted 1 line
Signed-off-by Likhitha Korrapati <[email protected]>Looks good to me
Reviewed-by Athira Rajeev <[email protected]>
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Tested this patch on perf-tools-next repo, and this patch fixes the issue.
Tested-by Venkat Rao Bagalkote <[email protected]>Arnaldo, Namhyung,
can you consider pulling this fix? since it is breaking the build in gcc13-3 or if you have any comments do let us know.
This isn't the only place in that file where this pattern exists:
⬢ [acme@toolbx perf-tools-next]$ grep malloc tools/lib/perf/cpumap.c
cpus = malloc(sizeof(*cpus) + sizeof(struct perf_cpu) * nr_cpus); tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu));
⬢ [acme@toolbx perf-tools-next]$
struct perf_cpu_map *perf_cpu_map__alloc(int nr_cpus)
{
RC_STRUCT(perf_cpu_map) *cpus;
struct perf_cpu_map *result;
if (nr_cpus == 0)
return NULL;
cpus = malloc(sizeof(*cpus) + sizeof(struct perf_cpu) * nr_cpus);int 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;
struct perf_cpu_map *merged;
if (perf_cpu_map__is_subset(*orig, other))
return 0;
if (perf_cpu_map__is_subset(other, *orig)) {
perf_cpu_map__put(*orig);
*orig = perf_cpu_map__get(other);
return 0;
}
tmp_len = __perf_cpu_map__nr(*orig) + __perf_cpu_map__nr(other);
tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu));struct perf_cpu_map *perf_cpu_map__intersect(struct perf_cpu_map *orig,
struct perf_cpu_map *other)
{
struct perf_cpu *tmp_cpus;
int tmp_len;
int i, j, k;
struct perf_cpu_map *merged = NULL;
if (perf_cpu_map__is_subset(other, orig))
return perf_cpu_map__get(orig);
if (perf_cpu_map__is_subset(orig, other))
return perf_cpu_map__get(other);
tmp_len = max(__perf_cpu_map__nr(orig), __perf_cpu_map__nr(other));
tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu));I'm trying to figure out why its only in perf_cpu_map__merge() that this triggers :-\
Maybe that max() call in perf_cpu_map__intersect() somehow makes the compiler happy.
And in perf_cpu_map__alloc() all calls seems to validate it.
But wouldn't turning this into a calloc() be better?
Like:
tools/lib/perf/cpumap.c +1 -1
@@ -411,7 +411,7 @@ int perf_cpu_map__merge(struct perf_cpu_map **orig, struct perf_cpu_map *other) } tmp_len =__perf_cpu_map__nr (*orig) +__perf_cpu_map__nr (other); - tmp_cpus =malloc (tmp_len *sizeof (struct perf_cpu )); + tmp_cpus =calloc (tmp_len,sizeof (struct perf_cpu ));if (!tmp_cpus)return -ENOMEM ;
⬢ [acme@toolbx perf-tools-next]$
And better, do the max size that the compiler is trying to help us catch?
- Arnaldo
Hi Arnaldo,
Quoted 95 lines · Arnaldo Carvalho de Melo
Quoted 29 lines · Athira Rajeev
Quoted 29 lines · Madhavan Srinivasan
Quoted 27 lines · Venkat Rao Bagalkote
Quoted 26 lines · Athira Rajeev
Quoted 26 lines · Likhitha Korrapati
perf build break observed when using gcc 13-3 (FC39 ppc64le)
with the following error.cpumap.c: In function 'perf_cpu_map__merge':
cpumap.c:414:20: error: argument 1 range [18446744069414584320, 18446744073709551614] exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=]414 | tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from cpumap.c:4: /usr/include/stdlib.h:672:14: note: in a call to allocation function 'malloc' declared here 672 | extern void *malloc (size_t __size) __THROW __attribute_malloc__ | ^~~~~~cc1: all warnings being treated as errors
Error happens to be only in gcc13-3 and not in latest gcc 14. Even though git-bisect pointed bad commit as:
'commitf5b07010c13c("libperf: Don't remove -g whenEXTRA_CFLAGSare used")', issue is withtmp_lenbeing "int". It holds number of cpus and making it "unsigned int" fixes the issues.
After the fix:CC util/pmu-flex.o CC util/expr-flex.o LD util/perf-util-in.o LD perf-util-in.o AR libperf-util.a LINK perf GEN python/perf.cpython-312-powerpc64le-linux-gnu.soSigned-off-by Likhitha Korrapati<[email protected]>Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Looks good to me
Reviewed-by Athira Rajeev<[email protected]>Tested this patch on perf-tools-next repo, and this patch fixes the issue.
Tested-by Venkat Rao Bagalkote<[email protected]>Arnaldo, Namhyung,
can you consider pulling this fix? since it is breaking the build in gcc13-3 or if you have any comments do let us know.This isn't the only place in that file where this pattern exists:
⬢ [acme@toolbx perf-tools-next]$ grep malloc
tools/lib/perf/cpumap.ccpus = malloc(sizeof(*cpus) + sizeof(struct perf_cpu) * nr_cpus); tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu));⬢ [acme@toolbx perf-tools-next]$
struct
perf_cpu_map*perf_cpu_map__alloc(intnr_cpus){ RC_STRUCT(perf_cpu_map) *cpus; struct perf_cpu_map *result; if (nr_cpus == 0) return NULL; cpus = malloc(sizeof(*cpus) + sizeof(struct perf_cpu) * nr_cpus);int
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; struct perf_cpu_map *merged; if (perf_cpu_map__is_subset(*orig, other)) return 0; if (perf_cpu_map__is_subset(other, *orig)) { perf_cpu_map__put(*orig); *orig = perf_cpu_map__get(other); return 0; } tmp_len = __perf_cpu_map__nr(*orig) + __perf_cpu_map__nr(other); tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu));struct
perf_cpu_map*perf_cpu_map__intersect(structperf_cpu_map*orig,
structperf_cpu_map*other){ struct perf_cpu *tmp_cpus; int tmp_len; int i, j, k; struct perf_cpu_map *merged = NULL; if (perf_cpu_map__is_subset(other, orig)) return perf_cpu_map__get(orig); if (perf_cpu_map__is_subset(orig, other)) return perf_cpu_map__get(other); tmp_len = max(__perf_cpu_map__nr(orig), __perf_cpu_map__nr(other)); tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu));I'm trying to figure out why its only in
perf_cpu_map__merge()that this triggers :-\
Quoted 6 lines
Maybe that
max()call inperf_cpu_map__intersect()somehow makes the compiler happy.And in
perf_cpu_map__alloc()all calls seems to validate it.But wouldn't turning this into a
calloc()be better?
I have tried using calloc() instead of malloc() and the issue still exists even using calloc(). cpumap.c: In function ‘perf_cpu_map__merge’:
cpumap.c:414:20: error: argument 1 range [18446744071562067968, 18446744073709551615] exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=]
414 | tmp_cpus = calloc(tmp_len , sizeof(struct perf_cpu));
Quoted 19 lines
Like:
tools/lib/perf/cpumap.c +1
-1@@ -411,7 +411,7 @@ int perf_cpu_map__merge(struct perf_cpu_map **orig, struct perf_cpu_map *other) } tmp_len =__perf_cpu_map__nr (*orig) +__perf_cpu_map__nr (other); - tmp_cpus =malloc (tmp_len *sizeof (struct perf_cpu )); + tmp_cpus =calloc (tmp_len,sizeof (struct perf_cpu ));if (!tmp_cpus)return -ENOMEM ;⬢ [acme@toolbx perf-tools-next]$
Quoted 4 lines
And better, do the max size that the compiler is trying to help us catch?
- Arnaldo
I have tried using max and it compiles with that. I am doing testing with this change and will be posting a V2.
Thanks
Likhitha.
Quoted 1 line
Hi Arnaldo,
Quoted 95 lines · Arnaldo Carvalho de Melo
Quoted 29 lines · Athira Rajeev
Quoted 29 lines · Madhavan Srinivasan
Quoted 27 lines · Venkat Rao Bagalkote
Quoted 26 lines · Athira Rajeev
Quoted 26 lines · Likhitha Korrapati
perf build break observed when using gcc 13-3 (FC39 ppc64le)
with the following error.cpumap.c: In function 'perf_cpu_map__merge':
cpumap.c:414:20: error: argument 1 range [18446744069414584320, 18446744073709551614] exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=]414 | tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from cpumap.c:4: /usr/include/stdlib.h:672:14: note: in a call to allocation function 'malloc' declared here 672 | extern void *malloc (size_t __size) __THROW __attribute_malloc__ | ^~~~~~cc1: all warnings being treated as errors
Error happens to be only in gcc13-3 and not in latest gcc 14. Even though git-bisect pointed bad commit as:
'commitf5b07010c13c("libperf: Don't remove -g whenEXTRA_CFLAGSare used")', issue is withtmp_lenbeing "int". It holds number of cpus and making it "unsigned int" fixes the issues.
After the fix:CC util/pmu-flex.o CC util/expr-flex.o LD util/perf-util-in.o LD perf-util-in.o AR libperf-util.a LINK perf GEN python/perf.cpython-312-powerpc64le-linux-gnu.soSigned-off-by Likhitha Korrapati<[email protected]>Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Looks good to me
Reviewed-by Athira Rajeev<[email protected]>Tested this patch on perf-tools-next repo, and this patch fixes the issue.
Tested-by Venkat Rao Bagalkote<[email protected]>Arnaldo, Namhyung,
can you consider pulling this fix? since it is breaking the build in gcc13-3 or if you have any comments do let us know.This isn't the only place in that file where this pattern exists:
⬢ [acme@toolbx perf-tools-next]$ grep malloc
tools/lib/perf/cpumap.ccpus = malloc(sizeof(*cpus) + sizeof(struct perf_cpu) * nr_cpus); tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu));⬢ [acme@toolbx perf-tools-next]$
struct
perf_cpu_map*perf_cpu_map__alloc(intnr_cpus){ RC_STRUCT(perf_cpu_map) *cpus; struct perf_cpu_map *result; if (nr_cpus == 0) return NULL; cpus = malloc(sizeof(*cpus) + sizeof(struct perf_cpu) * nr_cpus);int
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; struct perf_cpu_map *merged; if (perf_cpu_map__is_subset(*orig, other)) return 0; if (perf_cpu_map__is_subset(other, *orig)) { perf_cpu_map__put(*orig); *orig = perf_cpu_map__get(other); return 0; } tmp_len = __perf_cpu_map__nr(*orig) + __perf_cpu_map__nr(other); tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu));struct
perf_cpu_map*perf_cpu_map__intersect(structperf_cpu_map*orig,
structperf_cpu_map*other){ struct perf_cpu *tmp_cpus; int tmp_len; int i, j, k; struct perf_cpu_map *merged = NULL; if (perf_cpu_map__is_subset(other, orig)) return perf_cpu_map__get(orig); if (perf_cpu_map__is_subset(orig, other)) return perf_cpu_map__get(other); tmp_len = max(__perf_cpu_map__nr(orig), __perf_cpu_map__nr(other)); tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu));I'm trying to figure out why its only in
perf_cpu_map__merge()that this triggers :-\
Quoted 6 lines
Maybe that
max()call inperf_cpu_map__intersect()somehow makes the compiler happy.And in
perf_cpu_map__alloc()all calls seems to validate it.But wouldn't turning this into a
calloc()be better?
I have tried using calloc() instead of malloc() and the issue still exists even using calloc(). cpumap.c: In function ‘perf_cpu_map__merge’:
cpumap.c:414:20: error: argument 1 range [18446744071562067968, 18446744073709551615] exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=]
414 | tmp_cpus = calloc(tmp_len , sizeof(struct perf_cpu));
Quoted 19 lines
Like:
tools/lib/perf/cpumap.c +1
-1@@ -411,7 +411,7 @@ int perf_cpu_map__merge(struct perf_cpu_map **orig, struct perf_cpu_map *other) } tmp_len =__perf_cpu_map__nr (*orig) +__perf_cpu_map__nr (other); - tmp_cpus =malloc (tmp_len *sizeof (struct perf_cpu )); + tmp_cpus =calloc (tmp_len,sizeof (struct perf_cpu ));if (!tmp_cpus)return -ENOMEM ;⬢ [acme@toolbx perf-tools-next]$
Quoted 4 lines
And better, do the max size that the compiler is trying to help us catch?
- Arnaldo
I have tried using max and it compiles with that. I am doing testing with this change and will be posting a V2.
Thanks
Likhitha
Hi Arnaldo,
Quoted 107 lines · Arnaldo Carvalho de Melo
Quoted 39 lines · Athira Rajeev
Quoted 38 lines · Madhavan Srinivasan
Quoted 36 lines · Venkat Rao Bagalkote
Quoted 35 lines · Athira Rajeev
Quoted 31 lines · Likhitha Korrapati
perf build break observed when using gcc 13-3 (FC39 ppc64le) with the following error.
cpumap.c: In function 'perf_cpu_map__merge':
cpumap.c:414:20: error: argument 1 range [18446744069414584320, 18446744073709551614] exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=]414 | tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from cpumap.c:4: /usr/include/stdlib.h:672:14: note: in a call to allocation function 'malloc' declared here 672 | extern void *malloc (size_t __size) __THROW __attribute_malloc__ | ^~~~~~cc1: all warnings being treated as errors
Error happens to be only in gcc13-3 and not in latest gcc 14. Even though git-bisect pointed bad commit as:
'commitf5b07010c13c("libperf: Don't remove -g whenEXTRA_CFLAGSare used")', issue is withtmp_lenbeing "int". It holds number of cpus and making it "unsigned int" fixes the issues.After the fix:
CC util/pmu-flex.o CC util/expr-flex.o LD util/perf-util-in.o LD perf-util-in.o AR libperf-util.a LINK perf GEN python/perf.cpython-312-powerpc64le-linux-gnu.soQuoted 1 line
Signed-off-by Likhitha Korrapati <[email protected]>Looks good to me
Reviewed-by Athira Rajeev <[email protected]>Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Tested this patch on perf-tools-next repo, and this patch fixes the issue.
Tested-by Venkat Rao Bagalkote <[email protected]>Arnaldo, Namhyung,
can you consider pulling this fix? since it is breaking the build in gcc13-3 or if you have any comments do let us know.
This isn't the only place in that file where this pattern exists:
⬢ [acme@toolbx perf-tools-next]$ grep malloc
tools/lib/perf/cpumap.ccpus = malloc(sizeof(*cpus) + sizeof(struct perf_cpu) * nr_cpus); tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu));⬢ [acme@toolbx perf-tools-next]$
struct
perf_cpu_map*perf_cpu_map__alloc(intnr_cpus){ RC_STRUCT(perf_cpu_map) *cpus; struct perf_cpu_map *result; if (nr_cpus == 0) return NULL; cpus = malloc(sizeof(*cpus) + sizeof(struct perf_cpu) * nr_cpus);int
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; struct perf_cpu_map *merged; if (perf_cpu_map__is_subset(*orig, other)) return 0; if (perf_cpu_map__is_subset(other, *orig)) { perf_cpu_map__put(*orig); *orig = perf_cpu_map__get(other); return 0; } tmp_len = __perf_cpu_map__nr(*orig) + __perf_cpu_map__nr(other); tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu));struct
perf_cpu_map*perf_cpu_map__intersect(structperf_cpu_map*orig,
structperf_cpu_map*other){ struct perf_cpu *tmp_cpus; int tmp_len; int i, j, k; struct perf_cpu_map *merged = NULL; if (perf_cpu_map__is_subset(other, orig)) return perf_cpu_map__get(orig); if (perf_cpu_map__is_subset(orig, other)) return perf_cpu_map__get(other); tmp_len = max(__perf_cpu_map__nr(orig), __perf_cpu_map__nr(other)); tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu));I'm trying to figure out why its only in
perf_cpu_map__merge()that this triggers :-\
Quoted 6 lines
Maybe that
max()call inperf_cpu_map__intersect()somehow makes the compiler happy.And in
perf_cpu_map__alloc()all calls seems to validate it.But wouldn't turning this into a
calloc()be better?
I have tried using calloc() instead of malloc() and the issue still exists even using calloc().
cpumap.c: In function ‘perf_cpu_map__merge’:
cpumap.c:414:20: error: argument 1 range [18446744071562067968, 18446744073709551615] exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=]
414 | tmp_cpus = calloc(tmp_len , sizeof(struct perf_cpu));
Quoted 19 lines
Like:
tools/lib/perf/cpumap.c +1
-1@@ -411,7 +411,7 @@ int perf_cpu_map__merge(struct perf_cpu_map **orig, struct perf_cpu_map *other) } tmp_len =__perf_cpu_map__nr (*orig) +__perf_cpu_map__nr (other); - tmp_cpus =malloc (tmp_len *sizeof (struct perf_cpu )); + tmp_cpus =calloc (tmp_len,sizeof (struct perf_cpu ));if (!tmp_cpus)return -ENOMEM ;⬢ [acme@toolbx perf-tools-next]$
Quoted 4 lines
And better, do the max size that the compiler is trying to help us catch?
- Arnaldo
I have tried using max and it worked. I am doing testing with this change and will be posting a V2.
Thanks
Likhitha
Quoted 132 lines · Arnaldo Carvalho de Melo
Quoted 39 lines · Athira Rajeev
Quoted 38 lines · Madhavan Srinivasan
Quoted 36 lines · Venkat Rao Bagalkote
Quoted 35 lines · Athira Rajeev
Quoted 31 lines · Likhitha Korrapati
perf build break observed when using gcc 13-3 (FC39 ppc64le) with the following error.
cpumap.c: In function 'perf_cpu_map__merge':
cpumap.c:414:20: error: argument 1 range [18446744069414584320, 18446744073709551614] exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=]414 | tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from cpumap.c:4: /usr/include/stdlib.h:672:14: note: in a call to allocation function 'malloc' declared here 672 | extern void *malloc (size_t __size) __THROW __attribute_malloc__ | ^~~~~~cc1: all warnings being treated as errors
Error happens to be only in gcc13-3 and not in latest gcc 14. Even though git-bisect pointed bad commit as:
'commitf5b07010c13c("libperf: Don't remove -g whenEXTRA_CFLAGSare used")', issue is withtmp_lenbeing "int". It holds number of cpus and making it "unsigned int" fixes the issues.After the fix:
CC util/pmu-flex.o CC util/expr-flex.o LD util/perf-util-in.o LD perf-util-in.o AR libperf-util.a LINK perf GEN python/perf.cpython-312-powerpc64le-linux-gnu.soQuoted 1 line
Signed-off-by Likhitha Korrapati <[email protected]>Looks good to me
Reviewed-by Athira Rajeev <[email protected]>Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Tested this patch on perf-tools-next repo, and this patch fixes the issue.
Tested-by Venkat Rao Bagalkote <[email protected]>Arnaldo, Namhyung,
can you consider pulling this fix? since it is breaking the build in gcc13-3 or if you have any comments do let us know.
This isn't the only place in that file where this pattern exists:
⬢ [acme@toolbx perf-tools-next]$ grep malloc
tools/lib/perf/cpumap.ccpus = malloc(sizeof(*cpus) + sizeof(struct perf_cpu) * nr_cpus); tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu));⬢ [acme@toolbx perf-tools-next]$
struct
perf_cpu_map*perf_cpu_map__alloc(intnr_cpus){ RC_STRUCT(perf_cpu_map) *cpus; struct perf_cpu_map *result; if (nr_cpus == 0) return NULL; cpus = malloc(sizeof(*cpus) + sizeof(struct perf_cpu) * nr_cpus);int
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; struct perf_cpu_map *merged; if (perf_cpu_map__is_subset(*orig, other)) return 0; if (perf_cpu_map__is_subset(other, *orig)) { perf_cpu_map__put(*orig); *orig = perf_cpu_map__get(other); return 0; } tmp_len = __perf_cpu_map__nr(*orig) + __perf_cpu_map__nr(other); tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu));struct
perf_cpu_map*perf_cpu_map__intersect(structperf_cpu_map*orig,
structperf_cpu_map*other){ struct perf_cpu *tmp_cpus; int tmp_len; int i, j, k; struct perf_cpu_map *merged = NULL; if (perf_cpu_map__is_subset(other, orig)) return perf_cpu_map__get(orig); if (perf_cpu_map__is_subset(orig, other)) return perf_cpu_map__get(other); tmp_len = max(__perf_cpu_map__nr(orig), __perf_cpu_map__nr(other)); tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu));I'm trying to figure out why its only in
perf_cpu_map__merge()that this triggers :-\Maybe that
max()call inperf_cpu_map__intersect()somehow makes the compiler happy.And in
perf_cpu_map__alloc()all calls seems to validate it.But wouldn't turning this into a
calloc()be better?Like:
tools/lib/perf/cpumap.c +1
-1@@ -411,7 +411,7 @@ int perf_cpu_map__merge(struct perf_cpu_map **orig, struct perf_cpu_map *other) } tmp_len =__perf_cpu_map__nr (*orig) +__perf_cpu_map__nr (other); - tmp_cpus =malloc (tmp_len *sizeof (struct perf_cpu )); + tmp_cpus =calloc (tmp_len,sizeof (struct perf_cpu ));if (!tmp_cpus)return -ENOMEM ;⬢ [acme@toolbx perf-tools-next]$
Quoted 4 lines
And better, do the max size that the compiler is trying to help us catch?
- Arnaldo
Isn't it better to use perf_cpu_map__nr. That should fix this problem.
One question I have, in perf_cpu_map__nr, the function is returning 1 in case *cpus is NULL. Is it ok to do that? wouldn't it cause problems?
Regards,
Mukesh
Arnaldo Carvalho de Melo
maintainerQuoted 18 lines · Mukesh Kumar Chaurasiya
Quoted 16 lines · Arnaldo Carvalho de Melo
Maybe that
max()call inperf_cpu_map__intersect()somehow makes the compiler happy.And in
perf_cpu_map__alloc()all calls seems to validate it.Like:
+++
b/tools/lib/perf/cpumap.ctools/lib/perf/cpumap.c +1
-1@@ -411,7 +411,7 @@ int perf_cpu_map__merge(struct perf_cpu_map **orig, struct perf_cpu_map *other) } tmp_len =__perf_cpu_map__nr (*orig) +__perf_cpu_map__nr (other); - tmp_cpus =malloc (tmp_len *sizeof (struct perf_cpu )); + tmp_cpus =calloc (tmp_len,sizeof (struct perf_cpu ));if (!tmp_cpus)return -ENOMEM ;
Quoted 6 lines
Quoted 4 lines
⬢ [acme@toolbx perf-tools-next]$
And better, do the max size that the compiler is trying to help us catch?
Isn't it better to use
perf_cpu_map__nr. That should fix this problem.
Maybe, have you tried it?
Quoted 2 lines
One question I have, in
perf_cpu_map__nr, the function is returning 1 in case *cpus is NULL. Is it ok to do that? wouldn't it cause problems?
Indeed this better be documented, as by just looking at:
int perf_cpu_map__nr(const struct perf_cpu_map *cpus)
{
return cpus ? __perf_cpu_map__nr(cpus) : 1;
}It really doesn't make much sense to say that a NULL map has one entry.
But the next functions are:
bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map)
{
return map ? __perf_cpu_map__cpu(map, 0).cpu == -1 : true;
}bool perf_cpu_map__is_any_cpu_or_is_empty(const struct perf_cpu_map *map)
{
if (!map)
return true;
return __perf_cpu_map__nr(map) == 1 && __perf_cpu_map__cpu(map, 0).cpu == -1;
}bool perf_cpu_map__is_empty(const struct perf_cpu_map *map)
{
return map == NULL;
}So it seems that a NULL cpu map means "any/all CPU) and a map with just one entry would have as its content "-1" that would mean "any/all CPU".
Ian did work on trying to simplify/clarify this, so maybe he can chime in :-)
- Arnaldo
Ian Rogers
reviewerQuoted 61 lines · Arnaldo Carvalho de Melo
Quoted 18 lines · Mukesh Kumar Chaurasiya
Quoted 16 lines · Arnaldo Carvalho de Melo
Maybe that
max()call inperf_cpu_map__intersect()somehow makes the compiler happy.And in
perf_cpu_map__alloc()all calls seems to validate it.Like:
+++
b/tools/lib/perf/cpumap.ctools/lib/perf/cpumap.c +1
-1@@ -411,7 +411,7 @@ int perf_cpu_map__merge(struct perf_cpu_map **orig, struct perf_cpu_map *other) } tmp_len =__perf_cpu_map__nr (*orig) +__perf_cpu_map__nr (other); - tmp_cpus =malloc (tmp_len *sizeof (struct perf_cpu )); + tmp_cpus =calloc (tmp_len,sizeof (struct perf_cpu ));if (!tmp_cpus)return -ENOMEM ;Quoted 6 lines
Quoted 4 lines
⬢ [acme@toolbx perf-tools-next]$
And better, do the max size that the compiler is trying to help us catch?
Isn't it better to use
perf_cpu_map__nr. That should fix this problem.Maybe, have you tried it?
Quoted 2 lines
One question I have, in
perf_cpu_map__nr, the function is returning 1 in case *cpus is NULL. Is it ok to do that? wouldn't it cause problems?Indeed this better be documented, as by just looking at:
int
perf_cpu_map__nr(const struct perf_cpu_map *cpus){ return cpus ? __perf_cpu_map__nr(cpus) : 1; }It really doesn't make much sense to say that a NULL map has one entry.
But the next functions are:
bool
perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map){ return map ? __perf_cpu_map__cpu(map, 0).cpu == -1 : true; }bool
perf_cpu_map__is_any_cpu_or_is_empty(const struct perf_cpu_map *map){ if (!map) return true; return __perf_cpu_map__nr(map) == 1 && __perf_cpu_map__cpu(map, 0).cpu == -1; }bool
perf_cpu_map__is_empty(const struct perf_cpu_map *map){ return map == NULL; }
Quoted 5 lines
So it seems that a NULL cpu map means "any/all CPU) and a map with just one entry would have as its content "-1" that would mean "any/all CPU".
Ian did work on trying to simplify/clarify this, so maybe he can chime in :-)
So I've tried to improve the naming but not vary the implementation greatly - initially I was in the code fixing reference count checking issues. There is an important distinction between "all" meaning a range of CPUs like 0-15 on a 16 core/hyperthread system, and "any" meaning the special "-1" value. It is possible to have a perf_cpu_map to both be "all" and "any", iterating an empty perf_cpu_map has strangely also meant the "any" and so the code isn't specific but relies on these odd properties.
Anyway, I'm not sure on the implication of this with malloc/calloc/unsigned... It would seem reasonable to me for __perf_cpu_map__nr to return an unsigned number and to propagate that to fix the new GCC issue.
Thanks,
Ian
Quoted 1 line
- Arnaldo
Ian Rogers
reviewerQuoted 70 lines · Ian Rogers
Quoted 61 lines · Arnaldo Carvalho de Melo
Quoted 18 lines · Mukesh Kumar Chaurasiya
Quoted 16 lines · Arnaldo Carvalho de Melo
Maybe that
max()call inperf_cpu_map__intersect()somehow makes the compiler happy.And in
perf_cpu_map__alloc()all calls seems to validate it.Like:
+++
b/tools/lib/perf/cpumap.ctools/lib/perf/cpumap.c +1
-1@@ -411,7 +411,7 @@ int perf_cpu_map__merge(struct perf_cpu_map **orig, struct perf_cpu_map *other) } tmp_len =__perf_cpu_map__nr (*orig) +__perf_cpu_map__nr (other); - tmp_cpus =malloc (tmp_len *sizeof (struct perf_cpu )); + tmp_cpus =calloc (tmp_len,sizeof (struct perf_cpu ));if (!tmp_cpus)return -ENOMEM ;Quoted 6 lines
Quoted 4 lines
⬢ [acme@toolbx perf-tools-next]$
And better, do the max size that the compiler is trying to help us catch?
Isn't it better to use
perf_cpu_map__nr. That should fix this problem.Maybe, have you tried it?
Quoted 2 lines
One question I have, in
perf_cpu_map__nr, the function is returning 1 in case *cpus is NULL. Is it ok to do that? wouldn't it cause problems?Indeed this better be documented, as by just looking at:
int
perf_cpu_map__nr(const struct perf_cpu_map *cpus){ return cpus ? __perf_cpu_map__nr(cpus) : 1; }It really doesn't make much sense to say that a NULL map has one entry.
But the next functions are:
bool
perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map){ return map ? __perf_cpu_map__cpu(map, 0).cpu == -1 : true; }bool
perf_cpu_map__is_any_cpu_or_is_empty(const struct perf_cpu_map *map){ if (!map) return true; return __perf_cpu_map__nr(map) == 1 && __perf_cpu_map__cpu(map, 0).cpu == -1; }bool
perf_cpu_map__is_empty(const struct perf_cpu_map *map){ return map == NULL; }Quoted 5 lines
So it seems that a NULL cpu map means "any/all CPU) and a map with just one entry would have as its content "-1" that would mean "any/all CPU".
Ian did work on trying to simplify/clarify this, so maybe he can chime in :-)
Quoted 8 lines
So I've tried to improve the naming but not vary the implementation greatly - initially I was in the code fixing reference count checking issues. There is an important distinction between "all" meaning a range of CPUs like 0-15 on a 16 core/hyperthread system, and "any" meaning the special "-1" value. It is possible to have a
perf_cpu_mapto both be "all" and "any", iterating an emptyperf_cpu_maphas strangely also meant the "any" and so the code isn't specific but relies on these odd properties.
I just remembered, there was also a discussion with Adrian IIRC. I wanted "all" to mean all possible CPUs on a system, and "online" to be either the same or a subset of that, in case CPUs were offline. There was a different view that the distinction between "all" and "online" wasn't useful. So there is some confusion in the code in this regard and some errors may exist when CPU cores are offline. There's definitely work to make the API better.
Thanks,
Ian
Quoted 10 lines
Anyway, I'm not sure on the implication of this with malloc/calloc/unsigned... It would seem reasonable to me for
__perf_cpu_map__nrto return an unsigned number and to propagate that to fix the new GCC issue.Thanks,
IanQuoted 1 line
- Arnaldo
Hi Arnaldo,
Quoted 19 lines · Arnaldo Carvalho de Melo
Quoted 17 lines · Mukesh Kumar Chaurasiya
Quoted 16 lines · Arnaldo Carvalho de Melo
Maybe that
max()call inperf_cpu_map__intersect()somehow makes the compiler happy.And in
perf_cpu_map__alloc()all calls seems to validate it.Like:
+++
b/tools/lib/perf/cpumap.c(quoted diff) +1
-1@@ -411,7 +411,7 @@ int perf_cpu_map__merge(struct perf_cpu_map **orig, struct perf_cpu_map *other) } tmp_len = __perf_cpu_map__nr(*orig) + __perf_cpu_map__nr(other); - tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); + tmp_cpus = calloc(tmp_len, sizeof(struct perf_cpu)); if (!tmp_cpus) return -ENOMEM;
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
⬢ [acme@toolbx perf-tools-next]$
And better, do the max size that the compiler is trying to help us catch?
Isn't it better to use
perf_cpu_map__nr. That should fix this problem.Maybe, have you tried it?
I have tried this method and it works.
tools/lib/perf/cpumap.c +1 -1
@@ -410,7 +410,7 @@ int perf_cpu_map__merge(struct perf_cpu_map **orig, struct perf_cpu_map *other)return 0 ; } - tmp_len =max (__perf_cpu_map__nr (*orig),__perf_cpu_map__nr (other)); + tmp_len =perf_cpu_map__nr (*orig) +perf_cpu_map__nr (other); tmp_cpus =malloc (tmp_len *sizeof (struct perf_cpu ));if (!tmp_cpus)return -ENOMEM ;
I will send a V2 with this change if this looks good.
Thanks
Likhitha.
Quoted 40 lines
Quoted 3 lines
One question I have, in
perf_cpu_map__nr, the function is returning 1 in case *cpus is NULL. Is it ok to do that? wouldn't it cause problems?Indeed this better be documented, as by just looking at:
int
perf_cpu_map__nr(const struct perf_cpu_map *cpus){ return cpus ? __perf_cpu_map__nr(cpus) : 1; }It really doesn't make much sense to say that a NULL map has one entry.
But the next functions are:
bool
perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map){ return map ? __perf_cpu_map__cpu(map, 0).cpu == -1 : true; }bool
perf_cpu_map__is_any_cpu_or_is_empty(const struct perf_cpu_map *map){ if (!map) return true; return __perf_cpu_map__nr(map) == 1 && __perf_cpu_map__cpu(map, 0).cpu == -1; }bool
perf_cpu_map__is_empty(const struct perf_cpu_map *map){ return map == NULL; }So it seems that a NULL cpu map means "any/all CPU) and a map with just one entry would have as its content "-1" that would mean "any/all CPU".
Ian did work on trying to simplify/clarify this, so maybe he can chime in :-)
- Arnaldo
Ian Rogers
reviewerQuoted 47 lines · Likhitha Korrapati
Hi Arnaldo,
Quoted 19 lines · Arnaldo Carvalho de Melo
Quoted 17 lines · Mukesh Kumar Chaurasiya
Quoted 16 lines · Arnaldo Carvalho de Melo
Maybe that
max()call inperf_cpu_map__intersect()somehow makes the compiler happy.And in
perf_cpu_map__alloc()all calls seems to validate it.Like:
+++
b/tools/lib/perf/cpumap.ctools/lib/perf/cpumap.c +1
-1@@ -411,7 +411,7 @@ int perf_cpu_map__merge(struct perf_cpu_map **orig, struct perf_cpu_map *other) } tmp_len =__perf_cpu_map__nr (*orig) +__perf_cpu_map__nr (other); - tmp_cpus =malloc (tmp_len *sizeof (struct perf_cpu )); + tmp_cpus =calloc (tmp_len,sizeof (struct perf_cpu ));if (!tmp_cpus)return -ENOMEM ;Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
⬢ [acme@toolbx perf-tools-next]$
And better, do the max size that the compiler is trying to help us catch?
Isn't it better to use
perf_cpu_map__nr. That should fix this problem.Maybe, have you tried it?
I have tried this method and it works.
tools/lib/perf/cpumap.c +1
-1@@ -410,7 +410,7 @@ int perf_cpu_map__merge(struct perf_cpu_map **orig, struct perf_cpu_map *other)return 0 ; } - tmp_len =max (__perf_cpu_map__nr (*orig),__perf_cpu_map__nr (other)); + tmp_len =perf_cpu_map__nr (*orig) +perf_cpu_map__nr (other); tmp_cpus =malloc (tmp_len *sizeof (struct perf_cpu ));if (!tmp_cpus)return -ENOMEM;
Quoted 1 line
I will send a V2 with this change if this looks good.
How is this different from the existing code:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/lib/perf/cpumap.c?h=perf-tools-next#n423
```
tmp_len = __perf_cpu_map__nr(*orig) + __perf_cpu_map__nr(other);
tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu));
if (!tmp_cpus)
return -ENOMEM;
```Thanks,
Ian
Quoted 44 lines
Thanks
Likhitha.Quoted 40 lines
Quoted 3 lines
One question I have, in
perf_cpu_map__nr, the function is returning 1 in case *cpus is NULL. Is it ok to do that? wouldn't it cause problems?Indeed this better be documented, as by just looking at:
int
perf_cpu_map__nr(const struct perf_cpu_map *cpus){ return cpus ? __perf_cpu_map__nr(cpus) : 1; }It really doesn't make much sense to say that a NULL map has one entry.
But the next functions are:
bool
perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map){ return map ? __perf_cpu_map__cpu(map, 0).cpu == -1 : true; }bool
perf_cpu_map__is_any_cpu_or_is_empty(const struct perf_cpu_map *map){ if (!map) return true; return __perf_cpu_map__nr(map) == 1 && __perf_cpu_map__cpu(map, 0).cpu == -1; }bool
perf_cpu_map__is_empty(const struct perf_cpu_map *map){ return map == NULL; }So it seems that a NULL cpu map means "any/all CPU) and a map with just one entry would have as its content "-1" that would mean "any/all CPU".
Ian did work on trying to simplify/clarify this, so maybe he can chime in :-)
- Arnaldo
Hi Ian,
Quoted 60 lines · Ian Rogers
Quoted 47 lines · Likhitha Korrapati
Hi Arnaldo,
Quoted 19 lines · Arnaldo Carvalho de Melo
Quoted 17 lines · Mukesh Kumar Chaurasiya
Quoted 16 lines · Arnaldo Carvalho de Melo
Maybe that
max()call inperf_cpu_map__intersect()somehow makes the compiler happy.And in
perf_cpu_map__alloc()all calls seems to validate it.Like:
+++
b/tools/lib/perf/cpumap.c(quoted diff) +1
-1@@ -411,7 +411,7 @@ int perf_cpu_map__merge(struct perf_cpu_map **orig, struct perf_cpu_map *other) } tmp_len = __perf_cpu_map__nr(*orig) + __perf_cpu_map__nr(other); - tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); + tmp_cpus = calloc(tmp_len, sizeof(struct perf_cpu)); if (!tmp_cpus) return -ENOMEM;Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
⬢ [acme@toolbx perf-tools-next]$
And better, do the max size that the compiler is trying to help us catch?
Isn't it better to use
perf_cpu_map__nr. That should fix this problem.Maybe, have you tried it?
I have tried this method and it works.
tools/lib/perf/cpumap.c +1
-1@@ -410,7 +410,7 @@ int perf_cpu_map__merge(struct perf_cpu_map **orig, struct perf_cpu_map *other)return 0 ; } - tmp_len =max (__perf_cpu_map__nr (*orig),__perf_cpu_map__nr (other)); + tmp_len =perf_cpu_map__nr (*orig) +perf_cpu_map__nr (other); tmp_cpus =malloc (tmp_len *sizeof (struct perf_cpu ));if (!tmp_cpus)return -ENOMEM;Quoted 1 line
I will send a V2 with this change if this looks good.
How is this different from the existing code:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/lib/perf/cpumap.c?h=perf-tools-next#n423``` tmp_len = __perf_cpu_map__nr(*orig) + __perf_cpu_map__nr(other); tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); if (!tmp_cpus) return -ENOMEM; ```
Quoted 2 lines
Thanks,
Ian
I gave the wrong diff. Here is the corrected diff.
tools/lib/perf/cpumap.c +1 -1
@@ -410,7 +410,7 @@ int perf_cpu_map__merge(struct perf_cpu_map **orig, struct perf_cpu_map *other)return 0 ; } - tmp_len =__perf_cpu_map__nr (*orig) +__perf_cpu_map__nr (other); + tmp_len =perf_cpu_map__nr (*orig) +perf_cpu_map__nr (other); tmp_cpus =malloc (tmp_len *sizeof (struct perf_cpu ));if (!tmp_cpus)return -ENOMEM ;
I am using perf_cpu_map__nr instead of __perf_cpu_map__nr.
Thanks,
Likhitha.
Quoted 46 lines
Quoted 45 lines
Thanks
Likhitha.Quoted 40 lines
Quoted 3 lines
One question I have, in
perf_cpu_map__nr, the function is returning 1 in case *cpus is NULL. Is it ok to do that? wouldn't it cause problems?Indeed this better be documented, as by just looking at:
int
perf_cpu_map__nr(const struct perf_cpu_map *cpus){ return cpus ? __perf_cpu_map__nr(cpus) : 1; }It really doesn't make much sense to say that a NULL map has one entry.
But the next functions are:
bool
perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map){ return map ? __perf_cpu_map__cpu(map, 0).cpu == -1 : true; }bool
perf_cpu_map__is_any_cpu_or_is_empty(const struct perf_cpu_map *map){ if (!map) return true; return __perf_cpu_map__nr(map) == 1 && __perf_cpu_map__cpu(map, 0).cpu == -1; }bool
perf_cpu_map__is_empty(const struct perf_cpu_map *map){ return map == NULL; }So it seems that a NULL cpu map means "any/all CPU) and a map with just one entry would have as its content "-1" that would mean "any/all CPU".
Ian did work on trying to simplify/clarify this, so maybe he can chime in :-)
- Arnaldo
Ian Rogers
reviewerQuoted 83 lines · Likhitha Korrapati
Hi Ian,
Quoted 60 lines · Ian Rogers
Quoted 47 lines · Likhitha Korrapati
Hi Arnaldo,
Quoted 19 lines · Arnaldo Carvalho de Melo
Quoted 17 lines · Mukesh Kumar Chaurasiya
Quoted 16 lines · Arnaldo Carvalho de Melo
Maybe that
max()call inperf_cpu_map__intersect()somehow makes the compiler happy.And in
perf_cpu_map__alloc()all calls seems to validate it.Like:
+++
b/tools/lib/perf/cpumap.ctools/lib/perf/cpumap.c +1
-1@@ -411,7 +411,7 @@ int perf_cpu_map__merge(struct perf_cpu_map **orig, struct perf_cpu_map *other) } tmp_len =__perf_cpu_map__nr (*orig) +__perf_cpu_map__nr (other); - tmp_cpus =malloc (tmp_len *sizeof (struct perf_cpu )); + tmp_cpus =calloc (tmp_len,sizeof (struct perf_cpu ));if (!tmp_cpus)return -ENOMEM ;Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
⬢ [acme@toolbx perf-tools-next]$
And better, do the max size that the compiler is trying to help us catch?
Isn't it better to use
perf_cpu_map__nr. That should fix this problem.Maybe, have you tried it?
I have tried this method and it works.
tools/lib/perf/cpumap.c +1
-1@@ -410,7 +410,7 @@ int perf_cpu_map__merge(struct perf_cpu_map **orig, struct perf_cpu_map *other)return 0 ; } - tmp_len =max (__perf_cpu_map__nr (*orig),__perf_cpu_map__nr (other)); + tmp_len =perf_cpu_map__nr (*orig) +perf_cpu_map__nr (other); tmp_cpus =malloc (tmp_len *sizeof (struct perf_cpu ));if (!tmp_cpus)return -ENOMEM;Quoted 1 line
I will send a V2 with this change if this looks good.
How is this different from the existing code:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/lib/perf/cpumap.c?h=perf-tools-next#n423``` tmp_len = __perf_cpu_map__nr(*orig) + __perf_cpu_map__nr(other); tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); if (!tmp_cpus) return -ENOMEM; ```Quoted 2 lines
Thanks,
IanI gave the wrong diff. Here is the corrected diff.
tools/lib/perf/cpumap.c +1
-1@@ -410,7 +410,7 @@ int perf_cpu_map__merge(struct perf_cpu_map **orig, struct perf_cpu_map *other)return 0 ; } - tmp_len =__perf_cpu_map__nr (*orig) +__perf_cpu_map__nr (other); + tmp_len =perf_cpu_map__nr (*orig) +perf_cpu_map__nr (other); tmp_cpus =malloc (tmp_len *sizeof (struct perf_cpu ));if (!tmp_cpus)return -ENOMEM;
Quoted 1 line
I am using
perf_cpu_map__nrinstead of__perf_cpu_map__nr.
Ok, why is that a fix? The function declarations are near identical and perf_cpu_map__nr is implemented in terms of __perf_cpu_map__nr:
```
static int __perf_cpu_map__nr(const struct perf_cpu_map *cpus)
{
return RC_CHK_ACCESS(cpus)->nr;
}
int perf_cpu_map__nr(const struct perf_cpu_map *cpus)
{
return cpus ? __perf_cpu_map__nr(cpus) : 1;
}
```My guess is that being static allows all of the code to be analyzed in the compilation unit and thereby create the warning/error, your change is just defeating the analysis. The analysis could easily kick in again for Link Time Optimization. I'd prefer making these __nr functions return unsigned int or size_t over changes like this.
Thanks,
Ian
Quoted 50 lines
Thanks,
Likhitha.Quoted 46 lines
Quoted 45 lines
Thanks
Likhitha.Quoted 40 lines
Quoted 3 lines
One question I have, in
perf_cpu_map__nr, the function is returning 1 in case *cpus is NULL. Is it ok to do that? wouldn't it cause problems?Indeed this better be documented, as by just looking at:
int
perf_cpu_map__nr(const struct perf_cpu_map *cpus){ return cpus ? __perf_cpu_map__nr(cpus) : 1; }It really doesn't make much sense to say that a NULL map has one entry.
But the next functions are:
bool
perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map){ return map ? __perf_cpu_map__cpu(map, 0).cpu == -1 : true; }bool
perf_cpu_map__is_any_cpu_or_is_empty(const struct perf_cpu_map *map){ if (!map) return true; return __perf_cpu_map__nr(map) == 1 && __perf_cpu_map__cpu(map, 0).cpu == -1; }bool
perf_cpu_map__is_empty(const struct perf_cpu_map *map){ return map == NULL; }So it seems that a NULL cpu map means "any/all CPU) and a map with just one entry would have as its content "-1" that would mean "any/all CPU".
Ian did work on trying to simplify/clarify this, so maybe he can chime in :-)
- Arnaldo
Hi Arnaldo,
Quoted 132 lines · Arnaldo Carvalho de Melo
Quoted 39 lines · Athira Rajeev
Quoted 38 lines · Madhavan Srinivasan
Quoted 36 lines · Venkat Rao Bagalkote
Quoted 35 lines · Athira Rajeev
Quoted 31 lines · Likhitha Korrapati
perf build break observed when using gcc 13-3 (FC39 ppc64le) with the following error.
cpumap.c: In function 'perf_cpu_map__merge':
cpumap.c:414:20: error: argument 1 range [18446744069414584320, 18446744073709551614] exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=]414 | tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from cpumap.c:4: /usr/include/stdlib.h:672:14: note: in a call to allocation function 'malloc' declared here 672 | extern void *malloc (size_t __size) __THROW __attribute_malloc__ | ^~~~~~cc1: all warnings being treated as errors
Error happens to be only in gcc13-3 and not in latest gcc 14. Even though git-bisect pointed bad commit as:
'commitf5b07010c13c("libperf: Don't remove -g whenEXTRA_CFLAGSare used")', issue is withtmp_lenbeing "int". It holds number of cpus and making it "unsigned int" fixes the issues.After the fix:
CC util/pmu-flex.o CC util/expr-flex.o LD util/perf-util-in.o LD perf-util-in.o AR libperf-util.a LINK perf GEN python/perf.cpython-312-powerpc64le-linux-gnu.soQuoted 1 line
Signed-off-by Likhitha Korrapati <[email protected]>Looks good to me
Reviewed-by Athira Rajeev <[email protected]>Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Tested this patch on perf-tools-next repo, and this patch fixes the issue.
Tested-by Venkat Rao Bagalkote <[email protected]>Arnaldo, Namhyung,
can you consider pulling this fix? since it is breaking the build in gcc13-3 or if you have any comments do let us know.
This isn't the only place in that file where this pattern exists:
⬢ [acme@toolbx perf-tools-next]$ grep malloc
tools/lib/perf/cpumap.ccpus = malloc(sizeof(*cpus) + sizeof(struct perf_cpu) * nr_cpus); tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu));⬢ [acme@toolbx perf-tools-next]$
struct
perf_cpu_map*perf_cpu_map__alloc(intnr_cpus){ RC_STRUCT(perf_cpu_map) *cpus; struct perf_cpu_map *result; if (nr_cpus == 0) return NULL; cpus = malloc(sizeof(*cpus) + sizeof(struct perf_cpu) * nr_cpus);int
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; struct perf_cpu_map *merged; if (perf_cpu_map__is_subset(*orig, other)) return 0; if (perf_cpu_map__is_subset(other, *orig)) { perf_cpu_map__put(*orig); *orig = perf_cpu_map__get(other); return 0; } tmp_len = __perf_cpu_map__nr(*orig) + __perf_cpu_map__nr(other); tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu));struct
perf_cpu_map*perf_cpu_map__intersect(structperf_cpu_map*orig,
structperf_cpu_map*other){ struct perf_cpu *tmp_cpus; int tmp_len; int i, j, k; struct perf_cpu_map *merged = NULL; if (perf_cpu_map__is_subset(other, orig)) return perf_cpu_map__get(orig); if (perf_cpu_map__is_subset(orig, other)) return perf_cpu_map__get(other); tmp_len = max(__perf_cpu_map__nr(orig), __perf_cpu_map__nr(other)); tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu));I'm trying to figure out why its only in
perf_cpu_map__merge()that this triggers :-\Maybe that
max()call inperf_cpu_map__intersect()somehow makes the compiler happy.And in
perf_cpu_map__alloc()all calls seems to validate it.But wouldn't turning this into a
calloc()be better?Like:
tools/lib/perf/cpumap.c +1
-1@@ -411,7 +411,7 @@ int perf_cpu_map__merge(struct perf_cpu_map **orig, struct perf_cpu_map *other) } tmp_len =__perf_cpu_map__nr (*orig) +__perf_cpu_map__nr (other); - tmp_cpus =malloc (tmp_len *sizeof (struct perf_cpu )); + tmp_cpus =calloc (tmp_len,sizeof (struct perf_cpu ));if (!tmp_cpus)return -ENOMEM ;⬢ [acme@toolbx perf-tools-next]$
Quoted 4 lines
And better, do the max size that the compiler is trying to help us catch?
- Arnaldo
I am not sure if using max() is right incase of perf_cpu_map_merge as this is a merge functionality. As this is summation we get a value greater than the max of orig and other cpus. And this might be correct in perf_cpu_map__intersect() but will cause issues in perf_cpu_map__marge(). Can you please eloborate if you meant this or there is something else I missed.
I tried the following:
int perf_cpu_map__nr(const struct perf_cpu_map *cpus)
{
return cpus ? __perf_cpu_map__nr(cpus) : 1;
}static int __perf_cpu_map__nr(const struct perf_cpu_map *cpus)
{
return RC_CHK_ACCESS(cpus)->nr;
}This got introduced via commit 7d1b529f164d33ad4514b272bcec65036873d717 where it assumes cpu map as non-null.
And we are checking this non-null in perf_cpu_map__merge()
if (perf_cpu_map__is_subset(*orig, other))
return 0;
if (perf_cpu_map__is_subset(other, *orig)) {
perf_cpu_map__put(*orig);
*orig = perf_cpu_map__get(other);
return 0;
}Using perf_cpu_map__nr instead of __perf_cpu_map__nr and this works as it has a check.
tools/lib/perf/cpumap.c +1 -1
@@ -410,7 +410,7 @@ int perf_cpu_map__merge(struct perf_cpu_map **orig, struct perf_cpu_map *other)return 0 ; } - tmp_len =max (__perf_cpu_map__nr (*orig),__perf_cpu_map__nr (other)); + tmp_len =perf_cpu_map__nr (*orig) +perf_cpu_map__nr (other); tmp_cpus =malloc (tmp_len *sizeof (struct perf_cpu ));if (!tmp_cpus)return -ENOMEM ;
Regards,
Likhitha.
Quoted 39 lines · Likhitha Korrapati
perf build break observed when using gcc 13-3 (FC39 ppc64le) with the following error.
cpumap.c: In function 'perf_cpu_map__merge':
cpumap.c:414:20: error: argument 1 range [18446744069414584320, 18446744073709551614] exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=]414 | tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from cpumap.c:4: /usr/include/stdlib.h:672:14: note: in a call to allocation function 'malloc' declared here 672 | extern void *malloc (size_t __size) __THROW __attribute_malloc__ | ^~~~~~cc1: all warnings being treated as errors
Error happens to be only in gcc13-3 and not in latest gcc 14. Even though git-bisect pointed bad commit as:
'commitf5b07010c13c("libperf: Don't remove -g whenEXTRA_CFLAGSare used")', issue is withtmp_lenbeing "int". It holds number of cpus and making it "unsigned int" fixes the issues.After the fix:
CC util/pmu-flex.o CC util/expr-flex.o LD util/perf-util-in.o LD perf-util-in.o AR libperf-util.a LINK perf GEN python/perf.cpython-312-powerpc64le-linux-gnu.soSigned-off-by Likhitha Korrapati <[email protected]>--- tools/lib/perf/cpumap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/lib/perf/cpumap.c +0
-0
tools/lib/perf/cpumap.c bool perf_cpu_map__is_subset(const struct perf_cpu_map *a, const struct perf_cpu · line 404
Quoted 8 lines
tools/lib/perf/cpumap.c +1
-1@@ -398,7 +398,7 @@ bool perf_cpu_map__is_subset(const struct perf_cpu_map *a, const struct perf_cpuint perf_cpu_map__merge (struct perf_cpu_map **orig,struct perf_cpu_map *other) {struct perf_cpu *tmp_cpus; -int tmp_len; +unsigned int tmp_len;int i, j, k;struct perf_cpu_map *merged;
LGTM
Quoted 3 lines
Signature · 1 line
2.43.5