perf/core: Detach event groups during remove_on_exec
PATCH 1/1 perf/core: Detach event groups during remove_on_exec
[ Upstream commit 037a3c43edfb597665dd34457cd22b14692f2ba3 ]
perf_event_remove_on_exec() removes events by calling perf_event_exit_event(). For top-level events, this removes the event from the context with DETACH_EXIT only.
This can leave inconsistent group state when a removed event is a group leader and the group contains siblings without remove_on_exec. If the group was active, the surviving siblings can remain active and attached to the removed leader's sibling list, but are no longer represented by a valid group leader on the PMU context active lists.
A later close of the removed leader uses DETACH_GROUP and can promote the still-active siblings from this stale group state. The next schedule-in can then add an already-linked active_list entry again, corrupting the PMU context active list.
With DEBUG_LIST enabled, this is caught as a list_add double-add in merge_sched_in().
Fix this by detaching group relationships when remove_on_exec removes an event. This preserves the existing task-exit and revoke behavior, while ensuring surviving siblings are ungrouped before the removed event leaves the context.
--- kernel/events/core.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
kernel/events/core.c +8 -6
@@ -4537,7 +4537,8 @@ static void perf_event_enable_on_exec(struct perf_event_context *ctx)static void perf_remove_from_owner (struct perf_event *event);static void perf_event_exit_event (struct perf_event *event, -struct perf_event_context *ctx); +struct perf_event_context *ctx, +unsigned long detach_flags);/* * Removes all events from the current task that have been marked
@@ -4564,7 +4565,7 @@ static void perf_event_remove_on_exec(struct perf_event_context *ctx) modified = true; -perf_event_exit_event (event, ctx); +perf_event_exit_event (event, ctx,DETACH_GROUP ); }raw_spin_lock_irqsave (&ctx->lock, flags);
@@ -13478,10 +13479,11 @@ static void sync_child_event(struct perf_event *child_event) }static void -perf_event_exit_event (struct perf_event *event,struct perf_event_context *ctx) +perf_event_exit_event (struct perf_event *event, +struct perf_event_context *ctx, +unsigned long detach_flags) {struct perf_event *parent_event = event->parent; -unsigned long detach_flags =0 ;if (parent_event) {/*
@@ -13496,7 +13498,7 @@ perf_event_exit_event(struct perf_event *event, struct perf_event_context *ctx)* Do destroy all inherited groups, we don't care about those * and being thorough is better. */ - detach_flags =DETACH_GROUP |DETACH_CHILD ; + detach_flags |=DETACH_GROUP |DETACH_CHILD ;mutex_lock (&parent_event->child_mutex); }
@@ -13575,7 +13577,7 @@ static void perf_event_exit_task_context(struct task_struct *child)perf_event_task (child, child_ctx,0 );list_for_each_entry_safe (child_event, next, &child_ctx->event_list, event_entry) -perf_event_exit_event (child_event, child_ctx); +perf_event_exit_event (child_event, child_ctx,0 );mutex_unlock (&child_ctx->mutex);
Signature · 1 line
2.50.1 (Apple Git-155)