Linux IIO development
 help / color / mirror / Atom feed
* [PATCH 0/3] iio: light: cm3232: fix remove ordering, locking and calibscale validation
@ 2026-12-03 16:52 Vineet Thakran
  2026-12-03 16:52 ` [PATCH 1/3] iio: light: cm3232: unregister the device before powering it down Vineet Thakran
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Vineet Thakran @ 2026-12-03 16:52 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: linux-iio, Kevin Tsai, David Lechner, Nuno Sá,
	Andy Shevchenko, Vineet Thakran

This series fixes three bugs in the cm3232 ambient light sensor driver,
all present since the driver was added:

1. cm3232_remove() shuts the sensor down before unregistering the IIO
   device, so sysfs accesses in between can read a shut-down sensor or
   turn it back on.
2. Nothing serializes the read-modify-write of the cached command
   register (and calibscale) between read_raw(), write_raw(), suspend
   and resume.
3. in_illuminance_calibscale accepts zero, negative and fractional
   values, which give meaningless lux readings.

Patch 2 uses guard() and devm_mutex_init(), so older stable trees will
need a manual backport of it.

The series is compile-tested only (W=1, sparse, checkpatch --strict);
I don't have CM3232 hardware to test on.

Vineet Thakran (3):
  iio: light: cm3232: unregister the device before powering it down
  iio: light: cm3232: serialize access to the cached command register
  iio: light: cm3232: reject invalid calibscale values

 drivers/iio/light/cm3232.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)


base-commit: e5aad2bdbbee783611d32599f89cea3f12f9593d
-- 
2.43.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/3] iio: light: cm3232: unregister the device before powering it down
  2026-12-03 16:52 [PATCH 0/3] iio: light: cm3232: fix remove ordering, locking and calibscale validation Vineet Thakran
@ 2026-12-03 16:52 ` Vineet Thakran
  2026-12-04  6:59   ` Joshua Crofts
  2026-12-03 16:52 ` [PATCH 2/3] iio: light: cm3232: serialize access to the cached command register Vineet Thakran
  2026-12-03 16:52 ` [PATCH 3/3] iio: light: cm3232: reject invalid calibscale values Vineet Thakran
  2 siblings, 1 reply; 6+ messages in thread
From: Vineet Thakran @ 2026-12-03 16:52 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: linux-iio, Kevin Tsai, David Lechner, Nuno Sá,
	Andy Shevchenko, Vineet Thakran, stable

cm3232_remove() shuts the sensor down before calling
iio_device_unregister(), so the sysfs attributes stay live for a
window after shutdown. A read of in_illuminance_input in that window
reads the ALS register of a sensor that is shut down, and a write to
in_illuminance_integration_time rewrites the command register from the
cached regs_cmd, which does not have CM3232_CMD_ALS_DISABLE set, and so
turns the sensor back on after remove has shut it down.

Unregister the IIO device first. Once iio_device_unregister() returns,
the sysfs attributes are removed and no IIO callback can run, so the
sensor stays shut down.

Fixes: b84894c7f088 ("iio: Added Capella cm3232 ambient light sensor driver.")
Cc: stable@vger.kernel.org
Signed-off-by: Vineet Thakran <vineet789thakran@gmail.com>
---
 drivers/iio/light/cm3232.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/iio/light/cm3232.c b/drivers/iio/light/cm3232.c
index 5f25452633f2..7e81b1c8ce9c 100644
--- a/drivers/iio/light/cm3232.c
+++ b/drivers/iio/light/cm3232.c
@@ -358,10 +358,10 @@ static void cm3232_remove(struct i2c_client *client)
 {
 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
 
+	iio_device_unregister(indio_dev);
+
 	i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
 		CM3232_CMD_ALS_DISABLE);
-
-	iio_device_unregister(indio_dev);
 }
 
 static const struct i2c_device_id cm3232_id[] = {
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/3] iio: light: cm3232: serialize access to the cached command register
  2026-12-03 16:52 [PATCH 0/3] iio: light: cm3232: fix remove ordering, locking and calibscale validation Vineet Thakran
  2026-12-03 16:52 ` [PATCH 1/3] iio: light: cm3232: unregister the device before powering it down Vineet Thakran
@ 2026-12-03 16:52 ` Vineet Thakran
  2026-12-03 16:52 ` [PATCH 3/3] iio: light: cm3232: reject invalid calibscale values Vineet Thakran
  2 siblings, 0 replies; 6+ messages in thread
From: Vineet Thakran @ 2026-12-03 16:52 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: linux-iio, Kevin Tsai, David Lechner, Nuno Sá,
	Andy Shevchenko, Vineet Thakran, stable

The driver keeps a copy of the command register in chip->regs_cmd.
cm3232_write_als_it(), cm3232_suspend() and cm3232_resume() update it
with a read-modify-write followed by an I2C write, and
cm3232_get_lux() and cm3232_read_als_it() derive the integration time
from it, but nothing serializes these paths. The IIO core does not
serialize read_raw() and write_raw(), so concurrent sysfs accesses can
race: two integration time writes can leave regs_cmd out of sync with
the value last written to the device, and a lux read that races with
an integration time write can scale the ALS count of one integration
time by the other. calibscale is updated and used in the same way.

Add a mutex that protects calibscale, regs_cmd, regs_als and the
device register accesses, and take it in read_raw(), write_raw(),
suspend and resume.

Fixes: b84894c7f088 ("iio: Added Capella cm3232 ambient light sensor driver.")
Fixes: 1ec28ce66aa4 ("iio: light: Added PM support for Capella CM3232 ambient light sensor driver.")
Cc: stable@vger.kernel.org
Signed-off-by: Vineet Thakran <vineet789thakran@gmail.com>
---
 drivers/iio/light/cm3232.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/drivers/iio/light/cm3232.c b/drivers/iio/light/cm3232.c
index 7e81b1c8ce9c..6f0ec4ce6207 100644
--- a/drivers/iio/light/cm3232.c
+++ b/drivers/iio/light/cm3232.c
@@ -8,8 +8,10 @@
  * IIO driver for CM3232 (7-bit I2C slave address 0x10).
  */
 
+#include <linux/cleanup.h>
 #include <linux/i2c.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
 #include <linux/init.h>
@@ -67,6 +69,8 @@ static const struct cm3232_als_info cm3232_als_info_default = {
 struct cm3232_chip {
 	struct i2c_client *client;
 	const struct cm3232_als_info *als_info;
+	/* Protects calibscale, regs_cmd, regs_als and the device registers */
+	struct mutex lock;
 	int calibscale;
 	u8 regs_cmd;
 	u16 regs_als;
@@ -237,6 +241,8 @@ static int cm3232_read_raw(struct iio_dev *indio_dev,
 	struct cm3232_chip *chip = iio_priv(indio_dev);
 	int ret;
 
+	guard(mutex)(&chip->lock);
+
 	switch (mask) {
 	case IIO_CHAN_INFO_PROCESSED:
 		ret = cm3232_get_lux(chip);
@@ -260,6 +266,8 @@ static int cm3232_write_raw(struct iio_dev *indio_dev,
 {
 	struct cm3232_chip *chip = iio_priv(indio_dev);
 
+	guard(mutex)(&chip->lock);
+
 	switch (mask) {
 	case IIO_CHAN_INFO_CALIBSCALE:
 		chip->calibscale = val;
@@ -337,6 +345,10 @@ static int cm3232_probe(struct i2c_client *client)
 	chip->client = client;
 	chip->calibscale = CM3232_CALIBSCALE_DEFAULT;
 
+	ret = devm_mutex_init(&client->dev, &chip->lock);
+	if (ret)
+		return ret;
+
 	indio_dev->channels = cm3232_channels;
 	indio_dev->num_channels = ARRAY_SIZE(cm3232_channels);
 	indio_dev->info = &cm3232_info;
@@ -377,6 +389,8 @@ static int cm3232_suspend(struct device *dev)
 	struct i2c_client *client = chip->client;
 	int ret;
 
+	guard(mutex)(&chip->lock);
+
 	chip->regs_cmd |= CM3232_CMD_ALS_DISABLE;
 	ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
 					chip->regs_cmd);
@@ -391,6 +405,8 @@ static int cm3232_resume(struct device *dev)
 	struct i2c_client *client = chip->client;
 	int ret;
 
+	guard(mutex)(&chip->lock);
+
 	chip->regs_cmd &= ~CM3232_CMD_ALS_DISABLE;
 	ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
 					chip->regs_cmd | CM3232_CMD_ALS_RESET);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/3] iio: light: cm3232: reject invalid calibscale values
  2026-12-03 16:52 [PATCH 0/3] iio: light: cm3232: fix remove ordering, locking and calibscale validation Vineet Thakran
  2026-12-03 16:52 ` [PATCH 1/3] iio: light: cm3232: unregister the device before powering it down Vineet Thakran
  2026-12-03 16:52 ` [PATCH 2/3] iio: light: cm3232: serialize access to the cached command register Vineet Thakran
@ 2026-12-03 16:52 ` Vineet Thakran
  2026-12-04  6:58   ` Joshua Crofts
  2 siblings, 1 reply; 6+ messages in thread
From: Vineet Thakran @ 2026-12-03 16:52 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: linux-iio, Kevin Tsai, David Lechner, Nuno Sá,
	Andy Shevchenko, Vineet Thakran, stable

cm3232_write_raw() stores any value written to in_illuminance_calibscale
and ignores the fractional part. A value of zero makes every lux reading
zero. A negative value is converted to a large u64 when cm3232_get_lux()
multiplies by it, so every non-zero reading is clamped to 0xffff. A
fractional value such as 1.5 is silently truncated.

Accept only positive integers and return -EINVAL for anything else. No
upper bound is needed: the largest product in cm3232_get_lux(), with the
shortest integration time, a full-scale ALS count and INT_MAX as
calibscale, still fits in a u64.

Fixes: b84894c7f088 ("iio: Added Capella cm3232 ambient light sensor driver.")
Cc: stable@vger.kernel.org
Signed-off-by: Vineet Thakran <vineet789thakran@gmail.com>
---
 drivers/iio/light/cm3232.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/iio/light/cm3232.c b/drivers/iio/light/cm3232.c
index 6f0ec4ce6207..6f35bab916b6 100644
--- a/drivers/iio/light/cm3232.c
+++ b/drivers/iio/light/cm3232.c
@@ -270,6 +270,8 @@ static int cm3232_write_raw(struct iio_dev *indio_dev,
 
 	switch (mask) {
 	case IIO_CHAN_INFO_CALIBSCALE:
+		if (val <= 0 || val2)
+			return -EINVAL;
 		chip->calibscale = val;
 		return 0;
 	case IIO_CHAN_INFO_INT_TIME:
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 3/3] iio: light: cm3232: reject invalid calibscale values
  2026-12-03 16:52 ` [PATCH 3/3] iio: light: cm3232: reject invalid calibscale values Vineet Thakran
@ 2026-12-04  6:58   ` Joshua Crofts
  0 siblings, 0 replies; 6+ messages in thread
From: Joshua Crofts @ 2026-12-04  6:58 UTC (permalink / raw)
  To: Vineet Thakran, Jonathan Cameron
  Cc: linux-iio, Kevin Tsai, David Lechner, Nuno Sá,
	Andy Shevchenko, stable

On 03-Dec-26 17:52, Vineet Thakran wrote:

...

>  	switch (mask) {
>  	case IIO_CHAN_INFO_CALIBSCALE:
> +		if (val <= 0 || val2)
> +			return -EINVAL;

-ERANGE is better.

-- 
Kind regards,
Joshua Crofts


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/3] iio: light: cm3232: unregister the device before powering it down
  2026-12-03 16:52 ` [PATCH 1/3] iio: light: cm3232: unregister the device before powering it down Vineet Thakran
@ 2026-12-04  6:59   ` Joshua Crofts
  0 siblings, 0 replies; 6+ messages in thread
From: Joshua Crofts @ 2026-12-04  6:59 UTC (permalink / raw)
  To: Vineet Thakran, Jonathan Cameron
  Cc: linux-iio, Kevin Tsai, David Lechner, Nuno Sá,
	Andy Shevchenko, stable

On 03-Dec-26 17:52, Vineet Thakran wrote:
> cm3232_remove() shuts the sensor down before calling
> iio_device_unregister(), so the sysfs attributes stay live for a
> window after shutdown. A read of in_illuminance_input in that window
> reads the ALS register of a sensor that is shut down, and a write to
> in_illuminance_integration_time rewrites the command register from the
> cached regs_cmd, which does not have CM3232_CMD_ALS_DISABLE set, and so
> turns the sensor back on after remove has shut it down.
> 
> Unregister the IIO device first. Once iio_device_unregister() returns,
> the sysfs attributes are removed and no IIO callback can run, so the
> sensor stays shut down.
> 
> Fixes: b84894c7f088 ("iio: Added Capella cm3232 ambient light sensor driver.")
> Cc: stable@vger.kernel.org
> Signed-off-by: Vineet Thakran <vineet789thakran@gmail.com>
> ---
Reviewed-by: Joshua Crofts <crofts@kernel.org>

-- 
Kind regards,
Joshua Crofts


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-12-04  6:59 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 16:52 [PATCH 0/3] iio: light: cm3232: fix remove ordering, locking and calibscale validation Vineet Thakran
2026-12-03 16:52 ` [PATCH 1/3] iio: light: cm3232: unregister the device before powering it down Vineet Thakran
2026-12-04  6:59   ` Joshua Crofts
2026-12-03 16:52 ` [PATCH 2/3] iio: light: cm3232: serialize access to the cached command register Vineet Thakran
2026-12-03 16:52 ` [PATCH 3/3] iio: light: cm3232: reject invalid calibscale values Vineet Thakran
2026-12-04  6:58   ` Joshua Crofts

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox