mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andrea Righi <arighi@nvidia.com>
To: Peter Zijlstra <peterz@infradead.org>, Ingo Molnar <mingo@redhat.com>
Cc: Juri Lelli <juri.lelli@redhat.com>,
	Vincent Guittot <vincent.guittot@linaro.org>,
	Dietmar Eggemann <dietmar.eggemann@arm.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>,
	Valentin Schneider <vschneid@redhat.com>,
	K Prateek Nayak <kprateek.nayak@amd.com>,
	Tejun Heo <tj@kernel.org>, Vladimir Vdovin <deliran@verdict.gg>,
	Gregory Price <gourry@gourry.net>,
	David Dai <david.dai@linux.dev>,
	Balbir Singh <balbirs@nvidia.com>,
	Lee Trager <ltrager@nvidia.com>,
	linux-mm@kvack.org, sched-ext@lists.linux.dev,
	linux-kernel@vger.kernel.org
Subject: [PATCH 1/3] sched/numa: Ignore stale task placement state
Date: Tue, 24 Nov 2026 07:59:07 +0100	[thread overview]
Message-ID: <20261124070011.2498455-2-arighi@nvidia.com> (raw)
In-Reply-To: <20261124070011.2498455-1-arighi@nvidia.com>

NUMA balancing maintains the task placement state, such as
p->numa_preferred_nid, p->numa_faults and the cache-aware footprint
derived from them, from the hinting faults recorded by
task_numa_fault(). None of this state is reset when NUMA balancing is
disabled at runtime: task_numa_fault() bails out early and the state
simply stops being updated, so it can become arbitrarily stale. Some of
its users don't check whether NUMA balancing is enabled at all:

 - find_idlest_group() steers the slow wakeup path toward the preferred
   node when there is spare capacity at the NUMA level;

 - the fbq classification in the load balancer, based on
   rq->nr_numa_running and rq->nr_preferred_running, skips runqueues
   whose tasks look ideally placed according to their preferred node.

Gate both on the sched_numa_balancing static key, like the other users
of the placement state. For the fbq classification return the same
values as the !CONFIG_NUMA_BALANCING stubs, so a disabled NUMA balancing
behaves as if it was not built in. The static key can change between
the group and the runqueue classification of the same balancing pass,
but any combination only makes the "rt > fbq_type" skip less likely, so
no runqueue can be skipped incorrectly.

Keep updating nr_numa_running and nr_preferred_running at enqueue and
dequeue regardless: gating the accounting would leave the counters
unbalanced for tasks enqueued and dequeued on opposite sides of a
change.

Switching kernel.numa_balancing from 1 or 3 to 2 (memory tiering only)
has a similar issue. In that mode only the folios promoted from the
slower tiers feed the placement state, but top-tier PTEs armed before
the switch can still fault, and task_numa_fault() records them as if
NUMA_BALANCING_NORMAL was still set, possibly migrating the task.
Without NUMA_BALANCING_NORMAL, only record the faults that migrated the
folio: this drops the leftover faults and keeps the promotions.

Suggested-by: K Prateek Nayak <kprateek.nayak@amd.com>
Signed-off-by: Andrea Righi <arighi@nvidia.com>
---
 kernel/sched/fair.c | 33 +++++++++++++++++++++++++++------
 1 file changed, 27 insertions(+), 6 deletions(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 731d78155e654..46d3c927b8b58 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -4163,6 +4163,15 @@ void task_numa_fault(int last_cpupid, int mem_node, int pages, int flags)
 	     !cpupid_valid(last_cpupid)))
 		return;
 
+	/*
+	 * Without NUMA_BALANCING_NORMAL top-tier folios are not armed for
+	 * hinting faults, the only top-tier faults are the folios just
+	 * promoted from the slower tiers. Any other one was armed before
+	 * NUMA_BALANCING_NORMAL was cleared, don't let it drive placement.
+	 */
+	if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) && !migrated)
+		return;
+
 	/* Allocate buffer to track faults on a per-node basis */
 	if (unlikely(!p->numa_faults)) {
 		int size = sizeof(*p->numa_faults) *
@@ -12538,6 +12547,13 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 #ifdef CONFIG_NUMA_BALANCING
 static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs)
 {
+	/*
+	 * The preferred nodes behind nr_numa_running and nr_preferred_running
+	 * are not updated while NUMA balancing is disabled, don't classify on
+	 * stale values.
+	 */
+	if (!static_branch_likely(&sched_numa_balancing))
+		return all;
 	if (sgs->sum_h_nr_running > sgs->nr_numa_running)
 		return regular;
 	if (sgs->sum_h_nr_running > sgs->nr_preferred_running)
@@ -12547,6 +12563,8 @@ static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs)
 
 static inline enum fbq_type fbq_classify_rq(struct rq *rq)
 {
+	if (!static_branch_likely(&sched_numa_balancing))
+		return regular;
 	if (rq->nr_running > rq->nr_numa_running)
 		return regular;
 	if (rq->nr_running > rq->nr_preferred_running)
@@ -12848,17 +12866,20 @@ sched_balance_find_dst_group(struct sched_domain *sd, struct task_struct *p, int
 		if (sd->flags & SD_NUMA) {
 			int imb_numa_nr = sd->imb_numa_nr;
 #ifdef CONFIG_NUMA_BALANCING
-			int idlest_cpu;
 			/*
 			 * If there is spare capacity at NUMA, try to select
 			 * the preferred node
 			 */
-			if (cpu_to_node(this_cpu) == p->numa_preferred_nid)
-				return NULL;
+			if (static_branch_likely(&sched_numa_balancing)) {
+				int idlest_cpu;
+
+				if (cpu_to_node(this_cpu) == p->numa_preferred_nid)
+					return NULL;
 
-			idlest_cpu = cpumask_first(sched_group_span(idlest));
-			if (cpu_to_node(idlest_cpu) == p->numa_preferred_nid)
-				return idlest;
+				idlest_cpu = cpumask_first(sched_group_span(idlest));
+				if (cpu_to_node(idlest_cpu) == p->numa_preferred_nid)
+					return idlest;
+			}
 #endif /* CONFIG_NUMA_BALANCING */
 			/*
 			 * Otherwise, keep the task close to the wakeup source
-- 
2.56.1


  reply	other threads:[~2026-11-24  7:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  6:59 [PATCH 0/3] sched/numa: Don't act on stale NUMA " Andrea Righi
2026-11-24  6:59 ` Andrea Righi [this message]
2026-11-24  6:59 ` [PATCH 2/3] sched/numa: Don't migrate memory on stale " Andrea Righi
2026-11-24  6:59 ` [PATCH 3/3] sched/numa: Only pick a fair task as a swap partner Andrea Righi

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=20261124070011.2498455-2-arighi@nvidia.com \
    --to=arighi@nvidia.com \
    --cc=balbirs@nvidia.com \
    --cc=bsegall@google.com \
    --cc=david.dai@linux.dev \
    --cc=deliran@verdict.gg \
    --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=ltrager@nvidia.com \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=sched-ext@lists.linux.dev \
    --cc=tj@kernel.org \
    --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®