[PATCH sched_ext] sched/ext: Keep tick enabled for finite-slice tasks

"Zhimin Feng" <[email protected]> Thu, 16 Jul 2026 10:37:46 +0800
Newsgroups dev.linux.lists.sched-ext,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
SCX tasks with finite slices need the scheduler tick to make forward
progress on slice expiration. However, set_next_task_scx() used to refresh
the nohz dependency through sched_update_tick_dependency(), which evaluates
sched_can_stop_tick() using rq->curr.

When switching from idle, RT, CFS, or another previous task to a
finite-slice SCX task on a nohz_full CPU, rq->curr may still point at
the previous task while set_next_task_scx() is running. If that previous
task allows the tick to stop, the scheduler dependency can remain
cleared even though the next SCX task has p->scx.slice != SCX_SLICE_INF.

CFS does not hit the same gap for bandwidth-constrained tasks because its
pick path has a next-task based check: sched_fair_update_stop_tick()
explicitly sets TICK_DEP_BIT_SCHED when the selected CFS task needs
bandwidth accounting on a nohz_full CPU.

Add a helper to set the scheduler tick dependency directly and use it from
set_next_task_scx() whenever the next SCX task has a finite slice.
This avoids basing the finite-slice decision on the old rq->curr while
preserving the existing tickless behavior for SCX_SLICE_INF tasks.

Signed-off-by: Zhimin Feng <[email protected]>
Co-developed-by: Chengming Zhou <[email protected]>
Signed-off-by: Chengming Zhou <[email protected]>
---
 kernel/sched/ext/ext.c | 21 ++++++++++++++++++---
 kernel/sched/sched.h   | 11 +++++++++++
 2 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index 691d53fe0f64..6c0c805d489a 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -2950,6 +2950,8 @@ static int balance_one(struct rq *rq, struct task_struct *prev)
 static void set_next_task_scx(struct rq *rq, struct task_struct *p, bool first)
 {
 	struct scx_sched *sch = scx_task_sched(p);
+	bool can_stop_tick;
+	bool tick_state_changed;
 
 	if (p->scx.flags & SCX_TASK_QUEUED) {
 		/*
@@ -2972,15 +2974,28 @@ static void set_next_task_scx(struct rq *rq, struct task_struct *p, bool first)
 	 * @p is getting newly scheduled or got kicked after someone updated its
 	 * slice. Refresh whether tick can be stopped. See scx_can_stop_tick().
 	 */
-	if ((p->scx.slice == SCX_SLICE_INF) !=
-	    (bool)(rq->scx.flags & SCX_RQ_CAN_STOP_TICK)) {
-		if (p->scx.slice == SCX_SLICE_INF)
+	can_stop_tick = p->scx.slice == SCX_SLICE_INF;
+	tick_state_changed = can_stop_tick !=
+		(bool)(rq->scx.flags & SCX_RQ_CAN_STOP_TICK);
+	if (tick_state_changed) {
+		if (can_stop_tick)
 			rq->scx.flags |= SCX_RQ_CAN_STOP_TICK;
 		else
 			rq->scx.flags &= ~SCX_RQ_CAN_STOP_TICK;
+	}
 
+	/*
+	 * If @p has a finite slice, it needs the scheduler tick to make
+	 * forward progress on slice expiration. Don't rely on
+	 * sched_update_tick_dependency() here because rq->curr may still be
+	 * the previous task while set_next_task_scx() is running.
+	 */
+	if (!can_stop_tick)
+		sched_set_tick_dependency(rq);
+	else if (tick_state_changed)
 		sched_update_tick_dependency(rq);
 
+	if (tick_state_changed) {
 		/*
 		 * For now, let's refresh the load_avgs just when transitioning
 		 * in and out of nohz. In the future, we might want to add a
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 56acf502ba26..d692d92fc095 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -2998,9 +2998,20 @@ static inline void sched_update_tick_dependency(struct rq *rq)
 	else
 		tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED);
 }
+
+static inline void sched_set_tick_dependency(struct rq *rq)
+{
+	int cpu = cpu_of(rq);
+
+	if (!tick_nohz_full_cpu(cpu))
+		return;
+
+	tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED);
+}
 #else /* !CONFIG_NO_HZ_FULL: */
 static inline int sched_tick_offload_init(void) { return 0; }
 static inline void sched_update_tick_dependency(struct rq *rq) { }
+static inline void sched_set_tick_dependency(struct rq *rq) { }
 #endif /* !CONFIG_NO_HZ_FULL */
 
 static inline void add_nr_running(struct rq *rq, unsigned count)
-- 
2.39.5