Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Roman Vivchar" <rva333@protonmail.com>
Cc: robh@kernel.org, conor+dt@kernel.org, devicetree@vger.kernel.org
Subject: Re: [PATCH 3/3] iommu/mediatek-v1: add mt6572 support
Date: Wed, 09 Sep 2026 14:32:24 +0000	[thread overview]
Message-ID: <20260909143224.A2ED71F00A3D@smtp.kernel.org> (raw)
In-Reply-To: <20260909-6572-iommu-v1-3-de261da09fb2@protonmail.com>

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

New issues:
- [High] Insufficient hardware synchronization for MT6572 TLB flush completion

Pre-existing issues:
- [High] NULL pointer dereference in mtk_iommu_v1_domain_free() if domain was never attached
- [High] Race condition causing use-after-disable in hardware interrupt handling during teardown
- [High] Data corruption and Use-After-Free due to missing lock in TLB flush sequence
--

commit 29773ece1b87d664d6aa369fe95265b2f2898794
Author: Roman Vivchar <rva333@protonmail.com>

iommu/mediatek-v1: add mt6572 support

This commit adds support for the mt6572 SoC, which utilizes an earlier
version of the IOMMU that shares the same v1 architecture but has minor
hardware differences compared to mt2701. It introduces mtk_iommu_type to
distinguish between the mt2701 and mt6572 variants.

> diff --git a/drivers/iommu/mtk_iommu_v1.c b/drivers/iommu/mtk_iommu_v1.c
> index 72355f41c36af..b6f547b100d10 100644
> --- a/drivers/iommu/mtk_iommu_v1.c
> +++ b/drivers/iommu/mtk_iommu_v1.c

[ ... ]

> @@ -180,25 +199,33 @@ static void mtk_iommu_v1_tlb_flush_range(struct mtk_iommu_v1_data *data,
>  					 unsigned long iova, size_t size)
>  {
>  	int ret;
> -	u32 tmp;
> +	u32 tmp, val = F_INVLD_EN0;
>  
> -	writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
> -		data->base + REG_MMU_INV_SEL);
> +	if (data->type == MTK_IOMMU_V1)
> +		val |= F_INVLD_EN1;
> +
> +	writel_relaxed(val, data->base + REG_MMU_INV_SEL);
>  	writel_relaxed(iova & F_MMU_FAULT_VA_MSK,
>  		data->base + REG_MMU_INVLD_START_A);
>  	writel_relaxed((iova + size - 1) & F_MMU_FAULT_VA_MSK,
>  		data->base + REG_MMU_INVLD_END_A);
>  	writel_relaxed(F_MMU_INV_RANGE, data->base + REG_MMU_INVALIDATE);

[Severity: High]
This isn't a bug introduced by this patch, but does this sequence of global
register writes lack a lock?

In mtk_iommu_v1_unmap() and mtk_iommu_v1_map(), pgtlock is dropped before
calling mtk_iommu_v1_tlb_flush_range():

drivers/iommu/mtk_iommu_v1.c:mtk_iommu_v1_unmap() {
    ...
	spin_unlock_irqrestore(&dom->pgtlock, flags);

	mtk_iommu_v1_tlb_flush_range(dom->data, iova, size);
    ...
}

If multiple CPUs concurrently perform map/unmap operations, can the
non-atomic sequence of writel_relaxed() calls to these shared global MMIO
registers interleave, causing the IOMMU to flush incorrect IOVA ranges?

> -	ret = readl_poll_timeout_atomic(data->base + REG_MMU_CPE_DONE,
> -				tmp, tmp != 0, 10, 100000);
> -	if (ret) {
> -		dev_warn(data->dev,
> -			 "Partial TLB flush timed out, falling back to full flush\n");
> -		mtk_iommu_v1_tlb_flush_all(data);
> +	if (data->type == MTK_IOMMU_V1) {
> +		ret = readl_poll_timeout_atomic(data->base + REG_MMU_CPE_DONE,
> +						tmp, tmp != 0, 10, 100000);
> +		if (ret) {
> +			dev_warn(data->dev,
> +				 "Partial TLB flush timed out, falling back to full flush\n");
> +			mtk_iommu_v1_tlb_flush_all(data);
> +		}
> +
> +		/* Clear the CPE status */
> +		writel_relaxed(0, data->base + REG_MMU_CPE_DONE);
> +	} else {
> +		/* Make sure the TLB flush is done */
> +		wmb();
>  	}
> -	/* Clear the CPE status */
> -	writel_relaxed(0, data->base + REG_MMU_CPE_DONE);
>  }

[Severity: High]
Does a wmb() CPU barrier provide actual hardware synchronization for the
MT6572 TLB flush completion?

A wmb() only orders CPU memory writes; it does not ensure the write has
reached the IOMMU hardware, nor does it poll the hardware status to verify
the TLB flush has finished.

If the software unmap path returns and allows memory to be freed while the
hardware is still performing the TLB invalidation, can a device DMA into
freed memory before the TLB entry actually invalidates, causing
Use-After-Free?

[Severity: High]
This is a pre-existing issue, but is there a race condition during hardware
interrupt handling when tearing down the driver?

In the probe error path and in mtk_iommu_v1_remove(), clk_disable_unprepare()
is called while the hardware IRQ is still actively registered:

drivers/iommu/mtk_iommu_v1.c:mtk_iommu_v1_probe() {
    ...
out_clk_unprepare:
	clk_disable_unprepare(data->bclk);
out_put_larbs:
	for (i = 0; i < MTK_LARB_NR_MAX; i++)
		put_device(data->larb_imu[i].dev);

	return ret;
}

drivers/iommu/mtk_iommu_v1.c:mtk_iommu_v1_remove() {
    ...
	clk_disable_unprepare(data->bclk);
	devm_free_irq(&pdev->dev, data->irq, data);
    ...
}

If a hardware interrupt fires between the clock disable and the IRQ being
freed (or cleaned up by devres), could mtk_iommu_v1_isr() execute and
perform readl_relaxed() on the unclocked hardware, resulting in a
synchronous external abort?

[Severity: High]
This is a pre-existing issue, but can a NULL pointer dereference happen in
mtk_iommu_v1_domain_free() if the domain was never attached?

If an IOMMU domain is successfully allocated via
mtk_iommu_v1_domain_alloc_paging() but device attachment fails or is
skipped, dom->data remains NULL because it is only populated inside
mtk_iommu_v1_attach_device() (via mtk_iommu_v1_domain_finalise).

When the IOMMU core subsequently invokes iommu_domain_free(), it calls
mtk_iommu_v1_domain_free():

drivers/iommu/mtk_iommu_v1.c:mtk_iommu_v1_domain_free() {
    ...
	struct mtk_iommu_v1_data *data = dom->data;

	dma_free_coherent(data->dev, M2701_IOMMU_PGT_SIZE,
			dom->pgt_va, dom->pgt_pa);
    ...
}

Does unconditionally dereferencing data->dev here cause a kernel panic if
data is NULL?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260909-6572-iommu-v1-0-de261da09fb2@protonmail.com?part=3

      reply	other threads:[~2026-09-09 14:32 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-09 14:14 [PATCH 0/3] iommu/mediatek-v1: fix the driver + mt6572 support Roman Vivchar via B4 Relay
2026-09-09 14:14 ` [PATCH 1/3] dt-bindings: iommu: mediatek: add mt6572 Roman Vivchar via B4 Relay
2026-09-09 14:22   ` sashiko-bot
2026-09-11  7:27   ` Krzysztof Kozlowski
2026-09-09 14:14 ` [PATCH 2/3] iommu/mediatek-v1: fix IOMMU device binding Roman Vivchar via B4 Relay
2026-09-09 14:30   ` sashiko-bot
2026-09-09 14:14 ` [PATCH 3/3] iommu/mediatek-v1: add mt6572 support Roman Vivchar via B4 Relay
2026-09-09 14:32   ` sashiko-bot [this message]

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=20260909143224.A2ED71F00A3D@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=rva333@protonmail.com \
    --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