* [PATCH v3 1/2] perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time
@ 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
` (2 more replies)
0 siblings, 3 replies; 11+ messages in thread
From: Ian Rogers @ 2026-11-25 6:52 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim
Cc: Mark Rutland, Alexander Shishkin, Jiri Olsa, Adrian Hunter,
James Clark, Andi Kleen, Dapeng Mi, Kan Liang, Thomas Falcon,
Stephane Eranian, Ian Rogers, linux-perf-users, linux-kernel
Perf events that are filtered out during sched_in (via pmu->filter() on
hybrid/heterogeneous systems, or event_filter_match() for task+CPU and
cgroup events) remain unscheduled while the context is active. Because
enabled events previously rested in PERF_EVENT_STATE_INACTIVE, context
time accrued into total_time_enabled even when the event could not be
scheduled on the current CPU or PMU, while total_time_running did not
increase. Tools assume total_time_enabled > total_time_running implies
counter multiplexing and scale counts by enabled / running, producing
severely inaccurate counts:
https://lore.kernel.org/lkml/20250627192417.1157736-13-irogers@google.com/
For example:
- A task+CPU event opened on 1 CPU of a 128-CPU system accrues enabled
time whenever the task runs on the other 127 CPUs, potentially scaling
the count 128x.
- On hybrid systems, unpinned task events accrue enabled time while the
task runs on a different core type, causing bogus multiplexing scaling
(such as the 28.05% / 96.79% scaling on cpu_atom and cpu_core seen in
commit d9db9c8db56c ("perf test: Fix perf stat --bpf-counters on
hybrid machines"), which caused count mismatches until
commit b02027776ac5 ("perf tests: Fix flakiness in BPF counters test
on hybrid systems") pinned the workload to a single CPU) and
preventing perf stat from accurately merging common hybrid events
across core types.
Introduce PERF_EVENT_STATE_ON (0) for events that are enabled and not
scheduled, reserving PERF_EVENT_STATE_INACTIVE (1) for flexible events
that matched filters during merge_sched_in() but failed to go onto the
PMU due to insufficient counters or exclusivity constraints:
- total_time_enabled only accrues in PERF_EVENT_STATE_INACTIVE and
PERF_EVENT_STATE_ACTIVE; filtered-out events stay in
PERF_EVENT_STATE_ON and accrue neither enabled nor running time.
- Track INACTIVE flexible group leaders on epc->flexible_inactive (via
event->active_list) so __pmu_ctx_sched_out() transitions them and
their siblings back to PERF_EVENT_STATE_ON without touching filtered
ON events, preserving the context-switch scalability of state-based
timekeeping.
- Handle PERF_EVENT_STATE_INACTIVE in event_sched_out() without calling
pmu->del() or decrementing active_oncpu, and serialize perf_event_read()
on ctx->lock for both ON and INACTIVE events.
Fixes: 0d3d73aac2ff ("perf/core: Rewrite event timekeeping")
Signed-off-by: Ian Rogers <irogers@google.com>
---
Resending as a non-RFC v2 following Namhyung Kim's agreement on adding a
separate state for filtered events and Ingo Molnar's suggestion to
re-send:
https://lore.kernel.org/lkml/CAP-5=fV5gtSQOPkOLaAauBcwjgs1ohADs5O3GF+bJOx2tSynwQ@mail.gmail.com/
https://lore.kernel.org/lkml/aS3WXn4nz9GE87bM@gmail.com/
v2: Make the untested RFC v1 functional, tested, and handle all state
transitions:
https://lore.kernel.org/lkml/20250716223924.825772-1-irogers@google.com/
- Add epc->flexible_inactive (and handle sibling promotion on group
detach) so __pmu_ctx_sched_out() resets INACTIVE flexible groups
back to PERF_EVENT_STATE_ON on context switch / rotation.
- Transition flexible groups that fail to schedule in merge_sched_in()
(including after can_add_hw == 0 or partial group rollback) to
PERF_EVENT_STATE_INACTIVE, while pinned groups still enter
PERF_EVENT_STATE_ERROR.
- Handle PERF_EVENT_STATE_INACTIVE in event_sched_out() without
calling pmu->del() or decrementing active_oncpu/nr_freq, and update
the userpage.
- Drop premature PERF_EVENT_STATE_ACTIVE in __perf_event_enable(),
match INACTIVE leader state when enabling a sibling, serialize
perf_event_read() on ctx->lock for both ON and INACTIVE, and drop
invalid ERROR -> OFF disable() edge from the state diagram comment.
---
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:
*
- * ACTIVE --.
- * ^ |
- * | |
- * sched_{in,out}() |
- * | |
- * v |
- * ,---> INACTIVE --+ <-.
- * | | |
- * | {dis,en}able()
- * sched_in() | |
- * | OFF <--' --+
- * | |
- * `---> ERROR ------'
+ * OFF <----disable()-------.
+ * | ^ |
+ * .-----------------> enable() | |
+ * | | .------' |
+ * | v / |
+ * ERROR .---------> ON <-----------. |
+ * ^ | | | |
+ * | filter <-- sched_in() sched_out() |
+ * | | | |
+ * +-(lack of counters)--+ | |
+ * 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 -> {ON,ERROR,INACTIVE,ACTIVE}
+ * sched_out: {INACTIVE,ACTIVE} -> ON
+ * disable: {ON,INACTIVE,ACTIVE} -> OFF
+ * enable: {OFF,ERROR} -> ON
*
- * Where {OFF,ERROR} are disabled states.
+ * 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:
@@ -682,9 +698,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;
@@ -969,6 +986,7 @@ struct perf_event_pmu_context {
struct list_head pinned_active;
struct list_head flexible_active;
+ struct list_head flexible_inactive;
/* Used to identify the per-cpu perf_event_pmu_context */
unsigned int embedded : 1;
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 366fca000ff0..cf76e9290cc5 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -2023,7 +2023,7 @@ list_add_event(struct perf_event *event, struct perf_event_context *ctx)
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)
@@ -2374,6 +2374,8 @@ static void perf_promote_sibling_to_leader(struct perf_event *sibling,
if (sibling->state == PERF_EVENT_STATE_ACTIVE)
list_add_tail(&sibling->active_list, get_event_list(sibling));
+ else if (sibling->state == PERF_EVENT_STATE_INACTIVE)
+ list_add_tail(&sibling->active_list, &sibling->pmu_ctx->flexible_inactive);
}
perf_event__header_size(sibling);
@@ -2473,14 +2475,14 @@ event_sched_out(struct perf_event *event, struct perf_event_context *ctx)
{
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;
+ enum perf_event_state state = PERF_EVENT_STATE_ON;
// 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;
/*
@@ -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);
@@ -2520,7 +2533,7 @@ group_sched_out(struct perf_event *group_event, struct perf_event_context *ctx)
{
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);
@@ -2702,7 +2715,7 @@ static void __perf_event_disable(struct perf_event *event,
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);
@@ -2717,7 +2730,7 @@ static void __perf_event_disable(struct perf_event *event,
/*
* But only mark the leader OFF; the siblings will remain
- * INACTIVE.
+ * ON.
*/
__event_disable(event, ctx, PERF_EVENT_STATE_OFF);
@@ -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;
@@ -2886,7 +2899,7 @@ group_sched_in(struct perf_event *group_event, struct perf_event_context *ctx)
struct perf_event *event, *partial_group = NULL;
struct pmu *pmu = group_event->pmu_ctx->pmu;
- 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);
@@ -3261,13 +3274,13 @@ static void __perf_event_enable(struct perf_event *event,
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)
@@ -3280,8 +3293,11 @@ static void __perf_event_enable(struct perf_event *event,
* If the event is in a group and isn't the group leader,
* then don't put it on unless the group is on.
*/
- if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
+ if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) {
+ if (leader->state == PERF_EVENT_STATE_INACTIVE)
+ perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);
return;
+ }
task_ctx = cpuctx->task_ctx;
if (ctx->task)
@@ -3304,7 +3320,7 @@ static void _perf_event_enable(struct perf_event *event)
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);
@@ -3578,6 +3594,10 @@ static void __pmu_ctx_sched_out(struct perf_event_pmu_context *pmu_ctx,
&pmu_ctx->flexible_active,
active_list)
group_sched_out(event, ctx);
+ list_for_each_entry_safe(event, tmp,
+ &pmu_ctx->flexible_inactive,
+ active_list)
+ group_sched_out(event, ctx);
/*
* Since we cleared EVENT_FLEXIBLE, also clear
* rotate_necessary, is will be reset by
@@ -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);
@@ -4702,10 +4738,10 @@ static int event_enable_on_exec(struct perf_event *event,
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;
}
@@ -5032,13 +5068,13 @@ static int perf_event_read(struct perf_event *event, bool group)
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;
}
@@ -5079,6 +5115,7 @@ __perf_init_event_pmu_context(struct perf_event_pmu_context *epc, struct pmu *pm
INIT_LIST_HEAD(&epc->pmu_ctx_entry);
INIT_LIST_HEAD(&epc->pinned_active);
INIT_LIST_HEAD(&epc->flexible_active);
+ INIT_LIST_HEAD(&epc->flexible_inactive);
atomic_set(&epc->refcount, 1);
}
@@ -5314,6 +5351,7 @@ static void put_pmu_ctx(struct perf_event_pmu_context *epc)
WARN_ON_ONCE(!list_empty(&epc->pinned_active));
WARN_ON_ONCE(!list_empty(&epc->flexible_active));
+ WARN_ON_ONCE(!list_empty(&epc->flexible_inactive));
raw_spin_unlock_irqrestore(&ctx->lock, flags);
@@ -9155,7 +9193,7 @@ perf_iterate_ctx(struct perf_event_context *ctx,
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;
@@ -9179,7 +9217,7 @@ static void perf_iterate_sb_cpu(perf_iterate_f output, void *data)
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;
@@ -13554,7 +13592,7 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
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;
@@ -14600,7 +14638,7 @@ static void __perf_pmu_install_event(struct pmu *pmu,
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);
/*
@@ -15038,8 +15076,8 @@ inherit_event(struct perf_event *parent_event,
* 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;
--
2.56.1.831.g720f3f5937-goog
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH v3 2/2] perf test: Add filtered event enabled time tests to event-times 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 ` Ian Rogers 2026-12-02 14:13 ` Peter Zijlstra 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 2 siblings, 1 reply; 11+ messages in thread From: Ian Rogers @ 2026-11-25 6:52 UTC (permalink / raw) To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim Cc: Mark Rutland, Alexander Shishkin, Jiri Olsa, Adrian Hunter, James Clark, Andi Kleen, Dapeng Mi, Kan Liang, Thomas Falcon, Stephane Eranian, Ian Rogers, linux-perf-users, linux-kernel Extend the "Event times" test suite (tools/perf/tests/event-times.c) with subtests verifying that filtered-out events stay in PERF_EVENT_STATE_ON and do not accrue bogus total_time_enabled: - Task and CPU filtered event times: open a cpu-clock event targeting both the current thread and one CPU, verifying zero enabled/running time when enabled on a non-matching CPU (including the mmap userpage), matching enabled and running times on the target CPU, zero time accrued after migrating away, and matching times across fork() inheritance. - Hybrid PMU filtered event times: on systems with multiple core PMUs (including >2 core types), iterate all core PMUs and verify per-task core PMU events accrue zero enabled time while running on CPUs belonging to any other core PMU. - Multiplexed and group filtered event times: iterate core PMUs and verify flexible events and groups in PERF_EVENT_STATE_INACTIVE (including siblings promoted to group leader on leader close) transition back to PERF_EVENT_STATE_ON on context switch and accrue zero enabled time after migrating to a filtered-out CPU. Reuse struct affinity from tools/perf/util/affinity.{c,h}, hardening affinity__setup() to retry on EINVAL for large kernel cpumasks, adding affinity__has_cpu(), and returning an error status from affinity__set(). Signed-off-by: Ian Rogers <irogers@google.com> --- v3: - Try PERF_TYPE_HARDWARE with PERF_COUNT_HW_CPU_CYCLES first on non-hybrid systems (perf_pmus__num_core_pmus() <= 1) in open_core_pmu_event() before falling back to raw PMU configs, as spotted by Sashiko on v2: https://lore.kernel.org/linux-perf-users/c41f98313862f6545863598dcde871cfb80aba59.1795582691.git.irogers@google.com/ - Loop over candidate configs in open_core_pmu_event() and iterate all core PMUs via perf_pmus__scan_core() in the hybrid and multiplexing subtests to support hybrid machines with >2 CPU types. --- tools/perf/tests/event-times.c | 550 ++++++++++++++++++++++++++++++++- tools/perf/util/affinity.c | 100 ++++-- tools/perf/util/affinity.h | 5 +- 3 files changed, 620 insertions(+), 35 deletions(-) diff --git a/tools/perf/tests/event-times.c b/tools/perf/tests/event-times.c index 56dd37ca760e..92246c110767 100644 --- a/tools/perf/tests/event-times.c +++ b/tools/perf/tests/event-times.c @@ -1,18 +1,31 @@ // SPDX-License-Identifier: GPL-2.0 +#include <linux/bitmap.h> #include <linux/compiler.h> +#include <linux/perf_event.h> #include <linux/string.h> #include <errno.h> #include <inttypes.h> +#include <stdlib.h> #include <string.h> +#include <sys/ioctl.h> +#include <sys/mman.h> #include <sys/wait.h> +#include <time.h> +#include <unistd.h> +#include <internal/lib.h> #include <perf/cpumap.h> +#include "cloexec.h" #include "tests.h" #include "evlist.h" #include "evsel.h" #include "debug.h" #include "parse-events.h" +#include "pmu.h" +#include "pmus.h" #include "thread_map.h" #include "target.h" +#include "util/affinity.h" +#include "../perf-sys.h" static int attach__enable_on_exec(struct evlist *evlist) { @@ -239,4 +252,539 @@ static int test__event_times(struct test_suite *test __maybe_unused, int subtest return ret; } -DEFINE_SUITE("Event times", event_times); +static int find_two_allowed_cpus(const struct affinity *aff, int *cpu0, int *cpu1) +{ + unsigned long c0, c1; + + c0 = find_first_bit(aff->orig_cpus, aff->cpu_set_bits); + if (c0 >= aff->cpu_set_bits) + return -1; + + c1 = find_next_bit(aff->orig_cpus, aff->cpu_set_bits, c0 + 1); + if (c1 >= aff->cpu_set_bits) + return -1; + + *cpu0 = (int)c0; + *cpu1 = (int)c1; + return 0; +} + +static int first_allowed_pmu_cpu(const struct affinity *aff, + const struct perf_pmu *pmu) +{ + struct perf_cpu cpu; + unsigned int idx; + + if (!pmu || !pmu->cpus || perf_cpu_map__is_empty(pmu->cpus)) { + unsigned long c = find_first_bit(aff->orig_cpus, aff->cpu_set_bits); + + return c < aff->cpu_set_bits ? (int)c : -1; + } + + perf_cpu_map__for_each_cpu(cpu, idx, pmu->cpus) { + if (affinity__has_cpu(aff, cpu.cpu)) + return cpu.cpu; + } + return -1; +} + +static uint64_t mono_ns(void) +{ + struct timespec ts; + + clock_gettime(CLOCK_MONOTONIC, &ts); + return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec; +} + +static void burn_user_ms(unsigned int ms) +{ + uint64_t end = mono_ns() + (uint64_t)ms * 1000000ULL; + + while (mono_ns() < end) { + for (int i = 0; i < 10000; i++) + asm volatile("" : "+r"(i) :: "memory"); + } +} + +struct read_times { + uint64_t val; + uint64_t ena; + uint64_t run; +}; + +static int read_times(int fd, struct read_times *rt) +{ + return read(fd, rt, sizeof(*rt)) == (ssize_t)sizeof(*rt) ? 0 : -1; +} + +static int open_core_pmu_event(const struct perf_pmu *pmu, pid_t pid, int cpu, + int group_fd, bool disabled) +{ + struct { + __u32 type; + __u64 config; + } candidates[3]; + unsigned long flags = perf_event_open_cloexec_flag(); + int nr_candidates = 0; + + if (perf_pmus__num_core_pmus() <= 1) { + candidates[nr_candidates].type = PERF_TYPE_HARDWARE; + candidates[nr_candidates].config = PERF_COUNT_HW_CPU_CYCLES; + nr_candidates++; + } else if (perf_pmus__supports_extended_type()) { + candidates[nr_candidates].type = PERF_TYPE_HARDWARE; + candidates[nr_candidates].config = + ((__u64)pmu->type << PERF_PMU_TYPE_SHIFT) | + PERF_COUNT_HW_CPU_CYCLES; + nr_candidates++; + } + candidates[nr_candidates].type = pmu->type; + candidates[nr_candidates].config = 0x3c; + nr_candidates++; + candidates[nr_candidates].type = pmu->type; + candidates[nr_candidates].config = 0; + nr_candidates++; + + for (int i = 0; i < nr_candidates; i++) { + struct perf_event_attr attr = { + .type = candidates[i].type, + .size = sizeof(attr), + .config = candidates[i].config, + .read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | + PERF_FORMAT_TOTAL_TIME_RUNNING, + .disabled = disabled ? 1 : 0, + .exclude_kernel = 1, + .exclude_hv = 1, + }; + int fd = sys_perf_event_open(&attr, pid, cpu, group_fd, flags); + + if (fd >= 0) + return fd; + } + return -1; +} + +/* + * Verify that an event opened on both a task and a specific CPU remains in + * PERF_EVENT_STATE_ON (accruing neither enabled nor running time) when enabled + * or scheduled on other CPUs, including across fork() inheritance. + */ +static int test__event_times_task_and_cpu(struct test_suite *test __maybe_unused, + int subtest __maybe_unused) +{ + struct perf_event_attr attr = { + .type = PERF_TYPE_SOFTWARE, + .size = sizeof(attr), + .config = PERF_COUNT_SW_CPU_CLOCK, + .read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | + PERF_FORMAT_TOTAL_TIME_RUNNING, + .disabled = 1, + .inherit = 1, + .exclude_kernel = 1, + .exclude_hv = 1, + }; + struct perf_event_mmap_page *up = MAP_FAILED; + struct read_times rt0, rt_before, rt_after, rt_child; + struct affinity aff = {}; + int cpu0, cpu1, fd = -1, status = 0; + int ret = TEST_FAIL; + pid_t pid; + + if (affinity__setup(&aff) < 0) + return TEST_SKIP; + + if (find_two_allowed_cpus(&aff, &cpu0, &cpu1) < 0 || + affinity__set(&aff, cpu1) < 0) { + ret = TEST_SKIP; + goto out; + } + + fd = sys_perf_event_open(&attr, 0, cpu0, -1, perf_event_open_cloexec_flag()); + if (fd < 0) { + ret = (errno == EACCES || errno == EPERM) ? TEST_SKIP : TEST_FAIL; + goto out; + } + + up = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0); + + /* Enable while running on non-matching cpu1: must stay in ON state. */ + if (ioctl(fd, PERF_EVENT_IOC_ENABLE, 0) < 0) + goto out; + + burn_user_ms(25); + if (read_times(fd, &rt0) < 0) + goto out; + if (rt0.ena != 0 || rt0.run != 0) { + pr_debug("FAILED: filtered CPU %d: ena=%" PRIu64 " run=%" PRIu64 "\n", + cpu1, rt0.ena, rt0.run); + goto out; + } + if (up != MAP_FAILED && (up->time_enabled != 0 || up->time_running != 0)) { + pr_debug("FAILED: userpage accrued time while ON: ena=%llu run=%llu\n", + (unsigned long long)up->time_enabled, + (unsigned long long)up->time_running); + goto out; + } + + /* Run on matching cpu0: enabled and running time must match. */ + if (affinity__set(&aff, cpu0) < 0) + goto out; + burn_user_ms(25); + if (read_times(fd, &rt0) < 0 || rt0.run == 0 || rt0.ena != rt0.run) { + pr_debug("FAILED: mismatch on matching CPU %d: ena=%" PRIu64 " run=%" PRIu64 "\n", + cpu0, rt0.ena, rt0.run); + goto out; + } + + /* Migrate back to cpu1: neither enabled nor running time may advance. */ + if (affinity__set(&aff, cpu1) < 0 || read_times(fd, &rt_before) < 0) + goto out; + burn_user_ms(25); + if (read_times(fd, &rt_after) < 0) + goto out; + if (rt_after.ena != rt_before.ena || rt_after.run != rt_before.run || + rt_after.ena != rt_after.run) { + pr_debug("FAILED: CPU %d: %" PRIu64 "/%" PRIu64 " -> %" PRIu64 "/%" PRIu64 "\n", + cpu1, rt_before.ena, rt_before.run, rt_after.ena, rt_after.run); + goto out; + } + + /* Verify inherited child event also stays in ON state on cpu1. */ + pid = fork(); + if (pid < 0) + goto out; + if (pid == 0) { + if (affinity__set(&aff, cpu0) < 0) + _exit(1); + burn_user_ms(20); + if (affinity__set(&aff, cpu1) < 0) + _exit(1); + burn_user_ms(20); + _exit(0); + } + if (waitpid(pid, &status, 0) != pid || !WIFEXITED(status) || WEXITSTATUS(status) != 0) + goto out; + + if (read_times(fd, &rt_child) < 0 || + rt_child.run <= rt_after.run || rt_child.ena != rt_child.run) { + pr_debug("FAILED: inherited task+CPU mismatch: ena=%" PRIu64 " run=%" PRIu64 "\n", + rt_child.ena, rt_child.run); + goto out; + } + + ret = TEST_OK; +out: + if (up != MAP_FAILED) + munmap(up, page_size); + if (fd >= 0) + close(fd); + affinity__cleanup(&aff); + return ret; +} + +/* + * On hybrid / heterogeneous systems (>1 core PMU, including >2 CPU types), + * iterate all core PMUs and verify per-task events on each core PMU do not + * accrue enabled time while the task executes on CPUs belonging to any other + * core PMU. + */ +static int test__event_times_hybrid(struct test_suite *test __maybe_unused, + int subtest __maybe_unused) +{ + struct hybrid_pmu_entry { + const struct perf_pmu *pmu; + int cpu; + int fd; + } *entries = NULL; + struct read_times *before = NULL, *after = NULL; + struct perf_pmu *pmu = NULL; + struct affinity aff = {}; + int max_pmus = perf_pmus__num_core_pmus(); + int nr_pmus = 0, checked_pairs = 0; + int ret = TEST_FAIL; + + if (max_pmus <= 1) + return TEST_SKIP; + + if (affinity__setup(&aff) < 0) + return TEST_SKIP; + + entries = calloc(max_pmus, sizeof(*entries)); + if (entries) { + for (int i = 0; i < max_pmus; i++) + entries[i].fd = -1; + } + before = calloc(max_pmus, sizeof(*before)); + after = calloc(max_pmus, sizeof(*after)); + if (!entries || !before || !after) + goto out; + + while ((pmu = perf_pmus__scan_core(pmu)) != NULL && nr_pmus < max_pmus) { + int c; + + if (!pmu->cpus || perf_cpu_map__is_empty(pmu->cpus)) + continue; + c = first_allowed_pmu_cpu(&aff, pmu); + if (c < 0) + continue; + entries[nr_pmus].pmu = pmu; + entries[nr_pmus].cpu = c; + nr_pmus++; + } + + if (nr_pmus < 2 || affinity__set(&aff, entries[0].cpu) < 0) { + ret = TEST_SKIP; + goto out; + } + + for (int i = 0; i < nr_pmus; i++) { + entries[i].fd = open_core_pmu_event(entries[i].pmu, 0, -1, -1, true); + if (entries[i].fd < 0) { + ret = TEST_SKIP; + goto out; + } + } + + for (int i = 0; i < nr_pmus; i++) { + if (ioctl(entries[i].fd, PERF_EVENT_IOC_ENABLE, 0) < 0) + goto out; + } + + for (int i = 0; i < nr_pmus; i++) { + struct perf_cpu pc = { .cpu = entries[i].cpu }; + + if (affinity__set(&aff, entries[i].cpu) < 0) + goto out; + + for (int j = 0; j < nr_pmus; j++) { + if (read_times(entries[j].fd, &before[j]) < 0) + goto out; + } + + if (i == 0) { + for (int j = 1; j < nr_pmus; j++) { + if (!perf_cpu_map__has(entries[j].pmu->cpus, pc) && + (before[j].ena != 0 || before[j].run != 0)) { + pr_debug("FAILED: %s accrued time before running\n", + entries[j].pmu->name); + goto out; + } + } + } + + burn_user_ms(25); + + for (int j = 0; j < nr_pmus; j++) { + if (read_times(entries[j].fd, &after[j]) < 0) + goto out; + } + + if (after[i].ena <= before[i].ena) { + pr_debug("FAILED: %s accrued 0 time on CPU %d\n", + entries[i].pmu->name, entries[i].cpu); + goto out; + } + + for (int j = 0; j < nr_pmus; j++) { + if (j == i || perf_cpu_map__has(entries[j].pmu->cpus, pc)) + continue; + checked_pairs++; + if (after[j].ena != before[j].ena || + after[j].run != before[j].run) { + pr_debug("FAILED: on %s CPU %d, %s ena %" PRIu64 "->%" PRIu64 "\n", + entries[i].pmu->name, entries[i].cpu, + entries[j].pmu->name, before[j].ena, after[j].ena); + goto out; + } + } + } + + ret = checked_pairs > 0 ? TEST_OK : TEST_SKIP; +out: + if (entries) { + for (int i = 0; i < nr_pmus; i++) { + if (entries[i].fd >= 0) + close(entries[i].fd); + } + free(entries); + } + free(before); + free(after); + affinity__cleanup(&aff); + return ret; +} + +static int test_one_pmu_mux_migration(const struct perf_pmu *pmu, + struct affinity *aff) +{ + enum { NR_FLEX = 12, NR_CHECK = 14 }; + struct read_times before[NR_CHECK], after[NR_CHECK]; + int flex_fds[NR_FLEX]; + int leader_fd = -1, sib_fd = -1, dis_sib_fd = -1; + int cpu0 = -1, cpu1 = -1, ev_cpu = -1; + unsigned long c; + int ret = TEST_FAIL; + + for (int i = 0; i < NR_FLEX; i++) + flex_fds[i] = -1; + + cpu0 = first_allowed_pmu_cpu(aff, pmu); + if (cpu0 < 0) + return TEST_SKIP; + + if (perf_pmus__num_core_pmus() > 1 && pmu->cpus && + !perf_cpu_map__is_empty(pmu->cpus)) { + for_each_set_bit(c, aff->orig_cpus, aff->cpu_set_bits) { + struct perf_cpu pc = { .cpu = (int)c }; + + if (!perf_cpu_map__has(pmu->cpus, pc)) { + cpu1 = (int)c; + break; + } + } + ev_cpu = -1; + } else { + for_each_set_bit(c, aff->orig_cpus, aff->cpu_set_bits) { + if ((int)c != cpu0) { + cpu1 = (int)c; + break; + } + } + ev_cpu = cpu0; + } + + if (cpu1 < 0 || affinity__set(aff, cpu0) < 0) + return TEST_SKIP; + + for (int i = 0; i < NR_FLEX; i++) { + flex_fds[i] = open_core_pmu_event(pmu, 0, ev_cpu, -1, false); + if (flex_fds[i] < 0) { + ret = TEST_SKIP; + goto out; + } + } + + leader_fd = open_core_pmu_event(pmu, 0, ev_cpu, -1, false); + if (leader_fd < 0) { + ret = TEST_SKIP; + goto out; + } + sib_fd = open_core_pmu_event(pmu, 0, ev_cpu, leader_fd, false); + dis_sib_fd = open_core_pmu_event(pmu, 0, ev_cpu, leader_fd, true); + if (sib_fd < 0 || dis_sib_fd < 0) { + ret = TEST_SKIP; + goto out; + } + + if (ioctl(dis_sib_fd, PERF_EVENT_IOC_ENABLE, 0) < 0) + goto out; + + burn_user_ms(30); + + /* Detach leader while group may be INACTIVE to exercise sibling promotion. */ + close(leader_fd); + leader_fd = -1; + burn_user_ms(10); + + if (affinity__set(aff, cpu1) < 0) + goto out; + + for (int i = 0; i < NR_FLEX; i++) { + if (read_times(flex_fds[i], &before[i]) < 0) + goto out; + } + if (read_times(sib_fd, &before[NR_FLEX]) < 0 || + read_times(dis_sib_fd, &before[NR_FLEX + 1]) < 0) + goto out; + + burn_user_ms(20); + + for (int i = 0; i < NR_FLEX; i++) { + if (read_times(flex_fds[i], &after[i]) < 0) + goto out; + } + if (read_times(sib_fd, &after[NR_FLEX]) < 0 || + read_times(dis_sib_fd, &after[NR_FLEX + 1]) < 0) + goto out; + + for (int i = 0; i < NR_CHECK; i++) { + if (before[i].ena == 0) { + pr_debug("FAILED: %s event %d accrued 0 enabled time on CPU %d\n", + pmu->name, i, cpu0); + goto out; + } + if (after[i].ena != before[i].ena || after[i].run != before[i].run) { + pr_debug("FAILED: %s event %d CPU %d: %" PRIu64 " -> %" PRIu64 "\n", + pmu->name, i, cpu1, before[i].ena, after[i].ena); + goto out; + } + } + + ret = TEST_OK; +out: + for (int i = 0; i < NR_FLEX; i++) { + if (flex_fds[i] >= 0) + close(flex_fds[i]); + } + if (leader_fd >= 0) + close(leader_fd); + if (sib_fd >= 0) + close(sib_fd); + if (dis_sib_fd >= 0) + close(dis_sib_fd); + return ret; +} + +/* + * Verify that flexible events and groups in PERF_EVENT_STATE_INACTIVE (on + * epc->flexible_inactive), including promoted group siblings after leader + * close, transition back to PERF_EVENT_STATE_ON on sched_out and do not accrue + * enabled time after migrating to a filtered-out CPU across all core PMUs. + */ +static int test__event_times_mux_migration(struct test_suite *test __maybe_unused, + int subtest __maybe_unused) +{ + struct perf_pmu *pmu = NULL; + struct affinity aff = {}; + int tested = 0; + int ret = TEST_SKIP; + + if (affinity__setup(&aff) < 0) + return TEST_SKIP; + + while ((pmu = perf_pmus__scan_core(pmu)) != NULL) { + int err = test_one_pmu_mux_migration(pmu, &aff); + + if (err == TEST_FAIL) { + ret = TEST_FAIL; + goto out; + } + if (err == TEST_OK) + tested++; + } + + if (tested > 0) + ret = TEST_OK; +out: + affinity__cleanup(&aff); + return ret; +} + +static struct test_case tests__event_times[] = { + TEST_CASE("Basic event times", event_times), + TEST_CASE_REASON("Task and CPU filtered event times", + event_times_task_and_cpu, + "permissions or <2 allowed CPUs"), + TEST_CASE_REASON("Hybrid PMU filtered event times", + event_times_hybrid, + "not hybrid or permissions"), + TEST_CASE_REASON("Multiplexed and group filtered event times", + event_times_mux_migration, + "no hardware PMU or <2 allowed CPUs"), + { .name = NULL, } +}; + +struct test_suite suite__event_times = { + .desc = "Event times", + .test_cases = tests__event_times, +}; diff --git a/tools/perf/util/affinity.c b/tools/perf/util/affinity.c index 6c64b5f69a4e..e610eb959347 100644 --- a/tools/perf/util/affinity.c +++ b/tools/perf/util/affinity.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 /* Manage affinity to optimize IPIs inside the kernel perf API. */ #define _GNU_SOURCE 1 +#include <errno.h> #include <sched.h> #include <stdlib.h> #include <linux/bitmap.h> @@ -10,73 +11,98 @@ #include "cpumap.h" #include "affinity.h" -static int get_cpu_set_size(void) +static size_t get_cpu_set_bits(void) { - int sz = cpu__max_cpu().cpu + 8 - 1; + int max_cpu = cpu__max_cpu().cpu; + size_t bits = max_cpu > 0 ? (size_t)max_cpu : 0; + /* * sched_getaffinity doesn't like masks smaller than the kernel. - * Hopefully that's big enough. + * Start at >= 4096 bits, aligned to BITS_PER_LONG so bitmap_zalloc() + * matches the exact byte size passed to sched_{get,set}affinity(). */ - if (sz < 4096) - sz = 4096; - return sz / 8; + if (bits < 4096) + bits = 4096; + return BITS_TO_LONGS(bits) * BITS_PER_LONG; } int affinity__setup(struct affinity *a) { - int cpu_set_size = get_cpu_set_size(); + size_t bits = get_cpu_set_bits(); - a->orig_cpus = bitmap_zalloc(cpu_set_size * 8); - if (!a->orig_cpus) - return -1; - sched_getaffinity(0, cpu_set_size, (cpu_set_t *)a->orig_cpus); - a->sched_cpus = bitmap_zalloc(cpu_set_size * 8); + *a = (struct affinity){}; + for (;;) { + unsigned long *orig = bitmap_zalloc(bits); + + if (!orig) + return -1; + if (sched_getaffinity(0, bits / 8, (cpu_set_t *)orig) == 0) { + a->orig_cpus = orig; + break; + } + zfree(&orig); + if (errno != EINVAL || bits >= (1U << 20)) + return -1; + bits <<= 1; + } + + a->sched_cpus = bitmap_zalloc(bits); if (!a->sched_cpus) { zfree(&a->orig_cpus); return -1; } - bitmap_zero((unsigned long *)a->sched_cpus, cpu_set_size); - a->changed = false; + a->cpu_set_bits = bits; return 0; } +bool affinity__has_cpu(const struct affinity *a, int cpu) +{ + return a && a->orig_cpus && cpu >= 0 && + (size_t)cpu < a->cpu_set_bits && test_bit(cpu, a->orig_cpus); +} + /* * perf_event_open does an IPI internally to the target CPU. * It is more efficient to change perf's affinity to the target * CPU and then set up all events on that CPU, so we amortize * CPU communication. */ -void affinity__set(struct affinity *a, int cpu) +int affinity__set(struct affinity *a, int cpu) { - int cpu_set_size = get_cpu_set_size(); + int err; /* * Return: - * - if cpu is -1 + * - if cpu is < 0 (e.g. -1) * - restrict out of bound access to sched_cpus */ - if (cpu == -1 || ((cpu >= (cpu_set_size * 8)))) - return; + if (!a || !a->sched_cpus || cpu < 0 || (size_t)cpu >= a->cpu_set_bits) + return -1; - a->changed = true; __set_bit(cpu, a->sched_cpus); /* - * We ignore errors because affinity is just an optimization. - * This could happen for example with isolated CPUs or cpusets. - * In this case the IPIs inside the kernel's perf API still work. + * Callers such as evlist ignore errors because affinity is just an + * optimization (for example with isolated CPUs or cpusets, where IPIs + * inside the kernel's perf API still work), while tests may check the + * return value. */ - sched_setaffinity(0, cpu_set_size, (cpu_set_t *)a->sched_cpus); + err = sched_setaffinity(0, a->cpu_set_bits / 8, (cpu_set_t *)a->sched_cpus); __clear_bit(cpu, a->sched_cpus); + if (err) + return -1; + + a->changed = true; + return 0; } static void __affinity__cleanup(struct affinity *a) { - int cpu_set_size = get_cpu_set_size(); - - if (a->changed) - sched_setaffinity(0, cpu_set_size, (cpu_set_t *)a->orig_cpus); + if (a->changed && a->orig_cpus) + sched_setaffinity(0, a->cpu_set_bits / 8, (cpu_set_t *)a->orig_cpus); zfree(&a->sched_cpus); zfree(&a->orig_cpus); + a->cpu_set_bits = 0; + a->changed = false; } void affinity__cleanup(struct affinity *a) @@ -87,17 +113,25 @@ void affinity__cleanup(struct affinity *a) void cpu_map__set_affinity(const struct perf_cpu_map *cpumap) { - int cpu_set_size = get_cpu_set_size(); - unsigned long *cpuset = bitmap_zalloc(cpu_set_size * 8); + size_t bits = get_cpu_set_bits(); + unsigned long *cpuset; struct perf_cpu cpu; unsigned int idx; + perf_cpu_map__for_each_cpu_skip_any(cpu, idx, cpumap) { + if (cpu.cpu >= 0 && (size_t)cpu.cpu >= bits) + bits = BITS_TO_LONGS((size_t)cpu.cpu + 1) * BITS_PER_LONG; + } + + cpuset = bitmap_zalloc(bits); if (!cpuset) return; - perf_cpu_map__for_each_cpu_skip_any(cpu, idx, cpumap) - __set_bit(cpu.cpu, cpuset); + perf_cpu_map__for_each_cpu_skip_any(cpu, idx, cpumap) { + if (cpu.cpu >= 0) + __set_bit(cpu.cpu, cpuset); + } - sched_setaffinity(0, cpu_set_size, (cpu_set_t *)cpuset); + sched_setaffinity(0, bits / 8, (cpu_set_t *)cpuset); zfree(&cpuset); } diff --git a/tools/perf/util/affinity.h b/tools/perf/util/affinity.h index 7341194b2298..d21028e2465f 100644 --- a/tools/perf/util/affinity.h +++ b/tools/perf/util/affinity.h @@ -3,16 +3,19 @@ #define PERF_AFFINITY_H 1 #include <stdbool.h> +#include <stddef.h> struct perf_cpu_map; struct affinity { unsigned long *orig_cpus; unsigned long *sched_cpus; + size_t cpu_set_bits; bool changed; }; void affinity__cleanup(struct affinity *a); -void affinity__set(struct affinity *a, int cpu); +bool affinity__has_cpu(const struct affinity *a, int cpu); +int affinity__set(struct affinity *a, int cpu); int affinity__setup(struct affinity *a); void cpu_map__set_affinity(const struct perf_cpu_map *cpumap); -- 2.56.1.831.g720f3f5937-goog ^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH v3 2/2] perf test: Add filtered event enabled time tests to event-times 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 0 siblings, 0 replies; 11+ messages in thread From: Peter Zijlstra @ 2026-12-02 14:13 UTC (permalink / raw) 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, linux-kernel On Tue, Nov 24, 2026 at 10:52:29PM -0800, Ian Rogers wrote: > Extend the "Event times" test suite (tools/perf/tests/event-times.c) > with subtests verifying that filtered-out events stay in > PERF_EVENT_STATE_ON and do not accrue bogus total_time_enabled: > > - Task and CPU filtered event times: open a cpu-clock event targeting > both the current thread and one CPU, verifying zero enabled/running > time when enabled on a non-matching CPU (including the mmap userpage), > matching enabled and running times on the target CPU, zero time > accrued after migrating away, and matching times across fork() > inheritance. > - Hybrid PMU filtered event times: on systems with multiple core PMUs > (including >2 core types), iterate all core PMUs and verify per-task > core PMU events accrue zero enabled time while running on CPUs > belonging to any other core PMU. > - Multiplexed and group filtered event times: iterate core PMUs and > verify flexible events and groups in PERF_EVENT_STATE_INACTIVE > (including siblings promoted to group leader on leader close) > transition back to PERF_EVENT_STATE_ON on context switch and accrue > zero enabled time after migrating to a filtered-out CPU. > > Reuse struct affinity from tools/perf/util/affinity.{c,h}, hardening > affinity__setup() to retry on EINVAL for large kernel cpumasks, adding > affinity__has_cpu(), and returning an error status from affinity__set(). > > Signed-off-by: Ian Rogers <irogers@google.com> > --- > v3: > - Try PERF_TYPE_HARDWARE with PERF_COUNT_HW_CPU_CYCLES first on > non-hybrid systems (perf_pmus__num_core_pmus() <= 1) in > open_core_pmu_event() before falling back to raw PMU configs, as > spotted by Sashiko on v2: > https://lore.kernel.org/linux-perf-users/c41f98313862f6545863598dcde871cfb80aba59.1795582691.git.irogers@google.com/ > - Loop over candidate configs in open_core_pmu_event() and iterate all > core PMUs via perf_pmus__scan_core() in the hybrid and multiplexing > subtests to support hybrid machines with >2 CPU types. Can the perf build system please also mention the various NO_foo flags along with the libs it wants that I'm not going to install? I always have to grep for a minute before I can build a new perf :/ Build command of the day ended up being: make NO_SLANG=1 NO_BPF=1 NO_PYTHON_MODULE=1 NO_JEVENTS=1 ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v3 1/2] perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time 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 ` sashiko-bot 2026-11-25 12:37 ` Peter Zijlstra 2 siblings, 0 replies; 11+ messages in thread From: sashiko-bot @ 2026-11-25 7:09 UTC (permalink / raw) To: Ian Rogers; +Cc: linux-perf-users Sashiko has reviewed this 2-patch series and found no issues. -- Sashiko AI review · https://sashiko.dev/#/patchset/80d983c5713537f5743b69e9b35ced1177e4732e.1795589380.git.irogers@google.com ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v3 1/2] perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time 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 2026-11-25 13:02 ` Peter Zijlstra 2 siblings, 1 reply; 11+ messages in thread From: Peter Zijlstra @ 2026-11-25 12:37 UTC (permalink / raw) 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, linux-kernel 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. ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v3 1/2] perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time 2026-11-25 12:37 ` Peter Zijlstra @ 2026-11-25 13:02 ` Peter Zijlstra 2026-11-30 18:42 ` Ian Rogers 0 siblings, 1 reply; 11+ messages in thread From: Peter Zijlstra @ 2026-11-25 13:02 UTC (permalink / raw) 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, linux-kernel On Wed, Nov 25, 2026 at 01:37:12PM +0100, Peter Zijlstra wrote: > On Tue, Nov 24, 2026 at 10:52:28PM -0800, Ian Rogers wrote: > > @@ -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); > > +} This whole thing looks dodgy as well. This feels like __perf_effective_state() needs an update or something. ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v3 1/2] perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time 2026-11-25 13:02 ` Peter Zijlstra @ 2026-11-30 18:42 ` Ian Rogers 2026-12-01 9:18 ` Peter Zijlstra 0 siblings, 1 reply; 11+ messages in thread From: Ian Rogers @ 2026-11-30 18:42 UTC (permalink / raw) To: Peter Zijlstra 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, linux-kernel On Wed, Nov 25, 2026 at 5:02 AM Peter Zijlstra <peterz@infradead.org> wrote: > > On Wed, Nov 25, 2026 at 01:37:12PM +0100, Peter Zijlstra wrote: > > On Tue, Nov 24, 2026 at 10:52:28PM -0800, Ian Rogers wrote: > > > @@ -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); > > > +} > > This whole thing looks dodgy as well. This feels like > __perf_effective_state() needs an update or something. Thanks for the review, Peter! I've sent v4 addressing all of these: https://lore.kernel.org/linux-perf-users/8b61be11e09ae7f663253cde769eff5456ebc263.1796063587.git.irogers@google.com/ - Took your state diagram and comment cleanup in perf_event.h. - Updated __perf_effective_state() and perf_event_set_state() so enabled (ON) siblings inherit INACTIVE from their group leader, removing group_set_inactive() (while promoting ON siblings to INACTIVE on flexible_inactive if an INACTIVE leader is detached). - De-duplicated the pending_disable path in event_sched_out(). - Updated merge_sched_in() to branch directly on group_can_go_on() and group_sched_in() rather than testing event->state == ON. In event_sched_in(), reverting the speculative ACTIVE back to ON keeps siblings in ON and matches group_error: rollback so merge_sched_in() remains the single place transitioning ON -> {ERROR, INACTIVE}. Thanks, Ian ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v3 1/2] perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time 2026-11-30 18:42 ` Ian Rogers @ 2026-12-01 9:18 ` Peter Zijlstra 2026-12-01 16:47 ` Ian Rogers 0 siblings, 1 reply; 11+ messages in thread From: Peter Zijlstra @ 2026-12-01 9:18 UTC (permalink / raw) 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, linux-kernel On Mon, Nov 30, 2026 at 10:42:55AM -0800, Ian Rogers wrote: > On Wed, Nov 25, 2026 at 5:02 AM Peter Zijlstra <peterz@infradead.org> wrote: > > > > On Wed, Nov 25, 2026 at 01:37:12PM +0100, Peter Zijlstra wrote: > > > On Tue, Nov 24, 2026 at 10:52:28PM -0800, Ian Rogers wrote: > > > > @@ -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); > > > > +} > > > > This whole thing looks dodgy as well. This feels like > > __perf_effective_state() needs an update or something. > > > Thanks for the review, Peter! I've sent v4 addressing all of these: > https://lore.kernel.org/linux-perf-users/8b61be11e09ae7f663253cde769eff5456ebc263.1796063587.git.irogers@google.com/ > > - Took your state diagram and comment cleanup in perf_event.h. > - Updated __perf_effective_state() and perf_event_set_state() so enabled > (ON) siblings inherit INACTIVE from their group leader, removing > group_set_inactive() (while promoting ON siblings to INACTIVE on > flexible_inactive if an INACTIVE leader is detached). > - De-duplicated the pending_disable path in event_sched_out(). > - Updated merge_sched_in() to branch directly on group_can_go_on() and > group_sched_in() rather than testing event->state == ON. In > event_sched_in(), reverting the speculative ACTIVE back to ON keeps > siblings in ON and matches group_error: rollback so merge_sched_in() > remains the single place transitioning ON -> {ERROR, INACTIVE}. > I've got to ask; are you vibing this? There seem to be random changes in the patch not listed above that make review harder than it wants to be. Eg. there is now a shortcut in group_sched_out() that avoids iterating the siblings when !active. This then leads to other weirdness. ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v3 1/2] perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time 2026-12-01 9:18 ` Peter Zijlstra @ 2026-12-01 16:47 ` Ian Rogers 2026-12-02 10:17 ` Peter Zijlstra 0 siblings, 1 reply; 11+ messages in thread From: Ian Rogers @ 2026-12-01 16:47 UTC (permalink / raw) To: Peter Zijlstra 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, linux-kernel On Tue, Dec 1, 2026 at 1:19 AM Peter Zijlstra <peterz@infradead.org> wrote: > > On Mon, Nov 30, 2026 at 10:42:55AM -0800, Ian Rogers wrote: > > On Wed, Nov 25, 2026 at 5:02 AM Peter Zijlstra <peterz@infradead.org> wrote: > > > > > > On Wed, Nov 25, 2026 at 01:37:12PM +0100, Peter Zijlstra wrote: > > > > On Tue, Nov 24, 2026 at 10:52:28PM -0800, Ian Rogers wrote: > > > > > @@ -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); > > > > > +} > > > > > > This whole thing looks dodgy as well. This feels like > > > __perf_effective_state() needs an update or something. > > > > > > Thanks for the review, Peter! I've sent v4 addressing all of these: > > https://lore.kernel.org/linux-perf-users/8b61be11e09ae7f663253cde769eff5456ebc263.1796063587.git.irogers@google.com/ > > > > - Took your state diagram and comment cleanup in perf_event.h. > > - Updated __perf_effective_state() and perf_event_set_state() so enabled > > (ON) siblings inherit INACTIVE from their group leader, removing > > group_set_inactive() (while promoting ON siblings to INACTIVE on > > flexible_inactive if an INACTIVE leader is detached). > > - De-duplicated the pending_disable path in event_sched_out(). > > - Updated merge_sched_in() to branch directly on group_can_go_on() and > > group_sched_in() rather than testing event->state == ON. In > > event_sched_in(), reverting the speculative ACTIVE back to ON keeps > > siblings in ON and matches group_error: rollback so merge_sched_in() > > remains the single place transitioning ON -> {ERROR, INACTIVE}. > > > > I've got to ask; are you vibing this? There seem to be random changes in > the patch not listed above that make review harder than it wants to be. > > Eg. there is now a shortcut in group_sched_out() that avoids iterating > the siblings when !active. This then leads to other weirdness. Fair criticism - I used an AI assistant to help iterate and test between v3 and v4, and I didn't keep the v3->v4 diff tight enough or list all of the resulting churn in my reply. Sorry for wasting your review time. Specifically, the unlisted changes in v4 were: 1. group_sched_out() / group_update_userpage() move: Once siblings stayed in ON (inheriting INACTIVE via __perf_effective_state()), group_sched_out() iterating siblings would hit `event->state < INACTIVE` and skip updating sibling userpages. That led to moving event_update_userpage()/group_update_userpage() up in core.c, calling group_update_userpage() from event_sched_out(), and adding the `if (!active) return;` shortcut in group_sched_out(). Looking at it again, calling group_update_userpage() inside event_sched_out() and moving those helpers 1,600 lines up is ugly and unnecessary — if group_sched_out() itself handles the !active case (or updates userpages there instead of in event_sched_out()), none of that weirdness is needed. 2. READ_ONCE() in __perf_effective_state(): Was added so `leader->state` isn't loaded twice in the two checks from lockless calc_timer_values() callers. 3. `if (event->state <= PERF_EVENT_STATE_OFF)` in merge_sched_in(): Previously implicit in `if (event->state == PERF_EVENT_STATE_ON)` to avoid overwriting OFF if group_sched_in()'s rollback consumed pending_disable. 4. `bool inactive` passed to perf_group_detach() / perf_promote_sibling_to_leader(): Because siblings now stay ON, __event_disable() clears the leader's INACTIVE state before perf_group_detach() runs, so without recording that the detached leader was INACTIVE, promoted siblings would stay ON and stop accruing enabled time. 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)? - 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? Thanks, Ian ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v3 1/2] perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time 2026-12-01 16:47 ` Ian Rogers @ 2026-12-02 10:17 ` Peter Zijlstra 2026-12-02 14:26 ` Peter Zijlstra 0 siblings, 1 reply; 11+ messages in thread From: Peter Zijlstra @ 2026-12-02 10:17 UTC (permalink / raw) 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, linux-kernel On Tue, Dec 01, 2026 at 08:47:55AM -0800, Ian Rogers wrote: > Fair criticism - I used an AI assistant to help iterate and test > between v3 and v4, and I didn't keep the v3->v4 diff tight enough or > list all of the resulting churn in my reply. Sorry for wasting your > review time. > > Specifically, the unlisted changes in v4 were: > > 1. group_sched_out() / group_update_userpage() move: > Once siblings stayed in ON (inheriting INACTIVE via > __perf_effective_state()), group_sched_out() iterating siblings would > hit `event->state < INACTIVE` and skip updating sibling userpages. > That led to moving event_update_userpage()/group_update_userpage() up > in core.c, calling group_update_userpage() from event_sched_out(), > and adding the `if (!active) return;` shortcut in group_sched_out(). > Looking at it again, calling group_update_userpage() inside > event_sched_out() and moving those helpers 1,600 lines up is ugly and > unnecessary — if group_sched_out() itself handles the !active case > (or updates userpages there instead of in event_sched_out()), none of > that weirdness is needed. > > 2. READ_ONCE() in __perf_effective_state(): > Was added so `leader->state` isn't loaded twice in the two checks > from lockless calc_timer_values() callers. > > 3. `if (event->state <= PERF_EVENT_STATE_OFF)` in merge_sched_in(): > Previously implicit in `if (event->state == PERF_EVENT_STATE_ON)` to > avoid overwriting OFF if group_sched_in()'s rollback consumed > pending_disable. > > 4. `bool inactive` passed to perf_group_detach() / > perf_promote_sibling_to_leader(): > Because siblings now stay ON, __event_disable() clears the leader's > INACTIVE state before perf_group_detach() runs, so without recording > that the detached leader was INACTIVE, promoted siblings would stay > ON and stop accruing enabled time. > > 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)? > - 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? 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. ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v3 1/2] perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time 2026-12-02 10:17 ` Peter Zijlstra @ 2026-12-02 14:26 ` Peter Zijlstra 0 siblings, 0 replies; 11+ messages in thread From: Peter Zijlstra @ 2026-12-02 14:26 UTC (permalink / raw) 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, linux-kernel 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; ^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2026-12-02 14:26 UTC | newest] Thread overview: 11+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 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-12-02 14:13 ` Peter Zijlstra 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 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 is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox