Linux Perf Users
 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 v6 1/2] perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time
Date: Fri, 04 Dec 2026 07:00:49 +0000	[thread overview]
Message-ID: <sashiko-outbox-249279@kernel.org> (raw)
In-Reply-To: <ed76be2c2d97d398770d742df8028634925da3e4.1796365469.git.irogers@google.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] perf/core: O(N) INACTIVE scan per tick in
  perf_adjust_freq_unthr_context()
- [Medium] perf/core: O(N*S) sched mutations and double userpage updates
  on rotation
--

commit 7f7c05c1a50554767d36de028fe90262549caf18
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 and reserves PERF_EVENT_STATE_INACTIVE for
flexible groups that cannot be scheduled due to counter or exclusivity
constraints, linking them onto epc->flexible_active after the active groups.

> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index 614f9f0e65005..fc312d3e309fd 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 if (refcount_read(&event->mmap_count)) {
> +		/*
> +		 * active events get this through ->del().
> +		 */
> +		perf_event_update_userpage(event);
> +	}
>  }

[Severity: Medium]
Does transitioning every inactive flexible group and its siblings through
event_sched_out() and group_set_inactive() regress multiplexing rotation
latency and double userpage updates?

In perf_rotate_context() (every 4ms in hardirq context), __pmu_ctx_sched_out()
now walks all groups on flexible_active, including unscheduled inactive groups
and all enabled siblings, moving them to PERF_EVENT_STATE_ON and updating
mmapped userpages here.

__pmu_ctx_sched_in() -> merge_sched_in() then calls group_set_inactive() on
those same unscheduled groups and siblings, mutating them back to
PERF_EVENT_STATE_INACTIVE and updating mmapped userpages a second time under
ctx->lock with IRQs and the PMU disabled, even though neither the CPU, task,
cgroup, nor EVENT_TIME changed between the two calls.

[ ... ]

> @@ -4156,29 +4178,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 unscheduled inactive groups to pmu_ctx->flexible_active
degrade perf_adjust_freq_unthr_context() from scanning only active counters
to scanning all multiplexed flexible groups on every scheduler tick?

On every tick with a frequency-mode event or unthrottle pending,
perf_adjust_freq_unthr_context() holds ctx->lock with IRQs and the PMU
disabled and walks flexible_active in perf_adjust_freq_unthr_events(), which
continues past non-active entries rather than stopping at the first inactive
group.

In addition, perf_pmu_ctx_is_active() checks !list_empty() on
flexible_active, so a pmu_ctx with only inactive groups triggers unnecessary
perf_pmu_disable()/perf_pmu_enable() cycles and a full list scan on
unthrottle ticks.

Since active groups precede inactive ones on flexible_active, breaking on the
first non-active entry in perf_adjust_freq_unthr_events() and checking the
head entry state in perf_pmu_ctx_is_active() would avoid this.

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

      parent reply	other threads:[~2026-12-04  7:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  6:27 [PATCH v6 1/2] perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time Ian Rogers
2026-12-04  6:27 ` [PATCH v6 2/2] perf test: Add filtered event enabled time tests to event-times Ian Rogers
2026-12-04  7:00 ` [PATCH v6 1/2] perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time sashiko-bot
2026-12-04  7:00 ` 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-249279@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