staging: iio: adc: ad7816: Protect sysfs attributes with mutex
Subsystems (4)
PATCH 1/1 staging: iio: adc: ad7816: Protect sysfs attributes with mutex
The sysfs store functions (ad7816_store_mode and ad7816_store_channel) modify the device state and toggle GPIO pins. If accessed concurrently by userspace during an ongoing SPI transfer, this could corrupt the SPI transaction.
Add the existing chip->lock guard to these functions to serialize access and prevent race conditions.
--- drivers/staging/iio/adc/ad7816.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/staging/iio/adc/ad7816.c +2 -0
@@ -143,6 +143,7 @@ static ssize_t ad7816_store_mode(struct device *dev,struct iio_dev *indio_dev =dev_to_iio_dev (dev);struct ad7816_chip_info *chip =iio_priv (indio_dev); +guard (mutex)(&chip->lock);if (strcmp (buf,"full" ) ==0 ) {gpiod_set_value (chip->rdwr_pin,1 ); chip->mode =AD7816_FULL ;
@@ -207,6 +208,7 @@ static ssize_t ad7816_store_channel(struct device *dev,return -EINVAL ; } +guard (mutex)(&chip->lock); chip->channel_id = data;return len;
Signature · 1 line
2.54.0
On Sat, 19 Sep 2026 11:55:13 +0300
Abdelnasser Hussein <[email protected]> wrote:
...
drivers/staging/iio/adc/ad7816.c static ssize_t ad7816_store_mode(struct device *dev, · line 146
Quoted 5 lines
drivers/staging/iio/adc/ad7816.c +1
-0@@ -143,6 +143,7 @@ static ssize_t ad7816_store_mode(struct device *dev,struct iio_dev *indio_dev =dev_to_iio_dev (dev);struct ad7816_chip_info *chip =iio_priv (indio_dev); +guard (mutex)(&chip->lock);
+ Blank line.
drivers/staging/iio/adc/ad7816.c static ssize_t ad7816_store_channel(struct device *dev, · line 211
Quoted 8 lines
drivers/staging/iio/adc/ad7816.c +1
-0if (strcmp (buf,"full" ) ==0 ) {gpiod_set_value (chip->rdwr_pin,1 ); chip->mode =AD7816_FULL ;@@ -207,6 +208,7 @@ static ssize_t ad7816_store_channel(struct device *dev,return -EINVAL ; } +guard (mutex)(&chip->lock);
+ Blank line.
drivers/staging/iio/adc/ad7816.c static ssize_t ad7816_store_channel(struct device *dev, · line 212
Quoted 1 line
drivers/staging/iio/adc/ad7816.c +0
-0chip->channel_id = data;
With these issues addressed,
Signature · 2 lines
Kind regards, Joshua Crofts
Quoted 10 lines · Abdelnasser Hussein
The sysfs store functions (
ad7816_store_modeandad7816_store_channel) modify the device state and toggle GPIO pins. If accessed concurrently by userspace during an ongoing SPI transfer, this could corrupt the SPI transaction.Add the existing
chip->lockguard to these functions to serialize access and prevent race conditions.Suggested-by Joshua Crofts <[email protected]>Signed-off-by Abdelnasser Hussein <[email protected]>
Add a Fixes tag.
regards,
dan carpenter
Jonathan Cameron
maintainerQuoted 6 lines · On Sat, 19 Sep 2026 13:44:44 +0300 Dan Carpenter <[email protected]> wrote:
Quoted 5 lines · Abdelnasser Hussein
The sysfs store functions (
ad7816_store_modeandad7816_store_channel) modify the device state and toggle GPIO pins. If accessed concurrently by userspace during an ongoing SPI transfer, this could corrupt the SPI transaction.
Quoted 7 lines
Quoted 5 lines
Add the existing
chip->lockguard to these functions to serialize access and prevent race conditions.Suggested-by Joshua Crofts <[email protected]>Signed-off-by Abdelnasser Hussein <[email protected]>Add a Fixes tag.
Hmm - this is a follow up for a few missed cases. The original patch should probably have had a fixes tag as well even though it's in staging and the bug goes back probably a decade or more. Ah well, Abdelnasser,
add one here anyway and send a v2 with the spacing Joshua asked for.
Thanks,
Jonathan
Quoted 4 lines
regards,
dan carpenter