Skip to content

Commit 467bf30

Browse files
osctobebroonie
authored andcommitted
regulator: push allocation in regulator_ena_gpio_request() out of lock
Move another allocation out of regulator_list_mutex-protected region, as reclaim might want to take the same lock. WARNING: possible circular locking dependency detected 5.7.13+ #534 Not tainted ------------------------------------------------------ kswapd0/383 is trying to acquire lock: c0e5d920 (regulator_list_mutex){+.+.}-{3:3}, at: regulator_lock_dependent+0x54/0x2c0 but task is already holding lock: c0e38518 (fs_reclaim){+.+.}-{0:0}, at: __fs_reclaim_acquire+0x0/0x50 which lock already depends on the new lock. the existing dependency chain (in reverse order) is: -> #1 (fs_reclaim){+.+.}-{0:0}: fs_reclaim_acquire.part.11+0x40/0x50 fs_reclaim_acquire+0x24/0x28 kmem_cache_alloc_trace+0x40/0x1e8 regulator_register+0x384/0x1630 devm_regulator_register+0x50/0x84 reg_fixed_voltage_probe+0x248/0x35c [...] other info that might help us debug this: Possible unsafe locking scenario: CPU0 CPU1 ---- ---- lock(fs_reclaim); lock(regulator_list_mutex); lock(fs_reclaim); lock(regulator_list_mutex); *** DEADLOCK *** [...] 2 locks held by kswapd0/383: #0: c0e38518 (fs_reclaim){+.+.}-{0:0}, at: __fs_reclaim_acquire+0x0/0x50 #1: cb70e5e0 (hctx->srcu){....}-{0:0}, at: hctx_lock+0x60/0xb8 [...] Fixes: 541d052 ("regulator: core: Only support passing enable GPIO descriptors") [this commit only changes context] Fixes: f8702f9 ("regulator: core: Use ww_mutex for regulators locking") [this is when the regulator_list_mutex was introduced in reclaim locking path] Signed-off-by: Michał Mirosław <mirq-linux@rere.qmqm.pl> Link: https://lore.kernel.org/r/41fe6a9670335721b48e8f5195038c3d67a3bf92.1597195321.git.mirq-linux@rere.qmqm.pl Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 73a3212 commit 467bf30

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

Diff for: drivers/regulator/core.c

+14-5
Original file line numberDiff line numberDiff line change
@@ -2230,10 +2230,13 @@ EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
22302230
static int regulator_ena_gpio_request(struct regulator_dev *rdev,
22312231
const struct regulator_config *config)
22322232
{
2233-
struct regulator_enable_gpio *pin;
2233+
struct regulator_enable_gpio *pin, *new_pin;
22342234
struct gpio_desc *gpiod;
22352235

22362236
gpiod = config->ena_gpiod;
2237+
new_pin = kzalloc(sizeof(*new_pin), GFP_KERNEL);
2238+
2239+
mutex_lock(&regulator_list_mutex);
22372240

22382241
list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
22392242
if (pin->gpiod == gpiod) {
@@ -2242,16 +2245,24 @@ static int regulator_ena_gpio_request(struct regulator_dev *rdev,
22422245
}
22432246
}
22442247

2245-
pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
2246-
if (pin == NULL)
2248+
if (new_pin == NULL) {
2249+
mutex_unlock(&regulator_list_mutex);
22472250
return -ENOMEM;
2251+
}
2252+
2253+
pin = new_pin;
2254+
new_pin = NULL;
22482255

22492256
pin->gpiod = gpiod;
22502257
list_add(&pin->list, &regulator_ena_gpio_list);
22512258

22522259
update_ena_gpio_to_rdev:
22532260
pin->request_count++;
22542261
rdev->ena_pin = pin;
2262+
2263+
mutex_unlock(&regulator_list_mutex);
2264+
kfree(new_pin);
2265+
22552266
return 0;
22562267
}
22572268

@@ -5209,9 +5220,7 @@ regulator_register(const struct regulator_desc *regulator_desc,
52095220
}
52105221

52115222
if (config->ena_gpiod) {
5212-
mutex_lock(&regulator_list_mutex);
52135223
ret = regulator_ena_gpio_request(rdev, config);
5214-
mutex_unlock(&regulator_list_mutex);
52155224
if (ret != 0) {
52165225
rdev_err(rdev, "Failed to request enable GPIO: %d\n",
52175226
ret);

0 commit comments

Comments
 (0)