From: sashiko-bot@kernel.org
To: "Anand Moon" <linux.amoon@gmail.com>
Cc: linux-media@vger.kernel.org, neil.armstrong@linaro.org,
linux-amlogic@lists.infradead.org
Subject: Re: [PATCH v6 4/8] media: meson: vdec: Condition buffer flushing on queue type in start_streaming
Date: Sat, 30 May 2026 10:43:54 +0000 [thread overview]
Message-ID: <20260530104354.E32EB1F00893@smtp.kernel.org> (raw)
In-Reply-To: <20260530094326.11892-5-linux.amoon@gmail.com>
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
New issues:
- [Critical] Double-free of codec DMA buffers in the error path of `vdec_start_streaming`.
- [Critical] During a dynamic resolution change, STREAMOFF bypasses hardware cleanup and leads to a critical Use-After-Free upon session closure.
Pre-existing issues:
- [High] The `sess->priv` context and codec DMA memory are permanently leaked if `vdec_poweron(sess)` fails.
--
commit 8f59207c5628689c8af221e3f815a3651ccc803f
Author: Anand Moon <linux.amoon@gmail.com>
media: meson: vdec: Condition buffer flushing on queue type in start_streaming
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.
Closes: https://lore.kernel.org/all/20260521090944.F35401F00A3D@smtp.kernel.org/
> diff --git a/drivers/staging/media/meson/vdec/vdec.c b/drivers/staging/media/meson/vdec/vdec.c
> index 8a5bf1a96830c..698a95566ad27 100644
> --- a/drivers/staging/media/meson/vdec/vdec.c
> +++ b/drivers/staging/media/meson/vdec/vdec.c
> @@ -386,15 +386,15 @@ static int vdec_start_streaming(struct vb2_queue *q, unsigned int count)
> sess->status = STATUS_STOPPED;
> mutex_unlock(&core->lock);
[Severity: Critical]
Can this error path cause a double-free of the codec DMA buffers?
Looking at the error handling above this diff in vdec_start_streaming():
drivers/staging/media/meson/vdec/vdec.c:vdec_start_streaming() {
[ ... ]
err_cleanup:
vdec_free_canvas(sess);
vdec_poweroff(sess);
if (codec_ops && codec_ops->stop && sess->priv) {
codec_ops->stop(sess);
[ ... ]
}
When vdec_poweroff(sess) is called, it cascades to __vdec_1_stop(), which
calls codec_ops->stop(sess). The cleanup block then explicitly calls
codec_ops->stop(sess) a second time.
If the codec stop function (such as codec_h264_stop) frees the DMA memory
allocations without nullifying the pointers, could passing the exact same DMA
addresses to dma_free_coherent() a second time result in a double-free and
heap corruption?
[Severity: High]
This is a pre-existing issue, but does this bypass the cleanup of the session
context and permanently leak memory?
During vdec_poweron(sess), the driver allocates the sess->priv context and
codec DMA buffers. If a subsequent initialization step fails, vdec_poweron
propagates the error, and vdec_start_streaming() jumps directly to
vififo_free:
drivers/staging/media/meson/vdec/vdec.c:vdec_start_streaming() {
[ ... ]
ret = vdec_poweron(sess);
if (ret)
goto vififo_free;
[ ... ]
err_cleanup:
[ ... ]
kfree(sess->priv);
[ ... ]
vififo_free:
[ ... ]
bufs_done:
[ ... ]
sess->status = STATUS_STOPPED;
}
This bypasses the err_cleanup: label and the kfree(sess->priv) cleanup.
Because the session status is then explicitly set to STATUS_STOPPED right
here at bufs_done, won't later calls to vdec_stop_streaming() evaluate
old_status == STATUS_STOPPED and skip the entire cleanup block, permanently
leaking the memory?
[Severity: Critical]
Does the newly introduced locking fix in vdec_stop_streaming() lead to a
Use-After-Free during a dynamic resolution change?
When the decoder detects a resolution change, it sets sess->status to
STATUS_NEEDS_RESUME. When userspace handles this by calling STREAMOFF on the
capture queue (the first call), vdec_stop_streaming() unconditionally sets
the status to STATUS_STOPPED at the start of the function:
drivers/staging/media/meson/vdec/vdec.c:vdec_stop_streaming() {
[ ... ]
mutex_lock(&core->lock);
old_status = sess->status;
[ ... ]
sess->status = STATUS_STOPPED;
mutex_unlock(&core->lock);
if (old_status == STATUS_RUNNING ||
old_status == STATUS_INIT ||
(old_status == STATUS_NEEDS_RESUME &&
(!sess->streamon_out || !sess->streamon_cap))) {
[ ... ]
}
Because sess->streamon_cap is only cleared at the end of the function, both
streams are still active on the first STREAMOFF, so the hardware cleanup
condition evaluates to false.
When the second STREAMOFF is called on the output queue, old_status is now
STATUS_STOPPED, so cleanup is skipped again. Since cleanup is completely
bypassed, vififo memory and the recycle_thread are leaked.
If the user then calls STREAMON, won't it spawn a duplicate recycle_thread,
overwriting the old pointer? When the session is eventually closed, could
vdec_close() free the session structure while the orphaned thread continues
executing, resulting in a Use-After-Free?
> - 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;
> }
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260530094326.11892-1-linux.amoon@gmail.com?part=4
_______________________________________________
linux-amlogic mailing list
linux-amlogic@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-amlogic
next prev parent reply other threads:[~2026-05-30 10:44 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-30 9:42 [PATCH v6 0/8] media: meson: Fix memory leak in error path in vdec Anand Moon
2026-05-30 9:42 ` [PATCH v6 1/8] media: meson: vdec: Fix memory leaks and lifetime of m2m device Anand Moon
2026-05-30 9:55 ` sashiko-bot
2026-05-30 9:42 ` [PATCH v6 2/8] media: meson: vdec: Fix concurrent STREAMON / STREAMOFF race conditions Anand Moon
2026-05-30 10:08 ` sashiko-bot
2026-05-30 9:42 ` [PATCH v6 3/8] media: meson: vdec: Handle kthread failure and free codec state Anand Moon
2026-05-30 10:25 ` sashiko-bot
2026-05-30 9:42 ` [PATCH v6 4/8] media: meson: vdec: Condition buffer flushing on queue type in start_streaming Anand Moon
2026-05-30 10:43 ` sashiko-bot [this message]
2026-05-30 9:42 ` [PATCH v6 5/8] media: meson: vdec: Cancel esparser work during teardown Anand Moon
2026-05-30 10:59 ` sashiko-bot
2026-05-30 9:42 ` [PATCH v6 6/8] media: meson: vdec: Configure DMA mask and segment size in probe Anand Moon
2026-05-30 11:10 ` sashiko-bot
2026-05-30 9:42 ` [PATCH v6 7/8] media: meson: vdec: Fix NULL pointer dereference in ISR handlers Anand Moon
2026-05-30 11:23 ` sashiko-bot
2026-05-30 9:42 ` [PATCH v6 8/8] gpu: drm: meson: Fix DMA max segment size for DMABUF imports Anand Moon
2026-05-30 11:35 ` sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260530104354.E32EB1F00893@smtp.kernel.org \
--to=sashiko-bot@kernel.org \
--cc=linux-amlogic@lists.infradead.org \
--cc=linux-media@vger.kernel.org \
--cc=linux.amoon@gmail.com \
--cc=neil.armstrong@linaro.org \
--cc=sashiko-reviews@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox