From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796382147; b=jNhYSGY1ImRRfyr8ifErkdWcv638lRrluwSYFfS2CDMzgvWeHwgDHKfae6VLlmRYqh0W //p2kKQ7bp8eNGI5SpiAXXRVim/AAVXlZ7cgLbA9DuY5yxm6ivoFYHnG1Xb1tlDrZ7d9v kcPG2RSkqaTaZEt50+z/WbjN7zjWJm76jSV3x6ke0j34rHef/21FoyLj8S0Xq/d4vdzvm siG6mK68WZ3rrgW96PZkyNqNhVXm03IXHfCpRq/TQr2muGaciUJF4x5qQzd4JBOR2vhT7 TTj2Ja4E8NfHqj1uVydrriv8olcBq7PKs+YtpsV+w8aUwe6FPkbMuWKEVg3I5+R2wLQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382147; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=Kzw+v5mESKNiMbLTvwUg3OgsxVbigUSk/kGGVSGbQ+E=; b=V8aEP83FmhVOGcXeEn3NrZD2QleGIZ/6ioOFTLdnLCj6PCQ495G0N7tJzjn/NUYU5piW 21wP4J+/9tmQ/QP4jVNLskXRju9Cbs7nsgddFxOcq3HjzRCb681F0WJCUCHzVI/g+02/H /GdFUpMgYeCcQdiAcrc1XCWsHLY1t2u8wEu6Qa/H/SiDLp+NSg5yrAjNXZhU3mkV3aY8X GaiCy3yxDJsJqnK/LDipoLLYlLU3N4JiPd1dBbjfPocGblB0Cqon/JKeueeSCKWF+h6Tm YkoymYUtQdc2LebGQlVV5zVj2LH/mEovg97HoqgoCEjNN2MGcK8UsjMIsekDjea2TuA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=Kyhrm07s; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B20CE606A322; Fri, 4 Dec 2026 11:02:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=Kyhrm07s Received: by smtp.kernel.org (Postfix) with ESMTPSA id 25A261F00897; Fri, 4 Dec 2026 11:02:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796382147; bh=Kzw+v5mESKNiMbLTvwUg3OgsxVbigUSk/kGGVSGbQ+E=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Kyhrm07swv29koHvuJebtTV0zL9ec8j+fLQP1KZsHz3q2XZ77DyY+a9+bVe8hUHMF 399DsDXstk7yq/xdAQtKP38iD7Nq+JrFZeq0qYa2QUf4U13fQ/pSY4TWsTyBsiL+j0 V6yuKR2o1ejeSl/jTzfWFWlQVMXZ9kze1h2LH6Bo= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Tengda Wu , "Peter Zijlstra (Intel)" , Sasha Levin Subject: [PATCH 6.18 330/703] perf: Fix sigtrap vs fast-switch Date: Fri, 4 Dec 2026 19:03:49 +0900 Message-ID: <20261204100636.564275285@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100627.522266145@linuxfoundation.org> References: <20261204100627.522266145@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Peter Zijlstra [ Upstream commit 34dc983cc1628dea5054baa8687477cbb8683e28 ] perf_sigtrap() contains: WARN_ON_ONCE(event->ctx->task != current) The warning assumes that an event's ctx->task always matches the task that ends up running the pending task_work. That assumption can be broken by the context fast-swap path. The failure sequence is: 1. A -> B switch, perf_event_context_sched_out() takes the fast path and does WRITE_ONCE(ctx->task, next) / WRITE_ONCE(next_ctx->task, task). An NMI then hits while the context is swapped, and __perf_event_overflow() queues pending task_work on the *current* task (A), even though the event now belongs to B's swapped-in context view. 2. B -> A switch. Because A now has pending task_work, nr_no_switch_fast is non-zero, so the slow path is taken and ctx->task is *not* swapped back. 3. A returns to user mode, task_work runs perf_pending_task() -> perf_sigtrap(), and event->ctx->task no longer equals current, tripping the WARN_ON_ONCE(). Therefore; the task_work_add() should in fact use event->ctx->task, rather than current. They are always the same, except in this narrow window between perf_event_task_sched_out() and switch_to(). Fixes: c5d93d23a260 ("perf: Enqueue SIGTRAP always via task_work.") Reported-by: Tengda Wu Tested-by: Tengda Wu Signed-off-by: Peter Zijlstra (Intel) Link: https://patch.msgid.link/20261125161342.GP2669272%40noisy.programming.kicks-ass.net Signed-off-by: Sasha Levin --- kernel/events/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 012934ddd9ccc..ff9752ff8ff82 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -10652,7 +10652,7 @@ static int __perf_event_overflow(struct perf_event *event, * claim the pending_work before task_work_add() recurses. */ if (cmpxchg_local(&event->pending_work, 0, 1) == 0) { - if (!task_work_add(current, &event->pending_task, notify_mode)) { + if (!task_work_add(event->ctx->task, &event->pending_task, notify_mode)) { event->pending_work = pending_id; local_inc(&event->ctx->nr_no_switch_fast); WARN_ON_ONCE(!atomic_long_inc_not_zero(&event->refcount)); -- 2.53.0