Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
* [PATCH 0/3] RDMA/cma: reject a pending IB/RoCE connect in rdma_disconnect()
@ 2026-12-02 12:38 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
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Stefan Metzmacher @ 2026-12-02 12:38 UTC (permalink / raw)
  To: Jason Gunthorpe, Leon Romanovsky
  Cc: metze, Ammar Ratnani, Chuck Lever, Bernard Metzler, Yunseong Kim,
	Yunseong Kim, Jacob Moroni, Bart Van Assche, linux-rdma,
	linux-kernel, stable, Anna Schumaker, Dai Ngo, Jeff Layton,
	Li Xiasong, NeilBrown, Olga Kornievskaia, Tim Menninger,
	Tom Talpey, Trond Myklebust, Trond Myklebust, Wei Yongjun,
	Yue Haibing, Zhang Changzhong, linux-cifs, linux-nfs

Hi,

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 connection 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. RDMA_CM_CONNECT
covers both a pending and an established connection, so the rdma_cm
state doesn't distinguish them.

  - The first patch lets ib_send_cm_rej() cancel our outstanding REQ
    (or REP), otherwise a following rdma_destroy_id() waits about 76
    seconds until all retries of the REQ timed out.

  - The second patch adds ib_cm_reject_pending_connect(), which
    rejects a pending active connect and returns the former state,
    with the check and the REJ done atomically, so the caller knows
    whether a REP arrived in between.

  - The third patch uses it in rdma_disconnect(): a connect still
    waiting for the REP is rejected and reported as
    RDMA_CM_EVENT_CONNECT_ERROR with -ECONNABORTED. If the REP is already being processed, only the REJ
    is sent, as the REP upcall reports the failed connect itself.
    Without a QP (rdma_ucm), the caller keeps getting
    RDMA_CM_EVENT_DISCONNECTED as before. If ib_cm is already idle, a
    pending REJECTED or UNREACHABLE event reports the failed connect,
    so no second event is forced. An established connection still
    gets the forced RDMA_CM_EVENT_DISCONNECTED.

This came up in the discussion about ba88a28b47a7 with Ammar and Chuck:
https://lore.kernel.org/linux-nfs/f491838c-f364-41a3-b886-ec75d281bf03@gmail.com/
A larger series lets rdma_disconnect() abort a connection setup for
iWarp as well, on top of these two patches, I'll post it later.

These patches were developed with the help of Claude (Anthropic),
which was used for code analysis, writing parts of the code and the
commit messages. I reviewed all changes and take full responsibility
for them.

Testing: two hosts with rxe, Intel E810 (irdma) and ConnectX (mlx5) in
RoCEv2 mode: SMB over RDMA (cifs.ko/ksmbd.ko) connects against a
listener that never answers, aborted after about 5 seconds and by
killing the mounting processes, without waiting for the REQ retries,
and a user space rdma_cm test that aborts pending connects with
rdma_disconnect() or by destroying the QP or the rdma_cm_id first.
The established case (address removed or link down while connected)
works as before.

Base: linux-rdma/for-next (a8671ee9bde1).

Stefan Metzmacher (3):
  RDMA/cm: cancel the outstanding REQ or REP in ib_send_cm_rej()
  RDMA/cm: add ib_cm_reject_pending_connect()
  RDMA/cma: reject a pending IB/RoCE connect in rdma_disconnect()

 drivers/infiniband/core/cm.c        |  45 +++++++++++-
 drivers/infiniband/core/cma.c       | 110 ++++++++++++++++++++++++++++
 drivers/infiniband/core/cma_trace.h |   1 +
 include/rdma/ib_cm.h                |  17 +++++
 4 files changed, 171 insertions(+), 2 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] RDMA/cm: cancel the outstanding REQ or REP in ib_send_cm_rej()
  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 ` 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 ` [PATCH 3/3] RDMA/cma: reject a pending IB/RoCE connect in rdma_disconnect() Stefan Metzmacher
  2 siblings, 0 replies; 4+ messages in thread
From: Stefan Metzmacher @ 2026-12-02 12:38 UTC (permalink / raw)
  To: linux-rdma
  Cc: metze, stable, Bernard Metzler, Leon Romanovsky, Jason Gunthorpe,
	Yunseong Kim, Yunseong Kim, Jacob Moroni, Bart Van Assche,
	Ammar Ratnani, Chuck Lever, Li Xiasong, Trond Myklebust,
	Trond Myklebust, Anna Schumaker, Jeff Layton, NeilBrown,
	Olga Kornievskaia, Dai Ngo, Tom Talpey, Yue Haibing,
	Zhang Changzhong, Wei Yongjun, Tim Menninger, linux-nfs,
	linux-cifs, linux-kernel

ib_send_cm_rej() is allowed in IB_CM_REQ_SENT and IB_CM_MRA_REQ_RCVD,
but it only moves the cm_id to IB_CM_IDLE and sends the REJ, while our
REQ stays outstanding. It is retried and its send buffer keeps a
reference on the cm_id until all retries timed out, so a following
ib_destroy_cm_id(), which finds IB_CM_IDLE and doesn't cancel anything,
waits that long in cm_destroy_id(), about 76 seconds with the rdma_cm
defaults. The peer also keeps getting the REQ after the REJ.

This is needed for the following patch "RDMA/cma: reject a pending
IB/RoCE connect in rdma_disconnect()", which aborts a connect without
an answer from the peer, when the rdma_cm_id is destroyed afterwards:

  cm_destroy_id+0x23b/0x630 [ib_cm]
  ib_destroy_cm_id+0x10/0x20 [ib_cm]
  _destroy_id+0xd8/0x390 [rdma_cm]
  destroy_id_handler_unlock+0x56/0xb0 [rdma_cm]
  rdma_destroy_id+0x21/0x30 [rdma_cm]
  smbdirect_socket_destroy_sync+0x48f/0x9c0 [smbdirect]

cm_destroy_id() already cancelled the outstanding REQ (and our REP in
IB_CM_REP_SENT and IB_CM_MRA_REP_RCVD) before calling
cm_send_rej_locked(), so move that into cm_send_rej_locked() for all
callers.

Fixes: a977049dacde ("[PATCH] IB: Add the kernel CM implementation")
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/cm.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/drivers/infiniband/core/cm.c b/drivers/infiniband/core/cm.c
index 5347b7519a8d..d16fc71ed5e6 100644
--- a/drivers/infiniband/core/cm.c
+++ b/drivers/infiniband/core/cm.c
@@ -1095,7 +1095,6 @@ static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
 		break;
 	case IB_CM_REQ_SENT:
 	case IB_CM_MRA_REQ_RCVD:
-		ib_cancel_mad(cm_id_priv->msg);
 		cm_send_rej_locked(cm_id_priv, IB_CM_REJ_TIMEOUT,
 				   &cm_id_priv->id.device->node_guid,
 				   sizeof(cm_id_priv->id.device->node_guid),
@@ -1113,7 +1112,6 @@ static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
 		break;
 	case IB_CM_REP_SENT:
 	case IB_CM_MRA_REP_RCVD:
-		ib_cancel_mad(cm_id_priv->msg);
 		cm_send_rej_locked(cm_id_priv, IB_CM_REJ_CONSUMER_DEFINED, NULL,
 				   0, NULL, 0);
 		goto retest;
@@ -2965,6 +2963,13 @@ static int cm_send_rej_locked(struct cm_id_private *cm_id_priv,
 	switch (state) {
 	case IB_CM_REQ_SENT:
 	case IB_CM_MRA_REQ_RCVD:
+		/*
+		 * Our REQ is still outstanding, it would otherwise be
+		 * retried and keep a reference on the cm_id until all
+		 * retries timed out.
+		 */
+		ib_cancel_mad(cm_id_priv->msg);
+		fallthrough;
 	case IB_CM_REQ_RCVD:
 	case IB_CM_MRA_REQ_SENT:
 	case IB_CM_REP_RCVD:
@@ -2979,6 +2984,8 @@ static int cm_send_rej_locked(struct cm_id_private *cm_id_priv,
 		break;
 	case IB_CM_REP_SENT:
 	case IB_CM_MRA_REP_RCVD:
+		/* the same for our outstanding REP */
+		ib_cancel_mad(cm_id_priv->msg);
 		cm_enter_timewait(cm_id_priv);
 		msg = cm_alloc_msg(cm_id_priv);
 		if (IS_ERR(msg))
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/3] RDMA/cm: add ib_cm_reject_pending_connect()
  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 ` Stefan Metzmacher
  2026-12-02 12:38 ` [PATCH 3/3] RDMA/cma: reject a pending IB/RoCE connect in rdma_disconnect() Stefan Metzmacher
  2 siblings, 0 replies; 4+ messages in thread
From: Stefan Metzmacher @ 2026-12-02 12:38 UTC (permalink / raw)
  To: linux-rdma
  Cc: metze, stable, Bernard Metzler, Leon Romanovsky, Jason Gunthorpe,
	Yunseong Kim, Yunseong Kim, Jacob Moroni, Bart Van Assche,
	Ammar Ratnani, Chuck Lever, Li Xiasong, Trond Myklebust,
	Trond Myklebust, Anna Schumaker, Jeff Layton, NeilBrown,
	Olga Kornievskaia, Dai Ngo, Tom Talpey, Yue Haibing,
	Zhang Changzhong, Wei Yongjun, Tim Menninger, linux-nfs,
	linux-cifs, linux-kernel

rdma_disconnect() needs to know whether an active connect it aborts
was still waiting for the REP or whether the REP already arrived, as
in the latter case the REP upcall reports the failed connect itself.

Checking cm_id->state before calling ib_send_cm_rej() is racy, as
ib_send_cm_rej() also succeeds in IB_CM_REP_RCVD and
IB_CM_MRA_REP_SENT, so a REP arriving in between would be rejected
without the caller noticing.

Add ib_cm_reject_pending_connect(), which only rejects a pending
active connect and returns the former state, with the check and the
REJ done under cm_id_priv->lock. As cm_send_rej_locked() moves the
cm_id to IB_CM_IDLE before sending the REJ, the connect counts as
aborted even if sending the REJ fails.

This is needed for the next patch "RDMA/cma: reject a pending IB/RoCE
connect in rdma_disconnect()".

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/cm.c | 34 ++++++++++++++++++++++++++++++++++
 include/rdma/ib_cm.h         | 17 +++++++++++++++++
 2 files changed, 51 insertions(+)

diff --git a/drivers/infiniband/core/cm.c b/drivers/infiniband/core/cm.c
index d16fc71ed5e6..9d43097a6ef0 100644
--- a/drivers/infiniband/core/cm.c
+++ b/drivers/infiniband/core/cm.c
@@ -3025,6 +3025,40 @@ int ib_send_cm_rej(struct ib_cm_id *cm_id, enum ib_cm_rej_reason reason,
 }
 EXPORT_SYMBOL(ib_send_cm_rej);
 
+int ib_cm_reject_pending_connect(struct ib_cm_id *cm_id,
+				 enum ib_cm_state *old_state)
+{
+	struct cm_id_private *cm_id_priv =
+		container_of(cm_id, struct cm_id_private, id);
+	unsigned long flags;
+	int ret = -EINVAL;
+
+	spin_lock_irqsave(&cm_id_priv->lock, flags);
+	*old_state = cm_id->state;
+	switch (cm_id->state) {
+	case IB_CM_REQ_SENT:
+	case IB_CM_MRA_REQ_RCVD:
+	case IB_CM_REP_RCVD:
+	case IB_CM_MRA_REP_SENT:
+		/*
+		 * cm_send_rej_locked() moves the cm_id to IB_CM_IDLE
+		 * before it sends the REJ, so the connect is aborted
+		 * even if sending the REJ fails, the peer then
+		 * notices via its own timeout.
+		 */
+		cm_send_rej_locked(cm_id_priv, IB_CM_REJ_CONSUMER_DEFINED,
+				   NULL, 0, NULL, 0);
+		ret = 0;
+		break;
+	default:
+		break;
+	}
+	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
+
+	return ret;
+}
+EXPORT_SYMBOL(ib_cm_reject_pending_connect);
+
 static void cm_format_rej_event(struct cm_work *work)
 {
 	struct cm_rej_msg *rej_msg;
diff --git a/include/rdma/ib_cm.h b/include/rdma/ib_cm.h
index 4808a355de41..87071479f194 100644
--- a/include/rdma/ib_cm.h
+++ b/include/rdma/ib_cm.h
@@ -480,6 +480,23 @@ int ib_send_cm_rej(struct ib_cm_id *cm_id,
 		   const void *private_data,
 		   u8 private_data_len);
 
+/**
+ * ib_cm_reject_pending_connect - Rejects an active connect that is not
+ *   established yet.
+ * @cm_id: Connection identifier of the active side.
+ * @old_state: Returns the state of @cm_id before.
+ *
+ * Unlike ib_send_cm_rej() this only rejects in IB_CM_REQ_SENT,
+ * IB_CM_MRA_REQ_RCVD, IB_CM_REP_RCVD and IB_CM_MRA_REP_SENT, and the
+ * state check and the REJ are done atomically, so the caller knows
+ * whether a REP arrived before. @cm_id is in IB_CM_IDLE afterwards,
+ * even if the REJ could not be sent.
+ *
+ * Returns 0 if the connect was rejected, -EINVAL otherwise.
+ */
+int ib_cm_reject_pending_connect(struct ib_cm_id *cm_id,
+				 enum ib_cm_state *old_state);
+
 /**
  * ib_prepare_cm_mra - Prepares to send a message receipt acknowledgment to a
  *   connection message in case duplicates are received.
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 3/3] RDMA/cma: reject a pending IB/RoCE connect in rdma_disconnect()
  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:38 ` Stefan Metzmacher
  2 siblings, 0 replies; 4+ messages in thread
From: Stefan Metzmacher @ 2026-12-02 12:38 UTC (permalink / raw)
  To: linux-rdma
  Cc: metze, stable, Bernard Metzler, Leon Romanovsky, Jason Gunthorpe,
	Yunseong Kim, Yunseong Kim, Jacob Moroni, Bart Van Assche,
	Ammar Ratnani, Chuck Lever, Li Xiasong, Trond Myklebust,
	Trond Myklebust, Anna Schumaker, Jeff Layton, NeilBrown,
	Olga Kornievskaia, Dai Ngo, Tom Talpey, Yue Haibing,
	Zhang Changzhong, Wei Yongjun, Tim Menninger, linux-nfs,
	linux-cifs, linux-kernel

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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-02 12:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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:38 ` [PATCH 3/3] RDMA/cma: reject a pending IB/RoCE connect in rdma_disconnect() Stefan Metzmacher

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox