[PATCH 2/3] RDMA/cm: add ib_cm_reject_pending_connect()
From: Stefan Metzmacher
Date: Wed Dec 02 2026 - 07:38:48 EST
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@xxxxxxxxxxxxxxx
Cc: Bernard Metzler <bernard.metzler@xxxxxxxxx>
Cc: Leon Romanovsky <leon@xxxxxxxxxx>
Cc: Jason Gunthorpe <jgg@xxxxxxxx>
Cc: Yunseong Kim <yunseong.kim@xxxxxxxx>
Cc: Yunseong Kim <yunseong.kim@xxxxxxxxxxxx>
Cc: Jacob Moroni <jmoroni@xxxxxxxxxx>
Cc: Bart Van Assche <bvanassche@xxxxxxx>
Cc: Ammar Ratnani <ammrat13@xxxxxxxxx>
Cc: Chuck Lever <cel@xxxxxxxxxx>
Cc: Li Xiasong <lixiasong1@xxxxxxxxxx>
Cc: Trond Myklebust <trondmy@xxxxxxxxxx>
Cc: Trond Myklebust <trondmy@xxxxxxxxx>
Cc: Anna Schumaker <anna@xxxxxxxxxx>
Cc: Jeff Layton <jlayton@xxxxxxxxxx>
Cc: NeilBrown <neil@xxxxxxxxxx>
Cc: Olga Kornievskaia <okorniev@xxxxxxxxxx>
Cc: Dai Ngo <Dai.Ngo@xxxxxxxxxx>
Cc: Tom Talpey <tom@xxxxxxxxxx>
Cc: Yue Haibing <yuehaibing@xxxxxxxxxx>
Cc: Zhang Changzhong <zhangchangzhong@xxxxxxxxxx>
Cc: Wei Yongjun <weiyongjun1@xxxxxxxxxx>
Cc: Tim Menninger <tmenninger@xxxxxxxxxxxxxxxx>
Cc: linux-rdma@xxxxxxxxxxxxxxx
Cc: linux-nfs@xxxxxxxxxxxxxxx
Cc: linux-cifs@xxxxxxxxxxxxxxx
Cc: linux-kernel@xxxxxxxxxxxxxxx
Signed-off-by: Stefan Metzmacher <metze@xxxxxxxxx>
---
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