Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Esben Haabendal" <esben@geanix.com>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH v6 7/9] iio: accel: mma8452: Drop unneeded lock acquire on read
Date: Tue, 25 Aug 2026 08:45:03 +0000	[thread overview]
Message-ID: <20260825084504.778091F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260825-mma8452-open-drain-v6-7-9b252804ee80@geanix.com>

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:

New issues:
- [High] Removing data->lock exposes mma8452_read() to race conditions with PM auto-suspend and event configuration.

Pre-existing issues:
- [High] Polling for DRDY before PM runtime wake-up leads to I/O timeouts and errors.
- [High] PM runtime usage count leak on error paths.
- [High] Device is permanently locked in STANDBY mode on I2C write failure.
- [Medium] Unprotected read-modify-write operations on event configuration registers.
- [Medium] Kernel stack information leak due to mishandling of short I2C block reads.
--

--- Patch [7]: [PATCH v6 7/9] iio: accel: mma8452: Drop unneeded lock acquire on read ---
commit 9c2b3fc538cb28ca3a39bb1585927926bb25f284
Author: Esben Haabendal <esben@geanix.com>

iio: accel: mma8452: Drop unneeded lock acquire on read

There is no need to acquire data->lock when calling mma8452_read(), and
dropping that makes it less likely to end up in an AB-BA deadlock
situation.

> diff --git a/drivers/iio/accel/mma8452.c b/drivers/iio/accel/mma8452.c
> index 7ef1a9a91c31f..9ae2c3e605761 100644
> --- a/drivers/iio/accel/mma8452.c
> +++ b/drivers/iio/accel/mma8452.c
> @@ -504,9 +504,7 @@ static int mma8452_read_raw(struct iio_dev *indio_dev,
>  		if (!iio_device_claim_direct(indio_dev))
>  			return -EBUSY;
>  
> -		mutex_lock(&data->lock);
>  		ret = mma8452_read(data, buffer);

[Severity: High]
Could removing this lock expose mma8452_read() to race conditions with PM
auto-suspend and event configuration?

mma8452_read() can be interrupted by the PM auto-suspend worker, which puts
the device in STANDBY and disables regulators while mma8452_drdy() is actively
polling over I2C. This can lead to I/O timeouts or errors.

Additionally, concurrent sysfs writes to event configurations invoke
mma8452_change_config(), which puts the hardware into STANDBY to modify
registers. The Standby transition flushes the hardware FIFO. If this occurs
between the mma8452_drdy() check and the i2c_smbus_read_i2c_block_data() in
mma8452_read(), the block read will fetch flushed or stale data.

> -		mutex_unlock(&data->lock);
>  		iio_device_release_direct(indio_dev);
>  		if (ret < 0)
>  			return ret;

[Severity: High]
This is a pre-existing issue, but will polling for DRDY before PM runtime
wake-up lead to I/O timeouts and errors?

In mma8452_read():

	int ret = mma8452_drdy(data);

	if (ret < 0)
		return ret;

	ret = mma8452_set_runtime_pm_state(data->client, true);

If the device is in auto-suspend, its regulators are turned off by
mma8452_runtime_suspend().

The I2C read in mma8452_drdy() will communicate with a powered-off device,
resulting in immediate I/O errors or timeouts.

Should the PM runtime resume be performed before any hardware register polling?

[Severity: High]
This is a pre-existing issue, but is there a PM runtime usage count leak on
error paths?

In mma8452_read():

	ret = mma8452_set_runtime_pm_state(data->client, true);
	if (ret)
		return ret;

	ret = i2c_smbus_read_i2c_block_data(data->client, MMA8452_OUT_X,
					    3 * sizeof(__be16), (u8 *)buf);
	if (ret < 0)
		return ret;

If i2c_smbus_read_i2c_block_data() fails, it returns the error code
immediately. This bypasses the subsequent call to
mma8452_set_runtime_pm_state(data->client, false), permanently leaking the
usage count.

A similar leak occurs in mma8452_write_event_config():

	ret = mma8452_set_runtime_pm_state(data->client, state);
	if (ret)
		return ret;

	switch (dir) {
	case IIO_EV_DIR_FALLING:
        ...
	case IIO_EV_DIR_RISING:
		val = i2c_smbus_read_byte_data(data->client, ev_regs->ev_cfg);
		if (val < 0)
			return val;

If this I2C read fails and returns early, the PM runtime decrement is also
missed.

[Severity: Medium]
This is a pre-existing issue, but are the read-modify-write operations on
event configuration registers properly protected?

In mma8452_write_event_config():

	case IIO_EV_DIR_RISING:
		val = i2c_smbus_read_byte_data(data->client, ev_regs->ev_cfg);
		if (val < 0)
			return val;

The initial I2C read is performed without holding data->lock, while the write
is performed by mma8452_change_config() which does hold the lock.

Since IIO event sysfs attributes do not hold the IIO mlock, concurrent writes
to different event attributes can interleave.

Could two threads perform the unprotected read simultaneously, resulting in the
second thread silently overwriting the first thread's register modifications?

[Severity: High]
This is a pre-existing issue, but can an I2C write failure leave the device
permanently locked in STANDBY mode?

In mma8452_change_config():

	/* config can only be changed when in standby */
	if (is_active > 0) {
		ret = mma8452_standby(data);
		if (ret < 0)
			goto fail;
	}

	ret = i2c_smbus_write_byte_data(data->client, reg, val);
	if (ret < 0)
		goto fail;

	if (is_active > 0) {
		ret = mma8452_active(data);
...
fail:
	mutex_unlock(&data->lock);

	return ret;

If the I2C write fails, the code jumps to the fail label, bypassing the call to
mma8452_active().

Does this leave the device stuck in STANDBY mode, which breaks all future data
measurements?

[Severity: Medium]
This is a pre-existing issue, but might the mishandling of short I2C block
reads leak uninitialized kernel stack memory?

In mma8452_read():

	ret = i2c_smbus_read_i2c_block_data(data->client, MMA8452_OUT_X,
					    3 * sizeof(__be16), (u8 *)buf);
	if (ret < 0)
		return ret;

	ret = mma8452_set_runtime_pm_state(data->client, false);

	return ret;

i2c_smbus_read_i2c_block_data() returns the number of bytes successfully read.
If a short read occurs, it returns a positive value and passes the if (ret < 0)
check.

The subsequent call to mma8452_set_runtime_pm_state() will overwrite ret with 0
on success.

mma8452_read_raw() assumes the stack array buffer is fully initialized and
processes the uninitialized bytes, which are then returned to userspace.

Should the check verify if exactly the requested number of bytes were read?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260825-mma8452-open-drain-v6-0-9b252804ee80@geanix.com?part=7

  reply	other threads:[~2026-08-25  8:45 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-25  8:27 [PATCH v6 0/9] io: accel: mma8452: Allow open drain interrupt pin configuration Esben Haabendal
2026-08-25  8:27 ` [PATCH v6 1/9] dt-bindings: iio: accel: mma8452: Add drive-open-drain Esben Haabendal
2026-08-25  8:27 ` [PATCH v6 2/9] iio: accel: mma8452: Optimize struct mma8452_data member orders Esben Haabendal
2026-08-25  8:38   ` sashiko-bot
2026-08-25  8:27 ` [PATCH v6 3/9] iio: accel: mma8452: Only apply trigger type when not set by firmware Esben Haabendal
2026-08-25  8:43   ` sashiko-bot
2026-08-25  8:27 ` [PATCH v6 4/9] iio: accel: mma8452: Support interrupt sharing Esben Haabendal
2026-08-25  8:42   ` sashiko-bot
2026-08-25 11:39     ` Esben Haabendal
2026-08-25  8:27 ` [PATCH v6 5/9] iio: accel: mma8452: Allow open drain interrupt pin configuration Esben Haabendal
2026-08-25  8:42   ` sashiko-bot
2026-08-25 13:26     ` Esben Haabendal
2026-08-25  8:27 ` [PATCH v6 6/9] iio: accel: mma8452: Reuse existing dev pointer in mma8452_probe() Esben Haabendal
2026-08-25  8:38   ` sashiko-bot
2026-08-25 11:15     ` Esben Haabendal
2026-08-26  7:27   ` Andy Shevchenko
2026-08-25  8:27 ` [PATCH v6 7/9] iio: accel: mma8452: Drop unneeded lock acquire on read Esben Haabendal
2026-08-25  8:45   ` sashiko-bot [this message]
2026-08-25 10:17   ` Joshua Crofts
2026-08-25 13:35     ` Esben Haabendal
2026-08-25 13:44       ` Joshua Crofts
2026-08-25 14:06         ` Esben Haabendal
2026-08-25  8:27 ` [PATCH v6 8/9] iio: accel: mma8452: Fix use-after-free bug in error error path Esben Haabendal
2026-08-25  8:41   ` sashiko-bot
2026-08-25  8:27 ` [PATCH v6 9/9] iio: accel: mma8452: Use proper error code when missing device model Esben Haabendal
2026-08-25  8:40   ` sashiko-bot
2026-08-25 10:23   ` Joshua Crofts
2026-08-25 11:00     ` Esben Haabendal

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=20260825084504.778091F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=esben@geanix.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox