linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] iio: magnetometer: ak8974: Fix runtime PM imbalance on error
@ 2020-05-24  2:51 Dinghao Liu
  2020-05-25 12:08 ` Linus Walleij
  0 siblings, 1 reply; 5+ messages in thread
From: Dinghao Liu @ 2020-05-24  2:51 UTC (permalink / raw)
  To: dinghao.liu, kjlu
  Cc: Linus Walleij, Jonathan Cameron, Hartmut Knaack,
	Lars-Peter Clausen, Peter Meerwald-Stadler, linux-iio,
	linux-kernel

When devm_regmap_init_i2c() returns an error code, a pairing
runtime PM usage counter decrement is needed to keep the
counter balanced. For error paths after ak8974_set_power(),
ak8974_detect() and ak8974_reset(), things are the same.

However, When iio_triggered_buffer_setup() returns an error
code, we don't need such a decrement because there is already
one before this call. Things are the same for other error paths
after it.

Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
---
 drivers/iio/magnetometer/ak8974.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/magnetometer/ak8974.c b/drivers/iio/magnetometer/ak8974.c
index d32996702110..c1bdb304eb9e 100644
--- a/drivers/iio/magnetometer/ak8974.c
+++ b/drivers/iio/magnetometer/ak8974.c
@@ -764,18 +764,24 @@ static int ak8974_probe(struct i2c_client *i2c,
 	ak8974->map = devm_regmap_init_i2c(i2c, &ak8974_regmap_config);
 	if (IS_ERR(ak8974->map)) {
 		dev_err(&i2c->dev, "failed to allocate register map\n");
+		pm_runtime_put_noidle(&i2c->dev);
+		pm_runtime_disable(&i2c->dev);
 		return PTR_ERR(ak8974->map);
 	}
 
 	ret = ak8974_set_power(ak8974, AK8974_PWR_ON);
 	if (ret) {
 		dev_err(&i2c->dev, "could not power on\n");
+		pm_runtime_put_noidle(&i2c->dev);
+		pm_runtime_disable(&i2c->dev);
 		goto power_off;
 	}
 
 	ret = ak8974_detect(ak8974);
 	if (ret) {
 		dev_err(&i2c->dev, "neither AK8974 nor AMI30x found\n");
+		pm_runtime_put_noidle(&i2c->dev);
+		pm_runtime_disable(&i2c->dev);
 		goto power_off;
 	}
 
@@ -786,6 +792,8 @@ static int ak8974_probe(struct i2c_client *i2c,
 	ret = ak8974_reset(ak8974);
 	if (ret) {
 		dev_err(&i2c->dev, "AK8974 reset failed\n");
+		pm_runtime_put_noidle(&i2c->dev);
+		pm_runtime_disable(&i2c->dev);
 		goto power_off;
 	}
 
@@ -851,7 +859,6 @@ static int ak8974_probe(struct i2c_client *i2c,
 cleanup_buffer:
 	iio_triggered_buffer_cleanup(indio_dev);
 disable_pm:
-	pm_runtime_put_noidle(&i2c->dev);
 	pm_runtime_disable(&i2c->dev);
 	ak8974_set_power(ak8974, AK8974_PWR_OFF);
 power_off:
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] iio: magnetometer: ak8974: Fix runtime PM imbalance on error
  2020-05-24  2:51 [PATCH] iio: magnetometer: ak8974: Fix runtime PM imbalance on error Dinghao Liu
@ 2020-05-25 12:08 ` Linus Walleij
  2020-05-26  5:14   ` dinghao.liu
  0 siblings, 1 reply; 5+ messages in thread
From: Linus Walleij @ 2020-05-25 12:08 UTC (permalink / raw)
  To: Dinghao Liu
  Cc: Kangjie Lu, Jonathan Cameron, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, linux-iio, linux-kernel

On Sun, May 24, 2020 at 4:51 AM Dinghao Liu <dinghao.liu@zju.edu.cn> wrote:

> When devm_regmap_init_i2c() returns an error code, a pairing
> runtime PM usage counter decrement is needed to keep the
> counter balanced. For error paths after ak8974_set_power(),
> ak8974_detect() and ak8974_reset(), things are the same.
>
> However, When iio_triggered_buffer_setup() returns an error
> code, we don't need such a decrement because there is already
> one before this call. Things are the same for other error paths
> after it.
>
> Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>

>         ak8974->map = devm_regmap_init_i2c(i2c, &ak8974_regmap_config);
>         if (IS_ERR(ak8974->map)) {
>                 dev_err(&i2c->dev, "failed to allocate register map\n");
> +               pm_runtime_put_noidle(&i2c->dev);
> +               pm_runtime_disable(&i2c->dev);
>                 return PTR_ERR(ak8974->map);

This is correct.

>         ret = ak8974_set_power(ak8974, AK8974_PWR_ON);
>         if (ret) {
>                 dev_err(&i2c->dev, "could not power on\n");
> +               pm_runtime_put_noidle(&i2c->dev);
> +               pm_runtime_disable(&i2c->dev);
>                 goto power_off;

What about just changing this to goto disable_pm;

>         ret = ak8974_detect(ak8974);
>         if (ret) {
>                 dev_err(&i2c->dev, "neither AK8974 nor AMI30x found\n");
> +               pm_runtime_put_noidle(&i2c->dev);
> +               pm_runtime_disable(&i2c->dev);
>                 goto power_off;

goto disable_pm;

> @@ -786,6 +792,8 @@ static int ak8974_probe(struct i2c_client *i2c,
>         ret = ak8974_reset(ak8974);
>         if (ret) {
>                 dev_err(&i2c->dev, "AK8974 reset failed\n");
> +               pm_runtime_put_noidle(&i2c->dev);
> +               pm_runtime_disable(&i2c->dev);

goto disable_pm;

>  disable_pm:
> -       pm_runtime_put_noidle(&i2c->dev);
>         pm_runtime_disable(&i2c->dev);
>         ak8974_set_power(ak8974, AK8974_PWR_OFF);

Keep the top pm_runtime_put_noidle().

The ak8974_set_power() call is fine, the power on call does not
need to happen in balance. Sure it will attempt to write a register
but so will the power on call.

Yours,
Linus Walleij

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Re: [PATCH] iio: magnetometer: ak8974: Fix runtime PM imbalance on error
  2020-05-25 12:08 ` Linus Walleij
@ 2020-05-26  5:14   ` dinghao.liu
  2020-05-26  9:31     ` Linus Walleij
  0 siblings, 1 reply; 5+ messages in thread
From: dinghao.liu @ 2020-05-26  5:14 UTC (permalink / raw)
  To: Linus Walleij
  Cc: Kangjie Lu, Jonathan Cameron, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, linux-iio, linux-kernel

Hi, Linus

> On Sun, May 24, 2020 at 4:51 AM Dinghao Liu <dinghao.liu@zju.edu.cn> wrote:
> 
> > When devm_regmap_init_i2c() returns an error code, a pairing
> > runtime PM usage counter decrement is needed to keep the
> > counter balanced. For error paths after ak8974_set_power(),
> > ak8974_detect() and ak8974_reset(), things are the same.
> >
> > However, When iio_triggered_buffer_setup() returns an error
> > code, we don't need such a decrement because there is already
> > one before this call. Things are the same for other error paths
> > after it.
> >
> > Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
> 
> >         ak8974->map = devm_regmap_init_i2c(i2c, &ak8974_regmap_config);
> >         if (IS_ERR(ak8974->map)) {
> >                 dev_err(&i2c->dev, "failed to allocate register map\n");
> > +               pm_runtime_put_noidle(&i2c->dev);
> > +               pm_runtime_disable(&i2c->dev);
> >                 return PTR_ERR(ak8974->map);
> 
> This is correct.
> 
> >         ret = ak8974_set_power(ak8974, AK8974_PWR_ON);
> >         if (ret) {
> >                 dev_err(&i2c->dev, "could not power on\n");
> > +               pm_runtime_put_noidle(&i2c->dev);
> > +               pm_runtime_disable(&i2c->dev);
> >                 goto power_off;
> 
> What about just changing this to goto disable_pm;
>
> >         ret = ak8974_detect(ak8974);
> >         if (ret) {
> >                 dev_err(&i2c->dev, "neither AK8974 nor AMI30x found\n");
> > +               pm_runtime_put_noidle(&i2c->dev);
> > +               pm_runtime_disable(&i2c->dev);
> >                 goto power_off;
> 
> goto disable_pm;
> 
> > @@ -786,6 +792,8 @@ static int ak8974_probe(struct i2c_client *i2c,
> >         ret = ak8974_reset(ak8974);
> >         if (ret) {
> >                 dev_err(&i2c->dev, "AK8974 reset failed\n");
> > +               pm_runtime_put_noidle(&i2c->dev);
> > +               pm_runtime_disable(&i2c->dev);
> 
> goto disable_pm;
> 
> >  disable_pm:
> > -       pm_runtime_put_noidle(&i2c->dev);
> >         pm_runtime_disable(&i2c->dev);
> >         ak8974_set_power(ak8974, AK8974_PWR_OFF);
> 
> Keep the top pm_runtime_put_noidle().

I found that there was already a pm_runtime_put() before 
iio_triggered_buffer_setup() (just after pm_runtime_use_autosuspend).
So if we keep the pm_runtime_put_noidle() here, we will have
two pmusage counter decrement. Do you think this is a bug?

Regards,
Dinghao

> 
> The ak8974_set_power() call is fine, the power on call does not
> need to happen in balance. Sure it will attempt to write a register
> but so will the power on call.
> 
> Yours,
> Linus Walleij

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Re: [PATCH] iio: magnetometer: ak8974: Fix runtime PM imbalance on error
  2020-05-26  5:14   ` dinghao.liu
@ 2020-05-26  9:31     ` Linus Walleij
  2020-05-26 10:05       ` dinghao.liu
  0 siblings, 1 reply; 5+ messages in thread
From: Linus Walleij @ 2020-05-26  9:31 UTC (permalink / raw)
  To: Dinghao Liu
  Cc: Kangjie Lu, Jonathan Cameron, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, linux-iio, linux-kernel

On Tue, May 26, 2020 at 7:14 AM <dinghao.liu@zju.edu.cn> wrote:
> > On Sun, May 24, 2020 at 4:51 AM Dinghao Liu <dinghao.liu@zju.edu.cn> wrote:

> > >  disable_pm:
> > > -       pm_runtime_put_noidle(&i2c->dev);
> > >         pm_runtime_disable(&i2c->dev);
> > >         ak8974_set_power(ak8974, AK8974_PWR_OFF);
> >
> > Keep the top pm_runtime_put_noidle().
>
> I found that there was already a pm_runtime_put() before
> iio_triggered_buffer_setup() (just after pm_runtime_use_autosuspend).
> So if we keep the pm_runtime_put_noidle() here, we will have
> two pmusage counter decrement. Do you think this is a bug?

Yes you're right.

What about just moving the pm_runtime_put() until the end
of the initialization? Right before return 0;
Then we can keep this nice goto exits as they are.

Maybe move all these three:

      pm_runtime_set_autosuspend_delay(&i2c->dev,
                                         AK8974_AUTOSUSPEND_DELAY);
        pm_runtime_use_autosuspend(&i2c->dev);
        pm_runtime_put(&i2c->dev);

Yours,
Linus Walleij

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Re: Re: [PATCH] iio: magnetometer: ak8974: Fix runtime PM imbalance on error
  2020-05-26  9:31     ` Linus Walleij
@ 2020-05-26 10:05       ` dinghao.liu
  0 siblings, 0 replies; 5+ messages in thread
From: dinghao.liu @ 2020-05-26 10:05 UTC (permalink / raw)
  To: Linus Walleij
  Cc: Kangjie Lu, Jonathan Cameron, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, linux-iio, linux-kernel

> What about just moving the pm_runtime_put() until the end
> of the initialization? Right before return 0;
> Then we can keep this nice goto exits as they are.
> 
> Maybe move all these three:
> 
>       pm_runtime_set_autosuspend_delay(&i2c->dev,
>                                          AK8974_AUTOSUSPEND_DELAY);
>         pm_runtime_use_autosuspend(&i2c->dev);
>         pm_runtime_put(&i2c->dev);
> 

Good idea! Thank you for your advice and I will fix this 
in the next version of patch. 

Regards,
Dinghao

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-05-26 10:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-24  2:51 [PATCH] iio: magnetometer: ak8974: Fix runtime PM imbalance on error Dinghao Liu
2020-05-25 12:08 ` Linus Walleij
2020-05-26  5:14   ` dinghao.liu
2020-05-26  9:31     ` Linus Walleij
2020-05-26 10:05       ` dinghao.liu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).