]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
regulator: core: Avoid potential deadlock on regulator_unregister
authorCharles Keepax <ckeepax@opensource.cirrus.com>
Thu, 4 Apr 2019 15:32:18 +0000 (16:32 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 31 May 2019 13:43:37 +0000 (06:43 -0700)
[ Upstream commit 063773011d33bb36588a90385aa9eb75d13c6d80 ]

Lockdep reports the following issue on my setup:

Possible unsafe locking scenario:

CPU0                    CPU1
----                    ----
lock((work_completion)(&(&rdev->disable_work)->work));
                        lock(regulator_list_mutex);
                        lock((work_completion)(&(&rdev->disable_work)->work));
lock(regulator_list_mutex);

The problem is that regulator_unregister takes the
regulator_list_mutex and then calls flush_work on disable_work. But
regulator_disable_work calls regulator_lock_dependent which will
also take the regulator_list_mutex. Resulting in a deadlock if the
flush_work call actually needs to flush the work.

Fix this issue by moving the flush_work outside of the
regulator_list_mutex. The list mutex is not used to guard the point at
which the delayed work is queued, so its use adds no additional safety.

Fixes: f8702f9e4aa7 ("regulator: core: Use ww_mutex for regulators locking")
Signed-off-by: Charles Keepax <ckeepax@opensource.cirrus.com>
Reviewed-by: Dmitry Osipenko <digetx@gmail.com>
Signed-off-by: Mark Brown <broonie@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/regulator/core.c

index 6da41207e479aff0f317b216f592e9de3c78c3b4..35a7d020afecdcb093e9db4d653dc8a9a7b8b87c 100644 (file)
@@ -5062,10 +5062,11 @@ void regulator_unregister(struct regulator_dev *rdev)
                regulator_put(rdev->supply);
        }
 
+       flush_work(&rdev->disable_work.work);
+
        mutex_lock(&regulator_list_mutex);
 
        debugfs_remove_recursive(rdev->debugfs);
-       flush_work(&rdev->disable_work.work);
        WARN_ON(rdev->open_count);
        regulator_remove_coupling(rdev);
        unset_regulator_supplies(rdev);