Thank you for your contribution! Sashiko AI review found 1 potential issue(s) 
to consider:
- [Critical] drm/msm: bogus physical addresses in __do_map() when dma_iommu is 
active
--

commit b6b3547f9503a1fd43535b38edfaee1a6356607a
Author: Rob Clark <[email protected]>

drm/msm: Convert map helper to use dma-address

This commit switches __do_map() in drivers/gpu/drm/msm/msm_iommu.c from
iterating over scatterlist page entries with for_each_sgtable_sg() and
sg_phys() to iterating over DMA-mapped entries with for_each_sgtable_dma_sg(),
sg_dma_len(), and sg_dma_address().

> diff --git a/drivers/gpu/drm/msm/msm_iommu.c b/drivers/gpu/drm/msm/msm_iommu.c
> index 479e5450a4dcd..c2950d32c5075 100644
> --- a/drivers/gpu/drm/msm/msm_iommu.c
> +++ b/drivers/gpu/drm/msm/msm_iommu.c
> @@ -153,9 +153,9 @@ __do_map(unsigned long pgsize_bitmap, u64 iova, struct 
> sg_table *sgt, size_t off
>       u64 addr = iova;
>       unsigned int i;
>  
> -     for_each_sgtable_sg (sgt, sg, i) {
> -             size_t size = sg->length;
> -             phys_addr_t phys = sg_phys(sg);
> +     for_each_sgtable_dma_sg (sgt, sg, i) {
> +             size_t size = sg_dma_len(sg);
> +             phys_addr_t phys = sg_dma_address(sg);

[Severity: Critical]
Could passing sg_dma_address(sg) and sg_dma_len(sg) as physical addresses into
ops->map() program virtual IOVAs from an inactive default DMA domain into the
GPU and display SMMU page tables on platforms where use_dma_iommu() is true on
drm_dev_dma_dev(obj->dev)?

In drivers/gpu/drm/msm/msm_gem.c:get_pages() (and via dma_buf_map_attachment()
for PRIME imports), the scatterlist is DMA-mapped against
drm_dev_dma_dev(obj->dev):

    ret = dma_map_sgtable(drm_dev_dma_dev(obj->dev),
                          msm_obj->sgt,
                          DMA_BIDIRECTIONAL,
                          map_attrs);

On platforms where the iommus property is placed directly on the display
controller node (mdp_dev, which is obj->dev->dev) rather than a parent mdss
node, as handled in drivers/gpu/drm/msm/msm_kms.c:msm_kms_init_vm():

    if (device_iommu_mapped(mdp_dev))
        iommu_dev = mdp_dev;
    else if (mdss_dev && device_iommu_mapped(mdss_dev))
        iommu_dev = mdss_dev;

and the IOMMU driver does not return IOMMU_DOMAIN_IDENTITY from
.def_domain_type, the IOMMU core allocates a default DMA domain during probe
and drivers/iommu/dma-iommu.c:iommu_setup_dma_ops() sets dev->dma_iommu:

    dma_iommu = iommu_is_dma_domain(domain);
    dev_assign_dma_iommu(dev, dma_iommu);

For example, drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c:
qcom_smmu_client_of_match[] does not list "qcom,mdp5" (affecting MDP5
platforms such as SDM630, SDM660, and MSM8996), and qcom_iommu_ops in
drivers/iommu/arm/arm-smmu/qcom_iommu.c does not implement .def_domain_type at
all.

When msm_iommu_new() later attaches an unmanaged domain via
iommu_attach_device(), group->default_domain remains the DMA domain and
use_dma_iommu(dev) stays true. As a result, kernel/dma/mapping.c:
dma_go_direct() returns false even when get_dma_ops(dev) is NULL:

    if (use_dma_iommu(dev))
        return false;

    if (likely(!ops))
        return true;

This causes dma_map_sgtable() to call iommu_dma_map_sg(), which allocates
virtual IOVAs from the inactive group->default_domain, coalesces segments, and
stores those IOVAs and coalesced lengths into sg_dma_address(sg) and
sg_dma_len(sg).

When __do_map() subsequently passes phys = sg_dma_address(sg) and
size = sg_dma_len(sg) to io_pgtable_ops->map_pages() or
iommu_domain->ops->map_pages() (which expect CPU physical addresses), won't
this map bogus physical addresses into the active GPU and display page tables?

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

Reply via email to