While hunting for clues to a use-after-free, Oleg spotted that perf_event_init_context() can loose an error value with the result that fork() can succeed even though we did not fully inherit the perf event context. Cc: Arnaldo Carvalho de Melo Cc: Mathieu Desnoyers Cc: Ingo Molnar Cc: Alexander Shishkin Cc: stable@vger.kernel.org Cc: Dmitry Vyukov Cc: Frederic Weisbecker Spotted-by: Oleg Nesterov Fixes: 889ff0150661 ("perf/core: Split context's event group list into pinned and non-pinned lists") Signed-off-by: Peter Zijlstra (Intel) --- kernel/events/core.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -10820,7 +10819,7 @@ static int perf_event_init_context(struc ret = inherit_task_group(event, parent, parent_ctx, child, ctxn, &inherited_all); if (ret) - break; + goto out_unlock; } /* @@ -10836,7 +10835,7 @@ static int perf_event_init_context(struc ret = inherit_task_group(event, parent, parent_ctx, child, ctxn, &inherited_all); if (ret) - break; + goto out_unlock; } raw_spin_lock_irqsave(&parent_ctx->lock, flags); @@ -10864,6 +10863,7 @@ static int perf_event_init_context(struc } raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); +out_unlock: mutex_unlock(&parent_ctx->mutex); perf_unpin_context(parent_ctx);