Sched_ext development
 help / color / mirror / Atom feed
* [PATCH sched_ext/for-7.4-fixes] sched_ext: Don't skip NONE tasks in sched_ext_dead()
@ 2026-12-03 20:28 Chris Mason
  2026-12-03 20:50 ` sashiko-bot
  2026-12-03 22:45 ` Andrea Righi
  0 siblings, 2 replies; 3+ messages in thread
From: Chris Mason @ 2026-12-03 20:28 UTC (permalink / raw)
  To: tj, void, arighi, changwoo, sched-ext; +Cc: linux-kernel, mason

sched_ext_dead() leaves p->scx.sched set if the task was punted by
scx_fail_parent(), which leads to a use-after-free of the parent sched.

Tasks hold no reference on their sched, so p->scx.sched has to be
cleared before the sched is freed. sched_ext_dead() doesn't do that
for a task at SCX_TASK_NONE, and scx_punt_task() leaves a live task at
NONE with p->scx.sched set:

    scx_sub_disable(sub)
      __scx_init_task(parent) fails
      scx_fail_parent()
        scx_punt_task(parent, p)      NONE, p->scx.sched = parent
    sched_ext_dead(p)
      list_del_init()                 p is off scx_tasks
      state is NONE                   p->scx.sched stays
    scx_root_disable(parent)
      scx_task_iter_next_locked()     doesn't visit p
    scx_sched_free_rcu_work()
      kfree(parent)
    scx_bpf_select_cpu_and(p)         a new root has a sub-sched
      scx_error(p->scx.sched)

The fix used here is to drop the skip, so that every dead task is taken
off its sched and marked SCX_TASK_DEAD.

Fixes: 337ec00b1d9c ("sched_ext: Implement cgroup sub-sched enabling and disabling")
Signed-off-by: Chris Mason <mason@kernel.org>
---
This is an AI generated fix, on top of v7.4-rc5.

It makes every exiting task take its rq lock in sched_ext_dead(), even
without a BPF scheduler loaded.  I haven't benchmarked that.

 kernel/sched/ext/ext.c | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index efa1d01d03afa..fd3d3d49be145 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -4973,6 +4973,9 @@ static bool task_dead_and_done(struct task_struct *p)
 
 void sched_ext_dead(struct task_struct *p)
 {
+	struct rq_flags rf;
+	struct rq *rq;
+
 	/*
 	 * By the time control reaches here, @p has %TASK_DEAD set, switched out
 	 * for the last time and then dropped the rq lock - task_dead_and_done()
@@ -4992,24 +4995,19 @@ void sched_ext_dead(struct task_struct *p)
 	 * ENABLED transitions can't race us. Disable ops for @p.
 	 *
 	 * %SCX_TASK_DEAD synchronizes against cgroup task iteration - see
-	 * scx_task_iter_next_locked(). NONE tasks need no marking: cgroup
-	 * iteration is only used from sub-sched paths, which require root
-	 * enabled. Root enable transitions every live task to at least READY.
+	 * scx_task_iter_next_locked(). NONE tasks go through this too: the
+	 * sub-sched paths change the state under @p's rq lock, and
+	 * scx_punt_task() leaves a live task at NONE with its sched set.
 	 *
 	 * %INIT_BEGIN means ops.init_task() is running for @p. Don't call
 	 * into ops; transition to %DEAD so the post-init recheck unwinds
 	 * via scx_sub_init_cancel_task().
 	 */
-	if (scx_get_task_state(p) != SCX_TASK_NONE) {
-		struct rq_flags rf;
-		struct rq *rq;
-
-		rq = task_rq_lock(p, &rf);
-		if (scx_get_task_state(p) != SCX_TASK_INIT_BEGIN)
-			scx_disable_and_exit_task(scx_task_sched(p), p);
-		scx_set_task_state(p, SCX_TASK_DEAD);
-		task_rq_unlock(rq, p, &rf);
-	}
+	rq = task_rq_lock(p, &rf);
+	if (scx_get_task_state(p) != SCX_TASK_INIT_BEGIN)
+		scx_disable_and_exit_task(scx_task_sched(p), p);
+	scx_set_task_state(p, SCX_TASK_DEAD);
+	task_rq_unlock(rq, p, &rf);
 }
 
 static void reweight_task_scx(struct rq *rq, struct task_struct *p,

base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH sched_ext/for-7.4-fixes] sched_ext: Don't skip NONE tasks in sched_ext_dead()
  2026-12-03 20:28 [PATCH sched_ext/for-7.4-fixes] sched_ext: Don't skip NONE tasks in sched_ext_dead() Chris Mason
@ 2026-12-03 20:50 ` sashiko-bot
  2026-12-03 22:45 ` Andrea Righi
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-12-03 20:50 UTC (permalink / raw)
  To: Chris Mason, sched-ext

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] sched_ext: unconditional task_rq_lock() in sched_ext_dead()
  on every exit
--

commit 7328cb855c524883a9be737f7233e948be1c4e7b
Author: Chris Mason <mason@kernel.org>

sched_ext: Don't skip NONE tasks in sched_ext_dead()

This commit removes the SCX_TASK_NONE skip in sched_ext_dead() so that every
exiting task takes its runqueue lock, calls scx_disable_and_exit_task(), and
transitions to SCX_TASK_DEAD.

> diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
> index efa1d01d03afa..fd3d3d49be145 100644
> --- a/kernel/sched/ext/ext.c
> +++ b/kernel/sched/ext/ext.c

[ ... ]

> @@ -4992,24 +4995,19 @@ void sched_ext_dead(struct task_struct *p)
>  	 * ENABLED transitions can't race us. Disable ops for @p.
>  	 *
>  	 * %SCX_TASK_DEAD synchronizes against cgroup task iteration - see
> -	 * scx_task_iter_next_locked(). NONE tasks need no marking: cgroup
> -	 * iteration is only used from sub-sched paths, which require root
> -	 * enabled. Root enable transitions every live task to at least READY.
> +	 * scx_task_iter_next_locked(). NONE tasks go through this too: the
> +	 * sub-sched paths change the state under @p's rq lock, and
> +	 * scx_punt_task() leaves a live task at NONE with its sched set.
>  	 *
>  	 * %INIT_BEGIN means ops.init_task() is running for @p. Don't call
>  	 * into ops; transition to %DEAD so the post-init recheck unwinds
>  	 * via scx_sub_init_cancel_task().
>  	 */
> -	if (scx_get_task_state(p) != SCX_TASK_NONE) {
> -		struct rq_flags rf;
> -		struct rq *rq;
> -
> -		rq = task_rq_lock(p, &rf);
> -		if (scx_get_task_state(p) != SCX_TASK_INIT_BEGIN)
> -			scx_disable_and_exit_task(scx_task_sched(p), p);
> -		scx_set_task_state(p, SCX_TASK_DEAD);
> -		task_rq_unlock(rq, p, &rf);
> -	}
> +	rq = task_rq_lock(p, &rf);
> +	if (scx_get_task_state(p) != SCX_TASK_INIT_BEGIN)
> +		scx_disable_and_exit_task(scx_task_sched(p), p);
> +	scx_set_task_state(p, SCX_TASK_DEAD);
> +	task_rq_unlock(rq, p, &rf);
>  }

[Severity: Medium]
Does dropping the SCX_TASK_NONE check add an extra rq->__lock acquisition on
every task exit even when no BPF scheduler is loaded?

finish_task_switch() calls sched_ext_dead() on every TASK_DEAD exit right
after finish_lock_switch() drops rq->__lock and re-enables interrupts.  Now
every exiting task disables interrupts again, takes p->pi_lock and
rq->__lock, and runs scx_reset_task_fields(), hurting exit-heavy workloads.

Could this keep a fast path by checking scx_enable_state() == SCX_DISABLED
under scx_tasks_lock, since scx_root_enable_workfn() leaves SCX_DISABLED
before taking scx_tasks_lock and scx_root_disable() returns to it only after
draining all tasks and synchronize_rcu()?

If the unconditional lock is kept, the commit message itself should also
note this task-exit locking impact rather than leaving it below the ---
separator.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203202815.32456-1-mason@kernel.org?part=1

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH sched_ext/for-7.4-fixes] sched_ext: Don't skip NONE tasks in sched_ext_dead()
  2026-12-03 20:28 [PATCH sched_ext/for-7.4-fixes] sched_ext: Don't skip NONE tasks in sched_ext_dead() Chris Mason
  2026-12-03 20:50 ` sashiko-bot
@ 2026-12-03 22:45 ` Andrea Righi
  1 sibling, 0 replies; 3+ messages in thread
From: Andrea Righi @ 2026-12-03 22:45 UTC (permalink / raw)
  To: Chris Mason; +Cc: tj, void, changwoo, sched-ext, linux-kernel

Hi Chris,

On Thu, Dec 03, 2026 at 03:28:15PM -0500, Chris Mason wrote:
> sched_ext_dead() leaves p->scx.sched set if the task was punted by
> scx_fail_parent(), which leads to a use-after-free of the parent sched.
> 
> Tasks hold no reference on their sched, so p->scx.sched has to be
> cleared before the sched is freed. sched_ext_dead() doesn't do that
> for a task at SCX_TASK_NONE, and scx_punt_task() leaves a live task at
> NONE with p->scx.sched set:
> 
>     scx_sub_disable(sub)
>       __scx_init_task(parent) fails
>       scx_fail_parent()
>         scx_punt_task(parent, p)      NONE, p->scx.sched = parent
>     sched_ext_dead(p)
>       list_del_init()                 p is off scx_tasks
>       state is NONE                   p->scx.sched stays
>     scx_root_disable(parent)
>       scx_task_iter_next_locked()     doesn't visit p
>     scx_sched_free_rcu_work()
>       kfree(parent)
>     scx_bpf_select_cpu_and(p)         a new root has a sub-sched
>       scx_error(p->scx.sched)

The analysis makes sense to me.

> 
> The fix used here is to drop the skip, so that every dead task is taken
> off its sched and marked SCX_TASK_DEAD.
> 
> Fixes: 337ec00b1d9c ("sched_ext: Implement cgroup sub-sched enabling and disabling")
> Signed-off-by: Chris Mason <mason@kernel.org>
> ---
> This is an AI generated fix, on top of v7.4-rc5.
> 
> It makes every exiting task take its rq lock in sched_ext_dead(), even
> without a BPF scheduler loaded.  I haven't benchmarked that.

Yeah, this is a bit concerning, I think the suggestion in sashiko's
review makes sense, we can add a fast path that checks for SCX_DISABLED
under scx_tasks_lock(), to avoid the extra locking when a BPF scheduler
isn't loaded. When one is loaded, exiting tasks are normally READY or
ENABLED, so they were already taking the rq lock before this change.

Something like this (on top of your patch):

diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index fd3d3d49be145..b67eab423fc82 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -4988,6 +4988,16 @@ void sched_ext_dead(struct task_struct *p)
 			rhashtable_remove_fast(&scx_tid_hash,
 					       &p->scx.tid_hash_node,
 					       scx_tid_hash_params);
+
+		/*
+		 * If disabled, @p is %NONE with no sched and, being off
+		 * scx_tasks, can't be picked up by a racing root enable. Skip
+		 * the rq lock.
+		 */
+		if (scx_enable_state() == SCX_DISABLED) {
+			scx_set_task_state(p, SCX_TASK_DEAD);
+			return;
+		}
 	}
 
 	/*

BTW, I think the lockless NONE check is racy even without any
ops.init_task() failure, scx_rehome_task() transiently goes through NONE
under the rq lock but without holding scx_tasks_lock. If
sched_ext_dead() hits that window, the task is never exited from the
parent and it's left with a stale p->scx.sched.

Your patch fixes this case as well, and the fast path doesn't
reintroduce it. Maybe it's worth mentioning this case as well in the
patch description.

Thanks,
-Andrea

> 
>  kernel/sched/ext/ext.c | 24 +++++++++++-------------
>  1 file changed, 11 insertions(+), 13 deletions(-)
> 
> diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
> index efa1d01d03afa..fd3d3d49be145 100644
> --- a/kernel/sched/ext/ext.c
> +++ b/kernel/sched/ext/ext.c
> @@ -4973,6 +4973,9 @@ static bool task_dead_and_done(struct task_struct *p)
>  
>  void sched_ext_dead(struct task_struct *p)
>  {
> +	struct rq_flags rf;
> +	struct rq *rq;
> +
>  	/*
>  	 * By the time control reaches here, @p has %TASK_DEAD set, switched out
>  	 * for the last time and then dropped the rq lock - task_dead_and_done()
> @@ -4992,24 +4995,19 @@ void sched_ext_dead(struct task_struct *p)
>  	 * ENABLED transitions can't race us. Disable ops for @p.
>  	 *
>  	 * %SCX_TASK_DEAD synchronizes against cgroup task iteration - see
> -	 * scx_task_iter_next_locked(). NONE tasks need no marking: cgroup
> -	 * iteration is only used from sub-sched paths, which require root
> -	 * enabled. Root enable transitions every live task to at least READY.
> +	 * scx_task_iter_next_locked(). NONE tasks go through this too: the
> +	 * sub-sched paths change the state under @p's rq lock, and
> +	 * scx_punt_task() leaves a live task at NONE with its sched set.
>  	 *
>  	 * %INIT_BEGIN means ops.init_task() is running for @p. Don't call
>  	 * into ops; transition to %DEAD so the post-init recheck unwinds
>  	 * via scx_sub_init_cancel_task().
>  	 */
> -	if (scx_get_task_state(p) != SCX_TASK_NONE) {
> -		struct rq_flags rf;
> -		struct rq *rq;
> -
> -		rq = task_rq_lock(p, &rf);
> -		if (scx_get_task_state(p) != SCX_TASK_INIT_BEGIN)
> -			scx_disable_and_exit_task(scx_task_sched(p), p);
> -		scx_set_task_state(p, SCX_TASK_DEAD);
> -		task_rq_unlock(rq, p, &rf);
> -	}
> +	rq = task_rq_lock(p, &rf);
> +	if (scx_get_task_state(p) != SCX_TASK_INIT_BEGIN)
> +		scx_disable_and_exit_task(scx_task_sched(p), p);
> +	scx_set_task_state(p, SCX_TASK_DEAD);
> +	task_rq_unlock(rq, p, &rf);
>  }
>  
>  static void reweight_task_scx(struct rq *rq, struct task_struct *p,
> 
> base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
> -- 
> 2.55.0
> 

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-03 22:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 20:28 [PATCH sched_ext/for-7.4-fixes] sched_ext: Don't skip NONE tasks in sched_ext_dead() Chris Mason
2026-12-03 20:50 ` sashiko-bot
2026-12-03 22:45 ` Andrea Righi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox