Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Charvi Mehta" <charvi.mehta@arm.com>, devicetree@vger.kernel.org
Cc: robh@kernel.org, conor+dt@kernel.org, media-ci@linuxtv.org
Subject: Re: [RFC PATCH 10/28] media: mali-vpu: add firmware cache infrastructure
Date: Thu, 19 Nov 2026 15:50:02 +0000	[thread overview]
Message-ID: <sashiko-outbox-223425@kernel.org> (raw)
In-Reply-To: <20261119153758.1426673-11-charvi.mehta@arm.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] media: mali-vpu: race and UAF in mali_vpu_fw_cache_cleanup()
- [Low] media: mali-vpu: %zu format mismatch in
  mali_vpu_fw_parse_header()
--

commit c4904ccc1e8387726744e3d73159243657fc5c24
Author: Charvi Mehta <charvi.mehta@arm.com>

media: mali-vpu: add firmware cache infrastructure

This commit adds a per-device firmware cache for Mali VPU firmware
binaries, loading entries asynchronously and pruning unused binaries in a
background thread.

> diff --git a/drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c b/drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c
> new file mode 100644
> index 0000000000000..746d30f179305
> --- /dev/null
> +++ b/drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c
> @@ -0,0 +1,461 @@

[ ... ]

> +static int mali_vpu_fw_parse_header(const struct firmware *fw,
> +				    struct mali_vpu_fw_bin *vpu_fw_bin)
> +{

[ ... ]

> +	if (bss_bitmap_size > ARRAY_SIZE(header->bss_bitmap) * 32) {
> +		dev_err(vpu_fw_bin->cache->dev,
> +			"invalid firmware BSS bitmap size: %u (max %zu)\n",
> +			bss_bitmap_size, ARRAY_SIZE(header->bss_bitmap) * 32UL);
> +		return -EBADMSG;
> +	}

[Severity: Low]
Does multiplying ARRAY_SIZE(header->bss_bitmap) by 32UL promote the
argument to unsigned long, causing a -Wformat warning with %zu on 32-bit
builds where size_t is unsigned int?  Multiplying by 32 instead of 32UL
preserves size_t.

[ ... ]

> +static void mali_vpu_fw_request_done(const struct firmware *fw, void *context)
> +{
> +	struct mali_vpu_fw_bin *vpu_fw_bin = context;
> +	const struct mali_vpu_fw_header *hdr;
> +	LIST_HEAD(local_event_list);
> +	int ret = 0;
> +
> +	mutex_lock(&vpu_fw_bin->cache->lock);
> +	mutex_lock(&vpu_fw_bin->lock);
> +
> +	if (!fw) {
> +		ret = -ENOENT;
> +		vpu_fw_bin->fw = NULL;
> +	} else {
> +		ret = mali_vpu_fw_parse_header(fw, vpu_fw_bin);
> +		if (ret) {
> +			release_firmware(fw);
> +			vpu_fw_bin->fw = NULL;
> +		} else {
> +			vpu_fw_bin->fw = fw;
> +		}
> +	}
> +
> +	/* We have to notify waiters in failure or success case */
> +	vpu_fw_bin->load_status = ret;
> +	vpu_fw_bin->state = ret ? MALI_VPU_FW_BIN_FAILED : MALI_VPU_FW_BIN_READY;
> +
> +	/*
> +	 * Take a local copy of events/waiters
> +	 * so that notify callback can be called outside vpu_fw_bin->lock
> +	 */
> +	list_splice_init(&vpu_fw_bin->event_list, &local_event_list);
> +
> +	if (ret) {
> +		/* Firmware load failed, remove cached entry */
> +		list_del_init(&vpu_fw_bin->cache_link);
> +		kref_put(&vpu_fw_bin->refcount, mali_vpu_fw_bin_release); /* Drop cache-ref */
> +	}
> +
> +	mutex_unlock(&vpu_fw_bin->lock);
> +	mutex_unlock(&vpu_fw_bin->cache->lock);

[ ... ]

> +void mali_vpu_fw_cache_cleanup(struct mali_vpu_fw_cache *cache)
> +{
> +	struct mali_vpu_fw_bin *bin;
> +	struct mali_vpu_fw_bin *temp;
> +	LIST_HEAD(local);
> +
> +	if (cache->cache_thread)
> +		kthread_stop(cache->cache_thread);
> +
> +	mutex_lock(&cache->lock);
> +	list_splice_init(&cache->fw_bins, &local);
> +	mutex_unlock(&cache->lock);
> +
> +	list_for_each_entry_safe(bin, temp, &local, cache_link) {
> +		list_del_init(&bin->cache_link);
> +		kref_put(&bin->refcount, mali_vpu_fw_bin_release);
> +	}
> +}

[Severity: High]
Can an in-flight request_firmware_nowait() callback race with
mali_vpu_fw_cache_cleanup() during device removal, causing list
corruption, a double kref_put() of the cache reference, and use-after-free
of both vpu_fw_bin and vpu_dev->fw_cache?

CPU0                                  CPU1 (firmware loader work)
mali_vpu_remove()
  mali_vpu_fw_cache_cleanup()
    mutex_lock(&cache->lock)
    list_splice_init(&cache->fw_bins,
                     &local)
    mutex_unlock(&cache->lock)
                                      mali_vpu_fw_request_done()
                                        mutex_lock(&bin->cache->lock)
                                        /* load fails: ret != 0 */
                                        list_del_init(&bin->cache_link)
                                          <-- races with local traversal
                                        kref_put(&bin->refcount, ...)
                                          <-- drops cache-ref
    list_for_each_entry_safe(...,
                             &local, ...)
      list_del_init(&bin->cache_link)
      kref_put(&bin->refcount, ...)
        <-- drops cache-ref a second time
  /* devres frees vpu_dev->fw_cache */
                                        dev_warn(bin->cache->dev, ...)
                                          <-- use-after-free of cache
                                        kref_put(&bin->refcount, ...)
                                          <-- use-after-free of bin

mali_vpu_fw_cache_cleanup() releases cache->lock while each bin->cache_link
is still linked into the stack-allocated local list, so a failing
mali_vpu_fw_request_done() can mutate bin->cache_link while local is being
walked unlocked.  Even if the list operations do not overlap, both
mali_vpu_fw_cache_cleanup() and the failure path in
mali_vpu_fw_request_done() unconditionally drop the single cache-owned
reference on bin->refcount, freeing vpu_fw_bin before
mali_vpu_fw_request_done() finishes logging and dropping its loader
reference.  Finally, because mali_vpu_fw_cache_cleanup() does not wait for
pending request_firmware_nowait() callbacks before mali_vpu_remove() lets
devres free vpu_dev->fw_cache, any later callback dereferences freed memory
through vpu_fw_bin->cache.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119153758.1426673-1-charvi.mehta@arm.com?part=10

  reply	other threads:[~2026-11-19 15:50 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 15:37 [RFC PATCH 00/28] media: add arm Mali-V76 video decoder driver Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 01/28] dt-bindings: media: add Arm Mali-V76 VPU Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 02/28] media: mali-vpu: add Mali VPU platform driver Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 03/28] media: mali-vpu: register V4L2 decoder device and add querycap ioctl Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 04/28] media: mali-vpu: initialize decoder format state Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 05/28] media: mali-vpu: add V4L2 mem2mem and VB2 queue support Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 06/28] media: mali-vpu: implement format query ioctls Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 07/28] media: mali-vpu: implement TRY_FMT and S_FMT ioctls Charvi Mehta
2026-11-19 15:55   ` sashiko-bot
2026-11-19 15:37 ` [RFC PATCH 08/28] media: mali-vpu: register V76 decoder controls and introduce UAPI controls Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 09/28] media: mali-vpu: add MMU software page table manager Charvi Mehta
2026-11-19 15:59   ` sashiko-bot
2026-11-19 15:37 ` [RFC PATCH 10/28] media: mali-vpu: add firmware cache infrastructure Charvi Mehta
2026-11-19 15:50   ` sashiko-bot [this message]
2026-11-19 15:37 ` [RFC PATCH 11/28] media: mali-vpu: load firmware for decoder sessions Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 12/28] media: mali-vpu: prepare firmware address space Charvi Mehta
2026-11-19 15:57   ` sashiko-bot
2026-11-19 15:37 ` [RFC PATCH 13/28] media: mali-vpu: add firmware message transport Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 14/28] media: mali-vpu: route decoder controls to firmware Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 15/28] media: mali-vpu: handle firmware RPC requests Charvi Mehta
2026-11-19 15:51   ` sashiko-bot
2026-11-19 15:37 ` [RFC PATCH 16/28] media: mali-vpu: add LSID hardware support Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 17/28] media: mali-vpu: add single session scheduler Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 18/28] media: mali-vpu: handle firmware interrupts Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 19/28] media: mali-vpu: start decoder firmware through LSID scheduling Charvi Mehta
2026-11-19 16:03   ` sashiko-bot
2026-11-19 15:37 ` [RFC PATCH 20/28] media: mali-vpu: implement vb2 buffer queue handling Charvi Mehta
2026-11-19 16:06   ` sashiko-bot
2026-11-19 15:37 ` [RFC PATCH 21/28] media: mali-vpu: handle firmware buffer exchange Charvi Mehta
2026-11-19 15:58   ` sashiko-bot
2026-11-19 15:37 ` [RFC PATCH 22/28] media: mali-vpu: handle fatal firmware error Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 23/28] media: mali-vpu: propagate buffer metadata Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 24/28] media: mali-vpu: add decoder EOS and DRC handling Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 25/28] media: mali-vpu: handle firmware buffer parameters Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 26/28] media: mali-vpu: handle firmware event responses Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 27/28] media: mali-vpu: add stateful decoder state machine Charvi Mehta
2026-11-19 15:37 ` [RFC PATCH 28/28] media: MAINTAINERS: add Arm Mali-V76 VPU driver Charvi Mehta

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-223425@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=charvi.mehta@arm.com \
    --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 \
    /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