Skip to contentSkip to conversation

sched_ext: Start ext_server from the tick

PATCH In review Tejun Heo 2 participants 4 messages Nov 25

Subsystems (2)
lore
Share & tools
lore

Tejun Heo

author reviewer
#0 lore raw

PATCH 1/1 sched_ext: Start ext_server from the tick

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.

With the patchset, a SCHED_EXT task sharing a CPU with a SCHED_FIFO hog gets the server's share under both scx_simple and scx_central. The second patch does the same from the remote tick for nohz_full CPUs.

Based on sched_ext/for-7.5 (cb31a095aab1).

This patchset contains the following 2 patches.

0001 sched_ext: Start ext_server from the tick while a higher class holds the CPU 0002 sched_ext: Start ext_server from the remote tick on nohz_full CPUs

The patchset is also available in the following git branch:

git://git.kernel.org/pub/scm/linux/kernel/git/tj/sched_ext.git ext-server-tick

diffstat follows. Thanks.

 kernel/sched/core.c      |   5 +-
 kernel/sched/ext/ext.c   | 174 ++++++++++++++++++++++++++++++++++++++++++++++-
 kernel/sched/ext/ext.h   |   4 ++
 kernel/sched/ext/types.h |   1 +
 kernel/sched/sched.h     |   2 +
 5 files changed, 182 insertions(+), 4 deletions(-)

[1] https://lore.kernel.org/r/[email protected]
[2] https://lore.kernel.org/r/[email protected]

--
tejun

author reviewer
#1 lore raw

PATCH 1/2 sched_ext: Start ext_server from the tick while a higher class holds the CPU

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.

---
 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(-)
kernel/sched/core.c +1 -3
index 2f46922982f3..1ac779bb34f7 100644
@@ -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);
kernel/sched/ext/ext.c +161 -1

210 lines not on this page · raw message

kernel/sched/ext/ext.h +2 -0
index d32bc01071b0..366cc1d7089b 100644
@@ -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; }
kernel/sched/ext/types.h +1 -0
index 38514d6bf149..17d728c9cfbd 100644
@@ -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% */
kernel/sched/sched.h +2 -0
index 9e7993256c81..57cbfe36e9af 100644
@@ -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
Signature · 1 line
2.56.1
author reviewer
#2 lore raw

PATCH 2/2 sched_ext: Start ext_server from the remote tick on nohz_full CPUs

In reply to 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.

---
 kernel/sched/core.c    |  1 +
 kernel/sched/ext/ext.c | 12 ++++++++++++
 kernel/sched/ext/ext.h |  2 ++
 3 files changed, 15 insertions(+)
kernel/sched/core.c +1 -0
index 1ac779bb34f7..7a8ae8e8d7c6 100644
@@ -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);
 		}
kernel/sched/ext/ext.c +12 -0
index e081f853a155..c96d94a7767a 100644
@@ -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);
kernel/sched/ext/ext.h +2 -0
index 366cc1d7089b..6ce6d50ac393 100644
@@ -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) {}
Signature · 1 line
2.56.1
maintainer
#3 lore raw
In reply to Tejun Heo
Quoted 23 lines · Tejun Heo

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.

Quoted 7 lines

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.