perf/core: Fix WARN in perf_sigtrap()
authorTetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Wed, 9 Jul 2025 11:27:52 +0000 (20:27 +0900)
committerPeter Zijlstra <peterz@infradead.org>
Wed, 9 Jul 2025 11:40:17 +0000 (13:40 +0200)
Since exit_task_work() runs after perf_event_exit_task_context() updated
ctx->task to TASK_TOMBSTONE, perf_sigtrap() from perf_pending_task() might
observe event->ctx->task == TASK_TOMBSTONE.

Swap the early exit tests in order not to hit WARN_ON_ONCE().

Closes: https://syzkaller.appspot.com/bug?extid=2fe61cb2a86066be6985
Reported-by: syzbot <syzbot+2fe61cb2a86066be6985@syzkaller.appspotmail.com>
Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/b1c224bd-97f9-462c-a3e3-125d5e19c983@I-love.SAKURA.ne.jp
kernel/events/core.c

index 0db36b2b2448a862acb2ba71aaa04525d7d801ba..22fdf0c187cd970f59ad8d6de9c1b231f68ec2cb 100644 (file)
@@ -7204,18 +7204,18 @@ void perf_event_wakeup(struct perf_event *event)
 static void perf_sigtrap(struct perf_event *event)
 {
        /*
-        * We'd expect this to only occur if the irq_work is delayed and either
-        * ctx->task or current has changed in the meantime. This can be the
-        * case on architectures that do not implement arch_irq_work_raise().
+        * Both perf_pending_task() and perf_pending_irq() can race with the
+        * task exiting.
         */
-       if (WARN_ON_ONCE(event->ctx->task != current))
+       if (current->flags & PF_EXITING)
                return;
 
        /*
-        * Both perf_pending_task() and perf_pending_irq() can race with the
-        * task exiting.
+        * We'd expect this to only occur if the irq_work is delayed and either
+        * ctx->task or current has changed in the meantime. This can be the
+        * case on architectures that do not implement arch_irq_work_raise().
         */
-       if (current->flags & PF_EXITING)
+       if (WARN_ON_ONCE(event->ctx->task != current))
                return;
 
        send_sig_perf((void __user *)event->pending_addr,