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

regulator: vctrl: Avoid lockdep warning in enable/disable ops

vctrl_enable() and vctrl_disable() call regulator_enable() and
regulator_disable(), respectively. However, vctrl_* are regulator ops
and should not be calling the locked regulator APIs. Doing so results in
a lockdep warning.

Instead of exporting more internal regulator ops, model the ctrl supply
as an actual supply to vctrl-regulator. At probe time this driver still
needs to use the consumer API to fetch its constraints, but otherwise
lets the regulator core handle the upstream supply for it.

The enable/disable/is_enabled ops are not removed, but now only track
state internally. This preserves the original behavior with the ops
being available, but one could argue that the original behavior was
already incorrect: the internal state would not match the upstream
supply if that supply had another consumer that enabled the supply,
while vctrl-regulator was not enabled.

The lockdep warning is as follows:

WARNING: possible circular locking dependency detected
5.14.0-rc6 #2 Not tainted
------------------------------------------------------
swapper/0/1 is trying to acquire lock:
ffffffc011306d00 (regulator_list_mutex){+.+.}-{3:3}, at:
regulator_lock_dependent (arch/arm64/include/asm/current.h:19
include/linux/ww_mutex.h:111
drivers/regulator/core.c:329)

but task is already holding lock:
ffffff8004a77160 (regulator_ww_class_mutex){+.+.}-{3:3}, at:
regulator_lock_recursive (drivers/regulator/core.c:156
drivers/regulator/core.c:263)

which lock already depends on the new lock.

the existing dependency chain (in reverse order) is:

-> #2 (regulator_ww_class_mutex){+.+.}-{3:3}:
__mutex_lock_common (include/asm-generic/atomic-instrumented.h:606
include/asm-generic/atomic-long.h:29
kernel/locking/mutex.c:103
kernel/locking/mutex.c:144
kernel/locking/mutex.c:963)
ww_mutex_lock (kernel/locking/mutex.c:1199)
regulator_lock_recursive (drivers/regulator/core.c:156
drivers/regulator/core.c:263)
regulator_lock_dependent (drivers/regulator/core.c:343)
regulator_enable (drivers/regulator/core.c:2808)
set_machine_constraints (drivers/regulator/core.c:1536)
regulator_register (drivers/regulator/core.c:5486)
devm_regulator_register (drivers/regulator/devres.c:196)
reg_fixed_voltage_probe (drivers/regulator/fixed.c:289)
platform_probe (drivers/base/platform.c:1427)
[...]

-> #1 (regulator_ww_class_acquire){+.+.}-{0:0}:
regulator_lock_dependent (include/linux/ww_mutex.h:129
drivers/regulator/core.c:329)
regulator_enable (drivers/regulator/core.c:2808)
set_machine_constraints (drivers/regulator/core.c:1536)
regulator_register (drivers/regulator/core.c:5486)
devm_regulator_register (drivers/regulator/devres.c:196)
reg_fixed_voltage_probe (drivers/regulator/fixed.c:289)
[...]

-> #0 (regulator_list_mutex){+.+.}-{3:3}:
__lock_acquire (kernel/locking/lockdep.c:3052 (discriminator 4)
kernel/locking/lockdep.c:3174 (discriminator 4)
kernel/locking/lockdep.c:3789 (discriminator 4)
kernel/locking/lockdep.c:5015 (discriminator 4))
lock_acquire (arch/arm64/include/asm/percpu.h:39
kernel/locking/lockdep.c:438
kernel/locking/lockdep.c:5627)
__mutex_lock_common (include/asm-generic/atomic-instrumented.h:606
include/asm-generic/atomic-long.h:29
kernel/locking/mutex.c:103
kernel/locking/mutex.c:144
kernel/locking/mutex.c:963)
mutex_lock_nested (kernel/locking/mutex.c:1125)
regulator_lock_dependent (arch/arm64/include/asm/current.h:19
include/linux/ww_mutex.h:111
drivers/regulator/core.c:329)
regulator_enable (drivers/regulator/core.c:2808)
vctrl_enable (drivers/regulator/vctrl-regulator.c:400)
_regulator_do_enable (drivers/regulator/core.c:2617)
_regulator_enable (drivers/regulator/core.c:2764)
regulator_enable (drivers/regulator/core.c:308
drivers/regulator/core.c:2809)
_set_opp (drivers/opp/core.c:819 drivers/opp/core.c:1072)
dev_pm_opp_set_rate (drivers/opp/core.c:1164)
set_target (drivers/cpufreq/cpufreq-dt.c:62)
__cpufreq_driver_target (drivers/cpufreq/cpufreq.c:2216
drivers/cpufreq/cpufreq.c:2271)
cpufreq_online (drivers/cpufreq/cpufreq.c:1488 (discriminator 2))
cpufreq_add_dev (drivers/cpufreq/cpufreq.c:1563)
subsys_interface_register (drivers/base/bus.c:?)
cpufreq_register_driver (drivers/cpufreq/cpufreq.c:2819)
dt_cpufreq_probe (drivers/cpufreq/cpufreq-dt.c:344)
[...]

other info that might help us debug this:

Chain exists of:
regulator_list_mutex --> regulator_ww_class_acquire --> regulator_ww_class_mutex

Possible unsafe locking scenario:

CPU0 CPU1
---- ----
lock(regulator_ww_class_mutex);
lock(regulator_ww_class_acquire);
lock(regulator_ww_class_mutex);
lock(regulator_list_mutex);

*** DEADLOCK ***

6 locks held by swapper/0/1:
#0: ffffff8002d32188 (&dev->mutex){....}-{3:3}, at:
__device_driver_lock (drivers/base/dd.c:1030)
#1: ffffffc0111a0520 (cpu_hotplug_lock){++++}-{0:0}, at:
cpufreq_register_driver (drivers/cpufreq/cpufreq.c:2792 (discriminator 2))
#2: ffffff8002a8d918 (subsys mutex#9){+.+.}-{3:3}, at:
subsys_interface_register (drivers/base/bus.c:1033)
#3: ffffff800341bb90 (&policy->rwsem){+.+.}-{3:3}, at:
cpufreq_online (include/linux/bitmap.h:285
include/linux/cpumask.h:405
drivers/cpufreq/cpufreq.c:1399)
#4: ffffffc011f0b7b8 (regulator_ww_class_acquire){+.+.}-{0:0}, at:
regulator_enable (drivers/regulator/core.c:2808)
#5: ffffff8004a77160 (regulator_ww_class_mutex){+.+.}-{3:3}, at:
regulator_lock_recursive (drivers/regulator/core.c:156
drivers/regulator/core.c:263)

stack backtrace:
CPU: 1 PID: 1 Comm: swapper/0 Not tainted 5.14.0-rc6 #2 7c8f8996d021ed0f65271e6aeebf7999de74a9fa
Hardware name: Google Scarlet (DT)
Call trace:
dump_backtrace (arch/arm64/kernel/stacktrace.c:161)
show_stack (arch/arm64/kernel/stacktrace.c:218)
dump_stack_lvl (lib/dump_stack.c:106 (discriminator 2))
dump_stack (lib/dump_stack.c:113)
print_circular_bug (kernel/locking/lockdep.c:?)
check_noncircular (kernel/locking/lockdep.c:?)
__lock_acquire (kernel/locking/lockdep.c:3052 (discriminator 4)
kernel/locking/lockdep.c:3174 (discriminator 4)
kernel/locking/lockdep.c:3789 (discriminator 4)
kernel/locking/lockdep.c:5015 (discriminator 4))
lock_acquire (arch/arm64/include/asm/percpu.h:39
kernel/locking/lockdep.c:438
kernel/locking/lockdep.c:5627)
__mutex_lock_common (include/asm-generic/atomic-instrumented.h:606
include/asm-generic/atomic-long.h:29
kernel/locking/mutex.c:103
kernel/locking/mutex.c:144
kernel/locking/mutex.c:963)
mutex_lock_nested (kernel/locking/mutex.c:1125)
regulator_lock_dependent (arch/arm64/include/asm/current.h:19
include/linux/ww_mutex.h:111
drivers/regulator/core.c:329)
regulator_enable (drivers/regulator/core.c:2808)
vctrl_enable (drivers/regulator/vctrl-regulator.c:400)
_regulator_do_enable (drivers/regulator/core.c:2617)
_regulator_enable (drivers/regulator/core.c:2764)
regulator_enable (drivers/regulator/core.c:308
drivers/regulator/core.c:2809)
_set_opp (drivers/opp/core.c:819 drivers/opp/core.c:1072)
dev_pm_opp_set_rate (drivers/opp/core.c:1164)
set_target (drivers/cpufreq/cpufreq-dt.c:62)
__cpufreq_driver_target (drivers/cpufreq/cpufreq.c:2216
drivers/cpufreq/cpufreq.c:2271)
cpufreq_online (drivers/cpufreq/cpufreq.c:1488 (discriminator 2))
cpufreq_add_dev (drivers/cpufreq/cpufreq.c:1563)
subsys_interface_register (drivers/base/bus.c:?)
cpufreq_register_driver (drivers/cpufreq/cpufreq.c:2819)
dt_cpufreq_probe (drivers/cpufreq/cpufreq-dt.c:344)
[...]

Reported-by: Brian Norris <briannorris@chromium.org>
Fixes: f8702f9e4aa7 ("regulator: core: Use ww_mutex for regulators locking")
Fixes: e9153311491d ("regulator: vctrl-regulator: Avoid deadlock getting and setting the voltage")
Signed-off-by: Chen-Yu Tsai <wenst@chromium.org>
Link: https://lore.kernel.org/r/20210825033704.3307263-3-wenst@chromium.org
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Chen-Yu Tsai and committed by
Mark Brown
21e39809 98e47570

+42 -30
+42 -30
drivers/regulator/vctrl-regulator.c
··· 37 37 struct vctrl_data { 38 38 struct regulator_dev *rdev; 39 39 struct regulator_desc desc; 40 - struct regulator *ctrl_reg; 41 40 bool enabled; 42 41 unsigned int min_slew_down_rate; 43 42 unsigned int ovp_threshold; ··· 81 82 static int vctrl_get_voltage(struct regulator_dev *rdev) 82 83 { 83 84 struct vctrl_data *vctrl = rdev_get_drvdata(rdev); 84 - int ctrl_uV = regulator_get_voltage_rdev(vctrl->ctrl_reg->rdev); 85 + int ctrl_uV; 86 + 87 + if (!rdev->supply) 88 + return -EPROBE_DEFER; 89 + 90 + ctrl_uV = regulator_get_voltage_rdev(rdev->supply->rdev); 85 91 86 92 return vctrl_calc_output_voltage(vctrl, ctrl_uV); 87 93 } ··· 96 92 unsigned int *selector) 97 93 { 98 94 struct vctrl_data *vctrl = rdev_get_drvdata(rdev); 99 - struct regulator *ctrl_reg = vctrl->ctrl_reg; 100 - int orig_ctrl_uV = regulator_get_voltage_rdev(ctrl_reg->rdev); 101 - int uV = vctrl_calc_output_voltage(vctrl, orig_ctrl_uV); 95 + int orig_ctrl_uV; 96 + int uV; 102 97 int ret; 98 + 99 + if (!rdev->supply) 100 + return -EPROBE_DEFER; 101 + 102 + orig_ctrl_uV = regulator_get_voltage_rdev(rdev->supply->rdev); 103 + uV = vctrl_calc_output_voltage(vctrl, orig_ctrl_uV); 103 104 104 105 if (req_min_uV >= uV || !vctrl->ovp_threshold) 105 106 /* voltage rising or no OVP */ 106 - return regulator_set_voltage_rdev(ctrl_reg->rdev, 107 + return regulator_set_voltage_rdev(rdev->supply->rdev, 107 108 vctrl_calc_ctrl_voltage(vctrl, req_min_uV), 108 109 vctrl_calc_ctrl_voltage(vctrl, req_max_uV), 109 110 PM_SUSPEND_ON); ··· 126 117 next_uV = max_t(int, req_min_uV, uV - max_drop_uV); 127 118 next_ctrl_uV = vctrl_calc_ctrl_voltage(vctrl, next_uV); 128 119 129 - ret = regulator_set_voltage_rdev(ctrl_reg->rdev, 120 + ret = regulator_set_voltage_rdev(rdev->supply->rdev, 130 121 next_ctrl_uV, 131 122 next_ctrl_uV, 132 123 PM_SUSPEND_ON); ··· 143 134 144 135 err: 145 136 /* Try to go back to original voltage */ 146 - regulator_set_voltage_rdev(ctrl_reg->rdev, orig_ctrl_uV, orig_ctrl_uV, 137 + regulator_set_voltage_rdev(rdev->supply->rdev, orig_ctrl_uV, orig_ctrl_uV, 147 138 PM_SUSPEND_ON); 148 139 149 140 return ret; ··· 160 151 unsigned int selector) 161 152 { 162 153 struct vctrl_data *vctrl = rdev_get_drvdata(rdev); 163 - struct regulator *ctrl_reg = vctrl->ctrl_reg; 164 154 unsigned int orig_sel = vctrl->sel; 165 155 int ret; 156 + 157 + if (!rdev->supply) 158 + return -EPROBE_DEFER; 166 159 167 160 if (selector >= rdev->desc->n_voltages) 168 161 return -EINVAL; 169 162 170 163 if (selector >= vctrl->sel || !vctrl->ovp_threshold) { 171 164 /* voltage rising or no OVP */ 172 - ret = regulator_set_voltage_rdev(ctrl_reg->rdev, 165 + ret = regulator_set_voltage_rdev(rdev->supply->rdev, 173 166 vctrl->vtable[selector].ctrl, 174 167 vctrl->vtable[selector].ctrl, 175 168 PM_SUSPEND_ON); ··· 190 179 else 191 180 next_sel = vctrl->vtable[vctrl->sel].ovp_min_sel; 192 181 193 - ret = regulator_set_voltage_rdev(ctrl_reg->rdev, 182 + ret = regulator_set_voltage_rdev(rdev->supply->rdev, 194 183 vctrl->vtable[next_sel].ctrl, 195 184 vctrl->vtable[next_sel].ctrl, 196 185 PM_SUSPEND_ON); ··· 213 202 err: 214 203 if (vctrl->sel != orig_sel) { 215 204 /* Try to go back to original voltage */ 216 - if (!regulator_set_voltage_rdev(ctrl_reg->rdev, 205 + if (!regulator_set_voltage_rdev(rdev->supply->rdev, 217 206 vctrl->vtable[orig_sel].ctrl, 218 207 vctrl->vtable[orig_sel].ctrl, 219 208 PM_SUSPEND_ON)) ··· 244 233 struct device_node *np = pdev->dev.of_node; 245 234 u32 pval; 246 235 u32 vrange_ctrl[2]; 247 - 248 - vctrl->ctrl_reg = devm_regulator_get(&pdev->dev, "ctrl"); 249 - if (IS_ERR(vctrl->ctrl_reg)) 250 - return PTR_ERR(vctrl->ctrl_reg); 251 236 252 237 ret = of_property_read_u32(np, "ovp-threshold-percent", &pval); 253 238 if (!ret) { ··· 322 315 return at->ctrl - bt->ctrl; 323 316 } 324 317 325 - static int vctrl_init_vtable(struct platform_device *pdev) 318 + static int vctrl_init_vtable(struct platform_device *pdev, 319 + struct regulator *ctrl_reg) 326 320 { 327 321 struct vctrl_data *vctrl = platform_get_drvdata(pdev); 328 322 struct regulator_desc *rdesc = &vctrl->desc; 329 - struct regulator *ctrl_reg = vctrl->ctrl_reg; 330 323 struct vctrl_voltage_range *vrange_ctrl = &vctrl->vrange.ctrl; 331 324 int n_voltages; 332 325 int ctrl_uV; ··· 402 395 static int vctrl_enable(struct regulator_dev *rdev) 403 396 { 404 397 struct vctrl_data *vctrl = rdev_get_drvdata(rdev); 405 - int ret = regulator_enable(vctrl->ctrl_reg); 406 398 407 - if (!ret) 408 - vctrl->enabled = true; 399 + vctrl->enabled = true; 409 400 410 - return ret; 401 + return 0; 411 402 } 412 403 413 404 static int vctrl_disable(struct regulator_dev *rdev) 414 405 { 415 406 struct vctrl_data *vctrl = rdev_get_drvdata(rdev); 416 - int ret = regulator_disable(vctrl->ctrl_reg); 417 407 418 - if (!ret) 419 - vctrl->enabled = false; 408 + vctrl->enabled = false; 420 409 421 - return ret; 410 + return 0; 422 411 } 423 412 424 413 static int vctrl_is_enabled(struct regulator_dev *rdev) ··· 450 447 struct regulator_desc *rdesc; 451 448 struct regulator_config cfg = { }; 452 449 struct vctrl_voltage_range *vrange_ctrl; 450 + struct regulator *ctrl_reg; 453 451 int ctrl_uV; 454 452 int ret; 455 453 ··· 465 461 if (ret) 466 462 return ret; 467 463 464 + ctrl_reg = devm_regulator_get(&pdev->dev, "ctrl"); 465 + if (IS_ERR(ctrl_reg)) 466 + return PTR_ERR(ctrl_reg); 467 + 468 468 vrange_ctrl = &vctrl->vrange.ctrl; 469 469 470 470 rdesc = &vctrl->desc; 471 471 rdesc->name = "vctrl"; 472 472 rdesc->type = REGULATOR_VOLTAGE; 473 473 rdesc->owner = THIS_MODULE; 474 + rdesc->supply_name = "ctrl"; 474 475 475 - if ((regulator_get_linear_step(vctrl->ctrl_reg) == 1) || 476 - (regulator_count_voltages(vctrl->ctrl_reg) == -EINVAL)) { 476 + if ((regulator_get_linear_step(ctrl_reg) == 1) || 477 + (regulator_count_voltages(ctrl_reg) == -EINVAL)) { 477 478 rdesc->continuous_voltage_range = true; 478 479 rdesc->ops = &vctrl_ops_cont; 479 480 } else { ··· 495 486 cfg.init_data = init_data; 496 487 497 488 if (!rdesc->continuous_voltage_range) { 498 - ret = vctrl_init_vtable(pdev); 489 + ret = vctrl_init_vtable(pdev, ctrl_reg); 499 490 if (ret) 500 491 return ret; 501 492 502 493 /* Use locked consumer API when not in regulator framework */ 503 - ctrl_uV = regulator_get_voltage(vctrl->ctrl_reg); 494 + ctrl_uV = regulator_get_voltage(ctrl_reg); 504 495 if (ctrl_uV < 0) { 505 496 dev_err(&pdev->dev, "failed to get control voltage\n"); 506 497 return ctrl_uV; ··· 522 513 } 523 514 } 524 515 } 516 + 517 + /* Drop ctrl-supply here in favor of regulator core managed supply */ 518 + devm_regulator_put(ctrl_reg); 525 519 526 520 vctrl->rdev = devm_regulator_register(&pdev->dev, rdesc, &cfg); 527 521 if (IS_ERR(vctrl->rdev)) {