[PATCH] ASoC: SOF: Use flexible allocation for control data

From: Rosen Penev

Date: Mon May 18 2026 - 20:50:05 EST


Allocate IPC3 and IPC4 channel control data with kzalloc_flex()
for the trailing channel-value arrays instead of open-coding the
struct_size() allocation.

The existing scontrol size field continues to record the payload size
used by the control paths.

Assisted-by: Codex:GPT-5.5
Signed-off-by: Rosen Penev <rosenp@xxxxxxxxx>
---
sound/soc/sof/ipc3-topology.c | 6 ++++--
sound/soc/sof/ipc4-topology.c | 6 ++++--
2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/sound/soc/sof/ipc3-topology.c b/sound/soc/sof/ipc3-topology.c
index 8c37c0657315..9597f619fbe4 100644
--- a/sound/soc/sof/ipc3-topology.c
+++ b/sound/soc/sof/ipc3-topology.c
@@ -1826,7 +1826,8 @@ static int sof_ipc3_control_load_volume(struct snd_sof_dev *sdev, struct snd_sof
/* init the volume get/put data */
scontrol->size = struct_size(cdata, chanv, scontrol->num_channels);

- scontrol->ipc_control_data = kzalloc(scontrol->size, GFP_KERNEL);
+ scontrol->ipc_control_data = kzalloc_flex(*cdata, chanv,
+ scontrol->num_channels);
if (!scontrol->ipc_control_data)
return -ENOMEM;

@@ -1857,7 +1858,8 @@ static int sof_ipc3_control_load_enum(struct snd_sof_dev *sdev, struct snd_sof_c
/* init the enum get/put data */
scontrol->size = struct_size(cdata, chanv, scontrol->num_channels);

- scontrol->ipc_control_data = kzalloc(scontrol->size, GFP_KERNEL);
+ scontrol->ipc_control_data = kzalloc_flex(*cdata, chanv,
+ scontrol->num_channels);
if (!scontrol->ipc_control_data)
return -ENOMEM;

diff --git a/sound/soc/sof/ipc4-topology.c b/sound/soc/sof/ipc4-topology.c
index 76812d8fb567..9fd04a7857f6 100644
--- a/sound/soc/sof/ipc4-topology.c
+++ b/sound/soc/sof/ipc4-topology.c
@@ -2863,7 +2863,8 @@ static int sof_ipc4_control_load_volume(struct snd_sof_dev *sdev, struct snd_sof
scontrol->size = struct_size(control_data, chanv, scontrol->num_channels);

/* scontrol->ipc_control_data will be freed in sof_control_unload */
- scontrol->ipc_control_data = kzalloc(scontrol->size, GFP_KERNEL);
+ scontrol->ipc_control_data = kzalloc_flex(*control_data, chanv,
+ scontrol->num_channels);
if (!scontrol->ipc_control_data)
return -ENOMEM;

@@ -2905,7 +2906,8 @@ static int sof_ipc4_control_load_enum(struct snd_sof_dev *sdev, struct snd_sof_c
scontrol->size = struct_size(control_data, chanv, scontrol->num_channels);

/* scontrol->ipc_control_data will be freed in sof_control_unload */
- scontrol->ipc_control_data = kzalloc(scontrol->size, GFP_KERNEL);
+ scontrol->ipc_control_data = kzalloc_flex(*control_data, chanv,
+ scontrol->num_channels);
if (!scontrol->ipc_control_data)
return -ENOMEM;

--
2.54.0