Skip to contentSkip to conversation

media: amphion: fix VPU core lifetime during teardown

PATCH New Myeonghun Pak 1 participant 3 messages Oct 1

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

v5 cover media: amphion: fix VPU core lifetime during teardown

vpu_core_unregister() releases the message FIFO buffer and VPU reference before draining queued message work. An open video instance can also keep a pointer to devm-allocated core state while sysfs unbind removes the core or its parent.

The first patch drains message work before releasing core state. The second prevents manual unbind on both platform drivers and warns if core removal encounters a live instance.

Build-tested the Amphion driver for arm64 with LLVM 22 and W=1. Both patches pass checkpatch in strict mode.

Changes in v5:
- Rebase on the latest media-committers/next.
- Add Ming Qian's Reviewed-by tags to both patches. No functional changes.

Changes in v4:
- Resend through git send-email after the v3 Gmail submission altered diff headers and whitespace in transit. No code changes.

Changes in v3:
- Resend with intact diff headers after the v2 mail submission wrapped the diff --git lines. No code changes.

Changes in v2:
- Move vpu_core_put_vpu() after the work drain.
- Keep runtime PM and mailbox teardown unchanged.
- Address sysfs unbind in a separate patch.

Myeonghun Pak (2):
media: amphion: drain message work before releasing core state
media: amphion: prevent unbind while video instances are open

 drivers/media/platform/amphion/vpu_core.c | 18 +++++++++++-------
 drivers/media/platform/amphion/vpu_drv.c  |  1 +
 2 files changed, 12 insertions(+), 7 deletions(-)

base-commit: 4a069a763e8a86821edac4159f7c59a20015631b

Signature · 1 line
2.53.0
#1 lore raw

PATCH v5 1/2 media: amphion: drain message work before releasing core state

vpu_core_unregister() frees core->msg_buffer before cancelling the message workers, although core->msg_fifo still uses the buffer as its backing storage. A queued worker can read it after it has been freed.

Drain the core and instance message work first, then release core->vpu and the FIFO buffer. This follows the ordering in vpu_core_suspend().

Fixes 9f599f351e86 ("media: amphion: add vpu core driver")
---
 drivers/media/platform/amphion/vpu_core.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/media/platform/amphion/vpu_core.c +8 -7
index 85cc4a14f8ed..a0c58ec97a42 100644
@@ -299,22 +299,23 @@ static void vpu_core_put_vpu(struct vpu_core *core)
 	core->vpu->put_vpu(core->vpu);
 }
 
+static void vpu_core_cancel_work(struct vpu_core *core);
+
 static int vpu_core_unregister(struct device *dev, struct vpu_core *core)
 {
 	list_del_init(&core->list);
 
-	vpu_core_put_vpu(core);
-	core->vpu = NULL;
-	kfree(core->msg_buffer);
-	core->msg_buffer = NULL;
-
 	if (core->workqueue) {
-		cancel_work_sync(&core->msg_work);
-		cancel_delayed_work_sync(&core->msg_delayed_work);
+		vpu_core_cancel_work(core);
 		destroy_workqueue(core->workqueue);
 		core->workqueue = NULL;
 	}
 
+	vpu_core_put_vpu(core);
+	core->vpu = NULL;
+	kfree(core->msg_buffer);
+	core->msg_buffer = NULL;
+
 	return 0;
 }
 
Signature · 1 line
2.53.0
#2 lore raw

PATCH v5 2/2 media: amphion: prevent unbind while video instances are open

In reply to Myeonghun Pak

An open video instance holds a plain pointer to its devm-allocated core. Unbinding the core or its parent through sysfs can release that memory while the instance remains open. A later ioctl or close then uses the stale pointer.

The video file operations pin the module, but sysfs unbind bypasses that protection. Suppress manual bind and unbind for both platform drivers, and warn if core removal ever encounters a live instance.

Fixes 9f599f351e86 ("media: amphion: add vpu core driver")
---
 drivers/media/platform/amphion/vpu_core.c | 3 +++
 drivers/media/platform/amphion/vpu_drv.c  | 1 +
 2 files changed, 4 insertions(+)
drivers/media/platform/amphion/vpu_core.c +3 -0
index a0c58ec97a42..2e8e08e05de7 100644
@@ -696,6 +696,8 @@ static void vpu_core_remove(struct platform_device *pdev)
 	struct vpu_core *core = platform_get_drvdata(pdev);
 	int ret;
 
+	WARN_ON(!list_empty(&core->instances));
+
 	vpu_core_remove_dbgfs_file(core);
 	ret = pm_runtime_resume_and_get(dev);
 	WARN_ON(ret < 0);
@@ -846,6 +848,7 @@ static struct platform_driver amphion_vpu_core_driver = {
 	.remove = vpu_core_remove,
 	.driver = {
 		.name = "amphion-vpu-core",
+		.suppress_bind_attrs = true,
 		.of_match_table = vpu_core_dt_match,
 		.pm = &vpu_core_pm_ops,
 	},
drivers/media/platform/amphion/vpu_drv.c +1 -0
index 2cca61f41bea..9c6f32c5eae9 100644
@@ -205,6 +205,7 @@ static struct platform_driver amphion_vpu_driver = {
 	.remove = vpu_remove,
 	.driver = {
 		.name = "amphion-vpu",
+		.suppress_bind_attrs = true,
 		.of_match_table = vpu_dt_match,
 	},
 };
Signature · 1 line
2.53.0