BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ian Rogers" <irogers@google.com>
Cc: linux-perf-users@vger.kernel.org, bpf@vger.kernel.org
Subject: Re: [PATCH v5 1/2] perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time
Date: Thu, 03 Dec 2026 19:28:54 +0000	[thread overview]
Message-ID: <sashiko-outbox-248507@kernel.org> (raw)
In-Reply-To: <6e0c4ab555fda856605bd818854a6ef8ced4fcbd.1796323635.git.irogers@google.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] perf/core: O(N) INACTIVE scan in
  perf_adjust_freq_unthr_events()
- [Medium] perf/core: unguarded perf_event_update_userpage() in
  event_sched_out()
- [Medium] perf/core: NMI-unsafe perf_event_update_userpage() in
  __perf_event_enable()
--

commit dd98e51c34a35ea87d43dcd67b2004fe170ac7ce
Author: Ian Rogers <irogers@google.com>

perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time

This commit introduces PERF_EVENT_STATE_ON for enabled events that are
filtered out during sched_in, reserving PERF_EVENT_STATE_INACTIVE for
unscheduled flexible groups that could not fit on the PMU so enabled time
only accrues in INACTIVE and ACTIVE states.

> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index 614f9f0e65005..ba04f8d878267 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c

[ ... ]

> @@ -2505,16 +2509,23 @@ event_sched_out(struct perf_event *event, struct perf_event_context *ctx)
>  
>  	perf_event_set_state(event, state);
>  
> -	if (!is_software_event(event))
> -		cpc->active_oncpu--;
> -	if (is_event_in_freq_mode(event)) {
> -		ctx->nr_freq--;
> -		epc->nr_freq--;
> -	}
> -	if (event->attr.exclusive || !cpc->active_oncpu)
> -		cpc->exclusive = 0;
> +	if (active) {
> +		if (!is_software_event(event))
> +			cpc->active_oncpu--;
> +		if (is_event_in_freq_mode(event)) {
> +			ctx->nr_freq--;
> +			epc->nr_freq--;
> +		}
> +		if (event->attr.exclusive || !cpc->active_oncpu)
> +			cpc->exclusive = 0;
>  
> -	perf_pmu_enable(event->pmu);
> +		perf_pmu_enable(event->pmu);
> +	} else {
> +		/*
> +		 * active events get this through ->del().
> +		 */
> +		perf_event_update_userpage(event);
> +	}
>  }

[Severity: Medium]
Should this perf_event_update_userpage() call be guarded by
refcount_read(&event->mmap_count) like event_set_inactive()?

perf_event_update_userpage() only checks event->rb, not mmap_count.  For
multiplexed events redirected via PERF_EVENT_IOC_SET_OUTPUT (such as in
perf record), mmap_count is 0 while event->rb is non-NULL, so every
INACTIVE leader and enabled sibling runs perf_clock(),
arch_perf_update_userpage(), and seqlock writes under ctx->lock on every
rotation or context switch, overwriting the ring buffer owner's
rb->user_page.

Unmmapped events also pay an out-of-line RCU call and event->rb load per
inactive event, and directly mmapped events update the userpage twice in
perf_rotate_context() (here on INACTIVE -> ON, then again in
group_set_inactive()).

[ ... ]

> @@ -3280,10 +3290,17 @@ static void __perf_event_enable(struct perf_event *event,
>  
>  	/*
>  	 * If the event is in a group and isn't the group leader,
> -	 * then don't put it on unless the group is on.
> -	 */
> -	if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
> +	 * then don't put it on unless the group is on. If the leader
> +	 * is INACTIVE (waiting for counters), match its state so the
> +	 * sibling accrues enabled time with the group.
> +	 */
> +	if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) {
> +		if (leader->state == PERF_EVENT_STATE_INACTIVE) {
> +			perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);
> +			perf_event_update_userpage(event);
> +		}
>  		return;
> +	}

[Severity: Medium]
Can perf_event_update_userpage() here nest with an NMI on a shared
rb->user_page?

event_function() holds ctx->lock with local IRQs disabled, but leaves the
PMU enabled.  If an active sampling event on the same CPU shares event->rb
via PERF_EVENT_IOC_SET_OUTPUT and overflows during this call, the nested
perf_event_update_userpage() breaks the non-nestable userpg->lock seqlock:

__perf_event_enable(sibling)
  perf_event_update_userpage(sibling)
    ++userpg->lock                       (odd)
    <NMI>
      x86_pmu_handle_irq()
        x86_perf_event_set_period(active_ev)
          perf_event_update_userpage(active_ev)
            ++userpg->lock               <-- even while writing
            ...
            ++userpg->lock               (odd)
    write time_enabled/time_running      <-- mixes sibling & active_ev
    ++userpg->lock                       (even, torn fields published)

Also, without the mmap_count check from event_set_inactive(), a redirected
sibling unconditionally overwrites the active event's user_page.  Wrapping
this in perf_pmu_disable()/perf_pmu_enable() and calling
event_set_inactive(event) avoids both.

[ ... ]

> @@ -4156,29 +4174,38 @@ static int merge_sched_in(struct perf_event *event, void *data)

[ ... ]

> +	} else {
> +		struct perf_cpu_pmu_context *cpc = this_cpc(event->pmu_ctx->pmu);
> +
> +		event->pmu_ctx->rotate_necessary = 1;
> +		perf_mux_hrtimer_restart(cpc);
> +
> +		group_set_inactive(event);
> +		list_add_tail(&event->active_list,
> +			      &event->pmu_ctx->flexible_active);
>  	}

[Severity: Medium]
Does appending INACTIVE groups to flexible_active turn
perf_adjust_freq_unthr_events() into an O(N) scan over all multiplexed
groups on every timer tick?

perf_event_task_tick() -> perf_adjust_freq_unthr_context() calls
perf_adjust_freq_unthr_events(&pmu_ctx->flexible_active) in hardirq
context under ctx->lock with the PMU disabled, and its loop continues past
non-ACTIVE entries instead of stopping at the INACTIVE tail:

kernel/events/core.c:perf_adjust_freq_unthr_events() {
    ...
	list_for_each_entry(event, event_list, active_list) {
		if (event->state != PERF_EVENT_STATE_ACTIVE)
			continue;
    ...
}

In addition, perf_pmu_ctx_is_active() checks
!list_empty(&epc->flexible_active), so a pmu_ctx with only INACTIVE groups
and no ACTIVE events now invokes perf_pmu_disable()/perf_pmu_enable() and
scans the INACTIVE list on unthrottle ticks.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/6e0c4ab555fda856605bd818854a6ef8ced4fcbd.1796323635.git.irogers@google.com?part=1

      parent reply	other threads:[~2026-12-03 19:28 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 18:53 [PATCH v5 1/2] perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time Ian Rogers
2026-12-03 18:53 ` [PATCH v5 2/2] perf test: Add filtered event enabled time tests to event-times Ian Rogers
2026-12-03 19:17   ` sashiko-bot
2026-12-03 19:28 ` 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-248507@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=irogers@google.com \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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