Skip to content

Commit 256a997

Browse files
rfvirgilvinodkoul
authored andcommitted
soundwire: bus: Prevent lockdep asserts when stream has multiple buses
Give the bus_lock and msg_lock of each bus a different unique key so that it is possible to acquire the locks of multiple buses without lockdep asserting a possible deadlock. Using mutex_init() to initialize a mutex gives all those mutexes the same lock class. Lockdep checking treats it as an error to attempt to take a mutex while already holding a mutex of the same class. This causes a lockdep assert when sdw_acquire_bus_lock() attempts to lock multiple buses, and when do_bank_switch() takes multiple msg_lock. [ 138.697350] WARNING: possible recursive locking detected [ 138.697366] 6.3.0-test #1 Tainted: G E [ 138.697380] -------------------------------------------- [ 138.697394] play/903 is trying to acquire lock: [ 138.697409] ffff99b8c41aa8c8 (&bus->bus_lock){+.+.}-{3:3}, at: sdw_prepare_stream+0x52/0x2e0 [ 138.697443] but task is already holding lock: [ 138.697468] ffff99b8c41af8c8 (&bus->bus_lock){+.+.}-{3:3}, at: sdw_prepare_stream+0x52/0x2e0 [ 138.697493] other info that might help us debug this: [ 138.697521] Possible unsafe locking scenario: [ 138.697540] CPU0 [ 138.697550] ---- [ 138.697559] lock(&bus->bus_lock); [ 138.697570] lock(&bus->bus_lock); [ 138.697581] *** DEADLOCK *** Giving each mutex a unique key allows multiple to be held without triggering a lockdep assert. But note that it does not allow them to be taken in one order then a different order. If two mutexes are taken in the order A, B then they must always be taken in that order otherwise they could deadlock. 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 490937d commit 256a997

File tree

2 files changed

+16
-2
lines changed
  • drivers/soundwire
  • include/linux/soundwire

2 files changed

+16
-2
lines changed

drivers/soundwire/bus.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,17 @@ int sdw_bus_master_add(struct sdw_bus *bus, struct device *parent,
6969
return -EINVAL;
7070
}
7171

72-
mutex_init(&bus->msg_lock);
73-
mutex_init(&bus->bus_lock);
72+
/*
73+
* Give each bus_lock and msg_lock a unique key so that lockdep won't
74+
* trigger a deadlock warning when the locks of several buses are
75+
* grabbed during configuration of a multi-bus stream.
76+
*/
77+
lockdep_register_key(&bus->msg_lock_key);
78+
__mutex_init(&bus->msg_lock, "msg_lock", &bus->msg_lock_key);
79+
80+
lockdep_register_key(&bus->bus_lock_key);
81+
__mutex_init(&bus->bus_lock, "bus_lock", &bus->bus_lock_key);
82+
7483
INIT_LIST_HEAD(&bus->slaves);
7584
INIT_LIST_HEAD(&bus->m_rt_list);
7685

@@ -181,6 +190,8 @@ void sdw_bus_master_delete(struct sdw_bus *bus)
181190
sdw_master_device_del(bus);
182191

183192
sdw_bus_debugfs_exit(bus);
193+
lockdep_unregister_key(&bus->bus_lock_key);
194+
lockdep_unregister_key(&bus->msg_lock_key);
184195
ida_free(&sdw_bus_ida, bus->id);
185196
}
186197
EXPORT_SYMBOL(sdw_bus_master_delete);

include/linux/soundwire/sdw.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#define __SOUNDWIRE_H
66

77
#include <linux/bug.h>
8+
#include <linux/lockdep_types.h>
89
#include <linux/mod_devicetable.h>
910
#include <linux/bitfield.h>
1011

@@ -907,7 +908,9 @@ struct sdw_bus {
907908
struct list_head slaves;
908909
DECLARE_BITMAP(assigned, SDW_MAX_DEVICES);
909910
struct mutex bus_lock;
911+
struct lock_class_key bus_lock_key;
910912
struct mutex msg_lock;
913+
struct lock_class_key msg_lock_key;
911914
int (*compute_params)(struct sdw_bus *bus);
912915
const struct sdw_master_ops *ops;
913916
const struct sdw_master_port_ops *port_ops;

0 commit comments

Comments
 (0)