diff mbox series

[v2] iio: stm32-dac: Replace indio_dev->mlock with own device lock

Message ID 20200916092349.75647-1-alexandru.ardelean@analog.com (mailing list archive)
State New, archived
Headers show
Series [v2] iio: stm32-dac: Replace indio_dev->mlock with own device lock | expand

Commit Message

Alexandru Ardelean Sept. 16, 2020, 9:23 a.m. UTC
From: Sergiu Cuciurean <sergiu.cuciurean@analog.com>

As part of the general cleanup of indio_dev->mlock, this change replaces
it with a local lock. The lock protects against potential races when
reading the CR reg and then updating, so that the state of pm_runtime
is consistent between the two operations.

This is part of a bigger cleanup.
Link: https://lore.kernel.org/linux-iio/CA+U=Dsoo6YABe5ODLp+eFNPGFDjk5ZeQEceGkqjxXcVEhLWubw@mail.gmail.com/

Signed-off-by: Sergiu Cuciurean <sergiu.cuciurean@analog.com>
Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
---
 drivers/iio/dac/stm32-dac.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

Comments

Fabrice Gasnier Sept. 16, 2020, 10:18 a.m. UTC | #1
On 9/16/20 11:23 AM, Alexandru Ardelean wrote:
> From: Sergiu Cuciurean <sergiu.cuciurean@analog.com>
> 
> As part of the general cleanup of indio_dev->mlock, this change replaces
> it with a local lock. The lock protects against potential races when
> reading the CR reg and then updating, so that the state of pm_runtime
> is consistent between the two operations.
> 
> This is part of a bigger cleanup.
> Link: https://lore.kernel.org/linux-iio/CA+U=Dsoo6YABe5ODLp+eFNPGFDjk5ZeQEceGkqjxXcVEhLWubw@mail.gmail.com/
> 
> Signed-off-by: Sergiu Cuciurean <sergiu.cuciurean@analog.com>
> Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
> ---
>  drivers/iio/dac/stm32-dac.c | 13 +++++++++----
>  1 file changed, 9 insertions(+), 4 deletions(-)

Hi Alexandru,

Many thanks for this updated patch,

Reviewed-by: Fabrice Gasnier <fabrice.gasnier@st.com>

Best regards,
Fabrice

> 
> diff --git a/drivers/iio/dac/stm32-dac.c b/drivers/iio/dac/stm32-dac.c
> index 092c796fa3d9..12dec68c16f7 100644
> --- a/drivers/iio/dac/stm32-dac.c
> +++ b/drivers/iio/dac/stm32-dac.c
> @@ -26,9 +26,12 @@
>  /**
>   * struct stm32_dac - private data of DAC driver
>   * @common:		reference to DAC common data
> + * @lock:		lock to protect against potential races when reading
> + *			and update CR, to keep it in sync with pm_runtime
>   */
>  struct stm32_dac {
>  	struct stm32_dac_common *common;
> +	struct mutex		lock;
>  };
>  
>  static int stm32_dac_is_enabled(struct iio_dev *indio_dev, int channel)
> @@ -58,10 +61,10 @@ static int stm32_dac_set_enable_state(struct iio_dev *indio_dev, int ch,
>  	int ret;
>  
>  	/* already enabled / disabled ? */
> -	mutex_lock(&indio_dev->mlock);
> +	mutex_lock(&dac->lock);
>  	ret = stm32_dac_is_enabled(indio_dev, ch);
>  	if (ret < 0 || enable == !!ret) {
> -		mutex_unlock(&indio_dev->mlock);
> +		mutex_unlock(&dac->lock);
>  		return ret < 0 ? ret : 0;
>  	}
>  
> @@ -69,13 +72,13 @@ static int stm32_dac_set_enable_state(struct iio_dev *indio_dev, int ch,
>  		ret = pm_runtime_get_sync(dev);
>  		if (ret < 0) {
>  			pm_runtime_put_noidle(dev);
> -			mutex_unlock(&indio_dev->mlock);
> +			mutex_unlock(&dac->lock);
>  			return ret;
>  		}
>  	}
>  
>  	ret = regmap_update_bits(dac->common->regmap, STM32_DAC_CR, msk, en);
> -	mutex_unlock(&indio_dev->mlock);
> +	mutex_unlock(&dac->lock);
>  	if (ret < 0) {
>  		dev_err(&indio_dev->dev, "%s failed\n", en ?
>  			"Enable" : "Disable");
> @@ -327,6 +330,8 @@ static int stm32_dac_probe(struct platform_device *pdev)
>  	indio_dev->info = &stm32_dac_iio_info;
>  	indio_dev->modes = INDIO_DIRECT_MODE;
>  
> +	mutex_init(&dac->lock);
> +
>  	ret = stm32_dac_chan_of_init(indio_dev);
>  	if (ret < 0)
>  		return ret;
>
Jonathan Cameron Sept. 16, 2020, 5:52 p.m. UTC | #2
On Wed, 16 Sep 2020 12:18:02 +0200
Fabrice Gasnier <fabrice.gasnier@st.com> wrote:

> On 9/16/20 11:23 AM, Alexandru Ardelean wrote:
> > From: Sergiu Cuciurean <sergiu.cuciurean@analog.com>
> > 
> > As part of the general cleanup of indio_dev->mlock, this change replaces
> > it with a local lock. The lock protects against potential races when
> > reading the CR reg and then updating, so that the state of pm_runtime
> > is consistent between the two operations.
> > 
> > This is part of a bigger cleanup.
> > Link: https://lore.kernel.org/linux-iio/CA+U=Dsoo6YABe5ODLp+eFNPGFDjk5ZeQEceGkqjxXcVEhLWubw@mail.gmail.com/
> > 
> > Signed-off-by: Sergiu Cuciurean <sergiu.cuciurean@analog.com>
> > Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
> > ---
> >  drivers/iio/dac/stm32-dac.c | 13 +++++++++----
> >  1 file changed, 9 insertions(+), 4 deletions(-)  
> 
> Hi Alexandru,
> 
> Many thanks for this updated patch,
> 
> Reviewed-by: Fabrice Gasnier <fabrice.gasnier@st.com>

Applied to the togreg branch of iio.git and pushed out as testing for
the autobuilders to poke at it.

Thanks,

Jonathan

> 
> Best regards,
> Fabrice
> 
> > 
> > diff --git a/drivers/iio/dac/stm32-dac.c b/drivers/iio/dac/stm32-dac.c
> > index 092c796fa3d9..12dec68c16f7 100644
> > --- a/drivers/iio/dac/stm32-dac.c
> > +++ b/drivers/iio/dac/stm32-dac.c
> > @@ -26,9 +26,12 @@
> >  /**
> >   * struct stm32_dac - private data of DAC driver
> >   * @common:		reference to DAC common data
> > + * @lock:		lock to protect against potential races when reading
> > + *			and update CR, to keep it in sync with pm_runtime
> >   */
> >  struct stm32_dac {
> >  	struct stm32_dac_common *common;
> > +	struct mutex		lock;
> >  };
> >  
> >  static int stm32_dac_is_enabled(struct iio_dev *indio_dev, int channel)
> > @@ -58,10 +61,10 @@ static int stm32_dac_set_enable_state(struct iio_dev *indio_dev, int ch,
> >  	int ret;
> >  
> >  	/* already enabled / disabled ? */
> > -	mutex_lock(&indio_dev->mlock);
> > +	mutex_lock(&dac->lock);
> >  	ret = stm32_dac_is_enabled(indio_dev, ch);
> >  	if (ret < 0 || enable == !!ret) {
> > -		mutex_unlock(&indio_dev->mlock);
> > +		mutex_unlock(&dac->lock);
> >  		return ret < 0 ? ret : 0;
> >  	}
> >  
> > @@ -69,13 +72,13 @@ static int stm32_dac_set_enable_state(struct iio_dev *indio_dev, int ch,
> >  		ret = pm_runtime_get_sync(dev);
> >  		if (ret < 0) {
> >  			pm_runtime_put_noidle(dev);
> > -			mutex_unlock(&indio_dev->mlock);
> > +			mutex_unlock(&dac->lock);
> >  			return ret;
> >  		}
> >  	}
> >  
> >  	ret = regmap_update_bits(dac->common->regmap, STM32_DAC_CR, msk, en);
> > -	mutex_unlock(&indio_dev->mlock);
> > +	mutex_unlock(&dac->lock);
> >  	if (ret < 0) {
> >  		dev_err(&indio_dev->dev, "%s failed\n", en ?
> >  			"Enable" : "Disable");
> > @@ -327,6 +330,8 @@ static int stm32_dac_probe(struct platform_device *pdev)
> >  	indio_dev->info = &stm32_dac_iio_info;
> >  	indio_dev->modes = INDIO_DIRECT_MODE;
> >  
> > +	mutex_init(&dac->lock);
> > +
> >  	ret = stm32_dac_chan_of_init(indio_dev);
> >  	if (ret < 0)
> >  		return ret;
> >
diff mbox series

Patch

diff --git a/drivers/iio/dac/stm32-dac.c b/drivers/iio/dac/stm32-dac.c
index 092c796fa3d9..12dec68c16f7 100644
--- a/drivers/iio/dac/stm32-dac.c
+++ b/drivers/iio/dac/stm32-dac.c
@@ -26,9 +26,12 @@ 
 /**
  * struct stm32_dac - private data of DAC driver
  * @common:		reference to DAC common data
+ * @lock:		lock to protect against potential races when reading
+ *			and update CR, to keep it in sync with pm_runtime
  */
 struct stm32_dac {
 	struct stm32_dac_common *common;
+	struct mutex		lock;
 };
 
 static int stm32_dac_is_enabled(struct iio_dev *indio_dev, int channel)
@@ -58,10 +61,10 @@  static int stm32_dac_set_enable_state(struct iio_dev *indio_dev, int ch,
 	int ret;
 
 	/* already enabled / disabled ? */
-	mutex_lock(&indio_dev->mlock);
+	mutex_lock(&dac->lock);
 	ret = stm32_dac_is_enabled(indio_dev, ch);
 	if (ret < 0 || enable == !!ret) {
-		mutex_unlock(&indio_dev->mlock);
+		mutex_unlock(&dac->lock);
 		return ret < 0 ? ret : 0;
 	}
 
@@ -69,13 +72,13 @@  static int stm32_dac_set_enable_state(struct iio_dev *indio_dev, int ch,
 		ret = pm_runtime_get_sync(dev);
 		if (ret < 0) {
 			pm_runtime_put_noidle(dev);
-			mutex_unlock(&indio_dev->mlock);
+			mutex_unlock(&dac->lock);
 			return ret;
 		}
 	}
 
 	ret = regmap_update_bits(dac->common->regmap, STM32_DAC_CR, msk, en);
-	mutex_unlock(&indio_dev->mlock);
+	mutex_unlock(&dac->lock);
 	if (ret < 0) {
 		dev_err(&indio_dev->dev, "%s failed\n", en ?
 			"Enable" : "Disable");
@@ -327,6 +330,8 @@  static int stm32_dac_probe(struct platform_device *pdev)
 	indio_dev->info = &stm32_dac_iio_info;
 	indio_dev->modes = INDIO_DIRECT_MODE;
 
+	mutex_init(&dac->lock);
+
 	ret = stm32_dac_chan_of_init(indio_dev);
 	if (ret < 0)
 		return ret;