[PATCH v3 3/3] sched_ext: Fix cpu_released while changing sched policy of the running task

From: liuwenfang
Date: Sun Jul 20 2025 - 05:41:16 EST


Priority inheritance policy should be cared, eg., one SCX task can be
boosted to REAL-TIME while holding a mutex lock, and then restored while
releasing the lock. The value cpu_released is fixed when changing the
sched class of the running task.

Signed-off-by: Wenfang Liu liuwenfang@xxxxxxxxx
---
kernel/sched/ext.c | 21 ++++++++++++++++++++-
1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c
index ddf4bd060..e3b9032e2 100644
--- a/kernel/sched/ext.c
+++ b/kernel/sched/ext.c
@@ -3899,11 +3899,30 @@ static void switching_to_scx(struct rq *rq, struct task_struct *p)

static void switched_from_scx(struct rq *rq, struct task_struct *p)
{
+ if (task_current(rq, p))
+ switch_class(rq, p);
+
scx_ops_disable_task(p);
}

static void wakeup_preempt_scx(struct rq *rq, struct task_struct *p,int wake_flags) {}
-static void switched_to_scx(struct rq *rq, struct task_struct *p) {}
+static void switched_to_scx(struct rq *rq, struct task_struct *p)
+{
+ lockdep_assert_rq_held(rq);
+
+ if (static_branch_unlikely(&scx_ops_cpu_preempt) &&
+ unlikely(rq->scx.cpu_released) && task_current(rq, p)) {
+ /*
+ * If the previous sched_class for the current CPU was not SCX,
+ * notify the BPF scheduler that it again has control of the
+ * core. This callback complements ->cpu_release(), which is
+ * emitted in switch_class().
+ */
+ if (SCX_HAS_OP(cpu_acquire))
+ SCX_CALL_OP(SCX_KF_REST, cpu_acquire, rq, cpu_of(rq), NULL);
+ rq->scx.cpu_released = false;
+ }
+}

int scx_check_setscheduler(struct task_struct *p, int policy)
{
--
2.17.1