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=1796383658; b=3bgw3/E05EWq+10jie6a33v7mV3v036Atj9WfBva7sDs0dxdH4Dx4xmuOQ7OyWHMCXUv T9THN2pKYnDTOXF5R7yZ/fBGvwBja55FE/OLXkEDRXHpIvC0BtDvY9MU+Rp6wCi50rttM kOFkyCs9CU0062C1VWgiNyKz8eeWInkrAPFb+88UxdmEdqhSZDvGx6SWjTp5nOYuL172g 2fBpa4PqYXBXDFYpbG1qPSU6IhUZObOzSGR0p+T86+TM+ta2OCqP73T47DZrQnhau+cda qquwruNNp8ihcm62wlYafs4lCdsguWibRr6zCC8OtEraLik/kn9B1YzKnZahKDGPVLg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796383658; 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=TCTI61j3XcYdxij6Be6umvRpD6q8XnjPkZsmttdddSI=; b=AjPNVt+ByMlQwr5kCXXzyAnpNBQmKnP7LI8Ck0rfHtdpM2x5jsCLkmcLw/npBGojDePd Z57BqqG4kbYiEiCu9soF/n9oyt2bjBh53Ubc6Yr5abvk/Xy+HNiLtQIEteQENtFuYUSaM HN3JYJcyIiVebh2dxaVFZaIMAFjsylsJ45q2s97/5SOouB/zvTtQ+hnrxYoEis/TuRhf9 +Pi9BhoF5BdAM5tKgKyxVXYRxd/mmdaR/UwZvnCf82fkog4di5jLJr7d5yc8WwiRySZeZ w5nQYnVPd80xnlkIrfCYlJEiGMAOaNKWpkhLUYIGq27OE1pITmK5uxDRMoZptiOEH2A== 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=sswzWmdH; 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 7ED91606A32D; Fri, 4 Dec 2026 11:27:38 +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=sswzWmdH Received: by smtp.kernel.org (Postfix) with ESMTPSA id F19BE1F00897; Fri, 4 Dec 2026 11:27:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796383658; bh=TCTI61j3XcYdxij6Be6umvRpD6q8XnjPkZsmttdddSI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=sswzWmdHl0GnTXJDFff+K3Id3DSA/pLus0ZB/24BF0clHOKGw2jJ/JCw4s2F2zTPi Fbug2OY8Z8WabQwkeOEfmyhEiK62X85IDFZytN1GqghEMUJjPyvG7uH11hA9umm7zH v9xjV5cJUG4gYRthQVlh0U4ba41H/Q87Qzfd85Zc= 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.12 315/555] perf: Fix sigtrap vs fast-switch Date: Fri, 4 Dec 2026 19:05:58 +0900 Message-ID: <20261204100621.536561467@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100603.988357525@linuxfoundation.org> References: <20261204100603.988357525@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.12-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 b48b7222feae0..b414a8e078afe 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -9966,7 +9966,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