]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
ASoC: SOF: topology: use kzalloc_flex
authorRosen Penev <rosenp@gmail.com>
Thu, 26 Mar 2026 02:30:53 +0000 (19:30 -0700)
committerMark Brown <broonie@kernel.org>
Mon, 30 Mar 2026 19:11:31 +0000 (20:11 +0100)
Simplify allocation by using a flexible array member.

Add __counted_by for extra runtime analysis.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
Link: https://patch.msgid.link/20260326023053.53493-1-rosenp@gmail.com
Signed-off-by: Mark Brown <broonie@kernel.org>
sound/soc/sof/sof-audio.h
sound/soc/sof/topology.c

index 80b11625915ddf7a5fd1cf0d281a9363bdc2b40e..138e5fcc2dd094807f2f9d49bb80d62f7b83c06a 100644 (file)
@@ -411,11 +411,11 @@ struct snd_sof_dai_link {
        struct snd_sof_tuple *tuples;
        int num_tuples;
        struct snd_soc_dai_link *link;
-       struct snd_soc_tplg_hw_config *hw_configs;
        int num_hw_configs;
        int default_hw_cfg_id;
        int type;
        struct list_head list;
+       struct snd_soc_tplg_hw_config hw_configs[] __counted_by(num_hw_configs);
 };
 
 /* ASoC SOF DAPM widget */
index 907f9b1e9d1b2a4b89e6e371ae0c604c3da7d0cf..63d582c6589154ad6c420311c26d9965d741e50c 100644 (file)
@@ -1908,18 +1908,12 @@ static int sof_link_load(struct snd_soc_component *scomp, int index, struct snd_
                return -EINVAL;
        }
 
-       slink = kzalloc_obj(*slink);
+       slink = kzalloc_flex(*slink, hw_configs, le32_to_cpu(cfg->num_hw_configs));
        if (!slink)
                return -ENOMEM;
 
        slink->num_hw_configs = le32_to_cpu(cfg->num_hw_configs);
-       slink->hw_configs = kmemdup_array(cfg->hw_config,
-                                         slink->num_hw_configs, sizeof(*slink->hw_configs),
-                                         GFP_KERNEL);
-       if (!slink->hw_configs) {
-               kfree(slink);
-               return -ENOMEM;
-       }
+       memcpy(slink->hw_configs, cfg->hw_config, le32_to_cpu(cfg->num_hw_configs) * sizeof(*slink->hw_configs));
 
        slink->default_hw_cfg_id = le32_to_cpu(cfg->default_hw_config_id);
        slink->link = link;
@@ -1932,7 +1926,6 @@ static int sof_link_load(struct snd_soc_component *scomp, int index, struct snd_
                               private->array, le32_to_cpu(private->size));
        if (ret < 0) {
                dev_err(scomp->dev, "Failed tp parse common DAI link tokens\n");
-               kfree(slink->hw_configs);
                kfree(slink);
                return ret;
        }
@@ -2003,7 +1996,6 @@ static int sof_link_load(struct snd_soc_component *scomp, int index, struct snd_
        /* allocate memory for tuples array */
        slink->tuples = kzalloc_objs(*slink->tuples, num_tuples);
        if (!slink->tuples) {
-               kfree(slink->hw_configs);
                kfree(slink);
                return -ENOMEM;
        }
@@ -2061,7 +2053,6 @@ out:
 
 err:
        kfree(slink->tuples);
-       kfree(slink->hw_configs);
        kfree(slink);
 
        return ret;
@@ -2078,7 +2069,6 @@ static int sof_link_unload(struct snd_soc_component *scomp, struct snd_soc_dobj
 
        kfree(slink->tuples);
        list_del(&slink->list);
-       kfree(slink->hw_configs);
        kfree(slink);
        dobj->private = NULL;