The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Shuangpeng Bai <shuangpeng.kernel@gmail.com>
To: Mauro Carvalho Chehab <mchehab@kernel.org>
Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org,
	Shuangpeng Bai <shuangpeng.kernel@gmail.com>
Subject: [PATCH] media: em28xx: fix audio extension ref leak
Date: Sat,  4 Jul 2026 16:02:05 -0400	[thread overview]
Message-ID: <20260704200301.3968952-1-shuangpeng.kernel@gmail.com> (raw)

em28xx_audio_init() takes a reference on the em28xx device before
creating and registering the ALSA card. If snd_card_new(),
snd_pcm_new(), audio URB setup, or snd_card_register() fails, the
function returns an error without dropping the reference.

The em28xx extension framework ignores init callback errors, so the
failing init path must balance the reference itself.

Drop the reference on every audio init error path. Only store the ALSA
card in the audio extension after the card is registered successfully,
so em28xx_audio_fini() can use that pointer as the success marker. This
avoids a second put during disconnect or module unload after init has
already failed and dropped the reference.

Signed-off-by: Shuangpeng Bai <shuangpeng.kernel@gmail.com>
---
 drivers/media/usb/em28xx/em28xx-audio.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/drivers/media/usb/em28xx/em28xx-audio.c b/drivers/media/usb/em28xx/em28xx-audio.c
index ccff6be5599e..f583837f9a85 100644
--- a/drivers/media/usb/em28xx/em28xx-audio.c
+++ b/drivers/media/usb/em28xx/em28xx-audio.c
@@ -829,10 +829,9 @@ static int em28xx_audio_init(struct em28xx *dev)
 	err = snd_card_new(&dev->intf->dev, index[devnr], "Em28xx Audio",
 			   THIS_MODULE, 0, &card);
 	if (err < 0)
-		return err;
+		goto ref_put;
 
 	spin_lock_init(&adev->slock);
-	adev->sndcard = card;
 	adev->udev = udev;
 
 	err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
@@ -875,6 +874,8 @@ static int em28xx_audio_init(struct em28xx *dev)
 	if (err < 0)
 		goto urb_free;
 
+	adev->sndcard = card;
+
 	dev_info(&dev->intf->dev, "Audio extension successfully initialized\n");
 	return 0;
 
@@ -883,7 +884,9 @@ static int em28xx_audio_init(struct em28xx *dev)
 
 card_free:
 	snd_card_free(card);
-	adev->sndcard = NULL;
+
+ref_put:
+	kref_put(&dev->ref, em28xx_free_device);
 
 	return err;
 }
@@ -912,9 +915,9 @@ static int em28xx_audio_fini(struct em28xx *dev)
 
 		snd_card_free(dev->adev.sndcard);
 		dev->adev.sndcard = NULL;
-	}
 
-	kref_put(&dev->ref, em28xx_free_device);
+		kref_put(&dev->ref, em28xx_free_device);
+	}
 	return 0;
 }
 
-- 
2.43.0


                 reply	other threads:[~2026-07-04 20:03 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20260704200301.3968952-1-shuangpeng.kernel@gmail.com \
    --to=shuangpeng.kernel@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    /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