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=1796383656; b=itwo8NKL4FdTt10P6Dz3YCySgGYYpmikWpgK0ej/uePTR5VjDZZiiQhJfxvajVDPBwZX ydAcIqnIrbsVdHUXsIX4ljojNXX9YjFN17Ir0C9TYzeAcd6LVg0oSxlG0hGhq3bQ4GrXG KoKgbtLCXYgMdh/MhZj6Hm8vqoU9RUbb1h5tVqfZLK6a6nBd4aRXY+adPFl01mvv/NE4i /9Hfr5MwfoUGlrnAwQ5ZOaFcZvlQh/zd70LVhbBNdCFLnLLW6C5lw+gBDxMP9xf3jeaVH bvd0wK5AQkmToBF1tKwKwTEqjs3E636C3deXneDP5wZVl4JzhwlpSI/3A2+cklAg9kw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796383656; 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=WebZRPPwwUYNG1LL//KyHJKSTSPUBVNkSc4dHWdCJEU=; b=jZacVzEOjBVvLrrWhQUJdEXKENEYPWnfKwBVDRc3dJ+ZzPnMQBIWXyAlUqZlOgMHmoHd twKE9pf+SU2+9VU8ZdnXq3ORw4RqRgWPzyshhMRfwOZWMTQy7pfNLz6G7n6byMGaeKlp6 BVdIkgKUU8ets7WXrNhNRrm+2+/idkfA/fHucR/huIx6imK4Uv6rb5fVdFyTvZNRXT6jk 1lPvtN17ZsVDIenJ+aYCCBxsnemDnjlAv9/vvPOnL7rgg0CYgp7caOl/gzJptZXSjn5Vi +Eip/IQ90F2KLpKrfqePkCVWJnIO+/Scc4S4P4gxYXlpbYW8jzFlK8Ybif6fZ7GfI0w== 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=kESBENAQ; 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 54DC4606A322; Fri, 4 Dec 2026 11:27:36 +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=kESBENAQ Received: by smtp.kernel.org (Postfix) with ESMTPSA id C6F091F000CE; Fri, 4 Dec 2026 11:27:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796383656; bh=WebZRPPwwUYNG1LL//KyHJKSTSPUBVNkSc4dHWdCJEU=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=kESBENAQOUiDsmYGh9YoCPwa3gyFZd0kQ3p/IJpQXMnbI4jGS5oo/iigTMbtFlzKz 8Mb/KS4wq2YKmmgYyDROtS9a1xIJ5+2b/oZiGJOXYxP5FLRImN84zfXmNgxtyuvWSP CIQ893UO+dshpJnO8T0MwtQxlmXseHTTjI2IzqRk= 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 314/555] perf: Fix sigtrap for software events Date: Fri, 4 Dec 2026 19:05:57 +0900 Message-ID: <20261204100621.489844413@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 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 d88c0091e57dd..35f9377f70a2a 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -1148,6 +1148,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 b91838ce2a226..b48b7222feae0 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -1219,6 +1219,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; @@ -10157,6 +10163,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; @@ -10537,6 +10546,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