Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

platform/chrome: cros_ec: Use per-device lockdep key

Lockdep reports a bogus possible deadlock on MT8192 Chromebooks due to
the following lock sequences:

1. lock(i2c_register_adapter) [1]; lock(&ec_dev->lock)
2. lock(&ec_dev->lock); lock(prepare_lock);

The actual dependency chains are much longer. The shortened version
looks somewhat like:

1. cros-ec-rpmsg on mtk-scp
ec_dev->lock -> prepare_lock
2. In rt5682_i2c_probe() on native I2C bus:
prepare_lock -> regmap->lock -> (possibly) i2c_adapter->bus_lock
3. In rt5682_i2c_probe() on native I2C bus:
regmap->lock -> i2c_adapter->bus_lock
4. In sbs_probe() on i2c-cros-ec-tunnel I2C bus attached on cros-ec:
i2c_adapter->bus_lock -> ec_dev->lock

While lockdep is correct that the shared lockdep classes have a circular
dependency, it is bogus because

a) 2+3 happen on a native I2C bus
b) 4 happens on the actual EC on ChromeOS devices
c) 1 happens on the SCP coprocessor on MediaTek Chromebooks that just
happens to expose a cros-ec interface, but does not have an
i2c-cros-ec-tunnel I2C bus

In short, the "dependencies" are actually on different devices.

Setup a per-device lockdep key for cros_ec devices so lockdep can tell
the two instances apart. This helps with getting rid of the bogus
lockdep warning. For ChromeOS devices that only have one cros-ec
instance this doesn't change anything.

Also add a missing mutex_destroy, just to make the teardown complete.

[1] This is likely the per I2C bus lock with shared lockdep class

Signed-off-by: Chen-Yu Tsai <wenst@chromium.org>
Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
Link: https://lore.kernel.org/r/20230111074146.2624496-1-wenst@chromium.org

authored by

Chen-Yu Tsai and committed by
Tzung-Bi Shih
961a325b 5fa1dd81

+15 -3
+11 -3
drivers/platform/chrome/cros_ec.c
··· 199 199 if (!ec_dev->dout) 200 200 return -ENOMEM; 201 201 202 + lockdep_register_key(&ec_dev->lockdep_key); 202 203 mutex_init(&ec_dev->lock); 204 + lockdep_set_class(&ec_dev->lock, &ec_dev->lockdep_key); 203 205 204 206 err = cros_ec_query_all(ec_dev); 205 207 if (err) { 206 208 dev_err(dev, "Cannot identify the EC: error %d\n", err); 207 - return err; 209 + goto destroy_mutex; 208 210 } 209 211 210 212 if (ec_dev->irq > 0) { ··· 218 216 if (err) { 219 217 dev_err(dev, "Failed to request IRQ %d: %d\n", 220 218 ec_dev->irq, err); 221 - return err; 219 + goto destroy_mutex; 222 220 } 223 221 } 224 222 ··· 229 227 if (IS_ERR(ec_dev->ec)) { 230 228 dev_err(ec_dev->dev, 231 229 "Failed to create CrOS EC platform device\n"); 232 - return PTR_ERR(ec_dev->ec); 230 + err = PTR_ERR(ec_dev->ec); 231 + goto destroy_mutex; 233 232 } 234 233 235 234 if (ec_dev->max_passthru) { ··· 296 293 exit: 297 294 platform_device_unregister(ec_dev->ec); 298 295 platform_device_unregister(ec_dev->pd); 296 + destroy_mutex: 297 + mutex_destroy(&ec_dev->lock); 298 + lockdep_unregister_key(&ec_dev->lockdep_key); 299 299 return err; 300 300 } 301 301 EXPORT_SYMBOL(cros_ec_register); ··· 316 310 if (ec_dev->pd) 317 311 platform_device_unregister(ec_dev->pd); 318 312 platform_device_unregister(ec_dev->ec); 313 + mutex_destroy(&ec_dev->lock); 314 + lockdep_unregister_key(&ec_dev->lockdep_key); 319 315 } 320 316 EXPORT_SYMBOL(cros_ec_unregister); 321 317
+4
include/linux/platform_data/cros_ec_proto.h
··· 9 9 #define __LINUX_CROS_EC_PROTO_H 10 10 11 11 #include <linux/device.h> 12 + #include <linux/lockdep_types.h> 12 13 #include <linux/mutex.h> 13 14 #include <linux/notifier.h> 14 15 ··· 123 122 * command. The caller should check msg.result for the EC's result 124 123 * code. 125 124 * @pkt_xfer: Send packet to EC and get response. 125 + * @lockdep_key: Lockdep class for each instance. Unused if CONFIG_LOCKDEP is 126 + * not enabled. 126 127 * @lock: One transaction at a time. 127 128 * @mkbp_event_supported: 0 if MKBP not supported. Otherwise its value is 128 129 * the maximum supported version of the MKBP host event ··· 179 176 struct cros_ec_command *msg); 180 177 int (*pkt_xfer)(struct cros_ec_device *ec, 181 178 struct cros_ec_command *msg); 179 + struct lock_class_key lockdep_key; 182 180 struct mutex lock; 183 181 u8 mkbp_event_supported; 184 182 bool host_sleep_v1;