[BUG] ALSA: core: possible deadlock involving waiting and locking operations

From: Jia-Ju Bai
Date: Fri Jan 28 2022 - 22:33:42 EST


Hello,

My static analysis tool reports a possible deadlock in the sound driver in Linux 5.10:

snd_card_disconnect_sync()
  spin_lock_irq(&card->files_lock); --> Line 461 (Lock A)
  wait_event_lock_irq(card->remove_sleep, ...); --> Line 462 (Wait X)
  spin_unlock_irq(&card->files_lock); --> Line 465 (Unlock A)

snd_hwdep_release()
  mutex_lock(&hw->open_mutex); --> Line 152 (Lock B)
  mutex_unlock(&hw->open_mutex); --> Line 157 (Unlock B)
  snd_card_file_remove()
    wake_up_all(&card->remove_sleep); --> Line 976 (Wake X)

snd_hwdep_open()
  mutex_lock(&hw->open_mutex); --> Line 95 (Lock B)
  snd_card_file_add()
    spin_lock(&card->files_lock); --> Line 932 (Lock A)
    spin_unlock(&card->files_lock); --> Line 940 (Unlock A)
  mutex_unlock(&hw->open_mutex); --> Line 139 (Unlock B)

When snd_card_disconnect_sync() is executed, "Wait X" is performed by holding "Lock A". If snd_hwdep_open() is executed at this time, it holds "Lock B" and then waits for acquiring "Lock A". If snd_hwdep_release() is executed at this time, it waits for acquiring "Lock B", and thus "Wake X" cannot be performed to wake up "Wait X" in snd_card_disconnect_sync(), causing a possible deadlock.

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