[PATCH] hwrng: starfive: fix runtime PM autosuspend cleanup

Guangshuo Li posted 1 patch 1 month, 3 weeks ago
drivers/char/hw_random/jh7110-trng.c | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)
[PATCH] hwrng: starfive: fix runtime PM autosuspend cleanup
Posted by Guangshuo Li 1 month, 3 weeks ago
starfive_trng_probe() calls pm_runtime_use_autosuspend(), but runtime
PM is enabled with pm_runtime_enable() without a corresponding
pm_runtime_dont_use_autosuspend() during driver teardown.

If the autosuspend delay is set to a negative value while autosuspend
is enabled, the runtime PM core increments usage_count to prevent
runtime suspend. Without undoing the autosuspend setting during
teardown, this reference can remain unbalanced.

Use devm_pm_runtime_enable() so that runtime PM and the autosuspend
setting are automatically cleaned up on probe failure and device
removal. Remove the explicit pm_runtime_disable() from the hwrng
registration failure path since the managed cleanup handles it.

This issue was found by manual code inspection.

Fixes: c388f458bc34 ("hwrng: starfive - Add TRNG driver for StarFive SoC")
Cc: stable@vger.kernel.org
Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
---
 drivers/char/hw_random/jh7110-trng.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/drivers/char/hw_random/jh7110-trng.c b/drivers/char/hw_random/jh7110-trng.c
index 4712c3c530e4..34b2ec3e13b3 100644
--- a/drivers/char/hw_random/jh7110-trng.c
+++ b/drivers/char/hw_random/jh7110-trng.c
@@ -336,12 +336,18 @@ static int starfive_trng_probe(struct platform_device *pdev)
 
 	pm_runtime_use_autosuspend(&pdev->dev);
 	pm_runtime_set_autosuspend_delay(&pdev->dev, 100);
-	pm_runtime_enable(&pdev->dev);
+	ret = devm_pm_runtime_enable(&pdev->dev);
+	if (ret) {
+		reset_control_assert(trng->rst);
+		clk_disable_unprepare(trng->ahb);
+		clk_disable_unprepare(trng->hclk);
+
+		return dev_err_probe(&pdev->dev, ret,
+				     "Failed to enable runtime PM\n");
+	}
 
 	ret = devm_hwrng_register(&pdev->dev, &trng->rng);
 	if (ret) {
-		pm_runtime_disable(&pdev->dev);
-
 		reset_control_assert(trng->rst);
 		clk_disable_unprepare(trng->ahb);
 		clk_disable_unprepare(trng->hclk);
-- 
2.43.0