Skip to content

Commit dafc50d

Browse files
author
Jean Delvare
committed
i2c: Use a separate mutex for userspace client lists
Moving userspace-instantiated clients to separate lists wasn't nearly enough to avoid deadlocks in multiplexed bus cases. We also want to have a dedicated mutex to protect each list. Signed-off-by: Jean Delvare <khali@linux-fr.org> Cc: Michael Lawnick <ml.lawnick@gmx.de>
1 parent 8031d79 commit dafc50d

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

‎drivers/i2c/i2c-core.c‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -662,9 +662,9 @@ i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
662662
return -EINVAL;
663663

664664
/* Keep track of the added device */
665-
i2c_lock_adapter(adap);
665+
mutex_lock(&adap->userspace_clients_lock);
666666
list_add_tail(&client->detected, &adap->userspace_clients);
667-
i2c_unlock_adapter(adap);
667+
mutex_unlock(&adap->userspace_clients_lock);
668668
dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
669669
info.type, info.addr);
670670

@@ -703,7 +703,7 @@ i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
703703

704704
/* Make sure the device was added through sysfs */
705705
res = -ENOENT;
706-
i2c_lock_adapter(adap);
706+
mutex_lock(&adap->userspace_clients_lock);
707707
list_for_each_entry_safe(client, next, &adap->userspace_clients,
708708
detected) {
709709
if (client->addr == addr) {
@@ -716,7 +716,7 @@ i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
716716
break;
717717
}
718718
}
719-
i2c_unlock_adapter(adap);
719+
mutex_unlock(&adap->userspace_clients_lock);
720720

721721
if (res < 0)
722722
dev_err(dev, "%s: Can't find device in list\n",
@@ -798,6 +798,7 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
798798
}
799799

800800
rt_mutex_init(&adap->bus_lock);
801+
mutex_init(&adap->userspace_clients_lock);
801802
INIT_LIST_HEAD(&adap->userspace_clients);
802803

803804
/* Set default timeout to 1 second if not already set */
@@ -1003,15 +1004,15 @@ int i2c_del_adapter(struct i2c_adapter *adap)
10031004
return res;
10041005

10051006
/* Remove devices instantiated from sysfs */
1006-
i2c_lock_adapter(adap);
1007+
mutex_lock(&adap->userspace_clients_lock);
10071008
list_for_each_entry_safe(client, next, &adap->userspace_clients,
10081009
detected) {
10091010
dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
10101011
client->addr);
10111012
list_del(&client->detected);
10121013
i2c_unregister_device(client);
10131014
}
1014-
i2c_unlock_adapter(adap);
1015+
mutex_unlock(&adap->userspace_clients_lock);
10151016

10161017
/* Detach any active clients. This can't fail, thus we do not
10171018
checking the returned value. */

‎include/linux/i2c.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ struct i2c_adapter {
368368
char name[48];
369369
struct completion dev_released;
370370

371+
struct mutex userspace_clients_lock;
371372
struct list_head userspace_clients;
372373
};
373374
#define to_i2c_adapter(d) container_of(d, struct i2c_adapter, dev)

0 commit comments

Comments
 (0)