Re: [PATCH v2] Staging: iio: ade7758: expand buf_lock to cover both buffer and state protection

From: Shreeya Patel
Date: Sun Jan 28 2018 - 04:10:03 EST


On Sun, 2018-01-28 at 08:31 +0000, Jonathan Cameron wrote:
> On Thu, 25 Jan 2018 22:10:08 +0530
> Shreeya Patel <shreeya.patel23498@xxxxxxxxx> wrote:
>
> >
> > iio_dev->mlock is to be used only by the IIO core for protecting
> > device mode changes between INDIO_DIRECT and INDIO_BUFFER.
> >
> > This patch replaces the use of mlock with the already established
> > buf_lock mutex.
> >
> > Introducing 'unlocked' __ade7758_spi_write_reg_8 and
> > __ade7758_spi_read_reg_8 functions to be used by
> > ade7758_write_samp_freq
> > and ade7758_read_samp_freq which avoids nested locks and maintains
> > atomicity between bus and device frequency changes.
> >
> > Signed-off-by: Shreeya Patel <shreeya.patel23498@xxxxxxxxx>
> Hi Shreeya,
>
> This is now technically correct which is great.
> I would make one minor change to make it slightly easier to read.
>
> The read / write frequency functions now require the buf_lock to
> be held.ÂÂThat's not obvious so I would avoid this but moving
> the locking inside the functions where it is then clear that
> they are taking the unlocked forms of the register read/ write.
>
> This would also then make it clear why the normal locked form
> of _read_reg_8 is fine in the read_freq case but not the
> write_freq case.ÂÂ(Hence just use the normal locked form
> for the read and don't explicitly take the locks when
> reading the frequency - leave it to the register read function)
>

Hi,

I have introduced unlocked form of the _read_reg_8 also, which is wrong
on my side. I should have discarded the mlocks in the read_freq case
first, then there would have been no need of having unlocked form of
the _read_reg_8 :(

Please discard this patch and I'll send two patches in which first I'll
remove the mlocks from the read case and then I'll have only the
unlocked form of the _write_reg_8. In this way, there won't be any
useless code in the file for the read case.

Sorry, it took me a bit more time to understand.

Thanks
Â
> Thanks,
>
> Jonathan
> >
> > ---
> >
> > Changes in v2
> > Â -Add static keyword to newly introduced functions and remove some
> > added comments which are not required.
> >
> >
> > Âdrivers/staging/iio/meter/ade7758.hÂÂÂÂÂÂ|ÂÂ2 +-
> > Âdrivers/staging/iio/meter/ade7758_core.c | 47
> > +++++++++++++++++++++++---------
> > Â2 files changed, 35 insertions(+), 14 deletions(-)
> >
> > diff --git a/drivers/staging/iio/meter/ade7758.h
> > b/drivers/staging/iio/meter/ade7758.h
> > index 6ae78d8..2de81b5 100644
> > --- a/drivers/staging/iio/meter/ade7758.h
> > +++ b/drivers/staging/iio/meter/ade7758.h
> > @@ -111,7 +111,7 @@
> > Â * @trig: data ready trigger registered with iio
> > Â * @tx: transmit buffer
> > Â * @rx: receive buffer
> > - * @buf_lock: mutex to protect tx and rx
> > + * @buf_lock: mutex to protect tx, rx, read and
> > write frequency
> > Â **/
> > Âstruct ade7758_state {
> > Â struct spi_device *us;
> > diff --git a/drivers/staging/iio/meter/ade7758_core.c
> > b/drivers/staging/iio/meter/ade7758_core.c
> > index 7b7ffe5..fed4684 100644
> > --- a/drivers/staging/iio/meter/ade7758_core.c
> > +++ b/drivers/staging/iio/meter/ade7758_core.c
> > @@ -24,17 +24,25 @@
> > Â#include "meter.h"
> > Â#include "ade7758.h"
> > Â
> > -int ade7758_spi_write_reg_8(struct device *dev, u8 reg_address, u8
> > val)
> > +static int __ade7758_spi_write_reg_8(struct device *dev, u8
> > reg_address, u8 val)
> > Â{
> > - int ret;
> > Â struct iio_dev *indio_dev = dev_to_iio_dev(dev);
> > Â struct ade7758_state *st = iio_priv(indio_dev);
> > Â
> > - mutex_lock(&st->buf_lock);
> > Â st->tx[0] = ADE7758_WRITE_REG(reg_address);
> > Â st->tx[1] = val;
> > Â
> > - ret = spi_write(st->us, st->tx, 2);
> > + return spi_write(st->us, st->tx, 2);
> > +}
> > +
> > +int ade7758_spi_write_reg_8(struct device *dev, u8 reg_address, u8
> > val)
> > +{
> > + int ret;
> > + struct iio_dev *indio_dev = dev_to_iio_dev(dev);
> > + struct ade7758_state *st = iio_priv(indio_dev);
> > +
> > + mutex_lock(&st->buf_lock);
> > + ret = __ade7758_spi_write_reg_8(dev, reg_address, val);
> > Â mutex_unlock(&st->buf_lock);
> > Â
> > Â return ret;
> > @@ -91,7 +99,7 @@ static int ade7758_spi_write_reg_24(struct device
> > *dev, u8 reg_address,
> > Â return ret;
> > Â}
> > Â
> > -int ade7758_spi_read_reg_8(struct device *dev, u8 reg_address, u8
> > *val)
> > +static int __ade7758_spi_read_reg_8(struct device *dev, u8
> > reg_address, u8 *val)
> > Â{
> > Â struct iio_dev *indio_dev = dev_to_iio_dev(dev);
> > Â struct ade7758_state *st = iio_priv(indio_dev);
> > @@ -111,7 +119,6 @@ int ade7758_spi_read_reg_8(struct device *dev,
> > u8 reg_address, u8 *val)
> > Â },
> > Â };
> > Â
> > - mutex_lock(&st->buf_lock);
> > Â st->tx[0] = ADE7758_READ_REG(reg_address);
> > Â st->tx[1] = 0;
> > Â
> > @@ -124,7 +131,19 @@ int ade7758_spi_read_reg_8(struct device *dev,
> > u8 reg_address, u8 *val)
> > Â *val = st->rx[0];
> > Â
> > Âerror_ret:
> > + return ret;
> > +}
> > +
> > +int ade7758_spi_read_reg_8(struct device *dev, u8 reg_address, u8
> > *val)
> > +{
> > + struct iio_dev *indio_dev = dev_to_iio_dev(dev);
> > + struct ade7758_state *st = iio_priv(indio_dev);
> > + int ret;
> > +
> > + mutex_lock(&st->buf_lock);
> > + ret = __ade7758_spi_read_reg_8(dev, reg_address, val);
> > Â mutex_unlock(&st->buf_lock);
> > +
> > Â return ret;
> > Â}
> > Â
> > @@ -470,7 +489,7 @@ static int ade7758_read_samp_freq(struct device
> > *dev, int *val)
> > Â int ret;
> > Â u8 t;
> > Â
> > - ret = ade7758_spi_read_reg_8(dev, ADE7758_WAVMODE, &t);
> > + ret = __ade7758_spi_read_reg_8(dev, ADE7758_WAVMODE, &t);
> > Â if (ret)
> > Â return ret;
> > Â
> > @@ -503,14 +522,14 @@ static int ade7758_write_samp_freq(struct
> > device *dev, int val)
> > Â goto out;
> > Â }
> > Â
> > - ret = ade7758_spi_read_reg_8(dev, ADE7758_WAVMODE, &reg);
> > + ret = __ade7758_spi_read_reg_8(dev, ADE7758_WAVMODE,
> > &reg);
> > Â if (ret)
> > Â goto out;
> > Â
> > Â reg &= ~(5 << 3);
> > Â reg |= t << 5;
> > Â
> > - ret = ade7758_spi_write_reg_8(dev, ADE7758_WAVMODE, reg);
> > + ret = __ade7758_spi_write_reg_8(dev, ADE7758_WAVMODE,
> > reg);
> > Â
> > Âout:
> > Â return ret;
> > @@ -523,12 +542,13 @@ static int ade7758_read_raw(struct iio_dev
> > *indio_dev,
> > Â ÂÂÂÂlong mask)
> > Â{
> > Â int ret;
> > + struct ade7758_state *st = iio_priv(indio_dev);
> > Â
> > Â switch (mask) {
> > Â case IIO_CHAN_INFO_SAMP_FREQ:
> > - mutex_lock(&indio_dev->mlock);
> > + mutex_lock(&st->buf_lock);
> > Â ret = ade7758_read_samp_freq(&indio_dev->dev,
> > val);
> > - mutex_unlock(&indio_dev->mlock);
> > + mutex_unlock(&st->buf_lock);
> > Â return ret;
> > Â default:
> > Â return -EINVAL;
> > @@ -542,14 +562,15 @@ static int ade7758_write_raw(struct iio_dev
> > *indio_dev,
> > Â ÂÂÂÂÂint val, int val2, long mask)
> > Â{
> > Â int ret;
> > + struct ade7758_state *st = iio_priv(indio_dev);
> > Â
> > Â switch (mask) {
> > Â case IIO_CHAN_INFO_SAMP_FREQ:
> > Â if (val2)
> > Â return -EINVAL;
> > - mutex_lock(&indio_dev->mlock);
> > + mutex_lock(&st->buf_lock);
> > Â ret = ade7758_write_samp_freq(&indio_dev->dev,
> > val);
> > - mutex_unlock(&indio_dev->mlock);
> > + mutex_unlock(&st->buf_lock);
> > Â return ret;
> > Â default:
> > Â return -EINVAL;