All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Stefan Metzmacher" <metze@samba.org>, linux-rdma@vger.kernel.org
Cc: "Chuck Lever" <cel@kernel.org>,
	"Jeff Layton" <jlayton@kernel.org>,
	"Anna Schumaker" <anna@kernel.org>
Subject: Re: [PATCH 3/3] RDMA/cma: reject a pending IB/RoCE connect in rdma_disconnect()
Date: Wed, 02 Dec 2026 13:02:15 +0000	[thread overview]
Message-ID: <sashiko-outbox-245835@kernel.org> (raw)
In-Reply-To: <f4db1dba1e6a98cd357fc9ff66164349cd16fbc6.1796215022.git.metze@samba.org>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] RDMA/cma: unhandled IB_CM_TIMEWAIT in
  cma_ib_abort_pending_connect()
- [Medium] RDMA/cma: unconditional cma_work alloc in
  cma_ib_abort_pending_connect()
- [Medium] RDMA/cma: race in cma_ib_abort_pending_connect() drops REP
  event without QP
- [Low] RDMA/cma: unconditional trace_cm_send_rej() in
  cma_ib_abort_pending_connect()
--

commit dcca139b10a4d4a837fd675d7aac97c93bfbb127
Author: Stefan Metzmacher <metze@samba.org>

RDMA/cma: reject a pending IB/RoCE connect in rdma_disconnect()

This commit rejects a pending active IB/RoCE connect in rdma_disconnect()
via ib_cm_reject_pending_connect() and reports RDMA_CM_EVENT_CONNECT_ERROR
with -ECONNABORTED instead of forcing RDMA_CM_EVENT_DISCONNECTED.

> diff --git a/drivers/infiniband/core/cma.c b/drivers/infiniband/core/cma.c
> index 337a49d1acf72..077a98cd2caae 100644
> --- a/drivers/infiniband/core/cma.c
> +++ b/drivers/infiniband/core/cma.c
> @@ -4846,6 +4846,114 @@ static int cma_force_disconnect(struct rdma_id_private *id_priv)
>  	return 0;
>  }
>  

[ ... ]

> +/*
> + * Returns true if rdma_disconnect() is done with *ret, false if the
> + * connection is established (or closing) and needs a DREQ or DREP.
> + */
> +static bool cma_ib_abort_pending_connect(struct rdma_id_private *id_priv,
> +					 int *ret)
> +{
> +	struct ib_cm_id *cm_id = id_priv->cm_id.ib;
> +	enum ib_cm_state old_state;
> +	struct cma_work *work;
> +
> +	/*
> +	 * Allocated before the REJ, so that a rejected connect is
> +	 * always reported.
> +	 */
> +	work = cma_alloc_abort_work();
> +	if (!work) {
> +		*ret = -ENOMEM;
> +		return true;
> +	}

[Severity: Medium]
Does calling cma_alloc_abort_work() unconditionally here allocate and
immediately free an unused struct cma_work on every normal disconnect of
an established IB/RoCE connection?  If that allocation fails under memory
pressure on an established connection, rdma_disconnect() returns -ENOMEM
after moving the QP to IB_QPS_ERR without sending a DREQ or DREP.

Also, in the IB_CM_REP_RCVD / IB_CM_MRA_REP_SENT path without a QP below,
work is freed and cma_force_disconnect() allocates a second struct
cma_work after the REJ has already been sent, losing the disconnect event
if the second allocation fails.

> +
> +	trace_cm_send_rej(id_priv);

[Severity: Low]
Doesn't calling trace_cm_send_rej() before ib_cm_reject_pending_connect()
emit a spurious cm_send_rej trace event on every normal disconnect of an
established IB/RoCE connection where no REJ is sent?

> +	if (ib_cm_reject_pending_connect(cm_id, &old_state)) {
> +		kfree(work);
> +		if (old_state == IB_CM_IDLE) {
> +			/*
> +			 * The connect already failed, e.g. the peer sent
> +			 * a REJ or our REQ timed out, and the pending
> +			 * REJECTED or UNREACHABLE event reports it, or
> +			 * it was already aborted.
> +			 */
> +			*ret = 0;
> +			return true;
> +		}
> +		return false;
> +	}

[Severity: High]
What happens if the peer rejected the pending connect and cm_rej_handler()
moved cm_id->state to IB_CM_TIMEWAIT instead of IB_CM_IDLE?

drivers/infiniband/core/cm.c:cm_rej_handler() {
	...
	case IB_CM_REQ_SENT:
	case IB_CM_MRA_REQ_RCVD:
		ib_cancel_mad(cm_id_priv->msg);
		if (IBA_GET(CM_REJ_REASON, rej_msg) == IB_CM_REJ_STALE_CONN)
			cm_enter_timewait(cm_id_priv);
		else
			cm_reset_to_idle(cm_id_priv);
		break;
	case IB_CM_DREQ_SENT:
		ib_cancel_mad(cm_id_priv->msg);
		fallthrough;
	case IB_CM_REP_RCVD:
	case IB_CM_MRA_REP_SENT:
		cm_enter_timewait(cm_id_priv);
		break;
	...
}

When a pending connect is rejected with IB_CM_REJ_STALE_CONN (or rejected
in IB_CM_REP_RCVD or IB_CM_MRA_REP_SENT), cm_id->state remains in
IB_CM_TIMEWAIT until the timewait timer expires.  Because only
old_state == IB_CM_IDLE is checked here, cma_ib_abort_pending_connect()
returns false and rdma_disconnect() falls through to
cma_force_disconnect(), queuing a spurious RDMA_CM_EVENT_DISCONNECTED for
a connection that was never established.

In xprtrdma, xprt_rdma_connect_worker() calls rpcrdma_xprt_disconnect() ->
rdma_disconnect() right after waking up from RDMA_CM_EVENT_REJECTED; in
rpcrdma_cm_event_handler(), receiving the subsequent
RDMA_CM_EVENT_DISCONNECTED calls rpcrdma_ep_put(ep) without a matching
rpcrdma_ep_get(ep) from RDMA_CM_EVENT_ESTABLISHED, causing a reference
count underflow and use-after-free of struct rpcrdma_ep.

> +
> +	switch (old_state) {
> +	case IB_CM_REQ_SENT:
> +	case IB_CM_MRA_REQ_RCVD:
> +		/*
> +		 * Still waiting for the REP, so no other event is pending
> +		 * for the connect: report it as failed connect. A REP
> +		 * arriving later is dropped by ib_cm.
> +		 */
> +		cma_queue_abort_work(id_priv, work);
> +		*ret = 0;
> +		return true;
> +	default:
> +		break;
> +	}
> +
> +	/*
> +	 * The REP arrived (IB_CM_REP_RCVD or IB_CM_MRA_REP_SENT). With a
> +	 * QP it is being processed by cma_ib_handler() under the
> +	 * handler_mutex: cma_rep_recv() fails, as the QP is in error or
> +	 * ib_send_cm_rtu() fails after the REJ, and the REP upcall
> +	 * reports RDMA_CM_EVENT_CONNECT_ERROR itself, so we must not
> +	 * queue a second event.
> +	 *
> +	 * Without a QP the caller got (or is about to get)
> +	 * RDMA_CM_EVENT_CONNECT_RESPONSE and its rdma_accept() fails
> +	 * after the REJ, so keep reporting RDMA_CM_EVENT_DISCONNECTED via
> +	 * cma_force_disconnect() as before, librdmacm ignores it if it
> +	 * created the QP.
> +	 */
> +	kfree(work);
> +	*ret = id_priv->id.qp ? 0 : cma_force_disconnect(id_priv);
> +	return true;
> +}

[Severity: Medium]
When old_state is IB_CM_REP_RCVD and id_priv->id.qp is NULL (such as for
userspace rdma_ucm connections), can cma_force_disconnect() race with
cma_ib_handler() and drop RDMA_CM_EVENT_CONNECT_RESPONSE?

CPU0 (cm.wq)                      CPU1
cm_rep_handler()
  lock(&cm_id_priv->lock)
  cm_id->state = IB_CM_REP_RCVD
  unlock(&cm_id_priv->lock)
                                  rdma_disconnect()
                                    cma_ib_abort_pending_connect()
                                      ib_cm_reject_pending_connect()
                                        old_state == IB_CM_REP_RCVD
                                      cma_force_disconnect()
                                        queue_work(cma_wq, &work->work)

                                  cma_work_handler()
                                    mutex_lock(&id_priv->handler_mutex)
                                    id_priv->state = RDMA_CM_DISCONNECT
                                    deliver RDMA_CM_EVENT_DISCONNECTED
                                    mutex_unlock(&id_priv->handler_mutex)
cma_ib_handler(IB_CM_REP_RECEIVED)
  mutex_lock(&id_priv->handler_mutex)
  state != RDMA_CM_CONNECT
  goto out;  <-- drops RDMA_CM_EVENT_CONNECT_RESPONSE

cm_rep_handler() sets cm_id->state to IB_CM_REP_RCVD and drops
cm_id_priv->lock before cma_ib_handler() acquires id_priv->handler_mutex
and calls ib_prepare_cm_mra() (which moves cm_id->state to
IB_CM_MRA_REP_SENT).  If cma_work_handler() runs first, cma_ib_handler()
drops IB_CM_REP_RECEIVED, and because librdmacm ignores
RDMA_CM_EVENT_DISCONNECTED when it manages the QP in userspace, the caller
never receives RDMA_CM_EVENT_CONNECT_RESPONSE or fails in rdma_accept().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796215022.git.metze@samba.org?part=3

  reply	other threads:[~2026-12-02 13:02 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 12:38 [PATCH 0/3] RDMA/cma: reject a pending IB/RoCE connect in rdma_disconnect() Stefan Metzmacher
2026-12-02 12:38 ` [PATCH 1/3] RDMA/cm: cancel the outstanding REQ or REP in ib_send_cm_rej() Stefan Metzmacher
2026-12-02 12:38 ` [PATCH 2/3] RDMA/cm: add ib_cm_reject_pending_connect() Stefan Metzmacher
2026-12-02 12:50   ` sashiko-bot
2026-12-02 12:38 ` [PATCH 3/3] RDMA/cma: reject a pending IB/RoCE connect in rdma_disconnect() Stefan Metzmacher
2026-12-02 13:02   ` sashiko-bot [this message]
2026-12-02 13:02 ` [PATCH 0/3] " sashiko-bot

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-245835@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=anna@kernel.org \
    --cc=cel@kernel.org \
    --cc=jlayton@kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=metze@samba.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.