Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Nas Chung" <nas.chung@chipsnmedia.com>
Cc: devicetree@vger.kernel.org, imx@lists.linux.dev, robh@kernel.org,
	conor+dt@kernel.org, Frank.Li@kernel.org, media-ci@linuxtv.org
Subject: Re: [PATCH v8 07/11] media: chips-media: wave6: Add Wave6 m2m encoder
Date: Thu, 19 Nov 2026 09:35:27 +0000	[thread overview]
Message-ID: <sashiko-outbox-222858@kernel.org> (raw)
In-Reply-To: <542892d0f75d82102f122dfd55c73847e6b8c6f1.1795075581.git.nas.chung@chipsnmedia.com>

Thank you for your contribution! Sashiko AI review found 12 potential
issues to consider:
- [High] media: wave6: buffer leak and m2m job hang in
  wave6_vpu_enc_finish_encode()
- [High] media: wave6: unvalidated CAPTURE size and stale sizeimage in
  dst_fmt
- [High] media: wave6: asymmetric instance lifecycle in start and
  stop_streaming()
- [High] media: wave6: missing busy check and zero crop return in
  s_selection()
- [High] media: wave6: unlocked m2m rdy_queue traversal in encoder
  buffer lookup
- [High] media: wave6: race between wave6_vpu_enc_finish_encode() and
  timeout work
- [Medium] media: wave6: zero CSC coefficients in wave6_set_csc() for
  XV601 and XV709
- [Medium] media: wave6: HEVC refresh controls overwritten in
  wave6_set_enc_hevc_param()
- [Medium] media: wave6: skipped CAPTURE validation in
  wave6_vpu_enc_buf_prepare()
- [Medium] media: wave6: zero-padded format table match in
  wave6_find_vpu_fmt()
- [Medium] media: wave6: wave6_find_vpu_fmt() matches zeroed format
  table entries
- [Low] media: wave6: unupdated timeperframe in wave6_vpu_enc_s_parm()
--

commit d914baad0e743b67d2e3cd7e1bd636ba7a5de85f
Author: Nas Chung <nas.chung@chipsnmedia.com>

media: chips-media: wave6: Add Wave6 m2m encoder

This commit adds the V4L2 m2m stateful encoder driver for the Chips&Media
Wave6 video codec IP, supporting H.264 and HEVC encoding.

> diff --git a/drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c b/drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c
> new file mode 100644
> index 0000000000000..7142666d4503d
> --- /dev/null
> +++ b/drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c
> @@ -0,0 +1,2732 @@

[ ... ]

> +static const struct vpu_format wave6_vpu_enc_fmt_list[2][23] = {
> +	[VPU_FMT_TYPE_CODEC] = {
> +		{
> +			.v4l2_pix_fmt = V4L2_PIX_FMT_HEVC,
> +			.max_width = W6_MAX_ENC_PIC_WIDTH,
> +			.min_width = W6_MIN_ENC_PIC_WIDTH,
> +			.max_height = W6_MAX_ENC_PIC_HEIGHT,
> +			.min_height = W6_MIN_ENC_PIC_HEIGHT,
> +			.num_planes = 1,
> +		},
> +		{
> +			.v4l2_pix_fmt = V4L2_PIX_FMT_H264,
> +			.max_width = W6_MAX_ENC_PIC_WIDTH,
> +			.min_width = W6_MIN_ENC_PIC_WIDTH,
> +			.max_height = W6_MAX_ENC_PIC_HEIGHT,
> +			.min_height = W6_MIN_ENC_PIC_HEIGHT,
> +			.num_planes = 1,
> +		},
> +	},

[ ... ]

> +static const struct vpu_format *wave6_find_vpu_fmt(unsigned int v4l2_pix_fmt,
> +						   enum vpu_fmt_type type)
> +{
> +	for (int i = 0; i < ARRAY_SIZE(wave6_vpu_enc_fmt_list[type]); i++) {
> +		if (wave6_vpu_enc_fmt_list[type][i].v4l2_pix_fmt == v4l2_pix_fmt)
> +			return &wave6_vpu_enc_fmt_list[type][i];
> +	}
> +
> +	return NULL;
> +}

[Severity: Medium]
Can wave6_find_vpu_fmt() match the unpopulated trailing entries in
wave6_vpu_enc_fmt_list[VPU_FMT_TYPE_CODEC] when v4l2_pix_fmt is 0?

Because wave6_vpu_enc_fmt_list is sized to 23 elements for both sub-arrays
while VPU_FMT_TYPE_CODEC only populates 2 entries, the remaining 21 entries
are zero-initialized. Unlike wave6_find_vpu_fmt_by_idx(), wave6_find_vpu_fmt()
does not reject a zero pixelformat, so passing pixelformat 0 to
VIDIOC_TRY_FMT on the capture queue matches a zeroed entry and sets
pix_mp->pixelformat and pix_mp->num_planes to 0 instead of falling back to
inst->dst_fmt.

[ ... ]

> +static struct vb2_v4l2_buffer *wave6_get_valid_src_buf(struct vpu_instance *inst)
> +{
> +	struct vb2_v4l2_buffer *vb2_v4l2_buf;
> +	struct v4l2_m2m_buffer *v4l2_m2m_buf;
> +	struct vpu_buffer *vpu_buf = NULL;
> +
> +	v4l2_m2m_for_each_src_buf(inst->v4l2_fh.m2m_ctx, v4l2_m2m_buf) {
> +		vb2_v4l2_buf = &v4l2_m2m_buf->vb;
> +		vpu_buf = wave6_to_vpu_buf(vb2_v4l2_buf);
> +
> +		if (!vpu_buf->consumed) {
> +			dev_dbg(inst->dev->dev, "no consumed src idx : %d\n",
> +				vb2_v4l2_buf->vb2_buf.index);
> +			return vb2_v4l2_buf;
> +		}
> +	}
> +
> +	return NULL;
> +}
> +
> +static struct vb2_v4l2_buffer *wave6_get_valid_dst_buf(struct vpu_instance *inst)
> +{
> +	struct vb2_v4l2_buffer *vb2_v4l2_buf;
> +	struct v4l2_m2m_buffer *v4l2_m2m_buf;
> +	struct vpu_buffer *vpu_buf;
> +
> +	v4l2_m2m_for_each_dst_buf(inst->v4l2_fh.m2m_ctx, v4l2_m2m_buf) {
> +		vb2_v4l2_buf = &v4l2_m2m_buf->vb;
> +		vpu_buf = wave6_to_vpu_buf(vb2_v4l2_buf);
> +
> +		if (!vpu_buf->consumed) {
> +			dev_dbg(inst->dev->dev, "no consumed dst idx : %d\n",
> +				vb2_v4l2_buf->vb2_buf.index);
> +			return vb2_v4l2_buf;
> +		}
> +	}
> +
> +	return NULL;
> +}

[Severity: High]
Does iterating the M2M ready queues in wave6_get_valid_src_buf(),
wave6_get_valid_dst_buf(), and wave6_get_dst_buf_by_addr() race with
v4l2_m2m_buf_queue()?

v4l2_m2m_for_each_src_buf() and v4l2_m2m_for_each_dst_buf() expand to plain
list_for_each_entry() over out_q_ctx.rdy_queue and cap_q_ctx.rdy_queue
without taking q_ctx->rdy_spinlock:

CPU0 (v4l2_m2m_device_run_work)   CPU1 (VIDIOC_QBUF)
wave6_vpu_device_run()            wave6_vpu_enc_buf_queue()
  wave6_vpu_enc_start_encode()      v4l2_m2m_buf_queue()
    wave6_get_valid_src_buf()         spin_lock(&q_ctx->rdy_spinlock)
      v4l2_m2m_for_each_src_buf()     list_add_tail(&b->list,
                                                    &q_ctx->rdy_queue)
        /* unlocked list traversal <-- data race */

When wave6_vpu_device_run() runs from the M2M job workqueue, neither
inst->dev->dev_lock nor rdy_spinlock is held. Likewise, when
wave6_handle_encoded_frame() calls wave6_get_dst_buf_by_addr() from the IRQ
thread, it holds only inst->lock while VIDIOC_QBUF mutates rdy_queue under
inst->dev->dev_lock and rdy_spinlock.

> +
> +static void wave6_set_csc(struct vpu_instance *inst, struct enc_param *pic_param)
> +{
> +	const struct vpu_format *vpu_fmt;
> +	bool is_10bit;
> +
> +	vpu_fmt = wave6_find_vpu_fmt(inst->src_fmt.pixelformat, VPU_FMT_TYPE_RAW);
> +	if (!vpu_fmt)
> +		return;
> +
> +	if (!(vpu_fmt->flags & VPU_FMT_FLAG_RGB))
> +		return;
> +
> +	is_10bit = !!(vpu_fmt->flags & VPU_FMT_FLAG_10BIT);
> +	pic_param->csc.fmt_order = vpu_fmt->csc_fmt_order;
> +
> +	if (inst->ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT ||
> +	    inst->ycbcr_enc == V4L2_YCBCR_ENC_601) {

[ ... ]

> +	} else if (inst->ycbcr_enc == V4L2_YCBCR_ENC_XV601) {
> +		if (inst->quantization == V4L2_QUANTIZATION_LIM_RANGE) {
> +			/*
> +			 * Y   0.2558(R)  0.5021(G)  0.0975(B)
> +			 * Cb -0.1476(R) -0.2899(G)  0.4375(B)
> +			 * Cr  0.4375(R) -0.3664(G) -0.0711(B)
> +			 */
> +			pic_param->csc.coef_ry = 0x083;
> +			pic_param->csc.coef_gy = 0x101;
> +			pic_param->csc.coef_by = 0x032;
> +			pic_param->csc.coef_rcb = 0xffffffb4;
> +			pic_param->csc.coef_gcb = 0xffffff6c;
> +			pic_param->csc.coef_bcb = 0x0e0;
> +			pic_param->csc.coef_rcr = 0x0e0;
> +			pic_param->csc.coef_gcr = 0xffffff44;
> +			pic_param->csc.coef_bcr = 0xffffffdc;
> +			pic_param->csc.offset_y = (is_10bit) ? 0x40 : 0x10;
> +			pic_param->csc.offset_cb = 0x0;
> +			pic_param->csc.offset_cr = 0x0;
> +		}
> +	} else if (inst->ycbcr_enc == V4L2_YCBCR_ENC_XV709) {
> +		if (inst->quantization == V4L2_QUANTIZATION_LIM_RANGE) {
> +			/*
> +			 * Y   0.1819(R)  0.6118(G)  0.0618(B)
> +			 * Cb -0.1003(R) -0.3372(G)  0.4375(B)
> +			 * Cr  0.4375(R) -0.3974(G) -0.0401(B)
> +			 */
> +			pic_param->csc.coef_ry = 0x05d;
> +			pic_param->csc.coef_gy = 0x139;
> +			pic_param->csc.coef_by = 0x020;
> +			pic_param->csc.coef_rcb = 0xffffffcd;
> +			pic_param->csc.coef_gcb = 0xffffff53;
> +			pic_param->csc.coef_bcb = 0x0e0;
> +			pic_param->csc.coef_rcr = 0x0e0;
> +			pic_param->csc.coef_gcr = 0xffffff35;
> +			pic_param->csc.coef_bcr = 0xffffffeb;
> +			pic_param->csc.offset_y = (is_10bit) ? 0x40 : 0x10;
> +			pic_param->csc.offset_cb = 0x0;
> +			pic_param->csc.offset_cr = 0x0;
> +		}
> +	}
> +}

[Severity: Medium]
Does wave6_set_csc() leave pic_param->csc zeroed when encoding RGB formats
with V4L2_YCBCR_ENC_XV601 or V4L2_YCBCR_ENC_XV709 and default quantization?

inst->quantization defaults to V4L2_QUANTIZATION_DEFAULT, and
wave6_vpu_enc_try_fmt_out() only converts V4L2_QUANTIZATION_FULL_RANGE to
V4L2_QUANTIZATION_LIM_RANGE for XV601 and XV709. Because these two branches
require inst->quantization == V4L2_QUANTIZATION_LIM_RANGE, all CSC
coefficients remain 0 when quantization is V4L2_QUANTIZATION_DEFAULT,
producing all-zero YCbCr output. In addition, V4L2_YCBCR_ENC_DEFAULT is
always treated as BT.601 here instead of being resolved via
V4L2_MAP_YCBCR_ENC_DEFAULT(inst->colorspace) as in to_matrix_coeffs().

[ ... ]

> +static int wave6_vpu_enc_start_encode(struct vpu_instance *inst)
> +{

[ ... ]

> +	src_buf = wave6_get_valid_src_buf(inst);
> +	dst_buf = wave6_get_valid_dst_buf(inst);
> +
> +	if (!src_buf || !dst_buf) {
> +		dev_dbg(inst->dev->dev, "no valid src or dst buf\n");
> +		return 0;
> +	}
> +
> +	dst_vbuf = wave6_to_vpu_buf(dst_buf);
> +	dst_vbuf->consumed = true;
> +	dst_vbuf->used = true;
> +	pic_param.pic_stream_buffer_addr = wave6_get_dma_addr(dst_buf, 0);
> +	pic_param.pic_stream_buffer_size = vb2_plane_size(&dst_buf->vb2_buf, 0);
> +
> +	src_vbuf = wave6_to_vpu_buf(src_buf);
> +	src_vbuf->consumed = true;

[ ... ]

> +static void wave6_handle_encoded_frame(struct vpu_instance *inst,
> +				       struct enc_output_info *info)
> +{
> +	struct vb2_v4l2_buffer *src_buf;
> +	struct vb2_v4l2_buffer *dst_buf;
> +	struct vpu_buffer *vpu_buf;
> +	struct vpu_buffer *dst_vpu_buf;
> +	enum vb2_buffer_state state;
> +
> +	guard(mutex)(&inst->lock);
> +
> +	state = info->encoding_success ? VB2_BUF_STATE_DONE : VB2_BUF_STATE_ERROR;
> +
> +	dst_buf = wave6_get_dst_buf_by_addr(inst, info->bitstream_buffer);
> +	if (!dst_buf) {
> +		dev_err(inst->dev->dev, "[%d] encoder can't find dst buffer\n", inst->id);
> +		return;
> +	}
> +
> +	src_buf = v4l2_m2m_src_buf_remove_by_idx(inst->v4l2_fh.m2m_ctx,
> +						 info->enc_src_idx);
> +	if (!src_buf) {
> +		dev_err(inst->dev->dev, "[%d] encoder can't find src buffer\n", inst->id);
> +		return;
> +	}
> +
> +	vpu_buf = wave6_to_vpu_buf(src_buf);
> +	if (!vpu_buf->consumed) {
> +		dev_err(inst->dev->dev, "[%d] src buffer is not consumed\n", inst->id);
> +		v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
> +		return;
> +	}

[ ... ]

> +	v4l2_m2m_dst_buf_remove_by_buf(inst->v4l2_fh.m2m_ctx, dst_buf);
> +	if (state == VB2_BUF_STATE_ERROR) {
> +		dev_dbg(inst->dev->dev, "[%d] error frame %d\n", inst->id, inst->sequence);
> +		inst->error_recovery = true;
> +		inst->error_buf_num++;
> +	}
> +	v4l2_m2m_buf_done(dst_buf, state);
> +	inst->processed_buf_num++;
> +}

[Severity: High]
Can wave6_handle_encoded_frame() race with wave6_vpu_device_run_timeout()
and corrupt the destination queue list or complete dst_buf twice?

wave6_vpu_enc_finish_encode() runs wave6_handle_encoded_frame() before
cancelling inst->task_timer in wave6_vpu_finish_job(), and
wave6_vpu_finish_job() only uses asynchronous cancel_delayed_work() while
wave6_vpu_device_run_timeout() does not take inst->lock:

CPU0 (wave6_vpu_core_irq_thread)  CPU1 (wave6_vpu_device_run_timeout)
wave6_vpu_enc_finish_encode()
  wave6_handle_encoded_frame()
    mutex_lock(&inst->lock)
    dst_buf = wave6_get_dst_buf_by_addr()
                                  dst_buf = v4l2_m2m_dst_buf_remove()
                                  v4l2_m2m_buf_done(dst_buf,
                                                    VB2_BUF_STATE_ERROR)
    v4l2_m2m_dst_buf_remove_by_buf(..., dst_buf)
      list_del(&dst_buf->list)    <-- double list_del
    v4l2_m2m_buf_done(dst_buf, state)
                                  <-- double buffer completion

> +
> +static void wave6_vpu_enc_finish_encode(struct vpu_instance *inst, bool error)
> +{
> +	int ret;
> +	struct enc_output_info info;
> +
> +	if (error) {
> +		vb2_queue_error(v4l2_m2m_get_src_vq(inst->v4l2_fh.m2m_ctx));
> +		vb2_queue_error(v4l2_m2m_get_dst_vq(inst->v4l2_fh.m2m_ctx));
> +
> +		wave6_vpu_set_instance_state(inst, VPU_INST_STATE_ERROR);
> +
> +		goto finish_encode;
> +	}
> +
> +	memset(&info, 0, sizeof(info));
> +
> +	ret = wave6_vpu_enc_get_output_info(inst, &info);
> +	if (ret) {
> +		dev_dbg(inst->dev->dev, "vpu_enc_get_output_info fail %d\n", ret);
> +		goto finish_encode;
> +	}
> +
> +	if (info.enc_src_idx >= 0 && info.recon_frame_index >= 0)
> +		wave6_handle_encoded_frame(inst, &info);
> +
> +finish_encode:
> +	wave6_vpu_finish_job(inst);
> +}

[Severity: High]
Are the in-flight source and destination buffers stranded on the M2M ready
queues when a frame is skipped or an encode error occurs?

wave6_vpu_enc_start_encode() marks src_vbuf->consumed = true and
dst_vbuf->consumed = true while leaving both buffers on m2m_ctx until
wave6_handle_encoded_frame() removes and completes them. When a frame is
skipped due to V4L2_MPEG_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT (so
info.recon_frame_index < 0), or when error is true or
wave6_vpu_enc_get_output_info() fails, wave6_handle_encoded_frame() is
not called; similarly, its early returns if src_buf is missing or unconsumed
leave dst_buf unremoved. Those buffers remain on the ready queues with
consumed = true and are skipped forever by wave6_get_valid_src_buf() and
wave6_get_valid_dst_buf().

Furthermore, when wave6_vpu_enc_start_encode() subsequently finds no
unconsumed buffer (!src_buf || !dst_buf), it returns 0 without starting a
hardware command, so wave6_vpu_device_run() leaves the M2M job running with
inst->task_timer armed until the 6-second watchdog timeout fires and kills
both queues.

[ ... ]

> +static int wave6_vpu_enc_enum_framesizes(struct file *f, void *fh, struct v4l2_frmsizeenum *fsize)
> +{
> +	const struct vpu_format *vpu_fmt;
> +
> +	if (fsize->index)
> +		return -EINVAL;
> +
> +	vpu_fmt = wave6_find_vpu_fmt(fsize->pixel_format, VPU_FMT_TYPE_CODEC);
> +	if (!vpu_fmt) {
> +		vpu_fmt = wave6_find_vpu_fmt(fsize->pixel_format, VPU_FMT_TYPE_RAW);
> +		if (!vpu_fmt)
> +			return -EINVAL;
> +	}

[Severity: Medium]
Does wave6_vpu_enc_enum_framesizes() succeed with 0x0 stepwise bounds when
fsize->pixel_format is 0?

Because wave6_find_vpu_fmt(0, VPU_FMT_TYPE_CODEC) returns a pointer to the
first zero-padded entry in wave6_vpu_enc_fmt_list[VPU_FMT_TYPE_CODEC],
VIDIOC_ENUM_FRAMESIZES returns 0 with min_width, max_width, min_height, and
max_height all set to 0 instead of returning -EINVAL.

[ ... ]

> +static int wave6_vpu_enc_try_fmt_cap(struct file *file, void *fh, struct v4l2_format *f)
> +{
> +	struct vpu_instance *inst = wave6_file_to_vpu_inst(file);
> +	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
> +	const struct vpu_format *vpu_fmt;
> +	int width, height;
> +
> +	dev_dbg(inst->dev->dev, "%s: 4cc %d w %d h %d plane %d colorspace %d\n",
> +		__func__, pix_mp->pixelformat, pix_mp->width, pix_mp->height,
> +		pix_mp->num_planes, pix_mp->colorspace);
> +
> +	if (!V4L2_TYPE_IS_CAPTURE(f->type))
> +		return -EINVAL;
> +
> +	vpu_fmt = wave6_find_vpu_fmt(pix_mp->pixelformat, VPU_FMT_TYPE_CODEC);
> +	if (!vpu_fmt) {
> +		width = inst->dst_fmt.width;
> +		height = inst->dst_fmt.height;
> +		pix_mp->pixelformat = inst->dst_fmt.pixelformat;
> +		pix_mp->num_planes = inst->dst_fmt.num_planes;
> +	} else {
> +		width = pix_mp->width;
> +		height = pix_mp->height;
> +		pix_mp->pixelformat = vpu_fmt->v4l2_pix_fmt;
> +		pix_mp->num_planes = vpu_fmt->num_planes;
> +	}
> +
> +	wave6_update_pix_fmt(pix_mp, width, height);

[Severity: High]
Can wave6_vpu_enc_try_fmt_cap() set plane_fmt[0].sizeimage to 0, and do
wave6_vpu_enc_s_fmt_out() and wave6_vpu_enc_s_selection() leave
inst->dst_fmt.plane_fmt[0].sizeimage stuck at the initial 416x240 size?

In wave6_update_pix_fmt(), sizeimage for coded formats is only recalculated
when plane_fmt[0].sizeimage is 0:

drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c:wave6_update_pix_fmt() {
    ...
	if (!fmt_info) {
		pix_mp->plane_fmt[0].bytesperline = 0;
		if (!pix_mp->plane_fmt[0].sizeimage)
			pix_mp->plane_fmt[0].sizeimage = width * height;

		return;
	}
    ...
}

When pix_mp->pixelformat is valid in wave6_vpu_enc_try_fmt_cap(), width and
height are taken directly from userspace without clamping or using
inst->dst_fmt. Passing width = 0 or height = 0 with sizeimage = 0 sets
inst->dst_fmt.plane_fmt[0].sizeimage to 0 in wave6_vpu_enc_s_fmt_cap(),
which triggers WARN_ON(!plane_sizes[i]) in vb2_core_reqbufs().

Conversely, because wave6_vpu_open_enc() initializes inst->dst_fmt to
416x240 (sizeimage = 99840) and neither wave6_vpu_enc_s_fmt_out() nor
wave6_vpu_enc_s_selection() clears inst->dst_fmt.plane_fmt[0].sizeimage
before calling wave6_update_pix_fmt(&inst->dst_fmt, ...), raising the
output resolution leaves capture bitstream buffers undersized at 99840
bytes.

[ ... ]

> +static int wave6_vpu_enc_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
> +{
> +	struct vpu_instance *inst = wave6_file_to_vpu_inst(file);
> +	u32 max_crop_w, max_crop_h;
> +
> +	if (!V4L2_TYPE_IS_OUTPUT(s->type))
> +		return -EINVAL;
> +
> +	if (s->target != V4L2_SEL_TGT_CROP)
> +		return -EINVAL;
> +
> +	if (!(s->flags & (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE)))
> +		s->flags |= V4L2_SEL_FLAG_LE;
> +
> +	if (s->flags & V4L2_SEL_FLAG_GE) {
> +		s->r.left = round_up(s->r.left, W6_ENC_CROP_STEP);
> +		s->r.top = round_up(s->r.top, W6_ENC_CROP_STEP);
> +		s->r.width = round_up(s->r.width, W6_ENC_CROP_STEP);
> +		s->r.height = round_up(s->r.height, W6_ENC_CROP_STEP);
> +	}
> +	if (s->flags & V4L2_SEL_FLAG_LE) {
> +		s->r.left = round_down(s->r.left, W6_ENC_CROP_STEP);
> +		s->r.top = round_down(s->r.top, W6_ENC_CROP_STEP);
> +		s->r.width = round_down(s->r.width, W6_ENC_CROP_STEP);
> +		s->r.height = round_down(s->r.height, W6_ENC_CROP_STEP);
> +	}
> +
> +	s->r.left = clamp_t(s32, s->r.left, 0,
> +			    inst->src_fmt.width - W6_MIN_ENC_PIC_WIDTH);
> +	s->r.top = clamp_t(s32, s->r.top, 0,
> +			   inst->src_fmt.height - W6_MIN_ENC_PIC_HEIGHT);
> +
> +	max_crop_w = inst->src_fmt.width - s->r.left;
> +	max_crop_h = inst->src_fmt.height - s->r.top;
> +
> +	if (!s->r.width || !s->r.height)
> +		return 0;
> +	if (max_crop_w < W6_MIN_ENC_PIC_WIDTH)
> +		return 0;
> +	if (max_crop_h < W6_MIN_ENC_PIC_HEIGHT)
> +		return 0;
> +
> +	s->r.width = clamp(s->r.width, W6_MIN_ENC_PIC_WIDTH, max_crop_w);
> +	s->r.height = clamp(s->r.height, W6_MIN_ENC_PIC_HEIGHT, max_crop_h);
> +
> +	wave6_update_pix_fmt(&inst->dst_fmt, s->r.width, s->r.height);
> +	wave6_update_crop_info(inst, s->r.left, s->r.top, s->r.width, s->r.height);

[Severity: High]
Can wave6_vpu_enc_s_selection() modify inst->codec_rect while streaming,
and does it return a 0-sized rectangle when width or height is 0?

wave6_vpu_enc_s_selection() does not check vb2_is_busy() on the queues or
whether inst->state != VPU_INST_STATE_NONE. Because hardware picture
dimensions are configured once at streamon while
wave6_update_frame_buf_addr() adds inst->codec_rect.top and
inst->codec_rect.left to the source frame DMA addresses on every frame,
changing the crop offset mid-stream shifts the DMA base addresses while the
VPU still reads the original frame dimensions past the end of the source
buffer.

In addition, returning 0 early when !s->r.width || !s->r.height bypasses
the clamp() calls right below and reports an invalid 0-sized crop rectangle
back to userspace.

[ ... ]

> +static int wave6_vpu_enc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
> +{
> +	struct vpu_instance *inst = wave6_file_to_vpu_inst(file);
> +	struct enc_controls *ctrls = &inst->enc_ctrls;
> +
> +	dev_dbg(inst->dev->dev, "%s: type %d\n", __func__, a->type);
> +
> +	if (!V4L2_TYPE_IS_OUTPUT(a->type))
> +		return -EINVAL;
> +
> +	a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
> +	if (a->parm.output.timeperframe.denominator &&
> +	    a->parm.output.timeperframe.numerator) {
> +		ctrls->frame_rate = DIV_ROUND_CLOSEST(a->parm.output.timeperframe.denominator,
> +						      a->parm.output.timeperframe.numerator);
> +		ctrls->frame_rate = clamp(ctrls->frame_rate,
> +					  MIN_FRAME_RATE, MAX_FRAME_RATE);
> +	} else {
> +		a->parm.output.timeperframe.numerator = 1;
> +		a->parm.output.timeperframe.denominator = ctrls->frame_rate;
> +	}

[Severity: Low]
Should wave6_vpu_enc_s_parm() update a->parm.output.timeperframe after
computing and clamping ctrls->frame_rate?

When numerator and denominator are non-zero, the driver rounds and clamps
ctrls->frame_rate but leaves the caller's unadjusted values in
a->parm.output.timeperframe instead of returning 1 / ctrls->frame_rate.

[ ... ]

> +static void wave6_set_enc_hevc_param(struct enc_codec_param *output,
> +				     struct hevc_enc_controls *ctrls)
> +{

[ ... ]

> +	switch (ctrls->refresh_type) {
> +	case V4L2_MPEG_VIDEO_HEVC_REFRESH_NONE:
> +		output->decoding_refresh_type = DEC_REFRESH_TYPE_NON_IRAP;
> +		break;
> +	case V4L2_MPEG_VIDEO_HEVC_REFRESH_IDR:
> +		output->decoding_refresh_type = DEC_REFRESH_TYPE_IDR;
> +		break;
> +	default:
> +		break;
> +	}
> +	output->intra_period = ctrls->refresh_period;
> +	if (output->idr_period) {
> +		output->decoding_refresh_type = DEC_REFRESH_TYPE_IDR;
> +		output->intra_period = output->idr_period;
> +		output->idr_period = 0;
> +	}

[Severity: Medium]
Does checking if (output->idr_period) here overwrite the HEVC refresh type
and period whenever V4L2_CID_MPEG_VIDEO_GOP_SIZE is non-zero?

wave6_set_enc_open_param() sets output->idr_period = ctrls->gop_size
(which defaults to 30) before calling wave6_set_enc_hevc_param(). Checking
if (output->idr_period) instead of if (!output->intra_period) clobbers
ctrls->refresh_period and ctrls->refresh_type unless GOP size is explicitly
set to 0.

[ ... ]

> +static int wave6_vpu_enc_buf_prepare(struct vb2_buffer *vb)
> +{
> +	struct vpu_instance *inst = vb2_get_drv_priv(vb->vb2_queue);
> +
> +	if (V4L2_TYPE_IS_CAPTURE(vb->type))
> +		return 0;

[Severity: Medium]
Should wave6_vpu_enc_buf_prepare() validate the capture plane size against
inst->dst_fmt.plane_fmt[0].sizeimage and reset the plane payload to 0?

Returning early for capture buffers allows undersized imported DMABUFs to
be queued without error, and a re-queued capture buffer that is later
completed as an empty EOS buffer via v4l2_m2m_last_buffer_done() when
V4L2_ENC_CMD_STOP or output streamoff runs retains its stale bytesused
payload from the previous frame.

[ ... ]

> +static int wave6_vpu_enc_start_streaming(struct vb2_queue *q, unsigned int count)
> +{
> +	struct vpu_instance *inst = vb2_get_drv_priv(q);
> +	int ret = 0;
> +
> +	v4l2_m2m_update_start_streaming_state(inst->v4l2_fh.m2m_ctx, q);
> +
> +	if (inst->state == VPU_INST_STATE_NONE) {
> +		ret = wave6_vpu_enc_create_instance(inst);
> +		if (ret)
> +			goto exit;
> +	}
> +
> +	if (inst->state == VPU_INST_STATE_OPEN) {
> +		ret = wave6_vpu_enc_initialize_instance(inst);
> +		if (ret) {
> +			wave6_vpu_enc_destroy_instance(inst);
> +			goto exit;
> +		}
> +	}
> +
> +	if (inst->state == VPU_INST_STATE_INIT_SEQ) {
> +		ret = wave6_vpu_enc_prepare_fb(inst);
> +		if (ret) {
> +			wave6_vpu_enc_destroy_instance(inst);
> +			goto exit;
> +		}
> +	}
> +
> +exit:
> +	if (ret)
> +		wave6_vpu_return_buffers(inst, q->type, VB2_BUF_STATE_QUEUED);
> +
> +	return ret;
> +}
> +
> +static void wave6_vpu_enc_stop_streaming(struct vb2_queue *q)
> +{
> +	struct vpu_instance *inst = vb2_get_drv_priv(q);
> +
> +	dev_dbg(inst->dev->dev, "[%d] %s, input %d, encode %d\n",
> +		inst->id, V4L2_TYPE_IS_OUTPUT(q->type) ? "output" : "capture",
> +		inst->queued_src_buf_num, inst->sequence);
> +
> +	if (inst->state == VPU_INST_STATE_NONE)
> +		goto exit;
> +
> +	if (V4L2_TYPE_IS_OUTPUT(q->type)) {
> +		inst->queued_src_buf_num = 0;
> +		inst->processed_buf_num = 0;
> +		inst->error_buf_num = 0;
> +		inst->sequence = 0;
> +	} else {
> +		inst->queued_dst_buf_num = 0;
> +	}
> +
> +	if (V4L2_TYPE_IS_CAPTURE(q->type) && inst->state != VPU_INST_STATE_NONE)
> +		wave6_vpu_enc_destroy_instance(inst);
> +
> +exit:
> +	wave6_vpu_return_buffers(inst, q->type, VB2_BUF_STATE_ERROR);
> +	v4l2_m2m_update_stop_streaming_state(inst->v4l2_fh.m2m_ctx, q);
> +}

[Severity: High]
Can starting streaming on one queue before configuring the other leave the
hardware instance and FBC reference buffers out of sync with the configured
formats?

wave6_vpu_enc_start_streaming() creates the hardware instance and allocates
FBC buffers sized to inst->codec_rect on whichever queue starts streaming
first, while wave6_vpu_enc_s_fmt_out() and wave6_vpu_enc_s_fmt_cap() only
check vb2_is_busy() on their own queue. If userspace starts capture
streaming first (or stops only the output queue, which does not destroy the
instance) and then changes the output resolution or capture codec format,
the second STREAMON sees inst->state == VPU_INST_STATE_PIC_RUN and skips
reinitializing the hardware instance and FBC buffers.

In addition, if capture stops streaming before output,
wave6_vpu_enc_stop_streaming() destroys the instance and sets inst->state
to VPU_INST_STATE_NONE, so stopping the output queue afterwards takes the
early goto exit and skips resetting queued_src_buf_num, processed_buf_num,
error_buf_num, and sequence.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795075577.git.nas.chung@chipsnmedia.com?part=7

  reply	other threads:[~2026-11-19  9:35 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19  8:59 [PATCH v8 00/11] Add support for Wave6 video codec driver Nas Chung
2026-11-19  8:59 ` [PATCH v8 01/11] dt-bindings: media: nxp: Add Wave6 video codec device Nas Chung
2026-11-19  8:59 ` [PATCH v8 02/11] media: chips-media: wave6: Add Wave6 control driver Nas Chung
2026-11-19  9:26   ` sashiko-bot
2026-11-19  8:59 ` [PATCH v8 03/11] media: chips-media: wave6: Add Wave6 core driver Nas Chung
2026-11-19  9:32   ` sashiko-bot
2026-11-19  8:59 ` [PATCH v8 04/11] media: chips-media: wave6: Add Wave6 decoder firmware interface Nas Chung
2026-11-19  9:22   ` sashiko-bot
2026-11-19  8:59 ` [PATCH v8 05/11] media: chips-media: wave6: Add Wave6 m2m decoder Nas Chung
2026-11-19  9:31   ` sashiko-bot
2026-11-19  9:00 ` [PATCH v8 06/11] media: chips-media: wave6: Add Wave6 encoder firmware interface Nas Chung
2026-11-19  9:10   ` sashiko-bot
2026-11-19  9:00 ` [PATCH v8 07/11] media: chips-media: wave6: Add Wave6 m2m encoder Nas Chung
2026-11-19  9:35   ` sashiko-bot [this message]
2026-11-19  9:00 ` [PATCH v8 08/11] media: chips-media: wave6: Add trace and debugfs Nas Chung
2026-11-19  9:14   ` sashiko-bot
2026-11-19  9:00 ` [PATCH v8 09/11] media: chips-media: wave6: Add performance profiling Nas Chung
2026-11-19  9:20   ` sashiko-bot
2026-11-19  9:00 ` [PATCH v8 10/11] media: chips-media: wave6: Add Wave6 thermal cooling device Nas Chung
2026-11-19  9:21   ` sashiko-bot
2026-11-19  9:00 ` [PATCH v8 11/11] arm64: dts: freescale: imx95: Add video codec node Nas Chung
2026-11-19  9:37   ` sashiko-bot

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-222858@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=Frank.Li@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=imx@lists.linux.dev \
    --cc=media-ci@linuxtv.org \
    --cc=nas.chung@chipsnmedia.com \
    --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