linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Michał Mirosław" <mirq-linux@rere.qmqm.pl>
To: Dmitry Osipenko <digetx@gmail.com>,
	Liam Girdwood <lgirdwood@gmail.com>,
	Mark Brown <broonie@kernel.org>
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH] regulator: simplify locking
Date: Sun, 09 Aug 2020 23:16:53 +0200	[thread overview]
Message-ID: <b22fadc413fd7a1f4018c2c9dc261abf837731cb.1597007683.git.mirq-linux@rere.qmqm.pl> (raw)

Simplify regulator locking by removing locking around locking. rdev->ref
is now accessed only when the lock is taken. The code still smells fishy,
but now its obvious why.

Fixes: f8702f9e4aa7 ("regulator: core: Use ww_mutex for regulators locking")
Signed-off-by: Michał Mirosław <mirq-linux@rere.qmqm.pl>
---
 drivers/regulator/core.c         | 37 ++++++--------------------------
 include/linux/regulator/driver.h |  1 -
 2 files changed, 6 insertions(+), 32 deletions(-)

diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index 9e18997777d3..b0662927487c 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -45,7 +45,6 @@
 	pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
 
 static DEFINE_WW_CLASS(regulator_ww_class);
-static DEFINE_MUTEX(regulator_nesting_mutex);
 static DEFINE_MUTEX(regulator_list_mutex);
 static LIST_HEAD(regulator_map_list);
 static LIST_HEAD(regulator_ena_gpio_list);
@@ -150,32 +149,13 @@ static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
 static inline int regulator_lock_nested(struct regulator_dev *rdev,
 					struct ww_acquire_ctx *ww_ctx)
 {
-	bool lock = false;
 	int ret = 0;
 
-	mutex_lock(&regulator_nesting_mutex);
+	if (ww_ctx || !mutex_trylock_recursive(&rdev->mutex.base))
+		ret = ww_mutex_lock(&rdev->mutex, ww_ctx);
 
-	if (ww_ctx || !ww_mutex_trylock(&rdev->mutex)) {
-		if (rdev->mutex_owner == current)
-			rdev->ref_cnt++;
-		else
-			lock = true;
-
-		if (lock) {
-			mutex_unlock(&regulator_nesting_mutex);
-			ret = ww_mutex_lock(&rdev->mutex, ww_ctx);
-			mutex_lock(&regulator_nesting_mutex);
-		}
-	} else {
-		lock = true;
-	}
-
-	if (lock && ret != -EDEADLK) {
+	if (ret != -EDEADLK)
 		rdev->ref_cnt++;
-		rdev->mutex_owner = current;
-	}
-
-	mutex_unlock(&regulator_nesting_mutex);
 
 	return ret;
 }
@@ -205,16 +185,11 @@ EXPORT_SYMBOL_GPL(regulator_lock);
  */
 void regulator_unlock(struct regulator_dev *rdev)
 {
-	mutex_lock(&regulator_nesting_mutex);
+	if (WARN_ON_ONCE(rdev->ref_cnt <= 0))
+		return;
 
-	if (--rdev->ref_cnt == 0) {
-		rdev->mutex_owner = NULL;
+	if (--rdev->ref_cnt == 0)
 		ww_mutex_unlock(&rdev->mutex);
-	}
-
-	WARN_ON_ONCE(rdev->ref_cnt < 0);
-
-	mutex_unlock(&regulator_nesting_mutex);
 }
 EXPORT_SYMBOL_GPL(regulator_unlock);
 
diff --git a/include/linux/regulator/driver.h b/include/linux/regulator/driver.h
index 8539f34ae42b..2fe5b1bcbe2f 100644
--- a/include/linux/regulator/driver.h
+++ b/include/linux/regulator/driver.h
@@ -448,7 +448,6 @@ struct regulator_dev {
 
 	struct blocking_notifier_head notifier;
 	struct ww_mutex mutex; /* consumer lock */
-	struct task_struct *mutex_owner;
 	int ref_cnt;
 	struct module *owner;
 	struct device dev;
-- 
2.20.1


             reply	other threads:[~2020-08-09 21:17 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-09 21:16 Michał Mirosław [this message]
2020-08-09 21:40 ` Dmitry Osipenko
2020-08-09 22:30   ` Michał Mirosław
2020-08-10  0:21     ` Dmitry Osipenko
2020-08-10  0:59       ` Michał Mirosław
2020-08-10  5:14         ` Dmitry Osipenko
2020-08-10 16:12           ` Michał Mirosław
2020-08-10  1:08       ` Michał Mirosław
2020-08-10 16:23     ` Mark Brown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b22fadc413fd7a1f4018c2c9dc261abf837731cb.1597007683.git.mirq-linux@rere.qmqm.pl \
    --to=mirq-linux@rere.qmqm.pl \
    --cc=broonie@kernel.org \
    --cc=digetx@gmail.com \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --subject='Re: [PATCH] regulator: simplify locking' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
on how to clone and mirror all data and code used for this inbox