media: v4l2-mem2mem: cancel job work before freeing m2m_dev
PATCH 1/1 media: v4l2-mem2mem: cancel job work before freeing m2m_dev
v4l2_m2m_release() frees the mem2mem device without draining its embedded job_work. A job completion can queue the work while device removal runs, leaving the worker to access a freed m2m_dev.
Cancel and wait for job_work before freeing the device. This ensures that a previously queued or running job worker has completed before the memory backing it is released.
The use-after-free was reproduced with an in-tree V4L2 test driver under KASAN during stream teardown and device unbind; the same injected scheduling sequence completed without the report after the change.
--- drivers/media/v4l2-core/v4l2-mem2mem.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/media/v4l2-core/v4l2-mem2mem.c +6 -0
@@ -1207,6 +1207,12 @@ EXPORT_SYMBOL_GPL(v4l2_m2m_init);void v4l2_m2m_release (struct v4l2_m2m_dev *m2m_dev) { +/* +* job_work is embedded in m2m_dev and is re-queued unconditionally +* whenever a job finishes; wait for it before freeing the device. +*/ +cancel_work_sync (&m2m_dev->job_work); +kfree (m2m_dev); }EXPORT_SYMBOL_GPL (v4l2_m2m_release);
Signature · 1 line
2.34.1
Hi,
Le mercredi 04 novembre 2026 à 17:50 +0800, Yaohan Li a écrit :
Quoted 8 lines
v4l2_m2m_release()frees the mem2mem device without draining its embeddedjob_work. A job completion can queue the work while device removal runs, leaving the worker to access a freedm2m_dev.Cancel and wait for
job_workbefore freeing the device. This ensures that a previously queued or running job worker has completed before the memory backing it is released.
Quoted 8 lines
The use-after-free was reproduced with an in-tree V4L2 test driver under KASAN during stream teardown and device unbind; the same injected scheduling sequence completed without the report after the change.
Fixes cbd9463da1b12 ("media: v4l2-mem2mem: Avoid calling .device_run in v4l2_m2m_job_finish")Assisted-by ZCode:GLM-4Signed-off-by Yaohan Li <[email protected]>
Please, check the mailing list before sending patches, there is a v2 of this change already. Its as simple as adding this step to your prompt really.
Nicolas
Quoted 21 lines
--- drivers/media/v4l2-core/v4l2-mem2mem.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/media/v4l2-core/v4l2-mem2mem.c +6
-0@@ -1207,6 +1207,12 @@ EXPORT_SYMBOL_GPL(v4l2_m2m_init);void v4l2_m2m_release (struct v4l2_m2m_dev *m2m_dev) { +/* +* job_work is embedded in m2m_dev and is re-queued unconditionally +* whenever a job finishes; wait for it before freeing the device. +*/ +cancel_work_sync (&m2m_dev->job_work); +kfree (m2m_dev); }EXPORT_SYMBOL_GPL (v4l2_m2m_release);