sched_ext: Fix inconsistent NUMA node lookup in scx_select_cpu_dfl()
Subsystems (3)
PATCH 1/1 sched_ext: Fix inconsistent NUMA node lookup in scx_select_cpu_dfl()
In the WAKE_SYNC path of scx_select_cpu_dfl(), waker_node was computed with cpu_to_node(), while node (for prev_cpu) was computed with scx_cpu_node_if_enabled(). When scx_builtin_idle_per_node is disabled, node is NUMA_NO_NODE but waker_node would be the actual NUMA node, causing two issues:
1. The (waker_node == node) check always fails when SCX_PICK_IDLE_IN_NODE is set, preventing the waker CPU optimization from ever triggering.
2. idle_cpumask(waker_node) is called with a real node ID even though per-node idle tracking is disabled, resulting in undefined behavior.
Fix by using scx_cpu_node_if_enabled() for waker_node as well, ensuring both variables are computed consistently.
--- kernel/sched/ext_idle.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/ext_idle.c +1 -1
@@ -543,7 +543,7 @@ s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags,* piled up on it even if there is an idle core elsewhere on * the system. */ - waker_node =cpu_to_node (cpu); + waker_node =scx_cpu_node_if_enabled (cpu); if (!(current->flags & PF_EXITING) && cpu_rq(cpu)->scx.local_dsq.nr == 0 && (!(flags & SCX_PICK_IDLE_IN_NODE) || (waker_node == node)) &&
Signature · 1 line
2.48.1
Hi Cheng-Yang,
Quoted 8 lines · Cheng-Yang Chou
In the
WAKE_SYNCpath ofscx_select_cpu_dfl(),waker_nodewas computed withcpu_to_node(), while node (forprev_cpu) was computed withscx_cpu_node_if_enabled(). Whenscx_builtin_idle_per_nodeis disabled, node isNUMA_NO_NODEbutwaker_nodewould be the actual NUMA node, causing two issues:1. The (
waker_node== node) check always fails whenSCX_PICK_IDLE_IN_NODEis set, preventing the waker CPU optimization from ever triggering.
When scx_builtin_idle_per_node is disabled, SCX_PICK_IDLE_IN_NODE won't be set, which means !(flags & SCX_PICK_IDLE_IN_NODE) should be always true, short-circuiting the ||, and the waker_node == node comparison is never evaluated.
However, ...
Quoted 2 lines
2.
idle_cpumask(waker_node)is called with a real node ID even though per-node idle tracking is disabled, resulting in undefined behavior.
...this looks like a legit bug. I'm wondering how this fix impacts performance, will do some testing. Nice catch!
Quoted 6 lines
Fix by using
scx_cpu_node_if_enabled()forwaker_nodeas well, ensuring both variables are computed consistently.Fixes 48849271e6611 ("sched_ext: idle: Per-node idle cpumasks")Signed-off-by Cheng-Yang Chou <[email protected]>
We should also add:
Cc: [email protected] # v6.15+
Thanks!
-Andrea
Quoted 20 lines
--- kernel/sched/ext_idle.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/sched/ext_idle.c +1
-1@@ -543,7 +543,7 @@ s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags,* piled up on it even if there is an idle core elsewhere on * the system. */ - waker_node =cpu_to_node (cpu); + waker_node =scx_cpu_node_if_enabled (cpu); if (!(current->flags & PF_EXITING) && cpu_rq(cpu)->scx.local_dsq.nr == 0 && (!(flags & SCX_PICK_IDLE_IN_NODE) || (waker_node == node)) &&Signature · 1 line
2.48.1
Hello,
Applied to sched_ext/for-7.0-fixes with the following additions:
Thanks.
--
tejun
Hi Tejun, Andrea,
Quoted 11 lines · Tejun Heo
Hello,
Applied to sched_ext/for-7.0-fixes with the following additions:
Cc [email protected] # v6.15+Reviewed-by Andrea Righi <[email protected]>Thanks.
--
tejun
Thanks for reviewing and applying the patch!
Regarding Andrea's feedback on the commit message, thanks for pointinig out the short-circuiting behavior. Since the patch is already applied to sched_ext/for-7.0-fixes, how would you prefer to handle the commit message update?
I have sent a v2 patch in reply to the v1 thread with the corrected description. Please feel free to drop the current one and apply v2.
Alternatively, if it is easier, the commit message can be amended directly in your tree.
Sorry for the mix-up.
Signature · 2 lines
Thanks, Cheng-Yang
PATCH v2 1/1 sched_ext: Fix inconsistent NUMA node lookup in scx_select_cpu_dfl()
In reply to Cheng-Yang ChouIn the WAKE_SYNC path of scx_select_cpu_dfl(), waker_node was computed with cpu_to_node(), while node (for prev_cpu) was computed with scx_cpu_node_if_enabled(). When scx_builtin_idle_per_node is disabled, idle_cpumask(waker_node) is called with a real node ID even though per-node idle tracking is disabled, resulting in undefined behavior.
Fix by using scx_cpu_node_if_enabled() for waker_node as well, ensuring both variables are computed consistently.
---
Changes in v2:
- Update commit message to drop the incorrect short-circuiting claim (Andrea Righi)
- Link to v1:
https://lore.kernel.org/all/[email protected]/
kernel/sched/ext_idle.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/ext_idle.c +1 -1
@@ -543,7 +543,7 @@ s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags,* piled up on it even if there is an idle core elsewhere on * the system. */ - waker_node =cpu_to_node (cpu); + waker_node =scx_cpu_node_if_enabled (cpu); if (!(current->flags & PF_EXITING) && cpu_rq(cpu)->scx.local_dsq.nr == 0 && (!(flags & SCX_PICK_IDLE_IN_NODE) || (waker_node == node)) &&
Signature · 1 line
2.48.1
Tejun Heo
reviewerHello,
The commit message on sched_ext/for-7.0-fixes has been amended to match v2.
Thanks.
--
tejun