All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kuba Piecuch <jpiecuch@google.com>
To: Tejun Heo <tj@kernel.org>, David Vernet <void@manifault.com>,
	Andrea Righi <arighi@nvidia.com>,
	 Changwoo Min <changwoo@igalia.com>
Cc: Kuba Piecuch <jpiecuch@google.com>,
	Peter Zijlstra <peterz@infradead.org>,
	sched-ext@lists.linux.dev,  linux-kernel@vger.kernel.org
Subject: [RFC PATCH 1/2] sched_ext: Reenqueue IMMED tasks when a higher class wins the pick
Date: Wed, 25 Nov 2026 11:50:52 +0000	[thread overview]
Message-ID: <20261125114932.3198836-2-jpiecuch@google.com> (raw)
In-Reply-To: <20261125114932.3198836-1-jpiecuch@google.com>

SCX_ENQ_IMMED relies on rq->next_class to tell whether a higher class task
is about to take the CPU. rq_is_open() refuses IMMED insertions while
next_class is above ext and wakeup_preempt_scx() reenqueues IMMED tasks
when a higher class task raises next_class. However, a higher class task
can take the CPU without going through wakeup_preempt(), leaving IMMED
tasks on the local DSQ until it gives the CPU up:

- sched_setscheduler() or a PI boost promoting a queued task to RT or DL.
  sched_change_end() doesn't call wakeup_preempt() for tasks which aren't
  running, switched_to_rt() only calls resched_curr() and switched_to_dl()
  calls wakeup_preempt_dl() directly.

- DL replenishment, CFS bandwidth unthrottling (in partial mode) and RT
  group unthrottling make tasks runnable without wakeup_preempt().

- When the ext dl_server picks an SCX task, next_class becomes ext even
  though RT tasks are queued. Once the server runs out of budget, RT takes
  over without a wakeup.

- With core scheduling, a forced idle pick resets next_class to idle while
  tasks remain on the local DSQ. An RT task waking up on the CPU then goes
  through wakeup_preempt_idle() instead of wakeup_preempt_scx().

With proxy execution enabled, scx_proxy_reenqueue_retry() already catches
these. __schedule() sets rq->next_class from the picked task right before
calling it, so it reenqueues IMMED tasks whenever a higher class wins the
pick, whether or not that task went through wakeup_preempt(). Without proxy
execution (CONFIG_SCHED_PROXY_EXEC=n or sched_proxy_exec=0), nothing does.

Rather than chasing every path which can make a higher class task
runnable, check once the pick is final, whether or not proxy execution is
enabled. Add scx_settle_pick(), called by __schedule() after proxy
resolution, on the path which every pick reaches. If rq->next_class is
above SCX and the local DSQ has IMMED tasks, schedule a reenqueue. It runs
right after the context switch. rq_is_open() is false by then, so the
IMMED tasks go back to the BPF scheduler. With proxy execution,
rq->next_class is the class of the donor, so a higher class donor running
an SCX lock owner counts too. Core-sched siblings apply a pick made by
another CPU in their own __schedule(), so they're covered as well.

This makes the IMMED check in scx_proxy_reenqueue_retry() redundant, so
remove it. The reenqueue from wakeup_preempt_scx() stays, as it kicks in
earlier for the common case. When no BPF scheduler is loaded, the new hook
is a static branch.

Fixes: 98d709cba319 ("sched_ext: Implement SCX_ENQ_IMMED")
Cc: stable@vger.kernel.org # v7.1+
Signed-off-by: Kuba Piecuch <jpiecuch@google.com>
---
 kernel/sched/core.c    |  1 +
 kernel/sched/ext/ext.c | 53 +++++++++++++++++++++++++++++++++++-------
 kernel/sched/ext/ext.h | 13 +++++++++++
 3 files changed, 58 insertions(+), 9 deletions(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index f7a452123ef5..7eb99b833a4a 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -7340,6 +7340,7 @@ static void __sched notrace __schedule(int sched_mode)
 	clear_tsk_need_resched(prev);
 	clear_preempt_need_resched();
 keep_resched:
+	scx_settle_pick(rq);
 	rq->last_seen_need_resched_ns = 0;
 
 	is_switch = prev != next;
diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index efa1d01d03af..cea7bd9acc8b 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -1264,14 +1264,6 @@ void scx_proxy_reenqueue_retry(struct rq *rq, struct task_struct *next)
 		scx_proxy_update_tick(rq, next);
 #endif
 
-	/*
-	 * A higher priority class may wake up while @rq is transiently idle
-	 * for proxy resolution. wakeup_preempt_scx() isn't invoked in that
-	 * case, so reenqueue IMMED donors parked on the local DSQ here.
-	 */
-	if (rq->scx.nr_immed && sched_class_above(rq->next_class, &ext_sched_class))
-		scx_schedule_reenq_local(rq, 0);
-
 	if (rq->scx.flags & SCX_RQ_PROXY_RETRY) {
 		rq->scx.flags &= ~SCX_RQ_PROXY_RETRY;
 		schedule_deferred_locked(rq);
@@ -2713,18 +2705,61 @@ static void wakeup_preempt_scx(struct rq *rq, struct task_struct *p, int wake_fl
 
 	/*
 	 * Getting preempted by a higher-priority class. Reenqueue IMMED tasks.
-	 * This captures all preemption cases including:
+	 * This captures the preemption cases which go through wakeup_preempt()
+	 * including:
 	 *
 	 * - A SCX task is currently running.
 	 *
 	 * - @rq is waking from idle due to a SCX task waking to it.
 	 *
 	 * - A higher-priority wakes up while SCX dispatch is in progress.
+	 *
+	 * The rest is caught by scx_settle_pick() once the higher-priority task
+	 * gets picked.
 	 */
 	if (rq->scx.nr_immed)
 		scx_schedule_reenq_local(rq, 0);
 }
 
+/**
+ * __scx_settle_pick - Reenqueue IMMED tasks left behind by a pick
+ * @rq: rq of the current CPU, locked
+ *
+ * Called by __schedule() once the pick, including proxy resolution, is final.
+ * If rq->next_class is above SCX, IMMED tasks on @rq's local DSQ can't run.
+ * With proxy execution, rq->next_class is the class of the donor. A higher
+ * class can take the CPU without going through wakeup_preempt_scx(), as
+ * rq->next_class doesn't always reflect the highest class with runnable tasks:
+ *
+ * - A queued task getting promoted to RT or DL by sched_setscheduler() or a PI
+ *   boost only triggers resched_curr() or a class-local preemption check.
+ *
+ * - DL replenishment and CFS or RT bandwidth unthrottling make tasks runnable
+ *   without going through wakeup_preempt().
+ *
+ * - When the ext dl_server picks an SCX task, rq->next_class is set to ext even
+ *   if RT tasks are queued. RT takes over once the server runs out of budget.
+ *
+ * - With core scheduling, a forced idle pick resets rq->next_class to idle
+ *   while tasks are still on the local DSQ. A later wakeup goes through
+ *   wakeup_preempt_idle().
+ *
+ * - While @rq is transiently idle for proxy resolution, a higher class task
+ *   waking up also goes through wakeup_preempt_idle().
+ *
+ * Catch all of these here. The reenqueue is deferred and runs right after the
+ * context switch. rq->next_class is still above SCX by then, so rq_is_open() is
+ * false and the IMMED tasks on the local DSQ go back to the BPF scheduler.
+ */
+void __scx_settle_pick(struct rq *rq)
+{
+	lockdep_assert_rq_held(rq);
+
+	if (rq->scx.nr_immed &&
+	    sched_class_above(rq->next_class, &ext_sched_class))
+		scx_schedule_reenq_local(rq, 0);
+}
+
 void scx_move_local_task_to_local_dsq(struct scx_sched *sch, struct task_struct *p,
 				      u64 enq_flags, struct rq *dst_rq)
 {
diff --git a/kernel/sched/ext/ext.h b/kernel/sched/ext/ext.h
index d32bc01071b0..c66c4c97a2e0 100644
--- a/kernel/sched/ext/ext.h
+++ b/kernel/sched/ext/ext.h
@@ -26,6 +26,7 @@ void scx_proxy_donor_start(struct rq *rq);
 void scx_proxy_reenqueue_retry(struct rq *rq, struct task_struct *next);
 void init_sched_ext_class(void);
 void __scx_update_idle(struct rq *rq, bool idle, bool do_notify);
+void __scx_settle_pick(struct rq *rq);
 
 static inline bool scx_allow_proxy_exec(const struct task_struct *p)
 {
@@ -41,6 +42,17 @@ static inline void scx_update_idle(struct rq *rq, bool idle, bool do_notify)
 		__scx_update_idle(rq, idle, do_notify);
 }
 
+/*
+ * Called by __schedule() once the pick, including proxy resolution, is final.
+ * A pick which lands in a class above SCX can leave %SCX_ENQ_IMMED tasks behind
+ * on the local DSQ. See __scx_settle_pick().
+ */
+static inline void scx_settle_pick(struct rq *rq)
+{
+	if (scx_enabled())
+		__scx_settle_pick(rq);
+}
+
 static inline u32 scx_cpuperf_target(s32 cpu)
 {
 	if (scx_enabled())
@@ -80,6 +92,7 @@ static inline void scx_proxy_reenqueue_retry(struct rq *rq,
 					     struct task_struct *next) {}
 static inline void init_sched_ext_class(void) {}
 static inline void scx_update_idle(struct rq *rq, bool idle, bool do_notify) {}
+static inline void scx_settle_pick(struct rq *rq) {}
 
 #endif	/* CONFIG_SCHED_CLASS_EXT */
 
-- 
2.56.1.839.g22a8290db9-goog


  reply	other threads:[~2026-11-25 11:50 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 11:50 [RFC PATCHSET sched_ext/for-7.4-fixes] sched_ext: Reenqueue IMMED tasks when a higher class wins the pick Kuba Piecuch
2026-11-25 11:50 ` Kuba Piecuch [this message]
2026-11-25 12:16   ` [RFC PATCH 1/2] " sashiko-bot
2026-11-30 11:19     ` Kuba Piecuch
2026-11-27 15:10   ` Tejun Heo
2026-11-30 11:19     ` Kuba Piecuch
2026-11-30 15:11       ` Tejun Heo
2026-11-30 16:26         ` Kuba Piecuch
2026-11-25 11:50 ` [RFC PATCH 2/2] selftests/sched_ext: Add immed_strand test Kuba Piecuch

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=20261125114932.3198836-2-jpiecuch@google.com \
    --to=jpiecuch@google.com \
    --cc=arighi@nvidia.com \
    --cc=changwoo@igalia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=sched-ext@lists.linux.dev \
    --cc=tj@kernel.org \
    --cc=void@manifault.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.