Netdev List
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: 163wangjack@gmail.com
Cc: vinicius.gomes@intel.com, jhs@mojatatu.com,
	xiyou.wangcong@gmail.com, jiri@resnulli.us, davem@davemloft.net,
	edumazet@google.com, kuba@kernel.org, pabeni@redhat.com,
	horms@kernel.org, victor@mojatatu.com, mhm.dawod@gmail.com,
	esalomatkina@ispras.ru, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH net v4] net/sched: cbs: perform rate conversions in signed 64-bit arithmetic
Date: Fri, 04 Dec 2026 11:54:37 +0000	[thread overview]
Message-ID: <179638527754.3146.11550286389900504432@kernel.org> (raw)
In-Reply-To: <20261201085406.228-1-163wangjack@gmail.com>

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

  parent reply	other threads:[~2026-12-04 11:54 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2026-12-04 13:40   ` Jack Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=179638527754.3146.11550286389900504432@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=163wangjack@gmail.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=esalomatkina@ispras.ru \
    --cc=horms@kernel.org \
    --cc=jhs@mojatatu.com \
    --cc=jiri@resnulli.us \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mhm.dawod@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=victor@mojatatu.com \
    --cc=vinicius.gomes@intel.com \
    --cc=xiyou.wangcong@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox