From: bot+bpf-ci@kernel.org
To: leon.hwang@linux.dev,bpf@vger.kernel.org
Cc: ast@kernel.org,daniel@iogearbox.net,andrii@kernel.org,eddyz87@gmail.com,memxor@gmail.com,martin.lau@linux.dev,song@kernel.org,yonghong.song@linux.dev,jolsa@kernel.org,emil@etsalapatis.com,ihor.solodrai@linux.dev,john.fastabend@gmail.com,rostedt@goodmis.org,mhiramat@kernel.org,mathieu.desnoyers@efficios.com,peterz@infradead.org,mingo@redhat.com,acme@kernel.org,namhyung@kernel.org,mark.rutland@arm.com,alexander.shishkin@linux.intel.com,irogers@google.com,adrian.hunter@intel.com,james.clark@linaro.org,kpsingh@kernel.org,matt@bobrowski.net,shuah@kernel.org,leon.hwang@linux.dev,linux-kernel@vger.kernel.org,linux-trace-kernel@vger.kernel.org,linux-perf-users@vger.kernel.org,linux-kselftest@vger.kernel.org,ast@kernel.org,andrii@kernel.org,daniel@iogearbox.net,martin.lau@kernel.org,eddyz87@gmail.com,yonghong.song@linux.dev,mason@kernel.org,ihor.solodrai@linux.dev
Subject: Re: [PATCH bpf-next 1/4] bpf: Add log support for perf link_create
Date: Wed, 2 Dec 2026 16:26:28 +0000 (UTC) [thread overview]
Message-ID: <c337d3ac5126d187486c19146c935d46cd96770a78377adee9bbcc82e5b4816e@mail.kernel.org> (raw)
In-Reply-To: <20261202153739.57959-2-leon.hwang@linux.dev>
[-- Attachment #1: Type: text/plain, Size: 3248 bytes --]
> 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
next prev parent reply other threads:[~2026-12-02 16:26 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 15:37 [PATCH bpf-next 0/4] " Leon Hwang
2026-12-02 15:37 ` [PATCH bpf-next 1/4] " Leon Hwang
2026-12-02 16:26 ` bot+bpf-ci [this message]
2026-12-02 15:37 ` [PATCH bpf-next 2/4] bpf: Add log support for kprobe_multi link_create Leon Hwang
2026-12-02 15:37 ` [PATCH bpf-next 3/4] bpf: Add log support for uprobe_multi link_create Leon Hwang
2026-12-02 15:37 ` [PATCH bpf-next 4/4] selftests/bpf: Test perf event and multi-probe link creation logs Leon Hwang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=c337d3ac5126d187486c19146c935d46cd96770a78377adee9bbcc82e5b4816e@mail.kernel.org \
--to=bot+bpf-ci@kernel.org \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=ihor.solodrai@linux.dev \
--cc=irogers@google.com \
--cc=james.clark@linaro.org \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=leon.hwang@linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=martin.lau@kernel.org \
--cc=martin.lau@linux.dev \
--cc=mason@kernel.org \
--cc=mathieu.desnoyers@efficios.com \
--cc=matt@bobrowski.net \
--cc=memxor@gmail.com \
--cc=mhiramat@kernel.org \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®