Skip to content

Commit 82d9485

Browse files
Peter ZijlstraIngo Molnar
Peter Zijlstra
authored and
Ingo Molnar
committed
perf/core: Fix lock inversion between perf,trace,cpuhp
Lockdep gifted us with noticing the following 4-way lockup scenario: perf_trace_init() #0 mutex_lock(&event_mutex) perf_trace_event_init() perf_trace_event_reg() tp_event->class->reg() := tracepoint_probe_register #1 mutex_lock(&tracepoints_mutex) trace_point_add_func() #2 static_key_enable() #2 do_cpu_up() perf_event_init_cpu() #3 mutex_lock(&pmus_lock) #4 mutex_lock(&ctx->mutex) perf_event_task_disable() mutex_lock(&current->perf_event_mutex) #4 ctx = perf_event_ctx_lock() #5 perf_event_for_each_child() do_exit() task_work_run() __fput() perf_release() perf_event_release_kernel() #4 mutex_lock(&ctx->mutex) #5 mutex_lock(&event->child_mutex) free_event() _free_event() event->destroy() := perf_trace_destroy #0 mutex_lock(&event_mutex); Fix that by moving the free_event() out from under the locks. Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Arnaldo Carvalho de Melo <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Stephane Eranian <[email protected]> Cc: Steven Rostedt (VMware) <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Vince Weaver <[email protected]> Cc: [email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent 1f07476 commit 82d9485

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

kernel/events/core.c

+11-2
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,10 @@ static void put_ctx(struct perf_event_context *ctx)
12311231
* perf_event_context::lock
12321232
* perf_event::mmap_mutex
12331233
* mmap_sem
1234+
*
1235+
* cpu_hotplug_lock
1236+
* pmus_lock
1237+
* cpuctx->mutex / perf_event_context::mutex
12341238
*/
12351239
static struct perf_event_context *
12361240
perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
@@ -4196,6 +4200,7 @@ int perf_event_release_kernel(struct perf_event *event)
41964200
{
41974201
struct perf_event_context *ctx = event->ctx;
41984202
struct perf_event *child, *tmp;
4203+
LIST_HEAD(free_list);
41994204

42004205
/*
42014206
* If we got here through err_file: fput(event_file); we will not have
@@ -4268,8 +4273,7 @@ int perf_event_release_kernel(struct perf_event *event)
42684273
struct perf_event, child_list);
42694274
if (tmp == child) {
42704275
perf_remove_from_context(child, DETACH_GROUP);
4271-
list_del(&child->child_list);
4272-
free_event(child);
4276+
list_move(&child->child_list, &free_list);
42734277
/*
42744278
* This matches the refcount bump in inherit_event();
42754279
* this can't be the last reference.
@@ -4284,6 +4288,11 @@ int perf_event_release_kernel(struct perf_event *event)
42844288
}
42854289
mutex_unlock(&event->child_mutex);
42864290

4291+
list_for_each_entry_safe(child, tmp, &free_list, child_list) {
4292+
list_del(&child->child_list);
4293+
free_event(child);
4294+
}
4295+
42874296
no_ctx:
42884297
put_event(event); /* Must be the 'last' reference */
42894298
return 0;

0 commit comments

Comments
 (0)