Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Eric Davis" <eric.davis@broadcom.com>
Cc: linux-rdma@vger.kernel.org
Subject: Re: [PATCH rdma-next v3 13/13] RDMA/core: attach a memory region to a queue pair
Date: Tue, 10 Nov 2026 06:20:18 +0000	[thread overview]
Message-ID: <sashiko-outbox-206403@kernel.org> (raw)
In-Reply-To: <20261109-uet-ib-core-v3-13-3e7e7ffed2e9@broadcom.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] RDMA/core: mr->usecnt release ordering race in QP_DETACH_MR
  and destroy_qp
--

commit beeb942760f9f2e71c333c8b383858123a5c467f
Author: Eric Davis <eric.davis@broadcom.com>

RDMA/core: attach a memory region to a queue pair

This commit adds uverbs methods and driver callbacks to attach a user
memory region to a single RU queue pair and detach it again.

> diff --git a/drivers/infiniband/core/uverbs_std_types_qp.c b/drivers/infiniband/core/uverbs_std_types_qp.c
> index a1112083fb73f..88a6050d1d046 100644
> --- a/drivers/infiniband/core/uverbs_std_types_qp.c
> +++ b/drivers/infiniband/core/uverbs_std_types_qp.c
> @@ -683,12 +683,126 @@ DECLARE_UVERBS_NAMED_METHOD(
>  			     enum ib_uverbs_qp_attach_comp_cntr_op,
>  			     UA_MANDATORY));
>  
> +/* bind a region to the QP that will use it */
> +static int UVERBS_HANDLER(UVERBS_METHOD_QP_ATTACH_MR)(
> +	struct uverbs_attr_bundle *attrs)
> +{
[ ... ]
> +	/* the QP holds the region until detach or its own destroy */
> +	mr->attach_id = id;
> +	atomic_inc(&mr->usecnt);
> +	/* the MR's read lock drops with a relaxed atomic, order the hold
> +	 * before it so a dereg that gets in next sees the region in use
> +	 */
> +	smp_mb__after_atomic();
> +
> +	return 0;
> +
> +err_xa:
> +	xa_erase(&qp->attached_mrs, id);
> +err_claim:
> +	WRITE_ONCE(mr->attached_qp, NULL);
> +	return ret;
> +}
> +
> +static int UVERBS_HANDLER(UVERBS_METHOD_QP_DETACH_MR)(
> +	struct uverbs_attr_bundle *attrs)
> +{
> +	struct ib_qp *qp =
> +		uverbs_attr_get_obj(attrs, UVERBS_ATTR_QP_DETACH_MR_QP_HANDLE);
> +	struct ib_mr *mr =
> +		uverbs_attr_get_obj(attrs, UVERBS_ATTR_QP_DETACH_MR_MR_HANDLE);
> +	int ret;
> +
> +	if (!qp->device->ops.qp_detach_mr)
> +		return -EOPNOTSUPP;
> +
> +	if (qp->real_qp != qp)
> +		return -EINVAL;
> +
> +	if (READ_ONCE(mr->attached_qp) != qp)
> +		return -EINVAL;
> +
> +	ret = qp->device->ops.qp_detach_mr(qp, mr);
> +	if (ret)
> +		return ret;
> +
> +	xa_erase(&qp->attached_mrs, mr->attach_id);
> +	WRITE_ONCE(mr->attached_qp, NULL);
> +	/* once usecnt reads zero the MR can be freed under us */
> +	smp_mb__before_atomic();
> +	atomic_dec(&mr->usecnt);
> +
> +	return 0;
> +}

[Severity: Medium]
Can uverbs_free_mr() observe a stale non-zero mr->usecnt after
UVERBS_METHOD_QP_DETACH_MR or ib_destroy_qp_user() detaches the region and
spuriously fail with -EBUSY?

When UVERBS_METHOD_QP_DETACH_MR finishes, rdma_lookup_put_uobject() drops
UVERBS_LOOKUP_READ on the MR with a relaxed atomic_dec(&uobj->usecnt).
Because UVERBS_METHOD_QP_DETACH_MR places smp_mb__before_atomic() before
atomic_dec(&mr->usecnt) instead of smp_mb__after_atomic() after it, the
two relaxed atomic_dec() calls can be reordered on weakly-ordered
architectures:

CPU0                                  CPU1
UVERBS_METHOD_QP_DETACH_MR
  WRITE_ONCE(mr->attached_qp, NULL)
  smp_mb__before_atomic()
  [atomic_dec(&mr->usecnt) delayed]
  rdma_lookup_put_uobject(mr_uobj)
    atomic_dec(&uobj->usecnt)
                                      uverbs_free_mr()
                                        rdma_lookup_get_uobject()
                                          atomic_cmpxchg(&uobj->usecnt, 0, -1)
                                        atomic_read(&mr->usecnt) -> 1
                                        return -EBUSY;  <-- spurious -EBUSY
  atomic_dec(&mr->usecnt)

In addition, both UVERBS_METHOD_QP_DETACH_MR and ib_destroy_qp_user()
clear mr->attached_qp before decrementing mr->usecnt.  Once
mr->attached_qp is NULL, another thread can attach the MR to a second QP
(which only takes UVERBS_ACCESS_READ on the MR), detach it, and call
uverbs_free_mr() before the first thread runs atomic_dec(&mr->usecnt),
again seeing mr->usecnt == 1 and failing with -EBUSY.

Decrementing mr->usecnt with atomic_dec_return_release() before
smp_store_release(&mr->attached_qp, NULL) in both detach paths, paired
with smp_mb__after_atomic() in UVERBS_METHOD_QP_DETACH_MR, would close
both windows.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261109-uet-ib-core-v3-0-3e7e7ffed2e9@broadcom.com?part=13

  reply	other threads:[~2026-11-10  6:20 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-10  4:00 [PATCH rdma-next v3 00/13] RDMA/core: Ultra Ethernet transport verbs Eric Davis via B4 Relay
2026-11-10  4:00 ` [PATCH rdma-next v3 01/13] RDMA/core: widen the uverbs object key Eric Davis via B4 Relay
2026-11-10  4:10   ` sashiko-bot
2026-11-10  4:00 ` [PATCH rdma-next v3 02/13] RDMA/core: add the UET GID types Eric Davis via B4 Relay
2026-11-10  4:21   ` sashiko-bot
2026-11-10  4:00 ` [PATCH rdma-next v3 03/13] RDMA/core: report the reliable unconnected device capabilities Eric Davis via B4 Relay
2026-11-10  4:25   ` sashiko-bot
2026-11-10  4:00 ` [PATCH rdma-next v3 04/13] RDMA/core: add the reliable unconnected queue pair type Eric Davis via B4 Relay
2026-11-10  4:36   ` sashiko-bot
2026-11-10  4:00 ` [PATCH rdma-next v3 05/13] RDMA/core: extended address handle attributes Eric Davis via B4 Relay
2026-11-10  4:45   ` sashiko-bot
2026-11-10  4:00 ` [PATCH rdma-next v3 06/13] RDMA/core: query the semantics a queue pair type guarantees Eric Davis via B4 Relay
2026-11-10  4:49   ` sashiko-bot
2026-11-10  4:00 ` [PATCH rdma-next v3 07/13] RDMA/core: job and job key objects Eric Davis via B4 Relay
2026-11-10  4:59   ` sashiko-bot
2026-11-10  4:00 ` [PATCH rdma-next v3 08/13] RDMA/uverbs: factor out the queue pair attribute copy Eric Davis via B4 Relay
2026-11-10  5:04   ` sashiko-bot
2026-11-10  4:00 ` [PATCH rdma-next v3 09/13] RDMA/core: create a queue pair ready to send Eric Davis via B4 Relay
2026-11-10  5:14   ` sashiko-bot
2026-11-10  4:00 ` [PATCH rdma-next v3 10/13] RDMA/core: queue pair attributes for unconnected transports Eric Davis via B4 Relay
2026-11-10  5:29   ` sashiko-bot
2026-11-11 16:45     ` Eric Davis
2026-11-10  4:00 ` [PATCH rdma-next v3 11/13] RDMA/core: extended memory registration Eric Davis via B4 Relay
2026-11-10  5:55   ` sashiko-bot
2026-11-11 16:46     ` Eric Davis
2026-11-10  4:00 ` [PATCH rdma-next v3 12/13] RDMA/core: 64-bit memory keys Eric Davis via B4 Relay
2026-11-10  6:01   ` sashiko-bot
2026-11-10  4:00 ` [PATCH rdma-next v3 13/13] RDMA/core: attach a memory region to a queue pair Eric Davis via B4 Relay
2026-11-10  6:20   ` sashiko-bot [this message]
2026-11-11 16:49     ` Eric Davis

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=sashiko-outbox-206403@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=eric.davis@broadcom.com \
    --cc=linux-rdma@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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