]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
hwrng: jh7110 - fix refcount leak in starfive_trng_read()
authorWentao Liang <vulab@iscas.ac.cn>
Wed, 3 Jun 2026 11:03:27 +0000 (11:03 +0000)
committerHerbert Xu <herbert@gondor.apana.org.au>
Fri, 12 Jun 2026 01:56:45 +0000 (09:56 +0800)
The starfive_trng_read() function acquires a runtime PM reference
via pm_runtime_get_sync() but fails to release it on two error
paths.  If starfive_trng_wait_idle() or starfive_trng_cmd() returns
an error, the function exits without calling
pm_runtime_put_sync_autosuspend(), leaving the runtime PM usage
counter permanently elevated and preventing the device from entering
runtime suspend.

Refactor the function to use a unified error path that calls
pm_runtime_put_sync_autosuspend() before returning.

Cc: stable@vger.kernel.org
Fixes: c388f458bc34 ("hwrng: starfive - Add TRNG driver for StarFive SoC")
Signed-off-by: Wentao Liang <vulab@iscas.ac.cn>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
drivers/char/hw_random/jh7110-trng.c

index 9776f4daa044590af06c0a4b8b19db1e2ac1b567..4712c3c530e42c7cec8822d85a134d7c76381230 100644 (file)
@@ -256,19 +256,22 @@ static int starfive_trng_read(struct hwrng *rng, void *buf, size_t max, bool wai
 
        if (wait) {
                ret = starfive_trng_wait_idle(trng);
-               if (ret)
-                       return -ETIMEDOUT;
+               if (ret) {
+                       ret = -ETIMEDOUT;
+                       goto out_put;
+               }
        }
 
        ret = starfive_trng_cmd(trng, STARFIVE_CTRL_GENE_RANDNUM, wait);
        if (ret)
-               return ret;
+               goto out_put;
 
        memcpy_fromio(buf, trng->base + STARFIVE_RAND0, max);
+       ret = max;
 
+out_put:
        pm_runtime_put_sync_autosuspend(trng->dev);
-
-       return max;
+       return ret;
 }
 
 static int starfive_trng_probe(struct platform_device *pdev)