From: Lorenzo Bianconi <lorenzo@kernel.org>
To: Jonathan Cameron <jic23@kernel.org>
Cc: Lorenzo Bianconi <lorenzo.bianconi@redhat.com>,
Jonathan Cameron <Jonathan.Cameron@huawei.com>,
linux-iio@vger.kernel.org, mario.tesi@st.com,
denis.ciocca@st.com, armando.visconti@st.com
Subject: Re: [PATCH] iio: imu: st_lsm6dsx: fix edge-trigger interrupts
Date: Sun, 8 Nov 2020 19:27:28 +0100 [thread overview]
Message-ID: <20201108182728.GA17810@lore-desk> (raw)
In-Reply-To: <20201108164942.094e1112@archlinux>
[-- Attachment #1: Type: text/plain, Size: 7846 bytes --]
[...]
> >
> > I guess since edge interrupts run with the line unmasked a new interrupt can fire
> > while the irq thread is still running (so wake_up_process() will just return) but
> > the driver has already read fifo_status register and so it will not read new
> > sample. This case should be fixed reading again the fifo_status register.
>
> It doesn't actually help because there is always a window after the fifo_status register
> is read before we exit the thread.
>
> I 'think' what happens (it's been a while since I dug through this stuff) is
> that you end up with the task being added to the runqueue, even though it's
> already running. Upshot the thread gets scheduled gain.
>
> If this were not the case there would be a race with any edge based interrupt
> as the thread has to reenable the interrupt and it will always be able to fire
> whilst the thread is still running.
I guess this is the case (race between irq-thread and edge interrupt) since afaik
handle_edge_irq() runs with the irq-line unmasked.
I agree with you this approach does not fix completely the issue but it reduces
the race-surface since now the interrupt can fire while processing the previous one
(the issue occurs if the interrupt fires between the end of hw->settings->fifo_ops.read_fifo()
and the end of the irq-thread) while before the interrupt must always fire before
reading the fifo status register (in fact with the patch applied I am not able
to trigger the issue anymore).
@denis, mario, armando: can you please confirm the hw does not support pulsed
interrupts for fifo-watermark?
If not one possible approach would be to disable the interrupt generation on
the sensor at the beginning of st_lsm6dsx_handler_thread() and schedule a
workqueue at the end of st_lsm6dsx_handler_thread() to re-enable the sensor
interrupt generation. What do you think?
Regards,
Lorenzo
>
> Jonathan
>
>
>
>
> >
> > Regards,
> > Lorenzo
> >
> > >
> > > >
> > > > >
> > > > >
> > > > > Hmm. Having had a look at one of the datasheets, I'm far from convinced these
> > > > > parts truely support edge interrupts. I can't see anything about minimum
> > > > > off periods etc that you need for true edge interrupts. Otherwise they are
> > > > > going to be prone to races.
> > > >
> > > > @mario, denis, armando: any pointer for this?
> > > >
> > > > >
> > > > > So I think the following can happen.
> > > > >
> > > > > A) We drain the fifo and it stays under the limit. Hence once that
> > > > > is crossed in future we will interrupt as normal.
> > > > >
> > > > > B) We drain the fifo but it either has a very low watermark, or is
> > > > > filling very fast. We manage to drain enough to get the interrupt
> > > > > to fire again, so all is fine if less than ideal. With you loop we
> > > > > may up entering the interrupt handler when we don't actually need to.
> > > > > If you want to avoid that you would need to disable the interrupt,
> > > > > then drain the fifo and finally do a dance to successfully reenable
> > > > > the interrupt, whilst ensuring no chance of missing by checking it
> > > > > should not have fired (still below the threshold)
> > > > >
> > > > > C) We try to drain the fifo, but it is actually filling fast enough that
> > > > > we never get it under the limit, so no interrupt ever fires.
> > > > > With new code, we'll keep spinning to 0 so might eventually drain it.
> > > > > That needs a timeout so we just give up eventually.
> > > > >
> > > > > D) watershed is one sample, we drain low enough to successfully get down
> > > > > to zero at the moment of the read, but very very soon after that we get
> > > > > one sample again. There is a window in which the interrupt line dropped
> > > > > but analogue electronics etc being what they are, it may not have been
> > > > > detectable. Hence we miss an interrupt... What you are doing is reducing
> > > > > the chance of hitting this. It is nasty, but you might be able to ensure
> > > > > a reasonable period by widening this window. Limit the watermark to 2
> > > > > samples?
> > > > >
> > > > > Also needs a fixes tag :)
> > > >
> > > > ack, I will add them in v2
> > > >
> > > > Regards,
> > > > Lorenzo
> > > > >
> > > > > >
> > > > > > Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
> > > > > > ---
> > > > > > drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 33 +++++++++++++++-----
> > > > > > 1 file changed, 25 insertions(+), 8 deletions(-)
> > > > > >
> > > > > > diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
> > > > > > index 5e584c6026f1..d43b08ceec01 100644
> > > > > > --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
> > > > > > +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
> > > > > > @@ -2457,22 +2457,36 @@ st_lsm6dsx_report_motion_event(struct st_lsm6dsx_hw *hw)
> > > > > > return data & event_settings->wakeup_src_status_mask;
> > > > > > }
> > > > > >
> > > > > > +static irqreturn_t st_lsm6dsx_handler_irq(int irq, void *private)
> > > > > > +{
> > > > > > + return IRQ_WAKE_THREAD;
> > > > > > +}
> > > > > > +
> > > > > > static irqreturn_t st_lsm6dsx_handler_thread(int irq, void *private)
> > > > > > {
> > > > > > struct st_lsm6dsx_hw *hw = private;
> > > > > > + int fifo_len = 0, len = 0;
> > > > > > bool event;
> > > > > > - int count;
> > > > > >
> > > > > > event = st_lsm6dsx_report_motion_event(hw);
> > > > > >
> > > > > > if (!hw->settings->fifo_ops.read_fifo)
> > > > > > return event ? IRQ_HANDLED : IRQ_NONE;
> > > > > >
> > > > > > - mutex_lock(&hw->fifo_lock);
> > > > > > - count = hw->settings->fifo_ops.read_fifo(hw);
> > > > > > - mutex_unlock(&hw->fifo_lock);
> > > > > > + /*
> > > > > > + * If we are using edge IRQs, new samples can arrive while
> > > > > > + * processing current IRQ and those may be missed unless we
> > > > > > + * pick them here, so let's try read FIFO status again
> > > > > > + */
> > > > > > + do {
> > > > > > + mutex_lock(&hw->fifo_lock);
> > > > > > + len = hw->settings->fifo_ops.read_fifo(hw);
> > > > > > + mutex_unlock(&hw->fifo_lock);
> > > > > > +
> > > > > > + fifo_len += len;
> > > > > > + } while (len > 0);
> > > > > >
> > > > > > - return count || event ? IRQ_HANDLED : IRQ_NONE;
> > > > > > + return fifo_len || event ? IRQ_HANDLED : IRQ_NONE;
> > > > > > }
> > > > > >
> > > > > > static int st_lsm6dsx_irq_setup(struct st_lsm6dsx_hw *hw)
> > > > > > @@ -2488,10 +2502,14 @@ static int st_lsm6dsx_irq_setup(struct st_lsm6dsx_hw *hw)
> > > > > >
> > > > > > switch (irq_type) {
> > > > > > case IRQF_TRIGGER_HIGH:
> > > > > > + irq_type |= IRQF_ONESHOT;
> > > > > > + fallthrough;
> > > > > > case IRQF_TRIGGER_RISING:
> > > > > > irq_active_low = false;
> > > > > > break;
> > > > > > case IRQF_TRIGGER_LOW:
> > > > > > + irq_type |= IRQF_ONESHOT;
> > > > > > + fallthrough;
> > > > > > case IRQF_TRIGGER_FALLING:
> > > > > > irq_active_low = true;
> > > > > > break;
> > > > > > @@ -2520,10 +2538,9 @@ static int st_lsm6dsx_irq_setup(struct st_lsm6dsx_hw *hw)
> > > > > > }
> > > > > >
> > > > > > err = devm_request_threaded_irq(hw->dev, hw->irq,
> > > > > > - NULL,
> > > > > > + st_lsm6dsx_handler_irq,
> > > > > > st_lsm6dsx_handler_thread,
> > > > > > - irq_type | IRQF_ONESHOT,
> > > > > > - "lsm6dsx", hw);
> > > > > > + irq_type, "lsm6dsx", hw);
> > > > > > if (err) {
> > > > > > dev_err(hw->dev, "failed to request trigger irq %d\n",
> > > > > > hw->irq);
> > > > >
> > > >
> > >
>
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
next prev parent reply other threads:[~2020-11-08 18:27 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-10-22 9:26 [PATCH] iio: imu: st_lsm6dsx: fix edge-trigger interrupts Lorenzo Bianconi
2020-11-01 16:33 ` Jonathan Cameron
2020-11-02 10:15 ` Lorenzo Bianconi
2020-11-02 17:44 ` Jonathan Cameron
2020-11-02 18:18 ` Lorenzo Bianconi
2020-11-08 16:49 ` Jonathan Cameron
2020-11-08 18:27 ` Lorenzo Bianconi [this message]
2020-11-14 15:06 ` Jonathan Cameron
2020-11-14 16:48 ` Lorenzo Bianconi
2020-11-14 17:31 ` Jonathan Cameron
2020-11-14 17:58 ` Lorenzo Bianconi
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=20201108182728.GA17810@lore-desk \
--to=lorenzo@kernel.org \
--cc=Jonathan.Cameron@huawei.com \
--cc=armando.visconti@st.com \
--cc=denis.ciocca@st.com \
--cc=jic23@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=lorenzo.bianconi@redhat.com \
--cc=mario.tesi@st.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 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).