[PATCH v2 6/7] regulator/core: regulator_lock_two: propagate error up

Michał Mirosław posted 7 patches 2 years, 3 months ago
[PATCH v2 6/7] regulator/core: regulator_lock_two: propagate error up
Posted by Michał Mirosław 2 years, 3 months ago
Fix up error paths from regulator_lock_two(): although it should not
fail, returning with half-locked state after issuing a WARN() asks
for even more trouble.

Fixes: cba6cfdc7c3f ("regulator: core: Avoid lockdep reports when resolving supplies")
Signed-off-by: Michał Mirosław <mirq-linux@rere.qmqm.pl>
---
v2:
  - updated kerneldoc
  - call ww_acquire_done() on all exits
---
 drivers/regulator/core.c | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index 7201927c5d5b..3f9621621da9 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -209,11 +209,12 @@ static void regulator_unlock(struct regulator_dev *rdev)
  * @rdev2:		second regulator
  * @ww_ctx:		w/w mutex acquire context
  *
- * Locks both rdevs using the regulator_ww_class.
+ * Locks both rdevs using the regulator_ww_class. Returns error if an
+ * unexpected error has been detected during a locking sequence.
  */
-static void regulator_lock_two(struct regulator_dev *rdev1,
-			       struct regulator_dev *rdev2,
-			       struct ww_acquire_ctx *ww_ctx)
+static int regulator_lock_two(struct regulator_dev *rdev1,
+			      struct regulator_dev *rdev2,
+			      struct ww_acquire_ctx *ww_ctx)
 {
 	struct regulator_dev *held, *contended;
 	int ret;
@@ -222,10 +223,13 @@ static void regulator_lock_two(struct regulator_dev *rdev1,
 
 	/* Try to just grab both of them */
 	ret = regulator_lock_nested(rdev1, ww_ctx);
-	WARN_ON(ret);
+	if (WARN_ON(ret))
+		goto exit;
 	ret = regulator_lock_nested(rdev2, ww_ctx);
-	if (ret != -EDEADLOCK) {
-		WARN_ON(ret);
+	if (!ret)
+		goto exit;
+	if (WARN_ON(ret != -EDEADLOCK)) {
+		regulator_unlock(rdev1);
 		goto exit;
 	}
 
@@ -239,13 +243,15 @@ static void regulator_lock_two(struct regulator_dev *rdev1,
 		ret = regulator_lock_nested(contended, ww_ctx);
 
 		if (ret != -EDEADLOCK) {
-			WARN_ON(ret);
+			if (WARN_ON(ret))
+				regulator_unlock(held);
 			break;
 		}
 	}
 
 exit:
 	ww_acquire_done(ww_ctx);
+	return ret;
 }
 
 /**
@@ -2113,7 +2119,11 @@ static int regulator_resolve_supply(struct regulator_dev *rdev)
 	 * between rdev->supply null check and setting rdev->supply in
 	 * set_supply() from concurrent tasks.
 	 */
-	regulator_lock_two(rdev, r, &ww_ctx);
+	ret = regulator_lock_two(rdev, r, &ww_ctx);
+	if (ret < 0) {
+		put_device(&r->dev);
+		return ret;
+	}
 
 	/* Supply just resolved by a concurrent task? */
 	if (rdev->supply) {
-- 
2.39.2

Re: [PATCH v2 6/7] regulator/core: regulator_lock_two: propagate error up
Posted by Doug Anderson 2 years, 3 months ago
Hi,

On Wed, Aug 30, 2023 at 10:35 AM Michał Mirosław
<mirq-linux@rere.qmqm.pl> wrote:
>
> Fix up error paths from regulator_lock_two(): although it should not
> fail, returning with half-locked state after issuing a WARN() asks
> for even more trouble.
>
> Fixes: cba6cfdc7c3f ("regulator: core: Avoid lockdep reports when resolving supplies")
> Signed-off-by: Michał Mirosław <mirq-linux@rere.qmqm.pl>
> ---
> v2:
>   - updated kerneldoc
>   - call ww_acquire_done() on all exits
> ---
>  drivers/regulator/core.c | 28 +++++++++++++++++++---------
>  1 file changed, 19 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
> index 7201927c5d5b..3f9621621da9 100644
> --- a/drivers/regulator/core.c
> +++ b/drivers/regulator/core.c
> @@ -209,11 +209,12 @@ static void regulator_unlock(struct regulator_dev *rdev)
>   * @rdev2:             second regulator
>   * @ww_ctx:            w/w mutex acquire context
>   *
> - * Locks both rdevs using the regulator_ww_class.
> + * Locks both rdevs using the regulator_ww_class. Returns error if an
> + * unexpected error has been detected during a locking sequence.

I don't believe this is the correct way to document return values in
kernel-doc. See:

Documentation/doc-guide/kernel-doc.rst

Specifically if you run:

scripts/kernel-doc -v drivers/regulator/core.c

You can see that the description of the return doesn't show up in the
proper place.

With that fixed, feel free to add my Reviewed-by tag.

-Doug