[PATCH] sched/numa: Fix scan period adaptation and candidate

From: Herman Semenoff

Date: Tue Nov 24 2026 - 04:02:35 EST


In update_task_scan_period(), commit 37ec97deb3a8 ("sched/numa: Slow down
scan rate if shared faults dominate") intended to increase the scan
period when either memory is already predominantly local or when accesses
are shared across tasks, while accelerating scanning when private faults
occur on remote memory pages

However, the implementation suffered from several severe inversions:

1. ps_ratio (private / (private + shared)) and lr_ratio (local /
(local + remote)) were transposed in the conditional branches. The
first branch checked ps_ratio >= NUMA_PERIOD_THRESHOLD under the
comment "Most memory accesses are local", causing any task with
predominantly private memory residing on a remote NUMA node to slow
down scanning instead of accelerating it

2. The shared-fault check tested lr_ratio >= NUMA_PERIOD_THRESHOLD
instead of checking whether shared accesses dominate

3. In the fallback branch intended to speed up scanning, computing
diff = -(NUMA_PERIOD_THRESHOLD - max(lr_ratio, ps_ratio)) * period_slot
resulted in a positive diff whenever ps_ratio exceeded the threshold,
further slowing down scanning instead of reducing the period

4. When local + remote == 0 or private + shared == 0, divisions by zero
could occur

In addition, in task_numa_migrate(), the candidate node loop over
for_each_node_state(nid, N_CPU) computed dist = node_distance(env.src_nid,
env.dst_nid) before setting env.dst_nid = nid, querying distance to the
previous node instead of the candidate node nid.

Fix both issues by:
- Correctly checking lr_ratio >= NUMA_PERIOD_THRESHOLD to slow down
scanning when memory is predominantly local
- Checking ps_ratio <= (NUMA_PERIOD_SLOTS - NUMA_PERIOD_THRESHOLD) to
slow down scanning when memory accesses are predominantly shared
- Accelerating scanning via diff = -(NUMA_PERIOD_THRESHOLD - lr_ratio) *
period_slot when private memory is remote
- Guarding against zero fault totals
- Using node_distance(env.src_nid, nid) in task_numa_migrate()

Benchmark results on server motherboard LGA 2011-3 Lenovo RD450X (2x
Intel Xeon E5-2699 v3 (Haswell), 72 CPUs,
2 NUMA nodes, SLIT distance 10 / 21):
- test 'perf bench numa mem' (36 threads, 2 nodes):
Throughput: 16.165 GB/sec -> 27.247 GB/sec (+68.5%)
Latency: 2.227 ns/byte -> 1.321 ns/byte (-40.6%)
- test 'perf bench sched messaging':
No regression (1.783s vs 1.802s)

Signed-off-by: Herman Semenoff <GermanAizek@xxxxxxxxx>
Signed-off-by: Herman Semenoff <GermanAizek@xxxxxxxxxxxxx>
---
kernel/sched/fair.c | 18 ++++++++++--------
1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 69145dda0df5..6d77ea36a416 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -3371,7 +3371,7 @@ static int task_numa_migrate(struct task_struct *p)
if (nid == env.src_nid || nid == p->numa_preferred_nid)
continue;

- dist = node_distance(env.src_nid, env.dst_nid);
+ dist = node_distance(env.src_nid, nid);
if (sched_numa_topology_type == NUMA_BACKPLANE &&
dist != env.dist) {
taskweight = task_weight(p, env.src_nid, dist);
@@ -3514,7 +3514,8 @@ static void update_task_scan_period(struct task_struct *p,
* migration then it implies we are migrating too quickly or the local
* node is overloaded. In either case, scan slower
*/
- if (local + shared == 0 || p->numa_faults_locality[2]) {
+ if (local + remote == 0 || private + shared == 0 ||
+ local + shared == 0 || p->numa_faults_locality[2]) {
p->numa_scan_period = min(p->numa_scan_period_max,
p->numa_scan_period << 1);

@@ -3534,22 +3535,24 @@ static void update_task_scan_period(struct
task_struct *p,
lr_ratio = (local * NUMA_PERIOD_SLOTS) / (local + remote);
ps_ratio = (private * NUMA_PERIOD_SLOTS) / (private + shared);

- if (ps_ratio >= NUMA_PERIOD_THRESHOLD) {
+ if (lr_ratio >= NUMA_PERIOD_THRESHOLD) {
/*
* Most memory accesses are local. There is no need to
* do fast NUMA scanning, since memory is already local.
*/
- int slot = ps_ratio - NUMA_PERIOD_THRESHOLD;
+ int slot = lr_ratio - NUMA_PERIOD_THRESHOLD;
+
if (!slot)
slot = 1;
diff = slot * period_slot;
- } else if (lr_ratio >= NUMA_PERIOD_THRESHOLD) {
+ } else if (ps_ratio <= (NUMA_PERIOD_SLOTS - NUMA_PERIOD_THRESHOLD)) {
/*
* Most memory accesses are shared with other tasks.
* There is no point in continuing fast NUMA scanning,
* since other tasks may just move the memory elsewhere.
*/
- int slot = lr_ratio - NUMA_PERIOD_THRESHOLD;
+ int slot = (NUMA_PERIOD_SLOTS - NUMA_PERIOD_THRESHOLD) - ps_ratio;
+
if (!slot)
slot = 1;
diff = slot * period_slot;
@@ -3559,8 +3562,7 @@ static void update_task_scan_period(struct task_struct *p,
* yet they are not on the local NUMA node. Speed up
* NUMA scanning to get the memory moved over.
*/
- int ratio = max(lr_ratio, ps_ratio);
- diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot;
+ diff = -(NUMA_PERIOD_THRESHOLD - lr_ratio) * period_slot;
}

p->numa_scan_period = clamp(p->numa_scan_period + diff,
--
2.55.0