Linux CAN drivers development
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: mkl@pengutronix.de
Cc: mailhol@kernel.org, socketcan@hartkopp.net, wg@grandegger.com,
	davem@davemloft.net, kernel@pengutronix.de,
	linux-can@vger.kernel.org, 25031212351@stu.xidian.edu.cn,
	stable@vger.kernel.org, byu@xidian.edu.cn,
	wencheng@xidian.edu.cn, kuba@kernel.org
Subject: Re: [PATCH can-next v4 3/6] can: bittiming: can_calc_bittiming(): harden bitrate error calculation on unsigned operands
Date: Fri, 04 Dec 2026 03:51:44 +0000	[thread overview]
Message-ID: <179635630432.3142.3704619545620624823@kernel.org> (raw)
In-Reply-To: <20261202-bittiming-fix-conversion-v4-3-fae7aaaf3191@pengutronix.de>

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The commit message describes a bug that the old code did not
  have.

--

can: bittiming: can_calc_bittiming(): harden bitrate error calculation on unsigned operands

This commit replaces abs(bt->bitrate - bitrate) with
abs_diff(bt->bitrate, bitrate) in can_calc_bittiming(). The commit
message says the old expression turned a wrapped unsigned difference into
a small positive value, which would mis-score bitrate candidates.

> Both operands are unsigned int, the subtraction may wrap instead of
> becoming negative, abs() resolves an unsigned int argument to its int
> branch. A wrapped difference is therefore reinterpreted as a small positive
> value instead of the large error it actually represents.

[Severity: Low]
Is this description accurate? The kernel's abs() converts an unsigned int
to int before it takes the absolute value:

include/linux/math.h:
#define __abs_choose_expr(x, type, other) __builtin_choose_expr(	\
	__builtin_types_compatible_p(typeof(x),   signed type) ||	\
	__builtin_types_compatible_p(typeof(x), unsigned type),		\
	({ signed type __x = (x); __x < 0 ? -__x : __x; }), other)

Say the computed bitrate is larger than bt->bitrate by d. The subtraction
wraps to 2^32 - d, the cast to int gives -d, and abs() returns d. That is
the real bitrate error, not a hidden large one.

The old result could only be wrong if the true difference were 2^31 or
more. In can_calc_bittiming(), this check skips every candidate where
tsegall * bt->bitrate overflows:

		if (check_mul_overflow(tsegall, bt->bitrate, &div))
			continue;

Any constants with tseg1_min + tseg2_min >= 1 give tsegall >= 2, and every
in-tree driver table meets that. So every candidate that gets evaluated
has bt->bitrate below 2^31, and bitrate = priv->clock.freq / div is at
most UINT_MAX / 2.

The only exception is tseg1_min == tseg2_min == 0, where tsegall can be 1.
With those constants the unsigned loop condition tseg >= 0 is always true,
so the loop does not end properly anyway. The later commit "can:
bittiming: register_candev(): reject unusable bit-timing constants" also
rejects such constants.

So abs_diff() seems to give the same value as the old code on every
reachable path. The word "harden" in the subject fits that.

Could the body be reworded so it doesn't describe this as a fix for
mis-scored bitrate candidates? As written, it could lead stable
backporters to treat this cleanup as a functional fix.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202-bittiming-fix-conversion-v4-0-fae7aaaf3191%40pengutronix.de

  reply	other threads:[~2026-12-04  3:51 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 13:57 [PATCH can-next v4 0/6] can: bittiming: fix integer overflows in the userspace bit rate conversion and reject invalid timing constants Marc Kleine-Budde
2026-12-02 13:57 ` [PATCH can-next v4 1/6] can: bittiming: can_calc_bittiming(): fix potential divide-by-zero Marc Kleine-Budde
2026-12-02 13:57 ` [PATCH can-next v4 2/6] can: bittiming: can_calc_bittiming(): catch brp * tsegall overflow Marc Kleine-Budde
2026-12-04  3:51   ` netdev-bot+sashiko
2026-12-02 13:57 ` [PATCH can-next v4 3/6] can: bittiming: can_calc_bittiming(): harden bitrate error calculation on unsigned operands Marc Kleine-Budde
2026-12-04  3:51   ` netdev-bot+sashiko [this message]
2026-12-02 13:57 ` [PATCH can-next v4 4/6] can: bittiming: can_update_sample_point(): harden sample point " Marc Kleine-Budde
2026-12-04  3:51   ` netdev-bot+sashiko
2026-12-02 13:57 ` [PATCH can-next v4 5/6] can: bittiming: can_fixup_bittiming(): reject too high values for prop-seg/phase-seg1 Marc Kleine-Budde
2026-12-02 13:57 ` [PATCH can-next v4 6/6] can: bittiming: register_candev(): reject unusable bit-timing constants Marc Kleine-Budde
2026-12-04  3:51   ` netdev-bot+sashiko

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=179635630432.3142.3704619545620624823@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=25031212351@stu.xidian.edu.cn \
    --cc=byu@xidian.edu.cn \
    --cc=davem@davemloft.net \
    --cc=kernel@pengutronix.de \
    --cc=kuba@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=mailhol@kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=socketcan@hartkopp.net \
    --cc=stable@vger.kernel.org \
    --cc=wencheng@xidian.edu.cn \
    --cc=wg@grandegger.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