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 22/28] media: mali-vpu: handle fatal firmware error
Date: Thu, 19 Nov 2026 16:07:30 +0000	[thread overview]
Message-ID: <sashiko-outbox-223471@kernel.org> (raw)
In-Reply-To: <20261119153758.1426673-23-charvi.mehta@arm.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] media: mali-vpu: use-after-free in mali_vpu_fw_clear_buffers()
  on error
- [High] media: mali-vpu: re-entrancy race in mali_vpu_session_fw_init()
- [Medium] media: mali-vpu: unrejected buffer ops on failed session
--

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

media: mali-vpu: handle fatal firmware error

This commit records the first fatal error on a session, terminates the
scheduled session, marks the VB2 queues in error, and rejects subsequent
session operations with the recorded error code.

> 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 60e4b7e25eff8..9cbd116124109 100644
> --- a/drivers/media/platform/arm/mali-vpu/mali-vpu-session.c
> +++ b/drivers/media/platform/arm/mali-vpu/mali-vpu-session.c
[ ... ]
> @@ -307,6 +310,31 @@ static void mali_vpu_session_buffer_done(struct mali_vpu_session *sess,
>  	v4l2_m2m_buf_done(vbuf, state);
>  }
>  
> +static void mali_vpu_session_set_error(struct mali_vpu_session *sess, int error)
> +{
> +	struct vb2_queue *src_q;
> +	struct vb2_queue *dst_q;
> +
> +	lockdep_assert_held(&sess->vq_lock);
> +
> +	if (sess->error)
> +		return;
> +
> +	sess->error = error;
> +	sess->fw_requested_state = MALI_VPU_FW_STATE_STOPPED;
> +	sess->fw_state = MALI_VPU_FW_STATE_STOPPED;
> +
> +	mali_vpu_sched_session_terminate(&sess->sched_sess, &sess->vpu_dev->sched);
> +
> +	src_q = v4l2_m2m_get_src_vq(sess->m2m_ctx);
> +	dst_q = v4l2_m2m_get_dst_vq(sess->m2m_ctx);
> +
> +	vb2_queue_error(src_q);
> +	vb2_queue_error(dst_q);
> +
> +	wake_up(&sess->fw_waitq);
> +}

[Severity: High]
Can terminating a session with in-flight buffers cause a use-after-free in
mali_vpu_fw_clear_buffers() when the session is later closed?

Buffers submitted to firmware are recorded in sess->fw.inflight_buffers and
are normally removed when firmware returns them, or during final session
teardown in mali_vpu_fw_cleanup() -> mali_vpu_fw_clear_buffers().  When a
fatal error occurs with buffers in flight, mali_vpu_session_set_error()
terminates the LSID and prevents further interrupt handling, so firmware
never returns those buffers.

If userspace then calls VIDIOC_STREAMOFF followed by VIDIOC_REQBUFS(0) or
VIDIOC_REMOVE_BUFS before closing the fd, mali_vpu_vb2_stop_streaming()
completes all active m2m buffers back to VB2 and __vb2_queue_free() frees
the struct mali_vpu_buffer allocations without removing them from
sess->fw.inflight_buffers.  Closing the file descriptor then dereferences
the freed buffers:

drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c:mali_vpu_fw_clear_buffers() {
	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;
	}
}

Untracking any in-flight buffer in mali_vpu_vb2_buffer_cleanup() (or
clearing sess->fw.inflight_buffers when terminating the session on error)
would avoid leaving dangling pointers in the XArray.

> @@ -385,11 +413,13 @@ static int mali_vpu_session_wait_pending(struct mali_vpu_session *sess)
>  
>  	lockdep_assert_held(&sess->vq_lock);
>  
> -	while (sess->fw_bin_state == MALI_VPU_SESSION_FW_BIN_READY &&
> +	while (!sess->error &&
> +	       sess->fw_bin_state == MALI_VPU_SESSION_FW_BIN_READY &&
>  	       sess->fw.pending_response_count) {
>  		mutex_unlock(&sess->vq_lock);
>  
>  		timeout = wait_event_timeout(sess->fw_waitq,
> +					     READ_ONCE(sess->error) ||
>  					     READ_ONCE(sess->fw_bin_state) !=
>  					     MALI_VPU_SESSION_FW_BIN_READY ||
>  					     !READ_ONCE(sess->fw.pending_response_count),

[Severity: High]
Does dropping sess->vq_lock in mali_vpu_session_wait_pending() allow
concurrent re-entry into mali_vpu_session_fw_init()?

Both mali_vpu_session_prepare_streaming() and mali_vpu_session_fw_init()
detect an in-progress firmware startup by checking whether
sess->fw_requested_state != sess->fw_state.  However,
mali_vpu_session_fw_init() only updates sess->fw_requested_state to
MALI_VPU_FW_STATE_RUNNING in mali_vpu_session_fw_send_state() after
mali_vpu_apply_staged_controls() and mali_vpu_session_wait_pending()
complete.

When staged controls are present, mali_vpu_session_wait_pending() drops
sess->vq_lock while both sess->fw_state and sess->fw_requested_state are
still MALI_VPU_FW_STATE_STOPPED and sess->fw_bin_state is
MALI_VPU_SESSION_FW_BIN_READY:

CPU0                                  CPU1
mali_vpu_session_fw_bin_ready()
  sess->fw_bin_state = ..._READY
  mali_vpu_session_fw_init()
    mali_vpu_session_fw_send_job()
    mali_vpu_apply_staged_controls()
    mali_vpu_session_wait_pending()
      mutex_unlock(&sess->vq_lock)
                                      VIDIOC_STREAMON(CAPTURE)
                                      mutex_lock(&sess->vq_lock)
                                      mali_vpu_session_prepare_streaming()
                                        mali_vpu_session_fw_init()
                                          mali_vpu_session_fw_send_job()
                                          mali_vpu_session_wait_pending()
                                            mutex_unlock(&sess->vq_lock)
      /* wait fails or times out */
      mutex_lock(&sess->vq_lock)
  mali_vpu_fw_cleanup(&sess->fw)
  mutex_unlock(&sess->vq_lock)
                                            mutex_lock(&sess->vq_lock)
                                          mali_vpu_session_fw_send_state()
                                            <-- use-after-cleanup of sess->fw

In addition, because sess->vq_lock is the VB2 queue lock, dropping it
inside mali_vpu_vb2_prepare_streaming() before vb2_core_streamon() sets
q->streaming = 1 allows a second VIDIOC_STREAMON on the same queue to see
port->stream_requested == true, return 0 from
mali_vpu_session_prepare_streaming(), and run vb2_start_streaming() twice
on the same queue.

[ ... ]

> @@ -566,6 +600,9 @@ int mali_vpu_session_qbuf(struct mali_vpu_session *sess, struct vb2_buffer *vb2_
>  			mali_vpu_session_get_port(sess, vb2_buf->type);
>  	int ret;
>  
> +	if (sess->error)
> +		return sess->error;
> +
>  	if (!port->stream_requested ||
>  	    !mali_vpu_session_can_stream(sess) ||
>  	    sess->fw_state != MALI_VPU_FW_STATE_RUNNING) {

[Severity: Medium]
Does checking sess->error in mali_vpu_session_qbuf() reject buffer
operations with the recorded error as described in the commit message?

mali_vpu_session_qbuf() is only called from mali_vpu_vb2_buffer_queue(),
which has a void return type in struct vb2_ops and is only invoked by VB2
while the queue is already streaming and q->error is 0.  If VIDIOC_STREAMOFF
is called after a session error, __vb2_queue_cancel() clears q->error and
q->start_streaming_called; subsequent VIDIOC_QBUF and VIDIOC_PREPARE_BUF
calls only invoke mali_vpu_vb2_buffer_prepare(), which does not check
sess->error and therefore succeeds with 0.  Checking sess->error in
mali_vpu_vb2_buffer_prepare() would reject those buffer operations with the
recorded error.

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

  reply	other threads:[~2026-11-19 16:07 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
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 [this message]
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-223471@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