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=1796382145; b=NeS+a53DN6IFaQmSl5NYjPpt8lvgbrkMzdLP9+EioDCvLI0lRaFd+CqM4VM6+fENPhh/ GPqLlkVasrdnke2NTFu7V62pt8yxKd6V0OnCo3gnugBNflmzcdznk6tGaOpCZ7UwcCbr7 v5ZDZxMk50ziuE2jx1+NYDJXwomDOmHzjSXftQ5WeTxDV1Vszk5rdNnrOU31wr4/U9+Xy Xed2mogrdxMWw4gQgPJh3yhki6jFPM2s8Hws4HGTxRW9O/IvvB6aNfQIF9iYNfS69ccNS frPjq6E481Yrri+D7tBcccSxyYkULmW9y27EltyuQx2x9yOzwhJDxIqQYXD28vcHkIg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382145; 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=uxlOr9Ic2W/kIfnUk/cUTjgcttAMUh6ZvUiVQdwHhP8=; b=0G48IYaeig4wROTY3bW7kd1azoiK2krCTekVqjoWGmpRdxLYT+dv+4pJ2sswBWvBcBDA tQlwA4X9XE2uA8f7GVBi9HOjCVTQ31A1MhurcAAu9YnGEjyoJf0b7fJmWWLdTEdGJOL+b TiQKsyeC3y9pwQ5mlRYoEuaIOmVvg3hCydT8MjKURFvodo6YrcrlRWQmhUhvcctBCW/qi tq5MYP6KtBtPyWUo4PBeRjnY9ASVMTxuL1OQn+qql0xQ/Cpi0GlXRAsFbveztpC94GTVe mSabmvx9YQF8fZTtefu5MzCVxmZckUpN3u+0Thds2l9Z/zES0ymfRRQFb9g7k5JqrDQ== 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=oUyQovOY; 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 A57DA606A32C; Fri, 4 Dec 2026 11:02:25 +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=oUyQovOY Received: by smtp.kernel.org (Postfix) with ESMTPSA id C3B021F000C2; Fri, 4 Dec 2026 11:02:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796382145; bh=uxlOr9Ic2W/kIfnUk/cUTjgcttAMUh6ZvUiVQdwHhP8=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=oUyQovOYA6qnwZ8abHSrj+mnPlbVjdEke/3vbdSdIw86+urUbsVWSfLINJO0Zwxb7 zTSRoIg6Wg4DVd1VYaKhhAKHD0x1TCpN4NZrJyb0ZOhORVa6IWdFx9bSO8cU35Se+S fcyyVsB3B/JOp4M8SZLWjeN7o0oFJHvMBQNkqXl0= 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 329/703] perf: Fix sigtrap for software events Date: Fri, 4 Dec 2026 19:03:48 +0900 Message-ID: <20261204100636.537422676@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: 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 23444b1b2503b..1dd7a2fcebfc0 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -1230,6 +1230,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 698444fba7736..012934ddd9ccc 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -1265,6 +1265,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; @@ -10842,6 +10848,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; @@ -11222,6 +11231,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