From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.210 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796180829; b=lnUfas7ZDnLOBFxErh4mXQSbHB/Kb0IGNUoCOxd5+iu2lh8cuncI/R1CHck/nyq0h8ij PlzV8TJYGYWdCNDq1vrh2cA2k7cZGm5EZL/5LNXKIS4f6hVBE3fZt6ewU0ffme4pF99FE zVDwmdNqCKLEzXkO2BBI1/+6fUcukWl2t7lZDoyGobQJVBGuTLQIfXUxeKzRxNs0Fmd3g fjmHGd1/UP1qn7pJnLRYWEiAZrvkCxtRAiJtMWkc+UVMnsAG5IUL7nCIFGVTsvTDo3eKD gpg2jZIKYfCu79GrcCpBEaUBTjyGPNkH+tzZXLgelKGpe42LfFifodQ66rAIJo675yQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796180829; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:Message-ID:Date:MIME-Version:User-Agent: Cc:Subject:To:References:From:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=2PJHJn029jDv0h68IUGjaD4wu69s8Shsid4AEzK6VwY=; b=rEKIst8cWvEA23XggSmpBMyvv+EaocYj7/BMc17kTZpb9/ZclKvz5k6GPZnpjy+FKJPL wEICVgw0l5v/jiRaX6Wm5JLOcyiLCbE3mY/e6fxfFNTlGzXhGktgv5gyTyno1+niP0Tt7 oKI2BZ6YG4jzo74Ys7+vXkCnP+84H1Kbp/4xB3ySJvmS/nv1t+gxde60ek1PMimt4uMYD mp0WfYfjM9LcE+Mj9aoyXY3up58BMzxNsDjIYvNbod0qYYN5yhHwGVZSeDznZ2HiQPmEg 2ISUcx6BzbJ848lnpUjcLPB0XIoOYsmzE8f+FruE3rZfb/ho81ap1qAUqOl5CMD71bQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=e8B6BB8Z; arc=none smtp.remote-ip=95.215.58.210 Received: from mta1.migadu.com (out-210.mta1.migadu.com [95.215.58.210]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5DE49606A31A for ; Wed, 2 Dec 2026 03:07:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5DE49606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=e8B6BB8Z X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=G+Q92xJSEoeG3hgbJ92GHvKjKSM4WsSwCTJjM9UbiCI=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796180826; v=1; x=1796785626; b=e8B6BB8Ztv8EWTEFWHnglyu6bMfcmEEQMKIwI3YWrHk99Np4iRvD0ROCU0805dIk2d4kj3lp KUNJbmKUP/M8MCYhDWo8quhsUSL8vLvWPRO9pq3XDWJsonBuCaO8siqVh/jO/kj1rgermV6fK7e xBLOvYtdwMNbNv1v3N4gGmPI= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 0e5edec84099b9d9; Wed, 02 Dec 2026 03:07:06 +0000 X-Mizu-Trace-ID: 0e5edec84099b9d9 X-Migadu-Flow: FLOW_OUT Message-ID: Date: Wed, 2 Dec 2026 11:06:58 +0800 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Cc: cui.tao@linux.dev, Ingo Molnar , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , K Prateek Nayak , John Stultz , David Vernet , Changwoo Min , Tao Cui , sched-ext@lists.linux.dev, linux-kernel@vger.kernel.org Subject: Re: [PATCH 1/4] sched: Let switching_to() decide whether to keep a retained proxy donor To: Andrea Righi , Peter Zijlstra , Tejun Heo References: <20261126111649.208476-1-arighi@nvidia.com> <20261126111649.208476-2-arighi@nvidia.com> From: Tao Cui In-Reply-To: <20261126111649.208476-2-arighi@nvidia.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Hello, Andrea, 在 2026/11/26 19:13, Andrea Righi 写道: > 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. > Tested on qemu (x86_64, 4 vCPUs, CONFIG_SCHED_PROXY_EXEC=y), scx-proxy-donors, base cb31a095aab1 vs head 4e05843dbb48. This exercises the PI deboost hand-back from my report (donor PI-boosted into RT, blocks on a second kernel mutex while boosted, waiter times out) and an RT donor switched to SCHED_NORMAL under full takeover. On the baseline, both scenarios hand sched_ext a queued blocked donor without SCX_ENQ_BLOCKED. With the series, the donor is blocked on class entry instead, and with SCX_OPS_ENQ_BLOCKED it reaches ops.enqueue() with SCX_ENQ_BLOCKED set in every run. enq_blocked passes in all four combinations with nr_unadmitted_enqueues staying 0, and the rest of the sched_ext test suite passes except dsp_local_on, which fails in my initramfs because libbpf can't find a kernel config, unrelated to the series. Tested-by: Tao Cui Thanks, Tao > Fixes: be8be8acb268 ("sched_ext: Block proxy donors before taking control") > Reported-by: Tao Cui > Closes: https://lore.kernel.org/r/20261112144935.583937-3-cui.tao@linux.dev > Suggested-by: Peter Zijlstra > Signed-off-by: Andrea Righi > --- > 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(-) > > diff --git a/kernel/sched/core.c b/kernel/sched/core.c > index 2f46922982f32..1b4ec78738861 100644 > --- a/kernel/sched/core.c > +++ b/kernel/sched/core.c > @@ -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 int > return 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); > diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c > index e3d1bb3fadd84..99038c2f6f4b0 100644 > --- a/kernel/sched/ext/ext.c > +++ b/kernel/sched/ext/ext.c > @@ -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)); > diff --git a/kernel/sched/ext/ext.h b/kernel/sched/ext/ext.h > index d32bc01071b01..f2b98b870f083 100644 > --- a/kernel/sched/ext/ext.h > +++ b/kernel/sched/ext/ext.h > @@ -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; } > diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c > index 76f3c84ca6846..23217b91db03a 100644 > --- a/kernel/sched/idle.c > +++ b/kernel/sched/idle.c > @@ -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(); > } > diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h > index 9e7993256c817..11d38b2e18f05 100644 > --- a/kernel/sched/sched.h > +++ b/kernel/sched/sched.h > @@ -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, > diff --git a/kernel/sched/stop_task.c b/kernel/sched/stop_task.c > index 1e0109ec36b3f..b7541bb7b5933 100644 > --- a/kernel/sched/stop_task.c > +++ b/kernel/sched/stop_task.c > @@ -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 */ > } > diff --git a/kernel/sched/syscalls.c b/kernel/sched/syscalls.c > index 45a127deeadd9..b215b0ead9a60 100644 > --- a/kernel/sched/syscalls.c > +++ b/kernel/sched/syscalls.c > @@ -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)) { > diff --git a/tools/testing/selftests/sched_ext/enq_blocked.c b/tools/testing/selftests/sched_ext/enq_blocked.c > index 1d5e090b54bc5..54ee5633c5720 100644 > --- a/tools/testing/selftests/sched_ext/enq_blocked.c > +++ b/tools/testing/selftests/sched_ext/enq_blocked.c > @@ -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)",