perf trace: Fix BPF filtering and make tracing tests non-exclusive
Ian Rogers
reviewercover perf trace: Fix BPF filtering and make tracing tests non-exclusive
perf trace's BPF augmentation attaches to raw_syscalls:sys_enter and raw_syscalls:sys_exit system wide, and used the program return value to decide whether a syscall was interesting. Returning 0 from a BPF_PROG_TYPE_TRACEPOINT program makes perf_trace_run_bpf_submit() drop the event for every listener on that tracepoint, not just for the perf trace that installed the program. Any concurrent perf trace, perf record or ftrace session watching raw_syscalls therefore lost events, which is one of the reasons so many of the perf trace and perf probe shell tests had to be marked (exclusive) and run on their own.
Patches 1 to 5 fix perf trace. They stop the return value being used as a filter and do the filtering in BPF maps instead, fix argument handling for the __data_loc internal tracepoint fields that syscalls:sys_enter_* gained in 6.19, stop the sys_exit program array tail calling a sys_enter augmenter, and replace the userspace PERF_RECORD_FORK/PERF_RECORD_EXIT bookkeeping with BTF-typed raw tracepoint programs on sched_process_{fork,exit,exec}. A task is then registered before its first syscall and evicted in do_exit(), rather than whenever userspace next drains the ring buffer.
Patches 6 to 13 deal with the tests. Several collided with each other through global state rather than through perf trace: fixed probe names, clear_all_probes() disabling tracing events globally, and perf trace's hardcoded "probe:vfs_getname*" wildcard pinning probes belonging to other tests. With those scoped to a pid they can drop (exclusive) and run in parallel again.
Tested on x86_64. The trace and probe tests pass under 'perf test -r3', which runs the repeats concurrently. Every patch builds individually, and the series also builds with BUILD_BPF_SKEL=0.
Ian Rogers (13):
perf trace: Start BPF summary before starting workload perf trace: Skip internal tracepoint fields in formatting and beauty
map
perf trace: Do not set unaugmented BPF program on sys_exit map perf trace: Filter events in BPF and avoid tracepoint vetoes perf trace: Handle fork and exit directly in BPF filter maps perf test test_task_analyzer: Isolate in temporary directory and make
non-exclusive
perf test common: Do not globally disable tracing events in
clear_all_probes
perf test probe_vfs_getname: Scope probe name to PID and make
non-exclusive
perf test record+probe_libc_inet_pton: Scope event to PID, add
retries, and make non-exclusive
perf test trace_summary: Improve error diagnostics
perf test trace_btf_general: Drop --max-events=1 and make
non-exclusive
perf test trace_summary: Make non-exclusive
perf test uprobe_from_different_cu: Scope probe name to PID
tools/perf/Documentation/perf-trace.txt | 5 + tools/perf/builtin-trace.c | 330 ++++++++++++++---- tools/perf/tests/shell/common/init.sh | 1 - .../perf/tests/shell/lib/probe_vfs_getname.sh | 34 +- tools/perf/tests/shell/probe_vfs_getname.sh | 3 +- .../shell/record+probe_libc_inet_pton.sh | 83 +++-- .../shell/record+script_probe_vfs_getname.sh | 18 +- tools/perf/tests/shell/test_task_analyzer.sh | 11 +- .../shell/test_uprobe_from_different_cu.sh | 11 +- .../tests/shell/trace+probe_vfs_getname.sh | 9 + tools/perf/tests/shell/trace_btf_general.sh | 8 +- tools/perf/tests/shell/trace_summary.sh | 14 +- .../bpf_skel/augmented_raw_syscalls.bpf.c | 291 ++++++++++++++- tools/perf/util/bpf_trace_augment.c | 173 ++++++++- tools/perf/util/trace_augment.h | 25 +- 15 files changed, 875 insertions(+), 141 deletions(-)
base-commit: 91b0782fc9e9d2f0a40b5256146e014802fdbb36
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
Ian Rogers
reviewerPATCH 1/13 perf trace: Start BPF summary before starting workload
When using --bpf-summary, trace_start_bpf_summary() sets skel->bss->enabled = 1. In trace__run(), trace_start_bpf_summary() was previously invoked after evlist__start_workload().
Because evlist__start_workload() immediately unblocks the child process by writing to its go_pipe, short-lived workloads (such as cat /dev/null) can execute and complete their initial system calls before trace_start_bpf_summary() is reached by the parent process. Furthermore, under high system load, the child process may finish before the BPF summary tracking is enabled in the kernel at all, causing syscall summary tests to fail. Additionally, if initial_delay was configured, the workload was started before sleeping.
Move trace_start_bpf_summary() to be invoked before evlist__start_workload(), matching evlist__enable(), and ensure it respects target.initial_delay.
--- tools/perf/builtin-trace.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
tools/perf/builtin-trace.c +5 -3
@@ -4838,17 +4838,19 @@ static int trace__run(struct trace *trace, int argc, const char **argv)if (!target__none (&trace->opts.target) && !trace->opts.target.initial_delay)evlist__enable (evlist); +if (trace->summary_bpf && !trace->opts.target.initial_delay) +trace_start_bpf_summary (); +if (forks)evlist__start_workload (evlist);if (trace->opts.target.initial_delay) {usleep (trace->opts.target.initial_delay *1000 );evlist__enable (evlist); +if (trace->summary_bpf) +trace_start_bpf_summary (); } -if (trace->summary_bpf) -trace_start_bpf_summary (); - trace->multiple_threads =perf_thread_map__pid (evlist__core (evlist)->threads,0 ) ==-1 ||perf_thread_map__nr (evlist__core (evlist)->threads) >1 ||evlist__first (evlist)->core.attr.inherit;
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v1 01/13] perf trace: Start BPF summary before starting workload
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
When using --bpf-summary,
trace_start_bpf_summary()setsskel->bss->enabled= 1. Intrace__run(),trace_start_bpf_summary()was previously invoked afterevlist__start_workload().Because
evlist__start_workload()immediately unblocks the child process by writing to itsgo_pipe, short-lived workloads (such ascat /dev/null) can execute and complete their initial system calls beforetrace_start_bpf_summary()is reached by the parent process. Furthermore, under high system load, the child process may finish before the BPF summary tracking is enabled in the kernel at all, causing syscall summary tests to fail. Additionally, ifinitial_delaywas configured, the workload was started before sleeping.
Quoted 6 lines
Move
trace_start_bpf_summary()to be invoked beforeevlist__start_workload(), matchingevlist__enable(), and ensure it respects target.initial_delay.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Ian Rogers
reviewerPATCH 2/13 perf trace: Skip internal tracepoint fields in formatting and beauty map
In reply to Ian RogersLinux 6.19+ added __data_loc char[] internal fields for string arguments in syscalls:sys_enter_<name> tracepoints (e.g., __data_loc_oldname in sys_enter_renameat2). While is_internal_field() was added to detect them, several places did not properly account for them:
1. In syscall_arg_fmt__init_array(), when an internal field was skipped, the arg pointer was still incremented, causing the subsequent argument formatters to be mismatched.
2. In syscall__scnprintf_args(), internal fields were not skipped, causing spurious trailing arguments like ", 0, 16" to be formatted and printed.
3. In trace__bpf_sys_enter_beauty_map(), internal fields were not skipped, offsetting beauty array argument indices and breaking string and buffer augmentation.
4. In trace__find_usable_bpf_prog_entry(), candidate pointer checks matched on internal pointer fields, breaking signature compatibility matching between syscalls for augmenter sharing. Introduce next_user_arg() and advance both cursors with it, so that the two argument lists are always compared at a real argument and the walk ends when one syscall runs out of arguments rather than when one happens to have trailing internal fields.
5. syscall__augmented_args() computed the augmented payload as sample->raw_size - sc->args_size for any sys_enter style sample. sc->args_size deliberately stops at the last non-internal field, so on 6.19+ a native syscalls:sys_enter_<name> record leaves the __data_loc words and their string payloads in the remainder. Those bytes are not a struct augmented_arg, so syscall_arg__scnprintf_augmented_string() read a bogus length and walked arg->augmented.args out of bounds. This is reachable from trace__event_handler(), which calls trace__fprintf_sys_enter() for any evsel whose tracepoint name starts with "sys_enter_".
6. In syscall__read_info(), syscall__alloc_arg_fmts() was called before checking and dropping the leading __syscall_nr (or nr) field, using nr_fields - 1 unconditionally. If a tracepoint format lacks that leading field, the allocated arg_fmt array is one entry too small and syscall_arg_fmt__init_array() writes one entry past the end of the heap buffer. Drop __syscall_nr/nr first and size the allocation from the remaining fields.
Update these functions to check and skip is_internal_field() so that arguments are correctly formatted and beauty map entries match the expected syscall signatures, restrict syscall__augmented_args() to the __augmented_syscalls__ bpf-output evsel, and size arg_fmt after dropping the syscall number field.
--- tools/perf/builtin-trace.c | 153 ++++++++++++++++++++++++++++--------- 1 file changed, 115 insertions(+), 38 deletions(-)
tools/perf/builtin-trace.c +115 -38
@@ -2277,15 +2277,20 @@ syscall_arg_fmt__init_array(struct syscall_arg_fmt *arg, struct tep_format_fieldstruct tep_format_field *last_field =NULL ;int len; -for (; field; field = field->next, ++arg) { -/* assume it's the last argument */ +for (; field; field = field->next) { +/* +* Skip internal tracepoint fields (e.g., __data_loc strings in +* Linux 6.19+) so they do not advance the syscall arg array index. +*/ if (is_internal_field (field))continue ; last_field = field; -if (arg->scnprintf) +if (arg->scnprintf) { + ++arg;continue ; + } len =strlen (field->name);
@@ -2342,6 +2347,7 @@ syscall_arg_fmt__init_array(struct syscall_arg_fmt *arg, struct tep_format_field } } } + ++arg; }return last_field;
@@ -2363,6 +2369,7 @@ static int syscall__read_info(struct syscall *sc, struct trace *trace)char tp_name[128 ];const char *name;struct tep_format_field *field; +int nr_args;int err;if (sc->nonexistent)
@@ -2401,24 +2408,25 @@ static int syscall__read_info(struct syscall *sc, struct trace *trace)return err; } -/* -* The tracepoint format contains __syscall_nr field, so it's one more -* than the actual number of syscall arguments. -*/ -if (syscall__alloc_arg_fmts (sc, sc->tp_format->format.nr_fields -1 )) -return -ENOMEM ; - sc->args = sc->tp_format->format.fields; + nr_args = sc->tp_format->format.nr_fields;/* * We need to check and discard the first variable '__syscall_nr' * or 'nr' that mean the syscall number. It is needless here. * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels. +* +* Do this before allocating, and size the array from what is left, so +* that a format without the field does not leave +* syscall_arg_fmt__init_array() walking one entry past the end. */ if (sc->args && (!strcmp (sc->args->name,"__syscall_nr" ) || !strcmp (sc->args->name,"nr" ))) { sc->args = sc->args->next; - --sc->nr_args; + --nr_args; } +if (syscall__alloc_arg_fmts (sc, nr_args)) +return -ENOMEM ; + field = sc->args;while (field) {if (is_internal_field (field))
@@ -2636,11 +2644,17 @@ static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,if (sc->args !=NULL ) {struct tep_format_field *field; -for (field = sc->args; field; - field = field->next, ++arg.idx, bit <<=1 ) { -if (arg.mask & bit) +for (field = sc->args; field; field = field->next) { +/* Skip internal fields so they are not printed as spurious arguments */ +if (is_internal_field (field))continue ; +if (arg.mask & bit) { + ++arg.idx; + bit <<=1 ; +continue ; + } + arg.fmt = &sc->arg_fmt[arg.idx]; val =syscall_arg__val (&arg, arg.idx);/*
@@ -2658,8 +2672,11 @@ static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,*/ if (val ==0 && !trace->show_zeros && !(sc->arg_fmt && sc->arg_fmt[arg.idx].show_zero) && - !(sc->arg_fmt && sc->arg_fmt[arg.idx].strtoul ==STUL_BTF_TYPE )) + !(sc->arg_fmt && sc->arg_fmt[arg.idx].strtoul ==STUL_BTF_TYPE )) { + ++arg.idx; + bit <<=1 ;continue ; + } printed +=scnprintf (bf + printed, size - printed,"%s" , printed ?", " :"" );
@@ -2674,12 +2691,16 @@ static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size, size - printed, val, field->type);if (btf_printed) { printed += btf_printed; + ++arg.idx; + bit <<=1 ;continue ; } } printed +=syscall_arg_fmt__scnprintf_val (&sc->arg_fmt[arg.idx], bf + printed, size - printed, &arg, val); + ++arg.idx; + bit <<=1 ; } }else if (IS_ERR (sc->tp_format)) {/*
@@ -2940,7 +2961,9 @@ static int trace__fprintf_sample(struct trace *trace, struct perf_sample *samplereturn printed; } -static void *syscall__augmented_args (struct syscall *sc,struct perf_sample *sample,int *augmented_args_size,int raw_augmented_args_size) +static void *syscall__augmented_args (struct trace *trace,struct syscall *sc, +struct perf_sample *sample, +int *augmented_args_size,int raw_augmented_args_size) {/* * For now with BPF raw_augmented we hook into raw_syscalls:sys_enter
@@ -2958,6 +2981,24 @@ static void *syscall__augmented_args(struct syscall *sc, struct perf_sample *sam*/ int args_size = raw_augmented_args_size ?: sc->args_size; +/* +* Augmented arguments are a perf trace specific payload, they are only +* ever appended to samples emitted by the BPF __augmented_syscalls__ +* bpf-output event. +* +* Native syscalls:sys_enter_NAME tracepoints may also carry trailing +* data of their own: since Linux 6.19 they append __data_loc char[] +* fields plus the string payloads they point at. Those bytes are not a +* struct augmented_arg, so treating them as one would make +* syscall_arg__scnprintf_augmented_string() read a bogus length and +* walk arg->augmented.args far out of bounds. +* +* So only look for augmented arguments on the event that can actually +* produce them. +*/ +if (sample->evsel != trace->syscalls.events.bpf_output) +return NULL ; + *augmented_args_size = sample->raw_size - args_size;if (*augmented_args_size >0 ) {static uintptr_t argbuf[1024 ];/* assuming single-threaded */
@@ -3016,17 +3057,13 @@ static int trace__sys_enter(struct trace *trace,if (!(trace->duration_filter || trace->summary_only || trace->min_stack))trace__printf_interrupted_entry (trace);/* -* If this is raw_syscalls.sys_enter, then it always comes with the 6 possible -* arguments, even if the syscall being handled, say "openat", uses only 4 arguments -* this breaks syscall__augmented_args() check for augmented args, as we calculate -* syscall->args_size using each syscalls:sys_enter_NAME tracefs format file, -* so when handling, say the openat syscall, we end up getting 6 args for the -* raw_syscalls:sys_enter event, when we expected just 4, we end up mistakenly -* thinking that the extra 2 u64 args are the augmented filename, so just check -* here and avoid using augmented syscalls when the evsel is the raw_syscalls one. +* syscall__augmented_args() only returns a payload for the BPF +* __augmented_syscalls__ event, so raw_syscalls:sys_enter (which always +* carries all 6 possible arguments rather than sc->args_size worth) and +* the native syscalls:sys_enter_NAME tracepoints are both handled there. */ -if (evsel != trace->syscalls.events.sys_enter) - augmented_args =syscall__augmented_args (sc, sample, &augmented_args_size, trace->raw_augmented_syscalls_args_size); + augmented_args =syscall__augmented_args (trace, sc, sample, &augmented_args_size, + trace->raw_augmented_syscalls_args_size); ttrace->entry_time = sample->time; ttrace->entry_cpu = sample->cpu; msg = ttrace->entry_str;
@@ -3071,7 +3108,7 @@ static int trace__fprintf_sys_enter(struct trace *trace, struct perf_sample *samstruct syscall *sc;char msg[1024 ];void *args, *augmented_args =NULL ; -int augmented_args_size, e_machine; +int augmented_args_size =0 , e_machine;size_t printed =0 ;
@@ -3089,7 +3126,8 @@ static int trace__fprintf_sys_enter(struct trace *trace, struct perf_sample *sam goto out_put; args =perf_evsel__sc_tp_ptr (args, sample); - augmented_args =syscall__augmented_args (sc, sample, &augmented_args_size, trace->raw_augmented_syscalls_args_size); + augmented_args =syscall__augmented_args (trace, sc, sample, &augmented_args_size, + trace->raw_augmented_syscalls_args_size); printed +=syscall__scnprintf_args (sc, msg,sizeof (msg), args, augmented_args, augmented_args_size, trace, thread);fprintf (trace->output,"%.*s" , (int )printed, msg); err =0 ;
@@ -4121,10 +4159,16 @@ static int trace__bpf_sys_enter_beauty_map(struct trace *trace, int e_machine, iif (trace->btf ==NULL )return -1 ; -for (i =0 , field = sc->args; field; ++i, field = field->next) { +for (i =0 , field = sc->args; field; field = field->next) { +/* Skip internal fields to keep beauty array index aligned with syscall arguments */ +if (is_internal_field (field)) +continue ; +// XXX We're only collecting pointer payloads _from_ user space -if (!sc->arg_fmt[i].from_user) +if (!sc->arg_fmt[i].from_user) { + ++i;continue ; + } struct_offset =strstr (field->type,"struct " );if (struct_offset ==NULL )
@@ -4143,8 +4187,10 @@ static int trace__bpf_sys_enter_beauty_map(struct trace *trace, int e_machine, i name[cnt] ='\0' ;/* cache struct's btf_type and type_id */ -if (syscall_arg_fmt__cache_btf_struct (&sc->arg_fmt[i], trace->btf, name)) +if (syscall_arg_fmt__cache_btf_struct (&sc->arg_fmt[i], trace->btf, name)) { + ++i;continue ; + } bt = sc->arg_fmt[i].type; beauty_array[i] = bt->size;
@@ -4170,7 +4216,9 @@ static int trace__bpf_sys_enter_beauty_map(struct trace *trace, int e_machine, istruct tep_format_field *field_tmp;/* find the size of the buffer that appears in pairs with buf */ -for (j =0 , field_tmp = sc->args; field_tmp; ++j, field_tmp = field_tmp->next) { +for (j =0 , field_tmp = sc->args; field_tmp; field_tmp = field_tmp->next) { +if (is_internal_field (field_tmp)) +continue ; if (!(field_tmp->flags & TEP_FIELD_IS_POINTER) && /* only integers */ (strstr(field_tmp->name, "count") || strstr(field_tmp->name, "siz") || /* size, bufsiz */
@@ -4180,8 +4228,10 @@ static int trace__bpf_sys_enter_beauty_map(struct trace *trace, int e_machine, i can_augment = true;break ; } + ++j; } } + ++i; }if (can_augment)
@@ -4190,6 +4240,19 @@ static int trace__bpf_sys_enter_beauty_map(struct trace *trace, int e_machine, ireturn -1 ; } +/* +* Advance to the first field that is a real syscall argument, so that callers +* walking two argument lists in step never have to reason about internal +* fields appearing in one list but not the other. +*/ +static struct tep_format_field *next_user_arg (struct tep_format_field *field) +{ +while (field &&is_internal_field (field)) + field = field->next; + +return field; +} +static struct bpf_program *trace__find_usable_bpf_prog_entry (struct trace *trace,struct syscall *sc) {
@@ -4197,7 +4260,7 @@ static struct bpf_program *trace__find_usable_bpf_prog_entry(struct trace *trace/* * We're only interested in syscalls that have a pointer: */ -for (field = sc->args; field; field = field->next) { +for (field =next_user_arg (sc->args); field; field =next_user_arg (field->next)) {if (field->flags &TEP_FIELD_IS_POINTER ) goto try_to_find_pair; }
@@ -4215,21 +4278,31 @@ static struct bpf_program *trace__find_usable_bpf_prog_entry(struct trace *trace pair->bpf_prog.sys_enter == unaugmented_prog)continue ; -for (field = sc->args, candidate_field = pair->args; - field && candidate_field; field = field->next, candidate_field = candidate_field->next) { +/* +* Both cursors only ever point at real arguments, so the loop +* ends when one of the two syscalls runs out of them, rather +* than when one happens to have trailing internal fields. +*/ + field =next_user_arg (sc->args); + candidate_field =next_user_arg (pair->args); +while (field && candidate_field) {bool is_pointer = field->flags &TEP_FIELD_IS_POINTER , candidate_is_pointer = candidate_field->flags &TEP_FIELD_IS_POINTER ;if (is_pointer) { -if (!candidate_is_pointer) { +if (!candidate_is_pointer) {// The candidate just doesn't copies our pointer arg, might copy other pointers we want. + field =next_user_arg (field->next); + candidate_field =next_user_arg (candidate_field->next);continue ; - } + } }else {if (candidate_is_pointer) {// The candidate might copy a pointer we don't have, skip it. goto next_candidate; } + field =next_user_arg (field->next); + candidate_field =next_user_arg (candidate_field->next);continue ; }
@@ -4250,6 +4323,8 @@ static struct bpf_program *trace__find_usable_bpf_prog_entry(struct trace *trace goto next_candidate; is_candidate = true; + field =next_user_arg (field->next); + candidate_field =next_user_arg (candidate_field->next); }if (!is_candidate)
@@ -4261,7 +4336,9 @@ static struct bpf_program *trace__find_usable_bpf_prog_entry(struct trace *trace* more than what is common to the two syscalls. */ if (candidate_field) { -for (candidate_field = candidate_field->next; candidate_field; candidate_field = candidate_field->next) + candidate_field =next_user_arg (candidate_field->next); +for (; candidate_field; + candidate_field =next_user_arg (candidate_field->next))if (candidate_field->flags &TEP_FIELD_IS_POINTER ) goto next_candidate; }
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v1 02/13] perf trace: Skip internal tracepoint fields in formatting and beauty map
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 19 lines
Linux 6.19+ added
__data_locchar[] internal fields for string arguments in syscalls:sys_enter_<name> tracepoints (e.g.,__data_loc_oldnameinsys_enter_renameat2). Whileis_internal_field()was added to detect them, several places did not properly account for them:1. In
syscall_arg_fmt__init_array(), when an internal field was skipped, the arg pointer was still incremented, causing the subsequent argument formatters to be mismatched.
2. Insyscall__scnprintf_args(), internal fields were not skipped, causing spurious trailing arguments like ", 0, 16" to be formatted and printed.
3. Intrace__bpf_sys_enter_beauty_map(), internal fields were not skipped, offsetting beauty array argument indices and breaking string and buffer augmentation.
[ ... ]__augmented_syscalls__bpf-output evsel, and sizearg_fmtafter dropping the syscall number field.
Quoted 2 lines
Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Ian Rogers
reviewerPATCH 3/13 perf trace: Do not set unaugmented BPF program on sys_exit map
In reply to Ian RogersIn trace__init_syscalls_bpf_prog_array_maps(), the BPF program array map for sys_exit (syscalls_sys_exit) was populated with the result of trace__bpf_prog_sys_exit_fd().
When a syscall had no specific exit augmenter, trace__find_syscall_bpf_prog() fell back to unaugmented_prog (syscall_unaugmented). However, syscall_unaugmented is a sys_enter program that outputs enter arguments to __augmented_syscalls__.
As a consequence, when an unaugmented syscall exited, sys_exit tail-called syscall_unaugmented, which interpreted the exit arguments as enter arguments and emitted a duplicate, corrupt sys_enter event into __augmented_syscalls__ right as the syscall completed.
Fix this by:
1. Returning NULL from trace__find_syscall_bpf_prog() when looking up exit augmenters and none is found.
2. Returning -1 from trace__bpf_prog_sys_exit_fd() when no exit program is present.
3. Only updating map_exit_fd when prog_fd >= 0.
4. Clearing err = 0 when trace__bpf_sys_enter_beauty_map() returns non-zero (indicating the syscall has no augmentable pointer arguments) before continuing the loop, so a trailing run of such syscalls (e.g. 'perf trace -e close') does not leave err non-zero on return and abort the session.
--- tools/perf/builtin-trace.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-)
tools/perf/builtin-trace.c +22 -6
@@ -4117,7 +4117,12 @@ static struct bpf_program *trace__find_syscall_bpf_prog(struct trace *trace __mapr_debug ("Couldn't find BPF prog \"%s\" to associate with syscalls:sys_%s_%s, not augmenting it\n" , prog_name, type, sc->name); out_unaugmented: -return unaugmented_prog; +/* +* Do not set unaugmented_prog for exit: syscall_unaugmented is a +* sys_enter program that outputs enter arguments. Exit without a +* specialized return augmenter returns 1 directly from sys_exit. +*/ +return !strcmp (type,"exit" ) ?NULL : unaugmented_prog; }static void trace__init_syscall_bpf_progs (struct trace *trace,int e_machine,int id)
@@ -4140,7 +4145,7 @@ static int trace__bpf_prog_sys_enter_fd(struct trace *trace, int e_machine, intstatic int trace__bpf_prog_sys_exit_fd (struct trace *trace,int e_machine,int id) {struct syscall *sc =trace__syscall_info (trace,NULL , e_machine, id); -return sc ?bpf_program__fd (sc->bpf_prog.sys_exit) :bpf_program__fd (unaugmented_prog); +return sc && sc->bpf_prog.sys_exit ?bpf_program__fd (sc->bpf_prog.sys_exit) :-1 ; }static int trace__bpf_sys_enter_beauty_map (struct trace *trace,int e_machine,int key,unsigned int *beauty_array)
@@ -4395,16 +4400,27 @@ static int trace__init_syscalls_bpf_prog_array_maps(struct trace *trace, int e_m err =bpf_map_update_elem (map_enter_fd, &key, &prog_fd,BPF_ANY );if (err)break ; +/* Only update the exit prog array map if an exit augmenter exists */ prog_fd =trace__bpf_prog_sys_exit_fd (trace, e_machine, key); - err =bpf_map_update_elem (map_exit_fd, &key, &prog_fd,BPF_ANY ); -if (err) -break ; +if (prog_fd >=0 ) { + err =bpf_map_update_elem (map_exit_fd, &key, &prog_fd,BPF_ANY ); +if (err) +break ; + }/* use beauty_map to tell BPF how many bytes to collect, set beauty_map's value here */ memset (beauty_array,0 ,sizeof (beauty_array)); err =trace__bpf_sys_enter_beauty_map (trace, e_machine, key, (unsigned int *)beauty_array); -if (err) +if (err) { +/* +* Not a failure: the syscall just has no augmentable +* arguments. Clear err, or a trailing run of such +* syscalls, e.g. all of them for 'perf trace -e close', +* would leave it set on return and abort the session. +*/ + err =0 ;continue ; + } err =bpf_map_update_elem (beauty_map_fd, &key, beauty_array,BPF_ANY );if (err)break ;
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v1 03/13] perf trace: Do not set unaugmented BPF program on sys_exit map
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 26 lines
In
trace__init_syscalls_bpf_prog_array_maps(), the BPF program array map forsys_exit(syscalls_sys_exit) was populated with the result oftrace__bpf_prog_sys_exit_fd().When a syscall had no specific exit augmenter,
trace__find_syscall_bpf_prog()fell back tounaugmented_prog(syscall_unaugmented). However,syscall_unaugmentedis asys_enterprogram that outputs enter arguments to__augmented_syscalls__.As a consequence, when an unaugmented syscall exited,
sys_exittail-calledsyscall_unaugmented, which interpreted the exit arguments as enter arguments and emitted a duplicate, corruptsys_enterevent into__augmented_syscalls__right as the syscall completed.Fix this by:
1. Returning NULL fromtrace__find_syscall_bpf_prog()when looking up exit augmenters and none is found.
2. Returning -1 fromtrace__bpf_prog_sys_exit_fd()when no exit program is present.
3. Only updatingmap_exit_fdwhenprog_fd>= 0.
4. Clearing err = 0 whentrace__bpf_sys_enter_beauty_map()returns non-zero (indicating the syscall has no augmentable pointer arguments) before continuing the loop, so a trailing run of such syscalls (e.g. 'perf trace -e close') does not leave err non-zero on return and abort the session.
Quoted 2 lines
Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Ian Rogers
reviewerPATCH 4/13 perf trace: Filter events in BPF and avoid tracepoint vetoes
In reply to Ian RogersThe BPF augmented_raw_syscalls sys_enter and sys_exit programs returned 0 for syscalls that were not of interest. Returning 0 from a tracepoint BPF program vetoes the event for the whole system, so an unrelated concurrent perf trace, perf record or ftrace session listening to raw_syscalls would silently lose events. This is a cross-session side effect and shows up as flaky failures when perf tests run in parallel.
Furthermore, syscall_unaugmented previously returned 1 without writing anything to the __augmented_syscalls__ ring buffer. This forced userspace perf trace to listen to both raw_syscalls:sys_enter and __augmented_syscalls__ in its evlist, requiring userspace event deduplication.
Address these issues:
1. In augmented_raw_syscalls.bpf.c, never return 0 from tracepoint
handlers: return 1 so non-traced syscalls pass through without vetoing other concurrent listeners.
2. Introduce pids_to_trace and syscalls_to_trace BPF hash maps to perform targeted filtering directly in BPF. Unselected syscalls or PIDs return 1 immediately without writing to the buffer.
3. In syscall_unaugmented, output the unaugmented enter payload into __augmented_syscalls__ and return 1. Change its section from SEC("tp/raw_syscalls/sys_enter") to SEC("tp/syscalls/sys_enter_unaugmented") so libbpf does not attempt to auto-attach it to raw_syscalls:sys_enter.
4. In bpf_trace_augment.c, add helpers to configure target PIDs and syscalls in the BPF maps, setting the activation flags (has_pids_to_trace, has_syscalls_to_trace) only after the maps are fully populated so already-attached BPF programs do not filter against a half-filled map. Explicitly attach only sys_enter and sys_exit via an attach_prog() helper that saves -errno before calling pr_debug() or bpf_link__destroy().
Destroy the skeleton on every failure path. Leaving a loaded but unusable skeleton behind is not inert: the setters called later from trace__run() would program its maps, and a partial attach would leave a BPF program live on raw_syscalls for a session that never starts. Since augmented_syscalls__{prepare,create_bpf_output}() failures fall back to unaugmented tracing rather than aborting, those setters have to become no-ops, which they only do once skel is NULL again.
errno is used directly here, so include <errno.h> rather than relying on it arriving via another header, which it does not under musl.
5. In builtin-trace.c, hook trace__set_ev_qualifier_filter() and PID filtering into the BPF maps. When __augmented_syscalls__ is active, remove raw_syscalls:sys_enter from trace.evlist since all traced enter events (both augmented and unaugmented) are now emitted by BPF into __augmented_syscalls__. Restore tracking on the remaining evsel via evlist__set_tracking_event() so PERF_RECORD_COMM and tracking events continue to be recorded. Errors from augmented_syscalls__set_target_syscalls() are reported and propagated, the tracepoint filter string is freed on every exit path, and an allocation failure in trace__set_filter_pids() now returns -ENOMEM instead of being silently ignored.
Note that in trace__set_filter_pids() the target pids and the filtered pids are two independent axes and both have to be programmed. Naming pids to leave out with --filter-pids does not widen -p/-t or a workload to the whole system, and a BPF tracepoint program is attached system wide rather than to the target's file descriptors, so pids_to_trace is the only thing keeping other tasks out.
6. Add --syscall-augment option (defaulting to true) to allow users to explicitly use --no-syscall-augment to run perf trace in the classic unaugmented tracepoint mode without BPF. When BPF is unavailable or disabled, ensure the non-augmented tracepoint path cleanly configures sys_enter and sys_exit without duplicate entries.
--- tools/perf/Documentation/perf-trace.txt | 5 + tools/perf/builtin-trace.c | 146 +++++++++++++++--- .../bpf_skel/augmented_raw_syscalls.bpf.c | 133 ++++++++++++++-- tools/perf/util/bpf_trace_augment.c | 135 +++++++++++++++- tools/perf/util/trace_augment.h | 33 ++++ 5 files changed, 414 insertions(+), 38 deletions(-)
tools/perf/Documentation/perf-trace.txt +5 -0
@@ -260,6 +260,11 @@ the thread executes on the designated CPUs. Default is to monitor all CPUs. Maximum number of lines in the summary mode. Note that this applies to each entry (thread or cgroup). +--syscall-augment:: + Augment syscalls with BPF. Enabled by default when BPF support is available. + Use --no-syscall-augment to disable BPF augmentation and fall back to the + unaugmented tracepoint approach. + PAGEFAULTS ----------
tools/perf/builtin-trace.c +124 -22
245 lines not on this page · raw message
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +118 -15
201 lines not on this page · raw message
tools/perf/util/bpf_trace_augment.c +134 -1
165 lines not on this page · raw message
tools/perf/util/trace_augment.h +33 -0
@@ -12,6 +12,11 @@ int augmented_syscalls__prepare(void);int augmented_syscalls__create_bpf_output (struct evlist *evlist);void augmented_syscalls__setup_bpf_output (void );int augmented_syscalls__set_filter_pids (unsigned int nr,pid_t *pids); +int augmented_syscalls__set_target_pids (unsigned int nr,pid_t *pids); +int augmented_syscalls__add_target_pid (pid_t pid); +int augmented_syscalls__del_target_pid (pid_t pid); +bool augmented_syscalls__has_target_pid (pid_t pid); +int augmented_syscalls__set_target_syscalls (unsigned int nr,int *syscall_ids,bool not_syscalls);int augmented_syscalls__get_map_fds (int *enter_fd,int *exit_fd,int *beauty_fd);struct bpf_program *augmented_syscalls__find_by_title (const char *name);struct bpf_program *augmented_syscalls__unaugmented (void );
@@ -39,6 +44,34 @@ static inline int augmented_syscalls__set_filter_pids(unsigned int nr __maybe_unreturn 0 ; } +static inline int augmented_syscalls__set_target_pids (unsigned int nr__maybe_unused , +pid_t *pids__maybe_unused ) +{ +return 0 ; +} + +static inline int augmented_syscalls__add_target_pid (pid_t pid__maybe_unused ) +{ +return 0 ; +} + +static inline int augmented_syscalls__del_target_pid (pid_t pid__maybe_unused ) +{ +return 0 ; +} + +static inline bool augmented_syscalls__has_target_pid (pid_t pid__maybe_unused ) +{ +return false; +} + +static inline int augmented_syscalls__set_target_syscalls (unsigned int nr__maybe_unused , +int *syscall_ids__maybe_unused , +bool not_syscalls__maybe_unused ) +{ +return 0 ; +} +static inline int augmented_syscalls__get_map_fds (int *enter_fd__maybe_unused ,int *exit_fd__maybe_unused ,int *beauty_fd__maybe_unused )
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v1 04/13] perf trace: Filter events in BPF and avoid tracepoint vetoes
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] perf trace: filter string allocation failure is silently ignored --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] perf trace: filter string allocation failure is silently ignored --
commit b8bbff6d8561525fa9959a8a378bdbe134e7f00b
Author: Ian Rogers <[email protected]>
perf trace: Filter events in BPF and avoid tracepoint vetoes
The BPF augmented_raw_syscalls sys_enter and sys_exit programs returned 0 for syscalls that were not of interest. Returning 0 from a tracepoint BPF program vetoes the event for the whole system, so an unrelated concurrent perf trace, perf record or ftrace session listening to raw_syscalls would silently lose events.
Quoted 4 lines
tools/perf/builtin-trace.c +0
-0
[ ... ]
tools/perf/builtin-trace.c static int trace__set_ev_qualifier_tp_filter(struct trace *trace) · line 4071
Quoted 12 lines
tools/perf/builtin-trace.c +1
-1@@ -4043,7 +4061,7 @@ static int trace__add_syscall_newtp(struct trace *trace)static int trace__set_ev_qualifier_tp_filter (struct trace *trace) { -int err =-1 ; +int err =0 ;struct evsel *sys_exit; char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier, trace->ev_qualifier_ids.nr,@@ -4052,10 +4070,17 @@ static int trace__set_ev_qualifier_tp_filter(struct trace *trace)if (filter ==NULL ) goto out_enomem;
[Severity: Medium]
Does this change from err = -1 to err = 0 silently ignore the filter string allocation failure?
If asprintf_expr_inout_ints() returns NULL due to ENOMEM in trace__set_ev_qualifier_tp_filter(), the code jumps to out_enomem:
out_enomem:
errno = ENOMEM;
goto out;The out block then returns err. Since err is now 0, it returns success instead of a negative error code. Could this result in missing tracepoint filters and incorrectly collected events?
tools/perf/util/bpf_trace_augment.c line 3
Quoted 8 lines
tools/perf/util/bpf_trace_augment.c +1
-0@@ -1,4 +1,5 @@#include <bpf/libbpf.h> +#include <errno.h> #include <internal/xyarray.h>
[Severity: Low]
This is a pre-existing issue, but does this file fail to compile under musl libc due to missing explicit POSIX header inclusions?
The codebase uses strcmp() in augmented_syscalls__find_by_title(), but string.h is not explicitly included. Musl libc does not guarantee implicit header inclusions.
tools/perf/util/trace_augment.h int augmented_syscalls__prepare(void); · line 16
Quoted 10 lines
tools/perf/util/trace_augment.h +2
-0@@ -12,6 +12,11 @@ int augmented_syscalls__prepare(void);int augmented_syscalls__create_bpf_output (struct evlist *evlist);void augmented_syscalls__setup_bpf_output (void );int augmented_syscalls__set_filter_pids (unsigned int nr,pid_t *pids); +int augmented_syscalls__set_target_pids (unsigned int nr,pid_t *pids); +int augmented_syscalls__add_target_pid (pid_t pid);
[Severity: Low]
This isn't a bug introduced by this patch, but the POSIX type pid_t is used in function prototypes here without explicitly including sys/types.h.
Can this omission break compilation on systems using musl libc where types like pid_t are strictly separated?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Ian Rogers
reviewerPATCH 5/13 perf trace: Handle fork and exit directly in BPF filter maps
In reply to Ian RogersUpdating target or filtered PIDs in userspace upon processing PERF_RECORD_FORK and PERF_RECORD_EXIT events introduces latency between event occurrence and userspace BPF map updates. If a newly forked child executes system calls before userspace processes PERF_RECORD_FORK, those syscalls may be dropped by BPF PID filtering. Conversely, if userspace evicts PIDs asynchronously on PERF_RECORD_EXIT, the kernel may recycle a PID before userspace processes the exit event, causing the late eviction to silently drop a newly created task that received the recycled PID.
Address this by attaching BTF-typed raw tracepoint BPF programs directly to the scheduler task lifetime tracepoints:
1. Attach SEC("tp_btf/sched_process_fork") (sched_process_fork), which runs in copy_process() in the parent's context before wake_up_new_task() wakes the child. Using tp_btf rather than SEC("tp/sched/sched_process_fork") receives the stable TP_PROTO arguments (struct task_struct *parent, struct task_struct *child) rather than the tracepoint ring-buffer record (TP_STRUCT__entry), whose layout changed in Linux 6.16 when parent_comm and child_comm were converted from 16-byte arrays to 4-byte __data_loc strings. When inherit is enabled and the parent's PID or TGID is in pids_to_trace or pids_filtered, insert child->pid into the corresponding map immediately. Because child->pid is task_struct.pid (the global initial-namespace PID), this works accurately across PID namespaces without aliasing host PIDs, and covers both new processes and CLONE_THREAD threads without needing real_parent CO-RE walks or syscall-return heuristics.
2. Attach SEC("tp_btf/sched_process_exit") (sched_process_exit), which runs in do_exit() for every task in its own context, including tasks killed by signals (SIGKILL, SIGSEGV, etc.) and secondary threads torn down implicitly by exit_group. Delete the dying task's PID from pids_to_trace and pids_filtered immediately in kernel space, eliminating both map leaks and any asynchronous userspace eviction window where PID recycling could occur.
3. Attach SEC("tp_btf/sched_process_exec") (sched_process_exec) to follow the one case where a live task's pid changes underneath the maps. When a thread that is not the group leader execs, de_thread() kills the leader and hands the leader's pid, which is the tgid, to the exec'ing thread. The leader dies first, so sched_process_exit() has already dropped exactly the pid the survivor now holds, and the survivor's old entry would be stranded in the map for good. Move the entry from old_pid to p->pid. old_pid is sampled in bprm_execve() before de_thread() runs, so the ordinary group leader exec is a no-op here.
4. With every live task registered before its first syscall and evicted in do_exit(), simplify pid_to_trace__has() and pid_filter__has() to single BPF hash map lookups, and move bpf_probe_read_kernel() in sys_exit back after the PID filter checks.
5. Pass the inherit flag from userspace to BPF .rodata via augmented_syscalls__prepare(!trace.opts.no_inherit), and split attaching out of it into augmented_syscalls__attach(), called from trace__run() once the pid, syscall and program array maps have all been programmed. These are system wide programs, so from the instant they attach they alone decide what is traced: attaching at load time, as before, left a window in which a target could fork without sched_process_fork() knowing the parent was a target, and with the userspace fork handling gone there was nothing left to recover it. The scheduler programs are attached ahead of sys_enter and sys_exit for the same reason. Set has_pids_filtered only after populating pids_filtered.
6. Remove the userspace BPF map updates from PERF_RECORD_FORK and PERF_RECORD_EXIT in trace__process_event(), and delete the now-unused augmented_syscalls__{add,del,has}_target_pid() helpers. No coverage is lost with them: those records only come into being once the ring buffers are mapped by evlist__do_mmap() and the events are switched on by evlist__enable(), both of which run after augmented_syscalls__attach() in trace__run(), and they are then acted on later still, whenever the poll loop gets round to them. The scheduler programs therefore go live strictly earlier than the userspace path could ever have reacted.
7. Gate pid_filter__has() on a has_pids_filtered flag in .bss so the common case without --filter-pids performs no map lookups, and size pids_to_trace and pids_filtered at 16384 entries. pids_filtered is grown from 64 because it is no longer just the handful of pids userspace names: sched_process_fork() adds every descendant of those, so a --filter-pids target that forks or is heavily threaded needs the same headroom as a traced one.
A fork or exit is still not seen if it happens before the programs are attached, that is between evlist__create_maps() scanning /proc for a -p target and augmented_syscalls__attach(). Such a window is inherent in programming a system wide filter before switching it on, and as above the userspace handling did not cover it either. What it costs is small:
a child forked in the window is still traced through the tracepoints its parent's events were inherited by, only unaugmented, because sys_enter returns 1 for a pid that is not in the map rather than vetoing the tracepoint. A workload started by 'perf trace -- cmd' cannot hit it at all, as evlist__prepare_workload() leaves the child blocked on a pipe until evlist__start_workload(), well after the attach.
--- tools/perf/builtin-trace.c | 39 ++-- .../bpf_skel/augmented_raw_syscalls.bpf.c | 178 +++++++++++++++++- tools/perf/util/bpf_trace_augment.c | 116 +++++++----- tools/perf/util/trace_augment.h | 28 +-- 4 files changed, 262 insertions(+), 99 deletions(-)
tools/perf/builtin-trace.c +16 -23
@@ -2055,23 +2055,6 @@ static int trace__process_event(struct trace *trace, struct machine *machine, "LOST %" PRIu64 " events!\n", (u64)event->lost.lost); ret =machine__process_lost_event (machine, event, sample);break ; -case PERF_RECORD_FORK : -if (trace->raw_augmented_syscalls && - (augmented_syscalls__has_target_pid (event->fork.ppid) || -augmented_syscalls__has_target_pid (event->fork.ptid))) { -augmented_syscalls__add_target_pid (event->fork.pid); - } - ret =machine__process_fork_event (machine, event, sample); -break ; -case PERF_RECORD_EXIT : -if (trace->raw_augmented_syscalls) { -if (event->fork.pid == event->fork.tid) -augmented_syscalls__del_target_pid (event->fork.pid); -else -augmented_syscalls__del_target_pid (event->fork.tid); - } - ret =machine__process_exit_event (machine, event, sample); -break ;default : ret =machine__process_event (machine, event, sample);break ;
@@ -4982,6 +4965,16 @@ static int trace__run(struct trace *trace, int argc, const char **argv) } } +/* +* Everything the BPF programs filter on is now in their maps, so it is +* safe to let them run. They are attached system wide, so anything +* before this point would have been filtered against a map that was +* still being built up. +*/ + err =augmented_syscalls__attach (); +if (err <0 ) + goto out_errno; +/* * If the "close" syscall is not traced, then we will not have the * opportunity to, in syscall_arg__scnprintf_close_fd() invalidate the
@@ -6074,7 +6067,7 @@ int cmd_trace(int argc, const char **argv) goto skip_augmentation; } - err =augmented_syscalls__prepare (); + err =augmented_syscalls__prepare (!trace.opts.no_inherit);if (err <0 ) goto skip_augmentation;
@@ -6085,11 +6078,11 @@ int cmd_trace(int argc, const char **argv) trace.syscalls.events.bpf_output =evlist__last (trace.evlist); }else {/* -* augmented_syscalls__prepare() already attached sys_enter and -* sys_exit, which are system wide. Falling through to -* skip_augmentation without undoing that would run a BPF -* program for every syscall on the machine, for the whole -* session, with nothing consuming the output. +* Drop the loaded skeleton before falling back to unaugmented +* tracing. Otherwise the setters called from trace__run() would +* still program its maps, and augmented_syscalls__attach() would +* then put system wide BPF programs on raw_syscalls for a +* session with nothing consuming their output. */ pr_debug ("Failed to create the augmented syscalls bpf-output event, disabling augmentation\n" );augmented_syscalls__cleanup ();
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +168 -10
239 lines not on this page · raw message
tools/perf/util/bpf_trace_augment.c +70 -46
170 lines not on this page · raw message
tools/perf/util/trace_augment.h +8 -20
@@ -8,14 +8,12 @@ struct evlist;#ifdef HAVE_BPF_SKEL -int augmented_syscalls__prepare (void ); +int augmented_syscalls__prepare (bool inherit); +int augmented_syscalls__attach (void );int augmented_syscalls__create_bpf_output (struct evlist *evlist);void augmented_syscalls__setup_bpf_output (void );int augmented_syscalls__set_filter_pids (unsigned int nr,pid_t *pids);int augmented_syscalls__set_target_pids (unsigned int nr,pid_t *pids); -int augmented_syscalls__add_target_pid (pid_t pid); -int augmented_syscalls__del_target_pid (pid_t pid); -bool augmented_syscalls__has_target_pid (pid_t pid);int augmented_syscalls__set_target_syscalls (unsigned int nr,int *syscall_ids,bool not_syscalls);int augmented_syscalls__get_map_fds (int *enter_fd,int *exit_fd,int *beauty_fd);struct bpf_program *augmented_syscalls__find_by_title (const char *name);
@@ -24,11 +22,16 @@ void augmented_syscalls__cleanup(void);#else /* !HAVE_BPF_SKEL */ -static inline int augmented_syscalls__prepare (void ) +static inline int augmented_syscalls__prepare (bool inherit__maybe_unused ) {return -1 ; } +static inline int augmented_syscalls__attach (void ) +{ +return 0 ; +} +static inline int augmented_syscalls__create_bpf_output (struct evlist *evlist__maybe_unused ) {return -1 ;
@@ -50,21 +53,6 @@ static inline int augmented_syscalls__set_target_pids(unsigned int nr __maybe_unreturn 0 ; } -static inline int augmented_syscalls__add_target_pid (pid_t pid__maybe_unused ) -{ -return 0 ; -} - -static inline int augmented_syscalls__del_target_pid (pid_t pid__maybe_unused ) -{ -return 0 ; -} - -static inline bool augmented_syscalls__has_target_pid (pid_t pid__maybe_unused ) -{ -return false; -} -static inline int augmented_syscalls__set_target_syscalls (unsigned int nr__maybe_unused ,int *syscall_ids__maybe_unused ,bool not_syscalls__maybe_unused )
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v1 05/13] perf trace: Handle fork and exit directly in BPF filter maps
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 19 lines
Updating target or filtered PIDs in userspace upon processing
PERF_RECORD_FORKandPERF_RECORD_EXITevents introduces latency between event occurrence and userspace BPF map updates. If a newly forked child executes system calls before userspace processesPERF_RECORD_FORK, those syscalls may be dropped by BPF PID filtering. Conversely, if userspace evicts PIDs asynchronously onPERF_RECORD_EXIT, the kernel may recycle a PID before userspace processes the exit event, causing the late eviction to silently drop a newly created task that received the recycled PID.Address this by attaching BTF-typed raw tracepoint BPF programs directly to the scheduler task lifetime tracepoints:
1. Attach
SEC("tp_btf/sched_process_fork")(sched_process_fork), which runs incopy_process()in the parent's context beforewake_up_new_task()wakes the child. Usingtp_btfrather than
[ ... ]
all, asevlist__prepare_workload()leaves the child blocked on a pipe untilevlist__start_workload(), well after the attach.
Quoted 2 lines
Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Ian Rogers
reviewerPATCH 6/13 perf test test_task_analyzer: Isolate in temporary directory and make non-exclusive
In reply to Ian Rogerstest_task_analyzer.sh writes perf.data and temporary files directly into the current working directory, causing collisions when running tests in parallel.
As a temporary measure until perf script report supports an input file option, resolve perfdir to an absolute path, change directory into $tmpdir for the test duration, and clean up in the exit trap. Remove the (exclusive) tag so the test runs in parallel.
perfdir is derived from $0, which may be relative, so it has to be resolved before the cd into $tmpdir, otherwise both PERF_EXEC_PATH and the cleanup trap point at paths that no longer exist.
--- tools/perf/tests/shell/test_task_analyzer.sh | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
tools/perf/tests/shell/test_task_analyzer.sh +7 -4
@@ -1,8 +1,13 @@ #!/bin/bash -# perf script task-analyzer tests (exclusive) +# perf script task-analyzer tests # SPDX-License-Identifier: GPL-2.0 +# Resolve the source directory before changing the working directory below, +# $0 may be a relative path and would no longer resolve from $tmpdir. +perfdir=$(cd "$(dirname "$0")/../.." && pwd) + tmpdir=$(mktemp -d /tmp/perf-script-task-analyzer-XXXXX) +cd "$tmpdir" || exit 1 # TODO: perf script report only supports input from the CWD perf.data file, make # it support input from any file. perfdata="perf.data"
@@ -11,7 +16,6 @@ csvsummary="$tmpdir/csvsummary" err=0 # set PERF_EXEC_PATH to find scripts in the source directory -perfdir=$(dirname "$0")/../.. if [ -e "$perfdir/scripts/python/Perf-Trace-Util" ]; then export PERF_EXEC_PATH=$perfdir fi
@@ -20,8 +24,7 @@ fi export ASAN_OPTIONS=detect_leaks=0 cleanup() { - rm -f "${perfdata}" - rm -f "${perfdata}".old + cd "$perfdir" || cd /tmp || exit rm -rf "$tmpdir" trap - exit term int
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v1 06/13] perf test test_task_analyzer: Isolate in temporary directory and make non-exclusive
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] perf: tmpdir leak on cd failure in test_task_analyzer.sh --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] perf: tmpdir leak on cd failure in test_task_analyzer.sh
--
commit dfb340cf14f6e7255460556df8553dcb2273aaae
Author: Ian Rogers <[email protected]>
perf test test_task_analyzer: Isolate in temporary directory and make non-exclusive
This commit changes the test_task_analyzer script to resolve its execution path absolutely, create a temporary directory, and change into it. This isolates the test environment, allowing it to run in parallel without collisions.
Quoted 6 lines
tools/perf/tests/shell/test_task_analyzer.sh +0
-0@@ -1,8 +1,13 @@ #!/bin/bash
tools/perf/tests/shell/test_task_analyzer.sh line 10
Quoted 10 lines
tools/perf/tests/shell/test_task_analyzer.sh +6
-1-# perf script task-analyzer tests (exclusive) +# perf script task-analyzer tests # SPDX-License-Identifier: GPL-2.0 +# Resolve the source directory before changing the working directory below, +# $0 may be a relative path and would no longer resolve from $tmpdir. +perfdir=$(cd "$(dirname "$0")/../.." && pwd) + tmpdir=$(mktemp -d /tmp/perf-script-task-analyzer-XXXXX) +cd "$tmpdir" || exit 1
[Severity: Low]
Does this error path force an immediate exit before the cleanup trap is established? If the change directory fails and the script exits here, it appears the newly created temporary directory will be orphaned in /tmp.
Quoted 3 lines
tools/perf/tests/shell/test_task_analyzer.sh +0
-0# TODO: perf script report only supports input from the CWD perf.data file, make # it support input from any file. perfdata="perf.data"
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Ian Rogers
reviewerPATCH 7/13 perf test common: Do not globally disable tracing events in clear_all_probes
In reply to Ian Rogersecho 0 > /sys/kernel/debug/tracing/events/enable disables tracepoint events system-wide. When running tests in parallel, this kills active tracing and recording sessions in concurrent tests (such as perf trace and perf record).
Remove the global event disable from clear_all_probes().
--- tools/perf/tests/shell/common/init.sh | 1 - 1 file changed, 1 deletion(-)
tools/perf/tests/shell/common/init.sh +0 -1
@@ -132,7 +132,6 @@ check_uprobes_available() clear_all_probes() { - echo 0 > /sys/kernel/debug/tracing/events/enable check_kprobes_available && echo > /sys/kernel/debug/tracing/kprobe_events check_uprobes_available && echo > /sys/kernel/debug/tracing/uprobe_events }
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v1 07/13] perf test common: Do not globally disable tracing events in clear_all_probes
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
echo 0 > /sys/kernel/debug/tracing/events/enabledisables tracepoint events system-wide. When running tests in parallel, this kills active tracing and recording sessions in concurrent tests (such as perf trace and perf record).Remove the global event disable from
clear_all_probes().Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Ian Rogers
reviewerPATCH 8/13 perf test probe_vfs_getname: Scope probe name to PID and make non-exclusive
In reply to Ian RogersThe probe name vfs_getname was hardcoded, causing collisions when tests ran concurrently. Furthermore, cleanup_probe_vfs_getname() used perf probe -d probe:vfs_getname*, deleting probes registered by other parallel tests.
Scope the probe name to the pid, and rename it to getname_flags_$$ so that it no longer begins with "vfs_getname". perf trace calls evlist__add_vfs_getname(), which opens every event matching a hardcoded "probe:vfs_getname*" wildcard, so a perf trace run by any other test would otherwise pin this probe and make perf probe -d fail with -EBUSY. That also unblocks making the perf trace tests non-exclusive later in this series.
Enumerate the probes to record and to delete from perf probe -l, matching ^probe:${vfs_getname}(_[[:digit:]]+)?$ exactly, rather than globbing on ${vfs_getname}*. perf probe appends _1, _2, ... when getname_flags is inlined at more than one call site, so the variants do have to be matched, but since the name now ends in a pid a trailing wildcard would also match the probes of a test whose pid merely starts with this one's, e.g. 123 and 1234.
Remove the (exclusive) tag from probe_vfs_getname.sh and record+script_probe_vfs_getname.sh so they run concurrently in pass 1. trace+probe_vfs_getname.sh has to stay exclusive: it is the one test that wants to be discovered by that wildcard, so it sets vfs_getname to a "vfs_getname_$$" name before sourcing the library, and would then pin its siblings' probes if it ran alongside them. A comment in the test records this.
--- .../perf/tests/shell/lib/probe_vfs_getname.sh | 34 ++++++++++++++++--- tools/perf/tests/shell/probe_vfs_getname.sh | 3 +- .../shell/record+script_probe_vfs_getname.sh | 18 +++++++--- .../tests/shell/trace+probe_vfs_getname.sh | 9 +++++ 4 files changed, 54 insertions(+), 10 deletions(-)
tools/perf/tests/shell/lib/probe_vfs_getname.sh +30 -4
@@ -1,12 +1,38 @@ #!/bin/bash # Arnaldo Carvalho de Melo <[email protected]>, 2017 -perf probe -l 2>&1 | grep -q probe:vfs_getname +# The name of the getname_flags probe added and removed below. +# +# It is scoped to the pid so that tests running in parallel do not collide, +# and it deliberately does not start with "vfs_getname": perf trace calls +# evlist__add_vfs_getname(), which opens everything matching the hardcoded +# "probe:vfs_getname*" wildcard, so a perf trace running in another test would +# otherwise pin this probe and make the 'perf probe -d' below fail with -EBUSY. +# +# trace+probe_vfs_getname.sh is the one test that does want to be found that +# way, so it sets vfs_getname itself before sourcing this file, and is +# (exclusive) as a result. +: "${vfs_getname:=getname_flags_$$}" + +# Print the probes add_probe_vfs_getname() created. perf probe appends _1, _2, +# ... when getname_flags is inlined at more than one call site, so there can be +# several. Match them exactly rather than with a "${vfs_getname}*" glob: the +# name ends in a pid, so such a glob would also match the probes of a test +# whose pid merely starts with this one's, e.g. 123 and 1234. +probes_vfs_getname() { + perf probe -l 2>/dev/null | awk '{print $1}' | + grep -E "^probe:${vfs_getname}(_[[:digit:]]+)?$" +} + +[ -n "$(probes_vfs_getname)" ] had_vfs_getname=$? cleanup_probe_vfs_getname() { if [ $had_vfs_getname -eq 1 ] ; then - perf probe -q -d probe:vfs_getname* + local probe + for probe in $(probes_vfs_getname); do + perf probe -q -d "$probe" + done fi }
@@ -33,8 +59,8 @@ add_probe_vfs_getname() { return 2 fi - perf probe -q "vfs_getname=getname_flags:${line} pathname=result->name:string" || \ - perf probe $add_probe_verbose "vfs_getname=getname_flags:${line} pathname=filename:ustring" || return 1 + perf probe -q "${vfs_getname}=getname_flags:${line} pathname=result->name:string" || \ + perf probe $add_probe_verbose "${vfs_getname}=getname_flags:${line} pathname=filename:ustring" || return 1 fi }
tools/perf/tests/shell/probe_vfs_getname.sh +1 -2
@@ -1,6 +1,5 @@ #!/bin/bash -# Add vfs_getname probe to get syscall args filenames (exclusive) - +# Add vfs_getname probe to get syscall args filenames # SPDX-License-Identifier: GPL-2.0 # Arnaldo Carvalho de Melo <[email protected]>, 2017
tools/perf/tests/shell/record+script_probe_vfs_getname.sh +14 -4
@@ -1,5 +1,5 @@ #!/bin/bash -# Use vfs_getname probe to get syscall args filenames (exclusive) +# Use vfs_getname probe to get syscall args filenames # Uses the 'perf test shell' library to add probe:vfs_getname to the system # then use it with 'perf record' using 'touch' to write to a temp file, then
@@ -17,22 +17,32 @@ skip_if_no_perf_probe || exit 2 # shellcheck source=lib/probe_vfs_getname.sh . "$(dirname "$0")/lib/probe_vfs_getname.sh" +# shellcheck disable=SC2154 # vfs_getname is assigned in lib/probe_vfs_getname.sh record_open_file() { echo "Recording open file:" # Check presence of libtraceevent support to run perf record - skip_no_probe_record_support "probe:vfs_getname*" + skip_no_probe_record_support if [ $? -eq 2 ]; then echo "WARN: Skipping test record_open_file. No libtraceevent support" return 2 fi - perf record -o ${perfdata} -e probe:vfs_getname\* touch $file + # Record every probe the inlining of getname_flags produced, naming + # them exactly rather than with a "${vfs_getname}*" glob, which would + # also match the probes of a test whose pid starts with this one's. + local events + events=$(probes_vfs_getname | paste -sd, -) + if [ -z "${events}" ] ; then + echo "FAIL: no ${vfs_getname} probe to record" + return 1 + fi + perf record -o ${perfdata} -e "${events}" touch $file } perf_script_filenames() { echo "Looking at perf.data file for vfs_getname records for the file we touched:" perf script -i ${perfdata} | \ - grep -E " +touch +[0-9]+ +\[[0-9]+\] +[0-9]+\.[0-9]+: +probe:vfs_getname[_0-9]*: +\([[:xdigit:]]+\) +pathname=\"${file}\"" + grep -E " +touch +[0-9]+ +\[[0-9]+\] +[0-9]+\.[0-9]+: +probe:${vfs_getname}(_[0-9]+)?: +\([[:xdigit:]]+\) +pathname=\"${file}\"" } add_probe_vfs_getname
tools/perf/tests/shell/trace+probe_vfs_getname.sh +9 -0
@@ -10,6 +10,13 @@ # SPDX-License-Identifier: GPL-2.0 # Arnaldo Carvalho de Melo <[email protected]>, 2017 +# This test must stay exclusive, and is the only one of the probe tests that +# does: it does not name the event it uses. perf trace discovers it with the +# hardcoded "probe:vfs_getname*" wildcard in evlist__add_vfs_getname(), so the +# probe has to carry that prefix, and a parallel run of this test would then +# also match, and pin, the probes of the other tests. The sibling tests avoid +# all of this by using a name that the wildcard cannot reach. + # shellcheck source=lib/probe.sh . "$(dirname $0)"/lib/probe.sh
@@ -17,6 +24,8 @@ skip_if_no_perf_probe || exit 2 skip_if_no_perf_trace || exit 2 [ "$(id -u)" = 0 ] || exit 2 +# shellcheck disable=SC2034 # consumed by lib/probe_vfs_getname.sh +vfs_getname="vfs_getname_$$" . "$(dirname $0)"/lib/probe_vfs_getname.sh trace_open_vfs_getname() {
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v1 08/13] perf test probe_vfs_getname: Scope probe name to PID and make non-exclusive
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
The probe name
vfs_getnamewas hardcoded, causing collisions when tests ran concurrently. Furthermore,cleanup_probe_vfs_getname()usedperf probe -d probe:vfs_getname*, deleting probes registered by other parallel tests.Scope the probe name to the pid, and rename it to
getname_flags_$$so that it no longer begins with "vfs_getname". perf trace callsevlist__add_vfs_getname(), which opens every event matching a hardcoded "probe:vfs_getname*" wildcard, so a perf trace run by any other test would otherwise pin this probe and makeperf probe -dfail with-EBUSY. That also unblocks making the perf trace tests non-exclusive later in this series.
Quoted 8 lines
Enumerate the probes to record and to delete from
perf probe -l, matching^probe:${vfs_getname}(_[[:digit:]]+)?$exactly, rather than [ ... ]
its siblings' probes if it ran alongside them. A comment in the test records this.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Ian Rogers
reviewerPATCH 9/13 perf test record+probe_libc_inet_pton: Scope event to PID, add retries, and make non-exclusive
In reply to Ian RogersThe uprobe name was not scoped to PID, and concurrent writes to /sys/kernel/debug/tracing/uprobe_events can occasionally return -EBUSY when another process holds the tracefs inode lock.
Scope the probe event name with $$ (inet_pton_$$=inet_pton) and add a retry loop with backoff for uprobe addition. Drop the (exclusive) tag so the test can run in parallel during pass 1.
A PID scoped probe is no longer cleaned up by any other test, so add an EXIT/TERM/INT trap to delete it, otherwise an interrupted run leaks the uprobe into the system. The trap is installed only after the root and IPv6 checks that exit 2 to skip the test, as trap_cleanup() exits 1 and would otherwise turn those skips into failures. Deletion enumerates the probes from perf probe -l, matching ^probe_libc:inet_pton_$$(_[[:digit:]]+)?$ exactly, rather than reading $event_name: a signal arriving after perf probe injected the uprobe but before the assignment completed would leave that variable empty and leak the probe, and an inet_pton_$$* glob would reach the probe of a test whose pid merely starts with this one's.
While here use mktemp rather than mktemp -u for the temporary files:
this test runs as root in a world writable /tmp, and predicting a name without creating it allows another user to win the race and plant a symlink. The perf.data check becomes -s rather than -e as mktemp now pre-creates an empty file.
--- .../shell/record+probe_libc_inet_pton.sh | 83 ++++++++++++++----- 1 file changed, 63 insertions(+), 20 deletions(-)
tools/perf/tests/shell/record+probe_libc_inet_pton.sh +63 -20
@@ -1,5 +1,5 @@ #!/bin/bash -# probe libc's inet_pton & backtrace it with ping (exclusive) +# probe libc's inet_pton & backtrace it with ping # Installs a probe on libc's inet_pton function, that will use uprobes, # then use 'perf trace' on a ping to localhost asking for just one packet
@@ -21,20 +21,30 @@ nm -Dg $libc 2>/dev/null | grep -F -q inet_pton || exit 254 event_pattern='probe_libc:inet_pton(_[[:digit:]]+)?' add_libc_inet_pton_event() { + local attempts=0 + while [ $attempts -lt 3 ]; do + event_name=$(perf probe -f -x $libc -a "inet_pton_$$=inet_pton" 2>&1 | \ + awk -v ep="$event_pattern" -v l="$libc" '$0 ~ ep && $0 ~ \ + ("\\(on inet_pton in " l "\\)") {print $1}' | head -n 1) + + if [ -n "$event_name" ]; then + return 0 + fi + attempts=$((attempts + 1)) + sleep 0.1 + done - event_name=$(perf probe -f -x $libc -a inet_pton 2>&1 | \ - awk -v ep="$event_pattern" -v l="$libc" '$0 ~ ep && $0 ~ \ - ("\\(on inet_pton in " l "\\)") {print $1}' | head -n 1) - - if [ $? -ne 0 ] || [ -z "$event_name" ] ; then - printf "FAIL: could not add event\n" - return 1 - fi + printf "FAIL: could not add event\n" + return 1 } trace_libc_inet_pton_backtrace() { - expected=`mktemp -u /tmp/expected.XXX` + # Create the files rather than just reserving names with mktemp -u: + # this runs as root and /tmp is world writable, so a predictable name + # that is written to later can be pre-created as a symlink by an + # unprivileged user and used to clobber an arbitrary file. + expected=$(mktemp /tmp/expected.XXX) echo "ping[][0-9 \.:]+$event_name: \([[:xdigit:]]+\)" > $expected echo ".*inet_pton\+0x[[:xdigit:]]+[[:space:]]\($libc|inlined\)$" >> $expected
@@ -50,8 +60,8 @@ trace_libc_inet_pton_backtrace() { ;; esac - perf_data=`mktemp -u /tmp/perf.data.XXX` - perf_script=`mktemp -u /tmp/perf.script.XXX` + perf_data=$(mktemp /tmp/perf.data.XXX) + perf_script=$(mktemp /tmp/perf.script.XXX) # Check presence of libtraceevent support to run perf record skip_no_probe_record_support "$event_name/$eventattr/"
@@ -61,9 +71,10 @@ trace_libc_inet_pton_backtrace() { fi perf record -e $event_name/$eventattr/ -o $perf_data ping -6 -c 1 ::1 > /dev/null 2>&1 - # check if perf data file got created in above step. - if [ ! -e $perf_data ]; then - printf "FAIL: perf record failed to create \"%s\" \n" "$perf_data" + # Check perf record actually wrote data. mktemp already created the + # file, so test that it is non-empty rather than that it exists. + if [ ! -s $perf_data ]; then + printf "FAIL: perf record failed to write \"%s\" \n" "$perf_data" return 1 fi perf script -i $perf_data | tac | grep -m1 ^ping -B9 | tac > $perf_script
@@ -97,21 +108,53 @@ trace_libc_inet_pton_backtrace() { # even if the perf script output does not match. } +# Print the pid scoped uprobes this test may have created. perf probe appends +# _1, _2, ... when the name is already taken, so match those too, but anchor +# the match: an "inet_pton_$$*" glob would also match the probe of a test whose +# pid merely starts with this one's, e.g. 123 and 1234. +libc_inet_pton_events() { + perf probe -l 2>/dev/null | awk '{print $1}' | + grep -E "^probe_libc:inet_pton_$$(_[[:digit:]]+)?$" +} + delete_libc_inet_pton_event() { + # Ask the kernel what is actually there rather than trusting + # $event_name: a signal arriving after perf probe injected the uprobe + # but before the assignment to event_name completed would otherwise + # leave the variable empty and leak the probe. + local probe + for probe in $(libc_inet_pton_events); do + perf probe -q -d "$probe" + done +} - if [ -n "$event_name" ] ; then - perf probe -q -d $event_name - fi +cleanup() { + rm -f ${perf_data} ${perf_script} ${expected} + delete_libc_inet_pton_event + + trap - EXIT TERM INT +} + +trap_cleanup() { + cleanup + exit 1 } # Check for IPv6 interface existence ip a sh lo | grep -F -q inet6 || exit 2 [ "$(id -u)" = 0 ] || exit 2 +# Install the trap only now that the skips above are out of the way: it exits +# 1, so arming it any earlier would turn an 'exit 2' skip into a failure. +# +# The event name is pid scoped, so unlike the old fixed name an orphan left +# behind by an interrupted run is never overwritten by a later run: it would +# stay in the kernel forever. Always clean up, including on a signal. +trap trap_cleanup EXIT TERM INT + skip_if_no_perf_probe && \ add_libc_inet_pton_event && \ trace_libc_inet_pton_backtrace err=$? -rm -f ${perf_data} ${perf_script} ${expected} -delete_libc_inet_pton_event +cleanup exit $err
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v1 09/13] perf test record+probe_libc_inet_pton: Scope event to PID, add retries, and make non-exclusive
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 20 lines
The uprobe name was not scoped to PID, and concurrent writes to
/sys/kernel/debug/tracing/uprobe_eventscan occasionally return-EBUSYwhen another process holds the tracefs inode lock.Scope the probe event name with
$$(inet_pton_$$=inet_pton) and add a retry loop with backoff for uprobe addition. Drop the(exclusive)tag so the test can run in parallel during pass 1.A PID scoped probe is no longer cleaned up by any other test, so add an EXIT/TERM/INT trap to delete it, otherwise an interrupted run leaks the uprobe into the system. The trap is installed only after the root and IPv6 checks that
exit 2to skip the test, astrap_cleanup()exits 1 and would otherwise turn those skips into failures. Deletion enumerates the probes fromperf probe -l, matching^probe_libc:inet_pton_$$(_[[:digit:]]+)?$exactly, rather than reading $event_name: a signal arriving after perf probe injected the uprobe but before the assignment completed would leave that variable empty and leak the probe, and aninet_pton_$$*glob would reach the probe of a test whose pid merely starts with this one's.
Quoted 8 lines
While here use mktemp rather than mktemp -u for the temporary files:
this test runs as root in a world writable /tmp, and predicting a name without creating it allows another user to win the race and plant a symlink. The perf.data check becomes -s rather than -e as mktemp now pre-creates an empty file.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
Ian Rogers
reviewerPATCH 10/13 perf test trace_summary: Improve error diagnostics
In reply to Ian RogersWhen pattern matching fails in test_perf_trace(), print the command that failed along with the actual match count, the matching lines found, and the first 15 lines of output to aid debugging.
--- tools/perf/tests/shell/trace_summary.sh | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
tools/perf/tests/shell/trace_summary.sh +8 -4
@@ -28,10 +28,14 @@ test_perf_trace() { count=$(grep -E -c -m 3 "${search}" ${OUTPUT}) if [ "${count}" != "3" ]; then - echo "Error: cannot find enough pattern ${search} in the output" - cat ${OUTPUT} - rm -f ${OUTPUT} - exit 1 + echo "Error: cannot find enough pattern ${search} (count=${count}) in output of:" + echo "Error: perf trace ${args} -- ${workload}" + echo "Error: matched lines:" + grep -E "${search}" ${OUTPUT} || echo "none" + echo "Error: first 15 lines of output:" + head -n 15 ${OUTPUT} + rm -f ${OUTPUT} + exit 1 fi }
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v1 10/13] perf test trace_summary: Improve error diagnostics
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines
When pattern matching fails in
test_perf_trace(), print the command that failed along with the actual match count, the matching lines found, and the first 15 lines of output to aid debugging.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Ian Rogers
reviewerPATCH 11/13 perf test trace_btf_general: Drop --max-events=1 and make non-exclusive
In reply to Ian Rogerstrace_btf_general.sh used --max-events=1 with perf trace on commands such as mv, echo, and sleep. When background activity occurs or tests run in parallel, perf trace can capture an event from an unrelated process and exit prematurely before recording the target command's syscalls.
Drop --max-events=1 and let tracing run until the command completes, checking for the expected output with grep (matching trace_btf_enum.sh). Remove the (exclusive) tag so the test runs in parallel.
Running perf trace in parallel is only safe now that the probe tests no longer name their probes "vfs_getname...": perf trace opens everything matching a hardcoded "probe:vfs_getname*" wildcard, which used to pin those probes and make their cleanup fail with -EBUSY.
--- tools/perf/tests/shell/trace_btf_general.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
tools/perf/tests/shell/trace_btf_general.sh +4 -4
@@ -1,5 +1,5 @@ #!/bin/bash -# perf trace BTF general tests (exclusive) +# perf trace BTF general tests # SPDX-License-Identifier: GPL-2.0 err=0
@@ -27,7 +27,7 @@ check_vmlinux() { trace_test_string() { echo "Testing perf trace's string augmentation" - output="$(perf trace --sort-events -e renameat* --max-events=1 -- mv ${file1} ${file2} 2>&1)" + output="$(perf trace --sort-events -e renameat* -- mv ${file1} ${file2} 2>&1)" if ! echo "$output" | grep -q -E "^mv/[0-9]+ renameat(2)?\(.*, \"${file1}\", .*, \"${file2}\", .*\) += +[0-9]+$" then printf "String augmentation test failed, output:\n$output\n"
@@ -38,7 +38,7 @@ trace_test_string() { trace_test_buffer() { echo "Testing perf trace's buffer augmentation" # echo will insert a newline (\10) at the end of the buffer - output="$(perf trace --sort-events -e write --max-events=1 -- echo "${buffer}" 2>&1)" + output="$(perf trace --sort-events -e write -- echo "${buffer}" 2>&1)" if ! echo "$output" | grep -q -E "^echo/[0-9]+ write\([0-9]+, ${buffer}.*, [0-9]+\) += +[0-9]+$" then printf "Buffer augmentation test failed, output:\n$output\n"
@@ -48,7 +48,7 @@ trace_test_buffer() { trace_test_struct_btf() { echo "Testing perf trace's struct augmentation" - output="$(perf trace --sort-events -e clock_nanosleep --force-btf --max-events=1 -- sleep 1 2>&1)" + output="$(perf trace --sort-events -e clock_nanosleep --force-btf -- sleep 1 2>&1)" if ! echo "$output" | grep -q -E "^sleep/[0-9]+ clock_nanosleep\(0, 0, \{1,.*\}, 0x[0-9a-f]+\) += +[0-9]+$" then printf "BTF struct augmentation test failed, output:\n$output\n"
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v1 11/13] perf test trace_btf_general: Drop --max-events=1 and make non-exclusive
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines
trace_btf_general.shused--max-events=1withperf traceon commands such asmv,echo, andsleep. When background activity occurs or tests run in parallel,perf tracecan capture an event from an unrelated process and exit prematurely before recording the target command's syscalls.Drop
--max-events=1and let tracing run until the command completes, checking for the expected output with grep (matchingtrace_btf_enum.sh). Remove the (exclusive) tag so the test runs in parallel.
Quoted 7 lines
Running perf trace in parallel is only safe now that the probe tests no longer name their probes "
vfs_getname...": perf trace opens everything matching a hardcoded "probe:vfs_getname*" wildcard, which used to pin those probes and make their cleanup fail with-EBUSY.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
Ian Rogers
reviewerPATCH 12/13 perf test trace_summary: Make non-exclusive
In reply to Ian Rogerstrace_summary.sh tests various summary modes of perf trace. It already directs output to a unique temporary file without polluting the current working directory.
Remove the (exclusive) tag so it can run concurrently in parallel test runs.
Running perf trace in parallel is only safe now that the probe tests no longer name their probes "vfs_getname...": perf trace opens everything matching a hardcoded "probe:vfs_getname*" wildcard, which used to pin those probes and make their cleanup fail with -EBUSY.
--- tools/perf/tests/shell/trace_summary.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/tests/shell/trace_summary.sh +1 -1
@@ -1,5 +1,5 @@ #!/bin/bash -# perf trace summary (exclusive) +# perf trace summary # SPDX-License-Identifier: GPL-2.0 # Check that perf trace works with various summary mode
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v1 12/13] perf test trace_summary: Make non-exclusive
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines
trace_summary.shtests various summary modes ofperf trace. It already directs output to a unique temporary file without polluting the current working directory.Remove the (exclusive) tag so it can run concurrently in parallel test runs.
Quoted 7 lines
Running perf trace in parallel is only safe now that the probe tests no longer name their probes "
vfs_getname...": perf trace opens everything matching a hardcoded "probe:vfs_getname*" wildcard, which used to pin those probes and make their cleanup fail with-EBUSY.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
Ian Rogers
reviewerPATCH 13/13 perf test uprobe_from_different_cu: Scope probe name to PID
In reply to Ian RogersThe test builds a binary in a per-run temporary directory and probes its foo function. The directory name is unique, but perf probe derives the event name from the probed function and the group name from the binary's basename, so every run registers the same probe_testfile:foo event.
Running the test concurrently with itself, as 'perf test -r3' does, therefore fails in all but one of the runs with:
Error: event "foo" already exists.
Hint: Remove existing event by 'perf probe -d'
and a losing run's cleanup goes on to delete the winning run's probe out from under it.
Name the event after the pid, foo_$$, so that parallel runs no longer collide. This lets the test stay in the parallel pass rather than having to be marked (exclusive).
--- .../perf/tests/shell/test_uprobe_from_different_cu.sh | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
tools/perf/tests/shell/test_uprobe_from_different_cu.sh +9 -2
@@ -18,12 +18,19 @@ fi temp_dir=$(mktemp -d /tmp/perf-uprobe-different-cu-sh.XXXXXXXXXX) +# The name of the uprobe added and removed below. The probe is placed on +# ${temp_dir}/testfile, but perf probe derives the event name from the probed +# function and the group name from the binary's basename, so every run would +# otherwise share one probe_testfile:foo event, and a concurrent run would +# fail with 'event "foo" already exists'. Scope the event name to the pid. +probe_name="foo_$$" + cleanup() { trap - EXIT TERM INT if [[ "${temp_dir}" =~ ^/tmp/perf-uprobe-different-cu-sh.*$ ]]; then echo "--- Cleaning up ---" - perf probe -x ${temp_dir}/testfile -d foo || true + perf probe -x ${temp_dir}/testfile -d ${probe_name} || true rm -f "${temp_dir}/"* rmdir "${temp_dir}" fi
@@ -84,6 +91,6 @@ gcc -g -Og -c ${temp_dir}/testfile-main.c -o ${temp_dir}/testfile-main.o gcc -g -Og -o ${temp_dir}/testfile ${temp_dir}/testfile-foo.o ${temp_dir}/testfile-main.o perf probe -x ${temp_dir}/testfile --funcs foo | grep "foo" -perf probe -x ${temp_dir}/testfile foo +perf probe -x ${temp_dir}/testfile ${probe_name}=foo cleanup
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v1 13/13] perf test uprobe_from_different_cu: Scope probe name to PID
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines
The test builds a binary in a per-run temporary directory and probes its foo function. The directory name is unique, but perf probe derives the event name from the probed function and the group name from the binary's basename, so every run registers the same probe_testfile:foo event.
Running the test concurrently with itself, as 'perf test -r3' does, therefore fails in all but one of the runs with:
Error: event "foo" already exists.
Hint: Remove existing event by 'perf probe -d'and a losing run's cleanup goes on to delete the winning run's probe out from under it.
Quoted 6 lines
Name the event after the pid, foo_$$, so that parallel runs no longer collide. This lets the test stay in the parallel pass rather than having to be marked (exclusive).
Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
Ian Rogers
reviewerv2 cover perf trace: Fix BPF filtering and make tracing tests non-exclusive
In reply to Ian Rogersperf trace's BPF augmentation attaches to raw_syscalls:sys_enter and raw_syscalls:sys_exit system wide, and used the program return value to decide whether a syscall was interesting. Returning 0 from a BPF_PROG_TYPE_TRACEPOINT program makes perf_trace_run_bpf_submit() drop the event for every listener on that tracepoint, not just for the perf trace that installed the program. Any concurrent perf trace, perf record or ftrace session watching raw_syscalls therefore lost events, which is one of the reasons so many of the perf trace and perf probe shell tests had to be marked (exclusive) and run on their own.
Patch 1 is an independent build fix for the two files the rest of the series goes on to rework.
Patches 2 to 6 fix perf trace. They stop the return value being used as a filter and do the filtering in BPF maps instead, fix argument handling for the __data_loc internal tracepoint fields that syscalls:sys_enter_* gained in 6.19, stop the sys_exit program array tail calling a sys_enter augmenter, and replace the userspace PERF_RECORD_FORK/PERF_RECORD_EXIT bookkeeping with BTF-typed raw tracepoint programs on sched_process_{fork,exit,exec}. A task is then registered before its first syscall and evicted in do_exit(), rather than whenever userspace next drains the ring buffer.
Patches 7 to 14 deal with the tests. Several collided with each other through global state rather than through perf trace: fixed probe names, clear_all_probes() disabling tracing events globally, and perf trace's hardcoded "probe:vfs_getname*" wildcard pinning probes belonging to other tests. With those scoped to a pid they can drop (exclusive) and run in parallel again.
Tested on x86_64. The trace and probe tests pass under 'perf test -r3', which runs the repeats concurrently. Every patch builds individually, and the series also builds with BUILD_BPF_SKEL=0.
Changes since v1:
- New patch 1 includes <sys/types.h> and <string.h> for the pid_t and strcmp() uses that were relying on the include chain happening to drag them in, which does not hold on libcs such as musl.
- Patch 5 no longer returns success when the event qualifier filter string fails to allocate. err now defaults to 0 because either tracepoint may legitimately be absent, so the ENOMEM path has to set the error itself rather than rely on that default. It also includes <stdbool.h> for the bool parameters it adds to trace_augment.h.
- Patch 7 removes the temporary directory if the cd into it fails. That happens before the cleanup trap is installed, so the directory would otherwise be left behind in /tmp.
Ian Rogers (14):
perf trace: Include the headers declaring pid_t and strcmp perf trace: Start BPF summary before starting workload perf trace: Skip internal tracepoint fields in formatting and beauty
map
perf trace: Do not set unaugmented BPF program on sys_exit map perf trace: Filter events in BPF and avoid tracepoint vetoes perf trace: Handle fork and exit directly in BPF filter maps perf test test_task_analyzer: Isolate in temporary directory and make
non-exclusive
perf test common: Do not globally disable tracing events in
clear_all_probes
perf test probe_vfs_getname: Scope probe name to PID and make
non-exclusive
perf test record+probe_libc_inet_pton: Scope event to PID, add
retries, and make non-exclusive
perf test trace_summary: Improve error diagnostics
perf test trace_btf_general: Drop --max-events=1 and make
non-exclusive
perf test trace_summary: Make non-exclusive
perf test uprobe_from_different_cu: Scope probe name to PID
tools/perf/Documentation/perf-trace.txt | 5 + tools/perf/builtin-trace.c | 335 ++++++++++++++---- tools/perf/tests/shell/common/init.sh | 1 - .../perf/tests/shell/lib/probe_vfs_getname.sh | 34 +- tools/perf/tests/shell/probe_vfs_getname.sh | 3 +- .../shell/record+probe_libc_inet_pton.sh | 83 +++-- .../shell/record+script_probe_vfs_getname.sh | 18 +- tools/perf/tests/shell/test_task_analyzer.sh | 16 +- .../shell/test_uprobe_from_different_cu.sh | 11 +- .../tests/shell/trace+probe_vfs_getname.sh | 9 + tools/perf/tests/shell/trace_btf_general.sh | 8 +- tools/perf/tests/shell/trace_summary.sh | 14 +- .../bpf_skel/augmented_raw_syscalls.bpf.c | 291 ++++++++++++++- tools/perf/util/bpf_trace_augment.c | 174 ++++++++- tools/perf/util/trace_augment.h | 27 +- 15 files changed, 888 insertions(+), 141 deletions(-)
base-commit: 91b0782fc9e9d2f0a40b5256146e014802fdbb36
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
Ian Rogers
reviewerPATCH v2 1/14 perf trace: Include the headers declaring pid_t and strcmp
trace_augment.h uses pid_t in the augmented_syscalls__set_filter_pids() prototype and in the !HAVE_BPF_SKEL stub, and bpf_trace_augment.c calls strcmp() in augmented_syscalls__find_by_title(), but neither pulls in the header that declares what it uses. Both happen to build today only because something else in the include chain drags <sys/types.h> and <string.h> in first, which is not guaranteed and does not hold on libcs such as musl that keep the POSIX namespaces strictly separated.
Include <sys/types.h> and <string.h> explicitly.
--- tools/perf/util/bpf_trace_augment.c | 1 + tools/perf/util/trace_augment.h | 1 + 2 files changed, 2 insertions(+)
tools/perf/util/bpf_trace_augment.c +1 -0
@@ -1,5 +1,6 @@#include <bpf/libbpf.h> #include <internal/xyarray.h> +#include <string.h> #include "bpf_skel/augmented_raw_syscalls.skel.h" #include "debug.h"
tools/perf/util/trace_augment.h +1 -0
@@ -2,6 +2,7 @@#define TRACE_AUGMENT_H #include <linux/compiler.h> +#include <sys/types.h> struct bpf_program ;struct evlist ;
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v2 01/14] perf trace: Include the headers declaring pid_t and strcmp
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines
trace_augment.husespid_tin theaugmented_syscalls__set_filter_pids()prototype and in the !HAVE_BPF_SKEL stub, andbpf_trace_augment.ccallsstrcmp()inaugmented_syscalls__find_by_title(), but neither pulls in the header that declares what it uses. Both happen to build today only because something else in the include chain drags <sys/types.h> and <string.h> in first, which is not guaranteed and does not hold on libcs such as musl that keep the POSIX namespaces strictly separated.
Quoted 4 lines
Include <
sys/types.h> and <string.h> explicitly.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Ian Rogers
reviewerPATCH v2 2/14 perf trace: Start BPF summary before starting workload
In reply to Ian RogersWhen using --bpf-summary, trace_start_bpf_summary() sets skel->bss->enabled = 1. In trace__run(), trace_start_bpf_summary() was previously invoked after evlist__start_workload().
Because evlist__start_workload() immediately unblocks the child process by writing to its go_pipe, short-lived workloads (such as cat /dev/null) can execute and complete their initial system calls before trace_start_bpf_summary() is reached by the parent process. Furthermore, under high system load, the child process may finish before the BPF summary tracking is enabled in the kernel at all, causing syscall summary tests to fail. Additionally, if initial_delay was configured, the workload was started before sleeping.
Move trace_start_bpf_summary() to be invoked before evlist__start_workload(), matching evlist__enable(), and ensure it respects target.initial_delay.
--- tools/perf/builtin-trace.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
tools/perf/builtin-trace.c +5 -3
@@ -4838,17 +4838,19 @@ static int trace__run(struct trace *trace, int argc, const char **argv)if (!target__none (&trace->opts.target) && !trace->opts.target.initial_delay)evlist__enable (evlist); +if (trace->summary_bpf && !trace->opts.target.initial_delay) +trace_start_bpf_summary (); +if (forks)evlist__start_workload (evlist);if (trace->opts.target.initial_delay) {usleep (trace->opts.target.initial_delay *1000 );evlist__enable (evlist); +if (trace->summary_bpf) +trace_start_bpf_summary (); } -if (trace->summary_bpf) -trace_start_bpf_summary (); - trace->multiple_threads =perf_thread_map__pid (evlist__core (evlist)->threads,0 ) ==-1 ||perf_thread_map__nr (evlist__core (evlist)->threads) >1 ||evlist__first (evlist)->core.attr.inherit;
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v2 02/14] perf trace: Start BPF summary before starting workload
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
When using --bpf-summary,
trace_start_bpf_summary()setsskel->bss->enabled= 1. Intrace__run(),trace_start_bpf_summary()was previously invoked afterevlist__start_workload().Because
evlist__start_workload()immediately unblocks the child process by writing to itsgo_pipe, short-lived workloads (such ascat /dev/null) can execute and complete their initial system calls beforetrace_start_bpf_summary()is reached by the parent process. Furthermore, under high system load, the child process may finish before the BPF summary tracking is enabled in the kernel at all, causing syscall summary tests to fail. Additionally, ifinitial_delaywas configured, the workload was started before sleeping.
Quoted 6 lines
Move
trace_start_bpf_summary()to be invoked beforeevlist__start_workload(), matchingevlist__enable(), and ensure it respects target.initial_delay.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Ian Rogers
reviewerPATCH v2 3/14 perf trace: Skip internal tracepoint fields in formatting and beauty map
In reply to Ian RogersLinux 6.19+ added __data_loc char[] internal fields for string arguments in syscalls:sys_enter_<name> tracepoints (e.g., __data_loc_oldname in sys_enter_renameat2). While is_internal_field() was added to detect them, several places did not properly account for them:
1. In syscall_arg_fmt__init_array(), when an internal field was skipped, the arg pointer was still incremented, causing the subsequent argument formatters to be mismatched.
2. In syscall__scnprintf_args(), internal fields were not skipped, causing spurious trailing arguments like ", 0, 16" to be formatted and printed.
3. In trace__bpf_sys_enter_beauty_map(), internal fields were not skipped, offsetting beauty array argument indices and breaking string and buffer augmentation.
4. In trace__find_usable_bpf_prog_entry(), candidate pointer checks matched on internal pointer fields, breaking signature compatibility matching between syscalls for augmenter sharing. Introduce next_user_arg() and advance both cursors with it, so that the two argument lists are always compared at a real argument and the walk ends when one syscall runs out of arguments rather than when one happens to have trailing internal fields.
5. syscall__augmented_args() computed the augmented payload as sample->raw_size - sc->args_size for any sys_enter style sample. sc->args_size deliberately stops at the last non-internal field, so on 6.19+ a native syscalls:sys_enter_<name> record leaves the __data_loc words and their string payloads in the remainder. Those bytes are not a struct augmented_arg, so syscall_arg__scnprintf_augmented_string() read a bogus length and walked arg->augmented.args out of bounds. This is reachable from trace__event_handler(), which calls trace__fprintf_sys_enter() for any evsel whose tracepoint name starts with "sys_enter_".
6. In syscall__read_info(), syscall__alloc_arg_fmts() was called before checking and dropping the leading __syscall_nr (or nr) field, using nr_fields - 1 unconditionally. If a tracepoint format lacks that leading field, the allocated arg_fmt array is one entry too small and syscall_arg_fmt__init_array() writes one entry past the end of the heap buffer. Drop __syscall_nr/nr first and size the allocation from the remaining fields.
Update these functions to check and skip is_internal_field() so that arguments are correctly formatted and beauty map entries match the expected syscall signatures, restrict syscall__augmented_args() to the __augmented_syscalls__ bpf-output evsel, and size arg_fmt after dropping the syscall number field.
--- tools/perf/builtin-trace.c | 153 ++++++++++++++++++++++++++++--------- 1 file changed, 115 insertions(+), 38 deletions(-)
tools/perf/builtin-trace.c +115 -38
@@ -2277,15 +2277,20 @@ syscall_arg_fmt__init_array(struct syscall_arg_fmt *arg, struct tep_format_fieldstruct tep_format_field *last_field =NULL ;int len; -for (; field; field = field->next, ++arg) { -/* assume it's the last argument */ +for (; field; field = field->next) { +/* +* Skip internal tracepoint fields (e.g., __data_loc strings in +* Linux 6.19+) so they do not advance the syscall arg array index. +*/ if (is_internal_field (field))continue ; last_field = field; -if (arg->scnprintf) +if (arg->scnprintf) { + ++arg;continue ; + } len =strlen (field->name);
@@ -2342,6 +2347,7 @@ syscall_arg_fmt__init_array(struct syscall_arg_fmt *arg, struct tep_format_field } } } + ++arg; }return last_field;
@@ -2363,6 +2369,7 @@ static int syscall__read_info(struct syscall *sc, struct trace *trace)char tp_name[128 ];const char *name;struct tep_format_field *field; +int nr_args;int err;if (sc->nonexistent)
@@ -2401,24 +2408,25 @@ static int syscall__read_info(struct syscall *sc, struct trace *trace)return err; } -/* -* The tracepoint format contains __syscall_nr field, so it's one more -* than the actual number of syscall arguments. -*/ -if (syscall__alloc_arg_fmts (sc, sc->tp_format->format.nr_fields -1 )) -return -ENOMEM ; - sc->args = sc->tp_format->format.fields; + nr_args = sc->tp_format->format.nr_fields;/* * We need to check and discard the first variable '__syscall_nr' * or 'nr' that mean the syscall number. It is needless here. * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels. +* +* Do this before allocating, and size the array from what is left, so +* that a format without the field does not leave +* syscall_arg_fmt__init_array() walking one entry past the end. */ if (sc->args && (!strcmp (sc->args->name,"__syscall_nr" ) || !strcmp (sc->args->name,"nr" ))) { sc->args = sc->args->next; - --sc->nr_args; + --nr_args; } +if (syscall__alloc_arg_fmts (sc, nr_args)) +return -ENOMEM ; + field = sc->args;while (field) {if (is_internal_field (field))
@@ -2636,11 +2644,17 @@ static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,if (sc->args !=NULL ) {struct tep_format_field *field; -for (field = sc->args; field; - field = field->next, ++arg.idx, bit <<=1 ) { -if (arg.mask & bit) +for (field = sc->args; field; field = field->next) { +/* Skip internal fields so they are not printed as spurious arguments */ +if (is_internal_field (field))continue ; +if (arg.mask & bit) { + ++arg.idx; + bit <<=1 ; +continue ; + } + arg.fmt = &sc->arg_fmt[arg.idx]; val =syscall_arg__val (&arg, arg.idx);/*
@@ -2658,8 +2672,11 @@ static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,*/ if (val ==0 && !trace->show_zeros && !(sc->arg_fmt && sc->arg_fmt[arg.idx].show_zero) && - !(sc->arg_fmt && sc->arg_fmt[arg.idx].strtoul ==STUL_BTF_TYPE )) + !(sc->arg_fmt && sc->arg_fmt[arg.idx].strtoul ==STUL_BTF_TYPE )) { + ++arg.idx; + bit <<=1 ;continue ; + } printed +=scnprintf (bf + printed, size - printed,"%s" , printed ?", " :"" );
@@ -2674,12 +2691,16 @@ static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size, size - printed, val, field->type);if (btf_printed) { printed += btf_printed; + ++arg.idx; + bit <<=1 ;continue ; } } printed +=syscall_arg_fmt__scnprintf_val (&sc->arg_fmt[arg.idx], bf + printed, size - printed, &arg, val); + ++arg.idx; + bit <<=1 ; } }else if (IS_ERR (sc->tp_format)) {/*
@@ -2940,7 +2961,9 @@ static int trace__fprintf_sample(struct trace *trace, struct perf_sample *samplereturn printed; } -static void *syscall__augmented_args (struct syscall *sc,struct perf_sample *sample,int *augmented_args_size,int raw_augmented_args_size) +static void *syscall__augmented_args (struct trace *trace,struct syscall *sc, +struct perf_sample *sample, +int *augmented_args_size,int raw_augmented_args_size) {/* * For now with BPF raw_augmented we hook into raw_syscalls:sys_enter
@@ -2958,6 +2981,24 @@ static void *syscall__augmented_args(struct syscall *sc, struct perf_sample *sam*/ int args_size = raw_augmented_args_size ?: sc->args_size; +/* +* Augmented arguments are a perf trace specific payload, they are only +* ever appended to samples emitted by the BPF __augmented_syscalls__ +* bpf-output event. +* +* Native syscalls:sys_enter_NAME tracepoints may also carry trailing +* data of their own: since Linux 6.19 they append __data_loc char[] +* fields plus the string payloads they point at. Those bytes are not a +* struct augmented_arg, so treating them as one would make +* syscall_arg__scnprintf_augmented_string() read a bogus length and +* walk arg->augmented.args far out of bounds. +* +* So only look for augmented arguments on the event that can actually +* produce them. +*/ +if (sample->evsel != trace->syscalls.events.bpf_output) +return NULL ; + *augmented_args_size = sample->raw_size - args_size;if (*augmented_args_size >0 ) {static uintptr_t argbuf[1024 ];/* assuming single-threaded */
@@ -3016,17 +3057,13 @@ static int trace__sys_enter(struct trace *trace,if (!(trace->duration_filter || trace->summary_only || trace->min_stack))trace__printf_interrupted_entry (trace);/* -* If this is raw_syscalls.sys_enter, then it always comes with the 6 possible -* arguments, even if the syscall being handled, say "openat", uses only 4 arguments -* this breaks syscall__augmented_args() check for augmented args, as we calculate -* syscall->args_size using each syscalls:sys_enter_NAME tracefs format file, -* so when handling, say the openat syscall, we end up getting 6 args for the -* raw_syscalls:sys_enter event, when we expected just 4, we end up mistakenly -* thinking that the extra 2 u64 args are the augmented filename, so just check -* here and avoid using augmented syscalls when the evsel is the raw_syscalls one. +* syscall__augmented_args() only returns a payload for the BPF +* __augmented_syscalls__ event, so raw_syscalls:sys_enter (which always +* carries all 6 possible arguments rather than sc->args_size worth) and +* the native syscalls:sys_enter_NAME tracepoints are both handled there. */ -if (evsel != trace->syscalls.events.sys_enter) - augmented_args =syscall__augmented_args (sc, sample, &augmented_args_size, trace->raw_augmented_syscalls_args_size); + augmented_args =syscall__augmented_args (trace, sc, sample, &augmented_args_size, + trace->raw_augmented_syscalls_args_size); ttrace->entry_time = sample->time; ttrace->entry_cpu = sample->cpu; msg = ttrace->entry_str;
@@ -3071,7 +3108,7 @@ static int trace__fprintf_sys_enter(struct trace *trace, struct perf_sample *samstruct syscall *sc;char msg[1024 ];void *args, *augmented_args =NULL ; -int augmented_args_size, e_machine; +int augmented_args_size =0 , e_machine;size_t printed =0 ;
@@ -3089,7 +3126,8 @@ static int trace__fprintf_sys_enter(struct trace *trace, struct perf_sample *sam goto out_put; args =perf_evsel__sc_tp_ptr (args, sample); - augmented_args =syscall__augmented_args (sc, sample, &augmented_args_size, trace->raw_augmented_syscalls_args_size); + augmented_args =syscall__augmented_args (trace, sc, sample, &augmented_args_size, + trace->raw_augmented_syscalls_args_size); printed +=syscall__scnprintf_args (sc, msg,sizeof (msg), args, augmented_args, augmented_args_size, trace, thread);fprintf (trace->output,"%.*s" , (int )printed, msg); err =0 ;
@@ -4121,10 +4159,16 @@ static int trace__bpf_sys_enter_beauty_map(struct trace *trace, int e_machine, iif (trace->btf ==NULL )return -1 ; -for (i =0 , field = sc->args; field; ++i, field = field->next) { +for (i =0 , field = sc->args; field; field = field->next) { +/* Skip internal fields to keep beauty array index aligned with syscall arguments */ +if (is_internal_field (field)) +continue ; +// XXX We're only collecting pointer payloads _from_ user space -if (!sc->arg_fmt[i].from_user) +if (!sc->arg_fmt[i].from_user) { + ++i;continue ; + } struct_offset =strstr (field->type,"struct " );if (struct_offset ==NULL )
@@ -4143,8 +4187,10 @@ static int trace__bpf_sys_enter_beauty_map(struct trace *trace, int e_machine, i name[cnt] ='\0' ;/* cache struct's btf_type and type_id */ -if (syscall_arg_fmt__cache_btf_struct (&sc->arg_fmt[i], trace->btf, name)) +if (syscall_arg_fmt__cache_btf_struct (&sc->arg_fmt[i], trace->btf, name)) { + ++i;continue ; + } bt = sc->arg_fmt[i].type; beauty_array[i] = bt->size;
@@ -4170,7 +4216,9 @@ static int trace__bpf_sys_enter_beauty_map(struct trace *trace, int e_machine, istruct tep_format_field *field_tmp;/* find the size of the buffer that appears in pairs with buf */ -for (j =0 , field_tmp = sc->args; field_tmp; ++j, field_tmp = field_tmp->next) { +for (j =0 , field_tmp = sc->args; field_tmp; field_tmp = field_tmp->next) { +if (is_internal_field (field_tmp)) +continue ; if (!(field_tmp->flags & TEP_FIELD_IS_POINTER) && /* only integers */ (strstr(field_tmp->name, "count") || strstr(field_tmp->name, "siz") || /* size, bufsiz */
@@ -4180,8 +4228,10 @@ static int trace__bpf_sys_enter_beauty_map(struct trace *trace, int e_machine, i can_augment = true;break ; } + ++j; } } + ++i; }if (can_augment)
@@ -4190,6 +4240,19 @@ static int trace__bpf_sys_enter_beauty_map(struct trace *trace, int e_machine, ireturn -1 ; } +/* +* Advance to the first field that is a real syscall argument, so that callers +* walking two argument lists in step never have to reason about internal +* fields appearing in one list but not the other. +*/ +static struct tep_format_field *next_user_arg (struct tep_format_field *field) +{ +while (field &&is_internal_field (field)) + field = field->next; + +return field; +} +static struct bpf_program *trace__find_usable_bpf_prog_entry (struct trace *trace,struct syscall *sc) {
@@ -4197,7 +4260,7 @@ static struct bpf_program *trace__find_usable_bpf_prog_entry(struct trace *trace/* * We're only interested in syscalls that have a pointer: */ -for (field = sc->args; field; field = field->next) { +for (field =next_user_arg (sc->args); field; field =next_user_arg (field->next)) {if (field->flags &TEP_FIELD_IS_POINTER ) goto try_to_find_pair; }
@@ -4215,21 +4278,31 @@ static struct bpf_program *trace__find_usable_bpf_prog_entry(struct trace *trace pair->bpf_prog.sys_enter == unaugmented_prog)continue ; -for (field = sc->args, candidate_field = pair->args; - field && candidate_field; field = field->next, candidate_field = candidate_field->next) { +/* +* Both cursors only ever point at real arguments, so the loop +* ends when one of the two syscalls runs out of them, rather +* than when one happens to have trailing internal fields. +*/ + field =next_user_arg (sc->args); + candidate_field =next_user_arg (pair->args); +while (field && candidate_field) {bool is_pointer = field->flags &TEP_FIELD_IS_POINTER , candidate_is_pointer = candidate_field->flags &TEP_FIELD_IS_POINTER ;if (is_pointer) { -if (!candidate_is_pointer) { +if (!candidate_is_pointer) {// The candidate just doesn't copies our pointer arg, might copy other pointers we want. + field =next_user_arg (field->next); + candidate_field =next_user_arg (candidate_field->next);continue ; - } + } }else {if (candidate_is_pointer) {// The candidate might copy a pointer we don't have, skip it. goto next_candidate; } + field =next_user_arg (field->next); + candidate_field =next_user_arg (candidate_field->next);continue ; }
@@ -4250,6 +4323,8 @@ static struct bpf_program *trace__find_usable_bpf_prog_entry(struct trace *trace goto next_candidate; is_candidate = true; + field =next_user_arg (field->next); + candidate_field =next_user_arg (candidate_field->next); }if (!is_candidate)
@@ -4261,7 +4336,9 @@ static struct bpf_program *trace__find_usable_bpf_prog_entry(struct trace *trace* more than what is common to the two syscalls. */ if (candidate_field) { -for (candidate_field = candidate_field->next; candidate_field; candidate_field = candidate_field->next) + candidate_field =next_user_arg (candidate_field->next); +for (; candidate_field; + candidate_field =next_user_arg (candidate_field->next))if (candidate_field->flags &TEP_FIELD_IS_POINTER ) goto next_candidate; }
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v2 03/14] perf trace: Skip internal tracepoint fields in formatting and beauty map
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 19 lines
Linux 6.19+ added
__data_locchar[] internal fields for string arguments in syscalls:sys_enter_<name> tracepoints (e.g.,__data_loc_oldnameinsys_enter_renameat2). Whileis_internal_field()was added to detect them, several places did not properly account for them:1. In
syscall_arg_fmt__init_array(), when an internal field was skipped, the arg pointer was still incremented, causing the subsequent argument formatters to be mismatched.
2. Insyscall__scnprintf_args(), internal fields were not skipped, causing spurious trailing arguments like ", 0, 16" to be formatted and printed.
3. Intrace__bpf_sys_enter_beauty_map(), internal fields were not skipped, offsetting beauty array argument indices and breaking string and buffer augmentation.
[ ... ]__augmented_syscalls__bpf-output evsel, and sizearg_fmtafter dropping the syscall number field.
Quoted 2 lines
Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Ian Rogers
reviewerPATCH v2 4/14 perf trace: Do not set unaugmented BPF program on sys_exit map
In reply to Ian RogersIn trace__init_syscalls_bpf_prog_array_maps(), the BPF program array map for sys_exit (syscalls_sys_exit) was populated with the result of trace__bpf_prog_sys_exit_fd().
When a syscall had no specific exit augmenter, trace__find_syscall_bpf_prog() fell back to unaugmented_prog (syscall_unaugmented). However, syscall_unaugmented is a sys_enter program that outputs enter arguments to __augmented_syscalls__.
As a consequence, when an unaugmented syscall exited, sys_exit tail-called syscall_unaugmented, which interpreted the exit arguments as enter arguments and emitted a duplicate, corrupt sys_enter event into __augmented_syscalls__ right as the syscall completed.
Fix this by:
1. Returning NULL from trace__find_syscall_bpf_prog() when looking up exit augmenters and none is found.
2. Returning -1 from trace__bpf_prog_sys_exit_fd() when no exit program is present.
3. Only updating map_exit_fd when prog_fd >= 0.
4. Clearing err = 0 when trace__bpf_sys_enter_beauty_map() returns non-zero (indicating the syscall has no augmentable pointer arguments) before continuing the loop, so a trailing run of such syscalls (e.g. 'perf trace -e close') does not leave err non-zero on return and abort the session.
--- tools/perf/builtin-trace.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-)
tools/perf/builtin-trace.c +22 -6
@@ -4117,7 +4117,12 @@ static struct bpf_program *trace__find_syscall_bpf_prog(struct trace *trace __mapr_debug ("Couldn't find BPF prog \"%s\" to associate with syscalls:sys_%s_%s, not augmenting it\n" , prog_name, type, sc->name); out_unaugmented: -return unaugmented_prog; +/* +* Do not set unaugmented_prog for exit: syscall_unaugmented is a +* sys_enter program that outputs enter arguments. Exit without a +* specialized return augmenter returns 1 directly from sys_exit. +*/ +return !strcmp (type,"exit" ) ?NULL : unaugmented_prog; }static void trace__init_syscall_bpf_progs (struct trace *trace,int e_machine,int id)
@@ -4140,7 +4145,7 @@ static int trace__bpf_prog_sys_enter_fd(struct trace *trace, int e_machine, intstatic int trace__bpf_prog_sys_exit_fd (struct trace *trace,int e_machine,int id) {struct syscall *sc =trace__syscall_info (trace,NULL , e_machine, id); -return sc ?bpf_program__fd (sc->bpf_prog.sys_exit) :bpf_program__fd (unaugmented_prog); +return sc && sc->bpf_prog.sys_exit ?bpf_program__fd (sc->bpf_prog.sys_exit) :-1 ; }static int trace__bpf_sys_enter_beauty_map (struct trace *trace,int e_machine,int key,unsigned int *beauty_array)
@@ -4395,16 +4400,27 @@ static int trace__init_syscalls_bpf_prog_array_maps(struct trace *trace, int e_m err =bpf_map_update_elem (map_enter_fd, &key, &prog_fd,BPF_ANY );if (err)break ; +/* Only update the exit prog array map if an exit augmenter exists */ prog_fd =trace__bpf_prog_sys_exit_fd (trace, e_machine, key); - err =bpf_map_update_elem (map_exit_fd, &key, &prog_fd,BPF_ANY ); -if (err) -break ; +if (prog_fd >=0 ) { + err =bpf_map_update_elem (map_exit_fd, &key, &prog_fd,BPF_ANY ); +if (err) +break ; + }/* use beauty_map to tell BPF how many bytes to collect, set beauty_map's value here */ memset (beauty_array,0 ,sizeof (beauty_array)); err =trace__bpf_sys_enter_beauty_map (trace, e_machine, key, (unsigned int *)beauty_array); -if (err) +if (err) { +/* +* Not a failure: the syscall just has no augmentable +* arguments. Clear err, or a trailing run of such +* syscalls, e.g. all of them for 'perf trace -e close', +* would leave it set on return and abort the session. +*/ + err =0 ;continue ; + } err =bpf_map_update_elem (beauty_map_fd, &key, beauty_array,BPF_ANY );if (err)break ;
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v2 04/14] perf trace: Do not set unaugmented BPF program on sys_exit map
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 26 lines
In
trace__init_syscalls_bpf_prog_array_maps(), the BPF program array map forsys_exit(syscalls_sys_exit) was populated with the result oftrace__bpf_prog_sys_exit_fd().When a syscall had no specific exit augmenter,
trace__find_syscall_bpf_prog()fell back tounaugmented_prog(syscall_unaugmented). However,syscall_unaugmentedis asys_enterprogram that outputs enter arguments to__augmented_syscalls__.As a consequence, when an unaugmented syscall exited,
sys_exittail-calledsyscall_unaugmented, which interpreted the exit arguments as enter arguments and emitted a duplicate, corruptsys_enterevent into__augmented_syscalls__right as the syscall completed.Fix this by:
1. Returning NULL fromtrace__find_syscall_bpf_prog()when looking up exit augmenters and none is found.
2. Returning -1 fromtrace__bpf_prog_sys_exit_fd()when no exit program is present.
3. Only updatingmap_exit_fdwhenprog_fd>= 0.
4. Clearing err = 0 whentrace__bpf_sys_enter_beauty_map()returns non-zero (indicating the syscall has no augmentable pointer arguments) before continuing the loop, so a trailing run of such syscalls (e.g. 'perf trace -e close') does not leave err non-zero on return and abort the session.
Quoted 2 lines
Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Ian Rogers
reviewerPATCH v2 5/14 perf trace: Filter events in BPF and avoid tracepoint vetoes
In reply to Ian RogersThe BPF augmented_raw_syscalls sys_enter and sys_exit programs returned 0 for syscalls that were not of interest. Returning 0 from a tracepoint BPF program vetoes the event for the whole system, so an unrelated concurrent perf trace, perf record or ftrace session listening to raw_syscalls would silently lose events. This is a cross-session side effect and shows up as flaky failures when perf tests run in parallel.
Furthermore, syscall_unaugmented previously returned 1 without writing anything to the __augmented_syscalls__ ring buffer. This forced userspace perf trace to listen to both raw_syscalls:sys_enter and __augmented_syscalls__ in its evlist, requiring userspace event deduplication.
Address these issues:
1. In augmented_raw_syscalls.bpf.c, never return 0 from tracepoint
handlers: return 1 so non-traced syscalls pass through without vetoing other concurrent listeners.
2. Introduce pids_to_trace and syscalls_to_trace BPF hash maps to perform targeted filtering directly in BPF. Unselected syscalls or PIDs return 1 immediately without writing to the buffer.
3. In syscall_unaugmented, output the unaugmented enter payload into __augmented_syscalls__ and return 1. Change its section from SEC("tp/raw_syscalls/sys_enter") to SEC("tp/syscalls/sys_enter_unaugmented") so libbpf does not attempt to auto-attach it to raw_syscalls:sys_enter.
4. In bpf_trace_augment.c, add helpers to configure target PIDs and syscalls in the BPF maps, setting the activation flags (has_pids_to_trace, has_syscalls_to_trace) only after the maps are fully populated so already-attached BPF programs do not filter against a half-filled map. Explicitly attach only sys_enter and sys_exit via an attach_prog() helper that saves -errno before calling pr_debug() or bpf_link__destroy().
Destroy the skeleton on every failure path. Leaving a loaded but unusable skeleton behind is not inert: the setters called later from trace__run() would program its maps, and a partial attach would leave a BPF program live on raw_syscalls for a session that never starts. Since augmented_syscalls__{prepare,create_bpf_output}() failures fall back to unaugmented tracing rather than aborting, those setters have to become no-ops, which they only do once skel is NULL again.
errno is used directly here, so include <errno.h> rather than relying on it arriving via another header, which it does not under musl.
5. In builtin-trace.c, hook trace__set_ev_qualifier_filter() and PID filtering into the BPF maps. When __augmented_syscalls__ is active, remove raw_syscalls:sys_enter from trace.evlist since all traced enter events (both augmented and unaugmented) are now emitted by BPF into __augmented_syscalls__. Restore tracking on the remaining evsel via evlist__set_tracking_event() so PERF_RECORD_COMM and tracking events continue to be recorded. Errors from augmented_syscalls__set_target_syscalls() are reported and propagated, the tracepoint filter string is freed on every exit path, and an allocation failure in trace__set_filter_pids() now returns -ENOMEM instead of being silently ignored.
Note that in trace__set_filter_pids() the target pids and the filtered pids are two independent axes and both have to be programmed. Naming pids to leave out with --filter-pids does not widen -p/-t or a workload to the whole system, and a BPF tracepoint program is attached system wide rather than to the target's file descriptors, so pids_to_trace is the only thing keeping other tasks out.
6. Add --syscall-augment option (defaulting to true) to allow users to explicitly use --no-syscall-augment to run perf trace in the classic unaugmented tracepoint mode without BPF. When BPF is unavailable or disabled, ensure the non-augmented tracepoint path cleanly configures sys_enter and sys_exit without duplicate entries.
--- tools/perf/Documentation/perf-trace.txt | 5 + tools/perf/builtin-trace.c | 151 +++++++++++++++--- .../bpf_skel/augmented_raw_syscalls.bpf.c | 133 +++++++++++++-- tools/perf/util/bpf_trace_augment.c | 135 +++++++++++++++- tools/perf/util/trace_augment.h | 34 ++++ 5 files changed, 420 insertions(+), 38 deletions(-)
tools/perf/Documentation/perf-trace.txt +5 -0
@@ -260,6 +260,11 @@ the thread executes on the designated CPUs. Default is to monitor all CPUs. Maximum number of lines in the summary mode. Note that this applies to each entry (thread or cgroup). +--syscall-augment:: + Augment syscalls with BPF. Enabled by default when BPF support is available. + Use --no-syscall-augment to disable BPF augmentation and fall back to the + unaugmented tracepoint approach. + PAGEFAULTS ----------
tools/perf/builtin-trace.c +129 -22
255 lines not on this page · raw message
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +118 -15
201 lines not on this page · raw message
tools/perf/util/bpf_trace_augment.c +134 -1
165 lines not on this page · raw message
tools/perf/util/trace_augment.h +34 -0
@@ -2,6 +2,7 @@#define TRACE_AUGMENT_H #include <linux/compiler.h> +#include <stdbool.h> #include <sys/types.h> struct bpf_program ;
@@ -13,6 +14,11 @@ int augmented_syscalls__prepare(void);int augmented_syscalls__create_bpf_output (struct evlist *evlist);void augmented_syscalls__setup_bpf_output (void );int augmented_syscalls__set_filter_pids (unsigned int nr,pid_t *pids); +int augmented_syscalls__set_target_pids (unsigned int nr,pid_t *pids); +int augmented_syscalls__add_target_pid (pid_t pid); +int augmented_syscalls__del_target_pid (pid_t pid); +bool augmented_syscalls__has_target_pid (pid_t pid); +int augmented_syscalls__set_target_syscalls (unsigned int nr,int *syscall_ids,bool not_syscalls);int augmented_syscalls__get_map_fds (int *enter_fd,int *exit_fd,int *beauty_fd);struct bpf_program *augmented_syscalls__find_by_title (const char *name);struct bpf_program *augmented_syscalls__unaugmented (void );
@@ -40,6 +46,34 @@ static inline int augmented_syscalls__set_filter_pids(unsigned int nr __maybe_unreturn 0 ; } +static inline int augmented_syscalls__set_target_pids (unsigned int nr__maybe_unused , +pid_t *pids__maybe_unused ) +{ +return 0 ; +} + +static inline int augmented_syscalls__add_target_pid (pid_t pid__maybe_unused ) +{ +return 0 ; +} + +static inline int augmented_syscalls__del_target_pid (pid_t pid__maybe_unused ) +{ +return 0 ; +} + +static inline bool augmented_syscalls__has_target_pid (pid_t pid__maybe_unused ) +{ +return false; +} + +static inline int augmented_syscalls__set_target_syscalls (unsigned int nr__maybe_unused , +int *syscall_ids__maybe_unused , +bool not_syscalls__maybe_unused ) +{ +return 0 ; +} +static inline int augmented_syscalls__get_map_fds (int *enter_fd__maybe_unused ,int *exit_fd__maybe_unused ,int *beauty_fd__maybe_unused )
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v2 05/14] perf trace: Filter events in BPF and avoid tracepoint vetoes
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
The BPF
augmented_raw_syscallssys_enterandsys_exitprograms returned 0 for syscalls that were not of interest. Returning 0 from a tracepoint BPF program vetoes the event for the whole system, so an unrelated concurrent perf trace, perf record or ftrace session listening toraw_syscallswould silently lose events. This is a cross-session side effect and shows up as flaky failures when perf tests run in parallel.Furthermore,
syscall_unaugmentedpreviously returned 1 without writing anything to the__augmented_syscalls__ring buffer. This forced userspace perf trace to listen to both raw_syscalls:sys_enter and__augmented_syscalls__in its evlist, requiring userspace event deduplication.
Quoted 8 lines
Address these issues:
1. Inaugmented_raw_syscalls.bpf.c, never return 0 from tracepoint [ ... ]
disabled, ensure the non-augmented tracepoint path cleanly configuressys_enterandsys_exitwithout duplicate entries.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Ian Rogers
reviewerPATCH v2 6/14 perf trace: Handle fork and exit directly in BPF filter maps
In reply to Ian RogersUpdating target or filtered PIDs in userspace upon processing PERF_RECORD_FORK and PERF_RECORD_EXIT events introduces latency between event occurrence and userspace BPF map updates. If a newly forked child executes system calls before userspace processes PERF_RECORD_FORK, those syscalls may be dropped by BPF PID filtering. Conversely, if userspace evicts PIDs asynchronously on PERF_RECORD_EXIT, the kernel may recycle a PID before userspace processes the exit event, causing the late eviction to silently drop a newly created task that received the recycled PID.
Address this by attaching BTF-typed raw tracepoint BPF programs directly to the scheduler task lifetime tracepoints:
1. Attach SEC("tp_btf/sched_process_fork") (sched_process_fork), which runs in copy_process() in the parent's context before wake_up_new_task() wakes the child. Using tp_btf rather than SEC("tp/sched/sched_process_fork") receives the stable TP_PROTO arguments (struct task_struct *parent, struct task_struct *child) rather than the tracepoint ring-buffer record (TP_STRUCT__entry), whose layout changed in Linux 6.16 when parent_comm and child_comm were converted from 16-byte arrays to 4-byte __data_loc strings. When inherit is enabled and the parent's PID or TGID is in pids_to_trace or pids_filtered, insert child->pid into the corresponding map immediately. Because child->pid is task_struct.pid (the global initial-namespace PID), this works accurately across PID namespaces without aliasing host PIDs, and covers both new processes and CLONE_THREAD threads without needing real_parent CO-RE walks or syscall-return heuristics.
2. Attach SEC("tp_btf/sched_process_exit") (sched_process_exit), which runs in do_exit() for every task in its own context, including tasks killed by signals (SIGKILL, SIGSEGV, etc.) and secondary threads torn down implicitly by exit_group. Delete the dying task's PID from pids_to_trace and pids_filtered immediately in kernel space, eliminating both map leaks and any asynchronous userspace eviction window where PID recycling could occur.
3. Attach SEC("tp_btf/sched_process_exec") (sched_process_exec) to follow the one case where a live task's pid changes underneath the maps. When a thread that is not the group leader execs, de_thread() kills the leader and hands the leader's pid, which is the tgid, to the exec'ing thread. The leader dies first, so sched_process_exit() has already dropped exactly the pid the survivor now holds, and the survivor's old entry would be stranded in the map for good. Move the entry from old_pid to p->pid. old_pid is sampled in bprm_execve() before de_thread() runs, so the ordinary group leader exec is a no-op here.
4. With every live task registered before its first syscall and evicted in do_exit(), simplify pid_to_trace__has() and pid_filter__has() to single BPF hash map lookups, and move bpf_probe_read_kernel() in sys_exit back after the PID filter checks.
5. Pass the inherit flag from userspace to BPF .rodata via augmented_syscalls__prepare(!trace.opts.no_inherit), and split attaching out of it into augmented_syscalls__attach(), called from trace__run() once the pid, syscall and program array maps have all been programmed. These are system wide programs, so from the instant they attach they alone decide what is traced: attaching at load time, as before, left a window in which a target could fork without sched_process_fork() knowing the parent was a target, and with the userspace fork handling gone there was nothing left to recover it. The scheduler programs are attached ahead of sys_enter and sys_exit for the same reason. Set has_pids_filtered only after populating pids_filtered.
6. Remove the userspace BPF map updates from PERF_RECORD_FORK and PERF_RECORD_EXIT in trace__process_event(), and delete the now-unused augmented_syscalls__{add,del,has}_target_pid() helpers. No coverage is lost with them: those records only come into being once the ring buffers are mapped by evlist__do_mmap() and the events are switched on by evlist__enable(), both of which run after augmented_syscalls__attach() in trace__run(), and they are then acted on later still, whenever the poll loop gets round to them. The scheduler programs therefore go live strictly earlier than the userspace path could ever have reacted.
7. Gate pid_filter__has() on a has_pids_filtered flag in .bss so the common case without --filter-pids performs no map lookups, and size pids_to_trace and pids_filtered at 16384 entries. pids_filtered is grown from 64 because it is no longer just the handful of pids userspace names: sched_process_fork() adds every descendant of those, so a --filter-pids target that forks or is heavily threaded needs the same headroom as a traced one.
A fork or exit is still not seen if it happens before the programs are attached, that is between evlist__create_maps() scanning /proc for a -p target and augmented_syscalls__attach(). Such a window is inherent in programming a system wide filter before switching it on, and as above the userspace handling did not cover it either. What it costs is small:
a child forked in the window is still traced through the tracepoints its parent's events were inherited by, only unaugmented, because sys_enter returns 1 for a pid that is not in the map rather than vetoing the tracepoint. A workload started by 'perf trace -- cmd' cannot hit it at all, as evlist__prepare_workload() leaves the child blocked on a pipe until evlist__start_workload(), well after the attach.
--- tools/perf/builtin-trace.c | 39 ++-- .../bpf_skel/augmented_raw_syscalls.bpf.c | 178 +++++++++++++++++- tools/perf/util/bpf_trace_augment.c | 116 +++++++----- tools/perf/util/trace_augment.h | 28 +-- 4 files changed, 262 insertions(+), 99 deletions(-)
tools/perf/builtin-trace.c +16 -23
@@ -2055,23 +2055,6 @@ static int trace__process_event(struct trace *trace, struct machine *machine, "LOST %" PRIu64 " events!\n", (u64)event->lost.lost); ret =machine__process_lost_event (machine, event, sample);break ; -case PERF_RECORD_FORK : -if (trace->raw_augmented_syscalls && - (augmented_syscalls__has_target_pid (event->fork.ppid) || -augmented_syscalls__has_target_pid (event->fork.ptid))) { -augmented_syscalls__add_target_pid (event->fork.pid); - } - ret =machine__process_fork_event (machine, event, sample); -break ; -case PERF_RECORD_EXIT : -if (trace->raw_augmented_syscalls) { -if (event->fork.pid == event->fork.tid) -augmented_syscalls__del_target_pid (event->fork.pid); -else -augmented_syscalls__del_target_pid (event->fork.tid); - } - ret =machine__process_exit_event (machine, event, sample); -break ;default : ret =machine__process_event (machine, event, sample);break ;
@@ -4987,6 +4970,16 @@ static int trace__run(struct trace *trace, int argc, const char **argv) } } +/* +* Everything the BPF programs filter on is now in their maps, so it is +* safe to let them run. They are attached system wide, so anything +* before this point would have been filtered against a map that was +* still being built up. +*/ + err =augmented_syscalls__attach (); +if (err <0 ) + goto out_errno; +/* * If the "close" syscall is not traced, then we will not have the * opportunity to, in syscall_arg__scnprintf_close_fd() invalidate the
@@ -6079,7 +6072,7 @@ int cmd_trace(int argc, const char **argv) goto skip_augmentation; } - err =augmented_syscalls__prepare (); + err =augmented_syscalls__prepare (!trace.opts.no_inherit);if (err <0 ) goto skip_augmentation;
@@ -6090,11 +6083,11 @@ int cmd_trace(int argc, const char **argv) trace.syscalls.events.bpf_output =evlist__last (trace.evlist); }else {/* -* augmented_syscalls__prepare() already attached sys_enter and -* sys_exit, which are system wide. Falling through to -* skip_augmentation without undoing that would run a BPF -* program for every syscall on the machine, for the whole -* session, with nothing consuming the output. +* Drop the loaded skeleton before falling back to unaugmented +* tracing. Otherwise the setters called from trace__run() would +* still program its maps, and augmented_syscalls__attach() would +* then put system wide BPF programs on raw_syscalls for a +* session with nothing consuming their output. */ pr_debug ("Failed to create the augmented syscalls bpf-output event, disabling augmentation\n" );augmented_syscalls__cleanup ();
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +168 -10
239 lines not on this page · raw message
tools/perf/util/bpf_trace_augment.c +70 -46
170 lines not on this page · raw message
tools/perf/util/trace_augment.h +8 -20
@@ -10,14 +10,12 @@ struct evlist;#ifdef HAVE_BPF_SKEL -int augmented_syscalls__prepare (void ); +int augmented_syscalls__prepare (bool inherit); +int augmented_syscalls__attach (void );int augmented_syscalls__create_bpf_output (struct evlist *evlist);void augmented_syscalls__setup_bpf_output (void );int augmented_syscalls__set_filter_pids (unsigned int nr,pid_t *pids);int augmented_syscalls__set_target_pids (unsigned int nr,pid_t *pids); -int augmented_syscalls__add_target_pid (pid_t pid); -int augmented_syscalls__del_target_pid (pid_t pid); -bool augmented_syscalls__has_target_pid (pid_t pid);int augmented_syscalls__set_target_syscalls (unsigned int nr,int *syscall_ids,bool not_syscalls);int augmented_syscalls__get_map_fds (int *enter_fd,int *exit_fd,int *beauty_fd);struct bpf_program *augmented_syscalls__find_by_title (const char *name);
@@ -26,11 +24,16 @@ void augmented_syscalls__cleanup(void);#else /* !HAVE_BPF_SKEL */ -static inline int augmented_syscalls__prepare (void ) +static inline int augmented_syscalls__prepare (bool inherit__maybe_unused ) {return -1 ; } +static inline int augmented_syscalls__attach (void ) +{ +return 0 ; +} +static inline int augmented_syscalls__create_bpf_output (struct evlist *evlist__maybe_unused ) {return -1 ;
@@ -52,21 +55,6 @@ static inline int augmented_syscalls__set_target_pids(unsigned int nr __maybe_unreturn 0 ; } -static inline int augmented_syscalls__add_target_pid (pid_t pid__maybe_unused ) -{ -return 0 ; -} - -static inline int augmented_syscalls__del_target_pid (pid_t pid__maybe_unused ) -{ -return 0 ; -} - -static inline bool augmented_syscalls__has_target_pid (pid_t pid__maybe_unused ) -{ -return false; -} -static inline int augmented_syscalls__set_target_syscalls (unsigned int nr__maybe_unused ,int *syscall_ids__maybe_unused ,bool not_syscalls__maybe_unused )
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v2 06/14] perf trace: Handle fork and exit directly in BPF filter maps
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] perf trace: Dropped sys_enter events for children in attach window --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] perf trace: Dropped sys_enter events for children in attach window --
commit f502bbaf61708b9eab3d9d484a0f81359acb4676
Author: Ian Rogers <[email protected]>
perf trace: Handle fork and exit directly in BPF filter maps
This commit moves PID tracking from userspace (processing PERF_RECORD_FORK and PERF_RECORD_EXIT events) directly into BPF programs attached to scheduler tracepoints. This addresses latency issues between event occurrence and userspace map updates by immediately updating BPF maps from kernel space.
Quoted 6 lines
tools/perf/builtin-trace.c +0
-0@@ -2055,23 +2055,6 @@ static int trace__process_event(struct trace *trace, struct machine *machine, "LOST %" PRIu64 " events!\n", (u64)event->lost.lost);
tools/perf/builtin-trace.c static int trace__process_event(struct trace *trace, struct machine *machine, · line 2065
Quoted 10 lines
tools/perf/builtin-trace.c +0
-8ret =machine__process_lost_event (machine, event, sample);break ; -case PERF_RECORD_FORK : -if (trace->raw_augmented_syscalls && - (augmented_syscalls__has_target_pid (event->fork.ppid) || -augmented_syscalls__has_target_pid (event->fork.ptid))) { -augmented_syscalls__add_target_pid (event->fork.pid); - } - ret =machine__process_fork_event (machine, event, sample); -break ;
[Severity: Medium]
Does removing the PERF_RECORD_FORK handling create a silent blind spot for tasks forked during the attach window?
The commit message states that a child forked in this window is still traced unaugmented because the BPF sys_enter program returns 1:
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c:sys_enter() {
...
if (has_pids_to_trace && !pid_to_trace__has(getpid()))
return 1;
...
}However, cmd_trace() explicitly removes the raw_syscalls:sys_enter evsel from the evlist during initialization:
tools/perf/builtin-trace.c:cmd_trace() {
...
augmented->handler = trace__sys_enter;
evlist__remove(trace.evlist, evsel);
evsel__put_and_free_priv(evsel);
...
}Because this evsel is removed, there appears to be no userspace listener attached for it. Consequently, when the BPF sys_enter program returns 1 to allow the kernel to emit the tracepoint, the event is silently dropped instead of falling back to unaugmented tracing.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Ian Rogers
reviewerPATCH v2 7/14 perf test test_task_analyzer: Isolate in temporary directory and make non-exclusive
In reply to Ian Rogerstest_task_analyzer.sh writes perf.data and temporary files directly into the current working directory, causing collisions when running tests in parallel.
As a temporary measure until perf script report supports an input file option, resolve perfdir to an absolute path, change directory into $tmpdir for the test duration, and clean up in the exit trap. Remove the (exclusive) tag so the test runs in parallel.
perfdir is derived from $0, which may be relative, so it has to be resolved before the cd into $tmpdir, otherwise both PERF_EXEC_PATH and the cleanup trap point at paths that no longer exist.
--- tools/perf/tests/shell/test_task_analyzer.sh | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
tools/perf/tests/shell/test_task_analyzer.sh +12 -4
@@ -1,8 +1,18 @@ #!/bin/bash -# perf script task-analyzer tests (exclusive) +# perf script task-analyzer tests # SPDX-License-Identifier: GPL-2.0 +# Resolve the source directory before changing the working directory below, +# $0 may be a relative path and would no longer resolve from $tmpdir. +perfdir=$(cd "$(dirname "$0")/../.." && pwd) + tmpdir=$(mktemp -d /tmp/perf-script-task-analyzer-XXXXX) +# The cleanup trap is only installed further down, once the functions it +# calls have been defined, so tidy up by hand if this cd fails. +cd "$tmpdir" || { + rmdir "$tmpdir" + exit 1 +} # TODO: perf script report only supports input from the CWD perf.data file, make # it support input from any file. perfdata="perf.data"
@@ -11,7 +21,6 @@ csvsummary="$tmpdir/csvsummary" err=0 # set PERF_EXEC_PATH to find scripts in the source directory -perfdir=$(dirname "$0")/../.. if [ -e "$perfdir/scripts/python/Perf-Trace-Util" ]; then export PERF_EXEC_PATH=$perfdir fi
@@ -20,8 +29,7 @@ fi export ASAN_OPTIONS=detect_leaks=0 cleanup() { - rm -f "${perfdata}" - rm -f "${perfdata}".old + cd "$perfdir" || cd /tmp || exit rm -rf "$tmpdir" trap - exit term int
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v2 07/14] perf test test_task_analyzer: Isolate in temporary directory and make non-exclusive
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
test_task_analyzer.shwrites perf.data and temporary files directly into the current working directory, causing collisions when running tests in parallel.As a temporary measure until
perf script reportsupports an input file option, resolve perfdir to an absolute path, change directory into $tmpdir for the test duration, and clean up in the exit trap. Remove the (exclusive) tag so the test runs in parallel.
Quoted 6 lines
perfdir is derived from $0, which may be relative, so it has to be resolved before the cd into $tmpdir, otherwise both
PERF_EXEC_PATHand the cleanup trap point at paths that no longer exist.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Ian Rogers
reviewerPATCH v2 8/14 perf test common: Do not globally disable tracing events in clear_all_probes
In reply to Ian Rogersecho 0 > /sys/kernel/debug/tracing/events/enable disables tracepoint events system-wide. When running tests in parallel, this kills active tracing and recording sessions in concurrent tests (such as perf trace and perf record).
Remove the global event disable from clear_all_probes().
--- tools/perf/tests/shell/common/init.sh | 1 - 1 file changed, 1 deletion(-)
tools/perf/tests/shell/common/init.sh +0 -1
@@ -132,7 +132,6 @@ check_uprobes_available() clear_all_probes() { - echo 0 > /sys/kernel/debug/tracing/events/enable check_kprobes_available && echo > /sys/kernel/debug/tracing/kprobe_events check_uprobes_available && echo > /sys/kernel/debug/tracing/uprobe_events }
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v2 08/14] perf test common: Do not globally disable tracing events in clear_all_probes
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
echo 0 > /sys/kernel/debug/tracing/events/enabledisables tracepoint events system-wide. When running tests in parallel, this kills active tracing and recording sessions in concurrent tests (such as perf trace and perf record).Remove the global event disable from
clear_all_probes().Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Ian Rogers
reviewerPATCH v2 9/14 perf test probe_vfs_getname: Scope probe name to PID and make non-exclusive
In reply to Ian RogersThe probe name vfs_getname was hardcoded, causing collisions when tests ran concurrently. Furthermore, cleanup_probe_vfs_getname() used perf probe -d probe:vfs_getname*, deleting probes registered by other parallel tests.
Scope the probe name to the pid, and rename it to getname_flags_$$ so that it no longer begins with "vfs_getname". perf trace calls evlist__add_vfs_getname(), which opens every event matching a hardcoded "probe:vfs_getname*" wildcard, so a perf trace run by any other test would otherwise pin this probe and make perf probe -d fail with -EBUSY. That also unblocks making the perf trace tests non-exclusive later in this series.
Enumerate the probes to record and to delete from perf probe -l, matching ^probe:${vfs_getname}(_[[:digit:]]+)?$ exactly, rather than globbing on ${vfs_getname}*. perf probe appends _1, _2, ... when getname_flags is inlined at more than one call site, so the variants do have to be matched, but since the name now ends in a pid a trailing wildcard would also match the probes of a test whose pid merely starts with this one's, e.g. 123 and 1234.
Remove the (exclusive) tag from probe_vfs_getname.sh and record+script_probe_vfs_getname.sh so they run concurrently in pass 1. trace+probe_vfs_getname.sh has to stay exclusive: it is the one test that wants to be discovered by that wildcard, so it sets vfs_getname to a "vfs_getname_$$" name before sourcing the library, and would then pin its siblings' probes if it ran alongside them. A comment in the test records this.
--- .../perf/tests/shell/lib/probe_vfs_getname.sh | 34 ++++++++++++++++--- tools/perf/tests/shell/probe_vfs_getname.sh | 3 +- .../shell/record+script_probe_vfs_getname.sh | 18 +++++++--- .../tests/shell/trace+probe_vfs_getname.sh | 9 +++++ 4 files changed, 54 insertions(+), 10 deletions(-)
tools/perf/tests/shell/lib/probe_vfs_getname.sh +30 -4
@@ -1,12 +1,38 @@ #!/bin/bash # Arnaldo Carvalho de Melo <[email protected]>, 2017 -perf probe -l 2>&1 | grep -q probe:vfs_getname +# The name of the getname_flags probe added and removed below. +# +# It is scoped to the pid so that tests running in parallel do not collide, +# and it deliberately does not start with "vfs_getname": perf trace calls +# evlist__add_vfs_getname(), which opens everything matching the hardcoded +# "probe:vfs_getname*" wildcard, so a perf trace running in another test would +# otherwise pin this probe and make the 'perf probe -d' below fail with -EBUSY. +# +# trace+probe_vfs_getname.sh is the one test that does want to be found that +# way, so it sets vfs_getname itself before sourcing this file, and is +# (exclusive) as a result. +: "${vfs_getname:=getname_flags_$$}" + +# Print the probes add_probe_vfs_getname() created. perf probe appends _1, _2, +# ... when getname_flags is inlined at more than one call site, so there can be +# several. Match them exactly rather than with a "${vfs_getname}*" glob: the +# name ends in a pid, so such a glob would also match the probes of a test +# whose pid merely starts with this one's, e.g. 123 and 1234. +probes_vfs_getname() { + perf probe -l 2>/dev/null | awk '{print $1}' | + grep -E "^probe:${vfs_getname}(_[[:digit:]]+)?$" +} + +[ -n "$(probes_vfs_getname)" ] had_vfs_getname=$? cleanup_probe_vfs_getname() { if [ $had_vfs_getname -eq 1 ] ; then - perf probe -q -d probe:vfs_getname* + local probe + for probe in $(probes_vfs_getname); do + perf probe -q -d "$probe" + done fi }
@@ -33,8 +59,8 @@ add_probe_vfs_getname() { return 2 fi - perf probe -q "vfs_getname=getname_flags:${line} pathname=result->name:string" || \ - perf probe $add_probe_verbose "vfs_getname=getname_flags:${line} pathname=filename:ustring" || return 1 + perf probe -q "${vfs_getname}=getname_flags:${line} pathname=result->name:string" || \ + perf probe $add_probe_verbose "${vfs_getname}=getname_flags:${line} pathname=filename:ustring" || return 1 fi }
tools/perf/tests/shell/probe_vfs_getname.sh +1 -2
@@ -1,6 +1,5 @@ #!/bin/bash -# Add vfs_getname probe to get syscall args filenames (exclusive) - +# Add vfs_getname probe to get syscall args filenames # SPDX-License-Identifier: GPL-2.0 # Arnaldo Carvalho de Melo <[email protected]>, 2017
tools/perf/tests/shell/record+script_probe_vfs_getname.sh +14 -4
@@ -1,5 +1,5 @@ #!/bin/bash -# Use vfs_getname probe to get syscall args filenames (exclusive) +# Use vfs_getname probe to get syscall args filenames # Uses the 'perf test shell' library to add probe:vfs_getname to the system # then use it with 'perf record' using 'touch' to write to a temp file, then
@@ -17,22 +17,32 @@ skip_if_no_perf_probe || exit 2 # shellcheck source=lib/probe_vfs_getname.sh . "$(dirname "$0")/lib/probe_vfs_getname.sh" +# shellcheck disable=SC2154 # vfs_getname is assigned in lib/probe_vfs_getname.sh record_open_file() { echo "Recording open file:" # Check presence of libtraceevent support to run perf record - skip_no_probe_record_support "probe:vfs_getname*" + skip_no_probe_record_support if [ $? -eq 2 ]; then echo "WARN: Skipping test record_open_file. No libtraceevent support" return 2 fi - perf record -o ${perfdata} -e probe:vfs_getname\* touch $file + # Record every probe the inlining of getname_flags produced, naming + # them exactly rather than with a "${vfs_getname}*" glob, which would + # also match the probes of a test whose pid starts with this one's. + local events + events=$(probes_vfs_getname | paste -sd, -) + if [ -z "${events}" ] ; then + echo "FAIL: no ${vfs_getname} probe to record" + return 1 + fi + perf record -o ${perfdata} -e "${events}" touch $file } perf_script_filenames() { echo "Looking at perf.data file for vfs_getname records for the file we touched:" perf script -i ${perfdata} | \ - grep -E " +touch +[0-9]+ +\[[0-9]+\] +[0-9]+\.[0-9]+: +probe:vfs_getname[_0-9]*: +\([[:xdigit:]]+\) +pathname=\"${file}\"" + grep -E " +touch +[0-9]+ +\[[0-9]+\] +[0-9]+\.[0-9]+: +probe:${vfs_getname}(_[0-9]+)?: +\([[:xdigit:]]+\) +pathname=\"${file}\"" } add_probe_vfs_getname
tools/perf/tests/shell/trace+probe_vfs_getname.sh +9 -0
@@ -10,6 +10,13 @@ # SPDX-License-Identifier: GPL-2.0 # Arnaldo Carvalho de Melo <[email protected]>, 2017 +# This test must stay exclusive, and is the only one of the probe tests that +# does: it does not name the event it uses. perf trace discovers it with the +# hardcoded "probe:vfs_getname*" wildcard in evlist__add_vfs_getname(), so the +# probe has to carry that prefix, and a parallel run of this test would then +# also match, and pin, the probes of the other tests. The sibling tests avoid +# all of this by using a name that the wildcard cannot reach. + # shellcheck source=lib/probe.sh . "$(dirname $0)"/lib/probe.sh
@@ -17,6 +24,8 @@ skip_if_no_perf_probe || exit 2 skip_if_no_perf_trace || exit 2 [ "$(id -u)" = 0 ] || exit 2 +# shellcheck disable=SC2034 # consumed by lib/probe_vfs_getname.sh +vfs_getname="vfs_getname_$$" . "$(dirname $0)"/lib/probe_vfs_getname.sh trace_open_vfs_getname() {
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v2 09/14] perf test probe_vfs_getname: Scope probe name to PID and make non-exclusive
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
The probe name
vfs_getnamewas hardcoded, causing collisions when tests ran concurrently. Furthermore,cleanup_probe_vfs_getname()usedperf probe -d probe:vfs_getname*, deleting probes registered by other parallel tests.Scope the probe name to the pid, and rename it to
getname_flags_$$so that it no longer begins with "vfs_getname". perf trace callsevlist__add_vfs_getname(), which opens every event matching a hardcoded "probe:vfs_getname*" wildcard, so a perf trace run by any other test would otherwise pin this probe and makeperf probe -dfail with-EBUSY. That also unblocks making the perf trace tests non-exclusive later in this series.
Quoted 8 lines
Enumerate the probes to record and to delete from
perf probe -l, matching^probe:${vfs_getname}(_[[:digit:]]+)?$exactly, rather than [ ... ]
its siblings' probes if it ran alongside them. A comment in the test records this.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
Ian Rogers
reviewerPATCH v2 10/14 perf test record+probe_libc_inet_pton: Scope event to PID, add retries, and make non-exclusive
In reply to Ian RogersThe uprobe name was not scoped to PID, and concurrent writes to /sys/kernel/debug/tracing/uprobe_events can occasionally return -EBUSY when another process holds the tracefs inode lock.
Scope the probe event name with $$ (inet_pton_$$=inet_pton) and add a retry loop with backoff for uprobe addition. Drop the (exclusive) tag so the test can run in parallel during pass 1.
A PID scoped probe is no longer cleaned up by any other test, so add an EXIT/TERM/INT trap to delete it, otherwise an interrupted run leaks the uprobe into the system. The trap is installed only after the root and IPv6 checks that exit 2 to skip the test, as trap_cleanup() exits 1 and would otherwise turn those skips into failures. Deletion enumerates the probes from perf probe -l, matching ^probe_libc:inet_pton_$$(_[[:digit:]]+)?$ exactly, rather than reading $event_name: a signal arriving after perf probe injected the uprobe but before the assignment completed would leave that variable empty and leak the probe, and an inet_pton_$$* glob would reach the probe of a test whose pid merely starts with this one's.
While here use mktemp rather than mktemp -u for the temporary files:
this test runs as root in a world writable /tmp, and predicting a name without creating it allows another user to win the race and plant a symlink. The perf.data check becomes -s rather than -e as mktemp now pre-creates an empty file.
--- .../shell/record+probe_libc_inet_pton.sh | 83 ++++++++++++++----- 1 file changed, 63 insertions(+), 20 deletions(-)
tools/perf/tests/shell/record+probe_libc_inet_pton.sh +63 -20
@@ -1,5 +1,5 @@ #!/bin/bash -# probe libc's inet_pton & backtrace it with ping (exclusive) +# probe libc's inet_pton & backtrace it with ping # Installs a probe on libc's inet_pton function, that will use uprobes, # then use 'perf trace' on a ping to localhost asking for just one packet
@@ -21,20 +21,30 @@ nm -Dg $libc 2>/dev/null | grep -F -q inet_pton || exit 254 event_pattern='probe_libc:inet_pton(_[[:digit:]]+)?' add_libc_inet_pton_event() { + local attempts=0 + while [ $attempts -lt 3 ]; do + event_name=$(perf probe -f -x $libc -a "inet_pton_$$=inet_pton" 2>&1 | \ + awk -v ep="$event_pattern" -v l="$libc" '$0 ~ ep && $0 ~ \ + ("\\(on inet_pton in " l "\\)") {print $1}' | head -n 1) + + if [ -n "$event_name" ]; then + return 0 + fi + attempts=$((attempts + 1)) + sleep 0.1 + done - event_name=$(perf probe -f -x $libc -a inet_pton 2>&1 | \ - awk -v ep="$event_pattern" -v l="$libc" '$0 ~ ep && $0 ~ \ - ("\\(on inet_pton in " l "\\)") {print $1}' | head -n 1) - - if [ $? -ne 0 ] || [ -z "$event_name" ] ; then - printf "FAIL: could not add event\n" - return 1 - fi + printf "FAIL: could not add event\n" + return 1 } trace_libc_inet_pton_backtrace() { - expected=`mktemp -u /tmp/expected.XXX` + # Create the files rather than just reserving names with mktemp -u: + # this runs as root and /tmp is world writable, so a predictable name + # that is written to later can be pre-created as a symlink by an + # unprivileged user and used to clobber an arbitrary file. + expected=$(mktemp /tmp/expected.XXX) echo "ping[][0-9 \.:]+$event_name: \([[:xdigit:]]+\)" > $expected echo ".*inet_pton\+0x[[:xdigit:]]+[[:space:]]\($libc|inlined\)$" >> $expected
@@ -50,8 +60,8 @@ trace_libc_inet_pton_backtrace() { ;; esac - perf_data=`mktemp -u /tmp/perf.data.XXX` - perf_script=`mktemp -u /tmp/perf.script.XXX` + perf_data=$(mktemp /tmp/perf.data.XXX) + perf_script=$(mktemp /tmp/perf.script.XXX) # Check presence of libtraceevent support to run perf record skip_no_probe_record_support "$event_name/$eventattr/"
@@ -61,9 +71,10 @@ trace_libc_inet_pton_backtrace() { fi perf record -e $event_name/$eventattr/ -o $perf_data ping -6 -c 1 ::1 > /dev/null 2>&1 - # check if perf data file got created in above step. - if [ ! -e $perf_data ]; then - printf "FAIL: perf record failed to create \"%s\" \n" "$perf_data" + # Check perf record actually wrote data. mktemp already created the + # file, so test that it is non-empty rather than that it exists. + if [ ! -s $perf_data ]; then + printf "FAIL: perf record failed to write \"%s\" \n" "$perf_data" return 1 fi perf script -i $perf_data | tac | grep -m1 ^ping -B9 | tac > $perf_script
@@ -97,21 +108,53 @@ trace_libc_inet_pton_backtrace() { # even if the perf script output does not match. } +# Print the pid scoped uprobes this test may have created. perf probe appends +# _1, _2, ... when the name is already taken, so match those too, but anchor +# the match: an "inet_pton_$$*" glob would also match the probe of a test whose +# pid merely starts with this one's, e.g. 123 and 1234. +libc_inet_pton_events() { + perf probe -l 2>/dev/null | awk '{print $1}' | + grep -E "^probe_libc:inet_pton_$$(_[[:digit:]]+)?$" +} + delete_libc_inet_pton_event() { + # Ask the kernel what is actually there rather than trusting + # $event_name: a signal arriving after perf probe injected the uprobe + # but before the assignment to event_name completed would otherwise + # leave the variable empty and leak the probe. + local probe + for probe in $(libc_inet_pton_events); do + perf probe -q -d "$probe" + done +} - if [ -n "$event_name" ] ; then - perf probe -q -d $event_name - fi +cleanup() { + rm -f ${perf_data} ${perf_script} ${expected} + delete_libc_inet_pton_event + + trap - EXIT TERM INT +} + +trap_cleanup() { + cleanup + exit 1 } # Check for IPv6 interface existence ip a sh lo | grep -F -q inet6 || exit 2 [ "$(id -u)" = 0 ] || exit 2 +# Install the trap only now that the skips above are out of the way: it exits +# 1, so arming it any earlier would turn an 'exit 2' skip into a failure. +# +# The event name is pid scoped, so unlike the old fixed name an orphan left +# behind by an interrupted run is never overwritten by a later run: it would +# stay in the kernel forever. Always clean up, including on a signal. +trap trap_cleanup EXIT TERM INT + skip_if_no_perf_probe && \ add_libc_inet_pton_event && \ trace_libc_inet_pton_backtrace err=$? -rm -f ${perf_data} ${perf_script} ${expected} -delete_libc_inet_pton_event +cleanup exit $err
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v2 10/14] perf test record+probe_libc_inet_pton: Scope event to PID, add retries, and make non-exclusive
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] perf/tests: missing retry loop in uprobe deletion causes permanent leaks --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] perf/tests: missing retry loop in uprobe deletion causes permanent leaks --
commit fcd4501f2721d617618e1b6d7a9df07bf2b93da0
Author: Ian Rogers <[email protected]>
perf test record+probe_libc_inet_pton: Scope event to PID, add retries, and make non-exclusive
This commit adds a PID scope to the uprobe event name, includes a retry loop for uprobe addition to handle tracefs contention, and makes the test non-exclusive. It also introduces mktemp for temporary file creation and adds trap handlers for interrupt cleanup.
Quoted 4 lines
tools/perf/tests/shell/record+probe_libc_inet_pton.sh +0
-0
[ ... ]
tools/perf/tests/shell/record+probe_libc_inet_pton.sh trace_libc_inet_pton_backtrace() { · line 127
Quoted 8 lines
delete_libc_inet_pton_event(){tools/perf/tests/shell/record+probe_libc_inet_pton.sh +7
-0+ # Ask the kernel what is actually there rather than trusting + # $event_name: a signal arriving after perf probe injected the uprobe + # but before the assignment to event_name completed would otherwise + # leave the variable empty and leak the probe. + local probe + for probe in $(libc_inet_pton_events); do + perf probe -q -d "$probe"
[Severity: Medium]
The commit message notes that concurrent writes to uprobe_events can return -EBUSY, which is why a retry loop was added to add_libc_inet_pton_event(). Since perf probe -d also writes to the same tracefs file, could it hit the same -EBUSY contention here in delete_libc_inet_pton_event()?
If the deletion command fails with -EBUSY, the script would continue and exit without removing the uprobe. Because the probe name is now PID-scoped, would this turn a transient deletion failure into a permanent uprobe leak since subsequent tests won't overwrite it?
Should this deletion path use a similar retry loop to the addition path?
Quoted 5 lines
tools/perf/tests/shell/record+probe_libc_inet_pton.sh +1
-3- if [ -n "$event_name" ] ; then - perf probe -q -d $event_name - fi + done }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Ian Rogers
reviewerPATCH v2 11/14 perf test trace_summary: Improve error diagnostics
In reply to Ian RogersWhen pattern matching fails in test_perf_trace(), print the command that failed along with the actual match count, the matching lines found, and the first 15 lines of output to aid debugging.
--- tools/perf/tests/shell/trace_summary.sh | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
tools/perf/tests/shell/trace_summary.sh +8 -4
@@ -28,10 +28,14 @@ test_perf_trace() { count=$(grep -E -c -m 3 "${search}" ${OUTPUT}) if [ "${count}" != "3" ]; then - echo "Error: cannot find enough pattern ${search} in the output" - cat ${OUTPUT} - rm -f ${OUTPUT} - exit 1 + echo "Error: cannot find enough pattern ${search} (count=${count}) in output of:" + echo "Error: perf trace ${args} -- ${workload}" + echo "Error: matched lines:" + grep -E "${search}" ${OUTPUT} || echo "none" + echo "Error: first 15 lines of output:" + head -n 15 ${OUTPUT} + rm -f ${OUTPUT} + exit 1 fi }
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v2 11/14] perf test trace_summary: Improve error diagnostics
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines
When pattern matching fails in
test_perf_trace(), print the command that failed along with the actual match count, the matching lines found, and the first 15 lines of output to aid debugging.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
Ian Rogers
reviewerPATCH v2 12/14 perf test trace_btf_general: Drop --max-events=1 and make non-exclusive
In reply to Ian Rogerstrace_btf_general.sh used --max-events=1 with perf trace on commands such as mv, echo, and sleep. When background activity occurs or tests run in parallel, perf trace can capture an event from an unrelated process and exit prematurely before recording the target command's syscalls.
Drop --max-events=1 and let tracing run until the command completes, checking for the expected output with grep (matching trace_btf_enum.sh). Remove the (exclusive) tag so the test runs in parallel.
Running perf trace in parallel is only safe now that the probe tests no longer name their probes "vfs_getname...": perf trace opens everything matching a hardcoded "probe:vfs_getname*" wildcard, which used to pin those probes and make their cleanup fail with -EBUSY.
--- tools/perf/tests/shell/trace_btf_general.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
tools/perf/tests/shell/trace_btf_general.sh +4 -4
@@ -1,5 +1,5 @@ #!/bin/bash -# perf trace BTF general tests (exclusive) +# perf trace BTF general tests # SPDX-License-Identifier: GPL-2.0 err=0
@@ -27,7 +27,7 @@ check_vmlinux() { trace_test_string() { echo "Testing perf trace's string augmentation" - output="$(perf trace --sort-events -e renameat* --max-events=1 -- mv ${file1} ${file2} 2>&1)" + output="$(perf trace --sort-events -e renameat* -- mv ${file1} ${file2} 2>&1)" if ! echo "$output" | grep -q -E "^mv/[0-9]+ renameat(2)?\(.*, \"${file1}\", .*, \"${file2}\", .*\) += +[0-9]+$" then printf "String augmentation test failed, output:\n$output\n"
@@ -38,7 +38,7 @@ trace_test_string() { trace_test_buffer() { echo "Testing perf trace's buffer augmentation" # echo will insert a newline (\10) at the end of the buffer - output="$(perf trace --sort-events -e write --max-events=1 -- echo "${buffer}" 2>&1)" + output="$(perf trace --sort-events -e write -- echo "${buffer}" 2>&1)" if ! echo "$output" | grep -q -E "^echo/[0-9]+ write\([0-9]+, ${buffer}.*, [0-9]+\) += +[0-9]+$" then printf "Buffer augmentation test failed, output:\n$output\n"
@@ -48,7 +48,7 @@ trace_test_buffer() { trace_test_struct_btf() { echo "Testing perf trace's struct augmentation" - output="$(perf trace --sort-events -e clock_nanosleep --force-btf --max-events=1 -- sleep 1 2>&1)" + output="$(perf trace --sort-events -e clock_nanosleep --force-btf -- sleep 1 2>&1)" if ! echo "$output" | grep -q -E "^sleep/[0-9]+ clock_nanosleep\(0, 0, \{1,.*\}, 0x[0-9a-f]+\) += +[0-9]+$" then printf "BTF struct augmentation test failed, output:\n$output\n"
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v2 12/14] perf test trace_btf_general: Drop --max-events=1 and make non-exclusive
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines
trace_btf_general.shused--max-events=1withperf traceon commands such asmv,echo, andsleep. When background activity occurs or tests run in parallel,perf tracecan capture an event from an unrelated process and exit prematurely before recording the target command's syscalls.Drop
--max-events=1and let tracing run until the command completes, checking for the expected output with grep (matchingtrace_btf_enum.sh). Remove the (exclusive) tag so the test runs in parallel.
Quoted 7 lines
Running perf trace in parallel is only safe now that the probe tests no longer name their probes "
vfs_getname...": perf trace opens everything matching a hardcoded "probe:vfs_getname*" wildcard, which used to pin those probes and make their cleanup fail with-EBUSY.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
Ian Rogers
reviewerPATCH v2 13/14 perf test trace_summary: Make non-exclusive
In reply to Ian Rogerstrace_summary.sh tests various summary modes of perf trace. It already directs output to a unique temporary file without polluting the current working directory.
Remove the (exclusive) tag so it can run concurrently in parallel test runs.
Running perf trace in parallel is only safe now that the probe tests no longer name their probes "vfs_getname...": perf trace opens everything matching a hardcoded "probe:vfs_getname*" wildcard, which used to pin those probes and make their cleanup fail with -EBUSY.
--- tools/perf/tests/shell/trace_summary.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/tests/shell/trace_summary.sh +1 -1
@@ -1,5 +1,5 @@ #!/bin/bash -# perf trace summary (exclusive) +# perf trace summary # SPDX-License-Identifier: GPL-2.0 # Check that perf trace works with various summary mode
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v2 13/14] perf test trace_summary: Make non-exclusive
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines
trace_summary.shtests various summary modes ofperf trace. It already directs output to a unique temporary file without polluting the current working directory.Remove the (exclusive) tag so it can run concurrently in parallel test runs.
Quoted 7 lines
Running perf trace in parallel is only safe now that the probe tests no longer name their probes "
vfs_getname...": perf trace opens everything matching a hardcoded "probe:vfs_getname*" wildcard, which used to pin those probes and make their cleanup fail with-EBUSY.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
Ian Rogers
reviewerPATCH v2 14/14 perf test uprobe_from_different_cu: Scope probe name to PID
In reply to Ian RogersThe test builds a binary in a per-run temporary directory and probes its foo function. The directory name is unique, but perf probe derives the event name from the probed function and the group name from the binary's basename, so every run registers the same probe_testfile:foo event.
Running the test concurrently with itself, as 'perf test -r3' does, therefore fails in all but one of the runs with:
Error: event "foo" already exists.
Hint: Remove existing event by 'perf probe -d'
and a losing run's cleanup goes on to delete the winning run's probe out from under it.
Name the event after the pid, foo_$$, so that parallel runs no longer collide. This lets the test stay in the parallel pass rather than having to be marked (exclusive).
--- .../perf/tests/shell/test_uprobe_from_different_cu.sh | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
tools/perf/tests/shell/test_uprobe_from_different_cu.sh +9 -2
@@ -18,12 +18,19 @@ fi temp_dir=$(mktemp -d /tmp/perf-uprobe-different-cu-sh.XXXXXXXXXX) +# The name of the uprobe added and removed below. The probe is placed on +# ${temp_dir}/testfile, but perf probe derives the event name from the probed +# function and the group name from the binary's basename, so every run would +# otherwise share one probe_testfile:foo event, and a concurrent run would +# fail with 'event "foo" already exists'. Scope the event name to the pid. +probe_name="foo_$$" + cleanup() { trap - EXIT TERM INT if [[ "${temp_dir}" =~ ^/tmp/perf-uprobe-different-cu-sh.*$ ]]; then echo "--- Cleaning up ---" - perf probe -x ${temp_dir}/testfile -d foo || true + perf probe -x ${temp_dir}/testfile -d ${probe_name} || true rm -f "${temp_dir}/"* rmdir "${temp_dir}" fi
@@ -84,6 +91,6 @@ gcc -g -Og -c ${temp_dir}/testfile-main.c -o ${temp_dir}/testfile-main.o gcc -g -Og -o ${temp_dir}/testfile ${temp_dir}/testfile-foo.o ${temp_dir}/testfile-main.o perf probe -x ${temp_dir}/testfile --funcs foo | grep "foo" -perf probe -x ${temp_dir}/testfile foo +perf probe -x ${temp_dir}/testfile ${probe_name}=foo cleanup
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v2 14/14] perf test uprobe_from_different_cu: Scope probe name to PID
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines
The test builds a binary in a per-run temporary directory and probes its foo function. The directory name is unique, but perf probe derives the event name from the probed function and the group name from the binary's basename, so every run registers the same probe_testfile:foo event.
Running the test concurrently with itself, as 'perf test -r3' does, therefore fails in all but one of the runs with:
Error: event "foo" already exists.
Hint: Remove existing event by 'perf probe -d'and a losing run's cleanup goes on to delete the winning run's probe out from under it.
Quoted 6 lines
Name the event after the pid, foo_$$, so that parallel runs no longer collide. This lets the test stay in the parallel pass rather than having to be marked (exclusive).
Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
Ian Rogers
reviewerv3 cover perf trace: Fix BPF filtering and make tracing tests non-exclusive
In reply to Ian Rogersperf trace's BPF augmentation attaches to raw_syscalls:sys_enter and raw_syscalls:sys_exit system wide, and used the program return value to decide whether a syscall was interesting. Returning 0 from a BPF_PROG_TYPE_TRACEPOINT program makes perf_trace_run_bpf_submit() drop the event for every listener on that tracepoint, not just for the perf trace that installed the program. Any concurrent perf trace, perf record or ftrace session watching raw_syscalls therefore lost events, which is one of the reasons so many of the perf trace and perf probe shell tests had to be marked (exclusive) and run on their own.
Patches 1 and 2 are independent fixes to the files the rest of the series goes on to rework.
Patches 3 to 8 fix perf trace. They stop the return value being used as a filter and do the filtering in BPF maps instead, fix argument handling for the __data_loc internal tracepoint fields that syscalls:sys_enter_* gained in 6.19, stop the sys_exit program array tail calling a sys_enter augmenter, and replace the userspace PERF_RECORD_FORK/PERF_RECORD_EXIT bookkeeping with BTF-typed raw tracepoint programs on sched_process_{fork,exit,exec}. A task is then registered before its first syscall and evicted in do_exit(), rather than whenever userspace next drains the ring buffer.
Patches 9 to 16 deal with the tests. Several collided with each other through global state rather than through perf trace: fixed probe names, clear_all_probes() disabling every tracepoint on the system, and perf trace's hardcoded "probe:vfs_getname*" wildcard pinning probes belonging to other tests. With those scoped to a pid they can drop (exclusive) and run in parallel again.
Tested on x86_64. The trace and probe tests pass under 'perf test -r3', which runs the repeats concurrently. Every patch builds individually, and the series also builds with BUILD_BPF_SKEL=0.
Changes since v2:
- Rebased onto the current perf-tools-next.
- Patch 1 also includes <assert.h>, for the assert() in augmented_syscalls__create_bpf_output().
- New patch 2 makes evsel__put_and_free_priv() free the whole evsel_trace. It only zfree()d the struct, leaking the syscall_arg_fmt array hanging off it. No caller can reach that today, but patch 6 adds one that discards a fully set up evsel.
- Patch 6 identifies the evsel to drop from the evlist by comparing against trace.syscalls.events.sys_enter instead of a strstr() for "syscalls:sys_enter". That substring also matches the per syscall syscalls:sys_enter_SYSCALL tracepoints, so a user asking for one of those by name would have had it removed from the evlist, and __augmented_syscalls__ described with its tracefs format rather than the raw tracepoint's.
- Patch 6 reports a failure to program the pid filters with the error that caused it. trace__run() sent everything trace__set_filter_pids() returned to out_error_mem, which prints "Not enough memory to run!". That was already a guess, and a wrong one now the function writes BPF maps too: the update fails with -E2BIG when the target has more threads than pids_to_trace has room for.
- Patch 6 reports a failure to program the syscall filters the same way. The pr_err() in trace__set_ev_qualifier_filter() ran before trace__run() printed "%m" at out_errno, and anything called in between could have changed errno by then, so the two did not have to agree. The inner report is now a pr_debug() and the caller prints the error it was given, which leaves out_errno without a user.
- Patch 7 moves the pid across in sched_process_exec() by deleting the old key before inserting the new one. The move is only a rename, but holding both keys at once needs a spare slot, and on a full map the insert failed with -E2BIG while the delete still succeeded, losing the task instead of moving it.
- Patch 7 no longer claims that a task forked during the attach window is still traced unaugmented. That was wrong: cmd_trace() removes the sys_enter evsel once the bpf-output event exists, so __augmented_syscalls__ is the only source of enter events and such a task is not reported at all. Describe what is really lost, why the tgid fallback is not kept as a safety net for it, note that only 'perf trace -p' is exposed, and that closing it needs the target's descendants re-enumerated from /proc after the attach.
- Patch 7 drops the parent tgid test from sched_process_fork(), so a child inherits from the pid of the thread that called clone() and from nothing else. The lookups only ever match a task's own pid, and every thread of a -p target is enumerated from /proc/<pid>/task and inserted in its own right, so the tgid added no reach. What it did add was a child inheriting from a thread that is not traced itself, such as a sibling of the thread 'perf trace -t' selected.
- Patch 7 ends the session with the error that caused it when the BPF programs cannot be attached, rather than printing a bare errno left over from unwinding the attach. There is nothing to fall back to at that point, cmd_trace() has already built the evlist around __augmented_syscalls__ and dropped the sys_enter evsel, and the code being replaced did not fall back either: it ignored the result of augmented_raw_syscalls_bpf__attach() altogether.
- New patch 8 reads the target out of /proc again once the BPF programs are attached, so a task the target created while perf trace was starting up is traced rather than missed for the whole session. This is the gap patch 7 describes and left for later. It covers the target's new threads and, through task->children, anything it or they forked, to any depth. What is left is a task that made syscalls between sys_enter going live and being added to the map, which is momentary rather than lasting for the session.
- Patch 9 bails out if mktemp fails rather than carrying on with an empty $tmpdir. cd rejects the null directory, and the rmdir that was meant to undo the mktemp then failed to remove '' instead. Its cleanup() also no longer exits when it cannot cd out of the temporary directory, which would have skipped removing it. The removal takes an absolute path and does not need the cd to have succeeded.
- Patch 10 now narrows the disable in clear_all_probes() to the probes themselves rather than dropping it. Clearing kprobe_events or uprobe_events is all or nothing: dyn_events_release_all() returns -EBUSY without removing anything if it finds a probe that is still enabled, so simply removing the write could leave stale probes behind to collide with the next run. The set to disable is read from the kprobe_events and uprobe_events listings rather than assumed to be the groups perf uses, since a probe left enabled in another group, such as the default kprobes group used when kprobe_events is written directly, would abort the clear just the same.
- Patch 11 deletes the probes from an exit trap as well as on the way out. A pid scoped name is never seen again, so a run interrupted before cleanup_probe_vfs_getname() left its probes behind for good, a set per run, where the fixed name was at least found and reused by the next run.
- Patch 12 retries deleting the uprobe. Deletion writes uprobe_events just as addition does and can lose the same race with a concurrent test, and because the event name is now pid scoped a probe left behind is never overwritten by a later run. It also bails out if mktemp fails and quotes the path in the emptiness check, which unquoted would have tested the string "-s" and reported success.
- Patch 13 prints the tail of the output rather than the head. The pattern it matches only appears in the summary, which is printed after any trace output, so the head of the file is not the part that failed to match.
Changes since v1:
- New patch 1 includes <sys/types.h> and <string.h> for the pid_t and strcmp() uses that were relying on the include chain happening to drag them in, which does not hold on libcs such as musl.
- Patch 6 no longer returns success when the event qualifier filter string fails to allocate. err now defaults to 0 because either tracepoint may legitimately be absent, so the ENOMEM path has to set the error itself rather than rely on that default. It also includes <stdbool.h> for the bool parameters it adds to trace_augment.h.
- Patch 9 removes the temporary directory if the cd into it fails. That happens before the cleanup trap is installed, so the directory would otherwise be left behind in /tmp.
Ian Rogers (16):
perf trace: Include the headers declaring pid_t, strcmp and assert perf trace: Free the whole evsel_trace in evsel__put_and_free_priv perf trace: Start BPF summary before starting workload perf trace: Skip internal tracepoint fields in formatting and beauty
map
perf trace: Do not set unaugmented BPF program on sys_exit map perf trace: Filter events in BPF and avoid tracepoint vetoes perf trace: Handle fork and exit directly in BPF filter maps perf trace: Enumerate the target again once BPF is attached perf test test_task_analyzer: Isolate in temporary directory and make
non-exclusive
perf test common: Only disable probes in clear_all_probes perf test probe_vfs_getname: Scope probe name to PID and make
non-exclusive
perf test record+probe_libc_inet_pton: Scope event to PID, add
retries, and make non-exclusive
perf test trace_summary: Improve error diagnostics
perf test trace_btf_general: Drop --max-events=1 and make
non-exclusive
perf test trace_summary: Make non-exclusive
perf test uprobe_from_different_cu: Scope probe name to PID
tools/perf/Documentation/perf-trace.txt | 5 + tools/perf/builtin-trace.c | 637 +++++++++++++++--- tools/perf/tests/shell/common/init.sh | 33 +- .../perf/tests/shell/lib/probe_vfs_getname.sh | 51 +- tools/perf/tests/shell/probe_vfs_getname.sh | 3 +- .../shell/record+probe_libc_inet_pton.sh | 107 ++- .../shell/record+script_probe_vfs_getname.sh | 18 +- tools/perf/tests/shell/test_task_analyzer.sh | 20 +- .../shell/test_uprobe_from_different_cu.sh | 11 +- .../tests/shell/trace+probe_vfs_getname.sh | 9 + tools/perf/tests/shell/trace_btf_general.sh | 8 +- tools/perf/tests/shell/trace_summary.sh | 16 +- .../bpf_skel/augmented_raw_syscalls.bpf.c | 312 ++++++++- tools/perf/util/bpf_trace_augment.c | 175 ++++- tools/perf/util/trace_augment.h | 27 +- 15 files changed, 1281 insertions(+), 151 deletions(-)
base-commit: 86a27811675a415bd351efca1a194a1a94c082dd
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
Ian Rogers
reviewerPATCH v3 1/16 perf trace: Include the headers declaring pid_t, strcmp and assert
trace_augment.h uses pid_t in the augmented_syscalls__set_filter_pids() prototype and in the !HAVE_BPF_SKEL stub. bpf_trace_augment.c calls strcmp() in augmented_syscalls__find_by_title() and assert() in augmented_syscalls__create_bpf_output(), but neither pulls in the header that declares what it uses. Both happen to build today only because something else in the include chain drags <sys/types.h>, <string.h> and <assert.h> in first, which is not guaranteed and does not hold on libcs such as musl that keep the POSIX namespaces strictly separated.
Include <sys/types.h>, <string.h> and <assert.h> explicitly.
--- tools/perf/util/bpf_trace_augment.c | 2 ++ tools/perf/util/trace_augment.h | 1 + 2 files changed, 3 insertions(+)
tools/perf/util/bpf_trace_augment.c +2 -0
@@ -1,5 +1,7 @@ +#include <assert.h> #include <bpf/libbpf.h> #include <internal/xyarray.h> +#include <string.h> #include "bpf_skel/augmented_raw_syscalls.skel.h" #include "debug.h"
tools/perf/util/trace_augment.h +1 -0
@@ -2,6 +2,7 @@#define TRACE_AUGMENT_H #include <linux/compiler.h> +#include <sys/types.h> struct bpf_program ;struct evlist ;
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v3 01/16] perf trace: Include the headers declaring pid_t, strcmp and assert
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
trace_augment.husespid_tin theaugmented_syscalls__set_filter_pids()prototype and in the !HAVE_BPF_SKEL stub.bpf_trace_augment.ccallsstrcmp()inaugmented_syscalls__find_by_title()andassert()inaugmented_syscalls__create_bpf_output(), but neither pulls in the header that declares what it uses. Both happen to build today only because something else in the include chain drags <sys/types.h>, <string.h> and <assert.h> in first, which is not guaranteed and does not hold on libcs such as musl that keep the POSIX namespaces strictly separated.
Quoted 4 lines
Include <
sys/types.h>, <string.h> and <assert.h> explicitly.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Ian Rogers
reviewerPATCH v3 2/16 perf trace: Free the whole evsel_trace in evsel__put_and_free_priv
In reply to Ian RogersEvery evsel->priv in builtin-trace.c is a struct evsel_trace, allocated by evsel_trace__new(). It holds a syscall_arg_fmt array in its fmt member, which evsel__syscall_arg_fmt() allocates on demand for the syscalls:sys_{enter,exit}_SYSCALL tracepoints and for every other tracepoint that gets its arguments pretty printed.
evsel__put_and_free_priv() only did zfree(&evsel->priv), releasing the evsel_trace itself and leaking that array. Use evsel_trace__delete(), which frees fmt first, exactly as the out_delete path of evsel__syscall_arg_fmt() already does.
The current callers are all error paths that run before fmt can have been allocated, so nothing leaks in practice today, but the helper is the obvious thing to reach for whenever an evsel is discarded and it should be safe for that.
--- tools/perf/builtin-trace.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
tools/perf/builtin-trace.c +7 -1
@@ -464,7 +464,13 @@ static int evsel__init_tp_ptr_field(struct evsel *evsel, struct tp_field *field,static void evsel__put_and_free_priv (struct evsel *evsel) { -zfree (&evsel->priv); +/* +* evsel->priv is always a struct evsel_trace here, so it has to go +* through evsel_trace__delete(): zfree() on its own would release the +* struct while leaking the syscall_arg_fmt array hanging off it. +*/ +evsel_trace__delete (evsel->priv); + evsel->priv =NULL ;evsel__put (evsel); }
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v3 02/16] perf trace: Free the whole evsel_trace in evsel__put_and_free_priv
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines
Every
evsel->privinbuiltin-trace.cis a structevsel_trace, allocated byevsel_trace__new(). It holds asyscall_arg_fmtarray in its fmt member, whichevsel__syscall_arg_fmt()allocates on demand for the syscalls:sys_{enter,exit}_SYSCALL tracepoints and for every other tracepoint that gets its arguments pretty printed.
evsel__put_and_free_priv()only didzfree(&evsel->priv), releasing theevsel_traceitself and leaking that array. Useevsel_trace__delete(), which frees fmt first, exactly as theout_deletepath ofevsel__syscall_arg_fmt()already does.
Quoted 7 lines
The current callers are all error paths that run before fmt can have been allocated, so nothing leaks in practice today, but the helper is the obvious thing to reach for whenever an evsel is discarded and it should be safe for that.
Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Ian Rogers
reviewerPATCH v3 3/16 perf trace: Start BPF summary before starting workload
In reply to Ian RogersWhen using --bpf-summary, trace_start_bpf_summary() sets skel->bss->enabled = 1. In trace__run(), trace_start_bpf_summary() was previously invoked after evlist__start_workload().
Because evlist__start_workload() immediately unblocks the child process by writing to its go_pipe, short-lived workloads (such as cat /dev/null) can execute and complete their initial system calls before trace_start_bpf_summary() is reached by the parent process. Furthermore, under high system load, the child process may finish before the BPF summary tracking is enabled in the kernel at all, causing syscall summary tests to fail. Additionally, if initial_delay was configured, the workload was started before sleeping.
Move trace_start_bpf_summary() to be invoked before evlist__start_workload(), matching evlist__enable(), and ensure it respects target.initial_delay.
--- tools/perf/builtin-trace.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
tools/perf/builtin-trace.c +5 -3
@@ -4844,17 +4844,19 @@ static int trace__run(struct trace *trace, int argc, const char **argv)if (!target__none (&trace->opts.target) && !trace->opts.target.initial_delay)evlist__enable (evlist); +if (trace->summary_bpf && !trace->opts.target.initial_delay) +trace_start_bpf_summary (); +if (forks)evlist__start_workload (evlist);if (trace->opts.target.initial_delay) {usleep (trace->opts.target.initial_delay *1000 );evlist__enable (evlist); +if (trace->summary_bpf) +trace_start_bpf_summary (); } -if (trace->summary_bpf) -trace_start_bpf_summary (); - trace->multiple_threads =perf_thread_map__pid (evlist__core (evlist)->threads,0 ) ==-1 ||perf_thread_map__nr (evlist__core (evlist)->threads) >1 ||evlist__first (evlist)->core.attr.inherit;
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v3 03/16] perf trace: Start BPF summary before starting workload
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
When using --bpf-summary,
trace_start_bpf_summary()setsskel->bss->enabled= 1. Intrace__run(),trace_start_bpf_summary()was previously invoked afterevlist__start_workload().Because
evlist__start_workload()immediately unblocks the child process by writing to itsgo_pipe, short-lived workloads (such ascat /dev/null) can execute and complete their initial system calls beforetrace_start_bpf_summary()is reached by the parent process. Furthermore, under high system load, the child process may finish before the BPF summary tracking is enabled in the kernel at all, causing syscall summary tests to fail. Additionally, ifinitial_delaywas configured, the workload was started before sleeping.
Quoted 6 lines
Move
trace_start_bpf_summary()to be invoked beforeevlist__start_workload(), matchingevlist__enable(), and ensure it respects target.initial_delay.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Ian Rogers
reviewerPATCH v3 4/16 perf trace: Skip internal tracepoint fields in formatting and beauty map
In reply to Ian RogersLinux 6.19+ added __data_loc char[] internal fields for string arguments in syscalls:sys_enter_<name> tracepoints (e.g., __data_loc_oldname in sys_enter_renameat2). While is_internal_field() was added to detect them, several places did not properly account for them:
1. In syscall_arg_fmt__init_array(), when an internal field was skipped, the arg pointer was still incremented, causing the subsequent argument formatters to be mismatched.
2. In syscall__scnprintf_args(), internal fields were not skipped, causing spurious trailing arguments like ", 0, 16" to be formatted and printed.
3. In trace__bpf_sys_enter_beauty_map(), internal fields were not skipped, offsetting beauty array argument indices and breaking string and buffer augmentation.
4. In trace__find_usable_bpf_prog_entry(), candidate pointer checks matched on internal pointer fields, breaking signature compatibility matching between syscalls for augmenter sharing. Introduce next_user_arg() and advance both cursors with it, so that the two argument lists are always compared at a real argument and the walk ends when one syscall runs out of arguments rather than when one happens to have trailing internal fields.
5. syscall__augmented_args() computed the augmented payload as sample->raw_size - sc->args_size for any sys_enter style sample. sc->args_size deliberately stops at the last non-internal field, so on 6.19+ a native syscalls:sys_enter_<name> record leaves the __data_loc words and their string payloads in the remainder. Those bytes are not a struct augmented_arg, so syscall_arg__scnprintf_augmented_string() read a bogus length and walked arg->augmented.args out of bounds. This is reachable from trace__event_handler(), which calls trace__fprintf_sys_enter() for any evsel whose tracepoint name starts with "sys_enter_".
6. In syscall__read_info(), syscall__alloc_arg_fmts() was called before checking and dropping the leading __syscall_nr (or nr) field, using nr_fields - 1 unconditionally. If a tracepoint format lacks that leading field, the allocated arg_fmt array is one entry too small and syscall_arg_fmt__init_array() writes one entry past the end of the heap buffer. Drop __syscall_nr/nr first and size the allocation from the remaining fields.
Update these functions to check and skip is_internal_field() so that arguments are correctly formatted and beauty map entries match the expected syscall signatures, restrict syscall__augmented_args() to the __augmented_syscalls__ bpf-output evsel, and size arg_fmt after dropping the syscall number field.
--- tools/perf/builtin-trace.c | 153 ++++++++++++++++++++++++++++--------- 1 file changed, 115 insertions(+), 38 deletions(-)
tools/perf/builtin-trace.c +115 -38
@@ -2283,15 +2283,20 @@ syscall_arg_fmt__init_array(struct syscall_arg_fmt *arg, struct tep_format_fieldstruct tep_format_field *last_field =NULL ;int len; -for (; field; field = field->next, ++arg) { -/* assume it's the last argument */ +for (; field; field = field->next) { +/* +* Skip internal tracepoint fields (e.g., __data_loc strings in +* Linux 6.19+) so they do not advance the syscall arg array index. +*/ if (is_internal_field (field))continue ; last_field = field; -if (arg->scnprintf) +if (arg->scnprintf) { + ++arg;continue ; + } len =strlen (field->name);
@@ -2348,6 +2353,7 @@ syscall_arg_fmt__init_array(struct syscall_arg_fmt *arg, struct tep_format_field } } } + ++arg; }return last_field;
@@ -2369,6 +2375,7 @@ static int syscall__read_info(struct syscall *sc, struct trace *trace)char tp_name[128 ];const char *name;struct tep_format_field *field; +int nr_args;int err;if (sc->nonexistent)
@@ -2407,24 +2414,25 @@ static int syscall__read_info(struct syscall *sc, struct trace *trace)return err; } -/* -* The tracepoint format contains __syscall_nr field, so it's one more -* than the actual number of syscall arguments. -*/ -if (syscall__alloc_arg_fmts (sc, sc->tp_format->format.nr_fields -1 )) -return -ENOMEM ; - sc->args = sc->tp_format->format.fields; + nr_args = sc->tp_format->format.nr_fields;/* * We need to check and discard the first variable '__syscall_nr' * or 'nr' that mean the syscall number. It is needless here. * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels. +* +* Do this before allocating, and size the array from what is left, so +* that a format without the field does not leave +* syscall_arg_fmt__init_array() walking one entry past the end. */ if (sc->args && (!strcmp (sc->args->name,"__syscall_nr" ) || !strcmp (sc->args->name,"nr" ))) { sc->args = sc->args->next; - --sc->nr_args; + --nr_args; } +if (syscall__alloc_arg_fmts (sc, nr_args)) +return -ENOMEM ; + field = sc->args;while (field) {if (is_internal_field (field))
@@ -2642,11 +2650,17 @@ static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,if (sc->args !=NULL ) {struct tep_format_field *field; -for (field = sc->args; field; - field = field->next, ++arg.idx, bit <<=1 ) { -if (arg.mask & bit) +for (field = sc->args; field; field = field->next) { +/* Skip internal fields so they are not printed as spurious arguments */ +if (is_internal_field (field))continue ; +if (arg.mask & bit) { + ++arg.idx; + bit <<=1 ; +continue ; + } + arg.fmt = &sc->arg_fmt[arg.idx]; val =syscall_arg__val (&arg, arg.idx);/*
@@ -2664,8 +2678,11 @@ static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,*/ if (val ==0 && !trace->show_zeros && !(sc->arg_fmt && sc->arg_fmt[arg.idx].show_zero) && - !(sc->arg_fmt && sc->arg_fmt[arg.idx].strtoul ==STUL_BTF_TYPE )) + !(sc->arg_fmt && sc->arg_fmt[arg.idx].strtoul ==STUL_BTF_TYPE )) { + ++arg.idx; + bit <<=1 ;continue ; + } printed +=scnprintf (bf + printed, size - printed,"%s" , printed ?", " :"" );
@@ -2680,12 +2697,16 @@ static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size, size - printed, val, field->type);if (btf_printed) { printed += btf_printed; + ++arg.idx; + bit <<=1 ;continue ; } } printed +=syscall_arg_fmt__scnprintf_val (&sc->arg_fmt[arg.idx], bf + printed, size - printed, &arg, val); + ++arg.idx; + bit <<=1 ; } }else if (IS_ERR (sc->tp_format)) {/*
@@ -2946,7 +2967,9 @@ static int trace__fprintf_sample(struct trace *trace, struct perf_sample *samplereturn printed; } -static void *syscall__augmented_args (struct syscall *sc,struct perf_sample *sample,int *augmented_args_size,int raw_augmented_args_size) +static void *syscall__augmented_args (struct trace *trace,struct syscall *sc, +struct perf_sample *sample, +int *augmented_args_size,int raw_augmented_args_size) {/* * For now with BPF raw_augmented we hook into raw_syscalls:sys_enter
@@ -2964,6 +2987,24 @@ static void *syscall__augmented_args(struct syscall *sc, struct perf_sample *sam*/ int args_size = raw_augmented_args_size ?: sc->args_size; +/* +* Augmented arguments are a perf trace specific payload, they are only +* ever appended to samples emitted by the BPF __augmented_syscalls__ +* bpf-output event. +* +* Native syscalls:sys_enter_NAME tracepoints may also carry trailing +* data of their own: since Linux 6.19 they append __data_loc char[] +* fields plus the string payloads they point at. Those bytes are not a +* struct augmented_arg, so treating them as one would make +* syscall_arg__scnprintf_augmented_string() read a bogus length and +* walk arg->augmented.args far out of bounds. +* +* So only look for augmented arguments on the event that can actually +* produce them. +*/ +if (sample->evsel != trace->syscalls.events.bpf_output) +return NULL ; + *augmented_args_size = sample->raw_size - args_size;if (*augmented_args_size >0 ) {static uintptr_t argbuf[1024 ];/* assuming single-threaded */
@@ -3022,17 +3063,13 @@ static int trace__sys_enter(struct trace *trace,if (!(trace->duration_filter || trace->summary_only || trace->min_stack))trace__printf_interrupted_entry (trace);/* -* If this is raw_syscalls.sys_enter, then it always comes with the 6 possible -* arguments, even if the syscall being handled, say "openat", uses only 4 arguments -* this breaks syscall__augmented_args() check for augmented args, as we calculate -* syscall->args_size using each syscalls:sys_enter_NAME tracefs format file, -* so when handling, say the openat syscall, we end up getting 6 args for the -* raw_syscalls:sys_enter event, when we expected just 4, we end up mistakenly -* thinking that the extra 2 u64 args are the augmented filename, so just check -* here and avoid using augmented syscalls when the evsel is the raw_syscalls one. +* syscall__augmented_args() only returns a payload for the BPF +* __augmented_syscalls__ event, so raw_syscalls:sys_enter (which always +* carries all 6 possible arguments rather than sc->args_size worth) and +* the native syscalls:sys_enter_NAME tracepoints are both handled there. */ -if (evsel != trace->syscalls.events.sys_enter) - augmented_args =syscall__augmented_args (sc, sample, &augmented_args_size, trace->raw_augmented_syscalls_args_size); + augmented_args =syscall__augmented_args (trace, sc, sample, &augmented_args_size, + trace->raw_augmented_syscalls_args_size); ttrace->entry_time = sample->time; ttrace->entry_cpu = sample->cpu; msg = ttrace->entry_str;
@@ -3077,7 +3114,7 @@ static int trace__fprintf_sys_enter(struct trace *trace, struct perf_sample *samstruct syscall *sc;char msg[1024 ];void *args, *augmented_args =NULL ; -int augmented_args_size, e_machine; +int augmented_args_size =0 , e_machine;size_t printed =0 ;
@@ -3095,7 +3132,8 @@ static int trace__fprintf_sys_enter(struct trace *trace, struct perf_sample *sam goto out_put; args =perf_evsel__sc_tp_ptr (args, sample); - augmented_args =syscall__augmented_args (sc, sample, &augmented_args_size, trace->raw_augmented_syscalls_args_size); + augmented_args =syscall__augmented_args (trace, sc, sample, &augmented_args_size, + trace->raw_augmented_syscalls_args_size); printed +=syscall__scnprintf_args (sc, msg,sizeof (msg), args, augmented_args, augmented_args_size, trace, thread);fprintf (trace->output,"%.*s" , (int )printed, msg); err =0 ;
@@ -4127,10 +4165,16 @@ static int trace__bpf_sys_enter_beauty_map(struct trace *trace, int e_machine, iif (trace->btf ==NULL )return -1 ; -for (i =0 , field = sc->args; field; ++i, field = field->next) { +for (i =0 , field = sc->args; field; field = field->next) { +/* Skip internal fields to keep beauty array index aligned with syscall arguments */ +if (is_internal_field (field)) +continue ; +// XXX We're only collecting pointer payloads _from_ user space -if (!sc->arg_fmt[i].from_user) +if (!sc->arg_fmt[i].from_user) { + ++i;continue ; + } struct_offset =strstr (field->type,"struct " );if (struct_offset ==NULL )
@@ -4149,8 +4193,10 @@ static int trace__bpf_sys_enter_beauty_map(struct trace *trace, int e_machine, i name[cnt] ='\0' ;/* cache struct's btf_type and type_id */ -if (syscall_arg_fmt__cache_btf_struct (&sc->arg_fmt[i], trace->btf, name)) +if (syscall_arg_fmt__cache_btf_struct (&sc->arg_fmt[i], trace->btf, name)) { + ++i;continue ; + } bt = sc->arg_fmt[i].type; beauty_array[i] = bt->size;
@@ -4176,7 +4222,9 @@ static int trace__bpf_sys_enter_beauty_map(struct trace *trace, int e_machine, istruct tep_format_field *field_tmp;/* find the size of the buffer that appears in pairs with buf */ -for (j =0 , field_tmp = sc->args; field_tmp; ++j, field_tmp = field_tmp->next) { +for (j =0 , field_tmp = sc->args; field_tmp; field_tmp = field_tmp->next) { +if (is_internal_field (field_tmp)) +continue ; if (!(field_tmp->flags & TEP_FIELD_IS_POINTER) && /* only integers */ (strstr(field_tmp->name, "count") || strstr(field_tmp->name, "siz") || /* size, bufsiz */
@@ -4186,8 +4234,10 @@ static int trace__bpf_sys_enter_beauty_map(struct trace *trace, int e_machine, i can_augment = true;break ; } + ++j; } } + ++i; }if (can_augment)
@@ -4196,6 +4246,19 @@ static int trace__bpf_sys_enter_beauty_map(struct trace *trace, int e_machine, ireturn -1 ; } +/* +* Advance to the first field that is a real syscall argument, so that callers +* walking two argument lists in step never have to reason about internal +* fields appearing in one list but not the other. +*/ +static struct tep_format_field *next_user_arg (struct tep_format_field *field) +{ +while (field &&is_internal_field (field)) + field = field->next; + +return field; +} +static struct bpf_program *trace__find_usable_bpf_prog_entry (struct trace *trace,struct syscall *sc) {
@@ -4203,7 +4266,7 @@ static struct bpf_program *trace__find_usable_bpf_prog_entry(struct trace *trace/* * We're only interested in syscalls that have a pointer: */ -for (field = sc->args; field; field = field->next) { +for (field =next_user_arg (sc->args); field; field =next_user_arg (field->next)) {if (field->flags &TEP_FIELD_IS_POINTER ) goto try_to_find_pair; }
@@ -4221,21 +4284,31 @@ static struct bpf_program *trace__find_usable_bpf_prog_entry(struct trace *trace pair->bpf_prog.sys_enter == unaugmented_prog)continue ; -for (field = sc->args, candidate_field = pair->args; - field && candidate_field; field = field->next, candidate_field = candidate_field->next) { +/* +* Both cursors only ever point at real arguments, so the loop +* ends when one of the two syscalls runs out of them, rather +* than when one happens to have trailing internal fields. +*/ + field =next_user_arg (sc->args); + candidate_field =next_user_arg (pair->args); +while (field && candidate_field) {bool is_pointer = field->flags &TEP_FIELD_IS_POINTER , candidate_is_pointer = candidate_field->flags &TEP_FIELD_IS_POINTER ;if (is_pointer) { -if (!candidate_is_pointer) { +if (!candidate_is_pointer) {// The candidate just doesn't copies our pointer arg, might copy other pointers we want. + field =next_user_arg (field->next); + candidate_field =next_user_arg (candidate_field->next);continue ; - } + } }else {if (candidate_is_pointer) {// The candidate might copy a pointer we don't have, skip it. goto next_candidate; } + field =next_user_arg (field->next); + candidate_field =next_user_arg (candidate_field->next);continue ; }
@@ -4256,6 +4329,8 @@ static struct bpf_program *trace__find_usable_bpf_prog_entry(struct trace *trace goto next_candidate; is_candidate = true; + field =next_user_arg (field->next); + candidate_field =next_user_arg (candidate_field->next); }if (!is_candidate)
@@ -4267,7 +4342,9 @@ static struct bpf_program *trace__find_usable_bpf_prog_entry(struct trace *trace* more than what is common to the two syscalls. */ if (candidate_field) { -for (candidate_field = candidate_field->next; candidate_field; candidate_field = candidate_field->next) + candidate_field =next_user_arg (candidate_field->next); +for (; candidate_field; + candidate_field =next_user_arg (candidate_field->next))if (candidate_field->flags &TEP_FIELD_IS_POINTER ) goto next_candidate; }
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v3 04/16] perf trace: Skip internal tracepoint fields in formatting and beauty map
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] perf: segfault in perf trace on OOM due to partial syscall init --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] perf: segfault in perf trace on OOM due to partial syscall init --
commit 48dd6af64889b782b28660fe526345b7e8dc75bc
Author: Ian Rogers <[email protected]>
perf trace: Skip internal tracepoint fields in formatting and beauty map
Quoted 4 lines
tools/perf/builtin-trace.c +0
-0
[ ... ]
Quoted 21 lines
tools/perf/builtin-trace.c +5
-7@@ -2407,24 +2414,25 @@ static int syscall__read_info(struct syscall *sc, struct trace *trace)return err; } -/* -* The tracepoint format contains __syscall_nr field, so it's one more -* than the actual number of syscall arguments. -*/ -if (syscall__alloc_arg_fmts (sc, sc->tp_format->format.nr_fields -1 )) -return -ENOMEM ; - sc->args = sc->tp_format->format.fields; + nr_args = sc->tp_format->format.nr_fields;/* * We need to check and discard the first variable '__syscall_nr' * or 'nr' that mean the syscall number. It is needless here. * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels. +* +* Do this before allocating, and size the array from what is left, so +* that a format without the field does not leave +* syscall_arg_fmt__init_array() walking one entry past the end.
tools/perf/builtin-trace.c static int syscall__read_info(struct syscall *sc, struct trace *trace) · line 2435
Quoted 10 lines
tools/perf/builtin-trace.c +4
-1*/ if (sc->args && (!strcmp (sc->args->name,"__syscall_nr" ) || !strcmp (sc->args->name,"nr" ))) { sc->args = sc->args->next; - --sc->nr_args; + --nr_args; } +if (syscall__alloc_arg_fmts (sc, nr_args)) +return -ENOMEM ; +
[Severity: Medium]
When syscall__alloc_arg_fmts() fails here (e.g. on OOM), syscall__read_info() returns -ENOMEM but leaves sc->args populated. Is this partial initialization safe?
On the next occurrence of the same syscall, trace__syscall_info() sees sc->name is already set (it was initialized earlier in syscall__read_info()) and successfully returns the partially initialized struct without retrying initialization.
This struct is then passed to syscall__scnprintf_args().
[ ... ]
Quoted 8 lines
tools/perf/builtin-trace.c +1
-3@@ -2642,11 +2650,17 @@ static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,if (sc->args !=NULL ) {struct tep_format_field *field; -for (field = sc->args; field; - field = field->next, ++arg.idx, bit <<=1 ) { -if (arg.mask & bit) +for (field = sc->args; field; field = field->next) {
[ ... ]
tools/perf/builtin-trace.c
Quoted 7 lines
tools/perf/builtin-trace.c +0
-0arg.fmt = &sc->arg_fmt[arg.idx]; val =syscall_arg__val (&arg, arg.idx);/* * Some syscall args need some mask, most don't and * return val untouched. */ val =syscall_arg_fmt__mask_val (&sc->arg_fmt[arg.idx], &arg, val);
[Severity: Medium]
Because sc->args is not NULL, does this code enter the loop and dereference the unallocated sc->arg_fmt array, causing a segmentation fault?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Ian Rogers
reviewerPATCH v3 5/16 perf trace: Do not set unaugmented BPF program on sys_exit map
In reply to Ian RogersIn trace__init_syscalls_bpf_prog_array_maps(), the BPF program array map for sys_exit (syscalls_sys_exit) was populated with the result of trace__bpf_prog_sys_exit_fd().
When a syscall had no specific exit augmenter, trace__find_syscall_bpf_prog() fell back to unaugmented_prog (syscall_unaugmented). However, syscall_unaugmented is a sys_enter program that outputs enter arguments to __augmented_syscalls__.
As a consequence, when an unaugmented syscall exited, sys_exit tail-called syscall_unaugmented, which interpreted the exit arguments as enter arguments and emitted a duplicate, corrupt sys_enter event into __augmented_syscalls__ right as the syscall completed.
Fix this by:
1. Returning NULL from trace__find_syscall_bpf_prog() when looking up exit augmenters and none is found.
2. Returning -1 from trace__bpf_prog_sys_exit_fd() when no exit program is present.
3. Only updating map_exit_fd when prog_fd >= 0.
4. Clearing err = 0 when trace__bpf_sys_enter_beauty_map() returns non-zero (indicating the syscall has no augmentable pointer arguments) before continuing the loop, so a trailing run of such syscalls (e.g. 'perf trace -e close') does not leave err non-zero on return and abort the session.
--- tools/perf/builtin-trace.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-)
tools/perf/builtin-trace.c +22 -6
@@ -4123,7 +4123,12 @@ static struct bpf_program *trace__find_syscall_bpf_prog(struct trace *trace __mapr_debug ("Couldn't find BPF prog \"%s\" to associate with syscalls:sys_%s_%s, not augmenting it\n" , prog_name, type, sc->name); out_unaugmented: -return unaugmented_prog; +/* +* Do not set unaugmented_prog for exit: syscall_unaugmented is a +* sys_enter program that outputs enter arguments. Exit without a +* specialized return augmenter returns 1 directly from sys_exit. +*/ +return !strcmp (type,"exit" ) ?NULL : unaugmented_prog; }static void trace__init_syscall_bpf_progs (struct trace *trace,int e_machine,int id)
@@ -4146,7 +4151,7 @@ static int trace__bpf_prog_sys_enter_fd(struct trace *trace, int e_machine, intstatic int trace__bpf_prog_sys_exit_fd (struct trace *trace,int e_machine,int id) {struct syscall *sc =trace__syscall_info (trace,NULL , e_machine, id); -return sc ?bpf_program__fd (sc->bpf_prog.sys_exit) :bpf_program__fd (unaugmented_prog); +return sc && sc->bpf_prog.sys_exit ?bpf_program__fd (sc->bpf_prog.sys_exit) :-1 ; }static int trace__bpf_sys_enter_beauty_map (struct trace *trace,int e_machine,int key,unsigned int *beauty_array)
@@ -4401,16 +4406,27 @@ static int trace__init_syscalls_bpf_prog_array_maps(struct trace *trace, int e_m err =bpf_map_update_elem (map_enter_fd, &key, &prog_fd,BPF_ANY );if (err)break ; +/* Only update the exit prog array map if an exit augmenter exists */ prog_fd =trace__bpf_prog_sys_exit_fd (trace, e_machine, key); - err =bpf_map_update_elem (map_exit_fd, &key, &prog_fd,BPF_ANY ); -if (err) -break ; +if (prog_fd >=0 ) { + err =bpf_map_update_elem (map_exit_fd, &key, &prog_fd,BPF_ANY ); +if (err) +break ; + }/* use beauty_map to tell BPF how many bytes to collect, set beauty_map's value here */ memset (beauty_array,0 ,sizeof (beauty_array)); err =trace__bpf_sys_enter_beauty_map (trace, e_machine, key, (unsigned int *)beauty_array); -if (err) +if (err) { +/* +* Not a failure: the syscall just has no augmentable +* arguments. Clear err, or a trailing run of such +* syscalls, e.g. all of them for 'perf trace -e close', +* would leave it set on return and abort the session. +*/ + err =0 ;continue ; + } err =bpf_map_update_elem (beauty_map_fd, &key, beauty_array,BPF_ANY );if (err)break ;
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v3 05/16] perf trace: Do not set unaugmented BPF program on sys_exit map
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 26 lines
In
trace__init_syscalls_bpf_prog_array_maps(), the BPF program array map forsys_exit(syscalls_sys_exit) was populated with the result oftrace__bpf_prog_sys_exit_fd().When a syscall had no specific exit augmenter,
trace__find_syscall_bpf_prog()fell back tounaugmented_prog(syscall_unaugmented). However,syscall_unaugmentedis asys_enterprogram that outputs enter arguments to__augmented_syscalls__.As a consequence, when an unaugmented syscall exited,
sys_exittail-calledsyscall_unaugmented, which interpreted the exit arguments as enter arguments and emitted a duplicate, corruptsys_enterevent into__augmented_syscalls__right as the syscall completed.Fix this by:
1. Returning NULL fromtrace__find_syscall_bpf_prog()when looking up exit augmenters and none is found.
2. Returning -1 fromtrace__bpf_prog_sys_exit_fd()when no exit program is present.
3. Only updatingmap_exit_fdwhenprog_fd>= 0.
4. Clearing err = 0 whentrace__bpf_sys_enter_beauty_map()returns non-zero (indicating the syscall has no augmentable pointer arguments) before continuing the loop, so a trailing run of such syscalls (e.g. 'perf trace -e close') does not leave err non-zero on return and abort the session.
Quoted 2 lines
Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Ian Rogers
reviewerPATCH v3 6/16 perf trace: Filter events in BPF and avoid tracepoint vetoes
In reply to Ian RogersThe BPF augmented_raw_syscalls sys_enter and sys_exit programs returned 0 for syscalls that were not of interest. Returning 0 from a tracepoint BPF program vetoes the event for the whole system, so an unrelated concurrent perf trace, perf record or ftrace session listening to raw_syscalls would silently lose events. This is a cross-session side effect and shows up as flaky failures when perf tests run in parallel.
Furthermore, syscall_unaugmented previously returned 1 without writing anything to the __augmented_syscalls__ ring buffer. This forced userspace perf trace to listen to both raw_syscalls:sys_enter and __augmented_syscalls__ in its evlist, requiring userspace event deduplication.
Address these issues:
1. In augmented_raw_syscalls.bpf.c, never return 0 from tracepoint
handlers: return 1 so non-traced syscalls pass through without vetoing other concurrent listeners.
2. Introduce pids_to_trace and syscalls_to_trace BPF hash maps to perform targeted filtering directly in BPF. Unselected syscalls or PIDs return 1 immediately without writing to the buffer.
3. In syscall_unaugmented, output the unaugmented enter payload into __augmented_syscalls__ and return 1. Change its section from SEC("tp/raw_syscalls/sys_enter") to SEC("tp/syscalls/sys_enter_unaugmented") so libbpf does not attempt to auto-attach it to raw_syscalls:sys_enter.
4. In bpf_trace_augment.c, add helpers to configure target PIDs and syscalls in the BPF maps, setting the activation flags (has_pids_to_trace, has_syscalls_to_trace) only after the maps are fully populated so already-attached BPF programs do not filter against a half-filled map. Explicitly attach only sys_enter and sys_exit via an attach_prog() helper that saves -errno before calling pr_debug() or bpf_link__destroy().
Destroy the skeleton on every failure path. Leaving a loaded but unusable skeleton behind is not inert: the setters called later from trace__run() would program its maps, and a partial attach would leave a BPF program live on raw_syscalls for a session that never starts. Since augmented_syscalls__{prepare,create_bpf_output}() failures fall back to unaugmented tracing rather than aborting, those setters have to become no-ops, which they only do once skel is NULL again.
errno is used directly here, so include <errno.h> rather than relying on it arriving via another header, which it does not under musl.
5. In builtin-trace.c, hook trace__set_ev_qualifier_filter() and PID filtering into the BPF maps. When __augmented_syscalls__ is active, remove raw_syscalls:sys_enter from trace.evlist since all traced enter events (both augmented and unaugmented) are now emitted by BPF into __augmented_syscalls__. Identify that evsel by comparing against trace.syscalls.events.sys_enter rather than by a strstr() of its name. The substring "syscalls:sys_enter" also matches the per syscall syscalls:sys_enter_SYSCALL tracepoints, which a user can ask for by name, and now that the match decides what is taken out of the evlist, claiming one of those would drop an event that was asked for and would describe __augmented_syscalls__ with its format rather than the raw one's. Restore tracking on the remaining evsel via evlist__set_tracking_event() so PERF_RECORD_COMM and tracking events continue to be recorded. Errors from augmented_syscalls__set_target_syscalls() are reported and propagated, the tracepoint filter string is freed on every exit path, and an allocation failure in trace__set_filter_pids() now returns -ENOMEM instead of being silently ignored.
Note that in trace__set_filter_pids() the target pids and the filtered pids are two independent axes and both have to be programmed. Naming pids to leave out with --filter-pids does not widen -p/-t or a workload to the whole system, and a BPF tracepoint program is attached system wide rather than to the target's file descriptors, so pids_to_trace is the only thing keeping other tasks out.
6. Add --syscall-augment option (defaulting to true) to allow users to explicitly use --no-syscall-augment to run perf trace in the classic unaugmented tracepoint mode without BPF. When BPF is unavailable or disabled, ensure the non-augmented tracepoint path cleanly configures sys_enter and sys_exit without duplicate entries.
Report a failure to program the pid filters with the error that caused it. trace__run() sent everything trace__set_filter_pids() returned to out_error_mem, which prints "Not enough memory to run!". That was already a guess, and becomes a wrong one now that the function also writes BPF maps: the update fails with -E2BIG when the target has more threads than pids_to_trace has room for, which has nothing to do with memory and is not something the user can act on from that message.
Report a failure to program the syscall filters the same way. The pr_err() in trace__set_ev_qualifier_filter() ran before trace__run() reached out_errno and printed "%m", and anything called in between could have changed errno by then, so the two did not have to agree. The inner report becomes a pr_debug() and the caller prints the error it was given, which leaves out_errno without a user and it is removed.
--- tools/perf/Documentation/perf-trace.txt | 5 + tools/perf/builtin-trace.c | 196 +++++++++++++++--- .../bpf_skel/augmented_raw_syscalls.bpf.c | 133 ++++++++++-- tools/perf/util/bpf_trace_augment.c | 135 +++++++++++- tools/perf/util/trace_augment.h | 34 +++ 5 files changed, 457 insertions(+), 46 deletions(-)
tools/perf/Documentation/perf-trace.txt +5 -0
@@ -260,6 +260,11 @@ the thread executes on the designated CPUs. Default is to monitor all CPUs. Maximum number of lines in the summary mode. Note that this applies to each entry (thread or cgroup). +--syscall-augment:: + Augment syscalls with BPF. Enabled by default when BPF support is available. + Use --no-syscall-augment to disable BPF augmentation and fall back to the + unaugmented tracepoint approach. + PAGEFAULTS ----------
tools/perf/builtin-trace.c +166 -30
327 lines not on this page · raw message
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +118 -15
201 lines not on this page · raw message
tools/perf/util/bpf_trace_augment.c +134 -1
166 lines not on this page · raw message
tools/perf/util/trace_augment.h +34 -0
@@ -2,6 +2,7 @@#define TRACE_AUGMENT_H #include <linux/compiler.h> +#include <stdbool.h> #include <sys/types.h> struct bpf_program ;
@@ -13,6 +14,11 @@ int augmented_syscalls__prepare(void);int augmented_syscalls__create_bpf_output (struct evlist *evlist);void augmented_syscalls__setup_bpf_output (void );int augmented_syscalls__set_filter_pids (unsigned int nr,pid_t *pids); +int augmented_syscalls__set_target_pids (unsigned int nr,pid_t *pids); +int augmented_syscalls__add_target_pid (pid_t pid); +int augmented_syscalls__del_target_pid (pid_t pid); +bool augmented_syscalls__has_target_pid (pid_t pid); +int augmented_syscalls__set_target_syscalls (unsigned int nr,int *syscall_ids,bool not_syscalls);int augmented_syscalls__get_map_fds (int *enter_fd,int *exit_fd,int *beauty_fd);struct bpf_program *augmented_syscalls__find_by_title (const char *name);struct bpf_program *augmented_syscalls__unaugmented (void );
@@ -40,6 +46,34 @@ static inline int augmented_syscalls__set_filter_pids(unsigned int nr __maybe_unreturn 0 ; } +static inline int augmented_syscalls__set_target_pids (unsigned int nr__maybe_unused , +pid_t *pids__maybe_unused ) +{ +return 0 ; +} + +static inline int augmented_syscalls__add_target_pid (pid_t pid__maybe_unused ) +{ +return 0 ; +} + +static inline int augmented_syscalls__del_target_pid (pid_t pid__maybe_unused ) +{ +return 0 ; +} + +static inline bool augmented_syscalls__has_target_pid (pid_t pid__maybe_unused ) +{ +return false; +} + +static inline int augmented_syscalls__set_target_syscalls (unsigned int nr__maybe_unused , +int *syscall_ids__maybe_unused , +bool not_syscalls__maybe_unused ) +{ +return 0 ; +} +static inline int augmented_syscalls__get_map_fds (int *enter_fd__maybe_unused ,int *exit_fd__maybe_unused ,int *beauty_fd__maybe_unused )
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v3 06/16] perf trace: Filter events in BPF and avoid tracepoint vetoes
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
The BPF
augmented_raw_syscallssys_enterandsys_exitprograms returned 0 for syscalls that were not of interest. Returning 0 from a tracepoint BPF program vetoes the event for the whole system, so an unrelated concurrent perf trace, perf record or ftrace session listening toraw_syscallswould silently lose events. This is a cross-session side effect and shows up as flaky failures when perf tests run in parallel.Furthermore,
syscall_unaugmentedpreviously returned 1 without writing anything to the__augmented_syscalls__ring buffer. This forced userspace perf trace to listen to both raw_syscalls:sys_enter and__augmented_syscalls__in its evlist, requiring userspace event deduplication.
Quoted 8 lines
Address these issues:
1. Inaugmented_raw_syscalls.bpf.c, never return 0 from tracepoint [ ... ]
The inner report becomes apr_debug()and the caller prints the error it was given, which leavesout_errnowithout a user and it is removed.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Ian Rogers
reviewerPATCH v3 7/16 perf trace: Handle fork and exit directly in BPF filter maps
In reply to Ian RogersUpdating target or filtered PIDs in userspace upon processing PERF_RECORD_FORK and PERF_RECORD_EXIT events introduces latency between event occurrence and userspace BPF map updates. If a newly forked child executes system calls before userspace processes PERF_RECORD_FORK, those syscalls may be dropped by BPF PID filtering. Conversely, if userspace evicts PIDs asynchronously on PERF_RECORD_EXIT, the kernel may recycle a PID before userspace processes the exit event, causing the late eviction to silently drop a newly created task that received the recycled PID.
Address this by attaching BTF-typed raw tracepoint BPF programs directly to the scheduler task lifetime tracepoints:
1. Attach SEC("tp_btf/sched_process_fork") (sched_process_fork), which runs in copy_process() in the parent's context before wake_up_new_task() wakes the child. Using tp_btf rather than SEC("tp/sched/sched_process_fork") receives the stable TP_PROTO arguments (struct task_struct *parent, struct task_struct *child) rather than the tracepoint ring-buffer record (TP_STRUCT__entry), whose layout changed in Linux 6.16 when parent_comm and child_comm were converted from 16-byte arrays to 4-byte __data_loc strings. When inherit is enabled and the pid of the thread that called clone() is in pids_to_trace or pids_filtered, insert child->pid into the corresponding map immediately. Because child->pid is task_struct.pid (the global initial-namespace PID), this works accurately across PID namespaces without aliasing host PIDs, and covers both new processes and CLONE_THREAD threads without needing real_parent CO-RE walks or syscall-return heuristics.
2. Attach SEC("tp_btf/sched_process_exit") (sched_process_exit), which runs in do_exit() for every task in its own context, including tasks killed by signals (SIGKILL, SIGSEGV, etc.) and secondary threads torn down implicitly by exit_group. Delete the dying task's PID from pids_to_trace and pids_filtered immediately in kernel space, eliminating both map leaks and any asynchronous userspace eviction window where PID recycling could occur.
3. Attach SEC("tp_btf/sched_process_exec") (sched_process_exec) to follow the one case where a live task's pid changes underneath the maps. When a thread that is not the group leader execs, de_thread() kills the leader and hands the leader's pid, which is the tgid, to the exec'ing thread. The leader dies first, so sched_process_exit() has already dropped exactly the pid the survivor now holds, and the survivor's old entry would be stranded in the map for good. Move the entry from old_pid to p->pid. old_pid is sampled in bprm_execve() before de_thread() runs, so the ordinary group leader exec is a no-op here. Drop the old key before inserting the new one:
the move is only a rename, but holding both keys at once needs a spare slot, and on a full map the insert would fail with -E2BIG while the delete still succeeded, losing the task instead of moving it.
4. With every live task registered before its first syscall and evicted in do_exit(), simplify pid_to_trace__has() and pid_filter__has() to single BPF hash map lookups, and move bpf_probe_read_kernel() in sys_exit back after the PID filter checks.
5. Pass the inherit flag from userspace to BPF .rodata via augmented_syscalls__prepare(!trace.opts.no_inherit), and split attaching out of it into augmented_syscalls__attach(), called from trace__run() once the pid, syscall and program array maps have all been programmed. These are system wide programs, so from the instant they attach they alone decide what is traced: attaching at load time, as before, left a window in which a target could fork without sched_process_fork() knowing the parent was a target, and with the userspace fork handling gone there was nothing left to recover it. The scheduler programs are attached ahead of sys_enter and sys_exit for the same reason. Set has_pids_filtered only after populating pids_filtered. A failure to attach ends the session, reporting the error that caused it. There is no falling back to unaugmented tracing by that point: cmd_trace() built the evlist around __augmented_syscalls__ and dropped the sys_enter evsel, so a session that carried on would report nothing at all. Neither did the code this replaces, which ignored the result of augmented_raw_syscalls_bpf__attach() altogether and ran on with programs that had never been attached.
6. Remove the userspace BPF map updates from PERF_RECORD_FORK and PERF_RECORD_EXIT in trace__process_event(), and delete the now-unused augmented_syscalls__{add,del,has}_target_pid() helpers. No coverage is lost with them: those records only come into being once the ring buffers are mapped by evlist__do_mmap() and the events are switched on by evlist__enable(), both of which run after augmented_syscalls__attach() in trace__run(), and they are then acted on later still, whenever the poll loop gets round to them. The scheduler programs therefore go live strictly earlier than the userspace path could ever have reacted.
7. Gate pid_filter__has() on a has_pids_filtered flag in .bss so the common case without --filter-pids performs no map lookups, and size pids_to_trace and pids_filtered at 16384 entries. pids_filtered is grown from 64 because it is no longer just the handful of pids userspace names: sched_process_fork() adds every descendant of those, so a --filter-pids target that forks or is heavily threaded needs the same headroom as a traced one.
A fork or exit is still not seen if it happens before the programs are attached, that is between evlist__create_maps() scanning /proc for a -p target and augmented_syscalls__attach(). Such a window is inherent in programming a system wide filter before switching it on, and as above the userspace handling did not cover it either. The cost is not small though. Once the bpf-output event exists cmd_trace() removes the sys_enter evsel from the evlist, so __augmented_syscalls__ is the only source of enter events and a pid that is missing from pids_to_trace is not reported at all. sys_enter returning 1 keeps the kernel tracepoint alive for other subscribers, it does not give perf trace a second path to the event. A target that forks during perf trace's own startup can therefore have that child, and in turn everything the child forks, go untraced for the whole run.
The tgid fallback that pid_to_trace__has() used to have would have masked part of this, since a thread missed in the window still shares the tgid of a target userspace did insert. It is not kept because it would also re-admit tasks that sched_process_fork() deliberately
skipped: under --no-inherit a new thread of the target is not added to the map, yet it shares the target's tgid and a tgid test cannot tell it apart from one that was.
sched_process_fork() does not consult the parent's tgid either, for the same reason. Every thread of a -p target is enumerated from /proc/<pid>/task and inserted under its own pid, and -t names a single thread, so a tgid test adds no reach. What it would add is a child inheriting from a thread that is not traced itself: a sibling of the thread -t selected, or one missed in the attach window whose own syscalls go unreported. Inheritance keys off the pid of the thread that called clone(), exactly as the lookups do.
Two of the three ways of selecting what to trace are unaffected:
'perf trace -- cmd' cannot hit this because evlist__prepare_workload() leaves the child blocked on a pipe until evlist__start_workload(), well after the attach, and 'perf trace -a' never sets has_pids_to_trace so it does not filter at all. It is 'perf trace -p' against an already running target that is exposed. Closing that too needs the descendants of the target re-enumerated from /proc after the attach, which the next change does.
--- tools/perf/builtin-trace.c | 55 +++-- .../bpf_skel/augmented_raw_syscalls.bpf.c | 199 +++++++++++++++++- tools/perf/util/bpf_trace_augment.c | 116 ++++++---- tools/perf/util/trace_augment.h | 28 +-- 4 files changed, 299 insertions(+), 99 deletions(-)
tools/perf/builtin-trace.c +32 -23
@@ -2061,23 +2061,6 @@ static int trace__process_event(struct trace *trace, struct machine *machine, "LOST %" PRIu64 " events!\n", (u64)event->lost.lost); ret =machine__process_lost_event (machine, event, sample);break ; -case PERF_RECORD_FORK : -if (trace->raw_augmented_syscalls && - (augmented_syscalls__has_target_pid (event->fork.ppid) || -augmented_syscalls__has_target_pid (event->fork.ptid))) { -augmented_syscalls__add_target_pid (event->fork.pid); - } - ret =machine__process_fork_event (machine, event, sample); -break ; -case PERF_RECORD_EXIT : -if (trace->raw_augmented_syscalls) { -if (event->fork.pid == event->fork.tid) -augmented_syscalls__del_target_pid (event->fork.pid); -else -augmented_syscalls__del_target_pid (event->fork.tid); - } - ret =machine__process_exit_event (machine, event, sample); -break ;default : ret =machine__process_event (machine, event, sample);break ;
@@ -4993,6 +4976,22 @@ static int trace__run(struct trace *trace, int argc, const char **argv) } } +/* +* Everything the BPF programs filter on is now in their maps, so it is +* safe to let them run. They are attached system wide, so anything +* before this point would have been filtered against a map that was +* still being built up. +* +* Falling back to unaugmented tracing is no longer possible here: the +* evlist was built around __augmented_syscalls__ back in cmd_trace(), +* which is where that decision is taken and where the sys_enter evsel +* was dropped. Fail the session rather than run one that can report +* nothing. +*/ + err =augmented_syscalls__attach (); +if (err <0 ) + goto out_error_attach; +/* * If the "close" syscall is not traced, then we will not have the * opportunity to, in syscall_arg__scnprintf_close_fd() invalidate the
@@ -5187,6 +5186,16 @@ static int trace__run(struct trace *trace, int argc, const char **argv)fprintf (trace->output,"Failed to set the syscall filters: %s\n" ,str_error_r (-err, errbuf,sizeof (errbuf))); goto out_put_evlist; + +out_error_attach: +/* +* Use the returned error rather than errno: the failing attach is +* unwound before returning, and the libbpf calls that does can leave +* errno describing something else entirely. +*/ +fprintf (trace->output,"Failed to attach the augmented syscalls BPF programs: %s\n" , +str_error_r (-err, errbuf,sizeof (errbuf))); + goto out_put_evlist; } out_error_mem:fprintf (trace->output,"Not enough memory to run!\n" );
@@ -6103,7 +6112,7 @@ int cmd_trace(int argc, const char **argv) goto skip_augmentation; } - err =augmented_syscalls__prepare (); + err =augmented_syscalls__prepare (!trace.opts.no_inherit);if (err <0 ) goto skip_augmentation;
@@ -6114,11 +6123,11 @@ int cmd_trace(int argc, const char **argv) trace.syscalls.events.bpf_output =evlist__last (trace.evlist); }else {/* -* augmented_syscalls__prepare() already attached sys_enter and -* sys_exit, which are system wide. Falling through to -* skip_augmentation without undoing that would run a BPF -* program for every syscall on the machine, for the whole -* session, with nothing consuming the output. +* Drop the loaded skeleton before falling back to unaugmented +* tracing. Otherwise the setters called from trace__run() would +* still program its maps, and augmented_syscalls__attach() would +* then put system wide BPF programs on raw_syscalls for a +* session with nothing consuming their output. */ pr_debug ("Failed to create the augmented syscalls bpf-output event, disabling augmentation\n" );augmented_syscalls__cleanup ();
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +189 -10
260 lines not on this page · raw message
tools/perf/util/bpf_trace_augment.c +70 -46
170 lines not on this page · raw message
tools/perf/util/trace_augment.h +8 -20
@@ -10,14 +10,12 @@ struct evlist;#ifdef HAVE_BPF_SKEL -int augmented_syscalls__prepare (void ); +int augmented_syscalls__prepare (bool inherit); +int augmented_syscalls__attach (void );int augmented_syscalls__create_bpf_output (struct evlist *evlist);void augmented_syscalls__setup_bpf_output (void );int augmented_syscalls__set_filter_pids (unsigned int nr,pid_t *pids);int augmented_syscalls__set_target_pids (unsigned int nr,pid_t *pids); -int augmented_syscalls__add_target_pid (pid_t pid); -int augmented_syscalls__del_target_pid (pid_t pid); -bool augmented_syscalls__has_target_pid (pid_t pid);int augmented_syscalls__set_target_syscalls (unsigned int nr,int *syscall_ids,bool not_syscalls);int augmented_syscalls__get_map_fds (int *enter_fd,int *exit_fd,int *beauty_fd);struct bpf_program *augmented_syscalls__find_by_title (const char *name);
@@ -26,11 +24,16 @@ void augmented_syscalls__cleanup(void);#else /* !HAVE_BPF_SKEL */ -static inline int augmented_syscalls__prepare (void ) +static inline int augmented_syscalls__prepare (bool inherit__maybe_unused ) {return -1 ; } +static inline int augmented_syscalls__attach (void ) +{ +return 0 ; +} +static inline int augmented_syscalls__create_bpf_output (struct evlist *evlist__maybe_unused ) {return -1 ;
@@ -52,21 +55,6 @@ static inline int augmented_syscalls__set_target_pids(unsigned int nr __maybe_unreturn 0 ; } -static inline int augmented_syscalls__add_target_pid (pid_t pid__maybe_unused ) -{ -return 0 ; -} - -static inline int augmented_syscalls__del_target_pid (pid_t pid__maybe_unused ) -{ -return 0 ; -} - -static inline bool augmented_syscalls__has_target_pid (pid_t pid__maybe_unused ) -{ -return false; -} -static inline int augmented_syscalls__set_target_syscalls (unsigned int nr__maybe_unused ,int *syscall_ids__maybe_unused ,bool not_syscalls__maybe_unused )
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v3 07/16] perf trace: Handle fork and exit directly in BPF filter maps
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 19 lines
Updating target or filtered PIDs in userspace upon processing
PERF_RECORD_FORKandPERF_RECORD_EXITevents introduces latency between event occurrence and userspace BPF map updates. If a newly forked child executes system calls before userspace processesPERF_RECORD_FORK, those syscalls may be dropped by BPF PID filtering. Conversely, if userspace evicts PIDs asynchronously onPERF_RECORD_EXIT, the kernel may recycle a PID before userspace processes the exit event, causing the late eviction to silently drop a newly created task that received the recycled PID.Address this by attaching BTF-typed raw tracepoint BPF programs directly to the scheduler task lifetime tracepoints:
1. Attach
SEC("tp_btf/sched_process_fork")(sched_process_fork), which runs incopy_process()in the parent's context beforewake_up_new_task()wakes the child. Usingtp_btfrather than
[ ... ]
of the target re-enumerated from /proc after the attach, which the next change does.
Quoted 2 lines
Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Ian Rogers
reviewerPATCH v3 8/16 perf trace: Enumerate the target again once BPF is attached
In reply to Ian Rogersevlist__create_maps() reads the target out of /proc, and the BPF sched_process_fork() program only sees what is cloned once it is attached. A task the target creates between the two is in neither, and since cmd_trace() drops the sys_enter evsel in favour of __augmented_syscalls__ there is no other source of enter events. Such a task, and in turn everything it forks, goes unreported for the rest of the session.
Read the target out of /proc once more, after the attach. Whatever existed before the programs went live is there to be found, and whatever is created after it is sched_process_fork()'s to add, so between them nothing is left out. Doing this before the attach instead would only move the window rather than close it.
The enumeration follows the same rule as the BPF programs, which is that the maps name individual tasks:
- -p names a process, so its thread group is read from /proc/<pid>/task.
- -t names a thread, which is taken on its own. Expanding it to its thread group would trace the siblings it asked to leave out.
- Descendants come from task->children, read through /proc/<pid>/task/<tid>/children. A forked task leads a thread group of its own, so each one found is walked in turn and a tree of any depth is covered. New threads are not listed there, copy_process() gives a CLONE_THREAD child the real_parent of its creator rather than the creator itself, but the thread group walk above has them.
The other two ways of choosing what to trace need nothing, for the same reasons the window never affected them: 'perf trace -a' does not filter on pid at all, and evlist__prepare_workload() keeps a workload blocked on a pipe until evlist__start_workload(), well after the attach.
A target that exits during startup is not an error. Reading /proc for a task that has gone fails with ENOENT, and a task directory that is read but has nothing in it sets nothing at all, so errno is cleared before the enumeration and only an allocation failure is passed back. Anything else leaves the tasks that evlist__create_maps() already found in the map, which sched_process_exit() takes out again as they die, and the session runs on rather than being ended over a target that was going to stop producing events anyway.
What is left is smaller and no longer lasts. A task found here may have made syscalls between sys_enter going live and it being added to the map, and those are not reported, but it is traced from that point on. On a kernel built without CONFIG_PROC_CHILDREN the children files are absent and descendants cannot be named, leaving the threads of the target, which are still picked up.
pid_t, PATH_MAX, FILE and the directory reading are all used directly by the new code, so <sys/types.h>, <limits.h>, <stdio.h> and <dirent.h> are included rather than relied upon to arrive through another header.
--- tools/perf/builtin-trace.c | 233 +++++++++++++++++++++++++++++++++++++ 1 file changed, 233 insertions(+)
tools/perf/builtin-trace.c +233 -0
@@ -15,6 +15,7 @@*/ #include "util/record.h" +#include <api/fs/fs.h> #include <api/fs/tracing_path.h> #ifdef HAVE_LIBBPF_SUPPORT #include <bpf/bpf.h>
@@ -65,11 +66,15 @@#include "trace_augment.h" #include "dwarf-regs.h" +#include <dirent.h> #include <errno.h> #include <sys/stat.h> +#include <sys/types.h> #include <inttypes.h> +#include <limits.h> #include <poll.h> #include <signal.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> #include <linux/err.h>
@@ -4638,6 +4643,224 @@ static int trace__set_filter_pids(struct trace *trace)return err; } +/* A list of pids that grows as it is added to, holding each pid once. */ +struct pid_list { +pid_t *entries; +size_t nr; +size_t allocated; +}; + +static bool pid_list__has (const struct pid_list *list,pid_t pid) +{ +for (size_t i =0 ; i < list->nr; i++) { +if (list->entries[i] == pid) +return true; + } +return false; +} + +/* Append pid, unless it is already there. */ +static int pid_list__add (struct pid_list *list,pid_t pid) +{ +if (pid_list__has (list, pid)) +return 0 ; + +if (list->nr == list->allocated) { +size_t allocated = list->allocated ? list->allocated *2 :32 ; +pid_t *entries =realloc (list->entries, allocated *sizeof (*entries)); + +if (entries ==NULL ) +return -ENOMEM ; + + list->entries = entries; + list->allocated = allocated; + } + + list->entries[list->nr++] = pid; +return 0 ; +} + +static void pid_list__exit (struct pid_list *list) +{ +zfree (&list->entries); + list->nr =0 ; + list->allocated =0 ; +} + +/* +* Append the tasks tid has forked to tgids. +* +* task->children holds what a task forked, and a forked task leads a thread +* group of its own, so each is something to expand in turn. New threads are +* not listed: copy_process() gives a CLONE_THREAD child the real_parent of +* its creator rather than the creator itself, so a thread is a sibling of the +* task that created it. Those are enumerated from the task directory instead. +*/ +static int pid_list__add_children (struct pid_list *tgids,pid_t tid) +{ +char path[PATH_MAX ]; +pid_t child; +FILE *fp; +int err =0 ; + +scnprintf (path,sizeof (path),"%s/%d/task/%d/children" , +procfs__mountpoint (), tid, tid); + fp =fopen (path,"r" ); +/* +* Absent if the task exited, and on a kernel built without +* CONFIG_PROC_CHILDREN. Neither is worth failing for: what is missed +* is a task that has gone away, or descendants the kernel will not +* name. +*/ +if (fp ==NULL ) +return 0 ; + +while (fscanf (fp,"%d" , &child) ==1 ) { + err =pid_list__add (tgids, child); +if (err) +break ; + } + +fclose (fp); +return err; +} + +/* +* Collect the tasks to trace: the target, its threads, and everything they +* have forked. +* +* tgids is the queue of thread groups still to expand. It is walked as it +* grows, so a child found here has its own children picked up in a later +* pass and the depth of the tree does not matter. The walk terminates +* because a task cannot be its own ancestor and pid_list__add() ignores a +* pid that is already listed. +*/ +static int trace__collect_target_pids (struct trace *trace,struct pid_list *pids) +{ +struct target *target = &trace->opts.target; +/* +* -p names processes, so the whole thread group is a target. -t names +* threads, and expanding one to its group would trace the siblings +* that were deliberately left out. +*/ +bool whole_group = target->pid !=NULL ; +struct perf_thread_map *threads; +struct pid_list tgids = {}; +int err =0 ; + +/* Enumerate the target as evlist__create_maps() did, but now. */ + errno =0 ; + threads =thread_map__new_str (target->pid, target->tid, target->per_thread); +if (threads ==NULL ) { +char bf[128 ]; + +/* +* A target that exited while perf trace was starting up shows +* up here as a failure to read /proc/<pid>/task, with scandir() +* setting ENOENT, or as a task directory that is read but has +* nothing in it, which sets nothing at all and is why errno is +* cleared above. Neither is worth ending the session for: the +* tasks the target had are already in the map from +* evlist__create_maps() and sched_process_exit() takes them out +* again as they die. Carry on with what is known and let only +* an allocation failure through, matching how the pid_list +* additions below are treated. +*/ +if (errno ==ENOMEM ) +return -ENOMEM ; + +pr_debug ("Couldn't enumerate the target again (%s), tracing the tasks already known\n" , + errno ==0 ?"it exited" :str_error_r (errno, bf,sizeof (bf))); +return 0 ; + } + +for (int i =0 ; i <perf_thread_map__nr (threads); i++) { +pid_t pid =perf_thread_map__pid (threads, i); + + err =pid_list__add (whole_group ? &tgids : pids, pid); +/* A thread named by -t is not expanded, but its children are. */ +if (!err && !whole_group) + err =pid_list__add_children (&tgids, pid); +if (err) + goto out; + } + +for (size_t i =0 ; i < tgids.nr; i++) { +pid_t tgid = tgids.entries[i]; +char path[PATH_MAX ]; +struct dirent *dent; +DIR *tasks; + +scnprintf (path,sizeof (path),"%s/%d/task" ,procfs__mountpoint (), tgid); + tasks =opendir (path); +if (tasks ==NULL ) +continue ;/* Exited between being named and being read. */ + +while ((dent =readdir (tasks)) !=NULL ) { +char *end; +pid_t tid =strtol (dent->d_name, &end,10 ); + +/* Skip "." and "..". */ +if (*end !='\0' ) +continue ; + + err =pid_list__add (pids, tid); +if (!err) + err =pid_list__add_children (&tgids, tid); +if (err) +break ; + } + +closedir (tasks); +if (err) + goto out; + } +out: +perf_thread_map__put (threads); +pid_list__exit (&tgids); +return err; +} + +/* +* Trace the tasks that appeared while perf trace was starting up. +* +* evlist__create_maps() enumerated the target from /proc, and +* sched_process_fork() only sees what is cloned once it is attached. A task +* created in between is in neither, and since cmd_trace() drops the sys_enter +* evsel in favour of __augmented_syscalls__ it would go unreported for the +* whole session. +* +* Read the target out of /proc again now that the programs are attached. +* Whatever came before the attach is in /proc to be found, and whatever comes +* after it is sched_process_fork()'s to add, so between them nothing is left +* out. +* +* Doing this after the attach rather than before it is what makes that true; +* before it would only move the window. The cost is that a task found here +* may have made syscalls between sys_enter going live and it being added +* below, and those are not reported. It is traced from that point on. +*/ +static int trace__set_startup_pids (struct trace *trace) +{ +struct pid_list pids = {}; +int err; + +/* +* Nothing to do without a target: 'perf trace -a' does not filter on +* pid at all, and evlist__prepare_workload() keeps a workload blocked +* on a pipe until evlist__start_workload(), well after the attach. +*/ +if (!target__has_task (&trace->opts.target)) +return 0 ; + + err =trace__collect_target_pids (trace, &pids); +if (!err) + err =augmented_syscalls__set_target_pids (pids.nr, pids.entries); + +pid_list__exit (&pids); +return err; +} +static int __trace__deliver_event (struct trace *trace,union perf_event *event) {struct evlist *evlist = trace->evlist;
@@ -4992,6 +5215,16 @@ static int trace__run(struct trace *trace, int argc, const char **argv)if (err <0 ) goto out_error_attach; +/* +* With the programs live, anything the target created while they were +* being set up is now sched_process_fork()'s to keep track of, but it +* was not there to see it appear. Enumerate the target once more to +* pick those up. +*/ + err =trace__set_startup_pids (trace); +if (err <0 ) + goto out_error_filter_pids; +/* * If the "close" syscall is not traced, then we will not have the * opportunity to, in syscall_arg__scnprintf_close_fd() invalidate the
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v3 08/16] perf trace: Enumerate the target again once BPF is attached
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] perf trace: TOCTOU race in startup PID collection leaks dead PIDs in BPF map --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] perf trace: TOCTOU race in startup PID collection leaks dead PIDs in BPF map --
commit 1da31ce50a5973414bbbad29076bca00f6ff0aae
Author: Ian Rogers <[email protected]>
perf trace: Enumerate the target again once BPF is attached
This commit updates perf trace to enumerate target tasks again after BPF programs are attached. This ensures that any tasks created between the initial /proc read and the BPF attachment are discovered and not missed by the BPF sched_process_fork program.
Quoted 4 lines
tools/perf/builtin-trace.c +0
-0
[ ... ]
Quoted 1 line
tools/perf/builtin-trace.c +0
-0@@ -4638,6 +4643,224 @@ static int trace__set_filter_pids(struct trace *trace)
[ ... ]
Quoted 6 lines
tools/perf/builtin-trace.c +6
-0+static int trace__set_startup_pids (struct trace *trace) +{ +struct pid_list pids = {}; +int err; + +/*
tools/perf/builtin-trace.c static int trace__set_filter_pids(struct trace *trace) · line 4858
Quoted 10 lines
tools/perf/builtin-trace.c +10
-0+* Nothing to do without a target: 'perf trace -a' does not filter on +* pid at all, and evlist__prepare_workload() keeps a workload blocked +* on a pipe until evlist__start_workload(), well after the attach. +*/ +if (!target__has_task (&trace->opts.target)) +return 0 ; + + err =trace__collect_target_pids (trace, &pids); +if (!err) + err =augmented_syscalls__set_target_pids (pids.nr, pids.entries);
[Severity: Medium]
Could this sequence result in dead PIDs leaking into the pids_to_trace BPF map?
If a newly created task is discovered by trace__collect_target_pids() reading /proc, and that task exits immediately after being read, the BPF sched_process_exit hook will fire. However, the hook cannot evict the task because it is not yet in the map.
Next, user-space completes trace__collect_target_pids() and calls augmented_syscalls__set_target_pids(), which unconditionally inserts the now-dead PID into the BPF map. Since the task is already dead, the sched_process_exit hook will never fire for it again.
Since the map has a fixed limit, could repeated leaks from quickly exiting tasks exhaust the map capacity and prevent new threads from being traced? Also, if a leaked PID is reassigned by the OS to an unrelated task, might perf trace incorrectly trace that unrelated task instead?
Quoted 4 lines
tools/perf/builtin-trace.c +4
-0+ +pid_list__exit (&pids); +return err; +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Ian Rogers
reviewerPATCH v3 9/16 perf test test_task_analyzer: Isolate in temporary directory and make non-exclusive
In reply to Ian Rogerstest_task_analyzer.sh writes perf.data and temporary files directly into the current working directory, causing collisions when running tests in parallel.
As a temporary measure until perf script report supports an input file option, resolve perfdir to an absolute path, change directory into $tmpdir for the test duration, and clean up in the exit trap. Remove the (exclusive) tag so the test runs in parallel.
perfdir is derived from $0, which may be relative, so it has to be resolved before the cd into $tmpdir, otherwise both PERF_EXEC_PATH and the cleanup trap point at paths that no longer exist.
--- tools/perf/tests/shell/test_task_analyzer.sh | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)
tools/perf/tests/shell/test_task_analyzer.sh +15 -5
@@ -1,8 +1,18 @@ #!/bin/bash -# perf script task-analyzer tests (exclusive) +# perf script task-analyzer tests # SPDX-License-Identifier: GPL-2.0 -tmpdir=$(mktemp -d /tmp/perf-script-task-analyzer-XXXXX) +# Resolve the source directory before changing the working directory below, +# $0 may be a relative path and would no longer resolve from $tmpdir. +perfdir=$(cd "$(dirname "$0")/../.." && pwd) + +tmpdir=$(mktemp -d /tmp/perf-script-task-analyzer-XXXXX) || exit 1 +# The cleanup trap is only installed further down, once the functions it +# calls have been defined, so tidy up by hand if this cd fails. +cd "$tmpdir" || { + rmdir "$tmpdir" + exit 1 +} # TODO: perf script report only supports input from the CWD perf.data file, make # it support input from any file. perfdata="perf.data"
@@ -11,7 +21,6 @@ csvsummary="$tmpdir/csvsummary" err=0 # set PERF_EXEC_PATH to find scripts in the source directory -perfdir=$(dirname "$0")/../.. if [ -e "$perfdir/scripts/python/Perf-Trace-Util" ]; then export PERF_EXEC_PATH=$perfdir fi
@@ -20,8 +29,9 @@ fi export ASAN_OPTIONS=detect_leaks=0 cleanup() { - rm -f "${perfdata}" - rm -f "${perfdata}".old + # Step out of $tmpdir before removing it. The removal uses an absolute + # path and works from anywhere, so a failure to cd must not skip it. + cd "$perfdir" || cd / || true rm -rf "$tmpdir" trap - exit term int
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v3 09/16] perf test test_task_analyzer: Isolate in temporary directory and make non-exclusive
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
test_task_analyzer.shwrites perf.data and temporary files directly into the current working directory, causing collisions when running tests in parallel.As a temporary measure until
perf script reportsupports an input file option, resolve perfdir to an absolute path, change directory into $tmpdir for the test duration, and clean up in the exit trap. Remove the (exclusive) tag so the test runs in parallel.
Quoted 6 lines
perfdir is derived from $0, which may be relative, so it has to be resolved before the cd into $tmpdir, otherwise both
PERF_EXEC_PATHand the cleanup trap point at paths that no longer exist.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
Ian Rogers
reviewerPATCH v3 10/16 perf test common: Only disable probes in clear_all_probes
In reply to Ian Rogersclear_all_probes() began by writing 0 to /sys/kernel/debug/tracing/events/enable, which disables every tracepoint on the system rather than just the probes the probe tests created. When tests run in parallel that also silences the events of any concurrent perf record, perf trace or ftrace session, so unrelated tests lose the events they are waiting for and fail.
The write cannot simply be dropped. Clearing kprobe_events or uprobe_events is all or nothing: dyn_events_release_all() walks every probe of that type first and returns -EBUSY without removing any of them if it finds one that is still enabled, where enabled means TP_FLAG_TRACE from tracefs or TP_FLAG_PROFILE from a perf session. A probe left enabled through tracefs would therefore block the whole clear and leave stale probes behind to collide with the next run.
Disable the probes and only the probes. The set to disable is taken from the kprobe_events and uprobe_events listings rather than from a guess at which groups perf uses, so it matches what dyn_events_release_all() is going to inspect: a probe an unrelated session left enabled in, say, the kprobes group would otherwise still abort the clear.
--- tools/perf/tests/shell/common/init.sh | 33 ++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-)
tools/perf/tests/shell/common/init.sh +32 -1
@@ -130,9 +130,40 @@ check_uprobes_available() test -e /sys/kernel/debug/tracing/uprobe_events } +# Disable every kprobe and uprobe event. The listings name each probe as +# "TYPE:GROUP/EVENT ARGS...", for instance "p:probe/vfs_read vfs_read", and +# events/GROUP/EVENT/enable is the switch for it. +disable_all_probes() +{ + PROBE_SPECS=`cat /sys/kernel/debug/tracing/kprobe_events \ + /sys/kernel/debug/tracing/uprobe_events 2> /dev/null | + cut -d ' ' -f 1` + for PROBE_SPEC in $PROBE_SPECS + do + case "$PROBE_SPEC" in + *:*/*) ;; + *) continue ;; + esac + PROBE_ENABLE="/sys/kernel/debug/tracing/events/${PROBE_SPEC#*:}/enable" + test -e "$PROBE_ENABLE" && echo 0 > "$PROBE_ENABLE" + done +} + clear_all_probes() { - echo 0 > /sys/kernel/debug/tracing/events/enable + # Disable the probes before removing them. Writing to kprobe_events or + # uprobe_events is all or nothing: dyn_events_release_all() walks every + # probe of that type first and returns -EBUSY without removing any of + # them if it finds one that is still enabled, which would leave stale + # probes behind to collide with the next run. That covers probes this + # test suite never created, so disable all of them and not just the + # ones in the groups perf uses. + # + # Only probes are disabled. Writing to events/enable would also silence + # the tracepoints of any perf record, perf trace or ftrace session + # sharing the machine, which breaks those tests when they run in + # parallel with this one. + disable_all_probes check_kprobes_available && echo > /sys/kernel/debug/tracing/kprobe_events check_uprobes_available && echo > /sys/kernel/debug/tracing/uprobe_events }
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v3 10/16] perf test common: Only disable probes in clear_all_probes
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines
clear_all_probes()began by writing 0 to/sys/kernel/debug/tracing/events/enable, which disables every tracepoint on the system rather than just the probes the probe tests created. When tests run in parallel that also silences the events of any concurrent perf record, perf trace or ftrace session, so unrelated tests lose the events they are waiting for and fail.The write cannot simply be dropped. Clearing
kprobe_eventsoruprobe_eventsis all or nothing:dyn_events_release_all()walks every probe of that type first and returns-EBUSYwithout removing any of them if it finds one that is still enabled, where enabled meansTP_FLAG_TRACEfrom tracefs orTP_FLAG_PROFILEfrom a perf session. A probe left enabled through tracefs would therefore block the whole clear and leave stale probes behind to collide with the next run.
Quoted 8 lines
Disable the probes and only the probes. The set to disable is taken from the
kprobe_eventsanduprobe_eventslistings rather than from a guess at which groups perf uses, so it matches whatdyn_events_release_all()is going to inspect: a probe an unrelated session left enabled in, say, the kprobes group would otherwise still abort the clear.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Ian Rogers
reviewerPATCH v3 11/16 perf test probe_vfs_getname: Scope probe name to PID and make non-exclusive
In reply to Ian RogersThe probe name vfs_getname was hardcoded, causing collisions when tests ran concurrently. Furthermore, cleanup_probe_vfs_getname() used perf probe -d probe:vfs_getname*, deleting probes registered by other parallel tests.
Scope the probe name to the pid, and rename it to getname_flags_$$ so that it no longer begins with "vfs_getname". perf trace calls evlist__add_vfs_getname(), which opens every event matching a hardcoded "probe:vfs_getname*" wildcard, so a perf trace run by any other test would otherwise pin this probe and make perf probe -d fail with -EBUSY. That also unblocks making the perf trace tests non-exclusive later in this series.
Enumerate the probes to record and to delete from perf probe -l, matching ^probe:${vfs_getname}(_[[:digit:]]+)?$ exactly, rather than globbing on ${vfs_getname}*. perf probe appends _1, _2, ... when getname_flags is inlined at more than one call site, so the variants do have to be matched, but since the name now ends in a pid a trailing wildcard would also match the probes of a test whose pid merely starts with this one's, e.g. 123 and 1234.
Remove the (exclusive) tag from probe_vfs_getname.sh and record+script_probe_vfs_getname.sh so they run concurrently in pass 1. trace+probe_vfs_getname.sh has to stay exclusive: it is the one test that wants to be discovered by that wildcard, so it sets vfs_getname to a "vfs_getname_$$" name before sourcing the library, and would then pin its siblings' probes if it ran alongside them. A comment in the test records this.
Delete the probes from an exit trap as well as on the way out. A pid scoped name is never seen again, so a run interrupted before cleanup_probe_vfs_getname() would leave its probes behind for good, accumulating a set per run. The fixed name at least meant the next run found, and went on to reuse, whatever the last one left.
--- .../perf/tests/shell/lib/probe_vfs_getname.sh | 51 +++++++++++++++++-- tools/perf/tests/shell/probe_vfs_getname.sh | 3 +- .../shell/record+script_probe_vfs_getname.sh | 18 +++++-- .../tests/shell/trace+probe_vfs_getname.sh | 9 ++++ 4 files changed, 71 insertions(+), 10 deletions(-)
tools/perf/tests/shell/lib/probe_vfs_getname.sh +47 -4
@@ -1,15 +1,58 @@ #!/bin/bash # Arnaldo Carvalho de Melo <[email protected]>, 2017 -perf probe -l 2>&1 | grep -q probe:vfs_getname +# The name of the getname_flags probe added and removed below. +# +# It is scoped to the pid so that tests running in parallel do not collide, +# and it deliberately does not start with "vfs_getname": perf trace calls +# evlist__add_vfs_getname(), which opens everything matching the hardcoded +# "probe:vfs_getname*" wildcard, so a perf trace running in another test would +# otherwise pin this probe and make the 'perf probe -d' below fail with -EBUSY. +# +# trace+probe_vfs_getname.sh is the one test that does want to be found that +# way, so it sets vfs_getname itself before sourcing this file, and is +# (exclusive) as a result. +: "${vfs_getname:=getname_flags_$$}" + +# Print the probes add_probe_vfs_getname() created. perf probe appends _1, _2, +# ... when getname_flags is inlined at more than one call site, so there can be +# several. Match them exactly rather than with a "${vfs_getname}*" glob: the +# name ends in a pid, so such a glob would also match the probes of a test +# whose pid merely starts with this one's, e.g. 123 and 1234. +probes_vfs_getname() { + perf probe -l 2>/dev/null | awk '{print $1}' | + grep -E "^probe:${vfs_getname}(_[[:digit:]]+)?$" +} + +[ -n "$(probes_vfs_getname)" ] had_vfs_getname=$? cleanup_probe_vfs_getname() { if [ $had_vfs_getname -eq 1 ] ; then - perf probe -q -d probe:vfs_getname* + local probe + for probe in $(probes_vfs_getname); do + perf probe -q -d "$probe" + done fi } +# Delete the probes however the test ends, not just when it runs to +# completion. The name is scoped to the pid, so nothing that runs later +# reuses or tidies up a probe an interrupted test left behind, and they +# would accumulate one set per run. The fixed name used before was at least +# picked up again by the next run. +# +# Tests may still call cleanup_probe_vfs_getname directly. Doing so leaves +# nothing for probes_vfs_getname to find, so the trap below then does +# nothing. A test needing cleanup of its own should call +# cleanup_probe_vfs_getname from its own exit trap, since installing one +# replaces this rather than adding to it. +trap cleanup_probe_vfs_getname exit +# Turn a signal into an ordinary exit so that the exit trap above runs. An +# exit trap that returns leaves the exit status alone, so a test exiting 2 +# to skip still skips. +trap 'exit 1' term int + add_probe_vfs_getname() { add_probe_verbose=$1 if [ $had_vfs_getname -eq 1 ] ; then
@@ -33,8 +76,8 @@ add_probe_vfs_getname() { return 2 fi - perf probe -q "vfs_getname=getname_flags:${line} pathname=result->name:string" || \ - perf probe $add_probe_verbose "vfs_getname=getname_flags:${line} pathname=filename:ustring" || return 1 + perf probe -q "${vfs_getname}=getname_flags:${line} pathname=result->name:string" || \ + perf probe $add_probe_verbose "${vfs_getname}=getname_flags:${line} pathname=filename:ustring" || return 1 fi }
tools/perf/tests/shell/probe_vfs_getname.sh +1 -2
@@ -1,6 +1,5 @@ #!/bin/bash -# Add vfs_getname probe to get syscall args filenames (exclusive) - +# Add vfs_getname probe to get syscall args filenames # SPDX-License-Identifier: GPL-2.0 # Arnaldo Carvalho de Melo <[email protected]>, 2017
tools/perf/tests/shell/record+script_probe_vfs_getname.sh +14 -4
@@ -1,5 +1,5 @@ #!/bin/bash -# Use vfs_getname probe to get syscall args filenames (exclusive) +# Use vfs_getname probe to get syscall args filenames # Uses the 'perf test shell' library to add probe:vfs_getname to the system # then use it with 'perf record' using 'touch' to write to a temp file, then
@@ -17,22 +17,32 @@ skip_if_no_perf_probe || exit 2 # shellcheck source=lib/probe_vfs_getname.sh . "$(dirname "$0")/lib/probe_vfs_getname.sh" +# shellcheck disable=SC2154 # vfs_getname is assigned in lib/probe_vfs_getname.sh record_open_file() { echo "Recording open file:" # Check presence of libtraceevent support to run perf record - skip_no_probe_record_support "probe:vfs_getname*" + skip_no_probe_record_support if [ $? -eq 2 ]; then echo "WARN: Skipping test record_open_file. No libtraceevent support" return 2 fi - perf record -o ${perfdata} -e probe:vfs_getname\* touch $file + # Record every probe the inlining of getname_flags produced, naming + # them exactly rather than with a "${vfs_getname}*" glob, which would + # also match the probes of a test whose pid starts with this one's. + local events + events=$(probes_vfs_getname | paste -sd, -) + if [ -z "${events}" ] ; then + echo "FAIL: no ${vfs_getname} probe to record" + return 1 + fi + perf record -o ${perfdata} -e "${events}" touch $file } perf_script_filenames() { echo "Looking at perf.data file for vfs_getname records for the file we touched:" perf script -i ${perfdata} | \ - grep -E " +touch +[0-9]+ +\[[0-9]+\] +[0-9]+\.[0-9]+: +probe:vfs_getname[_0-9]*: +\([[:xdigit:]]+\) +pathname=\"${file}\"" + grep -E " +touch +[0-9]+ +\[[0-9]+\] +[0-9]+\.[0-9]+: +probe:${vfs_getname}(_[0-9]+)?: +\([[:xdigit:]]+\) +pathname=\"${file}\"" } add_probe_vfs_getname
tools/perf/tests/shell/trace+probe_vfs_getname.sh +9 -0
@@ -10,6 +10,13 @@ # SPDX-License-Identifier: GPL-2.0 # Arnaldo Carvalho de Melo <[email protected]>, 2017 +# This test must stay exclusive, and is the only one of the probe tests that +# does: it does not name the event it uses. perf trace discovers it with the +# hardcoded "probe:vfs_getname*" wildcard in evlist__add_vfs_getname(), so the +# probe has to carry that prefix, and a parallel run of this test would then +# also match, and pin, the probes of the other tests. The sibling tests avoid +# all of this by using a name that the wildcard cannot reach. + # shellcheck source=lib/probe.sh . "$(dirname $0)"/lib/probe.sh
@@ -17,6 +24,8 @@ skip_if_no_perf_probe || exit 2 skip_if_no_perf_trace || exit 2 [ "$(id -u)" = 0 ] || exit 2 +# shellcheck disable=SC2034 # consumed by lib/probe_vfs_getname.sh +vfs_getname="vfs_getname_$$" . "$(dirname $0)"/lib/probe_vfs_getname.sh trace_open_vfs_getname() {
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v3 11/16] perf test probe_vfs_getname: Scope probe name to PID and make non-exclusive
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
The probe name
vfs_getnamewas hardcoded, causing collisions when tests ran concurrently. Furthermore,cleanup_probe_vfs_getname()usedperf probe -d probe:vfs_getname*, deleting probes registered by other parallel tests.Scope the probe name to the pid, and rename it to
getname_flags_$$so that it no longer begins with "vfs_getname". perf trace callsevlist__add_vfs_getname(), which opens every event matching a hardcoded "probe:vfs_getname*" wildcard, so a perf trace run by any other test would otherwise pin this probe and makeperf probe -dfail with-EBUSY. That also unblocks making the perf trace tests non-exclusive later in this series.
Quoted 8 lines
Enumerate the probes to record and to delete from
perf probe -l, matching^probe:${vfs_getname}(_[[:digit:]]+)?$exactly, rather than [ ... ]
accumulating a set per run. The fixed name at least meant the next run found, and went on to reuse, whatever the last one left.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
Ian Rogers
reviewerPATCH v3 12/16 perf test record+probe_libc_inet_pton: Scope event to PID, add retries, and make non-exclusive
In reply to Ian RogersThe uprobe name was not scoped to PID, and concurrent writes to /sys/kernel/debug/tracing/uprobe_events can occasionally return -EBUSY when another process holds the tracefs inode lock.
Scope the probe event name with $$ (inet_pton_$$=inet_pton) and add a retry loop with backoff for uprobe addition. Drop the (exclusive) tag so the test can run in parallel during pass 1.
A PID scoped probe is no longer cleaned up by any other test, so add an EXIT/TERM/INT trap to delete it, otherwise an interrupted run leaks the uprobe into the system. The trap is installed only after the root and IPv6 checks that exit 2 to skip the test, as trap_cleanup() exits 1 and would otherwise turn those skips into failures. Deletion enumerates the probes from perf probe -l, matching ^probe_libc:inet_pton_$$(_[[:digit:]]+)?$ exactly, rather than reading $event_name: a signal arriving after perf probe injected the uprobe but before the assignment completed would leave that variable empty and leak the probe, and an inet_pton_$$* glob would reach the probe of a test whose pid merely starts with this one's.
While here use mktemp rather than mktemp -u for the temporary files:
this test runs as root in a world writable /tmp, and predicting a name without creating it allows another user to win the race and plant a symlink. The perf.data check becomes -s rather than -e as mktemp now pre-creates an empty file.
Pre-create the temporary files with mktemp rather than reserving names with mktemp -u, and bail out if mktemp fails. The emptiness check on the recorded data quotes its path for the same reason: unquoted, an empty value would leave [ ! -s ] testing the string "-s", which is true, so the negation would skip the failure path and the test would go on to pass without having recorded anything.
--- .../shell/record+probe_libc_inet_pton.sh | 107 ++++++++++++++---- 1 file changed, 87 insertions(+), 20 deletions(-)
tools/perf/tests/shell/record+probe_libc_inet_pton.sh +87 -20
@@ -1,5 +1,5 @@ #!/bin/bash -# probe libc's inet_pton & backtrace it with ping (exclusive) +# probe libc's inet_pton & backtrace it with ping # Installs a probe on libc's inet_pton function, that will use uprobes, # then use 'perf trace' on a ping to localhost asking for just one packet
@@ -21,20 +21,30 @@ nm -Dg $libc 2>/dev/null | grep -F -q inet_pton || exit 254 event_pattern='probe_libc:inet_pton(_[[:digit:]]+)?' add_libc_inet_pton_event() { + local attempts=0 + while [ $attempts -lt 3 ]; do + event_name=$(perf probe -f -x $libc -a "inet_pton_$$=inet_pton" 2>&1 | \ + awk -v ep="$event_pattern" -v l="$libc" '$0 ~ ep && $0 ~ \ + ("\\(on inet_pton in " l "\\)") {print $1}' | head -n 1) + + if [ -n "$event_name" ]; then + return 0 + fi + attempts=$((attempts + 1)) + sleep 0.1 + done - event_name=$(perf probe -f -x $libc -a inet_pton 2>&1 | \ - awk -v ep="$event_pattern" -v l="$libc" '$0 ~ ep && $0 ~ \ - ("\\(on inet_pton in " l "\\)") {print $1}' | head -n 1) - - if [ $? -ne 0 ] || [ -z "$event_name" ] ; then - printf "FAIL: could not add event\n" - return 1 - fi + printf "FAIL: could not add event\n" + return 1 } trace_libc_inet_pton_backtrace() { - expected=`mktemp -u /tmp/expected.XXX` + # Create the files rather than just reserving names with mktemp -u: + # this runs as root and /tmp is world writable, so a predictable name + # that is written to later can be pre-created as a symlink by an + # unprivileged user and used to clobber an arbitrary file. + expected=$(mktemp /tmp/expected.XXX) || return 1 echo "ping[][0-9 \.:]+$event_name: \([[:xdigit:]]+\)" > $expected echo ".*inet_pton\+0x[[:xdigit:]]+[[:space:]]\($libc|inlined\)$" >> $expected
@@ -50,8 +60,8 @@ trace_libc_inet_pton_backtrace() { ;; esac - perf_data=`mktemp -u /tmp/perf.data.XXX` - perf_script=`mktemp -u /tmp/perf.script.XXX` + perf_data=$(mktemp /tmp/perf.data.XXX) || return 1 + perf_script=$(mktemp /tmp/perf.script.XXX) || return 1 # Check presence of libtraceevent support to run perf record skip_no_probe_record_support "$event_name/$eventattr/"
@@ -61,9 +71,12 @@ trace_libc_inet_pton_backtrace() { fi perf record -e $event_name/$eventattr/ -o $perf_data ping -6 -c 1 ::1 > /dev/null 2>&1 - # check if perf data file got created in above step. - if [ ! -e $perf_data ]; then - printf "FAIL: perf record failed to create \"%s\" \n" "$perf_data" + # Check perf record actually wrote data. mktemp already created the + # file, so test that it is non-empty rather than that it exists. Quote + # the path: were it ever empty, [ ! -s ] would test the string "-s" + # instead and report success. + if [ ! -s "$perf_data" ]; then + printf "FAIL: perf record failed to write \"%s\" \n" "$perf_data" return 1 fi perf script -i $perf_data | tac | grep -m1 ^ping -B9 | tac > $perf_script
@@ -97,21 +110,75 @@ trace_libc_inet_pton_backtrace() { # even if the perf script output does not match. } +# Print the pid scoped uprobes this test may have created. perf probe appends +# _1, _2, ... when the name is already taken, so match those too, but anchor +# the match: an "inet_pton_$$*" glob would also match the probe of a test whose +# pid merely starts with this one's, e.g. 123 and 1234. +libc_inet_pton_events() { + perf probe -l 2>/dev/null | awk '{print $1}' | + grep -E "^probe_libc:inet_pton_$$(_[[:digit:]]+)?$" +} + delete_libc_inet_pton_event() { + # Ask the kernel what is actually there rather than trusting + # $event_name: a signal arriving after perf probe injected the uprobe + # but before the assignment to event_name completed would otherwise + # leave the variable empty and leak the probe. + # + # Retry as the addition does. Deleting writes to uprobe_events just as + # adding does, so it can lose the same race with a concurrent test and + # fail with -EBUSY. Re-list rather than assume the delete worked, and + # only give up once the probes are really gone: the name is pid + # scoped, so one left behind here is never reused or overwritten by a + # later run and would sit in the kernel until reboot. + local attempts=0 + local probe + + while [ $attempts -lt 3 ]; do + for probe in $(libc_inet_pton_events); do + perf probe -q -d "$probe" + done - if [ -n "$event_name" ] ; then - perf probe -q -d $event_name - fi + if [ -z "$(libc_inet_pton_events)" ]; then + return 0 + fi + + attempts=$((attempts + 1)) + sleep 0.1 + done + + printf "WARN: could not delete event(s): %s\n" \ + "$(libc_inet_pton_events | tr '\n' ' ')" + return 1 +} + +cleanup() { + rm -f ${perf_data} ${perf_script} ${expected} + delete_libc_inet_pton_event + + trap - EXIT TERM INT +} + +trap_cleanup() { + cleanup + exit 1 } # Check for IPv6 interface existence ip a sh lo | grep -F -q inet6 || exit 2 [ "$(id -u)" = 0 ] || exit 2 +# Install the trap only now that the skips above are out of the way: it exits +# 1, so arming it any earlier would turn an 'exit 2' skip into a failure. +# +# The event name is pid scoped, so unlike the old fixed name an orphan left +# behind by an interrupted run is never overwritten by a later run: it would +# stay in the kernel forever. Always clean up, including on a signal. +trap trap_cleanup EXIT TERM INT + skip_if_no_perf_probe && \ add_libc_inet_pton_event && \ trace_libc_inet_pton_backtrace err=$? -rm -f ${perf_data} ${perf_script} ${expected} -delete_libc_inet_pton_event +cleanup exit $err
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v3 12/16] perf test record+probe_libc_inet_pton: Scope event to PID, add retries, and make non-exclusive
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 19 lines
The uprobe name was not scoped to PID, and concurrent writes to
/sys/kernel/debug/tracing/uprobe_eventscan occasionally return-EBUSYwhen another process holds the tracefs inode lock.Scope the probe event name with
$$(inet_pton_$$=inet_pton) and add a retry loop with backoff for uprobe addition. Drop the(exclusive)tag so the test can run in parallel during pass 1.A PID scoped probe is no longer cleaned up by any other test, so add an EXIT/TERM/INT trap to delete it, otherwise an interrupted run leaks the uprobe into the system. The trap is installed only after the root and IPv6 checks that
exit 2to skip the test, astrap_cleanup()exits 1 and would otherwise turn those skips into failures. Deletion enumerates the probes fromperf probe -l, matching^probe_libc:inet_pton_$$(_[[:digit:]]+)?$exactly, rather than reading [ ... ]
true, so the negation would skip the failure path and the test would go on to pass without having recorded anything.
Quoted 2 lines
Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
Ian Rogers
reviewerPATCH v3 13/16 perf test trace_summary: Improve error diagnostics
In reply to Ian RogersWhen pattern matching fails in test_perf_trace(), print the command that failed along with the actual match count, the matching lines found, and the last 20 lines of output to aid debugging.
--- tools/perf/tests/shell/trace_summary.sh | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
tools/perf/tests/shell/trace_summary.sh +10 -4
@@ -28,10 +28,16 @@ test_perf_trace() { count=$(grep -E -c -m 3 "${search}" ${OUTPUT}) if [ "${count}" != "3" ]; then - echo "Error: cannot find enough pattern ${search} in the output" - cat ${OUTPUT} - rm -f ${OUTPUT} - exit 1 + echo "Error: cannot find enough pattern ${search} (count=${count}) in output of:" + echo "Error: perf trace ${args} -- ${workload}" + echo "Error: matched lines:" + grep -E "${search}" ${OUTPUT} || echo "none" + echo "Error: last 20 lines of output:" + # The summary is printed after any trace output, so show the end of + # the file: with -S the head of it is the trace, not what is matched. + tail -n 20 ${OUTPUT} + rm -f ${OUTPUT} + exit 1 fi }
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v3 13/16] perf test trace_summary: Improve error diagnostics
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines
When pattern matching fails in
test_perf_trace(), print the command that failed along with the actual match count, the matching lines found, and the last 20 lines of output to aid debugging.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
Ian Rogers
reviewerPATCH v3 14/16 perf test trace_btf_general: Drop --max-events=1 and make non-exclusive
In reply to Ian Rogerstrace_btf_general.sh used --max-events=1 with perf trace on commands such as mv, echo, and sleep. When background activity occurs or tests run in parallel, perf trace can capture an event from an unrelated process and exit prematurely before recording the target command's syscalls.
Drop --max-events=1 and let tracing run until the command completes, checking for the expected output with grep (matching trace_btf_enum.sh). Remove the (exclusive) tag so the test runs in parallel.
Running perf trace in parallel is only safe now that the probe tests no longer name their probes "vfs_getname...": perf trace opens everything matching a hardcoded "probe:vfs_getname*" wildcard, which used to pin those probes and make their cleanup fail with -EBUSY.
--- tools/perf/tests/shell/trace_btf_general.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
tools/perf/tests/shell/trace_btf_general.sh +4 -4
@@ -1,5 +1,5 @@ #!/bin/bash -# perf trace BTF general tests (exclusive) +# perf trace BTF general tests # SPDX-License-Identifier: GPL-2.0 err=0
@@ -27,7 +27,7 @@ check_vmlinux() { trace_test_string() { echo "Testing perf trace's string augmentation" - output="$(perf trace --sort-events -e renameat* --max-events=1 -- mv ${file1} ${file2} 2>&1)" + output="$(perf trace --sort-events -e renameat* -- mv ${file1} ${file2} 2>&1)" if ! echo "$output" | grep -q -E "^mv/[0-9]+ renameat(2)?\(.*, \"${file1}\", .*, \"${file2}\", .*\) += +[0-9]+$" then printf "String augmentation test failed, output:\n$output\n"
@@ -38,7 +38,7 @@ trace_test_string() { trace_test_buffer() { echo "Testing perf trace's buffer augmentation" # echo will insert a newline (\10) at the end of the buffer - output="$(perf trace --sort-events -e write --max-events=1 -- echo "${buffer}" 2>&1)" + output="$(perf trace --sort-events -e write -- echo "${buffer}" 2>&1)" if ! echo "$output" | grep -q -E "^echo/[0-9]+ write\([0-9]+, ${buffer}.*, [0-9]+\) += +[0-9]+$" then printf "Buffer augmentation test failed, output:\n$output\n"
@@ -48,7 +48,7 @@ trace_test_buffer() { trace_test_struct_btf() { echo "Testing perf trace's struct augmentation" - output="$(perf trace --sort-events -e clock_nanosleep --force-btf --max-events=1 -- sleep 1 2>&1)" + output="$(perf trace --sort-events -e clock_nanosleep --force-btf -- sleep 1 2>&1)" if ! echo "$output" | grep -q -E "^sleep/[0-9]+ clock_nanosleep\(0, 0, \{1,.*\}, 0x[0-9a-f]+\) += +[0-9]+$" then printf "BTF struct augmentation test failed, output:\n$output\n"
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v3 14/16] perf test trace_btf_general: Drop --max-events=1 and make non-exclusive
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines
trace_btf_general.shused--max-events=1withperf traceon commands such asmv,echo, andsleep. When background activity occurs or tests run in parallel,perf tracecan capture an event from an unrelated process and exit prematurely before recording the target command's syscalls.Drop
--max-events=1and let tracing run until the command completes, checking for the expected output with grep (matchingtrace_btf_enum.sh). Remove the (exclusive) tag so the test runs in parallel.
Quoted 7 lines
Running perf trace in parallel is only safe now that the probe tests no longer name their probes "
vfs_getname...": perf trace opens everything matching a hardcoded "probe:vfs_getname*" wildcard, which used to pin those probes and make their cleanup fail with-EBUSY.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
Ian Rogers
reviewerPATCH v3 15/16 perf test trace_summary: Make non-exclusive
In reply to Ian Rogerstrace_summary.sh tests various summary modes of perf trace. It already directs output to a unique temporary file without polluting the current working directory.
Remove the (exclusive) tag so it can run concurrently in parallel test runs.
Running perf trace in parallel is only safe now that the probe tests no longer name their probes "vfs_getname...": perf trace opens everything matching a hardcoded "probe:vfs_getname*" wildcard, which used to pin those probes and make their cleanup fail with -EBUSY.
--- tools/perf/tests/shell/trace_summary.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/tests/shell/trace_summary.sh +1 -1
@@ -1,5 +1,5 @@ #!/bin/bash -# perf trace summary (exclusive) +# perf trace summary # SPDX-License-Identifier: GPL-2.0 # Check that perf trace works with various summary mode
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v3 15/16] perf test trace_summary: Make non-exclusive
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines
trace_summary.shtests various summary modes ofperf trace. It already directs output to a unique temporary file without polluting the current working directory.Remove the (exclusive) tag so it can run concurrently in parallel test runs.
Quoted 7 lines
Running perf trace in parallel is only safe now that the probe tests no longer name their probes "
vfs_getname...": perf trace opens everything matching a hardcoded "probe:vfs_getname*" wildcard, which used to pin those probes and make their cleanup fail with-EBUSY.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
Ian Rogers
reviewerPATCH v3 16/16 perf test uprobe_from_different_cu: Scope probe name to PID
In reply to Ian RogersThe test builds a binary in a per-run temporary directory and probes its foo function. The directory name is unique, but perf probe derives the event name from the probed function and the group name from the binary's basename, so every run registers the same probe_testfile:foo event.
Running the test concurrently with itself, as 'perf test -r3' does, therefore fails in all but one of the runs with:
Error: event "foo" already exists.
Hint: Remove existing event by 'perf probe -d'
and a losing run's cleanup goes on to delete the winning run's probe out from under it.
Name the event after the pid, foo_$$, so that parallel runs no longer collide. This lets the test stay in the parallel pass rather than having to be marked (exclusive).
--- .../perf/tests/shell/test_uprobe_from_different_cu.sh | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
tools/perf/tests/shell/test_uprobe_from_different_cu.sh +9 -2
@@ -18,12 +18,19 @@ fi temp_dir=$(mktemp -d /tmp/perf-uprobe-different-cu-sh.XXXXXXXXXX) +# The name of the uprobe added and removed below. The probe is placed on +# ${temp_dir}/testfile, but perf probe derives the event name from the probed +# function and the group name from the binary's basename, so every run would +# otherwise share one probe_testfile:foo event, and a concurrent run would +# fail with 'event "foo" already exists'. Scope the event name to the pid. +probe_name="foo_$$" + cleanup() { trap - EXIT TERM INT if [[ "${temp_dir}" =~ ^/tmp/perf-uprobe-different-cu-sh.*$ ]]; then echo "--- Cleaning up ---" - perf probe -x ${temp_dir}/testfile -d foo || true + perf probe -x ${temp_dir}/testfile -d ${probe_name} || true rm -f "${temp_dir}/"* rmdir "${temp_dir}" fi
@@ -84,6 +91,6 @@ gcc -g -Og -c ${temp_dir}/testfile-main.c -o ${temp_dir}/testfile-main.o gcc -g -Og -o ${temp_dir}/testfile ${temp_dir}/testfile-foo.o ${temp_dir}/testfile-main.o perf probe -x ${temp_dir}/testfile --funcs foo | grep "foo" -perf probe -x ${temp_dir}/testfile foo +perf probe -x ${temp_dir}/testfile ${probe_name}=foo cleanup
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
Ian Rogers
reviewerv4 cover perf trace: Fix BPF filtering and make tracing tests non-exclusive
In reply to Ian Rogersperf trace's BPF augmentation attaches to raw_syscalls:sys_enter and raw_syscalls:sys_exit system wide, and used the program return value to decide whether a syscall was interesting. Returning 0 from a BPF_PROG_TYPE_TRACEPOINT program makes perf_trace_run_bpf_submit() drop the event for every listener on that tracepoint, not just for the perf trace that installed the program. Any concurrent perf trace, perf record or ftrace session watching raw_syscalls therefore lost events, which is one of the reasons so many of the perf trace and perf probe shell tests had to be marked (exclusive) and run on their own.
Patches 1 to 3 are independent fixes to the code the rest of the series goes on to rework or to rely on.
Patches 4 to 10 fix perf trace. They stop the return value being used as a filter and do the filtering in BPF maps instead, fix argument handling for the __data_loc internal tracepoint fields that syscalls:sys_enter_* gained in 6.19, stop the sys_exit program array tail calling a sys_enter augmenter, and replace the userspace PERF_RECORD_FORK/PERF_RECORD_EXIT bookkeeping with BTF-typed raw tracepoint programs on sched_process_{fork,exit,exec}. A task is then registered before its first syscall and evicted in do_exit(), rather than whenever userspace next drains the ring buffer.
Patches 11 to 18 deal with the tests. Several collided with each other through global state rather than through perf trace: fixed probe names, clear_all_probes() disabling every tracepoint on the system, and perf trace's hardcoded "probe:vfs_getname*" wildcard pinning probes belonging to other tests. With those scoped to a pid they can drop (exclusive) and run in parallel again.
Tested on x86_64. The trace and probe tests pass under 'perf test -r3', which runs the repeats concurrently. Every patch builds individually, and the series also builds with BUILD_BPF_SKEL=0.
Changes since v3:
- New patch 3 returns -ENOMEM rather than -1 from evsel__set_filter(), evsel__append_filter(), evlist__set_tp_filter() and evlist__append_tp_filter(). An allocation is the only thing that can fail in any of them, and patch 7 goes on to print what they return, where -1 negated is EPERM and a failure to allocate would have been reported as "Operation not permitted".
- Patch 5 sets sc->args only once the arg_fmt array that is indexed alongside it has been allocated. Publishing the field first left a syscall with args set and arg_fmt NULL, and since sc->name is already set by then a later syscall__read_info() returns success without retrying the allocation, leaving syscall_arg_fmt__mask_val() to dereference NULL.
- Patch 7 adds the raw_syscalls tracepoints in trace__run() only if they are not in the evlist already, and stops ignoring the result of trace__add_syscall_newtp(). cmd_trace() adds them before it creates the bpf-output event, and on the paths where that creation fails they were added a second time, giving the session two enter and two exit evsels for the same pair of tracepoints.
- Patch 7 no longer abandons the session when a target does not fit in the pid map. bpf_map__update_elem() answers -E2BIG once max_entries keys are present, so a target with more threads than the map has room for took perf trace down with it, on exactly the large workloads where there is least else to reach for. The tasks that fit are added and a warning says how many did not.
- Patch 9 includes <sys/types.h> for the pid_t it uses rather than relying on the include chain to drag it in.
- Patch 9 no longer ends the session when the target has already exited. thread_map__new_by_pid() fails with ENOENT once /proc/<pid>/task is gone, which is the ordinary outcome of 'perf trace -p' on a short lived process, so only ENOMEM is now propagated and everything else is logged and stepped over.
- Patch 9 reads /proc once per thread group instead of once per thread. 'perf trace -p' names every thread of the target in the thread map and /proc/<tid>/task lists the whole group whichever thread it is asked through, so the enumeration was quadratic in the number of threads. On a 64 thread target it went from 65 directory reads and 4097 entries to 2 and 65, for the same set of pids.
- New patch 10 removes from the BPF maps the tasks that died before userspace got round to writing them there. sched_process_exit() can only delete a pid that is already in the map, so a target that exits during startup leaves an entry behind for the rest of the session, and since pids are reused an unrelated task then gets traced, or silently filtered out, in its place. The map is walked with the cursor left on a task found alive, which is one the walk has decided to keep, so its own deletions can never leave the cursor on a key that htab_map_get_next_key() will answer by starting the walk over.
Changes since v2:
- Rebased onto the current perf-tools-next.
- Patch 1 also includes <assert.h>, for the assert() in augmented_syscalls__create_bpf_output().
- New patch 2 makes evsel__put_and_free_priv() free the whole evsel_trace. It only zfree()d the struct, leaking the syscall_arg_fmt array hanging off it. No caller can reach that today, but patch 6 adds one that discards a fully set up evsel.
- Patch 6 identifies the evsel to drop from the evlist by comparing against trace.syscalls.events.sys_enter instead of a strstr() for "syscalls:sys_enter". That substring also matches the per syscall syscalls:sys_enter_SYSCALL tracepoints, so a user asking for one of those by name would have had it removed from the evlist, and __augmented_syscalls__ described with its tracefs format rather than the raw tracepoint's.
- Patch 6 reports a failure to program the pid filters with the error that caused it. trace__run() sent everything trace__set_filter_pids() returned to out_error_mem, which prints "Not enough memory to run!". That was already a guess, and a wrong one now the function writes BPF maps too.
- Patch 7 moves the pid across in sched_process_exec() by deleting the old key before inserting the new one. The move is only a rename, but holding both keys at once needs a spare slot, and on a full map the insert failed with -E2BIG while the delete still succeeded, losing the task instead of moving it.
- Patch 7 no longer claims that a task forked during the attach window is still traced unaugmented. That was wrong: cmd_trace() removes the sys_enter evsel once the bpf-output event exists, so __augmented_syscalls__ is the only source of enter events and such a task is not reported at all. Describe what is really lost, why the tgid fallback is not kept as a safety net for it, note that only 'perf trace -p' is exposed, and that closing it needs the target's descendants re-enumerated from /proc after the attach.
- Patch 7 drops the parent tgid test from sched_process_fork(), so a child inherits from the pid of the thread that called clone() and from nothing else. The lookups only ever match a task's own pid, and every thread of a -p target is enumerated from /proc/<pid>/task and inserted in its own right, so the tgid added no reach. What it did add was a child inheriting from a thread that is not traced itself, such as a sibling of the thread 'perf trace -t' selected.
- Patch 7 ends the session with the error that caused it when the BPF programs cannot be attached, rather than printing a bare errno left over from unwinding the attach. There is nothing to fall back to at that point, cmd_trace() has already built the evlist around __augmented_syscalls__ and dropped the sys_enter evsel, and the code being replaced did not fall back either: it ignored the result of augmented_raw_syscalls_bpf__attach() altogether.
- New patch 8 reads the target out of /proc again once the BPF programs are attached, so a task the target created while perf trace was starting up is traced rather than missed for the whole session. This is the gap patch 7 describes and left for later. It covers the target's new threads and, through task->children, anything it or they forked, to any depth. What is left is a task that made syscalls between sys_enter going live and being added to the map, which is momentary rather than lasting for the session.
- Patch 9 bails out if mktemp fails rather than carrying on with an empty $tmpdir. cd rejects the null directory, and the rmdir that was meant to undo the mktemp then failed to remove '' instead. Its cleanup() also no longer exits when it cannot cd out of the temporary directory, which would have skipped removing it. The removal takes an absolute path and does not need the cd to have succeeded.
- Patch 10 now narrows the disable in clear_all_probes() to the probes themselves rather than dropping it. Clearing kprobe_events or uprobe_events is all or nothing: dyn_events_release_all() returns -EBUSY without removing anything if it finds a probe that is still enabled, so simply removing the write could leave stale probes behind to collide with the next run. The set to disable is read from the kprobe_events and uprobe_events listings rather than assumed to be the groups perf uses, since a probe left enabled in another group, such as the default kprobes group used when kprobe_events is written directly, would abort the clear just the same.
- Patch 11 deletes the probes from an exit trap as well as on the way out. A pid scoped name is never seen again, so a run interrupted before cleanup_probe_vfs_getname() left its probes behind for good, a set per run, where the fixed name was at least found and reused by the next run.
- Patch 12 retries deleting the uprobe. Deletion writes uprobe_events just as addition does and can lose the same race with a concurrent test, and because the event name is now pid scoped a probe left behind is never overwritten by a later run. It also bails out if mktemp fails and quotes the path in the emptiness check, which unquoted would have tested the string "-s" and reported success.
- Patch 13 prints the tail of the output rather than the head. The pattern it matches only appears in the summary, which is printed after any trace output, so the head of the file is not the part that failed to match.
Changes since v1:
- New patch 1 includes <sys/types.h> and <string.h> for the pid_t and strcmp() uses that were relying on the include chain happening to drag them in, which does not hold on libcs such as musl.
- Patch 6 no longer returns success when the event qualifier filter string fails to allocate. err now defaults to 0 because either tracepoint may legitimately be absent, so the ENOMEM path has to set the error itself rather than rely on that default. It also includes <stdbool.h> for the bool parameters it adds to trace_augment.h.
- Patch 9 removes the temporary directory if the cd into it fails. That happens before the cleanup trap is installed, so the directory would otherwise be left behind in /tmp.
Ian Rogers (18):
perf trace: Include the headers declaring pid_t, strcmp and assert perf trace: Free the whole evsel_trace in evsel__put_and_free_priv perf evsel: Report an allocation failure as ENOMEM when setting
filters
perf trace: Start BPF summary before starting workload perf trace: Skip internal tracepoint fields in formatting and beauty
map
perf trace: Do not set unaugmented BPF program on sys_exit map perf trace: Filter events in BPF and avoid tracepoint vetoes perf trace: Handle fork and exit directly in BPF filter maps perf trace: Enumerate the target again once BPF is attached perf trace: Drop targets that died before they were filtered perf test test_task_analyzer: Isolate in temporary directory and make
non-exclusive
perf test common: Only disable probes in clear_all_probes perf test probe_vfs_getname: Scope probe name to PID and make
non-exclusive
perf test record+probe_libc_inet_pton: Scope event to PID, add
retries, and make non-exclusive
perf test trace_summary: Improve error diagnostics
perf test trace_btf_general: Drop --max-events=1 and make
non-exclusive
perf test trace_summary: Make non-exclusive
perf test uprobe_from_different_cu: Scope probe name to PID
tools/perf/Documentation/perf-trace.txt | 5 + tools/perf/builtin-trace.c | 756 ++++++++++++++++-- tools/perf/tests/shell/common/init.sh | 33 +- .../perf/tests/shell/lib/probe_vfs_getname.sh | 51 +- tools/perf/tests/shell/probe_vfs_getname.sh | 3 +- .../shell/record+probe_libc_inet_pton.sh | 107 ++- .../shell/record+script_probe_vfs_getname.sh | 18 +- tools/perf/tests/shell/test_task_analyzer.sh | 20 +- .../shell/test_uprobe_from_different_cu.sh | 11 +- .../tests/shell/trace+probe_vfs_getname.sh | 9 + tools/perf/tests/shell/trace_btf_general.sh | 8 +- tools/perf/tests/shell/trace_summary.sh | 16 +- .../bpf_skel/augmented_raw_syscalls.bpf.c | 312 +++++++- tools/perf/util/bpf_trace_augment.c | 288 ++++++- tools/perf/util/evlist.c | 12 +- tools/perf/util/evsel.c | 4 +- tools/perf/util/trace_augment.h | 33 +- 17 files changed, 1524 insertions(+), 162 deletions(-)
base-commit: 86a27811675a415bd351efca1a194a1a94c082dd
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
Ian Rogers
reviewerPATCH v4 1/18 perf trace: Include the headers declaring pid_t, strcmp and assert
trace_augment.h uses pid_t in the augmented_syscalls__set_filter_pids() prototype and in the !HAVE_BPF_SKEL stub. bpf_trace_augment.c calls strcmp() in augmented_syscalls__find_by_title() and assert() in augmented_syscalls__create_bpf_output(), but neither pulls in the header that declares what it uses. Both happen to build today only because something else in the include chain drags <sys/types.h>, <string.h> and <assert.h> in first, which is not guaranteed and does not hold on libcs such as musl that keep the POSIX namespaces strictly separated.
Include <sys/types.h>, <string.h> and <assert.h> explicitly.
--- tools/perf/util/bpf_trace_augment.c | 2 ++ tools/perf/util/trace_augment.h | 1 + 2 files changed, 3 insertions(+)
tools/perf/util/bpf_trace_augment.c +2 -0
@@ -1,5 +1,7 @@ +#include <assert.h> #include <bpf/libbpf.h> #include <internal/xyarray.h> +#include <string.h> #include "bpf_skel/augmented_raw_syscalls.skel.h" #include "debug.h"
tools/perf/util/trace_augment.h +1 -0
@@ -2,6 +2,7 @@#define TRACE_AUGMENT_H #include <linux/compiler.h> +#include <sys/types.h> struct bpf_program ;struct evlist ;
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 01/18] perf trace: Include the headers declaring pid_t, strcmp and assert
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
trace_augment.husespid_tin theaugmented_syscalls__set_filter_pids()prototype and in the !HAVE_BPF_SKEL stub.bpf_trace_augment.ccallsstrcmp()inaugmented_syscalls__find_by_title()andassert()inaugmented_syscalls__create_bpf_output(), but neither pulls in the header that declares what it uses. Both happen to build today only because something else in the include chain drags <sys/types.h>, <string.h> and <assert.h> in first, which is not guaranteed and does not hold on libcs such as musl that keep the POSIX namespaces strictly separated.
Quoted 4 lines
Include <
sys/types.h>, <string.h> and <assert.h> explicitly.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Ian Rogers
reviewerPATCH v4 2/18 perf trace: Free the whole evsel_trace in evsel__put_and_free_priv
In reply to Ian RogersEvery evsel->priv in builtin-trace.c is a struct evsel_trace, allocated by evsel_trace__new(). It holds a syscall_arg_fmt array in its fmt member, which evsel__syscall_arg_fmt() allocates on demand for the syscalls:sys_{enter,exit}_SYSCALL tracepoints and for every other tracepoint that gets its arguments pretty printed.
evsel__put_and_free_priv() only did zfree(&evsel->priv), releasing the evsel_trace itself and leaking that array. Use evsel_trace__delete(), which frees fmt first, exactly as the out_delete path of evsel__syscall_arg_fmt() already does.
The current callers are all error paths that run before fmt can have been allocated, so nothing leaks in practice today, but the helper is the obvious thing to reach for whenever an evsel is discarded and it should be safe for that.
--- tools/perf/builtin-trace.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
tools/perf/builtin-trace.c +7 -1
@@ -464,7 +464,13 @@ static int evsel__init_tp_ptr_field(struct evsel *evsel, struct tp_field *field,static void evsel__put_and_free_priv (struct evsel *evsel) { -zfree (&evsel->priv); +/* +* evsel->priv is always a struct evsel_trace here, so it has to go +* through evsel_trace__delete(): zfree() on its own would release the +* struct while leaking the syscall_arg_fmt array hanging off it. +*/ +evsel_trace__delete (evsel->priv); + evsel->priv =NULL ;evsel__put (evsel); }
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 02/18] perf trace: Free the whole evsel_trace in evsel__put_and_free_priv
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines
Every
evsel->privinbuiltin-trace.cis a structevsel_trace, allocated byevsel_trace__new(). It holds asyscall_arg_fmtarray in its fmt member, whichevsel__syscall_arg_fmt()allocates on demand for the syscalls:sys_{enter,exit}_SYSCALL tracepoints and for every other tracepoint that gets its arguments pretty printed.
evsel__put_and_free_priv()only didzfree(&evsel->priv), releasing theevsel_traceitself and leaking that array. Useevsel_trace__delete(), which frees fmt first, exactly as theout_deletepath ofevsel__syscall_arg_fmt()already does.
Quoted 7 lines
The current callers are all error paths that run before fmt can have been allocated, so nothing leaks in practice today, but the helper is the obvious thing to reach for whenever an evsel is discarded and it should be safe for that.
Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Ian Rogers
reviewerPATCH v4 3/18 perf evsel: Report an allocation failure as ENOMEM when setting filters
In reply to Ian Rogersevsel__set_filter() and evsel__append_filter() return -1 when the strdup() or asprintf() that builds the new filter string fails, and evlist__set_tp_filter() and evlist__append_tp_filter() do the same when handed the NULL that asprintf__tp_filter_pids() returns for the same reason. In every case the only thing that can have gone wrong is an allocation.
Callers all test the result with "< 0" or for being non-zero, so -1 has been as good as any other error so far, but it is not an errno and so cannot be printed as one. A caller that does, such as
str_error_r(-err, errbuf, sizeof(errbuf))
turns it into EPERM and reports a failure to allocate as "Operation not permitted", which is no help to anyone trying to work out what happened.
Return -ENOMEM instead, which is what these failures are.
--- tools/perf/util/evlist.c | 12 ++++++++++-- tools/perf/util/evsel.c | 4 ++-- 2 files changed, 12 insertions(+), 4 deletions(-)
tools/perf/util/evlist.c +10 -2
@@ -1232,8 +1232,12 @@ int evlist__set_tp_filter(struct evlist *evlist, const char *filter)struct evsel *evsel;int err =0 ; +/* +* The only caller that passes NULL is evlist__set_tp_filter_pids(), +* where it means asprintf__tp_filter_pids() failed to allocate. +*/ if (filter ==NULL ) -return -1 ; +return -ENOMEM ;evlist__for_each_entry (evlist, evsel) {if (evsel->core.attr.type !=PERF_TYPE_TRACEPOINT )
@@ -1252,8 +1256,12 @@ int evlist__append_tp_filter(struct evlist *evlist, const char *filter)struct evsel *evsel;int err =0 ; +/* +* As above, a NULL filter is asprintf__tp_filter_pids() having failed +* to allocate in evlist__append_tp_filter_pids(). +*/ if (filter ==NULL ) -return -1 ; +return -ENOMEM ;evlist__for_each_entry (evlist, evsel) {if (evsel->core.attr.type !=PERF_TYPE_TRACEPOINT )
tools/perf/util/evsel.c +2 -2
@@ -1872,7 +1872,7 @@ int evsel__set_filter(struct evsel *evsel, const char *filter)return 0 ; } -return -1 ; +return -ENOMEM ; }static int evsel__append_filter (struct evsel *evsel,const char *fmt,const char *filter)
@@ -1888,7 +1888,7 @@ static int evsel__append_filter(struct evsel *evsel, const char *fmt, const charreturn 0 ; } -return -1 ; +return -ENOMEM ; }int evsel__append_tp_filter (struct evsel *evsel,const char *filter)
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 03/18] perf evsel: Report an allocation failure as ENOMEM when setting filters
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
evsel__set_filter()andevsel__append_filter()return -1 when thestrdup()orasprintf()that builds the new filter string fails, andevlist__set_tp_filter()andevlist__append_tp_filter()do the same when handed the NULL thatasprintf__tp_filter_pids()returns for the same reason. In every case the only thing that can have gone wrong is an allocation.Callers all test the result with "< 0" or for being non-zero, so -1 has been as good as any other error so far, but it is not an errno and so cannot be printed as one. A caller that does, such as
str_error_r(-err, errbuf, sizeof(errbuf))
Quoted 7 lines
turns it into EPERM and reports a failure to allocate as "Operation not permitted", which is no help to anyone trying to work out what happened.
Return
-ENOMEMinstead, which is what these failures are.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Ian Rogers
reviewerPATCH v4 4/18 perf trace: Start BPF summary before starting workload
In reply to Ian RogersWhen using --bpf-summary, trace_start_bpf_summary() sets skel->bss->enabled = 1. In trace__run(), trace_start_bpf_summary() was previously invoked after evlist__start_workload().
Because evlist__start_workload() immediately unblocks the child process by writing to its go_pipe, short-lived workloads (such as cat /dev/null) can execute and complete their initial system calls before trace_start_bpf_summary() is reached by the parent process. Furthermore, under high system load, the child process may finish before the BPF summary tracking is enabled in the kernel at all, causing syscall summary tests to fail. Additionally, if initial_delay was configured, the workload was started before sleeping.
Move trace_start_bpf_summary() to be invoked before evlist__start_workload(), matching evlist__enable(), and ensure it respects target.initial_delay.
--- tools/perf/builtin-trace.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
tools/perf/builtin-trace.c +5 -3
@@ -4844,17 +4844,19 @@ static int trace__run(struct trace *trace, int argc, const char **argv)if (!target__none (&trace->opts.target) && !trace->opts.target.initial_delay)evlist__enable (evlist); +if (trace->summary_bpf && !trace->opts.target.initial_delay) +trace_start_bpf_summary (); +if (forks)evlist__start_workload (evlist);if (trace->opts.target.initial_delay) {usleep (trace->opts.target.initial_delay *1000 );evlist__enable (evlist); +if (trace->summary_bpf) +trace_start_bpf_summary (); } -if (trace->summary_bpf) -trace_start_bpf_summary (); - trace->multiple_threads =perf_thread_map__pid (evlist__core (evlist)->threads,0 ) ==-1 ||perf_thread_map__nr (evlist__core (evlist)->threads) >1 ||evlist__first (evlist)->core.attr.inherit;
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 04/18] perf trace: Start BPF summary before starting workload
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
When using --bpf-summary,
trace_start_bpf_summary()setsskel->bss->enabled= 1. Intrace__run(),trace_start_bpf_summary()was previously invoked afterevlist__start_workload().Because
evlist__start_workload()immediately unblocks the child process by writing to itsgo_pipe, short-lived workloads (such ascat /dev/null) can execute and complete their initial system calls beforetrace_start_bpf_summary()is reached by the parent process. Furthermore, under high system load, the child process may finish before the BPF summary tracking is enabled in the kernel at all, causing syscall summary tests to fail. Additionally, ifinitial_delaywas configured, the workload was started before sleeping.
Quoted 6 lines
Move
trace_start_bpf_summary()to be invoked beforeevlist__start_workload(), matchingevlist__enable(), and ensure it respects target.initial_delay.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Ian Rogers
reviewerPATCH v4 5/18 perf trace: Skip internal tracepoint fields in formatting and beauty map
In reply to Ian RogersLinux 6.19+ added __data_loc char[] internal fields for string arguments in syscalls:sys_enter_<name> tracepoints (e.g., __data_loc_oldname in sys_enter_renameat2). While is_internal_field() was added to detect them, several places did not properly account for them:
1. In syscall_arg_fmt__init_array(), when an internal field was skipped, the arg pointer was still incremented, causing the subsequent argument formatters to be mismatched.
2. In syscall__scnprintf_args(), internal fields were not skipped, causing spurious trailing arguments like ", 0, 16" to be formatted and printed.
3. In trace__bpf_sys_enter_beauty_map(), internal fields were not skipped, offsetting beauty array argument indices and breaking string and buffer augmentation.
4. In trace__find_usable_bpf_prog_entry(), candidate pointer checks matched on internal pointer fields, breaking signature compatibility matching between syscalls for augmenter sharing. Introduce next_user_arg() and advance both cursors with it, so that the two argument lists are always compared at a real argument and the walk ends when one syscall runs out of arguments rather than when one happens to have trailing internal fields.
5. syscall__augmented_args() computed the augmented payload as sample->raw_size - sc->args_size for any sys_enter style sample. sc->args_size deliberately stops at the last non-internal field, so on 6.19+ a native syscalls:sys_enter_<name> record leaves the __data_loc words and their string payloads in the remainder. Those bytes are not a struct augmented_arg, so syscall_arg__scnprintf_augmented_string() read a bogus length and walked arg->augmented.args out of bounds. This is reachable from trace__event_handler(), which calls trace__fprintf_sys_enter() for any evsel whose tracepoint name starts with "sys_enter_".
6. In syscall__read_info(), syscall__alloc_arg_fmts() was called before checking and dropping the leading __syscall_nr (or nr) field, using nr_fields - 1 unconditionally. If a tracepoint format lacks that leading field, the allocated arg_fmt array is one entry too small and syscall_arg_fmt__init_array() writes one entry past the end of the heap buffer. Drop __syscall_nr/nr first and size the allocation from the remaining fields.
Update these functions to check and skip is_internal_field() so that arguments are correctly formatted and beauty map entries match the expected syscall signatures, restrict syscall__augmented_args() to the __augmented_syscalls__ bpf-output evsel, and size arg_fmt after dropping the syscall number field.
Publish sc->args only once that allocation has succeeded. sc->name is set earlier in syscall__read_info(), and a later call takes a syscall with a name to have been read already and returns it as it stands, so a syscall left with arguments and no arg_fmt to describe them would be printed by walking the arguments and indexing an array that was never allocated.
--- tools/perf/builtin-trace.c | 171 ++++++++++++++++++++++++++++--------- 1 file changed, 129 insertions(+), 42 deletions(-)
tools/perf/builtin-trace.c +129 -42
@@ -2283,15 +2283,20 @@ syscall_arg_fmt__init_array(struct syscall_arg_fmt *arg, struct tep_format_fieldstruct tep_format_field *last_field =NULL ;int len; -for (; field; field = field->next, ++arg) { -/* assume it's the last argument */ +for (; field; field = field->next) { +/* +* Skip internal tracepoint fields (e.g., __data_loc strings in +* Linux 6.19+) so they do not advance the syscall arg array index. +*/ if (is_internal_field (field))continue ; last_field = field; -if (arg->scnprintf) +if (arg->scnprintf) { + ++arg;continue ; + } len =strlen (field->name);
@@ -2348,6 +2353,7 @@ syscall_arg_fmt__init_array(struct syscall_arg_fmt *arg, struct tep_format_field } } } + ++arg; }return last_field;
@@ -2368,7 +2374,8 @@ static int syscall__read_info(struct syscall *sc, struct trace *trace) {char tp_name[128 ];const char *name; -struct tep_format_field *field; +struct tep_format_field *args, *field; +int nr_args;int err;if (sc->nonexistent)
@@ -2407,24 +2414,35 @@ static int syscall__read_info(struct syscall *sc, struct trace *trace)return err; } -/* -* The tracepoint format contains __syscall_nr field, so it's one more -* than the actual number of syscall arguments. -*/ -if (syscall__alloc_arg_fmts (sc, sc->tp_format->format.nr_fields -1 )) -return -ENOMEM ; - - sc->args = sc->tp_format->format.fields; + args = sc->tp_format->format.fields; + nr_args = sc->tp_format->format.nr_fields;/* * We need to check and discard the first variable '__syscall_nr' * or 'nr' that mean the syscall number. It is needless here. * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels. +* +* Do this before allocating, and size the array from what is left, so +* that a format without the field does not leave +* syscall_arg_fmt__init_array() walking one entry past the end. */ -if (sc->args && (!strcmp (sc->args->name,"__syscall_nr" ) || !strcmp (sc->args->name,"nr" ))) { - sc->args = sc->args->next; - --sc->nr_args; +if (args && (!strcmp (args->name,"__syscall_nr" ) || !strcmp (args->name,"nr" ))) { + args = args->next; + --nr_args; } +if (syscall__alloc_arg_fmts (sc, nr_args)) +return -ENOMEM ; + +/* +* Only now that there is an arg_fmt for each of them are the arguments +* published. sc->name was set above, so a later syscall__read_info() +* takes this syscall to be read already and returns it as it stands; +* were sc->args set with sc->arg_fmt still NULL, the printing of that +* syscall would walk the arguments and index an array that does not +* exist. +*/ + sc->args = args; + field = sc->args;while (field) {if (is_internal_field (field))
@@ -2642,11 +2660,17 @@ static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,if (sc->args !=NULL ) {struct tep_format_field *field; -for (field = sc->args; field; - field = field->next, ++arg.idx, bit <<=1 ) { -if (arg.mask & bit) +for (field = sc->args; field; field = field->next) { +/* Skip internal fields so they are not printed as spurious arguments */ +if (is_internal_field (field))continue ; +if (arg.mask & bit) { + ++arg.idx; + bit <<=1 ; +continue ; + } + arg.fmt = &sc->arg_fmt[arg.idx]; val =syscall_arg__val (&arg, arg.idx);/*
@@ -2664,8 +2688,11 @@ static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,*/ if (val ==0 && !trace->show_zeros && !(sc->arg_fmt && sc->arg_fmt[arg.idx].show_zero) && - !(sc->arg_fmt && sc->arg_fmt[arg.idx].strtoul ==STUL_BTF_TYPE )) + !(sc->arg_fmt && sc->arg_fmt[arg.idx].strtoul ==STUL_BTF_TYPE )) { + ++arg.idx; + bit <<=1 ;continue ; + } printed +=scnprintf (bf + printed, size - printed,"%s" , printed ?", " :"" );
@@ -2680,12 +2707,16 @@ static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size, size - printed, val, field->type);if (btf_printed) { printed += btf_printed; + ++arg.idx; + bit <<=1 ;continue ; } } printed +=syscall_arg_fmt__scnprintf_val (&sc->arg_fmt[arg.idx], bf + printed, size - printed, &arg, val); + ++arg.idx; + bit <<=1 ; } }else if (IS_ERR (sc->tp_format)) {/*
@@ -2946,7 +2977,9 @@ static int trace__fprintf_sample(struct trace *trace, struct perf_sample *samplereturn printed; } -static void *syscall__augmented_args (struct syscall *sc,struct perf_sample *sample,int *augmented_args_size,int raw_augmented_args_size) +static void *syscall__augmented_args (struct trace *trace,struct syscall *sc, +struct perf_sample *sample, +int *augmented_args_size,int raw_augmented_args_size) {/* * For now with BPF raw_augmented we hook into raw_syscalls:sys_enter
@@ -2964,6 +2997,24 @@ static void *syscall__augmented_args(struct syscall *sc, struct perf_sample *sam*/ int args_size = raw_augmented_args_size ?: sc->args_size; +/* +* Augmented arguments are a perf trace specific payload, they are only +* ever appended to samples emitted by the BPF __augmented_syscalls__ +* bpf-output event. +* +* Native syscalls:sys_enter_NAME tracepoints may also carry trailing +* data of their own: since Linux 6.19 they append __data_loc char[] +* fields plus the string payloads they point at. Those bytes are not a +* struct augmented_arg, so treating them as one would make +* syscall_arg__scnprintf_augmented_string() read a bogus length and +* walk arg->augmented.args far out of bounds. +* +* So only look for augmented arguments on the event that can actually +* produce them. +*/ +if (sample->evsel != trace->syscalls.events.bpf_output) +return NULL ; + *augmented_args_size = sample->raw_size - args_size;if (*augmented_args_size >0 ) {static uintptr_t argbuf[1024 ];/* assuming single-threaded */
@@ -3022,17 +3073,13 @@ static int trace__sys_enter(struct trace *trace,if (!(trace->duration_filter || trace->summary_only || trace->min_stack))trace__printf_interrupted_entry (trace);/* -* If this is raw_syscalls.sys_enter, then it always comes with the 6 possible -* arguments, even if the syscall being handled, say "openat", uses only 4 arguments -* this breaks syscall__augmented_args() check for augmented args, as we calculate -* syscall->args_size using each syscalls:sys_enter_NAME tracefs format file, -* so when handling, say the openat syscall, we end up getting 6 args for the -* raw_syscalls:sys_enter event, when we expected just 4, we end up mistakenly -* thinking that the extra 2 u64 args are the augmented filename, so just check -* here and avoid using augmented syscalls when the evsel is the raw_syscalls one. +* syscall__augmented_args() only returns a payload for the BPF +* __augmented_syscalls__ event, so raw_syscalls:sys_enter (which always +* carries all 6 possible arguments rather than sc->args_size worth) and +* the native syscalls:sys_enter_NAME tracepoints are both handled there. */ -if (evsel != trace->syscalls.events.sys_enter) - augmented_args =syscall__augmented_args (sc, sample, &augmented_args_size, trace->raw_augmented_syscalls_args_size); + augmented_args =syscall__augmented_args (trace, sc, sample, &augmented_args_size, + trace->raw_augmented_syscalls_args_size); ttrace->entry_time = sample->time; ttrace->entry_cpu = sample->cpu; msg = ttrace->entry_str;
@@ -3077,7 +3124,7 @@ static int trace__fprintf_sys_enter(struct trace *trace, struct perf_sample *samstruct syscall *sc;char msg[1024 ];void *args, *augmented_args =NULL ; -int augmented_args_size, e_machine; +int augmented_args_size =0 , e_machine;size_t printed =0 ;
@@ -3095,7 +3142,8 @@ static int trace__fprintf_sys_enter(struct trace *trace, struct perf_sample *sam goto out_put; args =perf_evsel__sc_tp_ptr (args, sample); - augmented_args =syscall__augmented_args (sc, sample, &augmented_args_size, trace->raw_augmented_syscalls_args_size); + augmented_args =syscall__augmented_args (trace, sc, sample, &augmented_args_size, + trace->raw_augmented_syscalls_args_size); printed +=syscall__scnprintf_args (sc, msg,sizeof (msg), args, augmented_args, augmented_args_size, trace, thread);fprintf (trace->output,"%.*s" , (int )printed, msg); err =0 ;
@@ -4127,10 +4175,16 @@ static int trace__bpf_sys_enter_beauty_map(struct trace *trace, int e_machine, iif (trace->btf ==NULL )return -1 ; -for (i =0 , field = sc->args; field; ++i, field = field->next) { +for (i =0 , field = sc->args; field; field = field->next) { +/* Skip internal fields to keep beauty array index aligned with syscall arguments */ +if (is_internal_field (field)) +continue ; +// XXX We're only collecting pointer payloads _from_ user space -if (!sc->arg_fmt[i].from_user) +if (!sc->arg_fmt[i].from_user) { + ++i;continue ; + } struct_offset =strstr (field->type,"struct " );if (struct_offset ==NULL )
@@ -4149,8 +4203,10 @@ static int trace__bpf_sys_enter_beauty_map(struct trace *trace, int e_machine, i name[cnt] ='\0' ;/* cache struct's btf_type and type_id */ -if (syscall_arg_fmt__cache_btf_struct (&sc->arg_fmt[i], trace->btf, name)) +if (syscall_arg_fmt__cache_btf_struct (&sc->arg_fmt[i], trace->btf, name)) { + ++i;continue ; + } bt = sc->arg_fmt[i].type; beauty_array[i] = bt->size;
@@ -4176,7 +4232,9 @@ static int trace__bpf_sys_enter_beauty_map(struct trace *trace, int e_machine, istruct tep_format_field *field_tmp;/* find the size of the buffer that appears in pairs with buf */ -for (j =0 , field_tmp = sc->args; field_tmp; ++j, field_tmp = field_tmp->next) { +for (j =0 , field_tmp = sc->args; field_tmp; field_tmp = field_tmp->next) { +if (is_internal_field (field_tmp)) +continue ; if (!(field_tmp->flags & TEP_FIELD_IS_POINTER) && /* only integers */ (strstr(field_tmp->name, "count") || strstr(field_tmp->name, "siz") || /* size, bufsiz */
@@ -4186,8 +4244,10 @@ static int trace__bpf_sys_enter_beauty_map(struct trace *trace, int e_machine, i can_augment = true;break ; } + ++j; } } + ++i; }if (can_augment)
@@ -4196,6 +4256,19 @@ static int trace__bpf_sys_enter_beauty_map(struct trace *trace, int e_machine, ireturn -1 ; } +/* +* Advance to the first field that is a real syscall argument, so that callers +* walking two argument lists in step never have to reason about internal +* fields appearing in one list but not the other. +*/ +static struct tep_format_field *next_user_arg (struct tep_format_field *field) +{ +while (field &&is_internal_field (field)) + field = field->next; + +return field; +} +static struct bpf_program *trace__find_usable_bpf_prog_entry (struct trace *trace,struct syscall *sc) {
@@ -4203,7 +4276,7 @@ static struct bpf_program *trace__find_usable_bpf_prog_entry(struct trace *trace/* * We're only interested in syscalls that have a pointer: */ -for (field = sc->args; field; field = field->next) { +for (field =next_user_arg (sc->args); field; field =next_user_arg (field->next)) {if (field->flags &TEP_FIELD_IS_POINTER ) goto try_to_find_pair; }
@@ -4221,21 +4294,31 @@ static struct bpf_program *trace__find_usable_bpf_prog_entry(struct trace *trace pair->bpf_prog.sys_enter == unaugmented_prog)continue ; -for (field = sc->args, candidate_field = pair->args; - field && candidate_field; field = field->next, candidate_field = candidate_field->next) { +/* +* Both cursors only ever point at real arguments, so the loop +* ends when one of the two syscalls runs out of them, rather +* than when one happens to have trailing internal fields. +*/ + field =next_user_arg (sc->args); + candidate_field =next_user_arg (pair->args); +while (field && candidate_field) {bool is_pointer = field->flags &TEP_FIELD_IS_POINTER , candidate_is_pointer = candidate_field->flags &TEP_FIELD_IS_POINTER ;if (is_pointer) { -if (!candidate_is_pointer) { +if (!candidate_is_pointer) {// The candidate just doesn't copies our pointer arg, might copy other pointers we want. + field =next_user_arg (field->next); + candidate_field =next_user_arg (candidate_field->next);continue ; - } + } }else {if (candidate_is_pointer) {// The candidate might copy a pointer we don't have, skip it. goto next_candidate; } + field =next_user_arg (field->next); + candidate_field =next_user_arg (candidate_field->next);continue ; }
@@ -4256,6 +4339,8 @@ static struct bpf_program *trace__find_usable_bpf_prog_entry(struct trace *trace goto next_candidate; is_candidate = true; + field =next_user_arg (field->next); + candidate_field =next_user_arg (candidate_field->next); }if (!is_candidate)
@@ -4267,7 +4352,9 @@ static struct bpf_program *trace__find_usable_bpf_prog_entry(struct trace *trace* more than what is common to the two syscalls. */ if (candidate_field) { -for (candidate_field = candidate_field->next; candidate_field; candidate_field = candidate_field->next) + candidate_field =next_user_arg (candidate_field->next); +for (; candidate_field; + candidate_field =next_user_arg (candidate_field->next))if (candidate_field->flags &TEP_FIELD_IS_POINTER ) goto next_candidate; }
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 05/18] perf trace: Skip internal tracepoint fields in formatting and beauty map
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 19 lines
Linux 6.19+ added
__data_locchar[] internal fields for string arguments in syscalls:sys_enter_<name> tracepoints (e.g.,__data_loc_oldnameinsys_enter_renameat2). Whileis_internal_field()was added to detect them, several places did not properly account for them:1. In
syscall_arg_fmt__init_array(), when an internal field was skipped, the arg pointer was still incremented, causing the subsequent argument formatters to be mismatched.
2. Insyscall__scnprintf_args(), internal fields were not skipped, causing spurious trailing arguments like ", 0, 16" to be formatted and printed.
3. Intrace__bpf_sys_enter_beauty_map(), internal fields were not skipped, offsetting beauty array argument indices and breaking string and buffer augmentation.
[ ... ]
printed by walking the arguments and indexing an array that was never allocated.
Quoted 2 lines
Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Ian Rogers
reviewerPATCH v4 6/18 perf trace: Do not set unaugmented BPF program on sys_exit map
In reply to Ian RogersIn trace__init_syscalls_bpf_prog_array_maps(), the BPF program array map for sys_exit (syscalls_sys_exit) was populated with the result of trace__bpf_prog_sys_exit_fd().
When a syscall had no specific exit augmenter, trace__find_syscall_bpf_prog() fell back to unaugmented_prog (syscall_unaugmented). However, syscall_unaugmented is a sys_enter program that outputs enter arguments to __augmented_syscalls__.
As a consequence, when an unaugmented syscall exited, sys_exit tail-called syscall_unaugmented, which interpreted the exit arguments as enter arguments and emitted a duplicate, corrupt sys_enter event into __augmented_syscalls__ right as the syscall completed.
Fix this by:
1. Returning NULL from trace__find_syscall_bpf_prog() when looking up exit augmenters and none is found.
2. Returning -1 from trace__bpf_prog_sys_exit_fd() when no exit program is present.
3. Only updating map_exit_fd when prog_fd >= 0.
4. Clearing err = 0 when trace__bpf_sys_enter_beauty_map() returns non-zero (indicating the syscall has no augmentable pointer arguments) before continuing the loop, so a trailing run of such syscalls (e.g. 'perf trace -e close') does not leave err non-zero on return and abort the session.
--- tools/perf/builtin-trace.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-)
tools/perf/builtin-trace.c +22 -6
@@ -4133,7 +4133,12 @@ static struct bpf_program *trace__find_syscall_bpf_prog(struct trace *trace __mapr_debug ("Couldn't find BPF prog \"%s\" to associate with syscalls:sys_%s_%s, not augmenting it\n" , prog_name, type, sc->name); out_unaugmented: -return unaugmented_prog; +/* +* Do not set unaugmented_prog for exit: syscall_unaugmented is a +* sys_enter program that outputs enter arguments. Exit without a +* specialized return augmenter returns 1 directly from sys_exit. +*/ +return !strcmp (type,"exit" ) ?NULL : unaugmented_prog; }static void trace__init_syscall_bpf_progs (struct trace *trace,int e_machine,int id)
@@ -4156,7 +4161,7 @@ static int trace__bpf_prog_sys_enter_fd(struct trace *trace, int e_machine, intstatic int trace__bpf_prog_sys_exit_fd (struct trace *trace,int e_machine,int id) {struct syscall *sc =trace__syscall_info (trace,NULL , e_machine, id); -return sc ?bpf_program__fd (sc->bpf_prog.sys_exit) :bpf_program__fd (unaugmented_prog); +return sc && sc->bpf_prog.sys_exit ?bpf_program__fd (sc->bpf_prog.sys_exit) :-1 ; }static int trace__bpf_sys_enter_beauty_map (struct trace *trace,int e_machine,int key,unsigned int *beauty_array)
@@ -4411,16 +4416,27 @@ static int trace__init_syscalls_bpf_prog_array_maps(struct trace *trace, int e_m err =bpf_map_update_elem (map_enter_fd, &key, &prog_fd,BPF_ANY );if (err)break ; +/* Only update the exit prog array map if an exit augmenter exists */ prog_fd =trace__bpf_prog_sys_exit_fd (trace, e_machine, key); - err =bpf_map_update_elem (map_exit_fd, &key, &prog_fd,BPF_ANY ); -if (err) -break ; +if (prog_fd >=0 ) { + err =bpf_map_update_elem (map_exit_fd, &key, &prog_fd,BPF_ANY ); +if (err) +break ; + }/* use beauty_map to tell BPF how many bytes to collect, set beauty_map's value here */ memset (beauty_array,0 ,sizeof (beauty_array)); err =trace__bpf_sys_enter_beauty_map (trace, e_machine, key, (unsigned int *)beauty_array); -if (err) +if (err) { +/* +* Not a failure: the syscall just has no augmentable +* arguments. Clear err, or a trailing run of such +* syscalls, e.g. all of them for 'perf trace -e close', +* would leave it set on return and abort the session. +*/ + err =0 ;continue ; + } err =bpf_map_update_elem (beauty_map_fd, &key, beauty_array,BPF_ANY );if (err)break ;
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 06/18] perf trace: Do not set unaugmented BPF program on sys_exit map
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 26 lines
In
trace__init_syscalls_bpf_prog_array_maps(), the BPF program array map forsys_exit(syscalls_sys_exit) was populated with the result oftrace__bpf_prog_sys_exit_fd().When a syscall had no specific exit augmenter,
trace__find_syscall_bpf_prog()fell back tounaugmented_prog(syscall_unaugmented). However,syscall_unaugmentedis asys_enterprogram that outputs enter arguments to__augmented_syscalls__.As a consequence, when an unaugmented syscall exited,
sys_exittail-calledsyscall_unaugmented, which interpreted the exit arguments as enter arguments and emitted a duplicate, corruptsys_enterevent into__augmented_syscalls__right as the syscall completed.Fix this by:
1. Returning NULL fromtrace__find_syscall_bpf_prog()when looking up exit augmenters and none is found.
2. Returning -1 fromtrace__bpf_prog_sys_exit_fd()when no exit program is present.
3. Only updatingmap_exit_fdwhenprog_fd>= 0.
4. Clearing err = 0 whentrace__bpf_sys_enter_beauty_map()returns non-zero (indicating the syscall has no augmentable pointer arguments) before continuing the loop, so a trailing run of such syscalls (e.g. 'perf trace -e close') does not leave err non-zero on return and abort the session.
Quoted 2 lines
Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Ian Rogers
reviewerPATCH v4 7/18 perf trace: Filter events in BPF and avoid tracepoint vetoes
In reply to Ian RogersThe BPF augmented_raw_syscalls sys_enter and sys_exit programs returned 0 for syscalls that were not of interest. Returning 0 from a tracepoint BPF program vetoes the event for the whole system, so an unrelated concurrent perf trace, perf record or ftrace session listening to raw_syscalls would silently lose events. This is a cross-session side effect and shows up as flaky failures when perf tests run in parallel.
Furthermore, syscall_unaugmented previously returned 1 without writing anything to the __augmented_syscalls__ ring buffer. This forced userspace perf trace to listen to both raw_syscalls:sys_enter and __augmented_syscalls__ in its evlist, requiring userspace event deduplication.
Address these issues:
1. In augmented_raw_syscalls.bpf.c, never return 0 from tracepoint
handlers: return 1 so non-traced syscalls pass through without vetoing other concurrent listeners.
2. Introduce pids_to_trace and syscalls_to_trace BPF hash maps to perform targeted filtering directly in BPF. Unselected syscalls or PIDs return 1 immediately without writing to the buffer.
3. In syscall_unaugmented, output the unaugmented enter payload into __augmented_syscalls__ and return 1. Change its section from SEC("tp/raw_syscalls/sys_enter") to SEC("tp/syscalls/sys_enter_unaugmented") so libbpf does not attempt to auto-attach it to raw_syscalls:sys_enter.
4. In bpf_trace_augment.c, add helpers to configure target PIDs and syscalls in the BPF maps, setting the activation flags (has_pids_to_trace, has_syscalls_to_trace) only after the maps are fully populated so already-attached BPF programs do not filter against a half-filled map. Explicitly attach only sys_enter and sys_exit via an attach_prog() helper that saves -errno before calling pr_debug() or bpf_link__destroy().
Destroy the skeleton on every failure path. Leaving a loaded but unusable skeleton behind is not inert: the setters called later from trace__run() would program its maps, and a partial attach would leave a BPF program live on raw_syscalls for a session that never starts. Since augmented_syscalls__{prepare,create_bpf_output}() failures fall back to unaugmented tracing rather than aborting, those setters have to become no-ops, which they only do once skel is NULL again.
errno is used directly here, so include <errno.h> rather than relying on it arriving via another header, which it does not under musl.
5. In builtin-trace.c, hook trace__set_ev_qualifier_filter() and PID filtering into the BPF maps. When __augmented_syscalls__ is active, remove raw_syscalls:sys_enter from trace.evlist since all traced enter events (both augmented and unaugmented) are now emitted by BPF into __augmented_syscalls__. Identify that evsel by comparing against trace.syscalls.events.sys_enter rather than by a strstr() of its name. The substring "syscalls:sys_enter" also matches the per syscall syscalls:sys_enter_SYSCALL tracepoints, which a user can ask for by name, and now that the match decides what is taken out of the evlist, claiming one of those would drop an event that was asked for and would describe __augmented_syscalls__ with its format rather than the raw one's. Restore tracking on the remaining evsel via evlist__set_tracking_event() so PERF_RECORD_COMM and tracking events continue to be recorded. Errors from augmented_syscalls__set_target_syscalls() are reported and propagated, the tracepoint filter string is freed on every exit path, and an allocation failure in trace__set_filter_pids() now returns -ENOMEM instead of being silently ignored.
Note that in trace__set_filter_pids() the target pids and the filtered pids are two independent axes and both have to be programmed. Naming pids to leave out with --filter-pids does not widen -p/-t or a workload to the whole system, and a BPF tracepoint program is attached system wide rather than to the target's file descriptors, so pids_to_trace is the only thing keeping other tasks out.
6. Add --syscall-augment option (defaulting to true) to allow users to explicitly use --no-syscall-augment to run perf trace in the classic unaugmented tracepoint mode without BPF. When BPF is unavailable or disabled, ensure the non-augmented tracepoint path cleanly configures sys_enter and sys_exit without duplicate entries.
In trace__run(), add those tracepoints only when neither of them is in the evlist already. trace__add_syscall_newtp() adds the pair whatever is there, and cmd_trace() may have left either or both
behind: both when it prepared augmentation and then gave up on it, the exit one alone when __augmented_syscalls__ took over the enters. Neither is covered by trace.raw_augmented_syscalls, which is only set when the exit evsel is named raw_syscalls:sys_exit, and not on a kernel old enough for perf_evsel__raw_syscall_newtp() to have fallen back to syscalls:sys_exit. Adding the pair again reports the events that were there twice, which the fallback after a bpf-output failure already did before this.
Act on trace__add_syscall_newtp()'s result in cmd_trace() rather than discarding it. Those tracepoints are what the augmented events are described and paired with, so without them there is nothing to
augment: the skeleton is dropped and the session carries on with plain tracepoints, which is what the bpf-output failure beside it already does, and what lets the evlist test above rely on the exit tracepoint being there whenever the bpf-output event is.
A target that does not fit in the map is not a reason to give up on the session. bpf_map__update_elem() answers -E2BIG once max_entries keys are present, so a target with more threads than pids_to_trace has room for would take the whole of perf trace down with it, on exactly the large workloads where there is least else to reach for. The tasks that fit are added, a warning says how many did not, and the ones left out are in the position they would have been in had they been created once the map was already full, which sched_process_fork() has to allow for regardless.
Report a failure to program the pid filters with the error that caused it. trace__run() sent everything trace__set_filter_pids() returned to out_error_mem, which prints "Not enough memory to run!". That was already a guess, and becomes a wrong one now that the function also writes BPF maps, which fail for reasons of their own that have nothing to do with memory and that the message gives the user no way to act on.
Report a failure to program the syscall filters the same way. The pr_err() in trace__set_ev_qualifier_filter() ran before trace__run() reached out_errno and printed "%m", and anything called in between could have changed errno by then, so the two did not have to agree. The inner report becomes a pr_debug() and the caller prints the error it was given, which leaves out_errno without a user and it is removed.
--- tools/perf/Documentation/perf-trace.txt | 5 + tools/perf/builtin-trace.c | 222 +++++++++++++++--- .../bpf_skel/augmented_raw_syscalls.bpf.c | 133 +++++++++-- tools/perf/util/bpf_trace_augment.c | 169 ++++++++++++- tools/perf/util/trace_augment.h | 34 +++ 5 files changed, 511 insertions(+), 52 deletions(-)
tools/perf/Documentation/perf-trace.txt +5 -0
@@ -260,6 +260,11 @@ the thread executes on the designated CPUs. Default is to monitor all CPUs. Maximum number of lines in the summary mode. Note that this applies to each entry (thread or cgroup). +--syscall-augment:: + Augment syscalls with BPF. Enabled by default when BPF support is available. + Use --no-syscall-augment to disable BPF augmentation and fall back to the + unaugmented tracepoint approach. + PAGEFAULTS ----------
tools/perf/builtin-trace.c +191 -31
355 lines not on this page · raw message
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +118 -15
201 lines not on this page · raw message
tools/perf/util/bpf_trace_augment.c +163 -6
211 lines not on this page · raw message
tools/perf/util/trace_augment.h +34 -0
@@ -2,6 +2,7 @@#define TRACE_AUGMENT_H #include <linux/compiler.h> +#include <stdbool.h> #include <sys/types.h> struct bpf_program ;
@@ -13,6 +14,11 @@ int augmented_syscalls__prepare(void);int augmented_syscalls__create_bpf_output (struct evlist *evlist);void augmented_syscalls__setup_bpf_output (void );int augmented_syscalls__set_filter_pids (unsigned int nr,pid_t *pids); +int augmented_syscalls__set_target_pids (unsigned int nr,pid_t *pids); +int augmented_syscalls__add_target_pid (pid_t pid); +int augmented_syscalls__del_target_pid (pid_t pid); +bool augmented_syscalls__has_target_pid (pid_t pid); +int augmented_syscalls__set_target_syscalls (unsigned int nr,int *syscall_ids,bool not_syscalls);int augmented_syscalls__get_map_fds (int *enter_fd,int *exit_fd,int *beauty_fd);struct bpf_program *augmented_syscalls__find_by_title (const char *name);struct bpf_program *augmented_syscalls__unaugmented (void );
@@ -40,6 +46,34 @@ static inline int augmented_syscalls__set_filter_pids(unsigned int nr __maybe_unreturn 0 ; } +static inline int augmented_syscalls__set_target_pids (unsigned int nr__maybe_unused , +pid_t *pids__maybe_unused ) +{ +return 0 ; +} + +static inline int augmented_syscalls__add_target_pid (pid_t pid__maybe_unused ) +{ +return 0 ; +} + +static inline int augmented_syscalls__del_target_pid (pid_t pid__maybe_unused ) +{ +return 0 ; +} + +static inline bool augmented_syscalls__has_target_pid (pid_t pid__maybe_unused ) +{ +return false; +} + +static inline int augmented_syscalls__set_target_syscalls (unsigned int nr__maybe_unused , +int *syscall_ids__maybe_unused , +bool not_syscalls__maybe_unused ) +{ +return 0 ; +} +static inline int augmented_syscalls__get_map_fds (int *enter_fd__maybe_unused ,int *exit_fd__maybe_unused ,int *beauty_fd__maybe_unused )
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 07/18] perf trace: Filter events in BPF and avoid tracepoint vetoes
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
The BPF
augmented_raw_syscallssys_enterandsys_exitprograms returned 0 for syscalls that were not of interest. Returning 0 from a tracepoint BPF program vetoes the event for the whole system, so an unrelated concurrent perf trace, perf record or ftrace session listening toraw_syscallswould silently lose events. This is a cross-session side effect and shows up as flaky failures when perf tests run in parallel.Furthermore,
syscall_unaugmentedpreviously returned 1 without writing anything to the__augmented_syscalls__ring buffer. This forced userspace perf trace to listen to both raw_syscalls:sys_enter and__augmented_syscalls__in its evlist, requiring userspace event deduplication.
Quoted 8 lines
Address these issues:
1. Inaugmented_raw_syscalls.bpf.c, never return 0 from tracepoint [ ... ]
The inner report becomes apr_debug()and the caller prints the error it was given, which leavesout_errnowithout a user and it is removed.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Ian Rogers
reviewerPATCH v4 8/18 perf trace: Handle fork and exit directly in BPF filter maps
In reply to Ian RogersUpdating target or filtered PIDs in userspace upon processing PERF_RECORD_FORK and PERF_RECORD_EXIT events introduces latency between event occurrence and userspace BPF map updates. If a newly forked child executes system calls before userspace processes PERF_RECORD_FORK, those syscalls may be dropped by BPF PID filtering. Conversely, if userspace evicts PIDs asynchronously on PERF_RECORD_EXIT, the kernel may recycle a PID before userspace processes the exit event, causing the late eviction to silently drop a newly created task that received the recycled PID.
Address this by attaching BTF-typed raw tracepoint BPF programs directly to the scheduler task lifetime tracepoints:
1. Attach SEC("tp_btf/sched_process_fork") (sched_process_fork), which runs in copy_process() in the parent's context before wake_up_new_task() wakes the child. Using tp_btf rather than SEC("tp/sched/sched_process_fork") receives the stable TP_PROTO arguments (struct task_struct *parent, struct task_struct *child) rather than the tracepoint ring-buffer record (TP_STRUCT__entry), whose layout changed in Linux 6.16 when parent_comm and child_comm were converted from 16-byte arrays to 4-byte __data_loc strings. When inherit is enabled and the pid of the thread that called clone() is in pids_to_trace or pids_filtered, insert child->pid into the corresponding map immediately. Because child->pid is task_struct.pid (the global initial-namespace PID), this works accurately across PID namespaces without aliasing host PIDs, and covers both new processes and CLONE_THREAD threads without needing real_parent CO-RE walks or syscall-return heuristics.
2. Attach SEC("tp_btf/sched_process_exit") (sched_process_exit), which runs in do_exit() for every task in its own context, including tasks killed by signals (SIGKILL, SIGSEGV, etc.) and secondary threads torn down implicitly by exit_group. Delete the dying task's PID from pids_to_trace and pids_filtered immediately in kernel space, eliminating both map leaks and any asynchronous userspace eviction window where PID recycling could occur.
3. Attach SEC("tp_btf/sched_process_exec") (sched_process_exec) to follow the one case where a live task's pid changes underneath the maps. When a thread that is not the group leader execs, de_thread() kills the leader and hands the leader's pid, which is the tgid, to the exec'ing thread. The leader dies first, so sched_process_exit() has already dropped exactly the pid the survivor now holds, and the survivor's old entry would be stranded in the map for good. Move the entry from old_pid to p->pid. old_pid is sampled in bprm_execve() before de_thread() runs, so the ordinary group leader exec is a no-op here. Drop the old key before inserting the new one:
the move is only a rename, but holding both keys at once needs a spare slot, and on a full map the insert would fail with -E2BIG while the delete still succeeded, losing the task instead of moving it.
4. With every live task registered before its first syscall and evicted in do_exit(), simplify pid_to_trace__has() and pid_filter__has() to single BPF hash map lookups, and move bpf_probe_read_kernel() in sys_exit back after the PID filter checks.
5. Pass the inherit flag from userspace to BPF .rodata via augmented_syscalls__prepare(!trace.opts.no_inherit), and split attaching out of it into augmented_syscalls__attach(), called from trace__run() once the pid, syscall and program array maps have all been programmed. These are system wide programs, so from the instant they attach they alone decide what is traced: attaching at load time, as before, left a window in which a target could fork without sched_process_fork() knowing the parent was a target, and with the userspace fork handling gone there was nothing left to recover it. The scheduler programs are attached ahead of sys_enter and sys_exit for the same reason. Set has_pids_filtered only after populating pids_filtered. A failure to attach ends the session, reporting the error that caused it. There is no falling back to unaugmented tracing by that point: cmd_trace() built the evlist around __augmented_syscalls__ and dropped the sys_enter evsel, so a session that carried on would report nothing at all. Neither did the code this replaces, which ignored the result of augmented_raw_syscalls_bpf__attach() altogether and ran on with programs that had never been attached.
6. Remove the userspace BPF map updates from PERF_RECORD_FORK and PERF_RECORD_EXIT in trace__process_event(), and delete the now-unused augmented_syscalls__{add,del,has}_target_pid() helpers. No coverage is lost with them: those records only come into being once the ring buffers are mapped by evlist__do_mmap() and the events are switched on by evlist__enable(), both of which run after augmented_syscalls__attach() in trace__run(), and they are then acted on later still, whenever the poll loop gets round to them. The scheduler programs therefore go live strictly earlier than the userspace path could ever have reacted.
7. Gate pid_filter__has() on a has_pids_filtered flag in .bss so the common case without --filter-pids performs no map lookups, and size pids_to_trace and pids_filtered at 16384 entries. pids_filtered is grown from 64 because it is no longer just the handful of pids userspace names: sched_process_fork() adds every descendant of those, so a --filter-pids target that forks or is heavily threaded needs the same headroom as a traced one.
A fork or exit is still not seen if it happens before the programs are attached, that is between evlist__create_maps() scanning /proc for a -p target and augmented_syscalls__attach(). Such a window is inherent in programming a system wide filter before switching it on, and as above the userspace handling did not cover it either. The cost is not small though. Once the bpf-output event exists cmd_trace() removes the sys_enter evsel from the evlist, so __augmented_syscalls__ is the only source of enter events and a pid that is missing from pids_to_trace is not reported at all. sys_enter returning 1 keeps the kernel tracepoint alive for other subscribers, it does not give perf trace a second path to the event. A target that forks during perf trace's own startup can therefore have that child, and in turn everything the child forks, go untraced for the whole run.
The tgid fallback that pid_to_trace__has() used to have would have masked part of this, since a thread missed in the window still shares the tgid of a target userspace did insert. It is not kept because it would also re-admit tasks that sched_process_fork() deliberately
skipped: under --no-inherit a new thread of the target is not added to the map, yet it shares the target's tgid and a tgid test cannot tell it apart from one that was.
sched_process_fork() does not consult the parent's tgid either, for the same reason. Every thread of a -p target is enumerated from /proc/<pid>/task and inserted under its own pid, and -t names a single thread, so a tgid test adds no reach. What it would add is a child inheriting from a thread that is not traced itself: a sibling of the thread -t selected, or one missed in the attach window whose own syscalls go unreported. Inheritance keys off the pid of the thread that called clone(), exactly as the lookups do.
Two of the three ways of selecting what to trace are unaffected:
'perf trace -- cmd' cannot hit this because evlist__prepare_workload() leaves the child blocked on a pipe until evlist__start_workload(), well after the attach, and 'perf trace -a' never sets has_pids_to_trace so it does not filter at all. It is 'perf trace -p' against an already running target that is exposed. Closing that too needs the descendants of the target re-enumerated from /proc after the attach, which the next change does.
--- tools/perf/builtin-trace.c | 55 +++-- .../bpf_skel/augmented_raw_syscalls.bpf.c | 199 +++++++++++++++++- tools/perf/util/bpf_trace_augment.c | 120 +++++++---- tools/perf/util/trace_augment.h | 28 +-- 4 files changed, 304 insertions(+), 98 deletions(-)
tools/perf/builtin-trace.c +32 -23
@@ -2061,23 +2061,6 @@ static int trace__process_event(struct trace *trace, struct machine *machine, "LOST %" PRIu64 " events!\n", (u64)event->lost.lost); ret =machine__process_lost_event (machine, event, sample);break ; -case PERF_RECORD_FORK : -if (trace->raw_augmented_syscalls && - (augmented_syscalls__has_target_pid (event->fork.ppid) || -augmented_syscalls__has_target_pid (event->fork.ptid))) { -augmented_syscalls__add_target_pid (event->fork.pid); - } - ret =machine__process_fork_event (machine, event, sample); -break ; -case PERF_RECORD_EXIT : -if (trace->raw_augmented_syscalls) { -if (event->fork.pid == event->fork.tid) -augmented_syscalls__del_target_pid (event->fork.pid); -else -augmented_syscalls__del_target_pid (event->fork.tid); - } - ret =machine__process_exit_event (machine, event, sample); -break ;default : ret =machine__process_event (machine, event, sample);break ;
@@ -5015,6 +4998,22 @@ static int trace__run(struct trace *trace, int argc, const char **argv) } } +/* +* Everything the BPF programs filter on is now in their maps, so it is +* safe to let them run. They are attached system wide, so anything +* before this point would have been filtered against a map that was +* still being built up. +* +* Falling back to unaugmented tracing is no longer possible here: the +* evlist was built around __augmented_syscalls__ back in cmd_trace(), +* which is where that decision is taken and where the sys_enter evsel +* was dropped. Fail the session rather than run one that can report +* nothing. +*/ + err =augmented_syscalls__attach (); +if (err <0 ) + goto out_error_attach; +/* * If the "close" syscall is not traced, then we will not have the * opportunity to, in syscall_arg__scnprintf_close_fd() invalidate the
@@ -5209,6 +5208,16 @@ static int trace__run(struct trace *trace, int argc, const char **argv)fprintf (trace->output,"Failed to set the syscall filters: %s\n" ,str_error_r (-err, errbuf,sizeof (errbuf))); goto out_put_evlist; + +out_error_attach: +/* +* Use the returned error rather than errno: the failing attach is +* unwound before returning, and the libbpf calls that does can leave +* errno describing something else entirely. +*/ +fprintf (trace->output,"Failed to attach the augmented syscalls BPF programs: %s\n" , +str_error_r (-err, errbuf,sizeof (errbuf))); + goto out_put_evlist; } out_error_mem:fprintf (trace->output,"Not enough memory to run!\n" );
@@ -6125,7 +6134,7 @@ int cmd_trace(int argc, const char **argv) goto skip_augmentation; } - err =augmented_syscalls__prepare (); + err =augmented_syscalls__prepare (!trace.opts.no_inherit);if (err <0 ) goto skip_augmentation;
@@ -6148,11 +6157,11 @@ int cmd_trace(int argc, const char **argv) trace.syscalls.events.bpf_output =evlist__last (trace.evlist); }else {/* -* augmented_syscalls__prepare() already attached sys_enter and -* sys_exit, which are system wide. Falling through to -* skip_augmentation without undoing that would run a BPF -* program for every syscall on the machine, for the whole -* session, with nothing consuming the output. +* Drop the loaded skeleton before falling back to unaugmented +* tracing. Otherwise the setters called from trace__run() would +* still program its maps, and augmented_syscalls__attach() would +* then put system wide BPF programs on raw_syscalls for a +* session with nothing consuming their output. */ pr_debug ("Failed to create the augmented syscalls bpf-output event, disabling augmentation\n" );augmented_syscalls__cleanup ();
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +189 -10
260 lines not on this page · raw message
tools/perf/util/bpf_trace_augment.c +75 -45
168 lines not on this page · raw message
tools/perf/util/trace_augment.h +8 -20
@@ -10,14 +10,12 @@ struct evlist;#ifdef HAVE_BPF_SKEL -int augmented_syscalls__prepare (void ); +int augmented_syscalls__prepare (bool inherit); +int augmented_syscalls__attach (void );int augmented_syscalls__create_bpf_output (struct evlist *evlist);void augmented_syscalls__setup_bpf_output (void );int augmented_syscalls__set_filter_pids (unsigned int nr,pid_t *pids);int augmented_syscalls__set_target_pids (unsigned int nr,pid_t *pids); -int augmented_syscalls__add_target_pid (pid_t pid); -int augmented_syscalls__del_target_pid (pid_t pid); -bool augmented_syscalls__has_target_pid (pid_t pid);int augmented_syscalls__set_target_syscalls (unsigned int nr,int *syscall_ids,bool not_syscalls);int augmented_syscalls__get_map_fds (int *enter_fd,int *exit_fd,int *beauty_fd);struct bpf_program *augmented_syscalls__find_by_title (const char *name);
@@ -26,11 +24,16 @@ void augmented_syscalls__cleanup(void);#else /* !HAVE_BPF_SKEL */ -static inline int augmented_syscalls__prepare (void ) +static inline int augmented_syscalls__prepare (bool inherit__maybe_unused ) {return -1 ; } +static inline int augmented_syscalls__attach (void ) +{ +return 0 ; +} +static inline int augmented_syscalls__create_bpf_output (struct evlist *evlist__maybe_unused ) {return -1 ;
@@ -52,21 +55,6 @@ static inline int augmented_syscalls__set_target_pids(unsigned int nr __maybe_unreturn 0 ; } -static inline int augmented_syscalls__add_target_pid (pid_t pid__maybe_unused ) -{ -return 0 ; -} - -static inline int augmented_syscalls__del_target_pid (pid_t pid__maybe_unused ) -{ -return 0 ; -} - -static inline bool augmented_syscalls__has_target_pid (pid_t pid__maybe_unused ) -{ -return false; -} -static inline int augmented_syscalls__set_target_syscalls (unsigned int nr__maybe_unused ,int *syscall_ids__maybe_unused ,bool not_syscalls__maybe_unused )
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 08/18] perf trace: Handle fork and exit directly in BPF filter maps
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] perf: PID reuse TOCTOU race in sched_process_exec --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] perf: PID reuse TOCTOU race in sched_process_exec
--
commit d8a2403eb7c48846672bae9851f669ee1a4f9446
Author: Ian Rogers <[email protected]>
perf trace: Handle fork and exit directly in BPF filter maps
This commit shifts PID map filtering for fork and exit events from asynchronous userspace handling directly to BTF-typed BPF programs attached to scheduler tracepoints. This closes windows where events might be dropped due to PIDs being recycled before userspace processes the tracking events.
Quoted 3 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +0
-0
[ ... ]
Quoted 18 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +18
-0+SEC("tp_btf/sched_process_exec") +int BPF_PROG (sched_process_exec ,struct task_struct *p,pid_t old_pid) +{ +pid_t pid = p->pid; +bool val = true; + +if (pid == old_pid) +return 0 ; + +/* +* Drop the old key before adding the new one. The maps are bounded and +* the move is only ever a rename, but inserting first needs a spare +* slot for as long as both keys are present: on a full map that insert +* fails with -E2BIG while the delete still succeeds, which would lose +* the task rather than move it. Deleting first frees the slot the +* insert goes on to use. The task is mid exec and issues no syscalls in +* between, so the gap is not observable. +*
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c int sys_exit(struct syscall_exit_args *args) · line 893
Quoted 10 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +10
-0+* There is no atomic rename for a hash map, so on a map that is exactly +* full this narrows the window rather than closing it: a fork on +* another CPU can still take the freed slot before the insert below +* runs, and the task is then dropped just as any other task is once the +* map is full. +*/ +if (bpf_map_lookup_elem (&pids_to_trace, &old_pid) !=NULL ) { +bpf_map_delete_elem (&pids_to_trace, &old_pid); +bpf_map_update_elem (&pids_to_trace, &pid, &val,BPF_ANY ); + }
[Severity: Medium]
Does this sequence have a TOCTOU race with PID reuse?
When a non-leader thread calls execve, its old PID is released in de_thread() before the sched_process_exec tracepoint fires. If a concurrent fork on another CPU recycles this old PID before the tracepoint executes, could this BPF program inadvertently delete the new process's state from pids_to_trace, while simultaneously updating the exec'ing task's tracing state based on the recycled PID's state?
While PID wrap-around in this microsecond window might be rare in practice, can local userspace trigger this by racing execve and fork under heavy thread creation load, leading to corrupted trace states and silent drops for newly created tasks?
Quoted 8 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +8
-0+ +if (bpf_map_lookup_elem (&pids_filtered, &old_pid) !=NULL ) { +bpf_map_delete_elem (&pids_filtered, &old_pid); +bpf_map_update_elem (&pids_filtered, &pid, &val,BPF_ANY ); + } + +return 0 ; +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Ian Rogers
reviewerRe: [PATCH v4 08/18] perf trace: Handle fork and exit directly in BPF filter maps
Quoted 58 lines · On Fri, Sep 18, 2026 at 2:31 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] perf: PID reuse TOCTOU race insched_process_exec
--commit
d8a2403eb7c48846672bae9851f669ee1a4f9446
Author: Ian Rogers <[email protected]>perf trace: Handle fork and exit directly in BPF filter maps
This commit shifts PID map filtering for fork and exit events from asynchronous userspace handling directly to BTF-typed BPF programs attached to scheduler tracepoints. This closes windows where events might be dropped due to PIDs being recycled before userspace processes the tracking events.
Quoted 3 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +0
-0[ ... ]
Quoted 18 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +18
-0+SEC("tp_btf/sched_process_exec") +int BPF_PROG (sched_process_exec ,struct task_struct *p,pid_t old_pid) +{ +pid_t pid = p->pid; +bool val = true; + +if (pid == old_pid) +return 0 ; + +/* +* Drop the old key before adding the new one. The maps are bounded and +* the move is only ever a rename, but inserting first needs a spare +* slot for as long as both keys are present: on a full map that insert +* fails with -E2BIG while the delete still succeeds, which would lose +* the task rather than move it. Deleting first frees the slot the +* insert goes on to use. The task is mid exec and issues no syscalls in +* between, so the gap is not observable. +* tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c int sys_exit(struct syscall_exit_args *args) · line 893
Quoted 10 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +10
-0+* There is no atomic rename for a hash map, so on a map that is exactly +* full this narrows the window rather than closing it: a fork on +* another CPU can still take the freed slot before the insert below +* runs, and the task is then dropped just as any other task is once the +* map is full. +*/ +if (bpf_map_lookup_elem (&pids_to_trace, &old_pid) !=NULL ) { +bpf_map_delete_elem (&pids_to_trace, &old_pid); +bpf_map_update_elem (&pids_to_trace, &pid, &val,BPF_ANY ); + }[Severity: Medium]
Does this sequence have a TOCTOU race with PID reuse?When a non-leader thread calls execve, its old PID is released in
de_thread()before thesched_process_exectracepoint fires. If a concurrent fork on another CPU recycles this old PID before the tracepoint executes, could this BPF program inadvertently delete the new process's state frompids_to_trace, while simultaneously updating the exec'ing task's tracing state based on the recycled PID's state?
Quoted 4 lines
While PID wrap-around in this microsecond window might be rare in practice, can local userspace trigger this by racing execve and fork under heavy thread creation load, leading to corrupted trace states and silent drops for newly created tasks?
Recycling PIDs within that window is possible, but it would require such heavy load that fixing it isn't considered a worthwhile introduction of complexity. A longer LLM explanation follows.
Thanks,
Ian
Yes, and the window is wider than the question suggests. old_pid is sampled in exec_binprm() before the loader runs:
/* Need to fetch pid before load_binary changes it */ old_pid = current->pid; ... ret = search_binary_handler(bprm); ... trace_sched_process_exec(current, old_pid, bprm);
de_thread() is reached from begin_new_exec(), inside that search_binary_handler(). exchange_tids() hands the exec'ing thread the leader's pid, and release_task(leader) then frees the pid the thread started with, since the leader is holding it after the swap. old_pid is therefore up for reuse for the whole of the rest of the exec - mapping the binary and the interpreter, setup_arg_pages(), and any faults those take - and not merely for an instant either side of the tracepoint.
## What the race actually costs
Because the maps are keyed by pid alone, the exec'ing thread T (old tid P_old) and the task N that was given P_old share one key — sched_process_fork() inserting N just overwrites the slot. So the program cannot tell whose entry it is moving, and the outcome depends on who the surviving entry was really for:
T in map? | N in map? | Result ------------------------|------------------------|---------------------------------------------- yes | no | Move is correct —
nothing lost
yes | yes | One shared entry
moves to T; N loses it
no | yes | N's entry moves
to T; N loses it, T gains it
no | no | Lookup fails, no-op
The three harms that fall out of the two bad rows:
1. N silently loses its entry. In pids_to_trace it stops being reported for the rest of the session; in pids_filtered its exclusion is gone, so it starts being reported even though -- filter-pids named it.
2. N's descendants go with it. sched_process_fork() admits a child by looking up parent->pid, so once N's entry is gone nothing N forks afterwards is added either. The loss is a subtree, not one task.
3. T gains an entry it did not earn (bottom row only). In pids_to_trace a task outside the target set gets reported; in pids_filtered a task that was being traced is now silently excluded.
None of it self-heals — nothing re-adds N, and sched_process_exit() only ever deletes.
## Why leave it alone
1. Nothing here can tell the two apart. The value is a bool: presence says only "some task with this pid was of interest". Closing the race means putting a task identity in the value for the exec handler to compare against p — start time, say. But userspace seeds entries too, and /proc only publishes start time at clock-tick resolution, so the target's own threads enumerated at startup could carry no usable identity and would have to be waved through. Partial coverage for a fair amount of machinery.
2. It cannot be aimed at. A task can't request a pid — clone3()'s set_tid is gated on checkpoint_restore_ns_capable(). Everything else goes through idr_alloc_cyclic(pid_min, pid_max), so hitting P_old means getting the cursor the whole way round inside one exec, i.e. running the pid space near exhaustion rather than just forking hard.
3. It stays inside the session. Both maps only ever hold tasks perf trace was already following, so this is misattribution within the target set, not tracing escaping it. No privilege boundary is involved — perf trace could already trace both tasks.
Quoted 11 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c int sys_exit(struct syscall_exit_args *args) · line 901
Quoted 8 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +8
-0+ +if (bpf_map_lookup_elem (&pids_filtered, &old_pid) !=NULL ) { +bpf_map_delete_elem (&pids_filtered, &old_pid); +bpf_map_update_elem (&pids_filtered, &pid, &val,BPF_ANY ); + } + +return 0 ; +}--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Ian Rogers
reviewerPATCH v4 9/18 perf trace: Enumerate the target again once BPF is attached
In reply to Ian Rogersevlist__create_maps() reads the target out of /proc, and the BPF sched_process_fork() program only sees what is cloned once it is attached. A task the target creates between the two is in neither, and since cmd_trace() drops the sys_enter evsel in favour of __augmented_syscalls__ there is no other source of enter events. Such a task, and in turn everything it forks, goes unreported for the rest of the session.
Read the target out of /proc once more, after the attach. Whatever existed before the programs went live is there to be found, and whatever is created after it is sched_process_fork()'s to add, so between them nothing is left out. Doing this before the attach instead would only move the window rather than close it.
The enumeration follows the same rule as the BPF programs, which is that the maps name individual tasks:
- -p names a process, so its thread group is read from /proc/<pid>/task.
- -t names a thread, which is taken on its own. Expanding it to its thread group would trace the siblings it asked to leave out.
- Descendants come from task->children, read through /proc/<pid>/task/<tid>/children. A forked task leads a thread group of its own, so each one found is walked in turn and a tree of any depth is covered. New threads are not listed there, copy_process() gives a CLONE_THREAD child the real_parent of its creator rather than the creator itself, but the thread group walk above has them.
The other two ways of choosing what to trace need nothing, for the same reasons the window never affected them: 'perf trace -a' does not filter on pid at all, and evlist__prepare_workload() keeps a workload blocked on a pipe until evlist__start_workload(), well after the attach.
A target that exits during startup is not an error. Reading /proc for a task that has gone fails with ENOENT, and a task directory that is read but has nothing in it sets nothing at all, so errno is cleared before the enumeration and only an allocation failure is passed back. Anything else leaves the tasks that evlist__create_maps() already found in the map, which sched_process_exit() takes out again as they die, and the session runs on rather than being ended over a target that was going to stop producing events anyway.
What is left is smaller and no longer lasts. A task found here may have made syscalls between sys_enter going live and it being added to the map, and those are not reported, but it is traced from that point on. On a kernel built without CONFIG_PROC_CHILDREN the children files are absent and descendants cannot be named, leaving the threads of the target, which are still picked up.
pid_t, PATH_MAX, FILE and the directory reading are all used directly by the new code, so <sys/types.h>, <limits.h>, <stdio.h> and <dirent.h> are included rather than relied upon to arrive through another header.
Nothing is read out of /proc twice. A pid is in the collection only because the task directory holding it was read, and that directory holds the whole thread group, so both the directory and the children files of everything in it have been read already. 'perf trace -p' needs this: the thread map names every thread of the target, each of them is queued as something to expand, and they all stand for the same directory, so a target of N threads was read N times over and a children file was opened N squared times. On a 64 thread target that is 4097 of them, against 65 once the ones already read are left alone.
--- tools/perf/builtin-trace.c | 248 +++++++++++++++++++++++++++++++++++++ 1 file changed, 248 insertions(+)
tools/perf/builtin-trace.c +248 -0
@@ -15,6 +15,7 @@*/ #include "util/record.h" +#include <api/fs/fs.h> #include <api/fs/tracing_path.h> #ifdef HAVE_LIBBPF_SUPPORT #include <bpf/bpf.h>
@@ -65,11 +66,15 @@#include "trace_augment.h" #include "dwarf-regs.h" +#include <dirent.h> #include <errno.h> #include <sys/stat.h> +#include <sys/types.h> #include <inttypes.h> +#include <limits.h> #include <poll.h> #include <signal.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> #include <linux/err.h>
@@ -4648,6 +4653,239 @@ static int trace__set_filter_pids(struct trace *trace)return err; } +/* A list of pids that grows as it is added to, holding each pid once. */ +struct pid_list { +pid_t *entries; +size_t nr; +size_t allocated; +}; + +static bool pid_list__has (const struct pid_list *list,pid_t pid) +{ +for (size_t i =0 ; i < list->nr; i++) { +if (list->entries[i] == pid) +return true; + } +return false; +} + +/* Append pid, unless it is already there. */ +static int pid_list__add (struct pid_list *list,pid_t pid) +{ +if (pid_list__has (list, pid)) +return 0 ; + +if (list->nr == list->allocated) { +size_t allocated = list->allocated ? list->allocated *2 :32 ; +pid_t *entries =realloc (list->entries, allocated *sizeof (*entries)); + +if (entries ==NULL ) +return -ENOMEM ; + + list->entries = entries; + list->allocated = allocated; + } + + list->entries[list->nr++] = pid; +return 0 ; +} + +static void pid_list__exit (struct pid_list *list) +{ +zfree (&list->entries); + list->nr =0 ; + list->allocated =0 ; +} + +/* +* Append the tasks tid has forked to tgids. +* +* task->children holds what a task forked, and a forked task leads a thread +* group of its own, so each is something to expand in turn. New threads are +* not listed: copy_process() gives a CLONE_THREAD child the real_parent of +* its creator rather than the creator itself, so a thread is a sibling of the +* task that created it. Those are enumerated from the task directory instead. +*/ +static int pid_list__add_children (struct pid_list *tgids,pid_t tid) +{ +char path[PATH_MAX ]; +pid_t child; +FILE *fp; +int err =0 ; + +scnprintf (path,sizeof (path),"%s/%d/task/%d/children" , +procfs__mountpoint (), tid, tid); + fp =fopen (path,"r" ); +/* +* Absent if the task exited, and on a kernel built without +* CONFIG_PROC_CHILDREN. Neither is worth failing for: what is missed +* is a task that has gone away, or descendants the kernel will not +* name. +*/ +if (fp ==NULL ) +return 0 ; + +while (fscanf (fp,"%d" , &child) ==1 ) { + err =pid_list__add (tgids, child); +if (err) +break ; + } + +fclose (fp); +return err; +} + +/* +* Collect the tasks to trace: the target, its threads, and everything they +* have forked. +* +* tgids is the queue of thread groups still to expand. It is walked as it +* grows, so a child found here has its own children picked up in a later +* pass and the depth of the tree does not matter. The walk terminates +* because a task cannot be its own ancestor and pid_list__add() ignores a +* pid that is already listed. +*/ +static int trace__collect_target_pids (struct trace *trace,struct pid_list *pids) +{ +struct target *target = &trace->opts.target; +/* +* -p names processes, so the whole thread group is a target. -t names +* threads, and expanding one to its group would trace the siblings +* that were deliberately left out. +*/ +bool whole_group = target->pid !=NULL ; +struct perf_thread_map *threads; +struct pid_list tgids = {}; +int err =0 ; + +/* Enumerate the target as evlist__create_maps() did, but now. */ + errno =0 ; + threads =thread_map__new_str (target->pid, target->tid, target->per_thread); +if (threads ==NULL ) { +char bf[128 ]; + +/* +* A target that exited while perf trace was starting up shows +* up here as a failure to read /proc/<pid>/task, with scandir() +* setting ENOENT, or as a task directory that is read but has +* nothing in it, which sets nothing at all and is why errno is +* cleared above. Neither is worth ending the session for: the +* tasks the target had are already in the map from +* evlist__create_maps() and sched_process_exit() takes them out +* again as they die. Carry on with what is known and let only +* an allocation failure through, matching how the pid_list +* additions below are treated. +*/ +if (errno ==ENOMEM ) +return -ENOMEM ; + +pr_debug ("Couldn't enumerate the target again (%s), tracing the tasks already known\n" , + errno ==0 ?"it exited" :str_error_r (errno, bf,sizeof (bf))); +return 0 ; + } + +for (int i =0 ; i <perf_thread_map__nr (threads); i++) { +pid_t pid =perf_thread_map__pid (threads, i); + + err =pid_list__add (whole_group ? &tgids : pids, pid); +/* A thread named by -t is not expanded, but its children are. */ +if (!err && !whole_group) + err =pid_list__add_children (&tgids, pid); +if (err) + goto out; + } + +for (size_t i =0 ; i < tgids.nr; i++) { +pid_t tgid = tgids.entries[i]; +char path[PATH_MAX ]; +struct dirent *dent; +DIR *tasks; + +/* +* A pid is in pids only because the task directory holding it +* was read, and that directory holds the whole thread group, +* so this one has been read already. 'perf trace -p' relies on +* this: the thread map names every thread of the target, each +* of them is queued here, and they all stand for the same +* directory, which would otherwise be read once per thread. +*/ +if (pid_list__has (pids, tgid)) +continue ; + +scnprintf (path,sizeof (path),"%s/%d/task" ,procfs__mountpoint (), tgid); + tasks =opendir (path); +if (tasks ==NULL ) +continue ;/* Exited between being named and being read. */ + +while ((dent =readdir (tasks)) !=NULL ) { +char *end; +pid_t tid =strtol (dent->d_name, &end,10 ); + +/* Skip "." and "..". */ +if (*end !='\0' ) +continue ; + +/* Seen before, so its children have been read too. */ +if (pid_list__has (pids, tid)) +continue ; + + err =pid_list__add (pids, tid); +if (!err) + err =pid_list__add_children (&tgids, tid); +if (err) +break ; + } + +closedir (tasks); +if (err) + goto out; + } +out: +perf_thread_map__put (threads); +pid_list__exit (&tgids); +return err; +} + +/* +* Trace the tasks that appeared while perf trace was starting up. +* +* evlist__create_maps() enumerated the target from /proc, and +* sched_process_fork() only sees what is cloned once it is attached. A task +* created in between is in neither, and since cmd_trace() drops the sys_enter +* evsel in favour of __augmented_syscalls__ it would go unreported for the +* whole session. +* +* Read the target out of /proc again now that the programs are attached. +* Whatever came before the attach is in /proc to be found, and whatever comes +* after it is sched_process_fork()'s to add, so between them nothing is left +* out. +* +* Doing this after the attach rather than before it is what makes that true; +* before it would only move the window. The cost is that a task found here +* may have made syscalls between sys_enter going live and it being added +* below, and those are not reported. It is traced from that point on. +*/ +static int trace__set_startup_pids (struct trace *trace) +{ +struct pid_list pids = {}; +int err; + +/* +* Nothing to do without a target: 'perf trace -a' does not filter on +* pid at all, and evlist__prepare_workload() keeps a workload blocked +* on a pipe until evlist__start_workload(), well after the attach. +*/ +if (!target__has_task (&trace->opts.target)) +return 0 ; + + err =trace__collect_target_pids (trace, &pids); +if (!err) + err =augmented_syscalls__set_target_pids (pids.nr, pids.entries); + +pid_list__exit (&pids); +return err; +} +static int __trace__deliver_event (struct trace *trace,union perf_event *event) {struct evlist *evlist = trace->evlist;
@@ -5014,6 +5252,16 @@ static int trace__run(struct trace *trace, int argc, const char **argv)if (err <0 ) goto out_error_attach; +/* +* With the programs live, anything the target created while they were +* being set up is now sched_process_fork()'s to keep track of, but it +* was not there to see it appear. Enumerate the target once more to +* pick those up. +*/ + err =trace__set_startup_pids (trace); +if (err <0 ) + goto out_error_filter_pids; +/* * If the "close" syscall is not traced, then we will not have the * opportunity to, in syscall_arg__scnprintf_close_fd() invalidate the
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 09/18] perf trace: Enumerate the target again once BPF is attached
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 14 lines
evlist__create_maps()reads the target out of /proc, and the BPFsched_process_fork()program only sees what is cloned once it is attached. A task the target creates between the two is in neither, and sincecmd_trace()drops thesys_enterevsel in favour of__augmented_syscalls__there is no other source of enter events. Such a task, and in turn everything it forks, goes unreported for the rest of the session.Read the target out of /proc once more, after the attach. Whatever existed before the programs went live is there to be found, and whatever is created after it is sched_process_fork()'s to add, so between them nothing is left out. Doing this before the attach instead would only move the window rather than close it.
Quoted 7 lines
The enumeration follows the same rule as the BPF programs, which is that [ ... ]
N squared times. On a 64 thread target that is 4097 of them, against 65 once the ones already read are left alone.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
Ian Rogers
reviewerPATCH v4 10/18 perf trace: Drop targets that died before they were filtered
In reply to Ian Rogerssched_process_exit() takes a task out of pids_to_trace and pids_filtered as it dies, which keeps the maps holding live tasks rather than growing for the length of the session. It only finds a task there if userspace put it there first, and userspace writes a pid some time after reading it:
- trace__set_filter_pids() writes the thread map and the --filter-pids list before the programs are attached, so for those there is nothing watching them die at all.
- trace__set_startup_pids() writes what it enumerated after the attach, which is a much smaller gap but still a gap.
A task that dies inside one of those windows is never evicted, because the delete that would have done it ran while the map had nothing to delete. Its pid then sits in the map for the rest of the session: it holds one of a fixed number of entries, and pids are reused, so the unrelated task that eventually receives it is treated as the target, or as a task the user asked to leave out and is then silently not reported.
Walk both maps once the last write to them is done and remove the tasks that are not alive. Sweeping the maps rather than the lists that were written to them covers every way a pid can get in, including the pid of perf itself and of the terminal it was started from, which trace__set_filter_loop_pids() adds without recording anywhere. A task found alive here and dying later is one sched_process_exit() can see and evict, so a single pass is enough, and the tasks the BPF programs add for themselves need nothing: sched_process_fork() inserts a task before it has run, so it cannot have died beforehand.
Deleting the key the walk is standing on leaves bpf_map__get_next_key() to resume from a key that is no longer there, which htab_map_get_next_key() answers with the first key of the whole map, starting the walk over. The walk therefore only ever stands on a key it has found alive, which is one it has just decided to keep, so nothing it deletes is ever the key it would go on to ask from. A key deleted while the walk stands on nothing is not a problem either: the next request is for the first key of the map, and the deleted one is no longer it.
sched_process_exit() deletes keys while this runs and can take out the key the walk is standing on, which there is no way to prevent or to notice. The walk is bounded at max_entries steps so that it ends however often that happens: the map holds no more than that many keys, so a walk that has taken as many steps has either seen them all or been restarted, and the bound is what stops a walk that keeps being restarted from going round for ever. Ending on the bound can leave a dead task behind, which costs one entry of a great many until the session ends, where not ending would cost the session itself.
A task is taken to be gone when its /proc entry is not there, and also when /proc reports it as Z or X. Both mean it is in or past do_exit(), which is where sched_process_exit() runs, so waiting for a zombie to be reaped before dropping it would only keep a dead task in the map for longer.
Everything else, including not being able to tell, leaves the task in the map. The two mistakes are not equal: a dead task left there holds an entry nothing needs, while a live one taken out stops being traced for the rest of the session, so a read that fails for perf's own reasons, being out of file descriptors or of memory, is not read as the task having exited.
Reading that state means finding the end of the command name, which do_task_stat() writes out unescaped, unlike /proc/<pid>/status: it holds whatever the task called itself, ')' and newlines included. The whole of /proc/<pid>/stat is read rather than a line of it, because a line stops at the first embedded newline, short of the name's closing ')', and the last ')' in the file is taken as that one, since no field after the name has parentheses in it.
--- tools/perf/builtin-trace.c | 76 ++++++++++++++++++++++++--- tools/perf/util/bpf_trace_augment.c | 79 +++++++++++++++++++++++++++++ tools/perf/util/trace_augment.h | 6 +++ 3 files changed, 153 insertions(+), 8 deletions(-)
tools/perf/builtin-trace.c +68 -8
@@ -4846,6 +4846,59 @@ static int trace__collect_target_pids(struct trace *trace, struct pid_list *pidsreturn err; } +/* +* Is the task still running? +* +* A pid with no /proc entry is gone, and one reported as Z (zombie) or X (dead) +* is in or past do_exit(), which is where sched_process_exit() runs. Either way +* the BPF programs will never hear about it again. +* +* Anything that is not one of those, including being unable to tell, is taken +* as alive. The two mistakes are not equal: a dead task left in the map holds +* an entry that nothing needs, while a live one taken out of it stops being +* traced for the rest of the session. +*/ +static bool trace__task_is_alive (pid_t pid) +{ +char path[PATH_MAX ]; +const char *state; +char *stat =NULL ; +bool alive = true; +size_t len; +int err; + +scnprintf (path,sizeof (path),"%s/%d/stat" ,procfs__mountpoint (), pid); + err =filename__read_str (path, &stat, &len); +if (err) { +/* +* ENOENT and ESRCH are the task being gone, which is what this +* is looking for. Any other error is perf's own, running out +* of file descriptors or of memory, and says nothing about the +* task. +*/ +return err != -ENOENT && err != -ESRCH ; + } + +/* +* The state is the field after the command name. do_task_stat() writes +* that name out as it is, so it can hold anything a task cares to call +* itself, ')' and newlines included. Nothing after it has parentheses, +* so the last ')' in the file is the one that closes it; the whole file +* is read rather than a line of it because a line stops at the first of +* those newlines, short of the ')' that is being looked for. +* +* A read that returns something the state cannot be read out of, such +* as the empty result of the task exiting midway through it, leaves the +* task alive by the rule above. +*/ + state =strrchr (stat,')' ); +if (state !=NULL && state[1 ] ==' ' ) + alive = state[2 ] !='Z' && state[2 ] !='X' ; + +free (stat); +return alive; +} +/* * Trace the tasks that appeared while perf trace was starting up. *
@@ -4868,19 +4921,26 @@ static int trace__collect_target_pids(struct trace *trace, struct pid_list *pidsstatic int trace__set_startup_pids (struct trace *trace) {struct pid_list pids = {}; -int err; +int err =0 ;/* -* Nothing to do without a target: 'perf trace -a' does not filter on -* pid at all, and evlist__prepare_workload() keeps a workload blocked -* on a pipe until evlist__start_workload(), well after the attach. +* Only a target is enumerated: 'perf trace -a' does not filter on pid +* at all, and evlist__prepare_workload() keeps a workload blocked on a +* pipe until evlist__start_workload(), well after the attach. */ -if (!target__has_task (&trace->opts.target)) -return 0 ; +if (target__has_task (&trace->opts.target)) { + err =trace__collect_target_pids (trace, &pids); +if (!err) + err =augmented_syscalls__set_target_pids (pids.nr, pids.entries); + } - err =trace__collect_target_pids (trace, &pids); +/* +* Every session gets the sweep, target or not: --filter-pids names +* tasks to leave out with no target of its own, and those are written +* to a map of their own, before the attach and with the same race. +*/ if (!err) - err =augmented_syscalls__set_target_pids (pids.nr, pids.entries); + err =augmented_syscalls__prune_dead_pids (trace__task_is_alive);pid_list__exit (&pids);return err;
tools/perf/util/bpf_trace_augment.c +79 -0
85 lines not on this page · raw message
tools/perf/util/trace_augment.h +6 -0
@@ -16,6 +16,7 @@ int augmented_syscalls__create_bpf_output(struct evlist *evlist);void augmented_syscalls__setup_bpf_output (void );int augmented_syscalls__set_filter_pids (unsigned int nr,pid_t *pids);int augmented_syscalls__set_target_pids (unsigned int nr,pid_t *pids); +int augmented_syscalls__prune_dead_pids (bool (*is_alive)(pid_t pid));int augmented_syscalls__set_target_syscalls (unsigned int nr,int *syscall_ids,bool not_syscalls);int augmented_syscalls__get_map_fds (int *enter_fd,int *exit_fd,int *beauty_fd);struct bpf_program *augmented_syscalls__find_by_title (const char *name);
@@ -55,6 +56,11 @@ static inline int augmented_syscalls__set_target_pids(unsigned int nr __maybe_unreturn 0 ; } +static inline int augmented_syscalls__prune_dead_pids (bool (*is_alive)(pid_t pid)__maybe_unused ) +{ +return 0 ; +} +static inline int augmented_syscalls__set_target_syscalls (unsigned int nr__maybe_unused ,int *syscall_ids__maybe_unused ,bool not_syscalls__maybe_unused )
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 10/18] perf trace: Drop targets that died before they were filtered
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 19 lines
sched_process_exit()takes a task out ofpids_to_traceandpids_filteredas it dies, which keeps the maps holding live tasks rather than growing for the length of the session. It only finds a task there if userspace put it there first, and userspace writes a pid some time after reading it:-
trace__set_filter_pids()writes the thread map and the --filter-pids list before the programs are attached, so for those there is nothing watching them die at all.
-trace__set_startup_pids()writes what it enumerated after the attach, which is a much smaller gap but still a gap.A task that dies inside one of those windows is never evicted, because the delete that would have done it ran while the map had nothing to delete. Its pid then sits in the map for the rest of the session: it [ ... ]
last ')' in the file is taken as that one, since no field after the name has parentheses in it.
Quoted 2 lines
Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Ian Rogers
reviewerPATCH v4 11/18 perf test test_task_analyzer: Isolate in temporary directory and make non-exclusive
In reply to Ian Rogerstest_task_analyzer.sh writes perf.data and temporary files directly into the current working directory, causing collisions when running tests in parallel.
As a temporary measure until perf script report supports an input file option, resolve perfdir to an absolute path, change directory into $tmpdir for the test duration, and clean up in the exit trap. Remove the (exclusive) tag so the test runs in parallel.
perfdir is derived from $0, which may be relative, so it has to be resolved before the cd into $tmpdir, otherwise both PERF_EXEC_PATH and the cleanup trap point at paths that no longer exist.
--- tools/perf/tests/shell/test_task_analyzer.sh | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)
tools/perf/tests/shell/test_task_analyzer.sh +15 -5
@@ -1,8 +1,18 @@ #!/bin/bash -# perf script task-analyzer tests (exclusive) +# perf script task-analyzer tests # SPDX-License-Identifier: GPL-2.0 -tmpdir=$(mktemp -d /tmp/perf-script-task-analyzer-XXXXX) +# Resolve the source directory before changing the working directory below, +# $0 may be a relative path and would no longer resolve from $tmpdir. +perfdir=$(cd "$(dirname "$0")/../.." && pwd) + +tmpdir=$(mktemp -d /tmp/perf-script-task-analyzer-XXXXX) || exit 1 +# The cleanup trap is only installed further down, once the functions it +# calls have been defined, so tidy up by hand if this cd fails. +cd "$tmpdir" || { + rmdir "$tmpdir" + exit 1 +} # TODO: perf script report only supports input from the CWD perf.data file, make # it support input from any file. perfdata="perf.data"
@@ -11,7 +21,6 @@ csvsummary="$tmpdir/csvsummary" err=0 # set PERF_EXEC_PATH to find scripts in the source directory -perfdir=$(dirname "$0")/../.. if [ -e "$perfdir/scripts/python/Perf-Trace-Util" ]; then export PERF_EXEC_PATH=$perfdir fi
@@ -20,8 +29,9 @@ fi export ASAN_OPTIONS=detect_leaks=0 cleanup() { - rm -f "${perfdata}" - rm -f "${perfdata}".old + # Step out of $tmpdir before removing it. The removal uses an absolute + # path and works from anywhere, so a failure to cd must not skip it. + cd "$perfdir" || cd / || true rm -rf "$tmpdir" trap - exit term int
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 11/18] perf test test_task_analyzer: Isolate in temporary directory and make non-exclusive
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
test_task_analyzer.shwrites perf.data and temporary files directly into the current working directory, causing collisions when running tests in parallel.As a temporary measure until
perf script reportsupports an input file option, resolve perfdir to an absolute path, change directory into $tmpdir for the test duration, and clean up in the exit trap. Remove the (exclusive) tag so the test runs in parallel.
Quoted 6 lines
perfdir is derived from $0, which may be relative, so it has to be resolved before the cd into $tmpdir, otherwise both
PERF_EXEC_PATHand the cleanup trap point at paths that no longer exist.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
Ian Rogers
reviewerPATCH v4 12/18 perf test common: Only disable probes in clear_all_probes
In reply to Ian Rogersclear_all_probes() began by writing 0 to /sys/kernel/debug/tracing/events/enable, which disables every tracepoint on the system rather than just the probes the probe tests created. When tests run in parallel that also silences the events of any concurrent perf record, perf trace or ftrace session, so unrelated tests lose the events they are waiting for and fail.
The write cannot simply be dropped. Clearing kprobe_events or uprobe_events is all or nothing: dyn_events_release_all() walks every probe of that type first and returns -EBUSY without removing any of them if it finds one that is still enabled, where enabled means TP_FLAG_TRACE from tracefs or TP_FLAG_PROFILE from a perf session. A probe left enabled through tracefs would therefore block the whole clear and leave stale probes behind to collide with the next run.
Disable the probes and only the probes. The set to disable is taken from the kprobe_events and uprobe_events listings rather than from a guess at which groups perf uses, so it matches what dyn_events_release_all() is going to inspect: a probe an unrelated session left enabled in, say, the kprobes group would otherwise still abort the clear.
--- tools/perf/tests/shell/common/init.sh | 33 ++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-)
tools/perf/tests/shell/common/init.sh +32 -1
@@ -130,9 +130,40 @@ check_uprobes_available() test -e /sys/kernel/debug/tracing/uprobe_events } +# Disable every kprobe and uprobe event. The listings name each probe as +# "TYPE:GROUP/EVENT ARGS...", for instance "p:probe/vfs_read vfs_read", and +# events/GROUP/EVENT/enable is the switch for it. +disable_all_probes() +{ + PROBE_SPECS=`cat /sys/kernel/debug/tracing/kprobe_events \ + /sys/kernel/debug/tracing/uprobe_events 2> /dev/null | + cut -d ' ' -f 1` + for PROBE_SPEC in $PROBE_SPECS + do + case "$PROBE_SPEC" in + *:*/*) ;; + *) continue ;; + esac + PROBE_ENABLE="/sys/kernel/debug/tracing/events/${PROBE_SPEC#*:}/enable" + test -e "$PROBE_ENABLE" && echo 0 > "$PROBE_ENABLE" + done +} + clear_all_probes() { - echo 0 > /sys/kernel/debug/tracing/events/enable + # Disable the probes before removing them. Writing to kprobe_events or + # uprobe_events is all or nothing: dyn_events_release_all() walks every + # probe of that type first and returns -EBUSY without removing any of + # them if it finds one that is still enabled, which would leave stale + # probes behind to collide with the next run. That covers probes this + # test suite never created, so disable all of them and not just the + # ones in the groups perf uses. + # + # Only probes are disabled. Writing to events/enable would also silence + # the tracepoints of any perf record, perf trace or ftrace session + # sharing the machine, which breaks those tests when they run in + # parallel with this one. + disable_all_probes check_kprobes_available && echo > /sys/kernel/debug/tracing/kprobe_events check_uprobes_available && echo > /sys/kernel/debug/tracing/uprobe_events }
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 12/18] perf test common: Only disable probes in clear_all_probes
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines
clear_all_probes()began by writing 0 to/sys/kernel/debug/tracing/events/enable, which disables every tracepoint on the system rather than just the probes the probe tests created. When tests run in parallel that also silences the events of any concurrent perf record, perf trace or ftrace session, so unrelated tests lose the events they are waiting for and fail.The write cannot simply be dropped. Clearing
kprobe_eventsoruprobe_eventsis all or nothing:dyn_events_release_all()walks every probe of that type first and returns-EBUSYwithout removing any of them if it finds one that is still enabled, where enabled meansTP_FLAG_TRACEfrom tracefs orTP_FLAG_PROFILEfrom a perf session. A probe left enabled through tracefs would therefore block the whole clear and leave stale probes behind to collide with the next run.
Quoted 8 lines
Disable the probes and only the probes. The set to disable is taken from the
kprobe_eventsanduprobe_eventslistings rather than from a guess at which groups perf uses, so it matches whatdyn_events_release_all()is going to inspect: a probe an unrelated session left enabled in, say, the kprobes group would otherwise still abort the clear.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
Ian Rogers
reviewerPATCH v4 13/18 perf test probe_vfs_getname: Scope probe name to PID and make non-exclusive
In reply to Ian RogersThe probe name vfs_getname was hardcoded, causing collisions when tests ran concurrently. Furthermore, cleanup_probe_vfs_getname() used perf probe -d probe:vfs_getname*, deleting probes registered by other parallel tests.
Scope the probe name to the pid, and rename it to getname_flags_$$ so that it no longer begins with "vfs_getname". perf trace calls evlist__add_vfs_getname(), which opens every event matching a hardcoded "probe:vfs_getname*" wildcard, so a perf trace run by any other test would otherwise pin this probe and make perf probe -d fail with -EBUSY. That also unblocks making the perf trace tests non-exclusive later in this series.
Enumerate the probes to record and to delete from perf probe -l, matching ^probe:${vfs_getname}(_[[:digit:]]+)?$ exactly, rather than globbing on ${vfs_getname}*. perf probe appends _1, _2, ... when getname_flags is inlined at more than one call site, so the variants do have to be matched, but since the name now ends in a pid a trailing wildcard would also match the probes of a test whose pid merely starts with this one's, e.g. 123 and 1234.
Remove the (exclusive) tag from probe_vfs_getname.sh and record+script_probe_vfs_getname.sh so they run concurrently in pass 1. trace+probe_vfs_getname.sh has to stay exclusive: it is the one test that wants to be discovered by that wildcard, so it sets vfs_getname to a "vfs_getname_$$" name before sourcing the library, and would then pin its siblings' probes if it ran alongside them. A comment in the test records this.
Delete the probes from an exit trap as well as on the way out. A pid scoped name is never seen again, so a run interrupted before cleanup_probe_vfs_getname() would leave its probes behind for good, accumulating a set per run. The fixed name at least meant the next run found, and went on to reuse, whatever the last one left.
--- .../perf/tests/shell/lib/probe_vfs_getname.sh | 51 +++++++++++++++++-- tools/perf/tests/shell/probe_vfs_getname.sh | 3 +- .../shell/record+script_probe_vfs_getname.sh | 18 +++++-- .../tests/shell/trace+probe_vfs_getname.sh | 9 ++++ 4 files changed, 71 insertions(+), 10 deletions(-)
tools/perf/tests/shell/lib/probe_vfs_getname.sh +47 -4
@@ -1,15 +1,58 @@ #!/bin/bash # Arnaldo Carvalho de Melo <[email protected]>, 2017 -perf probe -l 2>&1 | grep -q probe:vfs_getname +# The name of the getname_flags probe added and removed below. +# +# It is scoped to the pid so that tests running in parallel do not collide, +# and it deliberately does not start with "vfs_getname": perf trace calls +# evlist__add_vfs_getname(), which opens everything matching the hardcoded +# "probe:vfs_getname*" wildcard, so a perf trace running in another test would +# otherwise pin this probe and make the 'perf probe -d' below fail with -EBUSY. +# +# trace+probe_vfs_getname.sh is the one test that does want to be found that +# way, so it sets vfs_getname itself before sourcing this file, and is +# (exclusive) as a result. +: "${vfs_getname:=getname_flags_$$}" + +# Print the probes add_probe_vfs_getname() created. perf probe appends _1, _2, +# ... when getname_flags is inlined at more than one call site, so there can be +# several. Match them exactly rather than with a "${vfs_getname}*" glob: the +# name ends in a pid, so such a glob would also match the probes of a test +# whose pid merely starts with this one's, e.g. 123 and 1234. +probes_vfs_getname() { + perf probe -l 2>/dev/null | awk '{print $1}' | + grep -E "^probe:${vfs_getname}(_[[:digit:]]+)?$" +} + +[ -n "$(probes_vfs_getname)" ] had_vfs_getname=$? cleanup_probe_vfs_getname() { if [ $had_vfs_getname -eq 1 ] ; then - perf probe -q -d probe:vfs_getname* + local probe + for probe in $(probes_vfs_getname); do + perf probe -q -d "$probe" + done fi } +# Delete the probes however the test ends, not just when it runs to +# completion. The name is scoped to the pid, so nothing that runs later +# reuses or tidies up a probe an interrupted test left behind, and they +# would accumulate one set per run. The fixed name used before was at least +# picked up again by the next run. +# +# Tests may still call cleanup_probe_vfs_getname directly. Doing so leaves +# nothing for probes_vfs_getname to find, so the trap below then does +# nothing. A test needing cleanup of its own should call +# cleanup_probe_vfs_getname from its own exit trap, since installing one +# replaces this rather than adding to it. +trap cleanup_probe_vfs_getname exit +# Turn a signal into an ordinary exit so that the exit trap above runs. An +# exit trap that returns leaves the exit status alone, so a test exiting 2 +# to skip still skips. +trap 'exit 1' term int + add_probe_vfs_getname() { add_probe_verbose=$1 if [ $had_vfs_getname -eq 1 ] ; then
@@ -33,8 +76,8 @@ add_probe_vfs_getname() { return 2 fi - perf probe -q "vfs_getname=getname_flags:${line} pathname=result->name:string" || \ - perf probe $add_probe_verbose "vfs_getname=getname_flags:${line} pathname=filename:ustring" || return 1 + perf probe -q "${vfs_getname}=getname_flags:${line} pathname=result->name:string" || \ + perf probe $add_probe_verbose "${vfs_getname}=getname_flags:${line} pathname=filename:ustring" || return 1 fi }
tools/perf/tests/shell/probe_vfs_getname.sh +1 -2
@@ -1,6 +1,5 @@ #!/bin/bash -# Add vfs_getname probe to get syscall args filenames (exclusive) - +# Add vfs_getname probe to get syscall args filenames # SPDX-License-Identifier: GPL-2.0 # Arnaldo Carvalho de Melo <[email protected]>, 2017
tools/perf/tests/shell/record+script_probe_vfs_getname.sh +14 -4
@@ -1,5 +1,5 @@ #!/bin/bash -# Use vfs_getname probe to get syscall args filenames (exclusive) +# Use vfs_getname probe to get syscall args filenames # Uses the 'perf test shell' library to add probe:vfs_getname to the system # then use it with 'perf record' using 'touch' to write to a temp file, then
@@ -17,22 +17,32 @@ skip_if_no_perf_probe || exit 2 # shellcheck source=lib/probe_vfs_getname.sh . "$(dirname "$0")/lib/probe_vfs_getname.sh" +# shellcheck disable=SC2154 # vfs_getname is assigned in lib/probe_vfs_getname.sh record_open_file() { echo "Recording open file:" # Check presence of libtraceevent support to run perf record - skip_no_probe_record_support "probe:vfs_getname*" + skip_no_probe_record_support if [ $? -eq 2 ]; then echo "WARN: Skipping test record_open_file. No libtraceevent support" return 2 fi - perf record -o ${perfdata} -e probe:vfs_getname\* touch $file + # Record every probe the inlining of getname_flags produced, naming + # them exactly rather than with a "${vfs_getname}*" glob, which would + # also match the probes of a test whose pid starts with this one's. + local events + events=$(probes_vfs_getname | paste -sd, -) + if [ -z "${events}" ] ; then + echo "FAIL: no ${vfs_getname} probe to record" + return 1 + fi + perf record -o ${perfdata} -e "${events}" touch $file } perf_script_filenames() { echo "Looking at perf.data file for vfs_getname records for the file we touched:" perf script -i ${perfdata} | \ - grep -E " +touch +[0-9]+ +\[[0-9]+\] +[0-9]+\.[0-9]+: +probe:vfs_getname[_0-9]*: +\([[:xdigit:]]+\) +pathname=\"${file}\"" + grep -E " +touch +[0-9]+ +\[[0-9]+\] +[0-9]+\.[0-9]+: +probe:${vfs_getname}(_[0-9]+)?: +\([[:xdigit:]]+\) +pathname=\"${file}\"" } add_probe_vfs_getname
tools/perf/tests/shell/trace+probe_vfs_getname.sh +9 -0
@@ -10,6 +10,13 @@ # SPDX-License-Identifier: GPL-2.0 # Arnaldo Carvalho de Melo <[email protected]>, 2017 +# This test must stay exclusive, and is the only one of the probe tests that +# does: it does not name the event it uses. perf trace discovers it with the +# hardcoded "probe:vfs_getname*" wildcard in evlist__add_vfs_getname(), so the +# probe has to carry that prefix, and a parallel run of this test would then +# also match, and pin, the probes of the other tests. The sibling tests avoid +# all of this by using a name that the wildcard cannot reach. + # shellcheck source=lib/probe.sh . "$(dirname $0)"/lib/probe.sh
@@ -17,6 +24,8 @@ skip_if_no_perf_probe || exit 2 skip_if_no_perf_trace || exit 2 [ "$(id -u)" = 0 ] || exit 2 +# shellcheck disable=SC2034 # consumed by lib/probe_vfs_getname.sh +vfs_getname="vfs_getname_$$" . "$(dirname $0)"/lib/probe_vfs_getname.sh trace_open_vfs_getname() {
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 13/18] perf test probe_vfs_getname: Scope probe name to PID and make non-exclusive
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
The probe name
vfs_getnamewas hardcoded, causing collisions when tests ran concurrently. Furthermore,cleanup_probe_vfs_getname()usedperf probe -d probe:vfs_getname*, deleting probes registered by other parallel tests.Scope the probe name to the pid, and rename it to
getname_flags_$$so that it no longer begins with "vfs_getname". perf trace callsevlist__add_vfs_getname(), which opens every event matching a hardcoded "probe:vfs_getname*" wildcard, so a perf trace run by any other test would otherwise pin this probe and makeperf probe -dfail with-EBUSY. That also unblocks making the perf trace tests non-exclusive later in this series.
Quoted 8 lines
Enumerate the probes to record and to delete from
perf probe -l, matching^probe:${vfs_getname}(_[[:digit:]]+)?$exactly, rather than [ ... ]
accumulating a set per run. The fixed name at least meant the next run found, and went on to reuse, whatever the last one left.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
Ian Rogers
reviewerPATCH v4 14/18 perf test record+probe_libc_inet_pton: Scope event to PID, add retries, and make non-exclusive
In reply to Ian RogersThe uprobe name was not scoped to PID, and concurrent writes to /sys/kernel/debug/tracing/uprobe_events can occasionally return -EBUSY when another process holds the tracefs inode lock.
Scope the probe event name with $$ (inet_pton_$$=inet_pton) and add a retry loop with backoff for uprobe addition. Drop the (exclusive) tag so the test can run in parallel during pass 1.
A PID scoped probe is no longer cleaned up by any other test, so add an EXIT/TERM/INT trap to delete it, otherwise an interrupted run leaks the uprobe into the system. The trap is installed only after the root and IPv6 checks that exit 2 to skip the test, as trap_cleanup() exits 1 and would otherwise turn those skips into failures. Deletion enumerates the probes from perf probe -l, matching ^probe_libc:inet_pton_$$(_[[:digit:]]+)?$ exactly, rather than reading $event_name: a signal arriving after perf probe injected the uprobe but before the assignment completed would leave that variable empty and leak the probe, and an inet_pton_$$* glob would reach the probe of a test whose pid merely starts with this one's.
While here use mktemp rather than mktemp -u for the temporary files:
this test runs as root in a world writable /tmp, and predicting a name without creating it allows another user to win the race and plant a symlink. The perf.data check becomes -s rather than -e as mktemp now pre-creates an empty file.
Pre-create the temporary files with mktemp rather than reserving names with mktemp -u, and bail out if mktemp fails. The emptiness check on the recorded data quotes its path for the same reason: unquoted, an empty value would leave [ ! -s ] testing the string "-s", which is true, so the negation would skip the failure path and the test would go on to pass without having recorded anything.
--- .../shell/record+probe_libc_inet_pton.sh | 107 ++++++++++++++---- 1 file changed, 87 insertions(+), 20 deletions(-)
tools/perf/tests/shell/record+probe_libc_inet_pton.sh +87 -20
@@ -1,5 +1,5 @@ #!/bin/bash -# probe libc's inet_pton & backtrace it with ping (exclusive) +# probe libc's inet_pton & backtrace it with ping # Installs a probe on libc's inet_pton function, that will use uprobes, # then use 'perf trace' on a ping to localhost asking for just one packet
@@ -21,20 +21,30 @@ nm -Dg $libc 2>/dev/null | grep -F -q inet_pton || exit 254 event_pattern='probe_libc:inet_pton(_[[:digit:]]+)?' add_libc_inet_pton_event() { + local attempts=0 + while [ $attempts -lt 3 ]; do + event_name=$(perf probe -f -x $libc -a "inet_pton_$$=inet_pton" 2>&1 | \ + awk -v ep="$event_pattern" -v l="$libc" '$0 ~ ep && $0 ~ \ + ("\\(on inet_pton in " l "\\)") {print $1}' | head -n 1) + + if [ -n "$event_name" ]; then + return 0 + fi + attempts=$((attempts + 1)) + sleep 0.1 + done - event_name=$(perf probe -f -x $libc -a inet_pton 2>&1 | \ - awk -v ep="$event_pattern" -v l="$libc" '$0 ~ ep && $0 ~ \ - ("\\(on inet_pton in " l "\\)") {print $1}' | head -n 1) - - if [ $? -ne 0 ] || [ -z "$event_name" ] ; then - printf "FAIL: could not add event\n" - return 1 - fi + printf "FAIL: could not add event\n" + return 1 } trace_libc_inet_pton_backtrace() { - expected=`mktemp -u /tmp/expected.XXX` + # Create the files rather than just reserving names with mktemp -u: + # this runs as root and /tmp is world writable, so a predictable name + # that is written to later can be pre-created as a symlink by an + # unprivileged user and used to clobber an arbitrary file. + expected=$(mktemp /tmp/expected.XXX) || return 1 echo "ping[][0-9 \.:]+$event_name: \([[:xdigit:]]+\)" > $expected echo ".*inet_pton\+0x[[:xdigit:]]+[[:space:]]\($libc|inlined\)$" >> $expected
@@ -50,8 +60,8 @@ trace_libc_inet_pton_backtrace() { ;; esac - perf_data=`mktemp -u /tmp/perf.data.XXX` - perf_script=`mktemp -u /tmp/perf.script.XXX` + perf_data=$(mktemp /tmp/perf.data.XXX) || return 1 + perf_script=$(mktemp /tmp/perf.script.XXX) || return 1 # Check presence of libtraceevent support to run perf record skip_no_probe_record_support "$event_name/$eventattr/"
@@ -61,9 +71,12 @@ trace_libc_inet_pton_backtrace() { fi perf record -e $event_name/$eventattr/ -o $perf_data ping -6 -c 1 ::1 > /dev/null 2>&1 - # check if perf data file got created in above step. - if [ ! -e $perf_data ]; then - printf "FAIL: perf record failed to create \"%s\" \n" "$perf_data" + # Check perf record actually wrote data. mktemp already created the + # file, so test that it is non-empty rather than that it exists. Quote + # the path: were it ever empty, [ ! -s ] would test the string "-s" + # instead and report success. + if [ ! -s "$perf_data" ]; then + printf "FAIL: perf record failed to write \"%s\" \n" "$perf_data" return 1 fi perf script -i $perf_data | tac | grep -m1 ^ping -B9 | tac > $perf_script
@@ -97,21 +110,75 @@ trace_libc_inet_pton_backtrace() { # even if the perf script output does not match. } +# Print the pid scoped uprobes this test may have created. perf probe appends +# _1, _2, ... when the name is already taken, so match those too, but anchor +# the match: an "inet_pton_$$*" glob would also match the probe of a test whose +# pid merely starts with this one's, e.g. 123 and 1234. +libc_inet_pton_events() { + perf probe -l 2>/dev/null | awk '{print $1}' | + grep -E "^probe_libc:inet_pton_$$(_[[:digit:]]+)?$" +} + delete_libc_inet_pton_event() { + # Ask the kernel what is actually there rather than trusting + # $event_name: a signal arriving after perf probe injected the uprobe + # but before the assignment to event_name completed would otherwise + # leave the variable empty and leak the probe. + # + # Retry as the addition does. Deleting writes to uprobe_events just as + # adding does, so it can lose the same race with a concurrent test and + # fail with -EBUSY. Re-list rather than assume the delete worked, and + # only give up once the probes are really gone: the name is pid + # scoped, so one left behind here is never reused or overwritten by a + # later run and would sit in the kernel until reboot. + local attempts=0 + local probe + + while [ $attempts -lt 3 ]; do + for probe in $(libc_inet_pton_events); do + perf probe -q -d "$probe" + done - if [ -n "$event_name" ] ; then - perf probe -q -d $event_name - fi + if [ -z "$(libc_inet_pton_events)" ]; then + return 0 + fi + + attempts=$((attempts + 1)) + sleep 0.1 + done + + printf "WARN: could not delete event(s): %s\n" \ + "$(libc_inet_pton_events | tr '\n' ' ')" + return 1 +} + +cleanup() { + rm -f ${perf_data} ${perf_script} ${expected} + delete_libc_inet_pton_event + + trap - EXIT TERM INT +} + +trap_cleanup() { + cleanup + exit 1 } # Check for IPv6 interface existence ip a sh lo | grep -F -q inet6 || exit 2 [ "$(id -u)" = 0 ] || exit 2 +# Install the trap only now that the skips above are out of the way: it exits +# 1, so arming it any earlier would turn an 'exit 2' skip into a failure. +# +# The event name is pid scoped, so unlike the old fixed name an orphan left +# behind by an interrupted run is never overwritten by a later run: it would +# stay in the kernel forever. Always clean up, including on a signal. +trap trap_cleanup EXIT TERM INT + skip_if_no_perf_probe && \ add_libc_inet_pton_event && \ trace_libc_inet_pton_backtrace err=$? -rm -f ${perf_data} ${perf_script} ${expected} -delete_libc_inet_pton_event +cleanup exit $err
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 14/18] perf test record+probe_libc_inet_pton: Scope event to PID, add retries, and make non-exclusive
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 19 lines
The uprobe name was not scoped to PID, and concurrent writes to
/sys/kernel/debug/tracing/uprobe_eventscan occasionally return-EBUSYwhen another process holds the tracefs inode lock.Scope the probe event name with
$$(inet_pton_$$=inet_pton) and add a retry loop with backoff for uprobe addition. Drop the(exclusive)tag so the test can run in parallel during pass 1.A PID scoped probe is no longer cleaned up by any other test, so add an EXIT/TERM/INT trap to delete it, otherwise an interrupted run leaks the uprobe into the system. The trap is installed only after the root and IPv6 checks that
exit 2to skip the test, astrap_cleanup()exits 1 and would otherwise turn those skips into failures. Deletion enumerates the probes fromperf probe -l, matching^probe_libc:inet_pton_$$(_[[:digit:]]+)?$exactly, rather than reading [ ... ]
true, so the negation would skip the failure path and the test would go on to pass without having recorded anything.
Quoted 2 lines
Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
Ian Rogers
reviewerPATCH v4 15/18 perf test trace_summary: Improve error diagnostics
In reply to Ian RogersWhen pattern matching fails in test_perf_trace(), print the command that failed along with the actual match count, the matching lines found, and the last 20 lines of output to aid debugging.
--- tools/perf/tests/shell/trace_summary.sh | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
tools/perf/tests/shell/trace_summary.sh +10 -4
@@ -28,10 +28,16 @@ test_perf_trace() { count=$(grep -E -c -m 3 "${search}" ${OUTPUT}) if [ "${count}" != "3" ]; then - echo "Error: cannot find enough pattern ${search} in the output" - cat ${OUTPUT} - rm -f ${OUTPUT} - exit 1 + echo "Error: cannot find enough pattern ${search} (count=${count}) in output of:" + echo "Error: perf trace ${args} -- ${workload}" + echo "Error: matched lines:" + grep -E "${search}" ${OUTPUT} || echo "none" + echo "Error: last 20 lines of output:" + # The summary is printed after any trace output, so show the end of + # the file: with -S the head of it is the trace, not what is matched. + tail -n 20 ${OUTPUT} + rm -f ${OUTPUT} + exit 1 fi }
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 15/18] perf test trace_summary: Improve error diagnostics
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines
When pattern matching fails in
test_perf_trace(), print the command that failed along with the actual match count, the matching lines found, and the last 20 lines of output to aid debugging.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
Ian Rogers
reviewerPATCH v4 16/18 perf test trace_btf_general: Drop --max-events=1 and make non-exclusive
In reply to Ian Rogerstrace_btf_general.sh used --max-events=1 with perf trace on commands such as mv, echo, and sleep. When background activity occurs or tests run in parallel, perf trace can capture an event from an unrelated process and exit prematurely before recording the target command's syscalls.
Drop --max-events=1 and let tracing run until the command completes, checking for the expected output with grep (matching trace_btf_enum.sh). Remove the (exclusive) tag so the test runs in parallel.
Running perf trace in parallel is only safe now that the probe tests no longer name their probes "vfs_getname...": perf trace opens everything matching a hardcoded "probe:vfs_getname*" wildcard, which used to pin those probes and make their cleanup fail with -EBUSY.
--- tools/perf/tests/shell/trace_btf_general.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
tools/perf/tests/shell/trace_btf_general.sh +4 -4
@@ -1,5 +1,5 @@ #!/bin/bash -# perf trace BTF general tests (exclusive) +# perf trace BTF general tests # SPDX-License-Identifier: GPL-2.0 err=0
@@ -27,7 +27,7 @@ check_vmlinux() { trace_test_string() { echo "Testing perf trace's string augmentation" - output="$(perf trace --sort-events -e renameat* --max-events=1 -- mv ${file1} ${file2} 2>&1)" + output="$(perf trace --sort-events -e renameat* -- mv ${file1} ${file2} 2>&1)" if ! echo "$output" | grep -q -E "^mv/[0-9]+ renameat(2)?\(.*, \"${file1}\", .*, \"${file2}\", .*\) += +[0-9]+$" then printf "String augmentation test failed, output:\n$output\n"
@@ -38,7 +38,7 @@ trace_test_string() { trace_test_buffer() { echo "Testing perf trace's buffer augmentation" # echo will insert a newline (\10) at the end of the buffer - output="$(perf trace --sort-events -e write --max-events=1 -- echo "${buffer}" 2>&1)" + output="$(perf trace --sort-events -e write -- echo "${buffer}" 2>&1)" if ! echo "$output" | grep -q -E "^echo/[0-9]+ write\([0-9]+, ${buffer}.*, [0-9]+\) += +[0-9]+$" then printf "Buffer augmentation test failed, output:\n$output\n"
@@ -48,7 +48,7 @@ trace_test_buffer() { trace_test_struct_btf() { echo "Testing perf trace's struct augmentation" - output="$(perf trace --sort-events -e clock_nanosleep --force-btf --max-events=1 -- sleep 1 2>&1)" + output="$(perf trace --sort-events -e clock_nanosleep --force-btf -- sleep 1 2>&1)" if ! echo "$output" | grep -q -E "^sleep/[0-9]+ clock_nanosleep\(0, 0, \{1,.*\}, 0x[0-9a-f]+\) += +[0-9]+$" then printf "BTF struct augmentation test failed, output:\n$output\n"
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 16/18] perf test trace_btf_general: Drop --max-events=1 and make non-exclusive
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines
trace_btf_general.shused--max-events=1withperf traceon commands such asmv,echo, andsleep. When background activity occurs or tests run in parallel,perf tracecan capture an event from an unrelated process and exit prematurely before recording the target command's syscalls.Drop
--max-events=1and let tracing run until the command completes, checking for the expected output with grep (matchingtrace_btf_enum.sh). Remove the (exclusive) tag so the test runs in parallel.
Quoted 7 lines
Running perf trace in parallel is only safe now that the probe tests no longer name their probes "
vfs_getname...": perf trace opens everything matching a hardcoded "probe:vfs_getname*" wildcard, which used to pin those probes and make their cleanup fail with-EBUSY.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
Ian Rogers
reviewerPATCH v4 17/18 perf test trace_summary: Make non-exclusive
In reply to Ian Rogerstrace_summary.sh tests various summary modes of perf trace. It already directs output to a unique temporary file without polluting the current working directory.
Remove the (exclusive) tag so it can run concurrently in parallel test runs.
Running perf trace in parallel is only safe now that the probe tests no longer name their probes "vfs_getname...": perf trace opens everything matching a hardcoded "probe:vfs_getname*" wildcard, which used to pin those probes and make their cleanup fail with -EBUSY.
--- tools/perf/tests/shell/trace_summary.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/tests/shell/trace_summary.sh +1 -1
@@ -1,5 +1,5 @@ #!/bin/bash -# perf trace summary (exclusive) +# perf trace summary # SPDX-License-Identifier: GPL-2.0 # Check that perf trace works with various summary mode
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 17/18] perf test trace_summary: Make non-exclusive
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines
trace_summary.shtests various summary modes ofperf trace. It already directs output to a unique temporary file without polluting the current working directory.Remove the (exclusive) tag so it can run concurrently in parallel test runs.
Quoted 7 lines
Running perf trace in parallel is only safe now that the probe tests no longer name their probes "
vfs_getname...": perf trace opens everything matching a hardcoded "probe:vfs_getname*" wildcard, which used to pin those probes and make their cleanup fail with-EBUSY.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=17
Ian Rogers
reviewerPATCH v4 18/18 perf test uprobe_from_different_cu: Scope probe name to PID
In reply to Ian RogersThe test builds a binary in a per-run temporary directory and probes its foo function. The directory name is unique, but perf probe derives the event name from the probed function and the group name from the binary's basename, so every run registers the same probe_testfile:foo event.
Running the test concurrently with itself, as 'perf test -r3' does, therefore fails in all but one of the runs with:
Error: event "foo" already exists.
Hint: Remove existing event by 'perf probe -d'
and a losing run's cleanup goes on to delete the winning run's probe out from under it.
Name the event after the pid, foo_$$, so that parallel runs no longer collide. This lets the test stay in the parallel pass rather than having to be marked (exclusive).
--- .../perf/tests/shell/test_uprobe_from_different_cu.sh | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
tools/perf/tests/shell/test_uprobe_from_different_cu.sh +9 -2
@@ -18,12 +18,19 @@ fi temp_dir=$(mktemp -d /tmp/perf-uprobe-different-cu-sh.XXXXXXXXXX) +# The name of the uprobe added and removed below. The probe is placed on +# ${temp_dir}/testfile, but perf probe derives the event name from the probed +# function and the group name from the binary's basename, so every run would +# otherwise share one probe_testfile:foo event, and a concurrent run would +# fail with 'event "foo" already exists'. Scope the event name to the pid. +probe_name="foo_$$" + cleanup() { trap - EXIT TERM INT if [[ "${temp_dir}" =~ ^/tmp/perf-uprobe-different-cu-sh.*$ ]]; then echo "--- Cleaning up ---" - perf probe -x ${temp_dir}/testfile -d foo || true + perf probe -x ${temp_dir}/testfile -d ${probe_name} || true rm -f "${temp_dir}/"* rmdir "${temp_dir}" fi
@@ -84,6 +91,6 @@ gcc -g -Og -c ${temp_dir}/testfile-main.c -o ${temp_dir}/testfile-main.o gcc -g -Og -o ${temp_dir}/testfile ${temp_dir}/testfile-foo.o ${temp_dir}/testfile-main.o perf probe -x ${temp_dir}/testfile --funcs foo | grep "foo" -perf probe -x ${temp_dir}/testfile foo +perf probe -x ${temp_dir}/testfile ${probe_name}=foo cleanup
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 18/18] perf test uprobe_from_different_cu: Scope probe name to PID
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines
The test builds a binary in a per-run temporary directory and probes its foo function. The directory name is unique, but perf probe derives the event name from the probed function and the group name from the binary's basename, so every run registers the same probe_testfile:foo event.
Running the test concurrently with itself, as 'perf test -r3' does, therefore fails in all but one of the runs with:
Error: event "foo" already exists.
Hint: Remove existing event by 'perf probe -d'and a losing run's cleanup goes on to delete the winning run's probe out from under it.
Quoted 6 lines
Name the event after the pid, foo_$$, so that parallel runs no longer collide. This lets the test stay in the parallel pass rather than having to be marked (exclusive).
Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=18
Arnaldo Carvalho de Melo
maintainerQuoted 30 lines · Ian Rogers
perf trace's BPF augmentation attaches to raw_syscalls:sys_enter and raw_syscalls:sys_exit system wide, and used the program return value to decide whether a syscall was interesting. Returning 0 from a
BPF_PROG_TYPE_TRACEPOINTprogram makesperf_trace_run_bpf_submit()drop the event for every listener on that tracepoint, not just for the perf trace that installed the program. Any concurrent perf trace, perf record or ftrace session watchingraw_syscallstherefore lost events, which is one of the reasons so many of the perf trace and perf probe shell tests had to be marked (exclusive) and run on their own.Patches 1 to 3 are independent fixes to the code the rest of the series goes on to rework or to rely on.
Patches 4 to 10 fix perf trace. They stop the return value being used as a filter and do the filtering in BPF maps instead, fix argument handling for the
__data_locinternal tracepoint fields that syscalls:sys_enter_* gained in 6.19, stop thesys_exitprogram array tail calling asys_enteraugmenter, and replace the userspace PERF_RECORD_FORK/PERF_RECORD_EXIT bookkeeping with BTF-typed raw tracepoint programs on sched_process_{fork,exit,exec}. A task is then registered before its first syscall and evicted indo_exit(), rather than whenever userspace next drains the ring buffer.Patches 11 to 18 deal with the tests. Several collided with each other through global state rather than through perf trace: fixed probe names,
clear_all_probes()disabling every tracepoint on the system, and perf trace's hardcoded "probe:vfs_getname*" wildcard pinning probes belonging to other tests. With those scoped to a pid they can drop (exclusive) and run in parallel again.
Quoted 3 lines
Tested on
x86_64. The trace and probe tests pass under 'perf test -r3', which runs the repeats concurrently. Every patch builds individually, and the series also builds with BUILD_BPF_SKEL=0.
Lots of changes, from a quick look all look sensible, I'll test it some more, as in one case it segfaulted:
root@x2:~# strace -e perf_event_open perf trace -a sleep 1
<SNIP>
262.297 ( 0.002 ms): ptyxis/30654 write(fd: 4<anon_inode:[eventfd]>, buf: \1\0\0\0\0\0\0\0, count: 8) = 8
262.304 ( 0.011 ms): ptyxis/30654 ppoll(ufds: 0x55dfa7a62f10, nfds: 11, tsp: 0x7ffc0701eed0, sigsetsize: 8) = 2
262.318 ( 0.002 ms): ptyxis/30654 read(fd: 4<anon_inode:[eventfd]>, buf: 0x7ffc0701ee10, count: 8) = 8
262.324 ( 0.002 ms): ptyxis/30654 write(fd: 4<anon_inode:[eventfd]>, buf: \1\0\0\0\0\0\0\0, count: 8) = 8
262.328 ( 0.002 ms): ptyxis/30654 read(fd: 31</dev/ptmx>, buf: 0x55dfa8a54f19, count: 5751) = 8
262.332 ( 0.002 ms): ptyxis/30654 read(fd: 31</dev/ptmx>, buf: 0x55dfa8a54f20, count: 5744) = -1 (unknown) (Resource temporarily unavailable)
LOST 70 events!
--- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=0x117f000} ---
perf: Segmentation fault
#0 0x73104f in dump_stack debug.c:366
#1 0x7310c5 in sighandler_dump_stack debug.c:378
#2 0x7f749ea66fb0 in __restore_rt libc.so.6[19fb0]
#3 0x4c0fa0 in syscall_arg__scnprintf_buf builtin-trace.c:1955
#4 0x4c2f3d in syscall_arg_fmt__scnprintf_val builtin-trace.c:2632
#5 0x4c33ae in syscall__scnprintf_args builtin-trace.c:2722
#6 0x4c43d3 in trace__sys_enter builtin-trace.c:3094
#7 0x4c7865 in trace__handle_event builtin-trace.c:4013
#8 0x4c9ab4 in __trace__deliver_event builtin-trace.c:4962
#9 0x4c9c06 in trace__deliver_event builtin-trace.c:4988
#10 0x4caed8 in trace__run builtin-trace.c:5397
#11 0x4cf530 in cmd_trace builtin-trace.c:6681
#12 0x4da43c in run_builtin perf.c:380
#13 0x4da68e in handle_internal_command perf.c:430
#14 0x4da7ea in run_argv perf.c:477
#15 0x4daabe in main perf.c:577
#16 0x7f749ea50681 in __libc_start_call_main libc.so.6[3681]
#17 0x7f749ea50798 in __libc_start_main@@GLIBC_2.34 libc.so.6[3798]
#18 0x409f25 in _start perf[409f25]
--- SIGSEGV {si_signo=SIGSEGV, si_code=SI_TKILL, si_pid=1097958, si_uid=0} ---
+++ killed by SIGSEGV (core dumped) +++
Segmentation fault (core dumped) strace -e perf_event_open perf trace -a sleep 1
root@x2:~#root@x2:~# uname -a Linux x2 7.2.5-200.fc44.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Sep 11 15:11:05 UTC 2026 x86_64 GNU/Linux root@x2:~#
- Arnaldo
Ian Rogers
reviewerQuoted 38 lines · Arnaldo Carvalho de Melo
Lots of changes, from a quick look all look sensible, I'll test it some more, as in one case it segfaulted:
root@x2:~# strace -e perf_event_open perf trace -a sleep 1 <SNIP> 262.297 ( 0.002 ms): ptyxis/30654 write(fd: 4<anon_inode:[eventfd]>, buf: \1\0\0\0\0\0\0\0, count: 8) = 8 262.304 ( 0.011 ms): ptyxis/30654 ppoll(ufds: 0x55dfa7a62f10, nfds: 11, tsp: 0x7ffc0701eed0, sigsetsize: 8) = 2 262.318 ( 0.002 ms): ptyxis/30654 read(fd: 4<anon_inode:[eventfd]>, buf: 0x7ffc0701ee10, count: 8) = 8 262.324 ( 0.002 ms): ptyxis/30654 write(fd: 4<anon_inode:[eventfd]>, buf: \1\0\0\0\0\0\0\0, count: 8) = 8 262.328 ( 0.002 ms): ptyxis/30654 read(fd: 31</dev/ptmx>, buf: 0x55dfa8a54f19, count: 5751) = 8 262.332 ( 0.002 ms): ptyxis/30654 read(fd: 31</dev/ptmx>, buf: 0x55dfa8a54f20, count: 5744) = -1 (unknown) (Resource temporarily unavailable) LOST 70 events! --- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=0x117f000} --- perf: Segmentation fault #0 0x73104f in dump_stack debug.c:366 #1 0x7310c5 in sighandler_dump_stack debug.c:378 #2 0x7f749ea66fb0 in __restore_rt libc.so.6[19fb0] #3 0x4c0fa0 in syscall_arg__scnprintf_buf builtin-trace.c:1955 #4 0x4c2f3d in syscall_arg_fmt__scnprintf_val builtin-trace.c:2632 #5 0x4c33ae in syscall__scnprintf_args builtin-trace.c:2722 #6 0x4c43d3 in trace__sys_enter builtin-trace.c:3094 #7 0x4c7865 in trace__handle_event builtin-trace.c:4013 #8 0x4c9ab4 in __trace__deliver_event builtin-trace.c:4962 #9 0x4c9c06 in trace__deliver_event builtin-trace.c:4988 #10 0x4caed8 in trace__run builtin-trace.c:5397 #11 0x4cf530 in cmd_trace builtin-trace.c:6681 #12 0x4da43c in run_builtin perf.c:380 #13 0x4da68e in handle_internal_command perf.c:430 #14 0x4da7ea in run_argv perf.c:477 #15 0x4daabe in main perf.c:577 #16 0x7f749ea50681 in __libc_start_call_main libc.so.6[3681] #17 0x7f749ea50798 in __libc_start_main@@GLIBC_2.34 libc.so.6[3798] #18 0x409f25 in _start perf[409f25]killed by SIGSEGV (core dumped) +++ renamed from SIGSEGV {si_signo=SIGSEGV, si_code=SI_TKILL, si_pid=1097958, si_uid=0} --- +0
-0Segmentation fault (core dumped) strace -e
perf_event_openperf trace -a sleep 1
root@x2:~#
Quoted 3 lines
root@x2:~# uname -a Linux x2 7.2.5-200.fc44.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Sep 11 15:11:05 UTC 2026 x86_64 GNU/Linux root@x2:~#
Thanks Arnaldo for testing, and sorry for the crash. Working with antigravity I got the analysis below for the crash and I'll fix it in v5.
I can account for it, and it is a regression from patch 7 ("perf trace:
Filter events in BPF and avoid tracepoint vetoes") landing on a missing bounds check that predates the series.
Before the series syscall_unaugmented() was:
SEC("tp/raw_syscalls/sys_enter")
int syscall_unaugmented(struct syscall_enter_args *args) {
return 1;
}
so an unaugmented sys_enter emitted nothing at all. Patch 7 moved it to tp/syscalls/sys_enter_unaugmented and made it output the 64 byte struct syscall_enter_args to __augmented_syscalls__, so that perf trace could stop listening on the raw tracepoint and stop vetoing it.
That 64 byte record does not come back as 64 bytes. perf_sample_save_raw_data() does:
size = round_up(sum + sizeof(u32), sizeof(u64));raw->size = size - sizeof(u32);frag->pad = raw->size - sum;
so sum of 64 gives raw->size of 68, and the 4 bytes of padding are written by:
__output_skip(handle, NULL, frag->pad);
which advances over them rather than zeroing them. They are whatever the ring buffer last held there.
Userspace then does:
*augmented_args_size = sample->raw_size - args_size;
with args_size of 64, gets 4, decides that is an augmented payload, and copies those 4 stale bytes into argbuf as the size and int_arg of a struct augmented_arg. syscall_arg__scnprintf_buf() has no bound on the length it reads there:
for (int j = 0; j < augmented_arg->size; ++j)
so a size taken from, say, the low half of a pointer walks straight off the end of the 8K argbuf in bss. SCA_BUF is used by exactly one syscall, write()'s buf argument, which is why a terminal emulator's writes were what tripped it. The si_addr in your report is page aligned just past that buffer, which fits.
btf_struct_scnprintf() already had the check that would have caught it, which is why the BTF path survived this and SCA_BUF did not: a 4 byte trailing run is smaller than a struct augmented_arg, so it returns early.
v5 fixes it in two patches, placed before the patch that introduces the unaugmented output so that no commit in the series is broken:
perf trace: Bounds check augmented arguments before reading them
Lifts btf_struct_scnprintf()'s check into a shared syscall_arg__augmented_args_valid() in beauty.h and uses it at every site that reads an augmented argument. Besides the crash site this covers the timespec, sockaddr and perf_event_attr beautifiers, which read a fixed sized type out of a payload that may be shorter than it. Two unbounded reads that are not about the header are fixed with
them: perf_event_attr__fprintf() always reads a whole struct perf_event_attr, so the payload is now copied into a zero padded local rather than read in place, and the AF_LOCAL path is printed with a length from the payload rather than as a string it need not be.
perf trace: Do not read sample padding as an augmented argument
A trailing run shorter than a struct augmented_arg cannot be one, so it is recognised as the padding it is rather than being offered to the beautifiers at all.
The first alone stops the SIGSEGV. The second stops the syscall being printed with a bogus argument in the cases where the padding happens to hold a length that passes validation.
Thanks,
Ian
Ian Rogers
reviewerv5 cover perf trace: Fix BPF filtering and make tracing tests non-exclusive
In reply to Ian Rogersperf trace's BPF augmentation attaches to raw_syscalls:sys_enter and raw_syscalls:sys_exit system wide, and used the program return value to decide whether a syscall was interesting. Returning 0 from a BPF_PROG_TYPE_TRACEPOINT program makes perf_trace_run_bpf_submit() drop the event for every listener on that tracepoint, not just for the perf trace that installed the program. Any concurrent perf trace, perf record or ftrace session watching raw_syscalls therefore lost events, which is one of the reasons so many of the perf trace and perf probe shell tests had to be marked (exclusive) and run on their own.
Patches 1 to 6 are independent fixes to the code the rest of the series goes on to rework or to rely on.
Patches 7 to 10 fix how an augmented argument is read back. They skip the __data_loc internal tracepoint fields that syscalls:sys_enter_* gained in 6.19 for syscalls only, bounds check every read of an augmented payload, and stop the ring buffer padding at the end of a sample being offered to the beautifiers as one. Patch 8 fixes the SIGSEGV Arnaldo hit testing v4.
Patches 11 to 15 fix perf trace's filtering. They stop the return value being used as a filter and do the filtering in BPF maps instead, stop the sys_exit program array tail calling a sys_enter augmenter, and replace the userspace PERF_RECORD_FORK/PERF_RECORD_EXIT bookkeeping with BTF-typed raw tracepoint programs on sched_process_{fork,exit,exec}. A task is then registered before its first syscall and evicted in do_exit(), rather than whenever userspace next drains the ring buffer.
Patches 16 to 23 deal with the tests. Several collided with each other through global state rather than through perf trace: fixed probe names, clear_all_probes() disabling every tracepoint on the system, and perf trace's hardcoded "probe:vfs_getname*" wildcard pinning probes belonging to other tests. With those scoped to a pid they can drop (exclusive) and run in parallel again.
Patches 1 and 2 fix pre-existing bugs that stand on their own and can be applied ahead of the rest. Only patch 9 depends on them.
Tested on x86_64. The trace and probe tests pass under 'perf test -r3', which runs the repeats concurrently. Every patch builds individually, and the series also builds with BUILD_BPF_SKEL=0.
Changes since v4:
- New patch 1 fills in the size in the augmented argument header in the four augmenters that left it unset: sendto, perf_event_open, nanosleep and clock_nanosleep. They all build a payload for a fixed size type, and the beautifiers cast it directly rather than reading the length, so nothing has needed it so far. Patch 9 starts checking it. While there, report a failed bpf_probe_read_user() rather than claiming success for it: the payload length is set to zero and the error recorded in the header, so userspace falls back to printing the raw pointer instead of whatever the per-CPU scratch buffer last held.
- New patch 2 includes the augmented argument header in nanosleep's payload length. It used sizeof(augmented_args->args) where its three siblings use sizeof(u64) + sizeof(augmented_args->args), so the record was eight bytes short of the struct timespec it carries and tv_nsec was printed half from the ring buffer.
- Patch 7 only skips the internal tracepoint fields when it is setting up a syscall. syscall_arg_fmt__init_array() is also called for an ordinary tracepoint by evsel__init_tp_arg_scnprintf(), and trace__fprintf_tp_fields() walks that array in lockstep with the field list, so packing the entries shifted every formatter along by one from the field it belongs to. 'perf trace -e sched:sched_process_exec' printed its filename with the pid beautifier. The syscall path indexes the array packed and is unaffected, so the two callers now ask for what each of them needs.
- New patch 8 bounds checks an augmented argument before reading it. This is the SIGSEGV Arnaldo reported on v4. The check btf_struct_scnprintf() already had is lifted into a shared syscall_arg__augmented_args_valid() in beauty.h and used at every site that reads an augmented payload, in particular syscall_arg__scnprintf_buf(), which looped to a length taken from the payload with nothing bounding it and walked off the end of the 8K argbuf in bss.
- New patch 9 bounds the beautifiers that read a fixed size type out of a payload that may be shorter than it. perf_event_attr__fprintf() always reads a whole struct perf_event_attr, so the payload is copied into a zero padded local rather than read in place, and the AF_LOCAL socket address is printed with a length from the payload rather than as a string it need not be. Split out from patch 8 so the crash fix stays minimal.
- New patch 10 recognises the padding at the end of a sample for what it is. perf_sample_save_raw_data() rounds the raw size up to eight bytes and __output_skip() advances over the padding rather than zeroing it, so a 64 byte struct syscall_enter_args comes back as 68 bytes and userspace took the four stale bytes for an augmented argument. A trailing run shorter than a struct augmented_arg cannot be one.
- Patch 13 notes that a descendant added to pids_filtered by sched_process_fork() can still have a sys_exit printed without a matching enter. The raw_syscalls:sys_exit evsel is still in the evlist and its tracepoint filter is set once, so it never learns of the descendants, and a tracepoint program can only suppress an event by vetoing it, which is what this series removes. It needs --filter-pids naming a process that then forks. Routing sys_exit through __augmented_syscalls__ the way sys_enter now is would close it.
- Patch 14 notes that a thread the target creates during the attach window is still missed by 'perf trace -t'. The re-enumeration reads /proc/<tid>/task/<tid>/children, which lists forked processes, and copy_process() makes a CLONE_THREAD child a sibling of its creator rather than a child of it. /proc does not record which task in a group created which, so such a sibling cannot be told apart from one -t was deliberately pointed away from.
Changes since v3:
- New patch 3 returns -ENOMEM rather than -1 from evsel__set_filter(), evsel__append_filter(), evlist__set_tp_filter() and evlist__append_tp_filter(). An allocation is the only thing that can fail in any of them, and patch 7 goes on to print what they return, where -1 negated is EPERM and a failure to allocate would have been reported as "Operation not permitted".
- Patch 5 sets sc->args only once the arg_fmt array that is indexed alongside it has been allocated. Publishing the field first left a syscall with args set and arg_fmt NULL, and since sc->name is already set by then a later syscall__read_info() returns success without retrying the allocation, leaving syscall_arg_fmt__mask_val() to dereference NULL.
- Patch 7 adds the raw_syscalls tracepoints in trace__run() only if they are not in the evlist already, and stops ignoring the result of trace__add_syscall_newtp(). cmd_trace() adds them before it creates the bpf-output event, and on the paths where that creation fails they were added a second time, giving the session two enter and two exit evsels for the same pair of tracepoints.
- Patch 7 no longer abandons the session when a target does not fit in the pid map. bpf_map__update_elem() answers -E2BIG once max_entries keys are present, so a target with more threads than the map has room for took perf trace down with it, on exactly the large workloads where there is least else to reach for. The tasks that fit are added and a warning says how many did not.
- Patch 9 includes <sys/types.h> for the pid_t it uses rather than relying on the include chain to drag it in.
- Patch 9 no longer ends the session when the target has already exited. thread_map__new_by_pid() fails with ENOENT once /proc/<pid>/task is gone, which is the ordinary outcome of 'perf trace -p' on a short lived process, so only ENOMEM is now propagated and everything else is logged and stepped over.
- Patch 9 reads /proc once per thread group instead of once per thread. 'perf trace -p' names every thread of the target in the thread map and /proc/<tid>/task lists the whole group whichever thread it is asked through, so the enumeration was quadratic in the number of threads. On a 64 thread target it went from 65 directory reads and 4097 entries to 2 and 65, for the same set of pids.
- New patch 10 removes from the BPF maps the tasks that died before userspace got round to writing them there. sched_process_exit() can only delete a pid that is already in the map, so a target that exits during startup leaves an entry behind for the rest of the session, and since pids are reused an unrelated task then gets traced, or silently filtered out, in its place. The map is walked with the cursor left on a task found alive, which is one the walk has decided to keep, so its own deletions can never leave the cursor on a key that htab_map_get_next_key() will answer by starting the walk over.
Changes since v2:
- Rebased onto the current perf-tools-next.
- Patch 1 also includes <assert.h>, for the assert() in augmented_syscalls__create_bpf_output().
- New patch 2 makes evsel__put_and_free_priv() free the whole evsel_trace. It only zfree()d the struct, leaking the syscall_arg_fmt array hanging off it. No caller can reach that today, but patch 6 adds one that discards a fully set up evsel.
- Patch 6 identifies the evsel to drop from the evlist by comparing against trace.syscalls.events.sys_enter instead of a strstr() for "syscalls:sys_enter". That substring also matches the per syscall syscalls:sys_enter_SYSCALL tracepoints, so a user asking for one of those by name would have had it removed from the evlist, and __augmented_syscalls__ described with its tracefs format rather than the raw tracepoint's.
- Patch 6 reports a failure to program the pid filters with the error that caused it. trace__run() sent everything trace__set_filter_pids() returned to out_error_mem, which prints "Not enough memory to run!". That was already a guess, and a wrong one now the function writes BPF maps too.
- Patch 7 moves the pid across in sched_process_exec() by deleting the old key before inserting the new one. The move is only a rename, but holding both keys at once needs a spare slot, and on a full map the insert failed with -E2BIG while the delete still succeeded, losing the task instead of moving it.
- Patch 7 no longer claims that a task forked during the attach window is still traced unaugmented. That was wrong: cmd_trace() removes the sys_enter evsel once the bpf-output event exists, so __augmented_syscalls__ is the only source of enter events and such a task is not reported at all. Describe what is really lost, why the tgid fallback is not kept as a safety net for it, note that only 'perf trace -p' is exposed, and that closing it needs the target's descendants re-enumerated from /proc after the attach.
- Patch 7 drops the parent tgid test from sched_process_fork(), so a child inherits from the pid of the thread that called clone() and from nothing else. The lookups only ever match a task's own pid, and every thread of a -p target is enumerated from /proc/<pid>/task and inserted in its own right, so the tgid added no reach. What it did add was a child inheriting from a thread that is not traced itself, such as a sibling of the thread 'perf trace -t' selected.
- Patch 7 ends the session with the error that caused it when the BPF programs cannot be attached, rather than printing a bare errno left over from unwinding the attach. There is nothing to fall back to at that point, cmd_trace() has already built the evlist around __augmented_syscalls__ and dropped the sys_enter evsel, and the code being replaced did not fall back either: it ignored the result of augmented_raw_syscalls_bpf__attach() altogether.
- New patch 8 reads the target out of /proc again once the BPF programs are attached, so a task the target created while perf trace was starting up is traced rather than missed for the whole session. This is the gap patch 7 describes and left for later. It covers the target's new threads and, through task->children, anything it or they forked, to any depth. What is left is a task that made syscalls between sys_enter going live and being added to the map, which is momentary rather than lasting for the session.
- Patch 9 bails out if mktemp fails rather than carrying on with an empty $tmpdir. cd rejects the null directory, and the rmdir that was meant to undo the mktemp then failed to remove '' instead. Its cleanup() also no longer exits when it cannot cd out of the temporary directory, which would have skipped removing it. The removal takes an absolute path and does not need the cd to have succeeded.
- Patch 10 now narrows the disable in clear_all_probes() to the probes themselves rather than dropping it. Clearing kprobe_events or uprobe_events is all or nothing: dyn_events_release_all() returns -EBUSY without removing anything if it finds a probe that is still enabled, so simply removing the write could leave stale probes behind to collide with the next run. The set to disable is read from the kprobe_events and uprobe_events listings rather than assumed to be the groups perf uses, since a probe left enabled in another group, such as the default kprobes group used when kprobe_events is written directly, would abort the clear just the same.
- Patch 11 deletes the probes from an exit trap as well as on the way out. A pid scoped name is never seen again, so a run interrupted before cleanup_probe_vfs_getname() left its probes behind for good, a set per run, where the fixed name was at least found and reused by the next run.
- Patch 12 retries deleting the uprobe. Deletion writes uprobe_events just as addition does and can lose the same race with a concurrent test, and because the event name is now pid scoped a probe left behind is never overwritten by a later run. It also bails out if mktemp fails and quotes the path in the emptiness check, which unquoted would have tested the string "-s" and reported success.
- Patch 13 prints the tail of the output rather than the head. The pattern it matches only appears in the summary, which is printed after any trace output, so the head of the file is not the part that failed to match.
Changes since v1:
- New patch 1 includes <sys/types.h> and <string.h> for the pid_t and strcmp() uses that were relying on the include chain happening to drag them in, which does not hold on libcs such as musl.
- Patch 6 no longer returns success when the event qualifier filter string fails to allocate. err now defaults to 0 because either tracepoint may legitimately be absent, so the ENOMEM path has to set the error itself rather than rely on that default. It also includes <stdbool.h> for the bool parameters it adds to trace_augment.h.
- Patch 9 removes the temporary directory if the cd into it fails. That happens before the cleanup trap is installed, so the directory would otherwise be left behind in /tmp.
Ian Rogers (23):
perf trace: Set the augmented arg header in the augmenters that omit
it
perf trace: Include the augmented arg header in nanosleep's payload
length
perf trace: Include the headers declaring pid_t, strcmp and assert perf trace: Free the whole evsel_trace in evsel__put_and_free_priv perf evsel: Report an allocation failure as ENOMEM when setting
filters
perf trace: Start BPF summary before starting workload perf trace: Skip internal tracepoint fields in formatting and beauty
map
perf trace: Bounds check augmented arguments before reading them perf trace: Bound the fixed size augmented argument beautifiers perf trace: Do not read sample padding as an augmented argument perf trace: Do not set unaugmented BPF program on sys_exit map perf trace: Filter events in BPF and avoid tracepoint vetoes perf trace: Handle fork and exit directly in BPF filter maps perf trace: Enumerate the target again once BPF is attached perf trace: Drop targets that died before they were filtered perf test test_task_analyzer: Isolate in temporary directory and make
non-exclusive
perf test common: Only disable probes in clear_all_probes perf test probe_vfs_getname: Scope probe name to PID and make
non-exclusive
perf test record+probe_libc_inet_pton: Scope event to PID, add
retries, and make non-exclusive
perf test trace_summary: Improve error diagnostics
perf test trace_btf_general: Drop --max-events=1 and make
non-exclusive
perf test trace_summary: Make non-exclusive
perf test uprobe_from_different_cu: Scope probe name to PID
tools/perf/Documentation/perf-trace.txt | 5 + tools/perf/builtin-trace.c | 863 +++++++++++++++--- tools/perf/tests/shell/common/init.sh | 33 +- .../perf/tests/shell/lib/probe_vfs_getname.sh | 51 +- tools/perf/tests/shell/probe_vfs_getname.sh | 3 +- .../shell/record+probe_libc_inet_pton.sh | 107 ++- .../shell/record+script_probe_vfs_getname.sh | 18 +- tools/perf/tests/shell/test_task_analyzer.sh | 20 +- .../shell/test_uprobe_from_different_cu.sh | 11 +- .../tests/shell/trace+probe_vfs_getname.sh | 9 + tools/perf/tests/shell/trace_btf_general.sh | 8 +- tools/perf/tests/shell/trace_summary.sh | 16 +- tools/perf/trace/beauty/beauty.h | 43 + tools/perf/trace/beauty/perf_event_open.c | 38 +- tools/perf/trace/beauty/sockaddr.c | 55 +- tools/perf/trace/beauty/timespec.c | 2 +- .../bpf_skel/augmented_raw_syscalls.bpf.c | 407 ++++++++- tools/perf/util/bpf_trace_augment.c | 288 +++++- tools/perf/util/evlist.c | 12 +- tools/perf/util/evsel.c | 4 +- tools/perf/util/trace_augment.h | 33 +- 21 files changed, 1801 insertions(+), 225 deletions(-)
base-commit: 9db4e9d6cc7c7d318fb6d6bb73abe3fed27385a8
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Ian Rogers
reviewerPATCH v5 1/23 perf trace: Set the augmented arg header in the augmenters that omit it
An augmented argument is a struct augmented_arg header, holding the length of the payload and an error code, followed by the payload. The augmenters build it in augmented_args_tmp, a single entry per-CPU array reused by every syscall on that CPU, so a field left unassigned holds whatever the previous syscall on that CPU put there rather than anything about this one.
The string augmenters get both fields from augmented_arg__read_str(), and sys_enter_perf_event_open() bails out when its read fails, but sys_enter_connect(), sys_enter_sendto(), sys_enter_clock_nanosleep() and sys_enter_nanosleep() copy the payload and output the record without ever describing it, and augment_arg() sets the length but not the error.
This has gone unnoticed because the beautifiers for those payloads take a fixed sized type and so read the value without consulting the header:syscall_arg__scnprintf_augmented_sockaddr(), syscall_arg__scnprintf_augmented_timespec() and syscall_arg__scnprintf_augmented_perf_event_attr() all cast augmented.args->value directly. Nothing has yet read a length that was never written.
Describe the payload everywhere one is produced, so that a reader can bound it by its length.
These reads can fail, and none of the four checked whether they had. Rather than claim a payload that was not read, report a length of zero and the error, so the record describes what it holds and leaves the scratch out of it. A reader that bounds the payload by the header then shows the pointer, as it does for a syscall with no augmentation at all, rather than another task's data.
The error is set for the same reason the length is, that the header is in a buffer the next syscall on this CPU will reuse and so carries the previous one's value if it is not written, rather than because anything reads it yet: beauty.h still calls the field int_arg and no beautifier looks at it. augment_arg() reported a string it could not read as an empty one before this and still does.
--- .../bpf_skel/augmented_raw_syscalls.bpf.c | 53 ++++++++++++++++--- 1 file changed, 47 insertions(+), 6 deletions(-)
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +47 -6
@@ -210,6 +210,7 @@ int sys_enter_connect(struct syscall_enter_args *args)const void *sockaddr_arg = (const void *)args->args[1 ];unsigned int socklen = args->args[2 ];unsigned int len =sizeof (u64) +sizeof (augmented_args->args);// the size + err in all 'augmented_arg' structs +int err;if (augmented_args ==NULL )return 1 ;/* Failure: don't filter */
@@ -217,9 +218,16 @@ int sys_enter_connect(struct syscall_enter_args *args)_Static_assert (is_power_of_2 (sizeof (augmented_args->arg.saddr)),"sizeof(augmented_args->arg.saddr) needs to be a power of two" ); socklen &=sizeof (augmented_args->arg.saddr) -1 ; -bpf_probe_read_user (&augmented_args->arg.saddr, socklen, sockaddr_arg); + err =bpf_probe_read_user (&augmented_args->arg.saddr, socklen, sockaddr_arg); +/* +* A failed read leaves the scratch holding whatever the previous +* syscall on this CPU put there, so say there is no payload and why, +* rather than describing another task's data as this task's sockaddr. +*/ +if (err <0 ) + socklen =0 ; augmented_args->arg.size = socklen; - augmented_args->arg.err =0 ; + augmented_args->arg.err = err <0 ? err :0 ;return augmented__output (args, augmented_args, len + socklen); }
@@ -231,13 +239,19 @@ int sys_enter_sendto(struct syscall_enter_args *args)const void *sockaddr_arg = (const void *)args->args[4 ];unsigned int socklen = args->args[5 ];unsigned int len =sizeof (u64) +sizeof (augmented_args->args);// the size + err in all 'augmented_arg' structs +int err;if (augmented_args ==NULL )return 1 ;/* Failure: don't filter */ socklen &=sizeof (augmented_args->arg.saddr) -1 ; -bpf_probe_read_user (&augmented_args->arg.saddr, socklen, sockaddr_arg); + err =bpf_probe_read_user (&augmented_args->arg.saddr, socklen, sockaddr_arg); +/* As in sys_enter_connect(), do not describe scratch as a sockaddr. */ +if (err <0 ) + socklen =0 ; + augmented_args->arg.size = socklen; + augmented_args->arg.err = err <0 ? err :0 ;return augmented__output (args, augmented_args, len + socklen); }
@@ -372,6 +386,9 @@ int sys_enter_perf_event_open(struct syscall_enter_args *args)if (bpf_probe_read_user (&augmented_args->arg.value, size, attr) <0 ) goto failure; + augmented_args->arg.size = size; + augmented_args->arg.err =0 ; +return augmented__output (args, augmented_args, len + size); failure:return 1 ;/* Failure: don't filter */
@@ -384,6 +401,7 @@ int sys_enter_clock_nanosleep(struct syscall_enter_args *args)const void *rqtp_arg = (const void *)args->args[2 ];unsigned int len =sizeof (u64) +sizeof (augmented_args->args);// the size + err in all 'augmented_arg' structs __u32 size =sizeof (struct timespec64 ); +int err;if (augmented_args ==NULL ) goto failure;
@@ -391,7 +409,12 @@ int sys_enter_clock_nanosleep(struct syscall_enter_args *args)if (size >sizeof (augmented_args->arg.value)) goto failure; -bpf_probe_read_user (&augmented_args->arg.value, size, rqtp_arg); + err =bpf_probe_read_user (&augmented_args->arg.value, size, rqtp_arg); +/* As in sys_enter_connect(), do not describe scratch as a timespec. */ +if (err <0 ) + size =0 ; + augmented_args->arg.size = size; + augmented_args->arg.err = err <0 ? err :0 ;return augmented__output (args, augmented_args, len + size); failure:
@@ -405,6 +428,7 @@ int sys_enter_nanosleep(struct syscall_enter_args *args)const void *req_arg = (const void *)args->args[0 ];unsigned int len =sizeof (augmented_args->args);__u32 size =sizeof (struct timespec64 ); +int err;if (augmented_args ==NULL ) goto failure;
@@ -412,7 +436,12 @@ int sys_enter_nanosleep(struct syscall_enter_args *args)if (size >sizeof (augmented_args->arg.value)) goto failure; -bpf_probe_read_user (&augmented_args->arg.value, size, req_arg); + err =bpf_probe_read_user (&augmented_args->arg.value, size, req_arg); +/* As in sys_enter_connect(), do not describe scratch as a timespec. */ +if (err <0 ) + size =0 ; + augmented_args->arg.size = size; + augmented_args->arg.err = err <0 ? err :0 ;return augmented__output (args, augmented_args, len + size); failure:
@@ -445,6 +474,7 @@ static inline int augment_arg(struct syscall_enter_args *args, int i, struct beauty_payload_enter *payload, u64 offset) { int index, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value); +int read_err =0 ;struct augmented_arg *payload_offset;s64 aug_size, size;bool augmented;
@@ -467,8 +497,18 @@ static inline int augment_arg(struct syscall_enter_args *args, int i,if (size ==1 ) {/* string */ aug_size =bpf_probe_read_user_str (payload_offset->value, value_size, arg);/* minimum of 0 to pass the verifier */ -if (aug_size <0 ) +if (aug_size <0 ) { +/* +* Record why nothing was read. The header sits in +* scratch that the next syscall on this CPU reuses, +* so an error left unwritten is the previous one's. +* No beautifier reads it yet, beauty.h still calls +* the field int_arg, so the string is still shown +* as an empty one. +*/ + read_err = aug_size; aug_size =0 ; + } augmented = true; }else if (size >0 && size <= value_size) {/* struct */
@@ -498,6 +538,7 @@ static inline int augment_arg(struct syscall_enter_args *args, int i,return -1 ; payload_offset->size = aug_size; + payload_offset->err = read_err;return written; }
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
[email protected]
AI reviewRe: [PATCH v5 01/23] perf trace: Set the augmented arg header in the augmenters that omit it
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
An augmented argument is a struct
augmented_argheader, holding the length of the payload and an error code, followed by the payload. The augmenters build it inaugmented_args_tmp, a single entry per-CPU array reused by every syscall on that CPU, so a field left unassigned holds whatever the previous syscall on that CPU put there rather than anything about this one.The string augmenters get both fields from
augmented_arg__read_str(), andsys_enter_perf_event_open()bails out when its read fails, butsys_enter_connect(),sys_enter_sendto(),sys_enter_clock_nanosleep()andsys_enter_nanosleep()copy the payload and output the record without ever describing it, andaugment_arg()sets the length but not the error.
Quoted 8 lines
This has gone unnoticed because the beautifiers for those payloads take a fixed sized type and so read the value without consulting the header:
[ ... ]
looks at it.augment_arg()reported a string it could not read as an empty one before this and still does.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Namhyung Kim
maintainerRe: [PATCH v5 01/23] perf trace: Set the augmented arg header in the augmenters that omit it
In reply to Ian RogersQuoted 62 lines · Ian Rogers
An augmented argument is a struct
augmented_argheader, holding the length of the payload and an error code, followed by the payload. The augmenters build it inaugmented_args_tmp, a single entry per-CPU array reused by every syscall on that CPU, so a field left unassigned holds whatever the previous syscall on that CPU put there rather than anything about this one.The string augmenters get both fields from
augmented_arg__read_str(), andsys_enter_perf_event_open()bails out when its read fails, butsys_enter_connect(),sys_enter_sendto(),sys_enter_clock_nanosleep()andsys_enter_nanosleep()copy the payload and output the record without ever describing it, andaugment_arg()sets the length but not the error.This has gone unnoticed because the beautifiers for those payloads take a fixed sized type and so read the value without consulting the header:
syscall_arg__scnprintf_augmented_sockaddr(),syscall_arg__scnprintf_augmented_timespec()andsyscall_arg__scnprintf_augmented_perf_event_attr()all castaugmented.args->valuedirectly. Nothing has yet read a length that was never written.Describe the payload everywhere one is produced, so that a reader can bound it by its length.
These reads can fail, and none of the four checked whether they had. Rather than claim a payload that was not read, report a length of zero and the error, so the record describes what it holds and leaves the scratch out of it. A reader that bounds the payload by the header then shows the pointer, as it does for a syscall with no augmentation at all, rather than another task's data.
The error is set for the same reason the length is, that the header is in a buffer the next syscall on this CPU will reuse and so carries the previous one's value if it is not written, rather than because anything reads it yet:
beauty.hstill calls the fieldint_argand no beautifier looks at it.augment_arg()reported a string it could not read as an empty one before this and still does.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>--- .../bpf_skel/augmented_raw_syscalls.bpf.c | 53 ++++++++++++++++--- 1 file changed, 47 insertions(+), 6 deletions(-)tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +2
-1@@ -210,6 +210,7 @@ int sys_enter_connect(struct syscall_enter_args *args)const void *sockaddr_arg = (const void *)args->args[1 ];unsigned int socklen = args->args[2 ];unsigned int len =sizeof (u64) +sizeof (augmented_args->args);// the size + err in all 'augmented_arg' structs +int err;if (augmented_args ==NULL )return 1 ;/* Failure: don't filter */ @@ -217,9 +218,16 @@ int sys_enter_connect(struct syscall_enter_args *args)_Static_assert (is_power_of_2 (sizeof (augmented_args->arg.saddr)),"sizeof(augmented_args->arg.saddr) needs to be a power of two" ); socklen &=sizeof (augmented_args->arg.saddr) -1 ; -bpf_probe_read_user (&augmented_args->arg.saddr, socklen, sockaddr_arg); + err =bpf_probe_read_user (&augmented_args->arg.saddr, socklen, sockaddr_arg);
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c int sys_enter_connect(struct syscall_enter_args *args) · line 230
Quoted 10 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +8
-1+/* +* A failed read leaves the scratch holding whatever the previous +* syscall on this CPU put there, so say there is no payload and why, +* rather than describing another task's data as this task's sockaddr. +*/ +if (err <0 ) + socklen =0 ; augmented_args->arg.size = socklen; - augmented_args->arg.err =0 ; + augmented_args->arg.err = err <0 ? err :0 ;
It seems we can simply use the return value of bpf_probe_read_user() as it only returns 0 or a negative error according to the doc.
https://docs.ebpf.io/linux/helper-function/bpf_probe_read_user/
Quoted 90 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +25
-3return augmented__output (args, augmented_args, len + socklen); }@@ -231,13 +239,19 @@ int sys_enter_sendto(struct syscall_enter_args *args)const void *sockaddr_arg = (const void *)args->args[4 ];unsigned int socklen = args->args[5 ];unsigned int len =sizeof (u64) +sizeof (augmented_args->args);// the size + err in all 'augmented_arg' structs +int err;if (augmented_args ==NULL )return 1 ;/* Failure: don't filter */ socklen &=sizeof (augmented_args->arg.saddr) -1 ; -bpf_probe_read_user (&augmented_args->arg.saddr, socklen, sockaddr_arg); + err =bpf_probe_read_user (&augmented_args->arg.saddr, socklen, sockaddr_arg); +/* As in sys_enter_connect(), do not describe scratch as a sockaddr. */ +if (err <0 ) + socklen =0 ; + augmented_args->arg.size = socklen; + augmented_args->arg.err = err <0 ? err :0 ;return augmented__output (args, augmented_args, len + socklen); }@@ -372,6 +386,9 @@ int sys_enter_perf_event_open(struct syscall_enter_args *args)if (bpf_probe_read_user (&augmented_args->arg.value, size, attr) <0 ) goto failure; + augmented_args->arg.size = size; + augmented_args->arg.err =0 ; +return augmented__output (args, augmented_args, len + size); failure:return 1 ;/* Failure: don't filter */ @@ -384,6 +401,7 @@ int sys_enter_clock_nanosleep(struct syscall_enter_args *args)const void *rqtp_arg = (const void *)args->args[2 ];unsigned int len =sizeof (u64) +sizeof (augmented_args->args);// the size + err in all 'augmented_arg' structs __u32 size =sizeof (struct timespec64 ); +int err;if (augmented_args ==NULL ) goto failure;@@ -391,7 +409,12 @@ int sys_enter_clock_nanosleep(struct syscall_enter_args *args)if (size >sizeof (augmented_args->arg.value)) goto failure; -bpf_probe_read_user (&augmented_args->arg.value, size, rqtp_arg); + err =bpf_probe_read_user (&augmented_args->arg.value, size, rqtp_arg); +/* As in sys_enter_connect(), do not describe scratch as a timespec. */ +if (err <0 ) + size =0 ; + augmented_args->arg.size = size; + augmented_args->arg.err = err <0 ? err :0 ;return augmented__output (args, augmented_args, len + size); failure:@@ -405,6 +428,7 @@ int sys_enter_nanosleep(struct syscall_enter_args *args)const void *req_arg = (const void *)args->args[0 ];unsigned int len =sizeof (augmented_args->args);__u32 size =sizeof (struct timespec64 ); +int err;if (augmented_args ==NULL ) goto failure;@@ -412,7 +436,12 @@ int sys_enter_nanosleep(struct syscall_enter_args *args)if (size >sizeof (augmented_args->arg.value)) goto failure; -bpf_probe_read_user (&augmented_args->arg.value, size, req_arg); + err =bpf_probe_read_user (&augmented_args->arg.value, size, req_arg); +/* As in sys_enter_connect(), do not describe scratch as a timespec. */ +if (err <0 ) + size =0 ; + augmented_args->arg.size = size; + augmented_args->arg.err = err <0 ? err :0 ;return augmented__output (args, augmented_args, len + size); failure:@@ -445,6 +474,7 @@ static inline int augment_arg(struct syscall_enter_args *args, int i, struct beauty_payload_enter *payload, u64 offset) { int index, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value); +int read_err =0 ;struct augmented_arg *payload_offset;s64 aug_size, size;bool augmented;@@ -467,8 +497,18 @@ static inline int augment_arg(struct syscall_enter_args *args, int i,if (size ==1 ) {/* string */ aug_size =bpf_probe_read_user_str (payload_offset->value, value_size, arg);/* minimum of 0 to pass the verifier */
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c static inline int augment_arg(struct syscall_enter_args *args, int i, · line 508
Quoted 10 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +9
-1-if (aug_size <0 ) +if (aug_size <0 ) { +/* +* Record why nothing was read. The header sits in +* scratch that the next syscall on this CPU reuses, +* so an error left unwritten is the previous one's. +* No beautifier reads it yet, beauty.h still calls +* the field int_arg, so the string is still shown +* as an empty one. +*/
Do we really need this comment? Looks too verbose.
Thanks,
Namhyung
Quoted 17 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +3
-0+ read_err = aug_size; aug_size =0 ; + } augmented = true; }else if (size >0 && size <= value_size) {/* struct */ @@ -498,6 +538,7 @@ static inline int augment_arg(struct syscall_enter_args *args, int i,return -1 ; payload_offset->size = aug_size; + payload_offset->err = read_err;return written; }Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Ian Rogers
reviewerPATCH v5 2/23 perf trace: Include the augmented arg header in nanosleep's payload length
In reply to Ian Rogerssys_enter_nanosleep() sizes the record it outputs as
unsigned int len = sizeof(augmented_args->args);
where every other augmenter that appends a payload uses
unsigned int len = sizeof(u64) + sizeof(augmented_args->args);
the extra u64 being the size and err fields of the struct augmented_arg that the payload follows.
The payload is copied to augmented_args->arg.value, which starts 8 bytes after the tracepoint arguments, but only len + size bytes are output. So the record stops 8 bytes short of the end of the timespec, and userspace reads the tail of tv_nsec from whatever follows in the ring buffer.
Count the header, as the other augmenters do.
--- tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +2 -1
@@ -426,7 +426,8 @@ int sys_enter_nanosleep(struct syscall_enter_args *args) {struct augmented_args_payload *augmented_args =augmented_args_payload ();const void *req_arg = (const void *)args->args[0 ]; -unsigned int len =sizeof (augmented_args->args); +// the size + err in all 'augmented_arg' structs +unsigned int len =sizeof (u64) +sizeof (augmented_args->args);__u32 size =sizeof (struct timespec64 );int err;
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
[email protected]
AI reviewRe: [PATCH v5 02/23] perf trace: Include the augmented arg header in nanosleep's payload length
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] bpf: use of C99-style // comment violates BPF comment style guidelines --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] bpf: use of C99-style // comment violates BPF comment style guidelines --
commit fdd001e20d7e2bd912d88ce8d78d5f254e09e3cd
Author: Ian Rogers <[email protected]>
perf trace: Include the augmented arg header in nanosleep's payload length
This commit fixes how sys_enter_nanosleep() sizes its output record. It includes the size of the augmented_arg header in the length calculation, matching the behavior of other augmenters. This ensures the entire timespec payload is correctly output to the ring buffer.
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c int sys_enter_nanosleep(struct syscall_enter_args *args) · line 429
Quoted 9 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +1
-1@@ -426,7 +426,8 @@ int sys_enter_nanosleep(struct syscall_enter_args *args) {struct augmented_args_payload *augmented_args =augmented_args_payload ();const void *req_arg = (const void *)args->args[0 ]; -unsigned int len =sizeof (augmented_args->args); +// the size + err in all 'augmented_arg' structs
[Severity: Low]
This isn't a bug, but does this violate the BPF comment style guidelines? The BPF subsystem requires comments to use the standard /* */ format rather than C99-style // comments, even for single lines.
Quoted 3 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +1
-0+unsigned int len =sizeof (u64) +sizeof (augmented_args->args);__u32 size =sizeof (struct timespec64 );int err;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Ian Rogers
reviewerRe: [PATCH v5 02/23] perf trace: Include the augmented arg header in nanosleep's payload length
Quoted 15 lines · On Wed, Sep 23, 2026 at 12:25 AM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] bpf: use of C99-style // comment violates BPF comment style guidelines --commit
fdd001e20d7e2bd912d88ce8d78d5f254e09e3cd
Author: Ian Rogers <[email protected]>perf trace: Include the augmented arg header in nanosleep's payload length
This commit fixes how
sys_enter_nanosleep()sizes its output record. It includes the size of theaugmented_argheader in the length calculation, matching the behavior of other augmenters. This ensures the entire timespec payload is correctly output to the ring buffer.
Quoted 14 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c int sys_enter_nanosleep(struct syscall_enter_args *args) · line 429
Quoted 9 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +1
-1@@ -426,7 +426,8 @@ int sys_enter_nanosleep(struct syscall_enter_args *args) {struct augmented_args_payload *augmented_args =augmented_args_payload ();const void *req_arg = (const void *)args->args[0 ]; -unsigned int len =sizeof (augmented_args->args); +// the size + err in all 'augmented_arg' structs [Severity: Low]
This isn't a bug, but does this violate the BPF comment style guidelines? The BPF subsystem requires comments to use the standard /* */ format rather than C99-style // comments, even for single lines.
Ack. The code already uses // for the same comment elsewhere, so using // is more consistent. There should likely be a global clean up of BPF code to use /* */ comments. I'll not spin a new version for this change.
Thanks,
Ian
Quoted 6 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c int sys_enter_nanosleep(struct syscall_enter_args *args) · line 432
Quoted 3 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +1
-0+unsigned int len =sizeof (u64) +sizeof (augmented_args->args);__u32 size =sizeof (struct timespec64 );int err;--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Ian Rogers
reviewerPATCH v5 3/23 perf trace: Include the headers declaring pid_t, strcmp and assert
In reply to Ian Rogerstrace_augment.h uses pid_t in the augmented_syscalls__set_filter_pids() prototype and in the !HAVE_BPF_SKEL stub. bpf_trace_augment.c calls strcmp() in augmented_syscalls__find_by_title() and assert() in augmented_syscalls__create_bpf_output(), but neither pulls in the header that declares what it uses. Both happen to build today only because something else in the include chain drags <sys/types.h>, <string.h> and <assert.h> in first, which is not guaranteed and does not hold on libcs such as musl that keep the POSIX namespaces strictly separated.
Include <sys/types.h>, <string.h> and <assert.h> explicitly.
--- tools/perf/util/bpf_trace_augment.c | 2 ++ tools/perf/util/trace_augment.h | 1 + 2 files changed, 3 insertions(+)
tools/perf/util/bpf_trace_augment.c +2 -0
@@ -1,5 +1,7 @@ +#include <assert.h> #include <bpf/libbpf.h> #include <internal/xyarray.h> +#include <string.h> #include "bpf_skel/augmented_raw_syscalls.skel.h" #include "debug.h"
tools/perf/util/trace_augment.h +1 -0
@@ -2,6 +2,7 @@#define TRACE_AUGMENT_H #include <linux/compiler.h> +#include <sys/types.h> struct bpf_program ;struct evlist ;
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
[email protected]
AI reviewRe: [PATCH v5 03/23] perf trace: Include the headers declaring pid_t, strcmp and assert
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
trace_augment.husespid_tin theaugmented_syscalls__set_filter_pids()prototype and in the !HAVE_BPF_SKEL stub.bpf_trace_augment.ccallsstrcmp()inaugmented_syscalls__find_by_title()andassert()inaugmented_syscalls__create_bpf_output(), but neither pulls in the header that declares what it uses. Both happen to build today only because something else in the include chain drags <sys/types.h>, <string.h> and <assert.h> in first, which is not guaranteed and does not hold on libcs such as musl that keep the POSIX namespaces strictly separated.
Quoted 5 lines
Include <
sys/types.h>, <string.h> and <assert.h> explicitly.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>Signed-off-by Arnaldo Carvalho de Melo <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Namhyung Kim
maintainerRe: [PATCH v5 03/23] perf trace: Include the headers declaring pid_t, strcmp and assert
In reply to Ian RogersQuoted 9 lines · Ian Rogers
trace_augment.husespid_tin theaugmented_syscalls__set_filter_pids()prototype and in the !HAVE_BPF_SKEL stub.bpf_trace_augment.ccallsstrcmp()inaugmented_syscalls__find_by_title()andassert()inaugmented_syscalls__create_bpf_output(), but neither pulls in the header that declares what it uses. Both happen to build today only because something else in the include chain drags <sys/types.h>, <string.h> and <assert.h> in first, which is not guaranteed and does not hold on libcs such as musl that keep the POSIX namespaces strictly separated.
Quoted 5 lines
Include <
sys/types.h>, <string.h> and <assert.h> explicitly.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>Signed-off-by Arnaldo Carvalho de Melo <[email protected]>
I'm not sure why it has Arnaldo's Sign-off..
Thanks,
Namhyung
Quoted 32 lines
--- tools/perf/util/bpf_trace_augment.c | 2 ++ tools/perf/util/trace_augment.h | 1 + 2 files changed, 3 insertions(+)tools/perf/util/bpf_trace_augment.c +2
-0@@ -1,5 +1,7 @@ +#include <assert.h> #include <bpf/libbpf.h> #include <internal/xyarray.h> +#include <string.h> #include "bpf_skel/augmented_raw_syscalls.skel.h" #include "debug.h" tools/perf/util/trace_augment.h +1
-0@@ -2,6 +2,7 @@#define TRACE_AUGMENT_H #include <linux/compiler.h> +#include <sys/types.h> struct bpf_program ;struct evlist ;Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Ian Rogers
reviewerPATCH v5 4/23 perf trace: Free the whole evsel_trace in evsel__put_and_free_priv
In reply to Ian RogersEvery evsel->priv in builtin-trace.c is a struct evsel_trace, allocated by evsel_trace__new(). It holds a syscall_arg_fmt array in its fmt member, which evsel__syscall_arg_fmt() allocates on demand for the syscalls:sys_{enter,exit}_SYSCALL tracepoints and for every other tracepoint that gets its arguments pretty printed.
evsel__put_and_free_priv() only did zfree(&evsel->priv), releasing the evsel_trace itself and leaking that array. Use evsel_trace__delete(), which frees fmt first, exactly as the out_delete path of evsel__syscall_arg_fmt() already does.
The current callers are all error paths that run before fmt can have been allocated, so nothing leaks in practice today, but the helper is the obvious thing to reach for whenever an evsel is discarded and it should be safe for that.
--- tools/perf/builtin-trace.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
tools/perf/builtin-trace.c +7 -1
@@ -464,7 +464,13 @@ static int evsel__init_tp_ptr_field(struct evsel *evsel, struct tp_field *field,static void evsel__put_and_free_priv (struct evsel *evsel) { -zfree (&evsel->priv); +/* +* evsel->priv is always a struct evsel_trace here, so it has to go +* through evsel_trace__delete(): zfree() on its own would release the +* struct while leaking the syscall_arg_fmt array hanging off it. +*/ +evsel_trace__delete (evsel->priv); + evsel->priv =NULL ;evsel__put (evsel); }
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
[email protected]
AI reviewRe: [PATCH v5 04/23] perf trace: Free the whole evsel_trace in evsel__put_and_free_priv
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines
Every
evsel->privinbuiltin-trace.cis a structevsel_trace, allocated byevsel_trace__new(). It holds asyscall_arg_fmtarray in its fmt member, whichevsel__syscall_arg_fmt()allocates on demand for the syscalls:sys_{enter,exit}_SYSCALL tracepoints and for every other tracepoint that gets its arguments pretty printed.
evsel__put_and_free_priv()only didzfree(&evsel->priv), releasing theevsel_traceitself and leaking that array. Useevsel_trace__delete(), which frees fmt first, exactly as theout_deletepath ofevsel__syscall_arg_fmt()already does.
Quoted 8 lines
The current callers are all error paths that run before fmt can have been allocated, so nothing leaks in practice today, but the helper is the obvious thing to reach for whenever an evsel is discarded and it should be safe for that.
Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>Signed-off-by Arnaldo Carvalho de Melo <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Ian Rogers
reviewerPATCH v5 5/23 perf evsel: Report an allocation failure as ENOMEM when setting filters
In reply to Ian Rogersevsel__set_filter() and evsel__append_filter() return -1 when the strdup() or asprintf() that builds the new filter string fails, and evlist__set_tp_filter() and evlist__append_tp_filter() do the same when handed the NULL that asprintf__tp_filter_pids() returns for the same reason. In every case the only thing that can have gone wrong is an allocation.
Callers all test the result with "< 0" or for being non-zero, so -1 has been as good as any other error so far, but it is not an errno and so cannot be printed as one. A caller that does, such as
str_error_r(-err, errbuf, sizeof(errbuf))
turns it into EPERM and reports a failure to allocate as "Operation not permitted", which is no help to anyone trying to work out what happened.
Return -ENOMEM instead, which is what these failures are.
--- tools/perf/util/evlist.c | 12 ++++++++++-- tools/perf/util/evsel.c | 4 ++-- 2 files changed, 12 insertions(+), 4 deletions(-)
tools/perf/util/evlist.c +10 -2
@@ -1432,8 +1432,12 @@ int evlist__set_tp_filter(struct evlist *evlist, const char *filter)struct evsel *evsel;int err =0 ; +/* +* The only caller that passes NULL is evlist__set_tp_filter_pids(), +* where it means asprintf__tp_filter_pids() failed to allocate. +*/ if (filter ==NULL ) -return -1 ; +return -ENOMEM ;evlist__for_each_entry (evlist, evsel) {if (evsel->core.attr.type !=PERF_TYPE_TRACEPOINT )
@@ -1452,8 +1456,12 @@ int evlist__append_tp_filter(struct evlist *evlist, const char *filter)struct evsel *evsel;int err =0 ; +/* +* As above, a NULL filter is asprintf__tp_filter_pids() having failed +* to allocate in evlist__append_tp_filter_pids(). +*/ if (filter ==NULL ) -return -1 ; +return -ENOMEM ;evlist__for_each_entry (evlist, evsel) {if (evsel->core.attr.type !=PERF_TYPE_TRACEPOINT )
tools/perf/util/evsel.c +2 -2
@@ -1872,7 +1872,7 @@ int evsel__set_filter(struct evsel *evsel, const char *filter)return 0 ; } -return -1 ; +return -ENOMEM ; }static int evsel__append_filter (struct evsel *evsel,const char *fmt,const char *filter)
@@ -1888,7 +1888,7 @@ static int evsel__append_filter(struct evsel *evsel, const char *fmt, const charreturn 0 ; } -return -1 ; +return -ENOMEM ; }int evsel__append_tp_filter (struct evsel *evsel,const char *filter)
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
[email protected]
AI reviewRe: [PATCH v5 05/23] perf evsel: Report an allocation failure as ENOMEM when setting filters
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
evsel__set_filter()andevsel__append_filter()return -1 when thestrdup()orasprintf()that builds the new filter string fails, andevlist__set_tp_filter()andevlist__append_tp_filter()do the same when handed the NULL thatasprintf__tp_filter_pids()returns for the same reason. In every case the only thing that can have gone wrong is an allocation.Callers all test the result with "< 0" or for being non-zero, so -1 has been as good as any other error so far, but it is not an errno and so cannot be printed as one. A caller that does, such as
str_error_r(-err, errbuf, sizeof(errbuf))
Quoted 8 lines
turns it into EPERM and reports a failure to allocate as "Operation not permitted", which is no help to anyone trying to work out what happened.
Return
-ENOMEMinstead, which is what these failures are.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>Signed-off-by Arnaldo Carvalho de Melo <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Ian Rogers
reviewerPATCH v5 6/23 perf trace: Start BPF summary before starting workload
In reply to Ian RogersWhen using --bpf-summary, trace_start_bpf_summary() sets skel->bss->enabled = 1. In trace__run(), trace_start_bpf_summary() was previously invoked after evlist__start_workload().
Because evlist__start_workload() immediately unblocks the child process by writing to its go_pipe, short-lived workloads (such as cat /dev/null) can execute and complete their initial system calls before trace_start_bpf_summary() is reached by the parent process. Furthermore, under high system load, the child process may finish before the BPF summary tracking is enabled in the kernel at all, causing syscall summary tests to fail. Additionally, if initial_delay was configured, the workload was started before sleeping.
Move trace_start_bpf_summary() to be invoked before evlist__start_workload(), matching evlist__enable(), and ensure it respects target.initial_delay.
--- tools/perf/builtin-trace.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
tools/perf/builtin-trace.c +5 -3
@@ -4844,17 +4844,19 @@ static int trace__run(struct trace *trace, int argc, const char **argv)if (!target__none (&trace->opts.target) && !trace->opts.target.initial_delay)evlist__enable (evlist); +if (trace->summary_bpf && !trace->opts.target.initial_delay) +trace_start_bpf_summary (); +if (forks)evlist__start_workload (evlist);if (trace->opts.target.initial_delay) {usleep (trace->opts.target.initial_delay *1000 );evlist__enable (evlist); +if (trace->summary_bpf) +trace_start_bpf_summary (); } -if (trace->summary_bpf) -trace_start_bpf_summary (); - trace->multiple_threads =perf_thread_map__pid (evlist__core (evlist)->threads,0 ) ==-1 ||perf_thread_map__nr (evlist__core (evlist)->threads) >1 ||evlist__first (evlist)->core.attr.inherit;
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
[email protected]
AI reviewRe: [PATCH v5 06/23] perf trace: Start BPF summary before starting workload
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
When using --bpf-summary,
trace_start_bpf_summary()setsskel->bss->enabled= 1. Intrace__run(),trace_start_bpf_summary()was previously invoked afterevlist__start_workload().Because
evlist__start_workload()immediately unblocks the child process by writing to itsgo_pipe, short-lived workloads (such ascat /dev/null) can execute and complete their initial system calls beforetrace_start_bpf_summary()is reached by the parent process. Furthermore, under high system load, the child process may finish before the BPF summary tracking is enabled in the kernel at all, causing syscall summary tests to fail. Additionally, ifinitial_delaywas configured, the workload was started before sleeping.
Quoted 6 lines
Move
trace_start_bpf_summary()to be invoked beforeevlist__start_workload(), matchingevlist__enable(), and ensure it respects target.initial_delay.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6