]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
sched_ext: initialize built-in idle state before ops.init()
authorAndrea Righi <arighi@nvidia.com>
Tue, 25 Mar 2025 09:32:12 +0000 (10:32 +0100)
committerTejun Heo <tj@kernel.org>
Wed, 26 Mar 2025 20:47:50 +0000 (10:47 -1000)
A BPF scheduler may want to use the built-in idle cpumasks in ops.init()
before the scheduler is fully initialized, either directly or through a
BPF timer for example.

However, this would result in an error, since the idle state has not
been properly initialized yet.

This can be easily verified by modifying scx_simple to call
scx_bpf_get_idle_cpumask() in ops.init():

$ sudo scx_simple

DEBUG DUMP
===========================================================================

scx_simple[121] triggered exit kind 1024:
  runtime error (built-in idle tracking is disabled)
...

Fix this by properly initializing the idle state before ops.init() is
called. With this change applied:

$ sudo scx_simple
local=2 global=0
local=19 global=11
local=23 global=11
...

Fixes: d73249f88743d ("sched_ext: idle: Make idle static keys private")
Signed-off-by: Andrea Righi <arighi@nvidia.com>
Reviewed-by: Joel Fernandes <joelagnelf@nvidia.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
kernel/sched/ext.c
kernel/sched/ext_idle.c

index b47be2729ece1ae3dfbf7b673d3e5caa01bf77dc..66bcd40a28ca1dc20494db0777c19dae9138aa43 100644 (file)
@@ -5361,6 +5361,8 @@ static int scx_ops_enable(struct sched_ext_ops *ops, struct bpf_link *link)
         */
        cpus_read_lock();
 
+       scx_idle_enable(ops);
+
        if (scx_ops.init) {
                ret = SCX_CALL_OP_RET(SCX_KF_UNLOCKED, init);
                if (ret) {
@@ -5427,8 +5429,6 @@ static int scx_ops_enable(struct sched_ext_ops *ops, struct bpf_link *link)
        if (scx_ops.cpu_acquire || scx_ops.cpu_release)
                static_branch_enable(&scx_ops_cpu_preempt);
 
-       scx_idle_enable(ops);
-
        /*
         * Lock out forks, cgroup on/offlining and moves before opening the
         * floodgate so that they don't wander into the operations prematurely.
index b5f451e616d48c638f70b8e134f69104b9cea3ff..cb343ca889e0250cbd96c8e083304d7bc7b52dee 100644 (file)
@@ -721,14 +721,14 @@ static void reset_idle_masks(struct sched_ext_ops *ops)
 void scx_idle_enable(struct sched_ext_ops *ops)
 {
        if (!ops->update_idle || (ops->flags & SCX_OPS_KEEP_BUILTIN_IDLE))
-               static_branch_enable(&scx_builtin_idle_enabled);
+               static_branch_enable_cpuslocked(&scx_builtin_idle_enabled);
        else
-               static_branch_disable(&scx_builtin_idle_enabled);
+               static_branch_disable_cpuslocked(&scx_builtin_idle_enabled);
 
        if (ops->flags & SCX_OPS_BUILTIN_IDLE_PER_NODE)
-               static_branch_enable(&scx_builtin_idle_per_node);
+               static_branch_enable_cpuslocked(&scx_builtin_idle_per_node);
        else
-               static_branch_disable(&scx_builtin_idle_per_node);
+               static_branch_disable_cpuslocked(&scx_builtin_idle_per_node);
 
 #ifdef CONFIG_SMP
        reset_idle_masks(ops);