perf/core: Don't take owner->perf_event_mutex under ctx->mutex on exec
PATCH 1/1 perf/core: Don't take owner->perf_event_mutex under ctx->mutex on exec
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.
--- kernel/events/core.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-)
kernel/events/core.c +9 -13
@@ -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
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 13 lines
perf_event_remove_on_exec()holds the exec'ing task'sctx->mutexand callsperf_remove_from_owner(), which takes the owner'sperf_event_mutex. The order documented aboveperf_event_ctx_lock_nested()is the reverse:
exec_update_lock
task_struct::perf_event_mutex
perf_event_context::mutex
perf_event_task_enable()andperf_event_task_disable()follow it, holdingcurrent->perf_event_mutexwhile 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()
Quoted 8 lines
mutex_lock(perf_event_mutex) mutex_lock(ctx->mutex) perf_event_ctx_lock() perf_remove_from_owner()[ ... ]
Fixes 2e498d0a74e5 ("perf: Add support for event removal on exec")Reported-by Ian Rogers <[email protected]>Closes https://lore.kernel.org/all/CAP-5=fVq=2D6XjVEbAvJz54hG6PcwZZjn-za0hAfK8YV-J+MtQ@mail.gmail.com/ open hereSigned-off-by Daniel Wade <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1