All of lore.kernel.org
 help / color / mirror / Atom feed
* [BUG] iio: light: opt3001: possible deadlock in opt3001_read_raw() and opt3001_irq()
@ 2022-02-07 15:41 Jia-Ju Bai
  2022-02-07 20:39 ` Jonathan Cameron
  0 siblings, 1 reply; 2+ messages in thread
From: Jia-Ju Bai @ 2022-02-07 15:41 UTC (permalink / raw)
  To: jic23, lars, valek, gwendal; +Cc: linux-iio, linux-kernel

Hello,

My static analysis tool reports a possible deadlock in the opt3001 
driver in Linux 5.16:

opt3001_read_raw()
   mutex_lock(&opt->lock); --> Line 399 (Lock A)
   opt3001_get_lux()
     wait_event_timeout(opt->result_ready_queue, ...) --> Line 276 (Wait X)
   mutex_lock(&opt->lock); --> Line 412 (Unlock A)

opt3001_irq()
   mutex_lock(&opt->lock); --> Line 693 (Lock A)
   mutex_unlock(&opt->lock); --> Line 730 (Unlock A)
   wake_up(&opt->result_ready_queue); --> Line 733 (Wake X)

When opt3001_read_raw() is executed, "Wait X" is performed by holding 
"Lock A". If opt3001_irq() is executed at this time, "Wake X" cannot be 
performed to wake up "Wait X" in opt3001_read_raw(), because "Lock A" 
has been already hold by opt3001_read_raw(), causing a possible deadlock.
I find that "Wait X" is performed with a timeout, to relieve the 
possible deadlock; but I think this timeout can cause inefficient execution.

I am not quite sure whether this possible problem is real and how to fix 
it if it is real.
Any feedback would be appreciated, thanks :)


Best wishes,
Jia-Ju Bai

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [BUG] iio: light: opt3001: possible deadlock in opt3001_read_raw() and opt3001_irq()
  2022-02-07 15:41 [BUG] iio: light: opt3001: possible deadlock in opt3001_read_raw() and opt3001_irq() Jia-Ju Bai
@ 2022-02-07 20:39 ` Jonathan Cameron
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Cameron @ 2022-02-07 20:39 UTC (permalink / raw)
  To: Jia-Ju Bai; +Cc: lars, valek, gwendal, linux-iio, linux-kernel

On Mon, 7 Feb 2022 23:41:49 +0800
Jia-Ju Bai <baijiaju1990@gmail.com> wrote:

> Hello,
> 
> My static analysis tool reports a possible deadlock in the opt3001 
> driver in Linux 5.16:
> 
> opt3001_read_raw()
>    mutex_lock(&opt->lock); --> Line 399 (Lock A)
>    opt3001_get_lux()
>      wait_event_timeout(opt->result_ready_queue, ...) --> Line 276 (Wait X)
>    mutex_lock(&opt->lock); --> Line 412 (Unlock A)
> 
> opt3001_irq()
>    mutex_lock(&opt->lock); --> Line 693 (Lock A)
>    mutex_unlock(&opt->lock); --> Line 730 (Unlock A)
>    wake_up(&opt->result_ready_queue); --> Line 733 (Wake X)
> 
> When opt3001_read_raw() is executed, "Wait X" is performed by holding 
> "Lock A". If opt3001_irq() is executed at this time, "Wake X" cannot be 
> performed to wake up "Wait X" in opt3001_read_raw(), because "Lock A" 
> has been already hold by opt3001_read_raw(), causing a possible deadlock.
> I find that "Wait X" is performed with a timeout, to relieve the 
> possible deadlock; but I think this timeout can cause inefficient execution.
Hi Jia-Ju Bai,

There is a quirk in here thatyou haven't mentioned.  The "magic"
opt->ok_to_ignore_lock.

So there are two cases:
1) No irq in use.  In that case the opt3001_irq() will never run and we
   just sleep + check a status flag. That's the best we can do without
   an interrupt.
2) irq in use, the ok_to_ignore_lock = true statement occurs and
   in the opt3001_irq() the locks are never taken hence no deadlock.

It is a very odd bit of code though so I'd be surprised if a static
analyser hadn't highlighted it as a possible deadlock!

Now, I'm not immediately sure why the driver is done like this as opposed
to a more complete wait_for_completion() in read_raw(), complete() in the irq
handler and do the actual read of the data back in read_raw().
It's probably related to the other interrupt sources that we need to
differentiate from in the interrupt handler.

The lock definition is missing an documentation of exactly what it's scope
is which definitely doesn't help us understand this unusual structure.

Thanks,

Jonathan


> 
> I am not quite sure whether this possible problem is real and how to fix 
> it if it is real.
> Any feedback would be appreciated, thanks :)
> 
> 
> Best wishes,
> Jia-Ju Bai


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-02-07 20:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-07 15:41 [BUG] iio: light: opt3001: possible deadlock in opt3001_read_raw() and opt3001_irq() Jia-Ju Bai
2022-02-07 20:39 ` Jonathan Cameron

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.