From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=90.155.92.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796221607; b=tdBcXW2jtwyXAVkxXQs4/BtuQOf7fWp4fzuJZfwetmtSpTblU1nsolLSiEBZjdghSevx ukP5BeQGKCe/pnByU3Hw++7TBOnVIuI6do1KSGTVX3YbdZvyEUSxEmaaiCb6JbqSWPTDB 5mq2rDL2/mqRU2S9/v2sUblsJdkCdbBk0QSTWDeScU0uqbMM/rMLXQt3/CZVb0H474LFN TzYfI15n0aPY69UBkEWTu87gZ2Y3LCHA0YjULEhcudi+W/tPlQu07ZKoQps0xdu/5WzIA L+0f0NaeyE8+Ns+S6tT7E/tcbu9cf9g8zPdVqThW+UuHGAvdJrCHIe+PuWt85b3ZYNA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796221607; h=DMARC-Filter:DKIM-Signature:Received:Received:Date:From:To:Cc: Subject:Message-ID:References:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; bh=fUGbmwAg4qwJL5lHQClzg6lZnxdIUU4uyQiHd8hfE/w=; b=HBpbCD00Re4B/2PONUnk+D6cQVeOXB35yIOV/PVFnGoeMlYZmRT/jMeE+iQPwsCZ8gEW 9o+j7xMQVJQ7cjfiXavExiYr8f3fIxcKzbrNKFD0cyBFkiMUaDk+C6OZ28db6vaIMDfWU mt8ALph75b4Ba+9Za6lz2ful/FPyR3EMECKvASmLBm8LZ3lw3ZC1bX5XcB9+nrRXlGnS1 Ka+hNgoF9qe3K9Abcg4UJwdV0BA105g/Uh9ctv1wO22/K0ZsQBM91pRe46SqZOHxTd2ne 1LurY69KQnmeE/2QEd9iTOgxJAArmGBv604mTRimX7SJpbEhlsj7jtrnY+iw1b9DAkQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=infradead.org; spf=pass smtp.mailfrom=infradead.org; dkim=pass header.d=infradead.org header.i=@infradead.org header.a=rsa-sha256 header.s=desiato.20200630 header.b=iVWgUA2F; arc=none smtp.remote-ip=90.155.92.199 Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0CA9C606A322; Wed, 2 Dec 2026 14:26:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0CA9C606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.a=rsa-sha256 header.s=desiato.20200630 header.b=iVWgUA2F DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=fUGbmwAg4qwJL5lHQClzg6lZnxdIUU4uyQiHd8hfE/w=; b=iVWgUA2FxRU3Q20pem/JSoFEER lufSziiYTZdmex3Ap8OC88mWeFmhe8xBLTqq9rDCHfDcV0KIxCiJmaaeg7lMYPPPux+konzUMO0R8 /OUiVWxRBUDixHH1v8ZWKOnlrVMiOR5V6SRFCPfeVJbNyC5gToCB/hBj0xnnxhY+sGjijewShX/yS /2R2iRWvPoDc5bmeF+dpSnmDpDlCilB0NSfR2z4qCgm3WSE2gKYSSP9dzbeD8CDKNVpoVQ9bGcMky V0sF1qmvMS5teFiN3ATLHR/2r6a4YN9f+1cufQWVV65jxhArm1SluXEX/i9O0d2gnhgH4t6PnTJeb 6m1d21zQ==; Received: from 77-249-17-252.cable.dynamic.v4.ziggo.nl ([77.249.17.252] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYlI8-0000000CN9F-3ZVZ; Wed, 02 Dec 2026 14:26:41 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 1000) id BC18C30063F; Wed, 02 Dec 2026 15:26:39 +0100 (CET) Date: Wed, 2 Dec 2026 15:26:39 +0100 From: Peter Zijlstra To: Ian Rogers Cc: Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , James Clark , Andi Kleen , Dapeng Mi , Kan Liang , Thomas Falcon , Stephane Eranian , 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 Message-ID: <20261202142639.GE83911@noisy.programming.kicks-ass.net> References: <80d983c5713537f5743b69e9b35ced1177e4732e.1795589380.git.irogers@google.com> <20261125123712.GP1813469@noisy.programming.kicks-ass.net> <20261125130233.GO2669272@noisy.programming.kicks-ass.net> <20261201091858.GE2883950@noisy.programming.kicks-ass.net> <20261202101737.GF2883950@noisy.programming.kicks-ass.net> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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;