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=1796380627; b=EESVMLPzM6YyxvlUYr6Sw29904zF7bTv1OqcddnHxSxM20VYRSPZl8Zd9Iva1cFrKlbp ihByxyuAXDM0XlQ0t9/old7eT6R4sA0AbUWPb/t39VxGE2rxOxM0F09E4N/4UmtKxF6tZ yBjUO2Kk3DB9oo1Vohf7UvLSR29ZZoZe+YPA+Wlxu7/eCMIvK8c9m9rQQCaGjuYezd5jv 5QaTSvVDefKLzZO8XD2oQ+Y7Tp6jwxi4IMzrNlNDVTS50pCiu4ndtI1zhoMgPQx8VW+yr Zu0d/jYHmhu6NPAmEA+jBdW0dFpuh6PRYwoyYh+uAs8J3IhYhiRDTCxLK2x3xwNXX9g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796380627; 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=7ALv8az6U+ZtqyeA2UoKGy42A4vdqMlVolzkKZxHYcI=; b=pEsJmMEPa3n6BR7Jysvh72qthJfHcOJb11VleOkJ9gRo/iwA/Z4M5bI+GpXPIJW26eEM D5rW12Xtf3qaQhbe9AoKcFgG9ce8srNkz6V2KVude6TEupeJ9Fqv3yKZjKJ2ZfaPEh9mG HCUXBMNT1gc+uM9jnVfX55+8RibY8XxjZhofv/1unjkWMouZdyqXfeIqx8s55UPUn+nGE G0ch3CK7Nq5qzc1OtvMzlCxS0YJwCTCdIVyHgL8B4cKoso1fn76xL1UxtT7fsBS8eHR2G 4rTpNCxi0U1ufHvogL3OZSDbU7ZG02zIa+28RZ8/zekjqP9CgeRur+LUSU1AIgEn22g== 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=OIQVgDM+; 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 9A93D606A6A5; Fri, 4 Dec 2026 10:37:07 +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=OIQVgDM+ Received: by smtp.kernel.org (Postfix) with ESMTPSA id C9B801F00A02; Fri, 4 Dec 2026 10:37:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796380627; bh=7ALv8az6U+ZtqyeA2UoKGy42A4vdqMlVolzkKZxHYcI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=OIQVgDM+GyWSs8XrkLD5/gsRdju51wkFx+w7df7CHFhnLtEmzraewwZG47cmSICgM 57Xcazuhdu6cCcZe8zn/R+NzXQ0Z8hXoqqiHWWkf2um0DSl5pI2fySh4g1JQeDZMkJ I8iO4lsDpT3/6Vtt10mNAbF1XuUT57XHeWxO1N7c= 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 7.3 314/742] perf: Prevent duplicate sigtrap task work enqueue Date: Fri, 4 Dec 2026 19:02:58 +0900 Message-ID: <20261204100643.370590930@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@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 7.3-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 a62a1273fc691..cd070695f184b 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -7767,9 +7767,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); @@ -10940,16 +10946,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