Skip to content

soundwire: Fix bandwidth leak in multi-link aggregation failure - #5972

Closed
bardliao wants to merge 4 commits into
thesofproject:topic/sof-devfrom
bardliao:for-sdw-bandwidth-leak
Closed

bardliao wants to merge 4 commits into
thesofproject:topic/sof-devfrom
bardliao:for-sdw-bandwidth-leak

Conversation

@bardliao

Copy link
Copy Markdown
Collaborator

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 restoring the parameters for all buses.

lane_used_bandwidth is used to store the used bandwidth for each lane.
When we restore the bus->params, the lane_used_bandwidth value will not
be restored in current code. Move it to sdw_bus_params to fix the issue.

Fixes: 366fd59 ("soundwire: generic_bandwidth_allocation: add lane in sdw_group_params")
Signed-off-by: Bard Liao <yung-chuan.liao@linux.intel.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The rollback path can program the wrong bus and perform invalid or unnecessary bank switches.

1 open finding
What changed in this PR

Fixes SoundWire bandwidth leakage when multi-link stream preparation fails.

Changes:

  • Includes per-lane bandwidth in restorable bus parameters.
  • Snapshots each master’s parameters before preparation.
  • Adds multi-link rollback and bank restoration logic.
File Description
include/​linux/​soundwire/​sdw.h Moves lane bandwidth into bus parameters.
drivers/​soundwire/​stream.c Adds snapshot and rollback handling.
drivers/​soundwire/​generic_bandwidth_allocation.c Uses relocated lane bandwidth state.
drivers/​soundwire/​bus.h Stores parameter snapshots per master runtime.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread drivers/soundwire/stream.c Outdated
Copilot AI balanced review requested due to automatic review settings October 13, 2026 03:23
@bardliao
bardliao force-pushed the for-sdw-bandwidth-leak branch from 8f71e3c to 7f32ec1 Compare October 13, 2026 03:23

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The rollback can program or switch incorrect buses and corrupt bank state.

2 open findings
1 resolved since last review

🧠 Review effort: Balanced

Comment thread drivers/soundwire/stream.c Outdated
Comment thread drivers/soundwire/stream.c Outdated
@bardliao
bardliao force-pushed the for-sdw-bandwidth-leak branch from 7f32ec1 to b6fad93 Compare October 13, 2026 03:35
Copilot AI balanced review requested due to automatic review settings October 13, 2026 03:35
@bardliao
bardliao force-pushed the for-sdw-bandwidth-leak branch from b6fad93 to dc95264 Compare October 13, 2026 03:36

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Rollback can program the wrong bus and perform unsafe bank switches on failure paths.

2 open findings
2 resolved since last review

🧠 Review effort: Balanced

Comment thread drivers/soundwire/stream.c Outdated
Comment on lines +1579 to +1581
ret1 = do_bank_switch(stream);
if (ret1 < 0)
pr_err("%s: do_bank_switch failed: %d after restoring params\n", __func__, ret1);
Copilot AI balanced review requested due to automatic review settings October 13, 2026 03:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Rollback targets the wrong bus and leaves successfully prepared ports configured.

3 open findings

🧠 Review effort: Balanced

dev_err(bus->dev, "Prepare port(s) failed ret = %d\n",
ret);
goto restore_params;
goto revert_bank;
Copilot AI balanced review requested due to automatic review settings October 13, 2026 03:51
@bardliao
bardliao force-pushed the for-sdw-bandwidth-leak branch from dc95264 to 18d020e Compare October 13, 2026 03:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Rollback can program stale per-port configuration against restored bus parameters.

3 open findings
1 resolved since last review

🧠 Review effort: Balanced

Comment on lines +1571 to +1572
m_rt->bus->params = m_rt->params_backup;
ret1 = sdw_program_params(m_rt->bus, true);
The bank values are already switched in do_bank_switch(), so we need to
switch curr_bank and next_bank back to reflect the actual bank.

Signed-off-by: Bard Liao <yung-chuan.liao@linux.intel.com>
Restoring bus->params alone leaves the hardware programmed with the
values from the failed stream preparation. Reprogram the restored params
and switch banks so software and hardware use the same bus configuration.

Signed-off-by: Bard Liao <yung-chuan.liao@linux.intel.com>
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 parameters before making any
modifications. In case of failure, we iterate through the stream's master
list and restore each bus to its original parameter state, ensuring an
atomic rollback and preventing any bandwidth leaks.

Signed-off-by: David Lin <david.lin@intel.com>
@bardliao
bardliao force-pushed the for-sdw-bandwidth-leak branch from 18d020e to a90934b Compare October 13, 2026 04:38
Copilot AI balanced review requested due to automatic review settings October 13, 2026 04:38

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A rollback programming failure can leave later buses’ bandwidth allocations unrestored.

4 open findings

🧠 Review effort: Balanced

Comment on lines +1566 to +1580
list_for_each_entry(m_rt, &stream->master_list, stream_node) {
/*
* Revert all masters to their snapshotted original parameters.
* Any master that was not modified will safely revert to its own original state.
*/
m_rt->bus->params = m_rt->params_backup;
ret1 = sdw_program_params(m_rt->bus, true);
if (ret1 < 0) {
dev_err(m_rt->bus->dev,
"Program params failed: %d after restoring params\n",
ret1);
/* No need to do bank switch and return the original error code */
return ret;
}
}
@bardliao bardliao closed this Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants