[PATCH v13 05/36] sched/deadline: Keep a departing constrained task until its next period
From: Mathieu Desnoyers
Date: Tue Dec 01 2026 - 12:46:05 EST
A task that leaves SCHED_DEADLINE keeps its bandwidth until its 0-lag
time, so that leaving and coming back gains nothing. Two patches
which this one builds on also clear its deadline at that time:
- "sched/deadline: Reset the deadline and the runtime with the
parameters" resets the deadline in __dl_clear_params().
- "sched/deadline: Release the bandwidth of a task that leaves the class
asleep" calls __dl_clear_params() for a task that leaves asleep with
no timer pending.
For an implicit deadline, this changes nothing. A wakeup after the
0-lag time starts a new period anyway. A constrained task is different.
Commit df8eac8cafce ("sched/deadline: Throttle a constrained deadline
task activated after the deadline") throttles a task that wakes up
after its deadline until its next period. Without this throttle, the
task could run for runtime / deadline, more than its bandwidth.
With a constrained deadline, the 0-lag time can come before the next
period. A task that is out of the class at any time between the two
loses its deadline. If it comes back before its next period, its
wakeup starts a new period at once. Repeated, this gives the task more
than its bandwidth. Other deadline tasks could then miss their
deadlines.
For example, take a task with a runtime of 10ms, a deadline of 20ms and
a period of 500ms. Its period starts at 0ms.
1. It runs 2ms, then sleeps with 8ms left. Its 0-lag time is
500 - 8 * 500 / 10 = 100ms.
2. At 150ms, another thread calls sched_setattr() to make it
SCHED_OTHER, then SCHED_DEADLINE again. Leaving the class releases
its bandwidth and clears its deadline.
3. At 155ms, it wakes up. Its wakeup starts a new period at once, not
at 500ms. Between 0ms and 500ms, it can run 12ms, not 10ms.
Fix this by keeping the bandwidth and the deadline of a constrained
task that leaves the class until its next period. task_non_contending()
and inactive_task_timer() wait for the later of the 0-lag time and the
next period. In the example, the task keeps its deadline at 150ms. Its
wakeup throttles it until 500ms. "sched/deadline: Reset the deadline and
the runtime with the parameters" calls such a throttle a symptom. There,
the task had released its bandwidth at its 0-lag time. Here, it keeps
its bandwidth until its next period.
The proof of Lemma 5.1 in [1] uses the same rule for its H-CBSD-W
server.
Two timer cases need more care. First, inactive_task_timer() now
restarts itself until the next period. It decides this under the rq
lock, then drops the lock and returns HRTIMER_RESTART. If the task
comes back in between, switched_to_dl() cannot cancel the timer. The
timer is then queued again with the task back in the class. The next
task_non_contending() now cancels it first. If the timer fires before
that, its handler now leaves the task alone when dl_non_contending is
clear. Without this check, a handler that fires while the task exits
would clear its parameters while the task is still queued. Its last
dequeue would then leave its bandwidth in running_bw and this_bw.
Second, the task can leave the class while the handler waits for the
rq lock. T is a constrained deadline task on CPU 1. W and S are other
threads. They take the rq lock of CPU 1.
CPU 0 CPU 1
T blocks before its 0-lag time
task_non_contending() arms the timer
inactive_task_timer() runs
task_rq_lock(T) waits
W wakes T up
task_contending() cannot cancel
S: sched_setattr(T, SCHED_OTHER)
switched_from_dl()
task_non_contending()
cannot cancel
hrtimer_active() is true
releases the bandwidth now
Since commit 1b02cd6a2d7f ("sched/deadline: Correctly handle active
0-lag timers"), task_non_contending() acts as if the 0-lag time had
passed when the timer is active. For a task that blocks, this drops
its active utilization at once, as "the safest thing to do". For a
departing task, this releases its bandwidth and clears its deadline
before its next period.
hrtimer_try_to_cancel() fails from the expiry until the handler
returns. In that window, the handler is in one of three states:
- It waits for the rq lock. It will release the bandwidth or restart
the timer.
- It has dropped the rq lock and will restart the timer.
- It has done its work and dropped the rq lock.
In the first two states, the handler still acts on the task. So
task_non_contending() must not release the bandwidth, nor arm the
timer again. In the third state, nothing acts on the task any more,
unless task_non_contending() arms the timer again.
A new flag, dl_inactive_pending, tells the first two states from the
third. task_non_contending() sets it when it arms the timer. The handler
clears it when it ends, unless it restarts the timer. When
task_non_contending() cannot cancel the timer of a departing task:
- With the flag set, it returns. The handler will release the
bandwidth, or restart the timer.
- With the flag clear, it goes on as if no timer were armed. It arms
the timer, or releases the bandwidth at once if the 0-lag time and
the next period have both passed.
A task that blocks in the class keeps the rule of 1b02cd6a2d7f.
The Sashiko AI review of this series found these timer cases.
The cost: a constrained task that leaves the class keeps its bandwidth
in total_bw up to one period longer. Until then, sched_setattr() can
fail with EBUSY for a new deadline task.
The two patches named at the top carry Cc: stable. A backport of them
needs this patch, and "sched/deadline: Count the 0-lag time from the
next period" before it. Without either one, a constrained task that
leaves the class and comes back can get more than its bandwidth.
"selftests/sched: Test parameter changes in deadline_bw_test", later in
this series, adds the reenter_asleep_late_in_gap case. It runs the
example above. The case fails without this patch, and passes with it.
[1] 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
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
---
include/linux/sched.h | 10 +++++
kernel/sched/deadline.c | 91 +++++++++++++++++++++++++++++++++++++++--
2 files changed, 98 insertions(+), 3 deletions(-)
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 88cda9c10cb8..79e02494dcca 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -680,6 +680,15 @@ struct sched_dl_entity {
* conditions between the inactive timer handler and the wakeup
* code.
*
+ * @dl_inactive_pending tells that the handler of the inactive timer
+ * still has to act on the task. task_non_contending() sets it when
+ * it arms the timer. The handler clears it when it ends, unless it
+ * restarts the timer. A cancel leaves it set: task_non_contending()
+ * reads it only when it cannot cancel the timer.
+ * It is a bool at the end of the struct, not a bit field:
+ * check_dl_overrun() clears @dl_overrun without the rq lock, which
+ * can undo a change to another bit of the same word.
+ *
* @dl_overrun tells if the task asked to be informed about runtime
* overruns.
*
@@ -756,6 +765,7 @@ struct sched_dl_entity {
*/
struct sched_dl_entity *pi_se;
#endif
+ bool dl_inactive_pending;
};
#ifdef CONFIG_UCLAMP_TASK
diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
index a02975eb91f6..cd7f2ada8cdf 100644
--- a/kernel/sched/deadline.c
+++ b/kernel/sched/deadline.c
@@ -515,6 +515,7 @@ static void task_non_contending(struct sched_dl_entity *dl_se, bool dl_task)
struct rq *rq = rq_of_dl_se(dl_se);
struct dl_rq *dl_rq = &rq->dl;
s64 zerolag_time;
+ bool running, pending;
/*
* If this is a non-deadline task that has been boosted,
@@ -528,13 +529,50 @@ static void task_non_contending(struct sched_dl_entity *dl_se, bool dl_task)
WARN_ON(dl_se->dl_non_contending);
+ /*
+ * inactive_task_timer() restarts itself for a constrained task that
+ * has left the class. If the task came back while the handler ran,
+ * switched_to_dl() could not cancel the timer, which may be queued
+ * again. Cancel it here. A running handler cannot be canceled.
+ * dl_inactive_pending then tells whether it will still act on the
+ * task, once it gets the rq lock or after its restart.
+ */
+ if (dl_server(dl_se)) {
+ running = hrtimer_active(timer);
+ pending = running;
+ } else {
+ int ret = hrtimer_try_to_cancel(timer);
+
+ if (ret == 1)
+ put_task_struct(dl_task_of(dl_se));
+ running = ret < 0;
+ pending = running && dl_se->dl_inactive_pending;
+ }
+
zerolag_time = dl_time_to_zerolag(dl_se, rq);
+ /*
+ * A constrained task that leaves the class keeps its bandwidth and its
+ * deadline until its next period, if that comes after its 0-lag time.
+ * Back in the class before then, it goes on with its current period,
+ * as if it had stayed. A new period at once would give it more than
+ * its runtime in this period. A dying task does not come back, so it
+ * releases its bandwidth at its 0-lag time.
+ */
+ if (!dl_task && !dl_server(dl_se) && !dl_is_implicit(dl_se) &&
+ dl_se->deadline &&
+ READ_ONCE(dl_task_of(dl_se)->__state) != TASK_DEAD) {
+ s64 next = dl_next_period(dl_se) - rq_clock(rq);
+
+ if (next > zerolag_time)
+ zerolag_time = next;
+ }
+
/*
* If the "0-lag time" already passed, decrease the active
* utilization now, instead of starting a timer
*/
- if ((zerolag_time < 0) || hrtimer_active(&dl_se->inactive_timer)) {
+ if ((zerolag_time < 0) || (running && (dl_task || pending))) {
if (dl_server(dl_se)) {
sub_running_bw(dl_se, dl_rq);
} else {
@@ -543,6 +581,14 @@ static void task_non_contending(struct sched_dl_entity *dl_se, bool dl_task)
if (dl_task)
sub_running_bw(dl_se, dl_rq);
+ /*
+ * The pending handler of a task that has left the
+ * class releases its bandwidth, or restarts for its
+ * next period.
+ */
+ if (pending && !dl_task &&
+ READ_ONCE(p->__state) != TASK_DEAD)
+ return;
if (!dl_task || READ_ONCE(p->__state) == TASK_DEAD) {
struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
@@ -559,8 +605,10 @@ static void task_non_contending(struct sched_dl_entity *dl_se, bool dl_task)
}
dl_se->dl_non_contending = 1;
- if (!dl_server(dl_se))
+ if (!dl_server(dl_se)) {
get_task_struct(dl_task_of(dl_se));
+ dl_se->dl_inactive_pending = 1;
+ }
hrtimer_start(timer, ns_to_ktime(zerolag_time), HRTIMER_MODE_REL_HARD);
}
@@ -2321,6 +2369,40 @@ static enum hrtimer_restart inactive_task_timer(struct hrtimer *timer)
if (dl_server(dl_se))
goto no_task;
+ /*
+ * A constrained task which has left the class keeps its bandwidth
+ * and its deadline until its next period, as in
+ * task_non_contending(). This timer may have been armed for the
+ * 0-lag time while the task was still in the class.
+ */
+ if (!dl_task(p) && READ_ONCE(p->__state) != TASK_DEAD &&
+ !dl_is_implicit(dl_se)) {
+ s64 left = dl_next_period(dl_se) - rq_clock(rq);
+
+ if (left > 0) {
+ ktime_t at = ktime_add_ns(hrtimer_cb_get_time(timer), left);
+
+ /*
+ * The timer is not queued: while this handler runs,
+ * task_non_contending() finds dl_inactive_pending set
+ * and does not arm it.
+ */
+ WARN_ON_ONCE(hrtimer_is_queued(timer));
+ hrtimer_set_expires(timer, at);
+ task_rq_unlock(rq, p, &rf);
+ return HRTIMER_RESTART;
+ }
+ }
+
+ /*
+ * A task back in the class, with dl_non_contending clear, can still
+ * have a timer that restarted while the task was out of it. Leave
+ * such a task alone, even if it is dying. Its last dequeue needs its
+ * parameters to remove its bandwidth.
+ */
+ if (dl_task(p) && !dl_se->dl_non_contending)
+ goto unlock;
+
if (!dl_task(p) || READ_ONCE(p->__state) == TASK_DEAD) {
struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
@@ -2347,6 +2429,7 @@ static enum hrtimer_restart inactive_task_timer(struct hrtimer *timer)
unlock:
if (!dl_server(dl_se)) {
+ dl_se->dl_inactive_pending = 0;
task_rq_unlock(rq, p, &rf);
put_task_struct(p);
} else {
@@ -3674,7 +3757,8 @@ static void switched_from_dl(struct rq *rq, struct task_struct *p)
* the "inactive timer" fires, it can continue to consume its current
* runtime using its current deadline. If it stays outside of
* SCHED_DEADLINE until the 0-lag time passes, inactive_task_timer()
- * will reset the task parameters.
+ * will reset the task parameters. For a constrained task, the reset
+ * waits for its next period, if that is later.
*/
if (task_on_rq_queued(p) && p->dl.dl_runtime)
task_non_contending(&p->dl, false);
@@ -4152,6 +4236,7 @@ void init_dl_entity(struct sched_dl_entity *dl_se)
*/
dl_se->pi_se = dl_se;
#endif
+ dl_se->dl_inactive_pending = 0;
}
bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr)
--
2.47.3