perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time
Subsystems (2)
Ian Rogers
reviewerPATCH v6 1/2 perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time
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/[email protected]/
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.
Enabled time has accrued for any state > PERF_EVENT_STATE_OFF since the original event state machine, so this isn't a regression from a particular commit; hybrid and big.LITTLE PMU support just made the problem far more visible in practice.
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.
- During sched_in, merge_sched_in() is the only place moving events from ON to {ERROR, INACTIVE}: pinned groups that fail to schedule enter ERROR, while flexible groups that fail move (with their enabled siblings) to INACTIVE via group_set_inactive(), which replaces group_update_userpage(), and are placed on epc->flexible_active behind the ACTIVE groups. event_sched_in() and partial group rollback in group_sched_in() return events to ON.
- event_sched_out() and group_sched_out() handle INACTIVE events by moving them back to ON without calling pmu->del() or adjusting active_oncpu/nr_freq, so context switch and rotation reset INACTIVE groups via the existing flexible_active walk.
- Enabling a sibling whose group leader is INACTIVE moves it straight to INACTIVE in __perf_event_enable() so it accrues enabled time with the group.
- perf_promote_sibling_to_leader() adds promoted ACTIVE siblings to the head and INACTIVE siblings to the tail of flexible_active so ctx_event_to_rotate() keeps picking an ACTIVE group first.
- perf_event_read() serializes on ctx->lock for both ON and INACTIVE events.
---
v6: Address Sashiko review of v5:
https://lore.kernel.org/all/[email protected]/
- Only update the userpage of an INACTIVE event in event_sched_out() when it is mmapped, as in event_set_inactive(), so an event redirected with PERF_EVENT_IOC_SET_OUTPUT doesn't overwrite the ring buffer owner's userpage.
- Use event_set_inactive() with the PMU disabled when enabling a sibling of an INACTIVE leader in __perf_event_enable(), so a PMU interrupt can't nest the userpage update.
v5: Rework following Peter Zijlstra's simplified version and review on
v3/v4:
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/
- Drop epc->flexible_inactive and put INACTIVE flexible groups on
epc->flexible_active after the ACTIVE ones. - Drop the __perf_effective_state()/perf_event_set_state() sibling inheritance from v4 and instead set siblings INACTIVE directly in group_set_inactive() (replacing event_update_userpage() and group_update_userpage() in place).
- Roll back to ON in event_sched_in() and group_sched_in() so merge_sched_in() does the only sched_in ON -> {ERROR, INACTIVE} transition, avoiding siblings stuck in INACTIVE after pinned ERROR or pending_disable rollback, and avoiding enabling OFF siblings.
- Match an INACTIVE leader when enabling a sibling in __perf_event_enable(), and document this transition in perf_event.h.
- Use list_add() for promoted ACTIVE siblings and list_add_tail() for INACTIVE siblings in perf_promote_sibling_to_leader().
- Drop the Fixes: tag, as enabled time has always accrued for states
Quoted 1 line
PERF_EVENT_STATE_OFF.
v4: Address review feedback from Peter Zijlstra on v3:
https://lore.kernel.org/all/[email protected]/ https://lore.kernel.org/all/[email protected]/ - Update the state diagram and comments in perf_event.h as suggested. - Extend __perf_effective_state() and perf_event_set_state() so enabled (ON) siblings inherit INACTIVE from their group leader. - De-duplicate pending_disable handling in event_sched_out(). - Make merge_sched_in() branch directly on group_can_go_on() and group_sched_in().
v3: No changes.
v2: Make the untested RFC v1 functional, tested, and handle all state
transitions:
https://lore.kernel.org/lkml/[email protected]/
---
include/linux/perf_event.h | 63 +++++++++-----
kernel/events/core.c | 165 ++++++++++++++++++++++---------------
2 files changed, 140 insertions(+), 88 deletions(-)include/linux/perf_event.h +42 -21
@@ -639,28 +639,48 @@ 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 {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: +* +* - sched_out() with pending_disable moves {INACTIVE,ACTIVE} directly to OFF; +* +* - enable() of a group sibling whose leader is INACTIVE moves it directly +* to INACTIVE; +* +* - 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 +702,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 ;
kernel/events/core.c +98 -67
337 lines not on this page · raw message
Signature · 1 line
2.56.1.915.g69d5ec7298-goog
Ian Rogers
reviewerPATCH v6 2/2 perf test: Add filtered event enabled time tests to event-times
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.
- Pinned-blocked and group rollback event times: iterate core PMUs and verify that enabling a disabled sibling while a pinned exclusive event keeps its group leader INACTIVE makes the sibling accrue enabled (but not running) time with the group, and that group_sched_in() rollback of a pinned group (sized to pass the PMU's group validation, then blocked by earlier pinned events) leaves the rolled-back siblings ON and a disabled tail sibling OFF, including after re-enabling the leader on a filtered-out CPU and detaching it.
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().
---
v6: Address Sashiko review of v5:
https://lore.kernel.org/all/[email protected]/
- Mark the multiplexed/group and pinned-blocked subtests exclusive so they don't contend for counters with parallel tests.
- Set needs_mlock for the suite, as the task and CPU subtest mmaps the event userpage.
- Don't print the uninitialized rt_child when read_times() fails.
- Preserve errno across zfree() in affinity__setup().
- Reword the pinned group rollback description in the changelog.
v5:
- Add the "Pinned-blocked and group rollback event times" subtest covering the cases found reviewing Peter Zijlstra's v5 draft.
- Factor out pick_pmu_and_other_cpu() and open_core_pmu_event_opts().
- Update the comment for INACTIVE groups now living on
epc->flexible_active.
- Tested on non-hybrid as well as hybrid PMU configurations. The task and CPU subtest only needs a software event and 2 CPUs, so it covers any machine; the multiplexed/group and pinned-blocked subtests use a task+CPU filter on a single core PMU when not hybrid. Against an unpatched kernel with a single (non-hybrid) 4-counter core PMU, the task and CPU, multiplexed/group and pinned-blocked subtests fail (e.g. "filtered CPU 1:
ena=45214232 run=0"), while all pass with patch 1 applied.
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 | 855 ++++++++++++++++++++++++++++++++- tools/perf/util/affinity.c | 102 ++-- tools/perf/util/affinity.h | 5 +- 3 files changed, 927 insertions(+), 35 deletions(-)
tools/perf/tests/event-times.c +854 -1
@@ -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,844 @@ static int test__event_times(struct test_suite *test __maybe_unused, int subtestreturn 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++) + asmvolatile ("" :"+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_opts (const struct perf_pmu *pmu,pid_t pid, +int cpu,int group_fd,bool disabled, +bool pinned,bool exclusive) +{ +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 , + .pinned = pinned ?1 :0 , + .exclusive = exclusive ?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 ; +} + +static int open_core_pmu_event (const struct perf_pmu *pmu,pid_t pid,int cpu, +int group_fd,bool disabled) +{ +return open_core_pmu_event_opts (pmu, pid, cpu, group_fd, disabled, + false, false); +} + +/* +* 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 ) + goto out; +if (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 pick_pmu_and_other_cpu (const struct perf_pmu *pmu, +const struct affinity *aff, +int *cpu0,int *cpu1,int *ev_cpu) +{ +unsigned long c; + + *cpu0 =first_allowed_pmu_cpu (aff, pmu); + *cpu1 =-1 ; +if (*cpu0 <0 ) +return -1 ; + +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; + } + +return *cpu1 >=0 ?0 :-1 ; +} + +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 ; +int ret =TEST_FAIL ; + +for (int i =0 ; i <NR_FLEX ; i++) + flex_fds[i] =-1 ; + +if (pick_pmu_and_other_cpu (pmu, aff, &cpu0, &cpu1, &ev_cpu) <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_active), 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 int test_one_pmu_pinned_blocked_and_rollback (const struct perf_pmu *pmu, +struct affinity *aff) +{ +enum {NR_PIN_SIBS =20 ,NR_BLOCKERS =2 }; +struct read_times rt, before_l, after_l, before_s, after_s; +int pin_fd =-1 , flex_leader =-1 , flex_dis_sib =-1 ; +int pin_leader =-1 , pin_dis_sib =-1 ; +int pin_sibs[NR_PIN_SIBS ], blockers[NR_BLOCKERS ]; +int cpu0 =-1 , cpu1 =-1 , ev_cpu =-1 , nr_sibs =0 ; +bool leader_error = false, rollback_checked = false; +int ret =TEST_FAIL ; +ssize_t n; + +for (int i =0 ; i <NR_PIN_SIBS ; i++) + pin_sibs[i] =-1 ; +for (int i =0 ; i <NR_BLOCKERS ; i++) + blockers[i] =-1 ; + +if (pick_pmu_and_other_cpu (pmu, aff, &cpu0, &cpu1, &ev_cpu) <0 || +affinity__set (aff, cpu0) <0 ) +return TEST_SKIP ; + +/* +* Part 1: Hold the PMU with a pinned exclusive event so a flexible +* group stays in PERF_EVENT_STATE_INACTIVE, then enable a disabled +* sibling and verify it matches the INACTIVE leader and stops accruing +* enabled time after migrating to a filtered-out CPU. +*/ + pin_fd =open_core_pmu_event_opts (pmu,0 , ev_cpu,-1 , false, true, true); +if (pin_fd <0 ) { + ret =TEST_SKIP ; + goto out; + } +burn_user_ms (10 ); +if (read_times (pin_fd, &rt) <0 || rt.run ==0 ) { + ret =TEST_SKIP ; + goto out; + } + + flex_leader =open_core_pmu_event (pmu,0 , ev_cpu,-1 , false); +if (flex_leader <0 ) { + ret =TEST_SKIP ; + goto out; + } + flex_dis_sib =open_core_pmu_event (pmu,0 , ev_cpu, flex_leader, true); +if (flex_dis_sib <0 ) { + ret =TEST_SKIP ; + goto out; + } + +burn_user_ms (15 ); +if (read_times (flex_leader, &before_l) <0 || +read_times (flex_dis_sib, &before_s) <0 ) + goto out; +if (before_l.ena ==0 || before_l.run !=0 || + before_s.ena !=0 || before_s.run !=0 ) { +pr_debug ("FAILED: %s pre-enable: %" PRIu64"/%" PRIu64" %" PRIu64"/%" PRIu64"\n" , + pmu->name, before_l.ena, before_l.run, + before_s.ena, before_s.run); + goto out; + } + +if (ioctl (flex_dis_sib,PERF_EVENT_IOC_ENABLE ,0 ) <0 ) + goto out; +burn_user_ms (20 ); +if (read_times (flex_dis_sib, &after_s) <0 ) + goto out; +if (after_s.ena ==0 || after_s.run !=0 ) { +pr_debug ("FAILED: %s INACTIVE sibling enable: ena=%" PRIu64" run=%" PRIu64"\n" , + pmu->name, after_s.ena, after_s.run); + goto out; + } + +if (affinity__set (aff, cpu1) <0 || +read_times (flex_leader, &before_l) <0 || +read_times (flex_dis_sib, &before_s) <0 ) + goto out; +burn_user_ms (20 ); +if (read_times (flex_leader, &after_l) <0 || +read_times (flex_dis_sib, &after_s) <0 ) + goto out; +if (after_l.ena != before_l.ena || after_l.run != before_l.run || + after_s.ena != before_s.ena || after_s.run != before_s.run) { +pr_debug ("FAILED: %s pinned-blocked filtered CPU %d advanced\n" , + pmu->name, cpu1); + goto out; + } + +close (flex_dis_sib); + flex_dis_sib =-1 ; +close (flex_leader); + flex_leader =-1 ; +close (pin_fd); + pin_fd =-1 ; + +/* +* Part 2: Trigger group_sched_in() rollback of a pinned group +* (including a disabled tail sibling). Verify rolled-back siblings stay +* ON (not INACTIVE) and the disabled sibling stays OFF after +* re-enabling the leader on a filtered CPU and after detaching the +* leader. +* +* PMUs validate group size at perf_event_open() time, so grow the +* group until another sibling is rejected (or a cap is hit), keeping +* room for the disabled tail sibling. Earlier pinned blocker events +* hold counters so the group passes validation but cannot be +* scheduled. Part 2 is optional: if it can't be set up, the Part 1 +* result stands. +*/ +if (affinity__set (aff, cpu0) <0 ) + goto out; + +for (int i =0 ; i <NR_BLOCKERS ; i++) { + blockers[i] =open_core_pmu_event_opts (pmu,0 , ev_cpu,-1 , false, true, false); +if (blockers[i] <0 ) + goto done; + } + pin_leader =open_core_pmu_event_opts (pmu,0 , ev_cpu,-1 , true, true, false); +if (pin_leader <0 ) + goto done; +for (; nr_sibs <NR_PIN_SIBS ; nr_sibs++) { +int fd =open_core_pmu_event (pmu,0 , ev_cpu, pin_leader, false); + +if (fd <0 ) +break ; + pin_sibs[nr_sibs] = fd; + } +if (nr_sibs <NR_PIN_SIBS && nr_sibs >0 ) { +/* Make room for the disabled tail sibling. */ + nr_sibs--; +close (pin_sibs[nr_sibs]); + pin_sibs[nr_sibs] =-1 ; + } +if (nr_sibs ==0 ) + goto done; + pin_dis_sib =open_core_pmu_event (pmu,0 , ev_cpu, pin_leader, true); +if (pin_dis_sib <0 ) + goto done; + +if (ioctl (pin_leader,PERF_EVENT_IOC_ENABLE ,0 ) <0 ) + goto out; +burn_user_ms (15 ); + +/* read() returns 0 (EOF) for an event in PERF_EVENT_STATE_ERROR. */ + n =read (pin_leader, &rt,sizeof (rt)); +if (n ==0 ) { + leader_error = true; + rt.ena = rt.run =0 ; + }else if (n != (ssize_t )sizeof (rt)) { + goto out; + } +if (leader_error || rt.run ==0 ) { +if (rt.ena !=0 ) { +pr_debug ("FAILED: %s pinned ERROR leader accrued ena=%" PRIu64"\n" , + pmu->name, rt.ena); + goto out; + } +for (int i =0 ; i < nr_sibs; i++) { +if (read_times (pin_sibs[i], &rt) <0 ) + goto out; +if (rt.ena !=0 || rt.run !=0 ) { +pr_debug ("FAILED: %s rollback sib %d: %" PRIu64"/%" PRIu64"\n" , + pmu->name, i, rt.ena, rt.run); + goto out; + } + } +if (read_times (pin_dis_sib, &rt) <0 || rt.ena !=0 || rt.run !=0 ) { +pr_debug ("FAILED: %s rollback dis sib: ena=%" PRIu64" run=%" PRIu64"\n" , + pmu->name, rt.ena, rt.run); + goto out; + } + +/* +* Re-enable leader while filtered out on cpu1: moves leader +* ERROR -> ON; siblings must remain ON/OFF and accrue 0 time. +*/ +if (affinity__set (aff, cpu1) <0 || +ioctl (pin_leader,PERF_EVENT_IOC_ENABLE ,0 ) <0 ) + goto out; +burn_user_ms (20 ); + +for (int i =0 ; i < nr_sibs; i++) { +if (read_times (pin_sibs[i], &rt) <0 ) + goto out; +if (rt.ena !=0 || rt.run !=0 ) { +pr_debug ("FAILED: %s filtered CPU %d sib %d: ena=%" PRIu64"\n" , + pmu->name, cpu1, i, rt.ena); + goto out; + } + } +if (read_times (pin_dis_sib, &rt) <0 || rt.ena !=0 || rt.run !=0 ) { +pr_debug ("FAILED: %s filtered CPU %d dis sib: ena=%" PRIu64"\n" , + pmu->name, cpu1, rt.ena); + goto out; + } + +/* +* Detach leader on cpu1, then return to cpu0: disabled tail +* sibling must still be OFF and accrue 0 enabled/running time. +*/ +close (pin_leader); + pin_leader =-1 ; +burn_user_ms (15 ); +if (read_times (pin_sibs[0 ], &rt) <0 || rt.ena !=0 || rt.run !=0 ) { +pr_debug ("FAILED: %s promoted sib CPU %d: ena=%" PRIu64"\n" , + pmu->name, cpu1, rt.ena); + goto out; + } + +if (affinity__set (aff, cpu0) <0 ) + goto out; +burn_user_ms (15 ); +if (read_times (pin_dis_sib, &rt) <0 || rt.ena !=0 || rt.run !=0 ) { +pr_debug ("FAILED: %s tail sib enabled: ena=%" PRIu64" run=%" PRIu64"\n" , + pmu->name, rt.ena, rt.run); + goto out; + } + rollback_checked = true; + } + +done: +if (!rollback_checked) +pr_debug ("%s: pinned group rollback not triggered, checks skipped\n" , pmu->name); + ret =TEST_OK ; +out: +if (flex_dis_sib >=0 ) +close (flex_dis_sib); +if (flex_leader >=0 ) +close (flex_leader); +if (pin_fd >=0 ) +close (pin_fd); +if (pin_dis_sib >=0 ) +close (pin_dis_sib); +for (int i =0 ; i <NR_PIN_SIBS ; i++) { +if (pin_sibs[i] >=0 ) +close (pin_sibs[i]); + } +if (pin_leader >=0 ) +close (pin_leader); +for (int i =0 ; i <NR_BLOCKERS ; i++) { +if (blockers[i] >=0 ) +close (blockers[i]); + } +return ret; +} + +/* +* Verify enabling a disabled sibling while its group leader is INACTIVE +* (blocked by a pinned exclusive event) and partial group_sched_in() rollback +* of an oversized pinned group across all core PMUs. +*/ +static int test__event_times_pinned_rollback (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_pinned_blocked_and_rollback (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_EXCLUSIVE ("Multiplexed and group filtered event times" , + event_times_mux_migration, +"no hardware PMU or <2 allowed CPUs" ), +TEST_CASE_REASON_EXCLUSIVE ("Pinned-blocked and group rollback event times" , + event_times_pinned_rollback, +"no hardware PMU or <2 allowed CPUs" ), + { .name =NULL , } +}; + +struct test_suite suite__event_times = { + .desc ="Event times" , + .test_cases = tests__event_times, + .needs_mlock = true, +};
tools/perf/util/affinity.c +69 -33
165 lines not on this page · raw message
tools/perf/util/affinity.h +4 -1
@@ -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);
Signature · 1 line
2.56.1.915.g69d5ec7298-goog
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] perf/core: O(N) INACTIVE scan per tick in perf_adjust_freq_unthr_context() - [Medium] perf/core…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] perf/core: O(N) INACTIVE scan per tick in perf_adjust_freq_unthr_context()
- [Medium] perf/core: O(N*S) sched mutations and double userpage updates on rotation
--
commit 7f7c05c1a50554767d36de028fe90262549caf18
Author: Ian Rogers <[email protected]>
perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time
This commit introduces PERF_EVENT_STATE_ON for enabled events that are filtered out during sched_in and reserves PERF_EVENT_STATE_INACTIVE for flexible groups that cannot be scheduled due to counter or exclusivity constraints, linking them onto epc->flexible_active after the active groups.
Quoted 4 lines
kernel/events/core.c +0
-0
[ ... ]
Quoted 21 lines
kernel/events/core.c +9
-8@@ -2505,16 +2509,23 @@ event_sched_out(struct perf_event *event, struct perf_event_context *ctx)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 (event->attr.exclusive || !cpc->active_oncpu) - cpc->exclusive =0 ; +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 ;
kernel/events/core.c event_sched_out(struct perf_event *event, struct perf_event_context *ctx) · line 2529
Quoted 10 lines
kernel/events/core.c +7
-1-perf_pmu_enable (event->pmu); +perf_pmu_enable (event->pmu); + }else if (refcount_read (&event->mmap_count)) { +/* +* active events get this through ->del(). +*/ +perf_event_update_userpage (event); + } }
[Severity: Medium]
Does transitioning every inactive flexible group and its siblings through event_sched_out() and group_set_inactive() regress multiplexing rotation latency and double userpage updates?
In perf_rotate_context() (every 4ms in hardirq context), __pmu_ctx_sched_out() now walks all groups on flexible_active, including unscheduled inactive groups and all enabled siblings, moving them to PERF_EVENT_STATE_ON and updating mmapped userpages here.
__pmu_ctx_sched_in() -> merge_sched_in() then calls group_set_inactive() on those same unscheduled groups and siblings, mutating them back to PERF_EVENT_STATE_INACTIVE and updating mmapped userpages a second time under ctx->lock with IRQs and the PMU disabled, even though neither the CPU, task, cgroup, nor EVENT_TIME changed between the two calls.
[ ... ]
Quoted 1 line
kernel/events/core.c +0
-0@@ -4156,29 +4178,38 @@ static int merge_sched_in(struct perf_event *event, void *data)
[ ... ]
kernel/events/core.c static int merge_sched_in(struct perf_event *event, void *data) · line 4211
Quoted 10 lines
kernel/events/core.c +9
-0+ }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); }
[Severity: Medium]
Does appending unscheduled inactive groups to pmu_ctx->flexible_active degrade perf_adjust_freq_unthr_context() from scanning only active counters to scanning all multiplexed flexible groups on every scheduler tick?
On every tick with a frequency-mode event or unthrottle pending, perf_adjust_freq_unthr_context() holds ctx->lock with IRQs and the PMU disabled and walks flexible_active in perf_adjust_freq_unthr_events(), which continues past non-active entries rather than stopping at the first inactive group.
In addition, perf_pmu_ctx_is_active() checks !list_empty() on flexible_active, so a pmu_ctx with only inactive groups triggers unnecessary perf_pmu_disable()/perf_pmu_enable() cycles and a full list scan on unthrottle ticks.
Since active groups precede inactive ones on flexible_active, breaking on the first non-active entry in perf_adjust_freq_unthr_events() and checking the head entry state in perf_pmu_ctx_is_active() would avoid this.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/ed76be2c2d97d398770d742df8028634925da3e4.1796365469.git.irogers@google.com?part=1
Ian Rogers
reviewerQuoted 68 lines · On Thu, Dec 3, 2026 at 11:00 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] perf/core: O(N) INACTIVE scan per tick inperf_adjust_freq_unthr_context()
- [Medium] perf/core:O(N*S)sched mutations and double userpage updates on rotation
--commit
7f7c05c1a50554767d36de028fe90262549caf18
Author: Ian Rogers <[email protected]>perf/core: Add
PERF_EVENT_STATE_ONto fix filtered event enabled timeThis commit introduces
PERF_EVENT_STATE_ONfor enabled events that are filtered out duringsched_inand reservesPERF_EVENT_STATE_INACTIVEfor flexible groups that cannot be scheduled due to counter or exclusivity constraints, linking them ontoepc->flexible_activeafter the active groups.Quoted 4 lines
kernel/events/core.c +0
-0[ ... ]
Quoted 21 lines
kernel/events/core.c +9
-8@@ -2505,16 +2509,23 @@ event_sched_out(struct perf_event *event, struct perf_event_context *ctx)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 (event->attr.exclusive || !cpc->active_oncpu) - cpc->exclusive =0 ; +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 ;kernel/events/core.c event_sched_out(struct perf_event *event, struct perf_event_context *ctx) · line 2529
Quoted 10 lines
kernel/events/core.c +7
-1-perf_pmu_enable (event->pmu); +perf_pmu_enable (event->pmu); + }else if (refcount_read (&event->mmap_count)) { +/* +* active events get this through ->del(). +*/ +perf_event_update_userpage (event); + } }[Severity: Medium]
Does transitioning every inactive flexible group and its siblings throughevent_sched_out()andgroup_set_inactive()regress multiplexing rotation latency and double userpage updates?In
perf_rotate_context()(every 4ms in hardirq context),__pmu_ctx_sched_out()now walks all groups onflexible_active, including unscheduled inactive groups and all enabled siblings, moving them toPERF_EVENT_STATE_ONand updating mmapped userpages here.
Quoted 5 lines
__pmu_ctx_sched_in()->merge_sched_in()then callsgroup_set_inactive()on those same unscheduled groups and siblings, mutating them back toPERF_EVENT_STATE_INACTIVEand updating mmapped userpages a second time underctx->lockwith IRQs and the PMU disabled, even though neither the CPU, task, cgroup, norEVENT_TIMEchanged between the two calls.
This is the expected cost of moving the siblings with their leader, which Peter preferred over v4's approach of having ON siblings inherit INACTIVE through __perf_effective_state(): "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."
Rotation already visits every flexible group in merge_sched_in(), and previously group_update_userpage() already walked the siblings of an unscheduled group when it was mmapped. The extra work is a perf_event_set_state() per enabled sibling, and the second userpage update only happens for mmapped events, which are rarely multiplexed.
Quoted 28 lines
[ ... ]
Quoted 1 line
kernel/events/core.c +0
-0@@ -4156,29 +4178,38 @@ static int merge_sched_in(struct perf_event *event, void *data)[ ... ]
kernel/events/core.c static int merge_sched_in(struct perf_event *event, void *data) · line 4211
Quoted 10 lines
kernel/events/core.c +9
-0+ }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); }[Severity: Medium]
Does appending unscheduled inactive groups topmu_ctx->flexible_activedegradeperf_adjust_freq_unthr_context()from scanning only active counters to scanning all multiplexed flexible groups on every scheduler tick?On every tick with a frequency-mode event or unthrottle pending,
perf_adjust_freq_unthr_context()holdsctx->lockwith IRQs and the PMU disabled and walksflexible_activeinperf_adjust_freq_unthr_events(), which continues past non-active entries rather than stopping at the first inactive group.
Quoted 8 lines
In addition,
perf_pmu_ctx_is_active()checks !list_empty() onflexible_active, so apmu_ctxwith only inactive groups triggers unnecessary perf_pmu_disable()/perf_pmu_enable() cycles and a full list scan on unthrottle ticks.Since active groups precede inactive ones on
flexible_active, breaking on the first non-active entry inperf_adjust_freq_unthr_events()and checking the head entry state inperf_pmu_ctx_is_active()would avoid this.
perf_adjust_freq_unthr_context() only walks flexible_active when the context has ACTIVE freq events (nr_freq is only incremented for ACTIVE events) or on unthrottle ticks, and the walk filters on == ACTIVE, as Peter noted on v5.
Breaking at the first non-ACTIVE entry is possible, as on hardware PMUs the ACTIVE groups always precede the INACTIVE ones on flexible_active:
after the first failure in merge_sched_in() can_add_hw is 0, and perf_promote_sibling_to_leader() adds ACTIVE siblings at the head. It does tie the loop to that ordering though, so I've left it as is, but I'm happy to add it if Peter prefers.
Thanks,
Ian
Quoted 2 lines
--
Sashiko AI review · https://sashiko.dev/#/patchset/ed76be2c2d97d398770d742df8028634925da3e4.1796365469.git.irogers@google.com?part=1
Ian Rogers
reviewerQuoted 70 lines · Ian Rogers
Quoted 68 lines · On Thu, Dec 3, 2026 at 11:00 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] perf/core: O(N) INACTIVE scan per tick inperf_adjust_freq_unthr_context()
- [Medium] perf/core:O(N*S)sched mutations and double userpage updates on rotation
--commit
7f7c05c1a50554767d36de028fe90262549caf18
Author: Ian Rogers <[email protected]>perf/core: Add
PERF_EVENT_STATE_ONto fix filtered event enabled timeThis commit introduces
PERF_EVENT_STATE_ONfor enabled events that are filtered out duringsched_inand reservesPERF_EVENT_STATE_INACTIVEfor flexible groups that cannot be scheduled due to counter or exclusivity constraints, linking them ontoepc->flexible_activeafter the active groups.Quoted 4 lines
kernel/events/core.c +0
-0[ ... ]
Quoted 21 lines
kernel/events/core.c +9
-8@@ -2505,16 +2509,23 @@ event_sched_out(struct perf_event *event, struct perf_event_context *ctx)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 (event->attr.exclusive || !cpc->active_oncpu) - cpc->exclusive =0 ; +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 ;kernel/events/core.c event_sched_out(struct perf_event *event, struct perf_event_context *ctx) · line 2529
Quoted 10 lines
kernel/events/core.c +7
-1-perf_pmu_enable (event->pmu); +perf_pmu_enable (event->pmu); + }else if (refcount_read (&event->mmap_count)) { +/* +* active events get this through ->del(). +*/ +perf_event_update_userpage (event); + } }[Severity: Medium]
Does transitioning every inactive flexible group and its siblings throughevent_sched_out()andgroup_set_inactive()regress multiplexing rotation latency and double userpage updates?In
perf_rotate_context()(every 4ms in hardirq context),__pmu_ctx_sched_out()now walks all groups onflexible_active, including unscheduled inactive groups and all enabled siblings, moving them toPERF_EVENT_STATE_ONand updating mmapped userpages here.
Quoted 5 lines
Quoted 5 lines
__pmu_ctx_sched_in()->merge_sched_in()then callsgroup_set_inactive()on those same unscheduled groups and siblings, mutating them back toPERF_EVENT_STATE_INACTIVEand updating mmapped userpages a second time underctx->lockwith IRQs and the PMU disabled, even though neither the CPU, task, cgroup, norEVENT_TIMEchanged between the two calls.
+Peter Zijlstra
Quoted 66 lines
This is the expected cost of moving the siblings with their leader, which Peter preferred over v4's approach of having ON siblings inherit INACTIVE through
__perf_effective_state(): "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."Rotation already visits every flexible group in
merge_sched_in(), and previouslygroup_update_userpage()already walked the siblings of an unscheduled group when it was mmapped. The extra work is aperf_event_set_state()per enabled sibling, and the second userpage update only happens for mmapped events, which are rarely multiplexed.Quoted 28 lines
[ ... ]
Quoted 1 line
kernel/events/core.c +0
-0@@ -4156,29 +4178,38 @@ static int merge_sched_in(struct perf_event *event, void *data)[ ... ]
kernel/events/core.c static int merge_sched_in(struct perf_event *event, void *data) · line 4211
Quoted 10 lines
kernel/events/core.c +9
-0+ }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); }[Severity: Medium]
Does appending unscheduled inactive groups topmu_ctx->flexible_activedegradeperf_adjust_freq_unthr_context()from scanning only active counters to scanning all multiplexed flexible groups on every scheduler tick?On every tick with a frequency-mode event or unthrottle pending,
perf_adjust_freq_unthr_context()holdsctx->lockwith IRQs and the PMU disabled and walksflexible_activeinperf_adjust_freq_unthr_events(), which continues past non-active entries rather than stopping at the first inactive group.Quoted 8 lines
In addition,
perf_pmu_ctx_is_active()checks !list_empty() onflexible_active, so apmu_ctxwith only inactive groups triggers unnecessary perf_pmu_disable()/perf_pmu_enable() cycles and a full list scan on unthrottle ticks.Since active groups precede inactive ones on
flexible_active, breaking on the first non-active entry inperf_adjust_freq_unthr_events()and checking the head entry state inperf_pmu_ctx_is_active()would avoid this.
perf_adjust_freq_unthr_context()only walksflexible_activewhen the context has ACTIVE freq events (nr_freqis only incremented for ACTIVE events) or on unthrottle ticks, and the walk filters on == ACTIVE, as Peter noted on v5.Breaking at the first non-ACTIVE entry is possible, as on hardware PMUs the ACTIVE groups always precede the INACTIVE ones on
flexible_active:
after the first failure inmerge_sched_in()can_add_hwis 0, andperf_promote_sibling_to_leader()adds ACTIVE siblings at the head. It does tie the loop to that ordering though, so I've left it as is, but I'm happy to add it if Peter prefers.Thanks,
IanQuoted 2 lines
--
Sashiko AI review · https://sashiko.dev/#/patchset/ed76be2c2d97d398770d742df8028634925da3e4.1796365469.git.irogers@google.com?part=1
Peter Zijlstra
maintainerQuoted 71 lines · Ian Rogers
Quoted 70 lines · Ian Rogers
Quoted 68 lines · On Thu, Dec 3, 2026 at 11:00 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] perf/core: O(N) INACTIVE scan per tick inperf_adjust_freq_unthr_context()
- [Medium] perf/core:O(N*S)sched mutations and double userpage updates on rotation
--commit
7f7c05c1a50554767d36de028fe90262549caf18
Author: Ian Rogers <[email protected]>perf/core: Add
PERF_EVENT_STATE_ONto fix filtered event enabled timeThis commit introduces
PERF_EVENT_STATE_ONfor enabled events that are filtered out duringsched_inand reservesPERF_EVENT_STATE_INACTIVEfor flexible groups that cannot be scheduled due to counter or exclusivity constraints, linking them ontoepc->flexible_activeafter the active groups.Quoted 4 lines
kernel/events/core.c +0
-0[ ... ]
Quoted 21 lines
kernel/events/core.c +9
-8@@ -2505,16 +2509,23 @@ event_sched_out(struct perf_event *event, struct perf_event_context *ctx)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 (event->attr.exclusive || !cpc->active_oncpu) - cpc->exclusive =0 ; +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 ;kernel/events/core.c event_sched_out(struct perf_event *event, struct perf_event_context *ctx) · line 2529
Quoted 10 lines
kernel/events/core.c +7
-1-perf_pmu_enable (event->pmu); +perf_pmu_enable (event->pmu); + }else if (refcount_read (&event->mmap_count)) { +/* +* active events get this through ->del(). +*/ +perf_event_update_userpage (event); + } }[Severity: Medium]
Does transitioning every inactive flexible group and its siblings throughevent_sched_out()andgroup_set_inactive()regress multiplexing rotation latency and double userpage updates?In
perf_rotate_context()(every 4ms in hardirq context),__pmu_ctx_sched_out()now walks all groups onflexible_active, including unscheduled inactive groups and all enabled siblings, moving them toPERF_EVENT_STATE_ONand updating mmapped userpages here.
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
__pmu_ctx_sched_in()->merge_sched_in()then callsgroup_set_inactive()on those same unscheduled groups and siblings, mutating them back toPERF_EVENT_STATE_INACTIVEand updating mmapped userpages a second time underctx->lockwith IRQs and the PMU disabled, even though neither the CPU, task, cgroup, norEVENT_TIMEchanged between the two calls.+Peter Zijlstra
Just ignore the thing if its wrong?
Ian Rogers
reviewerQuoted 79 lines · Peter Zijlstra
Quoted 77 lines · Ian Rogers
Quoted 75 lines · Ian Rogers
Quoted 73 lines · On Thu, Dec 3, 2026 at 11:00 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] perf/core: O(N) INACTIVE scan per tick inperf_adjust_freq_unthr_context()
- [Medium] perf/core:O(N*S)sched mutations and double userpage updates on rotation
--commit
7f7c05c1a50554767d36de028fe90262549caf18
Author: Ian Rogers <[email protected]>perf/core: Add
PERF_EVENT_STATE_ONto fix filtered event enabled timeThis commit introduces
PERF_EVENT_STATE_ONfor enabled events that are filtered out duringsched_inand reservesPERF_EVENT_STATE_INACTIVEfor flexible groups that cannot be scheduled due to counter or exclusivity constraints, linking them ontoepc->flexible_activeafter the active groups.Quoted 4 lines
kernel/events/core.c +0
-0[ ... ]
Quoted 21 lines
kernel/events/core.c +9
-8@@ -2505,16 +2509,23 @@ event_sched_out(struct perf_event *event, struct perf_event_context *ctx)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 (event->attr.exclusive || !cpc->active_oncpu) - cpc->exclusive =0 ; +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 ;kernel/events/core.c event_sched_out(struct perf_event *event, struct perf_event_context *ctx) · line 2529
Quoted 10 lines
kernel/events/core.c +7
-1-perf_pmu_enable (event->pmu); +perf_pmu_enable (event->pmu); + }else if (refcount_read (&event->mmap_count)) { +/* +* active events get this through ->del(). +*/ +perf_event_update_userpage (event); + } }[Severity: Medium]
Does transitioning every inactive flexible group and its siblings throughevent_sched_out()andgroup_set_inactive()regress multiplexing rotation latency and double userpage updates?In
perf_rotate_context()(every 4ms in hardirq context),__pmu_ctx_sched_out()now walks all groups onflexible_active, including unscheduled inactive groups and all enabled siblings, moving them toPERF_EVENT_STATE_ONand updating mmapped userpages here.
__pmu_ctx_sched_in()->merge_sched_in()then callsgroup_set_inactive()on those same unscheduled groups and siblings, mutating them back toPERF_EVENT_STATE_INACTIVEand updating mmapped userpages a second time underctx->lockwith IRQs and the PMU disabled, even though neither the CPU, task, cgroup, norEVENT_TIMEchanged between the two calls.
Quoted 3 lines
Quoted 1 line
+Peter Zijlstra
Just ignore the thing if its wrong?
:-) I know a number of us have gotten into the habit of not reviewing code when Sashiko has feedback on it.
Thanks,
Ian
[email protected]
AI reviewSashiko has finished reviewing this 2-patch series:
Sashiko has finished reviewing this 2-patch series:
- Patch 2: no issues found.
- Patch 1 ("perf/core: Add PERF_EVENT_STATE_ON to fix filtered e..."): 2 potential issues (2 Medium)
A detailed review has been sent as a separate reply to patch 1.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/ed76be2c2d97d398770d742df8028634925da3e4.1796365469.git.irogers@google.com