Linux Hardware Monitor development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ferdinand Schwenk via B4 Relay"
	<devnull+ferdinand.schwenk.advastore.com@kernel.org>
Cc: linux-hwmon@vger.kernel.org
Subject: Re: [PATCH v2 1/3] hwmon: ina238: add support for samples and update_interval
Date: Mon, 08 Jun 2026 13:12:08 +0000	[thread overview]
Message-ID: <20260608131209.1EBD81F00893@smtp.kernel.org> (raw)
In-Reply-To: <20260608-hwmon-ina238-update-interval-us-v2-v2-1-2d939fbb2ea1@advastore.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Integer truncation from u64 to u32 in `ina238_interval_us_to_conv_time` causes large user inputs to wrap around, resulting in incorrect hardware configuration on 64-bit platforms.
--

commit 4bfb82fb9bb164125fe0facd52cce87c1d8b248e
Author: Ferdinand Schwenk <ferdinand.schwenk@advastore.com>

hwmon: ina238: add support for samples and update_interval

This commit exposes the INA238 ADC averaging count and conversion timing
through samples and update_interval hwmon attributes. It caches ADC_CONFIG
to optimize read-modify-write operations. The conversion times are derived
using per-chip lookup tables.

> diff --git a/drivers/hwmon/ina238.c b/drivers/hwmon/ina238.c
> index ff67b03189f73..667f78d43a6a8 100644
> --- a/drivers/hwmon/ina238.c
> +++ b/drivers/hwmon/ina238.c

[ ... ]

> @@ -261,6 +296,97 @@ static int ina228_read_voltage(struct ina238_data *data, int channel, long *val)

[ ... ]

> +static inline u32 ina238_interval_ms_to_conv_time(long interval, u32 samples)
> +{
> +	u64 interval_us;
> +
> +	interval = clamp_val(interval, 0, LONG_MAX / 1000);
> +	interval_us = (u64)interval * 1000;
> +
> +	/*
> +	 * update_interval reports the ADC cycle time including averaging.
> +	 * The target per-field conversion time is interval_us / (samples * 3).
> +	 */
> +	return DIV_ROUND_CLOSEST_ULL(interval_us, samples * 3);
> +}

[Severity: Medium]
Does returning a u32 here cause integer truncation for large user inputs on
64-bit platforms? Since interval is clamped to LONG_MAX / 1000, interval_us
can be extremely large. The DIV_ROUND_CLOSEST_ULL macro evaluates to an
unsigned long long, but returning it as u32 will silently truncate results
exceeding 4294967295.

[ ... ]

> +static int ina238_write_chip(struct device *dev, u32 attr, long val)
> +{
> +	struct ina238_data *data = dev_get_drvdata(dev);
> +	u16 adc_config;
> +	int idx, ret;
> +
> +	switch (attr) {

[ ... ]

> +	case hwmon_chip_update_interval:
> +		val = ina238_interval_ms_to_conv_time(val, ina238_samples(data));
> +		idx = find_closest(val, data->config->conv_time,
> +				   ARRAY_SIZE(ina238_conv_time));

[Severity: Medium]
Could the truncation in the interval helper above cause this find_closest()
lookup to select the wrong hardware configuration? For example, if a user
input results in a target conversion time of 4294967333 us, it wraps to
37 us, which would incorrectly select the minimum conversion time (50 us)
instead of clamping to the maximum supported hardware interval (8230 us).

[ ... ]

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260608-hwmon-ina238-update-interval-us-v2-v2-0-2d939fbb2ea1@advastore.com?part=1

  reply	other threads:[~2026-06-08 13:12 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-08 13:03 [PATCH v2 0/3] hwmon: ina238: add samples and update_interval_us support Ferdinand Schwenk via B4 Relay
2026-06-08 13:03 ` [PATCH v2 1/3] hwmon: ina238: add support for samples and update_interval Ferdinand Schwenk via B4 Relay
2026-06-08 13:12   ` sashiko-bot [this message]
2026-06-08 16:31     ` Guenter Roeck
2026-06-08 13:03 ` [PATCH v2 2/3] hwmon: Add update_interval_us chip attribute Ferdinand Schwenk via B4 Relay
2026-06-08 13:10   ` sashiko-bot
2026-06-08 16:34   ` Guenter Roeck
2026-06-08 13:03 ` [PATCH v2 3/3] hwmon: ina238: add update_interval_us attribute Ferdinand Schwenk via B4 Relay
2026-06-08 13:13   ` sashiko-bot

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=20260608131209.1EBD81F00893@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=devnull+ferdinand.schwenk.advastore.com@kernel.org \
    --cc=linux-hwmon@vger.kernel.org \
    --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