Re: [PATCH net v5 1/2] net/sched: cbs: use signed 64-bit math for BYTES_PER_KBIT conversions

From: Andy Shevchenko

Date: Sat Dec 05 2026 - 06:19:46 EST


On Sat, Dec 05, 2026 at 03:33:03PM +0800, Jack Wang wrote:
> BYTES_PER_KBIT is unsigned long, so ILP32 uses 32-bit unsigned math.
> Since v6.8, negative sendslope turns positive, breaking software CBS
> and cbs_dump() while high idleslope/port_rate overflow.
>
> Cast BYTES_PER_KBIT and speed in cbs_set_port_rate() to s64 to ensure
> signed 64-bit arithmetic on all architectures.

...

> skip:
> - port_rate = speed * 1000 * BYTES_PER_KBIT;
> + port_rate = (s64)speed * 1000 * (s64)BYTES_PER_KBIT;

The second casting is redundant. Actually how do you expect port_rate be
negative?

...

> - WRITE_ONCE(q->idleslope, qopt->idleslope * BYTES_PER_KBIT);
> - WRITE_ONCE(q->sendslope, qopt->sendslope * BYTES_PER_KBIT);
> + WRITE_ONCE(q->idleslope, qopt->idleslope * (s64)BYTES_PER_KBIT);
> + WRITE_ONCE(q->sendslope, qopt->sendslope * (s64)BYTES_PER_KBIT);

Taking into account the above, why this casting near to the constant and not to
the variable(s)?

...

> - opt.sendslope = div64_s64(READ_ONCE(q->sendslope), BYTES_PER_KBIT);
> - opt.idleslope = div64_s64(READ_ONCE(q->idleslope), BYTES_PER_KBIT);

> + opt.sendslope = div64_s64(READ_ONCE(q->sendslope), (s64)BYTES_PER_KBIT);
> + opt.idleslope = div64_s64(READ_ONCE(q->idleslope), (s64)BYTES_PER_KBIT);

Why? What makes the difference here? The parameter of the function is already
s64.

--
With Best Regards,
Andy Shevchenko