Re: [PATCH v13 05/36] sched/deadline: Keep a departing constrained task until its next period
From: sashiko-bot
Date: Tue Dec 01 2026 - 13:18:00 EST
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] sched/deadline: premature bw release in inactive_task_timer()
on pending race
- [Medium] sched/deadline: extra period wait when dl_task_timer()
precedes inactive timer
--
commit 013654a1a833237d3e6842ffbe9cffc69bd53725
Author: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
sched/deadline: Keep a departing constrained task until its next period
This commit retains the bandwidth and deadline of a constrained
SCHED_DEADLINE task that leaves the class until the later of its 0-lag
time and its next period, and adds dl_inactive_pending to coordinate with
an active inactive_task_timer() handler.
> diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
> index a02975eb91f63..cd7f2ada8cdf3 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;
> + }
[Severity: Medium]
Can a throttled constrained task that leaves SCHED_DEADLINE end up keeping
its bandwidth and deadline for an extra unused period?
When a constrained task is throttled, start_dl_timer() arms dl_se->dl_timer
for dl_next_period(dl_se). If the task switches out of SCHED_DEADLINE
before that period ends, task_non_contending() arms inactive_timer for at
least dl_next_period(dl_se), so dl_task_timer() expires at the period
boundary before inactive_task_timer() and replenishes the departed task:
kernel/sched/deadline.c:dl_task_timer() {
...
if (!dl_task(p)) {
if (dl_se->dl_throttled && !is_dl_boosted(dl_se)) {
sched_clock_tick();
update_rq_clock(rq);
replenish_dl_entity(dl_se);
}
goto unlock;
}
...
}
replenish_dl_entity() adds dl_period to dl_se->deadline and dl_runtime to
dl_se->runtime. That leaves dl_time_to_zerolag() unchanged, but shifts
dl_next_period(dl_se) forward by a full period. When inactive_task_timer()
then runs, it sees dl_next_period(dl_se) - rq_clock(rq) > 0 for the newly
replenished period and returns HRTIMER_RESTART.
The same happens here in task_non_contending() if a sleeping throttled
constrained task is replenished at the period boundary by dl_task_timer()
and then leaves SCHED_DEADLINE while still asleep before consuming any
runtime in the new period (so zerolag_time < 0, but next > 0). In both
cases, the task holds its bandwidth in total_bw for a second period in
which it never ran as SCHED_DEADLINE, and if it returns to the class after
the replenished dl_se->deadline in that second period,
dl_check_constrained_dl() throttles it until the period ends.
> /*
> * 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;
[ ... ]
> @@ -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;
> + }
> + }
[Severity: Medium]
Can inactive_task_timer() release a departing task's bandwidth before its
0-lag time when task_non_contending() takes the pending early return?
When pending is true for a departing task, task_non_contending() returns
early for both implicit and constrained tasks, relying on the running
inactive_task_timer() handler to wait until the required retention time.
However, inactive_task_timer() only checks dl_next_period(dl_se) -
rq_clock(rq) when !dl_is_implicit(dl_se), and never checks
dl_time_to_zerolag(dl_se, rq):
CPU0 CPU1
T blocks before 0-lag time
task_non_contending() arms timer
inactive_task_timer()
task_rq_lock(T) waits
T wakes up at/after 0-lag time
replenish_dl_new_period()
T runs and consumes runtime
sched_setattr(T, SCHED_OTHER)
switched_from_dl()
task_non_contending()
pending is true -> returns
acquires task_rq_lock(T)
__dl_sub() / __dl_clear_params() <-- releases bandwidth before new 0-lag
If T has an implicit deadline, !dl_is_implicit(dl_se) is false, so
inactive_task_timer() immediately subtracts T's bandwidth from total_bw and
clears its parameters even though its new 0-lag time is in the future. If
T has a constrained deadline and overran its runtime (dl_se->runtime < 0),
dl_time_to_zerolag(dl_se, rq) is later than dl_next_period(dl_se) -
rq_clock(rq), so inactive_task_timer() releases T's bandwidth at
dl_next_period(dl_se) before the overrun is paid back. In either case, T
can re-enter SCHED_DEADLINE and consume more than its reserved bandwidth.
In addition, unlike task_non_contending(), this check does not guard on
dl_se->deadline != 0 before calling dl_next_period(dl_se), which can be 0
if a sleeping task enters and leaves SCHED_DEADLINE with constrained
parameters while a previously restarted inactive_task_timer() is waiting
for task_rq_lock().
--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796146461.git.mathieu.desnoyers@xxxxxxxxxxxx?part=5