* [PATCH v5 0/6] media: meson: Fix memory leak in error path in vdec
@ 2026-05-25 9:51 Anand Moon
2026-05-25 9:51 ` [PATCH v5 1/6] media: meson: vdec: Fix memory leak in error path of vdec_open Anand Moon
` (5 more replies)
0 siblings, 6 replies; 7+ messages in thread
From: Anand Moon @ 2026-05-25 9:51 UTC (permalink / raw)
To: Neil Armstrong, Mauro Carvalho Chehab, Greg Kroah-Hartman,
Kevin Hilman, Jerome Brunet, Martin Blumenstingl, Maxime Jourdan,
Hans Verkuil,
open list:MESON VIDEO DECODER DRIVER FOR AMLOGIC SOCS,
open list:MESON VIDEO DECODER DRIVER FOR AMLOGIC SOCS,
open list:STAGING SUBSYSTEM,
moderated list:ARM/Amlogic Meson SoC support, open list
Cc: Anand Moon, Sashiko
V5: Changes
Following chamges try to fix the memory leak reported by Sashiko
New issues:
- [High] The newly added error path in `vdec_start_streaming()` leaks
`sess->priv` when `kthread_run()` fails.
Pre-existing issues:
- [Critical] Race condition between hardware power-on and `core->cur_sess`
initialization leads to a NULL pointer dereference in the IRQ handler.
- [High] Returning buffers for both source and destination queues upon
single-queue failure orphans active queue buffers.
- [High] Concurrent sessions can bypass the hardware exclusivity check,
leading to simultaneous hardware programming.
--
Reported-by: Sashiko <sashiko-bot@kernel.org>
https://lore.kernel.org/all/20260521090944.F35401F00A3D@smtp.kernel.org/
V4: Changes:
Following chamges try to fix the memory leak reported by Sashiko
Pre-existing issues:
- [Critical] The `sess->esparser_queue_work` work item is not canceled
before freeing the session context, leading to a potential Use-After-Free
vulnerability.
- [High] The patch attempts to fix a memory leak reported by kmemleak,
but misdiagnoses the root cause and leaves the primary memory leak
(the V4L2 control handler) unresolved.
- [High] The driver does not verify if `kthread_run()` returns an `ERR_PTR`,
leading to a kernel panic when `kthread_stop()` is called.
Reported-by: Sashiko <sashiko-bot@kernel.org>
https://lore.kernel.org/all/20260520045905.6ACBA1F000E9@smtp.kernel.org/#t
Thanks
-Anand
Anand Moon (6):
media: meson: vdec: Fix memory leak in error path of vdec_open
media: meson: vdec: Protect session exclusivity check with lock
media: meson: vdec: Set cur_sess before hardware vdec_poweron()
media: meson: vdec: Handle kthread error and free codec private data
media: meson: vdec: Isolate error path buffer flush to the active
queue
media: meson: vdec: Cancel esparser work in error and stop paths
drivers/staging/media/meson/vdec/vdec.c | 54 ++++++++++++++++++++-----
1 file changed, 44 insertions(+), 10 deletions(-)
base-commit: e7ae89a0c97ce2b68b0983cd01eda67cf373517d
--
2.50.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v5 1/6] media: meson: vdec: Fix memory leak in error path of vdec_open
2026-05-25 9:51 [PATCH v5 0/6] media: meson: Fix memory leak in error path in vdec Anand Moon
@ 2026-05-25 9:51 ` Anand Moon
2026-05-25 9:51 ` [PATCH v5 2/6] media: meson: vdec: Protect session exclusivity check with lock Anand Moon
` (4 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Anand Moon @ 2026-05-25 9:51 UTC (permalink / raw)
To: Neil Armstrong, Mauro Carvalho Chehab, Greg Kroah-Hartman,
Kevin Hilman, Jerome Brunet, Martin Blumenstingl, Hans Verkuil,
Maxime Jourdan,
open list:MESON VIDEO DECODER DRIVER FOR AMLOGIC SOCS,
open list:MESON VIDEO DECODER DRIVER FOR AMLOGIC SOCS,
open list:STAGING SUBSYSTEM,
moderated list:ARM/Amlogic Meson SoC support, open list
Cc: Anand Moon, Nicolas Dufresne, Sashiko
The vdec_open() function previously jumped directly to err_m2m_release
when vdec_init_ctrls() failed, skipping release of the m2m context.
This caused a resource leak.
Fix it by introducing a proper err_m2m_ctx_release label that calls
v4l2_m2m_ctx_release(sess->m2m_ctx) before releasing the m2m device.
Also free the v4l2 control handler memory allocated by vdec_init_ctrls()
in vdec_close().
This was identified via kmemleak:
unreferenced object 0xffff0000205d6878 (size 8):
comm "v4l_id", pid 5289, jiffies 4294938580
hex dump (first 8 bytes):
40 d2 49 18 00 00 ff ff @.I.....
backtrace (crc d3204599):
kmemleak_alloc+0xc8/0xf0
__kvmalloc_node_noprof+0x60c/0x850
v4l2_ctrl_handler_init_class+0x1b4/0x2e8 [videodev]
vdec_open+0x1f4/0x788 [meson_vdec]
v4l2_open+0x144/0x460 [videodev]
chrdev_open+0x1ac/0x500
do_dentry_open+0x3f0/0xfe8
vfs_open+0x68/0x320
do_open+0x2d8/0x9a8
path_openat+0x1d0/0x4f0
do_filp_open+0x190/0x380
do_sys_openat2+0xf8/0x1b0
__arm64_sys_openat+0x13c/0x1e8
invoke_syscall+0xdc/0x268
el0_svc_common.constprop.0+0x178/0x258
do_el0_svc+0x4c/0x70
Cc: Nicolas Dufresne <nicolas@ndufresne.ca>
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/20260520045905.6ACBA1F000E9@smtp.kernel.org/#t
Fixes: 3e7f51bd9607 ("media: meson: add v4l2 m2m video decoder driver")
Signed-off-by: Anand Moon <linux.amoon@gmail.com>
---
v5: update the error path for v4l2_ctrl_handler_free() as per the review
ccmment to fix the use after free bug.
[4] https://patchwork.kernel.org/project/linux-amlogic/patch/20260521073449.10057-2-linux.amoon@gmail.com/
v4: update the commit message to add v4l2_ctrl_handler_free() in vdec_close()
to adderss the issue:
This isn't a bug introduced by this patch, but does vdec_close() properly
free the v4l2 control handler memory allocated by vdec_init_ctrls() here?
v3: https://lore.kernel.org/all/20260520044046.7553-1-linux.amoon@gmail.com/
update the commit messagee.
v2: https://lore.kernel.org/all/20260321065408.209723-1-linux.amoon@gmail.com/
updated the commit message, applied the suggestion from sashiko below.
[3] https://sashiko.dev/#/patchset/20260321065408.209723-1-linux.amoon%40gmail.com
v1: https://lore.kernel.org/all/20260304100557.126488-1-linux.amoon@gmail.com/
tried to address the issue reported by Nicolas improve the commit message.
---
drivers/staging/media/meson/vdec/vdec.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/drivers/staging/media/meson/vdec/vdec.c b/drivers/staging/media/meson/vdec/vdec.c
index 4b77ec1af5a7..18a22b79e835 100644
--- a/drivers/staging/media/meson/vdec/vdec.c
+++ b/drivers/staging/media/meson/vdec/vdec.c
@@ -889,7 +889,7 @@ static int vdec_open(struct file *file)
ret = vdec_init_ctrls(sess);
if (ret)
- goto err_m2m_release;
+ goto err_m2m_ctx_release;
sess->pixfmt_cap = formats[0].pixfmts_cap[0];
sess->fmt_out = &formats[0];
@@ -913,6 +913,8 @@ static int vdec_open(struct file *file)
return 0;
+err_m2m_ctx_release:
+ v4l2_m2m_ctx_release(sess->m2m_ctx);
err_m2m_release:
v4l2_m2m_release(sess->m2m_dev);
err_free_sess:
@@ -928,6 +930,7 @@ static int vdec_close(struct file *file)
v4l2_m2m_release(sess->m2m_dev);
v4l2_fh_del(&sess->fh, file);
v4l2_fh_exit(&sess->fh);
+ v4l2_ctrl_handler_free(&sess->ctrl_handler);
mutex_destroy(&sess->lock);
mutex_destroy(&sess->bufs_recycle_lock);
--
2.50.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v5 2/6] media: meson: vdec: Protect session exclusivity check with lock
2026-05-25 9:51 [PATCH v5 0/6] media: meson: Fix memory leak in error path in vdec Anand Moon
2026-05-25 9:51 ` [PATCH v5 1/6] media: meson: vdec: Fix memory leak in error path of vdec_open Anand Moon
@ 2026-05-25 9:51 ` Anand Moon
2026-05-25 9:51 ` [PATCH v5 3/6] media: meson: vdec: Set cur_sess before hardware vdec_poweron() Anand Moon
` (3 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Anand Moon @ 2026-05-25 9:51 UTC (permalink / raw)
To: Neil Armstrong, Mauro Carvalho Chehab, Greg Kroah-Hartman,
Kevin Hilman, Jerome Brunet, Martin Blumenstingl, Hans Verkuil,
Maxime Jourdan,
open list:MESON VIDEO DECODER DRIVER FOR AMLOGIC SOCS,
open list:MESON VIDEO DECODER DRIVER FOR AMLOGIC SOCS,
open list:STAGING SUBSYSTEM,
moderated list:ARM/Amlogic Meson SoC support, open list
Cc: Anand Moon, Nicolas Dufresne, Sashiko
Add the check for an active hardware session is performed without holding
the core->lock mutex. In multi-threaded environments, two concurrent
STREAMON ioctls on different file descriptors can simultaneously find
core->cur_sess to be NULL, bypass the check, and concurrently call
vdec_poweron(), corrupting hardware state.
Fix this by wrapping the session exclusivity check inside core->lock.
Cc: Nicolas Dufresne <nicolas@ndufresne.ca>
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/20260521090944.F35401F00A3D@smtp.kernel.org/
Fixes: 3e7f51bd9607 ("media: meson: add v4l2 m2m video decoder driver")
Signed-off-by: Anand Moon <linux.amoon@gmail.com>
---
v5: New patch.
[High] Concurrent sessions can bypass the hardware exclusivity check,
leading to simultaneous hardware programming.
---
drivers/staging/media/meson/vdec/vdec.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/drivers/staging/media/meson/vdec/vdec.c b/drivers/staging/media/meson/vdec/vdec.c
index 18a22b79e835..e72f54af026e 100644
--- a/drivers/staging/media/meson/vdec/vdec.c
+++ b/drivers/staging/media/meson/vdec/vdec.c
@@ -286,10 +286,13 @@ static int vdec_start_streaming(struct vb2_queue *q, unsigned int count)
struct vb2_v4l2_buffer *buf;
int ret;
+ mutex_lock(&core->lock);
if (core->cur_sess && core->cur_sess != sess) {
+ mutex_unlock(&core->lock);
ret = -EBUSY;
goto bufs_done;
}
+ mutex_unlock(&core->lock);
if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
sess->streamon_out = 1;
--
2.50.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v5 3/6] media: meson: vdec: Set cur_sess before hardware vdec_poweron()
2026-05-25 9:51 [PATCH v5 0/6] media: meson: Fix memory leak in error path in vdec Anand Moon
2026-05-25 9:51 ` [PATCH v5 1/6] media: meson: vdec: Fix memory leak in error path of vdec_open Anand Moon
2026-05-25 9:51 ` [PATCH v5 2/6] media: meson: vdec: Protect session exclusivity check with lock Anand Moon
@ 2026-05-25 9:51 ` Anand Moon
2026-05-25 9:51 ` [PATCH v5 4/6] media: meson: vdec: Handle kthread error and free codec private data Anand Moon
` (2 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Anand Moon @ 2026-05-25 9:51 UTC (permalink / raw)
To: Neil Armstrong, Mauro Carvalho Chehab, Greg Kroah-Hartman,
Kevin Hilman, Jerome Brunet, Martin Blumenstingl, Maxime Jourdan,
Hans Verkuil,
open list:MESON VIDEO DECODER DRIVER FOR AMLOGIC SOCS,
open list:MESON VIDEO DECODER DRIVER FOR AMLOGIC SOCS,
open list:STAGING SUBSYSTEM,
moderated list:ARM/Amlogic Meson SoC support, open list
Cc: Anand Moon, Nicolas Dufresne, Sashiko
vdec_poweron() initializes hardware and unmasks device interrupts. If an
interrupt fires before core->cur_sess is set, vdec_isr() dereferences a
NULL pointer when updating sess->last_irq_jiffies, leading to a kernel
panic.
Fix this by assigning core->cur_sess and updating sess->status under
core->lock before calling vdec_poweron(). This ensures the interrupt
handler always sees a valid session pointer. On the error path, clear
core->cur_sess and reset sess->status to STATUS_STOPPED to avoid stale
references.
Following change also strengthens the hardware exclusivity check by
holding core->lock during session assignment, preventing concurrent
sessions from racing through cur_sess == NULL and corrupting hardware
state.
Cc: Nicolas Dufresne <nicolas@ndufresne.ca>
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/20260521090944.F35401F00A3D@smtp.kernel.org/
Fixes: 3e7f51bd9607 ("media: meson: add v4l2 m2m video decoder driver")
Signed-off-by: Anand Moon <linux.amoon@gmail.com>
---
v5: [Critical] Race condition between hardware power-on and `core->cur_sess`
initialization leads to a NULL pointer dereference in the IRQ handler.
---
drivers/staging/media/meson/vdec/vdec.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/drivers/staging/media/meson/vdec/vdec.c b/drivers/staging/media/meson/vdec/vdec.c
index e72f54af026e..52ace4de967c 100644
--- a/drivers/staging/media/meson/vdec/vdec.c
+++ b/drivers/staging/media/meson/vdec/vdec.c
@@ -334,6 +334,11 @@ static int vdec_start_streaming(struct vb2_queue *q, unsigned int count)
atomic_set(&sess->esparser_queued_bufs, 0);
v4l2_ctrl_s_ctrl(sess->ctrl_min_buf_capture, 1);
+ mutex_lock(&core->lock);
+ core->cur_sess = sess;
+ sess->status = STATUS_INIT;
+ mutex_unlock(&core->lock);
+
ret = vdec_poweron(sess);
if (ret)
goto vififo_free;
@@ -344,12 +349,14 @@ static int vdec_start_streaming(struct vb2_queue *q, unsigned int count)
sess->recycle_thread = kthread_run(vdec_recycle_thread, sess,
"vdec_recycle");
- sess->status = STATUS_INIT;
- core->cur_sess = sess;
schedule_work(&sess->esparser_queue_work);
return 0;
vififo_free:
+ mutex_lock(&core->lock);
+ core->cur_sess = NULL;
+ sess->status = STATUS_STOPPED;
+ mutex_unlock(&core->lock);
dma_free_coherent(sess->core->dev, sess->vififo_size,
sess->vififo_vaddr, sess->vififo_paddr);
bufs_done:
--
2.50.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v5 4/6] media: meson: vdec: Handle kthread error and free codec private data
2026-05-25 9:51 [PATCH v5 0/6] media: meson: Fix memory leak in error path in vdec Anand Moon
` (2 preceding siblings ...)
2026-05-25 9:51 ` [PATCH v5 3/6] media: meson: vdec: Set cur_sess before hardware vdec_poweron() Anand Moon
@ 2026-05-25 9:51 ` Anand Moon
2026-05-25 9:51 ` [PATCH v5 5/6] media: meson: vdec: Isolate error path buffer flush to the active queue Anand Moon
2026-05-25 9:51 ` [PATCH v5 6/6] media: meson: vdec: Cancel esparser work in error and stop paths Anand Moon
5 siblings, 0 replies; 7+ messages in thread
From: Anand Moon @ 2026-05-25 9:51 UTC (permalink / raw)
To: Neil Armstrong, Mauro Carvalho Chehab, Greg Kroah-Hartman,
Kevin Hilman, Jerome Brunet, Martin Blumenstingl, Hans Verkuil,
Maxime Jourdan,
open list:MESON VIDEO DECODER DRIVER FOR AMLOGIC SOCS,
open list:MESON VIDEO DECODER DRIVER FOR AMLOGIC SOCS,
open list:STAGING SUBSYSTEM,
moderated list:ARM/Amlogic Meson SoC support, open list
Cc: Anand Moon, Nicolas Dufresne, Sashiko
vdec_start_streaming() launches a recycle thread when required by the
codec. If kthread_run() fails, the previous error path only powered off
the hardware, leaving sess->priv and codec state allocated. This caused
a permanent leak of the codec context and associated DMA buffers.
Fix this by adding an err_cleanup path: if thread creation fails, call
codec_ops->stop() to release the codec context and clear sess->priv,
then power off the hardware. Also reset core->cur_sess and sess->status
to avoid stale references.
This change closes the memory leak on kthread_run() failure and ensures
proper cleanup of codec resources.
Cc: Nicolas Dufresne <nicolas@ndufresne.ca>
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/20260521090944.F35401F00A3D@smtp.kernel.org/
Fixes: 3e7f51bd9607 ("media: meson: add v4l2 m2m video decoder driver")
Signed-off-by: Anand Moon <linux.amoon@gmail.com>
---
v5: The vdec_poweron() function invoked earlier allocates dynamic memory for
the codec context and assigns it to sess->priv. When kthread_run() fails,
this new error path calls vdec_poweroff() which stops the hardware but
doesn't free sess->priv.
---
drivers/staging/media/meson/vdec/vdec.c | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)
diff --git a/drivers/staging/media/meson/vdec/vdec.c b/drivers/staging/media/meson/vdec/vdec.c
index 52ace4de967c..b31bf08af88e 100644
--- a/drivers/staging/media/meson/vdec/vdec.c
+++ b/drivers/staging/media/meson/vdec/vdec.c
@@ -345,13 +345,25 @@ static int vdec_start_streaming(struct vb2_queue *q, unsigned int count)
sess->sequence_cap = 0;
sess->sequence_out = 0;
- if (vdec_codec_needs_recycle(sess))
+ if (vdec_codec_needs_recycle(sess)) {
sess->recycle_thread = kthread_run(vdec_recycle_thread, sess,
"vdec_recycle");
+ if (IS_ERR(sess->recycle_thread)) {
+ ret = PTR_ERR(sess->recycle_thread);
+ sess->recycle_thread = NULL;
+ goto err_cleanup;
+ }
+ }
schedule_work(&sess->esparser_queue_work);
return 0;
+err_cleanup:
+ if (codec_ops && codec_ops->stop && sess->priv) {
+ codec_ops->stop(sess);
+ sess->priv = NULL;
+ }
+ vdec_poweroff(sess);
vififo_free:
mutex_lock(&core->lock);
core->cur_sess = NULL;
--
2.50.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v5 5/6] media: meson: vdec: Isolate error path buffer flush to the active queue
2026-05-25 9:51 [PATCH v5 0/6] media: meson: Fix memory leak in error path in vdec Anand Moon
` (3 preceding siblings ...)
2026-05-25 9:51 ` [PATCH v5 4/6] media: meson: vdec: Handle kthread error and free codec private data Anand Moon
@ 2026-05-25 9:51 ` Anand Moon
2026-05-25 9:51 ` [PATCH v5 6/6] media: meson: vdec: Cancel esparser work in error and stop paths Anand Moon
5 siblings, 0 replies; 7+ messages in thread
From: Anand Moon @ 2026-05-25 9:51 UTC (permalink / raw)
To: Neil Armstrong, Mauro Carvalho Chehab, Greg Kroah-Hartman,
Kevin Hilman, Jerome Brunet, Martin Blumenstingl, Hans Verkuil,
Maxime Jourdan,
open list:MESON VIDEO DECODER DRIVER FOR AMLOGIC SOCS,
open list:MESON VIDEO DECODER DRIVER FOR AMLOGIC SOCS,
open list:STAGING SUBSYSTEM,
moderated list:ARM/Amlogic Meson SoC support, open list
Cc: Anand Moon, Nicolas Dufresne, Sashiko
When vdec_start_streaming() fails, the error path clears buffers from both
the source and destination queues unconditionally. If one queue was already
streaming successfully from a prior invocation, flushing its buffers behind
its back leaves videobuf2 deadlocked waiting for completions.
Fix this by only sweeping buffers from the specific queue type container
that failed to initialize.
Cc: Nicolas Dufresne <nicolas@ndufresne.ca>
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/20260521090944.F35401F00A3D@smtp.kernel.org/
Fixes: 3e7f51bd9607 ("media: meson: add v4l2 m2m video decoder driver")
Signed-off-by: Anand Moon <linux.amoon@gmail.com>
---
v5:
This is a pre-existing issue, but will returning buffers for both queues upon
a single-queue failure orphan active queue buffers?
If the CAPTURE queue was successfully started in a previous call, returning
its buffers puts them back into the vb2 queued list while the driver
discards its references. Because the CAPTURE queue remains active,
userspace calling DQBUF will hang indefinitely waiting for frames that
the driver will never process.
---
drivers/staging/media/meson/vdec/vdec.c | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/drivers/staging/media/meson/vdec/vdec.c b/drivers/staging/media/meson/vdec/vdec.c
index b31bf08af88e..925537bd4d0b 100644
--- a/drivers/staging/media/meson/vdec/vdec.c
+++ b/drivers/staging/media/meson/vdec/vdec.c
@@ -372,15 +372,15 @@ static int vdec_start_streaming(struct vb2_queue *q, unsigned int count)
dma_free_coherent(sess->core->dev, sess->vififo_size,
sess->vififo_vaddr, sess->vififo_paddr);
bufs_done:
- while ((buf = v4l2_m2m_src_buf_remove(sess->m2m_ctx)))
- v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
- while ((buf = v4l2_m2m_dst_buf_remove(sess->m2m_ctx)))
- v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
-
- if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
+ if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
sess->streamon_out = 0;
- else
+ while ((buf = v4l2_m2m_src_buf_remove(sess->m2m_ctx)))
+ v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
+ } else {
sess->streamon_cap = 0;
+ while ((buf = v4l2_m2m_dst_buf_remove(sess->m2m_ctx)))
+ v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
+ }
return ret;
}
--
2.50.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v5 6/6] media: meson: vdec: Cancel esparser work in error and stop paths
2026-05-25 9:51 [PATCH v5 0/6] media: meson: Fix memory leak in error path in vdec Anand Moon
` (4 preceding siblings ...)
2026-05-25 9:51 ` [PATCH v5 5/6] media: meson: vdec: Isolate error path buffer flush to the active queue Anand Moon
@ 2026-05-25 9:51 ` Anand Moon
5 siblings, 0 replies; 7+ messages in thread
From: Anand Moon @ 2026-05-25 9:51 UTC (permalink / raw)
To: Neil Armstrong, Mauro Carvalho Chehab, Greg Kroah-Hartman,
Kevin Hilman, Jerome Brunet, Martin Blumenstingl, Maxime Jourdan,
Hans Verkuil,
open list:MESON VIDEO DECODER DRIVER FOR AMLOGIC SOCS,
open list:MESON VIDEO DECODER DRIVER FOR AMLOGIC SOCS,
open list:STAGING SUBSYSTEM,
moderated list:ARM/Amlogic Meson SoC support, open list
Cc: Anand Moon, Nicolas Dufresne, Sashiko
The esparser workqueue may remain pending when streaming is stopped or
the device is closed, leading to use-after-free if it runs after session
teardown. vdec_start_streaming(), vdec_stop_streaming(), and vdec_close()
did not cancel this work, leaving a race between session cleanup and
work execution.
Fix this by calling cancel_work_sync(&sess->esparser_queue_work) in all
cleanup paths. Unlocking and relocking sess->lock around the cancel
ensures the work handler cannot run concurrently with teardown. This
prevents dangling work items from accessing freed session memory and
eliminates a potential kernel crash.
Cc: Nicolas Dufresne <nicolas@ndufresne.ca>
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/20260520045905.6ACBA1F000E9@smtp.kernel.org/#t
Fixes: 3e7f51bd9607 ("media: meson: add v4l2 m2m video decoder driver")
Signed-off-by: Anand Moon <linux.amoon@gmail.com>
---
v5: Tried to fix the order of cancel_work_sync() which could lead to
a use-after-free. update the commit message.
---
drivers/staging/media/meson/vdec/vdec.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/drivers/staging/media/meson/vdec/vdec.c b/drivers/staging/media/meson/vdec/vdec.c
index 925537bd4d0b..296b387f3667 100644
--- a/drivers/staging/media/meson/vdec/vdec.c
+++ b/drivers/staging/media/meson/vdec/vdec.c
@@ -372,6 +372,10 @@ static int vdec_start_streaming(struct vb2_queue *q, unsigned int count)
dma_free_coherent(sess->core->dev, sess->vififo_size,
sess->vififo_vaddr, sess->vififo_paddr);
bufs_done:
+ mutex_unlock(&sess->lock);
+ cancel_work_sync(&sess->esparser_queue_work);
+ mutex_lock(&sess->lock);
+
if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
sess->streamon_out = 0;
while ((buf = v4l2_m2m_src_buf_remove(sess->m2m_ctx)))
@@ -430,6 +434,9 @@ static void vdec_stop_streaming(struct vb2_queue *q)
kthread_stop(sess->recycle_thread);
vdec_poweroff(sess);
+ mutex_unlock(&sess->lock);
+ cancel_work_sync(&sess->esparser_queue_work);
+ mutex_lock(&sess->lock);
vdec_free_canvas(sess);
dma_free_coherent(sess->core->dev, sess->vififo_size,
sess->vififo_vaddr, sess->vififo_paddr);
@@ -948,6 +955,8 @@ static int vdec_close(struct file *file)
{
struct amvdec_session *sess = file_to_amvdec_session(file);
+ cancel_work_sync(&sess->esparser_queue_work);
+
v4l2_m2m_ctx_release(sess->m2m_ctx);
v4l2_m2m_release(sess->m2m_dev);
v4l2_fh_del(&sess->fh, file);
--
2.50.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-05-25 9:53 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-25 9:51 [PATCH v5 0/6] media: meson: Fix memory leak in error path in vdec Anand Moon
2026-05-25 9:51 ` [PATCH v5 1/6] media: meson: vdec: Fix memory leak in error path of vdec_open Anand Moon
2026-05-25 9:51 ` [PATCH v5 2/6] media: meson: vdec: Protect session exclusivity check with lock Anand Moon
2026-05-25 9:51 ` [PATCH v5 3/6] media: meson: vdec: Set cur_sess before hardware vdec_poweron() Anand Moon
2026-05-25 9:51 ` [PATCH v5 4/6] media: meson: vdec: Handle kthread error and free codec private data Anand Moon
2026-05-25 9:51 ` [PATCH v5 5/6] media: meson: vdec: Isolate error path buffer flush to the active queue Anand Moon
2026-05-25 9:51 ` [PATCH v5 6/6] media: meson: vdec: Cancel esparser work in error and stop paths Anand Moon
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox