diff mbox series

[1/3] iio: adc: ad7124: Fix missbalanced regulator enable / disable on error.

Message ID 20210508182319.488551-2-jic23@kernel.org (mailing list archive)
State New, archived
Headers show
Series iio: adc: ad7124: Fixes and devm_ for all of probe | expand

Commit Message

Jonathan Cameron May 8, 2021, 6:23 p.m. UTC
From: Jonathan Cameron <Jonathan.Cameron@huawei.com>

If the devm_regulator_get() call succeeded but not the regulator_enable()
then regulator_disable() would be called on a regulator that was not
enabled.

Fix this by moving regulator enabling / disabling over to
devm_ management via devm_add_action_or_reset.

Fixes: b3af341bbd96 ("iio: adc: Add ad7124 support")
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Cc: Alexandru Ardelean <ardeleanalex@gmail.com>
---
 drivers/iio/adc/ad7124.c | 29 +++++++++++++----------------
 1 file changed, 13 insertions(+), 16 deletions(-)

Comments

Alexandru Ardelean May 9, 2021, 7:20 a.m. UTC | #1
On Sat, May 8, 2021 at 9:24 PM Jonathan Cameron <jic23@kernel.org> wrote:
>
> From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
>
> If the devm_regulator_get() call succeeded but not the regulator_enable()
> then regulator_disable() would be called on a regulator that was not
> enabled.
>
> Fix this by moving regulator enabling / disabling over to
> devm_ management via devm_add_action_or_reset.
>

Definitely looks unbalanced.
This was probably written in the days when I wasn't familiar with devm_ much.
I probably would have complained about this.

Reviewed-by: Alexandru Ardelean <ardeleanalex@gmail.com>

> Fixes: b3af341bbd96 ("iio: adc: Add ad7124 support")
> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Cc: Alexandru Ardelean <ardeleanalex@gmail.com>
> ---
>  drivers/iio/adc/ad7124.c | 29 +++++++++++++----------------
>  1 file changed, 13 insertions(+), 16 deletions(-)
>
> diff --git a/drivers/iio/adc/ad7124.c b/drivers/iio/adc/ad7124.c
> index 766c73333604..c0d0870a29ff 100644
> --- a/drivers/iio/adc/ad7124.c
> +++ b/drivers/iio/adc/ad7124.c
> @@ -707,6 +707,11 @@ static int ad7124_setup(struct ad7124_state *st)
>         return ret;
>  }
>
> +static void ad7124_reg_disable(void *r)
> +{
> +       regulator_disable(r);
> +}
> +
>  static int ad7124_probe(struct spi_device *spi)
>  {
>         const struct ad7124_chip_info *info;
> @@ -752,17 +757,20 @@ static int ad7124_probe(struct spi_device *spi)
>                 ret = regulator_enable(st->vref[i]);
>                 if (ret)
>                         return ret;
> +
> +               ret = devm_add_action_or_reset(&spi->dev, ad7124_reg_disable,
> +                                              st->vref[i]);
> +               if (ret)
> +                       return ret;
>         }
>
>         st->mclk = devm_clk_get(&spi->dev, "mclk");
> -       if (IS_ERR(st->mclk)) {
> -               ret = PTR_ERR(st->mclk);
> -               goto error_regulator_disable;
> -       }
> +       if (IS_ERR(st->mclk))
> +               return PTR_ERR(st->mclk);
>
>         ret = clk_prepare_enable(st->mclk);
>         if (ret < 0)
> -               goto error_regulator_disable;
> +               return ret;
>
>         ret = ad7124_soft_reset(st);
>         if (ret < 0)
> @@ -792,11 +800,6 @@ static int ad7124_probe(struct spi_device *spi)
>         ad_sd_cleanup_buffer_and_trigger(indio_dev);
>  error_clk_disable_unprepare:
>         clk_disable_unprepare(st->mclk);
> -error_regulator_disable:
> -       for (i = ARRAY_SIZE(st->vref) - 1; i >= 0; i--) {
> -               if (!IS_ERR_OR_NULL(st->vref[i]))
> -                       regulator_disable(st->vref[i]);
> -       }
>
>         return ret;
>  }
> @@ -805,17 +808,11 @@ static int ad7124_remove(struct spi_device *spi)
>  {
>         struct iio_dev *indio_dev = spi_get_drvdata(spi);
>         struct ad7124_state *st = iio_priv(indio_dev);
> -       int i;
>
>         iio_device_unregister(indio_dev);
>         ad_sd_cleanup_buffer_and_trigger(indio_dev);
>         clk_disable_unprepare(st->mclk);
>
> -       for (i = ARRAY_SIZE(st->vref) - 1; i >= 0; i--) {
> -               if (!IS_ERR_OR_NULL(st->vref[i]))
> -                       regulator_disable(st->vref[i]);
> -       }
> -
>         return 0;
>  }
>
> --
> 2.31.1
>
diff mbox series

Patch

diff --git a/drivers/iio/adc/ad7124.c b/drivers/iio/adc/ad7124.c
index 766c73333604..c0d0870a29ff 100644
--- a/drivers/iio/adc/ad7124.c
+++ b/drivers/iio/adc/ad7124.c
@@ -707,6 +707,11 @@  static int ad7124_setup(struct ad7124_state *st)
 	return ret;
 }
 
+static void ad7124_reg_disable(void *r)
+{
+	regulator_disable(r);
+}
+
 static int ad7124_probe(struct spi_device *spi)
 {
 	const struct ad7124_chip_info *info;
@@ -752,17 +757,20 @@  static int ad7124_probe(struct spi_device *spi)
 		ret = regulator_enable(st->vref[i]);
 		if (ret)
 			return ret;
+
+		ret = devm_add_action_or_reset(&spi->dev, ad7124_reg_disable,
+					       st->vref[i]);
+		if (ret)
+			return ret;
 	}
 
 	st->mclk = devm_clk_get(&spi->dev, "mclk");
-	if (IS_ERR(st->mclk)) {
-		ret = PTR_ERR(st->mclk);
-		goto error_regulator_disable;
-	}
+	if (IS_ERR(st->mclk))
+		return PTR_ERR(st->mclk);
 
 	ret = clk_prepare_enable(st->mclk);
 	if (ret < 0)
-		goto error_regulator_disable;
+		return ret;
 
 	ret = ad7124_soft_reset(st);
 	if (ret < 0)
@@ -792,11 +800,6 @@  static int ad7124_probe(struct spi_device *spi)
 	ad_sd_cleanup_buffer_and_trigger(indio_dev);
 error_clk_disable_unprepare:
 	clk_disable_unprepare(st->mclk);
-error_regulator_disable:
-	for (i = ARRAY_SIZE(st->vref) - 1; i >= 0; i--) {
-		if (!IS_ERR_OR_NULL(st->vref[i]))
-			regulator_disable(st->vref[i]);
-	}
 
 	return ret;
 }
@@ -805,17 +808,11 @@  static int ad7124_remove(struct spi_device *spi)
 {
 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
 	struct ad7124_state *st = iio_priv(indio_dev);
-	int i;
 
 	iio_device_unregister(indio_dev);
 	ad_sd_cleanup_buffer_and_trigger(indio_dev);
 	clk_disable_unprepare(st->mclk);
 
-	for (i = ARRAY_SIZE(st->vref) - 1; i >= 0; i--) {
-		if (!IS_ERR_OR_NULL(st->vref[i]))
-			regulator_disable(st->vref[i]);
-	}
-
 	return 0;
 }