Skip to content

Commit

Permalink
perf/core: Fix another perf,trace,cpuhp lock inversion
Browse files Browse the repository at this point in the history
Lockdep noticed the following 3-way lockup race:

        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_ioctl()
 #4	  ctx = perf_event_ctx_lock()
	  _perf_iotcl()
	    ftrace_profile_set_filter()
 #0	      mutex_lock(&event_mutex)

Fudge it for now by noting that the tracepoint state does not depend
on the event <-> context relation. Ugly though :/

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
  • Loading branch information
Peter Zijlstra authored and Ingo Molnar committed Jan 25, 2018
1 parent 82d9485 commit 43fa87f
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions kernel/events/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -8525,6 +8525,29 @@ perf_event_set_addr_filter(struct perf_event *event, char *filter_str)
return ret;
}

static int
perf_tracepoint_set_filter(struct perf_event *event, char *filter_str)
{
struct perf_event_context *ctx = event->ctx;
int ret;

/*
* Beware, here be dragons!!
*
* the tracepoint muck will deadlock against ctx->mutex, but the tracepoint
* stuff does not actually need it. So temporarily drop ctx->mutex. As per
* perf_event_ctx_lock() we already have a reference on ctx.
*
* This can result in event getting moved to a different ctx, but that
* does not affect the tracepoint state.
*/
mutex_unlock(&ctx->mutex);
ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
mutex_lock(&ctx->mutex);

return ret;
}

static int perf_event_set_filter(struct perf_event *event, void __user *arg)
{
char *filter_str;
Expand All @@ -8541,8 +8564,7 @@ static int perf_event_set_filter(struct perf_event *event, void __user *arg)

if (IS_ENABLED(CONFIG_EVENT_TRACING) &&
event->attr.type == PERF_TYPE_TRACEPOINT)
ret = ftrace_profile_set_filter(event, event->attr.config,
filter_str);
ret = perf_tracepoint_set_filter(event, filter_str);
else if (has_addr_filter(event))
ret = perf_event_set_addr_filter(event, filter_str);

Expand Down

0 comments on commit 43fa87f

Please sign in to comment.