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=1796380629; b=Rzc25z9T07UlndFXZkB4fd9A/0hZmoJjk+05yzVTEfPrpoFMu1B14OdQzgL6b2pmA6yM XQlGRW15BGGXPlHGCDrYeD5iHVkjFhx7SSnZumjFeMnBzgrYE2IXod6SmtfnDNmb+baY0 3GwmCDuiC0v/LVdraZoqwmzMvmAc9XirWrCsJTbhlZ1G4GXay8jT/dI5ir319IgmmckwD 8G1vw3RSZ9EMgU3PdZ++MDpyytHcyjD6QA9zYUqJLDqY9xGijTUVG1109upcQwWef8jKP tLB4CwADNzF1S8y+UxilmX5c2sH2DAmaYz2AVYvSi8w0lJwbC40VZcbKLdV6WcP20rA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796380629; 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=meYehL6HwUq31U7XZ4/wqSoaG0PP7K6DlIbR+cOGJDw=; b=5tVNBTV+ZNZdluWs/3wUj3ODlVjoAAePo6vYOC28FIIdicNYo+eQ88Ii1BihuWGvCULf 6hsQe4m/eHgbuoFtPoZcQF0tl9PrcYcT5jhfTDeb0kDIbMeFKoCg3r6Vh7uvMPLfWSuCs 0GE7K4cUeW5DkJgsd9uQ6Cvv/P/+tjoCE2E8VRMb3lY0XSm4thTk2z7VK6hmc5kTLvqEF KOjk0wVdHUJ+uvWRS5SpYhLD3m0ikIAr9gSr5JWVECEZcrXWb+mc5OFOxw+O2IQn6ruFx oimW4H+XbZi+rucWrdlVW6cYTYw3htC597TlMCF7zIAtghQizW7MVBUW8s9BSDEQPkw== 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=qOak6E43; 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 86789606A682; Fri, 4 Dec 2026 10:37:09 +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=qOak6E43 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 05CDD1F00931; Fri, 4 Dec 2026 10:37:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796380629; bh=meYehL6HwUq31U7XZ4/wqSoaG0PP7K6DlIbR+cOGJDw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=qOak6E431eTjak3GeMzjcxvfaZLe6H7/UgbDqeJLIOEhBR466tRH39S2YGS8FzhKm I0l1lXGMoP3lf3xn0aiGPbnuUSpjBkd2ikRGAJxBBu8wzjg2++CllKYEh4g60b90bF spKsG1/c3SK7j/y/z3EB1ZjvicuiRPNutdwMWtrQ= 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 315/742] perf: Fix sigtrap for software events Date: Fri, 4 Dec 2026 19:02:59 +0900 Message-ID: <20261204100643.398026645@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: Peter Zijlstra [ Upstream commit 38606d2911f7f2fe80388315da0c4a7fc98af0f8 ] 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(). The nr_no_switch_fast bits hard rely on perf_pmu_disable() actually working. It turns out the software events do not implement the pmu::pmu_{dis,en}able() methods and generally ignore the state, making the above scenario worse in that an overflow can happen in the middle of swapping task contexts, making it impossible to reconstruct state. Ensure software and tracepoint events respect the disable state. Fixes: ca6c21327c6a ("perf: Fix missing SIGTRAPs") Fixes: c5d93d23a260 ("perf: Enqueue SIGTRAP always via task_work.") Reported-by: Tengda Wu Signed-off-by: Peter Zijlstra (Intel) Tested-by: Tengda Wu Link: https://patch.msgid.link/20261125161342.GP2669272%40noisy.programming.kicks-ass.net Signed-off-by: Sasha Levin --- include/linux/perf_event.h | 1 + kernel/events/core.c | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index 8a985486ab122..07b9d5a319996 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -1237,6 +1237,7 @@ extern const struct perf_event_attr *perf_event_attrs(struct perf_event *event); extern void perf_event_print_debug(void); extern void perf_pmu_disable(struct pmu *pmu); extern void perf_pmu_enable(struct pmu *pmu); +extern bool perf_pmu_enabled(struct pmu *pmu); extern void perf_sched_cb_dec(struct pmu *pmu); extern void perf_sched_cb_inc(struct pmu *pmu); extern int perf_event_task_disable(void); diff --git a/kernel/events/core.c b/kernel/events/core.c index cd070695f184b..3b8aea12a2fc5 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -1366,6 +1366,12 @@ void perf_pmu_enable(struct pmu *pmu) pmu->pmu_enable(pmu); } +bool perf_pmu_enabled(struct pmu *pmu) +{ + int *count = &this_cpc(pmu)->pmu_disable_count; + return *count == 0; +} + static void perf_assert_pmu_disabled(struct pmu *pmu) { int *count = &this_cpc(pmu)->pmu_disable_count; @@ -11147,6 +11153,9 @@ static int perf_swevent_match(struct perf_event *event, struct perf_sample_data *data, struct pt_regs *regs) { + if (!perf_pmu_enabled(event->pmu)) + return 0; + if (event->attr.type != type) return 0; @@ -11527,6 +11536,9 @@ static int perf_tp_event_match(struct perf_event *event, struct perf_raw_record *raw, struct pt_regs *regs) { + if (!perf_pmu_enabled(event->pmu)) + return 0; + if (event->hw.state & PERF_HES_STOPPED) return 0; /* -- 2.53.0