sched/proxy: Handle retained donors on scheduling class transitions
Subsystems (3)
Andrea Righi
reviewerPATCH 1/1 sched/proxy: Handle retained donors on scheduling class transitions
Proxy execution keeps mutex-blocked donors on the runqueue. A BPF scheduler with SCX_OPS_ENQ_BLOCKED admits donors that block while it owns them, but sched_ext is not currently designed to pass it donors retained elsewhere, regardless of SCX_OPS_ENQ_BLOCKED. To enforce this, explicit hooks at the entry points into sched_ext block a retained donor before sched_change_begin() records its queued state.
Tao Cui reported [1] that rt_mutex_setprio() has no such hook: an EXT task that is PI-boosted into RT/DL and blocks on a second mutex while boosted is retained by RT/DL, and when the boost goes away it is handed back to sched_ext as a queued blocked donor that the BPF scheduler never admitted.
The same can happen in __sched_setscheduler(). It calls scx_prepare_setscheduler() to block a retained donor, but only when the requested policy is SCHED_EXT. When the BPF scheduler takes over all tasks (no SCX_OPS_SWITCH_PARTIAL), switching an RT/DL task to SCHED_NORMAL, e.g., via sched_setscheduler(2) or sysrq-n, also moves it into EXT without blocking the donor. In both cases, a BPF scheduler without SCX_OPS_ENQ_BLOCKED receives a queued blocked donor it never
admitted: the donor is enqueued without SCX_ENQ_BLOCKED and, once picked, proxy execution runs the mutex owner on its behalf, even though the BPF scheduler opted out of it.
As suggested by Peter [2], instead of adding more hooks, let the incoming class decide: switching_to() now returns whether the class accepts a retained proxy donor, and sched_change_end() blocks the task when it doesn't. This handles every class transition into sched_ext, including PI deboost, and replaces the existing explicit hooks.
Moreover, moving a task between BPF schedulers (sub-sched enable, rehome, punt) doesn't change its sched_class, so switching_to() doesn't run there. Rather than keeping a separate hook for these paths, make SCX_OPS_ENQ_BLOCKED a property of the whole hierarchy: a sub-scheduler must match the root scheduler's setting or it fails to load, as also suggested by Peter [3]. This makes the remaining scx-specific donor blocking, and sched_proxy_block_task(), unnecessary.
Behavior change: a BPF scheduler with SCX_OPS_ENQ_BLOCKED now inherits retained donors on class entry instead of having them blocked. A queued donor is passed to ops.enqueue() with SCX_ENQ_BLOCKED as soon as the scheduler isn't bypassing. The current donor, like any running task entering sched_ext, keeps running without going through ops.enqueue(), and gets SCX_ENQ_BLOCKED the next time it does.
Git tree: git://git.kernel.org/pub/scm/linux/kernel/git/arighi/linux.git scx-proxy-donors
Peter, should we route this through the sched_ext tree or would you prefer that I rebase it onto tip/sched/core?
Thanks,
-Andrea
[1] https://lore.kernel.org/r/[email protected]
[2] https://lore.kernel.org/r/[email protected]
[3] https://lore.kernel.org/r/[email protected]
Andrea Righi (4):
sched: Let switching_to() decide whether to keep a retained proxy donor
sched_ext: Make SCX_OPS_ENQ_BLOCKED a hierarchy-wide setting
sched: Remove sched_proxy_block_task()
selftests/sched_ext: Check that blocked donors are never enqueued unadmitted
kernel/sched/core.c | 118 ++++++++++++++------- kernel/sched/ext/ext.c | 78 +++++--------- kernel/sched/ext/ext.h | 2 - kernel/sched/ext/internal.h | 4 +- kernel/sched/ext/sub.c | 12 +-- kernel/sched/idle.c | 2 +- kernel/sched/sched.h | 16 +-- kernel/sched/stop_task.c | 2 +- kernel/sched/syscalls.c | 2 - tools/sched_ext/include/scx/enum_defs.autogen.h | 1 - tools/sched_ext/include/scx/enums_abi.autogen.h | 1 - tools/sched_ext/scx_qmap.c | 2 +- .../testing/selftests/sched_ext/enq_blocked.bpf.c | 9 ++ tools/testing/selftests/sched_ext/enq_blocked.c | 18 +++- 14 files changed, 149 insertions(+), 118 deletions(-)
Andrea Righi
reviewerPATCH 1/4 sched: Let switching_to() decide whether to keep a retained proxy donor
Proxy execution retains mutex-blocked donors on the runqueue. A BPF scheduler with SCX_OPS_ENQ_BLOCKED admits donors that block while it owns them, but sched_ext is not currently designed to pass it donors retained elsewhere, regardless of SCX_OPS_ENQ_BLOCKED. To enforce this, explicit hooks in __sched_setscheduler() and in the root enable path block a retained donor before sched_change_begin() records its queued state.
These hooks don't cover every way into sched_ext. For example, rt_mutex_setprio() has no such hook: an EXT task that is PI-boosted into RT/DL and blocks on a second mutex while boosted is retained by RT/DL and handed back to sched_ext as a queued blocked donor on deboost. The __sched_setscheduler() hook also only triggers when the requested policy is SCHED_EXT. When the BPF scheduler takes over all tasks (no SCX_OPS_SWITCH_PARTIAL), switching an RT/DL task to SCHED_NORMAL, e.g., via sched_setscheduler(2) or sysrq-n, also moves it into EXT without blocking the donor. In both cases, a BPF scheduler without SCX_OPS_ENQ_BLOCKED receives a queued blocked donor it never admitted:
the donor is enqueued without SCX_ENQ_BLOCKED and, once picked, proxy execution runs the mutex owner on its behalf, even though the BPF scheduler opted out of it.
Rather than adding more hooks, let the incoming class decide: make switching_to() return whether the class accepts a retained proxy donor. When it doesn't, sched_change_end() blocks the task instead of enqueueing it; sched_change_begin() already dequeued the task from its old class, so finish the block without dequeueing it again: hand the donor role back to rq->curr if needed, account the sleep and clear p->on_rq. The callers hold p->pi_lock, so the task cannot be woken and migrated underneath.
On the sched_ext side, switching_to_scx() refuses retained donors unless the BPF scheduler admits them, which covers every class transition into sched_ext, so the explicit hooks can go. The root enable path also drops DEQUEUE_NOCLOCK, since the hook was the one updating the rq clock.
A BPF scheduler with SCX_OPS_ENQ_BLOCKED now inherits retained donors on class entry instead of having them blocked. A queued donor is passed to ops.enqueue() with SCX_ENQ_BLOCKED as soon as the scheduler isn't bypassing. The current donor, like any running task entering sched_ext, keeps running without going through ops.enqueue(), and gets SCX_ENQ_BLOCKED the next time it does.
Update the enq_blocked selftest, which checked the donor state after root enable, accordingly.
--- kernel/sched/core.c | 90 +++++++++++++++++-- kernel/sched/ext/ext.c | 34 +++---- kernel/sched/ext/ext.h | 2 - kernel/sched/idle.c | 2 +- kernel/sched/sched.h | 9 +- kernel/sched/stop_task.c | 2 +- kernel/sched/syscalls.c | 2 - .../testing/selftests/sched_ext/enq_blocked.c | 7 +- 8 files changed, 111 insertions(+), 37 deletions(-)
kernel/sched/core.c +81 -9
@@ -2252,15 +2252,19 @@ void deactivate_task(struct rq *rq, struct task_struct *p, int flags)dequeue_task (rq, p, flags); } +static inline bool task_state_contributes_to_load (unsigned long task_state) +{ +return (task_state &TASK_UNINTERRUPTIBLE ) && + !(task_state &TASK_NOLOAD ) && + !(task_state &TASK_FROZEN ); +} +static bool dequeue_block_task (struct rq *rq,struct task_struct *p,unsigned long task_state) {int flags =DEQUEUE_NOCLOCK ; - p->sched_contributes_to_load = - (task_state &TASK_UNINTERRUPTIBLE ) && - !(task_state &TASK_NOLOAD ) && - !(task_state &TASK_FROZEN ); + p->sched_contributes_to_load =task_state_contributes_to_load (task_state);if (unlikely (is_special_task_state (task_state))) flags |=DEQUEUE_SPECIAL ;
@@ -3772,6 +3776,17 @@ void update_rq_avg_idle(struct rq *rq) rq->idle_stamp =0 ; } +/* +* Make the execution context the donor again once the old donor has been put +* and rq->curr has been set as the next task. +*/ +static inline void proxy_set_curr_donor (struct rq *rq) +{ + rq->next_class = rq->curr->sched_class; +rq_set_donor (rq, rq->curr); +resched_curr (rq); +} +#ifdef CONFIG_SCHED_PROXY_EXEC static void zap_balance_callbacks (struct rq *rq);
@@ -3780,10 +3795,8 @@ static inline void proxy_reset_donor(struct rq *rq)WARN_ON_ONCE (rq->donor == rq->curr);put_prev_set_next_task (rq, rq->donor, rq->curr); - rq->next_class = rq->curr->sched_class; -rq_set_donor (rq, rq->curr); +proxy_set_curr_donor (rq);zap_balance_callbacks (rq); -resched_curr (rq); }/*
@@ -11200,6 +11213,58 @@ struct sched_change_ctx *sched_change_begin(struct task_struct *p, unsigned intreturn ctx; } +/* +* Block a mutex-blocked task retained on the rq for proxy execution, because +* the class it is switching to does not accept blocked tasks. +* +* sched_change_begin() already dequeued @p from its old class and put it if +* it was the donor, so finish the job a sleeping dequeue would have done +* without going through a class again. +* +* The caller holds p->pi_lock, so @p cannot wake and migrate once +* __block_task() clears p->on_rq. +*/ +static void sched_change_block_task (struct rq *rq,struct sched_change_ctx *ctx) +{ +struct task_struct *p = ctx->p; +unsigned long state =READ_ONCE (p->__state); + +lockdep_assert_held (&p->pi_lock); + +if (WARN_ON_ONCE (state ==TASK_RUNNING )) +return ; + +/* +* Hand the donor role back to the execution context before clearing +* p->on_rq. This is proxy_reset_donor() without put_prev_task(), which +* sched_change_begin() already did, and without zapping the balance +* callbacks: nothing redoes them here, and they include the pull that +* switched_from_rt() or switched_from_dl() may have queued. +* +* If @p is rq->curr, __schedule() retained it and dropped the rq lock +* while picking. Block it like any prev that blocks in __schedule(): +* keep it set, and let the pending pick put it. +*/ +if (ctx->running && rq->curr != p) { +struct task_struct *curr = rq->curr; + +__put_prev_set_next_dl_server (rq, p, curr); + curr->sched_class->set_next_task (rq, curr,SNT_PICK ); +proxy_set_curr_donor (rq); + ctx->running = false; + } + + p->sched_contributes_to_load =task_state_contributes_to_load (state); + +/* The DEQUEUE_SAVE in sched_change_begin() skipped these. */ +sched_info_dequeue (rq, p); +psi_dequeue (p,DEQUEUE_SLEEP ); + +__block_task (rq, p); + + ctx->queued = false; +} +void sched_change_end (struct sched_change_ctx *ctx) {struct task_struct *p = ctx->p;
@@ -11212,8 +11277,15 @@ void sched_change_end(struct sched_change_ctx *ctx)*/ WARN_ON_ONCE (p->sched_class != ctx->class && !(ctx->flags &ENQUEUE_CLASS )); -if ((ctx->flags &ENQUEUE_CLASS ) && p->sched_class->switching_to) - p->sched_class->switching_to (rq, p); +if ((ctx->flags &ENQUEUE_CLASS ) && p->sched_class->switching_to) { +/* +* A class that refuses retained proxy donors gets @p blocked +* instead of enqueued, see struct sched_class::switching_to. +*/ +if (!p->sched_class->switching_to (rq, p) && + ctx->queued && p->is_blocked) +sched_change_block_task (rq, ctx); + }if (ctx->queued)enqueue_task (rq, p, ctx->flags);
kernel/sched/ext/ext.c +14 -20
@@ -4864,12 +4864,12 @@ static void prio_changed_scx(struct rq *rq, struct task_struct *p, u64 oldprio) { } -static void switching_to_scx (struct rq *rq,struct task_struct *p) +static bool switching_to_scx (struct rq *rq,struct task_struct *p) {struct scx_sched *sch =scx_task_sched (p);if (task_dead_and_done (p)) -return ; +return true;scx_enable_task (sch, p);
@@ -4880,6 +4880,18 @@ static void switching_to_scx(struct rq *rq, struct task_struct *p)*/ if (!scx_is_cid_type () &&SCX_HAS_OP (sch, set_cpumask))scx_call_op_set_cpumask (sch, rq, p, (struct cpumask *)p->cpus_ptr); + +/* +* Don't carry a donor retained by another class into sched_ext unless +* the BPF scheduler admits blocked donors. +* +* If it does and @p is the current donor, @p keeps its running session: +* like any running task entering sched_ext, the ENQUEUE_RESTORE in +* sched_change_end() puts it straight into the local DSQ without +* ops.enqueue(). It only carries %SCX_ENQ_BLOCKED once it goes through +* ops.enqueue() while still blocked, e.g., when its slice runs out. +*/ +return scx_allow_proxy_exec (p); }static void switched_from_scx (struct rq *rq,struct task_struct *p)
@@ -4921,20 +4933,6 @@ int scx_check_setscheduler(struct task_struct *p, int policy)return 0 ; } -/* -* Don't carry a donor retained by another class into sched_ext. The caller -* has updated the rq clock and invokes this immediately before -* sched_change_begin() records the task's queued state. -*/ -void scx_prepare_setscheduler (struct task_struct *p,int policy) -{ -lockdep_assert_held (&p->pi_lock); -lockdep_assert_rq_held (task_rq (p)); - -if (scx_enabled () && p->policy != policy && policy ==SCHED_EXT ) -scx_block_proxy_donor (task_rq (p), p); -} -static void process_ddsp_deferred_locals (struct rq *rq) {struct task_struct *p;
@@ -8641,10 +8639,6 @@ static void scx_root_enable_workfn(struct kthread_work *work)if (old_class != new_class) queue_flags |=DEQUEUE_CLASS ; -if (new_class == &ext_sched_class) { -scx_prepare_task_sched_change (p); - queue_flags |=DEQUEUE_NOCLOCK ; - } scoped_guard (sched_change, p, queue_flags) {scx_set_task_slice (p,READ_ONCE (sch->slice_dfl));
kernel/sched/ext/ext.h +0 -2
@@ -18,7 +18,6 @@ bool scx_can_stop_tick(struct rq *rq);void scx_rq_activate (struct rq *rq);void scx_rq_deactivate (struct rq *rq);int scx_check_setscheduler (struct task_struct *p,int policy); -void scx_prepare_setscheduler (struct task_struct *p,int policy);bool task_should_scx (int policy);bool scx_allow_ttwu_queue (const struct task_struct *p);bool __scx_allow_proxy_exec (const struct task_struct *p);
@@ -71,7 +70,6 @@ static inline bool scx_can_stop_tick(struct rq *rq) { return true; }static inline void scx_rq_activate (struct rq *rq) {}static inline void scx_rq_deactivate (struct rq *rq) {}static inline int scx_check_setscheduler (struct task_struct *p,int policy) {return 0 ; } -static inline void scx_prepare_setscheduler (struct task_struct *p,int policy) {}static inline bool task_on_scx (const struct task_struct *p) {return false; }static inline bool scx_allow_ttwu_queue (const struct task_struct *p) {return true; }static inline bool scx_allow_proxy_exec (const struct task_struct *p) {return true; }
kernel/sched/idle.c +1 -1
@@ -543,7 +543,7 @@ static void task_tick_idle(struct rq *rq, struct task_struct *curr, int queued)update_curr_idle (rq); } -static void switching_to_idle (struct rq *rq,struct task_struct *p) +static bool switching_to_idle (struct rq *rq,struct task_struct *p) {BUG (); }
kernel/sched/sched.h +8 -1
@@ -2775,10 +2775,17 @@ struct sched_class {/* * sched_change +* +* switching_to() is called with the rq lock held when @task enters +* this class, before it is enqueued. It returns whether the class +* accepts @task as a retained proxy donor, i.e., a mutex-blocked task +* that is still on the runqueue. If it returns false and @task is such +* a donor, sched_change_end() blocks @task instead of enqueueing it. +* Classes without switching_to() accept retained donors. */ void (*switching_from)(struct rq *this_rq,struct task_struct *task);void (*switched_from) (struct rq *this_rq,struct task_struct *task); -void (*switching_to) (struct rq *this_rq,struct task_struct *task); +bool (*switching_to) (struct rq *this_rq,struct task_struct *task);void (*switched_to) (struct rq *this_rq,struct task_struct *task);u64 (*get_prio) (struct rq *this_rq,struct task_struct *task); void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
kernel/sched/stop_task.c +1 -1
@@ -78,7 +78,7 @@ static void task_tick_stop(struct rq *rq, struct task_struct *curr, int queued) { } -static void switching_to_stop (struct rq *rq,struct task_struct *p) +static bool switching_to_stop (struct rq *rq,struct task_struct *p) {BUG ();/* its impossible to change to this class */ }
kernel/sched/syscalls.c +0 -2
@@ -678,8 +678,6 @@ int __sched_setscheduler(struct task_struct *p,if (prev_class != next_class) queue_flags |=DEQUEUE_CLASS ; -scx_prepare_setscheduler (p, policy); - scoped_guard (sched_change, p, queue_flags) {if (!(attr->sched_flags &SCHED_FLAG_KEEP_PARAMS )) {
tools/testing/selftests/sched_ext/enq_blocked.c +6 -1
@@ -624,8 +624,13 @@ static enum scx_test_status run_one(bool enq_blocked, bool cross_cpu, goto out; } -/* Scheduler ownership changes start from a fully blocked donor. */ +/* +* A scheduler that admits blocked donors inherits the retained donor, +* otherwise it must have been fully blocked on the way in. +*/ donor_state =ENQ_BLOCKED_DONOR_SLEEPING ; +if (enq_blocked) + donor_state |=ENQ_BLOCKED_DONOR_ON_RQ ; err =wait_for_donor_state (&thread_ctx, donor_state);if (err <0 ) { SCX_ERR("Unexpected donor state after scheduler attachment (%d)",
Signature · 1 line
2.56.1
Peter Zijlstra
maintainerRe: [PATCH 1/4] sched: Let switching_to() decide whether to keep a retained proxy donor
Quoted 40 lines · Andrea Righi
kernel/sched/core.c +36
-0@@ -11200,6 +11213,58 @@ struct sched_change_ctx *sched_change_begin(struct task_struct *p, unsigned intreturn ctx; } +/* +* Block a mutex-blocked task retained on the rq for proxy execution, because +* the class it is switching to does not accept blocked tasks. +* +* sched_change_begin() already dequeued @p from its old class and put it if +* it was the donor, so finish the job a sleeping dequeue would have done +* without going through a class again. +* +* The caller holds p->pi_lock, so @p cannot wake and migrate once +* __block_task() clears p->on_rq. +*/ +static void sched_change_block_task (struct rq *rq,struct sched_change_ctx *ctx) +{ +struct task_struct *p = ctx->p; +unsigned long state =READ_ONCE (p->__state); + +lockdep_assert_held (&p->pi_lock); + +if (WARN_ON_ONCE (state ==TASK_RUNNING )) +return ; + +/* +* Hand the donor role back to the execution context before clearing +* p->on_rq. This is proxy_reset_donor() without put_prev_task(), which +* sched_change_begin() already did, and without zapping the balance +* callbacks: nothing redoes them here, and they include the pull that +* switched_from_rt() or switched_from_dl() may have queued. +* +* If @p is rq->curr, __schedule() retained it and dropped the rq lock +* while picking. Block it like any prev that blocks in __schedule(): +* keep it set, and let the pending pick put it. +*/ +if (ctx->running && rq->curr != p) { +struct task_struct *curr = rq->curr; + +__put_prev_set_next_dl_server (rq, p, curr);
kernel/sched/core.c struct sched_change_ctx *sched_change_begin(struct task_struct *p, unsigned int · line 11261
Quoted 10 lines
kernel/sched/core.c +10
-0+ curr->sched_class->set_next_task (rq, curr,SNT_PICK ); +proxy_set_curr_donor (rq); + ctx->running = false; + } + + p->sched_contributes_to_load =task_state_contributes_to_load (state); + +/* The DEQUEUE_SAVE in sched_change_begin() skipped these. */ +sched_info_dequeue (rq, p); +psi_dequeue (p,DEQUEUE_SLEEP );
Should we assert DEQUEUE_SAVE was set? I mean, it should be, but afaict nothing screams if someone forgets? And with these changes on, it will go a bit funny.
kernel/sched/core.c struct sched_change_ctx *sched_change_begin(struct task_struct *p, unsigned int · line 11266
Quoted 4 lines
kernel/sched/core.c +4
-0+__block_task (rq, p); + + ctx->queued = false; +}
Other than that, this looks good. Let me go read the next patches.
Andrea Righi
reviewerPATCH 2/4 sched_ext: Make SCX_OPS_ENQ_BLOCKED a hierarchy-wide setting
In reply to Andrea RighiMoving a task between BPF schedulers (sub-sched enable, rehome and punt) doesn't change its sched_class, so switching_to() doesn't run and can't keep a scheduler that doesn't admit blocked donors from inheriting one. scx_prepare_task_sched_change() works around this by blocking a retained donor before every ownership change, even when both schedulers admit blocked donors.
Mixing proxy donor admission within a hierarchy has no real use, so don't allow it: a sub-scheduler must match the root scheduler's SCX_OPS_ENQ_BLOCKED setting or it fails to load. Ownership changes then can't affect donor admission: if the hierarchy doesn't admit blocked donors, no EXT task is a retained donor; if it does, the incoming scheduler accepts the donor just like on class entry.
Drop scx_prepare_task_sched_change() and the DEQUEUE_NOCLOCK it required from the sub-sched paths. With that, sched_ext never blocks a donor that it still owns, so scx_block_proxy_donor() and the SCX_RQ_PROXY_BLOCKING special case in put_prev_task_scx() go away too.
Update the scx_qmap -X help text and drop SCX_RQ_PROXY_BLOCKING from the generated enum headers.
--- kernel/sched/ext/ext.c | 44 ++++++------------- kernel/sched/ext/internal.h | 4 +- kernel/sched/ext/sub.c | 12 ++--- kernel/sched/sched.h | 1 - .../sched_ext/include/scx/enum_defs.autogen.h | 1 - .../sched_ext/include/scx/enums_abi.autogen.h | 1 - tools/sched_ext/scx_qmap.c | 2 +- 7 files changed, 20 insertions(+), 45 deletions(-)
kernel/sched/ext/ext.c +13 -31
@@ -24,17 +24,6 @@DEFINE_RAW_SPINLOCK (scx_sched_lock); -/* -* Block a retained proxy donor, telling put_prev_task_scx() that the donor is -* about to be dequeued and must not be reenqueued. -*/ -static void scx_block_proxy_donor (struct rq *rq,struct task_struct *p) -{ - rq->scx.flags |=SCX_RQ_PROXY_BLOCKING ; -sched_proxy_block_task (rq, p); - rq->scx.flags &= ~SCX_RQ_PROXY_BLOCKING ; -} -bool __scx_allow_proxy_exec (const struct task_struct *p) {struct scx_sched *sch;
@@ -46,21 +35,6 @@ bool __scx_allow_proxy_exec(const struct task_struct *p)return !sch || (sch->ops.flags &SCX_OPS_ENQ_BLOCKED ); } -/* -* End retained proxy execution before changing @p's BPF scheduler ownership. -* Called with @p's pi and rq locks held immediately before -* sched_change_begin(). The caller must pass DEQUEUE_NOCLOCK so the rq clock -* is updated only once. -*/ -void scx_prepare_task_sched_change (struct task_struct *p) -{ -lockdep_assert_held (&p->pi_lock); -lockdep_assert_rq_held (task_rq (p)); - -update_rq_clock (task_rq (p)); -scx_block_proxy_donor (task_rq (p), p); -} -/* * NOTE: sched_ext is in the process of growing multiple scheduler support and * scx_root usage is in a transitional state. Naked dereferences are safe if the
@@ -3596,7 +3570,6 @@ static void put_prev_task_scx(struct rq *rq, struct task_struct *p,struct scx_sched *sch =scx_task_sched (p);bool proxy_put = p->is_blocked && next == rq->idle && (rq->scx.flags &SCX_RQ_PROXY_PENDING ); -bool proxy_block = rq->scx.flags &SCX_RQ_PROXY_BLOCKING ;bool rescue_keep = false; rq->scx.flags &= ~SCX_RQ_PROXY_PENDING ;
@@ -3624,15 +3597,12 @@ static void put_prev_task_scx(struct rq *rq, struct task_struct *p,if (next != p && (p->scx.flags &SCX_TASK_QUEUED ) && (p->scx.flags &SCX_TASK_RUN_TRACKED )) {if (SCX_HAS_OP (sch, stopping)) -SCX_CALL_OP_TASK (sch, stopping, rq, p, !proxy_block); +SCX_CALL_OP_TASK (sch, stopping, rq, p, true); p->scx.flags &= ~SCX_TASK_RUN_TRACKED ; }if (p->scx.flags &SCX_TASK_QUEUED ) { -if (proxy_block) - goto switch_class; -set_task_runnable (rq, p);/*
@@ -8246,6 +8216,18 @@ int scx_validate_ops(struct scx_sched *sch, const struct sched_ext_ops *ops)return -EINVAL ; } +/* +* Proxy donor admission is a property of the whole hierarchy, so that +* moving a task between schedulers never changes whether a retained +* donor is admitted. Sub-scheds must follow the root scheduler. +*/ +if (scx_parent (sch) && + (ops->flags &SCX_OPS_ENQ_BLOCKED ) != + (sch->ancestors[0 ]->ops.flags &SCX_OPS_ENQ_BLOCKED )) { +scx_error (sch,"SCX_OPS_ENQ_BLOCKED must match the root scheduler" ); +return -EINVAL ; + } +/* * SCX_OPS_TID_TO_TASK is enabled by the root scheduler. A sub-sched * may set it to declare a dependency; reject if the root hasn't
kernel/sched/ext/internal.h +3 -1
@@ -240,6 +240,9 @@ enum scx_ops_flags {* * For blocked donors, this flag takes precedence over * %SCX_OPS_ENQ_EXITING and %SCX_OPS_ENQ_MIGRATION_DISABLED. +* +* The root scheduler decides for the whole hierarchy. A sub-scheduler +* must match the root's setting or it fails to load. */ SCX_OPS_ENQ_BLOCKED = 1LLU << 10,
@@ -2296,7 +2299,6 @@ struct task_struct *scx_task_iter_next_locked(struct scx_task_iter *iter);bool scx_set_task_slice (struct task_struct *p,u64 slice);void scx_task_slice_ended (struct rq *rq,struct task_struct *p);void scx_task_unlink_from_dsq (struct task_struct *p,struct scx_dispatch_q *dsq); -void scx_prepare_task_sched_change (struct task_struct *p);void scx_dispatch_dequeue (struct rq *rq,struct task_struct *p);void scx_do_enqueue_task (struct rq *rq,struct task_struct *p,u64 enq_flags,int sticky_cpu);
kernel/sched/ext/sub.c +3 -9
@@ -1623,9 +1623,7 @@ static void scx_rehome_task(struct scx_sched *to, struct task_struct *p)lockdep_assert_held (&p->pi_lock);lockdep_assert_rq_held (task_rq (p)); -scx_prepare_task_sched_change (p); -scoped_guard (sched_change, p, - DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK) { + scoped_guard (sched_change, p, DEQUEUE_SAVE | DEQUEUE_MOVE) {scx_disable_and_exit_task (scx_task_sched (p), p);scx_set_task_state (p,SCX_TASK_INIT_BEGIN );scx_set_task_state (p,SCX_TASK_INIT );
@@ -1655,9 +1653,7 @@ static void scx_punt_task(struct scx_sched *to, struct task_struct *p)lockdep_assert_rq_held (task_rq (p));WARN_ON_ONCE (!READ_ONCE (to->bypass_depth)); -scx_prepare_task_sched_change (p); -scoped_guard (sched_change, p, - DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK) { + scoped_guard (sched_change, p, DEQUEUE_SAVE | DEQUEUE_MOVE) {scx_disable_and_exit_task (scx_task_sched (p), p);scx_set_task_sched (p, to); }
@@ -2434,9 +2430,7 @@ void scx_sub_enable_workfn(struct kthread_work *work)if (!(p->scx.flags &SCX_TASK_SUB_INIT ))continue ; -scx_prepare_task_sched_change (p); -scoped_guard (sched_change, p, - DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK) { + scoped_guard (sched_change, p, DEQUEUE_SAVE | DEQUEUE_MOVE) {/* * $p must be either READY or ENABLED. If ENABLED, * __scx_disabled_and_exit_task() first disables and
kernel/sched/sched.h +0 -1
@@ -803,7 +803,6 @@ enum scx_rq_flags {SCX_RQ_PROXY_RETRY =1 <<9 ,/* proxy-rejected tasks need retry */ SCX_RQ_PROXY_TICK =1 <<10 ,/* proxy execution requires the tick */ SCX_RQ_PROXY_PENDING =1 <<11 ,/* blocked donor awaits proxy resolution */ -SCX_RQ_PROXY_BLOCKING =1 <<12 ,/* donor is being removed for sched change */ SCX_RQ_IN_WAKEUP =1 <<16 , SCX_RQ_IN_DISPATCH = 1 << 17,
tools/sched_ext/include/scx/enum_defs.autogen.h +0 -1
@@ -208,7 +208,6 @@#define HAVE_SCX_RQ_PROXY_RETRY #define HAVE_SCX_RQ_PROXY_TICK #define HAVE_SCX_RQ_PROXY_PENDING -#define HAVE_SCX_RQ_PROXY_BLOCKING #define HAVE_SCX_RQ_IN_WAKEUP #define HAVE_SCX_RQ_IN_DISPATCH #define HAVE_SCX_SCHED_PCPU_BYPASSING
tools/sched_ext/include/scx/enums_abi.autogen.h +0 -1
@@ -220,7 +220,6 @@ static const struct __scx_enum_abi_val __scx_enum_abi_vals[] { "scx_rq_flags", "SCX_RQ_PROXY_RETRY", 0x200LLU }, { "scx_rq_flags", "SCX_RQ_PROXY_TICK", 0x400LLU }, { "scx_rq_flags", "SCX_RQ_PROXY_PENDING", 0x800LLU }, - { "scx_rq_flags", "SCX_RQ_PROXY_BLOCKING", 0x1000LLU }, { "scx_rq_flags", "SCX_RQ_IN_WAKEUP", 0x10000LLU }, { "scx_rq_flags", "SCX_RQ_IN_DISPATCH", 0x20000LLU }, { "scx_sched_pcpu_flags", "SCX_SCHED_PCPU_BYPASSING", 0x1LLU },
tools/sched_ext/scx_qmap.c +1 -1
@@ -65,7 +65,7 @@ const char help_fmt[] =" -S Suppress qmap-specific debug dump\n" " -p Switch only tasks on SCHED_EXT policy instead of all\n" " -I Turn on SCX_OPS_ALWAYS_ENQ_IMMED\n" -" -X Turn on SCX_OPS_ENQ_BLOCKED\n" +" -X Turn on SCX_OPS_ENQ_BLOCKED, sub-schedulers must match the root\n" " -F COUNT IMMED stress: force every COUNT'th enqueue to a busy local DSQ (use with -I)\n" " -C MODE cid-override test (shuffle|bad-dup|bad-range|bad-mono)\n" " -i SEC Stats interval, seconds (default 5)\n"
Signature · 1 line
2.56.1
Andrea Righi
reviewerPATCH 3/4 sched: Remove sched_proxy_block_task()
In reply to Andrea Righisched_ext was the only user, and it now relies on switching_to() refusing retained donors instead.
--- kernel/sched/core.c | 28 ---------------------------- kernel/sched/sched.h | 6 ------ 2 files changed, 34 deletions(-)
kernel/sched/core.c +0 -28
@@ -6855,34 +6855,6 @@ static void proxy_deactivate(struct rq *rq, struct task_struct *donor)block_task (rq, donor, state); } -/* -* Remove a retained proxy donor before changing its scheduler ownership. -* The caller holds p->pi_lock, so p cannot wake and migrate if block_task() -* drops it from the runqueue. If DELAY_DEQUEUE keeps a blocked fair task -* queued, switching_from_fair() completes the dequeue in the immediately -* following sched_change_begin(). -*/ -void sched_proxy_block_task (struct rq *rq,struct task_struct *p) -{ -unsigned long state =READ_ONCE (p->__state); - -lockdep_assert_held (&p->pi_lock); -lockdep_assert_rq_held (rq); - -if (!p->is_blocked || !task_on_rq_queued (p)) -return ; -if (WARN_ON_ONCE (state ==TASK_RUNNING )) -return ; - -if (task_current_donor (rq, p)) -proxy_reset_donor (rq); - -if (!p->se.sched_delayed) -block_task (rq, p, state); - -WARN_ON_ONCE (task_on_rq_queued (p) && !p->se.sched_delayed); -} -static inline void proxy_release_rq_lock (struct rq *rq,struct rq_flags *rf)__releases (__rq_lockp (rq)) {
kernel/sched/sched.h +0 -6
@@ -2534,12 +2534,6 @@ static inline bool task_is_blocked(struct task_struct *p)return !!p->blocked_on; } -#ifdef CONFIG_SCHED_PROXY_EXEC -void sched_proxy_block_task (struct rq *rq,struct task_struct *p); -#else -static inline void sched_proxy_block_task (struct rq *rq,struct task_struct *p) {} -#endif -static inline int task_on_cpu (struct rq *rq,struct task_struct *p) {return p->on_cpu;
Signature · 1 line
2.56.1
Andrea Righi
reviewerPATCH 4/4 selftests/sched_ext: Check that blocked donors are never enqueued unadmitted
In reply to Andrea Righienq_blocked attaches the BPF scheduler while the fair class retains a mutex-blocked donor, so the root enable moves the donor into sched_ext through switching_to_scx(), the same path taken by every other class transition into sched_ext, such as PI deboost or sched_setscheduler().
On top of the donor state checks, verify that ops.enqueue() never sees a retained donor without SCX_ENQ_BLOCKED during the whole run, including the scheduler attachment. A full wakeup enqueue happens before is_blocked is cleared, so SCX_ENQ_WAKEUP enqueues are not counted.
--- tools/testing/selftests/sched_ext/enq_blocked.bpf.c | 9 +++++++++ tools/testing/selftests/sched_ext/enq_blocked.c | 11 +++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-)
tools/testing/selftests/sched_ext/enq_blocked.bpf.c +9 -0
@@ -20,6 +20,7 @@ u64 nr_blocked_enqueues_donor_cpu;u64 nr_blocked_enqueues_owner_cpu;u64 nr_blocked_enqueues_other_cpu;u64 nr_blocked_wakeups; +u64 nr_unadmitted_enqueues;static u64 vtime_now;UEI_DEFINE (uei);
@@ -34,6 +35,14 @@ void BPF_STRUCT_OPS(enq_blocked_enqueue, struct task_struct *p, u64 enq_flags) {u64 vtime = p->scx.dsq_vtime; +/* +* A retained proxy donor must only be enqueued with SCX_ENQ_BLOCKED. A +* full wakeup enqueue happens before is_blocked is cleared. +*/ +if (p->pid == donor_pid && p->is_blocked && + !(enq_flags & (SCX_ENQ_BLOCKED |SCX_ENQ_WAKEUP ))) +__sync_fetch_and_add (&nr_unadmitted_enqueues,1 ); +if (enq_flags &SCX_ENQ_BLOCKED ) {int cpu =scx_bpf_task_cpu (p);
tools/testing/selftests/sched_ext/enq_blocked.c +9 -2
@@ -497,7 +497,7 @@ static enum scx_test_status run_one(bool enq_blocked, bool cross_cpu,size_t i, nr_contenders, nr_contenders_started =0 ;size_t nr_contenders_joined =0 ;u64 nr_blocked, nr_blocked_donor_cpu, nr_blocked_owner_cpu; -u64 nr_blocked_other_cpu, nr_blocked_wakeups; +u64 nr_blocked_other_cpu, nr_blocked_wakeups, nr_unadmitted;struct enq_blocked_stats stats; err = select_test_cpus(cross_cpu, &allowed_cpus, &thread_ctx.donor_cpu,
@@ -815,6 +815,7 @@ static enum scx_test_status run_one(bool enq_blocked, bool cross_cpu, nr_blocked_owner_cpu = skel->bss->nr_blocked_enqueues_owner_cpu; nr_blocked_other_cpu = skel->bss->nr_blocked_enqueues_other_cpu; nr_blocked_wakeups = skel->bss->nr_blocked_wakeups; + nr_unadmitted = skel->bss->nr_unadmitted_enqueues; result->nr_blocked_enqueues = nr_blocked; result->nr_blocked_enqueues_donor_cpu = nr_blocked_donor_cpu; result->nr_blocked_enqueues_owner_cpu = nr_blocked_owner_cpu;
@@ -830,8 +831,14 @@ static enum scx_test_status run_one(bool enq_blocked, bool cross_cpu, (unsigned long long)nr_blocked_other_cpu);printf (" nr_blocked_wakeups=%llu\n" , (unsigned long long )nr_blocked_wakeups); +printf (" nr_unadmitted_enqueues=%llu\n" , + (unsigned long long )nr_unadmitted);if (status ==SCX_TEST_PASS ) { -if (enq_blocked && proxy_enabled && !nr_blocked) { +if (nr_unadmitted) { +SCX_ERR ("ops.enqueue() received %llu blocked donors without SCX_ENQ_BLOCKED" , + (unsigned long long )nr_unadmitted); + status =SCX_TEST_FAIL ; + }else if (enq_blocked && proxy_enabled && !nr_blocked) {SCX_ERR ("ops.enqueue() did not receive the blocked donor" ); status =SCX_TEST_FAIL ; }else if ((!enq_blocked || !proxy_enabled) && nr_blocked) {
Signature · 1 line
2.56.1