diff mbox series

ALSA: Fix deadlocks with kctl removals at disconnection

Message ID 20240510101424.6279-1-tiwai@suse.de (mailing list archive)
State Accepted
Commit 87988a534d8e12f2e6fc01fe63e6c1925dc5307c
Headers show
Series ALSA: Fix deadlocks with kctl removals at disconnection | expand

Commit Message

Takashi Iwai May 10, 2024, 10:14 a.m. UTC
In snd_card_disconnect(), we set card->shutdown flag at the beginning,
call callbacks and do sync for card->power_ref_sleep waiters at the
end.  The callback may delete a kctl element, and this can lead to a
deadlock when the device was in the suspended state.  Namely:

* A process waits for the power up at snd_power_ref_and_wait() in
  snd_ctl_info() or read/write() inside card->controls_rwsem.

* The system gets disconnected meanwhile, and the driver tries to
  delete a kctl via snd_ctl_remove*(); it tries to take
  card->controls_rwsem again, but this is already locked by the
  above.  Since the sleeper isn't woken up, this deadlocks.

An easy fix is to wake up sleepers before processing the driver
disconnect callbacks but right after setting the card->shutdown flag.
Then all sleepers will abort immediately, and the code flows again.

So, basically this patch moves the wait_event() call at the right
timing.  While we're at it, just to be sure, call wait_event_all()
instead of wait_event(), although we don't use exclusive events on
this queue for now.

Link: https://bugzilla.kernel.org/show_bug.cgi?id=218816
Cc: <stable@vger.kernel.org>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
 sound/core/init.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

Comments

Jaroslav Kysela May 10, 2024, 11:47 a.m. UTC | #1
On 10. 05. 24 12:14, Takashi Iwai wrote:
> In snd_card_disconnect(), we set card->shutdown flag at the beginning,
> call callbacks and do sync for card->power_ref_sleep waiters at the
> end.  The callback may delete a kctl element, and this can lead to a
> deadlock when the device was in the suspended state.  Namely:
> 
> * A process waits for the power up at snd_power_ref_and_wait() in
>    snd_ctl_info() or read/write() inside card->controls_rwsem.
> 
> * The system gets disconnected meanwhile, and the driver tries to
>    delete a kctl via snd_ctl_remove*(); it tries to take
>    card->controls_rwsem again, but this is already locked by the
>    above.  Since the sleeper isn't woken up, this deadlocks.
> 
> An easy fix is to wake up sleepers before processing the driver
> disconnect callbacks but right after setting the card->shutdown flag.
> Then all sleepers will abort immediately, and the code flows again.
> 
> So, basically this patch moves the wait_event() call at the right
> timing.  While we're at it, just to be sure, call wait_event_all()
> instead of wait_event(), although we don't use exclusive events on
> this queue for now.
> 
> Link: https://bugzilla.kernel.org/show_bug.cgi?id=218816
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Takashi Iwai <tiwai@suse.de>

LGTM

Reviewed-by: Jaroslav Kysela <perex@perex.cz>
diff mbox series

Patch

diff --git a/sound/core/init.c b/sound/core/init.c
index 4ed5037d8693..89c8354862c4 100644
--- a/sound/core/init.c
+++ b/sound/core/init.c
@@ -516,6 +516,14 @@  void snd_card_disconnect(struct snd_card *card)
 		}
 	}
 
+#ifdef CONFIG_PM
+	/* wake up sleepers here before other callbacks for avoiding potential
+	 * deadlocks with other locks (e.g. in kctls);
+	 * then this notifies the shutdown and sleepers would abort immediately
+	 */
+	wake_up_all(&card->power_sleep);
+#endif
+
 	/* notify all connected devices about disconnection */
 	/* at this point, they cannot respond to any calls except release() */
 
@@ -543,7 +551,6 @@  void snd_card_disconnect(struct snd_card *card)
 	}
 
 #ifdef CONFIG_PM
-	wake_up(&card->power_sleep);
 	snd_power_sync_ref(card);
 #endif
 }