Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Kyrie Wu" <kyrie.wu@mediatek.com>
Cc: conor+dt@kernel.org, robh@kernel.org, devicetree@vger.kernel.org
Subject: Re: [PATCH v15 01/12] media: mediatek: jpeg: fix jpeg cores' amounts setting
Date: Thu, 02 Jul 2026 07:41:23 +0000	[thread overview]
Message-ID: <20260702074123.A5D401F00A3D@smtp.kernel.org> (raw)
In-Reply-To: <20260702072614.10373-2-kyrie.wu@mediatek.com>

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

New issues:
- [Critical] Unbounded max_hw_count growth on probe failure causes stack and heap buffer overflows.
- [High] Uninitialized stack memory access in worker threads upon retry.
- [High] Mismatched tracking of available hardware causes an infinite busy-loop and job failure.
- [High] Data race and missing memory barriers between child probe and worker threads cause NULL pointer dereferences.

Pre-existing issues:
- [High] Use-After-Free during device removal due to asymmetric devres workqueue teardown.
--

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

media: mediatek: jpeg: fix jpeg cores' amounts setting

This commit updates the driver to track the number of available hardware
cores dynamically using a max_hw_count variable instead of relying on a
hardcoded macro. It modifies the probe routines to increment this counter
and bounds the iterations in the worker threads based on the new variable.

> diff --git a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
> index d147ec4830811..1e014fd698ae0 100644
> --- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
> +++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c

[ ... ]

> @@ -1592,7 +1592,7 @@ static void mtk_jpegenc_worker(struct work_struct *work)
>  		jpeg_work);
>  	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
>  
> -	for (i = 0; i < MTK_JPEGENC_HW_MAX; i++)
> +	for (i = 0; i < jpeg->max_hw_count; i++)
>  		comp_jpeg[i] = jpeg->enc_hw_dev[i];
>  	i = 0;

[Severity: Critical]
Since max_hw_count can increment on probe retries (as noted below in the
probe routines), could this unbounded count exceed the fixed size of the
comp_jpeg stack array and cause a buffer overflow?

[Severity: High]
Since max_hw_count and the enc_hw_dev pointers are updated during child
probes without locks or memory barriers, could this worker thread observe
an incremented max_hw_count but read a NULL or uninitialized enc_hw_dev
pointer if the operations are reordered?

[Severity: High]
If a deferred child probe completes while waiting, and a subsequent call to
mtk_jpegenc_get_hw() returns a newly probed hw_id, does the retry loop use
an uninitialized pointer from the comp_jpeg array?

mtk_jpegenc_worker() {
    ...
retry_select:
    hw_id = mtk_jpegenc_get_hw(ctx);
    if (hw_id < 0) {
        ret = wait_event_interruptible(...);
        ...
        goto retry_select;
    }
    ...
    ret = pm_runtime_get_sync(comp_jpeg[hw_id]->dev);
}

Because the comp_jpeg array initialization loop happens before retry_select,
it is not re-run to pick up the newly assigned pointers.

[Severity: High]
Does the mismatch between hw_rdy and max_hw_count cause an infinite busy
loop when active cores are busy?

The wait condition relies on hw_rdy, which is initialized to the total
number of child nodes during the master probe:

    ret = wait_event_interruptible(jpeg->hw_wq,
                                   atomic_read(&jpeg->hw_rdy) > 0);

If a child node defers probing, max_hw_count will be less than hw_rdy.
When active cores are busy, this overcount could cause the worker to wake
up immediately and retry until it exhausts the retry limit and fails.

[Severity: High]
This is a pre-existing issue, but does device removal have a use-after-free
regression risk with the worker threads? 

During unbind, mtk_jpeg_remove() synchronously releases the m2m device:

mtk_jpeg_remove()
    ...
    v4l2_m2m_release(jpeg->m2m_dev);

However, the workqueue teardown is registered via devres and executes after
remove completes. If a worker thread is running during unbind, could it
call v4l2_m2m_job_finish() and access the already-freed m2m_dev context?

> diff --git a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c
> index b6f5b2249f1f2..2765dafab4ad4 100644
> --- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c
> +++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c
> @@ -386,6 +386,7 @@ static int mtk_jpegenc_hw_probe(struct platform_device *pdev)
>  	master_dev->enc_hw_dev[i] = dev;
>  	master_dev->reg_encbase[i] = dev->reg_base;
>  	dev->master_dev = master_dev;
> +	master_dev->max_hw_count++;
>  
>  	platform_set_drvdata(pdev, dev);
>  	pm_runtime_enable(&pdev->dev);

[Severity: Critical]
If a failure occurs later in the probe routine, such as in
mtk_jpegenc_smmu_init(), are the master_dev->max_hw_count increment and
pointer assignments rolled back? 

Without a rollback, a subsequent probe retry would increment max_hw_count
again and leak the previous hw_index, potentially overflowing counters and
corrupting adjacent memory structures.

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

  reply	other threads:[~2026-07-02  7:41 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-02  7:26 [PATCH v15 00/12] Enable jpeg enc & dec multi-hardwares for MT8196 Kyrie Wu
2026-07-02  7:26 ` [PATCH v15 01/12] media: mediatek: jpeg: fix jpeg cores' amounts setting Kyrie Wu
2026-07-02  7:41   ` sashiko-bot [this message]
2026-07-02  7:26 ` [PATCH v15 02/12] media: mediatek: jpeg: fix jpeg buffer payload size setting Kyrie Wu
2026-07-02  7:50   ` sashiko-bot
2026-07-02  7:26 ` [PATCH v15 03/12] media: mediatek: jpeg: fix buffer structure size and layout Kyrie Wu
2026-07-02  7:26 ` [PATCH v15 04/12] media: mediatek: jpeg: Fix buffer completion on multi-core streaming stop Kyrie Wu
2026-07-02  7:44   ` sashiko-bot
2026-07-02  7:26 ` [PATCH v15 05/12] media: mediatek: jpeg: Fix multi-core clk suspend and resume setting Kyrie Wu
2026-07-02  7:45   ` sashiko-bot
2026-07-02  7:26 ` [PATCH v15 06/12] media: mediatek: jpeg: fix decoding buffer number setting timing issue Kyrie Wu
2026-07-02  7:41   ` sashiko-bot
2026-07-02  7:26 ` [PATCH v15 07/12] media: mediatek: jpeg: fix resolution change event handling in decoder Kyrie Wu
2026-07-02  7:48   ` sashiko-bot
2026-07-02  7:26 ` [PATCH v15 08/12] media: mediatek: jpeg: fix remove buffer removal timing for multi-core Kyrie Wu
2026-07-02  7:54   ` sashiko-bot
2026-07-02  7:26 ` [PATCH v15 09/12] media: dt-bindings: mediatek,jpeg: Add mediatek, mt8196-jpgdec compatible Kyrie Wu
2026-07-02  7:26 ` [PATCH v15 10/12] media: dt-bindings: mediatek,jpeg: Add mediatek, mt8196-jpgenc compatible Kyrie Wu
2026-07-02  7:49   ` sashiko-bot
2026-07-02  7:26 ` [PATCH v15 11/12] media: mediatek: jpeg: add jpeg compatible Kyrie Wu
2026-07-02  7:51   ` sashiko-bot
2026-07-02  7:26 ` [PATCH v15 12/12] media: mediatek: jpeg: add jpeg smmu sid setting Kyrie Wu
2026-07-02  7:56   ` 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=20260702074123.A5D401F00A3D@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=kyrie.wu@mediatek.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