Skip to contentSkip to conversation

perf/core: Fix race between perf_event_exit_task and perf_pending_task

PATCH Superseded Luo Gengkun 6 participants 7 messages Apr 16 – Oct 2 perf events

lore
Share & tools
lore
#0 lore raw

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.

Fixes 97ba62b27867 ("perf: Add support for SIGTRAP on perf events")
---
 kernel/events/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/events/core.c +1 -1
index 1f5699b339ec..3422900263fa 100644
@@ -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
reviewer
#1 lore raw
Quoted 11 lines · Luo Gengkun

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.

Fixes 97ba62b27867 ("perf: Add support for SIGTRAP on perf events")

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
index 1f5699b339ec..3422900263fa 100644
@@ -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

#2 lore raw
Quoted 9 lines · Ian Rogers
Quoted 7 lines · Luo Gengkun

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).

Quoted 6 lines
Quoted 4 lines

To fix this problem, also check if the event->ctx->task is TASK_TOMBSTONE.

Fixes 97ba62b27867 ("perf: Add support for SIGTRAP on perf events")

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,
Ian

Quoted 20 lines
---
 kernel/events/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/events/core.c +1 -2
index 1f5699b339ec..3422900263fa 100644
@@ -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

maintainer
#3 lore raw
In reply to Luo Gengkun
Quoted 19 lines · Luo Gengkun

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.

Fixes 97ba62b27867 ("perf: Add support for SIGTRAP on perf events")
---
 kernel/events/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/events/core.c +0 -0
index 1f5699b339ec..3422900263fa 100644

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.

#4 lore raw

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.

Fixes 97ba62b27867 ("perf: Add support for SIGTRAP on perf events")

---
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
index fe33fe15689d..8f4b61a135e8 100644
@@ -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
#5 lore raw

Re: [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() 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).

Quoted 7 lines

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.

Fixes 97ba62b27867 ("perf: Add support for SIGTRAP on perf events")

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
merge bot Applied
#6 lore raw

[tip: perf/urgent] perf: Fix race between perf_event_exit_task() and perf_pending_task()

In reply to Luo Gengkun
The 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.

Fixes 97ba62b27867 ("perf: Add support for SIGTRAP on perf events")
---
 kernel/events/core.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/events/core.c +4 -2
index 634d2cc..c2882b5 100644
@@ -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;
 
 	/*