Skip to contentSkip to conversation

RDMA: Support CQs with user memory

PATCH Applied Michael Margolin 3 participants 6 messages Jul 8, 2025 – Jul 13, 2025

Subsystems (2)
lore
Share & tools
lore

Michael Margolin

author maintainer
#0 lore raw

v2 cover RDMA: Support CQs with user memory

This series is adding a common way for creating CQs on top of preallocated memory supplied by userspace. The memory buffer can be provided as a virtual address or by dmabuf, in both cases a umem object is created for driver's use.
EFA is the first to support this new interface, new drivers or existing drivers that want/need to get the memory from userspace are expected to use this flow.

It's follow-up of the discussion in [1].

Changelog
=========
Changes from v1 [2]:
- Added a new "create_cq_umem" driver op instead of a flag set by supporting drivers
- Added ib_umem_is_contiguous() and ib_umem_start_dma_addr() helpers in a new patch
- Dropped references to umem scatter-gather list in EFA implementation and using the new helpers that hide the details instead

References
==========
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

Thanks

Michael Margolin (3):
RDMA/uverbs: Add a common way to create CQ with umem RDMA/core: Add umem "is_contiguous" and "start_dma_addr" helpers RDMA/efa: Add CQ with external memory support

 drivers/infiniband/core/device.c              |  1 +
 drivers/infiniband/core/uverbs_std_types_cq.c | 87 +++++++++++++++++--
 drivers/infiniband/hw/efa/efa.h               |  3 +
 drivers/infiniband/hw/efa/efa_main.c          |  1 +
 drivers/infiniband/hw/efa/efa_verbs.c         | 61 ++++++++++---
 include/rdma/ib_umem.h                        | 25 +++++-
 include/rdma/ib_verbs.h                       |  4 +
 include/uapi/rdma/efa-abi.h                   |  3 +-
 include/uapi/rdma/ib_user_ioctl_cmds.h        |  4 +
 9 files changed, 164 insertions(+), 25 deletions(-)
Signature · 1 line
2.47.1
author maintainer
#1 lore raw

PATCH v2 1/3 RDMA/uverbs: Add a common way to create CQ with umem

Add ioctl command attributes and a common handling for the option to create CQs with memory buffers passed from userspace. When required attributes are supplied, create umem and provide it for driver's use. The extension enables creation of CQs on top of preallocated CPU virtual or device memory buffers, by supplying VA or dmabuf fd, in a common way.
Drivers can support this flow by initializing a new create_cq_umem fp field in their ops struct, with a function that can handle the new parameter.

---
 drivers/infiniband/core/device.c              |  1 +
 drivers/infiniband/core/uverbs_std_types_cq.c | 87 +++++++++++++++++--
 include/rdma/ib_verbs.h                       |  4 +
 include/uapi/rdma/ib_user_ioctl_cmds.h        |  4 +
 4 files changed, 90 insertions(+), 6 deletions(-)
drivers/infiniband/core/device.c +1 -0
index 1ca6a9b7ba1a..f301cdce1728 100644
@@ -2728,6 +2728,7 @@ void ib_set_device_ops(struct ib_device *dev, const struct ib_device_ops *ops)
 	SET_DEVICE_OP(dev_ops, create_ah);
 	SET_DEVICE_OP(dev_ops, create_counters);
 	SET_DEVICE_OP(dev_ops, create_cq);
+	SET_DEVICE_OP(dev_ops, create_cq_umem);
 	SET_DEVICE_OP(dev_ops, create_flow);
 	SET_DEVICE_OP(dev_ops, create_qp);
 	SET_DEVICE_OP(dev_ops, create_rwq_ind_table);
drivers/infiniband/core/uverbs_std_types_cq.c +81 -6

118 lines not on this page · raw message

include/rdma/ib_verbs.h +4 -0
index 010594dc755b..5ad0136d43c4 100644
@@ -2486,6 +2486,10 @@ struct ib_device_ops {
 	int (*destroy_qp)(struct ib_qp *qp, struct ib_udata *udata);
 	int (*create_cq)(struct ib_cq *cq, const struct ib_cq_init_attr *attr,
 			 struct uverbs_attr_bundle *attrs);
+	int (*create_cq_umem)(struct ib_cq *cq,
+			      const struct ib_cq_init_attr *attr,
+			      struct ib_umem *umem,
+			      struct uverbs_attr_bundle *attrs);
 	int (*modify_cq)(struct ib_cq *cq, u16 cq_count, u16 cq_period);
 	int (*destroy_cq)(struct ib_cq *cq, struct ib_udata *udata);
 	int (*resize_cq)(struct ib_cq *cq, int cqe, struct ib_udata *udata);
include/uapi/rdma/ib_user_ioctl_cmds.h +4 -0
index ac7b162611ed..5f3e5bee51b2 100644
@@ -105,6 +105,10 @@ enum uverbs_attrs_create_cq_cmd_attr_ids {
 	UVERBS_ATTR_CREATE_CQ_FLAGS,
 	UVERBS_ATTR_CREATE_CQ_RESP_CQE,
 	UVERBS_ATTR_CREATE_CQ_EVENT_FD,
+	UVERBS_ATTR_CREATE_CQ_BUFFER_VA,
+	UVERBS_ATTR_CREATE_CQ_BUFFER_LENGTH,
+	UVERBS_ATTR_CREATE_CQ_BUFFER_FD,
+	UVERBS_ATTR_CREATE_CQ_BUFFER_OFFSET,
 };
 
 enum uverbs_attrs_destroy_cq_cmd_attr_ids {
Signature · 1 line
2.47.1
author maintainer
#2 lore raw

PATCH v2 2/3 RDMA/core: Add umem "is_contiguous" and "start_dma_addr" helpers

In reply to Michael Margolin

In some cases drivers may need to check if a given umem is contiguous. Add a helper function in core code so that drivers don't need to deal with umem or scatter-gather lists structure.
Additionally add a helper for getting umem's start DMA address and use it in other helper functions that open code it.

---
 include/rdma/ib_umem.h | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
include/rdma/ib_umem.h +21 -4
index 7dc7b1cc71b5..0a8e092c0ea8 100644
@@ -52,11 +52,15 @@ static inline int ib_umem_offset(struct ib_umem *umem)
 	return umem->address & ~PAGE_MASK;
 }
 
+static inline dma_addr_t ib_umem_start_dma_addr(struct ib_umem *umem)
+{
+	return sg_dma_address(umem->sgt_append.sgt.sgl) + ib_umem_offset(umem);
+}
+
 static inline unsigned long ib_umem_dma_offset(struct ib_umem *umem,
 					       unsigned long pgsz)
 {
-	return (sg_dma_address(umem->sgt_append.sgt.sgl) + ib_umem_offset(umem)) &
-	       (pgsz - 1);
+	return ib_umem_start_dma_addr(umem) & (pgsz - 1);
 }
 
 static inline size_t ib_umem_num_dma_blocks(struct ib_umem *umem,
@@ -135,14 +139,27 @@ static inline unsigned long ib_umem_find_best_pgoff(struct ib_umem *umem,
 						    unsigned long pgsz_bitmap,
 						    u64 pgoff_bitmask)
 {
-	struct scatterlist *sg = umem->sgt_append.sgt.sgl;
 	dma_addr_t dma_addr;
 
-	dma_addr = sg_dma_address(sg) + (umem->address & ~PAGE_MASK);
+	dma_addr = ib_umem_start_dma_addr(umem);
 	return ib_umem_find_best_pgsz(umem, pgsz_bitmap,
 				      dma_addr & pgoff_bitmask);
 }
 
+static inline bool ib_umem_is_contiguous(struct ib_umem *umem)
+{
+	dma_addr_t dma_addr;
+	unsigned long pgsz;
+
+	/*
+	 * Select the smallest aligned page that can contain the whole umem if
+	 * it was contiguous.
+	 */
+	dma_addr = ib_umem_start_dma_addr(umem);
+	pgsz = roundup_pow_of_two((dma_addr ^ (umem->length - 1 + dma_addr)) + 1);
+	return !!ib_umem_find_best_pgoff(umem, pgsz, U64_MAX);
+}
+
 struct ib_umem_dmabuf *ib_umem_dmabuf_get(struct ib_device *device,
 					  unsigned long offset, size_t size,
 					  int fd, int access,
Signature · 1 line
2.47.1
author maintainer
#3 lore raw

PATCH v2 3/3 RDMA/efa: Add CQ with external memory support

In reply to Michael Margolin

Add an option to create CQ using external memory instead of allocating in the driver. The memory can be passed from userspace by dmabuf fd and an offset or a VA. One of the possible usages is creating CQs that reside in accelerator memory, allowing low latency asynchronous direct polling from the accelerator device. Add a capability bit to reflect on the feature support.

---
 drivers/infiniband/hw/efa/efa.h       |  3 ++
 drivers/infiniband/hw/efa/efa_main.c  |  1 +
 drivers/infiniband/hw/efa/efa_verbs.c | 61 +++++++++++++++++++++------
 include/uapi/rdma/efa-abi.h           |  3 +-
 4 files changed, 53 insertions(+), 15 deletions(-)
drivers/infiniband/hw/efa/efa.h +3 -0
index 838182d0409c..3d49c1db928e 100644
@@ -107,6 +107,7 @@ struct efa_cq {
 	u16 cq_idx;
 	/* NULL when no interrupts requested */
 	struct efa_eq *eq;
+	struct ib_umem *umem;
 };
 
 struct efa_qp {
@@ -162,6 +163,8 @@ int efa_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *init_attr,
 int efa_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata);
 int efa_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr,
 		  struct uverbs_attr_bundle *attrs);
+int efa_create_cq_umem(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr,
+		       struct ib_umem *umem, struct uverbs_attr_bundle *attrs);
 struct ib_mr *efa_reg_mr(struct ib_pd *ibpd, u64 start, u64 length,
 			 u64 virt_addr, int access_flags,
 			 struct ib_udata *udata);
drivers/infiniband/hw/efa/efa_main.c +1 -0
index 4f03c0ec819f..6c415b9adb5f 100644
@@ -372,6 +372,7 @@ static const struct ib_device_ops efa_dev_ops = {
 	.alloc_pd = efa_alloc_pd,
 	.alloc_ucontext = efa_alloc_ucontext,
 	.create_cq = efa_create_cq,
+	.create_cq_umem = efa_create_cq_umem,
 	.create_qp = efa_create_qp,
 	.create_user_ah = efa_create_ah,
 	.dealloc_pd = efa_dealloc_pd,
drivers/infiniband/hw/efa/efa_verbs.c +47 -14

109 lines not on this page · raw message

include/uapi/rdma/efa-abi.h +2 -1
index 11b94b0b035b..98b71b9979f8 100644
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) */
 /*
- * Copyright 2018-2024 Amazon.com, Inc. or its affiliates. All rights reserved.
+ * Copyright 2018-2025 Amazon.com, Inc. or its affiliates. All rights reserved.
  */
 
 #ifndef EFA_ABI_USER_H
@@ -131,6 +131,7 @@ enum {
 	EFA_QUERY_DEVICE_CAPS_DATA_POLLING_128 = 1 << 4,
 	EFA_QUERY_DEVICE_CAPS_RDMA_WRITE = 1 << 5,
 	EFA_QUERY_DEVICE_CAPS_UNSOLICITED_WRITE_RECV = 1 << 6,
+	EFA_QUERY_DEVICE_CAPS_CQ_WITH_EXT_MEM = 1 << 7,
 };
 
 struct efa_ibv_ex_query_device_resp {
Signature · 1 line
2.47.1
maintainer Reviewed-by on the v2 series
#4 lore raw
In reply to Michael Margolin
Quoted 25 lines · Michael Margolin

This series is adding a common way for creating CQs on top of preallocated memory supplied by userspace. The memory buffer can be provided as a virtual address or by dmabuf, in both cases a umem object is created for driver's use.
EFA is the first to support this new interface, new drivers or existing drivers that want/need to get the memory from userspace are expected to use this flow.

It's follow-up of the discussion in [1].

Changelog
=========
Changes from v1 [2]:
- Added a new "create_cq_umem" driver op instead of a flag set by supporting drivers
- Added ib_umem_is_contiguous() and ib_umem_start_dma_addr() helpers in a new patch
- Dropped references to umem scatter-gather list in EFA implementation and using the new helpers that hide the details instead

References
==========
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

Quoted 6 lines

Thanks

Michael Margolin (3):
RDMA/uverbs: Add a common way to create CQ with umem RDMA/core: Add umem "is_contiguous" and "start_dma_addr" helpers RDMA/efa: Add CQ with external memory support

It looks Ok to me

Jason

#5 lore raw
In reply to Michael Margolin
Quoted 9 lines · Michael Margolin

This series is adding a common way for creating CQs on top of preallocated memory supplied by userspace. The memory buffer can be provided as a virtual address or by dmabuf, in both cases a umem object is created for driver's use.
EFA is the first to support this new interface, new drivers or existing drivers that want/need to get the memory from userspace are expected to use this flow.

[...]

Applied, thanks!

[1/3] RDMA/uverbs: Add a common way to create CQ with umem

      https://git.kernel.org/rdma/rdma/c/1a40c362ae265c
[2/3] RDMA/core: Add umem "is_contiguous" and "start_dma_addr" helpers
      https://git.kernel.org/rdma/rdma/c/c897c2c8b8e829
[3/3] RDMA/efa: Add CQ with external memory support
      https://git.kernel.org/rdma/rdma/c/9fb3dd85197f5e

Best regards,

Signature · 1 line
Leon Romanovsky <[email protected]>