[PATCH v2 2/2] hwrng: mtk - enable runtime PM before registering the RNG

From: Gu Bin

Date: Wed Nov 11 2026 - 04:20:08 EST


mtk_rng_probe() registers the RNG before setting up runtime PM (and
before dev_set_drvdata()). devm_hwrng_register() immediately exposes
the RNG: hwrng_register() makes it current when it has the best
quality and starts the hwrng_fillfn kthread right away, so
mtk_rng_read() can run in the window where runtime PM is not enabled
yet - pm_runtime_resume_and_get() then fails with -EACCES.

On unbind the devres teardown runs in reverse order: the RNG is
unregistered after runtime PM has been disabled, so the same failure
is possible during teardown.

Move the drvdata assignment and the runtime PM setup above
devm_hwrng_register(). The runtime PM resume callback needs the
drvdata, so the assignment has to come first anyway, and with this
order the devres teardown also unregisters the RNG before it disables
runtime PM, closing both windows. The SMC variant keeps skipping
runtime PM.

Fixes: 81d2b34508c6 ("hwrng: mtk - add runtime PM support")
Fixes: 78cb66caa6ab ("hwrng: mtk - Use devm_pm_runtime_enable")
Signed-off-by: Gu Bin <gubin@xxxxxxxxxxxxxxxxxxxx>
---
drivers/char/hw_random/mtk-rng.c | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/drivers/char/hw_random/mtk-rng.c b/drivers/char/hw_random/mtk-rng.c
index 1e8e458dab25..f337473ba99f 100644
--- a/drivers/char/hw_random/mtk-rng.c
+++ b/drivers/char/hw_random/mtk-rng.c
@@ -212,13 +212,6 @@ static int mtk_rng_probe(struct platform_device *pdev)
priv->rng.read = mtk_rng_read;
}

- ret = devm_hwrng_register(&pdev->dev, &priv->rng);
- if (ret) {
- dev_err(&pdev->dev, "failed to register rng device: %d\n",
- ret);
- return ret;
- }
-
if (!(priv->flags & MTK_RNG_SMC)) {
dev_set_drvdata(&pdev->dev, priv);
pm_runtime_set_autosuspend_delay(&pdev->dev,
@@ -229,6 +222,13 @@ static int mtk_rng_probe(struct platform_device *pdev)
return ret;
}

+ ret = devm_hwrng_register(&pdev->dev, &priv->rng);
+ if (ret) {
+ dev_err(&pdev->dev, "failed to register rng device: %d\n",
+ ret);
+ return ret;
+ }
+
dev_info(&pdev->dev, "registered RNG driver\n");

return 0;
--
2.55.0.windows.3