mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Tao Cui <cui.tao@linux.dev>
To: Andrea Righi <arighi@nvidia.com>,
	Peter Zijlstra <peterz@infradead.org>, Tejun Heo <tj@kernel.org>
Cc: cui.tao@linux.dev, Ingo Molnar <mingo@redhat.com>,
	Juri Lelli <juri.lelli@redhat.com>,
	Vincent Guittot <vincent.guittot@linaro.org>,
	Dietmar Eggemann <dietmar.eggemann@arm.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>,
	Valentin Schneider <vschneid@redhat.com>,
	K Prateek Nayak <kprateek.nayak@amd.com>,
	John Stultz <jstultz@google.com>,
	David Vernet <void@manifault.com>,
	Changwoo Min <changwoo@igalia.com>, Tao Cui <cuitao@kylinos.cn>,
	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
Date: Wed, 2 Dec 2026 11:06:58 +0800	[thread overview]
Message-ID: <f9868976-cf57-446f-8ba8-3c8ff6ae1cae@linux.dev> (raw)
In-Reply-To: <20261126111649.208476-2-arighi@nvidia.com>

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 <cuitao@kylinos.cn>

Thanks,
Tao

> Fixes: be8be8acb268 ("sched_ext: Block proxy donors before taking control")
> Reported-by: Tao Cui <cuitao@kylinos.cn>
> Closes: https://lore.kernel.org/r/20261112144935.583937-3-cui.tao@linux.dev
> Suggested-by: Peter Zijlstra <peterz@infradead.org>
> Signed-off-by: Andrea Righi <arighi@nvidia.com>
> ---
>  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)",


  parent reply	other threads:[~2026-12-02  3:07 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 11:13 [PATCHSET sched_ext/for-7.5] sched/proxy: Handle retained donors on scheduling class transitions Andrea Righi
2026-11-26 11:13 ` [PATCH 1/4] sched: Let switching_to() decide whether to keep a retained proxy donor Andrea Righi
2026-11-26 16:01   ` Peter Zijlstra
2026-11-28 16:42     ` Andrea Righi
2026-12-02  3:06   ` Tao Cui [this message]
2026-11-26 11:13 ` [PATCH 2/4] sched_ext: Make SCX_OPS_ENQ_BLOCKED a hierarchy-wide setting Andrea Righi
2026-11-27 20:29   ` Tejun Heo
2026-11-28 16:39     ` Andrea Righi
2026-11-28 16:53       ` Andrea Righi
2026-11-29  7:46       ` Tejun Heo
2026-11-29  8:25         ` Andrea Righi
2026-11-26 11:13 ` [PATCH 3/4] sched: Remove sched_proxy_block_task() Andrea Righi
2026-11-26 11:14 ` [PATCH 4/4] selftests/sched_ext: Check that blocked donors are never enqueued unadmitted Andrea Righi
2026-12-02  3:08   ` Tao Cui

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f9868976-cf57-446f-8ba8-3c8ff6ae1cae@linux.dev \
    --to=cui.tao@linux.dev \
    --cc=arighi@nvidia.com \
    --cc=bsegall@google.com \
    --cc=changwoo@igalia.com \
    --cc=cuitao@kylinos.cn \
    --cc=dietmar.eggemann@arm.com \
    --cc=jstultz@google.com \
    --cc=juri.lelli@redhat.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=sched-ext@lists.linux.dev \
    --cc=tj@kernel.org \
    --cc=vincent.guittot@linaro.org \
    --cc=void@manifault.com \
    --cc=vschneid@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®