Re: [PATCH v3 2/3] sched: Keep CPUs that stay active online across root domain moves

From: Li kunyu

Date: Mon Nov 23 2026 - 02:25:33 EST




On 2026/11/21 10:57, Qiliang Yuan wrote:
> +void rq_attach_root_domain(struct rq *rq)
> +{
> + struct root_domain *rd = rcu_dereference_root_domain(rq->rd);
> + const struct sched_class *class;
> +
> + cpumask_set_cpu(rq->cpu, rd->online);
> +
> + for_each_class(class) {
> + if (class->rq_attach_root)
> + class->rq_attach_root(rq);
> + }
> +}
> +
> +void rq_detach_root_domain(struct rq *rq)
> +{
> + 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_detach_root)
> + class->rq_detach_root(rq);
> + }
> +
> + cpumask_clear_cpu(rq->cpu, rd->online);
> +}
> +
>
> diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
> index 99878da03bce7..cc2bc832dad1a 100644
> --- a/kernel/sched/topology.c
> +++ b/kernel/sched/topology.c
> @@ -488,14 +488,26 @@ static void free_rootdomain(struct rcu_head *rcu)
> void rq_attach_root(struct rq *rq, struct root_domain *rd)
> {
> struct root_domain *old_rd = NULL;
> + bool active, move = false;
> struct rq_flags rf;
>
> rq_lock_irqsave(rq, &rf);
>
> + active = cpumask_test_cpu(rq->cpu, cpu_active_mask);
> +
> old_rd = rcu_dereference_root_domain(rq->rd);
> if (old_rd) {
> - if (cpumask_test_cpu(rq->cpu, old_rd->online))
> - set_rq_offline(rq);
> + if (cpumask_test_cpu(rq->cpu, old_rd->online)) {
> + /*
> + * Keep an online CPU that stays active online and move
> + * only its root domain state.
> + */
> + move = active;
> + if (move)
> + rq_detach_root_domain(rq);
> + else
> + set_rq_offline(rq);

Check whether rq->cpu is active. This approach effectively prevents a series of
issues such as the active CPU going offline and then back online (after the rd
root domain is updated). It also invokes the abstraction function for rd root
domain updates implemented for RT and DL, thereby ensuring the overall logic
remains correct.

> + }
>
> @@ -512,7 +524,9 @@ 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))
> + if (move)
> + rq_attach_root_domain(rq);
> + else if (active)
> set_rq_online(rq);
>

By the way, does "base-commit: b251360cb2e6c7b74d962831f5797a4520511b36" point
to a specific development branch of the kernel (such as tip/sched/core)? The
code in the rq_attach_root function appears to differ slightly from what is
currently in mainline.

--
thanks,
kunyu