* [PATCH] iio: adc: fsl-imx25-gcq: Replace indio_dev->mlock with own device lock
@ 2020-08-26 12:06 Alexandru Ardelean
2020-08-29 15:29 ` Jonathan Cameron
2020-09-16 9:29 ` [PATCH v2] " Alexandru Ardelean
0 siblings, 2 replies; 4+ messages in thread
From: Alexandru Ardelean @ 2020-08-26 12:06 UTC (permalink / raw)
To: linux-iio, linux-kernel
Cc: jic23, shawnguo, s.hauer, Sergiu Cuciurean, Alexandru Ardelean
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, to protect against any other accesses during the
reading of sample. Reading a sample requires multiple consecutive regmap
operations and a completion callback, so this requires that no other
read occurs until it completes.
Signed-off-by: Sergiu Cuciurean <sergiu.cuciurean@analog.com>
Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
---
drivers/iio/adc/fsl-imx25-gcq.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/drivers/iio/adc/fsl-imx25-gcq.c b/drivers/iio/adc/fsl-imx25-gcq.c
index 8cb51cf7a816..2a56ed0fc793 100644
--- a/drivers/iio/adc/fsl-imx25-gcq.c
+++ b/drivers/iio/adc/fsl-imx25-gcq.c
@@ -38,6 +38,7 @@ struct mx25_gcq_priv {
struct completion completed;
struct clk *clk;
int irq;
+ struct mutex lock;
struct regulator *vref[4];
u32 channel_vref_mv[MX25_NUM_CFGS];
};
@@ -137,9 +138,9 @@ static int mx25_gcq_read_raw(struct iio_dev *indio_dev,
switch (mask) {
case IIO_CHAN_INFO_RAW:
- mutex_lock(&indio_dev->mlock);
+ mutex_lock(&priv->lock);
ret = mx25_gcq_get_raw_value(&indio_dev->dev, chan, priv, val);
- mutex_unlock(&indio_dev->mlock);
+ mutex_unlock(&priv->lock);
return ret;
case IIO_CHAN_INFO_SCALE:
@@ -314,6 +315,8 @@ static int mx25_gcq_probe(struct platform_device *pdev)
return PTR_ERR(priv->regs);
}
+ mutex_init(&priv->lock);
+
init_completion(&priv->completed);
ret = mx25_gcq_setup_cfgs(pdev, priv);
--
2.25.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] iio: adc: fsl-imx25-gcq: Replace indio_dev->mlock with own device lock
2020-08-26 12:06 [PATCH] iio: adc: fsl-imx25-gcq: Replace indio_dev->mlock with own device lock Alexandru Ardelean
@ 2020-08-29 15:29 ` Jonathan Cameron
2020-09-16 9:29 ` [PATCH v2] " Alexandru Ardelean
1 sibling, 0 replies; 4+ messages in thread
From: Jonathan Cameron @ 2020-08-29 15:29 UTC (permalink / raw)
To: Alexandru Ardelean
Cc: linux-iio, linux-kernel, shawnguo, s.hauer, Sergiu Cuciurean
On Wed, 26 Aug 2020 15:06:09 +0300
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, to protect against any other accesses during the
> reading of sample. Reading a sample requires multiple consecutive regmap
> operations and a completion callback, so this requires that no other
> read occurs until it completes.
>
> Signed-off-by: Sergiu Cuciurean <sergiu.cuciurean@analog.com>
> Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
> ---
> drivers/iio/adc/fsl-imx25-gcq.c | 7 +++++--
> 1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/iio/adc/fsl-imx25-gcq.c b/drivers/iio/adc/fsl-imx25-gcq.c
> index 8cb51cf7a816..2a56ed0fc793 100644
> --- a/drivers/iio/adc/fsl-imx25-gcq.c
> +++ b/drivers/iio/adc/fsl-imx25-gcq.c
> @@ -38,6 +38,7 @@ struct mx25_gcq_priv {
> struct completion completed;
> struct clk *clk;
> int irq;
> + struct mutex lock;
Rule 1 of locks. Every single one need documentation so that the
scope that the lock protects is clearly stated.
Otherwise patch looks fine to me.
thanks,
Jonathan
> struct regulator *vref[4];
> u32 channel_vref_mv[MX25_NUM_CFGS];
> };
> @@ -137,9 +138,9 @@ static int mx25_gcq_read_raw(struct iio_dev *indio_dev,
>
> switch (mask) {
> case IIO_CHAN_INFO_RAW:
> - mutex_lock(&indio_dev->mlock);
> + mutex_lock(&priv->lock);
> ret = mx25_gcq_get_raw_value(&indio_dev->dev, chan, priv, val);
> - mutex_unlock(&indio_dev->mlock);
> + mutex_unlock(&priv->lock);
> return ret;
>
> case IIO_CHAN_INFO_SCALE:
> @@ -314,6 +315,8 @@ static int mx25_gcq_probe(struct platform_device *pdev)
> return PTR_ERR(priv->regs);
> }
>
> + mutex_init(&priv->lock);
> +
> init_completion(&priv->completed);
>
> ret = mx25_gcq_setup_cfgs(pdev, priv);
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2] iio: adc: fsl-imx25-gcq: Replace indio_dev->mlock with own device lock
2020-08-26 12:06 [PATCH] iio: adc: fsl-imx25-gcq: Replace indio_dev->mlock with own device lock Alexandru Ardelean
2020-08-29 15:29 ` Jonathan Cameron
@ 2020-09-16 9:29 ` Alexandru Ardelean
2020-09-17 18:27 ` Jonathan Cameron
1 sibling, 1 reply; 4+ messages in thread
From: Alexandru Ardelean @ 2020-09-16 9:29 UTC (permalink / raw)
To: linux-iio, linux-arm-kernel, linux-kernel
Cc: linux-imx, festevam, jic23, shawnguo, s.hauer, kernel,
Sergiu Cuciurean, Alexandru Ardelean
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, to protect against any other accesses during the
reading of sample. Reading a sample requires multiple consecutive regmap
operations and a completion callback, so this requires that no other
read occurs until it completes.
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/adc/fsl-imx25-gcq.c | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
diff --git a/drivers/iio/adc/fsl-imx25-gcq.c b/drivers/iio/adc/fsl-imx25-gcq.c
index 8cb51cf7a816..ab5139e911c3 100644
--- a/drivers/iio/adc/fsl-imx25-gcq.c
+++ b/drivers/iio/adc/fsl-imx25-gcq.c
@@ -40,6 +40,15 @@ struct mx25_gcq_priv {
int irq;
struct regulator *vref[4];
u32 channel_vref_mv[MX25_NUM_CFGS];
+ /*
+ * Lock to protect the device state during a potential concurrent
+ * read access from userspace. Reading a raw value requires a sequence
+ * of register writes, then a wait for a completion callback,
+ * and finally a register read, during which userspace could issue
+ * another read request. This lock protects a read access from
+ * ocurring before another one has finished.
+ */
+ struct mutex lock;
};
#define MX25_CQG_CHAN(chan, id) {\
@@ -137,9 +146,9 @@ static int mx25_gcq_read_raw(struct iio_dev *indio_dev,
switch (mask) {
case IIO_CHAN_INFO_RAW:
- mutex_lock(&indio_dev->mlock);
+ mutex_lock(&priv->lock);
ret = mx25_gcq_get_raw_value(&indio_dev->dev, chan, priv, val);
- mutex_unlock(&indio_dev->mlock);
+ mutex_unlock(&priv->lock);
return ret;
case IIO_CHAN_INFO_SCALE:
@@ -314,6 +323,8 @@ static int mx25_gcq_probe(struct platform_device *pdev)
return PTR_ERR(priv->regs);
}
+ mutex_init(&priv->lock);
+
init_completion(&priv->completed);
ret = mx25_gcq_setup_cfgs(pdev, priv);
--
2.17.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v2] iio: adc: fsl-imx25-gcq: Replace indio_dev->mlock with own device lock
2020-09-16 9:29 ` [PATCH v2] " Alexandru Ardelean
@ 2020-09-17 18:27 ` Jonathan Cameron
0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Cameron @ 2020-09-17 18:27 UTC (permalink / raw)
To: Alexandru Ardelean
Cc: linux-iio, linux-arm-kernel, linux-kernel, linux-imx, festevam,
shawnguo, s.hauer, kernel, Sergiu Cuciurean
On Wed, 16 Sep 2020 12:29:28 +0300
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, to protect against any other accesses during the
> reading of sample. Reading a sample requires multiple consecutive regmap
> operations and a completion callback, so this requires that no other
> read occurs until it completes.
>
> 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>
This seems simple enough so I've applied it to the togreg branch of iio.git
Thanks,
Jonathan
> ---
> drivers/iio/adc/fsl-imx25-gcq.c | 15 +++++++++++++--
> 1 file changed, 13 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/iio/adc/fsl-imx25-gcq.c b/drivers/iio/adc/fsl-imx25-gcq.c
> index 8cb51cf7a816..ab5139e911c3 100644
> --- a/drivers/iio/adc/fsl-imx25-gcq.c
> +++ b/drivers/iio/adc/fsl-imx25-gcq.c
> @@ -40,6 +40,15 @@ struct mx25_gcq_priv {
> int irq;
> struct regulator *vref[4];
> u32 channel_vref_mv[MX25_NUM_CFGS];
> + /*
> + * Lock to protect the device state during a potential concurrent
> + * read access from userspace. Reading a raw value requires a sequence
> + * of register writes, then a wait for a completion callback,
> + * and finally a register read, during which userspace could issue
> + * another read request. This lock protects a read access from
> + * ocurring before another one has finished.
> + */
> + struct mutex lock;
> };
>
> #define MX25_CQG_CHAN(chan, id) {\
> @@ -137,9 +146,9 @@ static int mx25_gcq_read_raw(struct iio_dev *indio_dev,
>
> switch (mask) {
> case IIO_CHAN_INFO_RAW:
> - mutex_lock(&indio_dev->mlock);
> + mutex_lock(&priv->lock);
> ret = mx25_gcq_get_raw_value(&indio_dev->dev, chan, priv, val);
> - mutex_unlock(&indio_dev->mlock);
> + mutex_unlock(&priv->lock);
> return ret;
>
> case IIO_CHAN_INFO_SCALE:
> @@ -314,6 +323,8 @@ static int mx25_gcq_probe(struct platform_device *pdev)
> return PTR_ERR(priv->regs);
> }
>
> + mutex_init(&priv->lock);
> +
> init_completion(&priv->completed);
>
> ret = mx25_gcq_setup_cfgs(pdev, priv);
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2020-09-17 18:28 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-26 12:06 [PATCH] iio: adc: fsl-imx25-gcq: Replace indio_dev->mlock with own device lock Alexandru Ardelean
2020-08-29 15:29 ` Jonathan Cameron
2020-09-16 9:29 ` [PATCH v2] " Alexandru Ardelean
2020-09-17 18:27 ` Jonathan Cameron
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).