[PATCH] sched/fair: Reorder variable reads in get_scan_cpumasks()
From: Li kunyu
Date: Fri Nov 27 2026 - 05:18:55 EST
get_scan_cpumasks() reads grp->cpu and curr_cpu, and derives nid via
cpu_to_node(), before the pref_nid == NUMA_NO_NODE check. However,
these values are only consumed after that check. When pref_nid is
NUMA_NO_NODE, the function exits early and discards them, so move the
reads past the check to avoid the redundant READ_ONCE(), cpu_to_node()
and task_cpu() on that path.
Signed-off-by: Li kunyu <likunyu@xxxxxxxxxxxxxx>
---
kernel/sched/fair.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index da0d9e19c457..6c0cae6c8eeb 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -1934,11 +1934,6 @@ static void get_scan_cpumasks(cpumask_var_t cpus, struct task_struct *p,
if (!static_branch_likely(&sched_numa_balancing))
goto out;
- cpu = READ_ONCE(grp->cpu);
- if (cpu != -1)
- nid = cpu_to_node(cpu);
- curr_cpu = task_cpu(p);
-
/*
* Scanning in the preferred NUMA node is ideal. However, the NUMA
* preferred node is per-task rather than per-process. It is possible
@@ -1954,6 +1949,10 @@ static void get_scan_cpumasks(cpumask_var_t cpus, struct task_struct *p,
if (pref_nid == NUMA_NO_NODE)
goto out;
+ cpu = READ_ONCE(grp->cpu);
+ if (cpu != -1)
+ nid = cpu_to_node(cpu);
+ curr_cpu = task_cpu(p);
cpumask_or(cpus, cpus, cpumask_of_node(pref_nid));
/* honor the task's preferred LLC CPU */
--
2.47.3