sched_ext/idle: Skip nodes without CPUs
Subsystems (2)
PATCH 1/1 sched_ext/idle: Skip nodes without CPUs
With SCX_OPS_BUILTIN_IDLE_PER_NODE, a CPU is tracked in the idle cpumask of cpu_to_node(cpu). When the starting node has no idle CPU, pick_idle_cpu_from_online_nodes() walks the other online nodes in distance order. Nodes that own no CPU can't have an idle one, but the walk still visits them.
On a Raspberry Pi 5, the firmware adds numa=fake=8. That gives eight online nodes, but cpu_to_node() puts every CPU in node 0, so a search that leaves node 0 visits seven nodes that can't have an idle CPU.
Walk only the nodes that have an active CPU, kept up to date across CPU hotplug. Nothing changes when every online node has an active CPU. On that rpi, five hackbench -p -g 10 -l 10000 runs under scx_simple gave these means (ops.select_cpu() time per call, run time):
global idle cpumask per-node idle cpumasks before 135ns, 7.39s 484ns, 7.49s after 134ns, 7.35s 141ns, 7.39s
With the patch, the per-node cost is close to the global one. The walk itself came up before and was left alone because the node count is usually low [1]. This doesn't change the walk. It only skips nodes that can't have an idle CPU able to take tasks.
[1] https://lore.kernel.org/all/[email protected]/
---
I'm not sure why the rpi does this, but I don't think this fix only matters downstream. Nothing changes when every online node has an active CPU, and other setups may run into the same problem as the rpi.
kernel/sched/ext/idle.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-)
kernel/sched/ext/idle.c +25 -3
@@ -145,6 +145,24 @@ static s32 pick_idle_cpu_in_node(const struct cpumask *cpus_allowed, int node, u*/ static DEFINE_PER_CPU(nodemask_t, per_cpu_unvisited); +/* +* Nodes with an active CPU. Idle CPUs are tracked per cpu_to_node(), so other +* nodes can't have one that can take tasks. Updated on enable and on CPU +* hotplug after cpu_active_mask changes. +*/ +static nodemask_t scx_idle_cpu_nodes; + +static void update_idle_cpu_nodes (void ) +{ +nodemask_t nodes =NODE_MASK_NONE ; +s32 cpu; + +for_each_cpu (cpu, cpu_active_mask) +node_set (cpu_to_node (cpu), nodes); + +nodes_copy (scx_idle_cpu_nodes, nodes); +} +/* * Search for an idle CPU across all nodes, excluding @node. */
@@ -168,10 +186,10 @@ static s32 pick_idle_cpu_from_online_nodes(const struct cpumask *cpus_allowed, i unvisited =this_cpu_ptr (&per_cpu_unvisited);/* -* Restrict the search to the online nodes (excluding the current -* node that has been visited already). +* Restrict the search to the nodes with active CPUs (excluding the +* current node that has been visited already). */ -nodes_copy (*unvisited, node_states[N_ONLINE ]); +nodes_copy (*unvisited, scx_idle_cpu_nodes);node_clear (node, *unvisited);/*
@@ -198,6 +216,8 @@ static s32 pick_idle_cpu_from_online_nodes(const struct cpumask *cpus_allowed, i return cpu; }#else +static inline void update_idle_cpu_nodes (void ) {} +static inline s32 pick_idle_cpu_from_online_nodes (const struct cpumask *cpus_allowed,int node,u64 flags) {
@@ -414,6 +434,8 @@ void scx_idle_update_selcpu_topology(struct sched_ext_ops *ops)static_branch_enable_cpuslocked (&scx_selcpu_topo_numa); else static_branch_disable_cpuslocked(&scx_selcpu_topo_numa); + +update_idle_cpu_nodes (); }/*
base-commit: 95a4df9944cd5d57a92662269114ba99cb972394
Signature · 1 line
2.56.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] sched_ext: missed update_idle_cpu_nodes() on hotplug during enable --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] sched_ext: missed update_idle_cpu_nodes() on hotplug during enable
--
commit 49bd96c9c4b1ca8a5782568b9a5893b41d485588
Author: Cheng-Yang Chou <[email protected]>
sched_ext/idle: Skip nodes without CPUs
This commit maintains a nodemask of NUMA nodes that have at least one active CPU (scx_idle_cpu_nodes) and restricts cross-node idle CPU searches in pick_idle_cpu_from_online_nodes() to those nodes instead of all online nodes.
Quoted 4 lines
kernel/sched/ext/idle.c +0
-0
[ ... ]
kernel/sched/ext/idle.c void scx_idle_update_selcpu_topology(struct sched_ext_ops *ops) · line 441
Quoted 9 lines
kernel/sched/ext/idle.c +2
-0@@ -414,6 +434,8 @@ void scx_idle_update_selcpu_topology(struct sched_ext_ops *ops)static_branch_enable_cpuslocked (&scx_selcpu_topo_numa); else static_branch_disable_cpuslocked(&scx_selcpu_topo_numa); + +update_idle_cpu_nodes (); }/*
[Severity: Medium]
Can scx_idle_cpu_nodes become stale if a CPU hotplug event occurs while a scheduler is being enabled?
scx_root_enable_workfn() calls scx_idle_update_selcpu_topology() right before cpus_read_unlock(), well before enabling __scx_enabled:
kernel/sched/ext/ext.c:scx_root_enable_workfn() {
...
for (i = SCX_OPI_CPU_HOTPLUG_BEGIN; i < SCX_OPI_CPU_HOTPLUG_END; i++)
if (((void (**)(void))ops)[i])
set_bit(i, sch->has_op);
ret = check_hotplug_seq(sch, ops);
if (ret) {
cpus_read_unlock();
goto err_disable;
}
scx_idle_update_selcpu_topology(ops);
cpus_read_unlock();
...
}If a CPU comes online or goes offline after cpus_read_unlock() and before static_branch_enable(&__scx_enabled) (while tasks are being initialized), handle_hotplug() skips scx_idle_update_selcpu_topology() because scx_enabled() is still false:
kernel/sched/ext/ext.c:handle_hotplug() {
...
if (unlikely(!sch))
return;
if (scx_enabled())
scx_idle_update_selcpu_topology(&sch->ops);
...
}When the scheduler has ops.cpu_online or ops.cpu_offline defined, the hotplug transition is accepted without aborting enablement, leaving scx_idle_cpu_nodes out of sync with cpu_active_mask once the scheduler goes live. If the first active CPU on a node came online during that window, pick_idle_cpu_from_online_nodes() will skip that node until the next hotplug event.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Tejun Heo
reviewerHello, Cheng-Yang.
Quoted 3 lines · Cheng-Yang Chou
On a Raspberry Pi 5, the firmware adds numa=fake=8. That gives eight online nodes, but
cpu_to_node()puts every CPU in node 0, so a search that leaves node 0 visits seven nodes that can't have an idle CPU.
AFAICS, with numa=fake, cpu_to_node() puts every CPU on node 0 but numa_add_cpu() also sets the CPU in cpumask_of_node() of every emulated node, so all eight nodes claim the same CPUs and have N_CPU set. No real topology looks like that and it doesn't make sense for a testing feature to break that. That looks like a bug in the NUMA emulation to me.
rpi5 seems to be the only real user of fake NUMA. How about fixing the NUMA emulation and then switching sched_ext to walk node_states[N_CPU] instead of N_ONLINE?
Thanks.
--
tejun
Hi Tejun,
Quoted 7 lines · Tejun Heo
Hello, Cheng-Yang.
Quoted 3 lines · Cheng-Yang Chou
On a Raspberry Pi 5, the firmware adds numa=fake=8. That gives eight online nodes, but
cpu_to_node()puts every CPU in node 0, so a search that leaves node 0 visits seven nodes that can't have an idle CPU.
Quoted 5 lines
AFAICS, with numa=fake,
cpu_to_node()puts every CPU on node 0 butnuma_add_cpu()also sets the CPU incpumask_of_node()of every emulated node, so all eight nodes claim the same CPUs and haveN_CPUset. No real topology looks like that and it doesn't make sense for a testing feature to break that. That looks like a bug in the NUMA emulation to me.
Thanks for the explanation.
Quoted 5 lines
rpi5 seems to be the only real user of fake NUMA. How about fixing the NUMA emulation and then switching
sched_extto walk node_states[N_CPU] instead ofN_ONLINE?
Make sense, will do. Start looking at the NUMA code now :)
Signature · 2 lines
Thanks, Cheng-Yang