mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Swapnil Sapkal <swapnil.sapkal@amd.com>
To: <peterz@infradead.org>, <mingo@redhat.com>,
	<juri.lelli@redhat.com>, <vincent.guittot@linaro.org>,
	<corbet@lwn.net>
Cc: <dietmar.eggemann@arm.com>, <rostedt@goodmis.org>,
	<bsegall@google.com>, <mgorman@suse.de>, <vschneid@redhat.com>,
	<iamjoonsoo.kim@lge.com>, <qyousef@layalina.io>,
	<sshegde@linux.ibm.com>, <alexs@kernel.org>,
	<lukasz.luba@arm.com>, <gautham.shenoy@amd.com>,
	<kprateek.nayak@amd.com>, <ravi.bangoria@amd.com>,
	<swapnil.sapkal@amd.com>, <linux-kernel@vger.kernel.org>,
	<linux-doc@vger.kernel.org>
Subject: [PATCH v2 2/6] sched/fair: Cleanup in migrate_degrades_locality() to improve readability
Date: Fri, 20 Dec 2024 06:32:20 +0000	[thread overview]
Message-ID: <20241220063224.17767-3-swapnil.sapkal@amd.com> (raw)
In-Reply-To: <20241220063224.17767-1-swapnil.sapkal@amd.com>

From: Peter Zijlstra <peterz@infradead.org>

migrate_degrade_locality() would return {1, 0, -1} respectively to
indicate that migration would degrade-locality, would improve
locality, would be ambivalent to locality improvements.

This patch improves readability by changing the return value to mean:
* Any positive value degrades locality
* 0 migration doesn't affect locality
* Any negative value improves locality

Link: https://lore.kernel.org/all/20230619094529.GL4253@hirez.programming.kicks-ass.net/

[Swapnil: Fixed comments around code and wrote commit log]

Not-yet-signed-off-by: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Swapnil Sapkal <swapnil.sapkal@amd.com>
---
 kernel/sched/fair.c | 41 +++++++++++++++++++++--------------------
 1 file changed, 21 insertions(+), 20 deletions(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index e66b10da7ac5..2ca3f098552c 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -9323,43 +9323,43 @@ static int task_hot(struct task_struct *p, struct lb_env *env)
 
 #ifdef CONFIG_NUMA_BALANCING
 /*
- * Returns 1, if task migration degrades locality
- * Returns 0, if task migration improves locality i.e migration preferred.
- * Returns -1, if task migration is not affected by locality.
+ * Returns a positive value, if task migration degrades locality.
+ * Returns 0, if task migration is not affected by locality.
+ * Returns a negative value, if task migration improves locality i.e migration preferred.
  */
-static int migrate_degrades_locality(struct task_struct *p, struct lb_env *env)
+static long migrate_degrades_locality(struct task_struct *p, struct lb_env *env)
 {
 	struct numa_group *numa_group = rcu_dereference(p->numa_group);
 	unsigned long src_weight, dst_weight;
 	int src_nid, dst_nid, dist;
 
 	if (!static_branch_likely(&sched_numa_balancing))
-		return -1;
+		return 0;
 
 	if (!p->numa_faults || !(env->sd->flags & SD_NUMA))
-		return -1;
+		return 0;
 
 	src_nid = cpu_to_node(env->src_cpu);
 	dst_nid = cpu_to_node(env->dst_cpu);
 
 	if (src_nid == dst_nid)
-		return -1;
+		return 0;
 
 	/* Migrating away from the preferred node is always bad. */
 	if (src_nid == p->numa_preferred_nid) {
 		if (env->src_rq->nr_running > env->src_rq->nr_preferred_running)
 			return 1;
 		else
-			return -1;
+			return 0;
 	}
 
 	/* Encourage migration to the preferred node. */
 	if (dst_nid == p->numa_preferred_nid)
-		return 0;
+		return -1;
 
 	/* Leaving a core idle is often worse than degrading locality. */
 	if (env->idle == CPU_IDLE)
-		return -1;
+		return 0;
 
 	dist = node_distance(src_nid, dst_nid);
 	if (numa_group) {
@@ -9370,14 +9370,14 @@ static int migrate_degrades_locality(struct task_struct *p, struct lb_env *env)
 		dst_weight = task_weight(p, dst_nid, dist);
 	}
 
-	return dst_weight < src_weight;
+	return src_weight - dst_weight;
 }
 
 #else
-static inline int migrate_degrades_locality(struct task_struct *p,
+static inline long migrate_degrades_locality(struct task_struct *p,
 					     struct lb_env *env)
 {
-	return -1;
+	return 0;
 }
 #endif
 
@@ -9387,7 +9387,7 @@ static inline int migrate_degrades_locality(struct task_struct *p,
 static
 int can_migrate_task(struct task_struct *p, struct lb_env *env)
 {
-	int tsk_cache_hot;
+	long degrades, hot;
 
 	lockdep_assert_rq_held(env->src_rq);
 	if (p->sched_task_hot)
@@ -9462,13 +9462,14 @@ int can_migrate_task(struct task_struct *p, struct lb_env *env)
 	if (env->flags & LBF_ACTIVE_LB)
 		return 1;
 
-	tsk_cache_hot = migrate_degrades_locality(p, env);
-	if (tsk_cache_hot == -1)
-		tsk_cache_hot = task_hot(p, env);
+	degrades = migrate_degrades_locality(p, env);
+	if (!degrades)
+		hot = task_hot(p, env);
+	else
+		hot = degrades > 0;
 
-	if (tsk_cache_hot <= 0 ||
-	    env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
-		if (tsk_cache_hot == 1)
+	if (!hot || env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
+		if (hot)
 			p->sched_task_hot = 1;
 		return 1;
 	}
-- 
2.43.0


  parent reply	other threads:[~2024-12-20  6:34 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-20  6:32 [PATCH v2 0/6] Fixes and improvements in /proc/schedstat Swapnil Sapkal
2024-12-20  6:32 ` [PATCH v2 1/6] sched/fair: Fix value reported by hot tasks pulled " Swapnil Sapkal
2024-12-24 18:54   ` [tip: sched/core] " tip-bot2 for Peter Zijlstra
2024-12-20  6:32 ` Swapnil Sapkal [this message]
2024-12-24 18:54   ` [tip: sched/core] sched/fair: Cleanup in migrate_degrades_locality() to improve readability tip-bot2 for Peter Zijlstra
2024-12-20  6:32 ` [PATCH v2 3/6] sched: Report the different kinds of imbalances in /proc/schedstat Swapnil Sapkal
2024-12-20 18:23   ` Shrikanth Hegde
2024-12-20 19:18     ` Shrikanth Hegde
2024-12-24 18:54   ` [tip: sched/core] " tip-bot2 for Swapnil Sapkal
2024-12-20  6:32 ` [PATCH v2 4/6] sched: Move sched domain name out of CONFIG_SCHED_DEBUG Swapnil Sapkal
2024-12-20 18:59   ` Shrikanth Hegde
2024-12-24  4:20     ` Sapkal, Swapnil
2024-12-24 18:54   ` [tip: sched/core] " tip-bot2 for Swapnil Sapkal
2024-12-20  6:32 ` [PATCH v2 5/6] sched/stats: Print domain name in /proc/schedstat Swapnil Sapkal
2024-12-24 18:54   ` [tip: sched/core] " tip-bot2 for K Prateek Nayak
2024-12-20  6:32 ` [PATCH v2 6/6] docs: Update Schedstat version to 17 Swapnil Sapkal
2024-12-20 19:02   ` Shrikanth Hegde
2024-12-23  7:35   ` Shrikanth Hegde
2024-12-24  5:40     ` Sapkal, Swapnil
2024-12-24 18:54   ` [tip: sched/core] " tip-bot2 for Swapnil Sapkal

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=20241220063224.17767-3-swapnil.sapkal@amd.com \
    --to=swapnil.sapkal@amd.com \
    --cc=alexs@kernel.org \
    --cc=bsegall@google.com \
    --cc=corbet@lwn.net \
    --cc=dietmar.eggemann@arm.com \
    --cc=gautham.shenoy@amd.com \
    --cc=iamjoonsoo.kim@lge.com \
    --cc=juri.lelli@redhat.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lukasz.luba@arm.com \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=qyousef@layalina.io \
    --cc=ravi.bangoria@amd.com \
    --cc=rostedt@goodmis.org \
    --cc=sshegde@linux.ibm.com \
    --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®