Re: [PATCH] ALSA: usb-audio: use kzalloc_flex

From: Rosen Penev

Date: Fri Mar 13 2026 - 18:02:30 EST


On Wed, Mar 11, 2026 at 11:45 PM Rosen Penev <rosenp@xxxxxxxxx> wrote:
>
> On Wed, Mar 11, 2026 at 11:34 PM Takashi Iwai <tiwai@xxxxxxx> wrote:
> >
> > On Thu, 12 Mar 2026 00:27:48 +0100,
> > Rosen Penev wrote:
> > >
> > > 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.
> >
> >
> > thanks,
> >
> > Takashi
> >
> > >
> > > Signed-off-by: Rosen Penev <rosenp@xxxxxxxxx>
> > > ---
> > > sound/usb/mixer.c | 22 +++++++---------------
> > > sound/usb/mixer.h | 5 +++--
> > > 2 files changed, 10 insertions(+), 17 deletions(-)
> > >
> > > diff --git a/sound/usb/mixer.c b/sound/usb/mixer.c
> > > index 7007e0c9489b..337815b9e72c 100644
> > > --- a/sound/usb/mixer.c
> > > +++ b/sound/usb/mixer.c
> > > @@ -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) {
> > > diff --git a/sound/usb/mixer.h b/sound/usb/mixer.h
> > > index 167fbfcf01ac..30dd30b925fc 100644
> > > --- a/sound/usb/mixer.h
> > > +++ b/sound/usb/mixer.h
> > > @@ -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
> > >