[RFC 3/4] RDMA/core: Add ib_dma_buf_dynamic_attach()

Anuj Gupta anuj20.g at samsung.com
Wed Nov 18 02:33:26 PST 2026


ULPs must not use the DMA device of an ib_device directly, but a ULP
importing a dma-buf has to attach it to that device.

Add a helper that attaches a dma-buf to the DMA device of an ib_device.
Devices using virtual DMA, such as rxe and siw, have no DMA device and
are rejected with -EOPNOTSUPP.

Signed-off-by: Nitesh Shetty <nj.shetty at samsung.com>
Signed-off-by: Anuj Gupta <anuj20.g at samsung.com>
---
 drivers/infiniband/core/verbs.c | 28 ++++++++++++++++++++++++++++
 include/rdma/ib_verbs.h         |  7 +++++++
 2 files changed, 35 insertions(+)

diff --git a/drivers/infiniband/core/verbs.c b/drivers/infiniband/core/verbs.c
index c4eb1de44155..eb3036d6f7e1 100644
--- a/drivers/infiniband/core/verbs.c
+++ b/drivers/infiniband/core/verbs.c
@@ -38,6 +38,7 @@
 
 #include <linux/errno.h>
 #include <linux/err.h>
+#include <linux/dma-buf.h>
 #include <linux/export.h>
 #include <linux/string.h>
 #include <linux/slab.h>
@@ -3217,3 +3218,30 @@ void rdma_free_hw_stats_struct(struct rdma_hw_stats *stats)
 	kfree(stats);
 }
 EXPORT_SYMBOL(rdma_free_hw_stats_struct);
+
+/**
+ * ib_dma_buf_dynamic_attach - attach a dma-buf for DMA by an RDMA device
+ * @dev: RDMA device that will access the dma-buf
+ * @dmabuf: dma-buf to attach
+ * @importer_ops: importer callbacks, see dma_buf_dynamic_attach()
+ * @importer_priv: importer private data, see dma_buf_dynamic_attach()
+ *
+ * Attach @dmabuf to the device that does DMA for @dev. Devices using
+ * virtual DMA have no such device and are not supported.
+ *
+ * Return: the attachment, or an ERR_PTR() on failure.
+ */
+struct dma_buf_attachment *
+ib_dma_buf_dynamic_attach(struct ib_device *dev, struct dma_buf *dmabuf,
+			  const struct dma_buf_attach_ops *importer_ops,
+			  void *importer_priv)
+{
+	if (ib_uses_virt_dma(dev))
+		return ERR_PTR(-EOPNOTSUPP);
+
+	return dma_buf_dynamic_attach(dmabuf, dev->dma_device, importer_ops,
+				      importer_priv);
+}
+EXPORT_SYMBOL_GPL(ib_dma_buf_dynamic_attach);
+
+MODULE_IMPORT_NS("DMA_BUF");
diff --git a/include/rdma/ib_verbs.h b/include/rdma/ib_verbs.h
index d6eaa3fee66e..c287bee1dd09 100644
--- a/include/rdma/ib_verbs.h
+++ b/include/rdma/ib_verbs.h
@@ -4323,6 +4323,13 @@ static inline bool ib_dma_pci_p2p_dma_supported(struct ib_device *dev)
 	return dma_pci_p2pdma_supported(dev->dma_device);
 }
 
+struct dma_buf;
+struct dma_buf_attach_ops;
+struct dma_buf_attachment *
+ib_dma_buf_dynamic_attach(struct ib_device *dev, struct dma_buf *dmabuf,
+			  const struct dma_buf_attach_ops *importer_ops,
+			  void *importer_priv);
+
 /**
  * ib_virt_dma_to_ptr - Convert a dma_addr to a kernel pointer
  * @dma_addr: The DMA address
-- 
2.25.1




More information about the Linux-nvme mailing list