Skip to content

Commit a83fe28

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
perf: Fix put_event() ctx lock
So what I suspect; but I'm in zombie mode today it seems; is that while I initially thought that it was impossible for ctx to change when refcount dropped to 0, I now suspect its possible. Note that until perf_remove_from_context() the event is still active and visible on the lists. So a concurrent sys_perf_event_open() from another task into this task can race. Reported-by: Vince Weaver <vincent.weaver@maine.edu> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Stephane Eranian <eranian@gmail.com> Cc: mark.rutland@arm.com Cc: Jiri Olsa <jolsa@redhat.com> Cc: Arnaldo Carvalho de Melo <acme@kernel.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Link: http://lkml.kernel.org/r/20150129134434.GB26304@twins.programming.kicks-ass.net Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 8f95b43 commit a83fe28

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

kernel/events/core.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,8 @@ static void put_ctx(struct perf_event_context *ctx)
947947
* perf_event::mmap_mutex
948948
* mmap_sem
949949
*/
950-
static struct perf_event_context *perf_event_ctx_lock(struct perf_event *event)
950+
static struct perf_event_context *
951+
perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
951952
{
952953
struct perf_event_context *ctx;
953954

@@ -960,7 +961,7 @@ static struct perf_event_context *perf_event_ctx_lock(struct perf_event *event)
960961
}
961962
rcu_read_unlock();
962963

963-
mutex_lock(&ctx->mutex);
964+
mutex_lock_nested(&ctx->mutex, nesting);
964965
if (event->ctx != ctx) {
965966
mutex_unlock(&ctx->mutex);
966967
put_ctx(ctx);
@@ -970,6 +971,12 @@ static struct perf_event_context *perf_event_ctx_lock(struct perf_event *event)
970971
return ctx;
971972
}
972973

974+
static inline struct perf_event_context *
975+
perf_event_ctx_lock(struct perf_event *event)
976+
{
977+
return perf_event_ctx_lock_nested(event, 0);
978+
}
979+
973980
static void perf_event_ctx_unlock(struct perf_event *event,
974981
struct perf_event_context *ctx)
975982
{
@@ -3572,15 +3579,14 @@ static void perf_remove_from_owner(struct perf_event *event)
35723579
*/
35733580
static void put_event(struct perf_event *event)
35743581
{
3575-
struct perf_event_context *ctx = event->ctx;
3582+
struct perf_event_context *ctx;
35763583

35773584
if (!atomic_long_dec_and_test(&event->refcount))
35783585
return;
35793586

35803587
if (!is_kernel_event(event))
35813588
perf_remove_from_owner(event);
35823589

3583-
WARN_ON_ONCE(ctx->parent_ctx);
35843590
/*
35853591
* There are two ways this annotation is useful:
35863592
*
@@ -3593,7 +3599,8 @@ static void put_event(struct perf_event *event)
35933599
* the last filedesc died, so there is no possibility
35943600
* to trigger the AB-BA case.
35953601
*/
3596-
mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
3602+
ctx = perf_event_ctx_lock_nested(event, SINGLE_DEPTH_NESTING);
3603+
WARN_ON_ONCE(ctx->parent_ctx);
35973604
perf_remove_from_context(event, true);
35983605
mutex_unlock(&ctx->mutex);
35993606

0 commit comments

Comments
 (0)