mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
To: juri.lelli@redhat.com, peterz@infradead.org, mingo@redhat.com,
	vincent.guittot@linaro.org
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	tglx@kernel.org, will@kernel.org, boqun@kernel.org,
	dietmar.eggemann@arm.com, rostedt@goodmis.org,
	bsegall@google.com, mgorman@suse.de, vschneid@redhat.com,
	kprateek.nayak@amd.com, luca.abeni@santannapisa.it,
	tommaso.cucinotta@gmail.com, mathieu.poirier@linaro.org,
	longman@redhat.com, ridong.chen@linux.dev, tj@kernel.org,
	hannes@cmpxchg.org, mkoutny@suse.com, shuah@kernel.org,
	bigeasy@linutronix.de, clrkwllms@kernel.org,
	parri.andrea@gmail.com, m.szyprowski@samsung.com,
	kayracizmeci@gmail.com, weihengqiuu@gmail.com,
	linux-kernel@vger.kernel.org, cgroups@vger.kernel.org,
	linux-kselftest@vger.kernel.org, linux-rt-devel@lists.linux.dev,
	stable@vger.kernel.org
Subject: [PATCH v11 25/43] sched/deadline: Count the 0-lag time from the next period
Date: Wed, 25 Nov 2026 20:08:24 -0500	[thread overview]
Message-ID: <1d535c0c45a21d94ebadfce2883ce8d8b6789c44.1795646914.git.mathieu.desnoyers@efficios.com> (raw)
In-Reply-To: <cover.1795646914.git.mathieu.desnoyers@efficios.com>

A deadline task that blocks keeps counting in running_bw until its
0-lag time. Commit 209a0cbda7a0 ("sched/deadline: Improve the tracking
of active utilization") added this for GRUB. A task that leaves the
class or exits also keeps its bandwidth in total_bw until then.
Commit 387e31300b57 ("sched/deadline: Fix the update of the
total -deadline utilization") added that.

The 0-lag time is when the task, served at its bandwidth since the
start of its period, has received the runtime it used. That is:

  next period - runtime * dl_period / dl_runtime

Here runtime is the runtime left. GRUB [1] has the same formula (rule 5
of section 4.1), with the deadline in place of the next period. In
GRUB, the deadline is always the end of the period (rules 1 and 3). For
a constrained deadline, rule R6 of [2] puts the end of the period at
deadline - dl_deadline + dl_period. This is what dl_next_period()
returns.

dl_time_to_zerolag() uses the deadline in place of the next period. The
two are the same only for an implicit deadline. A constrained deadline
comes dl_period - dl_deadline before the next period. So the 0-lag time
comes that much too early.

For example, take a task with a runtime of 10ms, a deadline of 20ms and
a period of 500ms. It runs 2ms into its period, then sleeps with 8ms
left. dl_time_to_zerolag() puts its 0-lag time at
20 - 8 * 500 / 10 = -380ms, before the task even blocked. Counted from
the next period, it is 500 - 400 = 100ms.

On v7.4-rc2, this would affect a constrained task:
- When it blocks, GRUB reclaiming would give its bandwidth to
  SCHED_FLAG_RECLAIM tasks too early. Schedutil could lower the CPU
  frequency too early. Deadline tasks could then miss their deadlines.
- When it exits or leaves the class, admission control could admit
  other tasks into a temporary overload.
This was found by reading.

Fix this by counting the 0-lag time from the next period, with
dl_next_period(). Update the formula in
Documentation/scheduler/sched-deadline.rst too.

This needs "sched/deadline: Fix an overflow of the 0-lag time with long
periods", which adds dl_time_to_zerolag().

A task that leaves the class and comes back after its 0-lag time
starts a new period. "sched/deadline: Reset the deadline and the
runtime with the parameters" does this. So a constrained task can come
back after the early 0-lag time, before its next period. It then runs
again before that next period. Three cases of deadline_bw_test check
this: reenter_runnable_in_gap, reenter_asleep_in_gap and
reenter_pi_blocked_in_gap. "selftests/sched: Test parameter changes in
deadline_bw_test", later in this series, adds them. They fail without
this patch and pass from it on.

[1] L. Abeni, G. Lipari, A. Parri, Y. Sun, "Parallel and sequential
    reclaiming in multicore real-time global scheduling", arXiv
    1512.01984, 2016, https://arxiv.org/abs/1512.01984
[2] D. Casini, L. Abeni, A. Biondi, T. Cucinotta, G. Buttazzo,
    "Constant Bandwidth Servers with Constrained Deadlines", RTNS 2017,
    https://retis.santannapisa.it/~a.biondi/papers/CBS_CD_RTNS17.pdf

Fixes: 209a0cbda7a0 ("sched/deadline: Improve the tracking of active utilization")
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: stable@vger.kernel.org
---
 Documentation/scheduler/sched-deadline.rst | 12 +++++-----
 kernel/sched/deadline.c                    | 26 +++++++++++++---------
 2 files changed, 22 insertions(+), 16 deletions(-)

diff --git a/Documentation/scheduler/sched-deadline.rst b/Documentation/scheduler/sched-deadline.rst
index 9019b66f6a5b..3529b667c07f 100644
--- a/Documentation/scheduler/sched-deadline.rst
+++ b/Documentation/scheduler/sched-deadline.rst
@@ -171,12 +171,14 @@ Deadline Task Scheduling
       The 0-lag time for a task entering the ActiveNonContending state is
       computed as::
 
-                        (runtime * dl_period)
-             deadline - ---------------------
-                             dl_runtime
+                           (runtime * dl_period)
+             next period - ---------------------
+                                dl_runtime
 
-      where runtime is the remaining runtime, while dl_runtime and dl_period
-      are the reservation parameters.
+      where runtime is the remaining runtime, next period is the start of
+      the task's next period, and dl_runtime and dl_period are the
+      reservation parameters. When dl_deadline equals dl_period, the next
+      period starts at the deadline.
 
   (b) If the task wakes up before the inactive timer fires, the task re-enters
       the ActiveContending state and the "inactive timer" is canceled.
diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
index 74b3c6b967ee..1d2dcf478f95 100644
--- a/kernel/sched/deadline.c
+++ b/kernel/sched/deadline.c
@@ -607,6 +607,7 @@ static void dl_change_utilization(struct task_struct *p, u64 new_bw)
 	dl_rq_change_utilization(task_rq(p), &p->dl, new_bw);
 }
 
+static inline u64 dl_next_period(struct sched_dl_entity *dl_se);
 static void __dl_clear_params(struct sched_dl_entity *dl_se);
 static unsigned int dl_task_root_domain_cpu(struct task_struct *p);
 
@@ -614,14 +615,17 @@ static unsigned int dl_task_root_domain_cpu(struct task_struct *p);
  * Return the time from now to the 0-lag time of @dl_se, or a negative
  * value if the 0-lag time has passed.
  *
- * The 0-lag time is when a task running at its reserved bandwidth would
- * use up the runtime it has left:
+ * The 0-lag time is when the task, served at its reserved bandwidth
+ * since the start of its period, has received the runtime it used. From
+ * there to the next period, the bandwidth gives exactly the runtime left:
  *
- *   0-lag = deadline - runtime * dl_period / dl_runtime
+ *   0-lag = next period - runtime * dl_period / dl_runtime
  *
  * Example: 30ms every 100ms, blocking 10ms into its period with 20ms
- * left. 0-lag = 100 - 20 * 100 / 30 = 33ms. From there to the deadline,
- * 30% of the CPU is exactly the 20ms left.
+ * left. 0-lag = 100 - 20 * 100 / 30 = 33ms.
+ *
+ * Do not count from the deadline. A constrained deadline comes before
+ * the next period, so the 0-lag time would come too early.
  *
  * The product can overflow 64 bits, so mul_u64_u64_div_u64() multiplies
  * on 128 bits. Its quotient must still fit: on x86-64 it traps
@@ -633,12 +637,12 @@ static unsigned int dl_task_root_domain_cpu(struct task_struct *p);
  */
 static s64 dl_time_to_zerolag(struct sched_dl_entity *dl_se, struct rq *rq)
 {
-	/* Time to the deadline. Negative if the deadline has passed. */
-	s64 left = dl_se->deadline - rq_clock(rq);
+	/* Time to the next period. Negative if it has started. */
+	s64 left = dl_next_period(dl_se) - rq_clock(rq);
 
 	if (dl_se->runtime < 0) {
 		/*
-		 * Overrun: the 0-lag time is after the deadline, by the time
+		 * Overrun: the 0-lag time is after the next period, by the time
 		 * needed to pay the overrun back at the reserved bandwidth.
 		 */
 		u64 overrun = -dl_se->runtime;
@@ -659,8 +663,8 @@ static s64 dl_time_to_zerolag(struct sched_dl_entity *dl_se, struct rq *rq)
 	}
 
 	/*
-	 * Runtime left: the 0-lag time is before the deadline. It has
-	 * passed if the deadline has.
+	 * Runtime left: the 0-lag time is before the next period. It has
+	 * passed if the next period has started.
 	 */
 	if (left < 0)
 		return -1;
@@ -668,7 +672,7 @@ static s64 dl_time_to_zerolag(struct sched_dl_entity *dl_se, struct rq *rq)
 	/*
 	 * runtime * dl_period / dl_runtime > left: the 0-lag time has
 	 * passed. The runtime left is more than the bandwidth gives until
-	 * the deadline, as a wakeup would find with dl_entity_overflow().
+	 * the next period.
 	 */
 	if (dl_se->runtime > mul_u64_u64_div_u64(left, dl_se->dl_runtime,
 						 dl_se->dl_period)) {
-- 
2.47.3


  parent reply	other threads:[~2026-11-26  1:09 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  1:07 [PATCH v11 00/43] sched/deadline: bandwidth accounting fixes and selftests Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 01/43] sched/deadline: Keep departing bandwidth across a root domain rebuild Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 02/43] sched/deadline: Rebuild the root domains' accounting from the task list Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 03/43] sched/deadline: Record the root domain holding a task's bandwidth Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 04/43] sched/deadline: Leave a dying task out of a rebuild of the accounting Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 05/43] selftests/sched: Test SCHED_DEADLINE bandwidth across a root domain rebuild Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 06/43] selftests/sched: Test the root domain rebuild in deadline_bw_test Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 07/43] sched/deadline: Move a task's bandwidth only where room was made Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 08/43] cgroup/cpuset: Wait for an attach before rebuilding the sched domains Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 09/43] sched: Make deadline policy and affinity changes wait for a cpuset attach Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 10/43] cgroup/cpuset: Give back the bandwidth an attach did not move Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 11/43] cgroup/cpuset: Reserve the bandwidth in the root domain a task moves to Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 12/43] selftests/sched: Test cpuset moves in deadline_bw_test Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 13/43] sched/deadline: Make a rebuild count a task in the root domain it runs in Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 14/43] sched/deadline: Move a task's bandwidth to " Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 15/43] sched/deadline: Keep a task in the root domain that counts its bandwidth Mathieu Desnoyers
2026-11-26  1:38   ` sashiko-bot
2026-11-26  1:08 ` [PATCH v11 16/43] sched/deadline: Keep the last CPU a task has in its root domain Mathieu Desnoyers
2026-11-26  1:35   ` sashiko-bot
2026-11-26  1:08 ` [PATCH v11 17/43] cgroup/cpuset: Refuse to take a deadline task out of " Mathieu Desnoyers
2026-11-26  1:41   ` sashiko-bot
2026-11-26  1:08 ` [PATCH v11 18/43] selftests/sched: Test the root domain of a task in deadline_bw_test Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 19/43] sched/deadline: Count only active CPUs when spreading extra_bw Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 20/43] sched/deadline: Do not truncate a bandwidth to 32 bits in __dl_add/sub() Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 21/43] rtmutex: Continue the chain walk when a waiter's deadline donor changes Mathieu Desnoyers
2026-11-26  1:32   ` sashiko-bot
2026-11-26  1:08 ` [PATCH v11 22/43] rtmutex: Do not stop the chain walk at an owner whose top waiter changed Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 23/43] sched/deadline: Hold a reference on the deadline donor of a boosted task Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 24/43] selftests/sched: Test the rtmutex chain walk in deadline_bw_test Mathieu Desnoyers
2026-11-26  1:08 ` Mathieu Desnoyers [this message]
2026-11-26  1:35   ` [PATCH v11 25/43] sched/deadline: Count the 0-lag time from the next period sashiko-bot
2026-11-26  1:08 ` [PATCH v11 26/43] sched/deadline: Keep a departing constrained task until its " Mathieu Desnoyers
2026-11-26  1:44   ` sashiko-bot
2026-11-26  1:08 ` [PATCH v11 27/43] sched/deadline: Replenish a throttled task boosted while asleep Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 28/43] sched/deadline: Restore the owner's period at a deboost in the class Mathieu Desnoyers
2026-11-26  1:57   ` sashiko-bot
2026-11-26  1:08 ` [PATCH v11 29/43] sched/deadline: Count the bandwidth a boosted task is given Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 30/43] sched/deadline: Start a period for a task that enters the class blocked Mathieu Desnoyers
2026-11-26  1:53   ` sashiko-bot
2026-11-26  1:08 ` [PATCH v11 31/43] sched/deadline: Restore the owner's period at a deboost out of the class Mathieu Desnoyers
2026-11-26  1:50   ` sashiko-bot
2026-11-26  1:08 ` [PATCH v11 32/43] sched/deadline: Start a period for a task requeued onto an rt_mutex Mathieu Desnoyers
2026-11-26  1:57   ` sashiko-bot
2026-11-26  1:08 ` [PATCH v11 33/43] selftests/sched: Test deadline boosts in deadline_bw_test Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 34/43] sched/deadline: Cancel the replenishment timer with the parameters Mathieu Desnoyers
2026-11-26  1:45   ` sashiko-bot
2026-11-26  1:08 ` [PATCH v11 35/43] sched/deadline: Cap the runtime left of a constrained task at dl_runtime Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 36/43] sched/deadline: Check the runtime of a task that comes back runnable Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 37/43] sched/deadline: Keep the debt of a task across a change of parameters Mathieu Desnoyers
2026-11-26  2:16   ` sashiko-bot
2026-11-26  1:08 ` [PATCH v11 38/43] sched/deadline: Boost an owner that cannot run on its own reservation Mathieu Desnoyers
2026-11-26  2:17   ` sashiko-bot
2026-11-26  1:08 ` [PATCH v11 39/43] sched/deadline: Boost an owner throttled after its waiter blocked Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 40/43] sched/deadline: Reschedule a running task throttled at its enqueue Mathieu Desnoyers
2026-11-26  1:08 ` [PATCH v11 41/43] sched/deadline: Run a boost held at its first refill on a donor period Mathieu Desnoyers
2026-11-26  2:02   ` sashiko-bot
2026-11-26  1:08 ` [PATCH v11 42/43] selftests/sched: Test parameter changes in deadline_bw_test Mathieu Desnoyers
2026-11-26  2:01   ` sashiko-bot
2026-11-26  1:08 ` [PATCH v11 43/43] selftests/sched: Share the helpers of the deadline tests Mathieu Desnoyers

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=1d535c0c45a21d94ebadfce2883ce8d8b6789c44.1795646914.git.mathieu.desnoyers@efficios.com \
    --to=mathieu.desnoyers@efficios.com \
    --cc=bigeasy@linutronix.de \
    --cc=boqun@kernel.org \
    --cc=bsegall@google.com \
    --cc=cgroups@vger.kernel.org \
    --cc=clrkwllms@kernel.org \
    --cc=dietmar.eggemann@arm.com \
    --cc=hannes@cmpxchg.org \
    --cc=juri.lelli@redhat.com \
    --cc=kayracizmeci@gmail.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-rt-devel@lists.linux.dev \
    --cc=longman@redhat.com \
    --cc=luca.abeni@santannapisa.it \
    --cc=m.szyprowski@samsung.com \
    --cc=mathieu.poirier@linaro.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=mkoutny@suse.com \
    --cc=parri.andrea@gmail.com \
    --cc=peterz@infradead.org \
    --cc=ridong.chen@linux.dev \
    --cc=rostedt@goodmis.org \
    --cc=shuah@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=tglx@kernel.org \
    --cc=tj@kernel.org \
    --cc=tommaso.cucinotta@gmail.com \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.com \
    --cc=weihengqiuu@gmail.com \
    --cc=will@kernel.org \
    /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

all inboxes | Powered by JetHome®