[PATCH v2 01/24] ASoC: soc-core: always allocate default ch_maps

From: Srinivas Kandagatla

Date: Sun Nov 22 2026 - 11:15:03 EST


The default ch_maps come from three file-scope static tables of twelve
entries each, and links wider than twelve DAIs are rejected up front.
The cap is arbitrary -- the tables encode "cpu = i" or "codec = i",
information soc-core can synthesize at bind time for any link width.

Replace the static tables with a devm allocation on card->dev sized
to the link, and drop the MAX_DEFAULT_CH_MAP_SIZE check.

The devm buffer's lifetime matches the card bind, but the pointer is
stored in dai_link->ch_maps -- and machine drivers commonly declare
dai_link as file-scope static storage, so a bare write would survive
into the next bind as a dangling pointer, and the "dai_link already
has ch_maps" fast path at the top of this function would then reuse
freed memory.

Register a devm release action that clears dai_link->ch_maps. devres
unwinds LIFO, so the action fires before the buffer is freed; on
rebind the static storage is already NULL and the normal allocation
path runs cleanly.

Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@xxxxxxxxxxxxxxxx>
---
sound/soc/soc-core.c | 101 +++++++++++++++----------------------------
1 file changed, 36 insertions(+), 65 deletions(-)

diff --git a/sound/soc/soc-core.c b/sound/soc/soc-core.c
index 7dfd79b4ca3e..ccd1cb5ee673 100644
--- a/sound/soc/soc-core.c
+++ b/sound/soc/soc-core.c
@@ -766,63 +766,20 @@ static int soc_dai_link_sanity_check(struct snd_soc_card *card,
return -EINVAL;
}

-#define MAX_DEFAULT_CH_MAP_SIZE 12
-static struct snd_soc_dai_link_ch_map default_ch_map_sync[MAX_DEFAULT_CH_MAP_SIZE] = {
- { .cpu = 0, .codec = 0 },
- { .cpu = 1, .codec = 1 },
- { .cpu = 2, .codec = 2 },
- { .cpu = 3, .codec = 3 },
- { .cpu = 4, .codec = 4 },
- { .cpu = 5, .codec = 5 },
- { .cpu = 6, .codec = 6 },
- { .cpu = 7, .codec = 7 },
- { .cpu = 8, .codec = 8 },
- { .cpu = 9, .codec = 9 },
- { .cpu = 10, .codec = 10 },
- { .cpu = 11, .codec = 11 },
-};
-static struct snd_soc_dai_link_ch_map default_ch_map_1cpu[MAX_DEFAULT_CH_MAP_SIZE] = {
- { .cpu = 0, .codec = 0 },
- { .cpu = 0, .codec = 1 },
- { .cpu = 0, .codec = 2 },
- { .cpu = 0, .codec = 3 },
- { .cpu = 0, .codec = 4 },
- { .cpu = 0, .codec = 5 },
- { .cpu = 0, .codec = 6 },
- { .cpu = 0, .codec = 7 },
- { .cpu = 0, .codec = 8 },
- { .cpu = 0, .codec = 9 },
- { .cpu = 0, .codec = 10 },
- { .cpu = 0, .codec = 11 },
-};
-static struct snd_soc_dai_link_ch_map default_ch_map_1codec[MAX_DEFAULT_CH_MAP_SIZE] = {
- { .cpu = 0, .codec = 0 },
- { .cpu = 1, .codec = 0 },
- { .cpu = 2, .codec = 0 },
- { .cpu = 3, .codec = 0 },
- { .cpu = 4, .codec = 0 },
- { .cpu = 5, .codec = 0 },
- { .cpu = 6, .codec = 0 },
- { .cpu = 7, .codec = 0 },
- { .cpu = 8, .codec = 0 },
- { .cpu = 9, .codec = 0 },
- { .cpu = 10, .codec = 0 },
- { .cpu = 11, .codec = 0 },
-};
+static void snd_soc_clear_ch_maps(void *data)
+{
+ struct snd_soc_dai_link *dai_link = data;
+
+ dai_link->ch_maps = NULL;
+}
+
static int snd_soc_compensate_channel_connection_map(struct snd_soc_card *card,
struct snd_soc_dai_link *dai_link)
{
struct snd_soc_dai_link_ch_map *ch_maps;
- int i;
-
- /*
- * dai_link->ch_maps indicates how CPU/Codec are connected.
- * It will be a map seen from a larger number of DAI.
- * see
- * soc.h :: [dai_link->ch_maps Image sample]
- */
+ int i, num_maps, ret;

- /* it should have ch_maps if connection was N:M */
+ /* N:M connections must be described by the machine driver */
if (dai_link->num_cpus > 1 && dai_link->num_codecs > 1 &&
dai_link->num_cpus != dai_link->num_codecs && !dai_link->ch_maps) {
dev_err(card->dev, "need to have ch_maps when N:M connection (%s)",
@@ -830,24 +787,38 @@ static int snd_soc_compensate_channel_connection_map(struct snd_soc_card *card,
return -EINVAL;
}

- /* do nothing if it has own maps */
if (dai_link->ch_maps)
goto sanity_check;

- /* check default map size */
- if (dai_link->num_cpus > MAX_DEFAULT_CH_MAP_SIZE ||
- dai_link->num_codecs > MAX_DEFAULT_CH_MAP_SIZE) {
- dev_err(card->dev, "soc-core.c needs update default_connection_maps");
- return -EINVAL;
+ /*
+ * No ch_maps provided: synthesize the default 1:1, 1:N or N:1 map.
+ * The N:M case was rejected above, so one side is always 1 here.
+ *
+ * Scrub dai_link->ch_maps on devm release so a static machine-driver
+ * dai_link does not carry a stale pointer into the next bind.
+ */
+ num_maps = max(dai_link->num_cpus, dai_link->num_codecs);
+ ch_maps = devm_kcalloc(card->dev, num_maps, sizeof(*ch_maps), GFP_KERNEL);
+ if (!ch_maps)
+ return -ENOMEM;
+
+ if (dai_link->num_cpus == dai_link->num_codecs) {
+ for (i = 0; i < num_maps; i++) {
+ ch_maps[i].cpu = i;
+ ch_maps[i].codec = i;
+ }
+ } else if (dai_link->num_cpus < dai_link->num_codecs) {
+ for (i = 0; i < num_maps; i++)
+ ch_maps[i].codec = i; /* cpu stays 0 */
+ } else {
+ for (i = 0; i < num_maps; i++)
+ ch_maps[i].cpu = i; /* codec stays 0 */
}

- /* Compensate missing map for ... */
- if (dai_link->num_cpus == dai_link->num_codecs)
- dai_link->ch_maps = default_ch_map_sync; /* for 1:1 or N:N */
- else if (dai_link->num_cpus < dai_link->num_codecs)
- dai_link->ch_maps = default_ch_map_1cpu; /* for 1:N */
- else
- dai_link->ch_maps = default_ch_map_1codec; /* for N:1 */
+ dai_link->ch_maps = ch_maps;
+ ret = devm_add_action_or_reset(card->dev, snd_soc_clear_ch_maps, dai_link);
+ if (ret)
+ return ret;

sanity_check:
dev_dbg(card->dev, "dai_link %s\n", dai_link->stream_name);
--
2.53.0