[PATCHES perf-tools-next v6 0/8] Cleanups and fixes

Arnaldo Carvalho de Melo posted 8 patches 2 months, 1 week ago
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-daemon.c                   |   4 +-
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                   |  10 +-
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/annotate.c                    |   3 +-
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-convert-json.c           |   4 +-
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/dsos.c                        |  32 ++----
tools/perf/util/event.c                       |   1 -
tools/perf/util/evlist.c                      |   3 +-
tools/perf/util/header.c                      |  35 ++++--
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                 |  20 ++--
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                     |  12 +-
tools/perf/util/stat-shadow.c                 |   1 -
tools/perf/util/symbol.c                      |   2 +-
tools/perf/util/symbol.h                      |  16 +--
tools/perf/util/unwind-libunwind-local.c      |   1 -
tools/perf/util/util.c                        |  10 +-
tools/perf/util/util.h                        |   3 +-
tools/perf/util/values.c                      |   8 +-
69 files changed, 251 insertions(+), 280 deletions(-)
[PATCHES perf-tools-next v6 0/8] Cleanups and fixes
Posted by Arnaldo Carvalho de Melo 2 months, 1 week ago
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.

	This also adds checks for perf.data headers that were suggested
by sashiko.

Thanks a lot,

- Arnaldo

v6: thinko, fix subject of cover letter, its 0/8...

v5: Address more comments and add perf.data header checks.

v4: Address some more Sashiko comments, see patch comments for details.

v3: Address https://sashiko.dev review, removed a malloc->calloc
conversion, flawed, and removed the changes to trace-event-scripting.c,
that made the build fail in some situation, see the committer notes
in the patch for more details.

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



Arnaldo Carvalho de Melo (7):
  perf symbols: Make variable receiving result strrchr() const
  perf util: Kill die() prototype, dead for a long time
  perf bench: Constify tables
  perf header: Use a max number of command line args
  perf header: Do validation of perf.data HEADER_CPU_DOMAIN_INFO
  perf tools: Use calloc() where applicable
  perf tools: Replace basename() calls with perf_basename()

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-daemon.c                   |   4 +-
 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                   |  10 +-
 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/annotate.c                    |   3 +-
 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-convert-json.c           |   4 +-
 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/dsos.c                        |  32 ++----
 tools/perf/util/event.c                       |   1 -
 tools/perf/util/evlist.c                      |   3 +-
 tools/perf/util/header.c                      |  35 ++++--
 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                 |  20 ++--
 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                     |  12 +-
 tools/perf/util/stat-shadow.c                 |   1 -
 tools/perf/util/symbol.c                      |   2 +-
 tools/perf/util/symbol.h                      |  16 +--
 tools/perf/util/unwind-libunwind-local.c      |   1 -
 tools/perf/util/util.c                        |  10 +-
 tools/perf/util/util.h                        |   3 +-
 tools/perf/util/values.c                      |   8 +-
 69 files changed, 251 insertions(+), 280 deletions(-)

-- 
2.53.0
Re: [PATCHES perf-tools-next v6 0/8] Cleanups and fixes
Posted by Namhyung Kim 2 months, 1 week ago
On Wed, Apr 08, 2026 at 02:31:55PM -0300, Arnaldo Carvalho de Melo wrote:
> 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.
> 
> 	This also adds checks for perf.data headers that were suggested
> by sashiko.

It seems sashiko keeps creating new issues or finding bugs.  As we
discussed, I'd like to apply this series to fix known things first
and we can address other issues separately.

Thanks,
Namhyung

> 
> Thanks a lot,
> 
> - Arnaldo
> 
> v6: thinko, fix subject of cover letter, its 0/8...
> 
> v5: Address more comments and add perf.data header checks.
> 
> v4: Address some more Sashiko comments, see patch comments for details.
> 
> v3: Address https://sashiko.dev review, removed a malloc->calloc
> conversion, flawed, and removed the changes to trace-event-scripting.c,
> that made the build fail in some situation, see the committer notes
> in the patch for more details.
> 
> v2: remove the commenting of gen_vmlinux in tools/perf/tests/make as
> noticed by Ian Rogers.
> 
> 
> 
> Arnaldo Carvalho de Melo (7):
>   perf symbols: Make variable receiving result strrchr() const
>   perf util: Kill die() prototype, dead for a long time
>   perf bench: Constify tables
>   perf header: Use a max number of command line args
>   perf header: Do validation of perf.data HEADER_CPU_DOMAIN_INFO
>   perf tools: Use calloc() where applicable
>   perf tools: Replace basename() calls with perf_basename()
> 
> 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-daemon.c                   |   4 +-
>  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                   |  10 +-
>  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/annotate.c                    |   3 +-
>  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-convert-json.c           |   4 +-
>  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/dsos.c                        |  32 ++----
>  tools/perf/util/event.c                       |   1 -
>  tools/perf/util/evlist.c                      |   3 +-
>  tools/perf/util/header.c                      |  35 ++++--
>  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                 |  20 ++--
>  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                     |  12 +-
>  tools/perf/util/stat-shadow.c                 |   1 -
>  tools/perf/util/symbol.c                      |   2 +-
>  tools/perf/util/symbol.h                      |  16 +--
>  tools/perf/util/unwind-libunwind-local.c      |   1 -
>  tools/perf/util/util.c                        |  10 +-
>  tools/perf/util/util.h                        |   3 +-
>  tools/perf/util/values.c                      |   8 +-
>  69 files changed, 251 insertions(+), 280 deletions(-)
> 
> -- 
> 2.53.0
>
Re: [PATCHES perf-tools-next v6 0/8] Cleanups and fixes
Posted by Arnaldo Carvalho de Melo 2 months ago
On Wed, Apr 08, 2026 at 07:20:31PM -0700, Namhyung Kim wrote:
> On Wed, Apr 08, 2026 at 02:31:55PM -0300, Arnaldo Carvalho de Melo wrote:
> >         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.

> > 	This also adds checks for perf.data headers that were suggested
> > by sashiko.

> It seems sashiko keeps creating new issues or finding bugs.  As we
> discussed, I'd like to apply this series to fix known things first
> and we can address other issues separately.

Sure, please merge v6 as-is if you think it is ok and I'll come up with
follow up patches addressing more issues, for instance, here are some
more checks I'm writing with the help of Claude:

diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index 22c44b6f0b098f95..2d23dbc666b676be 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -2722,6 +2722,13 @@ static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
 	ret = do_read_u32(ff, &nr_cpus_online);
 	if (ret)
 		return ret;
+
+	if (nr_cpus_online > nr_cpus_avail) {
+		pr_err("Invalid HEADER_NRCPUS: nr_cpus_online (%u) > nr_cpus_avail (%u)\n",
+		       nr_cpus_online, nr_cpus_avail);
+		return -1;
+	}
+
 	env->nr_cpus_avail = (int)nr_cpus_avail;
 	env->nr_cpus_online = (int)nr_cpus_online;
 	return 0;
@@ -3698,6 +3705,17 @@ static int process_cpu_domain_info(struct feat_fd *ff, void *data __maybe_unused
 	nra = env->nr_cpus_avail;
 	nr = env->nr_cpus_online;
 
+	if (nra == 0 || nr == 0) {
+		pr_err("Invalid HEADER_CPU_DOMAIN_INFO: missing HEADER_NRCPUS\n");
+		return -1;
+	}
+
+	if (ff->size < 2 * sizeof(u32) + nr * 2 * sizeof(u32)) {
+		pr_err("Invalid HEADER_CPU_DOMAIN_INFO: section too small (%zu) for %u CPUs\n",
+		       (size_t)ff->size, nr);
+		return -1;
+	}
+
 	cd_map = calloc(nra, sizeof(*cd_map));
 	if (!cd_map)
 		return -1;
@@ -3714,6 +3732,19 @@ static int process_cpu_domain_info(struct feat_fd *ff, void *data __maybe_unused
 	if (ret)
 		return ret;
 
+	/*
+	 * Sanity check: real systems have at most ~10 sched domain levels
+	 * (SMT, CLS, MC, PKG + NUMA hops). Reject obviously bogus values
+	 * from malformed perf.data files before they cause excessive
+	 * allocation in the per-CPU loop.
+	 */
+#define MAX_SCHED_DOMAINS 64
+	if (max_sched_domains > MAX_SCHED_DOMAINS) {
+		pr_err("Invalid HEADER_CPU_DOMAIN_INFO: max_sched_domains %u > %u\n",
+		       max_sched_domains, MAX_SCHED_DOMAINS);
+		return -1;
+	}
+
 	env->max_sched_domains = max_sched_domains;
 
 	for (i = 0; i < nr; i++) {
@@ -3725,6 +3756,11 @@ static int process_cpu_domain_info(struct feat_fd *ff, void *data __maybe_unused
 			return -1;
 		}
 
+		if (cd_map[cpu]) {
+			pr_err("Invalid HEADER_CPU_DOMAIN_INFO: duplicate cpu %u\n", cpu);
+			return -1;
+		}
+
 		cd_map[cpu] = zalloc(sizeof(*cd_map[cpu]));
 		if (!cd_map[cpu])
 			return -1;
@@ -3760,7 +3796,13 @@ static int process_cpu_domain_info(struct feat_fd *ff, void *data __maybe_unused
 			if (!d_info)
 				return -1;
 
-			assert(cd_map[cpu]->domains[domain] == NULL);
+			if (cd_map[cpu]->domains[domain]) {
+				pr_err("Invalid HEADER_CPU_DOMAIN_INFO: duplicate domain %u for cpu %u\n",
+				       domain, cpu);
+				free(d_info);
+				return -1;
+			}
+
 			cd_map[cpu]->domains[domain] = d_info;
 			d_info->domain = domain;
Re: [PATCHES perf-tools-next v6 0/8] Cleanups and fixes
Posted by Namhyung Kim 2 months ago
On Wed, 08 Apr 2026 14:31:55 -0300, Arnaldo Carvalho de Melo wrote:
>         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.
> 
> 	This also adds checks for perf.data headers that were suggested
> by sashiko.
> 
> [...]
Applied to perf-tools-next, thanks!

Best regards,
Namhyung