Cleanups and fixes
Arnaldo Carvalho de Melo
maintainerv5 cover Cleanups and fixes
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
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(-)
Signature · 1 line
2.53.0
Arnaldo Carvalho de Melo
maintainerPATCH 1/8 perf symbols: Make variable receiving result strrchr() const
From: Arnaldo Carvalho de Melo <[email protected]>
Fixing:
util/symbol.c: In function ‘symbol__config_symfs’:
util/symbol.c:2499:20: error: assignment discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
2499 | layout_str = strrchr(dir, ',');
|With recent gcc/glibc.
--- tools/perf/util/symbol.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/symbol.c +1 -1
@@ -2492,7 +2492,7 @@ int symbol__config_symfs(const struct option *opt __maybe_unused, const char *dir, int unset __maybe_unused) { char *bf = NULL; -char *layout_str; +const char *layout_str;char *dir_copy;int ret;
Signature · 1 line
2.53.0
Arnaldo Carvalho de Melo
maintainerPATCH 2/8 perf util: Kill die() prototype, dead for a long time
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
In fef2a735167a827a ("perf tools: Kill die()") the die() function was removed, but not the prototype in util.h, now when building with LIBPERL=1, during a 'make -C tools/perf build-test' routine test, it is failing as perl likes die() calls and then this clashes with this remnant, remove it.
--- tools/perf/util/util.h | 1 - 1 file changed, 1 deletion(-)
tools/perf/util/util.h +0 -1
@@ -30,7 +30,6 @@ extern bool perf_guest;/* General helper functions */ void usage (const char *err)__noreturn ; -void die(const char *err, ...) __noreturn __printf(1, 2);struct dirent ;struct strlist ;
Signature · 1 line
2.53.0
Arnaldo Carvalho de Melo
maintainerPATCH 3/8 perf tools: Make more global variables static
In reply to Arnaldo Carvalho de MeloFrom: Ian Rogers <[email protected]>
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.
Committer notes:
Refreshed the patch, the original one fell thru the cracks, updated the size reduction.
Remove the trace-event-scripting.c changes, break the build, noticed with container builds and with sashiko:
https://sashiko.dev/#/patchset/20260401215306.2152898-1-acme%40kernel.org
Also make two variables static to address another sashiko review comment:
https://sashiko.dev/#/patchset/20260402001740.2220481-1-acme%40kernel.org
--- 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/util.c | 2 - 24 files changed, 109 insertions(+), 112 deletions(-)
tools/perf/arch/common.c +11 -11
@@ -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-" ,
tools/perf/arch/sh/include/dwarf-regs-table.h +1 -1
@@ -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",
tools/perf/bench/breakpoint.c +2 -2
@@ -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 = {
tools/perf/bench/mem-functions.c +1 -1
@@ -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,
tools/perf/bench/numa.c +1 -1
@@ -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" ),
tools/perf/bench/uprobe.c +1 -1
@@ -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) {
tools/perf/builtin-c2c.c +4 -3
@@ -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)
tools/perf/builtin-config.c +1 -1
@@ -23,7 +23,7 @@ static const char * const config_usage[] = { NULL }; -enum actions { +static enum actions {ACTION_LIST =1 } actions;
tools/perf/builtin-data.c +4 -4
@@ -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" ),
tools/perf/builtin-diff.c +2 -2
@@ -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, };
tools/perf/builtin-kmem.c +1 -1
@@ -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)
tools/perf/builtin-kwork.c +6 -6
@@ -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;
tools/perf/builtin-script.c +1 -1
@@ -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[] = {
tools/perf/builtin-top.c +2 -3
@@ -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) { + static 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 ,
tools/perf/tests/bp_signal.c +1 -1
@@ -36,7 +36,7 @@ static int fd3;static int overflows;static int overflows_2; -volatile long the_var; +static volatile long the_var;/*
tools/perf/tests/dso-data.c +1 -1
@@ -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 ,
tools/perf/tests/wp.c +3 -3
@@ -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)
tools/perf/util/block-range.c +1 -1
@@ -4,7 +4,7 @@#include <assert.h> #include <stdlib.h> -struct { +static struct {struct rb_root root;u64 blocks; } block_ranges;
tools/perf/util/bpf_counter.c +2 -2
@@ -353,7 +353,7 @@ static int bpf_program_profiler__install_pe(struct evsel *evsel, int cpu_map_idxreturn 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,
tools/perf/util/bpf_off_cpu.c +1 -1
@@ -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) {
tools/perf/util/debug.c +1 -1
@@ -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 )
tools/perf/util/debuginfo.c +9 -10
@@ -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; +static 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 ;
tools/perf/util/sort.c +52 -52
399 lines not on this page · raw message
tools/perf/util/util.c +0 -2
@@ -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;
Signature · 1 line
2.53.0
Arnaldo Carvalho de Melo
maintainerPATCH 4/8 perf bench: Constify tables
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
Those tables and variables don't change, better capture this by explicitely using 'const'.
--- tools/perf/builtin-bench.c | 42 +++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 21 deletions(-)
tools/perf/builtin-bench.c +21 -21
@@ -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 freturn 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 ;
Signature · 1 line
2.53.0
Arnaldo Carvalho de Melo
maintainerPATCH 5/8 perf header: Use a max number of command line args
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
Sashiko suggests we use some reasonable max number of args to avoid overflows when reading perf.data files, do it.
--- tools/perf/util/header.c | 6 ++++++ 1 file changed, 6 insertions(+)
tools/perf/util/header.c +6 -0
@@ -2795,6 +2795,9 @@ process_event_desc(struct feat_fd *ff, void *data __maybe_unused)return 0 ; } +// Some reasonable arbitrary max for the number of command line arguments +#define MAX_CMDLINE_NR 32768 +static int process_cmdline (struct feat_fd *ff,void *data__maybe_unused ) {struct perf_env *env = &ff->ph->env;
@@ -2804,6 +2807,9 @@ static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)if (do_read_u32 (ff, &nr))return -1 ; +if (nr >MAX_CMDLINE_NR ) +return -1 ; + env->nr_cmdline = nr; cmdline =zalloc (ff->size + nr +1 );
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 5/8] perf header: Use a max number of command line args
Sashiko AI review found 5 potential issue(s): - [Critical] Incomplete Mitigation for Heap Buffer Overflow (Ignores section bounds) - [High] Incomplete Mitigation for Integer Overflow in `cmdline`…
Sashiko AI review found 5 potential issue(s):
- [Critical] Incomplete Mitigation for Heap Buffer Overflow (Ignores section bounds)
- [High] Incomplete Mitigation for Integer Overflow in cmdline allocation
- [High] Pre-existing Out-of-Bounds Read and Heap Data Leak in do_read_string
- [High] Pre-existing Inconsistent Error State Leads to NULL Pointer Dereference
- [Medium] Silent Truncation of Remaining Header Features on Large Command Lines --
commit f7dfe248f37e0c8cc488e6a1ae29de4882de3598
Author: Arnaldo Carvalho de Melo <[email protected]>
perf header: Use a max number of command line args
This commit limits the maximum number of command line arguments processed from the perf.data header to avoid overflows.
Quoted 3 lines
tools/perf/util/header.c +0
-0
[ ... ]
tools/perf/util/header.c static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused) · line 2799
Quoted 6 lines
tools/perf/util/header.c +2
-0@@ -2795,6 +2795,9 @@ static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)if (do_read_u32 (ff, &nr))return -1 ; +if (nr >MAX_CMDLINE_NR ) +return -1 ;
If nr exceeds this limit, returning -1 here aborts the rest of the section processing. Since perf_session__read_header() ignores errors from perf_header__process_sections(), does this silently discard remaining header features (like CPU_TOPOLOGY) without warning the user?
Furthermore, valid command lines can exceed 32,768 arguments when using wildcards, which might result in silently dropping features on valid data.
tools/perf/util/header.c static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused) · line 2815
Quoted 3 lines
tools/perf/util/header.c +0
-0env->nr_cmdline = nr; cmdline =zalloc (ff->size + nr +1 );
The commit message notes this is to avoid overflows, but can ff->size + nr + 1 still integer overflow? Since ff->size is a 64-bit value read directly from the user-controlled perf.data header, a value near SIZE_MAX could wrap the allocation size to a small number. zalloc() would succeed, but it could lead to a heap buffer overflow during the memcpy() operations below.
Also, if zalloc() fails here, or do_read_string() fails in the loop, env->nr_cmdline remains set to nr while env->cmdline_argv is NULL. Could this cause a NULL pointer dereference later in functions like print_cmdline() that iterate over env->nr_cmdline?
Quoted 4 lines
tools/perf/util/header.c +0
-0if (!cmdline)return -1 ; argv =zalloc (sizeof (char *) * (nr +1 ));
tools/perf/util/header.c static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
Quoted 10 lines
tools/perf/util/header.c +0
-0if (!argv) goto error;for (i =0 ; i < nr; i++) { str =do_read_string (ff);if (!str) goto error; argv[i] = cmdline + len;memcpy (argv[i], str,strlen (str) +1 );
While looking at the overflow mitigations here, I noticed a couple of potential issues with how do_read_string() interacts with this buffer.
First, do_read_string() reads strings without bounding them to ff->size. If a file provides a small ff->size (resulting in a small cmdline allocation) but embeds huge strings, will this memcpy() write past the end of the cmdline buffer?
Second, if the data returned by do_read_string() lacks a null terminator, will strlen(str) read out of bounds into adjacent heap memory, causing the subsequent memcpy() to leak that heap data into the cmdline buffer?
Quoted 6 lines
tools/perf/util/header.c +0
-0len +=strlen (str) +1 ;free (str); } env->cmdline = cmdline; env->cmdline_argv = (const char **) argv;return 0 ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Arnaldo Carvalho de Melo
maintainerPATCH 6/8 perf header: Do validation of perf.data HEADER_CPU_DOMAIN_INFO
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
As suggested in an unrelated sashiko review:
https://sashiko.dev/#/patchset/20260407195145.2372104-1-acme%40kernel.org
"
Could a malformed perf.data file provide out-of-bounds values for cpu and domain?
These variables are read directly from the file and used as indices for cd_map and cd_map[cpu]->domains without any validation against env->nr_cpus_avail or max_sched_domains.
Similar to the issue above, this is an existing lack of validation that becomes apparent when looking at the allocation boundaries.
"
Validate it.
--- tools/perf/util/header.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
tools/perf/util/header.c +11 -0
@@ -3717,6 +3717,11 @@ static int process_cpu_domain_info(struct feat_fd *ff, void *data __maybe_unusedif (do_read_u32 (ff, &cpu))return -1 ; +if (cpu >= nra) { +pr_err ("Invalid HEADER_CPU_DOMAIN_INFO: cpu %d >= nr_cpus_avail (%d)\n" , cpu, nra); +return -1 ; + } + cd_map[cpu] =zalloc (sizeof (*cd_map[cpu]));if (!cd_map[cpu])return -1 ;
@@ -3736,6 +3741,12 @@ static int process_cpu_domain_info(struct feat_fd *ff, void *data __maybe_unusedif (do_read_u32 (ff, &domain))return -1 ; +if (domain >= max_sched_domains) { +pr_err ("Invalid HEADER_CPU_DOMAIN_INFO: domain %d >= max_sched_domains (%d)\n" , + domain, max_sched_domains); +return -1 ; + } + d_info =zalloc (sizeof (*d_info));if (!d_info)return -1 ;
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 6/8] perf header: Do validation of perf.data HEADER_CPU_DOMAIN_INFO
Sashiko AI review found 1 potential issue(s): - [Critical] This problem wasn't introduced by this patch, but the patch fails to completely validate untrusted data from the `perf.data` file, leaving…
Sashiko AI review found 1 potential issue(s):
- [Critical] This problem wasn't introduced by this patch, but the patch fails to completely validate untrusted data from the perf.data file, leaving a critical out-of-bounds read and arbitrary free vulnerability. The bounds validation checks cpu >= nra and domain >= max_sched_domains but misses the validation of nr_domains. The variable nr_domains is read directly from the file and stored in cd_map[cpu]->nr_domains, while the domains array is allocated with a size of max_sched_domains (which is also read from the file). There is no check ensuring nr_domains <= max_sched_domains. If an attacker crafts a file where nr_domains > max_sched_domains and induces an early exit (e.g., by providing an invalid domain index, which the patch now explicitly catches and returns -1), the parsing function aborts and the cleanup function free_cpu_domain_info() is invoked. The cleanup loop iterates j from 0 up to cd_map[cpu]->nr_domains - 1, reading cd_map[cpu]->domains[j]. This reads out of bounds of the domains array, interpreting adjacent heap memory as struct domain_info * pointers. If these out-of-bounds values are non-NULL, the code calls zfree() on their fields and the pointer itself, resulting in a severe memory corruption vulnerability. --
commit 5c7601d84031087ca2a9bfeeedd3934e8e426309
Author: Arnaldo Carvalho de Melo <[email protected]>
perf header: Do validation of perf.data HEADER_CPU_DOMAIN_INFO
This commit validates the cpu and domain values read from perf.data files against nr_cpus_avail and max_sched_domains. The newly added validation prevents using out-of-bounds indices to access the cd_map and domains arrays.
Quoted 4 lines
tools/perf/util/header.c +0
-0
[ ... ]
tools/perf/util/header.c static int process_cpu_domain_info(struct feat_fd *ff, void *data __maybe_unused · line 3749
Quoted 10 lines
tools/perf/util/header.c +6
-0@@ -3736,6 +3741,12 @@ static int process_cpu_domain_info(struct feat_fd *ff, void *data __maybe_unusedif (do_read_u32 (ff, &domain))return -1 ; +if (domain >= max_sched_domains) { +pr_err ("Invalid HEADER_CPU_DOMAIN_INFO: domain %d >= max_sched_domains (%d)\n" , + domain, max_sched_domains); +return -1 ; + } +
Does this code leave nr_domains unvalidated against max_sched_domains?
Earlier in process_cpu_domain_info(), nr_domains is read and assigned to the map, but the domains array is allocated using max_sched_domains:
if (do_read_u32(ff, &nr_domains)) return -1; cd_map[cpu]->nr_domains = nr_domains; cd_map[cpu]->domains = zalloc(sizeof(*d_info) * max_sched_domains);
If a perf.data file contains a nr_domains value greater than max_sched_domains, and an invalid domain index triggers the new return -1 path shown above, could this lead to an out-of-bounds read during cleanup?
If the cleanup function iterates up to the unvalidated nr_domains, it would read past the end of the domains array. If these out-of-bounds values are then freed, could this regression result in an arbitrary free?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Arnaldo Carvalho de Melo
maintainerPATCH 7/8 perf tools: Use calloc() where applicable
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
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.
--- 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 | 8 ++++---- 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, 72 insertions(+), 93 deletions(-)
tools/perf/arch/arm/util/auxtrace.c +3 -3
@@ -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 ;
tools/perf/arch/powerpc/util/auxtrace.c +1 -0
@@ -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"
tools/perf/arch/x86/tests/amd-ibs-period.c +1 -2
@@ -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 ;
tools/perf/arch/x86/tests/dwarf-unwind.c +1 -10
@@ -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;
tools/perf/arch/x86/util/pmu.c +0 -1
@@ -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>
tools/perf/bench/numa.c +4 -9
@@ -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) {
tools/perf/bench/sched-messaging.c +1 -1
@@ -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()" );
tools/perf/builtin-annotate.c +0 -1
@@ -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"
tools/perf/builtin-c2c.c +3 -3
@@ -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 ;
tools/perf/builtin-diff.c +1 -1
@@ -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 ;
tools/perf/builtin-ftrace.c +1 -0
@@ -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"
tools/perf/builtin-kwork.c +1 -1
@@ -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 ;
tools/perf/builtin-record.c +5 -5
@@ -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_mareturn -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 ;
tools/perf/builtin-sched.c +3 -3
@@ -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 ; }
tools/perf/builtin-script.c +4 -4
@@ -3823,7 +3823,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" );
@@ -4312,7 +4312,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 ;
@@ -4338,7 +4338,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 ;
@@ -4376,7 +4376,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 ;
tools/perf/builtin-stat.c +1 -1
@@ -2774,7 +2774,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;
tools/perf/builtin-trace.c +1 -3
@@ -2269,9 +2269,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);
tools/perf/jvmti/libjvmti.c +2 -3
@@ -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" );
tools/perf/tests/code-reading.c +1 -0
@@ -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>
tools/perf/tests/thread-map.c +0 -1
@@ -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>
tools/perf/util/annotate-arch/annotate-x86.c +1 -0
@@ -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"
tools/perf/util/bpf-event.c +1 -1
@@ -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 ;
tools/perf/util/bpf_counter_cgroup.c +0 -1
@@ -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>
tools/perf/util/data-convert-bt.c +1 -1
@@ -1379,7 +1379,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 ;
tools/perf/util/data.c +1 -1
@@ -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 ;
tools/perf/util/db-export.c +0 -1
@@ -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) {
tools/perf/util/disasm.c +1 -0
@@ -13,6 +13,7 @@#include <unistd.h> #include <linux/string.h> +#include <linux/zalloc.h> #include <subcmd/run-command.h> #include "annotate.h"
tools/perf/util/event.c +0 -1
@@ -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"
tools/perf/util/evlist.c +1 -2
@@ -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 ;
tools/perf/util/header.c +9 -9
@@ -2816,7 +2816,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;
@@ -2970,7 +2970,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 ;
@@ -3168,7 +3168,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 ;
@@ -3260,7 +3260,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 ;
@@ -3350,7 +3350,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 ;
@@ -3565,7 +3565,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 ;
@@ -3642,7 +3642,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 ;
@@ -3695,7 +3695,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 ;
@@ -3733,7 +3733,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 ;
tools/perf/util/hist.c +1 -1
@@ -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 ;
tools/perf/util/mem2node.c +1 -1
@@ -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 ;
tools/perf/util/pmus.c +1 -1
@@ -621,7 +621,7 @@ void perf_pmus__print_pmu_events(const struct print_callbacks *print_cb, void *pwhile ((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 ;
tools/perf/util/powerpc-vpadtl.c +1 -0
@@ -4,6 +4,7 @@*/ #include <linux/string.h> +#include <linux/zalloc.h> #include <errno.h> #include <inttypes.h> #include "color.h"
tools/perf/util/probe-event.c +8 -9
@@ -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;
tools/perf/util/probe-file.c +1 -1
@@ -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 ;
tools/perf/util/probe-finder.c +4 -4
@@ -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 ;
tools/perf/util/session.c +1 -1
@@ -2559,7 +2559,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 ;
tools/perf/util/srcline.c +1 -0
@@ -12,6 +12,7 @@#include <inttypes.h> #include <string.h> #include <linux/string.h> +#include <linux/zalloc.h> bool srcline_full_filename;
tools/perf/util/stat-shadow.c +0 -1
@@ -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"
tools/perf/util/unwind-libunwind-local.c +0 -1
@@ -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>
tools/perf/util/values.c +4 -4
@@ -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 ;
Signature · 1 line
2.53.0
Arnaldo Carvalho de Melo
maintainerPATCH 8/8 perf tools: Replace basename() calls with perf_basename()
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
As noticed in a sashiko review for a patch adding a missing libgen.h in a file using basename():
https://sashiko.dev/#/patchset/20260402001740.2220481-1-acme%40kernel.org
So avoid these subtleties and instead reuse the gnu_basename() function we had in srcline.c, renaming it to perf_basename() and replace basename() calls with it, simplifying several cases by removing now needless strdups.
--- tools/perf/builtin-daemon.c | 4 ++-- tools/perf/util/annotate.c | 3 +-- tools/perf/util/data-convert-json.c | 4 ++-- tools/perf/util/dsos.c | 32 ++++++++--------------------- tools/perf/util/probe-event.c | 3 +-- tools/perf/util/srcline.c | 11 ++-------- tools/perf/util/symbol.h | 16 ++++----------- tools/perf/util/util.c | 8 ++++++++ tools/perf/util/util.h | 2 ++ 9 files changed, 31 insertions(+), 52 deletions(-)
tools/perf/builtin-daemon.c +2 -2
@@ -1016,7 +1016,7 @@ static int setup_config_changes(struct daemon *daemon) {char *basen =strdup (daemon->config_real);char *dirn =strdup (daemon->config_real); -char *base, *dir; +const char *base, *dir;int fd, wd =-1 ;if (!dirn || !basen)
@@ -1029,7 +1029,7 @@ static int setup_config_changes(struct daemon *daemon) } dir =dirname (dirn); - base =basename (basen); + base =perf_basename (basen);pr_debug ("config file: %s, dir: %s\n" , base, dir); wd =inotify_add_watch (fd, dir,IN_CLOSE_WRITE );
tools/perf/util/annotate.c +1 -2
@@ -8,7 +8,6 @@#include <errno.h> #include <inttypes.h> -#include <libgen.h> #include <stdlib.h> #include "util.h" // hex_width() #include "ui/ui.h"
@@ -1245,7 +1244,7 @@ int hist_entry__annotate_printf(struct hist_entry *he, struct evsel *evsel)if (opts->full_path) d_filename = filename;else - d_filename =basename (filename); + d_filename =perf_basename (filename);if (evsel__is_group_event (evsel)) {evsel__group_desc (evsel, buf,sizeof (buf));
tools/perf/util/data-convert-json.c +2 -2
@@ -326,7 +326,7 @@ static void output_headers(struct perf_session *session, struct convert_json *c)output_json_format (out, false,2 ,"]" ); } -int bt_convert__perf2json (const char *input_name,const char *output_name, +int bt_convert__perf2json (const char *_input_name,const char *output_name,struct perf_data_convert_opts *opts__maybe_unused ) {struct perf_session *session;
@@ -342,7 +342,7 @@ int bt_convert__perf2json(const char *input_name, const char *output_name, };struct perf_data data = { .mode =PERF_DATA_MODE_READ , - .path = input_name, + .path = _input_name, .force = opts->force, };
tools/perf/util/dsos.c +9 -23
@@ -6,7 +6,6 @@#include "vdso.h" #include "namespaces.h" #include <errno.h> -#include <libgen.h> #include <stdlib.h> #include <string.h> #include <symbol.h> // filename__read_build_id
@@ -297,34 +296,21 @@ struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)static void dso__set_basename (struct dso *dso) { -char *base, *lname; +bool allocated = false; +const char *base;int tid;if (perf_pid_map_tid (dso__long_name (dso), &tid)) { -if (asprintf (&base,"[JIT] tid %d" , tid) <0 ) -return ; - }else { -/* -* basename() may modify path buffer, so we must pass -* a copy. -*/ - lname =strdup (dso__long_name (dso)); -if (!lname) -return ; - -/* -* basename() may return a pointer to internal -* storage which is reused in subsequent calls -* so copy the result. -*/ - base =strdup (basename (lname)); +char *jitname; -free (lname); - -if (!base) +if (asprintf (&jitname,"[JIT] tid %d" , tid) <0 )return ; + allocated = true; + base = jitname; + }else { + base =perf_basename (dso__long_name (dso)); } -dso__set_short_name (dso, base, true); +dso__set_short_name (dso, base, allocated); }static struct dso *__dsos__addnew_id (struct dsos *dsos,const char *name,const struct dso_id *id)
tools/perf/util/probe-event.c +1 -2
@@ -11,7 +11,6 @@#include <sys/stat.h> #include <fcntl.h> #include <errno.h> -#include <libgen.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h>
@@ -229,7 +228,7 @@ static int convert_exec_to_group(const char *exec, char **result)if (!exec_copy)return -ENOMEM ; - ptr1 =basename (exec_copy); + ptr1 = (char *)perf_basename (exec_copy);if (!ptr1) { ret = -EINVAL ; goto out;
tools/perf/util/srcline.c +2 -9
@@ -8,6 +8,7 @@#include "symbol.h" #include "libdw.h" #include "debug.h" +#include "util.h" #include <inttypes.h> #include <string.h>
@@ -74,14 +75,6 @@ int inline_list__append_tail(struct symbol *symbol, char *srcline, struct inlinereturn 0 ; } -/* basename version that takes a const input string */ -static const char *gnu_basename (const char *path) -{ -const char *base =strrchr (path,'/' ); - -return base ? base +1 : path; -} -char *srcline_from_fileline (const char *file,unsigned int line) {char *srcline;
@@ -90,7 +83,7 @@ char *srcline_from_fileline(const char *file, unsigned int line)return NULL ;if (!srcline_full_filename) - file =gnu_basename (file); + file =perf_basename (file);if (asprintf (&srcline,"%s:%u" , file, line) <0 )return NULL ;
tools/perf/util/symbol.h +4 -12
@@ -14,6 +14,7 @@#include "path.h" #include "symbol_conf.h" #include "spark.h" +#include "util.h" #ifdef HAVE_LIBELF_SUPPORT #include <libelf.h>
@@ -97,18 +98,9 @@ struct intlist;static inline int __symbol__join_symfs (char *bf,size_t size,const char *path) { -if (symbol_conf.symfs_layout_flat) { -char *path_copy =strdup (path); -char *base; -int ret; - -if (!path_copy) -return -ENOMEM ; - base =basename (path_copy); - ret =path__join (bf, size, symbol_conf.symfs, base); -free (path_copy); -return ret; - } +if (symbol_conf.symfs_layout_flat) +return path__join (bf, size, symbol_conf.symfs,perf_basename (path)); +return path__join (bf, size, symbol_conf.symfs, path); }
tools/perf/util/util.c +8 -0
@@ -545,3 +545,11 @@ int scandirat(int dirfd, const char *dirp, return err; }#endif + +/* basename version that takes a const input string */ +const char *perf_basename (const char *path) +{ +const char *base =strrchr (path,'/' ); + +return base ? base +1 : path; +}
tools/perf/util/util.h +2 -0
@@ -86,6 +86,8 @@ struct perf_debuginfod { };void perf_debuginfod_setup (struct perf_debuginfod *di); +const char *perf_basename (const char *path); +char *filename_with_chroot (int pid,const char *filename); int do_realloc_array_as_needed(void **arr, size_t *arr_sz, size_t x,
Signature · 1 line
2.53.0