Skip to contentSkip to conversation

drm/msm/a6xx: check pm_runtime_resume_and_get() during resume

PATCH In review Roman Demidov 3 participants 5 messages Sep 4 – Oct 5

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/msm/a6xx: check pm_runtime_resume_and_get() during resume

The return values of pm_runtime_resume_and_get() calls in a6xx_pm_resume() are not checked, which can lead to hardware access on suspended devices and PM reference underflows.

Fix this by checking the return value of each pm_runtime_resume_and_get() call and properly unwinding the previously acquired resources on failure.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 5a903a44a984 ("drm/msm/a6xx: Introduce GMU wrapper support")
---
 drivers/gpu/drm/msm/adreno/a6xx_gpu.c | 38 +++++++++++++++------------
 1 file changed, 21 insertions(+), 17 deletions(-)
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +21 -17
index f9de9329dee3..0cf205ea744b 100644
@@ -2175,44 +2175,48 @@ static int a6xx_pm_resume(struct msm_gpu *gpu)
 	opp = dev_pm_opp_find_freq_ceil(&gpu->pdev->dev, &freq);
 	if (IS_ERR(opp)) {
 		ret = PTR_ERR(opp);
-		goto err_set_opp;
+		goto err_unlock;
 	}
 	dev_pm_opp_put(opp);
 
 	/* Set the core clock and bus bw, having VDD scaling in mind */
 	dev_pm_opp_set_opp(&gpu->pdev->dev, opp);
 
-	pm_runtime_resume_and_get(gmu->dev);
-	pm_runtime_resume_and_get(gmu->gxpd);
+	ret = pm_runtime_resume_and_get(gmu->dev);
+	if (ret < 0)
+		goto err_opp_clear;
+	ret = pm_runtime_resume_and_get(gmu->gxpd);
+	if (ret < 0)
+		goto err_put_dev;
 
 	ret = clk_bulk_prepare_enable(gpu->nr_clocks, gpu->grp_clks);
 	if (ret)
-		goto err_bulk_clk;
+		goto err_put_gxpd;
 
 	ret = clk_bulk_prepare_enable(gmu->nr_clocks, gmu->clocks);
 	if (ret) {
 		clk_bulk_disable_unprepare(gpu->nr_clocks, gpu->grp_clks);
-		goto err_bulk_clk;
+		goto err_put_gxpd;
 	}
 
 	if (adreno_is_a619_holi(adreno_gpu))
 		a6xx_sptprac_enable(gmu);
 
-	/* If anything goes south, tear the GPU down piece by piece.. */
-	if (ret) {
-err_bulk_clk:
-		pm_runtime_put(gmu->gxpd);
-		pm_runtime_put(gmu->dev);
-		dev_pm_opp_set_opp(&gpu->pdev->dev, NULL);
-	}
-err_set_opp:
 	mutex_unlock(&a6xx_gpu->gmu.lock);
+	msm_devfreq_resume(gpu);
+	a6xx_llc_activate(a6xx_gpu);
 
-	if (!ret) {
-		msm_devfreq_resume(gpu);
-		a6xx_llc_activate(a6xx_gpu);
-	}
+	return 0;
 
+	/* If anything goes south, tear the GPU down piece by piece.. */
+err_put_gxpd:
+	pm_runtime_put(gmu->gxpd);
+err_put_dev:
+	pm_runtime_put(gmu->dev);
+err_opp_clear:
+	dev_pm_opp_set_opp(&gpu->pdev->dev, NULL);
+err_unlock:
+	mutex_unlock(&a6xx_gpu->gmu.lock);
 	return ret;
 }
 
Signature · 1 line
2.53.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] Unconditional pm_runtime_resume_and_get() on gmu->gxpd causes a NULL pointer dereference if the gxpd power domain is absent.
- [High] The opp pointer is used after its reference count has been dropped, creating a Use-After-Free vulnerability. --

commit 8f5f79a7915a90cb43eb05a5eb535e070ae669bd
Author: Roman Demidov <[email protected]>

drm/msm/a6xx: check pm_runtime_resume_and_get() during resume

This commit adds return value checks to pm_runtime_resume_and_get() calls during a6xx GPU resume to prevent hardware access on suspended devices. It also unwinds acquired resources properly if these calls fail.

Quoted 5 lines
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +0 -0
index f9de9329dee39..0cf205ea744be 100644

drivers/gpu/drm/msm/adreno/a6xx_gpu.c static int a6xx_pm_resume(struct msm_gpu *gpu) · line 2183

Quoted 10 lines
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +1 -1
@@ -2175,44 +2175,48 @@ static int a6xx_pm_resume(struct msm_gpu *gpu)
 	opp = dev_pm_opp_find_freq_ceil(&gpu->pdev->dev, &freq);
 	if (IS_ERR(opp)) {
 		ret = PTR_ERR(opp);
-		goto err_set_opp;
+		goto err_unlock;
 	}
 	dev_pm_opp_put(opp);
 
 	/* Set the core clock and bus bw, having VDD scaling in mind */
 	dev_pm_opp_set_opp(&gpu->pdev->dev, opp);

[Severity: High]
This is a pre-existing issue, but does this code cause a use-after-free of the opp pointer?

The dev_pm_opp_find_freq_ceil() function returns an OPP object with an incremented reference count. Dropping this reference via dev_pm_opp_put() could cause the object to be freed if it is concurrently removed from the OPP table. Can dev_pm_opp_set_opp() then dereference the freed memory?

drivers/gpu/drm/msm/adreno/a6xx_gpu.c static int a6xx_pm_resume(struct msm_gpu *gpu) · line 2188

Quoted 7 lines
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +4 -2
-	pm_runtime_resume_and_get(gmu->dev);
-	pm_runtime_resume_and_get(gmu->gxpd);
+	ret = pm_runtime_resume_and_get(gmu->dev);
+	if (ret < 0)
+		goto err_opp_clear;
+	ret = pm_runtime_resume_and_get(gmu->gxpd);

[Severity: High]
This isn't a bug introduced by this patch, but can this cause a NULL pointer dereference if the gxpd power domain is absent?

If the driver initializes a GMU wrapper device on a platform where the device tree does not provide a 'gx' power domain, gmu->gxpd remains NULL. Calling pm_runtime_resume_and_get(NULL) will immediately dereference the pointer inside __pm_runtime_resume().

Quoted 2 lines
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +2 -0
+	if (ret < 0)
+		goto err_put_dev;

[ ... ]

Quoted 11 lines
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +2 -8
-	/* If anything goes south, tear the GPU down piece by piece.. */
-	if (ret) {
-err_bulk_clk:
-		pm_runtime_put(gmu->gxpd);
-		pm_runtime_put(gmu->dev);
-		dev_pm_opp_set_opp(&gpu->pdev->dev, NULL);
-	}
-err_set_opp:
 	mutex_unlock(&a6xx_gpu->gmu.lock);
+	msm_devfreq_resume(gpu);
+	a6xx_llc_activate(a6xx_gpu);

drivers/gpu/drm/msm/adreno/a6xx_gpu.c static int a6xx_pm_resume(struct msm_gpu *gpu) · line 2213

Quoted 10 lines
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +4 -4
 
-	if (!ret) {
-		msm_devfreq_resume(gpu);
-		a6xx_llc_activate(a6xx_gpu);
-	}
+	return 0;
 
+	/* If anything goes south, tear the GPU down piece by piece.. */
+err_put_gxpd:
+	pm_runtime_put(gmu->gxpd);

[Severity: High]
Can this error path also crash by unconditionally dereferencing a NULL gxpd pointer?

While the initial pm_runtime_resume_and_get() issue is pre-existing, this patch introduces an additional crash point here. If gmu->gxpd is NULL, this newly added pm_runtime_put() will trigger a kernel panic.

Quoted 8 lines
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +6 -0
+err_put_dev:
+	pm_runtime_put(gmu->dev);
+err_opp_clear:
+	dev_pm_opp_set_opp(&gpu->pdev->dev, NULL);
+err_unlock:
+	mutex_unlock(&a6xx_gpu->gmu.lock);
 	return ret;
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Roman Demidov
Quoted 73 lines · Roman Demidov

The return values of pm_runtime_resume_and_get() calls in a6xx_pm_resume() are not checked, which can lead to hardware access on suspended devices and PM reference underflows.

Fix this by checking the return value of each pm_runtime_resume_and_get() call and properly unwinding the previously acquired resources on failure.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 5a903a44a984 ("drm/msm/a6xx: Introduce GMU wrapper support")
---
 drivers/gpu/drm/msm/adreno/a6xx_gpu.c | 38 +++++++++++++++------------
 1 file changed, 21 insertions(+), 17 deletions(-)
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +12 -17
index f9de9329dee3..0cf205ea744b 100644
@@ -2175,44 +2175,48 @@ static int a6xx_pm_resume(struct msm_gpu *gpu)
        opp = dev_pm_opp_find_freq_ceil(&gpu->pdev->dev, &freq);
        if (IS_ERR(opp)) {
                ret = PTR_ERR(opp);
-               goto err_set_opp;
+               goto err_unlock;
        }
        dev_pm_opp_put(opp);
 
        /* Set the core clock and bus bw, having VDD scaling in mind */
        dev_pm_opp_set_opp(&gpu->pdev->dev, opp);
 
-       pm_runtime_resume_and_get(gmu->dev);
-       pm_runtime_resume_and_get(gmu->gxpd);
+       ret = pm_runtime_resume_and_get(gmu->dev);
+       if (ret < 0)
+               goto err_opp_clear;
+       ret = pm_runtime_resume_and_get(gmu->gxpd);
+       if (ret < 0)
+               goto err_put_dev;
 
        ret = clk_bulk_prepare_enable(gpu->nr_clocks, gpu->grp_clks);
        if (ret)
-               goto err_bulk_clk;
+               goto err_put_gxpd;
 
        ret = clk_bulk_prepare_enable(gmu->nr_clocks, gmu->clocks);
        if (ret) {
                clk_bulk_disable_unprepare(gpu->nr_clocks, gpu->grp_clks);
-               goto err_bulk_clk;
+               goto err_put_gxpd;
        }
 
        if (adreno_is_a619_holi(adreno_gpu))
                a6xx_sptprac_enable(gmu);
 
-       /* If anything goes south, tear the GPU down piece by piece.. */
-       if (ret) {
-err_bulk_clk:
-               pm_runtime_put(gmu->gxpd);
-               pm_runtime_put(gmu->dev);
-               dev_pm_opp_set_opp(&gpu->pdev->dev, NULL);
-       }
-err_set_opp:
        mutex_unlock(&a6xx_gpu->gmu.lock);
+       msm_devfreq_resume(gpu);
+       a6xx_llc_activate(a6xx_gpu);
 
-       if (!ret) {
-               msm_devfreq_resume(gpu);
-               a6xx_llc_activate(a6xx_gpu);
-       }
+       return 0;

drivers/gpu/drm/msm/adreno/a6xx_gpu.c static int a6xx_pm_resume(struct msm_gpu *gpu) · line 2219

Quoted 10 lines
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +9 -0
 
+       /* If anything goes south, tear the GPU down piece by piece.. */
+err_put_gxpd:
+       pm_runtime_put(gmu->gxpd);
+err_put_dev:
+       pm_runtime_put(gmu->dev);
+err_opp_clear:
+       dev_pm_opp_set_opp(&gpu->pdev->dev, NULL);
+err_unlock:
+       mutex_unlock(&a6xx_gpu->gmu.lock);

this does at least look a bit less terrifying than what came before... but maybe

   guard(mutex)(&a6xx_gpu->gmu.lock);

to simplify the locking part of this. And I think at least some of the runpm stuff could also be handled w/ guard/cleanup stuff?

BR,
-R

Quoted 6 lines
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +0 -1
        return ret;
 }
 
--

2.53.0

author
#3 lore raw

Hi Rob,

Thanks. I did consider guard/cleanup here.

A plain guard(mutex)(&a6xx_gpu->gmu.lock) would remove the manual unlock/err_unlock, but it would also keep gmu.lock held until the function returns, i.e. across msm_devfreq_resume() and a6xx_llc_activate(). The current code drops the lock before those calls, and I didn't want to change that as part of this fix.

scoped_guard() can preserve the original lock scope, but the error paths still need to unwind the PM refs and clear the OPP in the right order. Doing that with labels inside the scoped block and a success label outside ends up more convoluted than the current linear unwind. We'd still have goto, just with extra nesting/state.

I also considered PM_RUNTIME_ACQUIRE(). The auto-cleanup for pm_runtime_put() is nice, but it doesn't remove the goto-based unwind for the clk_bulk_prepare_enable() failures. If the GPU clock enable or GMU clock enable fails, we still need to unwind the previously enabled clocks, clear the OPP, and drop the mutex. So we'd still need a goto.

That said, if you'd prefer I switch the two pm_runtime_resume_and_get() calls to PM_RUNTIME_ACQUIRE() and keep the explicit clock/OPP unwind, I can send a v2. Or I can respin with guard(mutex) if holding the lock across those calls is acceptable.

BR,
Roman

сб, 12 сент. 2026 г. в 04:55, Rob Clark <[email protected]>:

Quoted 103 lines · Roman Demidov
Quoted 73 lines

The return values of pm_runtime_resume_and_get() calls in a6xx_pm_resume() are not checked, which can lead to hardware access on suspended devices and PM reference underflows.

Fix this by checking the return value of each pm_runtime_resume_and_get() call and properly unwinding the previously acquired resources on failure.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 5a903a44a984 ("drm/msm/a6xx: Introduce GMU wrapper support")
---
 drivers/gpu/drm/msm/adreno/a6xx_gpu.c | 38 +++++++++++++++------------
 1 file changed, 21 insertions(+), 17 deletions(-)
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +12 -17
index f9de9329dee3..0cf205ea744b 100644
@@ -2175,44 +2175,48 @@ static int a6xx_pm_resume(struct msm_gpu *gpu)
        opp = dev_pm_opp_find_freq_ceil(&gpu->pdev->dev, &freq);
        if (IS_ERR(opp)) {
                ret = PTR_ERR(opp);
-               goto err_set_opp;
+               goto err_unlock;
        }
        dev_pm_opp_put(opp);
 
        /* Set the core clock and bus bw, having VDD scaling in mind */
        dev_pm_opp_set_opp(&gpu->pdev->dev, opp);
 
-       pm_runtime_resume_and_get(gmu->dev);
-       pm_runtime_resume_and_get(gmu->gxpd);
+       ret = pm_runtime_resume_and_get(gmu->dev);
+       if (ret < 0)
+               goto err_opp_clear;
+       ret = pm_runtime_resume_and_get(gmu->gxpd);
+       if (ret < 0)
+               goto err_put_dev;
 
        ret = clk_bulk_prepare_enable(gpu->nr_clocks, gpu->grp_clks);
        if (ret)
-               goto err_bulk_clk;
+               goto err_put_gxpd;
 
        ret = clk_bulk_prepare_enable(gmu->nr_clocks, gmu->clocks);
        if (ret) {
                clk_bulk_disable_unprepare(gpu->nr_clocks, gpu->grp_clks);
-               goto err_bulk_clk;
+               goto err_put_gxpd;
        }
 
        if (adreno_is_a619_holi(adreno_gpu))
                a6xx_sptprac_enable(gmu);
 
-       /* If anything goes south, tear the GPU down piece by piece.. */
-       if (ret) {
-err_bulk_clk:
-               pm_runtime_put(gmu->gxpd);
-               pm_runtime_put(gmu->dev);
-               dev_pm_opp_set_opp(&gpu->pdev->dev, NULL);
-       }
-err_set_opp:
        mutex_unlock(&a6xx_gpu->gmu.lock);
+       msm_devfreq_resume(gpu);
+       a6xx_llc_activate(a6xx_gpu);
 
-       if (!ret) {
-               msm_devfreq_resume(gpu);
-               a6xx_llc_activate(a6xx_gpu);
-       }
+       return 0;

drivers/gpu/drm/msm/adreno/a6xx_gpu.c static int a6xx_pm_resume(struct msm_gpu *gpu) · line 2219

Quoted 10 lines
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +9 -0
 
+       /* If anything goes south, tear the GPU down piece by piece.. */
+err_put_gxpd:
+       pm_runtime_put(gmu->gxpd);
+err_put_dev:
+       pm_runtime_put(gmu->dev);
+err_opp_clear:
+       dev_pm_opp_set_opp(&gpu->pdev->dev, NULL);
+err_unlock:
+       mutex_unlock(&a6xx_gpu->gmu.lock);

this does at least look a bit less terrifying than what came before... but maybe

   guard(mutex)(&a6xx_gpu->gmu.lock);

to simplify the locking part of this. And I think at least some of the runpm stuff could also be handled w/ guard/cleanup stuff?

BR,
-R

Quoted 6 lines
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +0 -1
        return ret;
 }
 
--

2.53.0

#4 lore raw
Quoted 23 lines · Roman

Hi Rob,

Thanks. I did consider guard/cleanup here.

A plain guard(mutex)(&a6xx_gpu->gmu.lock) would remove the manual unlock/err_unlock, but it would also keep gmu.lock held until the function returns, i.e. across msm_devfreq_resume() and a6xx_llc_activate(). The current code drops the lock before those calls, and I didn't want to change that as part of this fix.

scoped_guard() can preserve the original lock scope, but the error paths still need to unwind the PM refs and clear the OPP in the right order. Doing that with labels inside the scoped block and a success label outside ends up more convoluted than the current linear unwind. We'd still have goto, just with extra nesting/state.

I also considered PM_RUNTIME_ACQUIRE(). The auto-cleanup for pm_runtime_put() is nice, but it doesn't remove the goto-based unwind for the clk_bulk_prepare_enable() failures. If the GPU clock enable or GMU clock enable fails, we still need to unwind the previously enabled clocks, clear the OPP, and drop the mutex. So we'd still need a goto.

Quoted 4 lines

That said, if you'd prefer I switch the two pm_runtime_resume_and_get() calls to PM_RUNTIME_ACQUIRE() and keep the explicit clock/OPP unwind, I can send a v2. Or I can respin with guard(mutex) if holding the lock across those calls is acceptable.

Ahh, right, holding the lock across devfreq call would be bad. So disregard my suggestion, I think it turns into a bit more of a refactor than what I had in mind.

BR,
-R

Quoted 107 lines

BR,
Roman

сб, 12 сент. 2026 г. в 04:55, Rob Clark <[email protected]>:

Quoted 103 lines · Roman Demidov
Quoted 73 lines

The return values of pm_runtime_resume_and_get() calls in a6xx_pm_resume() are not checked, which can lead to hardware access on suspended devices and PM reference underflows.

Fix this by checking the return value of each pm_runtime_resume_and_get() call and properly unwinding the previously acquired resources on failure.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 5a903a44a984 ("drm/msm/a6xx: Introduce GMU wrapper support")
---
 drivers/gpu/drm/msm/adreno/a6xx_gpu.c | 38 +++++++++++++++------------
 1 file changed, 21 insertions(+), 17 deletions(-)
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +12 -17
index f9de9329dee3..0cf205ea744b 100644
@@ -2175,44 +2175,48 @@ static int a6xx_pm_resume(struct msm_gpu *gpu)
        opp = dev_pm_opp_find_freq_ceil(&gpu->pdev->dev, &freq);
        if (IS_ERR(opp)) {
                ret = PTR_ERR(opp);
-               goto err_set_opp;
+               goto err_unlock;
        }
        dev_pm_opp_put(opp);
 
        /* Set the core clock and bus bw, having VDD scaling in mind */
        dev_pm_opp_set_opp(&gpu->pdev->dev, opp);
 
-       pm_runtime_resume_and_get(gmu->dev);
-       pm_runtime_resume_and_get(gmu->gxpd);
+       ret = pm_runtime_resume_and_get(gmu->dev);
+       if (ret < 0)
+               goto err_opp_clear;
+       ret = pm_runtime_resume_and_get(gmu->gxpd);
+       if (ret < 0)
+               goto err_put_dev;
 
        ret = clk_bulk_prepare_enable(gpu->nr_clocks, gpu->grp_clks);
        if (ret)
-               goto err_bulk_clk;
+               goto err_put_gxpd;
 
        ret = clk_bulk_prepare_enable(gmu->nr_clocks, gmu->clocks);
        if (ret) {
                clk_bulk_disable_unprepare(gpu->nr_clocks, gpu->grp_clks);
-               goto err_bulk_clk;
+               goto err_put_gxpd;
        }
 
        if (adreno_is_a619_holi(adreno_gpu))
                a6xx_sptprac_enable(gmu);
 
-       /* If anything goes south, tear the GPU down piece by piece.. */
-       if (ret) {
-err_bulk_clk:
-               pm_runtime_put(gmu->gxpd);
-               pm_runtime_put(gmu->dev);
-               dev_pm_opp_set_opp(&gpu->pdev->dev, NULL);
-       }
-err_set_opp:
        mutex_unlock(&a6xx_gpu->gmu.lock);
+       msm_devfreq_resume(gpu);
+       a6xx_llc_activate(a6xx_gpu);
 
-       if (!ret) {
-               msm_devfreq_resume(gpu);
-               a6xx_llc_activate(a6xx_gpu);
-       }
+       return 0;

drivers/gpu/drm/msm/adreno/a6xx_gpu.c static int a6xx_pm_resume(struct msm_gpu *gpu) · line 2219

Quoted 10 lines
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +9 -0
 
+       /* If anything goes south, tear the GPU down piece by piece.. */
+err_put_gxpd:
+       pm_runtime_put(gmu->gxpd);
+err_put_dev:
+       pm_runtime_put(gmu->dev);
+err_opp_clear:
+       dev_pm_opp_set_opp(&gpu->pdev->dev, NULL);
+err_unlock:
+       mutex_unlock(&a6xx_gpu->gmu.lock);

this does at least look a bit less terrifying than what came before... but maybe

   guard(mutex)(&a6xx_gpu->gmu.lock);

to simplify the locking part of this. And I think at least some of the runpm stuff could also be handled w/ guard/cleanup stuff?

BR,
-R

Quoted 6 lines
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +0 -1
        return ret;
 }
 
--

2.53.0