[PATCH] ALSA: pcm: Fix potential device leak on partial registration failure
From: Guangshuo Li
Date: Tue Nov 17 2026 - 09:47:23 EST
If capture registration fails after playback was registered, the PCM
remains in the BUILD state. Card teardown therefore skips its disconnect
callback, leaving the playback device registered and holding its parent
card reference.
Unregister previously registered streams and release their timers before
returning the registration error.
This issue was found by our static analysis tool under development.
Fixes: ef46c7af93f9 ("ALSA: pcm: Embed struct device")
Signed-off-by: Guangshuo Li <liguangshuo@xxxxxxxxxxxxxxx>
---
sound/core/pcm.c | 18 ++++++++++++++----
1 file changed, 14 insertions(+), 4 deletions(-)
diff --git a/sound/core/pcm.c b/sound/core/pcm.c
index 064df4cd3b50..7343b366986c 100644
--- a/sound/core/pcm.c
+++ b/sound/core/pcm.c
@@ -1080,16 +1080,26 @@ static int snd_pcm_dev_register(struct snd_device *device)
err = snd_register_device(devtype, pcm->card, pcm->device,
&snd_pcm_f_ops[cidx], pcm,
pcm->streams[cidx].dev);
- if (err < 0) {
- list_del_init(&pcm->list);
- return err;
- }
+ if (err < 0)
+ goto error;
for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
snd_pcm_timer_init(substream);
}
pcm_call_notify(pcm, n_register);
+ return 0;
+
+error:
+ while (--cidx >= 0) {
+ if (!pcm->streams[cidx].substream)
+ continue;
+ snd_unregister_device(pcm->streams[cidx].dev);
+ for (substream = pcm->streams[cidx].substream; substream;
+ substream = substream->next)
+ snd_pcm_timer_done(substream);
+ }
+ list_del_init(&pcm->list);
return err;
}
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
--
2.43.0