From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795905781; b=tEm/9MEveTnAO1HTywqe4sahU2N1+Einxm+LuK5S4Ue8e4DC3jPng6Qfsta5hykr2RLu TMNvpEIJHfSVgBBV9PbFLLtU13aPfcOg/lH4gC1hAv9j4A1fzi4kFXtXFPBNTbUp3GpcA q40424TJ/OSvRmpW1MX+EM2Ao3IA0uUMvBHvOD8G1NpNt0obdiVNXT/LGWiIOZASJvStC Guz7G8vO1mmq1f2T0I9vqvrIuckk/cWJtHmXsHx0afbgc11D+y7KdVcrODgGVxcXIHooy 9ma2vJxlXktTew0qtJkZCPRsRU8iRDs4Vx3oEJIym/bs2hgz+qjMhBddyJZJXO3K5eg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795905781; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=BHrHAEPIFZPF7/T9VXFVO3JAIiojSqJ5FbT+w/MA4r8=; b=Zcg9fxpeSIqxSv48gz5W5MG47W5gq0YFAE7Y/bmg6hM+qLh62ce0RjSvmxpxej8V9MzE RGqx3wR0HSgrmnTD3dt/NmH8MnhrzwPuEhrG3LQOsjIbSE92sz0q1d8tQheF98WNk6Z8/ GIzrhkEvxZGueG8AJiEEi/X0o99sup9t8gYhntAD+6ReSaYwYqKTcw7hJyMEIsCoqNlM/ MxYzH/k4ncqkXea6/6CG7OKxuy0UlysRXU1Pf+zm6iau/0NtK4v2vJbimNr0EhiP5BlsX leTMGP4VbNI+WF5oDwHH+l3IyduM1z1+ek96t2jxfwu1gY01NO1SJKNOmq7Y6a2up1w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=GDT8xAgd; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 18C5D606A31C for ; Sat, 28 Nov 2026 22:43:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=GDT8xAgd Received: by smtp.kernel.org (Postfix) with ESMTPSA id 529B01F00898; Sat, 28 Nov 2026 22:43:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795905780; bh=BHrHAEPIFZPF7/T9VXFVO3JAIiojSqJ5FbT+w/MA4r8=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=GDT8xAgdQYzvJt9zDy4TRxxYqik6+B5Cr/BHjUzMJW6EjlQXfc9x6OfMKpmInkQl0 D35n1Xvd0Bu3UBa0xd5pIJqIrqvvYVJTLXUIw3SZkN9VIeZ+aBbqiqvaCROMW+dP46 LXc4nZcrz+j3/AuX/rNBuJR1BHySBaBOnciN/QjKVWcgoQwhUNLiLUvVSdJSHVgVX9 XSmfUHpPNcO0QFzH7/XfMWsfu8HV3S355EQVppXV8W/GmDt5jwA5uaz3CKL23f8VBY uC0gRvbGKb624X9e58tIusrcshf+GWdHheEZ48gmAf+DJCi8IXPvVbIe9nzXmtks0/ kLP3rPr2Ak0Cw== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v4 06/17] media: apple: add avd driver Reply-To: sashiko-reviews@lists.linux.dev To: "Sofus Forstreuter" Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org, media-ci@linuxtv.org In-Reply-To: <20261128-avd-v4-6-35ad7d52c842@icloud.com> References: <20261128-avd-v4-0-35ad7d52c842@icloud.com> <20261128-avd-v4-6-35ad7d52c842@icloud.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Sat, 28 Nov 2026 22:42:59 +0000 X-Mailing-List: devicetree@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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/p= latform/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 segme= nts) > +{ > + int ret =3D 0; > + struct avd_job *job =3D &ctx->job; > + > + job->codec =3D codec; > + job->dest =3D SRAM_SEG_DST; > + job->num =3D 0; > + job->num_submit =3D 0; > + job->num_alloc =3D segments; > + ret =3D avd_buf_alloc(ctx->dev, &job->buf, > + job->num_alloc * sizeof(*job->segments)); > + if (ret) > + return ret; > + > + job->segments =3D 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 =3D container_of(to_delayed_work(work), struct avd_ctx, > + watchdog_work); > + if (!ctx) > + return; > + > + avd =3D ctx->dev; > + > + dev_err(avd->dev, "Frame processing timed out!"); > + > + writel(0, avd->mbox + AVD_REG_MBOX_IRQ_ENABLE); > + ret =3D 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 =3D data; > + struct avd_ctx *ctx; > + enum vb2_buffer_state state; > + enum fw_msg_sts msg_sts; > + u32 status; > + > + status =3D readl(avd->mbox + AVD_REG_MBOX0_RETRIEVE); > + writel(AVD_MBOX0_NOT_EMPTY, avd->mbox + AVD_REG_MBOX_IRQ_CLR); > + > + msg_sts =3D FIELD_GET(FW_MSG_STS, status); > + if (msg_sts =3D=3D STS_OK) { > + state =3D VB2_BUF_STATE_DONE; > + } else if (msg_sts =3D=3D STS_ERR_DEC) { > + dev_err_ratelimited(avd->dev, "VP%02ld failed to decode", > + FIELD_GET(FW_MSG_VAL, status)); > + state =3D 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 =3D 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 =3D 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 =3D 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 =3D 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 &=3D ~TRANS_RUNN= ING wake_up(&m2m_ctx->finished) ... kfree(m2m_ctx) v4l2_m2m_schedule_next_job(..., m2m_c= tx) <-- 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[] =3D { > + { .compatible =3D "apple,t8103-avd", .data =3D &avd_t8103_variant }, > + { .compatible =3D "apple,t6000-avd", .data =3D &avd_t6000_variant }, > + { .compatible =3D "apple,t8112-avd", .data =3D &avd_t8112_variant }, > + { .compatible =3D "apple,t6020-avd", .data =3D &avd_t6020_variant }, > + { .compatible =3D "apple,t8122-avd", .data =3D &avd_t8122_variant }, > + { .compatible =3D "apple,t8132-avd", .data =3D &avd_t8132_variant }, > + { .compatible =3D "apple,t8140-avd", .data =3D &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 =3D platform_get_irq_byname(pdev, "mbox0"); > + if (irq < 0) > + return irq; > + ret =3D devm_request_threaded_irq(&pdev->dev, irq, NULL, avd_irq_handle= r, > + IRQF_ONESHOT, dev_name(&pdev->dev), > + avd); > + if (ret) { > + dev_err(avd->dev, "Could not request mbox0 IRQ"); > + return ret; > + } [ ... ] > + res =3D 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 =3D -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 =3D pm_runtime_resume_and_get(avd->dev); > + if (ret < 0) > + goto err_disable_runtime_pm; > + > + if (avd->variant->revision > 3) > + avd_version =3D readl_relaxed(avd->ctrl); > + else > + avd_version =3D 2 << 16; > + piodma_version =3D 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 =3D 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 =3D 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 =3D platform_get_drvdata(to_platform_device(dev)); > + > + ret =3D 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 =3D platform_get_drvdata(to_platform_device(dev)); > + > + avd_shutdown(avd); > + return 0; > +} > + > +static const struct dev_pm_ops avd_pm_ops =3D { > + 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 =3D {}; > + u32 sizeimage =3D 0; > + > + v4l2_fill_pixfmt_mp(pix_mp, pix_mp->pixelformat, pix_mp->width, > + pix_mp->height); > + sizeimage =3D pix_mp->plane_fmt[0].sizeimage; > + > + if (is_interchange(pix_mp->pixelformat)) > + sizeimage =3D 0; > + else > + sizeimage =3D ALIGN(sizeimage, AVD_ALIGN); > + > + fill_comp(&comp, ctx->image_fmt, pix_mp->width, pix_mp->height); > + sizeimage +=3D ALIGN(comp.size, AVD_ALIGN); > + > + if (ctx->coded_fmt_desc->ops->aux_buf_size) > + sizeimage +=3D > + ALIGN(ctx->coded_fmt_desc->ops->aux_buf_size(ctx, > + pix_mp), > + AVD_ALIGN); > + > + pix_mp->plane_fmt[0].sizeimage =3D sizeimage; > +} [ ... ] > +static int avd_buf_prepare(struct vb2_buffer *vb) > +{ [ ... ] > + if (!is_interchange(f->fmt.pix_mp.pixelformat)) { > + size_t aux_size =3D 0; > + > + if (ctx->coded_fmt_desc->ops->aux_buf_size) > + aux_size =3D > + ALIGN(ctx->coded_fmt_desc->ops->aux_buf_size(ctx, &f->fmt.pix_mp), > + AVD_ALIGN); > + > + buf->dma.comp_start +=3D > + 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] =3D 8, bpp_div[1] =3D 3), integer division yields 8 * 8 / 3 =3D 21 = bits per UV pixel instead of 20, inflating the UV tile size in calc_tile_meta() from 640 bytes to 672 bytes. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261128-avd-v4-0-3= 5ad7d52c842@icloud.com?part=3D6