All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Kyrie Wu" <kyrie.wu@mediatek.com>, devicetree@vger.kernel.org
Cc: robh@kernel.org, conor+dt@kernel.org, media-ci@linuxtv.org
Subject: Re: [PATCH v20 05/15] media: mediatek: jpeg: Fix multi-core clk suspend and resume setting
Date: Wed, 25 Nov 2026 06:38:04 +0000	[thread overview]
Message-ID: <sashiko-outbox-232719@kernel.org> (raw)
In-Reply-To: <20261125061618.2544386-6-kyrie.wu@mediatek.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] media: mediatek: jpeg: system suspend deadlock in
  v4l2_m2m_suspend()
- [Medium] media: mediatek: jpeg: uncoordinated v4l2_m2m_resume() on
  shared m2m_dev
- [Medium] media: mediatek: jpeg: race between hw_state idle and
  pm_runtime_put()
--

commit ef54e8a58e87570a3e896564d7ed5ef3de2fbbc3
Author: Kyrie Wu <kyrie.wu@mediatek.com>

media: mediatek: jpeg: Fix multi-core clk suspend and resume setting

This commit moves multi-core JPEG clock management into runtime PM
callbacks on the hardware component devices and adds system sleep
callbacks for the encoder and decoder component drivers.

> diff --git a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c
> index 3c22417f4e7c4..b9ba37a880b2d 100644
> --- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c
> +++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c
> @@ -533,13 +533,12 @@ static void mtk_jpegdec_timeout_work(struct work_struct *work)
>  	v4l2_m2m_buf_copy_metadata(src_buf, dst_buf);
>  
>  	mtk_jpeg_dec_reset(cjpeg->reg_base);
> -	clk_disable_unprepare(cjpeg->jdec_clk.clks->clk);
> -	pm_runtime_put(cjpeg->dev);
>  	cjpeg->hw_state = MTK_JPEG_HW_IDLE;
>  	atomic_inc(&master_jpeg->hw_rdy);
>  	wake_up(&master_jpeg->hw_wq);
>  	v4l2_m2m_buf_done(src_buf, buf_state);
>  	mtk_jpegdec_put_buf(cjpeg);
> +	pm_runtime_put(cjpeg->dev);
>  }
>  
>  static irqreturn_t mtk_jpegdec_hw_irq_handler(int irq, void *priv)
> @@ -547,7 +546,6 @@ static irqreturn_t mtk_jpegdec_hw_irq_handler(int irq, void *priv)
>  	struct vb2_v4l2_buffer *src_buf, *dst_buf;
>  	struct mtk_jpeg_src_buf *jpeg_src_buf;
>  	enum vb2_buffer_state buf_state;
> -	struct mtk_jpeg_ctx *ctx;
>  	u32 dec_irq_ret;
>  	u32 irq_status;
>  	int i;
> @@ -557,7 +555,6 @@ static irqreturn_t mtk_jpegdec_hw_irq_handler(int irq, void *priv)
>  
>  	cancel_delayed_work(&jpeg->job_timeout_work);
>  
> -	ctx = jpeg->hw_param.curr_ctx;
>  	src_buf = jpeg->hw_param.src_buffer;
>  	dst_buf = jpeg->hw_param.dst_buffer;
>  	v4l2_m2m_buf_copy_metadata(src_buf, dst_buf);
> @@ -580,12 +577,11 @@ static irqreturn_t mtk_jpegdec_hw_irq_handler(int irq, void *priv)
>  	buf_state = VB2_BUF_STATE_DONE;
>  	v4l2_m2m_buf_done(src_buf, buf_state);
>  	mtk_jpegdec_put_buf(jpeg);
> -	pm_runtime_put(ctx->jpeg->dev);
> -	clk_disable_unprepare(jpeg->jdec_clk.clks->clk);
>  
>  	jpeg->hw_state = MTK_JPEG_HW_IDLE;
>  	wake_up(&master_jpeg->hw_wq);
>  	atomic_inc(&master_jpeg->hw_rdy);
> +	pm_runtime_put(jpeg->dev);
>  
>  	return IRQ_HANDLED;
>  }

[Severity: Medium]
Can setting hw_state = MTK_JPEG_HW_IDLE before calling pm_runtime_put()
race with mtk_jpegdec_suspend()?

CPU0 (IRQ / timeout)              CPU1 (system suspend)
mtk_jpegdec_hw_irq_handler()
  jpeg->hw_state = MTK_JPEG_HW_IDLE;
                                  mtk_jpegdec_suspend()
                                    READ_ONCE(jpeg->hw_state) == IDLE
                                    pm_runtime_force_suspend(dev)
                                      usage_count == 2 (> 1)
                                      dev->power.needs_force_resume = 1
  pm_runtime_put(jpeg->dev)

If mtk_jpegdec_suspend() runs between the hw_state store and
pm_runtime_put(), dev->power.usage_count is still 2 (1 from
device_prepare() plus 1 from the finishing job).
pm_runtime_force_suspend() then sees usage_count > 1, skips
pm_runtime_set_suspended(), and sets needs_force_resume, causing
pm_runtime_force_resume() to unconditionally re-enable the component
clocks on resume and leave the idle device active.  The same ordering
exists in mtk_jpegenc_timeout_work() and mtk_jpegenc_hw_irq_handler().

> @@ -673,11 +669,67 @@ static int mtk_jpegdec_hw_probe(struct platform_device *pdev)
>  	return 0;
>  }
>  
> +static int mtk_jpegdec_pm_suspend(struct device *dev)
> +{
> +	struct mtk_jpegdec_comp_dev *jpeg = dev_get_drvdata(dev);
> +
> +	clk_bulk_disable_unprepare(jpeg->jdec_clk.clk_num,
> +				   jpeg->jdec_clk.clks);
> +
> +	return 0;
> +}
> +
> +static int mtk_jpegdec_pm_resume(struct device *dev)
> +{
> +	struct mtk_jpegdec_comp_dev *jpeg = dev_get_drvdata(dev);
> +
> +	return clk_bulk_prepare_enable(jpeg->jdec_clk.clk_num,
> +				       jpeg->jdec_clk.clks);
> +}
> +
> +static int mtk_jpegdec_suspend(struct device *dev)
> +{
> +	struct mtk_jpegdec_comp_dev *jpeg = dev_get_drvdata(dev);
> +	int ret;
> +
> +	/* Stop new submissions before checking hardware activity. */
> +	v4l2_m2m_suspend(jpeg->master_dev->m2m_dev);

[Severity: High]
Can v4l2_m2m_suspend() deadlock during system suspend when a multi-core
job is queued on jpeg->workqueue?

In multi-core mode, mtk_jpeg_probe() allocates jpeg->workqueue with
WQ_FREEZABLE, and mtk_jpeg_multicore_dec_device_run() only queues
ctx->jpeg_work onto jpeg->workqueue while TRANS_RUNNING remains set on
curr_ctx until mtk_jpegdec_worker() runs and calls v4l2_m2m_job_finish().

kworker                           PM suspend
mtk_jpegdec_worker() (job 1)      freeze_kernel_threads()
                                    freeze_workqueues_begin()
  v4l2_m2m_job_finish()
    schedule_work(&m2m_dev->job_work)
v4l2_m2m_device_run_work()
  v4l2_m2m_try_run() (job 2)
    curr_ctx->job_flags |= TRANS_RUNNING
    queue_work(jpeg->workqueue, &ctx->jpeg_work)
    (queued on frozen wq; cannot run)
                                  dpm_suspend()
                                    mtk_jpegdec_suspend()
                                      v4l2_m2m_suspend()
                                        wait_event(..., !TRANS_RUNNING)
                                        <-- hangs forever

Because v4l2_m2m_device_run_work() runs on the non-freezable
system_percpu_wq, a worker finishing while workqueues are freezing can
schedule the next M2M job onto the already-frozen jpeg->workqueue before
v4l2_m2m_suspend() sets QUEUE_PAUSED.  v4l2_m2m_suspend() then waits
uninterruptibly for TRANS_RUNNING to clear on a work item that will not
run until thaw_workqueues().  mtk_jpegenc_suspend() has the same issue.

> +
> +	if (READ_ONCE(jpeg->hw_state) != MTK_JPEG_HW_IDLE)
> +		ret = -EBUSY;
> +	else
> +		ret = pm_runtime_force_suspend(dev);
> +	if (ret)
> +		v4l2_m2m_resume(jpeg->master_dev->m2m_dev);
> +
> +	return ret;
> +}
> +
> +static int mtk_jpegdec_resume(struct device *dev)
> +{
> +	struct mtk_jpegdec_comp_dev *jpeg = dev_get_drvdata(dev);
> +	int ret;
> +
> +	ret = pm_runtime_force_resume(dev);
> +	if (ret < 0)
> +		return ret;
> +
> +	v4l2_m2m_resume(jpeg->master_dev->m2m_dev);
> +
> +	return 0;
> +}

[Severity: Medium]
Does calling v4l2_m2m_resume() on the shared master_dev->m2m_dev from each
component and from the parent device unpause the M2M queue while component
devices are still force-suspended?

All component devices and the parent master device share a single
master_dev->m2m_dev, where QUEUE_PAUSED is a single non-reference-counted
bit and v4l2_m2m_resume() immediately calls v4l2_m2m_try_run().

During dpm_resume(), the parent device runs mtk_jpeg_resume() before its
child component devices and calls v4l2_m2m_resume(jpeg->m2m_dev) while all
children still have runtime PM disabled by pm_runtime_force_suspend(), and
the first child component to resume does the same before its remaining
siblings have resumed.  Similarly, if one child component is idle and
enters pm_runtime_force_suspend() during dpm_suspend(), and a later
sibling is busy and returns -EBUSY, its error path calls
v4l2_m2m_resume(jpeg->master_dev->m2m_dev) while the first sibling is
still force-suspended.  Any job dispatched in that window selects an idle
force-suspended component, fails pm_runtime_resume_and_get() with -EACCES,
and completes user buffers with VB2_BUF_STATE_ERROR.  mtk_jpegenc_suspend()
and mtk_jpegenc_resume() have the same issue.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125061618.2544386-1-kyrie.wu@mediatek.com?part=5

  reply	other threads:[~2026-11-25  6:38 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  6:16 [PATCH v20 00/15] media: mediatek: jpeg: add MT8196 JPEG support Kyrie Wu
2026-11-25  6:16 ` [PATCH v20 01/15] media: mediatek: jpeg: fix jpeg cores' amounts setting Kyrie Wu
2026-11-25  6:29   ` sashiko-bot
2026-11-25  6:16 ` [PATCH v20 02/15] media: mediatek: jpeg: fix jpeg buffer payload size setting Kyrie Wu
2026-11-25  6:16 ` [PATCH v20 03/15] media: mediatek: jpeg: fix buffer structure size and layout Kyrie Wu
2026-11-25  6:16 ` [PATCH v20 04/15] media: mediatek: jpeg: Fix buffer completion on multi-core streaming stop Kyrie Wu
2026-11-25  6:16 ` [PATCH v20 05/15] media: mediatek: jpeg: Fix multi-core clk suspend and resume setting Kyrie Wu
2026-11-25  6:38   ` sashiko-bot [this message]
2026-11-25  6:16 ` [PATCH v20 06/15] media: mediatek: jpeg: fix buffer state update timing Kyrie Wu
2026-11-25  6:16 ` [PATCH v20 07/15] media: mediatek: jpeg: fix resolution change event handling in decoder Kyrie Wu
2026-11-25  6:32   ` sashiko-bot
2026-11-25  6:16 ` [PATCH v20 08/15] media: mediatek: jpeg: fix remove buffer removal timing for multi-core Kyrie Wu
2026-11-25  6:16 ` [PATCH v20 09/15] dt-bindings: mfd: syscon: Add MT8196 JPEG SMMU compatible Kyrie Wu
2026-11-25  6:21   ` sashiko-bot
2026-11-25  6:16 ` [PATCH v20 10/15] media: dt-bindings: mediatek,jpeg: Add mediatek, mt8196-jpgdec compatible Kyrie Wu
2026-11-25  6:16 ` [PATCH v20 11/15] dt-bindings: media: mediatek,jpeg: Add MT8196 decoder SMMU configuration Kyrie Wu
2026-11-25  6:25   ` sashiko-bot
2026-11-25  6:16 ` [PATCH v20 12/15] media: dt-bindings: mediatek,jpeg: Add mediatek, mt8196-jpgenc compatible Kyrie Wu
2026-11-25  6:16 ` [PATCH v20 13/15] dt-bindings: media: mediatek,jpeg: Add MT8196 encoder SMMU configuration Kyrie Wu
2026-11-25  6:16 ` [PATCH v20 14/15] media: mediatek: jpeg: add jpeg compatible Kyrie Wu
2026-11-25  6:44   ` sashiko-bot
2026-11-25  6:16 ` [PATCH v20 15/15] media: mediatek: jpeg: add jpeg smmu sid setting Kyrie Wu
2026-11-25  6:38   ` 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-232719@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=kyrie.wu@mediatek.com \
    --cc=media-ci@linuxtv.org \
    --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.