Re: [PATCH v3 10/17] sched/deadline: Fix an overflow of the 0-lag time with long periods

From: sashiko-bot

Date: Thu Nov 19 2026 - 09:07:32 EST


Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] sched/deadline: divide error and overflow in
task_non_contending()
--

commit 8f3d9963b4f77fcaacfd297c2fa7927bf18777cd
Author: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>

sched/deadline: Fix an overflow of the 0-lag time with long periods

This commit replaces div64_long() in task_non_contending() with
mul_u64_u64_div_u64() so that computing the 0-lag time does not overflow
64-bit multiplication on long deadline periods.

> diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
> index d6d2657dcddd6..86297ef5ac076 100644
> --- a/kernel/sched/deadline.c
> +++ b/kernel/sched/deadline.c
> @@ -425,9 +425,20 @@ static void task_non_contending(struct sched_dl_entity *dl_se, bool dl_task)
>
> WARN_ON(dl_se->dl_non_contending);
>
> - zerolag_time = dl_se->deadline -
> - div64_long((dl_se->runtime * dl_se->dl_period),
> - dl_se->dl_runtime);
> + /*
> + * The 0-lag time is the deadline minus the runtime left, scaled by
> + * dl_period / dl_runtime. The product of the runtime and dl_period
> + * can overflow s64 with periods of a few seconds, so multiply on 128
> + * bits. The runtime is negative after an overrun.
> + */
> + if (dl_se->runtime >= 0)
> + zerolag_time = dl_se->deadline -
> + mul_u64_u64_div_u64(dl_se->runtime, dl_se->dl_period,
> + dl_se->dl_runtime);
> + else
> + zerolag_time = dl_se->deadline +
> + mul_u64_u64_div_u64(-dl_se->runtime, dl_se->dl_period,
> + dl_se->dl_runtime);

[Severity: Medium]
Can mul_u64_u64_div_u64() trap with a divide error (#DE) on x86-64 or
overflow s64 zerolag_time when |dl_se->runtime| exceeds dl_se->dl_runtime?

On x86-64, mul_u64_u64_div_u64() uses an unguarded divq instruction that
traps with #DE whenever the quotient does not fit in u64, and even when it
fits in u64, a quotient above S64_MAX wraps s64 zerolag_time into the wrong
sign.

|dl_se->runtime| is not bounded by dl_se->dl_runtime when root raises
sysctl_sched_dl_period_max: for example, if an active task with a 140s
runtime and period calls sched_setattr() to lower sched_runtime to 1024 ns
while dl_se->deadline is still in the future, __setparam_dl() updates
dl_se->dl_runtime to 1024 without clamping dl_se->runtime, and
enqueue_dl_entity() skips setup_new_dl_entity():

kernel/sched/deadline.c:enqueue_dl_entity() {
...
} else if ((flags & ENQUEUE_MOVE) &&
!is_dl_boosted(dl_se) &&
dl_time_before(dl_se->deadline, rq_clock(rq_of_dl_se(dl_se)))) {
setup_new_dl_entity(dl_se);
}
...
}

When the task then blocks or leaves SCHED_DEADLINE, task_non_contending()
divides 140e9 * 140e9 by 1024 (> U64_MAX) and panics in divq with rq->__lock
held (or in the else branch when a task with dl_runtime = 1024 ns and a
large dl_period overruns by a scheduler tick before blocking).

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