From: Michael Margolin <mrgolin@amazon.com>
To: <jgg@nvidia.com>, <leon@kernel.org>, <linux-rdma@vger.kernel.org>
Cc: <sleybo@amazon.com>, <matua@amazon.com>, <gal.pressman@linux.dev>,
"Yonatan Nachum" <ynachum@amazon.com>
Subject: [PATCH for-next v2 2/5] RDMA/core: Prevent destroying in-use completion counters
Date: Thu, 16 Apr 2026 21:23:24 +0000 [thread overview]
Message-ID: <20260416212327.18191-3-mrgolin@amazon.com> (raw)
In-Reply-To: <20260416212327.18191-1-mrgolin@amazon.com>
Reject comp_cntr destroy while it is attached to any QP. Track
attachments using an xarray in ib_qp keyed by the attach op_mask.
Use op bitmask to reject overlapping attaches early.
Reviewed-by: Yonatan Nachum <ynachum@amazon.com>
Signed-off-by: Michael Margolin <mrgolin@amazon.com>
---
.../core/uverbs_std_types_comp_cntr.c | 3 +++
drivers/infiniband/core/uverbs_std_types_qp.c | 22 ++++++++++++++++++-
drivers/infiniband/core/verbs.c | 1 +
include/rdma/ib_verbs.h | 3 +++
4 files changed, 28 insertions(+), 1 deletion(-)
diff --git a/drivers/infiniband/core/uverbs_std_types_comp_cntr.c b/drivers/infiniband/core/uverbs_std_types_comp_cntr.c
index 7651a565bb9f..6fd9f485692d 100644
--- a/drivers/infiniband/core/uverbs_std_types_comp_cntr.c
+++ b/drivers/infiniband/core/uverbs_std_types_comp_cntr.c
@@ -15,6 +15,9 @@ static int uverbs_free_comp_cntr(struct ib_uobject *uobject, enum rdma_remove_re
struct ib_comp_cntr *cc = uobject->object;
int ret;
+ if (atomic_read(&cc->usecnt))
+ return -EBUSY;
+
ret = cc->device->ops.destroy_comp_cntr(cc);
if (ret)
return ret;
diff --git a/drivers/infiniband/core/uverbs_std_types_qp.c b/drivers/infiniband/core/uverbs_std_types_qp.c
index 2c607b02d9d5..d4e214c56de9 100644
--- a/drivers/infiniband/core/uverbs_std_types_qp.c
+++ b/drivers/infiniband/core/uverbs_std_types_qp.c
@@ -15,6 +15,8 @@ static int uverbs_free_qp(struct ib_uobject *uobject,
struct ib_qp *qp = uobject->object;
struct ib_uqp_object *uqp =
container_of(uobject, struct ib_uqp_object, uevent.uobject);
+ struct ib_comp_cntr *cc;
+ unsigned long index;
int ret;
/*
@@ -35,6 +37,10 @@ static int uverbs_free_qp(struct ib_uobject *uobject,
if (ret)
return ret;
+ xa_for_each(&qp->comp_cntrs, index, cc)
+ atomic_dec(&cc->usecnt);
+ xa_destroy(&qp->comp_cntrs);
+
if (uqp->uxrcd)
atomic_dec(&uqp->uxrcd->refcnt);
@@ -392,7 +398,21 @@ static int UVERBS_HANDLER(UVERBS_METHOD_QP_ATTACH_COMP_CNTR)(
if (ret)
return ret;
- return qp->device->ops.qp_attach_comp_cntr(qp, cc, &attr);
+ if (attr.op_mask & qp->comp_cntr_op_mask)
+ return -EBUSY;
+
+ ret = qp->device->ops.qp_attach_comp_cntr(qp, cc, &attr);
+ if (ret)
+ return ret;
+
+ ret = xa_err(xa_store(&qp->comp_cntrs, attr.op_mask, cc, GFP_KERNEL));
+ if (ret)
+ return ret;
+
+ atomic_inc(&cc->usecnt);
+ qp->comp_cntr_op_mask |= attr.op_mask;
+
+ return 0;
}
DECLARE_UVERBS_NAMED_METHOD(
diff --git a/drivers/infiniband/core/verbs.c b/drivers/infiniband/core/verbs.c
index bac87de9cc67..df9a1bb9ece4 100644
--- a/drivers/infiniband/core/verbs.c
+++ b/drivers/infiniband/core/verbs.c
@@ -1293,6 +1293,7 @@ static struct ib_qp *create_qp(struct ib_device *dev, struct ib_pd *pd,
qp->qp_context = attr->qp_context;
spin_lock_init(&qp->mr_lock);
+ xa_init(&qp->comp_cntrs);
INIT_LIST_HEAD(&qp->rdma_mrs);
INIT_LIST_HEAD(&qp->sig_mrs);
init_completion(&qp->srq_completion);
diff --git a/include/rdma/ib_verbs.h b/include/rdma/ib_verbs.h
index b0db80447bf0..02f2e4dfd1c1 100644
--- a/include/rdma/ib_verbs.h
+++ b/include/rdma/ib_verbs.h
@@ -1754,6 +1754,7 @@ struct ib_comp_cntr {
struct ib_umem *err_umem;
u64 comp_count_max_value;
u64 err_count_max_value;
+ atomic_t usecnt;
};
enum ib_comp_cntr_entry {
@@ -1944,6 +1945,8 @@ struct ib_qp {
struct completion srq_completion;
struct ib_xrcd *xrcd; /* XRC TGT QPs only */
struct list_head xrcd_list;
+ struct xarray comp_cntrs; /* op_mask -> comp_cntr */
+ u32 comp_cntr_op_mask;
/* count times opened, mcast attaches, flow attaches */
atomic_t usecnt;
--
2.47.3
next prev parent reply other threads:[~2026-04-16 21:23 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-16 21:23 [PATCH for-next v2 0/5] Introduce Completion Counters Michael Margolin
2026-04-16 21:23 ` [PATCH for-next v2 1/5] RDMA/core: Add Completion Counters support Michael Margolin
2026-04-16 21:23 ` Michael Margolin [this message]
2026-04-16 21:23 ` [PATCH for-next v2 3/5] RDMA/core: Add Completion Counters to resource tracking Michael Margolin
2026-04-16 21:23 ` [PATCH for-next v2 4/5] RDMA/efa: Update device interface Michael Margolin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260416212327.18191-3-mrgolin@amazon.com \
--to=mrgolin@amazon.com \
--cc=gal.pressman@linux.dev \
--cc=jgg@nvidia.com \
--cc=leon@kernel.org \
--cc=linux-rdma@vger.kernel.org \
--cc=matua@amazon.com \
--cc=sleybo@amazon.com \
--cc=ynachum@amazon.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox