Skip to contentSkip to conversation

iio: adc: max1363: replace mlock with own lock

PATCH Superseded Rohit Sarkar 3 participants 5 messages Feb 27, 2020 – Feb 28, 2020 IIO

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 iio: adc: max1363: replace mlock with own lock

This change replaces indio_dev's mlock with the drivers own lock. In each case the lock is needed to protect the driver's own state.

Changes from v1:
Fix indentation.
Add a mutex_init() in the probe function.

---
 drivers/iio/adc/max1363.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
drivers/iio/adc/max1363.c +7 -6
index 5c2cc61b666e..b9557f957f3c 100644
@@ -169,6 +169,7 @@ struct max1363_state {
 	const struct max1363_mode	*current_mode;
 	u32				requestedmask;
 	struct regulator		*reg;
+	struct mutex lock;
 
 	/* Using monitor modes and buffer at the same time is
 	   currently not supported */
@@ -364,7 +365,7 @@ static int max1363_read_single_chan(struct iio_dev *indio_dev,
 	struct max1363_state *st = iio_priv(indio_dev);
 	struct i2c_client *client = st->client;
 
-	mutex_lock(&indio_dev->mlock);
+	mutex_lock(&st->lock);
 	/*
 	 * If monitor mode is enabled, the method for reading a single
 	 * channel will have to be rather different and has not yet
@@ -405,7 +406,7 @@ static int max1363_read_single_chan(struct iio_dev *indio_dev,
 	}
 	*val = data;
 error_ret:
-	mutex_unlock(&indio_dev->mlock);
+	mutex_unlock(&st->lock);
 	return ret;
 
 }
@@ -705,9 +706,9 @@ static ssize_t max1363_monitor_store_freq(struct device *dev,
 	if (!found)
 		return -EINVAL;
 
-	mutex_lock(&indio_dev->mlock);
+	mutex_lock(&st->mlock);
 	st->monitor_speed = i;
-	mutex_unlock(&indio_dev->mlock);
+	mutex_unlock(&st->mlock);
 
 	return 0;
 }
@@ -810,12 +811,12 @@ static int max1363_read_event_config(struct iio_dev *indio_dev,
 	int val;
 	int number = chan->channel;
 
-	mutex_lock(&indio_dev->mlock);
+	mutex_lock(&st->mlock);
 	if (dir == IIO_EV_DIR_FALLING)
 		val = (1 << number) & st->mask_low;
 	else
 		val = (1 << number) & st->mask_high;
-	mutex_unlock(&indio_dev->mlock);
+	mutex_unlock(&st->mlock);
 
 	return val;
 }
Signature · 1 line
2.23.0.385.gbc12974a89
#2 lore raw
In reply to Rohit Sarkar
Quoted 7 lines · Rohit Sarkar

This change replaces indio_dev's mlock with the drivers own lock. In each case the lock is needed to protect the driver's own state.

Changes from v1:
Fix indentation.
Add a mutex_init() in the probe function.

This looks like the first patch.
I don't see the code changes from the first one.

Quoted 66 lines
---
 drivers/iio/adc/max1363.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
drivers/iio/adc/max1363.c +2 -1
index 5c2cc61b666e..b9557f957f3c 100644
@@ -169,6 +169,7 @@ struct max1363_state {
 	const struct max1363_mode	*current_mode;
 	u32				requestedmask;
 	struct regulator		*reg;
+	struct mutex lock;
 
 	/* Using monitor modes and buffer at the same time is
 	   currently not supported */
@@ -364,7 +365,7 @@ static int max1363_read_single_chan(struct iio_dev
 *indio_dev,
 	struct max1363_state *st = iio_priv(indio_dev);
 	struct i2c_client *client = st->client;
 
-	mutex_lock(&indio_dev->mlock);
+	mutex_lock(&st->lock);
 	/*
 	 * If monitor mode is enabled, the method for reading a single
drivers/iio/adc/max1363.c +1 -1
 	 * channel will have to be rather different and has not yet
@@ -405,7 +406,7 @@ static int max1363_read_single_chan(struct iio_dev
 *indio_dev,
 	}
 	*val = data;
 error_ret:
-	mutex_unlock(&indio_dev->mlock);
+	mutex_unlock(&st->lock);
 	return ret;
 

}

drivers/iio/adc/max1363.c +2 -2
@@ -705,9 +706,9 @@ static ssize_t max1363_monitor_store_freq(struct device
 *dev,
 	if (!found)
 		return -EINVAL;
 
-	mutex_lock(&indio_dev->mlock);
+	mutex_lock(&st->mlock);
 	st->monitor_speed = i;
-	mutex_unlock(&indio_dev->mlock);
+	mutex_unlock(&st->mlock);
 
 	return 0;

}

drivers/iio/adc/max1363.c +2 -2
@@ -810,12 +811,12 @@ static int max1363_read_event_config(struct iio_dev
 *indio_dev,
 	int val;
 	int number = chan->channel;
 
-	mutex_lock(&indio_dev->mlock);
+	mutex_lock(&st->mlock);
 	if (dir == IIO_EV_DIR_FALLING)
 		val = (1 << number) & st->mask_low;
 	else
 		val = (1 << number) & st->mask_high;
-	mutex_unlock(&indio_dev->mlock);
+	mutex_unlock(&st->mlock);
 
 	return val;

}

#3 lore raw
Quoted 4 lines · Alexandru
Quoted 3 lines · Rohit Sarkar

This change replaces indio_dev's mlock with the drivers own lock. In each case the lock is needed to protect the driver's own state.

Quoted 8 lines
Quoted 4 lines

Changes from v1:
Fix indentation.
Add a mutex_init() in the probe function.

This looks like the first patch.
I don't see the code changes from the first one.

I messed up sorry. Sent out v2 again.
Thanks,
Rohit

#4 lore raw
In reply to Rohit Sarkar
Quoted 8 lines · On Fri, 28 Feb 2020 00:38:37 +0530 Rohit Sarkar <[email protected]>…

This change replaces indio_dev's mlock with the drivers own lock. In each case the lock is needed to protect the driver's own state.

Changes from v1:
Fix indentation.
Add a mutex_init() in the probe function.

Worth taking into account that perhaps some of the mlock cases aren't actually taking it for local purposes, but rather to explicitly stop the core from changing between buffered and polled modes (chrdev and sysfs access).

drivers/iio/adc/max1363.c struct max1363_state { · line 172

Quoted 13 lines
---
 drivers/iio/adc/max1363.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
drivers/iio/adc/max1363.c +1 -0
index 5c2cc61b666e..b9557f957f3c 100644
@@ -169,6 +169,7 @@ struct max1363_state {
 	const struct max1363_mode	*current_mode;
 	u32				requestedmask;
 	struct regulator		*reg;
+	struct mutex lock;

Scope of locks (what they protect) should always be described. So please add documentation explaining exactly what this is protecting.

drivers/iio/adc/max1363.c static int max1363_read_single_chan(struct iio_dev *indio_dev, · line 368

Quoted 9 lines
drivers/iio/adc/max1363.c +1 -1
 	/* Using monitor modes and buffer at the same time is
 	   currently not supported */
@@ -364,7 +365,7 @@ static int max1363_read_single_chan(struct iio_dev *indio_dev,
 	struct max1363_state *st = iio_priv(indio_dev);
 	struct i2c_client *client = st->client;
 
-	mutex_lock(&indio_dev->mlock);
+	mutex_lock(&st->lock);

This one is actually about preventing state changes. So it shouldn't be directly accessing the lock, but it should be calling iio_device_claim_direct_mode. Take a look at what else that function does.

For this driver things are more complex than normal though as we need to prevent either switching between polled and buffer mode or trying to sample with the monitor running.

Hence we may also need to take the local lock to protect the monitor_mode flag.

Quoted 29 lines
drivers/iio/adc/max1363.c +3 -4
 	/*
 	 * If monitor mode is enabled, the method for reading a single
 	 * channel will have to be rather different and has not yet
@@ -405,7 +406,7 @@ static int max1363_read_single_chan(struct iio_dev *indio_dev,
 	}
 	*val = data;
 error_ret:
-	mutex_unlock(&indio_dev->mlock);
+	mutex_unlock(&st->lock);
 	return ret;
 
 }
@@ -705,9 +706,9 @@ static ssize_t max1363_monitor_store_freq(struct device *dev,
 	if (!found)
 		return -EINVAL;
 
-	mutex_lock(&indio_dev->mlock);
+	mutex_lock(&st->mlock);
 	st->monitor_speed = i;
-	mutex_unlock(&indio_dev->mlock);
+	mutex_unlock(&st->mlock);
 
 	return 0;
 }
@@ -810,12 +811,12 @@ static int max1363_read_event_config(struct iio_dev *indio_dev,
 	int val;
 	int number = chan->channel;
 
-	mutex_lock(&indio_dev->mlock);

drivers/iio/adc/max1363.c static int max1363_read_event_config(struct iio_dev *indio_dev, · line 822

Quoted 10 lines
drivers/iio/adc/max1363.c +2 -1
+	mutex_lock(&st->mlock);
 	if (dir == IIO_EV_DIR_FALLING)
 		val = (1 << number) & st->mask_low;
 	else
 		val = (1 << number) & st->mask_high;
-	mutex_unlock(&indio_dev->mlock);
+	mutex_unlock(&st->mlock);
 
 	return val;
 }

Nothing for write_event_config?
That has the same problem that it should only be possible to change monitor mode if we aren't running in buffered mode. Hence it should try to claim direct mode and if it fails return an error.

Fiddly stuff :)

Thanks,

Jonathan