mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Nicolas Dufresne <nicolas@ndufresne.ca>
To: Fei Shao <fshao@chromium.org>,
	Mauro Carvalho Chehab <mchehab@kernel.org>,
	 AngeloGioacchino Del Regno
	<angelogioacchino.delregno@collabora.com>,
	Hans Verkuil <hverkuil@kernel.org>
Cc: Matthias Brugger <matthias.bgg@gmail.com>,
	Yunfei Dong <yunfei.dong@mediatek.com>,
	linux-media@vger.kernel.org,  linux-kernel@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	 linux-mediatek@lists.infradead.org
Subject: Re: [PATCH v3] media: mediatek: vcodec: Validate source buffer vaddr in buf_prepare
Date: Fri, 27 Nov 2026 13:55:14 -0500	[thread overview]
Message-ID: <b51b4c1ce171a77eb452455862d706680d211e71.camel@ndufresne.ca> (raw)
In-Reply-To: <20261127-vcodec-null-va-v3-1-e24521086062@chromium.org>

[-- Attachment #1: Type: text/plain, Size: 5494 bytes --]

Le vendredi 27 novembre 2026 à 13:37 +0800, Fei Shao a écrit :
> If an OUTPUT buffer has no kernel mapping, vb2_plane_vaddr() returns
> NULL. Both the stateful and stateless decoder workers only peek the
> source buffer and finish the m2m job without returning it, so the buffer
> stays at the head of the ready list and the same job is scheduled again.
> In the stateless decoder, the media request is also never completed; in
> the stateful decoder, vb2ops_vdec_stateful_buf_queue() also passes the
> source address to vdec_if_decode() without a NULL check.
> 
> Add a vb2_plane_vaddr() NULL check for OUTPUT buffers in the shared
> vb2ops_vdec_buf_prepare() instead, then remove the late checks from both
> workers. Unmappable buffers are rejected before entering the queue, and
> userspace gets immediate feedback.
> 
> Fixes: 590577a4e525 ("[media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver")
> Fixes: 7b182b8d9c85 ("media: mediatek: vcodec: Refactor get and put capture buffer flow")
> Signed-off-by: Fei Shao <fshao@chromium.org>
> ---
> Changes in v3:
> - Consolidate into a single patch.
>   Validate OUTPUT buffer in shared vb2ops_vdec_buf_prepare() and drop
>   the late checks from both workers per suggestion in v2.
> - Link to v2: https://lore.kernel.org/all/20261023075642.3671805-1-fshao@chromium.org/
> 
> Changes in v2:
> - Call media_request_manual_complete() instead of
>   mtk_vdec_stateless_cap_to_disp() to avoid dequeuing a capture buffer
>   that may be in use by the core workqueue on MediaTek LAT platforms.
> - Link to v1: https://lore.kernel.org/all/20261019060825.4163770-1-fshao@chromium.org/
> ---
>  drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c   | 8 +++++++-
>  .../platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateful.c    | 6 ------
>  .../platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateless.c   | 6 ------
>  3 files changed, 7 insertions(+), 13 deletions(-)
> 
> diff --git a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c
> index d76e891f784b..a5c6b56be927 100644
> --- a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c
> +++ b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c
> @@ -811,8 +811,14 @@ int vb2ops_vdec_buf_prepare(struct vb2_buffer *vb)
>  					  i, vb2_plane_size(vb, i), q_data->sizeimage[i]);
>  			return -EINVAL;
>  		}
> -		if (!V4L2_TYPE_IS_OUTPUT(vb->type))
> +		if (V4L2_TYPE_IS_OUTPUT(vb->type)) {
> +			if (!vb2_plane_vaddr(vb, i)) {
> +				mtk_v4l2_vdec_err(ctx, "failed to get vaddr for plane %d", i);
> +				return -EFAULT;

The spec only allow two errors, so I think it should be EINVAL, what do you
think?

https://www.kernel.org/doc/html//latest/userspace-api/media/v4l/vidioc-prepare-buf.html#return-value


> +			}
> +		} else {
>  			vb2_set_plane_payload(vb, i, q_data->sizeimage[i]);
> +		}

I would myself do two if, to keep the two separate.


		if (V4L2_TYPE_IS_OUTPUT(vb->type) && !vb2_plane_vaddr(vb, i)) {
			mtk_v4l2_vdec_err(ctx, "failed to get vaddr for plane
%d", i);
			return -EINVAL;
		}

		if (V4L2_TYPE_IS_CAPTURE (vb->type)
 			vb2_set_plane_payload(vb, i, q_data->sizeimage[i]);

Nicolas

>  	}
>  
>  	return 0;
> diff --git a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateful.c b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateful.c
> index aa9bdee7a96c..fbd5791e78c1 100644
> --- a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateful.c
> +++ b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateful.c
> @@ -347,12 +347,6 @@ static void mtk_vdec_worker(struct work_struct *work)
>  	buf.va = vb2_plane_vaddr(&src_buf->vb2_buf, 0);
>  	buf.dma_addr = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
>  	buf.size = (size_t)src_buf->vb2_buf.planes[0].bytesused;
> -	if (!buf.va) {
> -		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> -		mtk_v4l2_vdec_err(ctx, "[%d] id=%d src_addr is NULL!!", ctx->id,
> -				  src_buf->vb2_buf.index);
> -		return;
> -	}
>  	mtk_v4l2_vdec_dbg(3, ctx, "[%d] Bitstream VA=%p DMA=%pad Size=%zx vb=%p",
>  			  ctx->id, buf.va, &buf.dma_addr, buf.size, src_buf);
>  	dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
> diff --git a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateless.c b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateless.c
> index ab1894fba0d9..3d5f9e11a49b 100644
> --- a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateless.c
> +++ b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateless.c
> @@ -342,12 +342,6 @@ static void mtk_vdec_worker(struct work_struct *work)
>  	bs_src->va = vb2_plane_vaddr(vb2_src, 0);
>  	bs_src->dma_addr = vb2_dma_contig_plane_dma_addr(vb2_src, 0);
>  	bs_src->size = (size_t)vb2_src->planes[0].bytesused;
> -	if (!bs_src->va) {
> -		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> -		mtk_v4l2_vdec_err(ctx, "[%d] id=%d source buffer is NULL", ctx->id,
> -				  vb2_src->index);
> -		return;
> -	}
>  
>  	mtk_v4l2_vdec_dbg(3, ctx, "[%d] Bitstream VA=%p DMA=%pad Size=%zx vb=%p",
>  			  ctx->id, bs_src->va, &bs_src->dma_addr, bs_src->size, vb2_src);
> 
> ---
> base-commit: a9c48428cbd8d96b26f92631d0db6f8519c5fcc5
> change-id: 20261127-vcodec-null-va-cd0008e9f136

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

      reply	other threads:[~2026-11-27 18:55 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  5:37 Fei Shao
2026-11-27 18:55 ` Nicolas Dufresne [this message]

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=b51b4c1ce171a77eb452455862d706680d211e71.camel@ndufresne.ca \
    --to=nicolas@ndufresne.ca \
    --cc=angelogioacchino.delregno@collabora.com \
    --cc=fshao@chromium.org \
    --cc=hverkuil@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=matthias.bgg@gmail.com \
    --cc=mchehab@kernel.org \
    --cc=yunfei.dong@mediatek.com \
    /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

all inboxes | Powered by JetHome®