• Peter Zijlstra's avatar
    perf: Fix race in perf_event_exit_task_context() · 6a3351b6
    Peter Zijlstra authored
    There is a race between perf_event_exit_task_context() and
    orphans_remove_work() which results in a use-after-free.
    
    We mark ctx->task with TASK_TOMBSTONE to indicate a context is
    'dead', under ctx->lock. After which point event_function_call()
    on any event of that context will NOP
    
    A concurrent orphans_remove_work() will only hold ctx->mutex for
    the list iteration and not serialize against this. Therefore its
    possible that orphans_remove_work()'s perf_remove_from_context()
    call will fail, but we'll continue to free the event, with the
    result of free'd memory still being on lists and everything.
    
    Once perf_event_exit_task_context() gets around to acquiring
    ctx->mutex it too will iterate the event list, encounter the
    already free'd event and proceed to free it _again_. This fails
    with the WARN in free_event().
    
    Plug the race by having perf_event_exit_task_context() hold
    ctx::mutex over the whole tear-down, thereby 'naturally'
    serializing against all other sites, including the orphan work.
    Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
    Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
    Cc: Jiri Olsa <jolsa@redhat.com>
    Cc: Linus Torvalds <torvalds@linux-foundation.org>
    Cc: Peter Zijlstra <peterz@infradead.org>
    Cc: Stephane Eranian <eranian@google.com>
    Cc: Thomas Gleixner <tglx@linutronix.de>
    Cc: Vince Weaver <vincent.weaver@maine.edu>
    Cc: alexander.shishkin@linux.intel.com
    Cc: dsahern@gmail.com
    Cc: namhyung@kernel.org
    Link: http://lkml.kernel.org/r/20160125130954.GY6357@twins.programming.kicks-ass.netSigned-off-by: default avatarIngo Molnar <mingo@kernel.org>
    6a3351b6
core.c 221 KB