Re: [PATCH v12 20/43] sched/deadline: Do not truncate a bandwidth to 32 bits in __dl_add/sub()
From: Mathieu Desnoyers
Date: Mon Nov 30 2026 - 10:26:35 EST
On 2026-11-30 09:12, David Laight wrote:
> 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.
The divide runs rarely, so I do not think its cost matters here.
__dl_add() and __dl_sub() run only when deadline bandwidth is
admitted, released, or moved to another root domain:
- sched_setattr() and sched_setaffinity();
- a cpuset attach, CPU hotplug and a rebuild of the root domains;
- a write to the parameters of a deadline server in debugfs;
- the load or the unload of a sched_ext scheduler;
- a task that leaves the deadline class or exits;
- a task that wakes up or migrates into a root domain that does not
count its bandwidth. Patch 14 of this series adds this case.
Each caller first counts the active CPUs of the root domain, with
dl_bw_cpus(). __dl_update() then writes the extra_bw of each of them.
Next to these two loops, one divide costs little. div_u64() is one
divide instruction on 64-bit, and one or two divl on 32-bit x86.
> Changing to (u32) is enough to stop the sign being flipped and requires
> 4096 CPU before there is any overflow.
Two problems with (u32):
- In __dl_add(), -((u32)tsk_bw / cpus) is an unsigned int. The s64
argument of __dl_update() then gets a large positive value. So
extra_bw would grow instead of shrinking.
- x86 with MAXSMP allows 8192 CPUs. A sum of bandwidths can then still
pass U32_MAX.
> If this code runs every time a deadline process suspends it could
> get very expensive even without the divide.
No. A deadline task that blocks or wakes up changes only running_bw
and this_bw of its runqueue. At a wakeup, only the last case of the
list above calls __dl_sub() and __dl_add(). It does so once, when the
task changes root domain.
> The code seems to have loops across all the cpu - so I'm not sure how
> well it actually scales for large systems.
Those loops are already in v7.4-rc2, and this patch does not change
them. They run only on the paths listed above.
> I'm also not sure of the locking for the 'rq->dl_extra_bw += bw' in
> __dl_update().
Good point. __dl_update() writes extra_bw under the dl_bw lock of the
root domain. grub_reclaim() reads it under the lock of its own
runqueue only, and reads it twice. A write between the two reads could
make u_act wrap around. A 32-bit kernel could also read a torn value.
init_dl_rq_bw_ratio() also writes extra_bw without the dl_bw lock, from
the handler of the sched_rt_runtime_us and sched_rt_period_us sysctls.
It sets extra_bw back to max_bw, which forgets the bandwidth already
admitted. All of this is already in v7.4-rc2. I have only checked it by
reading. I would rather fix it in a separate patch.
Thanks,
Mathieu
--
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com