Skip to content

Commit 8668c10

Browse files
Shrusti-pklgirdwood
authored andcommitted
audio: module_adapter: cap connected buffers at MODULE_MAX_CONNECTIONS
struct processing_module holds CONFIG_MODULE_MAX_CONNECTIONS entries in sinks[] and sources[], but the module adapter stores one entry per connected buffer without bounding the index. On IPC4, module_adapter_update_sinks_sources() runs from module_adapter_bind(), and ipc4_comp_connect() connects a new buffer for every MOD_BIND, so a ninth bind on one side of a module writes sinks[8] over sources[0] and further binds run past the processing_module allocation. On IPC3, module_adapter_sink_src_prepare() does the same at prepare time. The connection count checks in module_adapter_prepare() only covered the audio_stream path removed in a42e9b7, and the max_sources and max_sinks fields are no longer read. Refuse a bind in module_adapter_bind() that brings the buffers connected on that side above CONFIG_MODULE_MAX_CONNECTIONS, and fail module_adapter_sink_src_prepare() with -EINVAL instead of writing past the arrays. A refusal on the sink side goes through the e_sink_bind path of ipc4_comp_connect(), which unbinds the source while the buffer is still connected. The source then refreshes its sinks[] with the buffer that is freed right after. Disconnect first, as ipc4_comp_disconnect() does. Signed-off-by: Shrushti P K <shrusthi@labs.digiscrypt.com>
1 parent d80a052 commit 8668c10

4 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎src/audio/module_adapter/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ sequenceDiagram
5151

5252
Connection establishes buffer relationships (`comp_buffer`) connecting the component sources and sinks to the rest of the generic pipeline graph.
5353

54+
Each connected buffer takes a slot in `mod->sinks[]` or `mod->sources[]`, which hold `CONFIG_MODULE_MAX_CONNECTIONS` entries each. A bind (IPC4) or prepare (IPC3) that would need more slots fails with `-EINVAL`.
55+
5456
```mermaid
5557
sequenceDiagram
5658
participant Pipe as Pipeline

‎src/audio/module_adapter/module_adapter_ipc3.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,23 @@ int module_adapter_sink_src_prepare(struct comp_dev *dev)
319319
/* acquire all sink and source buffers, get handlers to sink/source API */
320320
i = 0;
321321
comp_dev_for_each_consumer(dev, sink_buffer) {
322+
if (i == CONFIG_MODULE_MAX_CONNECTIONS) {
323+
comp_err(dev, "more than %d sinks connected",
324+
CONFIG_MODULE_MAX_CONNECTIONS);
325+
return -EINVAL;
326+
}
322327
mod->sinks[i] = audio_buffer_get_sink(&sink_buffer->audio_buffer);
323328
i++;
324329
}
325330
mod->num_of_sinks = i;
326331

327332
i = 0;
328333
comp_dev_for_each_producer(dev, source_buffer) {
334+
if (i == CONFIG_MODULE_MAX_CONNECTIONS) {
335+
comp_err(dev, "more than %d sources connected",
336+
CONFIG_MODULE_MAX_CONNECTIONS);
337+
return -EINVAL;
338+
}
329339
mod->sources[i] = audio_buffer_get_source(&source_buffer->audio_buffer);
330340
i++;
331341
}

‎src/audio/module_adapter/module_adapter_ipc4.c‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,29 @@ __cold static int module_update_source_buffer_params(struct processing_module *m
471471
__cold int module_adapter_bind(struct comp_dev *dev, const struct bind_info *bind_data)
472472
{
473473
struct processing_module *mod = comp_mod(dev);
474+
struct comp_buffer *buffer;
475+
unsigned int count = 0;
474476
int ret;
475477

476478
assert_can_be_cold();
477479

480+
/*
481+
* The buffer is already connected here and every connected buffer
482+
* takes a slot in mod->sinks[] or mod->sources[].
483+
*/
484+
if (bind_data->bind_type == COMP_BIND_TYPE_SINK) {
485+
comp_dev_for_each_consumer(dev, buffer)
486+
count++;
487+
} else {
488+
comp_dev_for_each_producer(dev, buffer)
489+
count++;
490+
}
491+
492+
if (count > CONFIG_MODULE_MAX_CONNECTIONS) {
493+
comp_err(dev, "%u connections exceed max %d", count, CONFIG_MODULE_MAX_CONNECTIONS);
494+
return -EINVAL;
495+
}
496+
478497
ret = module_update_source_buffer_params(mod, bind_data);
479498
if (ret < 0)
480499
return ret;

‎src/ipc/ipc4/helper.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1004,10 +1004,17 @@ __cold int ipc4_comp_connect(struct ipc *ipc, const struct ipc4_module_bind_unbi
10041004
return IPC4_SUCCESS;
10051005

10061006
e_sink_bind:
1007-
/* sink bind was already called */
1007+
/*
1008+
* The source is already bound. Disconnect first, as
1009+
* ipc4_comp_disconnect() does, so its unbind does not keep the
1010+
* buffer in the source's sinks[].
1011+
*/
1012+
pipeline_disconnect(sink, buffer, PPL_CONN_DIR_BUFFER_TO_COMP);
1013+
pipeline_disconnect(source, buffer, PPL_CONN_DIR_COMP_TO_BUFFER);
10081014
bind_data.bind_type = COMP_BIND_TYPE_SINK;
10091015
bind_data.sink = audio_buffer_get_sink(&buffer->audio_buffer);
10101016
comp_unbind(source, &bind_data);
1017+
goto free;
10111018
e_src_bind:
10121019
pipeline_disconnect(sink, buffer, PPL_CONN_DIR_BUFFER_TO_COMP);
10131020
e_sink_connect:

0 commit comments

Comments
 (0)