Like vfio-pci, the uverbs DMA-buf is a single MMIO range with a p2pdma
provider which is mapped for each importing device by calling
dma_buf_phys_vec_to_sgt() from map_dma_buf(). However the DMA-buf core
now does that mapping for an exporter that hands out a dma_shared_buf.

So hand the range out through map_dma_shared_buf() and
unmap_dma_shared_buf() and let the core build the sg_table for SGT
importers. This removes the last caller of dma_buf_phys_vec_to_sgt().

map_dma_shared_buf() refuses while revoked and holds the kref for the
life of the dma_shared_buf, as the SGT map did for the life of the
sg_table, and attach keeps refusing importers without allow_peer2peer,
so the behaviour an importer sees is unchanged.

Signed-off-by: Alistair Popple <[email protected]>
---
 drivers/infiniband/core/uverbs_std_types_dmabuf.c | 37 ++++++++++++-----------
 1 file changed, 20 insertions(+), 17 deletions(-)

diff --git a/drivers/infiniband/core/uverbs_std_types_dmabuf.c 
b/drivers/infiniband/core/uverbs_std_types_dmabuf.c
index 8d8c997a4883..10d7ae9918ef 100644
--- a/drivers/infiniband/core/uverbs_std_types_dmabuf.c
+++ b/drivers/infiniband/core/uverbs_std_types_dmabuf.c
@@ -3,7 +3,7 @@
  * Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved
  */
 
-#include <linux/dma-buf-mapping.h>
+#include <linux/dma-buf.h>
 #include <linux/pci-p2pdma.h>
 #include <linux/dma-resv.h>
 #include <rdma/uverbs_std_types.h>
@@ -21,36 +21,39 @@ static int uverbs_dmabuf_attach(struct dma_buf *dmabuf,
        return 0;
 }
 
-static struct sg_table *
-uverbs_dmabuf_map(struct dma_buf_attachment *attachment,
-                 enum dma_data_direction dir)
+static struct dma_shared_buf *
+uverbs_dmabuf_map_dma_shared_buf(struct dma_buf_attachment *attachment)
 {
        struct ib_uverbs_dmabuf_file *priv = attachment->dmabuf->priv;
-       struct sg_table *ret;
+       struct dma_shared_buf *shared_buf;
 
        dma_resv_assert_held(attachment->dmabuf->resv);
 
        if (priv->revoked)
                return ERR_PTR(-ENODEV);
 
-       ret = dma_buf_phys_vec_to_sgt(attachment, priv->provider,
-                                     &priv->phys_vec, 1, priv->phys_vec.len,
-                                     dir);
-       if (IS_ERR(ret))
-               return ret;
+       shared_buf = kmalloc(struct_size(shared_buf, ranges, 1), GFP_KERNEL);
+       if (!shared_buf)
+               return ERR_PTR(-ENOMEM);
+
+       shared_buf->provider = priv->provider;
+       shared_buf->attrs = 0;
+       shared_buf->size = priv->phys_vec.len;
+       shared_buf->nr_ranges = 1;
+       shared_buf->ranges[0] = priv->phys_vec;
 
        kref_get(&priv->kref);
-       return ret;
+       return shared_buf;
 }
 
-static void uverbs_dmabuf_unmap(struct dma_buf_attachment *attachment,
-                               struct sg_table *sgt,
-                               enum dma_data_direction dir)
+static void
+uverbs_dmabuf_unmap_dma_shared_buf(struct dma_buf_attachment *attachment,
+                                  struct dma_shared_buf *shared_buf)
 {
        struct ib_uverbs_dmabuf_file *priv = attachment->dmabuf->priv;
 
        dma_resv_assert_held(attachment->dmabuf->resv);
-       dma_buf_free_sgt(attachment, sgt, dir);
+       kfree(shared_buf);
        kref_put(&priv->kref, ib_uverbs_dmabuf_done);
 }
 
@@ -97,8 +100,8 @@ static void uverbs_dmabuf_release(struct dma_buf *dmabuf)
 
 static const struct dma_buf_ops uverbs_dmabuf_ops = {
        .attach = uverbs_dmabuf_attach,
-       .map_dma_buf = uverbs_dmabuf_map,
-       .unmap_dma_buf = uverbs_dmabuf_unmap,
+       .map_dma_shared_buf = uverbs_dmabuf_map_dma_shared_buf,
+       .unmap_dma_shared_buf = uverbs_dmabuf_unmap_dma_shared_buf,
        .pin = uverbs_dmabuf_pin,
        .unpin = uverbs_dmabuf_unpin,
        .release = uverbs_dmabuf_release,

-- 
2.54.0

Reply via email to