Linux Hardware Monitor development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Guenter Roeck" <linux@roeck-us.net>
Cc: linux-hwmon@vger.kernel.org
Subject: Re: [PATCH v4] hwmon: (ina2xx) Fix overflow issues
Date: Thu, 11 Jun 2026 21:39:11 +0000	[thread overview]
Message-ID: <20260611213912.0B7961F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260611212634.846262-1-linux@roeck-us.net>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The INA2XX_CURRENT limit calculation artificially restricts physically valid high-current alerts due to a mathematically flawed clamp derivation.
--

commit bc9d600ea70203e68f587fb999176459b42802ea
Author: Guenter Roeck <linux@roeck-us.net>

hwmon: (ina2xx) Fix overflow issues

This patch updates the return type of ina2xx_get_value() to long to
support a wider range of values on 64-bit systems. It also adds and adjusts
clamping limits in ina2xx_get_value() and ina226_alert_to_reg() to prevent
integer overflows during intermediate calculations.

> diff --git a/drivers/hwmon/ina2xx.c b/drivers/hwmon/ina2xx.c
> index 613ffb622b7c4..b488e58ed2f4f 100644
> --- a/drivers/hwmon/ina2xx.c
> +++ b/drivers/hwmon/ina2xx.c

[ ... ]

> @@ -378,23 +381,27 @@ static int ina2xx_read_init(struct device *dev, int reg, long *val)
>   */
>  static u16 ina226_alert_to_reg(struct ina2xx_data *data, int reg, long val)
>  {
> +	long limit;
> +
>  	switch (reg) {

[ ... ]

>  	case INA2XX_CURRENT:
> -		val = clamp_val(val, INT_MIN / 1000, INT_MAX / 1000);
> +		limit = ((INT_MAX - data->current_lsb_uA) / 1000) >>
> +		  data->config->current_shift;
> +		val = clamp_val(val, -limit, limit);

[Severity: High]
Does this calculation miscalculate the maximum safe input limit by not
accounting for the subsequent division?

To prevent the final shifted value (val * 1000 / current_lsb_uA) << shift
from overflowing, the limit would need to factor in the division by
current_lsb_uA. Instead, this formula subtracts the LSB and effectively
shifts the INT_MAX / 1000 limit right by current_shift.

For devices like the INA234 (current_shift = 4) configured with a small
shunt, the LSB can be very large. As noted in the commit message,
current_lsb_uA can reach 40,000,000. In this configuration, the hardware's
physical maximum alert limit is (32767 >> 4) * 40A = 81,880A.

By not multiplying the allowable upper bound by the LSB before the shift,
does this artificially cap the user input to ((INT_MAX - 40000000) / 1000)
>> 4 = 131,717 mA (131.7A)? This restricts users to less than 0.2% of the
sensor's valid measurement range.

>  		/* signed register, result in mA */
>  		val = DIV_ROUND_CLOSEST(val * 1000, data->current_lsb_uA);
>  		val <<= data->config->current_shift;
>  		return clamp_val(val, SHRT_MIN, SHRT_MAX);

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260611212634.846262-1-linux@roeck-us.net?part=1

      reply	other threads:[~2026-06-11 21:39 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-11 21:26 [PATCH v4] hwmon: (ina2xx) Fix overflow issues Guenter Roeck
2026-06-11 21:39 ` sashiko-bot [this message]

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=20260611213912.0B7961F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=sashiko-reviews@lists.linux.dev \
    /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