Skip to contentSkip to conversation

staging: iio: adc: ad7816: Protect sysfs attributes with mutex

PATCH Superseded Abdelnasser Hussein 4 participants 4 messages Sep 19 – Sep 20

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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
index f76f0215119a..d8f4b1f79e2a 100644
@@ -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
Reviewed-by on v1 1/1
#1 lore raw

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 -0
 	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);

+ 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 -0
 	chip->channel_id = data;

With these issues addressed,

Signature · 2 lines
Kind regards,
Joshua Crofts
#2 lore raw
In reply to Abdelnasser Hussein
Quoted 10 lines · Abdelnasser Hussein

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.

Add a Fixes tag.

regards,
dan carpenter

maintainer
#3 lore raw
Quoted 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_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.

Quoted 7 lines
Quoted 5 lines

Add the existing chip->lock guard to these functions to serialize access and prevent race conditions.

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