All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alexandru Ardelean <ardeleanalex@gmail.com>
To: Jonathan Cameron <jic23@kernel.org>
Cc: Mircea Caprioru <mircea.caprioru@analog.com>,
	"Hennerich, Michael" <Michael.Hennerich@analog.com>,
	Alexandru Ardelean <alexandru.ardelean@analog.com>,
	Lars-Peter Clausen <lars@metafoo.de>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	LKML <linux-kernel@vger.kernel.org>,
	linux-iio <linux-iio@vger.kernel.org>,
	Sergiu Cuciurean <sergiu.cuciurean@analog.com>
Subject: Re: [PATCH 4/5] iio: adc: vf610_adc: Replace indio_dev->mlock with own device lock
Date: Wed, 30 Sep 2020 08:57:55 +0300	[thread overview]
Message-ID: <CA+U=DsqDymuWht_H1z+TnTvcXbnuJ9tm45w4g_U6g=qWz+fGvQ@mail.gmail.com> (raw)
In-Reply-To: <20200929171441.5b4ff8c8@archlinux>

On Tue, Sep 29, 2020 at 7:15 PM Jonathan Cameron <jic23@kernel.org> wrote:
>
> On Mon, 28 Sep 2020 16:13:32 +0300
> Mircea Caprioru <mircea.caprioru@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 on the device's state structure.
> >
> > 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: Mircea Caprioru <mircea.caprioru@analog.com>
>
> There are more problems in the locking in here than just this one.
> See below.  The taking of mlock like this was what originally motivated
> the efforts to hide it away from drivers.
>
> In this particular case I don't think a local lock is the correct solution.
>
> Thanks,
>
> Jonathan
>
>
> > ---
> >  drivers/iio/adc/vf610_adc.c | 28 ++++++++++++++++++++--------
> >  1 file changed, 20 insertions(+), 8 deletions(-)
> >
> > diff --git a/drivers/iio/adc/vf610_adc.c b/drivers/iio/adc/vf610_adc.c
> > index 1d794cf3e3f1..b7d583993f0b 100644
> > --- a/drivers/iio/adc/vf610_adc.c
> > +++ b/drivers/iio/adc/vf610_adc.c
> > @@ -168,6 +168,15 @@ struct vf610_adc {
> >
> >       struct completion completion;
> >       u16 buffer[8];
>
> Side note.  That buffer isn't correctly aligned.  I'll add this one to
> my next series fixing those.
>
> > +     /*
> > +      * 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;
> >  };
> >
> >  static const u32 vf610_hw_avgs[] = { 1, 4, 8, 16, 32 };
> > @@ -464,11 +473,11 @@ static int vf610_set_conversion_mode(struct iio_dev *indio_dev,
> >  {
> >       struct vf610_adc *info = iio_priv(indio_dev);
> >
> > -     mutex_lock(&indio_dev->mlock);
> > +     mutex_lock(&info->lock);
> Hmm. So there is a bit of a question on what the locking here is doing.
> (see below for a different use of mlock).
>
> What it will do currently is to prevent the conversion mode changing whilst
> we are in buffered mode.  It will also protect against concurrent
> calls of this function.
>
> I would replace this with iio_device_claim_direct_mode() rather than a
> local lock.

This raises a new question: if there's any drivers that we missed [for
iio_device_claim_direct_mode()].
While I was aware of iio_device_claim_direct_mode(), I missed this
fact when pushing the mlock cleanup.

Oh well, I'll do a quick audit over the current drivers that were converted.
Hopefully I don't find anything :P

>
> >       info->adc_feature.conv_mode = mode;
> >       vf610_adc_calculate_rates(info);
> >       vf610_adc_hw_init(info);
> > -     mutex_unlock(&indio_dev->mlock);
> > +     mutex_unlock(&info->lock);
> >
> >       return 0;
> >  }
> > @@ -632,9 +641,9 @@ static int vf610_read_raw(struct iio_dev *indio_dev,
> >       switch (mask) {
> >       case IIO_CHAN_INFO_RAW:
> >       case IIO_CHAN_INFO_PROCESSED:
> > -             mutex_lock(&indio_dev->mlock);
> > +             mutex_lock(&info->lock);
> >               if (iio_buffer_enabled(indio_dev)) {
> > -                     mutex_unlock(&indio_dev->mlock);
> > +                     mutex_unlock(&info->lock);
>
> Should be use iio_device_claim_direct_mode()
>
> mlock is being taken here to stop us entering buffered mode.
>
> Whilst I'd rather a driver didn't rely on internal details of
> IIO, it is rather fiddly to get the locking right when there is a completion
> going on, so I think here you are safe to do so.
>
> >                       return -EBUSY;
> >               }
> >
> > @@ -645,11 +654,11 @@ static int vf610_read_raw(struct iio_dev *indio_dev,
> >               ret = wait_for_completion_interruptible_timeout
> >                               (&info->completion, VF610_ADC_TIMEOUT);
> >               if (ret == 0) {
> > -                     mutex_unlock(&indio_dev->mlock);
> > +                     mutex_unlock(&info->lock);
> >                       return -ETIMEDOUT;
> >               }
> >               if (ret < 0) {
> > -                     mutex_unlock(&indio_dev->mlock);
> > +                     mutex_unlock(&info->lock);
> >                       return ret;
> >               }
> >
> > @@ -668,11 +677,11 @@ static int vf610_read_raw(struct iio_dev *indio_dev,
> >
> >                       break;
> >               default:
> > -                     mutex_unlock(&indio_dev->mlock);
> > +                     mutex_unlock(&info->lock);
> >                       return -EINVAL;
> >               }
> >
> > -             mutex_unlock(&indio_dev->mlock);
> > +             mutex_unlock(&info->lock);
> >               return IIO_VAL_INT;
> >
> >       case IIO_CHAN_INFO_SCALE:
> > @@ -807,6 +816,9 @@ static int vf610_adc_probe(struct platform_device *pdev)
> >       }
> >
> >       info = iio_priv(indio_dev);
> > +
> > +     mutex_init(&info->lock);
> > +
> >       info->dev = &pdev->dev;
> >
> >       info->regs = devm_platform_ioremap_resource(pdev, 0);
>

  reply	other threads:[~2020-09-30  5:58 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-28 13:13 [PATCH 1/5] iio: adc: spear_adc: Replace indio_dev->mlock with own device lock Mircea Caprioru
2020-09-28 13:13 ` [PATCH 2/5] iio: adc: palmas_gpadc: " Mircea Caprioru
2021-02-21 16:38   ` Jonathan Cameron
2020-09-28 13:13 ` [PATCH 3/5] iio: adc: npcm_adc: " Mircea Caprioru
2021-02-21 16:39   ` Jonathan Cameron
2020-09-28 13:13 ` [PATCH 4/5] iio: adc: vf610_adc: " Mircea Caprioru
2020-09-29 16:14   ` Jonathan Cameron
2020-09-30  5:57     ` Alexandru Ardelean [this message]
2020-09-30 10:49       ` Jonathan Cameron
2020-09-28 13:13 ` [PATCH 5/5] iio: adc: rockchip_saradc: " Mircea Caprioru
2020-09-29 16:23   ` Jonathan Cameron
2021-02-21 16:37 ` [PATCH 1/5] iio: adc: spear_adc: " Jonathan Cameron

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CA+U=DsqDymuWht_H1z+TnTvcXbnuJ9tm45w4g_U6g=qWz+fGvQ@mail.gmail.com' \
    --to=ardeleanalex@gmail.com \
    --cc=Michael.Hennerich@analog.com \
    --cc=alexandru.ardelean@analog.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jic23@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mircea.caprioru@analog.com \
    --cc=sergiu.cuciurean@analog.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.