mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Qiliang Yuan <qiliang.yuan@linux.dev>
To: Ingo Molnar <mingo@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>,
	 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>,
	 K Prateek Nayak <kprateek.nayak@amd.com>
Cc: linux-kernel@vger.kernel.org, Qiliang Yuan <qiliang.yuan@linux.dev>
Subject: [PATCH v2 1/3] sched: Tell rq_online() and rq_offline() whether the CPU changes state
Date: Wed, 18 Nov 2026 19:57:21 -0500	[thread overview]
Message-ID: <20261118-bug-sched-fair-offline-active-v2-1-b6ede596c6a6@linux.dev> (raw)
In-Reply-To: <20261118-bug-sched-fair-offline-active-v2-0-b6ede596c6a6@linux.dev>

set_rq_online() and set_rq_offline() run both when a CPU comes up or
goes down and when a sched domain rebuild moves an active CPU to a new
root domain through rq_attach_root(). The class callbacks get the same
call in both cases.

Work such as resyncing CFS bandwidth state or unthrottling cfs_rqs is
only needed when the CPU actually changes state, but the callbacks have
no way to tell, so they repeat it for every active CPU on each rebuild.

Pass a hotplug argument down to the callbacks, and have
sched_set_rq_online() and sched_set_rq_offline() pass true.
rq_attach_root() passes false when taking a CPU offline, as
sched_cpu_deactivate() has already taken a CPU going down offline
before the rebuild. When putting a CPU online it passes true only if
the CPU was not online in its old root domain: sched_cpu_activate()
rebuilds the domains before calling sched_set_rq_online(), so this is
where a CPU coming up first goes online.

Suggested-by: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
 kernel/sched/core.c     | 12 ++++++------
 kernel/sched/deadline.c |  4 ++--
 kernel/sched/fair.c     |  4 ++--
 kernel/sched/rt.c       |  4 ++--
 kernel/sched/sched.h    |  8 ++++----
 kernel/sched/topology.c | 13 ++++++++++---
 6 files changed, 26 insertions(+), 19 deletions(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 8b4e4a6f4ae02..17ceb9d5046ba 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -8542,7 +8542,7 @@ static inline void balance_hotplug_wait(void)
 
 #endif /* !CONFIG_HOTPLUG_CPU */
 
-void set_rq_online(struct rq *rq)
+void set_rq_online(struct rq *rq, bool hotplug)
 {
 	if (!rq->online) {
 		const struct sched_class *class;
@@ -8554,12 +8554,12 @@ void set_rq_online(struct rq *rq)
 
 		for_each_class(class) {
 			if (class->rq_online)
-				class->rq_online(rq);
+				class->rq_online(rq, hotplug);
 		}
 	}
 }
 
-void set_rq_offline(struct rq *rq)
+void set_rq_offline(struct rq *rq, bool hotplug)
 {
 	if (rq->online) {
 		struct root_domain *rd = rcu_dereference_root_domain(rq->rd);
@@ -8568,7 +8568,7 @@ void set_rq_offline(struct rq *rq)
 		update_rq_clock(rq);
 		for_each_class(class) {
 			if (class->rq_offline)
-				class->rq_offline(rq);
+				class->rq_offline(rq, hotplug);
 		}
 
 		cpumask_clear_cpu(rq->cpu, rd->online);
@@ -8585,7 +8585,7 @@ static inline void sched_set_rq_online(struct rq *rq, int cpu)
 	rd = rcu_dereference_root_domain(rq->rd);
 	if (rd) {
 		BUG_ON(!cpumask_test_cpu(cpu, rd->span));
-		set_rq_online(rq);
+		set_rq_online(rq, true);
 	}
 	rq_unlock_irqrestore(rq, &rf);
 }
@@ -8599,7 +8599,7 @@ static inline void sched_set_rq_offline(struct rq *rq, int cpu)
 	rd = rcu_dereference_root_domain(rq->rd);
 	if (rd) {
 		BUG_ON(!cpumask_test_cpu(cpu, rd->span));
-		set_rq_offline(rq);
+		set_rq_offline(rq, true);
 	}
 	rq_unlock_irqrestore(rq, &rf);
 }
diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
index 61b6a6d781715..73f509b87b8fa 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_online_dl(struct rq *rq, bool hotplug)
 {
 	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_offline_dl(struct rq *rq, bool hotplug)
 {
 	if (rq->dl.overloaded)
 		dl_clear_overload(rq);
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 731d78155e654..db9ab8ce36500 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -15082,14 +15082,14 @@ void sched_balance_trigger(struct rq *rq)
 	nohz_balancer_kick(rq);
 }
 
-static void rq_online_fair(struct rq *rq)
+static void rq_online_fair(struct rq *rq, bool hotplug)
 {
 	update_sysctl();
 
 	update_runtime_enabled(rq);
 }
 
-static void rq_offline_fair(struct rq *rq)
+static void rq_offline_fair(struct rq *rq, bool hotplug)
 {
 	update_sysctl();
 
diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c
index 81a71aa5f094a..fc6224a343552 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_online_rt(struct rq *rq, bool hotplug)
 {
 	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_offline_rt(struct rq *rq, bool hotplug)
 {
 	if (rq->rt.overloaded)
 		rt_clear_overload(rq);
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index e957524c3ee52..5f7b6ab4dde6d 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -2796,8 +2796,8 @@ struct sched_class {
 	/*
 	 * sched_set_rq_{on,off}line: rq->lock
 	 */
-	void (*rq_online)(struct rq *rq);
-	void (*rq_offline)(struct rq *rq);
+	void (*rq_online)(struct rq *rq, bool hotplug);
+	void (*rq_offline)(struct rq *rq, bool hotplug);
 
 	/*
 	 * push_cpu_stop: p->pi_lock && rq->lock
@@ -3526,8 +3526,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 set_rq_online(struct rq *rq, bool hotplug);
+extern void set_rq_offline(struct rq *rq, bool hotplug);
 
 extern bool sched_smp_initialized;
 
diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
index 99878da03bce7..577dc012e5ac5 100644
--- a/kernel/sched/topology.c
+++ b/kernel/sched/topology.c
@@ -488,14 +488,16 @@ 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 was_online = false;
 	struct rq_flags rf;
 
 	rq_lock_irqsave(rq, &rf);
 
 	old_rd = rcu_dereference_root_domain(rq->rd);
 	if (old_rd) {
-		if (cpumask_test_cpu(rq->cpu, old_rd->online))
-			set_rq_offline(rq);
+		was_online = cpumask_test_cpu(rq->cpu, old_rd->online);
+		if (was_online)
+			set_rq_offline(rq, false);
 
 		cpumask_clear_cpu(rq->cpu, old_rd->span);
 
@@ -512,8 +514,13 @@ void rq_attach_root(struct rq *rq, struct root_domain *rd)
 	rcu_assign_pointer(rq->rd, rd);
 
 	cpumask_set_cpu(rq->cpu, rd->span);
+	/*
+	 * A CPU that comes up is first put online here, by the sched domain
+	 * rebuild of sched_cpu_activate(). Any other CPU only moves between
+	 * root domains.
+	 */
 	if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
-		set_rq_online(rq);
+		set_rq_online(rq, !was_online);
 
 	/*
 	 * Because the rq is not a task, dl_add_task_root_domain() did not

-- 
2.43.0


  reply	other threads:[~2026-11-19  0:57 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19  0:57 [PATCH v2 0/3] sched/fair: Cut per-group work from sched domain rebuilds Qiliang Yuan
2026-11-19  0:57 ` Qiliang Yuan [this message]
2026-11-19  0:57 ` [PATCH v2 2/3] sched/fair: Skip rq_offline_fair() work for CPUs that stay active Qiliang Yuan
2026-11-19  0:57 ` [PATCH v2 3/3] sched/fair: Avoid re-syncing runtime_enabled on root domain changes Qiliang Yuan

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=20261118-bug-sched-fair-offline-active-v2-1-b6ede596c6a6@linux.dev \
    --to=qiliang.yuan@linux.dev \
    --cc=bsegall@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=juri.lelli@redhat.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --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®