[PATCH v2 2/7] regulator/core: regulator_lock_nested: skip extra lock for -EDEADLK

From: Michał Mirosław
Date: Wed Aug 30 2023 - 14:43:50 EST


When ww_mutex_lock() returns -EDEADLK the nesting mutex-protected
section becomes a no-op. Return early and avoid the extra lock.

Reviewed-by: Douglas Anderson <dianders@xxxxxxxxxxxx>
Signed-off-by: Michał Mirosław <mirq-linux@xxxxxxxxxxxx>
---
drivers/regulator/core.c | 19 +++++++++----------
1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index 215b721e5cd4..921c7039baa3 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -145,18 +145,17 @@ static inline int regulator_lock_nested(struct regulator_dev *rdev,

mutex_lock(&regulator_nesting_mutex);

- if (!ww_mutex_trylock(&rdev->mutex, ww_ctx)) {
- if (rdev->mutex_owner != current) {
- mutex_unlock(&regulator_nesting_mutex);
- ret = ww_mutex_lock(&rdev->mutex, ww_ctx);
- mutex_lock(&regulator_nesting_mutex);
- }
+ if (!ww_mutex_trylock(&rdev->mutex, ww_ctx) &&
+ rdev->mutex_owner != current) {
+ mutex_unlock(&regulator_nesting_mutex);
+ ret = ww_mutex_lock(&rdev->mutex, ww_ctx);
+ if (ret == -EDEADLK)
+ return ret;
+ mutex_lock(&regulator_nesting_mutex);
}

- if (ret != -EDEADLK) {
- rdev->ref_cnt++;
- rdev->mutex_owner = current;
- }
+ rdev->ref_cnt++;
+ rdev->mutex_owner = current;

mutex_unlock(&regulator_nesting_mutex);

--
2.39.2