perf/core: Fix race between perf_event_exit_task and perf_pending_task
PATCH 1/1 perf/core: Fix race between perf_event_exit_task and perf_pending_task
A race condition exists between perf_event_exit_task() and perf_pending_task().
During begin_new_exec(), perf_event_exit_task() may be called, and the PF_EXITING flag is not set on task. so perf_sigtrap() continues to execute and triggers WARN_ON_ONCE(event->ctx->task != current).
To fix this problem, also check if the event->ctx->task is TASK_TOMBSTONE.
--- kernel/events/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/events/core.c +1 -1
@@ -7543,7 +7543,7 @@ static void perf_sigtrap(struct perf_event *event)* Both perf_pending_task() and perf_pending_irq() can race with the * task exiting. */ -if (current->flags &PF_EXITING ) +if (current->flags &PF_EXITING || event->ctx->task ==TASK_TOMBSTONE )return ;/*
Signature · 1 line
2.34.1
Ian Rogers
reviewerQuoted 11 lines · Luo Gengkun
A race condition exists between
perf_event_exit_task()andperf_pending_task().During
begin_new_exec(),perf_event_exit_task()may be called, and thePF_EXITINGflag is not set on task. soperf_sigtrap()continues to execute and triggersWARN_ON_ONCE(event->ctx->task != current).To fix this problem, also check if the
event->ctx->taskisTASK_TOMBSTONE.Fixes 97ba62b27867 ("perf: Add support for SIGTRAP on perf events")Signed-off-by Luo Gengkun <[email protected]>
Dmitry, the description and patch look sensible. Could you take a look?
Thanks,
Ian
Quoted 20 lines
--- kernel/events/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/events/core.c +1
-2@@ -7543,7 +7543,7 @@ static void perf_sigtrap(struct perf_event *event)* Both perf_pending_task() and perf_pending_irq() can race with the * task exiting. */ -if (current->flags &PF_EXITING ) +if (current->flags &PF_EXITING || event->ctx->task ==TASK_TOMBSTONE )return ;/* -- 2.34.1
Quoted 9 lines · Ian Rogers
Quoted 7 lines · Luo Gengkun
A race condition exists between
perf_event_exit_task()andperf_pending_task().During
begin_new_exec(),perf_event_exit_task()may be called, and thePF_EXITINGflag is not set on task. soperf_sigtrap()continues to execute and triggersWARN_ON_ONCE(event->ctx->task != current).
Quoted 6 lines
Quoted 4 lines
To fix this problem, also check if the
event->ctx->taskisTASK_TOMBSTONE.Fixes 97ba62b27867 ("perf: Add support for SIGTRAP on perf events")Signed-off-by Luo Gengkun <[email protected]>Dmitry, the description and patch look sensible. Could you take a look?
Marco, please take a look.
I paged out all context long time ago... The code is too tricky to say if it's possible/not possible/fixed by the patch/not fixed by the patch looking at the diff.
A test would be helpful. Will also prevent regression in the next release.
Quoted 23 lines
Thanks,
IanQuoted 20 lines
--- kernel/events/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/events/core.c +1
-2@@ -7543,7 +7543,7 @@ static void perf_sigtrap(struct perf_event *event)* Both perf_pending_task() and perf_pending_irq() can race with the * task exiting. */ -if (current->flags &PF_EXITING ) +if (current->flags &PF_EXITING || event->ctx->task ==TASK_TOMBSTONE )return ;/* -- 2.34.1
Peter Zijlstra
maintainerQuoted 19 lines · Luo Gengkun
A race condition exists between
perf_event_exit_task()andperf_pending_task().During
begin_new_exec(),perf_event_exit_task()may be called, and thePF_EXITINGflag is not set on task. soperf_sigtrap()continues to execute and triggersWARN_ON_ONCE(event->ctx->task != current).To fix this problem, also check if the
event->ctx->taskisTASK_TOMBSTONE.Fixes 97ba62b27867 ("perf: Add support for SIGTRAP on perf events")Signed-off-by Luo Gengkun <[email protected]>--- kernel/events/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/events/core.c +0
-0
kernel/events/core.c static void perf_sigtrap(struct perf_event *event) · line 7547
Quoted 6 lines
kernel/events/core.c +1
-1@@ -7543,7 +7543,7 @@ static void perf_sigtrap(struct perf_event *event)* Both perf_pending_task() and perf_pending_irq() can race with the * task exiting. */ -if (current->flags &PF_EXITING ) +if (current->flags &PF_EXITING || event->ctx->task ==TASK_TOMBSTONE )return ;
Since do_exit() also calls perf_event_exit_task() (albeit a little later than exit_signals()), and task_can_queue_signal() will return false when PF_EXITING, and we ignore the return value of send_sig_perf()/send_sig_info(). I don't think we need to check both.
That is, I think:
if (event->ctx->task == TASK_TOMBSTONE)
is sufficient. Also, the comment needs updating.
PATCH v2 1/1 perf/core: Fix race between perf_event_exit_task() and perf_pending_task()
A race condition exists between perf_event_exit_task() and perf_pending_task() during begin_new_exec().
During begin_new_exec(), perf_event_exit_task() may be called, and the PF_EXITING flag is not set on task. So perf_sigtrap() continues to execute and triggers WARN_ON_ONCE(event->ctx->task != current).
Since both task exit and exec paths can call perf_event_exit_task() which sets ctx->task to TASK_TOMBSTONE, fix this by explicitly checking if event->ctx->task equals TASK_TOMBSTONE and dropping the redundant PF_EXITING check.
---
v1 -> v2:
- Drop the redundant PF_EXITING check and only check for TASK_TOMBSTONE.
- Update the code comment to reflect this unified exec/exit race protection.
--- kernel/events/core.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/events/core.c +4 -2
@@ -7616,9 +7616,11 @@ static void perf_sigtrap(struct perf_event *event) {/* * Both perf_pending_task() and perf_pending_irq() can race with the -* task exiting. +* task exiting or exec-ing. We can determine if such a race has +* occurred by checking if perf_event_exit_task(), which will set +* ctx->task to TASK_TOMBSTONE, has already been called. */ -if (current->flags &PF_EXITING ) +if (event->ctx->task ==TASK_TOMBSTONE )return ;/*
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v2] perf/core: Fix race between perf_event_exit_task() and perf_pending_task()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines
A race condition exists between
perf_event_exit_task()andperf_pending_task()duringbegin_new_exec().During
begin_new_exec(),perf_event_exit_task()may be called, and thePF_EXITINGflag is not set on task. Soperf_sigtrap()continues to execute and triggersWARN_ON_ONCE(event->ctx->task != current).
Quoted 7 lines
Since both task exit and exec paths can call
perf_event_exit_task()which setsctx->tasktoTASK_TOMBSTONE, fix this by explicitly checking ifevent->ctx->taskequalsTASK_TOMBSTONEand dropping the redundantPF_EXITINGcheck.Fixes 97ba62b27867 ("perf: Add support for SIGTRAP on perf events")Signed-off-by Luo Gengkun <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[tip: perf/urgent] perf: Fix race between perf_event_exit_task() and perf_pending_task()
In reply to Luo GengkunThe following commit has been merged into the perf/urgent branch of tip:
The following commit has been merged into the perf/urgent branch of tip:
Commit-ID: ffb684f2aa141eeb0caa6308422e7e05d1ded7c4 Gitweb: https://git.kernel.org/tip/ffb684f2aa141eeb0caa6308422e7e05d1ded7c4 Author: Luo Gengkun <[email protected]> AuthorDate: Sun, 20 Sep 2026 07:50:26 Committer: Peter Zijlstra <[email protected]> CommitterDate: Thu, 01 Oct 2026 14:02:05 +02:00
perf: Fix race between perf_event_exit_task() and perf_pending_task()
A race condition exists between perf_event_exit_task() and perf_pending_task() during begin_new_exec().
During begin_new_exec(), perf_event_exit_task() may be called, and the PF_EXITING flag is not set on task. So perf_sigtrap() continues to execute and triggers WARN_ON_ONCE(event->ctx->task != current).
Since both task exit and exec paths can call perf_event_exit_task() which sets ctx->task to TASK_TOMBSTONE, fix this by explicitly checking if event->ctx->task equals TASK_TOMBSTONE and dropping the redundant PF_EXITING check.
--- kernel/events/core.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/events/core.c +4 -2
@@ -7626,9 +7626,11 @@ static void perf_sigtrap(struct perf_event *event) {/* * Both perf_pending_task() and perf_pending_irq() can race with the -* task exiting. +* task exiting or exec-ing. We can determine if such a race has +* occurred by checking if perf_event_exit_task(), which will set +* ctx->task to TASK_TOMBSTONE, has already been called. */ -if (current->flags &PF_EXITING ) +if (event->ctx->task ==TASK_TOMBSTONE )return ;/*