Re: [PATCH v12 20/43] sched/deadline: Do not truncate a bandwidth to 32 bits in __dl_add/sub()

From: David Laight

Date: Tue Dec 01 2026 - 11:15:07 EST


On Tue, 1 Dec 2026 10:11:14 -0500
Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx> wrote:

> On 2026-11-30 13:16, David Laight wrote:
> >> 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.
> >
> > Nope, the unsigned int is converted to u64 before being converted to s64.
>
> The u64 step does not change the result. The negation is done on 32
> bits: -(u32)x is 2^32 - x, a positive unsigned int. Its conversion to
> s64 keeps that value. Take a task with 10% of a CPU (104857), in a root
> domain of 4 active CPUs. __dl_update() gets 4294941082 instead of
> -26214. I checked this with a small user-space program.

You need to get the casts in the right order.
The quotient needs zero extending to 64bits before being negated.
eg -(s64)((u32)task_by / cpus)
or 0LL - (u32)task_bw / cpus.

Although that is still only right for 4096 cpu.

>
> > The existing code is doing a signed divide so can generate 'very large
> > positive values'; just changing 'int cpus' to 'unsigned int cpus'
> > would also have the same effect.
>
> Not quite. Once the sum passes S32_MAX, the existing code flips the
> sign of the value, as the changelog says. It does not make the value
> large. That is the bug this patch fixes. With an unsigned cpus, the
> division and the negation would be unsigned, as with (u32). So
> __dl_add() would pass 2^32 minus the quotient to __dl_update() for
> every bandwidth, even that of one small task.
>
> > That probably means that a lot of the '<< 20' should be '<< 19'.
>
> BW_SHIFT sets the precision of every bandwidth ratio. With a 64-bit
> divide, a sum needs no smaller shift. I would rather not change it in
> a fix.
>
> >> Each caller first counts the active CPUs of the root domain, with
> >> dl_bw_cpus().
> >
> > From the above it seems like that is 'almost constant' so doesn't
> > need recalculating all the time.
>
> Right. It changes only at CPU hotplug and at a rebuild of the root
> domains. The root domain could keep it.
>
> >> Those loops are already in v7.4-rc2, and this patch does not change
> >> them. They run only on the paths listed above.
> >
> > Indeed - that doesn't mean it is a good idea...
>
> Agreed. They are worth a look on large machines. That is outside this
> series.
>
> > Are you saying that either there is only one rd->span, or that they are
> > all disjoint.
> > Otherwise there can be two updates of the field under different locks.
>
> Yes, they are disjoint. Each CPU is in one span only, the span of its
> rq->rd. But partition_sched_domains() moves CPUs between root domains.
> Through cpu_attach_domain(), it calls rq_attach_root(), which moves a
> CPU under its rq lock only.
>
> rq_attach_root() clears the CPU in the old span, then sets it in the
> new one. The callers of __dl_update() hold the dl_bw lock of their
> root domain, not the rq lock. So two updates can see the moving CPU:
> - task_non_contending(), on another CPU of the old root domain,
> releases the bandwidth of a task that leaves the class. It read the
> old span before the clear.
> - rq_attach_root() itself adds the bandwidth of the fair server to the
> new root domain, with __dl_server_attach_root().
> Both then write the extra_bw of that CPU under different locks, and one
> update could be lost. At its end, partition_sched_domains() rebuilds
> the accounting. dl_clear_root_domain() then sets extra_bw again. So
> the loss lasts only until the end of the rebuild.

There is also the RCU_LOCKDEP check.
rcu is usually used lists (or similar) that are followed often but
relatively rarely modified.
I've not even tried to see what that is about.
If this code is only executed when cpu 'move' then I'd have thought
there would be minimal contention from a single global lock.
This code would then hold the lock.

David

> This is also in v7.4-rc2, and I have only checked it by reading. v13
> does not fix it.
>
> Thanks,
>
> Mathieu
>