* [PATCH 1/2] sched_ext: Start ext_server from the tick while a higher class holds the CPU
2026-11-25 10:34 [PATCHSET sched_ext/for-7.5] sched_ext: Start ext_server from the tick Tejun Heo
@ 2026-11-25 10:34 ` Tejun Heo
2026-11-25 10:34 ` [PATCH 2/2] sched_ext: Start ext_server from the remote tick on nohz_full CPUs Tejun Heo
2026-11-25 13:57 ` [PATCHSET sched_ext/for-7.5] sched_ext: Start ext_server from the tick Peter Zijlstra
2 siblings, 0 replies; 4+ messages in thread
From: Tejun Heo @ 2026-11-25 10:34 UTC (permalink / raw)
To: sched-ext
Cc: David Vernet, Andrea Righi, Changwoo Min, Emil Tsalapatis,
David Dai, Ingo Molnar, Peter Zijlstra, Juri Lelli,
Vincent Guittot, Dietmar Eggemann, Steven Rostedt, Ben Segall,
Mel Gorman, Valentin Schneider, K Prateek Nayak, Joel Fernandes,
Hang Zhengyang, linux-kernel, Tejun Heo
ext_server starts when the first task is enqueued on a CPU's rq and stops
when its pick finds no task. As the BPF scheduler can run a task on any CPU
whichever rq it is queued on, tasks can wait for a CPU whose ext_server has
stopped with no enqueue to start it again. A higher class task which keeps
the CPU then starves them until the watchdog fires.
Fix it by also starting ext_server from the tick while a higher class task
runs on the CPU. If the local DSQ has a task, start it right away.
Otherwise, if any task is runnable on SCX, start it at most once a server
period, as only ops.dispatch() can tell whether the CPU has a task to run.
Whether any task is runnable is a per-node count of rqs which have tasks and
a global count of such nodes, which the tick reads. A per-node timer
uncounts an rq after it has had no tasks for a second, so that an rq which
keeps running out of tasks doesn't write the counts each time.
Signed-off-by: Tejun Heo <tj@kernel.org>
---
kernel/sched/core.c | 4 +-
kernel/sched/ext/ext.c | 162 ++++++++++++++++++++++++++++++++++++++-
kernel/sched/ext/ext.h | 2 +
kernel/sched/ext/types.h | 1 +
kernel/sched/sched.h | 2 +
5 files changed, 167 insertions(+), 4 deletions(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 2f46922982f3..1ac779bb34f7 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -8807,9 +8807,7 @@ int sched_cpu_dying(unsigned int cpu)
dump_rq_tasks(rq, KERN_WARNING);
}
dl_server_stop(&rq->fair_server);
-#ifdef CONFIG_SCHED_CLASS_EXT
- dl_server_stop(&rq->ext_server);
-#endif
+ scx_rq_dying(rq);
rq_unlock_irqrestore(rq, &rf);
calc_load_migrate(rq);
diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index e3d1bb3fadd8..e081f853a155 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -213,6 +213,21 @@ static unsigned long scx_watchdog_timestamp = INITIAL_JIFFIES;
static struct delayed_work scx_watchdog_work;
+/* per-node state */
+struct scx_pnode {
+ s32 node;
+
+ /* see ext_server_timerfn() */
+ raw_spinlock_t lock; /* protects the following */
+ u32 nr_populated_rqs; /* %SCX_RQ_POPULATED rqs on node */
+ struct timer_list ext_server_timer; /* armed while the above isn't 0 */
+} ____cacheline_aligned_in_smp;
+
+static struct scx_pnode **scx_pnode;
+
+/* nodes with %SCX_RQ_POPULATED rqs, see ext_server_timerfn() */
+static atomic_t scx_nr_populated_nodes;
+
/*
* For %SCX_KICK_WAIT: Each CPU has a pointer to an array of kick_sync sequence
* numbers. The arrays are allocated with kvzalloc() as size can exceed percpu
@@ -2441,6 +2456,20 @@ static void enqueue_task_scx(struct rq *rq, struct task_struct *p, int core_enq_
if (rq->scx.nr_running == 1)
dl_server_start(&rq->ext_server);
+ /* see ext_server_timerfn() */
+ if (!(rq->scx.flags & SCX_RQ_POPULATED)) {
+ struct scx_pnode *pnode = scx_pnode[cpu_to_node(cpu_of(rq))];
+
+ rq->scx.flags |= SCX_RQ_POPULATED;
+
+ guard(raw_spinlock)(&pnode->lock);
+ if (!pnode->nr_populated_rqs++) {
+ atomic_inc(&scx_nr_populated_nodes);
+ timer_reduce(&pnode->ext_server_timer,
+ round_jiffies_up(jiffies + SCX_EXT_SERVER_INTV));
+ }
+ }
+
scx_do_enqueue_task(rq, p, enq_flags, sticky_cpu);
out:
rq->scx.flags &= ~SCX_RQ_IN_WAKEUP;
@@ -2581,6 +2610,8 @@ static bool dequeue_task_scx(struct rq *rq, struct task_struct *p, int core_deq_
p->scx.flags &= ~SCX_TASK_QUEUED;
rq->scx.nr_running--;
+ if (!rq->scx.nr_running)
+ rq->scx.empty_at = jiffies;
sub_nr_running(rq, 1);
scx_dispatch_dequeue(rq, p);
@@ -4247,6 +4278,31 @@ void scx_rq_deactivate(struct rq *rq)
handle_hotplug(rq, false);
}
+/* see ext_server_timerfn() */
+static void clear_rq_populated(struct rq *rq)
+{
+ struct scx_pnode *pnode = scx_pnode[cpu_to_node(cpu_of(rq))];
+
+ lockdep_assert_rq_held(rq);
+
+ rq->scx.flags &= ~SCX_RQ_POPULATED;
+
+ guard(raw_spinlock)(&pnode->lock);
+ if (!--pnode->nr_populated_rqs)
+ atomic_dec(&scx_nr_populated_nodes);
+}
+
+/* @rq's CPU is going down and has no tasks */
+void scx_rq_dying(struct rq *rq)
+{
+ lockdep_assert_rq_held(rq);
+
+ dl_server_stop(&rq->ext_server);
+
+ if (rq->scx.flags & SCX_RQ_POPULATED)
+ clear_rq_populated(rq);
+}
+
static void rq_online_scx(struct rq *rq)
{
rq->scx.flags |= SCX_RQ_ONLINE;
@@ -4339,6 +4395,96 @@ static void scx_watchdog_workfn(struct work_struct *work)
queue_delayed_work(system_dfl_wq, to_delayed_work(work), intv);
}
+/* whether @rq's %SCX_RQ_POPULATED is due for clearing */
+static bool rq_populated_expired(struct rq *rq)
+{
+ return (rq->scx.flags & SCX_RQ_POPULATED) && !rq->scx.nr_running &&
+ time_after(jiffies, rq->scx.empty_at + SCX_EXT_SERVER_INTV);
+}
+
+/**
+ * ext_server_timerfn - Unmark a node's rqs which have no tasks
+ * @timer: ext_server_timer of the node's scx_pnode
+ *
+ * ext_server_tick() needs to know whether any task is runnable on SCX. An rq is
+ * marked %SCX_RQ_POPULATED and counted in its node's ->nr_populated_rqs when a
+ * task is enqueued on it. The mark is cleared here, after the rq has had no
+ * tasks for %SCX_EXT_SERVER_INTV, so that an rq which keeps running out of
+ * tasks doesn't write the count each time.
+ *
+ * scx_nr_populated_nodes is the number of nodes which have populated rqs. A
+ * node is counted when its first rq becomes populated and uncounted when its
+ * last one is unmarked. ext_server_tick() tests it without a lock and can see a
+ * newly populated node a tick late.
+ *
+ * scx_rq_dying() clears the mark of an offline CPU, which isn't in
+ * cpumask_of_node().
+ */
+static void ext_server_timerfn(struct timer_list *timer)
+{
+ struct scx_pnode *pnode = timer_container_of(pnode, timer, ext_server_timer);
+ s32 cpu;
+
+ for_each_cpu(cpu, cpumask_of_node(pnode->node)) {
+ struct rq *rq = cpu_rq(cpu);
+
+ /* most rqs fail the test, try before taking the rq lock */
+ if (!data_race(rq_populated_expired(rq)))
+ continue;
+
+ guard(rq_lock_irqsave)(rq);
+
+ if (rq_populated_expired(rq))
+ clear_rq_populated(rq);
+ }
+
+ guard(raw_spinlock_irqsave)(&pnode->lock);
+ if (pnode->nr_populated_rqs)
+ mod_timer(timer, round_jiffies_up(jiffies + SCX_EXT_SERVER_INTV));
+}
+
+/**
+ * ext_server_tick - Start ext_server if tasks may be waiting for the CPU
+ * @rq: rq of the CPU the tick is for
+ *
+ * ext_server starts when the first task is enqueued on @rq and stops when its
+ * pick finds no task. While a higher class task runs on the CPU, ext_server can
+ * be stopped with tasks waiting for the CPU in two cases. Start it for both.
+ *
+ * - A task reached the local DSQ after the pick, without being enqueued on @rq.
+ * Start ext_server right away.
+ *
+ * - The BPF scheduler can run a task on any CPU the task is allowed on,
+ * whichever rq it is queued on. Only ops.dispatch() can tell whether this CPU
+ * has a task to run. If any rq is populated, start ext_server so that the CPU
+ * dispatches, at most once a server period.
+ */
+static void ext_server_tick(struct rq *rq)
+{
+ struct sched_dl_entity *dl_se = &rq->ext_server;
+
+ if (dl_server_active(dl_se) ||
+ !sched_class_above(rq->donor->sched_class, &ext_sched_class))
+ return;
+
+ if (rq->scx.local_dsq.nr) {
+ dl_server_start(dl_se);
+ return;
+ }
+
+ /*
+ * With no task known to be waiting for this CPU, wait for the deadline
+ * of the last period. A start before it can queue ext_server right
+ * away, and the higher class task would then be preempted for an empty
+ * pick on every tick.
+ */
+ if (dl_time_before(rq_clock(rq), dl_se->deadline))
+ return;
+
+ if (atomic_read(&scx_nr_populated_nodes))
+ dl_server_start(dl_se);
+}
+
void scx_tick(struct rq *rq)
{
struct scx_sched *root;
@@ -4362,6 +4508,7 @@ void scx_tick(struct rq *rq)
}
update_other_load_avgs(rq);
+ ext_server_tick(rq);
}
static void task_tick_scx(struct rq *rq, struct task_struct *donor, int queued)
@@ -9547,7 +9694,7 @@ static struct notifier_block scx_pm_notifier = {
void __init init_sched_ext_class(void)
{
- s32 cpu, v;
+ s32 cpu, node, v;
/*
* The following is to prevent the compiler from optimizing out the enum
@@ -9590,6 +9737,19 @@ void __init init_sched_ext_class(void)
register_sysrq_key('D', &sysrq_sched_ext_dump_op);
INIT_DELAYED_WORK(&scx_watchdog_work, scx_watchdog_workfn);
+ scx_pnode = kzalloc_objs(*scx_pnode, nr_node_ids);
+ BUG_ON(!scx_pnode);
+ for_each_node(node) {
+ struct scx_pnode *pnode;
+
+ pnode = kzalloc_node(sizeof(*pnode), GFP_KERNEL, node);
+ BUG_ON(!pnode);
+ pnode->node = node;
+ raw_spin_lock_init(&pnode->lock);
+ timer_setup(&pnode->ext_server_timer, ext_server_timerfn, 0);
+ scx_pnode[node] = pnode;
+ }
+
#ifdef CONFIG_EXT_SUB_SCHED
BUG_ON(rhashtable_init(&scx_sched_hash, &scx_sched_hash_params));
#endif /* CONFIG_EXT_SUB_SCHED */
diff --git a/kernel/sched/ext/ext.h b/kernel/sched/ext/ext.h
index d32bc01071b0..366cc1d7089b 100644
--- a/kernel/sched/ext/ext.h
+++ b/kernel/sched/ext/ext.h
@@ -17,6 +17,7 @@ void scx_cancel_fork(struct task_struct *p);
bool scx_can_stop_tick(struct rq *rq);
void scx_rq_activate(struct rq *rq);
void scx_rq_deactivate(struct rq *rq);
+void scx_rq_dying(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);
@@ -70,6 +71,7 @@ static inline u32 scx_cpuperf_target(s32 cpu) { return 0; }
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 void scx_rq_dying(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; }
diff --git a/kernel/sched/ext/types.h b/kernel/sched/ext/types.h
index 38514d6bf149..17d728c9cfbd 100644
--- a/kernel/sched/ext/types.h
+++ b/kernel/sched/ext/types.h
@@ -18,6 +18,7 @@ enum scx_consts {
SCX_DSP_DFL_MAX_BATCH = 32,
SCX_DSP_MAX_LOOPS = 32,
SCX_WATCHDOG_MAX_TIMEOUT = 30 * HZ,
+ SCX_EXT_SERVER_INTV = HZ, /* see ext_server_timerfn() */
/* rescue knob defaults and limits, see scx_rescue_timerfn() */
SCX_RESCUE_DFL_BW_PPT = 20, /* parts per thousand, 2% */
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 9e7993256c81..57cbfe36e9af 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -804,6 +804,7 @@ enum scx_rq_flags {
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_POPULATED = 1 << 13, /* see ext_server_timerfn() */
SCX_RQ_IN_WAKEUP = 1 << 16,
SCX_RQ_IN_DISPATCH = 1 << 17,
@@ -838,6 +839,7 @@ struct scx_rq {
bool cpu_released;
u32 flags;
u32 nr_immed; /* ENQ_IMMED tasks on local_dsq */
+ unsigned long empty_at; /* when nr_running last became 0 */
#ifdef CONFIG_SCHED_CORE
u32 lock_drop_seq; /* nr dispatch lock releases */
#endif
--
2.56.1
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH 2/2] sched_ext: Start ext_server from the remote tick on nohz_full CPUs
2026-11-25 10:34 [PATCHSET sched_ext/for-7.5] sched_ext: Start ext_server from the tick Tejun Heo
2026-11-25 10:34 ` [PATCH 1/2] sched_ext: Start ext_server from the tick while a higher class holds the CPU Tejun Heo
@ 2026-11-25 10:34 ` Tejun Heo
2026-11-25 13:57 ` [PATCHSET sched_ext/for-7.5] sched_ext: Start ext_server from the tick Peter Zijlstra
2 siblings, 0 replies; 4+ messages in thread
From: Tejun Heo @ 2026-11-25 10:34 UTC (permalink / raw)
To: sched-ext
Cc: David Vernet, Andrea Righi, Changwoo Min, Emil Tsalapatis,
David Dai, Ingo Molnar, Peter Zijlstra, Juri Lelli,
Vincent Guittot, Dietmar Eggemann, Steven Rostedt, Ben Segall,
Mel Gorman, Valentin Schneider, K Prateek Nayak, Joel Fernandes,
Hang Zhengyang, linux-kernel, Tejun Heo
On a nohz_full CPU, the tick can stop while SCHED_FIFO tasks or a single
SCHED_RR task run, and ext_server is no longer started from it.
sched_tick_remote() does the tick's work once a second for a CPU whose tick
is stopped. Start ext_server from there too.
A nohz_full CPU running an RT task is now made to dispatch about once every
two seconds while any task is runnable on SCX. Setting the CPU's ext_server
runtime to 0 turns that off.
Signed-off-by: Tejun Heo <tj@kernel.org>
---
kernel/sched/core.c | 1 +
kernel/sched/ext/ext.c | 12 ++++++++++++
kernel/sched/ext/ext.h | 2 ++
3 files changed, 15 insertions(+)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 1ac779bb34f7..7a8ae8e8d7c6 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -5925,6 +5925,7 @@ static void sched_tick_remote(struct work_struct *work)
WARN_ON_ONCE(delta > (u64)NSEC_PER_SEC * 30);
}
curr->sched_class->task_tick(rq, curr, 0);
+ scx_tick_remote(rq);
calc_load_nohz_remote(rq);
}
diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index e081f853a155..c96d94a7767a 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -4511,6 +4511,18 @@ void scx_tick(struct rq *rq)
ext_server_tick(rq);
}
+#ifdef CONFIG_NO_HZ_FULL
+/*
+ * Called once a second while @rq's tick is stopped. An RT task can keep the
+ * tick stopped with tasks runnable on SCX, see ext_server_tick().
+ */
+void scx_tick_remote(struct rq *rq)
+{
+ if (scx_enabled())
+ ext_server_tick(rq);
+}
+#endif
+
static void task_tick_scx(struct rq *rq, struct task_struct *donor, int queued)
{
struct scx_sched *sch = scx_task_sched(donor);
diff --git a/kernel/sched/ext/ext.h b/kernel/sched/ext/ext.h
index 366cc1d7089b..6ce6d50ac393 100644
--- a/kernel/sched/ext/ext.h
+++ b/kernel/sched/ext/ext.h
@@ -9,6 +9,7 @@
#ifdef CONFIG_SCHED_CLASS_EXT
void scx_tick(struct rq *rq);
+void scx_tick_remote(struct rq *rq);
void init_scx_entity(struct sched_ext_entity *scx);
void scx_pre_fork(struct task_struct *p);
int scx_fork(struct task_struct *p, struct kernel_clone_args *kargs);
@@ -63,6 +64,7 @@ bool scx_prio_less(const struct task_struct *a, const struct task_struct *b,
#else /* CONFIG_SCHED_CLASS_EXT */
static inline void scx_tick(struct rq *rq) {}
+static inline void scx_tick_remote(struct rq *rq) {}
static inline void scx_pre_fork(struct task_struct *p) {}
static inline int scx_fork(struct task_struct *p, struct kernel_clone_args *kargs) { return 0; }
static inline void scx_post_fork(struct task_struct *p) {}
--
2.56.1
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCHSET sched_ext/for-7.5] sched_ext: Start ext_server from the tick
2026-11-25 10:34 [PATCHSET sched_ext/for-7.5] sched_ext: Start ext_server from the tick Tejun Heo
2026-11-25 10:34 ` [PATCH 1/2] sched_ext: Start ext_server from the tick while a higher class holds the CPU Tejun Heo
2026-11-25 10:34 ` [PATCH 2/2] sched_ext: Start ext_server from the remote tick on nohz_full CPUs Tejun Heo
@ 2026-11-25 13:57 ` Peter Zijlstra
2 siblings, 0 replies; 4+ messages in thread
From: Peter Zijlstra @ 2026-11-25 13:57 UTC (permalink / raw)
To: Tejun Heo
Cc: sched-ext, David Vernet, Andrea Righi, Changwoo Min,
Emil Tsalapatis, David Dai, Ingo Molnar, Juri Lelli,
Vincent Guittot, Dietmar Eggemann, Steven Rostedt, Ben Segall,
Mel Gorman, Valentin Schneider, K Prateek Nayak, Joel Fernandes,
Hang Zhengyang, linux-kernel
On Wed, Nov 25, 2026 at 12:34:18AM -1000, Tejun Heo wrote:
> Hello,
>
> ext_server starts when the first task is enqueued on a CPU's rq and stops
> when its pick finds no task. As the BPF scheduler can run a task on any CPU
> whichever rq it is queued on, tasks can wait for a CPU whose ext_server has
> stopped with no enqueue to start it again, and a higher class task which
> keeps the CPU starves them until the watchdog fires. scx_central with a
> SCHED_FIFO task spinning on a CPU shows it.
>
> Peter pointed out the problem when ext_server was being added [1]. Hang's
> recent patch which starts ext_server on every enqueue [2] helps but leaves
> cases like the above.
>
> This patchset also starts ext_server from the tick, while a higher class
> task is running on the CPU:
>
> - If the local DSQ has a task, right away. This covers a task which another
> CPU inserts after the pick came up empty.
>
> - Otherwise, if any task is runnable on SCX, at most once a server period.
> Only ops.dispatch() can tell whether the BPF scheduler has a task for the
> CPU, and the server's pick calls it.
>
> Whether any task is runnable comes from per-node counts of rqs which have
> tasks, which a per-node timer lowers lazily, and a global count of such
> nodes, which is all the tick reads.
>
> This is a departure from how dl servers are usually driven and there may be
> better ways to go about it, but it does seem to solve the problem properly
> without adding much overhead.
This is all pretty awful; at the same time I don't really know if I
have a better solution at hand either :(
Could you perhaps punt the responsibility to the DSQ? Each DSQ has both:
- a count of how many tasks it has
- a sense of what CPUs it targets
Right? So can it make sure nr_tasks CPUs of its destination are running
ext_server?
That might avoid all this extra accounting and timers you're adding just
to keep stuff 'running.
^ permalink raw reply [flat|nested] 4+ messages in thread