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=1796382143; b=j0o+x1XS6FkLy3OPoYl3dRC34bB4esvV97WgvKXDWWuaKELyV71Ft4cHqvwCPOIXafqj gu3BgQ7wH4GWdEiqpNscKdNV9atpcEGNk094CIZkenZs7pazIJU7K7yEUNPW+1qJ2HZhN 8xQbq7QBZSS++mftnVQQI2OyFbqurAPxxlVaH2DrzAI/Om7yGWQP+yGUxiWsEEnyjQ44I KOwZrnQ8hQAuvdCNPIvLFesiDCB8qVJMSGrk7qHc3v448B54DpYkaqhRiYa2O+av3BYLN NN5YM1X80jVN7SkrBaUBxo/VWOu6Mgje9CP/Pp85Vr+SGx2ujiT2jhlz6mPQO84Msag== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382143; 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=yKVG9zhpcqivA+mC8KtxsCnymoroeL8pt0d5Rp37SdQ=; b=bW9LuYPoafayHgUVQ9nH9/4JZS8PHpY8kS2/jGKv+v9ihxcJ8DecUG29f21uW7+neXH1 avWPy97Be4xV7xLuAFTjowf32cdYrHX3ciViRI7k5BiFniPkbDlsq+L0CXYD+K8a7f2KL g0znkZochyqKehW8htM2DVhXAxHQOSgnp6xfjwyZP9sJSbmmQqj/j/6PhYrBqLT62xyqq P/vi6iV6w+zRMCdUfAERRA3VCWxhdr8aMs54DsxIfSaueHKmjj5kStPsGNUCObwqnkoev yalqwb3NVCR8tWcgmPYVufKD8D8/SfS5Kpk1C7gLM+6b1eWJy9U4T5nKf4QCYJDMdKw== 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="VQGc2Cp/"; 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 1B1E9606A322; Fri, 4 Dec 2026 11:02:23 +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=VQGc2Cp/ Received: by smtp.kernel.org (Postfix) with ESMTPSA id 859841F000CE; Fri, 4 Dec 2026 11:02:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796382143; bh=yKVG9zhpcqivA+mC8KtxsCnymoroeL8pt0d5Rp37SdQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=VQGc2Cp/99mTIzAetWzVhK6vjv1mgFi8UpeTDS9SVQYNVmdTCWvqD4Ne51O0NJBvY auhOHrj04cNajZfXbsjsPzymMvGBjFgmv8cJsTHSodXhWC/etsgdOVpSjKiXJYJ1bA miL1j2UlP6aYDSQ3gmW1kf42vrEY1At7y/yP21+I= 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 328/703] perf: Prevent duplicate sigtrap task work enqueue Date: Fri, 4 Dec 2026 19:03:47 +0900 Message-ID: <20261204100636.512378026@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: 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 eea5cab38bc81..698444fba7736 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -7538,9 +7538,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); @@ -10635,16 +10641,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