[PATCH] ALSA: usb-audio: fix recursive lock of PCM substream when starting playback PCM substream
From: Takashi Sakamoto
Date: Sat Jul 03 2021 - 02:23:25 EST
A commit 9ce650a75a3b ("ALSA: usb-audio: Reduce latency at playback start")
unfortunately introduced the call of snd_pcm_period_elapsed() under acquired
lock of PCM substream. This causes recursive lock and results in dead-lock.
->ioctl(2)
(sound/core/pcm_native.c)
->snd_pcm_stream_lock_irqsave() <-
...
->struct snd_pcm_ops.trigger()
(sound/usb/pcm.c)
= snd_usb_substream_playback_trigger()
->start_endpoints()
(sound/usb/endpoint.c)
->snd_usb_endpoint_start()
->prepare_outbound_urb()
->struct snd_usb_endpoint.prepare_data_urb()
(sound/usb/pcm.c)
= prepare_playback_urb()
(sound/core/pcm_lib.c)
->snd_pcm_period_elapsed()
(sound/core/pcm_native.c)
->snd_pcm_stream_lock_irqsave() <-
This commit fixes the issue to use newly added function;
snd_pcm_period_elapsed_under_stream_lock() with condition to check running
context.
Reported-by: Hector Martin <marcan@xxxxxxxxx>
Fixes: 9ce650a75a3b ("ALSA: usb-audio: Reduce latency at playback start")
Signed-off-by: Takashi Sakamoto <o-takashi@xxxxxxxxxxxxx>
---
sound/usb/pcm.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c
index c66831ee15f9..235070f0236a 100644
--- a/sound/usb/pcm.c
+++ b/sound/usb/pcm.c
@@ -1395,8 +1395,16 @@ static void prepare_playback_urb(struct snd_usb_substream *subs,
spin_unlock_irqrestore(&subs->lock, flags);
urb->transfer_buffer_length = bytes;
- if (period_elapsed)
- snd_pcm_period_elapsed(subs->pcm_substream);
+ if (period_elapsed) {
+ // The callback of struct snd_pcm_ops.trigger with SNDRV_PCM_TRIGGER_START command
+ // can reach here, under acquired lock of PCM substream. To avoid dead-lock, check
+ // current context and call corresponding function.
+ if (in_softirq()) {
+ snd_pcm_period_elapsed(subs->pcm_substream);
+ } else {
+ snd_pcm_period_elapsed_under_stream_lock(subs->pcm_substream);
+ }
+ }
}
/*
--
2.30.2
======== 8< --------
Thanks
Takashi (not subsystem maintainer) Sakamoto