* [PATCH] perf/core: Don't take owner->perf_event_mutex under ctx->mutex on exec
@ 2026-11-15 23:39 Daniel Wade
0 siblings, 0 replies; only message in thread
From: Daniel Wade @ 2026-11-15 23:39 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim
Cc: Mark Rutland, Alexander Shishkin, Jiri Olsa, Ian Rogers,
Adrian Hunter, James Clark, Marco Elver, Taeyang Lee,
linux-perf-users, linux-kernel
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
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-15 23:40 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-15 23:39 [PATCH] perf/core: Don't take owner->perf_event_mutex under ctx->mutex on exec Daniel Wade
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®