All of lore.kernel.org
 help / color / mirror / Atom feed
From: Luo Gengkun <luogengkun2@huawei.com>
To: <peterz@infradead.org>
Cc: <acme@kernel.org>, <adrian.hunter@intel.com>,
	<alexander.shishkin@linux.intel.com>, <dvyukov@google.com>,
	<elver@google.com>, <irogers@google.com>,
	<james.clark@linaro.org>, <jolsa@kernel.org>,
	<linux-kernel@vger.kernel.org>,
	<linux-perf-users@vger.kernel.org>, <luogengkun2@huawei.com>,
	<mark.rutland@arm.com>, <mingo@redhat.com>, <namhyung@kernel.org>
Subject: [PATCH v2] perf/core: Fix race between perf_event_exit_task() and perf_pending_task()
Date: Sun, 20 Sep 2026 07:50:26 +0000	[thread overview]
Message-ID: <20260920075026.990582-1-luogengkun2@huawei.com> (raw)
In-Reply-To: <20260918095049.GL1837346@noisy.programming.kicks-ass.net>

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")
Signed-off-by: Luo Gengkun <luogengkun2@huawei.com>
---
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(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index fe33fe15689d..8f4b61a135e8 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -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;
 
 	/*
-- 
2.34.1


  reply	other threads:[~2026-09-20  7:25 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-16  3:34 [PATCH] perf/core: Fix race between perf_event_exit_task and perf_pending_task Luo Gengkun
2026-09-15 21:34 ` Ian Rogers
2026-09-18  9:02   ` Dmitry Vyukov
2026-09-18  9:50 ` Peter Zijlstra
2026-09-20  7:50   ` Luo Gengkun [this message]
2026-09-20  7:32     ` [PATCH v2] perf/core: Fix race between perf_event_exit_task() and perf_pending_task() sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260920075026.990582-1-luogengkun2@huawei.com \
    --to=luogengkun2@huawei.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=dvyukov@google.com \
    --cc=elver@google.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.