Linux Hardware Monitor development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Stefan Popa" <stefan.popa@analog.com>
Cc: linux-hwmon@vger.kernel.org, devicetree@vger.kernel.org,
	robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH v6 2/2] iio: adc: add MAX40080 current-sense amplifier driver
Date: Thu, 30 Jul 2026 13:29:23 +0000	[thread overview]
Message-ID: <20260730132923.A6B781F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260730131738.822405-3-stefan.popa@analog.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Stale measurements: each raw sysfs read returns the previous conversion's data due to an un-flushed FIFO.
- [Medium] Missing READ_ONCE() for `st->range` in `max40080_read_raw()` can lead to data races and torn scale values.
--

--- Patch [2]: [PATCH v6 2/2] iio: adc: add MAX40080 current-sense amplifier driver ---
commit 557f2827eeb4edfa93bf2f63edf6a0664453dad6
Author: Stefan Popa <stefan.popa@analog.com>

iio: adc: add MAX40080 current-sense amplifier driver

The patch adds a direct-mode IIO driver for the MAX40080 current-sense
amplifier, exposing current and voltage channels with configurable
oversampling.

> diff --git a/drivers/iio/adc/max40080.c b/drivers/iio/adc/max40080.c
> new file mode 100644
> index 0000000000000..82572536b704d
> --- /dev/null
> +++ b/drivers/iio/adc/max40080.c

[ ... ]

> +static int max40080_read_iv(struct max40080_state *st, u32 *iv)
> +{
> +	u32 tmp = 0;
> +	int ret, io_ret;
> +
> +	guard(mutex)(&st->lock);
> +
> +	ret = max40080_trigger_measurement(st);
> +	if (ret < 0)
> +		return ret;
> +
> +	/*
> +	 * Wait for the conversion to complete by polling the FIFO valid bit
> +	 * (or bail out on an I2C error). Polling the device's own status makes
> +	 * this independent of the actual conversion time, which varies with the
> +	 * oversampling ratio and the bus speed. The timeout is only a safety
> +	 * ceiling: the worst case is the maximum 128x averaging on both the
> +	 * current and voltage channels at the slowest 15 ksps base rate plus the
> +	 * inter-channel switching time, i.e. roughly 20 ms; 50 ms leaves ample
> +	 * margin.
> +	 */
> +	ret = read_poll_timeout(max40080_read_iv_once, io_ret,
> +				io_ret || (tmp & MAX40080_IV_VALID_MSK),
> +				500, 50000, false, st, &tmp);

[Severity: High]
Does this polling loop read a stale sample?

Writing the SINGLE mode configuration in max40080_init() triggers an initial
conversion, leaving a sample in the FIFO. Because the FIFO already contains
this unread sample, the read_poll_timeout() loop here might instantly succeed
and pop the stale sample before the newly triggered measurement has a chance
to complete.

This would cause each sysfs read to return the previous conversion's data.

> +	/*
> +	 * Propagate the last-read value even on timeout so the caller can
> +	 * inspect it for debugging.
> +	 */
> +	*iv = tmp;
> +	if (ret)
> +		return ret;
> +
> +	return io_ret;
> +}

[ ... ]

> +static int max40080_read_raw(struct iio_dev *indio_dev,
> +			     struct iio_chan_spec const *chan,
> +			     int *val, int *val2, long mask)
> +{
> +	struct max40080_state *st = iio_priv(indio_dev);
> +	unsigned int range;
> +	int ret;
> +
> +	switch (mask) {

[ ... ]

> +	case IIO_CHAN_INFO_SCALE:
> +		if (chan->type == IIO_CURRENT) {
> +			/*
> +			 * The selectable current-sense range is exposed through
> +			 * scale: each RANGE setting has its own precomputed
> +			 * mA-per-code value. Userspace picks the range by writing
> +			 * the matching scale.
> +			 *
> +			 * Take a local copy of range to ensure val and val2 come
> +			 * from the same setting even if a concurrent write changes
> +			 * st->range between the two accesses.
> +			 */
> +			range = st->range;
> +			*val = st->current_scale[range][0];
> +			*val2 = st->current_scale[range][1];

[Severity: Medium]
Could this result in a torn read if st->range is modified concurrently?

Without a READ_ONCE() for st->range, the compiler might optimize away the local
variable and reload st->range directly from memory between reading
st->current_scale[range][0] and st->current_scale[range][1].

If max40080_write_raw() modifies st->range concurrently, this could return an
invalid combination of integer and fractional components.

> +			return IIO_VAL_INT_PLUS_NANO;
> +		}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260730131738.822405-1-stefan.popa@analog.com?part=2

      reply	other threads:[~2026-07-30 13:29 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-30 13:17 [PATCH v6 0/2] iio: adc: add MAX40080 current-sense amplifier driver Stefan Popa
2026-07-30 13:17 ` [PATCH v6 1/2] dt-bindings: iio: adc: add adi,max40080 Stefan Popa
2026-07-30 13:25   ` sashiko-bot
2026-07-30 18:42   ` Siratul Islam
2026-07-30 13:17 ` [PATCH v6 2/2] iio: adc: add MAX40080 current-sense amplifier driver Stefan Popa
2026-07-30 13:29   ` 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=20260730132923.A6B781F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=stefan.popa@analog.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