All of lore.kernel.org
 help / color / mirror / Atom feed
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, 25 Nov 2026 13:37:12 +0100	[thread overview]
Message-ID: <20261125123712.GP1813469@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <80d983c5713537f5743b69e9b35ced1177e4732e.1795589380.git.irogers@google.com>

On Tue, Nov 24, 2026 at 10:52:28PM -0800, Ian Rogers wrote:
>  include/linux/perf_event.h | 60 ++++++++++++++++----------
>  kernel/events/core.c       | 86 +++++++++++++++++++++++++++-----------
>  2 files changed, 101 insertions(+), 45 deletions(-)
> 
> diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
> index e1e11500df09..715f564a3f96 100644
> --- a/include/linux/perf_event.h
> +++ b/include/linux/perf_event.h
> @@ -639,28 +639,44 @@ struct perf_addr_filter_range {
>  /*
>   * The normal states are:
>   *

> + *                         OFF <----disable()-------.
> + *                          |         ^             |
> + *    .-----------------> enable()    |             |
> + *    |                     |  .------'             |
> + *    |                     v /                     |
> + *  ERROR      .---------> ON <-----------.         |
> + *    ^        |            |             |         |
> + *    |      filter <-- sched_in()  sched_out()     |
> + *    |                     |             |         |
> + *    +-(lack of counters)--+             |         |
> + *    v                     v             |         |
> + *  INACTIVE              ACTIVE          |         |
> + *    |                     |             |         |
> + *    '---------------------+-------------+---------'
>   *
>   * That is:
>   *
> + * sched_in:       ON                   -> {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.
> + *
> + * Additional special-case transitions not shown above:
> + *  - enable() on a sibling of an INACTIVE group leader moves directly from ON
> + *    to INACTIVE when the context is active and filters match;
> + *  - 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:

Compressed that diagram a litte, and removed spurious arrows from it.
Eg. ERROR->enable() is not correct since enable() is not a state, it is
the label.

I also removed the ON->ON transition, as that seemed superfluous.

--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -639,24 +639,25 @@ struct perf_addr_filter_range {
 /*
  * The normal states are:
  *
- *                         OFF <----disable()-------.
- *                          |         ^             |
- *    .-----------------> enable()    |             |
- *    |                     |  .------'             |
- *    |                     v /                     |
- *  ERROR      .---------> ON <-----------.         |
- *    ^        |            |             |         |
- *    |      filter <-- sched_in()  sched_out()     |
- *    |                     |             |         |
- *    +-(lack of counters)--+             |         |
- *    v                     v             |         |
- *  INACTIVE              ACTIVE          |         |
- *    |                     |             |         |
- *    '---------------------+-------------+---------'
+ *                 OFF <----disable()---.
+ *                  |         |         |
+ *    .----------enable()     |         |
+ *    |             |  .------'         |
+ *    |             v /                 |
+ *  ERROR           ON <----------.     |
+ *    ^             |             |     |
+ *    |         sched_in()  sched_out() |
+ *    |             |             |     |
+ *    +-(no space)--+             |     |
+ *    |             |             |     |
+ *    v             v             |     |
+ *  INACTIVE      ACTIVE          |     |
+ *    |             |             |     |
+ *    '-------------+-------------+-----'
  *
  * That is:
  *
- * sched_in:       ON                   -> {ON,ERROR,INACTIVE,ACTIVE}
+ * sched_in:       ON                   -> {ERROR,INACTIVE,ACTIVE}
  * sched_out:      {INACTIVE,ACTIVE}    -> ON
  * disable:        {ON,INACTIVE,ACTIVE} -> OFF
  * enable:         {OFF,ERROR}          -> ON
@@ -665,16 +666,19 @@ struct perf_addr_filter_range {
  * 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.
+ * 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.
  *
  * Additional special-case transitions not shown above:
+ *
  *  - enable() on a sibling of an INACTIVE group leader moves directly from ON
  *    to INACTIVE when the context is active and filters match;
+ *
  *  - 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.
  *


> @@ -2490,6 +2492,17 @@ event_sched_out(struct perf_event *event, struct perf_event_context *ctx)
>  	 */
>  	list_del_init(&event->active_list);
>  
> +	if (event->state == PERF_EVENT_STATE_INACTIVE) {
> +		if (event->pending_disable) {
> +			event->pending_disable = 0;
> +			perf_cgroup_event_disable(event, ctx);
> +			state = PERF_EVENT_STATE_OFF;
> +		}
> +		perf_event_set_state(event, state);
> +		perf_event_update_userpage(event);
> +		return;
> +	}
> +
>  	perf_pmu_disable(event->pmu);
>  
>  	event->pmu->del(event, 0);

This duplicates a bit of logic, which is sad, but I don't immediately
see a sane way around it either. That is, you're wanting to avoid that
perf_pmu_{dis,en}able() here.


> @@ -2859,7 +2872,7 @@ event_sched_in(struct perf_event *event, struct perf_event_context *ctx)
>  	perf_log_itrace_start(event);
>  
>  	if (event->pmu->add(event, PERF_EF_START)) {
> -		perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);
> +		perf_event_set_state(event, PERF_EVENT_STATE_ON);
>  		event->oncpu = -1;
>  		ret = -EAGAIN;
>  		goto out;

> @@ -4134,6 +4154,21 @@ struct merge_sched_data {
>  	enum event_type_t event_type;
>  };
>  
> +static void group_set_inactive(struct perf_event *group_event)
> +{
> +	struct perf_event *sibling;
> +
> +	perf_event_set_state(group_event, PERF_EVENT_STATE_INACTIVE);
> +
> +	for_each_sibling_event(sibling, group_event) {
> +		if (sibling->state == PERF_EVENT_STATE_ON)
> +			perf_event_set_state(sibling, PERF_EVENT_STATE_INACTIVE);
> +	}
> +
> +	list_add_tail(&group_event->active_list,
> +		      &group_event->pmu_ctx->flexible_inactive);
> +}
> +
>  static int merge_sched_in(struct perf_event *event, void *data)
>  {
>  	struct perf_event_context *ctx = event->ctx;
> @@ -4159,7 +4194,7 @@ static int merge_sched_in(struct perf_event *event, void *data)
>  			list_add_tail(&event->active_list, get_event_list(event));
>  	}
>  
> -	if (event->state == PERF_EVENT_STATE_INACTIVE) {
> +	if (event->state == PERF_EVENT_STATE_ON) {
>  		msd->can_add_hw = 0;
>  		if (event->attr.pinned) {
>  			perf_cgroup_event_disable(event, ctx);
> @@ -4173,6 +4208,7 @@ static int merge_sched_in(struct perf_event *event, void *data)
>  		} else {
>  			struct perf_cpu_pmu_context *cpc = this_cpc(event->pmu_ctx->pmu);
>  
> +			group_set_inactive(event);
>  			event->pmu_ctx->rotate_necessary = 1;
>  			perf_mux_hrtimer_restart(cpc);
>  			group_update_userpage(event);

This is all very confusing. The state diagram says that you go to either
INACTIVE or ERROR on no moar counters, but then the event_sched_in()
failure goes to ON only to then later be 'corrected' to INACTIVE.

That makes no sense.

  parent reply	other threads:[~2026-11-25 12:37 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  6:52 [PATCH v3 1/2] perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time 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-11-25  7:09 ` [PATCH v3 1/2] perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time sashiko-bot
2026-11-25 12:37 ` Peter Zijlstra [this message]
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

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=20261125123712.GP1813469@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.