hc
2024-01-03 2f7c68cb55ecb7331f2381deb497c27155f32faf
kernel/drivers/regulator/core.c
....@@ -222,6 +222,78 @@
222222 mutex_unlock(&regulator_nesting_mutex);
223223 }
224224
225
+/**
226
+ * regulator_lock_two - lock two regulators
227
+ * @rdev1: first regulator
228
+ * @rdev2: second regulator
229
+ * @ww_ctx: w/w mutex acquire context
230
+ *
231
+ * Locks both rdevs using the regulator_ww_class.
232
+ */
233
+static void regulator_lock_two(struct regulator_dev *rdev1,
234
+ struct regulator_dev *rdev2,
235
+ struct ww_acquire_ctx *ww_ctx)
236
+{
237
+ struct regulator_dev *tmp;
238
+ int ret;
239
+
240
+ ww_acquire_init(ww_ctx, &regulator_ww_class);
241
+
242
+ /* Try to just grab both of them */
243
+ ret = regulator_lock_nested(rdev1, ww_ctx);
244
+ WARN_ON(ret);
245
+ ret = regulator_lock_nested(rdev2, ww_ctx);
246
+ if (ret != -EDEADLOCK) {
247
+ WARN_ON(ret);
248
+ goto exit;
249
+ }
250
+
251
+ while (true) {
252
+ /*
253
+ * Start of loop: rdev1 was locked and rdev2 was contended.
254
+ * Need to unlock rdev1, slowly lock rdev2, then try rdev1
255
+ * again.
256
+ */
257
+ regulator_unlock(rdev1);
258
+
259
+ ww_mutex_lock_slow(&rdev2->mutex, ww_ctx);
260
+ rdev2->ref_cnt++;
261
+ rdev2->mutex_owner = current;
262
+ ret = regulator_lock_nested(rdev1, ww_ctx);
263
+
264
+ if (ret == -EDEADLOCK) {
265
+ /* More contention; swap which needs to be slow */
266
+ tmp = rdev1;
267
+ rdev1 = rdev2;
268
+ rdev2 = tmp;
269
+ } else {
270
+ WARN_ON(ret);
271
+ break;
272
+ }
273
+ }
274
+
275
+exit:
276
+ ww_acquire_done(ww_ctx);
277
+}
278
+
279
+/**
280
+ * regulator_unlock_two - unlock two regulators
281
+ * @rdev1: first regulator
282
+ * @rdev2: second regulator
283
+ * @ww_ctx: w/w mutex acquire context
284
+ *
285
+ * The inverse of regulator_lock_two().
286
+ */
287
+
288
+static void regulator_unlock_two(struct regulator_dev *rdev1,
289
+ struct regulator_dev *rdev2,
290
+ struct ww_acquire_ctx *ww_ctx)
291
+{
292
+ regulator_unlock(rdev2);
293
+ regulator_unlock(rdev1);
294
+ ww_acquire_fini(ww_ctx);
295
+}
296
+
225297 static bool regulator_supply_is_couple(struct regulator_dev *rdev)
226298 {
227299 struct regulator_dev *c_rdev;
....@@ -349,6 +421,7 @@
349421 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
350422 old_contended_rdev = new_contended_rdev;
351423 old_contended_rdev->ref_cnt++;
424
+ old_contended_rdev->mutex_owner = current;
352425 }
353426
354427 err = regulator_lock_recursive(rdev,
....@@ -986,7 +1059,7 @@
9861059 /* get input voltage */
9871060 input_uV = 0;
9881061 if (rdev->supply)
989
- input_uV = regulator_get_voltage(rdev->supply);
1062
+ input_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
9901063 if (input_uV <= 0)
9911064 input_uV = rdev->constraints->input_uV;
9921065 if (input_uV <= 0) {
....@@ -1434,7 +1507,13 @@
14341507 if (rdev->supply_name && !rdev->supply)
14351508 return -EPROBE_DEFER;
14361509
1437
- if (rdev->supply) {
1510
+ /* If supplying regulator has already been enabled,
1511
+ * it's not intended to have use_count increment
1512
+ * when rdev is only boot-on.
1513
+ */
1514
+ if (rdev->supply &&
1515
+ (rdev->constraints->always_on ||
1516
+ !regulator_is_enabled(rdev->supply))) {
14381517 ret = regulator_enable(rdev->supply);
14391518 if (ret < 0) {
14401519 _regulator_put(rdev->supply);
....@@ -1459,8 +1538,8 @@
14591538
14601539 /**
14611540 * set_supply - set regulator supply regulator
1462
- * @rdev: regulator name
1463
- * @supply_rdev: supply regulator name
1541
+ * @rdev: regulator (locked)
1542
+ * @supply_rdev: supply regulator (locked))
14641543 *
14651544 * Called by platform initialisation code to set the supply regulator for this
14661545 * regulator. This ensures that a regulators supply will also be enabled by the
....@@ -1478,6 +1557,7 @@
14781557
14791558 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
14801559 if (rdev->supply == NULL) {
1560
+ module_put(supply_rdev->owner);
14811561 err = -ENOMEM;
14821562 return err;
14831563 }
....@@ -1631,6 +1711,8 @@
16311711 struct regulator *regulator;
16321712 int err = 0;
16331713
1714
+ lockdep_assert_held_once(&rdev->mutex.base);
1715
+
16341716 if (dev) {
16351717 char buf[REG_STR_SIZE];
16361718 int size;
....@@ -1651,16 +1733,14 @@
16511733
16521734 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
16531735 if (regulator == NULL) {
1654
- kfree(supply_name);
1736
+ kfree_const(supply_name);
16551737 return NULL;
16561738 }
16571739
16581740 regulator->rdev = rdev;
16591741 regulator->supply_name = supply_name;
16601742
1661
- regulator_lock(rdev);
16621743 list_add(&regulator->list, &rdev->consumer_list);
1663
- regulator_unlock(rdev);
16641744
16651745 if (dev) {
16661746 regulator->dev = dev;
....@@ -1677,19 +1757,19 @@
16771757
16781758 if (err != -EEXIST)
16791759 regulator->debugfs = debugfs_create_dir(supply_name, rdev->debugfs);
1680
- if (!regulator->debugfs) {
1760
+ else
1761
+ regulator->debugfs = ERR_PTR(err);
1762
+ if (IS_ERR(regulator->debugfs))
16811763 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1682
- } else {
1683
- debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1684
- &regulator->uA_load);
1685
- debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1686
- &regulator->voltage[PM_SUSPEND_ON].min_uV);
1687
- debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1688
- &regulator->voltage[PM_SUSPEND_ON].max_uV);
1689
- debugfs_create_file("constraint_flags", 0444,
1690
- regulator->debugfs, regulator,
1691
- &constraint_flags_fops);
1692
- }
1764
+
1765
+ debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1766
+ &regulator->uA_load);
1767
+ debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1768
+ &regulator->voltage[PM_SUSPEND_ON].min_uV);
1769
+ debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1770
+ &regulator->voltage[PM_SUSPEND_ON].max_uV);
1771
+ debugfs_create_file("constraint_flags", 0444, regulator->debugfs,
1772
+ regulator, &constraint_flags_fops);
16931773
16941774 /*
16951775 * Check now if the regulator is an always on regulator - if
....@@ -1781,6 +1861,7 @@
17811861 node = of_get_regulator(dev, supply);
17821862 if (node) {
17831863 r = of_find_regulator_by_node(node);
1864
+ of_node_put(node);
17841865 if (r)
17851866 return r;
17861867
....@@ -1825,6 +1906,7 @@
18251906 {
18261907 struct regulator_dev *r;
18271908 struct device *dev = rdev->dev.parent;
1909
+ struct ww_acquire_ctx ww_ctx;
18281910 int ret = 0;
18291911
18301912 /* No supply to resolve? */
....@@ -1891,23 +1973,23 @@
18911973 * between rdev->supply null check and setting rdev->supply in
18921974 * set_supply() from concurrent tasks.
18931975 */
1894
- regulator_lock(rdev);
1976
+ regulator_lock_two(rdev, r, &ww_ctx);
18951977
18961978 /* Supply just resolved by a concurrent task? */
18971979 if (rdev->supply) {
1898
- regulator_unlock(rdev);
1980
+ regulator_unlock_two(rdev, r, &ww_ctx);
18991981 put_device(&r->dev);
19001982 goto out;
19011983 }
19021984
19031985 ret = set_supply(rdev, r);
19041986 if (ret < 0) {
1905
- regulator_unlock(rdev);
1987
+ regulator_unlock_two(rdev, r, &ww_ctx);
19061988 put_device(&r->dev);
19071989 goto out;
19081990 }
19091991
1910
- regulator_unlock(rdev);
1992
+ regulator_unlock_two(rdev, r, &ww_ctx);
19111993
19121994 /*
19131995 * In set_machine_constraints() we may have turned this regulator on
....@@ -2020,7 +2102,9 @@
20202102 return regulator;
20212103 }
20222104
2105
+ regulator_lock(rdev);
20232106 regulator = create_regulator(rdev, dev, id);
2107
+ regulator_unlock(rdev);
20242108 if (regulator == NULL) {
20252109 regulator = ERR_PTR(-ENOMEM);
20262110 module_put(rdev->owner);
....@@ -5216,10 +5300,8 @@
52165300 }
52175301
52185302 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
5219
- if (!rdev->debugfs) {
5220
- rdev_warn(rdev, "Failed to create debugfs directory\n");
5221
- return;
5222
- }
5303
+ if (IS_ERR(rdev->debugfs))
5304
+ rdev_dbg(rdev, "Failed to create debugfs directory\n");
52235305
52245306 debugfs_create_u32("use_count", 0444, rdev->debugfs,
52255307 &rdev->use_count);
....@@ -5742,6 +5824,7 @@
57425824 regulator_remove_coupling(rdev);
57435825 mutex_unlock(&regulator_list_mutex);
57445826 wash:
5827
+ regulator_put(rdev->supply);
57455828 kfree(rdev->coupling_desc.coupled_rdevs);
57465829 mutex_lock(&regulator_list_mutex);
57475830 regulator_ena_gpio_free(rdev);
....@@ -6135,6 +6218,7 @@
61356218 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
61366219 old_contended_rdev = new_contended_rdev;
61376220 old_contended_rdev->ref_cnt++;
6221
+ old_contended_rdev->mutex_owner = current;
61386222 }
61396223
61406224 err = regulator_summary_lock_all(ww_ctx,
....@@ -6195,8 +6279,8 @@
61956279 ret = class_register(&regulator_class);
61966280
61976281 debugfs_root = debugfs_create_dir("regulator", NULL);
6198
- if (!debugfs_root)
6199
- pr_warn("regulator: Failed to create debugfs directory\n");
6282
+ if (IS_ERR(debugfs_root))
6283
+ pr_debug("regulator: Failed to create debugfs directory\n");
62006284
62016285 #ifdef CONFIG_DEBUG_FS
62026286 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,