[PATCH v1 0/2] perf/bpf: Fix BPF program inheritance and overflow races
From: Ian Rogers
Date: Tue Dec 01 2026 - 02:17:17 EST
This two-patch series fixes pre-existing races, cookie handling, and a
reference leak in how perf_event handles BPF programs attached to
non-tracing and tracing events:
1. perf_event_alloc() / perf_event_set_bpf_handler() /
perf_event_free_bpf_handler():
- Skip inheriting parent_event->prog on tracing events (tracepoints,
kprobes, uprobes), where copying event->prog without attaching to
tp_event->prog_array caused child teardown to detach the parent's
program.
- Protect lockless inheritance of parent_event->prog with
rcu_tasks_trace + rcu read locks, smp_load_acquire()/
smp_store_release(), xchg(), synchronize_rcu() for sleepable
programs, and bpf_prog_inc_not_zero().
- Preserve parent_event->bpf_cookie across event inheritance.
- Move perf_event_free_bpf_prog() into __free_event() so
perf_event_alloc() error paths cannot leak the inherited bpf_prog
reference.
2. __perf_event_overflow() / bpf_overflow_handler():
- Avoid dereferencing event->prog->type outside an RCU read-side
critical section in __perf_event_overflow().
- Check prog->type == BPF_PROG_TYPE_PERF_EVENT under rcu_read_lock()
using smp_load_acquire(), returning 1 if event->prog was
concurrently cleared so samples are not dropped.
These issues were identified via code audit and Sashiko automated
review while developing perf/BPF changes; the fixes, regression tests,
and changelogs were drafted with an AI coding assistant.
Testing:
- Build-tested kernel/events/core.o across x86_64, arm64, riscv,
loongarch, CONFIG_BPF_SYSCALL=n (W=1 clean), and
CONFIG_BPF_SYSCALL=y + CONFIG_EVENT_TRACING=y + CONFIG_BPF_EVENTS=n.
- Runtime-tested in x86_64 QEMU/KVM with CONFIG_PROVE_LOCKING=y and
CONFIG_PROVE_RCU=y using targeted C regression tests covering:
(1) inherited tracepoint BPF program continuing to fire after an
inherited child exits,
(2) bpf_get_attach_cookie_pe() returning the parent's cookie on an
inherited child event, and
(3) concurrent sampling overflow and bpf_perf_link_release() detach
stress loops (40 iterations, zero lockdep/RCU splats).
Both patches are based on torvalds/master (and apply cleanly to
tip/perf/urgent).
Ian Rogers (2):
perf/bpf: Fix BPF program inheritance races, cookie, and leak in
perf_event_alloc()
perf/bpf: Fix racy event->prog dereference in __perf_event_overflow()
kernel/events/core.c | 84 ++++++++++++++++++++++++++++++++++----------
1 file changed, 65 insertions(+), 19 deletions(-)
base-commit: 19cc08c1a6a650f8e54c0461ff198be6448f425d
--
2.56.1.839.g22a8290db9-goog