Hand out the BAR's ranges through map_dma_shared_buf() and unmap_dma_shared_buf() and leave the DMA mapping to the core. The SGT importers of this buffer get their sg_table from the core, which maps the same provider and ranges this driver passed to dma_buf_phys_vec_to_sgt(), and are refused while revoked because map_dma_shared_buf() is; map_dma_shared_buf() holds the kref for the life of the dma_shared_buf as the SGT map did for the life of the table. Nothing changes for RDMA, nvme or the network importers. Attach keeps refusing importers without allow_peer2peer: this memory is always MMIO, and failing at attach beats failing at map. The private iommufd interface stays until iommufd is switched over to dma_buf_iommu_map(). Co-developed-by: Jason Gunthorpe Signed-off-by: Jason Gunthorpe Signed-off-by: Alistair Popple --- drivers/vfio/pci/vfio_pci_dmabuf.c | 39 +++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_dmabuf.c b/drivers/vfio/pci/vfio_pci_dmabuf.c index c16f460c01d6..667a31efa922 100644 --- a/drivers/vfio/pci/vfio_pci_dmabuf.c +++ b/drivers/vfio/pci/vfio_pci_dmabuf.c @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-only /* Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. */ -#include +#include #include #include @@ -47,37 +47,42 @@ static void vfio_pci_dma_buf_done(struct kref *kref) complete(&priv->comp); } -static struct sg_table * -vfio_pci_dma_buf_map(struct dma_buf_attachment *attachment, - enum dma_data_direction dir) +static struct dma_shared_buf * +vfio_pci_dma_buf_map_dma_shared_buf(struct dma_buf_attachment *attachment) { struct vfio_pci_dma_buf *priv = attachment->dmabuf->priv; - struct sg_table *ret; + struct dma_shared_buf *shared_buf; dma_resv_assert_held(priv->dmabuf->resv); if (priv->revoked) return ERR_PTR(-ENODEV); - ret = dma_buf_phys_vec_to_sgt(attachment, priv->provider, - priv->phys_vec, priv->nr_ranges, - priv->size, dir); - if (IS_ERR(ret)) - return ret; + shared_buf = kvmalloc(struct_size(shared_buf, ranges, priv->nr_ranges), + GFP_KERNEL); + if (!shared_buf) + return ERR_PTR(-ENOMEM); + + shared_buf->provider = priv->provider; + shared_buf->attrs = 0; + shared_buf->size = priv->size; + shared_buf->nr_ranges = priv->nr_ranges; + memcpy(shared_buf->ranges, priv->phys_vec, + sizeof(shared_buf->ranges[0]) * priv->nr_ranges); kref_get(&priv->kref); - return ret; + return shared_buf; } -static void vfio_pci_dma_buf_unmap(struct dma_buf_attachment *attachment, - struct sg_table *sgt, - enum dma_data_direction dir) +static void +vfio_pci_dma_buf_unmap_dma_shared_buf(struct dma_buf_attachment *attachment, + struct dma_shared_buf *shared_buf) { struct vfio_pci_dma_buf *priv = attachment->dmabuf->priv; dma_resv_assert_held(priv->dmabuf->resv); - dma_buf_free_sgt(attachment, sgt, dir); + kvfree(shared_buf); kref_put(&priv->kref, vfio_pci_dma_buf_done); } @@ -101,8 +106,8 @@ static void vfio_pci_dma_buf_release(struct dma_buf *dmabuf) static const struct dma_buf_ops vfio_pci_dmabuf_ops = { .attach = vfio_pci_dma_buf_attach, - .map_dma_buf = vfio_pci_dma_buf_map, - .unmap_dma_buf = vfio_pci_dma_buf_unmap, + .map_dma_shared_buf = vfio_pci_dma_buf_map_dma_shared_buf, + .unmap_dma_shared_buf = vfio_pci_dma_buf_unmap_dma_shared_buf, .release = vfio_pci_dma_buf_release, }; -- 2.54.0