Devicetree
 help / color / mirror / Atom feed
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 21/28] media: mali-vpu: handle firmware buffer exchange
Date: Thu, 19 Nov 2026 15:58:14 +0000	[thread overview]
Message-ID: <sashiko-outbox-223450@kernel.org> (raw)
In-Reply-To: <20261119153758.1426673-22-charvi.mehta@arm.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] media: mali-vpu: in-flight buffers not untracked on streamoff
  or cleanup
- [Medium] media: mali-vpu: bogus error and early loop exit in
  mali_vpu_fw_get_buffer()
- [Medium] media: mali-vpu: spurious V4L2_BUF_FLAG_LAST on rejected
  capture buffers
--

commit 470c746dd3c53a73aac1ec2091f9e9c5b26e72a8
Author: Rath Sankaralingam <rath.sankaralingam@arm.com>

media: mali-vpu: handle firmware buffer exchange

This commit implements buffer submission to the firmware and handling of
returned input and output buffer responses using the v3 buffer protocol,
tracking in-flight buffers in an xarray by non-zero buffer IDs.

> 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 c1df3e2d4eacf..e404fb933195f 100644
> --- a/drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c
> +++ b/drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c
[ ... ]
> @@ -102,6 +137,27 @@ static void mali_vpu_buffer_update_flags_from_v4l2(struct vb2_buffer *vb2_buf)
>  		vpu_buf->flags |= MALI_VPU_BUFFER_EOS;
>  }
>  
> +static void mali_vpu_buffer_update_flags_to_v4l2(struct mali_vpu_buffer *vpu_buf)
> +{
> +	struct vb2_v4l2_buffer *v4l2_buf = &vpu_buf->m2m_buf.vb;
> +
> +	v4l2_buf->flags &= ~(V4L2_BUF_FLAG_ERROR |
> +			     V4L2_BUF_FLAG_KEYFRAME |
> +			     V4L2_BUF_FLAG_LAST);
> +
> +	if (vpu_buf->flags & MALI_VPU_BUFFER_EOS ||
> +	    (V4L2_TYPE_IS_CAPTURE(vpu_buf->type) &&
> +	     mali_vpu_buffer_get_bytesused(&vpu_buf->m2m_buf.vb.vb2_buf) == 0))
> +		v4l2_buf->flags |= V4L2_BUF_FLAG_LAST;
> +
> +	if (vpu_buf->flags & MALI_VPU_BUFFER_CORRUPT)
> +		v4l2_buf->flags |= V4L2_BUF_FLAG_ERROR;
> +
> +	if (V4L2_TYPE_IS_CAPTURE(vpu_buf->type) &&
> +	    vpu_buf->flags & MALI_VPU_BUFFER_REJECTED)
> +		mali_vpu_buffer_clear_bytesused(vpu_buf);
> +}

[Severity: Medium]
Can a rejected capture buffer be returned to userspace with
V4L2_BUF_FLAG_LAST spuriously set?

When firmware rejects a capture frame buffer with
MVE_BUFFER_FRAME_FLAG_REJECTED and zero visible dimensions,
mali_vpu_convert_buffer_frame() zeroes plane payload_size and offset, and
mali_vpu_buffer_update_bytesused_to_v4l2() propagates bytesused = 0 to
vb2_buf before mali_vpu_buffer_update_flags_to_v4l2() runs.  As a result,
mali_vpu_buffer_get_bytesused() is already 0 here and sets
V4L2_BUF_FLAG_LAST even when MALI_VPU_BUFFER_EOS is not set.

> diff --git a/drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c b/drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c
> index 820853ca4519a..4e3eaecb8ebee 100644
> --- a/drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c
> +++ b/drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c
[ ... ]
> @@ -347,6 +348,20 @@ static int mali_vpu_fw_map_core_regions(struct mali_vpu_fw *fw,
>  	return ret;
>  }
>  
> +static void mali_vpu_fw_clear_buffers(struct mali_vpu_fw *fw)
> +{
> +	struct mali_vpu_buffer *vpu_buf;
> +	unsigned long id;
> +
> +	xa_for_each(&fw->inflight_buffers, id, vpu_buf) {
> +		xa_erase(&fw->inflight_buffers, id);
> +		vpu_buf->fw_buffer_id = 0;
> +		vpu_buf->attr = MALI_VPU_BUF_ATTR_NONE;
> +	}
> +
> +	xa_destroy(&fw->inflight_buffers);
> +}

[Severity: High]
Can this cause a use-after-free on vpu_buf if buffers are freed after
VIDIOC_STREAMOFF?

mali_vpu_vb2_stop_streaming() completes queued buffers back to vb2 without
removing in-flight buffers from fw->inflight_buffers or clearing
vpu_buf->fw_buffer_id, and mali_vpu_vb2_buffer_cleanup() does not untrack
them either.  If userspace frees the buffers after streamoff (e.g. via
VIDIOC_REQBUFS(0)), dangling vpu_buf pointers remain in
fw->inflight_buffers and are written to here during session close (or in
mali_vpu_fw_resolve_returned_buffer() if firmware returns the buffer).

If userspace instead restarts streaming and re-queues the same buffers
after streamoff, vpu_buf->fw_buffer_id is still non-zero, so
mali_vpu_fw_track_buffer() fails with -EBUSY.  Also, when firmware returns
a buffer that was already completed by stop_streaming,
mali_vpu_session_handle_fw_msg() calls
mali_vpu_buffer_update_info_to_v4l2() and mutates vb2_buf metadata before
mali_vpu_session_buffer_done() checks whether vb2_buf->state is still
VB2_BUF_STATE_ACTIVE.

[ ... ]

> +static int mali_vpu_fw_get_buffer(struct mali_vpu_fw *fw,
> +				  struct mali_vpu_fw_proto_page *buf_host_page,
> +				  struct mali_vpu_fw_proto_page *buf_mve_page,
> +				  struct mali_vpu_fw_msg *msg, u32 buf_type)
> +{
> +	u16 code;
> +	union {
> +		struct mve_buffer_frame frame;
> +		struct mve_buffer_bitstream bitstream;
> +	} fw_msg;
> +	size_t size = sizeof(fw_msg);
> +	int ret;
> +
> +	ret = mali_vpu_fw_read_msg(fw, buf_host_page, buf_mve_page, &code,
> +				   &fw_msg, &size);
> +	if (ret <= 0)
> +		return ret;
> +
> +	switch (code) {
> +	case MVE_BUFFER_CODE_FRAME:
> +		if (size != sizeof(fw_msg.frame) ||
> +		    (!fw_msg.frame.host_handle &&
> +		     !(fw_msg.frame.frame_flags & MVE_BUFFER_FRAME_FLAG_EOS)))
> +			return -EPROTO;
> +
> +		ret = mali_vpu_convert_buffer_frame(fw, msg, buf_type, &fw_msg.frame);
> +		if (ret)
> +			return ret;
> +		break;
> +	case MVE_BUFFER_CODE_BITSTREAM:
> +		if (size != sizeof(fw_msg.bitstream) ||
> +		    (!fw_msg.bitstream.host_handle &&
> +		     !(fw_msg.bitstream.bitstream_flags & MVE_BUFFER_BITSTREAM_FLAG_EOS)))
> +			return -EPROTO;
> +
> +		ret = mali_vpu_convert_buffer_bitstream(fw, msg, buf_type,
> +							&fw_msg.bitstream);
> +		if (ret)
> +			return ret;
> +		break;
> +	default:
> +		dev_dbg(fw->dev, "unknown buf code. code: %u.\n", code);
> +		break;
> +	}
> +
> +	return 1;
> +}

[Severity: Medium]
Does an unknown buffer code in mali_vpu_fw_get_buffer() leave msg->code at
0 (MALI_VPU_FW_CODE_ERROR) while returning 1?

mali_vpu_fw_parse_msg() zeroes *msg before calling mali_vpu_fw_get_buffer(),
so hitting the default branch returns 1 with msg->code ==
MALI_VPU_FW_CODE_ERROR, causing mali_vpu_session_handle_fw_msg() to treat
it as a fatal firmware error and fail with -EIO.  In addition, if
mali_vpu_fw_read_msg() returns 0 because the buffer queue is unexpectedly
empty after MVE_RESPONSE_CODE_INPUT or MVE_RESPONSE_CODE_OUTPUT was read
from msg_mve, propagating 0 causes mali_vpu_session_irq() to treat msg_mve
as empty and stop draining remaining messages instead of flagging -EPROTO.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119153758.1426673-1-charvi.mehta@arm.com?part=21

  reply	other threads:[~2026-11-19 15:58 UTC|newest]

Thread overview: 37+ 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
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 [this message]
2026-11-19 15:37 ` [RFC PATCH 22/28] media: mali-vpu: handle fatal firmware error Charvi Mehta
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-223450@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