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=1796383654; b=fmjaxLuY0/sELXpbi6L4eT7CMgAIKJciFV6gKGMOvBH9ZVdAk2RkIkCu18GO0irEe5PV Ly89uaU/iqEyZx13cH3qjPQFzfcgrz0t7gqJI8NGLsEFgmGKkp31G7JTkLf4QSUhK0D2I p1PsNbz1KWC2baPfbxXYD8M1K1u00QsNVFFXqi+Z3zv+HBH/6pmnp0egq8pcfSaARlmya h/PUtRPiDb1PMROPWD04LAsuhVyo+WMt/O1WyN6KjZ0o4+bWwnocHFUMsKKMaRk8VtH6I qMnpzOro7F0LsnIKBs8mBFp0E1WFWFXXW8LYbHY/eMyhcxmQRNpLb3e7evoFWeei9Jg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796383654; 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=0wsByaU3IXBA54efcSAKcc1Mm7hnsAm2QeVIqVnOdDM=; b=KJZv90AiyL6b+ytGokk6b0l3Nm5EhQobk0rrsJPkFf6dfpAj0kDOcKLvB6DOWnRvSWOA Ombh190CB7mn2jiB1tMHGG/itxHj1fEaINfsr0U8BmYf21bEnQnW0W2Rfk9RWnb9lKeAP svmO9bvyIIf8BOT4JDpENg8oJ3Q/zJ5fL4YsmuJmHGa80SeyAtQlydHf54pLYdiIRv5A6 BS3KUbp3iIFld10Q+2bvn2Wipx24dIR/IX1EXtulcajUr6rLgOPaH77GZDfB7QFnGnYya NppsSEy0dk5DRJzxSKnVoeD0ohSwbChj1+8udp9u2HhNfLm+SvJDqD9vTda8+9hFGig== 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=H1NEJGHk; 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 2B83C606A322; Fri, 4 Dec 2026 11:27:34 +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=H1NEJGHk Received: by smtp.kernel.org (Postfix) with ESMTPSA id 964AD1F000C2; Fri, 4 Dec 2026 11:27:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796383654; bh=0wsByaU3IXBA54efcSAKcc1Mm7hnsAm2QeVIqVnOdDM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=H1NEJGHkFeoPT1oYYyczb+dhdx6mwzjiuTSik1tSPkYy6EhfLsiCl2C4ZNZQ2n4VA duBm1T4t7mAzgndpF65UVbNEalNW9ys7byxnAOxQAfGnTcodoypeCjWhglUmy8rpXK QH6pOCSEG5/fy0MxTt5WdGc2phMxFkxoxZVjtvOw= 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 313/555] perf: Prevent duplicate sigtrap task work enqueue Date: Fri, 4 Dec 2026 19:05:56 +0900 Message-ID: <20261204100621.427407484@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: Tengda Wu [ Upstream commit e4c5112f01816b0ac6d74724545eca1195788c38 ] The sigtrap handling in __perf_event_overflow() checks event->pending_work before calling task_work_add(), but only sets pending_work after the task work has been queued. This leaves a window where an NMI can interrupt task_work_add() and re-enter __perf_event_overflow() for the same (software/tracepoint) event. Since pending_work is still zero, the nested invocation can enqueue the same pending_task again. The re-entrant call path looks like this: __perf_event_overflow() task_work_add() __perf_event_overflow() task_work_add() The nested invocation may enqueue pending_task before the outer invocation does. When the outer invocation resumes, it inserts the same task work entry again, potentially creating a cycle in the task work list. As a result, perf_pending_task() may be executed repeatedly, leading to unbalanced event reference counting. Fix this by atomically claiming pending_work with cmpxchg_local() before calling task_work_add(). This ensures that only one invocation can enqueue the task work, even when another overflow occurs re-entrantly from an NMI. Notably: sigtrap events must be per-task, meaning that the there is no concurrency, only re-entrancy. [peterz: clarify and rewrite] Fixes: c5d93d23a260 ("perf: Enqueue SIGTRAP always via task_work.") Signed-off-by: Tengda Wu Signed-off-by: Peter Zijlstra (Intel) Link: https://patch.msgid.link/45bd71c976aea1a0e8bf5762dd6b6552e6009439.1795053619.git.wutengda@huaweicloud.com Signed-off-by: Sasha Levin --- kernel/events/core.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index e830683daef43..b91838ce2a226 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -7063,9 +7063,15 @@ static void perf_pending_task(struct callback_head *head) rctx = perf_swevent_get_recursion_context(); if (event->pending_work) { - event->pending_work = 0; perf_sigtrap(event); local_dec(&event->ctx->nr_no_switch_fast); + /* + * event->pending_work also acts as a 'lock' for + * event->{pending_addr, orig_type, attr.sig_data}. As such we + * must clear after perf_sigtrap is complete. + */ + barrier(); + event->pending_work = 0; } put_event(event); @@ -9949,16 +9955,22 @@ static int __perf_event_overflow(struct perf_event *event, notify_mode = in_nmi() ? TWA_NMI_CURRENT : TWA_RESUME; - if (!event->pending_work && - !task_work_add(current, &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)); - - event->pending_addr = 0; - if (valid_sample && (data->sample_flags & PERF_SAMPLE_ADDR)) - event->pending_addr = data->addr; - + /* + * Software events can nest and attempt this recursively; + * 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)) { + event->pending_work = pending_id; + local_inc(&event->ctx->nr_no_switch_fast); + WARN_ON_ONCE(!atomic_long_inc_not_zero(&event->refcount)); + + event->pending_addr = 0; + if (valid_sample && (data->sample_flags & PERF_SAMPLE_ADDR)) + event->pending_addr = data->addr; + } else { + event->pending_work = 0; + } } else if (event->attr.exclude_kernel && valid_sample) { /* * Should not be able to return to user space without -- 2.53.0