[PATCH rdma-next v3 5/6] RDMA/irdma: Support UMEM attribute for CQ creation

From: David Hu

Date: Fri Oct 16 2026 - 16:30:39 EST


From: David Hu <xuehaohu@xxxxxxxxxx>

Support the standard 1-step queue buffer creation model for CQ by
`UVERBS_ATTR_CREATE_CQ_BUF_UMEM`, `IRDMA_IB_ATTR_CREATE_CQ_SHADOW_BUF_UMEM`
via `ib_umem_get_attr()`.

Set up hardware pble directly from the pinned CQ and shadow umems via
`irdma_setup_q_pbl_from_umem()`.

Proper umem and pble cleanup are added into existing `irdma_cq_free_rsrc()`
upon CQ destruction.

We maintain backward compatibility for legacy 2-step registration if UMEM
attributes are not provided.

`irdma_resize_cq()` will have a possibility to deal with a pble/mr that
is created via one-step logic, after this change. Therefore, a proper
cleanup is added.

Signed-off-by: David Hu <xuehaohu@xxxxxxxxxx>
---
drivers/infiniband/hw/irdma/verbs.c | 129 +++++++++++++++++++++++-----
1 file changed, 108 insertions(+), 21 deletions(-)

diff --git a/drivers/infiniband/hw/irdma/verbs.c b/drivers/infiniband/hw/irdma/verbs.c
index 4b7e9c2d9cff..02ed7ab99e48 100644
--- a/drivers/infiniband/hw/irdma/verbs.c
+++ b/drivers/infiniband/hw/irdma/verbs.c
@@ -2028,13 +2028,24 @@ static void irdma_cq_free_rsrc(struct irdma_pci_f *rf, struct irdma_cq *iwcq)
if (iwcq->iwpbl) {
iwmr = iwcq->iwpbl->iwmr;

- refcount_dec(&iwmr->user_ring_refs);
+ if (iwmr && refcount_dec_and_test(&iwmr->user_ring_refs)) {
+ ib_umem_release(iwmr->region);
+ if (iwcq->iwpbl->pbl_allocated)
+ irdma_free_pble(rf->pble_rsrc, &iwcq->iwpbl->pble_alloc);
+ kfree(iwmr);
+ }
}

if (iwcq->iwpbl_shadow) {
iwmr = iwcq->iwpbl_shadow->iwmr;

- refcount_dec(&iwmr->user_ring_refs);
+ if (iwmr && refcount_dec_and_test(&iwmr->user_ring_refs)) {
+ ib_umem_release(iwmr->region);
+ if (iwcq->iwpbl_shadow->pbl_allocated)
+ irdma_free_pble(rf->pble_rsrc,
+ &iwcq->iwpbl_shadow->pble_alloc);
+ kfree(iwmr);
+ }
}
}

@@ -2161,6 +2172,7 @@ static int irdma_resize_cq(struct ib_cq *ibcq, unsigned int entries,
struct irdma_dma_mem kmem_buf;
struct irdma_cq_mr *cqmr_buf;
struct irdma_pbl *iwpbl_buf = NULL;
+ struct irdma_pbl *old_iwpbl = NULL;
struct irdma_device *iwdev;
struct irdma_pci_f *rf;
struct irdma_cq_buf *cq_buf = NULL;
@@ -2273,17 +2285,12 @@ static int irdma_resize_cq(struct ib_cq *ibcq, unsigned int entries,

spin_lock_irqsave(&iwcq->lock, flags);
if (udata) {
- struct irdma_pbl *old_iwpbl = iwcq->iwpbl;
+ old_iwpbl = iwcq->iwpbl;

/* Only update if the resize was successful. Otherwise, HW is
* still pointing to the old PBL.
*/
iwcq->iwpbl = iwpbl_buf;
- if (old_iwpbl) {
- struct irdma_mr *old_iwmr = old_iwpbl->iwmr;
-
- refcount_dec(&old_iwmr->user_ring_refs);
- }
}
if (cq_buf) {
cq_buf->kmem_buf = iwcq->kmem;
@@ -2298,6 +2305,21 @@ static int irdma_resize_cq(struct ib_cq *ibcq, unsigned int entries,
ibcq->cqe = info.cq_size - 1;
spin_unlock_irqrestore(&iwcq->lock, flags);

+ /* Old pble/umem is cleaned up outside spinlock's atomic context
+ * since both of them take sleepable lock.
+ */
+ if (old_iwpbl) {
+ struct irdma_mr *old_iwmr = old_iwpbl->iwmr;
+
+ if (old_iwmr && refcount_dec_and_test(&old_iwmr->user_ring_refs)) {
+ ib_umem_release(old_iwmr->region);
+ if (old_iwpbl->pbl_allocated)
+ irdma_free_pble(rf->pble_rsrc,
+ &old_iwpbl->pble_alloc);
+ kfree(old_iwmr);
+ }
+ }
+
return 0;
error:
if (iwpbl_buf) {
@@ -2746,14 +2768,18 @@ static int irdma_setup_umode_cq(struct irdma_device *iwdev,
struct irdma_cq_init_info *info,
struct uverbs_attr_bundle *attrs)
{
+ struct irdma_cq_uk_init_info *ukinfo = &info->cq_uk_init_info;
struct ib_udata *udata = &attrs->driver_udata;
struct irdma_create_cq_req req = {};
struct irdma_pci_f *rf = iwdev->rf;
+ struct irdma_sc_dev *dev = &rf->sc_dev;
struct irdma_ucontext *ucontext;
struct irdma_cq_mr *cqmr_shadow;
struct irdma_cq_mr *cqmr;
unsigned long flags;
+ bool cqe_64byte_ena;
int err_code;
+ u8 cqe_size;

iwcq->user_mode = true;
ucontext = rdma_udata_to_drv_context(udata, struct irdma_ucontext,
@@ -2767,26 +2793,87 @@ static int irdma_setup_umode_cq(struct irdma_device *iwdev,
if (err_code)
return err_code;

- spin_lock_irqsave(&ucontext->cq_reg_mem_list_lock, flags);
- iwcq->iwpbl = irdma_get_pbl((unsigned long)req.user_cq_buf,
- &ucontext->cq_reg_mem_list);
- spin_unlock_irqrestore(&ucontext->cq_reg_mem_list_lock, flags);
- if (!iwcq->iwpbl)
- return -EPROTO;
+ cqe_64byte_ena = dev->hw_attrs.uk_attrs.feature_flags & IRDMA_FEATURE_64_BYTE_CQE ?
+ true : false;
+ cqe_size = cqe_64byte_ena ? 64 : 32;
+
+ if (uverbs_attr_is_valid(attrs, UVERBS_ATTR_CREATE_CQ_BUF_UMEM)) {
+ struct ib_umem *umem;
+ struct irdma_mem_reg_req cq_req = {};
+ u8 shadow_pgcnt = (rf->sc_dev.hw_attrs.uk_attrs.feature_flags &
+ IRDMA_FEATURE_CQ_RESIZE) ? 0 : IRDMA_SHADOW_PGCNT;
+ u32 min_size;
+
+ cq_req.cq_pages = DIV_ROUND_UP(ukinfo->cq_size * cqe_size,
+ IRDMA_HW_PAGE_SIZE);
+ min_size = (cq_req.cq_pages + shadow_pgcnt) << IRDMA_HW_PAGE_SHIFT;
+
+ umem = ib_umem_get_cq_buf(&iwdev->ibdev, attrs, min_size,
+ IB_ACCESS_LOCAL_WRITE);
+ if (IS_ERR(umem))
+ return PTR_ERR(umem);
+
+ iwcq->iwpbl = irdma_setup_q_pbl_from_umem(iwdev, umem, NULL,
+ IRDMA_MEMREG_TYPE_CQ, &cq_req);
+ if (IS_ERR(iwcq->iwpbl)) {
+ err_code = PTR_ERR(iwcq->iwpbl);
+ iwcq->iwpbl = NULL;
+ ib_umem_release(umem);
+ return err_code;
+ }
+ } else {
+ if (!req.user_cq_buf)
+ return -EINVAL;
+
+ spin_lock_irqsave(&ucontext->cq_reg_mem_list_lock, flags);
+ iwcq->iwpbl = irdma_get_pbl((unsigned long)req.user_cq_buf,
+ &ucontext->cq_reg_mem_list);
+ spin_unlock_irqrestore(&ucontext->cq_reg_mem_list_lock, flags);
+ if (!iwcq->iwpbl)
+ return -EPROTO;
+ }

cqmr = &iwcq->iwpbl->cq_mr;

if (rf->sc_dev.hw_attrs.uk_attrs.feature_flags &
IRDMA_FEATURE_CQ_RESIZE) {
- spin_lock_irqsave(&ucontext->cq_reg_mem_list_lock, flags);
- iwcq->iwpbl_shadow =
- irdma_get_pbl((unsigned long)req.user_shadow_area,
- &ucontext->cq_reg_mem_list);
- spin_unlock_irqrestore(&ucontext->cq_reg_mem_list_lock, flags);
+ if (uverbs_attr_is_valid(attrs, IRDMA_IB_ATTR_CREATE_CQ_SHADOW_BUF_UMEM)) {
+ struct ib_umem *shadow_umem;
+ struct irdma_mem_reg_req shadow_req = {
+ .cq_pages = IRDMA_SHADOW_PGCNT,
+ };
+
+ shadow_umem =
+ ib_umem_get_attr(&iwdev->ibdev, attrs,
+ IRDMA_IB_ATTR_CREATE_CQ_SHADOW_BUF_UMEM,
+ IRDMA_SHADOW_AREA_SIZE << 3,
+ IB_ACCESS_LOCAL_WRITE);
+ if (IS_ERR(shadow_umem))
+ return PTR_ERR(shadow_umem);
+
+ iwcq->iwpbl_shadow =
+ irdma_setup_q_pbl_from_umem(iwdev, shadow_umem, NULL,
+ IRDMA_MEMREG_TYPE_CQ,
+ &shadow_req);
+ if (IS_ERR(iwcq->iwpbl_shadow)) {
+ err_code = PTR_ERR(iwcq->iwpbl_shadow);
+ iwcq->iwpbl_shadow = NULL;
+ ib_umem_release(shadow_umem);
+ return err_code;
+ }
+ } else {
+ if (!req.user_shadow_area)
+ return -EINVAL;

- if (!iwcq->iwpbl_shadow)
- return -EPROTO;
+ spin_lock_irqsave(&ucontext->cq_reg_mem_list_lock, flags);
+ iwcq->iwpbl_shadow =
+ irdma_get_pbl((unsigned long)req.user_shadow_area,
+ &ucontext->cq_reg_mem_list);
+ spin_unlock_irqrestore(&ucontext->cq_reg_mem_list_lock, flags);

+ if (!iwcq->iwpbl_shadow)
+ return -EPROTO;
+ }
cqmr_shadow = &iwcq->iwpbl_shadow->cq_mr;
info->shadow_area_pa = cqmr_shadow->cq_pbl.addr;
} else {
--
2.56.0.176.g3575e65682-goog