mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: K Prateek Nayak <kprateek.nayak@amd.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Qiliang Yuan <qiliang.yuan@linux.dev>
Cc: Ingo Molnar <mingo@redhat.com>,
	Juri Lelli <juri.lelli@redhat.com>,
	Vincent Guittot <vincent.guittot@linaro.org>,
	Dietmar Eggemann <dietmar.eggemann@arm.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>,
	Valentin Schneider <vschneid@redhat.com>,
	<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 2/2] sched/fair: Avoid re-syncing runtime_enabled on root domain changes
Date: Fri, 13 Nov 2026 11:57:54 +0530	[thread overview]
Message-ID: <e1a63a3c-3975-4b05-8ab4-518c37d867d6@amd.com> (raw)
In-Reply-To: <20261112125947.GZ1813469@noisy.programming.kicks-ass.net>

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


      reply	other threads:[~2026-11-13  6:41 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12  1:24 [PATCH 0/2] sched/fair: Cut per-group work from sched domain rebuilds Qiliang Yuan
2026-11-12  1:24 ` [PATCH 1/2] sched/fair: Skip rq_offline_fair() work for CPUs that stay active Qiliang Yuan
2026-11-12  1:24 ` [PATCH 2/2] sched/fair: Avoid re-syncing runtime_enabled on root domain changes Qiliang Yuan
2026-11-12 10:56   ` Peter Zijlstra
2026-11-12 11:10     ` Qiliang Yuan
2026-11-12 12:59       ` Peter Zijlstra
2026-11-13  6:27         ` K Prateek Nayak [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e1a63a3c-3975-4b05-8ab4-518c37d867d6@amd.com \
    --to=kprateek.nayak@amd.com \
    --cc=bsegall@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=juri.lelli@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=qiliang.yuan@linux.dev \
    --cc=rostedt@goodmis.org \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®