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 09/11] media: chips-media: wave6: Add performance profiling
Date: Thu, 19 Nov 2026 09:20:41 +0000	[thread overview]
Message-ID: <sashiko-outbox-222815@kernel.org> (raw)
In-Reply-To: <5702baa6ed97eb30dec14bddba2919252ec8f3c8.1795075582.git.nas.chung@chipsnmedia.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] media: wave6: performance timestamps not re-recorded on
  OUTPUT stream restart
- [Medium] media: wave6: skipped performance reset in
  wave6_vpu_enc_stop_streaming()
- [Medium] media: wave6: wave6_vpu_cycle_to_ns() queries empty
  core->clks instead of vpu
- [Medium] media: wave6: unlocked TOCTOU race and div-by-zero in
  wave6_vpu_dbg_instance()
- [Low] media: wave6: negative setup latency in wave6_vpu_dbg_instance()
  before PIC_RUN
--

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

media: chips-media: wave6: Add performance profiling

This commit adds per-instance performance profiling to record per-frame
timestamps and firmware cycle counts, reporting frame rates and latencies
in debugfs.

> diff --git a/drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c b/drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c
> index 525e047ecebff..be1061b2b39f0 100644
> --- a/drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c
> +++ b/drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c
[ ... ]
> @@ -77,6 +83,36 @@ static int wave6_vpu_dbg_instance(struct seq_file *s, void *data)
>  		   inst->error_buf_num,
>  		   v4l2_m2m_has_stopped(inst->v4l2_fh.m2m_ctx));
>  
> +	seq_puts(s, "fps");
> +	tmp = MSEC_PER_SEC * inst->processed_buf_num;
> +	if (perf->ts_last > perf->ts_first + NSEC_PER_MSEC) {
> +		duration = ktime_ms_delta(perf->ts_last, perf->ts_first);
> +		fps = DIV64_U64_ROUND_CLOSEST(tmp, duration);
> +		seq_printf(s, " actual: %lld;", fps);
> +	}

[Severity: Medium]
Can a concurrent OUTPUT queue stop_streaming() cause a divide-by-zero in
DIV64_U64_ROUND_CLOSEST(tmp, duration)?

wave6_vpu_dbg_instance() reads inst->performance without locking while the
debugfs file is active:

CPU0                              CPU1
wave6_vpu_dbg_instance()
  if (perf->ts_last >
      perf->ts_first + NSEC_PER_MSEC)
                                  wave6_vpu_dec_stop_streaming()
                                    wave6_vpu_reset_performance()
                                      memset(&inst->performance, 0, ...)
    duration = ktime_ms_delta(
        perf->ts_last, perf->ts_first)
    DIV64_U64_ROUND_CLOSEST(
        tmp, duration)  <-- divide-by-zero when duration == 0

Unlike wave6_vpu_reset_performance(), this path does not check duration > 0
before dividing, and if only perf->ts_last is seen zeroed, duration becomes
negative and is cast to a large u64 divisor.

> +	duration = div_u64(perf->total_sw_time, NSEC_PER_MSEC);
> +	if (duration > 0) {
> +		fps = DIV64_U64_ROUND_CLOSEST(tmp, duration);
> +		seq_printf(s, " sw: %lld;", fps);
> +	}
> +	duration = div_u64(perf->total_hw_time, NSEC_PER_MSEC);
> +	if (duration > 0) {
> +		fps = DIV64_U64_ROUND_CLOSEST(tmp, duration);
> +		seq_printf(s, " hw: %lld", fps);
> +	}
> +	seq_putc(s, '\n');
> +
> +	ms[0] = div_s64_rem(perf->latency_first, NSEC_PER_MSEC, &ns[0]);
> +	ms[1] = div_s64_rem(perf->latency_max, NSEC_PER_MSEC, &ns[1]);
> +	ms[2] = div_s64_rem(ktime_sub(perf->ts_first, perf->ts_start), NSEC_PER_MSEC, &ns[2]);
> +	seq_printf(s, "latency(ms) first: %lld.%06d, max %lld.%06d, setup %lld.%06d\n",
> +		   ms[0], ns[0], ms[1], ns[1], ms[2], ns[2]);

[Severity: Low]
What happens to the setup latency output if the debugfs file is read before
the instance reaches VPU_INST_STATE_PIC_RUN?

perf->ts_start is set when the instance is created (before
wave6_vpu_create_dbgfs_file() exposes the debugfs entry in
VPU_INST_STATE_OPEN), whereas perf->ts_first remains 0 until
wave6_vpu_dec_prepare_fb() or wave6_vpu_enc_prepare_fb() transitions the
instance to VPU_INST_STATE_PIC_RUN.  Reading debugfs in VPU_INST_STATE_OPEN
or VPU_INST_STATE_INIT_SEQ passes -perf->ts_start to div_s64_rem(),
producing a negative quotient ms[2] and a negative remainder ns[2] formatted
with %lld.%06d (e.g. "setup -12345.-678900").

[ ... ]

> diff --git a/drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c b/drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c
> index 7eab283ec9e70..4fa4371545f9b 100644
> --- a/drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c
> +++ b/drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c
[ ... ]
> @@ -1670,6 +1684,7 @@ static void wave6_vpu_dec_stop_streaming(struct vb2_queue *q)
>  		goto exit;
>  
>  	if (V4L2_TYPE_IS_OUTPUT(q->type)) {
> +		wave6_vpu_reset_performance(inst);
>  		inst->queued_src_buf_num = 0;
>  		inst->processed_buf_num = 0;
>  		inst->error_buf_num = 0;

[Severity: Medium]
Does zeroing inst->performance on OUTPUT stop_streaming() leave
perf->ts_start and perf->ts_first at 0 when OUTPUT streaming is restarted?

During a decoder seek (VIDIOC_STREAMOFF followed by VIDIOC_STREAMON on the
OUTPUT queue), wave6_vpu_dec_stop_streaming() clears inst->performance and
moves inst->state to VPU_INST_STATE_SEEK.  When OUTPUT streaming restarts,
wave6_vpu_dec_start_streaming() restores inst->state directly to
inst->state_in_seek without calling wave6_vpu_record_performance_timestamps():

drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c:wave6_vpu_dec_start_streaming() {
	...
	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 (inst->state == VPU_INST_STATE_SEEK)
			wave6_vpu_set_instance_state(inst, inst->state_in_seek);
	}
	...
}

The same happens in the encoder when the OUTPUT queue is stopped and
restarted while CAPTURE remains streaming in VPU_INST_STATE_PIC_RUN.
Because perf->ts_first stays 0 while perf->ts_last updates on each decoded
or encoded frame, ktime_ms_delta(perf->ts_last, perf->ts_first) in debugfs
and wave6_vpu_reset_performance() divides by total system uptime since boot.

> diff --git a/drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c b/drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c
> index 4a0739033d1a9..ca2d4051f9adf 100644
> --- a/drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c
> +++ b/drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c
[ ... ]
> @@ -2363,6 +2375,7 @@ static void wave6_vpu_enc_stop_streaming(struct vb2_queue *q)
>  		goto exit;
>  
>  	if (V4L2_TYPE_IS_OUTPUT(q->type)) {
> +		wave6_vpu_reset_performance(inst);
>  		inst->queued_src_buf_num = 0;
>  		inst->processed_buf_num = 0;
>  		inst->error_buf_num = 0;

[Severity: Medium]
Is wave6_vpu_reset_performance() skipped whenever the encoder CAPTURE queue
stops streaming before the OUTPUT queue?

In wave6_vpu_enc_stop_streaming(), stopping the CAPTURE queue calls
wave6_vpu_enc_destroy_instance(inst, true), which sets inst->state to
VPU_INST_STATE_NONE.  When wave6_vpu_enc_stop_streaming() is then called for
the OUTPUT queue (for example via v4l2_m2m_ctx_release() on file close,
which always stops CAPTURE before OUTPUT, or via VIDIOC_STREAMOFF on CAPTURE
first), the early check:

	if (inst->state == VPU_INST_STATE_NONE)
		goto exit;

bypasses wave6_vpu_reset_performance(inst) and the counter resets.  This
skips logging the performance summary and leaves stale metrics and non-zero
ts_start / ts_first values in inst->performance if streaming is restarted on
the same file descriptor.

> diff --git a/drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c b/drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c
> index 6980b126dee55..3ec01ec867dfc 100644
> --- a/drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c
> +++ b/drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c
[ ... ]
> @@ -228,6 +231,20 @@ void wave6_vpu_set_instance_state(struct vpu_instance *inst,
>  	inst->state = state;
>  }
>  
> +u64 wave6_vpu_cycle_to_ns(struct vpu_core_device *core, u64 cycle)
> +{
> +	unsigned long rate;
> +
> +	if (!core->num_clks || !core->clks)
> +		return 0;
> +
> +	rate = clk_get_rate(core->clks[0].clk);
> +	if (!rate)
> +		return 0;
> +
> +	return div64_ul(cycle * NSEC_PER_SEC, rate);
> +}

[Severity: Medium]
Does wave6_vpu_cycle_to_ns() always return 0 because the clocks belong to
core->vpu rather than core?

In the Device Tree binding (nxp,imx95-vpu.yaml), the clocks property is on
the parent VPU node and is acquired into vpu->clks by wave6_vpu_probe(),
whereas the child interface nodes used to create the wave6-vpu-core devices
have no clocks property.  As a result, core->num_clks is 0, per-frame
hw_time and perf->total_hw_time remain 0, and hardware FPS is never
reported.

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

  reply	other threads:[~2026-11-19  9:20 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
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 [this message]
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-222815@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.