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 01/15] media: mediatek: jpeg: fix jpeg cores' amounts setting
Date: Wed, 25 Nov 2026 06:29:50 +0000 [thread overview]
Message-ID: <sashiko-outbox-232700@kernel.org> (raw)
In-Reply-To: <20261125061618.2544386-2-kyrie.wu@mediatek.com>
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] media: mediatek: jpeg: child probe in mtk_jpeg_probe() before
bounds check
- [High] media: mediatek: jpeg: NULL deref in get_hw() when max_hw_count
> probed
--
commit cfc2baea3485ec02d134ce90cc368bfa48a69866
Author: Kyrie Wu <kyrie.wu@mediatek.com>
media: mediatek: jpeg: fix jpeg cores' amounts setting
This commit tracks the number of JPEG hardware core child nodes in
jpeg->max_hw_count instead of iterating over the fixed MTK_JPEGENC_HW_MAX
and MTK_JPEGDEC_HW_MAX limits, and increments jpeg->hw_rdy as each child
core 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..3caee184ff5ed 100644
> --- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
> +++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
> @@ -1294,6 +1294,7 @@ static int mtk_jpeg_probe(struct platform_device *pdev)
> struct mtk_jpeg_dev *jpeg;
> struct device_node *child;
> int num_child = 0;
> + int hw_max;
> int ret;
>
> jpeg = devm_kzalloc_obj(&pdev->dev, *jpeg);
> @@ -1302,6 +1303,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 +1327,14 @@ 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);
> + hw_max = jpeg->variant->cap_q_default_fourcc == V4L2_PIX_FMT_JPEG ?
> + MTK_JPEGENC_HW_MAX : MTK_JPEGDEC_HW_MAX;
> + if (num_child > hw_max)
> + return dev_err_probe(jpeg->dev, -EINVAL,
> + "Too many JPEG cores: %d (max %d)\n",
> + num_child, hw_max);
> +
> + jpeg->max_hw_count = num_child;
> atomic_set(&jpeg->hw_index, 0);
>
> jpeg->workqueue = alloc_ordered_workqueue(MTK_JPEG_NAME,
[Severity: High]
Does the num_child > hw_max check and atomic_set(&jpeg->hw_index, 0) run
too late when devm_of_platform_populate() is called earlier in
mtk_jpeg_probe()?
When the child driver is already registered (such as when built as a
module, since mtk_jpeg.ko depends on mtk-jpeg-enc-hw.ko and
mtk-jpeg-dec-hw.ko), devm_of_platform_populate() synchronously runs
mtk_jpegenc_hw_probe() or mtk_jpegdec_hw_probe() before reaching the
multi_core block:
drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c:mtk_jpegdec_hw_probe() {
...
i = atomic_add_return(1, &master_dev->hw_index) - 1;
master_dev->dec_hw_dev[i] = dev;
master_dev->reg_decbase[i] = dev->reg_base;
...
}
If num_child > hw_max, the child probes write out of bounds into
dec_hw_dev[] or enc_hw_dev[] before the bounds check in mtk_jpeg_probe()
is reached. In addition, resetting jpeg->hw_index to 0 after
devm_of_platform_populate() means that if one child probes synchronously
(taking slot 0) and a second child defers probe, the deferred child will
also receive index 0 when it later probes, overwriting slot 0 and leaving
slot 1 NULL.
> @@ -1462,7 +1471,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;
> @@ -1509,7 +1518,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) {
> hw_id = i;
> @@ -1592,7 +1601,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;
>
> @@ -1688,7 +1697,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];
> i = 0;
>
[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?
jpeg->max_hw_count is set from for_each_child_of_node(), which counts all
DT child nodes including disabled ones, whereas enc_hw_dev[i] and
dec_hw_dev[i] are only populated as each child finishes probing. If a
child node is disabled, fails or defers probe, or has not bound yet when a
V4L2 job runs, mtk_jpegenc_worker() and mtk_jpegdec_worker() call
mtk_jpegenc_get_hw() / mtk_jpegdec_get_hw() before waiting on
jpeg->hw_rdy, and iterating up to jpeg->max_hw_count hits a NULL
enc_hw_dev[i] or dec_hw_dev[i] and dereferences comp_jpeg->hw_state.
Even if a child finishes probing while the worker is waiting on
jpeg->hw_wq, comp_jpeg[] in the worker is snapshotted before the
retry_select loop, so comp_jpeg[hw_id] remains NULL and crashes at
pm_runtime_resume_and_get(comp_jpeg[hw_id]->dev).
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125061618.2544386-1-kyrie.wu@mediatek.com?part=1
next prev parent reply other threads:[~2026-11-25 6:29 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 [this message]
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
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-232700@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