sched/fair: Cut per-group work from sched domain rebuilds
cover sched/fair: Cut per-group work from sched domain rebuilds
A sched domain rebuild, from CPU hotplug or a cpuset partition change, moves every active CPU to a new root domain through rq_attach_root(). For each of them rq_offline_fair() and rq_online_fair() walk all task groups with the rq lock held and interrupts disabled, so a rebuild costs O(nr_cpus * nr_task_groups). Dietmar pointed at the offline side when f60a631ab9ed was reviewed [1].
Patch 1 skips rq_offline_fair() for CPUs that stay active. Patch 2 skips the runtime_enabled re-sync in rq_online_fair() unless the CPU actually went down.
With 4000 cpu cgroups on a 32-CPU VM booted with rt_group_sched=0 (median of 20 partition switches and 16 hotplug operations, all three kernels measured in the same run):
operation base (ms) patch 1 (ms) patch 1+2 (ms) change
partition root 13.28 7.68 1.21 -91% cpu offline 44.95 30.96 13.44 -70% cpu online 36.84 28.48 12.82 -65%
With both patches these times no longer grow with the number of groups. With RT_GROUP_SCHED enabled, the rt.c runtime walks remain.
[1] https://lore.kernel.org/r/[email protected]
---
Qiliang Yuan (2):
sched/fair: Skip rq_offline_fair() work for CPUs that stay active sched/fair: Avoid re-syncing runtime_enabled on root domain changes
kernel/sched/fair.c | 20 +++++++++++++++++--- kernel/sched/sched.h | 2 ++ 2 files changed, 19 insertions(+), 3 deletions(-)
---
base-commit: 067c5c24cef23d913b67eaaf71ae67e7169a6620
change-id: 20261111-bug-sched-fair-offline-active-8d063e735d3f
Best regards,
Signature · 1 line
Qiliang Yuan <[email protected]>
PATCH 1/2 sched/fair: Skip rq_offline_fair() work for CPUs that stay active
A sched domain rebuild, triggered by CPU hotplug or a cpuset partition change, calls rq_attach_root() for every active CPU in the affected domains. That runs set_rq_offline() and set_rq_online() with the rq lock held and interrupts disabled, so rq_offline_fair() is called for CPUs that are not going down.
Commit af98d8a36a96 ("sched/fair: Fix CPU bandwidth limit bypass during CPU hotplug") made unthrottle_offline_cfs_rqs() return early for active CPUs, but clear_tg_offline_cfs_rqs(), added by commit f60a631ab9ed ("sched/fair: Fix tg->load when offlining a CPU"), still walks every task group and drops the CPU's tg->load_avg contribution, which update_tg_load_avg() then adds back. With thousands of cpu cgroups each rebuild costs O(nr_cpus * nr_task_groups).
Move the active check into rq_offline_fair() so that both walks only run for a CPU going down.
With 4000 cpu cgroups on a 32-CPU VM booted with rt_group_sched=0, switching a 4-CPU cpuset to a partition root and offlining or onlining one CPU take (median of 20 partition switches and 16 hotplug operations):
operation before (ms) after (ms) change
partition root 13.28 7.68 -42% cpu offline 44.95 30.96 -31% cpu online 36.84 28.48 -23%
--- kernel/sched/fair.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
kernel/sched/fair.c +7 -4
@@ -7816,10 +7816,6 @@ static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq)lockdep_assert_rq_held (rq); -// Do not unthrottle for an active CPU -if (cpumask_test_cpu (cpu_of (rq), cpu_active_mask)) -return ; -/* * The rq clock has already been updated in the * set_rq_offline(), so we should skip updating
@@ -15064,6 +15060,13 @@ static void rq_offline_fair(struct rq *rq) {update_sysctl (); +/* +* A sched domain rebuild moves every active CPU to a new root domain +* and lands here as well. Only a CPU going down needs the work below. +*/ +if (cpu_active (cpu_of (rq))) +return ; +/* Ensure any throttled groups are reachable by pick_next_task */ unthrottle_offline_cfs_rqs (rq);
Signature · 1 line
2.43.0
PATCH 2/2 sched/fair: Avoid re-syncing runtime_enabled on root domain changes
In reply to Qiliang Yuanrq_online_fair() calls update_runtime_enabled(), which walks every task group and takes each group's cfs_bandwidth lock to copy its quota state into the CPU's cfs_rq. A sched domain rebuild reaches it for every active CPU through rq_attach_root(), with the rq lock held and interrupts disabled.
The walk only matters when the cached state may be stale. While a CPU is online, tg_set_cfs_bandwidth() updates runtime_enabled on it under the rq lock, and it holds cpus_read_lock() so it cannot miss a CPU that is coming up. Only unthrottle_offline_cfs_rqs() clears runtime_enabled behind its back, and a CPU that has never been online has not been visited at all.
Track this in a per-rq flag that update_runtime_enabled() sets after the walk and unthrottle_offline_cfs_rqs() clears, and skip the walk while it is set.
With 4000 cpu cgroups on a 32-CPU VM booted with rt_group_sched=0, switching a 4-CPU cpuset to a partition root and offlining or onlining one CPU take (median of 20 partition switches and 16 hotplug operations):
operation before (ms) after (ms) change
partition root 7.68 1.21 -84% cpu offline 30.96 13.44 -57% cpu online 28.48 12.82 -55%
--- kernel/sched/fair.c | 11 +++++++++++ kernel/sched/sched.h | 2 ++ 2 files changed, 13 insertions(+)
kernel/sched/fair.c +11 -0
@@ -7798,6 +7798,15 @@ static void __maybe_unused update_runtime_enabled(struct rq *rq)lockdep_assert_rq_held (rq); +/* +* tg_set_cfs_bandwidth() keeps runtime_enabled up to date while the +* CPU is online, so only the first activation and the one following +* unthrottle_offline_cfs_rqs() need the walk. +*/ +if (rq->cfsb_runtime_valid) +return ; + rq->cfsb_runtime_valid = true; +guard (rcu)();list_for_each_entry_rcu (tg, &task_groups, list) {
@@ -7816,6 +7825,8 @@ static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq)lockdep_assert_rq_held (rq); + rq->cfsb_runtime_valid = false; +/* * The rq clock has already been updated in the * set_rq_offline(), so we should skip updating
kernel/sched/sched.h +2 -0
@@ -1412,6 +1412,8 @@ struct rq {#ifdef CONFIG_CFS_BANDWIDTH call_single_data_t cfsb_csd;struct list_head cfsb_csd_list; +/* cfs_rq->runtime_enabled is in sync with the quota of every group */ +bool cfsb_runtime_valid;#endif atomic_t nr_iowait;
Signature · 1 line
2.43.0
Peter Zijlstra
maintainerRe: [PATCH 2/2] sched/fair: Avoid re-syncing runtime_enabled on root domain changes
Quoted 17 lines · Qiliang Yuan
rq_online_fair()callsupdate_runtime_enabled(), which walks every task group and takes each group'scfs_bandwidthlock to copy its quota state into the CPU'scfs_rq. A sched domain rebuild reaches it for every active CPU throughrq_attach_root(), with the rq lock held and interrupts disabled.The walk only matters when the cached state may be stale. While a CPU is online,
tg_set_cfs_bandwidth()updatesruntime_enabledon it under the rq lock, and it holdscpus_read_lock()so it cannot miss a CPU that is coming up. Onlyunthrottle_offline_cfs_rqs()clearsruntime_enabledbehind its back, and a CPU that has never been online has not been visited at all.Track this in a per-rq flag that
update_runtime_enabled()sets after the walk andunthrottle_offline_cfs_rqs()clears, and skip the walk while it is set.
Quoted 8 lines
With 4000 cpu cgroups on a 32-CPU VM booted with rt_group_sched=0, switching a 4-CPU cpuset to a partition root and offlining or onlining one CPU take (median of 20 partition switches and 16 hotplug operations):
operation before (ms) after (ms) change
partition root 7.68 1.21 -84% cpu offline 30.96 13.44 -57% cpu online 28.48 12.82 -55%
How do you end up with 4k cgroups ?!? That's wild.
Also, why do you care about the rebuild performance? I'm not entirely sure it makes sense to add data for something that 'never' happens. Do we really need a new member variable for this?
Quoted 52 lines
Signed-off-by Qiliang Yuan <[email protected]>--- kernel/sched/fair.c | 11 +++++++++++ kernel/sched/sched.h | 2 ++ 2 files changed, 13 insertions(+)kernel/sched/fair.c +11
-0@@ -7798,6 +7798,15 @@ static void __maybe_unused update_runtime_enabled(struct rq *rq)lockdep_assert_rq_held (rq); +/* +* tg_set_cfs_bandwidth() keeps runtime_enabled up to date while the +* CPU is online, so only the first activation and the one following +* unthrottle_offline_cfs_rqs() need the walk. +*/ +if (rq->cfsb_runtime_valid) +return ; + rq->cfsb_runtime_valid = true; +guard (rcu)();list_for_each_entry_rcu (tg, &task_groups, list) {@@ -7816,6 +7825,8 @@ static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq)lockdep_assert_rq_held (rq); + rq->cfsb_runtime_valid = false; +/* * The rq clock has already been updated in the * set_rq_offline(), so we should skip updating kernel/sched/sched.h +2
-0@@ -1412,6 +1412,8 @@ struct rq {#ifdef CONFIG_CFS_BANDWIDTH call_single_data_t cfsb_csd;struct list_head cfsb_csd_list; +/* cfs_rq->runtime_enabled is in sync with the quota of every group */ +bool cfsb_runtime_valid;#endif atomic_t nr_iowait;Signature · 2 lines
-- 2.43.0
Re: [PATCH 2/2] sched/fair: Avoid re-syncing runtime_enabled on root domain changes
Quoted 1 line · Peter Zijlstra
How do you end up with 4k cgroups ?!? That's wild.
It is common on hosts running AI agent harnesses: every agent sandbox gets its own container, and each pod and container has a cgroup with the cpu controller enabled, so a single kernel easily carries over a thousand task groups. 4000 is the upper end of that, picked to show the scaling.
Quoted 3 lines
Also, why do you care about the rebuild performance? I'm not entirely sure it makes sense to add data for something that 'never' happens. Do we really need a new member variable for this?
On those setups it is not rare. Agent harnesses and KubeVirt both hot-add and remove vCPUs at run time to follow the load, and every hotplug rebuilds the sched domains of the guest.
A rebuild runs rq_attach_root() on every active CPU, and with the rq lock held and interrupts disabled it walks all task groups twice. With 4000 cpu cgroups on a 32-CPU VM, one rq_attach_root() call takes 188 us at the median and up to 938 us, against 2 us with both patches applied (20 partition switches, 1280 calls). So every rebuild leaves each CPU with interrupts off for close to a millisecond in the worst case.
The member is there because rq_online_fair() can't tell a rebuild from a CPU coming up: the CPU is active in both cases, but only the latter can have runtime_enabled out of date after unthrottle_offline_cfs_rqs().
Peter Zijlstra
maintainerRe: [PATCH 2/2] sched/fair: Avoid re-syncing runtime_enabled on root domain changes
Quoted 8 lines · Qiliang Yuan
Quoted 1 line · Peter Zijlstra
How do you end up with 4k cgroups ?!? That's wild.
It is common on hosts running AI agent harnesses: every agent sandbox gets its own container, and each pod and container has a cgroup with the cpu controller enabled, so a single kernel easily carries over a thousand task groups. 4000 is the upper end of that, picked to show the scaling.
*groan*
Quoted 7 lines
Quoted 3 lines
Also, why do you care about the rebuild performance? I'm not entirely sure it makes sense to add data for something that 'never' happens. Do we really need a new member variable for this?
On those setups it is not rare. Agent harnesses and KubeVirt both hot-add and remove vCPUs at run time to follow the load, and every hotplug rebuilds the sched domains of the guest.
So CPU hotplug is a slow path; there's nothing that's going to 'fix' that. If it hurts, its one of those things you need to stop doing.
Quoted 2 lines
A rebuild runs
rq_attach_root()on every active CPU, and with the rq lock held and interrupts disabled it walks all task groups twice.
Right, that is a bit daft. It should be relatively straight forward to make that one loop.
Quoted 5 lines
With 4000 cpu cgroups on a 32-CPU VM, one
rq_attach_root()call takes 188 us at the median and up to 938 us, against 2 us with both patches applied (20 partition switches, 1280 calls). So every rebuild leaves each CPU with interrupts off for close to a millisecond in the worst case.
Not ideal, but again, you did do hotplug so you get to keep the pieces.
Quoted 3 lines
The member is there because
rq_online_fair()can't tell a rebuild from a CPU coming up: the CPU is active in both cases, but only the latter can haveruntime_enabledout of date afterunthrottle_offline_cfs_rqs().
So the actual hotplug code obviously knows. It should be fairly simple to add a boolean argument to both functions and set the right value in rq_attach_root() and sched_set_rq_{on,off}line() respectively.
K Prateek Nayak
reviewerRe: [PATCH 2/2] sched/fair: Avoid re-syncing runtime_enabled on root domain changes
Hello Peter,
Quoted 5 lines · Peter Zijlstra
Quoted 2 lines
A rebuild runs
rq_attach_root()on every active CPU, and with the rq lock held and interrupts disabled it walks all task groups twice.Right, that is a bit daft. It should be relatively straight forward to make that one loop.
Question: Why do we need to do rq_offline_fair() + rq_online_fair() during cpuset for active CPUs since fair doesn't have any accounting linked to rd at all?
For deadline and RT it makes sense for bandwidth accounting and rto_mask updates on new rd but fair, and ext shouldn't really care for a rd change.
Would it make sense to separate the offline / online bits from a root domain switch? It won't speed up hotplug but it should speed up cpusets causing domain rebuilds.
Something like follows:
(Lightly tested on top of tip:sched/core at d7e670601af6)
kernel/sched/core.c +66 -22
@@ -8542,38 +8542,82 @@ static inline void balance_hotplug_wait(void)#endif /* !CONFIG_HOTPLUG_CPU */ -void set_rq_online (struct rq *rq) +void rq_attach_root_domain (struct rq *rq) { -if (!rq->online) { -const struct sched_class *class; -struct root_domain *rd; +struct root_domain *rd =rcu_dereference_root_domain (rq->rd); +const struct sched_class *class; - rd =rcu_dereference_root_domain (rq->rd); -cpumask_set_cpu (rq->cpu, rd->online); - rq->online =1 ; +/* Offline rq's are handled via set_rq_online(). */ +if (!rq->online) +return ; -for_each_class (class) { -if (class->rq_online) - class->rq_online (rq); - } +/* CPU already associated with the root domain. */ +if (cpumask_test_cpu (rq->cpu, rd->online)) +return ; + +cpumask_set_cpu (rq->cpu, rd->online); + +for_each_class (class) { +if (class->rq_attach_root) + class->rq_attach_root (rq); } } -void set_rq_offline (struct rq *rq) +void rq_detach_root_domain (struct rq *rq) { -if (rq->online) { -struct root_domain *rd =rcu_dereference_root_domain (rq->rd); -const struct sched_class *class; +struct root_domain *rd =rcu_dereference_root_domain (rq->rd); +const struct sched_class *class; -update_rq_clock (rq); -for_each_class (class) { -if (class->rq_offline) - class->rq_offline (rq); - } +/* CPU has already been handled via set_rq_offline(). */ +if (!rq->online) +return ; -cpumask_clear_cpu (rq->cpu, rd->online); - rq->online =0 ; +/* CPU is not associated with the root domain. */ +if (!cpumask_test_cpu (rq->cpu, rd->online)) +return ; + +update_rq_clock (rq); +for_each_class (class) { +if (class->rq_detach_root) + class->rq_detach_root (rq); } + +cpumask_clear_cpu (rq->cpu, rd->online); +} + +static void set_rq_online (struct rq *rq) +{ +const struct sched_class *class; +struct root_domain *rd; + +WARN_ON_ONCE (rq->online); + + rd =rcu_dereference_root_domain (rq->rd); +cpumask_set_cpu (rq->cpu, rd->online); + rq->online =1 ; + +update_rq_clock (rq); +for_each_class (class) { +if (class->rq_online) + class->rq_online (rq); + } +} + +static void set_rq_offline (struct rq *rq) +{ +struct root_domain *rd =rcu_dereference_root_domain (rq->rd); +const struct sched_class *class; + +WARN_ON_ONCE (!rq->online); + +update_rq_clock (rq); +for_each_class (class) { +if (class->rq_offline) + class->rq_offline (rq); + } + +cpumask_clear_cpu (rq->cpu, rd->online); + rq->online =0 ; }static inline void sched_set_rq_online (struct rq *rq,int cpu)
kernel/sched/deadline.c +16 -2
@@ -3366,7 +3366,7 @@ bool dl_task_needs_bw_move(struct task_struct *p, }/* Assumes rq->lock is held */ -static void rq_online_dl (struct rq *rq) +static void rq_attach_root_dl (struct rq *rq) {if (rq->dl.overloaded)dl_set_overload (rq);
@@ -3379,7 +3379,7 @@ static void rq_online_dl(struct rq *rq) }/* Assumes rq->lock is held */ -static void rq_offline_dl (struct rq *rq) +static void rq_detach_root_dl (struct rq *rq) {if (rq->dl.overloaded)dl_clear_overload (rq);
@@ -3387,6 +3387,17 @@ static void rq_offline_dl(struct rq *rq)cpudl_clear (&rcu_dereference_root_domain (rq->rd)->cpudl, rq->cpu, false); } +static void rq_online_dl (struct rq *rq) +{ +rq_attach_root_dl (rq); +} + +/* Assumes rq->lock is held */ +static void rq_offline_dl (struct rq *rq) +{ +rq_detach_root_dl (rq); +} +void __init init_sched_dl_class (void ) {unsigned int i;
@@ -3669,6 +3680,9 @@ DEFINE_SCHED_CLASS(dl) = { .task_woken = task_woken_dl, .find_lock_rq = find_lock_later_rq, + .rq_attach_root = rq_attach_root_dl, + .rq_detach_root = rq_detach_root_dl, + .task_tick = task_tick_dl, .task_fork = task_fork_dl,
kernel/sched/rt.c +15 -2
@@ -2398,7 +2398,7 @@ static void task_woken_rt(struct rq *rq, struct task_struct *p) }/* Assumes rq->lock is held */ -static void rq_online_rt (struct rq *rq) +static void rq_attach_root_rt (struct rq *rq) {if (rq->rt.overloaded)rt_set_overload (rq);
@@ -2409,7 +2409,7 @@ static void rq_online_rt(struct rq *rq) }/* Assumes rq->lock is held */ -static void rq_offline_rt (struct rq *rq) +static void rq_detach_root_rt (struct rq *rq) {if (rq->rt.overloaded)rt_clear_overload (rq);
@@ -2419,6 +2419,16 @@ static void rq_offline_rt(struct rq *rq)cpupri_set (&rcu_dereference_root_domain (rq->rd)->cpupri, rq->cpu,CPUPRI_INVALID ); } +static void rq_online_rt (struct rq *rq) +{ +rq_attach_root_rt (rq); +} + +static void rq_offline_rt (struct rq *rq) +{ +rq_detach_root_rt (rq); +} +/* * When switch from the rt queue, we bring ourselves to a position * that we might want to pull RT tasks from other runqueues.
@@ -2632,6 +2642,9 @@ DEFINE_SCHED_CLASS(rt) = { .switched_from = switched_from_rt, .find_lock_rq = find_lock_lowest_rq, + .rq_attach_root = rq_attach_root_rt, + .rq_detach_root = rq_detach_root_rt, + .task_tick = task_tick_rt, .get_rr_interval = get_rr_interval_rt,
kernel/sched/sched.h +8 -2
@@ -2791,6 +2791,12 @@ struct sched_class {void (*rq_online)(struct rq *rq);void (*rq_offline)(struct rq *rq); +/* +* rq_{detach,attach}_root: rq->lock +*/ +void (*rq_attach_root)(struct rq *rq); +void (*rq_detach_root)(struct rq *rq); +/* * push_cpu_stop: p->pi_lock && rq->lock */
@@ -3518,8 +3524,8 @@ static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)raw_spin_rq_unlock (rq1); } -extern void set_rq_online (struct rq *rq); -extern void set_rq_offline (struct rq *rq); +extern void rq_attach_root_domain (struct rq *rq); +extern void rq_detach_root_domain (struct rq *rq);extern bool sched_smp_initialized;
kernel/sched/topology.c +2 -5
@@ -494,9 +494,7 @@ void rq_attach_root(struct rq *rq, struct root_domain *rd) old_rd =rcu_dereference_root_domain (rq->rd);if (old_rd) { -if (cpumask_test_cpu (rq->cpu, old_rd->online)) -set_rq_offline (rq); - +rq_detach_root_domain (rq);cpumask_clear_cpu (rq->cpu, old_rd->span);/*
@@ -512,8 +510,7 @@ void rq_attach_root(struct rq *rq, struct root_domain *rd)rcu_assign_pointer (rq->rd, rd);cpumask_set_cpu (rq->cpu, rd->span); -if (cpumask_test_cpu (rq->cpu, cpu_active_mask)) -set_rq_online (rq); +rq_attach_root_domain (rq);/* * Because the rq is not a task, dl_add_task_root_domain() did not
Signature · 2 lines
Thanks and Regards, Prateek
Re: [PATCH 2/2] sched/fair: Avoid re-syncing runtime_enabled on root domain changes
Quoted 11 lines · K Prateek Nayak
Question: Why do we need to do
rq_offline_fair()+rq_online_fair()during cpuset for active CPUs since fair doesn't have any accounting linked to rd at all?For deadline and RT it makes sense for bandwidth accounting and
rto_maskupdates on new rd but fair, and ext shouldn't really care for a rd change.Would it make sense to separate the offline / online bits from a root domain switch? It won't speed up hotplug but it should speed up cpusets causing domain rebuilds.
[...]
Agreed, fair has nothing to redo when an active CPU only moves to a new root domain.
Peter, I have a v2 that follows your earlier suggestion: rq_online() and rq_offline() get a bool that says whether the CPU changes state, and fair skips both walks when it doesn't. Hotplug gets faster as well, since every hotplug rebuilds the domains and moves the other active CPUs. With 4000 cpu cgroups on a 32-CPU VM, switching a 4-CPU cpuset to a partition root and offlining or onlining one CPU take (median of 20 partition switches and 16 hotplug operations):
operation before (ms) after (ms) change
partition root 19.73 1.19 -94% cpu offline 44.93 12.83 -71% cpu online 37.03 12.94 -65%
Prateek's split should give the same results and keeps the root domain switch out of rq_online()/rq_offline(), which reads cleaner. One
difference: with it, a CPU coming up gets rq_online() from sched_set_rq_online() after the rebuild, while today and in my v2 it gets it from rq_attach_root() during the rebuild.
Which way would you prefer? I can post the v2 as it is, or rework it on top of Prateek's patch.
Thanks,
Qiliang