From: Peter Zijlstra <peterz@infradead.org>
To: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>,
Adrian Hunter <adrian.hunter@intel.com>,
James Clark <james.clark@linaro.org>,
Andi Kleen <ak@linux.intel.com>,
Dapeng Mi <dapeng1.mi@linux.intel.com>,
Kan Liang <kan.liang@linux.intel.com>,
Thomas Falcon <thomas.falcon@intel.com>,
Stephane Eranian <eranian@google.com>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 1/2] perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time
Date: Wed, 2 Dec 2026 15:26:39 +0100 [thread overview]
Message-ID: <20261202142639.GE83911@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <20261202101737.GF2883950@noisy.programming.kicks-ass.net>
On Wed, Dec 02, 2026 at 11:17:37AM +0100, Peter Zijlstra wrote:
> > Before I send a cleaner v5, I'd appreciate a quick sanity check on two
> > things:
> > - Is the `__perf_effective_state()` + `perf_event_set_state()` direction
> > for INACTIVE siblings what you had in mind (with the group_sched_out /
> > userpage weirdness cleaned up)?
Yeah, that turned out to be worse than the earlier version :-( In all
the various forms that I tried we need to iterate the siblings one way
or the other, so we might as well also change their state.
> > - 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.
> Hold off on the v5. I can't seem to make up my mind on what I want here.
> I'll poke at the thing a little.
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.
Anyway; the below seems to survive: perf test "Event times"
45: Event times :
45.1: Basic event times : Ok
45.2: Task and CPU filtered event times : Ok
45.3: Hybrid PMU filtered event times : Skip (not hybrid or permissions)
45.4: Multiplexed and group filtered event times : Ok
=== Test Summary ===
Passed main tests : 0
Passed subtests : 3
Skipped tests : 1
Failed tests : 0
---
All this does mean the 'enabled' time changes meaning, but I could not
convince myself that matters for anybody.
Is the Fixes tag actually correct; did I really break it? The enabled
time has always been about >STATE_OFF.
---
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -639,28 +639,45 @@ struct perf_addr_filter_range {
/*
* The normal states are:
*
- * ACTIVE --.
- * ^ |
- * | |
- * sched_{in,out}() |
- * | |
- * v |
- * ,---> INACTIVE --+ <-.
- * | | |
- * | {dis,en}able()
- * sched_in() | |
- * | OFF <--' --+
- * | |
- * `---> ERROR ------'
+ * OFF <----disable()---.
+ * | | |
+ * .----------enable() | |
+ * | | .------' |
+ * | v / |
+ * ERROR ON <----------. |
+ * ^ | | |
+ * | sched_in() sched_out() |
+ * | | | |
+ * +-(no space)--+ | |
+ * | | | |
+ * v v | |
+ * INACTIVE ACTIVE | |
+ * | | | |
+ * '-------------+-------------+-----'
*
* That is:
*
- * sched_in: INACTIVE -> {ACTIVE,ERROR}
- * sched_out: ACTIVE -> INACTIVE
- * disable: {ACTIVE,INACTIVE} -> OFF
- * enable: {OFF,ERROR} -> INACTIVE
+ * sched_in: ON -> {ERROR,INACTIVE,ACTIVE}
+ * sched_out: {INACTIVE,ACTIVE} -> ON
+ * disable: {ON,INACTIVE,ACTIVE} -> OFF
+ * enable: {OFF,ERROR} -> ON
+ *
+ * Where {ON,INACTIVE,ACTIVE} are enabled states and {OFF,ERROR} are disabled
+ * states.
+ *
+ * ON differs from INACTIVE as enabled time only increases for INACTIVE and
+ * ACTIVE events, running time only increases for ACTIVE events. A filter,
+ * such as the task, cgroup, CPU, or PMU an event should be scheduled on, can
+ * cause an event during sched_in to remain in the ON state, while lack of
+ * counters or exclusivity constraints move pinned events to ERROR and
+ * flexible events to INACTIVE.
*
- * Where {OFF,ERROR} are disabled states.
+ * Additional special-case transitions not shown above:
+ *
+ * - sched_out() with pending_disable moves {INACTIVE,ACTIVE} directly to OFF;
+ *
+ * - detaching a group sibling that requires a group leader (such as
+ * aux_output or PERF_EV_CAP_SIBLING) disables it into ERROR.
*
* Then we have the {EXIT,REVOKED,DEAD} states which are various shades of
* defunct events:
@@ -682,9 +699,10 @@ enum perf_event_state {
PERF_EVENT_STATE_REVOKED = -4, /* pmu gone, must not touch */
PERF_EVENT_STATE_EXIT = -3, /* task died, still inherit */
PERF_EVENT_STATE_ERROR = -2, /* scheduling error, can enable */
- PERF_EVENT_STATE_OFF = -1,
- PERF_EVENT_STATE_INACTIVE = 0,
- PERF_EVENT_STATE_ACTIVE = 1,
+ PERF_EVENT_STATE_OFF = -1, /* regular disabled event */
+ PERF_EVENT_STATE_ON = 0, /* enabled but not scheduled */
+ PERF_EVENT_STATE_INACTIVE = 1, /* not scheduled due to lack of counters */
+ PERF_EVENT_STATE_ACTIVE = 2, /* scheduled and counting */
};
struct file;
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -2031,7 +2031,7 @@ list_add_event(struct perf_event *event,
static inline void perf_event__state_init(struct perf_event *event)
{
event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
- PERF_EVENT_STATE_INACTIVE;
+ PERF_EVENT_STATE_ON;
}
static int __perf_event_read_size(u64 read_format, int nr_siblings)
@@ -2380,7 +2380,7 @@ static void perf_promote_sibling_to_lead
if (sibling->attach_state & PERF_ATTACH_CONTEXT) {
add_event_to_groups(sibling, ctx);
- if (sibling->state == PERF_EVENT_STATE_ACTIVE)
+ if (sibling->state >= PERF_EVENT_STATE_INACTIVE)
list_add_tail(&sibling->active_list, get_event_list(sibling));
}
@@ -2477,18 +2477,18 @@ static inline bool is_event_in_freq_mode
}
static void
-event_sched_out(struct perf_event *event, struct perf_event_context *ctx)
+__event_sched_out(struct perf_event *event, struct perf_event_context *ctx, enum perf_event_state state)
{
struct perf_event_pmu_context *epc = event->pmu_ctx;
struct perf_cpu_pmu_context *cpc = this_cpc(epc->pmu);
- enum perf_event_state state = PERF_EVENT_STATE_INACTIVE;
+ bool active = (event->state == PERF_EVENT_STATE_ACTIVE);
// XXX cpc serialization, probably per-cpu IRQ disabled
WARN_ON_ONCE(event->ctx != ctx);
lockdep_assert_held(&ctx->lock);
- if (event->state != PERF_EVENT_STATE_ACTIVE)
+ if (event->state < PERF_EVENT_STATE_INACTIVE)
return;
/*
@@ -2498,10 +2498,11 @@ event_sched_out(struct perf_event *event
*/
list_del_init(&event->active_list);
- perf_pmu_disable(event->pmu);
-
- event->pmu->del(event, 0);
- event->oncpu = -1;
+ if (active) {
+ perf_pmu_disable(event->pmu);
+ event->pmu->del(event, 0);
+ event->oncpu = -1;
+ }
if (event->pending_disable) {
event->pending_disable = 0;
@@ -2511,16 +2512,29 @@ event_sched_out(struct perf_event *event
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 (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);
+ } else {
+ /*
+ * active events get this through ->del().
+ */
+ perf_event_update_userpage(event);
}
- if (event->attr.exclusive || !cpc->active_oncpu)
- cpc->exclusive = 0;
+}
- perf_pmu_enable(event->pmu);
+static void
+event_sched_out(struct perf_event *event, struct perf_event_context *ctx)
+{
+ return __event_sched_out(event, ctx, PERF_EVENT_STATE_ON);
}
static void
@@ -2528,7 +2542,7 @@ group_sched_out(struct perf_event *group
{
struct perf_event *event;
- if (group_event->state != PERF_EVENT_STATE_ACTIVE)
+ if (group_event->state < PERF_EVENT_STATE_INACTIVE)
return;
perf_assert_pmu_disabled(group_event->pmu_ctx->pmu);
@@ -2710,7 +2724,7 @@ static void __perf_event_disable(struct
struct perf_event_context *ctx,
void *info)
{
- if (event->state < PERF_EVENT_STATE_INACTIVE)
+ if (event->state < PERF_EVENT_STATE_ON)
return;
perf_pmu_disable(event->pmu_ctx->pmu);
@@ -2724,8 +2738,7 @@ static void __perf_event_disable(struct
group_sched_out(event, ctx);
/*
- * But only mark the leader OFF; the siblings will remain
- * INACTIVE.
+ * But only mark the leader OFF; the siblings will remain ON.
*/
__event_disable(event, ctx, PERF_EVENT_STATE_OFF);
@@ -2893,8 +2906,9 @@ group_sched_in(struct perf_event *group_
{
struct perf_event *event, *partial_group = NULL;
struct pmu *pmu = group_event->pmu_ctx->pmu;
+ bool partial_tail = false;
- if (group_event->state == PERF_EVENT_STATE_OFF)
+ if (group_event->state <= PERF_EVENT_STATE_OFF)
return 0;
pmu->start_txn(pmu, PERF_PMU_TXN_ADD);
@@ -2923,11 +2937,16 @@ group_sched_in(struct perf_event *group_
*/
for_each_sibling_event(event, group_event) {
if (event == partial_group)
- break;
+ partial_tail = true;
- event_sched_out(event, ctx);
+ if (partial_tail) {
+ perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);
+ continue;
+ }
+
+ __event_sched_out(event, ctx, PERF_EVENT_STATE_INACTIVE);
}
- event_sched_out(group_event, ctx);
+ __event_sched_out(group_event, ctx, PERF_EVENT_STATE_INACTIVE);
error:
pmu->cancel_txn(pmu);
@@ -3269,13 +3288,13 @@ static void __perf_event_enable(struct p
struct perf_event *leader = event->group_leader;
struct perf_event_context *task_ctx;
- if (event->state >= PERF_EVENT_STATE_INACTIVE ||
+ if (event->state >= PERF_EVENT_STATE_ON ||
event->state <= PERF_EVENT_STATE_ERROR)
return;
ctx_time_freeze(cpuctx, ctx);
- perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);
+ perf_event_set_state(event, PERF_EVENT_STATE_ON);
perf_cgroup_event_enable(event, ctx);
if (!ctx->is_active)
@@ -3312,7 +3331,7 @@ static void _perf_event_enable(struct pe
struct perf_event_context *ctx = event->ctx;
raw_spin_lock_irq(&ctx->lock);
- if (event->state >= PERF_EVENT_STATE_INACTIVE ||
+ if (event->state >= PERF_EVENT_STATE_ON ||
event->state < PERF_EVENT_STATE_ERROR) {
out:
raw_spin_unlock_irq(&ctx->lock);
@@ -4115,26 +4134,27 @@ static noinline int visit_groups_merge(s
*
* IOW, we must not miss EVENT_TIME edges.
*/
-static inline bool event_update_userpage(struct perf_event *event)
+static inline void event_set_inactive(struct perf_event *event)
{
+ perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);
+
if (likely(!refcount_read(&event->mmap_count)))
- return false;
+ return;
- perf_event_update_time(event);
perf_event_update_userpage(event);
-
- return true;
}
-static inline void group_update_userpage(struct perf_event *group_event)
+static inline void group_set_inactive(struct perf_event *group_event)
{
struct perf_event *event;
- if (!event_update_userpage(group_event))
- return;
+ event_set_inactive(group_event);
+ for_each_sibling_event(event, group_event) {
+ if (event->state <= PERF_EVENT_STATE_OFF)
+ continue;
- for_each_sibling_event(event, group_event)
- event_update_userpage(event);
+ event_set_inactive(event);
+ }
}
struct merge_sched_data {
@@ -4162,29 +4182,38 @@ static int merge_sched_in(struct perf_ev
!(msd->event_type & EVENT_GUEST))
return 0;
- if (group_can_go_on(event, msd->can_add_hw)) {
- if (!group_sched_in(event, ctx))
- list_add_tail(&event->active_list, get_event_list(event));
- }
-
- if (event->state == PERF_EVENT_STATE_INACTIVE) {
- msd->can_add_hw = 0;
- if (event->attr.pinned) {
- perf_cgroup_event_disable(event, ctx);
- perf_event_set_state(event, PERF_EVENT_STATE_ERROR);
+ if (group_can_go_on(event, msd->can_add_hw) &&
+ !group_sched_in(event, ctx)) {
+ list_add_tail(&event->active_list, get_event_list(event));
+ return 0;
+ }
- if (*perf_event_fasync(event))
- event->pending_kill = POLL_ERR;
+ /*
+ * Partial group rollback in group_sched_in() may have consumed
+ * pending_disable and moved the event to OFF.
+ */
+ if (event->state <= PERF_EVENT_STATE_OFF)
+ return 0;
- event->pending_wakeup = 1;
- irq_work_queue(&event->pending_irq);
- } else {
- struct perf_cpu_pmu_context *cpc = this_cpc(event->pmu_ctx->pmu);
+ msd->can_add_hw = 0;
+ if (event->attr.pinned) {
+ perf_cgroup_event_disable(event, ctx);
+ perf_event_set_state(event, PERF_EVENT_STATE_ERROR);
- event->pmu_ctx->rotate_necessary = 1;
- perf_mux_hrtimer_restart(cpc);
- group_update_userpage(event);
- }
+ if (*perf_event_fasync(event))
+ event->pending_kill = POLL_ERR;
+
+ event->pending_wakeup = 1;
+ irq_work_queue(&event->pending_irq);
+ } 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);
}
return 0;
@@ -4710,10 +4739,10 @@ static int event_enable_on_exec(struct p
return 0;
event->attr.enable_on_exec = 0;
- if (event->state >= PERF_EVENT_STATE_INACTIVE)
+ if (event->state >= PERF_EVENT_STATE_ON)
return 0;
- perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);
+ perf_event_set_state(event, PERF_EVENT_STATE_ON);
return 1;
}
@@ -5040,13 +5069,13 @@ static int perf_event_read(struct perf_e
preempt_enable();
ret = data.ret;
- } else if (state == PERF_EVENT_STATE_INACTIVE) {
+ } else if (state >= PERF_EVENT_STATE_ON) {
struct perf_event_context *ctx = event->ctx;
unsigned long flags;
raw_spin_lock_irqsave(&ctx->lock, flags);
- state = event->state;
- if (state != PERF_EVENT_STATE_INACTIVE) {
+ if (event->state != state) {
+ state = event->state;
raw_spin_unlock_irqrestore(&ctx->lock, flags);
goto again;
}
@@ -9169,7 +9198,7 @@ perf_iterate_ctx(struct perf_event_conte
list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
if (!all) {
- if (event->state < PERF_EVENT_STATE_INACTIVE)
+ if (event->state < PERF_EVENT_STATE_ON)
continue;
if (!event_filter_match(event))
continue;
@@ -9193,7 +9222,7 @@ static void perf_iterate_sb_cpu(perf_ite
if (!smp_load_acquire(&event->ctx))
continue;
- if (event->state < PERF_EVENT_STATE_INACTIVE)
+ if (event->state < PERF_EVENT_STATE_ON)
continue;
if (!event_filter_match(event))
continue;
@@ -13582,7 +13611,7 @@ perf_event_alloc(struct perf_event_attr
event->ns = get_pid_ns(task_active_pid_ns(current));
event->id = atomic64_inc_return(&perf_event_id);
- event->state = PERF_EVENT_STATE_INACTIVE;
+ event->state = PERF_EVENT_STATE_ON;
if (parent_event)
event->event_caps = parent_event->event_caps;
@@ -14628,7 +14657,7 @@ static void __perf_pmu_install_event(str
event->pmu_ctx = epc;
if (event->state >= PERF_EVENT_STATE_OFF)
- event->state = PERF_EVENT_STATE_INACTIVE;
+ event->state = PERF_EVENT_STATE_ON;
perf_install_in_context(ctx, event, cpu);
/*
@@ -15066,8 +15095,8 @@ inherit_event(struct perf_event *parent_
* not its attr.disabled bit. We hold the parent's mutex,
* so we won't race with perf_event_{en, dis}able_family.
*/
- if (parent_state >= PERF_EVENT_STATE_INACTIVE)
- child_event->state = PERF_EVENT_STATE_INACTIVE;
+ if (parent_state >= PERF_EVENT_STATE_ON)
+ child_event->state = PERF_EVENT_STATE_ON;
else
child_event->state = PERF_EVENT_STATE_OFF;
prev parent reply other threads:[~2026-12-02 14:26 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 6:52 Ian Rogers
2026-11-25 6:52 ` [PATCH v3 2/2] perf test: Add filtered event enabled time tests to event-times Ian Rogers
2026-12-02 14:13 ` Peter Zijlstra
2026-11-25 12:37 ` [PATCH v3 1/2] perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time Peter Zijlstra
2026-11-25 13:02 ` Peter Zijlstra
2026-11-30 18:42 ` Ian Rogers
2026-12-01 9:18 ` Peter Zijlstra
2026-12-01 16:47 ` Ian Rogers
2026-12-02 10:17 ` Peter Zijlstra
2026-12-02 14:26 ` Peter Zijlstra [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=20261202142639.GE83911@noisy.programming.kicks-ass.net \
--to=peterz@infradead.org \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=ak@linux.intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=dapeng1.mi@linux.intel.com \
--cc=eranian@google.com \
--cc=irogers@google.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=kan.liang@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@kernel.org \
--cc=namhyung@kernel.org \
--cc=thomas.falcon@intel.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
all inboxes | Powered by JetHome®