]> Gentwo Git Trees - linux/.git/commitdiff
sched_ext: Fix cgroup exit ordering by moving sched_ext_free() to finish_task_switch()
authorTejun Heo <tj@kernel.org>
Mon, 3 Nov 2025 20:25:13 +0000 (10:25 -1000)
committerTejun Heo <tj@kernel.org>
Mon, 3 Nov 2025 21:57:30 +0000 (11:57 -1000)
sched_ext_free() was called from __put_task_struct() when the last reference
to the task is dropped, which could be long after the task has finished
running. This causes cgroup-related problems:

- ops.init_task() can be called on a cgroup which didn't get ops.cgroup_init()'d
  during scheduler load, because the cgroup might be destroyed/unlinked
  while the zombie or dead task is still lingering on the scx_tasks list.

- ops.cgroup_exit() could be called before ops.exit_task() is called on all
  member tasks, leading to incorrect exit ordering.

Fix by moving it to finish_task_switch() to be called right after the final
context switch away from the dying task, matching when sched_class->task_dead()
is called. Rename it to sched_ext_dead() to match the new calling context.

By calling sched_ext_dead() before cgroup_task_dead(), we ensure that:

- Tasks visible on scx_tasks list have valid cgroups during scheduler load,
  as cgroup_mutex prevents cgroup destruction while the task is still linked.

- All member tasks have ops.exit_task() called and are removed from scx_tasks
  before the cgroup can be destroyed and trigger ops.cgroup_exit().

This fix is made possible by the cgroup_task_dead() split in the previous patch.

This also makes more sense resource-wise as there's no point in keeping
scheduler side resources around for dead tasks.

Reported-by: Dan Schatzberg <dschatzberg@meta.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Reviewed-by: Andrea Righi <arighi@nvidia.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
include/linux/sched/ext.h
kernel/fork.c
kernel/sched/core.c
kernel/sched/ext.c

index 4713f374acc0e05165ecc9b0c870977f0a6a82bf..eb776b094d36e93a9dba41966d932e7086164993 100644 (file)
@@ -208,14 +208,14 @@ struct sched_ext_entity {
        struct list_head        tasks_node;
 };
 
-void sched_ext_free(struct task_struct *p);
+void sched_ext_dead(struct task_struct *p);
 void print_scx_info(const char *log_lvl, struct task_struct *p);
 void scx_softlockup(u32 dur_s);
 bool scx_rcu_cpu_stall(void);
 
 #else  /* !CONFIG_SCHED_CLASS_EXT */
 
-static inline void sched_ext_free(struct task_struct *p) {}
+static inline void sched_ext_dead(struct task_struct *p) {}
 static inline void print_scx_info(const char *log_lvl, struct task_struct *p) {}
 static inline void scx_softlockup(u32 dur_s) {}
 static inline bool scx_rcu_cpu_stall(void) { return false; }
index 960c39c9c264bbee35e56d570f0841ac94b6fa3c..5ae37909a813a4e7238d800dba0ac98fffce6a5f 100644 (file)
@@ -736,7 +736,6 @@ void __put_task_struct(struct task_struct *tsk)
        WARN_ON(tsk == current);
 
        unwind_task_free(tsk);
-       sched_ext_free(tsk);
        io_uring_free(tsk);
        cgroup_task_free(tsk);
        task_numa_free(tsk, true);
index 0324457622d77a18c2ce9b5a560e8990f49d2f31..3f465310621684a158d7dbf3ef332455ddb226a4 100644 (file)
@@ -5151,6 +5151,12 @@ static struct rq *finish_task_switch(struct task_struct *prev)
                if (prev->sched_class->task_dead)
                        prev->sched_class->task_dead(prev);
 
+               /*
+                * sched_ext_dead() must come before cgroup_task_dead() to
+                * prevent cgroups from being removed while its member tasks are
+                * visible to SCX schedulers.
+                */
+               sched_ext_dead(prev);
                cgroup_task_dead(prev);
 
                /* Task is done with its stack. */
index d1ef5bda95aec645f25094e3894fb1a4a18f758f..2811e4f42a379e20c0414998ab82afe825ec569c 100644 (file)
@@ -2966,7 +2966,7 @@ void scx_cancel_fork(struct task_struct *p)
        percpu_up_read(&scx_fork_rwsem);
 }
 
-void sched_ext_free(struct task_struct *p)
+void sched_ext_dead(struct task_struct *p)
 {
        unsigned long flags;