diff mbox series

[12/15] iio: fyro: itg3200_core: do not use internal iio_dev lock

Message ID 20220920112821.975359-13-nuno.sa@analog.com (mailing list archive)
State Changes Requested
Headers show
Series Make 'mlock' really private | expand

Commit Message

Nuno Sa Sept. 20, 2022, 11:28 a.m. UTC
The iio_device lock is only meant for internal use. Hence define a
device local lock to protect against concurrent accesses.

Signed-off-by: Nuno Sá <nuno.sa@analog.com>
---
 drivers/iio/gyro/itg3200_core.c  | 9 ++++++---
 include/linux/iio/gyro/itg3200.h | 2 ++
 2 files changed, 8 insertions(+), 3 deletions(-)

Comments

Jonathan Cameron Sept. 24, 2022, 3:43 p.m. UTC | #1
On Tue, 20 Sep 2022 13:28:18 +0200
Nuno Sá <nuno.sa@analog.com> wrote:

> The iio_device lock is only meant for internal use. Hence define a
> device local lock to protect against concurrent accesses.
> 
> Signed-off-by: Nuno Sá <nuno.sa@analog.com>
> ---
>  drivers/iio/gyro/itg3200_core.c  | 9 ++++++---
>  include/linux/iio/gyro/itg3200.h | 2 ++
>  2 files changed, 8 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/iio/gyro/itg3200_core.c b/drivers/iio/gyro/itg3200_core.c
> index 0491c64e1b32..358aa8ac0c6b 100644
> --- a/drivers/iio/gyro/itg3200_core.c
> +++ b/drivers/iio/gyro/itg3200_core.c
> @@ -18,6 +18,7 @@
>  #include <linux/slab.h>
>  #include <linux/stat.h>
>  #include <linux/module.h>
> +#include <linux/mutex.h>
>  #include <linux/delay.h>
>  
>  #include <linux/iio/iio.h>
> @@ -131,6 +132,7 @@ static int itg3200_write_raw(struct iio_dev *indio_dev,
>  			     int val2,
>  			     long mask)
>  {
> +	struct itg3200 *st = iio_priv(indio_dev);
>  	int ret;
>  	u8 t;
>  
> @@ -139,11 +141,11 @@ static int itg3200_write_raw(struct iio_dev *indio_dev,
>  		if (val == 0 || val2 != 0)
>  			return -EINVAL;
>  
> -		mutex_lock(&indio_dev->mlock);
> +		mutex_lock(&st->lock);
>  
>  		ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &t);
>  		if (ret) {
> -			mutex_unlock(&indio_dev->mlock);
> +			mutex_unlock(&st->lock);
>  			return ret;
>  		}
>  		t = ((t & ITG3200_DLPF_CFG_MASK) ? 1000u : 8000u) / val - 1;
> @@ -152,7 +154,7 @@ static int itg3200_write_raw(struct iio_dev *indio_dev,
>  					  ITG3200_REG_SAMPLE_RATE_DIV,
>  					  t);
>  
> -		mutex_unlock(&indio_dev->mlock);
> +		mutex_unlock(&st->lock);
>  		return ret;
>  
>  	default:
> @@ -307,6 +309,7 @@ static int itg3200_probe(struct i2c_client *client,
>  		return -ENOMEM;
>  
>  	st = iio_priv(indio_dev);
> +	mutex_init(&st->lock);
>  
>  	ret = iio_read_mount_matrix(&client->dev, &st->orientation);
>  	if (ret)
> diff --git a/include/linux/iio/gyro/itg3200.h b/include/linux/iio/gyro/itg3200.h
> index a602fe7b84fa..74b6d1cadc86 100644
> --- a/include/linux/iio/gyro/itg3200.h
> +++ b/include/linux/iio/gyro/itg3200.h
> @@ -102,6 +102,8 @@ struct itg3200 {
>  	struct i2c_client	*i2c;
>  	struct iio_trigger	*trig;
>  	struct iio_mount_matrix orientation;
> +	/* lock to protect against multiple access to the device */
> +	struct mutex		lock;
>  };
>  
>  enum ITG3200_SCAN_INDEX {
Jonathan Cameron Sept. 24, 2022, 3:46 p.m. UTC | #2
On Tue, 20 Sep 2022 13:28:18 +0200
Nuno Sá <nuno.sa@analog.com> wrote:

> The iio_device lock is only meant for internal use. Hence define a
> device local lock to protect against concurrent accesses.
As with earlier, add a note about mutex.h include.

Otherwise, looks fine.  My gut feeling is that we should
have a claim_direct_mode() in that write_raw() as well as
changing frequency whilst doing buffered capture is rarely a good
idea, but meh, we don't need to change the ABI so let us not do so.

Jonathan

> 
> Signed-off-by: Nuno Sá <nuno.sa@analog.com>
> ---
>  drivers/iio/gyro/itg3200_core.c  | 9 ++++++---
>  include/linux/iio/gyro/itg3200.h | 2 ++
>  2 files changed, 8 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/iio/gyro/itg3200_core.c b/drivers/iio/gyro/itg3200_core.c
> index 0491c64e1b32..358aa8ac0c6b 100644
> --- a/drivers/iio/gyro/itg3200_core.c
> +++ b/drivers/iio/gyro/itg3200_core.c
> @@ -18,6 +18,7 @@
>  #include <linux/slab.h>
>  #include <linux/stat.h>
>  #include <linux/module.h>
> +#include <linux/mutex.h>
>  #include <linux/delay.h>
>  
>  #include <linux/iio/iio.h>
> @@ -131,6 +132,7 @@ static int itg3200_write_raw(struct iio_dev *indio_dev,
>  			     int val2,
>  			     long mask)
>  {
> +	struct itg3200 *st = iio_priv(indio_dev);
>  	int ret;
>  	u8 t;
>  
> @@ -139,11 +141,11 @@ static int itg3200_write_raw(struct iio_dev *indio_dev,
>  		if (val == 0 || val2 != 0)
>  			return -EINVAL;
>  
> -		mutex_lock(&indio_dev->mlock);
> +		mutex_lock(&st->lock);
>  
>  		ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &t);
>  		if (ret) {
> -			mutex_unlock(&indio_dev->mlock);
> +			mutex_unlock(&st->lock);
>  			return ret;
>  		}
>  		t = ((t & ITG3200_DLPF_CFG_MASK) ? 1000u : 8000u) / val - 1;
> @@ -152,7 +154,7 @@ static int itg3200_write_raw(struct iio_dev *indio_dev,
>  					  ITG3200_REG_SAMPLE_RATE_DIV,
>  					  t);
>  
> -		mutex_unlock(&indio_dev->mlock);
> +		mutex_unlock(&st->lock);
>  		return ret;
>  
>  	default:
> @@ -307,6 +309,7 @@ static int itg3200_probe(struct i2c_client *client,
>  		return -ENOMEM;
>  
>  	st = iio_priv(indio_dev);
> +	mutex_init(&st->lock);
>  
>  	ret = iio_read_mount_matrix(&client->dev, &st->orientation);
>  	if (ret)
> diff --git a/include/linux/iio/gyro/itg3200.h b/include/linux/iio/gyro/itg3200.h
> index a602fe7b84fa..74b6d1cadc86 100644
> --- a/include/linux/iio/gyro/itg3200.h
> +++ b/include/linux/iio/gyro/itg3200.h
> @@ -102,6 +102,8 @@ struct itg3200 {
>  	struct i2c_client	*i2c;
>  	struct iio_trigger	*trig;
>  	struct iio_mount_matrix orientation;
> +	/* lock to protect against multiple access to the device */
> +	struct mutex		lock;
>  };
>  
>  enum ITG3200_SCAN_INDEX {
diff mbox series

Patch

diff --git a/drivers/iio/gyro/itg3200_core.c b/drivers/iio/gyro/itg3200_core.c
index 0491c64e1b32..358aa8ac0c6b 100644
--- a/drivers/iio/gyro/itg3200_core.c
+++ b/drivers/iio/gyro/itg3200_core.c
@@ -18,6 +18,7 @@ 
 #include <linux/slab.h>
 #include <linux/stat.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/delay.h>
 
 #include <linux/iio/iio.h>
@@ -131,6 +132,7 @@  static int itg3200_write_raw(struct iio_dev *indio_dev,
 			     int val2,
 			     long mask)
 {
+	struct itg3200 *st = iio_priv(indio_dev);
 	int ret;
 	u8 t;
 
@@ -139,11 +141,11 @@  static int itg3200_write_raw(struct iio_dev *indio_dev,
 		if (val == 0 || val2 != 0)
 			return -EINVAL;
 
-		mutex_lock(&indio_dev->mlock);
+		mutex_lock(&st->lock);
 
 		ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &t);
 		if (ret) {
-			mutex_unlock(&indio_dev->mlock);
+			mutex_unlock(&st->lock);
 			return ret;
 		}
 		t = ((t & ITG3200_DLPF_CFG_MASK) ? 1000u : 8000u) / val - 1;
@@ -152,7 +154,7 @@  static int itg3200_write_raw(struct iio_dev *indio_dev,
 					  ITG3200_REG_SAMPLE_RATE_DIV,
 					  t);
 
-		mutex_unlock(&indio_dev->mlock);
+		mutex_unlock(&st->lock);
 		return ret;
 
 	default:
@@ -307,6 +309,7 @@  static int itg3200_probe(struct i2c_client *client,
 		return -ENOMEM;
 
 	st = iio_priv(indio_dev);
+	mutex_init(&st->lock);
 
 	ret = iio_read_mount_matrix(&client->dev, &st->orientation);
 	if (ret)
diff --git a/include/linux/iio/gyro/itg3200.h b/include/linux/iio/gyro/itg3200.h
index a602fe7b84fa..74b6d1cadc86 100644
--- a/include/linux/iio/gyro/itg3200.h
+++ b/include/linux/iio/gyro/itg3200.h
@@ -102,6 +102,8 @@  struct itg3200 {
 	struct i2c_client	*i2c;
 	struct iio_trigger	*trig;
 	struct iio_mount_matrix orientation;
+	/* lock to protect against multiple access to the device */
+	struct mutex		lock;
 };
 
 enum ITG3200_SCAN_INDEX {