diff mbox series

[v3,25/79] staging: media: vde: use pm_runtime_resume_and_get()

Message ID d7fb2d00224d37ba1c6c6e9b73609af95c886844.1619519080.git.mchehab+huawei@kernel.org
State Superseded
Headers show
Series None | expand

Commit Message

Mauro Carvalho Chehab April 27, 2021, 10:26 a.m. UTC
Commit dd8088d5a896 ("PM: runtime: Add pm_runtime_resume_and_get to deal with usage counter")
added pm_runtime_resume_and_get() in order to automatically handle
dev->power.usage_count decrement on errors.

Use the new API, in order to cleanup the error check logic.

Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
---
 drivers/staging/media/tegra-vde/vde.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

Comments

Dmitry Osipenko April 27, 2021, 11:47 a.m. UTC | #1
27.04.2021 13:26, Mauro Carvalho Chehab пишет:
> @@ -1088,8 +1090,9 @@ static int tegra_vde_remove(struct platform_device *pdev)
>  {
>  	struct tegra_vde *vde = platform_get_drvdata(pdev);
>  	struct device *dev = &pdev->dev;
> +	int ret;
>  
> -	pm_runtime_get_sync(dev);
> +	ret = pm_runtime_resume_and_get(dev);

Should be cleaner to return error directly here, IMO.

>  	pm_runtime_dont_use_autosuspend(dev);
>  	pm_runtime_disable(dev);
>  
> @@ -1097,7 +1100,8 @@ static int tegra_vde_remove(struct platform_device *pdev)
>  	 * Balance RPM state, the VDE power domain is left ON and hardware
>  	 * is clock-gated. It's safe to reboot machine now.
>  	 */
> -	pm_runtime_put_noidle(dev);
> +	if (ret >= 0)
> +		pm_runtime_put_noidle(dev);
>  	clk_disable_unprepare(vde->clk);
Mauro Carvalho Chehab April 28, 2021, 7:20 a.m. UTC | #2
Em Tue, 27 Apr 2021 14:47:01 +0300
Dmitry Osipenko <digetx@gmail.com> escreveu:

> 27.04.2021 13:26, Mauro Carvalho Chehab пишет:

> > @@ -1088,8 +1090,9 @@ static int tegra_vde_remove(struct platform_device *pdev)

> >  {

> >  	struct tegra_vde *vde = platform_get_drvdata(pdev);

> >  	struct device *dev = &pdev->dev;

> > +	int ret;

> >  

> > -	pm_runtime_get_sync(dev);

> > +	ret = pm_runtime_resume_and_get(dev);  

> 

> Should be cleaner to return error directly here, IMO.


I double-checked how drivers/base/platform.c deals with non-zero
returns at the .remove method:

	static int platform_remove(struct device *_dev)
	{
	        struct platform_driver *drv = to_platform_driver(_dev->driver);
	        struct platform_device *dev = to_platform_device(_dev);
	
	        if (drv->remove) {
	                int ret = drv->remove(dev);
	
	                if (ret)
	                        dev_warn(_dev, "remove callback returned a non-zero value. This will be ignored.\n");
	        }
	        dev_pm_domain_detach(_dev, true);
	
	        return 0;
	}

Basically, it will print a message but will ignore whatever happens
afterwards.

So, if the driver is changed to return an error there, it will leak
resources.

Thanks,
Mauro
Dmitry Osipenko April 28, 2021, 8:05 a.m. UTC | #3
28.04.2021 10:20, Mauro Carvalho Chehab пишет:
> Em Tue, 27 Apr 2021 14:47:01 +0300

> Dmitry Osipenko <digetx@gmail.com> escreveu:

> 

>> 27.04.2021 13:26, Mauro Carvalho Chehab пишет:

>>> @@ -1088,8 +1090,9 @@ static int tegra_vde_remove(struct platform_device *pdev)

>>>  {

>>>  	struct tegra_vde *vde = platform_get_drvdata(pdev);

>>>  	struct device *dev = &pdev->dev;

>>> +	int ret;

>>>  

>>> -	pm_runtime_get_sync(dev);

>>> +	ret = pm_runtime_resume_and_get(dev);  

>>

>> Should be cleaner to return error directly here, IMO.

> 

> I double-checked how drivers/base/platform.c deals with non-zero

> returns at the .remove method:

> 

> 	static int platform_remove(struct device *_dev)

> 	{

> 	        struct platform_driver *drv = to_platform_driver(_dev->driver);

> 	        struct platform_device *dev = to_platform_device(_dev);

> 	

> 	        if (drv->remove) {

> 	                int ret = drv->remove(dev);

> 	

> 	                if (ret)

> 	                        dev_warn(_dev, "remove callback returned a non-zero value. This will be ignored.\n");

> 	        }

> 	        dev_pm_domain_detach(_dev, true);

> 	

> 	        return 0;

> 	}

> 

> Basically, it will print a message but will ignore whatever happens

> afterwards.

> 

> So, if the driver is changed to return an error there, it will leak

> resources.


Indeed, thank you. But then the pm_runtime_get_sync() should be more
appropriate since this function is specifically made for such cases
where returned value is ignored.

A better option could be better to add a clarifying comment to the code
rather than to change it to a variant which introduces confusion, IMO.
diff mbox series

Patch

diff --git a/drivers/staging/media/tegra-vde/vde.c b/drivers/staging/media/tegra-vde/vde.c
index 28845b5bafaf..8936f140a246 100644
--- a/drivers/staging/media/tegra-vde/vde.c
+++ b/drivers/staging/media/tegra-vde/vde.c
@@ -775,9 +775,9 @@  static int tegra_vde_ioctl_decode_h264(struct tegra_vde *vde,
 	if (ret)
 		goto release_dpb_frames;
 
-	ret = pm_runtime_get_sync(dev);
+	ret = pm_runtime_resume_and_get(dev);
 	if (ret < 0)
-		goto put_runtime_pm;
+		goto unlock;
 
 	/*
 	 * We rely on the VDE registers reset value, otherwise VDE
@@ -843,6 +843,8 @@  static int tegra_vde_ioctl_decode_h264(struct tegra_vde *vde,
 put_runtime_pm:
 	pm_runtime_mark_last_busy(dev);
 	pm_runtime_put_autosuspend(dev);
+
+unlock:
 	mutex_unlock(&vde->lock);
 
 release_dpb_frames:
@@ -1069,8 +1071,8 @@  static int tegra_vde_probe(struct platform_device *pdev)
 	 * power-cycle it in order to put hardware into a predictable lower
 	 * power state.
 	 */
-	pm_runtime_get_sync(dev);
-	pm_runtime_put(dev);
+	if (pm_runtime_resume_and_get(dev) >= 0)
+		pm_runtime_put(dev);
 
 	return 0;
 
@@ -1088,8 +1090,9 @@  static int tegra_vde_remove(struct platform_device *pdev)
 {
 	struct tegra_vde *vde = platform_get_drvdata(pdev);
 	struct device *dev = &pdev->dev;
+	int ret;
 
-	pm_runtime_get_sync(dev);
+	ret = pm_runtime_resume_and_get(dev);
 	pm_runtime_dont_use_autosuspend(dev);
 	pm_runtime_disable(dev);
 
@@ -1097,7 +1100,8 @@  static int tegra_vde_remove(struct platform_device *pdev)
 	 * Balance RPM state, the VDE power domain is left ON and hardware
 	 * is clock-gated. It's safe to reboot machine now.
 	 */
-	pm_runtime_put_noidle(dev);
+	if (ret >= 0)
+		pm_runtime_put_noidle(dev);
 	clk_disable_unprepare(vde->clk);
 
 	misc_deregister(&vde->miscdev);