Skip to contentSkip to conversation

Add a deadline server for sched_ext tasks

PATCH Superseded Joel Fernandes 3 participants 22 messages Jun 2, 2025 – Jun 12, 2025

Subsystems (4)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add a deadline server for sched_ext tasks

sched_ext tasks currently are starved by RT hoggers especially since RT throttling was replaced by deadline servers to boost only CFS tasks. Several users in the community have reported issues with RT stalling sched_ext tasks. Add a sched_ext deadline server as well so that sched_ext tasks are also boosted and do not suffer starvation.

A kselftest is also provided to verify the starvation issues are now fixed.

Link to v1: https://lore.kernel.org/all/[email protected]/

Changes in v2:
- Fixed a hang related to using rq_lock instead of rq_lock_irqsave.
- Added support to remove BW of DL servers when they are switched to/from EXT.

Andrea Righi (1):
selftests/sched_ext: Add test for sched_ext dl_server

Joel Fernandes (9):
sched: Add support to pick functions to take rf
sched: Add a server arg to dl_server_update_idle_time() sched/ext: Add a DL server for sched_ext tasks sched/debug: Fix updating of ppos on server write ops sched/debug: Stop and start server based on if it was active sched/debug: Add support to change sched_ext server params sched/deadline: Clear the defer params sched/deadline: Add support to remove DL server bandwidth sched/ext: Relinquish DL server reservations when not needed

 include/linux/sched.h                         |   2 +-
 kernel/sched/core.c                           |  19 +-
 kernel/sched/deadline.c                       |  85 +++++--
 kernel/sched/debug.c                          |  96 ++++----
 kernel/sched/ext.c                            | 108 ++++++++-
 kernel/sched/fair.c                           |  15 +-
 kernel/sched/idle.c                           |   4 +-
 kernel/sched/rt.c                             |   2 +-
 kernel/sched/sched.h                          |  13 +-
 kernel/sched/stop_task.c                      |   2 +-
 tools/testing/selftests/sched_ext/Makefile    |   1 +
 .../selftests/sched_ext/rt_stall.bpf.c        |  23 ++
 tools/testing/selftests/sched_ext/rt_stall.c  | 213 ++++++++++++++++++
 13 files changed, 502 insertions(+), 81 deletions(-)
 create mode 100644 tools/testing/selftests/sched_ext/rt_stall.bpf.c
 create mode 100644 tools/testing/selftests/sched_ext/rt_stall.c
CC Joel Fernandes [email protected]
CC Ingo Molnar [email protected]
CC Peter Zijlstra [email protected]
CC Juri Lelli [email protected]
CC Vincent Guittot [email protected]
CC Dietmar Eggemann [email protected]
CC Steven Rostedt [email protected]
CC Ben Segall [email protected]
CC Mel Gorman [email protected]
CC Valentin Schneider [email protected]
CC Tejun Heo [email protected]
CC David Vernet [email protected]
CC Andrea Righi [email protected]
CC Changwoo Min [email protected]
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/10 sched: Add support to pick functions to take rf

Some pick functions like the internal pick_next_task_fair() already take rf but some others dont. We need this for scx's server pick function. Prepare for this by having pick functions accept it.

---
 include/linux/sched.h    |  2 +-
 kernel/sched/core.c      | 16 ++++++++--------
 kernel/sched/deadline.c  |  8 ++++----
 kernel/sched/ext.c       |  2 +-
 kernel/sched/fair.c      | 13 ++++++++-----
 kernel/sched/idle.c      |  2 +-
 kernel/sched/rt.c        |  2 +-
 kernel/sched/sched.h     |  7 ++++---
 kernel/sched/stop_task.c |  2 +-
 9 files changed, 29 insertions(+), 25 deletions(-)
include/linux/sched.h +1 -1
index 45e5953b8f32..1000d2aa8482 100644
@@ -630,7 +630,7 @@ struct sched_rt_entity {
 } __randomize_layout;
 
 typedef bool (*dl_server_has_tasks_f)(struct sched_dl_entity *);
-typedef struct task_struct *(*dl_server_pick_f)(struct sched_dl_entity *);
+typedef struct task_struct *(*dl_server_pick_f)(struct sched_dl_entity *, void *);
 
 struct sched_dl_entity {
 	struct rb_node			rb_node;
kernel/sched/core.c +8 -8
index 62b3416f5e43..19b393b0b096 100644
@@ -6045,7 +6045,7 @@ __pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
 
 		/* Assume the next prioritized class is idle_sched_class */
 		if (!p) {
-			p = pick_task_idle(rq);
+			p = pick_task_idle(rq, rf);
 			put_prev_set_next_task(rq, prev, p);
 		}
 
@@ -6057,11 +6057,11 @@ __pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
 
 	for_each_active_class(class) {
 		if (class->pick_next_task) {
-			p = class->pick_next_task(rq, prev);
+			p = class->pick_next_task(rq, prev, rf);
 			if (p)
 				return p;
 		} else {
-			p = class->pick_task(rq);
+			p = class->pick_task(rq, rf);
 			if (p) {
 				put_prev_set_next_task(rq, prev, p);
 				return p;
@@ -6091,7 +6091,7 @@ static inline bool cookie_match(struct task_struct *a, struct task_struct *b)
 	return a->core_cookie == b->core_cookie;
 }
 
-static inline struct task_struct *pick_task(struct rq *rq)
+static inline struct task_struct *pick_task(struct rq *rq, struct rq_flags *rf)
 {
 	const struct sched_class *class;
 	struct task_struct *p;
@@ -6099,7 +6099,7 @@ static inline struct task_struct *pick_task(struct rq *rq)
 	rq->dl_server = NULL;
 
 	for_each_active_class(class) {
-		p = class->pick_task(rq);
+		p = class->pick_task(rq, rf);
 		if (p)
 			return p;
 	}
@@ -6199,7 +6199,7 @@ pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
 	 * and there are no cookied tasks running on siblings.
 	 */
 	if (!need_sync) {
-		next = pick_task(rq);
+		next = pick_task(rq, rf);
 		if (!next->core_cookie) {
 			rq->core_pick = NULL;
 			rq->core_dl_server = NULL;
@@ -6230,7 +6230,7 @@ pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
 		if (i != cpu && (rq_i != rq->core || !core_clock_updated))
 			update_rq_clock(rq_i);
 
-		rq_i->core_pick = p = pick_task(rq_i);
+		rq_i->core_pick = p = pick_task(rq_i, rf);
 		rq_i->core_dl_server = rq_i->dl_server;
 
 		if (!max || prio_less(max, p, fi_before))
@@ -6252,7 +6252,7 @@ pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
 			if (cookie)
 				p = sched_core_find(rq_i, cookie);
 			if (!p)
-				p = idle_sched_class.pick_task(rq_i);
+				p = idle_sched_class.pick_task(rq_i, rf);
 		}
 
 		rq_i->core_pick = p;
kernel/sched/deadline.c +4 -4
index ad45a8fea245..fce08e232926 100644
@@ -2419,7 +2419,7 @@ static struct sched_dl_entity *pick_next_dl_entity(struct dl_rq *dl_rq)
  * __pick_next_task_dl - Helper to pick the next -deadline task to run.
  * @rq: The runqueue to pick the next task from.
  */
-static struct task_struct *__pick_task_dl(struct rq *rq)
+static struct task_struct *__pick_task_dl(struct rq *rq, struct rq_flags *rf)
 {
 	struct sched_dl_entity *dl_se;
 	struct dl_rq *dl_rq = &rq->dl;
@@ -2433,7 +2433,7 @@ static struct task_struct *__pick_task_dl(struct rq *rq)
 	WARN_ON_ONCE(!dl_se);
 
 	if (dl_server(dl_se)) {
-		p = dl_se->server_pick_task(dl_se);
+		p = dl_se->server_pick_task(dl_se, rf);
 		if (!p) {
 			if (dl_server_active(dl_se)) {
 				dl_se->dl_yielded = 1;
@@ -2449,9 +2449,9 @@ static struct task_struct *__pick_task_dl(struct rq *rq)
 	return p;
 }
 
-static struct task_struct *pick_task_dl(struct rq *rq)
+static struct task_struct *pick_task_dl(struct rq *rq, struct rq_flags *rf)
 {
-	return __pick_task_dl(rq);
+	return __pick_task_dl(rq, rf);
 }
 
 static void put_prev_task_dl(struct rq *rq, struct task_struct *p, struct task_struct *next)
kernel/sched/ext.c +1 -1
index f5133249fd4d..d765379cd94c 100644
@@ -3281,7 +3281,7 @@ static struct task_struct *first_local_task(struct rq *rq)
 					struct task_struct, scx.dsq_list.node);
 }
 
-static struct task_struct *pick_task_scx(struct rq *rq)
+static struct task_struct *pick_task_scx(struct rq *rq, struct rq_flags *rf)
 {
 	struct task_struct *prev = rq->curr;
 	struct task_struct *p;
kernel/sched/fair.c +8 -5
index 125912c0e9dd..2b7958d2fb06 100644
@@ -8842,7 +8842,7 @@ static void check_preempt_wakeup_fair(struct rq *rq, struct task_struct *p, int
 	resched_curr_lazy(rq);
 }
 
-static struct task_struct *pick_task_fair(struct rq *rq)
+static struct task_struct *pick_task_fair(struct rq *rq, struct rq_flags *rf)
 {
 	struct sched_entity *se;
 	struct cfs_rq *cfs_rq;
@@ -8880,7 +8880,7 @@ pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf
 	int new_tasks;
 
 again:
-	p = pick_task_fair(rq);
+	p = pick_task_fair(rq, rf);
 	if (!p)
 		goto idle;
 	se = &p->se;
@@ -8959,7 +8959,8 @@ pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf
 	return NULL;
 }
 
-static struct task_struct *__pick_next_task_fair(struct rq *rq, struct task_struct *prev)
+static struct task_struct *__pick_next_task_fair(struct rq *rq, struct task_struct *prev,
+												 struct rq_flags *rf)
 {
 	return pick_next_task_fair(rq, prev, NULL);
 }
@@ -8969,9 +8970,11 @@ static bool fair_server_has_tasks(struct sched_dl_entity *dl_se)
 	return !!dl_se->rq->cfs.nr_queued;
 }
 
-static struct task_struct *fair_server_pick_task(struct sched_dl_entity *dl_se)
+static struct task_struct *fair_server_pick_task(struct sched_dl_entity *dl_se, void *flags)
 {
-	return pick_task_fair(dl_se->rq);
+	struct rq_flags *rf = flags;
+
+	return pick_task_fair(dl_se->rq, rf);
 }
 
 void fair_server_init(struct rq *rq)
kernel/sched/idle.c +1 -1
index 2c85c86b455f..01e9612deefe 100644
@@ -463,7 +463,7 @@ static void set_next_task_idle(struct rq *rq, struct task_struct *next, bool fir
 	next->se.exec_start = rq_clock_task(rq);
 }
 
-struct task_struct *pick_task_idle(struct rq *rq)
+struct task_struct *pick_task_idle(struct rq *rq, struct rq_flags *rf)
 {
 	scx_update_idle(rq, true, false);
 	return rq->idle;
kernel/sched/rt.c +1 -1
index e40422c37033..6e62fe531067 100644
@@ -1744,7 +1744,7 @@ static struct task_struct *_pick_next_task_rt(struct rq *rq)
 	return rt_task_of(rt_se);
 }
 
-static struct task_struct *pick_task_rt(struct rq *rq)
+static struct task_struct *pick_task_rt(struct rq *rq, struct rq_flags *rf)
 {
 	struct task_struct *p;
 
kernel/sched/sched.h +4 -3
index c5a6a503eb6d..b4b9c98f0c6d 100644
@@ -2401,7 +2401,7 @@ struct sched_class {
 	void (*wakeup_preempt)(struct rq *rq, struct task_struct *p, int flags);
 
 	int (*balance)(struct rq *rq, struct task_struct *prev, struct rq_flags *rf);
-	struct task_struct *(*pick_task)(struct rq *rq);
+	struct task_struct *(*pick_task)(struct rq *rq, struct rq_flags *rf);
 	/*
 	 * Optional! When implemented pick_next_task() should be equivalent to:
 	 *
@@ -2411,7 +2411,8 @@ struct sched_class {
 	 *       set_next_task_first(next);
 	 *   }
 	 */
-	struct task_struct *(*pick_next_task)(struct rq *rq, struct task_struct *prev);
+	struct task_struct *(*pick_next_task)(struct rq *rq, struct task_struct *prev,
+										  struct rq_flags *rf);
 
 	void (*put_prev_task)(struct rq *rq, struct task_struct *p, struct task_struct *next);
 	void (*set_next_task)(struct rq *rq, struct task_struct *p, bool first);
@@ -2574,7 +2575,7 @@ static inline bool sched_fair_runnable(struct rq *rq)
 }
 
 extern struct task_struct *pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf);
-extern struct task_struct *pick_task_idle(struct rq *rq);
+extern struct task_struct *pick_task_idle(struct rq *rq, struct rq_flags *rf);
 
 #define SCA_CHECK		0x01
 #define SCA_MIGRATE_DISABLE	0x02
kernel/sched/stop_task.c +1 -1
index 058dd42e3d9b..1c70123cb6a4 100644
@@ -33,7 +33,7 @@ static void set_next_task_stop(struct rq *rq, struct task_struct *stop, bool fir
 	stop->se.exec_start = rq_clock_task(rq);
 }
 
-static struct task_struct *pick_task_stop(struct rq *rq)
+static struct task_struct *pick_task_stop(struct rq *rq, struct rq_flags *rf)
 {
 	if (!sched_stop_runnable(rq))
 		return NULL;
Signature · 1 line
2.43.0
reviewer
#2 lore raw

Re: [PATCH v2 01/10] sched: Add support to pick functions to take rf

Hello,

Quoted 3 lines · Joel Fernandes

Some pick functions like the internal pick_next_task_fair() already take rf but some others dont. We need this for scx's server pick function. Prepare for this by having pick functions accept it.

Hmm.. after the whole patch series is applied, pick_task_scx() still doesn't seem to use @rf. What am I missing?

Thanks.

Signature · 1 line
tejun
#3 lore raw

Re: [PATCH v2 01/10] sched: Add support to pick functions to take rf

Quoted 9 lines · Tejun Heo

Hello,

Quoted 3 lines · Joel Fernandes

Some pick functions like the internal pick_next_task_fair() already take rf but some others dont. We need this for scx's server pick function. Prepare for this by having pick functions accept it.

Hmm.. after the whole patch series is applied, pick_task_scx() still doesn't seem to use @rf. What am I missing?

I need it for scx *server*'s pick_task, i.e. ext_server_pick_task().

Sure we're not using it in pick_task_scx(), but I added it there for consistency since pick_task_dl() needs it, due to ext_server_pick_task() needing it.

Thanks.

reviewer
#4 lore raw

Re: [PATCH v2 01/10] sched: Add support to pick functions to take rf

Quoted 8 lines · Joel Fernandes
Quoted 7 lines · Tejun Heo

Hello,

Quoted 3 lines · Joel Fernandes

Some pick functions like the internal pick_next_task_fair() already take rf but some others dont. We need this for scx's server pick function. Prepare for this by having pick functions accept it.

Quoted 6 lines
Quoted 2 lines

Hmm.. after the whole patch series is applied, pick_task_scx() still doesn't seem to use @rf. What am I missing?

I need it for scx *server*'s pick_task, i.e. ext_server_pick_task().

Sure we're not using it in pick_task_scx(), but I added it there for consistency since pick_task_dl() needs it, due to ext_server_pick_task() needing it.

Ah, yeah, I just missed the usage on the server side. Thanks for the explanation.

Signature · 1 line
tejun
#5 lore raw

PATCH v2 2/10 sched: Add a server arg to dl_server_update_idle_time()

In reply to Joel Fernandes

Since we are adding more servers, make dl_server_update_idle_time() accept a server argument than a specific server.

---
 kernel/sched/deadline.c | 17 +++++++++--------
 kernel/sched/fair.c     |  2 +-
 kernel/sched/idle.c     |  2 +-
 kernel/sched/sched.h    |  3 ++-
 4 files changed, 13 insertions(+), 11 deletions(-)
kernel/sched/deadline.c +9 -8
index fce08e232926..3181384881b8 100644
@@ -1609,28 +1609,29 @@ static void update_curr_dl_se(struct rq *rq, struct sched_dl_entity *dl_se, s64
  * as time available for the fair server, avoiding a penalty for the
  * rt scheduler that did not consumed that time.
  */
-void dl_server_update_idle_time(struct rq *rq, struct task_struct *p)
+void dl_server_update_idle_time(struct rq *rq, struct task_struct *p,
+			       struct sched_dl_entity *rq_dl_server)
 {
 	s64 delta_exec, scaled_delta_exec;
 
-	if (!rq->fair_server.dl_defer)
+	if (!rq_dl_server->dl_defer)
 		return;
 
 	/* no need to discount more */
-	if (rq->fair_server.runtime < 0)
+	if (rq_dl_server->runtime < 0)
 		return;
 
 	delta_exec = rq_clock_task(rq) - p->se.exec_start;
 	if (delta_exec < 0)
 		return;
 
-	scaled_delta_exec = dl_scaled_delta_exec(rq, &rq->fair_server, delta_exec);
+	scaled_delta_exec = dl_scaled_delta_exec(rq, rq_dl_server, delta_exec);
 
-	rq->fair_server.runtime -= scaled_delta_exec;
+	rq_dl_server->runtime -= scaled_delta_exec;
 
-	if (rq->fair_server.runtime < 0) {
-		rq->fair_server.dl_defer_running = 0;
-		rq->fair_server.runtime = 0;
+	if (rq_dl_server->runtime < 0) {
+		rq_dl_server->dl_defer_running = 0;
+		rq_dl_server->runtime = 0;
 	}
 
 	p->se.exec_start = rq_clock_task(rq);
kernel/sched/fair.c +1 -1
index 2b7958d2fb06..6fd4100fd5db 100644
@@ -7016,7 +7016,7 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
 	if (!rq_h_nr_queued && rq->cfs.h_nr_queued) {
 		/* Account for idle runtime */
 		if (!rq->nr_running)
-			dl_server_update_idle_time(rq, rq->curr);
+			dl_server_update_idle_time(rq, rq->curr, &rq->fair_server);
 		dl_server_start(&rq->fair_server);
 	}
 
kernel/sched/idle.c +1 -1
index 01e9612deefe..13a3d20d35e2 100644
@@ -451,7 +451,7 @@ static void wakeup_preempt_idle(struct rq *rq, struct task_struct *p, int flags)
 
 static void put_prev_task_idle(struct rq *rq, struct task_struct *prev, struct task_struct *next)
 {
-	dl_server_update_idle_time(rq, prev);
+	dl_server_update_idle_time(rq, prev, &rq->fair_server);
 	scx_update_idle(rq, false, true);
 }
 
kernel/sched/sched.h +2 -1
index b4b9c98f0c6d..467e39205ebf 100644
@@ -386,7 +386,8 @@ extern void dl_server_init(struct sched_dl_entity *dl_se, struct rq *rq,
 		    dl_server_pick_f pick_task);
 
 extern void dl_server_update_idle_time(struct rq *rq,
-		    struct task_struct *p);
+		    struct task_struct *p,
+		    struct sched_dl_entity *rq_dl_server);
 extern void fair_server_init(struct rq *rq);
 extern void __dl_server_attach_root(struct sched_dl_entity *dl_se, struct rq *rq);
 extern int dl_server_apply_params(struct sched_dl_entity *dl_se,
Signature · 1 line
2.43.0
#6 lore raw

PATCH v2 3/10 sched/ext: Add a DL server for sched_ext tasks

In reply to Joel Fernandes

sched_ext currently suffers starvation due to RT. The same workload when converted to EXT can get zero runtime if RT is 100% running, causing EXT processes to stall. Fix it by adding a DL server for EXT.

Only either the EXT or the CFS DL-servers are active at a time. The bandwidth of them is added and removed accordingly, when sched_ext programs are loaded/unloaded.

A kselftest is also provided later to verify:

./runner -t rt_stall
===== START =====
TEST: rt_stall
DESCRIPTION: Verify that RT tasks cannot stall SCHED_EXT tasks OUTPUT:
TAP version 13
1..1
ok 1 PASS: CFS task got more than 4.00% of runtime

---
 kernel/sched/core.c     |  3 ++
 kernel/sched/deadline.c |  2 +-
 kernel/sched/ext.c      | 62 +++++++++++++++++++++++++++++++++++++++--
 kernel/sched/sched.h    |  2 ++
 4 files changed, 66 insertions(+), 3 deletions(-)
kernel/sched/core.c +3 -0
index 19b393b0b096..17e7cab0ddf5 100644
@@ -8645,6 +8645,9 @@ void __init sched_init(void)
 		hrtick_rq_init(rq);
 		atomic_set(&rq->nr_iowait, 0);
 		fair_server_init(rq);
+#ifdef CONFIG_SCHED_CLASS_EXT
+		ext_server_init(rq);
+#endif
 
 #ifdef CONFIG_SCHED_CORE
 		rq->core = rq;
kernel/sched/deadline.c +1 -1
index 3181384881b8..b703cbf627c1 100644
@@ -1570,7 +1570,7 @@ static void update_curr_dl_se(struct rq *rq, struct sched_dl_entity *dl_se, s64
 	 * The fair server (sole dl_server) does not account for real-time
 	 * workload because it is running fair work.
 	 */
-	if (dl_se == &rq->fair_server)
+	if (dl_se == &rq->fair_server || dl_se == &rq->ext_server)
 		return;
 
 #ifdef CONFIG_RT_GROUP_SCHED
kernel/sched/ext.c +60 -2

98 lines not on this page · raw message

kernel/sched/sched.h +2 -0
index 467e39205ebf..d206421b1146 100644
@@ -389,6 +389,7 @@ extern void dl_server_update_idle_time(struct rq *rq,
 		    struct task_struct *p,
 		    struct sched_dl_entity *rq_dl_server);
 extern void fair_server_init(struct rq *rq);
+extern void ext_server_init(struct rq *rq);
 extern void __dl_server_attach_root(struct sched_dl_entity *dl_se, struct rq *rq);
 extern int dl_server_apply_params(struct sched_dl_entity *dl_se,
 		    u64 runtime, u64 period, bool init);
@@ -1137,6 +1138,7 @@ struct rq {
 #endif
 
 	struct sched_dl_entity	fair_server;
+	struct sched_dl_entity	ext_server;
 
 #ifdef CONFIG_FAIR_GROUP_SCHED
 	/* list of leaf cfs_rq on this CPU: */
Signature · 1 line
2.43.0
reviewer
#7 lore raw

Re: [PATCH v2 03/10] sched/ext: Add a DL server for sched_ext tasks

On Mon, Jun 02, 2025 at 02:00:59PM -0400, Joel Fernandes wrote:
...

kernel/sched/ext.c static void enqueue_task_scx(struct rq *rq, struct task_struct *p, int enq_flags · line 2321

Quoted 12 lines
kernel/sched/ext.c +8 -0
@@ -2308,6 +2311,15 @@ static void enqueue_task_scx(struct rq *rq, struct task_struct *p, int enq_flags
 	if (enq_flags & SCX_ENQ_WAKEUP)
 		touch_core_sched(rq, p);
 
+	if (rq->scx.nr_running == 1) {
+		/* Account for idle runtime */
+		if (!rq->nr_running)
+			dl_server_update_idle_time(rq, rq->curr, &rq->ext_server);
+
+		/* Start dl_server if this is the first task being enqueued */
+		dl_server_start(&rq->ext_server);
+	}

The following patch from Peter isn't upstream yet but SCX probably should do something similar. Otherwise, the start/stop overhead can become pretty expensive:

https://lore.kernel.org/all/[email protected]/

Another thing which is worth considering is that while rq->nr_running based test would work in a lot of cases, it won't work in all cases for SCX as the BPF scheduler may choose to not dispatch to the particular CPU even if a task is currently associated with it.

For example, a soft partitioning scheduler might change partition CPU allocations after enqueue() is complete and a task may end up associated with a CPU that's no longer in its partition and when dispatch() is called from the CPU, the BPF scheduler may not consume that task. This can become a problem for the dl server based forward progress guarantee as that task is enabling the dl server only on the rq that it's currently associated with.

This shouldn't be too common and the proposed patch puts us back in the same state as the original RT bandwidth control, so no need to hold this series for this issue but I think the right solution would be adding an optional SCX operation so that the BPF scheduler can decide which CPUs should be running the dl server.

Thanks.

Signature · 1 line
tejun
#8 lore raw

Re: [PATCH v2 03/10] sched/ext: Add a DL server for sched_ext tasks

Quoted 18 lines · Tejun Heo

On Mon, Jun 02, 2025 at 02:00:59PM -0400, Joel Fernandes wrote:
...

kernel/sched/ext.c static void enqueue_task_scx(struct rq *rq, struct task_struct *p, int enq_flags · line 2321

Quoted 12 lines
kernel/sched/ext.c +8 -0
@@ -2308,6 +2311,15 @@ static void enqueue_task_scx(struct rq *rq, struct task_struct *p, int enq_flags
 	if (enq_flags & SCX_ENQ_WAKEUP)
 		touch_core_sched(rq, p);
 
+	if (rq->scx.nr_running == 1) {
+		/* Account for idle runtime */
+		if (!rq->nr_running)
+			dl_server_update_idle_time(rq, rq->curr, &rq->ext_server);
+
+		/* Start dl_server if this is the first task being enqueued */
+		dl_server_start(&rq->ext_server);
+	}

The following patch from Peter isn't upstream yet but SCX probably should do something similar. Otherwise, the start/stop overhead can become pretty expensive:

Quoted 1 line

https://lore.kernel.org/all/[email protected]/

Right. If it is Ok with you, we can do that after this patchset can be merged, that way we can use the 'dl_server_idle' addition to 'sched_dl_entity', from that patch as well.

thanks,

- Joel

reviewer
#9 lore raw

Re: [PATCH v2 03/10] sched/ext: Add a DL server for sched_ext tasks

Quoted 5 lines · Joel Fernandes
Quoted 1 line

https://lore.kernel.org/all/[email protected]/

Right. If it is Ok with you, we can do that after this patchset can be merged, that way we can use the 'dl_server_idle' addition to 'sched_dl_entity', from that patch as well.

Oh yeah, any ordering is fine by me. Peter, how should we route these patches?

Thanks.

Signature · 1 line
tejun
#10 lore raw

PATCH v2 4/10 sched/debug: Fix updating of ppos on server write ops

In reply to Joel Fernandes

Updating "ppos" on error conditions does not make much sense. The pattern is to return the error code directly without modifying the position, or modify the position on success and return the number of bytes written.

Since on success, the return value of apply is 0, there is no point in modifying ppos either. Fix it by removing all this and just returning error code or number of bytes written on success.

---
 kernel/sched/debug.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
kernel/sched/debug.c +4 -3
index 557246880a7e..77b5d4bebc59 100644
@@ -350,8 +350,8 @@ static ssize_t sched_fair_server_write(struct file *filp, const char __user *ubu
 	long cpu = (long) ((struct seq_file *) filp->private_data)->private;
 	struct rq *rq = cpu_rq(cpu);
 	u64 runtime, period;
+	int retval = 0;
 	size_t err;
-	int retval;
 	u64 value;
 
 	err = kstrtoull_from_user(ubuf, cnt, 10, &value);
@@ -387,8 +387,6 @@ static ssize_t sched_fair_server_write(struct file *filp, const char __user *ubu
 		}
 
 		retval = dl_server_apply_params(&rq->fair_server, runtime, period, 0);
-		if (retval)
-			cnt = retval;
 
 		if (!runtime)
 			printk_deferred("Fair server disabled in CPU %d, system may crash due to starvation.\n",
@@ -396,6 +394,9 @@ static ssize_t sched_fair_server_write(struct file *filp, const char __user *ubu
 
 		if (rq->cfs.h_nr_queued)
 			dl_server_start(&rq->fair_server);
+
+		if (retval < 0)
+			return retval;
 	}
 
 	*ppos += cnt;
Signature · 1 line
2.43.0
maintainer
#11 lore raw

Re: [PATCH v2 04/10] sched/debug: Fix updating of ppos on server write ops

Hi Joel,

Quoted 7 lines · Joel Fernandes

Updating "ppos" on error conditions does not make much sense. The pattern is to return the error code directly without modifying the position, or modify the position on success and return the number of bytes written.

Since on success, the return value of apply is 0, there is no point in modifying ppos either. Fix it by removing all this and just returning error code or number of bytes written on success.

Looks like patches 04, 05 and 07 are standalone fixes. If that is indeed the case maybe we could move them to the start of this series so that they can picked up independently (or split them to a separate series)?

Thanks,
Juri

#12 lore raw

PATCH v2 5/10 sched/debug: Stop and start server based on if it was active

In reply to Joel Fernandes

Currently the DL server interface for applying parameters checkfs CFS-internals to identify if the server is active. This is error-prone and make it difficult when adding new servers in the future.

Fix it, by using dl_server_active() which is also used by the DL server code to determine if the DL server was started.

---
 kernel/sched/debug.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/sched/debug.c +4 -2
index 77b5d4bebc59..6866f0a9e88c 100644
@@ -349,6 +349,7 @@ static ssize_t sched_fair_server_write(struct file *filp, const char __user *ubu
 {
 	long cpu = (long) ((struct seq_file *) filp->private_data)->private;
 	struct rq *rq = cpu_rq(cpu);
+	bool was_active = false;
 	u64 runtime, period;
 	int retval = 0;
 	size_t err;
@@ -381,7 +382,8 @@ static ssize_t sched_fair_server_write(struct file *filp, const char __user *ubu
 			return  -EINVAL;
 		}
 
-		if (rq->cfs.h_nr_queued) {
+		if (dl_server_active(&rq->fair_server)) {
+			was_active = true;
 			update_rq_clock(rq);
 			dl_server_stop(&rq->fair_server);
 		}
@@ -392,7 +394,7 @@ static ssize_t sched_fair_server_write(struct file *filp, const char __user *ubu
 			printk_deferred("Fair server disabled in CPU %d, system may crash due to starvation.\n",
 					cpu_of(rq));
 
-		if (rq->cfs.h_nr_queued)
+		if (was_active)
 			dl_server_start(&rq->fair_server);
 
 		if (retval < 0)
Signature · 1 line
2.43.0
#13 lore raw

PATCH v2 6/10 sched/debug: Add support to change sched_ext server params

In reply to Joel Fernandes

When a sched_ext server is loaded, tasks in CFS are converted to run in sched_ext class. Modify the ext server parameters as well along with the fair ones.

Re-use the existing interface to modify both ext and fair servers to keep number of interfaces less (as it is, we have a per-cpu interface).

---
 kernel/sched/debug.c | 91 ++++++++++++++++++++++++++------------------
 1 file changed, 54 insertions(+), 37 deletions(-)
kernel/sched/debug.c +54 -37
index 6866f0a9e88c..f1af2999a8e5 100644
@@ -341,17 +341,18 @@ enum dl_param {
 	DL_PERIOD,
 };
 
-static unsigned long fair_server_period_max = (1UL << 22) * NSEC_PER_USEC; /* ~4 seconds */
-static unsigned long fair_server_period_min = (100) * NSEC_PER_USEC;     /* 100 us */
+static unsigned long dl_server_period_max = (1UL << 22) * NSEC_PER_USEC; /* ~4 seconds */
+static unsigned long dl_server_period_min = (100) * NSEC_PER_USEC;     /* 100 us */
 
-static ssize_t sched_fair_server_write(struct file *filp, const char __user *ubuf,
+static ssize_t sched_dl_server_write(struct file *filp, const char __user *ubuf,
 				       size_t cnt, loff_t *ppos, enum dl_param param)
 {
 	long cpu = (long) ((struct seq_file *) filp->private_data)->private;
 	struct rq *rq = cpu_rq(cpu);
-	bool was_active = false;
+	bool was_active_fair = false;
+	bool was_active_ext = false;
+	int retval = 0, retval2 = 0;
 	u64 runtime, period;
-	int retval = 0;
 	size_t err;
 	u64 value;
 
@@ -377,41 +378,57 @@ static ssize_t sched_fair_server_write(struct file *filp, const char __user *ubu
 		}
 
 		if (runtime > period ||
-		    period > fair_server_period_max ||
-		    period < fair_server_period_min) {
+		    period > dl_server_period_max ||
+		    period < dl_server_period_min) {
 			return  -EINVAL;
 		}
 
 		if (dl_server_active(&rq->fair_server)) {
-			was_active = true;
+			was_active_fair = true;
 			update_rq_clock(rq);
 			dl_server_stop(&rq->fair_server);
 		}
 
+		if (dl_server_active(&rq->ext_server)) {
+			was_active_ext = true;
+			update_rq_clock(rq);
+			dl_server_stop(&rq->ext_server);
+		}
+
 		retval = dl_server_apply_params(&rq->fair_server, runtime, period, 0);
+		retval2 = dl_server_apply_params(&rq->ext_server, runtime, period, 0);
 
 		if (!runtime)
-			printk_deferred("Fair server disabled in CPU %d, system may crash due to starvation.\n",
+			printk_deferred("Deadline servers are disabled on CPU %d, system may crash due to starvation.\n",
 					cpu_of(rq));
 
-		if (was_active)
+		if (was_active_fair)
 			dl_server_start(&rq->fair_server);
 
+		if (was_active_ext)
+			dl_server_start(&rq->ext_server);
+
 		if (retval < 0)
 			return retval;
+		if (retval2 < 0)
+			return retval2;
 	}
 
 	*ppos += cnt;
 	return cnt;
 }
 
-static size_t sched_fair_server_show(struct seq_file *m, void *v, enum dl_param param)
+static size_t sched_dl_server_show(struct seq_file *m, void *v, enum dl_param param)
 {
 	unsigned long cpu = (unsigned long) m->private;
 	struct rq *rq = cpu_rq(cpu);
 	u64 value;
 
 	switch (param) {
+	/*
+	 * The params for fair server and ext server as set via debugfs
+	 * are the same, so we can just use one of them
+	 */
 	case DL_RUNTIME:
 		value = rq->fair_server.dl_runtime;
 		break;
@@ -426,50 +443,50 @@ static size_t sched_fair_server_show(struct seq_file *m, void *v, enum dl_param
 }
 
 static ssize_t
-sched_fair_server_runtime_write(struct file *filp, const char __user *ubuf,
+sched_dl_server_runtime_write(struct file *filp, const char __user *ubuf,
 				size_t cnt, loff_t *ppos)
 {
-	return sched_fair_server_write(filp, ubuf, cnt, ppos, DL_RUNTIME);
+	return sched_dl_server_write(filp, ubuf, cnt, ppos, DL_RUNTIME);
 }
 
-static int sched_fair_server_runtime_show(struct seq_file *m, void *v)
+static int sched_dl_server_runtime_show(struct seq_file *m, void *v)
 {
-	return sched_fair_server_show(m, v, DL_RUNTIME);
+	return sched_dl_server_show(m, v, DL_RUNTIME);
 }
 
-static int sched_fair_server_runtime_open(struct inode *inode, struct file *filp)
+static int sched_dl_server_runtime_open(struct inode *inode, struct file *filp)
 {
-	return single_open(filp, sched_fair_server_runtime_show, inode->i_private);
+	return single_open(filp, sched_dl_server_runtime_show, inode->i_private);
 }
 
-static const struct file_operations fair_server_runtime_fops = {
-	.open		= sched_fair_server_runtime_open,
-	.write		= sched_fair_server_runtime_write,
+static const struct file_operations dl_server_runtime_fops = {
+	.open		= sched_dl_server_runtime_open,
+	.write		= sched_dl_server_runtime_write,
 	.read		= seq_read,
 	.llseek		= seq_lseek,
 	.release	= single_release,
 };
 
 static ssize_t
-sched_fair_server_period_write(struct file *filp, const char __user *ubuf,
+sched_dl_server_period_write(struct file *filp, const char __user *ubuf,
 			       size_t cnt, loff_t *ppos)
 {
-	return sched_fair_server_write(filp, ubuf, cnt, ppos, DL_PERIOD);
+	return sched_dl_server_write(filp, ubuf, cnt, ppos, DL_PERIOD);
 }
 
-static int sched_fair_server_period_show(struct seq_file *m, void *v)
+static int sched_dl_server_period_show(struct seq_file *m, void *v)
 {
-	return sched_fair_server_show(m, v, DL_PERIOD);
+	return sched_dl_server_show(m, v, DL_PERIOD);
 }
 
-static int sched_fair_server_period_open(struct inode *inode, struct file *filp)
+static int sched_dl_server_period_open(struct inode *inode, struct file *filp)
 {
-	return single_open(filp, sched_fair_server_period_show, inode->i_private);
+	return single_open(filp, sched_dl_server_period_show, inode->i_private);
 }
 
-static const struct file_operations fair_server_period_fops = {
-	.open		= sched_fair_server_period_open,
-	.write		= sched_fair_server_period_write,
+static const struct file_operations dl_server_period_fops = {
+	.open		= sched_dl_server_period_open,
+	.write		= sched_dl_server_period_write,
 	.read		= seq_read,
 	.llseek		= seq_lseek,
 	.release	= single_release,
@@ -477,13 +494,13 @@ static const struct file_operations fair_server_period_fops = {
 
 static struct dentry *debugfs_sched;
 
-static void debugfs_fair_server_init(void)
+static void debugfs_dl_server_init(void)
 {
-	struct dentry *d_fair;
+	struct dentry *d_server;
 	unsigned long cpu;
 
-	d_fair = debugfs_create_dir("fair_server", debugfs_sched);
-	if (!d_fair)
+	d_server = debugfs_create_dir("dl_server", debugfs_sched);
+	if (!d_server)
 		return;
 
 	for_each_possible_cpu(cpu) {
@@ -491,10 +508,10 @@ static void debugfs_fair_server_init(void)
 		char buf[32];
 
 		snprintf(buf, sizeof(buf), "cpu%lu", cpu);
-		d_cpu = debugfs_create_dir(buf, d_fair);
+		d_cpu = debugfs_create_dir(buf, d_server);
 
-		debugfs_create_file("runtime", 0644, d_cpu, (void *) cpu, &fair_server_runtime_fops);
-		debugfs_create_file("period", 0644, d_cpu, (void *) cpu, &fair_server_period_fops);
+		debugfs_create_file("runtime", 0644, d_cpu, (void *) cpu, &dl_server_runtime_fops);
+		debugfs_create_file("period", 0644, d_cpu, (void *) cpu, &dl_server_period_fops);
 	}
 }
 
@@ -537,7 +554,7 @@ static __init int sched_init_debug(void)
 
 	debugfs_create_file("debug", 0444, debugfs_sched, NULL, &sched_debug_fops);
 
-	debugfs_fair_server_init();
+	debugfs_dl_server_init();
 
 	return 0;
 }
Signature · 1 line
2.43.0
maintainer
#14 lore raw

Re: [PATCH v2 06/10] sched/debug: Add support to change sched_ext server params

Hi Joel,

Quoted 6 lines · Joel Fernandes

When a sched_ext server is loaded, tasks in CFS are converted to run in sched_ext class. Modify the ext server parameters as well along with the fair ones.

Re-use the existing interface to modify both ext and fair servers to keep number of interfaces less (as it is, we have a per-cpu interface).

We have a bit of code duplication, don't we? I wonder if we can do anything early on to prevent mis-alignment between servers in the future.

Also, is a single shared interface enough? Is the assumption that either all tasks are FAIR or SCX always guaranteed?

Thanks,
Juri

reviewer
#15 lore raw

Re: [PATCH v2 06/10] sched/debug: Add support to change sched_ext server params

Quoted 10 lines · Juri Lelli

Hi Joel,

Quoted 6 lines · Joel Fernandes

When a sched_ext server is loaded, tasks in CFS are converted to run in sched_ext class. Modify the ext server parameters as well along with the fair ones.

Re-use the existing interface to modify both ext and fair servers to keep number of interfaces less (as it is, we have a per-cpu interface).

Quoted 6 lines

We have a bit of code duplication, don't we? I wonder if we can do anything early on to prevent mis-alignment between servers in the future.

Also, is a single shared interface enough? Is the assumption that either all tasks are FAIR or SCX always guaranteed?

It's not a guarantee but at least all the current use cases are like that. No objection to splitting the interface tho. In fact, for SCX, it may make more sense to just make it part of sched_ext_ops, so that each scheduler can specify what they want.

Thanks.

Signature · 1 line
tejun
#16 lore raw

Re: [PATCH v2 06/10] sched/debug: Add support to change sched_ext server params

Hi,

Quoted 4 lines · Tejun Heo
Quoted 3 lines · Juri Lelli

Hi Joel,

On 02/06/25 14:01, Joel Fernandes wrote:

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

When a sched_ext server is loaded, tasks in CFS are converted to run in sched_ext class. Modify the ext server parameters as well along with the fair ones.

Re-use the existing interface to modify both ext and fair servers to keep number of interfaces less (as it is, we have a per-cpu interface).

We have a bit of code duplication, don't we

Hmm, I did try to keep the duplication less but I'll give it another try.

Quoted 3 lines
Quoted 3 lines

I wonder if we can do
anything early on to prevent mis-alignment between servers in the future.

Sure, let me look into that too.

Quoted 8 lines
Quoted 3 lines

Also, is a single shared interface enough? Is the assumption that either all tasks are FAIR or SCX always guaranteed?

It's not a guarantee but at least all the current use cases are like that. No objection to splitting the interface tho. In fact, for SCX, it may make more sense to just make it part of sched_ext_ops, so that each scheduler can specify what they want.

I am Ok with having different debugfs entries for different servers, and perhaps we can reuse the code for both to reduce code duplication. Will work on that next.

thanks,

- Joel

#17 lore raw

PATCH v2 7/10 sched/deadline: Clear the defer params

In reply to Joel Fernandes

The defer params were not cleared in __dl_clear_params. Clear them.

Without this is some of my test cases are flaking and the DL timer is not starting correctly AFAICS.

Fixes a110a81c52a9 ("sched/deadline: Deferrable dl server")
---
 kernel/sched/deadline.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/sched/deadline.c +3 -0
index b703cbf627c1..4ed61266f3ea 100644
@@ -3432,6 +3432,9 @@ static void __dl_clear_params(struct sched_dl_entity *dl_se)
 	dl_se->dl_non_contending	= 0;
 	dl_se->dl_overrun		= 0;
 	dl_se->dl_server		= 0;
+	dl_se->dl_defer			= 0;
+	dl_se->dl_defer_running	= 0;
+	dl_se->dl_defer_armed	= 0;
 
 #ifdef CONFIG_RT_MUTEXES
 	dl_se->pi_se			= dl_se;
Signature · 1 line
2.43.0
#18 lore raw

PATCH v2 8/10 sched/deadline: Add support to remove DL server bandwidth

In reply to Joel Fernandes

The DL server for sched_ext will have its bandwidth removed when sched_ext is unloaded. Add support to DEADLINE for this, so that the sched_ext DL server may do so.

---
 kernel/sched/deadline.c | 57 +++++++++++++++++++++++++++++++++++++++++
 kernel/sched/sched.h    |  1 +
 2 files changed, 58 insertions(+)
kernel/sched/deadline.c +57 -0
index 4ed61266f3ea..e585ff39fcc3 100644
@@ -1752,6 +1752,63 @@ int dl_server_apply_params(struct sched_dl_entity *dl_se, u64 runtime, u64 perio
 	return retval;
 }
 
+/**
+ * dl_server_remove_params - Remove bandwidth reservation for a DL server
+ * @dl_se: The DL server entity to remove bandwidth for
+ *
+ * This function removes the bandwidth reservation for a DL server entity,
+ * cleaning up all bandwidth accounting and server state.
+ *
+ * Returns: 0 on success, negative error code on failure
+ */
+int dl_server_remove_params(struct sched_dl_entity *dl_se)
+{
+	struct rq *rq = dl_se->rq;
+	int cpu = cpu_of(rq);
+	struct dl_bw *dl_b;
+	int cpus;
+
+	if (!dl_se->dl_runtime)
+		return 0;  /* Already disabled */
+
+	/*
+	 * First dequeue if still queued. It should not be queued since
+	 * we call this only after the last dl_server_stop().
+	 */
+	if (WARN_ON_ONCE(on_dl_rq(dl_se)))
+		dequeue_dl_entity(dl_se, DEQUEUE_SLEEP);
+
+	/* Cancel any pending timers */
+	hrtimer_try_to_cancel(&dl_se->dl_timer);
+	hrtimer_try_to_cancel(&dl_se->inactive_timer);
+
+	/* Remove bandwidth from both runqueue and root domain accounting */
+	dl_b = dl_bw_of(cpu);
+	guard(raw_spinlock)(&dl_b->lock);
+	cpus = dl_bw_cpus(cpu);
+	if (!cpus)
+		return 0;
+
+	sub_rq_bw(dl_se, &rq->dl);
+	__dl_sub(dl_b, dl_se->dl_bw, cpus);
+
+	/*
+	 * If server was active and consuming bandwidth, remove it from
+	 * running bandwidth accounting. This should not happen because
+	 * we call this only after the last dl_server_stop().
+	 */
+	if (WARN_ON_ONCE(!dl_se->dl_non_contending))
+		sub_running_bw(dl_se, &rq->dl);
+
+	/*
+	 * Clear all server parameters. This will also clear ->dl_server so
+	 * the next dl_server_apply_params() will reconfigure the server.
+	 */
+	__dl_clear_params(dl_se);
+
+	return 0;
+}
+
 /*
  * Update the current task's runtime statistics (provided it is still
  * a -deadline task and has not been removed from the dl_rq).
kernel/sched/sched.h +1 -0
index d206421b1146..e6af0c1fc985 100644
@@ -393,6 +393,7 @@ extern void ext_server_init(struct rq *rq);
 extern void __dl_server_attach_root(struct sched_dl_entity *dl_se, struct rq *rq);
 extern int dl_server_apply_params(struct sched_dl_entity *dl_se,
 		    u64 runtime, u64 period, bool init);
+extern int dl_server_remove_params(struct sched_dl_entity *dl_se);
 
 static inline bool dl_server_active(struct sched_dl_entity *dl_se)
 {
Signature · 1 line
2.43.0
#19 lore raw

PATCH v2 9/10 sched/ext: Relinquish DL server reservations when not needed

In reply to Joel Fernandes

I tested loading a test SCX program and verifying the bandwidth both before and after applying the patch:

Without patch:
Before loading scx:
.dl_bw->total_bw : 1887408
After unloading scx:
.dl_bw->total_bw : 3774816

After patch:
Before loading scx:
.dl_bw->total_bw : 1887408
After unloading scx:
.dl_bw->total_bw : 1887408

---
 kernel/sched/ext.c | 44 ++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 40 insertions(+), 4 deletions(-)
kernel/sched/ext.c +40 -4
index 52f98c3944ed..c938a19cd44f 100644
@@ -4784,13 +4784,28 @@ static void scx_ops_disable_workfn(struct kthread_work *work)
 	scx_task_iter_stop(&sti);
 	percpu_up_write(&scx_fork_rwsem);
 
-	/*
-	 * Invalidate all the rq clocks to prevent getting outdated
-	 * rq clocks from a previous scx scheduler.
-	 */
 	for_each_possible_cpu(cpu) {
 		struct rq *rq = cpu_rq(cpu);
+		struct rq_flags rf;
+
+		/*
+		 * Invalidate all the rq clocks to prevent getting outdated
+		 * rq clocks from a previous scx scheduler.
+		 */
 		scx_rq_clock_invalidate(rq);
+
+		/*
+		 * We are unloading the sched_ext scheduler, we do not need its
+		 * DL server bandwidth anymore, remove it for all CPUs. Whenever
+		 * the first SCX task is enqueued (when scx is re-loaded), its DL
+		 * server bandwidth will be re-initialized.
+		 */
+		rq_lock(rq, &rf);
+		if (dl_server_active(&rq->ext_server)) {
+			dl_server_stop(&rq->ext_server);
+		}
+		dl_server_remove_params(&rq->ext_server);
+		rq_unlock(rq, &rf);
 	}
 
 	/* no task is on scx, turn off all the switches and flush in-progress calls */
@@ -5547,6 +5562,27 @@ static int scx_ops_enable(struct sched_ext_ops *ops, struct bpf_link *link)
 		check_class_changed(task_rq(p), p, old_class, p->prio);
 	}
 	scx_task_iter_stop(&sti);
+
+	if (scx_switching_all) {
+		for_each_possible_cpu(cpu) {
+			struct rq *rq = cpu_rq(cpu);
+			struct rq_flags rf;
+
+			/*
+			 * We are switching all fair tasks to the sched_ext scheduler,
+			 * we do not need fair server's DL bandwidth anymore, remove it
+			 * for all CPUs. Whenever the first CFS task is enqueued (when
+			 * scx is unloaded), the fair server's DL bandwidth will be
+			 * re-initialized.
+			 */
+			rq_lock_irqsave(rq, &rf);
+			if (dl_server_active(&rq->fair_server))
+				dl_server_stop(&rq->fair_server);
+			dl_server_remove_params(&rq->fair_server);
+			rq_unlock_irqrestore(rq, &rf);
+		}
+	}
+
 	percpu_up_write(&scx_fork_rwsem);
 
 	scx_ops_bypass(false);
Signature · 1 line
2.43.0
#20 lore raw

Re: [PATCH v2 09/10] sched/ext: Relinquish DL server reservations when not needed

Quoted 44 lines · Joel Fernandes

I tested loading a test SCX program and verifying the bandwidth both before and after applying the patch:

Without patch:
Before loading scx:
.dl_bw->total_bw : 1887408
After unloading scx:
.dl_bw->total_bw : 3774816

After patch:
Before loading scx:
.dl_bw->total_bw : 1887408
After unloading scx:
.dl_bw->total_bw : 1887408

---
 kernel/sched/ext.c | 44 ++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 40 insertions(+), 4 deletions(-)
kernel/sched/ext.c +9 -4
index 52f98c3944ed..c938a19cd44f 100644
@@ -4784,13 +4784,28 @@ static void scx_ops_disable_workfn(struct kthread_work *work)
 	scx_task_iter_stop(&sti);
 	percpu_up_write(&scx_fork_rwsem);
 
-	/*
-	 * Invalidate all the rq clocks to prevent getting outdated
-	 * rq clocks from a previous scx scheduler.
-	 */
 	for_each_possible_cpu(cpu) {
 		struct rq *rq = cpu_rq(cpu);
+		struct rq_flags rf;
+
+		/*
+		 * Invalidate all the rq clocks to prevent getting outdated
+		 * rq clocks from a previous scx scheduler.
+		 */
 		scx_rq_clock_invalidate(rq);
+
+		/*
+		 * We are unloading the sched_ext scheduler, we do not need its

kernel/sched/ext.c static void scx_ops_disable_workfn(struct kthread_work *work) · line 4808

Quoted 10 lines
kernel/sched/ext.c +10 -0
+		 * DL server bandwidth anymore, remove it for all CPUs. Whenever
+		 * the first SCX task is enqueued (when scx is re-loaded), its DL
+		 * server bandwidth will be re-initialized.
+		 */
+		rq_lock(rq, &rf);
+		if (dl_server_active(&rq->ext_server)) {
+			dl_server_stop(&rq->ext_server);
+		}
+		dl_server_remove_params(&rq->ext_server);
+		rq_unlock(rq, &rf);

I am sorry - this bit also needs to use the irq-disable locking:

		rq_lock_irqsave(rq, &rf);
                ...
		rq_unlock_irqrestore(rq, &rf);

Otherwise the sched_ext self-test hangs. I have squashed it in my tree for next revision.

thanks,

- Joel

Quoted 31 lines
kernel/sched/ext.c +21 -0
 	}
 
 	/* no task is on scx, turn off all the switches and flush in-progress calls */
@@ -5547,6 +5562,27 @@ static int scx_ops_enable(struct sched_ext_ops *ops, struct bpf_link *link)
 		check_class_changed(task_rq(p), p, old_class, p->prio);
 	}
 	scx_task_iter_stop(&sti);
+
+	if (scx_switching_all) {
+		for_each_possible_cpu(cpu) {
+			struct rq *rq = cpu_rq(cpu);
+			struct rq_flags rf;
+
+			/*
+			 * We are switching all fair tasks to the sched_ext scheduler,
+			 * we do not need fair server's DL bandwidth anymore, remove it
+			 * for all CPUs. Whenever the first CFS task is enqueued (when
+			 * scx is unloaded), the fair server's DL bandwidth will be
+			 * re-initialized.
+			 */
+			rq_lock_irqsave(rq, &rf);
+			if (dl_server_active(&rq->fair_server))
+				dl_server_stop(&rq->fair_server);
+			dl_server_remove_params(&rq->fair_server);
+			rq_unlock_irqrestore(rq, &rf);
+		}
+	}
+
 	percpu_up_write(&scx_fork_rwsem);
 
 	scx_ops_bypass(false);
#21 lore raw

PATCH v2 10/10 selftests/sched_ext: Add test for sched_ext dl_server

In reply to Joel Fernandes

From: Andrea Righi <[email protected]>

Add a selftest to validate the correct behavior of the deadline server for the ext_sched_class.

[ Joel: Replaced occurences of CFS in the test with EXT. ]

---
 tools/testing/selftests/sched_ext/Makefile    |   1 +
 .../selftests/sched_ext/rt_stall.bpf.c        |  23 ++
 tools/testing/selftests/sched_ext/rt_stall.c  | 213 ++++++++++++++++++
 3 files changed, 237 insertions(+)
 create mode 100644 tools/testing/selftests/sched_ext/rt_stall.bpf.c
 create mode 100644 tools/testing/selftests/sched_ext/rt_stall.c
tools/testing/selftests/sched_ext/Makefile +1 -0
index f4531327b8e7..dcc803eeab39 100644
@@ -181,6 +181,7 @@ auto-test-targets :=			\
 	select_cpu_dispatch_bad_dsq	\
 	select_cpu_dispatch_dbl_dsp	\
 	select_cpu_vtime		\
+	rt_stall			\
 	test_example			\
 
 testcase-targets := $(addsuffix .o,$(addprefix $(SCXOBJ_DIR)/,$(auto-test-targets)))
tools/testing/selftests/sched_ext/rt_stall.bpf.c new file +23 -0
new file mode 100644
index 000000000000..80086779dd1e
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A scheduler that verified if RT tasks can stall SCHED_EXT tasks.
+ *
+ * Copyright (c) 2025 NVIDIA Corporation.
+ */
+
+#include <scx/common.bpf.h>
+
+char _license[] SEC("license") = "GPL";
+
+UEI_DEFINE(uei);
+
+void BPF_STRUCT_OPS(rt_stall_exit, struct scx_exit_info *ei)
+{
+	UEI_RECORD(uei, ei);
+}
+
+SEC(".struct_ops.link")
+struct sched_ext_ops rt_stall_ops = {
+	.exit			= (void *)rt_stall_exit,
+	.name			= "rt_stall",
+};
tools/testing/selftests/sched_ext/rt_stall.c new file +213 -0

213 lines not on this page · raw message

Signature · 1 line
2.43.0