[RFC 2/4] nvme: let transports provide the dma-buf attachment

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


From: Nitesh Shetty <nj.shetty at samsung.com>

dma-bufs are currently attached to ctrl->dev by nvme-core. This works
for nvme-pci, but fabrics transports can use a different device for
data DMA.

Let transports create the attachment and pass it to nvme-core, which
takes ownership of it. nvme-pci continues to attach to its PCI device.

Signed-off-by: Nitesh Shetty <nj.shetty at samsung.com>
Signed-off-by: Anuj Gupta <anuj20.g at samsung.com>
---
 drivers/nvme/host/dmabuf.c | 35 ++++++++++++++++++++++-------------
 drivers/nvme/host/dmabuf.h |  6 +++++-
 drivers/nvme/host/pci.c    | 11 ++++++++++-
 3 files changed, 37 insertions(+), 15 deletions(-)

diff --git a/drivers/nvme/host/dmabuf.c b/drivers/nvme/host/dmabuf.c
index 1f9104a5d75e..f68ffa89e56f 100644
--- a/drivers/nvme/host/dmabuf.c
+++ b/drivers/nvme/host/dmabuf.c
@@ -15,10 +15,11 @@ static void nvme_dmabuf_invalidate_mappings(struct dma_buf_attachment *attach)
 	dma_buf_io_invalidate_mappings(ctx);
 }
 
-static const struct dma_buf_attach_ops nvme_dmabuf_importer_ops = {
+const struct dma_buf_attach_ops nvme_dmabuf_importer_ops = {
 	.invalidate_mappings	= nvme_dmabuf_invalidate_mappings,
 	.allow_peer2peer	= true,
 };
+EXPORT_SYMBOL_GPL(nvme_dmabuf_importer_ops);
 
 static struct dma_buf_io_map *nvme_dma_buf_io_map(struct dma_buf_io_ctx *ctx)
 {
@@ -127,23 +128,21 @@ static const struct dma_buf_io_ops nvme_dma_buf_io_ops = {
 
 static int nvme_init_io_ctx_locked(struct nvme_ctrl *ctrl,
 		struct nvme_dma_buf_list *buf_list,
+		struct dma_buf_attachment *attach,
 		struct dma_buf_io_ctx *ctx)
 {
 	struct nvme_dma_buf_io_ctx *nvme_ctx;
-	struct dma_buf_attachment *attach;
+	int ret;
 
-	if (buf_list->dead)
-		return -ENODEV;
+	if (buf_list->dead) {
+		ret = -ENODEV;
+		goto err_detach;
+	}
 
 	nvme_ctx = kzalloc_obj(*nvme_ctx);
-	if (!nvme_ctx)
-		return -ENOMEM;
-
-	attach = dma_buf_dynamic_attach(ctx->dmabuf, ctrl->dev,
-					&nvme_dmabuf_importer_ops, ctx);
-	if (IS_ERR(attach)) {
-		kfree(nvme_ctx);
-		return PTR_ERR(attach);
+	if (!nvme_ctx) {
+		ret = -ENOMEM;
+		goto err_detach;
 	}
 
 	nvme_get_ctrl(ctrl);
@@ -155,16 +154,26 @@ static int nvme_init_io_ctx_locked(struct nvme_ctrl *ctrl,
 	ctx->dev_priv = nvme_ctx;
 	ctx->dev_ops = &nvme_dma_buf_io_ops;
 	return 0;
+
+err_detach:
+	dma_buf_detach(ctx->dmabuf, attach);
+	return ret;
 }
 
+/*
+ * @attach must have been created with nvme_dmabuf_importer_ops and @ctx as
+ * the importer private data. The context takes ownership of it, and it is
+ * detached on failure or when the context is released.
+ */
 int __nvme_init_dma_buf_io_ctx(struct nvme_ctrl *ctrl,
 		struct nvme_dma_buf_list *buf_list,
+		struct dma_buf_attachment *attach,
 		struct dma_buf_io_ctx *ctx)
 {
 	int ret;
 
 	mutex_lock(&buf_list->lock);
-	ret = nvme_init_io_ctx_locked(ctrl, buf_list, ctx);
+	ret = nvme_init_io_ctx_locked(ctrl, buf_list, attach, ctx);
 	mutex_unlock(&buf_list->lock);
 	return ret;
 }
diff --git a/drivers/nvme/host/dmabuf.h b/drivers/nvme/host/dmabuf.h
index 08a5b8e5d771..0c12ee6d3584 100644
--- a/drivers/nvme/host/dmabuf.h
+++ b/drivers/nvme/host/dmabuf.h
@@ -44,9 +44,12 @@ static inline struct nvme_dmabuf_map *nvme_req_to_map(struct request *req)
 	return to_nvme_dmabuf_map(map);
 }
 
+extern const struct dma_buf_attach_ops nvme_dmabuf_importer_ops;
+
 void __nvme_remove_dmabufs(struct nvme_dma_buf_list *list);
 int __nvme_init_dma_buf_io_ctx(struct nvme_ctrl *ctrl,
 		struct nvme_dma_buf_list *buf_list,
+		struct dma_buf_attachment *attach,
 		struct dma_buf_io_ctx *ctx);
 
 static inline void nvme_remove_dmabufs(struct nvme_dma_buf_list *list)
@@ -57,11 +60,12 @@ static inline void nvme_remove_dmabufs(struct nvme_dma_buf_list *list)
 
 static inline int nvme_init_dma_buf_io_ctx(struct nvme_ctrl *ctrl,
 		struct nvme_dma_buf_list *buf_list,
+		struct dma_buf_attachment *attach,
 		struct dma_buf_io_ctx *ctx)
 {
 	if (!IS_ENABLED(CONFIG_DMA_SHARED_BUFFER))
 		return -EOPNOTSUPP;
-	return __nvme_init_dma_buf_io_ctx(ctrl, buf_list, ctx);
+	return __nvme_init_dma_buf_io_ctx(ctrl, buf_list, attach, ctx);
 }
 
 static inline void nvme_init_buf_list(struct nvme_dma_buf_list *buf_list)
diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c
index 1af356abde0b..412fbc3d9544 100644
--- a/drivers/nvme/host/pci.c
+++ b/drivers/nvme/host/pci.c
@@ -3771,8 +3771,16 @@ static int nvme_pci_init_dma_buf_io_ctx(struct nvme_ctrl *ctrl,
 		struct dma_buf_io_ctx *ctx)
 {
 	struct nvme_dev *dev = to_nvme_dev(ctrl);
+	struct dma_buf_attachment *attach;
 
-	return nvme_init_dma_buf_io_ctx(ctrl, &dev->dmabufs, ctx);
+	if (!IS_ENABLED(CONFIG_DMA_SHARED_BUFFER))
+		return -EOPNOTSUPP;
+
+	attach = dma_buf_dynamic_attach(ctx->dmabuf, dev->dev,
+					&nvme_dmabuf_importer_ops, ctx);
+	if (IS_ERR(attach))
+		return PTR_ERR(attach);
+	return nvme_init_dma_buf_io_ctx(ctrl, &dev->dmabufs, attach, ctx);
 }
 
 static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
@@ -4571,5 +4579,6 @@ MODULE_AUTHOR("Matthew Wilcox <willy at linux.intel.com>");
 MODULE_LICENSE("GPL");
 MODULE_VERSION("1.0");
 MODULE_DESCRIPTION("NVMe host PCIe transport driver");
+MODULE_IMPORT_NS("DMA_BUF");
 module_init(nvme_init);
 module_exit(nvme_exit);
-- 
2.25.1




More information about the Linux-nvme mailing list