Skip to contentSkip to conversation

ALSA: usb-audio: use kzalloc_flex

PATCH In review Rosen Penev 2 participants 5 messages Mar 11 – Mar 14 Sound

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: usb-audio: use kzalloc_flex

Saves one allocation by using a flexible array member.

---
 sound/usb/mixer.c | 22 +++++++---------------
 sound/usb/mixer.h |  5 +++--
 2 files changed, 10 insertions(+), 17 deletions(-)
sound/usb/mixer.c +7 -15
index 7007e0c9489b..337815b9e72c 100644
@@ -1393,7 +1393,7 @@ static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
 		if (!cval->initialized) {
 			get_min_max_with_quirks(cval, 0, kcontrol);
 			if (cval->initialized && cval->dBmin >= cval->dBmax) {
-				kcontrol->vd[0].access &= 
+				kcontrol->vd[0].access &=
 					~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
 					  SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
 				snd_ctl_notify(cval->head.mixer->chip->card,
@@ -3006,15 +3006,12 @@ static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
 	snd_usb_mixer_disconnect(mixer);
 
 	/* Unregister controls first, snd_ctl_remove() frees the element */
-	if (mixer->id_elems) {
-		for (id = 0; id < MAX_ID_ELEMS; id++) {
-			for (list = mixer->id_elems[id]; list; list = next) {
-				next = list->next_id_elem;
-				if (list->kctl)
-					snd_ctl_remove(mixer->chip->card, list->kctl);
-			}
+	for (id = 0; id < MAX_ID_ELEMS; id++) {
+		for (list = mixer->id_elems[id]; list; list = next) {
+			next = list->next_id_elem;
+			if (list->kctl)
+				snd_ctl_remove(mixer->chip->card, list->kctl);
 		}
-		kfree(mixer->id_elems);
 	}
 	if (mixer->urb) {
 		kfree(mixer->urb->transfer_buffer);
@@ -3652,16 +3649,11 @@ int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif)
 
 	strscpy(chip->card->mixername, "USB Mixer");
 
-	mixer = kzalloc_obj(*mixer);
+	mixer = kzalloc_flex(*mixer, id_elems, MAX_ID_ELEMS);
 	if (!mixer)
 		return -ENOMEM;
 	mixer->chip = chip;
 	mixer->ignore_ctl_error = !!(chip->quirk_flags & QUIRK_FLAG_IGNORE_CTL_ERROR);
-	mixer->id_elems = kzalloc_objs(*mixer->id_elems, MAX_ID_ELEMS);
-	if (!mixer->id_elems) {
-		kfree(mixer);
-		return -ENOMEM;
-	}
 
 	mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
 	switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
sound/usb/mixer.h +3 -2
index 167fbfcf01ac..30dd30b925fc 100644
@@ -19,8 +19,6 @@ struct usb_mixer_interface {
 	struct list_head list;
 	unsigned int ignore_ctl_error;
 	struct urb *urb;
-	/* array[MAX_ID_ELEMS], indexed by unit id */
-	struct usb_mixer_elem_list **id_elems;
 
 	/* the usb audio specification version this interface complies to */
 	int protocol;
@@ -42,6 +40,9 @@ struct usb_mixer_interface {
 	void *private_data;
 	void (*private_free)(struct usb_mixer_interface *mixer);
 	void (*private_suspend)(struct usb_mixer_interface *mixer);
+
+	/* array[MAX_ID_ELEMS], indexed by unit id */
+	struct usb_mixer_elem_list *id_elems[];
 };
 
 #define MAX_CHANNELS	16	/* max logical channels */
Signature · 1 line
2.53.0
#1 lore raw
Quoted 2 lines · Rosen Penev

Saves one allocation by using a flexible array member.

As it's a fixed size array, it doesn't have to be a flex array at all, so it can be even simpler.

thanks,

Takashi

Quoted 85 lines
---
 sound/usb/mixer.c | 22 +++++++---------------
 sound/usb/mixer.h |  5 +++--
 2 files changed, 10 insertions(+), 17 deletions(-)
sound/usb/mixer.c +7 -15
index 7007e0c9489b..337815b9e72c 100644
@@ -1393,7 +1393,7 @@ static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
 		if (!cval->initialized) {
 			get_min_max_with_quirks(cval, 0, kcontrol);
 			if (cval->initialized && cval->dBmin >= cval->dBmax) {
-				kcontrol->vd[0].access &= 
+				kcontrol->vd[0].access &=
 					~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
 					  SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
 				snd_ctl_notify(cval->head.mixer->chip->card,
@@ -3006,15 +3006,12 @@ static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
 	snd_usb_mixer_disconnect(mixer);
 
 	/* Unregister controls first, snd_ctl_remove() frees the element */
-	if (mixer->id_elems) {
-		for (id = 0; id < MAX_ID_ELEMS; id++) {
-			for (list = mixer->id_elems[id]; list; list = next) {
-				next = list->next_id_elem;
-				if (list->kctl)
-					snd_ctl_remove(mixer->chip->card, list->kctl);
-			}
+	for (id = 0; id < MAX_ID_ELEMS; id++) {
+		for (list = mixer->id_elems[id]; list; list = next) {
+			next = list->next_id_elem;
+			if (list->kctl)
+				snd_ctl_remove(mixer->chip->card, list->kctl);
 		}
-		kfree(mixer->id_elems);
 	}
 	if (mixer->urb) {
 		kfree(mixer->urb->transfer_buffer);
@@ -3652,16 +3649,11 @@ int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif)
 
 	strscpy(chip->card->mixername, "USB Mixer");
 
-	mixer = kzalloc_obj(*mixer);
+	mixer = kzalloc_flex(*mixer, id_elems, MAX_ID_ELEMS);
 	if (!mixer)
 		return -ENOMEM;
 	mixer->chip = chip;
 	mixer->ignore_ctl_error = !!(chip->quirk_flags & QUIRK_FLAG_IGNORE_CTL_ERROR);
-	mixer->id_elems = kzalloc_objs(*mixer->id_elems, MAX_ID_ELEMS);
-	if (!mixer->id_elems) {
-		kfree(mixer);
-		return -ENOMEM;
-	}
 
 	mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
 	switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
sound/usb/mixer.h +3 -2
index 167fbfcf01ac..30dd30b925fc 100644
@@ -19,8 +19,6 @@ struct usb_mixer_interface {
 	struct list_head list;
 	unsigned int ignore_ctl_error;
 	struct urb *urb;
-	/* array[MAX_ID_ELEMS], indexed by unit id */
-	struct usb_mixer_elem_list **id_elems;
 
 	/* the usb audio specification version this interface complies to */
 	int protocol;
@@ -42,6 +40,9 @@ struct usb_mixer_interface {
 	void *private_data;
 	void (*private_free)(struct usb_mixer_interface *mixer);
 	void (*private_suspend)(struct usb_mixer_interface *mixer);
+
+	/* array[MAX_ID_ELEMS], indexed by unit id */
+	struct usb_mixer_elem_list *id_elems[];
 };
 
 #define MAX_CHANNELS	16	/* max logical channels */
Signature · 1 line
2.53.0
#2 lore raw
Quoted 8 lines · Takashi Iwai
Quoted 2 lines · Rosen Penev

Saves one allocation by using a flexible array member.

As it's a fixed size array, it doesn't have to be a flex array at all, so it can be even simpler.

That's true. The comment even admits as such. Wonder why it was ever a pointer allocated with calloc...

Quoted 91 lines

thanks,

Takashi

Quoted 85 lines
---
 sound/usb/mixer.c | 22 +++++++---------------
 sound/usb/mixer.h |  5 +++--
 2 files changed, 10 insertions(+), 17 deletions(-)
sound/usb/mixer.c +7 -15
index 7007e0c9489b..337815b9e72c 100644
@@ -1393,7 +1393,7 @@ static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
              if (!cval->initialized) {
                      get_min_max_with_quirks(cval, 0, kcontrol);
                      if (cval->initialized && cval->dBmin >= cval->dBmax) {
-                             kcontrol->vd[0].access &=
+                             kcontrol->vd[0].access &=
                                      ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
                                        SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
                              snd_ctl_notify(cval->head.mixer->chip->card,
@@ -3006,15 +3006,12 @@ static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
      snd_usb_mixer_disconnect(mixer);
 
      /* Unregister controls first, snd_ctl_remove() frees the element */
-     if (mixer->id_elems) {
-             for (id = 0; id < MAX_ID_ELEMS; id++) {
-                     for (list = mixer->id_elems[id]; list; list = next) {
-                             next = list->next_id_elem;
-                             if (list->kctl)
-                                     snd_ctl_remove(mixer->chip->card, list->kctl);
-                     }
+     for (id = 0; id < MAX_ID_ELEMS; id++) {
+             for (list = mixer->id_elems[id]; list; list = next) {
+                     next = list->next_id_elem;
+                     if (list->kctl)
+                             snd_ctl_remove(mixer->chip->card, list->kctl);
              }
-             kfree(mixer->id_elems);
      }
      if (mixer->urb) {
              kfree(mixer->urb->transfer_buffer);
@@ -3652,16 +3649,11 @@ int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif)
 
      strscpy(chip->card->mixername, "USB Mixer");
 
-     mixer = kzalloc_obj(*mixer);
+     mixer = kzalloc_flex(*mixer, id_elems, MAX_ID_ELEMS);
      if (!mixer)
              return -ENOMEM;
      mixer->chip = chip;
      mixer->ignore_ctl_error = !!(chip->quirk_flags & QUIRK_FLAG_IGNORE_CTL_ERROR);
-     mixer->id_elems = kzalloc_objs(*mixer->id_elems, MAX_ID_ELEMS);
-     if (!mixer->id_elems) {
-             kfree(mixer);
-             return -ENOMEM;
-     }
 
      mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
      switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
sound/usb/mixer.h +3 -3
index 167fbfcf01ac..30dd30b925fc 100644
@@ -19,8 +19,6 @@ struct usb_mixer_interface {
      struct list_head list;
      unsigned int ignore_ctl_error;
      struct urb *urb;
-     /* array[MAX_ID_ELEMS], indexed by unit id */
-     struct usb_mixer_elem_list **id_elems;
 
      /* the usb audio specification version this interface complies to */
      int protocol;
@@ -42,6 +40,9 @@ struct usb_mixer_interface {
      void *private_data;
      void (*private_free)(struct usb_mixer_interface *mixer);
      void (*private_suspend)(struct usb_mixer_interface *mixer);
+
+     /* array[MAX_ID_ELEMS], indexed by unit id */
+     struct usb_mixer_elem_list *id_elems[];
 };
 
 #define MAX_CHANNELS 16      /* max logical channels */
--

2.53.0

#3 lore raw
Quoted 6 lines · Rosen Penev
Quoted 4 lines · Takashi Iwai
Quoted 1 line · Rosen Penev
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Saves one allocation by using a flexible array member.

As it's a fixed size array, it doesn't have to be a flex array at all, so it can be even simpler.

That's true. The comment even admits as such. Wonder why it was ever a pointer allocated with calloc...

Oh no I see the issue:

sound/usb/mixer.c:#define MAX_ID_ELEMS 256

I don't think this is supposed to be in the header.

Quoted 91 lines
Quoted 91 lines

thanks,

Takashi

Quoted 85 lines
---
 sound/usb/mixer.c | 22 +++++++---------------
 sound/usb/mixer.h |  5 +++--
 2 files changed, 10 insertions(+), 17 deletions(-)
sound/usb/mixer.c +7 -15
index 7007e0c9489b..337815b9e72c 100644
@@ -1393,7 +1393,7 @@ static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
              if (!cval->initialized) {
                      get_min_max_with_quirks(cval, 0, kcontrol);
                      if (cval->initialized && cval->dBmin >= cval->dBmax) {
-                             kcontrol->vd[0].access &=
+                             kcontrol->vd[0].access &=
                                      ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
                                        SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
                              snd_ctl_notify(cval->head.mixer->chip->card,
@@ -3006,15 +3006,12 @@ static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
      snd_usb_mixer_disconnect(mixer);
 
      /* Unregister controls first, snd_ctl_remove() frees the element */
-     if (mixer->id_elems) {
-             for (id = 0; id < MAX_ID_ELEMS; id++) {
-                     for (list = mixer->id_elems[id]; list; list = next) {
-                             next = list->next_id_elem;
-                             if (list->kctl)
-                                     snd_ctl_remove(mixer->chip->card, list->kctl);
-                     }
+     for (id = 0; id < MAX_ID_ELEMS; id++) {
+             for (list = mixer->id_elems[id]; list; list = next) {
+                     next = list->next_id_elem;
+                     if (list->kctl)
+                             snd_ctl_remove(mixer->chip->card, list->kctl);
              }
-             kfree(mixer->id_elems);
      }
      if (mixer->urb) {
              kfree(mixer->urb->transfer_buffer);
@@ -3652,16 +3649,11 @@ int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif)
 
      strscpy(chip->card->mixername, "USB Mixer");
 
-     mixer = kzalloc_obj(*mixer);
+     mixer = kzalloc_flex(*mixer, id_elems, MAX_ID_ELEMS);
      if (!mixer)
              return -ENOMEM;
      mixer->chip = chip;
      mixer->ignore_ctl_error = !!(chip->quirk_flags & QUIRK_FLAG_IGNORE_CTL_ERROR);
-     mixer->id_elems = kzalloc_objs(*mixer->id_elems, MAX_ID_ELEMS);
-     if (!mixer->id_elems) {
-             kfree(mixer);
-             return -ENOMEM;
-     }
 
      mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
      switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
sound/usb/mixer.h +3 -3
index 167fbfcf01ac..30dd30b925fc 100644
@@ -19,8 +19,6 @@ struct usb_mixer_interface {
      struct list_head list;
      unsigned int ignore_ctl_error;
      struct urb *urb;
-     /* array[MAX_ID_ELEMS], indexed by unit id */
-     struct usb_mixer_elem_list **id_elems;
 
      /* the usb audio specification version this interface complies to */
      int protocol;
@@ -42,6 +40,9 @@ struct usb_mixer_interface {
      void *private_data;
      void (*private_free)(struct usb_mixer_interface *mixer);
      void (*private_suspend)(struct usb_mixer_interface *mixer);
+
+     /* array[MAX_ID_ELEMS], indexed by unit id */
+     struct usb_mixer_elem_list *id_elems[];
 };
 
 #define MAX_CHANNELS 16      /* max logical channels */
--

2.53.0

#4 lore raw
Quoted 12 lines · Rosen Penev
Quoted 10 lines · Rosen Penev
Quoted 8 lines · Takashi Iwai
Quoted 2 lines · Rosen Penev

Saves one allocation by using a flexible array member.

As it's a fixed size array, it doesn't have to be a flex array at all, so it can be even simpler.

Quoted 7 lines
Quoted 2 lines

That's true. The comment even admits as such. Wonder why it was ever a pointer allocated with calloc...

Oh no I see the issue:

sound/usb/mixer.c:#define MAX_ID_ELEMS 256

I don't think this is supposed to be in the header.

Why?

Takashi