mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Stefan Metzmacher <metze@samba.org>
To: linux-rdma@vger.kernel.org
Cc: metze@samba.org, stable@vger.kernel.org,
	Bernard Metzler <bernard.metzler@linux.dev>,
	Leon Romanovsky <leon@kernel.org>, Jason Gunthorpe <jgg@ziepe.ca>,
	Yunseong Kim <yunseong.kim@est.tech>,
	Yunseong Kim <yunseong.kim@ericsson.com>,
	Jacob Moroni <jmoroni@google.com>,
	Bart Van Assche <bvanassche@acm.org>,
	Ammar Ratnani <ammrat13@gmail.com>, Chuck Lever <cel@kernel.org>,
	Li Xiasong <lixiasong1@huawei.com>,
	Trond Myklebust <trondmy@kernel.org>,
	Trond Myklebust <trondmy@gmail.com>,
	Anna Schumaker <anna@kernel.org>,
	Jeff Layton <jlayton@kernel.org>, NeilBrown <neil@brown.name>,
	Olga Kornievskaia <okorniev@redhat.com>,
	Dai Ngo <Dai.Ngo@oracle.com>, Tom Talpey <tom@talpey.com>,
	Yue Haibing <yuehaibing@huawei.com>,
	Zhang Changzhong <zhangchangzhong@huawei.com>,
	Wei Yongjun <weiyongjun1@huawei.com>,
	Tim Menninger <tmenninger@everpuredata.com>,
	linux-nfs@vger.kernel.org, linux-cifs@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH 3/3] RDMA/cma: reject a pending IB/RoCE connect in rdma_disconnect()
Date: Wed,  2 Dec 2026 13:38:13 +0100	[thread overview]
Message-ID: <f4db1dba1e6a98cd357fc9ff66164349cd16fbc6.1796215022.git.metze@samba.org> (raw)
In-Reply-To: <cover.1796215022.git.metze@samba.org>

If rdma_disconnect() is called while an active IB/RoCE connect is
still waiting for the REP, ib_send_cm_dreq() and ib_send_cm_drep()
fail, as the ib_cm_id is not established. Since commit ba88a28b47a7
("RDMA/core: Force disconnect if DREP and DREQ fail") the caller then
gets RDMA_CM_EVENT_DISCONNECTED for a connection that was never
established, while the REQ is still pending in ib_cm and a REP
arriving later is ignored by cma without sending an RTU, so the peer
only notices via its own timeouts.

Instead reject the pending connect with ib_cm_reject_pending_connect(),
which checks the state and sends the REJ atomically, and report
RDMA_CM_EVENT_CONNECT_ERROR with -ECONNABORTED, moving to
RDMA_CM_DISCONNECT, so another rdma_disconnect() doesn't generate a
second event. The QP was already moved to error, so it can be
drained and destroyed.

So the caller, also in user space via rdma_ucm, gets the same kind
of event as for a connect rejected by the peer.

If the REP already arrived (IB_CM_REP_RCVD or, after
ib_prepare_cm_mra(), IB_CM_MRA_REP_SENT), cma_ib_handler() is
processing it under the handler_mutex. With a QP also reject it, but
don't queue an event: 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. Without a QP the caller got
RDMA_CM_EVENT_CONNECT_RESPONSE and its rdma_accept() fails after the
REJ, so it still gets RDMA_CM_EVENT_DISCONNECTED as before, which
librdmacm ignores if it created the QP.

If ib_cm is already in IB_CM_IDLE while the rdma_cm_id is still in
RDMA_CM_CONNECT, the connect already failed, e.g. the peer sent a REJ
or our REQ timed out, and the pending RDMA_CM_EVENT_REJECTED or
RDMA_CM_EVENT_UNREACHABLE reports it, so don't force a second event.

The work for the event is allocated before the REJ is sent, so that a
rejected connect is always reported.

On the passive side, if rdma_disconnect() is called after the REP was
sent, but before the RTU arrived, the caller still gets the forced
RDMA_CM_EVENT_DISCONNECTED, as before.

The new rdma_cma:cm_abort_connect trace event shows when this
happens.

Fixes: ba88a28b47a7 ("RDMA/core: Force disconnect if DREP and DREQ fail")
Cc: stable@vger.kernel.org
Cc: Bernard Metzler <bernard.metzler@linux.dev>
Cc: Leon Romanovsky <leon@kernel.org>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Yunseong Kim <yunseong.kim@est.tech>
Cc: Yunseong Kim <yunseong.kim@ericsson.com>
Cc: Jacob Moroni <jmoroni@google.com>
Cc: Bart Van Assche <bvanassche@acm.org>
Cc: Ammar Ratnani <ammrat13@gmail.com>
Cc: Chuck Lever <cel@kernel.org>
Cc: Li Xiasong <lixiasong1@huawei.com>
Cc: Trond Myklebust <trondmy@kernel.org>
Cc: Trond Myklebust <trondmy@gmail.com>
Cc: Anna Schumaker <anna@kernel.org>
Cc: Jeff Layton <jlayton@kernel.org>
Cc: NeilBrown <neil@brown.name>
Cc: Olga Kornievskaia <okorniev@redhat.com>
Cc: Dai Ngo <Dai.Ngo@oracle.com>
Cc: Tom Talpey <tom@talpey.com>
Cc: Yue Haibing <yuehaibing@huawei.com>
Cc: Zhang Changzhong <zhangchangzhong@huawei.com>
Cc: Wei Yongjun <weiyongjun1@huawei.com>
Cc: Tim Menninger <tmenninger@everpuredata.com>
Cc: linux-rdma@vger.kernel.org
Cc: linux-nfs@vger.kernel.org
Cc: linux-cifs@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Stefan Metzmacher <metze@samba.org>
---
 drivers/infiniband/core/cma.c       | 110 ++++++++++++++++++++++++++++
 drivers/infiniband/core/cma_trace.h |   1 +
 2 files changed, 111 insertions(+)

diff --git a/drivers/infiniband/core/cma.c b/drivers/infiniband/core/cma.c
index 337a49d1acf7..077a98cd2caa 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;
 }
 
+/*
+ * Called from rdma_disconnect() after the pending active connect was
+ * rejected locally: report it to the caller like any other failed
+ * connect.
+ */
+static struct cma_work *cma_alloc_abort_work(void)
+{
+	struct cma_work *work;
+
+	work = kzalloc_obj(*work);
+	if (!work)
+		return NULL;
+
+	INIT_WORK(&work->work, cma_work_handler);
+	/*
+	 * Like cma_force_disconnect() move to RDMA_CM_DISCONNECT, so
+	 * another rdma_disconnect() doesn't generate a second event.
+	 */
+	work->old_state = RDMA_CM_CONNECT;
+	work->new_state = RDMA_CM_DISCONNECT;
+	work->event.event = RDMA_CM_EVENT_CONNECT_ERROR;
+	work->event.status = -ECONNABORTED;
+
+	return work;
+}
+
+static void cma_queue_abort_work(struct rdma_id_private *id_priv,
+				 struct cma_work *work)
+{
+	cma_id_get(id_priv);
+	work->id = id_priv;
+
+	trace_cm_abort_connect(id_priv);
+
+	queue_work(cma_wq, &work->work);
+}
+
+/*
+ * 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;
+	}
+
+	trace_cm_send_rej(id_priv);
+	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;
+	}
+
+	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;
+}
+
 int rdma_disconnect(struct rdma_cm_id *id)
 {
 	struct rdma_id_private *id_priv;
@@ -4859,6 +4967,8 @@ int rdma_disconnect(struct rdma_cm_id *id)
 		ret = cma_modify_qp_err(id_priv);
 		if (ret)
 			goto out;
+		if (cma_ib_abort_pending_connect(id_priv, &ret))
+			goto out;
 		/* Initiate or respond to a disconnect. */
 		trace_cm_disconnect(id_priv);
 		if (!ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0)) {
diff --git a/drivers/infiniband/core/cma_trace.h b/drivers/infiniband/core/cma_trace.h
index 3d05d8e80856..b3f43c575851 100644
--- a/drivers/infiniband/core/cma_trace.h
+++ b/drivers/infiniband/core/cma_trace.h
@@ -62,6 +62,7 @@ DEFINE_CMA_FSM_EVENT(disconnect);
 DEFINE_CMA_FSM_EVENT(sent_drep);
 DEFINE_CMA_FSM_EVENT(sent_dreq);
 DEFINE_CMA_FSM_EVENT(force_disconnect);
+DEFINE_CMA_FSM_EVENT(abort_connect);
 DEFINE_CMA_FSM_EVENT(id_destroy);
 
 TRACE_EVENT(cm_id_attach,
-- 
2.43.0


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

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 12:38 [PATCH 0/3] " 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:38 ` Stefan Metzmacher [this message]

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=f4db1dba1e6a98cd357fc9ff66164349cd16fbc6.1796215022.git.metze@samba.org \
    --to=metze@samba.org \
    --cc=Dai.Ngo@oracle.com \
    --cc=ammrat13@gmail.com \
    --cc=anna@kernel.org \
    --cc=bernard.metzler@linux.dev \
    --cc=bvanassche@acm.org \
    --cc=cel@kernel.org \
    --cc=jgg@ziepe.ca \
    --cc=jlayton@kernel.org \
    --cc=jmoroni@google.com \
    --cc=leon@kernel.org \
    --cc=linux-cifs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=lixiasong1@huawei.com \
    --cc=neil@brown.name \
    --cc=okorniev@redhat.com \
    --cc=stable@vger.kernel.org \
    --cc=tmenninger@everpuredata.com \
    --cc=tom@talpey.com \
    --cc=trondmy@gmail.com \
    --cc=trondmy@kernel.org \
    --cc=weiyongjun1@huawei.com \
    --cc=yuehaibing@huawei.com \
    --cc=yunseong.kim@ericsson.com \
    --cc=yunseong.kim@est.tech \
    --cc=zhangchangzhong@huawei.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

all inboxes | Powered by JetHome®