From: Daniel Wade <danjwade95@gmail.com>
To: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
James Clark <james.clark@linaro.org>,
Marco Elver <elver@google.com>, Taeyang Lee <0wn@theori.io>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] perf/core: Don't take owner->perf_event_mutex under ctx->mutex on exec
Date: Mon, 16 Nov 2026 10:09:49 +1030 [thread overview]
Message-ID: <20261115233949.13685-1-danjwade95@gmail.com> (raw)
perf_event_remove_on_exec() holds the exec'ing task's ctx->mutex and calls
perf_remove_from_owner(), which takes the owner's perf_event_mutex. The
order documented above perf_event_ctx_lock_nested() is the reverse:
exec_update_lock
task_struct::perf_event_mutex
perf_event_context::mutex
perf_event_task_enable() and perf_event_task_disable() follow it, holding
current->perf_event_mutex while they lock the context of each event the
task owns. The two paths deadlock against each other:
perf_event_task_disable() perf_event_remove_on_exec()
mutex_lock(perf_event_mutex) mutex_lock(ctx->mutex)
perf_event_ctx_lock() perf_remove_from_owner()
mutex_lock(ctx->mutex) mutex_lock(perf_event_mutex)
Neither sleep is interruptible, so neither task can be killed, and the
target is stuck in execve() holding exec_update_lock for writing and
cred_guard_mutex. Reaching it takes no privilege beyond opening a
remove_on_exec event on the target: open one on a child and call
prctl(PR_TASK_PERF_EVENTS_DISABLE) while the child execs.
Lockdep does not report the inversion because perf_remove_from_owner()
takes the mutex with SINGLE_DEPTH_NESTING, annotated there for a caller
arriving through perf_event_exit_task() with a child's ctx->mutex held.
No such caller exists; the only other caller, perf_event_release_kernel(),
takes ctx->mutex afterwards.
Do not unlink the event from its owner on exec. It stays on the owner's
perf_event_list until perf_event_release_kernel() or the owner's
perf_event_exit_task() removes it, as an event of an exited task does.
perf_event_task_enable() and perf_event_task_disable() still visit it but
leave it alone, as _perf_event_enable() and _perf_event_disable() return
early for PERF_EVENT_STATE_EXIT. They now also reach its child events,
which PERF_EVENT_IOC_ENABLE and PERF_EVENT_IOC_DISABLE already do.
perf_remove_from_owner() is then never called under a ctx->mutex, so take
the mutex without the nesting annotation and let lockdep check it.
Fixes: 2e498d0a74e5 ("perf: Add support for event removal on exec")
Reported-by: Ian Rogers <irogers@google.com>
Closes: https://lore.kernel.org/all/CAP-5=fVq=2D6XjVEbAvJz54hG6PcwZZjn-za0hAfK8YV-J+MtQ@mail.gmail.com/
Signed-off-by: Daniel Wade <danjwade95@gmail.com>
Cc: stable@vger.kernel.org
---
kernel/events/core.c | 22 +++++++++-------------
1 file changed, 9 insertions(+), 13 deletions(-)
diff --git a/kernel/events/core.c b/kernel/events/core.c
index f5f2261..ec51e6b 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -4747,7 +4747,6 @@ out:
put_ctx(clone_ctx);
}
-static void perf_remove_from_owner(struct perf_event *event);
static void perf_event_exit_event(struct perf_event *event,
struct perf_event_context *ctx,
struct task_struct *task,
@@ -4756,6 +4755,11 @@ static void perf_event_exit_event(struct perf_event *event,
/*
* Removes all events from the current task that have been marked
* remove-on-exec, and feeds their values back to parent events.
+ *
+ * Top-level events stay on their owner's perf_event_list, like the events of
+ * an exited task, until perf_event_release_kernel() or the owner's
+ * perf_event_exit_task() takes them off. They cannot be unlinked here:
+ * owner->perf_event_mutex nests outside of ctx->mutex.
*/
static void perf_event_remove_on_exec(struct perf_event_context *ctx)
{
@@ -4773,9 +4777,6 @@ static void perf_event_remove_on_exec(struct perf_event_context *ctx)
if (!event->attr.remove_on_exec)
continue;
- if (!is_kernel_event(event))
- perf_remove_from_owner(event);
-
modified = true;
perf_event_exit_event(event, ctx, ctx->task, DETACH_GROUP);
@@ -5876,6 +5877,9 @@ static void free_event(struct perf_event *event)
/*
* Remove user event from the owner task.
+ *
+ * Must not be called with a ctx->mutex held: owner->perf_event_mutex nests
+ * outside of it, see perf_event_task_enable().
*/
static void perf_remove_from_owner(struct perf_event *event)
{
@@ -5900,15 +5904,7 @@ static void perf_remove_from_owner(struct perf_event *event)
rcu_read_unlock();
if (owner) {
- /*
- * If we're here through perf_event_exit_task() we're already
- * holding ctx->mutex which would be an inversion wrt. the
- * normal lock order.
- *
- * However we can safely take this lock because its the child
- * ctx->mutex.
- */
- mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
+ mutex_lock(&owner->perf_event_mutex);
/*
* We have to re-check the event->owner field, if it is cleared
next reply other threads:[~2026-11-15 23:40 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-15 23:39 Daniel Wade [this message]
2026-11-15 23:55 ` [PATCH] perf/core: Don't take owner->perf_event_mutex under ctx->mutex on exec sashiko-bot
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=20261115233949.13685-1-danjwade95@gmail.com \
--to=danjwade95@gmail.com \
--cc=0wn@theori.io \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=elver@google.com \
--cc=irogers@google.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
/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