Re: [PATCH bpf-next 1/4] bpf: Add log support for perf link_create

From: bot+bpf-ci

Date: Wed Dec 02 2026 - 11:26:40 EST


> bpf: Add log support for perf link_create

TL;DR: bpf_log() under bpf_event_mutex in perf_event_attach_bpf_prog() may
fault on the user log buffer, possibly causing an ABBA deadlock with mmap_lock
or stalling all perf BPF attaches; log after unlocking.

> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c

[ ... ]

> mutex_lock(&bpf_event_mutex);
>
> - if (event->prog)
> + if (event->prog) {
> + bpf_log(log, "Target perf event already has a program attached.\n");
> goto unlock;
> + }
>
> old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
> if (old_array &&
> bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
> + bpf_log(log, "Too many programs (limit %d).\n", BPF_TRACE_MAX_PROGS);
> ret = -E2BIG;
> goto unlock;
> }
>
> ret = bpf_prog_array_copy(old_array, NULL, prog, bpf_cookie, &new_array);
> + if (ret == -EEXIST)
> + bpf_log(log, "Program already attached to this trace event.\n");
> if (ret < 0)
> goto unlock;

Can these bpf_log() calls run safely under bpf_event_mutex?

On the BPF_LINK_CREATE path the log is a user log, so bpf_log() ends up in
bpf_verifier_vlog(), which calls copy_to_user() on log->ubuf. That can
fault, and might_fault() tells lockdep about current->mm->mmap_lock. This
records a bpf_event_mutex -> mmap_lock dependency.

The opposite order already exists:

perf_mmap_close() [mmap_lock held via remove_vma()]
put_event() [last reference]
_free_event()
perf_event_free_bpf_prog()
perf_event_detach_bpf_prog()
mutex_lock(&bpf_event_mutex);

Can this produce an ABBA deadlock between mmap_lock and bpf_event_mutex
when the log page has to be faulted in? It looks like the same inversion
that commit 3a38bb98d9ab ("bpf/tracing: fix a deadlock in
perf_event_detach_bpf_prog") fixed by moving copy_to_user() out of
bpf_event_mutex in perf_event_query_prog_array(), which still copies to
user only after mutex_unlock(&bpf_event_mutex).

The failing checks are easy to reach from userspace. Calling
BPF_LINK_CREATE twice on the same tracepoint, kprobe or uprobe perf fd hits
the event->prog branch, and attaching the same program through a second
perf event on the same trace event returns -EEXIST from
bpf_prog_array_copy().

Also, since bpf_event_mutex is global, could a slow or never-completing
fault on the log buffer (for example a FUSE-backed mapping) block every
perf-based BPF attach, detach and PERF_EVENT_IOC_QUERY_BPF on the system?

The bpf_log() calls made under ctx->mutex in __perf_event_set_bpf_prog()
and perf_event_set_bpf_handler(), and the kprobe_override message before
mutex_lock(), look fine.

Would it work to record the failure reason inside the critical section (for
example in a const char *msg) and call bpf_log() after
mutex_unlock(&bpf_event_mutex)? No later commit in the series changes
perf_event_attach_bpf_prog().


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/46921726766