All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Tengda Wu" <wutengda@huaweicloud.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v4 2/2] perf/core: Disable context fast-swap for sigtrap events
Date: Tue, 17 Nov 2026 11:53:21 +0000	[thread overview]
Message-ID: <sashiko-outbox-218736@kernel.org> (raw)
In-Reply-To: <cff9b939758dad9f16bb4eb0483c63b7787e62bb.1794914794.git.wutengda@huaweicloud.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] perf/core: list_del_event() drops nr_no_switch_fast before
  perf_pending_task()
--

commit a5820a5e4b514a31e08f42157caca3b1bf8abed1
Author: Tengda Wu <wutengda@huaweicloud.com>

perf/core: Disable context fast-swap for sigtrap events

This commit disables the context switch fast-swap path for contexts containing
sigtrap events by accounting attr->sigtrap in nr_no_switch_fast during
list_add_event() and list_del_event() instead of __perf_event_overflow() and
perf_pending_task().

> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index da86bc6e8be94..9fc8db14c6b83 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);
>  
>  	list_del_rcu(&event->event_entry);

[ ... ]

> @@ -7758,7 +7763,6 @@ static void perf_pending_task(struct callback_head *head)
>  
>  	if (smp_load_acquire(&event->pending_work)) {
>  		perf_sigtrap(event);
> -		local_dec(&event->ctx->nr_no_switch_fast);
>  		/*
>  		 * Ensure the pending work is fully handled before allowing
>  		 * another overflow to queue it. See __perf_event_overflow().
> @@ -10935,7 +10939,6 @@ static int __perf_event_overflow(struct perf_event *event,
>  		 */
>  		if (cmpxchg_acquire(&event->pending_work, 0, pending_id) == 0) {
>  			if (!task_work_add(current, &event->pending_task, notify_mode)) {
> -				local_inc(&event->ctx->nr_no_switch_fast);
>  				WARN_ON_ONCE(!atomic_long_inc_not_zero(&event->refcount));
>  
>  				event->pending_addr = 0;

[Severity: Medium]
Can ctx->nr_no_switch_fast drop to zero while event->pending_task is still
queued if the sigtrap event is detached before perf_pending_task() runs?

Detaching an event does not cancel or flush event->pending_task; instead,
pending_task holds a reference to the event until the task returns to user
space.  When a parent event with attr.inherit and attr.sigtrap is closed while
a child event has pending_task queued, perf_event_release_kernel() detaches the
child events from their cloned contexts without uncloning them:

CPU0 (task A)                     CPU1
__perf_event_overflow(child_A)
  task_work_add(A, &child_A->pending_task)
                                  perf_event_release_kernel(parent)
                                    perf_remove_from_context(child_A, ...)
                                      list_del_event(child_A, ctx_A)
                                        local_dec(&ctx_A->nr_no_switch_fast)
                                    perf_remove_from_context(child_B, ...)
                                      list_del_event(child_B, ctx_B)
                                        local_dec(&ctx_B->nr_no_switch_fast)
schedule() (A -> B)
  perf_event_context_sched_out(A, B)
    /* context_equiv(ctx_A, ctx_B) && nr_no_switch_fast == 0 */
    WRITE_ONCE(ctx_A->task, B)
...
perf_pending_task(&child_A->pending_task)
  perf_sigtrap(child_A)
    WARN_ON_ONCE(event->ctx->task != current)  <-- trips WARN, drops SIGTRAP

Keeping the increment in __perf_event_overflow() and decrement in
perf_pending_task() alongside list_add_event() and list_del_event() would keep
nr_no_switch_fast elevated until pending_task completes.  Note that because
list_add_event() and list_del_event() can run on a remote CPU under ctx->lock
while __perf_event_overflow() or perf_pending_task() runs on ctx->task's CPU
without ctx->lock, nr_no_switch_fast would also need to be atomic_t rather than
CPU-local local_t.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1794914794.git.wutengda@huaweicloud.com?part=2

  reply	other threads:[~2026-11-17 11:53 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 11:28 [PATCH v4 0/2] perf/core: Fix sigtrap task_work races Tengda Wu
2026-11-17 11:28 ` [PATCH v4 1/2] perf/core: Prevent duplicate sigtrap task work enqueue Tengda Wu
2026-11-17 11:47   ` sashiko-bot
2026-11-17 11:28 ` [PATCH v4 2/2] perf/core: Disable context fast-swap for sigtrap events Tengda Wu
2026-11-17 11:53   ` sashiko-bot [this message]
2026-11-18  2:52     ` Tengda Wu

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-218736@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.