Re: [PATCH v3 1/2] perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time

From: Ian Rogers

Date: Wed Dec 02 2026 - 13:24:47 EST


On Wed, Dec 2, 2026 at 6:26 AM Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
>
> I'm not sure I got the purpose of that flexible_inactive list; things
> seems to work fine by throwing everything on the flexible_active list.
>
> The actual active events are first on the list -- per construction, so
> the rotation stuff should 'just work'. And the frequency thing already
> filters on '== ACTIVE'. And I don't think there are other users of that
> list.

Putting them on flexible_active is much cleaner — it drops the extra
list from perf_event_pmu_context, removes the second loop in
__pmu_ctx_sched_out(), and turns perf_promote_sibling_to_leader() into a
one-line check.

One tiny ordering edge case to watch with sharing flexible_active: if an
ACTIVE flexible group leader is detached (DETACH_GROUP) while INACTIVE
events are already on flexible_active, perf_promote_sibling_to_leader()
using list_add_tail() would place the promoted ACTIVE sibling *after*
the INACTIVE entries, so ctx_event_to_rotate() would then rotate an
INACTIVE event instead of the ACTIVE sibling. Using list_add() for
ACTIVE (and list_add_tail() for INACTIVE) in
perf_promote_sibling_to_leader() preserves the active-before-inactive
ordering.

> All this does mean the 'enabled' time changes meaning, but I could not
> convince myself that matters for anybody.

Right — for filtered-out events (hybrid PMUs, task+CPU, cgroups),
enabled time now tracks time the event was actually eligible to run
rather than time while filtered out, which aligns with tools assuming
enabled > running implies multiplexing and scaling by enabled / running.

> Is the Fixes tag actually correct; did I really break it? The enabled
> time has always been about >STATE_OFF.

Good catch - you didn't break it and I agree we should drop the Fixes
tag. Enabled time has accrued whenever state > PERF_EVENT_STATE_OFF
since the original OFF/INACTIVE/ACTIVE state machine, so task+CPU and
cgroup filtered events behaved this way even before 0d3d73aac2ff or
PMU-level filtering for ARM big.LITTLE (66eb579e66ec) and Intel hybrid
(f83d2f91d259). Hybrid/big.LITTLE just made the problem much more
visible in practice because unpinned per-task core PMU events routinely
migrate across core types and get scaled as if multiplexed. Dropping the
Fixes tag also avoids pulling a change in total_time_enabled semantics
into stable kernels via autosel.

> > - For event_sched_in() / merge_sched_in(), do you prefer keeping
> > event_sched_in() reverting its speculative ACTIVE back to ON (with
> > merge_sched_in() doing ON -> {ERROR,INACTIVE}), or would you rather
> > see the state transition structured differently?
>
> I got rid of that speculative 'ON' thing, but I'm not convinced of the
> result, see below.

Replacing event_update_userpage()/group_update_userpage() in-place with
event_set_inactive()/group_set_inactive() looks much cleaner than the v4
detour. A few bugs I spotted in the draft patch above:

1. In group_sched_in()'s partial_tail loop:
- `if (partial_tail) perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);`
is missing `if (event->state <= PERF_EVENT_STATE_OFF) continue;`,
so it will transition disabled (OFF) siblings in the tail to
INACTIVE.
- For a *pinned* group that fails on a sibling or commit_txn(),
group_error: moves all siblings to PERF_EVENT_STATE_INACTIVE while
merge_sched_in() only sets the leader to ERROR, leaving the
siblings stuck in INACTIVE (so if the leader is later re-enabled on
a filtered CPU or detached, those siblings remain INACTIVE).
- Similarly, if __event_sched_out(group_event, ...) in group_error:
consumes pending_disable and moves the leader to OFF, siblings
already transitioned to INACTIVE earlier in the loop remain stuck
in INACTIVE when merge_sched_in() exits at `state <= OFF`.

Since merge_sched_in() already calls group_set_inactive(event) for
flexible groups (and sets ERROR for pinned groups), having
event_sched_in() and group_error: simply roll back to ON (no
__event_sched_out() or partial_tail) avoids all three cases and keeps
the single ON -> {ERROR, INACTIVE} transition in merge_sched_in().

2. In __perf_event_enable(), without __perf_effective_state(), enabling
a disabled sibling while its group leader is INACTIVE needs to match
the leader's state:

if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) {
if (leader->state == PERF_EVENT_STATE_INACTIVE)
event_set_inactive(event); /* or set_state + userpage */
return;
}

Otherwise the newly enabled sibling stays ON and accrues 0 enabled
time while the leader is INACTIVE (until the next rotation, or
indefinitely if counters are held by pinned events).

Happy to fold those into your version and re-test (including hybrid +
pinned-blocked/rollback tests) if this direction looks good to you.

Thanks,
Ian