Skip to contentSkip to conversation

media: venus: fix HFI teardown races

PATCH In review Myeonghun Pak 3 participants 6 messages Oct 24 – Dec 1

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

v5 cover media: venus: fix HFI teardown races

This issue was identified as part of our ongoing research while reviewing kernel code.

Synchronize IRQ handlers before freeing HFI state, and disable recovery work at the start of remove to prevent it from racing with teardown.

Changes in v5:
- Patch 1: no code changes; carry Bryan's Reviewed-by.
- Patch 2: drop the duplicate work disable from venus_hfi_destroy(), keeping it only in venus_remove(), as requested by Bryan.
- Rebase onto media-committers.git next.

Compilation test completed.

v4: https://lore.kernel.org/all/[email protected]/

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 | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

base-commit: d7e7bca7169873b46e857c025c10f3336c1258d8

Signature · 1 line
2.53.0
#1 lore raw

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

venus_hfi_destroy() frees hdev and its interface queues before disabling the IRQ. A handler that already loaded core->priv can therefore access freed state even after core->priv is cleared.

Move disable_irq() before clearing core->priv and releasing HFI state so both the hard and threaded handlers have completed before teardown.

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 bd82066bb6e77f68ac3fe1440a8b5429ad62a192..e7e4e78a186adaa7fd80deeb094ed53d54f27ba6 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
maintainer Reviewed-by Bryan O'Donoghue on v5 1/2
#2 lore raw

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

Quoted 8 lines · Myeonghun Pak

venus_hfi_destroy() frees hdev and its interface queues before disabling the IRQ. A handler that already loaded core->priv can therefore access freed state even after core->priv is cleared.

Move disable_irq() before clearing core->priv and releasing HFI state so both the hard and threaded handlers have completed before teardown.

Fixes d96d3f30c0f2 ("[media] media: venus: hfi: add Venus HFI files")
Fixes 640803003cd9 ("media: venus: hfi: explicitly release IRQ during teardown")

No need to resend, I'll fix this tag too.

Quoted 33 lines
---
 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 bd82066bb6e77f68ac3fe1440a8b5429ad62a192..e7e4e78a186adaa7fd80deeb094ed53d54f27ba6 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

---
bod

#3 lore raw

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

In reply to Myeonghun Pak
Quoted 4 lines · Myeonghun Pak

venus_hfi_destroy() frees hdev and its interface queues before disabling the IRQ. A handler that already loaded core->priv can therefore access freed state even after core->priv is cleared.

Quoted 8 lines

Move disable_irq() before clearing core->priv and releasing HFI state so both the hard and threaded handlers have completed before teardown.

Fixes d96d3f30c0f2 ("[media] media: venus: hfi: add Venus HFI files")
Assisted-by LLM

Checkpatch reports an error for the Assisted-by: LLM tag:

WARNING: Non-standard signature: Assisted-by:
#18:

Assisted-by LLM

ERROR: Unrecognized email address: 'LLM'
#18:

Assisted-by LLM

total: 1 errors, 1 warnings, 0 checks, 12 lines checked

Quoted 25 lines
---
 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 bd82066bb6e77f68ac3fe1440a8b5429ad62a192..e7e4e78a186adaa7fd80deeb094ed53d54f27ba6 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;
 }
 
#4 lore raw

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

In reply to Myeonghun Pak

venus_remove() cancels core->work while the IRQ can still schedule it. A later IRQ can requeue recovery work, which may access HFI state after it has been freed.

Use disable_delayed_work_sync() at the start of remove to drain recovery and prevent further IRQ or worker requeues. Drain the work before disabling the IRQ so recovery can finish completion waits that depend on IRQs.

Fixes af2c3834c8ca ("[media] media: venus: adding core part and helper functions")
Assisted-by LLM
---
 drivers/media/platform/qcom/venus/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/qcom/venus/core.c +1 -1
index 7087af32060f1ed4190bf9f5d4a58cf88dad537d..6e495bc046913b46c926a498c062a6b55c6f7b64 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);
 
Signature · 1 line
2.53.0
maintainer Reviewed-by Bryan O'Donoghue on v5 2/2
#5 lore raw

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

Quoted 10 lines · Myeonghun Pak

venus_remove() cancels core->work while the IRQ can still schedule it. A later IRQ can requeue recovery work, which may access HFI state after it has been freed.

Use disable_delayed_work_sync() at the start of remove to drain recovery and prevent further IRQ or worker requeues. Drain the work before disabling the IRQ so recovery can finish completion waits that depend on IRQs.

Fixes af2c3834c8ca ("[media] media: venus: adding core part and helper functions")
Fixes c5a85ed88e04 ("media: venus: fix use after free bug in venus_remove due to race condition")

Don't resend I'll fix the tag for you.

Quoted 29 lines
---
 drivers/media/platform/qcom/venus/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/qcom/venus/core.c +1 -1
index 7087af32060f1ed4190bf9f5d4a58cf88dad537d..6e495bc046913b46c926a498c062a6b55c6f7b64 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);
 
Signature · 1 line
2.53.0

---
bod