All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Akhilesh Patil" <akhilesh.patil@linux.dev>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH v2 2/2] iio: chemical: acd10: Add driver for ACD10 CO2 sensor
Date: Wed, 02 Dec 2026 12:33:25 +0000	[thread overview]
Message-ID: <sashiko-outbox-245727@kernel.org> (raw)
In-Reply-To: <8aad2c61de154bd9ffc82aa94747da6fa8333687.1796211342.git.akhilesh.patil@linux.dev>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] iio: chemical: acd10: shared TX/RX buffer race in
  acd10_serdev_transfer()
--

commit 0d5d850b993452ce98f539b196f7b69e4ad1b6e8
Author: Akhilesh Patil <akhilesh.patil@linux.dev>

iio: chemical: acd10: Add driver for ACD10 CO2 sensor

This commit adds an IIO driver for the Aosong ASAIR ACD10 CO2 sensor
using the serdev UART interface.

> diff --git a/drivers/iio/chemical/acd10.c b/drivers/iio/chemical/acd10.c
> new file mode 100644
> index 0000000000000..a77dde636ea74
> --- /dev/null
> +++ b/drivers/iio/chemical/acd10.c
[ ... ]
> +static int acd10_serdev_transfer(struct acd10_state *drv_state, enum acd10_cmds cmd)
> +				 __must_hold(&drv_state->lock)
> +{
> +	struct serdev_device *serdev = drv_state->serdev;
> +	struct device *dev = &serdev->dev;
> +	int ret;
> +
> +	/* Prepare transmit buffer along with checksum and send over serial */
> +	drv_state->buf[ACD10_FRAME_START_IDX] = ACD10_FRAME_START;
> +	drv_state->buf[ACD10_FRAME_FIXED_CODE_IDX] = ACD10_FRAME_FIXED_CODE;
> +	drv_state->buf[ACD10_FRAME_DATA_LEN_IDX] = cmd_info[cmd].tx_data_len;
> +	drv_state->buf[ACD10_FRAME_CMD_CODE_IDX] = cmd_info[cmd].cmd_code;
> +	drv_state->buf[cmd_info[cmd].tx_frame_len - 1] =
> +		acd10_get_checksum(drv_state->buf, cmd_info[cmd].tx_frame_len);
> +	drv_state->buf_idx = 0;
> +	drv_state->rx_expected_len = cmd_info[cmd].rx_frame_len;
> +	reinit_completion(&drv_state->buf_ready);
> +
> +	ret = serdev_device_write(serdev, drv_state->buf, cmd_info[cmd].tx_frame_len,
> +				  ACD10_SERDEV_TIMEOUT);
> +	if (ret < 0)
> +		return ret;
> +	if (ret != cmd_info[cmd].tx_frame_len)
> +		return -EIO;
> +
> +	/* Wait till we receive expected number of bytes for sent cmd */
> +	ret = wait_for_completion_interruptible_timeout(&drv_state->buf_ready,
> +							ACD10_SERDEV_TIMEOUT);
> +	if (ret < 0)
> +		return ret;
> +	if (!ret)
> +		return -ETIMEDOUT;
[ ... ]
> +static size_t acd10_receive_buf(struct serdev_device *serdev,
> +				const u8 *data, size_t len)
> +{
> +	struct iio_dev *indio_dev = serdev_device_get_drvdata(serdev);
> +	struct acd10_state *drv_state = iio_priv(indio_dev);
> +	size_t consume = len;
> +	int i = 0;
> +
> +	while (drv_state->rx_expected_len && consume > 0 &&
> +	       drv_state->buf_idx < sizeof(drv_state->buf)) {
> +		drv_state->buf[drv_state->buf_idx++] = data[i++];
> +		consume -= 1;
> +		drv_state->rx_expected_len -= 1;
> +	}
> +
> +	if (drv_state->rx_expected_len == 0) {
> +		drv_state->buf_idx = 0;
> +		complete(&drv_state->buf_ready);
> +	}
> +
> +	return len;
> +}

[Severity: Medium]
Can an interrupted or timed-out transfer leave drv_state->rx_expected_len
non-zero and allow acd10_receive_buf() to race on the shared drv_state->buf
during the next transfer?

At 1200 baud, a 9-byte response takes around 75 ms to arrive.  If
wait_for_completion_interruptible_timeout() is interrupted by a signal
(or times out) while the response is in flight, acd10_serdev_transfer()
returns without resetting drv_state->rx_expected_len:

CPU0                              kworker (tty flip buffer)
acd10_serdev_transfer()
  rx_expected_len = 9
  serdev_device_write(...)
  wait_for_completion...()
    -> -ERESTARTSYS

acd10_serdev_transfer()
  buf[0] = ACD10_FRAME_START
  ...
                                  acd10_receive_buf()
                                    buf[buf_idx++] = data[i++]
  buf[4] = acd10_get_checksum(buf)
  serdev_device_write(buf)        <-- sends corrupted TX frame

Because acd10_receive_buf() does not take drv_state->lock and shares
drv_state->buf with the transmit path, late bytes from the previous
response overwrite the outgoing frame or shift the receive buffer of subsequent
transfers out of alignment.

In addition, in acd10_receive_buf(), if an unexpected byte arrives when
drv_state->rx_expected_len is already 0, the while loop does not run, but
the if (drv_state->rx_expected_len == 0) check still calls
complete(&drv_state->buf_ready).  And in acd10_serdev_transfer(),
drv_state->rx_expected_len is armed before reinit_completion() and
serdev_device_write(), so a completion that fires before
reinit_completion() is lost.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796211342.git.akhilesh.patil@linux.dev?part=2

      reply	other threads:[~2026-12-02 12:33 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 12:23 [PATCH v2 0/2] iio: chemical: Add driver and bindings for acd10 Akhilesh Patil
2026-12-02 12:24 ` [PATCH v2 1/2] dt-bindings: iio: chemical: Add aosong acd10 Akhilesh Patil
2026-12-02 12:25 ` [PATCH v2 2/2] iio: chemical: acd10: Add driver for ACD10 CO2 sensor Akhilesh Patil
2026-12-02 12:33   ` 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=sashiko-outbox-245727@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akhilesh.patil@linux.dev \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=robh@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.