All of lore.kernel.org
 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 05/11] media: chips-media: wave6: Add Wave6 m2m decoder
Date: Thu, 19 Nov 2026 09:31:41 +0000	[thread overview]
Message-ID: <sashiko-outbox-222840@kernel.org> (raw)
In-Reply-To: <131b501b33d697b070553bb5ff9ffafcaea812d6.1795075581.git.nas.chung@chipsnmedia.com>

Thank you for your contribution! Sashiko AI review found 13 potential
issues to consider:
- [High] media: wave6: infinite job loop on failure in
  wave6_vpu_dec_seek_header()
- [High] media: wave6: missing CAPTURE buffer size check after format
  update
- [High] media: wave6: use-after-free of m2m_ctx in
  wave6_vpu_dec_release()
- [Medium] media: wave6: unhandled query failure in
  wave6_vpu_dec_finish_decode()
- [Medium] media: wave6: fbc_buf_registered not reset in
  wave6_vpu_dec_release_fb()
- [Medium] media: wave6: zero pixelformat matched in
  wave6_find_vpu_fmt()
- [Medium] media: wave6: skipped FBC setup in
  wave6_vpu_dec_start_streaming() after seek
- [Medium] media: wave6: duplicate display buffer entries in
  p_dec_info->disp_buf[]
- [Medium] media: wave6: infinite job rescheduling loop on
  DEC_NOTI_FLAG_NO_FB
- [Medium] media: wave6: race between wave6_vpu_dec_finish_decode() and
  timeout work
- [Medium] media: wave6: unlocked cap_q_ctx.rdy_queue traversal races
  with queue ops
- [Medium] media: wave6: unlocked state updates and early event in
  source change
- [Medium] media: wave6: ignored buffer registration errors in
  prepare_decode()
--

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

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

This commit adds the V4L2 m2m stateful decoder driver for the Chips&Media
Wave6 video codec IP, supporting H.264 and HEVC 8-bit decoding, downscaling,
and dynamic resolution changes.

> diff --git a/drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c b/drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c
> new file mode 100644
> index 0000000000000..54b408922bbc2
> --- /dev/null
> +++ b/drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c
> @@ -0,0 +1,1935 @@

[ ... ]

> +static const struct vpu_format wave6_vpu_dec_fmt_list[2][6] = {
> +	[VPU_FMT_TYPE_CODEC] = {
> +		{
> +			.v4l2_pix_fmt = V4L2_PIX_FMT_HEVC,
> +			.max_width = W6_MAX_DEC_PIC_WIDTH,
> +			.min_width = W6_MIN_DEC_PIC_WIDTH,
> +			.max_height = W6_MAX_DEC_PIC_HEIGHT,
> +			.min_height = W6_MIN_DEC_PIC_HEIGHT,
> +			.num_planes = 1,
> +		},
> +		{
> +			.v4l2_pix_fmt = V4L2_PIX_FMT_H264,
> +			.max_width = W6_MAX_DEC_PIC_WIDTH,
> +			.min_width = W6_MIN_DEC_PIC_WIDTH,
> +			.max_height = W6_MAX_DEC_PIC_HEIGHT,
> +			.min_height = W6_MIN_DEC_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_dec_fmt_list[type]); i++) {
> +		if (wave6_vpu_dec_fmt_list[type][i].v4l2_pix_fmt == v4l2_pix_fmt)
> +			return &wave6_vpu_dec_fmt_list[type][i];
> +	}
> +
> +	return NULL;
> +}

[Severity: Medium]
Can wave6_find_vpu_fmt() match an unpopulated entry in
wave6_vpu_dec_fmt_list[VPU_FMT_TYPE_CODEC] when v4l2_pix_fmt is 0?

VPU_FMT_TYPE_CODEC only populates 2 of its 6 slots, leaving slots 2..5
zero-initialized.  Passing pixelformat == 0 to VIDIOC_S_FMT on OUTPUT
matches slot 2 instead of falling back to inst->src_fmt, which sets
inst->src_fmt.num_planes and plane sizes to 0 and triggers WARN_ON() in
vb2_core_reqbufs() on a subsequent VIDIOC_REQBUFS.

[ ... ]

> +static void wave6_vpu_dec_release_fb(struct vpu_instance *inst)
> +{
> +	for (int i = 0; i < WAVE6_MAX_FBS; i++) {
> +		wave6_vdi_free_dma(&inst->frame_vbuf[i]);
> +		memset(&inst->frame_buf[i], 0, sizeof(struct frame_buffer));
> +		wave6_vdi_free_dma(&inst->aux_vbuf[AUX_BUF_FBC_Y_TBL][i]);
> +		wave6_vdi_free_dma(&inst->aux_vbuf[AUX_BUF_FBC_C_TBL][i]);
> +		wave6_vdi_free_dma(&inst->aux_vbuf[AUX_BUF_MV_COL][i]);
> +	}
> +}
> +
> +static void wave6_vpu_dec_destroy_instance(struct vpu_instance *inst)
> +{
> +	u32 fail_res = 0;
> +	int ret;
> +
> +	wave6_vpu_disable_instance(inst);
> +
> +	ret = wave6_vpu_dec_close(inst, &fail_res);
> +	if (ret) {
> +		dev_err(inst->dev->dev, "failed destroy instance: %d (%d)\n",
> +			ret, fail_res);
> +	}
> +
> +	scoped_guard(spinlock, &inst->dev->inst_lock)
> +		list_del_init(&inst->list);
> +	wave6_vpu_dec_release_fb(inst);
> +	wave6_vpu_set_instance_state(inst, VPU_INST_STATE_NONE);
> +
> +	pm_runtime_put_sync(inst->dev->dev);
> +	wave6_vpu_core_deactivate(inst->dev);
> +}

[Severity: Medium]
Should wave6_vpu_dec_release_fb() or wave6_vpu_dec_destroy_instance()
reset inst->fbc_buf_registered to false?

When wave6_vpu_dec_queue_setup() destroys the instance in
VPU_INST_STATE_SEEK and a new instance is created on the next OUTPUT
STREAMON, inst->fbc_buf_registered remains true from the destroyed
instance even though all FBC buffers were freed.  If V4L2_DEC_CMD_STOP is
then issued before a sequence header is parsed,
wave6_vpu_dec_prepare_decode() skips the !inst->fbc_buf_registered early
drain exit and issues W6_CMD_INIT_SEQ on an empty bitstream.

[ ... ]

> +static void wave6_vpu_dec_handle_dst_buffer(struct vpu_instance *inst)
> +{
> +	struct vb2_v4l2_buffer *dst_buf;
> +	struct v4l2_m2m_buffer *v4l2_m2m_buf;
> +	struct vpu_buffer *vpu_buf;
> +	u32 fb_stride = inst->dst_fmt.plane_fmt[0].bytesperline;
> +	u32 luma_size = fb_stride * inst->dst_fmt.height;
> +	u32 chroma_size = (fb_stride / 2) * (inst->dst_fmt.height / 2);
> +	struct frame_buffer disp_buffer = { };
> +	struct dec_seq_info *seq_info = &inst->codec_info->dec_info.seq_info;
> +	int consumed_num = wave6_vpu_get_consumed_fb_num(inst);
> +	int ret;
> +
> +	v4l2_m2m_for_each_dst_buf(inst->v4l2_fh.m2m_ctx, v4l2_m2m_buf) {

[Severity: Medium]
What serializes v4l2_m2m_for_each_dst_buf() against concurrent list
modifications on cap_q_ctx.rdy_queue?

v4l2_m2m_for_each_dst_buf() iterates cap_q_ctx.rdy_queue without holding
cap_q_ctx.rdy_spinlock:

kworker                           CPU0 (VIDIOC_QBUF)
wave6_vpu_device_run()
  wave6_vpu_dec_prepare_decode()
    wave6_vpu_dec_handle_dst_buffer()
      v4l2_m2m_for_each_dst_buf()
                                  wave6_vpu_dec_buf_queue_dst()
                                    guard(mutex)(&inst->lock)
                                    v4l2_m2m_buf_queue()
                                      list_add_tail(&b->list, ...)
        /* unlocked list traversal */

Similarly, wave6_vpu_core_irq_thread() traverses cap_q_ctx.rdy_queue
unlocked in wave6_get_dst_buf_by_addr() and calls
v4l2_m2m_dst_buf_remove_by_buf(), which can race with
wave6_vpu_dec_buf_queue_dst() or with v4l2_m2m_for_each_dst_buf() in
wave6_vpu_dec_cmd_start() and corrupt the list traversal.

> +		dst_buf = &v4l2_m2m_buf->vb;
> +		vpu_buf = wave6_to_vpu_buf(dst_buf);
> +
> +		if (vpu_buf->consumed)
> +			continue;
> +
> +		if (consumed_num >= WAVE6_MAX_FBS)
> +			break;
> +
> +		if (inst->dst_fmt.num_planes == 1) {
> +			disp_buffer.buf_y = wave6_get_dma_addr(dst_buf, 0);
> +			disp_buffer.buf_cb = disp_buffer.buf_y + luma_size;
> +			disp_buffer.buf_cr = disp_buffer.buf_cb + chroma_size;
> +		} else if (inst->dst_fmt.num_planes == 2) {
> +			disp_buffer.buf_y = wave6_get_dma_addr(dst_buf, 0);
> +			disp_buffer.buf_cb = wave6_get_dma_addr(dst_buf, 1);
> +			disp_buffer.buf_cr = disp_buffer.buf_cb + chroma_size;
> +		} else if (inst->dst_fmt.num_planes == 3) {
> +			disp_buffer.buf_y = wave6_get_dma_addr(dst_buf, 0);
> +			disp_buffer.buf_cb = wave6_get_dma_addr(dst_buf, 1);
> +			disp_buffer.buf_cr = wave6_get_dma_addr(dst_buf, 2);
> +		}
> +		disp_buffer.height = inst->src_fmt.height;
> +		disp_buffer.stride = fb_stride;
> +		disp_buffer.map_type = LINEAR_FRAME_MAP;
> +		disp_buffer.luma_bit_depth = seq_info->luma_bit_depth;
> +		disp_buffer.chroma_bit_depth = seq_info->chroma_bit_depth;
> +		disp_buffer.c_fmt_idc = seq_info->c_fmt_idc;
> +
> +		ret = wave6_vpu_dec_register_display_buffer(inst, disp_buffer);
> +		if (ret) {
> +			dev_err(inst->dev->dev, "fail register display buffer %d", ret);
> +			break;
> +		}
> +
> +		vpu_buf->consumed = true;
> +		consumed_num++;
> +	}

[Severity: Medium]
Does re-queuing a displayed CAPTURE buffer insert duplicate entries into
p_dec_info->disp_buf[] until all WAVE6_MAX_FBS slots are exhausted?

When a displayed buffer is dequeued and re-queued via VIDIOC_QBUF,
wave6_vpu_dec_buf_queue() resets vpu_buf->consumed = false, so
wave6_vpu_dec_handle_dst_buffer() calls
wave6_vpu_dec_register_display_buffer() again.  In commit "media:
chips-media: wave6: Add Wave6 decoder firmware interface",
wave6_dec_set_display_buffer() always writes to the first empty
!disp_buf[i].buf_y slot without checking if fb.buf_y is already present,
and wave6_vpu_dec_get_output_info() only removes buffers on
W6_RET_DEC_RELEASE_IDC, not W6_RET_DEC_DISP_IDC:

drivers/media/platform/chips-media/wave6/wave6-hw.c:wave6_dec_set_display_buffer() {
	for (int i = 0; i < WAVE6_MAX_FBS; i++) {
		if (!p_dec_info->disp_buf[i].buf_y) {
			p_dec_info->disp_buf[i] = fb;
			break;
		}
	}
}

After 31 displayed frames are re-queued, p_dec_info->disp_buf[] is full of
duplicates and cannot record any new buffer address.

> +
> +	if (!inst->fbc_buf_registered) {
> +		ret = wave6_vpu_dec_register_frame_buffer(inst);
> +		if (ret)
> +			dev_err(inst->dev->dev, "register frame buffer fail %d\n", ret);
> +		else
> +			inst->fbc_buf_registered = true;
> +	}
> +}

[Severity: Medium]
Should wave6_vpu_dec_handle_dst_buffer() propagate registration errors to
wave6_vpu_dec_prepare_decode()?

If wave6_vpu_dec_register_frame_buffer() or
wave6_vpu_dec_register_display_buffer() fails, the error is logged and
dropped, and wave6_vpu_dec_prepare_decode() still returns 0.
wave6_vpu_device_run() then arms inst->task_timer and calls
wave6_vpu_dec_start_decode() without registered FBC or display buffers.

[ ... ]

> +static void wave6_vpu_dec_handle_source_change(struct vpu_instance *inst)
> +{
> +	static const struct v4l2_event vpu_event_src_ch = {
> +		.type = V4L2_EVENT_SOURCE_CHANGE,
> +		.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
> +	};
> +	struct dec_seq_info *info = &inst->codec_info->dec_info.seq_info;
> +
> +	dev_dbg(inst->dev->dev, "pic size %dx%d profile %d, min_fb_cnt : %d | min_disp_cnt : %d\n",
> +		info->pic_width, info->pic_height,
> +		info->profile, info->min_frame_buffer_count, info->frame_buf_delay);
> +
> +	wave6_vpu_dec_retry_one_frame(inst);
> +	wave6_vpu_dec_reset_frame_buffer_info(inst);
> +	inst->fbc_buf_registered = false;
> +
> +	wave6_vpu_set_instance_state(inst, VPU_INST_STATE_INIT_SEQ);
> +
> +	inst->scaler_info.enable = false;
> +	inst->crop.left = info->pic_crop_rect.left;
> +	inst->crop.top = info->pic_crop_rect.top;
> +	inst->crop.width = info->pic_crop_rect.right - inst->crop.left;
> +	inst->crop.height = info->pic_crop_rect.bottom - inst->crop.top;
> +
> +	wave6_update_v4l2_ctrls(inst, info);
> +	wave6_update_color_info(inst, info);
> +	wave6_update_pix_fmt(&inst->src_fmt, info->pic_width, info->pic_height);
> +	wave6_update_pix_fmt_cap(&inst->dst_fmt,
> +				 info->pic_width, info->pic_height,
> +				 true);
> +
> +	v4l2_event_queue_fh(&inst->v4l2_fh, &vpu_event_src_ch);
> +}

[Severity: Medium]
Can queuing V4L2_EVENT_SOURCE_CHANGE inside
wave6_vpu_dec_handle_source_change() wake userspace before the caller
finishes marking the last frame?

CPU0 (irq_thread / kworker)       CPU1 (userspace)
wave6_vpu_dec_seek_header()
  wave6_vpu_dec_handle_source_change()
    /* updates formats unlocked */
    v4l2_event_queue_fh(...)
                                  /* wakes on SOURCE_CHANGE event */
                                  VIDIOC_STREAMON(CAPTURE)
                                  VIDIOC_QBUF(CAPTURE)
  if (vb2_is_streaming(dst_vq))
    wave6_handle_last_frame()     <-- tags new buffer with LAST

In addition, wave6_vpu_dec_handle_source_change() modifies inst->state,
inst->crop, inst->src_fmt, and inst->dst_fmt without holding
core->dev_lock, so a concurrent VIDIOC_G_FMT or VIDIOC_TRY_FMT can observe
partially updated format fields (such as bytesperline temporarily set to
0).  Queuing the event after wave6_handle_last_frame() and serializing the
format update would avoid this race.

[ ... ]

> +static void wave6_vpu_dec_finish_decode(struct vpu_instance *inst, bool error)
> +{
> +	struct dec_output_info info;
> +	int ret;
> +
> +	memset(&info, 0, sizeof(info));
> +
> +	ret = wave6_vpu_dec_get_output_info(inst, &info);
> +	if (ret)
> +		goto finish_decode;

[Severity: Medium]
What synchronizes wave6_vpu_dec_finish_decode() against inst->task_timer
(wave6_vpu_device_run_timeout())?

wave6_vpu_dec_finish_decode() does not cancel inst->task_timer on entry;
wave6_vpu_finish_job() only calls non-synchronous cancel_delayed_work() at
the very end:

CPU0 (irq_thread)                 CPU1 (kworker)
wave6_vpu_dec_finish_decode()     wave6_vpu_device_run_timeout()
  wave6_handle_decoded_frame()
    src_buf = v4l2_m2m_next_src_buf()
                                    src_buf = v4l2_m2m_src_buf_remove()
                                    v4l2_m2m_buf_done(src_buf, ...)
    wave6_vpu_dec_src_buf_done()
      v4l2_m2m_src_buf_remove_by_buf()
      v4l2_m2m_buf_done(src_buf)  <-- double remove and buf_done

If the timeout fires while the completion interrupt is being handled, both
paths remove and complete the same source and destination buffers and both
call v4l2_m2m_job_finish().

[Severity: Medium]
If wave6_vpu_dec_get_output_info() fails here, does jumping to
finish_decode leave the consumed head OUTPUT buffer on the ready queue?

Unlike wave6_vpu_dec_start_decode(), this error path neither removes and
completes the head src_buf with VB2_BUF_STATE_ERROR nor transitions
inst->state to VPU_INST_STATE_ERROR, so v4l2_m2m_job_finish() can schedule
the next job with the already-consumed buffer still at the head of the
queue.

> +
> +	dev_dbg(inst->dev->dev, "dec %d dis %d noti_flag %d stream_end %d\n",
> +		info.frame_decoded, info.frame_display,
> +		info.notification_flags, info.stream_end);
> +
> +	if (info.notification_flags & DEC_NOTI_FLAG_NO_FB) {
> +		wave6_vpu_dec_retry_one_frame(inst);
> +		goto finish_decode;
> +	}

[Severity: Medium]
Does DEC_NOTI_FLAG_NO_FB cause an immediate job rescheduling busy loop?

In wave6_vpu_dec_handle_dst_buffer(), queued CAPTURE buffers have
vpu_buf->consumed set to true while vpu_buf->used remains false until
wave6_handle_decoded_frame().  When DEC_NOTI_FLAG_NO_FB jumps to
finish_decode without setting vpu_buf->used, wave6_vpu_check_fb_available()
in commit "media: chips-media: wave6: Add Wave6 core driver" still sees
!vpu_buf->used and reports the job as ready:

drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c:wave6_vpu_check_fb_available() {
	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->used)
			return true;
	}
}

v4l2_m2m_job_finish() then immediately re-runs the job without waiting for
a new CAPTURE buffer, and wave6_vpu_dec_handle_dst_buffer() skips all
existing CAPTURE buffers because their vpu_buf->consumed is already true.

[ ... ]

> +static int wave6_vpu_dec_s_fmt_out(struct file *file, void *fh, struct v4l2_format *f)
> +{

[ ... ]

> +	wave6_update_pix_fmt_cap(&inst->dst_fmt,
> +				 pix_mp->width, pix_mp->height,
> +				 true);
> +
> +	return 0;
> +}

[Severity: High]
Can updating inst->dst_fmt in wave6_vpu_dec_s_fmt_out() or
wave6_vpu_dec_handle_source_change() allow undersized CAPTURE buffers to
be registered for DMA?

If userspace allocates and queues (or prepares via VIDIOC_PREPARE_BUF)
CAPTURE buffers at the default 720x480 format before CAPTURE STREAMON,
wave6_vpu_dec_buf_prepare() validates their sizes against 720x480 and marks
them prepared.  If inst->dst_fmt is then increased (up to 4096x4096) via
VIDIOC_S_FMT on OUTPUT or sequence header parsing, VIDIOC_STREAMON on
CAPTURE calls wave6_vpu_dec_start_streaming() -> wave6_vpu_dec_prepare_fb()
without re-checking the prepared buffers' plane sizes against the new
inst->dst_fmt.plane_fmt[i].sizeimage (unlike wave6_vpu_dec_cmd_start()).
wave6_vpu_dec_handle_dst_buffer() then registers those smaller DMA buffers
with the hardware using the new stride and height, causing out-of-bounds
DMA writes.

[ ... ]

> +static int wave6_vpu_dec_seek_header(struct vpu_instance *inst)
> +{
> +	struct dec_seq_info seq_info;
> +	int ret;
> +
> +	memset(&seq_info, 0, sizeof(struct dec_seq_info));
> +
> +	ret = wave6_vpu_dec_issue_seq_init(inst);
> +	if (ret) {
> +		dev_err(inst->dev->dev, "failed wave6_vpu_dec_issue_seq_init %d\n", ret);
> +		return ret;
> +	}
> +
> +	ret = wave6_vpu_wait_interrupt(inst, W6_VPU_TIMEOUT);
> +	if (ret) {
> +		dev_err(inst->dev->dev, "failed to call vpu_wait_interrupt()\n");
> +		return ret;
> +	}
> +
> +	ret = wave6_vpu_dec_complete_seq_init(inst, &seq_info);
> +	if (ret) {
> +		dev_err(inst->dev->dev, "vpu_dec_complete_seq_init: %d, reason : 0x%x\n",
> +			ret, seq_info.err_reason);
> +		if ((seq_info.err_reason & WAVE6_SYSERR_NOT_SUPPORT) ||
> +		    (seq_info.err_reason & WAVE6_SYSERR_NOT_SUPPORT_PROFILE)) {
> +			ret = -EINVAL;
> +		} else if (seq_info.err_reason == HEVC_ETCERR_INIT_SEQ_SPS_NOT_FOUND ||
> +			   seq_info.err_reason == AVC_ETCERR_INIT_SEQ_SPS_NOT_FOUND) {
> +			wave6_handle_skipped_frame(inst);
> +			ret = 0;
> +		}
> +	} else {
> +		wave6_vpu_dec_handle_source_change(inst);
> +		inst->v4l2_fh.m2m_ctx->ignore_cap_streaming = false;
> +		v4l2_m2m_set_dst_buffered(inst->v4l2_fh.m2m_ctx, false);
> +		if (vb2_is_streaming(v4l2_m2m_get_dst_vq(inst->v4l2_fh.m2m_ctx))) {
> +			scoped_guard(mutex, &inst->lock)
> +				wave6_handle_last_frame(inst, NULL);
> +		}
> +	}
> +
> +	return ret;
> +}
> +
> +static int wave6_vpu_dec_prepare_decode(struct vpu_instance *inst)
> +{
> +	bool stream_end;
> +
> +	scoped_guard(mutex, &inst->lock)
> +		stream_end = inst->drain && !inst->drain_src_buf_num;
> +
> +	if (stream_end && !inst->fbc_buf_registered) {
> +		wave6_vpu_dec_finish_drain(inst);
> +		return -EAGAIN;
> +	}
> +
> +	wave6_handle_bitstream_buffer(inst, stream_end);
> +
> +	if (inst->state < VPU_INST_STATE_PIC_RUN) {
> +		if (inst->state == VPU_INST_STATE_OPEN &&
> +		    wave6_vpu_dec_seek_header(inst)) {
> +			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));
> +		}
> +
> +		return -EAGAIN;
> +	}

[Severity: High]
Does a failure in wave6_vpu_dec_seek_header() trigger an infinite m2m job
loop?

When wave6_vpu_dec_seek_header() fails with any error other than
SPS_NOT_FOUND (such as WAVE6_SYSERR_NOT_SUPPORT_PROFILE on a 10-bit stream
or a timeout), the consumed head OUTPUT buffer is not removed from the
ready queue and inst->state remains VPU_INST_STATE_OPEN.  Calling
vb2_queue_error() only sets q->error without stopping streaming, and
wave6_vpu_job_ready() returns 1 whenever inst->state == VPU_INST_STATE_OPEN.
When wave6_vpu_dec_prepare_decode() returns -EAGAIN,
wave6_vpu_device_run() calls v4l2_m2m_job_finish(), which immediately
re-queues and re-runs the job in an unbounded loop on the same buffer.

[ ... ]

> +static int wave6_vpu_dec_start_streaming(struct vb2_queue *q, unsigned int count)
> +{
> +	struct vpu_instance *inst = vb2_get_drv_priv(q);
> +	struct v4l2_pix_format_mplane *fmt;
> +	int ret = 0;
> +
> +	if (V4L2_TYPE_IS_OUTPUT(q->type)) {
> +		fmt = &inst->src_fmt;
> +		if (inst->state == VPU_INST_STATE_NONE) {
> +			ret = wave6_vpu_dec_create_instance(inst);
> +			if (!ret) {
> +				inst->v4l2_fh.m2m_ctx->ignore_cap_streaming = true;
> +				v4l2_m2m_set_dst_buffered(inst->v4l2_fh.m2m_ctx, true);
> +			}
> +		}
> +		if (inst->state == VPU_INST_STATE_SEEK)
> +			wave6_vpu_set_instance_state(inst, inst->state_in_seek);
> +	} else {
> +		fmt = &inst->dst_fmt;
> +		if (inst->state == VPU_INST_STATE_INIT_SEQ) {
> +			ret = wave6_vpu_dec_prepare_fb(inst);
> +			if (ret)
> +				goto exit;
> +		}
> +	}

[Severity: Medium]
Can wave6_vpu_dec_prepare_fb() be skipped if CAPTURE streaming starts while
inst->state is VPU_INST_STATE_SEEK?

If OUTPUT streaming is stopped while in VPU_INST_STATE_INIT_SEQ,
wave6_vpu_dec_stop_streaming() saves inst->state_in_seek =
VPU_INST_STATE_INIT_SEQ and sets inst->state = VPU_INST_STATE_SEEK.  If
userspace then starts CAPTURE streaming before restarting OUTPUT streaming,
CAPTURE start_streaming sees inst->state == VPU_INST_STATE_SEEK and skips
wave6_vpu_dec_prepare_fb(), and OUTPUT start_streaming only restores
inst->state to VPU_INST_STATE_INIT_SEQ without calling
wave6_vpu_dec_prepare_fb(), leaving decoding permanently stalled in
VPU_INST_STATE_INIT_SEQ.

Also, in wave6_vpu_dec_try_fmt_cap(), checking inst->state >=
VPU_INST_STATE_INIT_SEQ evaluates to true in VPU_INST_STATE_SEEK even when
inst->state_in_seek was VPU_INST_STATE_OPEN.

[ ... ]

> +static int wave6_vpu_dec_release(struct file *filp)
> +{
> +	struct vpu_instance *inst = wave6_file_to_vpu_inst(filp);
> +
> +	v4l2_m2m_ctx_release(inst->v4l2_fh.m2m_ctx);
> +
> +	if (inst->state != VPU_INST_STATE_NONE)
> +		wave6_vpu_dec_destroy_instance(inst);

[Severity: High]
Can releasing inst->v4l2_fh.m2m_ctx before wave6_vpu_dec_destroy_instance()
cause a use-after-free on m2m_ctx?

v4l2_m2m_ctx_release() waits only for TRANS_RUNNING to clear and then frees
m2m_ctx, whereas wave6_vpu_disable_instance() (which sets inst->enable =
false, calls synchronize_irq(), and calls cancel_delayed_work_sync() on
inst->task_timer) only runs afterward inside
wave6_vpu_dec_destroy_instance():

CPU0                              CPU1 (irq_thread / kworker)
wave6_vpu_dec_release()
                                  wave6_vpu_device_run_timeout()
                                    v4l2_m2m_job_finish()
                                      /* clears TRANS_RUNNING */
  v4l2_m2m_ctx_release()
    kfree(m2m_ctx)
                                  wave6_vpu_core_irq_thread()
                                    wave6_vpu_dec_finish_decode()
                                      ...
                                      v4l2_m2m_next_src_buf(m2m_ctx)
                                        <-- use-after-free
  wave6_vpu_dec_destroy_instance()
    wave6_vpu_disable_instance()

A similar race happens if wave6_vpu_finish_job() calls non-synchronous
cancel_delayed_work(&inst->task_timer) and clears TRANS_RUNNING while
wave6_vpu_device_run_timeout() is already running on another CPU.  Calling
wave6_vpu_disable_instance() before v4l2_m2m_ctx_release() would ensure the
IRQ thread and timeout work cannot touch m2m_ctx after it is freed.

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

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

Thread overview: 34+ 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 19:36     ` Frank Li
2026-11-19 20:07   ` Frank Li
2026-11-20  8:58     ` Nas Chung
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 20:27   ` Frank Li
2026-11-20  8:49     ` Nas Chung
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 20:37   ` Frank Li
2026-11-20  8:35     ` Nas Chung
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 [this message]
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
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 20:56   ` Frank Li
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 21:03   ` Frank Li
2026-11-20  4:34     ` Nas Chung
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
2026-11-19 21:05   ` Frank Li
2026-11-20  4:32     ` Nas Chung

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-222840@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.