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 v19 01/13] media: mediatek: jpeg: fix jpeg cores' amounts setting
Date: Mon, 16 Nov 2026 01:48:03 +0000	[thread overview]
Message-ID: <sashiko-outbox-216010@kernel.org> (raw)
In-Reply-To: <20261116013402.1273016-2-kyrie.wu@mediatek.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] media: mediatek: jpeg: NULL dereference when max_hw_count
  exceeds probed cores
- [Medium] media: mediatek: jpeg: out-of-bounds access from unbounded
  max_hw_count
--

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

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

This commit replaces the fixed MTK_JPEGENC_HW_MAX and MTK_JPEGDEC_HW_MAX loop
bounds with a dynamic max_hw_count derived from the number of child device
tree nodes, and increments hw_rdy as each child hardware device probes.

> diff --git a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
> index e01dd4d80722c..c477f1a4dd178 100644
> --- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
> +++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
> @@ -1302,6 +1302,7 @@ static int mtk_jpeg_probe(struct platform_device *pdev)
>  
>  	mutex_init(&jpeg->lock);
>  	spin_lock_init(&jpeg->hw_lock);
> +	atomic_set(&jpeg->hw_rdy, 0);
>  	jpeg->dev = &pdev->dev;
>  	jpeg->variant = of_device_get_match_data(jpeg->dev);
>  
> @@ -1325,7 +1326,7 @@ 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;
>  		atomic_set(&jpeg->hw_index, 0);
>  
>  		jpeg->workqueue = alloc_ordered_workqueue(MTK_JPEG_NAME,

[Severity: Medium]
Can jpeg->max_hw_count overflow the fixed-size enc_hw_dev[], dec_hw_dev[] and
worker comp_jpeg[] arrays?

struct mtk_jpeg_dev and the workers size those arrays to MTK_JPEGENC_HW_MAX
(2) and MTK_JPEGDEC_HW_MAX (3), whereas mtk_jpeg_probe() sets
jpeg->max_hw_count directly to num_child without clamping or validating it
against those limits.  If a device tree node has more children than the array
capacity, the loops in mtk_jpegenc_get_hw(), mtk_jpegdec_get_hw(),
mtk_jpegenc_worker() and mtk_jpegdec_worker() will read past enc_hw_dev[] or
dec_hw_dev[] and overflow the stack-allocated comp_jpeg[] array.

> @@ -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) {
>  			hw_id = i;

[Severity: High]
Can mtk_jpegenc_get_hw() and mtk_jpegdec_get_hw() dereference a NULL
comp_jpeg pointer when jpeg->max_hw_count exceeds the number of probed child
cores?

mtk_jpeg_probe() counts all child nodes with for_each_child_of_node() and
registers the video device immediately, while enc_hw_dev[i], dec_hw_dev[i]
and hw_rdy are only populated as each child finishes mtk_jpegenc_hw_probe()
or mtk_jpegdec_hw_probe().  If any child node is disabled in DT, defers or
fails probe, or has not probed yet when userspace queues a job,
mtk_jpegenc_worker() and mtk_jpegdec_worker() call mtk_jpegenc_get_hw() or
mtk_jpegdec_get_hw() before waiting on hw_rdy > 0 and dereference
comp_jpeg->hw_state on an unpopulated NULL slot.

In addition, both workers snapshot enc_hw_dev[] or dec_hw_dev[] into a local
comp_jpeg[] array before the retry_select wait loop; if a child probes while
the worker is waiting on hw_wq, the worker will later dereference a stale
NULL comp_jpeg[hw_id] when calling pm_runtime_resume_and_get().

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

  reply	other threads:[~2026-11-16  1:48 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16  1:33 [PATCH v19 00/13] media: mediatek: jpeg: add MT8196 JPEG support Kyrie Wu
2026-11-16  1:33 ` [PATCH v19 01/13] media: mediatek: jpeg: fix jpeg cores' amounts setting Kyrie Wu
2026-11-16  1:48   ` sashiko-bot [this message]
2026-11-16  1:33 ` [PATCH v19 02/13] media: mediatek: jpeg: fix jpeg buffer payload size setting Kyrie Wu
2026-11-16  1:33 ` [PATCH v19 03/13] media: mediatek: jpeg: fix buffer structure size and layout Kyrie Wu
2026-11-16  1:33 ` [PATCH v19 04/13] media: mediatek: jpeg: Fix buffer completion on multi-core streaming stop Kyrie Wu
2026-11-16  1:33 ` [PATCH v19 05/13] media: mediatek: jpeg: Fix multi-core clk suspend and resume setting Kyrie Wu
2026-11-16  2:03   ` sashiko-bot
2026-11-17 11:32   ` AngeloGioacchino Del Regno
2026-11-16  1:33 ` [PATCH v19 06/13] media: mediatek: jpeg: fix buffer state update timing Kyrie Wu
2026-11-16  1:52   ` sashiko-bot
2026-11-16  1:33 ` [PATCH v19 07/13] media: mediatek: jpeg: fix resolution change event handling in decoder Kyrie Wu
2026-11-16  1:55   ` sashiko-bot
2026-11-16  1:33 ` [PATCH v19 08/13] media: mediatek: jpeg: fix remove buffer removal timing for multi-core Kyrie Wu
2026-11-16  1:33 ` [PATCH v19 09/13] dt-bindings: soc: mediatek: Add MT8196 JPEG SMMU Kyrie Wu
2026-11-16  1:52   ` sashiko-bot
2026-11-16 16:33   ` Rob Herring
2026-11-16  1:33 ` [PATCH v19 10/13] media: dt-bindings: mediatek,jpeg: Add mediatek, mt8196-jpgdec compatible Kyrie Wu
2026-11-16  1:33 ` [PATCH v19 11/13] media: dt-bindings: mediatek,jpeg: Add mediatek, mt8196-jpgenc compatible Kyrie Wu
2026-11-16  1:33 ` [PATCH v19 12/13] media: mediatek: jpeg: add jpeg compatible Kyrie Wu
2026-11-16  2:04   ` sashiko-bot
2026-11-16  1:33 ` [PATCH v19 13/13] media: mediatek: jpeg: add jpeg smmu sid setting Kyrie Wu
2026-11-16  2:04   ` 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-216010@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.