[PATCH 1/2] perf trace: Only call bpf_get_current_pid_tgid() when filtering tasks

From: Ian Rogers

Date: Fri Oct 02 2026 - 02:56:07 EST


In task_traced(), bpf_get_current_pid_tgid() was called before checking
has_pids_to_trace. Because BPF helper calls are not marked pure, the
compiler cannot sink the call past the has_pids_to_trace check, so the
helper was called on every syscall enter and exit even when tracing
system-wide.

Check has_pids_to_trace before calling bpf_get_current_pid_tgid().

Suggested-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
Assisted-by: Antigravity:gemini-3.1-pro
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c b/tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c
index d16e55706335..0b2ef9541f84 100644
--- a/tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c
+++ b/tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c
@@ -494,13 +494,15 @@ static bool pid_filter__has(struct pids_filtered *pids, pid_t pid)

static bool task_traced(void)
{
- u64 pid_tgid = bpf_get_current_pid_tgid();
- pid_t pid = uses_tgid ? pid_tgid >> 32 : (pid_t)pid_tgid;
+ u64 pid_tgid;
+ pid_t pid;
bool *traced;

if (!has_pids_to_trace)
return true;

+ pid_tgid = bpf_get_current_pid_tgid();
+ pid = uses_tgid ? pid_tgid >> 32 : (pid_t)pid_tgid;
traced = bpf_map_lookup_elem(&pids_to_trace, &pid);
return traced && *traced;
}

base-commit: 148cd3adf2df9e53fd9b3070ae1178d4876f8c67
--
2.56.0.rc1.315.gc6ed9934b7-goog