Re: [PATCH 2/2] sched/fair: Avoid re-syncing runtime_enabled on root domain changes
From: K Prateek Nayak
Date: Fri Nov 13 2026 - 03:01:51 EST
Hello Peter,
On 11/12/2026 6:29 PM, Peter Zijlstra wrote:
>> 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)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 175b52a7ea79..679d094c6077 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -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)
diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
index 61b6a6d78171..f9c0a1a0eb9e 100644
--- a/kernel/sched/deadline.c
+++ b/kernel/sched/deadline.c
@@ -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,
diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c
index 81a71aa5f094..99cc1307d515 100644
--- a/kernel/sched/rt.c
+++ b/kernel/sched/rt.c
@@ -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,
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index a4df59c9c9b6..135eadefd0eb 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -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;
diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
index 99878da03bce..23711105696b 100644
--- a/kernel/sched/topology.c
+++ b/kernel/sched/topology.c
@@ -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
--
Thanks and Regards,
Prateek