Re: [PATCH v11 26/43] sched/deadline: Keep a departing constrained task until its next period

From: sashiko-bot

Date: Wed Nov 25 2026 - 20:44:11 EST


Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] sched/deadline: dl_task_timer() advances period before
inactive_task_timer()
- [Medium] sched/deadline: HRTIMER_RESTART in inactive_task_timer()
races with cancel
--

commit 43e71343c7049144c9877a7ba7fddb15005d58e2
Author: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>

sched/deadline: Keep a departing constrained task until its next period

This commit keeps a departing constrained SCHED_DEADLINE task's bandwidth
and deadline until the later of its 0-lag time and its next period in
task_non_contending() and inactive_task_timer().

> diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
> index 1d2dcf478f95d..11774b3b9fe93 100644
> --- a/kernel/sched/deadline.c
> +++ b/kernel/sched/deadline.c

[ ... ]

> @@ -2598,6 +2614,25 @@ 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);

[Severity: Medium]
Can dl_task_timer() advance dl_se->deadline before inactive_task_timer()
computes dl_next_period(dl_se), holding the departed task's bandwidth for
an extra period?

When a constrained task is throttled before leaving SCHED_DEADLINE,
dl_se->dl_timer remains armed for the end of the departure period while
dl_se->inactive_timer is armed for max(0-lag time, dl_next_period(dl_se)).
When dl_task_timer() fires at the period boundary while !dl_task(p), it
replenishes the entity:

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_se->dl_period to dl_se->deadline until
dl_se->runtime > 0. When inactive_task_timer() then runs at the period
boundary (or later at the 0-lag time after an overrun), dl_next_period()
is computed from the replenished dl_se->deadline, so left > 0 and
inactive_task_timer() restarts for another full period instead of
releasing the task's bandwidth and clearing its parameters. If the task
re-enters SCHED_DEADLINE during that extra period after the advanced
deadline, dl_check_constrained_dl() also throttles it until the end of the
second period.

> +
> + if (left > 0) {
> + ktime_t at = ktime_add_ns(hrtimer_cb_get_time(timer), left);
> +
> + hrtimer_set_expires(timer, at);
> + task_rq_unlock(rq, p, &rf);
> + return HRTIMER_RESTART;
> + }
> + }

[Severity: Medium]
Can returning HRTIMER_RESTART after task_rq_unlock() race with
cancel_inactive_timer() in switched_to_dl() and leave inactive_timer
queued after the task returns to SCHED_DEADLINE?

cancel_dl_timer() assumes that when hrtimer_try_to_cancel() returns -1,
the running callback will finish with HRTIMER_NORESTART and drop the task
reference. Because __run_hrtimer() only re-acquires cpu_base->lock and
re-enqueues the timer after inactive_task_timer() returns, another CPU can
run switched_to_dl() in between:

CPU0 CPU1
__run_hrtimer()
inactive_task_timer()
hrtimer_set_expires(timer, at)
task_rq_unlock(rq, p, &rf)
__sched_setscheduler()
task_rq_lock(p, &rf)
switched_to_dl()
cancel_inactive_timer(&p->dl)
hrtimer_try_to_cancel() -> -1
task_rq_unlock(rq, p, &rf)
return HRTIMER_RESTART
raw_spin_lock(&cpu_base->lock)
enqueue_hrtimer(timer, base, ...) <-- queued while p is in DL

p is then back in SCHED_DEADLINE with dl_non_contending == 0 while
hrtimer_active(&p->dl.inactive_timer) is true. If p blocks or leaves
SCHED_DEADLINE before that stale timer fires, task_non_contending() sees
hrtimer_active(&dl_se->inactive_timer) and immediately subtracts
running_bw or releases the task's bandwidth and clears its deadline before
the 0-lag time.

--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795646914.git.mathieu.desnoyers@xxxxxxxxxxxx?part=26