From: sashiko-bot@kernel.org
To: "Sofus Forstreuter" <sofus.c@icloud.com>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org,
media-ci@linuxtv.org
Subject: Re: [PATCH v4 06/17] media: apple: add avd driver
Date: Sat, 28 Nov 2026 22:42:59 +0000 [thread overview]
Message-ID: <sashiko-outbox-239117@kernel.org> (raw)
In-Reply-To: <20261128-avd-v4-6-35ad7d52c842@icloud.com>
Thank you for your contribution! Sashiko AI review found 9 potential
issues to consider:
- [High] media: apple: avd: use-after-free of ctx and m2m_ctx in
avd_release()
- [High] media: apple: avd: race between avd_irq_handler() and
avd_watchdog_func()
- [High] media: apple: avd: misaligned comp_start address in
avd_buf_prepare()
- [Medium] media: apple: avd: format warnings and off-by-one fw size
check in avd_probe()
- [Medium] media: apple: avd: unreachable reset code and 2s error stall
in avd_irq_handler()
- [Medium] media: apple: avd: IRQ enabled and firmware booted before
m2m_dev init in avd_probe()
- [Medium] media: apple: avd: runtime PM disabled before V4L2 cleanup in
avd_remove()
- [Low] media: apple: avd: undocumented compatible strings in
avd_of_match
- [Low] media: apple: avd: avd_init_job() zeroes peak DMA buffer size
every frame
--
commit 2e74415a1028a1a51430630c565f2d1729f1407a
Author: Sofus Forstreuter <sofus.c@icloud.com>
media: apple: add avd driver
This commit adds a V4L2 M2M stateless video decoder driver for the Apple
Video Decoder (AVD) on Apple Silicon SoCs.
> diff --git a/drivers/media/platform/apple/avd/avd-drv.c b/drivers/media/platform/apple/avd/avd-drv.c
> new file mode 100644
> index 0000000000000..8896fe3e0b44a
> --- /dev/null
> +++ b/drivers/media/platform/apple/avd/avd-drv.c
[ ... ]
> +int avd_init_job(struct avd_ctx *ctx, enum avd_codec codec, size_t segments)
> +{
> + int ret = 0;
> + struct avd_job *job = &ctx->job;
> +
> + job->codec = codec;
> + job->dest = SRAM_SEG_DST;
> + job->num = 0;
> + job->num_submit = 0;
> + job->num_alloc = segments;
> + ret = avd_buf_alloc(ctx->dev, &job->buf,
> + job->num_alloc * sizeof(*job->segments));
> + if (ret)
> + return ret;
> +
> + job->segments = job->buf.cpu;
> + memset(job->buf.cpu, 0, job->buf.size);
> + return 0;
> +}
[Severity: Low]
Does avd_init_job() need to zero the entire job->buf.size on every frame?
When a frame requests fewer segments than a previous frame, avd_buf_alloc()
reuses the existing DMA buffer and keeps job->buf.size at the historical
peak size. Zeroing only job->num_alloc * sizeof(*job->segments) would
avoid clearing unused coherent DMA memory after a frame with a high slice
or tile count.
[ ... ]
> +static void avd_watchdog_func(struct work_struct *work)
> +{
> + struct avd_dev *avd;
> + struct avd_ctx *ctx;
> + int ret;
> +
> + ctx = container_of(to_delayed_work(work), struct avd_ctx,
> + watchdog_work);
> + if (!ctx)
> + return;
> +
> + avd = ctx->dev;
> +
> + dev_err(avd->dev, "Frame processing timed out!");
> +
> + writel(0, avd->mbox + AVD_REG_MBOX_IRQ_ENABLE);
> + ret = avd_reset(avd);
> + if (ret)
> + dev_err(avd->dev, "failed to reset: %d", ret);
> +
> + avd_job_finish(ctx, VB2_BUF_STATE_ERROR);
> +}
> +
> +static irqreturn_t avd_irq_handler(int irq, void *data)
> +{
> + struct avd_dev *avd = data;
> + struct avd_ctx *ctx;
> + enum vb2_buffer_state state;
> + enum fw_msg_sts msg_sts;
> + u32 status;
> +
> + status = readl(avd->mbox + AVD_REG_MBOX0_RETRIEVE);
> + writel(AVD_MBOX0_NOT_EMPTY, avd->mbox + AVD_REG_MBOX_IRQ_CLR);
> +
> + msg_sts = FIELD_GET(FW_MSG_STS, status);
> + if (msg_sts == STS_OK) {
> + state = VB2_BUF_STATE_DONE;
> + } else if (msg_sts == STS_ERR_DEC) {
> + dev_err_ratelimited(avd->dev, "VP%02ld failed to decode",
> + FIELD_GET(FW_MSG_VAL, status));
> + state = VB2_BUF_STATE_ERROR;
> +
> + /*
> + * Resetting now will cause an SError since nothing stops the
> + * iommu from accessing the hw while its turned of.
> + *
> + * Only reset in the watchdog where there is a good chance
> + * that nothing uses the iommu.
> + */
> + return IRQ_HANDLED;
> + /* The watchdog can try again if we fail */
> + if (avd_reset(avd))
> + return IRQ_HANDLED;
[Severity: Medium]
The unconditional return IRQ_HANDLED in the STS_ERR_DEC branch leaves the
avd_reset() check unreachable and state = VB2_BUF_STATE_ERROR unused.
Because the handler returns without finishing the job or rescheduling
ctx->watchdog_work to run sooner, every frame decode error stalls the M2M
queue for the remainder of the 2000ms watchdog timeout.
> + } else {
> + dev_err(avd->dev, "Unhandled FW status: %d %#x",
> + msg_sts, status);
> + writel(0, avd->mbox + AVD_REG_MBOX_IRQ_ENABLE);
> + return IRQ_HANDLED;
> + }
> +
> + ctx = v4l2_m2m_get_curr_priv(avd->m2m_dev);
> + if (WARN_ON(!ctx))
> + return IRQ_HANDLED;
> +
> + /* if the watchdog_work has run the work has already been submitted */
> + if (cancel_delayed_work(&ctx->watchdog_work))
> + avd_job_finish(ctx, state);
> +
> + return IRQ_HANDLED;
> +}
[Severity: High]
What serializes the threaded avd_irq_handler() against avd_watchdog_func()?
Writing 0 to AVD_REG_MBOX_IRQ_ENABLE in avd_watchdog_func() does not wait
for an already-running threaded IRQ handler, so avd_irq_handler() can touch
avd->mbox while avd_reset() is resetting the hardware and cycling the IOMMU
domain. Further, once the worker thread starts avd_watchdog_func(), the
pending bit on ctx->watchdog_work is already clear:
kworker irq/mbox0
avd_watchdog_func()
avd_reset(avd)
avd_job_finish(ctx, ...)
...
v4l2_m2m_schedule_next_job()
avd_device_run()
...
avd_submit_job()
schedule_delayed_work(
&ctx->watchdog_work)
avd_irq_handler()
ctx = v4l2_m2m_get_curr_priv(...)
cancel_delayed_work(&ctx->watchdog_work)
--> returns true (cancels job 2!)
avd_job_finish(ctx, state)
The late IRQ from the first job cancels the newly armed watchdog for the
second job and finishes the second job while DMA is still active. If
instead no second job is queued and userspace is closing the fd in
avd_release(), avd_job_finish() in avd_watchdog_func() unblocks
v4l2_m2m_ctx_release() and frees ctx after avd_irq_handler() has already
fetched ctx from v4l2_m2m_get_curr_priv(), leading to a use-after-free on
cancel_delayed_work(&ctx->watchdog_work).
[ ... ]
> +static int avd_release(struct file *filp)
> +{
> + struct avd_ctx *ctx = file_to_ctx(filp);
> +
> + /*
> + * Wait for any m2m job completions. This will either cancel or
> + * complete the watchdog
> + */
> + v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
> + v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
> + v4l2_fh_del(&ctx->fh, filp);
> + v4l2_fh_exit(&ctx->fh);
> + avd_buf_free(ctx->dev, &ctx->inst);
> + avd_buf_free(ctx->dev, &ctx->histogram);
> + avd_buf_free(ctx->dev, &ctx->job.buf);
> + kfree(ctx);
> +
> + return 0;
> +}
[Severity: High]
Can avd_release() free ctx->fh.m2m_ctx and ctx while avd_watchdog_func() or
avd_irq_handler() is still finishing a job?
v4l2_m2m_ctx_release() calls v4l2_m2m_cancel_job(), which only waits until
TRANS_RUNNING is cleared in _v4l2_m2m_job_finish(), and avd_release() never
calls cancel_delayed_work_sync(&ctx->watchdog_work):
CPU0 kworker
avd_release() avd_watchdog_func()
v4l2_m2m_ctx_release(m2m_ctx) avd_job_finish(ctx, ...)
v4l2_m2m_cancel_job() ...
wait_event(m2m_ctx->finished) _v4l2_m2m_job_finish()
m2m_ctx->job_flags &= ~TRANS_RUNNING
wake_up(&m2m_ctx->finished)
...
kfree(m2m_ctx)
v4l2_m2m_schedule_next_job(..., m2m_ctx)
<-- use-after-free of m2m_ctx
kfree(ctx) ...
<-- use-after-free of ctx
Closing the video device while a job times out or completes can therefore
use m2m_ctx and ctx after they have been freed.
[ ... ]
> +/* can also be derived from a version register */
> +static const struct of_device_id avd_of_match[] = {
> + { .compatible = "apple,t8103-avd", .data = &avd_t8103_variant },
> + { .compatible = "apple,t6000-avd", .data = &avd_t6000_variant },
> + { .compatible = "apple,t8112-avd", .data = &avd_t8112_variant },
> + { .compatible = "apple,t6020-avd", .data = &avd_t6020_variant },
> + { .compatible = "apple,t8122-avd", .data = &avd_t8122_variant },
> + { .compatible = "apple,t8132-avd", .data = &avd_t8132_variant },
> + { .compatible = "apple,t8140-avd", .data = &avd_t8140_variant },
> + {},
> +};
[Severity: Low]
"apple,t8132-avd" and "apple,t8140-avd" are not documented in
Documentation/devicetree/bindings/media/apple,avd.yaml, which triggers
checkpatch.pl UNDOCUMENTED_DT_STRING warnings and dtbs_check failures.
[ ... ]
> + irq = platform_get_irq_byname(pdev, "mbox0");
> + if (irq < 0)
> + return irq;
> + ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, avd_irq_handler,
> + IRQF_ONESHOT, dev_name(&pdev->dev),
> + avd);
> + if (ret) {
> + dev_err(avd->dev, "Could not request mbox0 IRQ");
> + return ret;
> + }
[ ... ]
> + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "code");
> + if (avd->fw->size > res->end - res->start) {
> + dev_err(avd->dev, "Firmware is to large: %#lx max %#llx",
> + avd->fw->size, res->end - res->start);
> + ret = -EINVAL;
> + goto err_rel_fw;
> + }
[Severity: Medium]
Should this check use resource_size(res) instead of res->end - res->start?
struct resource uses inclusive bounds, so res->end - res->start is off by
one byte. With COMPILE_TEST enabled in Kconfig, formatting size_t
(avd->fw->size) with %#lx and resource_size_t with %#llx also causes
-Wformat warnings on 32-bit builds (%#zx and %pa withresource_size() avoid
that). In addition, nothing checks that avd->fw->size is a multiple of 4
before avd_boot() passes it to avd_memcpy_toio(), which triggers
WARN_ON(len % 4) and truncates trailing bytes.
> +
> + pm_runtime_set_autosuspend_delay(avd->dev, 100);
> + pm_runtime_use_autosuspend(avd->dev);
> + pm_runtime_enable(avd->dev);
> +
> + ret = pm_runtime_resume_and_get(avd->dev);
> + if (ret < 0)
> + goto err_disable_runtime_pm;
> +
> + if (avd->variant->revision > 3)
> + avd_version = readl_relaxed(avd->ctrl);
> + else
> + avd_version = 2 << 16;
> + piodma_version = readl_relaxed(avd->piodma + PIODMA_REG_HW_VERSION);
> +
> + dev_info(avd->dev, "HW version: %d.%d PIODMA version: %d.%d",
> + avd_version >> 16, avd_version & 0xffff,
> + piodma_version >> 16, piodma_version & 0xffff);
> +
> + pm_runtime_put(avd->dev);
> +
> + ret = avd_v4l2_init(avd);
> + if (ret)
> + goto err_disable_runtime_pm;
[Severity: Medium]
Can an mbox0 interrupt fire during avd_probe() before avd->m2m_dev is
initialized?
devm_request_threaded_irq() enables the IRQ line before pm_runtime_enable()
and before avd_v4l2_init() calls v4l2_m2m_init(). When
pm_runtime_resume_and_get() runs avd_runtime_resume() -> avd_boot(), it
enables AVD_MBOX0_NOT_EMPTY in AVD_REG_MBOX_IRQ_ENABLE and starts the CM3
processor while avd->m2m_dev is still NULL. If an interrupt arrives before
v4l2_m2m_init() completes, avd_irq_handler() passes NULL to
v4l2_m2m_get_curr_priv(avd->m2m_dev), causing a NULL pointer dereference.
[ ... ]
> +static void avd_remove(struct platform_device *pdev)
> +{
> + struct avd_dev *avd = platform_get_drvdata(pdev);
> +
> + pm_runtime_dont_use_autosuspend(avd->dev);
> + pm_runtime_disable(avd->dev);
> +
> + avd_v4l2_cleanup(avd);
> +
> + release_firmware(avd->fw);
> + iommu_domain_free(avd->empty_domain);
> +}
[Severity: Medium]
Should avd_v4l2_cleanup(avd) be called before disabling runtime PM in
avd_remove()?
Disabling runtime PM while the video and media devices are still registered
allows concurrent V4L2 operations or in-flight jobs to call runtime PM
helpers after runtime PM is disabled, and does not mirror the reverse order
of avd_probe().
> +
> +static __maybe_unused int avd_runtime_resume(struct device *dev)
> +{
> + int ret;
> + struct avd_dev *avd = platform_get_drvdata(to_platform_device(dev));
> +
> + ret = avd_boot(avd);
> + if (ret)
> + dev_err(dev, "failed to boot");
> + return ret;
> +}
> +
> +static __maybe_unused int avd_runtime_suspend(struct device *dev)
> +{
> + struct avd_dev *avd = platform_get_drvdata(to_platform_device(dev));
> +
> + avd_shutdown(avd);
> + return 0;
> +}
> +
> +static const struct dev_pm_ops avd_pm_ops = {
> + SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
> + pm_runtime_force_resume)
> + SET_RUNTIME_PM_OPS(avd_runtime_suspend, avd_runtime_resume, NULL)
> +};
[Severity: Medium]
When CONFIG_PM is disabled (for example under COMPILE_TEST),
SET_RUNTIME_PM_OPS drops avd_runtime_suspend(), leaving avd_shutdown()
unreferenced and causing a -Wunused-function warning. Using
SYSTEM_SLEEP_PM_OPS() and RUNTIME_PM_OPS() (and removing __maybe_unused)
lets the compiler dead-code eliminate avd_shutdown() cleanly alongside
pm_ptr(&avd_pm_ops).
> diff --git a/drivers/media/platform/apple/avd/avd-v4l2.c b/drivers/media/platform/apple/avd/avd-v4l2.c
> new file mode 100644
> index 0000000000000..5b085fb047d84
> --- /dev/null
> +++ b/drivers/media/platform/apple/avd/avd-v4l2.c
[ ... ]
> +static void avd_fill_decoded_pixfmt(struct avd_ctx *ctx,
> + struct v4l2_pix_format_mplane *pix_mp)
> +{
> + struct avd_comp comp = {};
> + u32 sizeimage = 0;
> +
> + v4l2_fill_pixfmt_mp(pix_mp, pix_mp->pixelformat, pix_mp->width,
> + pix_mp->height);
> + sizeimage = pix_mp->plane_fmt[0].sizeimage;
> +
> + if (is_interchange(pix_mp->pixelformat))
> + sizeimage = 0;
> + else
> + sizeimage = ALIGN(sizeimage, AVD_ALIGN);
> +
> + fill_comp(&comp, ctx->image_fmt, pix_mp->width, pix_mp->height);
> + sizeimage += ALIGN(comp.size, AVD_ALIGN);
> +
> + if (ctx->coded_fmt_desc->ops->aux_buf_size)
> + sizeimage +=
> + ALIGN(ctx->coded_fmt_desc->ops->aux_buf_size(ctx,
> + pix_mp),
> + AVD_ALIGN);
> +
> + pix_mp->plane_fmt[0].sizeimage = sizeimage;
> +}
[ ... ]
> +static int avd_buf_prepare(struct vb2_buffer *vb)
> +{
[ ... ]
> + if (!is_interchange(f->fmt.pix_mp.pixelformat)) {
> + size_t aux_size = 0;
> +
> + if (ctx->coded_fmt_desc->ops->aux_buf_size)
> + aux_size =
> + ALIGN(ctx->coded_fmt_desc->ops->aux_buf_size(ctx, &f->fmt.pix_mp),
> + AVD_ALIGN);
> +
> + buf->dma.comp_start +=
> + ALIGN(f->fmt.pix_mp.plane_fmt[0].sizeimage,
> + AVD_ALIGN) - (buf->comp.size + aux_size);
> + }
[Severity: High]
Does subtracting unaligned buf->comp.size here misalign buf->dma.comp_start?
avd_fill_decoded_pixfmt() reserves ALIGN(comp.size, AVD_ALIGN) bytes for
the compressed section, whereas calc_tile_meta() only aligns the individual
tile and metadata components in comp.size to 16 bytes (for example, at
128x128 8-bit 4:2:0, comp.size is 0x6280, which is not 256-byte aligned).
Subtracting unaligned buf->comp.size instead of ALIGN(buf->comp.size,
AVD_ALIGN) leaves buf->dma.comp_start misaligned; on IAS36 variants,
push_comp_ias36() right-shifts (dec->dma.comp_start + dec->comp.offsets[i])
by 7, truncating the lower 7 bits and shifting the compressed buffer DMA
writes into the preceding decompressed UV plane.
Also, in avd-drv.c, v4l2_format_info_bpp() computes:
8 * info->bpp[plane] / info->bpp_div[plane]
For plane 1 of 10-bit V4L2_PIX_FMT_APPLE_AX03 and V4L2_PIX_FMT_APPLE_AX23
(bpp[1] = 8, bpp_div[1] = 3), integer division yields 8 * 8 / 3 = 21 bits
per UV pixel instead of 20, inflating the UV tile size in calc_tile_meta()
from 640 bytes to 672 bytes.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261128-avd-v4-0-35ad7d52c842@icloud.com?part=6
next prev parent reply other threads:[~2026-11-28 22:43 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-28 22:02 [PATCH v4 00/17] media: apple: add avd driver Sofus Forstreuter
2026-11-28 22:02 ` [PATCH v4 01/17] dt-bindings: media: add apple,avd Sofus Forstreuter
2026-11-28 22:08 ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 02/17] media: v4l2: Add P210 pixel format Sofus Forstreuter
2026-11-28 22:02 ` [PATCH v4 03/17] media: v4l2: Add Apple interchange pixel formats Sofus Forstreuter
2026-11-28 22:02 ` [PATCH v4 04/17] media: v4l2-ctrls: validate AV1 context_update_tile_id Sofus Forstreuter
2026-11-28 22:39 ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 05/17] media: v4l2-ctrls: validate vp9 tile_rows_log2 Sofus Forstreuter
2026-11-28 22:08 ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 06/17] media: apple: add avd driver Sofus Forstreuter
2026-11-28 22:42 ` sashiko-bot [this message]
2026-11-28 22:02 ` [PATCH v4 07/17] media: apple: avd: add h264 support Sofus Forstreuter
2026-11-28 22:43 ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 08/17] media: apple: avd: add vp9 support Sofus Forstreuter
2026-11-28 22:49 ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 09/17] media: apple: avd: add hevc support Sofus Forstreuter
2026-11-28 22:46 ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 10/17] media: apple: avd: add av1 support Sofus Forstreuter
2026-11-28 22:39 ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 11/17] arm64: dts: apple: t8103: add avd nodes Sofus Forstreuter
2026-11-28 22:36 ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 12/17] arm64: dts: apple: t8112: " Sofus Forstreuter
2026-11-28 22:21 ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 13/17] arm64: dts: apple: t8122: " Sofus Forstreuter
2026-11-28 22:37 ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 14/17] arm64: dts: apple: t600x: " Sofus Forstreuter
2026-11-28 22:35 ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 15/17] arm64: dts: apple: t602x: " Sofus Forstreuter
2026-11-28 22:20 ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 16/17] arm64: dts: apple: t6030: " Sofus Forstreuter
2026-11-28 22:42 ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 17/17] arm64: dts: apple: t6031: " Sofus Forstreuter
2026-11-28 22:42 ` 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-239117@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=media-ci@linuxtv.org \
--cc=robh@kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=sofus.c@icloud.com \
/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