Skip to content

Commit d6f3e7d

Browse files
committed
sched_ext: Fix incorrect autogroup migration detection
scx_move_task() is called from sched_move_task() and tells the BPF scheduler that cgroup migration is being committed. sched_move_task() is used by both cgroup and autogroup migrations and scx_move_task() tried to filter out autogroup migrations by testing the destination cgroup and PF_EXITING but this is not enough. In fact, without explicitly tagging the thread which is doing the cgroup migration, there is no good way to tell apart scx_move_task() invocations for racing migration to the root cgroup and an autogroup migration. This led to scx_move_task() incorrectly ignoring a migration from non-root cgroup to an autogroup of the root cgroup triggering the following warning: WARNING: CPU: 7 PID: 1 at kernel/sched/ext.c:3725 scx_cgroup_can_attach+0x196/0x340 ... Call Trace: <TASK> cgroup_migrate_execute+0x5b1/0x700 cgroup_attach_task+0x296/0x400 __cgroup_procs_write+0x128/0x140 cgroup_procs_write+0x17/0x30 kernfs_fop_write_iter+0x141/0x1f0 vfs_write+0x31d/0x4a0 __x64_sys_write+0x72/0xf0 do_syscall_64+0x82/0x160 entry_SYSCALL_64_after_hwframe+0x76/0x7e Fix it by adding an argument to sched_move_task() that indicates whether the moving is for a cgroup or autogroup migration. After the change, scx_move_task() is called only for cgroup migrations and renamed to scx_cgroup_move_task(). Link: sched-ext/scx#370 Fixes: 8195136 ("sched_ext: Add cgroup support") Cc: [email protected] # v6.12+ Acked-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: Tejun Heo <[email protected]>
1 parent e9fe182 commit d6f3e7d

File tree

5 files changed

+10
-22
lines changed

5 files changed

+10
-22
lines changed

kernel/sched/autogroup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ void sched_autogroup_exit_task(struct task_struct *p)
150150
* see this thread after that: we can no longer use signal->autogroup.
151151
* See the PF_EXITING check in task_wants_autogroup().
152152
*/
153-
sched_move_task(p);
153+
sched_move_task(p, true);
154154
}
155155

156156
static void
@@ -182,7 +182,7 @@ autogroup_move_group(struct task_struct *p, struct autogroup *ag)
182182
* sched_autogroup_exit_task().
183183
*/
184184
for_each_thread(p, t)
185-
sched_move_task(t);
185+
sched_move_task(t, true);
186186

187187
unlock_task_sighand(p, &flags);
188188
autogroup_kref_put(prev);

kernel/sched/core.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9042,7 +9042,7 @@ static void sched_change_group(struct task_struct *tsk, struct task_group *group
90429042
* now. This function just updates tsk->se.cfs_rq and tsk->se.parent to reflect
90439043
* its new group.
90449044
*/
9045-
void sched_move_task(struct task_struct *tsk)
9045+
void sched_move_task(struct task_struct *tsk, bool for_autogroup)
90469046
{
90479047
int queued, running, queue_flags =
90489048
DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK;
@@ -9071,7 +9071,8 @@ void sched_move_task(struct task_struct *tsk)
90719071
put_prev_task(rq, tsk);
90729072

90739073
sched_change_group(tsk, group);
9074-
scx_move_task(tsk);
9074+
if (!for_autogroup)
9075+
scx_cgroup_move_task(tsk);
90759076

90769077
if (queued)
90779078
enqueue_task(rq, tsk, queue_flags);
@@ -9172,7 +9173,7 @@ static void cpu_cgroup_attach(struct cgroup_taskset *tset)
91729173
struct cgroup_subsys_state *css;
91739174

91749175
cgroup_taskset_for_each(task, css, tset)
9175-
sched_move_task(task);
9176+
sched_move_task(task, false);
91769177

91779178
scx_cgroup_finish_attach();
91789179
}

kernel/sched/ext.c

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4323,24 +4323,11 @@ int scx_cgroup_can_attach(struct cgroup_taskset *tset)
43234323
return ops_sanitize_err("cgroup_prep_move", ret);
43244324
}
43254325

4326-
void scx_move_task(struct task_struct *p)
4326+
void scx_cgroup_move_task(struct task_struct *p)
43274327
{
43284328
if (!scx_cgroup_enabled)
43294329
return;
43304330

4331-
/*
4332-
* We're called from sched_move_task() which handles both cgroup and
4333-
* autogroup moves. Ignore the latter.
4334-
*
4335-
* Also ignore exiting tasks, because in the exit path tasks transition
4336-
* from the autogroup to the root group, so task_group_is_autogroup()
4337-
* alone isn't able to catch exiting autogroup tasks. This is safe for
4338-
* cgroup_move(), because cgroup migrations never happen for PF_EXITING
4339-
* tasks.
4340-
*/
4341-
if (task_group_is_autogroup(task_group(p)) || (p->flags & PF_EXITING))
4342-
return;
4343-
43444331
/*
43454332
* @p must have ops.cgroup_prep_move() called on it and thus
43464333
* cgrp_moving_from set.

kernel/sched/ext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static inline void scx_update_idle(struct rq *rq, bool idle, bool do_notify) {}
7373
int scx_tg_online(struct task_group *tg);
7474
void scx_tg_offline(struct task_group *tg);
7575
int scx_cgroup_can_attach(struct cgroup_taskset *tset);
76-
void scx_move_task(struct task_struct *p);
76+
void scx_cgroup_move_task(struct task_struct *p);
7777
void scx_cgroup_finish_attach(void);
7878
void scx_cgroup_cancel_attach(struct cgroup_taskset *tset);
7979
void scx_group_set_weight(struct task_group *tg, unsigned long cgrp_weight);
@@ -82,7 +82,7 @@ void scx_group_set_idle(struct task_group *tg, bool idle);
8282
static inline int scx_tg_online(struct task_group *tg) { return 0; }
8383
static inline void scx_tg_offline(struct task_group *tg) {}
8484
static inline int scx_cgroup_can_attach(struct cgroup_taskset *tset) { return 0; }
85-
static inline void scx_move_task(struct task_struct *p) {}
85+
static inline void scx_cgroup_move_task(struct task_struct *p) {}
8686
static inline void scx_cgroup_finish_attach(void) {}
8787
static inline void scx_cgroup_cancel_attach(struct cgroup_taskset *tset) {}
8888
static inline void scx_group_set_weight(struct task_group *tg, unsigned long cgrp_weight) {}

kernel/sched/sched.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ extern void sched_online_group(struct task_group *tg,
572572
extern void sched_destroy_group(struct task_group *tg);
573573
extern void sched_release_group(struct task_group *tg);
574574

575-
extern void sched_move_task(struct task_struct *tsk);
575+
extern void sched_move_task(struct task_struct *tsk, bool for_autogroup);
576576

577577
#ifdef CONFIG_FAIR_GROUP_SCHED
578578
extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);

0 commit comments

Comments
 (0)