Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: Andrea Righi <arighi@nvidia.com>
To: Ingo Molnar <mingo@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Juri Lelli <juri.lelli@redhat.com>,
	Vincent Guittot <vincent.guittot@linaro.org>
Cc: Dietmar Eggemann <dietmar.eggemann@arm.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>,
	Valentin Schneider <vschneid@redhat.com>,
	Tejun Heo <tj@kernel.org>, David Vernet <void@manifault.com>,
	Changwoo Min <changwoo@igalia.com>, Shuah Khan <shuah@kernel.org>,
	Joel Fernandes <joelagnelf@nvidia.com>,
	Christian Loehle <christian.loehle@arm.com>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	Luigi De Matteis <ldematteis123@gmail.com>,
	sched-ext@lists.linux.dev, bpf@vger.kernel.org,
	linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 04/11] sched/deadline: Add support to initialize and remove dl_server bandwidth
Date: Wed, 29 Oct 2025 20:08:41 +0100	[thread overview]
Message-ID: <20251029191111.167537-5-arighi@nvidia.com> (raw)
In-Reply-To: <20251029191111.167537-1-arighi@nvidia.com>

During switching from sched_ext to fair tasks and vice-versa, we need
support for intializing and removing the bandwidth contribution of
either DL server.

Add support for handling these transitions.

Moreover, remove references specific to the fair server, in preparation
for adding the ext server.

v2: - wait for inactive_task_timer to fire before removing the bandwidth
      reservation (Juri Lelli)
    - add WARN_ON_ONCE(!cpus) sanity check in dl_server_apply_params()
      (Andrea Righi)

Co-developed-by: Joel Fernandes <joelagnelf@nvidia.com>
Signed-off-by: Joel Fernandes <joelagnelf@nvidia.com>
Signed-off-by: Andrea Righi <arighi@nvidia.com>
---
 kernel/sched/deadline.c | 96 ++++++++++++++++++++++++++++++++++-------
 kernel/sched/sched.h    |  3 ++
 2 files changed, 84 insertions(+), 15 deletions(-)

diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
index 4aefb34a1d38b..8aff1aba7b8a9 100644
--- a/kernel/sched/deadline.c
+++ b/kernel/sched/deadline.c
@@ -1441,8 +1441,8 @@ static void update_curr_dl_se(struct rq *rq, struct sched_dl_entity *dl_se, s64
 	dl_se->runtime -= scaled_delta_exec;
 
 	/*
-	 * The fair server can consume its runtime while throttled (not queued/
-	 * running as regular CFS).
+	 * The dl_server can consume its runtime while throttled (not
+	 * queued / running as regular fair task).
 	 *
 	 * If the server consumes its entire runtime in this state. The server
 	 * is not required for the current period. Thus, reset the server by
@@ -1501,10 +1501,10 @@ 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.
+	 * The dl_server does not account real-time workload because it
+	 * runs non-RT tasks.
 	 */
-	if (dl_se == &rq->fair_server)
+	if (dl_se->dl_server)
 		return;
 
 #ifdef CONFIG_RT_GROUP_SCHED
@@ -1540,8 +1540,8 @@ static void update_curr_dl_se(struct rq *rq, struct sched_dl_entity *dl_se, s64
  * server provides a guarantee.
  *
  * If the dl_server is in defer mode, the idle time is also considered
- * as time available for the fair server, avoiding a penalty for the
- * rt scheduler that did not consumed that time.
+ * as time available for the dl_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)
 {
@@ -1570,11 +1570,37 @@ void dl_server_update_idle_time(struct rq *rq, struct task_struct *p)
 
 void dl_server_update(struct sched_dl_entity *dl_se, s64 delta_exec)
 {
-	/* 0 runtime = fair server disabled */
+	/* 0 runtime = dl_server disabled */
 	if (dl_se->dl_runtime)
 		update_curr_dl_se(dl_se->rq, dl_se, delta_exec);
 }
 
+/**
+ * dl_server_init_params - Initialize bandwidth reservation for a DL server
+ * @dl_se: The DL server entity to remove bandwidth for
+ *
+ * This function initializes the bandwidth reservation for a DL server
+ * entity, its bandwidth accounting and server state.
+ *
+ * Returns: 0 on success, negative error code on failure
+ */
+int dl_server_init_params(struct sched_dl_entity *dl_se)
+{
+	u64 runtime =  50 * NSEC_PER_MSEC;
+	u64 period = 1000 * NSEC_PER_MSEC;
+	int err;
+
+	err = dl_server_apply_params(dl_se, runtime, period, 1);
+	if (err)
+		return err;
+
+	dl_se->dl_server = 1;
+	dl_se->dl_defer = 1;
+	setup_new_dl_entity(dl_se);
+
+	return err;
+}
+
 void dl_server_start(struct sched_dl_entity *dl_se)
 {
 	struct rq *rq = dl_se->rq;
@@ -1614,8 +1640,7 @@ void sched_init_dl_servers(void)
 	struct sched_dl_entity *dl_se;
 
 	for_each_online_cpu(cpu) {
-		u64 runtime =  50 * NSEC_PER_MSEC;
-		u64 period = 1000 * NSEC_PER_MSEC;
+		int err;
 
 		rq = cpu_rq(cpu);
 
@@ -1625,11 +1650,8 @@ void sched_init_dl_servers(void)
 
 		WARN_ON(dl_server(dl_se));
 
-		dl_server_apply_params(dl_se, runtime, period, 1);
-
-		dl_se->dl_server = 1;
-		dl_se->dl_defer = 1;
-		setup_new_dl_entity(dl_se);
+		err = dl_server_init_params(dl_se);
+		WARN_ON_ONCE(err);
 	}
 }
 
@@ -1663,6 +1685,9 @@ int dl_server_apply_params(struct sched_dl_entity *dl_se, u64 runtime, u64 perio
 	guard(raw_spinlock)(&dl_b->lock);
 
 	cpus = dl_bw_cpus(cpu);
+	if (WARN_ON_ONCE(!cpus))
+		return -ENODEV;
+
 	cap = dl_bw_capacity(cpu);
 
 	if (__dl_overflow(dl_b, cap, old_bw, new_bw))
@@ -1678,6 +1703,12 @@ int dl_server_apply_params(struct sched_dl_entity *dl_se, u64 runtime, u64 perio
 		dl_rq_change_utilization(rq, dl_se, new_bw);
 	}
 
+	/* Clear these so that the dl_server is reinitialized */
+	if (new_bw == 0) {
+		dl_se->dl_defer = 0;
+		dl_se->dl_server = 0;
+	}
+
 	dl_se->dl_runtime = runtime;
 	dl_se->dl_deadline = period;
 	dl_se->dl_period = period;
@@ -1691,6 +1722,41 @@ 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, struct rq_flags *rf)
+{
+	if (!dl_se->dl_server)
+		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);
+
+	if (hrtimer_try_to_cancel(&dl_se->inactive_timer) == -1) {
+		rq_unlock_irqrestore(rq, rf);
+
+		hrtimer_cancel(&dl_se->inactive_timer);
+
+		rq_lock_irqsave(rq, rf);
+		update_rq_clock(rq);
+	}
+
+	/* Remove bandwidth reservation */
+	return dl_server_apply_params(dl_se, 0, dl_se->dl_period, false);
+}
+
 /*
  * Update the current task's runtime statistics (provided it is still
  * a -deadline task and has not been removed from the dl_rq).
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 27aae2a298f8b..4a0bf38dc71e9 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -417,6 +417,9 @@ 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,
 		    u64 runtime, u64 period, bool init);
+extern int dl_server_init_params(struct sched_dl_entity *dl_se);
+extern int dl_server_remove_params(struct sched_dl_entity *dl_se,
+				   struct rq *rq, struct rq_flags *rf);
 
 static inline bool dl_server_active(struct sched_dl_entity *dl_se)
 {
-- 
2.51.2


  parent reply	other threads:[~2025-10-29 19:12 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-29 19:08 [PATCHSET v10 sched_ext/for-6.19] Add a deadline server for sched_ext tasks Andrea Righi
2025-10-29 19:08 ` [PATCH 01/11] sched/debug: Fix updating of ppos on server write ops Andrea Righi
2025-10-29 19:08 ` [PATCH 02/11] sched/debug: Stop and start server based on if it was active Andrea Righi
2025-11-06  7:13   ` Juri Lelli
2025-11-06 16:39     ` Andrea Righi
2025-11-07  6:51       ` Juri Lelli
2025-11-12 17:35         ` Andrea Righi
2025-10-29 19:08 ` [PATCH 03/11] sched/deadline: Clear the defer params Andrea Righi
2025-10-29 19:08 ` Andrea Righi [this message]
2025-11-06  9:49   ` [PATCH 04/11] sched/deadline: Add support to initialize and remove dl_server bandwidth Juri Lelli
2025-11-06 17:09     ` Andrea Righi
2025-11-07 13:53       ` Juri Lelli
2025-10-29 19:08 ` [PATCH 05/11] sched/deadline: Add a server arg to dl_server_update_idle_time() Andrea Righi
2025-10-29 19:08 ` [PATCH 06/11] sched_ext: Add a DL server for sched_ext tasks Andrea Righi
2025-11-06 10:59   ` Juri Lelli
2025-11-06 17:15     ` Andrea Righi
2025-10-29 19:08 ` [PATCH 07/11] sched/debug: Add support to change sched_ext server params Andrea Righi
2025-10-29 19:08 ` [PATCH 08/11] sched/deadline: Account ext server bandwidth Andrea Righi
2025-10-29 19:08 ` [PATCH 09/11] sched_ext: Selectively enable ext and fair DL servers Andrea Righi
2025-10-29 19:08 ` [PATCH 10/11] selftests/sched_ext: Add test for sched_ext dl_server Andrea Righi
2025-10-30 16:49   ` Christian Loehle
2025-10-30 16:57     ` Andrea Righi
2025-10-29 19:08 ` [PATCH 11/11] selftests/sched_ext: Add test for DL server total_bw consistency Andrea Righi
2025-10-30 17:00 ` [PATCHSET v10 sched_ext/for-6.19] Add a deadline server for sched_ext tasks Christian Loehle
2025-11-05 13:47 ` Andrea Righi
2025-11-05 13:59   ` Peter Zijlstra
2025-11-05 14:20     ` Juri Lelli
2025-11-05 14:39       ` Andrea Righi

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=20251029191111.167537-5-arighi@nvidia.com \
    --to=arighi@nvidia.com \
    --cc=bpf@vger.kernel.org \
    --cc=bsegall@google.com \
    --cc=changwoo@igalia.com \
    --cc=christian.loehle@arm.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=emil@etsalapatis.com \
    --cc=joelagnelf@nvidia.com \
    --cc=juri.lelli@redhat.com \
    --cc=ldematteis123@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=sched-ext@lists.linux.dev \
    --cc=shuah@kernel.org \
    --cc=tj@kernel.org \
    --cc=vincent.guittot@linaro.org \
    --cc=void@manifault.com \
    --cc=vschneid@redhat.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox