[PATCH v11 25/43] sched/deadline: Count the 0-lag time from the next period
From: Mathieu Desnoyers
Date: Wed Nov 25 2026 - 20:12:21 EST
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@xxxxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
---
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