Re: [PATCH v4] sched/fair: Skip sched_balance_running cmpxchg when balance is not due

From: Peter Zijlstra

Date: Wed Nov 12 2025 - 05:46:02 EST


On Wed, Nov 12, 2025 at 11:37:40AM +0100, Peter Zijlstra wrote:
> On Wed, Nov 12, 2025 at 01:32:23PM +0530, Srikar Dronamraju wrote:
> > > group = sched_balance_find_src_group(&env);
> > > if (!group) {
> > > schedstat_inc(sd->lb_nobusyg[idle]);
> > > @@ -11892,6 +11916,9 @@ static int sched_balance_rq(int this_cpu, struct rq *this_rq,
> > > if (!cpumask_subset(cpus, env.dst_grpmask)) {
> > > env.loop = 0;
> > > env.loop_break = SCHED_NR_MIGRATE_BREAK;
> > > + if (need_unlock)
> > > + atomic_set_release(&sched_balance_running, 0);
> > > +
> >
> > One nit:
> > While the current code is good, would conditionally resetting the
> > need_unlock just after resetting the atomic variable better than
> > unconditional reset that we do now?
>
> Right, I had the same thought when grabbed the patch yesterday, but
> ignored it.
>

Hmm, should we not redo while keeping the lock? Doesn't make much sense
to drop and try to reacquire things here.

So perhaps this is the better option -- or did I overlook something with
should_we_balance? It doesn't look like that will make a different
decision on the retry.

---
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -11717,26 +11717,25 @@ static int sched_balance_rq(int this_cpu
.fbq_type = all,
.tasks = LIST_HEAD_INIT(env.tasks),
};
- bool need_unlock;
+ bool need_unlock = false;

cpumask_and(cpus, sched_domain_span(sd), cpu_active_mask);

schedstat_inc(sd->lb_count[idle]);

-redo:
- need_unlock = false;
if (!should_we_balance(&env)) {
*continue_balancing = 0;
goto out_balanced;
}

if (sd->flags & SD_SERIALIZE) {
- if (atomic_cmpxchg_acquire(&sched_balance_running, 0, 1)) {
+ if (!atomic_try_cmpxchg_acquire(&sched_balance_running, 0, 1))
goto out_balanced;
- }
+
need_unlock = true;
}

+redo:
group = sched_balance_find_src_group(&env);
if (!group) {
schedstat_inc(sd->lb_nobusyg[idle]);
@@ -11861,9 +11860,6 @@ static int sched_balance_rq(int this_cpu
if (!cpumask_subset(cpus, env.dst_grpmask)) {
env.loop = 0;
env.loop_break = SCHED_NR_MIGRATE_BREAK;
- if (need_unlock)
- atomic_set_release(&sched_balance_running, 0);
-
goto redo;
}
goto out_all_pinned;