public inbox for linux-perf-users@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 4/4] perf symbol: Add missing libgen.h include to get basename() prototype
  2026-04-01 21:01 [PATCHES perf-tools-next 0/4] Cleanups and a fix Arnaldo Carvalho de Melo
@ 2026-04-01 21:02 ` Arnaldo Carvalho de Melo
  2026-04-01 21:08   ` Ian Rogers
  0 siblings, 1 reply; 14+ messages in thread
From: Arnaldo Carvalho de Melo @ 2026-04-01 21:02 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Ingo Molnar, Thomas Gleixner, James Clark, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Kan Liang, Clark Williams, linux-kernel,
	linux-perf-users, Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <acme@redhat.com>

This works in some places because there are tricks to prefer a GNU
version instead of the XPG version, but started breaking in some musl
libc systems due to some unrelated change that made libgen.h stop being
included by chance.

Since symbol.h is using basename(), make it include libgen.h.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/tests/make    | 2 +-
 tools/perf/util/symbol.h | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/tools/perf/tests/make b/tools/perf/tests/make
index 6587dc326d1b272e..e12ff6397077806e 100644
--- a/tools/perf/tests/make
+++ b/tools/perf/tests/make
@@ -148,7 +148,7 @@ run += make_extra_tests
 run += make_no_jevents
 run += make_jevents_all
 run += make_no_bpf_skel
-run += make_gen_vmlinux_h
+#run += make_gen_vmlinux_h
 run += make_libperl
 run += make_no_libpython
 run += make_no_scripts
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index c67814d6d6d6f64a..b37ecc2e90f77efe 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -10,6 +10,7 @@
 #include <linux/rbtree.h>
 #include <stdio.h>
 #include <errno.h>
+#include <libgen.h>
 #include "addr_location.h"
 #include "path.h"
 #include "symbol_conf.h"
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCH 4/4] perf symbol: Add missing libgen.h include to get basename() prototype
  2026-04-01 21:02 ` [PATCH 4/4] perf symbol: Add missing libgen.h include to get basename() prototype Arnaldo Carvalho de Melo
@ 2026-04-01 21:08   ` Ian Rogers
  2026-04-01 21:41     ` Arnaldo Melo
  0 siblings, 1 reply; 14+ messages in thread
From: Ian Rogers @ 2026-04-01 21:08 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Namhyung Kim, Ingo Molnar, Thomas Gleixner, James Clark,
	Jiri Olsa, Adrian Hunter, Kan Liang, Clark Williams, linux-kernel,
	linux-perf-users, Arnaldo Carvalho de Melo

On Wed, Apr 1, 2026 at 2:02 PM Arnaldo Carvalho de Melo <acme@kernel.org> wrote:
>
> From: Arnaldo Carvalho de Melo <acme@redhat.com>
>
> This works in some places because there are tricks to prefer a GNU
> version instead of the XPG version, but started breaking in some musl
> libc systems due to some unrelated change that made libgen.h stop being
> included by chance.
>
> Since symbol.h is using basename(), make it include libgen.h.
>
> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
> ---
>  tools/perf/tests/make    | 2 +-
>  tools/perf/util/symbol.h | 1 +
>  2 files changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/tools/perf/tests/make b/tools/perf/tests/make
> index 6587dc326d1b272e..e12ff6397077806e 100644
> --- a/tools/perf/tests/make
> +++ b/tools/perf/tests/make
> @@ -148,7 +148,7 @@ run += make_extra_tests
>  run += make_no_jevents
>  run += make_jevents_all
>  run += make_no_bpf_skel
> -run += make_gen_vmlinux_h
> +#run += make_gen_vmlinux_h

This looks like something removed during testing that you meant to put back.

Thanks,
Ian

>  run += make_libperl
>  run += make_no_libpython
>  run += make_no_scripts
> diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
> index c67814d6d6d6f64a..b37ecc2e90f77efe 100644
> --- a/tools/perf/util/symbol.h
> +++ b/tools/perf/util/symbol.h
> @@ -10,6 +10,7 @@
>  #include <linux/rbtree.h>
>  #include <stdio.h>
>  #include <errno.h>
> +#include <libgen.h>
>  #include "addr_location.h"
>  #include "path.h"
>  #include "symbol_conf.h"
> --
> 2.53.0
>

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 4/4] perf symbol: Add missing libgen.h include to get basename() prototype
  2026-04-01 21:08   ` Ian Rogers
@ 2026-04-01 21:41     ` Arnaldo Melo
  2026-04-01 23:10       ` Namhyung Kim
  0 siblings, 1 reply; 14+ messages in thread
From: Arnaldo Melo @ 2026-04-01 21:41 UTC (permalink / raw)
  To: Ian Rogers, Arnaldo Carvalho de Melo
  Cc: Namhyung Kim, Ingo Molnar, Thomas Gleixner, James Clark,
	Jiri Olsa, Adrian Hunter, Kan Liang, Clark Williams, linux-kernel,
	linux-perf-users, Arnaldo Carvalho de Melo



On April 1, 2026 6:08:16 PM GMT-03:00, Ian Rogers <irogers@google.com> wrote:
>On Wed, Apr 1, 2026 at 2:02 PM Arnaldo Carvalho de Melo <acme@kernel.org> wrote:
>>
>> From: Arnaldo Carvalho de Melo <acme@redhat.com>
>>
>> This works in some places because there are tricks to prefer a GNU
>> version instead of the XPG version, but started breaking in some musl
>> libc systems due to some unrelated change that made libgen.h stop being
>> included by chance.
>>
>> Since symbol.h is using basename(), make it include libgen.h.
>>
>> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
>> ---
>>  tools/perf/tests/make    | 2 +-
>>  tools/perf/util/symbol.h | 1 +
>>  2 files changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/tools/perf/tests/make b/tools/perf/tests/make
>> index 6587dc326d1b272e..e12ff6397077806e 100644
>> --- a/tools/perf/tests/make
>> +++ b/tools/perf/tests/make
>> @@ -148,7 +148,7 @@ run += make_extra_tests
>>  run += make_no_jevents
>>  run += make_jevents_all
>>  run += make_no_bpf_skel
>> -run += make_gen_vmlinux_h
>> +#run += make_gen_vmlinux_h
>
>This looks like something removed during testing that you meant to put back.


Yeah, unrelated, I'll send a v2 removing the header dir move that was already merged, as you noted.

This gen vmlinux test is failing and I still need to figure out, seems like a newer clang is complaining about some struct forward declarations I'm vmlinux.h.

- Arnaldo 

>Thanks,
>Ian
>
>>  run += make_libperl
>>  run += make_no_libpython
>>  run += make_no_scripts
>> diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
>> index c67814d6d6d6f64a..b37ecc2e90f77efe 100644
>> --- a/tools/perf/util/symbol.h
>> +++ b/tools/perf/util/symbol.h
>> @@ -10,6 +10,7 @@
>>  #include <linux/rbtree.h>
>>  #include <stdio.h>
>>  #include <errno.h>
>> +#include <libgen.h>
>>  #include "addr_location.h"
>>  #include "path.h"
>>  #include "symbol_conf.h"
>> --
>> 2.53.0
>>
>

- Arnaldo

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCHES v2 perf-tools-next 0/4] Cleanups and a fix
@ 2026-04-01 21:53 Arnaldo Carvalho de Melo
  2026-04-01 21:53 ` [PATCH 1/4] perf tools: Make more global variables static Arnaldo Carvalho de Melo
                   ` (3 more replies)
  0 siblings, 4 replies; 14+ messages in thread
From: Arnaldo Carvalho de Melo @ 2026-04-01 21:53 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Ingo Molnar, Thomas Gleixner, James Clark, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Kan Liang, Clark Williams, linux-kernel,
	linux-perf-users, Arnaldo Carvalho de Melo

Hi,

        Here are some cleanups, a refresh for a patch Ian sent and fell
thru the cracks, using calloc where applicable, constify some arrays and
add a missing header that was making the build fail on some musl
systems.

Thanks a lot,

- Arnaldo

v2: remove the commenting of gen_vmlinux in tools/perf/tests/make as
noticed by Ian Rogers.

Arnaldo Carvalho de Melo (3):
  perf bench: Constify tables
  perf tools: Use calloc() were applicable
  perf symbol: Add missing libgen.h include to get basename() prototype

Ian Rogers (1):
  perf tools: Make more global variables static

 tools/perf/arch/arm/util/auxtrace.c           |   6 +-
 tools/perf/arch/common.c                      |  22 ++--
 tools/perf/arch/powerpc/util/auxtrace.c       |   1 +
 tools/perf/arch/sh/include/dwarf-regs-table.h |   2 +-
 tools/perf/arch/x86/tests/amd-ibs-period.c    |   3 +-
 tools/perf/arch/x86/tests/dwarf-unwind.c      |  11 +-
 tools/perf/arch/x86/util/pmu.c                |   1 -
 tools/perf/bench/breakpoint.c                 |   4 +-
 tools/perf/bench/mem-functions.c              |   2 +-
 tools/perf/bench/numa.c                       |  15 +--
 tools/perf/bench/sched-messaging.c            |   2 +-
 tools/perf/bench/uprobe.c                     |   2 +-
 tools/perf/builtin-annotate.c                 |   1 -
 tools/perf/builtin-bench.c                    |  42 +++----
 tools/perf/builtin-c2c.c                      |  13 ++-
 tools/perf/builtin-config.c                   |   2 +-
 tools/perf/builtin-data.c                     |   8 +-
 tools/perf/builtin-diff.c                     |   6 +-
 tools/perf/builtin-ftrace.c                   |   1 +
 tools/perf/builtin-kmem.c                     |   2 +-
 tools/perf/builtin-kwork.c                    |  14 +--
 tools/perf/builtin-record.c                   |  10 +-
 tools/perf/builtin-sched.c                    |   6 +-
 tools/perf/builtin-script.c                   |  12 +-
 tools/perf/builtin-stat.c                     |   2 +-
 tools/perf/builtin-top.c                      |   5 +-
 tools/perf/builtin-trace.c                    |   4 +-
 tools/perf/jvmti/libjvmti.c                   |   5 +-
 tools/perf/tests/bp_signal.c                  |   2 +-
 tools/perf/tests/code-reading.c               |   1 +
 tools/perf/tests/dso-data.c                   |   2 +-
 tools/perf/tests/thread-map.c                 |   1 -
 tools/perf/tests/wp.c                         |   6 +-
 tools/perf/util/annotate-arch/annotate-x86.c  |   1 +
 tools/perf/util/block-range.c                 |   2 +-
 tools/perf/util/bpf-event.c                   |   2 +-
 tools/perf/util/bpf_counter.c                 |   4 +-
 tools/perf/util/bpf_counter_cgroup.c          |   1 -
 tools/perf/util/bpf_off_cpu.c                 |   2 +-
 tools/perf/util/data-convert-bt.c             |   2 +-
 tools/perf/util/data.c                        |   2 +-
 tools/perf/util/db-export.c                   |   1 -
 tools/perf/util/debug.c                       |   2 +-
 tools/perf/util/debuginfo.c                   |  19 ++--
 tools/perf/util/disasm.c                      |   1 +
 tools/perf/util/event.c                       |   1 -
 tools/perf/util/evlist.c                      |   3 +-
 tools/perf/util/header.c                      |  18 +--
 tools/perf/util/hist.c                        |   2 +-
 tools/perf/util/mem2node.c                    |   2 +-
 tools/perf/util/pmus.c                        |   2 +-
 tools/perf/util/powerpc-vpadtl.c              |   1 +
 tools/perf/util/probe-event.c                 |  17 ++-
 tools/perf/util/probe-file.c                  |   2 +-
 tools/perf/util/probe-finder.c                |   8 +-
 tools/perf/util/session.c                     |   2 +-
 tools/perf/util/sort.c                        | 104 +++++++++---------
 tools/perf/util/srcline.c                     |   1 +
 tools/perf/util/stat-shadow.c                 |   1 -
 tools/perf/util/symbol.h                      |   1 +
 tools/perf/util/trace-event-scripting.c       |  98 +++++++++--------
 tools/perf/util/unwind-libunwind-local.c      |   1 -
 tools/perf/util/util.c                        |   2 -
 tools/perf/util/values.c                      |   8 +-
 64 files changed, 254 insertions(+), 275 deletions(-)

-- 
2.53.0


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH 1/4] perf tools: Make more global variables static
  2026-04-01 21:53 [PATCHES v2 perf-tools-next 0/4] Cleanups and a fix Arnaldo Carvalho de Melo
@ 2026-04-01 21:53 ` Arnaldo Carvalho de Melo
  2026-04-01 21:53 ` [PATCH 2/4] perf bench: Constify tables Arnaldo Carvalho de Melo
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 14+ messages in thread
From: Arnaldo Carvalho de Melo @ 2026-04-01 21:53 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Ingo Molnar, Thomas Gleixner, James Clark, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Kan Liang, Clark Williams, linux-kernel,
	linux-perf-users, Ankur Arora, Albert Ou, Alexander Shishkin,
	Alexandre Ghiti, Athira Rajeev, Guo Ren, Howard Chu, Ingo Molnar,
	Leo Yan, Masami Hiramatsu, Palmer Dabbelt, Paul Walmsley,
	Peter Zijlstra, Yujie Liu, Arnaldo Carvalho de Melo

From: Ian Rogers <irogers@google.com>

`make check` will run sparse on the perf code base. A frequent warning
is "warning: symbol '...' was not declared. Should it be static?" Go
through and make global definitions without declarations static.

In some cases it is deliberate due to dlsym accessing the symbol, this
change doesn't clean up the missing declarations for perf test suites.

Sometimes things can opportunistically be made const.

Making somethings static exposed unused functions warnings, so
restructuring of ifdefs was necessary for that.

These changes reduce the size of the perf binary by 568 bytes.

Signed-off-by: Ian Rogers <irogers@google.com>
Acked-by: Ankur Arora <ankur.a.arora@oracle.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexandre Ghiti <alex@ghiti.fr>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Guo Ren <guoren@kernel.org>
Cc: Howard Chu <howardchu95@gmail.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@linaro.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Leo Yan <leo.yan@arm.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Paul Walmsley <pjw@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Yujie Liu <yujie.liu@intel.com>
[ Refreshed the patch, the original one fell thru the cracks, updated the size reduction ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/arch/common.c                      |  22 ++--
 tools/perf/arch/sh/include/dwarf-regs-table.h |   2 +-
 tools/perf/bench/breakpoint.c                 |   4 +-
 tools/perf/bench/mem-functions.c              |   2 +-
 tools/perf/bench/numa.c                       |   2 +-
 tools/perf/bench/uprobe.c                     |   2 +-
 tools/perf/builtin-c2c.c                      |   7 +-
 tools/perf/builtin-config.c                   |   2 +-
 tools/perf/builtin-data.c                     |   8 +-
 tools/perf/builtin-diff.c                     |   4 +-
 tools/perf/builtin-kmem.c                     |   2 +-
 tools/perf/builtin-kwork.c                    |  12 +-
 tools/perf/builtin-script.c                   |   2 +-
 tools/perf/builtin-top.c                      |   5 +-
 tools/perf/tests/bp_signal.c                  |   2 +-
 tools/perf/tests/dso-data.c                   |   2 +-
 tools/perf/tests/wp.c                         |   6 +-
 tools/perf/util/block-range.c                 |   2 +-
 tools/perf/util/bpf_counter.c                 |   4 +-
 tools/perf/util/bpf_off_cpu.c                 |   2 +-
 tools/perf/util/debug.c                       |   2 +-
 tools/perf/util/debuginfo.c                   |  19 ++--
 tools/perf/util/sort.c                        | 104 +++++++++---------
 tools/perf/util/trace-event-scripting.c       |  98 +++++++++--------
 tools/perf/util/util.c                        |   2 -
 25 files changed, 159 insertions(+), 160 deletions(-)

diff --git a/tools/perf/arch/common.c b/tools/perf/arch/common.c
index 4908d54dd33b2733..21836f70f231e42d 100644
--- a/tools/perf/arch/common.c
+++ b/tools/perf/arch/common.c
@@ -9,14 +9,14 @@
 #include "../util/debug.h"
 #include <linux/zalloc.h>
 
-const char *const arc_triplets[] = {
+static const char *const arc_triplets[] = {
 	"arc-linux-",
 	"arc-snps-linux-uclibc-",
 	"arc-snps-linux-gnu-",
 	NULL
 };
 
-const char *const arm_triplets[] = {
+static const char *const arm_triplets[] = {
 	"arm-eabi-",
 	"arm-linux-androideabi-",
 	"arm-unknown-linux-",
@@ -28,13 +28,13 @@ const char *const arm_triplets[] = {
 	NULL
 };
 
-const char *const arm64_triplets[] = {
+static const char *const arm64_triplets[] = {
 	"aarch64-linux-android-",
 	"aarch64-linux-gnu-",
 	NULL
 };
 
-const char *const powerpc_triplets[] = {
+static const char *const powerpc_triplets[] = {
 	"powerpc-unknown-linux-gnu-",
 	"powerpc-linux-gnu-",
 	"powerpc64-unknown-linux-gnu-",
@@ -43,40 +43,40 @@ const char *const powerpc_triplets[] = {
 	NULL
 };
 
-const char *const riscv32_triplets[] = {
+static const char *const riscv32_triplets[] = {
 	"riscv32-unknown-linux-gnu-",
 	"riscv32-linux-android-",
 	"riscv32-linux-gnu-",
 	NULL
 };
 
-const char *const riscv64_triplets[] = {
+static const char *const riscv64_triplets[] = {
 	"riscv64-unknown-linux-gnu-",
 	"riscv64-linux-android-",
 	"riscv64-linux-gnu-",
 	NULL
 };
 
-const char *const s390_triplets[] = {
+static const char *const s390_triplets[] = {
 	"s390-ibm-linux-",
 	"s390x-linux-gnu-",
 	NULL
 };
 
-const char *const sh_triplets[] = {
+static const char *const sh_triplets[] = {
 	"sh-unknown-linux-gnu-",
 	"sh-linux-gnu-",
 	NULL
 };
 
-const char *const sparc_triplets[] = {
+static const char *const sparc_triplets[] = {
 	"sparc-unknown-linux-gnu-",
 	"sparc64-unknown-linux-gnu-",
 	"sparc64-linux-gnu-",
 	NULL
 };
 
-const char *const x86_triplets[] = {
+static const char *const x86_triplets[] = {
 	"x86_64-pc-linux-gnu-",
 	"x86_64-unknown-linux-gnu-",
 	"i686-pc-linux-gnu-",
@@ -90,7 +90,7 @@ const char *const x86_triplets[] = {
 	NULL
 };
 
-const char *const mips_triplets[] = {
+static const char *const mips_triplets[] = {
 	"mips-unknown-linux-gnu-",
 	"mipsel-linux-android-",
 	"mips-linux-gnu-",
diff --git a/tools/perf/arch/sh/include/dwarf-regs-table.h b/tools/perf/arch/sh/include/dwarf-regs-table.h
index 900e6961997091c7..b5974a090fb44a79 100644
--- a/tools/perf/arch/sh/include/dwarf-regs-table.h
+++ b/tools/perf/arch/sh/include/dwarf-regs-table.h
@@ -2,7 +2,7 @@
 #ifdef DEFINE_DWARF_REGSTR_TABLE
 /* This is included in perf/util/dwarf-regs.c */
 
-const char * const sh_regstr_tbl[] = {
+static const char * const sh_regstr_tbl[] = {
 	"r0",
 	"r1",
 	"r2",
diff --git a/tools/perf/bench/breakpoint.c b/tools/perf/bench/breakpoint.c
index dfd18f5db97dbc7e..1b7cd4481bd224aa 100644
--- a/tools/perf/bench/breakpoint.c
+++ b/tools/perf/bench/breakpoint.c
@@ -16,7 +16,7 @@
 #include "bench.h"
 #include "futex.h"
 
-struct {
+static struct {
 	unsigned int nbreakpoints;
 	unsigned int nparallel;
 	unsigned int nthreads;
@@ -173,7 +173,7 @@ int bench_breakpoint_thread(int argc, const char **argv)
 	return 0;
 }
 
-struct {
+static struct {
 	unsigned int npassive;
 	unsigned int nactive;
 } enable_params = {
diff --git a/tools/perf/bench/mem-functions.c b/tools/perf/bench/mem-functions.c
index f5ab41bb85bf67a0..5ede528539535b6a 100644
--- a/tools/perf/bench/mem-functions.c
+++ b/tools/perf/bench/mem-functions.c
@@ -399,7 +399,7 @@ static void mem_free(struct bench_mem_info *info __maybe_unused,
 	*dst = *src = NULL;
 }
 
-struct function memcpy_functions[] = {
+static struct function memcpy_functions[] = {
 	{ .name		= "default",
 	  .desc		= "Default memcpy() provided by glibc",
 	  .fn.init	= mem_alloc,
diff --git a/tools/perf/bench/numa.c b/tools/perf/bench/numa.c
index 19be2aaf4dc0c07e..6588a9b0b15aec6d 100644
--- a/tools/perf/bench/numa.c
+++ b/tools/perf/bench/numa.c
@@ -166,7 +166,7 @@ static struct global_info	*g = NULL;
 static int parse_cpus_opt(const struct option *opt, const char *arg, int unset);
 static int parse_nodes_opt(const struct option *opt, const char *arg, int unset);
 
-struct params p0;
+static struct params p0;
 
 static const struct option options[] = {
 	OPT_INTEGER('p', "nr_proc"	, &p0.nr_proc,		"number of processes"),
diff --git a/tools/perf/bench/uprobe.c b/tools/perf/bench/uprobe.c
index c4dac868f1eea401..89697ff788ef3035 100644
--- a/tools/perf/bench/uprobe.c
+++ b/tools/perf/bench/uprobe.c
@@ -58,7 +58,7 @@ static const char * const bench_uprobe_usage[] = {
 		goto cleanup; \
 	}
 
-struct bench_uprobe_bpf *skel;
+static struct bench_uprobe_bpf *skel;
 
 static int bench_uprobe__setup_bpf_skel(enum bench_uprobe bench)
 {
diff --git a/tools/perf/builtin-c2c.c b/tools/perf/builtin-c2c.c
index e60eea62c2fc76d6..3ce5f0adec2f31c2 100644
--- a/tools/perf/builtin-c2c.c
+++ b/tools/perf/builtin-c2c.c
@@ -2892,9 +2892,10 @@ static int ui_quirks(void)
 
 #define CALLCHAIN_DEFAULT_OPT  "graph,0.5,caller,function,percent"
 
-const char callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
-				CALLCHAIN_REPORT_HELP
-				"\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
+static const char callchain_help[] =
+	"Display call graph (stack chain/backtrace):\n\n"
+	CALLCHAIN_REPORT_HELP
+	"\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
 
 static int
 parse_callchain_opt(const struct option *opt, const char *arg, int unset)
diff --git a/tools/perf/builtin-config.c b/tools/perf/builtin-config.c
index 45b5312fbe8370e8..237600643bbd6f33 100644
--- a/tools/perf/builtin-config.c
+++ b/tools/perf/builtin-config.c
@@ -23,7 +23,7 @@ static const char * const config_usage[] = {
 	NULL
 };
 
-enum actions {
+static enum actions {
 	ACTION_LIST = 1
 } actions;
 
diff --git a/tools/perf/builtin-data.c b/tools/perf/builtin-data.c
index 85f59886b5cf0045..4c08ccb8c06b7598 100644
--- a/tools/perf/builtin-data.c
+++ b/tools/perf/builtin-data.c
@@ -28,15 +28,15 @@ static const char *data_usage[] = {
 	NULL
 };
 
-const char *to_json;
-const char *to_ctf;
-struct perf_data_convert_opts opts = {
+static const char *to_json;
+static const char *to_ctf;
+static struct perf_data_convert_opts opts = {
 	.force = false,
 	.all = false,
 	.time_str = NULL,
 };
 
-const struct option data_options[] = {
+static const struct option data_options[] = {
 		OPT_INCR('v', "verbose", &verbose, "be more verbose"),
 		OPT_STRING('i', "input", &input_name, "file", "input file name"),
 		OPT_STRING(0, "to-json", &to_json, NULL, "Convert to JSON format"),
diff --git a/tools/perf/builtin-diff.c b/tools/perf/builtin-diff.c
index 69069926dd0ba07b..e45f0ac1381ab2df 100644
--- a/tools/perf/builtin-diff.c
+++ b/tools/perf/builtin-diff.c
@@ -113,7 +113,7 @@ enum {
 	COMPUTE_STREAM,	/* After COMPUTE_MAX to avoid use current compute arrays */
 };
 
-const char *compute_names[COMPUTE_MAX] = {
+static const char *compute_names[COMPUTE_MAX] = {
 	[COMPUTE_DELTA] = "delta",
 	[COMPUTE_DELTA_ABS] = "delta-abs",
 	[COMPUTE_RATIO] = "ratio",
@@ -382,7 +382,7 @@ static void block_hist_free(void *he)
 	free(bh);
 }
 
-struct hist_entry_ops block_hist_ops = {
+static struct hist_entry_ops block_hist_ops = {
 	.new    = block_hist_zalloc,
 	.free   = block_hist_free,
 };
diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c
index 7929a5fa5f462b32..9c64a0d7482378cc 100644
--- a/tools/perf/builtin-kmem.c
+++ b/tools/perf/builtin-kmem.c
@@ -82,7 +82,7 @@ static unsigned long nr_allocs, nr_cross_allocs;
 
 /* filters for controlling start and stop of time of analysis */
 static struct perf_time_interval ptime;
-const char *time_str;
+static const char *time_str;
 
 static int insert_alloc_stat(unsigned long call_site, unsigned long ptr,
 			     int bytes_req, int bytes_alloc, int cpu)
diff --git a/tools/perf/builtin-kwork.c b/tools/perf/builtin-kwork.c
index 6f94a8f45f605f34..1140e00e874f4eed 100644
--- a/tools/perf/builtin-kwork.c
+++ b/tools/perf/builtin-kwork.c
@@ -985,7 +985,7 @@ static int process_irq_handler_exit_event(const struct perf_tool *tool,
 	return 0;
 }
 
-const struct evsel_str_handler irq_tp_handlers[] = {
+static const struct evsel_str_handler irq_tp_handlers[] = {
 	{ "irq:irq_handler_entry", process_irq_handler_entry_event, },
 	{ "irq:irq_handler_exit",  process_irq_handler_exit_event,  },
 };
@@ -1080,7 +1080,7 @@ static int process_softirq_exit_event(const struct perf_tool *tool,
 	return 0;
 }
 
-const struct evsel_str_handler softirq_tp_handlers[] = {
+static const struct evsel_str_handler softirq_tp_handlers[] = {
 	{ "irq:softirq_raise", process_softirq_raise_event, },
 	{ "irq:softirq_entry", process_softirq_entry_event, },
 	{ "irq:softirq_exit",  process_softirq_exit_event,  },
@@ -1211,7 +1211,7 @@ static int process_workqueue_execute_end_event(const struct perf_tool *tool,
 	return 0;
 }
 
-const struct evsel_str_handler workqueue_tp_handlers[] = {
+static const struct evsel_str_handler workqueue_tp_handlers[] = {
 	{ "workqueue:workqueue_activate_work", process_workqueue_activate_work_event, },
 	{ "workqueue:workqueue_execute_start", process_workqueue_execute_start_event, },
 	{ "workqueue:workqueue_execute_end",   process_workqueue_execute_end_event,   },
@@ -1281,7 +1281,7 @@ static int process_sched_switch_event(const struct perf_tool *tool,
 	return 0;
 }
 
-const struct evsel_str_handler sched_tp_handlers[] = {
+static const struct evsel_str_handler sched_tp_handlers[] = {
 	{ "sched:sched_switch",  process_sched_switch_event, },
 };
 
@@ -1561,13 +1561,13 @@ static void print_bad_events(struct perf_kwork *kwork)
 	}
 }
 
-const char *graph_load = "||||||||||||||||||||||||||||||||||||||||||||||||";
-const char *graph_idle = "                                                ";
 static void top_print_per_cpu_load(struct perf_kwork *kwork)
 {
 	int i, load_width;
 	u64 total, load, load_ratio;
 	struct kwork_top_stat *stat = &kwork->top_stat;
+	const char *graph_load = "||||||||||||||||||||||||||||||||||||||||||||||||";
+	const char *graph_idle = "                                                ";
 
 	for (i = 0; i < MAX_NR_CPUS; i++) {
 		total = stat->cpus_runtime[i].total;
diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index b005b23f9d8cd273..15a58da599581a7b 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -166,7 +166,7 @@ struct perf_script {
 	int			range_num;
 };
 
-struct output_option {
+static struct output_option {
 	const char *str;
 	enum perf_output_field field;
 } all_output_options[] = {
diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index 37950efb28aca8cc..35ff2495e1ee5897 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -1449,11 +1449,10 @@ parse_percent_limit(const struct option *opt, const char *arg,
 	return 0;
 }
 
-const char top_callchain_help[] = CALLCHAIN_RECORD_HELP CALLCHAIN_REPORT_HELP
-	"\n\t\t\t\tDefault: fp,graph,0.5,caller,function";
-
 int cmd_top(int argc, const char **argv)
 {
+	const char top_callchain_help[] = CALLCHAIN_RECORD_HELP CALLCHAIN_REPORT_HELP
+		"\n\t\t\t\tDefault: fp,graph,0.5,caller,function";
 	char errbuf[BUFSIZ];
 	struct perf_top top = {
 		.count_filter	     = 5,
diff --git a/tools/perf/tests/bp_signal.c b/tools/perf/tests/bp_signal.c
index 3faeb5b6fe0bb24a..f580ba7486b1d8ab 100644
--- a/tools/perf/tests/bp_signal.c
+++ b/tools/perf/tests/bp_signal.c
@@ -36,7 +36,7 @@ static int fd3;
 static int overflows;
 static int overflows_2;
 
-volatile long the_var;
+static volatile long the_var;
 
 
 /*
diff --git a/tools/perf/tests/dso-data.c b/tools/perf/tests/dso-data.c
index a1fff4203b75f665..46bc3f59726004fa 100644
--- a/tools/perf/tests/dso-data.c
+++ b/tools/perf/tests/dso-data.c
@@ -58,7 +58,7 @@ struct test_data_offset {
 	int size;
 };
 
-struct test_data_offset offsets[] = {
+static struct test_data_offset offsets[] = {
 	/* Fill first cache page. */
 	{
 		.offset = 10,
diff --git a/tools/perf/tests/wp.c b/tools/perf/tests/wp.c
index 6c178985e37f5eba..69b31f00eed04c5b 100644
--- a/tools/perf/tests/wp.c
+++ b/tools/perf/tests/wp.c
@@ -22,11 +22,11 @@ do {                                            \
 
 #ifdef __i386__
 /* Only breakpoint length less-than 8 has hardware support on i386. */
-volatile u32 data1;
+static volatile u32 data1;
 #else
-volatile u64 data1;
+static volatile u64 data1;
 #endif
-volatile u8 data2[3];
+static volatile u8 data2[3];
 
 #ifndef __s390x__
 static int wp_read(int fd, long long *count, int size)
diff --git a/tools/perf/util/block-range.c b/tools/perf/util/block-range.c
index 15c42196c24c8230..7c559fcfd7e0a6db 100644
--- a/tools/perf/util/block-range.c
+++ b/tools/perf/util/block-range.c
@@ -4,7 +4,7 @@
 #include <assert.h>
 #include <stdlib.h>
 
-struct {
+static struct {
 	struct rb_root root;
 	u64 blocks;
 } block_ranges;
diff --git a/tools/perf/util/bpf_counter.c b/tools/perf/util/bpf_counter.c
index 2ffd7aefb6eb3bf8..34b6b0da18b738c7 100644
--- a/tools/perf/util/bpf_counter.c
+++ b/tools/perf/util/bpf_counter.c
@@ -353,7 +353,7 @@ static int bpf_program_profiler__install_pe(struct evsel *evsel, int cpu_map_idx
 	return 0;
 }
 
-struct bpf_counter_ops bpf_program_profiler_ops = {
+static struct bpf_counter_ops bpf_program_profiler_ops = {
 	.load       = bpf_program_profiler__load,
 	.enable	    = bpf_program_profiler__enable,
 	.disable    = bpf_program_profiler__disable,
@@ -833,7 +833,7 @@ static int bperf__destroy(struct evsel *evsel)
  * the leader prog.
  */
 
-struct bpf_counter_ops bperf_ops = {
+static struct bpf_counter_ops bperf_ops = {
 	.load       = bperf__load,
 	.enable     = bperf__enable,
 	.disable    = bperf__disable,
diff --git a/tools/perf/util/bpf_off_cpu.c b/tools/perf/util/bpf_off_cpu.c
index 0891d9c736609cfa..a3b699a5322f1e51 100644
--- a/tools/perf/util/bpf_off_cpu.c
+++ b/tools/perf/util/bpf_off_cpu.c
@@ -39,7 +39,7 @@ union off_cpu_data {
 	u64 array[1024 / sizeof(u64)];
 };
 
-u64 off_cpu_raw[MAX_STACKS + 5];
+static u64 off_cpu_raw[MAX_STACKS + 5];
 
 static int off_cpu_config(struct evlist *evlist)
 {
diff --git a/tools/perf/util/debug.c b/tools/perf/util/debug.c
index 1dfa4d0eec4d7d5a..6b5ffe81f14121b7 100644
--- a/tools/perf/util/debug.c
+++ b/tools/perf/util/debug.c
@@ -48,7 +48,7 @@ int debug_ordered_events;
 static int redirect_to_stderr;
 int debug_data_convert;
 static FILE *_debug_file;
-bool debug_display_time;
+static bool debug_display_time;
 int debug_type_profile;
 
 FILE *debug_file(void)
diff --git a/tools/perf/util/debuginfo.c b/tools/perf/util/debuginfo.c
index 4a559b3e8cdce60a..8b819dea36ac1ef0 100644
--- a/tools/perf/util/debuginfo.c
+++ b/tools/perf/util/debuginfo.c
@@ -88,18 +88,17 @@ static struct debuginfo *__debuginfo__new(const char *path)
 	return dbg;
 }
 
-enum dso_binary_type distro_dwarf_types[] = {
-	DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
-	DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
-	DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
-	DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
-	DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO,
-	DSO_BINARY_TYPE__NOT_FOUND,
-};
-
 struct debuginfo *debuginfo__new(const char *path)
 {
-	enum dso_binary_type *type;
+	const enum dso_binary_type distro_dwarf_types[] = {
+		DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
+		DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
+		DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
+		DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
+		DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO,
+		DSO_BINARY_TYPE__NOT_FOUND,
+	};
+	const enum dso_binary_type *type;
 	char buf[PATH_MAX], nil = '\0';
 	struct dso *dso;
 	struct debuginfo *dinfo = NULL;
diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c
index fda8fcfa46e07d38..fe482eec7d9ada28 100644
--- a/tools/perf/util/sort.c
+++ b/tools/perf/util/sort.c
@@ -43,11 +43,11 @@ regex_t		parent_regex;
 const char	default_parent_pattern[] = "^sys_|^do_page_fault";
 const char	*parent_pattern = default_parent_pattern;
 const char	*default_sort_order = "comm,dso,symbol";
-const char	default_branch_sort_order[] = "comm,dso_from,symbol_from,symbol_to,cycles";
+static const char	default_branch_sort_order[] = "comm,dso_from,symbol_from,symbol_to,cycles";
 const char	default_mem_sort_order[] = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked,blocked,local_ins_lat,local_p_stage_cyc";
-const char	default_top_sort_order[] = "dso,symbol";
-const char	default_diff_sort_order[] = "dso,symbol";
-const char	default_tracepoint_sort_order[] = "trace";
+static const char	default_top_sort_order[] = "dso,symbol";
+static const char	default_diff_sort_order[] = "dso,symbol";
+static const char	default_tracepoint_sort_order[] = "trace";
 const char	*sort_order;
 const char	*field_order;
 regex_t		ignore_callees_regex;
@@ -172,7 +172,7 @@ static int hist_entry__tgid_snprintf(struct hist_entry *he, char *bf,
 	return repsep_snprintf(bf, size, "%7d:%-*.*s", tgid, width, width, comm ?: "");
 }
 
-struct sort_entry sort_tgid = {
+static struct sort_entry sort_tgid = {
 	.se_header	= "   Tgid:Command",
 	.se_cmp		= sort__tgid_cmp,
 	.se_snprintf	= hist_entry__tgid_snprintf,
@@ -218,7 +218,7 @@ static int hist_entry__simd_snprintf(struct hist_entry *he, char *bf,
 	return repsep_snprintf(bf, size, "[.] %s", name);
 }
 
-struct sort_entry sort_simd = {
+static struct sort_entry sort_simd = {
 	.se_header	= "Simd   ",
 	.se_cmp		= sort__simd_cmp,
 	.se_snprintf	= hist_entry__simd_snprintf,
@@ -604,7 +604,7 @@ hist_entry__symoff_snprintf(struct hist_entry *he, char *bf, size_t size, unsign
 	return repsep_snprintf(bf, size, "[%c] %s+0x%llx", he->level, sym->name, he->ip - sym->start);
 }
 
-struct sort_entry sort_sym_offset = {
+static struct sort_entry sort_sym_offset = {
 	.se_header	= "Symbol Offset",
 	.se_cmp		= sort__symoff_cmp,
 	.se_sort	= sort__symoff_sort,
@@ -715,7 +715,7 @@ static int hist_entry__srcline_from_snprintf(struct hist_entry *he, char *bf,
 	return repsep_snprintf(bf, size, "%-*.*s", width, width, he->branch_info->srcline_from);
 }
 
-struct sort_entry sort_srcline_from = {
+static struct sort_entry sort_srcline_from = {
 	.se_header	= "From Source:Line",
 	.se_cmp		= sort__srcline_from_cmp,
 	.se_collapse	= sort__srcline_from_collapse,
@@ -763,7 +763,7 @@ static int hist_entry__srcline_to_snprintf(struct hist_entry *he, char *bf,
 	return repsep_snprintf(bf, size, "%-*.*s", width, width, he->branch_info->srcline_to);
 }
 
-struct sort_entry sort_srcline_to = {
+static struct sort_entry sort_srcline_to = {
 	.se_header	= "To Source:Line",
 	.se_cmp		= sort__srcline_to_cmp,
 	.se_collapse	= sort__srcline_to_collapse,
@@ -799,7 +799,7 @@ static int hist_entry__sym_ipc_snprintf(struct hist_entry *he, char *bf,
 	return repsep_snprintf(bf, size, "%-*s", width, tmp);
 }
 
-struct sort_entry sort_sym_ipc = {
+static struct sort_entry sort_sym_ipc = {
 	.se_header	= "IPC   [IPC Coverage]",
 	.se_cmp		= sort__sym_cmp,
 	.se_snprintf	= hist_entry__sym_ipc_snprintf,
@@ -817,7 +817,7 @@ static int hist_entry__sym_ipc_null_snprintf(struct hist_entry *he
 	return repsep_snprintf(bf, size, "%-*s", width, tmp);
 }
 
-struct sort_entry sort_sym_ipc_null = {
+static struct sort_entry sort_sym_ipc_null = {
 	.se_header	= "IPC   [IPC Coverage]",
 	.se_cmp		= sort__sym_cmp,
 	.se_snprintf	= hist_entry__sym_ipc_null_snprintf,
@@ -850,7 +850,7 @@ static int hist_entry__callchain_branch_predicted_snprintf(
 	return repsep_snprintf(bf, size, "%-*.*s", width, width, str);
 }
 
-struct sort_entry sort_callchain_branch_predicted = {
+static struct sort_entry sort_callchain_branch_predicted = {
 	.se_header	= "Predicted",
 	.se_cmp		= sort__callchain_branch_predicted_cmp,
 	.se_snprintf	= hist_entry__callchain_branch_predicted_snprintf,
@@ -880,7 +880,7 @@ static int hist_entry__callchain_branch_abort_snprintf(struct hist_entry *he,
 	return repsep_snprintf(bf, size, "%-*.*s", width, width, str);
 }
 
-struct sort_entry sort_callchain_branch_abort = {
+static struct sort_entry sort_callchain_branch_abort = {
 	.se_header	= "Abort",
 	.se_cmp		= sort__callchain_branch_abort_cmp,
 	.se_snprintf	= hist_entry__callchain_branch_abort_snprintf,
@@ -913,7 +913,7 @@ static int hist_entry__callchain_branch_cycles_snprintf(struct hist_entry *he,
 	return repsep_snprintf(bf, size, "%-*.*s", width, width, str);
 }
 
-struct sort_entry sort_callchain_branch_cycles = {
+static struct sort_entry sort_callchain_branch_cycles = {
 	.se_header	= "Cycles",
 	.se_cmp		= sort__callchain_branch_cycles_cmp,
 	.se_snprintf	= hist_entry__callchain_branch_cycles_snprintf,
@@ -980,7 +980,7 @@ static int hist_entry__srcfile_snprintf(struct hist_entry *he, char *bf,
 	return repsep_snprintf(bf, size, "%-.*s", width, he->srcfile);
 }
 
-struct sort_entry sort_srcfile = {
+static struct sort_entry sort_srcfile = {
 	.se_header	= "Source File",
 	.se_cmp		= sort__srcfile_cmp,
 	.se_collapse	= sort__srcfile_collapse,
@@ -1032,7 +1032,7 @@ static int hist_entry__cpu_snprintf(struct hist_entry *he, char *bf,
 	return repsep_snprintf(bf, size, "%*.*d", width, width, he->cpu);
 }
 
-struct sort_entry sort_cpu = {
+static struct sort_entry sort_cpu = {
 	.se_header      = "CPU",
 	.se_cmp	        = sort__cpu_cmp,
 	.se_snprintf    = hist_entry__cpu_snprintf,
@@ -1063,7 +1063,7 @@ static int hist_entry__parallelism_snprintf(struct hist_entry *he, char *bf,
 	return repsep_snprintf(bf, size, "%*d", width, he->parallelism);
 }
 
-struct sort_entry sort_parallelism = {
+static struct sort_entry sort_parallelism = {
 	.se_header      = "Parallelism",
 	.se_cmp	        = sort__parallelism_cmp,
 	.se_filter	= hist_entry__parallelism_filter,
@@ -1104,7 +1104,7 @@ static int hist_entry__cgroup_id_snprintf(struct hist_entry *he,
 			       he->cgroup_id.ino);
 }
 
-struct sort_entry sort_cgroup_id = {
+static struct sort_entry sort_cgroup_id = {
 	.se_header      = "cgroup id (dev/inode)",
 	.se_cmp	        = sort__cgroup_id_cmp,
 	.se_snprintf    = hist_entry__cgroup_id_snprintf,
@@ -1137,7 +1137,7 @@ static int hist_entry__cgroup_snprintf(struct hist_entry *he,
 	return repsep_snprintf(bf, size, "%s", cgrp_name);
 }
 
-struct sort_entry sort_cgroup = {
+static struct sort_entry sort_cgroup = {
 	.se_header      = "Cgroup",
 	.se_cmp	        = sort__cgroup_cmp,
 	.se_snprintf    = hist_entry__cgroup_snprintf,
@@ -1168,7 +1168,7 @@ static int hist_entry__socket_filter(struct hist_entry *he, int type, const void
 	return sk >= 0 && he->socket != sk;
 }
 
-struct sort_entry sort_socket = {
+static struct sort_entry sort_socket = {
 	.se_header      = "Socket",
 	.se_cmp	        = sort__socket_cmp,
 	.se_snprintf    = hist_entry__socket_snprintf,
@@ -1199,7 +1199,7 @@ static int hist_entry__time_snprintf(struct hist_entry *he, char *bf,
 	return repsep_snprintf(bf, size, "%-.*s", width, he_time);
 }
 
-struct sort_entry sort_time = {
+static struct sort_entry sort_time = {
 	.se_header      = "Time",
 	.se_cmp	        = sort__time_cmp,
 	.se_snprintf    = hist_entry__time_snprintf,
@@ -1268,7 +1268,7 @@ static int hist_entry__trace_snprintf(struct hist_entry *he, char *bf,
 	return repsep_snprintf(bf, size, "%-.*s", width, he->trace_output);
 }
 
-struct sort_entry sort_trace = {
+static struct sort_entry sort_trace = {
 	.se_header      = "Trace output",
 	.se_cmp	        = sort__trace_cmp,
 	.se_snprintf    = hist_entry__trace_snprintf,
@@ -1563,7 +1563,7 @@ sort__addr_to_cmp(struct hist_entry *left, struct hist_entry *right)
 	return _sort__addr_cmp(to_l->addr, to_r->addr);
 }
 
-struct sort_entry sort_addr_from = {
+static struct sort_entry sort_addr_from = {
 	.se_header	= "Source Address",
 	.se_cmp		= sort__addr_from_cmp,
 	.se_snprintf	= hist_entry__addr_from_snprintf,
@@ -1571,7 +1571,7 @@ struct sort_entry sort_addr_from = {
 	.se_width_idx	= HISTC_ADDR_FROM,
 };
 
-struct sort_entry sort_addr_to = {
+static struct sort_entry sort_addr_to = {
 	.se_header	= "Target Address",
 	.se_cmp		= sort__addr_to_cmp,
 	.se_snprintf	= hist_entry__addr_to_snprintf,
@@ -1628,7 +1628,7 @@ static int hist_entry__cycles_snprintf(struct hist_entry *he, char *bf,
 			       he->branch_info->flags.cycles);
 }
 
-struct sort_entry sort_cycles = {
+static struct sort_entry sort_cycles = {
 	.se_header	= "Basic Block Cycles",
 	.se_cmp		= sort__cycles_cmp,
 	.se_snprintf	= hist_entry__cycles_snprintf,
@@ -1918,7 +1918,7 @@ static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf,
 	return _hist_entry__sym_snprintf(ms, addr, level, bf, size, width);
 }
 
-struct sort_entry sort_mispredict = {
+static struct sort_entry sort_mispredict = {
 	.se_header	= "Branch Mispredicted",
 	.se_cmp		= sort__mispredict_cmp,
 	.se_snprintf	= hist_entry__mispredict_snprintf,
@@ -1937,7 +1937,7 @@ static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf,
 	return repsep_snprintf(bf, size, "%-*llu", width, he->weight);
 }
 
-struct sort_entry sort_local_weight = {
+static struct sort_entry sort_local_weight = {
 	.se_header	= "Local Weight",
 	.se_cmp		= sort__weight_cmp,
 	.se_snprintf	= hist_entry__local_weight_snprintf,
@@ -1951,7 +1951,7 @@ static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf,
 			       he->weight * he->stat.nr_events);
 }
 
-struct sort_entry sort_global_weight = {
+static struct sort_entry sort_global_weight = {
 	.se_header	= "Weight",
 	.se_cmp		= sort__weight_cmp,
 	.se_snprintf	= hist_entry__global_weight_snprintf,
@@ -1970,7 +1970,7 @@ static int hist_entry__local_ins_lat_snprintf(struct hist_entry *he, char *bf,
 	return repsep_snprintf(bf, size, "%-*u", width, he->ins_lat);
 }
 
-struct sort_entry sort_local_ins_lat = {
+static struct sort_entry sort_local_ins_lat = {
 	.se_header	= "Local INSTR Latency",
 	.se_cmp		= sort__ins_lat_cmp,
 	.se_snprintf	= hist_entry__local_ins_lat_snprintf,
@@ -1984,7 +1984,7 @@ static int hist_entry__global_ins_lat_snprintf(struct hist_entry *he, char *bf,
 			       he->ins_lat * he->stat.nr_events);
 }
 
-struct sort_entry sort_global_ins_lat = {
+static struct sort_entry sort_global_ins_lat = {
 	.se_header	= "INSTR Latency",
 	.se_cmp		= sort__ins_lat_cmp,
 	.se_snprintf	= hist_entry__global_ins_lat_snprintf,
@@ -2010,70 +2010,70 @@ static int hist_entry__p_stage_cyc_snprintf(struct hist_entry *he, char *bf,
 	return repsep_snprintf(bf, size, "%-*u", width, he->weight3);
 }
 
-struct sort_entry sort_local_p_stage_cyc = {
+static struct sort_entry sort_local_p_stage_cyc = {
 	.se_header      = "Local Pipeline Stage Cycle",
 	.se_cmp         = sort__p_stage_cyc_cmp,
 	.se_snprintf	= hist_entry__p_stage_cyc_snprintf,
 	.se_width_idx	= HISTC_LOCAL_P_STAGE_CYC,
 };
 
-struct sort_entry sort_global_p_stage_cyc = {
+static struct sort_entry sort_global_p_stage_cyc = {
 	.se_header      = "Pipeline Stage Cycle",
 	.se_cmp         = sort__p_stage_cyc_cmp,
 	.se_snprintf    = hist_entry__global_p_stage_cyc_snprintf,
 	.se_width_idx   = HISTC_GLOBAL_P_STAGE_CYC,
 };
 
-struct sort_entry sort_mem_daddr_sym = {
+static struct sort_entry sort_mem_daddr_sym = {
 	.se_header	= "Data Symbol",
 	.se_cmp		= sort__daddr_cmp,
 	.se_snprintf	= hist_entry__daddr_snprintf,
 	.se_width_idx	= HISTC_MEM_DADDR_SYMBOL,
 };
 
-struct sort_entry sort_mem_iaddr_sym = {
+static struct sort_entry sort_mem_iaddr_sym = {
 	.se_header	= "Code Symbol",
 	.se_cmp		= sort__iaddr_cmp,
 	.se_snprintf	= hist_entry__iaddr_snprintf,
 	.se_width_idx	= HISTC_MEM_IADDR_SYMBOL,
 };
 
-struct sort_entry sort_mem_daddr_dso = {
+static struct sort_entry sort_mem_daddr_dso = {
 	.se_header	= "Data Object",
 	.se_cmp		= sort__dso_daddr_cmp,
 	.se_snprintf	= hist_entry__dso_daddr_snprintf,
 	.se_width_idx	= HISTC_MEM_DADDR_DSO,
 };
 
-struct sort_entry sort_mem_locked = {
+static struct sort_entry sort_mem_locked = {
 	.se_header	= "Locked",
 	.se_cmp		= sort__locked_cmp,
 	.se_snprintf	= hist_entry__locked_snprintf,
 	.se_width_idx	= HISTC_MEM_LOCKED,
 };
 
-struct sort_entry sort_mem_tlb = {
+static struct sort_entry sort_mem_tlb = {
 	.se_header	= "TLB access",
 	.se_cmp		= sort__tlb_cmp,
 	.se_snprintf	= hist_entry__tlb_snprintf,
 	.se_width_idx	= HISTC_MEM_TLB,
 };
 
-struct sort_entry sort_mem_lvl = {
+static struct sort_entry sort_mem_lvl = {
 	.se_header	= "Memory access",
 	.se_cmp		= sort__lvl_cmp,
 	.se_snprintf	= hist_entry__lvl_snprintf,
 	.se_width_idx	= HISTC_MEM_LVL,
 };
 
-struct sort_entry sort_mem_snoop = {
+static struct sort_entry sort_mem_snoop = {
 	.se_header	= "Snoop",
 	.se_cmp		= sort__snoop_cmp,
 	.se_snprintf	= hist_entry__snoop_snprintf,
 	.se_width_idx	= HISTC_MEM_SNOOP,
 };
 
-struct sort_entry sort_mem_dcacheline = {
+static struct sort_entry sort_mem_dcacheline = {
 	.se_header	= "Data Cacheline",
 	.se_cmp		= sort__dcacheline_cmp,
 	.se_snprintf	= hist_entry__dcacheline_snprintf,
@@ -2108,7 +2108,7 @@ static int hist_entry__blocked_snprintf(struct hist_entry *he, char *bf,
 	return repsep_snprintf(bf, size, "%.*s", width, out);
 }
 
-struct sort_entry sort_mem_blocked = {
+static struct sort_entry sort_mem_blocked = {
 	.se_header	= "Blocked",
 	.se_cmp		= sort__blocked_cmp,
 	.se_snprintf	= hist_entry__blocked_snprintf,
@@ -2149,7 +2149,7 @@ static int hist_entry__phys_daddr_snprintf(struct hist_entry *he, char *bf,
 	return width;
 }
 
-struct sort_entry sort_mem_phys_daddr = {
+static struct sort_entry sort_mem_phys_daddr = {
 	.se_header	= "Data Physical Address",
 	.se_cmp		= sort__phys_daddr_cmp,
 	.se_snprintf	= hist_entry__phys_daddr_snprintf,
@@ -2178,7 +2178,7 @@ static int hist_entry__data_page_size_snprintf(struct hist_entry *he, char *bf,
 			get_page_size_name(mem_info__daddr(he->mem_info)->data_page_size, str));
 }
 
-struct sort_entry sort_mem_data_page_size = {
+static struct sort_entry sort_mem_data_page_size = {
 	.se_header	= "Data Page Size",
 	.se_cmp		= sort__data_page_size_cmp,
 	.se_snprintf	= hist_entry__data_page_size_snprintf,
@@ -2203,7 +2203,7 @@ static int hist_entry__code_page_size_snprintf(struct hist_entry *he, char *bf,
 			       get_page_size_name(he->code_page_size, str));
 }
 
-struct sort_entry sort_code_page_size = {
+static struct sort_entry sort_code_page_size = {
 	.se_header	= "Code Page Size",
 	.se_cmp		= sort__code_page_size_cmp,
 	.se_snprintf	= hist_entry__code_page_size_snprintf,
@@ -2235,7 +2235,7 @@ static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf,
 	return repsep_snprintf(bf, size, "%-*s", width, out);
 }
 
-struct sort_entry sort_abort = {
+static struct sort_entry sort_abort = {
 	.se_header	= "Transaction abort",
 	.se_cmp		= sort__abort_cmp,
 	.se_snprintf	= hist_entry__abort_snprintf,
@@ -2267,7 +2267,7 @@ static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf,
 	return repsep_snprintf(bf, size, "%-*s", width, out);
 }
 
-struct sort_entry sort_in_tx = {
+static struct sort_entry sort_in_tx = {
 	.se_header	= "Branch in transaction",
 	.se_cmp		= sort__in_tx_cmp,
 	.se_snprintf	= hist_entry__in_tx_snprintf,
@@ -2339,7 +2339,7 @@ static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf,
 	return repsep_snprintf(bf, size, "%-*s", width, buf);
 }
 
-struct sort_entry sort_transaction = {
+static struct sort_entry sort_transaction = {
 	.se_header	= "Transaction                ",
 	.se_cmp		= sort__transaction_cmp,
 	.se_snprintf	= hist_entry__transaction_snprintf,
@@ -2378,7 +2378,7 @@ static int hist_entry__sym_size_snprintf(struct hist_entry *he, char *bf,
 	return _hist_entry__sym_size_snprintf(he->ms.sym, bf, size, width);
 }
 
-struct sort_entry sort_sym_size = {
+static struct sort_entry sort_sym_size = {
 	.se_header	= "Symbol size",
 	.se_cmp		= sort__sym_size_cmp,
 	.se_snprintf	= hist_entry__sym_size_snprintf,
@@ -2417,7 +2417,7 @@ static int hist_entry__dso_size_snprintf(struct hist_entry *he, char *bf,
 	return _hist_entry__dso_size_snprintf(he->ms.map, bf, size, width);
 }
 
-struct sort_entry sort_dso_size = {
+static struct sort_entry sort_dso_size = {
 	.se_header	= "DSO size",
 	.se_cmp		= sort__dso_size_cmp,
 	.se_snprintf	= hist_entry__dso_size_snprintf,
@@ -2454,7 +2454,7 @@ static int hist_entry__addr_snprintf(struct hist_entry *he, char *bf,
 	return repsep_snprintf(bf, size, "%-#*llx", width, ip);
 }
 
-struct sort_entry sort_addr = {
+static struct sort_entry sort_addr = {
 	.se_header	= "Address",
 	.se_cmp		= sort__addr_cmp,
 	.se_snprintf	= hist_entry__addr_snprintf,
@@ -2572,7 +2572,7 @@ static int hist_entry__typeoff_snprintf(struct hist_entry *he, char *bf,
 			       he->mem_type_off, buf);
 }
 
-struct sort_entry sort_type_offset = {
+static struct sort_entry sort_type_offset = {
 	.se_header	= "Data Type Offset",
 	.se_cmp		= sort__type_cmp,
 	.se_collapse	= sort__typeoff_sort,
@@ -2630,7 +2630,7 @@ static int hist_entry__typecln_snprintf(struct hist_entry *he, char *bf,
 			       he->mem_type_off / cln_size);
 }
 
-struct sort_entry sort_type_cacheline = {
+static struct sort_entry sort_type_cacheline = {
 	.se_header	= "Data Type Cacheline",
 	.se_cmp		= sort__type_cmp,
 	.se_collapse	= sort__typecln_sort,
diff --git a/tools/perf/util/trace-event-scripting.c b/tools/perf/util/trace-event-scripting.c
index fa850e44cb46cfd7..0745a2fe3af8c201 100644
--- a/tools/perf/util/trace-event-scripting.c
+++ b/tools/perf/util/trace-event-scripting.c
@@ -122,6 +122,7 @@ void scripting_context__update(struct scripting_context *c,
 	c->addr_al = addr_al;
 }
 
+#if !defined(HAVE_LIBPERL_SUPPORT) || !defined(HAVE_LIBPYTHON_SUPPORT)
 static int flush_script_unsupported(void)
 {
 	return 0;
@@ -139,7 +140,22 @@ static void process_event_unsupported(union perf_event *event __maybe_unused,
 				      struct addr_location *addr_al __maybe_unused)
 {
 }
+#endif
+
+static void register_python_scripting(struct scripting_ops *scripting_ops)
+{
+	if (scripting_context == NULL)
+		scripting_context = malloc(sizeof(*scripting_context));
 
+	if (scripting_context == NULL ||
+	   script_spec_register("Python", scripting_ops) ||
+	   script_spec_register("py", scripting_ops)) {
+		pr_err("Error registering Python script extension: disabling it\n");
+		zfree(&scripting_context);
+	}
+}
+
+#ifndef HAVE_LIBPYTHON_SUPPORT
 static void print_python_unsupported_msg(void)
 {
 	fprintf(stderr, "Python scripting not supported."
@@ -169,32 +185,18 @@ static int python_generate_script_unsupported(struct tep_handle *pevent
 	return -1;
 }
 
-struct scripting_ops python_scripting_unsupported_ops = {
-	.name = "Python",
-	.dirname = "python",
-	.start_script = python_start_script_unsupported,
-	.flush_script = flush_script_unsupported,
-	.stop_script = stop_script_unsupported,
-	.process_event = process_event_unsupported,
-	.generate_script = python_generate_script_unsupported,
-};
-
-static void register_python_scripting(struct scripting_ops *scripting_ops)
-{
-	if (scripting_context == NULL)
-		scripting_context = malloc(sizeof(*scripting_context));
-
-       if (scripting_context == NULL ||
-	   script_spec_register("Python", scripting_ops) ||
-	   script_spec_register("py", scripting_ops)) {
-		pr_err("Error registering Python script extension: disabling it\n");
-		zfree(&scripting_context);
-	}
-}
-
-#ifndef HAVE_LIBPYTHON_SUPPORT
 void setup_python_scripting(void)
 {
+	static struct scripting_ops python_scripting_unsupported_ops = {
+		.name = "Python",
+		.dirname = "python",
+		.start_script = python_start_script_unsupported,
+		.flush_script = flush_script_unsupported,
+		.stop_script = stop_script_unsupported,
+		.process_event = process_event_unsupported,
+		.generate_script = python_generate_script_unsupported,
+	};
+
 	register_python_scripting(&python_scripting_unsupported_ops);
 }
 #else
@@ -207,6 +209,20 @@ void setup_python_scripting(void)
 #endif
 
 #ifdef HAVE_LIBTRACEEVENT
+static void register_perl_scripting(struct scripting_ops *scripting_ops)
+{
+	if (scripting_context == NULL)
+		scripting_context = malloc(sizeof(*scripting_context));
+
+	if (scripting_context == NULL ||
+	   script_spec_register("Perl", scripting_ops) ||
+	   script_spec_register("pl", scripting_ops)) {
+		pr_err("Error registering Perl script extension: disabling it\n");
+		zfree(&scripting_context);
+	}
+}
+
+#ifndef HAVE_LIBPERL_SUPPORT
 static void print_perl_unsupported_msg(void)
 {
 	fprintf(stderr, "Perl scripting not supported."
@@ -235,32 +251,18 @@ static int perl_generate_script_unsupported(struct tep_handle *pevent
 	return -1;
 }
 
-struct scripting_ops perl_scripting_unsupported_ops = {
-	.name = "Perl",
-	.dirname = "perl",
-	.start_script = perl_start_script_unsupported,
-	.flush_script = flush_script_unsupported,
-	.stop_script = stop_script_unsupported,
-	.process_event = process_event_unsupported,
-	.generate_script = perl_generate_script_unsupported,
-};
-
-static void register_perl_scripting(struct scripting_ops *scripting_ops)
-{
-	if (scripting_context == NULL)
-		scripting_context = malloc(sizeof(*scripting_context));
-
-       if (scripting_context == NULL ||
-	   script_spec_register("Perl", scripting_ops) ||
-	   script_spec_register("pl", scripting_ops)) {
-		pr_err("Error registering Perl script extension: disabling it\n");
-		zfree(&scripting_context);
-	}
-}
-
-#ifndef HAVE_LIBPERL_SUPPORT
 void setup_perl_scripting(void)
 {
+	static struct scripting_ops perl_scripting_unsupported_ops = {
+		.name = "Perl",
+		.dirname = "perl",
+		.start_script = perl_start_script_unsupported,
+		.flush_script = flush_script_unsupported,
+		.stop_script = stop_script_unsupported,
+		.process_event = process_event_unsupported,
+		.generate_script = perl_generate_script_unsupported,
+	};
+
 	register_perl_scripting(&perl_scripting_unsupported_ops);
 }
 #else
diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index 8b893de35f777db0..c5fee8e394805544 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -77,8 +77,6 @@ bool sysctl__nmi_watchdog_enabled(void)
 	return nmi_watchdog;
 }
 
-bool test_attr__enabled;
-
 bool exclude_GH_default;
 
 bool perf_host  = true;
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 2/4] perf bench: Constify tables
  2026-04-01 21:53 [PATCHES v2 perf-tools-next 0/4] Cleanups and a fix Arnaldo Carvalho de Melo
  2026-04-01 21:53 ` [PATCH 1/4] perf tools: Make more global variables static Arnaldo Carvalho de Melo
@ 2026-04-01 21:53 ` Arnaldo Carvalho de Melo
  2026-04-01 21:53 ` [PATCH 3/4] perf tools: Use calloc() were applicable Arnaldo Carvalho de Melo
  2026-04-01 21:53 ` [PATCH 4/4] perf symbol: Add missing libgen.h include to get basename() prototype Arnaldo Carvalho de Melo
  3 siblings, 0 replies; 14+ messages in thread
From: Arnaldo Carvalho de Melo @ 2026-04-01 21:53 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Ingo Molnar, Thomas Gleixner, James Clark, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Kan Liang, Clark Williams, linux-kernel,
	linux-perf-users, Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <acme@redhat.com>

Those tables and variables don't change, better capture this by
explicitely using 'const'.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/builtin-bench.c | 42 +++++++++++++++++++-------------------
 1 file changed, 21 insertions(+), 21 deletions(-)

diff --git a/tools/perf/builtin-bench.c b/tools/perf/builtin-bench.c
index 02dea1b8822864e8..02d47913cc6a3fae 100644
--- a/tools/perf/builtin-bench.c
+++ b/tools/perf/builtin-bench.c
@@ -37,14 +37,14 @@ struct bench {
 };
 
 #ifdef HAVE_LIBNUMA_SUPPORT
-static struct bench numa_benchmarks[] = {
+static const struct bench numa_benchmarks[] = {
 	{ "mem",	"Benchmark for NUMA workloads",			bench_numa		},
 	{ "all",	"Run all NUMA benchmarks",			NULL			},
 	{ NULL,		NULL,						NULL			}
 };
 #endif
 
-static struct bench sched_benchmarks[] = {
+static const struct bench sched_benchmarks[] = {
 	{ "messaging",	"Benchmark for scheduling and IPC",		bench_sched_messaging	},
 	{ "pipe",	"Benchmark for pipe() between two processes",	bench_sched_pipe	},
 	{ "seccomp-notify",	"Benchmark for seccomp user notify",	bench_sched_seccomp_notify},
@@ -52,7 +52,7 @@ static struct bench sched_benchmarks[] = {
 	{ NULL,		NULL,						NULL			}
 };
 
-static struct bench syscall_benchmarks[] = {
+static const struct bench syscall_benchmarks[] = {
 	{ "basic",	"Benchmark for basic getppid(2) calls",		bench_syscall_basic	},
 	{ "getpgid",	"Benchmark for getpgid(2) calls",		bench_syscall_getpgid	},
 	{ "fork",	"Benchmark for fork(2) calls",			bench_syscall_fork	},
@@ -61,7 +61,7 @@ static struct bench syscall_benchmarks[] = {
 	{ NULL,		NULL,						NULL			},
 };
 
-static struct bench mem_benchmarks[] = {
+static const struct bench mem_benchmarks[] = {
 	{ "memcpy",	"Benchmark for memcpy() functions",		bench_mem_memcpy	},
 	{ "memset",	"Benchmark for memset() functions",		bench_mem_memset	},
 	{ "find_bit",	"Benchmark for find_bit() functions",		bench_mem_find_bit	},
@@ -70,7 +70,7 @@ static struct bench mem_benchmarks[] = {
 	{ NULL,		NULL,						NULL			}
 };
 
-static struct bench futex_benchmarks[] = {
+static const struct bench futex_benchmarks[] = {
 	{ "hash",	"Benchmark for futex hash table",               bench_futex_hash	},
 	{ "wake",	"Benchmark for futex wake calls",               bench_futex_wake	},
 	{ "wake-parallel", "Benchmark for parallel futex wake calls",   bench_futex_wake_parallel },
@@ -82,7 +82,7 @@ static struct bench futex_benchmarks[] = {
 };
 
 #ifdef HAVE_EVENTFD_SUPPORT
-static struct bench epoll_benchmarks[] = {
+static const struct bench epoll_benchmarks[] = {
 	{ "wait",	"Benchmark epoll concurrent epoll_waits",       bench_epoll_wait	},
 	{ "ctl",	"Benchmark epoll concurrent epoll_ctls",        bench_epoll_ctl		},
 	{ "all",	"Run all futex benchmarks",			NULL			},
@@ -90,7 +90,7 @@ static struct bench epoll_benchmarks[] = {
 };
 #endif // HAVE_EVENTFD_SUPPORT
 
-static struct bench internals_benchmarks[] = {
+static const struct bench internals_benchmarks[] = {
 	{ "synthesize", "Benchmark perf event synthesis",	bench_synthesize	},
 	{ "kallsyms-parse", "Benchmark kallsyms parsing",	bench_kallsyms_parse	},
 	{ "inject-build-id", "Benchmark build-id injection",	bench_inject_build_id	},
@@ -99,14 +99,14 @@ static struct bench internals_benchmarks[] = {
 	{ NULL,		NULL,					NULL			}
 };
 
-static struct bench breakpoint_benchmarks[] = {
+static const struct bench breakpoint_benchmarks[] = {
 	{ "thread", "Benchmark thread start/finish with breakpoints", bench_breakpoint_thread},
 	{ "enable", "Benchmark breakpoint enable/disable", bench_breakpoint_enable},
 	{ "all", "Run all breakpoint benchmarks", NULL},
 	{ NULL,	NULL, NULL },
 };
 
-static struct bench uprobe_benchmarks[] = {
+static const struct bench uprobe_benchmarks[] = {
 	{ "baseline",	"Baseline libc usleep(1000) call",				bench_uprobe_baseline,	},
 	{ "empty",	"Attach empty BPF prog to uprobe on usleep, system wide",	bench_uprobe_empty,	},
 	{ "trace_printk", "Attach trace_printk BPF prog to uprobe on usleep syswide",	bench_uprobe_trace_printk,	},
@@ -116,12 +116,12 @@ static struct bench uprobe_benchmarks[] = {
 };
 
 struct collection {
-	const char	*name;
-	const char	*summary;
-	struct bench	*benchmarks;
+	const char		*name;
+	const char		*summary;
+	const struct bench	*benchmarks;
 };
 
-static struct collection collections[] = {
+static const struct collection collections[] = {
 	{ "sched",	"Scheduler and IPC benchmarks",			sched_benchmarks	},
 	{ "syscall",	"System call benchmarks",			syscall_benchmarks	},
 	{ "mem",	"Memory access benchmarks",			mem_benchmarks		},
@@ -147,9 +147,9 @@ static struct collection collections[] = {
 #define for_each_bench(coll, bench) \
 	for (bench = coll->benchmarks; bench && bench->name; bench++)
 
-static void dump_benchmarks(struct collection *coll)
+static void dump_benchmarks(const struct collection *coll)
 {
-	struct bench *bench;
+	const struct bench *bench;
 
 	printf("\n        # List of available benchmarks for collection '%s':\n\n", coll->name);
 
@@ -178,7 +178,7 @@ static const char * const bench_usage[] = {
 
 static void print_usage(void)
 {
-	struct collection *coll;
+	const struct collection *coll;
 	int i;
 
 	printf("Usage: \n");
@@ -234,9 +234,9 @@ static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t f
 	return ret;
 }
 
-static void run_collection(struct collection *coll)
+static void run_collection(const struct collection *coll)
 {
-	struct bench *bench;
+	const struct bench *bench;
 	const char *argv[2];
 
 	argv[1] = NULL;
@@ -260,7 +260,7 @@ static void run_collection(struct collection *coll)
 
 static void run_all_collections(void)
 {
-	struct collection *coll;
+	const struct collection *coll;
 
 	for_each_collection(coll)
 		run_collection(coll);
@@ -268,7 +268,7 @@ static void run_all_collections(void)
 
 int cmd_bench(int argc, const char **argv)
 {
-	struct collection *coll;
+	const struct collection *coll;
 	int ret = 0;
 
 	/* Unbuffered output */
@@ -306,7 +306,7 @@ int cmd_bench(int argc, const char **argv)
 	}
 
 	for_each_collection(coll) {
-		struct bench *bench;
+		const struct bench *bench;
 
 		if (strcmp(coll->name, argv[0]))
 			continue;
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 3/4] perf tools: Use calloc() were applicable
  2026-04-01 21:53 [PATCHES v2 perf-tools-next 0/4] Cleanups and a fix Arnaldo Carvalho de Melo
  2026-04-01 21:53 ` [PATCH 1/4] perf tools: Make more global variables static Arnaldo Carvalho de Melo
  2026-04-01 21:53 ` [PATCH 2/4] perf bench: Constify tables Arnaldo Carvalho de Melo
@ 2026-04-01 21:53 ` Arnaldo Carvalho de Melo
  2026-04-01 22:14   ` Ian Rogers
  2026-04-01 21:53 ` [PATCH 4/4] perf symbol: Add missing libgen.h include to get basename() prototype Arnaldo Carvalho de Melo
  3 siblings, 1 reply; 14+ messages in thread
From: Arnaldo Carvalho de Melo @ 2026-04-01 21:53 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Ingo Molnar, Thomas Gleixner, James Clark, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Kan Liang, Clark Williams, linux-kernel,
	linux-perf-users, Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <acme@redhat.com>

Instead of using zalloc(nr_entries * sizeof_entry) that is what calloc()
does.

In some places where linux/zalloc.h isn't needed, remove it, add when
needed and was getting it indirectly.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/arch/arm/util/auxtrace.c          |  6 +++---
 tools/perf/arch/powerpc/util/auxtrace.c      |  1 +
 tools/perf/arch/x86/tests/amd-ibs-period.c   |  3 +--
 tools/perf/arch/x86/tests/dwarf-unwind.c     | 11 +----------
 tools/perf/arch/x86/util/pmu.c               |  1 -
 tools/perf/bench/numa.c                      | 13 ++++---------
 tools/perf/bench/sched-messaging.c           |  2 +-
 tools/perf/builtin-annotate.c                |  1 -
 tools/perf/builtin-c2c.c                     |  6 +++---
 tools/perf/builtin-diff.c                    |  2 +-
 tools/perf/builtin-ftrace.c                  |  1 +
 tools/perf/builtin-kwork.c                   |  2 +-
 tools/perf/builtin-record.c                  | 10 +++++-----
 tools/perf/builtin-sched.c                   |  6 +++---
 tools/perf/builtin-script.c                  | 10 +++++-----
 tools/perf/builtin-stat.c                    |  2 +-
 tools/perf/builtin-trace.c                   |  4 +---
 tools/perf/jvmti/libjvmti.c                  |  5 ++---
 tools/perf/tests/code-reading.c              |  1 +
 tools/perf/tests/thread-map.c                |  1 -
 tools/perf/util/annotate-arch/annotate-x86.c |  1 +
 tools/perf/util/bpf-event.c                  |  2 +-
 tools/perf/util/bpf_counter_cgroup.c         |  1 -
 tools/perf/util/data-convert-bt.c            |  2 +-
 tools/perf/util/data.c                       |  2 +-
 tools/perf/util/db-export.c                  |  1 -
 tools/perf/util/disasm.c                     |  1 +
 tools/perf/util/event.c                      |  1 -
 tools/perf/util/evlist.c                     |  3 +--
 tools/perf/util/header.c                     | 18 +++++++++---------
 tools/perf/util/hist.c                       |  2 +-
 tools/perf/util/mem2node.c                   |  2 +-
 tools/perf/util/pmus.c                       |  2 +-
 tools/perf/util/powerpc-vpadtl.c             |  1 +
 tools/perf/util/probe-event.c                | 17 ++++++++---------
 tools/perf/util/probe-file.c                 |  2 +-
 tools/perf/util/probe-finder.c               |  8 ++++----
 tools/perf/util/session.c                    |  2 +-
 tools/perf/util/srcline.c                    |  1 +
 tools/perf/util/stat-shadow.c                |  1 -
 tools/perf/util/unwind-libunwind-local.c     |  1 -
 tools/perf/util/values.c                     |  8 ++++----
 42 files changed, 73 insertions(+), 94 deletions(-)

diff --git a/tools/perf/arch/arm/util/auxtrace.c b/tools/perf/arch/arm/util/auxtrace.c
index eb6404267f1715a9..27bb14c8b880068a 100644
--- a/tools/perf/arch/arm/util/auxtrace.c
+++ b/tools/perf/arch/arm/util/auxtrace.c
@@ -8,7 +8,7 @@
 #include <errno.h>
 #include <stdbool.h>
 #include <linux/coresight-pmu.h>
-#include <linux/zalloc.h>
+#include <stdlib.h>
 #include <api/fs/fs.h>
 
 #include "../../../util/auxtrace.h"
@@ -27,7 +27,7 @@ static struct perf_pmu **find_all_arm_spe_pmus(int *nr_spes, int *err)
 	/* arm_spe_xxxxxxxxx\0 */
 	char arm_spe_pmu_name[sizeof(ARM_SPE_PMU_NAME) + 10];
 
-	arm_spe_pmus = zalloc(sizeof(struct perf_pmu *) * nr_cpus);
+	arm_spe_pmus = calloc(nr_cpus, sizeof(struct perf_pmu *));
 	if (!arm_spe_pmus) {
 		pr_err("spes alloc failed\n");
 		*err = -ENOMEM;
@@ -79,7 +79,7 @@ static struct perf_pmu **find_all_hisi_ptt_pmus(int *nr_ptts, int *err)
 	if (!(*nr_ptts))
 		goto out;
 
-	hisi_ptt_pmus = zalloc(sizeof(struct perf_pmu *) * (*nr_ptts));
+	hisi_ptt_pmus = calloc((*nr_ptts), sizeof(struct perf_pmu *));
 	if (!hisi_ptt_pmus) {
 		pr_err("hisi_ptt alloc failed\n");
 		*err = -ENOMEM;
diff --git a/tools/perf/arch/powerpc/util/auxtrace.c b/tools/perf/arch/powerpc/util/auxtrace.c
index 292ea335e4fff6b9..e39deff6c857a82a 100644
--- a/tools/perf/arch/powerpc/util/auxtrace.c
+++ b/tools/perf/arch/powerpc/util/auxtrace.c
@@ -6,6 +6,7 @@
 #include <linux/kernel.h>
 #include <linux/types.h>
 #include <linux/string.h>
+#include <linux/zalloc.h>
 
 #include "../../util/evlist.h"
 #include "../../util/debug.h"
diff --git a/tools/perf/arch/x86/tests/amd-ibs-period.c b/tools/perf/arch/x86/tests/amd-ibs-period.c
index 223e059e04deb005..cee9e11c05e08c09 100644
--- a/tools/perf/arch/x86/tests/amd-ibs-period.c
+++ b/tools/perf/arch/x86/tests/amd-ibs-period.c
@@ -8,7 +8,6 @@
 
 #include "arch-tests.h"
 #include "linux/perf_event.h"
-#include "linux/zalloc.h"
 #include "tests/tests.h"
 #include "../perf-sys.h"
 #include "pmu.h"
@@ -60,7 +59,7 @@ static int dummy_workload_1(unsigned long count)
 		0xcc, /* int 3 */
 	};
 
-	p = zalloc(2 * page_size);
+	p = calloc(2, page_size);
 	if (!p) {
 		printf("malloc() failed. %m");
 		return 1;
diff --git a/tools/perf/arch/x86/tests/dwarf-unwind.c b/tools/perf/arch/x86/tests/dwarf-unwind.c
index e91a73d09cecfd83..99d2b7ed016fd63f 100644
--- a/tools/perf/arch/x86/tests/dwarf-unwind.c
+++ b/tools/perf/arch/x86/tests/dwarf-unwind.c
@@ -54,22 +54,13 @@ int test__arch_unwind_sample(struct perf_sample *sample,
 			     struct thread *thread)
 {
 	struct regs_dump *regs = perf_sample__user_regs(sample);
-	u64 *buf;
+	u64 *buf = calloc(PERF_REGS_MAX, sizeof(u64));
 
-	buf = malloc(sizeof(u64) * PERF_REGS_MAX);
 	if (!buf) {
 		pr_debug("failed to allocate sample uregs data\n");
 		return -1;
 	}
 
-#ifdef MEMORY_SANITIZER
-	/*
-	 * Assignments to buf in the assembly function perf_regs_load aren't
-	 * seen by memory sanitizer. Zero the memory to convince memory
-	 * sanitizer the memory is initialized.
-	 */
-	memset(buf, 0, sizeof(u64) * PERF_REGS_MAX);
-#endif
 	perf_regs_load(buf);
 	regs->abi  = PERF_SAMPLE_REGS_ABI;
 	regs->regs = buf;
diff --git a/tools/perf/arch/x86/util/pmu.c b/tools/perf/arch/x86/util/pmu.c
index 0661e0f0b02d03cd..7c9d238922a6ce57 100644
--- a/tools/perf/arch/x86/util/pmu.c
+++ b/tools/perf/arch/x86/util/pmu.c
@@ -7,7 +7,6 @@
 #include <linux/stddef.h>
 #include <linux/string.h>
 #include <linux/perf_event.h>
-#include <linux/zalloc.h>
 #include <api/fs/fs.h>
 #include <api/io_dir.h>
 #include <internal/cpumap.h>
diff --git a/tools/perf/bench/numa.c b/tools/perf/bench/numa.c
index 6588a9b0b15aec6d..42d7afc03f9b9ed2 100644
--- a/tools/perf/bench/numa.c
+++ b/tools/perf/bench/numa.c
@@ -32,7 +32,6 @@
 #include <linux/kernel.h>
 #include <linux/time64.h>
 #include <linux/numa.h>
-#include <linux/zalloc.h>
 
 #include "../util/header.h"
 #include "../util/mutex.h"
@@ -980,10 +979,8 @@ static int count_process_nodes(int process_nr)
 	int nodes;
 	int n, t;
 
-	node_present = (char *)malloc(g->p.nr_nodes * sizeof(char));
+	node_present = calloc(g->p.nr_nodes, sizeof(char));
 	BUG_ON(!node_present);
-	for (nodes = 0; nodes < g->p.nr_nodes; nodes++)
-		node_present[nodes] = 0;
 
 	for (t = 0; t < g->p.nr_threads; t++) {
 		struct thread_data *td;
@@ -1090,10 +1087,8 @@ static void calc_convergence(double runtime_ns_max, double *convergence)
 	if (!g->p.show_convergence && !g->p.measure_convergence)
 		return;
 
-	nodes = (int *)malloc(g->p.nr_nodes * sizeof(int));
+	nodes = calloc(g->p.nr_nodes, sizeof(int));
 	BUG_ON(!nodes);
-	for (node = 0; node < g->p.nr_nodes; node++)
-		nodes[node] = 0;
 
 	loops_done_min = -1;
 	loops_done_max = 0;
@@ -1423,7 +1418,7 @@ static void worker_process(int process_nr)
 	bind_to_memnode(td->bind_node);
 	bind_to_cpumask(td->bind_cpumask);
 
-	pthreads = zalloc(g->p.nr_threads * sizeof(pthread_t));
+	pthreads = calloc(g->p.nr_threads, sizeof(pthread_t));
 	process_data = setup_private_data(g->p.bytes_process);
 
 	if (g->p.show_details >= 3) {
@@ -1629,7 +1624,7 @@ static int __bench_numa(const char *name)
 	if (init())
 		return -1;
 
-	pids = zalloc(g->p.nr_proc * sizeof(*pids));
+	pids = calloc(g->p.nr_proc, sizeof(*pids));
 	pid = -1;
 
 	if (g->p.serialize_startup) {
diff --git a/tools/perf/bench/sched-messaging.c b/tools/perf/bench/sched-messaging.c
index 93dcd9dba3d0dcb2..4fb6657fc826c76c 100644
--- a/tools/perf/bench/sched-messaging.c
+++ b/tools/perf/bench/sched-messaging.c
@@ -301,7 +301,7 @@ int bench_sched_messaging(int argc, const char **argv)
 	argc = parse_options(argc, argv, options,
 			     bench_sched_message_usage, 0);
 
-	worker_tab = malloc(num_fds * 2 * num_groups * sizeof(union messaging_worker));
+	worker_tab = calloc(num_fds * 2 * num_groups, sizeof(union messaging_worker));
 	if (!worker_tab)
 		err(EXIT_FAILURE, "main:malloc()");
 
diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c
index 686ad08561d66924..0924f0711a522a9f 100644
--- a/tools/perf/builtin-annotate.c
+++ b/tools/perf/builtin-annotate.c
@@ -13,7 +13,6 @@
 #include <linux/list.h>
 #include "util/cache.h"
 #include <linux/rbtree.h>
-#include <linux/zalloc.h>
 #include "util/symbol.h"
 
 #include "util/debug.h"
diff --git a/tools/perf/builtin-c2c.c b/tools/perf/builtin-c2c.c
index 3ce5f0adec2f31c2..72a7802775ee965f 100644
--- a/tools/perf/builtin-c2c.c
+++ b/tools/perf/builtin-c2c.c
@@ -155,7 +155,7 @@ static void *c2c_he_zalloc(size_t size)
 	if (!c2c_he->nodeset)
 		goto out_free;
 
-	c2c_he->node_stats = zalloc(c2c.nodes_cnt * sizeof(*c2c_he->node_stats));
+	c2c_he->node_stats = calloc(c2c.nodes_cnt, sizeof(*c2c_he->node_stats));
 	if (!c2c_he->node_stats)
 		goto out_free;
 
@@ -2324,13 +2324,13 @@ static int setup_nodes(struct perf_session *session)
 	if (!n)
 		return -EINVAL;
 
-	nodes = zalloc(sizeof(unsigned long *) * c2c.nodes_cnt);
+	nodes = calloc(c2c.nodes_cnt, sizeof(unsigned long *));
 	if (!nodes)
 		return -ENOMEM;
 
 	c2c.nodes = nodes;
 
-	cpu2node = zalloc(sizeof(int) * c2c.cpus_cnt);
+	cpu2node = calloc(c2c.cpus_cnt, sizeof(int));
 	if (!cpu2node)
 		return -ENOMEM;
 
diff --git a/tools/perf/builtin-diff.c b/tools/perf/builtin-diff.c
index e45f0ac1381ab2df..0188b3d7636f6983 100644
--- a/tools/perf/builtin-diff.c
+++ b/tools/perf/builtin-diff.c
@@ -1891,7 +1891,7 @@ static int data_init(int argc, const char **argv)
 		return -EINVAL;
 	}
 
-	data__files = zalloc(sizeof(*data__files) * data__files_cnt);
+	data__files = calloc(data__files_cnt, sizeof(*data__files));
 	if (!data__files)
 		return -ENOMEM;
 
diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
index 4cc33452d79b626b..8a7dbfb14535e2ba 100644
--- a/tools/perf/builtin-ftrace.c
+++ b/tools/perf/builtin-ftrace.c
@@ -20,6 +20,7 @@
 #include <linux/capability.h>
 #include <linux/err.h>
 #include <linux/string.h>
+#include <linux/zalloc.h>
 #include <sys/stat.h>
 
 #include "debug.h"
diff --git a/tools/perf/builtin-kwork.c b/tools/perf/builtin-kwork.c
index 1140e00e874f4eed..9d3a4c779a41e383 100644
--- a/tools/perf/builtin-kwork.c
+++ b/tools/perf/builtin-kwork.c
@@ -2208,7 +2208,7 @@ static int perf_kwork__top(struct perf_kwork *kwork)
 	struct __top_cpus_runtime *cpus_runtime;
 	int ret = 0;
 
-	cpus_runtime = zalloc(sizeof(struct __top_cpus_runtime) * (MAX_NR_CPUS + 1));
+	cpus_runtime = calloc(MAX_NR_CPUS + 1, sizeof(struct __top_cpus_runtime));
 	if (!cpus_runtime)
 		return -1;
 
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index e919d1f021c3cdbf..1adc37b451528798 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -1070,12 +1070,12 @@ static int record__thread_data_init_maps(struct record_thread *thread_data, stru
 		thread_data->nr_mmaps = bitmap_weight(thread_data->mask->maps.bits,
 						      thread_data->mask->maps.nbits);
 	if (mmap) {
-		thread_data->maps = zalloc(thread_data->nr_mmaps * sizeof(struct mmap *));
+		thread_data->maps = calloc(thread_data->nr_mmaps, sizeof(struct mmap *));
 		if (!thread_data->maps)
 			return -ENOMEM;
 	}
 	if (overwrite_mmap) {
-		thread_data->overwrite_maps = zalloc(thread_data->nr_mmaps * sizeof(struct mmap *));
+		thread_data->overwrite_maps = calloc(thread_data->nr_mmaps, sizeof(struct mmap *));
 		if (!thread_data->overwrite_maps) {
 			zfree(&thread_data->maps);
 			return -ENOMEM;
@@ -1220,7 +1220,7 @@ static int record__alloc_thread_data(struct record *rec, struct evlist *evlist)
 	int t, ret;
 	struct record_thread *thread_data;
 
-	rec->thread_data = zalloc(rec->nr_threads * sizeof(*(rec->thread_data)));
+	rec->thread_data = calloc(rec->nr_threads, sizeof(*(rec->thread_data)));
 	if (!rec->thread_data) {
 		pr_err("Failed to allocate thread data\n");
 		return -ENOMEM;
@@ -3710,7 +3710,7 @@ static int record__alloc_thread_masks(struct record *rec, int nr_threads, int nr
 {
 	int t, ret;
 
-	rec->thread_masks = zalloc(nr_threads * sizeof(*(rec->thread_masks)));
+	rec->thread_masks = calloc(nr_threads, sizeof(*(rec->thread_masks)));
 	if (!rec->thread_masks) {
 		pr_err("Failed to allocate thread masks\n");
 		return -ENOMEM;
@@ -3920,7 +3920,7 @@ static int record__init_thread_numa_masks(struct record *rec, struct perf_cpu_ma
 		return -ENOMEM;
 	}
 
-	spec = zalloc(topo->nr * sizeof(char *));
+	spec = calloc(topo->nr, sizeof(char *));
 	if (!spec) {
 		pr_err("Failed to allocate NUMA spec\n");
 		ret = -ENOMEM;
diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index d083e2bb770303a4..150f4c6f42685dbb 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -2405,7 +2405,7 @@ static int init_idle_threads(int ncpu)
 {
 	int i, ret;
 
-	idle_threads = zalloc(ncpu * sizeof(struct thread *));
+	idle_threads = calloc(ncpu, sizeof(struct thread *));
 	if (!idle_threads)
 		return -ENOMEM;
 
@@ -3483,7 +3483,7 @@ static int setup_cpus_switch_event(struct perf_sched *sched)
 	if (!sched->cpu_last_switched)
 		return -1;
 
-	sched->curr_pid = malloc(MAX_CPUS * sizeof(*(sched->curr_pid)));
+	sched->curr_pid = calloc(MAX_CPUS, sizeof(*(sched->curr_pid)));
 	if (!sched->curr_pid) {
 		zfree(&sched->cpu_last_switched);
 		return -1;
@@ -3559,7 +3559,7 @@ static int setup_map_cpus(struct perf_sched *sched)
 	sched->max_cpu.cpu  = sysconf(_SC_NPROCESSORS_CONF);
 
 	if (sched->map.comp) {
-		sched->map.comp_cpus = zalloc(sched->max_cpu.cpu * sizeof(int));
+		sched->map.comp_cpus = calloc(sched->max_cpu.cpu, sizeof(int));
 		if (!sched->map.comp_cpus)
 			return -1;
 	}
diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index 15a58da599581a7b..598e41dbd38edbef 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -3667,7 +3667,7 @@ static int list_available_scripts(const struct option *opt __maybe_unused,
 	struct script_desc *desc;
 	char *script_root;
 
-	buf = malloc(3 * MAXPATHLEN + BUFSIZ);
+	buf = calloc(3, MAXPATHLEN + BUFSIZ);
 	if (!buf) {
 		pr_err("malloc failed\n");
 		exit(-1);
@@ -3819,7 +3819,7 @@ static int has_required_arg(char *script_path)
 
 static int have_cmd(int argc, const char **argv)
 {
-	char **__argv = malloc(sizeof(const char *) * argc);
+	char **__argv = calloc(argc, sizeof(const char *));
 
 	if (!__argv) {
 		pr_err("malloc failed\n");
@@ -4317,7 +4317,7 @@ int cmd_script(int argc, const char **argv)
 				}
 			}
 
-			__argv = malloc((argc + 6) * sizeof(const char *));
+			__argv = calloc(argc + 6, sizeof(const char *));
 			if (!__argv) {
 				pr_err("malloc failed\n");
 				err = -ENOMEM;
@@ -4343,7 +4343,7 @@ int cmd_script(int argc, const char **argv)
 		dup2(live_pipe[0], 0);
 		close(live_pipe[1]);
 
-		__argv = malloc((argc + 4) * sizeof(const char *));
+		__argv = calloc(argc + 4, sizeof(const char *));
 		if (!__argv) {
 			pr_err("malloc failed\n");
 			err = -ENOMEM;
@@ -4381,7 +4381,7 @@ int cmd_script(int argc, const char **argv)
 			}
 		}
 
-		__argv = malloc((argc + 2) * sizeof(const char *));
+		__argv = calloc(argc + 2, sizeof(const char *));
 		if (!__argv) {
 			pr_err("malloc failed\n");
 			err = -ENOMEM;
diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index a24326c44297c534..94b753d25550324f 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -2766,7 +2766,7 @@ int cmd_stat(int argc, const char **argv)
 	}
 
 	if (stat_config.walltime_run_table) {
-		stat_config.walltime_run = zalloc(stat_config.run_count * sizeof(stat_config.walltime_run[0]));
+		stat_config.walltime_run = calloc(stat_config.run_count, sizeof(stat_config.walltime_run[0]));
 		if (!stat_config.walltime_run) {
 			pr_err("failed to setup -r option");
 			goto out;
diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
index f487fbaa0ad60028..8cdfd126115e1b69 100644
--- a/tools/perf/builtin-trace.c
+++ b/tools/perf/builtin-trace.c
@@ -2265,9 +2265,7 @@ static int trace__validate_ev_qualifier(struct trace *trace)
 	struct str_node *pos;
 	size_t nr_used = 0, nr_allocated = strlist__nr_entries(trace->ev_qualifier);
 
-	trace->ev_qualifier_ids.entries = malloc(nr_allocated *
-						 sizeof(trace->ev_qualifier_ids.entries[0]));
-
+	trace->ev_qualifier_ids.entries = calloc(nr_allocated, sizeof(trace->ev_qualifier_ids.entries[0]));
 	if (trace->ev_qualifier_ids.entries == NULL) {
 		fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
 		       trace->output);
diff --git a/tools/perf/jvmti/libjvmti.c b/tools/perf/jvmti/libjvmti.c
index 87bfd4781003a331..d3dc53010e768669 100644
--- a/tools/perf/jvmti/libjvmti.c
+++ b/tools/perf/jvmti/libjvmti.c
@@ -98,7 +98,7 @@ get_line_numbers(jvmtiEnv *jvmti, const void *compile_info, jvmti_line_info_t **
 	/*
 	 * Phase 2 -- allocate big enough line table
 	 */
-	*tab = malloc(nr_total * sizeof(**tab));
+	*tab = calloc(nr_total, sizeof(**tab));
 	if (!*tab)
 		return JVMTI_ERROR_OUT_OF_MEMORY;
 
@@ -262,11 +262,10 @@ compiled_method_load_cb(jvmtiEnv *jvmti,
 			}
 			nr_lines = 0;
 		} else if (nr_lines > 0) {
-			line_file_names = malloc(sizeof(char*) * nr_lines);
+			line_file_names = calloc(nr_lines, sizeof(char *));
 			if (!line_file_names) {
 				warnx("jvmti: cannot allocate space for line table method names");
 			} else {
-				memset(line_file_names, 0, sizeof(char*) * nr_lines);
 				ret = fill_source_filenames(jvmti, nr_lines, line_tab, line_file_names);
 				if (ret != JVMTI_ERROR_NONE) {
 					warnx("jvmti: fill_source_filenames failed");
diff --git a/tools/perf/tests/code-reading.c b/tools/perf/tests/code-reading.c
index 5927d1ea20e22331..47043a3a2fb4f833 100644
--- a/tools/perf/tests/code-reading.c
+++ b/tools/perf/tests/code-reading.c
@@ -4,6 +4,7 @@
 #include <linux/kernel.h>
 #include <linux/rbtree.h>
 #include <linux/types.h>
+#include <linux/zalloc.h>
 #include <inttypes.h>
 #include <stdlib.h>
 #include <unistd.h>
diff --git a/tools/perf/tests/thread-map.c b/tools/perf/tests/thread-map.c
index 54209592168d8aaf..877868107455e87e 100644
--- a/tools/perf/tests/thread-map.c
+++ b/tools/perf/tests/thread-map.c
@@ -9,7 +9,6 @@
 #include "debug.h"
 #include "event.h"
 #include "util/synthetic-events.h"
-#include <linux/zalloc.h>
 #include <perf/event.h>
 #include <internal/threadmap.h>
 
diff --git a/tools/perf/util/annotate-arch/annotate-x86.c b/tools/perf/util/annotate-arch/annotate-x86.c
index c77aabd48ebab693..7e61365363938728 100644
--- a/tools/perf/util/annotate-arch/annotate-x86.c
+++ b/tools/perf/util/annotate-arch/annotate-x86.c
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <string.h>
 #include <linux/compiler.h>
+#include <linux/zalloc.h>
 #include <assert.h>
 #include <inttypes.h>
 #include "../annotate-data.h"
diff --git a/tools/perf/util/bpf-event.c b/tools/perf/util/bpf-event.c
index 67e7786bb878b396..a27945c279efb779 100644
--- a/tools/perf/util/bpf-event.c
+++ b/tools/perf/util/bpf-event.c
@@ -349,7 +349,7 @@ static struct bpf_metadata *bpf_metadata_alloc(__u32 nr_prog_tags,
 	if (!metadata)
 		return NULL;
 
-	metadata->prog_names = zalloc(nr_prog_tags * sizeof(char *));
+	metadata->prog_names = calloc(nr_prog_tags, sizeof(char *));
 	if (!metadata->prog_names) {
 		bpf_metadata_free(metadata);
 		return NULL;
diff --git a/tools/perf/util/bpf_counter_cgroup.c b/tools/perf/util/bpf_counter_cgroup.c
index 5572ceccf86092ff..519fee3dc3d03685 100644
--- a/tools/perf/util/bpf_counter_cgroup.c
+++ b/tools/perf/util/bpf_counter_cgroup.c
@@ -11,7 +11,6 @@
 #include <sys/time.h>
 #include <sys/resource.h>
 #include <linux/err.h>
-#include <linux/zalloc.h>
 #include <linux/perf_event.h>
 #include <api/fs/fs.h>
 #include <bpf/bpf.h>
diff --git a/tools/perf/util/data-convert-bt.c b/tools/perf/util/data-convert-bt.c
index ba1c8e48d4952e4a..d4927d31a7a3a8d9 100644
--- a/tools/perf/util/data-convert-bt.c
+++ b/tools/perf/util/data-convert-bt.c
@@ -1360,7 +1360,7 @@ static int setup_streams(struct ctf_writer *cw, struct perf_session *session)
 	 */
 	ncpus = env->nr_cpus_avail ?: MAX_CPUS;
 
-	stream = zalloc(sizeof(*stream) * ncpus);
+	stream = calloc(ncpus, sizeof(*stream));
 	if (!stream) {
 		pr_err("Failed to allocate streams.\n");
 		return -ENOMEM;
diff --git a/tools/perf/util/data.c b/tools/perf/util/data.c
index 90df41da1a32b315..14fa83dae71a8b80 100644
--- a/tools/perf/util/data.c
+++ b/tools/perf/util/data.c
@@ -43,7 +43,7 @@ int perf_data__create_dir(struct perf_data *data, int nr)
 	if (WARN_ON(!data->is_dir))
 		return -EINVAL;
 
-	files = zalloc(nr * sizeof(*files));
+	files = calloc(nr, sizeof(*files));
 	if (!files)
 		return -ENOMEM;
 
diff --git a/tools/perf/util/db-export.c b/tools/perf/util/db-export.c
index ae9a9065aab76c63..cc2bb1af4243446c 100644
--- a/tools/perf/util/db-export.c
+++ b/tools/perf/util/db-export.c
@@ -19,7 +19,6 @@
 #include "callchain.h"
 #include "call-path.h"
 #include "db-export.h"
-#include <linux/zalloc.h>
 
 int db_export__init(struct db_export *dbe)
 {
diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
index 40fcaed5d0b1a3b8..4f5bd915355242ec 100644
--- a/tools/perf/util/disasm.c
+++ b/tools/perf/util/disasm.c
@@ -13,6 +13,7 @@
 #include <unistd.h>
 
 #include <linux/string.h>
+#include <linux/zalloc.h>
 #include <subcmd/run-command.h>
 
 #include "annotate.h"
diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
index bc045fddf7d57569..66f4843bb235df53 100644
--- a/tools/perf/util/event.c
+++ b/tools/perf/util/event.c
@@ -12,7 +12,6 @@
 #include <unistd.h>
 #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
 #include <linux/perf_event.h>
-#include <linux/zalloc.h>
 #include "cpumap.h"
 #include "dso.h"
 #include "event.h"
diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index c702741a917380b9..73ea382d826364dd 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -825,9 +825,8 @@ static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
 				       bool overwrite)
 {
 	int i;
-	struct mmap *map;
+	struct mmap *map = calloc(evlist->core.nr_mmaps, sizeof(struct mmap));
 
-	map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
 	if (!map)
 		return NULL;
 
diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index 9142a8ba401957c6..a0a5c96288af2674 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -2761,7 +2761,7 @@ static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
 	if (!cmdline)
 		return -1;
 
-	argv = zalloc(sizeof(char *) * (nr + 1));
+	argv = calloc(nr + 1, sizeof(char *));
 	if (!argv)
 		goto error;
 
@@ -2915,7 +2915,7 @@ static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
 	if (do_read_u32(ff, &nr))
 		return -1;
 
-	nodes = zalloc(sizeof(*nodes) * nr);
+	nodes = calloc(nr, sizeof(*nodes));
 	if (!nodes)
 		return -ENOMEM;
 
@@ -3113,7 +3113,7 @@ static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
 	if (do_read_u32(ff, &cnt))
 		return -1;
 
-	caches = zalloc(sizeof(*caches) * cnt);
+	caches = calloc(cnt, sizeof(*caches));
 	if (!caches)
 		return -1;
 
@@ -3195,7 +3195,7 @@ static int process_mem_topology(struct feat_fd *ff,
 	if (do_read_u64(ff, &nr))
 		return -1;
 
-	nodes = zalloc(sizeof(*nodes) * nr);
+	nodes = calloc(nr, sizeof(*nodes));
 	if (!nodes)
 		return -1;
 
@@ -3285,7 +3285,7 @@ static int process_hybrid_topology(struct feat_fd *ff,
 	if (do_read_u32(ff, &nr))
 		return -1;
 
-	nodes = zalloc(sizeof(*nodes) * nr);
+	nodes = calloc(nr, sizeof(*nodes));
 	if (!nodes)
 		return -ENOMEM;
 
@@ -3492,7 +3492,7 @@ static int __process_pmu_caps(struct feat_fd *ff, int *nr_caps,
 	if (!nr_pmu_caps)
 		return 0;
 
-	*caps = zalloc(sizeof(char *) * nr_pmu_caps);
+	*caps = calloc(nr_pmu_caps, sizeof(char *));
 	if (!*caps)
 		return -1;
 
@@ -3569,7 +3569,7 @@ static int process_pmu_caps(struct feat_fd *ff, void *data __maybe_unused)
 		return 0;
 	}
 
-	pmu_caps = zalloc(sizeof(*pmu_caps) * nr_pmu);
+	pmu_caps = calloc(nr_pmu, sizeof(*pmu_caps));
 	if (!pmu_caps)
 		return -ENOMEM;
 
@@ -3622,7 +3622,7 @@ static int process_cpu_domain_info(struct feat_fd *ff, void *data __maybe_unused
 	nra = env->nr_cpus_avail;
 	nr = env->nr_cpus_online;
 
-	cd_map = zalloc(sizeof(*cd_map) * nra);
+	cd_map = calloc(nra, sizeof(*cd_map));
 	if (!cd_map)
 		return -1;
 
@@ -3655,7 +3655,7 @@ static int process_cpu_domain_info(struct feat_fd *ff, void *data __maybe_unused
 
 		cd_map[cpu]->nr_domains = nr_domains;
 
-		cd_map[cpu]->domains = zalloc(sizeof(*d_info) * max_sched_domains);
+		cd_map[cpu]->domains = calloc(max_sched_domains, sizeof(*d_info));
 		if (!cd_map[cpu]->domains)
 			return -1;
 
diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c
index fc737a0a8e4d7acd..747fdc455c80ec0f 100644
--- a/tools/perf/util/hist.c
+++ b/tools/perf/util/hist.c
@@ -1151,7 +1151,7 @@ iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
 	 * cumulated only one time to prevent entries more than 100%
 	 * overhead.
 	 */
-	he_cache = malloc(sizeof(*he_cache) * (cursor->nr + 1));
+	he_cache = calloc(cursor->nr + 1, sizeof(*he_cache));
 	if (he_cache == NULL)
 		return -ENOMEM;
 
diff --git a/tools/perf/util/mem2node.c b/tools/perf/util/mem2node.c
index 03a7d7b2773774a0..51a2292cbf7ef44f 100644
--- a/tools/perf/util/mem2node.c
+++ b/tools/perf/util/mem2node.c
@@ -59,7 +59,7 @@ int mem2node__init(struct mem2node *map, struct perf_env *env)
 		max += bitmap_weight(n->set, n->size);
 	}
 
-	entries = zalloc(sizeof(*entries) * max);
+	entries = calloc(max, sizeof(*entries));
 	if (!entries)
 		return -ENOMEM;
 
diff --git a/tools/perf/util/pmus.c b/tools/perf/util/pmus.c
index 98be2eb8f1f03923..9a2023ceeefd933a 100644
--- a/tools/perf/util/pmus.c
+++ b/tools/perf/util/pmus.c
@@ -621,7 +621,7 @@ void perf_pmus__print_pmu_events(const struct print_callbacks *print_cb, void *p
 	while ((pmu = scan_fn(pmu)) != NULL)
 		len += perf_pmu__num_events(pmu);
 
-	aliases = zalloc(sizeof(struct sevent) * len);
+	aliases = calloc(len, sizeof(struct sevent));
 	if (!aliases) {
 		pr_err("FATAL: not enough memory to print PMU events\n");
 		return;
diff --git a/tools/perf/util/powerpc-vpadtl.c b/tools/perf/util/powerpc-vpadtl.c
index d1c3396f182fdd83..5884ae2ff5fff218 100644
--- a/tools/perf/util/powerpc-vpadtl.c
+++ b/tools/perf/util/powerpc-vpadtl.c
@@ -4,6 +4,7 @@
  */
 
 #include <linux/string.h>
+#include <linux/zalloc.h>
 #include <errno.h>
 #include <inttypes.h>
 #include "color.h"
diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c
index 710e4620923ea8b2..f37a783ea7723197 100644
--- a/tools/perf/util/probe-event.c
+++ b/tools/perf/util/probe-event.c
@@ -1850,7 +1850,7 @@ int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
 
 	/* Copy arguments and ensure return probe has no C argument */
 	pev->nargs = argc - 1;
-	pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
+	pev->args = calloc(pev->nargs, sizeof(struct perf_probe_arg));
 	if (pev->args == NULL) {
 		ret = -ENOMEM;
 		goto out;
@@ -2000,7 +2000,7 @@ int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
 	}
 
 	tev->nargs = argc - 2;
-	tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
+	tev->args = calloc(tev->nargs, sizeof(struct probe_trace_arg));
 	if (tev->args == NULL) {
 		ret = -ENOMEM;
 		goto out;
@@ -2373,7 +2373,7 @@ static int convert_to_perf_probe_event(struct probe_trace_event *tev,
 
 	/* Convert trace_arg to probe_arg */
 	pev->nargs = tev->nargs;
-	pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
+	pev->args = calloc(pev->nargs, sizeof(struct perf_probe_arg));
 	if (pev->args == NULL)
 		return -ENOMEM;
 	for (i = 0; i < tev->nargs && ret >= 0; i++) {
@@ -2480,7 +2480,7 @@ int perf_probe_event__copy(struct perf_probe_event *dst,
 	if (perf_probe_point__copy(&dst->point, &src->point) < 0)
 		goto out_err;
 
-	dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
+	dst->args = calloc(src->nargs, sizeof(struct perf_probe_arg));
 	if (!dst->args)
 		goto out_err;
 	dst->nargs = src->nargs;
@@ -3179,7 +3179,7 @@ static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
 	}
 
 	/* Setup result trace-probe-events */
-	*tevs = zalloc(sizeof(*tev) * num_matched_functions);
+	*tevs = calloc(num_matched_functions, sizeof(*tev));
 	if (!*tevs) {
 		ret = -ENOMEM;
 		goto out;
@@ -3251,8 +3251,7 @@ static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
 		tev->uprobes = pev->uprobes;
 		tev->nargs = pev->nargs;
 		if (tev->nargs) {
-			tev->args = zalloc(sizeof(struct probe_trace_arg) *
-					   tev->nargs);
+			tev->args = calloc(tev->nargs, sizeof(struct probe_trace_arg));
 			if (tev->args == NULL)
 				goto nomem_out;
 		}
@@ -3363,7 +3362,7 @@ static int try_to_find_absolute_address(struct perf_probe_event *pev,
 	}
 
 	tev->nargs = pev->nargs;
-	tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
+	tev->args = calloc(tev->nargs, sizeof(struct probe_trace_arg));
 	if (!tev->args)
 		goto errout;
 
@@ -3549,7 +3548,7 @@ static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
 		goto out;
 	}
 
-	*tevs = zalloc(ret * sizeof(*tev));
+	*tevs = calloc(ret, sizeof(*tev));
 	if (!*tevs) {
 		ret = -ENOMEM;
 		goto out;
diff --git a/tools/perf/util/probe-file.c b/tools/perf/util/probe-file.c
index f78c3bc3d601ea95..4032572cbf55df12 100644
--- a/tools/perf/util/probe-file.c
+++ b/tools/perf/util/probe-file.c
@@ -414,7 +414,7 @@ int probe_cache_entry__get_event(struct probe_cache_entry *entry,
 	if (ret > probe_conf.max_probes)
 		return -E2BIG;
 
-	*tevs = zalloc(ret * sizeof(*tev));
+	*tevs = calloc(ret, sizeof(*tev));
 	if (!*tevs)
 		return -ENOMEM;
 
diff --git a/tools/perf/util/probe-finder.c b/tools/perf/util/probe-finder.c
index 5ffd97ee4898e51e..64328abeef8b2427 100644
--- a/tools/perf/util/probe-finder.c
+++ b/tools/perf/util/probe-finder.c
@@ -1305,7 +1305,7 @@ static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf)
 		 tev->point.offset);
 
 	/* Expand special probe argument if exist */
-	args = zalloc(sizeof(struct perf_probe_arg) * MAX_PROBE_ARGS);
+	args = calloc(MAX_PROBE_ARGS, sizeof(struct perf_probe_arg));
 	if (args == NULL) {
 		ret = -ENOMEM;
 		goto end;
@@ -1316,7 +1316,7 @@ static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf)
 		goto end;
 
 	tev->nargs = ret;
-	tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
+	tev->args = calloc(tev->nargs, sizeof(struct probe_trace_arg));
 	if (tev->args == NULL) {
 		ret = -ENOMEM;
 		goto end;
@@ -1393,7 +1393,7 @@ int debuginfo__find_trace_events(struct debuginfo *dbg,
 	int ret, i;
 
 	/* Allocate result tevs array */
-	*tevs = zalloc(sizeof(struct probe_trace_event) * tf.max_tevs);
+	*tevs = calloc(tf.max_tevs, sizeof(struct probe_trace_event));
 	if (*tevs == NULL)
 		return -ENOMEM;
 
@@ -1566,7 +1566,7 @@ int debuginfo__find_available_vars_at(struct debuginfo *dbg,
 	int ret;
 
 	/* Allocate result vls array */
-	*vls = zalloc(sizeof(struct variable_list) * af.max_vls);
+	*vls = calloc(af.max_vls, sizeof(struct variable_list));
 	if (*vls == NULL)
 		return -ENOMEM;
 
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 09de5288f9e15bb0..7996c4c654b0f5b7 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -2533,7 +2533,7 @@ static int __perf_session__process_dir_events(struct perf_session *session)
 			nr_readers++;
 	}
 
-	rd = zalloc(nr_readers * sizeof(struct reader));
+	rd = calloc(nr_readers, sizeof(struct reader));
 	if (!rd)
 		return -ENOMEM;
 
diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c
index 9be42f3984405f9e..b58710624eadfe1b 100644
--- a/tools/perf/util/srcline.c
+++ b/tools/perf/util/srcline.c
@@ -12,6 +12,7 @@
 #include <inttypes.h>
 #include <string.h>
 #include <linux/string.h>
+#include <linux/zalloc.h>
 
 bool srcline_full_filename;
 
diff --git a/tools/perf/util/stat-shadow.c b/tools/perf/util/stat-shadow.c
index 59d2cd4f2188de72..bc2d44df7bafa4d5 100644
--- a/tools/perf/util/stat-shadow.c
+++ b/tools/perf/util/stat-shadow.c
@@ -13,7 +13,6 @@
 #include "metricgroup.h"
 #include "cgroup.h"
 #include "units.h"
-#include <linux/zalloc.h>
 #include "iostat.h"
 #include "util/hashmap.h"
 #include "tool_pmu.h"
diff --git a/tools/perf/util/unwind-libunwind-local.c b/tools/perf/util/unwind-libunwind-local.c
index 5b39ce21e33351f8..87d496e9dfa6669c 100644
--- a/tools/perf/util/unwind-libunwind-local.c
+++ b/tools/perf/util/unwind-libunwind-local.c
@@ -25,7 +25,6 @@
 #include <unistd.h>
 #include <sys/mman.h>
 #include <linux/list.h>
-#include <linux/zalloc.h>
 #ifndef REMOTE_UNWIND_LIBUNWIND
 #include <libunwind.h>
 #include <libunwind-ptrace.h>
diff --git a/tools/perf/util/values.c b/tools/perf/util/values.c
index ec72d29f3d586d47..6eaddfcf833e52a2 100644
--- a/tools/perf/util/values.c
+++ b/tools/perf/util/values.c
@@ -13,9 +13,9 @@
 int perf_read_values_init(struct perf_read_values *values)
 {
 	values->threads_max = 16;
-	values->pid = malloc(values->threads_max * sizeof(*values->pid));
-	values->tid = malloc(values->threads_max * sizeof(*values->tid));
-	values->value = zalloc(values->threads_max * sizeof(*values->value));
+	values->pid = calloc(values->threads_max, sizeof(*values->pid));
+	values->tid = calloc(values->threads_max, sizeof(*values->tid));
+	values->value = calloc(values->threads_max, sizeof(*values->value));
 	if (!values->pid || !values->tid || !values->value) {
 		pr_debug("failed to allocate read_values threads arrays");
 		goto out_free_pid;
@@ -96,7 +96,7 @@ static int perf_read_values__findnew_thread(struct perf_read_values *values,
 
 	i = values->threads;
 
-	values->value[i] = zalloc(values->counters_max * sizeof(**values->value));
+	values->value[i] = calloc(values->counters_max, sizeof(**values->value));
 	if (!values->value[i]) {
 		pr_debug("failed to allocate read_values counters array");
 		return -ENOMEM;
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 4/4] perf symbol: Add missing libgen.h include to get basename() prototype
  2026-04-01 21:53 [PATCHES v2 perf-tools-next 0/4] Cleanups and a fix Arnaldo Carvalho de Melo
                   ` (2 preceding siblings ...)
  2026-04-01 21:53 ` [PATCH 3/4] perf tools: Use calloc() were applicable Arnaldo Carvalho de Melo
@ 2026-04-01 21:53 ` Arnaldo Carvalho de Melo
  3 siblings, 0 replies; 14+ messages in thread
From: Arnaldo Carvalho de Melo @ 2026-04-01 21:53 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Ingo Molnar, Thomas Gleixner, James Clark, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Kan Liang, Clark Williams, linux-kernel,
	linux-perf-users, Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <acme@redhat.com>

This works in some places because there are tricks to prefer a GNU
version instead of the XPG version, but started breaking in some musl
libc systems due to some unrelated change that made libgen.h stop being
included by chance.

Since symbol.h is using basename(), make it include libgen.h.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/util/symbol.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index c67814d6d6d6f64a..b37ecc2e90f77efe 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -10,6 +10,7 @@
 #include <linux/rbtree.h>
 #include <stdio.h>
 #include <errno.h>
+#include <libgen.h>
 #include "addr_location.h"
 #include "path.h"
 #include "symbol_conf.h"
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCH 3/4] perf tools: Use calloc() were applicable
  2026-04-01 21:53 ` [PATCH 3/4] perf tools: Use calloc() were applicable Arnaldo Carvalho de Melo
@ 2026-04-01 22:14   ` Ian Rogers
  2026-04-01 23:15     ` Arnaldo Melo
  0 siblings, 1 reply; 14+ messages in thread
From: Ian Rogers @ 2026-04-01 22:14 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Namhyung Kim, Ingo Molnar, Thomas Gleixner, James Clark,
	Jiri Olsa, Adrian Hunter, Kan Liang, Clark Williams, linux-kernel,
	linux-perf-users, Arnaldo Carvalho de Melo

On Wed, Apr 1, 2026 at 2:53 PM Arnaldo Carvalho de Melo <acme@kernel.org> wrote:
>
> From: Arnaldo Carvalho de Melo <acme@redhat.com>
>
> Instead of using zalloc(nr_entries * sizeof_entry) that is what calloc()
> does.
>
> In some places where linux/zalloc.h isn't needed, remove it, add when
> needed and was getting it indirectly.
>
> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
> ---
>  tools/perf/arch/arm/util/auxtrace.c          |  6 +++---
>  tools/perf/arch/powerpc/util/auxtrace.c      |  1 +
>  tools/perf/arch/x86/tests/amd-ibs-period.c   |  3 +--
>  tools/perf/arch/x86/tests/dwarf-unwind.c     | 11 +----------
>  tools/perf/arch/x86/util/pmu.c               |  1 -
>  tools/perf/bench/numa.c                      | 13 ++++---------
>  tools/perf/bench/sched-messaging.c           |  2 +-
>  tools/perf/builtin-annotate.c                |  1 -
>  tools/perf/builtin-c2c.c                     |  6 +++---
>  tools/perf/builtin-diff.c                    |  2 +-
>  tools/perf/builtin-ftrace.c                  |  1 +
>  tools/perf/builtin-kwork.c                   |  2 +-
>  tools/perf/builtin-record.c                  | 10 +++++-----
>  tools/perf/builtin-sched.c                   |  6 +++---
>  tools/perf/builtin-script.c                  | 10 +++++-----
>  tools/perf/builtin-stat.c                    |  2 +-
>  tools/perf/builtin-trace.c                   |  4 +---
>  tools/perf/jvmti/libjvmti.c                  |  5 ++---
>  tools/perf/tests/code-reading.c              |  1 +
>  tools/perf/tests/thread-map.c                |  1 -
>  tools/perf/util/annotate-arch/annotate-x86.c |  1 +
>  tools/perf/util/bpf-event.c                  |  2 +-
>  tools/perf/util/bpf_counter_cgroup.c         |  1 -
>  tools/perf/util/data-convert-bt.c            |  2 +-
>  tools/perf/util/data.c                       |  2 +-
>  tools/perf/util/db-export.c                  |  1 -
>  tools/perf/util/disasm.c                     |  1 +
>  tools/perf/util/event.c                      |  1 -
>  tools/perf/util/evlist.c                     |  3 +--
>  tools/perf/util/header.c                     | 18 +++++++++---------
>  tools/perf/util/hist.c                       |  2 +-
>  tools/perf/util/mem2node.c                   |  2 +-
>  tools/perf/util/pmus.c                       |  2 +-
>  tools/perf/util/powerpc-vpadtl.c             |  1 +
>  tools/perf/util/probe-event.c                | 17 ++++++++---------
>  tools/perf/util/probe-file.c                 |  2 +-
>  tools/perf/util/probe-finder.c               |  8 ++++----
>  tools/perf/util/session.c                    |  2 +-
>  tools/perf/util/srcline.c                    |  1 +
>  tools/perf/util/stat-shadow.c                |  1 -
>  tools/perf/util/unwind-libunwind-local.c     |  1 -
>  tools/perf/util/values.c                     |  8 ++++----
>  42 files changed, 73 insertions(+), 94 deletions(-)
>
> diff --git a/tools/perf/arch/arm/util/auxtrace.c b/tools/perf/arch/arm/util/auxtrace.c
> index eb6404267f1715a9..27bb14c8b880068a 100644
> --- a/tools/perf/arch/arm/util/auxtrace.c
> +++ b/tools/perf/arch/arm/util/auxtrace.c
> @@ -8,7 +8,7 @@
>  #include <errno.h>
>  #include <stdbool.h>
>  #include <linux/coresight-pmu.h>
> -#include <linux/zalloc.h>
> +#include <stdlib.h>
>  #include <api/fs/fs.h>
>
>  #include "../../../util/auxtrace.h"
> @@ -27,7 +27,7 @@ static struct perf_pmu **find_all_arm_spe_pmus(int *nr_spes, int *err)
>         /* arm_spe_xxxxxxxxx\0 */
>         char arm_spe_pmu_name[sizeof(ARM_SPE_PMU_NAME) + 10];
>
> -       arm_spe_pmus = zalloc(sizeof(struct perf_pmu *) * nr_cpus);
> +       arm_spe_pmus = calloc(nr_cpus, sizeof(struct perf_pmu *));
>         if (!arm_spe_pmus) {
>                 pr_err("spes alloc failed\n");
>                 *err = -ENOMEM;
> @@ -79,7 +79,7 @@ static struct perf_pmu **find_all_hisi_ptt_pmus(int *nr_ptts, int *err)
>         if (!(*nr_ptts))
>                 goto out;
>
> -       hisi_ptt_pmus = zalloc(sizeof(struct perf_pmu *) * (*nr_ptts));
> +       hisi_ptt_pmus = calloc((*nr_ptts), sizeof(struct perf_pmu *));
>         if (!hisi_ptt_pmus) {
>                 pr_err("hisi_ptt alloc failed\n");
>                 *err = -ENOMEM;
> diff --git a/tools/perf/arch/powerpc/util/auxtrace.c b/tools/perf/arch/powerpc/util/auxtrace.c
> index 292ea335e4fff6b9..e39deff6c857a82a 100644
> --- a/tools/perf/arch/powerpc/util/auxtrace.c
> +++ b/tools/perf/arch/powerpc/util/auxtrace.c
> @@ -6,6 +6,7 @@
>  #include <linux/kernel.h>
>  #include <linux/types.h>
>  #include <linux/string.h>
> +#include <linux/zalloc.h>
>
>  #include "../../util/evlist.h"
>  #include "../../util/debug.h"
> diff --git a/tools/perf/arch/x86/tests/amd-ibs-period.c b/tools/perf/arch/x86/tests/amd-ibs-period.c
> index 223e059e04deb005..cee9e11c05e08c09 100644
> --- a/tools/perf/arch/x86/tests/amd-ibs-period.c
> +++ b/tools/perf/arch/x86/tests/amd-ibs-period.c
> @@ -8,7 +8,6 @@
>
>  #include "arch-tests.h"
>  #include "linux/perf_event.h"
> -#include "linux/zalloc.h"
>  #include "tests/tests.h"
>  #include "../perf-sys.h"
>  #include "pmu.h"
> @@ -60,7 +59,7 @@ static int dummy_workload_1(unsigned long count)
>                 0xcc, /* int 3 */
>         };
>
> -       p = zalloc(2 * page_size);
> +       p = calloc(2, page_size);
>         if (!p) {
>                 printf("malloc() failed. %m");
>                 return 1;
> diff --git a/tools/perf/arch/x86/tests/dwarf-unwind.c b/tools/perf/arch/x86/tests/dwarf-unwind.c
> index e91a73d09cecfd83..99d2b7ed016fd63f 100644
> --- a/tools/perf/arch/x86/tests/dwarf-unwind.c
> +++ b/tools/perf/arch/x86/tests/dwarf-unwind.c
> @@ -54,22 +54,13 @@ int test__arch_unwind_sample(struct perf_sample *sample,
>                              struct thread *thread)
>  {
>         struct regs_dump *regs = perf_sample__user_regs(sample);
> -       u64 *buf;
> +       u64 *buf = calloc(PERF_REGS_MAX, sizeof(u64));
>
> -       buf = malloc(sizeof(u64) * PERF_REGS_MAX);
>         if (!buf) {
>                 pr_debug("failed to allocate sample uregs data\n");
>                 return -1;
>         }
>
> -#ifdef MEMORY_SANITIZER
> -       /*
> -        * Assignments to buf in the assembly function perf_regs_load aren't
> -        * seen by memory sanitizer. Zero the memory to convince memory
> -        * sanitizer the memory is initialized.
> -        */
> -       memset(buf, 0, sizeof(u64) * PERF_REGS_MAX);
> -#endif
>         perf_regs_load(buf);
>         regs->abi  = PERF_SAMPLE_REGS_ABI;
>         regs->regs = buf;
> diff --git a/tools/perf/arch/x86/util/pmu.c b/tools/perf/arch/x86/util/pmu.c
> index 0661e0f0b02d03cd..7c9d238922a6ce57 100644
> --- a/tools/perf/arch/x86/util/pmu.c
> +++ b/tools/perf/arch/x86/util/pmu.c
> @@ -7,7 +7,6 @@
>  #include <linux/stddef.h>
>  #include <linux/string.h>
>  #include <linux/perf_event.h>
> -#include <linux/zalloc.h>
>  #include <api/fs/fs.h>
>  #include <api/io_dir.h>
>  #include <internal/cpumap.h>
> diff --git a/tools/perf/bench/numa.c b/tools/perf/bench/numa.c
> index 6588a9b0b15aec6d..42d7afc03f9b9ed2 100644
> --- a/tools/perf/bench/numa.c
> +++ b/tools/perf/bench/numa.c
> @@ -32,7 +32,6 @@
>  #include <linux/kernel.h>
>  #include <linux/time64.h>
>  #include <linux/numa.h>
> -#include <linux/zalloc.h>
>
>  #include "../util/header.h"
>  #include "../util/mutex.h"
> @@ -980,10 +979,8 @@ static int count_process_nodes(int process_nr)
>         int nodes;
>         int n, t;
>
> -       node_present = (char *)malloc(g->p.nr_nodes * sizeof(char));
> +       node_present = calloc(g->p.nr_nodes, sizeof(char));
>         BUG_ON(!node_present);
> -       for (nodes = 0; nodes < g->p.nr_nodes; nodes++)
> -               node_present[nodes] = 0;
>
>         for (t = 0; t < g->p.nr_threads; t++) {
>                 struct thread_data *td;
> @@ -1090,10 +1087,8 @@ static void calc_convergence(double runtime_ns_max, double *convergence)
>         if (!g->p.show_convergence && !g->p.measure_convergence)
>                 return;
>
> -       nodes = (int *)malloc(g->p.nr_nodes * sizeof(int));
> +       nodes = calloc(g->p.nr_nodes, sizeof(int));
>         BUG_ON(!nodes);
> -       for (node = 0; node < g->p.nr_nodes; node++)
> -               nodes[node] = 0;
>
>         loops_done_min = -1;
>         loops_done_max = 0;
> @@ -1423,7 +1418,7 @@ static void worker_process(int process_nr)
>         bind_to_memnode(td->bind_node);
>         bind_to_cpumask(td->bind_cpumask);
>
> -       pthreads = zalloc(g->p.nr_threads * sizeof(pthread_t));
> +       pthreads = calloc(g->p.nr_threads, sizeof(pthread_t));
>         process_data = setup_private_data(g->p.bytes_process);
>
>         if (g->p.show_details >= 3) {
> @@ -1629,7 +1624,7 @@ static int __bench_numa(const char *name)
>         if (init())
>                 return -1;
>
> -       pids = zalloc(g->p.nr_proc * sizeof(*pids));
> +       pids = calloc(g->p.nr_proc, sizeof(*pids));
>         pid = -1;
>
>         if (g->p.serialize_startup) {
> diff --git a/tools/perf/bench/sched-messaging.c b/tools/perf/bench/sched-messaging.c
> index 93dcd9dba3d0dcb2..4fb6657fc826c76c 100644
> --- a/tools/perf/bench/sched-messaging.c
> +++ b/tools/perf/bench/sched-messaging.c
> @@ -301,7 +301,7 @@ int bench_sched_messaging(int argc, const char **argv)
>         argc = parse_options(argc, argv, options,
>                              bench_sched_message_usage, 0);
>
> -       worker_tab = malloc(num_fds * 2 * num_groups * sizeof(union messaging_worker));
> +       worker_tab = calloc(num_fds * 2 * num_groups, sizeof(union messaging_worker));
>         if (!worker_tab)
>                 err(EXIT_FAILURE, "main:malloc()");
>
> diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c
> index 686ad08561d66924..0924f0711a522a9f 100644
> --- a/tools/perf/builtin-annotate.c
> +++ b/tools/perf/builtin-annotate.c
> @@ -13,7 +13,6 @@
>  #include <linux/list.h>
>  #include "util/cache.h"
>  #include <linux/rbtree.h>
> -#include <linux/zalloc.h>
>  #include "util/symbol.h"
>
>  #include "util/debug.h"
> diff --git a/tools/perf/builtin-c2c.c b/tools/perf/builtin-c2c.c
> index 3ce5f0adec2f31c2..72a7802775ee965f 100644
> --- a/tools/perf/builtin-c2c.c
> +++ b/tools/perf/builtin-c2c.c
> @@ -155,7 +155,7 @@ static void *c2c_he_zalloc(size_t size)
>         if (!c2c_he->nodeset)
>                 goto out_free;
>
> -       c2c_he->node_stats = zalloc(c2c.nodes_cnt * sizeof(*c2c_he->node_stats));
> +       c2c_he->node_stats = calloc(c2c.nodes_cnt, sizeof(*c2c_he->node_stats));
>         if (!c2c_he->node_stats)
>                 goto out_free;
>
> @@ -2324,13 +2324,13 @@ static int setup_nodes(struct perf_session *session)
>         if (!n)
>                 return -EINVAL;
>
> -       nodes = zalloc(sizeof(unsigned long *) * c2c.nodes_cnt);
> +       nodes = calloc(c2c.nodes_cnt, sizeof(unsigned long *));
>         if (!nodes)
>                 return -ENOMEM;
>
>         c2c.nodes = nodes;
>
> -       cpu2node = zalloc(sizeof(int) * c2c.cpus_cnt);
> +       cpu2node = calloc(c2c.cpus_cnt, sizeof(int));
>         if (!cpu2node)
>                 return -ENOMEM;
>
> diff --git a/tools/perf/builtin-diff.c b/tools/perf/builtin-diff.c
> index e45f0ac1381ab2df..0188b3d7636f6983 100644
> --- a/tools/perf/builtin-diff.c
> +++ b/tools/perf/builtin-diff.c
> @@ -1891,7 +1891,7 @@ static int data_init(int argc, const char **argv)
>                 return -EINVAL;
>         }
>
> -       data__files = zalloc(sizeof(*data__files) * data__files_cnt);
> +       data__files = calloc(data__files_cnt, sizeof(*data__files));
>         if (!data__files)
>                 return -ENOMEM;
>
> diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
> index 4cc33452d79b626b..8a7dbfb14535e2ba 100644
> --- a/tools/perf/builtin-ftrace.c
> +++ b/tools/perf/builtin-ftrace.c
> @@ -20,6 +20,7 @@
>  #include <linux/capability.h>
>  #include <linux/err.h>
>  #include <linux/string.h>
> +#include <linux/zalloc.h>
>  #include <sys/stat.h>
>
>  #include "debug.h"
> diff --git a/tools/perf/builtin-kwork.c b/tools/perf/builtin-kwork.c
> index 1140e00e874f4eed..9d3a4c779a41e383 100644
> --- a/tools/perf/builtin-kwork.c
> +++ b/tools/perf/builtin-kwork.c
> @@ -2208,7 +2208,7 @@ static int perf_kwork__top(struct perf_kwork *kwork)
>         struct __top_cpus_runtime *cpus_runtime;
>         int ret = 0;
>
> -       cpus_runtime = zalloc(sizeof(struct __top_cpus_runtime) * (MAX_NR_CPUS + 1));
> +       cpus_runtime = calloc(MAX_NR_CPUS + 1, sizeof(struct __top_cpus_runtime));
>         if (!cpus_runtime)
>                 return -1;
>
> diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
> index e919d1f021c3cdbf..1adc37b451528798 100644
> --- a/tools/perf/builtin-record.c
> +++ b/tools/perf/builtin-record.c
> @@ -1070,12 +1070,12 @@ static int record__thread_data_init_maps(struct record_thread *thread_data, stru
>                 thread_data->nr_mmaps = bitmap_weight(thread_data->mask->maps.bits,
>                                                       thread_data->mask->maps.nbits);
>         if (mmap) {
> -               thread_data->maps = zalloc(thread_data->nr_mmaps * sizeof(struct mmap *));
> +               thread_data->maps = calloc(thread_data->nr_mmaps, sizeof(struct mmap *));
>                 if (!thread_data->maps)
>                         return -ENOMEM;
>         }
>         if (overwrite_mmap) {
> -               thread_data->overwrite_maps = zalloc(thread_data->nr_mmaps * sizeof(struct mmap *));
> +               thread_data->overwrite_maps = calloc(thread_data->nr_mmaps, sizeof(struct mmap *));
>                 if (!thread_data->overwrite_maps) {
>                         zfree(&thread_data->maps);
>                         return -ENOMEM;
> @@ -1220,7 +1220,7 @@ static int record__alloc_thread_data(struct record *rec, struct evlist *evlist)
>         int t, ret;
>         struct record_thread *thread_data;
>
> -       rec->thread_data = zalloc(rec->nr_threads * sizeof(*(rec->thread_data)));
> +       rec->thread_data = calloc(rec->nr_threads, sizeof(*(rec->thread_data)));
>         if (!rec->thread_data) {
>                 pr_err("Failed to allocate thread data\n");
>                 return -ENOMEM;
> @@ -3710,7 +3710,7 @@ static int record__alloc_thread_masks(struct record *rec, int nr_threads, int nr
>  {
>         int t, ret;
>
> -       rec->thread_masks = zalloc(nr_threads * sizeof(*(rec->thread_masks)));
> +       rec->thread_masks = calloc(nr_threads, sizeof(*(rec->thread_masks)));
>         if (!rec->thread_masks) {
>                 pr_err("Failed to allocate thread masks\n");
>                 return -ENOMEM;
> @@ -3920,7 +3920,7 @@ static int record__init_thread_numa_masks(struct record *rec, struct perf_cpu_ma
>                 return -ENOMEM;
>         }
>
> -       spec = zalloc(topo->nr * sizeof(char *));
> +       spec = calloc(topo->nr, sizeof(char *));
>         if (!spec) {
>                 pr_err("Failed to allocate NUMA spec\n");
>                 ret = -ENOMEM;
> diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
> index d083e2bb770303a4..150f4c6f42685dbb 100644
> --- a/tools/perf/builtin-sched.c
> +++ b/tools/perf/builtin-sched.c
> @@ -2405,7 +2405,7 @@ static int init_idle_threads(int ncpu)
>  {
>         int i, ret;
>
> -       idle_threads = zalloc(ncpu * sizeof(struct thread *));
> +       idle_threads = calloc(ncpu, sizeof(struct thread *));
>         if (!idle_threads)
>                 return -ENOMEM;
>
> @@ -3483,7 +3483,7 @@ static int setup_cpus_switch_event(struct perf_sched *sched)
>         if (!sched->cpu_last_switched)
>                 return -1;
>
> -       sched->curr_pid = malloc(MAX_CPUS * sizeof(*(sched->curr_pid)));
> +       sched->curr_pid = calloc(MAX_CPUS, sizeof(*(sched->curr_pid)));
>         if (!sched->curr_pid) {
>                 zfree(&sched->cpu_last_switched);
>                 return -1;
> @@ -3559,7 +3559,7 @@ static int setup_map_cpus(struct perf_sched *sched)
>         sched->max_cpu.cpu  = sysconf(_SC_NPROCESSORS_CONF);
>
>         if (sched->map.comp) {
> -               sched->map.comp_cpus = zalloc(sched->max_cpu.cpu * sizeof(int));
> +               sched->map.comp_cpus = calloc(sched->max_cpu.cpu, sizeof(int));
>                 if (!sched->map.comp_cpus)
>                         return -1;
>         }
> diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
> index 15a58da599581a7b..598e41dbd38edbef 100644
> --- a/tools/perf/builtin-script.c
> +++ b/tools/perf/builtin-script.c
> @@ -3667,7 +3667,7 @@ static int list_available_scripts(const struct option *opt __maybe_unused,
>         struct script_desc *desc;
>         char *script_root;
>
> -       buf = malloc(3 * MAXPATHLEN + BUFSIZ);
> +       buf = calloc(3, MAXPATHLEN + BUFSIZ);

Sashiko caught this one, although I'd probably just keep it as a malloc/zalloc.

Does this unintentionally over-allocate memory?

The original malloc requested 3 * MAXPATHLEN + BUFSIZ bytes. The updated
calloc requests 3 elements of size MAXPATHLEN + BUFSIZ, which evaluates to
3 * MAXPATHLEN + 3 * BUFSIZ bytes.

Should this be calloc(1, 3 * MAXPATHLEN + BUFSIZ) to maintain the original
allocation size?

Thanks,
Ian

>         if (!buf) {
>                 pr_err("malloc failed\n");
>                 exit(-1);
> @@ -3819,7 +3819,7 @@ static int has_required_arg(char *script_path)
>
>  static int have_cmd(int argc, const char **argv)
>  {
> -       char **__argv = malloc(sizeof(const char *) * argc);
> +       char **__argv = calloc(argc, sizeof(const char *));
>
>         if (!__argv) {
>                 pr_err("malloc failed\n");
> @@ -4317,7 +4317,7 @@ int cmd_script(int argc, const char **argv)
>                                 }
>                         }
>
> -                       __argv = malloc((argc + 6) * sizeof(const char *));
> +                       __argv = calloc(argc + 6, sizeof(const char *));
>                         if (!__argv) {
>                                 pr_err("malloc failed\n");
>                                 err = -ENOMEM;
> @@ -4343,7 +4343,7 @@ int cmd_script(int argc, const char **argv)
>                 dup2(live_pipe[0], 0);
>                 close(live_pipe[1]);
>
> -               __argv = malloc((argc + 4) * sizeof(const char *));
> +               __argv = calloc(argc + 4, sizeof(const char *));
>                 if (!__argv) {
>                         pr_err("malloc failed\n");
>                         err = -ENOMEM;
> @@ -4381,7 +4381,7 @@ int cmd_script(int argc, const char **argv)
>                         }
>                 }
>
> -               __argv = malloc((argc + 2) * sizeof(const char *));
> +               __argv = calloc(argc + 2, sizeof(const char *));
>                 if (!__argv) {
>                         pr_err("malloc failed\n");
>                         err = -ENOMEM;
> diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
> index a24326c44297c534..94b753d25550324f 100644
> --- a/tools/perf/builtin-stat.c
> +++ b/tools/perf/builtin-stat.c
> @@ -2766,7 +2766,7 @@ int cmd_stat(int argc, const char **argv)
>         }
>
>         if (stat_config.walltime_run_table) {
> -               stat_config.walltime_run = zalloc(stat_config.run_count * sizeof(stat_config.walltime_run[0]));
> +               stat_config.walltime_run = calloc(stat_config.run_count, sizeof(stat_config.walltime_run[0]));
>                 if (!stat_config.walltime_run) {
>                         pr_err("failed to setup -r option");
>                         goto out;
> diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
> index f487fbaa0ad60028..8cdfd126115e1b69 100644
> --- a/tools/perf/builtin-trace.c
> +++ b/tools/perf/builtin-trace.c
> @@ -2265,9 +2265,7 @@ static int trace__validate_ev_qualifier(struct trace *trace)
>         struct str_node *pos;
>         size_t nr_used = 0, nr_allocated = strlist__nr_entries(trace->ev_qualifier);
>
> -       trace->ev_qualifier_ids.entries = malloc(nr_allocated *
> -                                                sizeof(trace->ev_qualifier_ids.entries[0]));
> -
> +       trace->ev_qualifier_ids.entries = calloc(nr_allocated, sizeof(trace->ev_qualifier_ids.entries[0]));
>         if (trace->ev_qualifier_ids.entries == NULL) {
>                 fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
>                        trace->output);
> diff --git a/tools/perf/jvmti/libjvmti.c b/tools/perf/jvmti/libjvmti.c
> index 87bfd4781003a331..d3dc53010e768669 100644
> --- a/tools/perf/jvmti/libjvmti.c
> +++ b/tools/perf/jvmti/libjvmti.c
> @@ -98,7 +98,7 @@ get_line_numbers(jvmtiEnv *jvmti, const void *compile_info, jvmti_line_info_t **
>         /*
>          * Phase 2 -- allocate big enough line table
>          */
> -       *tab = malloc(nr_total * sizeof(**tab));
> +       *tab = calloc(nr_total, sizeof(**tab));
>         if (!*tab)
>                 return JVMTI_ERROR_OUT_OF_MEMORY;
>
> @@ -262,11 +262,10 @@ compiled_method_load_cb(jvmtiEnv *jvmti,
>                         }
>                         nr_lines = 0;
>                 } else if (nr_lines > 0) {
> -                       line_file_names = malloc(sizeof(char*) * nr_lines);
> +                       line_file_names = calloc(nr_lines, sizeof(char *));
>                         if (!line_file_names) {
>                                 warnx("jvmti: cannot allocate space for line table method names");
>                         } else {
> -                               memset(line_file_names, 0, sizeof(char*) * nr_lines);
>                                 ret = fill_source_filenames(jvmti, nr_lines, line_tab, line_file_names);
>                                 if (ret != JVMTI_ERROR_NONE) {
>                                         warnx("jvmti: fill_source_filenames failed");
> diff --git a/tools/perf/tests/code-reading.c b/tools/perf/tests/code-reading.c
> index 5927d1ea20e22331..47043a3a2fb4f833 100644
> --- a/tools/perf/tests/code-reading.c
> +++ b/tools/perf/tests/code-reading.c
> @@ -4,6 +4,7 @@
>  #include <linux/kernel.h>
>  #include <linux/rbtree.h>
>  #include <linux/types.h>
> +#include <linux/zalloc.h>
>  #include <inttypes.h>
>  #include <stdlib.h>
>  #include <unistd.h>
> diff --git a/tools/perf/tests/thread-map.c b/tools/perf/tests/thread-map.c
> index 54209592168d8aaf..877868107455e87e 100644
> --- a/tools/perf/tests/thread-map.c
> +++ b/tools/perf/tests/thread-map.c
> @@ -9,7 +9,6 @@
>  #include "debug.h"
>  #include "event.h"
>  #include "util/synthetic-events.h"
> -#include <linux/zalloc.h>
>  #include <perf/event.h>
>  #include <internal/threadmap.h>
>
> diff --git a/tools/perf/util/annotate-arch/annotate-x86.c b/tools/perf/util/annotate-arch/annotate-x86.c
> index c77aabd48ebab693..7e61365363938728 100644
> --- a/tools/perf/util/annotate-arch/annotate-x86.c
> +++ b/tools/perf/util/annotate-arch/annotate-x86.c
> @@ -1,6 +1,7 @@
>  // SPDX-License-Identifier: GPL-2.0
>  #include <string.h>
>  #include <linux/compiler.h>
> +#include <linux/zalloc.h>
>  #include <assert.h>
>  #include <inttypes.h>
>  #include "../annotate-data.h"
> diff --git a/tools/perf/util/bpf-event.c b/tools/perf/util/bpf-event.c
> index 67e7786bb878b396..a27945c279efb779 100644
> --- a/tools/perf/util/bpf-event.c
> +++ b/tools/perf/util/bpf-event.c
> @@ -349,7 +349,7 @@ static struct bpf_metadata *bpf_metadata_alloc(__u32 nr_prog_tags,
>         if (!metadata)
>                 return NULL;
>
> -       metadata->prog_names = zalloc(nr_prog_tags * sizeof(char *));
> +       metadata->prog_names = calloc(nr_prog_tags, sizeof(char *));
>         if (!metadata->prog_names) {
>                 bpf_metadata_free(metadata);
>                 return NULL;
> diff --git a/tools/perf/util/bpf_counter_cgroup.c b/tools/perf/util/bpf_counter_cgroup.c
> index 5572ceccf86092ff..519fee3dc3d03685 100644
> --- a/tools/perf/util/bpf_counter_cgroup.c
> +++ b/tools/perf/util/bpf_counter_cgroup.c
> @@ -11,7 +11,6 @@
>  #include <sys/time.h>
>  #include <sys/resource.h>
>  #include <linux/err.h>
> -#include <linux/zalloc.h>
>  #include <linux/perf_event.h>
>  #include <api/fs/fs.h>
>  #include <bpf/bpf.h>
> diff --git a/tools/perf/util/data-convert-bt.c b/tools/perf/util/data-convert-bt.c
> index ba1c8e48d4952e4a..d4927d31a7a3a8d9 100644
> --- a/tools/perf/util/data-convert-bt.c
> +++ b/tools/perf/util/data-convert-bt.c
> @@ -1360,7 +1360,7 @@ static int setup_streams(struct ctf_writer *cw, struct perf_session *session)
>          */
>         ncpus = env->nr_cpus_avail ?: MAX_CPUS;
>
> -       stream = zalloc(sizeof(*stream) * ncpus);
> +       stream = calloc(ncpus, sizeof(*stream));
>         if (!stream) {
>                 pr_err("Failed to allocate streams.\n");
>                 return -ENOMEM;
> diff --git a/tools/perf/util/data.c b/tools/perf/util/data.c
> index 90df41da1a32b315..14fa83dae71a8b80 100644
> --- a/tools/perf/util/data.c
> +++ b/tools/perf/util/data.c
> @@ -43,7 +43,7 @@ int perf_data__create_dir(struct perf_data *data, int nr)
>         if (WARN_ON(!data->is_dir))
>                 return -EINVAL;
>
> -       files = zalloc(nr * sizeof(*files));
> +       files = calloc(nr, sizeof(*files));
>         if (!files)
>                 return -ENOMEM;
>
> diff --git a/tools/perf/util/db-export.c b/tools/perf/util/db-export.c
> index ae9a9065aab76c63..cc2bb1af4243446c 100644
> --- a/tools/perf/util/db-export.c
> +++ b/tools/perf/util/db-export.c
> @@ -19,7 +19,6 @@
>  #include "callchain.h"
>  #include "call-path.h"
>  #include "db-export.h"
> -#include <linux/zalloc.h>
>
>  int db_export__init(struct db_export *dbe)
>  {
> diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
> index 40fcaed5d0b1a3b8..4f5bd915355242ec 100644
> --- a/tools/perf/util/disasm.c
> +++ b/tools/perf/util/disasm.c
> @@ -13,6 +13,7 @@
>  #include <unistd.h>
>
>  #include <linux/string.h>
> +#include <linux/zalloc.h>
>  #include <subcmd/run-command.h>
>
>  #include "annotate.h"
> diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
> index bc045fddf7d57569..66f4843bb235df53 100644
> --- a/tools/perf/util/event.c
> +++ b/tools/perf/util/event.c
> @@ -12,7 +12,6 @@
>  #include <unistd.h>
>  #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
>  #include <linux/perf_event.h>
> -#include <linux/zalloc.h>
>  #include "cpumap.h"
>  #include "dso.h"
>  #include "event.h"
> diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
> index c702741a917380b9..73ea382d826364dd 100644
> --- a/tools/perf/util/evlist.c
> +++ b/tools/perf/util/evlist.c
> @@ -825,9 +825,8 @@ static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
>                                        bool overwrite)
>  {
>         int i;
> -       struct mmap *map;
> +       struct mmap *map = calloc(evlist->core.nr_mmaps, sizeof(struct mmap));
>
> -       map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
>         if (!map)
>                 return NULL;
>
> diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
> index 9142a8ba401957c6..a0a5c96288af2674 100644
> --- a/tools/perf/util/header.c
> +++ b/tools/perf/util/header.c
> @@ -2761,7 +2761,7 @@ static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
>         if (!cmdline)
>                 return -1;
>
> -       argv = zalloc(sizeof(char *) * (nr + 1));
> +       argv = calloc(nr + 1, sizeof(char *));
>         if (!argv)
>                 goto error;
>
> @@ -2915,7 +2915,7 @@ static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
>         if (do_read_u32(ff, &nr))
>                 return -1;
>
> -       nodes = zalloc(sizeof(*nodes) * nr);
> +       nodes = calloc(nr, sizeof(*nodes));
>         if (!nodes)
>                 return -ENOMEM;
>
> @@ -3113,7 +3113,7 @@ static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
>         if (do_read_u32(ff, &cnt))
>                 return -1;
>
> -       caches = zalloc(sizeof(*caches) * cnt);
> +       caches = calloc(cnt, sizeof(*caches));
>         if (!caches)
>                 return -1;
>
> @@ -3195,7 +3195,7 @@ static int process_mem_topology(struct feat_fd *ff,
>         if (do_read_u64(ff, &nr))
>                 return -1;
>
> -       nodes = zalloc(sizeof(*nodes) * nr);
> +       nodes = calloc(nr, sizeof(*nodes));
>         if (!nodes)
>                 return -1;
>
> @@ -3285,7 +3285,7 @@ static int process_hybrid_topology(struct feat_fd *ff,
>         if (do_read_u32(ff, &nr))
>                 return -1;
>
> -       nodes = zalloc(sizeof(*nodes) * nr);
> +       nodes = calloc(nr, sizeof(*nodes));
>         if (!nodes)
>                 return -ENOMEM;
>
> @@ -3492,7 +3492,7 @@ static int __process_pmu_caps(struct feat_fd *ff, int *nr_caps,
>         if (!nr_pmu_caps)
>                 return 0;
>
> -       *caps = zalloc(sizeof(char *) * nr_pmu_caps);
> +       *caps = calloc(nr_pmu_caps, sizeof(char *));
>         if (!*caps)
>                 return -1;
>
> @@ -3569,7 +3569,7 @@ static int process_pmu_caps(struct feat_fd *ff, void *data __maybe_unused)
>                 return 0;
>         }
>
> -       pmu_caps = zalloc(sizeof(*pmu_caps) * nr_pmu);
> +       pmu_caps = calloc(nr_pmu, sizeof(*pmu_caps));
>         if (!pmu_caps)
>                 return -ENOMEM;
>
> @@ -3622,7 +3622,7 @@ static int process_cpu_domain_info(struct feat_fd *ff, void *data __maybe_unused
>         nra = env->nr_cpus_avail;
>         nr = env->nr_cpus_online;
>
> -       cd_map = zalloc(sizeof(*cd_map) * nra);
> +       cd_map = calloc(nra, sizeof(*cd_map));
>         if (!cd_map)
>                 return -1;
>
> @@ -3655,7 +3655,7 @@ static int process_cpu_domain_info(struct feat_fd *ff, void *data __maybe_unused
>
>                 cd_map[cpu]->nr_domains = nr_domains;
>
> -               cd_map[cpu]->domains = zalloc(sizeof(*d_info) * max_sched_domains);
> +               cd_map[cpu]->domains = calloc(max_sched_domains, sizeof(*d_info));
>                 if (!cd_map[cpu]->domains)
>                         return -1;
>
> diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c
> index fc737a0a8e4d7acd..747fdc455c80ec0f 100644
> --- a/tools/perf/util/hist.c
> +++ b/tools/perf/util/hist.c
> @@ -1151,7 +1151,7 @@ iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
>          * cumulated only one time to prevent entries more than 100%
>          * overhead.
>          */
> -       he_cache = malloc(sizeof(*he_cache) * (cursor->nr + 1));
> +       he_cache = calloc(cursor->nr + 1, sizeof(*he_cache));
>         if (he_cache == NULL)
>                 return -ENOMEM;
>
> diff --git a/tools/perf/util/mem2node.c b/tools/perf/util/mem2node.c
> index 03a7d7b2773774a0..51a2292cbf7ef44f 100644
> --- a/tools/perf/util/mem2node.c
> +++ b/tools/perf/util/mem2node.c
> @@ -59,7 +59,7 @@ int mem2node__init(struct mem2node *map, struct perf_env *env)
>                 max += bitmap_weight(n->set, n->size);
>         }
>
> -       entries = zalloc(sizeof(*entries) * max);
> +       entries = calloc(max, sizeof(*entries));
>         if (!entries)
>                 return -ENOMEM;
>
> diff --git a/tools/perf/util/pmus.c b/tools/perf/util/pmus.c
> index 98be2eb8f1f03923..9a2023ceeefd933a 100644
> --- a/tools/perf/util/pmus.c
> +++ b/tools/perf/util/pmus.c
> @@ -621,7 +621,7 @@ void perf_pmus__print_pmu_events(const struct print_callbacks *print_cb, void *p
>         while ((pmu = scan_fn(pmu)) != NULL)
>                 len += perf_pmu__num_events(pmu);
>
> -       aliases = zalloc(sizeof(struct sevent) * len);
> +       aliases = calloc(len, sizeof(struct sevent));
>         if (!aliases) {
>                 pr_err("FATAL: not enough memory to print PMU events\n");
>                 return;
> diff --git a/tools/perf/util/powerpc-vpadtl.c b/tools/perf/util/powerpc-vpadtl.c
> index d1c3396f182fdd83..5884ae2ff5fff218 100644
> --- a/tools/perf/util/powerpc-vpadtl.c
> +++ b/tools/perf/util/powerpc-vpadtl.c
> @@ -4,6 +4,7 @@
>   */
>
>  #include <linux/string.h>
> +#include <linux/zalloc.h>
>  #include <errno.h>
>  #include <inttypes.h>
>  #include "color.h"
> diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c
> index 710e4620923ea8b2..f37a783ea7723197 100644
> --- a/tools/perf/util/probe-event.c
> +++ b/tools/perf/util/probe-event.c
> @@ -1850,7 +1850,7 @@ int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
>
>         /* Copy arguments and ensure return probe has no C argument */
>         pev->nargs = argc - 1;
> -       pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
> +       pev->args = calloc(pev->nargs, sizeof(struct perf_probe_arg));
>         if (pev->args == NULL) {
>                 ret = -ENOMEM;
>                 goto out;
> @@ -2000,7 +2000,7 @@ int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
>         }
>
>         tev->nargs = argc - 2;
> -       tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
> +       tev->args = calloc(tev->nargs, sizeof(struct probe_trace_arg));
>         if (tev->args == NULL) {
>                 ret = -ENOMEM;
>                 goto out;
> @@ -2373,7 +2373,7 @@ static int convert_to_perf_probe_event(struct probe_trace_event *tev,
>
>         /* Convert trace_arg to probe_arg */
>         pev->nargs = tev->nargs;
> -       pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
> +       pev->args = calloc(pev->nargs, sizeof(struct perf_probe_arg));
>         if (pev->args == NULL)
>                 return -ENOMEM;
>         for (i = 0; i < tev->nargs && ret >= 0; i++) {
> @@ -2480,7 +2480,7 @@ int perf_probe_event__copy(struct perf_probe_event *dst,
>         if (perf_probe_point__copy(&dst->point, &src->point) < 0)
>                 goto out_err;
>
> -       dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
> +       dst->args = calloc(src->nargs, sizeof(struct perf_probe_arg));
>         if (!dst->args)
>                 goto out_err;
>         dst->nargs = src->nargs;
> @@ -3179,7 +3179,7 @@ static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
>         }
>
>         /* Setup result trace-probe-events */
> -       *tevs = zalloc(sizeof(*tev) * num_matched_functions);
> +       *tevs = calloc(num_matched_functions, sizeof(*tev));
>         if (!*tevs) {
>                 ret = -ENOMEM;
>                 goto out;
> @@ -3251,8 +3251,7 @@ static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
>                 tev->uprobes = pev->uprobes;
>                 tev->nargs = pev->nargs;
>                 if (tev->nargs) {
> -                       tev->args = zalloc(sizeof(struct probe_trace_arg) *
> -                                          tev->nargs);
> +                       tev->args = calloc(tev->nargs, sizeof(struct probe_trace_arg));
>                         if (tev->args == NULL)
>                                 goto nomem_out;
>                 }
> @@ -3363,7 +3362,7 @@ static int try_to_find_absolute_address(struct perf_probe_event *pev,
>         }
>
>         tev->nargs = pev->nargs;
> -       tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
> +       tev->args = calloc(tev->nargs, sizeof(struct probe_trace_arg));
>         if (!tev->args)
>                 goto errout;
>
> @@ -3549,7 +3548,7 @@ static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
>                 goto out;
>         }
>
> -       *tevs = zalloc(ret * sizeof(*tev));
> +       *tevs = calloc(ret, sizeof(*tev));
>         if (!*tevs) {
>                 ret = -ENOMEM;
>                 goto out;
> diff --git a/tools/perf/util/probe-file.c b/tools/perf/util/probe-file.c
> index f78c3bc3d601ea95..4032572cbf55df12 100644
> --- a/tools/perf/util/probe-file.c
> +++ b/tools/perf/util/probe-file.c
> @@ -414,7 +414,7 @@ int probe_cache_entry__get_event(struct probe_cache_entry *entry,
>         if (ret > probe_conf.max_probes)
>                 return -E2BIG;
>
> -       *tevs = zalloc(ret * sizeof(*tev));
> +       *tevs = calloc(ret, sizeof(*tev));
>         if (!*tevs)
>                 return -ENOMEM;
>
> diff --git a/tools/perf/util/probe-finder.c b/tools/perf/util/probe-finder.c
> index 5ffd97ee4898e51e..64328abeef8b2427 100644
> --- a/tools/perf/util/probe-finder.c
> +++ b/tools/perf/util/probe-finder.c
> @@ -1305,7 +1305,7 @@ static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf)
>                  tev->point.offset);
>
>         /* Expand special probe argument if exist */
> -       args = zalloc(sizeof(struct perf_probe_arg) * MAX_PROBE_ARGS);
> +       args = calloc(MAX_PROBE_ARGS, sizeof(struct perf_probe_arg));
>         if (args == NULL) {
>                 ret = -ENOMEM;
>                 goto end;
> @@ -1316,7 +1316,7 @@ static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf)
>                 goto end;
>
>         tev->nargs = ret;
> -       tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
> +       tev->args = calloc(tev->nargs, sizeof(struct probe_trace_arg));
>         if (tev->args == NULL) {
>                 ret = -ENOMEM;
>                 goto end;
> @@ -1393,7 +1393,7 @@ int debuginfo__find_trace_events(struct debuginfo *dbg,
>         int ret, i;
>
>         /* Allocate result tevs array */
> -       *tevs = zalloc(sizeof(struct probe_trace_event) * tf.max_tevs);
> +       *tevs = calloc(tf.max_tevs, sizeof(struct probe_trace_event));
>         if (*tevs == NULL)
>                 return -ENOMEM;
>
> @@ -1566,7 +1566,7 @@ int debuginfo__find_available_vars_at(struct debuginfo *dbg,
>         int ret;
>
>         /* Allocate result vls array */
> -       *vls = zalloc(sizeof(struct variable_list) * af.max_vls);
> +       *vls = calloc(af.max_vls, sizeof(struct variable_list));
>         if (*vls == NULL)
>                 return -ENOMEM;
>
> diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
> index 09de5288f9e15bb0..7996c4c654b0f5b7 100644
> --- a/tools/perf/util/session.c
> +++ b/tools/perf/util/session.c
> @@ -2533,7 +2533,7 @@ static int __perf_session__process_dir_events(struct perf_session *session)
>                         nr_readers++;
>         }
>
> -       rd = zalloc(nr_readers * sizeof(struct reader));
> +       rd = calloc(nr_readers, sizeof(struct reader));
>         if (!rd)
>                 return -ENOMEM;
>
> diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c
> index 9be42f3984405f9e..b58710624eadfe1b 100644
> --- a/tools/perf/util/srcline.c
> +++ b/tools/perf/util/srcline.c
> @@ -12,6 +12,7 @@
>  #include <inttypes.h>
>  #include <string.h>
>  #include <linux/string.h>
> +#include <linux/zalloc.h>
>
>  bool srcline_full_filename;
>
> diff --git a/tools/perf/util/stat-shadow.c b/tools/perf/util/stat-shadow.c
> index 59d2cd4f2188de72..bc2d44df7bafa4d5 100644
> --- a/tools/perf/util/stat-shadow.c
> +++ b/tools/perf/util/stat-shadow.c
> @@ -13,7 +13,6 @@
>  #include "metricgroup.h"
>  #include "cgroup.h"
>  #include "units.h"
> -#include <linux/zalloc.h>
>  #include "iostat.h"
>  #include "util/hashmap.h"
>  #include "tool_pmu.h"
> diff --git a/tools/perf/util/unwind-libunwind-local.c b/tools/perf/util/unwind-libunwind-local.c
> index 5b39ce21e33351f8..87d496e9dfa6669c 100644
> --- a/tools/perf/util/unwind-libunwind-local.c
> +++ b/tools/perf/util/unwind-libunwind-local.c
> @@ -25,7 +25,6 @@
>  #include <unistd.h>
>  #include <sys/mman.h>
>  #include <linux/list.h>
> -#include <linux/zalloc.h>
>  #ifndef REMOTE_UNWIND_LIBUNWIND
>  #include <libunwind.h>
>  #include <libunwind-ptrace.h>
> diff --git a/tools/perf/util/values.c b/tools/perf/util/values.c
> index ec72d29f3d586d47..6eaddfcf833e52a2 100644
> --- a/tools/perf/util/values.c
> +++ b/tools/perf/util/values.c
> @@ -13,9 +13,9 @@
>  int perf_read_values_init(struct perf_read_values *values)
>  {
>         values->threads_max = 16;
> -       values->pid = malloc(values->threads_max * sizeof(*values->pid));
> -       values->tid = malloc(values->threads_max * sizeof(*values->tid));
> -       values->value = zalloc(values->threads_max * sizeof(*values->value));
> +       values->pid = calloc(values->threads_max, sizeof(*values->pid));
> +       values->tid = calloc(values->threads_max, sizeof(*values->tid));
> +       values->value = calloc(values->threads_max, sizeof(*values->value));
>         if (!values->pid || !values->tid || !values->value) {
>                 pr_debug("failed to allocate read_values threads arrays");
>                 goto out_free_pid;
> @@ -96,7 +96,7 @@ static int perf_read_values__findnew_thread(struct perf_read_values *values,
>
>         i = values->threads;
>
> -       values->value[i] = zalloc(values->counters_max * sizeof(**values->value));
> +       values->value[i] = calloc(values->counters_max, sizeof(**values->value));
>         if (!values->value[i]) {
>                 pr_debug("failed to allocate read_values counters array");
>                 return -ENOMEM;
> --
> 2.53.0
>

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 4/4] perf symbol: Add missing libgen.h include to get basename() prototype
  2026-04-01 21:41     ` Arnaldo Melo
@ 2026-04-01 23:10       ` Namhyung Kim
  0 siblings, 0 replies; 14+ messages in thread
From: Namhyung Kim @ 2026-04-01 23:10 UTC (permalink / raw)
  To: Arnaldo Melo
  Cc: Ian Rogers, Arnaldo Carvalho de Melo, Ingo Molnar,
	Thomas Gleixner, James Clark, Jiri Olsa, Adrian Hunter, Kan Liang,
	Clark Williams, linux-kernel, linux-perf-users,
	Arnaldo Carvalho de Melo

On Wed, Apr 01, 2026 at 06:41:28PM -0300, Arnaldo Melo wrote:
> 
> 
> On April 1, 2026 6:08:16 PM GMT-03:00, Ian Rogers <irogers@google.com> wrote:
> >On Wed, Apr 1, 2026 at 2:02 PM Arnaldo Carvalho de Melo <acme@kernel.org> wrote:
> >>
> >> From: Arnaldo Carvalho de Melo <acme@redhat.com>
> >>
> >> This works in some places because there are tricks to prefer a GNU
> >> version instead of the XPG version, but started breaking in some musl
> >> libc systems due to some unrelated change that made libgen.h stop being
> >> included by chance.
> >>
> >> Since symbol.h is using basename(), make it include libgen.h.
> >>
> >> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
> >> ---
> >>  tools/perf/tests/make    | 2 +-
> >>  tools/perf/util/symbol.h | 1 +
> >>  2 files changed, 2 insertions(+), 1 deletion(-)
> >>
> >> diff --git a/tools/perf/tests/make b/tools/perf/tests/make
> >> index 6587dc326d1b272e..e12ff6397077806e 100644
> >> --- a/tools/perf/tests/make
> >> +++ b/tools/perf/tests/make
> >> @@ -148,7 +148,7 @@ run += make_extra_tests
> >>  run += make_no_jevents
> >>  run += make_jevents_all
> >>  run += make_no_bpf_skel
> >> -run += make_gen_vmlinux_h
> >> +#run += make_gen_vmlinux_h
> >
> >This looks like something removed during testing that you meant to put back.
> 
> 
> Yeah, unrelated, I'll send a v2 removing the header dir move that was already merged, as you noted.
> 
> This gen vmlinux test is failing and I still need to figure out, seems like a newer clang is complaining about some struct forward declarations I'm vmlinux.h.

Right, I've seen this too.

Thanks,
Namhyung


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 3/4] perf tools: Use calloc() were applicable
  2026-04-01 22:14   ` Ian Rogers
@ 2026-04-01 23:15     ` Arnaldo Melo
  0 siblings, 0 replies; 14+ messages in thread
From: Arnaldo Melo @ 2026-04-01 23:15 UTC (permalink / raw)
  To: Ian Rogers, Arnaldo Carvalho de Melo
  Cc: Namhyung Kim, Ingo Molnar, Thomas Gleixner, James Clark,
	Jiri Olsa, Adrian Hunter, Kan Liang, Clark Williams, linux-kernel,
	linux-perf-users, Arnaldo Carvalho de Melo



On April 1, 2026 7:14:40 PM GMT-03:00, Ian Rogers <irogers@google.com> wrote:
>On Wed, Apr 1, 2026 at 2:53 PM Arnaldo Carvalho de Melo <acme@kernel.org> wrote:
>>
>> From: Arnaldo Carvalho de Melo <acme@redhat.com>
>>
>> Instead of using zalloc(nr_entries * sizeof_entry) that is what calloc()
>> does.
>>
>> In some places where linux/zalloc.h isn't needed, remove it, add when
>> needed and was getting it indirectly.
>>
>> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
>> ---

>>
>> +++ b/tools/perf/builtin-script.c
>> @@ -3667,7 +3667,7 @@ static int list_available_scripts(const struct option *opt __maybe_unused,
>>         struct script_desc *desc;
>>         char *script_root;
>>
>> -       buf = malloc(3 * MAXPATHLEN + BUFSIZ);
>> +       buf = calloc(3, MAXPATHLEN + BUFSIZ);
>
>Sashiko caught this one, although I'd probably just keep it as a malloc/zalloc.
>
>Does this unintentionally over-allocate memory?
>
>The original malloc requested 3 * MAXPATHLEN + BUFSIZ bytes. The updated
>calloc requests 3 elements of size MAXPATHLEN + BUFSIZ, which evaluates to
>3 * MAXPATHLEN + 3 * BUFSIZ bytes.
>
>Should this be calloc(1, 3 * MAXPATHLEN + BUFSIZ) to maintain the original
>allocation size?

I think we should just leave it as a malloc, will send a V3.

Eagle eyes, sashiko! :-)

- Arnaldo


>Thanks,
>Ian
>
>>         if (!buf) {
>>                 pr_err("malloc failed\n");
>>                 exit(-1);
>> @@ -3819,7 +3819,7 @@ static int has_required_arg(char *script_path)
>>
>>  static int have_cmd(int argc, const char **argv)
>>  {
>> -       char **__argv = malloc(sizeof(const char *) * argc);
>> +       char **__argv = calloc(argc, sizeof(const char *));
>>
>>         if (!__argv) {
>>                 pr_err("malloc failed\n");
>> @@ -4317,7 +4317,7 @@ int cmd_script(int argc, const char **argv)
>>                                 }
>>                         }
>>
>> -                       __argv = malloc((argc + 6) * sizeof(const char *));
>> +                       __argv = calloc(argc + 6, sizeof(const char *));
>>                         if (!__argv) {
>>                                 pr_err("malloc failed\n");
>>                                 err = -ENOMEM;
>> @@ -4343,7 +4343,7 @@ int cmd_script(int argc, const char **argv)
>>                 dup2(live_pipe[0], 0);
>>                 close(live_pipe[1]);
>>
>> -               __argv = malloc((argc + 4) * sizeof(const char *));
>> +               __argv = calloc(argc + 4, sizeof(const char *));
>>                 if (!__argv) {
>>                         pr_err("malloc failed\n");
>>                         err = -ENOMEM;
>> @@ -4381,7 +4381,7 @@ int cmd_script(int argc, const char **argv)
>>                         }
>>                 }
>>
>> -               __argv = malloc((argc + 2) * sizeof(const char *));
>> +               __argv = calloc(argc + 2, sizeof(const char *));
>>                 if (!__argv) {
>>                         pr_err("malloc failed\n");
>>                         err = -ENOMEM;
>> diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
>> index a24326c44297c534..94b753d25550324f 100644
>> --- a/tools/perf/builtin-stat.c
>> +++ b/tools/perf/builtin-stat.c
>> @@ -2766,7 +2766,7 @@ int cmd_stat(int argc, const char **argv)
>>         }
>>
>>         if (stat_config.walltime_run_table) {
>> -               stat_config.walltime_run = zalloc(stat_config.run_count * sizeof(stat_config.walltime_run[0]));
>> +               stat_config.walltime_run = calloc(stat_config.run_count, sizeof(stat_config.walltime_run[0]));
>>                 if (!stat_config.walltime_run) {
>>                         pr_err("failed to setup -r option");
>>                         goto out;
>> diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
>> index f487fbaa0ad60028..8cdfd126115e1b69 100644
>> --- a/tools/perf/builtin-trace.c
>> +++ b/tools/perf/builtin-trace.c
>> @@ -2265,9 +2265,7 @@ static int trace__validate_ev_qualifier(struct trace *trace)
>>         struct str_node *pos;
>>         size_t nr_used = 0, nr_allocated = strlist__nr_entries(trace->ev_qualifier);
>>
>> -       trace->ev_qualifier_ids.entries = malloc(nr_allocated *
>> -                                                sizeof(trace->ev_qualifier_ids.entries[0]));
>> -
>> +       trace->ev_qualifier_ids.entries = calloc(nr_allocated, sizeof(trace->ev_qualifier_ids.entries[0]));
>>         if (trace->ev_qualifier_ids.entries == NULL) {
>>                 fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
>>                        trace->output);
>> diff --git a/tools/perf/jvmti/libjvmti.c b/tools/perf/jvmti/libjvmti.c
>> index 87bfd4781003a331..d3dc53010e768669 100644
>> --- a/tools/perf/jvmti/libjvmti.c
>> +++ b/tools/perf/jvmti/libjvmti.c
>> @@ -98,7 +98,7 @@ get_line_numbers(jvmtiEnv *jvmti, const void *compile_info, jvmti_line_info_t **
>>         /*
>>          * Phase 2 -- allocate big enough line table
>>          */
>> -       *tab = malloc(nr_total * sizeof(**tab));
>> +       *tab = calloc(nr_total, sizeof(**tab));
>>         if (!*tab)
>>                 return JVMTI_ERROR_OUT_OF_MEMORY;
>>
>> @@ -262,11 +262,10 @@ compiled_method_load_cb(jvmtiEnv *jvmti,
>>                         }
>>                         nr_lines = 0;
>>                 } else if (nr_lines > 0) {
>> -                       line_file_names = malloc(sizeof(char*) * nr_lines);
>> +                       line_file_names = calloc(nr_lines, sizeof(char *));
>>                         if (!line_file_names) {
>>                                 warnx("jvmti: cannot allocate space for line table method names");
>>                         } else {
>> -                               memset(line_file_names, 0, sizeof(char*) * nr_lines);
>>                                 ret = fill_source_filenames(jvmti, nr_lines, line_tab, line_file_names);
>>                                 if (ret != JVMTI_ERROR_NONE) {
>>                                         warnx("jvmti: fill_source_filenames failed");
>> diff --git a/tools/perf/tests/code-reading.c b/tools/perf/tests/code-reading.c
>> index 5927d1ea20e22331..47043a3a2fb4f833 100644
>> --- a/tools/perf/tests/code-reading.c
>> +++ b/tools/perf/tests/code-reading.c
>> @@ -4,6 +4,7 @@
>>  #include <linux/kernel.h>
>>  #include <linux/rbtree.h>
>>  #include <linux/types.h>
>> +#include <linux/zalloc.h>
>>  #include <inttypes.h>
>>  #include <stdlib.h>
>>  #include <unistd.h>
>> diff --git a/tools/perf/tests/thread-map.c b/tools/perf/tests/thread-map.c
>> index 54209592168d8aaf..877868107455e87e 100644
>> --- a/tools/perf/tests/thread-map.c
>> +++ b/tools/perf/tests/thread-map.c
>> @@ -9,7 +9,6 @@
>>  #include "debug.h"
>>  #include "event.h"
>>  #include "util/synthetic-events.h"
>> -#include <linux/zalloc.h>
>>  #include <perf/event.h>
>>  #include <internal/threadmap.h>
>>
>> diff --git a/tools/perf/util/annotate-arch/annotate-x86.c b/tools/perf/util/annotate-arch/annotate-x86.c
>> index c77aabd48ebab693..7e61365363938728 100644
>> --- a/tools/perf/util/annotate-arch/annotate-x86.c
>> +++ b/tools/perf/util/annotate-arch/annotate-x86.c
>> @@ -1,6 +1,7 @@
>>  // SPDX-License-Identifier: GPL-2.0
>>  #include <string.h>
>>  #include <linux/compiler.h>
>> +#include <linux/zalloc.h>
>>  #include <assert.h>
>>  #include <inttypes.h>
>>  #include "../annotate-data.h"
>> diff --git a/tools/perf/util/bpf-event.c b/tools/perf/util/bpf-event.c
>> index 67e7786bb878b396..a27945c279efb779 100644
>> --- a/tools/perf/util/bpf-event.c
>> +++ b/tools/perf/util/bpf-event.c
>> @@ -349,7 +349,7 @@ static struct bpf_metadata *bpf_metadata_alloc(__u32 nr_prog_tags,
>>         if (!metadata)
>>                 return NULL;
>>
>> -       metadata->prog_names = zalloc(nr_prog_tags * sizeof(char *));
>> +       metadata->prog_names = calloc(nr_prog_tags, sizeof(char *));
>>         if (!metadata->prog_names) {
>>                 bpf_metadata_free(metadata);
>>                 return NULL;
>> diff --git a/tools/perf/util/bpf_counter_cgroup.c b/tools/perf/util/bpf_counter_cgroup.c
>> index 5572ceccf86092ff..519fee3dc3d03685 100644
>> --- a/tools/perf/util/bpf_counter_cgroup.c
>> +++ b/tools/perf/util/bpf_counter_cgroup.c
>> @@ -11,7 +11,6 @@
>>  #include <sys/time.h>
>>  #include <sys/resource.h>
>>  #include <linux/err.h>
>> -#include <linux/zalloc.h>
>>  #include <linux/perf_event.h>
>>  #include <api/fs/fs.h>
>>  #include <bpf/bpf.h>
>> diff --git a/tools/perf/util/data-convert-bt.c b/tools/perf/util/data-convert-bt.c
>> index ba1c8e48d4952e4a..d4927d31a7a3a8d9 100644
>> --- a/tools/perf/util/data-convert-bt.c
>> +++ b/tools/perf/util/data-convert-bt.c
>> @@ -1360,7 +1360,7 @@ static int setup_streams(struct ctf_writer *cw, struct perf_session *session)
>>          */
>>         ncpus = env->nr_cpus_avail ?: MAX_CPUS;
>>
>> -       stream = zalloc(sizeof(*stream) * ncpus);
>> +       stream = calloc(ncpus, sizeof(*stream));
>>         if (!stream) {
>>                 pr_err("Failed to allocate streams.\n");
>>                 return -ENOMEM;
>> diff --git a/tools/perf/util/data.c b/tools/perf/util/data.c
>> index 90df41da1a32b315..14fa83dae71a8b80 100644
>> --- a/tools/perf/util/data.c
>> +++ b/tools/perf/util/data.c
>> @@ -43,7 +43,7 @@ int perf_data__create_dir(struct perf_data *data, int nr)
>>         if (WARN_ON(!data->is_dir))
>>                 return -EINVAL;
>>
>> -       files = zalloc(nr * sizeof(*files));
>> +       files = calloc(nr, sizeof(*files));
>>         if (!files)
>>                 return -ENOMEM;
>>
>> diff --git a/tools/perf/util/db-export.c b/tools/perf/util/db-export.c
>> index ae9a9065aab76c63..cc2bb1af4243446c 100644
>> --- a/tools/perf/util/db-export.c
>> +++ b/tools/perf/util/db-export.c
>> @@ -19,7 +19,6 @@
>>  #include "callchain.h"
>>  #include "call-path.h"
>>  #include "db-export.h"
>> -#include <linux/zalloc.h>
>>
>>  int db_export__init(struct db_export *dbe)
>>  {
>> diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
>> index 40fcaed5d0b1a3b8..4f5bd915355242ec 100644
>> --- a/tools/perf/util/disasm.c
>> +++ b/tools/perf/util/disasm.c
>> @@ -13,6 +13,7 @@
>>  #include <unistd.h>
>>
>>  #include <linux/string.h>
>> +#include <linux/zalloc.h>
>>  #include <subcmd/run-command.h>
>>
>>  #include "annotate.h"
>> diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
>> index bc045fddf7d57569..66f4843bb235df53 100644
>> --- a/tools/perf/util/event.c
>> +++ b/tools/perf/util/event.c
>> @@ -12,7 +12,6 @@
>>  #include <unistd.h>
>>  #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
>>  #include <linux/perf_event.h>
>> -#include <linux/zalloc.h>
>>  #include "cpumap.h"
>>  #include "dso.h"
>>  #include "event.h"
>> diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
>> index c702741a917380b9..73ea382d826364dd 100644
>> --- a/tools/perf/util/evlist.c
>> +++ b/tools/perf/util/evlist.c
>> @@ -825,9 +825,8 @@ static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
>>                                        bool overwrite)
>>  {
>>         int i;
>> -       struct mmap *map;
>> +       struct mmap *map = calloc(evlist->core.nr_mmaps, sizeof(struct mmap));
>>
>> -       map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
>>         if (!map)
>>                 return NULL;
>>
>> diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
>> index 9142a8ba401957c6..a0a5c96288af2674 100644
>> --- a/tools/perf/util/header.c
>> +++ b/tools/perf/util/header.c
>> @@ -2761,7 +2761,7 @@ static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
>>         if (!cmdline)
>>                 return -1;
>>
>> -       argv = zalloc(sizeof(char *) * (nr + 1));
>> +       argv = calloc(nr + 1, sizeof(char *));
>>         if (!argv)
>>                 goto error;
>>
>> @@ -2915,7 +2915,7 @@ static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
>>         if (do_read_u32(ff, &nr))
>>                 return -1;
>>
>> -       nodes = zalloc(sizeof(*nodes) * nr);
>> +       nodes = calloc(nr, sizeof(*nodes));
>>         if (!nodes)
>>                 return -ENOMEM;
>>
>> @@ -3113,7 +3113,7 @@ static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
>>         if (do_read_u32(ff, &cnt))
>>                 return -1;
>>
>> -       caches = zalloc(sizeof(*caches) * cnt);
>> +       caches = calloc(cnt, sizeof(*caches));
>>         if (!caches)
>>                 return -1;
>>
>> @@ -3195,7 +3195,7 @@ static int process_mem_topology(struct feat_fd *ff,
>>         if (do_read_u64(ff, &nr))
>>                 return -1;
>>
>> -       nodes = zalloc(sizeof(*nodes) * nr);
>> +       nodes = calloc(nr, sizeof(*nodes));
>>         if (!nodes)
>>                 return -1;
>>
>> @@ -3285,7 +3285,7 @@ static int process_hybrid_topology(struct feat_fd *ff,
>>         if (do_read_u32(ff, &nr))
>>                 return -1;
>>
>> -       nodes = zalloc(sizeof(*nodes) * nr);
>> +       nodes = calloc(nr, sizeof(*nodes));
>>         if (!nodes)
>>                 return -ENOMEM;
>>
>> @@ -3492,7 +3492,7 @@ static int __process_pmu_caps(struct feat_fd *ff, int *nr_caps,
>>         if (!nr_pmu_caps)
>>                 return 0;
>>
>> -       *caps = zalloc(sizeof(char *) * nr_pmu_caps);
>> +       *caps = calloc(nr_pmu_caps, sizeof(char *));
>>         if (!*caps)
>>                 return -1;
>>
>> @@ -3569,7 +3569,7 @@ static int process_pmu_caps(struct feat_fd *ff, void *data __maybe_unused)
>>                 return 0;
>>         }
>>
>> -       pmu_caps = zalloc(sizeof(*pmu_caps) * nr_pmu);
>> +       pmu_caps = calloc(nr_pmu, sizeof(*pmu_caps));
>>         if (!pmu_caps)
>>                 return -ENOMEM;
>>
>> @@ -3622,7 +3622,7 @@ static int process_cpu_domain_info(struct feat_fd *ff, void *data __maybe_unused
>>         nra = env->nr_cpus_avail;
>>         nr = env->nr_cpus_online;
>>
>> -       cd_map = zalloc(sizeof(*cd_map) * nra);
>> +       cd_map = calloc(nra, sizeof(*cd_map));
>>         if (!cd_map)
>>                 return -1;
>>
>> @@ -3655,7 +3655,7 @@ static int process_cpu_domain_info(struct feat_fd *ff, void *data __maybe_unused
>>
>>                 cd_map[cpu]->nr_domains = nr_domains;
>>
>> -               cd_map[cpu]->domains = zalloc(sizeof(*d_info) * max_sched_domains);
>> +               cd_map[cpu]->domains = calloc(max_sched_domains, sizeof(*d_info));
>>                 if (!cd_map[cpu]->domains)
>>                         return -1;
>>
>> diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c
>> index fc737a0a8e4d7acd..747fdc455c80ec0f 100644
>> --- a/tools/perf/util/hist.c
>> +++ b/tools/perf/util/hist.c
>> @@ -1151,7 +1151,7 @@ iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
>>          * cumulated only one time to prevent entries more than 100%
>>          * overhead.
>>          */
>> -       he_cache = malloc(sizeof(*he_cache) * (cursor->nr + 1));
>> +       he_cache = calloc(cursor->nr + 1, sizeof(*he_cache));
>>         if (he_cache == NULL)
>>                 return -ENOMEM;
>>
>> diff --git a/tools/perf/util/mem2node.c b/tools/perf/util/mem2node.c
>> index 03a7d7b2773774a0..51a2292cbf7ef44f 100644
>> --- a/tools/perf/util/mem2node.c
>> +++ b/tools/perf/util/mem2node.c
>> @@ -59,7 +59,7 @@ int mem2node__init(struct mem2node *map, struct perf_env *env)
>>                 max += bitmap_weight(n->set, n->size);
>>         }
>>
>> -       entries = zalloc(sizeof(*entries) * max);
>> +       entries = calloc(max, sizeof(*entries));
>>         if (!entries)
>>                 return -ENOMEM;
>>
>> diff --git a/tools/perf/util/pmus.c b/tools/perf/util/pmus.c
>> index 98be2eb8f1f03923..9a2023ceeefd933a 100644
>> --- a/tools/perf/util/pmus.c
>> +++ b/tools/perf/util/pmus.c
>> @@ -621,7 +621,7 @@ void perf_pmus__print_pmu_events(const struct print_callbacks *print_cb, void *p
>>         while ((pmu = scan_fn(pmu)) != NULL)
>>                 len += perf_pmu__num_events(pmu);
>>
>> -       aliases = zalloc(sizeof(struct sevent) * len);
>> +       aliases = calloc(len, sizeof(struct sevent));
>>         if (!aliases) {
>>                 pr_err("FATAL: not enough memory to print PMU events\n");
>>                 return;
>> diff --git a/tools/perf/util/powerpc-vpadtl.c b/tools/perf/util/powerpc-vpadtl.c
>> index d1c3396f182fdd83..5884ae2ff5fff218 100644
>> --- a/tools/perf/util/powerpc-vpadtl.c
>> +++ b/tools/perf/util/powerpc-vpadtl.c
>> @@ -4,6 +4,7 @@
>>   */
>>
>>  #include <linux/string.h>
>> +#include <linux/zalloc.h>
>>  #include <errno.h>
>>  #include <inttypes.h>
>>  #include "color.h"
>> diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c
>> index 710e4620923ea8b2..f37a783ea7723197 100644
>> --- a/tools/perf/util/probe-event.c
>> +++ b/tools/perf/util/probe-event.c
>> @@ -1850,7 +1850,7 @@ int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
>>
>>         /* Copy arguments and ensure return probe has no C argument */
>>         pev->nargs = argc - 1;
>> -       pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
>> +       pev->args = calloc(pev->nargs, sizeof(struct perf_probe_arg));
>>         if (pev->args == NULL) {
>>                 ret = -ENOMEM;
>>                 goto out;
>> @@ -2000,7 +2000,7 @@ int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
>>         }
>>
>>         tev->nargs = argc - 2;
>> -       tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
>> +       tev->args = calloc(tev->nargs, sizeof(struct probe_trace_arg));
>>         if (tev->args == NULL) {
>>                 ret = -ENOMEM;
>>                 goto out;
>> @@ -2373,7 +2373,7 @@ static int convert_to_perf_probe_event(struct probe_trace_event *tev,
>>
>>         /* Convert trace_arg to probe_arg */
>>         pev->nargs = tev->nargs;
>> -       pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
>> +       pev->args = calloc(pev->nargs, sizeof(struct perf_probe_arg));
>>         if (pev->args == NULL)
>>                 return -ENOMEM;
>>         for (i = 0; i < tev->nargs && ret >= 0; i++) {
>> @@ -2480,7 +2480,7 @@ int perf_probe_event__copy(struct perf_probe_event *dst,
>>         if (perf_probe_point__copy(&dst->point, &src->point) < 0)
>>                 goto out_err;
>>
>> -       dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
>> +       dst->args = calloc(src->nargs, sizeof(struct perf_probe_arg));
>>         if (!dst->args)
>>                 goto out_err;
>>         dst->nargs = src->nargs;
>> @@ -3179,7 +3179,7 @@ static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
>>         }
>>
>>         /* Setup result trace-probe-events */
>> -       *tevs = zalloc(sizeof(*tev) * num_matched_functions);
>> +       *tevs = calloc(num_matched_functions, sizeof(*tev));
>>         if (!*tevs) {
>>                 ret = -ENOMEM;
>>                 goto out;
>> @@ -3251,8 +3251,7 @@ static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
>>                 tev->uprobes = pev->uprobes;
>>                 tev->nargs = pev->nargs;
>>                 if (tev->nargs) {
>> -                       tev->args = zalloc(sizeof(struct probe_trace_arg) *
>> -                                          tev->nargs);
>> +                       tev->args = calloc(tev->nargs, sizeof(struct probe_trace_arg));
>>                         if (tev->args == NULL)
>>                                 goto nomem_out;
>>                 }
>> @@ -3363,7 +3362,7 @@ static int try_to_find_absolute_address(struct perf_probe_event *pev,
>>         }
>>
>>         tev->nargs = pev->nargs;
>> -       tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
>> +       tev->args = calloc(tev->nargs, sizeof(struct probe_trace_arg));
>>         if (!tev->args)
>>                 goto errout;
>>
>> @@ -3549,7 +3548,7 @@ static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
>>                 goto out;
>>         }
>>
>> -       *tevs = zalloc(ret * sizeof(*tev));
>> +       *tevs = calloc(ret, sizeof(*tev));
>>         if (!*tevs) {
>>                 ret = -ENOMEM;
>>                 goto out;
>> diff --git a/tools/perf/util/probe-file.c b/tools/perf/util/probe-file.c
>> index f78c3bc3d601ea95..4032572cbf55df12 100644
>> --- a/tools/perf/util/probe-file.c
>> +++ b/tools/perf/util/probe-file.c
>> @@ -414,7 +414,7 @@ int probe_cache_entry__get_event(struct probe_cache_entry *entry,
>>         if (ret > probe_conf.max_probes)
>>                 return -E2BIG;
>>
>> -       *tevs = zalloc(ret * sizeof(*tev));
>> +       *tevs = calloc(ret, sizeof(*tev));
>>         if (!*tevs)
>>                 return -ENOMEM;
>>
>> diff --git a/tools/perf/util/probe-finder.c b/tools/perf/util/probe-finder.c
>> index 5ffd97ee4898e51e..64328abeef8b2427 100644
>> --- a/tools/perf/util/probe-finder.c
>> +++ b/tools/perf/util/probe-finder.c
>> @@ -1305,7 +1305,7 @@ static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf)
>>                  tev->point.offset);
>>
>>         /* Expand special probe argument if exist */
>> -       args = zalloc(sizeof(struct perf_probe_arg) * MAX_PROBE_ARGS);
>> +       args = calloc(MAX_PROBE_ARGS, sizeof(struct perf_probe_arg));
>>         if (args == NULL) {
>>                 ret = -ENOMEM;
>>                 goto end;
>> @@ -1316,7 +1316,7 @@ static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf)
>>                 goto end;
>>
>>         tev->nargs = ret;
>> -       tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
>> +       tev->args = calloc(tev->nargs, sizeof(struct probe_trace_arg));
>>         if (tev->args == NULL) {
>>                 ret = -ENOMEM;
>>                 goto end;
>> @@ -1393,7 +1393,7 @@ int debuginfo__find_trace_events(struct debuginfo *dbg,
>>         int ret, i;
>>
>>         /* Allocate result tevs array */
>> -       *tevs = zalloc(sizeof(struct probe_trace_event) * tf.max_tevs);
>> +       *tevs = calloc(tf.max_tevs, sizeof(struct probe_trace_event));
>>         if (*tevs == NULL)
>>                 return -ENOMEM;
>>
>> @@ -1566,7 +1566,7 @@ int debuginfo__find_available_vars_at(struct debuginfo *dbg,
>>         int ret;
>>
>>         /* Allocate result vls array */
>> -       *vls = zalloc(sizeof(struct variable_list) * af.max_vls);
>> +       *vls = calloc(af.max_vls, sizeof(struct variable_list));
>>         if (*vls == NULL)
>>                 return -ENOMEM;
>>
>> diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
>> index 09de5288f9e15bb0..7996c4c654b0f5b7 100644
>> --- a/tools/perf/util/session.c
>> +++ b/tools/perf/util/session.c
>> @@ -2533,7 +2533,7 @@ static int __perf_session__process_dir_events(struct perf_session *session)
>>                         nr_readers++;
>>         }
>>
>> -       rd = zalloc(nr_readers * sizeof(struct reader));
>> +       rd = calloc(nr_readers, sizeof(struct reader));
>>         if (!rd)
>>                 return -ENOMEM;
>>
>> diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c
>> index 9be42f3984405f9e..b58710624eadfe1b 100644
>> --- a/tools/perf/util/srcline.c
>> +++ b/tools/perf/util/srcline.c
>> @@ -12,6 +12,7 @@
>>  #include <inttypes.h>
>>  #include <string.h>
>>  #include <linux/string.h>
>> +#include <linux/zalloc.h>
>>
>>  bool srcline_full_filename;
>>
>> diff --git a/tools/perf/util/stat-shadow.c b/tools/perf/util/stat-shadow.c
>> index 59d2cd4f2188de72..bc2d44df7bafa4d5 100644
>> --- a/tools/perf/util/stat-shadow.c
>> +++ b/tools/perf/util/stat-shadow.c
>> @@ -13,7 +13,6 @@
>>  #include "metricgroup.h"
>>  #include "cgroup.h"
>>  #include "units.h"
>> -#include <linux/zalloc.h>
>>  #include "iostat.h"
>>  #include "util/hashmap.h"
>>  #include "tool_pmu.h"
>> diff --git a/tools/perf/util/unwind-libunwind-local.c b/tools/perf/util/unwind-libunwind-local.c
>> index 5b39ce21e33351f8..87d496e9dfa6669c 100644
>> --- a/tools/perf/util/unwind-libunwind-local.c
>> +++ b/tools/perf/util/unwind-libunwind-local.c
>> @@ -25,7 +25,6 @@
>>  #include <unistd.h>
>>  #include <sys/mman.h>
>>  #include <linux/list.h>
>> -#include <linux/zalloc.h>
>>  #ifndef REMOTE_UNWIND_LIBUNWIND
>>  #include <libunwind.h>
>>  #include <libunwind-ptrace.h>
>> diff --git a/tools/perf/util/values.c b/tools/perf/util/values.c
>> index ec72d29f3d586d47..6eaddfcf833e52a2 100644
>> --- a/tools/perf/util/values.c
>> +++ b/tools/perf/util/values.c
>> @@ -13,9 +13,9 @@
>>  int perf_read_values_init(struct perf_read_values *values)
>>  {
>>         values->threads_max = 16;
>> -       values->pid = malloc(values->threads_max * sizeof(*values->pid));
>> -       values->tid = malloc(values->threads_max * sizeof(*values->tid));
>> -       values->value = zalloc(values->threads_max * sizeof(*values->value));
>> +       values->pid = calloc(values->threads_max, sizeof(*values->pid));
>> +       values->tid = calloc(values->threads_max, sizeof(*values->tid));
>> +       values->value = calloc(values->threads_max, sizeof(*values->value));
>>         if (!values->pid || !values->tid || !values->value) {
>>                 pr_debug("failed to allocate read_values threads arrays");
>>                 goto out_free_pid;
>> @@ -96,7 +96,7 @@ static int perf_read_values__findnew_thread(struct perf_read_values *values,
>>
>>         i = values->threads;
>>
>> -       values->value[i] = zalloc(values->counters_max * sizeof(**values->value));
>> +       values->value[i] = calloc(values->counters_max, sizeof(**values->value));
>>         if (!values->value[i]) {
>>                 pr_debug("failed to allocate read_values counters array");
>>                 return -ENOMEM;
>> --
>> 2.53.0
>>
>

- Arnaldo

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH 4/4] perf symbol: Add missing libgen.h include to get basename() prototype
  2026-04-02  0:17 [PATCHES v3 perf-tools-next 0/4] Cleanups and a fix Arnaldo Carvalho de Melo
@ 2026-04-02  0:17 ` Arnaldo Carvalho de Melo
  2026-04-02 15:19   ` Ian Rogers
  0 siblings, 1 reply; 14+ messages in thread
From: Arnaldo Carvalho de Melo @ 2026-04-02  0:17 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Ingo Molnar, Thomas Gleixner, James Clark, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Kan Liang, Clark Williams, linux-kernel,
	linux-perf-users, Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <acme@redhat.com>

This works in some places because there are tricks to prefer a GNU
version instead of the XPG version, but started breaking in some musl
libc systems due to some unrelated change that made libgen.h stop being
included by chance.

Since symbol.h is using basename(), make it include libgen.h.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/util/symbol.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index c67814d6d6d6f64a..b37ecc2e90f77efe 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -10,6 +10,7 @@
 #include <linux/rbtree.h>
 #include <stdio.h>
 #include <errno.h>
+#include <libgen.h>
 #include "addr_location.h"
 #include "path.h"
 #include "symbol_conf.h"
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCH 4/4] perf symbol: Add missing libgen.h include to get basename() prototype
  2026-04-02  0:17 ` [PATCH 4/4] perf symbol: Add missing libgen.h include to get basename() prototype Arnaldo Carvalho de Melo
@ 2026-04-02 15:19   ` Ian Rogers
  2026-04-02 15:48     ` Arnaldo Melo
  0 siblings, 1 reply; 14+ messages in thread
From: Ian Rogers @ 2026-04-02 15:19 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Namhyung Kim, Ingo Molnar, Thomas Gleixner, James Clark,
	Jiri Olsa, Adrian Hunter, Kan Liang, Clark Williams, linux-kernel,
	linux-perf-users, Arnaldo Carvalho de Melo

On Wed, Apr 1, 2026 at 5:20 PM Arnaldo Carvalho de Melo <acme@kernel.org> wrote:
>
> From: Arnaldo Carvalho de Melo <acme@redhat.com>
>
> This works in some places because there are tricks to prefer a GNU
> version instead of the XPG version, but started breaking in some musl
> libc systems due to some unrelated change that made libgen.h stop being
> included by chance.
>
> Since symbol.h is using basename(), make it include libgen.h.

Huh, this is being flagged by Sashiko:
"""
Could this include cause segmentation faults by exposing the POSIX version
of basename() to the rest of the tools?

According to the POSIX specification, if the input is an empty string
or slashes, basename() can return a pointer to a statically allocated,
read-only string literal (such as "." or "/").

In tools/perf/util/probe-event.c:convert_exec_to_group(), the code takes
the return value of basename() and modifies it in-place:
    ptr = basename(exec);
    ...
    for (ptr2 = ptr; *ptr2 != '\0'; ptr2++) {
            if (!isalnum(*ptr2) && *ptr2 != '_')
                    *ptr2 = '_';
    }
If a user provides an empty target or "/", the POSIX basename() would
return a read-only string. When the loop attempts to write a null or
underscore into it, wouldn't it trigger an immediate segmentation fault?

Is there a way we can safely duplicate the returned string before modifying it,
or ensure the inputs never resolve to these static strings?
"""

The story of the two basename versions and the issues appears in the
man pages. The whole thing makes me think we should avoid basename :-)

Thanks,
Ian

> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
> ---
>  tools/perf/util/symbol.h | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
> index c67814d6d6d6f64a..b37ecc2e90f77efe 100644
> --- a/tools/perf/util/symbol.h
> +++ b/tools/perf/util/symbol.h
> @@ -10,6 +10,7 @@
>  #include <linux/rbtree.h>
>  #include <stdio.h>
>  #include <errno.h>
> +#include <libgen.h>
>  #include "addr_location.h"
>  #include "path.h"
>  #include "symbol_conf.h"
> --
> 2.53.0
>

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 4/4] perf symbol: Add missing libgen.h include to get basename() prototype
  2026-04-02 15:19   ` Ian Rogers
@ 2026-04-02 15:48     ` Arnaldo Melo
  0 siblings, 0 replies; 14+ messages in thread
From: Arnaldo Melo @ 2026-04-02 15:48 UTC (permalink / raw)
  To: Ian Rogers, Arnaldo Carvalho de Melo
  Cc: Namhyung Kim, Ingo Molnar, Thomas Gleixner, James Clark,
	Jiri Olsa, Adrian Hunter, Kan Liang, Clark Williams, linux-kernel,
	linux-perf-users, Arnaldo Carvalho de Melo



On April 2, 2026 12:19:24 PM GMT-03:00, Ian Rogers <irogers@google.com> wrote:
>On Wed, Apr 1, 2026 at 5:20 PM Arnaldo Carvalho de Melo <acme@kernel.org> wrote:
>>
>> From: Arnaldo Carvalho de Melo <acme@redhat.com>
>>
>> This works in some places because there are tricks to prefer a GNU
>> version instead of the XPG version, but started breaking in some musl
>> libc systems due to some unrelated change that made libgen.h stop being
>> included by chance.
>>
>> Since symbol.h is using basename(), make it include libgen.h.
>
>Huh, this is being flagged by Sashiko:
>"""
>Could this include cause segmentation faults by exposing the POSIX version
>of basename() to the rest of the tools?
>
>According to the POSIX specification, if the input is an empty string
>or slashes, basename() can return a pointer to a statically allocated,
>read-only string literal (such as "." or "/").
>
>In tools/perf/util/probe-event.c:convert_exec_to_group(), the code takes
>the return value of basename() and modifies it in-place:
>    ptr = basename(exec);
>    ...
>    for (ptr2 = ptr; *ptr2 != '\0'; ptr2++) {
>            if (!isalnum(*ptr2) && *ptr2 != '_')
>                    *ptr2 = '_';
>    }
>If a user provides an empty target or "/", the POSIX basename() would
>return a read-only string. When the loop attempts to write a null or
>underscore into it, wouldn't it trigger an immediate segmentation fault?
>
>Is there a way we can safely duplicate the returned string before modifying it,
>or ensure the inputs never resolve to these static strings?
>"""
>
>The story of the two basename versions and the issues appears in the
>man pages. The whole thing makes me think we should avoid basename :-)

:-)

I think we should just follow it's advice and use strdup in it's return, in a helper, and have it's users free it afterwards. 

- Arnaldo

>
>Thanks,
>Ian
>
>> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
>> ---
>>  tools/perf/util/symbol.h | 1 +
>>  1 file changed, 1 insertion(+)
>>
>> diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
>> index c67814d6d6d6f64a..b37ecc2e90f77efe 100644
>> --- a/tools/perf/util/symbol.h
>> +++ b/tools/perf/util/symbol.h
>> @@ -10,6 +10,7 @@
>>  #include <linux/rbtree.h>
>>  #include <stdio.h>
>>  #include <errno.h>
>> +#include <libgen.h>
>>  #include "addr_location.h"
>>  #include "path.h"
>>  #include "symbol_conf.h"
>> --
>> 2.53.0
>>

- Arnaldo

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2026-04-02 15:48 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-01 21:53 [PATCHES v2 perf-tools-next 0/4] Cleanups and a fix Arnaldo Carvalho de Melo
2026-04-01 21:53 ` [PATCH 1/4] perf tools: Make more global variables static Arnaldo Carvalho de Melo
2026-04-01 21:53 ` [PATCH 2/4] perf bench: Constify tables Arnaldo Carvalho de Melo
2026-04-01 21:53 ` [PATCH 3/4] perf tools: Use calloc() were applicable Arnaldo Carvalho de Melo
2026-04-01 22:14   ` Ian Rogers
2026-04-01 23:15     ` Arnaldo Melo
2026-04-01 21:53 ` [PATCH 4/4] perf symbol: Add missing libgen.h include to get basename() prototype Arnaldo Carvalho de Melo
  -- strict thread matches above, loose matches on Subject: below --
2026-04-02  0:17 [PATCHES v3 perf-tools-next 0/4] Cleanups and a fix Arnaldo Carvalho de Melo
2026-04-02  0:17 ` [PATCH 4/4] perf symbol: Add missing libgen.h include to get basename() prototype Arnaldo Carvalho de Melo
2026-04-02 15:19   ` Ian Rogers
2026-04-02 15:48     ` Arnaldo Melo
2026-04-01 21:01 [PATCHES perf-tools-next 0/4] Cleanups and a fix Arnaldo Carvalho de Melo
2026-04-01 21:02 ` [PATCH 4/4] perf symbol: Add missing libgen.h include to get basename() prototype Arnaldo Carvalho de Melo
2026-04-01 21:08   ` Ian Rogers
2026-04-01 21:41     ` Arnaldo Melo
2026-04-01 23:10       ` Namhyung Kim

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox