[PATCH 7/7] sched/fair: Clear idle_cpus_mask for CPUs with NULL sd

From: Pierre Gondois
Date: Wed Apr 03 2024 - 11:08:28 EST


As reported in [1], an isolated CPU keeps the values of:
- rq->nohz_tick_stopped
- nohz.idle_cpus_mask
- nohz.nr_cpus
when a NULL sd is attached to the CPU. Clear the values.

[1] https://lore.kernel.org/all/20230804090858.7605-1-rui.zhang@xxxxxxxxx/

Signed-off-by: Pierre Gondois <pierre.gondois@xxxxxxx>
---
include/linux/sched/nohz.h | 2 ++
kernel/sched/fair.c | 11 +++++++++++
kernel/sched/topology.c | 6 ++++--
3 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/include/linux/sched/nohz.h b/include/linux/sched/nohz.h
index 6d67e9a5af6b..18e620715c9d 100644
--- a/include/linux/sched/nohz.h
+++ b/include/linux/sched/nohz.h
@@ -9,8 +9,10 @@
#if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
extern void nohz_balance_enter_idle(int cpu);
extern int get_nohz_timer_target(void);
+extern void nohz_clear_state(int cpu);
#else
static inline void nohz_balance_enter_idle(int cpu) { }
+static void nohz_clear_state(int cpu) { }
#endif

#ifdef CONFIG_NO_HZ_COMMON
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 9657c8f2176b..6786d4d78e41 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -12014,6 +12014,17 @@ static void nohz_balancer_kick(struct rq *rq)
kick_ilb(flags);
}

+void nohz_clear_state(int cpu)
+{
+ struct rq *rq = cpu_rq(cpu);
+
+ if (rq->nohz_tick_stopped) {
+ rq->nohz_tick_stopped = 0;
+ cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
+ atomic_dec(&nohz.nr_cpus);
+ }
+}
+
static void set_cpu_sd_state_busy(int cpu)
{
struct sched_domain *sd;
diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
index b4fc212ccfb0..e8e40b7d964b 100644
--- a/kernel/sched/topology.c
+++ b/kernel/sched/topology.c
@@ -775,10 +775,12 @@ cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)

sched_domain_debug(sd, cpu);

- if (sd)
+ if (sd) {
housekeeping_runtime_set_cpu(cpu, HKR_TYPE_SCHED);
- else
+ } else {
housekeeping_runtime_clear_cpu(cpu, HKR_TYPE_SCHED);
+ nohz_clear_state(cpu);
+ }

rq_attach_root(rq, rd);
tmp = rq->sd;
--
2.25.1