Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] iommufd: missed revoke race during attach in
  iopt_map_dmabuf()
- [Low] iommufd: multi-domain unfill paths bypass dma_buf_iommu_unmap()
--

commit e899ecc3d9089496a227965c5bb36a21b049566a
Author: Alistair Popple <[email protected]>

iommufd: Map DMA-bufs through dma_buf_iommu_map()

This commit switches iommufd to map and unmap DMA-bufs in IOMMU domains
through dma_buf_iommu_map() and dma_buf_iommu_unmap() instead of reading
physical addresses through the PFN reader.

> diff --git a/drivers/iommu/iommufd/pages.c b/drivers/iommu/iommufd/pages.c
> index fcdbde007a9ab..aeac509e574fe 100644
> --- a/drivers/iommu/iommufd/pages.c
> +++ b/drivers/iommu/iommufd/pages.c

[ ... ]

> @@ -1467,38 +1461,16 @@ static const struct dma_buf_attach_ops 
> iopt_dmabuf_attach_revoke_ops = {
>       .invalidate_mappings = iopt_revoke_notify,
>  };
>  
> -/*
> - * iommufd and vfio have a circular dependency. Future work for a phys
> - * based private interconnect will remove this.
> - */
> -static int
> -sym_vfio_pci_dma_buf_iommufd_map(struct dma_buf_attachment *attachment,
> -                              struct phys_vec *phys)
> -{
> -     typeof(&vfio_pci_dma_buf_iommufd_map) fn;
> -     int rc;
> -
> -     rc = iommufd_test_dma_buf_iommufd_map(attachment, phys);
> -     if (rc != -EOPNOTSUPP)
> -             return rc;
> -
> -     if (!IS_ENABLED(CONFIG_VFIO_PCI_DMABUF))
> -             return -EOPNOTSUPP;
> -
> -     fn = symbol_get(vfio_pci_dma_buf_iommufd_map);
> -     if (!fn)
> -             return -EOPNOTSUPP;
> -     rc = fn(attachment, phys);
> -     symbol_put(vfio_pci_dma_buf_iommufd_map);
> -     return rc;
> -}
> -
>  static int iopt_map_dmabuf(struct iommufd_ctx *ictx, struct iopt_pages 
> *pages,
>                          struct dma_buf *dmabuf)
>  {
>       struct dma_buf_attachment *attach;
>       int rc;
>  
> +     /* Fail at import rather than when a domain is first filled */
> +     if (!dma_buf_iommu_mappable(dmabuf))
> +             return -EOPNOTSUPP;
> +
>       attach = dma_buf_dynamic_attach(dmabuf, iommufd_global_device(),
>                                       &iopt_dmabuf_attach_revoke_ops, pages);
>       if (IS_ERR(attach))
> @@ -1518,18 +1490,12 @@ static int iopt_map_dmabuf(struct iommufd_ctx *ictx, 
> struct iopt_pages *pages,
>       if (rc)
>               goto err_detach;
>  
> -     rc = sym_vfio_pci_dma_buf_iommufd_map(attach, &pages->dmabuf.phys);
> -     if (rc)
> -             goto err_unpin;
> -
>       dma_resv_unlock(dmabuf->resv);
>  
>       /* On success iopt_release_pages() will detach and put the dmabuf. */
>       pages->dmabuf.attach = attach;
>       return 0;

[Severity: Medium]
Can iopt_map_dmabuf() miss a concurrent revoke and leave
pages->dmabuf.revoked as false on a revoked buffer?

Previously, sym_vfio_pci_dma_buf_iommufd_map() checked priv->revoked under
dmabuf->resv after dma_buf_dynamic_attach() had added attach to
dmabuf->attachments.  Now the only check during import is in
dmabuf->ops->attach(), which dma_buf_dynamic_attach() calls before taking
dmabuf->resv and adding attach to dmabuf->attachments, while dma_buf_pin()
is a no-op for exporters without .pin (such as vfio_pci_dmabuf_ops and
iommufd_test_dmabuf_ops):

CPU0                                    CPU1
iopt_map_dmabuf()
  dma_buf_dynamic_attach()
    ops->attach()
      if (priv->revoked) // false
                                        vfio_pci_dma_buf_move() /
                                        iommufd_test_dmabuf_revoke()
                                          dma_resv_lock(dmabuf->resv, ...)
                                          priv->revoked = true;
                                          dma_buf_invalidate_mappings()
                                            // attach not in attachments yet
                                          dma_resv_unlock(dmabuf->resv)
    dma_resv_lock(dmabuf->resv, ...)
    list_add(&attach->node,
             &dmabuf->attachments)
    dma_resv_unlock(dmabuf->resv)
  ...
  // succeeds with pages->dmabuf.revoked == false

If the IOAS has no domains attached yet, iopt_area_fill_domains() returns 0
early when xa_empty(&area->iopt->domains) is true, so IOMMU_IOAS_MAP_FILE
succeeds with pages->dmabuf.revoked left as false.  When a domain is later
attached via iopt_table_add_domain() -> iopt_fill_domain(),
iopt_area_fill_domain() sees iopt_dmabuf_revoked() == false and calls
dma_buf_iommu_map(), failing the domain attach with -ENODEV instead of
skipping the revoked DMA-buf (or allowing a later mapping if the exporter
re-enables access after a revoke that should have been permanent).

> @@ -1764,8 +1730,8 @@ static void __iopt_area_unfill_domain(struct iopt_area 
> *area,
>       if (iopt_is_dmabuf(pages)) {
>               if (WARN_ON(iopt_dmabuf_revoked(pages)))
>                       return;
> -             iopt_area_unmap_domain_range(area, domain, start_index,
> -                                          last_index);
> +             iopt_dmabuf_unmap_domain_range(area, pages, domain, start_index,
> +                                            last_index);
>               return;
>       }

[Severity: Low]
Should the multi-domain unfill paths in iopt_unfill_domain() and
iopt_area_unfill_domains() also unmap DMA-bufs through
iopt_dmabuf_unmap_domain_range() (or iopt_area_unfill_domain()) instead of
iopt_area_unmap_domain() and iopt_area_unmap_domain_range()?

When more than one domain is attached to the IOAS, those two paths still
unmap DMA-buf areas directly via iommu_unmap_nofail() rather than
dma_buf_iommu_unmap():

drivers/iommu/iommufd/io_pagetable.c:iopt_unfill_domain() {
    ...
                        if (iopt_is_dmabuf(pages)) {
                                if (!iopt_dmabuf_revoked(pages))
                                        iopt_area_unmap_domain(area, domain);
                                iopt_dmabuf_untrack_domain(pages, area, domain);
                        }
    ...
}

drivers/iommu/iommufd/pages.c:iopt_area_unfill_domains() {
    ...
        xa_for_each(&iopt->domains, index, domain) {
                if (domain == area->storage_domain)
                        continue;

                if (!iopt_dmabuf_revoked(pages))
                        iopt_area_unmap_domain_range(
                                area, domain, iopt_area_index(area),
                                iopt_area_last_index(area));
        }
    ...
}

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/20261201-dmabuf_shared_buf_v1_xe_rc5-v1-0-b672717c7...@nvidia.com?part=13

Reply via email to