mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] media: v4l2-mem2mem: cancel job work before freeing m2m_dev
@ 2026-11-04  9:50 Yaohan Li
  2026-11-04 13:39 ` Nicolas Dufresne
  0 siblings, 1 reply; 2+ messages in thread
From: Yaohan Li @ 2026-11-04  9:50 UTC (permalink / raw)
  To: mchehab
  Cc: hverkuil+cisco, nicolas.dufresne, Frank.Li, ming.qian, dmantipov,
	121811682, ezequiel, linux-media, linux-kernel

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.

Fixes: cbd9463da1b12 ("media: v4l2-mem2mem: Avoid calling .device_run in v4l2_m2m_job_finish")
Assisted-by: ZCode:GLM-4
Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>
---
 drivers/media/v4l2-core/v4l2-mem2mem.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/media/v4l2-core/v4l2-mem2mem.c b/drivers/media/v4l2-core/v4l2-mem2mem.c
index a65cbb124cfe0..c4756e7bb1fb2 100644
--- a/drivers/media/v4l2-core/v4l2-mem2mem.c
+++ b/drivers/media/v4l2-core/v4l2-mem2mem.c
@@ -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);
-- 
2.34.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] media: v4l2-mem2mem: cancel job work before freeing m2m_dev
  2026-11-04  9:50 [PATCH] media: v4l2-mem2mem: cancel job work before freeing m2m_dev Yaohan Li
@ 2026-11-04 13:39 ` Nicolas Dufresne
  0 siblings, 0 replies; 2+ messages in thread
From: Nicolas Dufresne @ 2026-11-04 13:39 UTC (permalink / raw)
  To: Yaohan Li, mchehab
  Cc: hverkuil+cisco, Frank.Li, ming.qian, dmantipov, 121811682,
	ezequiel, linux-media, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1780 bytes --]

Hi,

Le mercredi 04 novembre 2026 à 17:50 +0800, Yaohan Li a écrit :
> 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.
> 
> Fixes: cbd9463da1b12 ("media: v4l2-mem2mem: Avoid calling .device_run in v4l2_m2m_job_finish")
> Assisted-by: ZCode:GLM-4
> Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>


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

> ---
>  drivers/media/v4l2-core/v4l2-mem2mem.c | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/drivers/media/v4l2-core/v4l2-mem2mem.c b/drivers/media/v4l2-core/v4l2-mem2mem.c
> index a65cbb124cfe0..c4756e7bb1fb2 100644
> --- a/drivers/media/v4l2-core/v4l2-mem2mem.c
> +++ b/drivers/media/v4l2-core/v4l2-mem2mem.c
> @@ -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);

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-04 13:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-04  9:50 [PATCH] media: v4l2-mem2mem: cancel job work before freeing m2m_dev Yaohan Li
2026-11-04 13:39 ` Nicolas Dufresne

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®