net/sched: cbs: fix 32-bit overflow in transmission time estimate
Subsystems (4)
PATCH v2 1/1 net/sched: cbs: fix 32-bit overflow in transmission time estimate
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.
--- net/sched/sch_cbs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/sched/sch_cbs.c +1 -1
@@ -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;
Signature · 1 line
2.25.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · 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] The fix only works on older 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, the len * NSEC_PER_SEC product is computed in 32 bits on 32-bit architectures and wraps. The commit message says this restores idleslope-limited throughput for software CBS on 32-bit, and includes before/after numbers measured on a BeagleBone Black.
net/sched/sch_cbs.c static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch) · line 226
Quoted 11 lines
net/sched/sch_cbs.c +1
-1@@ -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 this tree, does this change alone make 32-bit throughput follow idleslope? The same kind of 32-bit wrap still seems to happen when cbs_change() stores the sendslope:
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 an unsigned long. The old local definition was (1000LL / 8). On ILP32, the negative __s32 qopt->sendslope is converted to unsigned long. The product wraps modulo 2^32 and is then zero-extended into the s64 q->sendslope.
With the setup from the commit message (idleslope 10000, sendslope -90000), q->sendslope comes out as +4283717296 instead of -11250000.
In cbs_dequeue_soft(), credits_from_len(len, q->sendslope, port_rate) then adds credits after each packet instead of taking them away. cbs_enqueue_soft() only resets positive credits to 0, so q->credits < 0 never becomes true. The idleslope throttle and delay_from_credits() never run either.
That leaves the q->last gate fixed here as the only limit. Wouldn't traffic then go out at about line rate rather than at idleslope?
The "before" numbers in the commit message (11.1/25.0/42.9 Mbit/s) match idleslope * port_rate / |sendslope|. That only works with a correctly negative sendslope, so were they measured on a kernel without 000db9e9ad42?
Because of the Fixes: tag on 961d0e5b3294, this will go back to stable trees. On 32-bit kernels from v6.8 on, software CBS would still do no shaping after the backport.
cbs_dump() also hands the wrapped value back to userspace:
opt.sendslope = div64_s64(READ_ONCE(q->sendslope), BYTES_PER_KBIT);
A configured -90000 reads back as 34269738.
cbs_set_port_rate() has the same unsigned long multiply:
port_rate = speed * 1000 * BYTES_PER_KBIT;
On 32-bit this wraps for link speeds of 34360 Mb/s and above. For example, 40000 Mb/s gives 705032704 B/s instead of 5000000000 B/s. That port_rate is the divisor in the expression this patch changes.
Could these be widened as well, for example (s64)qopt->sendslope * BYTES_PER_KBIT, (s64)qopt->idleslope * BYTES_PER_KBIT and (s64)speed * 1000 * BYTES_PER_KBIT? That fix might need its own Fixes: tag for 000db9e9ad42.
If not, should the commit message stop saying that 32-bit throughput becomes idleslope on current kernels?
Quoted 2 lines
net/sched/sch_cbs.c +0
-0return skb;
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261122064155.2094434-1-mhm.dawod%40gmail.com
Thank you for the review.
I agree with the concern that my patch alone does not guarantee idleslope-limited throughput on 32-bit kernels affected by the signed/unsigned rate-conversion issue.
However, this is a separate issue that is already addressed by Jack Wang's patch series:
[PATCH net v3] net/sched: cbs: perform rate conversions in signed 64-bit arithmetic
https://lkml.iu.edu/2608.1/09680.html
My patch fixes the overflow in the transmission-time calculation in cbs_dequeue_soft(), while Jack's patch fixes the rate conversions in cbs_change() and cbs_set_port_rate().
Both fixes are needed for correct software CBS operation on affected 32-bit kernels.
I believe these changes should be integrated in the appropriate order, with the rate-conversion fix applied before my patch, so that the complete CBS functionality is restored.