diff mbox series

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

Message ID 20200826120042.200364-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 Aug. 26, 2020, noon 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.

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

Comments

Alexandru Ardelean Aug. 27, 2020, 8:55 a.m. UTC | #1
On Wed, Aug 26, 2020 at 3:03 PM Alexandru Ardelean
<alexandru.ardelean@analog.com> 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.
>
> Signed-off-by: Sergiu Cuciurean <sergiu.cuciurean@analog.com>
> Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
> ---

Forgot the changelog here.
Apologies.

Changelog v1 -> v2:
* removed whitespace change for 'common' field
* updated comment about the lock usage

>  drivers/iio/dac/stm32-dac.c | 12 ++++++++----
>  1 file changed, 8 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/iio/dac/stm32-dac.c b/drivers/iio/dac/stm32-dac.c
> index 092c796fa3d9..7a8aed476850 100644
> --- a/drivers/iio/dac/stm32-dac.c
> +++ b/drivers/iio/dac/stm32-dac.c
> @@ -26,9 +26,11 @@
>  /**
>   * struct stm32_dac - private data of DAC driver
>   * @common:            reference to DAC common data
> + * @lock:              lock to protect the data buffer during regmap ops
>   */
>  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 +60,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 +71,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 +329,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;
> --
> 2.25.1
>
Fabrice Gasnier Aug. 27, 2020, 9:03 a.m. UTC | #2
On 8/27/20 10:55 AM, Alexandru Ardelean wrote:
> On Wed, Aug 26, 2020 at 3:03 PM Alexandru Ardelean
> <alexandru.ardelean@analog.com> 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.
>>
>> Signed-off-by: Sergiu Cuciurean <sergiu.cuciurean@analog.com>
>> Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
>> ---
> Forgot the changelog here.
> Apologies.
> 
> Changelog v1 -> v2:
> * removed whitespace change for 'common' field
> * updated comment about the lock usage

Hi Alexandru,

Sorry if I missed it... is there an update on the comment :-) ?

Best Regards,
Fabrice
> 
>>  drivers/iio/dac/stm32-dac.c | 12 ++++++++----
>>  1 file changed, 8 insertions(+), 4 deletions(-)
>>
>> diff --git a/drivers/iio/dac/stm32-dac.c b/drivers/iio/dac/stm32-dac.c
>> index 092c796fa3d9..7a8aed476850 100644
>> --- a/drivers/iio/dac/stm32-dac.c
>> +++ b/drivers/iio/dac/stm32-dac.c
>> @@ -26,9 +26,11 @@
>>  /**
>>   * struct stm32_dac - private data of DAC driver
>>   * @common:            reference to DAC common data
>> + * @lock:              lock to protect the data buffer during regmap ops
>>   */
>>  struct stm32_dac {
>>         struct stm32_dac_common *common;
>> +       struct mutex            lock;
>>  };
Alexandru Ardelean Aug. 27, 2020, 10 a.m. UTC | #3
On Thu, Aug 27, 2020 at 12:03 PM Fabrice Gasnier <fabrice.gasnier@st.com> wrote:
>
> On 8/27/20 10:55 AM, Alexandru Ardelean wrote:
> > On Wed, Aug 26, 2020 at 3:03 PM Alexandru Ardelean
> > <alexandru.ardelean@analog.com> 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.
> >>
> >> Signed-off-by: Sergiu Cuciurean <sergiu.cuciurean@analog.com>
> >> Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
> >> ---
> > Forgot the changelog here.
> > Apologies.
> >
> > Changelog v1 -> v2:
> > * removed whitespace change for 'common' field
> > * updated comment about the lock usage
>
> Hi Alexandru,
>
> Sorry if I missed it... is there an update on the comment :-) ?

For a moment there, I thought I didn't.
GMail's threading is confusing.

----------------------------------------------------------------------------
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.
----------------------------------------------------------------------------

>
> Best Regards,
> Fabrice
> >
> >>  drivers/iio/dac/stm32-dac.c | 12 ++++++++----
> >>  1 file changed, 8 insertions(+), 4 deletions(-)
> >>
> >> diff --git a/drivers/iio/dac/stm32-dac.c b/drivers/iio/dac/stm32-dac.c
> >> index 092c796fa3d9..7a8aed476850 100644
> >> --- a/drivers/iio/dac/stm32-dac.c
> >> +++ b/drivers/iio/dac/stm32-dac.c
> >> @@ -26,9 +26,11 @@
> >>  /**
> >>   * struct stm32_dac - private data of DAC driver
> >>   * @common:            reference to DAC common data
> >> + * @lock:              lock to protect the data buffer during regmap ops
> >>   */
> >>  struct stm32_dac {
> >>         struct stm32_dac_common *common;
> >> +       struct mutex            lock;
> >>  };
Jonathan Cameron Aug. 29, 2020, 3:46 p.m. UTC | #4
On Thu, 27 Aug 2020 13:00:36 +0300
Alexandru Ardelean <ardeleanalex@gmail.com> wrote:

> On Thu, Aug 27, 2020 at 12:03 PM Fabrice Gasnier <fabrice.gasnier@st.com> wrote:
> >
> > On 8/27/20 10:55 AM, Alexandru Ardelean wrote:  
> > > On Wed, Aug 26, 2020 at 3:03 PM Alexandru Ardelean
> > > <alexandru.ardelean@analog.com> 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.
> > >>
> > >> Signed-off-by: Sergiu Cuciurean <sergiu.cuciurean@analog.com>
> > >> Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
> > >> ---  
> > > Forgot the changelog here.
> > > Apologies.
> > >
> > > Changelog v1 -> v2:
> > > * removed whitespace change for 'common' field
> > > * updated comment about the lock usage  
> >
> > Hi Alexandru,
> >
> > Sorry if I missed it... is there an update on the comment :-) ?  
> 
> For a moment there, I thought I didn't.
> GMail's threading is confusing.
> 
> ----------------------------------------------------------------------------
> 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.
> ----------------------------------------------------------------------------
I think this got confused...

see below.


> 
> >
> > Best Regards,
> > Fabrice  
> > >  
> > >>  drivers/iio/dac/stm32-dac.c | 12 ++++++++----
> > >>  1 file changed, 8 insertions(+), 4 deletions(-)
> > >>
> > >> diff --git a/drivers/iio/dac/stm32-dac.c b/drivers/iio/dac/stm32-dac.c
> > >> index 092c796fa3d9..7a8aed476850 100644
> > >> --- a/drivers/iio/dac/stm32-dac.c
> > >> +++ b/drivers/iio/dac/stm32-dac.c
> > >> @@ -26,9 +26,11 @@
> > >>  /**
> > >>   * struct stm32_dac - private data of DAC driver
> > >>   * @common:            reference to DAC common data
> > >> + * @lock:              lock to protect the data buffer during regmap ops

The original comment was:


In this particular case I'm not sure that's what mlock was being used for.
I think it's about avoiding races around checking if powered down and
actually doing it.

And Fabrice's reply:

Hi Sergiu,

Indeed, purpose is to protect against a race here when reading CR, and
updating it via regmap (this also makes the subsequent pm_runtime calls
to be balanced based on this).
(Side note: there is no data buffer involved for the DAC.)
Could you please update the comment ?

Thanks,
Fabrice

> > >>   */
> > >>  struct stm32_dac {
> > >>         struct stm32_dac_common *common;
> > >> +       struct mutex            lock;
> > >>  };
Alexandru Ardelean Sept. 2, 2020, 6:12 a.m. UTC | #5
On Sat, Aug 29, 2020 at 6:46 PM Jonathan Cameron <jic23@kernel.org> wrote:
>
> On Thu, 27 Aug 2020 13:00:36 +0300
> Alexandru Ardelean <ardeleanalex@gmail.com> wrote:
>
> > On Thu, Aug 27, 2020 at 12:03 PM Fabrice Gasnier <fabrice.gasnier@st.com> wrote:
> > >
> > > On 8/27/20 10:55 AM, Alexandru Ardelean wrote:
> > > > On Wed, Aug 26, 2020 at 3:03 PM Alexandru Ardelean
> > > > <alexandru.ardelean@analog.com> 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.
> > > >>
> > > >> Signed-off-by: Sergiu Cuciurean <sergiu.cuciurean@analog.com>
> > > >> Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
> > > >> ---
> > > > Forgot the changelog here.
> > > > Apologies.
> > > >
> > > > Changelog v1 -> v2:
> > > > * removed whitespace change for 'common' field
> > > > * updated comment about the lock usage
> > >
> > > Hi Alexandru,
> > >
> > > Sorry if I missed it... is there an update on the comment :-) ?
> >
> > For a moment there, I thought I didn't.
> > GMail's threading is confusing.
> >
> > ----------------------------------------------------------------------------
> > 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.
> > ----------------------------------------------------------------------------
> I think this got confused...
>
> see below.
>
>
> >
> > >
> > > Best Regards,
> > > Fabrice
> > > >
> > > >>  drivers/iio/dac/stm32-dac.c | 12 ++++++++----
> > > >>  1 file changed, 8 insertions(+), 4 deletions(-)
> > > >>
> > > >> diff --git a/drivers/iio/dac/stm32-dac.c b/drivers/iio/dac/stm32-dac.c
> > > >> index 092c796fa3d9..7a8aed476850 100644
> > > >> --- a/drivers/iio/dac/stm32-dac.c
> > > >> +++ b/drivers/iio/dac/stm32-dac.c
> > > >> @@ -26,9 +26,11 @@
> > > >>  /**
> > > >>   * struct stm32_dac - private data of DAC driver
> > > >>   * @common:            reference to DAC common data
> > > >> + * @lock:              lock to protect the data buffer during regmap ops

oh, silly me;
it's about this comment;
will re-spin

>
> The original comment was:
>
>
> In this particular case I'm not sure that's what mlock was being used for.
> I think it's about avoiding races around checking if powered down and
> actually doing it.
>
> And Fabrice's reply:
>
> Hi Sergiu,
>
> Indeed, purpose is to protect against a race here when reading CR, and
> updating it via regmap (this also makes the subsequent pm_runtime calls
> to be balanced based on this).
> (Side note: there is no data buffer involved for the DAC.)
> Could you please update the comment ?
>
> Thanks,
> Fabrice
>
> > > >>   */
> > > >>  struct stm32_dac {
> > > >>         struct stm32_dac_common *common;
> > > >> +       struct mutex            lock;
> > > >>  };
>
diff mbox series

Patch

diff --git a/drivers/iio/dac/stm32-dac.c b/drivers/iio/dac/stm32-dac.c
index 092c796fa3d9..7a8aed476850 100644
--- a/drivers/iio/dac/stm32-dac.c
+++ b/drivers/iio/dac/stm32-dac.c
@@ -26,9 +26,11 @@ 
 /**
  * struct stm32_dac - private data of DAC driver
  * @common:		reference to DAC common data
+ * @lock:		lock to protect the data buffer during regmap ops
  */
 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 +60,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 +71,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 +329,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;