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

Zhimin Feng posted 1 patch 1 week, 2 days ago
kernel/sched/ext/ext.c | 21 ++++++++++++++++++---
kernel/sched/sched.h   | 11 +++++++++++
2 files changed, 29 insertions(+), 3 deletions(-)
[PATCH sched_ext] sched/ext: Keep tick enabled for finite-slice tasks
Posted by Zhimin Feng 1 week, 2 days ago
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 <fengzhimin@bytedance.com>
Co-developed-by: Chengming Zhou <zhouchengming@bytedance.com>
Signed-off-by: Chengming Zhou <zhouchengming@bytedance.com>
---
 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
Re: [PATCH sched_ext] sched/ext: Keep tick enabled for finite-slice tasks
Posted by Andrea Righi 1 week, 2 days ago
Hi Zhimin,

On Thu, Jul 16, 2026 at 10:37:46AM +0800, Zhimin Feng wrote:
> 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 <fengzhimin@bytedance.com>
> Co-developed-by: Chengming Zhou <zhouchengming@bytedance.com>
> Signed-off-by: Chengming Zhou <zhouchengming@bytedance.com>

This should already be fixed upstream by commit 4ec10f38ff901 ("sched_ext:
Enable tick for finite slices on nohz_full"):

See also:
https://lore.kernel.org/all/20260708074812.1041434-1-arighi@nvidia.com/

Thanks,
-Andrea

> ---
>  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
Re: [PATCH sched_ext] sched/ext: Keep tick enabled for finite-slice tasks
Posted by 冯志敏 1 week, 2 days ago
Hi,Andrea

Thanks for the explanation, I get it now.

> From: "Andrea Righi"<arighi@nvidia.com>
> Date:  Thu, Jul 16, 2026, 13:15
> Subject:  Re: [PATCH sched_ext] sched/ext: Keep tick enabled for finite-slice tasks
> To: "Zhimin Feng"<fengzhimin@bytedance.com>
> Cc: "Tejun Heo"<tj@kernel.org>, "David Vernet"<void@manifault.com>, "Changwoo Min"<changwoo@igalia.com>, "Ingo Molnar"<mingo@redhat.com>, "Peter Zijlstra"<peterz@infradead.org>, "Juri Lelli"<juri.lelli@redhat.com>, "Vincent Guittot"<vincent.guittot@linaro.org>, "Dietmar Eggemann"<dietmar.eggemann@arm.com>, "Steven Rostedt"<rostedt@goodmis.org>, "Ben Segall"<bsegall@google.com>, "Mel Gorman"<mgorman@suse.de>, "Valentin Schneider"<vschneid@redhat.com>, "K Prateek Nayak"<kprateek.nayak@amd.com>, "Emil Tsalapatis"<etsal@meta.com>, "Cheng-Yang Chou"<yphbchou0911@gmail.com>, <sched-ext@lists.linux.dev>, <linux-kernel@vger.kernel.org>, "Chengming Zhou"<zhouchengming@bytedance.com>
> Hi Zhimin,
> 
> On Thu, Jul 16, 2026 at 10:37:46AM +0800, Zhimin Feng wrote:
> > 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 <fengzhimin@bytedance.com>
> > Co-developed-by: Chengming Zhou <zhouchengming@bytedance.com>
> > Signed-off-by: Chengming Zhou <zhouchengming@bytedance.com>
> 
> This should already be fixed upstream by commit 4ec10f38ff901 ("sched_ext:
> Enable tick for finite slices on nohz_full"):
> 
> See also:
> https://lore.kernel.org/all/20260708074812.1041434-1-arighi@nvidia.com/
> 
> Thanks,
> -Andrea
> 
> > ---
> >  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
>