Skip to contentSkip to conversation

staging: iio: meter: ade7758_core: Replace mlock with driver's private lock

PATCH In review sayli karnik 2 participants 2 messages Mar 21, 2017 – Mar 22, 2017

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 staging: iio: meter: ade7758_core: Replace mlock with driver's private lock

iio_dev->mlock should be used by the IIO core only for protecting device operating mode changes. ie. Changes between INDIO_DIRECT_MODE, INDIO_BUFFER_* modes.
Replace mlock with a lock in the device's global data to protect hardware state changes. Declare a new lock to avoid deadlocks due to nested mutex locks.

---
Changes in v2:
Declared a new lock for raw reads/writes

 drivers/staging/iio/meter/ade7758.h      |  4 +++-
 drivers/staging/iio/meter/ade7758_core.c | 12 +++++++-----
 2 files changed, 10 insertions(+), 6 deletions(-)
drivers/staging/iio/meter/ade7758.h +3 -1
index 6ae78d8..e45887e 100644
@@ -112,13 +112,15 @@
  * @tx:			transmit buffer
  * @rx:			receive buffer
  * @buf_lock:		mutex to protect tx and rx
+ * @lock:		mutex to protect raw reads and writes
  **/
 struct ade7758_state {
 	struct spi_device	*us;
 	struct iio_trigger	*trig;
 	u8			*tx;
 	u8			*rx;
-	struct mutex		buf_lock;
+	struct mutex		buf_lock; /* protect buffer reads/writes */
+	struct mutex		lock;	/* protect raw reads/writes */
 	struct spi_transfer	ring_xfer[4];
 	struct spi_message	ring_msg;
 	/*
drivers/staging/iio/meter/ade7758_core.c +7 -5
index 99c89e6..0996d74 100644
@@ -522,13 +522,14 @@ static int ade7758_read_raw(struct iio_dev *indio_dev,
 			    int *val2,
 			    long mask)
 {
+	struct ade7758_state *st = iio_priv(indio_dev);
 	int ret;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_SAMP_FREQ:
-		mutex_lock(&indio_dev->mlock);
+		mutex_lock(&st->lock);
 		ret = ade7758_read_samp_freq(&indio_dev->dev, val);
-		mutex_unlock(&indio_dev->mlock);
+		mutex_unlock(&st->lock);
 		return ret;
 	default:
 		return -EINVAL;
@@ -541,15 +542,16 @@ static int ade7758_write_raw(struct iio_dev *indio_dev,
 			     struct iio_chan_spec const *chan,
 			     int val, int val2, long mask)
 {
+	struct ade7758_state *st = iio_priv(indio_dev);
 	int ret;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		if (val2)
 			return -EINVAL;
-		mutex_lock(&indio_dev->mlock);
+		mutex_lock(&st->buf_lock);
 		ret = ade7758_write_samp_freq(&indio_dev->dev, val);
-		mutex_unlock(&indio_dev->mlock);
+		mutex_unlock(&st->buf_lock);
 		return ret;
 	default:
 		return -EINVAL;
@@ -855,7 +857,7 @@ static int ade7758_probe(struct spi_device *spi)
 	}
 	st->us = spi;
 	mutex_init(&st->buf_lock);
-
+	mutex_init(&st->lock);
 	indio_dev->name = spi->dev.driver->name;
 	indio_dev->dev.parent = &spi->dev;
 	indio_dev->info = &ade7758_info;
Signature · 1 line
2.7.4
maintainer
#1 lore raw
Quoted 8 lines · sayli karnik

iio_dev->mlock should be used by the IIO core only for protecting device operating mode changes. ie. Changes between INDIO_DIRECT_MODE, INDIO_BUFFER_* modes.
Replace mlock with a lock in the device's global data to protect hardware state changes. Declare a new lock to avoid deadlocks due to nested mutex locks.

Talk me through the logic of which lock you have used where...

Quoted 53 lines

---
Changes in v2:
Declared a new lock for raw reads/writes

 drivers/staging/iio/meter/ade7758.h      |  4 +++-
 drivers/staging/iio/meter/ade7758_core.c | 12 +++++++-----
 2 files changed, 10 insertions(+), 6 deletions(-)
drivers/staging/iio/meter/ade7758.h +3 -1
index 6ae78d8..e45887e 100644
@@ -112,13 +112,15 @@
  * @tx:			transmit buffer
  * @rx:			receive buffer
  * @buf_lock:		mutex to protect tx and rx
+ * @lock:		mutex to protect raw reads and writes
  **/
 struct ade7758_state {
 	struct spi_device	*us;
 	struct iio_trigger	*trig;
 	u8			*tx;
 	u8			*rx;
-	struct mutex		buf_lock;
+	struct mutex		buf_lock; /* protect buffer reads/writes */
+	struct mutex		lock;	/* protect raw reads/writes */
 	struct spi_transfer	ring_xfer[4];
 	struct spi_message	ring_msg;
 	/*
drivers/staging/iio/meter/ade7758_core.c +3 -2
index 99c89e6..0996d74 100644
@@ -522,13 +522,14 @@ static int ade7758_read_raw(struct iio_dev *indio_dev,
 			    int *val2,
 			    long mask)
 {
+	struct ade7758_state *st = iio_priv(indio_dev);
 	int ret;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_SAMP_FREQ:
-		mutex_lock(&indio_dev->mlock);
+		mutex_lock(&st->lock);
 		ret = ade7758_read_samp_freq(&indio_dev->dev, val);
-		mutex_unlock(&indio_dev->mlock);
+		mutex_unlock(&st->lock);
 		return ret;
 	default:
 		return -EINVAL;
@@ -541,15 +542,16 @@ static int ade7758_write_raw(struct iio_dev *indio_dev,
 			     struct iio_chan_spec const *chan,
 			     int val, int val2, long mask)

drivers/staging/iio/meter/ade7758_core.c static int ade7758_write_raw(struct iio_dev *indio_dev, · line 552

Quoted 10 lines
drivers/staging/iio/meter/ade7758_core.c +2 -1
 {
+	struct ade7758_state *st = iio_priv(indio_dev);
 	int ret;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		if (val2)
 			return -EINVAL;
-		mutex_lock(&indio_dev->mlock);
+		mutex_lock(&st->buf_lock);

Why buf_lock here?

Quoted 16 lines
drivers/staging/iio/meter/ade7758_core.c +2 -2
 		ret = ade7758_write_samp_freq(&indio_dev->dev, val);
-		mutex_unlock(&indio_dev->mlock);
+		mutex_unlock(&st->buf_lock);
 		return ret;
 	default:
 		return -EINVAL;
@@ -855,7 +857,7 @@ static int ade7758_probe(struct spi_device *spi)
 	}
 	st->us = spi;
 	mutex_init(&st->buf_lock);
-
+	mutex_init(&st->lock);
 	indio_dev->name = spi->dev.driver->name;
 	indio_dev->dev.parent = &spi->dev;
 	indio_dev->info = &ade7758_info;