All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Sascha Hauer" <s.hauer@pengutronix.de>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org,
	media-ci@linuxtv.org
Subject: Re: [PATCH v8 2/4] media: rockchip: Add JPEG decoder driver
Date: Mon, 23 Nov 2026 15:56:59 +0000	[thread overview]
Message-ID: <sashiko-outbox-229627@kernel.org> (raw)
In-Reply-To: <20261123-rockchip-jpegdec-v8-2-3316cd31b043@pengutronix.de>

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] media: rockchip: rkjpegd: race and early flag clear in
  rkjpegd_stop_at_change()
- [High] media: rockchip: rkjpegd: missing vdev_lock and work sync in
  rkjpegd_suspend()
- [High] media: rockchip: rkjpegd: race between rkjpegd_watchdog() and
  rkjpegd_vdpu720_irq()
- [Medium] media: rockchip: rkjpegd: missing EOS event in
  rkjpegd_stop_streaming()
- [Medium] media: rockchip: rkjpegd: stale draining state across stream
  stop
- [Medium] media: rockchip: rkjpegd: rkjpegd_track_fmt() clobbers
  negotiated format
--

commit b2f1b48c77bd5e7b91c91e6a3d7b73159e4ded9c
Author: Sascha Hauer <s.hauer@pengutronix.de>

media: rockchip: Add JPEG decoder driver

This commit adds a V4L2 M2M stateful JPEG decoder driver for the Rockchip
VDPU720 hardware block found on SoCs such as the RK3568 and RK3588.

> diff --git a/drivers/media/platform/rockchip/rkjpegd/rkjpegd.c b/drivers/media/platform/rockchip/rkjpegd/rkjpegd.c
> new file mode 100644
> index 0000000000000..21cc08c0d4672
> --- /dev/null
> +++ b/drivers/media/platform/rockchip/rkjpegd/rkjpegd.c
[ ... ]
> +static irqreturn_t rkjpegd_vdpu720_irq(int irq, void *dev_id)
> +{
> +	struct rkjpegd_dev *jpegd = dev_id;
> +	enum vb2_buffer_state state;
> +	u32 status;
> +
> +	status = rkjpegd_read(jpegd, VDPU720_REG_INT);
> +
> +	/* First phase of the IRQ clear, see VDPU720_IRQ_CLR_KEEP. */
> +	rkjpegd_write(jpegd, status & VDPU720_IRQ_CLR_KEEP, VDPU720_REG_INT);
> +
> +	if (!(status & VDPU720_IRQ_RAW))
> +		return IRQ_NONE;
> +
> +	rkjpegd_write(jpegd, 0, VDPU720_REG_INT);
> +
> +	state = (status & VDPU720_ERR_MASK) ?
> +		VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE;
> +
> +	/*
> +	 * A block that did not park itself wedges on the next frame, and a
> +	 * clean frame can leave it unparked too, as SOFT_RST_RDY shows.  The
> +	 * reset sleeps, so leave it to rkjpegd_vdpu720_run().  The next job
> +	 * is only started after job_spinlock has released this one, which
> +	 * orders the store.
> +	 */
> +	if (state == VB2_BUF_STATE_ERROR || !(status & VDPU720_SOFT_RST_RDY))
> +		jpegd->needs_reset = true;
> +
> +	if (status & VDPU720_DEC_ERR) {
> +		u32 mcu_pos  = rkjpegd_read(jpegd, VDPU720_REG_DBG_MCU_POS);
> +		u32 err_info = rkjpegd_read(jpegd, VDPU720_REG_DBG_ERROR);
> +
> +		dev_warn_ratelimited(jpegd->dev,
> +				     "decode error: MCU pos=(%u,%u) flags=0x%04x [%s%s%s%s%s%s%s%s%s%s] first_idx=%u\n",
> +				     (u32)FIELD_GET(VDPU720_DBG_MCU_POS_X, mcu_pos),
> +				     (u32)FIELD_GET(VDPU720_DBG_MCU_POS_Y, mcu_pos),
> +				     (u32)FIELD_GET(VDPU720_DERR_FLAGS, err_info),
> +				     (err_info & VDPU720_DERR_DRI_SEQ)     ? "dri_seq "    : "",
> +				     (err_info & VDPU720_DERR_STREAM_FFFF) ? "ffff "       : "",
> +				     (err_info & VDPU720_DERR_OTHER_MARK)  ? "bad_mark "   : "",
> +				     (err_info & VDPU720_DERR_MCU_CNT_L)   ? "dri_early "  : "",
> +				     (err_info & VDPU720_DERR_MCU_CNT_M)   ? "dri_late "   : "",
> +				     (err_info & VDPU720_DERR_EOI_NO_END)  ? "eoi_early "  : "",
> +				     (err_info & VDPU720_DERR_END_NO_EOI)  ? "no_eoi "     : "",
> +				     (err_info & VDPU720_DERR_OVERFLOW)    ? "overflow "   : "",
> +				     (err_info & VDPU720_DERR_HUFF_EMPTY)  ? "huff_empty " : "",
> +				     (err_info & (VDPU720_DERR_STREAM_R0 |
> +						  VDPU720_DERR_STREAM_R1)) ? "stream_mark " : "",
> +				     (u32)FIELD_GET(VDPU720_DERR_FIRST_IDX, err_info));
> +
> +		rkjpegd_write(jpegd, err_info, VDPU720_REG_DBG_ERROR);
> +	} else if (state == VB2_BUF_STATE_ERROR) {
> +		dev_warn_ratelimited(jpegd->dev, "decode error: status 0x%08x\n",
> +				     status);
> +	}
> +
> +	/* A pending watchdog means a job is running, see rkjpegd_watchdog(). */
> +	if (cancel_delayed_work(&jpegd->watchdog_work))
> +		rkjpegd_job_finish(v4l2_m2m_get_curr_priv(jpegd->m2m_dev),
> +				   state);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +/*
> + * The block ends a stalled job itself, TIMEOUT_E raises an interrupt after
> + * 2^24 idle cycles.  This only fires for a block that stopped answering
> + * altogether, whose interrupt will not come now either.
> + */
> +static void rkjpegd_watchdog(struct work_struct *work)
> +{
> +	struct rkjpegd_dev *jpegd = container_of(to_delayed_work(work),
> +						 struct rkjpegd_dev,
> +						 watchdog_work);
> +	/* Only armed while a job runs, so there is always a context. */
> +	struct rkjpegd_ctx *ctx = v4l2_m2m_get_curr_priv(jpegd->m2m_dev);
> +
> +	dev_err(jpegd->dev, "frame processing timed out\n");
> +
> +	rkjpegd_vdpu720_reset(ctx);
> +	rkjpegd_job_finish(ctx, VB2_BUF_STATE_ERROR);
> +}

[Severity: High]
Can rkjpegd_watchdog() and rkjpegd_vdpu720_irq() race when the watchdog
expires around the same time an interrupt fires?

Once the workqueue dequeues watchdog_work, cancel_delayed_work() in
rkjpegd_vdpu720_irq() returns false without waiting for rkjpegd_watchdog(),
and nothing serializes the two paths:

CPU0 (<hardirq>)                  CPU1 (kworker)
rkjpegd_vdpu720_irq()             rkjpegd_watchdog()
  /* job 1 IRQ */                   rkjpegd_vdpu720_reset()
  dev_warn_ratelimited(...)         rkjpegd_job_finish() /* job 1 */
                                      ...
                                      rkjpegd_vdpu720_run() /* job 2 */
                                        rkjpegd_arm_watchdog()
  cancel_delayed_work(...)
    /* cancels job 2's watchdog */
  rkjpegd_job_finish()
    /* completes job 2 during DMA */

In addition, if rkjpegd_vdpu720_irq() runs while vdpu720_soft_reset() is
polling VDPU720_REG_INT, the interrupt handler writes 0 to VDPU720_REG_INT
(clearing VDPU720_SOFT_RST_EN) and sets jpegd->needs_reset = true, after
which rkjpegd_vdpu720_reset() times out and overwrites
jpegd->needs_reset = false.

> +
> +/**
> + * rkjpegd_track_fmt() - find where the capture format changes
> + * @ctx:	context the coded buffer belongs to
> + * @src_buf:	coded buffer being queued
> + *
> + * The first frame queued while the capture queue is stopped sets the capture
> + * format and reports it.  A refused one has no format, but an application
> + * waits for the event either way, so it gets it for the format as it stands.
> + * A later frame that decodes to another format is marked, and the decoder
> + * stops in front of it, see rkjpegd_stop_at_change().
> + */
> +static void rkjpegd_track_fmt(struct rkjpegd_ctx *ctx,
> +			      struct rkjpegd_src_buf *src_buf)
> +{
> +	const struct v4l2_jpeg_header *hdr = &src_buf->header;
> +	struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
> +	u32 pixelformat;
> +
> +	src_buf->fmt_change = false;
> +
> +	if (!vb2_is_streaming(v4l2_m2m_get_dst_vq(m2m_ctx)) &&
> +	    !v4l2_m2m_num_src_bufs_ready(m2m_ctx)) {
> +		if (!src_buf->parse_error) {
> +			rkjpegd_set_hdr_fmt(ctx, hdr);
> +			rkjpegd_reset_queued(ctx);
> +		}
> +		v4l2_event_queue_fh(&ctx->fh, &rkjpegd_src_change_event);
> +		return;
> +	}

[Severity: Medium]
Does this branch reset ctx->dst_fmt and ctx->convert even when
ctx->hdr_known is already true and the format has not changed?

If userspace negotiates V4L2_PIX_FMT_NV12 for a 4:2:2 stream (setting
ctx->convert = true and allocating NV12 capture buffers) and later stops
and restarts streaming when no output buffers are ready, queuing the next
4:2:2 output buffer enters this branch.  rkjpegd_set_hdr_fmt() then resets
ctx->convert = false and overwrites ctx->dst_fmt back to V4L2_PIX_FMT_NV16,
causing rkjpegd_buf_prepare() to reject the already-allocated NV12 capture
buffers with -EINVAL and queuing a spurious V4L2_EVENT_SOURCE_CHANGE.

[ ... ]

> +static void rkjpegd_stop_at_change(struct rkjpegd_ctx *ctx,
> +				   struct vb2_v4l2_buffer *dst)
> +{
> +	struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
> +
> +	/* Set before the event, so that a query after it sees the change. */
> +	WRITE_ONCE(ctx->stopped_at_change, true);
> +	v4l2_event_queue_fh(&ctx->fh, &rkjpegd_src_change_event);
> +
> +	v4l2_m2m_dst_buf_remove(m2m_ctx);
> +	rkjpegd_last_buffer_done(ctx, dst);
> +	v4l2_m2m_job_finish(ctx->dev->m2m_dev, m2m_ctx);
> +}

[Severity: High]
Can rkjpegd_stop_at_change() race with rkjpegd_decoder_cmd() when
rkjpegd_device_run() runs from m2m_dev->job_work without jpegd->vdev_lock?

When a previous frame finishes in rkjpegd_vdpu720_irq(),
v4l2_m2m_buf_done_and_job_finish() schedules m2m_dev->job_work, which
invokes rkjpegd_device_run() -> rkjpegd_stop_at_change() without holding
jpegd->vdev_lock.  As soon as stopped_at_change and
V4L2_EVENT_SOURCE_CHANGE are published, userspace can issue
V4L2_DEC_CMD_START on another CPU before rkjpegd_stop_at_change() finishes:

CPU0 (kworker)                    CPU1 (ioctl)
rkjpegd_stop_at_change()
  stopped_at_change = true
  v4l2_event_queue_fh(...)
                                  rkjpegd_decoder_cmd(V4L2_DEC_CMD_START)
                                    rkjpegd_resume_at_change()
                                      src_buf->fmt_change = false
                                      stopped_at_change = false
                                    v4l2_m2m_try_schedule()
                                      /* skipped: TRANS_RUNNING set */
  v4l2_m2m_dst_buf_remove()
  rkjpegd_last_buffer_done()      <-- completes dst as V4L2_BUF_FLAG_LAST
                                      after resume
  v4l2_m2m_job_finish()

Separately, calling vb2_clear_last_buffer_dequeued() in
rkjpegd_decoder_cmd() (or in rkjpegd_stop_streaming() on the output queue)
before userspace has dequeued that V4L2_BUF_FLAG_LAST buffer from done_list
clears last_buffer_dequeued while it is still false; when userspace later
dequeues the V4L2_BUF_FLAG_LAST buffer, vb2_dqbuf() sets
last_buffer_dequeued = true and fails all subsequent capture dequeues with
-EPIPE.

[ ... ]

> +	if (V4L2_TYPE_IS_OUTPUT(vq->type)) {
> +		/* A seek aborts a format change, decoding goes on as before. */
> +		if (ctx->stopped_at_change) {
> +			WRITE_ONCE(ctx->stopped_at_change, false);
> +			vb2_clear_last_buffer_dequeued(&ctx->fh.m2m_ctx->cap_q_ctx.q);
> +		}
> +		rkjpegd_reset_queued(ctx);
> +	} else if (ctx->stopped_at_change) {
> +		/*
> +		 * Part of the format change, which does not abort a drain:
> +		 * dev-decoder.rst has the drain go on once it is handled.
> +		 */
> +		rkjpegd_resume_at_change(ctx);
> +		return;
> +	}

[Severity: Medium]
Can returning early here without calling
v4l2_m2m_update_stop_streaming_state() leave m2m_ctx->is_draining and
m2m_ctx->next_buf_last stuck at true when both queues are stopped?

If a drain is active when the decoder stops at a format change and
userspace stops the capture queue first, this branch clears
ctx->stopped_at_change and skips v4l2_m2m_update_stop_streaming_state(),
leaving m2m_ctx->is_draining set while the capture queue is not streaming
(the same state can be reached if V4L2_DEC_CMD_STOP is called in
rkjpegd_decoder_cmd() while the capture queue is not streaming).  If
userspace then stops the output queue as well,
v4l2_m2m_update_stop_streaming_state() on the output queue sees
is_draining == true with no ready capture buffer and sets
m2m_ctx->next_buf_last = true.  When streaming is later restarted, the
first capture buffer queued in rkjpegd_buf_queue() hits
v4l2_m2m_dst_buf_is_last() and terminates the new stream immediately.

> +
> +	v4l2_m2m_update_stop_streaming_state(ctx->fh.m2m_ctx, vq);
> +}

[Severity: Medium]
Should rkjpegd_stop_streaming() queue rkjpegd_eos_event when
v4l2_m2m_update_stop_streaming_state() marks the decoder stopped?

When the output queue stops streaming while a drain is in progress and a
capture buffer is already on rdy_queue,
v4l2_m2m_update_stop_streaming_state() completes that capture buffer with
V4L2_BUF_FLAG_LAST and sets m2m_ctx->has_stopped = true, so
rkjpegd_buf_queue() will not run later to queue V4L2_EVENT_EOS.

[ ... ]

> +/* pm_runtime_force_suspend() ignores the usage count, let the job finish. */
> +static int rkjpegd_suspend(struct device *dev)
> +{
> +	struct rkjpegd_dev *jpegd = dev_get_drvdata(dev);
> +
> +	v4l2_m2m_suspend(jpegd->m2m_dev);
> +
> +	return pm_runtime_force_suspend(dev);
> +}

[Severity: High]
Does rkjpegd_suspend() need jpegd->vdev_lock around v4l2_m2m_suspend() and
cancel_delayed_work_sync(&jpegd->watchdog_work) before
pm_runtime_force_suspend(), as rkjpegd_vdev_unregister() does?

v4l2_m2m_suspend() reads m2m_dev->curr_ctx under job_spinlock, drops the
spinlock, and waits on curr_ctx->finished.  Without jpegd->vdev_lock, a
concurrent rkjpegd_release() (for example from delayed_fput_work) can free
m2m_ctx while v4l2_m2m_suspend() is about to wait on it:

CPU0                              CPU1
rkjpegd_suspend()                 rkjpegd_release()
  v4l2_m2m_suspend()
    curr_ctx = m2m_dev->curr_ctx
                                    mutex_lock(&jpegd->vdev_lock)
                                    v4l2_m2m_ctx_release(m2m_ctx)
                                      kfree(m2m_ctx)
    wait_event(curr_ctx->finished, ...)
                                  <-- use-after-free

Without cancel_delayed_work_sync(&jpegd->watchdog_work), an in-flight
rkjpegd_watchdog() can also touch device registers after
pm_runtime_force_suspend() disables the clocks.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261123-rockchip-jpegdec-v8-0-3316cd31b043@pengutronix.de?part=2

  reply	other threads:[~2026-11-23 15:57 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 15:25 [PATCH v8 0/4] media: rockchip: Add JPEG decoder driver Sascha Hauer
2026-11-23 15:25 ` Sascha Hauer
2026-11-23 15:25 ` [PATCH v8 1/4] media: dt-bindings: Add Rockchip JPEG decoder Sascha Hauer
2026-11-23 15:25   ` Sascha Hauer
2026-11-23 15:30   ` sashiko-bot
2026-11-23 15:25 ` [PATCH v8 2/4] media: rockchip: Add JPEG decoder driver Sascha Hauer
2026-11-23 15:25   ` Sascha Hauer
2026-11-23 15:56   ` sashiko-bot [this message]
2026-11-23 15:25 ` [PATCH v8 3/4] arm64: dts: rockchip: rk3588: Add JPEG decoder node Sascha Hauer
2026-11-23 15:25   ` Sascha Hauer
2026-11-23 15:25 ` [PATCH v8 4/4] arm64: dts: rockchip: rk356x: " Sascha Hauer
2026-11-23 15:25   ` Sascha Hauer

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-229627@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=media-ci@linuxtv.org \
    --cc=robh@kernel.org \
    --cc=s.hauer@pengutronix.de \
    --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.