Skip to contentSkip to conversation

media: venus: fix HFI teardown races

PATCH Superseded Myeonghun Pak 2 participants 9 messages Sep 25 – Oct 24

Subsystems (5)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover media: venus: fix HFI teardown races

As part of our ongoing static-analysis research, we reviewed the Venus teardown path and confirmed the race reported by Sashiko.

This series fixes two HFI lifetime races during teardown. The first patch waits for the hard and threaded IRQ handlers before releasing HFI state. The second disables and drains recovery work so it cannot be requeued while HFI state is being torn down.

The later Sashiko report also identifies a separate resource leak when IRQ registration fails during probe. That issue is outside this series.

Changes in v3:
- Send through git send-email over SMTP. The v2 cover letter incorrectly said git send-email was used; v2 was sent through the Gmail API, which still wrapped the diff --git lines before delivery.
- No code changes.

Changes in v2:
- Regenerated the patch files with git format-patch after the v1 mail delivery wrapped the diff --git lines. No code changes.

Myeonghun Pak (2):
media: venus: hfi: disable IRQ before freeing HFI device
media: venus: disable recovery work before HFI teardown

 drivers/media/platform/qcom/venus/core.c      | 2 +-
 drivers/media/platform/qcom/venus/hfi_venus.c | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)

base-commit: 4efa625593e1e9c76346232413997556dea1d89f

Signature · 1 line
2.53.0
#1 lore raw

PATCH v3 1/2 media: venus: hfi: disable IRQ before freeing HFI device

venus_hfi_destroy() releases the interface queues and frees hdev before calling disable_irq(). An IRQ handler that has already read core->priv can continue to access hdev and its queues after they are freed. Clearing core->priv does not revoke the handler's local pointer.

Commit 640803003cd9 ("media: venus: hfi: explicitly release IRQ during teardown") added disable_irq(), but placed it after kfree(hdev), too late to protect a handler already using hdev.

Disable the IRQ first so that both the hard IRQ and threaded handler have completed before clearing core->priv or releasing hdev.

Fixes d96d3f30c0f2 ("[media] media: venus: hfi: add Venus HFI files")
Assisted-by LLM
---
 drivers/media/platform/qcom/venus/hfi_venus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/qcom/venus/hfi_venus.c +1 -1
index bd82066bb6e7..e7e4e78a186a 100644
@@ -1689,11 +1689,11 @@ void venus_hfi_destroy(struct venus_core *core)
 {
 	struct venus_hfi_device *hdev = to_hfi_priv(core);
 
+	disable_irq(core->irq);
 	core->priv = NULL;
 	venus_interface_queues_release(hdev);
 	mutex_destroy(&hdev->lock);
 	kfree(hdev);
-	disable_irq(core->irq);
 	core->ops = NULL;
 }
 
Signature · 1 line
2.53.0
#2 lore raw

PATCH v3 2/2 media: venus: disable recovery work before HFI teardown

In reply to Myeonghun Pak

venus_remove() cancels core->work before the IRQ is disabled. An IRQ thread can queue the work again after cancellation. The work can then access HFI state after venus_hfi_destroy() frees it. The work also requeues itself when recovery fails, so cancelling an already running instance alone does not close the race.

Disable and drain the work at the start of remove, before other resources are dismantled. Do the same in venus_hfi_destroy() for paths that bypass remove, including probe unwind. Disabling the work prevents both IRQ handlers and the work itself from requeuing it. Drain the work before disabling the IRQ so an active recovery can finish any IRQ based completion waits. Then synchronize the IRQ before freeing HFI state.

Fixes af2c3834c8ca ("[media] media: venus: adding core part and helper functions")
Assisted-by LLM
---
 drivers/media/platform/qcom/venus/core.c      | 2 +-
 drivers/media/platform/qcom/venus/hfi_venus.c | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
drivers/media/platform/qcom/venus/core.c +1 -1
index 243e342b0ae7..94d4b8688ac2 100644
@@ -538,7 +538,7 @@ static void venus_remove(struct platform_device *pdev)
 	struct device *dev = core->dev;
 	int ret;
 
-	cancel_delayed_work_sync(&core->work);
+	disable_delayed_work_sync(&core->work);
 	ret = pm_runtime_get_sync(dev);
 	WARN_ON(ret < 0);
 
drivers/media/platform/qcom/venus/hfi_venus.c +1 -0
index e7e4e78a186a..20b8ba1e62f1 100644
@@ -1689,6 +1689,7 @@ void venus_hfi_destroy(struct venus_core *core)
 {
 	struct venus_hfi_device *hdev = to_hfi_priv(core);
 
+	disable_delayed_work_sync(&core->work);
 	disable_irq(core->irq);
 	core->priv = NULL;
 	venus_interface_queues_release(hdev);
Signature · 1 line
2.53.0
#3 lore raw

v4 cover media: venus: fix HFI teardown races

In reply to Myeonghun Pak

As part of our ongoing static-analysis research, we reviewed the Venus teardown path and confirmed the race reported by Sashiko.

This series fixes two HFI lifetime races during teardown. The first patch waits for the hard and threaded IRQ handlers before releasing HFI state. The second disables and drains recovery work so it cannot be requeued while HFI state is being torn down.

The later Sashiko report also identifies a separate resource leak when IRQ registration fails during probe. That issue is outside this series.

Changes in v4:
- Rebase onto the latest media-committers.git next branch to address the Media CI base-branch check.
- No code changes.

Changes in v3:
- Send through git send-email over SMTP. The v2 cover letter incorrectly said git send-email was used; v2 was sent through the Gmail API, which still wrapped the diff --git lines before delivery.
- No code changes.

Changes in v2:
- Regenerated the patch files with git format-patch after the v1 mail delivery wrapped the diff --git lines. No code changes.

Myeonghun Pak (2):
media: venus: hfi: disable IRQ before freeing HFI device
media: venus: disable recovery work before HFI teardown

 drivers/media/platform/qcom/venus/core.c      | 2 +-
 drivers/media/platform/qcom/venus/hfi_venus.c | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)

base-commit: 4a069a763e8a86821edac4159f7c59a20015631b

Signature · 1 line
2.53.0
#4 lore raw

PATCH v4 1/2 media: venus: hfi: disable IRQ before freeing HFI device

venus_hfi_destroy() releases the interface queues and frees hdev before calling disable_irq(). An IRQ handler that has already read core->priv can continue to access hdev and its queues after they are freed. Clearing core->priv does not revoke the handler's local pointer.

Commit 640803003cd9 ("media: venus: hfi: explicitly release IRQ during teardown") added disable_irq(), but placed it after kfree(hdev), too late to protect a handler already using hdev.

Disable the IRQ first so that both the hard IRQ and threaded handler have completed before clearing core->priv or releasing hdev.

Fixes d96d3f30c0f2 ("[media] media: venus: hfi: add Venus HFI files")
Assisted-by LLM
---
 drivers/media/platform/qcom/venus/hfi_venus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/qcom/venus/hfi_venus.c +1 -1
index bd82066bb6e7..e7e4e78a186a 100644
@@ -1689,11 +1689,11 @@ void venus_hfi_destroy(struct venus_core *core)
 {
 	struct venus_hfi_device *hdev = to_hfi_priv(core);
 
+	disable_irq(core->irq);
 	core->priv = NULL;
 	venus_interface_queues_release(hdev);
 	mutex_destroy(&hdev->lock);
 	kfree(hdev);
-	disable_irq(core->irq);
 	core->ops = NULL;
 }
 
Signature · 1 line
2.53.0
#6 lore raw

PATCH v4 2/2 media: venus: disable recovery work before HFI teardown

In reply to Myeonghun Pak

venus_remove() cancels core->work before the IRQ is disabled. An IRQ thread can queue the work again after cancellation. The work can then access HFI state after venus_hfi_destroy() frees it. The work also requeues itself when recovery fails, so cancelling an already running instance alone does not close the race.

Disable and drain the work at the start of remove, before other resources are dismantled. Do the same in venus_hfi_destroy() for paths that bypass remove, including probe unwind. Disabling the work prevents both IRQ handlers and the work itself from requeuing it. Drain the work before disabling the IRQ so an active recovery can finish any IRQ based completion waits. Then synchronize the IRQ before freeing HFI state.

Fixes af2c3834c8ca ("[media] media: venus: adding core part and helper functions")
Assisted-by LLM
---
 drivers/media/platform/qcom/venus/core.c      | 2 +-
 drivers/media/platform/qcom/venus/hfi_venus.c | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
drivers/media/platform/qcom/venus/core.c +1 -1
index 7087af32060f..6e495bc04691 100644
@@ -596,7 +596,7 @@ static void venus_remove(struct platform_device *pdev)
 	struct device *dev = core->dev;
 	int ret;
 
-	cancel_delayed_work_sync(&core->work);
+	disable_delayed_work_sync(&core->work);
 	ret = pm_runtime_get_sync(dev);
 	WARN_ON(ret < 0);
 
drivers/media/platform/qcom/venus/hfi_venus.c +1 -0
index e7e4e78a186a..20b8ba1e62f1 100644
@@ -1689,6 +1689,7 @@ void venus_hfi_destroy(struct venus_core *core)
 {
 	struct venus_hfi_device *hdev = to_hfi_priv(core);
 
+	disable_delayed_work_sync(&core->work);
 	disable_irq(core->irq);
 	core->priv = NULL;
 	venus_interface_queues_release(hdev);
Signature · 1 line
2.53.0
maintainer Reviewed-by on v4 1/2
#5 lore raw

Re: [PATCH v4 1/2] media: venus: hfi: disable IRQ before freeing HFI device

In reply to Myeonghun Pak
Quoted 42 lines · Myeonghun Pak

venus_hfi_destroy() releases the interface queues and frees hdev before calling disable_irq(). An IRQ handler that has already read core->priv can continue to access hdev and its queues after they are freed. Clearing core->priv does not revoke the handler's local pointer.

Commit 640803003cd9 ("media: venus: hfi: explicitly release IRQ during teardown") added disable_irq(), but placed it after kfree(hdev), too late to protect a handler already using hdev.

Disable the IRQ first so that both the hard IRQ and threaded handler have completed before clearing core->priv or releasing hdev.

Fixes d96d3f30c0f2 ("[media] media: venus: hfi: add Venus HFI files")
Assisted-by LLM
---
  drivers/media/platform/qcom/venus/hfi_venus.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/qcom/venus/hfi_venus.c +1 -2
index bd82066bb6e7..e7e4e78a186a 100644
@@ -1689,11 +1689,11 @@ void venus_hfi_destroy(struct venus_core *core)
  {
  	struct venus_hfi_device *hdev = to_hfi_priv(core);
 
+	disable_irq(core->irq);
  	core->priv = NULL;
  	venus_interface_queues_release(hdev);
  	mutex_destroy(&hdev->lock);
  	kfree(hdev);
-	disable_irq(core->irq);
  	core->ops = NULL;
  }
 
--
Quoted 2 lines

2.53.0

maintainer NAK
#7 lore raw

Re: [PATCH v4 2/2] media: venus: disable recovery work before HFI teardown

In reply to Myeonghun Pak
Quoted 52 lines · Myeonghun Pak

venus_remove() cancels core->work before the IRQ is disabled. An IRQ thread can queue the work again after cancellation. The work can then access HFI state after venus_hfi_destroy() frees it. The work also requeues itself when recovery fails, so cancelling an already running instance alone does not close the race.

Disable and drain the work at the start of remove, before other resources are dismantled. Do the same in venus_hfi_destroy() for paths that bypass remove, including probe unwind. Disabling the work prevents both IRQ handlers and the work itself from requeuing it. Drain the work before disabling the IRQ so an active recovery can finish any IRQ based completion waits. Then synchronize the IRQ before freeing HFI state.

Fixes af2c3834c8ca ("[media] media: venus: adding core part and helper functions")
Assisted-by LLM
---
  drivers/media/platform/qcom/venus/core.c      | 2 +-
  drivers/media/platform/qcom/venus/hfi_venus.c | 1 +
  2 files changed, 2 insertions(+), 1 deletion(-)
drivers/media/platform/qcom/venus/core.c +1 -1
index 7087af32060f..6e495bc04691 100644
@@ -596,7 +596,7 @@ static void venus_remove(struct platform_device *pdev)
  	struct device *dev = core->dev;
  	int ret;
 
-	cancel_delayed_work_sync(&core->work);
+	disable_delayed_work_sync(&core->work);
  	ret = pm_runtime_get_sync(dev);
  	WARN_ON(ret < 0);
 
drivers/media/platform/qcom/venus/hfi_venus.c +1 -1
index e7e4e78a186a..20b8ba1e62f1 100644
@@ -1689,6 +1689,7 @@ void venus_hfi_destroy(struct venus_core *core)
  {
  	struct venus_hfi_device *hdev = to_hfi_priv(core);
 
+	disable_delayed_work_sync(&core->work);
  	disable_irq(core->irq);
  	core->priv = NULL;
  	venus_interface_queues_release(hdev);
--
Quoted 2 lines

2.53.0

You shouldn't have to disable the work queue twice.

Does a path actually exist where hfi_destroy() runs but venus_remove() does not ?

The commit text is vague about that - sure hfi_destroy() may be callable from the error path of probe() but is any work scheduled in that case ?

I mean lets just look at venus_hfi_destroy()

static void venus_remove(struct platform_device *pdev)

{
        struct venus_core *core = platform_get_drvdata(pdev);
        const struct venus_pm_ops *pm_ops = core->pm_ops;
        struct device *dev = core->dev;
        int ret;

        cancel_delayed_work_sync(&core->work);
        ret = pm_runtime_get_sync(dev);
        WARN_ON(ret < 0);

        ret = hfi_core_deinit(core, true);
        WARN_ON(ret);

        venus_shutdown(core);
        of_platform_depopulate(dev);

        venus_firmware_deinit(core);

        venus_remove_dynamic_nodes(core);

        pm_runtime_put_sync(dev);
        pm_runtime_disable(dev);

        if (pm_ops->core_put)
                pm_ops->core_put(core);

        v4l2_device_unregister(&core->v4l2_dev);

        hfi_destroy(core);

        mutex_destroy(&core->pm_lock);
        mutex_destroy(&core->lock);
        venus_dbgfs_deinit(core);
}

Your patch will call disable_delayed_work_sync() twice on this path once in venus_remove() per your patch and then again in hfi_destroy()...

NAK - can't be right.

---
bod

#8 lore raw

Re: [PATCH v4 2/2] media: venus: disable recovery work before HFI teardown

Hi Bryan,

Thanks for the review.

In v5, I removed the disable_delayed_work_sync() call from venus_hfi_destroy() and kept it only at the start of venus_remove(). The work is now disabled exactly once during removal.

Thanks,
Myeonghun

2026년 10월 1일 (목) 오후 4:10, Bryan O'Donoghue <[email protected]>님이 작성:

Quoted 111 lines · Myeonghun Pak
Quoted 52 lines

venus_remove() cancels core->work before the IRQ is disabled. An IRQ thread can queue the work again after cancellation. The work can then access HFI state after venus_hfi_destroy() frees it. The work also requeues itself when recovery fails, so cancelling an already running instance alone does not close the race.

Disable and drain the work at the start of remove, before other resources are dismantled. Do the same in venus_hfi_destroy() for paths that bypass remove, including probe unwind. Disabling the work prevents both IRQ handlers and the work itself from requeuing it. Drain the work before disabling the IRQ so an active recovery can finish any IRQ based completion waits. Then synchronize the IRQ before freeing HFI state.

Fixes af2c3834c8ca ("[media] media: venus: adding core part and helper functions")
Assisted-by LLM
---
  drivers/media/platform/qcom/venus/core.c      | 2 +-
  drivers/media/platform/qcom/venus/hfi_venus.c | 1 +
  2 files changed, 2 insertions(+), 1 deletion(-)
drivers/media/platform/qcom/venus/core.c +1 -1
index 7087af32060f..6e495bc04691 100644
@@ -596,7 +596,7 @@ static void venus_remove(struct platform_device *pdev)
      struct device *dev = core->dev;
      int ret;
 
-     cancel_delayed_work_sync(&core->work);
+     disable_delayed_work_sync(&core->work);
      ret = pm_runtime_get_sync(dev);
      WARN_ON(ret < 0);
 
drivers/media/platform/qcom/venus/hfi_venus.c +1 -1
index e7e4e78a186a..20b8ba1e62f1 100644
@@ -1689,6 +1689,7 @@ void venus_hfi_destroy(struct venus_core *core)
  {
      struct venus_hfi_device *hdev = to_hfi_priv(core);
 
+     disable_delayed_work_sync(&core->work);
      disable_irq(core->irq);
      core->priv = NULL;
      venus_interface_queues_release(hdev);
--
Quoted 2 lines

2.53.0

You shouldn't have to disable the work queue twice.

Does a path actually exist where hfi_destroy() runs but venus_remove() does not ?

The commit text is vague about that - sure hfi_destroy() may be callable from the error path of probe() but is any work scheduled in that case ?

I mean lets just look at venus_hfi_destroy()

static void venus_remove(struct platform_device *pdev)

{
         struct venus_core *core = platform_get_drvdata(pdev);
         const struct venus_pm_ops *pm_ops = core->pm_ops;
         struct device *dev = core->dev;
         int ret;

         cancel_delayed_work_sync(&core->work);
         ret = pm_runtime_get_sync(dev);
         WARN_ON(ret < 0);

         ret = hfi_core_deinit(core, true);
         WARN_ON(ret);

         venus_shutdown(core);
         of_platform_depopulate(dev);

         venus_firmware_deinit(core);

         venus_remove_dynamic_nodes(core);

         pm_runtime_put_sync(dev);
         pm_runtime_disable(dev);

         if (pm_ops->core_put)
                 pm_ops->core_put(core);

         v4l2_device_unregister(&core->v4l2_dev);

         hfi_destroy(core);

         mutex_destroy(&core->pm_lock);
         mutex_destroy(&core->lock);
         venus_dbgfs_deinit(core);
}

Your patch will call disable_delayed_work_sync() twice on this path once in venus_remove() per your patch and then again in hfi_destroy()...

NAK - can't be right.

---
bod