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 15/28] media: mali-vpu: handle firmware RPC requests
Date: Thu, 19 Nov 2026 15:51:04 +0000	[thread overview]
Message-ID: <sashiko-outbox-223429@kernel.org> (raw)
In-Reply-To: <20261119153758.1426673-16-charvi.mehta@arm.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] media: mali-vpu: out-of-bounds VA mapping in
  mali_vpu_rpc_mem_alloc()
- [Medium] media: mali-vpu: stale RPC return payload in
  mali_vpu_fw_handle_rpc()
- [Low] media: mali-vpu: log2_alignment ignored in
  mali_vpu_rpc_mem_alloc()
--

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

media: mali-vpu: handle firmware RPC requests

This commit adds handling for firmware RPC requests covering debug print
output and firmware memory allocation, resizing, and freeing, tracking
allocated pages by virtual address in a hash table.

> diff --git a/drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c b/drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c
> index 91a989c098cbc..c11d5c6c0ec5f 100644
> --- a/drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c
> +++ b/drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c

[ ... ]

> @@ -908,3 +929,259 @@ int mali_vpu_fw_parse_msg(struct mali_vpu_fw *fw, struct mali_vpu_fw_msg *msg)

[ ... ]

> +static void mali_vpu_rpc_mem_alloc(struct mali_vpu_fw *fw,
> +				   struct mve_rpc_communication_area *rpc_area)
> +{
> +	union mve_rpc_params *p = &rpc_area->params;
> +	enum mali_vpu_fw_region region;
> +	struct mali_vpu_mmu_pages *pages;
> +	size_t npages, max_pages;
> +	mali_vpu_mmu_va end, begin = 0;
> +	int ret;
> +
> +	if (!p->mem_alloc.size ||
> +	    p->mem_alloc.size > p->mem_alloc.max_size) {
> +		dev_warn(fw->dev,
> +			 "invalid RPC allocation:size=%u max_size=%u alignment=%u\n",
> +			 p->mem_alloc.size, p->mem_alloc.max_size,
> +			 p->mem_alloc.log2_alignment);
> +		goto err;
> +	}

[Severity: Low]
Should mali_vpu_rpc_mem_alloc() validate or apply
p->mem_alloc.log2_alignment?

union mve_rpc_params documents that the allocation must be placed on at
least a 2^(log2_alignment) boundary, and the warning above logs
log2_alignment, but the placement loop always steps candidate addresses by
MALI_VPU_RPC_VA_GRANULE (SZ_16M) without rejecting or honouring larger
alignments.

> +
> +	switch (p->mem_alloc.region) {
> +	case MVE_MEM_REGION_PROTECTED:
> +		region = MALI_VPU_FW_REGION_PROTECTED;
> +		break;
> +	case MVE_MEM_REGION_OUTBUF:
> +		region = MALI_VPU_FW_REGION_FRAMEBUF;
> +		break;
> +	default:
> +		dev_err(fw->dev,
> +			"unsupported RPC mem alloc region. region=%u\n",
> +			p->mem_alloc.region);
> +		goto err;
> +	}
> +
> +	ret = mali_vpu_fw_get_region(region, &begin, &end);
> +	if (ret)
> +		goto err;
> +
> +	npages = DIV_ROUND_UP(p->mem_alloc.size, MALI_VPU_PAGE_SIZE);
> +	max_pages = DIV_ROUND_UP(p->mem_alloc.max_size, MALI_VPU_PAGE_SIZE);
> +
> +	pages = mali_vpu_mmu_alloc_pages(fw->dev, npages, max_pages);
> +	if (IS_ERR(pages)) {
> +		begin = 0;
> +		goto err;
> +	}
> +
> +	ret = -EINVAL;
> +	while (begin < end) {
> +		ret = mali_vpu_mmu_map_pages(fw->mmu, begin, pages,
> +					     MALI_VPU_ATTR_SHARED_RW,
> +					     MALI_VPU_ACCESS_READ_WRITE);
> +		if (!ret)
> +			break;
> +
> +		begin += MALI_VPU_RPC_VA_GRANULE;
> +	}

[Severity: Medium]
Can this loop map pages past the end of the target region?

mali_vpu_mmu_map_pages() maps and reserves pages->capacity pages starting
at begin, but the loop only checks begin < end without verifying that the
full reserved range fits within end - begin.  When earlier 16 MiB granules
are occupied and begin advances near end (or when max_size exceeds the
region size), the mapping spills past end into the adjacent address
region.

In addition, p->mem_alloc.max_size and p->mem_resize.new_size in
mali_vpu_rpc_mem_resize() are passed directly to DIV_ROUND_UP(...,
MALI_VPU_PAGE_SIZE) without an upper bound check.  On 32-bit kernels
MALI_VPU_PAGE_SIZE is a 32-bit unsigned long, so values above 0xfffff000
wrap DIV_ROUND_UP() to 0 (causing mali_vpu_rpc_mem_resize() to shrink the
allocation to 0 pages while returning pages->va as a success), and on
64-bit kernels an unbounded max_size can trigger a slab allocator warning
in devm_kzalloc().

[ ... ]

> +int mali_vpu_fw_handle_rpc(struct mali_vpu_fw *fw, bool *flush_mmu)
> +{
> +	struct mve_rpc_communication_area *rpc_area = fw->rpc.cpu;
> +	int ret = 0;
> +
> +	if (flush_mmu)
> +		*flush_mmu = false;
> +
> +	dma_sync_single_for_cpu(fw->dev, fw->rpc.dma, sizeof(*rpc_area),
> +				DMA_FROM_DEVICE);
> +
> +	if (READ_ONCE(rpc_area->state) == MVE_RPC_STATE_PARAM) {
> +		/*
> +		 * Firmware publishes param only after writing the RPC request.
> +		 */
> +		dma_rmb();
> +
> +		ret = 1;
> +
> +		mali_vpu_fw_dbg_rpc(fw, "fw->host", rpc_area);
> +
> +		switch (rpc_area->call_id) {
> +		case MVE_RPC_FUNCTION_DEBUG_PRINTF: {
> +			if (!rpc_area->size ||
> +			    rpc_area->size > sizeof(rpc_area->params.debug_print.string)) {
> +				ret = -EPROTO;
> +				rpc_area->size = 0;
> +				break;
> +			}
> +
> +			const char *s = rpc_area->params.debug_print.string;
> +			size_t len = strnlen(s, rpc_area->size);
> +
> +			/* Strip trailing characters */
> +			while (len && (s[len - 1] == '\n' || s[len - 1] == '\r'))
> +				len--;
> +
> +			dev_info(fw->dev,
> +				 "firmware DEBUG Print: %.*s\n", (int)len, s);
> +			break;
> +		}
> +		case MVE_RPC_FUNCTION_MEM_ALLOC: {
> +			if (rpc_area->size != sizeof(rpc_area->params.mem_alloc)) {
> +				ret = -EPROTO;
> +				break;
> +			}
> +
> +			mali_vpu_rpc_mem_alloc(fw, rpc_area);
> +			break;
> +		}
> +		case MVE_RPC_FUNCTION_MEM_RESIZE: {
> +			if (rpc_area->size != sizeof(rpc_area->params.mem_resize)) {
> +				ret = -EPROTO;
> +				break;
> +			}
> +
> +			mali_vpu_rpc_mem_resize(fw, rpc_area);
> +			if (flush_mmu)
> +				*flush_mmu = true;
> +			break;
> +		}
> +		case MVE_RPC_FUNCTION_MEM_FREE: {
> +			if (rpc_area->size != sizeof(rpc_area->params.mem_free)) {
> +				ret = -EPROTO;
> +				break;
> +			}
> +
> +			mali_vpu_rpc_mem_free(fw, rpc_area);
> +			if (flush_mmu)
> +				*flush_mmu = true;
> +			break;
> +		}
> +		default:
> +			dev_warn_ratelimited(fw->dev,
> +					     "unsupported RPC request. call_id=%u\n",
> +					     rpc_area->call_id);
> +			ret = -EOPNOTSUPP;
> +			break;
> +		}
> +
> +		/*
> +		 * Make sure the whole RPC message body has been written before
> +		 * the RPC message area is returned to the firmware.
> +		 */
> +		dma_wmb();
> +		WRITE_ONCE(rpc_area->state, MVE_RPC_STATE_RETURN);
> +
> +		dma_sync_single_for_device(fw->dev, fw->rpc.dma,
> +					   sizeof(*rpc_area), DMA_TO_DEVICE);

[Severity: Medium]
Does returning MVE_RPC_STATE_RETURN after a size mismatch leave stale
request words in the RPC response payload?

union mve_rpc_params aliases data[0] with mem_alloc.size and
mem_resize.ve_pointer.  When MVE_RPC_FUNCTION_MEM_ALLOC or
MVE_RPC_FUNCTION_MEM_RESIZE breaks out with -EPROTO, rpc_area->state is
still set to MVE_RPC_STATE_RETURN and synced back to the device with
rpc_area->params.data[0] holding the non-zero input size or old
ve_pointer, which the firmware ABI interprets as a valid allocated virtual
address rather than 0 for failure.

Also, MVE_RPC_FUNCTION_DEBUG_PRINTF sets rpc_area->size = 0 on its error
path, but leaves rpc_area->size set to the input string length on normal
completion, and the other error paths leave rpc_area->size untouched.

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

  reply	other threads:[~2026-11-19 15:51 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
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 [this message]
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-223429@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