[PATCH v5 11/14] sched/ext: Relinquish DL server reservations when not needed

Joel Fernandes
2025-06-20T20:33:09Z

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

Co-developed-by: Andrea Righi <arighi@nvidia.com>
Signed-off-by: Joel Fernandes <joelagnelf@nvidia.com>
---
kernel/sched/ext.c | 56 ++++++++++++++++++++++++++++++++++++++--------
1 file changed, 47 insertions(+), 9 deletions(-)

diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c
index 34c95100fbe5..6bc0ce358238 100644
--- a/kernel/sched/ext.c
+++ b/kernel/sched/ext.c
@@ -4965,6 +4965,31 @@ static void scx_disable_workfn(struct kthread_work *work)

scx_init_task_enabled = false;

+ 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_irqsave(rq, &rf);
+ update_rq_clock(rq);
+ if (dl_server_active(&rq->ext_server))
+ dl_server_stop(&rq->ext_server);
+ dl_server_remove_params(&rq->ext_server);
+ rq_unlock_irqrestore(rq, &rf);
+ }
+
+
scx_task_iter_start(&sti);
while ((p = scx_task_iter_next_locked(&sti))) {
const struct sched_class *old_class = p->sched_class;
@@ -4988,15 +5013,6 @@ static void scx_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);
- scx_rq_clock_invalidate(rq);
- }
-
/* no task is on scx, turn off all the switches and flush in-progress calls */
static_branch_disable(&__scx_enabled);
bitmap_zero(sch->has_op, SCX_OPI_END);
@@ -5730,6 +5746,28 @@ static int scx_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);
+ update_rq_clock(rq);
+ 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_bypass(false);
--
2.43.0