Skip to contentSkip to conversation

media: mediatek: vcodec: Validate source buffer vaddr in buf_prepare

PATCH Superseded Fei Shao 2 participants 2 messages Nov 27

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Fei Shao

author
#0 lore raw

PATCH v3 1/1 media: mediatek: vcodec: Validate source buffer vaddr in buf_prepare

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")

---
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/[email protected]/

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/[email protected]/

---
 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(-)
drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c +7 -1
index d76e891f784b..a5c6b56be927 100644
@@ -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;
+			}
+		} else {
 			vb2_set_plane_payload(vb, i, q_data->sizeimage[i]);
+		}
 	}
 
 	return 0;
drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateful.c +0 -6
index aa9bdee7a96c..fbd5791e78c1 100644
@@ -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;
drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateless.c +0 -6
index ab1894fba0d9..3d5f9e11a49b 100644
@@ -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

#1 lore raw

Le vendredi 27 novembre 2026 à 13:37 +0800, Fei Shao a écrit :

Quoted 39 lines

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")

---
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/[email protected]/

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/[email protected]/

---
 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(-)
drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c +0 -0
index d76e891f784b..a5c6b56be927 100644

drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c int vb2ops_vdec_buf_prepare(struct vb2_buffer *vb) · line 817

Quoted 8 lines
drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c +4 -1
@@ -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

drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c int vb2ops_vdec_buf_prepare(struct vb2_buffer *vb) · line 821

Quoted 4 lines
drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c +3 -0
+			}
+		} 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

Quoted 41 lines
drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c +0 -0
 	}
 
 	return 0;
drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateful.c +0 -6
index aa9bdee7a96c..fbd5791e78c1 100644
@@ -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;
drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateless.c +0 -7
index ab1894fba0d9..3d5f9e11a49b 100644
@@ -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