[PATCH 2/3] sched/numa: Don't migrate memory on stale placement state

From: Andrea Righi

Date: Tue Nov 24 2026 - 02:00:58 EST


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@xxxxxxxxxx>
---
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