* [PATCH] perf/core: Protect ctx after dropping its reference
@ 2026-11-16 12:04 Haoli Zhang
2026-11-16 12:11 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Haoli Zhang @ 2026-11-16 12:04 UTC (permalink / raw)
To: peterz, mingo, acme, namhyung, jolsa, irogers
Cc: linux-perf-users, linux-kernel, syzbot+56ad4cdaff8716472033,
Jinning Zhang, Bin Yu, Haoli Zhang
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@syzkaller.appspotmail.com
Closes: https://syzbot.org/bug?extid=56ad4cdaff8716472033
Link: https://lkml.iu.edu/2610.0/09721.html
Co-developed-by: Jinning Zhang <25031212117@stu.xidian.edu.cn>
Signed-off-by: Jinning Zhang <25031212117@stu.xidian.edu.cn>
Co-developed-by: Bin Yu <byu@xidian.edu.cn>
Signed-off-by: Bin Yu <byu@xidian.edu.cn>
Signed-off-by: Haoli Zhang <26031212468@stu.xidian.edu.cn>
---
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();
}
}
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] perf/core: Protect ctx after dropping its reference
2026-11-16 12:04 [PATCH] perf/core: Protect ctx after dropping its reference Haoli Zhang
@ 2026-11-16 12:11 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-16 12:11 UTC (permalink / raw)
To: Haoli Zhang; +Cc: linux-perf-users
> 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@syzkaller.appspotmail.com
> Closes: https://syzbot.org/bug?extid=56ad4cdaff8716472033
> Link: https://lkml.iu.edu/2610.0/09721.html
> Co-developed-by: Jinning Zhang <25031212117@stu.xidian.edu.cn>
> Signed-off-by: Jinning Zhang <25031212117@stu.xidian.edu.cn>
> Co-developed-by: Bin Yu <byu@xidian.edu.cn>
> Signed-off-by: Bin Yu <byu@xidian.edu.cn>
> Signed-off-by: Haoli Zhang <26031212468@stu.xidian.edu.cn>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261116120452.22269-1-26031212468@stu.xidian.edu.cn?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-16 12:11 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16 12:04 [PATCH] perf/core: Protect ctx after dropping its reference Haoli Zhang
2026-11-16 12:11 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox