]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
sched_ext: Skip ops.set_weight() for disabled tasks
authorKuba Piecuch <jpiecuch@google.com>
Fri, 10 Jul 2026 14:43:41 +0000 (14:43 +0000)
committerTejun Heo <tj@kernel.org>
Fri, 10 Jul 2026 16:41:28 +0000 (06:41 -1000)
When switching a task's sched_class away from sched_ext, we get the
following sequence of events in __sched_setscheduler():

sched_change_begin()
  switched_from_scx()
    scx_disable_task(p)
      ops.disable(p)
__setscheduler_params()
  set_load_weight()
    reweight_task_scx(p)
      ops.set_weight(p)
p->sched_class = next_class;
sched_change_end()
  ...

Notably, ops.set_weight() is called _after_ ops.disable().
This violates the expected semantics of the callbacks, the expectation
being that ops.disable() can only be followed by ops.exit_task() or
ops.enable().

Skipping the weight adjustment for disabled tasks should be harmless
since the weight will be recalculated in scx_enable_task() if the task
ever rejoins SCX.

Fixes: 637b0682821b ("sched: Fold sched_class::switch{ing,ed}_{to,from}() into the change pattern")
Cc: stable@vger.kernel.org # v6.19+
Signed-off-by: Kuba Piecuch <jpiecuch@google.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
kernel/sched/ext/ext.c

index aeee44e016abbed6e25d9e5e608b19eed43258e1..e3fa7b2fac9dfc66bee17b031b5d11d2b0775158 100644 (file)
@@ -3967,6 +3967,17 @@ static void reweight_task_scx(struct rq *rq, struct task_struct *p,
        if (task_dead_and_done(p))
                return;
 
+       /*
+        * When switching sched_class away from SCX, reweight_task_scx()
+        * is called _after_ scx_disable_task(). Skip calling ops.set_weight()
+        * since the BPF scheduler may have already forgotten the task in
+        * ops.disable().
+        * p->scx.weight will be recalculated in scx_enable_task() if the task
+        * ever returns to SCX class.
+        */
+       if (scx_get_task_state(p) != SCX_TASK_ENABLED)
+               return;
+
        p->scx.weight = sched_weight_to_cgroup(scale_load_down(lw->weight));
        if (SCX_HAS_OP(sch, set_weight))
                SCX_CALL_OP_TASK(sch, set_weight, rq, p, p->scx.weight);