[PATCH v2 2/2] sched_ext: Fix cpu_released while changing sched policy of the running task
From: liuwenfang
Date: Sat Jun 28 2025 - 03:21:11 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: liuwenfang liuwenfang@xxxxxxxxx
---
kernel/sched/ext.c | 20 +++++++++++++++++++-
1 file changed, 19 insertions(+), 1 deletion(-)
diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c
index f161156be..7fab0a657 100644
--- a/kernel/sched/ext.c
+++ b/kernel/sched/ext.c
@@ -3903,11 +3903,29 @@ static void switching_to_scx(struct rq *rq, struct task_struct *p)
static void switched_from_scx(struct rq *rq, struct task_struct *p)
{
+ switch_class(rq, p, true);
+
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)) {
+ /*
+ * 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