media: amphion: fix VPU core lifetime during teardown
Subsystems (3)
v4 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.
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: 4efa625593e1e9c76346232413997556dea1d89f
Signature · 1 line
2.53.0
PATCH v4 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().
--- drivers/media/platform/amphion/vpu_core.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/media/platform/amphion/vpu_core.c +8 -7
@@ -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
PATCH v4 2/2 media: amphion: prevent unbind while video instances are open
In reply to Myeonghun PakAn 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.
--- 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
@@ -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
@@ -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
Re: [PATCH v4 1/2] media: amphion: drain message work before releasing core state
In reply to Myeonghun PakQuoted 4 lines · Myeonghun Pak
vpu_core_unregister()freescore->msg_bufferbefore cancelling the message workers, althoughcore->msg_fifostill uses the buffer as its backing storage. A queued worker can read it after it has been freed.
Quoted 8 lines
Drain the core and instance message work first, then release
core->vpuand the FIFO buffer. This follows the ordering invpu_core_suspend().Fixes 9f599f351e86 ("media: amphion: add vpu core driver")Co-developed-by Ijae Kim <[email protected]>Signed-off-by Ijae Kim <[email protected]>Signed-off-by Myeonghun Pak <[email protected]>
Quoted 39 lines
--- drivers/media/platform/amphion/vpu_core.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)drivers/media/platform/amphion/vpu_core.c +8
-7@@ -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 ; }
Re: [PATCH v4 2/2] media: amphion: prevent unbind while video instances are open
In reply to Myeonghun PakQuoted 9 lines · 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.
Quoted 5 lines
Fixes 9f599f351e86 ("media: amphion: add vpu core driver")Co-developed-by Ijae Kim <[email protected]>Signed-off-by Ijae Kim <[email protected]>Signed-off-by Myeonghun Pak <[email protected]>
Quoted 38 lines
--- 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@@ -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@@ -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, }, };