Re: [PATCH v4] sched/fair: Skip sched_balance_running cmpxchg when balance is not due
From: Srikar Dronamraju
Date: Wed Nov 12 2025 - 03:02:41 EST
* Tim Chen <tim.c.chen@xxxxxxxxxxxxxxx> [2025-11-10 10:47:35]:
> The NUMA sched domain sets the SD_SERIALIZE flag by default, allowing
> only one NUMA load balancing operation to run system-wide at a time.
>
> @@ -11757,17 +11772,26 @@ static int sched_balance_rq(int this_cpu, struct rq *this_rq,
> .fbq_type = all,
> .tasks = LIST_HEAD_INIT(env.tasks),
> };
> + bool need_unlock;
>
> 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)) {
> + goto out_balanced;
> + }
> + need_unlock = true;
> + }
> +
Moving the serialize check to sched_balance_rq is better since we only take
when its really needed. Previously we could have skipped the balancing for
the appropriate CPU.
> 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?
> goto redo;
Otherwise looks good to me.
Reviewed-by: Srikar Dronamraju <srikar@xxxxxxxxxxxxx>
--
Thanks and Regards
Srikar Dronamraju