Thank you for your contribution! Sashiko AI review found 3 potential issue(s) 
to consider:
- [Critical] drm/msm: skipped leaf IOTLB invalidation in msm_iommu_unmap()
- [High] drm/msm: missing IOTLB flush on error rollback in msm_iommu_map()
- [High] drm/msm: missing iotlb_sync_map call in msm_iommu_map()
--

commit 65df32b670bbcd4a7d2a95abeebac540248b0bbd
Author: Rob Clark <[email protected]>

drm/msm: Convert iommu map/unmap to helpers

This commit converts msm_iommu_map() and msm_iommu_unmap() to use the
__do_map() and __do_unmap() helpers with callbacks that invoke
domain->ops->map_pages() and domain->ops->unmap_pages() directly.

> diff --git a/drivers/gpu/drm/msm/msm_iommu.c b/drivers/gpu/drm/msm/msm_iommu.c
> index 78c82f4a01b92..435736c8d7150 100644
> --- a/drivers/gpu/drm/msm/msm_iommu.c
> +++ b/drivers/gpu/drm/msm/msm_iommu.c
> @@ -731,12 +731,27 @@ static void msm_iommu_detach(struct msm_mmu *mmu)
>       iommu_detach_device(iommu->domain, mmu->dev);
>  }
>  
> +static size_t
> +__unmap_iommu(void *arg, u64 iova, size_t pgsize, size_t pgcount)
> +{
> +     struct iommu_domain *domain = arg;
> +     return domain->ops->unmap_pages(domain, iova, pgsize, pgcount, NULL);
> +}
> +
> +static int
> +__map_iommu(void *arg, phys_addr_t paddr, u64 iova, size_t pgsize,
> +         size_t pgcount, int prot, size_t *mapped)
> +{
> +     struct iommu_domain *domain = arg;
> +     return domain->ops->map_pages(domain, iova, paddr, pgsize, pgcount,
> +                                   prot, GFP_KERNEL, mapped);
> +}
> +
>  static int msm_iommu_map(struct msm_mmu *mmu, uint64_t iova,
>                        struct sg_table *sgt, size_t off, size_t len,
>                        int prot)
>  {
> -     struct msm_iommu *iommu = to_msm_iommu(mmu);
> -     ssize_t ret;
> +     struct iommu_domain *domain = to_msm_iommu(mmu)->domain;
>  
>       WARN_ON(off != 0);
>  
> @@ -744,23 +759,23 @@ static int msm_iommu_map(struct msm_mmu *mmu, uint64_t 
> iova,
>       if (iova & BIT_ULL(48))
>               iova |= GENMASK_ULL(63, 49);
>  
> -     ret = iommu_map_sgtable(iommu->domain, iova, sgt, prot);
> -     if (ret < 0)
> -             return ret;
> -
> -     return (ret == len) ? 0 : -EINVAL;
> +     return __do_map(domain->pgsize_bitmap, iova, sgt, off, len, prot,
> +                     domain, __map_iommu, __unmap_iommu);
>  }

[Severity: High]
Does msm_iommu_map() miss an IOTLB flush when __do_map() fails mid-way?

If __map_iommu() fails after some pages have already been mapped,
__do_map() unwinds the partial mappings by calling __do_unmap() with
__unmap_iommu():

drivers/gpu/drm/msm/msm_iommu.c:__do_map() {
    ...
                        if (ret) {
                                __do_unmap(pgsize_bitmap, iova, addr - iova, 
arg, unmap);
                                return -EINVAL;
                        }
    ...
}

Because __unmap_iommu() passes NULL for the iotlb_gather parameter to
domain->ops->unmap_pages() and __do_unmap() does not flush the IOTLB,
stale translations for the rolled-back pages can remain cached in the
IOTLB after msm_iommu_map() returns an error.

In comparison, msm_iommu_pagetable_map() in the same file explicitly
flushes the IOTLB on error:

drivers/gpu/drm/msm/msm_iommu.c:msm_iommu_pagetable_map() {
    ...
        if (ret) {
                /*
                 * If the map failed mid-way, it will have triggered an
                 * unmap, so we need to flush the TLB:
                 */
                iommu_flush_iotlb_all(to_msm_iommu(pagetable->parent)->domain);
        }
    ...
}

Also, by replacing iommu_map_sgtable() with direct calls to
domain->ops->map_pages() via __map_iommu(), msm_iommu_map() no longer
invokes domain->ops->iotlb_sync_map().

On platforms using drivers/iommu/msm_iommu.c (such as APQ8064),
msm_iommu_ops defines .iotlb_sync_map = msm_iommu_sync_map to flush the
IOTLB after mapping pages. Without calling iotlb_sync_map, will those
platforms miss required IOTLB synchronization on map?

>  
>  static int msm_iommu_unmap(struct msm_mmu *mmu, uint64_t iova, size_t len)
>  {
> -     struct msm_iommu *iommu = to_msm_iommu(mmu);
> +     struct iommu_domain *domain = to_msm_iommu(mmu)->domain;
> +     int ret;
>  
>       if (iova & BIT_ULL(48))
>               iova |= GENMASK_ULL(63, 49);
>  
> -     iommu_unmap(iommu->domain, iova, len);
> +     ret = __do_unmap(domain->pgsize_bitmap, iova, len, domain, 
> __unmap_iommu);
>  
> -     return 0;
> +     iommu_flush_iotlb_all(domain);
> +
> +     return ret;
>  }

[Severity: Critical]
Can replacing iommu_unmap() with __do_unmap() and iommu_flush_iotlb_all()
skip leaf IOTLB invalidations on SoCs using qcom_iommu?

Here, msm_iommu_unmap() calls __do_unmap() with __unmap_iommu(), which
passes a NULL iotlb_gather pointer to domain->ops->unmap_pages().

In __arm_lpae_unmap() in drivers/iommu/io-pgtable-arm.c, per-page leaf TLB
invalidations are only issued when gather is non-NULL:

drivers/iommu/io-pgtable-arm.c:__arm_lpae_unmap() {
    ...
                if (gather && !iommu_iotlb_gather_queued(gather))
                        for (int j = 0; j < i; j++)
                                io_pgtable_tlb_add_page(iop, gather, iova + j * 
size, size);
    ...
}

When msm_iommu_unmap() subsequently calls iommu_flush_iotlb_all(domain),
qcom_iommu_flush_iotlb_all() in drivers/iommu/arm/arm-smmu/qcom_iommu.c
only issues a TLBSYNC without issuing a context-wide TLBIASID invalidation:

drivers/iommu/arm/arm-smmu/qcom_iommu.c:qcom_iommu_flush_iotlb_all() {
        struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
        struct io_pgtable *pgtable = container_of(qcom_domain->pgtbl_ops,
                                                  struct io_pgtable, ops);
        if (!qcom_domain->pgtbl_ops)
                return;

        pm_runtime_get_sync(qcom_domain->iommu->dev);
        qcom_iommu_tlb_sync(pgtable->cookie);
        pm_runtime_put_sync(qcom_domain->iommu->dev);
}

Because gather is NULL during unmap_pages() (skipping
qcom_iommu_tlb_add_page() and its ARM_SMMU_CB_S1_TLBIVAL writes) and
qcom_iommu_flush_iotlb_all() only syncs prior invalidations, wouldn't leaf
IOTLB entries remain uninvalidated after unmapping buffers?

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=4

Reply via email to