mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net] net/sched: cbs: fix 32-bit overflow in transmission time estimate
@ 2026-11-19  5:33 Mohamed Dawod
  2026-11-20  0:17 ` Vinicius Costa Gomes
  2026-11-23  5:35 ` netdev-bot+sashiko
  0 siblings, 2 replies; 3+ messages in thread
From: Mohamed Dawod @ 2026-11-19  5:33 UTC (permalink / raw)
  To: netdev
  Cc: vinicius.gomes, jhs, jiri, davem, edumazet, kuba, pabeni, horms,
	ye.zh-yuan, linux-kernel, Mohamed Dawod

cbs_dequeue_soft() estimates when the transmission of a packet completes:

	q->last = now + div64_s64(len * NSEC_PER_SEC, port_rate);

len is an int and NSEC_PER_SEC is 1000000000L. On 32-bit architectures
long is 32 bits wide, so the product is computed in 32 bits and wraps
before it is widened for div64_s64(). For a 1242 byte frame the product
is 1242000000000 mod 2^32 = 754451456, which gives a transmission time
of about 60 ns at 100 Mbit/s instead of about 99 us.

As a result q->last stays almost equal to now and the packet sending
time is ignored again, which is the problem 'commit 961d0e5b3294
("net: cbs: Fix software cbs to consider packet sending time")'
set out to fix. The throughput becomes

	idleslope * port_rate / |sendslope|

instead of idleslope.

Measured on a BeagleBone Black (32-bit ARM, CPSW, 100 Mbit/s full
duplex) with software CBS (offload 0), one UDP flow with a 1200 byte
payload (1242 byte frames), sendslope = idleslope - 100000. The rate
was read from the qdisc byte counters:

	idleslope [kbit/s]   before [Mbit/s]   after [Mbit/s]
	10000                11.1              10
	20000                25.0              20
	30000                42.9              30

Cast len to s64 so the multiplication is done in 64 bits on all
architectures.

Fixes: 961d0e5b3294 ("net: cbs: Fix software cbs to consider packet sending time")
Signed-off-by: Mohamed Dawod <mhm.dawod@gmail.com>
---
 net/sched/sch_cbs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

--- a/net/sched/sch_cbs.c
+++ b/net/sched/sch_cbs.c
@@ cbs_dequeue_soft()
 	if (unlikely(atomic64_read(&q->port_rate) == 0))
 		q->last = now;
 	else
-		q->last = now + div64_s64(len * NSEC_PER_SEC,
+		q->last = now + div64_s64((s64)len * NSEC_PER_SEC,
 					  atomic64_read(&q->port_rate));
---
 net/sched/sch_cbs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/sched/sch_cbs.c b/net/sched/sch_cbs.c
index 1c93469c56e3..107c2b6e9da3 100644
--- a/net/sched/sch_cbs.c
+++ b/net/sched/sch_cbs.c
@@ -222,7 +222,7 @@ static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch)
 	if (unlikely(atomic64_read(&q->port_rate) == 0))
 		q->last = now;
 	else
-		q->last = now + div64_s64(len * NSEC_PER_SEC,
+		q->last = now + div64_s64((s64)len * NSEC_PER_SEC,
 					  atomic64_read(&q->port_rate));
 
 	return skb;
-- 
2.25.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH net] net/sched: cbs: fix 32-bit overflow in transmission time estimate
  2026-11-19  5:33 [PATCH net] net/sched: cbs: fix 32-bit overflow in transmission time estimate Mohamed Dawod
@ 2026-11-20  0:17 ` Vinicius Costa Gomes
  2026-11-23  5:35 ` netdev-bot+sashiko
  1 sibling, 0 replies; 3+ messages in thread
From: Vinicius Costa Gomes @ 2026-11-20  0:17 UTC (permalink / raw)
  To: Mohamed Dawod, netdev
  Cc: jhs, jiri, davem, edumazet, kuba, pabeni, horms, ye.zh-yuan,
	linux-kernel, Mohamed Dawod

Mohamed Dawod <mhm.dawod@gmail.com> writes:

> cbs_dequeue_soft() estimates when the transmission of a packet completes:
>
> 	q->last = now + div64_s64(len * NSEC_PER_SEC, port_rate);
>
> len is an int and NSEC_PER_SEC is 1000000000L. On 32-bit architectures
> long is 32 bits wide, so the product is computed in 32 bits and wraps
> before it is widened for div64_s64(). For a 1242 byte frame the product
> is 1242000000000 mod 2^32 = 754451456, which gives a transmission time
> of about 60 ns at 100 Mbit/s instead of about 99 us.
>
> As a result q->last stays almost equal to now and the packet sending
> time is ignored again, which is the problem 'commit 961d0e5b3294
> ("net: cbs: Fix software cbs to consider packet sending time")'
> set out to fix. The throughput becomes
>
> 	idleslope * port_rate / |sendslope|
>
> instead of idleslope.
>
> Measured on a BeagleBone Black (32-bit ARM, CPSW, 100 Mbit/s full
> duplex) with software CBS (offload 0), one UDP flow with a 1200 byte
> payload (1242 byte frames), sendslope = idleslope - 100000. The rate
> was read from the qdisc byte counters:
>
> 	idleslope [kbit/s]   before [Mbit/s]   after [Mbit/s]
> 	10000                11.1              10
> 	20000                25.0              20
> 	30000                42.9              30
>
> Cast len to s64 so the multiplication is done in 64 bits on all
> architectures.
>
> Fixes: 961d0e5b3294 ("net: cbs: Fix software cbs to consider packet sending time")
> Signed-off-by: Mohamed Dawod <mhm.dawod@gmail.com>

It seems that you had tool issues and the diff ended up being
duplicated. With that mechanical/process issue fixed:

Acked-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>

It seems that it would also save CPU time, less 'qdisc_watchdog' being
scheduled without being able to make any progress.


Cheers,
-- 
Vinicius

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH net] net/sched: cbs: fix 32-bit overflow in transmission time estimate
  2026-11-19  5:33 [PATCH net] net/sched: cbs: fix 32-bit overflow in transmission time estimate Mohamed Dawod
  2026-11-20  0:17 ` Vinicius Costa Gomes
@ 2026-11-23  5:35 ` netdev-bot+sashiko
  1 sibling, 0 replies; 3+ messages in thread
From: netdev-bot+sashiko @ 2026-11-23  5:35 UTC (permalink / raw)
  To: mhm.dawod
  Cc: netdev, vinicius.gomes, jhs, jiri, davem, edumazet, kuba, pabeni,
	horms, ye.zh-yuan, linux-kernel

Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 1 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] On 32-bit kernels, software CBS still does not shape after this
  patch, because the configured negative sendslope is stored as a large…
- [Medium] The divisor of the patched transmission-time estimate is
  itself computed in 32 bits on 32-bit kernels.

--

net/sched: cbs: fix 32-bit overflow in transmission time estimate

This patch casts len to s64 in cbs_dequeue_soft(). Without the cast,
len * NSEC_PER_SEC is computed in 32 bits on 32-bit architectures, wraps,
and makes the q->last estimate far too small. The commit message includes
measurements on 32-bit ARM showing throughput matching idleslope after the
fix.

> diff --git a/net/sched/sch_cbs.c b/net/sched/sch_cbs.c
> index 1c93469c56e3d..107c2b6e9da30 100644
> --- a/net/sched/sch_cbs.c
> +++ b/net/sched/sch_cbs.c
> @@ -222,7 +222,7 @@ static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch)
>  	if (unlikely(atomic64_read(&q->port_rate) == 0))
>  		q->last = now;
>  	else
> -		q->last = now + div64_s64(len * NSEC_PER_SEC,
> +		q->last = now + div64_s64((s64)len * NSEC_PER_SEC,
>  					  atomic64_read(&q->port_rate));

[Severity: High]
On 32-bit kernels, does software CBS shape at all on this tree, even with
this fix? A negative sendslope looks like it gets stored as a large
positive value.

net/sched/sch_cbs.c:cbs_change() {
    ...
	WRITE_ONCE(q->idleslope, qopt->idleslope * BYTES_PER_KBIT);
	WRITE_ONCE(q->sendslope, qopt->sendslope * BYTES_PER_KBIT);
    ...
}

Since commit 000db9e9ad42 ("net/sched: cbs: Use units.h instead of the
copy of a definition"), BYTES_PER_KBIT comes from include/linux/units.h:

#define KILO	1000UL
#define BYTES_PER_KBIT		(KILO / BITS_PER_BYTE)

That makes it 125UL, an unsigned long. The old local definition was
(1000LL / 8), which is signed.

On ILP32, the __s32 qopt->sendslope is converted to a 32-bit unsigned
long. The product wraps mod 2^32 and is then zero-extended into the s64
q->sendslope.

With the setup from the commit message, sendslope = -90000 kbit/s is
stored as 4283717296 bytes/s instead of -11250000.

Then in cbs_dequeue_soft():

	/* As sendslope is a negative number, this will decrease the
	 * amount of q->credits.
	 */
	credits = credits_from_len(len, q->sendslope,
				   atomic64_read(&q->port_rate));

With the wrapped value, each 1242-byte frame at 100 Mbit/s adds about
425000 credits instead of subtracting them. q->credits never goes
negative, and the "if (q->credits < 0)" throttling branch never runs.

Wouldn't throughput then be limited only by the q->last estimate, close to
line rate rather than idleslope?

The "before" numbers in the commit message (idleslope * port_rate /
|sendslope|) only work out with a negative sendslope. Could they have come
from a kernel older than 000db9e9ad42?

cbs_dump() also looks affected:

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

On 32-bit this would report 34269738 to userspace for a configured
-90000.

The same unsigned 32-bit multiplication would also wrap idleslope above
about 34.36 Gbit/s.

Should the slope conversion in cbs_change() use signed 64-bit arithmetic
too, for example (s64)qopt->sendslope * BYTES_PER_KBIT, and the same for
idleslope?

[Severity: Medium]
The commit message says:

    Cast len to s64 so the multiplication is done in 64 bits on all
    architectures.

Is the divisor in this expression also safe on 32-bit? It is computed in
cbs_set_port_rate():

net/sched/sch_cbs.c:cbs_set_port_rate() {
    ...
	port_rate = speed * 1000 * BYTES_PER_KBIT;
    ...
}

speed * 1000 is an int and BYTES_PER_KBIT is 125UL. On ILP32 the product
is computed as a 32-bit unsigned long and only widened when it is
assigned to the s64 port_rate.

It wraps for link speeds of 34360 Mbit/s or more. 40G gives 705032704
instead of 5000000000 bytes/s, and 100G gives 3910065408 instead of
12500000000.

On such links, wouldn't the q->last estimate here (about 7x too long at
40G) and the per-packet charge in credits_from_len() both still be wrong
on 32-bit?

Before 000db9e9ad42, the local (1000LL / 8) made this product 64-bit.
Would it make sense to fix this in the same respin, since it has the same
cause as the sendslope conversion?

>  
>  	return skb;

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261119053357.2033707-1-mhm.dawod%40gmail.com

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-23  5:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19  5:33 [PATCH net] net/sched: cbs: fix 32-bit overflow in transmission time estimate Mohamed Dawod
2026-11-20  0:17 ` Vinicius Costa Gomes
2026-11-23  5:35 ` netdev-bot+sashiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®