Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Kyrie Wu" <kyrie.wu@mediatek.com>
Cc: media-ci@linuxtv.org, conor+dt@kernel.org,
	devicetree@vger.kernel.org, robh@kernel.org
Subject: Re: [PATCH v18 01/13] media: mediatek: jpeg: fix jpeg cores' amounts setting
Date: Sat, 10 Oct 2026 08:16:15 +0000	[thread overview]
Message-ID: <sashiko-outbox-166327@kernel.org> (raw)
In-Reply-To: <cc21c4887ec07ac8abf68100a9512a6a121b868b.1791614327.git.kyrie.wu@mediatek.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] media: mtk-jpeg: Parent probe resets hw_rdy and hw_index after child probes
- [High] media: mtk-jpeg: Stack buffer overflow from unbounded max_hw_count
- [High] media: mtk-jpeg: NULL pointer dereference on deferred or failed child probes
--

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

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

Different ICs have different amounts of cores,
use a variable to set the cores' amounts.

> diff --git a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
> index d147ec4830811..41c4cf8dc2626 100644
> --- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
> +++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
> @@ -1325,7 +1325,8 @@ static int mtk_jpeg_probe(struct platform_device *pdev)
>  		for_each_child_of_node(pdev->dev.of_node, child)
>  			num_child++;
>  
> -		atomic_set(&jpeg->hw_rdy, num_child);
> +		jpeg->max_hw_count = num_child;

[Severity: High]
Does this code overflow the comp_jpeg[] stack arrays in mtk_jpegenc_worker()
and mtk_jpegdec_worker()?

The child node count from the device tree is assigned to max_hw_count without
verifying it fits within the driver's hardcoded limits (MTK_JPEGENC_HW_MAX or
MTK_JPEGDEC_HW_MAX).

If a malformed device tree provides more nodes than these limits, the worker
functions will perform out-of-bounds writes on their fixed-size stack arrays.

> +		atomic_set(&jpeg->hw_rdy, 0);
>  		atomic_set(&jpeg->hw_index, 0);

[Severity: High]
Is it safe to reset these atomic variables here?

Earlier in mtk_jpeg_probe(), devm_of_platform_populate() synchronously probes
available child devices. When those child probes succeed, they increment hw_rdy
and hw_index, as seen in the changes to mtk_jpegdec_hw_probe() and
mtk_jpegenc_hw_probe() below.

By unconditionally resetting hw_rdy and hw_index to 0 here, the state
established by the successfully probed children is wiped out. This leaves hw_rdy
at 0 despite having ready cores.

When workers subsequently try to consume hardware, they will decrement hw_rdy to
a negative value, causing other workers calling wait_event_interruptible() to
block indefinitely, leading to a deadlock.

Also, deferred child probes will mistakenly receive index 0, overwriting pointers
of previously probed children.

[ ... ]

> @@ -1462,7 +1463,7 @@ static int mtk_jpegenc_get_hw(struct mtk_jpeg_ctx *ctx)
>  	int i;
>  
>  	spin_lock_irqsave(&jpeg->hw_lock, flags);
> -	for (i = 0; i < MTK_JPEGENC_HW_MAX; i++) {
> +	for (i = 0; i < jpeg->max_hw_count; i++) {
>  		comp_jpeg = jpeg->enc_hw_dev[i];
>  		if (comp_jpeg->hw_state == MTK_JPEG_HW_IDLE) {

[Severity: High]
Can this dereference a NULL pointer if a child node fails to probe or is
disabled?

max_hw_count is the total number of child nodes in the device tree, but
enc_hw_dev[] is only populated when a child driver successfully probes. If a
child node has status = "disabled" or fails to probe, its corresponding slot in
enc_hw_dev[] remains NULL.

Iterating up to max_hw_count and unconditionally dereferencing comp_jpeg will
cause a NULL pointer dereference in this scenario.

>  			hw_id = i;

[ ... ]

> @@ -1509,7 +1510,7 @@ static int mtk_jpegdec_get_hw(struct mtk_jpeg_ctx *ctx)
>  	int i;
>  
>  	spin_lock_irqsave(&jpeg->hw_lock, flags);
> -	for (i = 0; i < MTK_JPEGDEC_HW_MAX; i++) {
> +	for (i = 0; i < jpeg->max_hw_count; i++) {
>  		comp_jpeg = jpeg->dec_hw_dev[i];
>  		if (comp_jpeg->hw_state == MTK_JPEG_HW_IDLE) {

[Severity: High]
Similarly, can this trigger a NULL pointer dereference if a decoder child node
fails to probe or is disabled?

>  			hw_id = i;

[ ... ]

> @@ -1592,7 +1593,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];

[Severity: High]
As mentioned earlier, does this overflow the fixed-size comp_jpeg stack array
if max_hw_count exceeds MTK_JPEGENC_HW_MAX?

Additionally, this lockless copy will cache NULL pointers if there were failed
child probes, leading to a crash later in the worker when
pm_runtime_get_sync(comp_jpeg[hw_id]->dev) is called.

>  	i = 0;

[ ... ]

> @@ -1687,7 +1688,7 @@ static void mtk_jpegdec_worker(struct work_struct *work)
>  	struct mtk_jpeg_fb fb;
>  	unsigned long flags;
>  
> -	for (i = 0; i < MTK_JPEGDEC_HW_MAX; i++)
> +	for (i = 0; i < jpeg->max_hw_count; i++)
>  		comp_jpeg[i] = jpeg->dec_hw_dev[i];

[Severity: High]
Similarly, does this overflow the comp_jpeg stack array if max_hw_count exceeds
MTK_JPEGDEC_HW_MAX, and can it cache NULL pointers?

>  	i = 0;

[ ... ]

> 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 32372781daf57..9ca68cde45a48 100644
> --- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c
> +++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c
> @@ -668,6 +668,8 @@ static int mtk_jpegdec_hw_probe(struct platform_device *pdev)
>  	platform_set_drvdata(pdev, dev);
>  	pm_runtime_enable(&pdev->dev);
>  
> +	atomic_inc(&master_dev->hw_rdy);
> +
>  	return 0;
>  }
>  
> 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 b312a15d707b2..44a064dc7b3d0 100644
> --- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c
> +++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c
> @@ -389,6 +389,8 @@ static int mtk_jpegenc_hw_probe(struct platform_device *pdev)
>  	platform_set_drvdata(pdev, dev);
>  	pm_runtime_enable(&pdev->dev);
>  
> +	atomic_inc(&master_dev->hw_rdy);
> +
>  	return 0;
>  }

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

  reply	other threads:[~2026-10-10  8:16 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-10  8:07 [PATCH v18 00/13] media: mediatek: jpeg: add MT8196 JPEG support Kyrie Wu
2026-10-10  8:07 ` [PATCH v18 01/13] media: mediatek: jpeg: fix jpeg cores' amounts setting Kyrie Wu
2026-10-10  8:16   ` sashiko-bot [this message]
2026-10-10  8:07 ` [PATCH v18 02/13] media: mediatek: jpeg: fix jpeg buffer payload size setting Kyrie Wu
2026-10-10  8:07 ` [PATCH v18 03/13] media: mediatek: jpeg: fix buffer structure size and layout Kyrie Wu
2026-10-10  8:07 ` [PATCH v18 04/13] media: mediatek: jpeg: Fix buffer completion on multi-core streaming stop Kyrie Wu
2026-10-10  8:17   ` sashiko-bot
2026-10-10  8:07 ` [PATCH v18 05/13] media: mediatek: jpeg: Fix multi-core clk suspend and resume setting Kyrie Wu
2026-10-10  8:24   ` sashiko-bot
2026-10-10  8:07 ` [PATCH v18 06/13] media: mediatek: jpeg: fix buffer state update timing Kyrie Wu
2026-10-10  8:19   ` sashiko-bot
2026-10-10  8:07 ` [PATCH v18 07/13] media: mediatek: jpeg: fix resolution change event handling in decoder Kyrie Wu
2026-10-10  8:24   ` sashiko-bot
2026-10-10  8:07 ` [PATCH v18 08/13] media: mediatek: jpeg: fix remove buffer removal timing for multi-core Kyrie Wu
2026-10-10  8:18   ` sashiko-bot
2026-10-10  8:07 ` [PATCH v18 09/13] media: dt-bindings: mediatek,jpeg: Add mediatek, mt8196-jpgdec compatible Kyrie Wu
2026-10-10  8:07 ` [PATCH v18 10/13] media: dt-bindings: mediatek,jpeg: Add mediatek, mt8196-jpgenc compatible Kyrie Wu
2026-10-10  8:18   ` sashiko-bot
2026-10-10  8:07 ` [PATCH v18 11/13] media: mediatek: jpeg: add jpeg compatible Kyrie Wu
2026-10-10  8:21   ` sashiko-bot
2026-10-10  8:07 ` [PATCH v18 12/13] dt-bindings: mediatek: Add MT8196 JPEG SMMU bindings Kyrie Wu
2026-10-10  8:07 ` [PATCH v18 13/13] media: mediatek: jpeg: add jpeg smmu sid setting Kyrie Wu
2026-10-10  8:23   ` 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-166327@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox