Skip to contentSkip to conversation

RDMA subsystem DMA-BUF support

RFC New Haggai Eran 4 participants 17 messages Aug 1, 2016 – Aug 7, 2016

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover RDMA subsystem DMA-BUF support

Today's PCIe devices are capable of using peer-to-peer transactions to communicate directly with other devices. This has been shown to improve latency with communication between a GPU and HCA [1] as well as when transferring between storage and an HCA [2].

There is some missing functionality in the kernel to support this mode of operation. The RDMA subsystem uses get_user_pages() to pin user memory for memory regions used by its devices. There has been some attempts to add peer-to-peer transaction support to the kernel.

First, some linux-rdma patches that attempted to extend the RDMA subsystem with a plugin mechanism that would detect user pointers as belonging to a peer device [3,4] and request the bus address mappings from the plugin. These has been rejected saying that the RDMA subsystem is not the place to keep such a plugin mechanism.

Second, in newer kernels the ZONE_DEVICE feature allows persistent memory devices to describe their pages with a page struct, allowing for example for a get_user_pages() call from the RDMA stack to succeed. However, the current ZONE_DEVICE code only supports devices whose memory can be cached by the CPU, and patches to change that [5] has been rejected so far.

A third, more long term alternative is to use HMM [6]. HMM allows migrating anonymous memory to a device. It could possibly be extended to allow requesting a mapping for peer-to-peer access from another device. However, HMM is complex and it could take a long time until it is accepted.

This patch series attempts to use the existing DMA-BUF mechanism in the kernel to provide peer-to-peer transaction support for RDMA devices. DMA-BUF allows one device to attach to a buffer exported by another device. The series allows an RDMA application to use a new kernel API to create an RDMA memory region from a DMA-BUF file descriptor handle. The DMA-BUF object is pinned via the dma_buf_attach() call, and the code then uses dma_buf_map_attachment to get the mapping to the buffer and registers them with the RDMA device.

In order to provide an example use of the series, I've added DMA-BUF support to the NVMe driver. The code adds a ioctl to allocate CMB regions as DMA-BUF objects. These objects can then be used to register a memory region with the new API.

The series is structured as follows. The first two patches are preliminary patches needed for the mlx5_ib driver. They make it easier for the following patches to post a memory registration request using existing infrastructure. Patches 3-4 implement helper functions for DMA-BUF registration in the RDMA subsystem core, and expose a user-space command to do the registration. Patch 5 implements the new registration command in the mlx5_ib driver. Finally patches 6-7 add DMA-BUF support to the NVMe driver.

Caveats:
* Without having real NVMe device with CMB support I smoke-tested the patches with qemu-nvme [7] while assigning a Connect-IB card to the VM. Naturally this doesn't work. It remains to be tested with real devices on a system supporting peer to peer.
* GPU devices sometimes require either changing the addresses exposed on their BARs, or just invalidating them altogether. DMA-BUF however doesn't have such a mechanism right now, since it assumes cross device accesses will be short. DMA-BUF would need to be extended with a way to invalidate an existing attachment and the RDMA stack would need a way to invalidate existing MRs, similarly to what was done in [3,4].
* It is currently the responsibility of the DMA-BUF exporting code to make sure the buffer can be mapped for access by the attaching device. It is not always possible to do that (e.g. when the PCIe topology does not allow it) or the IOMMU needs to be configured accordingly.
* The scatterlists returned from DMA-BUF in this implementation do not have a struct page set. This is fine for RDMA as it only needs the DMA addresses. However, it may cause issues if someone tries to import these DMA-BUF objects into a driver that tries to use these page structs.

Comments are welcome.

Haggai

[1] Benchmarking GPUDirect RDMA on Modern Server Platforms, Davide Rosetti
https://devblogs.nvidia.com/parallelforall/benchmarking-gpudirect-rdma-on-modern-server-platforms/
[2] Project Donard: Peer-to-peer Communication with NVM Express Devices,
Stephen Bates, http://blog.pmcs.com/project-donard-peer-to-peer-communication-with-nvm-express-devices-part-1/
[3] [PATCH V2 for-next 0/9] Peer-Direct support Yishai Hadas,

    https://www.spinics.net/lists/linux-rdma/msg21770.html
[4] [RFC 0/7] Peer-direct memory, Artemy Kovalyov,
    https://www.spinics.net/lists/linux-rdma/msg33294.html
[5] [PATCH RFC 1/1] Add support for ZONE_DEVICE IO memory with struct pages, Stephen Bates
    https://www.spinics.net/lists/linux-rdma/msg34408.html
[6] HMM (Heterogeneous Memory Management), J?r?me Glisse,
    https://lkml.org/lkml/2016/3/8/721

[7] https://github.com/OpenChannelSSD/qemu-nvme

Haggai Eran (7):
IB/mlx5: Helper for posting work-requests on the UMR QP IB/mlx5: Support registration and invalidate operations on the UMR QP IB/core: Helpers for mapping DMA-BUF in MRs
IB/uverbs: Add command to register a DMA-BUF fd
IB/mlx5: Implement reg_user_dma_buf_mr
NVMe: Use genalloc to allocate CMB regions
NVMe: CMB on DMA-BUF

 drivers/infiniband/core/uverbs.h      |   1 +
 drivers/infiniband/core/uverbs_cmd.c  | 111 ++++++++++++
 drivers/infiniband/core/uverbs_main.c |   1 +
 drivers/infiniband/core/verbs.c       |  60 +++++++
 drivers/infiniband/hw/mlx5/main.c     |  10 +-
 drivers/infiniband/hw/mlx5/mlx5_ib.h  |   4 +
 drivers/infiniband/hw/mlx5/mr.c       | 149 ++++++++--------
 drivers/infiniband/hw/mlx5/qp.c       |  71 +++++---
 drivers/nvme/host/Makefile            |   2 +-
 drivers/nvme/host/core.c              |  29 ++++
 drivers/nvme/host/dmabuf.c            | 308 ++++++++++++++++++++++++++++++++++
 drivers/nvme/host/nvme-pci.h          |  26 +++
 drivers/nvme/host/nvme.h              |   1 +
 drivers/nvme/host/pci.c               |  60 ++++++-
 include/rdma/ib_verbs.h               |  15 ++
 include/uapi/linux/nvme_ioctl.h       |  11 ++
 include/uapi/rdma/ib_user_verbs.h     |  18 ++
 17 files changed, 778 insertions(+), 99 deletions(-)
 create mode 100644 drivers/nvme/host/dmabuf.c
 create mode 100644 drivers/nvme/host/nvme-pci.h
Signature · 1 line
1.7.11.2
#1 lore raw

RFC 1/7 IB/mlx5: Helper for posting work-requests on the UMR QP

Add mlx5_ib_post_umr_sync(), a helper function that posts work-requests on the UMR QP, waits for their completion, and returns the result. Modify several functions to use the new helper.

Signed-off-by Haggai Eran <haggaie at mellanox.com>
---
 drivers/infiniband/hw/mlx5/mr.c | 98 ++++++++++++++---------------------------
 1 file changed, 32 insertions(+), 66 deletions(-)
drivers/infiniband/hw/mlx5/mr.c +32 -66
index 8cf2ce50511f..fb2bb25c6cf0 100644
@@ -852,16 +852,41 @@ static inline void mlx5_ib_init_umr_context(struct mlx5_ib_umr_context *context)
 	init_completion(&context->done);
 }
 
+static int mlx5_ib_post_umr_sync(struct mlx5_ib_dev *dev, struct ib_send_wr *wr)
+{
+	struct umr_common *umrc = &dev->umrc;
+	struct mlx5_ib_umr_context umr_context;
+	struct ib_send_wr *bad;
+	int err;
+
+	mlx5_ib_init_umr_context(&umr_context);
+	wr->wr_cqe = &umr_context.cqe;
+
+	down(&umrc->sem);
+	err = ib_post_send(umrc->qp, wr, &bad);
+	if (err) {
+		mlx5_ib_warn(dev, "post send failed, err %d\n", err);
+		goto out;
+	} else {
+		wait_for_completion(&umr_context.done);
+		if (umr_context.status != IB_WC_SUCCESS) {
+			mlx5_ib_warn(dev, "umr failed\n");
+			err = -EFAULT;
+			goto out;
+		}
+	}
+out:
+	up(&umrc->sem);
+	return err;
+}
+
 static struct mlx5_ib_mr *reg_umr(struct ib_pd *pd, struct ib_umem *umem,
 				  u64 virt_addr, u64 len, int npages,
 				  int page_shift, int order, int access_flags)
 {
 	struct mlx5_ib_dev *dev = to_mdev(pd->device);
 	struct device *ddev = dev->ib_dev.dma_device;
-	struct umr_common *umrc = &dev->umrc;
-	struct mlx5_ib_umr_context umr_context;
 	struct mlx5_umr_wr umrwr = {};
-	struct ib_send_wr *bad;
 	struct mlx5_ib_mr *mr;
 	struct ib_sge sg;
 	int size;
@@ -890,24 +915,12 @@ static struct mlx5_ib_mr *reg_umr(struct ib_pd *pd, struct ib_umem *umem,
 	if (err)
 		goto free_mr;
 
-	mlx5_ib_init_umr_context(&umr_context);
-
-	umrwr.wr.wr_cqe = &umr_context.cqe;
 	prep_umr_reg_wqe(pd, &umrwr.wr, &sg, dma, npages, mr->mmkey.key,
 			 page_shift, virt_addr, len, access_flags);
 
-	down(&umrc->sem);
-	err = ib_post_send(umrc->qp, &umrwr.wr, &bad);
-	if (err) {
-		mlx5_ib_warn(dev, "post send failed, err %d\n", err);
+	err = mlx5_ib_post_umr_sync(dev, &umrwr.wr);
+	if (err)
 		goto unmap_dma;
-	} else {
-		wait_for_completion(&umr_context.done);
-		if (umr_context.status != IB_WC_SUCCESS) {
-			mlx5_ib_warn(dev, "reg umr failed\n");
-			err = -EFAULT;
-		}
-	}
 
 	mr->mmkey.iova = virt_addr;
 	mr->mmkey.size = len;
@@ -916,7 +929,6 @@ static struct mlx5_ib_mr *reg_umr(struct ib_pd *pd, struct ib_umem *umem,
 	mr->live = 1;
 
 unmap_dma:
-	up(&umrc->sem);
 	dma_unmap_single(ddev, dma, size, DMA_TO_DEVICE);
 
 	kfree(mr_pas);
@@ -1193,36 +1205,10 @@ error:
 
 static int unreg_umr(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr)
 {
-	struct umr_common *umrc = &dev->umrc;
-	struct mlx5_ib_umr_context umr_context;
 	struct mlx5_umr_wr umrwr = {};
-	struct ib_send_wr *bad;
-	int err;
-
-	mlx5_ib_init_umr_context(&umr_context);
 
-	umrwr.wr.wr_cqe = &umr_context.cqe;
 	prep_umr_unreg_wqe(dev, &umrwr.wr, mr->mmkey.key);
-
-	down(&umrc->sem);
-	err = ib_post_send(umrc->qp, &umrwr.wr, &bad);
-	if (err) {
-		up(&umrc->sem);
-		mlx5_ib_dbg(dev, "err %d\n", err);
-		goto error;
-	} else {
-		wait_for_completion(&umr_context.done);
-		up(&umrc->sem);
-	}
-	if (umr_context.status != IB_WC_SUCCESS) {
-		mlx5_ib_warn(dev, "unreg umr failed\n");
-		err = -EFAULT;
-		goto error;
-	}
-	return 0;
-
-error:
-	return err;
+	return mlx5_ib_post_umr_sync(dev, &umrwr.wr);
 }
 
 static int rereg_umr(struct ib_pd *pd, struct mlx5_ib_mr *mr, u64 virt_addr,
@@ -1231,19 +1217,13 @@ static int rereg_umr(struct ib_pd *pd, struct mlx5_ib_mr *mr, u64 virt_addr,
 {
 	struct mlx5_ib_dev *dev = to_mdev(pd->device);
 	struct device *ddev = dev->ib_dev.dma_device;
-	struct mlx5_ib_umr_context umr_context;
-	struct ib_send_wr *bad;
 	struct mlx5_umr_wr umrwr = {};
 	struct ib_sge sg;
-	struct umr_common *umrc = &dev->umrc;
 	dma_addr_t dma = 0;
 	__be64 *mr_pas = NULL;
 	int size;
 	int err;
 
-	mlx5_ib_init_umr_context(&umr_context);
-
-	umrwr.wr.wr_cqe = &umr_context.cqe;
 	umrwr.wr.send_flags = MLX5_IB_SEND_UMR_FAIL_IF_FREE;
 
 	if (flags & IB_MR_REREG_TRANS) {
@@ -1271,21 +1251,7 @@ static int rereg_umr(struct ib_pd *pd, struct mlx5_ib_mr *mr, u64 virt_addr,
 	}
 
 	/* post send request to UMR QP */
-	down(&umrc->sem);
-	err = ib_post_send(umrc->qp, &umrwr.wr, &bad);
-
-	if (err) {
-		mlx5_ib_warn(dev, "post send failed, err %d\n", err);
-	} else {
-		wait_for_completion(&umr_context.done);
-		if (umr_context.status != IB_WC_SUCCESS) {
-			mlx5_ib_warn(dev, "reg umr failed (%u)\n",
-				     umr_context.status);
-			err = -EFAULT;
-		}
-	}
-
-	up(&umrc->sem);
+	err = mlx5_ib_post_umr_sync(dev, &umrwr.wr);
 	if (flags & IB_MR_REREG_TRANS) {
 		dma_unmap_single(ddev, dma, size, DMA_TO_DEVICE);
 		kfree(mr_pas);
Signature · 1 line
1.7.11.2
#2 lore raw

RFC 2/7 IB/mlx5: Support registration and invalidate operations on the UMR QP

In reply to Haggai Eran

Currently the UMR QP only supports UMR work requests. There is nothing preventing it from also supporting fast registration and local invalidate operations. This would allow internal driver operations to use the fast registration API internally, without having to create an RC QP.

Use the ib_create_qp instead of mlx5_ib_create_qp in order to initialize the PD field of the new UMR QP.

Signed-off-by Haggai Eran <haggaie at mellanox.com>
---
 drivers/infiniband/hw/mlx5/main.c |  6 +---
 drivers/infiniband/hw/mlx5/qp.c   | 71 +++++++++++++++++++++++++++------------
 2 files changed, 50 insertions(+), 27 deletions(-)
drivers/infiniband/hw/mlx5/main.c +1 -5
index b48ad85315dc..f41254f3689a 100644
@@ -2023,16 +2023,12 @@ static int create_umr_res(struct mlx5_ib_dev *dev)
 	init_attr->cap.max_send_sge = 1;
 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
 	init_attr->port_num = 1;
-	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
+	qp = ib_create_qp(pd, init_attr);
 	if (IS_ERR(qp)) {
 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
 		ret = PTR_ERR(qp);
 		goto error_3;
 	}
-	qp->device     = &dev->ib_dev;
-	qp->real_qp    = qp;
-	qp->uobject    = NULL;
-	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
 
 	attr->qp_state = IB_QPS_INIT;
 	attr->port_num = 1;
drivers/infiniband/hw/mlx5/qp.c +49 -22

113 lines not on this page · raw message

Signature · 1 line
1.7.11.2
#3 lore raw

RFC 3/7 IB/core: Helpers for mapping DMA-BUF in MRs

In reply to Haggai Eran

DMA-BUF API allows devices to export buffers for other devices to access. This can provide a means to implement peer to peer communication for RDMA. A device with internal memory would expose its memory to the PCIe BAR, and export that region as a DMA-BUF object. Then, the user or ULP would register a memory region that is backed by the DMA-BUF object.

Signed-off-by Haggai Eran <haggaie at mellanox.com>
---
 drivers/infiniband/core/verbs.c | 60 +++++++++++++++++++++++++++++++++++++++++
 include/rdma/ib_verbs.h         |  9 +++++++
 2 files changed, 69 insertions(+)
drivers/infiniband/core/verbs.c +60 -0
index 6298f54b4137..633831e77a62 100644
@@ -44,6 +44,7 @@
 #include <linux/in.h>
 #include <linux/in6.h>
 #include <net/addrconf.h>
+#include <linux/dma-buf.h>
 
 #include <rdma/ib_verbs.h>
 #include <rdma/ib_cache.h>
@@ -1922,3 +1923,62 @@ void ib_drain_qp(struct ib_qp *qp)
 		ib_drain_rq(qp);
 }
 EXPORT_SYMBOL(ib_drain_qp);
+
+int ib_mr_attach_dmabuf(struct ib_mr *mr, struct dma_buf *dmabuf, int access)
+{
+	struct device *dev = mr->device->dma_device;
+	struct dma_buf_attachment *attach;
+	struct sg_table *sg;
+	int err;
+	int n;
+
+	get_dma_buf(dmabuf);
+
+	attach = dma_buf_attach(dmabuf, dev);
+	if (IS_ERR(attach)) {
+		dev_err(dev, "unable to attach to DMA-BUF object (from %s): %ld",
+			dmabuf->exp_name, PTR_ERR(attach));
+		err = PTR_ERR(attach);
+		goto put_dma_buf;
+	}
+
+	sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
+	if (IS_ERR(sg)) {
+		dev_err(dev, "unable to map a DMA-BUF object (from %s): %ld",
+			dmabuf->exp_name, PTR_ERR(sg));
+		err = PTR_ERR(sg);
+		goto detach;
+	}
+
+	n = ib_map_mr_sg_zbva(mr, sg->sgl, sg->nents, NULL, PAGE_SIZE);
+	if (unlikely(n != sg->nents)) {
+		dev_err(dev, "failed to map sg (%d/%d) of DMA-BUF object (from %s)\n",
+			n, sg->nents, dmabuf->exp_name);
+		err = n < 0 ? n : -EINVAL;
+		goto unmap;
+	}
+
+	mr->attach = attach;
+	mr->sg = sg;
+	return 0;
+
+unmap:
+	dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
+detach:
+	dma_buf_detach(dmabuf, attach);
+put_dma_buf:
+	dma_buf_put(dmabuf);
+	return err;
+}
+EXPORT_SYMBOL(ib_mr_attach_dmabuf);
+
+void ib_mr_detach_dmabuf(struct dma_buf_attachment *attach,
+			 struct sg_table *sg)
+{
+	struct dma_buf *dmabuf = attach->dmabuf;
+
+	dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
+	dma_buf_detach(dmabuf, attach);
+	dma_buf_put(dmabuf);
+}
+EXPORT_SYMBOL(ib_mr_detach_dmabuf);
include/rdma/ib_verbs.h +9 -0
index 7e440d41487a..da23ec0a06d7 100644
@@ -1465,6 +1465,9 @@ struct ib_mr {
 		struct ib_uobject	*uobject;	/* user */
 		struct list_head	qp_entry;	/* FR */
 	};
+	/* For MRs that expose a DMA-BUF */
+	struct dma_buf_attachment *attach;
+	struct sg_table *sg;
 };
 
 struct ib_mw {
@@ -3189,4 +3192,10 @@ int ib_sg_to_pages(struct ib_mr *mr, struct scatterlist *sgl, int sg_nents,
 void ib_drain_rq(struct ib_qp *qp);
 void ib_drain_sq(struct ib_qp *qp);
 void ib_drain_qp(struct ib_qp *qp);
+
+struct dma_buf;
+int ib_mr_attach_dmabuf(struct ib_mr *mr, struct dma_buf *dmabuf, int access);
+void ib_mr_detach_dmabuf(struct dma_buf_attachment *attach,
+			 struct sg_table *sg);
+
 #endif /* IB_VERBS_H */
Signature · 1 line
1.7.11.2
#4 lore raw

RFC 4/7 IB/uverbs: Add command to register a DMA-BUF fd

In reply to Haggai Eran

The new command accept the file descriptor of a DMA-BUF object as input, as well as the PD and access flags, and produces a memory region accessing the device memory exposed by the DMA-BUF object.

Signed-off-by Haggai Eran <haggaie at mellanox.com>
---
 drivers/infiniband/core/uverbs.h      |   1 +
 drivers/infiniband/core/uverbs_cmd.c  | 111 ++++++++++++++++++++++++++++++++++
 drivers/infiniband/core/uverbs_main.c |   1 +
 include/rdma/ib_verbs.h               |   6 ++
 include/uapi/rdma/ib_user_verbs.h     |  18 ++++++
 5 files changed, 137 insertions(+)
drivers/infiniband/core/uverbs.h +1 -0
index 612ccfd39bf9..491292740b67 100644
@@ -275,5 +275,6 @@ IB_UVERBS_DECLARE_EX_CMD(destroy_flow);
 IB_UVERBS_DECLARE_EX_CMD(query_device);
 IB_UVERBS_DECLARE_EX_CMD(create_cq);
 IB_UVERBS_DECLARE_EX_CMD(create_qp);
+IB_UVERBS_DECLARE_EX_CMD(reg_dma_buf_mr);
 
 #endif /* UVERBS_H */
drivers/infiniband/core/uverbs_cmd.c +111 -0

123 lines not on this page · raw message

drivers/infiniband/core/uverbs_main.c +1 -0
index 31f422a70623..c5368f2b1cf0 100644
@@ -130,6 +130,7 @@ static int (*uverbs_ex_cmd_table[])(struct ib_uverbs_file *file,
 	[IB_USER_VERBS_EX_CMD_QUERY_DEVICE]	= ib_uverbs_ex_query_device,
 	[IB_USER_VERBS_EX_CMD_CREATE_CQ]	= ib_uverbs_ex_create_cq,
 	[IB_USER_VERBS_EX_CMD_CREATE_QP]        = ib_uverbs_ex_create_qp,
+	[IB_USER_VERBS_EX_CMD_REG_DMA_BUF_MR]   = ib_uverbs_ex_reg_dma_buf_mr,
 };
 
 static void ib_uverbs_add_one(struct ib_device *device);
include/rdma/ib_verbs.h +6 -0
index da23ec0a06d7..9c4241aeec79 100644
@@ -1677,6 +1677,7 @@ struct ib_dma_mapping_ops {
 };
 
 struct iw_cm_verbs;
+struct dma_buf;
 
 struct ib_port_immutable {
 	int                           pkey_tbl_len;
@@ -1866,6 +1867,11 @@ struct ib_device {
 						    int mr_access_flags,
 						    struct ib_pd *pd,
 						    struct ib_udata *udata);
+	struct ib_mr *             (*reg_user_dma_buf_mr)(
+					struct ib_pd *pd,
+					struct dma_buf *dmabuf,
+					int mr_access_flags,
+					struct ib_udata *udata);
 	int                        (*dereg_mr)(struct ib_mr *mr);
 	struct ib_mr *		   (*alloc_mr)(struct ib_pd *pd,
 					       enum ib_mr_type mr_type,
include/uapi/rdma/ib_user_verbs.h +18 -0
index b6543d73d20a..aad37e4572b5 100644
@@ -95,6 +95,7 @@ enum {
 	IB_USER_VERBS_EX_CMD_CREATE_QP = IB_USER_VERBS_CMD_CREATE_QP,
 	IB_USER_VERBS_EX_CMD_CREATE_FLOW = IB_USER_VERBS_CMD_THRESHOLD,
 	IB_USER_VERBS_EX_CMD_DESTROY_FLOW,
+	IB_USER_VERBS_EX_CMD_REG_DMA_BUF_MR,
 };
 
 /*
@@ -320,6 +321,23 @@ struct ib_uverbs_rereg_mr_resp {
 	__u32 rkey;
 };
 
+struct ib_uverbs_ex_reg_dma_buf_mr {
+	__u64 response;
+	__u32 fd;
+	__u32 access_flags;
+	__u32 pd_handle;
+	__u32 comp_mask;
+};
+
+struct ib_uverbs_ex_reg_dma_buf_mr_resp {
+	__u32 mr_handle;
+	__u32 lkey;
+	__u32 rkey;
+	__u32 comp_mask;
+	__u32 response_length;
+	__u32 reserved;
+};
+
 struct ib_uverbs_dereg_mr {
 	__u32 mr_handle;
 };
Signature · 1 line
1.7.11.2
#5 lore raw

RFC 5/7 IB/mlx5: Implement reg_user_dma_buf_mr

In reply to Haggai Eran

Register DMA-BUF buffers as memory regions using the ib_mr_attach_dmabuf helper function. The code posts the fast registration request on the UMR QP and syncs before returning.

Signed-off-by Haggai Eran <haggaie at mellanox.com>
---
 drivers/infiniband/hw/mlx5/main.c    |  4 ++-
 drivers/infiniband/hw/mlx5/mlx5_ib.h |  4 +++
 drivers/infiniband/hw/mlx5/mr.c      | 51 ++++++++++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+), 1 deletion(-)
drivers/infiniband/hw/mlx5/main.c +3 -1
index f41254f3689a..852be161b68e 100644
@@ -2368,7 +2368,8 @@ static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
 	dev->ib_dev.uverbs_ex_cmd_mask =
 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
-		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP);
+		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
+		(1ull << IB_USER_VERBS_EX_CMD_REG_DMA_BUF_MR);
 
 	dev->ib_dev.query_device	= mlx5_ib_query_device;
 	dev->ib_dev.query_port		= mlx5_ib_query_port;
@@ -2409,6 +2410,7 @@ static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
 	dev->ib_dev.get_dma_mr		= mlx5_ib_get_dma_mr;
 	dev->ib_dev.reg_user_mr		= mlx5_ib_reg_user_mr;
 	dev->ib_dev.rereg_user_mr	= mlx5_ib_rereg_user_mr;
+	dev->ib_dev.reg_user_dma_buf_mr = mlx5_ib_reg_user_dma_buf_mr;
 	dev->ib_dev.dereg_mr		= mlx5_ib_dereg_mr;
 	dev->ib_dev.attach_mcast	= mlx5_ib_mcg_attach;
 	dev->ib_dev.detach_mcast	= mlx5_ib_mcg_detach;
drivers/infiniband/hw/mlx5/mlx5_ib.h +4 -0
index c4a9825828bc..a722dcb367fc 100644
@@ -711,6 +711,10 @@ int mlx5_ib_update_mtt(struct mlx5_ib_mr *mr, u64 start_page_index,
 int mlx5_ib_rereg_user_mr(struct ib_mr *ib_mr, int flags, u64 start,
 			  u64 length, u64 virt_addr, int access_flags,
 			  struct ib_pd *pd, struct ib_udata *udata);
+struct ib_mr *mlx5_ib_reg_user_dma_buf_mr(struct ib_pd *pd,
+					  struct dma_buf *dmabuf,
+					  int mr_access_flags,
+					  struct ib_udata *udata);
 int mlx5_ib_dereg_mr(struct ib_mr *ibmr);
 struct ib_mr *mlx5_ib_alloc_mr(struct ib_pd *pd,
 			       enum ib_mr_type mr_type,
drivers/infiniband/hw/mlx5/mr.c +51 -0

72 lines not on this page · raw message

Signature · 1 line
1.7.11.2
#6 lore raw

RFC 6/7 NVMe: Use genalloc to allocate CMB regions

In reply to Haggai Eran

Register the CMB in a gen_pool dedicated to manage CMB regions. Use the pool to allocate the SQs to make sure they are registered.

Signed-off-by Haggai Eran <haggaie at mellanox.com>
---
 drivers/nvme/host/nvme-pci.h | 24 ++++++++++++++++++++
 drivers/nvme/host/pci.c      | 54 ++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 74 insertions(+), 4 deletions(-)
 create mode 100644 drivers/nvme/host/nvme-pci.h
drivers/nvme/host/nvme-pci.h new file +24 -0
new file mode 100644
index 000000000000..5b29508dc182
@@ -0,0 +1,24 @@
+/*
+ * Copyright ? 2016 Mellanox Technlogies. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ */
+
+#ifndef _NVME_PCI_H
+#define _NVME_PCI_H
+
+#include "nvme.h"
+
+struct nvme_dev;
+
+void *nvme_alloc_cmb(struct nvme_dev *dev, size_t size, dma_addr_t *dma_addr);
+void nvme_free_cmb(struct nvme_dev *dev, void *addr, size_t size);
+
+#endif
drivers/nvme/host/pci.c +50 -4

108 lines not on this page · raw message

Signature · 1 line
1.7.11.2
#7 lore raw

RFC 6/7 NVMe: Use genalloc to allocate CMB regions

Quoted 2 lines · Haggai Eran

Register the CMB in a gen_pool dedicated to manage CMB regions. Use the pool to allocate the SQs to make sure they are registered.

And why would the NVMe driver care if "they are registered"?

Once we start allowing diverse CMB uses (what happened to Jon's patches btw?) genalloc might be a good backend allocator, but without that it's entirely pointless. Also please don't introduce useless header files.

#8 lore raw

RFC 6/7 NVMe: Use genalloc to allocate CMB regions

Quoted 10 lines · Christoph Hellwig
Quoted 2 lines · Haggai Eran

Register the CMB in a gen_pool dedicated to manage CMB regions. Use the pool to allocate the SQs to make sure they are registered.

And why would the NVMe driver care if "they are registered"?

Once we start allowing diverse CMB uses (what happened to Jon's patches btw?) genalloc might be a good backend allocator, but without that it's entirely pointless. Also please don't introduce useless header files.

My concern is using CMB as a generic memory space could lead to invalid uses and untested paths in broken firmware (leading to bricked drives...). The spec defines what it's allowed to be used for, but doesn't really leave it open for general purpose usage outside of that. Because of that I think it needs more hand-holding by the kernel to prevent invalid usage.

What I would like to see is a set of knobs controlling the 'chunks' of memory's usages (controlled through sysfs/configfs?) and the kernel takes care of allocation. My set was going to expose a resource file for WDS/RDS but everything else was unhandled except for its current sqes usage. I think the genalloc could fit into this scheme quite well.

Thoughts?

#9 lore raw

RFC 6/7 NVMe: Use genalloc to allocate CMB regions

Quoted 15 lines · Jon Derrick
Quoted 6 lines · Christoph Hellwig
Quoted 4 lines · Haggai Eran

Register the CMB in a gen_pool dedicated to manage CMB regions. Use the
pool to allocate the SQs to make sure they are registered.

Quoted 8 lines

And why would the NVMe driver care if "they are registered"?

Once we start allowing diverse CMB uses (what happened to Jon's patches
btw?) genalloc might be a good backend allocator, but without that it's entirely pointless.??Also please don't introduce useless header
files.

Quoted 6 lines

My concern is using CMB as a generic memory space could lead to invalid uses and untested paths in broken firmware (leading to bricked drives...). The spec defines what it's allowed to be used for, but doesn't really leave it open for general purpose usage outside of that. Because of that I think it needs more hand-holding by the kernel to prevent invalid usage.

I understand. Did you happen to think of how the kernel would expose it?

Quoted 6 lines

What I would like to see is a set of knobs controlling the 'chunks' of memory's usages (controlled through sysfs/configfs?) and the kernel takes care of allocation. My set was going to expose a resource file for WDS/RDS but everything else was unhandled except for its current sqes usage. I think the genalloc could fit into this scheme quite well.

What do you mean by a resource file? Are you referring to the sysfs/configfs knob or to a file that exposes the resource to user- space?

Thanks,
Haggai

#10 lore raw

RFC 6/7 NVMe: Use genalloc to allocate CMB regions

In reply to Christoph Hellwig
Quoted 6 lines · Christoph Hellwig
Quoted 4 lines · Haggai Eran

Register the CMB in a gen_pool dedicated to manage CMB regions. Use the
pool to allocate the SQs to make sure they are registered.

And why would the NVMe driver care if "they are registered"?

I just meant that they are allocated through genalloc so that it doesn't allocate the same buffer again.

Quoted 5 lines

Once we start allowing diverse CMB uses (what happened to Jon's patches
btw?) genalloc might be a good backend allocator, but without that it's entirely pointless.??

I missed Jon's patches. I'll have a look. The point in this patch was to allow this diverse use in the next patch.

Quoted 2 lines

Also please don't introduce useless header
files.

Fair enough.

Thanks,
Haggai

#11 lore raw

RFC 7/7 NVMe: CMB on DMA-BUF

In reply to Haggai Eran

Allocate a CMB region to user-space as a DMA-BUF object.

Signed-off-by Haggai Eran <haggaie at mellanox.com>
---
 drivers/nvme/host/Makefile      |   2 +-
 drivers/nvme/host/core.c        |  29 ++++
 drivers/nvme/host/dmabuf.c      | 308 ++++++++++++++++++++++++++++++++++++++++
 drivers/nvme/host/nvme-pci.h    |   2 +
 drivers/nvme/host/nvme.h        |   1 +
 drivers/nvme/host/pci.c         |   6 +
 include/uapi/linux/nvme_ioctl.h |  11 ++
 7 files changed, 358 insertions(+), 1 deletion(-)
 create mode 100644 drivers/nvme/host/dmabuf.c
drivers/nvme/host/Makefile +1 -1
index 9a3ca892b4a7..f8d4f5d33398 100644
@@ -5,4 +5,4 @@ nvme-core-y				:= core.o
 nvme-core-$(CONFIG_BLK_DEV_NVME_SCSI)	+= scsi.o
 nvme-core-$(CONFIG_NVM)			+= lightnvm.o
 
-nvme-y					+= pci.o
+nvme-y					+= pci.o dmabuf.o
drivers/nvme/host/core.c +29 -0
index d5fb55c0a9d9..5860b468ab39 100644
@@ -1209,6 +1209,33 @@ out_unlock:
 	return ret;
 }
 
+static int nvme_alloc_user_cmb(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
+			       struct nvme_alloc_user_cmb __user *ucmd)
+{
+	struct nvme_alloc_user_cmb cmd;
+	int status;
+
+	if (!capable(CAP_SYS_ADMIN))
+		return -EACCES;
+	if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
+		return -EFAULT;
+	if (cmd.flags || cmd.rsvd1 || cmd.opcode)
+		return -EINVAL;
+
+	if (!ctrl->ops->alloc_user_cmb)
+		return -ENOTTY;
+
+	status = ctrl->ops->alloc_user_cmb(ctrl, cmd.size);
+	if (status < 0)
+		return status;
+
+	cmd.fd = status;
+	if (copy_to_user(ucmd, &cmd, sizeof(cmd)))
+		return -EFAULT;
+
+	return 0;
+}
+
 static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
 		unsigned long arg)
 {
@@ -1228,6 +1255,8 @@ static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
 	case NVME_IOCTL_RESCAN:
 		nvme_queue_scan(ctrl);
 		return 0;
+	case NVME_IOCTL_ALLOC_USER_CMB:
+		return nvme_alloc_user_cmb(ctrl, NULL, argp);
 	default:
 		return -ENOTTY;
 	}
drivers/nvme/host/dmabuf.c new file +308 -0

308 lines not on this page · raw message

drivers/nvme/host/nvme-pci.h +2 -0
index 5b29508dc182..2292d2c24fda 100644
@@ -18,6 +18,8 @@
 
 struct nvme_dev;
 
+int nvme_pci_alloc_user_cmb(struct nvme_dev *dev, u64 size);
+
 void *nvme_alloc_cmb(struct nvme_dev *dev, size_t size, dma_addr_t *dma_addr);
 void nvme_free_cmb(struct nvme_dev *dev, void *addr, size_t size);
 
drivers/nvme/host/nvme.h +1 -0
index 1daa0482de0e..3a65144f23be 100644
@@ -152,6 +152,7 @@ struct nvme_ctrl_ops {
 	void (*free_ctrl)(struct nvme_ctrl *ctrl);
 	void (*post_scan)(struct nvme_ctrl *ctrl);
 	void (*submit_async_event)(struct nvme_ctrl *ctrl, int aer_idx);
+	int (*alloc_user_cmb)(struct nvme_ctrl *ctrl, u64 size);
 };
 
 static inline bool nvme_ctrl_ready(struct nvme_ctrl *ctrl)
drivers/nvme/host/pci.c +6 -0
index d3da5d9552dd..2a15755e845e 100644
@@ -1947,6 +1947,11 @@ static int nvme_pci_reset_ctrl(struct nvme_ctrl *ctrl)
 	return nvme_reset(to_nvme_dev(ctrl));
 }
 
+static int nvme_pci_alloc_user_cmb_wrapper(struct nvme_ctrl *ctrl, u64 size)
+{
+	return nvme_pci_alloc_user_cmb(to_nvme_dev(ctrl), size);
+}
+
 static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
 	.module			= THIS_MODULE,
 	.reg_read32		= nvme_pci_reg_read32,
@@ -1956,6 +1961,7 @@ static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
 	.free_ctrl		= nvme_pci_free_ctrl,
 	.post_scan		= nvme_pci_post_scan,
 	.submit_async_event	= nvme_pci_submit_async_event,
+	.alloc_user_cmb		= nvme_pci_alloc_user_cmb_wrapper,
 };
 
 static int nvme_dev_map(struct nvme_dev *dev)
include/uapi/linux/nvme_ioctl.h +11 -0
index 50ff21f748b6..d66f5b56b163 100644
@@ -55,6 +55,16 @@ struct nvme_passthru_cmd {
 
 #define nvme_admin_cmd nvme_passthru_cmd
 
+struct nvme_alloc_user_cmb {
+	/* in */
+	__u8	opcode;
+	__u8	flags;
+	__u16	rsvd1;
+	__u64 size;
+	/* out */
+	__u32 fd;
+};
+
 #define NVME_IOCTL_ID		_IO('N', 0x40)
 #define NVME_IOCTL_ADMIN_CMD	_IOWR('N', 0x41, struct nvme_admin_cmd)
 #define NVME_IOCTL_SUBMIT_IO	_IOW('N', 0x42, struct nvme_user_io)
@@ -62,5 +72,6 @@ struct nvme_passthru_cmd {
 #define NVME_IOCTL_RESET	_IO('N', 0x44)
 #define NVME_IOCTL_SUBSYS_RESET	_IO('N', 0x45)
 #define NVME_IOCTL_RESCAN	_IO('N', 0x46)
+#define NVME_IOCTL_ALLOC_USER_CMB _IOWR('N', 0x47, struct nvme_alloc_user_cmb)
 
 #endif /* _UAPI_LINUX_NVME_IOCTL_H */
Signature · 1 line
1.7.11.2
#12 lore raw

RFC 7/7 NVMe: CMB on DMA-BUF

Quoted 1 line · Haggai Eran

Allocate a CMB region to user-space as a DMA-BUF object.

NACK. Absolutely no way we're going to export a low-level device specific feature to userspace like that. If you want to DMA to the CMB do it from the kernel, and for that we'll need a proper block device level API.

#13 lore raw

RFC 7/7 NVMe: CMB on DMA-BUF

Quoted 7 lines · Christoph Hellwig
Quoted 2 lines · Haggai Eran

Allocate a CMB region to user-space as a DMA-BUF object.

NACK.??Absolutely no way we're going to export a low-level device specific feature to userspace like that.??If you want to DMA to the CMB do it from the kernel, and for that we'll need a proper block device level API.

The reason I thought this idea might work is because of the responses Stephen got for his attempt to use ZONE_DEVICE for things like the CMB. The problem with that is that the way RDMA registration works with get_user_pages means that the CPU also gets access to the CMB and sometimes you don't want that. DMA-BUF can solve that nicely in that it doesn't require mapping it to the CPU - a driver can attach to it directly.

Anyway, I understand exposing the CMB directly might be a little too low level. What do you think would work better? Something like direct_access that returns bus addresses instead of pfns?

Thanks,
Haggai

#14 lore raw

RFC 7/7 NVMe: CMB on DMA-BUF

Quoted 7 lines · Haggai Eran

The reason I thought this idea might work is because of the responses Stephen got for his attempt to use ZONE_DEVICE for things like the CMB. The problem with that is that the way RDMA registration works with get_user_pages means that the CPU also gets access to the CMB and sometimes you don't want that. DMA-BUF can solve that nicely in that it doesn't require mapping it to the CPU - a driver can attach to it directly.

I'm not complaining about DMA-BUF - that might be a useful low-level abstraction if we define a higher level API around it.

Quoted 3 lines

Anyway, I understand exposing the CMB directly might be a little too low level. What do you think would work better? Something like direct_access that returns bus addresses instead of pfns?

This would be my primary choicse. We'll really need to built an in-kernel framework for a) PCI P2P access involving drivers for the PCI switches etc, and then on top of that build something speicifly for block drivers. Which will be a bit complicated because both have the case of directly memory mapped device as well as indirect devices like the CMB. I'm pretty sure we won't get it right at the first attempt, so exposing it to userspace is an absolute no-go as that will lock us into the first ABI forever.

#15 lore raw

RFC 7/7 NVMe: CMB on DMA-BUF

Quoted 7 lines · On 8/4/2016 2:56 PM, [email protected] wrote:
Quoted 3 lines

Anyway, I understand exposing the CMB directly might be a little too low level. What do you think would work better? Something like direct_access that returns bus addresses instead of pfns?

This would be my primary choicse. We'll really need to built an in-kernel framework for a) PCI P2P access involving drivers for the PCI switches etc,

I was thinking perhaps the dma_map_resource patches by Will Davis [1] could be used (adding him to the thread).

Quoted 6 lines

and then on top of that build something speicifly for
block drivers. Which will be a bit complicated because both have the case of directly memory mapped device as well as indirect devices like the CMB. I'm pretty sure we won't get it right at the first attempt, so exposing it to userspace is an absolute no-go as that will lock us into the first ABI forever.

I understand. I'll try to think of something for kernel usage only for the next attempt.

Thanks,
Haggai

[1]
https://lists.linuxfoundation.org/pipermail/iommu/2015-August/014106.html

maintainer
#16 lore raw

RFC 7/7 NVMe: CMB on DMA-BUF

In reply to Christoph Hellwig

Hey Christoph, Haggai,

Quoted 6 lines
Quoted 1 line

Allocate a CMB region to user-space as a DMA-BUF object.

NACK. Absolutely no way we're going to export a low-level device specific feature to userspace like that. If you want to DMA to the CMB do it from the kernel, and for that we'll need a proper block device level API.

I think that in this patch, Haggai was aiming to demonstrate the use of dma_buf for peer2peer transactions with an RDMA device. I agree that the CMB example can/should be exposed in the block device level, but I also think we'd like to generalize the way we expose a bar space of any peer device to another peer device.

How would you suggest we do that? I think Haggai's proposal is pretty solid (considering the fact that the vast majority of RDMA applications live in user-space).