Re: [PATCH v12 20/43] sched/deadline: Do not truncate a bandwidth to 32 bits in __dl_add/sub()
From: David Laight
Date: Mon Nov 30 2026 - 09:12:34 EST
On Fri, 27 Nov 2026 09:35:47 -0500
Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx> wrote:
> __dl_add() and __dl_sub() spread a change of total_bw over the extra_bw
> of the active CPUs of the root domain. Commit daec57983670
> ("sched/deadline: Reclaim bandwidth not used by dl tasks") added this,
> for the bandwidth of one task. That is at most 1 << 20, the bandwidth
> of one CPU.
>
> But cpuset_can_attach() reserves the sum of the bandwidths of all the
> deadline tasks it moves, with dl_bw_alloc(). __dl_add() and __dl_sub()
> cast the bandwidth to s32 before the division. Past S32_MAX, more than
> 2048 CPUs of deadline bandwidth, the cast flips its sign. The extra_bw
> of each CPU then moves the wrong way. GRUB lets reclaiming tasks run
> more than the free bandwidth, or less. This was found by reading. It
> needs a machine with more than 2048 CPUs, so no case tests it.
>
> Fix this by dividing on 64 bits, with div_u64().
>
> The Sashiko AI review of the first series found this.
>
> Fixes: daec57983670 ("sched/deadline: Reclaim bandwidth not used by dl tasks")
> Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
> Cc: stable@xxxxxxxxxxxxxxx
> ---
> kernel/sched/deadline.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
> index 3f02d150e15d..7a6057326611 100644
> --- a/kernel/sched/deadline.c
> +++ b/kernel/sched/deadline.c
> @@ -205,7 +205,7 @@ void __dl_sub(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
> {
> dl_b->total_bw -= tsk_bw;
> if (cpus)
> - __dl_update(dl_b, (s32)tsk_bw / cpus);
> + __dl_update(dl_b, div_u64(tsk_bw, cpus));
It looks like the casts were added to avoid an expensive 64bit divide.
So I'm not at all sure it is sensible to remove them.
Changing to (u32) is enough to stop the sign being flipped and requires
4096 CPU before there is any overflow.
The code seems to have loops across all the cpu - so I'm not sure how
well it actually scales for large systems.
I'm also not sure of the locking for the 'rq->dl_extra_bw += bw' in
__dl_update().
If this code runs every time a deadline process suspends it could
get very expensive even without the divide.
David
> }
>
> static inline
> @@ -213,7 +213,7 @@ void __dl_add(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
> {
> dl_b->total_bw += tsk_bw;
> if (cpus)
> - __dl_update(dl_b, -((s32)tsk_bw / cpus));
> + __dl_update(dl_b, -(s64)div_u64(tsk_bw, cpus));
> }
>
> static inline bool