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 19/28] media: mali-vpu: start decoder firmware through LSID scheduling
Date: Thu, 19 Nov 2026 16:03:15 +0000	[thread overview]
Message-ID: <sashiko-outbox-223458@kernel.org> (raw)
In-Reply-To: <20261119153758.1426673-20-charvi.mehta@arm.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] media: mali-vpu: vq_lock drop in
  mali_vpu_session_wait_pending() races
- [Medium] media: mali-vpu: 10s hang in mali_vpu_session_cleanup() on
  unmapped LSID
--

commit df41ad5c55cc78aaaf50be9e6b1f9cc272e547c5
Author: Charvi Mehta <charvi.mehta@arm.com>

media: mali-vpu: start decoder firmware through LSID scheduling

This commit connects decoder firmware execution to VB2 queue preparation and
asynchronous firmware loading, activating the LSID and tracking requested and
confirmed firmware states across stream startup and shutdown.

> 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 09ec6d14f4ad0..6d22a5d479008 100644
> --- a/drivers/media/platform/arm/mali-vpu/mali-vpu-session.c
> +++ b/drivers/media/platform/arm/mali-vpu/mali-vpu-session.c

[ ... ]

> @@ -123,6 +211,102 @@ static int mali_vpu_session_handle_fw_msg(struct mali_vpu_session *sess,
>  	}
>  }
>  
> +static int mali_vpu_session_wait_pending(struct mali_vpu_session *sess)
> +{
> +	long timeout;
> +
> +	lockdep_assert_held(&sess->vq_lock);
> +
> +	while (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->fw_bin_state) !=
> +					     MALI_VPU_SESSION_FW_BIN_READY ||
> +					     !READ_ONCE(sess->fw.pending_response_count),
> +					     msecs_to_jiffies(mali_vpu_fw_response_timeout_ms));
> +
> +		mutex_lock(&sess->vq_lock);
> +
> +		if (!timeout && sess->fw.pending_response_count)
> +			return -ETIMEDOUT;
> +	}
> +
> +	return 0;
> +}
> +
> +static int mali_vpu_session_fw_init(struct mali_vpu_session *sess)
> +{
> +	int ret;
> +
> +	lockdep_assert_held(&sess->vq_lock);
> +
> +	if (sess->fw_requested_state != sess->fw_state) {
> +		if (sess->fw_requested_state != MALI_VPU_FW_STATE_RUNNING)
> +			return -EBUSY;
> +
> +		return mali_vpu_session_sched_activate(sess);
> +	}
> +
> +	if (sess->fw_state == MALI_VPU_FW_STATE_RUNNING)
> +		return 0;
> +
> +	ret = mali_vpu_session_fw_send_job(sess, 0);
> +	if (ret)
> +		return ret;
> +
> +	/*
> +	 * Map the LSID and allow the stopped firmware to process the
> +	 * queued SET_OPTION requests before it can be RUNNING.
> +	 */
> +	ret = mali_vpu_session_sched_activate(sess);
> +	if (ret)
> +		return ret;
> +
> +	ret = mali_vpu_session_fw_send_ping(sess);
> +	if (ret)
> +		return ret;
> +
> +	ret = mali_vpu_apply_staged_controls(sess);
> +	if (ret)
> +		return ret;
> +
> +	/*
> +	 * SET_OPTION is only valid while stopped. Wait for pending option
> +	 * responses before requesting RUNNING.
> +	 */
> +	ret = mali_vpu_session_wait_pending(sess);
> +	if (ret)
> +		return ret;
> +
> +	/* Waiting drops vq_lock, so streaming may have been withdrawn. */
> +	if (!mali_vpu_session_can_stream(sess))
> +		return 0;
> +
> +	return mali_vpu_session_fw_send_state(sess, MALI_VPU_FW_STATE_RUNNING);
> +}

[Severity: High]
Can dropping sess->vq_lock in mali_vpu_session_wait_pending() race with
concurrent queue ioctls during vb2_core_streamon()?

sess->vq_lock is also assigned to vq->lock and m2m_ctx->q_lock, which
videobuf2 expects to stay held across prepare_streaming() and
start_streaming() until q->streaming is set.  When staged controls are
applied, mali_vpu_session_prepare_streaming() sets port->stream_requested =
true and then drops sess->vq_lock inside mali_vpu_session_wait_pending()
while q->streaming is still 0:

CPU0                                  CPU1
vb2_core_streamon(q)
  mali_vpu_vb2_prepare_streaming(q)
    port->stream_requested = true
    mali_vpu_session_fw_init()
      mali_vpu_apply_staged_controls()
      mali_vpu_session_wait_pending()
        mutex_unlock(&sess->vq_lock)
                                      vb2_core_streamon(q)
                                        /* q->streaming is still 0 */
                                        mali_vpu_vb2_prepare_streaming(q)
                                          if (port->stream_requested)
                                            return 0;
                                        vb2_start_streaming(q)
        mutex_lock(&sess->vq_lock)
  vb2_start_streaming(q)  <-- corrupts q_ctx->rdy_queue list

Because both threads run vb2_start_streaming(q), the queued buffers are
passed to mali_vpu_vb2_buffer_queue() -> v4l2_m2m_buf_queue() twice,
corrupting q_ctx->rdy_queue via double list_add_tail() and double
incrementing q->owned_by_drv_count.

Dropping sess->vq_lock here exposes three other races while waiting:
- sess->fw_requested_state is not set to MALI_VPU_FW_STATE_RUNNING until
  mali_vpu_session_fw_send_state() after mali_vpu_session_wait_pending()
  returns, so a concurrent streamon on the other port (or against the
  asynchronous mali_vpu_session_fw_bin_ready() callback) sees
  fw_requested_state == fw_state == MALI_VPU_FW_STATE_STOPPED and re-enters
  mali_vpu_session_fw_init(), sending duplicate job and ping requests.
- If another thread calls VIDIOC_STREAMOFF while sess->vq_lock is dropped,
  !mali_vpu_session_can_stream(sess) returns 0 to vb2_core_streamon(),
  which still sets q->streaming = 1 even though port->stream_requested was
  cleared and the firmware remains stopped.
- A concurrent VIDIOC_REQBUFS(0) can enter __vb2_queue_cancel() and free
  buffers while vb2_core_streamon() is still in flight.

[ ... ]

> @@ -243,6 +433,21 @@ int mali_vpu_session_cleanup(struct mali_vpu_session *sess)
>  			sess->fw_bin_state = MALI_VPU_SESSION_FW_BIN_FAILED;
>  	}
>  
> +	if (sess->fw_bin_state == MALI_VPU_SESSION_FW_BIN_READY)
> +		fw_stop_ret = mali_vpu_session_fw_stop(sess);

[Severity: Medium]
Can mali_vpu_session_cleanup() hang for 10 seconds in
mali_vpu_session_fw_stop() when the firmware binary is ready but no LSID is
mapped?

Unlike mali_vpu_session_streamoff(), which returns early when
!sess->sched_sess.lsid, mali_vpu_session_cleanup() calls
mali_vpu_session_fw_stop() -> mali_vpu_session_wait_pending() without
checking sess->sched_sess.lsid.  Meanwhile, mali_vpu_apply_or_stage_ctrl()
sends MVE_REQUEST_CODE_SET_OPTION immediately whenever fw_bin_state is
MALI_VPU_SESSION_FW_BIN_READY, even if sess->sched_sess.lsid is NULL:

drivers/media/platform/arm/mali-vpu/mali-vpu-controls.c:mali_vpu_apply_or_stage_ctrl() {
	...
	staged = sess->fw_bin_state != MALI_VPU_SESSION_FW_BIN_READY;

	if (!staged) {
		ret = desc->set(sess, state->cid, value);
		if (ret)
			return ret;
	}
	...
}

If fw_bin_state reaches MALI_VPU_SESSION_FW_BIN_READY without an active LSID
(for example, when CAPTURE is prepared while OUTPUT is stopped, or when
mali_vpu_sched_switch_in() fails with -EBUSY because another session holds
LSID 0) and userspace sets a control before closing the fd,
sess->fw.pending_response_count is incremented for an unscheduled firmware
instance and mali_vpu_session_cleanup() blocks until the 10-second
mali_vpu_fw_response_timeout_ms expires.

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

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