From: Tengda Wu <wutengda@huaweicloud.com>
To: Peter Zijlstra <peterz@infradead.org>
Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
James Clark <james.clark@linaro.org>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v5 2/2] perf/core: Disable context fast-swap for sigtrap events
Date: Mon, 23 Nov 2026 11:11:45 +0800 [thread overview]
Message-ID: <5507e48c-4e98-4e76-8d3f-4630a2655085@huaweicloud.com> (raw)
In-Reply-To: <20261120174606.GM2669272@noisy.programming.kicks-ass.net>
On 2026/11/21 1:46, Peter Zijlstra wrote:
> On Fri, Nov 20, 2026 at 06:37:06PM +0100, Peter Zijlstra wrote:
>> On Thu, Nov 19, 2026 at 09:51:01AM +0000, Tengda Wu wrote:
>>> 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 context fast-swap path was already excluded for events that need
>>> stable task association (inherit + sample read). sigtrap events have
>>> the same requirement: the SIGTRAP is targeted at ctx->task and must not
>>> be delivered after ctx->task has been swapped to another task.
>>
>> Oh gawd, let me guess, another one of those tracepoint events, right?
>>
>> Must be someone that doesn't respect perf_ctx_disable(). Fixing that
>> will close this hole.
>
> Here, does this work?
>
> The clue is right in perf_event_context_sched_out():
>
> perf_ctx_disable(ctx, 0);
>
> /* PMIs are disabled; ctx->nr_no_switch_fast is stable. */
> if (local_read(&ctx->nr_no_switch_fast) ||
> local_read(&next_ctx->nr_no_switch_fast)) {
>
> There must be no overflow after perf_ctx_disable().
>
>
> --- a/include/linux/perf_event.h
> +++ b/include/linux/perf_event.h
> @@ -1236,6 +1236,7 @@ extern const struct perf_event_attr *per
> 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);
> --- 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;
> @@ -11151,6 +11157,9 @@ static int perf_swevent_match(struct per
> struct perf_sample_data *data,
> struct pt_regs *regs)
> {
> + if (!perf_pmu_enabled(event->pmu))
> + return 0;
> +
> if (event->attr.type != type)
> return 0;
>
> @@ -11531,6 +11540,9 @@ static int perf_tp_event_match(struct pe
> 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;
> /*
This patch does not fix the issue; it can still be reproduced after testing.
The PoC is still the one mentioned in the cover letter:
https://lore.kernel.org/all/d1b24bbf-9111-4766-87c8-ff97adcac727@huaweicloud.com/
[ 191.663702] ------------[ cut here ]------------
[ 191.663827] WARNING: kernel/events/core.c:7679 at perf_pending_task+0x2e4/0x370, CPU#1: fastswap/1419
[ 191.667014] Modules linked in:
[ 191.667731] CPU: 1 UID: 0 PID: 1419 Comm: fastswap Not tainted 7.4.0-rc2-next-20261113+ #10 PREEMPT(lazy)
[ 191.669103] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.15.0-0-g2dd4b9b3f840-prebuilt.qemu.org 04/4
[ 191.670736] RIP: 0010:perf_pending_task+0x2e4/0x370
[ 191.671441] Code: 4c 8b ab c8 fe ff ff 49 ff 8d e8 00 00 00 e9 02 fe ff ff 4c 89 ef e8 6b f9 24 00 e9 b1 fd ff ff e8 c1 9
[ 191.674022] RSP: 0000:ffff88810ff9fe08 EFLAGS: 00010202
[ 191.674782] RAX: ffff88811844ed80 RBX: ffff888107575f98 RCX: 0000000000000000
[ 191.675791] RDX: 1ffff11020eaeb95 RSI: 0000000000000004 RDI: ffff888107575ca8
[ 191.676792] RBP: ffff888107575c00 R08: 0000000000000001 R09: ffffed1021ff3fbf
[ 191.677800] R10: 0000000000000003 R11: ffffe8ffffc832a0 R12: 0000000000000000
[ 191.678803] R13: ffff888105b19200 R14: ffff888107575e60 R15: ffff888118928000
[ 191.679809] FS: 00007f39079c6600(0000) GS:ffff88826fd58000(0000) knlGS:0000000000000000
[ 191.680939] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 191.681768] CR2: 00007f3907145000 CR3: 000000010fe7a006 CR4: 0000000000170ef0
[ 191.682774] Call Trace:
[ 191.683139] <TASK>
[ 191.683525] task_work_run+0x13d/0x200
[ 191.684111] ? __pfx_task_work_run+0x10/0x10
[ 191.684750] irqentry_exit+0x3a8/0x700
[ 191.685291] asm_exc_page_fault+0x26/0x30
[ 191.685884] RIP: 0033:0x00000000d25df49d
[ 191.686560] Code: c6 bf 2a 01 00 00 b8 00 00 00 00 e8 14 fe ff ff c9 c3 55 48 89 e5 89 7d fc 48 89 75 f0 48 89 55 e8 8b d
[ 191.689089] RSP: 002b:00007ffd422628b0 EFLAGS: 00010202
[ 191.689842] RAX: 000000000000002c RBX: 0000000000000001 RCX: 00007f390789cd13
[ 191.690847] RDX: 00007ffd422628c0 RSI: 00007ffd422629f0 RDI: 0000000000000005
[ 191.691849] RBP: 00007ffd422628b0 R08: 0000000000000000 R09: 00007ffd42262c27
[ 191.692857] R10: 00007f39079c68d0 R11: 0000000000000246 R12: 00007ffd422631b8
[ 191.693857] R13: 0000000000401a8a R14: 00007f39079ffaa0 R15: 0000000000403e08
[ 191.694866] </TASK>
[ 191.695196] ---[ end trace 0000000000000000 ]---
Thanks,
Tengda
prev parent reply other threads:[~2026-11-23 3:11 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 9:50 [PATCH v5 0/2] perf/core: Fix sigtrap task_work races Tengda Wu
2026-11-19 9:51 ` [PATCH v5 1/2] perf/core: Prevent duplicate sigtrap task work enqueue Tengda Wu
2026-11-19 12:09 ` Sebastian Andrzej Siewior
2026-11-20 12:14 ` Peter Zijlstra
2026-11-20 14:16 ` Sebastian Andrzej Siewior
2026-11-20 15:04 ` Peter Zijlstra
2026-11-20 15:10 ` Sebastian Andrzej Siewior
2026-11-20 15:24 ` Peter Zijlstra
2026-11-20 15:44 ` Peter Zijlstra
2026-11-20 15:49 ` Sebastian Andrzej Siewior
2026-11-20 16:01 ` Peter Zijlstra
2026-11-20 16:26 ` Peter Zijlstra
2026-11-20 17:28 ` Sebastian Andrzej Siewior
2026-11-20 17:49 ` Peter Zijlstra
2026-11-23 2:10 ` Tengda Wu
2026-11-23 1:17 ` Tengda Wu
2026-11-19 9:51 ` [PATCH v5 2/2] perf/core: Disable context fast-swap for sigtrap events Tengda Wu
2026-11-19 12:11 ` Sebastian Andrzej Siewior
2026-11-19 12:17 ` Tengda Wu
2026-11-19 13:07 ` Sebastian Andrzej Siewior
2026-11-19 13:19 ` Tengda Wu
2026-11-19 13:48 ` Sebastian Andrzej Siewior
2026-11-20 0:55 ` Tengda Wu
2026-11-20 16:30 ` Peter Zijlstra
2026-11-20 17:06 ` Sebastian Andrzej Siewior
2026-11-20 17:37 ` Peter Zijlstra
2026-11-20 17:46 ` Peter Zijlstra
2026-11-23 3:11 ` Tengda Wu [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5507e48c-4e98-4e76-8d3f-4630a2655085@huaweicloud.com \
--to=wutengda@huaweicloud.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=bigeasy@linutronix.de \
--cc=irogers@google.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®