Skip to content

Commit 79bd2dd

Browse files
committed
Merge tag 'sched_ext-for-7.1-rc4-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/sched_ext
Pull sched_ext fixes from Tejun Heo: - Spurious WARN in ops_dequeue() racing with concurrent dispatch - Self-deadlock between scheduler disable and a concurrent sub-sched enable * tag 'sched_ext-for-7.1-rc4-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/sched_ext: sched_ext: Fix spurious WARN on stale ops_state in ops_dequeue() sched_ext: Fix deadlock between scx_root_disable() and concurrent forks
2 parents de37e50 + 0c1a9dc commit 79bd2dd

1 file changed

Lines changed: 36 additions & 3 deletions

File tree

kernel/sched/ext.c

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2078,6 +2078,7 @@ static void ops_dequeue(struct rq *rq, struct task_struct *p, u64 deq_flags)
20782078
/* dequeue is always temporary, don't reset runnable_at */
20792079
clr_task_runnable(p, false);
20802080

2081+
retry:
20812082
/* acquire ensures that we see the preceding updates on QUEUED */
20822083
opss = atomic_long_read_acquire(&p->scx.ops_state);
20832084

@@ -2091,8 +2092,20 @@ static void ops_dequeue(struct rq *rq, struct task_struct *p, u64 deq_flags)
20912092
*/
20922093
BUG();
20932094
case SCX_OPSS_QUEUED:
2094-
/* A queued task must always be in BPF scheduler's custody */
2095-
WARN_ON_ONCE(!(p->scx.flags & SCX_TASK_IN_CUSTODY));
2095+
/*
2096+
* A queued task must always be in BPF scheduler's custody. If
2097+
* SCX_TASK_IN_CUSTODY is clear, finish_dispatch() on another
2098+
* CPU has already passed call_task_dequeue() (which clears the
2099+
* flag), but has not yet written SCX_OPSS_NONE. That final
2100+
* store does not require this rq's lock, so retrying with
2101+
* cpu_relax() is bounded: we will observe NONE (or DISPATCHING,
2102+
* handled by the fallthrough) on a subsequent iteration.
2103+
*/
2104+
if (unlikely(!(READ_ONCE(p->scx.flags) & SCX_TASK_IN_CUSTODY))) {
2105+
cpu_relax();
2106+
goto retry;
2107+
}
2108+
20962109
if (atomic_long_try_cmpxchg(&p->scx.ops_state, &opss,
20972110
SCX_OPSS_NONE))
20982111
break;
@@ -4946,10 +4959,30 @@ static const struct kset_uevent_ops scx_uevent_ops = {
49464959
*/
49474960
bool task_should_scx(int policy)
49484961
{
4949-
if (!scx_enabled() || unlikely(scx_enable_state() == SCX_DISABLING))
4962+
/* if disabled, nothing should be on it */
4963+
if (!scx_enabled())
49504964
return false;
4965+
4966+
/* scx is taking over all SCHED_OTHER and SCHED_EXT tasks */
49514967
if (READ_ONCE(scx_switching_all))
49524968
return true;
4969+
4970+
/*
4971+
* scx is tearing down - keep new SCHED_EXT tasks out.
4972+
*
4973+
* Must come after scx_switching_all test, which serves as a proxy
4974+
* for __scx_switched_all. While __scx_switched_all is set, we must
4975+
* return true via the branch above: a fork routed to fair would
4976+
* stall because next_active_class() skips fair.
4977+
*
4978+
* This can develop into a deadlock - scx holds scx_enable_mutex across
4979+
* kthread_create() in scx_alloc_and_add_sched(); if the new kthread is
4980+
* the stalled task, the disable path can never grab the mutex to clear
4981+
* scx_switching_all.
4982+
*/
4983+
if (unlikely(scx_enable_state() == SCX_DISABLING))
4984+
return false;
4985+
49534986
return policy == SCHED_EXT;
49544987
}
49554988

0 commit comments

Comments
 (0)