[PATCH] perf/core: Protect ctx after dropping its reference
From: Haoli Zhang
Date: Mon Nov 16 2026 - 07:05:42 EST
Take an RCU read lock before decrementing the reference and retain it
through the non-final branch's tombstone check and wakeup. This keeps ctx
alive until that branch has finished. When the decrement is final,
release the read lock immediately; that caller continues the final
cleanup and queues the RCU callback.
Read ctx->task with READ_ONCE(), matching the WRITE_ONCE() used when the
task context is marked as a tombstone. Keep smp_mb__after_atomic(): it
preserves the existing wait_var_event() ordering, while RCU protects the
context's lifetime.
A similar fix was posted by Weiming Shi in October:
https://lkml.iu.edu/2610.0/09721.html. This version releases the RCU
read lock immediately on the final-put path and uses READ_ONCE() for the
lockless ctx->task check.
The report shows the access in put_ctx() reached through event release,
and the free callback was queued from task-exit context cleanup. These
stacks are consistent with the post-decrement lifetime race described
above.
Fixes: 59f3aa4a3ee2 ("perf: Simplify perf_event_free_task() wait")
Reported-by: syzbot+56ad4cdaff8716472033@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://syzbot.org/bug?extid=56ad4cdaff8716472033
Link: https://lkml.iu.edu/2610.0/09721.html
Co-developed-by: Jinning Zhang <25031212117@xxxxxxxxxxxxxxxxx>
Signed-off-by: Jinning Zhang <25031212117@xxxxxxxxxxxxxxxxx>
Co-developed-by: Bin Yu <byu@xxxxxxxxxxxxx>
Signed-off-by: Bin Yu <byu@xxxxxxxxxxxxx>
Signed-off-by: Haoli Zhang <26031212468@xxxxxxxxxxxxxxxxx>
---
kernel/events/core.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/kernel/events/core.c b/kernel/events/core.c
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -1385,13 +1385,17 @@ static void put_ctx(struct perf_event_context *ctx)
{
+ /* The non-final path accesses ctx after dropping its reference. */
+ rcu_read_lock();
if (refcount_dec_and_test(&ctx->refcount)) {
+ rcu_read_unlock();
if (ctx->parent_ctx)
put_ctx(ctx->parent_ctx);
if (ctx->task && ctx->task != TASK_TOMBSTONE)
put_task_struct(ctx->task);
call_rcu(&ctx->rcu_head, free_ctx);
} else {
smp_mb__after_atomic(); /* pairs with wait_var_event() */
- if (ctx->task == TASK_TOMBSTONE)
+ if (READ_ONCE(ctx->task) == TASK_TOMBSTONE)
wake_up_var(&ctx->refcount);
+ rcu_read_unlock();
}
}