Skip to content

Commit a4857d1

Browse files
rfvirgilvinodkoul
authored andcommitted
soundwire: stream: Make master_list ordered to prevent deadlocks
Always add buses to the stream->master_list in a fixed order. The unique bus->id is used to order the adding of buses to the list. This prevents lockdep asserts and possible deadlocks on streams that have multiple buses. sdw_acquire_bus_lock() takes bus_lock in the order that buses are listed in stream->master_list. do_bank_switch() takes all the msg_lock in the same order. To prevent a lockdep assert, and a possible real deadlock, the relative order of taking these mutexes must always be the same. For example, if a stream takes the mutexes in the order (bus0, bus1) lockdep will assert if another stream takes them in the order (bus1, bus0). More complex relative ordering will also assert, for example if two streams take (bus0, bus1) and (bus1, bus2), then a third stream takes (bus2, bus0). Previously sdw_stream_add_master() simply added the given bus to the end of the list, requiring the caller to guarantee that buses are added in a fixed order. This isn't reasonable or necessary - it's an internal implementation detail that should not be exposed by the API. It doesn't really make sense when there could be multiple independent calling drivers, to say "you must add your buses in the same order as a different driver, that you don't know about, added them". Signed-off-by: Richard Fitzgerald <[email protected]> Reviewed-by: Pierre-Louis Bossart <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Vinod Koul <[email protected]>
1 parent 256a997 commit a4857d1

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

drivers/soundwire/stream.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,8 @@ static struct sdw_master_runtime
11501150
*sdw_master_rt_alloc(struct sdw_bus *bus,
11511151
struct sdw_stream_runtime *stream)
11521152
{
1153-
struct sdw_master_runtime *m_rt;
1153+
struct sdw_master_runtime *m_rt, *walk_m_rt;
1154+
struct list_head *insert_after;
11541155

11551156
m_rt = kzalloc(sizeof(*m_rt), GFP_KERNEL);
11561157
if (!m_rt)
@@ -1159,7 +1160,20 @@ static struct sdw_master_runtime
11591160
/* Initialization of Master runtime handle */
11601161
INIT_LIST_HEAD(&m_rt->port_list);
11611162
INIT_LIST_HEAD(&m_rt->slave_rt_list);
1162-
list_add_tail(&m_rt->stream_node, &stream->master_list);
1163+
1164+
/*
1165+
* Add in order of bus id so that when taking the bus_lock
1166+
* of multiple buses they will always be taken in the same
1167+
* order to prevent a mutex deadlock.
1168+
*/
1169+
insert_after = &stream->master_list;
1170+
list_for_each_entry_reverse(walk_m_rt, &stream->master_list, stream_node) {
1171+
if (walk_m_rt->bus->id < bus->id) {
1172+
insert_after = &walk_m_rt->stream_node;
1173+
break;
1174+
}
1175+
}
1176+
list_add(&m_rt->stream_node, insert_after);
11631177

11641178
list_add_tail(&m_rt->bus_node, &bus->m_rt_list);
11651179

0 commit comments

Comments
 (0)