Netdev List
 help / color / mirror / Atom feed
* [PATCH net v4] net/sched: cbs: perform rate conversions in signed 64-bit arithmetic
@ 2026-12-01  8:54 Jack Wang
  2026-12-01  8:58 ` netdev-bot+sinfo
  2026-12-04 11:54 ` netdev-bot+sashiko
  0 siblings, 2 replies; 5+ messages in thread
From: Jack Wang @ 2026-12-01  8:54 UTC (permalink / raw)
  To: vinicius.gomes, jhs, xiyou.wangcong, jiri, davem, edumazet, kuba,
	pabeni
  Cc: horms, victor, mhm.dawod, esalomatkina, netdev, linux-kernel

Cast BYTES_PER_KBIT to s64 so conversions use signed 64-bit arithmetic
on all architecture. Convert q->idleslope and q->sendslope to atomic64_t
to avoid 64-bit tearing on 32-bit kernels.

Fixes: 000db9e9ad42 ("net/sched: cbs: Use units.h instead of the copy of a definition")
Fixes: 397006ba5d91 ("net/sched: cbs: Fix integer overflow in cbs_set_port_rate()")
Signed-off-by: Jack Wang <163wangjack@gmail.com>
---
v4:
- Drop the partial idleslope < 0 check (watchdog busy-loop will be
  fixed separately).
- Cast BYTES_PER_KBIT to s64 for signed 64-bit math on ILP32 and LP64.
- Convert idleslope/sendslope to atomic64_t to prevent 32-bit tearing.
- Update Fixes: tags, remove the blank line in the tag block, and drop
  v1 Acked-by due to the atomic64_t change.

v3: https://lore.kernel.org/netdev/20260813060256.28748-1-163wangjack@gmail.com/
- Reject only negative idleslope (keep 0 for tdc test 1820).
- Defer timediff_to_credits() overflow to a separate follow-up.

v2:
- Reject non-positive idleslope values.

v1: https://lore.kernel.org/netdev/20260806155253.50252-1-163wangjack@gmail.com/

 net/sched/sch_cbs.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)

diff --git a/net/sched/sch_cbs.c b/net/sched/sch_cbs.c
index 1c93469c56e3..27d08ce07ecf 100644
--- a/net/sched/sch_cbs.c
+++ b/net/sched/sch_cbs.c
@@ -75,8 +75,8 @@ struct cbs_sched_data {
 	s64 credits; /* in bytes */
 	s32 locredit; /* in bytes */
 	s32 hicredit; /* in bytes */
-	s64 sendslope; /* in bytes/s */
-	s64 idleslope; /* in bytes/s */
+	atomic64_t sendslope; /* in bytes/s */
+	atomic64_t idleslope; /* in bytes/s */
 	struct qdisc_watchdog watchdog;
 	int (*enqueue)(struct sk_buff *skb, struct Qdisc *sch,
 		       struct sk_buff **to_free);
@@ -188,7 +188,9 @@ static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch)
 		return NULL;
 	}
 	if (q->credits < 0) {
-		credits = timediff_to_credits(now - q->last, q->idleslope);
+		s64 idleslope = atomic64_read(&q->idleslope);
+
+		credits = timediff_to_credits(now - q->last, idleslope);
 
 		credits = q->credits + credits;
 		q->credits = min_t(s64, credits, q->hicredit);
@@ -196,7 +198,7 @@ static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch)
 		if (q->credits < 0) {
 			s64 delay;
 
-			delay = delay_from_credits(q->credits, q->idleslope);
+			delay = delay_from_credits(q->credits, idleslope);
 			qdisc_watchdog_schedule_ns(&q->watchdog, now + delay);
 
 			q->last = now;
@@ -213,7 +215,7 @@ static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch)
 	/* As sendslope is a negative number, this will decrease the
 	 * amount of q->credits.
 	 */
-	credits = credits_from_len(len, q->sendslope,
+	credits = credits_from_len(len, atomic64_read(&q->sendslope),
 				   atomic64_read(&q->port_rate));
 	credits += q->credits;
 
@@ -335,7 +337,7 @@ static void cbs_set_port_rate(struct net_device *dev, struct cbs_sched_data *q)
 		speed = ecmd.base.speed;
 
 skip:
-	port_rate = speed * 1000 * BYTES_PER_KBIT;
+	port_rate = (s64)speed * 1000 * (s64)BYTES_PER_KBIT;
 
 	atomic64_set(&q->port_rate, port_rate);
 	netdev_dbg(dev, "cbs: set %s's port_rate to: %lld, linkspeed: %d\n",
@@ -405,8 +407,8 @@ static int cbs_change(struct Qdisc *sch, struct nlattr *opt,
 	/* Everything went OK, save the parameters used. */
 	WRITE_ONCE(q->hicredit, qopt->hicredit);
 	WRITE_ONCE(q->locredit, qopt->locredit);
-	WRITE_ONCE(q->idleslope, qopt->idleslope * BYTES_PER_KBIT);
-	WRITE_ONCE(q->sendslope, qopt->sendslope * BYTES_PER_KBIT);
+	atomic64_set(&q->idleslope, qopt->idleslope * (s64)BYTES_PER_KBIT);
+	atomic64_set(&q->sendslope, qopt->sendslope * (s64)BYTES_PER_KBIT);
 	WRITE_ONCE(q->offload, qopt->offload);
 
 	return 0;
@@ -475,8 +477,8 @@ static int cbs_dump(struct Qdisc *sch, struct sk_buff *skb)
 
 	opt.hicredit = READ_ONCE(q->hicredit);
 	opt.locredit = READ_ONCE(q->locredit);
-	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(atomic64_read(&q->sendslope), (s64)BYTES_PER_KBIT);
+	opt.idleslope = div64_s64(atomic64_read(&q->idleslope), (s64)BYTES_PER_KBIT);
 	opt.offload = READ_ONCE(q->offload);
 
 	if (nla_put(skb, TCA_CBS_PARMS, sizeof(opt), &opt))
-- 
2.53.0


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

* Re: [PATCH net v4] net/sched: cbs: perform rate conversions in signed 64-bit arithmetic
  2026-12-01  8:54 [PATCH net v4] net/sched: cbs: perform rate conversions in signed 64-bit arithmetic Jack Wang
@ 2026-12-01  8:58 ` netdev-bot+sinfo
  2026-12-02  8:28   ` Jack Wang
  2026-12-04 11:54 ` netdev-bot+sashiko
  1 sibling, 1 reply; 5+ messages in thread
From: netdev-bot+sinfo @ 2026-12-01  8:58 UTC (permalink / raw)
  To: Jack Wang
  Cc: vinicius.gomes, jhs, xiyou.wangcong, jiri, davem, edumazet, kuba,
	pabeni, horms, victor, mhm.dawod, esalomatkina, netdev,
	linux-kernel

Hi!

This is an automated message. This series looks like a fix, but its
commit messages seem to be missing some information:

 - How the issue was discovered, e.g. hit in production, hit during
   development, syzbot report, manual code inspection, LLM or static
   analysis tool scan.

 - Whether the issue was actually triggered, or is only theoretical
   (e.g. found by LLM and not proven at runtime). If it was triggered
   please include the symptoms, like the stack trace or kernel messages.

Please do not repost the series just to address the above. Instead,
reply to this email with the missing information, so that reviewers
can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include
the relevant info in the cover letter or commit messages.
-- 
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.

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

* Re: [PATCH net v4] net/sched: cbs: perform rate conversions in signed 64-bit arithmetic
  2026-12-01  8:58 ` netdev-bot+sinfo
@ 2026-12-02  8:28   ` Jack Wang
  0 siblings, 0 replies; 5+ messages in thread
From: Jack Wang @ 2026-12-02  8:28 UTC (permalink / raw)
  To: netdev-bot+sinfo
  Cc: vinicius.gomes, jhs, xiyou.wangcong, jiri, davem, edumazet, kuba,
	pabeni, horms, victor, mhm.dawod, esalomatkina, netdev,
	linux-kernel


Thanks for mention this, I forget to describe the problem in my new version. As the version 1-3 cover, “cbs_set_port_rate() and cbs_change() multiply link rates and slope values by BYTES_PER_KBIT, an unsigned long constant.  On 32-bit architectures, the multiplications therefore take place in 32-bit unsigned arithmetic before the results are assigned to s64 fields.” This is the main problem this series aiming to solve.

During v1-v3 the Sashikomention other problem I missed with.”q->idleslope” and “q->sendslope” will be tearing by 32-bit machine, I change idleslope and sendslope to atomic64_t type to solve this.Here is the main change reason for my patch.

Jamal Hadi Salim mention cbs is still have a watchdog problem. I try to solve in v2 and v3 and it did not work well. Considering Mohamed is do a patch relevant to this patch, I believe separating this is a better way. For watchdog issue, I will send another patch to deal with.

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

* Re: [PATCH net v4] net/sched: cbs: perform rate conversions in signed 64-bit arithmetic
  2026-12-01  8:54 [PATCH net v4] net/sched: cbs: perform rate conversions in signed 64-bit arithmetic Jack Wang
  2026-12-01  8:58 ` netdev-bot+sinfo
@ 2026-12-04 11:54 ` netdev-bot+sashiko
  2026-12-04 13:40   ` Jack Wang
  1 sibling, 1 reply; 5+ messages in thread
From: netdev-bot+sashiko @ 2026-12-04 11:54 UTC (permalink / raw)
  To: 163wangjack
  Cc: vinicius.gomes, jhs, xiyou.wangcong, jiri, davem, edumazet, kuba,
	pabeni, horms, victor, mhm.dawod, esalomatkina, netdev,
	linux-kernel

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

Critical: 0 · High: 1 (1) · Medium: 0 (1) · Low: 2 (1)

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 patch title says it makes CBS 'perform rate conversions in
  signed 64-bit arithmetic', but one rate conversion in…
- [Low] The commit message does not say what bug this patch fixes or what
  users see.
- [Low] The patch bundles two separate fixes under one set of Fixes:
  tags.

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but it is a real
  local DoS, and the patch widens it on 32-bit.
- [Medium] This problem wasn't introduced by this patch, and the
  changelog explicitly defers it.
- [Low] This problem wasn't introduced by this patch, but cbs_change()
  publishes hicredit, locredit, idleslope, sendslope and offload as…

--

net/sched: cbs: perform rate conversions in signed 64-bit arithmetic

This patch casts BYTES_PER_KBIT to s64 in the CBS rate conversions, so
they are done in signed 64-bit arithmetic on 32-bit kernels too. It also
converts q->idleslope and q->sendslope to atomic64_t, so 64-bit loads and
stores of them cannot tear on 32-bit kernels.

> Cast BYTES_PER_KBIT to s64 so conversions use signed 64-bit arithmetic
> on all architecture. Convert q->idleslope and q->sendslope to atomic64_t
> to avoid 64-bit tearing on 32-bit kernels.

[Severity: Low]
Could the changelog describe the user-visible regression that this fixes?

Since 000db9e9ad42, BYTES_PER_KBIT comes from include/linux/units.h:

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

This makes it unsigned long. The old local definition was (1000LL / 8).

On ILP32, qopt->sendslope * BYTES_PER_KBIT in cbs_change() is a 32-bit
unsigned multiply, and the result is zero-extended into the s64 field. A
negative sendslope such as -900000 kbit/s is stored as about +4.18e9.
credits_from_len() in cbs_dequeue_soft() then adds credits for every
packet, so q->credits never goes negative and software CBS never shapes.
cbs_dump() also reports a positive sendslope to user space.

The same 32-bit wrap hits idleslope above about 34.36 Gbit/s. It also hits
port_rate in cbs_set_port_rate() for speeds above 34359 Mbit/s, which
397006ba5d91 did not fix on 32-bit.

Could the changelog say that LP64 kernels already gave correct results,
and that since v6.8 software CBS has not shaped at all on 32-bit kernels?

> Fixes: 000db9e9ad42 ("net/sched: cbs: Use units.h instead of the copy of a definition")
> Fixes: 397006ba5d91 ("net/sched: cbs: Fix integer overflow in cbs_set_port_rate()")

[Severity: Low]
Should the atomic64_t conversion be a separate patch with its own Fixes:
tag?

The s64 sendslope and idleslope fields come from 585d763af09c (the
original CBS qdisc). The lockless WRITE_ONCE() stores in cbs_change() came
from 8eb54a421a625a. Neither cited commit introduced the tearing.

With these tags, stable tooling would apply the tearing fix only to
kernels that contain 000db9e9ad42 or 397006ba5d91. The v4 notes also say
the earlier Acked-by was dropped because of the atomic64_t change.

> diff --git a/net/sched/sch_cbs.c b/net/sched/sch_cbs.c
> index 1c93469c56e3d..27d08ce07ecf9 100644
> --- a/net/sched/sch_cbs.c
> +++ b/net/sched/sch_cbs.c

[ ... ]

> @@ -188,7 +188,9 @@ static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch)
>  		return NULL;
>  	}
>  	if (q->credits < 0) {
> -		credits = timediff_to_credits(now - q->last, q->idleslope);
> +		s64 idleslope = atomic64_read(&q->idleslope);
> +
> +		credits = timediff_to_credits(now - q->last, idleslope);

[Severity: Medium]
This is a pre-existing issue, and the changelog already defers it, but
can the multiply in timediff_to_credits() overflow here?

static s64 timediff_to_credits(s64 timediff, s64 slope)
{
	return div64_s64(timediff * slope, NSEC_PER_SEC);
}

idleslope can be as large as about 2^31 * 125 bytes/s. At that maximum,
timediff * slope wraps once now - q->last exceeds about 34 ms. At an
idleslope of 1 Gbit/s it wraps after about 74 s.

The watchdog normally keeps this gap small. Dequeue can still run long
after the watchdog while q->credits < 0, for example when the TX queue
is stopped or softirqs are delayed. The wrapped product can then make
q->credits very negative.

delay_from_credits() then computes -credits * NSEC_PER_SEC, which can wrap
again. The result is either an expiry in the past or a very large delay.

On 32-bit, this patch makes idleslope values above 34.36 Gbit/s take
effect at full size, which lowers the threshold there. LP64 is unchanged.

>  
>  		credits = q->credits + credits;
>  		q->credits = min_t(s64, credits, q->hicredit);
> @@ -196,7 +198,7 @@ static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch)
>  		if (q->credits < 0) {
>  			s64 delay;
>  
> -			delay = delay_from_credits(q->credits, q->idleslope);
> +			delay = delay_from_credits(q->credits, idleslope);
>  			qdisc_watchdog_schedule_ns(&q->watchdog, now + delay);

[Severity: High]
This isn't a bug introduced by this patch, but can an idleslope of zero
or less pin a CPU here?

cbs_change() stores the user-supplied idleslope with no sign or range
check. cbs_policy only checks the attribute length, and v4 dropped the
idleslope < 0 check.

Once q->credits < 0, there are two cases:

With idleslope < 0, timediff_to_credits() returns <= 0, so credits stay
negative. delay_from_credits() returns a negative delay, so now + delay is
already in the past.

With idleslope == 0, delay_from_credits() returns S64_MAX, and
now + S64_MAX wraps to a negative value.

qdisc_watchdog_schedule_range_ns() passes this expiry to the hrtimer
without clamping it:

	hrtimer_start_range_ns(&wd->timer,
			       ns_to_ktime(expires),
			       delta_ns,
			       HRTIMER_MODE_ABS_PINNED);

The timer fires at once and the qdisc is rescheduled. Dequeue finds
credits still negative and arms the timer in the past again.

That looks like an endless hrtimer and NET_TX softirq loop. It only
needs CAP_NET_ADMIN, which an unprivileged user can get in a user
namespace, for example on a veth they own.

idleslope == 0 already triggers this on every architecture, and a
negative idleslope already did on LP64. On ILP32, the old unsigned
multiply turned a negative idleslope into a large positive value. With
the new (s64) cast, 32-bit kernels get this second trigger as well.

The v4 notes say the watchdog busy loop will be fixed separately.

>  
>  			q->last = now;
> @@ -213,7 +215,7 @@ static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch)
>  	/* As sendslope is a negative number, this will decrease the
>  	 * amount of q->credits.
>  	 */
> -	credits = credits_from_len(len, q->sendslope,
> +	credits = credits_from_len(len, atomic64_read(&q->sendslope),
>  				   atomic64_read(&q->port_rate));
>  	credits += q->credits;

[Severity: High]
Is there one more rate conversion in this function that still uses 32-bit
arithmetic on ILP32? Just below this hunk:

	if (unlikely(atomic64_read(&q->port_rate) == 0))
		q->last = now;
	else
		q->last = now + div64_s64(len * NSEC_PER_SEC,
					  atomic64_read(&q->port_rate));

len is an int, and NSEC_PER_SEC is 1000000000L, which is a 32-bit long on
ILP32. So len * NSEC_PER_SEC overflows for any len >= 3 before it is
widened for div64_s64().

For a 1500-byte packet the product wraps to 1,056,413,696. At 1 Gbit/s
that gives q->last = now + 8 ns instead of now + 12 us. For some other
lengths the product goes negative: len = 64 gives -424,509,440.

As a result, the "previous packet is still being sent" check almost never
waits:

	if (now < q->last) {

Idle credits also start to accrue from the wrong q->last.

So even with the sendslope sign fixed, software CBS on 32-bit would let
through idleslope * port_rate / (port_rate - idleslope). That means no
shaping at all when idleslope is half the line rate, and about 33% of the
line rate when it is 25%.

Would this need (s64)len * NSEC_PER_SEC to match the subject line?

[ ... ]

> @@ -405,8 +407,8 @@ static int cbs_change(struct Qdisc *sch, struct nlattr *opt,
>  	/* Everything went OK, save the parameters used. */
>  	WRITE_ONCE(q->hicredit, qopt->hicredit);
>  	WRITE_ONCE(q->locredit, qopt->locredit);
> -	WRITE_ONCE(q->idleslope, qopt->idleslope * BYTES_PER_KBIT);
> -	WRITE_ONCE(q->sendslope, qopt->sendslope * BYTES_PER_KBIT);
> +	atomic64_set(&q->idleslope, qopt->idleslope * (s64)BYTES_PER_KBIT);
> +	atomic64_set(&q->sendslope, qopt->sendslope * (s64)BYTES_PER_KBIT);
>  	WRITE_ONCE(q->offload, qopt->offload);

[Severity: Low]
This is a pre-existing issue, but can cbs_dequeue_soft() see a mix of old
and new parameters here?

qdisc_change() calls sch->ops->change() with only RTNL held, and
cbs_change() does not take sch_tree_lock(). So these five stores are
published one by one. Meanwhile cbs_dequeue_soft() reads idleslope,
hicredit, sendslope and locredit at different points under the root lock.

There are also data races KCSAN may report:

  - cbs_dequeue_soft() reads q->hicredit and q->locredit with plain
    loads, while cbs_change() writes them with WRITE_ONCE().
  - cbs_disable_offload() does plain stores to q->enqueue and q->dequeue,
    which race with plain loads in cbs_enqueue() and cbs_dequeue().

The atomic64_t conversion does stop each slope from tearing, which is all
the changelog claims. Would committing the whole parameter set under
sch_tree_lock() be a more complete fix?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261201085406.228-1-163wangjack%40gmail.com

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

* Re: [PATCH net v4] net/sched: cbs: perform rate conversions in signed 64-bit arithmetic
  2026-12-04 11:54 ` netdev-bot+sashiko
@ 2026-12-04 13:40   ` Jack Wang
  0 siblings, 0 replies; 5+ messages in thread
From: Jack Wang @ 2026-12-04 13:40 UTC (permalink / raw)
  To: netdev-bot+sashiko
  Cc: vinicius.gomes, jhs, xiyou.wangcong, jiri, davem, edumazet, kuba,
	pabeni, horms, victor, mhm.dawod, esalomatkina, netdev,
	linux-kernel

The high problem sashiko pointed already being addressed by Mohamed Dawod in separate patch

For the two Low items, I will address both in v5.

JW..

pw-bot: cr

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

end of thread, other threads:[~2026-12-04 13:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01  8:54 [PATCH net v4] net/sched: cbs: perform rate conversions in signed 64-bit arithmetic Jack Wang
2026-12-01  8:58 ` netdev-bot+sinfo
2026-12-02  8:28   ` Jack Wang
2026-12-04 11:54 ` netdev-bot+sashiko
2026-12-04 13:40   ` Jack Wang

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