mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Qiliang Yuan <qiliang.yuan@linux.dev>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	 Juri Lelli <juri.lelli@redhat.com>,
	 Vincent Guittot <vincent.guittot@linaro.org>,
	Mel Gorman <mgorman@suse.de>,
	 K Prateek Nayak <kprateek.nayak@amd.com>,
	 David Hildenbrand <david@kernel.org>,
	 Andrew Morton <akpm@linux-foundation.org>
Cc: Dietmar Eggemann <dietmar.eggemann@arm.com>,
	 Steven Rostedt <rostedt@goodmis.org>,
	Ben Segall <bsegall@google.com>,
	 Valentin Schneider <vschneid@redhat.com>,
	Zi Yan <ziy@nvidia.com>,  Gregory Price <gourry@gourry.net>,
	 Ying Huang <ying.huang@linux.alibaba.com>,
	 Kefeng Wang <wangkefeng.wang@huawei.com>,
	linux-mm@kvack.org,  linux-kernel@vger.kernel.org,
	Qiliang Yuan <qiliang.yuan@linux.dev>
Subject: [PATCH 7/8] sched/fair: Avoid pulling tasks away from memory that cannot follow them
Date: Sun, 06 Dec 2026 06:38:44 -0500	[thread overview]
Message-ID: <20261206-feat-numab-series-v1-7-2af21ad24718@linux.dev> (raw)
In-Reply-To: <20261206-feat-numab-series-v1-0-2af21ad24718@linux.dev>

The load balancer evens out busy CPUs across NUMA nodes and only
reluctantly moves a task away from its preferred node:
migrate_degrades_locality() makes such a task cache hot, but once a
domain has failed to balance more often than cache_nice_tries the move
happens anyway, and active balancing does not ask at all.

For a task whose memory cannot follow it, a move to another package is
not undone by migration, so the task keeps paying for remote accesses or
is pulled back by NUMA placement only to be pushed out again.

Refuse to pull a task whose memory was found stuck out of the lowest
NUMA domain around its preferred node. A task that shares its CPU may
still leave, but only when no CPU of that domain is idle to take it
instead. Moves within the domain are left to the usual rules. The check
comes before the active balance shortcut in can_migrate_task(), so both
regular and active balancing honour it, consistent with the placement
side, and sched_balance_rq() does not wake the stopper for an active
balance that would be refused.

Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
 kernel/sched/fair.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 7fc5845324f4f..515775a8db6ee 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -10823,7 +10823,53 @@ static long migrate_degrades_locality(struct task_struct *p, struct lb_env *env)
 	return src_weight - dst_weight;
 }
 
+/* Return true if @p could run on an idle CPU in the lowest NUMA domain of @cpu */
+static bool numa_nearby_cpu_idle(struct task_struct *p, int cpu)
+{
+	struct sched_domain *sd;
+	int i;
+
+	sd = rcu_dereference_all(per_cpu(sd_numa, cpu));
+	if (!sd)
+		return false;
+
+	for_each_cpu_and(i, sched_domain_span(sd), p->cpus_ptr) {
+		if (idle_cpu(i))
+			return true;
+	}
+	return false;
+}
+
+/*
+ * Pulling a task whose memory cannot follow it out of the NUMA neighbourhood of
+ * that memory puts every access on a remote link. Keep it there unless its CPU
+ * is contended and no CPU in the neighbourhood is idle to take it instead;
+ * moves within the neighbourhood are left to the usual rules.
+ */
+static bool migrate_strands_memory(struct task_struct *p, struct lb_env *env)
+{
+	int pref = p->numa_preferred_nid;
+
+	if (!static_branch_likely(&sched_numa_balancing) ||
+	    !(env->sd->flags & SD_NUMA) || !p->numa_mem_stuck ||
+	    pref == NUMA_NO_NODE)
+		return false;
+
+	if (!numa_node_nearby(pref, cpu_to_node(env->src_cpu)) ||
+	    numa_node_nearby(pref, cpu_to_node(env->dst_cpu)))
+		return false;
+
+	return env->src_rq->cfs.h_nr_runnable <= 1 ||
+	       numa_nearby_cpu_idle(p, env->src_cpu);
+}
+
 #else /* !CONFIG_NUMA_BALANCING: */
+static inline bool migrate_strands_memory(struct task_struct *p,
+					  struct lb_env *env)
+{
+	return false;
+}
+
 static inline long migrate_degrades_locality(struct task_struct *p,
 					     struct lb_env *env)
 {
@@ -11298,6 +11344,9 @@ int can_migrate_task(struct task_struct *p, struct lb_env *env)
 	 * 3) task is cache cold, or
 	 * 4) too many balance attempts have failed.
 	 */
+	if (migrate_strands_memory(p, env))
+		return 0;
+
 	if (env->flags & LBF_ACTIVE_LB)
 		return !migrate_llc_task_wrong_dst(p, env);
 
@@ -14033,6 +14082,10 @@ static int sched_balance_rq(int this_cpu, struct rq *this_rq,
 		if (!cpumask_test_cpu(this_cpu, busiest->curr->cpus_ptr))
 			goto out_one_pinned;
 
+		/* Nor if can_migrate_task() would refuse to strand its memory */
+		if (migrate_strands_memory(busiest->curr, &env))
+			goto out_one_pinned;
+
 		/* Record that we found at least one task that could run on this_cpu */
 		env.flags &= ~LBF_ALL_PINNED;
 

-- 
2.43.0


  parent reply	other threads:[~2026-12-06 11:40 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-06 11:38 [PATCH 0/8] sched/numa: Placement signal for memory that NUMA balancing cannot migrate Qiliang Yuan
2026-12-06 11:38 ` [PATCH 1/8] sched/numa: Sample the accessed bits of pinned memory Qiliang Yuan
2026-12-06 11:38 ` [PATCH 2/8] sched/numa: Cover hugetlb mappings with accessed-bit sampling Qiliang Yuan
2026-12-06 11:38 ` [PATCH 3/8] sched/numa: Extend accessed-bit sampling to DAX mappings Qiliang Yuan
2026-12-06 11:38 ` [PATCH 4/8] mm/mempolicy: consult secondary MMUs when sampling accessed bits Qiliang Yuan
2026-12-06 11:38 ` [PATCH 5/8] sched/numa: Track whether the memory of a task can follow it Qiliang Yuan
2026-12-06 11:38 ` [PATCH 6/8] sched/numa: Keep tasks near memory that cannot follow them Qiliang Yuan
2026-12-06 11:38 ` Qiliang Yuan [this message]
2026-12-06 11:38 ` [PATCH 8/8] sched/numa: Share sampled accesses between the threads of a process Qiliang Yuan

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=20261206-feat-numab-series-v1-7-2af21ad24718@linux.dev \
    --to=qiliang.yuan@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=bsegall@google.com \
    --cc=david@kernel.org \
    --cc=dietmar.eggemann@arm.com \
    --cc=gourry@gourry.net \
    --cc=juri.lelli@redhat.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.com \
    --cc=wangkefeng.wang@huawei.com \
    --cc=ying.huang@linux.alibaba.com \
    --cc=ziy@nvidia.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®