From: sashiko-bot@kernel.org
To: "Charvi Mehta" <charvi.mehta@arm.com>, devicetree@vger.kernel.org
Cc: robh@kernel.org, conor+dt@kernel.org, media-ci@linuxtv.org
Subject: Re: [RFC PATCH 20/28] media: mali-vpu: implement vb2 buffer queue handling
Date: Thu, 19 Nov 2026 16:06:08 +0000 [thread overview]
Message-ID: <sashiko-outbox-223466@kernel.org> (raw)
In-Reply-To: <20261119153758.1426673-21-charvi.mehta@arm.com>
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] media: mali-vpu: out-of-bounds VA mapping in
mali_vpu_buffer_plane_map()
- [High] media: mali-vpu: wrong capture DMA sync in
mali_vpu_queue_buffer()
- [High] media: mali-vpu: missing MMU flush in
mali_vpu_vb2_buffer_cleanup()
- [Medium] media: mali-vpu: stranded buffers on error in
mali_vpu_submit_deferred_bufs()
- [Medium] media: mali-vpu: unreachable 0-byte EOS check without
allow_zero_bytesused
--
commit 8e09878f27886489a304a46715a5c2d9a14beef9
Author: Rath Sankaralingam <rath.sankaralingam@arm.com>
media: mali-vpu: implement vb2 buffer queue handling
This commit implements the vb2 callbacks for initializing, queuing, and
cleaning up Mali VPU buffers, along with MMU mapping, cache synchronization,
and deferred buffer submission when the firmware enters the RUNNING state.
> diff --git a/drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c b/drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c
> index 7ace0489996a8..c1df3e2d4eacf 100644
> --- a/drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c
> +++ b/drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c
[ ... ]
> @@ -19,6 +50,82 @@ int mali_vpu_vb2_buffer_init(struct vb2_buffer *vb2_buf)
> return -EINVAL;
> }
>
> + vpu_buf->dev = sess->vpu_dev->dev;
[ ... ]
> +static void mali_vpu_buffer_update_flags_from_v4l2(struct vb2_buffer *vb2_buf)
> +{
> + struct vb2_v4l2_buffer *v4l2_buf = to_vb2_v4l2_buffer(vb2_buf);
> + struct mali_vpu_buffer *vpu_buf = to_mali_vpu_buffer(v4l2_buf);
> +
> + u32 flags = v4l2_buf->flags;
> +
> + vpu_buf->flags = 0;
> +
> + if (V4L2_TYPE_IS_OUTPUT(vb2_buf->type) && mali_vpu_buffer_get_bytesused(vb2_buf) == 0)
> + flags |= V4L2_BUF_FLAG_LAST;
> +
> + if (flags & V4L2_BUF_FLAG_LAST)
> + vpu_buf->flags |= MALI_VPU_BUFFER_EOS;
> +}
[Severity: Medium]
Can mali_vpu_buffer_get_bytesused(vb2_buf) == 0 ever be reached on an
OUTPUT buffer without vq->allow_zero_bytesused set?
mali_vpu_queue_init_type() does not set allow_zero_bytesused on the OUTPUT
vb2_queue, so vb2_fill_vb2_v4l2_buffer() replaces a 0-byte bytesused from
userspace with the full plane length. As a result, a 0-byte OUTPUT buffer
queued to signal EOS will instead be given a payload_size equal to the full
buffer capacity rather than setting MALI_VPU_BUFFER_EOS. Also,
mali_vpu_buffer_get_bytesused() sums b->planes[i].bytesused without
subtracting data_offset, so an empty payload with a non-zero data_offset
will not be treated as 0 bytes used either.
[ ... ]
> @@ -49,3 +156,171 @@ int mali_vpu_vb2_buffer_out_validate(struct vb2_buffer *vb2_buf)
>
> return 0;
> }
> +
> +static int mali_vpu_buffer_plane_map(struct mali_vpu_buffer *vpu_buf, mali_vpu_mmu_va begin,
> + mali_vpu_mmu_va end, u32 plane)
> +{
> + while (begin < end) {
> + struct mali_vpu_buffer_plane *p = &vpu_buf->planes[plane];
> + int ret;
> +
> + ret = mali_vpu_mmu_map_pages(vpu_buf->mmu, begin, p->pages,
> + MALI_VPU_ATTR_SHARED_RW, MALI_VPU_ACCESS_READ_WRITE);
> + if (!ret) {
> + dev_dbg(vpu_buf->dev,
> + "memory map buffer. buf=%p, plane=%u, va=0x%x, size=%zu.",
> + vpu_buf, plane, p->pages->va,
> + mali_vpu_mmu_size_pages(p->pages));
> + return 0;
> + }
> +
> + if (ret != -EAGAIN)
> + return ret;
> +
> + begin += SZ_1M;
> + }
> +
> + return -ENOMEM;
> +}
[Severity: High]
Can mali_vpu_buffer_plane_map() map a plane past the end of the firmware
virtual address region?
The loop only checks begin < end, and mali_vpu_mmu_map_pages() maps
p->pages->capacity contiguous VPU pages starting at begin without taking an
upper bound. When earlier mappings advance begin close to end (or a large
DMA-BUF is imported) so that the plane size exceeds end - begin, the
mapping spills past end into the adjacent firmware region or wraps around
the 32-bit VPU virtual address space.
[ ... ]
> +void mali_vpu_vb2_buffer_cleanup(struct vb2_buffer *vb2_buf)
> +{
> + struct vb2_v4l2_buffer *v4l2_buf = to_vb2_v4l2_buffer(vb2_buf);
> + struct mali_vpu_buffer *vpu_buf = to_mali_vpu_buffer(v4l2_buf);
> + u32 i;
> +
> + for (i = 0; i < vpu_buf->nplanes; i++) {
> + if (!vpu_buf->planes[i].pages)
> + continue;
> +
> + mali_vpu_mmu_free_pages(vpu_buf->planes[i].pages);
> + vpu_buf->planes[i].pages = NULL;
> + }
> +}
[Severity: High]
Does unmapping buffer pages in mali_vpu_vb2_buffer_cleanup() without
calling mali_vpu_sched_flush_mmu() leave stale TLB entries in the VPU MMU?
mali_vpu_mmu_free_pages() -> mali_vpu_mmu_unmap_pages() clears the L2 PTEs
in sess->mmu, and when a new DMA-BUF is queued on the same VB2 slot (or
CAPTURE buffers are reallocated while the OUTPUT queue keeps the session
LSID active), mali_vpu_buffer_plane_map() immediately reuses the lowest
unmapped virtual addresses from the start of the region. Without an LSID
MMU flush, the hardware can continue using cached translations pointing to
the previous buffer's freed physical pages.
> diff --git a/drivers/media/platform/arm/mali-vpu/mali-vpu-session.c b/drivers/media/platform/arm/mali-vpu/mali-vpu-session.c
> index 6d22a5d479008..540104bcd38e2 100644
> --- a/drivers/media/platform/arm/mali-vpu/mali-vpu-session.c
> +++ b/drivers/media/platform/arm/mali-vpu/mali-vpu-session.c
> @@ -150,6 +150,122 @@ int mali_vpu_session_fw_send_set_option(struct mali_vpu_session *sess,
[ ... ]
> +static int mali_vpu_queue_buffer(struct mali_vpu_session *sess, struct mali_vpu_buffer *vpu_buf)
> +{
> + int ret;
> + enum dma_data_direction dma_dir;
> +
> + dma_dir = (vpu_buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) ?
> + DMA_TO_DEVICE : DMA_FROM_DEVICE;
> +
> + /* Map MMU buffer pages if it is not already mapped */
> + if (!mali_vpu_is_buffer_mapped(vpu_buf)) {
> + ret = mali_vpu_buffer_map_fw_region(sess, vpu_buf);
> + if (ret)
> + return ret;
> + }
> +
> + if (!mali_vpu_is_coded_q(sess, vpu_buf->type) && sess->role == MALI_VPU_DECODER) {
> + ret = mali_vpu_buffer_set_frame_layout(vpu_buf);
> + if (ret)
> + return ret;
> + }
> +
> + /* dma sync for cache coherency */
> + ret = mali_vpu_buffer_sync(vpu_buf, dma_dir);
> + if (ret)
> + return ret;
> +
> + return 0;
> +}
[Severity: High]
Does calling mali_vpu_buffer_sync(vpu_buf, DMA_FROM_DEVICE) on a CAPTURE
buffer before VPU DMA runs sync in the wrong direction (for the CPU instead
of the device)?
drivers/media/platform/arm/mali-vpu/mali-vpu-mmu.c:mali_vpu_mmu_sync_pages() {
if (dir == DMA_FROM_DEVICE) {
for (i = 0; i < pages->count; i += MALI_VPU_PAGES_PER_LINUX_PAGE)
dma_sync_single_for_cpu(pages->dev, pages->pages[i].dma,
PAGE_SIZE, DMA_FROM_DEVICE);
}
When queuing a buffer to hardware before DMA starts, pages need to be
synced for the device (which vb2_dma_sg_prepare() already does during buffer
prepare), whereas dma_sync_single_for_cpu() transfers ownership back to the
CPU before the VPU writes the decoded frame.
> +
> +static int mali_vpu_submit_deferred_bufs_for_queue(struct mali_vpu_session *sess,
> + enum v4l2_buf_type type)
> +{
> + struct v4l2_m2m_buffer *buffer;
> + struct mali_vpu_buffer *vpu_buf;
> + int ret;
> +
> + if (!mali_vpu_session_get_port(sess, type)->stream_requested ||
> + !mali_vpu_session_can_stream(sess) ||
> + sess->fw_state != MALI_VPU_FW_STATE_RUNNING)
> + return 0;
> +
> + if (V4L2_TYPE_IS_OUTPUT(type)) {
> + v4l2_m2m_for_each_src_buf(sess->m2m_ctx, buffer) {
> + vpu_buf = to_mali_vpu_buffer(&buffer->vb);
> + if (vpu_buf->attr != MALI_VPU_BUF_ATTR_DEFERRED)
> + continue;
> +
> + ret = mali_vpu_queue_buffer(sess, vpu_buf);
> + if (ret)
> + return ret;
> +
> + vpu_buf->attr = MALI_VPU_BUF_ATTR_QUEUED;
> + }
> + } else if (V4L2_TYPE_IS_CAPTURE(type)) {
> + v4l2_m2m_for_each_dst_buf(sess->m2m_ctx, buffer) {
> + vpu_buf = to_mali_vpu_buffer(&buffer->vb);
> + if (vpu_buf->attr != MALI_VPU_BUF_ATTR_DEFERRED)
> + continue;
> +
> + ret = mali_vpu_queue_buffer(sess, vpu_buf);
> + if (ret)
> + return ret;
> +
> + vpu_buf->attr = MALI_VPU_BUF_ATTR_QUEUED;
> + }
> + } else {
> + return -EINVAL;
> + }
> +
> + return 0;
> +}
> +
> +static int mali_vpu_submit_deferred_bufs(struct mali_vpu_session *sess)
> +{
> + int ret;
> +
> + ret = mali_vpu_submit_deferred_bufs_for_queue(sess, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
> + if (ret)
> + goto err_submit;
> +
> + ret = mali_vpu_submit_deferred_bufs_for_queue(sess,
> + V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
> + if (ret)
> + goto err_submit;
> +
> + mali_vpu_sched_send_irq(&sess->sched_sess, &sess->vpu_dev->sched);
> +
> + return 0;
> +
> +err_submit:
> + dev_err(sess->vpu_dev->dev, "failed to queue deferred buffers\n");
> + return ret;
> +}
[Severity: Medium]
If mali_vpu_queue_buffer() fails for a deferred buffer here, doesn't this
leave both the failed buffer and the remaining buffers in the batch
stranded?
Unlike mali_vpu_vb2_buffer_queue(), which removes a failed buffer from
sess->m2m_ctx and completes it with VB2_BUF_STATE_ERROR, an error in
mali_vpu_submit_deferred_bufs_for_queue() returns immediately while the
buffer is still on the m2m ready queue in VB2_BUF_STATE_ACTIVE. Bailing
out to err_submit also skips remaining deferred buffers and skips
mali_vpu_sched_send_irq() for any earlier buffers in the loop that were
already marked MALI_VPU_BUF_ATTR_QUEUED.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261119153758.1426673-1-charvi.mehta@arm.com?part=20
next prev parent reply other threads:[~2026-11-19 16:06 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 15:37 [RFC PATCH 00/28] media: add arm Mali-V76 video decoder driver Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 01/28] dt-bindings: media: add Arm Mali-V76 VPU Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 02/28] media: mali-vpu: add Mali VPU platform driver Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 03/28] media: mali-vpu: register V4L2 decoder device and add querycap ioctl Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 04/28] media: mali-vpu: initialize decoder format state Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 05/28] media: mali-vpu: add V4L2 mem2mem and VB2 queue support Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 06/28] media: mali-vpu: implement format query ioctls Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 07/28] media: mali-vpu: implement TRY_FMT and S_FMT ioctls Charvi Mehta
2026-11-19 15:55 ` sashiko-bot
2026-11-19 15:37 ` [RFC PATCH 08/28] media: mali-vpu: register V76 decoder controls and introduce UAPI controls Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 09/28] media: mali-vpu: add MMU software page table manager Charvi Mehta
2026-11-19 15:59 ` sashiko-bot
2026-11-19 15:37 ` [RFC PATCH 10/28] media: mali-vpu: add firmware cache infrastructure Charvi Mehta
2026-11-19 15:50 ` sashiko-bot
2026-11-19 15:37 ` [RFC PATCH 11/28] media: mali-vpu: load firmware for decoder sessions Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 12/28] media: mali-vpu: prepare firmware address space Charvi Mehta
2026-11-19 15:57 ` sashiko-bot
2026-11-19 15:37 ` [RFC PATCH 13/28] media: mali-vpu: add firmware message transport Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 14/28] media: mali-vpu: route decoder controls to firmware Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 15/28] media: mali-vpu: handle firmware RPC requests Charvi Mehta
2026-11-19 15:51 ` sashiko-bot
2026-11-19 15:37 ` [RFC PATCH 16/28] media: mali-vpu: add LSID hardware support Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 17/28] media: mali-vpu: add single session scheduler Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 18/28] media: mali-vpu: handle firmware interrupts Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 19/28] media: mali-vpu: start decoder firmware through LSID scheduling Charvi Mehta
2026-11-19 16:03 ` sashiko-bot
2026-11-19 15:37 ` [RFC PATCH 20/28] media: mali-vpu: implement vb2 buffer queue handling Charvi Mehta
2026-11-19 16:06 ` sashiko-bot [this message]
2026-11-19 15:37 ` [RFC PATCH 21/28] media: mali-vpu: handle firmware buffer exchange Charvi Mehta
2026-11-19 15:58 ` sashiko-bot
2026-11-19 15:37 ` [RFC PATCH 22/28] media: mali-vpu: handle fatal firmware error Charvi Mehta
2026-11-19 16:07 ` sashiko-bot
2026-11-19 15:37 ` [RFC PATCH 23/28] media: mali-vpu: propagate buffer metadata Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 24/28] media: mali-vpu: add decoder EOS and DRC handling Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 25/28] media: mali-vpu: handle firmware buffer parameters Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 26/28] media: mali-vpu: handle firmware event responses Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 27/28] media: mali-vpu: add stateful decoder state machine Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 28/28] media: MAINTAINERS: add Arm Mali-V76 VPU driver Charvi Mehta
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=sashiko-outbox-223466@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=charvi.mehta@arm.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=media-ci@linuxtv.org \
--cc=robh@kernel.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