Skip to contentSkip to conversation

ALSA: 6fire: More hardening and cleanups

PATCH New Takashi Iwai 1 participant 5 messages Apr 14 Sound

lore
Share & tools
lore
#0 lore raw

cover ALSA: 6fire: More hardening and cleanups

While reviewing the recent fix patch for 6fire driver, I noticed that the driver code needs further hardening and cleanups. Here is the resultant patch set doing that.

Takashi

===

Takashi Iwai (4):
ALSA: 6fire: Cover the whole probe and disconnect calls with
register_mutex
ALSA: 6fire: Fix leftover global pointers after probe failures
ALSA: 6fire: Reduce multi-level conditionals in
usb6fire_chip_disconnect()
ALSA: 6fire: Drop unnecessary NULL checks

 sound/usb/6fire/chip.c | 113 ++++++++++++++++++++---------------------
 1 file changed, 54 insertions(+), 59 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/4 ALSA: 6fire: Cover the whole probe and disconnect calls with register_mutex

In 6fire driver, we protect the concurrent calls against probe and disconnect with the register_mutex, but it's applied only partially. Since we handle two global pointers in devices[] and chips[] pairs, the assignment of the latter can be inconsistent upon concurrent interface probes, and the refcount handling isn't properly protected at disconnect, either.

This patch extends the mutex application range to the whole probe and disconnect functions. It makes the code safer against potential concurrent probles and disconnects, while it makes the code easier to read, too.

---
 sound/usb/6fire/chip.c | 36 +++++++++++++++++-------------------
 1 file changed, 17 insertions(+), 19 deletions(-)
sound/usb/6fire/chip.c +17 -19
index 874f6cd503ca..18a25449bcd3 100644
@@ -83,22 +83,21 @@ static int usb6fire_chip_probe(struct usb_interface *intf,
 	struct snd_card *card = NULL;
 
 	/* look if we already serve this card and return if so */
-	scoped_guard(mutex, &register_mutex) {
-		for (i = 0; i < SNDRV_CARDS; i++) {
-			if (devices[i] == device) {
-				if (chips[i])
-					chips[i]->intf_count++;
-				usb_set_intfdata(intf, chips[i]);
-				return 0;
-			} else if (!devices[i] && regidx < 0)
-				regidx = i;
-		}
-		if (regidx < 0) {
-			dev_err(&intf->dev, "too many cards registered.\n");
-			return -ENODEV;
-		}
-		devices[regidx] = device;
+	guard(mutex)(&register_mutex);
+	for (i = 0; i < SNDRV_CARDS; i++) {
+		if (devices[i] == device) {
+			if (chips[i])
+				chips[i]->intf_count++;
+			usb_set_intfdata(intf, chips[i]);
+			return 0;
+		} else if (!devices[i] && regidx < 0)
+			regidx = i;
 	}
+	if (regidx < 0) {
+		dev_err(&intf->dev, "too many cards registered.\n");
+		return -ENODEV;
+	}
+	devices[regidx] = device;
 
 	/* check, if firmware is present on device, upload it if not */
 	ret = usb6fire_fw_init(intf);
@@ -165,14 +164,13 @@ static void usb6fire_chip_disconnect(struct usb_interface *intf)
 	struct sfire_chip *chip;
 	struct snd_card *card;
 
+	guard(mutex)(&register_mutex);
 	chip = usb_get_intfdata(intf);
 	if (chip) { /* if !chip, fw upload has been performed */
 		chip->intf_count--;
 		if (!chip->intf_count) {
-			scoped_guard(mutex, &register_mutex) {
-				devices[chip->regidx] = NULL;
-				chips[chip->regidx] = NULL;
-			}
+			devices[chip->regidx] = NULL;
+			chips[chip->regidx] = NULL;
 
 			/*
 			 * Save card pointer before teardown.
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/4 ALSA: 6fire: Fix leftover global pointers after probe failures

In reply to Takashi Iwai

snd-usb-6fire driver holds devices[] and chips[] pointer arrays to keep the usb_device and sfire_chip objects assigned to multiple interfaces. Those are, however, not properly cleared at the error path of usb6fire_chip_probe(), which may confuse the later probes. Also, the use of two pointer arrays makes things complicated; chips[] may be NULL while devices[] may be left over.

For addressing this inconsistency, unify the pointer arrays, and use only chips[] for managing the multiple devices, while the device is checked with chip->dev pointer, instead. Also, the assignment of chips[] is moved at a later point where the probe successfully returns, so that we don't leave the pointer there after the error.

---
 sound/usb/6fire/chip.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
sound/usb/6fire/chip.c +6 -8
index 18a25449bcd3..dd787de986b1 100644
@@ -31,7 +31,6 @@ static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for card */
 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable card */
 static struct sfire_chip *chips[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
-static struct usb_device *devices[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
 
 module_param_array(index, int, NULL, 0444);
 MODULE_PARM_DESC(index, "Index value for the 6fire sound device");
@@ -85,19 +84,17 @@ static int usb6fire_chip_probe(struct usb_interface *intf,
 	/* look if we already serve this card and return if so */
 	guard(mutex)(&register_mutex);
 	for (i = 0; i < SNDRV_CARDS; i++) {
-		if (devices[i] == device) {
-			if (chips[i])
-				chips[i]->intf_count++;
+		if (chips[i] && chips[i]->dev == device) {
+			chips[i]->intf_count++;
 			usb_set_intfdata(intf, chips[i]);
 			return 0;
-		} else if (!devices[i] && regidx < 0)
+		} else if (!chips[i] && regidx < 0)
 			regidx = i;
 	}
 	if (regidx < 0) {
 		dev_err(&intf->dev, "too many cards registered.\n");
 		return -ENODEV;
 	}
-	devices[regidx] = device;
 
 	/* check, if firmware is present on device, upload it if not */
 	ret = usb6fire_fw_init(intf);
@@ -123,7 +120,6 @@ static int usb6fire_chip_probe(struct usb_interface *intf,
 			device->bus->busnum, device->devnum);
 
 	chip = card->private_data;
-	chips[regidx] = chip;
 	chip->dev = device;
 	chip->regidx = regidx;
 	chip->intf_count = 1;
@@ -151,7 +147,10 @@ static int usb6fire_chip_probe(struct usb_interface *intf,
 		dev_err(&intf->dev, "cannot register card.");
 		goto destroy_chip;
 	}
+
 	usb_set_intfdata(intf, chip);
+	chips[regidx] = chip;
+
 	return 0;
 
 destroy_chip:
@@ -169,7 +168,6 @@ static void usb6fire_chip_disconnect(struct usb_interface *intf)
 	if (chip) { /* if !chip, fw upload has been performed */
 		chip->intf_count--;
 		if (!chip->intf_count) {
-			devices[chip->regidx] = NULL;
 			chips[chip->regidx] = NULL;
 
 			/*
Signature · 1 line
2.53.0
#3 lore raw

PATCH 3/4 ALSA: 6fire: Reduce multi-level conditionals in usb6fire_chip_disconnect()

In reply to Takashi Iwai

The current code has deep indentation levels because of multiple if's. Make it returning and reduce the multi-level conditionals for increasing the code readability.

No functional change, just but a code refactoring.

---
 sound/usb/6fire/chip.c | 41 ++++++++++++++++++++++-------------------
 1 file changed, 22 insertions(+), 19 deletions(-)
sound/usb/6fire/chip.c +22 -19
index dd787de986b1..556882bd9022 100644
@@ -165,26 +165,29 @@ static void usb6fire_chip_disconnect(struct usb_interface *intf)
 
 	guard(mutex)(&register_mutex);
 	chip = usb_get_intfdata(intf);
-	if (chip) { /* if !chip, fw upload has been performed */
-		chip->intf_count--;
-		if (!chip->intf_count) {
-			chips[chip->regidx] = NULL;
+	/* if !chip, fw upload has been performed */
+	if (!chip)
+		return;
 
-			/*
-			 * Save card pointer before teardown.
-			 * snd_card_free_when_closed() may free card (and
-			 * the embedded chip) immediately, so it must be
-			 * called last and chip must not be accessed after.
-			 */
-			card = chip->card;
-			chip->shutdown = true;
-			if (card)
-				snd_card_disconnect(card);
-			usb6fire_chip_abort(chip);
-			if (card)
-				snd_card_free_when_closed(card);
-		}
-	}
+	chip->intf_count--;
+	if (chip->intf_count)
+		return;
+
+	chips[chip->regidx] = NULL;
+
+	/*
+	 * Save card pointer before teardown.
+	 * snd_card_free_when_closed() may free card (and
+	 * the embedded chip) immediately, so it must be
+	 * called last and chip must not be accessed after.
+	 */
+	card = chip->card;
+	chip->shutdown = true;
+	if (card)
+		snd_card_disconnect(card);
+	usb6fire_chip_abort(chip);
+	if (card)
+		snd_card_free_when_closed(card);
 }
 
 static const struct usb_device_id device_table[] = {
Signature · 1 line
2.53.0
#4 lore raw

PATCH 4/4 ALSA: 6fire: Drop unnecessary NULL checks

In reply to Takashi Iwai

The NULL checks of chip pointer in usb6fire_chip_abrt() and usb6fire_card_free() are utterly useless, as it's guaranteed to be non-NULL. Drop them for increasing the readability.

---
 sound/usb/6fire/chip.c | 36 ++++++++++++++++--------------------
 1 file changed, 16 insertions(+), 20 deletions(-)
sound/usb/6fire/chip.c +16 -20
index 556882bd9022..2c5648966412 100644
@@ -43,32 +43,28 @@ static DEFINE_MUTEX(register_mutex);
 
 static void usb6fire_chip_abort(struct sfire_chip *chip)
 {
-	if (chip) {
-		if (chip->pcm)
-			usb6fire_pcm_abort(chip);
-		if (chip->midi)
-			usb6fire_midi_abort(chip);
-		if (chip->comm)
-			usb6fire_comm_abort(chip);
-		if (chip->control)
-			usb6fire_control_abort(chip);
-	}
+	if (chip->pcm)
+		usb6fire_pcm_abort(chip);
+	if (chip->midi)
+		usb6fire_midi_abort(chip);
+	if (chip->comm)
+		usb6fire_comm_abort(chip);
+	if (chip->control)
+		usb6fire_control_abort(chip);
 }
 
 static void usb6fire_card_free(struct snd_card *card)
 {
 	struct sfire_chip *chip = card->private_data;
 
-	if (chip) {
-		if (chip->pcm)
-			usb6fire_pcm_destroy(chip);
-		if (chip->midi)
-			usb6fire_midi_destroy(chip);
-		if (chip->comm)
-			usb6fire_comm_destroy(chip);
-		if (chip->control)
-			usb6fire_control_destroy(chip);
-	}
+	if (chip->pcm)
+		usb6fire_pcm_destroy(chip);
+	if (chip->midi)
+		usb6fire_midi_destroy(chip);
+	if (chip->comm)
+		usb6fire_comm_destroy(chip);
+	if (chip->control)
+		usb6fire_control_destroy(chip);
 }
 
 static int usb6fire_chip_probe(struct usb_interface *intf,
Signature · 1 line
2.53.0