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
  0 siblings, 1 reply; 2+ 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] 2+ messages in thread

end of thread, other threads:[~2026-12-01  8:58 UTC | newest]

Thread overview: 2+ 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

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