perf/core: Skip cgroup attach IPIs while perf_sched_events is disabled
Subsystems (2)
PATCH 1/1 perf/core: Skip cgroup attach IPIs while perf_sched_events is disabled
The perf_event controller is implicitly enabled on cgroup v2, so moving tasks to another cgroup calls perf_cgroup_attach(). For every migrated thread, it calls task_function_call(), which sends a synchronous IPI to the CPU the thread last ran on, so that a CPU counting cgroup events switches to the new cgroup if the thread is running there.
Without cgroup events, no CPU tracks a cgroup and perf_cgroup_switch() returns right away, yet every thread still costs an IPI. Moving a process with hundreds of threads, such as an inference server, then takes milliseconds, all with cgroup_threadgroup_rwsem held for writing, which blocks fork() and exit() on the whole system.
Return early when perf_sched_events is disabled. A cgroup event enables it and waits for a grace period before it is installed. With a full barrier before the key check, the grace period orders the cgroup changes of the migrated tasks before the installation, so the event picks up the new cgroup of the task running on its CPU.
Moving a process with sleeping threads between two cgroups 100 times, 20 ms apart, while another process forks and reaps children in a loop, base and patched kernels alternating over 2 rounds of 3 runs, median of the 6 runs and range in parentheses. Each move also waits about 7 ms for a grace period to take cgroup_threadgroup_rwsem. Median time per move:
threads before (ms) after (ms) change
1 7.8 (7.3-7.9) 7.9 (7.8-7.9) +1%
16 8.8 (8.6-9.1) 7.9 (7.8-7.9) -11%
128 14.0 (13.7-14.7) 7.9 (7.8-8.0) -44%
512 25.1 (24.5-26.1) 9.9 (9.8-10.0) -61%99th percentile of fork() and waitpid() in the other process:
threads before (ms) after (ms) change
1 3.8 (3.5-5.3) 3.6 (3.5-3.7) -5%
16 4.1 (3.8-5.0) 3.7 (3.6-3.8) -11%
128 9.8 (9.5-10.1) 3.7 (3.6-3.8) -63%
512 21.6 (20.8-22.7) 4.8 (4.7-4.9) -78%--- kernel/events/core.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
kernel/events/core.c +11 -0
@@ -15617,6 +15617,17 @@ static void perf_cgroup_attach(struct cgroup_taskset *tset)struct task_struct *task;struct cgroup_subsys_state *css; +/* +* A cgroup event enables perf_sched_events and waits for a grace +* period in account_event() before it is installed, so with the key +* disabled no CPU tracks a cgroup yet. Order the cgroup changes of the +* migrated tasks before the key check, pairing with that grace period, +* so that the event picks up the new cgroup of the running task. +*/ +smp_mb (); +if (!static_branch_unlikely (&perf_sched_events)) +return ; +cgroup_taskset_for_each (task, css, tset)task_function_call (task, __perf_cgroup_move, task); }
---
base-commit: 5f22fb20497f4983094131f00a734362300d22d2
change-id: 20261120-feat-perf-cgroup-attach-ipi-2824375d4d93
Best regards,
Signature · 1 line
Qiliang Yuan <[email protected]>
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 16 lines
The
perf_eventcontroller is implicitly enabled on cgroup v2, so moving tasks to another cgroup callsperf_cgroup_attach(). For every migrated thread, it callstask_function_call(), which sends a synchronous IPI to the CPU the thread last ran on, so that a CPU counting cgroup events switches to the new cgroup if the thread is running there.Without cgroup events, no CPU tracks a cgroup and
perf_cgroup_switch()returns right away, yet every thread still costs an IPI. Moving a process with hundreds of threads, such as an inference server, then takes milliseconds, all withcgroup_threadgroup_rwsemheld for writing, which blocksfork()andexit()on the whole system.Return early when
perf_sched_eventsis disabled. A cgroup event enables it and waits for a grace period before it is installed. With a full barrier before the key check, the grace period orders the cgroup changes [ ... ]
Quoted 5 lines
16 4.1 (3.8-5.0) 3.7 (3.6-3.8) -11% 128 9.8 (9.5-10.1) 3.7 (3.6-3.8) -63% 512 21.6 (20.8-22.7) 4.8 (4.7-4.9) -78%Signed-off-by Qiliang Yuan <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1