From: Jonathan Cameron <jic23@kernel.org>
To: Daniel Beer <dlbeer@gmail.com>
Cc: linux-iio@vger.kernel.org, Lars-Peter Clausen <lars@metafoo.de>,
Michael Hennerich <Michael.Hennerich@analog.com>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] ad_sigma_delta: fix race between IRQ and completion
Date: Sat, 31 Dec 2022 14:28:32 +0000 [thread overview]
Message-ID: <20221231142832.49cb47c7@jic23-huawei> (raw)
In-Reply-To: <20221224023158.GA254443@nyquist.nev>
On Sat, 24 Dec 2022 15:31:58 +1300
Daniel Beer <dlbeer@gmail.com> wrote:
> On Fri, Dec 23, 2022 at 04:16:59PM +0000, Jonathan Cameron wrote:
> > > ad_sigma_delta waits for a conversion which terminates with the firing
> > > of a one-shot IRQ handler. In this handler, the interrupt is disabled
> > > and a completion is set.
> > >
> > > Meanwhile, the thread that initiated the conversion is waiting on the
> > > completion to know when the conversion happened. If this wait times out,
> > > the conversion is aborted and IRQs are disabled. But the IRQ may fire
> > > anyway between the time the completion wait times out and the disabling
> > > of interrupts. If this occurs, we get a double-disabled interrupt.
> >
> > Ouch and good work tracking it down. just to check, did you see this
> > bug happen in the wild or spotted by code inspection?
>
> Hi Jonathan,
>
> Thanks for reviewing. It was by inspection -- I'd originally thought
> about it and fixed in in a similar way in this patch:
>
> https://lore.kernel.org/all/61dd3e0c.1c69fb81.cea15.8d98@mx.google.com/
>
> But since that's not applied, I thought I'd better put together a
> separate fix for the time being.
>
> > Given that timeout generally indicates hardware failure, I'm not sure
> > how critical this is to fix.
>
> Probably not very critical. I think you'd have to be pretty unlucky to
> encounter it.
>
> > Is this fix sufficient? If the interrupt is being handled on a different
> > CPU to the caller of this function, I think we can still race enough that
> > this fails to fix it up. Might need a spinlock to prevent that.
> >
> > CPU 0 CPU 1
> > ad_sd_data_rdy_trig_poll() ad_sd_wait_and_disable()
> >
> > //wait_for_completion ends
> >
> > Interrupt
> > disable_irq()
> > if (sigma-delta->irq_dis) !true
> > else
> > sigma_delta->irq_dis = true
> >
> > disable_irq_nosync(irq)
> > sigma_delta->irq_dis = true;
> >
> > So we still end up with a doubly disabled irq. Add a spinlock to make the
> > disable and the setting of sigma_delta->irq_dis atomic then it should all be fine.
>
> My understanding is that the suffix-less version of disable_irq would
> wait for all running handlers on other CPUs (i.e.
> ad_sd_data_rdy_trig_poll) to finish before proceeding, which would
> prevent this from happening. Is that not the case?
Ah. I'd missed that - it is indeed documented as waiting for
pending irq handlers so that race doesn't exist.
>
> But now that you mention it, there is another small problem: in the case
> where the conversion doesn't time out, the interrupt handler will call
> complete() and then perform some operations on the struct
> ad_sigma_delta.
>
> This is always ok on a single processor, but if there are multiple CPUs
> there is possibly a brief period where both the interrupt handler and
> the waiting thread are accessing the ad_sigma_delta struct without
> synchronization between them.
>
> Not sure if that's really a problem in practice, but I think an easy way
> to rule it out would just be to move the complete() call to the bottom
> of the handler and make sure it doesn't touch the structure again after
> that.
Hmm. At first glance, you are correct that moving completion later probably
makes sense. There may be some subtleties in that ordering though so I definitely want
some feedback from those more familiar with this driver than I am before
taking this patch or that further change.
Jonathan
>
> Cheers,
> Daniel
>
prev parent reply other threads:[~2022-12-31 14:15 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-19 7:48 [PATCH] ad_sigma_delta: fix race between IRQ and completion Daniel Beer
2022-12-23 16:16 ` Jonathan Cameron
2022-12-24 2:31 ` Daniel Beer
2022-12-31 14:28 ` Jonathan Cameron [this message]
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=20221231142832.49cb47c7@jic23-huawei \
--to=jic23@kernel.org \
--cc=Michael.Hennerich@analog.com \
--cc=dlbeer@gmail.com \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/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