mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Guangshuo Li <liguangshuo@mail.sdu.edu.cn>
To: Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.com>
Cc: linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] ALSA: pcm: Fix potential device leak on partial registration failure
Date: Tue, 17 Nov 2026 22:47:05 +0800	[thread overview]
Message-ID: <20261117-571e631490-alsa-pcm-0@mail.sdu.edu.cn> (raw)

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@mail.sdu.edu.cn>
---
 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


             reply	other threads:[~2026-11-17 14:47 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 14:47 Guangshuo Li [this message]
2026-11-18 12:52 ` Takashi Iwai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261117-571e631490-alsa-pcm-0@mail.sdu.edu.cn \
    --to=liguangshuo@mail.sdu.edu.cn \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=perex@perex.cz \
    --cc=tiwai@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®