All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ALSA: pcm: do not free a pcm with opened substreams on single-device teardown
@ 2026-12-02  2:09 Hanxiang Xu
  2026-12-02  3:22 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Hanxiang Xu @ 2026-12-02  2:09 UTC (permalink / raw)
  To: Takashi Iwai, Jaroslav Kysela; +Cc: linux-sound, linux-kernel

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@vger.kernel.org>
Signed-off-by: Hanxiang Xu <xuhx@hust.edu.cn>
---
 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, &register_mutex)
 			pcm_call_notify(pcm, n_unregister);
-- 
2.43.0


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

end of thread, other threads:[~2026-12-02  3:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  2:09 [PATCH] ALSA: pcm: do not free a pcm with opened substreams on single-device teardown Hanxiang Xu
2026-12-02  3:22 ` sashiko-bot

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.