From: sashiko-bot@kernel.org
To: "Tengda Wu" <wutengda@huaweicloud.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v5 2/2] perf/core: Disable context fast-swap for sigtrap events
Date: Thu, 19 Nov 2026 10:03:53 +0000 [thread overview]
Message-ID: <sashiko-outbox-222905@kernel.org> (raw)
In-Reply-To: <55b1a2845b0ad34a874a7653644e34af9eb068d3.1795053619.git.wutengda@huaweicloud.com>
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] perf/core: non-SMP-atomic local_t race on
ctx->nr_no_switch_fast
--
commit 5e829db733e2fe49cdac7cb99bb9428db1c83359
Author: Tengda Wu <wutengda@huaweicloud.com>
perf/core: Disable context fast-swap for sigtrap events
This commit excludes sigtrap events from the context fast-swap path by
incrementing and decrementing ctx->nr_no_switch_fast when adding and
removing events with attr.sigtrap set.
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index da86bc6e8be94..483ba8704d418 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -1999,7 +1999,7 @@ list_add_event(struct perf_event *event, struct perf_event_context *ctx)
> ctx->nr_user++;
> if (event->attr.inherit_stat)
> ctx->nr_stat++;
> - if (has_inherit_and_sample_read(&event->attr))
> + if (has_inherit_and_sample_read(&event->attr) || event->attr.sigtrap)
> local_inc(&ctx->nr_no_switch_fast);
>
> if (event->state > PERF_EVENT_STATE_OFF)
> @@ -2225,7 +2225,7 @@ list_del_event(struct perf_event *event, struct perf_event_context *ctx)
> ctx->nr_user--;
> if (event->attr.inherit_stat)
> ctx->nr_stat--;
> - if (has_inherit_and_sample_read(&event->attr))
> + if (has_inherit_and_sample_read(&event->attr) || event->attr.sigtrap)
> local_dec(&ctx->nr_no_switch_fast);
[Severity: Medium]
Can concurrent local_inc() and local_dec() calls on ctx->nr_no_switch_fast
from different CPUs race and lose updates?
ctx->nr_no_switch_fast is a local_t, so local_inc() and local_dec() are
only atomic on a single CPU (for example, unlocked inc/dec on x86). When
installing a disabled sigtrap event into a non-empty task context,
perf_install_in_context() runs add_event_to_ctx() directly on the
installer CPU under ctx->lock without an IPI, while an already-active
sigtrap event in that context can update ctx->nr_no_switch_fast on the
target task's CPU without holding ctx->lock:
CPU0 CPU1 (running ctx->task)
perf_install_in_context()
raw_spin_lock(&ctx->lock)
add_event_to_ctx()
list_add_event()
local_inc(&ctx->nr_no_switch_fast)
__perf_event_overflow()
local_inc(&ctx->nr_no_switch_fast)
<-- non-SMP-atomic RMW race
...
perf_pending_task()
local_dec(&ctx->nr_no_switch_fast)
A similar cross-CPU race is possible between list_del_event() in
perf_remove_from_context() (when !ctx->is_active) and perf_pending_task().
A lost increment allows ctx->nr_no_switch_fast to drop to 0 while a
sigtrap event or pending task_work is still present, re-enabling the
context fast-swap path and hitting WARN_ON_ONCE(event->ctx->task !=
current) in perf_sigtrap().
--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795053619.git.wutengda@huaweicloud.com?part=2
prev parent reply other threads:[~2026-11-19 10:03 UTC|newest]
Thread overview: 5+ 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 9:59 ` sashiko-bot
2026-11-19 9:51 ` [PATCH v5 2/2] perf/core: Disable context fast-swap for sigtrap events Tengda Wu
2026-11-19 10:03 ` sashiko-bot [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=sashiko-outbox-222905@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=wutengda@huaweicloud.com \
/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