Skip to contentSkip to conversation

ALSA: via82xx: Use common error handling code in snd_via82xx_create()

PATCH In review SF Markus Elfring 3 participants 7 messages Aug 22, 2017 – Nov 19, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: via82xx: Use common error handling code in snd_via82xx_create()

From: Markus Elfring <[email protected]>
Date: Tue, 22 Aug 2017 19:37:26 +0200

Add jump targets so that a bit of exception handling can be better reused at the end of this function.

This issue was detected by using the Coccinelle software.

---
 sound/pci/via82xx_modem.c | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)
sound/pci/via82xx_modem.c +19 -14
index 55f79b2599e7..8659776887b2 100644
@@ -1110,8 +1110,8 @@ static int snd_via82xx_create(struct snd_card *card,
 		return err;
 
 	if ((chip = kzalloc(sizeof(*chip), GFP_KERNEL)) = NULL) {
-		pci_disable_device(pci);
-		return -ENOMEM;
+		err = -ENOMEM;
+		goto disable_device;
 	}
 
 	spin_lock_init(&chip->reg_lock);
@@ -1121,30 +1121,27 @@ static int snd_via82xx_create(struct snd_card *card,
 
 	if ((err = pci_request_regions(pci, card->driver)) < 0) {
 		kfree(chip);
-		pci_disable_device(pci);
-		return err;
+		goto disable_device;
 	}
 	chip->port = pci_resource_start(pci, 0);
 	if (request_irq(pci->irq, snd_via82xx_interrupt, IRQF_SHARED,
 			KBUILD_MODNAME, chip)) {
 		dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
-		snd_via82xx_free(chip);
-		return -EBUSY;
+		err = -EBUSY;
+		goto free_chip;
 	}
 	chip->irq = pci->irq;
 	if (ac97_clock >= 8000 && ac97_clock <= 48000)
 		chip->ac97_clock = ac97_clock;
 	synchronize_irq(chip->irq);
 
-	if ((err = snd_via82xx_chip_init(chip)) < 0) {
-		snd_via82xx_free(chip);
-		return err;
-	}
+	err = snd_via82xx_chip_init(chip);
+	if (err < 0)
+		goto free_chip;
 
-	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
-		snd_via82xx_free(chip);
-		return err;
-	}
+	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
+	if (err < 0)
+		goto free_chip;
 
 	/* The 8233 ac97 controller does not implement the master bit
 	 * in the pci command register. IMHO this is a violation of the PCI spec.
@@ -1153,6 +1150,14 @@ static int snd_via82xx_create(struct snd_card *card,
 
 	*r_via = chip;
 	return 0;
+
+disable_device:
+	pci_disable_device(pci);
+	goto exit;
+free_chip:
+	snd_via82xx_free(chip);
+exit:
+	return err;
 }
 
 
Signature · 1 line
2.14.0
#1 lore raw
Quoted 71 lines · SF Markus Elfring

From: Markus Elfring <[email protected]>
Date: Tue, 22 Aug 2017 19:37:26 +0200

Add jump targets so that a bit of exception handling can be better reused at the end of this function.

This issue was detected by using the Coccinelle software.

---
 sound/pci/via82xx_modem.c | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)
sound/pci/via82xx_modem.c +11 -14
index 55f79b2599e7..8659776887b2 100644
@@ -1110,8 +1110,8 @@ static int snd_via82xx_create(struct snd_card *card,
 		return err;
 
 	if ((chip = kzalloc(sizeof(*chip), GFP_KERNEL)) == NULL) {
-		pci_disable_device(pci);
-		return -ENOMEM;
+		err = -ENOMEM;
+		goto disable_device;
 	}
 
 	spin_lock_init(&chip->reg_lock);
@@ -1121,30 +1121,27 @@ static int snd_via82xx_create(struct snd_card *card,
 
 	if ((err = pci_request_regions(pci, card->driver)) < 0) {
 		kfree(chip);
-		pci_disable_device(pci);
-		return err;
+		goto disable_device;
 	}
 	chip->port = pci_resource_start(pci, 0);
 	if (request_irq(pci->irq, snd_via82xx_interrupt, IRQF_SHARED,
 			KBUILD_MODNAME, chip)) {
 		dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
-		snd_via82xx_free(chip);
-		return -EBUSY;
+		err = -EBUSY;
+		goto free_chip;
 	}
 	chip->irq = pci->irq;
 	if (ac97_clock >= 8000 && ac97_clock <= 48000)
 		chip->ac97_clock = ac97_clock;
 	synchronize_irq(chip->irq);
 
-	if ((err = snd_via82xx_chip_init(chip)) < 0) {
-		snd_via82xx_free(chip);
-		return err;
-	}
+	err = snd_via82xx_chip_init(chip);
+	if (err < 0)
+		goto free_chip;
 
-	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
-		snd_via82xx_free(chip);
-		return err;
-	}
+	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
+	if (err < 0)
+		goto free_chip;
 
 	/* The 8233 ac97 controller does not implement the master bit
 	 * in the pci command register. IMHO this is a violation of the PCI spec.
@@ -1153,6 +1150,14 @@ static int snd_via82xx_create(struct snd_card *card,
 

sound/pci/via82xx_modem.c static int snd_via82xx_create(struct snd_card *card, · line 1160

Quoted 10 lines
sound/pci/via82xx_modem.c +8 -0
 	*r_via = chip;
 	return 0;
+
+disable_device:
+	pci_disable_device(pci);
+	goto exit;
+free_chip:
+	snd_via82xx_free(chip);
+exit:
+	return err;

Doubly goto doesn't look like an improvement.

Takashi

#2 lore raw
Quoted 14 lines

sound/pci/via82xx_modem.c static int snd_via82xx_create(struct snd_card *card, · line 1160

Quoted 12 lines
sound/pci/via82xx_modem.c +8 -0
@@ -1153,6 +1150,14 @@ static int snd_via82xx_create(struct snd_card *card,
 
 	*r_via = chip;
 	return 0;
+
+disable_device:
+	pci_disable_device(pci);
+	goto exit;
+free_chip:
+	snd_via82xx_free(chip);
+exit:
+	return err;

Doubly goto doesn't look like an improvement.

Would you like to integrate another software update with the statement “return err;” instead of “goto exit;” at this place?

Regards,
Markus

#3 lore raw
In reply to SF Markus Elfring
Quoted 8 lines · SF Markus Elfring
sound/pci/via82xx_modem.c +1 -2
@@ -1121,30 +1121,27 @@ static int snd_via82xx_create(struct snd_card *card,
 
 	if ((err = pci_request_regions(pci, card->driver)) < 0) {
 		kfree(chip);
-		pci_disable_device(pci);
-		return err;
+		goto disable_device;
 	}

[ snip ]

sound/pci/via82xx_modem.c static int snd_via82xx_create(struct snd_card *card, · line 1156

Quoted 8 lines
sound/pci/via82xx_modem.c +4 -0
@@ -1153,6 +1150,14 @@ static int snd_via82xx_create(struct snd_card *card,
 
 	*r_via = chip;
 	return 0;
+
+disable_device:
+	pci_disable_device(pci);
+	goto exit;

Why is this "goto exit" here? It's leaking now.

sound/pci/via82xx_modem.c static int snd_via82xx_create(struct snd_card *card, · line 1161

Quoted 5 lines
sound/pci/via82xx_modem.c +4 -0
+free_chip:
+	snd_via82xx_free(chip);
+exit:
+	return err;
 }

regards,
dan carpenter

#4 lore raw
Quoted 13 lines
Quoted 8 lines
sound/pci/via82xx_modem.c +1 -2
@@ -1121,30 +1121,27 @@ static int snd_via82xx_create(struct snd_card *card,
 
 	if ((err = pci_request_regions(pci, card->driver)) < 0) {
 		kfree(chip);
-		pci_disable_device(pci);
-		return err;
+		goto disable_device;
 	}

[ snip ]

Quoted 2 lines
(quoted diff) +0 -0
@@ -1153,6 +1150,14 @@ static int snd_via82xx_create(struct snd_card *card,
 
Quoted 8 lines

sound/pci/via82xx_modem.c static int snd_via82xx_create(struct snd_card *card, · line 1156

Quoted 6 lines
sound/pci/via82xx_modem.c +4 -0
 	*r_via = chip;
 	return 0;
+
+disable_device:
+	pci_disable_device(pci);
+	goto exit;

Why is this "goto exit" here?

Should the same return statement be reached after the proposed refactoring?

Would you like to move such a function call to this source code place?

Quoted 1 line

It's leaking now.

How do you come to this conclusion?

Quoted 5 lines

sound/pci/via82xx_modem.c static int snd_via82xx_create(struct snd_card *card, · line 1161

Quoted 5 lines
sound/pci/via82xx_modem.c +4 -0
+free_chip:
+	snd_via82xx_free(chip);
+exit:
+	return err;
 }

Is this update suggestion worth for another look?

Regards,
Markus

#5 lore raw
Quoted 7 lines · SF Markus Elfring
Quoted 7 lines

sound/pci/via82xx_modem.c static int snd_via82xx_create(struct snd_card *card, · line 1124

Quoted 7 lines
sound/pci/via82xx_modem.c +1 -2
@@ -1121,30 +1121,27 @@ static int snd_via82xx_create(struct snd_card *card,
 
 	if ((err = pci_request_regions(pci, card->driver)) < 0) {
 		kfree(chip);
-		pci_disable_device(pci);
-		return err;
+		goto disable_device;

Ah, you're right. It's not leaking. I thought you deleted the kfree(chip);...

regards,
dan carpenter

#6 lore raw
Quoted 4 lines · Dan Carpenter
Quoted 2 lines · SF Markus Elfring
Quoted 2 lines
Quoted 2 lines
(quoted diff) +0 -0
@@ -1121,30 +1121,27 @@ static int snd_via82xx_create(struct snd_card *card,
 
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

sound/pci/via82xx_modem.c static int snd_via82xx_create(struct snd_card *card, · line 1124

Quoted 5 lines
sound/pci/via82xx_modem.c +1 -2
 	if ((err = pci_request_regions(pci, card->driver)) < 0) {
 		kfree(chip);
-		pci_disable_device(pci);
-		return err;
+		goto disable_device;

Ah, you're right. It's not leaking. I thought you deleted the kfree(chip);...

Well, this also suggests something wrong by the patch...

If you can't achieve it cleanly, better not to touch it in a half-baked way.

Takashi