aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaciej Purski <m.purski@samsung.com>2018-04-23 10:33:37 -0400
committerMark Brown <broonie@kernel.org>2018-05-17 03:05:03 -0400
commit66cf9a7e0192734c1c94751e628bd075be62cff4 (patch)
treebd032804bb4f2cd5c17ab66c3da7b6e69ace45b8
parent0369e02b75e6381f892e3bd45f1d8d6330d855fb (diff)
regulator: core: Make locks re-entrant
Setting voltage, enabling/disabling regulators requires operations on all regulators related with the regulator being changed. Therefore, all of them should be locked for the whole operation. With the current locking implementation, adding additional dependency (regulators coupling) causes deadlocks in some cases. Introduce a possibility to attempt to lock a mutex multiple times by the same task without waiting on a mutex. This should handle all reasonable coupling-supplying combinations, especially when two coupled regulators share common supplies. The only situation that should be forbidden is simultaneous coupling and supplying between a pair of regulators. The idea is based on clk core. Signed-off-by: Maciej Purski <m.purski@samsung.com> Signed-off-by: Mark Brown <broonie@kernel.org>
-rw-r--r--drivers/regulator/core.c132
-rw-r--r--include/linux/regulator/driver.h2
2 files changed, 93 insertions, 41 deletions
diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index fe314ff56772..0ca941b53571 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -147,6 +147,56 @@ static inline struct regulator_dev *rdev_get_supply(struct regulator_dev *rdev)
147} 147}
148 148
149/** 149/**
150 * regulator_lock_nested - lock a single regulator
151 * @rdev: regulator source
152 * @subclass: mutex subclass used for lockdep
153 *
154 * This function can be called many times by one task on
155 * a single regulator and its mutex will be locked only
156 * once. If a task, which is calling this function is other
157 * than the one, which initially locked the mutex, it will
158 * wait on mutex.
159 */
160static void regulator_lock_nested(struct regulator_dev *rdev,
161 unsigned int subclass)
162{
163 if (!mutex_trylock(&rdev->mutex)) {
164 if (rdev->mutex_owner == current) {
165 rdev->ref_cnt++;
166 return;
167 }
168 mutex_lock_nested(&rdev->mutex, subclass);
169 }
170
171 rdev->ref_cnt = 1;
172 rdev->mutex_owner = current;
173}
174
175static inline void regulator_lock(struct regulator_dev *rdev)
176{
177 regulator_lock_nested(rdev, 0);
178}
179
180/**
181 * regulator_unlock - unlock a single regulator
182 * @rdev: regulator_source
183 *
184 * This function unlocks the mutex when the
185 * reference counter reaches 0.
186 */
187static void regulator_unlock(struct regulator_dev *rdev)
188{
189 if (rdev->ref_cnt != 0) {
190 rdev->ref_cnt--;
191
192 if (!rdev->ref_cnt) {
193 rdev->mutex_owner = NULL;
194 mutex_unlock(&rdev->mutex);
195 }
196 }
197}
198
199/**
150 * regulator_lock_supply - lock a regulator and its supplies 200 * regulator_lock_supply - lock a regulator and its supplies
151 * @rdev: regulator source 201 * @rdev: regulator source
152 */ 202 */
@@ -155,7 +205,7 @@ static void regulator_lock_supply(struct regulator_dev *rdev)
155 int i; 205 int i;
156 206
157 for (i = 0; rdev; rdev = rdev_get_supply(rdev), i++) 207 for (i = 0; rdev; rdev = rdev_get_supply(rdev), i++)
158 mutex_lock_nested(&rdev->mutex, i); 208 regulator_lock_nested(rdev, i);
159} 209}
160 210
161/** 211/**
@@ -167,7 +217,7 @@ static void regulator_unlock_supply(struct regulator_dev *rdev)
167 struct regulator *supply; 217 struct regulator *supply;
168 218
169 while (1) { 219 while (1) {
170 mutex_unlock(&rdev->mutex); 220 regulator_unlock(rdev);
171 supply = rdev->supply; 221 supply = rdev->supply;
172 222
173 if (!rdev->supply) 223 if (!rdev->supply)
@@ -350,9 +400,9 @@ static ssize_t regulator_uV_show(struct device *dev,
350 struct regulator_dev *rdev = dev_get_drvdata(dev); 400 struct regulator_dev *rdev = dev_get_drvdata(dev);
351 ssize_t ret; 401 ssize_t ret;
352 402
353 mutex_lock(&rdev->mutex); 403 regulator_lock(rdev);
354 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev)); 404 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
355 mutex_unlock(&rdev->mutex); 405 regulator_unlock(rdev);
356 406
357 return ret; 407 return ret;
358} 408}
@@ -416,9 +466,9 @@ static ssize_t regulator_state_show(struct device *dev,
416 struct regulator_dev *rdev = dev_get_drvdata(dev); 466 struct regulator_dev *rdev = dev_get_drvdata(dev);
417 ssize_t ret; 467 ssize_t ret;
418 468
419 mutex_lock(&rdev->mutex); 469 regulator_lock(rdev);
420 ret = regulator_print_state(buf, _regulator_is_enabled(rdev)); 470 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
421 mutex_unlock(&rdev->mutex); 471 regulator_unlock(rdev);
422 472
423 return ret; 473 return ret;
424} 474}
@@ -526,10 +576,10 @@ static ssize_t regulator_total_uA_show(struct device *dev,
526 struct regulator *regulator; 576 struct regulator *regulator;
527 int uA = 0; 577 int uA = 0;
528 578
529 mutex_lock(&rdev->mutex); 579 regulator_lock(rdev);
530 list_for_each_entry(regulator, &rdev->consumer_list, list) 580 list_for_each_entry(regulator, &rdev->consumer_list, list)
531 uA += regulator->uA_load; 581 uA += regulator->uA_load;
532 mutex_unlock(&rdev->mutex); 582 regulator_unlock(rdev);
533 return sprintf(buf, "%d\n", uA); 583 return sprintf(buf, "%d\n", uA);
534} 584}
535static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL); 585static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
@@ -1333,7 +1383,7 @@ static struct regulator *create_regulator(struct regulator_dev *rdev,
1333 if (regulator == NULL) 1383 if (regulator == NULL)
1334 return NULL; 1384 return NULL;
1335 1385
1336 mutex_lock(&rdev->mutex); 1386 regulator_lock(rdev);
1337 regulator->rdev = rdev; 1387 regulator->rdev = rdev;
1338 list_add(&regulator->list, &rdev->consumer_list); 1388 list_add(&regulator->list, &rdev->consumer_list);
1339 1389
@@ -1388,12 +1438,12 @@ static struct regulator *create_regulator(struct regulator_dev *rdev,
1388 _regulator_is_enabled(rdev)) 1438 _regulator_is_enabled(rdev))
1389 regulator->always_on = true; 1439 regulator->always_on = true;
1390 1440
1391 mutex_unlock(&rdev->mutex); 1441 regulator_unlock(rdev);
1392 return regulator; 1442 return regulator;
1393overflow_err: 1443overflow_err:
1394 list_del(&regulator->list); 1444 list_del(&regulator->list);
1395 kfree(regulator); 1445 kfree(regulator);
1396 mutex_unlock(&rdev->mutex); 1446 regulator_unlock(rdev);
1397 return NULL; 1447 return NULL;
1398} 1448}
1399 1449
@@ -1782,13 +1832,13 @@ static void _regulator_put(struct regulator *regulator)
1782 /* remove any sysfs entries */ 1832 /* remove any sysfs entries */
1783 if (regulator->dev) 1833 if (regulator->dev)
1784 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name); 1834 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1785 mutex_lock(&rdev->mutex); 1835 regulator_lock(rdev);
1786 list_del(&regulator->list); 1836 list_del(&regulator->list);
1787 1837
1788 rdev->open_count--; 1838 rdev->open_count--;
1789 rdev->exclusive = 0; 1839 rdev->exclusive = 0;
1790 put_device(&rdev->dev); 1840 put_device(&rdev->dev);
1791 mutex_unlock(&rdev->mutex); 1841 regulator_unlock(rdev);
1792 1842
1793 kfree_const(regulator->supply_name); 1843 kfree_const(regulator->supply_name);
1794 kfree(regulator); 1844 kfree(regulator);
@@ -2396,7 +2446,7 @@ static void regulator_disable_work(struct work_struct *work)
2396 disable_work.work); 2446 disable_work.work);
2397 int count, i, ret; 2447 int count, i, ret;
2398 2448
2399 mutex_lock(&rdev->mutex); 2449 regulator_lock(rdev);
2400 2450
2401 BUG_ON(!rdev->deferred_disables); 2451 BUG_ON(!rdev->deferred_disables);
2402 2452
@@ -2417,7 +2467,7 @@ static void regulator_disable_work(struct work_struct *work)
2417 rdev_err(rdev, "Deferred disable failed: %d\n", ret); 2467 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2418 } 2468 }
2419 2469
2420 mutex_unlock(&rdev->mutex); 2470 regulator_unlock(rdev);
2421 2471
2422 if (rdev->supply) { 2472 if (rdev->supply) {
2423 for (i = 0; i < count; i++) { 2473 for (i = 0; i < count; i++) {
@@ -2452,11 +2502,11 @@ int regulator_disable_deferred(struct regulator *regulator, int ms)
2452 if (!ms) 2502 if (!ms)
2453 return regulator_disable(regulator); 2503 return regulator_disable(regulator);
2454 2504
2455 mutex_lock(&rdev->mutex); 2505 regulator_lock(rdev);
2456 rdev->deferred_disables++; 2506 rdev->deferred_disables++;
2457 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work, 2507 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2458 msecs_to_jiffies(ms)); 2508 msecs_to_jiffies(ms));
2459 mutex_unlock(&rdev->mutex); 2509 regulator_unlock(rdev);
2460 2510
2461 return 0; 2511 return 0;
2462} 2512}
@@ -2488,10 +2538,10 @@ static int _regulator_list_voltage(struct regulator_dev *rdev,
2488 if (selector >= rdev->desc->n_voltages) 2538 if (selector >= rdev->desc->n_voltages)
2489 return -EINVAL; 2539 return -EINVAL;
2490 if (lock) 2540 if (lock)
2491 mutex_lock(&rdev->mutex); 2541 regulator_lock(rdev);
2492 ret = ops->list_voltage(rdev, selector); 2542 ret = ops->list_voltage(rdev, selector);
2493 if (lock) 2543 if (lock)
2494 mutex_unlock(&rdev->mutex); 2544 regulator_unlock(rdev);
2495 } else if (rdev->is_switch && rdev->supply) { 2545 } else if (rdev->is_switch && rdev->supply) {
2496 ret = _regulator_list_voltage(rdev->supply->rdev, 2546 ret = _regulator_list_voltage(rdev->supply->rdev,
2497 selector, lock); 2547 selector, lock);
@@ -3264,7 +3314,7 @@ int regulator_sync_voltage(struct regulator *regulator)
3264 struct regulator_voltage *voltage = &regulator->voltage[PM_SUSPEND_ON]; 3314 struct regulator_voltage *voltage = &regulator->voltage[PM_SUSPEND_ON];
3265 int ret, min_uV, max_uV; 3315 int ret, min_uV, max_uV;
3266 3316
3267 mutex_lock(&rdev->mutex); 3317 regulator_lock(rdev);
3268 3318
3269 if (!rdev->desc->ops->set_voltage && 3319 if (!rdev->desc->ops->set_voltage &&
3270 !rdev->desc->ops->set_voltage_sel) { 3320 !rdev->desc->ops->set_voltage_sel) {
@@ -3293,7 +3343,7 @@ int regulator_sync_voltage(struct regulator *regulator)
3293 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV); 3343 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3294 3344
3295out: 3345out:
3296 mutex_unlock(&rdev->mutex); 3346 regulator_unlock(rdev);
3297 return ret; 3347 return ret;
3298} 3348}
3299EXPORT_SYMBOL_GPL(regulator_sync_voltage); 3349EXPORT_SYMBOL_GPL(regulator_sync_voltage);
@@ -3386,7 +3436,7 @@ int regulator_set_current_limit(struct regulator *regulator,
3386 struct regulator_dev *rdev = regulator->rdev; 3436 struct regulator_dev *rdev = regulator->rdev;
3387 int ret; 3437 int ret;
3388 3438
3389 mutex_lock(&rdev->mutex); 3439 regulator_lock(rdev);
3390 3440
3391 /* sanity check */ 3441 /* sanity check */
3392 if (!rdev->desc->ops->set_current_limit) { 3442 if (!rdev->desc->ops->set_current_limit) {
@@ -3401,7 +3451,7 @@ int regulator_set_current_limit(struct regulator *regulator,
3401 3451
3402 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA); 3452 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
3403out: 3453out:
3404 mutex_unlock(&rdev->mutex); 3454 regulator_unlock(rdev);
3405 return ret; 3455 return ret;
3406} 3456}
3407EXPORT_SYMBOL_GPL(regulator_set_current_limit); 3457EXPORT_SYMBOL_GPL(regulator_set_current_limit);
@@ -3410,7 +3460,7 @@ static int _regulator_get_current_limit(struct regulator_dev *rdev)
3410{ 3460{
3411 int ret; 3461 int ret;
3412 3462
3413 mutex_lock(&rdev->mutex); 3463 regulator_lock(rdev);
3414 3464
3415 /* sanity check */ 3465 /* sanity check */
3416 if (!rdev->desc->ops->get_current_limit) { 3466 if (!rdev->desc->ops->get_current_limit) {
@@ -3420,7 +3470,7 @@ static int _regulator_get_current_limit(struct regulator_dev *rdev)
3420 3470
3421 ret = rdev->desc->ops->get_current_limit(rdev); 3471 ret = rdev->desc->ops->get_current_limit(rdev);
3422out: 3472out:
3423 mutex_unlock(&rdev->mutex); 3473 regulator_unlock(rdev);
3424 return ret; 3474 return ret;
3425} 3475}
3426 3476
@@ -3456,7 +3506,7 @@ int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3456 int ret; 3506 int ret;
3457 int regulator_curr_mode; 3507 int regulator_curr_mode;
3458 3508
3459 mutex_lock(&rdev->mutex); 3509 regulator_lock(rdev);
3460 3510
3461 /* sanity check */ 3511 /* sanity check */
3462 if (!rdev->desc->ops->set_mode) { 3512 if (!rdev->desc->ops->set_mode) {
@@ -3480,7 +3530,7 @@ int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3480 3530
3481 ret = rdev->desc->ops->set_mode(rdev, mode); 3531 ret = rdev->desc->ops->set_mode(rdev, mode);
3482out: 3532out:
3483 mutex_unlock(&rdev->mutex); 3533 regulator_unlock(rdev);
3484 return ret; 3534 return ret;
3485} 3535}
3486EXPORT_SYMBOL_GPL(regulator_set_mode); 3536EXPORT_SYMBOL_GPL(regulator_set_mode);
@@ -3489,7 +3539,7 @@ static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3489{ 3539{
3490 int ret; 3540 int ret;
3491 3541
3492 mutex_lock(&rdev->mutex); 3542 regulator_lock(rdev);
3493 3543
3494 /* sanity check */ 3544 /* sanity check */
3495 if (!rdev->desc->ops->get_mode) { 3545 if (!rdev->desc->ops->get_mode) {
@@ -3499,7 +3549,7 @@ static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3499 3549
3500 ret = rdev->desc->ops->get_mode(rdev); 3550 ret = rdev->desc->ops->get_mode(rdev);
3501out: 3551out:
3502 mutex_unlock(&rdev->mutex); 3552 regulator_unlock(rdev);
3503 return ret; 3553 return ret;
3504} 3554}
3505 3555
@@ -3520,7 +3570,7 @@ static int _regulator_get_error_flags(struct regulator_dev *rdev,
3520{ 3570{
3521 int ret; 3571 int ret;
3522 3572
3523 mutex_lock(&rdev->mutex); 3573 regulator_lock(rdev);
3524 3574
3525 /* sanity check */ 3575 /* sanity check */
3526 if (!rdev->desc->ops->get_error_flags) { 3576 if (!rdev->desc->ops->get_error_flags) {
@@ -3530,7 +3580,7 @@ static int _regulator_get_error_flags(struct regulator_dev *rdev,
3530 3580
3531 ret = rdev->desc->ops->get_error_flags(rdev, flags); 3581 ret = rdev->desc->ops->get_error_flags(rdev, flags);
3532out: 3582out:
3533 mutex_unlock(&rdev->mutex); 3583 regulator_unlock(rdev);
3534 return ret; 3584 return ret;
3535} 3585}
3536 3586
@@ -3579,10 +3629,10 @@ int regulator_set_load(struct regulator *regulator, int uA_load)
3579 struct regulator_dev *rdev = regulator->rdev; 3629 struct regulator_dev *rdev = regulator->rdev;
3580 int ret; 3630 int ret;
3581 3631
3582 mutex_lock(&rdev->mutex); 3632 regulator_lock(rdev);
3583 regulator->uA_load = uA_load; 3633 regulator->uA_load = uA_load;
3584 ret = drms_uA_update(rdev); 3634 ret = drms_uA_update(rdev);
3585 mutex_unlock(&rdev->mutex); 3635 regulator_unlock(rdev);
3586 3636
3587 return ret; 3637 return ret;
3588} 3638}
@@ -3610,7 +3660,7 @@ int regulator_allow_bypass(struct regulator *regulator, bool enable)
3610 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS)) 3660 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
3611 return 0; 3661 return 0;
3612 3662
3613 mutex_lock(&rdev->mutex); 3663 regulator_lock(rdev);
3614 3664
3615 if (enable && !regulator->bypass) { 3665 if (enable && !regulator->bypass) {
3616 rdev->bypass_count++; 3666 rdev->bypass_count++;
@@ -3634,7 +3684,7 @@ int regulator_allow_bypass(struct regulator *regulator, bool enable)
3634 if (ret == 0) 3684 if (ret == 0)
3635 regulator->bypass = enable; 3685 regulator->bypass = enable;
3636 3686
3637 mutex_unlock(&rdev->mutex); 3687 regulator_unlock(rdev);
3638 3688
3639 return ret; 3689 return ret;
3640} 3690}
@@ -4300,9 +4350,9 @@ static int _regulator_suspend_late(struct device *dev, void *data)
4300 suspend_state_t *state = data; 4350 suspend_state_t *state = data;
4301 int ret; 4351 int ret;
4302 4352
4303 mutex_lock(&rdev->mutex); 4353 regulator_lock(rdev);
4304 ret = suspend_set_state(rdev, *state); 4354 ret = suspend_set_state(rdev, *state);
4305 mutex_unlock(&rdev->mutex); 4355 regulator_unlock(rdev);
4306 4356
4307 return ret; 4357 return ret;
4308} 4358}
@@ -4332,14 +4382,14 @@ static int _regulator_resume_early(struct device *dev, void *data)
4332 if (rstate == NULL) 4382 if (rstate == NULL)
4333 return 0; 4383 return 0;
4334 4384
4335 mutex_lock(&rdev->mutex); 4385 regulator_lock(rdev);
4336 4386
4337 if (rdev->desc->ops->resume_early && 4387 if (rdev->desc->ops->resume_early &&
4338 (rstate->enabled == ENABLE_IN_SUSPEND || 4388 (rstate->enabled == ENABLE_IN_SUSPEND ||
4339 rstate->enabled == DISABLE_IN_SUSPEND)) 4389 rstate->enabled == DISABLE_IN_SUSPEND))
4340 ret = rdev->desc->ops->resume_early(rdev); 4390 ret = rdev->desc->ops->resume_early(rdev);
4341 4391
4342 mutex_unlock(&rdev->mutex); 4392 regulator_unlock(rdev);
4343 4393
4344 return ret; 4394 return ret;
4345} 4395}
@@ -4641,7 +4691,7 @@ static int __init regulator_late_cleanup(struct device *dev, void *data)
4641 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) 4691 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
4642 return 0; 4692 return 0;
4643 4693
4644 mutex_lock(&rdev->mutex); 4694 regulator_lock(rdev);
4645 4695
4646 if (rdev->use_count) 4696 if (rdev->use_count)
4647 goto unlock; 4697 goto unlock;
@@ -4672,7 +4722,7 @@ static int __init regulator_late_cleanup(struct device *dev, void *data)
4672 } 4722 }
4673 4723
4674unlock: 4724unlock:
4675 mutex_unlock(&rdev->mutex); 4725 regulator_unlock(rdev);
4676 4726
4677 return 0; 4727 return 0;
4678} 4728}
diff --git a/include/linux/regulator/driver.h b/include/linux/regulator/driver.h
index 14e512ad6d4f..c2a181fa7287 100644
--- a/include/linux/regulator/driver.h
+++ b/include/linux/regulator/driver.h
@@ -434,6 +434,8 @@ struct regulator_dev {
434 434
435 struct blocking_notifier_head notifier; 435 struct blocking_notifier_head notifier;
436 struct mutex mutex; /* consumer lock */ 436 struct mutex mutex; /* consumer lock */
437 struct task_struct *mutex_owner;
438 int ref_cnt;
437 struct module *owner; 439 struct module *owner;
438 struct device dev; 440 struct device dev;
439 struct regulation_constraints *constraints; 441 struct regulation_constraints *constraints;