[PATCH v14 31/39] sched/deadline: Record the start of the period
From: Mathieu Desnoyers
Date: Thu Dec 03 2026 - 10:02:21 EST
dl_next_period() computes the next period from the deadline:
deadline - dl_deadline + dl_period. After sched_setattr() changes
dl_deadline, this subtracts the new dl_deadline from a deadline set
with the old one. The result is a wrong next period.
The next patch fits the period in progress to new parameters at
sched_setattr(). To do that, it needs the start of the period. It also
makes dl_next_period() count from that start.
Add the field period_start. Set it wherever a period starts. Clear it
with the deadline. dl_release_params() keeps the deadline of a
constrained task: keep period_start there too. Nothing reads it yet:
no functional change.
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
---
include/linux/sched.h | 1 +
kernel/sched/deadline.c | 17 ++++++++++++++---
2 files changed, 15 insertions(+), 3 deletions(-)
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 69a9a9e5cc80..32ae977a2821 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -661,6 +661,7 @@ struct sched_dl_entity {
*/
s64 runtime; /* Remaining runtime for this instance */
u64 deadline; /* Absolute deadline for this instance */
+ u64 period_start; /* Absolute start of this instance */
unsigned int flags; /* Specifying the scheduler behaviour */
/*
diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
index 58fc1c6c4117..820e71ee7fdf 100644
--- a/kernel/sched/deadline.c
+++ b/kernel/sched/deadline.c
@@ -1121,7 +1121,8 @@ static inline void replenish_dl_new_period(struct sched_dl_entity *dl_se,
struct rq *rq)
{
/* for non-boosted task, pi_of(dl_se) == dl_se */
- dl_se->deadline = rq_clock(rq) + pi_of(dl_se)->dl_deadline;
+ dl_se->period_start = rq_clock(rq);
+ dl_se->deadline = dl_se->period_start + pi_of(dl_se)->dl_deadline;
dl_se->runtime = pi_of(dl_se)->dl_runtime;
/*
@@ -1220,6 +1221,7 @@ static void replenish_dl_entity(struct sched_dl_entity *dl_se)
WARN_ON_ONCE(!is_dl_boosted(dl_se));
if (!dl_se->dl_deadline) {
dl_se->deadline = rq_clock(rq);
+ dl_se->period_start = dl_se->deadline;
dl_se->runtime = 0;
}
if (dl_se->runtime < 0 || dl_se->dl_yielded)
@@ -1241,7 +1243,8 @@ static void replenish_dl_entity(struct sched_dl_entity *dl_se)
*/
if (dl_se->dl_deadline == 0 ||
(dl_se->dl_defer_armed && dl_entity_overflow(dl_se, rq_clock(rq)))) {
- dl_se->deadline = rq_clock(rq) + deadline;
+ dl_se->period_start = rq_clock(rq);
+ dl_se->deadline = dl_se->period_start + deadline;
dl_se->runtime = runtime;
}
@@ -1256,6 +1259,7 @@ static void replenish_dl_entity(struct sched_dl_entity *dl_se)
*/
while (dl_se->runtime <= 0) {
dl_se->deadline += period;
+ dl_se->period_start += period;
dl_se->runtime += runtime;
}
@@ -2398,6 +2402,7 @@ int dl_server_apply_params(struct sched_dl_entity *dl_se, u64 runtime, u64 perio
dl_se->runtime = 0;
dl_se->deadline = 0;
+ dl_se->period_start = 0;
dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime);
dl_se->dl_density = to_ratio(dl_se->dl_deadline, dl_se->dl_runtime);
@@ -4675,6 +4680,7 @@ static void switched_from_dl(struct rq *rq, struct task_struct *p)
*/
if (!p->dl.dl_runtime) {
p->dl.deadline = 0;
+ p->dl.period_start = 0;
p->dl.runtime = 0;
}
@@ -5196,6 +5202,7 @@ static void __dl_clear_params(struct sched_dl_entity *dl_se)
dl_se->dl_density = 0;
dl_se->deadline = 0;
+ dl_se->period_start = 0;
dl_se->runtime = 0;
dl_se->dl_throttled = 0;
@@ -5224,12 +5231,16 @@ static void __dl_clear_params(struct sched_dl_entity *dl_se)
*/
static void dl_release_params(struct sched_dl_entity *dl_se)
{
+ u64 period_start = dl_se->period_start;
u64 deadline = dl_se->deadline;
bool constrained = !dl_is_implicit(dl_se);
__dl_clear_params(dl_se);
- if (constrained)
+ if (constrained) {
+ /* Keep the start of the period with its deadline. */
+ dl_se->period_start = period_start;
dl_se->deadline = deadline;
+ }
}
void init_dl_entity(struct sched_dl_entity *dl_se)
--
2.47.3