mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/3] sched/numa: Don't act on stale NUMA placement state
@ 2026-11-24  6:59 Andrea Righi
  2026-11-24  6:59 ` [PATCH 1/3] sched/numa: Ignore stale task " Andrea Righi
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Andrea Righi @ 2026-11-24  6:59 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar
  Cc: Juri Lelli, Vincent Guittot, Dietmar Eggemann, Steven Rostedt,
	Ben Segall, Mel Gorman, Valentin Schneider, K Prateek Nayak,
	Tejun Heo, Vladimir Vdovin, Gregory Price, David Dai,
	Balbir Singh, Lee Trager, linux-mm, sched-ext, linux-kernel

While working on NUMA balancing support for sched_ext [1][2], the review
pointed out that some of the NUMA balancing state a BPF scheduler would
read, p->numa_preferred_nid in particular, can be stale: NUMA balancing
stops updating it when it is disabled at runtime, but never resets it,
and parts of the fair class keep acting on it. The agreement was to
clean this up first, and then build the sched_ext support on top [3][4].

This series is that cleanup. It has no dependency on sched_ext and fixes
the fair class on its own; the sched_ext series will follow on top of
it.

NUMA balancing maintains the task placement state (the NUMA fault
statistics, the preferred node and what is derived from them) from the
hinting faults. With numa_balancing=1 and 3 (NUMA_BALANCING_NORMAL set)
all the scanned memory feeds it, while with numa_balancing=2 (memory
tiering only) top-tier memory is not scanned and only the folios
promoted from the slower tiers do.

Switching numa_balancing at runtime doesn't reset any of it, and:

 - with NUMA balancing disabled the state is not updated anymore, but
   find_idlest_group() and the fbq classification in the load balancer
   keep using the preferred nodes, as they don't check whether NUMA
   balancing is enabled;

 - PTEs armed for hinting faults before switching from 1 or 3 to 0 or 2
   can fault much later, and still update the placement state, migrate
   the task and migrate the folio between top-tier nodes based on that
   state, as if NUMA_BALANCING_NORMAL was still set.

Patch 1 gates find_idlest_group() and the fbq classification on the
sched_numa_balancing static key, like the other users of the placement
state, and makes task_numa_fault() drop the leftover top-tier faults
when NUMA_BALANCING_NORMAL is cleared, keeping the faults of the
promoted folios.

Patch 2 does the same for the memory side: should_numa_migrate_memory()
keeps promoting folios from the slower tiers, which only depends on the
memory tiering mode, but no longer migrates the other folios without
NUMA_BALANCING_NORMAL. It also stops a task_numa_work() queued before
NUMA balancing was disabled from arming new hinting faults.

Patch 3 fixes the choice of the swap partner when NUMA balancing
migrates a fair task: the task running on the destination CPU can be RT,
deadline or sched_ext, which fair should not move around and whose load
average isn't updated. Only pick a fair task as the partner. Vladimir
hit this with sched_ext in partial mode [5], and Peter suggested
checking for the fair class rather than special-casing sched_ext [6].

To see the effect of leftover hinting faults, I ran a single-threaded
task on node 1 with 1GB of memory first touched on node 0, on a 2-node
VM, and switched numa_balancing from 1 to 0 or 2 while its memory was
still migrating. Pages migrated in the 8 seconds after the switch:

                    1 -> 0     1 -> 2
  tip/sched/core    184158     114703
  this series            0          0

I also tested on a 3-node vng instance where node 2 is a slower,
CPU-less memory tier: its memory is marked as soft reserved at boot and
then onlined through dax/kmem, which puts it in a lower tier:

  vng --cpus 4 -m 6G \
      --numa 2G,cpus=0-1 --numa 2G,cpus=2-3 --numa 2G \
      --append 'memmap=2G%0x140000000-1+0xefffffff'
  # daxctl reconfigure-device --mode=system-ram all

In both tests the workload runs on node 1:

 - numa_balancing=2 only, memory on node 2, promotion rate limited so
   that the promotions span several scan passes: the promoted folios
   still feed the placement state as before (preferred node 1, same
   number of recorded faults as tip/sched/core);

 - numa_balancing 3 -> 2 with half the memory on node 0 and half on
   node 2, switching while the node 0 memory is still migrating: in the
   10 seconds after the switch, tip/sched/core keeps migrating ~40000
   pages from node 0, with this series node 0 keeps the same page count,
   and in both cases the node 2 memory keeps being promoted.

With numa_balancing=1 the memory migrates as before.

[1] v1: https://lore.kernel.org/r/20261004072901.3579967-1-arighi@nvidia.com
[2] v2: https://lore.kernel.org/r/20261027041536.3317971-1-arighi@nvidia.com
[3] https://lore.kernel.org/r/20261027105056.GE2668564@noisy.programming.kicks-ass.net
[4] https://lore.kernel.org/r/20261027115410.GK2668564@noisy.programming.kicks-ass.net
[5] https://lore.kernel.org/r/20261002124559.10367-1-deliran@verdict.gg
[6] https://lore.kernel.org/r/20261027104317.GC2668564@noisy.programming.kicks-ass.net

Git tree:
git://git.kernel.org/pub/scm/linux/kernel/git/arighi/linux.git sched-numa-fix-stale-state

Andrea Righi (3):
  sched/numa: Ignore stale task placement state
  sched/numa: Don't migrate memory on stale placement state
  sched/numa: Only pick a fair task as a swap partner

 kernel/sched/fair.c | 57 +++++++++++++++++++++++++++++++++++++++------
 1 file changed, 50 insertions(+), 7 deletions(-)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] sched/numa: Ignore stale task placement state
  2026-11-24  6:59 [PATCH 0/3] sched/numa: Don't act on stale NUMA placement state Andrea Righi
@ 2026-11-24  6:59 ` Andrea Righi
  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
  2 siblings, 0 replies; 4+ messages in thread
From: Andrea Righi @ 2026-11-24  6:59 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar
  Cc: Juri Lelli, Vincent Guittot, Dietmar Eggemann, Steven Rostedt,
	Ben Segall, Mel Gorman, Valentin Schneider, K Prateek Nayak,
	Tejun Heo, Vladimir Vdovin, Gregory Price, David Dai,
	Balbir Singh, Lee Trager, linux-mm, sched-ext, linux-kernel

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/3] sched/numa: Don't migrate memory on stale placement state
  2026-11-24  6:59 [PATCH 0/3] sched/numa: Don't act on stale NUMA placement state Andrea Righi
  2026-11-24  6:59 ` [PATCH 1/3] sched/numa: Ignore stale task " Andrea Righi
@ 2026-11-24  6:59 ` Andrea Righi
  2026-11-24  6:59 ` [PATCH 3/3] sched/numa: Only pick a fair task as a swap partner Andrea Righi
  2 siblings, 0 replies; 4+ messages in thread
From: Andrea Righi @ 2026-11-24  6:59 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar
  Cc: Juri Lelli, Vincent Guittot, Dietmar Eggemann, Steven Rostedt,
	Ben Segall, Mel Gorman, Valentin Schneider, K Prateek Nayak,
	Tejun Heo, Vladimir Vdovin, Gregory Price, David Dai,
	Balbir Singh, Lee Trager, linux-mm, sched-ext, linux-kernel

When NUMA balancing is disabled or NUMA_BALANCING_NORMAL is cleared at
runtime, the memory side of NUMA balancing doesn't stop right away:

 - PTEs and PMDs armed for hinting faults before the change can fault
   much later, and should_numa_migrate_memory() then migrates the folio
   based on the last cpupid, the numa_group statistics and
   p->numa_preferred_nid, as if NUMA_BALANCING_NORMAL was still set.

 - A task_numa_work() queued before NUMA balancing is disabled still
   runs a full scan pass, arming the folios on the slower memory tiers
   for hinting faults.

In should_numa_migrate_memory(), keep the promotion of folios from the
slower tiers, which only depends on the memory tiering mode and the
fault latency, and only migrate the other folios with
NUMA_BALANCING_NORMAL set, so that a leftover hinting fault just
restores the mapping. This covers a disabled NUMA balancing as well,
since the mode is 0 then.

Skip the scan in task_numa_work() when sched_numa_balancing is disabled,
after re-arming the work, so it can be queued again when NUMA balancing
is re-enabled. The memory tiering mode alone still needs the scan to
promote folios, so it is not affected.

Signed-off-by: Andrea Righi <arighi@nvidia.com>
---
 kernel/sched/fair.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 46d3c927b8b58..5f420141c3026 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -2910,6 +2910,15 @@ bool should_numa_migrate_memory(struct task_struct *p, struct folio *folio,
 		return !numa_promotion_rate_limit(pgdat, rate_limit, nr);
 	}
 
+	/*
+	 * Past this point the folio is migrated based on the task placement
+	 * state, which only NUMA_BALANCING_NORMAL does. If it was cleared,
+	 * or NUMA balancing disabled, since this hinting fault was armed, just
+	 * restore the mapping.
+	 */
+	if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL))
+		return false;
+
 	this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid);
 	last_cpupid = folio_xchg_last_cpupid(folio, this_cpupid);
 
@@ -4313,6 +4322,13 @@ static void task_numa_work(struct callback_head *work)
 	if (p->flags & PF_EXITING)
 		return;
 
+	/*
+	 * NUMA balancing may have been disabled after this work was queued,
+	 * don't arm new hinting faults.
+	 */
+	if (!static_branch_likely(&sched_numa_balancing))
+		return;
+
 	/*
 	 * Memory is pinned to only one NUMA node via cpuset.mems, naturally
 	 * no page can be migrated.
-- 
2.56.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 3/3] sched/numa: Only pick a fair task as a swap partner
  2026-11-24  6:59 [PATCH 0/3] sched/numa: Don't act on stale NUMA placement state Andrea Righi
  2026-11-24  6:59 ` [PATCH 1/3] sched/numa: Ignore stale task " Andrea Righi
  2026-11-24  6:59 ` [PATCH 2/3] sched/numa: Don't migrate memory on stale " Andrea Righi
@ 2026-11-24  6:59 ` Andrea Righi
  2 siblings, 0 replies; 4+ messages in thread
From: Andrea Righi @ 2026-11-24  6:59 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar
  Cc: Juri Lelli, Vincent Guittot, Dietmar Eggemann, Steven Rostedt,
	Ben Segall, Mel Gorman, Valentin Schneider, K Prateek Nayak,
	Tejun Heo, Vladimir Vdovin, Gregory Price, David Dai,
	Balbir Singh, Lee Trager, linux-mm, sched-ext, linux-kernel

When NUMA balancing migrates a fair task, task_numa_compare() considers
the task currently running on the destination CPU as a swap partner and,
if it picks it, migrate_swap() moves that task to the source CPU.

The task running on the destination CPU may belong to a different
scheduling class: an RT or deadline task, or, with sched_ext in partial
mode, a task owned by a BPF scheduler. Moving such a task is wrong for
two reasons:

 - its placement is driven by its own class (push/pull for RT and
   deadline, the BPF scheduler for sched_ext), which fair would override;

 - the swap is weighed on task_h_load(cur), but the load average of a
   task outside the fair class isn't updated, so the decision is based on
   whatever load the task had when it left fair.

Treat a task outside the fair class like a kthread: never swap it, but
still allow the migrating task to move to the destination CPU alone.

Fixes: fb13c7ee0ed3 ("sched/numa: Use a system-wide search to find swap/migration candidates")
Suggested-by: Peter Zijlstra <peterz@infradead.org>
Reported-by: Vladimir Vdovin <deliran@verdict.gg>
Link: https://lore.kernel.org/r/20261002124559.10367-1-deliran@verdict.gg
Signed-off-by: Andrea Righi <arighi@nvidia.com>
---
 kernel/sched/fair.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 5f420141c3026..fa0cfd48a133f 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -3223,8 +3223,14 @@ static bool task_numa_compare(struct task_numa_env *env,
 
 	rcu_read_lock();
 	cur = rcu_dereference_all(dst_rq->curr);
+	/*
+	 * Only swap with a fair task: the placement of a task in any other
+	 * class is driven by that class, and its load, which the swap is
+	 * weighed on, isn't tracked by fair. Moving the task alone to the
+	 * destination is still allowed.
+	 */
 	if (cur && ((cur->flags & (PF_EXITING | PF_KTHREAD)) ||
-		    !cur->mm))
+		    !cur->mm || cur->sched_class != &fair_sched_class))
 		cur = NULL;
 
 	/*
-- 
2.56.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-24  7:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-24  6:59 [PATCH 0/3] sched/numa: Don't act on stale NUMA placement state Andrea Righi
2026-11-24  6:59 ` [PATCH 1/3] sched/numa: Ignore stale task " Andrea Righi
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

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®