Skip to contentSkip to conversation

ALSA: ice1712: Fix missing snd_card_free() at probe error

PATCH In review Haotian Zhang 2 participants 5 messages Aug 19 – Aug 20 Sound

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: ice1712: Fix missing snd_card_free() at probe error

snd_ice1712_probe() performs multiple initialization steps after snd_card_new(), but directly returns on failures from later steps without releasing the ALSA card, causing resource leaks when probing fails.

Route all initialization failures after snd_card_new() to a common error path and call snd_card_free() before returning the original error code.

Fixes ca642da4b33d ("ALSA: ice1712: Allocate resources with device-managed APIs")
---
 sound/pci/ice1712/ice1712.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
sound/pci/ice1712/ice1712.c +14 -11
index 1e39b985bef2..745af0c7020e 100644
@@ -2539,7 +2539,7 @@ static int snd_ice1712_probe(struct pci_dev *pci,
 	err = snd_ice1712_create(card, pci, model[dev], omni[dev],
 				 cs8427_timeout[dev], dxr_enable[dev]);
 	if (err < 0)
-		return err;
+		goto error;
 
 	for (tbl = card_tables; *tbl; tbl++) {
 		for (c = *tbl; c->subvendor; c++) {
@@ -2550,7 +2550,7 @@ static int snd_ice1712_probe(struct pci_dev *pci,
 				if (c->chip_init) {
 					err = c->chip_init(ice);
 					if (err < 0)
-						return err;
+						goto error;
 				}
 				ice->card_info = c;
 				goto __found;
@@ -2562,32 +2562,32 @@ static int snd_ice1712_probe(struct pci_dev *pci,
 
 	err = snd_ice1712_pcm_profi(ice, pcm_dev++);
 	if (err < 0)
-		return err;
+		goto error;
 
 	if (ice_has_con_ac97(ice)) {
 		err = snd_ice1712_pcm(ice, pcm_dev++);
 		if (err < 0)
-			return err;
+			goto error;
 	}
 
 	err = snd_ice1712_ac97_mixer(ice);
 	if (err < 0)
-		return err;
+		goto error;
 
 	err = snd_ice1712_build_controls(ice);
 	if (err < 0)
-		return err;
+		goto error;
 
 	if (c->build_controls) {
 		err = c->build_controls(ice);
 		if (err < 0)
-			return err;
+			goto error;
 	}
 
 	if (ice_has_con_ac97(ice)) {
 		err = snd_ice1712_pcm_ds(ice, pcm_dev++);
 		if (err < 0)
-			return err;
+			goto error;
 	}
 
 	if (!c->no_mpu401) {
@@ -2597,7 +2597,7 @@ static int snd_ice1712_probe(struct pci_dev *pci,
 			MPU401_INFO_INTEGRATED | MPU401_INFO_IRQ_HOOK,
 			-1, &ice->rmidi[0]);
 		if (err < 0)
-			return err;
+			goto error;
 		if (c->mpu401_1_name)
 			/*  Preferred name available in card_info */
 			snprintf(ice->rmidi[0]->name,
@@ -2613,7 +2613,7 @@ static int snd_ice1712_probe(struct pci_dev *pci,
 				-1, &ice->rmidi[1]);
 
 			if (err < 0)
-				return err;
+				goto error;
 			if (c->mpu401_2_name)
 				/*  Preferred name available in card_info */
 				snprintf(ice->rmidi[1]->name,
@@ -2630,10 +2630,13 @@ static int snd_ice1712_probe(struct pci_dev *pci,
 
 	err = snd_card_register(card);
 	if (err < 0)
-		return err;
+		goto error;
 	pci_set_drvdata(pci, card);
 	dev++;
 	return 0;
+error:
+	snd_card_free(card);
+	return err;
 }
 
 #ifdef CONFIG_PM_SLEEP
Signature · 1 line
2.43.0
#1 lore raw
Quoted 6 lines · Haotian Zhang

snd_ice1712_probe() performs multiple initialization steps after snd_card_new(), but directly returns on failures from later steps without releasing the ALSA card, causing resource leaks when probing fails.

Quoted 6 lines

Route all initialization failures after snd_card_new() to a common error path and call snd_card_free() before returning the original error code.

Fixes ca642da4b33d ("ALSA: ice1712: Allocate resources with device-managed APIs")

Actually, this was rather a forgotten replacement of snd_card_new() with snd_devm_card_new(), as implemented in a similar code ice1724.c. But the error handling before the registration is still needed (it was fixed later for ice1724.c, too), so it'll be something like below.

thanks,

Takashi

-- 8< --

sound/pci/ice1712/ice1712.c +10 -4
@@ -2519,8 +2519,8 @@ static int snd_ice1712_create(struct snd_card *card,
 
 static struct snd_ice1712_card_info no_matched;
 
-static int snd_ice1712_probe(struct pci_dev *pci,
-			     const struct pci_device_id *pci_id)
+static int __snd_ice1712_probe(struct pci_dev *pci,
+			       const struct pci_device_id *pci_id)
 {
 	static int dev;
 	struct snd_card *card;
@@ -2535,8 +2535,8 @@ static int snd_ice1712_probe(struct pci_dev *pci,
 		return -ENOENT;
 	}
 
-	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
-			   sizeof(*ice), &card);
+	err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
+				sizeof(*ice), &card);
 	if (err < 0)
 		return err;
 	ice = card->private_data;
@@ -2644,6 +2644,12 @@ static int snd_ice1712_probe(struct pci_dev *pci,
 	return 0;
 }
 
+static int snd_ice1712_probe(struct pci_dev *pci,
+			     const struct pci_device_id *pci_id)
+{
+	return snd_card_free_on_error(&pci->dev, __snd_ice1712_probe(pci, pci_id));
+}
+
 #ifdef CONFIG_PM_SLEEP
 static int snd_ice1712_suspend(struct device *dev)
 {
#2 lore raw
Quoted 13 lines · Takashi Iwai
Quoted 10 lines · Haotian Zhang

snd_ice1712_probe() performs multiple initialization steps after snd_card_new(), but directly returns on failures from later steps without releasing the ALSA card, causing resource leaks when probing fails.

Route all initialization failures after snd_card_new() to a common error path and call snd_card_free() before returning the original error code.

Quoted 7 lines
Quoted 2 lines
Fixes ca642da4b33d ("ALSA: ice1712: Allocate resources with device-managed APIs")

Actually, this was rather a forgotten replacement of snd_card_new() with snd_devm_card_new(), as implemented in a similar code ice1724.c. But the error handling before the registration is still needed (it was fixed later for ice1724.c, too), so it'll be something like below.

On the second thought, a more elegant alternative would be to use the auto-cleanup with snd_card_unref. A totally untested patch below.

Takashi

-- 8< --

sound/pci/ice1712/ice1712.c +4 -3
@@ -2523,7 +2523,7 @@ static int snd_ice1712_probe(struct pci_dev *pci,
 			     const struct pci_device_id *pci_id)
 {
 	static int dev;
-	struct snd_card *card;
+	struct snd_card *card __free(snd_card_unref) = NULL;
 	struct snd_ice1712 *ice;
 	int pcm_dev = 0, err;
 	const struct snd_ice1712_card_info * const *tbl, *c;
@@ -2535,8 +2535,8 @@ static int snd_ice1712_probe(struct pci_dev *pci,
 		return -ENOENT;
 	}
 
-	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
-			   sizeof(*ice), &card);
+	err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
+				sizeof(*ice), &card);
 	if (err < 0)
 		return err;
 	ice = card->private_data;
@@ -2640,6 +2640,7 @@ static int snd_ice1712_probe(struct pci_dev *pci,
 	if (err < 0)
 		return err;
 	pci_set_drvdata(pci, card);
+	card = NULL; /* probe succeeded, don't release as error */
 	dev++;
 	return 0;
 }
#3 lore raw

PATCH v2 1/1 ALSA: ice1712: Fix the card leak at probe error with the auto-cleanup

In reply to Haotian Zhang

snd_ice1712_probe() performs multiple initialization steps after snd_card_new(), but directly returns on failures from later steps without releasing the ALSA card, causing resource leaks when probing fails.

Use snd_devm_card_new() together with scope-based cleanup via __free(snd_card_unref), and clear the card pointer after successful registration to keep it alive.

Fixes ca642da4b33d ("ALSA: ice1712: Allocate resources with device-managed APIs")

---
changes in v2:
- Use the auto-cleanup instead as suggested by Takashi Iwai.

---
 sound/pci/ice1712/ice1712.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
sound/pci/ice1712/ice1712.c +4 -3
index 1e39b985bef2..6dc9d1183d37 100644
@@ -2515,7 +2515,7 @@ static int snd_ice1712_probe(struct pci_dev *pci,
 			     const struct pci_device_id *pci_id)
 {
 	static int dev;
-	struct snd_card *card;
+	struct snd_card *card __free(snd_card_unref) = NULL;
 	struct snd_ice1712 *ice;
 	int pcm_dev = 0, err;
 	const struct snd_ice1712_card_info * const *tbl, *c;
@@ -2527,8 +2527,8 @@ static int snd_ice1712_probe(struct pci_dev *pci,
 		return -ENOENT;
 	}
 
-	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
-			   sizeof(*ice), &card);
+	err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
+				sizeof(*ice), &card);
 	if (err < 0)
 		return err;
 	ice = card->private_data;
@@ -2632,6 +2632,7 @@ static int snd_ice1712_probe(struct pci_dev *pci,
 	if (err < 0)
 		return err;
 	pci_set_drvdata(pci, card);
+	card = NULL; /* probe succeeded, don't release as error */
 	dev++;
 	return 0;
 }
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH v2] ALSA: ice1712: Fix the card leak at probe error with the auto-cleanup

Quoted 6 lines · Haotian Zhang

snd_ice1712_probe() performs multiple initialization steps after snd_card_new(), but directly returns on failures from later steps without releasing the ALSA card, causing resource leaks when probing fails.

Quoted 7 lines

Use snd_devm_card_new() together with scope-based cleanup via __free(snd_card_unref), and clear the card pointer after successful registration to keep it alive.

Fixes ca642da4b33d ("ALSA: ice1712: Allocate resources with device-managed APIs")

Applied now. Thanks.

Takashi