[PATCH] ALSA: pcm: do not free a pcm with opened substreams on single-device teardown
From: Hanxiang Xu
Date: Tue Dec 01 2026 - 22:11:28 EST
An independent teardown path (e.g. snd_emu8000_remove() freeing the
pcm it created at probe time) calls snd_device_free(card, pcm), which
runs snd_pcm_dev_disconnect() and then snd_pcm_free(): kfree(substream)
for every substream and kfree(pcm) -- none of which waits for the open
file descriptors on /dev/snd/pcmC*D*. In-flight and later ioctl/read
operations and the final snd_pcm_release() dereference the freed
substream and pcm.
Refuse the free while either stream has opened substreams:
__snd_device_free() unlinks the snd_device wrapper but leaves the pcm
object tree alive for the open descriptors. A driver that properly
drains its users before teardown is unaffected, since
substream_opened is zero at that point.
Cc: <stable@xxxxxxxxxxxxxxx>
Signed-off-by: Hanxiang Xu <xuhx@xxxxxxxxxxx>
---
sound/core/pcm.c | 3 +++
1 file changed, 3 insertions(+), 0 deletions(-)
diff --git a/sound/core/pcm.c b/sound/core/pcm.c
index 6c82744..d5617fe 100644
--- a/sound/core/pcm.c
+++ b/sound/core/pcm.c
@@ -861,6 +861,9 @@ static int snd_pcm_free(struct snd_pcm *pcm)
{
if (!pcm)
return 0;
+ if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_opened ||
+ pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_opened)
+ return -EBUSY;
if (!pcm->internal) {
scoped_guard(mutex, ®ister_mutex)
pcm_call_notify(pcm, n_unregister);
--
2.43.0