Skip to content

Commit f78f746

Browse files
davidlin923David Lin
authored andcommitted
soundwire: stream: Fix bandwidth leak in multi-link aggregation failure
When preparing a multi-link aggregation stream, a failure on a subsequent link during _sdw_prepare_stream() causes the stream preparation to fail. However, the bandwidth allocated on the preceding successful links is never released, causing a cumulative bandwidth leak. Fix this by saving a snapshot of each bus's bandwidth before making any modifications. In case of failure, we iterate through the stream's master list and restore each bus to its original bandwidth state, ensuring an atomic rollback and preventing any bandwidth leaks. Signed-off-by: David Lin <david.lin@intel.com>
1 parent 31d325a commit f78f746

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

‎drivers/soundwire/bus.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ struct sdw_slave_runtime {
158158
* @direction: Data direction for Master
159159
* @ch_count: Number of channels handled by the Master for
160160
* this stream, can be zero.
161+
* @bandwidth_backup: Snapshot of bus bandwidth saved before stream
162+
* preparation, used to safely roll back accounting on failure.
161163
* @slave_rt_list: Slave runtime list
162164
* @port_list: List of Master Ports configured for this stream, can be zero.
163165
* @stream_node: sdw_stream_runtime master_list node
@@ -168,6 +170,7 @@ struct sdw_master_runtime {
168170
struct sdw_stream_runtime *stream;
169171
enum sdw_data_direction direction;
170172
unsigned int ch_count;
173+
unsigned int bandwidth_backup;
171174
struct list_head slave_rt_list;
172175
struct list_head port_list;
173176
struct list_head stream_node;

‎drivers/soundwire/stream.c‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,19 +1487,23 @@ static int _sdw_prepare_stream(struct sdw_stream_runtime *stream,
14871487
struct sdw_master_runtime *m_rt;
14881488
struct sdw_bus *bus;
14891489
struct sdw_master_prop *prop;
1490-
struct sdw_bus_params params;
14911490
int ret;
14921491

1492+
/* Pre-pass: snapshot original bus bandwidth for all masters */
1493+
list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1494+
m_rt->bandwidth_backup = m_rt->bus->params.bandwidth;
1495+
}
1496+
14931497
/* Prepare Master(s) and Slave(s) port(s) associated with stream */
14941498
list_for_each_entry(m_rt, &stream->master_list, stream_node) {
14951499
bus = m_rt->bus;
14961500
prop = &bus->prop;
1497-
memcpy(&params, &bus->params, sizeof(params));
14981501

14991502
/* TODO: Support Asynchronous mode */
15001503
if ((prop->max_clk_freq % stream->params.rate) != 0) {
15011504
dev_err(bus->dev, "Async mode not supported\n");
1502-
return -EINVAL;
1505+
ret = -EINVAL;
1506+
goto restore_params;
15031507
}
15041508

15051509
if (update_params) {
@@ -1550,7 +1554,13 @@ static int _sdw_prepare_stream(struct sdw_stream_runtime *stream,
15501554
return ret;
15511555

15521556
restore_params:
1553-
memcpy(&bus->params, &params, sizeof(params));
1557+
/*
1558+
* Revert all masters to their snapshotted original bandwidth.
1559+
* Any master that was not modified will safely revert to its own original state.
1560+
*/
1561+
list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1562+
m_rt->bus->params.bandwidth = m_rt->bandwidth_backup;
1563+
}
15541564
return ret;
15551565
}
15561566

0 commit comments

Comments
 (0)