Re: [PATCH v2 RESEND] perf/core: Fix missing read event generation on task exit

From: Peter Zijlstra

Date: Mon Oct 13 2025 - 10:08:17 EST


On Thu, Oct 09, 2025 at 12:35:30AM +0800, Thaumy Cheng wrote:

> @@ -13995,13 +13997,16 @@ 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, bool revoke)
> +perf_event_detach_event(struct perf_event *event,
> + struct perf_event_context *ctx, bool revoke, bool exit)
> {
> struct perf_event *parent_event = event->parent;
> - unsigned long detach_flags = DETACH_EXIT;
> + unsigned long detach_flags = 0;
> unsigned int attach_state;
>
> + if (exit)
> + detach_flags |= DETACH_EXIT;
> +

Urgh, at that point just pass in the right DETACH_flag directly,
something like:

perf_event_detach_event(struct perf_event *event,
struct perf_event_context *ctx,
unsigned long detach_flags);

perf_event_detach_event(event, ctx, DETACH_EXIT);

perf_event_detach_event(event, ctx, DETACH_REVOKE);



> if (parent_event) {
> /*
> * Do not destroy the 'original' grouping; because of the
> @@ -14077,6 +14082,17 @@ static void perf_event_exit_task_context(struct task_struct *task, bool exit)
> */
> mutex_lock(&ctx->mutex);
>
> + /*
> + * Report the task dead after unscheduling the events so that we
> + * won't get any samples after PERF_RECORD_EXIT. We can however still
> + * get a few PERF_RECORD_READ events.
> + */
> + if (exit)
> + perf_event_task(task, ctx, 0);
> +
> + list_for_each_entry_safe(child_event, next, &ctx->event_list, event_entry)
> + perf_event_detach_event(child_event, ctx, false, exit);
> +
> /*
> * In a single ctx::lock section, de-schedule the events and detach the
> * context from the task such that we cannot ever get it scheduled back

This can't be right; read the comment you moved and then look where we
unschedule the event.