Skip to contentSkip to conversation

media: hantro: release runtime resources when device_run fails

PATCH Superseded Tharit Tangkijwanichakul 4 participants 13 messages Jul 24 – Jul 29

Subsystems (3)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: hantro: release runtime resources when device_run fails

device_run() acquires a runtime PM reference and enables the VPU clocks before invoking the codec-specific run callback.

If clk_bulk_enable() fails, the runtime PM reference is left held. If the codec-specific run callback fails, both the enabled clocks and the runtime PM reference are left held.

Add separate error paths to release the resources acquired by device_run(). Disable the clocks when the codec run callback fails, and drop the runtime PM reference when either clock enabling or the codec run callback fails.

Fixes 775fec69008d ("media: add Rockchip VPU JPEG encoder driver")

---
Tested on a Rockchip RK3588 (Rock 5B) board with Fluster:
H.264 (JVT-AVC_V1): 129/135, unchanged
MPEG-2 (MPEG2_VIDEO-MAIN): 23/43, unchanged
VP8 (VP8-TEST-VECTORS): 61/61, unchanged

 drivers/media/platform/verisilicon/hantro_drv.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/media/platform/verisilicon/hantro_drv.c +8 -3
index 2e81877f640f..9daaf129211d 100644
@@ -170,6 +170,7 @@ void hantro_end_prepare_run(struct hantro_ctx *ctx)
 static void device_run(void *priv)
 {
 	struct hantro_ctx *ctx = priv;
+	struct hantro_dev *vpu = ctx->dev;
 	struct vb2_v4l2_buffer *src, *dst;
 	int ret;
 
@@ -178,11 +179,11 @@ static void device_run(void *priv)
 
 	ret = pm_runtime_resume_and_get(ctx->dev->dev);
 	if (ret < 0)
-		goto err_cancel_job;
+		goto err_disable_clock;
 
 	ret = clk_bulk_enable(ctx->dev->variant->num_clocks, ctx->dev->clocks);
 	if (ret)
-		goto err_cancel_job;
+		goto err_pm_put_autosuspend;
 
 	v4l2_m2m_buf_copy_metadata(src, dst);
 
@@ -191,8 +192,12 @@ static void device_run(void *priv)
 
 	return;
 
+err_disable_clock:
+	clk_bulk_disable(vpu->variant->num_clocks, ctx->dev->clocks);
+err_pm_put_autosuspend:
+	pm_runtime_put_autosuspend(vpu->dev);
 err_cancel_job:
-	hantro_job_finish_no_pm(ctx->dev, ctx, VB2_BUF_STATE_ERROR);
+	hantro_job_finish_no_pm(vpu, ctx, VB2_BUF_STATE_ERROR);
 }
 
 static const struct v4l2_m2m_ops vpu_m2m_ops = {

base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482

Signature · 1 line
2.47.3
maintainer
#1 lore raw
Quoted 36 lines · Tharit Tangkijwanichakul

device_run() acquires a runtime PM reference and enables the VPU clocks before invoking the codec-specific run callback.

If clk_bulk_enable() fails, the runtime PM reference is left held. If the codec-specific run callback fails, both the enabled clocks and the runtime PM reference are left held.

Add separate error paths to release the resources acquired by device_run(). Disable the clocks when the codec run callback fails, and drop the runtime PM reference when either clock enabling or the codec run callback fails.

Fixes 775fec69008d ("media: add Rockchip VPU JPEG encoder driver")

---
Tested on a Rockchip RK3588 (Rock 5B) board with Fluster:
H.264 (JVT-AVC_V1): 129/135, unchanged
MPEG-2 (MPEG2_VIDEO-MAIN): 23/43, unchanged
VP8 (VP8-TEST-VECTORS): 61/61, unchanged

 drivers/media/platform/verisilicon/hantro_drv.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/media/platform/verisilicon/hantro_drv.c +1 -0
index 2e81877f640f..9daaf129211d 100644
@@ -170,6 +170,7 @@ void hantro_end_prepare_run(struct hantro_ctx *ctx)
 static void device_run(void *priv)
 {
 	struct hantro_ctx *ctx = priv;
+	struct hantro_dev *vpu = ctx->dev;
 	struct vb2_v4l2_buffer *src, *dst;
 	int ret;
 

drivers/media/platform/verisilicon/hantro_drv.c static void device_run(void *priv) · line 182

Quoted 5 lines
drivers/media/platform/verisilicon/hantro_drv.c +1 -1
@@ -178,11 +179,11 @@ static void device_run(void *priv)
 
 	ret = pm_runtime_resume_and_get(ctx->dev->dev);
 	if (ret < 0)
-		goto err_cancel_job;
+		goto err_disable_clock;

This doesn't make any sense.

drivers/media/platform/verisilicon/hantro_drv.c static void device_run(void *priv) · line 186

Quoted 4 lines
drivers/media/platform/verisilicon/hantro_drv.c +1 -1
 	ret = clk_bulk_enable(ctx->dev->variant->num_clocks, ctx->dev->clocks);
 	if (ret)
-		goto err_cancel_job;
+		goto err_pm_put_autosuspend;

This looks fine to me.

drivers/media/platform/verisilicon/hantro_drv.c static void device_run(void *priv) · line 188

Quoted 1 line
drivers/media/platform/verisilicon/hantro_drv.c +0 -0
 	v4l2_m2m_buf_copy_metadata(src, dst);

But right below this, you are still letting

	if (ctx->codec_ops->run(ctx))
		goto err_cancel_job;

without disabling the clocks.

drivers/media/platform/verisilicon/hantro_drv.c static void device_run(void *priv) · line 196

Quoted 7 lines
drivers/media/platform/verisilicon/hantro_drv.c +2 -0
@@ -191,8 +192,12 @@ static void device_run(void *priv)
 
 	return;
 
+err_disable_clock:
+	clk_bulk_disable(vpu->variant->num_clocks, ctx->dev->clocks);

You have added vpu = ctx->dev, why not use vpu->clocks as second parameter?

drivers/media/platform/verisilicon/hantro_drv.c static void device_run(void *priv) · line 200

Quoted 5 lines
drivers/media/platform/verisilicon/hantro_drv.c +3 -1
+err_pm_put_autosuspend:
+	pm_runtime_put_autosuspend(vpu->dev);
 err_cancel_job:
-	hantro_job_finish_no_pm(ctx->dev, ctx, VB2_BUF_STATE_ERROR);
+	hantro_job_finish_no_pm(vpu, ctx, VB2_BUF_STATE_ERROR);

Why are you changing ctx->dev to vpu here, but not in the other function calls in device_run(), e.g. pm_runtime_resume_and_get() and clk_bulk_enable() above?

regards
Philipp

#2 lore raw
Quoted 24 lines

But right below this, you are still letting

        if (ctx->codec_ops->run(ctx))
                goto err_cancel_job;

without disabling the clocks.

drivers/media/platform/verisilicon/hantro_drv.c static void device_run(void *priv) · line 196

Quoted 7 lines
drivers/media/platform/verisilicon/hantro_drv.c +2 -0
@@ -191,8 +192,12 @@ static void device_run(void *priv)
 
      return;
 
+err_disable_clock:
+     clk_bulk_disable(vpu->variant->num_clocks, ctx->dev->clocks);

You have added vpu = ctx->dev, why not use vpu->clocks as second parameter?

drivers/media/platform/verisilicon/hantro_drv.c static void device_run(void *priv) · line 200

Quoted 5 lines
drivers/media/platform/verisilicon/hantro_drv.c +3 -1
+err_pm_put_autosuspend:
+     pm_runtime_put_autosuspend(vpu->dev);
 err_cancel_job:
-     hantro_job_finish_no_pm(ctx->dev, ctx, VB2_BUF_STATE_ERROR);
+     hantro_job_finish_no_pm(vpu, ctx, VB2_BUF_STATE_ERROR);
Quoted 6 lines

Why are you changing ctx->dev to vpu here, but not in the other function calls in device_run(), e.g. pm_runtime_resume_and_get() and clk_bulk_enable() above?

regards
Philipp

Hi Philipp,

This is embarrassing. I did notice this, made a local change and forgot to re-run git add.
I will fix this asap and send a v2.

Best,
Tharit

#3 lore raw

PATCH v2 1/1 media: hantro: release runtime resources when device_run fails

device_run() acquires a runtime PM reference and enables the VPU clocks before invoking the codec-specific run callback.

If clk_bulk_enable() fails, the runtime PM reference is left held. If the codec-specific run callback fails, both the enabled clocks and the runtime PM reference are left held.

Add separate error paths to release the resources acquired by device_run(). Disable the clocks when the codec run callback fails, and drop the runtime PM reference when either clock enabling or the codec run callback fails.

Fixes 775fec69008d ("media: add Rockchip VPU JPEG encoder driver")

---
Changes in v2:
- Fix the codec run failure path to disable the clocks before dropping the runtime PM reference.
- Use a local vpu variable in device_run().

Tested on a Rockchip RK3588 (Rock 5B) board with Fluster:
H.264 (JVT-AVC_V1): 129/135, unchanged
MPEG-2 (MPEG2_VIDEO-MAIN): 23/43, unchanged
VP8 (VP8-TEST-VECTORS): 61/61, unchanged

 drivers/media/platform/verisilicon/hantro_drv.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
drivers/media/platform/verisilicon/hantro_drv.c +10 -5
index 2e81877f640f..72250bb56872 100644
@@ -170,29 +170,34 @@ void hantro_end_prepare_run(struct hantro_ctx *ctx)
 static void device_run(void *priv)
 {
 	struct hantro_ctx *ctx = priv;
+	struct hantro_dev *vpu = ctx->dev;
 	struct vb2_v4l2_buffer *src, *dst;
 	int ret;
 
 	src = hantro_get_src_buf(ctx);
 	dst = hantro_get_dst_buf(ctx);
 
-	ret = pm_runtime_resume_and_get(ctx->dev->dev);
+	ret = pm_runtime_resume_and_get(vpu->dev);
 	if (ret < 0)
 		goto err_cancel_job;
 
-	ret = clk_bulk_enable(ctx->dev->variant->num_clocks, ctx->dev->clocks);
+	ret = clk_bulk_enable(vpu->variant->num_clocks, vpu->clocks);
 	if (ret)
-		goto err_cancel_job;
+		goto err_pm_put_autosuspend;
 
 	v4l2_m2m_buf_copy_metadata(src, dst);
 
 	if (ctx->codec_ops->run(ctx))
-		goto err_cancel_job;
+		goto err_disable_clock;
 
 	return;
 
+err_disable_clock:
+	clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
+err_pm_put_autosuspend:
+	pm_runtime_put_autosuspend(vpu->dev);
 err_cancel_job:
-	hantro_job_finish_no_pm(ctx->dev, ctx, VB2_BUF_STATE_ERROR);
+	hantro_job_finish_no_pm(vpu, ctx, VB2_BUF_STATE_ERROR);
 }
 
 static const struct v4l2_m2m_ops vpu_m2m_ops = {
Signature · 1 line
2.47.3
#11 lore raw
In reply to Tharit Tangkijwanichakul
Quoted 36 lines · Tharit Tangkijwanichakul

device_run() acquires a runtime PM reference and enables the VPU clocks before invoking the codec-specific run callback.

If clk_bulk_enable() fails, the runtime PM reference is left held. If the codec-specific run callback fails, both the enabled clocks and the runtime PM reference are left held.

Add separate error paths to release the resources acquired by device_run(). Disable the clocks when the codec run callback fails, and drop the runtime PM reference when either clock enabling or the codec run callback fails.

Fixes 775fec69008d ("media: add Rockchip VPU JPEG encoder driver")

---
Tested on a Rockchip RK3588 (Rock 5B) board with Fluster:
H.264 (JVT-AVC_V1): 129/135, unchanged
MPEG-2 (MPEG2_VIDEO-MAIN): 23/43, unchanged
VP8 (VP8-TEST-VECTORS): 61/61, unchanged

 drivers/media/platform/verisilicon/hantro_drv.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/media/platform/verisilicon/hantro_drv.c +1 -0
index 2e81877f640f..9daaf129211d 100644
@@ -170,6 +170,7 @@ void hantro_end_prepare_run(struct hantro_ctx *ctx)
 static void device_run(void *priv)
 {
 	struct hantro_ctx *ctx = priv;
+	struct hantro_dev *vpu = ctx->dev;
 	struct vb2_v4l2_buffer *src, *dst;
 	int ret;
 

drivers/media/platform/verisilicon/hantro_drv.c static void device_run(void *priv) · line 186

Quoted 10 lines
drivers/media/platform/verisilicon/hantro_drv.c +2 -2
@@ -178,11 +179,11 @@ static void device_run(void *priv)
 
 	ret = pm_runtime_resume_and_get(ctx->dev->dev);
 	if (ret < 0)
-		goto err_cancel_job;
+		goto err_disable_clock;
 
 	ret = clk_bulk_enable(ctx->dev->variant->num_clocks, ctx->dev->clocks);
 	if (ret)
-		goto err_cancel_job;
+		goto err_pm_put_autosuspend;

Mix use runtime pm and clock is not good.

You can put clk_bulk_enable() into runtime pm resume call back and clk_bulk_disable() into runtime pm suspend call back.

Frank

Quoted 22 lines
drivers/media/platform/verisilicon/hantro_drv.c +5 -1
 	v4l2_m2m_buf_copy_metadata(src, dst);
 
@@ -191,8 +192,12 @@ static void device_run(void *priv)
 
 	return;
 
+err_disable_clock:
+	clk_bulk_disable(vpu->variant->num_clocks, ctx->dev->clocks);
+err_pm_put_autosuspend:
+	pm_runtime_put_autosuspend(vpu->dev);
 err_cancel_job:
-	hantro_job_finish_no_pm(ctx->dev, ctx, VB2_BUF_STATE_ERROR);
+	hantro_job_finish_no_pm(vpu, ctx, VB2_BUF_STATE_ERROR);
 }
 
 static const struct v4l2_m2m_ops vpu_m2m_ops = {

base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482 --
2.47.3

#12 lore raw
Quoted 6 lines

Mix use runtime pm and clock is not good.

You can put clk_bulk_enable() into runtime pm resume call back and clk_bulk_disable() into runtime pm suspend call back.

Frank

Hi Frank,

Thanks for the suggestion. I will inspect the hantro pm_runtime call sites and let me implement your suggestion into v3.

Best,
Tharit

#4 lore raw

PATCH v3 1/1 media: hantro: release runtime resources when device_run fails

In reply to Tharit Tangkijwanichakul

device_run() acquires a runtime PM reference and enables the VPU clocks before invoking the codec-specific run callback.

If clk_bulk_enable() fails, the runtime PM reference is left held. If the codec-specific run callback fails, both the enabled clocks and the runtime PM reference are left held.

Make the clocks part of the device's runtime PM state by enabling them from the runtime-resume callback and disabling them from the runtime-suspend callback.

Fixes 775fec69008d ("media: add Rockchip VPU JPEG encoder driver")

---
Changes in v3:
- Move clock enable and disable operations into the runtime PM callbacks, as suggested by Frank Li.
- Remove vpu from argument in hantro_job_finish() and hantro_job_finish_no_pm(). It is accessible by ctx.

Changes in v2:
- Fix the codec run failure path to disable the clocks before dropping the runtime PM reference.
- Use a local vpu variable in device_run().

Tested on a ROCK 5B (RK3588) using the Hantro G1 decoder and Fluster:

H.264 (JVT-AVC_V1): 129/135, unchanged
MPEG-2 (MPEG2_VIDEO-MAIN): 23/43, unchanged
VP8 (VP8-TEST-VECTORS): 61/61, unchanged

 .../media/platform/verisilicon/hantro_drv.c   | 68 +++++++++++--------
 1 file changed, 39 insertions(+), 29 deletions(-)
drivers/media/platform/verisilicon/hantro_drv.c +39 -29
index 2e81877f640f..7978bb72569e 100644
@@ -59,8 +59,7 @@ static const struct v4l2_event hantro_eos_event = {
 	.type = V4L2_EVENT_EOS
 };
 
-static void hantro_job_finish_no_pm(struct hantro_dev *vpu,
-				    struct hantro_ctx *ctx,
+static void hantro_job_finish_no_pm(struct hantro_ctx *ctx,
 				    enum vb2_buffer_state result)
 {
 	struct vb2_v4l2_buffer *src, *dst;
@@ -86,15 +85,13 @@ static void hantro_job_finish_no_pm(struct hantro_dev *vpu,
 					 result);
 }
 
-static void hantro_job_finish(struct hantro_dev *vpu,
-			      struct hantro_ctx *ctx,
+static void hantro_job_finish(struct hantro_ctx *ctx,
 			      enum vb2_buffer_state result)
 {
-	pm_runtime_put_autosuspend(vpu->dev);
-
-	clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
+	struct hantro_dev *vpu = ctx->dev;
 
-	hantro_job_finish_no_pm(vpu, ctx, result);
+	pm_runtime_put_autosuspend(vpu->dev);
+	hantro_job_finish_no_pm(ctx, result);
 }
 
 void hantro_irq_done(struct hantro_dev *vpu,
@@ -111,7 +108,7 @@ void hantro_irq_done(struct hantro_dev *vpu,
 	if (cancel_delayed_work(&vpu->watchdog_work)) {
 		if (result == VB2_BUF_STATE_DONE && ctx->codec_ops->done)
 			ctx->codec_ops->done(ctx);
-		hantro_job_finish(vpu, ctx, result);
+		hantro_job_finish(ctx, result);
 	}
 }
 
@@ -127,7 +124,7 @@ void hantro_watchdog(struct work_struct *work)
 		vpu_err("frame processing timed out!\n");
 		if (ctx->codec_ops->reset)
 			ctx->codec_ops->reset(ctx);
-		hantro_job_finish(vpu, ctx, VB2_BUF_STATE_ERROR);
+		hantro_job_finish(ctx, VB2_BUF_STATE_ERROR);
 	}
 }
 
@@ -170,29 +167,24 @@ void hantro_end_prepare_run(struct hantro_ctx *ctx)
 static void device_run(void *priv)
 {
 	struct hantro_ctx *ctx = priv;
+	struct hantro_dev *vpu = ctx->dev;
 	struct vb2_v4l2_buffer *src, *dst;
 	int ret;
 
 	src = hantro_get_src_buf(ctx);
 	dst = hantro_get_dst_buf(ctx);
 
-	ret = pm_runtime_resume_and_get(ctx->dev->dev);
-	if (ret < 0)
-		goto err_cancel_job;
-
-	ret = clk_bulk_enable(ctx->dev->variant->num_clocks, ctx->dev->clocks);
-	if (ret)
-		goto err_cancel_job;
+	ret = pm_runtime_resume_and_get(vpu->dev);
+	if (ret < 0) {
+		hantro_job_finish_no_pm(ctx, VB2_BUF_STATE_ERROR);
+		return;
+	}
 
 	v4l2_m2m_buf_copy_metadata(src, dst);
 
-	if (ctx->codec_ops->run(ctx))
-		goto err_cancel_job;
-
-	return;
-
-err_cancel_job:
-	hantro_job_finish_no_pm(ctx->dev, ctx, VB2_BUF_STATE_ERROR);
+	ret = ctx->codec_ops->run(ctx);
+	if (ret)
+		hantro_job_finish(ctx, VB2_BUF_STATE_ERROR);
 }
 
 static const struct v4l2_m2m_ops vpu_m2m_ops = {
@@ -1292,22 +1284,40 @@ static void hantro_remove(struct platform_device *pdev)
 	pm_runtime_disable(vpu->dev);
 }
 
-#ifdef CONFIG_PM
 static int hantro_runtime_resume(struct device *dev)
 {
 	struct hantro_dev *vpu = dev_get_drvdata(dev);
+	int ret;
 
-	if (vpu->variant->runtime_resume)
-		return vpu->variant->runtime_resume(vpu);
+	ret = clk_bulk_enable(vpu->variant->num_clocks, vpu->clocks);
+	if (ret)
+		return ret;
+
+	if (vpu->variant->runtime_resume) {
+		ret = vpu->variant->runtime_resume(vpu);
+		if (ret)
+			goto err_disable_clocks;
+	}
 
 	return 0;
+
+err_disable_clocks:
+	clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
+	return ret;
+}
+
+static int hantro_runtime_suspend(struct device *dev)
+{
+	struct hantro_dev *vpu = dev_get_drvdata(dev);
+
+	clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
+	return 0;
 }
-#endif
 
 static const struct dev_pm_ops hantro_pm_ops = {
 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
 				pm_runtime_force_resume)
-	SET_RUNTIME_PM_OPS(NULL, hantro_runtime_resume, NULL)
+	SET_RUNTIME_PM_OPS(hantro_runtime_suspend, hantro_runtime_resume, NULL)
 };
 
 static struct platform_driver hantro_driver = {
Signature · 1 line
2.47.3
#5 lore raw
Quoted 160 lines · Tharit Tangkijwanichakul

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

device_run() acquires a runtime PM reference and enables the VPU clocks before invoking the codec-specific run callback.

If clk_bulk_enable() fails, the runtime PM reference is left held. If the codec-specific run callback fails, both the enabled clocks and the runtime PM reference are left held.

Make the clocks part of the device's runtime PM state by enabling them from the runtime-resume callback and disabling them from the runtime-suspend callback.

Fixes 775fec69008d ("media: add Rockchip VPU JPEG encoder driver")

---
Changes in v3:
- Move clock enable and disable operations into the runtime PM callbacks, as suggested by Frank Li.
- Remove vpu from argument in hantro_job_finish() and hantro_job_finish_no_pm(). It is accessible by ctx.

Changes in v2:
- Fix the codec run failure path to disable the clocks before dropping the runtime PM reference.
- Use a local vpu variable in device_run().

Tested on a ROCK 5B (RK3588) using the Hantro G1 decoder and Fluster:

H.264 (JVT-AVC_V1): 129/135, unchanged
MPEG-2 (MPEG2_VIDEO-MAIN): 23/43, unchanged
VP8 (VP8-TEST-VECTORS): 61/61, unchanged

 .../media/platform/verisilicon/hantro_drv.c   | 68 +++++++++++--------
 1 file changed, 39 insertions(+), 29 deletions(-)
drivers/media/platform/verisilicon/hantro_drv.c +36 -27
index 2e81877f640f..7978bb72569e 100644
@@ -59,8 +59,7 @@ static const struct v4l2_event hantro_eos_event = {
        .type = V4L2_EVENT_EOS
 };
 
-static void hantro_job_finish_no_pm(struct hantro_dev *vpu,
-                                   struct hantro_ctx *ctx,
+static void hantro_job_finish_no_pm(struct hantro_ctx *ctx,
                                    enum vb2_buffer_state result)
 {
        struct vb2_v4l2_buffer *src, *dst;
@@ -86,15 +85,13 @@ static void hantro_job_finish_no_pm(struct hantro_dev *vpu,
                                         result);
 }
 
-static void hantro_job_finish(struct hantro_dev *vpu,
-                             struct hantro_ctx *ctx,
+static void hantro_job_finish(struct hantro_ctx *ctx,
                              enum vb2_buffer_state result)
 {
-       pm_runtime_put_autosuspend(vpu->dev);
-
-       clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
+       struct hantro_dev *vpu = ctx->dev;
 
-       hantro_job_finish_no_pm(vpu, ctx, result);
+       pm_runtime_put_autosuspend(vpu->dev);
+       hantro_job_finish_no_pm(ctx, result);
 }
 
 void hantro_irq_done(struct hantro_dev *vpu,
@@ -111,7 +108,7 @@ void hantro_irq_done(struct hantro_dev *vpu,
        if (cancel_delayed_work(&vpu->watchdog_work)) {
                if (result == VB2_BUF_STATE_DONE && ctx->codec_ops->done)
                        ctx->codec_ops->done(ctx);
-               hantro_job_finish(vpu, ctx, result);
+               hantro_job_finish(ctx, result);
        }
 }
 
@@ -127,7 +124,7 @@ void hantro_watchdog(struct work_struct *work)
                vpu_err("frame processing timed out!\n");
                if (ctx->codec_ops->reset)
                        ctx->codec_ops->reset(ctx);
-               hantro_job_finish(vpu, ctx, VB2_BUF_STATE_ERROR);
+               hantro_job_finish(ctx, VB2_BUF_STATE_ERROR);
        }
 }
 
@@ -170,29 +167,24 @@ void hantro_end_prepare_run(struct hantro_ctx *ctx)
 static void device_run(void *priv)
 {
        struct hantro_ctx *ctx = priv;
+       struct hantro_dev *vpu = ctx->dev;
        struct vb2_v4l2_buffer *src, *dst;
        int ret;
 
        src = hantro_get_src_buf(ctx);
        dst = hantro_get_dst_buf(ctx);
 
-       ret = pm_runtime_resume_and_get(ctx->dev->dev);
-       if (ret < 0)
-               goto err_cancel_job;
-
-       ret = clk_bulk_enable(ctx->dev->variant->num_clocks, ctx->dev->clocks);
-       if (ret)
-               goto err_cancel_job;
+       ret = pm_runtime_resume_and_get(vpu->dev);
+       if (ret < 0) {
+               hantro_job_finish_no_pm(ctx, VB2_BUF_STATE_ERROR);
+               return;
+       }
 
        v4l2_m2m_buf_copy_metadata(src, dst);
 
-       if (ctx->codec_ops->run(ctx))
-               goto err_cancel_job;
-
-       return;
-
-err_cancel_job:
-       hantro_job_finish_no_pm(ctx->dev, ctx, VB2_BUF_STATE_ERROR);
+       ret = ctx->codec_ops->run(ctx);
+       if (ret)
+               hantro_job_finish(ctx, VB2_BUF_STATE_ERROR);
 }
 
 static const struct v4l2_m2m_ops vpu_m2m_ops = {
@@ -1292,22 +1284,40 @@ static void hantro_remove(struct platform_device *pdev)
        pm_runtime_disable(vpu->dev);
 }
 
-#ifdef CONFIG_PM
 static int hantro_runtime_resume(struct device *dev)
 {
        struct hantro_dev *vpu = dev_get_drvdata(dev);
+       int ret;
 
-       if (vpu->variant->runtime_resume)
-               return vpu->variant->runtime_resume(vpu);
+       ret = clk_bulk_enable(vpu->variant->num_clocks, vpu->clocks);
+       if (ret)
+               return ret;
+
+       if (vpu->variant->runtime_resume) {
+               ret = vpu->variant->runtime_resume(vpu);
+               if (ret)
+                       goto err_disable_clocks;
+       }
 
        return 0;
+
+err_disable_clocks:
+       clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
+       return ret;
+}
+
+static int hantro_runtime_suspend(struct device *dev)
+{
+       struct hantro_dev *vpu = dev_get_drvdata(dev);
+

drivers/media/platform/verisilicon/hantro_drv.c static void hantro_remove(struct platform_device *pdev) · line 1320

Quoted 10 lines
drivers/media/platform/verisilicon/hantro_drv.c +3 -2
+       clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
+       return 0;
 }
-#endif
 
 static const struct dev_pm_ops hantro_pm_ops = {
        SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
                                pm_runtime_force_resume)
-       SET_RUNTIME_PM_OPS(NULL, hantro_runtime_resume, NULL)
+       SET_RUNTIME_PM_OPS(hantro_runtime_suspend, hantro_runtime_resume, NULL)

You need convert to model RUMTIME_PM_OPS and SYSTEM_SLEEP_PM_OPS

pm_ptr(hantro_pm_ops) at below hantro_driver

and remove CONFIG_PM first,

then add move clock manage into runtime pm management

If you remove "CONFIG_PM" here, cause build error at some special config.

Or keep CONFIG_PM at this patch, then convert to RUMTIME_PM_OPS() in following patch.

Frank

Quoted 6 lines
drivers/media/platform/verisilicon/hantro_drv.c +0 -1
 };
 
 static struct platform_driver hantro_driver = {
--

2.47.3

#6 lore raw
Quoted 6 lines

You need convert to model RUMTIME_PM_OPS and SYSTEM_SLEEP_PM_OPS

pm_ptr(hantro_pm_ops) at below hantro_driver

and remove CONFIG_PM first,

Quoted 8 lines

then add move clock manage into runtime pm management

If you remove "CONFIG_PM" here, cause build error at some special config.

Or keep CONFIG_PM at this patch, then convert to RUMTIME_PM_OPS() in following patch.

Frank

I see, the compiler may flag resume/suspend callback unused if CONFIG_PM=n and cause build error. Thanks for pointing this out. I will keep CONFIG_PM and send a conversion in a subsequent patch.

Best,
Tharit

#7 lore raw

PATCH v4 1/1 media: hantro: release runtime resources when device_run fails

In reply to Tharit Tangkijwanichakul

device_run() acquires a runtime PM reference and enables the VPU clocks before invoking the codec-specific run callback.

If clk_bulk_enable() fails, the runtime PM reference is left held. If the codec-specific run callback fails, both the enabled clocks and the runtime PM reference are left held.

Make the clocks part of the device's runtime PM state by enabling them from the runtime-resume callback and disabling them from the runtime-suspend callback.

Fixes 775fec69008d ("media: add Rockchip VPU JPEG encoder driver")

---
Changes in v4:
- Keep CONFIG_PM guard to avoid possible build error, as suggested by Frank Li.

Changes in v3:
- Move clock enable and disable operations into the runtime PM callbacks, as suggested by Frank Li.
- Remove vpu from argument in hantro_job_finish() and hantro_job_finish_no_pm(). It is accessible by ctx.

Changes in v2:
- Fix the codec run failure path to disable the clocks before dropping the runtime PM reference.
- Use a local vpu variable in device_run().

Tested on a ROCK 5B (RK3588) using the Hantro G1 decoder and Fluster:

H.264 (JVT-AVC_V1): 129/135, unchanged
MPEG-2 (MPEG2_VIDEO-MAIN): 23/43, unchanged
VP8 (VP8-TEST-VECTORS): 61/61, unchanged

 .../media/platform/verisilicon/hantro_drv.c   | 66 +++++++++++--------
 1 file changed, 39 insertions(+), 27 deletions(-)
drivers/media/platform/verisilicon/hantro_drv.c +39 -27
index 2e81877f640f..d6fcd4f7f9da 100644
@@ -59,8 +59,7 @@ static const struct v4l2_event hantro_eos_event = {
 	.type = V4L2_EVENT_EOS
 };
 
-static void hantro_job_finish_no_pm(struct hantro_dev *vpu,
-				    struct hantro_ctx *ctx,
+static void hantro_job_finish_no_pm(struct hantro_ctx *ctx,
 				    enum vb2_buffer_state result)
 {
 	struct vb2_v4l2_buffer *src, *dst;
@@ -86,15 +85,13 @@ static void hantro_job_finish_no_pm(struct hantro_dev *vpu,
 					 result);
 }
 
-static void hantro_job_finish(struct hantro_dev *vpu,
-			      struct hantro_ctx *ctx,
+static void hantro_job_finish(struct hantro_ctx *ctx,
 			      enum vb2_buffer_state result)
 {
-	pm_runtime_put_autosuspend(vpu->dev);
-
-	clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
+	struct hantro_dev *vpu = ctx->dev;
 
-	hantro_job_finish_no_pm(vpu, ctx, result);
+	pm_runtime_put_autosuspend(vpu->dev);
+	hantro_job_finish_no_pm(ctx, result);
 }
 
 void hantro_irq_done(struct hantro_dev *vpu,
@@ -111,7 +108,7 @@ void hantro_irq_done(struct hantro_dev *vpu,
 	if (cancel_delayed_work(&vpu->watchdog_work)) {
 		if (result == VB2_BUF_STATE_DONE && ctx->codec_ops->done)
 			ctx->codec_ops->done(ctx);
-		hantro_job_finish(vpu, ctx, result);
+		hantro_job_finish(ctx, result);
 	}
 }
 
@@ -127,7 +124,7 @@ void hantro_watchdog(struct work_struct *work)
 		vpu_err("frame processing timed out!\n");
 		if (ctx->codec_ops->reset)
 			ctx->codec_ops->reset(ctx);
-		hantro_job_finish(vpu, ctx, VB2_BUF_STATE_ERROR);
+		hantro_job_finish(ctx, VB2_BUF_STATE_ERROR);
 	}
 }
 
@@ -170,29 +167,24 @@ void hantro_end_prepare_run(struct hantro_ctx *ctx)
 static void device_run(void *priv)
 {
 	struct hantro_ctx *ctx = priv;
+	struct hantro_dev *vpu = ctx->dev;
 	struct vb2_v4l2_buffer *src, *dst;
 	int ret;
 
 	src = hantro_get_src_buf(ctx);
 	dst = hantro_get_dst_buf(ctx);
 
-	ret = pm_runtime_resume_and_get(ctx->dev->dev);
-	if (ret < 0)
-		goto err_cancel_job;
-
-	ret = clk_bulk_enable(ctx->dev->variant->num_clocks, ctx->dev->clocks);
-	if (ret)
-		goto err_cancel_job;
+	ret = pm_runtime_resume_and_get(vpu->dev);
+	if (ret < 0) {
+		hantro_job_finish_no_pm(ctx, VB2_BUF_STATE_ERROR);
+		return;
+	}
 
 	v4l2_m2m_buf_copy_metadata(src, dst);
 
-	if (ctx->codec_ops->run(ctx))
-		goto err_cancel_job;
-
-	return;
-
-err_cancel_job:
-	hantro_job_finish_no_pm(ctx->dev, ctx, VB2_BUF_STATE_ERROR);
+	ret = ctx->codec_ops->run(ctx);
+	if (ret)
+		hantro_job_finish(ctx, VB2_BUF_STATE_ERROR);
 }
 
 static const struct v4l2_m2m_ops vpu_m2m_ops = {
@@ -1296,10 +1288,30 @@ static void hantro_remove(struct platform_device *pdev)
 static int hantro_runtime_resume(struct device *dev)
 {
 	struct hantro_dev *vpu = dev_get_drvdata(dev);
+	int ret;
+
+	ret = clk_bulk_enable(vpu->variant->num_clocks, vpu->clocks);
+	if (ret)
+		return ret;
+
+	if (vpu->variant->runtime_resume) {
+		ret = vpu->variant->runtime_resume(vpu);
+		if (ret)
+			goto err_disable_clocks;
+	}
+
+	return 0;
 
-	if (vpu->variant->runtime_resume)
-		return vpu->variant->runtime_resume(vpu);
+err_disable_clocks:
+	clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
+	return ret;
+}
 
+static int hantro_runtime_suspend(struct device *dev)
+{
+	struct hantro_dev *vpu = dev_get_drvdata(dev);
+
+	clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
 	return 0;
 }
 #endif
@@ -1307,7 +1319,7 @@ static int hantro_runtime_resume(struct device *dev)
 static const struct dev_pm_ops hantro_pm_ops = {
 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
 				pm_runtime_force_resume)
-	SET_RUNTIME_PM_OPS(NULL, hantro_runtime_resume, NULL)
+	SET_RUNTIME_PM_OPS(hantro_runtime_suspend, hantro_runtime_resume, NULL)
 };
 
 static struct platform_driver hantro_driver = {
Signature · 1 line
2.47.3
#8 lore raw
In reply to Tharit Tangkijwanichakul
Quoted 128 lines · Tharit Tangkijwanichakul

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

device_run() acquires a runtime PM reference and enables the VPU clocks before invoking the codec-specific run callback.

If clk_bulk_enable() fails, the runtime PM reference is left held. If the codec-specific run callback fails, both the enabled clocks and the runtime PM reference are left held.

Make the clocks part of the device's runtime PM state by enabling them from the runtime-resume callback and disabling them from the runtime-suspend callback.

Fixes 775fec69008d ("media: add Rockchip VPU JPEG encoder driver")

---
Changes in v3:
- Move clock enable and disable operations into the runtime PM callbacks, as suggested by Frank Li.
- Remove vpu from argument in hantro_job_finish() and hantro_job_finish_no_pm(). It is accessible by ctx.

Changes in v2:
- Fix the codec run failure path to disable the clocks before dropping the runtime PM reference.
- Use a local vpu variable in device_run().

Tested on a ROCK 5B (RK3588) using the Hantro G1 decoder and Fluster:

H.264 (JVT-AVC_V1): 129/135, unchanged
MPEG-2 (MPEG2_VIDEO-MAIN): 23/43, unchanged
VP8 (VP8-TEST-VECTORS): 61/61, unchanged

 .../media/platform/verisilicon/hantro_drv.c   | 68 +++++++++++--------
 1 file changed, 39 insertions(+), 29 deletions(-)
drivers/media/platform/verisilicon/hantro_drv.c +16 -24
index 2e81877f640f..7978bb72569e 100644
@@ -59,8 +59,7 @@ static const struct v4l2_event hantro_eos_event = {
        .type = V4L2_EVENT_EOS
 };
 
-static void hantro_job_finish_no_pm(struct hantro_dev *vpu,
-                                   struct hantro_ctx *ctx,
+static void hantro_job_finish_no_pm(struct hantro_ctx *ctx,
                                    enum vb2_buffer_state result)
 {
        struct vb2_v4l2_buffer *src, *dst;
@@ -86,15 +85,13 @@ static void hantro_job_finish_no_pm(struct hantro_dev *vpu,
                                         result);
 }
 
-static void hantro_job_finish(struct hantro_dev *vpu,
-                             struct hantro_ctx *ctx,
+static void hantro_job_finish(struct hantro_ctx *ctx,
                              enum vb2_buffer_state result)
 {
-       pm_runtime_put_autosuspend(vpu->dev);
-
-       clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
+       struct hantro_dev *vpu = ctx->dev;
 
-       hantro_job_finish_no_pm(vpu, ctx, result);
+       pm_runtime_put_autosuspend(vpu->dev);
+       hantro_job_finish_no_pm(ctx, result);
 }
 
 void hantro_irq_done(struct hantro_dev *vpu,
@@ -111,7 +108,7 @@ void hantro_irq_done(struct hantro_dev *vpu,
        if (cancel_delayed_work(&vpu->watchdog_work)) {
                if (result == VB2_BUF_STATE_DONE && ctx->codec_ops->done)
                        ctx->codec_ops->done(ctx);
-               hantro_job_finish(vpu, ctx, result);
+               hantro_job_finish(ctx, result);
        }
 }
 
@@ -127,7 +124,7 @@ void hantro_watchdog(struct work_struct *work)
                vpu_err("frame processing timed out!\n");
                if (ctx->codec_ops->reset)
                        ctx->codec_ops->reset(ctx);
-               hantro_job_finish(vpu, ctx, VB2_BUF_STATE_ERROR);
+               hantro_job_finish(ctx, VB2_BUF_STATE_ERROR);
        }
 }
 
@@ -170,29 +167,24 @@ void hantro_end_prepare_run(struct hantro_ctx *ctx)
 static void device_run(void *priv)
 {
        struct hantro_ctx *ctx = priv;
+       struct hantro_dev *vpu = ctx->dev;
        struct vb2_v4l2_buffer *src, *dst;
        int ret;
 
        src = hantro_get_src_buf(ctx);
        dst = hantro_get_dst_buf(ctx);
 
-       ret = pm_runtime_resume_and_get(ctx->dev->dev);
-       if (ret < 0)
-               goto err_cancel_job;
-
-       ret = clk_bulk_enable(ctx->dev->variant->num_clocks, ctx->dev->clocks);
-       if (ret)
-               goto err_cancel_job;
+       ret = pm_runtime_resume_and_get(vpu->dev);
+       if (ret < 0) {
+               hantro_job_finish_no_pm(ctx, VB2_BUF_STATE_ERROR);
+               return;
+       }
 
        v4l2_m2m_buf_copy_metadata(src, dst);
 
-       if (ctx->codec_ops->run(ctx))
-               goto err_cancel_job;
-
-       return;
-
-err_cancel_job:
-       hantro_job_finish_no_pm(ctx->dev, ctx, VB2_BUF_STATE_ERROR);
+       ret = ctx->codec_ops->run(ctx);
+       if (ret)
+               hantro_job_finish(ctx, VB2_BUF_STATE_ERROR);
 }
 
 static const struct v4l2_m2m_ops vpu_m2m_ops = {

drivers/media/platform/verisilicon/hantro_drv.c static void hantro_remove(struct platform_device *pdev) · line 1295

Quoted 4 lines
drivers/media/platform/verisilicon/hantro_drv.c +0 -1
@@ -1292,22 +1284,40 @@ static void hantro_remove(struct platform_device *pdev)
        pm_runtime_disable(vpu->dev);
 }
 
-#ifdef CONFIG_PM

You need sperate patch to remove this. simple remove this here may cause build error or warning at NO CONFIG_PM config

I suggest use following patch do below thing

1. Remove CONFIG_PM

2. Use DEFINE_RUNTIME_DEV_PM_OP()

3, Need update

.pm = pm_ptr(&hantro_pm_ops),

Frank

Quoted 44 lines
drivers/media/platform/verisilicon/hantro_drv.c +23 -5
 static int hantro_runtime_resume(struct device *dev)
 {
        struct hantro_dev *vpu = dev_get_drvdata(dev);
+       int ret;
 
-       if (vpu->variant->runtime_resume)
-               return vpu->variant->runtime_resume(vpu);
+       ret = clk_bulk_enable(vpu->variant->num_clocks, vpu->clocks);
+       if (ret)
+               return ret;
+
+       if (vpu->variant->runtime_resume) {
+               ret = vpu->variant->runtime_resume(vpu);
+               if (ret)
+                       goto err_disable_clocks;
+       }
 
        return 0;
+
+err_disable_clocks:
+       clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
+       return ret;
+}
+
+static int hantro_runtime_suspend(struct device *dev)
+{
+       struct hantro_dev *vpu = dev_get_drvdata(dev);
+
+       clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
+       return 0;
 }
-#endif
 
 static const struct dev_pm_ops hantro_pm_ops = {
        SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
                                pm_runtime_force_resume)
-       SET_RUNTIME_PM_OPS(NULL, hantro_runtime_resume, NULL)
+       SET_RUNTIME_PM_OPS(hantro_runtime_suspend, hantro_runtime_resume, NULL)
 };
 
 static struct platform_driver hantro_driver = {
--

2.47.3

#9 lore raw
Quoted 7 lines

You need sperate patch to remove this. simple remove this here may cause build error or warning at NO CONFIG_PM config

I suggest use following patch do below thing

1. Remove CONFIG_PM

Quoted 7 lines

2. Use DEFINE_RUNTIME_DEV_PM_OP()

3, Need update

.pm = pm_ptr(&hantro_pm_ops),

Frank

Thanks for the suggestion and the patience walking me through. I will send the v5 series as suggested.

Best,
Tharit

#10 lore raw
In reply to Tharit Tangkijwanichakul
kernel test robot noticed the following build warnings:

Hi Tharit,

kernel test robot noticed the following build warnings:

[auto build test WARNING on linus/master]
[also build test WARNING on pza/reset/next v7.2-rc5 next-20260729] [cannot apply to pza/imx-drm/next]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Tharit-Tangkijwanichakul/media-hantro-release-runtime-resources-when-device_run-fails/20260726-104549
base:   linus/master
patch link:    https://lore.kernel.org/r/20260726024243.730-1-tharitt97%40gmail.com
patch subject: [PATCH v3] media: hantro: release runtime resources when device_run fails
config: nios2-allmodconfig (https://download.01.org/0day-ci/archive/20260730/[email protected]/config)
compiler: nios2-linux-gcc (GCC) 11.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260730/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> drivers/media/platform/verisilicon/hantro_drv.c:1309:12: warning: 'hantro_runtime_suspend' defined but not used [-Wunused-function]
    1309 | static int hantro_runtime_suspend(struct device *dev)
         |            ^~~~~~~~~~~~~~~~~~~~~~
>> drivers/media/platform/verisilicon/hantro_drv.c:1287:12: warning: 'hantro_runtime_resume' defined but not used [-Wunused-function]

1287 | static int hantro_runtime_resume(struct device *dev)
| ^~~~~~~~~~~~~~~~~~~~~

vim +/hantro_runtime_suspend +1309 drivers/media/platform/verisilicon/hantro_drv.c

1286

> 1287	static int hantro_runtime_resume(struct device *dev)
  1288	{
  1289		struct hantro_dev *vpu = dev_get_drvdata(dev);
  1290		int ret;
  1291	
  1292		ret = clk_bulk_enable(vpu->variant->num_clocks, vpu->clocks);
  1293		if (ret)
  1294			return ret;
  1295	
  1296		if (vpu->variant->runtime_resume) {
  1297			ret = vpu->variant->runtime_resume(vpu);
  1298			if (ret)
  1299				goto err_disable_clocks;
  1300		}
  1301	
  1302		return 0;
  1303	
  1304	err_disable_clocks:
  1305		clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
  1306		return ret;
  1307	}
  1308	
> 1309	static int hantro_runtime_suspend(struct device *dev)

1310 {
1311 struct hantro_dev *vpu = dev_get_drvdata(dev);
1312
1313 clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
1314 return 0;
1315 }
1316

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki