All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Srikar Dronamraju <srikar@linux.ibm.com>,
	Linus Torvalds <torvalds@linux-foundation.org>
Cc: Tim Chen <tim.c.chen@linux.intel.com>,
	Ingo Molnar <mingo@kernel.org>, Chen Yu <yu.c.chen@intel.com>,
	Doug Nelson <doug.nelson@intel.com>,
	Mohini Narkhede <mohini.narkhede@intel.com>,
	linux-kernel@vger.kernel.org,
	Vincent Guittot <vincent.guittot@linaro.org>,
	Shrikanth Hegde <sshegde@linux.ibm.com>,
	K Prateek Nayak <kprateek.nayak@amd.com>
Subject: Re: [PATCH v4] sched/fair: Skip sched_balance_running cmpxchg when balance is not due
Date: Wed, 12 Nov 2025 11:37:40 +0100	[thread overview]
Message-ID: <20251112103740.GF4067720@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <aRQ_D1vyNfGVo-xK@linux.ibm.com>

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.

But perhaps something like so?

--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -11717,14 +11717,20 @@ 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]);
 
+	if (0) {
 redo:
-	need_unlock = false;
+		if (need_unlock) {
+			atomic_set_release(&sched_balance_running, 0);
+			need_unlock = false;
+		}
+	}
+
 	if (!should_we_balance(&env)) {
 		*continue_balancing = 0;
 		goto out_balanced;
@@ -11861,9 +11867,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;


---

The other option is something like this, but Linus hated on this pattern
when we started with things.

---
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -11692,6 +11692,8 @@ static void update_lb_imbalance_stat(str
  */
 static atomic_t sched_balance_running = ATOMIC_INIT(0);
 
+DEFINE_FREE(balance_unlock, atomic_t *, if (_T) atomic_set_release(_T, 0));
+
 /*
  * Check this_cpu to ensure it is balanced within domain. Attempt to move
  * tasks if there is an imbalance.
@@ -11717,24 +11719,23 @@ static int sched_balance_rq(int this_cpu
 		.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;
+		return 0;
 	}
 
+	atomic_t *lock __free(balance_unlock) = NULL;
 	if (sd->flags & SD_SERIALIZE) {
-		if (atomic_cmpxchg_acquire(&sched_balance_running, 0, 1)) {
-			goto out_balanced;
-		}
-		need_unlock = true;
+		if (!atomic_try_cmpxchg_acquire(&sched_balance_running, 0, 1))
+			return 0;
+
+		lock = &sched_balance_running;
 	}
 
 	group = sched_balance_find_src_group(&env);
@@ -11861,9 +11862,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;
@@ -11980,9 +11978,6 @@ static int sched_balance_rq(int this_cpu
 	    sd->balance_interval < sd->max_interval)
 		sd->balance_interval *= 2;
 out:
-	if (need_unlock)
-		atomic_set_release(&sched_balance_running, 0);
-
 	return ld_moved;
 }
 

  reply	other threads:[~2025-11-12 10:37 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-10 18:47 [PATCH v4] sched/fair: Skip sched_balance_running cmpxchg when balance is not due Tim Chen
2025-11-11  6:24 ` Shrikanth Hegde
2025-11-12  8:02 ` Srikar Dronamraju
2025-11-12 10:37   ` Peter Zijlstra [this message]
2025-11-12 10:45     ` Peter Zijlstra
2025-11-12 11:09       ` Shrikanth Hegde
2025-11-12 11:21         ` Peter Zijlstra
2025-11-12 21:10           ` Tim Chen
2025-11-13  4:25             ` Shrikanth Hegde
2025-11-13 17:49               ` Tim Chen
2025-11-12 11:25       ` Srikar Dronamraju
2025-11-12 13:39         ` Peter Zijlstra
2025-11-12 13:44           ` Peter Zijlstra
2025-11-12 16:02           ` Srikar Dronamraju
2025-11-12 10:53     ` Shrikanth Hegde
2025-11-14 12:19 ` [tip: sched/core] " tip-bot2 for Tim Chen
2025-11-15 20:56   ` Shrikanth Hegde
2025-11-17 18:55     ` Tim Chen
2025-11-17 19:00       ` K Prateek Nayak
2025-11-27 14:09         ` Peter Zijlstra
2025-11-18  9:54       ` Peter Zijlstra
2025-11-18  9:56         ` Peter Zijlstra
2025-11-21  6:26         ` Nathan Chancellor
2025-11-21  9:00           ` Peter Zijlstra
2025-11-17 19:06     ` Borislav Petkov

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=20251112103740.GF4067720@noisy.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=doug.nelson@intel.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=mohini.narkhede@intel.com \
    --cc=srikar@linux.ibm.com \
    --cc=sshegde@linux.ibm.com \
    --cc=tim.c.chen@linux.intel.com \
    --cc=torvalds@linux-foundation.org \
    --cc=vincent.guittot@linaro.org \
    --cc=yu.c.chen@intel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.