[PATCH v2] counter: ti-eqep: balance pm_runtime on devm_clk_get_enabled() failure

Stepan Ionichev posted 1 patch 2 weeks ago
drivers/counter/ti-eqep.c | 18 +++++++++++-------
1 file changed, 11 insertions(+), 7 deletions(-)
[PATCH v2] counter: ti-eqep: balance pm_runtime on devm_clk_get_enabled() failure
Posted by Stepan Ionichev 2 weeks ago
ti_eqep_probe() calls pm_runtime_enable() and pm_runtime_get_sync()
before devm_clk_get_enabled(). If the clk call fails, the function
returns directly via dev_err_probe(), leaving runtime PM enabled
and the usage counter incremented.

Route the clk error through the same err_pm cleanup as counter_add()
so the runtime PM state is unwound on every failure path.

Fixes: f213729f6796 ("counter: new TI eQEP driver")
Signed-off-by: Stepan Ionichev <sozdayvek@gmail.com>
---
v2:
- Collapse the dev_err_probe()+assignment pair into a single line
  using its return value (Maxwell)
- Add Fixes tag

v1: https://lore.kernel.org/all/20260523184448.7609-1-sozdayvek@gmail.com/

 drivers/counter/ti-eqep.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/drivers/counter/ti-eqep.c b/drivers/counter/ti-eqep.c
index d21c157e5..4f3b17939 100644
--- a/drivers/counter/ti-eqep.c
+++ b/drivers/counter/ti-eqep.c
@@ -548,17 +548,21 @@ static int ti_eqep_probe(struct platform_device *pdev)
 	pm_runtime_get_sync(dev);
 
 	clk = devm_clk_get_enabled(dev, NULL);
-	if (IS_ERR(clk))
-		return dev_err_probe(dev, PTR_ERR(clk), "failed to enable clock\n");
+	if (IS_ERR(clk)) {
+		err = dev_err_probe(dev, PTR_ERR(clk), "failed to enable clock\n");
+		goto err_pm;
+	}
 
 	err = counter_add(counter);
-	if (err < 0) {
-		pm_runtime_put_sync(dev);
-		pm_runtime_disable(dev);
-		return err;
-	}
+	if (err < 0)
+		goto err_pm;
 
 	return 0;
+
+err_pm:
+	pm_runtime_put_sync(dev);
+	pm_runtime_disable(dev);
+	return err;
 }
 
 static void ti_eqep_remove(struct platform_device *pdev)
-- 
2.43.0
Re: [PATCH v2] counter: ti-eqep: balance pm_runtime on devm_clk_get_enabled() failure
Posted by Andy Shevchenko 1 week, 5 days ago
On Mon, May 25, 2026 at 5:21 PM Stepan Ionichev <sozdayvek@gmail.com> wrote:
>
> ti_eqep_probe() calls pm_runtime_enable() and pm_runtime_get_sync()
> before devm_clk_get_enabled(). If the clk call fails, the function
> returns directly via dev_err_probe(), leaving runtime PM enabled
> and the usage counter incremented.
>
> Route the clk error through the same err_pm cleanup as counter_add()
> so the runtime PM state is unwound on every failure path.

...

>         pm_runtime_get_sync(dev);
>
>         clk = devm_clk_get_enabled(dev, NULL);
> -       if (IS_ERR(clk))
> -               return dev_err_probe(dev, PTR_ERR(clk), "failed to enable clock\n");
> +       if (IS_ERR(clk)) {
> +               err = dev_err_probe(dev, PTR_ERR(clk), "failed to enable clock\n");
> +               goto err_pm;

This is not right. The devm_*() should not be followed by goto.

> +       }

...

> +err_pm:
> +       pm_runtime_put_sync(dev);
> +       pm_runtime_disable(dev);
> +       return err;

You have to use the respective devm_pm_*() at the same time.

...

William, this patch should not be applied (or should be reverted) as
in current form it replaces one issue by another.

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH v2] counter: ti-eqep: balance pm_runtime on devm_clk_get_enabled() failure
Posted by William Breathitt Gray 1 week, 5 days ago
On Wed, May 27, 2026 at 10:10:20AM +0200, Andy Shevchenko wrote:
> On Mon, May 25, 2026 at 5:21 PM Stepan Ionichev <sozdayvek@gmail.com> wrote:
> >
> > ti_eqep_probe() calls pm_runtime_enable() and pm_runtime_get_sync()
> > before devm_clk_get_enabled(). If the clk call fails, the function
> > returns directly via dev_err_probe(), leaving runtime PM enabled
> > and the usage counter incremented.
> >
> > Route the clk error through the same err_pm cleanup as counter_add()
> > so the runtime PM state is unwound on every failure path.
> 
> ...
> 
> >         pm_runtime_get_sync(dev);
> >
> >         clk = devm_clk_get_enabled(dev, NULL);
> > -       if (IS_ERR(clk))
> > -               return dev_err_probe(dev, PTR_ERR(clk), "failed to enable clock\n");
> > +       if (IS_ERR(clk)) {
> > +               err = dev_err_probe(dev, PTR_ERR(clk), "failed to enable clock\n");
> > +               goto err_pm;
> 
> This is not right. The devm_*() should not be followed by goto.
> 
> > +       }
> 
> ...
> 
> > +err_pm:
> > +       pm_runtime_put_sync(dev);
> > +       pm_runtime_disable(dev);
> > +       return err;
> 
> You have to use the respective devm_pm_*() at the same time.
> 
> ...
> 
> William, this patch should not be applied (or should be reverted) as
> in current form it replaces one issue by another.
> 
> --
> With Best Regards,
> Andy Shevchenko

I'll revert for now. Would the proper fix be to replace all
pm_runtime_*() with their respective devm_pm_*() calls?

William Breathitt Gray
Re: [PATCH v2] counter: ti-eqep: balance pm_runtime on devm_clk_get_enabled() failure
Posted by Andy Shevchenko 1 week, 5 days ago
On Wed, May 27, 2026 at 10:18 AM William Breathitt Gray <wbg@kernel.org> wrote:
> On Wed, May 27, 2026 at 10:10:20AM +0200, Andy Shevchenko wrote:
> > On Mon, May 25, 2026 at 5:21 PM Stepan Ionichev <sozdayvek@gmail.com> wrote:

...

> > >         pm_runtime_get_sync(dev);
> > >
> > >         clk = devm_clk_get_enabled(dev, NULL);
> > > -       if (IS_ERR(clk))
> > > -               return dev_err_probe(dev, PTR_ERR(clk), "failed to enable clock\n");
> > > +       if (IS_ERR(clk)) {
> > > +               err = dev_err_probe(dev, PTR_ERR(clk), "failed to enable clock\n");
> > > +               goto err_pm;
> >
> > This is not right. The devm_*() should not be followed by goto.
> >
> > > +       }

...
> >
> > > +err_pm:
> > > +       pm_runtime_put_sync(dev);
> > > +       pm_runtime_disable(dev);
> > > +       return err;
> >
> > You have to use the respective devm_pm_*() at the same time.

...

> > William, this patch should not be applied (or should be reverted) as
> > in current form it replaces one issue by another.
>
> I'll revert for now. Would the proper fix be to replace all
> pm_runtime_*() with their respective devm_pm_*() calls?

Not all, but the ones that are opposite to
pm_runtime_put*()/pm_runtime_disable(). It should be done carefully as
well as PM runtime may be required before final registering of the
device to access the registers (I haven't checked the driver, so I
don't know if simple use devm_pm_*() will be enough).

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH v2] counter: ti-eqep: balance pm_runtime on devm_clk_get_enabled() failure
Posted by Stepan Ionichev 1 week, 3 days ago
On Wed, May 27, 2026 at 10:29 AM Andy Shevchenko <andy.shevchenko@gmail.com> wrote:
> > I'll revert for now. Would the proper fix be to replace all
> > pm_runtime_*() with their respective devm_pm_*() calls?
>
> Not all, but the ones that are opposite to
> pm_runtime_put*()/pm_runtime_disable(). It should be done carefully as
> well as PM runtime may be required before final registering of the
> device to access the registers (I haven't checked the driver, so I
> don't know if simple use devm_pm_*() will be enough).

The eqep driver has no runtime suspend/resume callbacks - it only
resumes in probe so that devm_clk_get_enabled() and counter_add() can
reach the registers. So devm_pm_runtime_enable() plus
pm_runtime_resume_and_get() before the clock get, with a devm action
for the put, keeps the device resumed across registration and unwinds
in reverse order on the error paths and on unbind. I'll send a v3 along
those lines.

Stepan
Re: [PATCH v2] counter: ti-eqep: balance pm_runtime on devm_clk_get_enabled() failure
Posted by William Breathitt Gray 1 week, 5 days ago
On Mon, 25 May 2026 20:21:37 +0500, Stepan Ionichev wrote:
> ti_eqep_probe() calls pm_runtime_enable() and pm_runtime_get_sync()
> before devm_clk_get_enabled(). If the clk call fails, the function
> returns directly via dev_err_probe(), leaving runtime PM enabled
> and the usage counter incremented.
> 
> Route the clk error through the same err_pm cleanup as counter_add()
> so the runtime PM state is unwound on every failure path.
> 
> [...]

Applied, thanks!

[1/1] counter: ti-eqep: balance pm_runtime on devm_clk_get_enabled() failure
      commit: abeabb5fc6a7d4c347d494dfe5bc9449c4535226

Best regards,
-- 
William Breathitt Gray <wbg@kernel.org>
Re: [PATCH v2] counter: ti-eqep: balance pm_runtime on devm_clk_get_enabled() failure
Posted by Joshua Crofts 1 week, 6 days ago
On Mon, 25 May 2026 at 17:21, Stepan Ionichev <sozdayvek@gmail.com> wrote:
>
> ti_eqep_probe() calls pm_runtime_enable() and pm_runtime_get_sync()
> before devm_clk_get_enabled(). If the clk call fails, the function
> returns directly via dev_err_probe(), leaving runtime PM enabled
> and the usage counter incremented.
>
> Route the clk error through the same err_pm cleanup as counter_add()
> so the runtime PM state is unwound on every failure path.
>
> Fixes: f213729f6796 ("counter: new TI eQEP driver")
> Signed-off-by: Stepan Ionichev <sozdayvek@gmail.com>
> ---
> v2:
> - Collapse the dev_err_probe()+assignment pair into a single line
>   using its return value (Maxwell)
> - Add Fixes tag
>
> v1: https://lore.kernel.org/all/20260523184448.7609-1-sozdayvek@gmail.com/
>
>  drivers/counter/ti-eqep.c | 18 +++++++++++-------
>  1 file changed, 11 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/counter/ti-eqep.c b/drivers/counter/ti-eqep.c
> index d21c157e5..4f3b17939 100644
> --- a/drivers/counter/ti-eqep.c
> +++ b/drivers/counter/ti-eqep.c
> @@ -548,17 +548,21 @@ static int ti_eqep_probe(struct platform_device *pdev)
>         pm_runtime_get_sync(dev);
>
>         clk = devm_clk_get_enabled(dev, NULL);
> -       if (IS_ERR(clk))
> -               return dev_err_probe(dev, PTR_ERR(clk), "failed to enable clock\n");
> +       if (IS_ERR(clk)) {
> +               err = dev_err_probe(dev, PTR_ERR(clk), "failed to enable clock\n");
> +               goto err_pm;
> +       }
>
>         err = counter_add(counter);
> -       if (err < 0) {
> -               pm_runtime_put_sync(dev);
> -               pm_runtime_disable(dev);
> -               return err;
> -       }
> +       if (err < 0)
> +               goto err_pm;
>
>         return 0;
> +
> +err_pm:
> +       pm_runtime_put_sync(dev);
> +       pm_runtime_disable(dev);
> +       return err;
>  }
>
>  static void ti_eqep_remove(struct platform_device *pdev)
> --
> 2.43.0
>

Sashiko came back with some notes, however they are issues unrelated
to this patch. LGTM.

Reviewed-by: Joshua Crofts <joshua.crofts1@gmail.com>

-- 
Kind regards

CJD