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

From: James Clark

Date: Fri May 15 2026 - 09:03:25 EST




On 04/03/2026 3:23 pm, Peter Zijlstra wrote:
On Wed, Mar 04, 2026 at 11:26:10AM +0000, James Clark wrote:

@@ -14564,11 +14556,21 @@ static void perf_event_exit_task_context(struct task_struct *task, bool exit)
* won't get any samples after PERF_RECORD_EXIT. We can however still
* get a few PERF_RECORD_READ events.
*/
- if (exit)
+ if (exit) {
perf_event_task(task, ctx, 0);
- list_for_each_entry_safe(child_event, next, &ctx->event_list, event_entry)
- perf_event_exit_event(child_event, ctx, exit ? task : NULL, false);
+ guard(raw_spinlock_irq)(&ctx->lock);
+ list_for_each_entry(event, &ctx->event_list, event_entry) {
+ if (event->attr.inherit_stat) {
+ if (task && task != TASK_TOMBSTONE &&
+ event_filter_match(event))
+ perf_event_read_event(event, task);

Doesn't this only work if you happened to have an event opened on every CPU?
Otherwise you could get rescheduled onto a core without an event and the
event_filter_match() will fail and you'd drop the sample read for that exit.

Yes, absolutely. But that is more or less what you get.

So perf has per-task buffers and per-cpu buffers. For a simple per-task
profile, the per-task buffer works fine. However, if you want inherited
tasks, they can't all emit to the same buffer -- this would be a
scalability nightmake. So in that scenario you have to have
per-task-per-cpu events and make sure everybody emits to the local
buffer.

If you 'forget' to create an event/buffer for a particular CPU the
task(s) can run on, you loose events the moment they end up there.

There just isn't much you can do about that.

Hi Peter,

Both of the diffs you posted above work for me. Do we need to do anything for revoke? It seems like a bit of an edge case if it's only for removing the PMU so could probably be left out. Or do we make a similar change to __pmu_detach_event()?

For remove_on_exec, I put the same loop in perf_event_remove_on_exec() and I think that covers it:

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 87712491f4c7..d152abcafe5a 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -4683,6 +4683,16 @@ static void perf_event_remove_on_exec(struct perf_event_context *ctx)
if (WARN_ON_ONCE(ctx->task != current))
goto unlock;

+ raw_spin_lock_irqsave(&ctx->lock, flags);
+ list_for_each_entry(event, &ctx->event_list, event_entry) {
+ if (event->attr.inherit_stat) {
+ if (ctx->task != TASK_TOMBSTONE &&
+ event_filter_match(event))
+ perf_event_read_event(event, ctx->task);
+ }
+ }
+ raw_spin_unlock_irqrestore(&ctx->lock, flags);
+
list_for_each_entry_safe(event, next, &ctx->event_list, event_entry) {
if (!event->attr.remove_on_exec)
continue;


Do you want me to send your two diffs as patches with you as an author? Or will you take it?

Thanks
James